73 #define QUANT_BIAS_SHIFT 8
75 #define QMAT_SHIFT_MMX 16
89 #if FF_API_MPEGVIDEO_OPTS
100 const uint16_t *quant_matrix,
101 int bias,
int qmin,
int qmax,
int intra)
112 else qscale2 =
qscale << 1;
119 for (
i = 0;
i < 64;
i++) {
120 const int j =
s->idsp.idct_permutation[
i];
121 int64_t den = (int64_t) qscale2 * quant_matrix[j];
131 for (
i = 0;
i < 64;
i++) {
132 const int j =
s->idsp.idct_permutation[
i];
133 int64_t den =
ff_aanscales[
i] * (int64_t) qscale2 * quant_matrix[j];
143 for (
i = 0;
i < 64;
i++) {
144 const int j =
s->idsp.idct_permutation[
i];
145 int64_t den = (int64_t) qscale2 * quant_matrix[j];
165 for (
i = intra;
i < 64;
i++) {
177 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
184 if (
s->q_scale_type == 1 && 0) {
186 int bestdiff=INT_MAX;
194 if (
diff < bestdiff) {
203 s->qscale =
av_clip(
s->qscale,
s->avctx->qmin,
s->vbv_ignore_qmax ? 31 :
s->avctx->qmax);
216 for (
i = 0;
i < 64;
i++) {
228 int8_t *
const qscale_table =
s->current_picture.qscale_table;
231 for (
i = 0;
i <
s->mb_num;
i++) {
232 unsigned int lam =
s->lambda_table[
s->mb_index2xy[
i]];
234 qscale_table[
s->mb_index2xy[
i]] =
av_clip(qp,
s->avctx->qmin,
242 #define COPY(a) dst->a= src->a
260 for (
int i = -16;
i < 16;
i++)
279 s->input_picture_number = 0;
280 s->picture_in_gop_number = 0;
288 if (CONFIG_H263_ENCODER)
290 if (!
s->dct_quantize)
294 s->fast_dct_quantize =
s->dct_quantize;
295 if (
s->avctx->trellis)
328 #if FF_API_PRIVATE_OPT
345 "keyframe interval too large!, reducing it from %d to %d\n",
360 s->rtp_mode = !!
s->rtp_payload_size;
364 if (
s->intra_dc_precision < 0) {
365 s->intra_dc_precision += 8;
366 }
else if (
s->intra_dc_precision >= 8)
367 s->intra_dc_precision -= 8;
369 if (
s->intra_dc_precision < 0) {
371 "intra dc precision must be positive, note some applications use"
372 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
385 if (
s->gop_size <= 1) {
439 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
456 "impossible bitrate constraints, this will fail\n");
466 if (!
s->fixed_qscale &&
481 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
482 "specified vbv buffer is too large for the given bitrate!\n");
494 "OBMC is only supported with simple mb decision\n");
503 if (
s->max_b_frames &&
510 if (
s->max_b_frames < 0) {
512 "max b frames must be 0 or positive for mpegvideo based encoders\n");
522 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
564 #if FF_API_PRIVATE_OPT
573 "mpeg2 style quantization not supported by codec\n");
594 "QP RD is no longer compatible with MJPEG or AMV\n");
598 #if FF_API_PRIVATE_OPT
605 if (
s->scenechange_threshold < 1000000000 &&
608 "closed gop with scene change detection are not supported yet, "
609 "set threshold to 1000000000\n");
617 "low delay forcing is only available for mpeg2, "
618 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
621 if (
s->max_b_frames != 0) {
623 "B-frames cannot be used with low delay\n");
628 if (
s->q_scale_type == 1) {
631 "non linear quant only supports qmax <= 28 currently\n");
649 "multi threaded encoding not supported by codec\n");
655 "automatic thread number detection not supported by codec, "
660 #if FF_API_PRIVATE_OPT
671 "notice: b_frame_strategy only affects the first pass\n");
672 s->b_frame_strategy = 0;
686 s->inter_quant_bias = 0;
688 s->intra_quant_bias = 0;
703 "timebase %d/%d not supported by MPEG 4 standard, "
704 "the maximum admitted value for the timebase denominator "
715 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
720 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
723 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
737 if (!CONFIG_SPEEDHQ_ENCODER)
745 if (!CONFIG_H261_ENCODER)
749 "The specified picture size of %dx%d is not valid for the "
750 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
751 s->width,
s->height);
760 if (!CONFIG_H263_ENCODER)
763 s->width,
s->height) == 8) {
765 "The specified picture size of %dx%d is not valid for "
766 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
767 "352x288, 704x576, and 1408x1152. "
768 "Try H.263+.\n",
s->width,
s->height);
780 s->modified_quant =
s->h263_aic;
782 s->unrestricted_mv =
s->obmc ||
s->loop_filter ||
s->umvplus;
792 s->unrestricted_mv = 1;
806 s->modified_quant = 1;
810 s->unrestricted_mv = 0;
815 s->unrestricted_mv = 1;
816 s->low_delay =
s->max_b_frames ? 0 : 1;
817 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
822 s->unrestricted_mv = 1;
823 s->msmpeg4_version = 2;
830 s->unrestricted_mv = 1;
831 s->msmpeg4_version = 3;
832 s->flipflop_rounding = 1;
839 s->unrestricted_mv = 1;
840 s->msmpeg4_version = 4;
841 s->flipflop_rounding = 1;
848 s->unrestricted_mv = 1;
849 s->msmpeg4_version = 5;
850 s->flipflop_rounding = 1;
858 #if FF_API_PRIVATE_OPT
869 s->progressive_frame =
885 if (
s->msmpeg4_version) {
902 if (
s->noise_reduction) {
909 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) &&
s->modified_quant)
912 if (
s->slice_context_count > 1) {
916 s->h263_slice_structured = 1;
919 s->quant_precision = 5;
921 #if FF_API_PRIVATE_OPT
935 ff_set_cmp(&
s->mecc,
s->mecc.frame_skip_cmp,
s->frame_skip_cmp);
937 if (CONFIG_H261_ENCODER &&
s->out_format ==
FMT_H261)
939 if (CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
943 if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
948 for (
i = 0;
i < 64;
i++) {
949 int j =
s->idsp.idct_permutation[
i];
962 s->chroma_intra_matrix[j] =
986 #if FF_API_PRIVATE_OPT
996 if (
s->b_frame_strategy == 2) {
997 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
999 if (!
s->tmp_frames[
i])
1003 s->tmp_frames[
i]->width =
s->width >>
s->brd_scale;
1004 s->tmp_frames[
i]->height =
s->height >>
s->brd_scale;
1031 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
1046 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
1047 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
1048 s->q_chroma_intra_matrix=
NULL;
1049 s->q_chroma_intra_matrix16=
NULL;
1066 for (y = 0; y < 16; y++) {
1067 for (x = 0; x < 16; x++) {
1082 h =
s->height & ~15;
1084 for (y = 0; y <
h; y += 16) {
1085 for (x = 0; x <
w; x += 16) {
1092 acc += sae + 500 < sad;
1101 s->chroma_x_shift,
s->chroma_y_shift,
s->out_format,
1102 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
1103 &
s->linesize, &
s->uvlinesize);
1110 int i, display_picture_number = 0,
ret;
1111 int encoding_delay =
s->max_b_frames ?
s->max_b_frames
1112 : (
s->low_delay ? 0 : 1);
1113 int flush_offset = 1;
1118 display_picture_number =
s->input_picture_number++;
1122 int64_t last =
s->user_specified_pts;
1126 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1131 if (!
s->low_delay && display_picture_number == 1)
1132 s->dts_delta =
pts - last;
1134 s->user_specified_pts =
pts;
1137 s->user_specified_pts =
1138 pts =
s->user_specified_pts + 1;
1140 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1143 pts = display_picture_number;
1147 if (!pic_arg->
buf[0] ||
1149 pic_arg->
linesize[1] !=
s->uvlinesize ||
1152 if ((
s->width & 15) || (
s->height & 15))
1160 pic_arg->
linesize[1],
s->linesize,
s->uvlinesize);
1166 pic = &
s->picture[
i];
1183 int h_chroma_shift, v_chroma_shift;
1188 for (
i = 0;
i < 3;
i++) {
1190 int dst_stride =
i ?
s->uvlinesize :
s->linesize;
1191 int h_shift =
i ? h_chroma_shift : 0;
1192 int v_shift =
i ? v_chroma_shift : 0;
1193 int w =
s->width >> h_shift;
1194 int h =
s->height >> v_shift;
1200 && !
s->progressive_sequence
1201 &&
FFALIGN(
s->height, 32) -
s->height > 16)
1204 if (!
s->avctx->rc_buffer_size)
1207 if (src_stride == dst_stride)
1208 memcpy(dst,
src, src_stride *
h);
1213 memcpy(dst2,
src,
w);
1218 if ((
s->width & 15) || (
s->height & (vpad-1))) {
1219 s->mpvencdsp.draw_edges(dst, dst_stride,
1238 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1239 if (
s->input_picture[flush_offset])
1242 if (flush_offset <= 1)
1245 encoding_delay = encoding_delay - flush_offset + 1;
1250 s->input_picture[
i - flush_offset] =
s->input_picture[
i];
1252 s->input_picture[encoding_delay] = (
Picture*) pic;
1261 int64_t score64 = 0;
1263 for (plane = 0; plane < 3; plane++) {
1265 const int bw = plane ? 1 : 2;
1266 for (y = 0; y <
s->mb_height * bw; y++) {
1267 for (x = 0; x <
s->mb_width * bw; x++) {
1268 int off = p->
shared ? 0 : 16;
1271 int v =
s->mecc.frame_skip_cmp[1](
s, dptr, rptr,
stride, 8);
1273 switch (
FFABS(
s->frame_skip_exp)) {
1274 case 0: score =
FFMAX(score, v);
break;
1275 case 1: score +=
FFABS(v);
break;
1276 case 2: score64 += v * (int64_t)v;
break;
1277 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1278 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1287 if (
s->frame_skip_exp < 0)
1288 score64 = pow(score64 / (
double)(
s->mb_width *
s->mb_height),
1289 -1.0/
s->frame_skip_exp);
1293 if (score64 < ((
s->frame_skip_factor * (int64_t)
s->lambda) >> 8))
1323 const int scale =
s->brd_scale;
1324 int width =
s->width >> scale;
1325 int height =
s->height >> scale;
1327 int64_t best_rd = INT64_MAX;
1328 int best_b_count = -1;
1343 b_lambda = p_lambda;
1347 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1348 Picture pre_input, *pre_input_ptr =
i ?
s->input_picture[
i - 1] :
1349 s->next_picture_ptr;
1352 if (pre_input_ptr && (!
i ||
s->input_picture[
i - 1])) {
1353 pre_input = *pre_input_ptr;
1362 s->mpvencdsp.shrink[scale](
s->tmp_frames[
i]->data[0],
1363 s->tmp_frames[
i]->linesize[0],
1367 s->mpvencdsp.shrink[scale](
s->tmp_frames[
i]->data[1],
1368 s->tmp_frames[
i]->linesize[1],
1372 s->mpvencdsp.shrink[scale](
s->tmp_frames[
i]->data[2],
1373 s->tmp_frames[
i]->linesize[2],
1380 for (j = 0; j <
s->max_b_frames + 1; j++) {
1384 if (!
s->input_picture[j])
1397 c->mb_decision =
s->avctx->mb_decision;
1398 c->me_cmp =
s->avctx->me_cmp;
1399 c->mb_cmp =
s->avctx->mb_cmp;
1400 c->me_sub_cmp =
s->avctx->me_sub_cmp;
1402 c->time_base =
s->avctx->time_base;
1403 c->max_b_frames =
s->max_b_frames;
1421 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1422 int is_p =
i % (j + 1) == j ||
i ==
s->max_b_frames;
1424 s->tmp_frames[
i + 1]->pict_type = is_p ?
1426 s->tmp_frames[
i + 1]->quality = is_p ? p_lambda : b_lambda;
1445 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1463 return best_b_count;
1471 s->reordered_input_picture[
i - 1] =
s->reordered_input_picture[
i];
1475 if (!
s->reordered_input_picture[0] &&
s->input_picture[0]) {
1476 if (
s->frame_skip_threshold ||
s->frame_skip_factor) {
1477 if (
s->picture_in_gop_number <
s->gop_size &&
1478 s->next_picture_ptr &&
1490 !
s->next_picture_ptr ||
s->intra_only) {
1491 s->reordered_input_picture[0] =
s->input_picture[0];
1493 s->reordered_input_picture[0]->f->coded_picture_number =
1494 s->coded_picture_number++;
1499 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1500 int pict_num =
s->input_picture[0]->f->display_picture_number +
i;
1502 if (pict_num >=
s->rc_context.num_entries)
1504 if (!
s->input_picture[
i]) {
1509 s->input_picture[
i]->f->pict_type =
1510 s->rc_context.entry[pict_num].new_pict_type;
1514 if (
s->b_frame_strategy == 0) {
1515 b_frames =
s->max_b_frames;
1516 while (b_frames && !
s->input_picture[b_frames])
1518 }
else if (
s->b_frame_strategy == 1) {
1519 for (
i = 1;
i <
s->max_b_frames + 1;
i++) {
1520 if (
s->input_picture[
i] &&
1521 s->input_picture[
i]->b_frame_score == 0) {
1522 s->input_picture[
i]->b_frame_score =
1524 s->input_picture[
i ]->f->data[0],
1525 s->input_picture[
i - 1]->f->data[0],
1529 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1530 if (!
s->input_picture[
i] ||
1531 s->input_picture[
i]->b_frame_score - 1 >
1532 s->mb_num /
s->b_sensitivity)
1536 b_frames =
FFMAX(0,
i - 1);
1539 for (
i = 0;
i < b_frames + 1;
i++) {
1540 s->input_picture[
i]->b_frame_score = 0;
1542 }
else if (
s->b_frame_strategy == 2) {
1550 for (
i = b_frames - 1;
i >= 0;
i--) {
1551 int type =
s->input_picture[
i]->f->pict_type;
1556 b_frames ==
s->max_b_frames) {
1558 "warning, too many B-frames in a row\n");
1561 if (
s->picture_in_gop_number + b_frames >=
s->gop_size) {
1563 s->gop_size >
s->picture_in_gop_number) {
1564 b_frames =
s->gop_size -
s->picture_in_gop_number - 1;
1576 s->reordered_input_picture[0] =
s->input_picture[b_frames];
1579 s->reordered_input_picture[0]->f->coded_picture_number =
1580 s->coded_picture_number++;
1581 for (
i = 0;
i < b_frames;
i++) {
1582 s->reordered_input_picture[
i + 1] =
s->input_picture[
i];
1583 s->reordered_input_picture[
i + 1]->f->pict_type =
1585 s->reordered_input_picture[
i + 1]->f->coded_picture_number =
1586 s->coded_picture_number++;
1593 if (
s->reordered_input_picture[0]) {
1594 s->reordered_input_picture[0]->reference =
1595 s->reordered_input_picture[0]->f->pict_type !=
1601 if (
s->reordered_input_picture[0]->shared ||
s->avctx->rc_buffer_size) {
1609 pic = &
s->picture[
i];
1611 pic->
reference =
s->reordered_input_picture[0]->reference;
1622 s->reordered_input_picture[0]->shared = 0;
1624 s->current_picture_ptr = pic;
1627 s->current_picture_ptr =
s->reordered_input_picture[0];
1628 for (
i = 0;
i < 4;
i++) {
1629 if (
s->new_picture.f->data[
i])
1635 s->current_picture_ptr)) < 0)
1638 s->picture_number =
s->new_picture.f->display_picture_number;
1645 if (
s->unrestricted_mv &&
1646 s->current_picture.reference &&
1649 int hshift =
desc->log2_chroma_w;
1650 int vshift =
desc->log2_chroma_h;
1651 s->mpvencdsp.draw_edges(
s->current_picture.f->data[0],
1652 s->current_picture.f->linesize[0],
1653 s->h_edge_pos,
s->v_edge_pos,
1656 s->mpvencdsp.draw_edges(
s->current_picture.f->data[1],
1657 s->current_picture.f->linesize[1],
1658 s->h_edge_pos >> hshift,
1659 s->v_edge_pos >> vshift,
1663 s->mpvencdsp.draw_edges(
s->current_picture.f->data[2],
1664 s->current_picture.f->linesize[2],
1665 s->h_edge_pos >> hshift,
1666 s->v_edge_pos >> vshift,
1674 s->last_pict_type =
s->pict_type;
1675 s->last_lambda_for [
s->pict_type] =
s->current_picture_ptr->f->quality;
1677 s->last_non_b_pict_type =
s->pict_type;
1679 #if FF_API_CODED_FRAME
1685 #if FF_API_ERROR_FRAME
1687 memcpy(
s->current_picture.f->error,
s->current_picture.encoding_error,
1688 sizeof(
s->current_picture.encoding_error));
1697 for (intra = 0; intra < 2; intra++) {
1698 if (
s->dct_count[intra] > (1 << 16)) {
1699 for (
i = 0;
i < 64;
i++) {
1700 s->dct_error_sum[intra][
i] >>= 1;
1702 s->dct_count[intra] >>= 1;
1705 for (
i = 0;
i < 64;
i++) {
1706 s->dct_offset[intra][
i] = (
s->noise_reduction *
1707 s->dct_count[intra] +
1708 s->dct_error_sum[intra][
i] / 2) /
1709 (
s->dct_error_sum[intra][
i] + 1);
1720 s->last_picture_ptr !=
s->next_picture_ptr &&
1721 s->last_picture_ptr->f->buf[0]) {
1725 s->current_picture_ptr->f->pict_type =
s->pict_type;
1730 s->current_picture_ptr)) < 0)
1734 s->last_picture_ptr =
s->next_picture_ptr;
1736 s->next_picture_ptr =
s->current_picture_ptr;
1739 if (
s->last_picture_ptr) {
1741 if (
s->last_picture_ptr->f->buf[0] &&
1743 s->last_picture_ptr)) < 0)
1746 if (
s->next_picture_ptr) {
1748 if (
s->next_picture_ptr->f->buf[0] &&
1750 s->next_picture_ptr)) < 0)
1756 for (
i = 0;
i < 4;
i++) {
1758 s->current_picture.f->data[
i] +=
1759 s->current_picture.f->linesize[
i];
1761 s->current_picture.f->linesize[
i] *= 2;
1762 s->last_picture.f->linesize[
i] *= 2;
1763 s->next_picture.f->linesize[
i] *= 2;
1768 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
1769 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
1771 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
1772 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
1774 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
1775 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
1778 if (
s->dct_error_sum) {
1787 const AVFrame *pic_arg,
int *got_packet)
1790 int i, stuffing_count,
ret;
1791 int context_count =
s->slice_context_count;
1793 s->vbv_ignore_qmax = 0;
1795 s->picture_in_gop_number++;
1805 if (
s->new_picture.f->data[0]) {
1806 int growing_buffer = context_count == 1 && !
pkt->
data && !
s->data_partitioning;
1815 s->mb_width*
s->mb_height*12);
1816 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1819 for (
i = 0;
i < context_count;
i++) {
1820 int start_y =
s->thread_context[
i]->start_mb_y;
1822 int h =
s->mb_height;
1829 s->pict_type =
s->new_picture.f->pict_type;
1836 if (growing_buffer) {
1844 #if FF_API_STAT_BITS
1860 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
s->out_format ==
FMT_MJPEG)
1870 s->lambda <
s->lmax) {
1871 s->next_lambda =
FFMAX(
s->lambda + min_step,
s->lambda *
1872 (
s->qscale + 1) /
s->qscale);
1873 if (
s->adaptive_quant) {
1875 for (
i = 0;
i <
s->mb_height *
s->mb_stride;
i++)
1876 s->lambda_table[
i] =
1877 FFMAX(
s->lambda_table[
i] + min_step,
1878 s->lambda_table[
i] * (
s->qscale + 1) /
1884 if (
s->flipflop_rounding ||
1887 s->no_rounding ^= 1;
1890 s->time_base =
s->last_time_base;
1891 s->last_non_b_time =
s->time -
s->pp_time;
1893 for (
i = 0;
i < context_count;
i++) {
1897 s->vbv_ignore_qmax = 1;
1908 for (
i = 0;
i < 4;
i++) {
1909 s->current_picture_ptr->encoding_error[
i] =
s->current_picture.encoding_error[
i];
1910 avctx->
error[
i] +=
s->current_picture_ptr->encoding_error[
i];
1913 s->current_picture_ptr->encoding_error,
1919 s->misc_bits +
s->i_tex_bits +
1925 s->stuffing_bits = 8*stuffing_count;
1926 if (stuffing_count) {
1932 switch (
s->codec_id) {
1935 while (stuffing_count--) {
1942 stuffing_count -= 4;
1943 while (stuffing_count--) {
1966 int minbits =
s->frame_bits - 8 *
1967 (
s->vbv_delay_ptr -
s->pb.buf - 1);
1968 double bits =
s->rc_context.buffer_index + minbits - inbits;
1972 "Internal error, negative bits\n");
1984 s->vbv_delay_ptr[0] &= 0xF8;
1987 s->vbv_delay_ptr[2] &= 0x07;
2002 #if FF_API_VBV_DELAY
2008 s->total_bits +=
s->frame_bits;
2009 #if FF_API_STAT_BITS
2016 pkt->
pts =
s->current_picture.f->pts;
2018 if (!
s->current_picture.f->coded_picture_number)
2025 if (
s->current_picture.f->key_frame)
2035 if (!
s->picture[
i].reference)
2047 int n,
int threshold)
2049 static const char tab[64] = {
2050 3, 2, 2, 1, 1, 1, 1, 1,
2051 1, 1, 1, 1, 1, 1, 1, 1,
2052 1, 1, 1, 1, 1, 1, 1, 1,
2053 0, 0, 0, 0, 0, 0, 0, 0,
2054 0, 0, 0, 0, 0, 0, 0, 0,
2055 0, 0, 0, 0, 0, 0, 0, 0,
2056 0, 0, 0, 0, 0, 0, 0, 0,
2057 0, 0, 0, 0, 0, 0, 0, 0
2062 int16_t *
block =
s->block[n];
2063 const int last_index =
s->block_last_index[n];
2066 if (threshold < 0) {
2068 threshold = -threshold;
2073 if (last_index <= skip_dc - 1)
2076 for (
i = 0;
i <= last_index;
i++) {
2077 const int j =
s->intra_scantable.permutated[
i];
2080 if (skip_dc &&
i == 0)
2084 }
else if (
level > 1) {
2090 if (score >= threshold)
2092 for (
i = skip_dc;
i <= last_index;
i++) {
2093 const int j =
s->intra_scantable.permutated[
i];
2097 s->block_last_index[n] = 0;
2099 s->block_last_index[n] = -1;
2106 const int maxlevel =
s->max_qcoeff;
2107 const int minlevel =
s->min_qcoeff;
2115 for (;
i <= last_index;
i++) {
2116 const int j =
s->intra_scantable.permutated[
i];
2119 if (
level > maxlevel) {
2122 }
else if (
level < minlevel) {
2132 "warning, clipping %d dct coefficients to %d..%d\n",
2140 for (y = 0; y < 8; y++) {
2141 for (x = 0; x < 8; x++) {
2147 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2148 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2149 int v = ptr[x2 + y2 *
stride];
2155 weight[x + 8 * y]= (36 *
ff_sqrt(count * sqr - sum * sum)) / count;
2161 int motion_x,
int motion_y,
2162 int mb_block_height,
2167 int16_t orig[12][64];
2168 const int mb_x =
s->mb_x;
2169 const int mb_y =
s->mb_y;
2173 int uv_dct_offset =
s->uvlinesize * 8;
2174 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2175 ptrdiff_t wrap_y, wrap_c;
2177 for (
i = 0;
i < mb_block_count;
i++)
2178 skip_dct[
i] =
s->skipdct;
2180 if (
s->adaptive_quant) {
2181 const int last_qp =
s->qscale;
2182 const int mb_xy =
mb_x +
mb_y *
s->mb_stride;
2184 s->lambda =
s->lambda_table[mb_xy];
2188 s->qscale =
s->current_picture_ptr->qscale_table[mb_xy];
2189 s->dquant =
s->qscale - last_qp;
2210 wrap_y =
s->linesize;
2211 wrap_c =
s->uvlinesize;
2212 ptr_y =
s->new_picture.f->data[0] +
2214 ptr_cb =
s->new_picture.f->data[1] +
2215 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2216 ptr_cr =
s->new_picture.f->data[2] +
2217 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2220 uint8_t *ebuf =
s->sc.edge_emu_buffer + 38 * wrap_y;
2221 int cw = (
s->width +
s->chroma_x_shift) >>
s->chroma_x_shift;
2222 int ch = (
s->height +
s->chroma_y_shift) >>
s->chroma_y_shift;
2223 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2226 s->width,
s->height);
2228 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2230 mb_block_width, mb_block_height,
2231 mb_x * mb_block_width,
mb_y * mb_block_height,
2233 ptr_cb = ebuf + 16 * wrap_y;
2234 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2236 mb_block_width, mb_block_height,
2237 mb_x * mb_block_width,
mb_y * mb_block_height,
2239 ptr_cr = ebuf + 16 * wrap_y + 16;
2244 int progressive_score, interlaced_score;
2246 s->interlaced_dct = 0;
2247 progressive_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
NULL, wrap_y, 8) +
2248 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y * 8,
2249 NULL, wrap_y, 8) - 400;
2251 if (progressive_score > 0) {
2252 interlaced_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
2253 NULL, wrap_y * 2, 8) +
2254 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y,
2255 NULL, wrap_y * 2, 8);
2256 if (progressive_score > interlaced_score) {
2257 s->interlaced_dct = 1;
2260 uv_dct_offset = wrap_c;
2269 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2270 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2271 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2272 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2278 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2279 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2280 if (!
s->chroma_y_shift &&
s->chroma_x_shift) {
2281 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2282 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2283 }
else if (!
s->chroma_y_shift && !
s->chroma_x_shift) {
2284 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2285 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2286 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2287 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2288 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2289 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2295 uint8_t *dest_y, *dest_cb, *dest_cr;
2297 dest_y =
s->dest[0];
2298 dest_cb =
s->dest[1];
2299 dest_cr =
s->dest[2];
2302 op_pix =
s->hdsp.put_pixels_tab;
2303 op_qpix =
s->qdsp.put_qpel_pixels_tab;
2305 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
2306 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
2311 s->last_picture.f->data,
2313 op_pix =
s->hdsp.avg_pixels_tab;
2314 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
2318 s->next_picture.f->data,
2323 int progressive_score, interlaced_score;
2325 s->interlaced_dct = 0;
2326 progressive_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2327 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y * 8,
2332 progressive_score -= 400;
2334 if (progressive_score > 0) {
2335 interlaced_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y,
2337 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y,
2341 if (progressive_score > interlaced_score) {
2342 s->interlaced_dct = 1;
2345 uv_dct_offset = wrap_c;
2353 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2354 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2357 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2364 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2365 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2366 if (!
s->chroma_y_shift) {
2367 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2368 dest_cb + uv_dct_offset, wrap_c);
2369 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2370 dest_cr + uv_dct_offset, wrap_c);
2374 if (
s->current_picture.mc_mb_var[
s->mb_stride *
mb_y +
mb_x] <
2375 2 *
s->qscale *
s->qscale) {
2377 if (
s->mecc.sad[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->qscale)
2379 if (
s->mecc.sad[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->qscale)
2382 wrap_y, 8) < 20 *
s->qscale)
2385 wrap_y, 8) < 20 *
s->qscale)
2387 if (
s->mecc.sad[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->qscale)
2389 if (
s->mecc.sad[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->qscale)
2391 if (!
s->chroma_y_shift) {
2392 if (
s->mecc.sad[1](
NULL, ptr_cb + uv_dct_offset,
2393 dest_cb + uv_dct_offset,
2394 wrap_c, 8) < 20 *
s->qscale)
2396 if (
s->mecc.sad[1](
NULL, ptr_cr + uv_dct_offset,
2397 dest_cr + uv_dct_offset,
2398 wrap_c, 8) < 20 *
s->qscale)
2404 if (
s->quantizer_noise_shaping) {
2417 if (!
s->chroma_y_shift) {
2425 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2431 for (
i = 0;
i < mb_block_count;
i++) {
2434 s->block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->qscale, &
overflow);
2443 s->block_last_index[
i] = -1;
2445 if (
s->quantizer_noise_shaping) {
2446 for (
i = 0;
i < mb_block_count;
i++) {
2448 s->block_last_index[
i] =
2450 orig[
i],
i,
s->qscale);
2455 if (
s->luma_elim_threshold && !
s->mb_intra)
2456 for (
i = 0;
i < 4;
i++)
2458 if (
s->chroma_elim_threshold && !
s->mb_intra)
2459 for (
i = 4;
i < mb_block_count;
i++)
2463 for (
i = 0;
i < mb_block_count;
i++) {
2464 if (
s->block_last_index[
i] == -1)
2465 s->coded_score[
i] = INT_MAX / 256;
2471 s->block_last_index[4] =
2472 s->block_last_index[5] = 0;
2474 s->block[5][0] = (1024 +
s->c_dc_scale / 2) /
s->c_dc_scale;
2475 if (!
s->chroma_y_shift) {
2476 for (
i=6;
i<12;
i++) {
2477 s->block_last_index[
i] = 0;
2478 s->block[
i][0] =
s->block[4][0];
2485 for (
i = 0;
i < mb_block_count;
i++) {
2487 if (
s->block_last_index[
i] > 0) {
2488 for (j = 63; j > 0; j--) {
2489 if (
s->block[
i][
s->intra_scantable.permutated[j]])
2492 s->block_last_index[
i] = j;
2498 switch(
s->codec_id){
2501 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2505 if (CONFIG_MPEG4_ENCODER)
2515 if (CONFIG_WMV2_ENCODER)
2519 if (CONFIG_H261_ENCODER)
2527 if (CONFIG_H263_ENCODER)
2530 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2537 if (CONFIG_SPEEDHQ_ENCODER)
2555 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2558 d->mb_skip_run=
s->mb_skip_run;
2560 d->last_dc[
i] =
s->last_dc[
i];
2563 d->mv_bits=
s->mv_bits;
2564 d->i_tex_bits=
s->i_tex_bits;
2565 d->p_tex_bits=
s->p_tex_bits;
2566 d->i_count=
s->i_count;
2567 d->f_count=
s->f_count;
2568 d->b_count=
s->b_count;
2569 d->skip_count=
s->skip_count;
2570 d->misc_bits=
s->misc_bits;
2574 d->qscale=
s->qscale;
2575 d->dquant=
s->dquant;
2577 d->esc3_level_length=
s->esc3_level_length;
2583 memcpy(
d->mv,
s->mv, 2*4*2*
sizeof(
int));
2584 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2587 d->mb_skip_run=
s->mb_skip_run;
2589 d->last_dc[
i] =
s->last_dc[
i];
2592 d->mv_bits=
s->mv_bits;
2593 d->i_tex_bits=
s->i_tex_bits;
2594 d->p_tex_bits=
s->p_tex_bits;
2595 d->i_count=
s->i_count;
2596 d->f_count=
s->f_count;
2597 d->b_count=
s->b_count;
2598 d->skip_count=
s->skip_count;
2599 d->misc_bits=
s->misc_bits;
2601 d->mb_intra=
s->mb_intra;
2602 d->mb_skipped=
s->mb_skipped;
2603 d->mv_type=
s->mv_type;
2604 d->mv_dir=
s->mv_dir;
2606 if(
s->data_partitioning){
2608 d->tex_pb=
s->tex_pb;
2612 d->block_last_index[
i]=
s->block_last_index[
i];
2613 d->interlaced_dct=
s->interlaced_dct;
2614 d->qscale=
s->qscale;
2616 d->esc3_level_length=
s->esc3_level_length;
2621 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2628 s->block=
s->blocks[*next_block];
2629 s->pb=
pb[*next_block];
2630 if(
s->data_partitioning){
2631 s->pb2 =
pb2 [*next_block];
2632 s->tex_pb=
tex_pb[*next_block];
2636 memcpy(dest_backup,
s->dest,
sizeof(
s->dest));
2637 s->dest[0] =
s->sc.rd_scratchpad;
2638 s->dest[1] =
s->sc.rd_scratchpad + 16*
s->linesize;
2639 s->dest[2] =
s->sc.rd_scratchpad + 16*
s->linesize + 8;
2646 if(
s->data_partitioning){
2654 score *=
s->lambda2;
2659 memcpy(
s->dest, dest_backup,
sizeof(
s->dest));
2677 else if(
w==8 &&
h==8)
2695 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
2696 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
2700 return s->mecc.nsse[0](
s,
s->new_picture.f->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
s->dest[0],
s->linesize, 16) +
2701 s->mecc.nsse[1](
s,
s->new_picture.f->data[1] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[1],
s->uvlinesize, 8) +
2702 s->mecc.nsse[1](
s,
s->new_picture.f->data[2] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[2],
s->uvlinesize, 8);
2704 return s->mecc.sse[0](
NULL,
s->new_picture.f->data[0] +
s->mb_x * 16 +
s->mb_y *
s->linesize * 16,
s->dest[0],
s->linesize, 16) +
2705 s->mecc.sse[1](
NULL,
s->new_picture.f->data[1] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[1],
s->uvlinesize, 8) +
2706 s->mecc.sse[1](
NULL,
s->new_picture.f->data[2] +
s->mb_x * 8 +
s->mb_y *
s->uvlinesize * 8,
s->dest[2],
s->uvlinesize, 8);
2709 return sse(
s,
s->new_picture.f->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
s->dest[0],
w,
h,
s->linesize)
2710 +
sse(
s,
s->new_picture.f->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*8,
s->dest[1],
w>>1,
h>>1,
s->uvlinesize)
2711 +
sse(
s,
s->new_picture.f->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*8,
s->dest[2],
w>>1,
h>>1,
s->uvlinesize);
2719 s->me.dia_size=
s->avctx->pre_dia_size;
2720 s->first_slice_line=1;
2721 for(
s->mb_y=
s->end_mb_y-1;
s->mb_y >=
s->start_mb_y;
s->mb_y--) {
2722 for(
s->mb_x=
s->mb_width-1;
s->mb_x >=0 ;
s->mb_x--) {
2725 s->first_slice_line=0;
2736 s->me.dia_size=
s->avctx->dia_size;
2737 s->first_slice_line=1;
2738 for(
s->mb_y=
s->start_mb_y;
s->mb_y <
s->end_mb_y;
s->mb_y++) {
2741 for(
s->mb_x=0;
s->mb_x <
s->mb_width;
s->mb_x++) {
2742 s->block_index[0]+=2;
2743 s->block_index[1]+=2;
2744 s->block_index[2]+=2;
2745 s->block_index[3]+=2;
2753 s->first_slice_line=0;
2766 uint8_t *pix =
s->new_picture.f->data[0] + (yy *
s->linesize) + xx;
2768 int sum =
s->mpvencdsp.pix_sum(pix,
s->linesize);
2770 varc = (
s->mpvencdsp.pix_norm1(pix,
s->linesize) -
2771 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2773 s->current_picture.mb_var [
s->mb_stride *
mb_y +
mb_x] = varc;
2774 s->current_picture.mb_mean[
s->mb_stride *
mb_y +
mb_x] = (sum+128)>>8;
2775 s->me.mb_var_sum_temp += varc;
2783 if(
s->partitioned_frame){
2788 }
else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2791 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->out_format ==
FMT_SPEEDHQ) {
2803 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2805 int mba =
s->mb_x +
s->mb_width * (
s->mb_y %
s->gob_index);
2806 int gobn =
s->mb_y /
s->gob_index;
2808 if (CONFIG_H263_ENCODER)
2810 bytestream_put_le32(&ptr,
offset);
2811 bytestream_put_byte(&ptr,
s->qscale);
2812 bytestream_put_byte(&ptr, gobn);
2813 bytestream_put_le16(&ptr, mba);
2814 bytestream_put_byte(&ptr, pred_x);
2815 bytestream_put_byte(&ptr, pred_y);
2817 bytestream_put_byte(&ptr, 0);
2818 bytestream_put_byte(&ptr, 0);
2826 s->mb_info_size += 12;
2827 s->prev_mb_info =
s->last_mb_info;
2839 if (!
s->mb_info_size)
2840 s->mb_info_size += 12;
2847 &&
s->slice_context_count == 1
2848 &&
s->pb.buf ==
s->avctx->internal->byte_buffer) {
2849 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2850 int vbv_pos =
s->vbv_delay_ptr -
s->pb.buf;
2853 int new_buffer_size = 0;
2855 if ((
s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2863 s->avctx->internal->byte_buffer_size + size_increase);
2867 memcpy(new_buffer,
s->avctx->internal->byte_buffer,
s->avctx->internal->byte_buffer_size);
2868 av_free(
s->avctx->internal->byte_buffer);
2869 s->avctx->internal->byte_buffer = new_buffer;
2870 s->avctx->internal->byte_buffer_size = new_buffer_size;
2872 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2873 s->vbv_delay_ptr =
s->pb.buf + vbv_pos;
2883 int chr_h= 16>>
s->chroma_y_shift;
2910 s->last_dc[
i] = 128 <<
s->intra_dc_precision;
2912 s->current_picture.encoding_error[
i] = 0;
2915 s->last_dc[0] = 128*8/13;
2916 s->last_dc[1] = 128*8/14;
2917 s->last_dc[2] = 128*8/14;
2920 memset(
s->last_mv, 0,
sizeof(
s->last_mv));
2924 switch(
s->codec_id){
2928 if (CONFIG_H263_ENCODER)
2932 if(CONFIG_MPEG4_ENCODER &&
s->partitioned_frame)
2939 s->first_slice_line = 1;
2940 s->ptr_lastgob =
s->pb.buf;
2941 for (mb_y_order =
s->start_mb_y; mb_y_order < s->
end_mb_y; mb_y_order++) {
2945 if (first_in_slice && mb_y_order !=
s->start_mb_y)
2947 s->last_dc[0] =
s->last_dc[1] =
s->last_dc[2] = 1024 <<
s->intra_dc_precision;
2963 int size_increase =
s->avctx->internal->byte_buffer_size/4
2971 if(
s->data_partitioning){
2985 xy=
s->mb_y*
s->mb_stride +
s->mb_x;
2991 int current_packet_size, is_gob_start;
2994 - (
s->ptr_lastgob -
s->pb.buf);
2996 is_gob_start =
s->rtp_payload_size &&
2997 current_packet_size >=
s->rtp_payload_size &&
3000 if(
s->start_mb_y ==
mb_y &&
mb_y > 0 &&
mb_x==0) is_gob_start=1;
3002 switch(
s->codec_id){
3005 if(!
s->h263_slice_structured)
3006 if(
s->mb_x ||
s->mb_y%
s->gob_index) is_gob_start=0;
3009 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
3011 if(
s->mb_skip_run) is_gob_start=0;
3014 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
3030 if (
s->error_rate &&
s->resync_mb_x +
s->resync_mb_y > 0) {
3032 int d = 100 /
s->error_rate;
3034 current_packet_size=0;
3035 s->pb.buf_ptr=
s->ptr_lastgob;
3040 #if FF_API_RTP_CALLBACK
3042 if (
s->avctx->rtp_callback){
3043 int number_mb = (
mb_y -
s->resync_mb_y)*
s->mb_width +
mb_x -
s->resync_mb_x;
3044 s->avctx->rtp_callback(
s->avctx,
s->ptr_lastgob, current_packet_size, number_mb);
3050 switch(
s->codec_id){
3052 if (CONFIG_MPEG4_ENCODER) {
3059 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
3066 if (CONFIG_H263_ENCODER)
3073 s->misc_bits+=
bits -
s->last_bits;
3077 s->ptr_lastgob += current_packet_size;
3078 s->first_slice_line=1;
3079 s->resync_mb_x=
mb_x;
3080 s->resync_mb_y=
mb_y;
3084 if( (
s->resync_mb_x ==
s->mb_x)
3085 &&
s->resync_mb_y+1 ==
s->mb_y){
3086 s->first_slice_line=0;
3096 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
3102 if(
s->data_partitioning){
3103 backup_s.pb2=
s->pb2;
3104 backup_s.tex_pb=
s->tex_pb;
3111 s->mv[0][0][0] =
s->p_mv_table[xy][0];
3112 s->mv[0][0][1] =
s->p_mv_table[xy][1];
3114 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3121 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3122 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3123 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3126 &dmin, &next_block, 0, 0);
3135 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3142 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3143 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3146 &dmin, &next_block, 0, 0);
3152 s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3153 s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3155 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3161 s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3162 s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3164 &dmin, &next_block,
s->mv[1][0][0],
s->mv[1][0][1]);
3170 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3171 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3172 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3173 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3175 &dmin, &next_block, 0, 0);
3182 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3183 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3184 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3187 &dmin, &next_block, 0, 0);
3194 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3195 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3196 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3199 &dmin, &next_block, 0, 0);
3205 for(dir=0; dir<2; dir++){
3207 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3208 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3209 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3213 &dmin, &next_block, 0, 0);
3222 &dmin, &next_block, 0, 0);
3223 if(
s->h263_pred ||
s->h263_aic){
3225 s->mbintra_table[
mb_x +
mb_y*
s->mb_stride]=1;
3233 const int last_qp= backup_s.qscale;
3237 static const int dquant_tab[4]={-1,1,-2,2};
3238 int storecoefs =
s->mb_intra &&
s->dc_val[0];
3246 s->mv[0][0][0] = best_s.
mv[0][0][0];
3247 s->mv[0][0][1] = best_s.
mv[0][0][1];
3248 s->mv[1][0][0] = best_s.
mv[1][0][0];
3249 s->mv[1][0][1] = best_s.
mv[1][0][1];
3252 for(; qpi<4; qpi++){
3253 int dquant= dquant_tab[qpi];
3255 if(qp < s->
avctx->
qmin || qp >
s->avctx->qmax)
3260 dc[
i]=
s->dc_val[0][
s->block_index[
i] ];
3261 memcpy(ac[
i],
s->ac_val[0][
s->block_index[
i]],
sizeof(int16_t)*16);
3266 &dmin, &next_block,
s->mv[mvdir][0][0],
s->mv[mvdir][0][1]);
3270 s->dc_val[0][
s->block_index[
i] ]=
dc[
i];
3271 memcpy(
s->ac_val[0][
s->block_index[
i]], ac[
i],
sizeof(int16_t)*16);
3279 int mx=
s->b_direct_mv_table[xy][0];
3280 int my=
s->b_direct_mv_table[xy][1];
3282 backup_s.dquant = 0;
3287 &dmin, &next_block, mx, my);
3290 backup_s.dquant = 0;
3295 &dmin, &next_block, 0, 0);
3300 coded |=
s->block_last_index[
i];
3303 memcpy(
s->mv, best_s.
mv,
sizeof(
s->mv));
3325 &dmin, &next_block, mx, my);
3330 s->current_picture.qscale_table[xy] = best_s.
qscale;
3336 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3339 if(
s->data_partitioning){
3342 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3343 s->pb2= backup_s.pb2;
3347 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3348 s->tex_pb= backup_s.tex_pb;
3352 if (CONFIG_H263_ENCODER &&
3357 s->hdsp.put_pixels_tab[0][0](
s->dest[0],
s->sc.rd_scratchpad ,
s->linesize ,16);
3358 s->hdsp.put_pixels_tab[1][0](
s->dest[1],
s->sc.rd_scratchpad + 16*
s->linesize ,
s->uvlinesize, 8);
3359 s->hdsp.put_pixels_tab[1][0](
s->dest[2],
s->sc.rd_scratchpad + 16*
s->linesize + 8,
s->uvlinesize, 8);
3365 int motion_x = 0, motion_y = 0;
3373 motion_x=
s->mv[0][0][0] = 0;
3374 motion_y=
s->mv[0][0][1] = 0;
3379 motion_x=
s->mv[0][0][0] =
s->p_mv_table[xy][0];
3380 motion_y=
s->mv[0][0][1] =
s->p_mv_table[xy][1];
3387 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3388 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3389 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3397 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3398 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3402 if (CONFIG_MPEG4_ENCODER) {
3405 motion_x=
s->b_direct_mv_table[xy][0];
3406 motion_y=
s->b_direct_mv_table[xy][1];
3411 if (CONFIG_MPEG4_ENCODER) {
3420 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3421 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3422 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3423 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3428 motion_x=
s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3429 motion_y=
s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3434 motion_x=
s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3435 motion_y=
s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3442 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3443 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3444 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3452 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3453 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3454 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3461 for(dir=0; dir<2; dir++){
3463 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3464 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3465 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3476 s->last_mv_dir =
s->mv_dir;
3478 if (CONFIG_H263_ENCODER &&
3487 s->p_mv_table[xy][0]=0;
3488 s->p_mv_table[xy][1]=0;
3495 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
3496 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
3498 s->current_picture.encoding_error[0] +=
sse(
3499 s,
s->new_picture.f->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
3500 s->dest[0],
w,
h,
s->linesize);
3501 s->current_picture.encoding_error[1] +=
sse(
3502 s,
s->new_picture.f->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3503 s->dest[1],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3504 s->current_picture.encoding_error[2] +=
sse(
3505 s,
s->new_picture.f->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3506 s->dest[2],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3509 if(CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
3512 ff_dlog(
s->avctx,
"MB %d %d bits\n",
3523 #if FF_API_RTP_CALLBACK
3526 if (
s->avctx->rtp_callback) {
3527 int number_mb = (
mb_y -
s->resync_mb_y)*
s->mb_width -
s->resync_mb_x;
3531 s->avctx->rtp_callback(
s->avctx,
s->ptr_lastgob, pdif, number_mb);
3539 #define MERGE(field) dst->field += src->field; src->field=0
3566 for(
i=0;
i<64;
i++){
3579 if (
s->next_lambda){
3580 s->current_picture_ptr->f->quality =
3581 s->current_picture.f->quality =
s->next_lambda;
3582 if(!dry_run)
s->next_lambda= 0;
3583 }
else if (!
s->fixed_qscale) {
3585 s->current_picture_ptr->f->quality =
3586 s->current_picture.f->quality =
quality;
3587 if (
s->current_picture.f->quality < 0)
3591 if(
s->adaptive_quant){
3592 switch(
s->codec_id){
3594 if (CONFIG_MPEG4_ENCODER)
3600 if (CONFIG_H263_ENCODER)
3607 s->lambda=
s->lambda_table[0];
3610 s->lambda =
s->current_picture.f->quality;
3618 s->time =
s->current_picture_ptr->f->pts *
s->avctx->time_base.num;
3621 s->pb_time=
s->pp_time - (
s->last_non_b_time -
s->time);
3624 s->pp_time=
s->time -
s->last_non_b_time;
3625 s->last_non_b_time=
s->time;
3634 int context_count =
s->slice_context_count;
3639 s->me.mb_var_sum_temp =
3640 s->me.mc_mb_var_sum_temp = 0;
3649 s->me.scene_change_score=0;
3654 if(
s->msmpeg4_version >= 3)
s->no_rounding=1;
3655 else s->no_rounding=0;
3658 s->no_rounding ^= 1;
3667 s->lambda=
s->last_lambda_for[
s->pict_type];
3669 s->lambda=
s->last_lambda_for[
s->last_non_b_pict_type];
3674 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
3675 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
3676 s->q_chroma_intra_matrix =
s->q_intra_matrix;
3677 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
3681 for(
i=1;
i<context_count;
i++){
3692 s->lambda = (
s->lambda *
s->me_penalty_compensation + 128) >> 8;
3693 s->lambda2 = (
s->lambda2 * (int64_t)
s->me_penalty_compensation + 128) >> 8;
3704 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3707 if(!
s->fixed_qscale){
3709 s->avctx->execute(
s->avctx,
mb_var_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3712 for(
i=1;
i<context_count;
i++){
3715 s->current_picture.mc_mb_var_sum=
s->current_picture_ptr->mc_mb_var_sum=
s->me.mc_mb_var_sum_temp;
3716 s->current_picture. mb_var_sum=
s->current_picture_ptr-> mb_var_sum=
s->me. mb_var_sum_temp;
3719 if (
s->me.scene_change_score >
s->scenechange_threshold &&
3722 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3724 if(
s->msmpeg4_version >= 3)
3726 ff_dlog(
s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3727 s->current_picture.mb_var_sum,
s->current_picture.mc_mb_var_sum);
3770 for(dir=0; dir<2; dir++){
3776 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3787 if (
s->qscale < 3 &&
s->max_qcoeff <= 128 &&
3796 if (
s->avctx->intra_matrix) {
3798 luma_matrix =
s->avctx->intra_matrix;
3800 if (
s->avctx->chroma_intra_matrix)
3801 chroma_matrix =
s->avctx->chroma_intra_matrix;
3805 int j =
s->idsp.idct_permutation[
i];
3807 s->chroma_intra_matrix[j] =
av_clip_uint8((chroma_matrix[
i] *
s->qscale) >> 3);
3810 s->y_dc_scale_table=
3812 s->chroma_intra_matrix[0] =
3815 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3817 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3821 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};
3822 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};
3829 s->y_dc_scale_table= y;
3830 s->c_dc_scale_table=
c;
3831 s->intra_matrix[0] = 13;
3832 s->chroma_intra_matrix[0] = 14;
3834 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3836 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3841 s->y_dc_scale_table=
3846 s->current_picture_ptr->f->key_frame =
3848 s->current_picture_ptr->f->pict_type =
3849 s->current_picture.f->pict_type =
s->pict_type;
3851 if (
s->current_picture.f->key_frame)
3852 s->picture_in_gop_number=0;
3854 s->mb_x =
s->mb_y = 0;
3856 switch(
s->out_format) {
3857 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
3862 s->pred,
s->intra_matrix,
s->chroma_intra_matrix);
3866 if (CONFIG_SPEEDHQ_ENCODER)
3870 if (CONFIG_H261_ENCODER)
3878 else if (CONFIG_MPEG4_ENCODER &&
s->h263_pred) {
3891 else if (CONFIG_H263_ENCODER)
3895 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3902 s->header_bits=
bits -
s->last_bits;
3904 for(
i=1;
i<context_count;
i++){
3907 s->avctx->execute(
s->avctx,
encode_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3908 for(
i=1;
i<context_count;
i++){
3909 if (
s->pb.buf_end ==
s->thread_context[
i]->pb.buf)
3918 const int intra=
s->mb_intra;
3921 s->dct_count[intra]++;
3923 for(
i=0;
i<64;
i++){
3928 s->dct_error_sum[intra][
i] +=
level;
3929 level -=
s->dct_offset[intra][
i];
3932 s->dct_error_sum[intra][
i] -=
level;
3933 level +=
s->dct_offset[intra][
i];
3942 int16_t *
block,
int n,
3945 const uint16_t *matrix;
3947 const uint8_t *perm_scantable;
3949 unsigned int threshold1, threshold2;
3961 int coeff_count[64];
3962 int qmul, qadd, start_i, last_non_zero,
i,
dc;
3963 const int esc_length=
s->ac_esc_length;
3971 if(
s->dct_error_sum)
3977 else mpeg2_qscale =
qscale << 1;
3981 scantable=
s->intra_scantable.scantable;
3982 perm_scantable=
s->intra_scantable.permutated;
3999 qmat = n < 4 ?
s->q_intra_matrix[
qscale] :
s->q_chroma_intra_matrix[
qscale];
4000 matrix = n < 4 ?
s->intra_matrix :
s->chroma_intra_matrix;
4004 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
4005 length =
s->intra_chroma_ac_vlc_length;
4006 last_length=
s->intra_chroma_ac_vlc_last_length;
4008 length =
s->intra_ac_vlc_length;
4009 last_length=
s->intra_ac_vlc_last_length;
4012 scantable=
s->inter_scantable.scantable;
4013 perm_scantable=
s->inter_scantable.permutated;
4016 qmat =
s->q_inter_matrix[
qscale];
4017 matrix =
s->inter_matrix;
4018 length =
s->inter_ac_vlc_length;
4019 last_length=
s->inter_ac_vlc_last_length;
4024 threshold2= (threshold1<<1);
4026 for(
i=63;
i>=start_i;
i--) {
4027 const int j = scantable[
i];
4030 if(((
unsigned)(
level+threshold1))>threshold2){
4036 for(
i=start_i;
i<=last_non_zero;
i++) {
4037 const int j = scantable[
i];
4042 if(((
unsigned)(
level+threshold1))>threshold2){
4065 if(last_non_zero < start_i){
4066 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4067 return last_non_zero;
4070 score_tab[start_i]= 0;
4071 survivor[0]= start_i;
4074 for(
i=start_i;
i<=last_non_zero;
i++){
4075 int level_index, j, zero_distortion;
4077 int best_score=256*256*256*120;
4081 zero_distortion= dct_coeff*dct_coeff;
4083 for(level_index=0; level_index < coeff_count[
i]; level_index++){
4092 unquant_coeff= alevel*qmul + qadd;
4094 j =
s->idsp.idct_permutation[scantable[
i]];
4095 unquant_coeff = alevel * matrix[j] * 8;
4097 j =
s->idsp.idct_permutation[scantable[
i]];
4099 unquant_coeff = (
int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
4100 unquant_coeff = (unquant_coeff - 1) | 1;
4102 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int) matrix[j])) >> 5;
4103 unquant_coeff = (unquant_coeff - 1) | 1;
4108 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
4110 if((
level&(~127)) == 0){
4111 for(j=survivor_count-1; j>=0; j--){
4112 int run=
i - survivor[j];
4114 score += score_tab[
i-
run];
4116 if(score < best_score){
4119 level_tab[
i+1]=
level-64;
4124 for(j=survivor_count-1; j>=0; j--){
4125 int run=
i - survivor[j];
4127 score += score_tab[
i-
run];
4128 if(score < last_score){
4131 last_level=
level-64;
4137 distortion += esc_length*
lambda;
4138 for(j=survivor_count-1; j>=0; j--){
4139 int run=
i - survivor[j];
4140 int score= distortion + score_tab[
i-
run];
4142 if(score < best_score){
4145 level_tab[
i+1]=
level-64;
4150 for(j=survivor_count-1; j>=0; j--){
4151 int run=
i - survivor[j];
4152 int score= distortion + score_tab[
i-
run];
4153 if(score < last_score){
4156 last_level=
level-64;
4164 score_tab[
i+1]= best_score;
4167 if(last_non_zero <= 27){
4168 for(; survivor_count; survivor_count--){
4169 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4173 for(; survivor_count; survivor_count--){
4174 if(score_tab[ survivor[survivor_count-1] ] <= best_score +
lambda)
4179 survivor[ survivor_count++ ]=
i+1;
4183 last_score= 256*256*256*120;
4184 for(
i= survivor[0];
i<=last_non_zero + 1;
i++){
4185 int score= score_tab[
i];