FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo.c
Date: 2026-06-16 12:54:33
Exec Total Coverage
Lines: 241 277 87.0%
Functions: 15 17 88.2%
Branches: 110 144 76.4%

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/intreadwrite.h"
35 #include "libavutil/mem.h"
36
37 #include "avcodec.h"
38 #include "blockdsp.h"
39 #include "idctdsp.h"
40 #include "mathops.h"
41 #include "mpeg_er.h"
42 #include "mpegutils.h"
43 #include "mpegvideo.h"
44 #include "mpegvideodata.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 822 static av_cold void dsp_init(MpegEncContext *s)
62 {
63 822 ff_blockdsp_init(&s->bdsp);
64 822 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
65 822 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
66
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 822 times.
822 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 822 }
80
81 940 av_cold void ff_mpv_idct_init(MpegEncContext *s)
82 {
83
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 684 times.
940 if (s->codec_id == AV_CODEC_ID_MPEG4)
84 256 s->idsp.mpeg4_studio_profile = s->studio_profile;
85 940 ff_idctdsp_init(&s->idsp, s->avctx);
86
87 /* load & permutate scantables
88 * note: only wmv uses different ones
89 */
90
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 936 times.
940 if (s->alternate_scan) {
91 4 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
92 4 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
93 } else {
94 936 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
95 936 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
96 }
97 940 }
98
99 871 av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
100 {
101 871 const int nb_slices = s->slice_context_count;
102 871 const size_t slice_size = s->slice_ctx_size;
103
104
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 871 times.
981 for (int i = 1; i < nb_slices; i++) {
105 110 s->thread_context[i] = av_memdup(s, slice_size);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
110 if (!s->thread_context[i])
107 return AVERROR(ENOMEM);
108 110 s->thread_context[i]->start_mb_y =
109 110 (s->mb_height * (i ) + nb_slices / 2) / nb_slices;
110 110 s->thread_context[i]->end_mb_y =
111 110 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
112 }
113 871 s->start_mb_y = 0;
114 901 s->end_mb_y = nb_slices > 1 ? (s->mb_height + nb_slices / 2) / nb_slices
115
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 841 times.
871 : s->mb_height;
116 871 return 0;
117 }
118
119 1055 static av_cold void free_duplicate_context(MpegEncContext *s)
120 {
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1055 times.
1055 if (!s)
122 return;
123
124 1055 av_freep(&s->sc.edge_emu_buffer);
125 1055 av_freep(&s->sc.scratchpad_buf);
126 1055 s->sc.obmc_scratchpad = NULL;
127 1055 s->sc.linesize = 0;
128 }
129
130 945 static av_cold void free_duplicate_contexts(MpegEncContext *s)
131 {
132
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 945 times.
1055 for (int i = 1; i < s->slice_context_count; i++) {
133 110 free_duplicate_context(s->thread_context[i]);
134 110 av_freep(&s->thread_context[i]);
135 }
136 945 free_duplicate_context(s);
137 945 }
138
139 10012 int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
140 {
141 #define COPY(M) \
142 M(ScratchpadContext, sc) \
143 M(int, start_mb_y) \
144 M(int, end_mb_y) \
145 M(int16_t*, dc_val) \
146 M(void*, ac_val)
147
148 int ret;
149 // FIXME copy only needed parts
150 #define BACKUP(T, member) T member = dst->member;
151 10012 COPY(BACKUP)
152 10012 memcpy(dst, src, sizeof(MpegEncContext));
153 #define RESTORE(T, member) dst->member = member;
154 10012 COPY(RESTORE)
155
156 10012 ret = ff_mpv_framesize_alloc(dst->avctx, &dst->sc, dst->linesize);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10012 times.
10012 if (ret < 0) {
158 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
159 "scratch buffers.\n");
160 return ret;
161 }
162 10012 return 0;
163 }
164
165 /**
166 * Set the given MpegEncContext to common defaults
167 * (same for encoding and decoding).
168 * The changed fields will not depend upon the
169 * prior state of the MpegEncContext.
170 */
171 862 av_cold void ff_mpv_common_defaults(MpegEncContext *s)
172 {
173 862 s->chroma_qscale_table = ff_default_chroma_qscale_table;
174 862 s->progressive_frame = 1;
175 862 s->progressive_sequence = 1;
176 862 s->picture_structure = PICT_FRAME;
177
178 862 s->slice_context_count = 1;
179 862 }
180
181 945 static av_cold void free_buffer_pools(BufferPoolContext *pools)
182 {
183 945 av_refstruct_pool_uninit(&pools->mbskip_table_pool);
184 945 av_refstruct_pool_uninit(&pools->qscale_table_pool);
185 945 av_refstruct_pool_uninit(&pools->mb_type_pool);
186 945 av_refstruct_pool_uninit(&pools->motion_val_pool);
187 945 av_refstruct_pool_uninit(&pools->ref_index_pool);
188 945 pools->alloc_mb_height = pools->alloc_mb_width = pools->alloc_mb_stride = 0;
189 945 }
190
191 871 av_cold int ff_mpv_init_context_frame(MpegEncContext *s)
192 {
193 871 int nb_slices = (HAVE_THREADS &&
194 871 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
195
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 841 times.
871 s->avctx->thread_count : 1;
196 871 BufferPoolContext *const pools = &s->buffer_pools;
197 int y_size, c_size, yc_size, mb_array_size, mv_table_size, x, y;
198 int mb_height;
199
200
4/4
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 655 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 204 times.
871 if (s->encoding && s->avctx->slices)
201 12 nb_slices = s->avctx->slices;
202
203
4/4
✓ Branch 0 taken 297 times.
✓ Branch 1 taken 574 times.
✓ Branch 2 taken 163 times.
✓ Branch 3 taken 134 times.
871 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
204 163 s->mb_height = (s->height + 31) / 32 * 2;
205 else
206 708 s->mb_height = (s->height + 15) / 16;
207
208
5/6
✓ Branch 0 taken 871 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 851 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 12 times.
871 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
209 int max_slices;
210
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (s->mb_height)
211 8 max_slices = FFMIN(MAX_THREADS, s->mb_height);
212 else
213 max_slices = MAX_THREADS;
214 8 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
215 " reducing to %d\n", nb_slices, max_slices);
216 8 nb_slices = max_slices;
217 }
218
219 871 s->slice_context_count = nb_slices;
220
221 /* VC-1 can change from being progressive to interlaced on a per-frame
222 * basis. We therefore allocate certain buffers so big that they work
223 * in both instances. */
224 1742 mb_height = s->msmpeg4_version == MSMP4_VC1 ?
225
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 837 times.
871 FFALIGN(s->mb_height, 2) : s->mb_height;
226
227 871 s->mb_width = (s->width + 15) / 16;
228 871 s->mb_stride = s->mb_width + 1;
229 871 s->b8_stride = s->mb_width * 2 + 1;
230 871 mb_array_size = mb_height * s->mb_stride;
231 871 mv_table_size = (mb_height + 2) * s->mb_stride + 1;
232
233 /* set default edge pos, will be overridden
234 * in decode_header if needed */
235 871 s->h_edge_pos = s->mb_width * 16;
236 871 s->v_edge_pos = s->mb_height * 16;
237
238 871 s->mb_num = s->mb_width * s->mb_height;
239
240 871 s->block_wrap[0] =
241 871 s->block_wrap[1] =
242 871 s->block_wrap[2] =
243 871 s->block_wrap[3] = s->b8_stride;
244 871 s->block_wrap[4] =
245 871 s->block_wrap[5] = s->mb_stride;
246
247 871 y_size = s->b8_stride * (2 * mb_height + 1);
248 871 c_size = s->mb_stride * (mb_height + 1);
249 871 yc_size = y_size + 2 * c_size;
250
251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 871 times.
871 if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_index2xy, s->mb_num + 1))
252 return AVERROR(ENOMEM);
253
2/2
✓ Branch 0 taken 17382 times.
✓ Branch 1 taken 871 times.
18253 for (y = 0; y < s->mb_height; y++)
254
2/2
✓ Branch 0 taken 562283 times.
✓ Branch 1 taken 17382 times.
579665 for (x = 0; x < s->mb_width; x++)
255 562283 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
256
257 871 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
258
259 #define ALLOC_POOL(name, size, flags) do { \
260 pools->name ##_pool = av_refstruct_pool_alloc((size), (flags)); \
261 if (!pools->name ##_pool) \
262 return AVERROR(ENOMEM); \
263 } while (0)
264
265
2/2
✓ Branch 0 taken 587 times.
✓ Branch 1 taken 284 times.
871 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
266
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 571 times.
587 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
267 /* interlaced direct mode decoding tables */
268 300 int16_t (*tmp)[2] = av_calloc(mv_table_size, 4 * sizeof(*tmp));
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
300 if (!tmp)
270 return AVERROR(ENOMEM);
271 300 s->p_field_mv_table_base = tmp;
272 300 tmp += s->mb_stride + 1;
273
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 300 times.
900 for (int i = 0; i < 2; i++) {
274
2/2
✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 600 times.
1800 for (int j = 0; j < 2; j++) {
275 1200 s->p_field_mv_table[i][j] = tmp;
276 1200 tmp += mv_table_size;
277 }
278 }
279
2/2
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 16 times.
300 if (s->codec_id == AV_CODEC_ID_MPEG4) {
280
3/4
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 64 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 284 times.
284 ALLOC_POOL(mbskip_table, mb_array_size + 2,
281 !s->encoding ? AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME : 0);
282
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 64 times.
284 if (!s->encoding) {
283 /* cbp, pred_dir */
284
1/2
✓ Branch 1 taken 220 times.
✗ Branch 2 not taken.
220 if (!(s->cbp_table = av_mallocz(mb_array_size)) ||
285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 220 times.
220 !(s->pred_dir_table = av_mallocz(mb_array_size)))
286 return AVERROR(ENOMEM);
287 }
288 }
289 }
290
291
2/2
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 790 times.
871 if (s->msmpeg4_version >= MSMP4_V3) {
292 81 s->coded_block_base = av_mallocz(y_size);
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 if (!s->coded_block_base)
294 return AVERROR(ENOMEM);
295 81 s->coded_block = s->coded_block_base + s->b8_stride + 1;
296 }
297
298
6/6
✓ Branch 0 taken 491 times.
✓ Branch 1 taken 380 times.
✓ Branch 2 taken 478 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 349 times.
✓ Branch 5 taken 129 times.
871 if (s->h263_pred || s->h263_aic || !s->encoding) {
299 // When encoding, each slice (and therefore each thread)
300 // gets its own ac_val and dc_val buffers in order to avoid
301 // races.
302
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 655 times.
742 size_t allslice_yc_size = yc_size * (s->encoding ? nb_slices : 1);
303
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 275 times.
742 if (s->out_format == FMT_H263) {
304 /* ac values */
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 467 times.
467 if (!FF_ALLOCZ_TYPED_ARRAY(s->ac_val_base, allslice_yc_size))
306 return AVERROR(ENOMEM);
307 467 s->ac_val = s->ac_val_base + s->b8_stride + 1;
308 }
309
310 /* dc values */
311 // MN: we need these for error resilience of intra-frames
312 // Allocating them unconditionally for decoders also means
313 // that we don't need to reinitialize when e.g. h263_aic changes.
314
315 // y_size and therefore yc_size is always odd; allocate one element
316 // more for each encoder slice in order to be able to align each slice's
317 // dc_val to four in order to use aligned stores when cleaning dc_val.
318 742 allslice_yc_size += s->encoding * nb_slices;
319
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 742 times.
742 if (!FF_ALLOC_TYPED_ARRAY(s->dc_val_base, allslice_yc_size))
320 return AVERROR(ENOMEM);
321 742 s->dc_val = s->dc_val_base + s->b8_stride + 1;
322
2/2
✓ Branch 0 taken 3230429 times.
✓ Branch 1 taken 742 times.
3231171 for (size_t i = 0; i < allslice_yc_size; ++i)
323 3230429 s->dc_val_base[i] = 1024;
324 }
325
326 // Note the + 1 is for a quicker MPEG-4 slice_end detection
327
1/2
✓ Branch 1 taken 871 times.
✗ Branch 2 not taken.
871 if (!(s->mbskip_table = av_mallocz(mb_array_size + 2)) ||
328 /* which mb is an intra block, init macroblock skip table */
329
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 871 times.
871 !(s->mbintra_table = av_mallocz(mb_array_size)))
330 return AVERROR(ENOMEM);
331
332
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 871 times.
871 ALLOC_POOL(qscale_table, mv_table_size, 0);
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 871 times.
871 ALLOC_POOL(mb_type, mv_table_size * sizeof(uint32_t), 0);
334
335
4/4
✓ Branch 0 taken 387 times.
✓ Branch 1 taken 484 times.
✓ Branch 2 taken 275 times.
✓ Branch 3 taken 112 times.
871 if (s->out_format == FMT_H263 || s->encoding ||
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 275 times.
275 (s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) {
337 596 const int b8_array_size = s->b8_stride * mb_height * 2;
338 596 int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
339 596 int ref_index_size = 4 * mb_array_size;
340
341 /* FIXME: The output of H.263 with OBMC depends upon
342 * the earlier content of the buffer; therefore we set
343 * the flags to always reset returned buffers here. */
344
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
596 ALLOC_POOL(motion_val, mv_size, AV_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME);
345
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 596 times.
596 ALLOC_POOL(ref_index, ref_index_size, 0);
346 }
347 #undef ALLOC_POOL
348 871 pools->alloc_mb_width = s->mb_width;
349 871 pools->alloc_mb_height = mb_height;
350 871 pools->alloc_mb_stride = s->mb_stride;
351
352
2/2
✓ Branch 0 taken 655 times.
✓ Branch 1 taken 216 times.
871 return !CONFIG_MPEGVIDEODEC || s->encoding ? 0 : ff_mpeg_er_init(s);
353 }
354
355 /**
356 * init common structure for both encoder and decoder.
357 * this assumes that some variables like width/height are already set
358 */
359 822 av_cold int ff_mpv_common_init(MpegEncContext *s)
360 {
361 int ret;
362
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 822 times.
822 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
364 av_log(s->avctx, AV_LOG_ERROR,
365 "decoding to AV_PIX_FMT_NONE is not supported.\n");
366 return AVERROR(EINVAL);
367 }
368
369
4/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 810 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 810 times.
1632 if ((s->width || s->height) &&
370 810 av_image_check_size(s->width, s->height, 0, s->avctx))
371 return AVERROR(EINVAL);
372
373 822 dsp_init(s);
374
375 /* set chroma shifts */
376 822 ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
377 &s->chroma_x_shift,
378 &s->chroma_y_shift);
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 822 times.
822 if (ret)
380 return ret;
381
382
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 822 times.
822 if ((ret = ff_mpv_init_context_frame(s)))
383 goto fail;
384
385 822 s->context_initialized = 1;
386 822 s->thread_context[0] = s;
387
388 // if (s->width && s->height) {
389
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 216 times.
822 if (!s->encoding) {
390 606 ret = ff_mpv_init_duplicate_contexts(s);
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (ret < 0)
392 goto fail;
393 }
394 // }
395
396 822 return 0;
397 fail:
398 ff_mpv_common_end(s);
399 return ret;
400 }
401
402 945 av_cold void ff_mpv_free_context_frame(MpegEncContext *s)
403 {
404 945 free_duplicate_contexts(s);
405
406 945 free_buffer_pools(&s->buffer_pools);
407 945 av_freep(&s->p_field_mv_table_base);
408
2/2
✓ Branch 0 taken 1890 times.
✓ Branch 1 taken 945 times.
2835 for (int i = 0; i < 2; i++)
409
2/2
✓ Branch 0 taken 3780 times.
✓ Branch 1 taken 1890 times.
5670 for (int j = 0; j < 2; j++)
410 3780 s->p_field_mv_table[i][j] = NULL;
411
412 945 av_freep(&s->ac_val_base);
413 945 av_freep(&s->dc_val_base);
414 945 av_freep(&s->coded_block_base);
415 945 av_freep(&s->mbintra_table);
416 945 av_freep(&s->cbp_table);
417 945 av_freep(&s->pred_dir_table);
418
419 945 av_freep(&s->mbskip_table);
420
421 945 av_freep(&s->er.error_status_table);
422 945 av_freep(&s->er.er_temp_buffer);
423 945 av_freep(&s->mb_index2xy);
424
425 945 s->linesize = s->uvlinesize = 0;
426 945 }
427
428 896 av_cold void ff_mpv_common_end(MpegEncContext *s)
429 {
430 896 ff_mpv_free_context_frame(s);
431
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 866 times.
896 if (s->slice_context_count > 1)
432 30 s->slice_context_count = 1;
433
434 896 ff_mpv_unref_picture(&s->last_pic);
435 896 ff_mpv_unref_picture(&s->cur_pic);
436 896 ff_mpv_unref_picture(&s->next_pic);
437
438 896 s->context_initialized = 0;
439 896 s->context_reinit = 0;
440 896 s->linesize = s->uvlinesize = 0;
441 896 }
442
443
444 /**
445 * Clean dc, ac for the current non-intra MB.
446 */
447 393855 void ff_clean_intra_table_entries(MpegEncContext *s)
448 {
449 393855 int wrap = s->b8_stride;
450 393855 int xy = s->block_index[0];
451 /* chroma */
452 393855 unsigned uxy = s->block_index[4];
453 393855 unsigned vxy = s->block_index[5];
454 393855 int16_t *dc_val = s->dc_val;
455
456 393855 AV_WN32A(dc_val + xy, 1024 << 16 | 1024);
457 393855 AV_WN32 (dc_val + xy + wrap, 1024 << 16 | 1024);
458 393855 dc_val[uxy] =
459 393855 dc_val[vxy] = 1024;
460 /* ac pred */
461 393855 int16_t (*ac_val)[16] = s->ac_val;
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 393855 times.
393855 av_assume(!((uintptr_t)ac_val & 0xF));
463 // Don't reset the upper-left luma block, as it will only ever be
464 // referenced by blocks from the same macroblock.
465 393855 memset(ac_val[xy + 1], 0, sizeof(*ac_val));
466 393855 memset(ac_val[xy + wrap], 0, 2 * sizeof(*ac_val));
467 /* ac pred */
468 393855 memset(ac_val[uxy], 0, sizeof(*ac_val));
469 393855 memset(ac_val[vxy], 0, sizeof(*ac_val));
470 393855 }
471
472 884061 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
473 884061 const int linesize = s->cur_pic.linesize[0]; //not s->linesize as this would be wrong for field pics
474 884061 const int uvlinesize = s->cur_pic.linesize[1];
475
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 884031 times.
884061 const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
476 884061 const int height_of_mb = 4 - s->avctx->lowres;
477
478 884061 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
479 884061 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
480 884061 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
481 884061 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
482 884061 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
483 884061 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;
484 //block_index is not used by mpeg2, so it is not affected by chroma_format
485
486 884061 s->dest[0] = s->cur_pic.data[0] + (int)((s->mb_x - 1U) << width_of_mb);
487 884061 s->dest[1] = s->cur_pic.data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
488 884061 s->dest[2] = s->cur_pic.data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
489
490
2/2
✓ Branch 0 taken 842309 times.
✓ Branch 1 taken 41752 times.
884061 if (s->picture_structure == PICT_FRAME) {
491 842309 s->dest[0] += s->mb_y * linesize << height_of_mb;
492 842309 s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
493 842309 s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
494 } else {
495 41752 s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
496 41752 s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
497 41752 s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
498 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
499 }
500 884061 }
501
502 /**
503 * set qscale and update qscale dependent variables.
504 */
505 1467058 void ff_set_qscale(MpegEncContext * s, int qscale)
506 {
507
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1467057 times.
1467058 if (qscale < 1)
508 1 qscale = 1;
509
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1467028 times.
1467057 else if (qscale > 31)
510 29 qscale = 31;
511
512 1467058 s->qscale = qscale;
513 1467058 s->chroma_qscale= s->chroma_qscale_table[qscale];
514
515 1467058 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
516 1467058 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
517 1467058 }
518