FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h261dec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 247 300 82.3%
Functions: 12 12 100.0%
Branches: 111 164 67.7%

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