34 #include "config_components.h"
81 #define QUANT_BIAS_SHIFT 8
83 #define QMAT_SHIFT_MMX 16
109 uint16_t (*
qmat16)[2][64],
110 const uint16_t *quant_matrix,
111 int bias,
int qmin,
int qmax,
int intra)
122 else qscale2 =
qscale << 1;
129 for (
i = 0;
i < 64;
i++) {
130 const int j =
s->idsp.idct_permutation[
i];
131 int64_t den = (int64_t) qscale2 * quant_matrix[j];
141 for (
i = 0;
i < 64;
i++) {
142 const int j =
s->idsp.idct_permutation[
i];
143 int64_t den =
ff_aanscales[
i] * (int64_t) qscale2 * quant_matrix[j];
153 for (
i = 0;
i < 64;
i++) {
154 const int j =
s->idsp.idct_permutation[
i];
155 int64_t den = (int64_t) qscale2 * quant_matrix[j];
175 for (
i = intra;
i < 64;
i++) {
187 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
194 if (
s->q_scale_type == 1 && 0) {
196 int bestdiff=INT_MAX;
204 if (
diff < bestdiff) {
213 s->qscale =
av_clip(
s->qscale,
s->avctx->qmin,
s->vbv_ignore_qmax ? 31 :
s->avctx->qmax);
226 for (
i = 0;
i < 64;
i++) {
238 int8_t *
const qscale_table =
s->current_picture.qscale_table;
241 for (
i = 0;
i <
s->mb_num;
i++) {
242 unsigned int lam =
s->lambda_table[
s->mb_index2xy[
i]];
244 qscale_table[
s->mb_index2xy[
i]] =
av_clip(qp,
s->avctx->qmin,
252 #define COPY(a) dst->a= src->a
268 for (
int i = -16;
i < 16;
i++)
287 s->input_picture_number = 0;
288 s->picture_in_gop_number = 0;
297 if (CONFIG_H263_ENCODER)
299 if (!
s->dct_quantize)
303 s->fast_dct_quantize =
s->dct_quantize;
304 if (
s->avctx->trellis)
316 int mb_array_size, mv_table_size;
344 "keyframe interval too large!, reducing it from %d to %d\n",
356 "max b frames must be 0 or positive for mpegvideo based encoders\n");
367 s->rtp_mode = !!
s->rtp_payload_size;
371 if (
s->intra_dc_precision < 0) {
372 s->intra_dc_precision += 8;
373 }
else if (
s->intra_dc_precision >= 8)
374 s->intra_dc_precision -= 8;
376 if (
s->intra_dc_precision < 0) {
378 "intra dc precision must be positive, note some applications use"
379 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
389 if (
s->gop_size <= 1) {
443 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
460 "impossible bitrate constraints, this will fail\n");
470 if (!
s->fixed_qscale &&
476 if (nbt <= INT_MAX) {
489 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
490 "specified vbv buffer is too large for the given bitrate!\n");
502 "OBMC is only supported with simple mb decision\n");
517 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
570 if (
s->scenechange_threshold < 1000000000 &&
573 "closed gop with scene change detection are not supported yet, "
574 "set threshold to 1000000000\n");
582 "low delay forcing is only available for mpeg2, "
583 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
586 if (
s->max_b_frames != 0) {
588 "B-frames cannot be used with low delay\n");
593 if (
s->q_scale_type == 1) {
596 "non linear quant only supports qmax <= 28 currently\n");
609 "notice: b_frame_strategy only affects the first pass\n");
610 s->b_frame_strategy = 0;
624 s->inter_quant_bias = 0;
626 s->intra_quant_bias = 0;
641 "timebase %d/%d not supported by MPEG 4 standard, "
642 "the maximum admitted value for the timebase denominator "
650 #if CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER
657 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
661 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
675 if (!CONFIG_SPEEDHQ_ENCODER)
683 if (!CONFIG_H261_ENCODER)
694 if (!CONFIG_H263_ENCODER)
697 s->width,
s->height) == 8) {
699 "The specified picture size of %dx%d is not valid for "
700 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
701 "352x288, 704x576, and 1408x1152. "
702 "Try H.263+.\n",
s->width,
s->height);
714 s->modified_quant =
s->h263_aic;
716 s->unrestricted_mv =
s->obmc ||
s->loop_filter ||
s->umvplus;
726 s->unrestricted_mv = 1;
740 s->modified_quant = 1;
744 s->unrestricted_mv = 0;
749 s->unrestricted_mv = 1;
750 s->low_delay =
s->max_b_frames ? 0 : 1;
751 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
756 s->unrestricted_mv = 1;
757 s->msmpeg4_version = 2;
764 s->unrestricted_mv = 1;
765 s->msmpeg4_version = 3;
766 s->flipflop_rounding = 1;
773 s->unrestricted_mv = 1;
774 s->msmpeg4_version = 4;
775 s->flipflop_rounding = 1;
782 s->unrestricted_mv = 1;
783 s->msmpeg4_version = 5;
784 s->flipflop_rounding = 1;
796 s->progressive_frame =
824 mv_table_size = (
s->mb_height + 2) *
s->mb_stride + 1;
832 s->p_mv_table =
s->p_mv_table_base +
s->mb_stride + 1;
833 s->b_forw_mv_table =
s->b_forw_mv_table_base +
s->mb_stride + 1;
834 s->b_back_mv_table =
s->b_back_mv_table_base +
s->mb_stride + 1;
835 s->b_bidir_forw_mv_table =
s->b_bidir_forw_mv_table_base +
s->mb_stride + 1;
836 s->b_bidir_back_mv_table =
s->b_bidir_back_mv_table_base +
s->mb_stride + 1;
837 s->b_direct_mv_table =
s->b_direct_mv_table_base +
s->mb_stride + 1;
840 mb_array_size =
s->mb_stride *
s->mb_height;
850 #define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p))))
855 if (!(tmp1 =
ALLOCZ_ARRAYS(
s->b_field_mv_table_base, 8, mv_table_size)) ||
856 !(tmp2 =
ALLOCZ_ARRAYS(
s->b_field_select_table[0][0], 2 * 4, mv_table_size)) ||
860 s->p_field_select_table[1] =
s->p_field_select_table[0] + 2 * mv_table_size;
861 tmp1 +=
s->mb_stride + 1;
863 for (
int i = 0;
i < 2;
i++) {
864 for (
int j = 0; j < 2; j++) {
865 for (
int k = 0; k < 2; k++) {
866 s->b_field_mv_table[
i][j][k] = tmp1;
867 tmp1 += mv_table_size;
869 s->b_field_select_table[
i][j] = tmp2;
870 tmp2 += 2 * mv_table_size;
875 if (
s->noise_reduction) {
883 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
884 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
886 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
887 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
889 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
890 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
893 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) &&
s->modified_quant)
896 if (
s->slice_context_count > 1) {
900 s->h263_slice_structured = 1;
903 s->quant_precision = 5;
906 ff_set_cmp(&
s->mecc,
s->mecc.frame_skip_cmp,
s->frame_skip_cmp);
908 if (CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263) {
910 if (CONFIG_MSMPEG4ENC &&
s->msmpeg4_version)
915 for (
i = 0;
i < 64;
i++) {
916 int j =
s->idsp.idct_permutation[
i];
929 s->chroma_intra_matrix[j] =
953 if (
s->b_frame_strategy == 2) {
954 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
956 if (!
s->tmp_frames[
i])
960 s->tmp_frames[
i]->width =
s->width >>
s->brd_scale;
961 s->tmp_frames[
i]->height =
s->height >>
s->brd_scale;
999 av_freep(&
s->b_bidir_forw_mv_table_base);
1000 av_freep(&
s->b_bidir_back_mv_table_base);
1003 av_freep(&
s->b_field_select_table[0][0]);
1012 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
1013 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
1014 s->q_chroma_intra_matrix=
NULL;
1015 s->q_chroma_intra_matrix16=
NULL;
1030 #define IS_ENCODER 1
1038 for (
int i = 0;
i < 6;
i++) {
1039 for (
int j = 0; j < 64; j++) {
1041 block[
i][
s->idsp.idct_permutation[j]]);
1055 for (y = 0; y < 16; y++) {
1056 for (x = 0; x < 16; x++) {
1071 h =
s->height & ~15;
1073 for (y = 0; y <
h; y += 16) {
1074 for (x = 0; x <
w; x += 16) {
1081 acc += sae + 500 < sad;
1090 s->chroma_x_shift,
s->chroma_y_shift,
s->out_format,
1091 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
1092 &
s->linesize, &
s->uvlinesize);
1099 int i, display_picture_number = 0,
ret;
1100 int encoding_delay =
s->max_b_frames ?
s->max_b_frames
1101 : (
s->low_delay ? 0 : 1);
1102 int flush_offset = 1;
1107 display_picture_number =
s->input_picture_number++;
1111 int64_t last =
s->user_specified_pts;
1115 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1120 if (!
s->low_delay && display_picture_number == 1)
1121 s->dts_delta =
pts - last;
1123 s->user_specified_pts =
pts;
1126 s->user_specified_pts =
1127 pts =
s->user_specified_pts + 1;
1129 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1132 pts = display_picture_number;
1136 if (!pic_arg->
buf[0] ||
1138 pic_arg->
linesize[1] !=
s->uvlinesize ||
1141 if ((
s->width & 15) || (
s->height & 15))
1149 pic_arg->
linesize[1],
s->linesize,
s->uvlinesize);
1155 pic = &
s->picture[
i];
1172 int h_chroma_shift, v_chroma_shift;
1177 for (
i = 0;
i < 3;
i++) {
1179 int dst_stride =
i ?
s->uvlinesize :
s->linesize;
1180 int h_shift =
i ? h_chroma_shift : 0;
1181 int v_shift =
i ? v_chroma_shift : 0;
1182 int w =
s->width >> h_shift;
1183 int h =
s->height >> v_shift;
1184 const uint8_t *
src = pic_arg->
data[
i];
1185 uint8_t *dst = pic->
f->
data[
i];
1189 && !
s->progressive_sequence
1190 &&
FFALIGN(
s->height, 32) -
s->height > 16)
1193 if (!
s->avctx->rc_buffer_size)
1196 if (src_stride == dst_stride)
1197 memcpy(dst,
src, src_stride *
h);
1200 uint8_t *dst2 = dst;
1202 memcpy(dst2,
src,
w);
1207 if ((
s->width & 15) || (
s->height & (vpad-1))) {
1208 s->mpvencdsp.draw_edges(dst, dst_stride,
1227 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1228 if (
s->input_picture[flush_offset])
1231 if (flush_offset <= 1)
1234 encoding_delay = encoding_delay - flush_offset + 1;
1239 s->input_picture[
i - flush_offset] =
s->input_picture[
i];
1241 s->input_picture[encoding_delay] = pic;
1250 int64_t score64 = 0;
1252 for (plane = 0; plane < 3; plane++) {
1254 const int bw = plane ? 1 : 2;
1255 for (y = 0; y <
s->mb_height * bw; y++) {
1256 for (x = 0; x <
s->mb_width * bw; x++) {
1257 int off = p->
shared ? 0 : 16;
1258 const uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y *
stride) + off;
1259 const uint8_t *rptr =
ref->f->data[plane] + 8 * (x + y *
stride);
1260 int v =
s->mecc.frame_skip_cmp[1](
s, dptr, rptr,
stride, 8);
1262 switch (
FFABS(
s->frame_skip_exp)) {
1263 case 0: score =
FFMAX(score, v);
break;
1264 case 1: score +=
FFABS(v);
break;
1265 case 2: score64 += v * (int64_t)v;
break;
1266 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1267 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1276 if (
s->frame_skip_exp < 0)
1277 score64 = pow(score64 / (
double)(
s->mb_width *
s->mb_height),
1278 -1.0/
s->frame_skip_exp);
1282 if (score64 < ((
s->frame_skip_factor * (int64_t)
s->lambda) >> 8))
1311 const int scale =
s->brd_scale;
1315 int64_t best_rd = INT64_MAX;
1316 int best_b_count = -1;
1331 b_lambda = p_lambda;
1335 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1336 const Picture *pre_input_ptr =
i ?
s->input_picture[
i - 1] :
1337 s->next_picture_ptr;
1339 if (pre_input_ptr) {
1340 const uint8_t *
data[4];
1343 if (!pre_input_ptr->
shared &&
i) {
1349 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[0],
1350 s->tmp_frames[
i]->linesize[0],
1354 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[1],
1355 s->tmp_frames[
i]->linesize[1],
1359 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[2],
1360 s->tmp_frames[
i]->linesize[2],
1367 for (j = 0; j <
s->max_b_frames + 1; j++) {
1371 if (!
s->input_picture[j])
1384 c->mb_decision =
s->avctx->mb_decision;
1385 c->me_cmp =
s->avctx->me_cmp;
1386 c->mb_cmp =
s->avctx->mb_cmp;
1387 c->me_sub_cmp =
s->avctx->me_sub_cmp;
1389 c->time_base =
s->avctx->time_base;
1390 c->max_b_frames =
s->max_b_frames;
1408 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1409 int is_p =
i % (j + 1) == j ||
i ==
s->max_b_frames;
1411 s->tmp_frames[
i + 1]->pict_type = is_p ?
1413 s->tmp_frames[
i + 1]->quality = is_p ? p_lambda : b_lambda;
1432 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1450 return best_b_count;
1458 s->reordered_input_picture[
i - 1] =
s->reordered_input_picture[
i];
1462 if (!
s->reordered_input_picture[0] &&
s->input_picture[0]) {
1463 if (
s->frame_skip_threshold ||
s->frame_skip_factor) {
1464 if (
s->picture_in_gop_number <
s->gop_size &&
1465 s->next_picture_ptr &&
1477 !
s->next_picture_ptr ||
s->intra_only) {
1478 s->reordered_input_picture[0] =
s->input_picture[0];
1480 s->reordered_input_picture[0]->f->coded_picture_number =
1481 s->coded_picture_number++;
1486 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1487 int pict_num =
s->input_picture[0]->f->display_picture_number +
i;
1489 if (pict_num >=
s->rc_context.num_entries)
1491 if (!
s->input_picture[
i]) {
1496 s->input_picture[
i]->f->pict_type =
1497 s->rc_context.entry[pict_num].new_pict_type;
1501 if (
s->b_frame_strategy == 0) {
1502 b_frames =
s->max_b_frames;
1503 while (b_frames && !
s->input_picture[b_frames])
1505 }
else if (
s->b_frame_strategy == 1) {
1506 for (
i = 1;
i <
s->max_b_frames + 1;
i++) {
1507 if (
s->input_picture[
i] &&
1508 s->input_picture[
i]->b_frame_score == 0) {
1509 s->input_picture[
i]->b_frame_score =
1511 s->input_picture[
i ]->f->data[0],
1512 s->input_picture[
i - 1]->f->data[0],
1516 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1517 if (!
s->input_picture[
i] ||
1518 s->input_picture[
i]->b_frame_score - 1 >
1519 s->mb_num /
s->b_sensitivity)
1523 b_frames =
FFMAX(0,
i - 1);
1526 for (
i = 0;
i < b_frames + 1;
i++) {
1527 s->input_picture[
i]->b_frame_score = 0;
1529 }
else if (
s->b_frame_strategy == 2) {
1537 for (
i = b_frames - 1;
i >= 0;
i--) {
1538 int type =
s->input_picture[
i]->f->pict_type;
1543 b_frames ==
s->max_b_frames) {
1545 "warning, too many B-frames in a row\n");
1548 if (
s->picture_in_gop_number + b_frames >=
s->gop_size) {
1550 s->gop_size >
s->picture_in_gop_number) {
1551 b_frames =
s->gop_size -
s->picture_in_gop_number - 1;
1563 s->reordered_input_picture[0] =
s->input_picture[b_frames];
1566 s->reordered_input_picture[0]->f->coded_picture_number =
1567 s->coded_picture_number++;
1568 for (
i = 0;
i < b_frames;
i++) {
1569 s->reordered_input_picture[
i + 1] =
s->input_picture[
i];
1570 s->reordered_input_picture[
i + 1]->f->pict_type =
1572 s->reordered_input_picture[
i + 1]->f->coded_picture_number =
1573 s->coded_picture_number++;
1580 if (
s->reordered_input_picture[0]) {
1581 s->reordered_input_picture[0]->reference =
1582 s->reordered_input_picture[0]->f->pict_type !=
1586 s->reordered_input_picture[0]->f)))
1589 if (
s->reordered_input_picture[0]->shared ||
s->avctx->rc_buffer_size) {
1597 pic = &
s->picture[
i];
1599 pic->
reference =
s->reordered_input_picture[0]->reference;
1610 s->reordered_input_picture[0]->shared = 0;
1612 s->current_picture_ptr = pic;
1615 s->current_picture_ptr =
s->reordered_input_picture[0];
1616 for (
i = 0;
i < 4;
i++) {
1617 if (
s->new_picture->data[
i])
1621 s->picture_number =
s->new_picture->display_picture_number;
1628 if (
s->unrestricted_mv &&
1629 s->current_picture.reference &&
1632 int hshift =
desc->log2_chroma_w;
1633 int vshift =
desc->log2_chroma_h;
1634 s->mpvencdsp.draw_edges(
s->current_picture.f->data[0],
1635 s->current_picture.f->linesize[0],
1636 s->h_edge_pos,
s->v_edge_pos,
1639 s->mpvencdsp.draw_edges(
s->current_picture.f->data[1],
1640 s->current_picture.f->linesize[1],
1641 s->h_edge_pos >> hshift,
1642 s->v_edge_pos >> vshift,
1646 s->mpvencdsp.draw_edges(
s->current_picture.f->data[2],
1647 s->current_picture.f->linesize[2],
1648 s->h_edge_pos >> hshift,
1649 s->v_edge_pos >> vshift,
1657 s->last_pict_type =
s->pict_type;
1658 s->last_lambda_for [
s->pict_type] =
s->current_picture_ptr->f->quality;
1660 s->last_non_b_pict_type =
s->pict_type;
1667 for (intra = 0; intra < 2; intra++) {
1668 if (
s->dct_count[intra] > (1 << 16)) {
1669 for (
i = 0;
i < 64;
i++) {
1670 s->dct_error_sum[intra][
i] >>= 1;
1672 s->dct_count[intra] >>= 1;
1675 for (
i = 0;
i < 64;
i++) {
1676 s->dct_offset[intra][
i] = (
s->noise_reduction *
1677 s->dct_count[intra] +
1678 s->dct_error_sum[intra][
i] / 2) /
1679 (
s->dct_error_sum[intra][
i] + 1);
1690 s->last_picture_ptr !=
s->next_picture_ptr &&
1691 s->last_picture_ptr->f->buf[0]) {
1695 s->current_picture_ptr->f->pict_type =
s->pict_type;
1700 s->current_picture_ptr)) < 0)
1704 s->last_picture_ptr =
s->next_picture_ptr;
1705 s->next_picture_ptr =
s->current_picture_ptr;
1708 if (
s->last_picture_ptr) {
1710 if (
s->last_picture_ptr->f->buf[0] &&
1712 s->last_picture_ptr)) < 0)
1715 if (
s->next_picture_ptr) {
1717 if (
s->next_picture_ptr->f->buf[0] &&
1719 s->next_picture_ptr)) < 0)
1725 for (
i = 0;
i < 4;
i++) {
1727 s->current_picture.f->data[
i] +=
1728 s->current_picture.f->linesize[
i];
1730 s->current_picture.f->linesize[
i] *= 2;
1731 s->last_picture.f->linesize[
i] *= 2;
1732 s->next_picture.f->linesize[
i] *= 2;
1736 if (
s->dct_error_sum) {
1745 const AVFrame *pic_arg,
int *got_packet)
1748 int i, stuffing_count,
ret;
1749 int context_count =
s->slice_context_count;
1751 s->vbv_ignore_qmax = 0;
1753 s->picture_in_gop_number++;
1763 if (
s->new_picture->data[0]) {
1764 int growing_buffer = context_count == 1 && !
s->data_partitioning;
1765 size_t pkt_size = 10000 +
s->mb_width *
s->mb_height *
1778 s->mb_width*
s->mb_height*12);
1779 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1782 for (
i = 0;
i < context_count;
i++) {
1783 int start_y =
s->thread_context[
i]->start_mb_y;
1785 int h =
s->mb_height;
1786 uint8_t *start =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * start_y /
h);
1787 uint8_t *end =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * end_y /
h);
1792 s->pict_type =
s->new_picture->pict_type;
1799 if (growing_buffer) {
1809 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
s->out_format ==
FMT_MJPEG)
1819 s->lambda <
s->lmax) {
1820 s->next_lambda =
FFMAX(
s->lambda + min_step,
s->lambda *
1821 (
s->qscale + 1) /
s->qscale);
1822 if (
s->adaptive_quant) {
1824 for (
i = 0;
i <
s->mb_height *
s->mb_stride;
i++)
1825 s->lambda_table[
i] =
1826 FFMAX(
s->lambda_table[
i] + min_step,
1827 s->lambda_table[
i] * (
s->qscale + 1) /
1833 if (
s->flipflop_rounding ||
1836 s->no_rounding ^= 1;
1839 s->time_base =
s->last_time_base;
1840 s->last_non_b_time =
s->time -
s->pp_time;
1842 for (
i = 0;
i < context_count;
i++) {
1846 s->vbv_ignore_qmax = 1;
1857 for (
i = 0;
i < 4;
i++) {
1867 s->misc_bits +
s->i_tex_bits +
1873 s->stuffing_bits = 8*stuffing_count;
1874 if (stuffing_count) {
1880 switch (
s->codec_id) {
1883 while (stuffing_count--) {
1890 stuffing_count -= 4;
1891 while (stuffing_count--) {
1897 s->stuffing_bits = 0;
1915 int minbits =
s->frame_bits - 8 *
1916 (
s->vbv_delay_pos - 1);
1917 double bits =
s->rc_context.buffer_index + minbits - inbits;
1918 uint8_t *
const vbv_delay_ptr =
s->pb.buf +
s->vbv_delay_pos;
1922 "Internal error, negative bits\n");
1934 vbv_delay_ptr[0] &= 0xF8;
1937 vbv_delay_ptr[2] &= 0x07;
1946 (uint8_t*)props, props_size);
1952 s->total_bits +=
s->frame_bits;
1954 pkt->
pts =
s->current_picture.f->pts;
1956 if (!
s->current_picture.f->coded_picture_number)
1963 if (
s->current_picture.f->key_frame)
1973 if (!
s->picture[
i].reference)
1985 int n,
int threshold)
1987 static const char tab[64] = {
1988 3, 2, 2, 1, 1, 1, 1, 1,
1989 1, 1, 1, 1, 1, 1, 1, 1,
1990 1, 1, 1, 1, 1, 1, 1, 1,
1991 0, 0, 0, 0, 0, 0, 0, 0,
1992 0, 0, 0, 0, 0, 0, 0, 0,
1993 0, 0, 0, 0, 0, 0, 0, 0,
1994 0, 0, 0, 0, 0, 0, 0, 0,
1995 0, 0, 0, 0, 0, 0, 0, 0
2000 int16_t *
block =
s->block[n];
2001 const int last_index =
s->block_last_index[n];
2004 if (threshold < 0) {
2006 threshold = -threshold;
2011 if (last_index <= skip_dc - 1)
2014 for (
i = 0;
i <= last_index;
i++) {
2015 const int j =
s->intra_scantable.permutated[
i];
2018 if (skip_dc &&
i == 0)
2022 }
else if (
level > 1) {
2028 if (score >= threshold)
2030 for (
i = skip_dc;
i <= last_index;
i++) {
2031 const int j =
s->intra_scantable.permutated[
i];
2035 s->block_last_index[n] = 0;
2037 s->block_last_index[n] = -1;
2044 const int maxlevel =
s->max_qcoeff;
2045 const int minlevel =
s->min_qcoeff;
2053 for (;
i <= last_index;
i++) {
2054 const int j =
s->intra_scantable.permutated[
i];
2057 if (
level > maxlevel) {
2060 }
else if (
level < minlevel) {
2070 "warning, clipping %d dct coefficients to %d..%d\n",
2078 for (y = 0; y < 8; y++) {
2079 for (x = 0; x < 8; x++) {
2085 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2086 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2087 int v = ptr[x2 + y2 *
stride];
2099 int motion_x,
int motion_y,
2100 int mb_block_height,
2109 #define INTERLACED_DCT(s) ((chroma_format == CHROMA_420 || chroma_format == CHROMA_422) && \
2110 (s)->avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT)
2112 int16_t orig[12][64];
2113 const int mb_x =
s->mb_x;
2114 const int mb_y =
s->mb_y;
2118 int uv_dct_offset =
s->uvlinesize * 8;
2119 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2120 ptrdiff_t wrap_y, wrap_c;
2122 for (
i = 0;
i < mb_block_count;
i++)
2123 skip_dct[
i] =
s->skipdct;
2125 if (
s->adaptive_quant) {
2126 const int last_qp =
s->qscale;
2127 const int mb_xy =
mb_x +
mb_y *
s->mb_stride;
2129 s->lambda =
s->lambda_table[mb_xy];
2133 s->qscale =
s->current_picture_ptr->qscale_table[mb_xy];
2134 s->dquant =
s->qscale - last_qp;
2155 wrap_y =
s->linesize;
2156 wrap_c =
s->uvlinesize;
2157 ptr_y =
s->new_picture->data[0] +
2159 ptr_cb =
s->new_picture->data[1] +
2160 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2161 ptr_cr =
s->new_picture->data[2] +
2162 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2165 uint8_t *ebuf =
s->sc.edge_emu_buffer + 38 * wrap_y;
2168 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2171 s->width,
s->height);
2173 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2175 mb_block_width, mb_block_height,
2176 mb_x * mb_block_width,
mb_y * mb_block_height,
2178 ptr_cb = ebuf + 16 * wrap_y;
2179 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2181 mb_block_width, mb_block_height,
2182 mb_x * mb_block_width,
mb_y * mb_block_height,
2184 ptr_cr = ebuf + 16 * wrap_y + 16;
2189 int progressive_score, interlaced_score;
2191 s->interlaced_dct = 0;
2192 progressive_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
NULL, wrap_y, 8) +
2193 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y * 8,
2194 NULL, wrap_y, 8) - 400;
2196 if (progressive_score > 0) {
2197 interlaced_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
2198 NULL, wrap_y * 2, 8) +
2199 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y,
2200 NULL, wrap_y * 2, 8);
2201 if (progressive_score > interlaced_score) {
2202 s->interlaced_dct = 1;
2205 uv_dct_offset = wrap_c;
2214 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2215 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2216 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2217 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2223 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2224 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2226 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2227 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2229 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2230 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2231 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2232 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2233 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2234 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2240 uint8_t *dest_y, *dest_cb, *dest_cr;
2242 dest_y =
s->dest[0];
2243 dest_cb =
s->dest[1];
2244 dest_cr =
s->dest[2];
2247 op_pix =
s->hdsp.put_pixels_tab;
2248 op_qpix =
s->qdsp.put_qpel_pixels_tab;
2250 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
2251 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
2256 s->last_picture.f->data,
2258 op_pix =
s->hdsp.avg_pixels_tab;
2259 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
2263 s->next_picture.f->data,
2268 int progressive_score, interlaced_score;
2270 s->interlaced_dct = 0;
2271 progressive_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2272 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y * 8,
2277 progressive_score -= 400;
2279 if (progressive_score > 0) {
2280 interlaced_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y,
2282 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y,
2286 if (progressive_score > interlaced_score) {
2287 s->interlaced_dct = 1;
2290 uv_dct_offset = wrap_c;
2298 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2299 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2302 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2309 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2310 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2312 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2313 dest_cb + uv_dct_offset, wrap_c);
2314 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2315 dest_cr + uv_dct_offset, wrap_c);
2319 if (
s->mc_mb_var[
s->mb_stride *
mb_y +
mb_x] < 2 *
s->qscale *
s->qscale) {
2321 if (
s->mecc.sad[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->qscale)
2323 if (
s->mecc.sad[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->qscale)
2326 wrap_y, 8) < 20 *
s->qscale)
2329 wrap_y, 8) < 20 *
s->qscale)
2331 if (
s->mecc.sad[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->qscale)
2333 if (
s->mecc.sad[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->qscale)
2336 if (
s->mecc.sad[1](
NULL, ptr_cb + uv_dct_offset,
2337 dest_cb + uv_dct_offset,
2338 wrap_c, 8) < 20 *
s->qscale)
2340 if (
s->mecc.sad[1](
NULL, ptr_cr + uv_dct_offset,
2341 dest_cr + uv_dct_offset,
2342 wrap_c, 8) < 20 *
s->qscale)
2348 if (
s->quantizer_noise_shaping) {
2369 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2375 for (
i = 0;
i < mb_block_count;
i++) {
2378 s->block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->qscale, &
overflow);
2387 s->block_last_index[
i] = -1;
2389 if (
s->quantizer_noise_shaping) {
2390 for (
i = 0;
i < mb_block_count;
i++) {
2392 s->block_last_index[
i] =
2394 orig[
i],
i,
s->qscale);
2399 if (
s->luma_elim_threshold && !
s->mb_intra)
2400 for (
i = 0;
i < 4;
i++)
2402 if (
s->chroma_elim_threshold && !
s->mb_intra)
2403 for (
i = 4;
i < mb_block_count;
i++)
2407 for (
i = 0;
i < mb_block_count;
i++) {
2408 if (
s->block_last_index[
i] == -1)
2409 s->coded_score[
i] = INT_MAX / 256;
2415 s->block_last_index[4] =
2416 s->block_last_index[5] = 0;
2418 s->block[5][0] = (1024 +
s->c_dc_scale / 2) /
s->c_dc_scale;
2420 for (
i=6;
i<12;
i++) {
2421 s->block_last_index[
i] = 0;
2422 s->block[
i][0] =
s->block[4][0];
2429 for (
i = 0;
i < mb_block_count;
i++) {
2431 if (
s->block_last_index[
i] > 0) {
2432 for (j = 63; j > 0; j--) {
2433 if (
s->block[
i][
s->intra_scantable.permutated[j]])
2436 s->block_last_index[
i] = j;
2442 switch(
s->codec_id){
2445 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2449 if (CONFIG_MPEG4_ENCODER)
2455 if (CONFIG_MSMPEG4ENC)
2459 if (CONFIG_WMV2_ENCODER)
2463 if (CONFIG_H261_ENCODER)
2471 if (CONFIG_H263_ENCODER)
2474 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2481 if (CONFIG_SPEEDHQ_ENCODER)
2504 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2507 d->mb_skip_run=
s->mb_skip_run;
2509 d->last_dc[
i] =
s->last_dc[
i];
2512 d->mv_bits=
s->mv_bits;
2513 d->i_tex_bits=
s->i_tex_bits;
2514 d->p_tex_bits=
s->p_tex_bits;
2515 d->i_count=
s->i_count;
2516 d->skip_count=
s->skip_count;
2517 d->misc_bits=
s->misc_bits;
2521 d->qscale=
s->qscale;
2522 d->dquant=
s->dquant;
2524 d->esc3_level_length=
s->esc3_level_length;
2532 memcpy(
d->mv,
s->mv, 2*4*2*
sizeof(
int));
2533 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2536 d->mb_skip_run=
s->mb_skip_run;
2538 d->last_dc[
i] =
s->last_dc[
i];
2541 d->mv_bits=
s->mv_bits;
2542 d->i_tex_bits=
s->i_tex_bits;
2543 d->p_tex_bits=
s->p_tex_bits;
2544 d->i_count=
s->i_count;
2545 d->skip_count=
s->skip_count;
2546 d->misc_bits=
s->misc_bits;
2548 d->mb_intra=
s->mb_intra;
2549 d->mb_skipped=
s->mb_skipped;
2550 d->mv_type=
s->mv_type;
2551 d->mv_dir=
s->mv_dir;
2553 if(
s->data_partitioning){
2555 d->tex_pb=
s->tex_pb;
2559 d->block_last_index[
i]=
s->block_last_index[
i];
2560 d->interlaced_dct=
s->interlaced_dct;
2561 d->qscale=
s->qscale;
2563 d->esc3_level_length=
s->esc3_level_length;
2568 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2571 uint8_t *dest_backup[3];
2575 s->block=
s->blocks[*next_block];
2576 s->pb=
pb[*next_block];
2577 if(
s->data_partitioning){
2578 s->pb2 =
pb2 [*next_block];
2579 s->tex_pb=
tex_pb[*next_block];
2583 memcpy(dest_backup,
s->dest,
sizeof(
s->dest));
2584 s->dest[0] =
s->sc.rd_scratchpad;
2585 s->dest[1] =
s->sc.rd_scratchpad + 16*
s->linesize;
2586 s->dest[2] =
s->sc.rd_scratchpad + 16*
s->linesize + 8;
2593 if(
s->data_partitioning){
2601 score *=
s->lambda2;
2606 memcpy(
s->dest, dest_backup,
sizeof(
s->dest));
2624 else if(
w==8 &&
h==8)
2641 int chroma_mb_w =
w >>
s->chroma_x_shift;
2642 int chroma_mb_h =
h >>
s->chroma_y_shift;
2644 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
2645 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
2649 return s->mecc.nsse[0](
s,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2650 s->dest[0],
s->linesize, 16) +
2651 s->mecc.nsse[1](
s,
s->new_picture->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2652 s->dest[1],
s->uvlinesize, chroma_mb_h) +
2653 s->mecc.nsse[1](
s,
s->new_picture->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2654 s->dest[2],
s->uvlinesize, chroma_mb_h);
2656 return s->mecc.sse[0](
NULL,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2657 s->dest[0],
s->linesize, 16) +
2658 s->mecc.sse[1](
NULL,
s->new_picture->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2659 s->dest[1],
s->uvlinesize, chroma_mb_h) +
2660 s->mecc.sse[1](
NULL,
s->new_picture->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2661 s->dest[2],
s->uvlinesize, chroma_mb_h);
2664 return sse(
s,
s->new_picture->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
2665 s->dest[0],
w,
h,
s->linesize) +
2666 sse(
s,
s->new_picture->data[1] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2667 s->dest[1],
w >>
s->chroma_x_shift,
h >>
s->chroma_y_shift,
s->uvlinesize) +
2668 sse(
s,
s->new_picture->data[2] +
s->mb_x * chroma_mb_w +
s->mb_y *
s->uvlinesize * chroma_mb_h,
2669 s->dest[2],
w >>
s->chroma_x_shift,
h >>
s->chroma_y_shift,
s->uvlinesize);
2677 s->me.dia_size=
s->avctx->pre_dia_size;
2678 s->first_slice_line=1;
2679 for(
s->mb_y=
s->end_mb_y-1;
s->mb_y >=
s->start_mb_y;
s->mb_y--) {
2680 for(
s->mb_x=
s->mb_width-1;
s->mb_x >=0 ;
s->mb_x--) {
2683 s->first_slice_line=0;
2694 s->me.dia_size=
s->avctx->dia_size;
2695 s->first_slice_line=1;
2696 for(
s->mb_y=
s->start_mb_y;
s->mb_y <
s->end_mb_y;
s->mb_y++) {
2699 for(
s->mb_x=0;
s->mb_x <
s->mb_width;
s->mb_x++) {
2700 s->block_index[0]+=2;
2701 s->block_index[1]+=2;
2702 s->block_index[2]+=2;
2703 s->block_index[3]+=2;
2711 s->first_slice_line=0;
2724 const uint8_t *pix =
s->new_picture->data[0] + (yy *
s->linesize) + xx;
2726 int sum =
s->mpvencdsp.pix_sum(pix,
s->linesize);
2728 varc = (
s->mpvencdsp.pix_norm1(pix,
s->linesize) -
2729 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2731 s->mb_var [
s->mb_stride *
mb_y +
mb_x] = varc;
2732 s->mb_mean[
s->mb_stride *
mb_y +
mb_x] = (sum+128)>>8;
2733 s->me.mb_var_sum_temp += varc;
2741 if(
s->partitioned_frame){
2746 }
else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2749 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->out_format ==
FMT_SPEEDHQ) {
2761 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2763 int mba =
s->mb_x +
s->mb_width * (
s->mb_y %
s->gob_index);
2764 int gobn =
s->mb_y /
s->gob_index;
2766 if (CONFIG_H263_ENCODER)
2768 bytestream_put_le32(&ptr,
offset);
2769 bytestream_put_byte(&ptr,
s->qscale);
2770 bytestream_put_byte(&ptr, gobn);
2771 bytestream_put_le16(&ptr, mba);
2772 bytestream_put_byte(&ptr, pred_x);
2773 bytestream_put_byte(&ptr, pred_y);
2775 bytestream_put_byte(&ptr, 0);
2776 bytestream_put_byte(&ptr, 0);
2784 s->mb_info_size += 12;
2785 s->prev_mb_info =
s->last_mb_info;
2797 if (!
s->mb_info_size)
2798 s->mb_info_size += 12;
2805 &&
s->slice_context_count == 1
2806 &&
s->pb.buf ==
s->avctx->internal->byte_buffer) {
2807 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2809 uint8_t *new_buffer =
NULL;
2810 int new_buffer_size = 0;
2812 if ((
s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2820 s->avctx->internal->byte_buffer_size + size_increase);
2824 memcpy(new_buffer,
s->avctx->internal->byte_buffer,
s->avctx->internal->byte_buffer_size);
2825 av_free(
s->avctx->internal->byte_buffer);
2826 s->avctx->internal->byte_buffer = new_buffer;
2827 s->avctx->internal->byte_buffer_size = new_buffer_size;
2829 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2839 int chr_h= 16>>
s->chroma_y_shift;
2864 s->last_dc[
i] = 128 <<
s->intra_dc_precision;
2866 s->encoding_error[
i] = 0;
2869 s->last_dc[0] = 128*8/13;
2870 s->last_dc[1] = 128*8/14;
2871 s->last_dc[2] = 128*8/14;
2874 memset(
s->last_mv, 0,
sizeof(
s->last_mv));
2878 switch(
s->codec_id){
2882 if (CONFIG_H263_ENCODER)
2886 if(CONFIG_MPEG4_ENCODER &&
s->partitioned_frame)
2893 s->first_slice_line = 1;
2894 s->ptr_lastgob =
s->pb.buf;
2895 for (mb_y_order =
s->start_mb_y; mb_y_order < s->
end_mb_y; mb_y_order++) {
2899 if (first_in_slice && mb_y_order !=
s->start_mb_y)
2901 s->last_dc[0] =
s->last_dc[1] =
s->last_dc[2] = 1024 <<
s->intra_dc_precision;
2917 int size_increase =
s->avctx->internal->byte_buffer_size/4
2925 if(
s->data_partitioning){
2939 xy=
s->mb_y*
s->mb_stride +
s->mb_x;
2945 int current_packet_size, is_gob_start;
2948 - (
s->ptr_lastgob -
s->pb.buf);
2950 is_gob_start =
s->rtp_payload_size &&
2951 current_packet_size >=
s->rtp_payload_size &&
2954 if(
s->start_mb_y ==
mb_y &&
mb_y > 0 &&
mb_x==0) is_gob_start=1;
2956 switch(
s->codec_id){
2959 if(!
s->h263_slice_structured)
2960 if(
s->mb_x ||
s->mb_y%
s->gob_index) is_gob_start=0;
2963 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2965 if(
s->mb_skip_run) is_gob_start=0;
2968 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2984 if (
s->error_rate &&
s->resync_mb_x +
s->resync_mb_y > 0) {
2986 int d = 100 /
s->error_rate;
2988 current_packet_size=0;
2989 s->pb.buf_ptr=
s->ptr_lastgob;
2994 switch(
s->codec_id){
2996 if (CONFIG_MPEG4_ENCODER) {
3003 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3010 if (CONFIG_H263_ENCODER) {
3019 s->misc_bits+=
bits -
s->last_bits;
3023 s->ptr_lastgob += current_packet_size;
3024 s->first_slice_line=1;
3025 s->resync_mb_x=
mb_x;
3026 s->resync_mb_y=
mb_y;
3030 if( (
s->resync_mb_x ==
s->mb_x)
3031 &&
s->resync_mb_y+1 ==
s->mb_y){
3032 s->first_slice_line=0;
3042 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3048 if(
s->data_partitioning){
3049 backup_s.pb2=
s->pb2;
3050 backup_s.tex_pb=
s->tex_pb;
3057 s->mv[0][0][0] =
s->p_mv_table[xy][0];
3058 s->mv[0][0][1] =
s->p_mv_table[xy][1];
3060 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3067 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3068 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3069 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3072 &dmin, &next_block, 0, 0);
3081 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3088 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3089 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3092 &dmin, &next_block, 0, 0);
3098 s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3099 s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3101 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3107 s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3108 s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3110 &dmin, &next_block,
s->mv[1][0][0],
s->mv[1][0][1]);
3116 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3117 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3118 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3119 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3121 &dmin, &next_block, 0, 0);
3128 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3129 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3130 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3133 &dmin, &next_block, 0, 0);
3140 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3141 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3142 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3145 &dmin, &next_block, 0, 0);
3151 for(dir=0; dir<2; dir++){
3153 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3154 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3155 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3159 &dmin, &next_block, 0, 0);
3168 &dmin, &next_block, 0, 0);
3169 if(
s->h263_pred ||
s->h263_aic){
3171 s->mbintra_table[
mb_x +
mb_y*
s->mb_stride]=1;
3179 const int last_qp= backup_s.qscale;
3183 static const int dquant_tab[4]={-1,1,-2,2};
3184 int storecoefs =
s->mb_intra &&
s->dc_val[0];
3192 s->mv[0][0][0] = best_s.
mv[0][0][0];
3193 s->mv[0][0][1] = best_s.
mv[0][0][1];
3194 s->mv[1][0][0] = best_s.
mv[1][0][0];
3195 s->mv[1][0][1] = best_s.
mv[1][0][1];
3198 for(; qpi<4; qpi++){
3199 int dquant= dquant_tab[qpi];
3201 if(qp < s->
avctx->
qmin || qp >
s->avctx->qmax)
3206 dc[
i]=
s->dc_val[0][
s->block_index[
i] ];
3207 memcpy(ac[
i],
s->ac_val[0][
s->block_index[
i]],
sizeof(int16_t)*16);
3212 &dmin, &next_block,
s->mv[mvdir][0][0],
s->mv[mvdir][0][1]);
3216 s->dc_val[0][
s->block_index[
i] ]=
dc[
i];
3217 memcpy(
s->ac_val[0][
s->block_index[
i]], ac[
i],
sizeof(int16_t)*16);
3225 int mx=
s->b_direct_mv_table[xy][0];
3226 int my=
s->b_direct_mv_table[xy][1];
3228 backup_s.dquant = 0;
3233 &dmin, &next_block, mx, my);
3236 backup_s.dquant = 0;
3241 &dmin, &next_block, 0, 0);
3246 coded |=
s->block_last_index[
i];
3249 memcpy(
s->mv, best_s.
mv,
sizeof(
s->mv));
3271 &dmin, &next_block, mx, my);
3276 s->current_picture.qscale_table[xy] = best_s.
qscale;
3282 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3285 if(
s->data_partitioning){
3288 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3289 s->pb2= backup_s.pb2;
3293 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3294 s->tex_pb= backup_s.tex_pb;
3298 if (CONFIG_H263_ENCODER &&
3303 s->hdsp.put_pixels_tab[0][0](
s->dest[0],
s->sc.rd_scratchpad ,
s->linesize ,16);
3304 s->hdsp.put_pixels_tab[1][0](
s->dest[1],
s->sc.rd_scratchpad + 16*
s->linesize ,
s->uvlinesize, 8);
3305 s->hdsp.put_pixels_tab[1][0](
s->dest[2],
s->sc.rd_scratchpad + 16*
s->linesize + 8,
s->uvlinesize, 8);
3311 int motion_x = 0, motion_y = 0;
3319 motion_x=
s->mv[0][0][0] = 0;
3320 motion_y=
s->mv[0][0][1] = 0;
3325 motion_x=
s->mv[0][0][0] =
s->p_mv_table[xy][0];
3326 motion_y=
s->mv[0][0][1] =
s->p_mv_table[xy][1];
3333 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3334 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3335 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3343 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3344 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3348 if (CONFIG_MPEG4_ENCODER) {
3351 motion_x=
s->b_direct_mv_table[xy][0];
3352 motion_y=
s->b_direct_mv_table[xy][1];
3357 if (CONFIG_MPEG4_ENCODER) {
3366 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3367 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3368 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3369 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3374 motion_x=
s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3375 motion_y=
s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3380 motion_x=
s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3381 motion_y=
s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3388 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3389 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3390 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3398 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3399 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3400 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3407 for(dir=0; dir<2; dir++){
3409 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3410 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3411 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3422 s->last_mv_dir =
s->mv_dir;
3424 if (CONFIG_H263_ENCODER &&
3433 s->p_mv_table[xy][0]=0;
3434 s->p_mv_table[xy][1]=0;
3441 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
3442 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
3444 s->encoding_error[0] +=
sse(
3445 s,
s->new_picture->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
3446 s->dest[0],
w,
h,
s->linesize);
3447 s->encoding_error[1] +=
sse(
3448 s,
s->new_picture->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3449 s->dest[1],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3450 s->encoding_error[2] +=
sse(
3451 s,
s->new_picture->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3452 s->dest[2],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3455 if(CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
3458 ff_dlog(
s->avctx,
"MB %d %d bits\n",
3464 if (CONFIG_MSMPEG4ENC &&
s->msmpeg4_version &&
s->msmpeg4_version<4 &&
s->pict_type ==
AV_PICTURE_TYPE_I)
3472 #define MERGE(field) dst->field += src->field; src->field=0
3495 for(
i=0;
i<64;
i++){
3508 if (
s->next_lambda){
3509 s->current_picture_ptr->f->quality =
3510 s->current_picture.f->quality =
s->next_lambda;
3511 if(!dry_run)
s->next_lambda= 0;
3512 }
else if (!
s->fixed_qscale) {
3514 s->current_picture_ptr->f->quality =
3515 s->current_picture.f->quality =
quality;
3516 if (
s->current_picture.f->quality < 0)
3520 if(
s->adaptive_quant){
3521 switch(
s->codec_id){
3523 if (CONFIG_MPEG4_ENCODER)
3529 if (CONFIG_H263_ENCODER)
3536 s->lambda=
s->lambda_table[0];
3539 s->lambda =
s->current_picture.f->quality;
3547 s->time =
s->current_picture_ptr->f->pts *
s->avctx->time_base.num;
3550 s->pb_time=
s->pp_time - (
s->last_non_b_time -
s->time);
3553 s->pp_time=
s->time -
s->last_non_b_time;
3554 s->last_non_b_time=
s->time;
3563 int context_count =
s->slice_context_count;
3566 s->me.mb_var_sum_temp =
3567 s->me.mc_mb_var_sum_temp = 0;
3571 if (
s->out_format ==
FMT_MPEG1 || (
s->h263_pred && !
s->msmpeg4_version))
3576 s->me.scene_change_score=0;
3581 if(
s->msmpeg4_version >= 3)
s->no_rounding=1;
3582 else s->no_rounding=0;
3585 s->no_rounding ^= 1;
3594 s->lambda=
s->last_lambda_for[
s->pict_type];
3596 s->lambda=
s->last_lambda_for[
s->last_non_b_pict_type];
3601 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
3602 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
3603 s->q_chroma_intra_matrix =
s->q_intra_matrix;
3604 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
3608 for(
i=1;
i<context_count;
i++){
3619 s->lambda = (
s->lambda *
s->me_penalty_compensation + 128) >> 8;
3620 s->lambda2 = (
s->lambda2 * (int64_t)
s->me_penalty_compensation + 128) >> 8;
3631 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3634 if(!
s->fixed_qscale){
3636 s->avctx->execute(
s->avctx,
mb_var_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3639 for(
i=1;
i<context_count;
i++){
3642 s->mc_mb_var_sum =
s->me.mc_mb_var_sum_temp;
3643 s->mb_var_sum =
s->me. mb_var_sum_temp;
3646 if (
s->me.scene_change_score >
s->scenechange_threshold &&
3649 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3651 if(
s->msmpeg4_version >= 3)
3653 ff_dlog(
s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3654 s->mb_var_sum,
s->mc_mb_var_sum);
3695 for(dir=0; dir<2; dir++){
3701 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3712 if (
s->qscale < 3 &&
s->max_qcoeff <= 128 &&
3721 if (
s->avctx->intra_matrix) {
3723 luma_matrix =
s->avctx->intra_matrix;
3725 if (
s->avctx->chroma_intra_matrix)
3726 chroma_matrix =
s->avctx->chroma_intra_matrix;
3730 int j =
s->idsp.idct_permutation[
i];
3732 s->chroma_intra_matrix[j] =
av_clip_uint8((chroma_matrix[
i] *
s->qscale) >> 3);
3735 s->y_dc_scale_table=
3737 s->chroma_intra_matrix[0] =
3740 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3742 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3746 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};
3747 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};
3748 for (
int i = 1;
i < 64;
i++) {
3754 s->y_dc_scale_table = y;
3755 s->c_dc_scale_table =
c;
3756 s->intra_matrix[0] = 13;
3757 s->chroma_intra_matrix[0] = 14;
3759 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3761 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3767 s->current_picture_ptr->f->key_frame =
3769 s->current_picture_ptr->f->pict_type =
3770 s->current_picture.f->pict_type =
s->pict_type;
3772 if (
s->current_picture.f->key_frame)
3773 s->picture_in_gop_number=0;
3775 s->mb_x =
s->mb_y = 0;
3777 switch(
s->out_format) {
3778 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
3784 if (CONFIG_SPEEDHQ_ENCODER)
3788 if (CONFIG_H261_ENCODER)
3794 else if (CONFIG_MSMPEG4ENC &&
s->msmpeg4_version)
3796 else if (CONFIG_MPEG4_ENCODER &&
s->h263_pred) {
3809 else if (CONFIG_H263_ENCODER)
3813 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3820 s->header_bits=
bits -
s->last_bits;
3822 for(
i=1;
i<context_count;
i++){
3825 s->avctx->execute(
s->avctx,
encode_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3826 for(
i=1;
i<context_count;
i++){
3827 if (
s->pb.buf_end ==
s->thread_context[
i]->pb.buf)
3836 const int intra=
s->mb_intra;
3839 s->dct_count[intra]++;
3841 for(
i=0;
i<64;
i++){
3846 s->dct_error_sum[intra][
i] +=
level;
3847 level -=
s->dct_offset[intra][
i];
3850 s->dct_error_sum[intra][
i] -=
level;
3851 level +=
s->dct_offset[intra][
i];
3860 int16_t *
block,
int n,
3864 const uint8_t *scantable;
3865 const uint8_t *perm_scantable;
3867 unsigned int threshold1, threshold2;
3879 int coeff_count[64];
3880 int qmul, qadd, start_i, last_non_zero,
i,
dc;
3881 const int esc_length=
s->ac_esc_length;
3883 uint8_t * last_length;
3889 if(
s->dct_error_sum)
3895 else mpeg2_qscale =
qscale << 1;
3899 scantable=
s->intra_scantable.scantable;
3900 perm_scantable=
s->intra_scantable.permutated;
3917 qmat = n < 4 ?
s->q_intra_matrix[
qscale] :
s->q_chroma_intra_matrix[
qscale];
3918 matrix = n < 4 ?
s->intra_matrix :
s->chroma_intra_matrix;
3922 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
3923 length =
s->intra_chroma_ac_vlc_length;
3924 last_length=
s->intra_chroma_ac_vlc_last_length;
3926 length =
s->intra_ac_vlc_length;
3927 last_length=
s->intra_ac_vlc_last_length;
3930 scantable=
s->inter_scantable.scantable;
3931 perm_scantable=
s->inter_scantable.permutated;
3934 qmat =
s->q_inter_matrix[
qscale];
3936 length =
s->inter_ac_vlc_length;
3937 last_length=
s->inter_ac_vlc_last_length;
3942 threshold2= (threshold1<<1);
3944 for(
i=63;
i>=start_i;
i--) {
3945 const int j = scantable[
i];
3948 if(((
unsigned)(
level+threshold1))>threshold2){
3954 for(
i=start_i;
i<=last_non_zero;
i++) {
3955 const int j = scantable[
i];
3960 if(((
unsigned)(
level+threshold1))>threshold2){
3983 if(last_non_zero < start_i){
3984 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3985 return last_non_zero;
3988 score_tab[start_i]= 0;
3989 survivor[0]= start_i;
3992 for(
i=start_i;
i<=last_non_zero;
i++){
3993 int level_index, j, zero_distortion;
3995 int best_score=256*256*256*120;
3999 zero_distortion= dct_coeff*dct_coeff;
4001 for(level_index=0; level_index < coeff_count[
i]; level_index++){
4010 unquant_coeff= alevel*qmul + qadd;
4012 j =
s->idsp.idct_permutation[scantable[
i]];
4013 unquant_coeff = alevel *
matrix[j] * 8;
4015 j =
s->idsp.idct_permutation[scantable[
i]];
4017 unquant_coeff = (
int)( alevel * mpeg2_qscale *
matrix[j]) >> 4;
4018 unquant_coeff = (unquant_coeff - 1) | 1;
4020 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int)
matrix[j])) >> 5;
4021 unquant_coeff = (unquant_coeff - 1) | 1;
4026 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4028 if((
level&(~127)) == 0){
4029 for(j=survivor_count-1; j>=0; j--){
4030 int run=
i - survivor[j];
4032 score += score_tab[
i-
run];
4034 if(score < best_score){
4037 level_tab[
i+1]=
level-64;
4042 for(j=survivor_count-1; j>=0; j--){
4043 int run=
i - survivor[j];
4045 score += score_tab[
i-
run];
4046 if(score < last_score){
4049 last_level=
level-64;
4055 distortion += esc_length*
lambda;
4056 for(j=survivor_count-1; j>=0; j--){
4057 int run=
i - survivor[j];
4058 int score= distortion + score_tab[
i-
run];
4060 if(score < best_score){
4063 level_tab[
i+1]=
level-64;
4068 for(j=survivor_count-1; j>=0; j--){
4069 int run=
i - survivor[j];
4070 int score= distortion + score_tab[
i-
run];
4071 if(score < last_score){
4074 last_level=
level-64;
4082 score_tab[
i+1]= best_score;
4085 if(last_non_zero <= 27){
4086 for(; survivor_count; survivor_count--){
4087 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4091 for(; survivor_count; survivor_count--){
4092 if(score_tab[ survivor[survivor_count-1] ] <= best_score +
lambda)
4097 survivor[ survivor_count++ ]=
i+1;
4101 last_score= 256*256*256*120;
4102 for(
i= survivor[0];
i<=last_non_zero + 1;
i++){
4103 int score= score_tab[
i];
4107 if(score < last_score){
4110 last_level= level_tab[
i];
4111 last_run= run_tab[
i];
4116 s->coded_score[n] = last_score;
4119 last_non_zero= last_i - 1;
4120 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4122 if(last_non_zero < start_i)
4123 return last_non_zero;
4125 if(last_non_zero == 0 && start_i == 0){
4127 int best_score=
dc *
dc;
4129 for(
i=0;
i<coeff_count[0];
i++){
4132 int unquant_coeff, score, distortion;
4135 unquant_coeff= (alevel*qmul + qadd)>>3;
4137 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int)
matrix[0])) >> 5;
4138 unquant_coeff = (unquant_coeff - 1) | 1;
4140 unquant_coeff = (unquant_coeff + 4) >> 3;
4141 unquant_coeff<<= 3 + 3;
4143 distortion= (unquant_coeff -
dc) * (unquant_coeff -
dc);
4146 else score= distortion + esc_length*
lambda;
4148 if(score < best_score){
4150 best_level=
level - 64;
4153 block[0]= best_level;
4154 s->coded_score[n] = best_score -
dc*
dc;
4155 if(best_level == 0)
return -1;
4156 else return last_non_zero;
4162 block[ perm_scantable[last_non_zero] ]= last_level;
4165 for(;
i>start_i;
i -= run_tab[
i] + 1){
4166 block[ perm_scantable[
i-1] ]= level_tab[
i];
4169 return last_non_zero;
4184 if(
i==0)
s*= sqrt(0.5);
4185 if(j==0)
s*= sqrt(0.5);
4198 const uint8_t *scantable;
4199 const uint8_t *perm_scantable;
4205 int qmul, qadd, start_i, last_non_zero,
i,
dc;
4207 uint8_t * last_length;
4209 int rle_index,
run, q = 1, sum;
4211 if(
basis[0][0] == 0)
4217 scantable=
s->intra_scantable.scantable;
4218 perm_scantable=
s->intra_scantable.permutated;
4236 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
4237 length =
s->intra_chroma_ac_vlc_length;
4238 last_length=
s->intra_chroma_ac_vlc_last_length;
4240 length =
s->intra_ac_vlc_length;
4241 last_length=
s->intra_ac_vlc_last_length;