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