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