1 |
|
|
/* |
2 |
|
|
* H.261 decoder |
3 |
|
|
* Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> |
4 |
|
|
* Copyright (c) 2004 Maarten Daniels |
5 |
|
|
* |
6 |
|
|
* This file is part of FFmpeg. |
7 |
|
|
* |
8 |
|
|
* FFmpeg is free software; you can redistribute it and/or |
9 |
|
|
* modify it under the terms of the GNU Lesser General Public |
10 |
|
|
* License as published by the Free Software Foundation; either |
11 |
|
|
* version 2.1 of the License, or (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* FFmpeg is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
|
|
* Lesser General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU Lesser General Public |
19 |
|
|
* License along with FFmpeg; if not, write to the Free Software |
20 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* @file |
25 |
|
|
* H.261 decoder. |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
#include "libavutil/avassert.h" |
29 |
|
|
#include "avcodec.h" |
30 |
|
|
#include "mpeg_er.h" |
31 |
|
|
#include "mpegutils.h" |
32 |
|
|
#include "mpegvideo.h" |
33 |
|
|
#include "h263.h" |
34 |
|
|
#include "h261.h" |
35 |
|
|
#include "internal.h" |
36 |
|
|
|
37 |
|
|
#define H261_MBA_VLC_BITS 9 |
38 |
|
|
#define H261_MTYPE_VLC_BITS 6 |
39 |
|
|
#define H261_MV_VLC_BITS 7 |
40 |
|
|
#define H261_CBP_VLC_BITS 9 |
41 |
|
|
#define TCOEFF_VLC_BITS 9 |
42 |
|
|
#define MBA_STUFFING 33 |
43 |
|
|
#define MBA_STARTCODE 34 |
44 |
|
|
|
45 |
|
|
static VLC h261_mba_vlc; |
46 |
|
|
static VLC h261_mtype_vlc; |
47 |
|
|
static VLC h261_mv_vlc; |
48 |
|
|
static VLC h261_cbp_vlc; |
49 |
|
|
|
50 |
|
13 |
static av_cold void h261_decode_init_vlc(H261Context *h) |
51 |
|
|
{ |
52 |
|
|
static int done = 0; |
53 |
|
|
|
54 |
✓✓ |
13 |
if (!done) { |
55 |
|
7 |
done = 1; |
56 |
|
7 |
INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35, |
57 |
|
|
ff_h261_mba_bits, 1, 1, |
58 |
|
|
ff_h261_mba_code, 1, 1, 662); |
59 |
|
7 |
INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10, |
60 |
|
|
ff_h261_mtype_bits, 1, 1, |
61 |
|
|
ff_h261_mtype_code, 1, 1, 80); |
62 |
|
7 |
INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17, |
63 |
|
|
&ff_h261_mv_tab[0][1], 2, 1, |
64 |
|
|
&ff_h261_mv_tab[0][0], 2, 1, 144); |
65 |
|
7 |
INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63, |
66 |
|
|
&ff_h261_cbp_tab[0][1], 2, 1, |
67 |
|
|
&ff_h261_cbp_tab[0][0], 2, 1, 512); |
68 |
✓✗✓✓
|
231 |
INIT_VLC_RL(ff_h261_rl_tcoeff, 552); |
69 |
|
|
} |
70 |
|
13 |
} |
71 |
|
|
|
72 |
|
13 |
static av_cold int h261_decode_init(AVCodecContext *avctx) |
73 |
|
|
{ |
74 |
|
13 |
H261Context *h = avctx->priv_data; |
75 |
|
13 |
MpegEncContext *const s = &h->s; |
76 |
|
|
|
77 |
|
|
// set defaults |
78 |
|
13 |
ff_mpv_decode_init(s, avctx); |
79 |
|
|
|
80 |
|
13 |
s->out_format = FMT_H261; |
81 |
|
13 |
s->low_delay = 1; |
82 |
|
13 |
avctx->pix_fmt = AV_PIX_FMT_YUV420P; |
83 |
|
|
|
84 |
|
13 |
ff_h261_common_init(); |
85 |
|
13 |
h261_decode_init_vlc(h); |
86 |
|
|
|
87 |
|
13 |
h->gob_start_code_skipped = 0; |
88 |
|
|
|
89 |
|
13 |
return 0; |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
/** |
93 |
|
|
* Decode the group of blocks header or slice header. |
94 |
|
|
* @return <0 if an error occurred |
95 |
|
|
*/ |
96 |
|
3600 |
static int h261_decode_gob_header(H261Context *h) |
97 |
|
|
{ |
98 |
|
|
unsigned int val; |
99 |
|
3600 |
MpegEncContext *const s = &h->s; |
100 |
|
|
|
101 |
✓✓ |
3600 |
if (!h->gob_start_code_skipped) { |
102 |
|
|
/* Check for GOB Start Code */ |
103 |
|
300 |
val = show_bits(&s->gb, 15); |
104 |
✗✓ |
300 |
if (val) |
105 |
|
|
return -1; |
106 |
|
|
|
107 |
|
|
/* We have a GBSC */ |
108 |
|
300 |
skip_bits(&s->gb, 16); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
3600 |
h->gob_start_code_skipped = 0; |
112 |
|
|
|
113 |
|
3600 |
h->gob_number = get_bits(&s->gb, 4); /* GN */ |
114 |
|
3600 |
s->qscale = get_bits(&s->gb, 5); /* GQUANT */ |
115 |
|
|
|
116 |
|
|
/* Check if gob_number is valid */ |
117 |
✓✗ |
3600 |
if (s->mb_height == 18) { // CIF |
118 |
✓✗✗✓
|
3600 |
if ((h->gob_number <= 0) || (h->gob_number > 12)) |
119 |
|
|
return -1; |
120 |
|
|
} else { // QCIF |
121 |
|
|
if ((h->gob_number != 1) && (h->gob_number != 3) && |
122 |
|
|
(h->gob_number != 5)) |
123 |
|
|
return -1; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
/* GEI */ |
127 |
✗✓ |
3600 |
if (skip_1stop_8data_bits(&s->gb) < 0) |
128 |
|
|
return AVERROR_INVALIDDATA; |
129 |
|
|
|
130 |
✗✓ |
3600 |
if (s->qscale == 0) { |
131 |
|
|
av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n"); |
132 |
|
|
if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT)) |
133 |
|
|
return -1; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/* For the first transmitted macroblock in a GOB, MBA is the absolute |
137 |
|
|
* address. For subsequent macroblocks, MBA is the difference between |
138 |
|
|
* the absolute addresses of the macroblock and the last transmitted |
139 |
|
|
* macroblock. */ |
140 |
|
3600 |
h->current_mba = 0; |
141 |
|
3600 |
h->mba_diff = 0; |
142 |
|
|
|
143 |
|
3600 |
return 0; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
/** |
147 |
|
|
* Decode the group of blocks / video packet header. |
148 |
|
|
* @return <0 if no resync found |
149 |
|
|
*/ |
150 |
|
3600 |
static int h261_resync(H261Context *h) |
151 |
|
|
{ |
152 |
|
3600 |
MpegEncContext *const s = &h->s; |
153 |
|
|
int left, ret; |
154 |
|
|
|
155 |
✓✓ |
3600 |
if (h->gob_start_code_skipped) { |
156 |
|
3300 |
ret = h261_decode_gob_header(h); |
157 |
✓✗ |
3300 |
if (ret >= 0) |
158 |
|
3300 |
return 0; |
159 |
|
|
} else { |
160 |
✓✗ |
300 |
if (show_bits(&s->gb, 15) == 0) { |
161 |
|
300 |
ret = h261_decode_gob_header(h); |
162 |
✓✗ |
300 |
if (ret >= 0) |
163 |
|
300 |
return 0; |
164 |
|
|
} |
165 |
|
|
// OK, it is not where it is supposed to be ... |
166 |
|
|
s->gb = s->last_resync_gb; |
167 |
|
|
align_get_bits(&s->gb); |
168 |
|
|
left = get_bits_left(&s->gb); |
169 |
|
|
|
170 |
|
|
for (; left > 15 + 1 + 4 + 5; left -= 8) { |
171 |
|
|
if (show_bits(&s->gb, 15) == 0) { |
172 |
|
|
GetBitContext bak = s->gb; |
173 |
|
|
|
174 |
|
|
ret = h261_decode_gob_header(h); |
175 |
|
|
if (ret >= 0) |
176 |
|
|
return 0; |
177 |
|
|
|
178 |
|
|
s->gb = bak; |
179 |
|
|
} |
180 |
|
|
skip_bits(&s->gb, 8); |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
return -1; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
/** |
188 |
|
|
* Decode skipped macroblocks. |
189 |
|
|
* @return 0 |
190 |
|
|
*/ |
191 |
|
121514 |
static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2) |
192 |
|
|
{ |
193 |
|
121514 |
MpegEncContext *const s = &h->s; |
194 |
|
|
int i; |
195 |
|
|
|
196 |
|
121514 |
s->mb_intra = 0; |
197 |
|
|
|
198 |
✓✓ |
122400 |
for (i = mba1; i < mba2; i++) { |
199 |
|
|
int j, xy; |
200 |
|
|
|
201 |
|
886 |
s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11; |
202 |
|
886 |
s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11; |
203 |
|
886 |
xy = s->mb_x + s->mb_y * s->mb_stride; |
204 |
|
886 |
ff_init_block_index(s); |
205 |
|
886 |
ff_update_block_index(s); |
206 |
|
|
|
207 |
✓✓ |
6202 |
for (j = 0; j < 6; j++) |
208 |
|
5316 |
s->block_last_index[j] = -1; |
209 |
|
|
|
210 |
|
886 |
s->mv_dir = MV_DIR_FORWARD; |
211 |
|
886 |
s->mv_type = MV_TYPE_16X16; |
212 |
|
886 |
s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0; |
213 |
|
886 |
s->mv[0][0][0] = 0; |
214 |
|
886 |
s->mv[0][0][1] = 0; |
215 |
|
886 |
s->mb_skipped = 1; |
216 |
|
886 |
h->mtype &= ~MB_TYPE_H261_FIL; |
217 |
|
|
|
218 |
✗✓ |
886 |
if (s->current_picture.motion_val[0]) { |
219 |
|
|
int b_stride = 2*s->mb_width + 1; |
220 |
|
|
int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride; |
221 |
|
|
s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0]; |
222 |
|
|
s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1]; |
223 |
|
|
} |
224 |
|
|
|
225 |
|
886 |
ff_mpv_reconstruct_mb(s, s->block); |
226 |
|
|
} |
227 |
|
|
|
228 |
|
121514 |
return 0; |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
static const int mvmap[17] = { |
232 |
|
|
0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16 |
233 |
|
|
}; |
234 |
|
|
|
235 |
|
193928 |
static int decode_mv_component(GetBitContext *gb, int v) |
236 |
|
|
{ |
237 |
|
193928 |
int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2); |
238 |
|
|
|
239 |
|
|
/* check if mv_diff is valid */ |
240 |
✗✓ |
193928 |
if (mv_diff < 0) |
241 |
|
|
return v; |
242 |
|
|
|
243 |
|
193928 |
mv_diff = mvmap[mv_diff]; |
244 |
|
|
|
245 |
✓✓✓✓
|
193928 |
if (mv_diff && !get_bits1(gb)) |
246 |
|
37209 |
mv_diff = -mv_diff; |
247 |
|
|
|
248 |
|
193928 |
v += mv_diff; |
249 |
✓✓ |
193928 |
if (v <= -16) |
250 |
|
36 |
v += 32; |
251 |
✓✓ |
193892 |
else if (v >= 16) |
252 |
|
30 |
v -= 32; |
253 |
|
|
|
254 |
|
193928 |
return v; |
255 |
|
|
} |
256 |
|
|
|
257 |
|
|
/** |
258 |
|
|
* Decode a macroblock. |
259 |
|
|
* @return <0 if an error occurred |
260 |
|
|
*/ |
261 |
|
590082 |
static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded) |
262 |
|
|
{ |
263 |
|
590082 |
MpegEncContext *const s = &h->s; |
264 |
|
|
int level, i, j, run; |
265 |
|
590082 |
RLTable *rl = &ff_h261_rl_tcoeff; |
266 |
|
|
const uint8_t *scan_table; |
267 |
|
|
|
268 |
|
|
/* For the variable length encoding there are two code tables, one being |
269 |
|
|
* used for the first transmitted LEVEL in INTER, INTER + MC and |
270 |
|
|
* INTER + MC + FIL blocks, the second for all other LEVELs except the |
271 |
|
|
* first one in INTRA blocks which is fixed length coded with 8 bits. |
272 |
|
|
* NOTE: The two code tables only differ in one VLC so we handle that |
273 |
|
|
* manually. */ |
274 |
|
590082 |
scan_table = s->intra_scantable.permutated; |
275 |
✓✓ |
590082 |
if (s->mb_intra) { |
276 |
|
|
/* DC coef */ |
277 |
|
106194 |
level = get_bits(&s->gb, 8); |
278 |
|
|
// 0 (00000000b) and -128 (10000000b) are FORBIDDEN |
279 |
✗✓ |
106194 |
if ((level & 0x7F) == 0) { |
280 |
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", |
281 |
|
|
level, s->mb_x, s->mb_y); |
282 |
|
|
return -1; |
283 |
|
|
} |
284 |
|
|
/* The code 1000 0000 is not used, the reconstruction level of 1024 |
285 |
|
|
* being coded as 1111 1111. */ |
286 |
✓✓ |
106194 |
if (level == 255) |
287 |
|
1211 |
level = 128; |
288 |
|
106194 |
block[0] = level; |
289 |
|
106194 |
i = 1; |
290 |
✓✓ |
483888 |
} else if (coded) { |
291 |
|
|
// Run Level Code |
292 |
|
|
// EOB Not possible for first level when cbp is available (that's why the table is different) |
293 |
|
|
// 0 1 1s |
294 |
|
|
// * * 0* |
295 |
|
242540 |
int check = show_bits(&s->gb, 2); |
296 |
|
242540 |
i = 0; |
297 |
✓✓ |
242540 |
if (check & 0x2) { |
298 |
|
81615 |
skip_bits(&s->gb, 2); |
299 |
✓✓ |
81615 |
block[0] = (check & 0x1) ? -1 : 1; |
300 |
|
81615 |
i = 1; |
301 |
|
|
} |
302 |
|
|
} else { |
303 |
|
241348 |
i = 0; |
304 |
|
|
} |
305 |
✓✓ |
590082 |
if (!coded) { |
306 |
|
241348 |
s->block_last_index[n] = i - 1; |
307 |
|
241348 |
return 0; |
308 |
|
|
} |
309 |
|
|
{ |
310 |
|
348734 |
OPEN_READER(re, &s->gb); |
311 |
|
348734 |
i--; // offset by -1 to allow direct indexing of scan_table |
312 |
|
|
for (;;) { |
313 |
|
2586037 |
UPDATE_CACHE(re, &s->gb); |
314 |
✓✓ |
2586037 |
GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TCOEFF_VLC_BITS, 2, 0); |
315 |
✓✓ |
2586037 |
if (run == 66) { |
316 |
✗✓ |
65180 |
if (level) { |
317 |
|
|
CLOSE_READER(re, &s->gb); |
318 |
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", |
319 |
|
|
s->mb_x, s->mb_y); |
320 |
|
|
return -1; |
321 |
|
|
} |
322 |
|
|
/* escape */ |
323 |
|
|
/* The remaining combinations of (run, level) are encoded with a |
324 |
|
|
* 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits |
325 |
|
|
* level. */ |
326 |
|
65180 |
run = SHOW_UBITS(re, &s->gb, 6) + 1; |
327 |
|
65180 |
SKIP_CACHE(re, &s->gb, 6); |
328 |
|
65180 |
level = SHOW_SBITS(re, &s->gb, 8); |
329 |
|
65180 |
SKIP_COUNTER(re, &s->gb, 6 + 8); |
330 |
✓✓ |
2520857 |
} else if (level == 0) { |
331 |
|
348734 |
break; |
332 |
|
|
} else { |
333 |
✓✓ |
2172123 |
if (SHOW_UBITS(re, &s->gb, 1)) |
334 |
|
1087153 |
level = -level; |
335 |
|
2172123 |
SKIP_COUNTER(re, &s->gb, 1); |
336 |
|
|
} |
337 |
|
2237303 |
i += run; |
338 |
✗✓ |
2237303 |
if (i >= 64) { |
339 |
|
|
CLOSE_READER(re, &s->gb); |
340 |
|
|
av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", |
341 |
|
|
s->mb_x, s->mb_y); |
342 |
|
|
return -1; |
343 |
|
|
} |
344 |
|
2237303 |
j = scan_table[i]; |
345 |
|
2237303 |
block[j] = level; |
346 |
|
|
} |
347 |
|
348734 |
CLOSE_READER(re, &s->gb); |
348 |
|
|
} |
349 |
|
348734 |
s->block_last_index[n] = i; |
350 |
|
348734 |
return 0; |
351 |
|
|
} |
352 |
|
|
|
353 |
|
121514 |
static int h261_decode_mb(H261Context *h) |
354 |
|
|
{ |
355 |
|
121514 |
MpegEncContext *const s = &h->s; |
356 |
|
|
int i, cbp, xy; |
357 |
|
|
|
358 |
|
121514 |
cbp = 63; |
359 |
|
|
// Read mba |
360 |
|
|
do { |
361 |
|
121514 |
h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, |
362 |
|
|
H261_MBA_VLC_BITS, 2); |
363 |
|
|
|
364 |
|
|
/* Check for slice end */ |
365 |
|
|
/* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */ |
366 |
✓✓ |
121514 |
if (h->mba_diff == MBA_STARTCODE) { // start code |
367 |
|
3300 |
h->gob_start_code_skipped = 1; |
368 |
|
3300 |
return SLICE_END; |
369 |
|
|
} |
370 |
✗✓ |
118214 |
} while (h->mba_diff == MBA_STUFFING); // stuffing |
371 |
|
|
|
372 |
✓✓ |
118214 |
if (h->mba_diff < 0) { |
373 |
✓✗ |
300 |
if (get_bits_left(&s->gb) <= 7) |
374 |
|
300 |
return SLICE_END; |
375 |
|
|
|
376 |
|
|
av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y); |
377 |
|
|
return SLICE_ERROR; |
378 |
|
|
} |
379 |
|
|
|
380 |
|
117914 |
h->mba_diff += 1; |
381 |
|
117914 |
h->current_mba += h->mba_diff; |
382 |
|
|
|
383 |
✗✓ |
117914 |
if (h->current_mba > MBA_STUFFING) |
384 |
|
|
return SLICE_ERROR; |
385 |
|
|
|
386 |
|
117914 |
s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11); |
387 |
|
117914 |
s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11); |
388 |
|
117914 |
xy = s->mb_x + s->mb_y * s->mb_stride; |
389 |
|
117914 |
ff_init_block_index(s); |
390 |
|
117914 |
ff_update_block_index(s); |
391 |
|
|
|
392 |
|
|
// Read mtype |
393 |
|
117914 |
h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2); |
394 |
✗✓ |
117914 |
if (h->mtype < 0) { |
395 |
|
|
av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n", |
396 |
|
|
h->mtype); |
397 |
|
|
return SLICE_ERROR; |
398 |
|
|
} |
399 |
✗✓ |
117914 |
av_assert0(h->mtype < FF_ARRAY_ELEMS(ff_h261_mtype_map)); |
400 |
|
117914 |
h->mtype = ff_h261_mtype_map[h->mtype]; |
401 |
|
|
|
402 |
|
|
// Read mquant |
403 |
✗✓ |
117914 |
if (IS_QUANT(h->mtype)) |
404 |
|
|
ff_set_qscale(s, get_bits(&s->gb, 5)); |
405 |
|
|
|
406 |
|
117914 |
s->mb_intra = IS_INTRA4x4(h->mtype); |
407 |
|
|
|
408 |
|
|
// Read mv |
409 |
✓✓ |
117914 |
if (IS_16X16(h->mtype)) { |
410 |
|
|
/* Motion vector data is included for all MC macroblocks. MVD is |
411 |
|
|
* obtained from the macroblock vector by subtracting the vector |
412 |
|
|
* of the preceding macroblock. For this calculation the vector |
413 |
|
|
* of the preceding macroblock is regarded as zero in the |
414 |
|
|
* following three situations: |
415 |
|
|
* 1) evaluating MVD for macroblocks 1, 12 and 23; |
416 |
|
|
* 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1; |
417 |
|
|
* 3) MTYPE of the previous macroblock was not MC. */ |
418 |
✓✓✓✓
|
96964 |
if ((h->current_mba == 1) || (h->current_mba == 12) || |
419 |
✓✓✓✓
|
91569 |
(h->current_mba == 23) || (h->mba_diff != 1)) { |
420 |
|
8682 |
h->current_mv_x = 0; |
421 |
|
8682 |
h->current_mv_y = 0; |
422 |
|
|
} |
423 |
|
|
|
424 |
|
96964 |
h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x); |
425 |
|
96964 |
h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y); |
426 |
|
|
} else { |
427 |
|
20950 |
h->current_mv_x = 0; |
428 |
|
20950 |
h->current_mv_y = 0; |
429 |
|
|
} |
430 |
|
|
|
431 |
|
|
// Read cbp |
432 |
✓✓ |
117914 |
if (HAS_CBP(h->mtype)) |
433 |
|
80648 |
cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 1) + 1; |
434 |
|
|
|
435 |
✓✓ |
117914 |
if (s->mb_intra) { |
436 |
|
17699 |
s->current_picture.mb_type[xy] = MB_TYPE_INTRA; |
437 |
|
17699 |
goto intra; |
438 |
|
|
} |
439 |
|
|
|
440 |
|
|
//set motion vectors |
441 |
|
100215 |
s->mv_dir = MV_DIR_FORWARD; |
442 |
|
100215 |
s->mv_type = MV_TYPE_16X16; |
443 |
|
100215 |
s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0; |
444 |
|
100215 |
s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation |
445 |
|
100215 |
s->mv[0][0][1] = h->current_mv_y * 2; |
446 |
|
|
|
447 |
✓✗ |
100215 |
if (s->current_picture.motion_val[0]) { |
448 |
|
|
int b_stride = 2*s->mb_width + 1; |
449 |
|
|
int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride; |
450 |
|
|
s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0]; |
451 |
|
|
s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1]; |
452 |
|
|
} |
453 |
|
|
|
454 |
|
100215 |
intra: |
455 |
|
|
/* decode each block */ |
456 |
✓✓✓✓
|
117914 |
if (s->mb_intra || HAS_CBP(h->mtype)) { |
457 |
|
98347 |
s->bdsp.clear_blocks(s->block[0]); |
458 |
✓✓ |
688429 |
for (i = 0; i < 6; i++) { |
459 |
✗✓ |
590082 |
if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0) |
460 |
|
|
return SLICE_ERROR; |
461 |
|
590082 |
cbp += cbp; |
462 |
|
|
} |
463 |
|
|
} else { |
464 |
✓✓ |
136969 |
for (i = 0; i < 6; i++) |
465 |
|
117402 |
s->block_last_index[i] = -1; |
466 |
|
|
} |
467 |
|
|
|
468 |
|
117914 |
ff_mpv_reconstruct_mb(s, s->block); |
469 |
|
|
|
470 |
|
117914 |
return SLICE_OK; |
471 |
|
|
} |
472 |
|
|
|
473 |
|
|
/** |
474 |
|
|
* Decode the H.261 picture header. |
475 |
|
|
* @return <0 if no startcode found |
476 |
|
|
*/ |
477 |
|
306 |
static int h261_decode_picture_header(H261Context *h) |
478 |
|
|
{ |
479 |
|
306 |
MpegEncContext *const s = &h->s; |
480 |
|
|
int format, i; |
481 |
|
306 |
uint32_t startcode = 0; |
482 |
|
|
|
483 |
✓✗ |
6120 |
for (i = get_bits_left(&s->gb); i > 24; i -= 1) { |
484 |
|
6120 |
startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF; |
485 |
|
|
|
486 |
✓✓ |
6120 |
if (startcode == 0x10) |
487 |
|
306 |
break; |
488 |
|
|
} |
489 |
|
|
|
490 |
✗✓ |
306 |
if (startcode != 0x10) { |
491 |
|
|
av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n"); |
492 |
|
|
return -1; |
493 |
|
|
} |
494 |
|
|
|
495 |
|
|
/* temporal reference */ |
496 |
|
306 |
i = get_bits(&s->gb, 5); /* picture timestamp */ |
497 |
✓✓ |
306 |
if (i < (s->picture_number & 31)) |
498 |
|
6 |
i += 32; |
499 |
|
306 |
s->picture_number = (s->picture_number & ~31) + i; |
500 |
|
|
|
501 |
|
306 |
s->avctx->framerate = (AVRational) { 30000, 1001 }; |
502 |
|
|
|
503 |
|
|
/* PTYPE starts here */ |
504 |
|
306 |
skip_bits1(&s->gb); /* split screen off */ |
505 |
|
306 |
skip_bits1(&s->gb); /* camera off */ |
506 |
|
306 |
skip_bits1(&s->gb); /* freeze picture release off */ |
507 |
|
|
|
508 |
|
306 |
format = get_bits1(&s->gb); |
509 |
|
|
|
510 |
|
|
// only 2 formats possible |
511 |
✗✓ |
306 |
if (format == 0) { // QCIF |
512 |
|
|
s->width = 176; |
513 |
|
|
s->height = 144; |
514 |
|
|
s->mb_width = 11; |
515 |
|
|
s->mb_height = 9; |
516 |
|
|
} else { // CIF |
517 |
|
306 |
s->width = 352; |
518 |
|
306 |
s->height = 288; |
519 |
|
306 |
s->mb_width = 22; |
520 |
|
306 |
s->mb_height = 18; |
521 |
|
|
} |
522 |
|
|
|
523 |
|
306 |
s->mb_num = s->mb_width * s->mb_height; |
524 |
|
|
|
525 |
|
306 |
skip_bits1(&s->gb); /* still image mode off */ |
526 |
|
306 |
skip_bits1(&s->gb); /* Reserved */ |
527 |
|
|
|
528 |
|
|
/* PEI */ |
529 |
✗✓ |
306 |
if (skip_1stop_8data_bits(&s->gb) < 0) |
530 |
|
|
return AVERROR_INVALIDDATA; |
531 |
|
|
|
532 |
|
|
/* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first |
533 |
|
|
* frame, the codec crashes if it does not contain all I-blocks |
534 |
|
|
* (e.g. when a packet is lost). */ |
535 |
|
306 |
s->pict_type = AV_PICTURE_TYPE_P; |
536 |
|
|
|
537 |
|
306 |
h->gob_number = 0; |
538 |
|
306 |
return 0; |
539 |
|
|
} |
540 |
|
|
|
541 |
|
3600 |
static int h261_decode_gob(H261Context *h) |
542 |
|
|
{ |
543 |
|
3600 |
MpegEncContext *const s = &h->s; |
544 |
|
|
|
545 |
|
3600 |
ff_set_qscale(s, s->qscale); |
546 |
|
|
|
547 |
|
|
/* decode mb's */ |
548 |
✓✗ |
121514 |
while (h->current_mba <= MBA_STUFFING) { |
549 |
|
|
int ret; |
550 |
|
|
/* DCT & quantize */ |
551 |
|
121514 |
ret = h261_decode_mb(h); |
552 |
✓✓ |
121514 |
if (ret < 0) { |
553 |
✓✗ |
3600 |
if (ret == SLICE_END) { |
554 |
|
3600 |
h261_decode_mb_skipped(h, h->current_mba, 33); |
555 |
|
3600 |
return 0; |
556 |
|
|
} |
557 |
|
|
av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", |
558 |
|
|
s->mb_x + s->mb_y * s->mb_stride); |
559 |
|
|
return -1; |
560 |
|
|
} |
561 |
|
|
|
562 |
|
117914 |
h261_decode_mb_skipped(h, |
563 |
|
117914 |
h->current_mba - h->mba_diff, |
564 |
|
117914 |
h->current_mba - 1); |
565 |
|
|
} |
566 |
|
|
|
567 |
|
|
return -1; |
568 |
|
|
} |
569 |
|
|
|
570 |
|
|
/** |
571 |
|
|
* returns the number of bytes consumed for building the current frame |
572 |
|
|
*/ |
573 |
|
300 |
static int get_consumed_bytes(MpegEncContext *s, int buf_size) |
574 |
|
|
{ |
575 |
|
300 |
int pos = get_bits_count(&s->gb) >> 3; |
576 |
✗✓ |
300 |
if (pos == 0) |
577 |
|
|
pos = 1; // avoid infinite loops (i doubt that is needed but ...) |
578 |
✓✗ |
300 |
if (pos + 10 > buf_size) |
579 |
|
300 |
pos = buf_size; // oops ;) |
580 |
|
|
|
581 |
|
300 |
return pos; |
582 |
|
|
} |
583 |
|
|
|
584 |
|
300 |
static int h261_decode_frame(AVCodecContext *avctx, void *data, |
585 |
|
|
int *got_frame, AVPacket *avpkt) |
586 |
|
|
{ |
587 |
|
300 |
const uint8_t *buf = avpkt->data; |
588 |
|
300 |
int buf_size = avpkt->size; |
589 |
|
300 |
H261Context *h = avctx->priv_data; |
590 |
|
300 |
MpegEncContext *s = &h->s; |
591 |
|
|
int ret; |
592 |
|
300 |
AVFrame *pict = data; |
593 |
|
|
|
594 |
|
|
ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size); |
595 |
|
|
ff_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); |
596 |
|
|
|
597 |
|
300 |
h->gob_start_code_skipped = 0; |
598 |
|
|
|
599 |
|
306 |
retry: |
600 |
|
306 |
init_get_bits(&s->gb, buf, buf_size * 8); |
601 |
|
|
|
602 |
✓✓ |
306 |
if (!s->context_initialized) |
603 |
|
|
// we need the IDCT permutation for reading a custom matrix |
604 |
|
6 |
ff_mpv_idct_init(s); |
605 |
|
|
|
606 |
|
306 |
ret = h261_decode_picture_header(h); |
607 |
|
|
|
608 |
|
|
/* skip if the header was thrashed */ |
609 |
✗✓ |
306 |
if (ret < 0) { |
610 |
|
|
av_log(s->avctx, AV_LOG_ERROR, "header damaged\n"); |
611 |
|
|
return -1; |
612 |
|
|
} |
613 |
|
|
|
614 |
✓✗✗✓
|
306 |
if (s->width != avctx->coded_width || s->height != avctx->coded_height) { |
615 |
|
|
ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat |
616 |
|
|
s->parse_context.buffer = 0; |
617 |
|
|
ff_mpv_common_end(s); |
618 |
|
|
s->parse_context = pc; |
619 |
|
|
} |
620 |
|
|
|
621 |
✓✓ |
306 |
if (!s->context_initialized) { |
622 |
✗✓ |
6 |
if ((ret = ff_mpv_common_init(s)) < 0) |
623 |
|
|
return ret; |
624 |
|
|
|
625 |
|
6 |
ret = ff_set_dimensions(avctx, s->width, s->height); |
626 |
✗✓ |
6 |
if (ret < 0) |
627 |
|
|
return ret; |
628 |
|
|
|
629 |
|
6 |
goto retry; |
630 |
|
|
} |
631 |
|
|
|
632 |
|
|
// for skipping the frame |
633 |
|
300 |
s->current_picture.f->pict_type = s->pict_type; |
634 |
|
300 |
s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; |
635 |
|
|
|
636 |
✗✓✗✗
|
300 |
if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) || |
637 |
✗✓✗✗
|
300 |
(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) || |
638 |
✗✓ |
300 |
avctx->skip_frame >= AVDISCARD_ALL) |
639 |
|
|
return get_consumed_bytes(s, buf_size); |
640 |
|
|
|
641 |
✗✓ |
300 |
if (ff_mpv_frame_start(s, avctx) < 0) |
642 |
|
|
return -1; |
643 |
|
|
|
644 |
|
300 |
ff_mpeg_er_frame_start(s); |
645 |
|
|
|
646 |
|
|
/* decode each macroblock */ |
647 |
|
300 |
s->mb_x = 0; |
648 |
|
300 |
s->mb_y = 0; |
649 |
|
|
|
650 |
✓✗✓✓
|
3900 |
while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) { |
651 |
✗✓ |
3600 |
if (h261_resync(h) < 0) |
652 |
|
|
break; |
653 |
|
3600 |
h261_decode_gob(h); |
654 |
|
|
} |
655 |
|
300 |
ff_mpv_frame_end(s); |
656 |
|
|
|
657 |
✗✓ |
300 |
av_assert0(s->current_picture.f->pict_type == s->current_picture_ptr->f->pict_type); |
658 |
✗✓ |
300 |
av_assert0(s->current_picture.f->pict_type == s->pict_type); |
659 |
|
|
|
660 |
✗✓ |
300 |
if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0) |
661 |
|
|
return ret; |
662 |
|
300 |
ff_print_debug_info(s, s->current_picture_ptr, pict); |
663 |
|
|
|
664 |
|
300 |
*got_frame = 1; |
665 |
|
|
|
666 |
|
300 |
return get_consumed_bytes(s, buf_size); |
667 |
|
|
} |
668 |
|
|
|
669 |
|
13 |
static av_cold int h261_decode_end(AVCodecContext *avctx) |
670 |
|
|
{ |
671 |
|
13 |
H261Context *h = avctx->priv_data; |
672 |
|
13 |
MpegEncContext *s = &h->s; |
673 |
|
|
|
674 |
|
13 |
ff_mpv_common_end(s); |
675 |
|
13 |
return 0; |
676 |
|
|
} |
677 |
|
|
|
678 |
|
|
AVCodec ff_h261_decoder = { |
679 |
|
|
.name = "h261", |
680 |
|
|
.long_name = NULL_IF_CONFIG_SMALL("H.261"), |
681 |
|
|
.type = AVMEDIA_TYPE_VIDEO, |
682 |
|
|
.id = AV_CODEC_ID_H261, |
683 |
|
|
.priv_data_size = sizeof(H261Context), |
684 |
|
|
.init = h261_decode_init, |
685 |
|
|
.close = h261_decode_end, |
686 |
|
|
.decode = h261_decode_frame, |
687 |
|
|
.capabilities = AV_CODEC_CAP_DR1, |
688 |
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
689 |
|
|
.max_lowres = 3, |
690 |
|
|
}; |