FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo.c
Date: 2022-07-04 19:11:22
Exec Total Coverage
Lines: 804 998 80.6%
Branches: 365 530 68.9%

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 "config_components.h"
31
32 #include "libavutil/attributes.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/imgutils.h"
35 #include "libavutil/internal.h"
36
37 #include "avcodec.h"
38 #include "blockdsp.h"
39 #include "h264chroma.h"
40 #include "idctdsp.h"
41 #include "mathops.h"
42 #include "mpeg_er.h"
43 #include "mpegutils.h"
44 #include "mpegvideo.h"
45 #include "mpeg4videodec.h"
46 #include "mpegvideodata.h"
47 #include "qpeldsp.h"
48 #include "threadframe.h"
49 #include "wmv2dec.h"
50 #include <limits.h>
51
52 126126 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
53 int16_t *block, int n, int qscale)
54 {
55 int i, level, nCoeffs;
56 const uint16_t *quant_matrix;
57
58 126126 nCoeffs= s->block_last_index[n];
59
60
2/2
✓ Branch 0 taken 84084 times.
✓ Branch 1 taken 42042 times.
126126 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
61 /* XXX: only MPEG-1 */
62 126126 quant_matrix = s->intra_matrix;
63
2/2
✓ Branch 0 taken 2915949 times.
✓ Branch 1 taken 126126 times.
3042075 for(i=1;i<=nCoeffs;i++) {
64 2915949 int j= s->intra_scantable.permutated[i];
65 2915949 level = block[j];
66
2/2
✓ Branch 0 taken 1215436 times.
✓ Branch 1 taken 1700513 times.
2915949 if (level) {
67
2/2
✓ Branch 0 taken 604537 times.
✓ Branch 1 taken 610899 times.
1215436 if (level < 0) {
68 604537 level = -level;
69 604537 level = (int)(level * qscale * quant_matrix[j]) >> 3;
70 604537 level = (level - 1) | 1;
71 604537 level = -level;
72 } else {
73 610899 level = (int)(level * qscale * quant_matrix[j]) >> 3;
74 610899 level = (level - 1) | 1;
75 }
76 1215436 block[j] = level;
77 }
78 }
79 126126 }
80
81 285535 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
82 int16_t *block, int n, int qscale)
83 {
84 int i, level, nCoeffs;
85 const uint16_t *quant_matrix;
86
87 285535 nCoeffs= s->block_last_index[n];
88
89 285535 quant_matrix = s->inter_matrix;
90
2/2
✓ Branch 0 taken 9217310 times.
✓ Branch 1 taken 285535 times.
9502845 for(i=0; i<=nCoeffs; i++) {
91 9217310 int j= s->intra_scantable.permutated[i];
92 9217310 level = block[j];
93
2/2
✓ Branch 0 taken 2101549 times.
✓ Branch 1 taken 7115761 times.
9217310 if (level) {
94
2/2
✓ Branch 0 taken 1052132 times.
✓ Branch 1 taken 1049417 times.
2101549 if (level < 0) {
95 1052132 level = -level;
96 1052132 level = (((level << 1) + 1) * qscale *
97 1052132 ((int) (quant_matrix[j]))) >> 4;
98 1052132 level = (level - 1) | 1;
99 1052132 level = -level;
100 } else {
101 1049417 level = (((level << 1) + 1) * qscale *
102 1049417 ((int) (quant_matrix[j]))) >> 4;
103 1049417 level = (level - 1) | 1;
104 }
105 2101549 block[j] = level;
106 }
107 }
108 285535 }
109
110 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
111 int16_t *block, int n, int qscale)
112 {
113 int i, level, nCoeffs;
114 const uint16_t *quant_matrix;
115
116 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
117 else qscale <<= 1;
118
119 if(s->alternate_scan) nCoeffs= 63;
120 else nCoeffs= s->block_last_index[n];
121
122 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
123 quant_matrix = s->intra_matrix;
124 for(i=1;i<=nCoeffs;i++) {
125 int j= s->intra_scantable.permutated[i];
126 level = block[j];
127 if (level) {
128 if (level < 0) {
129 level = -level;
130 level = (int)(level * qscale * quant_matrix[j]) >> 4;
131 level = -level;
132 } else {
133 level = (int)(level * qscale * quant_matrix[j]) >> 4;
134 }
135 block[j] = level;
136 }
137 }
138 }
139
140 1131872 static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s,
141 int16_t *block, int n, int qscale)
142 {
143 int i, level, nCoeffs;
144 const uint16_t *quant_matrix;
145 1131872 int sum=-1;
146
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1131872 times.
1131872 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
148 1131872 else qscale <<= 1;
149
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1131872 times.
1131872 if(s->alternate_scan) nCoeffs= 63;
151 1131872 else nCoeffs= s->block_last_index[n];
152
153
2/2
✓ Branch 0 taken 702568 times.
✓ Branch 1 taken 429304 times.
1131872 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
154 1131872 sum += block[0];
155 1131872 quant_matrix = s->intra_matrix;
156
2/2
✓ Branch 0 taken 18356758 times.
✓ Branch 1 taken 1131872 times.
19488630 for(i=1;i<=nCoeffs;i++) {
157 18356758 int j= s->intra_scantable.permutated[i];
158 18356758 level = block[j];
159
2/2
✓ Branch 0 taken 9025237 times.
✓ Branch 1 taken 9331521 times.
18356758 if (level) {
160
2/2
✓ Branch 0 taken 4537630 times.
✓ Branch 1 taken 4487607 times.
9025237 if (level < 0) {
161 4537630 level = -level;
162 4537630 level = (int)(level * qscale * quant_matrix[j]) >> 4;
163 4537630 level = -level;
164 } else {
165 4487607 level = (int)(level * qscale * quant_matrix[j]) >> 4;
166 }
167 9025237 block[j] = level;
168 9025237 sum+=level;
169 }
170 }
171 1131872 block[63]^=sum&1;
172 1131872 }
173
174 2598777 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
175 int16_t *block, int n, int qscale)
176 {
177 int i, level, nCoeffs;
178 const uint16_t *quant_matrix;
179 2598777 int sum=-1;
180
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2598777 times.
2598777 if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
182 2598777 else qscale <<= 1;
183
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2598777 times.
2598777 if(s->alternate_scan) nCoeffs= 63;
185 2598777 else nCoeffs= s->block_last_index[n];
186
187 2598777 quant_matrix = s->inter_matrix;
188
2/2
✓ Branch 0 taken 63010806 times.
✓ Branch 1 taken 2598777 times.
65609583 for(i=0; i<=nCoeffs; i++) {
189 63010806 int j= s->intra_scantable.permutated[i];
190 63010806 level = block[j];
191
2/2
✓ Branch 0 taken 22238600 times.
✓ Branch 1 taken 40772206 times.
63010806 if (level) {
192
2/2
✓ Branch 0 taken 11146218 times.
✓ Branch 1 taken 11092382 times.
22238600 if (level < 0) {
193 11146218 level = -level;
194 11146218 level = (((level << 1) + 1) * qscale *
195 11146218 ((int) (quant_matrix[j]))) >> 5;
196 11146218 level = -level;
197 } else {
198 11092382 level = (((level << 1) + 1) * qscale *
199 11092382 ((int) (quant_matrix[j]))) >> 5;
200 }
201 22238600 block[j] = level;
202 22238600 sum+=level;
203 }
204 }
205 2598777 block[63]^=sum&1;
206 2598777 }
207
208 3424176 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
209 int16_t *block, int n, int qscale)
210 {
211 int i, level, qmul, qadd;
212 int nCoeffs;
213
214 av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
215
216 3424176 qmul = qscale << 1;
217
218
2/2
✓ Branch 0 taken 3250980 times.
✓ Branch 1 taken 173196 times.
3424176 if (!s->h263_aic) {
219
2/2
✓ Branch 0 taken 2167320 times.
✓ Branch 1 taken 1083660 times.
3250980 block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
220 3250980 qadd = (qscale - 1) | 1;
221 }else{
222 173196 qadd = 0;
223 }
224
2/2
✓ Branch 0 taken 68304 times.
✓ Branch 1 taken 3355872 times.
3424176 if(s->ac_pred)
225 68304 nCoeffs=63;
226 else
227 3355872 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
228
229
2/2
✓ Branch 0 taken 118201238 times.
✓ Branch 1 taken 3424176 times.
121625414 for(i=1; i<=nCoeffs; i++) {
230 118201238 level = block[i];
231
2/2
✓ Branch 0 taken 36282569 times.
✓ Branch 1 taken 81918669 times.
118201238 if (level) {
232
2/2
✓ Branch 0 taken 18169839 times.
✓ Branch 1 taken 18112730 times.
36282569 if (level < 0) {
233 18169839 level = level * qmul - qadd;
234 } else {
235 18112730 level = level * qmul + qadd;
236 }
237 36282569 block[i] = level;
238 }
239 }
240 3424176 }
241
242 6060873 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
243 int16_t *block, int n, int qscale)
244 {
245 int i, level, qmul, qadd;
246 int nCoeffs;
247
248 av_assert2(s->block_last_index[n]>=0);
249
250 6060873 qadd = (qscale - 1) | 1;
251 6060873 qmul = qscale << 1;
252
253 6060873 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
254
255
2/2
✓ Branch 0 taken 231135804 times.
✓ Branch 1 taken 6060873 times.
237196677 for(i=0; i<=nCoeffs; i++) {
256 231135804 level = block[i];
257
2/2
✓ Branch 0 taken 48453077 times.
✓ Branch 1 taken 182682727 times.
231135804 if (level) {
258
2/2
✓ Branch 0 taken 24150126 times.
✓ Branch 1 taken 24302951 times.
48453077 if (level < 0) {
259 24150126 level = level * qmul - qadd;
260 } else {
261 24302951 level = level * qmul + qadd;
262 }
263 48453077 block[i] = level;
264 }
265 }
266 6060873 }
267
268
269 static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
270 {
271 while(h--)
272 memset(dst + h*linesize, 128, 16);
273 }
274
275 static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
276 {
277 while(h--)
278 memset(dst + h*linesize, 128, 8);
279 }
280
281 /* init common dct for both encoder and decoder */
282 762 static av_cold int dct_init(MpegEncContext *s)
283 {
284 762 ff_blockdsp_init(&s->bdsp, s->avctx);
285 762 ff_h264chroma_init(&s->h264chroma, 8); //for lowres
286 762 ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
287 762 ff_mpegvideodsp_init(&s->mdsp);
288 762 ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
289
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 762 times.
762 if (s->avctx->debug & FF_DEBUG_NOMC) {
291 int i;
292 for (i=0; i<4; i++) {
293 s->hdsp.avg_pixels_tab[0][i] = gray16;
294 s->hdsp.put_pixels_tab[0][i] = gray16;
295 s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
296
297 s->hdsp.avg_pixels_tab[1][i] = gray8;
298 s->hdsp.put_pixels_tab[1][i] = gray8;
299 s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
300 }
301 }
302
303 762 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
304 762 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
305 762 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
306 762 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
307 762 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
308
2/2
✓ Branch 0 taken 548 times.
✓ Branch 1 taken 214 times.
762 if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
309 548 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
310 762 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
311
312 #if HAVE_INTRINSICS_NEON
313 ff_mpv_common_init_neon(s);
314 #endif
315
316 #if ARCH_ALPHA
317 ff_mpv_common_init_axp(s);
318 #elif ARCH_ARM
319 ff_mpv_common_init_arm(s);
320 #elif ARCH_PPC
321 ff_mpv_common_init_ppc(s);
322 #elif ARCH_X86
323 762 ff_mpv_common_init_x86(s);
324 #elif ARCH_MIPS
325 ff_mpv_common_init_mips(s);
326 #endif
327
328 762 return 0;
329 }
330
331 1107 av_cold void ff_mpv_idct_init(MpegEncContext *s)
332 {
333
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 875 times.
1107 if (s->codec_id == AV_CODEC_ID_MPEG4)
334 232 s->idsp.mpeg4_studio_profile = s->studio_profile;
335 1107 ff_idctdsp_init(&s->idsp, s->avctx);
336
337 /* load & permutate scantables
338 * note: only wmv uses different ones
339 */
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1107 times.
1107 if (s->alternate_scan) {
341 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
342 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
343 } else {
344 1107 ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
345 1107 ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
346 }
347 1107 ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_alternate_horizontal_scan);
348 1107 ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_alternate_vertical_scan);
349 1107 }
350
351 847 static int init_duplicate_context(MpegEncContext *s)
352 {
353 847 int y_size = s->b8_stride * (2 * s->mb_height + 1);
354 847 int c_size = s->mb_stride * (s->mb_height + 1);
355 847 int yc_size = y_size + 2 * c_size;
356 int i;
357
358
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 700 times.
847 if (s->mb_height & 1)
359 147 yc_size += 2*s->b8_stride + 2*s->mb_stride;
360
361
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 648 times.
847 if (s->encoding) {
362
1/2
✓ Branch 1 taken 199 times.
✗ Branch 2 not taken.
199 if (!FF_ALLOCZ_TYPED_ARRAY(s->me.map, ME_MAP_SIZE) ||
363
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 199 times.
199 !FF_ALLOCZ_TYPED_ARRAY(s->me.score_map, ME_MAP_SIZE))
364 return AVERROR(ENOMEM);
365
366
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 195 times.
199 if (s->noise_reduction) {
367
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_error_sum, 2))
368 return AVERROR(ENOMEM);
369 }
370 }
371
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 847 times.
847 if (!FF_ALLOCZ_TYPED_ARRAY(s->blocks, 2))
372 return AVERROR(ENOMEM);
373 847 s->block = s->blocks[0];
374
375
2/2
✓ Branch 0 taken 10164 times.
✓ Branch 1 taken 847 times.
11011 for (i = 0; i < 12; i++) {
376 10164 s->pblocks[i] = &s->block[i];
377 }
378
379
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 843 times.
847 if (s->avctx->codec_tag == AV_RL32("VCR2")) {
380 // exchange uv
381 4 FFSWAP(void *, s->pblocks[4], s->pblocks[5]);
382 }
383
384
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 379 times.
847 if (s->out_format == FMT_H263) {
385 /* ac values */
386
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 468 times.
468 if (!FF_ALLOCZ_TYPED_ARRAY(s->ac_val_base, yc_size))
387 return AVERROR(ENOMEM);
388 468 s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
389 468 s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
390 468 s->ac_val[2] = s->ac_val[1] + c_size;
391 }
392
393 847 return 0;
394 }
395
396 803 int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
397 {
398 803 int nb_slices = s->slice_context_count, ret;
399
400 /* We initialize the copies before the original so that
401 * fields allocated in init_duplicate_context are NULL after
402 * copying. This prevents double-frees upon allocation error. */
403
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 803 times.
847 for (int i = 1; i < nb_slices; i++) {
404 44 s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (!s->thread_context[i])
406 return AVERROR(ENOMEM);
407
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
44 if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
408 return ret;
409 44 s->thread_context[i]->start_mb_y =
410 44 (s->mb_height * (i ) + nb_slices / 2) / nb_slices;
411 44 s->thread_context[i]->end_mb_y =
412 44 (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
413 }
414 803 s->start_mb_y = 0;
415 819 s->end_mb_y = nb_slices > 1 ? (s->mb_height + nb_slices / 2) / nb_slices
416
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 787 times.
803 : s->mb_height;
417 803 return init_duplicate_context(s);
418 }
419
420 868 static void free_duplicate_context(MpegEncContext *s)
421 {
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 868 times.
868 if (!s)
423 return;
424
425 868 av_freep(&s->sc.edge_emu_buffer);
426 868 av_freep(&s->me.scratchpad);
427 868 s->me.temp =
428 868 s->sc.rd_scratchpad =
429 868 s->sc.b_scratchpad =
430 868 s->sc.obmc_scratchpad = NULL;
431
432 868 av_freep(&s->dct_error_sum);
433 868 av_freep(&s->me.map);
434 868 av_freep(&s->me.score_map);
435 868 av_freep(&s->blocks);
436 868 av_freep(&s->ac_val_base);
437 868 s->block = NULL;
438 }
439
440 824 static void free_duplicate_contexts(MpegEncContext *s)
441 {
442
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 824 times.
868 for (int i = 1; i < s->slice_context_count; i++) {
443 44 free_duplicate_context(s->thread_context[i]);
444 44 av_freep(&s->thread_context[i]);
445 }
446 824 free_duplicate_context(s);
447 824 }
448
449 10912 static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
450 {
451 #define COPY(a) bak->a = src->a
452 10912 COPY(sc.edge_emu_buffer);
453 10912 COPY(me.scratchpad);
454 10912 COPY(me.temp);
455 10912 COPY(sc.rd_scratchpad);
456 10912 COPY(sc.b_scratchpad);
457 10912 COPY(sc.obmc_scratchpad);
458 10912 COPY(me.map);
459 10912 COPY(me.score_map);
460 10912 COPY(blocks);
461 10912 COPY(block);
462 10912 COPY(start_mb_y);
463 10912 COPY(end_mb_y);
464 10912 COPY(me.map_generation);
465 10912 COPY(pb);
466 10912 COPY(dct_error_sum);
467 10912 COPY(dct_count[0]);
468 10912 COPY(dct_count[1]);
469 10912 COPY(ac_val_base);
470 10912 COPY(ac_val[0]);
471 10912 COPY(ac_val[1]);
472 10912 COPY(ac_val[2]);
473 #undef COPY
474 10912 }
475
476 5456 int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
477 {
478 MpegEncContext bak;
479 int i, ret;
480 // FIXME copy only needed parts
481 5456 backup_duplicate_context(&bak, dst);
482 5456 memcpy(dst, src, sizeof(MpegEncContext));
483 5456 backup_duplicate_context(dst, &bak);
484
2/2
✓ Branch 0 taken 65472 times.
✓ Branch 1 taken 5456 times.
70928 for (i = 0; i < 12; i++) {
485 65472 dst->pblocks[i] = &dst->block[i];
486 }
487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5456 times.
5456 if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
488 // exchange uv
489 FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
490 }
491
3/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5412 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
5500 if (!dst->sc.edge_emu_buffer &&
492 44 (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
493 44 &dst->sc, dst->linesize)) < 0) {
494 av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
495 "scratch buffers.\n");
496 return ret;
497 }
498 5456 return 0;
499 }
500
501 /**
502 * Set the given MpegEncContext to common defaults
503 * (same for encoding and decoding).
504 * The changed fields will not depend upon the
505 * prior state of the MpegEncContext.
506 */
507 789 void ff_mpv_common_defaults(MpegEncContext *s)
508 {
509 789 s->y_dc_scale_table =
510 789 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
511 789 s->chroma_qscale_table = ff_default_chroma_qscale_table;
512 789 s->progressive_frame = 1;
513 789 s->progressive_sequence = 1;
514 789 s->picture_structure = PICT_FRAME;
515
516 789 s->coded_picture_number = 0;
517 789 s->picture_number = 0;
518
519 789 s->f_code = 1;
520 789 s->b_code = 1;
521
522 789 s->slice_context_count = 1;
523 789 }
524
525 803 int ff_mpv_init_context_frame(MpegEncContext *s)
526 {
527 int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
528
529 803 s->mb_width = (s->width + 15) / 16;
530 803 s->mb_stride = s->mb_width + 1;
531 803 s->b8_stride = s->mb_width * 2 + 1;
532 803 mb_array_size = s->mb_height * s->mb_stride;
533 803 mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
534
535 /* set default edge pos, will be overridden
536 * in decode_header if needed */
537 803 s->h_edge_pos = s->mb_width * 16;
538 803 s->v_edge_pos = s->mb_height * 16;
539
540 803 s->mb_num = s->mb_width * s->mb_height;
541
542 803 s->block_wrap[0] =
543 803 s->block_wrap[1] =
544 803 s->block_wrap[2] =
545 803 s->block_wrap[3] = s->b8_stride;
546 803 s->block_wrap[4] =
547 803 s->block_wrap[5] = s->mb_stride;
548
549 803 y_size = s->b8_stride * (2 * s->mb_height + 1);
550 803 c_size = s->mb_stride * (s->mb_height + 1);
551 803 yc_size = y_size + 2 * c_size;
552
553
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 657 times.
803 if (s->mb_height & 1)
554 146 yc_size += 2*s->b8_stride + 2*s->mb_stride;
555
556
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 803 times.
803 if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_index2xy, s->mb_num + 1))
557 return AVERROR(ENOMEM);
558
2/2
✓ Branch 0 taken 14824 times.
✓ Branch 1 taken 803 times.
15627 for (y = 0; y < s->mb_height; y++)
559
2/2
✓ Branch 0 taken 447105 times.
✓ Branch 1 taken 14824 times.
461929 for (x = 0; x < s->mb_width; x++)
560 447105 s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
561
562 803 s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
563
564
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 616 times.
803 if (s->encoding) {
565 /* Allocate MV tables */
566
1/2
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
187 if (!FF_ALLOCZ_TYPED_ARRAY(s->p_mv_table_base, mv_table_size) ||
567
1/2
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
187 !FF_ALLOCZ_TYPED_ARRAY(s->b_forw_mv_table_base, mv_table_size) ||
568
1/2
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
187 !FF_ALLOCZ_TYPED_ARRAY(s->b_back_mv_table_base, mv_table_size) ||
569
1/2
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
187 !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_forw_mv_table_base, mv_table_size) ||
570
1/2
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
187 !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_back_mv_table_base, mv_table_size) ||
571
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 187 times.
187 !FF_ALLOCZ_TYPED_ARRAY(s->b_direct_mv_table_base, mv_table_size))
572 return AVERROR(ENOMEM);
573 187 s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
574 187 s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
575 187 s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
576 187 s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
577 187 s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
578 187 s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
579
580 /* Allocate MB type table */
581
1/2
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
187 if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_type, mb_array_size) ||
582
1/2
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
187 !FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size) ||
583
1/2
✓ Branch 1 taken 187 times.
✗ Branch 2 not taken.
187 !FF_ALLOC_TYPED_ARRAY (s->cplx_tab, mb_array_size) ||
584
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 187 times.
187 !FF_ALLOC_TYPED_ARRAY (s->bits_tab, mb_array_size))
585 return AVERROR(ENOMEM);
586
587 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
588
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 60 times.
187 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
589
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 111 times.
127 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
590 int16_t (*tmp1)[2];
591 uint8_t *tmp2;
592
1/2
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
76 if (!(tmp1 = ALLOCZ_ARRAYS(s->b_field_mv_table_base, 8, mv_table_size)) ||
593
1/2
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
76 !(tmp2 = ALLOCZ_ARRAYS(s->b_field_select_table[0][0], 2 * 4, mv_table_size)) ||
594
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
76 !ALLOCZ_ARRAYS(s->p_field_select_table[0], 2 * 2, mv_table_size))
595 return AVERROR(ENOMEM);
596
597 76 s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size;
598 76 tmp1 += s->mb_stride + 1;
599
600
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 76 times.
228 for (int i = 0; i < 2; i++) {
601
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 152 times.
456 for (int j = 0; j < 2; j++) {
602
2/2
✓ Branch 0 taken 608 times.
✓ Branch 1 taken 304 times.
912 for (int k = 0; k < 2; k++) {
603 608 s->b_field_mv_table[i][j][k] = tmp1;
604 608 tmp1 += mv_table_size;
605 }
606 304 s->b_field_select_table[i][j] = tmp2;
607 304 tmp2 += 2 * mv_table_size;
608 }
609 }
610 }
611 }
612
613
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 256 times.
803 if (s->codec_id == AV_CODEC_ID_MPEG4 ||
614
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 531 times.
547 (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
615 int16_t (*tmp)[2];
616 /* interlaced direct mode decoding tables */
617
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 272 times.
272 if (!(tmp = ALLOCZ_ARRAYS(s->p_field_mv_table_base, 4, mv_table_size)))
618 return AVERROR(ENOMEM);
619 272 tmp += s->mb_stride + 1;
620
2/2
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 272 times.
816 for (int i = 0; i < 2; i++) {
621
2/2
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 544 times.
1632 for (int j = 0; j < 2; j++) {
622 1088 s->p_field_mv_table[i][j] = tmp;
623 1088 tmp += mv_table_size;
624 }
625 }
626 }
627
628
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 339 times.
803 if (s->out_format == FMT_H263) {
629 /* cbp values, cbp, ac_pred, pred_dir */
630
1/2
✓ Branch 1 taken 464 times.
✗ Branch 2 not taken.
464 if (!(s->coded_block_base = av_mallocz(y_size + (s->mb_height&1)*2*s->b8_stride)) ||
631
1/2
✓ Branch 1 taken 464 times.
✗ Branch 2 not taken.
464 !(s->cbp_table = av_mallocz(mb_array_size)) ||
632
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 464 times.
464 !(s->pred_dir_table = av_mallocz(mb_array_size)))
633 return AVERROR(ENOMEM);
634 464 s->coded_block = s->coded_block_base + s->b8_stride + 1;
635 }
636
637
6/6
✓ Branch 0 taken 443 times.
✓ Branch 1 taken 360 times.
✓ Branch 2 taken 430 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 326 times.
✓ Branch 5 taken 104 times.
803 if (s->h263_pred || s->h263_plus || !s->encoding) {
638 /* dc values */
639 // MN: we need these for error resilience of intra-frames
640
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 699 times.
699 if (!FF_ALLOCZ_TYPED_ARRAY(s->dc_val_base, yc_size))
641 return AVERROR(ENOMEM);
642 699 s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
643 699 s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
644 699 s->dc_val[2] = s->dc_val[1] + c_size;
645
2/2
✓ Branch 0 taken 2564313 times.
✓ Branch 1 taken 699 times.
2565012 for (i = 0; i < yc_size; i++)
646 2564313 s->dc_val_base[i] = 1024;
647 }
648
649 /* which mb is an intra block, init macroblock skip table */
650
1/2
✓ Branch 1 taken 803 times.
✗ Branch 2 not taken.
803 if (!(s->mbintra_table = av_mallocz(mb_array_size)) ||
651 // Note the + 1 is for a quicker MPEG-4 slice_end detection
652
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 803 times.
803 !(s->mbskip_table = av_mallocz(mb_array_size + 2)))
653 return AVERROR(ENOMEM);
654 803 memset(s->mbintra_table, 1, mb_array_size);
655
656
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 187 times.
803 return !CONFIG_MPEGVIDEODEC || s->encoding ? 0 : ff_mpeg_er_init(s);
657 }
658
659 762 static void clear_context(MpegEncContext *s)
660 {
661 int i, j, k;
662
663 762 memset(&s->next_picture, 0, sizeof(s->next_picture));
664 762 memset(&s->last_picture, 0, sizeof(s->last_picture));
665 762 memset(&s->current_picture, 0, sizeof(s->current_picture));
666 762 memset(&s->new_picture, 0, sizeof(s->new_picture));
667
668 762 memset(s->thread_context, 0, sizeof(s->thread_context));
669
670 762 s->me.map = NULL;
671 762 s->me.score_map = NULL;
672 762 s->dct_error_sum = NULL;
673 762 s->block = NULL;
674 762 s->blocks = NULL;
675 762 memset(s->pblocks, 0, sizeof(s->pblocks));
676 762 s->ac_val_base = NULL;
677 762 s->ac_val[0] =
678 762 s->ac_val[1] =
679 762 s->ac_val[2] =NULL;
680 762 s->sc.edge_emu_buffer = NULL;
681 762 s->me.scratchpad = NULL;
682 762 s->me.temp =
683 762 s->sc.rd_scratchpad =
684 762 s->sc.b_scratchpad =
685 762 s->sc.obmc_scratchpad = NULL;
686
687
688 762 s->bitstream_buffer = NULL;
689 762 s->allocated_bitstream_buffer_size = 0;
690 762 s->picture = NULL;
691 762 s->mb_type = NULL;
692 762 s->p_mv_table_base = NULL;
693 762 s->b_forw_mv_table_base = NULL;
694 762 s->b_back_mv_table_base = NULL;
695 762 s->b_bidir_forw_mv_table_base = NULL;
696 762 s->b_bidir_back_mv_table_base = NULL;
697 762 s->b_direct_mv_table_base = NULL;
698 762 s->p_mv_table = NULL;
699 762 s->b_forw_mv_table = NULL;
700 762 s->b_back_mv_table = NULL;
701 762 s->b_bidir_forw_mv_table = NULL;
702 762 s->b_bidir_back_mv_table = NULL;
703 762 s->b_direct_mv_table = NULL;
704 762 s->b_field_mv_table_base = NULL;
705 762 s->p_field_mv_table_base = NULL;
706
2/2
✓ Branch 0 taken 1524 times.
✓ Branch 1 taken 762 times.
2286 for (i = 0; i < 2; i++) {
707
2/2
✓ Branch 0 taken 3048 times.
✓ Branch 1 taken 1524 times.
4572 for (j = 0; j < 2; j++) {
708
2/2
✓ Branch 0 taken 6096 times.
✓ Branch 1 taken 3048 times.
9144 for (k = 0; k < 2; k++) {
709 6096 s->b_field_mv_table[i][j][k] = NULL;
710 }
711 3048 s->b_field_select_table[i][j] = NULL;
712 3048 s->p_field_mv_table[i][j] = NULL;
713 }
714 1524 s->p_field_select_table[i] = NULL;
715 }
716
717 762 s->dc_val_base = NULL;
718 762 s->coded_block_base = NULL;
719 762 s->mbintra_table = NULL;
720 762 s->cbp_table = NULL;
721 762 s->pred_dir_table = NULL;
722
723 762 s->mbskip_table = NULL;
724
725 762 s->er.error_status_table = NULL;
726 762 s->er.er_temp_buffer = NULL;
727 762 s->mb_index2xy = NULL;
728 762 s->lambda_table = NULL;
729
730 762 s->cplx_tab = NULL;
731 762 s->bits_tab = NULL;
732 762 }
733
734 /**
735 * init common structure for both encoder and decoder.
736 * this assumes that some variables like width/height are already set
737 */
738 762 av_cold int ff_mpv_common_init(MpegEncContext *s)
739 {
740 int i, ret;
741 762 int nb_slices = (HAVE_THREADS &&
742 762 s->avctx->active_thread_type & FF_THREAD_SLICE) ?
743
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 746 times.
762 s->avctx->thread_count : 1;
744
745 762 clear_context(s);
746
747
4/4
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 575 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 175 times.
762 if (s->encoding && s->avctx->slices)
748 12 nb_slices = s->avctx->slices;
749
750
4/4
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 507 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 127 times.
762 if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
751 128 s->mb_height = (s->height + 31) / 32 * 2;
752 else
753 634 s->mb_height = (s->height + 15) / 16;
754
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 762 times.
762 if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
756 av_log(s->avctx, AV_LOG_ERROR,
757 "decoding to AV_PIX_FMT_NONE is not supported.\n");
758 return AVERROR(EINVAL);
759 }
760
761
4/6
✓ Branch 0 taken 762 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 746 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 16 times.
762 if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
762 int max_slices;
763 if (s->mb_height)
764 max_slices = FFMIN(MAX_THREADS, s->mb_height);
765 else
766 max_slices = MAX_THREADS;
767 av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
768 " reducing to %d\n", nb_slices, max_slices);
769 nb_slices = max_slices;
770 }
771
772
4/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 746 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 746 times.
1508 if ((s->width || s->height) &&
773 746 av_image_check_size(s->width, s->height, 0, s->avctx))
774 return AVERROR(EINVAL);
775
776 762 dct_init(s);
777
778 /* set chroma shifts */
779 762 ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
780 &s->chroma_x_shift,
781 &s->chroma_y_shift);
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 762 times.
762 if (ret)
783 return ret;
784
785
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 762 times.
762 if (!FF_ALLOCZ_TYPED_ARRAY(s->picture, MAX_PICTURE_COUNT))
786 return AVERROR(ENOMEM);
787
2/2
✓ Branch 0 taken 27432 times.
✓ Branch 1 taken 762 times.
28194 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
788 27432 s->picture[i].f = av_frame_alloc();
789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27432 times.
27432 if (!s->picture[i].f)
790 goto fail_nomem;
791 }
792
793
1/2
✓ Branch 1 taken 762 times.
✗ Branch 2 not taken.
762 if (!(s->next_picture.f = av_frame_alloc()) ||
794
1/2
✓ Branch 1 taken 762 times.
✗ Branch 2 not taken.
762 !(s->last_picture.f = av_frame_alloc()) ||
795
1/2
✓ Branch 1 taken 762 times.
✗ Branch 2 not taken.
762 !(s->current_picture.f = av_frame_alloc()) ||
796
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 762 times.
762 !(s->new_picture = av_frame_alloc()))
797 goto fail_nomem;
798
799
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 762 times.
762 if ((ret = ff_mpv_init_context_frame(s)))
800 goto fail;
801
802 #if FF_API_FLAG_TRUNCATED
803 762 s->parse_context.state = -1;
804 #endif
805
806 762 s->context_initialized = 1;
807 762 memset(s->thread_context, 0, sizeof(s->thread_context));
808 762 s->thread_context[0] = s;
809 762 s->slice_context_count = nb_slices;
810
811 // if (s->width && s->height) {
812 762 ret = ff_mpv_init_duplicate_contexts(s);
813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 762 times.
762 if (ret < 0)
814 goto fail;
815 // }
816
817 762 return 0;
818 fail_nomem:
819 ret = AVERROR(ENOMEM);
820 fail:
821 ff_mpv_common_end(s);
822 return ret;
823 }
824
825 824 void ff_mpv_free_context_frame(MpegEncContext *s)
826 {
827 int i, j, k;
828
829 824 free_duplicate_contexts(s);
830
831 824 av_freep(&s->mb_type);
832 824 av_freep(&s->p_mv_table_base);
833 824 av_freep(&s->b_forw_mv_table_base);
834 824 av_freep(&s->b_back_mv_table_base);
835 824 av_freep(&s->b_bidir_forw_mv_table_base);
836 824 av_freep(&s->b_bidir_back_mv_table_base);
837 824 av_freep(&s->b_direct_mv_table_base);
838 824 s->p_mv_table = NULL;
839 824 s->b_forw_mv_table = NULL;
840 824 s->b_back_mv_table = NULL;
841 824 s->b_bidir_forw_mv_table = NULL;
842 824 s->b_bidir_back_mv_table = NULL;
843 824 s->b_direct_mv_table = NULL;
844 824 av_freep(&s->b_field_mv_table_base);
845 824 av_freep(&s->b_field_select_table[0][0]);
846 824 av_freep(&s->p_field_mv_table_base);
847 824 av_freep(&s->p_field_select_table[0]);
848
2/2
✓ Branch 0 taken 1648 times.
✓ Branch 1 taken 824 times.
2472 for (i = 0; i < 2; i++) {
849
2/2
✓ Branch 0 taken 3296 times.
✓ Branch 1 taken 1648 times.
4944 for (j = 0; j < 2; j++) {
850
2/2
✓ Branch 0 taken 6592 times.
✓ Branch 1 taken 3296 times.
9888 for (k = 0; k < 2; k++) {
851 6592 s->b_field_mv_table[i][j][k] = NULL;
852 }
853 3296 s->b_field_select_table[i][j] = NULL;
854 3296 s->p_field_mv_table[i][j] = NULL;
855 }
856 1648 s->p_field_select_table[i] = NULL;
857 }
858
859 824 av_freep(&s->dc_val_base);
860 824 av_freep(&s->coded_block_base);
861 824 av_freep(&s->mbintra_table);
862 824 av_freep(&s->cbp_table);
863 824 av_freep(&s->pred_dir_table);
864
865 824 av_freep(&s->mbskip_table);
866
867 824 av_freep(&s->er.error_status_table);
868 824 av_freep(&s->er.er_temp_buffer);
869 824 av_freep(&s->mb_index2xy);
870 824 av_freep(&s->lambda_table);
871
872 824 av_freep(&s->cplx_tab);
873 824 av_freep(&s->bits_tab);
874
875 824 s->linesize = s->uvlinesize = 0;
876 824 }
877
878 /* init common structure for both encoder and decoder */
879 783 void ff_mpv_common_end(MpegEncContext *s)
880 {
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 783 times.
783 if (!s)
882 return;
883
884 783 ff_mpv_free_context_frame(s);
885
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 767 times.
783 if (s->slice_context_count > 1)
886 16 s->slice_context_count = 1;
887
888 #if FF_API_FLAG_TRUNCATED
889 783 av_freep(&s->parse_context.buffer);
890 783 s->parse_context.buffer_size = 0;
891 #endif
892
893 783 av_freep(&s->bitstream_buffer);
894 783 s->allocated_bitstream_buffer_size = 0;
895
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 783 times.
783 if (!s->avctx)
897 return;
898
899
2/2
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 21 times.
783 if (s->picture) {
900
2/2
✓ Branch 0 taken 27432 times.
✓ Branch 1 taken 762 times.
28194 for (int i = 0; i < MAX_PICTURE_COUNT; i++)
901 27432 ff_mpv_picture_free(s->avctx, &s->picture[i]);
902 }
903 783 av_freep(&s->picture);
904 783 ff_mpv_picture_free(s->avctx, &s->last_picture);
905 783 ff_mpv_picture_free(s->avctx, &s->current_picture);
906 783 ff_mpv_picture_free(s->avctx, &s->next_picture);
907 783 av_frame_free(&s->new_picture);
908
909 783 s->context_initialized = 0;
910 783 s->context_reinit = 0;
911 783 s->last_picture_ptr =
912 783 s->next_picture_ptr =
913 783 s->current_picture_ptr = NULL;
914 783 s->linesize = s->uvlinesize = 0;
915 }
916
917
918 126300 static inline int hpel_motion_lowres(MpegEncContext *s,
919 uint8_t *dest, uint8_t *src,
920 int field_based, int field_select,
921 int src_x, int src_y,
922 int width, int height, ptrdiff_t stride,
923 int h_edge_pos, int v_edge_pos,
924 int w, int h, h264_chroma_mc_func *pix_op,
925 int motion_x, int motion_y)
926 {
927 126300 const int lowres = s->avctx->lowres;
928 126300 const int op_index = FFMIN(lowres, 3);
929 126300 const int s_mask = (2 << lowres) - 1;
930 126300 int emu = 0;
931 int sx, sy;
932
933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (s->quarter_sample) {
934 motion_x /= 2;
935 motion_y /= 2;
936 }
937
938 126300 sx = motion_x & s_mask;
939 126300 sy = motion_y & s_mask;
940 126300 src_x += motion_x >> lowres + 1;
941 126300 src_y += motion_y >> lowres + 1;
942
943 126300 src += src_y * stride + src_x;
944
945
1/2
✓ Branch 0 taken 126300 times.
✗ Branch 1 not taken.
126300 if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
946
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 126002 times.
126300 (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
947 298 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
948 s->linesize, s->linesize,
949 298 w + 1, (h + 1) << field_based,
950 src_x, src_y << field_based,
951 h_edge_pos, v_edge_pos);
952 298 src = s->sc.edge_emu_buffer;
953 298 emu = 1;
954 }
955
956 126300 sx = (sx << 2) >> lowres;
957 126300 sy = (sy << 2) >> lowres;
958
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126300 times.
126300 if (field_select)
959 src += s->linesize;
960 126300 pix_op[op_index](dest, src, stride, h, sx, sy);
961 126300 return emu;
962 }
963
964 /* apply one mpeg motion vector to the three components */
965 24814 static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
966 uint8_t *dest_y,
967 uint8_t *dest_cb,
968 uint8_t *dest_cr,
969 int field_based,
970 int bottom_field,
971 int field_select,
972 uint8_t **ref_picture,
973 h264_chroma_mc_func *pix_op,
974 int motion_x, int motion_y,
975 int h, int mb_y)
976 {
977 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
978 int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
979 ptrdiff_t uvlinesize, linesize;
980 24814 const int lowres = s->avctx->lowres;
981 24814 const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
982 24814 const int block_s = 8>>lowres;
983 24814 const int s_mask = (2 << lowres) - 1;
984 24814 const int h_edge_pos = s->h_edge_pos >> lowres;
985 24814 const int v_edge_pos = s->v_edge_pos >> lowres;
986 24814 linesize = s->current_picture.f->linesize[0] << field_based;
987 24814 uvlinesize = s->current_picture.f->linesize[1] << field_based;
988
989 // FIXME obviously not perfect but qpel will not work in lowres anyway
990
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (s->quarter_sample) {
991 motion_x /= 2;
992 motion_y /= 2;
993 }
994
995
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if(field_based){
996 motion_y += (bottom_field - field_select)*((1 << lowres)-1);
997 }
998
999 24814 sx = motion_x & s_mask;
1000 24814 sy = motion_y & s_mask;
1001 24814 src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
1002 24814 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
1003
1004
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (s->out_format == FMT_H263) {
1005 24814 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
1006 24814 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
1007 24814 uvsrc_x = src_x >> 1;
1008 24814 uvsrc_y = src_y >> 1;
1009 } else if (s->out_format == FMT_H261) {
1010 // even chroma mv's are full pel in H261
1011 mx = motion_x / 4;
1012 my = motion_y / 4;
1013 uvsx = (2 * mx) & s_mask;
1014 uvsy = (2 * my) & s_mask;
1015 uvsrc_x = s->mb_x * block_s + (mx >> lowres);
1016 uvsrc_y = mb_y * block_s + (my >> lowres);
1017 } else {
1018 if(s->chroma_y_shift){
1019 mx = motion_x / 2;
1020 my = motion_y / 2;
1021 uvsx = mx & s_mask;
1022 uvsy = my & s_mask;
1023 uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
1024 uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
1025 } else {
1026 if(s->chroma_x_shift){
1027 //Chroma422
1028 mx = motion_x / 2;
1029 uvsx = mx & s_mask;
1030 uvsy = motion_y & s_mask;
1031 uvsrc_y = src_y;
1032 uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
1033 } else {
1034 //Chroma444
1035 uvsx = motion_x & s_mask;
1036 uvsy = motion_y & s_mask;
1037 uvsrc_x = src_x;
1038 uvsrc_y = src_y;
1039 }
1040 }
1041 }
1042
1043 24814 ptr_y = ref_picture[0] + src_y * linesize + src_x;
1044 24814 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
1045 24814 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
1046
1047
3/4
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24712 times.
✓ Branch 3 taken 102 times.
24814 if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
1048
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 24617 times.
24712 (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
1049 197 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
1050 linesize >> field_based, linesize >> field_based,
1051 17, 17 + field_based,
1052 src_x, src_y << field_based, h_edge_pos,
1053 v_edge_pos);
1054 197 ptr_y = s->sc.edge_emu_buffer;
1055 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1056 197 uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
1057 197 uint8_t *vbuf =ubuf + 10 * s->uvlinesize;
1058
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 197 times.
197 if (s->workaround_bugs & FF_BUG_IEDGE)
1059 vbuf -= s->uvlinesize;
1060 197 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
1061 uvlinesize >> field_based, uvlinesize >> field_based,
1062 9, 9 + field_based,
1063 uvsrc_x, uvsrc_y << field_based,
1064 h_edge_pos >> 1, v_edge_pos >> 1);
1065 197 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
1066 uvlinesize >> field_based,uvlinesize >> field_based,
1067 9, 9 + field_based,
1068 uvsrc_x, uvsrc_y << field_based,
1069 h_edge_pos >> 1, v_edge_pos >> 1);
1070 197 ptr_cb = ubuf;
1071 197 ptr_cr = vbuf;
1072 }
1073 }
1074
1075 // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
1076
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (bottom_field) {
1077 dest_y += s->linesize;
1078 dest_cb += s->uvlinesize;
1079 dest_cr += s->uvlinesize;
1080 }
1081
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24814 times.
24814 if (field_select) {
1083 ptr_y += s->linesize;
1084 ptr_cb += s->uvlinesize;
1085 ptr_cr += s->uvlinesize;
1086 }
1087
1088 24814 sx = (sx << 2) >> lowres;
1089 24814 sy = (sy << 2) >> lowres;
1090 24814 pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
1091
1092 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1093
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
1094 24814 uvsx = (uvsx << 2) >> lowres;
1095 24814 uvsy = (uvsy << 2) >> lowres;
1096
1/2
✓ Branch 0 taken 24814 times.
✗ Branch 1 not taken.
24814 if (hc) {
1097 24814 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
1098 24814 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
1099 }
1100 }
1101 // FIXME h261 lowres loop filter
1102 24814 }
1103
1104 31575 static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
1105 uint8_t *dest_cb, uint8_t *dest_cr,
1106 uint8_t **ref_picture,
1107 h264_chroma_mc_func * pix_op,
1108 int mx, int my)
1109 {
1110 31575 const int lowres = s->avctx->lowres;
1111 31575 const int op_index = FFMIN(lowres, 3);
1112 31575 const int block_s = 8 >> lowres;
1113 31575 const int s_mask = (2 << lowres) - 1;
1114 31575 const int h_edge_pos = s->h_edge_pos >> lowres + 1;
1115 31575 const int v_edge_pos = s->v_edge_pos >> lowres + 1;
1116 31575 int emu = 0, src_x, src_y, sx, sy;
1117 ptrdiff_t offset;
1118 uint8_t *ptr;
1119
1120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31575 times.
31575 if (s->quarter_sample) {
1121 mx /= 2;
1122 my /= 2;
1123 }
1124
1125 /* In case of 8X8, we construct a single chroma motion vector
1126 with a special rounding */
1127 31575 mx = ff_h263_round_chroma(mx);
1128 31575 my = ff_h263_round_chroma(my);
1129
1130 31575 sx = mx & s_mask;
1131 31575 sy = my & s_mask;
1132 31575 src_x = s->mb_x * block_s + (mx >> lowres + 1);
1133 31575 src_y = s->mb_y * block_s + (my >> lowres + 1);
1134
1135 31575 offset = src_y * s->uvlinesize + src_x;
1136 31575 ptr = ref_picture[1] + offset;
1137
1/2
✓ Branch 0 taken 31575 times.
✗ Branch 1 not taken.
31575 if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
1138
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
1139 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
1140 s->uvlinesize, s->uvlinesize,
1141 9, 9,
1142 src_x, src_y, h_edge_pos, v_edge_pos);
1143 144 ptr = s->sc.edge_emu_buffer;
1144 144 emu = 1;
1145 }
1146 31575 sx = (sx << 2) >> lowres;
1147 31575 sy = (sy << 2) >> lowres;
1148 31575 pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
1149
1150 31575 ptr = ref_picture[2] + offset;
1151
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 31431 times.
31575 if (emu) {
1152 144 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
1153 s->uvlinesize, s->uvlinesize,
1154 9, 9,
1155 src_x, src_y, h_edge_pos, v_edge_pos);
1156 144 ptr = s->sc.edge_emu_buffer;
1157 }
1158 31575 pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
1159 31575 }
1160
1161 /**
1162 * motion compensation of a single macroblock
1163 * @param s context
1164 * @param dest_y luma destination pointer
1165 * @param dest_cb chroma cb/u destination pointer
1166 * @param dest_cr chroma cr/v destination pointer
1167 * @param dir direction (0->forward, 1->backward)
1168 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
1169 * @param pix_op halfpel motion compensation function (average or put normally)
1170 * the motion vectors are taken from s->mv and the MV type from s->mv_type
1171 */
1172 56389 static inline void MPV_motion_lowres(MpegEncContext *s,
1173 uint8_t *dest_y, uint8_t *dest_cb,
1174 uint8_t *dest_cr,
1175 int dir, uint8_t **ref_picture,
1176 h264_chroma_mc_func *pix_op)
1177 {
1178 int mx, my;
1179 int mb_x, mb_y, i;
1180 56389 const int lowres = s->avctx->lowres;
1181 56389 const int block_s = 8 >>lowres;
1182
1183 56389 mb_x = s->mb_x;
1184 56389 mb_y = s->mb_y;
1185
1186
2/6
✓ Branch 0 taken 24814 times.
✓ Branch 1 taken 31575 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
56389 switch (s->mv_type) {
1187 24814 case MV_TYPE_16X16:
1188 24814 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1189 0, 0, 0,
1190 ref_picture, pix_op,
1191 s->mv[dir][0][0], s->mv[dir][0][1],
1192 2 * block_s, mb_y);
1193 24814 break;
1194 31575 case MV_TYPE_8X8:
1195 31575 mx = 0;
1196 31575 my = 0;
1197
2/2
✓ Branch 0 taken 126300 times.
✓ Branch 1 taken 31575 times.
157875 for (i = 0; i < 4; i++) {
1198 126300 hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
1199 126300 s->linesize) * block_s,
1200 ref_picture[0], 0, 0,
1201 126300 (2 * mb_x + (i & 1)) * block_s,
1202 126300 (2 * mb_y + (i >> 1)) * block_s,
1203 s->width, s->height, s->linesize,
1204 126300 s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
1205 block_s, block_s, pix_op,
1206 s->mv[dir][i][0], s->mv[dir][i][1]);
1207
1208 126300 mx += s->mv[dir][i][0];
1209 126300 my += s->mv[dir][i][1];
1210 }
1211
1212 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
1213 31575 chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
1214 pix_op, mx, my);
1215 31575 break;
1216 case MV_TYPE_FIELD:
1217 if (s->picture_structure == PICT_FRAME) {
1218 /* top field */
1219 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1220 1, 0, s->field_select[dir][0],
1221 ref_picture, pix_op,
1222 s->mv[dir][0][0], s->mv[dir][0][1],
1223 block_s, mb_y);
1224 /* bottom field */
1225 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1226 1, 1, s->field_select[dir][1],
1227 ref_picture, pix_op,
1228 s->mv[dir][1][0], s->mv[dir][1][1],
1229 block_s, mb_y);
1230 } else {
1231 if (s->picture_structure != s->field_select[dir][0] + 1 &&
1232 s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
1233 ref_picture = s->current_picture_ptr->f->data;
1234
1235 }
1236 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1237 0, 0, s->field_select[dir][0],
1238 ref_picture, pix_op,
1239 s->mv[dir][0][0],
1240 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
1241 }
1242 break;
1243 case MV_TYPE_16X8:
1244 for (i = 0; i < 2; i++) {
1245 uint8_t **ref2picture;
1246
1247 if (s->picture_structure == s->field_select[dir][i] + 1 ||
1248 s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
1249 ref2picture = ref_picture;
1250 } else {
1251 ref2picture = s->current_picture_ptr->f->data;
1252 }
1253
1254 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1255 0, 0, s->field_select[dir][i],
1256 ref2picture, pix_op,
1257 s->mv[dir][i][0], s->mv[dir][i][1] +
1258 2 * block_s * i, block_s, mb_y >> 1);
1259
1260 dest_y += 2 * block_s * s->linesize;
1261 dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
1262 dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
1263 }
1264 break;
1265 case MV_TYPE_DMV:
1266 if (s->picture_structure == PICT_FRAME) {
1267 for (i = 0; i < 2; i++) {
1268 int j;
1269 for (j = 0; j < 2; j++) {
1270 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1271 1, j, j ^ i,
1272 ref_picture, pix_op,
1273 s->mv[dir][2 * i + j][0],
1274 s->mv[dir][2 * i + j][1],
1275 block_s, mb_y);
1276 }
1277 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
1278 }
1279 } else {
1280 for (i = 0; i < 2; i++) {
1281 mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
1282 0, 0, s->picture_structure != i + 1,
1283 ref_picture, pix_op,
1284 s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
1285 2 * block_s, mb_y >> 1);
1286
1287 // after put we make avg of the same block
1288 pix_op = s->h264chroma.avg_h264_chroma_pixels_tab;
1289
1290 // opposite parity is always in the same
1291 // frame if this is second field
1292 if (!s->first_field) {
1293 ref_picture = s->current_picture_ptr->f->data;
1294 }
1295 }
1296 }
1297 break;
1298 56389 default:
1299 av_assert2(0);
1300 }
1301 56389 }
1302
1303 /**
1304 * find the lowest MB row referenced in the MVs
1305 */
1306 static int lowest_referenced_row(MpegEncContext *s, int dir)
1307 {
1308 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
1309 int my, off, i, mvs;
1310
1311 if (s->picture_structure != PICT_FRAME || s->mcsel)
1312 goto unhandled;
1313
1314 switch (s->mv_type) {
1315 case MV_TYPE_16X16:
1316 mvs = 1;
1317 break;
1318 case MV_TYPE_16X8:
1319 mvs = 2;
1320 break;
1321 case MV_TYPE_8X8:
1322 mvs = 4;
1323 break;
1324 default:
1325 goto unhandled;
1326 }
1327
1328 for (i = 0; i < mvs; i++) {
1329 my = s->mv[dir][i][1];
1330 my_max = FFMAX(my_max, my);
1331 my_min = FFMIN(my_min, my);
1332 }
1333
1334 off = ((FFMAX(-my_min, my_max)<<qpel_shift) + 63) >> 6;
1335
1336 return av_clip(s->mb_y + off, 0, s->mb_height - 1);
1337 unhandled:
1338 return s->mb_height-1;
1339 }
1340
1341 /* put block[] to dest[] */
1342 4834232 static inline void put_dct(MpegEncContext *s,
1343 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1344 {
1345 4834232 s->dct_unquantize_intra(s, block, i, qscale);
1346 4834232 s->idsp.idct_put(dest, line_size, block);
1347 4834232 }
1348
1349 /* add block[] to dest[] */
1350 19756868 static inline void add_dct(MpegEncContext *s,
1351 int16_t *block, int i, uint8_t *dest, int line_size)
1352 {
1353
2/2
✓ Branch 0 taken 7190377 times.
✓ Branch 1 taken 12566491 times.
19756868 if (s->block_last_index[i] >= 0) {
1354 7190377 s->idsp.idct_add(dest, line_size, block);
1355 }
1356 19756868 }
1357
1358 22591838 static inline void add_dequant_dct(MpegEncContext *s,
1359 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1360 {
1361
2/2
✓ Branch 0 taken 9001045 times.
✓ Branch 1 taken 13590793 times.
22591838 if (s->block_last_index[i] >= 0) {
1362 9001045 s->dct_unquantize_inter(s, block, i, qscale);
1363
1364 9001045 s->idsp.idct_add(dest, line_size, block);
1365 }
1366 22591838 }
1367
1368 /**
1369 * Clean dc, ac, coded_block for the current non-intra MB.
1370 */
1371 389410 void ff_clean_intra_table_entries(MpegEncContext *s)
1372 {
1373 389410 int wrap = s->b8_stride;
1374 389410 int xy = s->block_index[0];
1375
1376 389410 s->dc_val[0][xy ] =
1377 389410 s->dc_val[0][xy + 1 ] =
1378 389410 s->dc_val[0][xy + wrap] =
1379 389410 s->dc_val[0][xy + 1 + wrap] = 1024;
1380 /* ac pred */
1381 389410 memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
1382 389410 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
1383
2/2
✓ Branch 0 taken 45701 times.
✓ Branch 1 taken 343709 times.
389410 if (s->msmpeg4_version>=3) {
1384 45701 s->coded_block[xy ] =
1385 45701 s->coded_block[xy + 1 ] =
1386 45701 s->coded_block[xy + wrap] =
1387 45701 s->coded_block[xy + 1 + wrap] = 0;
1388 }
1389 /* chroma */
1390 389410 wrap = s->mb_stride;
1391 389410 xy = s->mb_x + s->mb_y * wrap;
1392 389410 s->dc_val[1][xy] =
1393 389410 s->dc_val[2][xy] = 1024;
1394 /* ac pred */
1395 389410 memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
1396 389410 memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
1397
1398 389410 s->mbintra_table[xy]= 0;
1399 389410 }
1400
1401 /* generic function called after a macroblock has been parsed by the
1402 decoder or after it has been encoded by the encoder.
1403
1404 Important variables used:
1405 s->mb_intra : true if intra macroblock
1406 s->mv_dir : motion vector direction
1407 s->mv_type : motion vector type
1408 s->mv : motion vector
1409 s->interlaced_dct : true if interlaced dct used (mpeg2)
1410 */
1411 static av_always_inline
1412 9090059 void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64],
1413 int lowres_flag, int is_mpeg12)
1414 {
1415 #define IS_ENCODER(s) (CONFIG_MPEGVIDEOENC && !lowres_flag && (s)->encoding)
1416 #define IS_MPEG12(s) (CONFIG_SMALL ? ((s)->out_format == FMT_MPEG1) : is_mpeg12)
1417 9090059 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
1418
1419 9090059 s->current_picture.qscale_table[mb_xy] = s->qscale;
1420
1421 /* update DC predictors for P macroblocks */
1422
2/2
✓ Branch 0 taken 7326661 times.
✓ Branch 1 taken 1763398 times.
9090059 if (!s->mb_intra) {
1423
6/6
✓ Branch 0 taken 4219701 times.
✓ Branch 1 taken 3106960 times.
✓ Branch 2 taken 952206 times.
✓ Branch 3 taken 3267495 times.
✓ Branch 4 taken 103314 times.
✓ Branch 5 taken 848892 times.
7326661 if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
1424
2/2
✓ Branch 0 taken 311786 times.
✓ Branch 1 taken 3059023 times.
3370809 if(s->mbintra_table[mb_xy])
1425 311786 ff_clean_intra_table_entries(s);
1426 } else {
1427 3955852 s->last_dc[0] =
1428 3955852 s->last_dc[1] =
1429 3955852 s->last_dc[2] = 128 << s->intra_dc_precision;
1430 }
1431 }
1432
6/6
✓ Branch 0 taken 1034717 times.
✓ Branch 1 taken 728681 times.
✓ Branch 2 taken 604022 times.
✓ Branch 3 taken 430695 times.
✓ Branch 4 taken 28866 times.
✓ Branch 5 taken 575156 times.
1763398 else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
1433 459561 s->mbintra_table[mb_xy]=1;
1434
1435
7/10
✓ Branch 0 taken 9046559 times.
✓ Branch 1 taken 43500 times.
✓ Branch 2 taken 4511315 times.
✓ Branch 3 taken 4535244 times.
✓ Branch 4 taken 4511315 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4511315 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4511315 times.
✗ Branch 9 not taken.
9090059 if (!IS_ENCODER(s) || (s->avctx->flags & AV_CODEC_FLAG_PSNR) || s->frame_skip_threshold || s->frame_skip_factor ||
1436
4/4
✓ Branch 0 taken 3820753 times.
✓ Branch 1 taken 690562 times.
✓ Branch 2 taken 1236491 times.
✓ Branch 3 taken 2584262 times.
4511315 !((s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
1437
2/2
✓ Branch 0 taken 1019795 times.
✓ Branch 1 taken 907258 times.
1927053 s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
1438 uint8_t *dest_y, *dest_cb, *dest_cr;
1439 int dct_linesize, dct_offset;
1440 op_pixels_func (*op_pix)[4];
1441 qpel_mc_func (*op_qpix)[16];
1442 8182801 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1443 8182801 const int uvlinesize = s->current_picture.f->linesize[1];
1444
9/10
✓ Branch 0 taken 2125941 times.
✓ Branch 1 taken 6056860 times.
✓ Branch 2 taken 2104341 times.
✓ Branch 3 taken 21600 times.
✓ Branch 4 taken 1084546 times.
✓ Branch 5 taken 1019795 times.
✓ Branch 6 taken 1106146 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 21600 times.
✓ Branch 9 taken 1084546 times.
8182801 const int readable = s->pict_type != AV_PICTURE_TYPE_B || IS_ENCODER(s) || s->avctx->draw_horiz_band || lowres_flag;
1445
2/2
✓ Branch 0 taken 43500 times.
✓ Branch 1 taken 8139301 times.
8182801 const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
1446
1447 /* avoid copy if macroblock skipped in last frame too */
1448 /* skip only during decoding as we might trash the buffers during encoding a bit */
1449
4/4
✓ Branch 0 taken 8139301 times.
✓ Branch 1 taken 43500 times.
✓ Branch 2 taken 4535244 times.
✓ Branch 3 taken 3604057 times.
8182801 if (!IS_ENCODER(s)) {
1450 4578744 uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
1451
1452
2/2
✓ Branch 0 taken 323448 times.
✓ Branch 1 taken 4255296 times.
4578744 if (s->mb_skipped) {
1453 323448 s->mb_skipped= 0;
1454 av_assert2(s->pict_type!=AV_PICTURE_TYPE_I);
1455 323448 *mbskip_ptr = 1;
1456
2/2
✓ Branch 0 taken 999062 times.
✓ Branch 1 taken 3256234 times.
4255296 } else if(!s->current_picture.reference) {
1457 999062 *mbskip_ptr = 1;
1458 } else{
1459 3256234 *mbskip_ptr = 0; /* not skipped */
1460 }
1461 }
1462
1463 8182801 dct_linesize = linesize << s->interlaced_dct;
1464
2/2
✓ Branch 0 taken 8032043 times.
✓ Branch 1 taken 150758 times.
8182801 dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
1465
1466
2/2
✓ Branch 0 taken 7098255 times.
✓ Branch 1 taken 1084546 times.
8182801 if(readable){
1467 7098255 dest_y= s->dest[0];
1468 7098255 dest_cb= s->dest[1];
1469 7098255 dest_cr= s->dest[2];
1470 }else{
1471 1084546 dest_y = s->sc.b_scratchpad;
1472 1084546 dest_cb= s->sc.b_scratchpad+16*linesize;
1473 1084546 dest_cr= s->sc.b_scratchpad+32*linesize;
1474 }
1475
1476
2/2
✓ Branch 0 taken 7109965 times.
✓ Branch 1 taken 1072836 times.
8182801 if (!s->mb_intra) {
1477 /* motion handling */
1478 /* decoding or more than one mb_type (MC was already done otherwise) */
1479
4/4
✓ Branch 0 taken 7068842 times.
✓ Branch 1 taken 41123 times.
✓ Branch 2 taken 3957870 times.
✓ Branch 3 taken 3110972 times.
7109965 if (!IS_ENCODER(s)) {
1480
1481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3998993 times.
3998993 if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1482 if (s->mv_dir & MV_DIR_FORWARD) {
1483 ff_thread_await_progress(&s->last_picture_ptr->tf,
1484 lowest_referenced_row(s, 0),
1485 0);
1486 }
1487 if (s->mv_dir & MV_DIR_BACKWARD) {
1488 ff_thread_await_progress(&s->next_picture_ptr->tf,
1489 lowest_referenced_row(s, 1),
1490 0);
1491 }
1492 }
1493
1494
2/2
✓ Branch 0 taken 41123 times.
✓ Branch 1 taken 3957870 times.
3998993 if(lowres_flag){
1495 41123 h264_chroma_mc_func *op_pix = s->h264chroma.put_h264_chroma_pixels_tab;
1496
1497
2/2
✓ Branch 0 taken 37429 times.
✓ Branch 1 taken 3694 times.
41123 if (s->mv_dir & MV_DIR_FORWARD) {
1498 37429 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
1499 37429 op_pix = s->h264chroma.avg_h264_chroma_pixels_tab;
1500 }
1501
2/2
✓ Branch 0 taken 18960 times.
✓ Branch 1 taken 22163 times.
41123 if (s->mv_dir & MV_DIR_BACKWARD) {
1502 18960 MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
1503 }
1504 }else{
1505 3957870 op_qpix = s->me.qpel_put;
1506
5/6
✓ Branch 0 taken 2112480 times.
✓ Branch 1 taken 1845390 times.
✓ Branch 2 taken 759743 times.
✓ Branch 3 taken 1352737 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 759743 times.
3957870 if ((is_mpeg12 || !s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1507 3198127 op_pix = s->hdsp.put_pixels_tab;
1508 }else{
1509 759743 op_pix = s->hdsp.put_no_rnd_pixels_tab;
1510 }
1511
2/2
✓ Branch 0 taken 3690417 times.
✓ Branch 1 taken 267453 times.
3957870 if (s->mv_dir & MV_DIR_FORWARD) {
1512 3690417 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
1513 3690417 op_pix = s->hdsp.avg_pixels_tab;
1514 3690417 op_qpix= s->me.qpel_avg;
1515 }
1516
2/2
✓ Branch 0 taken 837796 times.
✓ Branch 1 taken 3120074 times.
3957870 if (s->mv_dir & MV_DIR_BACKWARD) {
1517 837796 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
1518 }
1519 }
1520 }
1521
1522 /* skip dequant / idct if we are really late ;) */
1523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7109965 times.
7109965 if(s->avctx->skip_idct){
1524 if( (s->avctx->skip_idct >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B)
1525 ||(s->avctx->skip_idct >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I)
1526 || s->avctx->skip_idct >= AVDISCARD_ALL)
1527 goto skip_idct;
1528 }
1529
1530 /* add dct residue */
1531
8/8
✓ Branch 0 taken 7068842 times.
✓ Branch 1 taken 41123 times.
✓ Branch 2 taken 3957870 times.
✓ Branch 3 taken 3110972 times.
✓ Branch 4 taken 2153603 times.
✓ Branch 5 taken 1845390 times.
✓ Branch 6 taken 1784955 times.
✓ Branch 7 taken 368648 times.
7109965 if (IS_ENCODER(s) || !(IS_MPEG12(s) || s->msmpeg4_version
1532
4/4
✓ Branch 0 taken 1308023 times.
✓ Branch 1 taken 476932 times.
✓ Branch 2 taken 61534 times.
✓ Branch 3 taken 1246489 times.
1784955 || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
1533 3649438 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1534 3649438 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1535 3649438 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1536 3649438 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1537
1538 3649438 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1539
2/2
✓ Branch 0 taken 3301833 times.
✓ Branch 1 taken 347605 times.
3649438 if (s->chroma_y_shift){
1540 3301833 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1541 3301833 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1542 }else{
1543 347605 dct_linesize >>= 1;
1544 347605 dct_offset >>=1;
1545 347605 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1546 347605 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1547 347605 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1548 347605 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1549 }
1550 }
1551
4/4
✓ Branch 0 taken 1615137 times.
✓ Branch 1 taken 1845390 times.
✓ Branch 2 taken 1424988 times.
✓ Branch 3 taken 190149 times.
3460527 } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
1552 3270378 add_dct(s, block[0], 0, dest_y , dct_linesize);
1553 3270378 add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
1554 3270378 add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
1555 3270378 add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
1556
1557 3270378 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1558
2/2
✓ Branch 0 taken 3203078 times.
✓ Branch 1 taken 67300 times.
3270378 if(s->chroma_y_shift){//Chroma420
1559 3203078 add_dct(s, block[4], 4, dest_cb, uvlinesize);
1560 3203078 add_dct(s, block[5], 5, dest_cr, uvlinesize);
1561 }else{
1562 //chroma422
1563 67300 dct_linesize = uvlinesize << s->interlaced_dct;
1564
2/2
✓ Branch 0 taken 65664 times.
✓ Branch 1 taken 1636 times.
67300 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
1565
1566 67300 add_dct(s, block[4], 4, dest_cb, dct_linesize);
1567 67300 add_dct(s, block[5], 5, dest_cr, dct_linesize);
1568 67300 add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
1569 67300 add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
1570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67300 times.
67300 if(!s->chroma_x_shift){//Chroma444
1571 add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
1572 add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
1573 add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
1574 add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
1575 }
1576 }
1577 }//fi gray
1578 } else if (CONFIG_WMV2_DECODER) {
1579 190149 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
1580 }
1581 } else {
1582 /* Only MPEG-4 Simple Studio Profile is supported in > 8-bit mode.
1583 TODO: Integrate 10-bit properly into mpegvideo.c so that ER works properly */
1584
2/2
✓ Branch 0 taken 600655 times.
✓ Branch 1 taken 472181 times.
1072836 if (!is_mpeg12 && CONFIG_MPEG4_DECODER && /* s->codec_id == AV_CODEC_ID_MPEG4 && */
1585
2/2
✓ Branch 0 taken 1350 times.
✓ Branch 1 taken 599305 times.
600655 s->avctx->bits_per_raw_sample > 8) {
1586 1350 ff_mpeg4_decode_studio(s, dest_y, dest_cb, dest_cr, block_size,
1587 uvlinesize, dct_linesize, dct_offset);
1588 }
1589 /* dct only in intra block */
1590
6/6
✓ Branch 0 taken 1069109 times.
✓ Branch 1 taken 2377 times.
✓ Branch 2 taken 576024 times.
✓ Branch 3 taken 493085 times.
✓ Branch 4 taken 299617 times.
✓ Branch 5 taken 278784 times.
1071486 else if (IS_ENCODER(s) || !IS_MPEG12(s)) {
1591 792702 put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
1592 792702 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
1593 792702 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
1594 792702 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
1595
1596 792702 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1597
2/2
✓ Branch 0 taken 753692 times.
✓ Branch 1 taken 39010 times.
792702 if(s->chroma_y_shift){
1598 753692 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
1599 753692 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
1600 }else{
1601 39010 dct_offset >>=1;
1602 39010 dct_linesize >>=1;
1603 39010 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
1604 39010 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
1605 39010 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
1606 39010 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
1607 }
1608 }
1609 }else{
1610 278784 s->idsp.idct_put(dest_y, dct_linesize, block[0]);
1611 278784 s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
1612 278784 s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
1613 278784 s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
1614
1615 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1616
2/2
✓ Branch 0 taken 224974 times.
✓ Branch 1 taken 53810 times.
278784 if(s->chroma_y_shift){
1617 224974 s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
1618 224974 s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
1619 }else{
1620
1621 53810 dct_linesize = uvlinesize << s->interlaced_dct;
1622
2/2
✓ Branch 0 taken 53780 times.
✓ Branch 1 taken 30 times.
53810 dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
1623
1624 53810 s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
1625 53810 s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
1626 53810 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
1627 53810 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
1628
1/2
✓ Branch 0 taken 53810 times.
✗ Branch 1 not taken.
53810 if(!s->chroma_x_shift){//Chroma444
1629 s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
1630 s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
1631 s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
1632 s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
1633 }
1634 }
1635 }//gray
1636 }
1637 }
1638 53810 skip_idct:
1639
2/2
✓ Branch 0 taken 1084546 times.
✓ Branch 1 taken 7098255 times.
8182801 if(!readable){
1640 1084546 s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
1641 if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
1642 1084546 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
1643 1084546 s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
1644 }
1645 }
1646 }
1647 9090059 }
1648
1649 9090059 void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
1650 {
1651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9090059 times.
9090059 if (s->avctx->debug & FF_DEBUG_DCT_COEFF) {
1652 /* print DCT coefficients */
1653 av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
1654 for (int i = 0; i < 6; i++) {
1655 for (int j = 0; j < 64; j++) {
1656 av_log(s->avctx, AV_LOG_DEBUG, "%5d",
1657 block[i][s->idsp.idct_permutation[j]]);
1658 }
1659 av_log(s->avctx, AV_LOG_DEBUG, "\n");
1660 }
1661 }
1662
1663 #if !CONFIG_SMALL
1664
2/2
✓ Branch 0 taken 3835641 times.
✓ Branch 1 taken 5254418 times.
9090059 if(s->out_format == FMT_MPEG1) {
1665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3835641 times.
3835641 if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 1);
1666 3835641 else mpv_reconstruct_mb_internal(s, block, 0, 1);
1667 } else
1668 #endif
1669
2/2
✓ Branch 0 taken 43500 times.
✓ Branch 1 taken 5210918 times.
5254418 if(s->avctx->lowres) mpv_reconstruct_mb_internal(s, block, 1, 0);
1670 5210918 else mpv_reconstruct_mb_internal(s, block, 0, 0);
1671 9090059 }
1672
1673 850028 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
1674 850028 const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
1675 850028 const int uvlinesize = s->current_picture.f->linesize[1];
1676
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 849998 times.
850028 const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
1677 850028 const int height_of_mb = 4 - s->avctx->lowres;
1678
1679 850028 s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
1680 850028 s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
1681 850028 s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
1682 850028 s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
1683 850028 s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
1684 850028 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;
1685 //block_index is not used by mpeg2, so it is not affected by chroma_format
1686
1687 850028 s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) << width_of_mb);
1688 850028 s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
1689 850028 s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
1690
1691
3/6
✓ Branch 0 taken 116031 times.
✓ Branch 1 taken 733997 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 116031 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
850028 if(!(s->pict_type==AV_PICTURE_TYPE_B && s->avctx->draw_horiz_band && s->picture_structure==PICT_FRAME))
1692 {
1693
2/2
✓ Branch 0 taken 823448 times.
✓ Branch 1 taken 26580 times.
850028 if(s->picture_structure==PICT_FRAME){
1694 823448 s->dest[0] += s->mb_y * linesize << height_of_mb;
1695 823448 s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
1696 823448 s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
1697 }else{
1698 26580 s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
1699 26580 s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
1700 26580 s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
1701 av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
1702 }
1703 }
1704 850028 }
1705
1706 /**
1707 * set qscale and update qscale dependent variables.
1708 */
1709 1452086 void ff_set_qscale(MpegEncContext * s, int qscale)
1710 {
1711
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1452085 times.
1452086 if (qscale < 1)
1712 1 qscale = 1;
1713
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1452056 times.
1452085 else if (qscale > 31)
1714 29 qscale = 31;
1715
1716 1452086 s->qscale = qscale;
1717 1452086 s->chroma_qscale= s->chroma_qscale_table[qscale];
1718
1719 1452086 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
1720 1452086 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
1721 1452086 }
1722