FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 281 319 88.1%
Functions: 18 20 90.0%
Branches: 115 150 76.7%

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 804 static av_cold void dsp_init(MpegEncContext *s)
62 {
63 804 ff_blockdsp_init(&s->bdsp);
64 804 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
65 804 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
66
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 804 times.
804 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 804 }
80
81 5449 av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st,
82 const uint8_t *src_scantable)
83 {
84 5449 st->scantable = src_scantable;
85
86
2/2
✓ Branch 0 taken 348736 times.
✓ Branch 1 taken 5449 times.
354185 for (int i = 0, end = -1; i < 64; i++) {
87 348736 int j = src_scantable[i];
88 348736 st->permutated[i] = permutation[j];
89
2/2
✓ Branch 0 taken 102721 times.
✓ Branch 1 taken 246015 times.
348736 if (permutation[j] > end)
90 102721 end = permutation[j];
91 348736 st->raster_end[i] = end;
92 }
93 5449 }
94
95 916 av_cold void ff_mpv_idct_init(MpegEncContext *s)
96 {
97
2/2
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 663 times.
916 if (s->codec_id == AV_CODEC_ID_MPEG4)
98 253 s->idsp.mpeg4_studio_profile = s->studio_profile;
99 916 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 912 times.
916 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 912 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
109 912 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
110 }
111 916 ff_permute_scantable(s->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
112 916 s->idsp.idct_permutation);
113 916 ff_permute_scantable(s->permutated_intra_v_scantable, ff_alternate_vertical_scan,
114 916 s->idsp.idct_permutation);
115 916 }
116
117 925 static av_cold int init_duplicate_context(MpegEncContext *s)
118 {
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 925 times.
925 if (!FF_ALLOCZ_TYPED_ARRAY(s->blocks, 1 + s->encoding))
120 return AVERROR(ENOMEM);
121 925 s->block = s->blocks[0];
122
123
2/2
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 442 times.
925 if (s->out_format == FMT_H263) {
124 966 int mb_height = s->msmpeg4_version == MSMP4_VC1 ?
125
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 449 times.
483 FFALIGN(s->mb_height, 2) : s->mb_height;
126 483 int y_size = s->b8_stride * (2 * mb_height + 1);
127 483 int c_size = s->mb_stride * (mb_height + 1);
128 483 int yc_size = y_size + 2 * c_size;
129 /* ac values */
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 483 times.
483 if (!FF_ALLOCZ_TYPED_ARRAY(s->ac_val_base, yc_size))
131 return AVERROR(ENOMEM);
132 483 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
133 483 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
134 483 s->ac_val[2] = s->ac_val[1] + c_size;
135 }
136
137 925 return 0;
138 }
139
140 851 av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
141 {
142 851 int nb_slices = s->slice_context_count, ret;
143
2/2
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 647 times.
851 size_t slice_size = s->slice_ctx_size ? s->slice_ctx_size : sizeof(*s);
144
145 /* We initialize the copies before the original so that
146 * fields allocated in init_duplicate_context are NULL after
147 * copying. This prevents double-frees upon allocation error. */
148
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 851 times.
925 for (int i = 1; i < nb_slices; i++) {
149 74 s->thread_context[i] = av_memdup(s, slice_size);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 if (!s->thread_context[i])
151 return AVERROR(ENOMEM);
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 74 times.
74 if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
153 return ret;
154 74 s->thread_context[i]->start_mb_y =
155 74 (s->mb_height * (i ) + nb_slices / 2) / nb_slices;
156 74 s->thread_context[i]->end_mb_y =
157 74 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
158 }
159 851 s->start_mb_y = 0;
160 873 s->end_mb_y = nb_slices > 1 ? (s->mb_height + nb_slices / 2) / nb_slices
161
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 829 times.
851 : s->mb_height;
162 851 return init_duplicate_context(s);
163 }
164
165 997 static av_cold void free_duplicate_context(MpegEncContext *s)
166 {
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 997 times.
997 if (!s)
168 return;
169
170 997 av_freep(&s->sc.edge_emu_buffer);
171 997 av_freep(&s->sc.scratchpad_buf);
172 997 s->sc.obmc_scratchpad = NULL;
173 997 s->sc.linesize = 0;
174
175 997 av_freep(&s->blocks);
176 997 av_freep(&s->ac_val_base);
177 997 s->block = NULL;
178 }
179
180 923 static av_cold void free_duplicate_contexts(MpegEncContext *s)
181 {
182
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 923 times.
997 for (int i = 1; i < s->slice_context_count; i++) {
183 74 free_duplicate_context(s->thread_context[i]);
184 74 av_freep(&s->thread_context[i]);
185 }
186 923 free_duplicate_context(s);
187 923 }
188
189 16632 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
190 {
191 #define COPY(a) bak->a = src->a
192 16632 COPY(sc);
193 16632 COPY(blocks);
194 16632 COPY(block);
195 16632 COPY(start_mb_y);
196 16632 COPY(end_mb_y);
197 16632 COPY(ac_val_base);
198 16632 COPY(ac_val[0]);
199 16632 COPY(ac_val[1]);
200 16632 COPY(ac_val[2]);
201 #undef COPY
202 16632 }
203
204 8316 int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
205 {
206 MpegEncContext bak;
207 int ret;
208 // FIXME copy only needed parts
209 8316 backup_duplicate_context(&bak, dst);
210 8316 memcpy(dst, src, sizeof(MpegEncContext));
211 8316 backup_duplicate_context(dst, &bak);
212
213 8316 ret = ff_mpv_framesize_alloc(dst->avctx, &dst->sc, dst->linesize);
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8316 times.
8316 if (ret < 0) {
215 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
216 "scratch buffers.\n");
217 return ret;
218 }
219 8316 return 0;
220 }
221
222 /**
223 * Set the given MpegEncContext to common defaults
224 * (same for encoding and decoding).
225 * The changed fields will not depend upon the
226 * prior state of the MpegEncContext.
227 */
228 838 av_cold void ff_mpv_common_defaults(MpegEncContext *s)
229 {
230 838 s->chroma_qscale_table = ff_default_chroma_qscale_table;
231 838 s->progressive_frame = 1;
232 838 s->progressive_sequence = 1;
233 838 s->picture_structure = PICT_FRAME;
234
235 838 s->picture_number = 0;
236
237 838 s->slice_context_count = 1;
238 838 }
239
240 923 static av_cold void free_buffer_pools(BufferPoolContext *pools)
241 {
242 923 av_refstruct_pool_uninit(&pools->mbskip_table_pool);
243 923 av_refstruct_pool_uninit(&pools->qscale_table_pool);
244 923 av_refstruct_pool_uninit(&pools->mb_type_pool);
245 923 av_refstruct_pool_uninit(&pools->motion_val_pool);
246 923 av_refstruct_pool_uninit(&pools->ref_index_pool);
247 923 pools->alloc_mb_height = pools->alloc_mb_width = pools->alloc_mb_stride = 0;
248 923 }
249
250 851 av_cold int ff_mpv_init_context_frame(MpegEncContext *s)
251 {
252 851 BufferPoolContext *const pools = &s->buffer_pools;
253 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
254 int mb_height;
255
256
4/4
✓ Branch 0 taken 280 times.
✓ Branch 1 taken 571 times.
✓ Branch 2 taken 159 times.
✓ Branch 3 taken 121 times.
851 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
257 159 s->mb_height = (s->height + 31) / 32 * 2;
258 else
259 692 s->mb_height = (s->height + 15) / 16;
260
261 /* VC-1 can change from being progressive to interlaced on a per-frame
262 * basis. We therefore allocate certain buffers so big that they work
263 * in both instances. */
264 1702 mb_height = s->msmpeg4_version == MSMP4_VC1 ?
265
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 817 times.
851 FFALIGN(s->mb_height, 2) : s->mb_height;
266
267 851 s->mb_width = (s->width + 15) / 16;
268 851 s->mb_stride = s->mb_width + 1;
269 851 s->b8_stride = s->mb_width * 2 + 1;
270 851 mb_array_size = mb_height * s->mb_stride;
271 851 mv_table_size = (mb_height + 2) * s->mb_stride + 1;
272
273 /* set default edge pos, will be overridden
274 * in decode_header if needed */
275 851 s->h_edge_pos = s->mb_width * 16;
276 851 s->v_edge_pos = s->mb_height * 16;
277
278 851 s->mb_num = s->mb_width * s->mb_height;
279
280 851 s->block_wrap[0] =
281 851 s->block_wrap[1] =
282 851 s->block_wrap[2] =
283 851 s->block_wrap[3] = s->b8_stride;
284 851 s->block_wrap[4] =
285 851 s->block_wrap[5] = s->mb_stride;
286
287 851 y_size = s->b8_stride * (2 * mb_height + 1);
288 851 c_size = s->mb_stride * (mb_height + 1);
289 851 yc_size = y_size + 2 * c_size;
290
291
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 851 times.
851 if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_index2xy, s->mb_num + 1))
292 return AVERROR(ENOMEM);
293
2/2
✓ Branch 0 taken 17043 times.
✓ Branch 1 taken 851 times.
17894 for (y = 0; y < s->mb_height; y++)
294
2/2
✓ Branch 0 taken 552603 times.
✓ Branch 1 taken 17043 times.
569646 for (x = 0; x < s->mb_width; x++)
295 552603 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
296
297 851 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
298
299 #define ALLOC_POOL(name, size, flags) do { \
300 pools->name ##_pool = av_refstruct_pool_alloc((size), (flags)); \
301 if (!pools->name ##_pool) \
302 return AVERROR(ENOMEM); \
303 } while (0)
304
305
2/2
✓ Branch 0 taken 572 times.
✓ Branch 1 taken 279 times.
851 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
306
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 556 times.
572 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
307 /* interlaced direct mode decoding tables */
308 295 int16_t (*tmp)[2] = av_calloc(mv_table_size, 4 * sizeof(*tmp));
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 if (!tmp)
310 return AVERROR(ENOMEM);
311 295 s->p_field_mv_table_base = tmp;
312 295 tmp += s->mb_stride + 1;
313
2/2
✓ Branch 0 taken 590 times.
✓ Branch 1 taken 295 times.
885 for (int i = 0; i < 2; i++) {
314
2/2
✓ Branch 0 taken 1180 times.
✓ Branch 1 taken 590 times.
1770 for (int j = 0; j < 2; j++) {
315 1180 s->p_field_mv_table[i][j] = tmp;
316 1180 tmp += mv_table_size;
317 }
318 }
319
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 16 times.
295 if (s->codec_id == AV_CODEC_ID_MPEG4) {
320
3/4
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 63 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 279 times.
279 ALLOC_POOL(mbskip_table, mb_array_size + 2,
321 !s->encoding ? AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME : 0);
322
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 63 times.
279 if (!s->encoding) {
323 /* cbp, pred_dir */
324
1/2
✓ Branch 1 taken 216 times.
✗ Branch 2 not taken.
216 if (!(s->cbp_table = av_mallocz(mb_array_size)) ||
325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 216 times.
216 !(s->pred_dir_table = av_mallocz(mb_array_size)))
326 return AVERROR(ENOMEM);
327 }
328 }
329 }
330
331
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 770 times.
851 if (s->msmpeg4_version >= MSMP4_V3) {
332 81 s->coded_block_base = av_mallocz(y_size);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (!s->coded_block_base)
334 return AVERROR(ENOMEM);
335 81 s->coded_block = s->coded_block_base + s->b8_stride + 1;
336 }
337
338
6/6
✓ Branch 0 taken 476 times.
✓ Branch 1 taken 375 times.
✓ Branch 2 taken 463 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 345 times.
✓ Branch 5 taken 118 times.
851 if (s->h263_pred || s->h263_aic || !s->encoding) {
339 /* dc values */
340 // MN: we need these for error resilience of intra-frames
341 // Allocating them unconditionally for decoders also means
342 // that we don't need to reinitialize when e.g. h263_aic changes.
343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 733 times.
733 if (!FF_ALLOCZ_TYPED_ARRAY(s->dc_val_base, yc_size))
344 return AVERROR(ENOMEM);
345 733 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
346 733 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
347 733 s->dc_val[2] = s->dc_val[1] + c_size;
348
2/2
✓ Branch 0 taken 3168201 times.
✓ Branch 1 taken 733 times.
3168934 for (i = 0; i < yc_size; i++)
349 3168201 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 851 times.
✗ Branch 2 not taken.
851 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 851 times.
851 !(s->mbintra_table = av_malloc(mb_array_size)))
356 return AVERROR(ENOMEM);
357 851 memset(s->mbintra_table, 1, mb_array_size);
358
359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 851 times.
851 ALLOC_POOL(qscale_table, mv_table_size, 0);
360
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 851 times.
851 ALLOC_POOL(mb_type, mv_table_size * sizeof(uint32_t), 0);
361
362
4/4
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 479 times.
✓ Branch 2 taken 271 times.
✓ Branch 3 taken 101 times.
851 if (s->out_format == FMT_H263 || s->encoding ||
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 271 times.
271 (s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) {
364 580 const int b8_array_size = s->b8_stride * mb_height * 2;
365 580 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
366 580 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 580 times.
580 ALLOC_POOL(motion_val, mv_size, AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
372
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 580 times.
580 ALLOC_POOL(ref_index, ref_index_size, 0);
373 }
374 #undef ALLOC_POOL
375 851 pools->alloc_mb_width = s->mb_width;
376 851 pools->alloc_mb_height = mb_height;
377 851 pools->alloc_mb_stride = s->mb_stride;
378
379
2/2
✓ Branch 0 taken 647 times.
✓ Branch 1 taken 204 times.
851 return !CONFIG_MPEGVIDEODEC || s->encoding ? 0 : ff_mpeg_er_init(s);
380 }
381
382 /**
383 * init common structure for both encoder and decoder.
384 * this assumes that some variables like width/height are already set
385 */
386 804 av_cold int ff_mpv_common_init(MpegEncContext *s)
387 {
388 804 int nb_slices = (HAVE_THREADS &&
389 804 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
390
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 782 times.
804 s->avctx->thread_count : 1;
391 int ret;
392
393
4/4
✓ Branch 0 taken 204 times.
✓ Branch 1 taken 600 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 192 times.
804 if (s->encoding && s->avctx->slices)
394 12 nb_slices = s->avctx->slices;
395
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 804 times.
804 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
397 av_log(s->avctx, AV_LOG_ERROR,
398 "decoding to AV_PIX_FMT_NONE is not supported.\n");
399 return AVERROR(EINVAL);
400 }
401
402
4/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 788 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 788 times.
1592 if ((s->width || s->height) &&
403 788 av_image_check_size(s->width, s->height, 0, s->avctx))
404 return AVERROR(EINVAL);
405
406 804 dsp_init(s);
407
408 /* set chroma shifts */
409 804 ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
410 &s->chroma_x_shift,
411 &s->chroma_y_shift);
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 804 times.
804 if (ret)
413 return ret;
414
415
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 804 times.
804 if ((ret = ff_mpv_init_context_frame(s)))
416 goto fail;
417
418
5/6
✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 787 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 16 times.
804 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
419 int max_slices;
420
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (s->mb_height)
421 1 max_slices = FFMIN(MAX_THREADS, s->mb_height);
422 else
423 max_slices = MAX_THREADS;
424 1 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
425 " reducing to %d\n", nb_slices, max_slices);
426 1 nb_slices = max_slices;
427 }
428
429 804 s->context_initialized = 1;
430 804 memset(s->thread_context, 0, sizeof(s->thread_context));
431 804 s->thread_context[0] = s;
432 804 s->slice_context_count = nb_slices;
433
434 // if (s->width && s->height) {
435 804 ret = ff_mpv_init_duplicate_contexts(s);
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 804 times.
804 if (ret < 0)
437 goto fail;
438 // }
439
440 804 return 0;
441 fail:
442 ff_mpv_common_end(s);
443 return ret;
444 }
445
446 923 av_cold void ff_mpv_free_context_frame(MpegEncContext *s)
447 {
448 923 free_duplicate_contexts(s);
449
450 923 free_buffer_pools(&s->buffer_pools);
451 923 av_freep(&s->p_field_mv_table_base);
452
2/2
✓ Branch 0 taken 1846 times.
✓ Branch 1 taken 923 times.
2769 for (int i = 0; i < 2; i++)
453
2/2
✓ Branch 0 taken 3692 times.
✓ Branch 1 taken 1846 times.
5538 for (int j = 0; j < 2; j++)
454 3692 s->p_field_mv_table[i][j] = NULL;
455
456 923 av_freep(&s->dc_val_base);
457 923 av_freep(&s->coded_block_base);
458 923 av_freep(&s->mbintra_table);
459 923 av_freep(&s->cbp_table);
460 923 av_freep(&s->pred_dir_table);
461
462 923 av_freep(&s->mbskip_table);
463
464 923 av_freep(&s->er.error_status_table);
465 923 av_freep(&s->er.er_temp_buffer);
466 923 av_freep(&s->mb_index2xy);
467
468 923 s->linesize = s->uvlinesize = 0;
469 923 }
470
471 876 av_cold void ff_mpv_common_end(MpegEncContext *s)
472 {
473 876 ff_mpv_free_context_frame(s);
474
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 854 times.
876 if (s->slice_context_count > 1)
475 22 s->slice_context_count = 1;
476
477 876 ff_mpv_unref_picture(&s->last_pic);
478 876 ff_mpv_unref_picture(&s->cur_pic);
479 876 ff_mpv_unref_picture(&s->next_pic);
480
481 876 s->context_initialized = 0;
482 876 s->context_reinit = 0;
483 876 s->linesize = s->uvlinesize = 0;
484 876 }
485
486
487 /**
488 * Clean dc, ac for the current non-intra MB.
489 */
490 448313 void ff_clean_intra_table_entries(MpegEncContext *s)
491 {
492 448313 int wrap = s->b8_stride;
493 448313 int xy = s->block_index[0];
494
495 448313 s->dc_val[0][xy ] =
496 448313 s->dc_val[0][xy + 1 ] =
497 448313 s->dc_val[0][xy + wrap] =
498 448313 s->dc_val[0][xy + 1 + wrap] = 1024;
499 /* ac pred */
500 448313 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
501 448313 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
502 /* chroma */
503 448313 wrap = s->mb_stride;
504 448313 xy = s->mb_x + s->mb_y * wrap;
505 448313 s->dc_val[1][xy] =
506 448313 s->dc_val[2][xy] = 1024;
507 /* ac pred */
508 448313 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
509 448313 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
510
511 448313 s->mbintra_table[xy]= 0;
512 448313 }
513
514 840875 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
515 840875 const int linesize = s->cur_pic.linesize[0]; //not s->linesize as this would be wrong for field pics
516 840875 const int uvlinesize = s->cur_pic.linesize[1];
517
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 840845 times.
840875 const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
518 840875 const int height_of_mb = 4 - s->avctx->lowres;
519
520 840875 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
521 840875 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
522 840875 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
523 840875 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
524 840875 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
525 840875 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;
526 //block_index is not used by mpeg2, so it is not affected by chroma_format
527
528 840875 s->dest[0] = s->cur_pic.data[0] + (int)((s->mb_x - 1U) << width_of_mb);
529 840875 s->dest[1] = s->cur_pic.data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
530 840875 s->dest[2] = s->cur_pic.data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
531
532
2/2
✓ Branch 0 taken 800931 times.
✓ Branch 1 taken 39944 times.
840875 if (s->picture_structure == PICT_FRAME) {
533 800931 s->dest[0] += s->mb_y * linesize << height_of_mb;
534 800931 s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
535 800931 s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
536 } else {
537 39944 s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
538 39944 s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
539 39944 s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
540 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
541 }
542 840875 }
543
544 /**
545 * set qscale and update qscale dependent variables.
546 */
547 1454077 void ff_set_qscale(MpegEncContext * s, int qscale)
548 {
549
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1454076 times.
1454077 if (qscale < 1)
550 1 qscale = 1;
551
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1454047 times.
1454076 else if (qscale > 31)
552 29 qscale = 31;
553
554 1454077 s->qscale = qscale;
555 1454077 s->chroma_qscale= s->chroma_qscale_table[qscale];
556
557 1454077 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
558 1454077 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
559 1454077 }
560