FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2022-01-16 20:33:26
Exec Total Coverage
Lines: 1946 2808 69.3%
Branches: 1127 1828 61.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 <stdint.h>
35
36 #include "libavutil/internal.h"
37 #include "libavutil/intmath.h"
38 #include "libavutil/mathematics.h"
39 #include "libavutil/mem_internal.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/thread.h"
43 #include "avcodec.h"
44 #include "dct.h"
45 #include "encode.h"
46 #include "idctdsp.h"
47 #include "mpeg12.h"
48 #include "mpeg12data.h"
49 #include "mpegvideo.h"
50 #include "mpegvideodata.h"
51 #include "h261.h"
52 #include "h263.h"
53 #include "h263data.h"
54 #include "mjpegenc_common.h"
55 #include "mathops.h"
56 #include "mpegutils.h"
57 #include "mjpegenc.h"
58 #include "speedhqenc.h"
59 #include "msmpeg4.h"
60 #include "pixblockdsp.h"
61 #include "qpeldsp.h"
62 #include "faandct.h"
63 #include "thread.h"
64 #include "aandcttab.h"
65 #include "flv.h"
66 #include "mpeg4video.h"
67 #include "mpeg4videodata.h"
68 #include "internal.h"
69 #include "bytestream.h"
70 #include "wmv2.h"
71 #include "rv10.h"
72 #include "packet_internal.h"
73 #include <limits.h>
74 #include "sp5x.h"
75
76 #define QUANT_BIAS_SHIFT 8
77
78 #define QMAT_SHIFT_MMX 16
79 #define QMAT_SHIFT 21
80
81 static int encode_picture(MpegEncContext *s, int picture_number);
82 static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
83 static int sse_mb(MpegEncContext *s);
84 static void denoise_dct_c(MpegEncContext *s, int16_t *block);
85 static int dct_quantize_trellis_c(MpegEncContext *s, int16_t *block, int n, int qscale, int *overflow);
86
87 static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1];
88 static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
89
90 static const AVOption mpv_generic_options[] = {
91 FF_MPV_COMMON_OPTS
92 #if FF_API_MPEGVIDEO_OPTS
93 FF_MPV_DEPRECATED_MPEG_QUANT_OPT
94 FF_MPV_DEPRECATED_A53_CC_OPT
95 FF_MPV_DEPRECATED_MATRIX_OPT
96 FF_MPV_DEPRECATED_BFRAME_OPTS
97 #endif
98 { NULL },
99 };
100
101 const AVClass ff_mpv_enc_class = {
102 .class_name = "generic mpegvideo encoder",
103 .item_name = av_default_item_name,
104 .option = mpv_generic_options,
105 .version = LIBAVUTIL_VERSION_INT,
106 };
107
108 3718 void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
109 uint16_t (*qmat16)[2][64],
110 const uint16_t *quant_matrix,
111 int bias, int qmin, int qmax, int intra)
112 {
113 3718 FDCTDSPContext *fdsp = &s->fdsp;
114 int qscale;
115 3718 int shift = 0;
116
117
2/2
✓ Branch 0 taken 96946 times.
✓ Branch 1 taken 3718 times.
100664 for (qscale = qmin; qscale <= qmax; qscale++) {
118 int i;
119 int qscale2;
120
121
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 96884 times.
96946 if (s->q_scale_type) qscale2 = ff_mpeg2_non_linear_qscale[qscale];
122 96884 else qscale2 = qscale << 1;
123
124
2/2
✓ Branch 0 taken 96646 times.
✓ Branch 1 taken 300 times.
96946 if (fdsp->fdct == ff_jpeg_fdct_islow_8 ||
125 #if CONFIG_FAANDCT
126
1/2
✓ Branch 0 taken 96646 times.
✗ Branch 1 not taken.
96646 fdsp->fdct == ff_faandct ||
127 #endif /* CONFIG_FAANDCT */
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96646 times.
96646 fdsp->fdct == ff_jpeg_fdct_islow_10) {
129
2/2
✓ Branch 0 taken 19200 times.
✓ Branch 1 taken 300 times.
19500 for (i = 0; i < 64; i++) {
130 19200 const int j = s->idsp.idct_permutation[i];
131 19200 int64_t den = (int64_t) qscale2 * quant_matrix[j];
132 /* 16 <= qscale * quant_matrix[i] <= 7905
133 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
134 * 19952 <= x <= 249205026
135 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
136 * 3444240 >= (1 << 36) / (x) >= 275 */
137
138 19200 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
139 }
140
1/2
✓ Branch 0 taken 96646 times.
✗ Branch 1 not taken.
96646 } else if (fdsp->fdct == ff_fdct_ifast) {
141
2/2
✓ Branch 0 taken 6185344 times.
✓ Branch 1 taken 96646 times.
6281990 for (i = 0; i < 64; i++) {
142 6185344 const int j = s->idsp.idct_permutation[i];
143 6185344 int64_t den = ff_aanscales[i] * (int64_t) qscale2 * quant_matrix[j];
144 /* 16 <= qscale * quant_matrix[i] <= 7905
145 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
146 * 19952 <= x <= 249205026
147 * (1 << 36) / 19952 >= (1 << 36) / (x) >= (1 << 36) / 249205026
148 * 3444240 >= (1 << 36) / (x) >= 275 */
149
150 6185344 qmat[qscale][i] = (int)((UINT64_C(2) << (QMAT_SHIFT + 14)) / den);
151 }
152 } else {
153 for (i = 0; i < 64; i++) {
154 const int j = s->idsp.idct_permutation[i];
155 int64_t den = (int64_t) qscale2 * quant_matrix[j];
156 /* We can safely suppose that 16 <= quant_matrix[i] <= 255
157 * Assume x = qscale * quant_matrix[i]
158 * So 16 <= x <= 7905
159 * so (1 << 19) / 16 >= (1 << 19) / (x) >= (1 << 19) / 7905
160 * so 32768 >= (1 << 19) / (x) >= 67 */
161 qmat[qscale][i] = (int)((UINT64_C(2) << QMAT_SHIFT) / den);
162 //qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) /
163 // (qscale * quant_matrix[i]);
164 qmat16[qscale][0][i] = (2 << QMAT_SHIFT_MMX) / den;
165
166 if (qmat16[qscale][0][i] == 0 ||
167 qmat16[qscale][0][i] == 128 * 256)
168 qmat16[qscale][0][i] = 128 * 256 - 1;
169 qmat16[qscale][1][i] =
170 ROUNDED_DIV(bias * (1<<(16 - QUANT_BIAS_SHIFT)),
171 qmat16[qscale][0][i]);
172 }
173 }
174
175
2/2
✓ Branch 0 taken 6112249 times.
✓ Branch 1 taken 96946 times.
6209195 for (i = intra; i < 64; i++) {
176 6112249 int64_t max = 8191;
177
2/2
✓ Branch 0 taken 6093199 times.
✓ Branch 1 taken 19050 times.
6112249 if (fdsp->fdct == ff_fdct_ifast) {
178 6093199 max = (8191LL * ff_aanscales[i]) >> 14;
179 }
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6112249 times.
6112249 while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
181 shift++;
182 }
183 }
184 }
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3718 times.
3718 if (shift) {
186 av_log(s->avctx, AV_LOG_INFO,
187 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
188 QMAT_SHIFT - shift);
189 }
190 3718 }
191
192 1281992 static inline void update_qscale(MpegEncContext *s)
193 {
194 if (s->q_scale_type == 1 && 0) {
195 int i;
196 int bestdiff=INT_MAX;
197 int best = 1;
198
199 for (i = 0 ; i<FF_ARRAY_ELEMS(ff_mpeg2_non_linear_qscale); i++) {
200 int diff = FFABS((ff_mpeg2_non_linear_qscale[i]<<(FF_LAMBDA_SHIFT + 6)) - (int)s->lambda * 139);
201 if (ff_mpeg2_non_linear_qscale[i] < s->avctx->qmin ||
202 (ff_mpeg2_non_linear_qscale[i] > s->avctx->qmax && !s->vbv_ignore_qmax))
203 continue;
204 if (diff < bestdiff) {
205 bestdiff = diff;
206 best = i;
207 }
208 }
209 s->qscale = best;
210 } else {
211 1281992 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
212 (FF_LAMBDA_SHIFT + 7);
213
2/2
✓ Branch 0 taken 1281867 times.
✓ Branch 1 taken 125 times.
1281992 s->qscale = av_clip(s->qscale, s->avctx->qmin, s->vbv_ignore_qmax ? 31 : s->avctx->qmax);
214 }
215
216 1281992 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
217 FF_LAMBDA_SHIFT;
218 1281992 }
219
220 766 void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
221 {
222 int i;
223
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 766 times.
766 if (matrix) {
225 put_bits(pb, 1, 1);
226 for (i = 0; i < 64; i++) {
227 put_bits(pb, 8, matrix[ff_zigzag_direct[i]]);
228 }
229 } else
230 766 put_bits(pb, 1, 0);
231 766 }
232
233 /**
234 * init s->current_picture.qscale_table from s->lambda_table
235 */
236 800 void ff_init_qscale_tab(MpegEncContext *s)
237 {
238 800 int8_t * const qscale_table = s->current_picture.qscale_table;
239 int i;
240
241
2/2
✓ Branch 0 taken 239550 times.
✓ Branch 1 taken 800 times.
240350 for (i = 0; i < s->mb_num; i++) {
242 239550 unsigned int lam = s->lambda_table[s->mb_index2xy[i]];
243 239550 int qp = (lam * 139 + FF_LAMBDA_SCALE * 64) >> (FF_LAMBDA_SHIFT + 7);
244 239550 qscale_table[s->mb_index2xy[i]] = av_clip(qp, s->avctx->qmin,
245 239550 s->avctx->qmax);
246 }
247 800 }
248
249 600 static void update_duplicate_context_after_me(MpegEncContext *dst,
250 MpegEncContext *src)
251 {
252 #define COPY(a) dst->a= src->a
253 600 COPY(pict_type);
254 600 COPY(current_picture);
255 600 COPY(f_code);
256 600 COPY(b_code);
257 600 COPY(qscale);
258 600 COPY(lambda);
259 600 COPY(lambda2);
260 600 COPY(picture_in_gop_number);
261 600 COPY(gop_picture_number);
262 600 COPY(frame_pred_frame_dct); // FIXME don't set in encode_header
263 600 COPY(progressive_frame); // FIXME don't set in encode_header
264 600 COPY(partitioned_frame); // FIXME don't set in encode_header
265 #undef COPY
266 600 }
267
268 185 static void mpv_encode_init_static(void)
269 {
270
2/2
✓ Branch 0 taken 5920 times.
✓ Branch 1 taken 185 times.
6105 for (int i = -16; i < 16; i++)
271 5920 default_fcode_tab[i + MAX_MV] = 1;
272 185 }
273
274 /**
275 * Set the given MpegEncContext to defaults for encoding.
276 * the changed fields will not depend upon the prior state of the MpegEncContext.
277 */
278 185 static void mpv_encode_defaults(MpegEncContext *s)
279 {
280 static AVOnce init_static_once = AV_ONCE_INIT;
281
282 185 ff_mpv_common_defaults(s);
283
284 185 ff_thread_once(&init_static_once, mpv_encode_init_static);
285
286 185 s->me.mv_penalty = default_mv_penalty;
287 185 s->fcode_tab = default_fcode_tab;
288
289 185 s->input_picture_number = 0;
290 185 s->picture_in_gop_number = 0;
291 185 }
292
293 258 av_cold int ff_dct_encode_init(MpegEncContext *s)
294 {
295 if (ARCH_X86)
296 258 ff_dct_encode_init_x86(s);
297
298 if (CONFIG_H263_ENCODER)
299 258 ff_h263dsp_init(&s->h263dsp);
300
1/2
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
258 if (!s->dct_quantize)
301 258 s->dct_quantize = ff_dct_quantize_c;
302
1/2
✓ Branch 0 taken 258 times.
✗ Branch 1 not taken.
258 if (!s->denoise_dct)
303 258 s->denoise_dct = denoise_dct_c;
304 258 s->fast_dct_quantize = s->dct_quantize;
305
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 223 times.
258 if (s->avctx->trellis)
306 35 s->dct_quantize = dct_quantize_trellis_c;
307
308 258 return 0;
309 }
310
311 /* init video encoder */
312 185 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
313 {
314 185 MpegEncContext *s = avctx->priv_data;
315 AVCPBProperties *cpb_props;
316 int i, ret;
317
318 185 mpv_encode_defaults(s);
319
320
3/3
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 172 times.
185 switch (avctx->pix_fmt) {
321 4 case AV_PIX_FMT_YUVJ444P:
322 case AV_PIX_FMT_YUV444P:
323 4 s->chroma_format = CHROMA_444;
324 4 break;
325 9 case AV_PIX_FMT_YUVJ422P:
326 case AV_PIX_FMT_YUV422P:
327 9 s->chroma_format = CHROMA_422;
328 9 break;
329 172 case AV_PIX_FMT_YUVJ420P:
330 case AV_PIX_FMT_YUV420P:
331 default:
332 172 s->chroma_format = CHROMA_420;
333 172 break;
334 }
335
336 185 avctx->bits_per_raw_sample = av_clip(avctx->bits_per_raw_sample, 0, 8);
337
338 185 s->bit_rate = avctx->bit_rate;
339 185 s->width = avctx->width;
340 185 s->height = avctx->height;
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (avctx->gop_size > 600 &&
342 avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
343 av_log(avctx, AV_LOG_WARNING,
344 "keyframe interval too large!, reducing it from %d to %d\n",
345 avctx->gop_size, 600);
346 avctx->gop_size = 600;
347 }
348 185 s->gop_size = avctx->gop_size;
349 185 s->avctx = avctx;
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (avctx->max_b_frames > MAX_B_FRAMES) {
351 av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
352 "is %d.\n", MAX_B_FRAMES);
353 avctx->max_b_frames = MAX_B_FRAMES;
354 }
355 185 s->max_b_frames = avctx->max_b_frames;
356 185 s->codec_id = avctx->codec->id;
357 185 s->strict_std_compliance = avctx->strict_std_compliance;
358 185 s->quarter_sample = (avctx->flags & AV_CODEC_FLAG_QPEL) != 0;
359 185 s->rtp_mode = !!s->rtp_payload_size;
360 185 s->intra_dc_precision = avctx->intra_dc_precision;
361
362 // workaround some differences between how applications specify dc precision
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (s->intra_dc_precision < 0) {
364 s->intra_dc_precision += 8;
365
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 184 times.
185 } else if (s->intra_dc_precision >= 8)
366 1 s->intra_dc_precision -= 8;
367
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (s->intra_dc_precision < 0) {
369 av_log(avctx, AV_LOG_ERROR,
370 "intra dc precision must be positive, note some applications use"
371 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
372 return AVERROR(EINVAL);
373 }
374
375
3/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 185 times.
185 if (s->intra_dc_precision > (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ? 3 : 0)) {
376 av_log(avctx, AV_LOG_ERROR, "intra dc precision too large\n");
377 return AVERROR(EINVAL);
378 }
379 185 s->user_specified_pts = AV_NOPTS_VALUE;
380
381
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 184 times.
185 if (s->gop_size <= 1) {
382 1 s->intra_only = 1;
383 1 s->gop_size = 12;
384 } else {
385 184 s->intra_only = 0;
386 }
387
388 /* Fixed QSCALE */
389 185 s->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
390
391 555 s->adaptive_quant = (avctx->lumi_masking ||
392
1/2
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
185 avctx->dark_masking ||
393
1/2
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
185 avctx->temporal_cplx_masking ||
394
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 4 times.
185 avctx->spatial_cplx_masking ||
395
1/2
✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
181 avctx->p_masking ||
396
1/2
✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
181 s->border_masking ||
397
3/4
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 169 times.
382 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
398
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 !s->fixed_qscale;
399
400 185 s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
401
402
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
185 if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
403 switch(avctx->codec_id) {
404 case AV_CODEC_ID_MPEG1VIDEO:
405 case AV_CODEC_ID_MPEG2VIDEO:
406 avctx->rc_buffer_size = FFMAX(avctx->rc_max_rate, 15000000) * 112LL / 15000000 * 16384;
407 break;
408 case AV_CODEC_ID_MPEG4:
409 case AV_CODEC_ID_MSMPEG4V1:
410 case AV_CODEC_ID_MSMPEG4V2:
411 case AV_CODEC_ID_MSMPEG4V3:
412 if (avctx->rc_max_rate >= 15000000) {
413 avctx->rc_buffer_size = 320 + (avctx->rc_max_rate - 15000000LL) * (760-320) / (38400000 - 15000000);
414 } else if(avctx->rc_max_rate >= 2000000) {
415 avctx->rc_buffer_size = 80 + (avctx->rc_max_rate - 2000000LL) * (320- 80) / (15000000 - 2000000);
416 } else if(avctx->rc_max_rate >= 384000) {
417 avctx->rc_buffer_size = 40 + (avctx->rc_max_rate - 384000LL) * ( 80- 40) / ( 2000000 - 384000);
418 } else
419 avctx->rc_buffer_size = 40;
420 avctx->rc_buffer_size *= 16384;
421 break;
422 }
423 if (avctx->rc_buffer_size) {
424 av_log(avctx, AV_LOG_INFO, "Automatically choosing VBV buffer size of %d kbyte\n", avctx->rc_buffer_size/8192);
425 }
426 }
427
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if ((!avctx->rc_max_rate) != (!avctx->rc_buffer_size)) {
429 av_log(avctx, AV_LOG_ERROR, "Either both buffer size and max rate or neither must be specified\n");
430 return AVERROR(EINVAL);
431 }
432
433
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
185 if (avctx->rc_min_rate && avctx->rc_max_rate != avctx->rc_min_rate) {
434 av_log(avctx, AV_LOG_INFO,
435 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
436 }
437
438
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
185 if (avctx->rc_min_rate && avctx->rc_min_rate > avctx->bit_rate) {
439 av_log(avctx, AV_LOG_ERROR, "bitrate below min bitrate\n");
440 return AVERROR(EINVAL);
441 }
442
443
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 183 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
185 if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
444 av_log(avctx, AV_LOG_ERROR, "bitrate above max bitrate\n");
445 return AVERROR(EINVAL);
446 }
447
448
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 183 times.
185 if (avctx->rc_max_rate &&
449
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 avctx->rc_max_rate == avctx->bit_rate &&
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 avctx->rc_max_rate != avctx->rc_min_rate) {
451 av_log(avctx, AV_LOG_INFO,
452 "impossible bitrate constraints, this will fail\n");
453 }
454
455
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 183 times.
185 if (avctx->rc_buffer_size &&
456 2 avctx->bit_rate * (int64_t)avctx->time_base.num >
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 avctx->rc_buffer_size * (int64_t)avctx->time_base.den) {
458 av_log(avctx, AV_LOG_ERROR, "VBV buffer too small for bitrate\n");
459 return AVERROR(EINVAL);
460 }
461
462
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 152 times.
185 if (!s->fixed_qscale &&
463 33 avctx->bit_rate * av_q2d(avctx->time_base) >
464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 avctx->bit_rate_tolerance) {
465 double nbt = avctx->bit_rate * av_q2d(avctx->time_base) * 5;
466 av_log(avctx, AV_LOG_WARNING,
467 "bitrate tolerance %d too small for bitrate %"PRId64", overriding\n", avctx->bit_rate_tolerance, avctx->bit_rate);
468 if (nbt <= INT_MAX) {
469 avctx->bit_rate_tolerance = nbt;
470 } else
471 avctx->bit_rate_tolerance = INT_MAX;
472 }
473
474
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 183 times.
185 if (avctx->rc_max_rate &&
475
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 avctx->rc_min_rate == avctx->rc_max_rate &&
476
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
477
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
478 2 90000LL * (avctx->rc_buffer_size - 1) >
479
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 avctx->rc_max_rate * 0xFFFFLL) {
480 1 av_log(avctx, AV_LOG_INFO,
481 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
482 "specified vbv buffer is too large for the given bitrate!\n");
483 }
484
485
3/4
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 153 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
185 if ((avctx->flags & AV_CODEC_FLAG_4MV) && s->codec_id != AV_CODEC_ID_MPEG4 &&
486 s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
487 s->codec_id != AV_CODEC_ID_FLV1) {
488 av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
489 return AVERROR(EINVAL);
490 }
491
492
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 182 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
185 if (s->obmc && avctx->mb_decision != FF_MB_DECISION_SIMPLE) {
493 av_log(avctx, AV_LOG_ERROR,
494 "OBMC is only supported with simple mb decision\n");
495 return AVERROR(EINVAL);
496 }
497
498
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 181 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
185 if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
499 av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
500 return AVERROR(EINVAL);
501 }
502
503
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 142 times.
185 if (s->max_b_frames &&
504
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 20 times.
43 s->codec_id != AV_CODEC_ID_MPEG4 &&
505
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 4 times.
23 s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
507 av_log(avctx, AV_LOG_ERROR, "B-frames not supported by codec\n");
508 return AVERROR(EINVAL);
509 }
510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (s->max_b_frames < 0) {
511 av_log(avctx, AV_LOG_ERROR,
512 "max b frames must be 0 or positive for mpegvideo based encoders\n");
513 return AVERROR(EINVAL);
514 }
515
516
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 60 times.
185 if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
517
2/2
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 6 times.
125 s->codec_id == AV_CODEC_ID_H263 ||
518
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 116 times.
119 s->codec_id == AV_CODEC_ID_H263P) &&
519
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 (avctx->sample_aspect_ratio.num > 255 ||
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69 times.
69 avctx->sample_aspect_ratio.den > 255)) {
521 av_log(avctx, AV_LOG_WARNING,
522 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
523 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
524 av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,
525 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
526 }
527
528
2/2
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 6 times.
185 if ((s->codec_id == AV_CODEC_ID_H263 ||
529
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 176 times.
179 s->codec_id == AV_CODEC_ID_H263P) &&
530
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 (avctx->width > 2048 ||
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 avctx->height > 1152 )) {
532 av_log(avctx, AV_LOG_ERROR, "H.263 does not support resolutions above 2048x1152\n");
533 return AVERROR(EINVAL);
534 }
535
2/2
✓ Branch 0 taken 179 times.
✓ Branch 1 taken 6 times.
185 if ((s->codec_id == AV_CODEC_ID_H263 ||
536
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 3 times.
179 s->codec_id == AV_CODEC_ID_H263P ||
537
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 173 times.
176 s->codec_id == AV_CODEC_ID_RV20) &&
538
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 ((avctx->width &3) ||
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 (avctx->height&3) )) {
540 av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 4\n");
541 return AVERROR(EINVAL);
542 }
543
544
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 181 times.
185 if (s->codec_id == AV_CODEC_ID_RV10 &&
545
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 (avctx->width &15 ||
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 avctx->height&15 )) {
547 av_log(avctx, AV_LOG_ERROR, "width and height must be a multiple of 16\n");
548 return AVERROR(EINVAL);
549 }
550
551
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 4 times.
185 if ((s->codec_id == AV_CODEC_ID_WMV1 ||
552
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 177 times.
181 s->codec_id == AV_CODEC_ID_WMV2) &&
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 avctx->width & 1) {
554 av_log(avctx, AV_LOG_ERROR, "width must be multiple of 2\n");
555 return AVERROR(EINVAL);
556 }
557
558
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 168 times.
185 if ((avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME)) &&
559
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) {
560 av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
561 return AVERROR(EINVAL);
562 }
563
564
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
185 if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
565 av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
566 return AVERROR(EINVAL);
567 }
568
569
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 173 times.
185 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 avctx->mb_decision != FF_MB_DECISION_RD) {
571 av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
572 return AVERROR(EINVAL);
573 }
574
575
1/2
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
185 if (s->scenechange_threshold < 1000000000 &&
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 (avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)) {
577 av_log(avctx, AV_LOG_ERROR,
578 "closed gop with scene change detection are not supported yet, "
579 "set threshold to 1000000000\n");
580 return AVERROR_PATCHWELCOME;
581 }
582
583
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 184 times.
185 if (avctx->flags & AV_CODEC_FLAG_LOW_DELAY) {
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
585 s->strict_std_compliance >= FF_COMPLIANCE_NORMAL) {
586 av_log(avctx, AV_LOG_ERROR,
587 "low delay forcing is only available for mpeg2, "
588 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
589 return AVERROR(EINVAL);
590 }
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->max_b_frames != 0) {
592 av_log(avctx, AV_LOG_ERROR,
593 "B-frames cannot be used with low delay\n");
594 return AVERROR(EINVAL);
595 }
596 }
597
598
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 184 times.
185 if (s->q_scale_type == 1) {
599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->qmax > 28) {
600 av_log(avctx, AV_LOG_ERROR,
601 "non linear quant only supports qmax <= 28 currently\n");
602 return AVERROR_PATCHWELCOME;
603 }
604 }
605
606
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 173 times.
185 if (avctx->slices > 1 &&
607
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
12 (avctx->codec_id == AV_CODEC_ID_FLV1 || avctx->codec_id == AV_CODEC_ID_H261)) {
608 av_log(avctx, AV_LOG_ERROR, "Multiple slices are not supported by this codec\n");
609 return AVERROR(EINVAL);
610 }
611
612
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
185 if (s->b_frame_strategy && (avctx->flags & AV_CODEC_FLAG_PASS2)) {
613 av_log(avctx, AV_LOG_INFO,
614 "notice: b_frame_strategy only affects the first pass\n");
615 s->b_frame_strategy = 0;
616 }
617
618 185 i = av_gcd(avctx->time_base.den, avctx->time_base.num);
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (i > 1) {
620 av_log(avctx, AV_LOG_INFO, "removing common factors from framerate\n");
621 avctx->time_base.den /= i;
622 avctx->time_base.num /= i;
623 //return -1;
624 }
625
626
10/12
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 11 times.
✓ Branch 4 taken 136 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 110 times.
✓ Branch 7 taken 26 times.
✓ Branch 8 taken 106 times.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 106 times.
185 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) {
627 // (a + x * 3 / 8) / x
628 79 s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
629 79 s->inter_quant_bias = 0;
630 } else {
631 106 s->intra_quant_bias = 0;
632 // (a - x / 4) / x
633 106 s->inter_quant_bias = -(1 << (QUANT_BIAS_SHIFT - 2));
634 }
635
636
2/4
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 185 times.
185 if (avctx->qmin > avctx->qmax || avctx->qmin <= 0) {
637 av_log(avctx, AV_LOG_ERROR, "qmin and or qmax are invalid, they must be 0 < min <= max\n");
638 return AVERROR(EINVAL);
639 }
640
641 185 av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
642
643
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 125 times.
185 if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 avctx->time_base.den > (1 << 16) - 1) {
645 av_log(avctx, AV_LOG_ERROR,
646 "timebase %d/%d not supported by MPEG 4 standard, "
647 "the maximum admitted value for the timebase denominator "
648 "is %d\n", avctx->time_base.num, avctx->time_base.den,
649 (1 << 16) - 1);
650 return AVERROR(EINVAL);
651 }
652 185 s->time_increment_bits = av_log2(avctx->time_base.den - 1) + 1;
653
654
14/16
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 38 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ 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 60 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.
185 switch (avctx->codec->id) {
655 11 case AV_CODEC_ID_MPEG1VIDEO:
656 11 s->out_format = FMT_MPEG1;
657 11 s->low_delay = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
658
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
659 11 break;
660 38 case AV_CODEC_ID_MPEG2VIDEO:
661 38 s->out_format = FMT_MPEG1;
662 38 s->low_delay = !!(avctx->flags & AV_CODEC_FLAG_LOW_DELAY);
663
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1 times.
38 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
664 38 s->rtp_mode = 1;
665 38 break;
666 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
667 30 case AV_CODEC_ID_MJPEG:
668 case AV_CODEC_ID_AMV:
669 30 s->out_format = FMT_MJPEG;
670 30 s->intra_only = 1; /* force intra only for jpeg */
671
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
30 if ((ret = ff_mjpeg_encode_init(s)) < 0)
672 return ret;
673 30 avctx->delay = 0;
674 30 s->low_delay = 1;
675 30 break;
676 #endif
677 case AV_CODEC_ID_SPEEDHQ:
678 s->out_format = FMT_SPEEDHQ;
679 s->intra_only = 1; /* force intra only for SHQ */
680 if (!CONFIG_SPEEDHQ_ENCODER)
681 return AVERROR_ENCODER_NOT_FOUND;
682 if ((ret = ff_speedhq_encode_init(s)) < 0)
683 return ret;
684 avctx->delay = 0;
685 s->low_delay = 1;
686 break;
687 6 case AV_CODEC_ID_H261:
688 if (!CONFIG_H261_ENCODER)
689 return AVERROR_ENCODER_NOT_FOUND;
690
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (ff_h261_get_picture_format(s->width, s->height) < 0) {
691 av_log(avctx, AV_LOG_ERROR,
692 "The specified picture size of %dx%d is not valid for the "
693 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
694 s->width, s->height);
695 return AVERROR(EINVAL);
696 }
697 6 s->out_format = FMT_H261;
698 6 avctx->delay = 0;
699 6 s->low_delay = 1;
700 6 s->rtp_mode = 0; /* Sliced encoding not supported */
701 6 break;
702 6 case AV_CODEC_ID_H263:
703 if (!CONFIG_H263_ENCODER)
704 return AVERROR_ENCODER_NOT_FOUND;
705
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),
706 s->width, s->height) == 8) {
707 av_log(avctx, AV_LOG_ERROR,
708 "The specified picture size of %dx%d is not valid for "
709 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
710 "352x288, 704x576, and 1408x1152. "
711 "Try H.263+.\n", s->width, s->height);
712 return AVERROR(EINVAL);
713 }
714 6 s->out_format = FMT_H263;
715 6 avctx->delay = 0;
716 6 s->low_delay = 1;
717 6 break;
718 3 case AV_CODEC_ID_H263P:
719 3 s->out_format = FMT_H263;
720 3 s->h263_plus = 1;
721 /* Fx */
722 3 s->h263_aic = (avctx->flags & AV_CODEC_FLAG_AC_PRED) ? 1 : 0;
723 3 s->modified_quant = s->h263_aic;
724 3 s->loop_filter = (avctx->flags & AV_CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
725
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;
726
727 /* /Fx */
728 /* These are just to be sure */
729 3 avctx->delay = 0;
730 3 s->low_delay = 1;
731 3 break;
732 7 case AV_CODEC_ID_FLV1:
733 7 s->out_format = FMT_H263;
734 7 s->h263_flv = 2; /* format = 1; 11-bit codes */
735 7 s->unrestricted_mv = 1;
736 7 s->rtp_mode = 0; /* don't allow GOB */
737 7 avctx->delay = 0;
738 7 s->low_delay = 1;
739 7 break;
740 4 case AV_CODEC_ID_RV10:
741 4 s->out_format = FMT_H263;
742 4 avctx->delay = 0;
743 4 s->low_delay = 1;
744 4 break;
745 3 case AV_CODEC_ID_RV20:
746 3 s->out_format = FMT_H263;
747 3 avctx->delay = 0;
748 3 s->low_delay = 1;
749 3 s->modified_quant = 1;
750 3 s->h263_aic = 1;
751 3 s->h263_plus = 1;
752 3 s->loop_filter = 1;
753 3 s->unrestricted_mv = 0;
754 3 break;
755 60 case AV_CODEC_ID_MPEG4:
756 60 s->out_format = FMT_H263;
757 60 s->h263_pred = 1;
758 60 s->unrestricted_mv = 1;
759 60 s->low_delay = s->max_b_frames ? 0 : 1;
760
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 40 times.
60 avctx->delay = s->low_delay ? 0 : (s->max_b_frames + 1);
761 60 break;
762 4 case AV_CODEC_ID_MSMPEG4V2:
763 4 s->out_format = FMT_H263;
764 4 s->h263_pred = 1;
765 4 s->unrestricted_mv = 1;
766 4 s->msmpeg4_version = 2;
767 4 avctx->delay = 0;
768 4 s->low_delay = 1;
769 4 break;
770 5 case AV_CODEC_ID_MSMPEG4V3:
771 5 s->out_format = FMT_H263;
772 5 s->h263_pred = 1;
773 5 s->unrestricted_mv = 1;
774 5 s->msmpeg4_version = 3;
775 5 s->flipflop_rounding = 1;
776 5 avctx->delay = 0;
777 5 s->low_delay = 1;
778 5 break;
779 4 case AV_CODEC_ID_WMV1:
780 4 s->out_format = FMT_H263;
781 4 s->h263_pred = 1;
782 4 s->unrestricted_mv = 1;
783 4 s->msmpeg4_version = 4;
784 4 s->flipflop_rounding = 1;
785 4 avctx->delay = 0;
786 4 s->low_delay = 1;
787 4 break;
788 4 case AV_CODEC_ID_WMV2:
789 4 s->out_format = FMT_H263;
790 4 s->h263_pred = 1;
791 4 s->unrestricted_mv = 1;
792 4 s->msmpeg4_version = 5;
793 4 s->flipflop_rounding = 1;
794 4 avctx->delay = 0;
795 4 s->low_delay = 1;
796 4 break;
797 default:
798 return AVERROR(EINVAL);
799 }
800
801 185 avctx->has_b_frames = !s->low_delay;
802
803 185 s->encoding = 1;
804
805 185 s->progressive_frame =
806
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 17 times.
353 s->progressive_sequence = !(avctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT |
807 AV_CODEC_FLAG_INTERLACED_ME) ||
808
1/2
✓ Branch 0 taken 168 times.
✗ Branch 1 not taken.
168 s->alternate_scan);
809
810 /* init */
811 185 ff_mpv_idct_init(s);
812
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
185 if ((ret = ff_mpv_common_init(s)) < 0)
813 return ret;
814
815 185 ff_fdctdsp_init(&s->fdsp, avctx);
816 185 ff_me_cmp_init(&s->mecc, avctx);
817 185 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
818 185 ff_pixblockdsp_init(&s->pdsp, avctx);
819 185 ff_qpeldsp_init(&s->qdsp);
820
821
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 168 times.
185 if (s->msmpeg4_version) {
822 17 int ac_stats_size = 2 * 2 * (MAX_LEVEL + 1) * (MAX_RUN + 1) * 2 * sizeof(int);
823
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (!(s->ac_stats = av_mallocz(ac_stats_size)))
824 return AVERROR(ENOMEM);
825 }
826
827
1/2
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
185 if (!(avctx->stats_out = av_mallocz(256)) ||
828
1/2
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
185 !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix, 32) ||
829
1/2
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
185 !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix, 32) ||
830
1/2
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
185 !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix, 32) ||
831
1/2
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
185 !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix16, 32) ||
832
1/2
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
185 !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix16, 32) ||
833
1/2
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
185 !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix16, 32) ||
834
1/2
✓ Branch 1 taken 185 times.
✗ Branch 2 not taken.
185 !FF_ALLOCZ_TYPED_ARRAY(s->input_picture, MAX_PICTURE_COUNT) ||
835
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
185 !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MAX_PICTURE_COUNT))
836 return AVERROR(ENOMEM);
837
838
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 181 times.
185 if (s->noise_reduction) {
839
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_offset, 2))
840 return AVERROR(ENOMEM);
841 }
842
843 185 ff_dct_encode_init(s);
844
845
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 179 times.
185 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
846 6 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
847
848
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 173 times.
185 if (s->slice_context_count > 1) {
849 12 s->rtp_mode = 1;
850
851
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (avctx->codec_id == AV_CODEC_ID_H263P)
852 s->h263_slice_structured = 1;
853 }
854
855 185 s->quant_precision = 5;
856
857 185 ff_set_cmp(&s->mecc, s->mecc.ildct_cmp, avctx->ildct_cmp);
858 185 ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->frame_skip_cmp);
859
860
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 179 times.
185 if (CONFIG_H261_ENCODER && s->out_format == FMT_H261) {
861 6 ff_h261_encode_init(s);
862 179 } else if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
863
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 130 times.
179 && s->out_format == FMT_MPEG1) {
864 49 ff_mpeg1_encode_init(s);
865
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 30 times.
130 } else if (CONFIG_H263_ENCODER && s->out_format == FMT_H263) {
866 100 ff_h263_encode_init(s);
867
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 83 times.
100 if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
868 17 ff_msmpeg4_encode_init(s);
869 }
870
871 /* init q matrix */
872
2/2
✓ Branch 0 taken 11840 times.
✓ Branch 1 taken 185 times.
12025 for (i = 0; i < 64; i++) {
873 11840 int j = s->idsp.idct_permutation[i];
874
2/2
✓ Branch 0 taken 3840 times.
✓ Branch 1 taken 8000 times.
11840 if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3840 times.
3840 s->mpeg_quant) {
876 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
877 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
878
4/4
✓ Branch 0 taken 5440 times.
✓ Branch 1 taken 6400 times.
✓ Branch 2 taken 384 times.
✓ Branch 3 taken 5056 times.
11840 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
879 6784 s->intra_matrix[j] =
880 6784 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5056 times.
5056 } else if (CONFIG_SPEEDHQ_ENCODER && s->codec_id == AV_CODEC_ID_SPEEDHQ) {
882 s->intra_matrix[j] =
883 s->inter_matrix[j] = ff_mpeg1_default_intra_matrix[i];
884 } else {
885 /* MPEG-1/2 */
886 5056 s->chroma_intra_matrix[j] =
887 5056 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i];
888 5056 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i];
889 }
890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11840 times.
11840 if (avctx->intra_matrix)
891 s->intra_matrix[j] = avctx->intra_matrix[i];
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11840 times.
11840 if (avctx->inter_matrix)
893 s->inter_matrix[j] = avctx->inter_matrix[i];
894 }
895
896 /* precompute matrix */
897 /* for mjpeg, we do include qscale in the matrix */
898
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 30 times.
185 if (s->out_format != FMT_MJPEG) {
899 155 ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
900 155 s->intra_matrix, s->intra_quant_bias, avctx->qmin,
901 31, 1);
902 155 ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
903 155 s->inter_matrix, s->inter_quant_bias, avctx->qmin,
904 31, 0);
905 }
906
907
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
185 if ((ret = ff_rate_control_init(s)) < 0)
908 return ret;
909
910
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (s->b_frame_strategy == 2) {
911 for (i = 0; i < s->max_b_frames + 2; i++) {
912 s->tmp_frames[i] = av_frame_alloc();
913 if (!s->tmp_frames[i])
914 return AVERROR(ENOMEM);
915
916 s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
917 s->tmp_frames[i]->width = s->width >> s->brd_scale;
918 s->tmp_frames[i]->height = s->height >> s->brd_scale;
919
920 ret = av_frame_get_buffer(s->tmp_frames[i], 0);
921 if (ret < 0)
922 return ret;
923 }
924 }
925
926 185 cpb_props = ff_add_cpb_side_data(avctx);
927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 185 times.
185 if (!cpb_props)
928 return AVERROR(ENOMEM);
929 185 cpb_props->max_bitrate = avctx->rc_max_rate;
930 185 cpb_props->min_bitrate = avctx->rc_min_rate;
931 185 cpb_props->avg_bitrate = avctx->bit_rate;
932 185 cpb_props->buffer_size = avctx->rc_buffer_size;
933
934 185 return 0;
935 }
936
937 185 av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
938 {
939 185 MpegEncContext *s = avctx->priv_data;
940 int i;
941
942 185 ff_rate_control_uninit(s);
943
944 185 ff_mpv_common_end(s);
945
946
2/2
✓ Branch 0 taken 3330 times.
✓ Branch 1 taken 185 times.
3515 for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
947 3330 av_frame_free(&s->tmp_frames[i]);
948
949 185 ff_free_picture_tables(&s->new_picture);
950 185 ff_mpeg_unref_picture(avctx, &s->new_picture);
951
952 185 av_freep(&avctx->stats_out);
953 185 av_freep(&s->ac_stats);
954
955
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 155 times.
185 if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix);
956
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 155 times.
185 if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
957 185 s->q_chroma_intra_matrix= NULL;
958 185 s->q_chroma_intra_matrix16= NULL;
959 185 av_freep(&s->q_intra_matrix);
960 185 av_freep(&s->q_inter_matrix);
961 185 av_freep(&s->q_intra_matrix16);
962 185 av_freep(&s->q_inter_matrix16);
963 185 av_freep(&s->input_picture);
964 185 av_freep(&s->reordered_input_picture);
965 185 av_freep(&s->dct_offset);
966
967 185 return 0;
968 }
969
970 static int get_sae(uint8_t *src, int ref, int stride)
971 {
972 int x,y;
973 int acc = 0;
974
975 for (y = 0; y < 16; y++) {
976 for (x = 0; x < 16; x++) {
977 acc += FFABS(src[x + y * stride] - ref);
978 }
979 }
980
981 return acc;
982 }
983
984 static int get_intra_count(MpegEncContext *s, uint8_t *src,
985 uint8_t *ref, int stride)
986 {
987 int x, y, w, h;
988 int acc = 0;
989
990 w = s->width & ~15;
991 h = s->height & ~15;
992
993 for (y = 0; y < h; y += 16) {
994 for (x = 0; x < w; x += 16) {
995 int offset = x + y * stride;
996 int sad = s->mecc.sad[0](NULL, src + offset, ref + offset,
997 stride, 16);
998 int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
999 int sae = get_sae(src + offset, mean, stride);
1000
1001 acc += sae + 500 < sad;
1002 }
1003 }
1004 return acc;
1005 }
1006
1007 9157 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
1008 {
1009 18314 return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 1,
1010 9157 s->chroma_x_shift, s->chroma_y_shift, s->out_format,
1011 s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
1012 &s->linesize, &s->uvlinesize);
1013 }
1014
1015 8682 static int load_input_picture(MpegEncContext *s, const AVFrame *pic_arg)
1016 {
1017 8682 Picture *pic = NULL;
1018 int64_t pts;
1019 8682 int i, display_picture_number = 0, ret;
1020 17364 int encoding_delay = s->max_b_frames ? s->max_b_frames
1021
2/2
✓ Branch 0 taken 2213 times.
✓ Branch 1 taken 6469 times.
8682 : (s->low_delay ? 0 : 1);
1022 8682 int flush_offset = 1;
1023 8682 int direct = 1;
1024
1025
2/2
✓ Branch 0 taken 8458 times.
✓ Branch 1 taken 224 times.
8682 if (pic_arg) {
1026 8458 pts = pic_arg->pts;
1027 8458 display_picture_number = s->input_picture_number++;
1028
1029
1/2
✓ Branch 0 taken 8458 times.
✗ Branch 1 not taken.
8458 if (pts != AV_NOPTS_VALUE) {
1030
2/2
✓ Branch 0 taken 8273 times.
✓ Branch 1 taken 185 times.
8458 if (s->user_specified_pts != AV_NOPTS_VALUE) {
1031 8273 int64_t last = s->user_specified_pts;
1032
1033
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8273 times.
8273 if (pts <= last) {
1034 av_log(s->avctx, AV_LOG_ERROR,
1035 "Invalid pts (%"PRId64") <= last (%"PRId64")\n",
1036 pts, last);
1037 return AVERROR(EINVAL);
1038 }
1039
1040
4/4
✓ Branch 0 taken 3047 times.
✓ Branch 1 taken 5226 times.
✓ Branch 2 taken 68 times.
✓ Branch 3 taken 2979 times.
8273 if (!s->low_delay && display_picture_number == 1)
1041 68 s->dts_delta = pts - last;
1042 }
1043 8458 s->user_specified_pts = pts;
1044 } else {
1045 if (s->user_specified_pts != AV_NOPTS_VALUE) {
1046 s->user_specified_pts =
1047 pts = s->user_specified_pts + 1;
1048 av_log(s->avctx, AV_LOG_INFO,
1049 "Warning: AVFrame.pts=? trying to guess (%"PRId64")\n",
1050 pts);
1051 } else {
1052 pts = display_picture_number;
1053 }
1054 }
1055
1056
1/2
✓ Branch 0 taken 8458 times.
✗ Branch 1 not taken.
8458 if (!pic_arg->buf[0] ||
1057
2/2
✓ Branch 0 taken 721 times.
✓ Branch 1 taken 7737 times.
8458 pic_arg->linesize[0] != s->linesize ||
1058
1/2
✓ Branch 0 taken 721 times.
✗ Branch 1 not taken.
721 pic_arg->linesize[1] != s->uvlinesize ||
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 721 times.
721 pic_arg->linesize[2] != s->uvlinesize)
1060 7737 direct = 0;
1061
3/4
✓ Branch 0 taken 6833 times.
✓ Branch 1 taken 1625 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6833 times.
8458 if ((s->width & 15) || (s->height & 15))
1062 1625 direct = 0;
1063
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 8308 times.
8458 if (((intptr_t)(pic_arg->data[0])) & (STRIDE_ALIGN-1))
1064 150 direct = 0;
1065
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8458 times.
8458 if (s->linesize & (STRIDE_ALIGN-1))
1066 direct = 0;
1067
1068 ff_dlog(s->avctx, "%d %d %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"\n", pic_arg->linesize[0],
1069 pic_arg->linesize[1], s->linesize, s->uvlinesize);
1070
1071 8458 i = ff_find_unused_picture(s->avctx, s->picture, direct);
1072
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8458 times.
8458 if (i < 0)
1073 return i;
1074
1075 8458 pic = &s->picture[i];
1076 8458 pic->reference = 3;
1077
1078
2/2
✓ Branch 0 taken 649 times.
✓ Branch 1 taken 7809 times.
8458 if (direct) {
1079
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 649 times.
649 if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
1080 return ret;
1081 }
1082 8458 ret = alloc_picture(s, pic, direct);
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8458 times.
8458 if (ret < 0)
1084 return ret;
1085
1086
2/2
✓ Branch 0 taken 7809 times.
✓ Branch 1 taken 649 times.
8458 if (!direct) {
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7809 times.
7809 if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
1088 pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
1089 pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
1090 // empty
1091 } else {
1092 int h_chroma_shift, v_chroma_shift;
1093 7809 av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
1094 &h_chroma_shift,
1095 &v_chroma_shift);
1096
1097
2/2
✓ Branch 0 taken 23427 times.
✓ Branch 1 taken 7809 times.
31236 for (i = 0; i < 3; i++) {
1098 23427 int src_stride = pic_arg->linesize[i];
1099
2/2
✓ Branch 0 taken 15618 times.
✓ Branch 1 taken 7809 times.
23427 int dst_stride = i ? s->uvlinesize : s->linesize;
1100
2/2
✓ Branch 0 taken 15618 times.
✓ Branch 1 taken 7809 times.
23427 int h_shift = i ? h_chroma_shift : 0;
1101
2/2
✓ Branch 0 taken 15618 times.
✓ Branch 1 taken 7809 times.
23427 int v_shift = i ? v_chroma_shift : 0;
1102 23427 int w = s->width >> h_shift;
1103 23427 int h = s->height >> v_shift;
1104 23427 uint8_t *src = pic_arg->data[i];
1105 23427 uint8_t *dst = pic->f->data[i];
1106 23427 int vpad = 16;
1107
1108
2/2
✓ Branch 0 taken 4605 times.
✓ Branch 1 taken 18822 times.
23427 if ( s->codec_id == AV_CODEC_ID_MPEG2VIDEO
1109
2/2
✓ Branch 0 taken 2475 times.
✓ Branch 1 taken 2130 times.
4605 && !s->progressive_sequence
1110
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 1875 times.
2475 && FFALIGN(s->height, 32) - s->height > 16)
1111 600 vpad = 32;
1112
1113
2/2
✓ Branch 0 taken 23277 times.
✓ Branch 1 taken 150 times.
23427 if (!s->avctx->rc_buffer_size)
1114 23277 dst += INPLACE_OFFSET;
1115
1116
2/2
✓ Branch 0 taken 2389 times.
✓ Branch 1 taken 21038 times.
23427 if (src_stride == dst_stride)
1117 2389 memcpy(dst, src, src_stride * h);
1118 else {
1119 21038 int h2 = h;
1120 21038 uint8_t *dst2 = dst;
1121
2/2
✓ Branch 0 taken 3436644 times.
✓ Branch 1 taken 21038 times.
3457682 while (h2--) {
1122 3436644 memcpy(dst2, src, w);
1123 3436644 dst2 += dst_stride;
1124 3436644 src += src_stride;
1125 }
1126 }
1127
3/4
✓ Branch 0 taken 18552 times.
✓ Branch 1 taken 4875 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18552 times.
23427 if ((s->width & 15) || (s->height & (vpad-1))) {
1128 4875 s->mpvencdsp.draw_edges(dst, dst_stride,
1129 w, h,
1130 16 >> h_shift,
1131 vpad >> v_shift,
1132 EDGE_BOTTOM);
1133 }
1134 }
1135 7809 emms_c();
1136 }
1137 }
1138 8458 ret = av_frame_copy_props(pic->f, pic_arg);
1139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8458 times.
8458 if (ret < 0)
1140 return ret;
1141
1142 8458 pic->f->display_picture_number = display_picture_number;
1143 8458 pic->f->pts = pts; // we set this here to avoid modifying pic_arg
1144 } else {
1145 /* Flushing: When we have not received enough input frames,
1146 * ensure s->input_picture[0] contains the first picture */
1147
1/2
✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
224 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1148
1/2
✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
224 if (s->input_picture[flush_offset])
1149 224 break;
1150
1151
1/2
✓ Branch 0 taken 224 times.
✗ Branch 1 not taken.
224 if (flush_offset <= 1)
1152 224 flush_offset = 1;
1153 else
1154 encoding_delay = encoding_delay - flush_offset + 1;
1155 }
1156
1157 /* shift buffer entries */
1158
2/2
✓ Branch 0 taken 303870 times.
✓ Branch 1 taken 8682 times.
312552 for (i = flush_offset; i < MAX_PICTURE_COUNT /*s->encoding_delay + 1*/; i++)
1159 303870 s->input_picture[i - flush_offset] = s->input_picture[i];
1160
1161 8682 s->input_picture[encoding_delay] = (Picture*) pic;
1162
1163 8682 return 0;
1164 }
1165
1166 static int skip_check(MpegEncContext *s, Picture *p, Picture *ref)
1167 {
1168 int x, y, plane;
1169 int score = 0;
1170 int64_t score64 = 0;
1171
1172 for (plane = 0; plane < 3; plane++) {
1173 const int stride = p->f->linesize[plane];
1174 const int bw = plane ? 1 : 2;
1175 for (y = 0; y < s->mb_height * bw; y++) {
1176 for (x = 0; x < s->mb_width * bw; x++) {
1177 int off = p->shared ? 0 : 16;
1178 uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
1179 uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
1180 int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1181
1182 switch (FFABS(s->frame_skip_exp)) {
1183 case 0: score = FFMAX(score, v); break;
1184 case 1: score += FFABS(v); break;
1185 case 2: score64 += v * (int64_t)v; break;
1186 case 3: score64 += FFABS(v * (int64_t)v * v); break;
1187 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v); break;
1188 }
1189 }
1190 }
1191 }
1192 emms_c();
1193
1194 if (score)
1195 score64 = score;
1196 if (s->frame_skip_exp < 0)
1197 score64 = pow(score64 / (double)(s->mb_width * s->mb_height),
1198 -1.0/s->frame_skip_exp);
1199
1200 if (score64 < s->frame_skip_threshold)
1201 return 1;
1202 if (score64 < ((s->frame_skip_factor * (int64_t) s->lambda) >> 8))
1203 return 1;
1204 return 0;
1205 }
1206
1207 static int encode_frame(AVCodecContext *c, AVFrame *frame, AVPacket *pkt)
1208 {
1209 int ret;
1210 int size = 0;
1211
1212 ret = avcodec_send_frame(c, frame);
1213 if (ret < 0)
1214 return ret;
1215
1216 do {
1217 ret = avcodec_receive_packet(c, pkt);
1218 if (ret >= 0) {
1219 size += pkt->size;
1220 av_packet_unref(pkt);
1221 } else if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1222 return ret;
1223 } while (ret >= 0);
1224
1225 return size;
1226 }
1227
1228 static int estimate_best_b_count(MpegEncContext *s)
1229 {
1230 const AVCodec *codec = avcodec_find_encoder(s->avctx->codec_id);
1231 AVPacket *pkt;
1232 const int scale = s->brd_scale;
1233 int width = s->width >> scale;
1234 int height = s->height >> scale;
1235 int i, j, out_size, p_lambda, b_lambda, lambda2;
1236 int64_t best_rd = INT64_MAX;
1237 int best_b_count = -1;
1238 int ret = 0;
1239
1240 av_assert0(scale >= 0 && scale <= 3);
1241
1242 pkt = av_packet_alloc();
1243 if (!pkt)
1244 return AVERROR(ENOMEM);
1245
1246 //emms_c();
1247 //s->next_picture_ptr->quality;
1248 p_lambda = s->last_lambda_for[AV_PICTURE_TYPE_P];
1249 //p_lambda * FFABS(s->avctx->b_quant_factor) + s->avctx->b_quant_offset;
1250 b_lambda = s->last_lambda_for[AV_PICTURE_TYPE_B];
1251 if (!b_lambda) // FIXME we should do this somewhere else
1252 b_lambda = p_lambda;
1253 lambda2 = (b_lambda * b_lambda + (1 << FF_LAMBDA_SHIFT) / 2) >>
1254 FF_LAMBDA_SHIFT;
1255
1256 for (i = 0; i < s->max_b_frames + 2; i++) {
1257 Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1258 s->next_picture_ptr;
1259 uint8_t *data[4];
1260
1261 if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1262 pre_input = *pre_input_ptr;
1263 memcpy(data, pre_input_ptr->f->data, sizeof(data));
1264
1265 if (!pre_input.shared && i) {
1266 data[0] += INPLACE_OFFSET;
1267 data[1] += INPLACE_OFFSET;
1268 data[2] += INPLACE_OFFSET;
1269 }
1270
1271 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
1272 s->tmp_frames[i]->linesize[0],
1273 data[0],
1274 pre_input.f->linesize[0],
1275 width, height);
1276 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
1277 s->tmp_frames[i]->linesize[1],
1278 data[1],
1279 pre_input.f->linesize[1],
1280 width >> 1, height >> 1);
1281 s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
1282 s->tmp_frames[i]->linesize[2],
1283 data[2],
1284 pre_input.f->linesize[2],
1285 width >> 1, height >> 1);
1286 }
1287 }
1288
1289 for (j = 0; j < s->max_b_frames + 1; j++) {
1290 AVCodecContext *c;
1291 int64_t rd = 0;
1292
1293 if (!s->input_picture[j])
1294 break;
1295
1296 c = avcodec_alloc_context3(NULL);
1297 if (!c) {
1298 ret = AVERROR(ENOMEM);
1299 goto fail;
1300 }
1301
1302 c->width = width;
1303 c->height = height;
1304 c->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_PSNR;
1305 c->flags |= s->avctx->flags & AV_CODEC_FLAG_QPEL;
1306 c->mb_decision = s->avctx->mb_decision;
1307 c->me_cmp = s->avctx->me_cmp;
1308 c->mb_cmp = s->avctx->mb_cmp;
1309 c->me_sub_cmp = s->avctx->me_sub_cmp;
1310 c->pix_fmt = AV_PIX_FMT_YUV420P;
1311 c->time_base = s->avctx->time_base;
1312 c->max_b_frames = s->max_b_frames;
1313
1314 ret = avcodec_open2(c, codec, NULL);
1315 if (ret < 0)
1316 goto fail;
1317
1318
1319 s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
1320 s->tmp_frames[0]->quality = 1 * FF_QP2LAMBDA;
1321
1322 out_size = encode_frame(c, s->tmp_frames[0], pkt);
1323 if (out_size < 0) {
1324 ret = out_size;
1325 goto fail;
1326 }
1327
1328 //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1329
1330 for (i = 0; i < s->max_b_frames + 1; i++) {
1331 int is_p = i % (j + 1) == j || i == s->max_b_frames;
1332
1333 s->tmp_frames[i + 1]->pict_type = is_p ?
1334 AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1335 s->tmp_frames[i + 1]->quality = is_p ? p_lambda : b_lambda;
1336
1337 out_size = encode_frame(c, s->tmp_frames[i + 1], pkt);
1338 if (out_size < 0) {
1339 ret = out_size;
1340 goto fail;
1341 }
1342
1343 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1344 }
1345
1346 /* get the delayed frames */
1347 out_size = encode_frame(c, NULL, pkt);
1348 if (out_size < 0) {
1349 ret = out_size;
1350 goto fail;
1351 }
1352 rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1353
1354 rd += c->error[0] + c->error[1] + c->error[2];
1355
1356 if (rd < best_rd) {
1357 best_rd = rd;
1358 best_b_count = j;
1359 }
1360
1361 fail:
1362 avcodec_free_context(&c);
1363 av_packet_unref(pkt);
1364 if (ret < 0) {
1365 best_b_count = ret;
1366 break;
1367 }
1368 }
1369
1370 av_packet_free(&pkt);
1371
1372 return best_b_count;
1373 }
1374
1375 8682 static int select_input_picture(MpegEncContext *s)
1376 {
1377 int i, ret;
1378
1379
2/2
✓ Branch 0 taken 303870 times.
✓ Branch 1 taken 8682 times.
312552 for (i = 1; i < MAX_PICTURE_COUNT; i++)
1380 303870 s->reordered_input_picture[i - 1] = s->reordered_input_picture[i];
1381 8682 s->reordered_input_picture[MAX_PICTURE_COUNT - 1] = NULL;
1382
1383 /* set next picture type & ordering */
1384
4/4
✓ Branch 0 taken 1347 times.
✓ Branch 1 taken 7335 times.
✓ Branch 2 taken 224 times.
✓ Branch 3 taken 7111 times.
8682 if (!s->reordered_input_picture[0] && s->input_picture[0]) {
1385
2/4
✓ Branch 0 taken 7111 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7111 times.
7111 if (s->frame_skip_threshold || s->frame_skip_factor) {
1386 if (s->picture_in_gop_number < s->gop_size &&
1387 s->next_picture_ptr &&
1388 skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1389 // FIXME check that the gop check above is +-1 correct
1390 av_frame_unref(s->input_picture[0]->f);
1391
1392 ff_vbv_update(s, 0);
1393
1394 goto no_output_pic;
1395 }
1396 }
1397
1398 7111 if (/*s->picture_in_gop_number >= s->gop_size ||*/
1399
4/4
✓ Branch 0 taken 6926 times.
✓ Branch 1 taken 185 times.
✓ Branch 2 taken 1432 times.
✓ Branch 3 taken 5494 times.
7111 !s->next_picture_ptr || s->intra_only) {
1400 1617 s->reordered_input_picture[0] = s->input_picture[0];
1401 1617 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
1402 1617 s->reordered_input_picture[0]->f->coded_picture_number =
1403 1617 s->coded_picture_number++;
1404 } else {
1405 5494 int b_frames = 0;
1406
1407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5494 times.
5494 if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
1408 for (i = 0; i < s->max_b_frames + 1; i++) {
1409 int pict_num = s->input_picture[0]->f->display_picture_number + i;
1410
1411 if (pict_num >= s->rc_context.num_entries)
1412 break;
1413 if (!s->input_picture[i]) {
1414 s->rc_context.entry[pict_num - 1].new_pict_type = AV_PICTURE_TYPE_P;
1415 break;
1416 }
1417
1418 s->input_picture[i]->f->pict_type =
1419 s->rc_context.entry[pict_num].new_pict_type;
1420 }
1421 }
1422
1423
1/2
✓ Branch 0 taken 5494 times.
✗ Branch 1 not taken.
5494 if (s->b_frame_strategy == 0) {
1424 5494 b_frames = s->max_b_frames;
1425
4/4
✓ Branch 0 taken 735 times.
✓ Branch 1 taken 4844 times.
✓ Branch 2 taken 85 times.
✓ Branch 3 taken 650 times.
5579 while (b_frames && !s->input_picture[b_frames])
1426 85 b_frames--;
1427 } else if (s->b_frame_strategy == 1) {
1428 for (i = 1; i < s->max_b_frames + 1; i++) {
1429 if (s->input_picture[i] &&
1430 s->input_picture[i]->b_frame_score == 0) {
1431 s->input_picture[i]->b_frame_score =
1432 get_intra_count(s,
1433 s->input_picture[i ]->f->data[0],
1434 s->input_picture[i - 1]->f->data[0],
1435 s->linesize) + 1;
1436 }
1437 }
1438 for (i = 0; i < s->max_b_frames + 1; i++) {
1439 if (!s->input_picture[i] ||
1440 s->input_picture[i]->b_frame_score - 1 >
1441 s->mb_num / s->b_sensitivity)
1442 break;
1443 }
1444
1445 b_frames = FFMAX(0, i - 1);
1446
1447 /* reset scores */
1448 for (i = 0; i < b_frames + 1; i++) {
1449 s->input_picture[i]->b_frame_score = 0;
1450 }
1451 } else if (s->b_frame_strategy == 2) {
1452 b_frames = estimate_best_b_count(s);
1453 if (b_frames < 0)
1454 return b_frames;
1455 }
1456
1457 5494 emms_c();
1458
1459
2/2
✓ Branch 0 taken 1347 times.
✓ Branch 1 taken 5494 times.
6841 for (i = b_frames - 1; i >= 0; i--) {
1460 1347 int type = s->input_picture[i]->f->pict_type;
1461
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)
1462 b_frames = i;
1463 }
1464
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5494 times.
5494 if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1465 b_frames == s->max_b_frames) {
1466 av_log(s->avctx, AV_LOG_ERROR,
1467 "warning, too many B-frames in a row\n");
1468 }
1469
1470
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 4947 times.
5494 if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1472 s->gop_size > s->picture_in_gop_number) {
1473 b_frames = s->gop_size - s->picture_in_gop_number - 1;
1474 } else {
1475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)
1476 b_frames = 0;
1477 547 s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1478 }
1479 }
1480
1481
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5494 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5494 if ((s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP) && b_frames &&
1482 s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1483 b_frames--;
1484
1485 5494 s->reordered_input_picture[0] = s->input_picture[b_frames];
1486
2/2
✓ Branch 0 taken 4945 times.
✓ Branch 1 taken 549 times.
5494 if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
1487 4945 s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
1488 5494 s->reordered_input_picture[0]->f->coded_picture_number =
1489 5494 s->coded_picture_number++;
1490
2/2
✓ Branch 0 taken 1347 times.
✓ Branch 1 taken 5494 times.
6841 for (i = 0; i < b_frames; i++) {
1491 1347 s->reordered_input_picture[i + 1] = s->input_picture[i];
1492 1347 s->reordered_input_picture[i + 1]->f->pict_type =
1493 AV_PICTURE_TYPE_B;
1494 1347 s->reordered_input_picture[i + 1]->f->coded_picture_number =
1495 1347 s->coded_picture_number++;
1496 }
1497 }
1498 }
1499 7065 no_output_pic:
1500 8682 ff_mpeg_unref_picture(s->avctx, &s->new_picture);
1501
1502
2/2
✓ Branch 0 taken 8458 times.
✓ Branch 1 taken 224 times.
8682 if (s->reordered_input_picture[0]) {
1503 8458 s->reordered_input_picture[0]->reference =
1504 8458 s->reordered_input_picture[0]->f->pict_type !=
1505
2/2
✓ Branch 0 taken 7111 times.
✓ Branch 1 taken 1347 times.
8458 AV_PICTURE_TYPE_B ? 3 : 0;
1506
1507
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8458 times.
8458 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->new_picture, s->reordered_input_picture[0])))
1508 return ret;
1509
1510
4/4
✓ Branch 0 taken 7809 times.
✓ Branch 1 taken 649 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 7759 times.
8458 if (s->reordered_input_picture[0]->shared || s->avctx->rc_buffer_size) {
1511 // input is a shared pix, so we can't modify it -> allocate a new
1512 // one & ensure that the shared one is reuseable
1513
1514 Picture *pic;
1515 699 int i = ff_find_unused_picture(s->avctx, s->picture, 0);
1516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 699 times.
699 if (i < 0)
1517 return i;
1518 699 pic = &s->picture[i];
1519
1520 699 pic->reference = s->reordered_input_picture[0]->reference;
1521
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 699 times.
699 if (alloc_picture(s, pic, 0) < 0) {
1522 return -1;
1523 }
1524
1525 699 ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 699 times.
699 if (ret < 0)
1527 return ret;
1528
1529 /* mark us unused / free shared pic */
1530 699 av_frame_unref(s->reordered_input_picture[0]->f);
1531 699 s->reordered_input_picture[0]->shared = 0;
1532
1533 699 s->current_picture_ptr = pic;
1534 } else {
1535 // input is not a shared pix -> reuse buffer for current_pix
1536 7759 s->current_picture_ptr = s->reordered_input_picture[0];
1537
2/2
✓ Branch 0 taken 31036 times.
✓ Branch 1 taken 7759 times.
38795 for (i = 0; i < 4; i++) {
1538
2/2
✓ Branch 0 taken 23277 times.
✓ Branch 1 taken 7759 times.
31036 if (s->new_picture.f->data[i])
1539 23277 s->new_picture.f->data[i] += INPLACE_OFFSET;
1540 }
1541 }
1542 8458 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1543
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8458 times.
8458 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1544 s->current_picture_ptr)) < 0)
1545 return ret;
1546
1547 8458 s->picture_number = s->new_picture.f->display_picture_number;
1548 }
1549 8682 return 0;
1550 }
1551
1552 8583 static void frame_end(MpegEncContext *s)
1553 {
1554
2/2
✓ Branch 0 taken 3955 times.
✓ Branch 1 taken 4628 times.
8583 if (s->unrestricted_mv &&
1555
2/2
✓ Branch 0 taken 3315 times.
✓ Branch 1 taken 640 times.
3955 s->current_picture.reference &&
1556
1/2
✓ Branch 0 taken 3315 times.
✗ Branch 1 not taken.
3315 !s->intra_only) {
1557 3315 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1558 3315 int hshift = desc->log2_chroma_w;
1559 3315 int vshift = desc->log2_chroma_h;
1560 3315 s->mpvencdsp.draw_edges(s->current_picture.f->data[0],
1561 3315 s->current_picture.f->linesize[0],
1562 s->h_edge_pos, s->v_edge_pos,
1563 EDGE_WIDTH, EDGE_WIDTH,
1564 EDGE_TOP | EDGE_BOTTOM);
1565 3315 s->mpvencdsp.draw_edges(s->current_picture.f->data[1],
1566 3315 s->current_picture.f->linesize[1],
1567 3315 s->h_edge_pos >> hshift,
1568 3315 s->v_edge_pos >> vshift,
1569 EDGE_WIDTH >> hshift,
1570 EDGE_WIDTH >> vshift,
1571 EDGE_TOP | EDGE_BOTTOM);
1572 3315 s->mpvencdsp.draw_edges(s->current_picture.f->data[2],
1573 3315 s->current_picture.f->linesize[2],
1574 3315 s->h_edge_pos >> hshift,
1575 3315 s->v_edge_pos >> vshift,
1576 EDGE_WIDTH >> hshift,
1577 EDGE_WIDTH >> vshift,
1578 EDGE_TOP | EDGE_BOTTOM);
1579 }
1580
1581 8583 emms_c();
1582
1583 8583 s->last_pict_type = s->pict_type;
1584 8583 s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1585
2/2
✓ Branch 0 taken 7236 times.
✓ Branch 1 taken 1347 times.
8583 if (s->pict_type!= AV_PICTURE_TYPE_B)
1586 7236 s->last_non_b_pict_type = s->pict_type;
1587 8583 }
1588
1589 200 static void update_noise_reduction(MpegEncContext *s)
1590 {
1591 int intra, i;
1592
1593
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 200 times.
600 for (intra = 0; intra < 2; intra++) {
1594
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 392 times.
400 if (s->dct_count[intra] > (1 << 16)) {
1595
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 8 times.
520 for (i = 0; i < 64; i++) {
1596 512 s->dct_error_sum[intra][i] >>= 1;
1597 }
1598 8 s->dct_count[intra] >>= 1;
1599 }
1600
1601
2/2
✓ Branch 0 taken 25600 times.
✓ Branch 1 taken 400 times.
26000 for (i = 0; i < 64; i++) {
1602 25600 s->dct_offset[intra][i] = (s->noise_reduction *
1603 25600 s->dct_count[intra] +
1604 25600 s->dct_error_sum[intra][i] / 2) /
1605 25600 (s->dct_error_sum[intra][i] + 1);
1606 }
1607 }
1608 200 }
1609
1610 8458 static int frame_start(MpegEncContext *s)
1611 {
1612 int ret;
1613
1614 /* mark & release old frames */
1615
4/4
✓ Branch 0 taken 7111 times.
✓ Branch 1 taken 1347 times.
✓ Branch 2 taken 6741 times.
✓ Branch 3 taken 370 times.
8458 if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1616
1/2
✓ Branch 0 taken 6741 times.
✗ Branch 1 not taken.
6741 s->last_picture_ptr != s->next_picture_ptr &&
1617
1/2
✓ Branch 0 taken 6741 times.
✗ Branch 1 not taken.
6741 s->last_picture_ptr->f->buf[0]) {
1618 6741 ff_mpeg_unref_picture(s->avctx, s->last_picture_ptr);
1619 }
1620
1621 8458 s->current_picture_ptr->f->pict_type = s->pict_type;
1622 8458 s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1623
1624 8458 ff_mpeg_unref_picture(s->avctx, &s->current_picture);
1625
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8458 times.
8458 if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1626 s->current_picture_ptr)) < 0)
1627 return ret;
1628
1629
2/2
✓ Branch 0 taken 7111 times.
✓ Branch 1 taken 1347 times.
8458 if (s->pict_type != AV_PICTURE_TYPE_B) {
1630 7111 s->last_picture_ptr = s->next_picture_ptr;
1631 7111 s->next_picture_ptr = s->current_picture_ptr;
1632 }
1633
1634
2/2
✓ Branch 0 taken 8273 times.
✓ Branch 1 taken 185 times.
8458 if (s->last_picture_ptr) {
1635 8273 ff_mpeg_unref_picture(s->avctx, &s->last_picture);
1636
2/4
✓ Branch 0 taken 8273 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8273 times.
16546 if (s->last_picture_ptr->f->buf[0] &&
1637 8273 (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1638 s->last_picture_ptr)) < 0)
1639 return ret;
1640 }
1641
1/2
✓ Branch 0 taken 8458 times.
✗ Branch 1 not taken.
8458 if (s->next_picture_ptr) {
1642 8458 ff_mpeg_unref_picture(s->avctx, &s->next_picture);
1643
2/4
✓ Branch 0 taken 8458 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8458 times.
16916 if (s->next_picture_ptr->f->buf[0] &&
1644 8458 (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1645 s->next_picture_ptr)) < 0)
1646 return ret;
1647 }
1648
1649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8458 times.
8458 if (s->picture_structure!= PICT_FRAME) {
1650 int i;
1651 for (i = 0; i < 4; i++) {
1652 if (s->picture_structure == PICT_BOTTOM_FIELD) {
1653 s->current_picture.f->data[i] +=
1654 s->current_picture.f->linesize[i];
1655 }
1656 s->current_picture.f->linesize[i] *= 2;
1657 s->last_picture.f->linesize[i] *= 2;
1658 s->next_picture.f->linesize[i] *= 2;
1659 }
1660 }
1661
1662
3/4
✓ Branch 0 taken 6798 times.
✓ Branch 1 taken 1660 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6798 times.
8458 if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1663 1660 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1664 1660 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1665
4/4
✓ Branch 0 taken 2218 times.
✓ Branch 1 taken 4580 times.
✓ Branch 2 taken 300 times.
✓ Branch 3 taken 1918 times.
6798 } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1666 4880 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1667 4880 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1668 } else {
1669 1918 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1670 1918 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1671 }
1672
1673
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 8258 times.
8458 if (s->dct_error_sum) {
1674 av_assert2(s->noise_reduction && s->encoding);
1675 200 update_noise_reduction(s);
1676 }
1677
1678 8458 return 0;
1679 }
1680
1681 8682 int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1682 const AVFrame *pic_arg, int *got_packet)
1683 {
1684 8682 MpegEncContext *s = avctx->priv_data;
1685 int i, stuffing_count, ret;
1686 8682 int context_count = s->slice_context_count;
1687
1688 8682 s->vbv_ignore_qmax = 0;
1689
1690 8682 s->picture_in_gop_number++;
1691
1692
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8682 times.
8682 if (load_input_picture(s, pic_arg) < 0)
1693 return -1;
1694
1695
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8682 times.
8682 if (select_input_picture(s) < 0) {
1696 return -1;
1697 }
1698
1699 /* output? */
1700
2/2
✓ Branch 0 taken 8458 times.
✓ Branch 1 taken 224 times.
8682 if (s->new_picture.f->data[0]) {
1701
5/6
✓ Branch 0 taken 7858 times.
✓ Branch 1 taken 600 times.
✓ Branch 2 taken 7858 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7458 times.
✓ Branch 5 taken 400 times.
8458 int growing_buffer = context_count == 1 && !pkt->data && !s->data_partitioning;
1702 15916 int pkt_size = growing_buffer ? FFMAX(s->mb_width*s->mb_height*64+10000, avctx->internal->byte_buffer_size) - AV_INPUT_BUFFER_PADDING_SIZE
1703
2/2
✓ Branch 0 taken 7458 times.
✓ Branch 1 taken 1000 times.
8458 :
1704 1000 s->mb_width*s->mb_height*(MAX_MB_BYTES+100)+10000;
1705
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8458 times.
8458 if ((ret = ff_alloc_packet(avctx, pkt, pkt_size)) < 0)
1706 return ret;
1707
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8458 times.
8458 if (s->mb_info) {
1708 s->mb_info_ptr = av_packet_new_side_data(pkt,
1709 AV_PKT_DATA_H263_MB_INFO,
1710 s->mb_width*s->mb_height*12);
1711 s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
1712 }
1713
1714
2/2
✓ Branch 0 taken 9058 times.
✓ Branch 1 taken 8458 times.
17516 for (i = 0; i < context_count; i++) {
1715 9058 int start_y = s->thread_context[i]->start_mb_y;
1716 9058 int end_y = s->thread_context[i]-> end_mb_y;
1717 9058 int h = s->mb_height;
1718 9058 uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
1719 9058 uint8_t *end = pkt->data + (size_t)(((int64_t) pkt->size) * end_y / h);
1720
1721 9058 init_put_bits(&s->thread_context[i]->pb, start, end - start);
1722 }
1723
1724 8458 s->pict_type = s->new_picture.f->pict_type;
1725 //emms_c();
1726 8458 ret = frame_start(s);
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8458 times.
8458 if (ret < 0)
1728 return ret;
1729 8458 vbv_retry:
1730 8583 ret = encode_picture(s, s->picture_number);
1731
2/2
✓ Branch 0 taken 7583 times.
✓ Branch 1 taken 1000 times.
8583 if (growing_buffer) {
1732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7583 times.
7583 av_assert0(s->pb.buf == avctx->internal->byte_buffer);
1733 7583 pkt->data = s->pb.buf;
1734 7583 pkt->size = avctx->internal->byte_buffer_size;
1735 }
1736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8583 times.
8583 if (ret < 0)
1737 return -1;
1738
1739 8583 frame_end(s);
1740
1741
2/2
✓ Branch 0 taken 1438 times.
✓ Branch 1 taken 7145 times.
8583 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) && s->out_format == FMT_MJPEG)
1742 1438 ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1743
1744
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 8408 times.
8583 if (avctx->rc_buffer_size) {
1745 175 RateControlContext *rcc = &s->rc_context;
1746
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);
1747
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);
1748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
1749
1750
2/2
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 50 times.
175 if (put_bits_count(&s->pb) > max_size &&
1751
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 s->lambda < s->lmax) {
1752 125 s->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
1753 (s->qscale + 1) / s->qscale);
1754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (s->adaptive_quant) {
1755 int i;
1756 for (i = 0; i < s->mb_height * s->mb_stride; i++)
1757 s->lambda_table[i] =
1758 FFMAX(s->lambda_table[i] + min_step,
1759 s->lambda_table[i] * (s->qscale + 1) /
1760 s->qscale);
1761 }
1762 125 s->mb_skipped = 0; // done in frame_start()
1763 // done in encode_picture() so we must undo it
1764
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if (s->pict_type == AV_PICTURE_TYPE_P) {
1765 if (s->flipflop_rounding ||
1766 s->codec_id == AV_CODEC_ID_H263P ||
1767 s->codec_id == AV_CODEC_ID_MPEG4)
1768 s->no_rounding ^= 1;
1769 }
1770
1/2
✓ Branch 0 taken 125 times.
✗ Branch 1 not taken.
125 if (s->pict_type != AV_PICTURE_TYPE_B) {
1771 125 s->time_base = s->last_time_base;
1772 125 s->last_non_b_time = s->time - s->pp_time;
1773 }
1774
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 125 times.
250 for (i = 0; i < context_count; i++) {
1775 125 PutBitContext *pb = &s->thread_context[i]->pb;
1776 125 init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1777 }
1778 125 s->vbv_ignore_qmax = 1;
1779 125 av_log(avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
1780 125 goto vbv_retry;
1781 }
1782
1783
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 av_assert0(avctx->rc_max_rate);
1784 }
1785
1786
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8458 times.
8458 if (avctx->flags & AV_CODEC_FLAG_PASS1)
1787 ff_write_pass1_stats(s);
1788
1789
2/2
✓ Branch 0 taken 33832 times.
✓ Branch 1 taken 8458 times.
42290 for (i = 0; i < 4; i++) {
1790 33832 s->current_picture_ptr->encoding_error[i] = s->current_picture.encoding_error[i];
1791 33832 avctx->error[i] += s->current_picture_ptr->encoding_error[i];
1792 }
1793 8458 ff_side_data_set_encoder_stats(pkt, s->current_picture.f->quality,
1794 8458 s->current_picture_ptr->encoding_error,
1795 8458 (avctx->flags&AV_CODEC_FLAG_PSNR) ? MPEGVIDEO_MAX_PLANES : 0,
1796 s->pict_type);
1797
1798 8458 if (avctx->flags & AV_CODEC_FLAG_PASS1)
1799 assert(put_bits_count(&s->pb) == s->header_bits + s->mv_bits +
1800 s->misc_bits + s->i_tex_bits +
1801 s->p_tex_bits);
1802 8458 flush_put_bits(&s->pb);
1803 8458 s->frame_bits = put_bits_count(&s->pb);
1804
1805 8458 stuffing_count = ff_vbv_update(s, s->frame_bits);
1806 8458 s->stuffing_bits = 8*stuffing_count;
1807
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 8408 times.
8458 if (stuffing_count) {
1808
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 if (put_bytes_left(&s->pb, 0) < stuffing_count + 50) {
1809 av_log(avctx, AV_LOG_ERROR, "stuffing too large\n");
1810 return -1;
1811 }
1812
1813
1/3
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
50 switch (s->codec_id) {
1814 50 case AV_CODEC_ID_MPEG1VIDEO:
1815 case AV_CODEC_ID_MPEG2VIDEO:
1816
2/2
✓ Branch 0 taken 3648955 times.
✓ Branch 1 taken 50 times.
3649005 while (stuffing_count--) {
1817 3648955 put_bits(&s->pb, 8, 0);
1818 }
1819 50 break;
1820 case AV_CODEC_ID_MPEG4:
1821 put_bits(&s->pb, 16, 0);
1822 put_bits(&s->pb, 16, 0x1C3);
1823 stuffing_count -= 4;
1824 while (stuffing_count--) {
1825 put_bits(&s->pb, 8, 0xFF);
1826 }
1827 break;
1828 default:
1829 av_log(avctx, AV_LOG_ERROR, "vbv buffer overflow\n");
1830 s->stuffing_bits = 0;
1831 }
1832 50 flush_put_bits(&s->pb);
1833 50 s->frame_bits = put_bits_count(&s->pb);
1834 }
1835
1836 /* update MPEG-1/2 vbv_delay for CBR */
1837
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 8408 times.
8458 if (avctx->rc_max_rate &&
1838
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 avctx->rc_min_rate == avctx->rc_max_rate &&
1839
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 s->out_format == FMT_MPEG1 &&
1840 50 90000LL * (avctx->rc_buffer_size - 1) <=
1841
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 25 times.
50 avctx->rc_max_rate * 0xFFFFLL) {
1842 AVCPBProperties *props;
1843 size_t props_size;
1844
1845 int vbv_delay, min_delay;
1846 50 double inbits = avctx->rc_max_rate *
1847 25 av_q2d(avctx->time_base);
1848 25 int minbits = s->frame_bits - 8 *
1849 25 (s->vbv_delay_ptr - s->pb.buf - 1);
1850 25 double bits = s->rc_context.buffer_index + minbits - inbits;
1851
1852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (bits < 0)
1853 av_log(avctx, AV_LOG_ERROR,
1854 "Internal error, negative bits\n");
1855
1856 av_assert1(s->repeat_first_field == 0);
1857
1858 25 vbv_delay = bits * 90000 / avctx->rc_max_rate;
1859 25 min_delay = (minbits * 90000LL + avctx->rc_max_rate - 1) /
1860 25 avctx->rc_max_rate;
1861
1862 25 vbv_delay = FFMAX(vbv_delay, min_delay);
1863
1864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 av_assert0(vbv_delay < 0xFFFF);
1865
1866 25 s->vbv_delay_ptr[0] &= 0xF8;
1867 25 s->vbv_delay_ptr[0] |= vbv_delay >> 13;
1868 25 s->vbv_delay_ptr[1] = vbv_delay >> 5;
1869 25 s->vbv_delay_ptr[2] &= 0x07;
1870 25 s->vbv_delay_ptr[2] |= vbv_delay << 3;
1871
1872 25 props = av_cpb_properties_alloc(&props_size);
1873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!props)
1874 return AVERROR(ENOMEM);
1875 25 props->vbv_delay = vbv_delay * 300;
1876
1877 25 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_CPB_PROPERTIES,
1878 (uint8_t*)props, props_size);
1879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0) {
1880 av_freep(&props);
1881 return ret;
1882 }
1883 }
1884 8458 s->total_bits += s->frame_bits;
1885
1886 8458 pkt->pts = s->current_picture.f->pts;
1887
4/4
✓ Branch 0 taken 3115 times.
✓ Branch 1 taken 5343 times.
✓ Branch 2 taken 1768 times.
✓ Branch 3 taken 1347 times.
8458 if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
1888
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1700 times.
1768 if (!s->current_picture.f->coded_picture_number)
1889 68 pkt->dts = pkt->pts - s->dts_delta;
1890 else
1891 1700 pkt->dts = s->reordered_pts;
1892 1768 s->reordered_pts = pkt->pts;
1893 } else
1894 6690 pkt->dts = pkt->pts;
1895
2/2
✓ Branch 0 taken 2198 times.
✓ Branch 1 taken 6260 times.
8458 if (s->current_picture.f->key_frame)
1896 2198 pkt->flags |= AV_PKT_FLAG_KEY;
1897
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8458 times.
8458 if (s->mb_info)
1898 av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1899 } else {
1900 224 s->frame_bits = 0;
1901 }
1902
1903 /* release non-reference frames */
1904
2/2
✓ Branch 0 taken 312552 times.
✓ Branch 1 taken 8682 times.
321234 for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1905
2/2
✓ Branch 0 taken 289539 times.
✓ Branch 1 taken 23013 times.
312552 if (!s->picture[i].reference)
1906 289539 ff_mpeg_unref_picture(avctx, &s->picture[i]);
1907 }
1908
1909 av_assert1((s->frame_bits & 7) == 0);
1910
1911 8682 pkt->size = s->frame_bits / 8;
1912 8682 *got_packet = !!pkt->size;
1913 8682 return 0;
1914 }
1915
1916 static inline void dct_single_coeff_elimination(MpegEncContext *s,
1917 int n, int threshold)
1918 {
1919 static const char tab[64] = {
1920 3, 2, 2, 1, 1, 1, 1, 1,
1921 1, 1, 1, 1, 1, 1, 1, 1,
1922 1, 1, 1, 1, 1, 1, 1, 1,
1923 0, 0, 0, 0, 0, 0, 0, 0,
1924 0, 0, 0, 0, 0, 0, 0, 0,
1925 0, 0, 0, 0, 0, 0, 0, 0,
1926 0, 0, 0, 0, 0, 0, 0, 0,
1927 0, 0, 0, 0, 0, 0, 0, 0
1928 };
1929 int score = 0;
1930 int run = 0;
1931 int i;
1932 int16_t *block = s->block[n];
1933 const int last_index = s->block_last_index[n];
1934 int skip_dc;
1935
1936 if (threshold < 0) {
1937 skip_dc = 0;
1938 threshold = -threshold;
1939 } else
1940 skip_dc = 1;
1941
1942 /* Are all we could set to zero already zero? */
1943 if (last_index <= skip_dc - 1)
1944 return;
1945
1946 for (i = 0; i <= last_index; i++) {
1947 const int j = s->intra_scantable.permutated[i];
1948 const int level = FFABS(block[j]);
1949 if (level == 1) {
1950 if (skip_dc && i == 0)
1951 continue;
1952 score += tab[run];
1953 run = 0;
1954 } else if (level > 1) {
1955 return;
1956 } else {
1957 run++;
1958 }
1959 }
1960 if (score >= threshold)
1961 return;
1962 for (i = skip_dc; i <= last_index; i++) {
1963 const int j = s->intra_scantable.permutated[i];
1964 block[j] = 0;
1965 }
1966 if (block[0])
1967 s->block_last_index[n] = 0;
1968 else
1969 s->block_last_index[n] = -1;
1970 }
1971
1972 static inline void clip_coeffs(MpegEncContext *s, int16_t *block,
1973 int last_index)
1974 {
1975 int i;
1976 const int maxlevel = s->max_qcoeff;
1977 const int minlevel = s->min_qcoeff;
1978 int overflow = 0;
1979
1980 if (s->mb_intra) {
1981 i = 1; // skip clipping of intra dc
1982 } else
1983 i = 0;
1984
1985 for (; i <= last_index; i++) {
1986 const int j = s->intra_scantable.permutated[i];
1987 int level = block[j];
1988
1989 if (level > maxlevel) {
1990 level = maxlevel;
1991 overflow++;
1992 } else if (level < minlevel) {
1993 level = minlevel;
1994 overflow++;
1995 }
1996
1997 block[j] = level;
1998 }
1999
2000 if (overflow && s->avctx->mb_decision == FF_MB_DECISION_SIMPLE)
2001 av_log(s->avctx, AV_LOG_INFO,
2002 "warning, clipping %d dct coefficients to %d..%d\n",
2003 overflow, minlevel, maxlevel);
2004 }
2005
2006 static void get_visual_weight(int16_t *weight, uint8_t *ptr, int stride)
2007 {
2008 int x, y;
2009 // FIXME optimize
2010 for (y = 0; y < 8; y++) {
2011 for (x = 0; x < 8; x++) {
2012 int x2, y2;
2013 int sum = 0;
2014 int sqr = 0;
2015 int count = 0;
2016
2017 for (y2 = FFMAX(y - 1, 0); y2 < FFMIN(8, y + 2); y2++) {
2018 for (x2= FFMAX(x - 1, 0); x2 < FFMIN(8, x + 2); x2++) {
2019 int v = ptr[x2 + y2 * stride];
2020 sum += v;
2021 sqr += v * v;
2022 count++;
2023 }
2024 }
2025 weight[x + 8 * y]= (36 * ff_sqrt(count * sqr - sum * sum)) / count;
2026 }
2027 }
2028 }
2029
2030 4693654 static av_always_inline void encode_mb_internal(MpegEncContext *s,
2031 int motion_x, int motion_y,
2032 int mb_block_height,
2033 int mb_block_width,
2034 int mb_block_count,
2035 int chroma_x_shift,
2036 int chroma_y_shift,
2037 int chroma_format)
2038 {
2039 /* Interlaced DCT is only possible with MPEG-2 and MPEG-4
2040 * and neither of these encoders currently supports 444. */
2041 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2042 (s)->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2043 int16_t weight[12][64];
2044 int16_t orig[12][64];
2045 4693654 const int mb_x = s->mb_x;
2046 4693654 const int mb_y = s->mb_y;
2047 int i;
2048 int skip_dct[12];
2049 4693654 int dct_offset = s->linesize * 8; // default for progressive frames
2050 4693654 int uv_dct_offset = s->uvlinesize * 8;
2051 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2052 ptrdiff_t wrap_y, wrap_c;
2053
2054
2/2
✓ Branch 0 taken 29926954 times.
✓ Branch 1 taken 4693654 times.
34620608 for (i = 0; i < mb_block_count; i++)
2055 29926954 skip_dct[i] = s->skipdct;
2056
2057
2/2
✓ Branch 0 taken 1271769 times.
✓ Branch 1 taken 3421885 times.
4693654 if (s->adaptive_quant) {
2058 1271769 const int last_qp = s->qscale;
2059 1271769 const int mb_xy = mb_x + mb_y * s->mb_stride;
2060
2061 1271769 s->lambda = s->lambda_table[mb_xy];
2062 1271769 update_qscale(s);
2063
2064
2/2
✓ Branch 0 taken 232420 times.
✓ Branch 1 taken 1039349 times.
1271769 if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
2065 232420 s->qscale = s->current_picture_ptr->qscale_table[mb_xy];
2066 232420 s->dquant = s->qscale - last_qp;
2067
2068
1/2
✓ Branch 0 taken 232420 times.
✗ Branch 1 not taken.
232420 if (s->out_format == FMT_H263) {
2069 232420 s->dquant = av_clip(s->dquant, -2, 2);
2070
2071
1/2
✓ Branch 0 taken 232420 times.
✗ Branch 1 not taken.
232420 if (s->codec_id == AV_CODEC_ID_MPEG4) {
2072
2/2
✓ Branch 0 taken 221829 times.
✓ Branch 1 taken 10591 times.
232420 if (!s->mb_intra) {
2073
2/2
✓ Branch 0 taken 177110 times.
✓ Branch 1 taken 44719 times.
221829 if (s->pict_type == AV_PICTURE_TYPE_B) {
2074
4/4
✓ Branch 0 taken 170173 times.
✓ Branch 1 taken 6937 times.
✓ Branch 2 taken 62215 times.
✓ Branch 3 taken 107958 times.
177110 if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
2075 69152 s->dquant = 0;
2076 }
2077
2/2
✓ Branch 0 taken 31669 times.
✓ Branch 1 taken 190160 times.
221829 if (s->mv_type == MV_TYPE_8X8)
2078 31669 s->dquant = 0;
2079 }
2080 }
2081 }
2082 }
2083 1271769 ff_set_qscale(s, last_qp + s->dquant);
2084
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3421885 times.
3421885 } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
2085 ff_set_qscale(s, s->qscale + s->dquant);
2086
2087 4693654 wrap_y = s->linesize;
2088 4693654 wrap_c = s->uvlinesize;
2089 4693654 ptr_y = s->new_picture.f->data[0] +
2090 4693654 (mb_y * 16 * wrap_y) + mb_x * 16;
2091 4693654 ptr_cb = s->new_picture.f->data[1] +
2092 4693654 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2093 4693654 ptr_cr = s->new_picture.f->data[2] +
2094 4693654 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2095
2096
6/6
✓ Branch 0 taken 4683460 times.
✓ Branch 1 taken 10194 times.
✓ Branch 2 taken 10379 times.
✓ Branch 3 taken 4673081 times.
✓ Branch 4 taken 20323 times.
✓ Branch 5 taken 250 times.
4693654 if((mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
2097 20323 uint8_t *ebuf = s->sc.edge_emu_buffer + 38 * wrap_y;
2098 20323 int cw = (s->width + chroma_x_shift) >> chroma_x_shift;
2099 20323 int ch = (s->height + chroma_y_shift) >> chroma_y_shift;
2100 20323 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2101 wrap_y, wrap_y,
2102 16, 16, mb_x * 16, mb_y * 16,
2103 s->width, s->height);
2104 20323 ptr_y = ebuf;
2105 20323 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2106 wrap_c, wrap_c,
2107 mb_block_width, mb_block_height,
2108 mb_x * mb_block_width, mb_y * mb_block_height,
2109 cw, ch);
2110 20323 ptr_cb = ebuf + 16 * wrap_y;
2111 20323 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2112 wrap_c, wrap_c,
2113 mb_block_width, mb_block_height,
2114 mb_x * mb_block_width, mb_y * mb_block_height,
2115 cw, ch);
2116 20323 ptr_cr = ebuf + 16 * wrap_y + 16;
2117 }
2118
2119
2/2
✓ Branch 0 taken 1200048 times.
✓ Branch 1 taken 3493606 times.
4693654 if (s->mb_intra) {
2120
6/6
✓ Branch 0 taken 415210 times.
✓ Branch 1 taken 784838 times.
✓ Branch 2 taken 355360 times.
✓ Branch 3 taken 59850 times.
✓ Branch 4 taken 317596 times.
✓ Branch 5 taken 822602 times.
1200048 if (INTERLACED_DCT(s)) {
2121 int progressive_score, interlaced_score;
2122
2123 317596 s->interlaced_dct = 0;
2124 317596 progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
2125 317596 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
2126 NULL, wrap_y, 8) - 400;
2127
2128
2/2
✓ Branch 0 taken 298126 times.
✓ Branch 1 taken 19470 times.
317596 if (progressive_score > 0) {
2129 298126 interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
2130 NULL, wrap_y * 2, 8) +
2131 298126 s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
2132 NULL, wrap_y * 2, 8);
2133
2/2
✓ Branch 0 taken 364 times.
✓ Branch 1 taken 297762 times.
298126 if (progressive_score > interlaced_score) {
2134 364 s->interlaced_dct = 1;
2135
2136 364 dct_offset = wrap_y;
2137 364 uv_dct_offset = wrap_c;
2138 364 wrap_y <<= 1;
2139
3/4
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 179 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 185 times.
364 if (chroma_format == CHROMA_422 ||
2140 chroma_format == CHROMA_444)
2141 179 wrap_c <<= 1;
2142 }
2143 }
2144 }
2145
2146 1200048 s->pdsp.get_pixels(s->block[0], ptr_y, wrap_y);
2147 1200048 s->pdsp.get_pixels(s->block[1], ptr_y + 8, wrap_y);
2148 1200048 s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset, wrap_y);
2149 1200048 s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
2150
2151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1200048 times.
1200048 if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2152 skip_dct[4] = 1;
2153 skip_dct[5] = 1;
2154 } else {
2155 1200048 s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
2156 1200048 s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
2157
2/2
✓ Branch 0 taken 355360 times.
✓ Branch 1 taken 844688 times.
1200048 if (chroma_format == CHROMA_422) {
2158 355360 s->pdsp.get_pixels(s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2159 355360 s->pdsp.get_pixels(s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2160
2/2
✓ Branch 0 taken 59850 times.
✓ Branch 1 taken 784838 times.
844688 } else if (chroma_format == CHROMA_444) {
2161 59850 s->pdsp.get_pixels(s->block[ 6], ptr_cb + 8, wrap_c);
2162 59850 s->pdsp.get_pixels(s->block[ 7], ptr_cr + 8, wrap_c);
2163 59850 s->pdsp.get_pixels(s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2164 59850 s->pdsp.get_pixels(s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2165 59850 s->pdsp.get_pixels(s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2166 59850 s->pdsp.get_pixels(s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2167 }
2168 }
2169 } else {
2170 op_pixels_func (*op_pix)[4];
2171 qpel_mc_func (*op_qpix)[16];
2172 uint8_t *dest_y, *dest_cb, *dest_cr;
2173
2174 3493606 dest_y = s->dest[0];
2175 3493606 dest_cb = s->dest[1];
2176 3493606 dest_cr = s->dest[2];
2177
2178
4/4
✓ Branch 0 taken 959117 times.
✓ Branch 1 taken 2534489 times.
✓ Branch 2 taken 379428 times.
✓ Branch 3 taken 579689 times.
3493606 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
2179 2913917 op_pix = s->hdsp.put_pixels_tab;
2180 2913917 op_qpix = s->qdsp.put_qpel_pixels_tab;
2181 } else {
2182 579689 op_pix = s->hdsp.put_no_rnd_pixels_tab;
2183 579689 op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
2184 }
2185
2186
2/2
✓ Branch 0 taken 3143540 times.
✓ Branch 1 taken 350066 times.
3493606 if (s->mv_dir & MV_DIR_FORWARD) {
2187 3143540 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0,
2188 3143540 s->last_picture.f->data,
2189 op_pix, op_qpix);
2190 3143540 op_pix = s->hdsp.avg_pixels_tab;
2191 3143540 op_qpix = s->qdsp.avg_qpel_pixels_tab;
2192 }
2193
2/2
✓ Branch 0 taken 1023258 times.
✓ Branch 1 taken 2470348 times.
3493606 if (s->mv_dir & MV_DIR_BACKWARD) {
2194 1023258 ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1,
2195 1023258 s->next_picture.f->data,
2196 op_pix, op_qpix);
2197 }
2198
2199
5/6
✓ Branch 0 taken 347605 times.
✓ Branch 1 taken 3146001 times.
✓ Branch 2 taken 347605 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 505519 times.
✓ Branch 5 taken 2988087 times.
3493606 if (INTERLACED_DCT(s)) {
2200 int progressive_score, interlaced_score;
2201
2202 505519 s->interlaced_dct = 0;
2203 505519 progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
2204 505519 s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
2205 505519 ptr_y + wrap_y * 8,
2206 wrap_y, 8) - 400;
2207
2208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 505519 times.
505519 if (s->avctx->ildct_cmp == FF_CMP_VSSE)
2209 progressive_score -= 400;
2210
2211
2/2
✓ Branch 0 taken 451992 times.
✓ Branch 1 taken 53527 times.
505519 if (progressive_score > 0) {
2212 451992 interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
2213 wrap_y * 2, 8) +
2214 451992 s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
2215 ptr_y + wrap_y,
2216 wrap_y * 2, 8);
2217
2218
2/2
✓ Branch 0 taken 11826 times.
✓ Branch 1 taken 440166 times.
451992 if (progressive_score > interlaced_score) {
2219 11826 s->interlaced_dct = 1;
2220
2221 11826 dct_offset = wrap_y;
2222 11826 uv_dct_offset = wrap_c;
2223 11826 wrap_y <<= 1;
2224
2/2
✓ Branch 0 taken 8280 times.
✓ Branch 1 taken 3546 times.
11826 if (chroma_format == CHROMA_422)
2225 8280 wrap_c <<= 1;
2226 }
2227 }
2228 }
2229
2230 3493606 s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
2231 3493606 s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2232 3493606 s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
2233 3493606 dest_y + dct_offset, wrap_y);
2234 3493606 s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
2235 3493606 dest_y + dct_offset + 8, wrap_y);
2236
2237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3493606 times.
3493606 if (s->avctx->flags & AV_CODEC_FLAG_GRAY) {
2238 skip_dct[4] = 1;
2239 skip_dct[5] = 1;
2240 } else {
2241 3493606 s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
2242 3493606 s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
2243
2/2
✓ Branch 0 taken 347605 times.
✓ Branch 1 taken 3146001 times.
3493606 if (!chroma_y_shift) { /* 422 */
2244 347605 s->pdsp.diff_pixels(s->block[6], ptr_cb + uv_dct_offset,
2245 347605 dest_cb + uv_dct_offset, wrap_c);
2246 347605 s->pdsp.diff_pixels(s->block[7], ptr_cr + uv_dct_offset,
2247 347605 dest_cr + uv_dct_offset, wrap_c);
2248 }
2249 }
2250 /* pre quantization */
2251 3493606 if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2252
2/2
✓ Branch 0 taken 2917989 times.
✓ Branch 1 taken 575617 times.
3493606 2 * s->qscale * s->qscale) {
2253 // FIXME optimize
2254
2/2
✓ Branch 1 taken 918251 times.
✓ Branch 2 taken 1999738 times.
2917989 if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
2255 918251 skip_dct[0] = 1;
2256
2/2
✓ Branch 1 taken 935564 times.
✓ Branch 2 taken 1982425 times.
2917989 if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2257 935564 skip_dct[1] = 1;
2258 2917989 if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
2259
2/2
✓ Branch 0 taken 937339 times.
✓ Branch 1 taken 1980650 times.
2917989 wrap_y, 8) < 20 * s->qscale)
2260 937339 skip_dct[2] = 1;
2261 2917989 if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
2262
2/2
✓ Branch 0 taken 910140 times.
✓ Branch 1 taken 2007849 times.
2917989 wrap_y, 8) < 20 * s->qscale)
2263 910140 skip_dct[3] = 1;
2264
2/2
✓ Branch 1 taken 1264272 times.
✓ Branch 2 taken 1653717 times.
2917989 if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
2265 1264272 skip_dct[4] = 1;
2266
2/2
✓ Branch 1 taken 1232637 times.
✓ Branch 2 taken 1685352 times.
2917989 if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
2267 1232637 skip_dct[5] = 1;
2268
2/2
✓ Branch 0 taken 294319 times.
✓ Branch 1 taken 2623670 times.
2917989 if (!chroma_y_shift) { /* 422 */
2269 294319 if (s->mecc.sad[1](NULL, ptr_cb + uv_dct_offset,
2270 dest_cb + uv_dct_offset,
2271
2/2
✓ Branch 0 taken 153422 times.
✓ Branch 1 taken 140897 times.
294319 wrap_c, 8) < 20 * s->qscale)
2272 153422 skip_dct[6] = 1;
2273 294319 if (s->mecc.sad[1](NULL, ptr_cr + uv_dct_offset,
2274 dest_cr + uv_dct_offset,
2275
2/2
✓ Branch 0 taken 147975 times.
✓ Branch 1 taken 146344 times.
294319 wrap_c, 8) < 20 * s->qscale)
2276 147975 skip_dct[7] = 1;
2277 }
2278 }
2279 }
2280
2281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4693654 times.
4693654 if (s->quantizer_noise_shaping) {
2282 if (!skip_dct[0])
2283 get_visual_weight(weight[0], ptr_y , wrap_y);
2284 if (!skip_dct[1])
2285 get_visual_weight(weight[1], ptr_y + 8, wrap_y);
2286 if (!skip_dct[2])
2287 get_visual_weight(weight[2], ptr_y + dct_offset , wrap_y);
2288 if (!skip_dct[3])
2289 get_visual_weight(weight[3], ptr_y + dct_offset + 8, wrap_y);
2290 if (!skip_dct[4])
2291 get_visual_weight(weight[4], ptr_cb , wrap_c);
2292 if (!skip_dct[5])
2293 get_visual_weight(weight[5], ptr_cr , wrap_c);
2294 if (!chroma_y_shift) { /* 422 */
2295 if (!skip_dct[6])
2296 get_visual_weight(weight[6], ptr_cb + uv_dct_offset,
2297 wrap_c);
2298 if (!skip_dct[7])
2299 get_visual_weight(weight[7], ptr_cr + uv_dct_offset,
2300 wrap_c);
2301 }
2302 memcpy(orig[0], s->block[0], sizeof(int16_t) * 64 * mb_block_count);
2303 }
2304
2305 /* DCT & quantize */
2306 av_assert2(s->out_format != FMT_MJPEG || s->qscale == 8);
2307 {
2308
2/2
✓ Branch 0 taken 29926954 times.
✓ Branch 1 taken 4693654 times.
34620608 for (i = 0; i < mb_block_count; i++) {
2309
2/2
✓ Branch 0 taken 23427354 times.
✓ Branch 1 taken 6499600 times.
29926954 if (!skip_dct[i]) {
2310 int overflow;
2311 23427354 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow);
2312 // FIXME we could decide to change to quantizer instead of
2313 // clipping
2314 // JS: I don't think that would be a good idea it could lower
2315 // quality instead of improve it. Just INTRADC clipping
2316 // deserves changes in quantizer
2317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23427354 times.
23427354 if (overflow)
2318 clip_coeffs(s, s->block[i], s->block_last_index[i]);
2319 } else
2320 6499600 s->block_last_index[i] = -1;
2321 }
2322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4693654 times.
4693654 if (s->quantizer_noise_shaping) {
2323 for (i = 0; i < mb_block_count; i++) {
2324 if (!skip_dct[i]) {
2325 s->block_last_index[i] =
2326 dct_quantize_refine(s, s->block[i], weight[i],
2327 orig[i], i, s->qscale);
2328 }
2329 }
2330 }
2331
2332
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4693654 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4693654 if (s->luma_elim_threshold && !s->mb_intra)
2333 for (i = 0; i < 4; i++)
2334 dct_single_coeff_elimination(s, i, s->luma_elim_threshold);
2335
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4693654 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4693654 if (s->chroma_elim_threshold && !s->mb_intra)
2336 for (i = 4; i < mb_block_count; i++)
2337 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2338
2339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4693654 times.
4693654 if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2340 for (i = 0; i < mb_block_count; i++) {
2341 if (s->block_last_index[i] == -1)
2342 s->coded_score[i] = INT_MAX / 256;
2343 }
2344 }
2345 }
2346
2347
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4693654 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4693654 if ((s->avctx->flags & AV_CODEC_FLAG_GRAY) && s->mb_intra) {
2348 s->block_last_index[4] =
2349 s->block_last_index[5] = 0;
2350 s->block[4][0] =
2351 s->block[5][0] = (1024 + s->c_dc_scale / 2) / s->c_dc_scale;
2352 if (!chroma_y_shift) { /* 422 / 444 */
2353 for (i=6; i<12; i++) {
2354 s->block_last_index[i] = 0;
2355 s->block[i][0] = s->block[4][0];
2356 }
2357 }
2358 }
2359
2360 // non c quantize code returns incorrect block_last_index FIXME
2361
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4693654 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4693654 if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2362 for (i = 0; i < mb_block_count; i++) {
2363 int j;
2364 if (s->block_last_index[i] > 0) {
2365 for (j = 63; j > 0; j--) {
2366 if (s->block[i][s->intra_scantable.permutated[j]])
2367 break;
2368 }
2369 s->block_last_index[i] = j;
2370 }
2371 }
2372 }
2373
2374 /* huffman encode */
2375
7/9
✓ Branch 0 taken 1657917 times.
✓ Branch 1 taken 1790211 times.
✓ Branch 2 taken 189450 times.
✓ Branch 3 taken 59850 times.
✓ Branch 4 taken 133678 times.
✓ Branch 5 taken 428550 times.
✓ Branch 6 taken 433998 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
4693654 switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2376 1657917 case AV_CODEC_ID_MPEG1VIDEO:
2377 case AV_CODEC_ID_MPEG2VIDEO:
2378 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2379 1657917 ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2380 1657917 break;
2381 1790211 case AV_CODEC_ID_MPEG4:
2382 if (CONFIG_MPEG4_ENCODER)
2383 1790211 ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2384 1790211 break;
2385 189450 case AV_CODEC_ID_MSMPEG4V2:
2386 case AV_CODEC_ID_MSMPEG4V3:
2387 case AV_CODEC_ID_WMV1:
2388 if (CONFIG_MSMPEG4_ENCODER)
2389 189450 ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2390 189450 break;
2391 59850 case AV_CODEC_ID_WMV2:
2392 if (CONFIG_WMV2_ENCODER)
2393 59850 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2394 59850 break;
2395 133678 case AV_CODEC_ID_H261:
2396 if (CONFIG_H261_ENCODER)
2397 133678 ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2398 133678 break;
2399 428550 case AV_CODEC_ID_H263:
2400 case AV_CODEC_ID_H263P:
2401 case AV_CODEC_ID_FLV1:
2402 case AV_CODEC_ID_RV10:
2403 case AV_CODEC_ID_RV20:
2404 if (CONFIG_H263_ENCODER)
2405 428550 ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2406 428550 break;
2407 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2408 433998 case AV_CODEC_ID_MJPEG:
2409 case AV_CODEC_ID_AMV:
2410 433998 ff_mjpeg_encode_mb(s, s->block);
2411 433998 break;
2412 #endif
2413 case AV_CODEC_ID_SPEEDHQ:
2414 if (CONFIG_SPEEDHQ_ENCODER)
2415 ff_speedhq_encode_mb(s, s->block);
2416 break;
2417 4693654 default:
2418 av_assert1(0);
2419 }
2420 4693654 }
2421
2422 4693654 static av_always_inline void encode_mb(MpegEncContext *s, int motion_x, int motion_y)
2423 {
2424
2/2
✓ Branch 0 taken 3930839 times.
✓ Branch 1 taken 762815 times.
4693654 if (s->chroma_format == CHROMA_420)
2425 3930839 encode_mb_internal(s, motion_x, motion_y, 8, 8, 6, 1, 1, CHROMA_420);
2426
2/2
✓ Branch 0 taken 702965 times.
✓ Branch 1 taken 59850 times.
762815 else if (s->chroma_format == CHROMA_422)
2427 702965 encode_mb_internal(s, motion_x, motion_y, 16, 8, 8, 1, 0, CHROMA_422);
2428 else
2429 59850 encode_mb_internal(s, motion_x, motion_y, 16, 16, 12, 0, 0, CHROMA_444);
2430 4693654 }
2431
2432 2583315 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){
2433 int i;
2434
2435 2583315 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2436
2437 /* MPEG-1 */
2438 2583315 d->mb_skip_run= s->mb_skip_run;
2439
2/2
✓ Branch 0 taken 7749945 times.
✓ Branch 1 taken 2583315 times.
10333260 for(i=0; i<3; i++)
2440 7749945 d->last_dc[i] = s->last_dc[i];
2441
2442 /* statistics */
2443 2583315 d->mv_bits= s->mv_bits;
2444 2583315 d->i_tex_bits= s->i_tex_bits;
2445 2583315 d->p_tex_bits= s->p_tex_bits;
2446 2583315 d->i_count= s->i_count;
2447 2583315 d->f_count= s->f_count;
2448 2583315 d->b_count= s->b_count;
2449 2583315 d->skip_count= s->skip_count;
2450 2583315 d->misc_bits= s->misc_bits;
2451 2583315 d->last_bits= 0;
2452
2453 2583315 d->mb_skipped= 0;
2454 2583315 d->qscale= s->qscale;
2455 2583315 d->dquant= s->dquant;
2456
2457 2583315 d->esc3_level_length= s->esc3_level_length;
2458 2583315 }
2459
2460 1525818 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){
2461 int i;
2462
2463 1525818 memcpy(d->mv, s->mv, 2*4*2*sizeof(int));
2464 1525818 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster than a loop?
2465
2466 /* MPEG-1 */
2467 1525818 d->mb_skip_run= s->mb_skip_run;
2468
2/2
✓ Branch 0 taken 4577454 times.
✓ Branch 1 taken 1525818 times.
6103272 for(i=0; i<3; i++)
2469 4577454 d->last_dc[i] = s->last_dc[i];
2470
2471 /* statistics */
2472 1525818 d->mv_bits= s->mv_bits;
2473 1525818 d->i_tex_bits= s->i_tex_bits;
2474 1525818 d->p_tex_bits= s->p_tex_bits;
2475 1525818 d->i_count= s->i_count;
2476 1525818 d->f_count= s->f_count;
2477 1525818 d->b_count= s->b_count;
2478 1525818 d->skip_count= s->skip_count;
2479 1525818 d->misc_bits= s->misc_bits;
2480
2481 1525818 d->mb_intra= s->mb_intra;
2482 1525818 d->mb_skipped= s->mb_skipped;
2483 1525818 d->mv_type= s->mv_type;
2484 1525818 d->mv_dir= s->mv_dir;
2485 1525818 d->pb= s->pb;
2486
2/2
✓ Branch 0 taken 373903 times.
✓ Branch 1 taken 1151915 times.
1525818 if(s->data_partitioning){
2487 373903 d->pb2= s->pb2;
2488 373903 d->tex_pb= s->tex_pb;
2489 }
2490 1525818 d->block= s->block;
2491
2/2
✓ Branch 0 taken 12206544 times.
✓ Branch 1 taken 1525818 times.
13732362 for(i=0; i<8; i++)
2492 12206544 d->block_last_index[i]= s->block_last_index[i];
2493 1525818 d->interlaced_dct= s->interlaced_dct;
2494 1525818 d->qscale= s->qscale;
2495
2496 1525818 d->esc3_level_length= s->esc3_level_length;
2497 1525818 }
2498
2499 2051423 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type,
2500 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2],
2501 int *dmin, int *next_block, int motion_x, int motion_y)
2502 {
2503 int score;
2504 uint8_t *dest_backup[3];
2505
2506 2051423 copy_context_before_encode(s, backup, type);
2507
2508 2051423 s->block= s->blocks[*next_block];
2509 2051423 s->pb= pb[*next_block];
2510
2/2
✓ Branch 0 taken 376394 times.
✓ Branch 1 taken 1675029 times.
2051423 if(s->data_partitioning){
2511 376394 s->pb2 = pb2 [*next_block];
2512 376394 s->tex_pb= tex_pb[*next_block];
2513 }
2514
2515
2/2
✓ Branch 0 taken 1083400 times.
✓ Branch 1 taken 968023 times.
2051423 if(*next_block){
2516 1083400 memcpy(dest_backup, s->dest, sizeof(s->dest));
2517 1083400 s->dest[0] = s->sc.rd_scratchpad;
2518 1083400 s->dest[1] = s->sc.rd_scratchpad + 16*s->linesize;
2519 1083400 s->dest[2] = s->sc.rd_scratchpad + 16*s->linesize + 8;
2520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1083400 times.
1083400 av_assert0(s->linesize >= 32); //FIXME
2521 }
2522
2523 2051423 encode_mb(s, motion_x, motion_y);
2524
2525 2051423 score= put_bits_count(&s->pb);
2526
2/2
✓ Branch 0 taken 376394 times.
✓ Branch 1 taken 1675029 times.
2051423 if(s->data_partitioning){
2527 376394 score+= put_bits_count(&s->pb2);
2528 376394 score+= put_bits_count(&s->tex_pb);
2529 }
2530
2531
2/2
✓ Branch 0 taken 1674464 times.
✓ Branch 1 taken 376959 times.
2051423 if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2532 1674464 ff_mpv_reconstruct_mb(s, s->block);
2533
2534 1674464 score *= s->lambda2;
2535 1674464 score += sse_mb(s) << FF_LAMBDA_SHIFT;
2536 }
2537
2538
2/2
✓ Branch 0 taken 1083400 times.
✓ Branch 1 taken 968023 times.
2051423 if(*next_block){
2539 1083400 memcpy(s->dest, dest_backup, sizeof(s->dest));
2540 }
2541
2542
2/2
✓ Branch 0 taken 993926 times.
✓ Branch 1 taken 1057497 times.
2051423 if(score<*dmin){
2543 993926 *dmin= score;
2544 993926 *next_block^=1;
2545
2546 993926 copy_context_after_encode(best, s, type);
2547 }
2548 2051423 }
2549
2550 24804 static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2551 24804 const uint32_t *sq = ff_square_tab + 256;
2552 24804 int acc=0;
2553 int x,y;
2554
2555
3/4
✓ Branch 0 taken 3765 times.
✓ Branch 1 taken 21039 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3765 times.
24804 if(w==16 && h==16)
2556 return s->mecc.sse[0](NULL, src1, src2, stride, 16);
2557
3/4
✓ Branch 0 taken 7530 times.
✓ Branch 1 taken 17274 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7530 times.
24804 else if(w==8 && h==8)
2558 return s->mecc.sse[1](NULL, src1, src2, stride, 8);
2559
2560
2/2
✓ Branch 0 taken 107368 times.
✓ Branch 1 taken 24804 times.
132172 for(y=0; y<h; y++){
2561
2/2
✓ Branch 0 taken 290118 times.
✓ Branch 1 taken 107368 times.
397486 for(x=0; x<w; x++){
2562 290118 acc+= sq[src1[x + y*stride] - src2[x + y*stride]];
2563 }
2564 }
2565
2566 av_assert2(acc>=0);
2567
2568 24804 return acc;
2569 }
2570
2571 1674464 static int sse_mb(MpegEncContext *s){
2572 1674464 int w= 16;
2573 1674464 int h= 16;
2574
2575
2/2
✓ Branch 0 taken 4503 times.
✓ Branch 1 taken 1669961 times.
1674464 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
2576
2/2
✓ Branch 0 taken 5469 times.
✓ Branch 1 taken 1668995 times.
1674464 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
2577
2578
4/4
✓ Branch 0 taken 1669961 times.
✓ Branch 1 taken 4503 times.
✓ Branch 2 taken 1666196 times.
✓ Branch 3 taken 3765 times.
1674464 if(w==16 && h==16)
2579
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1666196 times.
1666196 if(s->avctx->mb_cmp == FF_CMP_NSSE){
2580 return s->mecc.nsse[0](s, s->new_picture.f->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16, s->dest[0], s->linesize, 16) +
2581 s->mecc.nsse[1](s, s->new_picture.f->data[1] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[1], s->uvlinesize, 8) +
2582 s->mecc.nsse[1](s, s->new_picture.f->data[2] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[2], s->uvlinesize, 8);
2583 }else{
2584 1666196 return s->mecc.sse[0](NULL, s->new_picture.f->data[0] + s->mb_x * 16 + s->mb_y * s->linesize * 16, s->dest[0], s->linesize, 16) +
2585 3332392 s->mecc.sse[1](NULL, s->new_picture.f->data[1] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[1], s->uvlinesize, 8) +
2586 1666196 s->mecc.sse[1](NULL, s->new_picture.f->data[2] + s->mb_x * 8 + s->mb_y * s->uvlinesize * 8, s->dest[2], s->uvlinesize, 8);
2587 }
2588 else
2589 8268 return sse(s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, s->dest[0], w, h, s->linesize)
2590 8268 +sse(s, s->new_picture.f->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[1], w>>1, h>>1, s->uvlinesize)
2591 8268 +sse(s, s->new_picture.f->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8,s->dest[2], w>>1, h>>1, s->uvlinesize);
2592 }
2593
2594 static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2595 MpegEncContext *s= *(void**)arg;
2596
2597
2598 s->me.pre_pass=1;
2599 s->me.dia_size= s->avctx->pre_dia_size;
2600 s->first_slice_line=1;
2601 for(s->mb_y= s->end_mb_y-1; s->mb_y >= s->start_mb_y; s->mb_y--) {
2602 for(s->mb_x=s->mb_width-1; s->mb_x >=0 ;s->mb_x--) {
2603 ff_pre_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2604 }
2605 s->first_slice_line=0;
2606 }
2607
2608 s->me.pre_pass=0;
2609
2610 return 0;
2611 }
2612
2613 6834 static int estimate_motion_thread(AVCodecContext *c, void *arg){
2614 6834 MpegEncContext *s= *(void**)arg;
2615
2616 6834 s->me.dia_size= s->avctx->dia_size;
2617 6834 s->first_slice_line=1;
2618
2/2
✓ Branch 0 taken 98181 times.
✓ Branch 1 taken 6834 times.
105015 for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2619 98181 s->mb_x=0; //for block init below
2620 98181 ff_init_block_index(s);
2621
2/2
✓ Branch 0 taken 2225799 times.
✓ Branch 1 taken 98181 times.
2323980 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
2622 2225799 s->block_index[0]+=2;
2623 2225799 s->block_index[1]+=2;
2624 2225799 s->block_index[2]+=2;
2625 2225799 s->block_index[3]+=2;
2626
2627 /* compute motion vector & mb_type and store in context */
2628
2/2
✓ Branch 0 taken 408312 times.
✓ Branch 1 taken 1817487 times.
2225799 if(s->pict_type==AV_PICTURE_TYPE_B)
2629 408312 ff_estimate_b_frame_motion(s, s->mb_x, s->mb_y);
2630 else
2631 1817487 ff_estimate_p_frame_motion(s, s->mb_x, s->mb_y);
2632 }
2633 98181 s->first_slice_line=0;
2634 }
2635 6834 return 0;
2636 }
2637
2638 369 static int mb_var_thread(AVCodecContext *c, void *arg){
2639 369 MpegEncContext *s= *(void**)arg;
2640 int mb_x, mb_y;
2641
2642
2/2
✓ Branch 0 taken 5126 times.
✓ Branch 1 taken 369 times.
5495 for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2643
2/2
✓ Branch 0 taken 118623 times.
✓ Branch 1 taken 5126 times.
123749 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2644 118623 int xx = mb_x * 16;
2645 118623 int yy = mb_y * 16;
2646 118623 uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2647 int varc;
2648 118623 int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
2649
2650 118623 varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
2651 118623 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2652
2653 118623 s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2654 118623 s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2655 118623 s->me.mb_var_sum_temp += varc;
2656 }
2657 }
2658 369 return 0;
2659 }
2660
2661 302273 static void write_slice_end(MpegEncContext *s){
2662
2/2
✓ Branch 0 taken 5913 times.
✓ Branch 1 taken 296360 times.
302273 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2663
2/2
✓ Branch 0 taken 1760 times.
✓ Branch 1 taken 4153 times.
5913 if(s->partitioned_frame){
2664 1760 ff_mpeg4_merge_partitions(s);
2665 }
2666
2667 5913 ff_mpeg4_stuffing(&s->pb);
2668 296360 } else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2669
2/2
✓ Branch 0 taken 1438 times.
✓ Branch 1 taken 294922 times.
296360 s->out_format == FMT_MJPEG) {
2670 1438 ff_mjpeg_encode_stuffing(s);
2671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294922 times.
294922 } else if (CONFIG_SPEEDHQ_ENCODER && s->out_format == FMT_SPEEDHQ) {
2672 ff_speedhq_end_slice(s);
2673 }
2674
2675 302273 flush_put_bits(&s->pb);
2676
2677
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 302273 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
302273 if ((s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->partitioned_frame)
2678 s->misc_bits+= get_bits_diff(s);
2679 302273 }
2680
2681 static void write_mb_info(MpegEncContext *s)
2682 {
2683 uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
2684 int offset = put_bits_count(&s->pb);
2685 int mba = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
2686 int gobn = s->mb_y / s->gob_index;
2687 int pred_x, pred_y;
2688 if (CONFIG_H263_ENCODER)
2689 ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
2690 bytestream_put_le32(&ptr, offset);
2691 bytestream_put_byte(&ptr, s->qscale);
2692 bytestream_put_byte(&ptr, gobn);
2693 bytestream_put_le16(&ptr, mba);
2694 bytestream_put_byte(&ptr, pred_x); /* hmv1 */
2695 bytestream_put_byte(&ptr, pred_y); /* vmv1 */
2696 /* 4MV not implemented */
2697 bytestream_put_byte(&ptr, 0); /* hmv2 */
2698 bytestream_put_byte(&ptr, 0); /* vmv2 */
2699 }
2700
2701 3176667 static void update_mb_info(MpegEncContext *s, int startcode)
2702 {
2703
1/2
✓ Branch 0 taken 3176667 times.
✗ Branch 1 not taken.
3176667 if (!s->mb_info)
2704 3176667 return;
2705 if (put_bytes_count(&s->pb, 0) - s->prev_mb_info >= s->mb_info) {
2706 s->mb_info_size += 12;
2707 s->prev_mb_info = s->last_mb_info;
2708 }
2709 if (startcode) {
2710 s->prev_mb_info = put_bytes_count(&s->pb, 0);
2711 /* This might have incremented mb_info_size above, and we return without
2712 * actually writing any info into that slot yet. But in that case,
2713 * this will be called again at the start of the after writing the
2714 * start code, actually writing the mb info. */
2715 return;
2716 }
2717
2718 s->last_mb_info = put_bytes_count(&s->pb, 0);
2719 if (!s->mb_info_size)
2720 s->mb_info_size += 12;
2721 write_mb_info(s);
2722 }
2723
2724 3176799 int ff_mpv_reallocate_putbitbuffer(MpegEncContext *s, size_t threshold, size_t size_increase)
2725 {
2726
2/2
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 3176774 times.
3176799 if (put_bytes_left(&s->pb, 0) < threshold
2727
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 && s->slice_context_count == 1
2728
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 && s->pb.buf == s->avctx->internal->byte_buffer) {
2729 25 int lastgob_pos = s->ptr_lastgob - s->pb.buf;
2730 25 int vbv_pos = s->vbv_delay_ptr - s->pb.buf;
2731
2732 25 uint8_t *new_buffer = NULL;
2733 25 int new_buffer_size = 0;
2734
2735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if ((s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2736 av_log(s->avctx, AV_LOG_ERROR, "Cannot reallocate putbit buffer\n");
2737 return AVERROR(ENOMEM);
2738 }
2739
2740 25 emms_c();
2741
2742 25 av_fast_padded_malloc(&new_buffer, &new_buffer_size,
2743 25 s->avctx->internal->byte_buffer_size + size_increase);
2744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!new_buffer)
2745 return AVERROR(ENOMEM);
2746
2747 25 memcpy(new_buffer, s->avctx->internal->byte_buffer, s->avctx->internal->byte_buffer_size);
2748 25 av_free(s->avctx->internal->byte_buffer);
2749 25 s->avctx->internal->byte_buffer = new_buffer;
2750 25 s->avctx->internal->byte_buffer_size = new_buffer_size;
2751 25 rebase_put_bits(&s->pb, new_buffer, new_buffer_size);
2752 25 s->ptr_lastgob = s->pb.buf + lastgob_pos;
2753 25 s->vbv_delay_ptr = s->pb.buf + vbv_pos;
2754 }
2755
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3176799 times.
3176799 if (put_bytes_left(&s->pb, 0) < threshold)
2756 return AVERROR(EINVAL);
2757 3176799 return 0;
2758 }
2759
2760 9183 static int encode_thread(AVCodecContext *c, void *arg){
2761 9183 MpegEncContext *s= *(void**)arg;
2762 int mb_x, mb_y, mb_y_order;
2763 9183 int chr_h= 16>>s->chroma_y_shift;
2764 int i, j;
2765 9183 MpegEncContext best_s = { 0 }, backup_s;
2766 uint8_t bit_buf[2][MAX_MB_BYTES];
2767 uint8_t bit_buf2[2][MAX_MB_BYTES];
2768 uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2769 PutBitContext pb[2], pb2[2], tex_pb[2];
2770
2771
2/2
✓ Branch 0 taken 18366 times.
✓ Branch 1 taken 9183 times.
27549 for(i=0; i<2; i++){
2772 18366 init_put_bits(&pb [i], bit_buf [i], MAX_MB_BYTES);
2773 18366 init_put_bits(&pb2 [i], bit_buf2 [i], MAX_MB_BYTES);
2774 18366 init_put_bits(&tex_pb[i], bit_buf_tex[i], MAX_MB_BYTES);
2775 }
2776
2777 9183 s->last_bits= put_bits_count(&s->pb);
2778 9183 s->mv_bits=0;
2779 9183 s->misc_bits=0;
2780 9183 s->i_tex_bits=0;
2781 9183 s->p_tex_bits=0;
2782 9183 s->i_count=0;
2783 9183 s->f_count=0;
2784 9183 s->b_count=0;
2785 9183 s->skip_count=0;
2786
2787
2/2
✓ Branch 0 taken 27549 times.
✓ Branch 1 taken 9183 times.
36732 for(i=0; i<3; i++){
2788 /* init last dc values */
2789 /* note: quant matrix value (8) is implied here */
2790 27549 s->last_dc[i] = 128 << s->intra_dc_precision;
2791
2792 27549 s->current_picture.encoding_error[i] = 0;
2793 }
2794
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 8983 times.
9183 if(s->codec_id==AV_CODEC_ID_AMV){
2795 200 s->last_dc[0] = 128*8/13;
2796 200 s->last_dc[1] = 128*8/14;
2797 200 s->last_dc[2] = 128*8/14;
2798 }
2799 9183 s->mb_skip_run = 0;
2800 9183 memset(s->last_mv, 0, sizeof(s->last_mv));
2801
2802 9183 s->last_mv_dir = 0;
2803
2804
3/3
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 2790 times.
✓ Branch 2 taken 5553 times.
9183 switch(s->codec_id){
2805 840 case AV_CODEC_ID_H263:
2806 case AV_CODEC_ID_H263P:
2807 case AV_CODEC_ID_FLV1:
2808 if (CONFIG_H263_ENCODER)
2809
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);
2810 840 break;
2811 2790 case AV_CODEC_ID_MPEG4:
2812
2/2
✓ Branch 0 taken 544 times.
✓ Branch 1 taken 2246 times.
2790 if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2813 544 ff_mpeg4_init_partitions(s);
2814 2790 break;
2815 }
2816
2817 9183 s->resync_mb_x=0;
2818 9183 s->resync_mb_y=0;
2819 9183 s->first_slice_line = 1;
2820 9183 s->ptr_lastgob = s->pb.buf;
2821
2/2
✓ Branch 0 taken 135734 times.
✓ Branch 1 taken 9183 times.
144917 for (mb_y_order = s->start_mb_y; mb_y_order < s->end_mb_y; mb_y_order++) {
2822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135734 times.
135734 if (CONFIG_SPEEDHQ_ENCODER && s->codec_id == AV_CODEC_ID_SPEEDHQ) {
2823 int first_in_slice;
2824 mb_y = ff_speedhq_mb_y_order_to_mb(mb_y_order, s->mb_height, &first_in_slice);
2825 if (first_in_slice && mb_y_order != s->start_mb_y)
2826 ff_speedhq_end_slice(s);
2827 s->last_dc[0] = s->last_dc[1] = s->last_dc[2] = 1024 << s->intra_dc_precision;
2828 } else {
2829 135734 mb_y = mb_y_order;
2830 }
2831 135734 s->mb_x=0;
2832 135734 s->mb_y= mb_y;
2833
2834 135734 ff_set_qscale(s, s->qscale);
2835 135734 ff_init_block_index(s);
2836
2837
2/2
✓ Branch 0 taken 3174123 times.
✓ Branch 1 taken 135734 times.
3309857 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2838 3174123 int xy= mb_y*s->mb_stride + mb_x; // removed const, H261 needs to adjust this
2839 3174123 int mb_type= s->mb_type[xy];
2840 // int d;
2841 3174123 int dmin= INT_MAX;
2842 int dir;
2843 3174123 int size_increase = s->avctx->internal->byte_buffer_size/4
2844 3174123 + s->mb_width*MAX_MB_BYTES;
2845
2846 3174123 ff_mpv_reallocate_putbitbuffer(s, MAX_MB_BYTES, size_increase);
2847
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3174123 times.
3174123 if (put_bytes_left(&s->pb, 0) < MAX_MB_BYTES){
2848 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
2849 return -1;
2850 }
2851
2/2
✓ Branch 0 taken 179550 times.
✓ Branch 1 taken 2994573 times.
3174123 if(s->data_partitioning){
2852
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 ||
2853 179550 put_bytes_left(&s->tex_pb, 0) < MAX_MB_BYTES) {
2854 av_log(s->avctx, AV_LOG_ERROR, "encoded partitioned frame too large\n");
2855 return -1;
2856 }
2857 }
2858
2859 3174123 s->mb_x = mb_x;
2860 3174123 s->mb_y = mb_y; // moved into loop, can get changed by H.261
2861 3174123 ff_update_block_index(s);
2862
2863
2/2
✓ Branch 0 taken 118800 times.
✓ Branch 1 taken 3055323 times.
3174123 if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2864 118800 ff_h261_reorder_mb_index(s);
2865 118800 xy= s->mb_y*s->mb_stride + s->mb_x;
2866 118800 mb_type= s->mb_type[xy];
2867 }
2868
2869 /* write gob / video packet header */
2870
2/2
✓ Branch 0 taken 1237830 times.
✓ Branch 1 taken 1936293 times.
3174123 if(s->rtp_mode){
2871 int current_packet_size, is_gob_start;
2872
2873 1237830 current_packet_size = put_bytes_count(&s->pb, 1)
2874 1237830 - (s->ptr_lastgob - s->pb.buf);
2875
2876 3030960 is_gob_start = s->rtp_payload_size &&
2877
4/4
✓ Branch 0 taken 555300 times.
✓ Branch 1 taken 682530 times.
✓ Branch 2 taken 310021 times.
✓ Branch 3 taken 245279 times.
1547851 current_packet_size >= s->rtp_payload_size &&
2878
2/2
✓ Branch 0 taken 309871 times.
✓ Branch 1 taken 150 times.
310021 mb_y + mb_x > 0;
2879
2880
6/6
✓ Branch 0 taken 66810 times.
✓ Branch 1 taken 1171020 times.
✓ Branch 2 taken 10350 times.
✓ Branch 3 taken 56460 times.
✓ Branch 4 taken 600 times.
✓ Branch 5 taken 9750 times.
1237830 if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2881
2882
4/5
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 939030 times.
✓ Branch 2 taken 59850 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 179550 times.
1237830 switch(s->codec_id){
2883 59400 case AV_CODEC_ID_H263:
2884 case AV_CODEC_ID_H263P:
2885
1/2
✓ Branch 0 taken 59400 times.
✗ Branch 1 not taken.
59400 if(!s->h263_slice_structured)
2886
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;
2887 59400 break;
2888 939030 case AV_CODEC_ID_MPEG2VIDEO:
2889
4/4
✓ Branch 0 taken 32015 times.
✓ Branch 1 taken 907015 times.
✓ Branch 2 taken 30230 times.
✓ Branch 3 taken 1785 times.
939030 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2890 case AV_CODEC_ID_MPEG1VIDEO:
2891
2/2
✓ Branch 0 taken 84516 times.
✓ Branch 1 taken 914364 times.
998880 if(s->mb_skip_run) is_gob_start=0;
2892 998880 break;
2893 case AV_CODEC_ID_MJPEG:
2894 if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2895 break;
2896 }
2897
2898
2/2
✓ Branch 0 taken 293690 times.
✓ Branch 1 taken 944140 times.
1237830 if(is_gob_start){
2899
4/4
✓ Branch 0 taken 7731 times.
✓ Branch 1 taken 285959 times.
✓ Branch 2 taken 7131 times.
✓ Branch 3 taken 600 times.
293690 if(s->start_mb_y != mb_y || mb_x!=0){
2900 293090 write_slice_end(s);
2901
2902
4/4
✓ Branch 0 taken 3123 times.
✓ Branch 1 taken 289967 times.
✓ Branch 2 taken 1216 times.
✓ Branch 3 taken 1907 times.
293090 if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2903 1216 ff_mpeg4_init_partitions(s);
2904 }
2905 }
2906
2907 av_assert2((put_bits_count(&s->pb)&7) == 0);
2908 293690 current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2909
2910
4/4
✓ Branch 0 taken 437 times.
✓ Branch 1 taken 293253 times.
✓ Branch 2 taken 287 times.
✓ Branch 3 taken 150 times.
293690 if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2911 287 int r = put_bytes_count(&s->pb, 0) + s->picture_number + 16 + s->mb_x + s->mb_y;
2912 287 int d = 100 / s->error_rate;
2913
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 255 times.
287 if(r % d == 0){
2914 32 current_packet_size=0;
2915 32 s->pb.buf_ptr= s->ptr_lastgob;
2916 av_assert1(put_bits_ptr(&s->pb) == s->ptr_lastgob);
2917 }
2918 }
2919
2920
3/4
✓ Branch 0 taken 3323 times.
✓ Branch 1 taken 287823 times.
✓ Branch 2 taken 2544 times.
✗ Branch 3 not taken.
293690 switch(s->codec_id){
2921 3323 case AV_CODEC_ID_MPEG4:
2922 if (CONFIG_MPEG4_ENCODER) {
2923 3323 ff_mpeg4_encode_video_packet_header(s);
2924 3323 ff_mpeg4_clean_buffers(s);
2925 }
2926 3323 break;
2927 287823 case AV_CODEC_ID_MPEG1VIDEO:
2928 case AV_CODEC_ID_MPEG2VIDEO:
2929 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2930 287823 ff_mpeg1_encode_slice_header(s);
2931 287823 ff_mpeg1_clean_buffers(s);
2932 }
2933 287823 break;
2934 2544 case AV_CODEC_ID_H263:
2935 case AV_CODEC_ID_H263P:
2936 if (CONFIG_H263_ENCODER) {
2937 2544 update_mb_info(s, 1);
2938 2544 ff_h263_encode_gob_header(s, mb_y);
2939 }
2940 2544 break;
2941 }
2942
2943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293690 times.
293690 if (s->avctx->flags & AV_CODEC_FLAG_PASS1) {
2944 int bits= put_bits_count(&s->pb);
2945 s->misc_bits+= bits - s->last_bits;
2946 s->last_bits= bits;
2947 }
2948
2949 293690 s->ptr_lastgob += current_packet_size;
2950 293690 s->first_slice_line=1;
2951 293690 s->resync_mb_x=mb_x;
2952 293690 s->resync_mb_y=mb_y;
2953 }
2954 }
2955
2956
2/2
✓ Branch 0 taken 393477 times.
✓ Branch 1 taken 2780646 times.
3174123 if( (s->resync_mb_x == s->mb_x)
2957
2/2
✓ Branch 0 taken 8503 times.
✓ Branch 1 taken 384974 times.
393477 && s->resync_mb_y+1 == s->mb_y){
2958 8503 s->first_slice_line=0;
2959 }
2960
2961 3174123 s->mb_skipped=0;
2962 3174123 s->dquant=0; //only for QP_RD
2963
2964 3174123 update_mb_info(s, 0);
2965
2966
4/4
✓ Branch 0 taken 2660962 times.
✓ Branch 1 taken 513161 times.
✓ Branch 2 taken 18731 times.
✓ Branch 3 taken 2642231 times.
3706015 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
2967 531892 int next_block=0;
2968 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2969
2970 531892 copy_context_before_encode(&backup_s, s, -1);
2971 531892 backup_s.pb= s->pb;
2972 531892 best_s.data_partitioning= s->data_partitioning;
2973 531892 best_s.partitioned_frame= s->partitioned_frame;
2974
2/2
✓ Branch 0 taken 141085 times.
✓ Branch 1 taken 390807 times.
531892 if(s->data_partitioning){
2975 141085 backup_s.pb2= s->pb2;
2976 141085 backup_s.tex_pb= s->tex_pb;
2977 }
2978
2979
2/2
✓ Branch 0 taken 282073 times.
✓ Branch 1 taken 249819 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_INTER){
2980 282073 s->mv_dir = MV_DIR_FORWARD;
2981 282073 s->mv_type = MV_TYPE_16X16;
2982 282073 s->mb_intra= 0;
2983 282073 s->mv[0][0][0] = s->p_mv_table[xy][0];
2984 282073 s->mv[0][0][1] = s->p_mv_table[xy][1];
2985 282073 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER, pb, pb2, tex_pb,
2986 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
2987 }
2988
2/2
✓ Branch 0 taken 13502 times.
✓ Branch 1 taken 518390 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_INTER_I){
2989 13502 s->mv_dir = MV_DIR_FORWARD;
2990 13502 s->mv_type = MV_TYPE_FIELD;
2991 13502 s->mb_intra= 0;
2992
2/2
✓ Branch 0 taken 27004 times.
✓ Branch 1 taken 13502 times.
40506 for(i=0; i<2; i++){
2993 27004 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
2994 27004 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
2995 27004 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
2996 }
2997 13502 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER_I, pb, pb2, tex_pb,
2998 &dmin, &next_block, 0, 0);
2999 }
3000
2/2
✓ Branch 0 taken 60612 times.
✓ Branch 1 taken 471280 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_SKIPPED){
3001 60612 s->mv_dir = MV_DIR_FORWARD;
3002 60612 s->mv_type = MV_TYPE_16X16;
3003 60612 s->mb_intra= 0;
3004 60612 s->mv[0][0][0] = 0;
3005 60612 s->mv[0][0][1] = 0;
3006 60612 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_SKIPPED, pb, pb2, tex_pb,
3007 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3008 }
3009
2/2
✓ Branch 0 taken 209210 times.
✓ Branch 1 taken 322682 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_INTER4V){
3010 209210 s->mv_dir = MV_DIR_FORWARD;
3011 209210 s->mv_type = MV_TYPE_8X8;
3012 209210 s->mb_intra= 0;
3013
2/2
✓ Branch 0 taken 836840 times.
✓ Branch 1 taken 209210 times.
1046050 for(i=0; i<4; i++){
3014 836840 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3015 836840 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3016 }
3017 209210 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER4V, pb, pb2, tex_pb,
3018 &dmin, &next_block, 0, 0);
3019 }
3020
2/2
✓ Branch 0 taken 229850 times.
✓ Branch 1 taken 302042 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_FORWARD){
3021 229850 s->mv_dir = MV_DIR_FORWARD;
3022 229850 s->mv_type = MV_TYPE_16X16;
3023 229850 s->mb_intra= 0;
3024 229850 s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3025 229850 s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3026 229850 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD, pb, pb2, tex_pb,
3027 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]);
3028 }
3029
2/2
✓ Branch 0 taken 229850 times.
✓ Branch 1 taken 302042 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD){
3030 229850 s->mv_dir = MV_DIR_BACKWARD;
3031 229850 s->mv_type = MV_TYPE_16X16;
3032 229850 s->mb_intra= 0;
3033 229850 s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3034 229850 s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3035 229850 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD, pb, pb2, tex_pb,
3036 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]);
3037 }
3038
2/2
✓ Branch 0 taken 229850 times.
✓ Branch 1 taken 302042 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_BIDIR){
3039 229850 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3040 229850 s->mv_type = MV_TYPE_16X16;
3041 229850 s->mb_intra= 0;
3042 229850 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3043 229850 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3044 229850 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3045 229850 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3046 229850 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR, pb, pb2, tex_pb,
3047 &dmin, &next_block, 0, 0);
3048 }
3049
2/2
✓ Branch 0 taken 31700 times.
✓ Branch 1 taken 500192 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_FORWARD_I){
3050 31700 s->mv_dir = MV_DIR_FORWARD;
3051 31700 s->mv_type = MV_TYPE_FIELD;
3052 31700 s->mb_intra= 0;
3053
2/2
✓ Branch 0 taken 63400 times.
✓ Branch 1 taken 31700 times.
95100 for(i=0; i<2; i++){
3054 63400 j= s->field_select[0][i] = s->b_field_select_table[0][i][xy];
3055 63400 s->mv[0][i][0] = s->b_field_mv_table[0][i][j][xy][0];
3056 63400 s->mv[0][i][1] = s->b_field_mv_table[0][i][j][xy][1];
3057 }
3058 31700 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_FORWARD_I, pb, pb2, tex_pb,
3059 &dmin, &next_block, 0, 0);
3060 }
3061
2/2
✓ Branch 0 taken 31688 times.
✓ Branch 1 taken 500204 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_BACKWARD_I){
3062 31688 s->mv_dir = MV_DIR_BACKWARD;
3063 31688 s->mv_type = MV_TYPE_FIELD;
3064 31688 s->mb_intra= 0;
3065
2/2
✓ Branch 0 taken 63376 times.
✓ Branch 1 taken 31688 times.
95064 for(i=0; i<2; i++){
3066 63376 j= s->field_select[1][i] = s->b_field_select_table[1][i][xy];
3067 63376 s->mv[1][i][0] = s->b_field_mv_table[1][i][j][xy][0];
3068 63376 s->mv[1][i][1] = s->b_field_mv_table[1][i][j][xy][1];
3069 }
3070 31688 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BACKWARD_I, pb, pb2, tex_pb,
3071 &dmin, &next_block, 0, 0);
3072 }
3073
2/2
✓ Branch 0 taken 26999 times.
✓ Branch 1 taken 504893 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_BIDIR_I){
3074 26999 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3075 26999 s->mv_type = MV_TYPE_FIELD;
3076 26999 s->mb_intra= 0;
3077
2/2
✓ Branch 0 taken 53998 times.
✓ Branch 1 taken 26999 times.
80997 for(dir=0; dir<2; dir++){
3078
2/2
✓ Branch 0 taken 107996 times.
✓ Branch 1 taken 53998 times.
161994 for(i=0; i<2; i++){
3079 107996 j= s->field_select[dir][i] = s->b_field_select_table[dir][i][xy];
3080 107996 s->mv[dir][i][0] = s->b_field_mv_table[dir][i][j][xy][0];
3081 107996 s->mv[dir][i][1] = s->b_field_mv_table[dir][i][j][xy][1];
3082 }
3083 }
3084 26999 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_BIDIR_I, pb, pb2, tex_pb,
3085 &dmin, &next_block, 0, 0);
3086 }
3087
2/2
✓ Branch 0 taken 115405 times.
✓ Branch 1 taken 416487 times.
531892 if(mb_type&CANDIDATE_MB_TYPE_INTRA){
3088 115405 s->mv_dir = 0;
3089 115405 s->mv_type = MV_TYPE_16X16;
3090 115405 s->mb_intra= 1;
3091 115405 s->mv[0][0][0] = 0;
3092 115405 s->mv[0][0][1] = 0;
3093 115405 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTRA, pb, pb2, tex_pb,
3094 &dmin, &next_block, 0, 0);
3095
3/4
✓ Branch 0 taken 38212 times.
✓ Branch 1 taken 77193 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 38212 times.
115405 if(s->h263_pred || s->h263_aic){
3096
2/2
✓ Branch 0 taken 19644 times.
✓ Branch 1 taken 57549 times.
77193 if(best_s.mb_intra)
3097 19644 s->mbintra_table[mb_x + mb_y*s->mb_stride]=1;
3098 else
3099 57549 ff_clean_intra_table_entries(s); //old mode?
3100 }
3101 }
3102
3103
4/4
✓ Branch 0 taken 179700 times.
✓ Branch 1 taken 352192 times.
✓ Branch 2 taken 179660 times.
✓ Branch 3 taken 40 times.
531892 if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
3104
2/2
✓ Branch 0 taken 161759 times.
✓ Branch 1 taken 17901 times.
179660 if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
3105 161759 const int last_qp= backup_s.qscale;
3106 int qpi, qp, dc[6];
3107 int16_t ac[6][16];
3108 161759 const int mvdir= (best_s.mv_dir&MV_DIR_BACKWARD) ? 1 : 0;
3109 static const int dquant_tab[4]={-1,1,-2,2};
3110
4/4
✓ Branch 0 taken 32407 times.
✓ Branch 1 taken 129352 times.
✓ Branch 2 taken 10117 times.
✓ Branch 3 taken 22290 times.
161759 int storecoefs = s->mb_intra && s->dc_val[0];
3111
3112 av_assert2(backup_s.dquant == 0);
3113
3114 //FIXME intra
3115 161759 s->mv_dir= best_s.mv_dir;
3116 161759 s->mv_type = MV_TYPE_16X16;
3117 161759 s->mb_intra= best_s.mb_intra;
3118 161759 s->mv[0][0][0] = best_s.mv[0][0][0];
3119 161759 s->mv[0][0][1] = best_s.mv[0][0][1];
3120 161759 s->mv[1][0][0] = best_s.mv[1][0][0];
3121 161759 s->mv[1][0][1] = best_s.mv[1][0][1];
3122
3123
2/2
✓ Branch 0 taken 107866 times.
✓ Branch 1 taken 53893 times.
161759 qpi = s->pict_type == AV_PICTURE_TYPE_B ? 2 : 0;
3124
2/2
✓ Branch 0 taken 431304 times.
✓ Branch 1 taken 161759 times.
593063 for(; qpi<4; qpi++){
3125 431304 int dquant= dquant_tab[qpi];
3126 431304 qp= last_qp + dquant;
3127
4/4
✓ Branch 0 taken 395441 times.
✓ Branch 1 taken 35863 times.
✓ Branch 2 taken 5308 times.
✓ Branch 3 taken 390133 times.
431304 if(qp < s->avctx->qmin || qp > s->avctx->qmax)
3128 41171 continue;
3129 390133 backup_s.dquant= dquant;
3130
2/2
✓ Branch 0 taken 37374 times.
✓ Branch 1 taken 352759 times.
390133 if(storecoefs){
3131
2/2
✓ Branch 0 taken 224244 times.
✓ Branch 1 taken 37374 times.
261618 for(i=0; i<6; i++){
3132 224244 dc[i]= s->dc_val[0][ s->block_index[i] ];
3133 224244 memcpy(ac[i], s->ac_val[0][s->block_index[i]], sizeof(int16_t)*16);
3134 }
3135 }
3136
3137 390133 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3138 &dmin, &next_block, s->mv[mvdir][0][0], s->mv[mvdir][0][1]);
3139
2/2
✓ Branch 0 taken 357862 times.
✓ Branch 1 taken 32271 times.
390133 if(best_s.qscale != qp){
3140
2/2
✓ Branch 0 taken 33686 times.
✓ Branch 1 taken 324176 times.
357862 if(storecoefs){
3141
2/2
✓ Branch 0 taken 202116 times.
✓ Branch 1 taken 33686 times.
235802 for(i=0; i<6; i++){
3142 202116 s->dc_val[0][ s->block_index[i] ]= dc[i];
3143 202116 memcpy(s->ac_val[0][s->block_index[i]], ac[i], sizeof(int16_t)*16);
3144 }
3145 }
3146 }
3147 }
3148 }
3149 }
3150
2/2
✓ Branch 0 taken 153046 times.
✓ Branch 1 taken 378846 times.
531892 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT){
3151 153046 int mx= s->b_direct_mv_table[xy][0];
3152 153046 int my= s->b_direct_mv_table[xy][1];
3153
3154 153046 backup_s.dquant = 0;
3155 153046 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3156 153046 s->mb_intra= 0;
3157 153046 ff_mpeg4_set_direct_mv(s, mx, my);
3158 153046 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3159 &dmin, &next_block, mx, my);
3160 }
3161
2/2
✓ Branch 0 taken 47505 times.
✓ Branch 1 taken 484387 times.
531892 if(CONFIG_MPEG4_ENCODER && mb_type&CANDIDATE_MB_TYPE_DIRECT0){
3162 47505 backup_s.dquant = 0;
3163 47505 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT;
3164 47505 s->mb_intra= 0;
3165 47505 ff_mpeg4_set_direct_mv(s, 0, 0);
3166 47505 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
3167 &dmin, &next_block, 0, 0);
3168 }
3169
3/4
✓ Branch 0 taken 493277 times.
✓ Branch 1 taken 38615 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 493277 times.
531892 if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
3170 int coded=0;
3171 for(i=0; i<6; i++)
3172 coded |= s->block_last_index[i];
3173 if(coded){
3174 int mx,my;
3175 memcpy(s->mv, best_s.mv, sizeof(s->mv));
3176 if(CONFIG_MPEG4_ENCODER && best_s.mv_dir & MV_DIRECT){
3177 mx=my=0; //FIXME find the one we actually used
3178 ff_mpeg4_set_direct_mv(s, mx, my);
3179 }else if(best_s.mv_dir&MV_DIR_BACKWARD){
3180 mx= s->mv[1][0][0];
3181 my= s->mv[1][0][1];
3182 }else{
3183 mx= s->mv[0][0][0];
3184 my= s->mv[0][0][1];
3185 }
3186
3187 s->mv_dir= best_s.mv_dir;
3188 s->mv_type = best_s.mv_type;
3189 s->mb_intra= 0;
3190 /* s->mv[0][0][0] = best_s.mv[0][0][0];
3191 s->mv[0][0][1] = best_s.mv[0][0][1];
3192 s->mv[1][0][0] = best_s.mv[1][0][0];
3193 s->mv[1][0][1] = best_s.mv[1][0][1];*/
3194 backup_s.dquant= 0;
3195 s->skipdct=1;
3196 encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_INTER /* wrong but unused */, pb, pb2, tex_pb,
3197 &dmin, &next_block, mx, my);
3198 s->skipdct=0;
3199 }
3200 }
3201
3202 531892 s->current_picture.qscale_table[xy] = best_s.qscale;
3203
3204 531892 copy_context_after_encode(s, &best_s, -1);
3205
3206 531892 pb_bits_count= put_bits_count(&s->pb);
3207 531892 flush_put_bits(&s->pb);
3208 531892 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3209 531892 s->pb= backup_s.pb;
3210
3211
2/2
✓ Branch 0 taken 141085 times.
✓ Branch 1 taken 390807 times.
531892 if(s->data_partitioning){
3212 141085 pb2_bits_count= put_bits_count(&s->pb2);
3213 141085 flush_put_bits(&s->pb2);
3214 141085 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3215 141085 s->pb2= backup_s.pb2;
3216
3217 141085 tex_pb_bits_count= put_bits_count(&s->tex_pb);
3218 141085 flush_put_bits(&s->tex_pb);
3219 141085 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3220 141085 s->tex_pb= backup_s.tex_pb;
3221 }
3222 531892 s->last_bits= put_bits_count(&s->pb);
3223
3224 531892 if (CONFIG_H263_ENCODER &&
3225
4/4
✓ Branch 0 taken 397164 times.
✓ Branch 1 taken 134728 times.
✓ Branch 2 taken 243978 times.
✓ Branch 3 taken 153186 times.
531892 s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
3226 243978 ff_h263_update_motion_val(s);
3227
3228
2/2
✓ Branch 0 taken 225272 times.
✓ Branch 1 taken 306620 times.
531892 if(next_block==0){ //FIXME 16 vs linesize16
3229 225272 s->hdsp.put_pixels_tab[0][0](s->dest[0], s->sc.rd_scratchpad , s->linesize ,16);
3230 225272 s->hdsp.put_pixels_tab[1][0](s->dest[1], s->sc.rd_scratchpad + 16*s->linesize , s->uvlinesize, 8);
3231 225272 s->hdsp.put_pixels_tab[1][0](s->dest[2], s->sc.rd_scratchpad + 16*s->linesize + 8, s->uvlinesize, 8);
3232 }
3233
3234
2/2
✓ Branch 0 taken 141006 times.
✓ Branch 1 taken 390886 times.
531892 if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
3235 141006 ff_mpv_reconstruct_mb(s, s->block);
3236 } else {
3237 2642231 int motion_x = 0, motion_y = 0;
3238 2642231 s->mv_type=MV_TYPE_16X16;
3239 // only one MB-Type possible
3240
3241
10/13
✓ Branch 0 taken 1002170 times.
✓ Branch 1 taken 1452188 times.
✓ Branch 2 taken 9451 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15807 times.
✓ Branch 5 taken 78 times.
✓ Branch 6 taken 101729 times.
✓ Branch 7 taken 33452 times.
✓ Branch 8 taken 22694 times.
✓ Branch 9 taken 2174 times.
✓ Branch 10 taken 2488 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
2642231 switch(mb_type){
3242 1002170 case CANDIDATE_MB_TYPE_INTRA:
3243 1002170 s->mv_dir = 0;
3244 1002170 s->mb_intra= 1;
3245 1002170 motion_x= s->mv[0][0][0] = 0;
3246 1002170 motion_y= s->mv[0][0][1] = 0;
3247 1002170 break;
3248 1452188 case CANDIDATE_MB_TYPE_INTER:
3249 1452188 s->mv_dir = MV_DIR_FORWARD;
3250 1452188 s->mb_intra= 0;
3251 1452188 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0];
3252 1452188 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1];
3253 1452188 break;
3254 9451 case CANDIDATE_MB_TYPE_INTER_I:
3255 9451 s->mv_dir = MV_DIR_FORWARD;
3256 9451 s->mv_type = MV_TYPE_FIELD;
3257 9451 s->mb_intra= 0;
3258
2/2
✓ Branch 0 taken 18902 times.
✓ Branch 1 taken 9451 times.
28353 for(i=0; i<2; i++){
3259 18902 j= s->field_select[0][i] = s->p_field_select_table[i][xy];
3260 18902 s->mv[0][i][0] = s->p_field_mv_table[i][j][xy][0];
3261 18902 s->mv[0][i][1] = s->p_field_mv_table[i][j][xy][1];
3262 }
3263 9451 break;
3264 case CANDIDATE_MB_TYPE_INTER4V:
3265 s->mv_dir = MV_DIR_FORWARD;
3266 s->mv_type = MV_TYPE_8X8;
3267 s->mb_intra= 0;
3268 for(i=0; i<4; i++){
3269 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0];
3270 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1];
3271 }
3272 break;
3273 15807 case CANDIDATE_MB_TYPE_DIRECT:
3274 if (CONFIG_MPEG4_ENCODER) {
3275 15807 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3276 15807 s->mb_intra= 0;
3277 15807 motion_x=s->b_direct_mv_table[xy][0];
3278 15807 motion_y=s->b_direct_mv_table[xy][1];
3279 15807 ff_mpeg4_set_direct_mv(s, motion_x, motion_y);
3280 }
3281 15807 break;
3282 78 case CANDIDATE_MB_TYPE_DIRECT0:
3283 if (CONFIG_MPEG4_ENCODER) {
3284 78 s->mv_dir = MV_DIR_FORWARD|MV_DIR_BACKWARD|MV_DIRECT;
3285 78 s->mb_intra= 0;
3286 78 ff_mpeg4_set_direct_mv(s, 0, 0);
3287 }
3288 78 break;
3289 101729 case CANDIDATE_MB_TYPE_BIDIR:
3290 101729 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
3291 101729 s->mb_intra= 0;
3292 101729 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0];
3293 101729 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1];
3294 101729 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0];
3295 101729 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1];
3296 101729 break;
3297 33452 case CANDIDATE_MB_TYPE_BACKWARD:
3298 33452 s->mv_dir = MV_DIR_BACKWARD;
3299 33452 s->mb_intra= 0;
3300 33452 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0];
3301 33452 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1];
3302 33452 break;
3303 22694 case CANDIDATE_MB_TYPE_FORWARD:
3304 22694 s->mv_dir = MV_DIR_FORWARD;
3305 22694 s->mb_intra= 0;
3306 22694 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
3307 22694 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
3308 22694 break;
3309 2174 case CANDIDATE_MB_TYPE_FORWARD_I: