FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2026-10-17 12:12:06
Exec Total Coverage
Lines: 2022 2814 71.9%
Functions: 52 62 83.9%
Branches: 1161 1768 65.7%

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 3811 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 3811 FDCTDSPContext *fdsp = &s->fdsp;
115 int qscale;
116 3811 int shift = 0;
117
118
2/2
✓ Branch 0 taken 99561 times.
✓ Branch 1 taken 3811 times.
103372 for (qscale = qmin; qscale <= qmax; qscale++) {
119 int i;
120 int qscale2;
121
122
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 99530 times.
99561 if (s->c.q_scale_type) qscale2 = ff_mpeg2_non_linear_qscale[qscale];
123 99530 else qscale2 = qscale << 1;
124
125
2/2
✓ Branch 0 taken 98807 times.
✓ Branch 1 taken 754 times.
99561 if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
126 #if CONFIG_FAANDCT
127
1/2
✓ Branch 0 taken 98807 times.
✗ Branch 1 not taken.
98807 fdsp->fdct == ff_faandct ||
128 #endif /* CONFIG_FAANDCT */
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98807 times.
98807 fdsp->fdct == ff_jpeg_fdct_islow_10) {
130
2/2
✓ Branch 0 taken 48256 times.
✓ Branch 1 taken 754 times.
49010 for (i = 0; i < 64; i++) {
131 48256 const int j = s->c.idsp.idct_permutation[i];
132 48256 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 48256 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
140 }
141
2/2
✓ Branch 0 taken 98147 times.
✓ Branch 1 taken 660 times.
98807 } else if (fdsp->fdct == ff_fdct_ifast) {
142
2/2
✓ Branch 0 taken 6281408 times.
✓ Branch 1 taken 98147 times.
6379555 for (i = 0; i < 64; i++) {
143 6281408 const int j = s->c.idsp.idct_permutation[i];
144 6281408 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 6281408 qmat[qscale][i] = (int)((UINT64_C(2) << (QMAT_SHIFT + 14)) / den);
152 }
153 } else {
154
2/2
✓ Branch 0 taken 42240 times.
✓ Branch 1 taken 660 times.
42900 for (i = 0; i < 64; i++) {
155 42240 const int j = s->c.idsp.idct_permutation[i];
156 42240 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 42240 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
168 42240 qmat16[qscale][0][i] = (2 << QMAT_SHIFT_MMX) / den;
169
170
1/2
✓ Branch 0 taken 42240 times.
✗ Branch 1 not taken.
42240 if (qmat16[qscale][0][i] == 0 ||
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42240 times.
42240 qmat16[qscale][0][i] == 128 * 256)
172 ✗ qmat16[qscale][0][i] = 128 * 256 - 1;
173 42240 qmat16[qscale][1][i] =
174
2/2
✓ Branch 0 taken 28800 times.
✓ Branch 1 taken 13440 times.
42240 ROUNDED_DIV(bias * (1<<(16 - QUANT_BIAS_SHIFT)),
175 qmat16[qscale][0][i]);
176 }
177 }
178
179
2/2
✓ Branch 0 taken 6278013 times.
✓ Branch 1 taken 99561 times.
6377574 for (i = intra; i < 64; i++) {
180 6278013 int64_t max = 8191;
181
2/2
✓ Branch 0 taken 6188361 times.
✓ Branch 1 taken 89652 times.
6278013 if (fdsp->fdct == ff_fdct_ifast) {
182 6188361 max = (8191LL * ff_aanscales[i]) >> 14;
183 }
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6278013 times.
6278013 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
185 ✗ shift++;
186 }
187 }
188 }
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3811 times.
3811 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 3811 }
195
196 17730 static inline void update_qscale(MPVMainEncContext *const m)
197 {
198 17730 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 17730 s->c.qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
218 (FF_LAMBDA_SHIFT + 7);
219
2/2
✓ Branch 0 taken 17605 times.
✓ Branch 1 taken 125 times.
17730 s->c.qscale = av_clip(s->c.qscale, s->c.avctx->qmin, m->vbv_ignore_qmax ? 31 : s->c.avctx->qmax);
220 }
221
222 17730 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
223 FF_LAMBDA_SHIFT;
224 17730 }
225
226 1010 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 1010 times.
1010 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 1010 put_bits(pb, 1, 0);
237 1010 }
238
239 /**
240 * init s->c.cur_pic.qscale_table from s->lambda_table
241 */
242 850 static void init_qscale_tab(MPVEncContext *const s)
243 {
244 850 int8_t *const qscale_table = s->c.cur_pic.qscale_table;
245
246
2/2
✓ Branch 0 taken 259350 times.
✓ Branch 1 taken 850 times.
260200 for (int i = 0; i < s->c.mb_num; i++) {
247 259350 unsigned int lam = s->lambda_table[s->c.mb_index2xy[i]];
248 259350 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
249 259350 qscale_table[s->c.mb_index2xy[i]] = av_clip(qp, s->c.avctx->qmin,
250 259350 s->c.avctx->qmax);
251 }
252 850 }
253
254 3468 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 3468 COPY(c.pict_type);
259 3468 COPY(f_code);
260 3468 COPY(b_code);
261 3468 COPY(c.qscale);
262 3468 COPY(lambda);
263 3468 COPY(lambda2);
264 3468 COPY(c.frame_pred_frame_dct); // FIXME don't set in encode_header
265 3468 COPY(c.progressive_frame); // FIXME don't set in encode_header
266 3468 COPY(partitioned_frame); // FIXME don't set in encode_header
267 #undef COPY
268 3468 }
269
270 93 static av_cold void mpv_encode_init_static(void)
271 {
272
2/2
✓ Branch 0 taken 2976 times.
✓ Branch 1 taken 93 times.
3069 for (int i = -16; i < 16; i++)
273 2976 default_fcode_tab[i + MAX_MV] = 1;
274 93 }
275
276 /**
277 * Set the given MPVEncContext to defaults for encoding.
278 */
279 245 static av_cold void mpv_encode_defaults(MPVMainEncContext *const m)
280 {
281 245 MPVEncContext *const s = &m->s;
282 static AVOnce init_static_once = AV_ONCE_INIT;
283
284 245 ff_mpv_common_defaults(&s->c);
285
286 245 s->f_code = 1;
287 245 s->b_code = 1;
288
289
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 151 times.
245 if (!m->fcode_tab) {
290 94 m->fcode_tab = default_fcode_tab + MAX_MV;
291 94 ff_thread_once(&init_static_once, mpv_encode_init_static);
292 }
293
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 160 times.
245 if (!s->c.y_dc_scale_table) {
294 85 s->c.y_dc_scale_table =
295 85 s->c.c_dc_scale_table = ff_mpeg1_dc_scale_table;
296 }
297 245 }
298
299 318 av_cold void ff_dct_encode_init(MPVEncContext *const s)
300 {
301 318 s->dct_quantize = dct_quantize_c;
302
303 #if ARCH_X86
304 318 ff_dct_encode_init_x86(s);
305 #endif
306
307
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 282 times.
318 if (s->c.avctx->trellis)
308 36 s->dct_quantize = dct_quantize_trellis_c;
309 318 }
310
311 245 static av_cold void init_unquantize(MPVEncContext *const s2, AVCodecContext *avctx)
312 {
313 245 MpegEncContext *const s = &s2->c;
314 MPVUnquantDSPContext unquant_dsp_ctx;
315
316 245 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 174 times.
✓ Branch 1 taken 71 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 174 times.
245 if (s2->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
320 71 s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_mpeg2_intra;
321 71 s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter;
322
4/4
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 109 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 57 times.
174 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
323 117 s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_h263_intra;
324 117 s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_h263_inter;
325 } else {
326 57 s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_mpeg1_intra;
327 57 s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_mpeg1_inter;
328 }
329 245 }
330
331 245 static av_cold int me_cmp_init(MPVMainEncContext *const m, AVCodecContext *avctx)
332 {
333 245 MPVEncContext *const s = &m->s;
334 MECmpContext mecc;
335 me_cmp_func me_cmp[6];
336 int ret;
337
338 245 ff_me_cmp_init(&mecc, avctx);
339 245 ret = ff_me_init(&s->me, avctx, &mecc, 1);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (ret < 0)
341 ✗ return ret;
342 245 ret = ff_set_cmp(&mecc, me_cmp, m->frame_skip_cmp, 1);
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (ret < 0)
344 ✗ return ret;
345 245 m->frame_skip_cmp_fn = me_cmp[1];
346
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 228 times.
245 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 245 s->sum_abs_dctelem = mecc.sum_abs_dctelem;
357
358 245 s->sse_cmp[0] = mecc.sse[0];
359 245 s->sse_cmp[1] = mecc.sse[1];
360 245 s->sad_cmp[0] = mecc.sad[0];
361 245 s->sad_cmp[1] = mecc.sad[1];
362
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 241 times.
245 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 241 s->n_sse_cmp[0] = mecc.sse[0];
367 241 s->n_sse_cmp[1] = mecc.sse[1];
368 }
369
370 245 return 0;
371 }
372
373 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
374 245 static av_cold int init_matrices(MPVMainEncContext *const m, AVCodecContext *avctx)
375 {
376 245 MPVEncContext *const s = &m->s;
377
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 208 times.
245 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 245 times.
✗ Branch 2 not taken.
245 if (!ALLOCZ_ARRAYS(s->q_intra_matrix, 32, nb_matrices) ||
382
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 245 times.
245 !ALLOCZ_ARRAYS(s->q_intra_matrix16, 32, nb_matrices))
383 ✗ return AVERROR(ENOMEM);
384
385
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 208 times.
245 if (s->c.out_format == FMT_MJPEG) {
386 37 s->q_chroma_intra_matrix = s->q_intra_matrix + 32;
387 37 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 37 return 0;
392 } else {
393 208 s->q_chroma_intra_matrix = s->q_intra_matrix;
394 208 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
395 }
396
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 19 times.
208 if (!m->intra_only) {
397 189 s->q_inter_matrix = s->q_intra_matrix + 32;
398 189 s->q_inter_matrix16 = s->q_intra_matrix16 + 32;
399 }
400
401
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 135 times.
208 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4 &&
402
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 69 times.
73 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 95 times.
✓ Branch 1 taken 109 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 87 times.
204 } else if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
406 117 intra_matrix =
407 117 inter_matrix = ff_mpeg1_default_non_intra_matrix;
408 } else {
409 /* MPEG-1/2, SpeedHQ */
410 87 intra_matrix = ff_mpeg1_default_intra_matrix;
411 87 inter_matrix = ff_mpeg1_default_non_intra_matrix;
412 }
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208 times.
208 if (avctx->intra_matrix)
414 ✗ intra_matrix = avctx->intra_matrix;
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 208 times.
208 if (avctx->inter_matrix)
416 ✗ inter_matrix = avctx->inter_matrix;
417
418 /* init q matrix */
419
2/2
✓ Branch 0 taken 13312 times.
✓ Branch 1 taken 208 times.
13520 for (int i = 0; i < 64; i++) {
420 13312 int j = s->c.idsp.idct_permutation[i];
421
422 13312 s->c.intra_matrix[j] = s->c.chroma_intra_matrix[j] = intra_matrix[i];
423 13312 s->c.inter_matrix[j] = inter_matrix[i];
424 }
425
426 /* precompute matrix */
427 208 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 208 times.
208 if (ret < 0)
429 ✗ return ret;
430
431 208 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
432 208 s->c.intra_matrix, s->intra_quant_bias, avctx->qmin,
433 31, 1);
434
2/2
✓ Branch 0 taken 189 times.
✓ Branch 1 taken 19 times.
208 if (s->q_inter_matrix)
435 189 ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
436 189 s->c.inter_matrix, s->inter_quant_bias, avctx->qmin,
437 31, 0);
438
439 208 return 0;
440 }
441
442 245 static av_cold int init_buffers(MPVMainEncContext *const m)
443 {
444 245 MPVEncContext *const s = &m->s;
445 245 int has_b_frames = !!m->max_b_frames;
446 int16_t (*mv_table)[2];
447
448 /* Allocate MB type table */
449 245 unsigned mb_array_size = s->c.mb_stride * s->c.mb_height;
450 245 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 245 times.
245 if (!s->mb_type)
452 ✗ return AVERROR(ENOMEM);
453 245 s->mc_mb_var = s->mb_type + mb_array_size;
454 245 s->mb_var = s->mc_mb_var + mb_array_size;
455 245 s->mb_mean = (uint8_t*)(s->mb_var + mb_array_size);
456
457
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 245 times.
245 if (!FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size))
458 ✗ return AVERROR(ENOMEM);
459
460 245 unsigned mv_table_size = (s->c.mb_height + 2) * s->c.mb_stride + 1;
461 245 unsigned nb_mv_tables = 1 + 5 * has_b_frames;
462
2/2
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 73 times.
245 if (s->c.codec_id == AV_CODEC_ID_MPEG4 ||
463
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 156 times.
172 (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
464 89 nb_mv_tables += 8 * has_b_frames;
465 89 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 89 times.
89 if (!s->p_field_select_table[0])
467 ✗ return AVERROR(ENOMEM);
468 89 s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size;
469 }
470
471 245 mv_table = av_calloc(mv_table_size, nb_mv_tables * sizeof(*mv_table));
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (!mv_table)
473 ✗ return AVERROR(ENOMEM);
474 245 m->mv_table_base = mv_table;
475 245 mv_table += s->c.mb_stride + 1;
476
477 245 s->p_mv_table = mv_table;
478
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 199 times.
245 if (has_b_frames) {
479 46 s->b_forw_mv_table = mv_table += mv_table_size;
480 46 s->b_back_mv_table = mv_table += mv_table_size;
481 46 s->b_bidir_forw_mv_table = mv_table += mv_table_size;
482 46 s->b_bidir_back_mv_table = mv_table += mv_table_size;
483 46 s->b_direct_mv_table = mv_table += mv_table_size;
484
485
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 12 times.
46 if (s->p_field_select_table[1]) { // MPEG-4 or INTERLACED_ME above
486 34 uint8_t *field_select = s->p_field_select_table[1];
487
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34 times.
102 for (int j = 0; j < 2; j++) {
488
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 68 times.
204 for (int k = 0; k < 2; k++) {
489
2/2
✓ Branch 0 taken 272 times.
✓ Branch 1 taken 136 times.
408 for (int l = 0; l < 2; l++)
490 272 s->b_field_mv_table[j][k][l] = mv_table += mv_table_size;
491 136 s->b_field_select_table[j][k] = field_select += 2 * mv_table_size;
492 }
493 }
494 }
495 }
496
497 245 return 0;
498 }
499
500 245 static av_cold int init_slice_buffers(MPVMainEncContext *const m)
501 {
502 245 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 245 unsigned nb_slices = s->c.slice_context_count;
516 245 char *dct_error = NULL;
517
518
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 238 times.
245 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 245 const int y_size = s->c.b8_stride * (2 * s->c.mb_height + 1);
529 245 const int c_size = s->c.mb_stride * (s->c.mb_height + 1);
530 245 const int yc_size = y_size + 2 * c_size;
531 245 ptrdiff_t offset = 0;
532
533
2/2
✓ Branch 0 taken 357 times.
✓ Branch 1 taken 245 times.
602 for (unsigned i = 0; i < nb_slices; ++i) {
534 357 MPVEncContext *const s2 = s->c.enc_contexts[i];
535
536 357 s2->block = s2->blocks[0];
537
538
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 350 times.
357 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 128 times.
✓ Branch 1 taken 229 times.
357 if (s2->c.ac_val) {
545 128 s2->c.dc_val += offset + i;
546 128 s2->c.ac_val += offset;
547 128 offset += yc_size;
548 }
549 }
550 245 return 0;
551 }
552
553 /* init video encoder */
554 245 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
555 {
556 245 MPVMainEncContext *const m = avctx->priv_data;
557 245 MPVEncContext *const s = &m->s;
558 AVCPBProperties *cpb_props;
559 int gcd, ret;
560
561 245 mpv_encode_defaults(m);
562
563
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 223 times.
245 switch (avctx->pix_fmt) {
564 10 case AV_PIX_FMT_YUVJ444P:
565 case AV_PIX_FMT_YUV444P:
566 10 s->c.chroma_format = CHROMA_444;
567 10 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 223 case AV_PIX_FMT_YUVJ420P:
575 case AV_PIX_FMT_YUV420P:
576 223 s->c.chroma_format = CHROMA_420;
577 223 break;
578 }
579
580 245 avctx->bits_per_raw_sample = av_clip(avctx->bits_per_raw_sample, 0, 8);
581
582 245 m->bit_rate = avctx->bit_rate;
583 245 s->c.width = avctx->width;
584 245 s->c.height = avctx->height;
585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 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 245 m->gop_size = avctx->gop_size;
593 245 s->c.avctx = avctx;
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 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 245 times.
245 } 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 245 m->max_b_frames = avctx->max_b_frames;
604 245 s->c.codec_id = avctx->codec->id;
605
3/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 199 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 46 times.
245 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 245 s->c.quarter_sample = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
611 245 s->rtp_mode = !!s->rtp_payload_size;
612 245 m->user_specified_pts = AV_NOPTS_VALUE;
613
614
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 235 times.
245 if (m->gop_size <= 1) {
615 10 m->intra_only = 1;
616 10 m->gop_size = 12;
617 } else {
618 235 m->intra_only = 0;
619 }
620
621 /* Fixed QSCALE */
622 245 m->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
623
624 735 s->adaptive_quant = (avctx->lumi_masking ||
625
1/2
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
245 avctx->dark_masking ||
626
1/2
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
245 avctx->temporal_cplx_masking ||
627
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 5 times.
245 avctx->spatial_cplx_masking ||
628
1/2
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
240 avctx->p_masking ||
629
1/2
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
240 m->border_masking ||
630
3/4
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 228 times.
502 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
631
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 !m->fixed_qscale;
632
633 245 s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
634
635
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 243 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
245 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 245 times.
245 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 243 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
245 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 243 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
245 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 243 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
245 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 243 times.
245 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 243 times.
245 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 82 times.
✓ Branch 1 taken 163 times.
245 if (!m->fixed_qscale &&
696 82 avctx->bit_rate * av_q2d(avctx->time_base) >
697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 avctx->bit_rate_tolerance) {
698 ✗ double nbt = avctx->bit_rate * av_q2d(avctx->time_base) * 5;
699 ✗ av_log(avctx, AV_LOG_WARNING,
700 "bitrate tolerance %d too small for bitrate %"PRId64", overriding\n", avctx->bit_rate_tolerance, avctx->bit_rate);
701 ✗ if (nbt <= INT_MAX) {
702 ✗ avctx->bit_rate_tolerance = nbt;
703 } else
704 ✗ avctx->bit_rate_tolerance = INT_MAX;
705 }
706
707
3/4
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 212 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 33 times.
245 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 242 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
245 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 241 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
245 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 172 times.
✓ Branch 1 taken 73 times.
245 if ((s->c.codec_id == AV_CODEC_ID_MPEG4 ||
726
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 6 times.
172 s->c.codec_id == AV_CODEC_ID_H263 ||
727
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 163 times.
166 s->c.codec_id == AV_CODEC_ID_H263P) &&
728
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 (avctx->sample_aspect_ratio.num > 255 ||
729
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 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 239 times.
✓ Branch 1 taken 6 times.
245 if ((s->c.codec_id == AV_CODEC_ID_H263 ||
738
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 236 times.
239 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 238 times.
245 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 239 times.
✓ Branch 1 taken 6 times.
245 if ((s->c.codec_id == AV_CODEC_ID_H263 ||
751
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 3 times.
239 s->c.codec_id == AV_CODEC_ID_H263P ||
752
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 233 times.
236 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 241 times.
✓ Branch 1 taken 4 times.
245 if ((s->c.codec_id == AV_CODEC_ID_WMV1 ||
760
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 237 times.
241 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 228 times.
245 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 245 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
245 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 233 times.
245 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 242 times.
✓ Branch 1 taken 3 times.
245 if (m->scenechange_threshold < 1000000000 &&
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 242 times.
242 (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 244 times.
245 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 233 times.
245 if (avctx->slices > 1 &&
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 !(avctx->codec->capabilities & AV_CODEC_CAP_SLICE_THREADS)) {
808 ✗ av_log(avctx, AV_LOG_ERROR, "Multiple slices are not supported by this codec\n");
809 ✗ return AVERROR(EINVAL);
810 }
811
812
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 240 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
245 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 245 gcd = av_gcd(avctx->time_base.den, avctx->time_base.num);
819
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 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 174 times.
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 163 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 163 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 130 times.
✓ Branch 7 taken 33 times.
✓ Branch 8 taken 126 times.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 9 times.
✓ Branch 11 taken 117 times.
245 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 128 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
829 128 s->inter_quant_bias = 0;
830 } else {
831 117 s->intra_quant_bias = 0;
832 // (a - x / 4) / x
833 117 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
834 }
835
836
2/4
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 245 times.
245 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 245 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 67 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 37 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 73 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.
245 switch (avctx->codec->id) {
844 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
845 67 case AV_CODEC_ID_MPEG2VIDEO:
846 67 s->rtp_mode = 1;
847 av_fallthrough;
848 78 case AV_CODEC_ID_MPEG1VIDEO:
849 78 s->c.out_format = FMT_MPEG1;
850 78 s->c.low_delay = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
851
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 1 times.
78 avctx->delay = s->c.low_delay ? 0 : (m->max_b_frames + 1);
852 78 break;
853 #endif
854 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
855 37 case AV_CODEC_ID_MJPEG:
856 case AV_CODEC_ID_AMV:
857 37 s->c.out_format = FMT_MJPEG;
858 37 m->intra_only = 1; /* force intra only for jpeg */
859 37 avctx->delay = 0;
860 37 s->c.low_delay = 1;
861
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 4 times.
37 if (avctx->codec_id == AV_CODEC_ID_MJPEG) {
862 33 s->c.y_dc_scale_table =
863 33 s->c.c_dc_scale_table = ff_mpeg12_dc_scale_table[s->c.intra_dc_precision];
864 }
865 37 break;
866 #endif
867 9 case AV_CODEC_ID_SPEEDHQ:
868 9 s->c.out_format = FMT_SPEEDHQ;
869 9 m->intra_only = 1; /* force intra only for SHQ */
870 9 avctx->delay = 0;
871 9 s->c.low_delay = 1;
872 9 break;
873 8 case AV_CODEC_ID_H261:
874 8 s->c.out_format = FMT_H261;
875 8 avctx->delay = 0;
876 8 s->c.low_delay = 1;
877 8 s->rtp_mode = 0; /* Sliced encoding not supported */
878 8 break;
879 6 case AV_CODEC_ID_H263:
880 if (!CONFIG_H263_ENCODER)
881 return AVERROR_ENCODER_NOT_FOUND;
882
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),
883 s->c.width, s->c.height) == 8) {
884 ✗ av_log(avctx, AV_LOG_ERROR,
885 "The specified picture size of %dx%d is not valid for "
886 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
887 "352x288, 704x576, and 1408x1152. "
888 "Try H.263+.\n", s->c.width, s->c.height);
889 ✗ return AVERROR(EINVAL);
890 }
891 6 s->c.out_format = FMT_H263;
892 6 avctx->delay = 0;
893 6 s->c.low_delay = 1;
894 6 break;
895 3 case AV_CODEC_ID_H263P:
896 3 s->c.out_format = FMT_H263;
897 /* Fx */
898 3 s->c.h263_aic = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
899 3 s->modified_quant = s->c.h263_aic;
900 3 s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
901
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;
902 3 s->flipflop_rounding = 1;
903
904 /* /Fx */
905 /* These are just to be sure */
906 3 avctx->delay = 0;
907 3 s->c.low_delay = 1;
908 3 break;
909 7 case AV_CODEC_ID_FLV1:
910 7 s->c.out_format = FMT_H263;
911 7 s->me.unrestricted_mv = 1;
912 7 s->rtp_mode = 0; /* don't allow GOB */
913 7 avctx->delay = 0;
914 7 s->c.low_delay = 1;
915 7 break;
916 #if CONFIG_RV10_ENCODER
917 4 case AV_CODEC_ID_RV10:
918 4 s->c.out_format = FMT_H263;
919 4 avctx->delay = 0;
920 4 s->c.low_delay = 1;
921 4 break;
922 #endif
923 #if CONFIG_RV20_ENCODER
924 3 case AV_CODEC_ID_RV20:
925 3 m->encode_picture_header = ff_rv20_encode_picture_header;
926 3 s->c.out_format = FMT_H263;
927 3 avctx->delay = 0;
928 3 s->c.low_delay = 1;
929 3 s->modified_quant = 1;
930 // Set here to force allocation of dc_val;
931 // will be set later on a per-frame basis.
932 3 s->c.h263_aic = 1;
933 3 s->loop_filter = 1;
934 3 s->me.unrestricted_mv = 0;
935 3 break;
936 #endif
937 73 case AV_CODEC_ID_MPEG4:
938 73 s->c.out_format = FMT_H263;
939 73 s->c.h263_pred = 1;
940 73 s->me.unrestricted_mv = 1;
941 73 s->flipflop_rounding = 1;
942 73 s->c.low_delay = m->max_b_frames ? 0 : 1;
943
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 51 times.
73 avctx->delay = s->c.low_delay ? 0 : (m->max_b_frames + 1);
944 73 break;
945 4 case AV_CODEC_ID_MSMPEG4V2:
946 4 s->c.out_format = FMT_H263;
947 4 s->c.h263_pred = 1;
948 4 s->me.unrestricted_mv = 1;
949 4 s->c.msmpeg4_version = MSMP4_V2;
950 4 avctx->delay = 0;
951 4 s->c.low_delay = 1;
952 4 break;
953 5 case AV_CODEC_ID_MSMPEG4V3:
954 5 s->c.out_format = FMT_H263;
955 5 s->c.h263_pred = 1;
956 5 s->me.unrestricted_mv = 1;
957 5 s->c.msmpeg4_version = MSMP4_V3;
958 5 s->flipflop_rounding = 1;
959 5 avctx->delay = 0;
960 5 s->c.low_delay = 1;
961 5 break;
962 4 case AV_CODEC_ID_WMV1:
963 4 s->c.out_format = FMT_H263;
964 4 s->c.h263_pred = 1;
965 4 s->me.unrestricted_mv = 1;
966 4 s->c.msmpeg4_version = MSMP4_WMV1;
967 4 s->flipflop_rounding = 1;
968 4 avctx->delay = 0;
969 4 s->c.low_delay = 1;
970 4 break;
971 4 case AV_CODEC_ID_WMV2:
972 4 s->c.out_format = FMT_H263;
973 4 s->c.h263_pred = 1;
974 4 s->me.unrestricted_mv = 1;
975 4 s->c.msmpeg4_version = MSMP4_WMV2;
976 4 s->flipflop_rounding = 1;
977 4 avctx->delay = 0;
978 4 s->c.low_delay = 1;
979 4 break;
980 ✗ default:
981 ✗ av_unreachable("List contains all codecs using ff_mpv_encode_init()");
982 }
983
984 245 avctx->has_b_frames = !s->c.low_delay;
985
986 245 s->c.encoding = 1;
987
988 245 s->c.progressive_frame =
989
2/2
✓ Branch 0 taken 228 times.
✓ Branch 1 taken 17 times.
473 s->c.progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
990 AV_CODEC_FLAG_INTERLACED_ME) ||
991
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 4 times.
228 s->c.alternate_scan);
992
993
3/4
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 213 times.
✓ Branch 3 taken 32 times.
245 if (avctx->flags & AV_CODEC_FLAG_PSNR || avctx->mb_decision == FF_MB_DECISION_RD ||
994
2/4
✓ Branch 0 taken 213 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 213 times.
213 m->frame_skip_threshold || m->frame_skip_factor) {
995 32 s->frame_reconstruction_bitfield = (1 << AV_PICTURE_TYPE_I) |
996 (1 << AV_PICTURE_TYPE_P) |
997 (1 << AV_PICTURE_TYPE_B);
998
2/2
✓ Branch 0 taken 157 times.
✓ Branch 1 taken 56 times.
213 } else if (!m->intra_only) {
999 157 s->frame_reconstruction_bitfield = (1 << AV_PICTURE_TYPE_I) |
1000 (1 << AV_PICTURE_TYPE_P);
1001 } else {
1002 56 s->frame_reconstruction_bitfield = 0;
1003 }
1004
1005
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (m->lmin > m->lmax) {
1006 ✗ av_log(avctx, AV_LOG_WARNING, "Clipping lmin value to %d\n", m->lmax);
1007 ✗ m->lmin = m->lmax;
1008 }
1009
1010 /* ff_mpv_init_duplicate_contexts() will copy (memdup) the contents of the
1011 * main slice to the slice contexts, so we initialize various fields of it
1012 * before calling ff_mpv_init_duplicate_contexts(). */
1013 245 s->parent = m;
1014 245 ff_mpv_idct_init(&s->c);
1015 245 init_unquantize(s, avctx);
1016 245 ff_fdctdsp_init(&s->fdsp, avctx);
1017 245 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
1018 245 ff_pixblockdsp_init(&s->pdsp, 8);
1019 245 ret = me_cmp_init(m, avctx);
1020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (ret < 0)
1021 ✗ return ret;
1022
1023
1/2
✓ Branch 1 taken 245 times.
✗ Branch 2 not taken.
245 if (!(avctx->stats_out = av_mallocz(256)) ||
1024
1/2
✓ Branch 1 taken 245 times.
✗ Branch 2 not taken.
245 !(s->new_pic = av_frame_alloc()) ||
1025
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 245 times.
245 !(s->c.picture_pool = ff_mpv_alloc_pic_pool(0)))
1026 ✗ return AVERROR(ENOMEM);
1027
1028 245 ret = init_matrices(m, avctx);
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (ret < 0)
1030 ✗ return ret;
1031
1032 245 ff_dct_encode_init(s);
1033
1034
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 132 times.
245 if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263) {
1035 113 ff_h263_encode_init(m);
1036 #if CONFIG_MSMPEG4ENC
1037
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 96 times.
113 if (s->c.msmpeg4_version != MSMP4_UNUSED)
1038 17 ff_msmpeg4_encode_init(m);
1039 #endif
1040 }
1041
1042 245 s->c.slice_ctx_size = sizeof(*s);
1043 245 ret = ff_mpv_common_init(&s->c);
1044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (ret < 0)
1045 ✗ return ret;
1046 245 ret = init_buffers(m);
1047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (ret < 0)
1048 ✗ return ret;
1049
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 211 times.
245 if (s->c.slice_context_count > 1) {
1050 34 s->rtp_mode = 1;
1051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
34 if (avctx->codec_id == AV_CODEC_ID_H263P)
1052 ✗ s->h263_slice_structured = 1;
1053 }
1054 245 ret = ff_mpv_init_duplicate_contexts(&s->c);
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (ret < 0)
1056 ✗ return ret;
1057
1058 245 ret = init_slice_buffers(m);
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (ret < 0)
1060 ✗ return ret;
1061
1062 245 ret = ff_rate_control_init(m);
1063
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (ret < 0)
1064 ✗ return ret;
1065
1066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (m->b_frame_strategy == 2) {
1067 ✗ for (int i = 0; i < m->max_b_frames + 2; i++) {
1068 ✗ m->tmp_frames[i] = av_frame_alloc();
1069 ✗ if (!m->tmp_frames[i])
1070 ✗ return AVERROR(ENOMEM);
1071
1072 ✗ m->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
1073 ✗ m->tmp_frames[i]->width = s->c.width >> m->brd_scale;
1074 ✗ m->tmp_frames[i]->height = s->c.height >> m->brd_scale;
1075
1076 ✗ ret = av_frame_get_buffer(m->tmp_frames[i], 0);
1077 ✗ if (ret < 0)
1078 ✗ return ret;
1079 }
1080 }
1081
1082 245 cpb_props = ff_encode_add_cpb_side_data(avctx);
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 245 times.
245 if (!cpb_props)
1084 ✗ return AVERROR(ENOMEM);
1085 245 cpb_props->max_bitrate = avctx->rc_max_rate;
1086 245 cpb_props->min_bitrate = avctx->rc_min_rate;
1087 245 cpb_props->avg_bitrate = avctx->bit_rate;
1088 245 cpb_props->buffer_size = avctx->rc_buffer_size;
1089
1090 245 return 0;
1091 }
1092
1093 245 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
1094 {
1095 245 MPVMainEncContext *const m = avctx->priv_data;
1096 245 MPVEncContext *const s = &m->s;
1097
1098 245 ff_rate_control_uninit(&m->rc_context);
1099
1100 245 ff_mpv_common_end(&s->c);
1101 245 av_refstruct_pool_uninit(&s->c.picture_pool);
1102
1103
2/2
✓ Branch 0 taken 4165 times.
✓ Branch 1 taken 245 times.
4410 for (int i = 0; i < MPVENC_MAX_B_FRAMES + 1; i++) {
1104 4165 av_refstruct_unref(&m->input_picture[i]);
1105 4165 av_refstruct_unref(&m->reordered_input_picture[i]);
1106 }
1107
2/2
✓ Branch 0 taken 4410 times.
✓ Branch 1 taken 245 times.
4655 for (int i = 0; i < FF_ARRAY_ELEMS(m->tmp_frames); i++)
1108 4410 av_frame_free(&m->tmp_frames[i]);
1109
1110 245 av_frame_free(&s->new_pic);
1111
1112 245 av_freep(&avctx->stats_out);
1113
1114 245 av_freep(&m->mv_table_base);
1115 245 av_freep(&s->p_field_select_table[0]);
1116 245 av_freep(&m->dct_error_sum_base);
1117
1118 245 av_freep(&s->mb_type);
1119 245 av_freep(&s->lambda_table);
1120
1121 245 av_freep(&s->q_intra_matrix);
1122 245 av_freep(&s->q_intra_matrix16);
1123 245 av_freep(&s->dct_offset);
1124
1125 245 return 0;
1126 }
1127
1128 /* put block[] to dest[] */
1129 3374146 static inline void put_dct(MPVEncContext *const s,
1130 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1131 {
1132 3374146 s->c.dct_unquantize_intra(&s->c, block, i, qscale);
1133 3374146 s->c.idsp.idct_put(dest, line_size, block);
1134 3374146 }
1135
1136 22825572 static inline void add_dequant_dct(MPVEncContext *const s,
1137 int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
1138 {
1139
2/2
✓ Branch 0 taken 8321107 times.
✓ Branch 1 taken 14504465 times.
22825572 if (s->c.block_last_index[i] >= 0) {
1140 8321107 s->c.dct_unquantize_inter(&s->c, block, i, qscale);
1141
1142 8321107 s->c.idsp.idct_add(dest, line_size, block);
1143 }
1144 22825572 }
1145
1146 /**
1147 * Performs dequantization and IDCT (if necessary)
1148 */
1149 5385542 static void mpv_reconstruct_mb(MPVEncContext *const s, int16_t block[12][64])
1150 {
1151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5385542 times.
5385542 if (s->c.avctx->debug & FF_DEBUG_DCT_COEFF) {
1152 /* print DCT coefficients */
1153 ✗ av_log(s->c.avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->c.mb_x, s->c.mb_y);
1154 ✗ for (int i = 0; i < 6; i++) {
1155 ✗ for (int j = 0; j < 64; j++) {
1156 ✗ av_log(s->c.avctx, AV_LOG_DEBUG, "%5d",
1157 ✗ block[i][s->c.idsp.idct_permutation[j]]);
1158 }
1159 ✗ av_log(s->c.avctx, AV_LOG_DEBUG, "\n");
1160 }
1161 }
1162
1163
2/2
✓ Branch 0 taken 4237649 times.
✓ Branch 1 taken 1147893 times.
5385542 if ((1 << s->c.pict_type) & s->frame_reconstruction_bitfield) {
1164 4237649 uint8_t *dest_y = s->c.dest[0], *dest_cb = s->c.dest[1], *dest_cr = s->c.dest[2];
1165 int dct_linesize, dct_offset;
1166 4237649 const int linesize = s->c.cur_pic.linesize[0];
1167 4237649 const int uvlinesize = s->c.cur_pic.linesize[1];
1168 4237649 const int block_size = 8;
1169
1170 4237649 dct_linesize = linesize << s->c.interlaced_dct;
1171
2/2
✓ Branch 0 taken 4225873 times.
✓ Branch 1 taken 11776 times.
4237649 dct_offset = s->c.interlaced_dct ? linesize : linesize * block_size;
1172
1173
2/2
✓ Branch 0 taken 3688182 times.
✓ Branch 1 taken 549467 times.
4237649 if (!s->c.mb_intra) {
1174 /* No MC, as that was already done otherwise */
1175 3688182 add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->c.qscale);
1176 3688182 add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->c.qscale);
1177 3688182 add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->c.qscale);
1178 3688182 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->c.qscale);
1179
1180 if (!CONFIG_GRAY || !(s->c.avctx->flags & AV_CODEC_FLAG_GRAY)) {
1181
2/2
✓ Branch 0 taken 3339942 times.
✓ Branch 1 taken 348240 times.
3688182 if (s->c.chroma_y_shift) {
1182 3339942 add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->c.chroma_qscale);
1183 3339942 add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->c.chroma_qscale);
1184 } else {
1185 348240 dct_linesize >>= 1;
1186 348240 dct_offset >>= 1;
1187 348240 add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->c.chroma_qscale);
1188 348240 add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->c.chroma_qscale);
1189 348240 add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->c.chroma_qscale);
1190 348240 add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->c.chroma_qscale);
1191 }
1192 }
1193 } else {
1194 /* dct only in intra block */
1195 549467 put_dct(s, block[0], 0, dest_y , dct_linesize, s->c.qscale);
1196 549467 put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->c.qscale);
1197 549467 put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->c.qscale);
1198 549467 put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->c.qscale);
1199
1200 if (!CONFIG_GRAY || !(s->c.avctx->flags & AV_CODEC_FLAG_GRAY)) {
1201
2/2
✓ Branch 0 taken 510795 times.
✓ Branch 1 taken 38672 times.
549467 if (s->c.chroma_y_shift) {
1202 510795 put_dct(s, block[4], 4, dest_cb, uvlinesize, s->c.chroma_qscale);
1203 510795 put_dct(s, block[5], 5, dest_cr, uvlinesize, s->c.chroma_qscale);
1204 } else {
1205 38672 dct_offset >>= 1;
1206 38672 dct_linesize >>= 1;
1207 38672 put_dct(s, block[4], 4, dest_cb, dct_linesize, s->c.chroma_qscale);
1208 38672 put_dct(s, block[5], 5, dest_cr, dct_linesize, s->c.chroma_qscale);
1209 38672 put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->c.chroma_qscale);
1210 38672 put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->c.chroma_qscale);
1211 }
1212 }
1213 }
1214 }
1215 5385542 }
1216
1217 61160 static int get_sae(const uint8_t *src, int ref, int stride)
1218 {
1219 int x,y;
1220 61160 int acc = 0;
1221
1222
2/2
✓ Branch 0 taken 978560 times.
✓ Branch 1 taken 61160 times.
1039720 for (y = 0; y < 16; y++) {
1223
2/2
✓ Branch 0 taken 15656960 times.
✓ Branch 1 taken 978560 times.
16635520 for (x = 0; x < 16; x++) {
1224 15656960 acc += FFABS(src[x + y * stride] - ref);
1225 }
1226 }
1227
1228 61160 return acc;
1229 }
1230
1231 198 static int get_intra_count(MPVEncContext *const s, const uint8_t *src,
1232 const uint8_t *ref, int stride)
1233 {
1234 int x, y, w, h;
1235 198 int acc = 0;
1236
1237 198 w = s->c.width & ~15;
1238 198 h = s->c.height & ~15;
1239
1240
2/2
✓ Branch 0 taken 2860 times.
✓ Branch 1 taken 198 times.
3058 for (y = 0; y < h; y += 16) {
1241
2/2
✓ Branch 0 taken 61160 times.
✓ Branch 1 taken 2860 times.
64020 for (x = 0; x < w; x += 16) {
1242 61160 int offset = x + y * stride;
1243 61160 int sad = s->sad_cmp[0](NULL, src + offset, ref + offset,
1244 stride, 16);
1245 61160 int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
1246 61160 int sae = get_sae(src + offset, mean, stride);
1247
1248 61160 acc += sae + 500 < sad;
1249 }
1250 }
1251 198 return acc;
1252 }
1253
1254 /**
1255 * Allocates new buffers for an AVFrame and copies the properties
1256 * from another AVFrame.
1257 */
1258 12388 static int prepare_picture(MPVEncContext *const s, AVFrame *f, const AVFrame *props_frame)
1259 {
1260 12388 AVCodecContext *avctx = s->c.avctx;
1261 int ret;
1262
1263 12388 f->width = avctx->width + 2 * EDGE_WIDTH;
1264 12388 f->height = avctx->height + 2 * EDGE_WIDTH;
1265
1266 12388 ret = ff_encode_alloc_frame(avctx, f);
1267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12388 times.
12388 if (ret < 0)
1268 ✗ return ret;
1269
1270 12388 ret = ff_mpv_pic_check_linesize(avctx, f, &s->c.linesize, &s->c.uvlinesize);
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12388 times.
12388 if (ret < 0)
1272 ✗ return ret;
1273
1274
2/2
✓ Branch 0 taken 37164 times.
✓ Branch 1 taken 12388 times.
49552 for (int i = 0; f->data[i]; i++) {
1275
2/2
✓ Branch 0 taken 24776 times.
✓ Branch 1 taken 12388 times.
37164 int offset = (EDGE_WIDTH >> (i ? s->c.chroma_y_shift : 0)) *
1276 37164 f->linesize[i] +
1277
2/2
✓ Branch 0 taken 24776 times.
✓ Branch 1 taken 12388 times.
37164 (EDGE_WIDTH >> (i ? s->c.chroma_x_shift : 0));
1278 37164 f->data[i] += offset;
1279 }
1280 12388 f->width = avctx->width;
1281 12388 f->height = avctx->height;
1282
1283 12388 ret = av_frame_copy_props(f, props_frame);
1284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12388 times.
12388 if (ret < 0)
1285 ✗ return ret;
1286
1287 12388 return 0;
1288 }
1289
1290 12638 static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg)
1291 {
1292 12638 MPVEncContext *const s = &m->s;
1293 12638 MPVPicture *pic = NULL;
1294 int64_t pts;
1295 12638 int display_picture_number = 0, ret;
1296 25276 int encoding_delay = m->max_b_frames ? m->max_b_frames
1297
2/2
✓ Branch 0 taken 2335 times.
✓ Branch 1 taken 10303 times.
12638 : (s->c.low_delay ? 0 : 1);
1298 12638 int flush_offset = 1;
1299 12638 int direct = 1;
1300
1301 av_assert1(!m->input_picture[0]);
1302
1303
2/2
✓ Branch 0 taken 12338 times.
✓ Branch 1 taken 300 times.
12638 if (pic_arg) {
1304 12338 pts = pic_arg->pts;
1305 12338 display_picture_number = m->input_picture_number++;
1306
1307
1/2
✓ Branch 0 taken 12338 times.
✗ Branch 1 not taken.
12338 if (pts != AV_NOPTS_VALUE) {
1308
2/2
✓ Branch 0 taken 2749 times.
✓ Branch 1 taken 9589 times.
12338 if (s->c.codec_id == AV_CODEC_ID_MPEG4 &&
1309
1/2
✓ Branch 0 taken 2749 times.
✗ Branch 1 not taken.
2749 (pts > INT64_MAX / 2 / s->c.avctx->time_base.num ||
1310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2749 times.
2749 pts < INT64_MIN / 2 / s->c.avctx->time_base.num)) {
1311 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "pts %"PRId64" is out of the supported range\n", pts);
1312 ✗ return AVERROR_PATCHWELCOME;
1313 }
1314
1315
2/2
✓ Branch 0 taken 12093 times.
✓ Branch 1 taken 245 times.
12338 if (m->user_specified_pts != AV_NOPTS_VALUE) {
1316 12093 int64_t last = m->user_specified_pts;
1317
1318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12093 times.
12093 if (pts <= last) {
1319 ✗ av_log(s->c.avctx, AV_LOG_ERROR,
1320 "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
1321 pts, last);
1322 ✗ return AVERROR(EINVAL);
1323 }
1324
1325
4/4
✓ Branch 0 taken 6133 times.
✓ Branch 1 taken 5960 times.
✓ Branch 2 taken 98 times.
✓ Branch 3 taken 6035 times.
12093 if (!s->c.low_delay && display_picture_number == 1)
1326 98 m->dts_delta = pts - last;
1327 }
1328 12338 m->user_specified_pts = pts;
1329 } else {
1330 ✗ if (m->user_specified_pts != AV_NOPTS_VALUE) {
1331 ✗ m->user_specified_pts =
1332 ✗ pts = m->user_specified_pts + 1;
1333 ✗ av_log(s->c.avctx, AV_LOG_INFO,
1334 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
1335 pts);
1336 } else {
1337 ✗ pts = display_picture_number;
1338 }
1339 }
1340
1341
2/2
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 11592 times.
12338 if (pic_arg->linesize[0] != s->c.linesize ||
1342
1/2
✓ Branch 0 taken 746 times.
✗ Branch 1 not taken.
746 pic_arg->linesize[1] != s->c.uvlinesize ||
1343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 746 times.
746 pic_arg->linesize[2] != s->c.uvlinesize)
1344 11592 direct = 0;
1345
4/4
✓ Branch 0 taken 9227 times.
✓ Branch 1 taken 3111 times.
✓ Branch 2 taken 1261 times.
✓ Branch 3 taken 7966 times.
12338 if ((s->c.width & 15) || (s->c.height & 15))
1346 4372 direct = 0;
1347
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 12072 times.
12338 if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
1348 266 direct = 0;
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12338 times.
12338 if (s->c.linesize & (STRIDE_ALIGN-1))
1350 ✗ direct = 0;
1351
1352 ff_dlog(s->c.avctx, "%d %d %td %td\n", pic_arg->linesize[0],
1353 pic_arg->linesize[1], s->c.linesize, s->c.uvlinesize);
1354
1355 12338 pic = av_refstruct_pool_get(s->c.picture_pool);
1356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12338 times.
12338 if (!pic)
1357 ✗ return AVERROR(ENOMEM);
1358
1359
2/2
✓ Branch 0 taken 674 times.
✓ Branch 1 taken 11664 times.
12338 if (direct) {
1360
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 674 times.
674 if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1361 ✗ goto fail;
1362 674 pic->shared = 1;
1363 } else {
1364 11664 ret = prepare_picture(s, pic->f, pic_arg);
1365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11664 times.
11664 if (ret < 0)
1366 ✗ goto fail;
1367
1368
2/2
✓ Branch 0 taken 34992 times.
✓ Branch 1 taken 11664 times.
46656 for (int i = 0; i < 3; i++) {
1369 34992 ptrdiff_t src_stride = pic_arg->linesize[i];
1370
2/2
✓ Branch 0 taken 23328 times.
✓ Branch 1 taken 11664 times.
34992 ptrdiff_t dst_stride = i ? s->c.uvlinesize : s->c.linesize;
1371
2/2
✓ Branch 0 taken 23328 times.
✓ Branch 1 taken 11664 times.
34992 int h_shift = i ? s->c.chroma_x_shift : 0;
1372
2/2
✓ Branch 0 taken 23328 times.
✓ Branch 1 taken 11664 times.
34992 int v_shift = i ? s->c.chroma_y_shift : 0;
1373 34992 int w = AV_CEIL_RSHIFT(s->c.width , h_shift);
1374 34992 int h = AV_CEIL_RSHIFT(s->c.height, v_shift);
1375 34992 const uint8_t *src = pic_arg->data[i];
1376 34992 int vpad = 16;
1377
1378
2/2
✓ Branch 0 taken 13797 times.
✓ Branch 1 taken 21195 times.
34992 if ( s->c.codec_id == AV_CODEC_ID_MPEG2VIDEO
1379
2/2
✓ Branch 0 taken 3075 times.
✓ Branch 1 taken 10722 times.
13797 && !s->c.progressive_sequence
1380
2/2
✓ Branch 0 taken 750 times.
✓ Branch 1 taken 2325 times.
3075 && FFALIGN(s->c.height, 32) - s->c.height > 16)
1381 750 vpad = 32;
1382
1383
2/2
✓ Branch 0 taken 34842 times.
✓ Branch 1 taken 150 times.
34992 if (!s->c.avctx->rc_buffer_size)
1384 34842 pic->f->data[i] += INPLACE_OFFSET;
1385 34992 uint8_t *dst = pic->f->data[i];
1386
1387
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 34679 times.
34992 if (src_stride == dst_stride)
1388 313 memcpy(dst, src, src_stride * h - src_stride + w);
1389 else {
1390 34679 int h2 = h;
1391 34679 uint8_t *dst2 = dst;
1392
2/2
✓ Branch 0 taken 5179180 times.
✓ Branch 1 taken 34679 times.
5213859 while (h2--) {
1393 5179180 memcpy(dst2, src, w);
1394 5179180 dst2 += dst_stride;
1395 5179180 src += src_stride;
1396 }
1397 }
1398
4/4
✓ Branch 0 taken 25659 times.
✓ Branch 1 taken 9333 times.
✓ Branch 2 taken 3783 times.
✓ Branch 3 taken 21876 times.
34992 if ((s->c.width & 15) || (s->c.height & (vpad-1))) {
1399 13116 s->mpvencdsp.draw_edges(dst, dst_stride,
1400 w, h,
1401 16 >> h_shift,
1402 vpad >> v_shift,
1403 EDGE_BOTTOM);
1404 }
1405 }
1406 }
1407
1408 12338 pic->display_picture_number = display_picture_number;
1409 12338 pic->f->pts = pts; // we set this here to avoid modifying pic_arg
1410
2/2
✓ Branch 0 taken 247 times.
✓ Branch 1 taken 53 times.
300 } else if (!m->reordered_input_picture[1]) {
1411 /* Flushing: When the above check is true, the encoder is about to run
1412 * out of frames to encode. Check if there are input_pictures left;
1413 * if so, ensure m->input_picture[0] contains the first picture.
1414 * A flush_offset != 1 will only happen if we did not receive enough
1415 * input frames. */
1416
2/2
✓ Branch 0 taken 492 times.
✓ Branch 1 taken 151 times.
643 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1417
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 396 times.
492 if (m->input_picture[flush_offset])
1418 96 break;
1419
1420 247 encoding_delay -= flush_offset - 1;
1421 }
1422
1423 /* shift buffer entries */
1424
2/2
✓ Branch 0 taken 202059 times.
✓ Branch 1 taken 12638 times.
214697 for (int i = flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
1425 202059 m->input_picture[i - flush_offset] = m->input_picture[i];
1426
2/2
✓ Branch 0 taken 12787 times.
✓ Branch 1 taken 12638 times.
25425 for (int i = MPVENC_MAX_B_FRAMES + 1 - flush_offset; i <= MPVENC_MAX_B_FRAMES; i++)
1427 12787 m->input_picture[i] = NULL;
1428
1429 12638 m->input_picture[encoding_delay] = pic;
1430
1431 12638 return 0;
1432 ✗ fail:
1433 ✗ av_refstruct_unref(&pic);
1434 ✗ return ret;
1435 }
1436
1437 ✗ static int skip_check(MPVMainEncContext *const m,
1438 const MPVPicture *p, const MPVPicture *ref)
1439 {
1440 ✗ MPVEncContext *const s = &m->s;
1441 ✗ int score = 0;
1442 ✗ int64_t score64 = 0;
1443
1444 ✗ for (int plane = 0; plane < 3; plane++) {
1445 ✗ const int stride = p->f->linesize[plane];
1446 ✗ const int bw = plane ? 1 : 2;
1447 ✗ for (int y = 0; y < s->c.mb_height * bw; y++) {
1448 ✗ for (int x = 0; x < s->c.mb_width * bw; x++) {
1449 ✗ const uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride);
1450 ✗ const uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1451 ✗ int v = m->frame_skip_cmp_fn(s, dptr, rptr, stride, 8);
1452
1453 ✗ switch (FFABS(m->frame_skip_exp)) {
1454 ✗ case 0: score = FFMAX(score, v); break;
1455 ✗ case 1: score += FFABS(v); break;
1456 ✗ case 2: score64 += v * (int64_t)v; break;
1457 ✗ case 3: score64 += FFABS(v * (int64_t)v * v); break;
1458 ✗ case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
1459 }
1460 }
1461 }
1462 }
1463 ✗ emms_c();
1464
1465 ✗ if (score)
1466 ✗ score64 = score;
1467 ✗ if (m->frame_skip_exp < 0)
1468 ✗ score64 = pow(score64 / (double)(s->c.mb_width * s->c.mb_height),
1469 ✗ -1.0/m->frame_skip_exp);
1470
1471 ✗ if (score64 < m->frame_skip_threshold)
1472 ✗ return 1;
1473 ✗ if (score64 < ((m->frame_skip_factor * (int64_t) s->lambda) >> 8))
1474 ✗ return 1;
1475 ✗ return 0;
1476 }
1477
1478 ✗ static int encode_frame(AVCodecContext *c, const AVFrame *frame, AVPacket *pkt)
1479 {
1480 int ret;
1481 ✗ int size = 0;
1482
1483 ✗ ret = avcodec_send_frame(c, frame);
1484 ✗ if (ret < 0)
1485 ✗ return ret;
1486
1487 do {
1488 ✗ ret = avcodec_receive_packet(c, pkt);
1489 ✗ if (ret >= 0) {
1490 ✗ size += pkt->size;
1491 ✗ av_packet_unref(pkt);
1492 ✗ } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1493 ✗ return ret;
1494 ✗ } while (ret >= 0);
1495
1496 ✗ return size;
1497 }
1498
1499 ✗ static int estimate_best_b_count(MPVMainEncContext *const m)
1500 {
1501 ✗ MPVEncContext *const s = &m->s;
1502 AVPacket *pkt;
1503 ✗ const int scale = m->brd_scale;
1504 ✗ int width = s->c.width >> scale;
1505 ✗ int height = s->c.height >> scale;
1506 int out_size, p_lambda, b_lambda, lambda2;
1507 ✗ int64_t best_rd = INT64_MAX;
1508 ✗ int best_b_count = -1;
1509 ✗ int ret = 0;
1510
1511 ✗ av_assert0(scale >= 0 && scale <= 3);
1512
1513 ✗ pkt = av_packet_alloc();
1514 ✗ if (!pkt)
1515 ✗ return AVERROR(ENOMEM);
1516
1517 ✗ p_lambda = m->last_lambda_for[AV_PICTURE_TYPE_P];
1518 //p_lambda * FFABS(s->c.avctx->b_quant_factor) + s->c.avctx->b_quant_offset;
1519 ✗ b_lambda = m->last_lambda_for[AV_PICTURE_TYPE_B];
1520 ✗ if (!b_lambda) // FIXME we should do this somewhere else
1521 ✗ b_lambda = p_lambda;
1522 ✗ lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1523 FF_LAMBDA_SHIFT;
1524
1525 ✗ for (int i = 0; i < m->max_b_frames + 2; i++) {
1526 ✗ const MPVPicture *pre_input_ptr = i ? m->input_picture[i - 1] :
1527 s->c.next_pic.ptr;
1528
1529 ✗ if (pre_input_ptr) {
1530 ✗ const AVFrame *const pre_input = pre_input_ptr->f;
1531
1532 ✗ s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[0],
1533 ✗ m->tmp_frames[i]->linesize[0],
1534 ✗ pre_input->data[0],
1535 ✗ pre_input->linesize[0],
1536 width, height);
1537 ✗ s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[1],
1538 ✗ m->tmp_frames[i]->linesize[1],
1539 ✗ pre_input->data[1],
1540 ✗ pre_input->linesize[1],
1541 width >> 1, height >> 1);
1542 ✗ s->mpvencdsp.shrink[scale](m->tmp_frames[i]->data[2],
1543 ✗ m->tmp_frames[i]->linesize[2],
1544 ✗ pre_input->data[2],
1545 ✗ pre_input->linesize[2],
1546 width >> 1, height >> 1);
1547 }
1548 }
1549
1550 ✗ for (int j = 0; j < m->max_b_frames + 1; j++) {
1551 AVCodecContext *c;
1552 ✗ int64_t rd = 0;
1553
1554 ✗ if (!m->input_picture[j])
1555 ✗ break;
1556
1557 ✗ c = avcodec_alloc_context3(NULL);
1558 ✗ if (!c) {
1559 ✗ ret = AVERROR(ENOMEM);
1560 ✗ goto fail;
1561 }
1562
1563 ✗ c->width = width;
1564 ✗ c->height = height;
1565 ✗ c->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_PSNR;
1566 ✗ c->flags |= s->c.avctx->flags & AV_CODEC_FLAG_QPEL;
1567 ✗ c->mb_decision = s->c.avctx->mb_decision;
1568 ✗ c->me_cmp = s->c.avctx->me_cmp;
1569 ✗ c->mb_cmp = s->c.avctx->mb_cmp;
1570 ✗ c->me_sub_cmp = s->c.avctx->me_sub_cmp;
1571 ✗ c->pix_fmt = AV_PIX_FMT_YUV420P;
1572 ✗ c->time_base = s->c.avctx->time_base;
1573 ✗ c->max_b_frames = m->max_b_frames;
1574
1575 ✗ ret = avcodec_open2(c, s->c.avctx->codec, NULL);
1576 ✗ if (ret < 0)
1577 ✗ goto fail;
1578
1579
1580 ✗ m->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1581 ✗ m->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1582
1583 ✗ out_size = encode_frame(c, m->tmp_frames[0], pkt);
1584 ✗ if (out_size < 0) {
1585 ✗ ret = out_size;
1586 ✗ goto fail;
1587 }
1588
1589 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1590
1591 ✗ for (int i = 0; i < m->max_b_frames + 1; i++) {
1592 ✗ int is_p = i % (j + 1) == j || i == m->max_b_frames;
1593
1594 ✗ m->tmp_frames[i + 1]->pict_type = is_p ?
1595 ✗ AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1596 ✗ m->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1597
1598 ✗ out_size = encode_frame(c, m->tmp_frames[i + 1], pkt);
1599 ✗ if (out_size < 0) {
1600 ✗ ret = out_size;
1601 ✗ goto fail;
1602 }
1603
1604 ✗ rd += (out_size * (uint64_t)lambda2) >> (FF_LAMBDA_SHIFT - 3);
1605 }
1606
1607 /* get the delayed frames */
1608 ✗ out_size = encode_frame(c, NULL, pkt);
1609 ✗ if (out_size < 0) {
1610 ✗ ret = out_size;
1611 ✗ goto fail;
1612 }
1613 ✗ rd += (out_size * (uint64_t)lambda2) >> (FF_LAMBDA_SHIFT - 3);
1614
1615 ✗ rd += c->error[0] + c->error[1] + c->error[2];
1616
1617 ✗ if (rd < best_rd) {
1618 ✗ best_rd = rd;
1619 ✗ best_b_count = j;
1620 }
1621
1622 ✗ fail:
1623 ✗ avcodec_free_context(&c);
1624 ✗ av_packet_unref(pkt);
1625 ✗ if (ret < 0) {
1626 ✗ best_b_count = ret;
1627 ✗ break;
1628 }
1629 }
1630
1631 ✗ av_packet_free(&pkt);
1632
1633 ✗ return best_b_count;
1634 }
1635
1636 /**
1637 * Determines whether an input picture is discarded or not
1638 * and if not determines the length of the next chain of B frames
1639 * and moves these pictures (including the P frame) into
1640 * reordered_input_picture.
1641 * input_picture[0] is always NULL when exiting this function, even on error;
1642 * reordered_input_picture[0] is always NULL when exiting this function on error.
1643 */
1644 12638 static int set_bframe_chain_length(MPVMainEncContext *const m)
1645 {
1646 12638 MPVEncContext *const s = &m->s;
1647
1648 /* Either nothing to do or can't do anything */
1649
4/4
✓ Branch 0 taken 11290 times.
✓ Branch 1 taken 1348 times.
✓ Branch 2 taken 300 times.
✓ Branch 3 taken 10990 times.
12638 if (m->reordered_input_picture[0] || !m->input_picture[0])
1650 1648 return 0;
1651
1652 /* set next picture type & ordering */
1653
2/4
✓ Branch 0 taken 10990 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10990 times.
10990 if (m->frame_skip_threshold || m->frame_skip_factor) {
1654 ✗ if (m->picture_in_gop_number < m->gop_size &&
1655 ✗ s->c.next_pic.ptr &&
1656 ✗ skip_check(m, m->input_picture[0], s->c.next_pic.ptr)) {
1657 // FIXME check that the gop check above is +-1 correct
1658 ✗ av_refstruct_unref(&m->input_picture[0]);
1659
1660 ✗ ff_vbv_update(m, 0);
1661
1662 ✗ return 0;
1663 }
1664 }
1665
1666 10990 if (/* m->picture_in_gop_number >= m->gop_size || */
1667
4/4
✓ Branch 0 taken 10745 times.
✓ Branch 1 taken 245 times.
✓ Branch 2 taken 2085 times.
✓ Branch 3 taken 8660 times.
10990 !s->c.next_pic.ptr || m->intra_only) {
1668 2330 m->reordered_input_picture[0] = m->input_picture[0];
1669 2330 m->input_picture[0] = NULL;
1670 2330 m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1671 2330 m->reordered_input_picture[0]->coded_picture_number =
1672 2330 m->coded_picture_number++;
1673 } else {
1674 8660 int b_frames = 0;
1675
1676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8660 times.
8660 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
1677 ✗ for (int i = 0; i < m->max_b_frames + 1; i++) {
1678 ✗ int pict_num = m->input_picture[0]->display_picture_number + i;
1679
1680 ✗ if (pict_num >= m->rc_context.num_entries)
1681 ✗ break;
1682 ✗ if (!m->input_picture[i]) {
1683 ✗ m->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1684 ✗ break;
1685 }
1686
1687 ✗ m->input_picture[i]->f->pict_type =
1688 ✗ m->rc_context.entry[pict_num].new_pict_type;
1689 }
1690 }
1691
1692
2/2
✓ Branch 0 taken 8504 times.
✓ Branch 1 taken 156 times.
8660 if (m->b_frame_strategy == 0) {
1693 8504 b_frames = m->max_b_frames;
1694
4/4
✓ Branch 0 taken 686 times.
✓ Branch 1 taken 7897 times.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 607 times.
8583 while (b_frames && !m->input_picture[b_frames])
1695 79 b_frames--;
1696
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 } else if (m->b_frame_strategy == 1) {
1697
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 156 times.
468 for (int i = 1; i < m->max_b_frames + 1; i++) {
1698
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 6 times.
312 if (m->input_picture[i] &&
1699
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 108 times.
306 m->input_picture[i]->b_frame_score == 0) {
1700 198 m->input_picture[i]->b_frame_score =
1701 198 get_intra_count(s,
1702 198 m->input_picture[i ]->f->data[0],
1703 198 m->input_picture[i - 1]->f->data[0],
1704 198 s->c.linesize) + 1;
1705 }
1706 }
1707 298 for (int i = 0;; i++) {
1708
2/2
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 43 times.
298 if (i >= m->max_b_frames + 1 ||
1709
2/2
✓ Branch 0 taken 251 times.
✓ Branch 1 taken 4 times.
255 !m->input_picture[i] ||
1710 251 m->input_picture[i]->b_frame_score - 1 >
1711
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 142 times.
251 s->c.mb_num / m->b_sensitivity) {
1712 156 b_frames = FFMAX(0, i - 1);
1713 156 break;
1714 }
1715 }
1716
1717 /* reset scores */
1718
2/2
✓ Branch 0 taken 245 times.
✓ Branch 1 taken 156 times.
401 for (int i = 0; i < b_frames + 1; i++)
1719 245 m->input_picture[i]->b_frame_score = 0;
1720 ✗ } else if (m->b_frame_strategy == 2) {
1721 ✗ b_frames = estimate_best_b_count(m);
1722 ✗ if (b_frames < 0) {
1723 ✗ av_refstruct_unref(&m->input_picture[0]);
1724 ✗ return b_frames;
1725 }
1726 }
1727
1728
2/2
✓ Branch 0 taken 2074 times.
✓ Branch 1 taken 6586 times.
8660 if (s->c.codec_id == AV_CODEC_ID_MPEG4)
1729
2/2
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 1771 times.
2074 while (b_frames &&
1730 303 m->input_picture[b_frames]->f->pts * s->c.avctx->time_base.num -
1731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 303 times.
303 s->c.last_non_b_time > UINT16_MAX)
1732 ✗ b_frames--;
1733
1734
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 8660 times.
10008 for (int i = b_frames - 1; i >= 0; i--) {
1735 1348 int type = m->input_picture[i]->f->pict_type;
1736
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1348 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1348 if (type && type != AV_PICTURE_TYPE_B)
1737 ✗ b_frames = i;
1738 }
1739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8660 times.
8660 if (m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1740 ✗ b_frames == m->max_b_frames) {
1741 ✗ av_log(s->c.avctx, AV_LOG_ERROR,
1742 "warning, too many B-frames in a row\n");
1743 }
1744
1745
2/2
✓ Branch 0 taken 603 times.
✓ Branch 1 taken 8057 times.
8660 if (m->picture_in_gop_number + b_frames >= m->gop_size) {
1746
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
603 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1747 ✗ m->gop_size > m->picture_in_gop_number) {
1748 ✗ b_frames = m->gop_size - m->picture_in_gop_number - 1;
1749 } else {
1750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 603 times.
603 if (s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
1751 ✗ b_frames = 0;
1752 603 m->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1753 }
1754 }
1755
1756
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8660 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8660 if ((s->c.avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
1757 ✗ m->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1758 ✗ b_frames--;
1759
1760 8660 m->reordered_input_picture[0] = m->input_picture[b_frames];
1761 8660 m->input_picture[b_frames] = NULL;
1762
2/2
✓ Branch 0 taken 8041 times.
✓ Branch 1 taken 619 times.
8660 if (m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1763 8041 m->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1764 8660 m->reordered_input_picture[0]->coded_picture_number =
1765 8660 m->coded_picture_number++;
1766
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 8660 times.
10008 for (int i = 0; i < b_frames; i++) {
1767 1348 m->reordered_input_picture[i + 1] = m->input_picture[i];
1768 1348 m->input_picture[i] = NULL;
1769 1348 m->reordered_input_picture[i + 1]->f->pict_type =
1770 AV_PICTURE_TYPE_B;
1771 1348 m->reordered_input_picture[i + 1]->coded_picture_number =
1772 1348 m->coded_picture_number++;
1773 }
1774 }
1775
1776 10990 return 0;
1777 }
1778
1779 12638 static int select_input_picture(MPVMainEncContext *const m)
1780 {
1781 12638 MPVEncContext *const s = &m->s;
1782 int ret;
1783
1784 av_assert1(!m->reordered_input_picture[0]);
1785
1786
2/2
✓ Branch 0 taken 202208 times.
✓ Branch 1 taken 12638 times.
214846 for (int i = 1; i <= MPVENC_MAX_B_FRAMES; i++)
1787 202208 m->reordered_input_picture[i - 1] = m->reordered_input_picture[i];
1788 12638 m->reordered_input_picture[MPVENC_MAX_B_FRAMES] = NULL;
1789
1790 12638 ret = set_bframe_chain_length(m);
1791 av_assert1(!m->input_picture[0]);
1792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12638 times.
12638 if (ret < 0)
1793 ✗ return ret;
1794
1795 12638 av_frame_unref(s->new_pic);
1796
1797
2/2
✓ Branch 0 taken 12338 times.
✓ Branch 1 taken 300 times.
12638 if (m->reordered_input_picture[0]) {
1798 12338 m->reordered_input_picture[0]->reference =
1799 12338 m->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_B;
1800
1801
4/4
✓ Branch 0 taken 11664 times.
✓ Branch 1 taken 674 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 11614 times.
12338 if (m->reordered_input_picture[0]->shared || s->c.avctx->rc_buffer_size) {
1802 // input is a shared pix, so we can't modify it -> allocate a new
1803 // one & ensure that the shared one is reusable
1804 724 av_frame_move_ref(s->new_pic, m->reordered_input_picture[0]->f);
1805
1806 724 ret = prepare_picture(s, m->reordered_input_picture[0]->f, s->new_pic);
1807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 724 times.
724 if (ret < 0)
1808 ✗ goto fail;
1809 } else {
1810 // input is not a shared pix -> reuse buffer for current_pix
1811 11614 ret = av_frame_ref(s->new_pic, m->reordered_input_picture[0]->f);
1812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11614 times.
11614 if (ret < 0)
1813 ✗ goto fail;
1814 // The input was stored INPLACE_OFFSET into the buffer, which
1815 // new_pic now points at. Point the frame back at the start of
1816 // the buffer for the reconstruction.
1817
2/2
✓ Branch 0 taken 34842 times.
✓ Branch 1 taken 11614 times.
46456 for (int i = 0; i < MPV_MAX_PLANES; i++)
1818 34842 m->reordered_input_picture[0]->f->data[i] -= INPLACE_OFFSET;
1819 }
1820 12338 s->c.cur_pic.ptr = m->reordered_input_picture[0];
1821 12338 m->reordered_input_picture[0] = NULL;
1822 av_assert1(s->c.mb_width == s->c.buffer_pools.alloc_mb_width);
1823 av_assert1(s->c.mb_height == s->c.buffer_pools.alloc_mb_height);
1824 av_assert1(s->c.mb_stride == s->c.buffer_pools.alloc_mb_stride);
1825 12338 ret = ff_mpv_alloc_pic_accessories(s->c.avctx, &s->c.cur_pic,
1826 &s->c.sc, &s->c.buffer_pools, s->c.mb_height);
1827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12338 times.
12338 if (ret < 0) {
1828 ✗ ff_mpv_unref_picture(&s->c.cur_pic);
1829 ✗ return ret;
1830 }
1831 12338 s->picture_number = s->c.cur_pic.ptr->display_picture_number;
1832
1833 }
1834 12638 return 0;
1835 ✗ fail:
1836 ✗ av_refstruct_unref(&m->reordered_input_picture[0]);
1837 ✗ return ret;
1838 }
1839
1840 12463 static void frame_end(MPVMainEncContext *const m)
1841 {
1842 12463 MPVEncContext *const s = &m->s;
1843
1844
2/2
✓ Branch 0 taken 4114 times.
✓ Branch 1 taken 8349 times.
12463 if (s->me.unrestricted_mv &&
1845
2/2
✓ Branch 0 taken 3512 times.
✓ Branch 1 taken 602 times.
4114 s->c.cur_pic.reference &&
1846
2/2
✓ Branch 0 taken 3511 times.
✓ Branch 1 taken 1 times.
3512 !m->intra_only) {
1847 3511 int hshift = s->c.chroma_x_shift;
1848 3511 int vshift = s->c.chroma_y_shift;
1849 3511 s->mpvencdsp.draw_edges(s->c.cur_pic.data[0],
1850 s->c.cur_pic.linesize[0],
1851 s->c.h_edge_pos, s->c.v_edge_pos,
1852 EDGE_WIDTH, EDGE_WIDTH,
1853 EDGE_TOP | EDGE_BOTTOM);
1854 3511 s->mpvencdsp.draw_edges(s->c.cur_pic.data[1],
1855 s->c.cur_pic.linesize[1],
1856 3511 s->c.h_edge_pos >> hshift,
1857 3511 s->c.v_edge_pos >> vshift,
1858 EDGE_WIDTH >> hshift,
1859 EDGE_WIDTH >> vshift,
1860 EDGE_TOP | EDGE_BOTTOM);
1861 3511 s->mpvencdsp.draw_edges(s->c.cur_pic.data[2],
1862 s->c.cur_pic.linesize[2],
1863 3511 s->c.h_edge_pos >> hshift,
1864 3511 s->c.v_edge_pos >> vshift,
1865 EDGE_WIDTH >> hshift,
1866 EDGE_WIDTH >> vshift,
1867 EDGE_TOP | EDGE_BOTTOM);
1868 }
1869
1870 12463 m->last_pict_type = s->c.pict_type;
1871 12463 m->last_lambda_for[s->c.pict_type] = s->c.cur_pic.ptr->f->quality;
1872
2/2
✓ Branch 0 taken 11115 times.
✓ Branch 1 taken 1348 times.
12463 if (s->c.pict_type != AV_PICTURE_TYPE_B)
1873 11115 m->last_non_b_pict_type = s->c.pict_type;
1874 12463 }
1875
1876 350 static void update_noise_reduction(MPVMainEncContext *const m)
1877 {
1878 350 MPVEncContext *const s = &m->s;
1879 int intra, i;
1880
1881
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 350 times.
1050 for (intra = 0; intra < 2; intra++) {
1882
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 682 times.
700 if (s->dct_count[intra] > (1 << 16)) {
1883
2/2
✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 18 times.
1170 for (i = 0; i < 64; i++) {
1884 1152 s->dct_error_sum[intra][i] >>= 1;
1885 }
1886 18 s->dct_count[intra] >>= 1;
1887 }
1888
1889
2/2
✓ Branch 0 taken 44800 times.
✓ Branch 1 taken 700 times.
45500 for (i = 0; i < 64; i++) {
1890 44800 s->dct_offset[intra][i] = (m->noise_reduction *
1891 44800 s->dct_count[intra] +
1892 44800 s->dct_error_sum[intra][i] / 2) /
1893 44800 (s->dct_error_sum[intra][i] + 1);
1894 }
1895 }
1896 350 }
1897
1898 12338 static void frame_start(MPVMainEncContext *const m)
1899 {
1900 12338 MPVEncContext *const s = &m->s;
1901
1902 12338 s->c.cur_pic.ptr->f->pict_type = s->c.pict_type;
1903
1904
2/2
✓ Branch 0 taken 10990 times.
✓ Branch 1 taken 1348 times.
12338 if (s->c.pict_type != AV_PICTURE_TYPE_B) {
1905 10990 ff_mpv_replace_picture(&s->c.last_pic, &s->c.next_pic);
1906 10990 ff_mpv_replace_picture(&s->c.next_pic, &s->c.cur_pic);
1907 }
1908
1909 av_assert2(!!m->noise_reduction == !!s->dct_error_sum);
1910
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 11988 times.
12338 if (s->dct_error_sum) {
1911 350 update_noise_reduction(m);
1912 }
1913 12338 }
1914
1915 12638 int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1916 const AVFrame *pic_arg, int *got_packet)
1917 {
1918 12638 MPVMainEncContext *const m = avctx->priv_data;
1919 12638 MPVEncContext *const s = &m->s;
1920 int stuffing_count, ret;
1921 12638 int context_count = s->c.slice_context_count;
1922
1923 12638 ff_mpv_unref_picture(&s->c.cur_pic);
1924
1925 12638 m->vbv_ignore_qmax = 0;
1926
1927 12638 m->picture_in_gop_number++;
1928
1929 12638 ret = load_input_picture(m, pic_arg);
1930
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12638 times.
12638 if (ret < 0)
1931 ✗ return ret;
1932
1933 12638 ret = select_input_picture(m);
1934
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12638 times.
12638 if (ret < 0)
1935 ✗ return ret;
1936
1937 /* output? */
1938
2/2
✓ Branch 0 taken 12338 times.
✓ Branch 1 taken 300 times.
12638 if (s->new_pic->data[0]) {
1939
4/4
✓ Branch 0 taken 11112 times.
✓ Branch 1 taken 1226 times.
✓ Branch 2 taken 10712 times.
✓ Branch 3 taken 400 times.
12338 int growing_buffer = context_count == 1 && !s->data_partitioning;
1940 24676 size_t pkt_size = 10000 + s->c.mb_width * s->c.mb_height *
1941
2/2
✓ Branch 0 taken 10712 times.
✓ Branch 1 taken 1626 times.
12338 (growing_buffer ? 64 : (MAX_MB_BYTES + 100));
1942
2/2
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 10897 times.
12338 if (CONFIG_MJPEG_ENCODER && avctx->codec_id == AV_CODEC_ID_MJPEG) {
1943 1441 ret = ff_mjpeg_add_icc_profile_size(avctx, s->new_pic, &pkt_size);
1944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441 times.
1441 if (ret < 0)
1945 ✗ return ret;
1946 1441 ret = ff_mjpeg_add_gain_map_size(avctx, s->mjpeg_ctx, s->new_pic, &pkt_size);
1947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441 times.
1441 if (ret < 0)
1948 ✗ return ret;
1949 }
1950
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12338 times.
12338 if ((ret = ff_alloc_packet(avctx, pkt, pkt_size)) < 0)
1951 ✗ return ret;
1952 12338 pkt->size = avctx->internal->byte_buffer_size - AV_INPUT_BUFFER_PADDING_SIZE;
1953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12338 times.
12338 if (s->mb_info) {
1954 ✗ s->mb_info_ptr = av_packet_new_side_data(pkt,
1955 AV_PKT_DATA_H263_MB_INFO,
1956 ✗ s->c.mb_width*s->c.mb_height*12);
1957 ✗ if (!s->mb_info_ptr)
1958 ✗ return AVERROR(ENOMEM);
1959 ✗ s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1960 }
1961
1962 12338 s->c.pict_type = s->new_pic->pict_type;
1963 12338 frame_start(m);
1964 12463 vbv_retry:
1965 12463 ret = encode_picture(m, pkt);
1966
2/2
✓ Branch 0 taken 10837 times.
✓ Branch 1 taken 1626 times.
12463 if (growing_buffer) {
1967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10837 times.
10837 av_assert0(s->pb.buf == avctx->internal->byte_buffer);
1968 10837 pkt->data = s->pb.buf;
1969 10837 pkt->size = avctx->internal->byte_buffer_size;
1970 }
1971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12463 times.
12463 if (ret < 0)
1972 ✗ return -1;
1973
1974 12463 frame_end(m);
1975
1976
2/2
✓ Branch 0 taken 1641 times.
✓ Branch 1 taken 10822 times.
12463 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) && s->c.out_format == FMT_MJPEG)
1977 1641 ff_mjpeg_encode_picture_trailer(&s->pb, m->header_bits);
1978
1979
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 12288 times.
12463 if (avctx->rc_buffer_size) {
1980 175 RateControlContext *rcc = &m->rc_context;
1981
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);
1982
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);
1983
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
1984
1985
2/2
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 50 times.
175 if (put_bits_count(&s->pb) > max_size &&
1986
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 s->lambda < m->lmax) {
1987 125 m->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
1988 (s->c.qscale + 1) / s->c.qscale);
1989
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (s->adaptive_quant) {
1990 ✗ for (int i = 0; i < s->c.mb_height * s->c.mb_stride; i++)
1991 ✗ s->lambda_table[i] =
1992 ✗ FFMAX(s->lambda_table[i] + min_step,
1993 s->lambda_table[i] * (s->c.qscale + 1) /
1994 s->c.qscale);
1995 }
1996 125 s->c.mb_skipped = 0; // done in frame_start()
1997 // done in encode_picture() so we must undo it
1998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (s->c.pict_type == AV_PICTURE_TYPE_P) {
1999 ✗ s->c.no_rounding ^= s->flipflop_rounding;
2000 }
2001
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if (s->c.pict_type != AV_PICTURE_TYPE_B) {
2002 125 s->c.time_base = s->c.last_time_base;
2003 125 s->c.last_non_b_time = s->c.time - s->c.pp_time;
2004 }
2005 125 m->vbv_ignore_qmax = 1;
2006 125 av_log(avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
2007 125 goto vbv_retry;
2008 }
2009
2010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 av_assert0(avctx->rc_max_rate);
2011 }
2012
2013
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12338 times.
12338 if (avctx->flags & AV_CODEC_FLAG_PASS1)
2014 ✗ ff_write_pass1_stats(m);
2015
2016
2/2
✓ Branch 0 taken 37014 times.
✓ Branch 1 taken 12338 times.
49352 for (int i = 0; i < MPV_MAX_PLANES; i++)
2017 37014 avctx->error[i] += s->encoding_error[i];
2018 12338 ff_encode_add_stats_side_data(pkt, s->c.cur_pic.ptr->f->quality,
2019 12338 s->encoding_error,
2020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12338 times.
12338 (avctx->flags&AV_CODEC_FLAG_PSNR) ? MPV_MAX_PLANES : 0,
2021 s->c.pict_type);
2022
2023 12338 if (avctx->flags & AV_CODEC_FLAG_PASS1)
2024 assert(put_bits_count(&s->pb) == m->header_bits + s->mv_bits +
2025 s->misc_bits + s->i_tex_bits +
2026 s->p_tex_bits);
2027 12338 flush_put_bits(&s->pb);
2028 12338 m->frame_bits = put_bits_count(&s->pb);
2029
2030 12338 stuffing_count = ff_vbv_update(m, m->frame_bits);
2031 12338 m->stuffing_bits = 8*stuffing_count;
2032
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 12288 times.
12338 if (stuffing_count) {
2033
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if (put_bytes_left(&s->pb, 0) < stuffing_count + 50) {
2034 ✗ av_log(avctx, AV_LOG_ERROR, "stuffing too large\n");
2035 ✗ return -1;
2036 }
2037
2038
1/3
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
50 switch (s->c.codec_id) {
2039 50 case AV_CODEC_ID_MPEG1VIDEO:
2040 case AV_CODEC_ID_MPEG2VIDEO:
2041
2/2
✓ Branch 0 taken 3663682 times.
✓ Branch 1 taken 50 times.
3663732 while (stuffing_count--) {
2042 3663682 put_bits(&s->pb, 8, 0);
2043 }
2044 50 break;
2045 ✗ case AV_CODEC_ID_MPEG4:
2046 ✗ put_bits(&s->pb, 16, 0);
2047 ✗ put_bits(&s->pb, 16, 0x1C3);
2048 ✗ stuffing_count -= 4;
2049 ✗ while (stuffing_count--) {
2050 ✗ put_bits(&s->pb, 8, 0xFF);
2051 }
2052 ✗ break;
2053 ✗ default:
2054 ✗ av_log(avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
2055 ✗ m->stuffing_bits = 0;
2056 }
2057 50 flush_put_bits(&s->pb);
2058 50 m->frame_bits = put_bits_count(&s->pb);
2059 }
2060
2061 /* update MPEG-1/2 vbv_delay for CBR */
2062
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 12288 times.
12338 if (avctx->rc_max_rate &&
2063
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 avctx->rc_min_rate == avctx->rc_max_rate &&
2064
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 s->c.out_format == FMT_MPEG1 &&
2065 50 90000LL * (avctx->rc_buffer_size - 1) <=
2066
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
50 avctx->rc_max_rate * 0xFFFFLL) {
2067 AVCPBProperties *props;
2068 size_t props_size;
2069
2070 int vbv_delay, min_delay;
2071 50 double inbits = avctx->rc_max_rate *
2072 25 av_q2d(avctx->time_base);
2073 25 int minbits = m->frame_bits - 8 *
2074 25 (m->vbv_delay_pos - 1);
2075 25 double bits = m->rc_context.buffer_index + minbits - inbits;
2076 25 uint8_t *const vbv_delay_ptr = s->pb.buf + m->vbv_delay_pos;
2077
2078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (bits < 0)
2079 ✗ av_log(avctx, AV_LOG_ERROR,
2080 "Internal error, negative bits\n");
2081
2082 av_assert1(s->c.repeat_first_field == 0);
2083
2084 25 vbv_delay = bits * 90000 / avctx->rc_max_rate;
2085 25 min_delay = (minbits * 90000LL + avctx->rc_max_rate - 1) /
2086 25 avctx->rc_max_rate;
2087
2088 25 vbv_delay = FFMAX(vbv_delay, min_delay);
2089
2090
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 av_assert0(vbv_delay < 0xFFFF);
2091
2092 25 vbv_delay_ptr[0] &= 0xF8;
2093 25 vbv_delay_ptr[0] |= vbv_delay >> 13;
2094 25 vbv_delay_ptr[1] = vbv_delay >> 5;
2095 25 vbv_delay_ptr[2] &= 0x07;
2096 25 vbv_delay_ptr[2] |= vbv_delay << 3;
2097
2098 25 props = av_cpb_properties_alloc(&props_size);
2099
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!props)
2100 ✗ return AVERROR(ENOMEM);
2101 25 props->vbv_delay = vbv_delay * 300;
2102
2103 25 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
2104 (uint8_t*)props, props_size);
2105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0) {
2106 ✗ av_freep(&props);
2107 ✗ return ret;
2108 }
2109 }
2110 12338 m->total_bits += m->frame_bits;
2111
2112 12338 pkt->pts = s->c.cur_pic.ptr->f->pts;
2113 12338 pkt->duration = s->c.cur_pic.ptr->f->duration;
2114
4/4
✓ Branch 0 taken 6232 times.
✓ Branch 1 taken 6106 times.
✓ Branch 2 taken 4884 times.
✓ Branch 3 taken 1348 times.
12338 if (!s->c.low_delay && s->c.pict_type != AV_PICTURE_TYPE_B) {
2115
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 4785 times.
4884 if (!s->c.cur_pic.ptr->coded_picture_number)
2116 99 pkt->dts = pkt->pts - m->dts_delta;
2117 else
2118 4785 pkt->dts = m->reordered_pts;
2119 4884 m->reordered_pts = pkt->pts;
2120 } else
2121 7454 pkt->dts = pkt->pts;
2122
2123 // the no-delay case is handled in generic code
2124
2/2
✓ Branch 0 taken 7953 times.
✓ Branch 1 taken 4385 times.
12338 if (avctx->codec->capabilities & AV_CODEC_CAP_DELAY) {
2125 7953 ret = ff_encode_reordered_opaque(avctx, pkt, s->c.cur_pic.ptr->f);
2126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7953 times.
7953 if (ret < 0)
2127 ✗ return ret;
2128 }
2129
2130
2/2
✓ Branch 0 taken 2980 times.
✓ Branch 1 taken 9358 times.
12338 if (s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)
2131 2980 pkt->flags |= AV_PKT_FLAG_KEY;
2132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12338 times.
12338 if (s->mb_info)
2133 ✗ av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
2134 } else {
2135 300 m->frame_bits = 0;
2136 }
2137
2138 12638 ff_mpv_unref_picture(&s->c.cur_pic);
2139
2140 av_assert1((m->frame_bits & 7) == 0);
2141
2142 12638 pkt->size = m->frame_bits / 8;
2143 12638 *got_packet = !!pkt->size;
2144 12638 return 0;
2145 }
2146
2147 ✗ static inline void dct_single_coeff_elimination(MPVEncContext *const s,
2148 int n, int threshold)
2149 {
2150 static const char tab[64] = {
2151 3, 2, 2, 1, 1, 1, 1, 1,
2152 1, 1, 1, 1, 1, 1, 1, 1,
2153 1, 1, 1, 1, 1, 1, 1, 1,
2154 0, 0, 0, 0, 0, 0, 0, 0,
2155 0, 0, 0, 0, 0, 0, 0, 0,
2156 0, 0, 0, 0, 0, 0, 0, 0,
2157 0, 0, 0, 0, 0, 0, 0, 0,
2158 0, 0, 0, 0, 0, 0, 0, 0
2159 };
2160 ✗ int score = 0;
2161 ✗ int run = 0;
2162 int i;
2163 ✗ int16_t *block = s->block[n];
2164 ✗ const int last_index = s->c.block_last_index[n];
2165 int skip_dc;
2166
2167 ✗ if (threshold < 0) {
2168 ✗ skip_dc = 0;
2169 ✗ threshold = -threshold;
2170 } else
2171 ✗ skip_dc = 1;
2172
2173 /* Are all we could set to zero already zero? */
2174 ✗ if (last_index <= skip_dc - 1)
2175 ✗ return;
2176
2177 ✗ for (i = 0; i <= last_index; i++) {
2178 ✗ const int j = s->c.intra_scantable.permutated[i];
2179 ✗ const int level = FFABS(block[j]);
2180 ✗ if (level == 1) {
2181 ✗ if (skip_dc && i == 0)
2182 ✗ continue;
2183 ✗ score += tab[run];
2184 ✗ run = 0;
2185 ✗ } else if (level > 1) {
2186 ✗ return;
2187 } else {
2188 ✗ run++;
2189 }
2190 }
2191 ✗ if (score >= threshold)
2192 ✗ return;
2193 ✗ for (i = skip_dc; i <= last_index; i++) {
2194 ✗ const int j = s->c.intra_scantable.permutated[i];
2195 ✗ block[j] = 0;
2196 }
2197 ✗ if (block[0])
2198 ✗ s->c.block_last_index[n] = 0;
2199 else
2200 ✗ s->c.block_last_index[n] = -1;
2201 }
2202
2203 ✗ static inline void clip_coeffs(const MPVEncContext *const s, int16_t block[],
2204 int last_index)
2205 {
2206 int i;
2207 ✗ const int maxlevel = s->max_qcoeff;
2208 ✗ const int minlevel = s->min_qcoeff;
2209 ✗ int overflow = 0;
2210
2211 ✗ if (s->c.mb_intra) {
2212 ✗ i = 1; // skip clipping of intra dc
2213 } else
2214 ✗ i = 0;
2215
2216 ✗ for (; i <= last_index; i++) {
2217 ✗ const int j = s->c.intra_scantable.permutated[i];
2218 ✗ int level = block[j];
2219
2220 ✗ if (level > maxlevel) {
2221 ✗ level = maxlevel;
2222 ✗ overflow++;
2223 ✗ } else if (level < minlevel) {
2224 ✗ level = minlevel;
2225 ✗ overflow++;
2226 }
2227
2228 ✗ block[j] = level;
2229 }
2230
2231 ✗ if (overflow && s->c.avctx->mb_decision == FF_MB_DECISION_SIMPLE)
2232 ✗ av_log(s->c.avctx, AV_LOG_INFO,
2233 "warning, clipping %d dct coefficients to %d..%d\n",
2234 overflow, minlevel, maxlevel);
2235 ✗ }
2236
2237 ✗ static void get_visual_weight(int16_t *weight, const uint8_t *ptr, int stride)
2238 {
2239 int x, y;
2240 // FIXME optimize
2241 ✗ for (y = 0; y < 8; y++) {
2242 ✗ for (x = 0; x < 8; x++) {
2243 int x2, y2;
2244 ✗ int sum = 0;
2245 ✗ int sqr = 0;
2246 ✗ int count = 0;
2247
2248 ✗ for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
2249 ✗ for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
2250 ✗ int v = ptr[x2 + y2 * stride];
2251 ✗ sum += v;
2252 ✗ sqr += v * v;
2253 ✗ count++;
2254 }
2255 }
2256 ✗ weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
2257 }
2258 }
2259 ✗ }
2260
2261 5621484 static av_always_inline void encode_mb_internal(MPVEncContext *const s,
2262 int motion_x, int motion_y,
2263 int mb_block_height,
2264 int mb_block_width,
2265 int mb_block_count,
2266 int chroma_x_shift,
2267 int chroma_y_shift,
2268 int chroma_format)
2269 {
2270 /* Interlaced DCT is only possible with MPEG-2 and MPEG-4
2271 * and neither of these encoders currently supports 444. */
2272 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2273 (s)->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2274 DECLARE_ALIGNED(16, int16_t, weight)[12][64];
2275 int16_t orig[12][64];
2276 5621484 const int mb_x = s->c.mb_x;
2277 5621484 const int mb_y = s->c.mb_y;
2278 int i;
2279 int skip_dct[12];
2280 5621484 int dct_offset = s->c.linesize * 8; // default for progressive frames
2281 5621484 int uv_dct_offset = s->c.uvlinesize * 8;
2282 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2283 ptrdiff_t wrap_y, wrap_c;
2284
2285
2/2
✓ Branch 0 taken 35970256 times.
✓ Branch 1 taken 5621484 times.
41591740 for (i = 0; i < mb_block_count; i++)
2286 35970256 skip_dct[i] = s->skipdct;
2287
2288
2/2
✓ Branch 0 taken 1318068 times.
✓ Branch 1 taken 4303416 times.
5621484 if (s->adaptive_quant) {
2289 1318068 const int last_qp = s->c.qscale;
2290 1318068 const int mb_xy = mb_x + mb_y * s->c.mb_stride;
2291
2292 1318068 s->lambda = s->lambda_table[mb_xy];
2293 1318068 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
2294 FF_LAMBDA_SHIFT;
2295
2296
2/2
✓ Branch 0 taken 278217 times.
✓ Branch 1 taken 1039851 times.
1318068 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
2297 278217 s->dquant = s->c.cur_pic.qscale_table[mb_xy] - last_qp;
2298
2299
1/2
✓ Branch 0 taken 278217 times.
✗ Branch 1 not taken.
278217 if (s->c.out_format == FMT_H263) {
2300 278217 s->dquant = av_clip(s->dquant, -2, 2);
2301
2302
1/2
✓ Branch 0 taken 278217 times.
✗ Branch 1 not taken.
278217 if (s->c.codec_id == AV_CODEC_ID_MPEG4) {
2303
2/2
✓ Branch 0 taken 259608 times.
✓ Branch 1 taken 18609 times.
278217 if (!s->c.mb_intra) {
2304
2/2
✓ Branch 0 taken 140594 times.
✓ Branch 1 taken 119014 times.
259608 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
2305
4/4
✓ Branch 0 taken 94668 times.
✓ Branch 1 taken 45926 times.
✓ Branch 2 taken 32940 times.
✓ Branch 3 taken 61728 times.
140594 if (s->dquant & 1 || s->c.mv_dir & MV_DIRECT)
2306 78866 s->dquant = 0;
2307 }
2308
2/2
✓ Branch 0 taken 53070 times.
✓ Branch 1 taken 206538 times.
259608 if (s->c.mv_type == MV_TYPE_8X8)
2309 53070 s->dquant = 0;
2310 }
2311 }
2312 }
2313 }
2314 1318068 ff_set_qscale(&s->c, last_qp + s->dquant);
2315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4303416 times.
4303416 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
2316 ✗ ff_set_qscale(&s->c, s->c.qscale + s->dquant);
2317
2318 5621484 wrap_y = s->c.linesize;
2319 5621484 wrap_c = s->c.uvlinesize;
2320 5621484 ptr_y = s->new_pic->data[0] +
2321 5621484 (mb_y * 16 * wrap_y) + mb_x * 16;
2322 5621484 ptr_cb = s->new_pic->data[1] +
2323 5621484 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2324 5621484 ptr_cr = s->new_pic->data[2] +
2325 5621484 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2326
2327
4/4
✓ Branch 0 taken 5609818 times.
✓ Branch 1 taken 11666 times.
✓ Branch 2 taken 34752 times.
✓ Branch 3 taken 5575066 times.
5621484 if ((mb_x * 16 + 16 > s->c.width || mb_y * 16 + 16 > s->c.height) &&
2328
2/2
✓ Branch 0 taken 46168 times.
✓ Branch 1 taken 250 times.
46418 s->c.codec_id != AV_CODEC_ID_AMV) {
2329 46168 uint8_t *ebuf = s->c.sc.edge_emu_buffer + 38 * wrap_y;
2330 46168 int cw = (s->c.width + chroma_x_shift) >> chroma_x_shift;
2331 46168 int ch = (s->c.height + chroma_y_shift) >> chroma_y_shift;
2332 46168 s->c.vdsp.emulated_edge_mc(ebuf, ptr_y,
2333 wrap_y, wrap_y,
2334 16, 16, mb_x * 16, mb_y * 16,
2335 s->c.width, s->c.height);
2336 46168 ptr_y = ebuf;
2337 46168 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2338 wrap_c, wrap_c,
2339 mb_block_width, mb_block_height,
2340 mb_x * mb_block_width, mb_y * mb_block_height,
2341 cw, ch);
2342 46168 ptr_cb = ebuf + 16 * wrap_y;
2343 46168 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2344 wrap_c, wrap_c,
2345 mb_block_width, mb_block_height,
2346 mb_x * mb_block_width, mb_y * mb_block_height,
2347 cw, ch);
2348 46168 ptr_cr = ebuf + 16 * wrap_y + 16;
2349 }
2350
2351
2/2
✓ Branch 0 taken 1505898 times.
✓ Branch 1 taken 4115586 times.
5621484 if (s->c.mb_intra) {
2352
6/6
✓ Branch 0 taken 533760 times.
✓ Branch 1 taken 972138 times.
✓ Branch 2 taken 414422 times.
✓ Branch 3 taken 119338 times.
✓ Branch 4 taken 317258 times.
✓ Branch 5 taken 1069302 times.
1505898 if (INTERLACED_DCT(s)) {
2353 int progressive_score, interlaced_score;
2354
2355 317258 s->c.interlaced_dct = 0;
2356 317258 progressive_score = s->ildct_cmp[1](s, ptr_y, NULL, wrap_y, 8) +
2357 317258 s->ildct_cmp[1](s, ptr_y + wrap_y * 8,
2358 NULL, wrap_y, 8) - 400;
2359
2360
2/2
✓ Branch 0 taken 298166 times.
✓ Branch 1 taken 19092 times.
317258 if (progressive_score > 0) {
2361 298166 interlaced_score = s->ildct_cmp[1](s, ptr_y,
2362 NULL, wrap_y * 2, 8) +
2363 298166 s->ildct_cmp[1](s, ptr_y + wrap_y,
2364 NULL, wrap_y * 2, 8);
2365
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 297782 times.
298166 if (progressive_score > interlaced_score) {
2366 384 s->c.interlaced_dct = 1;
2367
2368 384 dct_offset = wrap_y;
2369 384 uv_dct_offset = wrap_c;
2370 384 wrap_y <<= 1;
2371
3/4
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 199 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 185 times.
384 if (chroma_format == CHROMA_422 ||
2372 chroma_format == CHROMA_444)
2373 199 wrap_c <<= 1;
2374 }
2375 }
2376 }
2377
2378 1505898 s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
2379 1505898 s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
2380 1505898 s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
2381 1505898 s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
2382
2383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1505898 times.
1505898 if (s->c.avctx->flags & AV_CODEC_FLAG_GRAY) {
2384 ✗ skip_dct[4] = 1;
2385 ✗ skip_dct[5] = 1;
2386 } else {
2387 1505898 s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2388 1505898 s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2389
2/2
✓ Branch 0 taken 414422 times.
✓ Branch 1 taken 1091476 times.
1505898 if (chroma_format == CHROMA_422) {
2390 414422 s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2391 414422 s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2392
2/2
✓ Branch 0 taken 119338 times.
✓ Branch 1 taken 972138 times.
1091476 } else if (chroma_format == CHROMA_444) {
2393 119338 s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
2394 119338 s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
2395 119338 s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2396 119338 s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2397 119338 s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2398 119338 s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2399 }
2400 }
2401 } else {
2402 op_pixels_func (*op_pix)[4];
2403 qpel_mc_func (*op_qpix)[16];
2404 uint8_t *dest_y, *dest_cb, *dest_cr;
2405
2406 4115586 dest_y = s->c.dest[0];
2407 4115586 dest_cb = s->c.dest[1];
2408 4115586 dest_cr = s->c.dest[2];
2409
2410
4/4
✓ Branch 0 taken 983639 times.
✓ Branch 1 taken 3131947 times.
✓ Branch 2 taken 351275 times.
✓ Branch 3 taken 632364 times.
4115586 if ((!s->c.no_rounding) || s->c.pict_type == AV_PICTURE_TYPE_B) {
2411 3483222 op_pix = s->c.hdsp.put_pixels_tab;
2412 3483222 op_qpix = s->c.qdsp.put_qpel_pixels_tab;
2413 } else {
2414 632364 op_pix = s->c.hdsp.put_no_rnd_pixels_tab;
2415 632364 op_qpix = s->c.qdsp.put_no_rnd_qpel_pixels_tab;
2416 }
2417
2418
2/2
✓ Branch 0 taken 3773890 times.
✓ Branch 1 taken 341696 times.
4115586 if (s->c.mv_dir & MV_DIR_FORWARD) {
2419 3773890 ff_mpv_motion(&s->c, dest_y, dest_cb, dest_cr, 0,
2420 3773890 s->c.last_pic.data,
2421 op_pix, op_qpix);
2422 3773890 op_pix = s->c.hdsp.avg_pixels_tab;
2423 3773890 op_qpix = s->c.qdsp.avg_qpel_pixels_tab;
2424 }
2425
2/2
✓ Branch 0 taken 998580 times.
✓ Branch 1 taken 3117006 times.
4115586 if (s->c.mv_dir & MV_DIR_BACKWARD) {
2426 998580 ff_mpv_motion(&s->c, dest_y, dest_cb, dest_cr, 1,
2427 998580 s->c.next_pic.data,
2428 op_pix, op_qpix);
2429 }
2430
2431
5/6
✓ Branch 0 taken 348240 times.
✓ Branch 1 taken 3767346 times.
✓ Branch 2 taken 348240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 506154 times.
✓ Branch 5 taken 3609432 times.
4115586 if (INTERLACED_DCT(s)) {
2432 int progressive_score, interlaced_score;
2433
2434 506154 s->c.interlaced_dct = 0;
2435 506154 progressive_score = s->ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2436 506154 s->ildct_cmp[0](s, dest_y + wrap_y * 8,
2437 506154 ptr_y + wrap_y * 8,
2438 wrap_y, 8) - 400;
2439
2440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 506154 times.
506154 if (s->c.avctx->ildct_cmp == FF_CMP_VSSE)
2441 ✗ progressive_score -= 400;
2442
2443
2/2
✓ Branch 0 taken 445577 times.
✓ Branch 1 taken 60577 times.
506154 if (progressive_score > 0) {
2444 445577 interlaced_score = s->ildct_cmp[0](s, dest_y, ptr_y,
2445 wrap_y * 2, 8) +
2446 445577 s->ildct_cmp[0](s, dest_y + wrap_y,
2447 ptr_y + wrap_y,
2448 wrap_y * 2, 8);
2449
2450
2/2
✓ Branch 0 taken 12580 times.
✓ Branch 1 taken 432997 times.
445577 if (progressive_score > interlaced_score) {
2451 12580 s->c.interlaced_dct = 1;
2452
2453 12580 dct_offset = wrap_y;
2454 12580 uv_dct_offset = wrap_c;
2455 12580 wrap_y <<= 1;
2456
2/2
✓ Branch 0 taken 9034 times.
✓ Branch 1 taken 3546 times.
12580 if (chroma_format == CHROMA_422)
2457 9034 wrap_c <<= 1;
2458 }
2459 }
2460 }
2461
2462 4115586 s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2463 4115586 s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2464 4115586 s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2465 4115586 dest_y + dct_offset, wrap_y);
2466 4115586 s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2467 4115586 dest_y + dct_offset + 8, wrap_y);
2468
2469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4115586 times.
4115586 if (s->c.avctx->flags & AV_CODEC_FLAG_GRAY) {
2470 ✗ skip_dct[4] = 1;
2471 ✗ skip_dct[5] = 1;
2472 } else {
2473 4115586 s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2474 4115586 s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2475
2/2
✓ Branch 0 taken 348240 times.
✓ Branch 1 taken 3767346 times.
4115586 if (!chroma_y_shift) { /* 422 */
2476 348240 s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
2477 348240 dest_cb + uv_dct_offset, wrap_c);
2478 348240 s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
2479 348240 dest_cr + uv_dct_offset, wrap_c);
2480 }
2481 }
2482 /* pre quantization */
2483
2/2
✓ Branch 0 taken 3498169 times.
✓ Branch 1 taken 617417 times.
4115586 if (s->mc_mb_var[s->c.mb_stride * mb_y + mb_x] < 2 * s->c.qscale * s->c.qscale) {
2484 // FIXME optimize
2485
2/2
✓ Branch 1 taken 1382372 times.
✓ Branch 2 taken 2115797 times.
3498169 if (s->sad_cmp[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->c.qscale)
2486 1382372 skip_dct[0] = 1;
2487
2/2
✓ Branch 1 taken 1395799 times.
✓ Branch 2 taken 2102370 times.
3498169 if (s->sad_cmp[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->c.qscale)
2488 1395799 skip_dct[1] = 1;
2489 3498169 if (s->sad_cmp[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2490
2/2
✓ Branch 0 taken 1373433 times.
✓ Branch 1 taken 2124736 times.
3498169 wrap_y, 8) < 20 * s->c.qscale)
2491 1373433 skip_dct[2] = 1;
2492 3498169 if (s->sad_cmp[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2493
2/2
✓ Branch 0 taken 1338539 times.
✓ Branch 1 taken 2159630 times.
3498169 wrap_y, 8) < 20 * s->c.qscale)
2494 1338539 skip_dct[3] = 1;
2495
2/2
✓ Branch 1 taken 1710253 times.
✓ Branch 2 taken 1787916 times.
3498169 if (s->sad_cmp[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->c.qscale)
2496 1710253 skip_dct[4] = 1;
2497
2/2
✓ Branch 1 taken 1683564 times.
✓ Branch 2 taken 1814605 times.
3498169 if (s->sad_cmp[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->c.qscale)
2498 1683564 skip_dct[5] = 1;
2499
2/2
✓ Branch 0 taken 288057 times.
✓ Branch 1 taken 3210112 times.
3498169 if (!chroma_y_shift) { /* 422 */
2500 576114 if (s->sad_cmp[1](NULL, ptr_cb + uv_dct_offset,
2501 288057 dest_cb + uv_dct_offset,
2502
2/2
✓ Branch 0 taken 160089 times.
✓ Branch 1 taken 127968 times.
288057 wrap_c, 8) < 20 * s->c.qscale)
2503 160089 skip_dct[6] = 1;
2504 576114 if (s->sad_cmp[1](NULL, ptr_cr + uv_dct_offset,
2505 288057 dest_cr + uv_dct_offset,
2506
2/2
✓ Branch 0 taken 154194 times.
✓ Branch 1 taken 133863 times.
288057 wrap_c, 8) < 20 * s->c.qscale)
2507 154194 skip_dct[7] = 1;
2508 }
2509 }
2510 }
2511
2512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5621484 times.
5621484 if (s->quantizer_noise_shaping) {
2513 ✗ if (!skip_dct[0])
2514 ✗ get_visual_weight(weight[0], ptr_y , wrap_y);
2515 ✗ if (!skip_dct[1])
2516 ✗ get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2517 ✗ if (!skip_dct[2])
2518 ✗ get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2519 ✗ if (!skip_dct[3])
2520 ✗ get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2521 ✗ if (!skip_dct[4])
2522 ✗ get_visual_weight(weight[4], ptr_cb , wrap_c);
2523 ✗ if (!skip_dct[5])
2524 ✗ get_visual_weight(weight[5], ptr_cr , wrap_c);
2525 ✗ if (!chroma_y_shift) { /* 422 */
2526 ✗ if (!skip_dct[6])
2527 ✗ get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2528 wrap_c);
2529 ✗ if (!skip_dct[7])
2530 ✗ get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2531 wrap_c);
2532 }
2533 ✗ memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2534 }
2535
2536 /* DCT & quantize */
2537 av_assert2(s->c.out_format != FMT_MJPEG || s->c.qscale == 8);
2538 {
2539
2/2
✓ Branch 0 taken 35970256 times.
✓ Branch 1 taken 5621484 times.
41591740 for (i = 0; i < mb_block_count; i++) {
2540
2/2
✓ Branch 0 taken 26772013 times.
✓ Branch 1 taken 9198243 times.
35970256 if (!skip_dct[i]) {
2541 int overflow;
2542 26772013 s->c.block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->c.qscale, &overflow);
2543 // FIXME we could decide to change to quantizer instead of
2544 // clipping
2545 // JS: I don't think that would be a good idea it could lower
2546 // quality instead of improve it. Just INTRADC clipping
2547 // deserves changes in quantizer
2548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26772013 times.
26772013 if (overflow)
2549 ✗ clip_coeffs(s, s->block[i], s->c.block_last_index[i]);
2550 } else
2551 9198243 s->c.block_last_index[i] = -1;
2552 }
2553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5621484 times.
5621484 if (s->quantizer_noise_shaping) {
2554 ✗ for (i = 0; i < mb_block_count; i++) {
2555 ✗ if (!skip_dct[i]) {
2556 ✗ s->c.block_last_index[i] =
2557 ✗ dct_quantize_refine(s, s->block[i], weight[i],
2558 ✗ orig[i], i, s->c.qscale);
2559 }
2560 }
2561 }
2562
2563
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5621484 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5621484 if (s->luma_elim_threshold && !s->c.mb_intra)
2564 ✗ for (i = 0; i < 4; i++)
2565 ✗ dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2566
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5621484 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5621484 if (s->chroma_elim_threshold && !s->c.mb_intra)
2567 ✗ for (i = 4; i < mb_block_count; i++)
2568 ✗ dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2569
2570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5621484 times.
5621484 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2571 ✗ for (i = 0; i < mb_block_count; i++) {
2572 ✗ if (s->c.block_last_index[i] == -1)
2573 ✗ s->coded_score[i] = INT_MAX / 256;
2574 }
2575 }
2576 }
2577
2578
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5621484 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5621484 if ((s->c.avctx->flags & AV_CODEC_FLAG_GRAY) && s->c.mb_intra) {
2579 ✗ s->c.block_last_index[4] =
2580 ✗ s->c.block_last_index[5] = 0;
2581 ✗ s->block[4][0] =
2582 ✗ s->block[5][0] = (1024 + s->c.c_dc_scale / 2) / s->c.c_dc_scale;
2583 ✗ if (!chroma_y_shift) { /* 422 / 444 */
2584 ✗ for (i=6; i<12; i++) {
2585 ✗ s->c.block_last_index[i] = 0;
2586 ✗ s->block[i][0] = s->block[4][0];
2587 }
2588 }
2589 }
2590
2591 // non c quantize code returns incorrect block_last_index FIXME
2592
3/4
✓ Branch 0 taken 60000 times.
✓ Branch 1 taken 5561484 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 60000 times.
5621484 if (s->c.alternate_scan && s->dct_quantize != dct_quantize_c) {
2593 ✗ for (i = 0; i < mb_block_count; i++) {
2594 int j;
2595 ✗ if (s->c.block_last_index[i] > 0) {
2596 ✗ for (j = 63; j > 0; j--) {
2597 ✗ if (s->block[i][s->c.intra_scantable.permutated[j]])
2598 ✗ break;
2599 }
2600 ✗ s->c.block_last_index[i] = j;
2601 }
2602 }
2603 }
2604
2605 5621484 s->encode_mb(s, s->block, motion_x, motion_y);
2606 5621484 }
2607
2608 5621484 static void encode_mb(MPVEncContext *const s, int motion_x, int motion_y)
2609 {
2610
2/2
✓ Branch 0 taken 4739484 times.
✓ Branch 1 taken 882000 times.
5621484 if (s->c.chroma_format == CHROMA_420)
2611 4739484 encode_mb_internal(s, motion_x, motion_y, 8, 8, 6, 1, 1, CHROMA_420);
2612
2/2
✓ Branch 0 taken 762662 times.
✓ Branch 1 taken 119338 times.
882000 else if (s->c.chroma_format == CHROMA_422)
2613 762662 encode_mb_internal(s, motion_x, motion_y, 16, 8, 8, 1, 0, CHROMA_422);
2614 else
2615 119338 encode_mb_internal(s, motion_x, motion_y, 16, 16, 12, 0, 0, CHROMA_444);
2616 5621484 }
2617
2618 typedef struct MBBackup {
2619 struct {
2620 int mv[2][4][2];
2621 int last_mv[2][2][2];
2622 int mv_type, mv_dir;
2623 int mb_intra, mb_skipped;
2624 int qscale;
2625 int block_last_index[8];
2626 int interlaced_dct;
2627 } c;
2628 int mb_skip_run;
2629 int last_dc[3];
2630 int mv_bits, i_tex_bits, p_tex_bits, i_count, misc_bits, last_bits;
2631 int dquant;
2632 int esc3_level_length;
2633 int16_t (*block)[64];
2634 PutBitContext pb, pb2, tex_pb;
2635 } MBBackup;
2636
2637 #define COPY_CONTEXT(BEFORE, AFTER, DST_TYPE, SRC_TYPE) \
2638 static inline void BEFORE ##_context_before_encode(DST_TYPE *const d, \
2639 const SRC_TYPE *const s) \
2640 { \
2641 /* FIXME is memcpy faster than a loop? */ \
2642 memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
2643 \
2644 /* MPEG-1 */ \
2645 d->mb_skip_run = s->mb_skip_run; \
2646 for (int i = 0; i < 3; i++) \
2647 d->last_dc[i] = s->last_dc[i]; \
2648 \
2649 /* statistics */ \
2650 d->mv_bits = s->mv_bits; \
2651 d->i_tex_bits = s->i_tex_bits; \
2652 d->p_tex_bits = s->p_tex_bits; \
2653 d->i_count = s->i_count; \
2654 d->misc_bits = s->misc_bits; \
2655 d->last_bits = 0; \
2656 \
2657 d->c.mb_skipped = 0; \
2658 d->c.qscale = s->c.qscale; \
2659 d->dquant = s->dquant; \
2660 \
2661 d->esc3_level_length = s->esc3_level_length; \
2662 } \
2663 \
2664 static inline void AFTER ## _context_after_encode(DST_TYPE *const d, \
2665 const SRC_TYPE *const s, \
2666 int data_partitioning) \
2667 { \
2668 /* FIXME is memcpy faster than a loop? */ \
2669 memcpy(d->c.mv, s->c.mv, 2*4*2*sizeof(int)); \
2670 memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
2671 \
2672 /* MPEG-1 */ \
2673 d->mb_skip_run = s->mb_skip_run; \
2674 for (int i = 0; i < 3; i++) \
2675 d->last_dc[i] = s->last_dc[i]; \
2676 \
2677 /* statistics */ \
2678 d->mv_bits = s->mv_bits; \
2679 d->i_tex_bits = s->i_tex_bits; \
2680 d->p_tex_bits = s->p_tex_bits; \
2681 d->i_count = s->i_count; \
2682 d->misc_bits = s->misc_bits; \
2683 \
2684 d->c.mb_intra = s->c.mb_intra; \
2685 d->c.mb_skipped = s->c.mb_skipped; \
2686 d->c.mv_type = s->c.mv_type; \
2687 d->c.mv_dir = s->c.mv_dir; \
2688 d->pb = s->pb; \
2689 if (data_partitioning) { \
2690 d->pb2 = s->pb2; \
2691 d->tex_pb = s->tex_pb; \
2692 } \
2693 d->block = s->block; \
2694 for (int i = 0; i < 8; i++) \
2695 d->c.block_last_index[i] = s->c.block_last_index[i]; \
2696 d->c.interlaced_dct = s->c.interlaced_dct; \
2697 d->c.qscale = s->c.qscale; \
2698 \
2699 d->esc3_level_length = s->esc3_level_length; \
2700 }
2701
2702
6/6
✓ Branch 0 taken 4657353 times.
✓ Branch 1 taken 1552451 times.
✓ Branch 2 taken 232894 times.
✓ Branch 3 taken 769895 times.
✓ Branch 4 taken 8022312 times.
✓ Branch 5 taken 1002789 times.
28464232 COPY_CONTEXT(backup, save, MBBackup, MPVEncContext)
2703
6/6
✓ Branch 0 taken 7935915 times.
✓ Branch 1 taken 2645305 times.
✓ Branch 2 taken 141082 times.
✓ Branch 3 taken 408580 times.
✓ Branch 4 taken 4397296 times.
✓ Branch 5 taken 549662 times.
29957032 COPY_CONTEXT(reset, store, MPVEncContext, MBBackup)
2704
2705 2095643 static void encode_mb_hq(MPVEncContext *const s, MBBackup *const backup, MBBackup *const best,
2706 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2707 int *dmin, int *next_block, int motion_x, int motion_y)
2708 {
2709 int score;
2710 uint8_t *dest_backup[3];
2711
2712 2095643 reset_context_before_encode(s, backup);
2713
2714 2095643 s->block = s->blocks[*next_block];
2715 2095643 s->pb = pb[*next_block];
2716
2/2
✓ Branch 0 taken 376392 times.
✓ Branch 1 taken 1719251 times.
2095643 if (s->data_partitioning) {
2717 376392 s->pb2 = pb2 [*next_block];
2718 376392 s->tex_pb= tex_pb[*next_block];
2719 }
2720
2721
2/2
✓ Branch 0 taken 1127357 times.
✓ Branch 1 taken 968286 times.
2095643 if(*next_block){
2722 1127357 memcpy(dest_backup, s->c.dest, sizeof(s->c.dest));
2723 1127357 s->c.dest[0] = s->c.sc.rd_scratchpad;
2724 1127357 s->c.dest[1] = s->c.sc.rd_scratchpad + 16*s->c.linesize;
2725 1127357 s->c.dest[2] = s->c.sc.rd_scratchpad + 16*s->c.linesize + 8;
2726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1127357 times.
1127357 av_assert0(s->c.linesize >= 32); //FIXME
2727 }
2728
2729 2095643 encode_mb(s, motion_x, motion_y);
2730
2731 2095643 score= put_bits_count(&s->pb);
2732
2/2
✓ Branch 0 taken 376392 times.
✓ Branch 1 taken 1719251 times.
2095643 if (s->data_partitioning) {
2733 376392 score+= put_bits_count(&s->pb2);
2734 376392 score+= put_bits_count(&s->tex_pb);
2735 }
2736
2737
2/2
✓ Branch 0 taken 1718683 times.
✓ Branch 1 taken 376960 times.
2095643 if (s->c.avctx->mb_decision == FF_MB_DECISION_RD) {
2738 1718683 mpv_reconstruct_mb(s, s->block);
2739
2740 1718683 score *= s->lambda2;
2741 1718683 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2742 }
2743
2744
2/2
✓ Branch 0 taken 1127357 times.
✓ Branch 1 taken 968286 times.
2095643 if(*next_block){
2745 1127357 memcpy(s->c.dest, dest_backup, sizeof(s->c.dest));
2746 }
2747
2748
2/2
✓ Branch 0 taken 1002789 times.
✓ Branch 1 taken 1092854 times.
2095643 if(score<*dmin){
2749 1002789 *dmin= score;
2750 1002789 *next_block^=1;
2751
2752 1002789 save_context_after_encode(best, s, s->data_partitioning);
2753 }
2754 2095643 }
2755
2756 24213 static int sse(const MPVEncContext *const s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride)
2757 {
2758 24213 const uint32_t *sq = ff_square_tab + 256;
2759 24213 int acc=0;
2760 int x,y;
2761
2762
3/4
✓ Branch 0 taken 3735 times.
✓ Branch 1 taken 20478 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3735 times.
24213 if(w==16 && h==16)
2763 ✗ return s->sse_cmp[0](NULL, src1, src2, stride, 16);
2764
3/4
✓ Branch 0 taken 7470 times.
✓ Branch 1 taken 16743 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7470 times.
24213 else if(w==8 && h==8)
2765 ✗ return s->sse_cmp[1](NULL, src1, src2, stride, 8);
2766
2767
2/2
✓ Branch 0 taken 115316 times.
✓ Branch 1 taken 24213 times.
139529 for(y=0; y<h; y++){
2768
2/2
✓ Branch 0 taken 304204 times.
✓ Branch 1 taken 115316 times.
419520 for(x=0; x<w; x++){
2769 304204 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2770 }
2771 }
2772
2773 av_assert2(acc>=0);
2774
2775 24213 return acc;
2776 }
2777
2778 1718683 static int sse_mb(MPVEncContext *const s)
2779 {
2780 1718683 int w= 16;
2781 1718683 int h= 16;
2782 1718683 int chroma_mb_w = w >> s->c.chroma_x_shift;
2783 1718683 int chroma_mb_h = h >> s->c.chroma_y_shift;
2784
2785
2/2
✓ Branch 0 taken 4336 times.
✓ Branch 1 taken 1714347 times.
1718683 if (s->c.mb_x*16 + 16 > s->c.width ) w = s->c.width - s->c.mb_x*16;
2786
2/2
✓ Branch 0 taken 5382 times.
✓ Branch 1 taken 1713301 times.
1718683 if (s->c.mb_y*16 + 16 > s->c.height) h = s->c.height- s->c.mb_y*16;
2787
2788
4/4
✓ Branch 0 taken 1714347 times.
✓ Branch 1 taken 4336 times.
✓ Branch 2 taken 1710612 times.
✓ Branch 3 taken 3735 times.
1718683 if(w==16 && h==16)
2789 1710612 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,
2790 1710612 s->c.dest[0], s->c.linesize, 16) +
2791 1710612 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,
2792 3421224 s->c.dest[1], s->c.uvlinesize, chroma_mb_h) +
2793 1710612 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,
2794 1710612 s->c.dest[2], s->c.uvlinesize, chroma_mb_h);
2795 else
2796 8071 return sse(s, s->new_pic->data[0] + s->c.mb_x * 16 + s->c.mb_y * s->c.linesize * 16,
2797 8071 s->c.dest[0], w, h, s->c.linesize) +
2798 8071 sse(s, s->new_pic->data[1] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
2799 16142 s->c.dest[1], w >> s->c.chroma_x_shift, h >> s->c.chroma_y_shift, s->c.uvlinesize) +
2800 8071 sse(s, s->new_pic->data[2] + s->c.mb_x * chroma_mb_w + s->c.mb_y * s->c.uvlinesize * chroma_mb_h,
2801 8071 s->c.dest[2], w >> s->c.chroma_x_shift, h >> s->c.chroma_y_shift, s->c.uvlinesize);
2802 }
2803
2804 ✗ static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2805 ✗ MPVEncContext *const s = *(void**)arg;
2806
2807
2808 ✗ s->me.pre_pass = 1;
2809 ✗ s->me.dia_size = s->c.avctx->pre_dia_size;
2810 ✗ s->c.first_slice_line = 1;
2811 ✗ for (s->c.mb_y = s->c.end_mb_y - 1; s->c.mb_y >= s->c.start_mb_y; s->c.mb_y--) {
2812 ✗ for (s->c.mb_x = s->c.mb_width - 1; s->c.mb_x >=0 ; s->c.mb_x--)
2813 ✗ ff_pre_estimate_p_frame_motion(s, s->c.mb_x, s->c.mb_y);
2814 ✗ s->c.first_slice_line = 0;
2815 }
2816
2817 ✗ s->me.pre_pass = 0;
2818
2819 ✗ return 0;
2820 }
2821
2822 11215 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2823 11215 MPVEncContext *const s = *(void**)arg;
2824
2825 11215 s->me.dia_size = s->c.avctx->dia_size;
2826 11215 s->c.first_slice_line = 1;
2827
2/2
✓ Branch 0 taken 126570 times.
✓ Branch 1 taken 11215 times.
137785 for (s->c.mb_y = s->c.start_mb_y; s->c.mb_y < s->c.end_mb_y; s->c.mb_y++) {
2828 126570 s->c.mb_x = 0; //for block init below
2829 126570 ff_init_block_index(&s->c);
2830
2/2
✓ Branch 0 taken 2853361 times.
✓ Branch 1 taken 126570 times.
2979931 for (s->c.mb_x = 0; s->c.mb_x < s->c.mb_width; s->c.mb_x++) {
2831 2853361 s->c.block_index[0] += 2;
2832 2853361 s->c.block_index[1] += 2;
2833 2853361 s->c.block_index[2] += 2;
2834 2853361 s->c.block_index[3] += 2;
2835
2836 /* compute motion vector & mb_type and store in context */
2837
2/2
✓ Branch 0 taken 402556 times.
✓ Branch 1 taken 2450805 times.
2853361 if (s->c.pict_type == AV_PICTURE_TYPE_B)
2838 402556 ff_estimate_b_frame_motion(s, s->c.mb_x, s->c.mb_y);
2839 else
2840 2450805 ff_estimate_p_frame_motion(s, s->c.mb_x, s->c.mb_y);
2841 }
2842 126570 s->c.first_slice_line = 0;
2843 }
2844 11215 return 0;
2845 }
2846
2847 1125 static int mb_var_thread(AVCodecContext *c, void *arg){
2848 1125 MPVEncContext *const s = *(void**)arg;
2849
2850
2/2
✓ Branch 0 taken 14859 times.
✓ Branch 1 taken 1125 times.
15984 for (int mb_y = s->c.start_mb_y; mb_y < s->c.end_mb_y; mb_y++) {
2851
2/2
✓ Branch 0 taken 330578 times.
✓ Branch 1 taken 14859 times.
345437 for (int mb_x = 0; mb_x < s->c.mb_width; mb_x++) {
2852 330578 int xx = mb_x * 16;
2853 330578 int yy = mb_y * 16;
2854 330578 const uint8_t *pix = s->new_pic->data[0] + (yy * s->c.linesize) + xx;
2855 int varc;
2856 330578 int sum = s->mpvencdsp.pix_sum(pix, s->c.linesize);
2857
2858 330578 varc = (s->mpvencdsp.pix_norm1(pix, s->c.linesize) -
2859 330578 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2860
2861 330578 s->mb_var [s->c.mb_stride * mb_y + mb_x] = varc;
2862 330578 s->mb_mean[s->c.mb_stride * mb_y + mb_x] = (sum+128)>>8;
2863 330578 s->me.mb_var_sum_temp += varc;
2864 }
2865 }
2866 1125 return 0;
2867 }
2868
2869 335987 static void write_slice_end(MPVEncContext *const s)
2870 {
2871
2/2
✓ Branch 0 taken 6071 times.
✓ Branch 1 taken 329916 times.
335987 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4) {
2872
2/2
✓ Branch 0 taken 1756 times.
✓ Branch 1 taken 4315 times.
6071 if (s->partitioned_frame)
2873 1756 ff_mpeg4_merge_partitions(s);
2874
2875 6071 ff_mpeg4_stuffing(&s->pb);
2876 329916 } else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2877
2/2
✓ Branch 0 taken 6941 times.
✓ Branch 1 taken 322975 times.
329916 s->c.out_format == FMT_MJPEG) {
2878 6941 ff_mjpeg_encode_stuffing(s);
2879
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 322525 times.
322975 } else if (CONFIG_SPEEDHQ_ENCODER && s->c.out_format == FMT_SPEEDHQ) {
2880 450 ff_speedhq_end_slice(s);
2881 }
2882
2883 335987 flush_put_bits(&s->pb);
2884
2885
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 335987 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
335987 if ((s->c.avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
2886 ✗ s->misc_bits+= get_bits_diff(s);
2887 335987 }
2888
2889 ✗ static void write_mb_info(MPVEncContext *const s)
2890 {
2891 ✗ uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2892 ✗ int offset = put_bits_count(&s->pb);
2893 ✗ int mba = s->c.mb_x + s->c.mb_width * (s->c.mb_y % s->gob_index);
2894 ✗ int gobn = s->c.mb_y / s->gob_index;
2895 int pred_x, pred_y;
2896 if (CONFIG_H263_ENCODER)
2897 ✗ ff_h263_pred_motion(&s->c, 0, 0, &pred_x, &pred_y);
2898 ✗ bytestream_put_le32(&ptr, offset);
2899 ✗ bytestream_put_byte(&ptr, s->c.qscale);
2900 ✗ bytestream_put_byte(&ptr, gobn);
2901 ✗ bytestream_put_le16(&ptr, mba);
2902 ✗ bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2903 ✗ bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2904 /* 4MV not implemented */
2905 ✗ bytestream_put_byte(&ptr, 0); /* hmv2 */
2906 ✗ bytestream_put_byte(&ptr, 0); /* vmv2 */
2907 ✗ }
2908
2909 4075503 static void update_mb_info(MPVEncContext *const s)
2910 {
2911
1/2
✓ Branch 0 taken 4075503 times.
✗ Branch 1 not taken.
4075503 if (!s->mb_info)
2912 4075503 return;
2913 ✗ if (put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info) {
2914 ✗ s->mb_info_size += 12;
2915 ✗ s->prev_mb_info = s->last_mb_info;
2916 }
2917
2918 ✗ s->last_mb_info = put_bytes_count(&s->pb, 0);
2919 ✗ if (!s->mb_info_size)
2920 ✗ s->mb_info_size += 12;
2921 ✗ write_mb_info(s);
2922 }
2923
2924 4083285 int ff_mpv_reallocate_putbitbuffer(MPVEncContext *const s, size_t threshold, size_t size_increase)
2925 {
2926
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 4083253 times.
4083285 if (put_bytes_left(&s->pb, 0) < threshold
2927
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 && s->c.slice_context_count == 1
2928
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 && s->pb.buf == s->c.avctx->internal->byte_buffer) {
2929 32 int lastgob_pos = s->ptr_lastgob - s->pb.buf;
2930
2931 32 uint8_t *new_buffer = NULL;
2932 32 int new_buffer_size = 0;
2933
2934
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) {
2935 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "Cannot reallocate putbit buffer\n");
2936 ✗ return AVERROR(ENOMEM);
2937 }
2938
2939 32 emms_c();
2940
2941 32 av_fast_padded_malloc(&new_buffer, &new_buffer_size,
2942 32 s->c.avctx->internal->byte_buffer_size + size_increase);
2943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!new_buffer)
2944 ✗ return AVERROR(ENOMEM);
2945
2946 32 memcpy(new_buffer, s->c.avctx->internal->byte_buffer, s->c.avctx->internal->byte_buffer_size);
2947 32 av_free(s->c.avctx->internal->byte_buffer);
2948 32 s->c.avctx->internal->byte_buffer = new_buffer;
2949 32 s->c.avctx->internal->byte_buffer_size = new_buffer_size;
2950 32 rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
2951 32 s->ptr_lastgob = s->pb.buf + lastgob_pos;
2952 }
2953
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4083285 times.
4083285 if (put_bytes_left(&s->pb, 0) < threshold)
2954 ✗ return AVERROR(EINVAL);
2955 4083285 return 0;
2956 }
2957
2958 15931 static int encode_thread(AVCodecContext *c, void *arg){
2959 15931 MPVEncContext *const s = *(void**)arg;
2960 15931 int chr_h = 16 >> s->c.chroma_y_shift;
2961 int i;
2962 15931 MBBackup best_s = { 0 }, backup_s;
2963 uint8_t bit_buf[2][MAX_MB_BYTES];
2964 // + 2 because ff_copy_bits() overreads
2965 uint8_t bit_buf2[2][MAX_PB2_MB_SIZE + 2];
2966 uint8_t bit_buf_tex[2][MAX_AC_TEX_MB_SIZE + 2];
2967 PutBitContext pb[2], pb2[2], tex_pb[2];
2968
2969
2/2
✓ Branch 0 taken 31862 times.
✓ Branch 1 taken 15931 times.
47793 for(i=0; i<2; i++){
2970 31862 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2971 31862 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_PB2_MB_SIZE);
2972 31862 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_AC_TEX_MB_SIZE);
2973 }
2974
2975 15931 s->last_bits= put_bits_count(&s->pb);
2976 15931 s->mv_bits=0;
2977 15931 s->misc_bits=0;
2978 15931 s->i_tex_bits=0;
2979 15931 s->p_tex_bits=0;
2980 15931 s->i_count=0;
2981
2982
2/2
✓ Branch 0 taken 47793 times.
✓ Branch 1 taken 15931 times.
63724 for(i=0; i<3; i++){
2983 /* init last dc values */
2984 /* note: quant matrix value (8) is implied here */
2985 47793 s->last_dc[i] = 128 << s->c.intra_dc_precision;
2986
2987 47793 s->encoding_error[i] = 0;
2988 }
2989
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 15731 times.
15931 if (s->c.codec_id == AV_CODEC_ID_AMV) {
2990 200 s->last_dc[0] = 128 * 8 / 13;
2991 200 s->last_dc[1] = 128 * 8 / 14;
2992 200 s->last_dc[2] = 128 * 8 / 14;
2993 #if CONFIG_MPEG4_ENCODER
2994
2/2
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 15187 times.
15731 } else if (s->partitioned_frame) {
2995 av_assert1(s->c.codec_id == AV_CODEC_ID_MPEG4);
2996 544 ff_mpeg4_init_partitions(s);
2997 #endif
2998 }
2999 15931 s->mb_skip_run = 0;
3000 15931 memset(s->c.last_mv, 0, sizeof(s->c.last_mv));
3001
3002 15931 s->last_mv_dir = 0;
3003
3004 15931 s->c.resync_mb_x = 0;
3005 15931 s->c.resync_mb_y = 0;
3006 15931 s->c.first_slice_line = 1;
3007 15931 s->ptr_lastgob = s->pb.buf;
3008
2/2
✓ Branch 0 taken 176809 times.
✓ Branch 1 taken 15931 times.
192740 for (int mb_y_order = s->c.start_mb_y; mb_y_order < s->c.end_mb_y; mb_y_order++) {
3009 int mb_y;
3010
2/2
✓ Branch 0 taken 8100 times.
✓ Branch 1 taken 168709 times.
176809 if (CONFIG_SPEEDHQ_ENCODER && s->c.codec_id == AV_CODEC_ID_SPEEDHQ) {
3011 int first_in_slice;
3012 8100 mb_y = ff_speedhq_mb_y_order_to_mb(mb_y_order, s->c.mb_height, &first_in_slice);
3013
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)
3014 1350 ff_speedhq_end_slice(s);
3015 8100 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 1024;
3016 } else {
3017 168709 mb_y = mb_y_order;
3018 }
3019 176809 s->c.mb_x = 0;
3020 176809 s->c.mb_y = mb_y;
3021
3022 176809 ff_set_qscale(&s->c, s->c.qscale);
3023 176809 ff_init_block_index(&s->c);
3024
3025
2/2
✓ Branch 0 taken 4075503 times.
✓ Branch 1 taken 176809 times.
4252312 for (int mb_x = 0; mb_x < s->c.mb_width; mb_x++) {
3026 int mb_type, xy;
3027 // int d;
3028 4075503 int dmin= INT_MAX;
3029 int dir;
3030 4075503 int size_increase = s->c.avctx->internal->byte_buffer_size/4
3031 4075503 + s->c.mb_width*MAX_MB_BYTES;
3032
3033 4075503 ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
3034
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4075503 times.
4075503 if (put_bytes_left(&s->pb, 0) < MAX_MB_BYTES){
3035 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "encoded frame too large\n");
3036 ✗ return -1;
3037 }
3038
2/2
✓ Branch 0 taken 179550 times.
✓ Branch 1 taken 3895953 times.
4075503 if (s->data_partitioning) {
3039
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 ||
3040 179550 put_bytes_left(&s->tex_pb, 0) < MAX_MB_BYTES) {
3041 ✗ av_log(s->c.avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
3042 ✗ return -1;
3043 }
3044 }
3045
3046 4075503 s->c.mb_x = mb_x;
3047 4075503 s->c.mb_y = mb_y; // moved into loop, can get changed by H.261
3048 4075503 ff_update_block_index(&s->c, 8, 0, s->c.chroma_x_shift);
3049
3050
2/2
✓ Branch 0 taken 119790 times.
✓ Branch 1 taken 3955713 times.
4075503 if (CONFIG_H261_ENCODER && s->c.codec_id == AV_CODEC_ID_H261)
3051 119790 ff_h261_reorder_mb_index(s);
3052 4075503 xy = s->c.mb_y * s->c.mb_stride + s->c.mb_x;
3053 4075503 mb_type = s->mb_type[xy];
3054
3055 /* write gob / video packet header */
3056
2/2
✓ Branch 0 taken 1968675 times.
✓ Branch 1 taken 2106828 times.
4075503 if(s->rtp_mode){
3057 int current_packet_size, is_gob_start;
3058
3059 1968675 current_packet_size = put_bytes_count(&s->pb, 1)
3060 1968675 - (s->ptr_lastgob - s->pb.buf);
3061
3062 4492650 is_gob_start = s->rtp_payload_size &&
3063
4/4
✓ Branch 0 taken 555300 times.
✓ Branch 1 taken 1413375 times.
✓ Branch 2 taken 309968 times.
✓ Branch 3 taken 245332 times.
2278643 current_packet_size >= s->rtp_payload_size &&
3064
2/2
✓ Branch 0 taken 309818 times.
✓ Branch 1 taken 150 times.
309968 mb_y + mb_x > 0;
3065
3066
6/6
✓ Branch 0 taken 188580 times.
✓ Branch 1 taken 1780095 times.
✓ Branch 2 taken 88646 times.
✓ Branch 3 taken 99934 times.
✓ Branch 4 taken 3468 times.
✓ Branch 5 taken 85178 times.
1968675 if (s->c.start_mb_y == mb_y && mb_y > 0 && mb_x == 0) is_gob_start = 1;
3067
3068
5/5
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 1547759 times.
✓ Branch 2 taken 59850 times.
✓ Branch 3 taken 119700 times.
✓ Branch 4 taken 181966 times.
1968675 switch (s->c.codec_id) {
3069 59400 case AV_CODEC_ID_H263:
3070 case AV_CODEC_ID_H263P:
3071
1/2
✓ Branch 0 taken 59400 times.
✗ Branch 1 not taken.
59400 if (!s->h263_slice_structured)
3072
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;
3073 59400 break;
3074 1547759 case AV_CODEC_ID_MPEG2VIDEO:
3075
4/4
✓ Branch 0 taken 59614 times.
✓ Branch 1 taken 1488145 times.
✓ Branch 2 taken 54765 times.
✓ Branch 3 taken 4849 times.
1547759 if (s->c.mb_x == 0 && s->c.mb_y != 0) is_gob_start = 1;
3076 av_fallthrough;
3077 case AV_CODEC_ID_MPEG1VIDEO:
3078
3/4
✓ Branch 0 taken 59850 times.
✓ Branch 1 taken 1547759 times.
✓ Branch 2 taken 59850 times.
✗ Branch 3 not taken.
1607609 if (s->c.codec_id == AV_CODEC_ID_MPEG1VIDEO && s->c.mb_y >= 175 ||
3079
2/2
✓ Branch 0 taken 413680 times.
✓ Branch 1 taken 1193929 times.
1607609 s->mb_skip_run)
3080 413680 is_gob_start=0;
3081 1607609 break;
3082 119700 case AV_CODEC_ID_MJPEG:
3083
4/4
✓ Branch 0 taken 5700 times.
✓ Branch 1 taken 114000 times.
✓ Branch 2 taken 5300 times.
✓ Branch 3 taken 400 times.
119700 if (s->c.mb_x == 0 && s->c.mb_y != 0) is_gob_start = 1;
3084 119700 break;
3085 }
3086
3087
2/2
✓ Branch 0 taken 323524 times.
✓ Branch 1 taken 1645151 times.
1968675 if(is_gob_start){
3088
4/4
✓ Branch 0 taken 10589 times.
✓ Branch 1 taken 312935 times.
✓ Branch 2 taken 7121 times.
✓ Branch 3 taken 3468 times.
323524 if (s->c.start_mb_y != mb_y || mb_x != 0) {
3089 320056 write_slice_end(s);
3090
3091
4/4
✓ Branch 0 taken 3070 times.
✓ Branch 1 taken 316986 times.
✓ Branch 2 taken 1212 times.
✓ Branch 3 taken 1858 times.
320056 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4 && s->partitioned_frame)
3092 1212 ff_mpeg4_init_partitions(s);
3093 }
3094
3095 av_assert2((put_bits_count(&s->pb)&7) == 0);
3096 323524 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
3097
3098
4/4
✓ Branch 0 taken 434 times.
✓ Branch 1 taken 323090 times.
✓ Branch 2 taken 284 times.
✓ Branch 3 taken 150 times.
323524 if (s->error_rate && s->c.resync_mb_x + s->c.resync_mb_y > 0) {
3099 284 int r = put_bytes_count(&s->pb, 0) + s->picture_number + 16 + s->c.mb_x + s->c.mb_y;
3100 284 int d = 100 / s->error_rate;
3101
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 257 times.
284 if(r % d == 0){
3102 27 current_packet_size=0;
3103 27 s->pb.buf_ptr= s->ptr_lastgob;
3104 av_assert1(put_bits_ptr(&s->pb) == s->ptr_lastgob);
3105 }
3106 }
3107
3108
4/5
✓ Branch 0 taken 3322 times.
✓ Branch 1 taken 312358 times.
✓ Branch 2 taken 2544 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5300 times.
323524 switch (s->c.codec_id) {
3109 3322 case AV_CODEC_ID_MPEG4:
3110 if (CONFIG_MPEG4_ENCODER) {
3111 3322 ff_mpeg4_encode_video_packet_header(s);
3112 3322 ff_mpeg4_clean_buffers(&s->c);
3113 3322 ff_h263_mpeg4_reset_dc(s);
3114 }
3115 3322 break;
3116 312358 case AV_CODEC_ID_MPEG1VIDEO:
3117 case AV_CODEC_ID_MPEG2VIDEO:
3118 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3119 312358 ff_mpeg1_encode_slice_header(s);
3120 312358 ff_mpeg1_clean_buffers(s);
3121 }
3122 312358 break;
3123 #if CONFIG_H263P_ENCODER
3124 2544 case AV_CODEC_ID_H263P:
3125
1/2
✓ Branch 0 taken 2544 times.
✗ Branch 1 not taken.
2544 if (s->c.dc_val)
3126 2544 ff_h263_mpeg4_reset_dc(s);
3127 av_fallthrough;
3128 #endif
3129 case AV_CODEC_ID_H263:
3130 if (CONFIG_H263_ENCODER) {
3131
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)
3132 ✗ s->mb_info_size += 12;
3133
3134 2544 ff_h263_encode_gob_header(s, mb_y);
3135 2544 s->prev_mb_info = put_bits_count(&s->pb)/8;
3136 }
3137 2544 break;
3138 }
3139
3140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 323524 times.
323524 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS1) {
3141 ✗ int bits= put_bits_count(&s->pb);
3142 ✗ s->misc_bits+= bits - s->last_bits;
3143 ✗ s->last_bits= bits;
3144 }
3145
3146 323524 s->ptr_lastgob += current_packet_size;
3147 323524 s->c.first_slice_line = 1;
3148 323524 s->c.resync_mb_x = mb_x;
3149 323524 s->c.resync_mb_y = mb_y;
3150 }
3151 }
3152
3153
2/2
✓ Branch 0 taken 434482 times.
✓ Branch 1 taken 3641021 times.
4075503 if (s->c.resync_mb_x == s->c.mb_x &&
3154
2/2
✓ Branch 0 taken 8940 times.
✓ Branch 1 taken 425542 times.
434482 s->c.resync_mb_y+1 == s->c.mb_y)
3155 8940 s->c.first_slice_line = 0;
3156
3157 4075503 s->c.mb_skipped = 0;
3158 4075503 s->dquant=0; //only for QP_RD
3159
3160 4075503 update_mb_info(s);
3161
3162
4/4
✓ Branch 0 taken 3544552 times.
✓ Branch 1 taken 530951 times.
✓ Branch 2 taken 18711 times.
✓ Branch 3 taken 3525841 times.
4625165 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
3163 549662 int next_block=0;
3164 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3165
3166 549662 backup_context_before_encode(&backup_s, s);
3167 549662 backup_s.pb= s->pb;
3168
2/2
✓ Branch 0 taken 141082 times.
✓ Branch 1 taken 408580 times.
549662 if (s->data_partitioning) {
3169 141082 backup_s.pb2= s->pb2;
3170 141082 backup_s.tex_pb= s->tex_pb;
3171 }
3172
3173
2/2
✓ Branch 0 taken 307134 times.
✓ Branch 1 taken 242528 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_INTER){
3174 307134 s->c.mv_dir = MV_DIR_FORWARD;
3175 307134 s->c.mv_type = MV_TYPE_16X16;
3176 307134 s->c.mb_intra = 0;
3177 307134 s->c.mv[0][0][0] = s->p_mv_table[xy][0];
3178 307134 s->c.mv[0][0][1] = s->p_mv_table[xy][1];
3179 307134 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3180 &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
3181 }
3182
2/2
✓ Branch 0 taken 13407 times.
✓ Branch 1 taken 536255 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
3183 13407 s->c.mv_dir = MV_DIR_FORWARD;
3184 13407 s->c.mv_type = MV_TYPE_FIELD;
3185 13407 s->c.mb_intra = 0;
3186
2/2
✓ Branch 0 taken 26814 times.
✓ Branch 1 taken 13407 times.
40221 for(i=0; i<2; i++){
3187 26814 int j = s->c.field_select[0][i] = s->p_field_select_table[i][xy];
3188 26814 s->c.mv[0][i][0] = s->c.p_field_mv_table[i][j][xy][0];
3189 26814 s->c.mv[0][i][1] = s->c.p_field_mv_table[i][j][xy][1];
3190 }
3191 13407 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3192 &dmin, &next_block, 0, 0);
3193 }
3194
2/2
✓ Branch 0 taken 85564 times.
✓ Branch 1 taken 464098 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
3195 85564 s->c.mv_dir = MV_DIR_FORWARD;
3196 85564 s->c.mv_type = MV_TYPE_16X16;
3197 85564 s->c.mb_intra = 0;
3198 85564 s->c.mv[0][0][0] = 0;
3199 85564 s->c.mv[0][0][1] = 0;
3200 85564 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3201 &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
3202 }
3203
2/2
✓ Branch 0 taken 233468 times.
✓ Branch 1 taken 316194 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
3204 233468 s->c.mv_dir = MV_DIR_FORWARD;
3205 233468 s->c.mv_type = MV_TYPE_8X8;
3206 233468 s->c.mb_intra = 0;
3207
2/2
✓ Branch 0 taken 933872 times.
✓ Branch 1 taken 233468 times.
1167340 for(i=0; i<4; i++){
3208 933872 s->c.mv[0][i][0] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
3209 933872 s->c.mv[0][i][1] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
3210 }
3211 233468 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3212 &dmin, &next_block, 0, 0);
3213 }
3214
2/2
✓ Branch 0 taken 222517 times.
✓ Branch 1 taken 327145 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
3215 222517 s->c.mv_dir = MV_DIR_FORWARD;
3216 222517 s->c.mv_type = MV_TYPE_16X16;
3217 222517 s->c.mb_intra = 0;
3218 222517 s->c.mv[0][0][0] = s->b_forw_mv_table[xy][0];
3219 222517 s->c.mv[0][0][1] = s->b_forw_mv_table[xy][1];
3220 222517 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3221 &dmin, &next_block, s->c.mv[0][0][0], s->c.mv[0][0][1]);
3222 }
3223
2/2
✓ Branch 0 taken 222517 times.
✓ Branch 1 taken 327145 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
3224 222517 s->c.mv_dir = MV_DIR_BACKWARD;
3225 222517 s->c.mv_type = MV_TYPE_16X16;
3226 222517 s->c.mb_intra = 0;
3227 222517 s->c.mv[1][0][0] = s->b_back_mv_table[xy][0];
3228 222517 s->c.mv[1][0][1] = s->b_back_mv_table[xy][1];
3229 222517 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3230 &dmin, &next_block, s->c.mv[1][0][0], s->c.mv[1][0][1]);
3231 }
3232
2/2
✓ Branch 0 taken 222517 times.
✓ Branch 1 taken 327145 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
3233 222517 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3234 222517 s->c.mv_type = MV_TYPE_16X16;
3235 222517 s->c.mb_intra = 0;
3236 222517 s->c.mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3237 222517 s->c.mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3238 222517 s->c.mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3239 222517 s->c.mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3240 222517 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3241 &dmin, &next_block, 0, 0);
3242 }
3243
2/2
✓ Branch 0 taken 31126 times.
✓ Branch 1 taken 518536 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
3244 31126 s->c.mv_dir = MV_DIR_FORWARD;
3245 31126 s->c.mv_type = MV_TYPE_FIELD;
3246 31126 s->c.mb_intra = 0;
3247
2/2
✓ Branch 0 taken 62252 times.
✓ Branch 1 taken 31126 times.
93378 for(i=0; i<2; i++){
3248 62252 int j = s->c.field_select[0][i] = s->b_field_select_table[0][i][xy];
3249 62252 s->c.mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3250 62252 s->c.mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3251 }
3252 31126 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3253 &dmin, &next_block, 0, 0);
3254 }
3255
2/2
✓ Branch 0 taken 31126 times.
✓ Branch 1 taken 518536 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
3256 31126 s->c.mv_dir = MV_DIR_BACKWARD;
3257 31126 s->c.mv_type = MV_TYPE_FIELD;
3258 31126 s->c.mb_intra = 0;
3259
2/2
✓ Branch 0 taken 62252 times.
✓ Branch 1 taken 31126 times.
93378 for(i=0; i<2; i++){
3260 62252 int j = s->c.field_select[1][i] = s->b_field_select_table[1][i][xy];
3261 62252 s->c.mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3262 62252 s->c.mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3263 }
3264 31126 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3265 &dmin, &next_block, 0, 0);
3266 }
3267
2/2
✓ Branch 0 taken 26116 times.
✓ Branch 1 taken 523546 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
3268 26116 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3269 26116 s->c.mv_type = MV_TYPE_FIELD;
3270 26116 s->c.mb_intra = 0;
3271
2/2
✓ Branch 0 taken 52232 times.
✓ Branch 1 taken 26116 times.
78348 for(dir=0; dir<2; dir++){
3272
2/2
✓ Branch 0 taken 104464 times.
✓ Branch 1 taken 52232 times.
156696 for(i=0; i<2; i++){
3273 104464 int j = s->c.field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3274 104464 s->c.mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3275 104464 s->c.mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3276 }
3277 }
3278 26116 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3279 &dmin, &next_block, 0, 0);
3280 }
3281
2/2
✓ Branch 0 taken 121465 times.
✓ Branch 1 taken 428197 times.
549662 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
3282 121465 s->c.mv_dir = 0;
3283 121465 s->c.mv_type = MV_TYPE_16X16;
3284 121465 s->c.mb_intra = 1;
3285 121465 s->c.mv[0][0][0] = 0;
3286 121465 s->c.mv[0][0][1] = 0;
3287 121465 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3288 &dmin, &next_block, 0, 0);
3289 121465 s->c.mbintra_table[xy] = 1;
3290 }
3291
3292
4/4
✓ Branch 0 taken 179700 times.
✓ Branch 1 taken 369962 times.
✓ Branch 2 taken 179668 times.
✓ Branch 3 taken 32 times.
549662 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
3293
2/2
✓ Branch 0 taken 166451 times.
✓ Branch 1 taken 13217 times.
179668 if (best_s.c.mv_type == MV_TYPE_16X16) { //FIXME move 4mv after QPRD
3294 166451 const int last_qp = backup_s.c.qscale;
3295 int qpi, qp, dc[6];
3296 int16_t ac[6][16];
3297 166451 const int mvdir = (best_s.c.mv_dir & MV_DIR_BACKWARD) ? 1 : 0;
3298 static const int dquant_tab[4]={-1,1,-2,2};
3299
4/4
✓ Branch 0 taken 32741 times.
✓ Branch 1 taken 133710 times.
✓ Branch 2 taken 10072 times.
✓ Branch 3 taken 22669 times.
166451 int storecoefs = s->c.mb_intra && s->c.dc_val;
3300
3301 av_assert2(backup_s.dquant == 0);
3302
3303 //FIXME intra
3304 166451 s->c.mv_dir = best_s.c.mv_dir;
3305 166451 s->c.mv_type = MV_TYPE_16X16;
3306 166451 s->c.mb_intra = best_s.c.mb_intra;
3307 166451 s->c.mv[0][0][0] = best_s.c.mv[0][0][0];
3308 166451 s->c.mv[0][0][1] = best_s.c.mv[0][0][1];
3309 166451 s->c.mv[1][0][0] = best_s.c.mv[1][0][0];
3310 166451 s->c.mv[1][0][1] = best_s.c.mv[1][0][1];
3311
3312
2/2
✓ Branch 0 taken 111476 times.
✓ Branch 1 taken 54975 times.
166451 qpi = s->c.pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3313
2/2
✓ Branch 0 taken 442852 times.
✓ Branch 1 taken 166451 times.
609303 for(; qpi<4; qpi++){
3314 442852 int dquant= dquant_tab[qpi];
3315 442852 qp= last_qp + dquant;
3316
4/4
✓ Branch 0 taken 397648 times.
✓ Branch 1 taken 45204 times.
✓ Branch 2 taken 4975 times.
✓ Branch 3 taken 392673 times.
442852 if (qp < s->c.avctx->qmin || qp > s->c.avctx->qmax)
3317 50179 continue;
3318 392673 backup_s.dquant= dquant;
3319
2/2
✓ Branch 0 taken 37259 times.
✓ Branch 1 taken 355414 times.
392673 if(storecoefs){
3320
2/2
✓ Branch 0 taken 223554 times.
✓ Branch 1 taken 37259 times.
260813 for(i=0; i<6; i++){
3321 223554 dc[i] = s->c.dc_val[s->c.block_index[i]];
3322 223554 memcpy(ac[i], s->c.ac_val[s->c.block_index[i]], sizeof(*s->c.ac_val));
3323 }
3324 }
3325
3326 392673 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3327 &dmin, &next_block, s->c.mv[mvdir][0][0], s->c.mv[mvdir][0][1]);
3328
2/2
✓ Branch 0 taken 370389 times.
✓ Branch 1 taken 22284 times.
392673 if (best_s.c.qscale != qp) {
3329
2/2
✓ Branch 0 taken 33540 times.
✓ Branch 1 taken 336849 times.
370389 if(storecoefs){
3330
2/2
✓ Branch 0 taken 201240 times.
✓ Branch 1 taken 33540 times.
234780 for(i=0; i<6; i++){
3331 201240 s->c.dc_val[s->c.block_index[i]] = dc[i];
3332 201240 memcpy(s->c.ac_val[s->c.block_index[i]], ac[i], sizeof(*s->c.ac_val));
3333 }
3334 }
3335 }
3336 }
3337 }
3338 }
3339
2/2
✓ Branch 0 taken 145733 times.
✓ Branch 1 taken 403929 times.
549662 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3340 145733 int mx= s->b_direct_mv_table[xy][0];
3341 145733 int my= s->b_direct_mv_table[xy][1];
3342
3343 145733 backup_s.dquant = 0;
3344 145733 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3345 145733 s->c.mb_intra = 0;
3346 145733 ff_mpeg4_set_direct_mv(&s->c, mx, my);
3347 145733 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3348 &dmin, &next_block, mx, my);
3349 }
3350
2/2
✓ Branch 0 taken 40280 times.
✓ Branch 1 taken 509382 times.
549662 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3351 40280 backup_s.dquant = 0;
3352 40280 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3353 40280 s->c.mb_intra = 0;
3354 40280 ff_mpeg4_set_direct_mv(&s->c, 0, 0);
3355 40280 encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3356 &dmin, &next_block, 0, 0);
3357 }
3358
3/4
✓ Branch 0 taken 509697 times.
✓ Branch 1 taken 39965 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 509697 times.
549662 if (!best_s.c.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3359 ✗ int coded=0;
3360 ✗ for(i=0; i<6; i++)
3361 ✗ coded |= s->c.block_last_index[i];
3362 ✗ if(coded){
3363 int mx,my;
3364 ✗ memcpy(s->c.mv, best_s.c.mv, sizeof(s->c.mv));
3365 ✗ if (CONFIG_MPEG4_ENCODER && best_s.c.mv_dir & MV_DIRECT) {
3366 ✗ mx=my=0; //FIXME find the one we actually used
3367 ✗ ff_mpeg4_set_direct_mv(&s->c, mx, my);
3368 ✗ } else if (best_s.c.mv_dir & MV_DIR_BACKWARD) {
3369 ✗ mx = s->c.mv[1][0][0];
3370 ✗ my = s->c.mv[1][0][1];
3371 }else{
3372 ✗ mx = s->c.mv[0][0][0];
3373 ✗ my = s->c.mv[0][0][1];
3374 }
3375
3376 ✗ s->c.mv_dir = best_s.c.mv_dir;
3377 ✗ s->c.mv_type = best_s.c.mv_type;
3378 ✗ s->c.mb_intra = 0;
3379 /* s->c.mv[0][0][0] = best_s.mv[0][0][0];
3380 s->c.mv[0][0][1] = best_s.mv[0][0][1];
3381 s->c.mv[1][0][0] = best_s.mv[1][0][0];
3382 s->c.mv[1][0][1] = best_s.mv[1][0][1];*/
3383 ✗ backup_s.dquant= 0;
3384 ✗ s->skipdct=1;
3385 ✗ encode_mb_hq(s, &backup_s, &best_s, pb, pb2, tex_pb,
3386 &dmin, &next_block, mx, my);
3387 ✗ s->skipdct=0;
3388 }
3389 }
3390
3391 549662 store_context_after_encode(s, &best_s, s->data_partitioning);
3392
3393 549662 pb_bits_count= put_bits_count(&s->pb);
3394 549662 flush_put_bits(&s->pb);
3395 549662 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3396 549662 s->pb= backup_s.pb;
3397
3398
2/2
✓ Branch 0 taken 141082 times.
✓ Branch 1 taken 408580 times.
549662 if (s->data_partitioning) {
3399 141082 pb2_bits_count= put_bits_count(&s->pb2);
3400 141082 flush_put_bits(&s->pb2);
3401 141082 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3402 141082 s->pb2= backup_s.pb2;
3403
3404 141082 tex_pb_bits_count= put_bits_count(&s->tex_pb);
3405 141082 flush_put_bits(&s->tex_pb);
3406 141082 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3407 141082 s->tex_pb= backup_s.tex_pb;
3408 }
3409 549662 s->last_bits= put_bits_count(&s->pb);
3410
3411 549662 if (CONFIG_H263_ENCODER &&
3412
4/4
✓ Branch 0 taken 414922 times.
✓ Branch 1 taken 134740 times.
✓ Branch 2 taken 269077 times.
✓ Branch 3 taken 145845 times.
549662 s->c.out_format == FMT_H263 && s->c.pict_type != AV_PICTURE_TYPE_B)
3413 269077 ff_h263_update_mb(s);
3414
3415
2/2
✓ Branch 0 taken 226321 times.
✓ Branch 1 taken 323341 times.
549662 if(next_block==0){ //FIXME 16 vs linesize16
3416 226321 s->c.hdsp.put_pixels_tab[0][0](s->c.dest[0], s->c.sc.rd_scratchpad , s->c.linesize ,16);
3417 226321 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);
3418 226321 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);
3419 }
3420
3421
2/2
✓ Branch 0 taken 141018 times.
✓ Branch 1 taken 408644 times.
549662 if (s->c.avctx->mb_decision == FF_MB_DECISION_BITS)
3422 141018 mpv_reconstruct_mb(s, s->block);
3423 } else {
3424 3525841 int motion_x = 0, motion_y = 0;
3425 3525841 s->c.mv_type = MV_TYPE_16X16;
3426 // only one MB-Type possible
3427
3428
10/13
✓ Branch 0 taken 1302455 times.
✓ Branch 1 taken 2033928 times.
✓ Branch 2 taken 9451 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16241 times.
✓ Branch 5 taken 75 times.
✓ Branch 6 taken 101701 times.
✓ Branch 7 taken 35510 times.
✓ Branch 8 taken 21818 times.
✓ Branch 9 taken 2174 times.
✓ Branch 10 taken 2488 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
3525841 switch(mb_type){
3429 1302455 case CANDIDATE_MB_TYPE_INTRA:
3430 1302455 s->c.mv_dir = 0;
3431 1302455 s->c.mb_intra = 1;
3432 1302455 motion_x= s->c.mv[0][0][0] = 0;
3433 1302455 motion_y= s->c.mv[0][0][1] = 0;
3434 1302455 s->c.mbintra_table[xy] = 1;
3435 1302455 break;
3436 2033928 case CANDIDATE_MB_TYPE_INTER:
3437 2033928 s->c.mv_dir = MV_DIR_FORWARD;
3438 2033928 s->c.mb_intra = 0;
3439 2033928 motion_x= s->c.mv[0][0][0] = s->p_mv_table[xy][0];
3440 2033928 motion_y= s->c.mv[0][0][1] = s->p_mv_table[xy][1];
3441 2033928 break;
3442 9451 case CANDIDATE_MB_TYPE_INTER_I:
3443 9451 s->c.mv_dir = MV_DIR_FORWARD;
3444 9451 s->c.mv_type = MV_TYPE_FIELD;
3445 9451 s->c.mb_intra = 0;
3446
2/2
✓ Branch 0 taken 18902 times.
✓ Branch 1 taken 9451 times.
28353 for(i=0; i<2; i++){
3447 18902 int j = s->c.field_select[0][i] = s->p_field_select_table[i][xy];
3448 18902 s->c.mv[0][i][0] = s->c.p_field_mv_table[i][j][xy][0];
3449 18902 s->c.mv[0][i][1] = s->c.p_field_mv_table[i][j][xy][1];
3450 }
3451 9451 break;
3452 ✗ case CANDIDATE_MB_TYPE_INTER4V:
3453 ✗ s->c.mv_dir = MV_DIR_FORWARD;
3454 ✗ s->c.mv_type = MV_TYPE_8X8;
3455 ✗ s->c.mb_intra = 0;
3456 ✗ for(i=0; i<4; i++){
3457 ✗ s->c.mv[0][i][0] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][0];
3458 ✗ s->c.mv[0][i][1] = s->c.cur_pic.motion_val[0][s->c.block_index[i]][1];
3459 }
3460 ✗ break;
3461 16241 case CANDIDATE_MB_TYPE_DIRECT:
3462 if (CONFIG_MPEG4_ENCODER) {
3463 16241 s->c.mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3464 16241 s->c.mb_intra = 0;
3465 16241 motion_x=s->b_direct_mv_table[xy][0];
3466 16241 motion_y=s->b_direct_mv_table[xy][1];
3467 16241 ff_mpeg4_set_direct_mv(&s->c, motion_x, motion_y);
3468 }
3469 16241 break;
3470 75 case CANDIDATE_MB_TYPE_DIRECT0:
3471 if (CONFIG_MPEG4_ENCODER) {
3472 75 s->c.mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3473 75 s->c.mb_intra = 0;
3474 75 ff_mpeg4_set_direct_mv(&s->c, 0, 0);
3475 }
3476 75 break;
3477 101701 case CANDIDATE_MB_TYPE_BIDIR:
3478 101701 s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3479 101701 s->c.mb_intra = 0;
3480 101701 s->c.mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3481 101701 s->c.mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3482 101701 s->c.mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3483 101701 s->c.mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3484 101701 break;
3485 35510 case CANDIDATE_MB_TYPE_BACKWARD:
3486 35510 s->c.mv_dir = MV_DIR_BACKWARD;
3487 35510 s->c.mb_intra = 0;
3488 35510 motion_x= s->c.mv[1][0][0] = s->b_back_mv_table[xy][0];
3489 35510 motion_y= s->c.mv[1][0][1] = s->b_back_mv_table[xy][1];
3490 35510 break;
3491 21818 case CANDIDATE_MB_TYPE_FORWARD:
3492 21818 s->c.mv_dir = MV_DIR_FORWARD;
3493 21818 s->c.mb_intra = 0;
3494 21818 motion_x= s->c.mv[0][0][0] = s->b_forw_mv_table[xy][0];
3495 21818 motion_y= s->c.mv[0][0][1] = s->b_forw_mv_table[xy][1];
3496 21818 break;
3497 2174 case CANDIDATE_MB_TYPE_FORWARD_I:
3498 2174 s->c.mv_dir = MV_DIR_FORWARD;
3499 2174 s->c.mv_type = MV_TYPE_FIELD;
3500 2174 s->c.mb_intra = 0;
3501
2/2
✓ Branch 0 taken 4348 times.
✓ Branch 1 taken 2174 times.
6522 for(i=0; i<2; i++){
3502 4348 int j = s->c.field_select[0][i] = s->b_field_select_table[0][i][xy];
3503 4348 s->c.mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3504 4348 s->c.mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3505 }
3506 2174 break;
3507 2488 case CANDIDATE_MB_TYPE_BACKWARD_I:
3508 2488 s->c.mv_dir = MV_DIR_BACKWARD;
3509 2488 s->c.mv_type = MV_TYPE_FIELD;
3510 2488 s->c.mb_intra = 0;
3511
2/2
✓ Branch 0 taken 4976 times.
✓ Branch 1 taken 2488 times.
7464 for(i=0; i<2; i++){
3512 4976 int j = s->c.field_select[1][i] = s->b_field_select_table[1][i][xy];
3513 4976 s->c.mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3514 4976 s->c.mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3515 }
3516 2488 break;
3517 ✗ case CANDIDATE_MB_TYPE_BIDIR_I:
3518 ✗ s->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3519 ✗ s->c.mv_type = MV_TYPE_FIELD;
3520 ✗ s->c.mb_intra = 0;
3521 ✗ for(dir=0; dir<2; dir++){
3522 ✗ for(i=0; i<2; i++){
3523 ✗ int j = s->c.field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3524 ✗ s->c.mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3525 ✗ s->c.mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3526 }
3527 }
3528 ✗ break;
3529 ✗ default:
3530 ✗ av_unreachable("There is a case for every CANDIDATE_MB_TYPE_* "
3531 "except CANDIDATE_MB_TYPE_SKIPPED which is never "
3532 "the only candidate (always coupled with INTER) "
3533 "so that it never reaches this switch");
3534 }
3535
3536 3525841 encode_mb(s, motion_x, motion_y);
3537
3538 // RAL: Update last macroblock type
3539 3525841 s->last_mv_dir = s->c.mv_dir;
3540
3541 3525841 if (CONFIG_H263_ENCODER &&
3542
4/4
✓ Branch 0 taken 1169516 times.
✓ Branch 1 taken 2356325 times.
✓ Branch 2 taken 1131157 times.
✓ Branch 3 taken 38359 times.
3525841 s->c.out_format == FMT_H263 && s->c.pict_type != AV_PICTURE_TYPE_B)
3543 1131157 ff_h263_update_mb(s);
3544
3545 3525841 mpv_reconstruct_mb(s, s->block);
3546 }
3547
3548 4075503 s->c.cur_pic.qscale_table[xy] = s->c.qscale;
3549
3550 /* clean the MV table in IPS frames for direct mode in B-frames */
3551
2/2
✓ Branch 0 taken 1342420 times.
✓ Branch 1 taken 2733083 times.
4075503 if (s->c.mb_intra /* && I,P,S_TYPE */) {
3552 1342420 s->p_mv_table[xy][0]=0;
3553 1342420 s->p_mv_table[xy][1]=0;
3554 #if CONFIG_H263_ENCODER
3555
4/4
✓ Branch 0 taken 1745613 times.
✓ Branch 1 taken 987470 times.
✓ Branch 2 taken 51657 times.
✓ Branch 3 taken 1693956 times.
2733083 } else if (s->c.h263_pred || s->c.h263_aic) {
3556 1039127 ff_h263_clean_intra_table_entries(&s->c, xy);
3557 #endif
3558 }
3559
3560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4075503 times.
4075503 if (s->c.avctx->flags & AV_CODEC_FLAG_PSNR) {
3561 ✗ int w= 16;
3562 ✗ int h= 16;
3563
3564 ✗ if (s->c.mb_x*16 + 16 > s->c.width ) w = s->c.width - s->c.mb_x*16;
3565 ✗ if (s->c.mb_y*16 + 16 > s->c.height) h = s->c.height- s->c.mb_y*16;
3566
3567 ✗ s->encoding_error[0] += sse(
3568 ✗ s, s->new_pic->data[0] + s->c.mb_x*16 + s->c.mb_y*s->c.linesize*16,
3569 ✗ s->c.dest[0], w, h, s->c.linesize);
3570 ✗ s->encoding_error[1] += sse(
3571 ✗ s, s->new_pic->data[1] + s->c.mb_x*8 + s->c.mb_y*s->c.uvlinesize*chr_h,
3572 ✗ s->c.dest[1], w>>1, h>>s->c.chroma_y_shift, s->c.uvlinesize);
3573 ✗ s->encoding_error[2] += sse(
3574 ✗ s, s->new_pic->data[2] + s->c.mb_x*8 + s->c.mb_y*s->c.uvlinesize*chr_h,
3575 ✗ s->c.dest[2], w>>1, h>>s->c.chroma_y_shift, s->c.uvlinesize);
3576 }
3577
2/2
✓ Branch 0 taken 118800 times.
✓ Branch 1 taken 3956703 times.
4075503 if (s->loop_filter) {
3578
2/2
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 59400 times.
118800 if (CONFIG_H263_ENCODER && s->c.out_format == FMT_H263)
3579 59400 ff_h263_loop_filter(&s->c);
3580 }
3581 ff_dlog(s->c.avctx, "MB %d %d bits\n",
3582 s->c.mb_x + s->c.mb_y * s->c.mb_stride, put_bits_count(&s->pb));
3583 }
3584 }
3585
3586 #if CONFIG_MSMPEG4ENC
3587 //not beautiful here but we must write it before flushing so it has to be here
3588
4/4
✓ Branch 0 taken 825 times.
✓ Branch 1 taken 15106 times.
✓ Branch 2 taken 425 times.
✓ Branch 3 taken 400 times.
15931 if (s->c.msmpeg4_version != MSMP4_UNUSED && s->c.msmpeg4_version < MSMP4_WMV1 &&
3589
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 382 times.
425 s->c.pict_type == AV_PICTURE_TYPE_I)
3590 43 ff_msmpeg4_encode_ext_header(s);
3591 #endif
3592
3593 15931 write_slice_end(s);
3594
3595 15931 return 0;
3596 }
3597
3598 #define ADD(field) dst->field += src->field;
3599 #define MERGE(field) dst->field += src->field; src->field=0
3600 3468 static void merge_context_after_me(MPVEncContext *const dst, MPVEncContext *const src)
3601 {
3602 3468 ADD(me.scene_change_score);
3603 3468 ADD(me.mc_mb_var_sum_temp);
3604 3468 ADD(me.mb_var_sum_temp);
3605 3468 }
3606
3607 3468 static void merge_context_after_encode(MPVEncContext *const dst, MPVEncContext *const src)
3608 {
3609 int i;
3610
3611 3468 MERGE(dct_count[0]); //note, the other dct vars are not part of the context
3612 3468 MERGE(dct_count[1]);
3613 3468 ADD(mv_bits);
3614 3468 ADD(i_tex_bits);
3615 3468 ADD(p_tex_bits);
3616 3468 ADD(i_count);
3617 3468 ADD(misc_bits);
3618 3468 ADD(encoding_error[0]);
3619 3468 ADD(encoding_error[1]);
3620 3468 ADD(encoding_error[2]);
3621
3622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3468 times.
3468 if (dst->dct_error_sum) {
3623 ✗ for(i=0; i<64; i++){
3624 ✗ MERGE(dct_error_sum[0][i]);
3625 ✗ MERGE(dct_error_sum[1][i]);
3626 }
3627 }
3628
3629 av_assert1(put_bits_count(&src->pb) % 8 ==0);
3630 av_assert1(put_bits_count(&dst->pb) % 8 ==0);
3631 3468 ff_copy_bits(&dst->pb, src->pb.buf, put_bits_count(&src->pb));
3632 3468 flush_put_bits(&dst->pb);
3633 3468 }
3634
3635 12463 static int estimate_qp(MPVMainEncContext *const m, int dry_run)
3636 {
3637 12463 MPVEncContext *const s = &m->s;
3638
3639
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 12338 times.
12463 if (m->next_lambda){
3640 125 s->c.cur_pic.ptr->f->quality = m->next_lambda;
3641
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if(!dry_run) m->next_lambda= 0;
3642
2/2
✓ Branch 0 taken 5267 times.
✓ Branch 1 taken 7071 times.
12338 } else if (!m->fixed_qscale) {
3643 5267 int quality = ff_rate_estimate_qscale(m, dry_run);
3644 5267 s->c.cur_pic.ptr->f->quality = quality;
3645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5267 times.
5267 if (s->c.cur_pic.ptr->f->quality < 0)
3646 ✗ return -1;
3647 }
3648
3649
2/2
✓ Branch 0 taken 850 times.
✓ Branch 1 taken 11613 times.
12463 if(s->adaptive_quant){
3650 850 init_qscale_tab(s);
3651
3652
2/3
✓ Branch 0 taken 450 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 400 times.
850 switch (s->c.codec_id) {
3653 450 case AV_CODEC_ID_MPEG4:
3654 if (CONFIG_MPEG4_ENCODER)
3655 450 ff_clean_mpeg4_qscales(s);
3656 450 break;
3657 ✗ case AV_CODEC_ID_H263:
3658 case AV_CODEC_ID_H263P:
3659 case AV_CODEC_ID_FLV1:
3660 if (CONFIG_H263_ENCODER)
3661 ✗ ff_clean_h263_qscales(s);
3662 ✗ break;
3663 }
3664
3665 850 s->lambda = s->lambda_table[0];
3666 //FIXME broken
3667 }else
3668 11613 s->lambda = s->c.cur_pic.ptr->f->quality;
3669 12463 update_qscale(m);
3670 12463 return 0;
3671 }
3672
3673 /* must be called before writing the header */
3674 2749 static void set_frame_distances(MPVEncContext *const s)
3675 {
3676 av_assert1(s->c.cur_pic.ptr->f->pts != AV_NOPTS_VALUE);
3677 2749 s->c.time = s->c.cur_pic.ptr->f->pts * s->c.avctx->time_base.num;
3678
3679
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 2147 times.
2749 if (s->c.pict_type == AV_PICTURE_TYPE_B) {
3680 602 s->c.pb_time = s->c.pp_time - (s->c.last_non_b_time - s->c.time);
3681 av_assert1(s->c.pb_time > 0 && s->c.pb_time < s->c.pp_time);
3682 }else{
3683 av_assert1(s->picture_number == 0 || s->c.time > s->c.last_non_b_time);
3684 2147 s->c.pp_time = s->c.time - s->c.last_non_b_time;
3685 2147 s->c.last_non_b_time = s->c.time;
3686 }
3687 2749 }
3688
3689 12463 static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt)
3690 {
3691 12463 MPVEncContext *const s = &m->s;
3692 int ret;
3693 int bits;
3694 12463 int context_count = s->c.slice_context_count;
3695
3696
2/2
✓ Branch 0 taken 2749 times.
✓ Branch 1 taken 9714 times.
12463 if (CONFIG_MPEG4_ENCODER && s->c.codec_id == AV_CODEC_ID_MPEG4) {
3697 2749 set_frame_distances(s);
3698 2749 ff_set_mpeg4_time(s);
3699 }
3700
3701 // s->lambda = s->c.cur_pic.ptr->quality; //FIXME qscale / ... stuff for ME rate distortion
3702
3703
2/2
✓ Branch 0 taken 3074 times.
✓ Branch 1 taken 9389 times.
12463 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
3704 3074 s->c.no_rounding = s->c.msmpeg4_version >= MSMP4_V3;
3705
2/2
✓ Branch 0 taken 8041 times.
✓ Branch 1 taken 1348 times.
9389 } else if (s->c.pict_type != AV_PICTURE_TYPE_B) {
3706 8041 s->c.no_rounding ^= s->flipflop_rounding;
3707 }
3708
3709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12463 times.
12463 if (s->c.avctx->flags & AV_CODEC_FLAG_PASS2) {
3710 ✗ ret = estimate_qp(m, 1);
3711 ✗ if (ret < 0)
3712 ✗ return ret;
3713 ✗ ff_get_2pass_fcode(m);
3714
2/2
✓ Branch 0 taken 5267 times.
✓ Branch 1 taken 7196 times.
12463 } else if (!(s->c.avctx->flags & AV_CODEC_FLAG_QSCALE)) {
3715
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 4499 times.
5267 if (s->c.pict_type == AV_PICTURE_TYPE_B)
3716 768 s->lambda = m->last_lambda_for[s->c.pict_type];
3717 else
3718 4499 s->lambda = m->last_lambda_for[m->last_non_b_pict_type];
3719 5267 update_qscale(m);
3720 }
3721
3722 12463 s->c.mb_intra = 0; //for the rate distortion & bit compare functions
3723
2/2
✓ Branch 0 taken 15931 times.
✓ Branch 1 taken 12463 times.
28394 for (int i = 0; i < context_count; i++) {
3724 15931 MPVEncContext *const slice = s->c.enc_contexts[i];
3725 15931 int h = s->c.mb_height;
3726 15931 uint8_t *start = pkt->data + (int64_t)pkt->size * slice->c.start_mb_y / h;
3727 15931 uint8_t *end = pkt->data + (int64_t)pkt->size * slice->c. end_mb_y / h;
3728
3729 15931 init_put_bits(&slice->pb, start, end - start);
3730
3731
2/2
✓ Branch 0 taken 3468 times.
✓ Branch 1 taken 12463 times.
15931 if (i) {
3732 3468 ret = ff_update_duplicate_context(&slice->c, &s->c);
3733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3468 times.
3468 if (ret < 0)
3734 ✗ return ret;
3735 3468 slice->lambda = s->lambda;
3736 3468 slice->lambda2 = s->lambda2;
3737 }
3738 15931 slice->me.temp = slice->me.scratchpad = slice->c.sc.scratchpad_buf;
3739 15931 ff_me_init_pic(slice);
3740 }
3741
3742 /* Estimate motion for every MB */
3743
2/2
✓ Branch 0 taken 9389 times.
✓ Branch 1 taken 3074 times.
12463 if (s->c.pict_type != AV_PICTURE_TYPE_I) {
3744 9389 s->lambda = (s->lambda * m->me_penalty_compensation + 128) >> 8;
3745 9389 s->lambda2 = (s->lambda2 * (int64_t) m->me_penalty_compensation + 128) >> 8;
3746
2/2
✓ Branch 0 taken 8041 times.
✓ Branch 1 taken 1348 times.
9389 if (s->c.pict_type != AV_PICTURE_TYPE_B) {
3747
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 8041 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
8041 if ((m->me_pre && m->last_non_b_pict_type == AV_PICTURE_TYPE_I) ||
3748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8041 times.
8041 m->me_pre == 2) {
3749 ✗ s->c.avctx->execute(s->c.avctx, pre_estimate_motion_thread,
3750 ✗ &s->c.enc_contexts[0], NULL,
3751 context_count, sizeof(void*));
3752 }
3753 }
3754
3755 9389 s->c.avctx->execute(s->c.avctx, estimate_motion_thread, &s->c.enc_contexts[0],
3756 NULL, context_count, sizeof(void*));
3757 }else /* if (s->c.pict_type == AV_PICTURE_TYPE_I) */{
3758 /* I-Frame */
3759
2/2
✓ Branch 0 taken 1272381 times.
✓ Branch 1 taken 3074 times.
1275455 for (int i = 0; i < s->c.mb_stride * s->c.mb_height; i++)
3760 1272381 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3761
3762
2/2
✓ Branch 0 taken 921 times.
✓ Branch 1 taken 2153 times.
3074 if (!m->fixed_qscale) {
3763 /* finding spatial complexity for I-frame rate control */
3764 921 s->c.avctx->execute(s->c.avctx, mb_var_thread, &s->c.enc_contexts[0],
3765 NULL, context_count, sizeof(void*));
3766 }
3767 }
3768
2/2
✓ Branch 0 taken 3468 times.
✓ Branch 1 taken 12463 times.
15931 for (int i = 1; i < context_count; i++)
3769 3468 merge_context_after_me(s, s->c.enc_contexts[i]);
3770 12463 m->mc_mb_var_sum = s->me.mc_mb_var_sum_temp;
3771 12463 m->mb_var_sum = s->me. mb_var_sum_temp;
3772 12463 emms_c();
3773
3774
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 12432 times.
12463 if (s->me.scene_change_score > m->scenechange_threshold &&
3775
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 s->c.pict_type == AV_PICTURE_TYPE_P) {
3776 31 s->c.pict_type = AV_PICTURE_TYPE_I;
3777
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++)
3778 420 s->mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
3779
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
31 if (s->c.msmpeg4_version >= MSMP4_V3)
3780 2 s->c.no_rounding = 1;
3781 ff_dlog(s->c.avctx, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
3782 m->mb_var_sum, m->mc_mb_var_sum);
3783 }
3784
3785
2/2
✓ Branch 0 taken 12313 times.
✓ Branch 1 taken 150 times.
12463 if (!s->umvplus) {
3786
3/4
✓ Branch 0 taken 4438 times.
✓ Branch 1 taken 7875 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4438 times.
12313 if (s->c.pict_type == AV_PICTURE_TYPE_P || s->c.pict_type == AV_PICTURE_TYPE_S) {
3787 7875 s->f_code = ff_get_best_fcode(m, s->p_mv_table, CANDIDATE_MB_TYPE_INTER);
3788
3789
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 7543 times.
7875 if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3790 int a,b;
3791 332 a = ff_get_best_fcode(m, s->c.p_field_mv_table[0][0], CANDIDATE_MB_TYPE_INTER_I); //FIXME field_select
3792 332 b = ff_get_best_fcode(m, s->c.p_field_mv_table[1][1], CANDIDATE_MB_TYPE_INTER_I);
3793 332 s->f_code = FFMAX3(s->f_code, a, b);
3794 }
3795
3796
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7875 times.
7875 ff_fix_long_p_mvs(s, s->intra_penalty ? CANDIDATE_MB_TYPE_INTER : CANDIDATE_MB_TYPE_INTRA);
3797 7875 ff_fix_long_mvs(s, NULL, 0, s->p_mv_table, s->f_code, CANDIDATE_MB_TYPE_INTER, !!s->intra_penalty);
3798
2/2
✓ Branch 0 taken 332 times.
✓ Branch 1 taken 7543 times.
7875 if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3799 int j;
3800
2/2
✓ Branch 0 taken 664 times.
✓ Branch 1 taken 332 times.
996 for (int i = 0; i < 2; i++) {
3801
2/2
✓ Branch 0 taken 1328 times.
✓ Branch 1 taken 664 times.
1992 for(j=0; j<2; j++)
3802 1328 ff_fix_long_mvs(s, s->p_field_select_table[i], j,
3803 1328 s->c.p_field_mv_table[i][j], s->f_code, CANDIDATE_MB_TYPE_INTER_I, !!s->intra_penalty);
3804 }
3805 }
3806
2/2
✓ Branch 0 taken 1348 times.
✓ Branch 1 taken 3090 times.
4438 } else if (s->c.pict_type == AV_PICTURE_TYPE_B) {
3807 int a, b;
3808
3809 1348 a = ff_get_best_fcode(m, s->b_forw_mv_table, CANDIDATE_MB_TYPE_FORWARD);
3810 1348 b = ff_get_best_fcode(m, s->b_bidir_forw_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3811 1348 s->f_code = FFMAX(a, b);
3812
3813 1348 a = ff_get_best_fcode(m, s->b_back_mv_table, CANDIDATE_MB_TYPE_BACKWARD);
3814 1348 b = ff_get_best_fcode(m, s->b_bidir_back_mv_table, CANDIDATE_MB_TYPE_BIDIR);
3815 1348 s->b_code = FFMAX(a, b);
3816
3817 1348 ff_fix_long_mvs(s, NULL, 0, s->b_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_FORWARD, 1);
3818 1348 ff_fix_long_mvs(s, NULL, 0, s->b_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BACKWARD, 1);
3819 1348 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_forw_mv_table, s->f_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3820 1348 ff_fix_long_mvs(s, NULL, 0, s->b_bidir_back_mv_table, s->b_code, CANDIDATE_MB_TYPE_BIDIR, 1);
3821
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 964 times.
1348 if (s->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
3822 int dir, j;
3823
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 384 times.
1152 for(dir=0; dir<2; dir++){
3824
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 768 times.
2304 for (int i = 0; i < 2; i++) {
3825
2/2
✓ Branch 0 taken 3072 times.
✓ Branch 1 taken 1536 times.
4608 for(j=0; j<2; j++){
3826 3072 int type= dir ? (CANDIDATE_MB_TYPE_BACKWARD_I|CANDIDATE_MB_TYPE_BIDIR_I)
3827
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 1536 times.
3072 : (CANDIDATE_MB_TYPE_FORWARD_I |CANDIDATE_MB_TYPE_BIDIR_I);
3828
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,
3829 s->b_field_mv_table[dir][i][j], dir ? s->b_code : s->f_code, type, 1);
3830 }
3831 }
3832 }
3833 }
3834 }
3835 }
3836
3837 12463 ret = estimate_qp(m, 0);
3838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12463 times.
12463 if (ret < 0)
3839 ✗ return ret;
3840
3841
4/4
✓ Branch 0 taken 3360 times.
✓ Branch 1 taken 9103 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 3356 times.
12463 if (s->c.qscale < 3 && s->max_qcoeff <= 128 &&
3842
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 s->c.pict_type == AV_PICTURE_TYPE_I &&
3843
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !(s->c.avctx->flags & AV_CODEC_FLAG_QSCALE))
3844 2 s->c.qscale = 3; //reduce clipping problems
3845
3846
2/2
✓ Branch 0 taken 1641 times.
✓ Branch 1 taken 10822 times.
12463 if (s->c.out_format == FMT_MJPEG) {
3847 1641 ret = ff_check_codec_matrices(s->c.avctx, FF_MATRIX_TYPE_INTRA | FF_MATRIX_TYPE_CHROMA_INTRA,
3848 1641 (7 + s->c.qscale) / s->c.qscale, 65535);
3849
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1641 times.
1641 if (ret < 0)
3850 ✗ return ret;
3851
3852
2/2
✓ Branch 0 taken 1441 times.
✓ Branch 1 taken 200 times.
1641 if (s->c.codec_id != AV_CODEC_ID_AMV) {
3853 1441 const uint16_t * luma_matrix = ff_mpeg1_default_intra_matrix;
3854 1441 const uint16_t *chroma_matrix = ff_mpeg1_default_intra_matrix;
3855
3856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441 times.
1441 if (s->c.avctx->intra_matrix) {
3857 ✗ chroma_matrix =
3858 ✗ luma_matrix = s->c.avctx->intra_matrix;
3859 }
3860
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1441 times.
1441 if (s->c.avctx->chroma_intra_matrix)
3861 ✗ chroma_matrix = s->c.avctx->chroma_intra_matrix;
3862
3863 /* for mjpeg, we do include qscale in the matrix */
3864
2/2
✓ Branch 0 taken 90783 times.
✓ Branch 1 taken 1441 times.
92224 for (int i = 1; i < 64; i++) {
3865 90783 int j = s->c.idsp.idct_permutation[i];
3866
3867 90783 s->c.chroma_intra_matrix[j] = av_clip_uint8((chroma_matrix[i] * s->c.qscale) >> 3);
3868 90783 s->c. intra_matrix[j] = av_clip_uint8(( luma_matrix[i] * s->c.qscale) >> 3);
3869 }
3870 1441 s->c.chroma_intra_matrix[0] = s->c.intra_matrix[0] = 8 >> s->c.intra_dc_precision;
3871 } else {
3872 static const uint8_t y[32] = {13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13};
3873 static const uint8_t c[32] = {14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14};
3874
2/2
✓ Branch 0 taken 12600 times.
✓ Branch 1 taken 200 times.
12800 for (int i = 1; i < 64; i++) {
3875 12600 int j = s->c.idsp.idct_permutation[ff_zigzag_direct[i]];
3876
3877 12600 s->c.intra_matrix[j] = sp5x_qscale_five_quant_table[0][i];
3878 12600 s->c.chroma_intra_matrix[j] = sp5x_qscale_five_quant_table[1][i];
3879 }
3880 200 s->c.y_dc_scale_table = y;
3881 200 s->c.c_dc_scale_table = c;
3882 200 s->c.intra_matrix[0] = 13;
3883 200 s->c.chroma_intra_matrix[0] = 14;
3884 }
3885 1641 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3886 1641 s->c.intra_matrix, s->intra_quant_bias, 8, 8, 1);
3887 1641 ff_convert_matrix(s, s->q_chroma_intra_matrix, s->q_chroma_intra_matrix16,
3888 1641 s->c.chroma_intra_matrix, s->intra_quant_bias, 8, 8, 1);
3889 1641 s->c.qscale = 8;
3890 }
3891
3892
2/2
✓ Branch 0 taken 3105 times.
✓ Branch 1 taken 9358 times.
12463 if (s->c.pict_type == AV_PICTURE_TYPE_I) {
3893 3105 s->c.cur_pic.ptr->f->flags |= AV_FRAME_FLAG_KEY;
3894 } else {
3895 9358 s->c.cur_pic.ptr->f->flags &= ~AV_FRAME_FLAG_KEY;
3896 }
3897 12463 s->c.cur_pic.ptr->f->pict_type = s->c.pict_type;
3898
3899
2/2
✓ Branch 0 taken 3105 times.
✓ Branch 1 taken 9358 times.
12463 if (s->c.cur_pic.ptr->f->flags & AV_FRAME_FLAG_KEY)
3900 3105 m->picture_in_gop_number = 0;
3901
3902 12463 s->c.mb_x = s->c.mb_y = 0;
3903 12463 s->last_bits= put_bits_count(&s->pb);
3904 12463 ret = m->encode_picture_header(m);
3905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12463 times.
12463 if (ret < 0)
3906 ✗ return ret;
3907 12463 bits= put_bits_count(&s->pb);
3908 12463 m->header_bits = bits - s->last_bits;
3909
3910
2/2
✓ Branch 0 taken 3468 times.
✓ Branch 1 taken 12463 times.
15931 for (int i = 1; i < context_count; i++)
3911 3468 update_duplicate_context_after_me(s->c.enc_contexts[i], s);
3912 12463 s->c.avctx->execute(s->c.avctx, encode_thread, &s->c.enc_contexts[0],
3913 NULL, context_count, sizeof(void*));
3914
2/2
✓ Branch 0 taken 3468 times.
✓ Branch 1 taken 12463 times.
15931 for (int i = 1; i < context_count; i++) {
3915
2/2
✓ Branch 0 taken 3396 times.
✓ Branch 1 taken 72 times.
3468 if (s->pb.buf_end == s->c.enc_contexts[i]->pb.buf)
3916 3396 set_put_bits_buffer_size(&s->pb, FFMIN(s->c.enc_contexts[i]->pb.buf_end - s->pb.buf, INT_MAX/8-BUF_BITS));
3917 3468 merge_context_after_encode(s, s->c.enc_contexts[i]);
3918 }
3919 12463 emms_c();
3920 12463 return 0;
3921 }
3922
3923 101608621 static inline void denoise_dct(MPVEncContext *const s, int16_t block[])
3924 {
3925
2/2
✓ Branch 0 taken 100569275 times.
✓ Branch 1 taken 1039346 times.
101608621 if (!s->dct_error_sum)
3926 100569275 return;
3927
3928 1039346 const int intra = s->c.mb_intra;
3929 1039346 s->dct_count[intra]++;
3930 1039346 s->mpvencdsp.denoise_dct(block, s->dct_error_sum[intra], s->dct_offset[intra]);
3931 }
3932
3933 8459139 static int dct_quantize_trellis_c(MPVEncContext *const s,
3934 int16_t *block, int n,
3935 int qscale, int *overflow){
3936 const int *qmat;
3937 const uint16_t *matrix;
3938 const uint8_t *scantable;
3939 const uint8_t *perm_scantable;
3940 8459139 int max=0;
3941 unsigned int threshold1, threshold2;
3942 8459139 int bias=0;
3943 int run_tab[65];
3944 int level_tab[65];
3945 int score_tab[65];
3946 int survivor[65];
3947 int survivor_count;
3948 8459139 int last_run=0;
3949 8459139 int last_level=0;
3950 8459139 int last_score= 0;
3951 int last_i;
3952 int coeff[2][64];
3953 int coeff_count[64];
3954 int qmul, qadd, start_i, last_non_zero, i, dc;
3955 8459139 const int esc_length= s->ac_esc_length;
3956 const uint8_t *length, *last_length;
3957 8459139 const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3958 int mpeg2_qscale;
3959
3960 8459139 s->fdsp.fdct(block);
3961
3962 8459139 denoise_dct(s, block);
3963
3964 8459139 qmul= qscale*16;
3965 8459139 qadd= ((qscale-1)|1)*8;
3966
3967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8459139 times.
8459139 if (s->c.q_scale_type) mpeg2_qscale = ff_mpeg2_non_linear_qscale[qscale];
3968 8459139 else mpeg2_qscale = qscale << 1;
3969
3970
2/2
✓ Branch 0 taken 1911148 times.
✓ Branch 1 taken 6547991 times.
8459139 if (s->c.mb_intra) {
3971 int q;
3972 1911148 scantable = s->c.intra_scantable.scantable;
3973 1911148 perm_scantable = s->c.intra_scantable.permutated;
3974
1/2
✓ Branch 0 taken 1911148 times.
✗ Branch 1 not taken.
1911148 if (!s->c.h263_aic) {
3975
2/2
✓ Branch 0 taken 1222536 times.
✓ Branch 1 taken 688612 times.
1911148 if (n < 4)
3976 1222536 q = s->c.y_dc_scale;
3977 else
3978 688612 q = s->c.c_dc_scale;
3979 1911148 q = q << 3;
3980 } else{
3981 /* For AIC we skip quant/dequant of INTRADC */
3982 ✗ q = 1 << 3;
3983 ✗ qadd=0;
3984 }
3985
3986 /* note: block[0] is assumed to be positive */
3987 1911148 block[0] = (block[0] + (q >> 1)) / q;
3988 1911148 start_i = 1;
3989 1911148 last_non_zero = 0;
3990
2/2
✓ Branch 0 taken 1222536 times.
✓ Branch 1 taken 688612 times.
1911148 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
3991
2/2
✓ Branch 0 taken 1222536 times.
✓ Branch 1 taken 688612 times.
1911148 matrix = n < 4 ? s->c.intra_matrix : s->c.chroma_intra_matrix;
3992
5/6
✓ Branch 0 taken 1365360 times.
✓ Branch 1 taken 545788 times.
✓ Branch 2 taken 1365360 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 718200 times.
✓ Branch 5 taken 647160 times.
1911148 if (s->mpeg_quant || s->c.out_format == FMT_MPEG1 || s->c.out_format == FMT_MJPEG)
3993 1263988 bias= 1<<(QMAT_SHIFT-1);
3994
3995
4/4
✓ Branch 0 taken 688612 times.
✓ Branch 1 taken 1222536 times.
✓ Branch 2 taken 239400 times.
✓ Branch 3 taken 449212 times.
1911148 if (n > 3 && s->intra_chroma_ac_vlc_length) {
3996 239400 length = s->intra_chroma_ac_vlc_length;
3997 239400 last_length= s->intra_chroma_ac_vlc_last_length;
3998 } else {
3999 1671748 length = s->intra_ac_vlc_length;
4000 1671748 last_length= s->intra_ac_vlc_last_length;
4001 }
4002 } else {
4003 6547991 scantable = s->c.inter_scantable.scantable;
4004 6547991 perm_scantable = s->c.inter_scantable.permutated;
4005 6547991 start_i = 0;
4006 6547991 last_non_zero = -1;
4007 6547991 qmat = s->q_inter_matrix[qscale];
4008 6547991 matrix = s->c.inter_matrix;
4009 6547991 length = s->inter_ac_vlc_length;
4010 6547991 last_length= s->inter_ac_vlc_last_length;
4011 }
4012 8459139 last_i= start_i;
4013
4014 8459139 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4015 8459139 threshold2= (threshold1<<1);
4016
4017
2/2
✓ Branch 0 taken 366653336 times.
✓ Branch 1 taken 2077612 times.
368730948 for(i=63; i>=start_i; i--) {
4018 366653336 const int j = scantable[i];
4019 366653336 int64_t level = (int64_t)block[j] * qmat[j];
4020
4021
2/2
✓ Branch 0 taken 6381527 times.
✓ Branch 1 taken 360271809 times.
366653336 if(((uint64_t)(level+threshold1))>threshold2){
4022 6381527 last_non_zero = i;
4023 6381527 break;
4024 }
4025 }
4026
4027
2/2
✓ Branch 0 taken 179201939 times.
✓ Branch 1 taken 8459139 times.
187661078 for(i=start_i; i<=last_non_zero; i++) {
4028 179201939 const int j = scantable[i];
4029 179201939 int64_t level = (int64_t)block[j] * qmat[j];
4030
4031 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
4032 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
4033
2/2
✓ Branch 0 taken 61513068 times.
✓ Branch 1 taken 117688871 times.
179201939 if(((uint64_t)(level+threshold1))>threshold2){
4034
2/2
✓ Branch 0 taken 30664772 times.
✓ Branch 1 taken 30848296 times.
61513068 if(level>0){
4035 30664772 level= (bias + level)>>QMAT_SHIFT;
4036 30664772 coeff[0][i]= level;
4037 30664772 coeff[1][i]= level-1;
4038 // coeff[2][k]= level-2;
4039 }else{
4040 30848296 level= (bias - level)>>QMAT_SHIFT;
4041 30848296 coeff[0][i]= -level;
4042 30848296 coeff[1][i]= -level+1;
4043 // coeff[2][k]= -level+2;
4044 }
4045 61513068 coeff_count[i]= FFMIN(level, 2);
4046 av_assert2(coeff_count[i]);
4047 61513068 max |=level;
4048 }else{
4049 117688871 coeff[0][i]= (level>>31)|1;
4050 117688871 coeff_count[i]= 1;
4051 }
4052 }
4053
4054 8459139 *overflow= s->max_qcoeff < max; //overflow might have happened
4055
4056
2/2
✓ Branch 0 taken 2077612 times.
✓ Branch 1 taken 6381527 times.
8459139 if(last_non_zero < start_i){
4057 2077612 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4058 2077612 return last_non_zero;
4059 }
4060
4061 6381527 score_tab[start_i]= 0;
4062 6381527 survivor[0]= start_i;
4063 6381527 survivor_count= 1;
4064
4065
2/2
✓ Branch 0 taken 179201939 times.
✓ Branch 1 taken 6381527 times.
185583466 for(i=start_i; i<=last_non_zero; i++){
4066 int level_index, j, zero_distortion;
4067 179201939 int dct_coeff= FFABS(block[ scantable[i] ]);
4068 179201939 int best_score=256*256*256*120;
4069
4070
1/2
✓ Branch 0 taken 179201939 times.
✗ Branch 1 not taken.
179201939 if (s->fdsp.fdct == ff_fdct_ifast)
4071 179201939 dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
4072 179201939 zero_distortion= dct_coeff*dct_coeff;
4073
4074
2/2
✓ Branch 0 taken 205076339 times.
✓ Branch 1 taken 179201939 times.
384278278 for(level_index=0; level_index < coeff_count[i]; level_index++){
4075 int distortion;
4076 205076339 int level= coeff[level_index][i];
4077 205076339 const int alevel= FFABS(level);
4078 int unquant_coeff;
4079
4080 av_assert2(level);
4081
4082
4/4
✓ Branch 0 taken 111255542 times.
✓ Branch 1 taken 93820797 times.
✓ Branch 2 taken 7518283 times.
✓ Branch 3 taken 103737259 times.
205076339 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4083 101339080 unquant_coeff= alevel*qmul + qadd;
4084
2/2
✓ Branch 0 taken 21010944 times.
✓ Branch 1 taken 82726315 times.
103737259 } else if (s->c.out_format == FMT_MJPEG) {
4085 21010944 j = s->c.idsp.idct_permutation[scantable[i]];
4086 21010944 unquant_coeff = alevel * matrix[j] * 8;
4087 }else{ // MPEG-1
4088 82726315 j = s->c.idsp.idct_permutation[scantable[i]]; // FIXME: optimize
4089
2/2
✓ Branch 0 taken 15790877 times.
✓ Branch 1 taken 66935438 times.
82726315 if (s->c.mb_intra) {
4090 15790877 unquant_coeff = (int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4091 15790877 unquant_coeff = (unquant_coeff - 1) | 1;
4092 }else{
4093 66935438 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[j])) >> 5;
4094 66935438 unquant_coeff = (unquant_coeff - 1) | 1;
4095 }
4096 82726315 unquant_coeff<<= 3;
4097 }
4098
4099 205076339 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4100 205076339 level+=64;
4101
2/2
✓ Branch 0 taken 204974554 times.
✓ Branch 1 taken 101785 times.
205076339 if((level&(~127)) == 0){
4102
2/2
✓ Branch 0 taken 693579132 times.
✓ Branch 1 taken 204974554 times.
898553686 for(j=survivor_count-1; j>=0; j--){
4103 693579132 int run= i - survivor[j];
4104 693579132 int score= distortion + length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4105 693579132 score += score_tab[i-run];
4106
4107
2/2
✓ Branch 0 taken 425271023 times.
✓ Branch 1 taken 268308109 times.
693579132 if(score < best_score){
4108 425271023 best_score= score;
4109 425271023 run_tab[i+1]= run;
4110 425271023 level_tab[i+1]= level-64;
4111 }
4112 }
4113
4114
4/4
✓ Branch 0 taken 111186669 times.
✓ Branch 1 taken 93787885 times.
✓ Branch 2 taken 7518283 times.
✓ Branch 3 taken 103668386 times.
204974554 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4115
2/2
✓ Branch 0 taken 352387873 times.
✓ Branch 1 taken 101306168 times.
453694041 for(j=survivor_count-1; j>=0; j--){
4116 352387873 int run= i - survivor[j];
4117 352387873 int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
4118 352387873 score += score_tab[i-run];
4119
2/2
✓ Branch 0 taken 36176666 times.
✓ Branch 1 taken 316211207 times.
352387873 if(score < last_score){
4120 36176666 last_score= score;
4121 36176666 last_run= run;
4122 36176666 last_level= level-64;
4123 36176666 last_i= i+1;
4124 }
4125 }
4126 }
4127 }else{
4128 101785 distortion += esc_length*lambda;
4129
2/2
✓ Branch 0 taken 105007 times.
✓ Branch 1 taken 101785 times.
206792 for(j=survivor_count-1; j>=0; j--){
4130 105007 int run= i - survivor[j];
4131 105007 int score= distortion + score_tab[i-run];
4132
4133
2/2
✓ Branch 0 taken 53572 times.
✓ Branch 1 taken 51435 times.
105007 if(score < best_score){
4134 53572 best_score= score;
4135 53572 run_tab[i+1]= run;
4136 53572 level_tab[i+1]= level-64;
4137 }
4138 }
4139
4140
3/4
✓ Branch 0 taken 68873 times.
✓ Branch 1 taken 32912 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 68873 times.
101785 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4141
2/2
✓ Branch 0 taken 34132 times.
✓ Branch 1 taken 32912 times.
67044 for(j=survivor_count-1; j>=0; j--){
4142 34132 int run= i - survivor[j];
4143 34132 int score= distortion + score_tab[i-run];
4144
2/2
✓ Branch 0 taken 17414 times.
✓ Branch 1 taken 16718 times.
34132 if(score < last_score){
4145 17414 last_score= score;
4146 17414 last_run= run;
4147 17414 last_level= level-64;
4148 17414 last_i= i+1;
4149 }
4150 }
4151 }
4152 }
4153 }
4154
4155 179201939 score_tab[i+1]= best_score;
4156
4157 // Note: there is a vlc code in MPEG-4 which is 1 bit shorter then another one with a shorter run and the same level
4158
2/2
✓ Branch 0 taken 37762357 times.
✓ Branch 1 taken 141439582 times.
179201939 if(last_non_zero <= 27){
4159
2/2
✓ Branch 0 taken 62316500 times.
✓ Branch 1 taken 10633222 times.
72949722 for(; survivor_count; survivor_count--){
4160
2/2
✓ Branch 0 taken 27129135 times.
✓ Branch 1 taken 35187365 times.
62316500 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4161 27129135 break;
4162 }
4163 }else{
4164
2/2
✓ Branch 0 taken 235857959 times.
✓ Branch 1 taken 40003691 times.
275861650 for(; survivor_count; survivor_count--){
4165
2/2
✓ Branch 0 taken 101435891 times.
✓ Branch 1 taken 134422068 times.
235857959 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4166 101435891 break;
4167 }
4168 }
4169
4170 179201939 survivor[ survivor_count++ ]= i+1;
4171 }
4172
4173
4/4
✓ Branch 0 taken 3492117 times.
✓ Branch 1 taken 2889410 times.
✓ Branch 2 taken 3234524 times.
✓ Branch 3 taken 257593 times.
6381527 if (s->c.out_format != FMT_H263 && s->c.out_format != FMT_H261) {
4174 3234524 last_score= 256*256*256*120;
4175
2/2
✓ Branch 0 taken 30156323 times.
✓ Branch 1 taken 3234524 times.
33390847 for(i= survivor[0]; i<=last_non_zero + 1; i++){
4176 30156323 int score= score_tab[i];
4177
2/2
✓ Branch 0 taken 29808692 times.
✓ Branch 1 taken 347631 times.
30156323 if (i)
4178 29808692 score += lambda * 2; // FIXME more exact?
4179
4180
2/2
✓ Branch 0 taken 3368440 times.
✓ Branch 1 taken 26787883 times.
30156323 if(score < last_score){
4181 3368440 last_score= score;
4182 3368440 last_i= i;
4183 3368440 last_level= level_tab[i];
4184 3368440 last_run= run_tab[i];
4185 }
4186 }
4187 }
4188
4189 6381527 s->coded_score[n] = last_score;
4190
4191 6381527 dc= FFABS(block[0]);
4192 6381527 last_non_zero= last_i - 1;
4193 6381527 memset(block + start_i, 0, (64-start_i)*sizeof(int16_t));
4194
4195
2/2
✓ Branch 0 taken 1072041 times.
✓ Branch 1 taken 5309486 times.
6381527 if(last_non_zero < start_i)
4196 1072041 return last_non_zero;
4197
4198
3/4
✓ Branch 0 taken 516513 times.
✓ Branch 1 taken 4792973 times.
✓ Branch 2 taken 516513 times.
✗ Branch 3 not taken.
5309486 if(last_non_zero == 0 && start_i == 0){
4199 516513 int best_level= 0;
4200 516513 int best_score= dc * dc;
4201
4202
2/2
✓ Branch 0 taken 719139 times.
✓ Branch 1 taken 516513 times.
1235652 for(i=0; i<coeff_count[0]; i++){
4203 719139 int level= coeff[i][0];
4204 719139 int alevel= FFABS(level);
4205 int unquant_coeff, score, distortion;
4206
4207
4/4
✓ Branch 0 taken 392700 times.
✓ Branch 1 taken 326439 times.
✓ Branch 2 taken 20722 times.
✓ Branch 3 taken 371978 times.
719139 if (s->c.out_format == FMT_H263 || s->c.out_format == FMT_H261) {
4208 347161 unquant_coeff= (alevel*qmul + qadd)>>3;
4209 } else{ // MPEG-1
4210 371978 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int) matrix[0])) >> 5;
4211 371978 unquant_coeff = (unquant_coeff - 1) | 1;
4212 }
4213 719139 unquant_coeff = (unquant_coeff + 4) >> 3;
4214 719139 unquant_coeff<<= 3 + 3;
4215
4216 719139 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
4217 719139 level+=64;
4218
2/2
✓ Branch 0 taken 716874 times.
✓ Branch 1 taken 2265 times.
719139 if((level&(~127)) == 0) score= distortion + last_length[UNI_AC_ENC_INDEX(0, level)]*lambda;
4219 2265 else score= distortion + esc_length*lambda;
4220
4221
2/2
✓ Branch 0 taken 563096 times.
✓ Branch 1 taken 156043 times.
719139 if(score < best_score){
4222 563096 best_score= score;
4223 563096 best_level= level - 64;
4224 }
4225 }
4226 516513 block[0]= best_level;
4227 516513 s->coded_score[n] = best_score - dc*dc;
4228
2/2
✓ Branch 0 taken 6359 times.
✓ Branch 1 taken 510154 times.
516513 if(best_level == 0) return -1;
4229 510154 else return last_non_zero;
4230 }
4231
4232 4792973 i= last_i;
4233 av_assert2(last_level);
4234
4235 4792973 block[ perm_scantable[last_non_zero] ]= last_level;
4236 4792973 i -= last_run + 1;
4237
4238
2/2
✓ Branch 0 taken 52603989 times.
✓ Branch 1 taken 4792973 times.
57396962 for(; i>start_i; i -= run_tab[i] + 1){
4239 52603989 block[ perm_scantable[i-1] ]= level_tab[i];
4240 }
4241
4242 4792973 return last_non_zero;
4243 }
4244
4245 static DECLARE_ALIGNED(16, int16_t, basis)[64][64];
4246
4247 ✗ static void build_basis(uint8_t *perm){
4248 int i, j, x, y;
4249 ✗ emms_c();
4250 ✗ for(i=0; i<8; i++){
4251 ✗ for(j=0; j<8; j++){
4252 ✗ for(y=0; y<8; y++){
4253 ✗ for(x=0; x<8; x++){
4254 ✗ double s= 0.25*(1<<BASIS_SHIFT);
4255 ✗ int index= 8*i + j;
4256 ✗ int perm_index= perm[index];
4257 ✗ if(i==0) s*= sqrt(0.5);
4258 ✗ if(j==0) s*= sqrt(0.5);
4259 ✗ basis[perm_index][8*x + y]= lrintf(s * cos((M_PI/8.0)*i*(x+0.5)) * cos((M_PI/8.0)*j*(y+0.5)));
4260 }
4261 }
4262 }
4263 }
4264 ✗ }
4265
4266 ✗ static int dct_quantize_refine(MPVEncContext *const s, //FIXME breaks denoise?
4267 int16_t *block, int16_t *weight, int16_t *orig,
4268 int n, int qscale){
4269 DECLARE_ALIGNED(16, int16_t, rem)[64];
4270 ✗ LOCAL_ALIGNED_16(int16_t, d1, [64]);
4271 const uint8_t *scantable;
4272 const uint8_t *perm_scantable;
4273 // unsigned int threshold1, threshold2;
4274 // int bias=0;
4275 int run_tab[65];
4276 ✗ int prev_run=0;
4277 ✗ int prev_level=0;
4278 int qmul, qadd, start_i, last_non_zero, i, dc;
4279 const uint8_t *length;
4280 const uint8_t *last_length;
4281 int lambda;
4282 ✗ int rle_index, run, q = 1, sum; //q is only used when s->c.mb_intra is true
4283
4284 ✗ if(basis[0][0] == 0)
4285 ✗ build_basis(s->c.idsp.idct_permutation);
4286
4287 ✗ qmul= qscale*2;
4288 ✗ qadd= (qscale-1)|1;
4289 ✗ if (s->c.mb_intra) {
4290 ✗ scantable = s->c.intra_scantable.scantable;
4291 ✗ perm_scantable = s->c.intra_scantable.permutated;
4292 ✗ if (!s->c.h263_aic) {
4293 ✗ if (n < 4)
4294 ✗ q = s->c.y_dc_scale;
4295 else
4296 ✗ q = s->c.c_dc_scale;
4297 } else{
4298 /* For AIC we skip quant/dequant of INTRADC */
4299 ✗ q = 1;
4300 ✗ qadd=0;
4301 }
4302 ✗ q <<= RECON_SHIFT-3;
4303 /* note: block[0] is assumed to be positive */
4304 ✗ dc= block[0]*q;
4305 // block[0] = (block[0] + (q >> 1)) / q;
4306 ✗ start_i = 1;
4307 // if (s->mpeg_quant || s->c.out_format == FMT_MPEG1)
4308 // bias= 1<<(QMAT_SHIFT-1);
4309 ✗ if (n > 3 && s->intra_chroma_ac_vlc_length) {
4310 ✗ length = s->intra_chroma_ac_vlc_length;
4311 ✗ last_length= s->intra_chroma_ac_vlc_last_length;
4312 } else {
4313 ✗ length = s->intra_ac_vlc_length;
4314 ✗ last_length= s->intra_ac_vlc_last_length;
4315 }
4316 } else {
4317 ✗ scantable = s->c.inter_scantable.scantable;
4318 ✗ perm_scantable = s->c.inter_scantable.permutated;
4319 ✗ dc= 0;
4320 ✗ start_i = 0;
4321 ✗ length = s->inter_ac_vlc_length;
4322 ✗ last_length= s->inter_ac_vlc_last_length;
4323 }
4324 ✗ last_non_zero = s->c.block_last_index[n];
4325
4326 ✗ dc += (1<<(RECON_SHIFT-1));
4327 ✗ for(i=0; i<64; i++){
4328 ✗ rem[i] = dc - (orig[i] << RECON_SHIFT); // FIXME use orig directly instead of copying to rem[]
4329 }
4330
4331 ✗ sum=0;
4332 ✗ for(i=0; i<64; i++){
4333 ✗ int one= 36;
4334 ✗ int qns=4;
4335 int w;
4336
4337 ✗ w= FFABS(weight[i]) + qns*one;
4338 ✗ w= 15 + (48*qns*one + w/2)/w; // 16 .. 63
4339
4340 ✗ weight[i] = w;
4341 // w=weight[i] = (63*qns + (w/2)) / w;
4342
4343 av_assert2(w>0);
4344 av_assert2(w<(1<<6));
4345 ✗ sum += w*w;
4346 }
4347 ✗ lambda = sum*(uint64_t)s->lambda2 >> (FF_LAMBDA_SHIFT - 6 + 6 + 6 + 6);
4348
4349 ✗ run=0;
4350 ✗ rle_index=0;
4351 ✗ for(i=start_i; i<=last_non_zero; i++){
4352 ✗ int j= perm_scantable[i];
4353 ✗ const int level= block[j];
4354 int coeff;
4355
4356 ✗ if(level){
4357 ✗ if(level<0) coeff= qmul*level - qadd;
4358 ✗ else coeff= qmul*level + qadd;
4359 ✗ run_tab[rle_index++]=run;
4360 ✗ run=0;
4361
4362 ✗ s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
4363 }else{
4364 ✗ run++;
4365 }
4366 }
4367
4368 ✗ for(;;){
4369 ✗ int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
4370 ✗ int best_coeff=0;
4371 ✗ int best_change=0;
4372 ✗ int run2, best_unquant_change=0, analyze_gradient;
4373 ✗ analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
4374
4375 ✗ if(analyze_gradient){
4376 ✗ for(i=0; i<64; i++){
4377 ✗ int w= weight[i];
4378
4379 ✗ d1[i] = (rem[i]*w*w + (1<<(RECON_SHIFT+12-1)))>>(RECON_SHIFT+12);
4380 }
4381 ✗ s->fdsp.fdct(d1);
4382 }
4383
4384 ✗ if(start_i){
4385 ✗ const int level= block[0];
4386 int change, old_coeff;
4387
4388 av_assert2(s->c.mb_intra);
4389
4390 ✗ old_coeff= q*level;
4391
4392 ✗ for(change=-1; change<=1; change+=2){
4393 ✗ int new_level= level + change;
4394 int score, new_coeff;
4395
4396 ✗ new_coeff= q*new_level;
4397 ✗ if(new_coeff >= 2048 || new_coeff < 0)
4398 ✗ continue;
4399
4400 ✗ score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
4401 new_coeff - old_coeff);
4402 ✗ if(score<best_score){
4403 ✗ best_score= score;
4404 ✗ best_coeff= 0;
4405 ✗ best_change= change;
4406 ✗ best_unquant_change= new_coeff - old_coeff;
4407 }
4408 }
4409 }
4410
4411 ✗ run=0;
4412 ✗ rle_index=0;
4413 ✗ run2= run_tab[rle_index++];
4414 ✗ prev_level=0;
4415 ✗ prev_run=0;
4416
4417 ✗ for(i=start_i; i<64; i++){
4418 ✗ int j= perm_scantable[i];
4419 ✗ const int level= block[j];
4420 int change, old_coeff;
4421
4422 ✗ if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
4423 ✗ break;
4424
4425 ✗ if(level){
4426 ✗ if(level<0) old_coeff= qmul*level - qadd;
4427 ✗ else old_coeff= qmul*level + qadd;
4428 ✗ run2= run_tab[rle_index++]; //FIXME ! maybe after last
4429 }else{
4430 ✗ old_coeff=0;
4431 ✗ run2--;
4432 av_assert2(run2>=0 || i >= last_non_zero );
4433 }
4434
4435 ✗ for(change=-1; change<=1; change+=2){
4436 ✗ int new_level= level + change;
4437 int score, new_coeff, unquant_change;
4438
4439 ✗ score=0;
4440 ✗ if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
4441 ✗ continue;
4442
4443 ✗ if(new_level){
4444 ✗ if(new_level<0) new_coeff= qmul*new_level - qadd;
4445 ✗ else new_coeff= qmul*new_level + qadd;
4446 ✗ if(new_coeff >= 2048 || new_coeff <= -2048)
4447 ✗ continue;
4448 //FIXME check for overflow
4449
4450 ✗ if(level){
4451 ✗ if(level < 63 && level > -63){
4452 ✗ if(i < last_non_zero)
4453 ✗ score += length[UNI_AC_ENC_INDEX(run, new_level+64)]
4454 ✗ - length[UNI_AC_ENC_INDEX(run, level+64)];
4455 else
4456 ✗ score += last_length[UNI_AC_ENC_INDEX(run, new_level+64)]
4457 ✗ - last_length[UNI_AC_ENC_INDEX(run, level+64)];
4458 }
4459 }else{
4460 av_assert2(FFABS(new_level)==1);
4461
4462 ✗ if(analyze_gradient){
4463 ✗ int g= d1[ scantable[i] ];
4464 ✗ if(g && (g^new_level) >= 0)
4465 ✗ continue;
4466 }
4467
4468 ✗ if(i < last_non_zero){
4469 ✗ int next_i= i + run2 + 1;
4470 ✗ int next_level= block[ perm_scantable[next_i] ] + 64;
4471
4472 ✗ if(next_level&(~127))
4473 ✗ next_level= 0;
4474
4475 ✗ if(next_i < last_non_zero)
4476 ✗ score += length[UNI_AC_ENC_INDEX(run, 65)]
4477 ✗ + length[UNI_AC_ENC_INDEX(run2, next_level)]
4478 ✗ - length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4479 else
4480 ✗ score += length[UNI_AC_ENC_INDEX(run, 65)]
4481 ✗ + last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4482 ✗ - last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)];
4483 }else{
4484 ✗ score += last_length[UNI_AC_ENC_INDEX(run, 65)];
4485 ✗ if(prev_level){
4486 ✗ score += length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4487 ✗ - last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4488 }
4489 }
4490 }
4491 }else{
4492 ✗ new_coeff=0;
4493 av_assert2(FFABS(level)==1);
4494
4495 ✗ if(i < last_non_zero){
4496 ✗ int next_i= i + run2 + 1;
4497 ✗ int next_level= block[ perm_scantable[next_i] ] + 64;
4498
4499 ✗ if(next_level&(~127))
4500 ✗ next_level= 0;
4501
4502 ✗ if(next_i < last_non_zero)
4503 ✗ score += length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4504 ✗ - length[UNI_AC_ENC_INDEX(run2, next_level)]
4505 ✗ - length[UNI_AC_ENC_INDEX(run, 65)];
4506 else
4507 ✗ score += last_length[UNI_AC_ENC_INDEX(run + run2 + 1, next_level)]
4508 ✗ - last_length[UNI_AC_ENC_INDEX(run2, next_level)]
4509 ✗ - length[UNI_AC_ENC_INDEX(run, 65)];
4510 }else{
4511 ✗ score += -last_length[UNI_AC_ENC_INDEX(run, 65)];
4512 ✗ if(prev_level){
4513 ✗ score += last_length[UNI_AC_ENC_INDEX(prev_run, prev_level)]
4514 ✗ - length[UNI_AC_ENC_INDEX(prev_run, prev_level)];
4515 }
4516 }
4517 }
4518
4519 ✗ score *= lambda;
4520
4521 ✗ unquant_change= new_coeff - old_coeff;
4522 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4523
4524 ✗ score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
4525 unquant_change);
4526 ✗ if(score<best_score){
4527 ✗ best_score= score;
4528 ✗ best_coeff= i;
4529 ✗ best_change= change;
4530 ✗ best_unquant_change= unquant_change;
4531 }
4532 }
4533 ✗ if(level){
4534 ✗ prev_level= level + 64;
4535 ✗ if(prev_level&(~127))
4536 ✗ prev_level= 0;
4537 ✗ prev_run= run;
4538 ✗ run=0;
4539 }else{
4540 ✗ run++;
4541 }
4542 }
4543
4544 ✗ if(best_change){
4545 ✗ int j= perm_scantable[ best_coeff ];
4546
4547 ✗ block[j] += best_change;
4548
4549 ✗ if(best_coeff > last_non_zero){
4550 ✗ last_non_zero= best_coeff;
4551 av_assert2(block[j]);
4552 }else{
4553 ✗ for(; last_non_zero>=start_i; last_non_zero--){
4554 ✗ if(block[perm_scantable[last_non_zero]])
4555 ✗ break;
4556 }
4557 }
4558
4559 ✗ run=0;
4560 ✗ rle_index=0;
4561 ✗ for(i=start_i; i<=last_non_zero; i++){
4562 ✗ const int level = block[perm_scantable[i]];
4563
4564 ✗ if(level){
4565 ✗ run_tab[rle_index++]=run;
4566 ✗ run=0;
4567 }else{
4568 ✗ run++;
4569 }
4570 }
4571
4572 ✗ s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4573 }else{
4574 ✗ break;
4575 }
4576 }
4577
4578 ✗ return last_non_zero;
4579 }
4580
4581 /**
4582 * Permute an 8x8 block according to permutation.
4583 * @param block the block which will be permuted according to
4584 * the given permutation vector
4585 * @param permutation the permutation vector
4586 * @param last the last non zero coefficient in scantable order, used to
4587 * speed the permutation up
4588 * @param scantable the used scantable, this is only used to speed the
4589 * permutation up, the block is not (inverse) permutated
4590 * to scantable order!
4591 */
4592 397889 void ff_block_permute(int16_t *block, const uint8_t *permutation,
4593 const uint8_t *scantable, int last)
4594 {
4595 int i;
4596 int16_t temp[64];
4597
4598
2/2
✓ Branch 0 taken 161103 times.
✓ Branch 1 taken 236786 times.
397889 if (last <= 0)
4599 161103 return;
4600 //FIXME it is ok but not clean and might fail for some permutations
4601 // if (permutation[1] == 1)
4602 // return;
4603
4604
2/2
✓ Branch 0 taken 6875051 times.
✓ Branch 1 taken 236786 times.
7111837 for (i = 0; i <= last; i++) {
4605 6875051 const int j = scantable[i];
4606 6875051 temp[j] = block[j];
4607 6875051 block[j] = 0;
4608 }
4609
4610
2/2
✓ Branch 0 taken 6875051 times.
✓ Branch 1 taken 236786 times.
7111837 for (i = 0; i <= last; i++) {
4611 6875051 const int j = scantable[i];
4612 6875051 const int perm_j = permutation[j];
4613 6875051 block[perm_j] = temp[j];
4614 }
4615 }
4616
4617 93149482 static int dct_quantize_c(MPVEncContext *const s,
4618 int16_t *block, int n,
4619 int qscale, int *overflow)
4620 {
4621 int i, last_non_zero, q, start_i;
4622 const int *qmat;
4623 const uint8_t *scantable;
4624 int bias;
4625 93149482 int max=0;
4626 unsigned int threshold1, threshold2;
4627
4628 93149482 s->fdsp.fdct(block);
4629
4630 93149482 denoise_dct(s, block);
4631
4632
2/2
✓ Branch 0 taken 83509824 times.
✓ Branch 1 taken 9639658 times.
93149482 if (s->c.mb_intra) {
4633 83509824 scantable = s->c.intra_scantable.scantable;
4634
2/2
✓ Branch 0 taken 8573334 times.
✓ Branch 1 taken 74936490 times.
83509824 if (!s->c.h263_aic) {
4635
2/2
✓ Branch 0 taken 4737204 times.
✓ Branch 1 taken 3836130 times.
8573334 if (n < 4)
4636 4737204 q = s->c.y_dc_scale;
4637 else
4638 3836130 q = s->c.c_dc_scale;
4639 8573334 q = q << 3;
4640 } else
4641 /* For AIC we skip quant/dequant of INTRADC */
4642 74936490 q = 1 << 3;
4643
4644 /* note: block[0] is assumed to be positive */
4645 83509824 block[0] = (block[0] + (q >> 1)) / q;
4646 83509824 start_i = 1;
4647 83509824 last_non_zero = 0;
4648
2/2
✓ Branch 0 taken 42219132 times.
✓ Branch 1 taken 41290692 times.
83509824 qmat = n < 4 ? s->q_intra_matrix[qscale] : s->q_chroma_intra_matrix[qscale];
4649 83509824 bias= s->intra_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4650 } else {
4651 9639658 scantable = s->c.inter_scantable.scantable;
4652 9639658 start_i = 0;
4653 9639658 last_non_zero = -1;
4654 9639658 qmat = s->q_inter_matrix[qscale];
4655 9639658 bias= s->inter_quant_bias*(1<<(QMAT_SHIFT - QUANT_BIAS_SHIFT));
4656 }
4657 93149482 threshold1= (1<<QMAT_SHIFT) - bias - 1;
4658 93149482 threshold2= (threshold1<<1);
4659
2/2
✓ Branch 0 taken 4934989593 times.
✓ Branch 1 taken 15914430 times.
4950904023 for(i=63;i>=start_i;i--) {
4660 4934989593 const int j = scantable[i];
4661 4934989593 int64_t level = (int64_t)block[j] * qmat[j];
4662
4663
2/2
✓ Branch 0 taken 77235052 times.
✓ Branch 1 taken 4857754541 times.
4934989593 if(((uint64_t)(level+threshold1))>threshold2){
4664 77235052 last_non_zero = i;
4665 77235052 break;
4666 }else{
4667 4857754541 block[j]=0;
4668 }
4669 }
4670
2/2
✓ Branch 0 taken 1020302483 times.
✓ Branch 1 taken 93149482 times.
1113451965 for(i=start_i; i<=last_non_zero; i++) {
4671 1020302483 const int j = scantable[i];
4672 1020302483 int64_t level = (int64_t)block[j] * qmat[j];
4673
4674 // if( bias+level >= (1<<QMAT_SHIFT)
4675 // || bias-level >= (1<<QMAT_SHIFT)){
4676
2/2
✓ Branch 0 taken 385652957 times.
✓ Branch 1 taken 634649526 times.
1020302483 if(((uint64_t)(level+threshold1))>threshold2){
4677
2/2
✓ Branch 0 taken 191077039 times.
✓ Branch 1 taken 194575918 times.
385652957 if(level>0){
4678 191077039 level= (bias + level)>>QMAT_SHIFT;
4679 191077039 block[j]= level;
4680 }else{
4681 194575918 level= (bias - level)>>QMAT_SHIFT;
4682 194575918 block[j]= -level;
4683 }
4684 385652957 max |=level;
4685 }else{
4686 634649526 block[j]=0;
4687 }
4688 }
4689 93149482 *overflow= s->max_qcoeff < max; //overflow might have happened
4690
4691 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4692
2/2
✓ Branch 0 taken 397889 times.
✓ Branch 1 taken 92751593 times.
93149482 if (s->c.idsp.perm_type != FF_IDCT_PERM_NONE)
4693 397889 ff_block_permute(block, s->c.idsp.idct_permutation,
4694 scantable, last_non_zero);
4695
4696 93149482 return last_non_zero;
4697 }
4698