FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 2038 2885 70.6%
Functions: 45 57 78.9%
Branches: 1158 1834 63.1%

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