FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mpegvideo_enc.c
Date: 2024-04-15 22:47:37
Exec Total Coverage
Lines: 2018 2869 70.3%
Functions: 40 52 76.9%
Branches: 1164 1858 62.6%

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