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