FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h261dec.c
Date: 2024-04-25 15:36:26
Exec Total Coverage
Lines: 259 323 80.2%
Functions: 14 14 100.0%
Branches: 114 176 64.8%

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