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