Go to the documentation of this file.
34 #include "config_components.h"
80 #define QUANT_BIAS_SHIFT 8
82 #define QMAT_SHIFT_MMX 16
89 int16_t *
block,
int n,
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);
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;
614 if (
s->c.intra_dc_precision < 0) {
615 s->c.intra_dc_precision += 8;
616 }
else if (
s->c.intra_dc_precision >= 8)
617 s->c.intra_dc_precision -= 8;
619 if (
s->c.intra_dc_precision < 0) {
621 "intra dc precision must be positive, note some applications use"
622 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
680 av_log(avctx,
AV_LOG_ERROR,
"Either both buffer size and max rate or neither must be specified\n");
686 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
703 "impossible bitrate constraints, this will fail\n");
719 if (nbt <= INT_MAX) {
734 "OBMC is only supported with simple mb decision\n");
749 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
757 (avctx->
width > 2048 ||
763 (avctx->
width > 65535 ||
764 avctx->
height > 65535 )) {
771 ((avctx->
width &3) ||
811 "closed gop with scene change detection are not supported yet, "
812 "set threshold to 1000000000\n");
820 "low delay forcing is only available for mpeg2, "
821 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
826 "B-frames cannot be used with low delay\n");
839 "notice: b_frame_strategy only affects the first pass\n");
854 s->inter_quant_bias = 0;
856 s->intra_quant_bias = 0;
862 av_log(avctx,
AV_LOG_ERROR,
"qmin and or qmax are invalid, they must be 0 < min <= max\n");
866 av_log(avctx,
AV_LOG_DEBUG,
"intra_quant_bias = %d inter_quant_bias = %d\n",
s->intra_quant_bias,
s->inter_quant_bias);
869 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
880 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
902 if (!CONFIG_H263_ENCODER)
905 s->c.width,
s->c.height) == 8) {
907 "The specified picture size of %dx%d is not valid for "
908 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
909 "352x288, 704x576, and 1408x1152. "
910 "Try H.263+.\n",
s->c.width,
s->c.height);
921 s->modified_quant =
s->c.h263_aic;
923 s->me.unrestricted_mv =
s->c.obmc ||
s->loop_filter ||
s->umvplus;
924 s->flipflop_rounding = 1;
933 s->me.unrestricted_mv = 1;
938 #if CONFIG_RV10_ENCODER
946 #if CONFIG_RV20_ENCODER
952 s->modified_quant = 1;
957 s->me.unrestricted_mv = 0;
963 s->me.unrestricted_mv = 1;
964 s->flipflop_rounding = 1;
971 s->me.unrestricted_mv = 1;
972 s->c.msmpeg4_version = MSMP4_V2;
979 s->me.unrestricted_mv = 1;
980 s->c.msmpeg4_version = MSMP4_V3;
981 s->flipflop_rounding = 1;
988 s->me.unrestricted_mv = 1;
989 s->c.msmpeg4_version = MSMP4_WMV1;
990 s->flipflop_rounding = 1;
997 s->me.unrestricted_mv = 1;
998 s->c.msmpeg4_version = MSMP4_WMV2;
999 s->flipflop_rounding = 1;
1004 av_unreachable(
"List contains all codecs using ff_mpv_encode_init()");
1011 s->c.progressive_frame =
1014 s->c.alternate_scan);
1025 s->frame_reconstruction_bitfield = 0;
1057 if (CONFIG_H263_ENCODER &&
s->c.out_format ==
FMT_H263) {
1059 #if CONFIG_MSMPEG4ENC
1060 if (
s->c.msmpeg4_version != MSMP4_UNUSED)
1065 s->c.slice_ctx_size =
sizeof(*s);
1072 if (
s->c.slice_context_count > 1) {
1075 s->h263_slice_structured = 1;
1153 int16_t *
block,
int i, uint8_t *dest,
int line_size,
int qscale)
1155 s->c.dct_unquantize_intra(&
s->c,
block,
i, qscale);
1156 s->c.idsp.idct_put(dest, line_size,
block);
1160 int16_t *
block,
int i, uint8_t *dest,
int line_size,
int qscale)
1162 if (
s->c.block_last_index[
i] >= 0) {
1163 s->c.dct_unquantize_inter(&
s->c,
block,
i, qscale);
1165 s->c.idsp.idct_add(dest, line_size,
block);
1177 for (
int i = 0;
i < 6;
i++) {
1178 for (
int j = 0; j < 64; j++) {
1180 block[
i][
s->c.idsp.idct_permutation[j]]);
1186 if ((1 <<
s->c.pict_type) &
s->frame_reconstruction_bitfield) {
1187 uint8_t *dest_y =
s->c.dest[0], *dest_cb =
s->c.dest[1], *dest_cr =
s->c.dest[2];
1189 const int linesize =
s->c.cur_pic.linesize[0];
1190 const int uvlinesize =
s->c.cur_pic.linesize[1];
1191 const int block_size = 8;
1193 dct_linesize = linesize <<
s->c.interlaced_dct;
1194 dct_offset =
s->c.interlaced_dct ? linesize : linesize * block_size;
1196 if (!
s->c.mb_intra) {
1204 if (
s->c.chroma_y_shift) {
1219 put_dct(
s,
block[1], 1, dest_y + block_size, dct_linesize,
s->c.qscale);
1224 if (
s->c.chroma_y_shift) {
1225 put_dct(
s,
block[4], 4, dest_cb, uvlinesize,
s->c.chroma_qscale);
1226 put_dct(
s,
block[5], 5, dest_cr, uvlinesize,
s->c.chroma_qscale);
1230 put_dct(
s,
block[4], 4, dest_cb, dct_linesize,
s->c.chroma_qscale);
1231 put_dct(
s,
block[5], 5, dest_cr, dct_linesize,
s->c.chroma_qscale);
1245 for (y = 0; y < 16; y++) {
1246 for (x = 0; x < 16; x++) {
1260 w =
s->c.width & ~15;
1261 h =
s->c.height & ~15;
1263 for (y = 0; y <
h; y += 16) {
1264 for (x = 0; x <
w; x += 16) {
1271 acc += sae + 500 < sad;
1297 for (
int i = 0;
f->data[
i];
i++) {
1318 int display_picture_number = 0,
ret;
1320 : (
s->c.low_delay ? 0 : 1);
1321 int flush_offset = 1;
1336 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1341 if (!
s->c.low_delay && display_picture_number == 1)
1350 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1353 pts = display_picture_number;
1357 if (pic_arg->
linesize[0] !=
s->c.linesize ||
1358 pic_arg->
linesize[1] !=
s->c.uvlinesize ||
1359 pic_arg->
linesize[2] !=
s->c.uvlinesize)
1361 if ((
s->c.width & 15) || (
s->c.height & 15))
1369 pic_arg->
linesize[1],
s->c.linesize,
s->c.uvlinesize);
1384 for (
int i = 0;
i < 3;
i++) {
1385 ptrdiff_t src_stride = pic_arg->
linesize[
i];
1386 ptrdiff_t dst_stride =
i ?
s->c.uvlinesize :
s->c.linesize;
1387 int h_shift =
i ?
s->c.chroma_x_shift : 0;
1388 int v_shift =
i ?
s->c.chroma_y_shift : 0;
1391 const uint8_t *
src = pic_arg->
data[
i];
1396 && !
s->c.progressive_sequence
1397 &&
FFALIGN(
s->c.height, 32) -
s->c.height > 16)
1400 if (!
s->c.avctx->rc_buffer_size)
1403 if (src_stride == dst_stride)
1404 memcpy(
dst,
src, src_stride *
h - src_stride +
w);
1407 uint8_t *dst2 =
dst;
1409 memcpy(dst2,
src,
w);
1414 if ((
s->c.width & 15) || (
s->c.height & (vpad-1))) {
1415 s->mpvencdsp.draw_edges(
dst, dst_stride,
1432 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1436 encoding_delay -= flush_offset - 1;
1460 for (
int plane = 0; plane < 3; plane++) {
1461 const int stride =
p->f->linesize[plane];
1462 const int bw = plane ? 1 : 2;
1463 for (
int y = 0; y <
s->c.mb_height * bw; y++) {
1464 for (
int x = 0; x <
s->c.mb_width * bw; x++) {
1465 int off =
p->shared ? 0 : 16;
1466 const uint8_t *dptr =
p->f->data[plane] + 8 * (x + y *
stride) + off;
1467 const uint8_t *rptr =
ref->f->data[plane] + 8 * (x + y *
stride);
1471 case 0: score =
FFMAX(score, v);
break;
1472 case 1: score +=
FFABS(v);
break;
1473 case 2: score64 += v * (
int64_t)v;
break;
1485 score64 = pow(score64 / (
double)(
s->c.mb_width *
s->c.mb_height),
1488 if (score64 < m->frame_skip_threshold)
1525 int best_b_count = -1;
1538 b_lambda = p_lambda;
1546 if (pre_input_ptr) {
1547 const uint8_t *
data[4];
1550 if (!pre_input_ptr->
shared &&
i) {
1591 c->mb_decision =
s->c.avctx->mb_decision;
1592 c->me_cmp =
s->c.avctx->me_cmp;
1593 c->mb_cmp =
s->c.avctx->mb_cmp;
1594 c->me_sub_cmp =
s->c.avctx->me_sub_cmp;
1596 c->time_base =
s->c.avctx->time_base;
1639 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1657 return best_b_count;
1679 s->c.next_pic.ptr &&
1731 for (
int i = 0;;
i++) {
1736 b_frames =
FFMAX(0,
i - 1);
1742 for (
int i = 0;
i < b_frames + 1;
i++)
1752 for (
int i = b_frames - 1;
i >= 0;
i--) {
1760 "warning, too many B-frames in a row\n");
1784 for (
int i = 0;
i < b_frames;
i++) {
1837 av_assert1(
s->c.mb_width ==
s->c.buffer_pools.alloc_mb_width);
1838 av_assert1(
s->c.mb_height ==
s->c.buffer_pools.alloc_mb_height);
1839 av_assert1(
s->c.mb_stride ==
s->c.buffer_pools.alloc_mb_stride);
1841 &
s->c.sc, &
s->c.buffer_pools,
s->c.mb_height);
1846 s->picture_number =
s->c.cur_pic.ptr->display_picture_number;
1859 if (
s->me.unrestricted_mv &&
1860 s->c.cur_pic.reference &&
1862 int hshift =
s->c.chroma_x_shift;
1863 int vshift =
s->c.chroma_y_shift;
1864 s->mpvencdsp.draw_edges(
s->c.cur_pic.data[0],
1865 s->c.cur_pic.linesize[0],
1866 s->c.h_edge_pos,
s->c.v_edge_pos,
1869 s->mpvencdsp.draw_edges(
s->c.cur_pic.data[1],
1870 s->c.cur_pic.linesize[1],
1871 s->c.h_edge_pos >> hshift,
1872 s->c.v_edge_pos >> vshift,
1876 s->mpvencdsp.draw_edges(
s->c.cur_pic.data[2],
1877 s->c.cur_pic.linesize[2],
1878 s->c.h_edge_pos >> hshift,
1879 s->c.v_edge_pos >> vshift,
1896 for (intra = 0; intra < 2; intra++) {
1897 if (
s->dct_count[intra] > (1 << 16)) {
1898 for (
i = 0;
i < 64;
i++) {
1899 s->dct_error_sum[intra][
i] >>= 1;
1901 s->dct_count[intra] >>= 1;
1904 for (
i = 0;
i < 64;
i++) {
1906 s->dct_count[intra] +
1907 s->dct_error_sum[intra][
i] / 2) /
1908 (
s->dct_error_sum[intra][
i] + 1);
1917 s->c.cur_pic.ptr->f->pict_type =
s->c.pict_type;
1925 if (
s->dct_error_sum) {
1931 const AVFrame *pic_arg,
int *got_packet)
1935 int stuffing_count,
ret;
1936 int context_count =
s->c.slice_context_count;
1953 if (
s->new_pic->data[0]) {
1954 int growing_buffer = context_count == 1 && !
s->data_partitioning;
1955 size_t pkt_size = 10000 +
s->c.mb_width *
s->c.mb_height *
1968 s->c.mb_width*
s->c.mb_height*12);
1969 if (!
s->mb_info_ptr)
1971 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1974 s->c.pict_type =
s->new_pic->pict_type;
1978 if (growing_buffer) {
1988 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
s->c.out_format ==
FMT_MJPEG)
1998 s->lambda < m->
lmax) {
2000 (
s->c.qscale + 1) /
s->c.qscale);
2001 if (
s->adaptive_quant) {
2002 for (
int i = 0;
i <
s->c.mb_height *
s->c.mb_stride;
i++)
2003 s->lambda_table[
i] =
2004 FFMAX(
s->lambda_table[
i] + min_step,
2005 s->lambda_table[
i] * (
s->c.qscale + 1) /
2008 s->c.mb_skipped = 0;
2011 s->c.no_rounding ^=
s->flipflop_rounding;
2014 s->c.time_base =
s->c.last_time_base;
2015 s->c.last_non_b_time =
s->c.time -
s->c.pp_time;
2029 avctx->
error[
i] +=
s->encoding_error[
i];
2037 s->misc_bits +
s->i_tex_bits +
2044 if (stuffing_count) {
2050 switch (
s->c.codec_id) {
2053 while (stuffing_count--) {
2060 stuffing_count -= 4;
2061 while (stuffing_count--) {
2082 int vbv_delay, min_delay;
2092 "Internal error, negative bits\n");
2097 min_delay = (minbits * 90000LL + avctx->
rc_max_rate - 1) /
2100 vbv_delay =
FFMAX(vbv_delay, min_delay);
2104 vbv_delay_ptr[0] &= 0xF8;
2105 vbv_delay_ptr[0] |= vbv_delay >> 13;
2106 vbv_delay_ptr[1] = vbv_delay >> 5;
2107 vbv_delay_ptr[2] &= 0x07;
2108 vbv_delay_ptr[2] |= vbv_delay << 3;
2116 (uint8_t*)props, props_size);
2124 pkt->
pts =
s->c.cur_pic.ptr->f->pts;
2127 if (!
s->c.cur_pic.ptr->coded_picture_number)
2160 int n,
int threshold)
2162 static const char tab[64] = {
2163 3, 2, 2, 1, 1, 1, 1, 1,
2164 1, 1, 1, 1, 1, 1, 1, 1,
2165 1, 1, 1, 1, 1, 1, 1, 1,
2166 0, 0, 0, 0, 0, 0, 0, 0,
2167 0, 0, 0, 0, 0, 0, 0, 0,
2168 0, 0, 0, 0, 0, 0, 0, 0,
2169 0, 0, 0, 0, 0, 0, 0, 0,
2170 0, 0, 0, 0, 0, 0, 0, 0
2175 int16_t *
block =
s->block[n];
2176 const int last_index =
s->c.block_last_index[n];
2179 if (threshold < 0) {
2181 threshold = -threshold;
2186 if (last_index <= skip_dc - 1)
2189 for (
i = 0;
i <= last_index;
i++) {
2190 const int j =
s->c.intra_scantable.permutated[
i];
2193 if (skip_dc &&
i == 0)
2197 }
else if (
level > 1) {
2203 if (score >= threshold)
2205 for (
i = skip_dc;
i <= last_index;
i++) {
2206 const int j =
s->c.intra_scantable.permutated[
i];
2210 s->c.block_last_index[n] = 0;
2212 s->c.block_last_index[n] = -1;
2219 const int maxlevel =
s->max_qcoeff;
2220 const int minlevel =
s->min_qcoeff;
2223 if (
s->c.mb_intra) {
2228 for (;
i <= last_index;
i++) {
2229 const int j =
s->c.intra_scantable.permutated[
i];
2232 if (
level > maxlevel) {
2235 }
else if (
level < minlevel) {
2245 "warning, clipping %d dct coefficients to %d..%d\n",
2253 for (y = 0; y < 8; y++) {
2254 for (x = 0; x < 8; x++) {
2260 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2261 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2262 int v = ptr[x2 + y2 *
stride];
2274 int motion_x,
int motion_y,
2275 int mb_block_height,
2284 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2285 (s)->c.avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2287 int16_t orig[12][64];
2288 const int mb_x =
s->c.mb_x;
2289 const int mb_y =
s->c.mb_y;
2293 int uv_dct_offset =
s->c.uvlinesize * 8;
2294 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2295 ptrdiff_t wrap_y, wrap_c;
2297 for (
i = 0;
i < mb_block_count;
i++)
2298 skip_dct[
i] =
s->skipdct;
2300 if (
s->adaptive_quant) {
2301 const int last_qp =
s->c.qscale;
2302 const int mb_xy = mb_x + mb_y *
s->c.mb_stride;
2304 s->lambda =
s->lambda_table[mb_xy];
2309 s->dquant =
s->c.cur_pic.qscale_table[mb_xy] - last_qp;
2315 if (!
s->c.mb_intra) {
2330 wrap_y =
s->c.linesize;
2331 wrap_c =
s->c.uvlinesize;
2332 ptr_y =
s->new_pic->data[0] +
2333 (mb_y * 16 * wrap_y) + mb_x * 16;
2334 ptr_cb =
s->new_pic->data[1] +
2335 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2336 ptr_cr =
s->new_pic->data[2] +
2337 (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
2339 if ((mb_x * 16 + 16 >
s->c.width || mb_y * 16 + 16 >
s->c.height) &&
2341 uint8_t *ebuf =
s->c.sc.edge_emu_buffer + 38 * wrap_y;
2342 int cw = (
s->c.width + chroma_x_shift) >> chroma_x_shift;
2343 int ch = (
s->c.height + chroma_y_shift) >> chroma_y_shift;
2344 s->c.vdsp.emulated_edge_mc(ebuf, ptr_y,
2346 16, 16, mb_x * 16, mb_y * 16,
2347 s->c.width,
s->c.height);
2349 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2351 mb_block_width, mb_block_height,
2352 mb_x * mb_block_width, mb_y * mb_block_height,
2354 ptr_cb = ebuf + 16 * wrap_y;
2355 s->c.vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2357 mb_block_width, mb_block_height,
2358 mb_x * mb_block_width, mb_y * mb_block_height,
2360 ptr_cr = ebuf + 16 * wrap_y + 16;
2363 if (
s->c.mb_intra) {
2365 int progressive_score, interlaced_score;
2367 s->c.interlaced_dct = 0;
2368 progressive_score =
s->ildct_cmp[1](
s, ptr_y,
NULL, wrap_y, 8) +
2369 s->ildct_cmp[1](
s, ptr_y + wrap_y * 8,
2370 NULL, wrap_y, 8) - 400;
2372 if (progressive_score > 0) {
2373 interlaced_score =
s->ildct_cmp[1](
s, ptr_y,
2374 NULL, wrap_y * 2, 8) +
2375 s->ildct_cmp[1](
s, ptr_y + wrap_y,
2376 NULL, wrap_y * 2, 8);
2377 if (progressive_score > interlaced_score) {
2378 s->c.interlaced_dct = 1;
2381 uv_dct_offset = wrap_c;
2390 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2391 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2392 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2393 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2399 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2400 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2402 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2403 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2405 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2406 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2407 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2408 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2409 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2410 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2416 uint8_t *dest_y, *dest_cb, *dest_cr;
2418 dest_y =
s->c.dest[0];
2419 dest_cb =
s->c.dest[1];
2420 dest_cr =
s->c.dest[2];
2423 op_pix =
s->c.hdsp.put_pixels_tab;
2424 op_qpix =
s->c.qdsp.put_qpel_pixels_tab;
2426 op_pix =
s->c.hdsp.put_no_rnd_pixels_tab;
2427 op_qpix =
s->c.qdsp.put_no_rnd_qpel_pixels_tab;
2434 op_pix =
s->c.hdsp.avg_pixels_tab;
2435 op_qpix =
s->c.qdsp.avg_qpel_pixels_tab;
2444 int progressive_score, interlaced_score;
2446 s->c.interlaced_dct = 0;
2447 progressive_score =
s->ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2448 s->ildct_cmp[0](
s, dest_y + wrap_y * 8,
2453 progressive_score -= 400;
2455 if (progressive_score > 0) {
2456 interlaced_score =
s->ildct_cmp[0](
s, dest_y, ptr_y,
2458 s->ildct_cmp[0](
s, dest_y + wrap_y,
2462 if (progressive_score > interlaced_score) {
2463 s->c.interlaced_dct = 1;
2466 uv_dct_offset = wrap_c;
2474 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2475 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2478 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2485 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2486 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2487 if (!chroma_y_shift) {
2488 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2489 dest_cb + uv_dct_offset, wrap_c);
2490 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2491 dest_cr + uv_dct_offset, wrap_c);
2495 if (
s->mc_mb_var[
s->c.mb_stride * mb_y + mb_x] < 2 *
s->c.qscale *
s->c.qscale) {
2497 if (
s->sad_cmp[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->c.qscale)
2499 if (
s->sad_cmp[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->c.qscale)
2502 wrap_y, 8) < 20 *
s->c.qscale)
2505 wrap_y, 8) < 20 *
s->c.qscale)
2507 if (
s->sad_cmp[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->c.qscale)
2509 if (
s->sad_cmp[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->c.qscale)
2511 if (!chroma_y_shift) {
2512 if (
s->sad_cmp[1](
NULL, ptr_cb + uv_dct_offset,
2513 dest_cb + uv_dct_offset,
2514 wrap_c, 8) < 20 *
s->c.qscale)
2516 if (
s->sad_cmp[1](
NULL, ptr_cr + uv_dct_offset,
2517 dest_cr + uv_dct_offset,
2518 wrap_c, 8) < 20 *
s->c.qscale)
2524 if (
s->quantizer_noise_shaping) {
2537 if (!chroma_y_shift) {
2545 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2551 for (
i = 0;
i < mb_block_count;
i++) {
2554 s->c.block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->c.qscale, &
overflow);
2563 s->c.block_last_index[
i] = -1;
2565 if (
s->quantizer_noise_shaping) {
2566 for (
i = 0;
i < mb_block_count;
i++) {
2568 s->c.block_last_index[
i] =
2570 orig[
i],
i,
s->c.qscale);
2575 if (
s->luma_elim_threshold && !
s->c.mb_intra)
2576 for (
i = 0;
i < 4;
i++)
2578 if (
s->chroma_elim_threshold && !
s->c.mb_intra)
2579 for (
i = 4;
i < mb_block_count;
i++)
2583 for (
i = 0;
i < mb_block_count;
i++) {
2584 if (
s->c.block_last_index[
i] == -1)
2585 s->coded_score[
i] = INT_MAX / 256;
2591 s->c.block_last_index[4] =
2592 s->c.block_last_index[5] = 0;
2594 s->block[5][0] = (1024 +
s->c.c_dc_scale / 2) /
s->c.c_dc_scale;
2595 if (!chroma_y_shift) {
2596 for (
i=6;
i<12;
i++) {
2597 s->c.block_last_index[
i] = 0;
2598 s->block[
i][0] =
s->block[4][0];
2605 for (
i = 0;
i < mb_block_count;
i++) {
2607 if (
s->c.block_last_index[
i] > 0) {
2608 for (j = 63; j > 0; j--) {
2609 if (
s->block[
i][
s->c.intra_scantable.permutated[j]])
2612 s->c.block_last_index[
i] = j;
2617 s->encode_mb(
s,
s->block, motion_x, motion_y);
2649 #define COPY_CONTEXT(BEFORE, AFTER, DST_TYPE, SRC_TYPE) \
2650 static inline void BEFORE ##_context_before_encode(DST_TYPE *const d, \
2651 const SRC_TYPE *const s) \
2654 memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
2657 d->mb_skip_run = s->mb_skip_run; \
2658 for (int i = 0; i < 3; i++) \
2659 d->last_dc[i] = s->last_dc[i]; \
2662 d->mv_bits = s->mv_bits; \
2663 d->i_tex_bits = s->i_tex_bits; \
2664 d->p_tex_bits = s->p_tex_bits; \
2665 d->i_count = s->i_count; \
2666 d->misc_bits = s->misc_bits; \
2669 d->c.mb_skipped = 0; \
2670 d->c.qscale = s->c.qscale; \
2671 d->dquant = s->dquant; \
2673 d->esc3_level_length = s->esc3_level_length; \
2676 static inline void AFTER ## _context_after_encode(DST_TYPE *const d, \
2677 const SRC_TYPE *const s, \
2678 int data_partitioning) \
2681 memcpy(d->c.mv, s->c.mv, 2*4*2*sizeof(int)); \
2682 memcpy(d->c.last_mv, s->c.last_mv, 2*2*2*sizeof(int)); \
2685 d->mb_skip_run = s->mb_skip_run; \
2686 for (int i = 0; i < 3; i++) \
2687 d->last_dc[i] = s->last_dc[i]; \
2690 d->mv_bits = s->mv_bits; \
2691 d->i_tex_bits = s->i_tex_bits; \
2692 d->p_tex_bits = s->p_tex_bits; \
2693 d->i_count = s->i_count; \
2694 d->misc_bits = s->misc_bits; \
2696 d->c.mb_intra = s->c.mb_intra; \
2697 d->c.mb_skipped = s->c.mb_skipped; \
2698 d->c.mv_type = s->c.mv_type; \
2699 d->c.mv_dir = s->c.mv_dir; \
2701 if (data_partitioning) { \
2703 d->tex_pb = s->tex_pb; \
2705 d->block = s->block; \
2706 for (int i = 0; i < 8; i++) \
2707 d->c.block_last_index[i] = s->c.block_last_index[i]; \
2708 d->c.interlaced_dct = s->c.interlaced_dct; \
2709 d->c.qscale = s->c.qscale; \
2711 d->esc3_level_length = s->esc3_level_length; \
2719 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2722 uint8_t *dest_backup[3];
2724 reset_context_before_encode(
s, backup);
2726 s->block =
s->blocks[*next_block];
2727 s->pb = pb[*next_block];
2728 if (
s->data_partitioning) {
2729 s->pb2 = pb2 [*next_block];
2730 s->tex_pb= tex_pb[*next_block];
2734 memcpy(dest_backup,
s->c.dest,
sizeof(
s->c.dest));
2735 s->c.dest[0] =
s->c.sc.rd_scratchpad;
2736 s->c.dest[1] =
s->c.sc.rd_scratchpad + 16*
s->c.linesize;
2737 s->c.dest[2] =
s->c.sc.rd_scratchpad + 16*
s->c.linesize + 8;
2744 if (
s->data_partitioning) {
2752 score *=
s->lambda2;
2757 memcpy(
s->c.dest, dest_backup,
sizeof(
s->c.dest));
2764 save_context_after_encode(best,
s,
s->data_partitioning);
2776 else if(
w==8 &&
h==8)
2794 int chroma_mb_w =
w >>
s->c.chroma_x_shift;
2795 int chroma_mb_h =
h >>
s->c.chroma_y_shift;
2797 if (
s->c.mb_x*16 + 16 >
s->c.width )
w =
s->c.width -
s->c.mb_x*16;
2798 if (
s->c.mb_y*16 + 16 >
s->c.height)
h =
s->c.height-
s->c.mb_y*16;
2801 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,
2802 s->c.dest[0],
s->c.linesize, 16) +
2803 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,
2804 s->c.dest[1],
s->c.uvlinesize, chroma_mb_h) +
2805 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,
2806 s->c.dest[2],
s->c.uvlinesize, chroma_mb_h);
2808 return sse(
s,
s->new_pic->data[0] +
s->c.mb_x * 16 +
s->c.mb_y *
s->c.linesize * 16,
2809 s->c.dest[0],
w,
h,
s->c.linesize) +
2810 sse(
s,
s->new_pic->data[1] +
s->c.mb_x * chroma_mb_w +
s->c.mb_y *
s->c.uvlinesize * chroma_mb_h,
2811 s->c.dest[1],
w >>
s->c.chroma_x_shift,
h >>
s->c.chroma_y_shift,
s->c.uvlinesize) +
2812 sse(
s,
s->new_pic->data[2] +
s->c.mb_x * chroma_mb_w +
s->c.mb_y *
s->c.uvlinesize * chroma_mb_h,
2813 s->c.dest[2],
w >>
s->c.chroma_x_shift,
h >>
s->c.chroma_y_shift,
s->c.uvlinesize);
2821 s->me.dia_size =
s->c.avctx->pre_dia_size;
2822 s->c.first_slice_line = 1;
2823 for (
s->c.mb_y =
s->c.end_mb_y - 1;
s->c.mb_y >=
s->c.start_mb_y;
s->c.mb_y--) {
2824 for (
s->c.mb_x =
s->c.mb_width - 1;
s->c.mb_x >=0 ;
s->c.mb_x--)
2826 s->c.first_slice_line = 0;
2837 s->me.dia_size =
s->c.avctx->dia_size;
2838 s->c.first_slice_line = 1;
2839 for (
s->c.mb_y =
s->c.start_mb_y;
s->c.mb_y <
s->c.end_mb_y;
s->c.mb_y++) {
2842 for (
s->c.mb_x = 0;
s->c.mb_x <
s->c.mb_width;
s->c.mb_x++) {
2843 s->c.block_index[0] += 2;
2844 s->c.block_index[1] += 2;
2845 s->c.block_index[2] += 2;
2846 s->c.block_index[3] += 2;
2854 s->c.first_slice_line = 0;
2862 for (
int mb_y =
s->c.start_mb_y; mb_y < s->
c.end_mb_y; mb_y++) {
2863 for (
int mb_x = 0; mb_x <
s->c.mb_width; mb_x++) {
2866 const uint8_t *
pix =
s->new_pic->data[0] + (yy *
s->c.linesize) + xx;
2868 int sum =
s->mpvencdsp.pix_sum(
pix,
s->c.linesize);
2870 varc = (
s->mpvencdsp.pix_norm1(
pix,
s->c.linesize) -
2871 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2873 s->mb_var [
s->c.mb_stride * mb_y + mb_x] = varc;
2874 s->mb_mean[
s->c.mb_stride * mb_y + mb_x] = (sum+128)>>8;
2875 s->me.mb_var_sum_temp += varc;
2884 if (
s->partitioned_frame)
2888 }
else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2891 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->c.out_format ==
FMT_SPEEDHQ) {
2903 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2905 int mba =
s->c.mb_x +
s->c.mb_width * (
s->c.mb_y %
s->gob_index);
2906 int gobn =
s->c.mb_y /
s->gob_index;
2908 if (CONFIG_H263_ENCODER)
2910 bytestream_put_le32(&ptr,
offset);
2911 bytestream_put_byte(&ptr,
s->c.qscale);
2912 bytestream_put_byte(&ptr, gobn);
2913 bytestream_put_le16(&ptr, mba);
2914 bytestream_put_byte(&ptr, pred_x);
2915 bytestream_put_byte(&ptr, pred_y);
2917 bytestream_put_byte(&ptr, 0);
2918 bytestream_put_byte(&ptr, 0);
2926 s->mb_info_size += 12;
2927 s->prev_mb_info =
s->last_mb_info;
2931 if (!
s->mb_info_size)
2932 s->mb_info_size += 12;
2939 &&
s->c.slice_context_count == 1
2940 &&
s->pb.buf ==
s->c.avctx->internal->byte_buffer) {
2941 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2943 uint8_t *new_buffer =
NULL;
2944 int new_buffer_size = 0;
2946 if ((
s->c.avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2954 s->c.avctx->internal->byte_buffer_size + size_increase);
2958 memcpy(new_buffer,
s->c.avctx->internal->byte_buffer,
s->c.avctx->internal->byte_buffer_size);
2959 av_free(
s->c.avctx->internal->byte_buffer);
2960 s->c.avctx->internal->byte_buffer = new_buffer;
2961 s->c.avctx->internal->byte_buffer_size = new_buffer_size;
2963 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2972 int chr_h = 16 >>
s->c.chroma_y_shift;
2997 s->last_dc[
i] = 128 <<
s->c.intra_dc_precision;
2999 s->encoding_error[
i] = 0;
3002 s->last_dc[0] = 128 * 8 / 13;
3003 s->last_dc[1] = 128 * 8 / 14;
3004 s->last_dc[2] = 128 * 8 / 14;
3005 #if CONFIG_MPEG4_ENCODER
3006 }
else if (
s->partitioned_frame) {
3012 memset(
s->c.last_mv, 0,
sizeof(
s->c.last_mv));
3016 s->c.resync_mb_x = 0;
3017 s->c.resync_mb_y = 0;
3018 s->c.first_slice_line = 1;
3019 s->ptr_lastgob =
s->pb.buf;
3020 for (
int mb_y_order =
s->c.start_mb_y; mb_y_order < s->
c.end_mb_y; mb_y_order++) {
3025 if (first_in_slice && mb_y_order !=
s->c.start_mb_y)
3027 s->last_dc[0] =
s->last_dc[1] =
s->last_dc[2] = 1024 <<
s->c.intra_dc_precision;
3037 for (
int mb_x = 0; mb_x <
s->c.mb_width; mb_x++) {
3042 int size_increase =
s->c.avctx->internal->byte_buffer_size/4
3050 if (
s->data_partitioning) {
3064 xy =
s->c.mb_y *
s->c.mb_stride +
s->c.mb_x;
3065 mb_type =
s->mb_type[xy];
3069 int current_packet_size, is_gob_start;
3072 - (
s->ptr_lastgob -
s->pb.buf);
3074 is_gob_start =
s->rtp_payload_size &&
3075 current_packet_size >=
s->rtp_payload_size &&
3078 if (
s->c.start_mb_y == mb_y && mb_y > 0 && mb_x == 0) is_gob_start = 1;
3080 switch (
s->c.codec_id) {
3083 if (!
s->h263_slice_structured)
3084 if (
s->c.mb_x ||
s->c.mb_y %
s->gob_index) is_gob_start = 0;
3087 if (
s->c.mb_x == 0 &&
s->c.mb_y != 0) is_gob_start = 1;
3094 if (
s->c.mb_x == 0 &&
s->c.mb_y != 0) is_gob_start = 1;
3099 if (
s->c.start_mb_y != mb_y || mb_x != 0) {
3109 if (
s->error_rate &&
s->c.resync_mb_x +
s->c.resync_mb_y > 0) {
3111 int d = 100 /
s->error_rate;
3113 current_packet_size=0;
3114 s->pb.buf_ptr=
s->ptr_lastgob;
3119 switch (
s->c.codec_id) {
3121 if (CONFIG_MPEG4_ENCODER) {
3129 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3134 #if CONFIG_H263P_ENCODER
3141 if (CONFIG_H263_ENCODER) {
3143 s->mb_info_size += 12;
3153 s->misc_bits+=
bits -
s->last_bits;
3157 s->ptr_lastgob += current_packet_size;
3158 s->c.first_slice_line = 1;
3159 s->c.resync_mb_x = mb_x;
3160 s->c.resync_mb_y = mb_y;
3164 if (
s->c.resync_mb_x ==
s->c.mb_x &&
3165 s->c.resync_mb_y+1 ==
s->c.mb_y)
3166 s->c.first_slice_line = 0;
3168 s->c.mb_skipped = 0;
3175 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3177 backup_context_before_encode(&backup_s,
s);
3179 if (
s->data_partitioning) {
3180 backup_s.pb2=
s->pb2;
3181 backup_s.tex_pb=
s->tex_pb;
3188 s->c.mv[0][0][0] =
s->p_mv_table[xy][0];
3189 s->c.mv[0][0][1] =
s->p_mv_table[xy][1];
3191 &dmin, &next_block,
s->c.mv[0][0][0],
s->c.mv[0][0][1]);
3198 int j =
s->c.field_select[0][
i] =
s->p_field_select_table[
i][xy];
3199 s->c.mv[0][
i][0] =
s->c.p_field_mv_table[
i][j][xy][0];
3200 s->c.mv[0][
i][1] =
s->c.p_field_mv_table[
i][j][xy][1];
3203 &dmin, &next_block, 0, 0);
3209 s->c.mv[0][0][0] = 0;
3210 s->c.mv[0][0][1] = 0;
3212 &dmin, &next_block,
s->c.mv[0][0][0],
s->c.mv[0][0][1]);
3219 s->c.mv[0][
i][0] =
s->c.cur_pic.motion_val[0][
s->c.block_index[
i]][0];
3220 s->c.mv[0][
i][1] =
s->c.cur_pic.motion_val[0][
s->c.block_index[
i]][1];
3223 &dmin, &next_block, 0, 0);
3229 s->c.mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3230 s->c.mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3232 &dmin, &next_block,
s->c.mv[0][0][0],
s->c.mv[0][0][1]);
3238 s->c.mv[1][0][0] =
s->b_back_mv_table[xy][0];
3239 s->c.mv[1][0][1] =
s->b_back_mv_table[xy][1];
3241 &dmin, &next_block,
s->c.mv[1][0][0],
s->c.mv[1][0][1]);
3247 s->c.mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3248 s->c.mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3249 s->c.mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3250 s->c.mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3252 &dmin, &next_block, 0, 0);
3259 int j =
s->c.field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3260 s->c.mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3261 s->c.mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3264 &dmin, &next_block, 0, 0);
3271 int j =
s->c.field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3272 s->c.mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3273 s->c.mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3276 &dmin, &next_block, 0, 0);
3282 for(dir=0; dir<2; dir++){
3284 int j =
s->c.field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3285 s->c.mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3286 s->c.mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3290 &dmin, &next_block, 0, 0);
3296 s->c.mv[0][0][0] = 0;
3297 s->c.mv[0][0][1] = 0;
3299 &dmin, &next_block, 0, 0);
3300 s->c.mbintra_table[xy] = 1;
3305 const int last_qp = backup_s.c.qscale;
3309 static const int dquant_tab[4]={-1,1,-2,2};
3310 int storecoefs =
s->c.mb_intra &&
s->c.dc_val;
3318 s->c.mv[0][0][0] = best_s.
c.
mv[0][0][0];
3319 s->c.mv[0][0][1] = best_s.
c.
mv[0][0][1];
3320 s->c.mv[1][0][0] = best_s.
c.
mv[1][0][0];
3321 s->c.mv[1][0][1] = best_s.
c.
mv[1][0][1];
3324 for(; qpi<4; qpi++){
3325 int dquant= dquant_tab[qpi];
3326 qp= last_qp + dquant;
3327 if (qp < s->
c.avctx->qmin || qp >
s->c.avctx->qmax)
3329 backup_s.dquant= dquant;
3332 dc[
i] =
s->c.dc_val[
s->c.block_index[
i]];
3333 memcpy(ac[
i],
s->c.ac_val[
s->c.block_index[
i]],
sizeof(*
s->c.ac_val));
3338 &dmin, &next_block,
s->c.mv[mvdir][0][0],
s->c.mv[mvdir][0][1]);
3342 s->c.dc_val[
s->c.block_index[
i]] =
dc[
i];
3343 memcpy(
s->c.ac_val[
s->c.block_index[
i]], ac[
i],
sizeof(*
s->c.ac_val));
3351 int mx=
s->b_direct_mv_table[xy][0];
3352 int my=
s->b_direct_mv_table[xy][1];
3354 backup_s.dquant = 0;
3359 &dmin, &next_block,
mx,
my);
3362 backup_s.dquant = 0;
3367 &dmin, &next_block, 0, 0);
3372 coded |=
s->c.block_last_index[
i];
3375 memcpy(
s->c.mv, best_s.
c.
mv,
sizeof(
s->c.mv));
3380 mx =
s->c.mv[1][0][0];
3381 my =
s->c.mv[1][0][1];
3383 mx =
s->c.mv[0][0][0];
3384 my =
s->c.mv[0][0][1];
3397 &dmin, &next_block,
mx,
my);
3402 store_context_after_encode(
s, &best_s,
s->data_partitioning);
3406 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3409 if (
s->data_partitioning) {
3412 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3413 s->pb2= backup_s.pb2;
3417 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3418 s->tex_pb= backup_s.tex_pb;
3422 if (CONFIG_H263_ENCODER &&
3427 s->c.hdsp.put_pixels_tab[0][0](
s->c.dest[0],
s->c.sc.rd_scratchpad ,
s->c.linesize ,16);
3428 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);
3429 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);
3435 int motion_x = 0, motion_y = 0;
3443 motion_x=
s->c.mv[0][0][0] = 0;
3444 motion_y=
s->c.mv[0][0][1] = 0;
3445 s->c.mbintra_table[xy] = 1;
3450 motion_x=
s->c.mv[0][0][0] =
s->p_mv_table[xy][0];
3451 motion_y=
s->c.mv[0][0][1] =
s->p_mv_table[xy][1];
3458 int j =
s->c.field_select[0][
i] =
s->p_field_select_table[
i][xy];
3459 s->c.mv[0][
i][0] =
s->c.p_field_mv_table[
i][j][xy][0];
3460 s->c.mv[0][
i][1] =
s->c.p_field_mv_table[
i][j][xy][1];
3468 s->c.mv[0][
i][0] =
s->c.cur_pic.motion_val[0][
s->c.block_index[
i]][0];
3469 s->c.mv[0][
i][1] =
s->c.cur_pic.motion_val[0][
s->c.block_index[
i]][1];
3473 if (CONFIG_MPEG4_ENCODER) {
3476 motion_x=
s->b_direct_mv_table[xy][0];
3477 motion_y=
s->b_direct_mv_table[xy][1];
3482 if (CONFIG_MPEG4_ENCODER) {
3491 s->c.mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3492 s->c.mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3493 s->c.mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3494 s->c.mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3499 motion_x=
s->c.mv[1][0][0] =
s->b_back_mv_table[xy][0];
3500 motion_y=
s->c.mv[1][0][1] =
s->b_back_mv_table[xy][1];
3505 motion_x=
s->c.mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3506 motion_y=
s->c.mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3513 int j =
s->c.field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3514 s->c.mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3515 s->c.mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3523 int j =
s->c.field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3524 s->c.mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3525 s->c.mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3532 for(dir=0; dir<2; dir++){
3534 int j =
s->c.field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3535 s->c.mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3536 s->c.mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3542 "except CANDIDATE_MB_TYPE_SKIPPED which is never "
3543 "the only candidate (always coupled with INTER) "
3544 "so that it never reaches this switch");
3550 s->last_mv_dir =
s->c.mv_dir;
3552 if (CONFIG_H263_ENCODER &&
3559 s->c.cur_pic.qscale_table[xy] =
s->c.qscale;
3562 if (
s->c.mb_intra ) {
3563 s->p_mv_table[xy][0]=0;
3564 s->p_mv_table[xy][1]=0;
3565 #if CONFIG_H263_ENCODER
3566 }
else if (
s->c.h263_pred ||
s->c.h263_aic) {
3575 if (
s->c.mb_x*16 + 16 >
s->c.width )
w =
s->c.width -
s->c.mb_x*16;
3576 if (
s->c.mb_y*16 + 16 >
s->c.height)
h =
s->c.height-
s->c.mb_y*16;
3578 s->encoding_error[0] +=
sse(
3579 s,
s->new_pic->data[0] +
s->c.mb_x*16 +
s->c.mb_y*
s->c.linesize*16,
3580 s->c.dest[0],
w,
h,
s->c.linesize);
3581 s->encoding_error[1] +=
sse(
3582 s,
s->new_pic->data[1] +
s->c.mb_x*8 +
s->c.mb_y*
s->c.uvlinesize*chr_h,
3583 s->c.dest[1],
w>>1,
h>>
s->c.chroma_y_shift,
s->c.uvlinesize);
3584 s->encoding_error[2] +=
sse(
3585 s,
s->new_pic->data[2] +
s->c.mb_x*8 +
s->c.mb_y*
s->c.uvlinesize*chr_h,
3586 s->c.dest[2],
w>>1,
h>>
s->c.chroma_y_shift,
s->c.uvlinesize);
3588 if (
s->loop_filter) {
3589 if (CONFIG_H263_ENCODER &&
s->c.out_format ==
FMT_H263)
3592 ff_dlog(
s->c.avctx,
"MB %d %d bits\n",
3597 #if CONFIG_MSMPEG4ENC
3599 if (
s->c.msmpeg4_version != MSMP4_UNUSED &&
s->c.msmpeg4_version < MSMP4_WMV1 &&
3609 #define ADD(field) dst->field += src->field;
3610 #define MERGE(field) dst->field += src->field; src->field=0
3613 ADD(
me.scene_change_score);
3614 ADD(
me.mc_mb_var_sum_temp);
3615 ADD(
me.mb_var_sum_temp);
3622 MERGE(dct_count[0]);
3623 MERGE(dct_count[1]);
3629 ADD(encoding_error[0]);
3630 ADD(encoding_error[1]);
3631 ADD(encoding_error[2]);
3633 if (
dst->dct_error_sum) {
3634 for(
i=0;
i<64;
i++){
3635 MERGE(dct_error_sum[0][
i]);
3636 MERGE(dct_error_sum[1][
i]);
3655 s->c.cur_pic.ptr->f->quality =
quality;
3656 if (
s->c.cur_pic.ptr->f->quality < 0)
3660 if(
s->adaptive_quant){
3663 switch (
s->c.codec_id) {
3665 if (CONFIG_MPEG4_ENCODER)
3671 if (CONFIG_H263_ENCODER)
3676 s->lambda =
s->lambda_table[0];
3679 s->lambda =
s->c.cur_pic.ptr->f->quality;
3688 s->c.time =
s->c.cur_pic.ptr->f->pts *
s->c.avctx->time_base.num;
3691 s->c.pb_time =
s->c.pp_time - (
s->c.last_non_b_time -
s->c.time);
3692 av_assert1(
s->c.pb_time > 0 &&
s->c.pb_time <
s->c.pp_time);
3694 s->c.pp_time =
s->c.time -
s->c.last_non_b_time;
3695 s->c.last_non_b_time =
s->c.time;
3696 av_assert1(
s->picture_number == 0 ||
s->c.pp_time > 0);
3705 int context_count =
s->c.slice_context_count;
3709 if (
s->c.out_format ==
FMT_MPEG1 || (
s->c.h263_pred &&
s->c.msmpeg4_version == MSMP4_UNUSED))
3717 s->c.no_rounding =
s->c.msmpeg4_version >= MSMP4_V3;
3719 s->c.no_rounding ^=
s->flipflop_rounding;
3736 for (
int i = 0;
i < context_count;
i++) {
3738 int h =
s->c.mb_height;
3763 &
s->c.enc_contexts[0],
NULL,
3764 context_count,
sizeof(
void*));
3769 NULL, context_count,
sizeof(
void*));
3772 for (
int i = 0;
i <
s->c.mb_stride *
s->c.mb_height;
i++)
3778 NULL, context_count,
sizeof(
void*));
3781 for(
i=1;
i<context_count;
i++){
3791 for (
int i = 0;
i <
s->c.mb_stride *
s->c.mb_height;
i++)
3793 if (
s->c.msmpeg4_version >= MSMP4_V3)
3794 s->c.no_rounding = 1;
3795 ff_dlog(
s->c.avctx,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3837 for(dir=0; dir<2; dir++){
3843 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3855 if (
s->c.qscale < 3 &&
s->max_qcoeff <= 128 &&
3862 (7 +
s->c.qscale) /
s->c.qscale, 65535);
3870 if (
s->c.avctx->intra_matrix) {
3872 luma_matrix =
s->c.avctx->intra_matrix;
3874 if (
s->c.avctx->chroma_intra_matrix)
3875 chroma_matrix =
s->c.avctx->chroma_intra_matrix;
3878 for (
int i = 1;
i < 64;
i++) {
3879 int j =
s->c.idsp.idct_permutation[
i];
3881 s->c.chroma_intra_matrix[j] =
av_clip_uint8((chroma_matrix[
i] *
s->c.qscale) >> 3);
3882 s->c. intra_matrix[j] =
av_clip_uint8(( luma_matrix[
i] *
s->c.qscale) >> 3);
3884 s->c.y_dc_scale_table =
3886 s->c.chroma_intra_matrix[0] =
3889 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};
3890 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};
3891 for (
int i = 1;
i < 64;
i++) {
3897 s->c.y_dc_scale_table = y;
3898 s->c.c_dc_scale_table =
c;
3899 s->c.intra_matrix[0] = 13;
3900 s->c.chroma_intra_matrix[0] = 14;
3903 s->c.intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3905 s->c.chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3914 s->c.cur_pic.ptr->f->pict_type =
s->c.pict_type;
3919 s->c.mb_x =
s->c.mb_y = 0;
3927 for(
i=1;
i<context_count;
i++){
3931 NULL, context_count,
sizeof(
void*));
3932 for(
i=1;
i<context_count;
i++){
3933 if (
s->pb.buf_end ==
s->c.enc_contexts[
i]->pb.buf)
3943 if (!
s->dct_error_sum)
3946 const int intra =
s->c.mb_intra;
3947 s->dct_count[intra]++;
3948 s->mpvencdsp.denoise_dct(
block,
s->dct_error_sum[intra],
s->dct_offset[intra]);
3952 int16_t *
block,
int n,
3956 const uint8_t *scantable;
3957 const uint8_t *perm_scantable;
3959 unsigned int threshold1, threshold2;
3971 int coeff_count[64];
3972 int qmul, qadd, start_i, last_non_zero,
i,
dc;
3973 const int esc_length=
s->ac_esc_length;
3974 const uint8_t *length, *last_length;
3983 qadd= ((qscale-1)|1)*8;
3986 else mpeg2_qscale = qscale << 1;
3988 if (
s->c.mb_intra) {
3990 scantable =
s->c.intra_scantable.scantable;
3991 perm_scantable =
s->c.intra_scantable.permutated;
3992 if (!
s->c.h263_aic) {
3994 q =
s->c.y_dc_scale;
3996 q =
s->c.c_dc_scale;
4008 qmat = n < 4 ?
s->q_intra_matrix[qscale] :
s->q_chroma_intra_matrix[qscale];
4009 matrix = n < 4 ?
s->c.intra_matrix :
s->c.chroma_intra_matrix;
4013 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
4014 length =
s->intra_chroma_ac_vlc_length;
4015 last_length=
s->intra_chroma_ac_vlc_last_length;
4017 length =
s->intra_ac_vlc_length;
4018 last_length=
s->intra_ac_vlc_last_length;
4021 scantable =
s->c.inter_scantable.scantable;
4022 perm_scantable =
s->c.inter_scantable.permutated;
4025 qmat =
s->q_inter_matrix[qscale];
4027 length =
s->inter_ac_vlc_length;
4028 last_length=
s->inter_ac_vlc_last_length;
4033 threshold2= (threshold1<<1);
4035 for(
i=63;
i>=start_i;
i--) {
4036 const int j = scantable[
i];
4039 if(((uint64_t)(
level+threshold1))>threshold2){
4045 for(
i=start_i;
i<=last_non_zero;
i++) {
4046 const int j = scantable[
i];
4051 if(((uint64_t)(
level+threshold1))>threshold2){
4074 if(last_non_zero < start_i){
4075 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4076 return last_non_zero;
4079 score_tab[start_i]= 0;
4080 survivor[0]= start_i;
4083 for(
i=start_i;
i<=last_non_zero;
i++){
4084 int level_index, j, zero_distortion;
4086 int best_score=256*256*256*120;
4090 zero_distortion= dct_coeff*dct_coeff;
4092 for(level_index=0; level_index < coeff_count[
i]; level_index++){
4101 unquant_coeff= alevel*qmul + qadd;
4103 j =
s->c.idsp.idct_permutation[scantable[
i]];
4104 unquant_coeff = alevel *
matrix[j] * 8;
4106 j =
s->c.idsp.idct_permutation[scantable[
i]];
4107 if (
s->c.mb_intra) {
4108 unquant_coeff = (int)( alevel * mpeg2_qscale *
matrix[j]) >> 4;
4109 unquant_coeff = (unquant_coeff - 1) | 1;
4111 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int)
matrix[j])) >> 5;
4112 unquant_coeff = (unquant_coeff - 1) | 1;
4117 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4119 if((
level&(~127)) == 0){
4120 for(j=survivor_count-1; j>=0; j--){
4121 int run=
i - survivor[j];
4123 score += score_tab[
i-
run];
4125 if(score < best_score){
4128 level_tab[
i+1]=
level-64;
4133 for(j=survivor_count-1; j>=0; j--){
4134 int run=
i - survivor[j];
4136 score += score_tab[
i-
run];
4137 if(score < last_score){
4140 last_level=
level-64;
4146 distortion += esc_length*lambda;
4147 for(j=survivor_count-1; j>=0; j--){
4148 int run=
i - survivor[j];
4149 int score= distortion + score_tab[
i-
run];
4151 if(score < best_score){
4154 level_tab[
i+1]=
level-64;
4159 for(j=survivor_count-1; j>=0; j--){
4160 int run=
i - survivor[j];
4161 int score= distortion + score_tab[
i-
run];
4162 if(score < last_score){
4165 last_level=
level-64;
4173 score_tab[
i+1]= best_score;
4176 if(last_non_zero <= 27){
4177 for(; survivor_count; survivor_count--){
4178 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4182 for(; survivor_count; survivor_count--){
4183 if(score_tab[ survivor[survivor_count-1] ] <= best_score + lambda)
4188 survivor[ survivor_count++ ]=
i+1;
4192 last_score= 256*256*256*120;
4193 for(
i= survivor[0];
i<=last_non_zero + 1;
i++){
4194 int score= score_tab[
i];
4196 score += lambda * 2;
4198 if(score < last_score){
4201 last_level= level_tab[
i];
4202 last_run= run_tab[
i];
4207 s->coded_score[n] = last_score;
4210 last_non_zero= last_i - 1;
4211 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4213 if(last_non_zero < start_i)
4214 return last_non_zero;
4216 if(last_non_zero == 0 && start_i == 0){
4218 int best_score=
dc *
dc;
4220 for(
i=0;
i<coeff_count[0];
i++){
4223 int unquant_coeff, score, distortion;
4226 unquant_coeff= (alevel*qmul + qadd)>>3;
4228 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int)
matrix[0])) >> 5;
4229 unquant_coeff = (unquant_coeff - 1) | 1;
4231 unquant_coeff = (unquant_coeff + 4) >> 3;
4232 unquant_coeff<<= 3 + 3;
4234 distortion= (unquant_coeff -
dc) * (unquant_coeff -
dc);
4237 else score= distortion + esc_length*lambda;
4239 if(score < best_score){
4241 best_level=
level - 64;
4244 block[0]= best_level;
4245 s->coded_score[n] = best_score -
dc*
dc;
4246 if(best_level == 0)
return -1;
4247 else return last_non_zero;
4253 block[ perm_scantable[last_non_zero] ]= last_level;
4256 for(;
i>start_i;
i -= run_tab[
i] + 1){
4257 block[ perm_scantable[
i-1] ]= level_tab[
i];
4260 return last_non_zero;
4275 if(
i==0)
s*= sqrt(0.5);
4276 if(j==0)
s*= sqrt(0.5);
4289 const uint8_t *scantable;
4290 const uint8_t *perm_scantable;
4296 int qmul, qadd, start_i, last_non_zero,
i,
dc;
4297 const uint8_t *length;
4298 const uint8_t *last_length;
4300 int rle_index,
run, q = 1, sum;
4302 if(
basis[0][0] == 0)
4307 if (
s->c.mb_intra) {
4308 scantable =
s->c.intra_scantable.scantable;
4309 perm_scantable =
s->c.intra_scantable.permutated;
4310 if (!
s->c.h263_aic) {
4312 q =
s->c.y_dc_scale;
4314 q =
s->c.c_dc_scale;
4327 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
4328 length =
s->intra_chroma_ac_vlc_length;
4329 last_length=
s->intra_chroma_ac_vlc_last_length;
4331 length =
s->intra_ac_vlc_length;
4332 last_length=
s->intra_ac_vlc_last_length;
4335 scantable =
s->c.inter_scantable.scantable;
4336 perm_scantable =
s->c.inter_scantable.permutated;
4339 length =
s->inter_ac_vlc_length;
4340 last_length=
s->inter_ac_vlc_last_length;
4342 last_non_zero =
s->c.block_last_index[n];
4345 for(
i=0;
i<64;
i++){
4350 for(
i=0;
i<64;
i++){
4356 w= 15 + (48*qns*one +
w/2)/
w;
4369 for(
i=start_i;
i<=last_non_zero;
i++){
4370 int j= perm_scantable[
i];
4377 run_tab[rle_index++]=
run;
4387 int best_score =
s->mpvencdsp.try_8x8basis(rem,
weight,
basis[0], 0);
4390 int run2, best_unquant_change=0, analyze_gradient;
4391 analyze_gradient = last_non_zero > 2 ||
s->quantizer_noise_shaping >= 3;
4393 if(analyze_gradient){
4394 for(
i=0;
i<64;
i++){
4404 int change, old_coeff;
4410 for(change=-1; change<=1; change+=2){
4411 int new_level=
level + change;
4412 int score, new_coeff;
4414 new_coeff= q*new_level;
4415 if(new_coeff >= 2048 || new_coeff < 0)
4418 score =
s->mpvencdsp.try_8x8basis(rem,
weight,
basis[0],
4419 new_coeff - old_coeff);
4420 if(score<best_score){
4423 best_change= change;
4424 best_unquant_change= new_coeff - old_coeff;
4431 run2= run_tab[rle_index++];
4435 for(
i=start_i;
i<64;
i++){
4436 int j= perm_scantable[
i];
4438 int change, old_coeff;
4440 if(
s->quantizer_noise_shaping < 3 &&
i > last_non_zero + 1)
4445 else old_coeff= qmul*
level + qadd;
4446 run2= run_tab[rle_index++];
4453 for(change=-1; change<=1; change+=2){
4454 int new_level=
level + change;
4455 int score, new_coeff, unquant_change;
4462 if(new_level<0) new_coeff= qmul*new_level - qadd;
4463 else new_coeff= qmul*new_level + qadd;
4464 if(new_coeff >= 2048 || new_coeff <= -2048)
4469 if(level < 63 && level > -63){
4470 if(
i < last_non_zero)
4480 if(analyze_gradient){
4481 int g= d1[ scantable[
i] ];
4482 if(
g && (
g^new_level) >= 0)
4486 if(
i < last_non_zero){
4487 int next_i=
i + run2 + 1;
4488 int next_level=
block[ perm_scantable[next_i] ] + 64;
4490 if(next_level&(~127))
4493 if(next_i < last_non_zero)
4513 if(
i < last_non_zero){
4514 int next_i=
i + run2 + 1;
4515 int next_level=
block[ perm_scantable[next_i] ] + 64;
4517 if(next_level&(~127))
4520 if(next_i < last_non_zero)
4539 unquant_change= new_coeff - old_coeff;
4540 av_assert2((score < 100*lambda && score > -100*lambda) || lambda==0);
4542 score +=
s->mpvencdsp.try_8x8basis(rem,
weight,
basis[j],
4544 if(score<best_score){
4547 best_change= change;
4548 best_unquant_change= unquant_change;
4552 prev_level=
level + 64;
4553 if(prev_level&(~127))
4563 int j= perm_scantable[ best_coeff ];
4565 block[j] += best_change;
4567 if(best_coeff > last_non_zero){
4568 last_non_zero= best_coeff;
4571 for(; last_non_zero>=start_i; last_non_zero--){
4572 if(
block[perm_scantable[last_non_zero]])
4579 for(
i=start_i;
i<=last_non_zero;
i++){
4580 int j= perm_scantable[
i];
4584 run_tab[rle_index++]=
run;
4591 s->mpvencdsp.add_8x8basis(rem,
basis[j], best_unquant_change);
4597 return last_non_zero;
4612 const uint8_t *scantable,
int last)
4623 for (
i = 0;
i <= last;
i++) {
4624 const int j = scantable[
i];
4629 for (
i = 0;
i <= last;
i++) {
4630 const int j = scantable[
i];
4631 const int perm_j = permutation[j];
4637 int16_t *
block,
int n,
4640 int i, last_non_zero, q, start_i;
4642 const uint8_t *scantable;
4645 unsigned int threshold1, threshold2;
4651 if (
s->c.mb_intra) {
4652 scantable =
s->c.intra_scantable.scantable;
4653 if (!
s->c.h263_aic) {
4655 q =
s->c.y_dc_scale;
4657 q =
s->c.c_dc_scale;
4667 qmat = n < 4 ?
s->q_intra_matrix[qscale] :
s->q_chroma_intra_matrix[qscale];
4670 scantable =
s->c.inter_scantable.scantable;
4673 qmat =
s->q_inter_matrix[qscale];
4677 threshold2= (threshold1<<1);
4678 for(
i=63;
i>=start_i;
i--) {
4679 const int j = scantable[
i];
4682 if(((uint64_t)(
level+threshold1))>threshold2){
4689 for(
i=start_i;
i<=last_non_zero;
i++) {
4690 const int j = scantable[
i];
4695 if(((uint64_t)(
level+threshold1))>threshold2){
4713 scantable, last_non_zero);
4715 return last_non_zero;
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
static int encode_frame(AVCodecContext *c, const AVFrame *frame, AVPacket *pkt)
static int dct_quantize_trellis_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)
void ff_fix_long_p_mvs(MPVEncContext *const s, int type)
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
#define FF_MATRIX_TYPE_INTRA
Check if the elements of codec context matrices (intra_matrix, inter_matrix or chroma_intra_matrix) a...
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 me_pre
prepass for motion estimation
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)
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
const uint8_t * fcode_tab
smallest fcode needed for each MV
int fixed_qscale
fixed qscale if non zero
#define CANDIDATE_MB_TYPE_BIDIR
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)
me_cmp_func frame_skip_cmp_fn
static void dct_single_coeff_elimination(MPVEncContext *const s, int n, int threshold)
#define MV_TYPE_16X16
1 vector for the whole mb
#define AV_LOG_WARNING
Something somehow does not look correct.
static av_cold void init_unquantize(MPVEncContext *const s2, AVCodecContext *avctx)
const AVClass ff_mpv_enc_class
static void encode_mb(MPVEncContext *const s, int motion_x, int motion_y)
void ff_estimate_b_frame_motion(MPVEncContext *const s, int mb_x, int mb_y)
int b_code
backward MV resolution for B-frames
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
Read encoded data from the encoder.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
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 init_qscale_tab(MPVEncContext *const s)
init s->c.cur_pic.qscale_table from s->lambda_table
av_cold int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
Initialize an MpegEncContext's thread contexts.
static void update_noise_reduction(MPVMainEncContext *const m)
void(* dct_unquantize_mpeg2_intra)(const MPVContext *s, int16_t *block, int n, int qscale)
char * dct_error_sum_base
backs dct_error_sum
av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx, const MECmpContext *mecc, int mpvenc)
int16_t * dc_val
used for H.263 AIC/MPEG-4 DC prediction and ER
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
int64_t rc_min_rate
minimum bitrate
static void set_frame_distances(MPVEncContext *const s)
static void frame_start(MPVMainEncContext *const m)
#define AVERROR_EOF
End of file.
void ff_speedhq_end_slice(MPVEncContext *const s)
static int estimate_qp(MPVMainEncContext *const m, int dry_run)
av_cold void ff_msmpeg4_encode_init(MPVMainEncContext *const m)
MpegEncContext c
the common base context
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
av_cold void ff_dct_encode_init(MPVEncContext *const s)
void ff_me_init_pic(MPVEncContext *const s)
static int16_t basis[64][64]
uint16_t * intra_matrix
custom intra quantization matrix Must be allocated with the av_malloc() family of functions,...
static int estimate_best_b_count(MPVMainEncContext *const m)
int last_lambda_for[5]
last lambda for a specific pict type
static const uint8_t mv_bits[2][16][10]
static int estimate_motion_thread(AVCodecContext *c, void *arg)
void ff_clean_h263_qscales(MPVEncContext *s)
float lumi_masking
luminance masking (0-> disabled)
#define MV_DIRECT
bidirectional mode where the difference equals the MV of the last P/S/I-Frame (MPEG-4)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
static int sse(const MPVEncContext *const s, const uint8_t *src1, const uint8_t *src2, int w, int h, int stride)
#define CANDIDATE_MB_TYPE_INTER
int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
This structure describes decoded (raw) audio or video data.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define INTERLACED_DCT(s)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
int capabilities
Codec capabilities.
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Shrink the already allocated side data buffer.
static int put_bytes_count(const PutBitContext *s, int round_up)
unsigned int lambda
Lagrange multiplier used in rate distortion.
int64_t dts_delta
pts difference between the first and second input frame, used for calculating dts of the first frame ...
const uint8_t ff_mpeg2_non_linear_qscale[32]
static void write_slice_end(MPVEncContext *const s)
#define AV_LOG_VERBOSE
Detailed information.
void ff_init_block_index(MpegEncContext *s)
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
#define FF_MPV_FLAG_SKIP_RD
const uint8_t ff_mpeg12_dc_scale_table[4][32]
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
static double sqr(double in)
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
static int pre_estimate_motion_thread(AVCodecContext *c, void *arg)
static void get_visual_weight(int16_t *weight, const uint8_t *ptr, int stride)
#define COPY_CONTEXT(BEFORE, AFTER, DST_TYPE, SRC_TYPE)
int mb_decision
macroblock decision mode
int qmax
maximum quantizer
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
int64_t mb_var_sum
sum of MB variance for current frame
#define AV_CODEC_FLAG_4MV
4 MV per MB allowed / advanced prediction for H.263.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int mb_cmp
macroblock comparison function (not supported yet)
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
int(* encode_picture_header)(struct MPVMainEncContext *m)
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
#define CANDIDATE_MB_TYPE_BACKWARD_I
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int(* sum_abs_dctelem)(const int16_t *block)
int coded_picture_number
used to set pic->coded_picture_number
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
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 ...
#define FF_MPV_COMMON_MOTION_EST_OPTS
static void mpv_reconstruct_mb(MPVEncContext *const s, int16_t block[12][64])
Performs dequantization and IDCT (if necessary)
int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pic_arg, int *got_packet)
#define FF_MPV_COMMON_OPTS
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
static av_cold int init_slice_buffers(MPVMainEncContext *const m)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t mx
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.
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.
const struct AVCodec * codec
int16_t * ff_h263_pred_motion(MpegEncContext *s, int block, int dir, int *px, int *py)
int ff_vbv_update(MPVMainEncContext *m, int frame_size)
static const struct twinvq_data tab
void ff_h263_encode_init(MPVMainEncContext *m)
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
void(* dct_unquantize_mpeg2_inter)(const MPVContext *s, int16_t *block, int n, int qscale)
int flags
AV_CODEC_FLAG_*.
#define CANDIDATE_MB_TYPE_SKIPPED
const h264_weight_func weight
MPVPicture * input_picture[MPVENC_MAX_B_FRAMES+1]
next pictures in display order
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
#define AV_CODEC_FLAG_LOW_DELAY
Force low delay.
#define FF_MPV_FLAG_CBP_RD
static int get_intra_count(MPVEncContext *const s, const uint8_t *src, const uint8_t *ref, int stride)
void ff_mpeg4_init_partitions(MPVEncContext *const s)
static int sse_mb(MPVEncContext *const s)
int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[], int error_count, enum AVPictureType pict_type)
#define AV_CODEC_FLAG_LOOP_FILTER
loop filter.
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
static void ff_mpeg1_encode_init(MPVEncContext *s)
static av_cold int init_matrices(MPVMainEncContext *const m, AVCodecContext *avctx)
static int put_bytes_left(const PutBitContext *s, int round_up)
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
#define CANDIDATE_MB_TYPE_DIRECT
#define CANDIDATE_MB_TYPE_INTER_I
static int skip_check(MPVMainEncContext *const m, const MPVPicture *p, const MPVPicture *ref)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
int stuffing_bits
bits used for stuffing
int picture_in_gop_number
0-> first pic in gop, ...
int num_entries
number of RateControlEntries
static int ff_thread_once(char *control, void(*routine)(void))
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
void ff_h263_encode_gob_header(MPVEncContext *s, int mb_line)
int(* me_cmp_func)(MPVEncContext *c, const uint8_t *blk1, const uint8_t *blk2, ptrdiff_t stride, int h)
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
static uint8_t default_fcode_tab[MAX_MV *2+1]
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my)
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
static void build_basis(uint8_t *perm)
int has_b_frames
Size of the frame reordering buffer in the decoder.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
AVFrame * tmp_frames[MPVENC_MAX_B_FRAMES+2]
temporary frames used by b_frame_strategy = 2
static int get_sae(const uint8_t *src, int ref, int stride)
int ff_rv10_encode_picture_header(MPVMainEncContext *const m)
static void rebase_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Rebase the bit writer onto a reallocated buffer.
#define AV_CEIL_RSHIFT(a, b)
MPVPicture * reordered_input_picture[MPVENC_MAX_B_FRAMES+1]
next pictures in coded order
int intra_only
if true, only intra pictures are generated
int64_t mc_mb_var_sum
motion compensated MB variance for current frame
static void merge_context_after_me(MPVEncContext *const dst, MPVEncContext *const src)
void ff_mpeg4_stuffing(PutBitContext *pbc)
add MPEG-4 stuffing bits (01...1)
RateControlContext rc_context
contains stuff only accessed in ratecontrol.c
static double av_q2d(AVRational a)
Convert an AVRational to a double.
static const uint8_t *const ff_mpeg1_dc_scale_table
#define LOCAL_ALIGNED_16(t, v,...)
PutBitContext pb
bit output
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int start_mb_y
start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y)
void ff_write_quant_matrix(PutBitContext *pb, uint16_t *matrix)
int max_b_frames
max number of B-frames
int ff_pre_estimate_p_frame_motion(MPVEncContext *const s, int mb_x, int mb_y)
void ff_clean_mpeg4_qscales(MPVEncContext *const s)
modify mb_type & qscale so that encoding is actually possible in MPEG-4
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
int64_t rc_max_rate
maximum bitrate
void ff_block_permute(int16_t *block, const uint8_t *permutation, const uint8_t *scantable, int last)
Permute an 8x8 block according to permutation.
uint64_t error[AV_NUM_DATA_POINTERS]
error
This structure describes the bitrate properties of an encoded bitstream.
static int ff_speedhq_mb_y_order_to_mb(int mb_y_order, int mb_height, int *first_in_slice)
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
#define CANDIDATE_MB_TYPE_FORWARD
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t my
float p_masking
p block masking (0-> disabled)
static int mb_var_thread(AVCodecContext *c, void *arg)
static av_cold void mpv_encode_init_static(void)
av_cold void ff_mpv_common_end(MpegEncContext *s)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
void ff_mpv_unref_picture(MPVWorkPicture *pic)
int rc_buffer_size
decoder bitstream buffer size
#define LIBAVUTIL_VERSION_INT
#define CANDIDATE_MB_TYPE_FORWARD_I
Describe the class of an AVClass context structure.
int16_t(* block)[64]
points into blocks below
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
static int bias(int x, int c)
av_cold void ff_mpv_idct_init(MpegEncContext *s)
av_cold void ff_mpv_common_defaults(MpegEncContext *s)
Set the given MpegEncContext to common defaults (same for encoding and decoding).
void(* dct_unquantize_mpeg1_intra)(const MPVContext *s, int16_t *block, int n, int qscale)
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run)
#define CANDIDATE_MB_TYPE_BACKWARD
struct AVCodecInternal * internal
Private context used for internal data.
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
int64_t bit_rate
the average bitrate
int display_picture_number
#define ROUNDED_DIV(a, b)
void ff_faandct(int16_t *data)
const char * av_default_item_name(void *ptr)
Return the context name.
@ AV_PICTURE_TYPE_I
Intra.
unsigned int lambda2
(lambda*lambda) >> FF_LAMBDA_SHIFT
static av_cold int me_cmp_init(MPVMainEncContext *const m, AVCodecContext *avctx)
static int select_input_picture(MPVMainEncContext *const m)
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
static int dct_error(const struct algo *dct, int test, int is_idct, int speed, const int bits)
#define AV_CODEC_FLAG_AC_PRED
H.263 advanced intra coding / MPEG-4 AC prediction.
int ildct_cmp
interlaced DCT comparison function
void * av_refstruct_pool_get(AVRefStructPool *pool)
Get an object from the pool, reusing an old one from the pool when available.
av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
#define FF_MB_DECISION_SIMPLE
uses mb_cmp
int ff_mpv_reallocate_putbitbuffer(MPVEncContext *const s, size_t threshold, size_t size_increase)
void ff_h261_reorder_mb_index(MPVEncContext *const s)
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
#define ff_mpv_unquantize_init(s, bitexact, q_scale_type)
static void add_dequant_dct(MPVEncContext *const s, int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
int16_t(* ac_val)[16]
used for H.263 AIC, MPEG-4 AC prediction
int trellis
trellis RD quantization
void ff_mpeg4_encode_video_packet_header(MPVEncContext *const s)
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 update_duplicate_context_after_me(MPVEncContext *const dst, const MPVEncContext *const src)
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
void(* qpel_mc_func)(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
#define MV_TYPE_8X8
4 vectors (H.263, MPEG-4 4MV)
float temporal_cplx_masking
temporary complexity masking (0-> disabled)
static int load_input_picture(MPVMainEncContext *const m, const AVFrame *pic_arg)
void(* dct_unquantize_h263_inter)(const MPVContext *s, int16_t *block, int n, int qscale)
static void set_put_bits_buffer_size(PutBitContext *s, int size)
Change the end of the buffer.
void ff_set_mpeg4_time(MPVEncContext *const s)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Allocate buffers for a frame.
#define FF_DEBUG_DCT_COEFF
static void ff_h263_clean_intra_table_entries(MpegEncContext *s, int xy)
char * stats_out
pass1 encoding statistics output buffer
#define AV_CODEC_FLAG_QPEL
Use qpel MC.
enum AVPictureType pict_type
Picture type of the frame.
static void clip_coeffs(const MPVEncContext *const s, int16_t block[], int last_index)
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled top and top right vectors is used as motion vector prediction the used motion vector is the sum of the predictor and(mvx_diff, mvy_diff) *mv_scale Intra DC Prediction block[y][x] dc[1]
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
void ff_mpeg4_clean_buffers(MpegEncContext *s)
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
void(* dct_unquantize_mpeg1_inter)(const MPVContext *s, int16_t *block, int n, int qscale)
#define DECLARE_ALIGNED(n, t, v)
int vbv_delay_pos
offset of vbv_delay in the bitstream
static int shift(int a, int b)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
#define i(width, name, range_min, range_max)
static void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample, int lowres, int chroma_x_shift)
#define CANDIDATE_MB_TYPE_DIRECT0
const int16_t ff_mpeg4_default_intra_matrix[64]
#define CANDIDATE_MB_TYPE_INTRA
#define AV_NOPTS_VALUE
Undefined timestamp value.
static const AVOption mpv_generic_options[]
int frame_bits
bits used for the current frame
uint8_t * byte_buffer
temporary buffer used for encoders to store their bitstream
#define FF_MPV_FLAG_QP_RD
static int encode_picture(MPVMainEncContext *const s, const AVPacket *pkt)
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
int64_t min_bitrate
Minimum bitrate of the stream, in bits per second.
const uint16_t ff_mpeg1_default_intra_matrix[256]
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
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.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
#define FF_COMPLIANCE_NORMAL
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
const int16_t ff_mpeg4_default_non_intra_matrix[64]
#define ALLOCZ_ARRAYS(p, mult, numb)
int input_picture_number
used to set pic->display_picture_number
#define AV_CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
void ff_mpeg1_encode_slice_header(MPVEncContext *s)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
#define MV_TYPE_FIELD
2 vectors, one per field
int flags
A combination of AV_PKT_FLAG values.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
int64_t avg_bitrate
Average bitrate of the stream, in bits per second.
unsigned int byte_buffer_size
uint8_t * scratchpad_buf
the other *_scratchpad point into this buffer
int me_penalty_compensation
#define UNI_AC_ENC_INDEX(run, level)
#define CANDIDATE_MB_TYPE_BIDIR_I
#define AV_LOG_INFO
Standard information.
#define CANDIDATE_MB_TYPE_INTER4V
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
int ff_mjpeg_add_icc_profile_size(AVCodecContext *avctx, const AVFrame *frame, size_t *max_pkt_size)
uint64_t vbv_delay
The delay between the time the packet this structure is associated with is received and the time when...
static int get_bits_diff(MPVEncContext *s)
void(* dct_unquantize_h263_intra)(const MPVContext *s, int16_t *block, int n, int qscale)
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
@ 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...
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static int put_bits_count(PutBitContext *s)
int ff_rv20_encode_picture_header(MPVMainEncContext *m)
static int encode_thread(AVCodecContext *c, void *arg)
int f_code
forward MV resolution
int16_t(* mv_table_base)[2]
void ff_jpeg_fdct_islow_8(int16_t *data)
av_cold void ff_fdctdsp_init(FDCTDSPContext *c, AVCodecContext *avctx)
#define FF_MATRIX_TYPE_CHROMA_INTRA
void ff_h263_update_mb(MPVEncContext *s)
int partitioned_frame
is current frame partitioned
int intra_dc_precision
precision of the intra DC coefficient - 8
uint16_t(* dct_offset)[64]
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
av_cold int ff_rate_control_init(MPVMainEncContext *const m)
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...
static void update_mb_info(MPVEncContext *const s)
#define MPVENC_MAX_B_FRAMES
void ff_jpeg_fdct_islow_10(int16_t *data)
static av_cold void mpv_encode_defaults(MPVMainEncContext *const m)
Set the given MPVEncContext to defaults for encoding.
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
int next_lambda
next lambda used for retrying to encode a frame
const uint16_t ff_h263_format[8][2]
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
void ff_write_pass1_stats(MPVMainEncContext *const m)
void ff_msmpeg4_encode_ext_header(MPVEncContext *const s)
const EXTERN uint32_t ff_square_tab[512]
int last_non_b_pict_type
used for MPEG-4 gmc B-frames & ratecontrol
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Supply a raw video or audio frame to the encoder.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void * av_calloc(size_t nmemb, size_t size)
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.
double buffer_index
amount of bits in the video/audio buffer
void ff_get_2pass_fcode(MPVMainEncContext *const m)
static void frame_end(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 av_cold int init_buffers(MPVMainEncContext *const m)
av_cold void ff_pixblockdsp_init(PixblockDSPContext *c, int bits_per_raw_sample)
const uint8_t ff_zigzag_direct[64]
#define AV_CODEC_FLAG_CLOSED_GOP
void ff_h263_mpeg4_reset_dc(MPVEncContext *s)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
const uint16_t ff_mpeg1_default_non_intra_matrix[64]
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
void ff_fdct_ifast(int16_t *data)
const uint16_t ff_inv_aanscales[64]
void ff_h263_loop_filter(MpegEncContext *s)
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)
#define AV_INPUT_BUFFER_PADDING_SIZE
int64_t reordered_pts
reordered pts to be used as dts for the next output frame when there's a delay
uint8_t * scratchpad
data area for the ME algo, so that the ME does not need to malloc/free.
float dark_masking
darkness masking (0-> disabled)
main external API structure.
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
int qmin
minimum quantizer
int ff_mjpeg_encode_stuffing(MPVEncContext *const s)
Writes the complete JPEG frame when optimal huffman tables are enabled, otherwise writes the stuffing...
float spatial_cplx_masking
spatial complexity masking (0-> disabled)
static int ref[MAX_W *MAX_W]
int ff_mpv_pic_check_linesize(void *logctx, const AVFrame *f, ptrdiff_t *linesizep, ptrdiff_t *uvlinesizep)
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
static float mean(const float *input, int size)
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
#define FF_MB_DECISION_RD
rate distortion
void ff_mpv_replace_picture(MPVWorkPicture *dst, const MPVWorkPicture *src)
void ff_estimate_p_frame_motion(MPVEncContext *const s, int mb_x, int mb_y)
@ AV_PICTURE_TYPE_P
Predicted.
static void ff_mpeg1_clean_buffers(MPVEncContext *s)
#define AVERROR_ENCODER_NOT_FOUND
Encoder not found.
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
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...
Undefined Behavior In the C some operations are like signed integer overflow
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
static void denoise_dct(MPVEncContext *const s, int16_t block[])
static int dct_quantize_refine(MPVEncContext *const s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale)
void(* fdct)(int16_t *block)
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
float rc_max_available_vbv_use
Ratecontrol attempt to use, at maximum, of what can be used without an underflow.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
void ff_mpeg4_merge_partitions(MPVEncContext *const s)
static void merge_context_after_encode(MPVEncContext *const dst, MPVEncContext *const src)
static void av_refstruct_pool_uninit(AVRefStructPool **poolp)
Mark the pool as being available for freeing.
static void scale(int *out, const int *in, const int w, const int h, const int shift)
int slices
Number of slices.
#define FF_MB_DECISION_BITS
chooses the one which needs the fewest bits
This structure stores compressed data.
uint16_t * inter_matrix
custom inter quantization matrix Must be allocated with the av_malloc() family of functions,...
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, AVCodecContext *avctx)
int scenechange_threshold
void ff_dct_encode_init_x86(MPVEncContext *s)
int width
picture width / height.
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
static const double coeff[2][5]
The exact code depends on how similar the blocks are and how related they are to the block
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
int64_t user_specified_pts
last non-zero pts from user-supplied AVFrame
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
static int dct_quantize_c(MPVEncContext *const s, int16_t *block, int n, int qscale, int *overflow)
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
#define FF_MPV_FLAG_STRICT_GOP
static const uint8_t sp5x_qscale_five_quant_table[][64]
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
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...
static void update_qscale(MPVMainEncContext *const m)
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
MPVEncContext s
The main slicecontext.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
static void write_mb_info(MPVEncContext *const s)
av_cold AVRefStructPool * ff_mpv_alloc_pic_pool(int init_progress)
Allocate a pool of MPVPictures.
const uint16_t ff_aanscales[64]
AVCPBProperties * av_cpb_properties_alloc(size_t *size)
Allocate a CPB properties structure and initialize its fields to default values.
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
#define FF_MATRIX_TYPE_INTER
av_cold void ff_rate_control_uninit(RateControlContext *rcc)
int ff_get_best_fcode(MPVMainEncContext *const m, const int16_t(*mv_table)[2], int type)