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