34#include "config_components.h"
80#define QUANT_BIAS_SHIFT 8
82#define QMAT_SHIFT_MMX 16
89 int16_t *
block,
int n,
90 int qscale,
int *overflow);
102 .class_name =
"generic mpegvideo encoder",
109 uint16_t (*qmat16)[2][64],
110 const uint16_t *quant_matrix,
111 int bias,
int qmin,
int qmax,
int intra)
117 for (qscale = qmin; qscale <= qmax; qscale++) {
122 else qscale2 = qscale << 1;
129 for (
i = 0;
i < 64;
i++) {
130 const int j =
s->c.idsp.idct_permutation[
i];
138 qmat[qscale][
i] = (int)((UINT64_C(2) <<
QMAT_SHIFT) / den);
141 for (
i = 0;
i < 64;
i++) {
142 const int j =
s->c.idsp.idct_permutation[
i];
150 qmat[qscale][
i] = (int)((UINT64_C(2) << (
QMAT_SHIFT + 14)) / den);
153 for (
i = 0;
i < 64;
i++) {
154 const int j =
s->c.idsp.idct_permutation[
i];
166 qmat[qscale][
i] = (int)((UINT64_C(2) <<
QMAT_SHIFT) / den);
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] =
174 qmat16[qscale][0][
i]);
178 for (
i = intra;
i < 64;
i++) {
183 while (((
max * qmat[qscale][
i]) >>
shift) > INT_MAX) {
190 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
199 if (
s->c.q_scale_type == 1 && 0) {
201 int bestdiff=INT_MAX;
209 if (
diff < bestdiff) {
231 for (
i = 0;
i < 64;
i++) {
243 int8_t *
const qscale_table =
s->c.cur_pic.qscale_table;
245 for (
int i = 0;
i <
s->c.mb_num;
i++) {
246 unsigned int lam =
s->lambda_table[
s->c.mb_index2xy[
i]];
248 qscale_table[
s->c.mb_index2xy[
i]] =
av_clip(qp,
s->c.avctx->qmin,
256#define COPY(a) dst->a = src->a
263 COPY(
c.frame_pred_frame_dct);
264 COPY(
c.progressive_frame);
265 COPY(partitioned_frame);
271 for (
int i = -16;
i < 16;
i++)
292 if (!
s->c.y_dc_scale_table) {
293 s->c.y_dc_scale_table =
306 if (
s->c.avctx->trellis)
349 if (!me_cmp[0] || !me_cmp[4])
351 s->ildct_cmp[0] = me_cmp[0];
352 s->ildct_cmp[1] = me_cmp[4];
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];
362 s->n_sse_cmp[0] = mecc.
nsse[0];
363 s->n_sse_cmp[1] = mecc.
nsse[1];
365 s->n_sse_cmp[0] = mecc.
sse[0];
366 s->n_sse_cmp[1] = mecc.
sse[1];
372#define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
377 const uint16_t *intra_matrix, *inter_matrix;
385 s->q_chroma_intra_matrix =
s->q_intra_matrix + 32;
386 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16 + 32;
392 s->q_chroma_intra_matrix =
s->q_intra_matrix;
393 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
396 s->q_inter_matrix =
s->q_intra_matrix + 32;
397 s->q_inter_matrix16 =
s->q_intra_matrix16 + 32;
418 for (
int i = 0;
i < 64;
i++) {
419 int j =
s->c.idsp.idct_permutation[
i];
421 s->c.intra_matrix[j] =
s->c.chroma_intra_matrix[j] = intra_matrix[
i];
422 s->c.inter_matrix[j] = inter_matrix[
i];
431 s->c.intra_matrix,
s->intra_quant_bias, avctx->
qmin,
433 if (
s->q_inter_matrix)
435 s->c.inter_matrix,
s->inter_quant_bias, avctx->
qmin,
445 int16_t (*mv_table)[2];
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));
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);
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;
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])
467 s->p_field_select_table[1] =
s->p_field_select_table[0] + 2 * mv_table_size;
470 mv_table =
av_calloc(mv_table_size, nb_mv_tables *
sizeof(*mv_table));
474 mv_table +=
s->c.mb_stride + 1;
476 s->p_mv_table = mv_table;
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;
484 if (
s->p_field_select_table[1]) {
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;
510 DCT_ERROR_SIZE =
FFALIGN(2 *
sizeof(*
s->dct_error_sum),
ALIGN),
513 "Need checks for potential overflow.");
514 unsigned nb_slices =
s->c.slice_context_count;
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;
532 for (
unsigned i = 0;
i < nb_slices; ++
i) {
535 s2->
block = s2->blocks[0];
582 s->c.width = avctx->
width;
587 "keyframe interval too large!, reducing it from %d to %d\n",
599 "max b frames must be 0 or positive for mpegvideo based encoders\n");
610 s->rtp_mode = !!
s->rtp_payload_size;
661 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
667 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
684 "impossible bitrate constraints, this will fail\n");
700 if (nbt <= INT_MAX) {
715 "OBMC is only supported with simple mb decision\n");
730 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
738 (avctx->
width > 2048 ||
744 (avctx->
width > 65535 ||
745 avctx->
height > 65535 )) {
752 ((avctx->
width &3) ||
785 "closed gop with scene change detection are not supported yet, "
786 "set threshold to 1000000000\n");
794 "low delay forcing is only available for mpeg2, "
795 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
800 "B-frames cannot be used with low delay\n");
813 "notice: b_frame_strategy only affects the first pass\n");
828 s->inter_quant_bias = 0;
830 s->intra_quant_bias = 0;
836 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
840 av_log(avctx,
AV_LOG_DEBUG,
"intra_quant_bias = %d inter_quant_bias = %d\n",
s->intra_quant_bias,
s->inter_quant_bias);
843#if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
853#if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
861 s->c.y_dc_scale_table =
879 if (!CONFIG_H263_ENCODER)
882 s->c.width,
s->c.height) == 8) {
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);
898 s->modified_quant =
s->c.h263_aic;
900 s->me.unrestricted_mv =
s->c.obmc ||
s->loop_filter ||
s->umvplus;
901 s->flipflop_rounding = 1;
910 s->me.unrestricted_mv = 1;
915#if CONFIG_RV10_ENCODER
922#if CONFIG_RV20_ENCODER
928 s->modified_quant = 1;
933 s->me.unrestricted_mv = 0;
939 s->me.unrestricted_mv = 1;
940 s->flipflop_rounding = 1;
947 s->me.unrestricted_mv = 1;
948 s->c.msmpeg4_version = MSMP4_V2;
955 s->me.unrestricted_mv = 1;
956 s->c.msmpeg4_version = MSMP4_V3;
957 s->flipflop_rounding = 1;
964 s->me.unrestricted_mv = 1;
965 s->c.msmpeg4_version = MSMP4_WMV1;
966 s->flipflop_rounding = 1;
973 s->me.unrestricted_mv = 1;
974 s->c.msmpeg4_version = MSMP4_WMV2;
975 s->flipflop_rounding = 1;
980 av_unreachable(
"List contains all codecs using ff_mpv_encode_init()");
987 s->c.progressive_frame =
990 s->c.alternate_scan);
1001 s->frame_reconstruction_bitfield = 0;
1033 if (CONFIG_H263_ENCODER &&
s->c.out_format ==
FMT_H263) {
1035#if CONFIG_MSMPEG4ENC
1036 if (
s->c.msmpeg4_version != MSMP4_UNUSED)
1041 s->c.slice_ctx_size =
sizeof(*s);
1048 if (
s->c.slice_context_count > 1) {
1051 s->h263_slice_structured = 1;
1129 int16_t *
block,
int i, uint8_t *dest,
int line_size,
int qscale)
1131 if (
s->skipdct & (1U <<
i))
1133 s->c.dct_unquantize_intra(&
s->c,
block,
i, qscale);
1134 s->c.idsp.idct_put(dest, line_size,
block);
1138 int16_t *
block,
int i, uint8_t *dest,
int line_size,
int qscale)
1140 if (
s->c.block_last_index[
i] >= 0) {
1141 s->c.dct_unquantize_inter(&
s->c,
block,
i, qscale);
1143 s->c.idsp.idct_add(dest, line_size,
block);
1150 return pair * row_offset;
1151 return (pair >> 1) * row_offset + (pair & 1) * 8;
1162 for (
int i = 0;
i < 6;
i++) {
1163 if (
s->c.mb_intra && (
s->skipdct & (1U <<
i)))
1165 for (
int j = 0; j < 64; j++) {
1167 block[
i][
s->c.idsp.idct_permutation[j]]);
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;
1180 dct_linesize = linesize <<
s->c.interlaced_dct;
1181 dct_offset =
s->c.interlaced_dct ? linesize : linesize * block_size;
1183 if (!
s->c.mb_intra) {
1191 if (
s->c.chroma_y_shift) {
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);
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++) {
1220 uv_dct_linesize,
s->c.chroma_qscale);
1222 uv_dct_linesize,
s->c.chroma_qscale);
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);
1242 for (y = 0; y < 16; y++) {
1243 for (x = 0; x < 16; x++) {
1257 w =
s->c.width & ~15;
1258 h =
s->c.height & ~15;
1260 for (y = 0; y <
h; y += 16) {
1261 for (x = 0; x <
w; x += 16) {
1268 acc += sae + 500 < sad;
1294 for (
int i = 0;
f->data[
i];
i++) {
1315 int display_picture_number = 0,
ret;
1317 : (
s->c.low_delay ? 0 : 1);
1318 int flush_offset = 1;
1329 (
pts > INT64_MAX / 2 /
s->c.avctx->time_base.num ||
1340 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1345 if (!
s->c.low_delay && display_picture_number == 1)
1354 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1357 pts = display_picture_number;
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)
1365 if ((
s->c.width & 15) || (
s->c.height & 15))
1373 pic_arg->
linesize[1],
s->c.linesize,
s->c.uvlinesize);
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;
1395 const uint8_t *
src = pic_arg->
data[
i];
1399 && !
s->c.progressive_sequence
1400 &&
FFALIGN(
s->c.height, 32) -
s->c.height > 16)
1403 if (!
s->c.avctx->rc_buffer_size)
1407 if (src_stride == dst_stride)
1408 memcpy(
dst,
src, src_stride *
h - src_stride +
w);
1411 uint8_t *dst2 =
dst;
1413 memcpy(dst2,
src,
w);
1418 if ((
s->c.width & 15) || (
s->c.height & (vpad-1))) {
1419 s->mpvencdsp.draw_edges(
dst, dst_stride,
1436 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1440 encoding_delay -= flush_offset - 1;
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);
1474 case 0: score =
FFMAX(score, v);
break;
1475 case 1: score +=
FFABS(v);
break;
1476 case 2: score64 += v * (
int64_t)v;
break;
1487 score64 = pow(score64 / (
double)(
s->c.mb_width *
s->c.mb_height),
1490 if (score64 < m->frame_skip_threshold)
1525 int out_size, p_lambda, b_lambda, lambda2;
1527 int best_b_count = -1;
1540 b_lambda = p_lambda;
1548 if (pre_input_ptr) {
1549 const AVFrame *
const pre_input = pre_input_ptr->
f;
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;
1591 c->time_base =
s->c.avctx->time_base;
1634 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1652 return best_b_count;
1674 s->c.next_pic.ptr &&
1726 for (
int i = 0;;
i++) {
1731 b_frames =
FFMAX(0,
i - 1);
1737 for (
int i = 0;
i < b_frames + 1;
i++)
1750 s->c.last_non_b_time > UINT16_MAX)
1753 for (
int i = b_frames - 1;
i >= 0;
i--) {
1761 "warning, too many B-frames in a row\n");
1785 for (
int i = 0;
i < b_frames;
i++) {
1821 (
s->mjpeg_ctx && !
s->c.chroma_y_shift &&
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);
1847 &
s->c.sc, &
s->c.buffer_pools,
s->c.mb_height);
1852 s->picture_number =
s->c.cur_pic.ptr->display_picture_number;
1865 if (
s->me.unrestricted_mv &&
1866 s->c.cur_pic.reference &&
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,
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,
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;
1907 s->dct_count[intra] >>= 1;
1910 for (
i = 0;
i < 64;
i++) {
1912 s->dct_count[intra] +
1913 s->dct_error_sum[intra][
i] / 2) /
1914 (
s->dct_error_sum[intra][
i] + 1);
1923 s->c.cur_pic.ptr->f->pict_type =
s->c.pict_type;
1931 if (
s->dct_error_sum) {
1937 const AVFrame *pic_arg,
int *got_packet)
1941 int stuffing_count,
ret;
1942 int seq_end = !pic_arg && m->
seq_end;
1943 int context_count =
s->c.slice_context_count;
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 *
1982 s->c.mb_width*
s->c.mb_height*12);
1983 if (!
s->mb_info_ptr)
1985 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1988 s->c.pict_type =
s->new_pic->pict_type;
1992 if (growing_buffer) {
1994 pkt->data =
s->pb.buf;
2002 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
s->c.out_format ==
FMT_MJPEG)
2012 s->lambda < m->
lmax) {
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) /
2022 s->c.mb_skipped = 0;
2025 s->c.no_rounding ^=
s->flipflop_rounding;
2028 s->c.time_base =
s->c.last_time_base;
2029 s->c.last_non_b_time =
s->c.time -
s->c.pp_time;
2043 avctx->
error[
i] +=
s->encoding_error[
i];
2051 s->misc_bits +
s->i_tex_bits +
2059 if (stuffing_count) {
2065 switch (
s->c.codec_id) {
2068 while (stuffing_count--) {
2075 stuffing_count -= 4;
2076 while (stuffing_count--) {
2103 int vbv_delay, min_delay;
2113 "Internal error, negative bits\n");
2118 min_delay = (minbits * 90000LL + avctx->
rc_max_rate - 1) /
2121 vbv_delay =
FFMAX(vbv_delay, min_delay);
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;
2137 (uint8_t*)props, props_size);
2145 pkt->pts =
s->c.cur_pic.ptr->f->pts;
2146 pkt->duration =
s->c.cur_pic.ptr->f->duration;
2148 if (!
s->c.cur_pic.ptr->coded_picture_number)
2176 *got_packet = !!
pkt->size;
2181 int n,
int threshold)
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
2196 int16_t *
block =
s->block[n];
2197 const int last_index =
s->c.block_last_index[n];
2200 if (threshold < 0) {
2202 threshold = -threshold;
2207 if (last_index <= skip_dc - 1)
2210 for (
i = 0;
i <= last_index;
i++) {
2211 const int j =
s->c.intra_scantable.permutated[
i];
2214 if (skip_dc &&
i == 0)
2218 }
else if (
level > 1) {
2224 if (score >= threshold)
2226 for (
i = skip_dc;
i <= last_index;
i++) {
2227 const int j =
s->c.intra_scantable.permutated[
i];
2231 s->c.block_last_index[n] = 0;
2233 s->c.block_last_index[n] = -1;
2240 const int maxlevel =
s->max_qcoeff;
2241 const int minlevel =
s->min_qcoeff;
2244 if (
s->c.mb_intra) {
2249 for (;
i <= last_index;
i++) {
2250 const int j =
s->c.intra_scantable.permutated[
i];
2253 if (
level > maxlevel) {
2256 }
else if (
level < minlevel) {
2266 "warning, clipping %d dct coefficients to %d..%d\n",
2267 overflow, minlevel, maxlevel);
2274 for (y = 0; y < 8; y++) {
2275 for (x = 0; x < 8; x++) {
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];
2295 const int *skip_dct,
int first,
int last)
2297 for (
int i = first;
i < last;
i++) {
2300 s->c.block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->c.qscale, &overflow);
2309 s->c.block_last_index[
i] = -1;
2314 int motion_x,
int motion_y,
2315 int mb_block_height,
2324#define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2325 (s)->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2327 int16_t orig[12][64];
2328 const int mb_x =
s->c.mb_x;
2329 const int mb_y =
s->c.mb_y;
2332 int dct_offset =
s->c.linesize * 8;
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;
2337 for (
i = 0;
i < mb_block_count;
i++)
2338 skip_dct[
i] =
s->skipdct & (1U <<
i);
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;
2344 s->lambda =
s->lambda_table[mb_xy];
2349 s->dquant =
s->c.cur_pic.qscale_table[mb_xy] - last_qp;
2355 if (!
s->c.mb_intra) {
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;
2379 if ((mb_x * 16 + 16 >
s->c.width || mb_y * 16 + 16 >
s->c.height) &&
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,
2386 16, 16, mb_x * 16, mb_y * 16,
2387 s->c.width,
s->c.height);
2389 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2391 mb_block_width, mb_block_height,
2392 mb_x * mb_block_width, mb_y * mb_block_height,
2394 ptr_cb = ebuf + 16 * wrap_y;
2395 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2397 mb_block_width, mb_block_height,
2398 mb_x * mb_block_width, mb_y * mb_block_height,
2400 ptr_cr = ebuf + 16 * wrap_y + 16;
2403 if (
s->c.mb_intra) {
2405 int progressive_score, interlaced_score;
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;
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;
2420 dct_offset = wrap_y;
2421 uv_dct_offset = wrap_c;
2430 for (
i = 0;
i < 4;
i++) {
2432 s->pdsp.get_pixels(
s->block[
i],
2433 ptr_y + (
i & 1) * 8 + (
i >> 1) * dct_offset,
2441 for (
i = 4;
i < mb_block_count;
i += 2) {
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);
2453 uint8_t *dest_y, *dest_cb, *dest_cr;
2455 dest_y =
s->c.dest[0];
2456 dest_cb =
s->c.dest[1];
2457 dest_cr =
s->c.dest[2];
2460 op_pix =
s->c.hdsp.put_pixels_tab;
2461 op_qpix =
s->c.qdsp.put_qpel_pixels_tab;
2463 op_pix =
s->c.hdsp.put_no_rnd_pixels_tab;
2464 op_qpix =
s->c.qdsp.put_no_rnd_qpel_pixels_tab;
2471 op_pix =
s->c.hdsp.avg_pixels_tab;
2472 op_qpix =
s->c.qdsp.avg_qpel_pixels_tab;
2481 int progressive_score, interlaced_score;
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,
2490 progressive_score -= 400;
2492 if (progressive_score > 0) {
2493 interlaced_score =
s->ildct_cmp[0](
s, dest_y, ptr_y,
2495 s->ildct_cmp[0](
s, dest_y + wrap_y,
2499 if (progressive_score > interlaced_score) {
2500 s->c.interlaced_dct = 1;
2502 dct_offset = wrap_y;
2503 uv_dct_offset = wrap_c;
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);
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) {
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);
2532 if (
s->mc_mb_var[
s->c.mb_stride * mb_y + mb_x] < 2 *
s->c.qscale *
s->c.qscale) {
2534 if (
s->sad_cmp[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->c.qscale)
2536 if (
s->sad_cmp[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->c.qscale)
2538 if (
s->sad_cmp[1](
NULL, ptr_y + dct_offset, dest_y + dct_offset,
2539 wrap_y, 8) < 20 *
s->c.qscale)
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)
2544 if (
s->sad_cmp[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->c.qscale)
2546 if (
s->sad_cmp[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->c.qscale)
2548 if (!chroma_y_shift) {
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)
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)
2561 if (
s->quantizer_noise_shaping) {
2570 for (
i = 4;
i < mb_block_count;
i++) {
2577 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
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));
2589 first = 4 + bottom * chroma_blocks;
2594 if (
s->quantizer_noise_shaping) {
2595 for (
i = 0;
i < mb_block_count;
i++) {
2597 s->c.block_last_index[
i] =
2599 orig[
i],
i,
s->c.qscale);
2604 if (
s->luma_elim_threshold && !
s->c.mb_intra)
2605 for (
i = 0;
i < 4;
i++)
2607 if (
s->chroma_elim_threshold && !
s->c.mb_intra)
2608 for (
i = 4;
i < mb_block_count;
i++)
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;
2620 s->c.block_last_index[4] =
2621 s->c.block_last_index[5] = 0;
2623 s->block[5][0] = (1024 +
s->c.c_dc_scale / 2) /
s->c.c_dc_scale;
2624 if (!chroma_y_shift) {
2625 for (
i=6;
i<12;
i++) {
2626 s->c.block_last_index[
i] = 0;
2627 s->block[
i][0] =
s->block[4][0];
2634 for (
i = 0;
i < mb_block_count;
i++) {
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]])
2641 s->c.block_last_index[
i] = j;
2646 s->encode_mb(
s,
s->block, motion_x, motion_y);
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) \
2683 memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
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]; \
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; \
2698 d->c.mb_skipped = 0; \
2699 d->c.qscale = s->c.qscale; \
2700 d->dquant = s->dquant; \
2702 d->esc3_level_length = s->esc3_level_length; \
2705static inline void AFTER ## _context_after_encode(DST_TYPE *const d, \
2706 const SRC_TYPE *const s, \
2707 int data_partitioning) \
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)); \
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]; \
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; \
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; \
2730 if (data_partitioning) { \
2732 d->tex_pb = s->tex_pb; \
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; \
2740 d->esc3_level_length = s->esc3_level_length; \
2748 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2751 uint8_t *dest_backup[3];
2753 reset_context_before_encode(
s, backup);
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];
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;
2773 if (
s->data_partitioning) {
2781 score *=
s->lambda2;
2786 memcpy(
s->c.dest, dest_backup,
sizeof(
s->c.dest));
2793 save_context_after_encode(best,
s,
s->data_partitioning);
2805 else if (
w == 16 &&
h == 8)
2808 else if(
w==8 &&
h==8)
2826 int chroma_mb_w =
w >>
s->c.chroma_x_shift;
2827 int chroma_mb_h =
h >>
s->c.chroma_y_shift;
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;
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);
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);
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--)
2858 s->c.first_slice_line = 0;
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++) {
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;
2886 s->c.first_slice_line = 0;
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++) {
2898 const uint8_t *
pix =
s->new_pic->data[0] + (yy *
s->c.linesize) + xx;
2900 int sum =
s->mpvencdsp.pix_sum(
pix,
s->c.linesize);
2902 varc = (
s->mpvencdsp.pix_norm1(
pix,
s->c.linesize) -
2903 (((
unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
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;
2919 if (
s->partitioned_frame)
2923 }
else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2926 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->c.out_format ==
FMT_SPEEDHQ) {
2938 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
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;
2943 if (CONFIG_H263_ENCODER)
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);
2950 bytestream_put_byte(&ptr, pred_y);
2952 bytestream_put_byte(&ptr, 0);
2953 bytestream_put_byte(&ptr, 0);
2961 s->mb_info_size += 12;
2962 s->prev_mb_info =
s->last_mb_info;
2966 if (!
s->mb_info_size)
2967 s->mb_info_size += 12;
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;
2978 uint8_t *new_buffer =
NULL;
2979 int new_buffer_size = 0;
2981 if ((
s->c.avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2987 s->c.avctx->internal->byte_buffer_size + size_increase);
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;
2996 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
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;
3008 int chr_h = 16 >>
s->c.chroma_y_shift;
3033 s->last_dc[
i] = 128 <<
s->c.intra_dc_precision;
3035 s->encoding_error[
i] = 0;
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) {
3048 memset(
s->c.last_mv, 0,
sizeof(
s->c.last_mv));
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;
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;
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;
3068 mb_y = mb_y_order / row_passes;
3070 const int has_bottom = row_passes == 2 && 16 * mb_y + 8 <
s->c.height;
3072 if (pass && !has_bottom)
3076 s->skipdct = row_passes == 1 ? 0
3078 ? (pass ? 0x033 : 0x0cc) : (pass ? 0x0f3 : 0xf0c);
3085 for (
int mb_x = 0; mb_x <
s->c.mb_width; mb_x++) {
3090 int size_increase =
s->c.avctx->internal->byte_buffer_size/4
3098 if (
s->data_partitioning) {
3112 xy =
s->c.mb_y *
s->c.mb_stride +
s->c.mb_x;
3113 mb_type =
s->mb_type[xy];
3116 if (
s->rtp_mode && !pass) {
3117 int current_packet_size, is_gob_start;
3120 - (
s->ptr_lastgob -
s->pb.buf);
3122 is_gob_start =
s->rtp_payload_size &&
3123 current_packet_size >=
s->rtp_payload_size &&
3126 if (
s->c.start_mb_y == mb_y && mb_y > 0 && mb_x == 0) is_gob_start = 1;
3128 switch (
s->c.codec_id) {
3131 if (!
s->h263_slice_structured)
3132 if (
s->c.mb_x ||
s->c.mb_y %
s->gob_index) is_gob_start = 0;
3135 if (
s->c.mb_x == 0 &&
s->c.mb_y != 0) is_gob_start = 1;
3143 if (
s->c.mb_x == 0 &&
s->c.mb_y != 0) is_gob_start = 1;
3148 if (
s->c.start_mb_y != mb_y || mb_x != 0) {
3158 if (
s->error_rate &&
s->c.resync_mb_x +
s->c.resync_mb_y > 0) {
3160 int d = 100 /
s->error_rate;
3162 current_packet_size=0;
3163 s->pb.buf_ptr=
s->ptr_lastgob;
3168 switch (
s->c.codec_id) {
3170 if (CONFIG_MPEG4_ENCODER) {
3178 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3183#if CONFIG_H263P_ENCODER
3190 if (CONFIG_H263_ENCODER) {
3192 s->mb_info_size += 12;
3202 s->misc_bits+=
bits -
s->last_bits;
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;
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;
3217 s->c.mb_skipped = 0;
3224 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3226 backup_context_before_encode(&backup_s,
s);
3228 if (
s->data_partitioning) {
3229 backup_s.pb2=
s->pb2;
3230 backup_s.tex_pb=
s->tex_pb;
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];
3240 &dmin, &next_block,
s->c.mv[0][0][0],
s->c.mv[0][0][1]);
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];
3252 &dmin, &next_block, 0, 0);
3258 s->c.mv[0][0][0] = 0;
3259 s->c.mv[0][0][1] = 0;
3261 &dmin, &next_block,
s->c.mv[0][0][0],
s->c.mv[0][0][1]);
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];
3272 &dmin, &next_block, 0, 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];
3281 &dmin, &next_block,
s->c.mv[0][0][0],
s->c.mv[0][0][1]);
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];
3290 &dmin, &next_block,
s->c.mv[1][0][0],
s->c.mv[1][0][1]);
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];
3301 &dmin, &next_block, 0, 0);
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];
3313 &dmin, &next_block, 0, 0);
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];
3325 &dmin, &next_block, 0, 0);
3331 for(dir=0; dir<2; dir++){
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];
3339 &dmin, &next_block, 0, 0);
3345 s->c.mv[0][0][0] = 0;
3346 s->c.mv[0][0][1] = 0;
3348 &dmin, &next_block, 0, 0);
3349 s->c.mbintra_table[xy] = 1;
3354 const int last_qp = backup_s.c.qscale;
3358 static const int dquant_tab[4]={-1,1,-2,2};
3359 int storecoefs =
s->c.mb_intra &&
s->c.dc_val;
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];
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)
3378 backup_s.dquant= dquant;
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));
3387 &dmin, &next_block,
s->c.mv[mvdir][0][0],
s->c.mv[mvdir][0][1]);
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));
3400 int mx=
s->b_direct_mv_table[xy][0];
3401 int my=
s->b_direct_mv_table[xy][1];
3403 backup_s.dquant = 0;
3408 &dmin, &next_block,
mx,
my);
3411 backup_s.dquant = 0;
3416 &dmin, &next_block, 0, 0);
3421 coded |=
s->c.block_last_index[
i];
3424 memcpy(
s->c.mv, best_s.
c.
mv,
sizeof(
s->c.mv));
3429 mx =
s->c.mv[1][0][0];
3430 my =
s->c.mv[1][0][1];
3432 mx =
s->c.mv[0][0][0];
3433 my =
s->c.mv[0][0][1];
3446 &dmin, &next_block,
mx,
my);
3451 store_context_after_encode(
s, &best_s,
s->data_partitioning);
3455 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3458 if (
s->data_partitioning) {
3461 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3462 s->pb2= backup_s.pb2;
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;
3471 if (CONFIG_H263_ENCODER &&
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);
3484 int motion_x = 0, motion_y = 0;
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;
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];
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];
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];
3522 if (CONFIG_MPEG4_ENCODER) {
3525 motion_x=
s->b_direct_mv_table[xy][0];
3526 motion_y=
s->b_direct_mv_table[xy][1];
3531 if (CONFIG_MPEG4_ENCODER) {
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];
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];
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];
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];
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];
3581 for(dir=0; dir<2; dir++){
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];
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");
3599 s->last_mv_dir =
s->c.mv_dir;
3601 if (CONFIG_H263_ENCODER &&
3608 s->c.cur_pic.qscale_table[xy] =
s->c.qscale;
3611 if (
s->c.mb_intra ) {
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) {
3621 (psnr_per_pass || pass || !has_bottom)) {
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;
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);
3639 if (
s->loop_filter) {
3640 if (CONFIG_H263_ENCODER &&
s->c.out_format ==
FMT_H263)
3643 ff_dlog(
s->c.avctx,
"MB %d %d bits\n",
3648#if CONFIG_MSMPEG4ENC
3650 if (
s->c.msmpeg4_version != MSMP4_UNUSED &&
s->c.msmpeg4_version < MSMP4_WMV1 &&
3660#define ADD(field) dst->field += src->field;
3661#define MERGE(field) dst->field += src->field; src->field=0
3664 ADD(
me.scene_change_score);
3665 ADD(
me.mc_mb_var_sum_temp);
3666 ADD(
me.mb_var_sum_temp);
3673 MERGE(dct_count[0]);
3674 MERGE(dct_count[1]);
3680 ADD(encoding_error[0]);
3681 ADD(encoding_error[1]);
3682 ADD(encoding_error[2]);
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]);
3706 s->c.cur_pic.ptr->f->quality =
quality;
3707 if (
s->c.cur_pic.ptr->f->quality < 0)
3711 if(
s->adaptive_quant){
3714 switch (
s->c.codec_id) {
3716 if (CONFIG_MPEG4_ENCODER)
3722 if (CONFIG_H263_ENCODER)
3727 s->lambda =
s->lambda_table[0];
3730 s->lambda =
s->c.cur_pic.ptr->f->quality;
3739 s->c.time =
s->c.cur_pic.ptr->f->pts *
s->c.avctx->time_base.num;
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);
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;
3756 int context_count =
s->c.slice_context_count;
3766 s->c.no_rounding =
s->c.msmpeg4_version >= MSMP4_V3;
3768 s->c.no_rounding ^=
s->flipflop_rounding;
3787 for (
int i = 0;
i < context_count;
i++) {
3789 int h =
s->c.mb_height;
3791 uint8_t *end =
pkt->data + (
int64_t)
pkt->size * slice->
c. end_mb_y /
h;
3814 &
s->c.enc_contexts[0],
NULL,
3815 context_count,
sizeof(
void*));
3820 NULL, context_count,
sizeof(
void*));
3823 for (
int i = 0;
i <
s->c.mb_stride *
s->c.mb_height;
i++)
3829 NULL, context_count,
sizeof(
void*));
3832 for (
int i = 1;
i < context_count;
i++)
3840 for (
int i = 0;
i <
s->c.mb_stride *
s->c.mb_height;
i++)
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",
3863 for (
int i = 0;
i < 2;
i++) {
3886 for(dir=0; dir<2; dir++){
3887 for (
int i = 0;
i < 2;
i++) {
3892 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3904 if (
s->c.qscale < 3 &&
s->max_qcoeff <= 128 &&
3911 (7 +
s->c.qscale) /
s->c.qscale, 65535);
3919 if (
s->c.avctx->intra_matrix) {
3921 luma_matrix =
s->c.avctx->intra_matrix;
3923 if (
s->c.avctx->chroma_intra_matrix)
3924 chroma_matrix =
s->c.avctx->chroma_intra_matrix;
3927 for (
int i = 1;
i < 64;
i++) {
3928 int j =
s->c.idsp.idct_permutation[
i];
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);
3933 s->c.chroma_intra_matrix[0] =
s->c.intra_matrix[0] = 8 >>
s->c.intra_dc_precision;
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++) {
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;
3949 s->c.intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3951 s->c.chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3960 s->c.cur_pic.ptr->f->pict_type =
s->c.pict_type;
3965 s->c.mb_x =
s->c.mb_y = 0;
3973 for (
int i = 1;
i < context_count;
i++)
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)
3987 if (!
s->dct_error_sum)
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]);
3996 int16_t *
block,
int n,
3997 int qscale,
int *overflow){
4000 const uint8_t *scantable;
4001 const uint8_t *perm_scantable;
4003 unsigned int threshold1, threshold2;
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;
4027 qadd= ((qscale-1)|1)*8;
4030 else mpeg2_qscale = qscale << 1;
4032 if (
s->c.mb_intra) {
4034 scantable =
s->c.intra_scantable.scantable;
4035 perm_scantable =
s->c.intra_scantable.permutated;
4036 if (!
s->c.h263_aic) {
4038 q =
s->c.y_dc_scale;
4040 q =
s->c.c_dc_scale;
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;
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;
4061 length =
s->intra_ac_vlc_length;
4062 last_length=
s->intra_ac_vlc_last_length;
4065 scantable =
s->c.inter_scantable.scantable;
4066 perm_scantable =
s->c.inter_scantable.permutated;
4069 qmat =
s->q_inter_matrix[qscale];
4071 length =
s->inter_ac_vlc_length;
4072 last_length=
s->inter_ac_vlc_last_length;
4077 threshold2= (threshold1<<1);
4079 for(
i=63;
i>=start_i;
i--) {
4080 const int j = scantable[
i];
4083 if(((uint64_t)(
level+threshold1))>threshold2){
4089 for(
i=start_i;
i<=last_non_zero;
i++) {
4090 const int j = scantable[
i];
4095 if(((uint64_t)(
level+threshold1))>threshold2){
4111 coeff[0][
i]= (
level>>31)|1;
4116 *overflow=
s->max_qcoeff <
max;
4118 if(last_non_zero < start_i){
4119 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4120 return last_non_zero;
4123 score_tab[start_i]= 0;
4124 survivor[0]= start_i;
4127 for(
i=start_i;
i<=last_non_zero;
i++){
4128 int level_index, j, zero_distortion;
4130 int best_score=256*256*256*120;
4134 zero_distortion= dct_coeff*dct_coeff;
4136 for(level_index=0; level_index < coeff_count[
i]; level_index++){
4138 int level= coeff[level_index][
i];
4145 unquant_coeff= alevel*qmul + qadd;
4147 j =
s->c.idsp.idct_permutation[scantable[
i]];
4148 unquant_coeff = alevel *
matrix[j] * 8;
4150 j =
s->c.idsp.idct_permutation[scantable[
i]];
4151 if (
s->c.mb_intra) {
4152 unquant_coeff = (int)( alevel * mpeg2_qscale *
matrix[j]) >> 4;
4153 unquant_coeff = (unquant_coeff - 1) | 1;
4155 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int)
matrix[j])) >> 5;
4156 unquant_coeff = (unquant_coeff - 1) | 1;
4161 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4163 if((
level&(~127)) == 0){
4164 for(j=survivor_count-1; j>=0; j--){
4165 int run=
i - survivor[j];
4167 score += score_tab[
i-
run];
4169 if(score < best_score){
4172 level_tab[
i+1]=
level-64;
4177 for(j=survivor_count-1; j>=0; j--){
4178 int run=
i - survivor[j];
4180 score += score_tab[
i-
run];
4181 if(score < last_score){
4184 last_level=
level-64;
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];
4195 if(score < best_score){
4198 level_tab[
i+1]=
level-64;
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){
4209 last_level=
level-64;
4217 score_tab[
i+1]= best_score;
4220 if(last_non_zero <= 27){
4221 for(; survivor_count; survivor_count--){
4222 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4226 for(; survivor_count; survivor_count--){
4227 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4232 survivor[ survivor_count++ ]=
i+1;
4236 last_score= 256*256*256*120;
4237 for(
i= survivor[0];
i<=last_non_zero + 1;
i++){
4238 int score= score_tab[
i];
4240 score += lambda * 2;
4242 if(score < last_score){
4245 last_level= level_tab[
i];
4246 last_run= run_tab[
i];
4251 s->coded_score[n] = last_score;
4254 last_non_zero= last_i - 1;
4255 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4257 if(last_non_zero < start_i)
4258 return last_non_zero;
4260 if(last_non_zero == 0 && start_i == 0){
4262 int best_score= dc * dc;
4264 for(
i=0;
i<coeff_count[0];
i++){
4267 int unquant_coeff, score, distortion;
4270 unquant_coeff= (alevel*qmul + qadd)>>3;
4272 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int)
matrix[0])) >> 5;
4273 unquant_coeff = (unquant_coeff - 1) | 1;
4275 unquant_coeff = (unquant_coeff + 4) >> 3;
4276 unquant_coeff<<= 3 + 3;
4278 distortion= (unquant_coeff - dc) * (unquant_coeff - dc);
4281 else score= distortion + esc_length*lambda;
4283 if(score < best_score){
4285 best_level=
level - 64;
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;
4297 block[ perm_scantable[last_non_zero] ]= last_level;
4300 for(;
i>start_i;
i -= run_tab[
i] + 1){
4301 block[ perm_scantable[
i-1] ]= level_tab[
i];
4304 return last_non_zero;
4318 if(
i==0)
s*= sqrt(0.5);
4319 if(j==0)
s*= sqrt(0.5);
4332 const uint8_t *scantable;
4333 const uint8_t *perm_scantable;
4339 int qmul, qadd, start_i, last_non_zero,
i, dc;
4340 const uint8_t *length;
4341 const uint8_t *last_length;
4343 int rle_index,
run, q = 1, sum;
4345 if(
basis[0][0] == 0)
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) {
4355 q =
s->c.y_dc_scale;
4357 q =
s->c.c_dc_scale;
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;
4374 length =
s->intra_ac_vlc_length;
4375 last_length=
s->intra_ac_vlc_last_length;
4378 scantable =
s->c.inter_scantable.scantable;
4379 perm_scantable =
s->c.inter_scantable.permutated;
4382 length =
s->inter_ac_vlc_length;
4383 last_length=
s->inter_ac_vlc_last_length;
4385 last_non_zero =
s->c.block_last_index[n];
4388 for(
i=0;
i<64;
i++){
4393 for(
i=0;
i<64;
i++){
4399 w= 15 + (48*qns*one +
w/2)/
w;
4412 for(
i=start_i;
i<=last_non_zero;
i++){
4413 int j= perm_scantable[
i];
4419 else coeff= qmul*
level + qadd;
4420 run_tab[rle_index++]=
run;
4423 s->mpvencdsp.add_8x8basis(rem,
basis[j], coeff);
4430 int best_score =
s->mpvencdsp.try_8x8basis(rem,
weight,
basis[0], 0);
4433 int run2, best_unquant_change=0, analyze_gradient;
4434 analyze_gradient = last_non_zero > 2 ||
s->quantizer_noise_shaping >= 3;
4436 if(analyze_gradient){
4437 for(
i=0;
i<64;
i++){
4447 int change, old_coeff;
4453 for(change=-1; change<=1; change+=2){
4454 int new_level=
level + change;
4455 int score, new_coeff;
4457 new_coeff= q*new_level;
4458 if(new_coeff >= 2048 || new_coeff < 0)
4461 score =
s->mpvencdsp.try_8x8basis(rem,
weight,
basis[0],
4462 new_coeff - old_coeff);
4463 if(score<best_score){
4466 best_change= change;
4467 best_unquant_change= new_coeff - old_coeff;
4474 run2= run_tab[rle_index++];
4478 for(
i=start_i;
i<64;
i++){
4479 int j= perm_scantable[
i];
4481 int change, old_coeff;
4483 if(
s->quantizer_noise_shaping < 3 &&
i > last_non_zero + 1)
4488 else old_coeff= qmul*
level + qadd;
4489 run2= run_tab[rle_index++];
4496 for(change=-1; change<=1; change+=2){
4497 int new_level=
level + change;
4498 int score, new_coeff, unquant_change;
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)
4513 if(
i < last_non_zero)
4523 if(analyze_gradient){
4524 int g= d1[ scantable[
i] ];
4525 if(
g && (
g^new_level) >= 0)
4529 if(
i < last_non_zero){
4530 int next_i=
i + run2 + 1;
4531 int next_level=
block[ perm_scantable[next_i] ] + 64;
4533 if(next_level&(~127))
4536 if(next_i < last_non_zero)
4556 if(
i < last_non_zero){
4557 int next_i=
i + run2 + 1;
4558 int next_level=
block[ perm_scantable[next_i] ] + 64;
4560 if(next_level&(~127))
4563 if(next_i < last_non_zero)
4582 unquant_change= new_coeff - old_coeff;
4583 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4585 score +=
s->mpvencdsp.try_8x8basis(rem,
weight,
basis[j],
4587 if(score<best_score){
4590 best_change= change;
4591 best_unquant_change= unquant_change;
4595 prev_level=
level + 64;
4596 if(prev_level&(~127))
4606 int j= perm_scantable[ best_coeff ];
4608 block[j] += best_change;
4610 if(best_coeff > last_non_zero){
4611 last_non_zero= best_coeff;
4614 for(; last_non_zero>=start_i; last_non_zero--){
4615 if(
block[perm_scantable[last_non_zero]])
4622 for(
i=start_i;
i<=last_non_zero;
i++){
4626 run_tab[rle_index++]=
run;
4633 s->mpvencdsp.add_8x8basis(rem,
basis[j], best_unquant_change);
4639 return last_non_zero;
4654 const uint8_t *scantable,
int last)
4665 for (
i = 0;
i <= last;
i++) {
4666 const int j = scantable[
i];
4671 for (
i = 0;
i <= last;
i++) {
4672 const int j = scantable[
i];
4673 const int perm_j = permutation[j];
4679 int16_t *
block,
int n,
4680 int qscale,
int *overflow)
4682 int i, last_non_zero, q, start_i;
4684 const uint8_t *scantable;
4687 unsigned int threshold1, threshold2;
4693 if (
s->c.mb_intra) {
4694 scantable =
s->c.intra_scantable.scantable;
4695 if (!
s->c.h263_aic) {
4697 q =
s->c.y_dc_scale;
4699 q =
s->c.c_dc_scale;
4709 qmat = n < 4 ?
s->q_intra_matrix[qscale] :
s->q_chroma_intra_matrix[qscale];
4712 scantable =
s->c.inter_scantable.scantable;
4715 qmat =
s->q_inter_matrix[qscale];
4719 threshold2= (threshold1<<1);
4720 for(
i=63;
i>=start_i;
i--) {
4721 const int j = scantable[
i];
4724 if(((uint64_t)(
level+threshold1))>threshold2){
4731 for(
i=start_i;
i<=last_non_zero;
i++) {
4732 const int j = scantable[
i];
4737 if(((uint64_t)(
level+threshold1))>threshold2){
4750 *overflow=
s->max_qcoeff <
max;
4755 scantable, last_non_zero);
4757 return last_non_zero;
const uint16_t ff_aanscales[64]
const uint16_t ff_inv_aanscales[64]
AAN (Arai, Agui and Nakajima) (I)DCT tables.
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static double sqr(double in)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
#define FF_MB_DECISION_RD
rate distortion
#define FF_DEBUG_DCT_COEFF
#define FF_MB_DECISION_BITS
chooses the one which needs the fewest bits
#define FF_MB_DECISION_SIMPLE
uses mb_cmp
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
#define i(width, name, range_min, range_max)
#define AV_CEIL_RSHIFT(a, b)
#define ROUNDED_DIV(a, b)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static int dct_error(const struct algo *dct, int test, int is_idct, int speed, const int bits)
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
#define FF_COMPLIANCE_NORMAL
AVCPBProperties * av_cpb_properties_alloc(size_t *size)
Allocate a CPB properties structure and initialize its fields to default values.
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[], int error_count, enum AVPictureType pict_type)
int ff_encode_reordered_opaque(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame)
Propagate user opaque values from the frame to avctx/pkt as needed.
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Allocate buffers for a frame.
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
#define FF_MATRIX_TYPE_INTER
#define FF_MATRIX_TYPE_CHROMA_INTRA
#define FF_MATRIX_TYPE_INTRA
Check if the elements of codec context matrices (intra_matrix, inter_matrix or chroma_intra_matrix) a...
void ff_faandct(int16_t *data)
static const uint8_t bits[8]
void ff_fdct_ifast(int16_t *data)
void ff_jpeg_fdct_islow_10(int16_t *data)
void ff_jpeg_fdct_islow_8(int16_t *data)
#define AV_CODEC_FLAG_QPEL
Use qpel MC.
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_FLAG_CLOSED_GOP
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
#define AV_CODEC_FLAG_AC_PRED
H.263 advanced intra coding / MPEG-4 AC prediction.
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
#define AV_CODEC_FLAG_LOOP_FILTER
loop filter.
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
#define AV_CODEC_FLAG_PSNR
error[?
#define AV_CODEC_FLAG_4MV
4 MV per MB allowed / advanced prediction for H.263.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
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...
@ 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...
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Shrink the already allocated side data buffer.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
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.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define AVERROR_ENCODER_NOT_FOUND
Encoder not found.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AVERROR_EOF
End of file.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
static double av_q2d(AVRational a)
Convert an AVRational to a double.
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
@ AV_PICTURE_TYPE_I
Intra.
@ AV_PICTURE_TYPE_P
Predicted.
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define LIBAVUTIL_VERSION_INT
void ff_h261_reorder_mb_index(MPVEncContext *const s)
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
void ff_h263_loop_filter(MpegEncContext *s)
static void ff_h263_clean_intra_table_entries(MpegEncContext *s, int xy)
const uint16_t ff_h263_format[8][2]
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)
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.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static int shift(int a, int b)
av_cold void ff_fdctdsp_init(FDCTDSPContext *c, AVCodecContext *avctx)
common internal api header.
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.
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
av_cold void ff_pixblockdsp_init(PixblockDSPContext *c, int bits_per_raw_sample)
Macro definitions for various function/variable attributes.
common internal API header
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
static int ff_thread_once(char *control, void(*routine)(void))
const uint8_t ff_zigzag_direct[64]
EXTERN const uint32_t ff_square_tab[512]
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.
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
int(* me_cmp_func)(MPVEncContext *c, const uint8_t *blk1, const uint8_t *blk2, ptrdiff_t stride, int h)
void * av_calloc(size_t nmemb, size_t size)
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...
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]
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)
void ff_me_init_pic(MPVEncContext *const s)
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)
const uint16_t ff_mpeg1_default_intra_matrix[256]
const uint16_t ff_mpeg1_default_non_intra_matrix[64]
static void ff_mpeg1_clean_buffers(MPVEncContext *s)
void ff_mpeg1_encode_slice_header(MPVEncContext *s)
const int16_t ff_mpeg4_default_intra_matrix[64]
const int16_t ff_mpeg4_default_non_intra_matrix[64]
void ff_mpeg4_clean_buffers(MpegEncContext *s)
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my)
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)
void ff_mpv_unref_picture(MPVWorkPicture *pic)
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.
void ff_mpv_replace_picture(MPVWorkPicture *dst, const MPVWorkPicture *src)
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
Initialize an MpegEncContext's thread contexts.
av_cold void ff_mpv_idct_init(MpegEncContext *s)
av_cold void ff_mpv_common_end(MpegEncContext *s)
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
void ff_init_block_index(MpegEncContext *s)
av_cold void ff_mpv_common_defaults(MpegEncContext *s)
Set the given MpegEncContext to common defaults (same for encoding and decoding).
int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
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])
static void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample, int lowres, int chroma_x_shift)
#define MV_TYPE_FIELD
2 vectors, one per field
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
#define MV_DIRECT
bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4)
#define MV_TYPE_16X16
1 vector for the whole mb
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)
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.
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)
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)
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)
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 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
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 FF_MPV_FLAG_SKIP_RD
#define FF_MPV_COMMON_MOTION_EST_OPTS
#define FF_MPV_FLAG_STRICT_GOP
void ff_msmpeg4_encode_ext_header(MPVEncContext *const s)
av_cold void ff_msmpeg4_encode_init(MPVMainEncContext *const m)
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static void rebase_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Rebase the bit writer onto a reallocated buffer.
static void set_put_bits_buffer_size(PutBitContext *s, int size)
Change the end of the buffer.
static int put_bits_count(PutBitContext *s)
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
static int put_bytes_count(const PutBitContext *s, int round_up)
static int put_bytes_left(const PutBitContext *s, int round_up)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static av_unused void put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
void(* qpel_mc_func)(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
int ff_get_2pass_fcode(MPVMainEncContext *const m)
void ff_write_pass1_stats(MPVMainEncContext *const m)
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...
void * av_refstruct_pool_get(AVRefStructPool *pool)
Get an object from the pool, reusing an old one from the pool when available.
static void av_refstruct_pool_uninit(AVRefStructPool **poolp)
Mark the pool as being available for freeing.
const h264_weight_func weight
int ff_rv20_encode_picture_header(MPVMainEncContext *const m)
#define FF_ARRAY_ELEMS(a)
static const uint8_t sp5x_qscale_five_quant_table[][64]
void ff_speedhq_end_slice(MPVEncContext *const s)
static int ff_speedhq_mb_y_order_to_mb(int mb_y_order, int mb_height, int *first_in_slice)
This structure describes the bitrate properties of an encoded bitstream.
int64_t avg_bitrate
Average bitrate of the stream, in bits per second.
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
uint64_t vbv_delay
The delay between the time the packet this structure is associated with is received and the time when...
int64_t min_bitrate
Minimum bitrate of the stream, in bits per second.
Describe the class of an AVClass context structure.
main external API structure.
float rc_max_available_vbv_use
Ratecontrol attempt to use, at maximum, of what can be used without an underflow.
int trellis
trellis RD quantization
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int width
picture width / height.
char * stats_out
pass1 encoding statistics output buffer
int rc_buffer_size
decoder bitstream buffer size
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
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...
int qmin
minimum quantizer
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
uint16_t * inter_matrix
custom inter quantization matrix Must be allocated with the av_malloc() family of functions,...
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
int mb_decision
macroblock decision mode
int has_b_frames
Size of the frame reordering buffer in the decoder.
int64_t bit_rate
the average bitrate
const struct AVCodec * codec
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
float temporal_cplx_masking
temporary complexity masking (0-> disabled)
float p_masking
p block masking (0-> disabled)
float dark_masking
darkness masking (0-> disabled)
int mb_cmp
macroblock comparison function (not supported yet)
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
int ildct_cmp
interlaced DCT comparison function
int64_t rc_max_rate
maximum bitrate
int qmax
maximum quantizer
uint16_t * intra_matrix
custom intra quantization matrix Must be allocated with the av_malloc() family of functions,...
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int flags
AV_CODEC_FLAG_*.
int64_t rc_min_rate
minimum bitrate
uint64_t error[AV_NUM_DATA_POINTERS]
error
float lumi_masking
luminance masking (0-> disabled)
struct AVCodecInternal * internal
Private context used for internal data.
float spatial_cplx_masking
spatial complexity masking (0-> disabled)
int slices
Number of slices.
unsigned int byte_buffer_size
uint8_t * byte_buffer
temporary buffer used for encoders to store their bitstream
int capabilities
Codec capabilities.
This structure describes decoded (raw) audio or video data.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
enum AVPictureType pict_type
Picture type of the frame.
This structure stores compressed data.
void(* fdct)(int16_t *block)
struct MBBackup::@342113007365243263012337326261363212055053105164 c
int(* sum_abs_dctelem)(const int16_t *block)
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
int scenechange_threshold
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 me_penalty_compensation
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)
int display_picture_number
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.
int16_t * dc_val
used for H.263 AIC/MPEG-4 DC prediction and ER
int start_mb_y
start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
int16_t(* ac_val)[16]
used for H.263 AIC, MPEG-4 AC prediction
double buffer_index
amount of bits in the video/audio buffer
int num_entries
number of RateControlEntries
uint8_t * scratchpad_buf
the other *_scratchpad point into this buffer
static int ref[MAX_W *MAX_W]
static const struct twinvq_data tab
static float mean(const float *input, int size)
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[]
static int bias(int x, int c)