FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 1963 2833 69.3%
Branches: 1147 1858 61.7%

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