FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 310 348 89.1%
Functions: 19 21 90.5%
Branches: 119 154 77.3%

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 "mpegvideo_unquantize.h"
45 #include "libavutil/refstruct.h"
46
47
48 static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
49 {
50 while(h--)
51 memset(dst + h*linesize, 128, 16);
52 }
53
54 static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
55 {
56 while(h--)
57 memset(dst + h*linesize, 128, 8);
58 }
59
60 /* init common dct for both encoder and decoder */
61 808 static av_cold void dsp_init(MpegEncContext *s)
62 {
63 808 ff_blockdsp_init(&s->bdsp);
64 808 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
65 808 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
66
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 808 times.
808 if (s->avctx->debug & FF_DEBUG_NOMC) {
68 int i;
69 for (i=0; i<4; i++) {
70 s->hdsp.avg_pixels_tab[0][i] = gray16;
71 s->hdsp.put_pixels_tab[0][i] = gray16;
72 s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
73
74 s->hdsp.avg_pixels_tab[1][i] = gray8;
75 s->hdsp.put_pixels_tab[1][i] = gray8;
76 s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
77 }
78 }
79 808 }
80
81 5473 av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st,
82 const uint8_t *src_scantable)
83 {
84 5473 st->scantable = src_scantable;
85
86
2/2
✓ Branch 0 taken 350272 times.
✓ Branch 1 taken 5473 times.
355745 for (int i = 0, end = -1; i < 64; i++) {
87 350272 int j = src_scantable[i];
88 350272 st->permutated[i] = permutation[j];
89
2/2
✓ Branch 0 taken 103171 times.
✓ Branch 1 taken 247101 times.
350272 if (permutation[j] > end)
90 103171 end = permutation[j];
91 350272 st->raster_end[i] = end;
92 }
93 5473 }
94
95 918 av_cold void ff_mpv_idct_init(MpegEncContext *s)
96 {
97
2/2
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 663 times.
918 if (s->codec_id == AV_CODEC_ID_MPEG4)
98 255 s->idsp.mpeg4_studio_profile = s->studio_profile;
99 918 ff_idctdsp_init(&s->idsp, s->avctx);
100
101 /* load & permutate scantables
102 * note: only wmv uses different ones
103 */
104
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 914 times.
918 if (s->alternate_scan) {
105 4 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
106 4 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
107 } else {
108 914 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
109 914 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
110 }
111 918 ff_permute_scantable(s->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
112 918 s->idsp.idct_permutation);
113 918 ff_permute_scantable(s->permutated_intra_v_scantable, ff_alternate_vertical_scan,
114 918 s->idsp.idct_permutation);
115 918 }
116
117 929 static av_cold int init_duplicate_context(MpegEncContext *s)
118 {
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 929 times.
929 if (!FF_ALLOCZ_TYPED_ARRAY(s->blocks, 1 + s->encoding))
120 return AVERROR(ENOMEM);
121 929 s->block = s->blocks[0];
122
123
2/2
✓ Branch 0 taken 485 times.
✓ Branch 1 taken 444 times.
929 if (s->out_format == FMT_H263) {
124 970 int mb_height = s->msmpeg4_version == MSMP4_VC1 ?
125
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 451 times.
485 FFALIGN(s->mb_height, 2) : s->mb_height;
126 485 int y_size = s->b8_stride * (2 * mb_height + 1);
127 485 int c_size = s->mb_stride * (mb_height + 1);
128 485 int yc_size = y_size + 2 * c_size;
129 /* ac values */
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 485 times.
485 if (!FF_ALLOCZ_TYPED_ARRAY(s->ac_val_base, yc_size))
131 return AVERROR(ENOMEM);
132 485 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
133 485 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
134 485 s->ac_val[2] = s->ac_val[1] + c_size;
135 }
136
137 929 return 0;
138 }
139
140 855 av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
141 {
142 855 int nb_slices = s->slice_context_count, ret;
143
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 651 times.
855 size_t slice_size = s->slice_ctx_size ? s->slice_ctx_size : sizeof(*s);
144
145 855 s->parent = s;
146
147 /* We initialize the copies before the original so that
148 * fields allocated in init_duplicate_context are NULL after
149 * copying. This prevents double-frees upon allocation error. */
150
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 855 times.
929 for (int i = 1; i < nb_slices; i++) {
151 74 s->thread_context[i] = av_memdup(s, slice_size);
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (!s->thread_context[i])
153 return AVERROR(ENOMEM);
154
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
74 if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
155 return ret;
156 74 s->thread_context[i]->start_mb_y =
157 74 (s->mb_height * (i ) + nb_slices / 2) / nb_slices;
158 74 s->thread_context[i]->end_mb_y =
159 74 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
160 }
161 855 s->start_mb_y = 0;
162 877 s->end_mb_y = nb_slices > 1 ? (s->mb_height + nb_slices / 2) / nb_slices
163
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 833 times.
855 : s->mb_height;
164 855 return init_duplicate_context(s);
165 }
166
167 1001 static av_cold void free_duplicate_context(MpegEncContext *s)
168 {
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1001 times.
1001 if (!s)
170 return;
171
172 1001 av_freep(&s->sc.edge_emu_buffer);
173 1001 av_freep(&s->sc.scratchpad_buf);
174 1001 s->sc.obmc_scratchpad = NULL;
175 1001 s->sc.linesize = 0;
176
177 1001 av_freep(&s->blocks);
178 1001 av_freep(&s->ac_val_base);
179 1001 s->block = NULL;
180 }
181
182 927 static av_cold void free_duplicate_contexts(MpegEncContext *s)
183 {
184
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 927 times.
1001 for (int i = 1; i < s->slice_context_count; i++) {
185 74 free_duplicate_context(s->thread_context[i]);
186 74 av_freep(&s->thread_context[i]);
187 }
188 927 free_duplicate_context(s);
189 927 }
190
191 16632 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
192 {
193 #define COPY(a) bak->a = src->a
194 16632 COPY(sc);
195 16632 COPY(blocks);
196 16632 COPY(block);
197 16632 COPY(start_mb_y);
198 16632 COPY(end_mb_y);
199 16632 COPY(ac_val_base);
200 16632 COPY(ac_val[0]);
201 16632 COPY(ac_val[1]);
202 16632 COPY(ac_val[2]);
203 #undef COPY
204 16632 }
205
206 8316 int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
207 {
208 MpegEncContext bak;
209 int ret;
210 // FIXME copy only needed parts
211 8316 backup_duplicate_context(&bak, dst);
212 8316 memcpy(dst, src, sizeof(MpegEncContext));
213 8316 backup_duplicate_context(dst, &bak);
214
215 8316 ret = ff_mpv_framesize_alloc(dst->avctx, &dst->sc, dst->linesize);
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8316 times.
8316 if (ret < 0) {
217 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
218 "scratch buffers.\n");
219 return ret;
220 }
221 8316 return 0;
222 }
223
224 /**
225 * Set the given MpegEncContext to common defaults
226 * (same for encoding and decoding).
227 * The changed fields will not depend upon the
228 * prior state of the MpegEncContext.
229 */
230 840 av_cold void ff_mpv_common_defaults(MpegEncContext *s)
231 {
232 840 s->chroma_qscale_table = ff_default_chroma_qscale_table;
233 840 s->progressive_frame = 1;
234 840 s->progressive_sequence = 1;
235 840 s->picture_structure = PICT_FRAME;
236
237 840 s->picture_number = 0;
238
239 840 s->slice_context_count = 1;
240 840 }
241
242 927 static av_cold void free_buffer_pools(BufferPoolContext *pools)
243 {
244 927 av_refstruct_pool_uninit(&pools->mbskip_table_pool);
245 927 av_refstruct_pool_uninit(&pools->qscale_table_pool);
246 927 av_refstruct_pool_uninit(&pools->mb_type_pool);
247 927 av_refstruct_pool_uninit(&pools->motion_val_pool);
248 927 av_refstruct_pool_uninit(&pools->ref_index_pool);
249 927 pools->alloc_mb_height = pools->alloc_mb_width = pools->alloc_mb_stride = 0;
250 927 }
251
252 855 av_cold int ff_mpv_init_context_frame(MpegEncContext *s)
253 {
254 855 BufferPoolContext *const pools = &s->buffer_pools;
255 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
256 int mb_height;
257
258
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)
259 159 s->mb_height = (s->height + 31) / 32 * 2;
260 else
261 696 s->mb_height = (s->height + 15) / 16;
262
263 /* VC-1 can change from being progressive to interlaced on a per-frame
264 * basis. We therefore allocate certain buffers so big that they work
265 * in both instances. */
266 1710 mb_height = s->msmpeg4_version == MSMP4_VC1 ?
267
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 821 times.
855 FFALIGN(s->mb_height, 2) : s->mb_height;
268
269 855 s->mb_width = (s->width + 15) / 16;
270 855 s->mb_stride = s->mb_width + 1;
271 855 s->b8_stride = s->mb_width * 2 + 1;
272 855 mb_array_size = mb_height * s->mb_stride;
273 855 mv_table_size = (mb_height + 2) * s->mb_stride + 1;
274
275 /* set default edge pos, will be overridden
276 * in decode_header if needed */
277 855 s->h_edge_pos = s->mb_width * 16;
278 855 s->v_edge_pos = s->mb_height * 16;
279
280 855 s->mb_num = s->mb_width * s->mb_height;
281
282 855 s->block_wrap[0] =
283 855 s->block_wrap[1] =
284 855 s->block_wrap[2] =
285 855 s->block_wrap[3] = s->b8_stride;
286 855 s->block_wrap[4] =
287 855 s->block_wrap[5] = s->mb_stride;
288
289 855 y_size = s->b8_stride * (2 * mb_height + 1);
290 855 c_size = s->mb_stride * (mb_height + 1);
291 855 yc_size = y_size + 2 * c_size;
292
293
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 855 times.
855 if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_index2xy, s->mb_num + 1))
294 return AVERROR(ENOMEM);
295
2/2
✓ Branch 0 taken 17097 times.
✓ Branch 1 taken 855 times.
17952 for (y = 0; y < s->mb_height; y++)
296
2/2
✓ Branch 0 taken 553593 times.
✓ Branch 1 taken 17097 times.
570690 for (x = 0; x < s->mb_width; x++)
297 553593 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
298
299 855 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
300
301 #define ALLOC_POOL(name, size, flags) do { \
302 pools->name ##_pool = av_refstruct_pool_alloc((size), (flags)); \
303 if (!pools->name ##_pool) \
304 return AVERROR(ENOMEM); \
305 } while (0)
306
307
2/2
✓ Branch 0 taken 574 times.
✓ Branch 1 taken 281 times.
855 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
308
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 558 times.
574 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
309 /* interlaced direct mode decoding tables */
310 297 int16_t (*tmp)[2] = av_calloc(mv_table_size, 4 * sizeof(*tmp));
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (!tmp)
312 return AVERROR(ENOMEM);
313 297 s->p_field_mv_table_base = tmp;
314 297 tmp += s->mb_stride + 1;
315
2/2
✓ Branch 0 taken 594 times.
✓ Branch 1 taken 297 times.
891 for (int i = 0; i < 2; i++) {
316
2/2
✓ Branch 0 taken 1188 times.
✓ Branch 1 taken 594 times.
1782 for (int j = 0; j < 2; j++) {
317 1188 s->p_field_mv_table[i][j] = tmp;
318 1188 tmp += mv_table_size;
319 }
320 }
321
2/2
✓ Branch 0 taken 281 times.
✓ Branch 1 taken 16 times.
297 if (s->codec_id == AV_CODEC_ID_MPEG4) {
322
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,
323 !s->encoding ? AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME : 0);
324
2/2
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 63 times.
281 if (!s->encoding) {
325 /* cbp, pred_dir */
326
1/2
✓ Branch 1 taken 218 times.
✗ Branch 2 not taken.
218 if (!(s->cbp_table = av_mallocz(mb_array_size)) ||
327
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 218 times.
218 !(s->pred_dir_table = av_mallocz(mb_array_size)))
328 return AVERROR(ENOMEM);
329 }
330 }
331 }
332
333
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 774 times.
855 if (s->msmpeg4_version >= MSMP4_V3) {
334 81 s->coded_block_base = av_mallocz(y_size);
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (!s->coded_block_base)
336 return AVERROR(ENOMEM);
337 81 s->coded_block = s->coded_block_base + s->b8_stride + 1;
338 }
339
340
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) {
341 /* dc values */
342 // MN: we need these for error resilience of intra-frames
343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 737 times.
737 if (!FF_ALLOCZ_TYPED_ARRAY(s->dc_val_base, yc_size))
344 return AVERROR(ENOMEM);
345 737 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
346 737 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
347 737 s->dc_val[2] = s->dc_val[1] + c_size;
348
2/2
✓ Branch 0 taken 3174633 times.
✓ Branch 1 taken 737 times.
3175370 for (i = 0; i < yc_size; i++)
349 3174633 s->dc_val_base[i] = 1024;
350 }
351
352 // Note the + 1 is for a quicker MPEG-4 slice_end detection
353
1/2
✓ Branch 1 taken 855 times.
✗ Branch 2 not taken.
855 if (!(s->mbskip_table = av_mallocz(mb_array_size + 2)) ||
354 /* which mb is an intra block, init macroblock skip table */
355
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 855 times.
855 !(s->mbintra_table = av_malloc(mb_array_size)))
356 return AVERROR(ENOMEM);
357 855 memset(s->mbintra_table, 1, mb_array_size);
358
359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 855 times.
855 ALLOC_POOL(qscale_table, mv_table_size, 0);
360
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 855 times.
855 ALLOC_POOL(mb_type, mv_table_size * sizeof(uint32_t), 0);
361
362
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 ||
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 273 times.
273 (s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) {
364 582 const int b8_array_size = s->b8_stride * mb_height * 2;
365 582 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
366 582 int ref_index_size = 4 * mb_array_size;
367
368 /* FIXME: The output of H.263 with OBMC depends upon
369 * the earlier content of the buffer; therefore we set
370 * the flags to always reset returned buffers here. */
371
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);
372
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 582 times.
582 ALLOC_POOL(ref_index, ref_index_size, 0);
373 }
374 #undef ALLOC_POOL
375 855 pools->alloc_mb_width = s->mb_width;
376 855 pools->alloc_mb_height = mb_height;
377 855 pools->alloc_mb_stride = s->mb_stride;
378
379
2/2
✓ Branch 0 taken 651 times.
✓ Branch 1 taken 204 times.
855 return !CONFIG_MPEGVIDEODEC || s->encoding ? 0 : ff_mpeg_er_init(s);
380 }
381
382 808 static void clear_context(MpegEncContext *s)
383 {
384 808 memset(&s->buffer_pools, 0, sizeof(s->buffer_pools));
385 808 memset(&s->next_pic, 0, sizeof(s->next_pic));
386 808 memset(&s->last_pic, 0, sizeof(s->last_pic));
387 808 memset(&s->cur_pic, 0, sizeof(s->cur_pic));
388
389 808 memset(s->thread_context, 0, sizeof(s->thread_context));
390
391 808 s->block = NULL;
392 808 s->blocks = NULL;
393 808 s->ac_val_base = NULL;
394 808 s->ac_val[0] =
395 808 s->ac_val[1] =
396 808 s->ac_val[2] =NULL;
397 808 memset(&s->sc, 0, sizeof(s->sc));
398
399
400 808 s->p_field_mv_table_base = NULL;
401
2/2
✓ Branch 0 taken 1616 times.
✓ Branch 1 taken 808 times.
2424 for (int i = 0; i < 2; i++)
402
2/2
✓ Branch 0 taken 3232 times.
✓ Branch 1 taken 1616 times.
4848 for (int j = 0; j < 2; j++)
403 3232 s->p_field_mv_table[i][j] = NULL;
404
405 808 s->dc_val_base = NULL;
406 808 s->coded_block_base = NULL;
407 808 s->mbintra_table = NULL;
408 808 s->cbp_table = NULL;
409 808 s->pred_dir_table = NULL;
410
411 808 s->mbskip_table = NULL;
412
413 808 s->er.error_status_table = NULL;
414 808 s->er.er_temp_buffer = NULL;
415 808 s->mb_index2xy = NULL;
416 808 }
417
418 /**
419 * init common structure for both encoder and decoder.
420 * this assumes that some variables like width/height are already set
421 */
422 808 av_cold int ff_mpv_common_init(MpegEncContext *s)
423 {
424 808 int nb_slices = (HAVE_THREADS &&
425 808 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
426
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 786 times.
808 s->avctx->thread_count : 1;
427 int ret;
428
429 808 clear_context(s);
430
431
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)
432 12 nb_slices = s->avctx->slices;
433
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 808 times.
808 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
435 av_log(s->avctx, AV_LOG_ERROR,
436 "decoding to AV_PIX_FMT_NONE is not supported.\n");
437 return AVERROR(EINVAL);
438 }
439
440
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) &&
441 792 av_image_check_size(s->width, s->height, 0, s->avctx))
442 return AVERROR(EINVAL);
443
444 808 dsp_init(s);
445
446 /* set chroma shifts */
447 808 ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
448 &s->chroma_x_shift,
449 &s->chroma_y_shift);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 808 times.
808 if (ret)
451 return ret;
452
453
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 808 times.
808 if ((ret = ff_mpv_init_context_frame(s)))
454 goto fail;
455
456
5/6
✓ Branch 0 taken 808 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 791 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 16 times.
808 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
457 int max_slices;
458
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->mb_height)
459 1 max_slices = FFMIN(MAX_THREADS, s->mb_height);
460 else
461 max_slices = MAX_THREADS;
462 1 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
463 " reducing to %d\n", nb_slices, max_slices);
464 1 nb_slices = max_slices;
465 }
466
467 808 s->context_initialized = 1;
468 808 memset(s->thread_context, 0, sizeof(s->thread_context));
469 808 s->thread_context[0] = s;
470 808 s->slice_context_count = nb_slices;
471
472 // if (s->width && s->height) {
473 808 ret = ff_mpv_init_duplicate_contexts(s);
474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 808 times.
808 if (ret < 0)
475 goto fail;
476 // }
477
478 808 return 0;
479 fail:
480 ff_mpv_common_end(s);
481 return ret;
482 }
483
484 927 av_cold void ff_mpv_free_context_frame(MpegEncContext *s)
485 {
486 927 free_duplicate_contexts(s);
487
488 927 free_buffer_pools(&s->buffer_pools);
489 927 av_freep(&s->p_field_mv_table_base);
490
2/2
✓ Branch 0 taken 1854 times.
✓ Branch 1 taken 927 times.
2781 for (int i = 0; i < 2; i++)
491
2/2
✓ Branch 0 taken 3708 times.
✓ Branch 1 taken 1854 times.
5562 for (int j = 0; j < 2; j++)
492 3708 s->p_field_mv_table[i][j] = NULL;
493
494 927 av_freep(&s->dc_val_base);
495 927 av_freep(&s->coded_block_base);
496 927 av_freep(&s->mbintra_table);
497 927 av_freep(&s->cbp_table);
498 927 av_freep(&s->pred_dir_table);
499
500 927 av_freep(&s->mbskip_table);
501
502 927 av_freep(&s->er.error_status_table);
503 927 av_freep(&s->er.er_temp_buffer);
504 927 av_freep(&s->mb_index2xy);
505
506 927 s->linesize = s->uvlinesize = 0;
507 927 }
508
509 880 av_cold void ff_mpv_common_end(MpegEncContext *s)
510 {
511 880 ff_mpv_free_context_frame(s);
512
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 858 times.
880 if (s->slice_context_count > 1)
513 22 s->slice_context_count = 1;
514
515 880 ff_mpv_unref_picture(&s->last_pic);
516 880 ff_mpv_unref_picture(&s->cur_pic);
517 880 ff_mpv_unref_picture(&s->next_pic);
518
519 880 s->context_initialized = 0;
520 880 s->context_reinit = 0;
521 880 s->linesize = s->uvlinesize = 0;
522 880 }
523
524
525 /**
526 * Clean dc, ac for the current non-intra MB.
527 */
528 449447 void ff_clean_intra_table_entries(MpegEncContext *s)
529 {
530 449447 int wrap = s->b8_stride;
531 449447 int xy = s->block_index[0];
532
533 449447 s->dc_val[0][xy ] =
534 449447 s->dc_val[0][xy + 1 ] =
535 449447 s->dc_val[0][xy + wrap] =
536 449447 s->dc_val[0][xy + 1 + wrap] = 1024;
537 /* ac pred */
538 449447 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
539 449447 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
540 /* chroma */
541 449447 wrap = s->mb_stride;
542 449447 xy = s->mb_x + s->mb_y * wrap;
543 449447 s->dc_val[1][xy] =
544 449447 s->dc_val[2][xy] = 1024;
545 /* ac pred */
546 449447 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
547 449447 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
548
549 449447 s->mbintra_table[xy]= 0;
550 449447 }
551
552 840724 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
553 840724 const int linesize = s->cur_pic.linesize[0]; //not s->linesize as this would be wrong for field pics
554 840724 const int uvlinesize = s->cur_pic.linesize[1];
555
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 840694 times.
840724 const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
556 840724 const int height_of_mb = 4 - s->avctx->lowres;
557
558 840724 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
559 840724 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
560 840724 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
561 840724 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
562 840724 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
563 840724 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;
564 //block_index is not used by mpeg2, so it is not affected by chroma_format
565
566 840724 s->dest[0] = s->cur_pic.data[0] + (int)((s->mb_x - 1U) << width_of_mb);
567 840724 s->dest[1] = s->cur_pic.data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
568 840724 s->dest[2] = s->cur_pic.data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
569
570
2/2
✓ Branch 0 taken 801130 times.
✓ Branch 1 taken 39594 times.
840724 if (s->picture_structure == PICT_FRAME) {
571 801130 s->dest[0] += s->mb_y * linesize << height_of_mb;
572 801130 s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
573 801130 s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
574 } else {
575 39594 s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
576 39594 s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
577 39594 s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
578 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
579 }
580 840724 }
581
582 /**
583 * set qscale and update qscale dependent variables.
584 */
585 1456170 void ff_set_qscale(MpegEncContext * s, int qscale)
586 {
587
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1456169 times.
1456170 if (qscale < 1)
588 1 qscale = 1;
589
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1456140 times.
1456169 else if (qscale > 31)
590 29 qscale = 31;
591
592 1456170 s->qscale = qscale;
593 1456170 s->chroma_qscale= s->chroma_qscale_table[qscale];
594
595 1456170 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
596 1456170 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
597 1456170 }
598