FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 461 505 91.3%
Functions: 26 28 92.9%
Branches: 180 222 81.1%

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