FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h261dec.c
Date: 2022-01-28 07:56:06
Exec Total Coverage
Lines: 264 330 80.0%
Branches: 116 178 65.2%

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