FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2026-06-16 12:54:33
Exec Total Coverage
Lines: 2013 2803 71.8%
Functions: 52 62 83.9%
Branches: 1155 1764 65.5%

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