FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo.c
Date: 2022-01-28 07:56:06
Exec Total Coverage
Lines: 820 1056 77.7%
Branches: 363 562 64.6%

Line Branch Exec Source
1 /*
2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /**
26 * @file
27 * The simplest mpeg encoder (well, it was the simplest!).
28 */
29
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34
35 #include "avcodec.h"
36 #include "blockdsp.h"
37 #include "h264chroma.h"
38 #include "idctdsp.h"
39 #include "mathops.h"
40 #include "mpeg_er.h"
41 #include "mpegutils.h"
42 #include "mpegvideo.h"
43 #include "mpegvideodata.h"
44 #include "qpeldsp.h"
45 #include "thread.h"
46 #include "wmv2.h"
47 #include <limits.h>
48
49 126126 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
50 int16_t *block, int n, int qscale)
51 {
52 int i, level, nCoeffs;
53 const uint16_t *quant_matrix;
54
55 126126 nCoeffs= s->block_last_index[n];
56
57
2/2
✓ Branch 0 taken 84084 times.
✓ Branch 1 taken 42042 times.
126126 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
58 /* XXX: only MPEG-1 */
59 126126 quant_matrix = s->intra_matrix;
60
2/2
✓ Branch 0 taken 2915949 times.
✓ Branch 1 taken 126126 times.
3042075 for(i=1;i<=nCoeffs;i++) {
61 2915949 int j= s->intra_scantable.permutated[i];
62 2915949 level = block[j];
63
2/2
✓ Branch 0 taken 1215436 times.
✓ Branch 1 taken 1700513 times.
2915949 if (level) {
64
2/2
✓ Branch 0 taken 604537 times.
✓ Branch 1 taken 610899 times.
1215436 if (level < 0) {
65 604537 level = -level;
66 604537 level = (int)(level * qscale * quant_matrix[j]) >> 3;
67 604537 level = (level - 1) | 1;
68 604537 level = -level;
69 } else {
70 610899 level = (int)(level * qscale * quant_matrix[j]) >> 3;
71 610899 level = (level - 1) | 1;
72 }
73 1215436 block[j] = level;
74 }
75 }
76 126126 }
77
78 285535 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
79 int16_t *block, int n, int qscale)
80 {
81 int i, level, nCoeffs;
82 const uint16_t *quant_matrix;
83
84 285535 nCoeffs= s->block_last_index[n];
85
86 285535 quant_matrix = s->inter_matrix;
87
2/2
✓ Branch 0 taken 9217310 times.
✓ Branch 1 taken 285535 times.
9502845 for(i=0; i<=nCoeffs; i++) {
88 9217310 int j= s->intra_scantable.permutated[i];
89 9217310 level = block[j];
90
2/2
✓ Branch 0 taken 2101549 times.
✓ Branch 1 taken 7115761 times.
9217310 if (level) {
91
2/2
✓ Branch 0 taken 1052132 times.
✓ Branch 1 taken 1049417 times.
2101549 if (level < 0) {
92 1052132 level = -level;
93 1052132 level = (((level << 1) + 1) * qscale *
94 1052132 ((int) (quant_matrix[j]))) >> 4;
95 1052132 level = (level - 1) | 1;
96 1052132 level = -level;
97 } else {
98 1049417 level = (((level << 1) + 1) * qscale *
99 1049417 ((int) (quant_matrix[j]))) >> 4;
100 1049417 level = (level - 1) | 1;
101 }
102 2101549 block[j] = level;
103 }
104 }
105 285535 }
106
107 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
108 int16_t *block, int n, int qscale)
109 {
110 int i, level, nCoeffs;
111 const uint16_t *quant_matrix;
112
113 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
114 else qscale <<= 1;
115
116 if(s->alternate_scan) nCoeffs= 63;
117 else nCoeffs= s->block_last_index[n];
118
119 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
120 quant_matrix = s->intra_matrix;
121 for(i=1;i<=nCoeffs;i++) {
122 int j= s->intra_scantable.permutated[i];
123 level = block[j];
124 if (level) {
125 if (level < 0) {
126 level = -level;
127 level = (int)(level * qscale * quant_matrix[j]) >> 4;
128 level = -level;
129 } else {
130 level = (int)(level * qscale * quant_matrix[j]) >> 4;
131 }
132 block[j] = level;
133 }
134 }
135 }
136
137 1131872 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
138 int16_t *block, int n, int qscale)
139 {
140 int i, level, nCoeffs;
141 const uint16_t *quant_matrix;
142 1131872 int sum=-1;
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1131872 times.
1131872 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
145 1131872 else qscale <<= 1;
146
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1131872 times.
1131872 if(s->alternate_scan) nCoeffs= 63;
148 1131872 else nCoeffs= s->block_last_index[n];
149
150
2/2
✓ Branch 0 taken 702568 times.
✓ Branch 1 taken 429304 times.
1131872 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
151 1131872 sum += block[0];
152 1131872 quant_matrix = s->intra_matrix;
153
2/2
✓ Branch 0 taken 18356758 times.
✓ Branch 1 taken 1131872 times.
19488630 for(i=1;i<=nCoeffs;i++) {
154 18356758 int j= s->intra_scantable.permutated[i];
155 18356758 level = block[j];
156
2/2
✓ Branch 0 taken 9025237 times.
✓ Branch 1 taken 9331521 times.
18356758 if (level) {
157
2/2
✓ Branch 0 taken 4537630 times.
✓ Branch 1 taken 4487607 times.
9025237 if (level < 0) {
158 4537630 level = -level;
159 4537630 level = (int)(level * qscale * quant_matrix[j]) >> 4;
160 4537630 level = -level;
161 } else {
162 4487607 level = (int)(level * qscale * quant_matrix[j]) >> 4;
163 }
164 9025237 block[j] = level;
165 9025237 sum+=level;
166 }
167 }
168 1131872 block[63]^=sum&1;
169 1131872 }
170
171 2598777 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
172 int16_t *block, int n, int qscale)
173 {
174 int i, level, nCoeffs;
175 const uint16_t *quant_matrix;
176 2598777 int sum=-1;
177
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2598777 times.
2598777 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
179 2598777 else qscale <<= 1;
180
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2598777 times.
2598777 if(s->alternate_scan) nCoeffs= 63;
182 2598777 else nCoeffs= s->block_last_index[n];
183
184 2598777 quant_matrix = s->inter_matrix;
185
2/2
✓ Branch 0 taken 63010806 times.
✓ Branch 1 taken 2598777 times.
65609583 for(i=0; i<=nCoeffs; i++) {
186 63010806 int j= s->intra_scantable.permutated[i];
187 63010806 level = block[j];
188
2/2
✓ Branch 0 taken 22238600 times.
✓ Branch 1 taken 40772206 times.
63010806 if (level) {
189
2/2
✓ Branch 0 taken 11146218 times.
✓ Branch 1 taken 11092382 times.
22238600 if (level < 0) {
190 11146218 level = -level;
191 11146218 level = (((level << 1) + 1) * qscale *
192 11146218 ((int) (quant_matrix[j]))) >> 5;
193 11146218 level = -level;
194 } else {
195 11092382 level = (((level << 1) + 1) * qscale *
196 11092382 ((int) (quant_matrix[j]))) >> 5;
197 }
198 22238600 block[j] = level;
199 22238600 sum+=level;
200 }
201 }
202 2598777 block[63]^=sum&1;
203 2598777 }
204
205 3386658 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
206 int16_t *block, int n, int qscale)
207 {
208 int i, level, qmul, qadd;
209 int nCoeffs;
210
211 av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
212
213 3386658 qmul = qscale << 1;
214
215
2/2
✓ Branch 0 taken 3213462 times.
✓ Branch 1 taken 173196 times.
3386658 if (!s->h263_aic) {
216
2/2
✓ Branch 0 taken 2142308 times.
✓ Branch 1 taken 1071154 times.
3213462 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
217 3213462 qadd = (qscale - 1) | 1;
218 }else{
219 173196 qadd = 0;
220 }
221
2/2
✓ Branch 0 taken 68304 times.
✓ Branch 1 taken 3318354 times.
3386658 if(s->ac_pred)
222 68304 nCoeffs=63;
223 else
224 3318354 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
225
226
2/2
✓ Branch 0 taken 117684880 times.
✓ Branch 1 taken 3386658 times.
121071538 for(i=1; i<=nCoeffs; i++) {
227 117684880 level = block[i];
228
2/2
✓ Branch 0 taken 36101991 times.
✓ Branch 1 taken 81582889 times.
117684880 if (level) {
229
2/2
✓ Branch 0 taken 18078518 times.
✓ Branch 1 taken 18023473 times.
36101991 if (level < 0) {
230 18078518 level = level * qmul - qadd;
231 } else {
232 18023473 level = level * qmul + qadd;
233 }
234 36101991 block[i] = level;
235 }
236 }
237 3386658 }
238
239 6037698 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
240 int16_t *block, int n, int qscale)
241 {
242 int i, level, qmul, qadd;
243 int nCoeffs;
244
245 av_assert2(s->block_last_index[n]>=0);
246
247 6037698 qadd = (qscale - 1) | 1;
248 6037698 qmul = qscale << 1;
249
250 6037698 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
251
252
2/2
✓ Branch 0 taken 230319256 times.
✓ Branch 1 taken 6037698 times.
236356954 for(i=0; i<=nCoeffs; i++) {
253 230319256 level = block[i];
254
2/2
✓ Branch 0 taken 48273100 times.
✓ Branch 1 taken 182046156 times.
230319256 if (level) {
255
2/2
✓ Branch 0 taken 24057698 times.
✓ Branch 1 taken 24215402 times.
48273100 if (level < 0) {
256 24057698 level = level * qmul - qadd;
257 } else {
258 24215402 level = level * qmul + qadd;
259 }
260 48273100 block[i] = level;
261 }
262 }
263 6037698 }
264
265
266 static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
267 {
268 while(h--)
269 memset(dst + h*linesize, 128, 16);
270 }
271
272 static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
273 {
274 while(h--)
275 memset(dst + h*linesize, 128, 8);
276 }
277
278 /* init common dct for both encoder and decoder */
279 758 static av_cold int dct_init(MpegEncContext *s)
280 {
281 758 ff_blockdsp_init(&s->bdsp, s->avctx);
282 758 ff_h264chroma_init(&s->h264chroma, 8); //for lowres
283 758 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
284 758 ff_mpegvideodsp_init(&s->mdsp);
285 758 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
286
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 758 times.
758 if (s->avctx->debug & FF_DEBUG_NOMC) {
288 int i;
289 for (i=0; i<4; i++) {
290 s->hdsp.avg_pixels_tab[0][i] = gray16;
291 s->hdsp.put_pixels_tab[0][i] = gray16;
292 s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
293
294 s->hdsp.avg_pixels_tab[1][i] = gray8;
295 s->hdsp.put_pixels_tab[1][i] = gray8;
296 s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
297 }
298 }
299
300 758 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
301 758 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
302 758 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
303 758 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
304 758 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
305
2/2
✓ Branch 0 taken 546 times.
✓ Branch 1 taken 212 times.
758 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
306 546 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
307 758 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
308
309 if (HAVE_INTRINSICS_NEON)
310 ff_mpv_common_init_neon(s);
311
312 if (ARCH_ALPHA)
313 ff_mpv_common_init_axp(s);
314 if (ARCH_ARM)
315 ff_mpv_common_init_arm(s);
316 if (ARCH_PPC)
317 ff_mpv_common_init_ppc(s);
318 if (ARCH_X86)
319 758 ff_mpv_common_init_x86(s);
320 if (ARCH_MIPS)
321 ff_mpv_common_init_mips(s);
322
323 758 return 0;
324 }
325
326 1101 av_cold void ff_mpv_idct_init(MpegEncContext *s)
327 {
328
2/2
✓ Branch 0 taken 230 times.
✓ Branch 1 taken 871 times.
1101 if (s->codec_id == AV_CODEC_ID_MPEG4)
329 230 s->idsp.mpeg4_studio_profile = s->studio_profile;
330 1101 ff_idctdsp_init(&s->idsp, s->avctx);
331
332 /* load & permutate scantables
333 * note: only wmv uses different ones
334 */
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1101 times.
1101 if (s->alternate_scan) {
336 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
337 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
338 } else {
339 1101 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
340 1101 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
341 }
342 1101 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
343 1101 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
344 1101 }
345
346 841 static int init_duplicate_context(MpegEncContext *s)
347 {
348 841 int y_size = s->b8_stride * (2 * s->mb_height + 1);
349 841 int c_size = s->mb_stride * (s->mb_height + 1);
350 841 int yc_size = y_size + 2 * c_size;
351 int i;
352
353
2/2
✓ Branch 0 taken 145 times.
✓ Branch 1 taken 696 times.
841 if (s->mb_height & 1)
354 145 yc_size += 2*s->b8_stride + 2*s->mb_stride;
355
356
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 643 times.
841 if (s->encoding) {
357
1/2
✓ Branch 1 taken 198 times.
✗ Branch 2 not taken.
198 if (!FF_ALLOCZ_TYPED_ARRAY(s->me.map, ME_MAP_SIZE) ||
358
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 198 times.
198 !FF_ALLOCZ_TYPED_ARRAY(s->me.score_map, ME_MAP_SIZE))
359 return AVERROR(ENOMEM);
360
361
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 194 times.
198 if (s->noise_reduction) {
362
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_error_sum, 2))
363 return AVERROR(ENOMEM);
364 }
365 }
366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 841 times.
841 if (!FF_ALLOCZ_TYPED_ARRAY(s->blocks, 2))
367 return AVERROR(ENOMEM);
368 841 s->block = s->blocks[0];
369
370
2/2
✓ Branch 0 taken 10092 times.
✓ Branch 1 taken 841 times.
10933 for (i = 0; i < 12; i++) {
371 10092 s->pblocks[i] = &s->block[i];
372 }
373
374
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 837 times.
841 if (s->avctx->codec_tag == AV_RL32("VCR2")) {
375 // exchange uv
376 4 FFSWAP(void *, s->pblocks[4], s->pblocks[5]);
377 }
378
379
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 378 times.
841 if (s->out_format == FMT_H263) {
380
1/2
✓ Branch 1 taken 463 times.
✗ Branch 2 not taken.
463 if (!(s->block32 = av_mallocz(sizeof(*s->block32))) ||
381
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 463 times.
463 !(s->dpcm_macroblock = av_mallocz(sizeof(*s->dpcm_macroblock))))
382 return AVERROR(ENOMEM);
383 463 s->dpcm_direction = 0;
384
385 /* ac values */
386
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 463 times.
463 if (!FF_ALLOCZ_TYPED_ARRAY(s->ac_val_base, yc_size))
387 return AVERROR(ENOMEM);
388 463 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
389 463 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
390 463 s->ac_val[2] = s->ac_val[1] + c_size;
391 }
392
393 841 return 0;
394 }
395
396 797 int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
397 {
398 797 int nb_slices = s->slice_context_count, ret;
399
400 /* We initialize the copies before the original so that
401 * fields allocated in init_duplicate_context are NULL after
402 * copying. This prevents double-frees upon allocation error. */
403
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 797 times.
841 for (int i = 1; i < nb_slices; i++) {
404 44 s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (!s->thread_context[i])
406 return AVERROR(ENOMEM);
407
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
44 if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
408 return ret;
409 44 s->thread_context[i]->start_mb_y =
410 44 (s->mb_height * (i ) + nb_slices / 2) / nb_slices;
411 44 s->thread_context[i]->end_mb_y =
412 44 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
413 }
414 797 s->start_mb_y = 0;
415 813 s->end_mb_y = nb_slices > 1 ? (s->mb_height + nb_slices / 2) / nb_slices
416
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 781 times.
797 : s->mb_height;
417 797 return init_duplicate_context(s);
418 }
419
420 861 static void free_duplicate_context(MpegEncContext *s)
421 {
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 861 times.
861 if (!s)
423 return;
424
425 861 av_freep(&s->sc.edge_emu_buffer);
426 861 av_freep(&s->me.scratchpad);
427 861 s->me.temp =
428 861 s->sc.rd_scratchpad =
429 861 s->sc.b_scratchpad =
430 861 s->sc.obmc_scratchpad = NULL;
431
432 861 av_freep(&s->dct_error_sum);
433 861 av_freep(&s->me.map);
434 861 av_freep(&s->me.score_map);
435 861 av_freep(&s->blocks);
436 861 av_freep(&s->block32);
437 861 av_freep(&s->dpcm_macroblock);
438 861 av_freep(&s->ac_val_base);
439 861 s->block = NULL;
440 }
441
442 817 static void free_duplicate_contexts(MpegEncContext *s)
443 {
444
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 817 times.
861 for (int i = 1; i < s->slice_context_count; i++) {
445 44 free_duplicate_context(s->thread_context[i]);
446 44 av_freep(&s->thread_context[i]);
447 }
448 817 free_duplicate_context(s);
449 817 }
450
451 10912 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
452 {
453 #define COPY(a) bak->a = src->a
454 10912 COPY(sc.edge_emu_buffer);
455 10912 COPY(me.scratchpad);
456 10912 COPY(me.temp);
457 10912 COPY(sc.rd_scratchpad);
458 10912 COPY(sc.b_scratchpad);
459 10912 COPY(sc.obmc_scratchpad);
460 10912 COPY(me.map);
461 10912 COPY(me.score_map);
462 10912 COPY(blocks);
463 10912 COPY(block);
464 10912 COPY(block32);
465 10912 COPY(dpcm_macroblock);
466 10912 COPY(dpcm_direction);
467 10912 COPY(start_mb_y);
468 10912 COPY(end_mb_y);
469 10912 COPY(me.map_generation);
470 10912 COPY(pb);
471 10912 COPY(dct_error_sum);
472 10912 COPY(dct_count[0]);
473 10912 COPY(dct_count[1]);
474 10912 COPY(ac_val_base);
475 10912 COPY(ac_val[0]);
476 10912 COPY(ac_val[1]);
477 10912 COPY(ac_val[2]);
478 #undef COPY
479 10912 }
480
481 5456 int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
482 {
483 MpegEncContext bak;
484 int i, ret;
485 // FIXME copy only needed parts
486 5456 backup_duplicate_context(&bak, dst);
487 5456 memcpy(dst, src, sizeof(MpegEncContext));
488 5456 backup_duplicate_context(dst, &bak);
489
2/2
✓ Branch 0 taken 65472 times.
✓ Branch 1 taken 5456 times.
70928 for (i = 0; i < 12; i++) {
490 65472 dst->pblocks[i] = &dst->block[i];
491 }
492
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5456 times.
5456 if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
493 // exchange uv
494 FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
495 }
496
3/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5412 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
5500 if (!dst->sc.edge_emu_buffer &&
497 44 (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
498 44 &dst->sc, dst->linesize)) < 0) {
499 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
500 "scratch buffers.\n");
501 return ret;
502 }
503 5456 return 0;
504 }
505
506 /**
507 * Set the given MpegEncContext to common defaults
508 * (same for encoding and decoding).
509 * The changed fields will not depend upon the
510 * prior state of the MpegEncContext.
511 */
512 783 void ff_mpv_common_defaults(MpegEncContext *s)
513 {
514 783 s->y_dc_scale_table =
515 783 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
516 783 s->chroma_qscale_table = ff_default_chroma_qscale_table;
517 783 s->progressive_frame = 1;
518 783 s->progressive_sequence = 1;
519 783 s->picture_structure = PICT_FRAME;
520
521 783 s->coded_picture_number = 0;
522 783 s->picture_number = 0;
523
524 783 s->f_code = 1;
525 783 s->b_code = 1;
526
527 783 s->slice_context_count = 1;
528 783 }
529
530 797 int ff_mpv_init_context_frame(MpegEncContext *s)
531 {
532 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
533
534 797 s->mb_width = (s->width + 15) / 16;
535 797 s->mb_stride = s->mb_width + 1;
536 797 s->b8_stride = s->mb_width * 2 + 1;
537 797 mb_array_size = s->mb_height * s->mb_stride;
538 797 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
539
540 /* set default edge pos, will be overridden
541 * in decode_header if needed */
542 797 s->h_edge_pos = s->mb_width * 16;
543 797 s->v_edge_pos = s->mb_height * 16;
544
545 797 s->mb_num = s->mb_width * s->mb_height;
546
547 797 s->block_wrap[0] =
548 797 s->block_wrap[1] =
549 797 s->block_wrap[2] =
550 797 s->block_wrap[3] = s->b8_stride;
551 797 s->block_wrap[4] =
552 797 s->block_wrap[5] = s->mb_stride;
553
554 797 y_size = s->b8_stride * (2 * s->mb_height + 1);
555 797 c_size = s->mb_stride * (s->mb_height + 1);
556 797 yc_size = y_size + 2 * c_size;
557
558
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 653 times.
797 if (s->mb_height & 1)
559 144 yc_size += 2*s->b8_stride + 2*s->mb_stride;
560
561
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 797 times.
797 if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_index2xy, s->mb_num + 1))
562 return AVERROR(ENOMEM);
563
2/2
✓ Branch 0 taken 14726 times.
✓ Branch 1 taken 797 times.
15523 for (y = 0; y < s->mb_height; y++)
564
2/2
✓ Branch 0 taken 443741 times.
✓ Branch 1 taken 14726 times.
458467 for (x = 0; x < s->mb_width; x++)
565 443741 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
566
567 797 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
568
569
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 611 times.
797 if (s->encoding) {
570 /* Allocate MV tables */
571
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 if (!FF_ALLOCZ_TYPED_ARRAY(s->p_mv_table_base, mv_table_size) ||
572
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 !FF_ALLOCZ_TYPED_ARRAY(s->b_forw_mv_table_base, mv_table_size) ||
573
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 !FF_ALLOCZ_TYPED_ARRAY(s->b_back_mv_table_base, mv_table_size) ||
574
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_forw_mv_table_base, mv_table_size) ||
575
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_back_mv_table_base, mv_table_size) ||
576
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 186 times.
186 !FF_ALLOCZ_TYPED_ARRAY(s->b_direct_mv_table_base, mv_table_size))
577 return AVERROR(ENOMEM);
578 186 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
579 186 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
580 186 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
581 186 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
582 186 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
583 186 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
584
585 /* Allocate MB type table */
586
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_type, mb_array_size) ||
587
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 !FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size) ||
588
1/2
✓ Branch 1 taken 186 times.
✗ Branch 2 not taken.
186 !FF_ALLOC_TYPED_ARRAY (s->cplx_tab, mb_array_size) ||
589
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 186 times.
186 !FF_ALLOC_TYPED_ARRAY (s->bits_tab, mb_array_size))
590 return AVERROR(ENOMEM);
591
592 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
593
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 60 times.
186 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
594
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 110 times.
126 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
595 int16_t (*tmp1)[2];
596 uint8_t *tmp2;
597
1/2
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
76 if (!(tmp1 = ALLOCZ_ARRAYS(s->b_field_mv_table_base, 8, mv_table_size)) ||
598
1/2
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
76 !(tmp2 = ALLOCZ_ARRAYS(s->b_field_select_table[0][0], 2 * 4, mv_table_size)) ||
599
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
76 !ALLOCZ_ARRAYS(s->p_field_select_table[0], 2 * 2, mv_table_size))
600 return AVERROR(ENOMEM);
601
602 76 s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size;
603 76 tmp1 += s->mb_stride + 1;
604
605
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 76 times.
228 for (int i = 0; i < 2; i++) {
606
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 152 times.
456 for (int j = 0; j < 2; j++) {
607
2/2
✓ Branch 0 taken 608 times.
✓ Branch 1 taken 304 times.
912 for (int k = 0; k < 2; k++) {
608 608 s->b_field_mv_table[i][j][k] = tmp1;
609 608 tmp1 += mv_table_size;
610 }
611 304 s->b_field_select_table[i][j] = tmp2;
612 304 tmp2 += 2 * mv_table_size;
613 }
614 }
615 }
616 }
617
618
2/2
✓ Branch 0 taken 543 times.
✓ Branch 1 taken 254 times.
797 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
619
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 527 times.
543 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
620 int16_t (*tmp)[2];
621 /* interlaced direct mode decoding tables */
622
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 270 times.
270 if (!(tmp = ALLOCZ_ARRAYS(s->p_field_mv_table_base, 4, mv_table_size)))
623 return AVERROR(ENOMEM);
624 270 tmp += s->mb_stride + 1;
625
2/2
✓ Branch 0 taken 540 times.
✓ Branch 1 taken 270 times.
810 for (int i = 0; i < 2; i++) {
626
2/2
✓ Branch 0 taken 1080 times.
✓ Branch 1 taken 540 times.
1620 for (int j = 0; j < 2; j++) {
627 1080 s->p_field_mv_table[i][j] = tmp;
628 1080 tmp += mv_table_size;
629 }
630 }
631 }
632
633
2/2
✓ Branch 0 taken 459 times.
✓ Branch 1 taken 338 times.
797 if (s->out_format == FMT_H263) {
634 /* cbp values, cbp, ac_pred, pred_dir */
635
1/2
✓ Branch 1 taken 459 times.
✗ Branch 2 not taken.
459 if (!(s->coded_block_base = av_mallocz(y_size + (s->mb_height&1)*2*s->b8_stride)) ||
636
1/2
✓ Branch 1 taken 459 times.
✗ Branch 2 not taken.
459 !(s->cbp_table = av_mallocz(mb_array_size)) ||
637
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 459 times.
459 !(s->pred_dir_table = av_mallocz(mb_array_size)))
638 return AVERROR(ENOMEM);
639 459 s->coded_block = s->coded_block_base + s->b8_stride + 1;
640 }
641
642
6/6
✓ Branch 0 taken 439 times.
✓ Branch 1 taken 358 times.
✓ Branch 2 taken 426 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 323 times.
✓ Branch 5 taken 103 times.
797 if (s->h263_pred || s->h263_plus || !s->encoding) {
643 /* dc values */
644 // MN: we need these for error resilience of intra-frames
645
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 694 times.
694 if (!FF_ALLOCZ_TYPED_ARRAY(s->dc_val_base, yc_size))
646 return AVERROR(ENOMEM);
647 694 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
648 694 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
649 694 s->dc_val[2] = s->dc_val[1] + c_size;
650
2/2
✓ Branch 0 taken 2543370 times.
✓ Branch 1 taken 694 times.
2544064 for (i = 0; i < yc_size; i++)
651 2543370 s->dc_val_base[i] = 1024;
652 }
653
654 /* which mb is an intra block, init macroblock skip table */
655
1/2
✓ Branch 1 taken 797 times.
✗ Branch 2 not taken.
797 if (!(s->mbintra_table = av_mallocz(mb_array_size)) ||
656 // Note the + 1 is for a quicker MPEG-4 slice_end detection
657
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 797 times.
797 !(s->mbskip_table = av_mallocz(mb_array_size + 2)))
658 return AVERROR(ENOMEM);
659 797 memset(s->mbintra_table, 1, mb_array_size);
660
661
2/2
✓ Branch 0 taken 611 times.
✓ Branch 1 taken 186 times.
797 return !CONFIG_MPEGVIDEODEC || s->encoding ? 0 : ff_mpeg_er_init(s);
662 }
663
664 758 static void clear_context(MpegEncContext *s)
665 {
666 int i, j, k;
667
668 758 memset(&s->next_picture, 0, sizeof(s->next_picture));
669 758 memset(&s->last_picture, 0, sizeof(s->last_picture));
670 758 memset(&s->current_picture, 0, sizeof(s->current_picture));
671 758 memset(&s->new_picture, 0, sizeof(s->new_picture));
672
673 758 memset(s->thread_context, 0, sizeof(s->thread_context));
674
675 758 s->me.map = NULL;
676 758 s->me.score_map = NULL;
677 758 s->dct_error_sum = NULL;
678 758 s->block = NULL;
679 758 s->blocks = NULL;
680 758 s->block32 = NULL;
681 758 memset(s->pblocks, 0, sizeof(s->pblocks));
682 758 s->dpcm_direction = 0;
683 758 s->dpcm_macroblock = NULL;
684 758 s->ac_val_base = NULL;
685 758 s->ac_val[0] =
686 758 s->ac_val[1] =
687 758 s->ac_val[2] =NULL;
688 758 s->sc.edge_emu_buffer = NULL;
689 758 s->me.scratchpad = NULL;
690 758 s->me.temp =
691 758 s->sc.rd_scratchpad =
692 758 s->sc.b_scratchpad =
693 758 s->sc.obmc_scratchpad = NULL;
694
695
696 758 s->bitstream_buffer = NULL;
697 758 s->allocated_bitstream_buffer_size = 0;
698 758 s->picture = NULL;
699 758 s->mb_type = NULL;
700 758 s->p_mv_table_base = NULL;
701 758 s->b_forw_mv_table_base = NULL;
702 758 s->b_back_mv_table_base = NULL;
703 758 s->b_bidir_forw_mv_table_base = NULL;
704 758 s->b_bidir_back_mv_table_base = NULL;
705 758 s->b_direct_mv_table_base = NULL;
706 758 s->p_mv_table = NULL;
707 758 s->b_forw_mv_table = NULL;
708 758 s->b_back_mv_table = NULL;
709 758 s->b_bidir_forw_mv_table = NULL;
710 758 s->b_bidir_back_mv_table = NULL;
711 758 s->b_direct_mv_table = NULL;
712 758 s->b_field_mv_table_base = NULL;
713 758 s->p_field_mv_table_base = NULL;
714
2/2
✓ Branch 0 taken 1516 times.
✓ Branch 1 taken 758 times.
2274 for (i = 0; i < 2; i++) {
715
2/2
✓ Branch 0 taken 3032 times.
✓ Branch 1 taken 1516 times.
4548 for (j = 0; j < 2; j++) {
716
2/2
✓ Branch 0 taken 6064 times.
✓ Branch 1 taken 3032 times.
9096 for (k = 0; k < 2; k++) {
717 6064 s->b_field_mv_table[i][j][k] = NULL;
718 }
719 3032 s->b_field_select_table[i][j] = NULL;
720 3032 s->p_field_mv_table[i][j] = NULL;
721 }
722 1516 s->p_field_select_table[i] = NULL;
723 }
724
725 758 s->dc_val_base = NULL;
726 758 s->coded_block_base = NULL;
727 758 s->mbintra_table = NULL;
728 758 s->cbp_table = NULL;
729 758 s->pred_dir_table = NULL;
730
731 758 s->mbskip_table = NULL;
732
733 758 s->er.error_status_table = NULL;
734 758 s->er.er_temp_buffer = NULL;
735 758 s->mb_index2xy = NULL;
736 758 s->lambda_table = NULL;
737
738 758 s->cplx_tab = NULL;
739 758 s->bits_tab = NULL;
740 758 }
741
742 /**
743 * init common structure for both encoder and decoder.
744 * this assumes that some variables like width/height are already set
745 */
746 758 av_cold int ff_mpv_common_init(MpegEncContext *s)
747 {
748 int i, ret;
749 758 int nb_slices = (HAVE_THREADS &&
750 758 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
751
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 742 times.
758 s->avctx->thread_count : 1;
752
753 758 clear_context(s);
754
755
4/4
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 572 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 174 times.
758 if (s->encoding && s->avctx->slices)
756 12 nb_slices = s->avctx->slices;
757
758
4/4
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 503 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 127 times.
758 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
759 128 s->mb_height = (s->height + 31) / 32 * 2;
760 else
761 630 s->mb_height = (s->height + 15) / 16;
762
763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 758 times.
758 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
764 av_log(s->avctx, AV_LOG_ERROR,
765 "decoding to AV_PIX_FMT_NONE is not supported.\n");
766 return AVERROR(EINVAL);
767 }
768
769
4/6
✓ Branch 0 taken 758 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 743 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
758 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
770 int max_slices;
771 if (s->mb_height)
772 max_slices = FFMIN(MAX_THREADS, s->mb_height);
773 else
774 max_slices = MAX_THREADS;
775 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
776 " reducing to %d\n", nb_slices, max_slices);
777 nb_slices = max_slices;
778 }
779
780
4/6
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 743 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 743 times.
1501 if ((s->width || s->height) &&
781 743 av_image_check_size(s->width, s->height, 0, s->avctx))
782 return AVERROR(EINVAL);
783
784 758 dct_init(s);
785
786 /* set chroma shifts */
787 758 ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
788 &s->chroma_x_shift,
789 &s->chroma_y_shift);
790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 758 times.
758 if (ret)
791 return ret;
792
793
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 758 times.
758 if (!FF_ALLOCZ_TYPED_ARRAY(s->picture, MAX_PICTURE_COUNT))
794 return AVERROR(ENOMEM);
795
2/2
✓ Branch 0 taken 27288 times.
✓ Branch 1 taken 758 times.
28046 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
796 27288 s->picture[i].f = av_frame_alloc();
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27288 times.
27288 if (!s->picture[i].f)
798 goto fail_nomem;
799 }
800
801
1/2
✓ Branch 1 taken 758 times.
✗ Branch 2 not taken.
758 if (!(s->next_picture.f = av_frame_alloc()) ||
802
1/2
✓ Branch 1 taken 758 times.
✗ Branch 2 not taken.
758 !(s->last_picture.f = av_frame_alloc()) ||
803
1/2
✓ Branch 1 taken 758 times.
✗ Branch 2 not taken.
758 !(s->current_picture.f = av_frame_alloc()) ||
804
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 758 times.
758 !(s->new_picture.f = av_frame_alloc()))
805 goto fail_nomem;
806
807
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 758 times.
758 if ((ret = ff_mpv_init_context_frame(s)))
808 goto fail;
809
810 #if FF_API_FLAG_TRUNCATED
811 758 s->parse_context.state = -1;
812 #endif
813
814 758 s->context_initialized = 1;
815 758 memset(s->thread_context, 0, sizeof(s->thread_context));
816 758 s->thread_context[0] = s;
817 758 s->slice_context_count = nb_slices;
818
819 // if (s->width && s->height) {
820 758 ret = ff_mpv_init_duplicate_contexts(s);
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 758 times.
758 if (ret < 0)
822 goto fail;
823 // }
824
825 758 return 0;
826 fail_nomem:
827 ret = AVERROR(ENOMEM);
828 fail:
829 ff_mpv_common_end(s);
830 return ret;
831 }
832
833 817 void ff_mpv_free_context_frame(MpegEncContext *s)
834 {
835 int i, j, k;
836
837 817 free_duplicate_contexts(s);
838
839 817 av_freep(&s->mb_type);
840 817 av_freep(&s->p_mv_table_base);
841 817 av_freep(&s->b_forw_mv_table_base);
842 817 av_freep(&s->b_back_mv_table_base);
843 817 av_freep(&s->b_bidir_forw_mv_table_base);
844 817 av_freep(&s->b_bidir_back_mv_table_base);
845 817 av_freep(&s->b_direct_mv_table_base);
846 817 s->p_mv_table = NULL;
847 817 s->b_forw_mv_table = NULL;
848 817 s->b_back_mv_table = NULL;
849 817 s->b_bidir_forw_mv_table = NULL;
850 817 s->b_bidir_back_mv_table = NULL;
851 817 s->b_direct_mv_table = NULL;
852 817 av_freep(&s->b_field_mv_table_base);
853 817 av_freep(&s->b_field_select_table[0][0]);
854 817 av_freep(&s->p_field_mv_table_base);
855 817 av_freep(&s->p_field_select_table[0]);
856
2/2
✓ Branch 0 taken 1634 times.
✓ Branch 1 taken 817 times.
2451 for (i = 0; i < 2; i++) {
857
2/2
✓ Branch 0 taken 3268 times.
✓ Branch 1 taken 1634 times.
4902 for (j = 0; j < 2; j++) {
858
2/2
✓ Branch 0 taken 6536 times.
✓ Branch 1 taken 3268 times.
9804 for (k = 0; k < 2; k++) {
859 6536 s->b_field_mv_table[i][j][k] = NULL;
860 }
861 3268 s->b_field_select_table[i][j] = NULL;
862 3268 s->p_field_mv_table[i][j] = NULL;
863 }
864 1634 s->p_field_select_table[i] = NULL;
865 }
866
867 817 av_freep(&s->dc_val_base);
868 817 av_freep(&s->coded_block_base);
869 817 av_freep(&s->mbintra_table);
870 817 av_freep(&s->cbp_table);
871 817 av_freep(&s->pred_dir_table);
872
873 817 av_freep(&s->mbskip_table);
874
875 817 av_freep(&s->er.error_status_table);
876 817 av_freep(&s->er.er_temp_buffer);
877 817 av_freep(&s->mb_index2xy);
878 817 av_freep(&s->lambda_table);
879
880 817 av_freep(&s->cplx_tab);
881 817 av_freep(&s->bits_tab);
882
883 817 s->linesize = s->uvlinesize = 0;
884 817 }
885
886 /* init common structure for both encoder and decoder */
887 778 void ff_mpv_common_end(MpegEncContext *s)
888 {
889 int i;
890
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 778 times.
778 if (!s)
892 return;
893
894 778 ff_mpv_free_context_frame(s);
895
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 762 times.
778 if (s->slice_context_count > 1)
896 16 s->slice_context_count = 1;
897
898 #if FF_API_FLAG_TRUNCATED
899 778 av_freep(&s->parse_context.buffer);
900 778 s->parse_context.buffer_size = 0;
901 #endif
902
903 778 av_freep(&s->bitstream_buffer);
904 778 s->allocated_bitstream_buffer_size = 0;
905
906
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 778 times.
778 if (!s->avctx)
907 return;
908
909
2/2
✓ Branch 0 taken 758 times.
✓ Branch 1 taken 20 times.
778 if (s->picture) {
910
2/2
✓ Branch 0 taken 27288 times.
✓ Branch 1 taken 758 times.
28046 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
911 27288 ff_free_picture_tables(&s->picture[i]);
912 27288 ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
913 27288 av_frame_free(&s->picture[i].f);
914 }
915 }
916 778 av_freep(&s->picture);
917 778 ff_free_picture_tables(&s->last_picture);
918 778 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
919 778 av_frame_free(&s->last_picture.f);
920 778 ff_free_picture_tables(&s->current_picture);
921 778 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
922 778 av_frame_free(&s->current_picture.f);
923 778 ff_free_picture_tables(&s->next_picture);
924 778 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
925 778 av_frame_free(&s->next_picture.f);
926 778 ff_free_picture_tables(&s->new_picture);
927 778 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
928 778 av_frame_free(&s->new_picture.f);
929
930 778 s->context_initialized = 0;
931 778 s->context_reinit = 0;
932 778 s->last_picture_ptr =
933 778 s->next_picture_ptr =
934 778 s->current_picture_ptr = NULL;
935 778 s->linesize = s->uvlinesize = 0;
936 }
937
938
939 126300 static inline int hpel_motion_lowres(MpegEncContext *s,
940 uint8_t *dest, uint8_t *src,
941 int field_based, int field_select,
942 int src_x, int src_y,
943 int width, int height, ptrdiff_t stride,
944 int h_edge_pos, int v_edge_pos,
945 int w, int h, h264_chroma_mc_func *pix_op,
946 int motion_x, int motion_y)
947 {
948 126300 const int lowres = s->avctx->lowres;
949 126300 const int op_index = FFMIN(lowres, 3);
950 126300 const int s_mask = (2 << lowres) - 1;
951 126300 int emu = 0;
952 int sx, sy;
953
954
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (s->quarter_sample) {
955 motion_x /= 2;
956 motion_y /= 2;
957 }
958
959 126300 sx = motion_x & s_mask;
960 126300 sy = motion_y & s_mask;
961 126300 src_x += motion_x >> lowres + 1;
962 126300 src_y += motion_y >> lowres + 1;
963
964 126300 src += src_y * stride + src_x;
965
966
1/2
✓ Branch 0 taken 126300 times.
✗ Branch 1 not taken.
126300 if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
967
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 126002 times.
126300 (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
968 298 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
969 s->linesize, s->linesize,
970 298 w + 1, (h + 1) << field_based,
971 src_x, src_y << field_based,
972 h_edge_pos, v_edge_pos);
973 298 src = s->sc.edge_emu_buffer;
974 298 emu = 1;
975 }
976
977 126300 sx = (sx << 2) >> lowres;
978 126300 sy = (sy << 2) >> lowres;
979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (field_select)
980 src += s->linesize;
981 126300 pix_op[op_index](dest, src, stride, h, sx, sy);
982 126300 return emu;
983 }
984
985 /* apply one mpeg motion vector to the three components */
986 24814 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
987 uint8_t *dest_y,
988 uint8_t *dest_cb,
989 uint8_t *dest_cr,
990 int field_based,
991 int bottom_field,
992 int field_select,
993 uint8_t **ref_picture,
994 h264_chroma_mc_func *pix_op,
995 int motion_x, int motion_y,
996 int h, int mb_y)
997 {
998 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
999 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
1000 ptrdiff_t uvlinesize, linesize;
1001 24814 const int lowres = s->avctx->lowres;
1002 24814 const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
1003 24814 const int block_s = 8>>lowres;
1004 24814 const int s_mask = (2 << lowres) - 1;
1005 24814 const int h_edge_pos = s->h_edge_pos >> lowres;
1006 24814 const int v_edge_pos = s->v_edge_pos >> lowres;
1007 24814 linesize = s->current_picture.f->linesize[0] << field_based;
1008 24814 uvlinesize = s->current_picture.f->linesize[1] << field_based;
1009
1010 // FIXME obviously not perfect but qpel will not work in lowres anyway
1011
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (s->quarter_sample) {
1012 motion_x /= 2;
1013 motion_y /= 2;
1014 }
1015
1016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if(field_based){
1017 motion_y += (bottom_field - field_select)*((1 << lowres)-1);
1018 }
1019
1020 24814 sx = motion_x & s_mask;
1021 24814 sy = motion_y & s_mask;
1022 24814 src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
1023 24814 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
1024
1025
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (s->out_format == FMT_H263) {
1026 24814 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
1027 24814 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
1028 24814 uvsrc_x = src_x >> 1;
1029 24814 uvsrc_y = src_y >> 1;
1030 } else if (s->out_format == FMT_H261) {
1031 // even chroma mv's are full pel in H261
1032 mx = motion_x / 4;
1033 my = motion_y / 4;
1034 uvsx = (2 * mx) & s_mask;
1035 uvsy = (2 * my) & s_mask;
1036 uvsrc_x = s->mb_x * block_s + (mx >> lowres);
1037 uvsrc_y = mb_y * block_s + (my >> lowres);
1038 } else {
1039 if(s->chroma_y_shift){
1040 mx = motion_x / 2;
1041 my = motion_y / 2;
1042 uvsx = mx & s_mask;
1043 uvsy = my & s_mask;
1044 uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
1045 uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
1046 } else {
1047 if(s->chroma_x_shift){
1048 //Chroma422
1049 mx = motion_x / 2;
1050 uvsx = mx & s_mask;
1051 uvsy = motion_y & s_mask;
1052 uvsrc_y = src_y;
1053 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
1054 } else {
1055 //Chroma444
1056 uvsx = motion_x & s_mask;
1057 uvsy = motion_y & s_mask;
1058 uvsrc_x = src_x;
1059 uvsrc_y = src_y;
1060 }
1061 }
1062 }
1063
1064 24814 ptr_y = ref_picture[0] + src_y * linesize + src_x;
1065 24814 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
1066 24814 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
1067
1068
3/4
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24712 times.
✓ Branch 3 taken 102 times.
24814 if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
1069
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 24617 times.
24712 (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
1070 197 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
1071 linesize >> field_based, linesize >> field_based,
1072 17, 17 + field_based,
1073 src_x, src_y << field_based, h_edge_pos,
1074 v_edge_pos);
1075 197 ptr_y = s->sc.edge_emu_buffer;
1076 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1077 197 uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
1078 197 uint8_t *vbuf =ubuf + 10 * s->uvlinesize;
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (s->workaround_bugs & FF_BUG_IEDGE)
1080 vbuf -= s->uvlinesize;
1081 197 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
1082 uvlinesize >> field_based, uvlinesize >> field_based,
1083 9, 9 + field_based,
1084 uvsrc_x, uvsrc_y << field_based,
1085 h_edge_pos >> 1, v_edge_pos >> 1);
1086 197 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
1087 uvlinesize >> field_based,uvlinesize >> field_based,
1088 9, 9 + field_based,
1089 uvsrc_x, uvsrc_y << field_based,
1090 h_edge_pos >> 1, v_edge_pos >> 1);
1091 197 ptr_cb = ubuf;
1092 197 ptr_cr = vbuf;
1093 }
1094 }
1095
1096 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
1097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (bottom_field) {
1098 dest_y += s->linesize;
1099 dest_cb += s->uvlinesize;
1100 dest_cr += s->uvlinesize;
1101 }
1102
1103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (field_select) {
1104 ptr_y += s->linesize;
1105 ptr_cb += s->uvlinesize;
1106 ptr_cr += s->uvlinesize;
1107 }
1108
1109 24814 sx = (sx << 2) >> lowres;
1110 24814 sy = (sy << 2) >> lowres;
1111 24814 pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
1112
1113 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1114
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
1115 24814 uvsx = (uvsx << 2) >> lowres;
1116 24814 uvsy = (uvsy << 2) >> lowres;
1117
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (hc) {
1118 24814 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
1119 24814 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
1120 }
1121 }
1122 // FIXME h261 lowres loop filter
1123 24814 }
1124
1125 31575 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
1126 uint8_t *dest_cb, uint8_t *dest_cr,
1127 uint8_t **ref_picture,
1128 h264_chroma_mc_func * pix_op,
1129 int mx, int my)
1130 {
1131 31575 const int lowres = s->avctx->lowres;
1132 31575 const int op_index = FFMIN(lowres, 3);
1133 31575 const int block_s = 8 >> lowres;
1134 31575 const int s_mask = (2 << lowres) - 1;
1135 31575 const int h_edge_pos = s->h_edge_pos >> lowres + 1;
1136 31575 const int v_edge_pos = s->v_edge_pos >> lowres + 1;
1137 31575 int emu = 0, src_x, src_y, sx, sy;
1138 ptrdiff_t offset;
1139 uint8_t *ptr;
1140
1141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31575 times.
31575 if (s->quarter_sample) {
1142 mx /= 2;
1143 my /= 2;
1144 }
1145
1146 /* In case of 8X8, we construct a single chroma motion vector
1147 with a special rounding */
1148 31575 mx = ff_h263_round_chroma(mx);
1149 31575 my = ff_h263_round_chroma(my);
1150
1151 31575 sx = mx & s_mask;
1152 31575 sy = my & s_mask;
1153 31575 src_x = s->mb_x * block_s + (mx >> lowres + 1);
1154 31575 src_y = s->mb_y * block_s + (my >> lowres + 1);
1155
1156 31575 offset = src_y * s->uvlinesize + src_x;
1157 31575 ptr = ref_picture[1] + offset;
1158
1/2
✓ Branch 0 taken 31575 times.
✗ Branch 1 not taken.
31575 if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
1159
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
1160 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
1161 s->uvlinesize, s->uvlinesize,
1162 9, 9,
1163 src_x, src_y, h_edge_pos, v_edge_pos);
1164 144 ptr = s->sc.edge_emu_buffer;
1165 144 emu = 1;
1166 }
1167 31575 sx = (sx << 2) >> lowres;
1168 31575 sy = (sy << 2) >> lowres;
1169 31575 pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
1170
1171 31575 ptr = ref_picture[2] + offset;
1172
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 if (emu) {
1173 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
1174 s->uvlinesize, s->uvlinesize,
1175 9, 9,
1176 src_x, src_y, h_edge_pos, v_edge_pos);
1177 144 ptr = s->sc.edge_emu_buffer;
1178 }
1179 31575 pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
1180 31575 }
1181
1182 /**
1183 * motion compensation of a single macroblock
1184 * @param s context
1185 * @param dest_y luma destination pointer
1186 * @param dest_cb chroma cb/u destination pointer
1187 * @param dest_cr chroma cr/v destination pointer
1188 * @param dir direction (0->forward, 1->backward)
1189 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
1190 * @param pix_op halfpel motion compensation function (average or put normally)
1191 * the motion vectors are taken from s->mv and the MV type from s->mv_type
1192 */
1193 56389 static inline void MPV_motion_lowres(MpegEncContext *s,
1194 uint8_t *dest_y, uint8_t *dest_cb,
1195 uint8_t *dest_cr,
1196 int dir, uint8_t **ref_picture,
1197 h264_chroma_mc_func *pix_op)
1198 {
1199 int mx, my;
1200 int mb_x, mb_y, i;
1201 56389 const int lowres = s->avctx->lowres;
1202 56389 const int block_s = 8 >>lowres;
1203
1204 56389 mb_x = s->mb_x;
1205 56389 mb_y = s->mb_y;
1206
1207
2/6
✓ Branch 0 taken 24814 times.
✓ Branch 1 taken 31575 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
56389 switch (s->mv_type) {
1208 24814 case MV_TYPE_16X16:
1209 24814 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1210 0, 0, 0,
1211 ref_picture, pix_op,
1212 s->mv[dir][0][0], s->mv[dir][0][1],
1213 2 * block_s, mb_y);
1214 24814 break;
1215 31575 case MV_TYPE_8X8:
1216 31575 mx = 0;
1217 31575 my = 0;
1218
2/2
✓ Branch 0 taken 126300 times.
✓ Branch 1 taken 31575 times.
157875 for (i = 0; i < 4; i++) {
1219 126300 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
1220 126300 s->linesize) * block_s,
1221 ref_picture[0], 0, 0,
1222 126300 (2 * mb_x + (i & 1)) * block_s,
1223 126300 (2 * mb_y + (i >> 1)) * block_s,
1224 s->width, s->height, s->linesize,
1225 126300 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
1226 block_s, block_s, pix_op,
1227 s->mv[dir][i][0], s->mv[dir][i][1]);
1228
1229 126300 mx += s->mv[dir][i][0];
1230 126300 my += s->mv[dir][i][1];
1231 }
1232
1233 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
1234 31575 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
1235 pix_op, mx, my);
1236 31575 break;
1237 case MV_TYPE_FIELD:
1238 if (s->picture_structure == PICT_FRAME) {
1239 /* top field */
1240 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1241 1, 0, s->field_select[dir][0],
1242 ref_picture, pix_op,
1243 s->mv[dir][0][0], s->mv[dir][0][1],
1244 block_s, mb_y);
1245 /* bottom field */
1246 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1247 1, 1, s->field_select[dir][1],
1248 ref_picture, pix_op,
1249 s->mv[dir][1][0], s->mv[dir][1][1],
1250 block_s, mb_y);
1251 } else {
1252 if (s->picture_structure != s->field_select[dir][0] + 1 &&
1253 s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
1254 ref_picture = s->current_picture_ptr->f->data;
1255
1256 }
1257 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1258 0, 0, s->field_select[dir][0],
1259 ref_picture, pix_op,
1260 s->mv[dir][0][0],
1261 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
1262 }
1263 break;
1264 case MV_TYPE_16X8:
1265 for (i = 0; i < 2; i++) {
1266 uint8_t **ref2picture;
1267
1268 if (s->picture_structure == s->field_select[dir][i] + 1 ||
1269 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
1270 ref2picture = ref_picture;
1271 } else {
1272 ref2picture = s->current_picture_ptr->f->data;
1273 }
1274
1275 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1276 0, 0, s->field_select[dir][i],
1277 ref2picture, pix_op,
1278 s->mv[dir][i][0], s->mv[dir][i][1] +
1279 2 * block_s * i, block_s, mb_y >> 1);
1280
1281 dest_y += 2 * block_s * s->linesize;
1282 dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
1283 dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
1284 }
1285 break;
1286 case MV_TYPE_DMV:
1287 if (s->picture_structure == PICT_FRAME) {
1288 for (i = 0; i < 2; i++) {
1289 int j;
1290 for (j = 0; j < 2; j++) {
1291 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1292 1, j, j ^ i,
1293 ref_picture, pix_op,
1294 s->mv[dir][2 * i + j][0],
1295 s->mv[dir][2 * i + j][1],
1296 block_s, mb_y);
1297 }
1298 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
1299 }
1300 } else {
1301 for (i = 0; i < 2; i++) {
1302 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1303 0, 0, s->picture_structure != i + 1,
1304 ref_picture, pix_op,
1305 s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
1306 2 * block_s, mb_y >> 1);
1307
1308 // after put we make avg of the same block
1309 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
1310
1311 // opposite parity is always in the same
1312 // frame if this is second field
1313 if (!s->first_field) {
1314 ref_picture = s->current_picture_ptr->f->data;
1315 }
1316 }
1317 }
1318 break;
1319 56389 default:
1320 av_assert2(0);
1321 }
1322 56389 }
1323
1324 /**
1325 * find the lowest MB row referenced in the MVs
1326 */
1327 static int lowest_referenced_row(MpegEncContext *s, int dir)
1328 {
1329 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
1330 int my, off, i, mvs;
1331
1332 if (s->picture_structure != PICT_FRAME || s->mcsel)
1333 goto unhandled;
1334
1335 switch (s->mv_type) {
1336 case MV_TYPE_16X16:
1337 mvs = 1;
1338 break;
1339 case MV_TYPE_16X8:
1340 mvs = 2;
1341 break;
1342 case MV_TYPE_8X8:
1343 mvs = 4;
1344 break;
1345 default:
1346 goto unhandled;
1347 }
1348
1349 for (i = 0; i < mvs; i++) {
1350 my = s->mv[dir][i][1];
1351 my_max = FFMAX(my_max, my);
1352 my_min = FFMIN(my_min, my);
1353 }
1354
1355 off = ((FFMAX(-my_min, my_max)<<qpel_shift) + 63) >> 6;
1356
1357 return av_clip(s->mb_y + off, 0, s->mb_height - 1);
1358 unhandled:
1359 return s->mb_height-1;
1360 }
1361
1362 /* put block[] to dest[] */
1363 4834232 static inline void put_dct(MpegEncContext *s,
1364 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1365 {
1366 4834232 s->dct_unquantize_intra(s, block, i, qscale);
1367 4834232 s->idsp.idct_put(dest, line_size, block);
1368 4834232 }
1369
1370 /* add block[] to dest[] */
1371 19756868 static inline void add_dct(MpegEncContext *s,
1372 int16_t *block, int i, uint8_t *dest, int line_size)
1373 {
1374
2/2
✓ Branch 0 taken 7190377 times.
✓ Branch 1 taken 12566491 times.
19756868 if (s->block_last_index[i] >= 0) {
1375 7190377 s->idsp.idct_add(dest, line_size, block);
1376 }
1377 19756868 }
1378
1379 22591838 static inline void add_dequant_dct(MpegEncContext *s,
1380 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1381 {
1382
2/2
✓ Branch 0 taken 9001045 times.
✓ Branch 1 taken 13590793 times.
22591838 if (s->block_last_index[i] >= 0) {
1383 9001045 s->dct_unquantize_inter(s, block, i, qscale);
1384
1385 9001045 s->idsp.idct_add(dest, line_size, block);
1386 }
1387 22591838 }
1388
1389 /**
1390 * Clean dc, ac, coded_block for the current non-intra MB.
1391 */
1392 389410 void ff_clean_intra_table_entries(MpegEncContext *s)
1393 {
1394 389410 int wrap = s->b8_stride;
1395 389410 int xy = s->block_index[0];
1396
1397 389410 s->dc_val[0][xy ] =
1398 389410 s->dc_val[0][xy + 1 ] =
1399 389410 s->dc_val[0][xy + wrap] =
1400 389410 s->dc_val[0][xy + 1 + wrap] = 1024;
1401 /* ac pred */
1402 389410 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
1403 389410 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
1404
2/2
✓ Branch 0 taken 45701 times.
✓ Branch 1 taken 343709 times.
389410 if (s->msmpeg4_version>=3) {
1405 45701 s->coded_block[xy ] =
1406 45701 s->coded_block[xy + 1 ] =
1407 45701 s->coded_block[xy + wrap] =
1408 45701 s->coded_block[xy + 1 + wrap] = 0;
1409 }
1410 /* chroma */
1411 389410 wrap = s->mb_stride;
1412 389410 xy = s->mb_x + s->mb_y * wrap;
1413 389410 s->dc_val[1][xy] =
1414 389410 s->dc_val[2][xy] = 1024;
1415 /* ac pred */
1416 389410 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1417 389410 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
1418
1419 389410 s->mbintra_table[xy]= 0;
1420 389410 }
1421
1422 /* generic function called after a macroblock has been parsed by the
1423 decoder or after it has been encoded by the encoder.
1424
1425 Important variables used:
1426 s->mb_intra : true if intra macroblock
1427 s->mv_dir : motion vector direction
1428 s->mv_type : motion vector type
1429 s->mv : motion vector
1430 s->interlaced_dct : true if interlaced dct used (mpeg2)
1431 */
1432 static av_always_inline
1433 9088645 void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
1434 int lowres_flag, int is_mpeg12)
1435 {
1436 #define IS_ENCODER(s) (CONFIG_MPEGVIDEOENC && !lowres_flag && (s)->encoding)
1437 #define IS_MPEG12(s) (CONFIG_SMALL ? ((s)->out_format == FMT_MPEG1) : is_mpeg12)
1438 9088645 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
1439
1440 9088645 s->current_picture.qscale_table[mb_xy] = s->qscale;
1441
1442 /* update DC predictors for P macroblocks */
1443
2/2
✓ Branch 0 taken 7326661 times.
✓ Branch 1 taken 1761984 times.
9088645 if (!s->mb_intra) {
1444
6/6
✓ Branch 0 taken 4219701 times.
✓ Branch 1 taken 3106960 times.
✓ Branch 2 taken 952206 times.
✓ Branch 3 taken 3267495 times.
✓ Branch 4 taken 103314 times.
✓ Branch 5 taken 848892 times.
7326661 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
1445
2/2
✓ Branch 0 taken 311786 times.
✓ Branch 1 taken 3059023 times.
3370809 if(s->mbintra_table[mb_xy])
1446 311786 ff_clean_intra_table_entries(s);
1447 } else {
1448 3955852 s->last_dc[0] =
1449 3955852 s->last_dc[1] =
1450 3955852 s->last_dc[2] = 128 << s->intra_dc_precision;
1451 }
1452 }
1453
6/6
✓ Branch 0 taken 1033303 times.
✓ Branch 1 taken 728681 times.
✓ Branch 2 taken 603958 times.
✓ Branch 3 taken 429345 times.
✓ Branch 4 taken 28866 times.
✓ Branch 5 taken 575092 times.
1761984 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
1454 458211 s->mbintra_table[mb_xy]=1;
1455
1456
7/10
✓ Branch 0 taken 9045145 times.
✓ Branch 1 taken 43500 times.
✓ Branch 2 taken 4511251 times.
✓ Branch 3 taken 4533894 times.
✓ Branch 4 taken 4511251 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4511251 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4511251 times.
✗ Branch 9 not taken.
9088645 if (!IS_ENCODER(s) || (s->avctx->flags & AV_CODEC_FLAG_PSNR) || s->frame_skip_threshold || s->frame_skip_factor ||
1457
4/4
✓ Branch 0 taken 3820753 times.
✓ Branch 1 taken 690498 times.
✓ Branch 2 taken 1236491 times.
✓ Branch 3 taken 2584262 times.
4511251 !((s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
1458
2/2
✓ Branch 0 taken 1019795 times.
✓ Branch 1 taken 907194 times.
1926989 s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
1459 uint8_t *dest_y, *dest_cb, *dest_cr;
1460 int dct_linesize, dct_offset;
1461 op_pixels_func (*op_pix)[4];
1462 qpel_mc_func (*op_qpix)[16];
1463 8181451 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1464 8181451 const int uvlinesize = s->current_picture.f->linesize[1];
1465
9/10
✓ Branch 0 taken 2125941 times.
✓ Branch 1 taken 6055510 times.
✓ Branch 2 taken 2104341 times.
✓ Branch 3 taken 21600 times.
✓ Branch 4 taken 1084546 times.
✓ Branch 5 taken 1019795 times.
✓ Branch 6 taken 1106146 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 21600 times.
✓ Branch 9 taken 1084546 times.
8181451 const int readable = s->pict_type != AV_PICTURE_TYPE_B || IS_ENCODER(s) || s->avctx->draw_horiz_band || lowres_flag;
1466
2/2
✓ Branch 0 taken 43500 times.
✓ Branch 1 taken 8137951 times.
8181451 const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
1467
1468 /* avoid copy if macroblock skipped in last frame too */
1469 /* skip only during decoding as we might trash the buffers during encoding a bit */
1470
4/4
✓ Branch 0 taken 8137951 times.
✓ Branch 1 taken 43500 times.
✓ Branch 2 taken 4533894 times.
✓ Branch 3 taken 3604057 times.
8181451 if (!IS_ENCODER(s)) {
1471 4577394 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
1472
1473
2/2
✓ Branch 0 taken 323448 times.
✓ Branch 1 taken 4253946 times.
4577394 if (s->mb_skipped) {
1474 323448 s->mb_skipped= 0;
1475 av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
1476 323448 *mbskip_ptr = 1;
1477
2/2
✓ Branch 0 taken 999062 times.
✓ Branch 1 taken 3254884 times.
4253946 } else if(!s->current_picture.reference) {
1478 999062 *mbskip_ptr = 1;
1479 } else{
1480 3254884 *mbskip_ptr = 0; /* not skipped */
1481 }
1482 }
1483
1484 8181451 dct_linesize = linesize << s->interlaced_dct;
1485
2/2
✓ Branch 0 taken 8030693 times.
✓ Branch 1 taken 150758 times.
8181451 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
1486
1487
2/2
✓ Branch 0 taken 7096905 times.
✓ Branch 1 taken 1084546 times.
8181451 if(readable){
1488 7096905 dest_y= s->dest[0];
1489 7096905 dest_cb= s->dest[1];
1490 7096905 dest_cr= s->dest[2];
1491 }else{
1492 1084546 dest_y = s->sc.b_scratchpad;
1493 1084546 dest_cb= s->sc.b_scratchpad+16*linesize;
1494 1084546 dest_cr= s->sc.b_scratchpad+32*linesize;
1495 }
1496
1497
2/2
✓ Branch 0 taken 7109965 times.
✓ Branch 1 taken 1071486 times.
8181451 if (!s->mb_intra) {
1498 /* motion handling */
1499 /* decoding or more than one mb_type (MC was already done otherwise) */
1500
4/4
✓ Branch 0 taken 7068842 times.
✓ Branch 1 taken 41123 times.
✓ Branch 2 taken 3957870 times.
✓ Branch 3 taken 3110972 times.
7109965 if (!IS_ENCODER(s)) {
1501
1502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3998993 times.
3998993 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1503 if (s->mv_dir & MV_DIR_FORWARD) {
1504 ff_thread_await_progress(&s->last_picture_ptr->tf,
1505 lowest_referenced_row(s, 0),
1506 0);
1507 }
1508 if (s->mv_dir & MV_DIR_BACKWARD) {
1509 ff_thread_await_progress(&s->next_picture_ptr->tf,
1510 lowest_referenced_row(s, 1),
1511 0);
1512 }
1513 }
1514
1515
2/2
✓ Branch 0 taken 41123 times.
✓ Branch 1 taken 3957870 times.
3998993 if(lowres_flag){
1516 41123 h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
1517
1518
2/2
✓ Branch 0 taken 37429 times.
✓ Branch 1 taken 3694 times.
41123 if (s->mv_dir & MV_DIR_FORWARD) {
1519 37429 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
1520 37429 op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
1521 }
1522
2/2
✓ Branch 0 taken 18960 times.
✓ Branch 1 taken 22163 times.
41123 if (s->mv_dir & MV_DIR_BACKWARD) {
1523 18960 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
1524 }
1525 }else{
1526 3957870 op_qpix = s->me.qpel_put;
1527
5/6
✓ Branch 0 taken 2112480 times.
✓ Branch 1 taken 1845390 times.
✓ Branch 2 taken 759743 times.
✓ Branch 3 taken 1352737 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 759743 times.
3957870 if ((is_mpeg12 || !s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1528 3198127 op_pix = s->hdsp.put_pixels_tab;
1529 }else{
1530 759743 op_pix = s->hdsp.put_no_rnd_pixels_tab;
1531 }
1532
2/2
✓ Branch 0 taken 3690417 times.
✓ Branch 1 taken 267453 times.
3957870 if (s->mv_dir & MV_DIR_FORWARD) {
1533 3690417 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
1534 3690417 op_pix = s->hdsp.avg_pixels_tab;
1535 3690417 op_qpix= s->me.qpel_avg;
1536 }
1537
2/2
✓ Branch 0 taken 837796 times.
✓ Branch 1 taken 3120074 times.
3957870 if (s->mv_dir & MV_DIR_BACKWARD) {
1538 837796 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
1539 }
1540 }
1541 }
1542
1543 /* skip dequant / idct if we are really late ;) */
1544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7109965 times.
7109965 if(s->avctx->skip_idct){
1545 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
1546 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
1547 || s->avctx->skip_idct >= AVDISCARD_ALL)
1548 goto skip_idct;
1549 }
1550
1551 /* add dct residue */
1552
8/8
✓ Branch 0 taken 7068842 times.
✓ Branch 1 taken 41123 times.
✓ Branch 2 taken 3957870 times.
✓ Branch 3 taken 3110972 times.
✓ Branch 4 taken 2153603 times.
✓ Branch 5 taken 1845390 times.
✓ Branch 6 taken 1784955 times.
✓ Branch 7 taken 368648 times.
7109965 if (IS_ENCODER(s) || !(IS_MPEG12(s) || s->msmpeg4_version
1553
4/4
✓ Branch 0 taken 1308023 times.
✓ Branch 1 taken 476932 times.
✓ Branch 2 taken 61534 times.
✓ Branch 3 taken 1246489 times.
1784955 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
1554 3649438 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1555 3649438 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1556 3649438 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1557 3649438 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1558
1559 3649438 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1560
2/2
✓ Branch 0 taken 3301833 times.
✓ Branch 1 taken 347605 times.
3649438 if (s->chroma_y_shift){
1561 3301833 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1562 3301833 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1563 }else{
1564 347605 dct_linesize >>= 1;
1565 347605 dct_offset >>=1;
1566 347605 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1567 347605 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1568 347605 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1569 347605 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1570 }
1571 }
1572
4/4
✓ Branch 0 taken 1615137 times.
✓ Branch 1 taken 1845390 times.
✓ Branch 2 taken 1424988 times.
✓ Branch 3 taken 190149 times.
3460527 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
1573 3270378 add_dct(s, block[0], 0, dest_y , dct_linesize);
1574 3270378 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
1575 3270378 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
1576 3270378 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
1577
1578 3270378 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1579
2/2
✓ Branch 0 taken 3203078 times.
✓ Branch 1 taken 67300 times.
3270378 if(s->chroma_y_shift){//Chroma420
1580 3203078 add_dct(s, block[4], 4, dest_cb, uvlinesize);
1581 3203078 add_dct(s, block[5], 5, dest_cr, uvlinesize);
1582 }else{
1583 //chroma422
1584 67300 dct_linesize = uvlinesize << s->interlaced_dct;
1585
2/2
✓ Branch 0 taken 65664 times.
✓ Branch 1 taken 1636 times.
67300 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
1586
1587 67300 add_dct(s, block[4], 4, dest_cb, dct_linesize);
1588 67300 add_dct(s, block[5], 5, dest_cr, dct_linesize);
1589 67300 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
1590 67300 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
1591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67300 times.
67300 if(!s->chroma_x_shift){//Chroma444
1592 add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
1593 add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
1594 add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
1595 add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
1596 }
1597 }
1598 }//fi gray
1599 } else if (CONFIG_WMV2_DECODER) {
1600 190149 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
1601 }
1602 } else {
1603 /* Only MPEG-4 Simple Studio Profile is supported in > 8-bit mode.
1604 TODO: Integrate 10-bit properly into mpegvideo.c so that ER works properly */
1605
3/4
✓ Branch 0 taken 599305 times.
✓ Branch 1 taken 472181 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 599305 times.
1071486 if (!is_mpeg12 && s->avctx->bits_per_raw_sample > 8) {
1606 const int act_block_size = block_size * 2;
1607
1608 if(s->dpcm_direction == 0) {
1609 s->idsp.idct_put(dest_y, dct_linesize, (int16_t*)(*s->block32)[0]);
1610 s->idsp.idct_put(dest_y + act_block_size, dct_linesize, (int16_t*)(*s->block32)[1]);
1611 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, (int16_t*)(*s->block32)[2]);
1612 s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)(*s->block32)[3]);
1613
1614 dct_linesize = uvlinesize << s->interlaced_dct;
1615 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
1616
1617 s->idsp.idct_put(dest_cb, dct_linesize, (int16_t*)(*s->block32)[4]);
1618 s->idsp.idct_put(dest_cr, dct_linesize, (int16_t*)(*s->block32)[5]);
1619 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)(*s->block32)[6]);
1620 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)(*s->block32)[7]);
1621 if(!s->chroma_x_shift){//Chroma444
1622 s->idsp.idct_put(dest_cb + act_block_size, dct_linesize, (int16_t*)(*s->block32)[8]);
1623 s->idsp.idct_put(dest_cr + act_block_size, dct_linesize, (int16_t*)(*s->block32)[9]);
1624 s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)(*s->block32)[10]);
1625 s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)(*s->block32)[11]);
1626 }
1627 } else if(s->dpcm_direction == 1) {
1628 int i, w, h;
1629 uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
1630 int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
1631 for(i = 0; i < 3; i++) {
1632 int idx = 0;
1633 int vsub = i ? s->chroma_y_shift : 0;
1634 int hsub = i ? s->chroma_x_shift : 0;
1635 for(h = 0; h < (16 >> vsub); h++){
1636 for(w = 0; w < (16 >> hsub); w++)
1637 dest_pcm[i][w] = (*s->dpcm_macroblock)[i][idx++];
1638 dest_pcm[i] += linesize[i] / 2;
1639 }
1640 }
1641 } else {
1642 int i, w, h;
1643 uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr};
1644 int linesize[3] = {dct_linesize, uvlinesize, uvlinesize};
1645 av_assert2(s->dpcm_direction == -1);
1646 for(i = 0; i < 3; i++) {
1647 int idx = 0;
1648 int vsub = i ? s->chroma_y_shift : 0;
1649 int hsub = i ? s->chroma_x_shift : 0;
1650 dest_pcm[i] += (linesize[i] / 2) * ((16 >> vsub) - 1);
1651 for(h = (16 >> vsub)-1; h >= 1; h--){
1652 for(w = (16 >> hsub)-1; w >= 1; w--)
1653 dest_pcm[i][w] = (*s->dpcm_macroblock)[i][idx++];
1654 dest_pcm[i] -= linesize[i] / 2;
1655 }
1656 }
1657 }
1658 }
1659 /* dct only in intra block */
1660
6/6
✓ Branch 0 taken 1069109 times.
✓ Branch 1 taken 2377 times.
✓ Branch 2 taken 576024 times.
✓ Branch 3 taken 493085 times.
✓ Branch 4 taken 299617 times.
✓ Branch 5 taken 278784 times.
1071486 else if (IS_ENCODER(s) || !IS_MPEG12(s)) {
1661 792702 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1662 792702 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1663 792702 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1664 792702 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1665
1666 792702 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1667
2/2
✓ Branch 0 taken 753692 times.
✓ Branch 1 taken 39010 times.
792702 if(s->chroma_y_shift){
1668 753692 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1669 753692 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1670 }else{
1671 39010 dct_offset >>=1;
1672 39010 dct_linesize >>=1;
1673 39010 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1674 39010 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1675 39010 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1676 39010 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1677 }
1678 }
1679 }else{
1680 278784 s->idsp.idct_put(dest_y, dct_linesize, block[0]);
1681 278784 s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
1682 278784 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
1683 278784 s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
1684
1685 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1686
2/2
✓ Branch 0 taken 224974 times.
✓ Branch 1 taken 53810 times.
278784 if(s->chroma_y_shift){
1687 224974 s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
1688 224974 s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
1689 }else{
1690
1691 53810 dct_linesize = uvlinesize << s->interlaced_dct;
1692
2/2
✓ Branch 0 taken 53780 times.
✓ Branch 1 taken 30 times.
53810 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
1693
1694 53810 s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
1695 53810 s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
1696 53810 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
1697 53810 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
1698
1/2
✓ Branch 0 taken 53810 times.
✗ Branch 1 not taken.
53810 if(!s->chroma_x_shift){//Chroma444
1699 s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
1700 s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
1701 s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
1702 s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
1703 }
1704 }
1705 }//gray
1706 }
1707 }
1708 53810 skip_idct:
1709
2/2
✓ Branch 0 taken 1084546 times.
✓ Branch 1 taken 7096905 times.
8181451 if(!readable){
1710 1084546 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
1711 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1712 1084546 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
1713 1084546 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
1714 }
1715 }
1716 }
1717 9088645 }
1718
1719 9088645 void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
1720 {
1721 if (CONFIG_XVMC &&
1722 s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
1723 s->avctx->hwaccel->decode_mb(s); //xvmc uses pblocks
1724 return;
1725 }
1726
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9088645 times.
9088645 if (s->avctx->debug & FF_DEBUG_DCT_COEFF) {
1728 /* print DCT coefficients */
1729 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
1730 for (int i = 0; i < 6; i++) {
1731 for (int j = 0; j < 64; j++) {
1732 av_log(s->avctx, AV_LOG_DEBUG, "%5d",
1733 block[i][s->idsp.idct_permutation[j]]);
1734 }
1735 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1736 }
1737 }
1738
1739 #if !CONFIG_SMALL
1740
2/2
✓ Branch 0 taken 3835641 times.
✓ Branch 1 taken 5253004 times.
9088645 if(s->out_format == FMT_MPEG1) {
1741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3835641 times.
3835641 if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 1);
1742 3835641 else mpv_reconstruct_mb_internal(s, block, 0, 1);
1743 } else
1744 #endif
1745
2/2
✓ Branch 0 taken 43500 times.
✓ Branch 1 taken 5209504 times.
5253004 if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 0);
1746 5209504 else mpv_reconstruct_mb_internal(s, block, 0, 0);
1747 }
1748
1749 849990 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
1750 849990 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1751 849990 const int uvlinesize = s->current_picture.f->linesize[1];
1752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 849990 times.
849990 const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
1753 849990 const int height_of_mb = 4 - s->avctx->lowres;
1754
1755 849990 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
1756 849990 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
1757 849990 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
1758 849990 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
1759 849990 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
1760 849990 s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
1761 //block_index is not used by mpeg2, so it is not affected by chroma_format
1762
1763 849990 s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) << width_of_mb);
1764 849990 s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
1765 849990 s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
1766
1767
3/6
✓ Branch 0 taken 116031 times.
✓ Branch 1 taken 733959 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 116031 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
849990 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
1768 {
1769
2/2
✓ Branch 0 taken 823410 times.
✓ Branch 1 taken 26580 times.
849990 if(s->picture_structure==PICT_FRAME){
1770 823410 s->dest[0] += s->mb_y * linesize << height_of_mb;
1771 823410 s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
1772 823410 s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
1773 }else{
1774 26580 s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
1775 26580 s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
1776 26580 s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
1777 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
1778 }
1779 }
1780 849990 }
1781
1782 /**
1783 * set qscale and update qscale dependent variables.
1784 */
1785 1452048 void ff_set_qscale(MpegEncContext * s, int qscale)
1786 {
1787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1452048 times.
1452048 if (qscale < 1)
1788 qscale = 1;
1789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1452048 times.
1452048 else if (qscale > 31)
1790 qscale = 31;
1791
1792 1452048 s->qscale = qscale;
1793 1452048 s->chroma_qscale= s->chroma_qscale_table[qscale];
1794
1795 1452048 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
1796 1452048 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
1797 1452048 }
1798