FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 482 533 90.4%
Functions: 25 27 92.6%
Branches: 190 236 80.5%

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 #include "libavutil/mem.h"
35
36 #include "avcodec.h"
37 #include "blockdsp.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
45 126024 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
46 int16_t *block, int n, int qscale)
47 {
48 int i, level, nCoeffs;
49 const uint16_t *quant_matrix;
50
51 126024 nCoeffs= s->block_last_index[n];
52
53
2/2
✓ Branch 0 taken 84016 times.
✓ Branch 1 taken 42008 times.
126024 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
54 /* XXX: only MPEG-1 */
55 126024 quant_matrix = s->intra_matrix;
56
2/2
✓ Branch 0 taken 2913490 times.
✓ Branch 1 taken 126024 times.
3039514 for(i=1;i<=nCoeffs;i++) {
57 2913490 int j= s->intra_scantable.permutated[i];
58 2913490 level = block[j];
59
2/2
✓ Branch 0 taken 1214954 times.
✓ Branch 1 taken 1698536 times.
2913490 if (level) {
60
2/2
✓ Branch 0 taken 604308 times.
✓ Branch 1 taken 610646 times.
1214954 if (level < 0) {
61 604308 level = -level;
62 604308 level = (int)(level * qscale * quant_matrix[j]) >> 3;
63 604308 level = (level - 1) | 1;
64 604308 level = -level;
65 } else {
66 610646 level = (int)(level * qscale * quant_matrix[j]) >> 3;
67 610646 level = (level - 1) | 1;
68 }
69 1214954 block[j] = level;
70 }
71 }
72 126024 }
73
74 285599 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
75 int16_t *block, int n, int qscale)
76 {
77 int i, level, nCoeffs;
78 const uint16_t *quant_matrix;
79
80 285599 nCoeffs= s->block_last_index[n];
81
82 285599 quant_matrix = s->inter_matrix;
83
2/2
✓ Branch 0 taken 9213074 times.
✓ Branch 1 taken 285599 times.
9498673 for(i=0; i<=nCoeffs; i++) {
84 9213074 int j= s->intra_scantable.permutated[i];
85 9213074 level = block[j];
86
2/2
✓ Branch 0 taken 2100996 times.
✓ Branch 1 taken 7112078 times.
9213074 if (level) {
87
2/2
✓ Branch 0 taken 1051940 times.
✓ Branch 1 taken 1049056 times.
2100996 if (level < 0) {
88 1051940 level = -level;
89 1051940 level = (((level << 1) + 1) * qscale *
90 1051940 ((int) (quant_matrix[j]))) >> 4;
91 1051940 level = (level - 1) | 1;
92 1051940 level = -level;
93 } else {
94 1049056 level = (((level << 1) + 1) * qscale *
95 1049056 ((int) (quant_matrix[j]))) >> 4;
96 1049056 level = (level - 1) | 1;
97 }
98 2100996 block[j] = level;
99 }
100 }
101 285599 }
102
103 131268 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
104 int16_t *block, int n, int qscale)
105 {
106 int i, level, nCoeffs;
107 const uint16_t *quant_matrix;
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131268 times.
131268 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
110 131268 else qscale <<= 1;
111
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131268 times.
131268 if(s->alternate_scan) nCoeffs= 63;
113 131268 else nCoeffs= s->block_last_index[n];
114
115
2/2
✓ Branch 0 taken 87512 times.
✓ Branch 1 taken 43756 times.
131268 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
116 131268 quant_matrix = s->intra_matrix;
117
2/2
✓ Branch 0 taken 462435 times.
✓ Branch 1 taken 131268 times.
593703 for(i=1;i<=nCoeffs;i++) {
118 462435 int j= s->intra_scantable.permutated[i];
119 462435 level = block[j];
120
2/2
✓ Branch 0 taken 152199 times.
✓ Branch 1 taken 310236 times.
462435 if (level) {
121
2/2
✓ Branch 0 taken 74700 times.
✓ Branch 1 taken 77499 times.
152199 if (level < 0) {
122 74700 level = -level;
123 74700 level = (int)(level * qscale * quant_matrix[j]) >> 4;
124 74700 level = -level;
125 } else {
126 77499 level = (int)(level * qscale * quant_matrix[j]) >> 4;
127 }
128 152199 block[j] = level;
129 }
130 }
131 131268 }
132
133 1148348 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
134 int16_t *block, int n, int qscale)
135 {
136 int i, level, nCoeffs;
137 const uint16_t *quant_matrix;
138 1148348 int sum=-1;
139
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1148348 times.
1148348 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
141 1148348 else qscale <<= 1;
142
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1148348 times.
1148348 if(s->alternate_scan) nCoeffs= 63;
144 1148348 else nCoeffs= s->block_last_index[n];
145
146
2/2
✓ Branch 0 taken 713884 times.
✓ Branch 1 taken 434464 times.
1148348 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
147 1148348 sum += block[0];
148 1148348 quant_matrix = s->intra_matrix;
149
2/2
✓ Branch 0 taken 19138345 times.
✓ Branch 1 taken 1148348 times.
20286693 for(i=1;i<=nCoeffs;i++) {
150 19138345 int j= s->intra_scantable.permutated[i];
151 19138345 level = block[j];
152
2/2
✓ Branch 0 taken 9486353 times.
✓ Branch 1 taken 9651992 times.
19138345 if (level) {
153
2/2
✓ Branch 0 taken 4770795 times.
✓ Branch 1 taken 4715558 times.
9486353 if (level < 0) {
154 4770795 level = -level;
155 4770795 level = (int)(level * qscale * quant_matrix[j]) >> 4;
156 4770795 level = -level;
157 } else {
158 4715558 level = (int)(level * qscale * quant_matrix[j]) >> 4;
159 }
160 9486353 block[j] = level;
161 9486353 sum+=level;
162 }
163 }
164 1148348 block[63]^=sum&1;
165 1148348 }
166
167 2930396 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
168 int16_t *block, int n, int qscale)
169 {
170 int i, level, nCoeffs;
171 const uint16_t *quant_matrix;
172 2930396 int sum=-1;
173
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2930396 times.
2930396 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
175 2930396 else qscale <<= 1;
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2930396 times.
2930396 if(s->alternate_scan) nCoeffs= 63;
178 2930396 else nCoeffs= s->block_last_index[n];
179
180 2930396 quant_matrix = s->inter_matrix;
181
2/2
✓ Branch 0 taken 68906001 times.
✓ Branch 1 taken 2930396 times.
71836397 for(i=0; i<=nCoeffs; i++) {
182 68906001 int j= s->intra_scantable.permutated[i];
183 68906001 level = block[j];
184
2/2
✓ Branch 0 taken 25048732 times.
✓ Branch 1 taken 43857269 times.
68906001 if (level) {
185
2/2
✓ Branch 0 taken 12560887 times.
✓ Branch 1 taken 12487845 times.
25048732 if (level < 0) {
186 12560887 level = -level;
187 12560887 level = (((level << 1) + 1) * qscale *
188 12560887 ((int) (quant_matrix[j]))) >> 5;
189 12560887 level = -level;
190 } else {
191 12487845 level = (((level << 1) + 1) * qscale *
192 12487845 ((int) (quant_matrix[j]))) >> 5;
193 }
194 25048732 block[j] = level;
195 25048732 sum+=level;
196 }
197 }
198 2930396 block[63]^=sum&1;
199 2930396 }
200
201 3450882 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
202 int16_t *block, int n, int qscale)
203 {
204 int i, level, qmul, qadd;
205 int nCoeffs;
206
207 av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
208
209 3450882 qmul = qscale << 1;
210
211
2/2
✓ Branch 0 taken 3277686 times.
✓ Branch 1 taken 173196 times.
3450882 if (!s->h263_aic) {
212
2/2
✓ Branch 0 taken 2185124 times.
✓ Branch 1 taken 1092562 times.
3277686 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
213 3277686 qadd = (qscale - 1) | 1;
214 }else{
215 173196 qadd = 0;
216 }
217
2/2
✓ Branch 0 taken 68304 times.
✓ Branch 1 taken 3382578 times.
3450882 if(s->ac_pred)
218 68304 nCoeffs=63;
219 else
220 3382578 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
221
222
2/2
✓ Branch 0 taken 120374437 times.
✓ Branch 1 taken 3450882 times.
123825319 for(i=1; i<=nCoeffs; i++) {
223 120374437 level = block[i];
224
2/2
✓ Branch 0 taken 37212013 times.
✓ Branch 1 taken 83162424 times.
120374437 if (level) {
225
2/2
✓ Branch 0 taken 18632568 times.
✓ Branch 1 taken 18579445 times.
37212013 if (level < 0) {
226 18632568 level = level * qmul - qadd;
227 } else {
228 18579445 level = level * qmul + qadd;
229 }
230 37212013 block[i] = level;
231 }
232 }
233 3450882 }
234
235 6079793 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
236 int16_t *block, int n, int qscale)
237 {
238 int i, level, qmul, qadd;
239 int nCoeffs;
240
241 av_assert2(s->block_last_index[n]>=0);
242
243 6079793 qadd = (qscale - 1) | 1;
244 6079793 qmul = qscale << 1;
245
246 6079793 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
247
248
2/2
✓ Branch 0 taken 232240960 times.
✓ Branch 1 taken 6079793 times.
238320753 for(i=0; i<=nCoeffs; i++) {
249 232240960 level = block[i];
250
2/2
✓ Branch 0 taken 48201604 times.
✓ Branch 1 taken 184039356 times.
232240960 if (level) {
251
2/2
✓ Branch 0 taken 24019112 times.
✓ Branch 1 taken 24182492 times.
48201604 if (level < 0) {
252 24019112 level = level * qmul - qadd;
253 } else {
254 24182492 level = level * qmul + qadd;
255 }
256 48201604 block[i] = level;
257 }
258 }
259 6079793 }
260
261
262 static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
263 {
264 while(h--)
265 memset(dst + h*linesize, 128, 16);
266 }
267
268 static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
269 {
270 while(h--)
271 memset(dst + h*linesize, 128, 8);
272 }
273
274 /* init common dct for both encoder and decoder */
275 799 static av_cold int dct_init(MpegEncContext *s)
276 {
277 799 ff_blockdsp_init(&s->bdsp);
278 799 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
279 799 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
280
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799 times.
799 if (s->avctx->debug & FF_DEBUG_NOMC) {
282 int i;
283 for (i=0; i<4; i++) {
284 s->hdsp.avg_pixels_tab[0][i] = gray16;
285 s->hdsp.put_pixels_tab[0][i] = gray16;
286 s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
287
288 s->hdsp.avg_pixels_tab[1][i] = gray8;
289 s->hdsp.put_pixels_tab[1][i] = gray8;
290 s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
291 }
292 }
293
294 799 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
295 799 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
296 799 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
297 799 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
298 799 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
299
2/2
✓ Branch 0 taken 577 times.
✓ Branch 1 taken 222 times.
799 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
300 577 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
301 799 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
302
303 #if HAVE_INTRINSICS_NEON
304 ff_mpv_common_init_neon(s);
305 #endif
306
307 #if ARCH_ALPHA
308 ff_mpv_common_init_axp(s);
309 #elif ARCH_ARM
310 ff_mpv_common_init_arm(s);
311 #elif ARCH_PPC
312 ff_mpv_common_init_ppc(s);
313 #elif ARCH_X86
314 799 ff_mpv_common_init_x86(s);
315 #elif ARCH_MIPS
316 ff_mpv_common_init_mips(s);
317 #endif
318
319 799 return 0;
320 }
321
322 25154 av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st,
323 const uint8_t *src_scantable)
324 {
325 int end;
326
327 25154 st->scantable = src_scantable;
328
329
2/2
✓ Branch 0 taken 1609856 times.
✓ Branch 1 taken 25154 times.
1635010 for (int i = 0; i < 64; i++) {
330 1609856 int j = src_scantable[i];
331 1609856 st->permutated[i] = permutation[j];
332 }
333
334 25154 end = -1;
335
2/2
✓ Branch 0 taken 1609856 times.
✓ Branch 1 taken 25154 times.
1635010 for (int i = 0; i < 64; i++) {
336 1609856 int j = st->permutated[i];
337
2/2
✓ Branch 0 taken 341916 times.
✓ Branch 1 taken 1267940 times.
1609856 if (j > end)
338 341916 end = j;
339 1609856 st->raster_end[i] = end;
340 }
341 25154 }
342
343 905 av_cold void ff_mpv_idct_init(MpegEncContext *s)
344 {
345
2/2
✓ Branch 0 taken 244 times.
✓ Branch 1 taken 661 times.
905 if (s->codec_id == AV_CODEC_ID_MPEG4)
346 244 s->idsp.mpeg4_studio_profile = s->studio_profile;
347 905 ff_idctdsp_init(&s->idsp, s->avctx);
348
349 /* load & permutate scantables
350 * note: only wmv uses different ones
351 */
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 905 times.
905 if (s->alternate_scan) {
353 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
354 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
355 } else {
356 905 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
357 905 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
358 }
359 905 ff_permute_scantable(s->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
360 905 s->idsp.idct_permutation);
361 905 ff_permute_scantable(s->permutated_intra_v_scantable, ff_alternate_vertical_scan,
362 905 s->idsp.idct_permutation);
363 905 }
364
365 901 static int init_duplicate_context(MpegEncContext *s)
366 {
367 901 int y_size = s->b8_stride * (2 * s->mb_height + 1);
368 901 int c_size = s->mb_stride * (s->mb_height + 1);
369 901 int yc_size = y_size + 2 * c_size;
370 int i;
371
372
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 754 times.
901 if (s->mb_height & 1)
373 147 yc_size += 2*s->b8_stride + 2*s->mb_stride;
374
375
2/2
✓ Branch 0 taken 221 times.
✓ Branch 1 taken 680 times.
901 if (s->encoding) {
376 221 s->me.map = av_mallocz(2 * ME_MAP_SIZE * sizeof(*s->me.map));
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
221 if (!s->me.map)
378 return AVERROR(ENOMEM);
379 221 s->me.score_map = s->me.map + ME_MAP_SIZE;
380
381
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 214 times.
221 if (s->noise_reduction) {
382
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_error_sum, 2))
383 return AVERROR(ENOMEM);
384 }
385 }
386
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 901 times.
901 if (!FF_ALLOCZ_TYPED_ARRAY(s->blocks, 1 + s->encoding))
387 return AVERROR(ENOMEM);
388 901 s->block = s->blocks[0];
389
390
2/2
✓ Branch 0 taken 10812 times.
✓ Branch 1 taken 901 times.
11713 for (i = 0; i < 12; i++) {
391 10812 s->pblocks[i] = &s->block[i];
392 }
393
394
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 897 times.
901 if (s->avctx->codec_tag == AV_RL32("VCR2")) {
395 // exchange uv
396 4 FFSWAP(void *, s->pblocks[4], s->pblocks[5]);
397 }
398
399
2/2
✓ Branch 0 taken 471 times.
✓ Branch 1 taken 430 times.
901 if (s->out_format == FMT_H263) {
400 /* ac values */
401
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 471 times.
471 if (!FF_ALLOCZ_TYPED_ARRAY(s->ac_val_base, yc_size))
402 return AVERROR(ENOMEM);
403 471 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
404 471 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
405 471 s->ac_val[2] = s->ac_val[1] + c_size;
406 }
407
408 901 return 0;
409 }
410
411 841 int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
412 {
413 841 int nb_slices = s->slice_context_count, ret;
414
415 /* We initialize the copies before the original so that
416 * fields allocated in init_duplicate_context are NULL after
417 * copying. This prevents double-frees upon allocation error. */
418
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 841 times.
901 for (int i = 1; i < nb_slices; i++) {
419 60 s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (!s->thread_context[i])
421 return AVERROR(ENOMEM);
422
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
60 if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
423 return ret;
424 60 s->thread_context[i]->start_mb_y =
425 60 (s->mb_height * (i ) + nb_slices / 2) / nb_slices;
426 60 s->thread_context[i]->end_mb_y =
427 60 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
428 }
429 841 s->start_mb_y = 0;
430 859 s->end_mb_y = nb_slices > 1 ? (s->mb_height + nb_slices / 2) / nb_slices
431
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 823 times.
841 : s->mb_height;
432 841 return init_duplicate_context(s);
433 }
434
435 935 static void free_duplicate_context(MpegEncContext *s)
436 {
437
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 935 times.
935 if (!s)
438 return;
439
440 935 av_freep(&s->sc.edge_emu_buffer);
441 935 av_freep(&s->me.scratchpad);
442 935 s->me.temp =
443 935 s->sc.rd_scratchpad =
444 935 s->sc.b_scratchpad =
445 935 s->sc.obmc_scratchpad = NULL;
446
447 935 av_freep(&s->dct_error_sum);
448 935 av_freep(&s->me.map);
449 935 s->me.score_map = NULL;
450 935 av_freep(&s->blocks);
451 935 av_freep(&s->ac_val_base);
452 935 s->block = NULL;
453 }
454
455 875 static void free_duplicate_contexts(MpegEncContext *s)
456 {
457
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 875 times.
935 for (int i = 1; i < s->slice_context_count; i++) {
458 60 free_duplicate_context(s->thread_context[i]);
459 60 av_freep(&s->thread_context[i]);
460 }
461 875 free_duplicate_context(s);
462 875 }
463
464 15232 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
465 {
466 #define COPY(a) bak->a = src->a
467 15232 COPY(sc.edge_emu_buffer);
468 15232 COPY(me.scratchpad);
469 15232 COPY(me.temp);
470 15232 COPY(sc.rd_scratchpad);
471 15232 COPY(sc.b_scratchpad);
472 15232 COPY(sc.obmc_scratchpad);
473 15232 COPY(me.map);
474 15232 COPY(me.score_map);
475 15232 COPY(blocks);
476 15232 COPY(block);
477 15232 COPY(start_mb_y);
478 15232 COPY(end_mb_y);
479 15232 COPY(me.map_generation);
480 15232 COPY(pb);
481 15232 COPY(dct_error_sum);
482 15232 COPY(dct_count[0]);
483 15232 COPY(dct_count[1]);
484 15232 COPY(ac_val_base);
485 15232 COPY(ac_val[0]);
486 15232 COPY(ac_val[1]);
487 15232 COPY(ac_val[2]);
488 #undef COPY
489 15232 }
490
491 7616 int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
492 {
493 MpegEncContext bak;
494 int i, ret;
495 // FIXME copy only needed parts
496 7616 backup_duplicate_context(&bak, dst);
497 7616 memcpy(dst, src, sizeof(MpegEncContext));
498 7616 backup_duplicate_context(dst, &bak);
499
2/2
✓ Branch 0 taken 91392 times.
✓ Branch 1 taken 7616 times.
99008 for (i = 0; i < 12; i++) {
500 91392 dst->pblocks[i] = &dst->block[i];
501 }
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7616 times.
7616 if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
503 // exchange uv
504 FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
505 }
506
3/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 7556 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
7676 if (!dst->sc.edge_emu_buffer &&
507 60 (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
508 60 &dst->sc, dst->linesize)) < 0) {
509 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
510 "scratch buffers.\n");
511 return ret;
512 }
513 7616 return 0;
514 }
515
516 /**
517 * Set the given MpegEncContext to common defaults
518 * (same for encoding and decoding).
519 * The changed fields will not depend upon the
520 * prior state of the MpegEncContext.
521 */
522 827 void ff_mpv_common_defaults(MpegEncContext *s)
523 {
524 827 s->y_dc_scale_table =
525 827 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
526 827 s->chroma_qscale_table = ff_default_chroma_qscale_table;
527 827 s->progressive_frame = 1;
528 827 s->progressive_sequence = 1;
529 827 s->picture_structure = PICT_FRAME;
530
531 827 s->coded_picture_number = 0;
532 827 s->picture_number = 0;
533
534 827 s->f_code = 1;
535 827 s->b_code = 1;
536
537 827 s->slice_context_count = 1;
538 827 }
539
540 841 int ff_mpv_init_context_frame(MpegEncContext *s)
541 {
542 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
543
544
4/4
✓ Branch 0 taken 281 times.
✓ Branch 1 taken 560 times.
✓ Branch 2 taken 147 times.
✓ Branch 3 taken 134 times.
841 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
545 147 s->mb_height = (s->height + 31) / 32 * 2;
546 else
547 694 s->mb_height = (s->height + 15) / 16;
548
549 841 s->mb_width = (s->width + 15) / 16;
550 841 s->mb_stride = s->mb_width + 1;
551 841 s->b8_stride = s->mb_width * 2 + 1;
552 841 mb_array_size = s->mb_height * s->mb_stride;
553 841 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
554
555 /* set default edge pos, will be overridden
556 * in decode_header if needed */
557 841 s->h_edge_pos = s->mb_width * 16;
558 841 s->v_edge_pos = s->mb_height * 16;
559
560 841 s->mb_num = s->mb_width * s->mb_height;
561
562 841 s->block_wrap[0] =
563 841 s->block_wrap[1] =
564 841 s->block_wrap[2] =
565 841 s->block_wrap[3] = s->b8_stride;
566 841 s->block_wrap[4] =
567 841 s->block_wrap[5] = s->mb_stride;
568
569 841 y_size = s->b8_stride * (2 * s->mb_height + 1);
570 841 c_size = s->mb_stride * (s->mb_height + 1);
571 841 yc_size = y_size + 2 * c_size;
572
573
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 695 times.
841 if (s->mb_height & 1)
574 146 yc_size += 2*s->b8_stride + 2*s->mb_stride;
575
576
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 841 times.
841 if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_index2xy, s->mb_num + 1))
577 return AVERROR(ENOMEM);
578
2/2
✓ Branch 0 taken 16842 times.
✓ Branch 1 taken 841 times.
17683 for (y = 0; y < s->mb_height; y++)
579
2/2
✓ Branch 0 taken 548040 times.
✓ Branch 1 taken 16842 times.
564882 for (x = 0; x < s->mb_width; x++)
580 548040 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
581
582 841 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
583
584
2/2
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 267 times.
841 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
585
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 558 times.
574 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
586 /* interlaced direct mode decoding tables */
587 283 int16_t (*tmp)[2] = av_calloc(mv_table_size, 4 * sizeof(*tmp));
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 283 times.
283 if (!tmp)
589 return AVERROR(ENOMEM);
590 283 s->p_field_mv_table_base = tmp;
591 283 tmp += s->mb_stride + 1;
592
2/2
✓ Branch 0 taken 566 times.
✓ Branch 1 taken 283 times.
849 for (int i = 0; i < 2; i++) {
593
2/2
✓ Branch 0 taken 1132 times.
✓ Branch 1 taken 566 times.
1698 for (int j = 0; j < 2; j++) {
594 1132 s->p_field_mv_table[i][j] = tmp;
595 1132 tmp += mv_table_size;
596 }
597 }
598 }
599
600
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 374 times.
841 if (s->out_format == FMT_H263) {
601 /* cbp values, cbp, ac_pred, pred_dir */
602
1/2
✓ Branch 1 taken 467 times.
✗ Branch 2 not taken.
467 if (!(s->coded_block_base = av_mallocz(y_size + (s->mb_height&1)*2*s->b8_stride)) ||
603
1/2
✓ Branch 1 taken 467 times.
✗ Branch 2 not taken.
467 !(s->cbp_table = av_mallocz(mb_array_size)) ||
604
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 467 times.
467 !(s->pred_dir_table = av_mallocz(mb_array_size)))
605 return AVERROR(ENOMEM);
606 467 s->coded_block = s->coded_block_base + s->b8_stride + 1;
607 }
608
609
6/6
✓ Branch 0 taken 478 times.
✓ Branch 1 taken 363 times.
✓ Branch 2 taken 465 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 347 times.
✓ Branch 5 taken 118 times.
841 if (s->h263_pred || s->h263_plus || !s->encoding) {
610 /* dc values */
611 // MN: we need these for error resilience of intra-frames
612
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 723 times.
723 if (!FF_ALLOCZ_TYPED_ARRAY(s->dc_val_base, yc_size))
613 return AVERROR(ENOMEM);
614 723 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
615 723 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
616 723 s->dc_val[2] = s->dc_val[1] + c_size;
617
2/2
✓ Branch 0 taken 3150113 times.
✓ Branch 1 taken 723 times.
3150836 for (i = 0; i < yc_size; i++)
618 3150113 s->dc_val_base[i] = 1024;
619 }
620
621 // Note the + 1 is for a quicker MPEG-4 slice_end detection
622
1/2
✓ Branch 1 taken 841 times.
✗ Branch 2 not taken.
841 if (!(s->mbskip_table = av_mallocz(mb_array_size + 2)) ||
623 /* which mb is an intra block, init macroblock skip table */
624
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 841 times.
841 !(s->mbintra_table = av_malloc(mb_array_size)))
625 return AVERROR(ENOMEM);
626 841 memset(s->mbintra_table, 1, mb_array_size);
627
628
2/2
✓ Branch 0 taken 640 times.
✓ Branch 1 taken 201 times.
841 return !CONFIG_MPEGVIDEODEC || s->encoding ? 0 : ff_mpeg_er_init(s);
629 }
630
631 799 static void clear_context(MpegEncContext *s)
632 {
633 799 memset(&s->next_picture, 0, sizeof(s->next_picture));
634 799 memset(&s->last_picture, 0, sizeof(s->last_picture));
635 799 memset(&s->current_picture, 0, sizeof(s->current_picture));
636
637 799 memset(s->thread_context, 0, sizeof(s->thread_context));
638
639 799 s->me.map = NULL;
640 799 s->me.score_map = NULL;
641 799 s->dct_error_sum = NULL;
642 799 s->block = NULL;
643 799 s->blocks = NULL;
644 799 memset(s->pblocks, 0, sizeof(s->pblocks));
645 799 s->ac_val_base = NULL;
646 799 s->ac_val[0] =
647 799 s->ac_val[1] =
648 799 s->ac_val[2] =NULL;
649 799 s->sc.edge_emu_buffer = NULL;
650 799 s->me.scratchpad = NULL;
651 799 s->me.temp =
652 799 s->sc.rd_scratchpad =
653 799 s->sc.b_scratchpad =
654 799 s->sc.obmc_scratchpad = NULL;
655
656
657 799 s->bitstream_buffer = NULL;
658 799 s->allocated_bitstream_buffer_size = 0;
659 799 s->picture = NULL;
660 799 s->p_field_mv_table_base = NULL;
661
2/2
✓ Branch 0 taken 1598 times.
✓ Branch 1 taken 799 times.
2397 for (int i = 0; i < 2; i++)
662
2/2
✓ Branch 0 taken 3196 times.
✓ Branch 1 taken 1598 times.
4794 for (int j = 0; j < 2; j++)
663 3196 s->p_field_mv_table[i][j] = NULL;
664
665 799 s->dc_val_base = NULL;
666 799 s->coded_block_base = NULL;
667 799 s->mbintra_table = NULL;
668 799 s->cbp_table = NULL;
669 799 s->pred_dir_table = NULL;
670
671 799 s->mbskip_table = NULL;
672
673 799 s->er.error_status_table = NULL;
674 799 s->er.er_temp_buffer = NULL;
675 799 s->mb_index2xy = NULL;
676 799 }
677
678 /**
679 * init common structure for both encoder and decoder.
680 * this assumes that some variables like width/height are already set
681 */
682 799 av_cold int ff_mpv_common_init(MpegEncContext *s)
683 {
684 int i, ret;
685 799 int nb_slices = (HAVE_THREADS &&
686 799 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
687
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 781 times.
799 s->avctx->thread_count : 1;
688
689 799 clear_context(s);
690
691
4/4
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 598 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 189 times.
799 if (s->encoding && s->avctx->slices)
692 12 nb_slices = s->avctx->slices;
693
694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799 times.
799 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
695 av_log(s->avctx, AV_LOG_ERROR,
696 "decoding to AV_PIX_FMT_NONE is not supported.\n");
697 return AVERROR(EINVAL);
698 }
699
700
4/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 783 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 783 times.
1582 if ((s->width || s->height) &&
701 783 av_image_check_size(s->width, s->height, 0, s->avctx))
702 return AVERROR(EINVAL);
703
704 799 dct_init(s);
705
706 /* set chroma shifts */
707 799 ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
708 &s->chroma_x_shift,
709 &s->chroma_y_shift);
710
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799 times.
799 if (ret)
711 return ret;
712
713
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 799 times.
799 if (!FF_ALLOCZ_TYPED_ARRAY(s->picture, MAX_PICTURE_COUNT))
714 return AVERROR(ENOMEM);
715
2/2
✓ Branch 0 taken 28764 times.
✓ Branch 1 taken 799 times.
29563 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
716 28764 s->picture[i].f = av_frame_alloc();
717
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28764 times.
28764 if (!s->picture[i].f)
718 goto fail_nomem;
719 }
720
721
1/2
✓ Branch 1 taken 799 times.
✗ Branch 2 not taken.
799 if (!(s->next_picture.f = av_frame_alloc()) ||
722
1/2
✓ Branch 1 taken 799 times.
✗ Branch 2 not taken.
799 !(s->last_picture.f = av_frame_alloc()) ||
723
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 799 times.
799 !(s->current_picture.f = av_frame_alloc()))
724 goto fail_nomem;
725
726
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 799 times.
799 if ((ret = ff_mpv_init_context_frame(s)))
727 goto fail;
728
729
4/6
✓ Branch 0 taken 799 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 783 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 16 times.
799 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
730 int max_slices;
731 if (s->mb_height)
732 max_slices = FFMIN(MAX_THREADS, s->mb_height);
733 else
734 max_slices = MAX_THREADS;
735 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
736 " reducing to %d\n", nb_slices, max_slices);
737 nb_slices = max_slices;
738 }
739
740 799 s->context_initialized = 1;
741 799 memset(s->thread_context, 0, sizeof(s->thread_context));
742 799 s->thread_context[0] = s;
743 799 s->slice_context_count = nb_slices;
744
745 // if (s->width && s->height) {
746 799 ret = ff_mpv_init_duplicate_contexts(s);
747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 799 times.
799 if (ret < 0)
748 goto fail;
749 // }
750
751 799 return 0;
752 fail_nomem:
753 ret = AVERROR(ENOMEM);
754 fail:
755 ff_mpv_common_end(s);
756 return ret;
757 }
758
759 875 void ff_mpv_free_context_frame(MpegEncContext *s)
760 {
761 875 free_duplicate_contexts(s);
762
763 875 av_freep(&s->p_field_mv_table_base);
764
2/2
✓ Branch 0 taken 1750 times.
✓ Branch 1 taken 875 times.
2625 for (int i = 0; i < 2; i++)
765
2/2
✓ Branch 0 taken 3500 times.
✓ Branch 1 taken 1750 times.
5250 for (int j = 0; j < 2; j++)
766 3500 s->p_field_mv_table[i][j] = NULL;
767
768 875 av_freep(&s->dc_val_base);
769 875 av_freep(&s->coded_block_base);
770 875 av_freep(&s->mbintra_table);
771 875 av_freep(&s->cbp_table);
772 875 av_freep(&s->pred_dir_table);
773
774 875 av_freep(&s->mbskip_table);
775
776 875 av_freep(&s->er.error_status_table);
777 875 av_freep(&s->er.er_temp_buffer);
778 875 av_freep(&s->mb_index2xy);
779
780 875 s->linesize = s->uvlinesize = 0;
781 875 }
782
783 833 void ff_mpv_common_end(MpegEncContext *s)
784 {
785 833 ff_mpv_free_context_frame(s);
786
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 815 times.
833 if (s->slice_context_count > 1)
787 18 s->slice_context_count = 1;
788
789 833 av_freep(&s->bitstream_buffer);
790 833 s->allocated_bitstream_buffer_size = 0;
791
792
2/2
✓ Branch 0 taken 799 times.
✓ Branch 1 taken 34 times.
833 if (s->picture) {
793
2/2
✓ Branch 0 taken 28764 times.
✓ Branch 1 taken 799 times.
29563 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
794 28764 ff_mpv_picture_free(&s->picture[i]);
795 }
796 833 av_freep(&s->picture);
797 833 ff_mpv_picture_free(&s->last_picture);
798 833 ff_mpv_picture_free(&s->current_picture);
799 833 ff_mpv_picture_free(&s->next_picture);
800
801 833 s->context_initialized = 0;
802 833 s->context_reinit = 0;
803 833 s->last_picture_ptr =
804 833 s->next_picture_ptr =
805 833 s->current_picture_ptr = NULL;
806 833 s->linesize = s->uvlinesize = 0;
807 833 }
808
809
810 /**
811 * Clean dc, ac, coded_block for the current non-intra MB.
812 */
813 452262 void ff_clean_intra_table_entries(MpegEncContext *s)
814 {
815 452262 int wrap = s->b8_stride;
816 452262 int xy = s->block_index[0];
817
818 452262 s->dc_val[0][xy ] =
819 452262 s->dc_val[0][xy + 1 ] =
820 452262 s->dc_val[0][xy + wrap] =
821 452262 s->dc_val[0][xy + 1 + wrap] = 1024;
822 /* ac pred */
823 452262 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
824 452262 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
825
2/2
✓ Branch 0 taken 45921 times.
✓ Branch 1 taken 406341 times.
452262 if (s->msmpeg4_version>=3) {
826 45921 s->coded_block[xy ] =
827 45921 s->coded_block[xy + 1 ] =
828 45921 s->coded_block[xy + wrap] =
829 45921 s->coded_block[xy + 1 + wrap] = 0;
830 }
831 /* chroma */
832 452262 wrap = s->mb_stride;
833 452262 xy = s->mb_x + s->mb_y * wrap;
834 452262 s->dc_val[1][xy] =
835 452262 s->dc_val[2][xy] = 1024;
836 /* ac pred */
837 452262 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
838 452262 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
839
840 452262 s->mbintra_table[xy]= 0;
841 452262 }
842
843 896500 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
844 896500 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
845 896500 const int uvlinesize = s->current_picture.f->linesize[1];
846
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 896470 times.
896500 const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
847 896500 const int height_of_mb = 4 - s->avctx->lowres;
848
849 896500 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
850 896500 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
851 896500 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
852 896500 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
853 896500 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
854 896500 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;
855 //block_index is not used by mpeg2, so it is not affected by chroma_format
856
857 896500 s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) << width_of_mb);
858 896500 s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
859 896500 s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
860
861
2/2
✓ Branch 0 taken 857186 times.
✓ Branch 1 taken 39314 times.
896500 if (s->picture_structure == PICT_FRAME) {
862 857186 s->dest[0] += s->mb_y * linesize << height_of_mb;
863 857186 s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
864 857186 s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
865 } else {
866 39314 s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
867 39314 s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
868 39314 s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
869 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
870 }
871 896500 }
872
873 /**
874 * set qscale and update qscale dependent variables.
875 */
876 1469781 void ff_set_qscale(MpegEncContext * s, int qscale)
877 {
878
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1469780 times.
1469781 if (qscale < 1)
879 1 qscale = 1;
880
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1469751 times.
1469780 else if (qscale > 31)
881 29 qscale = 31;
882
883 1469781 s->qscale = qscale;
884 1469781 s->chroma_qscale= s->chroma_qscale_table[qscale];
885
886 1469781 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
887 1469781 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
888 1469781 }
889