FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 2012 2859 70.4%
Functions: 42 54 77.8%
Branches: 1153 1840 62.7%

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