FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2026-01-23 19:11:46
Exec Total Coverage
Lines: 2015 2827 71.3%
Functions: 52 62 83.9%
Branches: 1153 1778 64.8%

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