FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2026-09-19 20:18:21
Exec Total Coverage
Lines: 2019 2812 71.8%
Functions: 52 62 83.9%
Branches: 1158 1770 65.4%

Line Branch Exec Source
1 /*
2 * The simplest mpeg encoder (well, it was the simplest!)
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7 *
8 * This file is part of FFmpeg.
9 *
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /*
26 * 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 3401 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 3401 FDCTDSPContext *fdsp = &s->fdsp;
115 int qscale;
116 3401 int shift = 0;
117
118
2/2
✓ Branch 0 taken 98977 times.
✓ Branch 1 taken 3401 times.
102378 for (qscale = qmin; qscale <= qmax; qscale++) {
119 int i;
120 int qscale2;
121
122
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 98946 times.
98977 if (s->c.q_scale_type) qscale2 = ff_mpeg2_non_linear_qscale[qscale];
123 98946 else qscale2 = qscale << 1;
124
125
2/2
✓ Branch 0 taken 98227 times.
✓ Branch 1 taken 750 times.
98977 if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
126 #if CONFIG_FAANDCT
127
1/2
✓ Branch 0 taken 98227 times.
✗ Branch 1 not taken.
98227 fdsp->fdct == ff_faandct ||
128 #endif /* CONFIG_FAANDCT */
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98227 times.
98227 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 97627 times.
✓ Branch 1 taken 600 times.
98227 } else if (fdsp->fdct == ff_fdct_ifast) {
142
2/2
✓ Branch 0 taken 6248128 times.
✓ Branch 1 taken 97627 times.
6345755 for (i = 0; i < 64; i++) {
143 6248128 const int j = s->c.idsp.idct_permutation[i];
144 6248128 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 6248128 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 6241131 times.
✓ Branch 1 taken 98977 times.
6340108 for (i = intra; i < 64; i++) {
180 6241131 int64_t max = 8191;
181
2/2
✓ Branch 0 taken 6155541 times.
✓ Branch 1 taken 85590 times.
6241131 if (fdsp->fdct == ff_fdct_ifast) {
182 6155541 max = (8191LL * ff_aanscales[i]) >> 14;
183 }
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6241131 times.
6241131 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
185 shift++;
186 }
187 }
188 }
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3401 times.
3401 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 3401 }
195
196 17417 static inline void update_qscale(MPVMainEncContext *const m)
197 {
198 17417 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 17417 s->c.qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
218 (FF_LAMBDA_SHIFT + 7);
219
2/2
✓ Branch 0 taken 17292 times.
✓ Branch 1 taken 125 times.
17417 s->c.qscale = av_clip(s->c.qscale, s->c.avctx->qmin, m->vbv_ignore_qmax ? 31 : s->c.avctx->qmax);
220 }
221
222 17417 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
223 FF_LAMBDA_SHIFT;
224 17417 }
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 800 static void init_qscale_tab(MPVEncContext *const s)
243 {
244 800 int8_t *const qscale_table = s->c.cur_pic.qscale_table;
245
246
2/2
✓ Branch 0 taken 239550 times.
✓ Branch 1 taken 800 times.
240350 for (int i = 0; i < s->c.mb_num; i++) {
247 239550 unsigned int lam = s->lambda_table[s->c.mb_index2xy[i]];
248 239550 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
249 239550 qscale_table[s->c.mb_index2xy[i]] = av_clip(qp, s->c.avctx->qmin,
250 239550 s->c.avctx->qmax);
251 }
252 800 }
253
254 2772 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 2772 COPY(c.pict_type);
259 2772 COPY(f_code);
260 2772 COPY(b_code);
261 2772 COPY(c.qscale);
262 2772 COPY(lambda);
263 2772 COPY(lambda2);
264 2772 COPY(c.frame_pred_frame_dct); // FIXME don't set in encode_header
265 2772 COPY(c.progressive_frame); // FIXME don't set in encode_header
266 2772 COPY(partitioned_frame); // FIXME don't set in encode_header
267 #undef COPY
268 2772 }
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 236 static av_cold void mpv_encode_defaults(MPVMainEncContext *const m)
280 {
281 236 MPVEncContext *const s = &m->s;
282 static AVOnce init_static_once = AV_ONCE_INIT;
283
284 236 ff_mpv_common_defaults(&s->c);
285
286 236 s->f_code = 1;
287 236 s->b_code = 1;
288
289
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 148 times.
236 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 157 times.
236 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 236 }
298
299 309 av_cold void ff_dct_encode_init(MPVEncContext *const s)
300 {
301 309 s->dct_quantize = dct_quantize_c;
302
303 #if ARCH_X86
304 309 ff_dct_encode_init_x86(s);
305 #endif
306
307
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 274 times.
309 if (s->c.avctx->trellis)
308 35 s->dct_quantize = dct_quantize_trellis_c;
309 309 }
310
311 236 static av_cold void init_unquantize(MPVEncContext *const s2, AVCodecContext *avctx)
312 {
313 236 MpegEncContext *const s = &s2->c;
314 MPVUnquantDSPContext unquant_dsp_ctx;
315
316 236 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 166 times.
✓ Branch 1 taken 70 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 166 times.
236 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 107 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 51 times.
166 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
323 115 s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_h263_intra;
324 115 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 236 }
330
331 236 static av_cold int me_cmp_init(MPVMainEncContext *const m, AVCodecContext *avctx)
332 {
333 236 MPVEncContext *const s = &m->s;
334 MECmpContext mecc;
335 me_cmp_func me_cmp[6];
336 int ret;
337
338 236 ff_me_cmp_init(&mecc, avctx);
339 236 ret = ff_me_init(&s->me, avctx, &mecc, 1);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (ret < 0)
341 return ret;
342 236 ret = ff_set_cmp(&mecc, me_cmp, m->frame_skip_cmp, 1);
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (ret < 0)
344 return ret;
345 236 m->frame_skip_cmp_fn = me_cmp[1];
346
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 219 times.
236 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 236 s->sum_abs_dctelem = mecc.sum_abs_dctelem;
357
358 236 s->sse_cmp[0] = mecc.sse[0];
359 236 s->sse_cmp[1] = mecc.sse[1];
360 236 s->sad_cmp[0] = mecc.sad[0];
361 236 s->sad_cmp[1] = mecc.sad[1];
362
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 232 times.
236 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 232 s->n_sse_cmp[0] = mecc.sse[0];
367 232 s->n_sse_cmp[1] = mecc.sse[1];
368 }
369
370 236 return 0;
371 }
372
373 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
374 236 static av_cold int init_matrices(MPVMainEncContext *const m, AVCodecContext *avctx)
375 {
376 236 MPVEncContext *const s = &m->s;
377
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 205 times.
236 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 236 times.
✗ Branch 2 not taken.
236 if (!ALLOCZ_ARRAYS(s->q_intra_matrix, 32, nb_matrices) ||
382
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 236 times.
236 !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 205 times.
236 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 205 s->q_chroma_intra_matrix = s->q_intra_matrix;
394 205 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
395 }
396
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 19 times.
205 if (!m->intra_only) {
397 186 s->q_inter_matrix = s->q_intra_matrix + 32;
398 186 s->q_inter_matrix16 = s->q_intra_matrix16 + 32;
399 }
400
401
2/2
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 134 times.
205 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4 &&
402
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 67 times.
71 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 107 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 86 times.
201 } else if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
406 115 intra_matrix =
407 115 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 205 times.
205 if (avctx->intra_matrix)
414 intra_matrix = avctx->intra_matrix;
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 205 times.
205 if (avctx->inter_matrix)
416 inter_matrix = avctx->inter_matrix;
417
418 /* init q matrix */
419
2/2
✓ Branch 0 taken 13120 times.
✓ Branch 1 taken 205 times.
13325 for (int i = 0; i < 64; i++) {
420 13120 int j = s->c.idsp.idct_permutation[i];
421
422 13120 s->c.intra_matrix[j] = s->c.chroma_intra_matrix[j] = intra_matrix[i];
423 13120 s->c.inter_matrix[j] = inter_matrix[i];
424 }
425
426 /* precompute matrix */
427 205 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 205 times.
205 if (ret < 0)
429 return ret;
430
431 205 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
432 205 s->c.intra_matrix, s->intra_quant_bias, avctx->qmin,
433 31, 1);
434
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 19 times.
205 if (s->q_inter_matrix)
435 186 ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
436 186 s->c.inter_matrix, s->inter_quant_bias, avctx->qmin,
437 31, 0);
438
439 205 return 0;
440 }
441
442 236 static av_cold int init_buffers(MPVMainEncContext *const m)
443 {
444 236 MPVEncContext *const s = &m->s;
445 236 int has_b_frames = !!m->max_b_frames;
446 int16_t (*mv_table)[2];
447
448 /* Allocate MB type table */
449 236 unsigned mb_array_size = s->c.mb_stride * s->c.mb_height;
450 236 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 236 times.
236 if (!s->mb_type)
452 return AVERROR(ENOMEM);
453 236 s->mc_mb_var = s->mb_type + mb_array_size;
454 236 s->mb_var = s->mc_mb_var + mb_array_size;
455 236 s->mb_mean = (uint8_t*)(s->mb_var + mb_array_size);
456
457
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 236 times.
236 if (!FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size))
458 return AVERROR(ENOMEM);
459
460 236 unsigned mv_table_size = (s->c.mb_height + 2) * s->c.mb_stride + 1;
461 236 unsigned nb_mv_tables = 1 + 5 * has_b_frames;
462
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 71 times.
236 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 87 nb_mv_tables += 8 * has_b_frames;
465 87 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 87 times.
87 if (!s->p_field_select_table[0])
467 return AVERROR(ENOMEM);
468 87 s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size;
469 }
470
471 236 mv_table = av_calloc(mv_table_size, nb_mv_tables * sizeof(*mv_table));
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (!mv_table)
473 return AVERROR(ENOMEM);
474 236 m->mv_table_base = mv_table;
475 236 mv_table += s->c.mb_stride + 1;
476
477 236 s->p_mv_table = mv_table;
478
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 191 times.
236 if (has_b_frames) {
479 45 s->b_forw_mv_table = mv_table += mv_table_size;
480 45 s->b_back_mv_table = mv_table += mv_table_size;
481 45 s->b_bidir_forw_mv_table = mv_table += mv_table_size;
482 45 s->b_bidir_back_mv_table = mv_table += mv_table_size;
483 45 s->b_direct_mv_table = mv_table += mv_table_size;
484
485
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 12 times.
45 if (s->p_field_select_table[1]) { // MPEG-4 or INTERLACED_ME above
486 33 uint8_t *field_select = s->p_field_select_table[1];
487
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 33 times.
99 for (int j = 0; j < 2; j++) {
488
2/2
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 66 times.
198 for (int k = 0; k < 2; k++) {
489
2/2
✓ Branch 0 taken 264 times.
✓ Branch 1 taken 132 times.
396 for (int l = 0; l < 2; l++)
490 264 s->b_field_mv_table[j][k][l] = mv_table += mv_table_size;
491 132 s->b_field_select_table[j][k] = field_select += 2 * mv_table_size;
492 }
493 }
494 }
495 }
496
497 236 return 0;
498 }
499
500 236 static av_cold int init_slice_buffers(MPVMainEncContext *const m)
501 {
502 236 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 236 unsigned nb_slices = s->c.slice_context_count;
516 236 char *dct_error = NULL;
517
518
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 229 times.
236 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 236 const int y_size = s->c.b8_stride * (2 * s->c.mb_height + 1);
529 236 const int c_size = s->c.mb_stride * (s->c.mb_height + 1);
530 236 const int yc_size = y_size + 2 * c_size;
531 236 ptrdiff_t offset = 0;
532
533
2/2
✓ Branch 0 taken 334 times.
✓ Branch 1 taken 236 times.
570 for (unsigned i = 0; i < nb_slices; ++i) {
534 334 MPVEncContext *const s2 = s->c.enc_contexts[i];
535
536 334 s2->block = s2->blocks[0];
537
538
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 327 times.
334 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 126 times.
✓ Branch 1 taken 208 times.
334 if (s2->c.ac_val) {
545 126 s2->c.dc_val += offset + i;
546 126 s2->c.ac_val += offset;
547 126 offset += yc_size;
548 }
549 }
550 236 return 0;
551 }
552
553 /* init video encoder */
554 236 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
555 {
556 236 MPVMainEncContext *const m = avctx->priv_data;
557 236 MPVEncContext *const s = &m->s;
558 AVCPBProperties *cpb_props;
559 int gcd, ret;
560
561 236 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 216 times.
236 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 216 case AV_PIX_FMT_YUVJ420P:
575 case AV_PIX_FMT_YUV420P:
576 216 s->c.chroma_format = CHROMA_420;
577 216 break;
578 }
579
580 236 avctx->bits_per_raw_sample = av_clip(avctx->bits_per_raw_sample, 0, 8);
581
582 236 m->bit_rate = avctx->bit_rate;
583 236 s->c.width = avctx->width;
584 236 s->c.height = avctx->height;
585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 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 236 m->gop_size = avctx->gop_size;
593 236 s->c.avctx = avctx;
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 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 236 times.
236 } 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 236 m->max_b_frames = avctx->max_b_frames;
604 236 s->c.codec_id = avctx->codec->id;
605
3/4
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 191 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45 times.
236 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 236 s->c.quarter_sample = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
611 236 s->rtp_mode = !!s->rtp_payload_size;
612 236 m->user_specified_pts = AV_NOPTS_VALUE;
613
614
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 226 times.
236 if (m->gop_size <= 1) {
615 10 m->intra_only = 1;
616 10 m->gop_size = 12;
617 } else {
618 226 m->intra_only = 0;
619 }
620
621 /* Fixed QSCALE */
622 236 m->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
623
624 708 s->adaptive_quant = (avctx->lumi_masking ||
625
1/2
✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
236 avctx->dark_masking ||
626
1/2
✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
236 avctx->temporal_cplx_masking ||
627
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 4 times.
236 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 236 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 220 times.
484 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
631
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 !m->fixed_qscale;
632
633 236 s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
634
635
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 234 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
236 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 236 times.
236 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 234 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
236 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 234 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
236 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 234 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
236 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 234 times.
236 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 234 times.
236 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 79 times.
✓ Branch 1 taken 157 times.
236 if (!m->fixed_qscale &&
696 79 avctx->bit_rate * av_q2d(avctx->time_base) >
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79 times.
79 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 32 times.
✓ Branch 1 taken 204 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
236 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 233 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
236 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 232 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
236 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 71 times.
236 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 80 times.
✗ Branch 1 not taken.
80 (avctx->sample_aspect_ratio.num > 255 ||
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 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 230 times.
✓ Branch 1 taken 6 times.
236 if ((s->c.codec_id == AV_CODEC_ID_H263 ||
738
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 227 times.
230 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 229 times.
236 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 230 times.
✓ Branch 1 taken 6 times.
236 if ((s->c.codec_id == AV_CODEC_ID_H263 ||
751
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 3 times.
230 s->c.codec_id == AV_CODEC_ID_H263P ||
752
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 224 times.
227 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 232 times.
✓ Branch 1 taken 4 times.
236 if ((s->c.codec_id == AV_CODEC_ID_WMV1 ||
760
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 228 times.
232 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 219 times.
236 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 236 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
236 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 224 times.
236 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 233 times.
✓ Branch 1 taken 3 times.
236 if (m->scenechange_threshold < 1000000000 &&
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 233 times.
233 (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 235 times.
236 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 224 times.
236 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 4 times.
✓ Branch 1 taken 232 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
236 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 236 gcd = av_gcd(avctx->time_base.den, avctx->time_base.num);
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 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 166 times.
✓ Branch 1 taken 70 times.
✓ Branch 2 taken 155 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 155 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 128 times.
✓ Branch 7 taken 27 times.
✓ Branch 8 taken 124 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 9 times.
✓ Branch 11 taken 115 times.
236 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 115 s->intra_quant_bias = 0;
832 // (a - x / 4) / x
833 115 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
834 }
835
836
2/4
✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 236 times.
236 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 236 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 71 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.
236 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 71 case AV_CODEC_ID_MPEG4:
934 71 s->c.out_format = FMT_H263;
935 71 s->c.h263_pred = 1;
936 71 s->me.unrestricted_mv = 1;
937 71 s->flipflop_rounding = 1;
938 71 s->c.low_delay = m->max_b_frames ? 0 : 1;
939
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 50 times.
71 avctx->delay = s->c.low_delay ? 0 : (m->max_b_frames + 1);
940 71 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 236 avctx->has_b_frames = !s->c.low_delay;
981
982 236 s->c.encoding = 1;
983
984 236 s->c.progressive_frame =
985
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 17 times.
455 s->c.progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
986 AV_CODEC_FLAG_INTERLACED_ME) ||
987
2/2
✓ Branch 0 taken 215 times.
✓ Branch 1 taken 4 times.
219 s->c.alternate_scan);
988
989
3/4
✓ Branch 0 taken 236 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 205 times.
✓ Branch 3 taken 31 times.
236 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 31 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 236 times.
236 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 236 s->parent = m;
1010 236 ff_mpv_idct_init(&s->c);
1011 236 init_unquantize(s, avctx);
1012 236 ff_fdctdsp_init(&s->fdsp, avctx);
1013 236 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
1014 236 ff_pixblockdsp_init(&s->pdsp, 8);
1015 236 ret = me_cmp_init(m, avctx);
1016
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (ret < 0)
1017 return ret;
1018
1019
1/2
✓ Branch 1 taken 236 times.
✗ Branch 2 not taken.
236 if (!(avctx->stats_out = av_mallocz(256)) ||
1020
1/2
✓ Branch 1 taken 236 times.
✗ Branch 2 not taken.
236 !(s->new_pic = av_frame_alloc()) ||
1021
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 236 times.
236 !(s->c.picture_pool = ff_mpv_alloc_pic_pool(0)))
1022 return AVERROR(ENOMEM);
1023
1024 236 ret = init_matrices(m, avctx);
1025
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (ret < 0)
1026 return ret;
1027
1028 236 ff_dct_encode_init(s);
1029
1030
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 125 times.
236 if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263) {
1031 111 ff_h263_encode_init(m);
1032 #if CONFIG_MSMPEG4ENC
1033
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 94 times.
111 if (s->c.msmpeg4_version != MSMP4_UNUSED)
1034 17 ff_msmpeg4_encode_init(m);
1035 #endif
1036 }
1037
1038 236 s->c.slice_ctx_size = sizeof(*s);
1039 236 ret = ff_mpv_common_init(&s->c);
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (ret < 0)
1041 return ret;
1042 236 ret = init_buffers(m);
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (ret < 0)
1044 return ret;
1045
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 206 times.
236 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 236 ret = ff_mpv_init_duplicate_contexts(&s->c);
1051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (ret < 0)
1052 return ret;
1053
1054 236 ret = init_slice_buffers(m);
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (ret < 0)
1056 return ret;
1057
1058 236 ret = ff_rate_control_init(m);
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (ret < 0)
1060 return ret;
1061
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 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 236 cpb_props = ff_encode_add_cpb_side_data(avctx);
1079
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 236 times.
236 if (!cpb_props)
1080 return AVERROR(ENOMEM);
1081 236 cpb_props->max_bitrate = avctx->rc_max_rate;
1082 236 cpb_props->min_bitrate = avctx->rc_min_rate;
1083 236 cpb_props->avg_bitrate = avctx->bit_rate;
1084 236 cpb_props->buffer_size = avctx->rc_buffer_size;
1085
1086 236 return 0;
1087 }
1088
1089 236 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
1090 {
1091 236 MPVMainEncContext *const m = avctx->priv_data;
1092 236 MPVEncContext *const s = &m->s;
1093
1094 236 ff_rate_control_uninit(&m->rc_context);
1095
1096 236 ff_mpv_common_end(&s->c);
1097 236 av_refstruct_pool_uninit(&s->c.picture_pool);
1098
1099
2/2
✓ Branch 0 taken 4012 times.
✓ Branch 1 taken 236 times.
4248 for (int i = 0; i < MPVENC_MAX_B_FRAMES + 1; i++) {
1100 4012 av_refstruct_unref(&m->input_picture[i]);
1101 4012 av_refstruct_unref(&m->reordered_input_picture[i]);
1102 }
1103
2/2
✓ Branch 0 taken 4248 times.
✓ Branch 1 taken 236 times.
4484 for (int i = 0; i < FF_ARRAY_ELEMS(m->tmp_frames); i++)
1104 4248 av_frame_free(&m->tmp_frames[i]);
1105
1106 236 av_frame_free(&s->new_pic);
1107
1108 236 av_freep(&avctx->stats_out);
1109
1110 236 av_freep(&m->mv_table_base);
1111 236 av_freep(&s->p_field_select_table[0]);
1112 236 av_freep(&m->dct_error_sum_base);
1113
1114 236 av_freep(&s->mb_type);
1115 236 av_freep(&s->lambda_table);
1116
1117 236 av_freep(&s->q_intra_matrix);
1118 236 av_freep(&s->q_intra_matrix16);
1119 236 av_freep(&s->dct_offset);
1120
1121 236 return 0;
1122 }
1123
1124 /* put block[] to dest[] */
1125 3341492 static inline void put_dct(MPVEncContext *const s,
1126 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1127 {
1128 3341492 s->c.dct_unquantize_intra(&s->c, block, i, qscale);
1129 3341492 s->c.idsp.idct_put(dest, line_size, block);
1130 3341492 }
1131
1132 22456674 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 8156159 times.
✓ Branch 1 taken 14300515 times.
22456674 if (s->c.block_last_index[i] >= 0) {
1136 8156159 s->c.dct_unquantize_inter(&s->c, block, i, qscale);
1137
1138 8156159 s->c.idsp.idct_add(dest, line_size, block);
1139 }
1140 22456674 }
1141
1142 /**
1143 * Performs dequantization and IDCT (if necessary)
1144 */
1145 5258935 static void mpv_reconstruct_mb(MPVEncContext *const s, int16_t block[12][64])
1146 {
1147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5258935 times.
5258935 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 4170876 times.
✓ Branch 1 taken 1088059 times.
5258935 if ((1 << s->c.pict_type) & s->frame_reconstruction_bitfield) {
1160 4170876 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 4170876 const int linesize = s->c.cur_pic.linesize[0];
1163 4170876 const int uvlinesize = s->c.cur_pic.linesize[1];
1164 4170876 const int block_size = 8;
1165
1166 4170876 dct_linesize = linesize << s->c.interlaced_dct;
1167
2/2
✓ Branch 0 taken 4159093 times.
✓ Branch 1 taken 11783 times.
4170876 dct_offset = s->c.interlaced_dct ? linesize : linesize * block_size;
1168
1169
2/2
✓ Branch 0 taken 3626881 times.
✓ Branch 1 taken 543995 times.
4170876 if (!s->c.mb_intra) {
1170 /* No MC, as that was already done otherwise */
1171 3626881 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->c.qscale);
1172 3626881 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->c.qscale);
1173 3626881 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->c.qscale);
1174 3626881 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 3279187 times.
✓ Branch 1 taken 347694 times.
3626881 if (s->c.chroma_y_shift) {
1178 3279187 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->c.chroma_qscale);
1179 3279187 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 543995 put_dct(s, block[0], 0, dest_y , dct_linesize, s->c.qscale);
1192 543995 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->c.qscale);
1193 543995 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->c.qscale);
1194 543995 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 505234 times.
✓ Branch 1 taken 38761 times.
543995 if (s->c.chroma_y_shift) {
1198 505234 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->c.chroma_qscale);
1199 505234 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 5258935 }
1212
1213 43740 static int get_sae(const uint8_t *src, int ref, int stride)
1214 {
1215 int x,y;
1216 43740 int acc = 0;
1217
1218
2/2
✓ Branch 0 taken 699840 times.
✓ Branch 1 taken 43740 times.
743580 for (y = 0; y < 16; y++) {
1219
2/2
✓ Branch 0 taken 11197440 times.
✓ Branch 1 taken 699840 times.
11897280 for (x = 0; x < 16; x++) {
1220 11197440 acc += FFABS(src[x + y * stride] - ref);
1221 }
1222 }
1223
1224 43740 return acc;
1225 }
1226
1227 155 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 155 int acc = 0;
1232
1233 155 w = s->c.width & ~15;
1234 155 h = s->c.height & ~15;
1235
1236
2/2
✓ Branch 0 taken 2070 times.
✓ Branch 1 taken 155 times.
2225 for (y = 0; y < h; y += 16) {
1237
2/2
✓ Branch 0 taken 43740 times.
✓ Branch 1 taken 2070 times.
45810 for (x = 0; x < w; x += 16) {
1238 43740 int offset = x + y * stride;
1239 43740 int sad = s->sad_cmp[0](NULL, src + offset, ref + offset,
1240 stride, 16);
1241 43740 int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
1242 43740 int sae = get_sae(src + offset, mean, stride);
1243
1244 43740 acc += sae + 500 < sad;
1245 }
1246 }
1247 155 return acc;
1248 }
1249
1250 /**
1251 * Allocates new buffers for an AVFrame and copies the properties
1252 * from another AVFrame.
1253 */
1254 12126 static int prepare_picture(MPVEncContext *const s, AVFrame *f, const AVFrame *props_frame)
1255 {
1256 12126 AVCodecContext *avctx = s->c.avctx;
1257 int ret;
1258
1259 12126 f->width = avctx->width + 2 * EDGE_WIDTH;
1260 12126 f->height = avctx->height + 2 * EDGE_WIDTH;
1261
1262 12126 ret = ff_encode_alloc_frame(avctx, f);
1263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12126 times.
12126 if (ret < 0)
1264 return ret;
1265
1266 12126 ret = ff_mpv_pic_check_linesize(avctx, f, &s->c.linesize, &s->c.uvlinesize);
1267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12126 times.
12126 if (ret < 0)
1268 return ret;
1269
1270
2/2
✓ Branch 0 taken 36378 times.
✓ Branch 1 taken 12126 times.
48504 for (int i = 0; f->data[i]; i++) {
1271
2/2
✓ Branch 0 taken 24252 times.
✓ Branch 1 taken 12126 times.
36378 int offset = (EDGE_WIDTH >> (i ? s->c.chroma_y_shift : 0)) *
1272 36378 f->linesize[i] +
1273
2/2
✓ Branch 0 taken 24252 times.
✓ Branch 1 taken 12126 times.
36378 (EDGE_WIDTH >> (i ? s->c.chroma_x_shift : 0));
1274 36378 f->data[i] += offset;
1275 }
1276 12126 f->width = avctx->width;
1277 12126 f->height = avctx->height;
1278
1279 12126 ret = av_frame_copy_props(f, props_frame);
1280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12126 times.
12126 if (ret < 0)
1281 return ret;
1282
1283 12126 return 0;
1284 }
1285
1286 12370 static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg)
1287 {
1288 12370 MPVEncContext *const s = &m->s;
1289 12370 MPVPicture *pic = NULL;
1290 int64_t pts;
1291 12370 int display_picture_number = 0, ret;
1292 24740 int encoding_delay = m->max_b_frames ? m->max_b_frames
1293
2/2
✓ Branch 0 taken 2282 times.
✓ Branch 1 taken 10088 times.
12370 : (s->c.low_delay ? 0 : 1);
1294 12370 int flush_offset = 1;
1295 12370 int direct = 1;
1296
1297 av_assert1(!m->input_picture[0]);
1298
1299
2/2
✓ Branch 0 taken 12076 times.
✓ Branch 1 taken 294 times.
12370 if (pic_arg) {
1300 12076 pts = pic_arg->pts;
1301 12076 display_picture_number = m->input_picture_number++;
1302
1303
1/2
✓ Branch 0 taken 12076 times.
✗ Branch 1 not taken.
12076 if (pts != AV_NOPTS_VALUE) {
1304
2/2
✓ Branch 0 taken 2690 times.
✓ Branch 1 taken 9386 times.
12076 if (s->c.codec_id == AV_CODEC_ID_MPEG4 &&
1305
1/2
✓ Branch 0 taken 2690 times.
✗ Branch 1 not taken.
2690 (pts > INT64_MAX / 2 / s->c.avctx->time_base.num ||
1306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2690 times.
2690 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 11840 times.
✓ Branch 1 taken 236 times.
12076 if (m->user_specified_pts != AV_NOPTS_VALUE) {
1312 11840 int64_t last = m->user_specified_pts;
1313
1314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11840 times.
11840 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 6084 times.
✓ Branch 1 taken 5756 times.
✓ Branch 2 taken 97 times.
✓ Branch 3 taken 5987 times.
11840 if (!s->c.low_delay && display_picture_number == 1)
1322 97 m->dts_delta = pts - last;
1323 }
1324 12076 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 746 times.
✓ Branch 1 taken 11330 times.
12076 if (pic_arg->linesize[0] != s->c.linesize ||
1338
1/2
✓ Branch 0 taken 746 times.
✗ Branch 1 not taken.
746 pic_arg->linesize[1] != s->c.uvlinesize ||
1339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 746 times.
746 pic_arg->linesize[2] != s->c.uvlinesize)
1340 11330 direct = 0;
1341
4/4
✓ Branch 0 taken 9025 times.
✓ Branch 1 taken 3051 times.
✓ Branch 2 taken 1262 times.
✓ Branch 3 taken 7763 times.
12076 if ((s->c.width & 15) || (s->c.height & 15))
1342 4313 direct = 0;
1343
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 11810 times.
12076 if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
1344 266 direct = 0;
1345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12076 times.
12076 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 12076 pic = av_refstruct_pool_get(s->c.picture_pool);
1352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12076 times.
12076 if (!pic)
1353 return AVERROR(ENOMEM);
1354
1355
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 11402 times.
12076 if (direct) {
1356
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 674 times.
674 if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1357 goto fail;
1358 674 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 uint8_t *dst = pic->f->data[i];
1373 34206 int vpad = 16;
1374
1375
2/2
✓ Branch 0 taken 13794 times.
✓ Branch 1 taken 20412 times.
34206 if ( s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO
1376
2/2
✓ Branch 0 taken 3075 times.
✓ Branch 1 taken 10719 times.
13794 && !s->c.progressive_sequence
1377
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 2325 times.
3075 && FFALIGN(s->c.height, 32) - s->c.height > 16)
1378 750 vpad = 32;
1379
1380
2/2
✓ Branch 0 taken 34056 times.
✓ Branch 1 taken 150 times.
34206 if (!s->c.avctx->rc_buffer_size)
1381 34056 dst += INPLACE_OFFSET;
1382
1383
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 33893 times.
34206 if (src_stride == dst_stride)
1384 313 memcpy(dst, src, src_stride * h - src_stride + w);
1385 else {
1386 33893 int h2 = h;
1387 33893 uint8_t *dst2 = dst;
1388
2/2
✓ Branch 0 taken 5060268 times.
✓ Branch 1 taken 33893 times.
5094161 while (h2--) {
1389 5060268 memcpy(dst2, src, w);
1390 5060268 dst2 += dst_stride;
1391 5060268 src += src_stride;
1392 }
1393 }
1394
4/4
✓ Branch 0 taken 25053 times.
✓ Branch 1 taken 9153 times.
✓ Branch 2 taken 3786 times.
✓ Branch 3 taken 21267 times.
34206 if ((s->c.width & 15) || (s->c.height & (vpad-1))) {
1395 12939 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 12076 pic->display_picture_number = display_picture_number;
1405 12076 pic->f->pts = pts; // we set this here to avoid modifying pic_arg
1406
2/2
✓ Branch 0 taken 243 times.
✓ Branch 1 taken 51 times.
294 } 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 484 times.
✓ Branch 1 taken 148 times.
632 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1413
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 389 times.
484 if (m->input_picture[flush_offset])
1414 95 break;
1415
1416 243 encoding_delay -= flush_offset - 1;
1417 }
1418
1419 /* shift buffer entries */
1420
2/2
✓ Branch 0 taken 197774 times.
✓ Branch 1 taken 12370 times.
210144 for (int i = flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
1421 197774 m->input_picture[i - flush_offset] = m->input_picture[i];
1422
2/2
✓ Branch 0 taken 12516 times.
✓ Branch 1 taken 12370 times.
24886 for (int i = MPVENC_MAX_B_FRAMES + 1 - flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
1423 12516 m->input_picture[i] = NULL;
1424
1425 12370 m->input_picture[encoding_delay] = pic;
1426
1427 12370 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 int off = p->shared ? 0 : 16;
1446 const uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1447 const uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1448 int v = m->frame_skip_cmp_fn(s, dptr, rptr, stride, 8);
1449
1450 switch (FFABS(m->frame_skip_exp)) {
1451 case 0: score = FFMAX(score, v); break;
1452 case 1: score += FFABS(v); break;
1453 case 2: score64 += v * (int64_t)v; break;
1454 case 3: score64 += FFABS(v * (int64_t)v * v); break;
1455 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
1456 }
1457 }
1458 }
1459 }
1460 emms_c();
1461
1462 if (score)
1463 score64 = score;
1464 if (m->frame_skip_exp < 0)
1465 score64 = pow(score64 / (double)(s->c.mb_width * s->c.mb_height),
1466 -1.0/m->frame_skip_exp);
1467
1468 if (score64 < m->frame_skip_threshold)
1469 return 1;
1470 if (score64 < ((m->frame_skip_factor * (int64_t) s->lambda) >> 8))
1471 return 1;
1472 return 0;
1473 }
1474
1475 static int encode_frame(AVCodecContext *c, const AVFrame *frame, AVPacket *pkt)
1476 {
1477 int ret;
1478 int size = 0;
1479
1480 ret = avcodec_send_frame(c, frame);
1481 if (ret < 0)
1482 return ret;
1483
1484 do {
1485 ret = avcodec_receive_packet(c, pkt);
1486 if (ret >= 0) {
1487 size += pkt->size;
1488 av_packet_unref(pkt);
1489 } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1490 return ret;
1491 } while (ret >= 0);
1492
1493 return size;
1494 }
1495
1496 static int estimate_best_b_count(MPVMainEncContext *const m)
1497 {
1498 MPVEncContext *const s = &m->s;
1499 AVPacket *pkt;
1500 const int scale = m->brd_scale;
1501 int width = s->c.width >> scale;
1502 int height = s->c.height >> scale;
1503 int out_size, p_lambda, b_lambda, lambda2;
1504 int64_t best_rd = INT64_MAX;
1505 int best_b_count = -1;
1506 int ret = 0;
1507
1508 av_assert0(scale >= 0 && scale <= 3);
1509
1510 pkt = av_packet_alloc();
1511 if (!pkt)
1512 return AVERROR(ENOMEM);
1513
1514 p_lambda = m->last_lambda_for[AV_PICTURE_TYPE_P];
1515 //p_lambda * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset;
1516 b_lambda = m->last_lambda_for[AV_PICTURE_TYPE_B];
1517 if (!b_lambda) // FIXME we should do this somewhere else
1518 b_lambda = p_lambda;
1519 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1520 FF_LAMBDA_SHIFT;
1521
1522 for (int i = 0; i < m->max_b_frames + 2; i++) {
1523 const MPVPicture *pre_input_ptr = i ? m->input_picture[i - 1] :
1524 s->c.next_pic.ptr;
1525
1526 if (pre_input_ptr) {
1527 const uint8_t *data[4];
1528 memcpy(data, pre_input_ptr->f->data, sizeof(data));
1529
1530 if (!pre_input_ptr->shared && i) {
1531 data[0] += INPLACE_OFFSET;
1532 data[1] += INPLACE_OFFSET;
1533 data[2] += INPLACE_OFFSET;
1534 }
1535
1536 s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[0],
1537 m->tmp_frames[i]->linesize[0],
1538 data[0],
1539 pre_input_ptr->f->linesize[0],
1540 width, height);
1541 s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[1],
1542 m->tmp_frames[i]->linesize[1],
1543 data[1],
1544 pre_input_ptr->f->linesize[1],
1545 width >> 1, height >> 1);
1546 s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[2],
1547 m->tmp_frames[i]->linesize[2],
1548 data[2],
1549 pre_input_ptr->f->linesize[2],
1550 width >> 1, height >> 1);
1551 }
1552 }
1553
1554 for (int j = 0; j < m->max_b_frames + 1; j++) {
1555 AVCodecContext *c;
1556 int64_t rd = 0;
1557
1558 if (!m->input_picture[j])
1559 break;
1560
1561 c = avcodec_alloc_context3(NULL);
1562 if (!c) {
1563 ret = AVERROR(ENOMEM);
1564 goto fail;
1565 }
1566
1567 c->width = width;
1568 c->height = height;
1569 c->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_PSNR;
1570 c->flags |= s->c.avctx->flags & AV_CODEC_FLAG_QPEL;
1571 c->mb_decision = s->c.avctx->mb_decision;
1572 c->me_cmp = s->c.avctx->me_cmp;
1573 c->mb_cmp = s->c.avctx->mb_cmp;
1574 c->me_sub_cmp = s->c.avctx->me_sub_cmp;
1575 c->pix_fmt = AV_PIX_FMT_YUV420P;
1576 c->time_base = s->c.avctx->time_base;
1577 c->max_b_frames = m->max_b_frames;
1578
1579 ret = avcodec_open2(c, s->c.avctx->codec, NULL);
1580 if (ret < 0)
1581 goto fail;
1582
1583
1584 m->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1585 m->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1586
1587 out_size = encode_frame(c, m->tmp_frames[0], pkt);
1588 if (out_size < 0) {
1589 ret = out_size;
1590 goto fail;
1591 }
1592
1593 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1594
1595 for (int i = 0; i < m->max_b_frames + 1; i++) {
1596 int is_p = i % (j + 1) == j || i == m->max_b_frames;
1597
1598 m->tmp_frames[i + 1]->pict_type = is_p ?
1599 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1600 m->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1601
1602 out_size = encode_frame(c, m->tmp_frames[i + 1], pkt);
1603 if (out_size < 0) {
1604 ret = out_size;
1605 goto fail;
1606 }
1607
1608 rd += (out_size * (uint64_t)lambda2) >> (FF_LAMBDA_SHIFT - 3);
1609 }
1610
1611 /* get the delayed frames */
1612 out_size = encode_frame(c, NULL, pkt);
1613 if (out_size < 0) {
1614 ret = out_size;
1615 goto fail;
1616 }
1617 rd += (out_size * (uint64_t)lambda2) >> (FF_LAMBDA_SHIFT - 3);
1618
1619 rd += c->error[0] + c->error[1] + c->error[2];
1620
1621 if (rd < best_rd) {
1622 best_rd = rd;
1623 best_b_count = j;
1624 }
1625
1626 fail:
1627 avcodec_free_context(&c);
1628 av_packet_unref(pkt);
1629 if (ret < 0) {
1630 best_b_count = ret;
1631 break;
1632 }
1633 }
1634
1635 av_packet_free(&pkt);
1636
1637 return best_b_count;
1638 }
1639
1640 /**
1641 * Determines whether an input picture is discarded or not
1642 * and if not determines the length of the next chain of B frames
1643 * and moves these pictures (including the P frame) into
1644 * reordered_input_picture.
1645 * input_picture[0] is always NULL when exiting this function, even on error;
1646 * reordered_input_picture[0] is always NULL when exiting this function on error.
1647 */
1648 12370 static int set_bframe_chain_length(MPVMainEncContext *const m)
1649 {
1650 12370 MPVEncContext *const s = &m->s;
1651
1652 /* Either nothing to do or can't do anything */
1653
4/4
✓ Branch 0 taken 11036 times.
✓ Branch 1 taken 1334 times.
✓ Branch 2 taken 294 times.
✓ Branch 3 taken 10742 times.
12370 if (m->reordered_input_picture[0] || !m->input_picture[0])
1654 1628 return 0;
1655
1656 /* set next picture type & ordering */
1657
2/4
✓ Branch 0 taken 10742 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10742 times.
10742 if (m->frame_skip_threshold || m->frame_skip_factor) {
1658 if (m->picture_in_gop_number < m->gop_size &&
1659 s->c.next_pic.ptr &&
1660 skip_check(m, m->input_picture[0], s->c.next_pic.ptr)) {
1661 // FIXME check that the gop check above is +-1 correct
1662 av_refstruct_unref(&m->input_picture[0]);
1663
1664 ff_vbv_update(m, 0);
1665
1666 return 0;
1667 }
1668 }
1669
1670 10742 if (/* m->picture_in_gop_number >= m->gop_size || */
1671
4/4
✓ Branch 0 taken 10506 times.
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 1890 times.
✓ Branch 3 taken 8616 times.
10742 !s->c.next_pic.ptr || m->intra_only) {
1672 2126 m->reordered_input_picture[0] = m->input_picture[0];
1673 2126 m->input_picture[0] = NULL;
1674 2126 m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1675 2126 m->reordered_input_picture[0]->coded_picture_number =
1676 2126 m->coded_picture_number++;
1677 } else {
1678 8616 int b_frames = 0;
1679
1680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8616 times.
8616 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
1681 for (int i = 0; i < m->max_b_frames + 1; i++) {
1682 int pict_num = m->input_picture[0]->display_picture_number + i;
1683
1684 if (pict_num >= m->rc_context.num_entries)
1685 break;
1686 if (!m->input_picture[i]) {
1687 m->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1688 break;
1689 }
1690
1691 m->input_picture[i]->f->pict_type =
1692 m->rc_context.entry[pict_num].new_pict_type;
1693 }
1694 }
1695
1696
2/2
✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 121 times.
8616 if (m->b_frame_strategy == 0) {
1697 8495 b_frames = m->max_b_frames;
1698
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])
1699 79 b_frames--;
1700
1/2
✓ Branch 0 taken 121 times.
✗ Branch 1 not taken.
121 } else if (m->b_frame_strategy == 1) {
1701
2/2
✓ Branch 0 taken 242 times.
✓ Branch 1 taken 121 times.
363 for (int i = 1; i < m->max_b_frames + 1; i++) {
1702
2/2
✓ Branch 0 taken 235 times.
✓ Branch 1 taken 7 times.
242 if (m->input_picture[i] &&
1703
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 80 times.
235 m->input_picture[i]->b_frame_score == 0) {
1704 155 m->input_picture[i]->b_frame_score =
1705 155 get_intra_count(s,
1706 155 m->input_picture[i ]->f->data[0],
1707 155 m->input_picture[i - 1]->f->data[0],
1708 155 s->c.linesize) + 1;
1709 }
1710 }
1711 243 for (int i = 0;; i++) {
1712
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 37 times.
243 if (i >= m->max_b_frames + 1 ||
1713
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 4 times.
206 !m->input_picture[i] ||
1714 202 m->input_picture[i]->b_frame_score - 1 >
1715
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 122 times.
202 s->c.mb_num / m->b_sensitivity) {
1716 121 b_frames = FFMAX(0, i - 1);
1717 121 break;
1718 }
1719 }
1720
1721 /* reset scores */
1722
2/2
✓ Branch 0 taken 196 times.
✓ Branch 1 taken 121 times.
317 for (int i = 0; i < b_frames + 1; i++)
1723 196 m->input_picture[i]->b_frame_score = 0;
1724 } else if (m->b_frame_strategy == 2) {
1725 b_frames = estimate_best_b_count(m);
1726 if (b_frames < 0) {
1727 av_refstruct_unref(&m->input_picture[0]);
1728 return b_frames;
1729 }
1730 }
1731
1732
2/2
✓ Branch 0 taken 2030 times.
✓ Branch 1 taken 6586 times.
8616 if (s->c.codec_id == AV_CODEC_ID_MPEG4)
1733
2/2
✓ Branch 0 taken 295 times.
✓ Branch 1 taken 1735 times.
2030 while (b_frames &&
1734 295 m->input_picture[b_frames]->f->pts * s->c.avctx->time_base.num -
1735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295 times.
295 s->c.last_non_b_time > UINT16_MAX)
1736 b_frames--;
1737
1738
2/2
✓ Branch 0 taken 1334 times.
✓ Branch 1 taken 8616 times.
9950 for (int i = b_frames - 1; i >= 0; i--) {
1739 1334 int type = m->input_picture[i]->f->pict_type;
1740
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1334 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1334 if (type && type != AV_PICTURE_TYPE_B)
1741 b_frames = i;
1742 }
1743
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8616 times.
8616 if (m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1744 b_frames == m->max_b_frames) {
1745 av_log(s->c.avctx, AV_LOG_ERROR,
1746 "warning, too many B-frames in a row\n");
1747 }
1748
1749
2/2
✓ Branch 0 taken 598 times.
✓ Branch 1 taken 8018 times.
8616 if (m->picture_in_gop_number + b_frames >= m->gop_size) {
1750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 598 times.
598 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1751 m->gop_size > m->picture_in_gop_number) {
1752 b_frames = m->gop_size - m->picture_in_gop_number - 1;
1753 } else {
1754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 598 times.
598 if (s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
1755 b_frames = 0;
1756 598 m->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1757 }
1758 }
1759
1760
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8616 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8616 if ((s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
1761 m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1762 b_frames--;
1763
1764 8616 m->reordered_input_picture[0] = m->input_picture[b_frames];
1765 8616 m->input_picture[b_frames] = NULL;
1766
2/2
✓ Branch 0 taken 8002 times.
✓ Branch 1 taken 614 times.
8616 if (m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1767 8002 m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1768 8616 m->reordered_input_picture[0]->coded_picture_number =
1769 8616 m->coded_picture_number++;
1770
2/2
✓ Branch 0 taken 1334 times.
✓ Branch 1 taken 8616 times.
9950 for (int i = 0; i < b_frames; i++) {
1771 1334 m->reordered_input_picture[i + 1] = m->input_picture[i];
1772 1334 m->input_picture[i] = NULL;
1773 1334 m->reordered_input_picture[i + 1]->f->pict_type =
1774 AV_PICTURE_TYPE_B;
1775 1334 m->reordered_input_picture[i + 1]->coded_picture_number =
1776 1334 m->coded_picture_number++;
1777 }
1778 }
1779
1780 10742 return 0;
1781 }
1782
1783 12370 static int select_input_picture(MPVMainEncContext *const m)
1784 {
1785 12370 MPVEncContext *const s = &m->s;
1786 int ret;
1787
1788 av_assert1(!m->reordered_input_picture[0]);
1789
1790
2/2
✓ Branch 0 taken 197920 times.
✓ Branch 1 taken 12370 times.
210290 for (int i = 1; i <= MPVENC_MAX_B_FRAMES; i++)
1791 197920 m->reordered_input_picture[i - 1] = m->reordered_input_picture[i];
1792 12370 m->reordered_input_picture[MPVENC_MAX_B_FRAMES] = NULL;
1793
1794 12370 ret = set_bframe_chain_length(m);
1795 av_assert1(!m->input_picture[0]);
1796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12370 times.
12370 if (ret < 0)
1797 return ret;
1798
1799 12370 av_frame_unref(s->new_pic);
1800
1801
2/2
✓ Branch 0 taken 12076 times.
✓ Branch 1 taken 294 times.
12370 if (m->reordered_input_picture[0]) {
1802 12076 m->reordered_input_picture[0]->reference =
1803 12076 m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_B;
1804
1805
4/4
✓ Branch 0 taken 11402 times.
✓ Branch 1 taken 674 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 11352 times.
12076 if (m->reordered_input_picture[0]->shared || s->c.avctx->rc_buffer_size) {
1806 // input is a shared pix, so we can't modify it -> allocate a new
1807 // one & ensure that the shared one is reusable
1808 724 av_frame_move_ref(s->new_pic, m->reordered_input_picture[0]->f);
1809
1810 724 ret = prepare_picture(s, m->reordered_input_picture[0]->f, s->new_pic);
1811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 724 times.
724 if (ret < 0)
1812 goto fail;
1813 } else {
1814 // input is not a shared pix -> reuse buffer for current_pix
1815 11352 ret = av_frame_ref(s->new_pic, m->reordered_input_picture[0]->f);
1816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11352 times.
11352 if (ret < 0)
1817 goto fail;
1818
2/2
✓ Branch 0 taken 34056 times.
✓ Branch 1 taken 11352 times.
45408 for (int i = 0; i < MPV_MAX_PLANES; i++)
1819 34056 s->new_pic->data[i] += INPLACE_OFFSET;
1820 }
1821 12076 s->c.cur_pic.ptr = m->reordered_input_picture[0];
1822 12076 m->reordered_input_picture[0] = NULL;
1823 av_assert1(s->c.mb_width == s->c.buffer_pools.alloc_mb_width);
1824 av_assert1(s->c.mb_height == s->c.buffer_pools.alloc_mb_height);
1825 av_assert1(s->c.mb_stride == s->c.buffer_pools.alloc_mb_stride);
1826 12076 ret = ff_mpv_alloc_pic_accessories(s->c.avctx, &s->c.cur_pic,
1827 &s->c.sc, &s->c.buffer_pools, s->c.mb_height);
1828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12076 times.
12076 if (ret < 0) {
1829 ff_mpv_unref_picture(&s->c.cur_pic);
1830 return ret;
1831 }
1832 12076 s->picture_number = s->c.cur_pic.ptr->display_picture_number;
1833
1834 }
1835 12370 return 0;
1836 fail:
1837 av_refstruct_unref(&m->reordered_input_picture[0]);
1838 return ret;
1839 }
1840
1841 12201 static void frame_end(MPVMainEncContext *const m)
1842 {
1843 12201 MPVEncContext *const s = &m->s;
1844
1845
2/2
✓ Branch 0 taken 4055 times.
✓ Branch 1 taken 8146 times.
12201 if (s->me.unrestricted_mv &&
1846
2/2
✓ Branch 0 taken 3467 times.
✓ Branch 1 taken 588 times.
4055 s->c.cur_pic.reference &&
1847
2/2
✓ Branch 0 taken 3465 times.
✓ Branch 1 taken 2 times.
3467 !m->intra_only) {
1848 3465 int hshift = s->c.chroma_x_shift;
1849 3465 int vshift = s->c.chroma_y_shift;
1850 3465 s->mpvencdsp.draw_edges(s->c.cur_pic.data[0],
1851 s->c.cur_pic.linesize[0],
1852 s->c.h_edge_pos, s->c.v_edge_pos,
1853 EDGE_WIDTH, EDGE_WIDTH,
1854 EDGE_TOP | EDGE_BOTTOM);
1855 3465 s->mpvencdsp.draw_edges(s->c.cur_pic.data[1],
1856 s->c.cur_pic.linesize[1],
1857 3465 s->c.h_edge_pos >> hshift,
1858 3465 s->c.v_edge_pos >> vshift,
1859 EDGE_WIDTH >> hshift,
1860 EDGE_WIDTH >> vshift,
1861 EDGE_TOP | EDGE_BOTTOM);
1862 3465 s->mpvencdsp.draw_edges(s->c.cur_pic.data[2],
1863 s->c.cur_pic.linesize[2],
1864 3465 s->c.h_edge_pos >> hshift,
1865 3465 s->c.v_edge_pos >> vshift,
1866 EDGE_WIDTH >> hshift,
1867 EDGE_WIDTH >> vshift,
1868 EDGE_TOP | EDGE_BOTTOM);
1869 }
1870
1871 12201 m->last_pict_type = s->c.pict_type;
1872 12201 m->last_lambda_for[s->c.pict_type] = s->c.cur_pic.ptr->f->quality;
1873
2/2
✓ Branch 0 taken 10867 times.
✓ Branch 1 taken 1334 times.
12201 if (s->c.pict_type != AV_PICTURE_TYPE_B)
1874 10867 m->last_non_b_pict_type = s->c.pict_type;
1875 12201 }
1876
1877 350 static void update_noise_reduction(MPVMainEncContext *const m)
1878 {
1879 350 MPVEncContext *const s = &m->s;
1880 int intra, i;
1881
1882
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 350 times.
1050 for (intra = 0; intra < 2; intra++) {
1883
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 683 times.
700 if (s->dct_count[intra] > (1 << 16)) {
1884
2/2
✓ Branch 0 taken 1088 times.
✓ Branch 1 taken 17 times.
1105 for (i = 0; i < 64; i++) {
1885 1088 s->dct_error_sum[intra][i] >>= 1;
1886 }
1887 17 s->dct_count[intra] >>= 1;
1888 }
1889
1890
2/2
✓ Branch 0 taken 44800 times.
✓ Branch 1 taken 700 times.
45500 for (i = 0; i < 64; i++) {
1891 44800 s->dct_offset[intra][i] = (m->noise_reduction *
1892 44800 s->dct_count[intra] +
1893 44800 s->dct_error_sum[intra][i] / 2) /
1894 44800 (s->dct_error_sum[intra][i] + 1);
1895 }
1896 }
1897 350 }
1898
1899 12076 static void frame_start(MPVMainEncContext *const m)
1900 {
1901 12076 MPVEncContext *const s = &m->s;
1902
1903 12076 s->c.cur_pic.ptr->f->pict_type = s->c.pict_type;
1904
1905
2/2
✓ Branch 0 taken 10742 times.
✓ Branch 1 taken 1334 times.
12076 if (s->c.pict_type != AV_PICTURE_TYPE_B) {
1906 10742 ff_mpv_replace_picture(&s->c.last_pic, &s->c.next_pic);
1907 10742 ff_mpv_replace_picture(&s->c.next_pic, &s->c.cur_pic);
1908 }
1909
1910 av_assert2(!!m->noise_reduction == !!s->dct_error_sum);
1911
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 11726 times.
12076 if (s->dct_error_sum) {
1912 350 update_noise_reduction(m);
1913 }
1914 12076 }
1915
1916 12370 int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1917 const AVFrame *pic_arg, int *got_packet)
1918 {
1919 12370 MPVMainEncContext *const m = avctx->priv_data;
1920 12370 MPVEncContext *const s = &m->s;
1921 int stuffing_count, ret;
1922 12370 int context_count = s->c.slice_context_count;
1923
1924 12370 ff_mpv_unref_picture(&s->c.cur_pic);
1925
1926 12370 m->vbv_ignore_qmax = 0;
1927
1928 12370 m->picture_in_gop_number++;
1929
1930 12370 ret = load_input_picture(m, pic_arg);
1931
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12370 times.
12370 if (ret < 0)
1932 return ret;
1933
1934 12370 ret = select_input_picture(m);
1935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12370 times.
12370 if (ret < 0)
1936 return ret;
1937
1938 /* output? */
1939
2/2
✓ Branch 0 taken 12076 times.
✓ Branch 1 taken 294 times.
12370 if (s->new_pic->data[0]) {
1940
4/4
✓ Branch 0 taken 11049 times.
✓ Branch 1 taken 1027 times.
✓ Branch 2 taken 10649 times.
✓ Branch 3 taken 400 times.
12076 int growing_buffer = context_count == 1 && !s->data_partitioning;
1941 24152 size_t pkt_size = 10000 + s->c.mb_width * s->c.mb_height *
1942
2/2
✓ Branch 0 taken 10649 times.
✓ Branch 1 taken 1427 times.
12076 (growing_buffer ? 64 : (MAX_MB_BYTES + 100));
1943
2/2
✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 10837 times.
12076 if (CONFIG_MJPEG_ENCODER && avctx->codec_id == AV_CODEC_ID_MJPEG) {
1944 1239 ret = ff_mjpeg_add_icc_profile_size(avctx, s->new_pic, &pkt_size);
1945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 if (ret < 0)
1946 return ret;
1947 }
1948
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12076 times.
12076 if ((ret = ff_alloc_packet(avctx, pkt, pkt_size)) < 0)
1949 return ret;
1950 12076 pkt->size = avctx->internal->byte_buffer_size - AV_INPUT_BUFFER_PADDING_SIZE;
1951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12076 times.
12076 if (s->mb_info) {
1952 s->mb_info_ptr = av_packet_new_side_data(pkt,
1953 AV_PKT_DATA_H263_MB_INFO,
1954 s->c.mb_width*s->c.mb_height*12);
1955 if (!s->mb_info_ptr)
1956 return AVERROR(ENOMEM);
1957 s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1958 }
1959
1960 12076 s->c.pict_type = s->new_pic->pict_type;
1961 12076 frame_start(m);
1962 12201 vbv_retry:
1963 12201 ret = encode_picture(m, pkt);
1964
2/2
✓ Branch 0 taken 10774 times.
✓ Branch 1 taken 1427 times.
12201 if (growing_buffer) {
1965
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10774 times.
10774 av_assert0(s->pb.buf == avctx->internal->byte_buffer);
1966 10774 pkt->data = s->pb.buf;
1967 10774 pkt->size = avctx->internal->byte_buffer_size;
1968 }
1969
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12201 times.
12201 if (ret < 0)
1970 return -1;
1971
1972 12201 frame_end(m);
1973
1974
2/2
✓ Branch 0 taken 1439 times.
✓ Branch 1 taken 10762 times.
12201 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) && s->c.out_format == FMT_MJPEG)
1975 1439 ff_mjpeg_encode_picture_trailer(&s->pb, m->header_bits);
1976
1977
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 12026 times.
12201 if (avctx->rc_buffer_size) {
1978 175 RateControlContext *rcc = &m->rc_context;
1979
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);
1980
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);
1981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
1982
1983
2/2
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 50 times.
175 if (put_bits_count(&s->pb) > max_size &&
1984
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 s->lambda < m->lmax) {
1985 125 m->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
1986 (s->c.qscale + 1) / s->c.qscale);
1987
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (s->adaptive_quant) {
1988 for (int i = 0; i < s->c.mb_height * s->c.mb_stride; i++)
1989 s->lambda_table[i] =
1990 FFMAX(s->lambda_table[i] + min_step,
1991 s->lambda_table[i] * (s->c.qscale + 1) /
1992 s->c.qscale);
1993 }
1994 125 s->c.mb_skipped = 0; // done in frame_start()
1995 // done in encode_picture() so we must undo it
1996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
1997 s->c.no_rounding ^= s->flipflop_rounding;
1998 }
1999
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if (s->c.pict_type != AV_PICTURE_TYPE_B) {
2000 125 s->c.time_base = s->c.last_time_base;
2001 125 s->c.last_non_b_time = s->c.time - s->c.pp_time;
2002 }
2003 125 m->vbv_ignore_qmax = 1;
2004 125 av_log(avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
2005 125 goto vbv_retry;
2006 }
2007
2008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 av_assert0(avctx->rc_max_rate);
2009 }
2010
2011
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12076 times.
12076 if (avctx->flags & AV_CODEC_FLAG_PASS1)
2012 ff_write_pass1_stats(m);
2013
2014
2/2
✓ Branch 0 taken 36228 times.
✓ Branch 1 taken 12076 times.
48304 for (int i = 0; i < MPV_MAX_PLANES; i++)
2015 36228 avctx->error[i] += s->encoding_error[i];
2016 12076 ff_encode_add_stats_side_data(pkt, s->c.cur_pic.ptr->f->quality,
2017 12076 s->encoding_error,
2018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12076 times.
12076 (avctx->flags&AV_CODEC_FLAG_PSNR) ? MPV_MAX_PLANES : 0,
2019 s->c.pict_type);
2020
2021 12076 if (avctx->flags & AV_CODEC_FLAG_PASS1)
2022 assert(put_bits_count(&s->pb) == m->header_bits + s->mv_bits +
2023 s->misc_bits + s->i_tex_bits +
2024 s->p_tex_bits);
2025 12076 flush_put_bits(&s->pb);
2026 12076 m->frame_bits = put_bits_count(&s->pb);
2027
2028 12076 stuffing_count = ff_vbv_update(m, m->frame_bits);
2029 12076 m->stuffing_bits = 8*stuffing_count;
2030
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 12026 times.
12076 if (stuffing_count) {
2031
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if (put_bytes_left(&s->pb, 0) < stuffing_count + 50) {
2032 av_log(avctx, AV_LOG_ERROR, "stuffing too large\n");
2033 return -1;
2034 }
2035
2036
1/3
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
50 switch (s->c.codec_id) {
2037 50 case AV_CODEC_ID_MPEG1VIDEO:
2038 case AV_CODEC_ID_MPEG2VIDEO:
2039
2/2
✓ Branch 0 taken 3648955 times.
✓ Branch 1 taken 50 times.
3649005 while (stuffing_count--) {
2040 3648955 put_bits(&s->pb, 8, 0);
2041 }
2042 50 break;
2043 case AV_CODEC_ID_MPEG4:
2044 put_bits(&s->pb, 16, 0);
2045 put_bits(&s->pb, 16, 0x1C3);
2046 stuffing_count -= 4;
2047 while (stuffing_count--) {
2048 put_bits(&s->pb, 8, 0xFF);
2049 }
2050 break;
2051 default:
2052 av_log(avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
2053 m->stuffing_bits = 0;
2054 }
2055 50 flush_put_bits(&s->pb);
2056 50 m->frame_bits = put_bits_count(&s->pb);
2057 }
2058
2059 /* update MPEG-1/2 vbv_delay for CBR */
2060
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 12026 times.
12076 if (avctx->rc_max_rate &&
2061
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 avctx->rc_min_rate == avctx->rc_max_rate &&
2062
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 s->c.out_format == FMT_MPEG1 &&
2063 50 90000LL * (avctx->rc_buffer_size - 1) <=
2064
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
50 avctx->rc_max_rate * 0xFFFFLL) {
2065 AVCPBProperties *props;
2066 size_t props_size;
2067
2068 int vbv_delay, min_delay;
2069 50 double inbits = avctx->rc_max_rate *
2070 25 av_q2d(avctx->time_base);
2071 25 int minbits = m->frame_bits - 8 *
2072 25 (m->vbv_delay_pos - 1);
2073 25 double bits = m->rc_context.buffer_index + minbits - inbits;
2074 25 uint8_t *const vbv_delay_ptr = s->pb.buf + m->vbv_delay_pos;
2075
2076
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (bits < 0)
2077 av_log(avctx, AV_LOG_ERROR,
2078 "Internal error, negative bits\n");
2079
2080 av_assert1(s->c.repeat_first_field == 0);
2081
2082 25 vbv_delay = bits * 90000 / avctx->rc_max_rate;
2083 25 min_delay = (minbits * 90000LL + avctx->rc_max_rate - 1) /
2084 25 avctx->rc_max_rate;
2085
2086 25 vbv_delay = FFMAX(vbv_delay, min_delay);
2087
2088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 av_assert0(vbv_delay < 0xFFFF);
2089
2090 25 vbv_delay_ptr[0] &= 0xF8;
2091 25 vbv_delay_ptr[0] |= vbv_delay >> 13;
2092 25 vbv_delay_ptr[1] = vbv_delay >> 5;
2093 25 vbv_delay_ptr[2] &= 0x07;
2094 25 vbv_delay_ptr[2] |= vbv_delay << 3;
2095
2096 25 props = av_cpb_properties_alloc(&props_size);
2097
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!props)
2098 return AVERROR(ENOMEM);
2099 25 props->vbv_delay = vbv_delay * 300;
2100
2101 25 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
2102 (uint8_t*)props, props_size);
2103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0) {
2104 av_freep(&props);
2105 return ret;
2106 }
2107 }
2108 12076 m->total_bits += m->frame_bits;
2109
2110 12076 pkt->pts = s->c.cur_pic.ptr->f->pts;
2111 12076 pkt->duration = s->c.cur_pic.ptr->f->duration;
2112
4/4
✓ Branch 0 taken 6181 times.
✓ Branch 1 taken 5895 times.
✓ Branch 2 taken 4847 times.
✓ Branch 3 taken 1334 times.
12076 if (!s->c.low_delay && s->c.pict_type != AV_PICTURE_TYPE_B) {
2113
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 4750 times.
4847 if (!s->c.cur_pic.ptr->coded_picture_number)
2114 97 pkt->dts = pkt->pts - m->dts_delta;
2115 else
2116 4750 pkt->dts = m->reordered_pts;
2117 4847 m->reordered_pts = pkt->pts;
2118 } else
2119 7229 pkt->dts = pkt->pts;
2120
2121 // the no-delay case is handled in generic code
2122
2/2
✓ Branch 0 taken 7893 times.
✓ Branch 1 taken 4183 times.
12076 if (avctx->codec->capabilities & AV_CODEC_CAP_DELAY) {
2123 7893 ret = ff_encode_reordered_opaque(avctx, pkt, s->c.cur_pic.ptr->f);
2124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7893 times.
7893 if (ret < 0)
2125 return ret;
2126 }
2127
2128
2/2
✓ Branch 0 taken 2771 times.
✓ Branch 1 taken 9305 times.
12076 if (s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)
2129 2771 pkt->flags |= AV_PKT_FLAG_KEY;
2130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12076 times.
12076 if (s->mb_info)
2131 av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
2132 } else {
2133 294 m->frame_bits = 0;
2134 }
2135
2136 12370 ff_mpv_unref_picture(&s->c.cur_pic);
2137
2138 av_assert1((m->frame_bits & 7) == 0);
2139
2140 12370 pkt->size = m->frame_bits / 8;
2141 12370 *got_packet = !!pkt->size;
2142 12370 return 0;
2143 }
2144
2145 static inline void dct_single_coeff_elimination(MPVEncContext *const s,
2146 int n, int threshold)
2147 {
2148 static const char tab[64] = {
2149 3, 2, 2, 1, 1, 1, 1, 1,
2150 1, 1, 1, 1, 1, 1, 1, 1,
2151 1, 1, 1, 1, 1, 1, 1, 1,
2152 0, 0, 0, 0, 0, 0, 0, 0,
2153 0, 0, 0, 0, 0, 0, 0, 0,
2154 0, 0, 0, 0, 0, 0, 0, 0,
2155 0, 0, 0, 0, 0, 0, 0, 0,
2156 0, 0, 0, 0, 0, 0, 0, 0
2157 };
2158 int score = 0;
2159 int run = 0;
2160 int i;
2161 int16_t *block = s->block[n];
2162 const int last_index = s->c.block_last_index[n];
2163 int skip_dc;
2164
2165 if (threshold < 0) {
2166 skip_dc = 0;
2167 threshold = -threshold;
2168 } else
2169 skip_dc = 1;
2170
2171 /* Are all we could set to zero already zero? */
2172 if (last_index <= skip_dc - 1)
2173 return;
2174
2175 for (i = 0; i <= last_index; i++) {
2176 const int j = s->c.intra_scantable.permutated[i];
2177 const int level = FFABS(block[j]);
2178 if (level == 1) {
2179 if (skip_dc && i == 0)
2180 continue;
2181 score += tab[run];
2182 run = 0;
2183 } else if (level > 1) {
2184 return;
2185 } else {
2186 run++;
2187 }
2188 }
2189 if (score >= threshold)
2190 return;
2191 for (i = skip_dc; i <= last_index; i++) {
2192 const int j = s->c.intra_scantable.permutated[i];
2193 block[j] = 0;
2194 }
2195 if (block[0])
2196 s->c.block_last_index[n] = 0;
2197 else
2198 s->c.block_last_index[n] = -1;
2199 }
2200
2201 static inline void clip_coeffs(const MPVEncContext *const s, int16_t block[],
2202 int last_index)
2203 {
2204 int i;
2205 const int maxlevel = s->max_qcoeff;
2206 const int minlevel = s->min_qcoeff;
2207 int overflow = 0;
2208
2209 if (s->c.mb_intra) {
2210 i = 1; // skip clipping of intra dc
2211 } else
2212 i = 0;
2213
2214 for (; i <= last_index; i++) {
2215 const int j = s->c.intra_scantable.permutated[i];
2216 int level = block[j];
2217
2218 if (level > maxlevel) {
2219 level = maxlevel;
2220 overflow++;
2221 } else if (level < minlevel) {
2222 level = minlevel;
2223 overflow++;
2224 }
2225
2226 block[j] = level;
2227 }
2228
2229 if (overflow && s->c.avctx->mb_decision == FF_MB_DECISION_SIMPLE)
2230 av_log(s->c.avctx, AV_LOG_INFO,
2231 "warning, clipping %d dct coefficients to %d..%d\n",
2232 overflow, minlevel, maxlevel);
2233 }
2234
2235 static void get_visual_weight(int16_t *weight, const uint8_t *ptr, int stride)
2236 {
2237 int x, y;
2238 // FIXME optimize
2239 for (y = 0; y < 8; y++) {
2240 for (x = 0; x < 8; x++) {
2241 int x2, y2;
2242 int sum = 0;
2243 int sqr = 0;
2244 int count = 0;
2245
2246 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
2247 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
2248 int v = ptr[x2 + y2 * stride];
2249 sum += v;
2250 sqr += v * v;
2251 count++;
2252 }
2253 }
2254 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
2255 }
2256 }
2257 }
2258
2259 5494888 static av_always_inline void encode_mb_internal(MPVEncContext *const s,
2260 int motion_x, int motion_y,
2261 int mb_block_height,
2262 int mb_block_width,
2263 int mb_block_count,
2264 int chroma_x_shift,
2265 int chroma_y_shift,
2266 int chroma_format)
2267 {
2268 /* Interlaced DCT is only possible with MPEG-2 and MPEG-4
2269 * and neither of these encoders currently supports 444. */
2270 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2271 (s)->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2272 DECLARE_ALIGNED(16, int16_t, weight)[12][64];
2273 int16_t orig[12][64];
2274 5494888 const int mb_x = s->c.mb_x;
2275 5494888 const int mb_y = s->c.mb_y;
2276 int i;
2277 int skip_dct[12];
2278 5494888 int dct_offset = s->c.linesize * 8; // default for progressive frames
2279 5494888 int uv_dct_offset = s->c.uvlinesize * 8;
2280 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2281 ptrdiff_t wrap_y, wrap_c;
2282
2283
2/2
✓ Branch 0 taken 35209622 times.
✓ Branch 1 taken 5494888 times.
40704510 for (i = 0; i < mb_block_count; i++)
2284 35209622 skip_dct[i] = s->skipdct;
2285
2286
2/2
✓ Branch 0 taken 1251271 times.
✓ Branch 1 taken 4243617 times.
5494888 if (s->adaptive_quant) {
2287 1251271 const int last_qp = s->c.qscale;
2288 1251271 const int mb_xy = mb_x + mb_y * s->c.mb_stride;
2289
2290 1251271 s->lambda = s->lambda_table[mb_xy];
2291 1251271 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
2292 FF_LAMBDA_SHIFT;
2293
2294
2/2
✓ Branch 0 taken 212086 times.
✓ Branch 1 taken 1039185 times.
1251271 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
2295 212086 s->dquant = s->c.cur_pic.qscale_table[mb_xy] - last_qp;
2296
2297
1/2
✓ Branch 0 taken 212086 times.
✗ Branch 1 not taken.
212086 if (s->c.out_format == FMT_H263) {
2298 212086 s->dquant = av_clip(s->dquant, -2, 2);
2299
2300
1/2
✓ Branch 0 taken 212086 times.
✗ Branch 1 not taken.
212086 if (s->c.codec_id == AV_CODEC_ID_MPEG4) {
2301
2/2
✓ Branch 0 taken 199139 times.
✓ Branch 1 taken 12947 times.
212086 if (!s->c.mb_intra) {
2302
2/2
✓ Branch 0 taken 121052 times.
✓ Branch 1 taken 78087 times.
199139 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
2303
4/4
✓ Branch 0 taken 76098 times.
✓ Branch 1 taken 44954 times.
✓ Branch 2 taken 25443 times.
✓ Branch 3 taken 50655 times.
121052 if (s->dquant & 1 || s->c.mv_dir & MV_DIRECT)
2304 70397 s->dquant = 0;
2305 }
2306
2/2
✓ Branch 0 taken 38700 times.
✓ Branch 1 taken 160439 times.
199139 if (s->c.mv_type == MV_TYPE_8X8)
2307 38700 s->dquant = 0;
2308 }
2309 }
2310 }
2311 }
2312 1251271 ff_set_qscale(&s->c, last_qp + s->dquant);
2313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4243617 times.
4243617 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
2314 ff_set_qscale(&s->c, s->c.qscale + s->dquant);
2315
2316 5494888 wrap_y = s->c.linesize;
2317 5494888 wrap_c = s->c.uvlinesize;
2318 5494888 ptr_y = s->new_pic->data[0] +
2319 5494888 (mb_y * 16 * wrap_y) + mb_x * 16;
2320 5494888 ptr_cb = s->new_pic->data[1] +
2321 5494888 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2322 5494888 ptr_cr = s->new_pic->data[2] +
2323 5494888 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2324
2325
4/4
✓ Branch 0 taken 5483398 times.
✓ Branch 1 taken 11490 times.
✓ Branch 2 taken 34636 times.
✓ Branch 3 taken 5448762 times.
5494888 if ((mb_x * 16 + 16 > s->c.width || mb_y * 16 + 16 > s->c.height) &&
2326
2/2
✓ Branch 0 taken 45876 times.
✓ Branch 1 taken 250 times.
46126 s->c.codec_id != AV_CODEC_ID_AMV) {
2327 45876 uint8_t *ebuf = s->c.sc.edge_emu_buffer + 38 * wrap_y;
2328 45876 int cw = (s->c.width + chroma_x_shift) >> chroma_x_shift;
2329 45876 int ch = (s->c.height + chroma_y_shift) >> chroma_y_shift;
2330 45876 s->c.vdsp.emulated_edge_mc(ebuf, ptr_y,
2331 wrap_y, wrap_y,
2332 16, 16, mb_x * 16, mb_y * 16,
2333 s->c.width, s->c.height);
2334 45876 ptr_y = ebuf;
2335 45876 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2336 wrap_c, wrap_c,
2337 mb_block_width, mb_block_height,
2338 mb_x * mb_block_width, mb_y * mb_block_height,
2339 cw, ch);
2340 45876 ptr_cb = ebuf + 16 * wrap_y;
2341 45876 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2342 wrap_c, wrap_c,
2343 mb_block_width, mb_block_height,
2344 mb_x * mb_block_width, mb_y * mb_block_height,
2345 cw, ch);
2346 45876 ptr_cr = ebuf + 16 * wrap_y + 16;
2347 }
2348
2349
2/2
✓ Branch 0 taken 1440574 times.
✓ Branch 1 taken 4054314 times.
5494888 if (s->c.mb_intra) {
2350
6/6
✓ Branch 0 taken 533825 times.
✓ Branch 1 taken 906749 times.
✓ Branch 2 taken 414511 times.
✓ Branch 3 taken 119314 times.
✓ Branch 4 taken 317347 times.
✓ Branch 5 taken 1003913 times.
1440574 if (INTERLACED_DCT(s)) {
2351 int progressive_score, interlaced_score;
2352
2353 317347 s->c.interlaced_dct = 0;
2354 317347 progressive_score = s->ildct_cmp[1](s, ptr_y, NULL, wrap_y, 8) +
2355 317347 s->ildct_cmp[1](s, ptr_y + wrap_y * 8,
2356 NULL, wrap_y, 8) - 400;
2357
2358
2/2
✓ Branch 0 taken 298252 times.
✓ Branch 1 taken 19095 times.
317347 if (progressive_score > 0) {
2359 298252 interlaced_score = s->ildct_cmp[1](s, ptr_y,
2360 NULL, wrap_y * 2, 8) +
2361 298252 s->ildct_cmp[1](s, ptr_y + wrap_y,
2362 NULL, wrap_y * 2, 8);
2363
2/2
✓ Branch 0 taken 377 times.
✓ Branch 1 taken 297875 times.
298252 if (progressive_score > interlaced_score) {
2364 377 s->c.interlaced_dct = 1;
2365
2366 377 dct_offset = wrap_y;
2367 377 uv_dct_offset = wrap_c;
2368 377 wrap_y <<= 1;
2369
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 ||
2370 chroma_format == CHROMA_444)
2371 192 wrap_c <<= 1;
2372 }
2373 }
2374 }
2375
2376 1440574 s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
2377 1440574 s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
2378 1440574 s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
2379 1440574 s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
2380
2381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1440574 times.
1440574 if (s->c.avctx->flags & AV_CODEC_FLAG_GRAY) {
2382 skip_dct[4] = 1;
2383 skip_dct[5] = 1;
2384 } else {
2385 1440574 s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2386 1440574 s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2387
2/2
✓ Branch 0 taken 414511 times.
✓ Branch 1 taken 1026063 times.
1440574 if (chroma_format == CHROMA_422) {
2388 414511 s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2389 414511 s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2390
2/2
✓ Branch 0 taken 119314 times.
✓ Branch 1 taken 906749 times.
1026063 } else if (chroma_format == CHROMA_444) {
2391 119314 s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
2392 119314 s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
2393 119314 s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2394 119314 s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2395 119314 s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2396 119314 s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2397 }
2398 }
2399 } else {
2400 op_pixels_func (*op_pix)[4];
2401 qpel_mc_func (*op_qpix)[16];
2402 uint8_t *dest_y, *dest_cb, *dest_cr;
2403
2404 4054314 dest_y = s->c.dest[0];
2405 4054314 dest_cb = s->c.dest[1];
2406 4054314 dest_cr = s->c.dest[2];
2407
2408
4/4
✓ Branch 0 taken 956650 times.
✓ Branch 1 taken 3097664 times.
✓ Branch 2 taken 347460 times.
✓ Branch 3 taken 609190 times.
4054314 if ((!s->c.no_rounding) || s->c.pict_type == AV_PICTURE_TYPE_B) {
2409 3445124 op_pix = s->c.hdsp.put_pixels_tab;
2410 3445124 op_qpix = s->c.qdsp.put_qpel_pixels_tab;
2411 } else {
2412 609190 op_pix = s->c.hdsp.put_no_rnd_pixels_tab;
2413 609190 op_qpix = s->c.qdsp.put_no_rnd_qpel_pixels_tab;
2414 }
2415
2416
2/2
✓ Branch 0 taken 3716607 times.
✓ Branch 1 taken 337707 times.
4054314 if (s->c.mv_dir & MV_DIR_FORWARD) {
2417 3716607 ff_mpv_motion(&s->c, dest_y, dest_cb, dest_cr, 0,
2418 3716607 s->c.last_pic.data,
2419 op_pix, op_qpix);
2420 3716607 op_pix = s->c.hdsp.avg_pixels_tab;
2421 3716607 op_qpix = s->c.qdsp.avg_qpel_pixels_tab;
2422 }
2423
2/2
✓ Branch 0 taken 982573 times.
✓ Branch 1 taken 3071741 times.
4054314 if (s->c.mv_dir & MV_DIR_BACKWARD) {
2424 982573 ff_mpv_motion(&s->c, dest_y, dest_cb, dest_cr, 1,
2425 982573 s->c.next_pic.data,
2426 op_pix, op_qpix);
2427 }
2428
2429
5/6
✓ Branch 0 taken 347694 times.
✓ Branch 1 taken 3706620 times.
✓ Branch 2 taken 347694 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 505608 times.
✓ Branch 5 taken 3548706 times.
4054314 if (INTERLACED_DCT(s)) {
2430 int progressive_score, interlaced_score;
2431
2432 505608 s->c.interlaced_dct = 0;
2433 505608 progressive_score = s->ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2434 505608 s->ildct_cmp[0](s, dest_y + wrap_y * 8,
2435 505608 ptr_y + wrap_y * 8,
2436 wrap_y, 8) - 400;
2437
2438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505608 times.
505608 if (s->c.avctx->ildct_cmp == FF_CMP_VSSE)
2439 progressive_score -= 400;
2440
2441
2/2
✓ Branch 0 taken 445649 times.
✓ Branch 1 taken 59959 times.
505608 if (progressive_score > 0) {
2442 445649 interlaced_score = s->ildct_cmp[0](s, dest_y, ptr_y,
2443 wrap_y * 2, 8) +
2444 445649 s->ildct_cmp[0](s, dest_y + wrap_y,
2445 ptr_y + wrap_y,
2446 wrap_y * 2, 8);
2447
2448
2/2
✓ Branch 0 taken 12594 times.
✓ Branch 1 taken 433055 times.
445649 if (progressive_score > interlaced_score) {
2449 12594 s->c.interlaced_dct = 1;
2450
2451 12594 dct_offset = wrap_y;
2452 12594 uv_dct_offset = wrap_c;
2453 12594 wrap_y <<= 1;
2454
2/2
✓ Branch 0 taken 9048 times.
✓ Branch 1 taken 3546 times.
12594 if (chroma_format == CHROMA_422)
2455 9048 wrap_c <<= 1;
2456 }
2457 }
2458 }
2459
2460 4054314 s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2461 4054314 s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2462 4054314 s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2463 4054314 dest_y + dct_offset, wrap_y);
2464 4054314 s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2465 4054314 dest_y + dct_offset + 8, wrap_y);
2466
2467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4054314 times.
4054314 if (s->c.avctx->flags & AV_CODEC_FLAG_GRAY) {
2468 skip_dct[4] = 1;
2469 skip_dct[5] = 1;
2470 } else {
2471 4054314 s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2472 4054314 s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2473
2/2
✓ Branch 0 taken 347694 times.
✓ Branch 1 taken 3706620 times.
4054314 if (!chroma_y_shift) { /* 422 */
2474 347694 s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
2475 347694 dest_cb + uv_dct_offset, wrap_c);
2476 347694 s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
2477 347694 dest_cr + uv_dct_offset, wrap_c);
2478 }
2479 }
2480 /* pre quantization */
2481
2/2
✓ Branch 0 taken 3443445 times.
✓ Branch 1 taken 610869 times.
4054314 if (s->mc_mb_var[s->c.mb_stride * mb_y + mb_x] < 2 * s->c.qscale * s->c.qscale) {
2482 // FIXME optimize
2483
2/2
✓ Branch 1 taken 1366678 times.
✓ Branch 2 taken 2076767 times.
3443445 if (s->sad_cmp[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->c.qscale)
2484 1366678 skip_dct[0] = 1;
2485
2/2
✓ Branch 1 taken 1379262 times.
✓ Branch 2 taken 2064183 times.
3443445 if (s->sad_cmp[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->c.qscale)
2486 1379262 skip_dct[1] = 1;
2487 3443445 if (s->sad_cmp[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2488
2/2
✓ Branch 0 taken 1356806 times.
✓ Branch 1 taken 2086639 times.
3443445 wrap_y, 8) < 20 * s->c.qscale)
2489 1356806 skip_dct[2] = 1;
2490 3443445 if (s->sad_cmp[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2491
2/2
✓ Branch 0 taken 1322522 times.
✓ Branch 1 taken 2120923 times.
3443445 wrap_y, 8) < 20 * s->c.qscale)
2492 1322522 skip_dct[3] = 1;
2493
2/2
✓ Branch 1 taken 1683160 times.
✓ Branch 2 taken 1760285 times.
3443445 if (s->sad_cmp[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->c.qscale)
2494 1683160 skip_dct[4] = 1;
2495
2/2
✓ Branch 1 taken 1653667 times.
✓ Branch 2 taken 1789778 times.
3443445 if (s->sad_cmp[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->c.qscale)
2496 1653667 skip_dct[5] = 1;
2497
2/2
✓ Branch 0 taken 287272 times.
✓ Branch 1 taken 3156173 times.
3443445 if (!chroma_y_shift) { /* 422 */
2498 574544 if (s->sad_cmp[1](NULL, ptr_cb + uv_dct_offset,
2499 287272 dest_cb + uv_dct_offset,
2500
2/2
✓ Branch 0 taken 155766 times.
✓ Branch 1 taken 131506 times.
287272 wrap_c, 8) < 20 * s->c.qscale)
2501 155766 skip_dct[6] = 1;
2502 574544 if (s->sad_cmp[1](NULL, ptr_cr + uv_dct_offset,
2503 287272 dest_cr + uv_dct_offset,
2504
2/2
✓ Branch 0 taken 149390 times.
✓ Branch 1 taken 137882 times.
287272 wrap_c, 8) < 20 * s->c.qscale)
2505 149390 skip_dct[7] = 1;
2506 }
2507 }
2508 }
2509
2510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5494888 times.
5494888 if (s->quantizer_noise_shaping) {
2511 if (!skip_dct[0])
2512 get_visual_weight(weight[0], ptr_y , wrap_y);
2513 if (!skip_dct[1])
2514 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2515 if (!skip_dct[2])
2516 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2517 if (!skip_dct[3])
2518 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2519 if (!skip_dct[4])
2520 get_visual_weight(weight[4], ptr_cb , wrap_c);
2521 if (!skip_dct[5])
2522 get_visual_weight(weight[5], ptr_cr , wrap_c);
2523 if (!chroma_y_shift) { /* 422 */
2524 if (!skip_dct[6])
2525 get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2526 wrap_c);
2527 if (!skip_dct[7])
2528 get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2529 wrap_c);
2530 }
2531 memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2532 }
2533
2534 /* DCT & quantize */
2535 av_assert2(s->c.out_format != FMT_MJPEG || s->c.qscale == 8);
2536 {
2537
2/2
✓ Branch 0 taken 35209622 times.
✓ Branch 1 taken 5494888 times.
40704510 for (i = 0; i < mb_block_count; i++) {
2538
2/2
✓ Branch 0 taken 26142371 times.
✓ Branch 1 taken 9067251 times.
35209622 if (!skip_dct[i]) {
2539 int overflow;
2540 26142371 s->c.block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->c.qscale, &overflow);
2541 // FIXME we could decide to change to quantizer instead of
2542 // clipping
2543 // JS: I don't think that would be a good idea it could lower
2544 // quality instead of improve it. Just INTRADC clipping
2545 // deserves changes in quantizer
2546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26142371 times.
26142371 if (overflow)
2547 clip_coeffs(s, s->block[i], s->c.block_last_index[i]);
2548 } else
2549 9067251 s->c.block_last_index[i] = -1;
2550 }
2551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5494888 times.
5494888 if (s->quantizer_noise_shaping) {
2552 for (i = 0; i < mb_block_count; i++) {
2553 if (!skip_dct[i]) {
2554 s->c.block_last_index[i] =
2555 dct_quantize_refine(s, s->block[i], weight[i],
2556 orig[i], i, s->c.qscale);
2557 }
2558 }
2559 }
2560
2561
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5494888 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5494888 if (s->luma_elim_threshold && !s->c.mb_intra)
2562 for (i = 0; i < 4; i++)
2563 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2564
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5494888 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5494888 if (s->chroma_elim_threshold && !s->c.mb_intra)
2565 for (i = 4; i < mb_block_count; i++)
2566 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2567
2568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5494888 times.
5494888 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2569 for (i = 0; i < mb_block_count; i++) {
2570 if (s->c.block_last_index[i] == -1)
2571 s->coded_score[i] = INT_MAX / 256;
2572 }
2573 }
2574 }
2575
2576
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5494888 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5494888 if ((s->c.avctx->flags & AV_CODEC_FLAG_GRAY) && s->c.mb_intra) {
2577 s->c.block_last_index[4] =
2578 s->c.block_last_index[5] = 0;
2579 s->block[4][0] =
2580 s->block[5][0] = (1024 + s->c.c_dc_scale / 2) / s->c.c_dc_scale;
2581 if (!chroma_y_shift) { /* 422 / 444 */
2582 for (i=6; i<12; i++) {
2583 s->c.block_last_index[i] = 0;
2584 s->block[i][0] = s->block[4][0];
2585 }
2586 }
2587 }
2588
2589 // non c quantize code returns incorrect block_last_index FIXME
2590
3/4
✓ Branch 0 taken 60000 times.
✓ Branch 1 taken 5434888 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 60000 times.
5494888 if (s->c.alternate_scan && s->dct_quantize != dct_quantize_c) {
2591 for (i = 0; i < mb_block_count; i++) {
2592 int j;
2593 if (s->c.block_last_index[i] > 0) {
2594 for (j = 63; j > 0; j--) {
2595 if (s->block[i][s->c.intra_scantable.permutated[j]])
2596 break;
2597 }
2598 s->c.block_last_index[i] = j;
2599 }
2600 }
2601 }
2602
2603 5494888 s->encode_mb(s, s->block, motion_x, motion_y);
2604 5494888 }
2605
2606 5494888 static void encode_mb(MPVEncContext *const s, int motion_x, int motion_y)
2607 {
2608
2/2
✓ Branch 0 taken 4613369 times.
✓ Branch 1 taken 881519 times.
5494888 if (s->c.chroma_format == CHROMA_420)
2609 4613369 encode_mb_internal(s, motion_x, motion_y, 8, 8, 6, 1, 1, CHROMA_420);
2610
2/2
✓ Branch 0 taken 762205 times.
✓ Branch 1 taken 119314 times.
881519 else if (s->c.chroma_format == CHROMA_422)
2611 762205 encode_mb_internal(s, motion_x, motion_y, 16, 8, 8, 1, 0, CHROMA_422);
2612 else
2613 119314 encode_mb_internal(s, motion_x, motion_y, 16, 16, 12, 0, 0, CHROMA_444);
2614 5494888 }
2615
2616 typedef struct MBBackup {
2617 struct {
2618 int mv[2][4][2];
2619 int last_mv[2][2][2];
2620 int mv_type, mv_dir;
2621 int mb_intra, mb_skipped;
2622 int qscale;
2623 int block_last_index[8];
2624 int interlaced_dct;
2625 } c;
2626 int mb_skip_run;
2627 int last_dc[3];
2628 int mv_bits, i_tex_bits, p_tex_bits, i_count, misc_bits, last_bits;
2629 int dquant;
2630 int esc3_level_length;
2631 int16_t (*block)[64];
2632 PutBitContext pb, pb2, tex_pb;
2633 } MBBackup;
2634
2635 #define COPY_CONTEXT(BEFORE, AFTER, DST_TYPE, SRC_TYPE) \
2636 static inline void BEFORE ##_context_before_encode(DST_TYPE *const d, \
2637 const SRC_TYPE *const s) \
2638 { \
2639 /* FIXME is memcpy faster than a loop? */ \
2640 memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
2641 \
2642 /* MPEG-1 */ \
2643 d->mb_skip_run = s->mb_skip_run; \
2644 for (int i = 0; i < 3; i++) \
2645 d->last_dc[i] = s->last_dc[i]; \
2646 \
2647 /* statistics */ \
2648 d->mv_bits = s->mv_bits; \
2649 d->i_tex_bits = s->i_tex_bits; \
2650 d->p_tex_bits = s->p_tex_bits; \
2651 d->i_count = s->i_count; \
2652 d->misc_bits = s->misc_bits; \
2653 d->last_bits = 0; \
2654 \
2655 d->c.mb_skipped = 0; \
2656 d->c.qscale = s->c.qscale; \
2657 d->dquant = s->dquant; \
2658 \
2659 d->esc3_level_length = s->esc3_level_length; \
2660 } \
2661 \
2662 static inline void AFTER ## _context_after_encode(DST_TYPE *const d, \
2663 const SRC_TYPE *const s, \
2664 int data_partitioning) \
2665 { \
2666 /* FIXME is memcpy faster than a loop? */ \
2667 memcpy(d->c.mv, s->c.mv, 2*4*2*sizeof(int)); \
2668 memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
2669 \
2670 /* MPEG-1 */ \
2671 d->mb_skip_run = s->mb_skip_run; \
2672 for (int i = 0; i < 3; i++) \
2673 d->last_dc[i] = s->last_dc[i]; \
2674 \
2675 /* statistics */ \
2676 d->mv_bits = s->mv_bits; \
2677 d->i_tex_bits = s->i_tex_bits; \
2678 d->p_tex_bits = s->p_tex_bits; \
2679 d->i_count = s->i_count; \
2680 d->misc_bits = s->misc_bits; \
2681 \
2682 d->c.mb_intra = s->c.mb_intra; \
2683 d->c.mb_skipped = s->c.mb_skipped; \
2684 d->c.mv_type = s->c.mv_type; \
2685 d->c.mv_dir = s->c.mv_dir; \
2686 d->pb = s->pb; \
2687 if (data_partitioning) { \
2688 d->pb2 = s->pb2; \
2689 d->tex_pb = s->tex_pb; \
2690 } \
2691 d->block = s->block; \
2692 for (int i = 0; i < 8; i++) \
2693 d->c.block_last_index[i] = s->c.block_last_index[i]; \
2694 d->c.interlaced_dct = s->c.interlaced_dct; \
2695 d->c.qscale = s->c.qscale; \
2696 \
2697 d->esc3_level_length = s->esc3_level_length; \
2698 }
2699
2700
6/6
✓ Branch 0 taken 4520907 times.
✓ Branch 1 taken 1506969 times.
✓ Branch 2 taken 232816 times.
✓ Branch 3 taken 742262 times.
✓ Branch 4 taken 7800624 times.
✓ Branch 5 taken 975078 times.
27657000 COPY_CONTEXT(backup, save, MBBackup, MPVEncContext)
2701
6/6
✓ Branch 0 taken 7688481 times.
✓ Branch 1 taken 2562827 times.
✓ Branch 2 taken 141085 times.
✓ Branch 3 taken 390806 times.
✓ Branch 4 taken 4255128 times.
✓ Branch 5 taken 531891 times.
29012872 COPY_CONTEXT(reset, store, MPVEncContext, MBBackup)
2702
2703 2030936 static void encode_mb_hq(MPVEncContext *const s, MBBackup *const backup, MBBackup *const best,
2704 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2705 int *dmin, int *next_block, int motion_x, int motion_y)
2706 {
2707 int score;
2708 uint8_t *dest_backup[3];
2709
2710 2030936 reset_context_before_encode(s, backup);
2711
2712 2030936 s->block = s->blocks[*next_block];
2713 2030936 s->pb = pb[*next_block];
2714
2/2
✓ Branch 0 taken 376394 times.
✓ Branch 1 taken 1654542 times.
2030936 if (s->data_partitioning) {
2715 376394 s->pb2 = pb2 [*next_block];
2716 376394 s->tex_pb= tex_pb[*next_block];
2717 }
2718
2719
2/2
✓ Branch 0 taken 1089864 times.
✓ Branch 1 taken 941072 times.
2030936 if(*next_block){
2720 1089864 memcpy(dest_backup, s->c.dest, sizeof(s->c.dest));
2721 1089864 s->c.dest[0] = s->c.sc.rd_scratchpad;
2722 1089864 s->c.dest[1] = s->c.sc.rd_scratchpad + 16*s->c.linesize;
2723 1089864 s->c.dest[2] = s->c.sc.rd_scratchpad + 16*s->c.linesize + 8;
2724
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1089864 times.
1089864 av_assert0(s->c.linesize >= 32); //FIXME
2725 }
2726
2727 2030936 encode_mb(s, motion_x, motion_y);
2728
2729 2030936 score= put_bits_count(&s->pb);
2730
2/2
✓ Branch 0 taken 376394 times.
✓ Branch 1 taken 1654542 times.
2030936 if (s->data_partitioning) {
2731 376394 score+= put_bits_count(&s->pb2);
2732 376394 score+= put_bits_count(&s->tex_pb);
2733 }
2734
2735
2/2
✓ Branch 0 taken 1653977 times.
✓ Branch 1 taken 376959 times.
2030936 if (s->c.avctx->mb_decision == FF_MB_DECISION_RD) {
2736 1653977 mpv_reconstruct_mb(s, s->block);
2737
2738 1653977 score *= s->lambda2;
2739 1653977 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2740 }
2741
2742
2/2
✓ Branch 0 taken 1089864 times.
✓ Branch 1 taken 941072 times.
2030936 if(*next_block){
2743 1089864 memcpy(s->c.dest, dest_backup, sizeof(s->c.dest));
2744 }
2745
2746
2/2
✓ Branch 0 taken 975078 times.
✓ Branch 1 taken 1055858 times.
2030936 if(score<*dmin){
2747 975078 *dmin= score;
2748 975078 *next_block^=1;
2749
2750 975078 save_context_after_encode(best, s, s->data_partitioning);
2751 }
2752 2030936 }
2753
2754 24084 static int sse(const MPVEncContext *const s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride)
2755 {
2756 24084 const uint32_t *sq = ff_square_tab + 256;
2757 24084 int acc=0;
2758 int x,y;
2759
2760
3/4
✓ Branch 0 taken 3713 times.
✓ Branch 1 taken 20371 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3713 times.
24084 if(w==16 && h==16)
2761 return s->sse_cmp[0](NULL, src1, src2, stride, 16);
2762
3/4
✓ Branch 0 taken 7426 times.
✓ Branch 1 taken 16658 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7426 times.
24084 else if(w==8 && h==8)
2763 return s->sse_cmp[1](NULL, src1, src2, stride, 8);
2764
2765
2/2
✓ Branch 0 taken 114894 times.
✓ Branch 1 taken 24084 times.
138978 for(y=0; y<h; y++){
2766
2/2
✓ Branch 0 taken 302834 times.
✓ Branch 1 taken 114894 times.
417728 for(x=0; x<w; x++){
2767 302834 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2768 }
2769 }
2770
2771 av_assert2(acc>=0);
2772
2773 24084 return acc;
2774 }
2775
2776 1653977 static int sse_mb(MPVEncContext *const s)
2777 {
2778 1653977 int w= 16;
2779 1653977 int h= 16;
2780 1653977 int chroma_mb_w = w >> s->c.chroma_x_shift;
2781 1653977 int chroma_mb_h = h >> s->c.chroma_y_shift;
2782
2783
2/2
✓ Branch 0 taken 4315 times.
✓ Branch 1 taken 1649662 times.
1653977 if (s->c.mb_x*16 + 16 > s->c.width ) w = s->c.width - s->c.mb_x*16;
2784
2/2
✓ Branch 0 taken 5345 times.
✓ Branch 1 taken 1648632 times.
1653977 if (s->c.mb_y*16 + 16 > s->c.height) h = s->c.height- s->c.mb_y*16;
2785
2786
4/4
✓ Branch 0 taken 1649662 times.
✓ Branch 1 taken 4315 times.
✓ Branch 2 taken 1645949 times.
✓ Branch 3 taken 3713 times.
1653977 if(w==16 && h==16)
2787 1645949 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,
2788 1645949 s->c.dest[0], s->c.linesize, 16) +
2789 1645949 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,
2790 3291898 s->c.dest[1], s->c.uvlinesize, chroma_mb_h) +
2791 1645949 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,
2792 1645949 s->c.dest[2], s->c.uvlinesize, chroma_mb_h);
2793 else
2794 8028 return sse(s, s->new_pic->data[0] + s->c.mb_x * 16 + s->c.mb_y * s->c.linesize * 16,
2795 8028 s->c.dest[0], w, h, s->c.linesize) +
2796 8028 sse(s, s->new_pic->data[1] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
2797 16056 s->c.dest[1], w >> s->c.chroma_x_shift, h >> s->c.chroma_y_shift, s->c.uvlinesize) +
2798 8028 sse(s, s->new_pic->data[2] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
2799 8028 s->c.dest[2], w >> s->c.chroma_x_shift, h >> s->c.chroma_y_shift, s->c.uvlinesize);
2800 }
2801
2802 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2803 MPVEncContext *const s = *(void**)arg;
2804
2805
2806 s->me.pre_pass = 1;
2807 s->me.dia_size = s->c.avctx->pre_dia_size;
2808 s->c.first_slice_line = 1;
2809 for (s->c.mb_y = s->c.end_mb_y - 1; s->c.mb_y >= s->c.start_mb_y; s->c.mb_y--) {
2810 for (s->c.mb_x = s->c.mb_width - 1; s->c.mb_x >=0 ; s->c.mb_x--)
2811 ff_pre_estimate_p_frame_motion(s, s->c.mb_x, s->c.mb_y);
2812 s->c.first_slice_line = 0;
2813 }
2814
2815 s->me.pre_pass = 0;
2816
2817 return 0;
2818 }
2819
2820 11162 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2821 11162 MPVEncContext *const s = *(void**)arg;
2822
2823 11162 s->me.dia_size = s->c.avctx->dia_size;
2824 11162 s->c.first_slice_line = 1;
2825
2/2
✓ Branch 0 taken 125752 times.
✓ Branch 1 taken 11162 times.
136914 for (s->c.mb_y = s->c.start_mb_y; s->c.mb_y < s->c.end_mb_y; s->c.mb_y++) {
2826 125752 s->c.mb_x = 0; //for block init below
2827 125752 ff_init_block_index(&s->c);
2828
2/2
✓ Branch 0 taken 2835533 times.
✓ Branch 1 taken 125752 times.
2961285 for (s->c.mb_x = 0; s->c.mb_x < s->c.mb_width; s->c.mb_x++) {
2829 2835533 s->c.block_index[0] += 2;
2830 2835533 s->c.block_index[1] += 2;
2831 2835533 s->c.block_index[2] += 2;
2832 2835533 s->c.block_index[3] += 2;
2833
2834 /* compute motion vector & mb_type and store in context */
2835
2/2
✓ Branch 0 taken 398560 times.
✓ Branch 1 taken 2436973 times.
2835533 if (s->c.pict_type == AV_PICTURE_TYPE_B)
2836 398560 ff_estimate_b_frame_motion(s, s->c.mb_x, s->c.mb_y);
2837 else
2838 2436973 ff_estimate_p_frame_motion(s, s->c.mb_x, s->c.mb_y);
2839 }
2840 125752 s->c.first_slice_line = 0;
2841 }
2842 11162 return 0;
2843 }
2844
2845 1123 static int mb_var_thread(AVCodecContext *c, void *arg){
2846 1123 MPVEncContext *const s = *(void**)arg;
2847
2848
2/2
✓ Branch 0 taken 14768 times.
✓ Branch 1 taken 1123 times.
15891 for (int mb_y = s->c.start_mb_y; mb_y < s->c.end_mb_y; mb_y++) {
2849
2/2
✓ Branch 0 taken 328614 times.
✓ Branch 1 taken 14768 times.
343382 for (int mb_x = 0; mb_x < s->c.mb_width; mb_x++) {
2850 328614 int xx = mb_x * 16;
2851 328614 int yy = mb_y * 16;
2852 328614 const uint8_t *pix = s->new_pic->data[0] + (yy * s->c.linesize) + xx;
2853 int varc;
2854 328614 int sum = s->mpvencdsp.pix_sum(pix, s->c.linesize);
2855
2856 328614 varc = (s->mpvencdsp.pix_norm1(pix, s->c.linesize) -
2857 328614 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2858
2859 328614 s->mb_var [s->c.mb_stride * mb_y + mb_x] = varc;
2860 328614 s->mb_mean[s->c.mb_stride * mb_y + mb_x] = (sum+128)>>8;
2861 328614 s->me.mb_var_sum_temp += varc;
2862 }
2863 }
2864 1123 return 0;
2865 }
2866
2867 333129 static void write_slice_end(MPVEncContext *const s)
2868 {
2869
2/2
✓ Branch 0 taken 6069 times.
✓ Branch 1 taken 327060 times.
333129 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4) {
2870
2/2
✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 4309 times.
6069 if (s->partitioned_frame)
2871 1760 ff_mpeg4_merge_partitions(s);
2872
2873 6069 ff_mpeg4_stuffing(&s->pb);
2874 327060 } else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2875
2/2
✓ Branch 0 taken 4089 times.
✓ Branch 1 taken 322971 times.
327060 s->c.out_format == FMT_MJPEG) {
2876 4089 ff_mjpeg_encode_stuffing(s);
2877
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 322521 times.
322971 } else if (CONFIG_SPEEDHQ_ENCODER && s->c.out_format == FMT_SPEEDHQ) {
2878 450 ff_speedhq_end_slice(s);
2879 }
2880
2881 333129 flush_put_bits(&s->pb);
2882
2883
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 333129 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
333129 if ((s->c.avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
2884 s->misc_bits+= get_bits_diff(s);
2885 333129 }
2886
2887 static void write_mb_info(MPVEncContext *const s)
2888 {
2889 uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2890 int offset = put_bits_count(&s->pb);
2891 int mba = s->c.mb_x + s->c.mb_width * (s->c.mb_y % s->gob_index);
2892 int gobn = s->c.mb_y / s->gob_index;
2893 int pred_x, pred_y;
2894 if (CONFIG_H263_ENCODER)
2895 ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
2896 bytestream_put_le32(&ptr, offset);
2897 bytestream_put_byte(&ptr, s->c.qscale);
2898 bytestream_put_byte(&ptr, gobn);
2899 bytestream_put_le16(&ptr, mba);
2900 bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2901 bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2902 /* 4MV not implemented */
2903 bytestream_put_byte(&ptr, 0); /* hmv2 */
2904 bytestream_put_byte(&ptr, 0); /* vmv2 */
2905 }
2906
2907 3995843 static void update_mb_info(MPVEncContext *const s)
2908 {
2909
1/2
✓ Branch 0 taken 3995843 times.
✗ Branch 1 not taken.
3995843 if (!s->mb_info)
2910 3995843 return;
2911 if (put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info) {
2912 s->mb_info_size += 12;
2913 s->prev_mb_info = s->last_mb_info;
2914 }
2915
2916 s->last_mb_info = put_bytes_count(&s->pb, 0);
2917 if (!s->mb_info_size)
2918 s->mb_info_size += 12;
2919 write_mb_info(s);
2920 }
2921
2922 4000771 int ff_mpv_reallocate_putbitbuffer(MPVEncContext *const s, size_t threshold, size_t size_increase)
2923 {
2924
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 4000739 times.
4000771 if (put_bytes_left(&s->pb, 0) < threshold
2925
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 && s->c.slice_context_count == 1
2926
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 && s->pb.buf == s->c.avctx->internal->byte_buffer) {
2927 32 int lastgob_pos = s->ptr_lastgob - s->pb.buf;
2928
2929 32 uint8_t *new_buffer = NULL;
2930 32 int new_buffer_size = 0;
2931
2932
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) {
2933 av_log(s->c.avctx, AV_LOG_ERROR, "Cannot reallocate putbit buffer\n");
2934 return AVERROR(ENOMEM);
2935 }
2936
2937 32 emms_c();
2938
2939 32 av_fast_padded_malloc(&new_buffer, &new_buffer_size,
2940 32 s->c.avctx->internal->byte_buffer_size + size_increase);
2941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!new_buffer)
2942 return AVERROR(ENOMEM);
2943
2944 32 memcpy(new_buffer, s->c.avctx->internal->byte_buffer, s->c.avctx->internal->byte_buffer_size);
2945 32 av_free(s->c.avctx->internal->byte_buffer);
2946 32 s->c.avctx->internal->byte_buffer = new_buffer;
2947 32 s->c.avctx->internal->byte_buffer_size = new_buffer_size;
2948 32 rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
2949 32 s->ptr_lastgob = s->pb.buf + lastgob_pos;
2950 }
2951
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4000771 times.
4000771 if (put_bytes_left(&s->pb, 0) < threshold)
2952 return AVERROR(EINVAL);
2953 4000771 return 0;
2954 }
2955
2956 14973 static int encode_thread(AVCodecContext *c, void *arg){
2957 14973 MPVEncContext *const s = *(void**)arg;
2958 14973 int chr_h = 16 >> s->c.chroma_y_shift;
2959 int i;
2960 14973 MBBackup best_s = { 0 }, backup_s;
2961 uint8_t bit_buf[2][MAX_MB_BYTES];
2962 // + 2 because ff_copy_bits() overreads
2963 uint8_t bit_buf2[2][MAX_PB2_MB_SIZE + 2];
2964 uint8_t bit_buf_tex[2][MAX_AC_TEX_MB_SIZE + 2];
2965 PutBitContext pb[2], pb2[2], tex_pb[2];
2966
2967
2/2
✓ Branch 0 taken 29946 times.
✓ Branch 1 taken 14973 times.
44919 for(i=0; i<2; i++){
2968 29946 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2969 29946 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_PB2_MB_SIZE);
2970 29946 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_AC_TEX_MB_SIZE);
2971 }
2972
2973 14973 s->last_bits= put_bits_count(&s->pb);
2974 14973 s->mv_bits=0;
2975 14973 s->misc_bits=0;
2976 14973 s->i_tex_bits=0;
2977 14973 s->p_tex_bits=0;
2978 14973 s->i_count=0;
2979
2980
2/2
✓ Branch 0 taken 44919 times.
✓ Branch 1 taken 14973 times.
59892 for(i=0; i<3; i++){
2981 /* init last dc values */
2982 /* note: quant matrix value (8) is implied here */
2983 44919 s->last_dc[i] = 128 << s->c.intra_dc_precision;
2984
2985 44919 s->encoding_error[i] = 0;
2986 }
2987
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 14773 times.
14973 if (s->c.codec_id == AV_CODEC_ID_AMV) {
2988 200 s->last_dc[0] = 128 * 8 / 13;
2989 200 s->last_dc[1] = 128 * 8 / 14;
2990 200 s->last_dc[2] = 128 * 8 / 14;
2991 #if CONFIG_MPEG4_ENCODER
2992
2/2
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 14229 times.
14773 } else if (s->partitioned_frame) {
2993 av_assert1(s->c.codec_id == AV_CODEC_ID_MPEG4);
2994 544 ff_mpeg4_init_partitions(s);
2995 #endif
2996 }
2997 14973 s->mb_skip_run = 0;
2998 14973 memset(s->c.last_mv, 0, sizeof(s->c.last_mv));
2999
3000 14973 s->last_mv_dir = 0;
3001
3002 14973 s->c.resync_mb_x = 0;
3003 14973 s->c.resync_mb_y = 0;
3004 14973 s->c.first_slice_line = 1;
3005 14973 s->ptr_lastgob = s->pb.buf;
3006
2/2
✓ Branch 0 taken 173044 times.
✓ Branch 1 taken 14973 times.
188017 for (int mb_y_order = s->c.start_mb_y; mb_y_order < s->c.end_mb_y; mb_y_order++) {
3007 int mb_y;
3008
2/2
✓ Branch 0 taken 8100 times.
✓ Branch 1 taken 164944 times.
173044 if (CONFIG_SPEEDHQ_ENCODER && s->c.codec_id == AV_CODEC_ID_SPEEDHQ) {
3009 int first_in_slice;
3010 8100 mb_y = ff_speedhq_mb_y_order_to_mb(mb_y_order, s->c.mb_height, &first_in_slice);
3011
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)
3012 1350 ff_speedhq_end_slice(s);
3013 8100 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 1024;
3014 } else {
3015 164944 mb_y = mb_y_order;
3016 }
3017 173044 s->c.mb_x = 0;
3018 173044 s->c.mb_y = mb_y;
3019
3020 173044 ff_set_qscale(&s->c, s->c.qscale);
3021 173044 ff_init_block_index(&s->c);
3022
3023
2/2
✓ Branch 0 taken 3995843 times.
✓ Branch 1 taken 173044 times.
4168887 for (int mb_x = 0; mb_x < s->c.mb_width; mb_x++) {
3024 int mb_type, xy;
3025 // int d;
3026 3995843 int dmin= INT_MAX;
3027 int dir;
3028 3995843 int size_increase = s->c.avctx->internal->byte_buffer_size/4
3029 3995843 + s->c.mb_width*MAX_MB_BYTES;
3030
3031 3995843 ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
3032
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3995843 times.
3995843 if (put_bytes_left(&s->pb, 0) < MAX_MB_BYTES){
3033 av_log(s->c.avctx, AV_LOG_ERROR, "encoded frame too large\n");
3034 return -1;
3035 }
3036
2/2
✓ Branch 0 taken 179550 times.
✓ Branch 1 taken 3816293 times.
3995843 if (s->data_partitioning) {
3037
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 ||
3038 179550 put_bytes_left(&s->tex_pb, 0) < MAX_MB_BYTES) {
3039 av_log(s->c.avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
3040 return -1;
3041 }
3042 }
3043
3044 3995843 s->c.mb_x = mb_x;
3045 3995843 s->c.mb_y = mb_y; // moved into loop, can get changed by H.261
3046 3995843 ff_update_block_index(&s->c, 8, 0, s->c.chroma_x_shift);
3047
3048
2/2
✓ Branch 0 taken 119790 times.
✓ Branch 1 taken 3876053 times.
3995843 if (CONFIG_H261_ENCODER && s->c.codec_id == AV_CODEC_ID_H261)
3049 119790 ff_h261_reorder_mb_index(s);
3050 3995843 xy = s->c.mb_y * s->c.mb_stride + s->c.mb_x;
3051 3995843 mb_type = s->mb_type[xy];
3052
3053 /* write gob / video packet header */
3054
2/2
✓ Branch 0 taken 1908849 times.
✓ Branch 1 taken 2086994 times.
3995843 if(s->rtp_mode){
3055 int current_packet_size, is_gob_start;
3056
3057 1908849 current_packet_size = put_bytes_count(&s->pb, 1)
3058 1908849 - (s->ptr_lastgob - s->pb.buf);
3059
3060 4372998 is_gob_start = s->rtp_payload_size &&
3061
4/4
✓ Branch 0 taken 555300 times.
✓ Branch 1 taken 1353549 times.
✓ Branch 2 taken 310021 times.
✓ Branch 3 taken 245279 times.
2218870 current_packet_size >= s->rtp_payload_size &&
3062
2/2
✓ Branch 0 taken 309871 times.
✓ Branch 1 taken 150 times.
310021 mb_y + mb_x > 0;
3063
3064
6/6
✓ Branch 0 taken 171666 times.
✓ Branch 1 taken 1737183 times.
✓ Branch 2 taken 75178 times.
✓ Branch 3 taken 96488 times.
✓ Branch 4 taken 2772 times.
✓ Branch 5 taken 72406 times.
1908849 if (s->c.start_mb_y == mb_y && mb_y > 0 && mb_x == 0) is_gob_start = 1;
3065
3066
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 182006 times.
1908849 switch (s->c.codec_id) {
3067 59400 case AV_CODEC_ID_H263:
3068 case AV_CODEC_ID_H263P:
3069
1/2
✓ Branch 0 taken 59400 times.
✗ Branch 1 not taken.
59400 if (!s->h263_slice_structured)
3070
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;
3071 59400 break;
3072 1547743 case AV_CODEC_ID_MPEG2VIDEO:
3073
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;
3074 av_fallthrough;
3075 case AV_CODEC_ID_MPEG1VIDEO:
3076
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 ||
3077
2/2
✓ Branch 0 taken 413888 times.
✓ Branch 1 taken 1193705 times.
1607593 s->mb_skip_run)
3078 413888 is_gob_start=0;
3079 1607593 break;
3080 59850 case AV_CODEC_ID_MJPEG:
3081
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;
3082 59850 break;
3083 }
3084
3085
2/2
✓ Branch 0 taken 320928 times.
✓ Branch 1 taken 1587921 times.
1908849 if(is_gob_start){
3086
4/4
✓ Branch 0 taken 9903 times.
✓ Branch 1 taken 311025 times.
✓ Branch 2 taken 7131 times.
✓ Branch 3 taken 2772 times.
320928 if (s->c.start_mb_y != mb_y || mb_x != 0) {
3087 318156 write_slice_end(s);
3088
3089
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)
3090 1216 ff_mpeg4_init_partitions(s);
3091 }
3092
3093 av_assert2((put_bits_count(&s->pb)&7) == 0);
3094 320928 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
3095
3096
4/4
✓ Branch 0 taken 437 times.
✓ Branch 1 taken 320491 times.
✓ Branch 2 taken 287 times.
✓ Branch 3 taken 150 times.
320928 if (s->error_rate && s->c.resync_mb_x + s->c.resync_mb_y > 0) {
3097 287 int r = put_bytes_count(&s->pb, 0) + s->picture_number + 16 + s->c.mb_x + s->c.mb_y;
3098 287 int d = 100 / s->error_rate;
3099
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 255 times.
287 if(r % d == 0){
3100 32 current_packet_size=0;
3101 32 s->pb.buf_ptr= s->ptr_lastgob;
3102 av_assert1(put_bits_ptr(&s->pb) == s->ptr_lastgob);
3103 }
3104 }
3105
3106
4/5
✓ Branch 0 taken 3379 times.
✓ Branch 1 taken 312355 times.
✓ Branch 2 taken 2544 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2650 times.
320928 switch (s->c.codec_id) {
3107 3379 case AV_CODEC_ID_MPEG4:
3108 if (CONFIG_MPEG4_ENCODER) {
3109 3379 ff_mpeg4_encode_video_packet_header(s);
3110 3379 ff_mpeg4_clean_buffers(&s->c);
3111 3379 ff_h263_mpeg4_reset_dc(s);
3112 }
3113 3379 break;
3114 312355 case AV_CODEC_ID_MPEG1VIDEO:
3115 case AV_CODEC_ID_MPEG2VIDEO:
3116 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3117 312355 ff_mpeg1_encode_slice_header(s);
3118 312355 ff_mpeg1_clean_buffers(s);
3119 }
3120 312355 break;
3121 #if CONFIG_H263P_ENCODER
3122 2544 case AV_CODEC_ID_H263P:
3123
1/2
✓ Branch 0 taken 2544 times.
✗ Branch 1 not taken.
2544 if (s->c.dc_val)
3124 2544 ff_h263_mpeg4_reset_dc(s);
3125 av_fallthrough;
3126 #endif
3127 case AV_CODEC_ID_H263:
3128 if (CONFIG_H263_ENCODER) {
3129
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)
3130 s->mb_info_size += 12;
3131
3132 2544 ff_h263_encode_gob_header(s, mb_y);
3133 2544 s->prev_mb_info = put_bits_count(&s->pb)/8;
3134 }
3135 2544 break;
3136 }
3137
3138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 320928 times.
320928 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS1) {
3139 int bits= put_bits_count(&s->pb);
3140 s->misc_bits+= bits - s->last_bits;
3141 s->last_bits= bits;
3142 }
3143
3144 320928 s->ptr_lastgob += current_packet_size;
3145 320928 s->c.first_slice_line = 1;
3146 320928 s->c.resync_mb_x = mb_x;
3147 320928 s->c.resync_mb_y = mb_y;
3148 }
3149 }
3150
3151
2/2
✓ Branch 0 taken 430787 times.
✓ Branch 1 taken 3565056 times.
3995843 if (s->c.resync_mb_x == s->c.mb_x &&
3152
2/2
✓ Branch 0 taken 8891 times.
✓ Branch 1 taken 421896 times.
430787 s->c.resync_mb_y+1 == s->c.mb_y)
3153 8891 s->c.first_slice_line = 0;
3154
3155 3995843 s->c.mb_skipped = 0;
3156 3995843 s->dquant=0; //only for QP_RD
3157
3158 3995843 update_mb_info(s);
3159
3160
4/4
✓ Branch 0 taken 3482690 times.
✓ Branch 1 taken 513153 times.
✓ Branch 2 taken 18738 times.
✓ Branch 3 taken 3463952 times.
4527734 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
3161 531891 int next_block=0;
3162 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3163
3164 531891 backup_context_before_encode(&backup_s, s);
3165 531891 backup_s.pb= s->pb;
3166
2/2
✓ Branch 0 taken 141085 times.
✓ Branch 1 taken 390806 times.
531891 if (s->data_partitioning) {
3167 141085 backup_s.pb2= s->pb2;
3168 141085 backup_s.tex_pb= s->tex_pb;
3169 }
3170
3171
2/2
✓ Branch 0 taken 293373 times.
✓ Branch 1 taken 238518 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_INTER){
3172 293373 s->c.mv_dir = MV_DIR_FORWARD;
3173 293373 s->c.mv_type = MV_TYPE_16X16;
3174 293373 s->c.mb_intra = 0;
3175 293373 s->c.mv[0][0][0] = s->p_mv_table[xy][0];
3176 293373 s->c.mv[0][0][1] = s->p_mv_table[xy][1];
3177 293373 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3178 &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
3179 }
3180
2/2
✓ Branch 0 taken 13398 times.
✓ Branch 1 taken 518493 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
3181 13398 s->c.mv_dir = MV_DIR_FORWARD;
3182 13398 s->c.mv_type = MV_TYPE_FIELD;
3183 13398 s->c.mb_intra = 0;
3184
2/2
✓ Branch 0 taken 26796 times.
✓ Branch 1 taken 13398 times.
40194 for(i=0; i<2; i++){
3185 26796 int j = s->c.field_select[0][i] = s->p_field_select_table[i][xy];
3186 26796 s->c.mv[0][i][0] = s->c.p_field_mv_table[i][j][xy][0];
3187 26796 s->c.mv[0][i][1] = s->c.p_field_mv_table[i][j][xy][1];
3188 }
3189 13398 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3190 &dmin, &next_block, 0, 0);
3191 }
3192
2/2
✓ Branch 0 taken 71871 times.
✓ Branch 1 taken 460020 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
3193 71871 s->c.mv_dir = MV_DIR_FORWARD;
3194 71871 s->c.mv_type = MV_TYPE_16X16;
3195 71871 s->c.mb_intra = 0;
3196 71871 s->c.mv[0][0][0] = 0;
3197 71871 s->c.mv[0][0][1] = 0;
3198 71871 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3199 &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
3200 }
3201
2/2
✓ Branch 0 taken 220049 times.
✓ Branch 1 taken 311842 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
3202 220049 s->c.mv_dir = MV_DIR_FORWARD;
3203 220049 s->c.mv_type = MV_TYPE_8X8;
3204 220049 s->c.mb_intra = 0;
3205
2/2
✓ Branch 0 taken 880196 times.
✓ Branch 1 taken 220049 times.
1100245 for(i=0; i<4; i++){
3206 880196 s->c.mv[0][i][0] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
3207 880196 s->c.mv[0][i][1] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
3208 }
3209 220049 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3210 &dmin, &next_block, 0, 0);
3211 }
3212
2/2
✓ Branch 0 taken 218539 times.
✓ Branch 1 taken 313352 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
3213 218539 s->c.mv_dir = MV_DIR_FORWARD;
3214 218539 s->c.mv_type = MV_TYPE_16X16;
3215 218539 s->c.mb_intra = 0;
3216 218539 s->c.mv[0][0][0] = s->b_forw_mv_table[xy][0];
3217 218539 s->c.mv[0][0][1] = s->b_forw_mv_table[xy][1];
3218 218539 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3219 &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
3220 }
3221
2/2
✓ Branch 0 taken 218539 times.
✓ Branch 1 taken 313352 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
3222 218539 s->c.mv_dir = MV_DIR_BACKWARD;
3223 218539 s->c.mv_type = MV_TYPE_16X16;
3224 218539 s->c.mb_intra = 0;
3225 218539 s->c.mv[1][0][0] = s->b_back_mv_table[xy][0];
3226 218539 s->c.mv[1][0][1] = s->b_back_mv_table[xy][1];
3227 218539 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3228 &dmin, &next_block, s->c.mv[1][0][0], s->c.mv[1][0][1]);
3229 }
3230
2/2
✓ Branch 0 taken 218539 times.
✓ Branch 1 taken 313352 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
3231 218539 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3232 218539 s->c.mv_type = MV_TYPE_16X16;
3233 218539 s->c.mb_intra = 0;
3234 218539 s->c.mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3235 218539 s->c.mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3236 218539 s->c.mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3237 218539 s->c.mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3238 218539 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3239 &dmin, &next_block, 0, 0);
3240 }
3241
2/2
✓ Branch 0 taken 30990 times.
✓ Branch 1 taken 500901 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
3242 30990 s->c.mv_dir = MV_DIR_FORWARD;
3243 30990 s->c.mv_type = MV_TYPE_FIELD;
3244 30990 s->c.mb_intra = 0;
3245
2/2
✓ Branch 0 taken 61980 times.
✓ Branch 1 taken 30990 times.
92970 for(i=0; i<2; i++){
3246 61980 int j = s->c.field_select[0][i] = s->b_field_select_table[0][i][xy];
3247 61980 s->c.mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3248 61980 s->c.mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3249 }
3250 30990 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3251 &dmin, &next_block, 0, 0);
3252 }
3253
2/2
✓ Branch 0 taken 31154 times.
✓ Branch 1 taken 500737 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
3254 31154 s->c.mv_dir = MV_DIR_BACKWARD;
3255 31154 s->c.mv_type = MV_TYPE_FIELD;
3256 31154 s->c.mb_intra = 0;
3257
2/2
✓ Branch 0 taken 62308 times.
✓ Branch 1 taken 31154 times.
93462 for(i=0; i<2; i++){
3258 62308 int j = s->c.field_select[1][i] = s->b_field_select_table[1][i][xy];
3259 62308 s->c.mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3260 62308 s->c.mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3261 }
3262 31154 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3263 &dmin, &next_block, 0, 0);
3264 }
3265
2/2
✓ Branch 0 taken 26027 times.
✓ Branch 1 taken 505864 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
3266 26027 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3267 26027 s->c.mv_type = MV_TYPE_FIELD;
3268 26027 s->c.mb_intra = 0;
3269
2/2
✓ Branch 0 taken 52054 times.
✓ Branch 1 taken 26027 times.
78081 for(dir=0; dir<2; dir++){
3270
2/2
✓ Branch 0 taken 104108 times.
✓ Branch 1 taken 52054 times.
156162 for(i=0; i<2; i++){
3271 104108 int j = s->c.field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3272 104108 s->c.mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3273 104108 s->c.mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3274 }
3275 }
3276 26027 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3277 &dmin, &next_block, 0, 0);
3278 }
3279
2/2
✓ Branch 0 taken 117782 times.
✓ Branch 1 taken 414109 times.
531891 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
3280 117782 s->c.mv_dir = 0;
3281 117782 s->c.mv_type = MV_TYPE_16X16;
3282 117782 s->c.mb_intra = 1;
3283 117782 s->c.mv[0][0][0] = 0;
3284 117782 s->c.mv[0][0][1] = 0;
3285 117782 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3286 &dmin, &next_block, 0, 0);
3287 117782 s->c.mbintra_table[xy] = 1;
3288 }
3289
3290
4/4
✓ Branch 0 taken 179700 times.
✓ Branch 1 taken 352191 times.
✓ Branch 2 taken 179660 times.
✓ Branch 3 taken 40 times.
531891 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
3291
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
3292 166470 const int last_qp = backup_s.c.qscale;
3293 int qpi, qp, dc[6];
3294 int16_t ac[6][16];
3295 166470 const int mvdir = (best_s.c.mv_dir & MV_DIR_BACKWARD) ? 1 : 0;
3296 static const int dquant_tab[4]={-1,1,-2,2};
3297
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;
3298
3299 av_assert2(backup_s.dquant == 0);
3300
3301 //FIXME intra
3302 166470 s->c.mv_dir = best_s.c.mv_dir;
3303 166470 s->c.mv_type = MV_TYPE_16X16;
3304 166470 s->c.mb_intra = best_s.c.mb_intra;
3305 166470 s->c.mv[0][0][0] = best_s.c.mv[0][0][0];
3306 166470 s->c.mv[0][0][1] = best_s.c.mv[0][0][1];
3307 166470 s->c.mv[1][0][0] = best_s.c.mv[1][0][0];
3308 166470 s->c.mv[1][0][1] = best_s.c.mv[1][0][1];
3309
3310
2/2
✓ Branch 0 taken 111396 times.
✓ Branch 1 taken 55074 times.
166470 qpi = s->c.pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3311
2/2
✓ Branch 0 taken 443088 times.
✓ Branch 1 taken 166470 times.
609558 for(; qpi<4; qpi++){
3312 443088 int dquant= dquant_tab[qpi];
3313 443088 qp= last_qp + dquant;
3314
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)
3315 50837 continue;
3316 392251 backup_s.dquant= dquant;
3317
2/2
✓ Branch 0 taken 37382 times.
✓ Branch 1 taken 354869 times.
392251 if(storecoefs){
3318
2/2
✓ Branch 0 taken 224292 times.
✓ Branch 1 taken 37382 times.
261674 for(i=0; i<6; i++){
3319 224292 dc[i] = s->c.dc_val[s->c.block_index[i]];
3320 224292 memcpy(ac[i], s->c.ac_val[s->c.block_index[i]], sizeof(*s->c.ac_val));
3321 }
3322 }
3323
3324 392251 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3325 &dmin, &next_block, s->c.mv[mvdir][0][0], s->c.mv[mvdir][0][1]);
3326
2/2
✓ Branch 0 taken 369889 times.
✓ Branch 1 taken 22362 times.
392251 if (best_s.c.qscale != qp) {
3327
2/2
✓ Branch 0 taken 33691 times.
✓ Branch 1 taken 336198 times.
369889 if(storecoefs){
3328
2/2
✓ Branch 0 taken 202146 times.
✓ Branch 1 taken 33691 times.
235837 for(i=0; i<6; i++){
3329 202146 s->c.dc_val[s->c.block_index[i]] = dc[i];
3330 202146 memcpy(s->c.ac_val[s->c.block_index[i]], ac[i], sizeof(*s->c.ac_val));
3331 }
3332 }
3333 }
3334 }
3335 }
3336 }
3337
2/2
✓ Branch 0 taken 141760 times.
✓ Branch 1 taken 390131 times.
531891 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3338 141760 int mx= s->b_direct_mv_table[xy][0];
3339 141760 int my= s->b_direct_mv_table[xy][1];
3340
3341 141760 backup_s.dquant = 0;
3342 141760 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3343 141760 s->c.mb_intra = 0;
3344 141760 ff_mpeg4_set_direct_mv(&s->c, mx, my);
3345 141760 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3346 &dmin, &next_block, mx, my);
3347 }
3348
2/2
✓ Branch 0 taken 36664 times.
✓ Branch 1 taken 495227 times.
531891 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3349 36664 backup_s.dquant = 0;
3350 36664 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3351 36664 s->c.mb_intra = 0;
3352 36664 ff_mpeg4_set_direct_mv(&s->c, 0, 0);
3353 36664 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3354 &dmin, &next_block, 0, 0);
3355 }
3356
3/4
✓ Branch 0 taken 492790 times.
✓ Branch 1 taken 39101 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 492790 times.
531891 if (!best_s.c.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3357 int coded=0;
3358 for(i=0; i<6; i++)
3359 coded |= s->c.block_last_index[i];
3360 if(coded){
3361 int mx,my;
3362 memcpy(s->c.mv, best_s.c.mv, sizeof(s->c.mv));
3363 if (CONFIG_MPEG4_ENCODER && best_s.c.mv_dir & MV_DIRECT) {
3364 mx=my=0; //FIXME find the one we actually used
3365 ff_mpeg4_set_direct_mv(&s->c, mx, my);
3366 } else if (best_s.c.mv_dir & MV_DIR_BACKWARD) {
3367 mx = s->c.mv[1][0][0];
3368 my = s->c.mv[1][0][1];
3369 }else{
3370 mx = s->c.mv[0][0][0];
3371 my = s->c.mv[0][0][1];
3372 }
3373
3374 s->c.mv_dir = best_s.c.mv_dir;
3375 s->c.mv_type = best_s.c.mv_type;
3376 s->c.mb_intra = 0;
3377 /* s->c.mv[0][0][0] = best_s.mv[0][0][0];
3378 s->c.mv[0][0][1] = best_s.mv[0][0][1];
3379 s->c.mv[1][0][0] = best_s.mv[1][0][0];
3380 s->c.mv[1][0][1] = best_s.mv[1][0][1];*/
3381 backup_s.dquant= 0;
3382 s->skipdct=1;
3383 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3384 &dmin, &next_block, mx, my);
3385 s->skipdct=0;
3386 }
3387 }
3388
3389 531891 store_context_after_encode(s, &best_s, s->data_partitioning);
3390
3391 531891 pb_bits_count= put_bits_count(&s->pb);
3392 531891 flush_put_bits(&s->pb);
3393 531891 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3394 531891 s->pb= backup_s.pb;
3395
3396
2/2
✓ Branch 0 taken 141085 times.
✓ Branch 1 taken 390806 times.
531891 if (s->data_partitioning) {
3397 141085 pb2_bits_count= put_bits_count(&s->pb2);
3398 141085 flush_put_bits(&s->pb2);
3399 141085 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3400 141085 s->pb2= backup_s.pb2;
3401
3402 141085 tex_pb_bits_count= put_bits_count(&s->tex_pb);
3403 141085 flush_put_bits(&s->tex_pb);
3404 141085 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3405 141085 s->tex_pb= backup_s.tex_pb;
3406 }
3407 531891 s->last_bits= put_bits_count(&s->pb);
3408
3409 531891 if (CONFIG_H263_ENCODER &&
3410
4/4
✓ Branch 0 taken 397151 times.
✓ Branch 1 taken 134740 times.
✓ Branch 2 taken 255276 times.
✓ Branch 3 taken 141875 times.
531891 s->c.out_format == FMT_H263 && s->c.pict_type != AV_PICTURE_TYPE_B)
3411 255276 ff_h263_update_mb(s);
3412
3413
2/2
✓ Branch 0 taken 219489 times.
✓ Branch 1 taken 312402 times.
531891 if(next_block==0){ //FIXME 16 vs linesize16
3414 219489 s->c.hdsp.put_pixels_tab[0][0](s->c.dest[0], s->c.sc.rd_scratchpad , s->c.linesize ,16);
3415 219489 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);
3416 219489 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);
3417 }
3418
3419
2/2
✓ Branch 0 taken 141006 times.
✓ Branch 1 taken 390885 times.
531891 if (s->c.avctx->mb_decision == FF_MB_DECISION_BITS)
3420 141006 mpv_reconstruct_mb(s, s->block);
3421 } else {
3422 3463952 int motion_x = 0, motion_y = 0;
3423 3463952 s->c.mv_type = MV_TYPE_16X16;
3424 // only one MB-Type possible
3425
3426
10/13
✓ Branch 0 taken 1240592 times.
✓ Branch 1 taken 2033928 times.
✓ Branch 2 taken 9451 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16201 times.
✓ Branch 5 taken 51 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.
3463952 switch(mb_type){
3427 1240592 case CANDIDATE_MB_TYPE_INTRA:
3428 1240592 s->c.mv_dir = 0;
3429 1240592 s->c.mb_intra = 1;
3430 1240592 motion_x= s->c.mv[0][0][0] = 0;
3431 1240592 motion_y= s->c.mv[0][0][1] = 0;
3432 1240592 s->c.mbintra_table[xy] = 1;
3433 1240592 break;
3434 2033928 case CANDIDATE_MB_TYPE_INTER:
3435 2033928 s->c.mv_dir = MV_DIR_FORWARD;
3436 2033928 s->c.mb_intra = 0;
3437 2033928 motion_x= s->c.mv[0][0][0] = s->p_mv_table[xy][0];
3438 2033928 motion_y= s->c.mv[0][0][1] = s->p_mv_table[xy][1];
3439 2033928 break;
3440 9451 case CANDIDATE_MB_TYPE_INTER_I:
3441 9451 s->c.mv_dir = MV_DIR_FORWARD;
3442 9451 s->c.mv_type = MV_TYPE_FIELD;
3443 9451 s->c.mb_intra = 0;
3444
2/2
✓ Branch 0 taken 18902 times.
✓ Branch 1 taken 9451 times.
28353 for(i=0; i<2; i++){
3445 18902 int j = s->c.field_select[0][i] = s->p_field_select_table[i][xy];
3446 18902 s->c.mv[0][i][0] = s->c.p_field_mv_table[i][j][xy][0];
3447 18902 s->c.mv[0][i][1] = s->c.p_field_mv_table[i][j][xy][1];
3448 }
3449 9451 break;
3450 case CANDIDATE_MB_TYPE_INTER4V:
3451 s->c.mv_dir = MV_DIR_FORWARD;
3452 s->c.mv_type = MV_TYPE_8X8;
3453 s->c.mb_intra = 0;
3454 for(i=0; i<4; i++){
3455 s->c.mv[0][i][0] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
3456 s->c.mv[0][i][1] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
3457 }
3458 break;
3459 16201 case CANDIDATE_MB_TYPE_DIRECT:
3460 if (CONFIG_MPEG4_ENCODER) {
3461 16201 s->c.mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3462 16201 s->c.mb_intra = 0;
3463 16201 motion_x=s->b_direct_mv_table[xy][0];
3464 16201 motion_y=s->b_direct_mv_table[xy][1];
3465 16201 ff_mpeg4_set_direct_mv(&s->c, motion_x, motion_y);
3466 }
3467 16201 break;
3468 51 case CANDIDATE_MB_TYPE_DIRECT0:
3469 if (CONFIG_MPEG4_ENCODER) {
3470 51 s->c.mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3471 51 s->c.mb_intra = 0;
3472 51 ff_mpeg4_set_direct_mv(&s->c, 0, 0);
3473 }
3474 51 break;
3475 101722 case CANDIDATE_MB_TYPE_BIDIR:
3476 101722 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3477 101722 s->c.mb_intra = 0;
3478 101722 s->c.mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3479 101722 s->c.mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3480 101722 s->c.mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3481 101722 s->c.mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3482 101722 break;
3483 35516 case CANDIDATE_MB_TYPE_BACKWARD:
3484 35516 s->c.mv_dir = MV_DIR_BACKWARD;
3485 35516 s->c.mb_intra = 0;
3486 35516 motion_x= s->c.mv[1][0][0] = s->b_back_mv_table[xy][0];
3487 35516 motion_y= s->c.mv[1][0][1] = s->b_back_mv_table[xy][1];
3488 35516 break;
3489 21829 case CANDIDATE_MB_TYPE_FORWARD:
3490 21829 s->c.mv_dir = MV_DIR_FORWARD;
3491 21829 s->c.mb_intra = 0;
3492 21829 motion_x= s->c.mv[0][0][0] = s->b_forw_mv_table[xy][0];
3493 21829 motion_y= s->c.mv[0][0][1] = s->b_forw_mv_table[xy][1];
3494 21829 break;
3495 2174 case CANDIDATE_MB_TYPE_FORWARD_I:
3496 2174 s->c.mv_dir = MV_DIR_FORWARD;
3497 2174 s->c.mv_type = MV_TYPE_FIELD;
3498 2174 s->c.mb_intra = 0;
3499
2/2
✓ Branch 0 taken 4348 times.
✓ Branch 1 taken 2174 times.
6522 for(i=0; i<2; i++){
3500 4348 int j = s->c.field_select[0][i] = s->b_field_select_table[0][i][xy];
3501 4348 s->c.mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3502 4348 s->c.mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3503 }
3504 2174 break;
3505 2488 case CANDIDATE_MB_TYPE_BACKWARD_I:
3506 2488 s->c.mv_dir = MV_DIR_BACKWARD;
3507 2488 s->c.mv_type = MV_TYPE_FIELD;
3508 2488 s->c.mb_intra = 0;
3509
2/2
✓ Branch 0 taken 4976 times.
✓ Branch 1 taken 2488 times.
7464 for(i=0; i<2; i++){
3510 4976 int j = s->c.field_select[1][i] = s->b_field_select_table[1][i][xy];
3511 4976 s->c.mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3512 4976 s->c.mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3513 }
3514 2488 break;
3515 case CANDIDATE_MB_TYPE_BIDIR_I:
3516 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3517 s->c.mv_type = MV_TYPE_FIELD;
3518 s->c.mb_intra = 0;
3519 for(dir=0; dir<2; dir++){
3520 for(i=0; i<2; i++){
3521 int j = s->c.field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3522 s->c.mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3523 s->c.mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3524 }
3525 }
3526 break;
3527 default:
3528 av_unreachable("There is a case for every CANDIDATE_MB_TYPE_* "
3529 "except CANDIDATE_MB_TYPE_SKIPPED which is never "
3530 "the only candidate (always coupled with INTER) "
3531 "so that it never reaches this switch");
3532 }
3533
3534 3463952 encode_mb(s, motion_x, motion_y);
3535
3536 // RAL: Update last macroblock type
3537 3463952 s->last_mv_dir = s->c.mv_dir;
3538
3539 3463952 if (CONFIG_H263_ENCODER &&
3540
4/4
✓ Branch 0 taken 1167517 times.
✓ Branch 1 taken 2296435 times.
✓ Branch 2 taken 1129184 times.
✓ Branch 3 taken 38333 times.
3463952 s->c.out_format == FMT_H263 && s->c.pict_type != AV_PICTURE_TYPE_B)
3541 1129184 ff_h263_update_mb(s);
3542
3543 3463952 mpv_reconstruct_mb(s, s->block);
3544 }
3545
3546 3995843 s->c.cur_pic.qscale_table[xy] = s->c.qscale;
3547
3548 /* clean the MV table in IPS frames for direct mode in B-frames */
3549
2/2
✓ Branch 0 taken 1279693 times.
✓ Branch 1 taken 2716150 times.
3995843 if (s->c.mb_intra /* && I,P,S_TYPE */) {
3550 1279693 s->p_mv_table[xy][0]=0;
3551 1279693 s->p_mv_table[xy][1]=0;
3552 #if CONFIG_H263_ENCODER
3553
4/4
✓ Branch 0 taken 1745558 times.
✓ Branch 1 taken 970592 times.
✓ Branch 2 taken 51657 times.
✓ Branch 3 taken 1693901 times.
2716150 } else if (s->c.h263_pred || s->c.h263_aic) {
3554 1022249 ff_h263_clean_intra_table_entries(&s->c, xy);
3555 #endif
3556 }
3557
3558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3995843 times.
3995843 if (s->c.avctx->flags & AV_CODEC_FLAG_PSNR) {
3559 int w= 16;
3560 int h= 16;
3561
3562 if (s->c.mb_x*16 + 16 > s->c.width ) w = s->c.width - s->c.mb_x*16;
3563 if (s->c.mb_y*16 + 16 > s->c.height) h = s->c.height- s->c.mb_y*16;
3564
3565 s->encoding_error[0] += sse(
3566 s, s->new_pic->data[0] + s->c.mb_x*16 + s->c.mb_y*s->c.linesize*16,
3567 s->c.dest[0], w, h, s->c.linesize);
3568 s->encoding_error[1] += sse(
3569 s, s->new_pic->data[1] + s->c.mb_x*8 + s->c.mb_y*s->c.uvlinesize*chr_h,
3570 s->c.dest[1], w>>1, h>>s->c.chroma_y_shift, s->c.uvlinesize);
3571 s->encoding_error[2] += sse(
3572 s, s->new_pic->data[2] + s->c.mb_x*8 + s->c.mb_y*s->c.uvlinesize*chr_h,
3573 s->c.dest[2], w>>1, h>>s->c.chroma_y_shift, s->c.uvlinesize);
3574 }
3575
2/2
✓ Branch 0 taken 118800 times.
✓ Branch 1 taken 3877043 times.
3995843 if (s->loop_filter) {
3576
2/2
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 59400 times.
118800 if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263)
3577 59400 ff_h263_loop_filter(&s->c);
3578 }
3579 ff_dlog(s->c.avctx, "MB %d %d bits\n",
3580 s->c.mb_x + s->c.mb_y * s->c.mb_stride, put_bits_count(&s->pb));
3581 }
3582 }
3583
3584 #if CONFIG_MSMPEG4ENC
3585 //not beautiful here but we must write it before flushing so it has to be here
3586
4/4
✓ Branch 0 taken 825 times.
✓ Branch 1 taken 14148 times.
✓ Branch 2 taken 425 times.
✓ Branch 3 taken 400 times.
14973 if (s->c.msmpeg4_version != MSMP4_UNUSED && s->c.msmpeg4_version < MSMP4_WMV1 &&
3587
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 382 times.
425 s->c.pict_type == AV_PICTURE_TYPE_I)
3588 43 ff_msmpeg4_encode_ext_header(s);
3589 #endif
3590
3591 14973 write_slice_end(s);
3592
3593 14973 return 0;
3594 }
3595
3596 #define ADD(field) dst->field += src->field;
3597 #define MERGE(field) dst->field += src->field; src->field=0
3598 2772 static void merge_context_after_me(MPVEncContext *const dst, MPVEncContext *const src)
3599 {
3600 2772 ADD(me.scene_change_score);
3601 2772 ADD(me.mc_mb_var_sum_temp);
3602 2772 ADD(me.mb_var_sum_temp);
3603 2772 }
3604
3605 2772 static void merge_context_after_encode(MPVEncContext *const dst, MPVEncContext *const src)
3606 {
3607 int i;
3608
3609 2772 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3610 2772 MERGE(dct_count[1]);
3611 2772 ADD(mv_bits);
3612 2772 ADD(i_tex_bits);
3613 2772 ADD(p_tex_bits);
3614 2772 ADD(i_count);
3615 2772 ADD(misc_bits);
3616 2772 ADD(encoding_error[0]);
3617 2772 ADD(encoding_error[1]);
3618 2772 ADD(encoding_error[2]);
3619
3620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2772 times.
2772 if (dst->dct_error_sum) {
3621 for(i=0; i<64; i++){
3622 MERGE(dct_error_sum[0][i]);
3623 MERGE(dct_error_sum[1][i]);
3624 }
3625 }
3626
3627 av_assert1(put_bits_count(&src->pb) % 8 ==0);
3628 av_assert1(put_bits_count(&dst->pb) % 8 ==0);
3629 2772 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3630 2772 flush_put_bits(&dst->pb);
3631 2772 }
3632
3633 12201 static int estimate_qp(MPVMainEncContext *const m, int dry_run)
3634 {
3635 12201 MPVEncContext *const s = &m->s;
3636
3637
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 12076 times.
12201 if (m->next_lambda){
3638 125 s->c.cur_pic.ptr->f->quality = m->next_lambda;
3639
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if(!dry_run) m->next_lambda= 0;
3640
2/2
✓ Branch 0 taken 5216 times.
✓ Branch 1 taken 6860 times.
12076 } else if (!m->fixed_qscale) {
3641 5216 int quality = ff_rate_estimate_qscale(m, dry_run);
3642 5216 s->c.cur_pic.ptr->f->quality = quality;
3643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5216 times.
5216 if (s->c.cur_pic.ptr->f->quality < 0)
3644 return -1;
3645 }
3646
3647
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 11401 times.
12201 if(s->adaptive_quant){
3648 800 init_qscale_tab(s);
3649
3650
2/3
✓ Branch 0 taken 400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
800 switch (s->c.codec_id) {
3651 400 case AV_CODEC_ID_MPEG4:
3652 if (CONFIG_MPEG4_ENCODER)
3653 400 ff_clean_mpeg4_qscales(s);
3654 400 break;
3655 case AV_CODEC_ID_H263:
3656 case AV_CODEC_ID_H263P:
3657 case AV_CODEC_ID_FLV1:
3658 if (CONFIG_H263_ENCODER)
3659 ff_clean_h263_qscales(s);
3660 break;
3661 }
3662
3663 800 s->lambda = s->lambda_table[0];
3664 //FIXME broken
3665 }else
3666 11401 s->lambda = s->c.cur_pic.ptr->f->quality;
3667 12201 update_qscale(m);
3668 12201 return 0;
3669 }
3670
3671 /* must be called before writing the header */
3672 2690 static void set_frame_distances(MPVEncContext *const s)
3673 {
3674 av_assert1(s->c.cur_pic.ptr->f->pts != AV_NOPTS_VALUE);
3675 2690 s->c.time = s->c.cur_pic.ptr->f->pts * s->c.avctx->time_base.num;
3676
3677
2/2
✓ Branch 0 taken 588 times.
✓ Branch 1 taken 2102 times.
2690 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
3678 588 s->c.pb_time = s->c.pp_time - (s->c.last_non_b_time - s->c.time);
3679 av_assert1(s->c.pb_time > 0 && s->c.pb_time < s->c.pp_time);
3680 }else{
3681 av_assert1(s->picture_number == 0 || s->c.time > s->c.last_non_b_time);
3682 2102 s->c.pp_time = s->c.time - s->c.last_non_b_time;
3683 2102 s->c.last_non_b_time = s->c.time;
3684 }
3685 2690 }
3686
3687 12201 static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt)
3688 {
3689 12201 MPVEncContext *const s = &m->s;
3690 int ret;
3691 int bits;
3692 12201 int context_count = s->c.slice_context_count;
3693
3694
2/2
✓ Branch 0 taken 2690 times.
✓ Branch 1 taken 9511 times.
12201 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4) {
3695 2690 set_frame_distances(s);
3696 2690 ff_set_mpeg4_time(s);
3697 }
3698
3699 // s->lambda = s->c.cur_pic.ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3700
3701
2/2
✓ Branch 0 taken 2865 times.
✓ Branch 1 taken 9336 times.
12201 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
3702 2865 s->c.no_rounding = s->c.msmpeg4_version >= MSMP4_V3;
3703
2/2
✓ Branch 0 taken 8002 times.
✓ Branch 1 taken 1334 times.
9336 } else if (s->c.pict_type != AV_PICTURE_TYPE_B) {
3704 8002 s->c.no_rounding ^= s->flipflop_rounding;
3705 }
3706
3707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12201 times.
12201 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
3708 ret = estimate_qp(m, 1);
3709 if (ret < 0)
3710 return ret;
3711 ff_get_2pass_fcode(m);
3712
2/2
✓ Branch 0 taken 5216 times.
✓ Branch 1 taken 6985 times.
12201 } else if (!(s->c.avctx->flags & AV_CODEC_FLAG_QSCALE)) {
3713
2/2
✓ Branch 0 taken 754 times.
✓ Branch 1 taken 4462 times.
5216 if (s->c.pict_type == AV_PICTURE_TYPE_B)
3714 754 s->lambda = m->last_lambda_for[s->c.pict_type];
3715 else
3716 4462 s->lambda = m->last_lambda_for[m->last_non_b_pict_type];
3717 5216 update_qscale(m);
3718 }
3719
3720 12201 s->c.mb_intra = 0; //for the rate distortion & bit compare functions
3721
2/2
✓ Branch 0 taken 14973 times.
✓ Branch 1 taken 12201 times.
27174 for (int i = 0; i < context_count; i++) {
3722 14973 MPVEncContext *const slice = s->c.enc_contexts[i];
3723 14973 int h = s->c.mb_height;
3724 14973 uint8_t *start = pkt->data + (int64_t)pkt->size * slice->c.start_mb_y / h;
3725 14973 uint8_t *end = pkt->data + (int64_t)pkt->size * slice->c. end_mb_y / h;
3726
3727 14973 init_put_bits(&slice->pb, start, end - start);
3728
3729
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 12201 times.
14973 if (i) {
3730 2772 ret = ff_update_duplicate_context(&slice->c, &s->c);
3731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2772 times.
2772 if (ret < 0)
3732 return ret;
3733 2772 slice->lambda = s->lambda;
3734 2772 slice->lambda2 = s->lambda2;
3735 }
3736 14973 slice->me.temp = slice->me.scratchpad = slice->c.sc.scratchpad_buf;
3737 14973 ff_me_init_pic(slice);
3738 }
3739
3740 /* Estimate motion for every MB */
3741
2/2
✓ Branch 0 taken 9336 times.
✓ Branch 1 taken 2865 times.
12201 if (s->c.pict_type != AV_PICTURE_TYPE_I) {
3742 9336 s->lambda = (s->lambda * m->me_penalty_compensation + 128) >> 8;
3743 9336 s->lambda2 = (s->lambda2 * (int64_t) m->me_penalty_compensation + 128) >> 8;
3744
2/2
✓ Branch 0 taken 8002 times.
✓ Branch 1 taken 1334 times.
9336 if (s->c.pict_type != AV_PICTURE_TYPE_B) {
3745
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8002 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8002 if ((m->me_pre && m->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
3746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8002 times.
8002 m->me_pre == 2) {
3747 s->c.avctx->execute(s->c.avctx, pre_estimate_motion_thread,
3748 &s->c.enc_contexts[0], NULL,
3749 context_count, sizeof(void*));
3750 }
3751 }
3752
3753 9336 s->c.avctx->execute(s->c.avctx, estimate_motion_thread, &s->c.enc_contexts[0],
3754 NULL, context_count, sizeof(void*));
3755 }else /* if (s->c.pict_type == AV_PICTURE_TYPE_I) */{
3756 /* I-Frame */
3757
2/2
✓ Branch 0 taken 1207602 times.
✓ Branch 1 taken 2865 times.
1210467 for (int i = 0; i < s->c.mb_stride * s->c.mb_height; i++)
3758 1207602 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3759
3760
2/2
✓ Branch 0 taken 915 times.
✓ Branch 1 taken 1950 times.
2865 if (!m->fixed_qscale) {
3761 /* finding spatial complexity for I-frame rate control */
3762 915 s->c.avctx->execute(s->c.avctx, mb_var_thread, &s->c.enc_contexts[0],
3763 NULL, context_count, sizeof(void*));
3764 }
3765 }
3766
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 12201 times.
14973 for (int i = 1; i < context_count; i++)
3767 2772 merge_context_after_me(s, s->c.enc_contexts[i]);
3768 12201 m->mc_mb_var_sum = s->me.mc_mb_var_sum_temp;
3769 12201 m->mb_var_sum = s->me. mb_var_sum_temp;
3770 12201 emms_c();
3771
3772
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 12170 times.
12201 if (s->me.scene_change_score > m->scenechange_threshold &&
3773
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 s->c.pict_type == AV_PICTURE_TYPE_P) {
3774 31 s->c.pict_type = AV_PICTURE_TYPE_I;
3775
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++)
3776 420 s->mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
3777
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
31 if (s->c.msmpeg4_version >= MSMP4_V3)
3778 2 s->c.no_rounding = 1;
3779 ff_dlog(s->c.avctx, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
3780 m->mb_var_sum, m->mc_mb_var_sum);
3781 }
3782
3783
2/2
✓ Branch 0 taken 12051 times.
✓ Branch 1 taken 150 times.
12201 if (!s->umvplus) {
3784
3/4
✓ Branch 0 taken 4215 times.
✓ Branch 1 taken 7836 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4215 times.
12051 if (s->c.pict_type == AV_PICTURE_TYPE_P || s->c.pict_type == AV_PICTURE_TYPE_S) {
3785 7836 s->f_code = ff_get_best_fcode(m, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3786
3787
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 7504 times.
7836 if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3788 int a,b;
3789 332 a = ff_get_best_fcode(m, s->c.p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3790 332 b = ff_get_best_fcode(m, s->c.p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3791 332 s->f_code = FFMAX3(s->f_code, a, b);
3792 }
3793
3794
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7836 times.
7836 ff_fix_long_p_mvs(s, s->intra_penalty ? CANDIDATE_MB_TYPE_INTER : CANDIDATE_MB_TYPE_INTRA);
3795 7836 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, !!s->intra_penalty);
3796
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 7504 times.
7836 if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3797 int j;
3798
2/2
✓ Branch 0 taken 664 times.
✓ Branch 1 taken 332 times.
996 for (int i = 0; i < 2; i++) {
3799
2/2
✓ Branch 0 taken 1328 times.
✓ Branch 1 taken 664 times.
1992 for(j=0; j<2; j++)
3800 1328 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3801 1328 s->c.p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, !!s->intra_penalty);
3802 }
3803 }
3804
2/2
✓ Branch 0 taken 1334 times.
✓ Branch 1 taken 2881 times.
4215 } else if (s->c.pict_type == AV_PICTURE_TYPE_B) {
3805 int a, b;
3806
3807 1334 a = ff_get_best_fcode(m, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3808 1334 b = ff_get_best_fcode(m, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3809 1334 s->f_code = FFMAX(a, b);
3810
3811 1334 a = ff_get_best_fcode(m, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3812 1334 b = ff_get_best_fcode(m, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3813 1334 s->b_code = FFMAX(a, b);
3814
3815 1334 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3816 1334 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3817 1334 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3818 1334 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3819
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 950 times.
1334 if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3820 int dir, j;
3821
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 384 times.
1152 for(dir=0; dir<2; dir++){
3822
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 768 times.
2304 for (int i = 0; i < 2; i++) {
3823
2/2
✓ Branch 0 taken 3072 times.
✓ Branch 1 taken 1536 times.
4608 for(j=0; j<2; j++){
3824 3072 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3825
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 1536 times.
3072 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3826
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,
3827 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3828 }
3829 }
3830 }
3831 }
3832 }
3833 }
3834
3835 12201 ret = estimate_qp(m, 0);
3836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12201 times.
12201 if (ret < 0)
3837 return ret;
3838
3839
4/4
✓ Branch 0 taken 3349 times.
✓ Branch 1 taken 8852 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3345 times.
12201 if (s->c.qscale < 3 && s->max_qcoeff <= 128 &&
3840
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 s->c.pict_type == AV_PICTURE_TYPE_I &&
3841
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !(s->c.avctx->flags & AV_CODEC_FLAG_QSCALE))
3842 2 s->c.qscale = 3; //reduce clipping problems
3843
3844
2/2
✓ Branch 0 taken 1439 times.
✓ Branch 1 taken 10762 times.
12201 if (s->c.out_format == FMT_MJPEG) {
3845 1439 ret = ff_check_codec_matrices(s->c.avctx, FF_MATRIX_TYPE_INTRA | FF_MATRIX_TYPE_CHROMA_INTRA,
3846 1439 (7 + s->c.qscale) / s->c.qscale, 65535);
3847
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1439 times.
1439 if (ret < 0)
3848 return ret;
3849
3850
2/2
✓ Branch 0 taken 1239 times.
✓ Branch 1 taken 200 times.
1439 if (s->c.codec_id != AV_CODEC_ID_AMV) {
3851 1239 const uint16_t * luma_matrix = ff_mpeg1_default_intra_matrix;
3852 1239 const uint16_t *chroma_matrix = ff_mpeg1_default_intra_matrix;
3853
3854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 if (s->c.avctx->intra_matrix) {
3855 chroma_matrix =
3856 luma_matrix = s->c.avctx->intra_matrix;
3857 }
3858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1239 times.
1239 if (s->c.avctx->chroma_intra_matrix)
3859 chroma_matrix = s->c.avctx->chroma_intra_matrix;
3860
3861 /* for mjpeg, we do include qscale in the matrix */
3862
2/2
✓ Branch 0 taken 78057 times.
✓ Branch 1 taken 1239 times.
79296 for (int i = 1; i < 64; i++) {
3863 78057 int j = s->c.idsp.idct_permutation[i];
3864
3865 78057 s->c.chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[i] * s->c.qscale) >> 3);
3866 78057 s->c. intra_matrix[j] = av_clip_uint8(( luma_matrix[i] * s->c.qscale) >> 3);
3867 }
3868 1239 s->c.y_dc_scale_table =
3869 1239 s->c.c_dc_scale_table = ff_mpeg12_dc_scale_table[0];
3870 1239 s->c.chroma_intra_matrix[0] = s->c.intra_matrix[0] = 8;
3871 } else {
3872 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};
3873 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};
3874
2/2
✓ Branch 0 taken 12600 times.
✓ Branch 1 taken 200 times.
12800 for (int i = 1; i < 64; i++) {
3875 12600 int j = s->c.idsp.idct_permutation[ff_zigzag_direct[i]];
3876
3877 12600 s->c.intra_matrix[j] = sp5x_qscale_five_quant_table[0][i];
3878 12600 s->c.chroma_intra_matrix[j] = sp5x_qscale_five_quant_table[1][i];
3879 }
3880 200 s->c.y_dc_scale_table = y;
3881 200 s->c.c_dc_scale_table = c;
3882 200 s->c.intra_matrix[0] = 13;
3883 200 s->c.chroma_intra_matrix[0] = 14;
3884 }
3885 1439 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3886 1439 s->c.intra_matrix, s->intra_quant_bias, 8, 8, 1);
3887 1439 ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3888 1439 s->c.chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3889 1439 s->c.qscale = 8;
3890 }
3891
3892
2/2
✓ Branch 0 taken 2896 times.
✓ Branch 1 taken 9305 times.
12201 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
3893 2896 s->c.cur_pic.ptr->f->flags |= AV_FRAME_FLAG_KEY;
3894 } else {
3895 9305 s->c.cur_pic.ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
3896 }
3897 12201 s->c.cur_pic.ptr->f->pict_type = s->c.pict_type;
3898
3899
2/2
✓ Branch 0 taken 2896 times.
✓ Branch 1 taken 9305 times.
12201 if (s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)
3900 2896 m->picture_in_gop_number = 0;
3901
3902 12201 s->c.mb_x = s->c.mb_y = 0;
3903 12201 s->last_bits= put_bits_count(&s->pb);
3904 12201 ret = m->encode_picture_header(m);
3905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12201 times.
12201 if (ret < 0)
3906 return ret;
3907 12201 bits= put_bits_count(&s->pb);
3908 12201 m->header_bits = bits - s->last_bits;
3909
3910
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 12201 times.
14973 for (int i = 1; i < context_count; i++)
3911 2772 update_duplicate_context_after_me(s->c.enc_contexts[i], s);
3912 12201 s->c.avctx->execute(s->c.avctx, encode_thread, &s->c.enc_contexts[0],
3913 NULL, context_count, sizeof(void*));
3914
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 12201 times.
14973 for (int i = 1; i < context_count; i++) {
3915
2/2
✓ Branch 0 taken 2700 times.
✓ Branch 1 taken 72 times.
2772 if (s->pb.buf_end == s->c.enc_contexts[i]->pb.buf)
3916 2700 set_put_bits_buffer_size(&s->pb, FFMIN(s->c.enc_contexts[i]->pb.buf_end - s->pb.buf, INT_MAX/8-BUF_BITS));
3917 2772 merge_context_after_encode(s, s->c.enc_contexts[i]);
3918 }
3919 12201 emms_c();
3920 12201 return 0;
3921 }
3922
3923 100978751 static inline void denoise_dct(MPVEncContext *const s, int16_t block[])
3924 {
3925
2/2
✓ Branch 0 taken 99938654 times.
✓ Branch 1 taken 1040097 times.
100978751 if (!s->dct_error_sum)
3926 99938654 return;
3927
3928 1040097 const int intra = s->c.mb_intra;
3929 1040097 s->dct_count[intra]++;
3930 1040097 s->mpvencdsp.denoise_dct(block, s->dct_error_sum[intra], s->dct_offset[intra]);
3931 }
3932
3933 8179791 static int dct_quantize_trellis_c(MPVEncContext *const s,
3934 int16_t *block, int n,
3935 int qscale, int *overflow){
3936 const int *qmat;
3937 const uint16_t *matrix;
3938 const uint8_t *scantable;
3939 const uint8_t *perm_scantable;
3940 8179791 int max=0;
3941 unsigned int threshold1, threshold2;
3942 8179791 int bias=0;
3943 int run_tab[65];
3944 int level_tab[65];
3945 int score_tab[65];
3946 int survivor[65];
3947 int survivor_count;
3948 8179791 int last_run=0;
3949 8179791 int last_level=0;
3950 8179791 int last_score= 0;
3951 int last_i;
3952 int coeff[2][64];
3953 int coeff_count[64];
3954 int qmul, qadd, start_i, last_non_zero, i, dc;
3955 8179791 const int esc_length= s->ac_esc_length;
3956 const uint8_t *length, *last_length;
3957 8179791 const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3958 int mpeg2_qscale;
3959
3960 8179791 s->fdsp.fdct(block);
3961
3962 8179791 denoise_dct(s, block);
3963
3964 8179791 qmul= qscale*16;
3965 8179791 qadd= ((qscale-1)|1)*8;
3966
3967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8179791 times.
8179791 if (s->c.q_scale_type) mpeg2_qscale = ff_mpeg2_non_linear_qscale[qscale];
3968 8179791 else mpeg2_qscale = qscale << 1;
3969
3970
2/2
✓ Branch 0 taken 1878764 times.
✓ Branch 1 taken 6301027 times.
8179791 if (s->c.mb_intra) {
3971 int q;
3972 1878764 scantable = s->c.intra_scantable.scantable;
3973 1878764 perm_scantable = s->c.intra_scantable.permutated;
3974
1/2
✓ Branch 0 taken 1878764 times.
✗ Branch 1 not taken.
1878764 if (!s->c.h263_aic) {
3975
2/2
✓ Branch 0 taken 1200828 times.
✓ Branch 1 taken 677936 times.
1878764 if (n < 4)
3976 1200828 q = s->c.y_dc_scale;
3977 else
3978 677936 q = s->c.c_dc_scale;
3979 1878764 q = q << 3;
3980 } else{
3981 /* For AIC we skip quant/dequant of INTRADC */
3982 q = 1 << 3;
3983 qadd=0;
3984 }
3985
3986 /* note: block[0] is assumed to be positive */
3987 1878764 block[0] = (block[0] + (q >> 1)) / q;
3988 1878764 start_i = 1;
3989 1878764 last_non_zero = 0;
3990
2/2
✓ Branch 0 taken 1200828 times.
✓ Branch 1 taken 677936 times.
1878764 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3991
2/2
✓ Branch 0 taken 1200828 times.
✓ Branch 1 taken 677936 times.
1878764 matrix = n < 4 ? s->c.intra_matrix : s->c.chroma_intra_matrix;
3992
5/6
✓ Branch 0 taken 1331544 times.
✓ Branch 1 taken 547220 times.
✓ Branch 2 taken 1331544 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 718200 times.
✓ Branch 5 taken 613344 times.
1878764 if (s->mpeg_quant || s->c.out_format == FMT_MPEG1 || s->c.out_format == FMT_MJPEG)
3993 1265420 bias= 1<<(QMAT_SHIFT-1);
3994
3995
4/4
✓ Branch 0 taken 677936 times.
✓ Branch 1 taken 1200828 times.
✓ Branch 2 taken 239400 times.
✓ Branch 3 taken 438536 times.
1878764 if (n > 3 && s->intra_chroma_ac_vlc_length) {
3996 239400 length = s->intra_chroma_ac_vlc_length;
3997 239400 last_length= s->intra_chroma_ac_vlc_last_length;
3998 } else {
3999 1639364 length = s->intra_ac_vlc_length;
4000 1639364 last_length= s->intra_ac_vlc_last_length;
4001 }
4002 } else {
4003 6301027 scantable = s->c.inter_scantable.scantable;
4004 6301027 perm_scantable = s->c.inter_scantable.permutated;
4005 6301027 start_i = 0;
4006 6301027 last_non_zero = -1;
4007 6301027 qmat = s->q_inter_matrix[qscale];
4008 6301027 matrix = s->c.inter_matrix;
4009 6301027 length = s->inter_ac_vlc_length;
4010 6301027 last_length= s->inter_ac_vlc_last_length;
4011 }
4012 8179791 last_i= start_i;
4013
4014 8179791 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4015 8179791 threshold2= (threshold1<<1);
4016
4017
2/2
✓ Branch 0 taken 352150349 times.
✓ Branch 1 taken 2025469 times.
354175818 for(i=63; i>=start_i; i--) {
4018 352150349 const int j = scantable[i];
4019 352150349 int64_t level = (int64_t)block[j] * qmat[j];
4020
4021
2/2
✓ Branch 0 taken 6154322 times.
✓ Branch 1 taken 345996027 times.
352150349 if(((uint64_t)(level+threshold1))>threshold2){
4022 6154322 last_non_zero = i;
4023 6154322 break;
4024 }
4025 }
4026
4027
2/2
✓ Branch 0 taken 175631833 times.
✓ Branch 1 taken 8179791 times.
183811624 for(i=start_i; i<=last_non_zero; i++) {
4028 175631833 const int j = scantable[i];
4029 175631833 int64_t level = (int64_t)block[j] * qmat[j];
4030
4031 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
4032 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
4033
2/2
✓ Branch 0 taken 60368026 times.
✓ Branch 1 taken 115263807 times.
175631833 if(((uint64_t)(level+threshold1))>threshold2){
4034
2/2
✓ Branch 0 taken 30086895 times.
✓ Branch 1 taken 30281131 times.
60368026 if(level>0){
4035 30086895 level= (bias + level)>>QMAT_SHIFT;
4036 30086895 coeff[0][i]= level;
4037 30086895 coeff[1][i]= level-1;
4038 // coeff[2][k]= level-2;
4039 }else{
4040 30281131 level= (bias - level)>>QMAT_SHIFT;
4041 30281131 coeff[0][i]= -level;
4042 30281131 coeff[1][i]= -level+1;
4043 // coeff[2][k]= -level+2;
4044 }
4045 60368026 coeff_count[i]= FFMIN(level, 2);
4046 av_assert2(coeff_count[i]);
4047 60368026 max |=level;
4048 }else{
4049 115263807 coeff[0][i]= (level>>31)|1;
4050 115263807 coeff_count[i]= 1;
4051 }
4052 }
4053
4054 8179791 *overflow= s->max_qcoeff < max; //overflow might have happened
4055
4056
2/2
✓ Branch 0 taken 2025469 times.
✓ Branch 1 taken 6154322 times.
8179791 if(last_non_zero < start_i){
4057 2025469 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4058 2025469 return last_non_zero;
4059 }
4060
4061 6154322 score_tab[start_i]= 0;
4062 6154322 survivor[0]= start_i;
4063 6154322 survivor_count= 1;
4064
4065
2/2
✓ Branch 0 taken 175631833 times.
✓ Branch 1 taken 6154322 times.
181786155 for(i=start_i; i<=last_non_zero; i++){
4066 int level_index, j, zero_distortion;
4067 175631833 int dct_coeff= FFABS(block[ scantable[i] ]);
4068 175631833 int best_score=256*256*256*120;
4069
4070
1/2
✓ Branch 0 taken 175631833 times.
✗ Branch 1 not taken.
175631833 if (s->fdsp.fdct == ff_fdct_ifast)
4071 175631833 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
4072 175631833 zero_distortion= dct_coeff*dct_coeff;
4073
4074
2/2
✓ Branch 0 taken 201124522 times.
✓ Branch 1 taken 175631833 times.
376756355 for(level_index=0; level_index < coeff_count[i]; level_index++){
4075 int distortion;
4076 201124522 int level= coeff[level_index][i];
4077 201124522 const int alevel= FFABS(level);
4078 int unquant_coeff;
4079
4080 av_assert2(level);
4081
4082
4/4
✓ Branch 0 taken 113005928 times.
✓ Branch 1 taken 88118594 times.
✓ Branch 2 taken 7518283 times.
✓ Branch 3 taken 105487645 times.
201124522 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4083 95636877 unquant_coeff= alevel*qmul + qadd;
4084
2/2
✓ Branch 0 taken 21010944 times.
✓ Branch 1 taken 84476701 times.
105487645 } else if (s->c.out_format == FMT_MJPEG) {
4085 21010944 j = s->c.idsp.idct_permutation[scantable[i]];
4086 21010944 unquant_coeff = alevel * matrix[j] * 8;
4087 }else{ // MPEG-1
4088 84476701 j = s->c.idsp.idct_permutation[scantable[i]]; // FIXME: optimize
4089
2/2
✓ Branch 0 taken 16116353 times.
✓ Branch 1 taken 68360348 times.
84476701 if (s->c.mb_intra) {
4090 16116353 unquant_coeff = (int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4091 16116353 unquant_coeff = (unquant_coeff - 1) | 1;
4092 }else{
4093 68360348 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4094 68360348 unquant_coeff = (unquant_coeff - 1) | 1;
4095 }
4096 84476701 unquant_coeff<<= 3;
4097 }
4098
4099 201124522 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4100 201124522 level+=64;
4101
2/2
✓ Branch 0 taken 201022712 times.
✓ Branch 1 taken 101810 times.
201124522 if((level&(~127)) == 0){
4102
2/2
✓ Branch 0 taken 681396599 times.
✓ Branch 1 taken 201022712 times.
882419311 for(j=survivor_count-1; j>=0; j--){
4103 681396599 int run= i - survivor[j];
4104 681396599 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4105 681396599 score += score_tab[i-run];
4106
4107
2/2
✓ Branch 0 taken 417033792 times.
✓ Branch 1 taken 264362807 times.
681396599 if(score < best_score){
4108 417033792 best_score= score;
4109 417033792 run_tab[i+1]= run;
4110 417033792 level_tab[i+1]= level-64;
4111 }
4112 }
4113
4114
4/4
✓ Branch 0 taken 112936824 times.
✓ Branch 1 taken 88085888 times.
✓ Branch 2 taken 7518283 times.
✓ Branch 3 taken 105418541 times.
201022712 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4115
2/2
✓ Branch 0 taken 330996746 times.
✓ Branch 1 taken 95604171 times.
426600917 for(j=survivor_count-1; j>=0; j--){
4116 330996746 int run= i - survivor[j];
4117 330996746 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4118 330996746 score += score_tab[i-run];
4119
2/2
✓ Branch 0 taken 34325103 times.
✓ Branch 1 taken 296671643 times.
330996746 if(score < last_score){
4120 34325103 last_score= score;
4121 34325103 last_run= run;
4122 34325103 last_level= level-64;
4123 34325103 last_i= i+1;
4124 }
4125 }
4126 }
4127 }else{
4128 101810 distortion += esc_length*lambda;
4129
2/2
✓ Branch 0 taken 105109 times.
✓ Branch 1 taken 101810 times.
206919 for(j=survivor_count-1; j>=0; j--){
4130 105109 int run= i - survivor[j];
4131 105109 int score= distortion + score_tab[i-run];
4132
4133
2/2
✓ Branch 0 taken 53587 times.
✓ Branch 1 taken 51522 times.
105109 if(score < best_score){
4134 53587 best_score= score;
4135 53587 run_tab[i+1]= run;
4136 53587 level_tab[i+1]= level-64;
4137 }
4138 }
4139
4140
3/4
✓ Branch 0 taken 69104 times.
✓ Branch 1 taken 32706 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 69104 times.
101810 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4141
2/2
✓ Branch 0 taken 33909 times.
✓ Branch 1 taken 32706 times.
66615 for(j=survivor_count-1; j>=0; j--){
4142 33909 int run= i - survivor[j];
4143 33909 int score= distortion + score_tab[i-run];
4144
2/2
✓ Branch 0 taken 17291 times.
✓ Branch 1 taken 16618 times.
33909 if(score < last_score){
4145 17291 last_score= score;
4146 17291 last_run= run;
4147 17291 last_level= level-64;
4148 17291 last_i= i+1;
4149 }
4150 }
4151 }
4152 }
4153 }
4154
4155 175631833 score_tab[i+1]= best_score;
4156
4157 // 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
4158
2/2
✓ Branch 0 taken 35520358 times.
✓ Branch 1 taken 140111475 times.
175631833 if(last_non_zero <= 27){
4159
2/2
✓ Branch 0 taken 58671414 times.
✓ Branch 1 taken 9902257 times.
68573671 for(; survivor_count; survivor_count--){
4160
2/2
✓ Branch 0 taken 25618101 times.
✓ Branch 1 taken 33053313 times.
58671414 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4161 25618101 break;
4162 }
4163 }else{
4164
2/2
✓ Branch 0 taken 233449848 times.
✓ Branch 1 taken 39875166 times.
273325014 for(; survivor_count; survivor_count--){
4165
2/2
✓ Branch 0 taken 100236309 times.
✓ Branch 1 taken 133213539 times.
233449848 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4166 100236309 break;
4167 }
4168 }
4169
4170 175631833 survivor[ survivor_count++ ]= i+1;
4171 }
4172
4173
4/4
✓ Branch 0 taken 3483808 times.
✓ Branch 1 taken 2670514 times.
✓ Branch 2 taken 3226215 times.
✓ Branch 3 taken 257593 times.
6154322 if (s->c.out_format != FMT_H263 && s->c.out_format != FMT_H261) {
4174 3226215 last_score= 256*256*256*120;
4175
2/2
✓ Branch 0 taken 30608954 times.
✓ Branch 1 taken 3226215 times.
33835169 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4176 30608954 int score= score_tab[i];
4177
2/2
✓ Branch 0 taken 30258707 times.
✓ Branch 1 taken 350247 times.
30608954 if (i)
4178 30258707 score += lambda * 2; // FIXME more exact?
4179
4180
2/2
✓ Branch 0 taken 3360817 times.
✓ Branch 1 taken 27248137 times.
30608954 if(score < last_score){
4181 3360817 last_score= score;
4182 3360817 last_i= i;
4183 3360817 last_level= level_tab[i];
4184 3360817 last_run= run_tab[i];
4185 }
4186 }
4187 }
4188
4189 6154322 s->coded_score[n] = last_score;
4190
4191 6154322 dc= FFABS(block[0]);
4192 6154322 last_non_zero= last_i - 1;
4193 6154322 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4194
4195
2/2
✓ Branch 0 taken 1040240 times.
✓ Branch 1 taken 5114082 times.
6154322 if(last_non_zero < start_i)
4196 1040240 return last_non_zero;
4197
4198
3/4
✓ Branch 0 taken 486714 times.
✓ Branch 1 taken 4627368 times.
✓ Branch 2 taken 486714 times.
✗ Branch 3 not taken.
5114082 if(last_non_zero == 0 && start_i == 0){
4199 486714 int best_level= 0;
4200 486714 int best_score= dc * dc;
4201
4202
2/2
✓ Branch 0 taken 676184 times.
✓ Branch 1 taken 486714 times.
1162898 for(i=0; i<coeff_count[0]; i++){
4203 676184 int level= coeff[i][0];
4204 676184 int alevel= FFABS(level);
4205 int unquant_coeff, score, distortion;
4206
4207
4/4
✓ Branch 0 taken 383635 times.
✓ Branch 1 taken 292549 times.
✓ Branch 2 taken 20722 times.
✓ Branch 3 taken 362913 times.
676184 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4208 313271 unquant_coeff= (alevel*qmul + qadd)>>3;
4209 } else{ // MPEG-1
4210 362913 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4211 362913 unquant_coeff = (unquant_coeff - 1) | 1;
4212 }
4213 676184 unquant_coeff = (unquant_coeff + 4) >> 3;
4214 676184 unquant_coeff<<= 3 + 3;
4215
4216 676184 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
4217 676184 level+=64;
4218
2/2
✓ Branch 0 taken 673327 times.
✓ Branch 1 taken 2857 times.
676184 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
4219 2857 else score= distortion + esc_length*lambda;
4220
4221
2/2
✓ Branch 0 taken 528497 times.
✓ Branch 1 taken 147687 times.
676184 if(score < best_score){
4222 528497 best_score= score;
4223 528497 best_level= level - 64;
4224 }
4225 }
4226 486714 block[0]= best_level;
4227 486714 s->coded_score[n] = best_score - dc*dc;
4228
2/2
✓ Branch 0 taken 5907 times.
✓ Branch 1 taken 480807 times.
486714 if(best_level == 0) return -1;
4229 480807 else return last_non_zero;
4230 }
4231
4232 4627368 i= last_i;
4233 av_assert2(last_level);
4234
4235 4627368 block[ perm_scantable[last_non_zero] ]= last_level;
4236 4627368 i -= last_run + 1;
4237
4238
2/2
✓ Branch 0 taken 51833085 times.
✓ Branch 1 taken 4627368 times.
56460453 for(; i>start_i; i -= run_tab[i] + 1){
4239 51833085 block[ perm_scantable[i-1] ]= level_tab[i];
4240 }
4241
4242 4627368 return last_non_zero;
4243 }
4244
4245 static DECLARE_ALIGNED(16, int16_t, basis)[64][64];
4246
4247 static void build_basis(uint8_t *perm){
4248 int i, j, x, y;
4249 emms_c();
4250 for(i=0; i<8; i++){
4251 for(j=0; j<8; j++){
4252 for(y=0; y<8; y++){
4253 for(x=0; x<8; x++){
4254 double s= 0.25*(1<<BASIS_SHIFT);
4255 int index= 8*i + j;
4256 int perm_index= perm[index];
4257 if(i==0) s*= sqrt(0.5);
4258 if(j==0) s*= sqrt(0.5);
4259 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)));
4260 }
4261 }
4262 }
4263 }
4264 }
4265
4266 static int dct_quantize_refine(MPVEncContext *const s, //FIXME breaks denoise?
4267 int16_t *block, int16_t *weight, int16_t *orig,
4268 int n, int qscale){
4269 DECLARE_ALIGNED(16, int16_t, rem)[64];
4270 LOCAL_ALIGNED_16(int16_t, d1, [64]);
4271 const uint8_t *scantable;
4272 const uint8_t *perm_scantable;
4273 // unsigned int threshold1, threshold2;
4274 // int bias=0;
4275 int run_tab[65];
4276 int prev_run=0;
4277 int prev_level=0;
4278 int qmul, qadd, start_i, last_non_zero, i, dc;
4279 const uint8_t *length;
4280 const uint8_t *last_length;
4281 int lambda;
4282 int rle_index, run, q = 1, sum; //q is only used when s->c.mb_intra is true
4283
4284 if(basis[0][0] == 0)
4285 build_basis(s->c.idsp.idct_permutation);
4286
4287 qmul= qscale*2;
4288 qadd= (qscale-1)|1;
4289 if (s->c.mb_intra) {
4290 scantable = s->c.intra_scantable.scantable;
4291 perm_scantable = s->c.intra_scantable.permutated;
4292 if (!s->c.h263_aic) {
4293 if (n < 4)
4294 q = s->c.y_dc_scale;
4295 else
4296 q = s->c.c_dc_scale;
4297 } else{
4298 /* For AIC we skip quant/dequant of INTRADC */
4299 q = 1;
4300 qadd=0;
4301 }
4302 q <<= RECON_SHIFT-3;
4303 /* note: block[0] is assumed to be positive */
4304 dc= block[0]*q;
4305 // block[0] = (block[0] + (q >> 1)) / q;
4306 start_i = 1;
4307 // if (s->mpeg_quant || s->c.out_format == FMT_MPEG1)
4308 // bias= 1<<(QMAT_SHIFT-1);
4309 if (n > 3 && s->intra_chroma_ac_vlc_length) {
4310 length = s->intra_chroma_ac_vlc_length;
4311 last_length= s->intra_chroma_ac_vlc_last_length;
4312 } else {
4313 length = s->intra_ac_vlc_length;
4314 last_length= s->intra_ac_vlc_last_length;
4315 }
4316 } else {
4317 scantable = s->c.inter_scantable.scantable;
4318 perm_scantable = s->c.inter_scantable.permutated;
4319 dc= 0;
4320 start_i = 0;
4321 length = s->inter_ac_vlc_length;
4322 last_length= s->inter_ac_vlc_last_length;
4323 }
4324 last_non_zero = s->c.block_last_index[n];
4325
4326 dc += (1<<(RECON_SHIFT-1));
4327 for(i=0; i<64; i++){
4328 rem[i] = dc - (orig[i] << RECON_SHIFT); // FIXME use orig directly instead of copying to rem[]
4329 }
4330
4331 sum=0;
4332 for(i=0; i<64; i++){
4333 int one= 36;
4334 int qns=4;
4335 int w;
4336
4337 w= FFABS(weight[i]) + qns*one;
4338 w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
4339
4340 weight[i] = w;
4341 // w=weight[i] = (63*qns + (w/2)) / w;
4342
4343 av_assert2(w>0);
4344 av_assert2(w<(1<<6));
4345 sum += w*w;
4346 }
4347 lambda = sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
4348
4349 run=0;
4350 rle_index=0;
4351 for(i=start_i; i<=last_non_zero; i++){
4352 int j= perm_scantable[i];
4353 const int level= block[j];
4354 int coeff;
4355
4356 if(level){
4357 if(level<0) coeff= qmul*level - qadd;
4358 else coeff= qmul*level + qadd;
4359 run_tab[rle_index++]=run;
4360 run=0;
4361
4362 s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
4363 }else{
4364 run++;
4365 }
4366 }
4367
4368 for(;;){
4369 int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
4370 int best_coeff=0;
4371 int best_change=0;
4372 int run2, best_unquant_change=0, analyze_gradient;
4373 analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
4374
4375 if(analyze_gradient){
4376 for(i=0; i<64; i++){
4377 int w= weight[i];
4378
4379 d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
4380 }
4381 s->fdsp.fdct(d1);
4382 }
4383
4384 if(start_i){
4385 const int level= block[0];
4386 int change, old_coeff;
4387
4388 av_assert2(s->c.mb_intra);
4389
4390 old_coeff= q*level;
4391
4392 for(change=-1; change<=1; change+=2){
4393 int new_level= level + change;
4394 int score, new_coeff;
4395
4396 new_coeff= q*new_level;
4397 if(new_coeff >= 2048 || new_coeff < 0)
4398 continue;
4399
4400 score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
4401 new_coeff - old_coeff);
4402 if(score<best_score){
4403 best_score= score;
4404 best_coeff= 0;
4405 best_change= change;
4406 best_unquant_change= new_coeff - old_coeff;
4407 }
4408 }
4409 }
4410
4411 run=0;
4412 rle_index=0;
4413 run2= run_tab[rle_index++];
4414 prev_level=0;
4415 prev_run=0;
4416
4417 for(i=start_i; i<64; i++){
4418 int j= perm_scantable[i];
4419 const int level= block[j];
4420 int change, old_coeff;
4421
4422 if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
4423 break;
4424
4425 if(level){
4426 if(level<0) old_coeff= qmul*level - qadd;
4427 else old_coeff= qmul*level + qadd;
4428 run2= run_tab[rle_index++]; //FIXME ! maybe after last
4429 }else{
4430 old_coeff=0;
4431 run2--;
4432 av_assert2(run2>=0 || i >= last_non_zero );
4433 }
4434
4435 for(change=-1; change<=1; change+=2){
4436 int new_level= level + change;
4437 int score, new_coeff, unquant_change;
4438
4439 score=0;
4440 if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
4441 continue;
4442
4443 if(new_level){
4444 if(new_level<0) new_coeff= qmul*new_level - qadd;
4445 else new_coeff= qmul*new_level + qadd;
4446 if(new_coeff >= 2048 || new_coeff <= -2048)
4447 continue;
4448 //FIXME check for overflow
4449
4450 if(level){
4451 if(level < 63 && level > -63){
4452 if(i < last_non_zero)
4453 score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
4454 - length[UNI_AC_ENC_INDEX(run, level+64)];
4455 else
4456 score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
4457 - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4458 }
4459 }else{
4460 av_assert2(FFABS(new_level)==1);
4461
4462 if(analyze_gradient){
4463 int g= d1[ scantable[i] ];
4464 if(g && (g^new_level) >= 0)
4465 continue;
4466 }
4467
4468 if(i < last_non_zero){
4469 int next_i= i + run2 + 1;
4470 int next_level= block[ perm_scantable[next_i] ] + 64;
4471
4472 if(next_level&(~127))
4473 next_level= 0;
4474
4475 if(next_i < last_non_zero)
4476 score += length[UNI_AC_ENC_INDEX(run, 65)]
4477 + length[UNI_AC_ENC_INDEX(run2, next_level)]
4478 - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4479 else
4480 score += length[UNI_AC_ENC_INDEX(run, 65)]
4481 + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4482 - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4483 }else{
4484 score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4485 if(prev_level){
4486 score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4487 - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4488 }
4489 }
4490 }
4491 }else{
4492 new_coeff=0;
4493 av_assert2(FFABS(level)==1);
4494
4495 if(i < last_non_zero){
4496 int next_i= i + run2 + 1;
4497 int next_level= block[ perm_scantable[next_i] ] + 64;
4498
4499 if(next_level&(~127))
4500 next_level= 0;
4501
4502 if(next_i < last_non_zero)
4503 score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4504 - length[UNI_AC_ENC_INDEX(run2, next_level)]
4505 - length[UNI_AC_ENC_INDEX(run, 65)];
4506 else
4507 score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4508 - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4509 - length[UNI_AC_ENC_INDEX(run, 65)];
4510 }else{
4511 score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4512 if(prev_level){
4513 score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4514 - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4515 }
4516 }
4517 }
4518
4519 score *= lambda;
4520
4521 unquant_change= new_coeff - old_coeff;
4522 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4523
4524 score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
4525 unquant_change);
4526 if(score<best_score){
4527 best_score= score;
4528 best_coeff= i;
4529 best_change= change;
4530 best_unquant_change= unquant_change;
4531 }
4532 }
4533 if(level){
4534 prev_level= level + 64;
4535 if(prev_level&(~127))
4536 prev_level= 0;
4537 prev_run= run;
4538 run=0;
4539 }else{
4540 run++;
4541 }
4542 }
4543
4544 if(best_change){
4545 int j= perm_scantable[ best_coeff ];
4546
4547 block[j] += best_change;
4548
4549 if(best_coeff > last_non_zero){
4550 last_non_zero= best_coeff;
4551 av_assert2(block[j]);
4552 }else{
4553 for(; last_non_zero>=start_i; last_non_zero--){
4554 if(block[perm_scantable[last_non_zero]])
4555 break;
4556 }
4557 }
4558
4559 run=0;
4560 rle_index=0;
4561 for(i=start_i; i<=last_non_zero; i++){
4562 const int level = block[perm_scantable[i]];
4563
4564 if(level){
4565 run_tab[rle_index++]=run;
4566 run=0;
4567 }else{
4568 run++;
4569 }
4570 }
4571
4572 s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4573 }else{
4574 break;
4575 }
4576 }
4577
4578 return last_non_zero;
4579 }
4580
4581 /**
4582 * Permute an 8x8 block according to permutation.
4583 * @param block the block which will be permuted according to
4584 * the given permutation vector
4585 * @param permutation the permutation vector
4586 * @param last the last non zero coefficient in scantable order, used to
4587 * speed the permutation up
4588 * @param scantable the used scantable, this is only used to speed the
4589 * permutation up, the block is not (inverse) permutated
4590 * to scantable order!
4591 */
4592 398775 void ff_block_permute(int16_t *block, const uint8_t *permutation,
4593 const uint8_t *scantable, int last)
4594 {
4595 int i;
4596 int16_t temp[64];
4597
4598
2/2
✓ Branch 0 taken 161381 times.
✓ Branch 1 taken 237394 times.
398775 if (last <= 0)
4599 161381 return;
4600 //FIXME it is ok but not clean and might fail for some permutations
4601 // if (permutation[1] == 1)
4602 // return;
4603
4604
2/2
✓ Branch 0 taken 7058131 times.
✓ Branch 1 taken 237394 times.
7295525 for (i = 0; i <= last; i++) {
4605 7058131 const int j = scantable[i];
4606 7058131 temp[j] = block[j];
4607 7058131 block[j] = 0;
4608 }
4609
4610
2/2
✓ Branch 0 taken 7058131 times.
✓ Branch 1 taken 237394 times.
7295525 for (i = 0; i <= last; i++) {
4611 7058131 const int j = scantable[i];
4612 7058131 const int perm_j = permutation[j];
4613 7058131 block[perm_j] = temp[j];
4614 }
4615 }
4616
4617 92798960 static int dct_quantize_c(MPVEncContext *const s,
4618 int16_t *block, int n,
4619 int qscale, int *overflow)
4620 {
4621 int i, last_non_zero, q, start_i;
4622 const int *qmat;
4623 const uint8_t *scantable;
4624 int bias;
4625 92798960 int max=0;
4626 unsigned int threshold1, threshold2;
4627
4628 92798960 s->fdsp.fdct(block);
4629
4630 92798960 denoise_dct(s, block);
4631
4632
2/2
✓ Branch 0 taken 83150070 times.
✓ Branch 1 taken 9648890 times.
92798960 if (s->c.mb_intra) {
4633 83150070 scantable = s->c.intra_scantable.scantable;
4634
2/2
✓ Branch 0 taken 8213580 times.
✓ Branch 1 taken 74936490 times.
83150070 if (!s->c.h263_aic) {
4635
2/2
✓ Branch 0 taken 4497464 times.
✓ Branch 1 taken 3716116 times.
8213580 if (n < 4)
4636 4497464 q = s->c.y_dc_scale;
4637 else
4638 3716116 q = s->c.c_dc_scale;
4639 8213580 q = q << 3;
4640 } else
4641 /* For AIC we skip quant/dequant of INTRADC */
4642 74936490 q = 1 << 3;
4643
4644 /* note: block[0] is assumed to be positive */
4645 83150070 block[0] = (block[0] + (q >> 1)) / q;
4646 83150070 start_i = 1;
4647 83150070 last_non_zero = 0;
4648
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];
4649 83150070 bias= s->intra_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4650 } else {
4651 9648890 scantable = s->c.inter_scantable.scantable;
4652 9648890 start_i = 0;
4653 9648890 last_non_zero = -1;
4654 9648890 qmat = s->q_inter_matrix[qscale];
4655 9648890 bias= s->inter_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4656 }
4657 92798960 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4658 92798960 threshold2= (threshold1<<1);
4659
2/2
✓ Branch 0 taken 4916978379 times.
✓ Branch 1 taken 15889453 times.
4932867832 for(i=63;i>=start_i;i--) {
4660 4916978379 const int j = scantable[i];
4661 4916978379 int64_t level = (int64_t)block[j] * qmat[j];
4662
4663
2/2
✓ Branch 0 taken 76909507 times.
✓ Branch 1 taken 4840068872 times.
4916978379 if(((uint64_t)(level+threshold1))>threshold2){
4664 76909507 last_non_zero = i;
4665 76909507 break;
4666 }else{
4667 4840068872 block[j]=0;
4668 }
4669 }
4670
2/2
✓ Branch 0 taken 1015914498 times.
✓ Branch 1 taken 92798960 times.
1108713458 for(i=start_i; i<=last_non_zero; i++) {
4671 1015914498 const int j = scantable[i];
4672 1015914498 int64_t level = (int64_t)block[j] * qmat[j];
4673
4674 // if( bias+level >= (1<<QMAT_SHIFT)
4675 // || bias-level >= (1<<QMAT_SHIFT)){
4676
2/2
✓ Branch 0 taken 382844904 times.
✓ Branch 1 taken 633069594 times.
1015914498 if(((uint64_t)(level+threshold1))>threshold2){
4677
2/2
✓ Branch 0 taken 189645170 times.
✓ Branch 1 taken 193199734 times.
382844904 if(level>0){
4678 189645170 level= (bias + level)>>QMAT_SHIFT;
4679 189645170 block[j]= level;
4680 }else{
4681 193199734 level= (bias - level)>>QMAT_SHIFT;
4682 193199734 block[j]= -level;
4683 }
4684 382844904 max |=level;
4685 }else{
4686 633069594 block[j]=0;
4687 }
4688 }
4689 92798960 *overflow= s->max_qcoeff < max; //overflow might have happened
4690
4691 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4692
2/2
✓ Branch 0 taken 398775 times.
✓ Branch 1 taken 92400185 times.
92798960 if (s->c.idsp.perm_type != FF_IDCT_PERM_NONE)
4693 398775 ff_block_permute(block, s->c.idsp.idct_permutation,
4694 scantable, last_non_zero);
4695
4696 92798960 return last_non_zero;
4697 }
4698