FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 463 509 91.0%
Functions: 26 28 92.9%
Branches: 179 222 80.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 #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 "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 131268 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 131268 times.
131268 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
111 131268 else qscale <<= 1;
112
113 131268 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 1148348 nCoeffs= s->block_last_index[n];
144
145
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;
146 1148348 sum += block[0];
147 1148348 quant_matrix = s->intra_matrix;
148
2/2
✓ Branch 0 taken 19138354 times.
✓ Branch 1 taken 1148348 times.
20286702 for(i=1;i<=nCoeffs;i++) {
149 19138354 int j= s->intra_scantable.permutated[i];
150 19138354 level = block[j];
151
2/2
✓ Branch 0 taken 9486356 times.
✓ Branch 1 taken 9651998 times.
19138354 if (level) {
152
2/2
✓ Branch 0 taken 4770795 times.
✓ Branch 1 taken 4715561 times.
9486356 if (level < 0) {
153 4770795 level = -level;
154 4770795 level = (int)(level * qscale * quant_matrix[j]) >> 4;
155 4770795 level = -level;
156 } else {
157 4715561 level = (int)(level * qscale * quant_matrix[j]) >> 4;
158 }
159 9486356 block[j] = level;
160 9486356 sum+=level;
161 }
162 }
163 1148348 block[63]^=sum&1;
164 1148348 }
165
166 2930397 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 2930397 int sum=-1;
172
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2930397 times.
2930397 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
174 2930397 else qscale <<= 1;
175
176 2930397 nCoeffs= s->block_last_index[n];
177
178 2930397 quant_matrix = s->inter_matrix;
179
2/2
✓ Branch 0 taken 68906021 times.
✓ Branch 1 taken 2930397 times.
71836418 for(i=0; i<=nCoeffs; i++) {
180 68906021 int j= s->intra_scantable.permutated[i];
181 68906021 level = block[j];
182
2/2
✓ Branch 0 taken 25048735 times.
✓ Branch 1 taken 43857286 times.
68906021 if (level) {
183
2/2
✓ Branch 0 taken 12560886 times.
✓ Branch 1 taken 12487849 times.
25048735 if (level < 0) {
184 12560886 level = -level;
185 12560886 level = (((level << 1) + 1) * qscale *
186 12560886 ((int) (quant_matrix[j]))) >> 5;
187 12560886 level = -level;
188 } else {
189 12487849 level = (((level << 1) + 1) * qscale *
190 12487849 ((int) (quant_matrix[j]))) >> 5;
191 }
192 25048735 block[j] = level;
193 25048735 sum+=level;
194 }
195 }
196 2930397 block[63]^=sum&1;
197 2930397 }
198
199 3385212 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 3385212 qmul = qscale << 1;
208
209
2/2
✓ Branch 0 taken 3212016 times.
✓ Branch 1 taken 173196 times.
3385212 if (!s->h263_aic) {
210
2/2
✓ Branch 0 taken 2141344 times.
✓ Branch 1 taken 1070672 times.
3212016 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
211 3212016 qadd = (qscale - 1) | 1;
212 }else{
213 173196 qadd = 0;
214 }
215
2/2
✓ Branch 0 taken 68304 times.
✓ Branch 1 taken 3316908 times.
3385212 if(s->ac_pred)
216 68304 nCoeffs=63;
217 else
218 3316908 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
219
220
2/2
✓ Branch 0 taken 118630384 times.
✓ Branch 1 taken 3385212 times.
122015596 for(i=1; i<=nCoeffs; i++) {
221 118630384 level = block[i];
222
2/2
✓ Branch 0 taken 36761055 times.
✓ Branch 1 taken 81869329 times.
118630384 if (level) {
223
2/2
✓ Branch 0 taken 18405092 times.
✓ Branch 1 taken 18355963 times.
36761055 if (level < 0) {
224 18405092 level = level * qmul - qadd;
225 } else {
226 18355963 level = level * qmul + qadd;
227 }
228 36761055 block[i] = level;
229 }
230 }
231 3385212 }
232
233 5893484 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 5893484 qadd = (qscale - 1) | 1;
242 5893484 qmul = qscale << 1;
243
244 5893484 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
245
246
2/2
✓ Branch 0 taken 225321181 times.
✓ Branch 1 taken 5893484 times.
231214665 for(i=0; i<=nCoeffs; i++) {
247 225321181 level = block[i];
248
2/2
✓ Branch 0 taken 47423983 times.
✓ Branch 1 taken 177897198 times.
225321181 if (level) {
249
2/2
✓ Branch 0 taken 23631556 times.
✓ Branch 1 taken 23792427 times.
47423983 if (level < 0) {
250 23631556 level = level * qmul - qadd;
251 } else {
252 23792427 level = level * qmul + qadd;
253 }
254 47423983 block[i] = level;
255 }
256 }
257 5893484 }
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 13641 av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st,
294 const uint8_t *src_scantable)
295 {
296 13641 st->scantable = src_scantable;
297
298
2/2
✓ Branch 0 taken 873024 times.
✓ Branch 1 taken 13641 times.
886665 for (int i = 0, end = -1; i < 64; i++) {
299 873024 int j = src_scantable[i];
300 873024 st->permutated[i] = permutation[j];
301
2/2
✓ Branch 0 taken 190787 times.
✓ Branch 1 taken 682237 times.
873024 if (permutation[j] > end)
302 190787 end = permutation[j];
303 873024 st->raster_end[i] = end;
304 }
305 13641 }
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 918 times.
918 if (s->alternate_scan) {
317 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
318 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
319 } else {
320 918 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
321 918 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 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 ff_refstruct_pool_uninit(&pools->mbskip_table_pool);
503 927 ff_refstruct_pool_uninit(&pools->qscale_table_pool);
504 927 ff_refstruct_pool_uninit(&pools->mb_type_pool);
505 927 ff_refstruct_pool_uninit(&pools->motion_val_pool);
506 927 ff_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 147 times.
✓ Branch 3 taken 135 times.
855 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
517 147 s->mb_height = (s->height + 31) / 32 * 2;
518 else
519 708 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 17094 times.
✓ Branch 1 taken 855 times.
17949 for (y = 0; y < s->mb_height; y++)
554
2/2
✓ Branch 0 taken 553584 times.
✓ Branch 1 taken 17094 times.
570678 for (x = 0; x < s->mb_width; x++)
555 553584 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 = ff_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 ? FF_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 3174589 times.
✓ Branch 1 taken 737 times.
3175326 for (i = 0; i < yc_size; i++)
607 3174589 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, FF_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->bitstream_buffer = NULL;
664 808 s->allocated_bitstream_buffer_size = 0;
665 808 s->p_field_mv_table_base = NULL;
666
2/2
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 808 times.
2424 for (int i = 0; i < 2; i++)
667
2/2
✓ Branch 0 taken 3232 times.
✓ Branch 1 taken 1616 times.
4848 for (int j = 0; j < 2; j++)
668 3232 s->p_field_mv_table[i][j] = NULL;
669
670 808 s->dc_val_base = NULL;
671 808 s->coded_block_base = NULL;
672 808 s->mbintra_table = NULL;
673 808 s->cbp_table = NULL;
674 808 s->pred_dir_table = NULL;
675
676 808 s->mbskip_table = NULL;
677
678 808 s->er.error_status_table = NULL;
679 808 s->er.er_temp_buffer = NULL;
680 808 s->mb_index2xy = NULL;
681 808 }
682
683 /**
684 * init common structure for both encoder and decoder.
685 * this assumes that some variables like width/height are already set
686 */
687 808 av_cold int ff_mpv_common_init(MpegEncContext *s)
688 {
689 808 int nb_slices = (HAVE_THREADS &&
690 808 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
691
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 790 times.
808 s->avctx->thread_count : 1;
692 int ret;
693
694 808 clear_context(s);
695
696
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)
697 12 nb_slices = s->avctx->slices;
698
699
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 808 times.
808 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
700 av_log(s->avctx, AV_LOG_ERROR,
701 "decoding to AV_PIX_FMT_NONE is not supported.\n");
702 return AVERROR(EINVAL);
703 }
704
705
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) &&
706 792 av_image_check_size(s->width, s->height, 0, s->avctx))
707 return AVERROR(EINVAL);
708
709 808 dsp_init(s);
710
711 /* set chroma shifts */
712 808 ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
713 &s->chroma_x_shift,
714 &s->chroma_y_shift);
715
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 808 times.
808 if (ret)
716 return ret;
717
718
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 808 times.
808 if ((ret = ff_mpv_init_context_frame(s)))
719 goto fail;
720
721
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)) {
722 int max_slices;
723 if (s->mb_height)
724 max_slices = FFMIN(MAX_THREADS, s->mb_height);
725 else
726 max_slices = MAX_THREADS;
727 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
728 " reducing to %d\n", nb_slices, max_slices);
729 nb_slices = max_slices;
730 }
731
732 808 s->context_initialized = 1;
733 808 memset(s->thread_context, 0, sizeof(s->thread_context));
734 808 s->thread_context[0] = s;
735 808 s->slice_context_count = nb_slices;
736
737 // if (s->width && s->height) {
738 808 ret = ff_mpv_init_duplicate_contexts(s);
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 808 times.
808 if (ret < 0)
740 goto fail;
741 // }
742
743 808 return 0;
744 fail:
745 ff_mpv_common_end(s);
746 return ret;
747 }
748
749 927 void ff_mpv_free_context_frame(MpegEncContext *s)
750 {
751 927 free_duplicate_contexts(s);
752
753 927 free_buffer_pools(&s->buffer_pools);
754 927 av_freep(&s->p_field_mv_table_base);
755
2/2
✓ Branch 0 taken 1854 times.
✓ Branch 1 taken 927 times.
2781 for (int i = 0; i < 2; i++)
756
2/2
✓ Branch 0 taken 3708 times.
✓ Branch 1 taken 1854 times.
5562 for (int j = 0; j < 2; j++)
757 3708 s->p_field_mv_table[i][j] = NULL;
758
759 927 av_freep(&s->dc_val_base);
760 927 av_freep(&s->coded_block_base);
761 927 av_freep(&s->mbintra_table);
762 927 av_freep(&s->cbp_table);
763 927 av_freep(&s->pred_dir_table);
764
765 927 av_freep(&s->mbskip_table);
766
767 927 av_freep(&s->er.error_status_table);
768 927 av_freep(&s->er.er_temp_buffer);
769 927 av_freep(&s->mb_index2xy);
770
771 927 s->linesize = s->uvlinesize = 0;
772 927 }
773
774 880 void ff_mpv_common_end(MpegEncContext *s)
775 {
776 880 ff_mpv_free_context_frame(s);
777
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 862 times.
880 if (s->slice_context_count > 1)
778 18 s->slice_context_count = 1;
779
780 880 av_freep(&s->bitstream_buffer);
781 880 s->allocated_bitstream_buffer_size = 0;
782
783 880 ff_mpv_unref_picture(&s->last_pic);
784 880 ff_mpv_unref_picture(&s->cur_pic);
785 880 ff_mpv_unref_picture(&s->next_pic);
786
787 880 s->context_initialized = 0;
788 880 s->context_reinit = 0;
789 880 s->linesize = s->uvlinesize = 0;
790 880 }
791
792
793 /**
794 * Clean dc, ac for the current non-intra MB.
795 */
796 456474 void ff_clean_intra_table_entries(MpegEncContext *s)
797 {
798 456474 int wrap = s->b8_stride;
799 456474 int xy = s->block_index[0];
800
801 456474 s->dc_val[0][xy ] =
802 456474 s->dc_val[0][xy + 1 ] =
803 456474 s->dc_val[0][xy + wrap] =
804 456474 s->dc_val[0][xy + 1 + wrap] = 1024;
805 /* ac pred */
806 456474 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
807 456474 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
808 /* chroma */
809 456474 wrap = s->mb_stride;
810 456474 xy = s->mb_x + s->mb_y * wrap;
811 456474 s->dc_val[1][xy] =
812 456474 s->dc_val[2][xy] = 1024;
813 /* ac pred */
814 456474 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
815 456474 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
816
817 456474 s->mbintra_table[xy]= 0;
818 456474 }
819
820 841191 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
821 841191 const int linesize = s->cur_pic.linesize[0]; //not s->linesize as this would be wrong for field pics
822 841191 const int uvlinesize = s->cur_pic.linesize[1];
823
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 841161 times.
841191 const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
824 841191 const int height_of_mb = 4 - s->avctx->lowres;
825
826 841191 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
827 841191 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
828 841191 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
829 841191 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
830 841191 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
831 841191 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;
832 //block_index is not used by mpeg2, so it is not affected by chroma_format
833
834 841191 s->dest[0] = s->cur_pic.data[0] + (int)((s->mb_x - 1U) << width_of_mb);
835 841191 s->dest[1] = s->cur_pic.data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
836 841191 s->dest[2] = s->cur_pic.data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
837
838
2/2
✓ Branch 0 taken 800897 times.
✓ Branch 1 taken 40294 times.
841191 if (s->picture_structure == PICT_FRAME) {
839 800897 s->dest[0] += s->mb_y * linesize << height_of_mb;
840 800897 s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
841 800897 s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
842 } else {
843 40294 s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
844 40294 s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
845 40294 s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
846 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
847 }
848 841191 }
849
850 /**
851 * set qscale and update qscale dependent variables.
852 */
853 1470666 void ff_set_qscale(MpegEncContext * s, int qscale)
854 {
855
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1470665 times.
1470666 if (qscale < 1)
856 1 qscale = 1;
857
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1470636 times.
1470665 else if (qscale > 31)
858 29 qscale = 31;
859
860 1470666 s->qscale = qscale;
861 1470666 s->chroma_qscale= s->chroma_qscale_table[qscale];
862
863 1470666 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
864 1470666 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
865 1470666 }
866