FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2026-09-24 14:13:09
Exec Total Coverage
Lines: 2019 2810 71.9%
Functions: 52 62 83.9%
Branches: 1158 1764 65.6%

Line Branch Exec Source
1 /*
2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /*
26 * non linear quantizers with large QPs and VBV with restrictive qmin fixes sponsored by NOA GmbH
27 */
28
29 /**
30 * @file
31 * The simplest mpeg encoder (well, it was the simplest!).
32 */
33
34 #include "config_components.h"
35
36 #include <assert.h>
37 #include <stdint.h>
38
39 #include "libavutil/attributes.h"
40 #include "libavutil/emms.h"
41 #include "libavutil/internal.h"
42 #include "libavutil/intmath.h"
43 #include "libavutil/mathematics.h"
44 #include "libavutil/mem.h"
45 #include "libavutil/mem_internal.h"
46 #include "libavutil/opt.h"
47 #include "libavutil/thread.h"
48 #include "avcodec.h"
49 #include "encode.h"
50 #include "idctdsp.h"
51 #include "mpeg12data.h"
52 #include "mpeg12enc.h"
53 #include "mpegvideo.h"
54 #include "mpegvideodata.h"
55 #include "mpegvideoenc.h"
56 #include "h261enc.h"
57 #include "h263.h"
58 #include "h263data.h"
59 #include "h263enc.h"
60 #include "mjpegenc_common.h"
61 #include "mathops.h"
62 #include "mpegutils.h"
63 #include "mpegvideo_unquantize.h"
64 #include "mjpegenc.h"
65 #include "speedhqenc.h"
66 #include "msmpeg4enc.h"
67 #include "pixblockdsp.h"
68 #include "qpeldsp.h"
69 #include "faandct.h"
70 #include "aandcttab.h"
71 #include "mpeg4video.h"
72 #include "mpeg4videodata.h"
73 #include "mpeg4videoenc.h"
74 #include "internal.h"
75 #include "bytestream.h"
76 #include "rv20enc.h"
77 #include "libavutil/refstruct.h"
78 #include <limits.h>
79 #include "sp5x.h"
80
81 #define QUANT_BIAS_SHIFT 8
82
83 #define QMAT_SHIFT_MMX 16
84 #define QMAT_SHIFT 21
85
86 static int encode_picture(MPVMainEncContext *const s, const AVPacket *pkt);
87 static int dct_quantize_refine(MPVEncContext *const s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
88 static int sse_mb(MPVEncContext *const s);
89 static int dct_quantize_c(MPVEncContext *const s,
90 int16_t *block, int n,
91 int qscale, int *overflow);
92 static int dct_quantize_trellis_c(MPVEncContext *const s, int16_t *block, int n, int qscale, int *overflow);
93
94 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
95
96 static const AVOption mpv_generic_options[] = {
97 FF_MPV_COMMON_OPTS
98 FF_MPV_COMMON_MOTION_EST_OPTS
99 { NULL },
100 };
101
102 const AVClass ff_mpv_enc_class = {
103 .class_name = "generic mpegvideo encoder",
104 .item_name = av_default_item_name,
105 .option = mpv_generic_options,
106 .version = LIBAVUTIL_VERSION_INT,
107 };
108
109 3403 void ff_convert_matrix(MPVEncContext *const s, int (*qmat)[64],
110 uint16_t (*qmat16)[2][64],
111 const uint16_t *quant_matrix,
112 int bias, int qmin, int qmax, int intra)
113 {
114 3403 FDCTDSPContext *fdsp = &s->fdsp;
115 int qscale;
116 3403 int shift = 0;
117
118
2/2
✓ Branch 0 taken 99037 times.
✓ Branch 1 taken 3403 times.
102440 for (qscale = qmin; qscale <= qmax; qscale++) {
119 int i;
120 int qscale2;
121
122
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 99006 times.
99037 if (s->c.q_scale_type) qscale2 = ff_mpeg2_non_linear_qscale[qscale];
123 99006 else qscale2 = qscale << 1;
124
125
2/2
✓ Branch 0 taken 98287 times.
✓ Branch 1 taken 750 times.
99037 if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
126 #if CONFIG_FAANDCT
127
1/2
✓ Branch 0 taken 98287 times.
✗ Branch 1 not taken.
98287 fdsp->fdct == ff_faandct ||
128 #endif /* CONFIG_FAANDCT */
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98287 times.
98287 fdsp->fdct == ff_jpeg_fdct_islow_10) {
130
2/2
✓ Branch 0 taken 48000 times.
✓ Branch 1 taken 750 times.
48750 for (i = 0; i < 64; i++) {
131 48000 const int j = s->c.idsp.idct_permutation[i];
132 48000 int64_t den = (int64_t) qscale2 * quant_matrix[j];
133 /* 1 * 1 <= qscale2 * quant_matrix[j] <= 112 * 255
134 * Assume x = qscale2 * quant_matrix[j]
135 * 1 <= x <= 28560
136 * (1 << 22) / 1 >= (1 << 22) / (x) >= (1 << 22) / 28560
137 * 4194304 >= (1 << 22) / (x) >= 146 */
138
139 48000 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
140 }
141
2/2
✓ Branch 0 taken 97687 times.
✓ Branch 1 taken 600 times.
98287 } else if (fdsp->fdct == ff_fdct_ifast) {
142
2/2
✓ Branch 0 taken 6251968 times.
✓ Branch 1 taken 97687 times.
6349655 for (i = 0; i < 64; i++) {
143 6251968 const int j = s->c.idsp.idct_permutation[i];
144 6251968 int64_t den = ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j];
145 /* 1247 * 1 * 1 <= ff_aanscales[i] * qscale2 * quant_matrix[j] <= 31521 * 112 * 255
146 * Assume x = ff_aanscales[i] * qscale2 * quant_matrix[j]
147 * 1247 <= x <= 900239760
148 * (1 << 36) / 1247 >= (1 << 36) / (x) >= (1 << 36) / 900239760
149 * 55107840 >= (1 << 36) / (x) >= 76 */
150
151 6251968 qmat[qscale][i] = (int)((UINT64_C(2) << (QMAT_SHIFT + 14)) / den);
152 }
153 } else {
154
2/2
✓ Branch 0 taken 38400 times.
✓ Branch 1 taken 600 times.
39000 for (i = 0; i < 64; i++) {
155 38400 const int j = s->c.idsp.idct_permutation[i];
156 38400 int64_t den = (int64_t) qscale2 * quant_matrix[j];
157 /* 1 * 1 <= qscale2 * quant_matrix[j] <= 112 * 255
158 * Assume x = qscale2 * quant_matrix[j]
159 * 1 <= x <= 28560
160 * (1 << 22) / 1 >= (1 << 22) / (x) >= (1 << 22) / 28560
161 * 4194304 >= (1 << 22) / (x) >= 146
162 *
163 * 1 <= x <= 28560
164 * (1 << 17) / 1 >= (1 << 17) / (x) >= (1 << 17) / 28560
165 * 131072 >= (1 << 17) / (x) >= 4 */
166
167 38400 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
168 38400 qmat16[qscale][0][i] = (2 << QMAT_SHIFT_MMX) / den;
169
170
1/2
✓ Branch 0 taken 38400 times.
✗ Branch 1 not taken.
38400 if (qmat16[qscale][0][i] == 0 ||
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38400 times.
38400 qmat16[qscale][0][i] == 128 * 256)
172 qmat16[qscale][0][i] = 128 * 256 - 1;
173 38400 qmat16[qscale][1][i] =
174
2/2
✓ Branch 0 taken 26880 times.
✓ Branch 1 taken 11520 times.
38400 ROUNDED_DIV(bias * (1<<(16 - QUANT_BIAS_SHIFT)),
175 qmat16[qscale][0][i]);
176 }
177 }
178
179
2/2
✓ Branch 0 taken 6244941 times.
✓ Branch 1 taken 99037 times.
6343978 for (i = intra; i < 64; i++) {
180 6244941 int64_t max = 8191;
181
2/2
✓ Branch 0 taken 6159351 times.
✓ Branch 1 taken 85590 times.
6244941 if (fdsp->fdct == ff_fdct_ifast) {
182 6159351 max = (8191LL * ff_aanscales[i]) >> 14;
183 }
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6244941 times.
6244941 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
185 shift++;
186 }
187 }
188 }
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3403 times.
3403 if (shift) {
190 av_log(s->c.avctx, AV_LOG_INFO,
191 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
192 QMAT_SHIFT - shift);
193 }
194 3403 }
195
196 17515 static inline void update_qscale(MPVMainEncContext *const m)
197 {
198 17515 MPVEncContext *const s = &m->s;
199
200 if (s->c.q_scale_type == 1 && 0) {
201 int i;
202 int bestdiff=INT_MAX;
203 int best = 1;
204
205 for (i = 0 ; i<FF_ARRAY_ELEMS(ff_mpeg2_non_linear_qscale); i++) {
206 int diff = FFABS((ff_mpeg2_non_linear_qscale[i]<<(FF_LAMBDA_SHIFT + 6)) - (int)s->lambda * 139);
207 if (ff_mpeg2_non_linear_qscale[i] < s->c.avctx->qmin ||
208 (ff_mpeg2_non_linear_qscale[i] > s->c.avctx->qmax && !m->vbv_ignore_qmax))
209 continue;
210 if (diff < bestdiff) {
211 bestdiff = diff;
212 best = i;
213 }
214 }
215 s->c.qscale = best;
216 } else {
217 17515 s->c.qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
218 (FF_LAMBDA_SHIFT + 7);
219
2/2
✓ Branch 0 taken 17390 times.
✓ Branch 1 taken 125 times.
17515 s->c.qscale = av_clip(s->c.qscale, s->c.avctx->qmin, m->vbv_ignore_qmax ? 31 : s->c.avctx->qmax);
220 }
221
222 17515 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
223 FF_LAMBDA_SHIFT;
224 17515 }
225
226 1008 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
227 {
228 int i;
229
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1008 times.
1008 if (matrix) {
231 put_bits(pb, 1, 1);
232 for (i = 0; i < 64; i++) {
233 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
234 }
235 } else
236 1008 put_bits(pb, 1, 0);
237 1008 }
238
239 /**
240 * init s->c.cur_pic.qscale_table from s->lambda_table
241 */
242 850 static void init_qscale_tab(MPVEncContext *const s)
243 {
244 850 int8_t *const qscale_table = s->c.cur_pic.qscale_table;
245
246
2/2
✓ Branch 0 taken 259350 times.
✓ Branch 1 taken 850 times.
260200 for (int i = 0; i < s->c.mb_num; i++) {
247 259350 unsigned int lam = s->lambda_table[s->c.mb_index2xy[i]];
248 259350 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
249 259350 qscale_table[s->c.mb_index2xy[i]] = av_clip(qp, s->c.avctx->qmin,
250 259350 s->c.avctx->qmax);
251 }
252 850 }
253
254 2768 static void update_duplicate_context_after_me(MPVEncContext *const dst,
255 const MPVEncContext *const src)
256 {
257 #define COPY(a) dst->a = src->a
258 2768 COPY(c.pict_type);
259 2768 COPY(f_code);
260 2768 COPY(b_code);
261 2768 COPY(c.qscale);
262 2768 COPY(lambda);
263 2768 COPY(lambda2);
264 2768 COPY(c.frame_pred_frame_dct); // FIXME don't set in encode_header
265 2768 COPY(c.progressive_frame); // FIXME don't set in encode_header
266 2768 COPY(partitioned_frame); // FIXME don't set in encode_header
267 #undef COPY
268 2768 }
269
270 88 static av_cold void mpv_encode_init_static(void)
271 {
272
2/2
✓ Branch 0 taken 2816 times.
✓ Branch 1 taken 88 times.
2904 for (int i = -16; i < 16; i++)
273 2816 default_fcode_tab[i + MAX_MV] = 1;
274 88 }
275
276 /**
277 * Set the given MPVEncContext to defaults for encoding.
278 */
279 237 static av_cold void mpv_encode_defaults(MPVMainEncContext *const m)
280 {
281 237 MPVEncContext *const s = &m->s;
282 static AVOnce init_static_once = AV_ONCE_INIT;
283
284 237 ff_mpv_common_defaults(&s->c);
285
286 237 s->f_code = 1;
287 237 s->b_code = 1;
288
289
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 149 times.
237 if (!m->fcode_tab) {
290 88 m->fcode_tab = default_fcode_tab + MAX_MV;
291 88 ff_thread_once(&init_static_once, mpv_encode_init_static);
292 }
293
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 158 times.
237 if (!s->c.y_dc_scale_table) {
294 79 s->c.y_dc_scale_table =
295 79 s->c.c_dc_scale_table = ff_mpeg1_dc_scale_table;
296 }
297 237 }
298
299 310 av_cold void ff_dct_encode_init(MPVEncContext *const s)
300 {
301 310 s->dct_quantize = dct_quantize_c;
302
303 #if ARCH_X86
304 310 ff_dct_encode_init_x86(s);
305 #endif
306
307
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 274 times.
310 if (s->c.avctx->trellis)
308 36 s->dct_quantize = dct_quantize_trellis_c;
309 310 }
310
311 237 static av_cold void init_unquantize(MPVEncContext *const s2, AVCodecContext *avctx)
312 {
313 237 MpegEncContext *const s = &s2->c;
314 MPVUnquantDSPContext unquant_dsp_ctx;
315
316 237 ff_mpv_unquantize_init(&unquant_dsp_ctx,
317 avctx->flags & AV_CODEC_FLAG_BITEXACT, s->q_scale_type);
318
319
3/4
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 167 times.
237 if (s2->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
320 70 s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_mpeg2_intra;
321 70 s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter;
322
4/4
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 51 times.
167 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
323 116 s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_h263_intra;
324 116 s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_h263_inter;
325 } else {
326 51 s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_mpeg1_intra;
327 51 s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_mpeg1_inter;
328 }
329 237 }
330
331 237 static av_cold int me_cmp_init(MPVMainEncContext *const m, AVCodecContext *avctx)
332 {
333 237 MPVEncContext *const s = &m->s;
334 MECmpContext mecc;
335 me_cmp_func me_cmp[6];
336 int ret;
337
338 237 ff_me_cmp_init(&mecc, avctx);
339 237 ret = ff_me_init(&s->me, avctx, &mecc, 1);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (ret < 0)
341 return ret;
342 237 ret = ff_set_cmp(&mecc, me_cmp, m->frame_skip_cmp, 1);
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (ret < 0)
344 return ret;
345 237 m->frame_skip_cmp_fn = me_cmp[1];
346
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 220 times.
237 if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) {
347 17 ret = ff_set_cmp(&mecc, me_cmp, avctx->ildct_cmp, 1);
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
349 return ret;
350
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
17 if (!me_cmp[0] || !me_cmp[4])
351 return AVERROR(EINVAL);
352 17 s->ildct_cmp[0] = me_cmp[0];
353 17 s->ildct_cmp[1] = me_cmp[4];
354 }
355
356 237 s->sum_abs_dctelem = mecc.sum_abs_dctelem;
357
358 237 s->sse_cmp[0] = mecc.sse[0];
359 237 s->sse_cmp[1] = mecc.sse[1];
360 237 s->sad_cmp[0] = mecc.sad[0];
361 237 s->sad_cmp[1] = mecc.sad[1];
362
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 233 times.
237 if (avctx->mb_cmp == FF_CMP_NSSE) {
363 4 s->n_sse_cmp[0] = mecc.nsse[0];
364 4 s->n_sse_cmp[1] = mecc.nsse[1];
365 } else {
366 233 s->n_sse_cmp[0] = mecc.sse[0];
367 233 s->n_sse_cmp[1] = mecc.sse[1];
368 }
369
370 237 return 0;
371 }
372
373 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
374 237 static av_cold int init_matrices(MPVMainEncContext *const m, AVCodecContext *avctx)
375 {
376 237 MPVEncContext *const s = &m->s;
377
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 206 times.
237 const int nb_matrices = 1 + (s->c.out_format == FMT_MJPEG) + !m->intra_only;
378 const uint16_t *intra_matrix, *inter_matrix;
379 int ret;
380
381
1/2
✓ Branch 1 taken 237 times.
✗ Branch 2 not taken.
237 if (!ALLOCZ_ARRAYS(s->q_intra_matrix, 32, nb_matrices) ||
382
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 237 times.
237 !ALLOCZ_ARRAYS(s->q_intra_matrix16, 32, nb_matrices))
383 return AVERROR(ENOMEM);
384
385
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 206 times.
237 if (s->c.out_format == FMT_MJPEG) {
386 31 s->q_chroma_intra_matrix = s->q_intra_matrix + 32;
387 31 s->q_chroma_intra_matrix16 = s->q_intra_matrix16 + 32;
388 // No need to set q_inter_matrix
389 av_assert1(m->intra_only);
390 // intra_matrix, chroma_intra_matrix will be set later for MJPEG.
391 31 return 0;
392 } else {
393 206 s->q_chroma_intra_matrix = s->q_intra_matrix;
394 206 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
395 }
396
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 19 times.
206 if (!m->intra_only) {
397 187 s->q_inter_matrix = s->q_intra_matrix + 32;
398 187 s->q_inter_matrix16 = s->q_intra_matrix16 + 32;
399 }
400
401
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 134 times.
206 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4 &&
402
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 68 times.
72 s->mpeg_quant) {
403 4 intra_matrix = ff_mpeg4_default_intra_matrix;
404 4 inter_matrix = ff_mpeg4_default_non_intra_matrix;
405
4/4
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 86 times.
202 } else if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
406 116 intra_matrix =
407 116 inter_matrix = ff_mpeg1_default_non_intra_matrix;
408 } else {
409 /* MPEG-1/2, SpeedHQ */
410 86 intra_matrix = ff_mpeg1_default_intra_matrix;
411 86 inter_matrix = ff_mpeg1_default_non_intra_matrix;
412 }
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 if (avctx->intra_matrix)
414 intra_matrix = avctx->intra_matrix;
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 if (avctx->inter_matrix)
416 inter_matrix = avctx->inter_matrix;
417
418 /* init q matrix */
419
2/2
✓ Branch 0 taken 13184 times.
✓ Branch 1 taken 206 times.
13390 for (int i = 0; i < 64; i++) {
420 13184 int j = s->c.idsp.idct_permutation[i];
421
422 13184 s->c.intra_matrix[j] = s->c.chroma_intra_matrix[j] = intra_matrix[i];
423 13184 s->c.inter_matrix[j] = inter_matrix[i];
424 }
425
426 /* precompute matrix */
427 206 ret = ff_check_codec_matrices(avctx, FF_MATRIX_TYPE_INTRA | FF_MATRIX_TYPE_INTER, 1, 255);
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 if (ret < 0)
429 return ret;
430
431 206 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
432 206 s->c.intra_matrix, s->intra_quant_bias, avctx->qmin,
433 31, 1);
434
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 19 times.
206 if (s->q_inter_matrix)
435 187 ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
436 187 s->c.inter_matrix, s->inter_quant_bias, avctx->qmin,
437 31, 0);
438
439 206 return 0;
440 }
441
442 237 static av_cold int init_buffers(MPVMainEncContext *const m)
443 {
444 237 MPVEncContext *const s = &m->s;
445 237 int has_b_frames = !!m->max_b_frames;
446 int16_t (*mv_table)[2];
447
448 /* Allocate MB type table */
449 237 unsigned mb_array_size = s->c.mb_stride * s->c.mb_height;
450 237 s->mb_type = av_calloc(mb_array_size, 3 * sizeof(*s->mb_type) + sizeof(*s->mb_mean));
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (!s->mb_type)
452 return AVERROR(ENOMEM);
453 237 s->mc_mb_var = s->mb_type + mb_array_size;
454 237 s->mb_var = s->mc_mb_var + mb_array_size;
455 237 s->mb_mean = (uint8_t*)(s->mb_var + mb_array_size);
456
457
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 237 times.
237 if (!FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size))
458 return AVERROR(ENOMEM);
459
460 237 unsigned mv_table_size = (s->c.mb_height + 2) * s->c.mb_stride + 1;
461 237 unsigned nb_mv_tables = 1 + 5 * has_b_frames;
462
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 72 times.
237 if (s->c.codec_id == AV_CODEC_ID_MPEG4 ||
463
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 149 times.
165 (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
464 88 nb_mv_tables += 8 * has_b_frames;
465 88 s->p_field_select_table[0] = av_calloc(mv_table_size, 2 * (2 + 4 * has_b_frames));
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if (!s->p_field_select_table[0])
467 return AVERROR(ENOMEM);
468 88 s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size;
469 }
470
471 237 mv_table = av_calloc(mv_table_size, nb_mv_tables * sizeof(*mv_table));
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (!mv_table)
473 return AVERROR(ENOMEM);
474 237 m->mv_table_base = mv_table;
475 237 mv_table += s->c.mb_stride + 1;
476
477 237 s->p_mv_table = mv_table;
478
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 191 times.
237 if (has_b_frames) {
479 46 s->b_forw_mv_table = mv_table += mv_table_size;
480 46 s->b_back_mv_table = mv_table += mv_table_size;
481 46 s->b_bidir_forw_mv_table = mv_table += mv_table_size;
482 46 s->b_bidir_back_mv_table = mv_table += mv_table_size;
483 46 s->b_direct_mv_table = mv_table += mv_table_size;
484
485
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 12 times.
46 if (s->p_field_select_table[1]) { // MPEG-4 or INTERLACED_ME above
486 34 uint8_t *field_select = s->p_field_select_table[1];
487
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34 times.
102 for (int j = 0; j < 2; j++) {
488
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 68 times.
204 for (int k = 0; k < 2; k++) {
489
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 136 times.
408 for (int l = 0; l < 2; l++)
490 272 s->b_field_mv_table[j][k][l] = mv_table += mv_table_size;
491 136 s->b_field_select_table[j][k] = field_select += 2 * mv_table_size;
492 }
493 }
494 }
495 }
496
497 237 return 0;
498 }
499
500 237 static av_cold int init_slice_buffers(MPVMainEncContext *const m)
501 {
502 237 MPVEncContext *const s = &m->s;
503 // Align the following per-thread buffers to avoid false sharing.
504 enum {
505 #ifndef _MSC_VER
506 /// The number is supposed to match/exceed the cache-line size.
507 ALIGN = FFMAX(128, _Alignof(max_align_t)),
508 #else
509 ALIGN = 128,
510 #endif
511 DCT_ERROR_SIZE = FFALIGN(2 * sizeof(*s->dct_error_sum), ALIGN),
512 };
513 static_assert(DCT_ERROR_SIZE * MAX_THREADS + ALIGN - 1 <= SIZE_MAX,
514 "Need checks for potential overflow.");
515 237 unsigned nb_slices = s->c.slice_context_count;
516 237 char *dct_error = NULL;
517
518
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 230 times.
237 if (m->noise_reduction) {
519
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_offset, 2))
520 return AVERROR(ENOMEM);
521 7 dct_error = av_mallocz(ALIGN - 1 + nb_slices * DCT_ERROR_SIZE);
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!dct_error)
523 return AVERROR(ENOMEM);
524 7 m->dct_error_sum_base = dct_error;
525 7 dct_error += FFALIGN((uintptr_t)dct_error, ALIGN) - (uintptr_t)dct_error;
526 }
527
528 237 const int y_size = s->c.b8_stride * (2 * s->c.mb_height + 1);
529 237 const int c_size = s->c.mb_stride * (s->c.mb_height + 1);
530 237 const int yc_size = y_size + 2 * c_size;
531 237 ptrdiff_t offset = 0;
532
533
2/2
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 237 times.
572 for (unsigned i = 0; i < nb_slices; ++i) {
534 335 MPVEncContext *const s2 = s->c.enc_contexts[i];
535
536 335 s2->block = s2->blocks[0];
537
538
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 328 times.
335 if (dct_error) {
539 7 s2->dct_offset = s->dct_offset;
540 7 s2->dct_error_sum = (void*)dct_error;
541 7 dct_error += DCT_ERROR_SIZE;
542 }
543
544
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 208 times.
335 if (s2->c.ac_val) {
545 127 s2->c.dc_val += offset + i;
546 127 s2->c.ac_val += offset;
547 127 offset += yc_size;
548 }
549 }
550 237 return 0;
551 }
552
553 /* init video encoder */
554 237 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
555 {
556 237 MPVMainEncContext *const m = avctx->priv_data;
557 237 MPVEncContext *const s = &m->s;
558 AVCPBProperties *cpb_props;
559 int gcd, ret;
560
561 237 mpv_encode_defaults(m);
562
563
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 217 times.
237 switch (avctx->pix_fmt) {
564 8 case AV_PIX_FMT_YUVJ444P:
565 case AV_PIX_FMT_YUV444P:
566 8 s->c.chroma_format = CHROMA_444;
567 8 break;
568 12 case AV_PIX_FMT_YUVJ422P:
569 case AV_PIX_FMT_YUV422P:
570 12 s->c.chroma_format = CHROMA_422;
571 12 break;
572 default:
573 av_unreachable("Already checked via CODEC_PIXFMTS");
574 217 case AV_PIX_FMT_YUVJ420P:
575 case AV_PIX_FMT_YUV420P:
576 217 s->c.chroma_format = CHROMA_420;
577 217 break;
578 }
579
580 237 avctx->bits_per_raw_sample = av_clip(avctx->bits_per_raw_sample, 0, 8);
581
582 237 m->bit_rate = avctx->bit_rate;
583 237 s->c.width = avctx->width;
584 237 s->c.height = avctx->height;
585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (avctx->gop_size > 600 &&
586 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
587 av_log(avctx, AV_LOG_WARNING,
588 "keyframe interval too large!, reducing it from %d to %d\n",
589 avctx->gop_size, 600);
590 avctx->gop_size = 600;
591 }
592 237 m->gop_size = avctx->gop_size;
593 237 s->c.avctx = avctx;
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (avctx->max_b_frames > MPVENC_MAX_B_FRAMES) {
595 av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
596 "is " AV_STRINGIFY(MPVENC_MAX_B_FRAMES) ".\n");
597 avctx->max_b_frames = MPVENC_MAX_B_FRAMES;
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 } else if (avctx->max_b_frames < 0) {
599 av_log(avctx, AV_LOG_ERROR,
600 "max b frames must be 0 or positive for mpegvideo based encoders\n");
601 return AVERROR(EINVAL);
602 }
603 237 m->max_b_frames = avctx->max_b_frames;
604 237 s->c.codec_id = avctx->codec->id;
605
3/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 191 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
237 if (m->max_b_frames && !(avctx->codec->capabilities & AV_CODEC_CAP_DELAY)) {
606 av_log(avctx, AV_LOG_ERROR, "B-frames not supported by codec\n");
607 return AVERROR(EINVAL);
608 }
609
610 237 s->c.quarter_sample = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
611 237 s->rtp_mode = !!s->rtp_payload_size;
612 237 m->user_specified_pts = AV_NOPTS_VALUE;
613
614
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 227 times.
237 if (m->gop_size <= 1) {
615 10 m->intra_only = 1;
616 10 m->gop_size = 12;
617 } else {
618 227 m->intra_only = 0;
619 }
620
621 /* Fixed QSCALE */
622 237 m->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
623
624 711 s->adaptive_quant = (avctx->lumi_masking ||
625
1/2
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
237 avctx->dark_masking ||
626
1/2
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
237 avctx->temporal_cplx_masking ||
627
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 5 times.
237 avctx->spatial_cplx_masking ||
628
1/2
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
232 avctx->p_masking ||
629
1/2
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
232 m->border_masking ||
630
3/4
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 220 times.
486 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
631
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 !m->fixed_qscale;
632
633 237 s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
634
635
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 235 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
237 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
636 switch(avctx->codec_id) {
637 case AV_CODEC_ID_MPEG1VIDEO:
638 case AV_CODEC_ID_MPEG2VIDEO:
639 avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112LL / 15000000 * 16384;
640 break;
641 case AV_CODEC_ID_MPEG4:
642 case AV_CODEC_ID_MSMPEG4V1:
643 case AV_CODEC_ID_MSMPEG4V2:
644 case AV_CODEC_ID_MSMPEG4V3:
645 if (avctx->rc_max_rate >= 15000000) {
646 avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000LL) * (760-320) / (38400000 - 15000000);
647 } else if(avctx->rc_max_rate >= 2000000) {
648 avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000LL) * (320- 80) / (15000000 - 2000000);
649 } else if(avctx->rc_max_rate >= 384000) {
650 avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000LL) * ( 80- 40) / ( 2000000 - 384000);
651 } else
652 avctx->rc_buffer_size = 40;
653 avctx->rc_buffer_size *= 16384;
654 break;
655 }
656 if (avctx->rc_buffer_size) {
657 av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
658 }
659 }
660
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
662 av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
663 return AVERROR(EINVAL);
664 }
665
666
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 235 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
237 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
667 av_log(avctx, AV_LOG_INFO,
668 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
669 }
670
671
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 235 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
237 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
672 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
673 return AVERROR(EINVAL);
674 }
675
676
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 235 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
237 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
677 av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
678 return AVERROR(EINVAL);
679 }
680
681
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 235 times.
237 if (avctx->rc_max_rate &&
682
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 avctx->rc_max_rate == avctx->bit_rate &&
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 avctx->rc_max_rate != avctx->rc_min_rate) {
684 av_log(avctx, AV_LOG_INFO,
685 "impossible bitrate constraints, this will fail\n");
686 }
687
688
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 235 times.
237 if (avctx->rc_buffer_size &&
689 2 avctx->bit_rate * (int64_t)avctx->time_base.num >
690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
691 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
692 return AVERROR(EINVAL);
693 }
694
695
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 157 times.
237 if (!m->fixed_qscale &&
696 80 avctx->bit_rate * av_q2d(avctx->time_base) >
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 avctx->bit_rate_tolerance) {
698 double nbt = avctx->bit_rate * av_q2d(avctx->time_base) * 5;
699 av_log(avctx, AV_LOG_WARNING,
700 "bitrate tolerance %d too small for bitrate %"PRId64", overriding\n", avctx->bit_rate_tolerance, avctx->bit_rate);
701 if (nbt <= INT_MAX) {
702 avctx->bit_rate_tolerance = nbt;
703 } else
704 avctx->bit_rate_tolerance = INT_MAX;
705 }
706
707
3/4
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
237 if ((avctx->flags & AV_CODEC_FLAG_4MV) && s->c.codec_id != AV_CODEC_ID_MPEG4 &&
708 s->c.codec_id != AV_CODEC_ID_H263 && s->c.codec_id != AV_CODEC_ID_H263P &&
709 s->c.codec_id != AV_CODEC_ID_FLV1) {
710 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
711 return AVERROR(EINVAL);
712 }
713
714
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 234 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
237 if (s->c.obmc && avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
715 av_log(avctx, AV_LOG_ERROR,
716 "OBMC is only supported with simple mb decision\n");
717 return AVERROR(EINVAL);
718 }
719
720
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 233 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
237 if (s->c.quarter_sample && s->c.codec_id != AV_CODEC_ID_MPEG4) {
721 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
722 return AVERROR(EINVAL);
723 }
724
725
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 72 times.
237 if ((s->c.codec_id == AV_CODEC_ID_MPEG4 ||
726
2/2
✓ Branch 0 taken 159 times.
✓ Branch 1 taken 6 times.
165 s->c.codec_id == AV_CODEC_ID_H263 ||
727
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 156 times.
159 s->c.codec_id == AV_CODEC_ID_H263P) &&
728
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 (avctx->sample_aspect_ratio.num > 255 ||
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
81 avctx->sample_aspect_ratio.den > 255)) {
730 av_log(avctx, AV_LOG_WARNING,
731 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
732 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
733 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
734 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
735 }
736
737
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 6 times.
237 if ((s->c.codec_id == AV_CODEC_ID_H263 ||
738
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 228 times.
231 s->c.codec_id == AV_CODEC_ID_H263P) &&
739
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 (avctx->width > 2048 ||
740
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 avctx->height > 1152 )) {
741 av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
742 return AVERROR(EINVAL);
743 }
744
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 230 times.
237 if (s->c.codec_id == AV_CODEC_ID_FLV1 &&
745
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 (avctx->width > 65535 ||
746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 avctx->height > 65535 )) {
747 av_log(avctx, AV_LOG_ERROR, "FLV does not support resolutions above 16bit\n");
748 return AVERROR(EINVAL);
749 }
750
2/2
✓ Branch 0 taken 231 times.
✓ Branch 1 taken 6 times.
237 if ((s->c.codec_id == AV_CODEC_ID_H263 ||
751
2/2
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 3 times.
231 s->c.codec_id == AV_CODEC_ID_H263P ||
752
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 225 times.
228 s->c.codec_id == AV_CODEC_ID_RV20) &&
753
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 ((avctx->width &3) ||
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 (avctx->height&3) )) {
755 av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
756 return AVERROR(EINVAL);
757 }
758
759
2/2
✓ Branch 0 taken 233 times.
✓ Branch 1 taken 4 times.
237 if ((s->c.codec_id == AV_CODEC_ID_WMV1 ||
760
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 229 times.
233 s->c.codec_id == AV_CODEC_ID_WMV2) &&
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 avctx->width & 1) {
762 av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
763 return AVERROR(EINVAL);
764 }
765
766
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 220 times.
237 if ((avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME)) &&
767
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
17 s->c.codec_id != AV_CODEC_ID_MPEG4 && s->c.codec_id != AV_CODEC_ID_MPEG2VIDEO) {
768 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
769 return AVERROR(EINVAL);
770 }
771
772
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
237 if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
773 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
774 return AVERROR(EINVAL);
775 }
776
777
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 225 times.
237 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 avctx->mb_decision != FF_MB_DECISION_RD) {
779 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=rd\n");
780 return AVERROR(EINVAL);
781 }
782
783
2/2
✓ Branch 0 taken 234 times.
✓ Branch 1 taken 3 times.
237 if (m->scenechange_threshold < 1000000000 &&
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 234 times.
234 (avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)) {
785 av_log(avctx, AV_LOG_ERROR,
786 "closed gop with scene change detection are not supported yet, "
787 "set threshold to 1000000000\n");
788 return AVERROR_PATCHWELCOME;
789 }
790
791
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 236 times.
237 if (avctx->flags & AV_CODEC_FLAG_LOW_DELAY) {
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->c.codec_id != AV_CODEC_ID_MPEG2VIDEO &&
793 avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
794 av_log(avctx, AV_LOG_ERROR,
795 "low delay forcing is only available for mpeg2, "
796 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
797 return AVERROR(EINVAL);
798 }
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m->max_b_frames != 0) {
800 av_log(avctx, AV_LOG_ERROR,
801 "B-frames cannot be used with low delay\n");
802 return AVERROR(EINVAL);
803 }
804 }
805
806
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 225 times.
237 if (avctx->slices > 1 &&
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 !(avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS)) {
808 av_log(avctx, AV_LOG_ERROR, "Multiple slices are not supported by this codec\n");
809 return AVERROR(EINVAL);
810 }
811
812
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 232 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
237 if (m->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
813 av_log(avctx, AV_LOG_INFO,
814 "notice: b_frame_strategy only affects the first pass\n");
815 m->b_frame_strategy = 0;
816 }
817
818 237 gcd = av_gcd(avctx->time_base.den, avctx->time_base.num);
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (gcd > 1) {
820 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
821 avctx->time_base.den /= gcd;
822 avctx->time_base.num /= gcd;
823 //return -1;
824 }
825
826
11/12
✓ Branch 0 taken 167 times.
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 156 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 156 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 129 times.
✓ Branch 7 taken 27 times.
✓ Branch 8 taken 125 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 9 times.
✓ Branch 11 taken 116 times.
237 if (s->mpeg_quant || s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO || s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO || s->c.codec_id == AV_CODEC_ID_MJPEG || s->c.codec_id == AV_CODEC_ID_AMV || s->c.codec_id == AV_CODEC_ID_SPEEDHQ) {
827 // (a + x * 3 / 8) / x
828 121 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
829 121 s->inter_quant_bias = 0;
830 } else {
831 116 s->intra_quant_bias = 0;
832 // (a - x / 4) / x
833 116 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
834 }
835
836
2/4
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 237 times.
237 if (avctx->qmin > avctx->qmax || avctx->qmin <= 0) {
837 av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are invalid, they must be 0 < min <= max\n");
838 return AVERROR(EINVAL);
839 }
840
841 237 av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
842
843
15/16
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 7 times.
✓ Branch 8 taken 4 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 72 times.
✓ Branch 11 taken 4 times.
✓ Branch 12 taken 5 times.
✓ Branch 13 taken 4 times.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
237 switch (avctx->codec->id) {
844 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
845 66 case AV_CODEC_ID_MPEG2VIDEO:
846 66 s->rtp_mode = 1;
847 av_fallthrough;
848 77 case AV_CODEC_ID_MPEG1VIDEO:
849 77 s->c.out_format = FMT_MPEG1;
850 77 s->c.low_delay = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
851
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1 times.
77 avctx->delay = s->c.low_delay ? 0 : (m->max_b_frames + 1);
852 77 break;
853 #endif
854 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
855 31 case AV_CODEC_ID_MJPEG:
856 case AV_CODEC_ID_AMV:
857 31 s->c.out_format = FMT_MJPEG;
858 31 m->intra_only = 1; /* force intra only for jpeg */
859 31 avctx->delay = 0;
860 31 s->c.low_delay = 1;
861 31 break;
862 #endif
863 9 case AV_CODEC_ID_SPEEDHQ:
864 9 s->c.out_format = FMT_SPEEDHQ;
865 9 m->intra_only = 1; /* force intra only for SHQ */
866 9 avctx->delay = 0;
867 9 s->c.low_delay = 1;
868 9 break;
869 8 case AV_CODEC_ID_H261:
870 8 s->c.out_format = FMT_H261;
871 8 avctx->delay = 0;
872 8 s->c.low_delay = 1;
873 8 s->rtp_mode = 0; /* Sliced encoding not supported */
874 8 break;
875 6 case AV_CODEC_ID_H263:
876 if (!CONFIG_H263_ENCODER)
877 return AVERROR_ENCODER_NOT_FOUND;
878
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
879 s->c.width, s->c.height) == 8) {
880 av_log(avctx, AV_LOG_ERROR,
881 "The specified picture size of %dx%d is not valid for "
882 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
883 "352x288, 704x576, and 1408x1152. "
884 "Try H.263+.\n", s->c.width, s->c.height);
885 return AVERROR(EINVAL);
886 }
887 6 s->c.out_format = FMT_H263;
888 6 avctx->delay = 0;
889 6 s->c.low_delay = 1;
890 6 break;
891 3 case AV_CODEC_ID_H263P:
892 3 s->c.out_format = FMT_H263;
893 /* Fx */
894 3 s->c.h263_aic = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
895 3 s->modified_quant = s->c.h263_aic;
896 3 s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
897
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 s->me.unrestricted_mv = s->c.obmc || s->loop_filter || s->umvplus;
898 3 s->flipflop_rounding = 1;
899
900 /* /Fx */
901 /* These are just to be sure */
902 3 avctx->delay = 0;
903 3 s->c.low_delay = 1;
904 3 break;
905 7 case AV_CODEC_ID_FLV1:
906 7 s->c.out_format = FMT_H263;
907 7 s->me.unrestricted_mv = 1;
908 7 s->rtp_mode = 0; /* don't allow GOB */
909 7 avctx->delay = 0;
910 7 s->c.low_delay = 1;
911 7 break;
912 #if CONFIG_RV10_ENCODER
913 4 case AV_CODEC_ID_RV10:
914 4 s->c.out_format = FMT_H263;
915 4 avctx->delay = 0;
916 4 s->c.low_delay = 1;
917 4 break;
918 #endif
919 #if CONFIG_RV20_ENCODER
920 3 case AV_CODEC_ID_RV20:
921 3 m->encode_picture_header = ff_rv20_encode_picture_header;
922 3 s->c.out_format = FMT_H263;
923 3 avctx->delay = 0;
924 3 s->c.low_delay = 1;
925 3 s->modified_quant = 1;
926 // Set here to force allocation of dc_val;
927 // will be set later on a per-frame basis.
928 3 s->c.h263_aic = 1;
929 3 s->loop_filter = 1;
930 3 s->me.unrestricted_mv = 0;
931 3 break;
932 #endif
933 72 case AV_CODEC_ID_MPEG4:
934 72 s->c.out_format = FMT_H263;
935 72 s->c.h263_pred = 1;
936 72 s->me.unrestricted_mv = 1;
937 72 s->flipflop_rounding = 1;
938 72 s->c.low_delay = m->max_b_frames ? 0 : 1;
939
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 50 times.
72 avctx->delay = s->c.low_delay ? 0 : (m->max_b_frames + 1);
940 72 break;
941 4 case AV_CODEC_ID_MSMPEG4V2:
942 4 s->c.out_format = FMT_H263;
943 4 s->c.h263_pred = 1;
944 4 s->me.unrestricted_mv = 1;
945 4 s->c.msmpeg4_version = MSMP4_V2;
946 4 avctx->delay = 0;
947 4 s->c.low_delay = 1;
948 4 break;
949 5 case AV_CODEC_ID_MSMPEG4V3:
950 5 s->c.out_format = FMT_H263;
951 5 s->c.h263_pred = 1;
952 5 s->me.unrestricted_mv = 1;
953 5 s->c.msmpeg4_version = MSMP4_V3;
954 5 s->flipflop_rounding = 1;
955 5 avctx->delay = 0;
956 5 s->c.low_delay = 1;
957 5 break;
958 4 case AV_CODEC_ID_WMV1:
959 4 s->c.out_format = FMT_H263;
960 4 s->c.h263_pred = 1;
961 4 s->me.unrestricted_mv = 1;
962 4 s->c.msmpeg4_version = MSMP4_WMV1;
963 4 s->flipflop_rounding = 1;
964 4 avctx->delay = 0;
965 4 s->c.low_delay = 1;
966 4 break;
967 4 case AV_CODEC_ID_WMV2:
968 4 s->c.out_format = FMT_H263;
969 4 s->c.h263_pred = 1;
970 4 s->me.unrestricted_mv = 1;
971 4 s->c.msmpeg4_version = MSMP4_WMV2;
972 4 s->flipflop_rounding = 1;
973 4 avctx->delay = 0;
974 4 s->c.low_delay = 1;
975 4 break;
976 default:
977 av_unreachable("List contains all codecs using ff_mpv_encode_init()");
978 }
979
980 237 avctx->has_b_frames = !s->c.low_delay;
981
982 237 s->c.encoding = 1;
983
984 237 s->c.progressive_frame =
985
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 17 times.
457 s->c.progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
986 AV_CODEC_FLAG_INTERLACED_ME) ||
987
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 4 times.
220 s->c.alternate_scan);
988
989
3/4
✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 205 times.
✓ Branch 3 taken 32 times.
237 if (avctx->flags & AV_CODEC_FLAG_PSNR || avctx->mb_decision == FF_MB_DECISION_RD ||
990
2/4
✓ Branch 0 taken 205 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 205 times.
205 m->frame_skip_threshold || m->frame_skip_factor) {
991 32 s->frame_reconstruction_bitfield = (1 << AV_PICTURE_TYPE_I) |
992 (1 << AV_PICTURE_TYPE_P) |
993 (1 << AV_PICTURE_TYPE_B);
994
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 50 times.
205 } else if (!m->intra_only) {
995 155 s->frame_reconstruction_bitfield = (1 << AV_PICTURE_TYPE_I) |
996 (1 << AV_PICTURE_TYPE_P);
997 } else {
998 50 s->frame_reconstruction_bitfield = 0;
999 }
1000
1001
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (m->lmin > m->lmax) {
1002 av_log(avctx, AV_LOG_WARNING, "Clipping lmin value to %d\n", m->lmax);
1003 m->lmin = m->lmax;
1004 }
1005
1006 /* ff_mpv_init_duplicate_contexts() will copy (memdup) the contents of the
1007 * main slice to the slice contexts, so we initialize various fields of it
1008 * before calling ff_mpv_init_duplicate_contexts(). */
1009 237 s->parent = m;
1010 237 ff_mpv_idct_init(&s->c);
1011 237 init_unquantize(s, avctx);
1012 237 ff_fdctdsp_init(&s->fdsp, avctx);
1013 237 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
1014 237 ff_pixblockdsp_init(&s->pdsp, 8);
1015 237 ret = me_cmp_init(m, avctx);
1016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (ret < 0)
1017 return ret;
1018
1019
1/2
✓ Branch 1 taken 237 times.
✗ Branch 2 not taken.
237 if (!(avctx->stats_out = av_mallocz(256)) ||
1020
1/2
✓ Branch 1 taken 237 times.
✗ Branch 2 not taken.
237 !(s->new_pic = av_frame_alloc()) ||
1021
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 237 times.
237 !(s->c.picture_pool = ff_mpv_alloc_pic_pool(0)))
1022 return AVERROR(ENOMEM);
1023
1024 237 ret = init_matrices(m, avctx);
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (ret < 0)
1026 return ret;
1027
1028 237 ff_dct_encode_init(s);
1029
1030
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 125 times.
237 if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263) {
1031 112 ff_h263_encode_init(m);
1032 #if CONFIG_MSMPEG4ENC
1033
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 95 times.
112 if (s->c.msmpeg4_version != MSMP4_UNUSED)
1034 17 ff_msmpeg4_encode_init(m);
1035 #endif
1036 }
1037
1038 237 s->c.slice_ctx_size = sizeof(*s);
1039 237 ret = ff_mpv_common_init(&s->c);
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (ret < 0)
1041 return ret;
1042 237 ret = init_buffers(m);
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (ret < 0)
1044 return ret;
1045
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 207 times.
237 if (s->c.slice_context_count > 1) {
1046 30 s->rtp_mode = 1;
1047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (avctx->codec_id == AV_CODEC_ID_H263P)
1048 s->h263_slice_structured = 1;
1049 }
1050 237 ret = ff_mpv_init_duplicate_contexts(&s->c);
1051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (ret < 0)
1052 return ret;
1053
1054 237 ret = init_slice_buffers(m);
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (ret < 0)
1056 return ret;
1057
1058 237 ret = ff_rate_control_init(m);
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (ret < 0)
1060 return ret;
1061
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (m->b_frame_strategy == 2) {
1063 for (int i = 0; i < m->max_b_frames + 2; i++) {
1064 m->tmp_frames[i] = av_frame_alloc();
1065 if (!m->tmp_frames[i])
1066 return AVERROR(ENOMEM);
1067
1068 m->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
1069 m->tmp_frames[i]->width = s->c.width >> m->brd_scale;
1070 m->tmp_frames[i]->height = s->c.height >> m->brd_scale;
1071
1072 ret = av_frame_get_buffer(m->tmp_frames[i], 0);
1073 if (ret < 0)
1074 return ret;
1075 }
1076 }
1077
1078 237 cpb_props = ff_encode_add_cpb_side_data(avctx);
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
237 if (!cpb_props)
1080 return AVERROR(ENOMEM);
1081 237 cpb_props->max_bitrate = avctx->rc_max_rate;
1082 237 cpb_props->min_bitrate = avctx->rc_min_rate;
1083 237 cpb_props->avg_bitrate = avctx->bit_rate;
1084 237 cpb_props->buffer_size = avctx->rc_buffer_size;
1085
1086 237 return 0;
1087 }
1088
1089 237 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
1090 {
1091 237 MPVMainEncContext *const m = avctx->priv_data;
1092 237 MPVEncContext *const s = &m->s;
1093
1094 237 ff_rate_control_uninit(&m->rc_context);
1095
1096 237 ff_mpv_common_end(&s->c);
1097 237 av_refstruct_pool_uninit(&s->c.picture_pool);
1098
1099
2/2
✓ Branch 0 taken 4029 times.
✓ Branch 1 taken 237 times.
4266 for (int i = 0; i < MPVENC_MAX_B_FRAMES + 1; i++) {
1100 4029 av_refstruct_unref(&m->input_picture[i]);
1101 4029 av_refstruct_unref(&m->reordered_input_picture[i]);
1102 }
1103
2/2
✓ Branch 0 taken 4266 times.
✓ Branch 1 taken 237 times.
4503 for (int i = 0; i < FF_ARRAY_ELEMS(m->tmp_frames); i++)
1104 4266 av_frame_free(&m->tmp_frames[i]);
1105
1106 237 av_frame_free(&s->new_pic);
1107
1108 237 av_freep(&avctx->stats_out);
1109
1110 237 av_freep(&m->mv_table_base);
1111 237 av_freep(&s->p_field_select_table[0]);
1112 237 av_freep(&m->dct_error_sum_base);
1113
1114 237 av_freep(&s->mb_type);
1115 237 av_freep(&s->lambda_table);
1116
1117 237 av_freep(&s->q_intra_matrix);
1118 237 av_freep(&s->q_intra_matrix16);
1119 237 av_freep(&s->dct_offset);
1120
1121 237 return 0;
1122 }
1123
1124 /* put block[] to dest[] */
1125 3374798 static inline void put_dct(MPVEncContext *const s,
1126 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1127 {
1128 3374798 s->c.dct_unquantize_intra(&s->c, block, i, qscale);
1129 3374798 s->c.idsp.idct_put(dest, line_size, block);
1130 3374798 }
1131
1132 22818684 static inline void add_dequant_dct(MPVEncContext *const s,
1133 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1134 {
1135
2/2
✓ Branch 0 taken 8316915 times.
✓ Branch 1 taken 14501769 times.
22818684 if (s->c.block_last_index[i] >= 0) {
1136 8316915 s->c.dct_unquantize_inter(&s->c, block, i, qscale);
1137
1138 8316915 s->c.idsp.idct_add(dest, line_size, block);
1139 }
1140 22818684 }
1141
1142 /**
1143 * Performs dequantization and IDCT (if necessary)
1144 */
1145 5324781 static void mpv_reconstruct_mb(MPVEncContext *const s, int16_t block[12][64])
1146 {
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5324781 times.
5324781 if (s->c.avctx->debug & FF_DEBUG_DCT_COEFF) {
1148 /* print DCT coefficients */
1149 av_log(s->c.avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->c.mb_x, s->c.mb_y);
1150 for (int i = 0; i < 6; i++) {
1151 for (int j = 0; j < 64; j++) {
1152 av_log(s->c.avctx, AV_LOG_DEBUG, "%5d",
1153 block[i][s->c.idsp.idct_permutation[j]]);
1154 }
1155 av_log(s->c.avctx, AV_LOG_DEBUG, "\n");
1156 }
1157 }
1158
1159
2/2
✓ Branch 0 taken 4236762 times.
✓ Branch 1 taken 1088019 times.
5324781 if ((1 << s->c.pict_type) & s->frame_reconstruction_bitfield) {
1160 4236762 uint8_t *dest_y = s->c.dest[0], *dest_cb = s->c.dest[1], *dest_cr = s->c.dest[2];
1161 int dct_linesize, dct_offset;
1162 4236762 const int linesize = s->c.cur_pic.linesize[0];
1163 4236762 const int uvlinesize = s->c.cur_pic.linesize[1];
1164 4236762 const int block_size = 8;
1165
1166 4236762 dct_linesize = linesize << s->c.interlaced_dct;
1167
2/2
✓ Branch 0 taken 4224979 times.
✓ Branch 1 taken 11783 times.
4236762 dct_offset = s->c.interlaced_dct ? linesize : linesize * block_size;
1168
1169
2/2
✓ Branch 0 taken 3687216 times.
✓ Branch 1 taken 549546 times.
4236762 if (!s->c.mb_intra) {
1170 /* No MC, as that was already done otherwise */
1171 3687216 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->c.qscale);
1172 3687216 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->c.qscale);
1173 3687216 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->c.qscale);
1174 3687216 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->c.qscale);
1175
1176 if (!CONFIG_GRAY || !(s->c.avctx->flags & AV_CODEC_FLAG_GRAY)) {
1177
2/2
✓ Branch 0 taken 3339522 times.
✓ Branch 1 taken 347694 times.
3687216 if (s->c.chroma_y_shift) {
1178 3339522 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->c.chroma_qscale);
1179 3339522 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->c.chroma_qscale);
1180 } else {
1181 347694 dct_linesize >>= 1;
1182 347694 dct_offset >>= 1;
1183 347694 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->c.chroma_qscale);
1184 347694 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->c.chroma_qscale);
1185 347694 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->c.chroma_qscale);
1186 347694 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->c.chroma_qscale);
1187 }
1188 }
1189 } else {
1190 /* dct only in intra block */
1191 549546 put_dct(s, block[0], 0, dest_y , dct_linesize, s->c.qscale);
1192 549546 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->c.qscale);
1193 549546 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->c.qscale);
1194 549546 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->c.qscale);
1195
1196 if (!CONFIG_GRAY || !(s->c.avctx->flags & AV_CODEC_FLAG_GRAY)) {
1197
2/2
✓ Branch 0 taken 510785 times.
✓ Branch 1 taken 38761 times.
549546 if (s->c.chroma_y_shift) {
1198 510785 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->c.chroma_qscale);
1199 510785 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->c.chroma_qscale);
1200 } else {
1201 38761 dct_offset >>= 1;
1202 38761 dct_linesize >>= 1;
1203 38761 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->c.chroma_qscale);
1204 38761 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->c.chroma_qscale);
1205 38761 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->c.chroma_qscale);
1206 38761 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->c.chroma_qscale);
1207 }
1208 }
1209 }
1210 }
1211 5324781 }
1212
1213 61160 static int get_sae(const uint8_t *src, int ref, int stride)
1214 {
1215 int x,y;
1216 61160 int acc = 0;
1217
1218
2/2
✓ Branch 0 taken 978560 times.
✓ Branch 1 taken 61160 times.
1039720 for (y = 0; y < 16; y++) {
1219
2/2
✓ Branch 0 taken 15656960 times.
✓ Branch 1 taken 978560 times.
16635520 for (x = 0; x < 16; x++) {
1220 15656960 acc += FFABS(src[x + y * stride] - ref);
1221 }
1222 }
1223
1224 61160 return acc;
1225 }
1226
1227 198 static int get_intra_count(MPVEncContext *const s, const uint8_t *src,
1228 const uint8_t *ref, int stride)
1229 {
1230 int x, y, w, h;
1231 198 int acc = 0;
1232
1233 198 w = s->c.width & ~15;
1234 198 h = s->c.height & ~15;
1235
1236
2/2
✓ Branch 0 taken 2860 times.
✓ Branch 1 taken 198 times.
3058 for (y = 0; y < h; y += 16) {
1237
2/2
✓ Branch 0 taken 61160 times.
✓ Branch 1 taken 2860 times.
64020 for (x = 0; x < w; x += 16) {
1238 61160 int offset = x + y * stride;
1239 61160 int sad = s->sad_cmp[0](NULL, src + offset, ref + offset,
1240 stride, 16);
1241 61160 int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
1242 61160 int sae = get_sae(src + offset, mean, stride);
1243
1244 61160 acc += sae + 500 < sad;
1245 }
1246 }
1247 198 return acc;
1248 }
1249
1250 /**
1251 * Allocates new buffers for an AVFrame and copies the properties
1252 * from another AVFrame.
1253 */
1254 12175 static int prepare_picture(MPVEncContext *const s, AVFrame *f, const AVFrame *props_frame)
1255 {
1256 12175 AVCodecContext *avctx = s->c.avctx;
1257 int ret;
1258
1259 12175 f->width = avctx->width + 2 * EDGE_WIDTH;
1260 12175 f->height = avctx->height + 2 * EDGE_WIDTH;
1261
1262 12175 ret = ff_encode_alloc_frame(avctx, f);
1263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12175 times.
12175 if (ret < 0)
1264 return ret;
1265
1266 12175 ret = ff_mpv_pic_check_linesize(avctx, f, &s->c.linesize, &s->c.uvlinesize);
1267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12175 times.
12175 if (ret < 0)
1268 return ret;
1269
1270
2/2
✓ Branch 0 taken 36525 times.
✓ Branch 1 taken 12175 times.
48700 for (int i = 0; f->data[i]; i++) {
1271
2/2
✓ Branch 0 taken 24350 times.
✓ Branch 1 taken 12175 times.
36525 int offset = (EDGE_WIDTH >> (i ? s->c.chroma_y_shift : 0)) *
1272 36525 f->linesize[i] +
1273
2/2
✓ Branch 0 taken 24350 times.
✓ Branch 1 taken 12175 times.
36525 (EDGE_WIDTH >> (i ? s->c.chroma_x_shift : 0));
1274 36525 f->data[i] += offset;
1275 }
1276 12175 f->width = avctx->width;
1277 12175 f->height = avctx->height;
1278
1279 12175 ret = av_frame_copy_props(f, props_frame);
1280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12175 times.
12175 if (ret < 0)
1281 return ret;
1282
1283 12175 return 0;
1284 }
1285
1286 12422 static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg)
1287 {
1288 12422 MPVEncContext *const s = &m->s;
1289 12422 MPVPicture *pic = NULL;
1290 int64_t pts;
1291 12422 int display_picture_number = 0, ret;
1292 24844 int encoding_delay = m->max_b_frames ? m->max_b_frames
1293
2/2
✓ Branch 0 taken 2335 times.
✓ Branch 1 taken 10087 times.
12422 : (s->c.low_delay ? 0 : 1);
1294 12422 int flush_offset = 1;
1295 12422 int direct = 1;
1296
1297 av_assert1(!m->input_picture[0]);
1298
1299
2/2
✓ Branch 0 taken 12125 times.
✓ Branch 1 taken 297 times.
12422 if (pic_arg) {
1300 12125 pts = pic_arg->pts;
1301 12125 display_picture_number = m->input_picture_number++;
1302
1303
1/2
✓ Branch 0 taken 12125 times.
✗ Branch 1 not taken.
12125 if (pts != AV_NOPTS_VALUE) {
1304
2/2
✓ Branch 0 taken 2739 times.
✓ Branch 1 taken 9386 times.
12125 if (s->c.codec_id == AV_CODEC_ID_MPEG4 &&
1305
1/2
✓ Branch 0 taken 2739 times.
✗ Branch 1 not taken.
2739 (pts > INT64_MAX / 2 / s->c.avctx->time_base.num ||
1306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2739 times.
2739 pts < INT64_MIN / 2 / s->c.avctx->time_base.num)) {
1307 av_log(s->c.avctx, AV_LOG_ERROR, "pts %"PRId64" is out of the supported range\n", pts);
1308 return AVERROR_PATCHWELCOME;
1309 }
1310
1311
2/2
✓ Branch 0 taken 11888 times.
✓ Branch 1 taken 237 times.
12125 if (m->user_specified_pts != AV_NOPTS_VALUE) {
1312 11888 int64_t last = m->user_specified_pts;
1313
1314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11888 times.
11888 if (pts <= last) {
1315 av_log(s->c.avctx, AV_LOG_ERROR,
1316 "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
1317 pts, last);
1318 return AVERROR(EINVAL);
1319 }
1320
1321
4/4
✓ Branch 0 taken 6133 times.
✓ Branch 1 taken 5755 times.
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 6035 times.
11888 if (!s->c.low_delay && display_picture_number == 1)
1322 98 m->dts_delta = pts - last;
1323 }
1324 12125 m->user_specified_pts = pts;
1325 } else {
1326 if (m->user_specified_pts != AV_NOPTS_VALUE) {
1327 m->user_specified_pts =
1328 pts = m->user_specified_pts + 1;
1329 av_log(s->c.avctx, AV_LOG_INFO,
1330 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
1331 pts);
1332 } else {
1333 pts = display_picture_number;
1334 }
1335 }
1336
1337
2/2
✓ Branch 0 taken 795 times.
✓ Branch 1 taken 11330 times.
12125 if (pic_arg->linesize[0] != s->c.linesize ||
1338
1/2
✓ Branch 0 taken 795 times.
✗ Branch 1 not taken.
795 pic_arg->linesize[1] != s->c.uvlinesize ||
1339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 795 times.
795 pic_arg->linesize[2] != s->c.uvlinesize)
1340 11330 direct = 0;
1341
4/4
✓ Branch 0 taken 9074 times.
✓ Branch 1 taken 3051 times.
✓ Branch 2 taken 1261 times.
✓ Branch 3 taken 7813 times.
12125 if ((s->c.width & 15) || (s->c.height & 15))
1342 4312 direct = 0;
1343
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 11842 times.
12125 if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
1344 283 direct = 0;
1345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12125 times.
12125 if (s->c.linesize & (STRIDE_ALIGN-1))
1346 direct = 0;
1347
1348 ff_dlog(s->c.avctx, "%d %d %td %td\n", pic_arg->linesize[0],
1349 pic_arg->linesize[1], s->c.linesize, s->c.uvlinesize);
1350
1351 12125 pic = av_refstruct_pool_get(s->c.picture_pool);
1352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12125 times.
12125 if (!pic)
1353 return AVERROR(ENOMEM);
1354
1355
2/2
✓ Branch 0 taken 723 times.
✓ Branch 1 taken 11402 times.
12125 if (direct) {
1356
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 723 times.
723 if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1357 goto fail;
1358 723 pic->shared = 1;
1359 } else {
1360 11402 ret = prepare_picture(s, pic->f, pic_arg);
1361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11402 times.
11402 if (ret < 0)
1362 goto fail;
1363
1364
2/2
✓ Branch 0 taken 34206 times.
✓ Branch 1 taken 11402 times.
45608 for (int i = 0; i < 3; i++) {
1365 34206 ptrdiff_t src_stride = pic_arg->linesize[i];
1366
2/2
✓ Branch 0 taken 22804 times.
✓ Branch 1 taken 11402 times.
34206 ptrdiff_t dst_stride = i ? s->c.uvlinesize : s->c.linesize;
1367
2/2
✓ Branch 0 taken 22804 times.
✓ Branch 1 taken 11402 times.
34206 int h_shift = i ? s->c.chroma_x_shift : 0;
1368
2/2
✓ Branch 0 taken 22804 times.
✓ Branch 1 taken 11402 times.
34206 int v_shift = i ? s->c.chroma_y_shift : 0;
1369 34206 int w = AV_CEIL_RSHIFT(s->c.width , h_shift);
1370 34206 int h = AV_CEIL_RSHIFT(s->c.height, v_shift);
1371 34206 const uint8_t *src = pic_arg->data[i];
1372 34206 int vpad = 16;
1373
1374
2/2
✓ Branch 0 taken 13794 times.
✓ Branch 1 taken 20412 times.
34206 if ( s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO
1375
2/2
✓ Branch 0 taken 3075 times.
✓ Branch 1 taken 10719 times.
13794 && !s->c.progressive_sequence
1376
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 2325 times.
3075 && FFALIGN(s->c.height, 32) - s->c.height > 16)
1377 750 vpad = 32;
1378
1379
2/2
✓ Branch 0 taken 34056 times.
✓ Branch 1 taken 150 times.
34206 if (!s->c.avctx->rc_buffer_size)
1380 34056 pic->f->data[i] += INPLACE_OFFSET;
1381 34206 uint8_t *dst = pic->f->data[i];
1382
1383
2/2
✓ Branch 0 taken 316 times.
✓ Branch 1 taken 33890 times.
34206 if (src_stride == dst_stride)
1384 316 memcpy(dst, src, src_stride * h - src_stride + w);
1385 else {
1386 33890 int h2 = h;
1387 33890 uint8_t *dst2 = dst;
1388
2/2
✓ Branch 0 taken 5060124 times.
✓ Branch 1 taken 33890 times.
5094014 while (h2--) {
1389 5060124 memcpy(dst2, src, w);
1390 5060124 dst2 += dst_stride;
1391 5060124 src += src_stride;
1392 }
1393 }
1394
4/4
✓ Branch 0 taken 25053 times.
✓ Branch 1 taken 9153 times.
✓ Branch 2 taken 3783 times.
✓ Branch 3 taken 21270 times.
34206 if ((s->c.width & 15) || (s->c.height & (vpad-1))) {
1395 12936 s->mpvencdsp.draw_edges(dst, dst_stride,
1396 w, h,
1397 16 >> h_shift,
1398 vpad >> v_shift,
1399 EDGE_BOTTOM);
1400 }
1401 }
1402 }
1403
1404 12125 pic->display_picture_number = display_picture_number;
1405 12125 pic->f->pts = pts; // we set this here to avoid modifying pic_arg
1406
2/2
✓ Branch 0 taken 244 times.
✓ Branch 1 taken 53 times.
297 } else if (!m->reordered_input_picture[1]) {
1407 /* Flushing: When the above check is true, the encoder is about to run
1408 * out of frames to encode. Check if there are input_pictures left;
1409 * if so, ensure m->input_picture[0] contains the first picture.
1410 * A flush_offset != 1 will only happen if we did not receive enough
1411 * input frames. */
1412
2/2
✓ Branch 0 taken 487 times.
✓ Branch 1 taken 149 times.
636 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1413
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 392 times.
487 if (m->input_picture[flush_offset])
1414 95 break;
1415
1416 244 encoding_delay -= flush_offset - 1;
1417 }
1418
1419 /* shift buffer entries */
1420
2/2
✓ Branch 0 taken 198604 times.
✓ Branch 1 taken 12422 times.
211026 for (int i = flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
1421 198604 m->input_picture[i - flush_offset] = m->input_picture[i];
1422
2/2
✓ Branch 0 taken 12570 times.
✓ Branch 1 taken 12422 times.
24992 for (int i = MPVENC_MAX_B_FRAMES + 1 - flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
1423 12570 m->input_picture[i] = NULL;
1424
1425 12422 m->input_picture[encoding_delay] = pic;
1426
1427 12422 return 0;
1428 fail:
1429 av_refstruct_unref(&pic);
1430 return ret;
1431 }
1432
1433 static int skip_check(MPVMainEncContext *const m,
1434 const MPVPicture *p, const MPVPicture *ref)
1435 {
1436 MPVEncContext *const s = &m->s;
1437 int score = 0;
1438 int64_t score64 = 0;
1439
1440 for (int plane = 0; plane < 3; plane++) {
1441 const int stride = p->f->linesize[plane];
1442 const int bw = plane ? 1 : 2;
1443 for (int y = 0; y < s->c.mb_height * bw; y++) {
1444 for (int x = 0; x < s->c.mb_width * bw; x++) {
1445 const uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride);
1446 const uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1447 int v = m->frame_skip_cmp_fn(s, dptr, rptr, stride, 8);
1448
1449 switch (FFABS(m->frame_skip_exp)) {
1450 case 0: score = FFMAX(score, v); break;
1451 case 1: score += FFABS(v); break;
1452 case 2: score64 += v * (int64_t)v; break;
1453 case 3: score64 += FFABS(v * (int64_t)v * v); break;
1454 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
1455 }
1456 }
1457 }
1458 }
1459 emms_c();
1460
1461 if (score)
1462 score64 = score;
1463 if (m->frame_skip_exp < 0)
1464 score64 = pow(score64 / (double)(s->c.mb_width * s->c.mb_height),
1465 -1.0/m->frame_skip_exp);
1466
1467 if (score64 < m->frame_skip_threshold)
1468 return 1;
1469 if (score64 < ((m->frame_skip_factor * (int64_t) s->lambda) >> 8))
1470 return 1;
1471 return 0;
1472 }
1473
1474 static int encode_frame(AVCodecContext *c, const AVFrame *frame, AVPacket *pkt)
1475 {
1476 int ret;
1477 int size = 0;
1478
1479 ret = avcodec_send_frame(c, frame);
1480 if (ret < 0)
1481 return ret;
1482
1483 do {
1484 ret = avcodec_receive_packet(c, pkt);
1485 if (ret >= 0) {
1486 size += pkt->size;
1487 av_packet_unref(pkt);
1488 } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1489 return ret;
1490 } while (ret >= 0);
1491
1492 return size;
1493 }
1494
1495 static int estimate_best_b_count(MPVMainEncContext *const m)
1496 {
1497 MPVEncContext *const s = &m->s;
1498 AVPacket *pkt;
1499 const int scale = m->brd_scale;
1500 int width = s->c.width >> scale;
1501 int height = s->c.height >> scale;
1502 int out_size, p_lambda, b_lambda, lambda2;
1503 int64_t best_rd = INT64_MAX;
1504 int best_b_count = -1;
1505 int ret = 0;
1506
1507 av_assert0(scale >= 0 && scale <= 3);
1508
1509 pkt = av_packet_alloc();
1510 if (!pkt)
1511 return AVERROR(ENOMEM);
1512
1513 p_lambda = m->last_lambda_for[AV_PICTURE_TYPE_P];
1514 //p_lambda * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset;
1515 b_lambda = m->last_lambda_for[AV_PICTURE_TYPE_B];
1516 if (!b_lambda) // FIXME we should do this somewhere else
1517 b_lambda = p_lambda;
1518 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1519 FF_LAMBDA_SHIFT;
1520
1521 for (int i = 0; i < m->max_b_frames + 2; i++) {
1522 const MPVPicture *pre_input_ptr = i ? m->input_picture[i - 1] :
1523 s->c.next_pic.ptr;
1524
1525 if (pre_input_ptr) {
1526 const AVFrame *const pre_input = pre_input_ptr->f;
1527
1528 s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[0],
1529 m->tmp_frames[i]->linesize[0],
1530 pre_input->data[0],
1531 pre_input->linesize[0],
1532 width, height);
1533 s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[1],
1534 m->tmp_frames[i]->linesize[1],
1535 pre_input->data[1],
1536 pre_input->linesize[1],
1537 width >> 1, height >> 1);
1538 s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[2],
1539 m->tmp_frames[i]->linesize[2],
1540 pre_input->data[2],
1541 pre_input->linesize[2],
1542 width >> 1, height >> 1);
1543 }
1544 }
1545
1546 for (int j = 0; j < m->max_b_frames + 1; j++) {
1547 AVCodecContext *c;
1548 int64_t rd = 0;
1549
1550 if (!m->input_picture[j])
1551 break;
1552
1553 c = avcodec_alloc_context3(NULL);
1554 if (!c) {
1555 ret = AVERROR(ENOMEM);
1556 goto fail;
1557 }
1558
1559 c->width = width;
1560 c->height = height;
1561 c->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_PSNR;
1562 c->flags |= s->c.avctx->flags & AV_CODEC_FLAG_QPEL;
1563 c->mb_decision = s->c.avctx->mb_decision;
1564 c->me_cmp = s->c.avctx->me_cmp;
1565 c->mb_cmp = s->c.avctx->mb_cmp;
1566 c->me_sub_cmp = s->c.avctx->me_sub_cmp;
1567 c->pix_fmt = AV_PIX_FMT_YUV420P;
1568 c->time_base = s->c.avctx->time_base;
1569 c->max_b_frames = m->max_b_frames;
1570
1571 ret = avcodec_open2(c, s->c.avctx->codec, NULL);
1572 if (ret < 0)
1573 goto fail;
1574
1575
1576 m->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1577 m->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1578
1579 out_size = encode_frame(c, m->tmp_frames[0], pkt);
1580 if (out_size < 0) {
1581 ret = out_size;
1582 goto fail;
1583 }
1584
1585 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1586
1587 for (int i = 0; i < m->max_b_frames + 1; i++) {
1588 int is_p = i % (j + 1) == j || i == m->max_b_frames;
1589
1590 m->tmp_frames[i + 1]->pict_type = is_p ?
1591 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1592 m->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1593
1594 out_size = encode_frame(c, m->tmp_frames[i + 1], pkt);
1595 if (out_size < 0) {
1596 ret = out_size;
1597 goto fail;
1598 }
1599
1600 rd += (out_size * (uint64_t)lambda2) >> (FF_LAMBDA_SHIFT - 3);
1601 }
1602
1603 /* get the delayed frames */
1604 out_size = encode_frame(c, NULL, pkt);
1605 if (out_size < 0) {
1606 ret = out_size;
1607 goto fail;
1608 }
1609 rd += (out_size * (uint64_t)lambda2) >> (FF_LAMBDA_SHIFT - 3);
1610
1611 rd += c->error[0] + c->error[1] + c->error[2];
1612
1613 if (rd < best_rd) {
1614 best_rd = rd;
1615 best_b_count = j;
1616 }
1617
1618 fail:
1619 avcodec_free_context(&c);
1620 av_packet_unref(pkt);
1621 if (ret < 0) {
1622 best_b_count = ret;
1623 break;
1624 }
1625 }
1626
1627 av_packet_free(&pkt);
1628
1629 return best_b_count;
1630 }
1631
1632 /**
1633 * Determines whether an input picture is discarded or not
1634 * and if not determines the length of the next chain of B frames
1635 * and moves these pictures (including the P frame) into
1636 * reordered_input_picture.
1637 * input_picture[0] is always NULL when exiting this function, even on error;
1638 * reordered_input_picture[0] is always NULL when exiting this function on error.
1639 */
1640 12422 static int set_bframe_chain_length(MPVMainEncContext *const m)
1641 {
1642 12422 MPVEncContext *const s = &m->s;
1643
1644 /* Either nothing to do or can't do anything */
1645
4/4
✓ Branch 0 taken 11074 times.
✓ Branch 1 taken 1348 times.
✓ Branch 2 taken 297 times.
✓ Branch 3 taken 10777 times.
12422 if (m->reordered_input_picture[0] || !m->input_picture[0])
1646 1645 return 0;
1647
1648 /* set next picture type & ordering */
1649
2/4
✓ Branch 0 taken 10777 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10777 times.
10777 if (m->frame_skip_threshold || m->frame_skip_factor) {
1650 if (m->picture_in_gop_number < m->gop_size &&
1651 s->c.next_pic.ptr &&
1652 skip_check(m, m->input_picture[0], s->c.next_pic.ptr)) {
1653 // FIXME check that the gop check above is +-1 correct
1654 av_refstruct_unref(&m->input_picture[0]);
1655
1656 ff_vbv_update(m, 0);
1657
1658 return 0;
1659 }
1660 }
1661
1662 10777 if (/* m->picture_in_gop_number >= m->gop_size || */
1663
4/4
✓ Branch 0 taken 10540 times.
✓ Branch 1 taken 237 times.
✓ Branch 2 taken 1889 times.
✓ Branch 3 taken 8651 times.
10777 !s->c.next_pic.ptr || m->intra_only) {
1664 2126 m->reordered_input_picture[0] = m->input_picture[0];
1665 2126 m->input_picture[0] = NULL;
1666 2126 m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1667 2126 m->reordered_input_picture[0]->coded_picture_number =
1668 2126 m->coded_picture_number++;
1669 } else {
1670 8651 int b_frames = 0;
1671
1672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8651 times.
8651 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
1673 for (int i = 0; i < m->max_b_frames + 1; i++) {
1674 int pict_num = m->input_picture[0]->display_picture_number + i;
1675
1676 if (pict_num >= m->rc_context.num_entries)
1677 break;
1678 if (!m->input_picture[i]) {
1679 m->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1680 break;
1681 }
1682
1683 m->input_picture[i]->f->pict_type =
1684 m->rc_context.entry[pict_num].new_pict_type;
1685 }
1686 }
1687
1688
2/2
✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 156 times.
8651 if (m->b_frame_strategy == 0) {
1689 8495 b_frames = m->max_b_frames;
1690
4/4
✓ Branch 0 taken 686 times.
✓ Branch 1 taken 7888 times.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 607 times.
8574 while (b_frames && !m->input_picture[b_frames])
1691 79 b_frames--;
1692
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 } else if (m->b_frame_strategy == 1) {
1693
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 156 times.
468 for (int i = 1; i < m->max_b_frames + 1; i++) {
1694
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 6 times.
312 if (m->input_picture[i] &&
1695
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 108 times.
306 m->input_picture[i]->b_frame_score == 0) {
1696 198 m->input_picture[i]->b_frame_score =
1697 198 get_intra_count(s,
1698 198 m->input_picture[i ]->f->data[0],
1699 198 m->input_picture[i - 1]->f->data[0],
1700 198 s->c.linesize) + 1;
1701 }
1702 }
1703 298 for (int i = 0;; i++) {
1704
2/2
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 43 times.
298 if (i >= m->max_b_frames + 1 ||
1705
2/2
✓ Branch 0 taken 251 times.
✓ Branch 1 taken 4 times.
255 !m->input_picture[i] ||
1706 251 m->input_picture[i]->b_frame_score - 1 >
1707
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 142 times.
251 s->c.mb_num / m->b_sensitivity) {
1708 156 b_frames = FFMAX(0, i - 1);
1709 156 break;
1710 }
1711 }
1712
1713 /* reset scores */
1714
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 156 times.
401 for (int i = 0; i < b_frames + 1; i++)
1715 245 m->input_picture[i]->b_frame_score = 0;
1716 } else if (m->b_frame_strategy == 2) {
1717 b_frames = estimate_best_b_count(m);
1718 if (b_frames < 0) {
1719 av_refstruct_unref(&m->input_picture[0]);
1720 return b_frames;
1721 }
1722 }
1723
1724
2/2
✓ Branch 0 taken 2065 times.
✓ Branch 1 taken 6586 times.
8651 if (s->c.codec_id == AV_CODEC_ID_MPEG4)
1725
2/2
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 1762 times.
2065 while (b_frames &&
1726 303 m->input_picture[b_frames]->f->pts * s->c.avctx->time_base.num -
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
303 s->c.last_non_b_time > UINT16_MAX)
1728 b_frames--;
1729
1730
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 8651 times.
9999 for (int i = b_frames - 1; i >= 0; i--) {
1731 1348 int type = m->input_picture[i]->f->pict_type;
1732
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1348 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1348 if (type && type != AV_PICTURE_TYPE_B)
1733 b_frames = i;
1734 }
1735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8651 times.
8651 if (m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1736 b_frames == m->max_b_frames) {
1737 av_log(s->c.avctx, AV_LOG_ERROR,
1738 "warning, too many B-frames in a row\n");
1739 }
1740
1741
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 8049 times.
8651 if (m->picture_in_gop_number + b_frames >= m->gop_size) {
1742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 602 times.
602 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1743 m->gop_size > m->picture_in_gop_number) {
1744 b_frames = m->gop_size - m->picture_in_gop_number - 1;
1745 } else {
1746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 602 times.
602 if (s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
1747 b_frames = 0;
1748 602 m->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1749 }
1750 }
1751
1752
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8651 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8651 if ((s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
1753 m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1754 b_frames--;
1755
1756 8651 m->reordered_input_picture[0] = m->input_picture[b_frames];
1757 8651 m->input_picture[b_frames] = NULL;
1758
2/2
✓ Branch 0 taken 8033 times.
✓ Branch 1 taken 618 times.
8651 if (m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1759 8033 m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1760 8651 m->reordered_input_picture[0]->coded_picture_number =
1761 8651 m->coded_picture_number++;
1762
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 8651 times.
9999 for (int i = 0; i < b_frames; i++) {
1763 1348 m->reordered_input_picture[i + 1] = m->input_picture[i];
1764 1348 m->input_picture[i] = NULL;
1765 1348 m->reordered_input_picture[i + 1]->f->pict_type =
1766 AV_PICTURE_TYPE_B;
1767 1348 m->reordered_input_picture[i + 1]->coded_picture_number =
1768 1348 m->coded_picture_number++;
1769 }
1770 }
1771
1772 10777 return 0;
1773 }
1774
1775 12422 static int select_input_picture(MPVMainEncContext *const m)
1776 {
1777 12422 MPVEncContext *const s = &m->s;
1778 int ret;
1779
1780 av_assert1(!m->reordered_input_picture[0]);
1781
1782
2/2
✓ Branch 0 taken 198752 times.
✓ Branch 1 taken 12422 times.
211174 for (int i = 1; i <= MPVENC_MAX_B_FRAMES; i++)
1783 198752 m->reordered_input_picture[i - 1] = m->reordered_input_picture[i];
1784 12422 m->reordered_input_picture[MPVENC_MAX_B_FRAMES] = NULL;
1785
1786 12422 ret = set_bframe_chain_length(m);
1787 av_assert1(!m->input_picture[0]);
1788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12422 times.
12422 if (ret < 0)
1789 return ret;
1790
1791 12422 av_frame_unref(s->new_pic);
1792
1793
2/2
✓ Branch 0 taken 12125 times.
✓ Branch 1 taken 297 times.
12422 if (m->reordered_input_picture[0]) {
1794 12125 m->reordered_input_picture[0]->reference =
1795 12125 m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_B;
1796
1797
4/4
✓ Branch 0 taken 11402 times.
✓ Branch 1 taken 723 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 11352 times.
12125 if (m->reordered_input_picture[0]->shared || s->c.avctx->rc_buffer_size) {
1798 // input is a shared pix, so we can't modify it -> allocate a new
1799 // one & ensure that the shared one is reusable
1800 773 av_frame_move_ref(s->new_pic, m->reordered_input_picture[0]->f);
1801
1802 773 ret = prepare_picture(s, m->reordered_input_picture[0]->f, s->new_pic);
1803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 773 times.
773 if (ret < 0)
1804 goto fail;
1805 } else {
1806 // input is not a shared pix -> reuse buffer for current_pix
1807 11352 ret = av_frame_ref(s->new_pic, m->reordered_input_picture[0]->f);
1808
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11352 times.
11352 if (ret < 0)
1809 goto fail;
1810 // The input was stored INPLACE_OFFSET into the buffer, which
1811 // new_pic now points at. Point the frame back at the start of
1812 // the buffer for the reconstruction.
1813
2/2
✓ Branch 0 taken 34056 times.
✓ Branch 1 taken 11352 times.
45408 for (int i = 0; i < MPV_MAX_PLANES; i++)
1814 34056 m->reordered_input_picture[0]->f->data[i] -= INPLACE_OFFSET;
1815 }
1816 12125 s->c.cur_pic.ptr = m->reordered_input_picture[0];
1817 12125 m->reordered_input_picture[0] = NULL;
1818 av_assert1(s->c.mb_width == s->c.buffer_pools.alloc_mb_width);
1819 av_assert1(s->c.mb_height == s->c.buffer_pools.alloc_mb_height);
1820 av_assert1(s->c.mb_stride == s->c.buffer_pools.alloc_mb_stride);
1821 12125 ret = ff_mpv_alloc_pic_accessories(s->c.avctx, &s->c.cur_pic,
1822 &s->c.sc, &s->c.buffer_pools, s->c.mb_height);
1823
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12125 times.
12125 if (ret < 0) {
1824 ff_mpv_unref_picture(&s->c.cur_pic);
1825 return ret;
1826 }
1827 12125 s->picture_number = s->c.cur_pic.ptr->display_picture_number;
1828
1829 }
1830 12422 return 0;
1831 fail:
1832 av_refstruct_unref(&m->reordered_input_picture[0]);
1833 return ret;
1834 }
1835
1836 12250 static void frame_end(MPVMainEncContext *const m)
1837 {
1838 12250 MPVEncContext *const s = &m->s;
1839
1840
2/2
✓ Branch 0 taken 4104 times.
✓ Branch 1 taken 8146 times.
12250 if (s->me.unrestricted_mv &&
1841
2/2
✓ Branch 0 taken 3502 times.
✓ Branch 1 taken 602 times.
4104 s->c.cur_pic.reference &&
1842
2/2
✓ Branch 0 taken 3501 times.
✓ Branch 1 taken 1 times.
3502 !m->intra_only) {
1843 3501 int hshift = s->c.chroma_x_shift;
1844 3501 int vshift = s->c.chroma_y_shift;
1845 3501 s->mpvencdsp.draw_edges(s->c.cur_pic.data[0],
1846 s->c.cur_pic.linesize[0],
1847 s->c.h_edge_pos, s->c.v_edge_pos,
1848 EDGE_WIDTH, EDGE_WIDTH,
1849 EDGE_TOP | EDGE_BOTTOM);
1850 3501 s->mpvencdsp.draw_edges(s->c.cur_pic.data[1],
1851 s->c.cur_pic.linesize[1],
1852 3501 s->c.h_edge_pos >> hshift,
1853 3501 s->c.v_edge_pos >> vshift,
1854 EDGE_WIDTH >> hshift,
1855 EDGE_WIDTH >> vshift,
1856 EDGE_TOP | EDGE_BOTTOM);
1857 3501 s->mpvencdsp.draw_edges(s->c.cur_pic.data[2],
1858 s->c.cur_pic.linesize[2],
1859 3501 s->c.h_edge_pos >> hshift,
1860 3501 s->c.v_edge_pos >> vshift,
1861 EDGE_WIDTH >> hshift,
1862 EDGE_WIDTH >> vshift,
1863 EDGE_TOP | EDGE_BOTTOM);
1864 }
1865
1866 12250 m->last_pict_type = s->c.pict_type;
1867 12250 m->last_lambda_for[s->c.pict_type] = s->c.cur_pic.ptr->f->quality;
1868
2/2
✓ Branch 0 taken 10902 times.
✓ Branch 1 taken 1348 times.
12250 if (s->c.pict_type != AV_PICTURE_TYPE_B)
1869 10902 m->last_non_b_pict_type = s->c.pict_type;
1870 12250 }
1871
1872 350 static void update_noise_reduction(MPVMainEncContext *const m)
1873 {
1874 350 MPVEncContext *const s = &m->s;
1875 int intra, i;
1876
1877
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 350 times.
1050 for (intra = 0; intra < 2; intra++) {
1878
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 683 times.
700 if (s->dct_count[intra] > (1 << 16)) {
1879
2/2
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 17 times.
1105 for (i = 0; i < 64; i++) {
1880 1088 s->dct_error_sum[intra][i] >>= 1;
1881 }
1882 17 s->dct_count[intra] >>= 1;
1883 }
1884
1885
2/2
✓ Branch 0 taken 44800 times.
✓ Branch 1 taken 700 times.
45500 for (i = 0; i < 64; i++) {
1886 44800 s->dct_offset[intra][i] = (m->noise_reduction *
1887 44800 s->dct_count[intra] +
1888 44800 s->dct_error_sum[intra][i] / 2) /
1889 44800 (s->dct_error_sum[intra][i] + 1);
1890 }
1891 }
1892 350 }
1893
1894 12125 static void frame_start(MPVMainEncContext *const m)
1895 {
1896 12125 MPVEncContext *const s = &m->s;
1897
1898 12125 s->c.cur_pic.ptr->f->pict_type = s->c.pict_type;
1899
1900
2/2
✓ Branch 0 taken 10777 times.
✓ Branch 1 taken 1348 times.
12125 if (s->c.pict_type != AV_PICTURE_TYPE_B) {
1901 10777 ff_mpv_replace_picture(&s->c.last_pic, &s->c.next_pic);
1902 10777 ff_mpv_replace_picture(&s->c.next_pic, &s->c.cur_pic);
1903 }
1904
1905 av_assert2(!!m->noise_reduction == !!s->dct_error_sum);
1906
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 11775 times.
12125 if (s->dct_error_sum) {
1907 350 update_noise_reduction(m);
1908 }
1909 12125 }
1910
1911 12422 int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1912 const AVFrame *pic_arg, int *got_packet)
1913 {
1914 12422 MPVMainEncContext *const m = avctx->priv_data;
1915 12422 MPVEncContext *const s = &m->s;
1916 int stuffing_count, ret;
1917 12422 int context_count = s->c.slice_context_count;
1918
1919 12422 ff_mpv_unref_picture(&s->c.cur_pic);
1920
1921 12422 m->vbv_ignore_qmax = 0;
1922
1923 12422 m->picture_in_gop_number++;
1924
1925 12422 ret = load_input_picture(m, pic_arg);
1926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12422 times.
12422 if (ret < 0)
1927 return ret;
1928
1929 12422 ret = select_input_picture(m);
1930
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12422 times.
12422 if (ret < 0)
1931 return ret;
1932
1933 /* output? */
1934
2/2
✓ Branch 0 taken 12125 times.
✓ Branch 1 taken 297 times.
12422 if (s->new_pic->data[0]) {
1935
4/4
✓ Branch 0 taken 11099 times.
✓ Branch 1 taken 1026 times.
✓ Branch 2 taken 10699 times.
✓ Branch 3 taken 400 times.
12125 int growing_buffer = context_count == 1 && !s->data_partitioning;
1936 24250 size_t pkt_size = 10000 + s->c.mb_width * s->c.mb_height *
1937
2/2
✓ Branch 0 taken 10699 times.
✓ Branch 1 taken 1426 times.
12125 (growing_buffer ? 64 : (MAX_MB_BYTES + 100));
1938
2/2
✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 10886 times.
12125 if (CONFIG_MJPEG_ENCODER && avctx->codec_id == AV_CODEC_ID_MJPEG) {
1939 1239 ret = ff_mjpeg_add_icc_profile_size(avctx, s->new_pic, &pkt_size);
1940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 if (ret < 0)
1941 return ret;
1942 }
1943
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12125 times.
12125 if ((ret = ff_alloc_packet(avctx, pkt, pkt_size)) < 0)
1944 return ret;
1945 12125 pkt->size = avctx->internal->byte_buffer_size - AV_INPUT_BUFFER_PADDING_SIZE;
1946
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12125 times.
12125 if (s->mb_info) {
1947 s->mb_info_ptr = av_packet_new_side_data(pkt,
1948 AV_PKT_DATA_H263_MB_INFO,
1949 s->c.mb_width*s->c.mb_height*12);
1950 if (!s->mb_info_ptr)
1951 return AVERROR(ENOMEM);
1952 s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1953 }
1954
1955 12125 s->c.pict_type = s->new_pic->pict_type;
1956 12125 frame_start(m);
1957 12250 vbv_retry:
1958 12250 ret = encode_picture(m, pkt);
1959
2/2
✓ Branch 0 taken 10824 times.
✓ Branch 1 taken 1426 times.
12250 if (growing_buffer) {
1960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10824 times.
10824 av_assert0(s->pb.buf == avctx->internal->byte_buffer);
1961 10824 pkt->data = s->pb.buf;
1962 10824 pkt->size = avctx->internal->byte_buffer_size;
1963 }
1964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12250 times.
12250 if (ret < 0)
1965 return -1;
1966
1967 12250 frame_end(m);
1968
1969
2/2
✓ Branch 0 taken 1439 times.
✓ Branch 1 taken 10811 times.
12250 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) && s->c.out_format == FMT_MJPEG)
1970 1439 ff_mjpeg_encode_picture_trailer(&s->pb, m->header_bits);
1971
1972
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 12075 times.
12250 if (avctx->rc_buffer_size) {
1973 175 RateControlContext *rcc = &m->rc_context;
1974
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 25 times.
175 int max_size = FFMAX(rcc->buffer_index * avctx->rc_max_available_vbv_use, rcc->buffer_index - 500);
1975
2/4
✓ Branch 0 taken 175 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 175 times.
175 int hq = (avctx->mb_decision == FF_MB_DECISION_RD || avctx->trellis);
1976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
1977
1978
2/2
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 50 times.
175 if (put_bits_count(&s->pb) > max_size &&
1979
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 s->lambda < m->lmax) {
1980 125 m->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
1981 (s->c.qscale + 1) / s->c.qscale);
1982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (s->adaptive_quant) {
1983 for (int i = 0; i < s->c.mb_height * s->c.mb_stride; i++)
1984 s->lambda_table[i] =
1985 FFMAX(s->lambda_table[i] + min_step,
1986 s->lambda_table[i] * (s->c.qscale + 1) /
1987 s->c.qscale);
1988 }
1989 125 s->c.mb_skipped = 0; // done in frame_start()
1990 // done in encode_picture() so we must undo it
1991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
1992 s->c.no_rounding ^= s->flipflop_rounding;
1993 }
1994
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if (s->c.pict_type != AV_PICTURE_TYPE_B) {
1995 125 s->c.time_base = s->c.last_time_base;
1996 125 s->c.last_non_b_time = s->c.time - s->c.pp_time;
1997 }
1998 125 m->vbv_ignore_qmax = 1;
1999 125 av_log(avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
2000 125 goto vbv_retry;
2001 }
2002
2003
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 av_assert0(avctx->rc_max_rate);
2004 }
2005
2006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12125 times.
12125 if (avctx->flags & AV_CODEC_FLAG_PASS1)
2007 ff_write_pass1_stats(m);
2008
2009
2/2
✓ Branch 0 taken 36375 times.
✓ Branch 1 taken 12125 times.
48500 for (int i = 0; i < MPV_MAX_PLANES; i++)
2010 36375 avctx->error[i] += s->encoding_error[i];
2011 12125 ff_encode_add_stats_side_data(pkt, s->c.cur_pic.ptr->f->quality,
2012 12125 s->encoding_error,
2013
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12125 times.
12125 (avctx->flags&AV_CODEC_FLAG_PSNR) ? MPV_MAX_PLANES : 0,
2014 s->c.pict_type);
2015
2016 12125 if (avctx->flags & AV_CODEC_FLAG_PASS1)
2017 assert(put_bits_count(&s->pb) == m->header_bits + s->mv_bits +
2018 s->misc_bits + s->i_tex_bits +
2019 s->p_tex_bits);
2020 12125 flush_put_bits(&s->pb);
2021 12125 m->frame_bits = put_bits_count(&s->pb);
2022
2023 12125 stuffing_count = ff_vbv_update(m, m->frame_bits);
2024 12125 m->stuffing_bits = 8*stuffing_count;
2025
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 12075 times.
12125 if (stuffing_count) {
2026
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if (put_bytes_left(&s->pb, 0) < stuffing_count + 50) {
2027 av_log(avctx, AV_LOG_ERROR, "stuffing too large\n");
2028 return -1;
2029 }
2030
2031
1/3
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
50 switch (s->c.codec_id) {
2032 50 case AV_CODEC_ID_MPEG1VIDEO:
2033 case AV_CODEC_ID_MPEG2VIDEO:
2034
2/2
✓ Branch 0 taken 3648955 times.
✓ Branch 1 taken 50 times.
3649005 while (stuffing_count--) {
2035 3648955 put_bits(&s->pb, 8, 0);
2036 }
2037 50 break;
2038 case AV_CODEC_ID_MPEG4:
2039 put_bits(&s->pb, 16, 0);
2040 put_bits(&s->pb, 16, 0x1C3);
2041 stuffing_count -= 4;
2042 while (stuffing_count--) {
2043 put_bits(&s->pb, 8, 0xFF);
2044 }
2045 break;
2046 default:
2047 av_log(avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
2048 m->stuffing_bits = 0;
2049 }
2050 50 flush_put_bits(&s->pb);
2051 50 m->frame_bits = put_bits_count(&s->pb);
2052 }
2053
2054 /* update MPEG-1/2 vbv_delay for CBR */
2055
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 12075 times.
12125 if (avctx->rc_max_rate &&
2056
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 avctx->rc_min_rate == avctx->rc_max_rate &&
2057
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 s->c.out_format == FMT_MPEG1 &&
2058 50 90000LL * (avctx->rc_buffer_size - 1) <=
2059
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
50 avctx->rc_max_rate * 0xFFFFLL) {
2060 AVCPBProperties *props;
2061 size_t props_size;
2062
2063 int vbv_delay, min_delay;
2064 50 double inbits = avctx->rc_max_rate *
2065 25 av_q2d(avctx->time_base);
2066 25 int minbits = m->frame_bits - 8 *
2067 25 (m->vbv_delay_pos - 1);
2068 25 double bits = m->rc_context.buffer_index + minbits - inbits;
2069 25 uint8_t *const vbv_delay_ptr = s->pb.buf + m->vbv_delay_pos;
2070
2071
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (bits < 0)
2072 av_log(avctx, AV_LOG_ERROR,
2073 "Internal error, negative bits\n");
2074
2075 av_assert1(s->c.repeat_first_field == 0);
2076
2077 25 vbv_delay = bits * 90000 / avctx->rc_max_rate;
2078 25 min_delay = (minbits * 90000LL + avctx->rc_max_rate - 1) /
2079 25 avctx->rc_max_rate;
2080
2081 25 vbv_delay = FFMAX(vbv_delay, min_delay);
2082
2083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 av_assert0(vbv_delay < 0xFFFF);
2084
2085 25 vbv_delay_ptr[0] &= 0xF8;
2086 25 vbv_delay_ptr[0] |= vbv_delay >> 13;
2087 25 vbv_delay_ptr[1] = vbv_delay >> 5;
2088 25 vbv_delay_ptr[2] &= 0x07;
2089 25 vbv_delay_ptr[2] |= vbv_delay << 3;
2090
2091 25 props = av_cpb_properties_alloc(&props_size);
2092
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!props)
2093 return AVERROR(ENOMEM);
2094 25 props->vbv_delay = vbv_delay * 300;
2095
2096 25 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
2097 (uint8_t*)props, props_size);
2098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0) {
2099 av_freep(&props);
2100 return ret;
2101 }
2102 }
2103 12125 m->total_bits += m->frame_bits;
2104
2105 12125 pkt->pts = s->c.cur_pic.ptr->f->pts;
2106 12125 pkt->duration = s->c.cur_pic.ptr->f->duration;
2107
4/4
✓ Branch 0 taken 6231 times.
✓ Branch 1 taken 5894 times.
✓ Branch 2 taken 4883 times.
✓ Branch 3 taken 1348 times.
12125 if (!s->c.low_delay && s->c.pict_type != AV_PICTURE_TYPE_B) {
2108
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 4785 times.
4883 if (!s->c.cur_pic.ptr->coded_picture_number)
2109 98 pkt->dts = pkt->pts - m->dts_delta;
2110 else
2111 4785 pkt->dts = m->reordered_pts;
2112 4883 m->reordered_pts = pkt->pts;
2113 } else
2114 7242 pkt->dts = pkt->pts;
2115
2116 // the no-delay case is handled in generic code
2117
2/2
✓ Branch 0 taken 7942 times.
✓ Branch 1 taken 4183 times.
12125 if (avctx->codec->capabilities & AV_CODEC_CAP_DELAY) {
2118 7942 ret = ff_encode_reordered_opaque(avctx, pkt, s->c.cur_pic.ptr->f);
2119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7942 times.
7942 if (ret < 0)
2120 return ret;
2121 }
2122
2123
2/2
✓ Branch 0 taken 2775 times.
✓ Branch 1 taken 9350 times.
12125 if (s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)
2124 2775 pkt->flags |= AV_PKT_FLAG_KEY;
2125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12125 times.
12125 if (s->mb_info)
2126 av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
2127 } else {
2128 297 m->frame_bits = 0;
2129 }
2130
2131 12422 ff_mpv_unref_picture(&s->c.cur_pic);
2132
2133 av_assert1((m->frame_bits & 7) == 0);
2134
2135 12422 pkt->size = m->frame_bits / 8;
2136 12422 *got_packet = !!pkt->size;
2137 12422 return 0;
2138 }
2139
2140 static inline void dct_single_coeff_elimination(MPVEncContext *const s,
2141 int n, int threshold)
2142 {
2143 static const char tab[64] = {
2144 3, 2, 2, 1, 1, 1, 1, 1,
2145 1, 1, 1, 1, 1, 1, 1, 1,
2146 1, 1, 1, 1, 1, 1, 1, 1,
2147 0, 0, 0, 0, 0, 0, 0, 0,
2148 0, 0, 0, 0, 0, 0, 0, 0,
2149 0, 0, 0, 0, 0, 0, 0, 0,
2150 0, 0, 0, 0, 0, 0, 0, 0,
2151 0, 0, 0, 0, 0, 0, 0, 0
2152 };
2153 int score = 0;
2154 int run = 0;
2155 int i;
2156 int16_t *block = s->block[n];
2157 const int last_index = s->c.block_last_index[n];
2158 int skip_dc;
2159
2160 if (threshold < 0) {
2161 skip_dc = 0;
2162 threshold = -threshold;
2163 } else
2164 skip_dc = 1;
2165
2166 /* Are all we could set to zero already zero? */
2167 if (last_index <= skip_dc - 1)
2168 return;
2169
2170 for (i = 0; i <= last_index; i++) {
2171 const int j = s->c.intra_scantable.permutated[i];
2172 const int level = FFABS(block[j]);
2173 if (level == 1) {
2174 if (skip_dc && i == 0)
2175 continue;
2176 score += tab[run];
2177 run = 0;
2178 } else if (level > 1) {
2179 return;
2180 } else {
2181 run++;
2182 }
2183 }
2184 if (score >= threshold)
2185 return;
2186 for (i = skip_dc; i <= last_index; i++) {
2187 const int j = s->c.intra_scantable.permutated[i];
2188 block[j] = 0;
2189 }
2190 if (block[0])
2191 s->c.block_last_index[n] = 0;
2192 else
2193 s->c.block_last_index[n] = -1;
2194 }
2195
2196 static inline void clip_coeffs(const MPVEncContext *const s, int16_t block[],
2197 int last_index)
2198 {
2199 int i;
2200 const int maxlevel = s->max_qcoeff;
2201 const int minlevel = s->min_qcoeff;
2202 int overflow = 0;
2203
2204 if (s->c.mb_intra) {
2205 i = 1; // skip clipping of intra dc
2206 } else
2207 i = 0;
2208
2209 for (; i <= last_index; i++) {
2210 const int j = s->c.intra_scantable.permutated[i];
2211 int level = block[j];
2212
2213 if (level > maxlevel) {
2214 level = maxlevel;
2215 overflow++;
2216 } else if (level < minlevel) {
2217 level = minlevel;
2218 overflow++;
2219 }
2220
2221 block[j] = level;
2222 }
2223
2224 if (overflow && s->c.avctx->mb_decision == FF_MB_DECISION_SIMPLE)
2225 av_log(s->c.avctx, AV_LOG_INFO,
2226 "warning, clipping %d dct coefficients to %d..%d\n",
2227 overflow, minlevel, maxlevel);
2228 }
2229
2230 static void get_visual_weight(int16_t *weight, const uint8_t *ptr, int stride)
2231 {
2232 int x, y;
2233 // FIXME optimize
2234 for (y = 0; y < 8; y++) {
2235 for (x = 0; x < 8; x++) {
2236 int x2, y2;
2237 int sum = 0;
2238 int sqr = 0;
2239 int count = 0;
2240
2241 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
2242 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
2243 int v = ptr[x2 + y2 * stride];
2244 sum += v;
2245 sqr += v * v;
2246 count++;
2247 }
2248 }
2249 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
2250 }
2251 }
2252 }
2253
2254 5560734 static av_always_inline void encode_mb_internal(MPVEncContext *const s,
2255 int motion_x, int motion_y,
2256 int mb_block_height,
2257 int mb_block_width,
2258 int mb_block_count,
2259 int chroma_x_shift,
2260 int chroma_y_shift,
2261 int chroma_format)
2262 {
2263 /* Interlaced DCT is only possible with MPEG-2 and MPEG-4
2264 * and neither of these encoders currently supports 444. */
2265 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2266 (s)->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2267 DECLARE_ALIGNED(16, int16_t, weight)[12][64];
2268 int16_t orig[12][64];
2269 5560734 const int mb_x = s->c.mb_x;
2270 5560734 const int mb_y = s->c.mb_y;
2271 int i;
2272 int skip_dct[12];
2273 5560734 int dct_offset = s->c.linesize * 8; // default for progressive frames
2274 5560734 int uv_dct_offset = s->c.uvlinesize * 8;
2275 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2276 ptrdiff_t wrap_y, wrap_c;
2277
2278
2/2
✓ Branch 0 taken 35604698 times.
✓ Branch 1 taken 5560734 times.
41165432 for (i = 0; i < mb_block_count; i++)
2279 35604698 skip_dct[i] = s->skipdct;
2280
2281
2/2
✓ Branch 0 taken 1317157 times.
✓ Branch 1 taken 4243577 times.
5560734 if (s->adaptive_quant) {
2282 1317157 const int last_qp = s->c.qscale;
2283 1317157 const int mb_xy = mb_x + mb_y * s->c.mb_stride;
2284
2285 1317157 s->lambda = s->lambda_table[mb_xy];
2286 1317157 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
2287 FF_LAMBDA_SHIFT;
2288
2289
2/2
✓ Branch 0 taken 277972 times.
✓ Branch 1 taken 1039185 times.
1317157 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
2290 277972 s->dquant = s->c.cur_pic.qscale_table[mb_xy] - last_qp;
2291
2292
1/2
✓ Branch 0 taken 277972 times.
✗ Branch 1 not taken.
277972 if (s->c.out_format == FMT_H263) {
2293 277972 s->dquant = av_clip(s->dquant, -2, 2);
2294
2295
1/2
✓ Branch 0 taken 277972 times.
✗ Branch 1 not taken.
277972 if (s->c.codec_id == AV_CODEC_ID_MPEG4) {
2296
2/2
✓ Branch 0 taken 259474 times.
✓ Branch 1 taken 18498 times.
277972 if (!s->c.mb_intra) {
2297
2/2
✓ Branch 0 taken 140628 times.
✓ Branch 1 taken 118846 times.
259474 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
2298
4/4
✓ Branch 0 taken 96799 times.
✓ Branch 1 taken 43829 times.
✓ Branch 2 taken 33529 times.
✓ Branch 3 taken 63270 times.
140628 if (s->dquant & 1 || s->c.mv_dir & MV_DIRECT)
2299 77358 s->dquant = 0;
2300 }
2301
2/2
✓ Branch 0 taken 52828 times.
✓ Branch 1 taken 206646 times.
259474 if (s->c.mv_type == MV_TYPE_8X8)
2302 52828 s->dquant = 0;
2303 }
2304 }
2305 }
2306 }
2307 1317157 ff_set_qscale(&s->c, last_qp + s->dquant);
2308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4243577 times.
4243577 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
2309 ff_set_qscale(&s->c, s->c.qscale + s->dquant);
2310
2311 5560734 wrap_y = s->c.linesize;
2312 5560734 wrap_c = s->c.uvlinesize;
2313 5560734 ptr_y = s->new_pic->data[0] +
2314 5560734 (mb_y * 16 * wrap_y) + mb_x * 16;
2315 5560734 ptr_cb = s->new_pic->data[1] +
2316 5560734 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2317 5560734 ptr_cr = s->new_pic->data[2] +
2318 5560734 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2319
2320
4/4
✓ Branch 0 taken 5549219 times.
✓ Branch 1 taken 11515 times.
✓ Branch 2 taken 34636 times.
✓ Branch 3 taken 5514583 times.
5560734 if ((mb_x * 16 + 16 > s->c.width || mb_y * 16 + 16 > s->c.height) &&
2321
2/2
✓ Branch 0 taken 45901 times.
✓ Branch 1 taken 250 times.
46151 s->c.codec_id != AV_CODEC_ID_AMV) {
2322 45901 uint8_t *ebuf = s->c.sc.edge_emu_buffer + 38 * wrap_y;
2323 45901 int cw = (s->c.width + chroma_x_shift) >> chroma_x_shift;
2324 45901 int ch = (s->c.height + chroma_y_shift) >> chroma_y_shift;
2325 45901 s->c.vdsp.emulated_edge_mc(ebuf, ptr_y,
2326 wrap_y, wrap_y,
2327 16, 16, mb_x * 16, mb_y * 16,
2328 s->c.width, s->c.height);
2329 45901 ptr_y = ebuf;
2330 45901 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2331 wrap_c, wrap_c,
2332 mb_block_width, mb_block_height,
2333 mb_x * mb_block_width, mb_y * mb_block_height,
2334 cw, ch);
2335 45901 ptr_cb = ebuf + 16 * wrap_y;
2336 45901 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2337 wrap_c, wrap_c,
2338 mb_block_width, mb_block_height,
2339 mb_x * mb_block_width, mb_y * mb_block_height,
2340 cw, ch);
2341 45901 ptr_cr = ebuf + 16 * wrap_y + 16;
2342 }
2343
2344
2/2
✓ Branch 0 taken 1446085 times.
✓ Branch 1 taken 4114649 times.
5560734 if (s->c.mb_intra) {
2345
6/6
✓ Branch 0 taken 533825 times.
✓ Branch 1 taken 912260 times.
✓ Branch 2 taken 414511 times.
✓ Branch 3 taken 119314 times.
✓ Branch 4 taken 317347 times.
✓ Branch 5 taken 1009424 times.
1446085 if (INTERLACED_DCT(s)) {
2346 int progressive_score, interlaced_score;
2347
2348 317347 s->c.interlaced_dct = 0;
2349 317347 progressive_score = s->ildct_cmp[1](s, ptr_y, NULL, wrap_y, 8) +
2350 317347 s->ildct_cmp[1](s, ptr_y + wrap_y * 8,
2351 NULL, wrap_y, 8) - 400;
2352
2353
2/2
✓ Branch 0 taken 298252 times.
✓ Branch 1 taken 19095 times.
317347 if (progressive_score > 0) {
2354 298252 interlaced_score = s->ildct_cmp[1](s, ptr_y,
2355 NULL, wrap_y * 2, 8) +
2356 298252 s->ildct_cmp[1](s, ptr_y + wrap_y,
2357 NULL, wrap_y * 2, 8);
2358
2/2
✓ Branch 0 taken 377 times.
✓ Branch 1 taken 297875 times.
298252 if (progressive_score > interlaced_score) {
2359 377 s->c.interlaced_dct = 1;
2360
2361 377 dct_offset = wrap_y;
2362 377 uv_dct_offset = wrap_c;
2363 377 wrap_y <<= 1;
2364
3/4
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 192 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 185 times.
377 if (chroma_format == CHROMA_422 ||
2365 chroma_format == CHROMA_444)
2366 192 wrap_c <<= 1;
2367 }
2368 }
2369 }
2370
2371 1446085 s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
2372 1446085 s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
2373 1446085 s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
2374 1446085 s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
2375
2376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1446085 times.
1446085 if (s->c.avctx->flags & AV_CODEC_FLAG_GRAY) {
2377 skip_dct[4] = 1;
2378 skip_dct[5] = 1;
2379 } else {
2380 1446085 s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2381 1446085 s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2382
2/2
✓ Branch 0 taken 414511 times.
✓ Branch 1 taken 1031574 times.
1446085 if (chroma_format == CHROMA_422) {
2383 414511 s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2384 414511 s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2385
2/2
✓ Branch 0 taken 119314 times.
✓ Branch 1 taken 912260 times.
1031574 } else if (chroma_format == CHROMA_444) {
2386 119314 s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
2387 119314 s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
2388 119314 s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2389 119314 s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2390 119314 s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2391 119314 s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2392 }
2393 }
2394 } else {
2395 op_pixels_func (*op_pix)[4];
2396 qpel_mc_func (*op_qpix)[16];
2397 uint8_t *dest_y, *dest_cb, *dest_cr;
2398
2399 4114649 dest_y = s->c.dest[0];
2400 4114649 dest_cb = s->c.dest[1];
2401 4114649 dest_cr = s->c.dest[2];
2402
2403
4/4
✓ Branch 0 taken 983764 times.
✓ Branch 1 taken 3130885 times.
✓ Branch 2 taken 351320 times.
✓ Branch 3 taken 632444 times.
4114649 if ((!s->c.no_rounding) || s->c.pict_type == AV_PICTURE_TYPE_B) {
2404 3482205 op_pix = s->c.hdsp.put_pixels_tab;
2405 3482205 op_qpix = s->c.qdsp.put_qpel_pixels_tab;
2406 } else {
2407 632444 op_pix = s->c.hdsp.put_no_rnd_pixels_tab;
2408 632444 op_qpix = s->c.qdsp.put_no_rnd_qpel_pixels_tab;
2409 }
2410
2411
2/2
✓ Branch 0 taken 3772962 times.
✓ Branch 1 taken 341687 times.
4114649 if (s->c.mv_dir & MV_DIR_FORWARD) {
2412 3772962 ff_mpv_motion(&s->c, dest_y, dest_cb, dest_cr, 0,
2413 3772962 s->c.last_pic.data,
2414 op_pix, op_qpix);
2415 3772962 op_pix = s->c.hdsp.avg_pixels_tab;
2416 3772962 op_qpix = s->c.qdsp.avg_qpel_pixels_tab;
2417 }
2418
2/2
✓ Branch 0 taken 998169 times.
✓ Branch 1 taken 3116480 times.
4114649 if (s->c.mv_dir & MV_DIR_BACKWARD) {
2419 998169 ff_mpv_motion(&s->c, dest_y, dest_cb, dest_cr, 1,
2420 998169 s->c.next_pic.data,
2421 op_pix, op_qpix);
2422 }
2423
2424
5/6
✓ Branch 0 taken 347694 times.
✓ Branch 1 taken 3766955 times.
✓ Branch 2 taken 347694 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 505608 times.
✓ Branch 5 taken 3609041 times.
4114649 if (INTERLACED_DCT(s)) {
2425 int progressive_score, interlaced_score;
2426
2427 505608 s->c.interlaced_dct = 0;
2428 505608 progressive_score = s->ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2429 505608 s->ildct_cmp[0](s, dest_y + wrap_y * 8,
2430 505608 ptr_y + wrap_y * 8,
2431 wrap_y, 8) - 400;
2432
2433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505608 times.
505608 if (s->c.avctx->ildct_cmp == FF_CMP_VSSE)
2434 progressive_score -= 400;
2435
2436
2/2
✓ Branch 0 taken 445649 times.
✓ Branch 1 taken 59959 times.
505608 if (progressive_score > 0) {
2437 445649 interlaced_score = s->ildct_cmp[0](s, dest_y, ptr_y,
2438 wrap_y * 2, 8) +
2439 445649 s->ildct_cmp[0](s, dest_y + wrap_y,
2440 ptr_y + wrap_y,
2441 wrap_y * 2, 8);
2442
2443
2/2
✓ Branch 0 taken 12594 times.
✓ Branch 1 taken 433055 times.
445649 if (progressive_score > interlaced_score) {
2444 12594 s->c.interlaced_dct = 1;
2445
2446 12594 dct_offset = wrap_y;
2447 12594 uv_dct_offset = wrap_c;
2448 12594 wrap_y <<= 1;
2449
2/2
✓ Branch 0 taken 9048 times.
✓ Branch 1 taken 3546 times.
12594 if (chroma_format == CHROMA_422)
2450 9048 wrap_c <<= 1;
2451 }
2452 }
2453 }
2454
2455 4114649 s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2456 4114649 s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2457 4114649 s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2458 4114649 dest_y + dct_offset, wrap_y);
2459 4114649 s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2460 4114649 dest_y + dct_offset + 8, wrap_y);
2461
2462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4114649 times.
4114649 if (s->c.avctx->flags & AV_CODEC_FLAG_GRAY) {
2463 skip_dct[4] = 1;
2464 skip_dct[5] = 1;
2465 } else {
2466 4114649 s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2467 4114649 s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2468
2/2
✓ Branch 0 taken 347694 times.
✓ Branch 1 taken 3766955 times.
4114649 if (!chroma_y_shift) { /* 422 */
2469 347694 s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
2470 347694 dest_cb + uv_dct_offset, wrap_c);
2471 347694 s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
2472 347694 dest_cr + uv_dct_offset, wrap_c);
2473 }
2474 }
2475 /* pre quantization */
2476
2/2
✓ Branch 0 taken 3497400 times.
✓ Branch 1 taken 617249 times.
4114649 if (s->mc_mb_var[s->c.mb_stride * mb_y + mb_x] < 2 * s->c.qscale * s->c.qscale) {
2477 // FIXME optimize
2478
2/2
✓ Branch 1 taken 1385306 times.
✓ Branch 2 taken 2112094 times.
3497400 if (s->sad_cmp[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->c.qscale)
2479 1385306 skip_dct[0] = 1;
2480
2/2
✓ Branch 1 taken 1398361 times.
✓ Branch 2 taken 2099039 times.
3497400 if (s->sad_cmp[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->c.qscale)
2481 1398361 skip_dct[1] = 1;
2482 3497400 if (s->sad_cmp[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2483
2/2
✓ Branch 0 taken 1375791 times.
✓ Branch 1 taken 2121609 times.
3497400 wrap_y, 8) < 20 * s->c.qscale)
2484 1375791 skip_dct[2] = 1;
2485 3497400 if (s->sad_cmp[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2486
2/2
✓ Branch 0 taken 1340613 times.
✓ Branch 1 taken 2156787 times.
3497400 wrap_y, 8) < 20 * s->c.qscale)
2487 1340613 skip_dct[3] = 1;
2488
2/2
✓ Branch 1 taken 1692176 times.
✓ Branch 2 taken 1805224 times.
3497400 if (s->sad_cmp[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->c.qscale)
2489 1692176 skip_dct[4] = 1;
2490
2/2
✓ Branch 1 taken 1662544 times.
✓ Branch 2 taken 1834856 times.
3497400 if (s->sad_cmp[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->c.qscale)
2491 1662544 skip_dct[5] = 1;
2492
2/2
✓ Branch 0 taken 287272 times.
✓ Branch 1 taken 3210128 times.
3497400 if (!chroma_y_shift) { /* 422 */
2493 574544 if (s->sad_cmp[1](NULL, ptr_cb + uv_dct_offset,
2494 287272 dest_cb + uv_dct_offset,
2495
2/2
✓ Branch 0 taken 155766 times.
✓ Branch 1 taken 131506 times.
287272 wrap_c, 8) < 20 * s->c.qscale)
2496 155766 skip_dct[6] = 1;
2497 574544 if (s->sad_cmp[1](NULL, ptr_cr + uv_dct_offset,
2498 287272 dest_cr + uv_dct_offset,
2499
2/2
✓ Branch 0 taken 149390 times.
✓ Branch 1 taken 137882 times.
287272 wrap_c, 8) < 20 * s->c.qscale)
2500 149390 skip_dct[7] = 1;
2501 }
2502 }
2503 }
2504
2505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5560734 times.
5560734 if (s->quantizer_noise_shaping) {
2506 if (!skip_dct[0])
2507 get_visual_weight(weight[0], ptr_y , wrap_y);
2508 if (!skip_dct[1])
2509 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2510 if (!skip_dct[2])
2511 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2512 if (!skip_dct[3])
2513 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2514 if (!skip_dct[4])
2515 get_visual_weight(weight[4], ptr_cb , wrap_c);
2516 if (!skip_dct[5])
2517 get_visual_weight(weight[5], ptr_cr , wrap_c);
2518 if (!chroma_y_shift) { /* 422 */
2519 if (!skip_dct[6])
2520 get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2521 wrap_c);
2522 if (!skip_dct[7])
2523 get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2524 wrap_c);
2525 }
2526 memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2527 }
2528
2529 /* DCT & quantize */
2530 av_assert2(s->c.out_format != FMT_MJPEG || s->c.qscale == 8);
2531 {
2532
2/2
✓ Branch 0 taken 35604698 times.
✓ Branch 1 taken 5560734 times.
41165432 for (i = 0; i < mb_block_count; i++) {
2533
2/2
✓ Branch 0 taken 26444751 times.
✓ Branch 1 taken 9159947 times.
35604698 if (!skip_dct[i]) {
2534 int overflow;
2535 26444751 s->c.block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->c.qscale, &overflow);
2536 // FIXME we could decide to change to quantizer instead of
2537 // clipping
2538 // JS: I don't think that would be a good idea it could lower
2539 // quality instead of improve it. Just INTRADC clipping
2540 // deserves changes in quantizer
2541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26444751 times.
26444751 if (overflow)
2542 clip_coeffs(s, s->block[i], s->c.block_last_index[i]);
2543 } else
2544 9159947 s->c.block_last_index[i] = -1;
2545 }
2546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5560734 times.
5560734 if (s->quantizer_noise_shaping) {
2547 for (i = 0; i < mb_block_count; i++) {
2548 if (!skip_dct[i]) {
2549 s->c.block_last_index[i] =
2550 dct_quantize_refine(s, s->block[i], weight[i],
2551 orig[i], i, s->c.qscale);
2552 }
2553 }
2554 }
2555
2556
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5560734 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5560734 if (s->luma_elim_threshold && !s->c.mb_intra)
2557 for (i = 0; i < 4; i++)
2558 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2559
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5560734 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5560734 if (s->chroma_elim_threshold && !s->c.mb_intra)
2560 for (i = 4; i < mb_block_count; i++)
2561 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2562
2563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5560734 times.
5560734 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2564 for (i = 0; i < mb_block_count; i++) {
2565 if (s->c.block_last_index[i] == -1)
2566 s->coded_score[i] = INT_MAX / 256;
2567 }
2568 }
2569 }
2570
2571
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5560734 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5560734 if ((s->c.avctx->flags & AV_CODEC_FLAG_GRAY) && s->c.mb_intra) {
2572 s->c.block_last_index[4] =
2573 s->c.block_last_index[5] = 0;
2574 s->block[4][0] =
2575 s->block[5][0] = (1024 + s->c.c_dc_scale / 2) / s->c.c_dc_scale;
2576 if (!chroma_y_shift) { /* 422 / 444 */
2577 for (i=6; i<12; i++) {
2578 s->c.block_last_index[i] = 0;
2579 s->block[i][0] = s->block[4][0];
2580 }
2581 }
2582 }
2583
2584 // non c quantize code returns incorrect block_last_index FIXME
2585
3/4
✓ Branch 0 taken 60000 times.
✓ Branch 1 taken 5500734 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 60000 times.
5560734 if (s->c.alternate_scan && s->dct_quantize != dct_quantize_c) {
2586 for (i = 0; i < mb_block_count; i++) {
2587 int j;
2588 if (s->c.block_last_index[i] > 0) {
2589 for (j = 63; j > 0; j--) {
2590 if (s->block[i][s->c.intra_scantable.permutated[j]])
2591 break;
2592 }
2593 s->c.block_last_index[i] = j;
2594 }
2595 }
2596 }
2597
2598 5560734 s->encode_mb(s, s->block, motion_x, motion_y);
2599 5560734 }
2600
2601 5560734 static void encode_mb(MPVEncContext *const s, int motion_x, int motion_y)
2602 {
2603
2/2
✓ Branch 0 taken 4679215 times.
✓ Branch 1 taken 881519 times.
5560734 if (s->c.chroma_format == CHROMA_420)
2604 4679215 encode_mb_internal(s, motion_x, motion_y, 8, 8, 6, 1, 1, CHROMA_420);
2605
2/2
✓ Branch 0 taken 762205 times.
✓ Branch 1 taken 119314 times.
881519 else if (s->c.chroma_format == CHROMA_422)
2606 762205 encode_mb_internal(s, motion_x, motion_y, 16, 8, 8, 1, 0, CHROMA_422);
2607 else
2608 119314 encode_mb_internal(s, motion_x, motion_y, 16, 16, 12, 0, 0, CHROMA_444);
2609 5560734 }
2610
2611 typedef struct MBBackup {
2612 struct {
2613 int mv[2][4][2];
2614 int last_mv[2][2][2];
2615 int mv_type, mv_dir;
2616 int mb_intra, mb_skipped;
2617 int qscale;
2618 int block_last_index[8];
2619 int interlaced_dct;
2620 } c;
2621 int mb_skip_run;
2622 int last_dc[3];
2623 int mv_bits, i_tex_bits, p_tex_bits, i_count, misc_bits, last_bits;
2624 int dquant;
2625 int esc3_level_length;
2626 int16_t (*block)[64];
2627 PutBitContext pb, pb2, tex_pb;
2628 } MBBackup;
2629
2630 #define COPY_CONTEXT(BEFORE, AFTER, DST_TYPE, SRC_TYPE) \
2631 static inline void BEFORE ##_context_before_encode(DST_TYPE *const d, \
2632 const SRC_TYPE *const s) \
2633 { \
2634 /* FIXME is memcpy faster than a loop? */ \
2635 memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
2636 \
2637 /* MPEG-1 */ \
2638 d->mb_skip_run = s->mb_skip_run; \
2639 for (int i = 0; i < 3; i++) \
2640 d->last_dc[i] = s->last_dc[i]; \
2641 \
2642 /* statistics */ \
2643 d->mv_bits = s->mv_bits; \
2644 d->i_tex_bits = s->i_tex_bits; \
2645 d->p_tex_bits = s->p_tex_bits; \
2646 d->i_count = s->i_count; \
2647 d->misc_bits = s->misc_bits; \
2648 d->last_bits = 0; \
2649 \
2650 d->c.mb_skipped = 0; \
2651 d->c.qscale = s->c.qscale; \
2652 d->dquant = s->dquant; \
2653 \
2654 d->esc3_level_length = s->esc3_level_length; \
2655 } \
2656 \
2657 static inline void AFTER ## _context_after_encode(DST_TYPE *const d, \
2658 const SRC_TYPE *const s, \
2659 int data_partitioning) \
2660 { \
2661 /* FIXME is memcpy faster than a loop? */ \
2662 memcpy(d->c.mv, s->c.mv, 2*4*2*sizeof(int)); \
2663 memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
2664 \
2665 /* MPEG-1 */ \
2666 d->mb_skip_run = s->mb_skip_run; \
2667 for (int i = 0; i < 3; i++) \
2668 d->last_dc[i] = s->last_dc[i]; \
2669 \
2670 /* statistics */ \
2671 d->mv_bits = s->mv_bits; \
2672 d->i_tex_bits = s->i_tex_bits; \
2673 d->p_tex_bits = s->p_tex_bits; \
2674 d->i_count = s->i_count; \
2675 d->misc_bits = s->misc_bits; \
2676 \
2677 d->c.mb_intra = s->c.mb_intra; \
2678 d->c.mb_skipped = s->c.mb_skipped; \
2679 d->c.mv_type = s->c.mv_type; \
2680 d->c.mv_dir = s->c.mv_dir; \
2681 d->pb = s->pb; \
2682 if (data_partitioning) { \
2683 d->pb2 = s->pb2; \
2684 d->tex_pb = s->tex_pb; \
2685 } \
2686 d->block = s->block; \
2687 for (int i = 0; i < 8; i++) \
2688 d->c.block_last_index[i] = s->c.block_last_index[i]; \
2689 d->c.interlaced_dct = s->c.interlaced_dct; \
2690 d->c.qscale = s->c.qscale; \
2691 \
2692 d->esc3_level_length = s->esc3_level_length; \
2693 }
2694
2695
6/6
✓ Branch 0 taken 4656228 times.
✓ Branch 1 taken 1552076 times.
✓ Branch 2 taken 232816 times.
✓ Branch 3 taken 769570 times.
✓ Branch 4 taken 8019088 times.
✓ Branch 5 taken 1002386 times.
28454784 COPY_CONTEXT(backup, save, MBBackup, MPVEncContext)
2696
6/6
✓ Branch 0 taken 7933533 times.
✓ Branch 1 taken 2644511 times.
✓ Branch 2 taken 141085 times.
✓ Branch 3 taken 408605 times.
✓ Branch 4 taken 4397520 times.
✓ Branch 5 taken 549690 times.
29951128 COPY_CONTEXT(reset, store, MPVEncContext, MBBackup)
2697
2698 2094821 static void encode_mb_hq(MPVEncContext *const s, MBBackup *const backup, MBBackup *const best,
2699 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2700 int *dmin, int *next_block, int motion_x, int motion_y)
2701 {
2702 int score;
2703 uint8_t *dest_backup[3];
2704
2705 2094821 reset_context_before_encode(s, backup);
2706
2707 2094821 s->block = s->blocks[*next_block];
2708 2094821 s->pb = pb[*next_block];
2709
2/2
✓ Branch 0 taken 376394 times.
✓ Branch 1 taken 1718427 times.
2094821 if (s->data_partitioning) {
2710 376394 s->pb2 = pb2 [*next_block];
2711 376394 s->tex_pb= tex_pb[*next_block];
2712 }
2713
2714
2/2
✓ Branch 0 taken 1127720 times.
✓ Branch 1 taken 967101 times.
2094821 if(*next_block){
2715 1127720 memcpy(dest_backup, s->c.dest, sizeof(s->c.dest));
2716 1127720 s->c.dest[0] = s->c.sc.rd_scratchpad;
2717 1127720 s->c.dest[1] = s->c.sc.rd_scratchpad + 16*s->c.linesize;
2718 1127720 s->c.dest[2] = s->c.sc.rd_scratchpad + 16*s->c.linesize + 8;
2719
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1127720 times.
1127720 av_assert0(s->c.linesize >= 32); //FIXME
2720 }
2721
2722 2094821 encode_mb(s, motion_x, motion_y);
2723
2724 2094821 score= put_bits_count(&s->pb);
2725
2/2
✓ Branch 0 taken 376394 times.
✓ Branch 1 taken 1718427 times.
2094821 if (s->data_partitioning) {
2726 376394 score+= put_bits_count(&s->pb2);
2727 376394 score+= put_bits_count(&s->tex_pb);
2728 }
2729
2730
2/2
✓ Branch 0 taken 1717862 times.
✓ Branch 1 taken 376959 times.
2094821 if (s->c.avctx->mb_decision == FF_MB_DECISION_RD) {
2731 1717862 mpv_reconstruct_mb(s, s->block);
2732
2733 1717862 score *= s->lambda2;
2734 1717862 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2735 }
2736
2737
2/2
✓ Branch 0 taken 1127720 times.
✓ Branch 1 taken 967101 times.
2094821 if(*next_block){
2738 1127720 memcpy(s->c.dest, dest_backup, sizeof(s->c.dest));
2739 }
2740
2741
2/2
✓ Branch 0 taken 1002386 times.
✓ Branch 1 taken 1092435 times.
2094821 if(score<*dmin){
2742 1002386 *dmin= score;
2743 1002386 *next_block^=1;
2744
2745 1002386 save_context_after_encode(best, s, s->data_partitioning);
2746 }
2747 2094821 }
2748
2749 24192 static int sse(const MPVEncContext *const s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride)
2750 {
2751 24192 const uint32_t *sq = ff_square_tab + 256;
2752 24192 int acc=0;
2753 int x,y;
2754
2755
3/4
✓ Branch 0 taken 3721 times.
✓ Branch 1 taken 20471 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3721 times.
24192 if(w==16 && h==16)
2756 return s->sse_cmp[0](NULL, src1, src2, stride, 16);
2757
3/4
✓ Branch 0 taken 7442 times.
✓ Branch 1 taken 16750 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7442 times.
24192 else if(w==8 && h==8)
2758 return s->sse_cmp[1](NULL, src1, src2, stride, 8);
2759
2760
2/2
✓ Branch 0 taken 115458 times.
✓ Branch 1 taken 24192 times.
139650 for(y=0; y<h; y++){
2761
2/2
✓ Branch 0 taken 304016 times.
✓ Branch 1 taken 115458 times.
419474 for(x=0; x<w; x++){
2762 304016 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2763 }
2764 }
2765
2766 av_assert2(acc>=0);
2767
2768 24192 return acc;
2769 }
2770
2771 1717862 static int sse_mb(MPVEncContext *const s)
2772 {
2773 1717862 int w= 16;
2774 1717862 int h= 16;
2775 1717862 int chroma_mb_w = w >> s->c.chroma_x_shift;
2776 1717862 int chroma_mb_h = h >> s->c.chroma_y_shift;
2777
2778
2/2
✓ Branch 0 taken 4343 times.
✓ Branch 1 taken 1713519 times.
1717862 if (s->c.mb_x*16 + 16 > s->c.width ) w = s->c.width - s->c.mb_x*16;
2779
2/2
✓ Branch 0 taken 5366 times.
✓ Branch 1 taken 1712496 times.
1717862 if (s->c.mb_y*16 + 16 > s->c.height) h = s->c.height- s->c.mb_y*16;
2780
2781
4/4
✓ Branch 0 taken 1713519 times.
✓ Branch 1 taken 4343 times.
✓ Branch 2 taken 1709798 times.
✓ Branch 3 taken 3721 times.
1717862 if(w==16 && h==16)
2782 1709798 return s->n_sse_cmp[0](s, s->new_pic->data[0] + s->c.mb_x * 16 + s->c.mb_y * s->c.linesize * 16,
2783 1709798 s->c.dest[0], s->c.linesize, 16) +
2784 1709798 s->n_sse_cmp[1](s, s->new_pic->data[1] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
2785 3419596 s->c.dest[1], s->c.uvlinesize, chroma_mb_h) +
2786 1709798 s->n_sse_cmp[1](s, s->new_pic->data[2] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
2787 1709798 s->c.dest[2], s->c.uvlinesize, chroma_mb_h);
2788 else
2789 8064 return sse(s, s->new_pic->data[0] + s->c.mb_x * 16 + s->c.mb_y * s->c.linesize * 16,
2790 8064 s->c.dest[0], w, h, s->c.linesize) +
2791 8064 sse(s, s->new_pic->data[1] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
2792 16128 s->c.dest[1], w >> s->c.chroma_x_shift, h >> s->c.chroma_y_shift, s->c.uvlinesize) +
2793 8064 sse(s, s->new_pic->data[2] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
2794 8064 s->c.dest[2], w >> s->c.chroma_x_shift, h >> s->c.chroma_y_shift, s->c.uvlinesize);
2795 }
2796
2797 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2798 MPVEncContext *const s = *(void**)arg;
2799
2800
2801 s->me.pre_pass = 1;
2802 s->me.dia_size = s->c.avctx->pre_dia_size;
2803 s->c.first_slice_line = 1;
2804 for (s->c.mb_y = s->c.end_mb_y - 1; s->c.mb_y >= s->c.start_mb_y; s->c.mb_y--) {
2805 for (s->c.mb_x = s->c.mb_width - 1; s->c.mb_x >=0 ; s->c.mb_x--)
2806 ff_pre_estimate_p_frame_motion(s, s->c.mb_x, s->c.mb_y);
2807 s->c.first_slice_line = 0;
2808 }
2809
2810 s->me.pre_pass = 0;
2811
2812 return 0;
2813 }
2814
2815 11207 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2816 11207 MPVEncContext *const s = *(void**)arg;
2817
2818 11207 s->me.dia_size = s->c.avctx->dia_size;
2819 11207 s->c.first_slice_line = 1;
2820
2/2
✓ Branch 0 taken 126562 times.
✓ Branch 1 taken 11207 times.
137769 for (s->c.mb_y = s->c.start_mb_y; s->c.mb_y < s->c.end_mb_y; s->c.mb_y++) {
2821 126562 s->c.mb_x = 0; //for block init below
2822 126562 ff_init_block_index(&s->c);
2823
2/2
✓ Branch 0 taken 2853353 times.
✓ Branch 1 taken 126562 times.
2979915 for (s->c.mb_x = 0; s->c.mb_x < s->c.mb_width; s->c.mb_x++) {
2824 2853353 s->c.block_index[0] += 2;
2825 2853353 s->c.block_index[1] += 2;
2826 2853353 s->c.block_index[2] += 2;
2827 2853353 s->c.block_index[3] += 2;
2828
2829 /* compute motion vector & mb_type and store in context */
2830
2/2
✓ Branch 0 taken 402556 times.
✓ Branch 1 taken 2450797 times.
2853353 if (s->c.pict_type == AV_PICTURE_TYPE_B)
2831 402556 ff_estimate_b_frame_motion(s, s->c.mb_x, s->c.mb_y);
2832 else
2833 2450797 ff_estimate_p_frame_motion(s, s->c.mb_x, s->c.mb_y);
2834 }
2835 126562 s->c.first_slice_line = 0;
2836 }
2837 11207 return 0;
2838 }
2839
2840 1123 static int mb_var_thread(AVCodecContext *c, void *arg){
2841 1123 MPVEncContext *const s = *(void**)arg;
2842
2843
2/2
✓ Branch 0 taken 14853 times.
✓ Branch 1 taken 1123 times.
15976 for (int mb_y = s->c.start_mb_y; mb_y < s->c.end_mb_y; mb_y++) {
2844
2/2
✓ Branch 0 taken 330554 times.
✓ Branch 1 taken 14853 times.
345407 for (int mb_x = 0; mb_x < s->c.mb_width; mb_x++) {
2845 330554 int xx = mb_x * 16;
2846 330554 int yy = mb_y * 16;
2847 330554 const uint8_t *pix = s->new_pic->data[0] + (yy * s->c.linesize) + xx;
2848 int varc;
2849 330554 int sum = s->mpvencdsp.pix_sum(pix, s->c.linesize);
2850
2851 330554 varc = (s->mpvencdsp.pix_norm1(pix, s->c.linesize) -
2852 330554 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2853
2854 330554 s->mb_var [s->c.mb_stride * mb_y + mb_x] = varc;
2855 330554 s->mb_mean[s->c.mb_stride * mb_y + mb_x] = (sum+128)>>8;
2856 330554 s->me.mb_var_sum_temp += varc;
2857 }
2858 }
2859 1123 return 0;
2860 }
2861
2862 333174 static void write_slice_end(MPVEncContext *const s)
2863 {
2864
2/2
✓ Branch 0 taken 6114 times.
✓ Branch 1 taken 327060 times.
333174 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4) {
2865
2/2
✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 4354 times.
6114 if (s->partitioned_frame)
2866 1760 ff_mpeg4_merge_partitions(s);
2867
2868 6114 ff_mpeg4_stuffing(&s->pb);
2869 327060 } else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2870
2/2
✓ Branch 0 taken 4089 times.
✓ Branch 1 taken 322971 times.
327060 s->c.out_format == FMT_MJPEG) {
2871 4089 ff_mjpeg_encode_stuffing(s);
2872
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 322521 times.
322971 } else if (CONFIG_SPEEDHQ_ENCODER && s->c.out_format == FMT_SPEEDHQ) {
2873 450 ff_speedhq_end_slice(s);
2874 }
2875
2876 333174 flush_put_bits(&s->pb);
2877
2878
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 333174 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
333174 if ((s->c.avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
2879 s->misc_bits+= get_bits_diff(s);
2880 333174 }
2881
2882 static void write_mb_info(MPVEncContext *const s)
2883 {
2884 uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2885 int offset = put_bits_count(&s->pb);
2886 int mba = s->c.mb_x + s->c.mb_width * (s->c.mb_y % s->gob_index);
2887 int gobn = s->c.mb_y / s->gob_index;
2888 int pred_x, pred_y;
2889 if (CONFIG_H263_ENCODER)
2890 ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
2891 bytestream_put_le32(&ptr, offset);
2892 bytestream_put_byte(&ptr, s->c.qscale);
2893 bytestream_put_byte(&ptr, gobn);
2894 bytestream_put_le16(&ptr, mba);
2895 bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2896 bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2897 /* 4MV not implemented */
2898 bytestream_put_byte(&ptr, 0); /* hmv2 */
2899 bytestream_put_byte(&ptr, 0); /* vmv2 */
2900 }
2901
2902 4015603 static void update_mb_info(MPVEncContext *const s)
2903 {
2904
1/2
✓ Branch 0 taken 4015603 times.
✗ Branch 1 not taken.
4015603 if (!s->mb_info)
2905 4015603 return;
2906 if (put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info) {
2907 s->mb_info_size += 12;
2908 s->prev_mb_info = s->last_mb_info;
2909 }
2910
2911 s->last_mb_info = put_bytes_count(&s->pb, 0);
2912 if (!s->mb_info_size)
2913 s->mb_info_size += 12;
2914 write_mb_info(s);
2915 }
2916
2917 4020531 int ff_mpv_reallocate_putbitbuffer(MPVEncContext *const s, size_t threshold, size_t size_increase)
2918 {
2919
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 4020499 times.
4020531 if (put_bytes_left(&s->pb, 0) < threshold
2920
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 && s->c.slice_context_count == 1
2921
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 && s->pb.buf == s->c.avctx->internal->byte_buffer) {
2922 32 int lastgob_pos = s->ptr_lastgob - s->pb.buf;
2923
2924 32 uint8_t *new_buffer = NULL;
2925 32 int new_buffer_size = 0;
2926
2927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if ((s->c.avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2928 av_log(s->c.avctx, AV_LOG_ERROR, "Cannot reallocate putbit buffer\n");
2929 return AVERROR(ENOMEM);
2930 }
2931
2932 32 emms_c();
2933
2934 32 av_fast_padded_malloc(&new_buffer, &new_buffer_size,
2935 32 s->c.avctx->internal->byte_buffer_size + size_increase);
2936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!new_buffer)
2937 return AVERROR(ENOMEM);
2938
2939 32 memcpy(new_buffer, s->c.avctx->internal->byte_buffer, s->c.avctx->internal->byte_buffer_size);
2940 32 av_free(s->c.avctx->internal->byte_buffer);
2941 32 s->c.avctx->internal->byte_buffer = new_buffer;
2942 32 s->c.avctx->internal->byte_buffer_size = new_buffer_size;
2943 32 rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
2944 32 s->ptr_lastgob = s->pb.buf + lastgob_pos;
2945 }
2946
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4020531 times.
4020531 if (put_bytes_left(&s->pb, 0) < threshold)
2947 return AVERROR(EINVAL);
2948 4020531 return 0;
2949 }
2950
2951 15018 static int encode_thread(AVCodecContext *c, void *arg){
2952 15018 MPVEncContext *const s = *(void**)arg;
2953 15018 int chr_h = 16 >> s->c.chroma_y_shift;
2954 int i;
2955 15018 MBBackup best_s = { 0 }, backup_s;
2956 uint8_t bit_buf[2][MAX_MB_BYTES];
2957 // + 2 because ff_copy_bits() overreads
2958 uint8_t bit_buf2[2][MAX_PB2_MB_SIZE + 2];
2959 uint8_t bit_buf_tex[2][MAX_AC_TEX_MB_SIZE + 2];
2960 PutBitContext pb[2], pb2[2], tex_pb[2];
2961
2962
2/2
✓ Branch 0 taken 30036 times.
✓ Branch 1 taken 15018 times.
45054 for(i=0; i<2; i++){
2963 30036 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2964 30036 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_PB2_MB_SIZE);
2965 30036 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_AC_TEX_MB_SIZE);
2966 }
2967
2968 15018 s->last_bits= put_bits_count(&s->pb);
2969 15018 s->mv_bits=0;
2970 15018 s->misc_bits=0;
2971 15018 s->i_tex_bits=0;
2972 15018 s->p_tex_bits=0;
2973 15018 s->i_count=0;
2974
2975
2/2
✓ Branch 0 taken 45054 times.
✓ Branch 1 taken 15018 times.
60072 for(i=0; i<3; i++){
2976 /* init last dc values */
2977 /* note: quant matrix value (8) is implied here */
2978 45054 s->last_dc[i] = 128 << s->c.intra_dc_precision;
2979
2980 45054 s->encoding_error[i] = 0;
2981 }
2982
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 14818 times.
15018 if (s->c.codec_id == AV_CODEC_ID_AMV) {
2983 200 s->last_dc[0] = 128 * 8 / 13;
2984 200 s->last_dc[1] = 128 * 8 / 14;
2985 200 s->last_dc[2] = 128 * 8 / 14;
2986 #if CONFIG_MPEG4_ENCODER
2987
2/2
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 14274 times.
14818 } else if (s->partitioned_frame) {
2988 av_assert1(s->c.codec_id == AV_CODEC_ID_MPEG4);
2989 544 ff_mpeg4_init_partitions(s);
2990 #endif
2991 }
2992 15018 s->mb_skip_run = 0;
2993 15018 memset(s->c.last_mv, 0, sizeof(s->c.last_mv));
2994
2995 15018 s->last_mv_dir = 0;
2996
2997 15018 s->c.resync_mb_x = 0;
2998 15018 s->c.resync_mb_y = 0;
2999 15018 s->c.first_slice_line = 1;
3000 15018 s->ptr_lastgob = s->pb.buf;
3001
2/2
✓ Branch 0 taken 173939 times.
✓ Branch 1 taken 15018 times.
188957 for (int mb_y_order = s->c.start_mb_y; mb_y_order < s->c.end_mb_y; mb_y_order++) {
3002 int mb_y;
3003
2/2
✓ Branch 0 taken 8100 times.
✓ Branch 1 taken 165839 times.
173939 if (CONFIG_SPEEDHQ_ENCODER && s->c.codec_id == AV_CODEC_ID_SPEEDHQ) {
3004 int first_in_slice;
3005 8100 mb_y = ff_speedhq_mb_y_order_to_mb(mb_y_order, s->c.mb_height, &first_in_slice);
3006
4/4
✓ Branch 0 taken 1800 times.
✓ Branch 1 taken 6300 times.
✓ Branch 2 taken 1350 times.
✓ Branch 3 taken 450 times.
8100 if (first_in_slice && mb_y_order != s->c.start_mb_y)
3007 1350 ff_speedhq_end_slice(s);
3008 8100 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 1024;
3009 } else {
3010 165839 mb_y = mb_y_order;
3011 }
3012 173939 s->c.mb_x = 0;
3013 173939 s->c.mb_y = mb_y;
3014
3015 173939 ff_set_qscale(&s->c, s->c.qscale);
3016 173939 ff_init_block_index(&s->c);
3017
3018
2/2
✓ Branch 0 taken 4015603 times.
✓ Branch 1 taken 173939 times.
4189542 for (int mb_x = 0; mb_x < s->c.mb_width; mb_x++) {
3019 int mb_type, xy;
3020 // int d;
3021 4015603 int dmin= INT_MAX;
3022 int dir;
3023 4015603 int size_increase = s->c.avctx->internal->byte_buffer_size/4
3024 4015603 + s->c.mb_width*MAX_MB_BYTES;
3025
3026 4015603 ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
3027
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4015603 times.
4015603 if (put_bytes_left(&s->pb, 0) < MAX_MB_BYTES){
3028 av_log(s->c.avctx, AV_LOG_ERROR, "encoded frame too large\n");
3029 return -1;
3030 }
3031
2/2
✓ Branch 0 taken 179550 times.
✓ Branch 1 taken 3836053 times.
4015603 if (s->data_partitioning) {
3032
2/4
✓ Branch 1 taken 179550 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 179550 times.
359100 if (put_bytes_left(&s->pb2, 0) < MAX_MB_BYTES ||
3033 179550 put_bytes_left(&s->tex_pb, 0) < MAX_MB_BYTES) {
3034 av_log(s->c.avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
3035 return -1;
3036 }
3037 }
3038
3039 4015603 s->c.mb_x = mb_x;
3040 4015603 s->c.mb_y = mb_y; // moved into loop, can get changed by H.261
3041 4015603 ff_update_block_index(&s->c, 8, 0, s->c.chroma_x_shift);
3042
3043
2/2
✓ Branch 0 taken 119790 times.
✓ Branch 1 taken 3895813 times.
4015603 if (CONFIG_H261_ENCODER && s->c.codec_id == AV_CODEC_ID_H261)
3044 119790 ff_h261_reorder_mb_index(s);
3045 4015603 xy = s->c.mb_y * s->c.mb_stride + s->c.mb_x;
3046 4015603 mb_type = s->mb_type[xy];
3047
3048 /* write gob / video packet header */
3049
2/2
✓ Branch 0 taken 1908809 times.
✓ Branch 1 taken 2106794 times.
4015603 if(s->rtp_mode){
3050 int current_packet_size, is_gob_start;
3051
3052 1908809 current_packet_size = put_bytes_count(&s->pb, 1)
3053 1908809 - (s->ptr_lastgob - s->pb.buf);
3054
3055 4372918 is_gob_start = s->rtp_payload_size &&
3056
4/4
✓ Branch 0 taken 555300 times.
✓ Branch 1 taken 1353509 times.
✓ Branch 2 taken 310021 times.
✓ Branch 3 taken 245279 times.
2218830 current_packet_size >= s->rtp_payload_size &&
3057
2/2
✓ Branch 0 taken 309871 times.
✓ Branch 1 taken 150 times.
310021 mb_y + mb_x > 0;
3058
3059
6/6
✓ Branch 0 taken 171626 times.
✓ Branch 1 taken 1737183 times.
✓ Branch 2 taken 75146 times.
✓ Branch 3 taken 96480 times.
✓ Branch 4 taken 2768 times.
✓ Branch 5 taken 72378 times.
1908809 if (s->c.start_mb_y == mb_y && mb_y > 0 && mb_x == 0) is_gob_start = 1;
3060
3061
5/5
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 1547743 times.
✓ Branch 2 taken 59850 times.
✓ Branch 3 taken 59850 times.
✓ Branch 4 taken 181966 times.
1908809 switch (s->c.codec_id) {
3062 59400 case AV_CODEC_ID_H263:
3063 case AV_CODEC_ID_H263P:
3064
1/2
✓ Branch 0 taken 59400 times.
✗ Branch 1 not taken.
59400 if (!s->h263_slice_structured)
3065
3/4
✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 56700 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2700 times.
59400 if (s->c.mb_x || s->c.mb_y % s->gob_index) is_gob_start = 0;
3066 59400 break;
3067 1547743 case AV_CODEC_ID_MPEG2VIDEO:
3068
4/4
✓ Branch 0 taken 59610 times.
✓ Branch 1 taken 1488133 times.
✓ Branch 2 taken 54762 times.
✓ Branch 3 taken 4848 times.
1547743 if (s->c.mb_x == 0 && s->c.mb_y != 0) is_gob_start = 1;
3069 av_fallthrough;
3070 case AV_CODEC_ID_MPEG1VIDEO:
3071
3/4
✓ Branch 0 taken 59850 times.
✓ Branch 1 taken 1547743 times.
✓ Branch 2 taken 59850 times.
✗ Branch 3 not taken.
1607593 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO && s->c.mb_y >= 175 ||
3072
2/2
✓ Branch 0 taken 413888 times.
✓ Branch 1 taken 1193705 times.
1607593 s->mb_skip_run)
3073 413888 is_gob_start=0;
3074 1607593 break;
3075 59850 case AV_CODEC_ID_MJPEG:
3076
4/4
✓ Branch 0 taken 2850 times.
✓ Branch 1 taken 57000 times.
✓ Branch 2 taken 2650 times.
✓ Branch 3 taken 200 times.
59850 if (s->c.mb_x == 0 && s->c.mb_y != 0) is_gob_start = 1;
3077 59850 break;
3078 }
3079
3080
2/2
✓ Branch 0 taken 320924 times.
✓ Branch 1 taken 1587885 times.
1908809 if(is_gob_start){
3081
4/4
✓ Branch 0 taken 9899 times.
✓ Branch 1 taken 311025 times.
✓ Branch 2 taken 7131 times.
✓ Branch 3 taken 2768 times.
320924 if (s->c.start_mb_y != mb_y || mb_x != 0) {
3082 318156 write_slice_end(s);
3083
3084
4/4
✓ Branch 0 taken 3123 times.
✓ Branch 1 taken 315033 times.
✓ Branch 2 taken 1216 times.
✓ Branch 3 taken 1907 times.
318156 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4 && s->partitioned_frame)
3085 1216 ff_mpeg4_init_partitions(s);
3086 }
3087
3088 av_assert2((put_bits_count(&s->pb)&7) == 0);
3089 320924 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
3090
3091
4/4
✓ Branch 0 taken 437 times.
✓ Branch 1 taken 320487 times.
✓ Branch 2 taken 287 times.
✓ Branch 3 taken 150 times.
320924 if (s->error_rate && s->c.resync_mb_x + s->c.resync_mb_y > 0) {
3092 287 int r = put_bytes_count(&s->pb, 0) + s->picture_number + 16 + s->c.mb_x + s->c.mb_y;
3093 287 int d = 100 / s->error_rate;
3094
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 255 times.
287 if(r % d == 0){
3095 32 current_packet_size=0;
3096 32 s->pb.buf_ptr= s->ptr_lastgob;
3097 av_assert1(put_bits_ptr(&s->pb) == s->ptr_lastgob);
3098 }
3099 }
3100
3101
4/5
✓ Branch 0 taken 3375 times.
✓ Branch 1 taken 312355 times.
✓ Branch 2 taken 2544 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2650 times.
320924 switch (s->c.codec_id) {
3102 3375 case AV_CODEC_ID_MPEG4:
3103 if (CONFIG_MPEG4_ENCODER) {
3104 3375 ff_mpeg4_encode_video_packet_header(s);
3105 3375 ff_mpeg4_clean_buffers(&s->c);
3106 3375 ff_h263_mpeg4_reset_dc(s);
3107 }
3108 3375 break;
3109 312355 case AV_CODEC_ID_MPEG1VIDEO:
3110 case AV_CODEC_ID_MPEG2VIDEO:
3111 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3112 312355 ff_mpeg1_encode_slice_header(s);
3113 312355 ff_mpeg1_clean_buffers(s);
3114 }
3115 312355 break;
3116 #if CONFIG_H263P_ENCODER
3117 2544 case AV_CODEC_ID_H263P:
3118
1/2
✓ Branch 0 taken 2544 times.
✗ Branch 1 not taken.
2544 if (s->c.dc_val)
3119 2544 ff_h263_mpeg4_reset_dc(s);
3120 av_fallthrough;
3121 #endif
3122 case AV_CODEC_ID_H263:
3123 if (CONFIG_H263_ENCODER) {
3124
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2544 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2544 if (s->mb_info && put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info)
3125 s->mb_info_size += 12;
3126
3127 2544 ff_h263_encode_gob_header(s, mb_y);
3128 2544 s->prev_mb_info = put_bits_count(&s->pb)/8;
3129 }
3130 2544 break;
3131 }
3132
3133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 320924 times.
320924 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS1) {
3134 int bits= put_bits_count(&s->pb);
3135 s->misc_bits+= bits - s->last_bits;
3136 s->last_bits= bits;
3137 }
3138
3139 320924 s->ptr_lastgob += current_packet_size;
3140 320924 s->c.first_slice_line = 1;
3141 320924 s->c.resync_mb_x = mb_x;
3142 320924 s->c.resync_mb_y = mb_y;
3143 }
3144 }
3145
3146
2/2
✓ Branch 0 taken 431682 times.
✓ Branch 1 taken 3583921 times.
4015603 if (s->c.resync_mb_x == s->c.mb_x &&
3147
2/2
✓ Branch 0 taken 8941 times.
✓ Branch 1 taken 422741 times.
431682 s->c.resync_mb_y+1 == s->c.mb_y)
3148 8941 s->c.first_slice_line = 0;
3149
3150 4015603 s->c.mb_skipped = 0;
3151 4015603 s->dquant=0; //only for QP_RD
3152
3153 4015603 update_mb_info(s);
3154
3155
4/4
✓ Branch 0 taken 3484651 times.
✓ Branch 1 taken 530952 times.
✓ Branch 2 taken 18738 times.
✓ Branch 3 taken 3465913 times.
4565293 if (mb_type & (mb_type-1) || (s->mpv_flags & FF_MPV_FLAG_QP_RD)) { // more than 1 MB type possible or FF_MPV_FLAG_QP_RD
3156 549690 int next_block=0;
3157 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3158
3159 549690 backup_context_before_encode(&backup_s, s);
3160 549690 backup_s.pb= s->pb;
3161
2/2
✓ Branch 0 taken 141085 times.
✓ Branch 1 taken 408605 times.
549690 if (s->data_partitioning) {
3162 141085 backup_s.pb2= s->pb2;
3163 141085 backup_s.tex_pb= s->tex_pb;
3164 }
3165
3166
2/2
✓ Branch 0 taken 307119 times.
✓ Branch 1 taken 242571 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_INTER){
3167 307119 s->c.mv_dir = MV_DIR_FORWARD;
3168 307119 s->c.mv_type = MV_TYPE_16X16;
3169 307119 s->c.mb_intra = 0;
3170 307119 s->c.mv[0][0][0] = s->p_mv_table[xy][0];
3171 307119 s->c.mv[0][0][1] = s->p_mv_table[xy][1];
3172 307119 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3173 &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
3174 }
3175
2/2
✓ Branch 0 taken 13398 times.
✓ Branch 1 taken 536292 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
3176 13398 s->c.mv_dir = MV_DIR_FORWARD;
3177 13398 s->c.mv_type = MV_TYPE_FIELD;
3178 13398 s->c.mb_intra = 0;
3179
2/2
✓ Branch 0 taken 26796 times.
✓ Branch 1 taken 13398 times.
40194 for(i=0; i<2; i++){
3180 26796 int j = s->c.field_select[0][i] = s->p_field_select_table[i][xy];
3181 26796 s->c.mv[0][i][0] = s->c.p_field_mv_table[i][j][xy][0];
3182 26796 s->c.mv[0][i][1] = s->c.p_field_mv_table[i][j][xy][1];
3183 }
3184 13398 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3185 &dmin, &next_block, 0, 0);
3186 }
3187
2/2
✓ Branch 0 taken 85560 times.
✓ Branch 1 taken 464130 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
3188 85560 s->c.mv_dir = MV_DIR_FORWARD;
3189 85560 s->c.mv_type = MV_TYPE_16X16;
3190 85560 s->c.mb_intra = 0;
3191 85560 s->c.mv[0][0][0] = 0;
3192 85560 s->c.mv[0][0][1] = 0;
3193 85560 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3194 &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
3195 }
3196
2/2
✓ Branch 0 taken 233373 times.
✓ Branch 1 taken 316317 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
3197 233373 s->c.mv_dir = MV_DIR_FORWARD;
3198 233373 s->c.mv_type = MV_TYPE_8X8;
3199 233373 s->c.mb_intra = 0;
3200
2/2
✓ Branch 0 taken 933492 times.
✓ Branch 1 taken 233373 times.
1166865 for(i=0; i<4; i++){
3201 933492 s->c.mv[0][i][0] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
3202 933492 s->c.mv[0][i][1] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
3203 }
3204 233373 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3205 &dmin, &next_block, 0, 0);
3206 }
3207
2/2
✓ Branch 0 taken 222519 times.
✓ Branch 1 taken 327171 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
3208 222519 s->c.mv_dir = MV_DIR_FORWARD;
3209 222519 s->c.mv_type = MV_TYPE_16X16;
3210 222519 s->c.mb_intra = 0;
3211 222519 s->c.mv[0][0][0] = s->b_forw_mv_table[xy][0];
3212 222519 s->c.mv[0][0][1] = s->b_forw_mv_table[xy][1];
3213 222519 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3214 &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
3215 }
3216
2/2
✓ Branch 0 taken 222519 times.
✓ Branch 1 taken 327171 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
3217 222519 s->c.mv_dir = MV_DIR_BACKWARD;
3218 222519 s->c.mv_type = MV_TYPE_16X16;
3219 222519 s->c.mb_intra = 0;
3220 222519 s->c.mv[1][0][0] = s->b_back_mv_table[xy][0];
3221 222519 s->c.mv[1][0][1] = s->b_back_mv_table[xy][1];
3222 222519 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3223 &dmin, &next_block, s->c.mv[1][0][0], s->c.mv[1][0][1]);
3224 }
3225
2/2
✓ Branch 0 taken 222519 times.
✓ Branch 1 taken 327171 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
3226 222519 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3227 222519 s->c.mv_type = MV_TYPE_16X16;
3228 222519 s->c.mb_intra = 0;
3229 222519 s->c.mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3230 222519 s->c.mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3231 222519 s->c.mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3232 222519 s->c.mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3233 222519 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3234 &dmin, &next_block, 0, 0);
3235 }
3236
2/2
✓ Branch 0 taken 30990 times.
✓ Branch 1 taken 518700 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
3237 30990 s->c.mv_dir = MV_DIR_FORWARD;
3238 30990 s->c.mv_type = MV_TYPE_FIELD;
3239 30990 s->c.mb_intra = 0;
3240
2/2
✓ Branch 0 taken 61980 times.
✓ Branch 1 taken 30990 times.
92970 for(i=0; i<2; i++){
3241 61980 int j = s->c.field_select[0][i] = s->b_field_select_table[0][i][xy];
3242 61980 s->c.mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3243 61980 s->c.mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3244 }
3245 30990 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3246 &dmin, &next_block, 0, 0);
3247 }
3248
2/2
✓ Branch 0 taken 31154 times.
✓ Branch 1 taken 518536 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
3249 31154 s->c.mv_dir = MV_DIR_BACKWARD;
3250 31154 s->c.mv_type = MV_TYPE_FIELD;
3251 31154 s->c.mb_intra = 0;
3252
2/2
✓ Branch 0 taken 62308 times.
✓ Branch 1 taken 31154 times.
93462 for(i=0; i<2; i++){
3253 62308 int j = s->c.field_select[1][i] = s->b_field_select_table[1][i][xy];
3254 62308 s->c.mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3255 62308 s->c.mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3256 }
3257 31154 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3258 &dmin, &next_block, 0, 0);
3259 }
3260
2/2
✓ Branch 0 taken 26027 times.
✓ Branch 1 taken 523663 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
3261 26027 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3262 26027 s->c.mv_type = MV_TYPE_FIELD;
3263 26027 s->c.mb_intra = 0;
3264
2/2
✓ Branch 0 taken 52054 times.
✓ Branch 1 taken 26027 times.
78081 for(dir=0; dir<2; dir++){
3265
2/2
✓ Branch 0 taken 104108 times.
✓ Branch 1 taken 52054 times.
156162 for(i=0; i<2; i++){
3266 104108 int j = s->c.field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3267 104108 s->c.mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3268 104108 s->c.mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3269 }
3270 }
3271 26027 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3272 &dmin, &next_block, 0, 0);
3273 }
3274
2/2
✓ Branch 0 taken 121348 times.
✓ Branch 1 taken 428342 times.
549690 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
3275 121348 s->c.mv_dir = 0;
3276 121348 s->c.mv_type = MV_TYPE_16X16;
3277 121348 s->c.mb_intra = 1;
3278 121348 s->c.mv[0][0][0] = 0;
3279 121348 s->c.mv[0][0][1] = 0;
3280 121348 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3281 &dmin, &next_block, 0, 0);
3282 121348 s->c.mbintra_table[xy] = 1;
3283 }
3284
3285
4/4
✓ Branch 0 taken 179700 times.
✓ Branch 1 taken 369990 times.
✓ Branch 2 taken 179660 times.
✓ Branch 3 taken 40 times.
549690 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
3286
2/2
✓ Branch 0 taken 166470 times.
✓ Branch 1 taken 13190 times.
179660 if (best_s.c.mv_type == MV_TYPE_16X16) { //FIXME move 4mv after QPRD
3287 166470 const int last_qp = backup_s.c.qscale;
3288 int qpi, qp, dc[6];
3289 int16_t ac[6][16];
3290 166470 const int mvdir = (best_s.c.mv_dir & MV_DIR_BACKWARD) ? 1 : 0;
3291 static const int dquant_tab[4]={-1,1,-2,2};
3292
4/4
✓ Branch 0 taken 32838 times.
✓ Branch 1 taken 133632 times.
✓ Branch 2 taken 10119 times.
✓ Branch 3 taken 22719 times.
166470 int storecoefs = s->c.mb_intra && s->c.dc_val;
3293
3294 av_assert2(backup_s.dquant == 0);
3295
3296 //FIXME intra
3297 166470 s->c.mv_dir = best_s.c.mv_dir;
3298 166470 s->c.mv_type = MV_TYPE_16X16;
3299 166470 s->c.mb_intra = best_s.c.mb_intra;
3300 166470 s->c.mv[0][0][0] = best_s.c.mv[0][0][0];
3301 166470 s->c.mv[0][0][1] = best_s.c.mv[0][0][1];
3302 166470 s->c.mv[1][0][0] = best_s.c.mv[1][0][0];
3303 166470 s->c.mv[1][0][1] = best_s.c.mv[1][0][1];
3304
3305
2/2
✓ Branch 0 taken 111396 times.
✓ Branch 1 taken 55074 times.
166470 qpi = s->c.pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3306
2/2
✓ Branch 0 taken 443088 times.
✓ Branch 1 taken 166470 times.
609558 for(; qpi<4; qpi++){
3307 443088 int dquant= dquant_tab[qpi];
3308 443088 qp= last_qp + dquant;
3309
4/4
✓ Branch 0 taken 398061 times.
✓ Branch 1 taken 45027 times.
✓ Branch 2 taken 5810 times.
✓ Branch 3 taken 392251 times.
443088 if (qp < s->c.avctx->qmin || qp > s->c.avctx->qmax)
3310 50837 continue;
3311 392251 backup_s.dquant= dquant;
3312
2/2
✓ Branch 0 taken 37382 times.
✓ Branch 1 taken 354869 times.
392251 if(storecoefs){
3313
2/2
✓ Branch 0 taken 224292 times.
✓ Branch 1 taken 37382 times.
261674 for(i=0; i<6; i++){
3314 224292 dc[i] = s->c.dc_val[s->c.block_index[i]];
3315 224292 memcpy(ac[i], s->c.ac_val[s->c.block_index[i]], sizeof(*s->c.ac_val));
3316 }
3317 }
3318
3319 392251 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3320 &dmin, &next_block, s->c.mv[mvdir][0][0], s->c.mv[mvdir][0][1]);
3321
2/2
✓ Branch 0 taken 369889 times.
✓ Branch 1 taken 22362 times.
392251 if (best_s.c.qscale != qp) {
3322
2/2
✓ Branch 0 taken 33691 times.
✓ Branch 1 taken 336198 times.
369889 if(storecoefs){
3323
2/2
✓ Branch 0 taken 202146 times.
✓ Branch 1 taken 33691 times.
235837 for(i=0; i<6; i++){
3324 202146 s->c.dc_val[s->c.block_index[i]] = dc[i];
3325 202146 memcpy(s->c.ac_val[s->c.block_index[i]], ac[i], sizeof(*s->c.ac_val));
3326 }
3327 }
3328 }
3329 }
3330 }
3331 }
3332
2/2
✓ Branch 0 taken 145738 times.
✓ Branch 1 taken 403952 times.
549690 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3333 145738 int mx= s->b_direct_mv_table[xy][0];
3334 145738 int my= s->b_direct_mv_table[xy][1];
3335
3336 145738 backup_s.dquant = 0;
3337 145738 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3338 145738 s->c.mb_intra = 0;
3339 145738 ff_mpeg4_set_direct_mv(&s->c, mx, my);
3340 145738 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3341 &dmin, &next_block, mx, my);
3342 }
3343
2/2
✓ Branch 0 taken 40306 times.
✓ Branch 1 taken 509384 times.
549690 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3344 40306 backup_s.dquant = 0;
3345 40306 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3346 40306 s->c.mb_intra = 0;
3347 40306 ff_mpeg4_set_direct_mv(&s->c, 0, 0);
3348 40306 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3349 &dmin, &next_block, 0, 0);
3350 }
3351
3/4
✓ Branch 0 taken 509668 times.
✓ Branch 1 taken 40022 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 509668 times.
549690 if (!best_s.c.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3352 int coded=0;
3353 for(i=0; i<6; i++)
3354 coded |= s->c.block_last_index[i];
3355 if(coded){
3356 int mx,my;
3357 memcpy(s->c.mv, best_s.c.mv, sizeof(s->c.mv));
3358 if (CONFIG_MPEG4_ENCODER && best_s.c.mv_dir & MV_DIRECT) {
3359 mx=my=0; //FIXME find the one we actually used
3360 ff_mpeg4_set_direct_mv(&s->c, mx, my);
3361 } else if (best_s.c.mv_dir & MV_DIR_BACKWARD) {
3362 mx = s->c.mv[1][0][0];
3363 my = s->c.mv[1][0][1];
3364 }else{
3365 mx = s->c.mv[0][0][0];
3366 my = s->c.mv[0][0][1];
3367 }
3368
3369 s->c.mv_dir = best_s.c.mv_dir;
3370 s->c.mv_type = best_s.c.mv_type;
3371 s->c.mb_intra = 0;
3372 /* s->c.mv[0][0][0] = best_s.mv[0][0][0];
3373 s->c.mv[0][0][1] = best_s.mv[0][0][1];
3374 s->c.mv[1][0][0] = best_s.mv[1][0][0];
3375 s->c.mv[1][0][1] = best_s.mv[1][0][1];*/
3376 backup_s.dquant= 0;
3377 s->skipdct=1;
3378 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3379 &dmin, &next_block, mx, my);
3380 s->skipdct=0;
3381 }
3382 }
3383
3384 549690 store_context_after_encode(s, &best_s, s->data_partitioning);
3385
3386 549690 pb_bits_count= put_bits_count(&s->pb);
3387 549690 flush_put_bits(&s->pb);
3388 549690 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3389 549690 s->pb= backup_s.pb;
3390
3391
2/2
✓ Branch 0 taken 141085 times.
✓ Branch 1 taken 408605 times.
549690 if (s->data_partitioning) {
3392 141085 pb2_bits_count= put_bits_count(&s->pb2);
3393 141085 flush_put_bits(&s->pb2);
3394 141085 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3395 141085 s->pb2= backup_s.pb2;
3396
3397 141085 tex_pb_bits_count= put_bits_count(&s->tex_pb);
3398 141085 flush_put_bits(&s->tex_pb);
3399 141085 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3400 141085 s->tex_pb= backup_s.tex_pb;
3401 }
3402 549690 s->last_bits= put_bits_count(&s->pb);
3403
3404 549690 if (CONFIG_H263_ENCODER &&
3405
4/4
✓ Branch 0 taken 414950 times.
✓ Branch 1 taken 134740 times.
✓ Branch 2 taken 269095 times.
✓ Branch 3 taken 145855 times.
549690 s->c.out_format == FMT_H263 && s->c.pict_type != AV_PICTURE_TYPE_B)
3406 269095 ff_h263_update_mb(s);
3407
3408
2/2
✓ Branch 0 taken 225812 times.
✓ Branch 1 taken 323878 times.
549690 if(next_block==0){ //FIXME 16 vs linesize16
3409 225812 s->c.hdsp.put_pixels_tab[0][0](s->c.dest[0], s->c.sc.rd_scratchpad , s->c.linesize ,16);
3410 225812 s->c.hdsp.put_pixels_tab[1][0](s->c.dest[1], s->c.sc.rd_scratchpad + 16*s->c.linesize , s->c.uvlinesize, 8);
3411 225812 s->c.hdsp.put_pixels_tab[1][0](s->c.dest[2], s->c.sc.rd_scratchpad + 16*s->c.linesize + 8, s->c.uvlinesize, 8);
3412 }
3413
3414
2/2
✓ Branch 0 taken 141006 times.
✓ Branch 1 taken 408684 times.
549690 if (s->c.avctx->mb_decision == FF_MB_DECISION_BITS)
3415 141006 mpv_reconstruct_mb(s, s->block);
3416 } else {
3417 3465913 int motion_x = 0, motion_y = 0;
3418 3465913 s->c.mv_type = MV_TYPE_16X16;
3419 // only one MB-Type possible
3420
3421
10/13
✓ Branch 0 taken 1242537 times.
✓ Branch 1 taken 2033928 times.
✓ Branch 2 taken 9451 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16201 times.
✓ Branch 5 taken 67 times.
✓ Branch 6 taken 101722 times.
✓ Branch 7 taken 35516 times.
✓ Branch 8 taken 21829 times.
✓ Branch 9 taken 2174 times.
✓ Branch 10 taken 2488 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
3465913 switch(mb_type){
3422 1242537 case CANDIDATE_MB_TYPE_INTRA:
3423 1242537 s->c.mv_dir = 0;
3424 1242537 s->c.mb_intra = 1;
3425 1242537 motion_x= s->c.mv[0][0][0] = 0;
3426 1242537 motion_y= s->c.mv[0][0][1] = 0;
3427 1242537 s->c.mbintra_table[xy] = 1;
3428 1242537 break;
3429 2033928 case CANDIDATE_MB_TYPE_INTER:
3430 2033928 s->c.mv_dir = MV_DIR_FORWARD;
3431 2033928 s->c.mb_intra = 0;
3432 2033928 motion_x= s->c.mv[0][0][0] = s->p_mv_table[xy][0];
3433 2033928 motion_y= s->c.mv[0][0][1] = s->p_mv_table[xy][1];
3434 2033928 break;
3435 9451 case CANDIDATE_MB_TYPE_INTER_I:
3436 9451 s->c.mv_dir = MV_DIR_FORWARD;
3437 9451 s->c.mv_type = MV_TYPE_FIELD;
3438 9451 s->c.mb_intra = 0;
3439
2/2
✓ Branch 0 taken 18902 times.
✓ Branch 1 taken 9451 times.
28353 for(i=0; i<2; i++){
3440 18902 int j = s->c.field_select[0][i] = s->p_field_select_table[i][xy];
3441 18902 s->c.mv[0][i][0] = s->c.p_field_mv_table[i][j][xy][0];
3442 18902 s->c.mv[0][i][1] = s->c.p_field_mv_table[i][j][xy][1];
3443 }
3444 9451 break;
3445 case CANDIDATE_MB_TYPE_INTER4V:
3446 s->c.mv_dir = MV_DIR_FORWARD;
3447 s->c.mv_type = MV_TYPE_8X8;
3448 s->c.mb_intra = 0;
3449 for(i=0; i<4; i++){
3450 s->c.mv[0][i][0] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
3451 s->c.mv[0][i][1] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
3452 }
3453 break;
3454 16201 case CANDIDATE_MB_TYPE_DIRECT:
3455 if (CONFIG_MPEG4_ENCODER) {
3456 16201 s->c.mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3457 16201 s->c.mb_intra = 0;
3458 16201 motion_x=s->b_direct_mv_table[xy][0];
3459 16201 motion_y=s->b_direct_mv_table[xy][1];
3460 16201 ff_mpeg4_set_direct_mv(&s->c, motion_x, motion_y);
3461 }
3462 16201 break;
3463 67 case CANDIDATE_MB_TYPE_DIRECT0:
3464 if (CONFIG_MPEG4_ENCODER) {
3465 67 s->c.mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3466 67 s->c.mb_intra = 0;
3467 67 ff_mpeg4_set_direct_mv(&s->c, 0, 0);
3468 }
3469 67 break;
3470 101722 case CANDIDATE_MB_TYPE_BIDIR:
3471 101722 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3472 101722 s->c.mb_intra = 0;
3473 101722 s->c.mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3474 101722 s->c.mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3475 101722 s->c.mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3476 101722 s->c.mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3477 101722 break;
3478 35516 case CANDIDATE_MB_TYPE_BACKWARD:
3479 35516 s->c.mv_dir = MV_DIR_BACKWARD;
3480 35516 s->c.mb_intra = 0;
3481 35516 motion_x= s->c.mv[1][0][0] = s->b_back_mv_table[xy][0];
3482 35516 motion_y= s->c.mv[1][0][1] = s->b_back_mv_table[xy][1];
3483 35516 break;
3484 21829 case CANDIDATE_MB_TYPE_FORWARD:
3485 21829 s->c.mv_dir = MV_DIR_FORWARD;
3486 21829 s->c.mb_intra = 0;
3487 21829 motion_x= s->c.mv[0][0][0] = s->b_forw_mv_table[xy][0];
3488 21829 motion_y= s->c.mv[0][0][1] = s->b_forw_mv_table[xy][1];
3489 21829 break;
3490 2174 case CANDIDATE_MB_TYPE_FORWARD_I:
3491 2174 s->c.mv_dir = MV_DIR_FORWARD;
3492 2174 s->c.mv_type = MV_TYPE_FIELD;
3493 2174 s->c.mb_intra = 0;
3494
2/2
✓ Branch 0 taken 4348 times.
✓ Branch 1 taken 2174 times.
6522 for(i=0; i<2; i++){
3495 4348 int j = s->c.field_select[0][i] = s->b_field_select_table[0][i][xy];
3496 4348 s->c.mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3497 4348 s->c.mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3498 }
3499 2174 break;
3500 2488 case CANDIDATE_MB_TYPE_BACKWARD_I:
3501 2488 s->c.mv_dir = MV_DIR_BACKWARD;
3502 2488 s->c.mv_type = MV_TYPE_FIELD;
3503 2488 s->c.mb_intra = 0;
3504
2/2
✓ Branch 0 taken 4976 times.
✓ Branch 1 taken 2488 times.
7464 for(i=0; i<2; i++){
3505 4976 int j = s->c.field_select[1][i] = s->b_field_select_table[1][i][xy];
3506 4976 s->c.mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3507 4976 s->c.mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3508 }
3509 2488 break;
3510 case CANDIDATE_MB_TYPE_BIDIR_I:
3511 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3512 s->c.mv_type = MV_TYPE_FIELD;
3513 s->c.mb_intra = 0;
3514 for(dir=0; dir<2; dir++){
3515 for(i=0; i<2; i++){
3516 int j = s->c.field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3517 s->c.mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3518 s->c.mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3519 }
3520 }
3521 break;
3522 default:
3523 av_unreachable("There is a case for every CANDIDATE_MB_TYPE_* "
3524 "except CANDIDATE_MB_TYPE_SKIPPED which is never "
3525 "the only candidate (always coupled with INTER) "
3526 "so that it never reaches this switch");
3527 }
3528
3529 3465913 encode_mb(s, motion_x, motion_y);
3530
3531 // RAL: Update last macroblock type
3532 3465913 s->last_mv_dir = s->c.mv_dir;
3533
3534 3465913 if (CONFIG_H263_ENCODER &&
3535
4/4
✓ Branch 0 taken 1169478 times.
✓ Branch 1 taken 2296435 times.
✓ Branch 2 taken 1131129 times.
✓ Branch 3 taken 38349 times.
3465913 s->c.out_format == FMT_H263 && s->c.pict_type != AV_PICTURE_TYPE_B)
3536 1131129 ff_h263_update_mb(s);
3537
3538 3465913 mpv_reconstruct_mb(s, s->block);
3539 }
3540
3541 4015603 s->c.cur_pic.qscale_table[xy] = s->c.qscale;
3542
3543 /* clean the MV table in IPS frames for direct mode in B-frames */
3544
2/2
✓ Branch 0 taken 1282559 times.
✓ Branch 1 taken 2733044 times.
4015603 if (s->c.mb_intra /* && I,P,S_TYPE */) {
3545 1282559 s->p_mv_table[xy][0]=0;
3546 1282559 s->p_mv_table[xy][1]=0;
3547 #if CONFIG_H263_ENCODER
3548
4/4
✓ Branch 0 taken 1745558 times.
✓ Branch 1 taken 987486 times.
✓ Branch 2 taken 51657 times.
✓ Branch 3 taken 1693901 times.
2733044 } else if (s->c.h263_pred || s->c.h263_aic) {
3549 1039143 ff_h263_clean_intra_table_entries(&s->c, xy);
3550 #endif
3551 }
3552
3553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4015603 times.
4015603 if (s->c.avctx->flags & AV_CODEC_FLAG_PSNR) {
3554 int w= 16;
3555 int h= 16;
3556
3557 if (s->c.mb_x*16 + 16 > s->c.width ) w = s->c.width - s->c.mb_x*16;
3558 if (s->c.mb_y*16 + 16 > s->c.height) h = s->c.height- s->c.mb_y*16;
3559
3560 s->encoding_error[0] += sse(
3561 s, s->new_pic->data[0] + s->c.mb_x*16 + s->c.mb_y*s->c.linesize*16,
3562 s->c.dest[0], w, h, s->c.linesize);
3563 s->encoding_error[1] += sse(
3564 s, s->new_pic->data[1] + s->c.mb_x*8 + s->c.mb_y*s->c.uvlinesize*chr_h,
3565 s->c.dest[1], w>>1, h>>s->c.chroma_y_shift, s->c.uvlinesize);
3566 s->encoding_error[2] += sse(
3567 s, s->new_pic->data[2] + s->c.mb_x*8 + s->c.mb_y*s->c.uvlinesize*chr_h,
3568 s->c.dest[2], w>>1, h>>s->c.chroma_y_shift, s->c.uvlinesize);
3569 }
3570
2/2
✓ Branch 0 taken 118800 times.
✓ Branch 1 taken 3896803 times.
4015603 if (s->loop_filter) {
3571
2/2
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 59400 times.
118800 if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263)
3572 59400 ff_h263_loop_filter(&s->c);
3573 }
3574 ff_dlog(s->c.avctx, "MB %d %d bits\n",
3575 s->c.mb_x + s->c.mb_y * s->c.mb_stride, put_bits_count(&s->pb));
3576 }
3577 }
3578
3579 #if CONFIG_MSMPEG4ENC
3580 //not beautiful here but we must write it before flushing so it has to be here
3581
4/4
✓ Branch 0 taken 825 times.
✓ Branch 1 taken 14193 times.
✓ Branch 2 taken 425 times.
✓ Branch 3 taken 400 times.
15018 if (s->c.msmpeg4_version != MSMP4_UNUSED && s->c.msmpeg4_version < MSMP4_WMV1 &&
3582
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 382 times.
425 s->c.pict_type == AV_PICTURE_TYPE_I)
3583 43 ff_msmpeg4_encode_ext_header(s);
3584 #endif
3585
3586 15018 write_slice_end(s);
3587
3588 15018 return 0;
3589 }
3590
3591 #define ADD(field) dst->field += src->field;
3592 #define MERGE(field) dst->field += src->field; src->field=0
3593 2768 static void merge_context_after_me(MPVEncContext *const dst, MPVEncContext *const src)
3594 {
3595 2768 ADD(me.scene_change_score);
3596 2768 ADD(me.mc_mb_var_sum_temp);
3597 2768 ADD(me.mb_var_sum_temp);
3598 2768 }
3599
3600 2768 static void merge_context_after_encode(MPVEncContext *const dst, MPVEncContext *const src)
3601 {
3602 int i;
3603
3604 2768 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3605 2768 MERGE(dct_count[1]);
3606 2768 ADD(mv_bits);
3607 2768 ADD(i_tex_bits);
3608 2768 ADD(p_tex_bits);
3609 2768 ADD(i_count);
3610 2768 ADD(misc_bits);
3611 2768 ADD(encoding_error[0]);
3612 2768 ADD(encoding_error[1]);
3613 2768 ADD(encoding_error[2]);
3614
3615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2768 times.
2768 if (dst->dct_error_sum) {
3616 for(i=0; i<64; i++){
3617 MERGE(dct_error_sum[0][i]);
3618 MERGE(dct_error_sum[1][i]);
3619 }
3620 }
3621
3622 av_assert1(put_bits_count(&src->pb) % 8 ==0);
3623 av_assert1(put_bits_count(&dst->pb) % 8 ==0);
3624 2768 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3625 2768 flush_put_bits(&dst->pb);
3626 2768 }
3627
3628 12250 static int estimate_qp(MPVMainEncContext *const m, int dry_run)
3629 {
3630 12250 MPVEncContext *const s = &m->s;
3631
3632
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 12125 times.
12250 if (m->next_lambda){
3633 125 s->c.cur_pic.ptr->f->quality = m->next_lambda;
3634
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if(!dry_run) m->next_lambda= 0;
3635
2/2
✓ Branch 0 taken 5265 times.
✓ Branch 1 taken 6860 times.
12125 } else if (!m->fixed_qscale) {
3636 5265 int quality = ff_rate_estimate_qscale(m, dry_run);
3637 5265 s->c.cur_pic.ptr->f->quality = quality;
3638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5265 times.
5265 if (s->c.cur_pic.ptr->f->quality < 0)
3639 return -1;
3640 }
3641
3642
2/2
✓ Branch 0 taken 850 times.
✓ Branch 1 taken 11400 times.
12250 if(s->adaptive_quant){
3643 850 init_qscale_tab(s);
3644
3645
2/3
✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
850 switch (s->c.codec_id) {
3646 450 case AV_CODEC_ID_MPEG4:
3647 if (CONFIG_MPEG4_ENCODER)
3648 450 ff_clean_mpeg4_qscales(s);
3649 450 break;
3650 case AV_CODEC_ID_H263:
3651 case AV_CODEC_ID_H263P:
3652 case AV_CODEC_ID_FLV1:
3653 if (CONFIG_H263_ENCODER)
3654 ff_clean_h263_qscales(s);
3655 break;
3656 }
3657
3658 850 s->lambda = s->lambda_table[0];
3659 //FIXME broken
3660 }else
3661 11400 s->lambda = s->c.cur_pic.ptr->f->quality;
3662 12250 update_qscale(m);
3663 12250 return 0;
3664 }
3665
3666 /* must be called before writing the header */
3667 2739 static void set_frame_distances(MPVEncContext *const s)
3668 {
3669 av_assert1(s->c.cur_pic.ptr->f->pts != AV_NOPTS_VALUE);
3670 2739 s->c.time = s->c.cur_pic.ptr->f->pts * s->c.avctx->time_base.num;
3671
3672
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 2137 times.
2739 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
3673 602 s->c.pb_time = s->c.pp_time - (s->c.last_non_b_time - s->c.time);
3674 av_assert1(s->c.pb_time > 0 && s->c.pb_time < s->c.pp_time);
3675 }else{
3676 av_assert1(s->picture_number == 0 || s->c.time > s->c.last_non_b_time);
3677 2137 s->c.pp_time = s->c.time - s->c.last_non_b_time;
3678 2137 s->c.last_non_b_time = s->c.time;
3679 }
3680 2739 }
3681
3682 12250 static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt)
3683 {
3684 12250 MPVEncContext *const s = &m->s;
3685 int ret;
3686 int bits;
3687 12250 int context_count = s->c.slice_context_count;
3688
3689
2/2
✓ Branch 0 taken 2739 times.
✓ Branch 1 taken 9511 times.
12250 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4) {
3690 2739 set_frame_distances(s);
3691 2739 ff_set_mpeg4_time(s);
3692 }
3693
3694 // s->lambda = s->c.cur_pic.ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3695
3696
2/2
✓ Branch 0 taken 2869 times.
✓ Branch 1 taken 9381 times.
12250 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
3697 2869 s->c.no_rounding = s->c.msmpeg4_version >= MSMP4_V3;
3698
2/2
✓ Branch 0 taken 8033 times.
✓ Branch 1 taken 1348 times.
9381 } else if (s->c.pict_type != AV_PICTURE_TYPE_B) {
3699 8033 s->c.no_rounding ^= s->flipflop_rounding;
3700 }
3701
3702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12250 times.
12250 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
3703 ret = estimate_qp(m, 1);
3704 if (ret < 0)
3705 return ret;
3706 ff_get_2pass_fcode(m);
3707
2/2
✓ Branch 0 taken 5265 times.
✓ Branch 1 taken 6985 times.
12250 } else if (!(s->c.avctx->flags & AV_CODEC_FLAG_QSCALE)) {
3708
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 4497 times.
5265 if (s->c.pict_type == AV_PICTURE_TYPE_B)
3709 768 s->lambda = m->last_lambda_for[s->c.pict_type];
3710 else
3711 4497 s->lambda = m->last_lambda_for[m->last_non_b_pict_type];
3712 5265 update_qscale(m);
3713 }
3714
3715 12250 s->c.mb_intra = 0; //for the rate distortion & bit compare functions
3716
2/2
✓ Branch 0 taken 15018 times.
✓ Branch 1 taken 12250 times.
27268 for (int i = 0; i < context_count; i++) {
3717 15018 MPVEncContext *const slice = s->c.enc_contexts[i];
3718 15018 int h = s->c.mb_height;
3719 15018 uint8_t *start = pkt->data + (int64_t)pkt->size * slice->c.start_mb_y / h;
3720 15018 uint8_t *end = pkt->data + (int64_t)pkt->size * slice->c. end_mb_y / h;
3721
3722 15018 init_put_bits(&slice->pb, start, end - start);
3723
3724
2/2
✓ Branch 0 taken 2768 times.
✓ Branch 1 taken 12250 times.
15018 if (i) {
3725 2768 ret = ff_update_duplicate_context(&slice->c, &s->c);
3726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2768 times.
2768 if (ret < 0)
3727 return ret;
3728 2768 slice->lambda = s->lambda;
3729 2768 slice->lambda2 = s->lambda2;
3730 }
3731 15018 slice->me.temp = slice->me.scratchpad = slice->c.sc.scratchpad_buf;
3732 15018 ff_me_init_pic(slice);
3733 }
3734
3735 /* Estimate motion for every MB */
3736
2/2
✓ Branch 0 taken 9381 times.
✓ Branch 1 taken 2869 times.
12250 if (s->c.pict_type != AV_PICTURE_TYPE_I) {
3737 9381 s->lambda = (s->lambda * m->me_penalty_compensation + 128) >> 8;
3738 9381 s->lambda2 = (s->lambda2 * (int64_t) m->me_penalty_compensation + 128) >> 8;
3739
2/2
✓ Branch 0 taken 8033 times.
✓ Branch 1 taken 1348 times.
9381 if (s->c.pict_type != AV_PICTURE_TYPE_B) {
3740
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8033 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8033 if ((m->me_pre && m->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
3741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8033 times.
8033 m->me_pre == 2) {
3742 s->c.avctx->execute(s->c.avctx, pre_estimate_motion_thread,
3743 &s->c.enc_contexts[0], NULL,
3744 context_count, sizeof(void*));
3745 }
3746 }
3747
3748 9381 s->c.avctx->execute(s->c.avctx, estimate_motion_thread, &s->c.enc_contexts[0],
3749 NULL, context_count, sizeof(void*));
3750 }else /* if (s->c.pict_type == AV_PICTURE_TYPE_I) */{
3751 /* I-Frame */
3752
2/2
✓ Branch 0 taken 1209627 times.
✓ Branch 1 taken 2869 times.
1212496 for (int i = 0; i < s->c.mb_stride * s->c.mb_height; i++)
3753 1209627 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3754
3755
2/2
✓ Branch 0 taken 919 times.
✓ Branch 1 taken 1950 times.
2869 if (!m->fixed_qscale) {
3756 /* finding spatial complexity for I-frame rate control */
3757 919 s->c.avctx->execute(s->c.avctx, mb_var_thread, &s->c.enc_contexts[0],
3758 NULL, context_count, sizeof(void*));
3759 }
3760 }
3761
2/2
✓ Branch 0 taken 2768 times.
✓ Branch 1 taken 12250 times.
15018 for (int i = 1; i < context_count; i++)
3762 2768 merge_context_after_me(s, s->c.enc_contexts[i]);
3763 12250 m->mc_mb_var_sum = s->me.mc_mb_var_sum_temp;
3764 12250 m->mb_var_sum = s->me. mb_var_sum_temp;
3765 12250 emms_c();
3766
3767
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 12219 times.
12250 if (s->me.scene_change_score > m->scenechange_threshold &&
3768
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 s->c.pict_type == AV_PICTURE_TYPE_P) {
3769 31 s->c.pict_type = AV_PICTURE_TYPE_I;
3770
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 31 times.
451 for (int i = 0; i < s->c.mb_stride * s->c.mb_height; i++)
3771 420 s->mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
3772
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
31 if (s->c.msmpeg4_version >= MSMP4_V3)
3773 2 s->c.no_rounding = 1;
3774 ff_dlog(s->c.avctx, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
3775 m->mb_var_sum, m->mc_mb_var_sum);
3776 }
3777
3778
2/2
✓ Branch 0 taken 12100 times.
✓ Branch 1 taken 150 times.
12250 if (!s->umvplus) {
3779
3/4
✓ Branch 0 taken 4233 times.
✓ Branch 1 taken 7867 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4233 times.
12100 if (s->c.pict_type == AV_PICTURE_TYPE_P || s->c.pict_type == AV_PICTURE_TYPE_S) {
3780 7867 s->f_code = ff_get_best_fcode(m, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3781
3782
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 7535 times.
7867 if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3783 int a,b;
3784 332 a = ff_get_best_fcode(m, s->c.p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3785 332 b = ff_get_best_fcode(m, s->c.p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3786 332 s->f_code = FFMAX3(s->f_code, a, b);
3787 }
3788
3789
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7867 times.
7867 ff_fix_long_p_mvs(s, s->intra_penalty ? CANDIDATE_MB_TYPE_INTER : CANDIDATE_MB_TYPE_INTRA);
3790 7867 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, !!s->intra_penalty);
3791
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 7535 times.
7867 if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3792 int j;
3793
2/2
✓ Branch 0 taken 664 times.
✓ Branch 1 taken 332 times.
996 for (int i = 0; i < 2; i++) {
3794
2/2
✓ Branch 0 taken 1328 times.
✓ Branch 1 taken 664 times.
1992 for(j=0; j<2; j++)
3795 1328 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3796 1328 s->c.p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, !!s->intra_penalty);
3797 }
3798 }
3799
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 2885 times.
4233 } else if (s->c.pict_type == AV_PICTURE_TYPE_B) {
3800 int a, b;
3801
3802 1348 a = ff_get_best_fcode(m, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3803 1348 b = ff_get_best_fcode(m, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3804 1348 s->f_code = FFMAX(a, b);
3805
3806 1348 a = ff_get_best_fcode(m, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3807 1348 b = ff_get_best_fcode(m, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3808 1348 s->b_code = FFMAX(a, b);
3809
3810 1348 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3811 1348 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3812 1348 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3813 1348 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3814
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 964 times.
1348 if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3815 int dir, j;
3816
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 384 times.
1152 for(dir=0; dir<2; dir++){
3817
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 768 times.
2304 for (int i = 0; i < 2; i++) {
3818
2/2
✓ Branch 0 taken 3072 times.
✓ Branch 1 taken 1536 times.
4608 for(j=0; j<2; j++){
3819 3072 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3820
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 1536 times.
3072 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3821
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 1536 times.
3072 ff_fix_long_mvs(s, s->b_field_select_table[dir][i], j,
3822 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3823 }
3824 }
3825 }
3826 }
3827 }
3828 }
3829
3830 12250 ret = estimate_qp(m, 0);
3831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12250 times.
12250 if (ret < 0)
3832 return ret;
3833
3834
4/4
✓ Branch 0 taken 3348 times.
✓ Branch 1 taken 8902 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3344 times.
12250 if (s->c.qscale < 3 && s->max_qcoeff <= 128 &&
3835
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 s->c.pict_type == AV_PICTURE_TYPE_I &&
3836
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !(s->c.avctx->flags & AV_CODEC_FLAG_QSCALE))
3837 2 s->c.qscale = 3; //reduce clipping problems
3838
3839
2/2
✓ Branch 0 taken 1439 times.
✓ Branch 1 taken 10811 times.
12250 if (s->c.out_format == FMT_MJPEG) {
3840 1439 ret = ff_check_codec_matrices(s->c.avctx, FF_MATRIX_TYPE_INTRA | FF_MATRIX_TYPE_CHROMA_INTRA,
3841 1439 (7 + s->c.qscale) / s->c.qscale, 65535);
3842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1439 times.
1439 if (ret < 0)
3843 return ret;
3844
3845
2/2
✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 200 times.
1439 if (s->c.codec_id != AV_CODEC_ID_AMV) {
3846 1239 const uint16_t * luma_matrix = ff_mpeg1_default_intra_matrix;
3847 1239 const uint16_t *chroma_matrix = ff_mpeg1_default_intra_matrix;
3848
3849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 if (s->c.avctx->intra_matrix) {
3850 chroma_matrix =
3851 luma_matrix = s->c.avctx->intra_matrix;
3852 }
3853
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 if (s->c.avctx->chroma_intra_matrix)
3854 chroma_matrix = s->c.avctx->chroma_intra_matrix;
3855
3856 /* for mjpeg, we do include qscale in the matrix */
3857
2/2
✓ Branch 0 taken 78057 times.
✓ Branch 1 taken 1239 times.
79296 for (int i = 1; i < 64; i++) {
3858 78057 int j = s->c.idsp.idct_permutation[i];
3859
3860 78057 s->c.chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[i] * s->c.qscale) >> 3);
3861 78057 s->c. intra_matrix[j] = av_clip_uint8(( luma_matrix[i] * s->c.qscale) >> 3);
3862 }
3863 1239 s->c.y_dc_scale_table =
3864 1239 s->c.c_dc_scale_table = ff_mpeg12_dc_scale_table[0];
3865 1239 s->c.chroma_intra_matrix[0] = s->c.intra_matrix[0] = 8;
3866 } else {
3867 static const uint8_t y[32] = {13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13};
3868 static const uint8_t c[32] = {14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14};
3869
2/2
✓ Branch 0 taken 12600 times.
✓ Branch 1 taken 200 times.
12800 for (int i = 1; i < 64; i++) {
3870 12600 int j = s->c.idsp.idct_permutation[ff_zigzag_direct[i]];
3871
3872 12600 s->c.intra_matrix[j] = sp5x_qscale_five_quant_table[0][i];
3873 12600 s->c.chroma_intra_matrix[j] = sp5x_qscale_five_quant_table[1][i];
3874 }
3875 200 s->c.y_dc_scale_table = y;
3876 200 s->c.c_dc_scale_table = c;
3877 200 s->c.intra_matrix[0] = 13;
3878 200 s->c.chroma_intra_matrix[0] = 14;
3879 }
3880 1439 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3881 1439 s->c.intra_matrix, s->intra_quant_bias, 8, 8, 1);
3882 1439 ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3883 1439 s->c.chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3884 1439 s->c.qscale = 8;
3885 }
3886
3887
2/2
✓ Branch 0 taken 2900 times.
✓ Branch 1 taken 9350 times.
12250 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
3888 2900 s->c.cur_pic.ptr->f->flags |= AV_FRAME_FLAG_KEY;
3889 } else {
3890 9350 s->c.cur_pic.ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
3891 }
3892 12250 s->c.cur_pic.ptr->f->pict_type = s->c.pict_type;
3893
3894
2/2
✓ Branch 0 taken 2900 times.
✓ Branch 1 taken 9350 times.
12250 if (s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)
3895 2900 m->picture_in_gop_number = 0;
3896
3897 12250 s->c.mb_x = s->c.mb_y = 0;
3898 12250 s->last_bits= put_bits_count(&s->pb);
3899 12250 ret = m->encode_picture_header(m);
3900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12250 times.
12250 if (ret < 0)
3901 return ret;
3902 12250 bits= put_bits_count(&s->pb);
3903 12250 m->header_bits = bits - s->last_bits;
3904
3905
2/2
✓ Branch 0 taken 2768 times.
✓ Branch 1 taken 12250 times.
15018 for (int i = 1; i < context_count; i++)
3906 2768 update_duplicate_context_after_me(s->c.enc_contexts[i], s);
3907 12250 s->c.avctx->execute(s->c.avctx, encode_thread, &s->c.enc_contexts[0],
3908 NULL, context_count, sizeof(void*));
3909
2/2
✓ Branch 0 taken 2768 times.
✓ Branch 1 taken 12250 times.
15018 for (int i = 1; i < context_count; i++) {
3910
2/2
✓ Branch 0 taken 2696 times.
✓ Branch 1 taken 72 times.
2768 if (s->pb.buf_end == s->c.enc_contexts[i]->pb.buf)
3911 2696 set_put_bits_buffer_size(&s->pb, FFMIN(s->c.enc_contexts[i]->pb.buf_end - s->pb.buf, INT_MAX/8-BUF_BITS));
3912 2768 merge_context_after_encode(s, s->c.enc_contexts[i]);
3913 }
3914 12250 emms_c();
3915 12250 return 0;
3916 }
3917
3918 101281371 static inline void denoise_dct(MPVEncContext *const s, int16_t block[])
3919 {
3920
2/2
✓ Branch 0 taken 100241274 times.
✓ Branch 1 taken 1040097 times.
101281371 if (!s->dct_error_sum)
3921 100241274 return;
3922
3923 1040097 const int intra = s->c.mb_intra;
3924 1040097 s->dct_count[intra]++;
3925 1040097 s->mpvencdsp.denoise_dct(block, s->dct_error_sum[intra], s->dct_offset[intra]);
3926 }
3927
3928 8482411 static int dct_quantize_trellis_c(MPVEncContext *const s,
3929 int16_t *block, int n,
3930 int qscale, int *overflow){
3931 const int *qmat;
3932 const uint16_t *matrix;
3933 const uint8_t *scantable;
3934 const uint8_t *perm_scantable;
3935 8482411 int max=0;
3936 unsigned int threshold1, threshold2;
3937 8482411 int bias=0;
3938 int run_tab[65];
3939 int level_tab[65];
3940 int score_tab[65];
3941 int survivor[65];
3942 int survivor_count;
3943 8482411 int last_run=0;
3944 8482411 int last_level=0;
3945 8482411 int last_score= 0;
3946 int last_i;
3947 int coeff[2][64];
3948 int coeff_count[64];
3949 int qmul, qadd, start_i, last_non_zero, i, dc;
3950 8482411 const int esc_length= s->ac_esc_length;
3951 const uint8_t *length, *last_length;
3952 8482411 const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3953 int mpeg2_qscale;
3954
3955 8482411 s->fdsp.fdct(block);
3956
3957 8482411 denoise_dct(s, block);
3958
3959 8482411 qmul= qscale*16;
3960 8482411 qadd= ((qscale-1)|1)*8;
3961
3962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8482411 times.
8482411 if (s->c.q_scale_type) mpeg2_qscale = ff_mpeg2_non_linear_qscale[qscale];
3963 8482411 else mpeg2_qscale = qscale << 1;
3964
3965
2/2
✓ Branch 0 taken 1912070 times.
✓ Branch 1 taken 6570341 times.
8482411 if (s->c.mb_intra) {
3966 int q;
3967 1912070 scantable = s->c.intra_scantable.scantable;
3968 1912070 perm_scantable = s->c.intra_scantable.permutated;
3969
1/2
✓ Branch 0 taken 1912070 times.
✗ Branch 1 not taken.
1912070 if (!s->c.h263_aic) {
3970
2/2
✓ Branch 0 taken 1223032 times.
✓ Branch 1 taken 689038 times.
1912070 if (n < 4)
3971 1223032 q = s->c.y_dc_scale;
3972 else
3973 689038 q = s->c.c_dc_scale;
3974 1912070 q = q << 3;
3975 } else{
3976 /* For AIC we skip quant/dequant of INTRADC */
3977 q = 1 << 3;
3978 qadd=0;
3979 }
3980
3981 /* note: block[0] is assumed to be positive */
3982 1912070 block[0] = (block[0] + (q >> 1)) / q;
3983 1912070 start_i = 1;
3984 1912070 last_non_zero = 0;
3985
2/2
✓ Branch 0 taken 1223032 times.
✓ Branch 1 taken 689038 times.
1912070 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3986
2/2
✓ Branch 0 taken 1223032 times.
✓ Branch 1 taken 689038 times.
1912070 matrix = n < 4 ? s->c.intra_matrix : s->c.chroma_intra_matrix;
3987
5/6
✓ Branch 0 taken 1364850 times.
✓ Branch 1 taken 547220 times.
✓ Branch 2 taken 1364850 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 718200 times.
✓ Branch 5 taken 646650 times.
1912070 if (s->mpeg_quant || s->c.out_format == FMT_MPEG1 || s->c.out_format == FMT_MJPEG)
3988 1265420 bias= 1<<(QMAT_SHIFT-1);
3989
3990
4/4
✓ Branch 0 taken 689038 times.
✓ Branch 1 taken 1223032 times.
✓ Branch 2 taken 239400 times.
✓ Branch 3 taken 449638 times.
1912070 if (n > 3 && s->intra_chroma_ac_vlc_length) {
3991 239400 length = s->intra_chroma_ac_vlc_length;
3992 239400 last_length= s->intra_chroma_ac_vlc_last_length;
3993 } else {
3994 1672670 length = s->intra_ac_vlc_length;
3995 1672670 last_length= s->intra_ac_vlc_last_length;
3996 }
3997 } else {
3998 6570341 scantable = s->c.inter_scantable.scantable;
3999 6570341 perm_scantable = s->c.inter_scantable.permutated;
4000 6570341 start_i = 0;
4001 6570341 last_non_zero = -1;
4002 6570341 qmat = s->q_inter_matrix[qscale];
4003 6570341 matrix = s->c.inter_matrix;
4004 6570341 length = s->inter_ac_vlc_length;
4005 6570341 last_length= s->inter_ac_vlc_last_length;
4006 }
4007 8482411 last_i= start_i;
4008
4009 8482411 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4010 8482411 threshold2= (threshold1<<1);
4011
4012
2/2
✓ Branch 0 taken 365101499 times.
✓ Branch 1 taken 2104403 times.
367205902 for(i=63; i>=start_i; i--) {
4013 365101499 const int j = scantable[i];
4014 365101499 int64_t level = (int64_t)block[j] * qmat[j];
4015
4016
2/2
✓ Branch 0 taken 6378008 times.
✓ Branch 1 taken 358723491 times.
365101499 if(((uint64_t)(level+threshold1))>threshold2){
4017 6378008 last_non_zero = i;
4018 6378008 break;
4019 }
4020 }
4021
4022
2/2
✓ Branch 0 taken 182238743 times.
✓ Branch 1 taken 8482411 times.
190721154 for(i=start_i; i<=last_non_zero; i++) {
4023 182238743 const int j = scantable[i];
4024 182238743 int64_t level = (int64_t)block[j] * qmat[j];
4025
4026 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
4027 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
4028
2/2
✓ Branch 0 taken 62162910 times.
✓ Branch 1 taken 120075833 times.
182238743 if(((uint64_t)(level+threshold1))>threshold2){
4029
2/2
✓ Branch 0 taken 30992212 times.
✓ Branch 1 taken 31170698 times.
62162910 if(level>0){
4030 30992212 level= (bias + level)>>QMAT_SHIFT;
4031 30992212 coeff[0][i]= level;
4032 30992212 coeff[1][i]= level-1;
4033 // coeff[2][k]= level-2;
4034 }else{
4035 31170698 level= (bias - level)>>QMAT_SHIFT;
4036 31170698 coeff[0][i]= -level;
4037 31170698 coeff[1][i]= -level+1;
4038 // coeff[2][k]= -level+2;
4039 }
4040 62162910 coeff_count[i]= FFMIN(level, 2);
4041 av_assert2(coeff_count[i]);
4042 62162910 max |=level;
4043 }else{
4044 120075833 coeff[0][i]= (level>>31)|1;
4045 120075833 coeff_count[i]= 1;
4046 }
4047 }
4048
4049 8482411 *overflow= s->max_qcoeff < max; //overflow might have happened
4050
4051
2/2
✓ Branch 0 taken 2104403 times.
✓ Branch 1 taken 6378008 times.
8482411 if(last_non_zero < start_i){
4052 2104403 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4053 2104403 return last_non_zero;
4054 }
4055
4056 6378008 score_tab[start_i]= 0;
4057 6378008 survivor[0]= start_i;
4058 6378008 survivor_count= 1;
4059
4060
2/2
✓ Branch 0 taken 182238743 times.
✓ Branch 1 taken 6378008 times.
188616751 for(i=start_i; i<=last_non_zero; i++){
4061 int level_index, j, zero_distortion;
4062 182238743 int dct_coeff= FFABS(block[ scantable[i] ]);
4063 182238743 int best_score=256*256*256*120;
4064
4065
1/2
✓ Branch 0 taken 182238743 times.
✗ Branch 1 not taken.
182238743 if (s->fdsp.fdct == ff_fdct_ifast)
4066 182238743 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
4067 182238743 zero_distortion= dct_coeff*dct_coeff;
4068
4069
2/2
✓ Branch 0 taken 208392422 times.
✓ Branch 1 taken 182238743 times.
390631165 for(level_index=0; level_index < coeff_count[i]; level_index++){
4070 int distortion;
4071 208392422 int level= coeff[level_index][i];
4072 208392422 const int alevel= FFABS(level);
4073 int unquant_coeff;
4074
4075 av_assert2(level);
4076
4077
4/4
✓ Branch 0 taken 113005928 times.
✓ Branch 1 taken 95386494 times.
✓ Branch 2 taken 7518283 times.
✓ Branch 3 taken 105487645 times.
208392422 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4078 102904777 unquant_coeff= alevel*qmul + qadd;
4079
2/2
✓ Branch 0 taken 21010944 times.
✓ Branch 1 taken 84476701 times.
105487645 } else if (s->c.out_format == FMT_MJPEG) {
4080 21010944 j = s->c.idsp.idct_permutation[scantable[i]];
4081 21010944 unquant_coeff = alevel * matrix[j] * 8;
4082 }else{ // MPEG-1
4083 84476701 j = s->c.idsp.idct_permutation[scantable[i]]; // FIXME: optimize
4084
2/2
✓ Branch 0 taken 16116353 times.
✓ Branch 1 taken 68360348 times.
84476701 if (s->c.mb_intra) {
4085 16116353 unquant_coeff = (int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4086 16116353 unquant_coeff = (unquant_coeff - 1) | 1;
4087 }else{
4088 68360348 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4089 68360348 unquant_coeff = (unquant_coeff - 1) | 1;
4090 }
4091 84476701 unquant_coeff<<= 3;
4092 }
4093
4094 208392422 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4095 208392422 level+=64;
4096
2/2
✓ Branch 0 taken 208290587 times.
✓ Branch 1 taken 101835 times.
208392422 if((level&(~127)) == 0){
4097
2/2
✓ Branch 0 taken 710968881 times.
✓ Branch 1 taken 208290587 times.
919259468 for(j=survivor_count-1; j>=0; j--){
4098 710968881 int run= i - survivor[j];
4099 710968881 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4100 710968881 score += score_tab[i-run];
4101
4102
2/2
✓ Branch 0 taken 434079668 times.
✓ Branch 1 taken 276889213 times.
710968881 if(score < best_score){
4103 434079668 best_score= score;
4104 434079668 run_tab[i+1]= run;
4105 434079668 level_tab[i+1]= level-64;
4106 }
4107 }
4108
4109
4/4
✓ Branch 0 taken 112936824 times.
✓ Branch 1 taken 95353763 times.
✓ Branch 2 taken 7518283 times.
✓ Branch 3 taken 105418541 times.
208290587 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4110
2/2
✓ Branch 0 taken 360569028 times.
✓ Branch 1 taken 102872046 times.
463441074 for(j=survivor_count-1; j>=0; j--){
4111 360569028 int run= i - survivor[j];
4112 360569028 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4113 360569028 score += score_tab[i-run];
4114
2/2
✓ Branch 0 taken 36583166 times.
✓ Branch 1 taken 323985862 times.
360569028 if(score < last_score){
4115 36583166 last_score= score;
4116 36583166 last_run= run;
4117 36583166 last_level= level-64;
4118 36583166 last_i= i+1;
4119 }
4120 }
4121 }
4122 }else{
4123 101835 distortion += esc_length*lambda;
4124
2/2
✓ Branch 0 taken 105134 times.
✓ Branch 1 taken 101835 times.
206969 for(j=survivor_count-1; j>=0; j--){
4125 105134 int run= i - survivor[j];
4126 105134 int score= distortion + score_tab[i-run];
4127
4128
2/2
✓ Branch 0 taken 53600 times.
✓ Branch 1 taken 51534 times.
105134 if(score < best_score){
4129 53600 best_score= score;
4130 53600 run_tab[i+1]= run;
4131 53600 level_tab[i+1]= level-64;
4132 }
4133 }
4134
4135
3/4
✓ Branch 0 taken 69104 times.
✓ Branch 1 taken 32731 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 69104 times.
101835 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4136
2/2
✓ Branch 0 taken 33934 times.
✓ Branch 1 taken 32731 times.
66665 for(j=survivor_count-1; j>=0; j--){
4137 33934 int run= i - survivor[j];
4138 33934 int score= distortion + score_tab[i-run];
4139
2/2
✓ Branch 0 taken 17304 times.
✓ Branch 1 taken 16630 times.
33934 if(score < last_score){
4140 17304 last_score= score;
4141 17304 last_run= run;
4142 17304 last_level= level-64;
4143 17304 last_i= i+1;
4144 }
4145 }
4146 }
4147 }
4148 }
4149
4150 182238743 score_tab[i+1]= best_score;
4151
4152 // Note: there is a vlc code in MPEG-4 which is 1 bit shorter then another one with a shorter run and the same level
4153
2/2
✓ Branch 0 taken 36863393 times.
✓ Branch 1 taken 145375350 times.
182238743 if(last_non_zero <= 27){
4154
2/2
✓ Branch 0 taken 60878011 times.
✓ Branch 1 taken 10317054 times.
71195065 for(; survivor_count; survivor_count--){
4155
2/2
✓ Branch 0 taken 26546339 times.
✓ Branch 1 taken 34331672 times.
60878011 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4156 26546339 break;
4157 }
4158 }else{
4159
2/2
✓ Branch 0 taken 242670015 times.
✓ Branch 1 taken 40856018 times.
283526033 for(; survivor_count; survivor_count--){
4160
2/2
✓ Branch 0 taken 104519332 times.
✓ Branch 1 taken 138150683 times.
242670015 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4161 104519332 break;
4162 }
4163 }
4164
4165 182238743 survivor[ survivor_count++ ]= i+1;
4166 }
4167
4168
4/4
✓ Branch 0 taken 3483808 times.
✓ Branch 1 taken 2894200 times.
✓ Branch 2 taken 3226215 times.
✓ Branch 3 taken 257593 times.
6378008 if (s->c.out_format != FMT_H263 && s->c.out_format != FMT_H261) {
4169 3226215 last_score= 256*256*256*120;
4170
2/2
✓ Branch 0 taken 30608954 times.
✓ Branch 1 taken 3226215 times.
33835169 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4171 30608954 int score= score_tab[i];
4172
2/2
✓ Branch 0 taken 30258707 times.
✓ Branch 1 taken 350247 times.
30608954 if (i)
4173 30258707 score += lambda * 2; // FIXME more exact?
4174
4175
2/2
✓ Branch 0 taken 3360817 times.
✓ Branch 1 taken 27248137 times.
30608954 if(score < last_score){
4176 3360817 last_score= score;
4177 3360817 last_i= i;
4178 3360817 last_level= level_tab[i];
4179 3360817 last_run= run_tab[i];
4180 }
4181 }
4182 }
4183
4184 6378008 s->coded_score[n] = last_score;
4185
4186 6378008 dc= FFABS(block[0]);
4187 6378008 last_non_zero= last_i - 1;
4188 6378008 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4189
4190
2/2
✓ Branch 0 taken 1079604 times.
✓ Branch 1 taken 5298404 times.
6378008 if(last_non_zero < start_i)
4191 1079604 return last_non_zero;
4192
4193
3/4
✓ Branch 0 taken 508342 times.
✓ Branch 1 taken 4790062 times.
✓ Branch 2 taken 508342 times.
✗ Branch 3 not taken.
5298404 if(last_non_zero == 0 && start_i == 0){
4194 508342 int best_level= 0;
4195 508342 int best_score= dc * dc;
4196
4197
2/2
✓ Branch 0 taken 708868 times.
✓ Branch 1 taken 508342 times.
1217210 for(i=0; i<coeff_count[0]; i++){
4198 708868 int level= coeff[i][0];
4199 708868 int alevel= FFABS(level);
4200 int unquant_coeff, score, distortion;
4201
4202
4/4
✓ Branch 0 taken 383635 times.
✓ Branch 1 taken 325233 times.
✓ Branch 2 taken 20722 times.
✓ Branch 3 taken 362913 times.
708868 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4203 345955 unquant_coeff= (alevel*qmul + qadd)>>3;
4204 } else{ // MPEG-1
4205 362913 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4206 362913 unquant_coeff = (unquant_coeff - 1) | 1;
4207 }
4208 708868 unquant_coeff = (unquant_coeff + 4) >> 3;
4209 708868 unquant_coeff<<= 3 + 3;
4210
4211 708868 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
4212 708868 level+=64;
4213
2/2
✓ Branch 0 taken 706007 times.
✓ Branch 1 taken 2861 times.
708868 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
4214 2861 else score= distortion + esc_length*lambda;
4215
4216
2/2
✓ Branch 0 taken 553827 times.
✓ Branch 1 taken 155041 times.
708868 if(score < best_score){
4217 553827 best_score= score;
4218 553827 best_level= level - 64;
4219 }
4220 }
4221 508342 block[0]= best_level;
4222 508342 s->coded_score[n] = best_score - dc*dc;
4223
2/2
✓ Branch 0 taken 6085 times.
✓ Branch 1 taken 502257 times.
508342 if(best_level == 0) return -1;
4224 502257 else return last_non_zero;
4225 }
4226
4227 4790062 i= last_i;
4228 av_assert2(last_level);
4229
4230 4790062 block[ perm_scantable[last_non_zero] ]= last_level;
4231 4790062 i -= last_run + 1;
4232
4233
2/2
✓ Branch 0 taken 53296191 times.
✓ Branch 1 taken 4790062 times.
58086253 for(; i>start_i; i -= run_tab[i] + 1){
4234 53296191 block[ perm_scantable[i-1] ]= level_tab[i];
4235 }
4236
4237 4790062 return last_non_zero;
4238 }
4239
4240 static DECLARE_ALIGNED(16, int16_t, basis)[64][64];
4241
4242 static void build_basis(uint8_t *perm){
4243 int i, j, x, y;
4244 emms_c();
4245 for(i=0; i<8; i++){
4246 for(j=0; j<8; j++){
4247 for(y=0; y<8; y++){
4248 for(x=0; x<8; x++){
4249 double s= 0.25*(1<<BASIS_SHIFT);
4250 int index= 8*i + j;
4251 int perm_index= perm[index];
4252 if(i==0) s*= sqrt(0.5);
4253 if(j==0) s*= sqrt(0.5);
4254 basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
4255 }
4256 }
4257 }
4258 }
4259 }
4260
4261 static int dct_quantize_refine(MPVEncContext *const s, //FIXME breaks denoise?
4262 int16_t *block, int16_t *weight, int16_t *orig,
4263 int n, int qscale){
4264 DECLARE_ALIGNED(16, int16_t, rem)[64];
4265 LOCAL_ALIGNED_16(int16_t, d1, [64]);
4266 const uint8_t *scantable;
4267 const uint8_t *perm_scantable;
4268 // unsigned int threshold1, threshold2;
4269 // int bias=0;
4270 int run_tab[65];
4271 int prev_run=0;
4272 int prev_level=0;
4273 int qmul, qadd, start_i, last_non_zero, i, dc;
4274 const uint8_t *length;
4275 const uint8_t *last_length;
4276 int lambda;
4277 int rle_index, run, q = 1, sum; //q is only used when s->c.mb_intra is true
4278
4279 if(basis[0][0] == 0)
4280 build_basis(s->c.idsp.idct_permutation);
4281
4282 qmul= qscale*2;
4283 qadd= (qscale-1)|1;
4284 if (s->c.mb_intra) {
4285 scantable = s->c.intra_scantable.scantable;
4286 perm_scantable = s->c.intra_scantable.permutated;
4287 if (!s->c.h263_aic) {
4288 if (n < 4)
4289 q = s->c.y_dc_scale;
4290 else
4291 q = s->c.c_dc_scale;
4292 } else{
4293 /* For AIC we skip quant/dequant of INTRADC */
4294 q = 1;
4295 qadd=0;
4296 }
4297 q <<= RECON_SHIFT-3;
4298 /* note: block[0] is assumed to be positive */
4299 dc= block[0]*q;
4300 // block[0] = (block[0] + (q >> 1)) / q;
4301 start_i = 1;
4302 // if (s->mpeg_quant || s->c.out_format == FMT_MPEG1)
4303 // bias= 1<<(QMAT_SHIFT-1);
4304 if (n > 3 && s->intra_chroma_ac_vlc_length) {
4305 length = s->intra_chroma_ac_vlc_length;
4306 last_length= s->intra_chroma_ac_vlc_last_length;
4307 } else {
4308 length = s->intra_ac_vlc_length;
4309 last_length= s->intra_ac_vlc_last_length;
4310 }
4311 } else {
4312 scantable = s->c.inter_scantable.scantable;
4313 perm_scantable = s->c.inter_scantable.permutated;
4314 dc= 0;
4315 start_i = 0;
4316 length = s->inter_ac_vlc_length;
4317 last_length= s->inter_ac_vlc_last_length;
4318 }
4319 last_non_zero = s->c.block_last_index[n];
4320
4321 dc += (1<<(RECON_SHIFT-1));
4322 for(i=0; i<64; i++){
4323 rem[i] = dc - (orig[i] << RECON_SHIFT); // FIXME use orig directly instead of copying to rem[]
4324 }
4325
4326 sum=0;
4327 for(i=0; i<64; i++){
4328 int one= 36;
4329 int qns=4;
4330 int w;
4331
4332 w= FFABS(weight[i]) + qns*one;
4333 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
4334
4335 weight[i] = w;
4336 // w=weight[i] = (63*qns + (w/2)) / w;
4337
4338 av_assert2(w>0);
4339 av_assert2(w<(1<<6));
4340 sum += w*w;
4341 }
4342 lambda = sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
4343
4344 run=0;
4345 rle_index=0;
4346 for(i=start_i; i<=last_non_zero; i++){
4347 int j= perm_scantable[i];
4348 const int level= block[j];
4349 int coeff;
4350
4351 if(level){
4352 if(level<0) coeff= qmul*level - qadd;
4353 else coeff= qmul*level + qadd;
4354 run_tab[rle_index++]=run;
4355 run=0;
4356
4357 s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
4358 }else{
4359 run++;
4360 }
4361 }
4362
4363 for(;;){
4364 int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
4365 int best_coeff=0;
4366 int best_change=0;
4367 int run2, best_unquant_change=0, analyze_gradient;
4368 analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
4369
4370 if(analyze_gradient){
4371 for(i=0; i<64; i++){
4372 int w= weight[i];
4373
4374 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
4375 }
4376 s->fdsp.fdct(d1);
4377 }
4378
4379 if(start_i){
4380 const int level= block[0];
4381 int change, old_coeff;
4382
4383 av_assert2(s->c.mb_intra);
4384
4385 old_coeff= q*level;
4386
4387 for(change=-1; change<=1; change+=2){
4388 int new_level= level + change;
4389 int score, new_coeff;
4390
4391 new_coeff= q*new_level;
4392 if(new_coeff >= 2048 || new_coeff < 0)
4393 continue;
4394
4395 score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
4396 new_coeff - old_coeff);
4397 if(score<best_score){
4398 best_score= score;
4399 best_coeff= 0;
4400 best_change= change;
4401 best_unquant_change= new_coeff - old_coeff;
4402 }
4403 }
4404 }
4405
4406 run=0;
4407 rle_index=0;
4408 run2= run_tab[rle_index++];
4409 prev_level=0;
4410 prev_run=0;
4411
4412 for(i=start_i; i<64; i++){
4413 int j= perm_scantable[i];
4414 const int level= block[j];
4415 int change, old_coeff;
4416
4417 if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
4418 break;
4419
4420 if(level){
4421 if(level<0) old_coeff= qmul*level - qadd;
4422 else old_coeff= qmul*level + qadd;
4423 run2= run_tab[rle_index++]; //FIXME ! maybe after last
4424 }else{
4425 old_coeff=0;
4426 run2--;
4427 av_assert2(run2>=0 || i >= last_non_zero );
4428 }
4429
4430 for(change=-1; change<=1; change+=2){
4431 int new_level= level + change;
4432 int score, new_coeff, unquant_change;
4433
4434 score=0;
4435 if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
4436 continue;
4437
4438 if(new_level){
4439 if(new_level<0) new_coeff= qmul*new_level - qadd;
4440 else new_coeff= qmul*new_level + qadd;
4441 if(new_coeff >= 2048 || new_coeff <= -2048)
4442 continue;
4443 //FIXME check for overflow
4444
4445 if(level){
4446 if(level < 63 && level > -63){
4447 if(i < last_non_zero)
4448 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
4449 - length[UNI_AC_ENC_INDEX(run, level+64)];
4450 else
4451 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
4452 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4453 }
4454 }else{
4455 av_assert2(FFABS(new_level)==1);
4456
4457 if(analyze_gradient){
4458 int g= d1[ scantable[i] ];
4459 if(g && (g^new_level) >= 0)
4460 continue;
4461 }
4462
4463 if(i < last_non_zero){
4464 int next_i= i + run2 + 1;
4465 int next_level= block[ perm_scantable[next_i] ] + 64;
4466
4467 if(next_level&(~127))
4468 next_level= 0;
4469
4470 if(next_i < last_non_zero)
4471 score += length[UNI_AC_ENC_INDEX(run, 65)]
4472 + length[UNI_AC_ENC_INDEX(run2, next_level)]
4473 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4474 else
4475 score += length[UNI_AC_ENC_INDEX(run, 65)]
4476 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4477 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4478 }else{
4479 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4480 if(prev_level){
4481 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4482 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4483 }
4484 }
4485 }
4486 }else{
4487 new_coeff=0;
4488 av_assert2(FFABS(level)==1);
4489
4490 if(i < last_non_zero){
4491 int next_i= i + run2 + 1;
4492 int next_level= block[ perm_scantable[next_i] ] + 64;
4493
4494 if(next_level&(~127))
4495 next_level= 0;
4496
4497 if(next_i < last_non_zero)
4498 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4499 - length[UNI_AC_ENC_INDEX(run2, next_level)]
4500 - length[UNI_AC_ENC_INDEX(run, 65)];
4501 else
4502 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4503 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4504 - length[UNI_AC_ENC_INDEX(run, 65)];
4505 }else{
4506 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4507 if(prev_level){
4508 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4509 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4510 }
4511 }
4512 }
4513
4514 score *= lambda;
4515
4516 unquant_change= new_coeff - old_coeff;
4517 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4518
4519 score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
4520 unquant_change);
4521 if(score<best_score){
4522 best_score= score;
4523 best_coeff= i;
4524 best_change= change;
4525 best_unquant_change= unquant_change;
4526 }
4527 }
4528 if(level){
4529 prev_level= level + 64;
4530 if(prev_level&(~127))
4531 prev_level= 0;
4532 prev_run= run;
4533 run=0;
4534 }else{
4535 run++;
4536 }
4537 }
4538
4539 if(best_change){
4540 int j= perm_scantable[ best_coeff ];
4541
4542 block[j] += best_change;
4543
4544 if(best_coeff > last_non_zero){
4545 last_non_zero= best_coeff;
4546 av_assert2(block[j]);
4547 }else{
4548 for(; last_non_zero>=start_i; last_non_zero--){
4549 if(block[perm_scantable[last_non_zero]])
4550 break;
4551 }
4552 }
4553
4554 run=0;
4555 rle_index=0;
4556 for(i=start_i; i<=last_non_zero; i++){
4557 const int level = block[perm_scantable[i]];
4558
4559 if(level){
4560 run_tab[rle_index++]=run;
4561 run=0;
4562 }else{
4563 run++;
4564 }
4565 }
4566
4567 s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4568 }else{
4569 break;
4570 }
4571 }
4572
4573 return last_non_zero;
4574 }
4575
4576 /**
4577 * Permute an 8x8 block according to permutation.
4578 * @param block the block which will be permuted according to
4579 * the given permutation vector
4580 * @param permutation the permutation vector
4581 * @param last the last non zero coefficient in scantable order, used to
4582 * speed the permutation up
4583 * @param scantable the used scantable, this is only used to speed the
4584 * permutation up, the block is not (inverse) permutated
4585 * to scantable order!
4586 */
4587 398775 void ff_block_permute(int16_t *block, const uint8_t *permutation,
4588 const uint8_t *scantable, int last)
4589 {
4590 int i;
4591 int16_t temp[64];
4592
4593
2/2
✓ Branch 0 taken 161381 times.
✓ Branch 1 taken 237394 times.
398775 if (last <= 0)
4594 161381 return;
4595 //FIXME it is ok but not clean and might fail for some permutations
4596 // if (permutation[1] == 1)
4597 // return;
4598
4599
2/2
✓ Branch 0 taken 7058131 times.
✓ Branch 1 taken 237394 times.
7295525 for (i = 0; i <= last; i++) {
4600 7058131 const int j = scantable[i];
4601 7058131 temp[j] = block[j];
4602 7058131 block[j] = 0;
4603 }
4604
4605
2/2
✓ Branch 0 taken 7058131 times.
✓ Branch 1 taken 237394 times.
7295525 for (i = 0; i <= last; i++) {
4606 7058131 const int j = scantable[i];
4607 7058131 const int perm_j = permutation[j];
4608 7058131 block[perm_j] = temp[j];
4609 }
4610 }
4611
4612 92798960 static int dct_quantize_c(MPVEncContext *const s,
4613 int16_t *block, int n,
4614 int qscale, int *overflow)
4615 {
4616 int i, last_non_zero, q, start_i;
4617 const int *qmat;
4618 const uint8_t *scantable;
4619 int bias;
4620 92798960 int max=0;
4621 unsigned int threshold1, threshold2;
4622
4623 92798960 s->fdsp.fdct(block);
4624
4625 92798960 denoise_dct(s, block);
4626
4627
2/2
✓ Branch 0 taken 83150070 times.
✓ Branch 1 taken 9648890 times.
92798960 if (s->c.mb_intra) {
4628 83150070 scantable = s->c.intra_scantable.scantable;
4629
2/2
✓ Branch 0 taken 8213580 times.
✓ Branch 1 taken 74936490 times.
83150070 if (!s->c.h263_aic) {
4630
2/2
✓ Branch 0 taken 4497464 times.
✓ Branch 1 taken 3716116 times.
8213580 if (n < 4)
4631 4497464 q = s->c.y_dc_scale;
4632 else
4633 3716116 q = s->c.c_dc_scale;
4634 8213580 q = q << 3;
4635 } else
4636 /* For AIC we skip quant/dequant of INTRADC */
4637 74936490 q = 1 << 3;
4638
4639 /* note: block[0] is assumed to be positive */
4640 83150070 block[0] = (block[0] + (q >> 1)) / q;
4641 83150070 start_i = 1;
4642 83150070 last_non_zero = 0;
4643
2/2
✓ Branch 0 taken 41979392 times.
✓ Branch 1 taken 41170678 times.
83150070 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
4644 83150070 bias= s->intra_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4645 } else {
4646 9648890 scantable = s->c.inter_scantable.scantable;
4647 9648890 start_i = 0;
4648 9648890 last_non_zero = -1;
4649 9648890 qmat = s->q_inter_matrix[qscale];
4650 9648890 bias= s->inter_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4651 }
4652 92798960 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4653 92798960 threshold2= (threshold1<<1);
4654
2/2
✓ Branch 0 taken 4916978379 times.
✓ Branch 1 taken 15889453 times.
4932867832 for(i=63;i>=start_i;i--) {
4655 4916978379 const int j = scantable[i];
4656 4916978379 int64_t level = (int64_t)block[j] * qmat[j];
4657
4658
2/2
✓ Branch 0 taken 76909507 times.
✓ Branch 1 taken 4840068872 times.
4916978379 if(((uint64_t)(level+threshold1))>threshold2){
4659 76909507 last_non_zero = i;
4660 76909507 break;
4661 }else{
4662 4840068872 block[j]=0;
4663 }
4664 }
4665
2/2
✓ Branch 0 taken 1015914498 times.
✓ Branch 1 taken 92798960 times.
1108713458 for(i=start_i; i<=last_non_zero; i++) {
4666 1015914498 const int j = scantable[i];
4667 1015914498 int64_t level = (int64_t)block[j] * qmat[j];
4668
4669 // if( bias+level >= (1<<QMAT_SHIFT)
4670 // || bias-level >= (1<<QMAT_SHIFT)){
4671
2/2
✓ Branch 0 taken 382844904 times.
✓ Branch 1 taken 633069594 times.
1015914498 if(((uint64_t)(level+threshold1))>threshold2){
4672
2/2
✓ Branch 0 taken 189645170 times.
✓ Branch 1 taken 193199734 times.
382844904 if(level>0){
4673 189645170 level= (bias + level)>>QMAT_SHIFT;
4674 189645170 block[j]= level;
4675 }else{
4676 193199734 level= (bias - level)>>QMAT_SHIFT;
4677 193199734 block[j]= -level;
4678 }
4679 382844904 max |=level;
4680 }else{
4681 633069594 block[j]=0;
4682 }
4683 }
4684 92798960 *overflow= s->max_qcoeff < max; //overflow might have happened
4685
4686 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4687
2/2
✓ Branch 0 taken 398775 times.
✓ Branch 1 taken 92400185 times.
92798960 if (s->c.idsp.perm_type != FF_IDCT_PERM_NONE)
4688 398775 ff_block_permute(block, s->c.idsp.idct_permutation,
4689 scantable, last_non_zero);
4690
4691 92798960 return last_non_zero;
4692 }
4693