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