FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dvdec.c
Date: 2021-09-22 05:58:22
Exec Total Coverage
Lines: 288 330 87.3%
Branches: 154 178 86.5%

Line Branch Exec Source
1 /*
2 * DV decoder
3 * Copyright (c) 2002 Fabrice Bellard
4 * Copyright (c) 2004 Roman Shaposhnik
5 *
6 * 50 Mbps (DVCPRO50) support
7 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
8 *
9 * 100 Mbps (DVCPRO HD) support
10 * Initial code by Daniel Maas <dmaas@maasdigital.com> (funded by BBC R&D)
11 * Final code by Roman Shaposhnik
12 *
13 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
14 * of DV technical info.
15 *
16 * This file is part of FFmpeg.
17 *
18 * FFmpeg is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU Lesser General Public
20 * License as published by the Free Software Foundation; either
21 * version 2.1 of the License, or (at your option) any later version.
22 *
23 * FFmpeg is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with FFmpeg; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 */
32
33 /**
34 * @file
35 * DV decoder
36 */
37
38 #include "libavutil/avassert.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/internal.h"
41 #include "libavutil/mem_internal.h"
42 #include "libavutil/pixdesc.h"
43 #include "libavutil/thread.h"
44
45 #include "avcodec.h"
46 #include "dv.h"
47 #include "dv_profile_internal.h"
48 #include "dvdata.h"
49 #include "get_bits.h"
50 #include "internal.h"
51 #include "put_bits.h"
52 #include "simple_idct.h"
53 #include "thread.h"
54
55 typedef struct BlockInfo {
56 const uint32_t *factor_table;
57 const uint8_t *scan_table;
58 uint8_t pos; /* position in block */
59 void (*idct_put)(uint8_t *dest, ptrdiff_t stride, int16_t *block);
60 uint8_t partial_bit_count;
61 uint32_t partial_bit_buffer;
62 int shift_offset;
63 } BlockInfo;
64
65 static const int dv_iweight_bits = 14;
66
67 static const uint16_t dv_iweight_88[64] = {
68 32768, 16705, 16705, 17734, 17032, 17734, 18205, 18081,
69 18081, 18205, 18725, 18562, 19195, 18562, 18725, 19266,
70 19091, 19705, 19705, 19091, 19266, 21407, 19643, 20267,
71 20228, 20267, 19643, 21407, 22725, 21826, 20853, 20806,
72 20806, 20853, 21826, 22725, 23170, 23170, 21407, 21400,
73 21407, 23170, 23170, 24598, 23786, 22018, 22018, 23786,
74 24598, 25251, 24465, 22654, 24465, 25251, 25972, 25172,
75 25172, 25972, 26722, 27969, 26722, 29692, 29692, 31521,
76 };
77 static const uint16_t dv_iweight_248[64] = {
78 32768, 16384, 16705, 16705, 17734, 17734, 17734, 17734,
79 18081, 18081, 18725, 18725, 21407, 21407, 19091, 19091,
80 19195, 19195, 18205, 18205, 18725, 18725, 19705, 19705,
81 20267, 20267, 21826, 21826, 23170, 23170, 20806, 20806,
82 20267, 20267, 19266, 19266, 21407, 21407, 20853, 20853,
83 21400, 21400, 23786, 23786, 24465, 24465, 22018, 22018,
84 23170, 23170, 22725, 22725, 24598, 24598, 24465, 24465,
85 25172, 25172, 27969, 27969, 25972, 25972, 29692, 29692
86 };
87
88 /**
89 * The "inverse" DV100 weights are actually just the spec weights (zig-zagged).
90 */
91 static const uint16_t dv_iweight_1080_y[64] = {
92 128, 16, 16, 17, 17, 17, 18, 18,
93 18, 18, 18, 18, 19, 18, 18, 19,
94 19, 19, 19, 19, 19, 42, 38, 40,
95 40, 40, 38, 42, 44, 43, 41, 41,
96 41, 41, 43, 44, 45, 45, 42, 42,
97 42, 45, 45, 48, 46, 43, 43, 46,
98 48, 49, 48, 44, 48, 49, 101, 98,
99 98, 101, 104, 109, 104, 116, 116, 123,
100 };
101 static const uint16_t dv_iweight_1080_c[64] = {
102 128, 16, 16, 17, 17, 17, 25, 25,
103 25, 25, 26, 25, 26, 25, 26, 26,
104 26, 27, 27, 26, 26, 42, 38, 40,
105 40, 40, 38, 42, 44, 43, 41, 41,
106 41, 41, 43, 44, 91, 91, 84, 84,
107 84, 91, 91, 96, 93, 86, 86, 93,
108 96, 197, 191, 177, 191, 197, 203, 197,
109 197, 203, 209, 219, 209, 232, 232, 246,
110 };
111 static const uint16_t dv_iweight_720_y[64] = {
112 128, 16, 16, 17, 17, 17, 18, 18,
113 18, 18, 18, 18, 19, 18, 18, 19,
114 19, 19, 19, 19, 19, 42, 38, 40,
115 40, 40, 38, 42, 44, 43, 41, 41,
116 41, 41, 43, 44, 68, 68, 63, 63,
117 63, 68, 68, 96, 92, 86, 86, 92,
118 96, 98, 96, 88, 96, 98, 202, 196,
119 196, 202, 208, 218, 208, 232, 232, 246,
120 };
121 static const uint16_t dv_iweight_720_c[64] = {
122 128, 24, 24, 26, 26, 26, 36, 36,
123 36, 36, 36, 36, 38, 36, 36, 38,
124 38, 38, 38, 38, 38, 84, 76, 80,
125 80, 80, 76, 84, 88, 86, 82, 82,
126 82, 82, 86, 88, 182, 182, 168, 168,
127 168, 182, 182, 192, 186, 192, 172, 186,
128 192, 394, 382, 354, 382, 394, 406, 394,
129 394, 406, 418, 438, 418, 464, 464, 492,
130 };
131
132 #define TEX_VLC_BITS 10
133
134 /* XXX: also include quantization */
135 static RL_VLC_ELEM dv_rl_vlc[1664];
136
137 33 static void dv_init_static(void)
138 {
139 33 VLC_TYPE vlc_buf[FF_ARRAY_ELEMS(dv_rl_vlc)][2] = { 0 };
140 33 VLC dv_vlc = { .table = vlc_buf, .table_allocated = FF_ARRAY_ELEMS(vlc_buf) };
141 uint16_t new_dv_vlc_bits[NB_DV_VLC * 2];
142 uint8_t new_dv_vlc_len[NB_DV_VLC * 2];
143 uint8_t new_dv_vlc_run[NB_DV_VLC * 2];
144 int16_t new_dv_vlc_level[NB_DV_VLC * 2];
145 int i, j;
146
147 /* it's faster to include sign bit in a generic VLC parsing scheme */
148
2/2
✓ Branch 0 taken 13497 times.
✓ Branch 1 taken 33 times.
13530 for (i = 0, j = 0; i < NB_DV_VLC; i++, j++) {
149 13497 new_dv_vlc_bits[j] = ff_dv_vlc_bits[i];
150 13497 new_dv_vlc_len[j] = ff_dv_vlc_len[i];
151 13497 new_dv_vlc_run[j] = ff_dv_vlc_run[i];
152 13497 new_dv_vlc_level[j] = ff_dv_vlc_level[i];
153
154
2/2
✓ Branch 0 taken 11121 times.
✓ Branch 1 taken 2376 times.
13497 if (ff_dv_vlc_level[i]) {
155 11121 new_dv_vlc_bits[j] <<= 1;
156 11121 new_dv_vlc_len[j]++;
157
158 11121 j++;
159 11121 new_dv_vlc_bits[j] = (ff_dv_vlc_bits[i] << 1) | 1;
160 11121 new_dv_vlc_len[j] = ff_dv_vlc_len[i] + 1;
161 11121 new_dv_vlc_run[j] = ff_dv_vlc_run[i];
162 11121 new_dv_vlc_level[j] = -ff_dv_vlc_level[i];
163 }
164 }
165
166 /* NOTE: as a trick, we use the fact the no codes are unused
167 * to accelerate the parsing of partial codes */
168 33 init_vlc(&dv_vlc, TEX_VLC_BITS, j, new_dv_vlc_len,
169 1, 1, new_dv_vlc_bits, 2, 2, INIT_VLC_USE_NEW_STATIC);
170 av_assert1(dv_vlc.table_size == 1664);
171
172
2/2
✓ Branch 0 taken 54912 times.
✓ Branch 1 taken 33 times.
54945 for (int i = 0; i < dv_vlc.table_size; i++) {
173 54912 int code = dv_vlc.table[i][0];
174 54912 int len = dv_vlc.table[i][1];
175 int level, run;
176
177
2/2
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 53856 times.
54912 if (len < 0) { // more bits needed
178 1056 run = 0;
179 1056 level = code;
180 } else {
181 53856 run = new_dv_vlc_run[code] + 1;
182 53856 level = new_dv_vlc_level[code];
183 }
184 54912 dv_rl_vlc[i].len = len;
185 54912 dv_rl_vlc[i].level = level;
186 54912 dv_rl_vlc[i].run = run;
187 }
188 33 }
189
190 57 static void dv_init_weight_tables(DVVideoContext *ctx, const AVDVProfile *d)
191 {
192 int j, i, c, s;
193 57 uint32_t *factor1 = &ctx->idct_factor[0],
194
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 41 times.
57 *factor2 = &ctx->idct_factor[DV_PROFILE_IS_HD(d) ? 4096 : 2816];
195
196
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 41 times.
57 if (DV_PROFILE_IS_HD(d)) {
197 /* quantization quanta by QNO for DV100 */
198 static const uint8_t dv100_qstep[16] = {
199 1, /* QNO = 0 and 1 both have no quantization */
200 1,
201 2, 3, 4, 5, 6, 7, 8, 16, 18, 20, 22, 24, 28, 52
202 };
203 const uint16_t *iweight1, *iweight2;
204
205
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if (d->height == 720) {
206 8 iweight1 = &dv_iweight_720_y[0];
207 8 iweight2 = &dv_iweight_720_c[0];
208 } else {
209 8 iweight1 = &dv_iweight_1080_y[0];
210 8 iweight2 = &dv_iweight_1080_c[0];
211 }
212
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 16 times.
80 for (c = 0; c < 4; c++) {
213
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 64 times.
1088 for (s = 0; s < 16; s++) {
214
2/2
✓ Branch 0 taken 65536 times.
✓ Branch 1 taken 1024 times.
66560 for (i = 0; i < 64; i++) {
215 65536 *factor1++ = (dv100_qstep[s] << (c + 9)) * iweight1[i];
216 65536 *factor2++ = (dv100_qstep[s] << (c + 9)) * iweight2[i];
217 }
218 }
219 }
220 } else {
221 static const uint8_t dv_quant_areas[4] = { 6, 21, 43, 64 };
222 41 const uint16_t *iweight1 = &dv_iweight_88[0];
223
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 41 times.
123 for (j = 0; j < 2; j++, iweight1 = &dv_iweight_248[0]) {
224
2/2
✓ Branch 0 taken 1804 times.
✓ Branch 1 taken 82 times.
1886 for (s = 0; s < 22; s++) {
225
2/2
✓ Branch 0 taken 7216 times.
✓ Branch 1 taken 1804 times.
9020 for (i = c = 0; c < 4; c++) {
226
2/2
✓ Branch 0 taken 115456 times.
✓ Branch 1 taken 7216 times.
122672 for (; i < dv_quant_areas[c]; i++) {
227 115456 *factor1 = iweight1[i] << (ff_dv_quant_shifts[s][c] + 1);
228 115456 *factor2++ = (*factor1++) << 1;
229 }
230 }
231 }
232 }
233 }
234 57 }
235
236 59 static av_cold int dvvideo_decode_init(AVCodecContext *avctx)
237 {
238 static AVOnce init_static_once = AV_ONCE_INIT;
239 59 DVVideoContext *s = avctx->priv_data;
240 int i;
241
242 59 ff_idctdsp_init(&s->idsp, avctx);
243
244
2/2
✓ Branch 0 taken 3776 times.
✓ Branch 1 taken 59 times.
3835 for (i = 0; i < 64; i++)
245 3776 s->dv_zigzag[0][i] = s->idsp.idct_permutation[ff_zigzag_direct[i]];
246
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (avctx->lowres){
248 for (i = 0; i < 64; i++){
249 int j = ff_dv_zigzag248_direct[i];
250 s->dv_zigzag[1][i] = s->idsp.idct_permutation[(j & 7) + (j & 8) * 4 + (j & 48) / 2];
251 }
252 }else
253 59 memcpy(s->dv_zigzag[1], ff_dv_zigzag248_direct, sizeof(s->dv_zigzag[1]));
254
255 59 s->idct_put[0] = s->idsp.idct_put;
256 59 s->idct_put[1] = ff_simple_idct248_put;
257
258 59 ff_thread_once(&init_static_once, dv_init_static);
259
260 59 return ff_dvvideo_init(avctx);
261 }
262
263 /* decode AC coefficients */
264 30575972 static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, int16_t *block)
265 {
266 30575972 int last_index = gb->size_in_bits;
267 30575972 const uint8_t *scan_table = mb->scan_table;
268 30575972 const uint32_t *factor_table = mb->factor_table;
269 30575972 int pos = mb->pos;
270 30575972 int partial_bit_count = mb->partial_bit_count;
271 int level, run, vlc_len, index;
272
273 30575972 OPEN_READER_NOSIZE(re, gb);
274 30575972 UPDATE_CACHE(re, gb);
275
276 /* if we must parse a partial VLC, we do it here */
277
2/2
✓ Branch 0 taken 7006249 times.
✓ Branch 1 taken 23569723 times.
30575972 if (partial_bit_count > 0) {
278 7006249 re_cache = re_cache >> partial_bit_count |
279 7006249 mb->partial_bit_buffer;
280 7006249 re_index -= partial_bit_count;
281 7006249 mb->partial_bit_count = 0;
282 }
283
284 /* get the AC coefficients until last_index is reached */
285 for (;;) {
286 205653546 ff_dlog(NULL, "%2d: bits=%04"PRIx32" index=%u\n",
287 pos, SHOW_UBITS(re, gb, 16), re_index);
288 /* our own optimized GET_RL_VLC */
289 236229518 index = NEG_USR32(re_cache, TEX_VLC_BITS);
290 236229518 vlc_len = dv_rl_vlc[index].len;
291
2/2
✓ Branch 0 taken 18064790 times.
✓ Branch 1 taken 218164728 times.
236229518 if (vlc_len < 0) {
292 18064790 index = NEG_USR32((unsigned) re_cache << TEX_VLC_BITS, -vlc_len) +
293 18064790 dv_rl_vlc[index].level;
294 18064790 vlc_len = TEX_VLC_BITS - vlc_len;
295 }
296 236229518 level = dv_rl_vlc[index].level;
297 236229518 run = dv_rl_vlc[index].run;
298
299 /* gotta check if we're still within gb boundaries */
300
2/2
✓ Branch 0 taken 8401374 times.
✓ Branch 1 taken 227828144 times.
236229518 if (re_index + vlc_len > last_index) {
301 /* should be < 16 bits otherwise a codeword could have been parsed */
302 8401374 mb->partial_bit_count = last_index - re_index;
303 8401374 mb->partial_bit_buffer = re_cache & ~(-1u >> mb->partial_bit_count);
304 8401374 re_index = last_index;
305 8401374 break;
306 }
307 227828144 re_index += vlc_len;
308
309 ff_dlog(NULL, "run=%d level=%d\n", run, level);
310 227828144 pos += run;
311
2/2
✓ Branch 0 taken 22174598 times.
✓ Branch 1 taken 205653546 times.
227828144 if (pos >= 64)
312 22174598 break;
313
314 205653546 level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >>
315 dv_iweight_bits;
316 205653546 block[scan_table[pos]] = level;
317
318 205653546 UPDATE_CACHE(re, gb);
319 }
320 30575972 CLOSE_READER(re, gb);
321 30575972 mb->pos = pos;
322 30575972 }
323
324 16879840 static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
325 {
326 16879840 int bits_left = get_bits_left(gb);
327
2/2
✓ Branch 0 taken 36610433 times.
✓ Branch 1 taken 16879840 times.
53490273 while (bits_left >= MIN_CACHE_BITS) {
328 36610433 put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
329 36610433 bits_left -= MIN_CACHE_BITS;
330 }
331
2/2
✓ Branch 0 taken 16321402 times.
✓ Branch 1 taken 558438 times.
16879840 if (bits_left > 0)
332 16321402 put_bits(pb, bits_left, get_bits(gb, bits_left));
333 16879840 }
334
335 static av_always_inline void put_block_8x4(int16_t *block, uint8_t *av_restrict p, int stride)
336 {
337 int i, j;
338
339 for (i = 0; i < 4; i++) {
340 for (j = 0; j < 8; j++)
341 p[j] = av_clip_uint8(block[j]);
342 block += 8;
343 p += stride;
344 }
345 }
346
347 static void dv100_idct_put_last_row_field_chroma(DVVideoContext *s, uint8_t *data,
348 int stride, int16_t *blocks)
349 {
350 s->idsp.idct(blocks + 0*64);
351 s->idsp.idct(blocks + 1*64);
352
353 put_block_8x4(blocks+0*64, data, stride<<1);
354 put_block_8x4(blocks+0*64 + 4*8, data + 8, stride<<1);
355 put_block_8x4(blocks+1*64, data + stride, stride<<1);
356 put_block_8x4(blocks+1*64 + 4*8, data + 8 + stride, stride<<1);
357 }
358
359 static void dv100_idct_put_last_row_field_luma(DVVideoContext *s, uint8_t *data,
360 int stride, int16_t *blocks)
361 {
362 s->idsp.idct(blocks + 0*64);
363 s->idsp.idct(blocks + 1*64);
364 s->idsp.idct(blocks + 2*64);
365 s->idsp.idct(blocks + 3*64);
366
367 put_block_8x4(blocks+0*64, data, stride<<1);
368 put_block_8x4(blocks+0*64 + 4*8, data + 16, stride<<1);
369 put_block_8x4(blocks+1*64, data + 8, stride<<1);
370 put_block_8x4(blocks+1*64 + 4*8, data + 24, stride<<1);
371 put_block_8x4(blocks+2*64, data + stride, stride<<1);
372 put_block_8x4(blocks+2*64 + 4*8, data + 16 + stride, stride<<1);
373 put_block_8x4(blocks+3*64, data + 8 + stride, stride<<1);
374 put_block_8x4(blocks+3*64 + 4*8, data + 24 + stride, stride<<1);
375 }
376
377 /* mb_x and mb_y are in units of 8 pixels */
378 620028 static int dv_decode_video_segment(AVCodecContext *avctx, void *arg)
379 {
380 620028 DVVideoContext *s = avctx->priv_data;
381 620028 DVwork_chunk *work_chunk = arg;
382 int quant, dc, dct_mode, class1, j;
383 int mb_index, mb_x, mb_y, last_index;
384 int y_stride, linesize;
385 int16_t *block, *block1;
386 int c_offset;
387 uint8_t *y_ptr;
388 const uint8_t *buf_ptr;
389 PutBitContext pb, vs_pb;
390 GetBitContext gb;
391 BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
392 620028 LOCAL_ALIGNED_16(int16_t, sblock, [5 * DV_MAX_BPM], [64]);
393 620028 LOCAL_ALIGNED_16(uint8_t, mb_bit_buffer, [80 + AV_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
394 620028 LOCAL_ALIGNED_16(uint8_t, vs_bit_buffer, [80 * 5 + AV_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
395 620028 const int log2_blocksize = 3-s->avctx->lowres;
396 int is_field_mode[5];
397 620028 int vs_bit_buffer_damaged = 0;
398 620028 int mb_bit_buffer_damaged[5] = {0};
399 620028 int retried = 0;
400 int sta;
401
402 av_assert1((((int) mb_bit_buffer) & 7) == 0);
403 av_assert1((((int) vs_bit_buffer) & 7) == 0);
404
405 620029 retry:
406
407 620029 memset(sblock, 0, 5 * DV_MAX_BPM * sizeof(*sblock));
408
409 /* pass 1: read DC and AC coefficients in blocks */
410 620029 buf_ptr = &s->buf[work_chunk->buf_offset * 80];
411 620029 block1 = &sblock[0][0];
412 620029 mb1 = mb_data;
413 620029 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
414
2/2
✓ Branch 0 taken 3100145 times.
✓ Branch 1 taken 620029 times.
3720174 for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
415 /* skip header */
416 3100145 quant = buf_ptr[3] & 0x0f;
417
1/2
✓ Branch 0 taken 3100145 times.
✗ Branch 1 not taken.
3100145 if (avctx->error_concealment) {
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3100145 times.
3100145 if ((buf_ptr[3] >> 4) == 0x0E)
419 vs_bit_buffer_damaged = 1;
420
2/2
✓ Branch 0 taken 620029 times.
✓ Branch 1 taken 2480116 times.
3100145 if (!mb_index) {
421 620029 sta = buf_ptr[3] >> 4;
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480116 times.
2480116 } else if (sta != (buf_ptr[3] >> 4))
423 vs_bit_buffer_damaged = 1;
424 }
425 3100145 buf_ptr += 4;
426 3100145 init_put_bits(&pb, mb_bit_buffer, 80);
427 3100145 mb = mb1;
428 3100145 block = block1;
429 3100145 is_field_mode[mb_index] = 0;
430
2/2
✓ Branch 0 taken 22181070 times.
✓ Branch 1 taken 3100145 times.
25281215 for (j = 0; j < s->sys->bpm; j++) {
431 22181070 last_index = s->sys->block_sizes[j];
432 22181070 init_get_bits(&gb, buf_ptr, last_index);
433
434 /* get the DC */
435 22181070 dc = get_sbits(&gb, 9);
436 22181070 dct_mode = get_bits1(&gb);
437 22181070 class1 = get_bits(&gb, 2);
438
2/2
✓ Branch 0 taken 14320800 times.
✓ Branch 1 taken 7860270 times.
22181070 if (DV_PROFILE_IS_HD(s->sys)) {
439 14320800 mb->idct_put = s->idct_put[0];
440 14320800 mb->scan_table = s->dv_zigzag[0];
441
2/2
✓ Branch 0 taken 7160400 times.
✓ Branch 1 taken 7160400 times.
14320800 mb->factor_table = &s->idct_factor[(j >= 4) * 4 * 16 * 64 +
442 14320800 class1 * 16 * 64 +
443 14320800 quant * 64];
444
3/4
✓ Branch 0 taken 1790100 times.
✓ Branch 1 taken 12530700 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1790100 times.
14320800 is_field_mode[mb_index] |= !j && dct_mode;
445 } else {
446
3/4
✓ Branch 0 taken 7576 times.
✓ Branch 1 taken 7852694 times.
✓ Branch 2 taken 7576 times.
✗ Branch 3 not taken.
7860270 mb->idct_put = s->idct_put[dct_mode && log2_blocksize == 3];
447 7860270 mb->scan_table = s->dv_zigzag[dct_mode];
448 7860270 mb->factor_table =
449
2/2
✓ Branch 0 taken 2565500 times.
✓ Branch 1 taken 5294770 times.
7860270 &s->idct_factor[(class1 == 3) * 2 * 22 * 64 +
450 7860270 dct_mode * 22 * 64 +
451 7860270 (quant + ff_dv_quant_offset[class1]) * 64];
452 }
453 22181070 dc = dc * 4;
454 /* convert to unsigned because 128 is not added in the
455 * standard IDCT */
456 22181070 dc += 1024;
457 22181070 block[0] = dc;
458 22181070 buf_ptr += last_index >> 3;
459 22181070 mb->pos = 0;
460 22181070 mb->partial_bit_count = 0;
461
462 ff_dlog(avctx, "MB block: %d, %d ", mb_index, j);
463 22181070 dv_decode_ac(&gb, mb, block);
464
465 /* write the remaining bits in a new buffer only if the
466 * block is finished */
467
2/2
✓ Branch 0 taken 14778593 times.
✓ Branch 1 taken 7402477 times.
22181070 if (mb->pos >= 64)
468 14778593 bit_copy(&pb, &gb);
469
3/4
✓ Branch 0 taken 14778593 times.
✓ Branch 1 taken 7402477 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14778593 times.
22181070 if (mb->pos >= 64 && mb->pos < 127)
470 vs_bit_buffer_damaged = mb_bit_buffer_damaged[mb_index] = 1;
471
472 22181070 block += 64;
473 22181070 mb++;
474 }
475
476
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3100144 times.
3100145 if (mb_bit_buffer_damaged[mb_index] > 0)
477 1 continue;
478
479 /* pass 2: we can do it just after */
480 ff_dlog(avctx, "***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
481 3100144 block = block1;
482 3100144 mb = mb1;
483 3100144 init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
484 3100144 put_bits32(&pb, 0); // padding must be zeroed
485 3100144 flush_put_bits(&pb);
486
2/2
✓ Branch 0 taken 17715756 times.
✓ Branch 1 taken 2101247 times.
19817003 for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {
487
4/4
✓ Branch 0 taken 4961515 times.
✓ Branch 1 taken 12754241 times.
✓ Branch 3 taken 4506592 times.
✓ Branch 4 taken 454923 times.
17715756 if (mb->pos < 64 && get_bits_left(&gb) > 0) {
488 4506592 dv_decode_ac(&gb, mb, block);
489 /* if still not finished, no need to parse other blocks */
490
2/2
✓ Branch 0 taken 998897 times.
✓ Branch 1 taken 3507695 times.
4506592 if (mb->pos < 64)
491 998897 break;
492
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3507693 times.
3507695 if (mb->pos < 127)
493 2 vs_bit_buffer_damaged = mb_bit_buffer_damaged[mb_index] = 1;
494 }
495 }
496 /* all blocks are finished, so the extra bytes can be used at
497 * the video segment level */
498
2/2
✓ Branch 0 taken 2101247 times.
✓ Branch 1 taken 998897 times.
3100144 if (j >= s->sys->bpm)
499 2101247 bit_copy(&vs_pb, &gb);
500 }
501
502 /* we need a pass over the whole video segment */
503 ff_dlog(avctx, "***pass 3 size=%d\n", put_bits_count(&vs_pb));
504 620029 block = &sblock[0][0];
505 620029 mb = mb_data;
506 620029 init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
507 620029 put_bits32(&vs_pb, 0); // padding must be zeroed
508 620029 flush_put_bits(&vs_pb);
509
2/2
✓ Branch 0 taken 3100145 times.
✓ Branch 1 taken 620029 times.
3720174 for (mb_index = 0; mb_index < 5; mb_index++) {
510
2/2
✓ Branch 0 taken 22181070 times.
✓ Branch 1 taken 3100145 times.
25281215 for (j = 0; j < s->sys->bpm; j++) {
511
5/6
✓ Branch 0 taken 3894782 times.
✓ Branch 1 taken 18286288 times.
✓ Branch 3 taken 3888310 times.
✓ Branch 4 taken 6472 times.
✓ Branch 5 taken 3888310 times.
✗ Branch 6 not taken.
22181070 if (mb->pos < 64 && get_bits_left(&gb) > 0 && !vs_bit_buffer_damaged) {
512 ff_dlog(avctx, "start %d:%d\n", mb_index, j);
513 3888310 dv_decode_ac(&gb, mb, block);
514 }
515
516
4/4
✓ Branch 0 taken 22174598 times.
✓ Branch 1 taken 6472 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 22174596 times.
22181070 if (mb->pos >= 64 && mb->pos < 127) {
517 2 av_log(avctx, AV_LOG_ERROR,
518 2 "AC EOB marker is absent pos=%d\n", mb->pos);
519 2 vs_bit_buffer_damaged = 1;
520 }
521 22181070 block += 64;
522 22181070 mb++;
523 }
524 }
525
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 620027 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
620029 if (vs_bit_buffer_damaged && !retried) {
526 1 av_log(avctx, AV_LOG_ERROR, "Concealing bitstream errors\n");
527 1 retried = 1;
528 1 goto retry;
529 }
530
531 /* compute idct and place blocks */
532 620028 block = &sblock[0][0];
533 620028 mb = mb_data;
534
2/2
✓ Branch 0 taken 3100140 times.
✓ Branch 1 taken 620028 times.
3720168 for (mb_index = 0; mb_index < 5; mb_index++) {
535 3100140 dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
536
537 /* idct_put'ting luminance */
538
2/2
✓ Branch 0 taken 2677320 times.
✓ Branch 1 taken 422820 times.
3100140 if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P) ||
539
4/4
✓ Branch 0 taken 300780 times.
✓ Branch 1 taken 2376540 times.
✓ Branch 2 taken 294096 times.
✓ Branch 3 taken 6684 times.
2677320 (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
540
4/4
✓ Branch 0 taken 1790100 times.
✓ Branch 1 taken 880536 times.
✓ Branch 2 taken 1780920 times.
✓ Branch 3 taken 9180 times.
2670636 (s->sys->height >= 720 && mb_y != 134)) {
541 2210424 y_stride = (s->frame->linesize[0] <<
542 2210424 ((!is_field_mode[mb_index]) * log2_blocksize));
543 } else {
544 889716 y_stride = (2 << log2_blocksize);
545 }
546 3100140 y_ptr = s->frame->data[0] +
547 3100140 ((mb_y * s->frame->linesize[0] + mb_x) << log2_blocksize);
548
3/4
✓ Branch 0 taken 9180 times.
✓ Branch 1 taken 3090960 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9180 times.
3100140 if (mb_y == 134 && is_field_mode[mb_index]) {
549 dv100_idct_put_last_row_field_luma(s, y_ptr, s->frame->linesize[0], block);
550 } else {
551 3100140 linesize = s->frame->linesize[0] << is_field_mode[mb_index];
552 3100140 mb[0].idct_put(y_ptr, linesize, block + 0 * 64);
553
2/2
✓ Branch 0 taken 586440 times.
✓ Branch 1 taken 2513700 times.
3100140 if (s->sys->video_stype == 4) { /* SD 422 */
554 586440 mb[2].idct_put(y_ptr + (1 << log2_blocksize), linesize, block + 2 * 64);
555 } else {
556 2513700 mb[1].idct_put(y_ptr + (1 << log2_blocksize), linesize, block + 1 * 64);
557 2513700 mb[2].idct_put(y_ptr + y_stride, linesize, block + 2 * 64);
558 2513700 mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3 * 64);
559 }
560 }
561 3100140 mb += 4;
562 3100140 block += 4 * 64;
563
564 /* idct_put'ting chrominance */
565 6200280 c_offset = (((mb_y >> (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->frame->linesize[1] +
566
2/2
✓ Branch 0 taken 300780 times.
✓ Branch 1 taken 2799360 times.
3100140 (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);
567
2/2
✓ Branch 0 taken 6200280 times.
✓ Branch 1 taken 3100140 times.
9300420 for (j = 2; j; j--) {
568 6200280 uint8_t *c_ptr = s->frame->data[j] + c_offset;
569
4/4
✓ Branch 0 taken 601560 times.
✓ Branch 1 taken 5598720 times.
✓ Branch 2 taken 13368 times.
✓ Branch 3 taken 588192 times.
6200280 if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
570 uint64_t aligned_pixels[64 / 8];
571 13368 uint8_t *pixels = (uint8_t *) aligned_pixels;
572 uint8_t *c_ptr1, *ptr1;
573 int x, y;
574 13368 mb->idct_put(pixels, 8, block);
575
2/2
✓ Branch 0 taken 106944 times.
✓ Branch 1 taken 13368 times.
120312 for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->frame->linesize[j], pixels += 8) {
576 106944 ptr1 = pixels + ((1 << (log2_blocksize))>>1);
577 106944 c_ptr1 = c_ptr + (s->frame->linesize[j] << log2_blocksize);
578
2/2
✓ Branch 0 taken 427776 times.
✓ Branch 1 taken 106944 times.
534720 for (x = 0; x < (1 << FFMAX(log2_blocksize - 1, 0)); x++) {
579 427776 c_ptr[x] = pixels[x];
580 427776 c_ptr1[x] = ptr1[x];
581 }
582 }
583 13368 block += 64;
584 13368 mb++;
585 } else {
586
2/2
✓ Branch 0 taken 18360 times.
✓ Branch 1 taken 6168552 times.
6186912 y_stride = (mb_y == 134) ? (1 << log2_blocksize) :
587 6168552 s->frame->linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);
588
3/4
✓ Branch 0 taken 18360 times.
✓ Branch 1 taken 6168552 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18360 times.
6186912 if (mb_y == 134 && is_field_mode[mb_index]) {
589 dv100_idct_put_last_row_field_chroma(s, c_ptr, s->frame->linesize[j], block);
590 mb += 2;
591 block += 2*64;
592 } else {
593 6186912 linesize = s->frame->linesize[j] << is_field_mode[mb_index];
594 6186912 (mb++)->idct_put(c_ptr, linesize, block);
595 6186912 block += 64;
596
2/2
✓ Branch 0 taken 3580200 times.
✓ Branch 1 taken 2606712 times.
6186912 if (s->sys->bpm == 8) {
597 3580200 (mb++)->idct_put(c_ptr + y_stride, linesize, block);
598 3580200 block += 64;
599 }
600 }
601 }
602 }
603 }
604 620028 return 0;
605 }
606
607 /* NOTE: exactly one frame must be given (120000 bytes for NTSC,
608 * 144000 bytes for PAL - or twice those for 50Mbps) */
609 1042 static int dvvideo_decode_frame(AVCodecContext *avctx, void *data,
610 int *got_frame, AVPacket *avpkt)
611 {
612 1042 uint8_t *buf = avpkt->data;
613 1042 int buf_size = avpkt->size;
614 1042 DVVideoContext *s = avctx->priv_data;
615 1042 ThreadFrame frame = { .f = data };
616 const uint8_t *vsc_pack;
617 int apt, is16_9, ret;
618 const AVDVProfile *sys;
619
620 1042 sys = ff_dv_frame_profile(avctx, s->sys, buf, buf_size);
621
2/4
✓ Branch 0 taken 1042 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1042 times.
1042 if (!sys || buf_size < sys->frame_size) {
622 av_log(avctx, AV_LOG_ERROR, "could not find dv frame profile\n");
623 return -1; /* NOTE: we only accept several full frames */
624 }
625
626
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 985 times.
1042 if (sys != s->sys) {
627 57 ret = ff_dv_init_dynamic_tables(s, sys);
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (ret < 0) {
629 av_log(avctx, AV_LOG_ERROR, "Error initializing the work tables.\n");
630 return ret;
631 }
632 57 dv_init_weight_tables(s, sys);
633 57 s->sys = sys;
634 }
635
636 1042 s->frame = frame.f;
637 1042 frame.f->key_frame = 1;
638 1042 frame.f->pict_type = AV_PICTURE_TYPE_I;
639 1042 avctx->pix_fmt = s->sys->pix_fmt;
640 1042 avctx->framerate = av_inv_q(s->sys->time_base);
641
642 1042 ret = ff_set_dimensions(avctx, s->sys->width, s->sys->height);
643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1042 times.
1042 if (ret < 0)
644 return ret;
645
646 /* Determine the codec's sample_aspect ratio from the packet */
647 1042 vsc_pack = buf + 80 * 5 + 48 + 5;
648
1/2
✓ Branch 0 taken 1042 times.
✗ Branch 1 not taken.
1042 if (*vsc_pack == dv_video_control) {
649 1042 apt = buf[4] & 0x07;
650
4/4
✓ Branch 0 taken 607 times.
✓ Branch 1 taken 435 times.
✓ Branch 2 taken 269 times.
✓ Branch 3 taken 338 times.
1311 is16_9 = (vsc_pack[2] & 0x07) == 0x02 ||
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 269 times.
269 (!apt && (vsc_pack[2] & 0x07) == 0x07);
652 1042 ff_set_sar(avctx, s->sys->sar[is16_9]);
653 }
654
655
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1042 times.
1042 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
656 return ret;
657
658 /* Determine the codec's field order from the packet */
659
1/2
✓ Branch 0 taken 1042 times.
✗ Branch 1 not taken.
1042 if ( *vsc_pack == dv_video_control ) {
660
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 838 times.
1042 if (avctx->height == 720) {
661 204 frame.f->interlaced_frame = 0;
662 204 frame.f->top_field_first = 0;
663
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 634 times.
838 } else if (avctx->height == 1080) {
664 204 frame.f->interlaced_frame = 1;
665 204 frame.f->top_field_first = (vsc_pack[3] & 0x40) == 0x40;
666 } else {
667 634 frame.f->interlaced_frame = (vsc_pack[3] & 0x10) == 0x10;
668 634 frame.f->top_field_first = !(vsc_pack[3] & 0x40);
669 }
670 }
671
672 1042 s->buf = buf;
673 1042 avctx->execute(avctx, dv_decode_video_segment, s->work_chunks, NULL,
674 dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
675
676 1042 emms_c();
677
678 /* return image */
679 1042 *got_frame = 1;
680
681 1042 return s->sys->frame_size;
682 }
683
684 const AVCodec ff_dvvideo_decoder = {
685 .name = "dvvideo",
686 .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
687 .type = AVMEDIA_TYPE_VIDEO,
688 .id = AV_CODEC_ID_DVVIDEO,
689 .priv_data_size = sizeof(DVVideoContext),
690 .init = dvvideo_decode_init,
691 .decode = dvvideo_decode_frame,
692 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS,
693 .max_lowres = 3,
694 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
695 };
696