34 #include "config_components.h"
82 #define QUANT_BIAS_SHIFT 8
84 #define QMAT_SHIFT_MMX 16
92 int16_t *
block,
int n,
113 uint16_t (*
qmat16)[2][64],
114 const uint16_t *quant_matrix,
115 int bias,
int qmin,
int qmax,
int intra)
126 else qscale2 =
qscale << 1;
133 for (
i = 0;
i < 64;
i++) {
134 const int j =
s->idsp.idct_permutation[
i];
145 for (
i = 0;
i < 64;
i++) {
146 const int j =
s->idsp.idct_permutation[
i];
157 for (
i = 0;
i < 64;
i++) {
158 const int j =
s->idsp.idct_permutation[
i];
179 for (
i = intra;
i < 64;
i++) {
191 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
198 if (
s->q_scale_type == 1 && 0) {
200 int bestdiff=INT_MAX;
208 if (
diff < bestdiff) {
217 s->qscale =
av_clip(
s->qscale,
s->avctx->qmin,
s->vbv_ignore_qmax ? 31 :
s->avctx->qmax);
230 for (
i = 0;
i < 64;
i++) {
242 int8_t *
const qscale_table =
s->cur_pic.qscale_table;
245 for (
i = 0;
i <
s->mb_num;
i++) {
246 unsigned int lam =
s->lambda_table[
s->mb_index2xy[
i]];
248 qscale_table[
s->mb_index2xy[
i]] =
av_clip(qp,
s->avctx->qmin,
256 #define COPY(a) dst->a= src->a
271 for (
int i = -16;
i < 16;
i++)
290 s->input_picture_number = 0;
291 s->picture_in_gop_number = 0;
305 if (
s->avctx->trellis)
322 s->frame_skip_cmp_fn = me_cmp[1];
327 if (!me_cmp[0] || !me_cmp[4])
329 s->ildct_cmp[0] = me_cmp[0];
330 s->ildct_cmp[1] = me_cmp[4];
335 s->sse_cmp[0] = mecc.
sse[0];
336 s->sse_cmp[1] = mecc.
sse[1];
337 s->sad_cmp[0] = mecc.
sad[0];
338 s->sad_cmp[1] = mecc.
sad[1];
340 s->n_sse_cmp[0] = mecc.
nsse[0];
341 s->n_sse_cmp[1] = mecc.
nsse[1];
343 s->n_sse_cmp[0] = mecc.
sse[0];
344 s->n_sse_cmp[1] = mecc.
sse[1];
356 int mb_array_size, mv_table_size;
384 "keyframe interval too large!, reducing it from %d to %d\n",
396 "max b frames must be 0 or positive for mpegvideo based encoders\n");
407 s->rtp_mode = !!
s->rtp_payload_size;
411 if (
s->intra_dc_precision < 0) {
412 s->intra_dc_precision += 8;
413 }
else if (
s->intra_dc_precision >= 8)
414 s->intra_dc_precision -= 8;
416 if (
s->intra_dc_precision < 0) {
418 "intra dc precision must be positive, note some applications use"
419 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
429 if (
s->gop_size <= 1) {
483 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
500 "impossible bitrate constraints, this will fail\n");
510 if (!
s->fixed_qscale &&
516 if (nbt <= INT_MAX) {
529 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
530 "specified vbv buffer is too large for the given bitrate!\n");
542 "OBMC is only supported with simple mb decision\n");
557 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
610 if (
s->scenechange_threshold < 1000000000 &&
613 "closed gop with scene change detection are not supported yet, "
614 "set threshold to 1000000000\n");
622 "low delay forcing is only available for mpeg2, "
623 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
626 if (
s->max_b_frames != 0) {
628 "B-frames cannot be used with low delay\n");
633 if (
s->q_scale_type == 1) {
636 "non linear quant only supports qmax <= 28 currently\n");
649 "notice: b_frame_strategy only affects the first pass\n");
650 s->b_frame_strategy = 0;
664 s->inter_quant_bias = 0;
666 s->intra_quant_bias = 0;
681 "timebase %d/%d not supported by MPEG 4 standard, "
682 "the maximum admitted value for the timebase denominator "
690 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
697 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
701 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
715 if (!CONFIG_SPEEDHQ_ENCODER)
723 if (!CONFIG_H261_ENCODER)
734 if (!CONFIG_H263_ENCODER)
737 s->width,
s->height) == 8) {
739 "The specified picture size of %dx%d is not valid for "
740 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
741 "352x288, 704x576, and 1408x1152. "
742 "Try H.263+.\n",
s->width,
s->height);
754 s->modified_quant =
s->h263_aic;
756 s->unrestricted_mv =
s->obmc ||
s->loop_filter ||
s->umvplus;
757 s->flipflop_rounding = 1;
767 s->unrestricted_mv = 1;
781 s->modified_quant = 1;
785 s->unrestricted_mv = 0;
790 s->unrestricted_mv = 1;
791 s->flipflop_rounding = 1;
792 s->low_delay =
s->max_b_frames ? 0 : 1;
793 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
798 s->unrestricted_mv = 1;
806 s->unrestricted_mv = 1;
808 s->flipflop_rounding = 1;
815 s->unrestricted_mv = 1;
817 s->flipflop_rounding = 1;
824 s->unrestricted_mv = 1;
826 s->flipflop_rounding = 1;
838 s->progressive_frame =
843 if (
s->lmin >
s->lmax) {
875 mv_table_size = (
s->mb_height + 2) *
s->mb_stride + 1;
883 s->p_mv_table =
s->p_mv_table_base +
s->mb_stride + 1;
884 s->b_forw_mv_table =
s->b_forw_mv_table_base +
s->mb_stride + 1;
885 s->b_back_mv_table =
s->b_back_mv_table_base +
s->mb_stride + 1;
886 s->b_bidir_forw_mv_table =
s->b_bidir_forw_mv_table_base +
s->mb_stride + 1;
887 s->b_bidir_back_mv_table =
s->b_bidir_back_mv_table_base +
s->mb_stride + 1;
888 s->b_direct_mv_table =
s->b_direct_mv_table_base +
s->mb_stride + 1;
891 mb_array_size =
s->mb_stride *
s->mb_height;
901 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
906 if (!(tmp1 =
ALLOCZ_ARRAYS(
s->b_field_mv_table_base, 8, mv_table_size)) ||
907 !(tmp2 =
ALLOCZ_ARRAYS(
s->b_field_select_table[0][0], 2 * 4, mv_table_size)) ||
911 s->p_field_select_table[1] =
s->p_field_select_table[0] + 2 * mv_table_size;
912 tmp1 +=
s->mb_stride + 1;
914 for (
int i = 0;
i < 2;
i++) {
915 for (
int j = 0; j < 2; j++) {
916 for (
int k = 0; k < 2; k++) {
917 s->b_field_mv_table[
i][j][k] = tmp1;
918 tmp1 += mv_table_size;
920 s->b_field_select_table[
i][j] = tmp2;
921 tmp2 += 2 * mv_table_size;
926 if (
s->noise_reduction) {
934 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
935 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
937 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
938 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
940 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
941 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
944 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) &&
s->modified_quant)
947 if (
s->slice_context_count > 1) {
951 s->h263_slice_structured = 1;
954 if (CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263) {
956 #if CONFIG_MSMPEG4ENC
963 for (
i = 0;
i < 64;
i++) {
964 int j =
s->idsp.idct_permutation[
i];
977 s->chroma_intra_matrix[j] =
1001 if (
s->b_frame_strategy == 2) {
1002 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1004 if (!
s->tmp_frames[
i])
1008 s->tmp_frames[
i]->width =
s->width >>
s->brd_scale;
1009 s->tmp_frames[
i]->height =
s->height >>
s->brd_scale;
1038 if (
s->input_picture &&
s->reordered_input_picture) {
1054 av_freep(&
s->b_bidir_forw_mv_table_base);
1055 av_freep(&
s->b_bidir_back_mv_table_base);
1058 av_freep(&
s->b_field_select_table[0][0]);
1067 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
1068 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
1069 s->q_chroma_intra_matrix=
NULL;
1070 s->q_chroma_intra_matrix16=
NULL;
1085 #define IS_ENCODER 1
1093 for (
int i = 0;
i < 6;
i++) {
1094 for (
int j = 0; j < 64; j++) {
1096 block[
i][
s->idsp.idct_permutation[j]]);
1110 for (y = 0; y < 16; y++) {
1111 for (x = 0; x < 16; x++) {
1126 h =
s->height & ~15;
1128 for (y = 0; y <
h; y += 16) {
1129 for (x = 0; x <
w; x += 16) {
1136 acc += sae + 500 < sad;
1162 for (
int i = 0;
f->data[
i];
i++) {
1182 int display_picture_number = 0,
ret;
1183 int encoding_delay =
s->max_b_frames ?
s->max_b_frames
1184 : (
s->low_delay ? 0 : 1);
1185 int flush_offset = 1;
1192 display_picture_number =
s->input_picture_number++;
1196 int64_t last =
s->user_specified_pts;
1200 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1205 if (!
s->low_delay && display_picture_number == 1)
1206 s->dts_delta =
pts - last;
1208 s->user_specified_pts =
pts;
1211 s->user_specified_pts =
1212 pts =
s->user_specified_pts + 1;
1214 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1217 pts = display_picture_number;
1221 if (pic_arg->
linesize[0] !=
s->linesize ||
1222 pic_arg->
linesize[1] !=
s->uvlinesize ||
1225 if ((
s->width & 15) || (
s->height & 15))
1233 pic_arg->
linesize[1],
s->linesize,
s->uvlinesize);
1248 for (
int i = 0;
i < 3;
i++) {
1249 ptrdiff_t src_stride = pic_arg->
linesize[
i];
1250 ptrdiff_t dst_stride =
i ?
s->uvlinesize :
s->linesize;
1251 int h_shift =
i ?
s->chroma_x_shift : 0;
1252 int v_shift =
i ?
s->chroma_y_shift : 0;
1255 const uint8_t *
src = pic_arg->
data[
i];
1260 && !
s->progressive_sequence
1261 &&
FFALIGN(
s->height, 32) -
s->height > 16)
1264 if (!
s->avctx->rc_buffer_size)
1267 if (src_stride == dst_stride)
1268 memcpy(
dst,
src, src_stride *
h - src_stride +
w);
1271 uint8_t *dst2 =
dst;
1273 memcpy(dst2,
src,
w);
1278 if ((
s->width & 15) || (
s->height & (vpad-1))) {
1279 s->mpvencdsp.draw_edges(
dst, dst_stride,
1291 }
else if (!
s->reordered_input_picture[1]) {
1297 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1298 if (
s->input_picture[flush_offset])
1301 encoding_delay -= flush_offset - 1;
1306 s->input_picture[
i - flush_offset] =
s->input_picture[
i];
1308 s->input_picture[
i] =
NULL;
1310 s->input_picture[encoding_delay] = pic;
1324 for (plane = 0; plane < 3; plane++) {
1326 const int bw = plane ? 1 : 2;
1327 for (y = 0; y <
s->mb_height * bw; y++) {
1328 for (x = 0; x <
s->mb_width * bw; x++) {
1329 int off = p->
shared ? 0 : 16;
1330 const uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y *
stride) + off;
1331 const uint8_t *rptr =
ref->f->data[plane] + 8 * (x + y *
stride);
1332 int v =
s->frame_skip_cmp_fn(
s, dptr, rptr,
stride, 8);
1334 switch (
FFABS(
s->frame_skip_exp)) {
1335 case 0: score =
FFMAX(score, v);
break;
1336 case 1: score +=
FFABS(v);
break;
1337 case 2: score64 += v * (
int64_t)v;
break;
1348 if (
s->frame_skip_exp < 0)
1349 score64 = pow(score64 / (
double)(
s->mb_width *
s->mb_height),
1350 -1.0/
s->frame_skip_exp);
1354 if (score64 < ((
s->frame_skip_factor * (
int64_t)
s->lambda) >> 8))
1383 const int scale =
s->brd_scale;
1388 int best_b_count = -1;
1402 b_lambda = p_lambda;
1406 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1407 const MPVPicture *pre_input_ptr =
i ?
s->input_picture[
i - 1] :
1410 if (pre_input_ptr) {
1411 const uint8_t *
data[4];
1414 if (!pre_input_ptr->
shared &&
i) {
1420 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[0],
1421 s->tmp_frames[
i]->linesize[0],
1425 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[1],
1426 s->tmp_frames[
i]->linesize[1],
1430 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[2],
1431 s->tmp_frames[
i]->linesize[2],
1438 for (j = 0; j <
s->max_b_frames + 1; j++) {
1442 if (!
s->input_picture[j])
1455 c->mb_decision =
s->avctx->mb_decision;
1456 c->me_cmp =
s->avctx->me_cmp;
1457 c->mb_cmp =
s->avctx->mb_cmp;
1458 c->me_sub_cmp =
s->avctx->me_sub_cmp;
1460 c->time_base =
s->avctx->time_base;
1461 c->max_b_frames =
s->max_b_frames;
1479 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1480 int is_p =
i % (j + 1) == j ||
i ==
s->max_b_frames;
1482 s->tmp_frames[
i + 1]->pict_type = is_p ?
1484 s->tmp_frames[
i + 1]->quality = is_p ? p_lambda : b_lambda;
1503 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1521 return best_b_count;
1535 if (
s->reordered_input_picture[0] || !
s->input_picture[0])
1539 if (
s->frame_skip_threshold ||
s->frame_skip_factor) {
1540 if (
s->picture_in_gop_number <
s->gop_size &&
1553 !
s->next_pic.ptr ||
s->intra_only) {
1554 s->reordered_input_picture[0] =
s->input_picture[0];
1555 s->input_picture[0] =
NULL;
1557 s->reordered_input_picture[0]->coded_picture_number =
1558 s->coded_picture_number++;
1563 for (
int i = 0;
i <
s->max_b_frames + 1;
i++) {
1564 int pict_num =
s->input_picture[0]->display_picture_number +
i;
1566 if (pict_num >=
s->rc_context.num_entries)
1568 if (!
s->input_picture[
i]) {
1573 s->input_picture[
i]->f->pict_type =
1574 s->rc_context.entry[pict_num].new_pict_type;
1578 if (
s->b_frame_strategy == 0) {
1579 b_frames =
s->max_b_frames;
1580 while (b_frames && !
s->input_picture[b_frames])
1582 }
else if (
s->b_frame_strategy == 1) {
1584 for (
i = 1;
i <
s->max_b_frames + 1;
i++) {
1585 if (
s->input_picture[
i] &&
1586 s->input_picture[
i]->b_frame_score == 0) {
1587 s->input_picture[
i]->b_frame_score =
1589 s->input_picture[
i ]->f->data[0],
1590 s->input_picture[
i - 1]->f->data[0],
1594 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1595 if (!
s->input_picture[
i] ||
1596 s->input_picture[
i]->b_frame_score - 1 >
1597 s->mb_num /
s->b_sensitivity)
1601 b_frames =
FFMAX(0,
i - 1);
1604 for (
i = 0;
i < b_frames + 1;
i++) {
1605 s->input_picture[
i]->b_frame_score = 0;
1607 }
else if (
s->b_frame_strategy == 2) {
1617 for (
int i = b_frames - 1;
i >= 0;
i--) {
1618 int type =
s->input_picture[
i]->f->pict_type;
1623 b_frames ==
s->max_b_frames) {
1625 "warning, too many B-frames in a row\n");
1628 if (
s->picture_in_gop_number + b_frames >=
s->gop_size) {
1630 s->gop_size >
s->picture_in_gop_number) {
1631 b_frames =
s->gop_size -
s->picture_in_gop_number - 1;
1643 s->reordered_input_picture[0] =
s->input_picture[b_frames];
1644 s->input_picture[b_frames] =
NULL;
1647 s->reordered_input_picture[0]->coded_picture_number =
1648 s->coded_picture_number++;
1649 for (
int i = 0;
i < b_frames;
i++) {
1650 s->reordered_input_picture[
i + 1] =
s->input_picture[
i];
1651 s->input_picture[
i] =
NULL;
1652 s->reordered_input_picture[
i + 1]->f->pict_type =
1654 s->reordered_input_picture[
i + 1]->coded_picture_number =
1655 s->coded_picture_number++;
1669 s->reordered_input_picture[
i - 1] =
s->reordered_input_picture[
i];
1679 if (
s->reordered_input_picture[0]) {
1680 s->reordered_input_picture[0]->reference =
1683 if (
s->reordered_input_picture[0]->shared ||
s->avctx->rc_buffer_size) {
1697 if (
s->new_pic->data[
i])
1701 s->cur_pic.ptr =
s->reordered_input_picture[0];
1702 s->reordered_input_picture[0] =
NULL;
1703 av_assert1(
s->mb_width ==
s->buffer_pools.alloc_mb_width);
1704 av_assert1(
s->mb_height ==
s->buffer_pools.alloc_mb_height);
1705 av_assert1(
s->mb_stride ==
s->buffer_pools.alloc_mb_stride);
1707 &
s->sc, &
s->buffer_pools,
s->mb_height);
1712 s->picture_number =
s->cur_pic.ptr->display_picture_number;
1723 if (
s->unrestricted_mv &&
1724 s->cur_pic.reference &&
1726 int hshift =
s->chroma_x_shift;
1727 int vshift =
s->chroma_y_shift;
1728 s->mpvencdsp.draw_edges(
s->cur_pic.data[0],
1729 s->cur_pic.linesize[0],
1730 s->h_edge_pos,
s->v_edge_pos,
1733 s->mpvencdsp.draw_edges(
s->cur_pic.data[1],
1734 s->cur_pic.linesize[1],
1735 s->h_edge_pos >> hshift,
1740 s->mpvencdsp.draw_edges(
s->cur_pic.data[2],
1741 s->cur_pic.linesize[2],
1742 s->h_edge_pos >> hshift,
1751 s->last_pict_type =
s->pict_type;
1752 s->last_lambda_for [
s->pict_type] =
s->cur_pic.ptr->f->quality;
1754 s->last_non_b_pict_type =
s->pict_type;
1761 for (intra = 0; intra < 2; intra++) {
1762 if (
s->dct_count[intra] > (1 << 16)) {
1763 for (
i = 0;
i < 64;
i++) {
1764 s->dct_error_sum[intra][
i] >>= 1;
1766 s->dct_count[intra] >>= 1;
1769 for (
i = 0;
i < 64;
i++) {
1770 s->dct_offset[intra][
i] = (
s->noise_reduction *
1771 s->dct_count[intra] +
1772 s->dct_error_sum[intra][
i] / 2) /
1773 (
s->dct_error_sum[intra][
i] + 1);
1780 s->cur_pic.ptr->f->pict_type =
s->pict_type;
1787 if (
s->dct_error_sum) {
1794 const AVFrame *pic_arg,
int *got_packet)
1797 int stuffing_count,
ret;
1798 int context_count =
s->slice_context_count;
1802 s->vbv_ignore_qmax = 0;
1804 s->picture_in_gop_number++;
1814 if (
s->new_pic->data[0]) {
1815 int growing_buffer = context_count == 1 && !
s->data_partitioning;
1816 size_t pkt_size = 10000 +
s->mb_width *
s->mb_height *
1829 s->mb_width*
s->mb_height*12);
1830 if (!
s->mb_info_ptr)
1832 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1835 s->pict_type =
s->new_pic->pict_type;
1840 if (growing_buffer) {
1850 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
s->out_format ==
FMT_MJPEG)
1860 s->lambda <
s->lmax) {
1861 s->next_lambda =
FFMAX(
s->lambda + min_step,
s->lambda *
1862 (
s->qscale + 1) /
s->qscale);
1863 if (
s->adaptive_quant) {
1865 for (
i = 0;
i <
s->mb_height *
s->mb_stride;
i++)
1866 s->lambda_table[
i] =
1867 FFMAX(
s->lambda_table[
i] + min_step,
1868 s->lambda_table[
i] * (
s->qscale + 1) /
1874 s->no_rounding ^=
s->flipflop_rounding;
1877 s->time_base =
s->last_time_base;
1878 s->last_non_b_time =
s->time -
s->pp_time;
1880 s->vbv_ignore_qmax = 1;
1900 s->misc_bits +
s->i_tex_bits +
1906 s->stuffing_bits = 8*stuffing_count;
1907 if (stuffing_count) {
1913 switch (
s->codec_id) {
1916 while (stuffing_count--) {
1923 stuffing_count -= 4;
1924 while (stuffing_count--) {
1930 s->stuffing_bits = 0;
1948 int minbits =
s->frame_bits - 8 *
1949 (
s->vbv_delay_pos - 1);
1950 double bits =
s->rc_context.buffer_index + minbits - inbits;
1951 uint8_t *
const vbv_delay_ptr =
s->pb.buf +
s->vbv_delay_pos;
1955 "Internal error, negative bits\n");
1967 vbv_delay_ptr[0] &= 0xF8;
1970 vbv_delay_ptr[2] &= 0x07;
1979 (uint8_t*)props, props_size);
1985 s->total_bits +=
s->frame_bits;
1987 pkt->
pts =
s->cur_pic.ptr->f->pts;
1990 if (!
s->cur_pic.ptr->coded_picture_number)
2023 int n,
int threshold)
2025 static const char tab[64] = {
2026 3, 2, 2, 1, 1, 1, 1, 1,
2027 1, 1, 1, 1, 1, 1, 1, 1,
2028 1, 1, 1, 1, 1, 1, 1, 1,
2029 0, 0, 0, 0, 0, 0, 0, 0,
2030 0, 0, 0, 0, 0, 0, 0, 0,
2031 0, 0, 0, 0, 0, 0, 0, 0,
2032 0, 0, 0, 0, 0, 0, 0, 0,
2033 0, 0, 0, 0, 0, 0, 0, 0
2038 int16_t *
block =
s->block[n];
2039 const int last_index =
s->block_last_index[n];
2042 if (threshold < 0) {
2044 threshold = -threshold;
2049 if (last_index <= skip_dc - 1)
2052 for (
i = 0;
i <= last_index;
i++) {
2053 const int j =
s->intra_scantable.permutated[
i];
2056 if (skip_dc &&
i == 0)
2060 }
else if (
level > 1) {
2066 if (score >= threshold)
2068 for (
i = skip_dc;
i <= last_index;
i++) {
2069 const int j =
s->intra_scantable.permutated[
i];
2073 s->block_last_index[n] = 0;
2075 s->block_last_index[n] = -1;
2082 const int maxlevel =
s->max_qcoeff;
2083 const int minlevel =
s->min_qcoeff;
2091 for (;
i <= last_index;
i++) {
2092 const int j =
s->intra_scantable.permutated[
i];
2095 if (
level > maxlevel) {
2098 }
else if (
level < minlevel) {
2108 "warning, clipping %d dct coefficients to %d..%d\n",
2116 for (y = 0; y < 8; y++) {
2117 for (x = 0; x < 8; x++) {
2123 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2124 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2125 int v = ptr[x2 + y2 *
stride];
2137 int motion_x,
int motion_y,
2138 int mb_block_height,
2147 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2148 (s)->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2150 int16_t orig[12][64];
2151 const int mb_x =
s->mb_x;
2152 const int mb_y =
s->mb_y;
2156 int uv_dct_offset =
s->uvlinesize * 8;
2157 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2158 ptrdiff_t wrap_y, wrap_c;
2160 for (
i = 0;
i < mb_block_count;
i++)
2161 skip_dct[
i] =
s->skipdct;
2163 if (
s->adaptive_quant) {
2164 const int last_qp =
s->qscale;
2165 const int mb_xy =
mb_x +
mb_y *
s->mb_stride;
2167 s->lambda =
s->lambda_table[mb_xy];
2172 s->dquant =
s->cur_pic.qscale_table[mb_xy] - last_qp;
2193 wrap_y =
s->linesize;
2194 wrap_c =
s->uvlinesize;
2195 ptr_y =
s->new_pic->data[0] +
2197 ptr_cb =
s->new_pic->data[1] +
2198 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2199 ptr_cr =
s->new_pic->data[2] +
2200 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2203 uint8_t *ebuf =
s->sc.edge_emu_buffer + 38 * wrap_y;
2206 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2209 s->width,
s->height);
2211 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2213 mb_block_width, mb_block_height,
2214 mb_x * mb_block_width,
mb_y * mb_block_height,
2216 ptr_cb = ebuf + 16 * wrap_y;
2217 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2219 mb_block_width, mb_block_height,
2220 mb_x * mb_block_width,
mb_y * mb_block_height,
2222 ptr_cr = ebuf + 16 * wrap_y + 16;
2227 int progressive_score, interlaced_score;
2229 s->interlaced_dct = 0;
2230 progressive_score =
s->ildct_cmp[1](
s, ptr_y,
NULL, wrap_y, 8) +
2231 s->ildct_cmp[1](
s, ptr_y + wrap_y * 8,
2232 NULL, wrap_y, 8) - 400;
2234 if (progressive_score > 0) {
2235 interlaced_score =
s->ildct_cmp[1](
s, ptr_y,
2236 NULL, wrap_y * 2, 8) +
2237 s->ildct_cmp[1](
s, ptr_y + wrap_y,
2238 NULL, wrap_y * 2, 8);
2239 if (progressive_score > interlaced_score) {
2240 s->interlaced_dct = 1;
2243 uv_dct_offset = wrap_c;
2252 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2253 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2254 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2255 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2261 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2262 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2264 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2265 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2267 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2268 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2269 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2270 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2271 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2272 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2278 uint8_t *dest_y, *dest_cb, *dest_cr;
2280 dest_y =
s->dest[0];
2281 dest_cb =
s->dest[1];
2282 dest_cr =
s->dest[2];
2285 op_pix =
s->hdsp.put_pixels_tab;
2286 op_qpix =
s->qdsp.put_qpel_pixels_tab;
2288 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
2289 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
2296 op_pix =
s->hdsp.avg_pixels_tab;
2297 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
2306 int progressive_score, interlaced_score;
2308 s->interlaced_dct = 0;
2309 progressive_score =
s->ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2310 s->ildct_cmp[0](
s, dest_y + wrap_y * 8,
2315 progressive_score -= 400;
2317 if (progressive_score > 0) {
2318 interlaced_score =
s->ildct_cmp[0](
s, dest_y, ptr_y,
2320 s->ildct_cmp[0](
s, dest_y + wrap_y,
2324 if (progressive_score > interlaced_score) {
2325 s->interlaced_dct = 1;
2328 uv_dct_offset = wrap_c;
2336 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2337 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2340 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2347 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2348 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2350 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2351 dest_cb + uv_dct_offset, wrap_c);
2352 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2353 dest_cr + uv_dct_offset, wrap_c);
2357 if (
s->mc_mb_var[
s->mb_stride *
mb_y +
mb_x] < 2 *
s->qscale *
s->qscale) {
2359 if (
s->sad_cmp[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->qscale)
2361 if (
s->sad_cmp[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->qscale)
2364 wrap_y, 8) < 20 *
s->qscale)
2367 wrap_y, 8) < 20 *
s->qscale)
2369 if (
s->sad_cmp[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->qscale)
2371 if (
s->sad_cmp[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->qscale)
2374 if (
s->sad_cmp[1](
NULL, ptr_cb + uv_dct_offset,
2375 dest_cb + uv_dct_offset,
2376 wrap_c, 8) < 20 *
s->qscale)
2378 if (
s->sad_cmp[1](
NULL, ptr_cr + uv_dct_offset,
2379 dest_cr + uv_dct_offset,
2380 wrap_c, 8) < 20 *
s->qscale)
2386 if (
s->quantizer_noise_shaping) {
2407 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2413 for (
i = 0;
i < mb_block_count;
i++) {
2416 s->block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->qscale, &
overflow);
2425 s->block_last_index[
i] = -1;
2427 if (
s->quantizer_noise_shaping) {
2428 for (
i = 0;
i < mb_block_count;
i++) {
2430 s->block_last_index[
i] =
2432 orig[
i],
i,
s->qscale);
2437 if (
s->luma_elim_threshold && !
s->mb_intra)
2438 for (
i = 0;
i < 4;
i++)
2440 if (
s->chroma_elim_threshold && !
s->mb_intra)
2441 for (
i = 4;
i < mb_block_count;
i++)
2445 for (
i = 0;
i < mb_block_count;
i++) {
2446 if (
s->block_last_index[
i] == -1)
2447 s->coded_score[
i] = INT_MAX / 256;
2453 s->block_last_index[4] =
2454 s->block_last_index[5] = 0;
2456 s->block[5][0] = (1024 +
s->c_dc_scale / 2) /
s->c_dc_scale;
2458 for (
i=6;
i<12;
i++) {
2459 s->block_last_index[
i] = 0;
2460 s->block[
i][0] =
s->block[4][0];
2467 for (
i = 0;
i < mb_block_count;
i++) {
2469 if (
s->block_last_index[
i] > 0) {
2470 for (j = 63; j > 0; j--) {
2471 if (
s->block[
i][
s->intra_scantable.permutated[j]])
2474 s->block_last_index[
i] = j;
2480 switch(
s->codec_id){
2483 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2487 if (CONFIG_MPEG4_ENCODER)
2493 if (CONFIG_MSMPEG4ENC)
2497 if (CONFIG_WMV2_ENCODER)
2501 if (CONFIG_H261_ENCODER)
2509 if (CONFIG_H263_ENCODER)
2512 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2519 if (CONFIG_SPEEDHQ_ENCODER)
2542 memcpy(d->
last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2569 memcpy(d->
mv,
s->mv, 2*4*2*
sizeof(
int));
2570 memcpy(d->
last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2589 if(
s->data_partitioning){
2604 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2607 uint8_t *dest_backup[3];
2611 s->block=
s->blocks[*next_block];
2612 s->pb=
pb[*next_block];
2613 if(
s->data_partitioning){
2614 s->pb2 =
pb2 [*next_block];
2615 s->tex_pb=
tex_pb[*next_block];
2619 memcpy(dest_backup,
s->dest,
sizeof(
s->dest));
2620 s->dest[0] =
s->sc.rd_scratchpad;
2621 s->dest[1] =
s->sc.rd_scratchpad + 16*
s->linesize;
2622 s->dest[2] =
s->sc.rd_scratchpad + 16*
s->linesize + 8;
2629 if(
s->data_partitioning){
2637 score *=
s->lambda2;
2642 memcpy(
s->dest, dest_backup,
sizeof(
s->dest));
2660 else if(
w==8 &&
h==8)
2677 int chroma_mb_w =
w >>
s->chroma_x_shift;
2678 int chroma_mb_h =
h >>
s->chroma_y_shift;
2680 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
2681 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
2684 return s->n_sse_cmp[0](
s,
s->new_pic->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2685 s->dest[0],
s->linesize, 16) +
2686 s->n_sse_cmp[1](
s,
s->new_pic->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2687 s->dest[1],
s->uvlinesize, chroma_mb_h) +
2688 s->n_sse_cmp[1](
s,
s->new_pic->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2689 s->dest[2],
s->uvlinesize, chroma_mb_h);
2691 return sse(
s,
s->new_pic->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2692 s->dest[0],
w,
h,
s->linesize) +
2693 sse(
s,
s->new_pic->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2694 s->dest[1],
w >>
s->chroma_x_shift,
h >>
s->chroma_y_shift,
s->uvlinesize) +
2695 sse(
s,
s->new_pic->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2696 s->dest[2],
w >>
s->chroma_x_shift,
h >>
s->chroma_y_shift,
s->uvlinesize);
2704 s->me.dia_size=
s->avctx->pre_dia_size;
2705 s->first_slice_line=1;
2706 for(
s->mb_y=
s->end_mb_y-1;
s->mb_y >=
s->start_mb_y;
s->mb_y--) {
2707 for(
s->mb_x=
s->mb_width-1;
s->mb_x >=0 ;
s->mb_x--) {
2710 s->first_slice_line=0;
2721 s->me.dia_size=
s->avctx->dia_size;
2722 s->first_slice_line=1;
2723 for(
s->mb_y=
s->start_mb_y;
s->mb_y <
s->end_mb_y;
s->mb_y++) {
2726 for(
s->mb_x=0;
s->mb_x <
s->mb_width;
s->mb_x++) {
2727 s->block_index[0]+=2;
2728 s->block_index[1]+=2;
2729 s->block_index[2]+=2;
2730 s->block_index[3]+=2;
2738 s->first_slice_line=0;
2751 const uint8_t *pix =
s->new_pic->data[0] + (yy *
s->linesize) + xx;
2753 int sum =
s->mpvencdsp.pix_sum(pix,
s->linesize);
2755 varc = (
s->mpvencdsp.pix_norm1(pix,
s->linesize) -
2756 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2758 s->mb_var [
s->mb_stride *
mb_y +
mb_x] = varc;
2759 s->mb_mean[
s->mb_stride *
mb_y +
mb_x] = (sum+128)>>8;
2760 s->me.mb_var_sum_temp += varc;
2768 if(
s->partitioned_frame){
2773 }
else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2776 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->out_format ==
FMT_SPEEDHQ) {
2788 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2790 int mba =
s->mb_x +
s->mb_width * (
s->mb_y %
s->gob_index);
2791 int gobn =
s->mb_y /
s->gob_index;
2793 if (CONFIG_H263_ENCODER)
2795 bytestream_put_le32(&ptr,
offset);
2796 bytestream_put_byte(&ptr,
s->qscale);
2797 bytestream_put_byte(&ptr, gobn);
2798 bytestream_put_le16(&ptr, mba);
2799 bytestream_put_byte(&ptr, pred_x);
2800 bytestream_put_byte(&ptr, pred_y);
2802 bytestream_put_byte(&ptr, 0);
2803 bytestream_put_byte(&ptr, 0);
2811 s->mb_info_size += 12;
2812 s->prev_mb_info =
s->last_mb_info;
2824 if (!
s->mb_info_size)
2825 s->mb_info_size += 12;
2832 &&
s->slice_context_count == 1
2833 &&
s->pb.buf ==
s->avctx->internal->byte_buffer) {
2834 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2836 uint8_t *new_buffer =
NULL;
2837 int new_buffer_size = 0;
2839 if ((
s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2847 s->avctx->internal->byte_buffer_size + size_increase);
2851 memcpy(new_buffer,
s->avctx->internal->byte_buffer,
s->avctx->internal->byte_buffer_size);
2852 av_free(
s->avctx->internal->byte_buffer);
2853 s->avctx->internal->byte_buffer = new_buffer;
2854 s->avctx->internal->byte_buffer_size = new_buffer_size;
2856 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2866 int chr_h= 16>>
s->chroma_y_shift;
2890 s->last_dc[
i] = 128 <<
s->intra_dc_precision;
2892 s->encoding_error[
i] = 0;
2895 s->last_dc[0] = 128*8/13;
2896 s->last_dc[1] = 128*8/14;
2897 s->last_dc[2] = 128*8/14;
2900 memset(
s->last_mv, 0,
sizeof(
s->last_mv));
2904 switch(
s->codec_id){
2908 if (CONFIG_H263_ENCODER)
2912 if(CONFIG_MPEG4_ENCODER &&
s->partitioned_frame)
2919 s->first_slice_line = 1;
2920 s->ptr_lastgob =
s->pb.buf;
2921 for (mb_y_order =
s->start_mb_y; mb_y_order < s->
end_mb_y; mb_y_order++) {
2925 if (first_in_slice && mb_y_order !=
s->start_mb_y)
2927 s->last_dc[0] =
s->last_dc[1] =
s->last_dc[2] = 1024 <<
s->intra_dc_precision;
2943 int size_increase =
s->avctx->internal->byte_buffer_size/4
2951 if(
s->data_partitioning){
2965 xy=
s->mb_y*
s->mb_stride +
s->mb_x;
2971 int current_packet_size, is_gob_start;
2974 - (
s->ptr_lastgob -
s->pb.buf);
2976 is_gob_start =
s->rtp_payload_size &&
2977 current_packet_size >=
s->rtp_payload_size &&
2980 if(
s->start_mb_y ==
mb_y &&
mb_y > 0 &&
mb_x==0) is_gob_start=1;
2982 switch(
s->codec_id){
2985 if(!
s->h263_slice_structured)
2986 if(
s->mb_x ||
s->mb_y%
s->gob_index) is_gob_start=0;
2989 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2991 if(
s->mb_skip_run) is_gob_start=0;
2994 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
3010 if (
s->error_rate &&
s->resync_mb_x +
s->resync_mb_y > 0) {
3012 int d = 100 /
s->error_rate;
3014 current_packet_size=0;
3015 s->pb.buf_ptr=
s->ptr_lastgob;
3020 switch(
s->codec_id){
3022 if (CONFIG_MPEG4_ENCODER) {
3029 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3036 if (CONFIG_H263_ENCODER) {
3045 s->misc_bits+=
bits -
s->last_bits;
3049 s->ptr_lastgob += current_packet_size;
3050 s->first_slice_line=1;
3051 s->resync_mb_x=
mb_x;
3052 s->resync_mb_y=
mb_y;
3056 if( (
s->resync_mb_x ==
s->mb_x)
3057 &&
s->resync_mb_y+1 ==
s->mb_y){
3058 s->first_slice_line=0;
3068 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3074 if(
s->data_partitioning){
3075 backup_s.pb2=
s->pb2;
3076 backup_s.tex_pb=
s->tex_pb;
3083 s->mv[0][0][0] =
s->p_mv_table[xy][0];
3084 s->mv[0][0][1] =
s->p_mv_table[xy][1];
3086 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3093 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3094 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3095 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3098 &dmin, &next_block, 0, 0);
3107 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3114 s->mv[0][
i][0] =
s->cur_pic.motion_val[0][
s->block_index[
i]][0];
3115 s->mv[0][
i][1] =
s->cur_pic.motion_val[0][
s->block_index[
i]][1];
3118 &dmin, &next_block, 0, 0);
3124 s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3125 s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3127 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3133 s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3134 s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3136 &dmin, &next_block,
s->mv[1][0][0],
s->mv[1][0][1]);
3142 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3143 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3144 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3145 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3147 &dmin, &next_block, 0, 0);
3154 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3155 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3156 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3159 &dmin, &next_block, 0, 0);
3166 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3167 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3168 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3171 &dmin, &next_block, 0, 0);
3177 for(dir=0; dir<2; dir++){
3179 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3180 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3181 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3185 &dmin, &next_block, 0, 0);
3194 &dmin, &next_block, 0, 0);
3195 if(
s->h263_pred ||
s->h263_aic){
3197 s->mbintra_table[
mb_x +
mb_y*
s->mb_stride]=1;
3205 const int last_qp= backup_s.qscale;
3209 static const int dquant_tab[4]={-1,1,-2,2};
3210 int storecoefs =
s->mb_intra &&
s->dc_val[0];
3218 s->mv[0][0][0] = best_s.
mv[0][0][0];
3219 s->mv[0][0][1] = best_s.
mv[0][0][1];
3220 s->mv[1][0][0] = best_s.
mv[1][0][0];
3221 s->mv[1][0][1] = best_s.
mv[1][0][1];
3224 for(; qpi<4; qpi++){
3225 int dquant= dquant_tab[qpi];
3227 if(qp < s->
avctx->
qmin || qp >
s->avctx->qmax)
3232 dc[
i]=
s->dc_val[0][
s->block_index[
i] ];
3233 memcpy(ac[
i],
s->ac_val[0][
s->block_index[
i]],
sizeof(int16_t)*16);
3238 &dmin, &next_block,
s->mv[mvdir][0][0],
s->mv[mvdir][0][1]);
3242 s->dc_val[0][
s->block_index[
i] ]=
dc[
i];
3243 memcpy(
s->ac_val[0][
s->block_index[
i]], ac[
i],
sizeof(int16_t)*16);
3251 int mx=
s->b_direct_mv_table[xy][0];
3252 int my=
s->b_direct_mv_table[xy][1];
3254 backup_s.dquant = 0;
3259 &dmin, &next_block,
mx,
my);
3262 backup_s.dquant = 0;
3267 &dmin, &next_block, 0, 0);
3272 coded |=
s->block_last_index[
i];
3275 memcpy(
s->mv, best_s.
mv,
sizeof(
s->mv));
3297 &dmin, &next_block,
mx,
my);
3302 s->cur_pic.qscale_table[xy] = best_s.
qscale;
3308 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3311 if(
s->data_partitioning){
3314 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3315 s->pb2= backup_s.pb2;
3319 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3320 s->tex_pb= backup_s.tex_pb;
3324 if (CONFIG_H263_ENCODER &&
3329 s->hdsp.put_pixels_tab[0][0](
s->dest[0],
s->sc.rd_scratchpad ,
s->linesize ,16);
3330 s->hdsp.put_pixels_tab[1][0](
s->dest[1],
s->sc.rd_scratchpad + 16*
s->linesize ,
s->uvlinesize, 8);
3331 s->hdsp.put_pixels_tab[1][0](
s->dest[2],
s->sc.rd_scratchpad + 16*
s->linesize + 8,
s->uvlinesize, 8);
3337 int motion_x = 0, motion_y = 0;
3345 motion_x=
s->mv[0][0][0] = 0;
3346 motion_y=
s->mv[0][0][1] = 0;
3351 motion_x=
s->mv[0][0][0] =
s->p_mv_table[xy][0];
3352 motion_y=
s->mv[0][0][1] =
s->p_mv_table[xy][1];
3359 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3360 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3361 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3369 s->mv[0][
i][0] =
s->cur_pic.motion_val[0][
s->block_index[
i]][0];
3370 s->mv[0][
i][1] =
s->cur_pic.motion_val[0][
s->block_index[
i]][1];
3374 if (CONFIG_MPEG4_ENCODER) {
3377 motion_x=
s->b_direct_mv_table[xy][0];
3378 motion_y=
s->b_direct_mv_table[xy][1];
3383 if (CONFIG_MPEG4_ENCODER) {
3392 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3393 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3394 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3395 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3400 motion_x=
s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3401 motion_y=
s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3406 motion_x=
s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3407 motion_y=
s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3414 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3415 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3416 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3424 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3425 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3426 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3433 for(dir=0; dir<2; dir++){
3435 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3436 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3437 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3448 s->last_mv_dir =
s->mv_dir;
3450 if (CONFIG_H263_ENCODER &&
3459 s->p_mv_table[xy][0]=0;
3460 s->p_mv_table[xy][1]=0;
3467 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
3468 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
3470 s->encoding_error[0] +=
sse(
3471 s,
s->new_pic->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
3472 s->dest[0],
w,
h,
s->linesize);
3473 s->encoding_error[1] +=
sse(
3474 s,
s->new_pic->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3475 s->dest[1],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3476 s->encoding_error[2] +=
sse(
3477 s,
s->new_pic->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3478 s->dest[2],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3481 if(CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
3484 ff_dlog(
s->avctx,
"MB %d %d bits\n",
3489 #if CONFIG_MSMPEG4ENC
3501 #define MERGE(field) dst->field += src->field; src->field=0
3522 if (
dst->noise_reduction){
3523 for(
i=0;
i<64;
i++){
3536 if (
s->next_lambda){
3537 s->cur_pic.ptr->f->quality =
s->next_lambda;
3538 if(!dry_run)
s->next_lambda= 0;
3539 }
else if (!
s->fixed_qscale) {
3541 s->cur_pic.ptr->f->quality =
quality;
3542 if (
s->cur_pic.ptr->f->quality < 0)
3546 if(
s->adaptive_quant){
3549 switch(
s->codec_id){
3551 if (CONFIG_MPEG4_ENCODER)
3557 if (CONFIG_H263_ENCODER)
3562 s->lambda=
s->lambda_table[0];
3565 s->lambda =
s->cur_pic.ptr->f->quality;
3573 s->time =
s->cur_pic.ptr->f->pts *
s->avctx->time_base.num;
3576 s->pb_time=
s->pp_time - (
s->last_non_b_time -
s->time);
3579 s->pp_time=
s->time -
s->last_non_b_time;
3580 s->last_non_b_time=
s->time;
3589 int context_count =
s->slice_context_count;
3592 s->me.mb_var_sum_temp =
3593 s->me.mc_mb_var_sum_temp = 0;
3602 s->me.scene_change_score=0;
3607 s->no_rounding =
s->msmpeg4_version >=
MSMP4_V3;
3609 s->no_rounding ^=
s->flipflop_rounding;
3618 s->lambda=
s->last_lambda_for[
s->pict_type];
3620 s->lambda=
s->last_lambda_for[
s->last_non_b_pict_type];
3625 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
3626 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
3627 s->q_chroma_intra_matrix =
s->q_intra_matrix;
3628 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
3634 for (
int i = 0;
i < context_count;
i++) {
3636 uint8_t *start, *end;
3655 s->lambda = (
s->lambda *
s->me_penalty_compensation + 128) >> 8;
3656 s->lambda2 = (
s->lambda2 * (
int64_t)
s->me_penalty_compensation + 128) >> 8;
3667 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3670 if(!
s->fixed_qscale){
3672 s->avctx->execute(
s->avctx,
mb_var_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3675 for(
i=1;
i<context_count;
i++){
3678 s->mc_mb_var_sum =
s->me.mc_mb_var_sum_temp;
3679 s->mb_var_sum =
s->me. mb_var_sum_temp;
3682 if (
s->me.scene_change_score >
s->scenechange_threshold &&
3685 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3689 ff_dlog(
s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3690 s->mb_var_sum,
s->mc_mb_var_sum);
3731 for(dir=0; dir<2; dir++){
3737 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3748 if (
s->qscale < 3 &&
s->max_qcoeff <= 128 &&
3757 if (
s->avctx->intra_matrix) {
3759 luma_matrix =
s->avctx->intra_matrix;
3761 if (
s->avctx->chroma_intra_matrix)
3762 chroma_matrix =
s->avctx->chroma_intra_matrix;
3766 int j =
s->idsp.idct_permutation[
i];
3768 s->chroma_intra_matrix[j] =
av_clip_uint8((chroma_matrix[
i] *
s->qscale) >> 3);
3771 s->y_dc_scale_table=
3773 s->chroma_intra_matrix[0] =
3776 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3778 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3782 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};
3783 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};
3784 for (
int i = 1;
i < 64;
i++) {
3790 s->y_dc_scale_table = y;
3791 s->c_dc_scale_table =
c;
3792 s->intra_matrix[0] = 13;
3793 s->chroma_intra_matrix[0] = 14;
3795 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3797 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3807 s->cur_pic.ptr->f->pict_type =
s->pict_type;
3810 s->picture_in_gop_number=0;
3812 s->mb_x =
s->mb_y = 0;
3814 switch(
s->out_format) {
3815 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
3821 if (CONFIG_SPEEDHQ_ENCODER)
3825 if (CONFIG_H261_ENCODER)
3831 #if CONFIG_MSMPEG4ENC
3835 else if (CONFIG_MPEG4_ENCODER &&
s->h263_pred) {
3848 else if (CONFIG_H263_ENCODER)
3852 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3859 s->header_bits=
bits -
s->last_bits;
3861 for(
i=1;
i<context_count;
i++){
3864 s->avctx->execute(
s->avctx,
encode_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3865 for(
i=1;
i<context_count;
i++){
3866 if (
s->pb.buf_end ==
s->thread_context[
i]->pb.buf)
3875 const int intra=
s->mb_intra;
3878 s->dct_count[intra]++;
3880 for(
i=0;
i<64;
i++){
3885 s->dct_error_sum[intra][
i] +=
level;
3886 level -=
s->dct_offset[intra][
i];
3889 s->dct_error_sum[intra][
i] -=
level;
3890 level +=
s->dct_offset[intra][
i];
3899 int16_t *
block,
int n,
3903 const uint8_t *scantable;
3904 const uint8_t *perm_scantable;
3906 unsigned int threshold1, threshold2;
3918 int coeff_count[64];
3919 int qmul, qadd, start_i, last_non_zero,
i,
dc;
3920 const int esc_length=
s->ac_esc_length;
3921 const uint8_t *length, *last_length;
3927 if(
s->dct_error_sum)
3933 else mpeg2_qscale =
qscale << 1;
3937 scantable=
s->intra_scantable.scantable;
3938 perm_scantable=
s->intra_scantable.permutated;
3955 qmat = n < 4 ?
s->q_intra_matrix[
qscale] :
s->q_chroma_intra_matrix[
qscale];
3956 matrix = n < 4 ?
s->intra_matrix :
s->chroma_intra_matrix;
3960 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
3961 length =
s->intra_chroma_ac_vlc_length;
3962 last_length=
s->intra_chroma_ac_vlc_last_length;
3964 length =
s->intra_ac_vlc_length;
3965 last_length=
s->intra_ac_vlc_last_length;
3968 scantable=
s->inter_scantable.scantable;
3969 perm_scantable=
s->inter_scantable.permutated;
3972 qmat =
s->q_inter_matrix[
qscale];
3974 length =
s->inter_ac_vlc_length;
3975 last_length=
s->inter_ac_vlc_last_length;
3980 threshold2= (threshold1<<1);
3982 for(
i=63;
i>=start_i;
i--) {
3983 const int j = scantable[
i];
3986 if(((
unsigned)(
level+threshold1))>threshold2){
3992 for(
i=start_i;
i<=last_non_zero;
i++) {
3993 const int j = scantable[
i];
3998 if(((
unsigned)(
level+threshold1))>threshold2){
4021 if(last_non_zero < start_i){
4022 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4023 return last_non_zero;
4026 score_tab[start_i]= 0;
4027 survivor[0]= start_i;
4030 for(
i=start_i;
i<=last_non_zero;
i++){
4031 int level_index, j, zero_distortion;
4033 int best_score=256*256*256*120;
4037 zero_distortion= dct_coeff*dct_coeff;
4039 for(level_index=0; level_index < coeff_count[
i]; level_index++){
4048 unquant_coeff= alevel*qmul + qadd;
4050 j =
s->idsp.idct_permutation[scantable[
i]];
4051 unquant_coeff = alevel *
matrix[j] * 8;
4053 j =
s->idsp.idct_permutation[scantable[
i]];
4055 unquant_coeff = (int)( alevel * mpeg2_qscale *
matrix[j]) >> 4;
4056 unquant_coeff = (unquant_coeff - 1) | 1;
4058 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int)
matrix[j])) >> 5;
4059 unquant_coeff = (unquant_coeff - 1) | 1;
4064 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4066 if((
level&(~127)) == 0){
4067 for(j=survivor_count-1; j>=0; j--){
4068 int run=
i - survivor[j];
4070 score += score_tab[
i-
run];
4072 if(score < best_score){
4075 level_tab[
i+1]=
level-64;
4080 for(j=survivor_count-1; j>=0; j--){
4081 int run=
i - survivor[j];
4083 score += score_tab[
i-
run];
4084 if(score < last_score){
4087 last_level=
level-64;
4093 distortion += esc_length*
lambda;
4094 for(j=survivor_count-1; j>=0; j--){
4095 int run=
i - survivor[j];
4096 int score= distortion + score_tab[
i-
run];
4098 if(score < best_score){
4101 level_tab[
i+1]=
level-64;
4106 for(j=survivor_count-1; j>=0; j--){
4107 int run=
i - survivor[j];
4108 int score= distortion + score_tab[
i-
run];
4109 if(score < last_score){
4112 last_level=
level-64;
4120 score_tab[
i+1]= best_score;
4123 if(last_non_zero <= 27){
4124 for(; survivor_count; survivor_count--){
4125 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4129 for(; survivor_count; survivor_count--){
4130 if(score_tab[ survivor[survivor_count-1] ] <= best_score +
lambda)
4135 survivor[ survivor_count++ ]=
i+1;
4139 last_score= 256*256*256*120;
4140 for(
i= survivor[0];
i<=last_non_zero + 1;
i++){
4141 int score= score_tab[
i];
4145 if(score < last_score){
4148 last_level= level_tab[
i];
4149 last_run= run_tab[
i];
4154 s->coded_score[n] = last_score;
4157 last_non_zero= last_i - 1;
4158 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4160 if(last_non_zero < start_i)
4161 return last_non_zero;
4163 if(last_non_zero == 0 && start_i == 0){
4165 int best_score=
dc *
dc;
4167 for(
i=0;
i<coeff_count[0];
i++){
4170 int unquant_coeff, score, distortion;
4173 unquant_coeff= (alevel*qmul + qadd)>>3;
4175 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((int)
matrix[0])) >> 5;
4176 unquant_coeff = (unquant_coeff - 1) | 1;
4178 unquant_coeff = (unquant_coeff + 4) >> 3;
4179 unquant_coeff<<= 3 + 3;
4181 distortion= (unquant_coeff -
dc) * (unquant_coeff -
dc);
4184 else score= distortion + esc_length*
lambda;
4186 if(score < best_score){
4188 best_level=
level - 64;
4191 block[0]= best_level;
4192 s->coded_score[n] = best_score -
dc*
dc;
4193 if(best_level == 0)
return -1;
4194 else return last_non_zero;
4200 block[ perm_scantable[last_non_zero] ]= last_level;
4203 for(;
i>start_i;
i -= run_tab[
i] + 1){
4204 block[ perm_scantable[
i-1] ]= level_tab[
i];
4207 return last_non_zero;