FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2022-07-07 01:21:54
Exec Total Coverage
Lines: 1928 2788 69.2%
Branches: 1120 1818 61.6%

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