74 #define QUANT_BIAS_SHIFT 8
76 #define QMAT_SHIFT_MMX 16
90 #if FF_API_MPEGVIDEO_OPTS
107 uint16_t (*
qmat16)[2][64],
108 const uint16_t *quant_matrix,
109 int bias,
int qmin,
int qmax,
int intra)
120 else qscale2 =
qscale << 1;
127 for (
i = 0;
i < 64;
i++) {
128 const int j =
s->idsp.idct_permutation[
i];
129 int64_t den = (int64_t) qscale2 * quant_matrix[j];
139 for (
i = 0;
i < 64;
i++) {
140 const int j =
s->idsp.idct_permutation[
i];
141 int64_t den =
ff_aanscales[
i] * (int64_t) qscale2 * quant_matrix[j];
151 for (
i = 0;
i < 64;
i++) {
152 const int j =
s->idsp.idct_permutation[
i];
153 int64_t den = (int64_t) qscale2 * quant_matrix[j];
173 for (
i = intra;
i < 64;
i++) {
185 "Warning, QMAT_SHIFT is larger than %d, overflows possible\n",
192 if (
s->q_scale_type == 1 && 0) {
194 int bestdiff=INT_MAX;
202 if (
diff < bestdiff) {
211 s->qscale =
av_clip(
s->qscale,
s->avctx->qmin,
s->vbv_ignore_qmax ? 31 :
s->avctx->qmax);
224 for (
i = 0;
i < 64;
i++) {
236 int8_t *
const qscale_table =
s->current_picture.qscale_table;
239 for (
i = 0;
i <
s->mb_num;
i++) {
240 unsigned int lam =
s->lambda_table[
s->mb_index2xy[
i]];
242 qscale_table[
s->mb_index2xy[
i]] =
av_clip(qp,
s->avctx->qmin,
250 #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;
296 if (CONFIG_H263_ENCODER)
298 if (!
s->dct_quantize)
302 s->fast_dct_quantize =
s->dct_quantize;
303 if (
s->avctx->trellis)
342 "keyframe interval too large!, reducing it from %d to %d\n",
357 s->rtp_mode = !!
s->rtp_payload_size;
361 if (
s->intra_dc_precision < 0) {
362 s->intra_dc_precision += 8;
363 }
else if (
s->intra_dc_precision >= 8)
364 s->intra_dc_precision -= 8;
366 if (
s->intra_dc_precision < 0) {
368 "intra dc precision must be positive, note some applications use"
369 " 0 and some 8 as base meaning 8bit, the value must not be smaller than that\n");
382 if (
s->gop_size <= 1) {
436 "Warning min_rate > 0 but min_rate != max_rate isn't recommended!\n");
453 "impossible bitrate constraints, this will fail\n");
463 if (!
s->fixed_qscale &&
469 if (nbt <= INT_MAX) {
482 "Warning vbv_delay will be set to 0xFFFF (=VBR) as the "
483 "specified vbv buffer is too large for the given bitrate!\n");
495 "OBMC is only supported with simple mb decision\n");
504 if (
s->max_b_frames &&
511 if (
s->max_b_frames < 0) {
513 "max b frames must be 0 or positive for mpegvideo based encoders\n");
523 "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
581 "QP RD is no longer compatible with MJPEG or AMV\n");
585 if (
s->scenechange_threshold < 1000000000 &&
588 "closed gop with scene change detection are not supported yet, "
589 "set threshold to 1000000000\n");
597 "low delay forcing is only available for mpeg2, "
598 "set strict_std_compliance to 'unofficial' or lower in order to allow it\n");
601 if (
s->max_b_frames != 0) {
603 "B-frames cannot be used with low delay\n");
608 if (
s->q_scale_type == 1) {
611 "non linear quant only supports qmax <= 28 currently\n");
629 "multi threaded encoding not supported by codec\n");
635 "automatic thread number detection not supported by codec, "
642 "notice: b_frame_strategy only affects the first pass\n");
643 s->b_frame_strategy = 0;
657 s->inter_quant_bias = 0;
659 s->intra_quant_bias = 0;
674 "timebase %d/%d not supported by MPEG 4 standard, "
675 "the maximum admitted value for the timebase denominator "
686 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
691 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
694 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
708 if (!CONFIG_SPEEDHQ_ENCODER)
716 if (!CONFIG_H261_ENCODER)
720 "The specified picture size of %dx%d is not valid for the "
721 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
722 s->width,
s->height);
731 if (!CONFIG_H263_ENCODER)
734 s->width,
s->height) == 8) {
736 "The specified picture size of %dx%d is not valid for "
737 "the H.263 codec.\nValid sizes are 128x96, 176x144, "
738 "352x288, 704x576, and 1408x1152. "
739 "Try H.263+.\n",
s->width,
s->height);
751 s->modified_quant =
s->h263_aic;
753 s->unrestricted_mv =
s->obmc ||
s->loop_filter ||
s->umvplus;
763 s->unrestricted_mv = 1;
777 s->modified_quant = 1;
781 s->unrestricted_mv = 0;
786 s->unrestricted_mv = 1;
787 s->low_delay =
s->max_b_frames ? 0 : 1;
788 avctx->
delay =
s->low_delay ? 0 : (
s->max_b_frames + 1);
793 s->unrestricted_mv = 1;
794 s->msmpeg4_version = 2;
801 s->unrestricted_mv = 1;
802 s->msmpeg4_version = 3;
803 s->flipflop_rounding = 1;
810 s->unrestricted_mv = 1;
811 s->msmpeg4_version = 4;
812 s->flipflop_rounding = 1;
819 s->unrestricted_mv = 1;
820 s->msmpeg4_version = 5;
821 s->flipflop_rounding = 1;
833 s->progressive_frame =
849 if (
s->msmpeg4_version) {
866 if (
s->noise_reduction) {
873 if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) &&
s->modified_quant)
876 if (
s->slice_context_count > 1) {
880 s->h263_slice_structured = 1;
883 s->quant_precision = 5;
886 ff_set_cmp(&
s->mecc,
s->mecc.frame_skip_cmp,
s->frame_skip_cmp);
888 if (CONFIG_H261_ENCODER &&
s->out_format ==
FMT_H261) {
890 }
else if ((CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
893 }
else if (CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263) {
900 for (
i = 0;
i < 64;
i++) {
901 int j =
s->idsp.idct_permutation[
i];
914 s->chroma_intra_matrix[j] =
938 if (
s->b_frame_strategy == 2) {
939 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
941 if (!
s->tmp_frames[
i])
945 s->tmp_frames[
i]->width =
s->width >>
s->brd_scale;
946 s->tmp_frames[
i]->height =
s->height >>
s->brd_scale;
973 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
986 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
987 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
988 s->q_chroma_intra_matrix=
NULL;
989 s->q_chroma_intra_matrix16=
NULL;
1006 for (y = 0; y < 16; y++) {
1007 for (x = 0; x < 16; x++) {
1022 h =
s->height & ~15;
1024 for (y = 0; y <
h; y += 16) {
1025 for (x = 0; x <
w; x += 16) {
1032 acc += sae + 500 < sad;
1041 s->chroma_x_shift,
s->chroma_y_shift,
s->out_format,
1042 s->mb_stride,
s->mb_width,
s->mb_height,
s->b8_stride,
1043 &
s->linesize, &
s->uvlinesize);
1050 int i, display_picture_number = 0,
ret;
1051 int encoding_delay =
s->max_b_frames ?
s->max_b_frames
1052 : (
s->low_delay ? 0 : 1);
1053 int flush_offset = 1;
1058 display_picture_number =
s->input_picture_number++;
1062 int64_t last =
s->user_specified_pts;
1066 "Invalid pts (%"PRId64
") <= last (%"PRId64
")\n",
1071 if (!
s->low_delay && display_picture_number == 1)
1072 s->dts_delta =
pts - last;
1074 s->user_specified_pts =
pts;
1077 s->user_specified_pts =
1078 pts =
s->user_specified_pts + 1;
1080 "Warning: AVFrame.pts=? trying to guess (%"PRId64
")\n",
1083 pts = display_picture_number;
1087 if (!pic_arg->
buf[0] ||
1089 pic_arg->
linesize[1] !=
s->uvlinesize ||
1092 if ((
s->width & 15) || (
s->height & 15))
1100 pic_arg->
linesize[1],
s->linesize,
s->uvlinesize);
1106 pic = &
s->picture[
i];
1123 int h_chroma_shift, v_chroma_shift;
1128 for (
i = 0;
i < 3;
i++) {
1130 int dst_stride =
i ?
s->uvlinesize :
s->linesize;
1131 int h_shift =
i ? h_chroma_shift : 0;
1132 int v_shift =
i ? v_chroma_shift : 0;
1133 int w =
s->width >> h_shift;
1134 int h =
s->height >> v_shift;
1136 uint8_t *dst = pic->
f->
data[
i];
1140 && !
s->progressive_sequence
1141 &&
FFALIGN(
s->height, 32) -
s->height > 16)
1144 if (!
s->avctx->rc_buffer_size)
1147 if (src_stride == dst_stride)
1148 memcpy(dst,
src, src_stride *
h);
1151 uint8_t *dst2 = dst;
1153 memcpy(dst2,
src,
w);
1158 if ((
s->width & 15) || (
s->height & (vpad-1))) {
1159 s->mpvencdsp.draw_edges(dst, dst_stride,
1178 for (flush_offset = 0; flush_offset < encoding_delay + 1; flush_offset++)
1179 if (
s->input_picture[flush_offset])
1182 if (flush_offset <= 1)
1185 encoding_delay = encoding_delay - flush_offset + 1;
1190 s->input_picture[
i - flush_offset] =
s->input_picture[
i];
1192 s->input_picture[encoding_delay] = (
Picture*) pic;
1201 int64_t score64 = 0;
1203 for (plane = 0; plane < 3; plane++) {
1205 const int bw = plane ? 1 : 2;
1206 for (y = 0; y <
s->mb_height * bw; y++) {
1207 for (x = 0; x <
s->mb_width * bw; x++) {
1208 int off = p->
shared ? 0 : 16;
1209 uint8_t *dptr = p->
f->
data[plane] + 8 * (x + y *
stride) + off;
1210 uint8_t *rptr =
ref->f->data[plane] + 8 * (x + y *
stride);
1211 int v =
s->mecc.frame_skip_cmp[1](
s, dptr, rptr,
stride, 8);
1213 switch (
FFABS(
s->frame_skip_exp)) {
1214 case 0: score =
FFMAX(score, v);
break;
1215 case 1: score +=
FFABS(v);
break;
1216 case 2: score64 += v * (int64_t)v;
break;
1217 case 3: score64 +=
FFABS(v * (int64_t)v * v);
break;
1218 case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);
break;
1227 if (
s->frame_skip_exp < 0)
1228 score64 = pow(score64 / (
double)(
s->mb_width *
s->mb_height),
1229 -1.0/
s->frame_skip_exp);
1233 if (score64 < ((
s->frame_skip_factor * (int64_t)
s->lambda) >> 8))
1263 const int scale =
s->brd_scale;
1267 int64_t best_rd = INT64_MAX;
1268 int best_b_count = -1;
1283 b_lambda = p_lambda;
1287 for (
i = 0;
i <
s->max_b_frames + 2;
i++) {
1288 Picture pre_input, *pre_input_ptr =
i ?
s->input_picture[
i - 1] :
1289 s->next_picture_ptr;
1292 if (pre_input_ptr && (!
i ||
s->input_picture[
i - 1])) {
1293 pre_input = *pre_input_ptr;
1302 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[0],
1303 s->tmp_frames[
i]->linesize[0],
1307 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[1],
1308 s->tmp_frames[
i]->linesize[1],
1312 s->mpvencdsp.shrink[
scale](
s->tmp_frames[
i]->data[2],
1313 s->tmp_frames[
i]->linesize[2],
1320 for (j = 0; j <
s->max_b_frames + 1; j++) {
1324 if (!
s->input_picture[j])
1337 c->mb_decision =
s->avctx->mb_decision;
1338 c->me_cmp =
s->avctx->me_cmp;
1339 c->mb_cmp =
s->avctx->mb_cmp;
1340 c->me_sub_cmp =
s->avctx->me_sub_cmp;
1342 c->time_base =
s->avctx->time_base;
1343 c->max_b_frames =
s->max_b_frames;
1361 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1362 int is_p =
i % (j + 1) == j ||
i ==
s->max_b_frames;
1364 s->tmp_frames[
i + 1]->pict_type = is_p ?
1366 s->tmp_frames[
i + 1]->quality = is_p ? p_lambda : b_lambda;
1385 rd +=
c->error[0] +
c->error[1] +
c->error[2];
1403 return best_b_count;
1411 s->reordered_input_picture[
i - 1] =
s->reordered_input_picture[
i];
1415 if (!
s->reordered_input_picture[0] &&
s->input_picture[0]) {
1416 if (
s->frame_skip_threshold ||
s->frame_skip_factor) {
1417 if (
s->picture_in_gop_number <
s->gop_size &&
1418 s->next_picture_ptr &&
1430 !
s->next_picture_ptr ||
s->intra_only) {
1431 s->reordered_input_picture[0] =
s->input_picture[0];
1433 s->reordered_input_picture[0]->f->coded_picture_number =
1434 s->coded_picture_number++;
1439 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1440 int pict_num =
s->input_picture[0]->f->display_picture_number +
i;
1442 if (pict_num >=
s->rc_context.num_entries)
1444 if (!
s->input_picture[
i]) {
1449 s->input_picture[
i]->f->pict_type =
1450 s->rc_context.entry[pict_num].new_pict_type;
1454 if (
s->b_frame_strategy == 0) {
1455 b_frames =
s->max_b_frames;
1456 while (b_frames && !
s->input_picture[b_frames])
1458 }
else if (
s->b_frame_strategy == 1) {
1459 for (
i = 1;
i <
s->max_b_frames + 1;
i++) {
1460 if (
s->input_picture[
i] &&
1461 s->input_picture[
i]->b_frame_score == 0) {
1462 s->input_picture[
i]->b_frame_score =
1464 s->input_picture[
i ]->f->data[0],
1465 s->input_picture[
i - 1]->f->data[0],
1469 for (
i = 0;
i <
s->max_b_frames + 1;
i++) {
1470 if (!
s->input_picture[
i] ||
1471 s->input_picture[
i]->b_frame_score - 1 >
1472 s->mb_num /
s->b_sensitivity)
1476 b_frames =
FFMAX(0,
i - 1);
1479 for (
i = 0;
i < b_frames + 1;
i++) {
1480 s->input_picture[
i]->b_frame_score = 0;
1482 }
else if (
s->b_frame_strategy == 2) {
1490 for (
i = b_frames - 1;
i >= 0;
i--) {
1491 int type =
s->input_picture[
i]->f->pict_type;
1496 b_frames ==
s->max_b_frames) {
1498 "warning, too many B-frames in a row\n");
1501 if (
s->picture_in_gop_number + b_frames >=
s->gop_size) {
1503 s->gop_size >
s->picture_in_gop_number) {
1504 b_frames =
s->gop_size -
s->picture_in_gop_number - 1;
1516 s->reordered_input_picture[0] =
s->input_picture[b_frames];
1519 s->reordered_input_picture[0]->f->coded_picture_number =
1520 s->coded_picture_number++;
1521 for (
i = 0;
i < b_frames;
i++) {
1522 s->reordered_input_picture[
i + 1] =
s->input_picture[
i];
1523 s->reordered_input_picture[
i + 1]->f->pict_type =
1525 s->reordered_input_picture[
i + 1]->f->coded_picture_number =
1526 s->coded_picture_number++;
1533 if (
s->reordered_input_picture[0]) {
1534 s->reordered_input_picture[0]->reference =
1535 s->reordered_input_picture[0]->f->pict_type !=
1541 if (
s->reordered_input_picture[0]->shared ||
s->avctx->rc_buffer_size) {
1549 pic = &
s->picture[
i];
1551 pic->
reference =
s->reordered_input_picture[0]->reference;
1562 s->reordered_input_picture[0]->shared = 0;
1564 s->current_picture_ptr = pic;
1567 s->current_picture_ptr =
s->reordered_input_picture[0];
1568 for (
i = 0;
i < 4;
i++) {
1569 if (
s->new_picture.f->data[
i])
1575 s->current_picture_ptr)) < 0)
1578 s->picture_number =
s->new_picture.f->display_picture_number;
1585 if (
s->unrestricted_mv &&
1586 s->current_picture.reference &&
1589 int hshift =
desc->log2_chroma_w;
1590 int vshift =
desc->log2_chroma_h;
1591 s->mpvencdsp.draw_edges(
s->current_picture.f->data[0],
1592 s->current_picture.f->linesize[0],
1593 s->h_edge_pos,
s->v_edge_pos,
1596 s->mpvencdsp.draw_edges(
s->current_picture.f->data[1],
1597 s->current_picture.f->linesize[1],
1598 s->h_edge_pos >> hshift,
1599 s->v_edge_pos >> vshift,
1603 s->mpvencdsp.draw_edges(
s->current_picture.f->data[2],
1604 s->current_picture.f->linesize[2],
1605 s->h_edge_pos >> hshift,
1606 s->v_edge_pos >> vshift,
1614 s->last_pict_type =
s->pict_type;
1615 s->last_lambda_for [
s->pict_type] =
s->current_picture_ptr->f->quality;
1617 s->last_non_b_pict_type =
s->pict_type;
1624 for (intra = 0; intra < 2; intra++) {
1625 if (
s->dct_count[intra] > (1 << 16)) {
1626 for (
i = 0;
i < 64;
i++) {
1627 s->dct_error_sum[intra][
i] >>= 1;
1629 s->dct_count[intra] >>= 1;
1632 for (
i = 0;
i < 64;
i++) {
1633 s->dct_offset[intra][
i] = (
s->noise_reduction *
1634 s->dct_count[intra] +
1635 s->dct_error_sum[intra][
i] / 2) /
1636 (
s->dct_error_sum[intra][
i] + 1);
1647 s->last_picture_ptr !=
s->next_picture_ptr &&
1648 s->last_picture_ptr->f->buf[0]) {
1652 s->current_picture_ptr->f->pict_type =
s->pict_type;
1657 s->current_picture_ptr)) < 0)
1661 s->last_picture_ptr =
s->next_picture_ptr;
1662 s->next_picture_ptr =
s->current_picture_ptr;
1665 if (
s->last_picture_ptr) {
1667 if (
s->last_picture_ptr->f->buf[0] &&
1669 s->last_picture_ptr)) < 0)
1672 if (
s->next_picture_ptr) {
1674 if (
s->next_picture_ptr->f->buf[0] &&
1676 s->next_picture_ptr)) < 0)
1682 for (
i = 0;
i < 4;
i++) {
1684 s->current_picture.f->data[
i] +=
1685 s->current_picture.f->linesize[
i];
1687 s->current_picture.f->linesize[
i] *= 2;
1688 s->last_picture.f->linesize[
i] *= 2;
1689 s->next_picture.f->linesize[
i] *= 2;
1694 s->dct_unquantize_intra =
s->dct_unquantize_mpeg2_intra;
1695 s->dct_unquantize_inter =
s->dct_unquantize_mpeg2_inter;
1697 s->dct_unquantize_intra =
s->dct_unquantize_h263_intra;
1698 s->dct_unquantize_inter =
s->dct_unquantize_h263_inter;
1700 s->dct_unquantize_intra =
s->dct_unquantize_mpeg1_intra;
1701 s->dct_unquantize_inter =
s->dct_unquantize_mpeg1_inter;
1704 if (
s->dct_error_sum) {
1713 const AVFrame *pic_arg,
int *got_packet)
1716 int i, stuffing_count,
ret;
1717 int context_count =
s->slice_context_count;
1719 s->vbv_ignore_qmax = 0;
1721 s->picture_in_gop_number++;
1731 if (
s->new_picture.f->data[0]) {
1732 int growing_buffer = context_count == 1 && !
pkt->
data && !
s->data_partitioning;
1741 s->mb_width*
s->mb_height*12);
1742 s->prev_mb_info =
s->last_mb_info =
s->mb_info_size = 0;
1745 for (
i = 0;
i < context_count;
i++) {
1746 int start_y =
s->thread_context[
i]->start_mb_y;
1748 int h =
s->mb_height;
1749 uint8_t *start =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * start_y /
h);
1750 uint8_t *end =
pkt->
data + (size_t)(((int64_t)
pkt->
size) * end_y /
h);
1755 s->pict_type =
s->new_picture.f->pict_type;
1762 if (growing_buffer) {
1772 if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
s->out_format ==
FMT_MJPEG)
1782 s->lambda <
s->lmax) {
1783 s->next_lambda =
FFMAX(
s->lambda + min_step,
s->lambda *
1784 (
s->qscale + 1) /
s->qscale);
1785 if (
s->adaptive_quant) {
1787 for (
i = 0;
i <
s->mb_height *
s->mb_stride;
i++)
1788 s->lambda_table[
i] =
1789 FFMAX(
s->lambda_table[
i] + min_step,
1790 s->lambda_table[
i] * (
s->qscale + 1) /
1796 if (
s->flipflop_rounding ||
1799 s->no_rounding ^= 1;
1802 s->time_base =
s->last_time_base;
1803 s->last_non_b_time =
s->time -
s->pp_time;
1805 for (
i = 0;
i < context_count;
i++) {
1809 s->vbv_ignore_qmax = 1;
1820 for (
i = 0;
i < 4;
i++) {
1821 s->current_picture_ptr->encoding_error[
i] =
s->current_picture.encoding_error[
i];
1822 avctx->
error[
i] +=
s->current_picture_ptr->encoding_error[
i];
1825 s->current_picture_ptr->encoding_error,
1831 s->misc_bits +
s->i_tex_bits +
1837 s->stuffing_bits = 8*stuffing_count;
1838 if (stuffing_count) {
1844 switch (
s->codec_id) {
1847 while (stuffing_count--) {
1854 stuffing_count -= 4;
1855 while (stuffing_count--) {
1861 s->stuffing_bits = 0;
1879 int minbits =
s->frame_bits - 8 *
1880 (
s->vbv_delay_ptr -
s->pb.buf - 1);
1881 double bits =
s->rc_context.buffer_index + minbits - inbits;
1885 "Internal error, negative bits\n");
1897 s->vbv_delay_ptr[0] &= 0xF8;
1900 s->vbv_delay_ptr[2] &= 0x07;
1909 (uint8_t*)props, props_size);
1915 s->total_bits +=
s->frame_bits;
1917 pkt->
pts =
s->current_picture.f->pts;
1919 if (!
s->current_picture.f->coded_picture_number)
1926 if (
s->current_picture.f->key_frame)
1936 if (!
s->picture[
i].reference)
1948 int n,
int threshold)
1950 static const char tab[64] = {
1951 3, 2, 2, 1, 1, 1, 1, 1,
1952 1, 1, 1, 1, 1, 1, 1, 1,
1953 1, 1, 1, 1, 1, 1, 1, 1,
1954 0, 0, 0, 0, 0, 0, 0, 0,
1955 0, 0, 0, 0, 0, 0, 0, 0,
1956 0, 0, 0, 0, 0, 0, 0, 0,
1957 0, 0, 0, 0, 0, 0, 0, 0,
1958 0, 0, 0, 0, 0, 0, 0, 0
1963 int16_t *
block =
s->block[n];
1964 const int last_index =
s->block_last_index[n];
1967 if (threshold < 0) {
1969 threshold = -threshold;
1974 if (last_index <= skip_dc - 1)
1977 for (
i = 0;
i <= last_index;
i++) {
1978 const int j =
s->intra_scantable.permutated[
i];
1981 if (skip_dc &&
i == 0)
1985 }
else if (
level > 1) {
1991 if (score >= threshold)
1993 for (
i = skip_dc;
i <= last_index;
i++) {
1994 const int j =
s->intra_scantable.permutated[
i];
1998 s->block_last_index[n] = 0;
2000 s->block_last_index[n] = -1;
2007 const int maxlevel =
s->max_qcoeff;
2008 const int minlevel =
s->min_qcoeff;
2016 for (;
i <= last_index;
i++) {
2017 const int j =
s->intra_scantable.permutated[
i];
2020 if (
level > maxlevel) {
2023 }
else if (
level < minlevel) {
2033 "warning, clipping %d dct coefficients to %d..%d\n",
2041 for (y = 0; y < 8; y++) {
2042 for (x = 0; x < 8; x++) {
2048 for (y2 =
FFMAX(y - 1, 0); y2 <
FFMIN(8, y + 2); y2++) {
2049 for (x2=
FFMAX(x - 1, 0); x2 <
FFMIN(8, x + 2); x2++) {
2050 int v = ptr[x2 + y2 *
stride];
2062 int motion_x,
int motion_y,
2063 int mb_block_height,
2068 int16_t orig[12][64];
2069 const int mb_x =
s->mb_x;
2070 const int mb_y =
s->mb_y;
2074 int uv_dct_offset =
s->uvlinesize * 8;
2075 uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2076 ptrdiff_t wrap_y, wrap_c;
2078 for (
i = 0;
i < mb_block_count;
i++)
2079 skip_dct[
i] =
s->skipdct;
2081 if (
s->adaptive_quant) {
2082 const int last_qp =
s->qscale;
2083 const int mb_xy =
mb_x +
mb_y *
s->mb_stride;
2085 s->lambda =
s->lambda_table[mb_xy];
2089 s->qscale =
s->current_picture_ptr->qscale_table[mb_xy];
2090 s->dquant =
s->qscale - last_qp;
2111 wrap_y =
s->linesize;
2112 wrap_c =
s->uvlinesize;
2113 ptr_y =
s->new_picture.f->data[0] +
2115 ptr_cb =
s->new_picture.f->data[1] +
2116 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2117 ptr_cr =
s->new_picture.f->data[2] +
2118 (
mb_y * mb_block_height * wrap_c) +
mb_x * mb_block_width;
2121 uint8_t *ebuf =
s->sc.edge_emu_buffer + 38 * wrap_y;
2122 int cw = (
s->width +
s->chroma_x_shift) >>
s->chroma_x_shift;
2123 int ch = (
s->height +
s->chroma_y_shift) >>
s->chroma_y_shift;
2124 s->vdsp.emulated_edge_mc(ebuf, ptr_y,
2127 s->width,
s->height);
2129 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y, ptr_cb,
2131 mb_block_width, mb_block_height,
2132 mb_x * mb_block_width,
mb_y * mb_block_height,
2134 ptr_cb = ebuf + 16 * wrap_y;
2135 s->vdsp.emulated_edge_mc(ebuf + 16 * wrap_y + 16, ptr_cr,
2137 mb_block_width, mb_block_height,
2138 mb_x * mb_block_width,
mb_y * mb_block_height,
2140 ptr_cr = ebuf + 16 * wrap_y + 16;
2145 int progressive_score, interlaced_score;
2147 s->interlaced_dct = 0;
2148 progressive_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
NULL, wrap_y, 8) +
2149 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y * 8,
2150 NULL, wrap_y, 8) - 400;
2152 if (progressive_score > 0) {
2153 interlaced_score =
s->mecc.ildct_cmp[4](
s, ptr_y,
2154 NULL, wrap_y * 2, 8) +
2155 s->mecc.ildct_cmp[4](
s, ptr_y + wrap_y,
2156 NULL, wrap_y * 2, 8);
2157 if (progressive_score > interlaced_score) {
2158 s->interlaced_dct = 1;
2161 uv_dct_offset = wrap_c;
2170 s->pdsp.get_pixels(
s->block[0], ptr_y, wrap_y);
2171 s->pdsp.get_pixels(
s->block[1], ptr_y + 8, wrap_y);
2172 s->pdsp.get_pixels(
s->block[2], ptr_y +
dct_offset, wrap_y);
2173 s->pdsp.get_pixels(
s->block[3], ptr_y +
dct_offset + 8, wrap_y);
2179 s->pdsp.get_pixels(
s->block[4], ptr_cb, wrap_c);
2180 s->pdsp.get_pixels(
s->block[5], ptr_cr, wrap_c);
2181 if (!
s->chroma_y_shift &&
s->chroma_x_shift) {
2182 s->pdsp.get_pixels(
s->block[6], ptr_cb + uv_dct_offset, wrap_c);
2183 s->pdsp.get_pixels(
s->block[7], ptr_cr + uv_dct_offset, wrap_c);
2184 }
else if (!
s->chroma_y_shift && !
s->chroma_x_shift) {
2185 s->pdsp.get_pixels(
s->block[ 6], ptr_cb + 8, wrap_c);
2186 s->pdsp.get_pixels(
s->block[ 7], ptr_cr + 8, wrap_c);
2187 s->pdsp.get_pixels(
s->block[ 8], ptr_cb + uv_dct_offset, wrap_c);
2188 s->pdsp.get_pixels(
s->block[ 9], ptr_cr + uv_dct_offset, wrap_c);
2189 s->pdsp.get_pixels(
s->block[10], ptr_cb + uv_dct_offset + 8, wrap_c);
2190 s->pdsp.get_pixels(
s->block[11], ptr_cr + uv_dct_offset + 8, wrap_c);
2196 uint8_t *dest_y, *dest_cb, *dest_cr;
2198 dest_y =
s->dest[0];
2199 dest_cb =
s->dest[1];
2200 dest_cr =
s->dest[2];
2203 op_pix =
s->hdsp.put_pixels_tab;
2204 op_qpix =
s->qdsp.put_qpel_pixels_tab;
2206 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
2207 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
2212 s->last_picture.f->data,
2214 op_pix =
s->hdsp.avg_pixels_tab;
2215 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
2219 s->next_picture.f->data,
2224 int progressive_score, interlaced_score;
2226 s->interlaced_dct = 0;
2227 progressive_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y, wrap_y, 8) +
2228 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y * 8,
2233 progressive_score -= 400;
2235 if (progressive_score > 0) {
2236 interlaced_score =
s->mecc.ildct_cmp[0](
s, dest_y, ptr_y,
2238 s->mecc.ildct_cmp[0](
s, dest_y + wrap_y,
2242 if (progressive_score > interlaced_score) {
2243 s->interlaced_dct = 1;
2246 uv_dct_offset = wrap_c;
2254 s->pdsp.diff_pixels(
s->block[0], ptr_y, dest_y, wrap_y);
2255 s->pdsp.diff_pixels(
s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
2258 s->pdsp.diff_pixels(
s->block[3], ptr_y +
dct_offset + 8,
2265 s->pdsp.diff_pixels(
s->block[4], ptr_cb, dest_cb, wrap_c);
2266 s->pdsp.diff_pixels(
s->block[5], ptr_cr, dest_cr, wrap_c);
2267 if (!
s->chroma_y_shift) {
2268 s->pdsp.diff_pixels(
s->block[6], ptr_cb + uv_dct_offset,
2269 dest_cb + uv_dct_offset, wrap_c);
2270 s->pdsp.diff_pixels(
s->block[7], ptr_cr + uv_dct_offset,
2271 dest_cr + uv_dct_offset, wrap_c);
2275 if (
s->current_picture.mc_mb_var[
s->mb_stride *
mb_y +
mb_x] <
2276 2 *
s->qscale *
s->qscale) {
2278 if (
s->mecc.sad[1](
NULL, ptr_y, dest_y, wrap_y, 8) < 20 *
s->qscale)
2280 if (
s->mecc.sad[1](
NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 *
s->qscale)
2283 wrap_y, 8) < 20 *
s->qscale)
2286 wrap_y, 8) < 20 *
s->qscale)
2288 if (
s->mecc.sad[1](
NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 *
s->qscale)
2290 if (
s->mecc.sad[1](
NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 *
s->qscale)
2292 if (!
s->chroma_y_shift) {
2293 if (
s->mecc.sad[1](
NULL, ptr_cb + uv_dct_offset,
2294 dest_cb + uv_dct_offset,
2295 wrap_c, 8) < 20 *
s->qscale)
2297 if (
s->mecc.sad[1](
NULL, ptr_cr + uv_dct_offset,
2298 dest_cr + uv_dct_offset,
2299 wrap_c, 8) < 20 *
s->qscale)
2305 if (
s->quantizer_noise_shaping) {
2318 if (!
s->chroma_y_shift) {
2326 memcpy(orig[0],
s->block[0],
sizeof(int16_t) * 64 * mb_block_count);
2332 for (
i = 0;
i < mb_block_count;
i++) {
2335 s->block_last_index[
i] =
s->dct_quantize(
s,
s->block[
i],
i,
s->qscale, &
overflow);
2344 s->block_last_index[
i] = -1;
2346 if (
s->quantizer_noise_shaping) {
2347 for (
i = 0;
i < mb_block_count;
i++) {
2349 s->block_last_index[
i] =
2351 orig[
i],
i,
s->qscale);
2356 if (
s->luma_elim_threshold && !
s->mb_intra)
2357 for (
i = 0;
i < 4;
i++)
2359 if (
s->chroma_elim_threshold && !
s->mb_intra)
2360 for (
i = 4;
i < mb_block_count;
i++)
2364 for (
i = 0;
i < mb_block_count;
i++) {
2365 if (
s->block_last_index[
i] == -1)
2366 s->coded_score[
i] = INT_MAX / 256;
2372 s->block_last_index[4] =
2373 s->block_last_index[5] = 0;
2375 s->block[5][0] = (1024 +
s->c_dc_scale / 2) /
s->c_dc_scale;
2376 if (!
s->chroma_y_shift) {
2377 for (
i=6;
i<12;
i++) {
2378 s->block_last_index[
i] = 0;
2379 s->block[
i][0] =
s->block[4][0];
2386 for (
i = 0;
i < mb_block_count;
i++) {
2388 if (
s->block_last_index[
i] > 0) {
2389 for (j = 63; j > 0; j--) {
2390 if (
s->block[
i][
s->intra_scantable.permutated[j]])
2393 s->block_last_index[
i] = j;
2399 switch(
s->codec_id){
2402 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2406 if (CONFIG_MPEG4_ENCODER)
2416 if (CONFIG_WMV2_ENCODER)
2420 if (CONFIG_H261_ENCODER)
2428 if (CONFIG_H263_ENCODER)
2431 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
2438 if (CONFIG_SPEEDHQ_ENCODER)
2456 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2459 d->mb_skip_run=
s->mb_skip_run;
2461 d->last_dc[
i] =
s->last_dc[
i];
2464 d->mv_bits=
s->mv_bits;
2465 d->i_tex_bits=
s->i_tex_bits;
2466 d->p_tex_bits=
s->p_tex_bits;
2467 d->i_count=
s->i_count;
2468 d->f_count=
s->f_count;
2469 d->b_count=
s->b_count;
2470 d->skip_count=
s->skip_count;
2471 d->misc_bits=
s->misc_bits;
2475 d->qscale=
s->qscale;
2476 d->dquant=
s->dquant;
2478 d->esc3_level_length=
s->esc3_level_length;
2484 memcpy(
d->mv,
s->mv, 2*4*2*
sizeof(
int));
2485 memcpy(
d->last_mv,
s->last_mv, 2*2*2*
sizeof(
int));
2488 d->mb_skip_run=
s->mb_skip_run;
2490 d->last_dc[
i] =
s->last_dc[
i];
2493 d->mv_bits=
s->mv_bits;
2494 d->i_tex_bits=
s->i_tex_bits;
2495 d->p_tex_bits=
s->p_tex_bits;
2496 d->i_count=
s->i_count;
2497 d->f_count=
s->f_count;
2498 d->b_count=
s->b_count;
2499 d->skip_count=
s->skip_count;
2500 d->misc_bits=
s->misc_bits;
2502 d->mb_intra=
s->mb_intra;
2503 d->mb_skipped=
s->mb_skipped;
2504 d->mv_type=
s->mv_type;
2505 d->mv_dir=
s->mv_dir;
2507 if(
s->data_partitioning){
2509 d->tex_pb=
s->tex_pb;
2513 d->block_last_index[
i]=
s->block_last_index[
i];
2514 d->interlaced_dct=
s->interlaced_dct;
2515 d->qscale=
s->qscale;
2517 d->esc3_level_length=
s->esc3_level_length;
2522 int *dmin,
int *next_block,
int motion_x,
int motion_y)
2525 uint8_t *dest_backup[3];
2529 s->block=
s->blocks[*next_block];
2530 s->pb=
pb[*next_block];
2531 if(
s->data_partitioning){
2532 s->pb2 =
pb2 [*next_block];
2533 s->tex_pb=
tex_pb[*next_block];
2537 memcpy(dest_backup,
s->dest,
sizeof(
s->dest));
2538 s->dest[0] =
s->sc.rd_scratchpad;
2539 s->dest[1] =
s->sc.rd_scratchpad + 16*
s->linesize;
2540 s->dest[2] =
s->sc.rd_scratchpad + 16*
s->linesize + 8;
2547 if(
s->data_partitioning){
2555 score *=
s->lambda2;
2560 memcpy(
s->dest, dest_backup,
sizeof(
s->dest));
2578 else if(
w==8 &&
h==8)
2596 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
2597 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
2601 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) +
2602 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) +
2603 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);
2605 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) +
2606 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) +
2607 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);
2610 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)
2611 +
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)
2612 +
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);
2620 s->me.dia_size=
s->avctx->pre_dia_size;
2621 s->first_slice_line=1;
2622 for(
s->mb_y=
s->end_mb_y-1;
s->mb_y >=
s->start_mb_y;
s->mb_y--) {
2623 for(
s->mb_x=
s->mb_width-1;
s->mb_x >=0 ;
s->mb_x--) {
2626 s->first_slice_line=0;
2637 s->me.dia_size=
s->avctx->dia_size;
2638 s->first_slice_line=1;
2639 for(
s->mb_y=
s->start_mb_y;
s->mb_y <
s->end_mb_y;
s->mb_y++) {
2642 for(
s->mb_x=0;
s->mb_x <
s->mb_width;
s->mb_x++) {
2643 s->block_index[0]+=2;
2644 s->block_index[1]+=2;
2645 s->block_index[2]+=2;
2646 s->block_index[3]+=2;
2654 s->first_slice_line=0;
2667 uint8_t *pix =
s->new_picture.f->data[0] + (yy *
s->linesize) + xx;
2669 int sum =
s->mpvencdsp.pix_sum(pix,
s->linesize);
2671 varc = (
s->mpvencdsp.pix_norm1(pix,
s->linesize) -
2672 (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2674 s->current_picture.mb_var [
s->mb_stride *
mb_y +
mb_x] = varc;
2675 s->current_picture.mb_mean[
s->mb_stride *
mb_y +
mb_x] = (sum+128)>>8;
2676 s->me.mb_var_sum_temp += varc;
2684 if(
s->partitioned_frame){
2689 }
else if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
2692 }
else if (CONFIG_SPEEDHQ_ENCODER &&
s->out_format ==
FMT_SPEEDHQ) {
2704 uint8_t *ptr =
s->mb_info_ptr +
s->mb_info_size - 12;
2706 int mba =
s->mb_x +
s->mb_width * (
s->mb_y %
s->gob_index);
2707 int gobn =
s->mb_y /
s->gob_index;
2709 if (CONFIG_H263_ENCODER)
2711 bytestream_put_le32(&ptr,
offset);
2712 bytestream_put_byte(&ptr,
s->qscale);
2713 bytestream_put_byte(&ptr, gobn);
2714 bytestream_put_le16(&ptr, mba);
2715 bytestream_put_byte(&ptr, pred_x);
2716 bytestream_put_byte(&ptr, pred_y);
2718 bytestream_put_byte(&ptr, 0);
2719 bytestream_put_byte(&ptr, 0);
2727 s->mb_info_size += 12;
2728 s->prev_mb_info =
s->last_mb_info;
2740 if (!
s->mb_info_size)
2741 s->mb_info_size += 12;
2748 &&
s->slice_context_count == 1
2749 &&
s->pb.buf ==
s->avctx->internal->byte_buffer) {
2750 int lastgob_pos =
s->ptr_lastgob -
s->pb.buf;
2751 int vbv_pos =
s->vbv_delay_ptr -
s->pb.buf;
2753 uint8_t *new_buffer =
NULL;
2754 int new_buffer_size = 0;
2756 if ((
s->avctx->internal->byte_buffer_size + size_increase) >= INT_MAX/8) {
2764 s->avctx->internal->byte_buffer_size + size_increase);
2768 memcpy(new_buffer,
s->avctx->internal->byte_buffer,
s->avctx->internal->byte_buffer_size);
2769 av_free(
s->avctx->internal->byte_buffer);
2770 s->avctx->internal->byte_buffer = new_buffer;
2771 s->avctx->internal->byte_buffer_size = new_buffer_size;
2773 s->ptr_lastgob =
s->pb.buf + lastgob_pos;
2774 s->vbv_delay_ptr =
s->pb.buf + vbv_pos;
2784 int chr_h= 16>>
s->chroma_y_shift;
2811 s->last_dc[
i] = 128 <<
s->intra_dc_precision;
2813 s->current_picture.encoding_error[
i] = 0;
2816 s->last_dc[0] = 128*8/13;
2817 s->last_dc[1] = 128*8/14;
2818 s->last_dc[2] = 128*8/14;
2821 memset(
s->last_mv, 0,
sizeof(
s->last_mv));
2825 switch(
s->codec_id){
2829 if (CONFIG_H263_ENCODER)
2833 if(CONFIG_MPEG4_ENCODER &&
s->partitioned_frame)
2840 s->first_slice_line = 1;
2841 s->ptr_lastgob =
s->pb.buf;
2842 for (mb_y_order =
s->start_mb_y; mb_y_order < s->
end_mb_y; mb_y_order++) {
2846 if (first_in_slice && mb_y_order !=
s->start_mb_y)
2848 s->last_dc[0] =
s->last_dc[1] =
s->last_dc[2] = 1024 <<
s->intra_dc_precision;
2864 int size_increase =
s->avctx->internal->byte_buffer_size/4
2872 if(
s->data_partitioning){
2886 xy=
s->mb_y*
s->mb_stride +
s->mb_x;
2892 int current_packet_size, is_gob_start;
2895 - (
s->ptr_lastgob -
s->pb.buf);
2897 is_gob_start =
s->rtp_payload_size &&
2898 current_packet_size >=
s->rtp_payload_size &&
2901 if(
s->start_mb_y ==
mb_y &&
mb_y > 0 &&
mb_x==0) is_gob_start=1;
2903 switch(
s->codec_id){
2906 if(!
s->h263_slice_structured)
2907 if(
s->mb_x ||
s->mb_y%
s->gob_index) is_gob_start=0;
2910 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2912 if(
s->mb_skip_run) is_gob_start=0;
2915 if(
s->mb_x==0 &&
s->mb_y!=0) is_gob_start=1;
2931 if (
s->error_rate &&
s->resync_mb_x +
s->resync_mb_y > 0) {
2933 int d = 100 /
s->error_rate;
2935 current_packet_size=0;
2936 s->pb.buf_ptr=
s->ptr_lastgob;
2943 switch(
s->codec_id){
2945 if (CONFIG_MPEG4_ENCODER) {
2952 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2959 if (CONFIG_H263_ENCODER)
2966 s->misc_bits+=
bits -
s->last_bits;
2970 s->ptr_lastgob += current_packet_size;
2971 s->first_slice_line=1;
2972 s->resync_mb_x=
mb_x;
2973 s->resync_mb_y=
mb_y;
2977 if( (
s->resync_mb_x ==
s->mb_x)
2978 &&
s->resync_mb_y+1 ==
s->mb_y){
2979 s->first_slice_line=0;
2989 int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2995 if(
s->data_partitioning){
2996 backup_s.pb2=
s->pb2;
2997 backup_s.tex_pb=
s->tex_pb;
3004 s->mv[0][0][0] =
s->p_mv_table[xy][0];
3005 s->mv[0][0][1] =
s->p_mv_table[xy][1];
3007 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3014 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3015 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3016 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3019 &dmin, &next_block, 0, 0);
3028 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3035 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3036 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3039 &dmin, &next_block, 0, 0);
3045 s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3046 s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3048 &dmin, &next_block,
s->mv[0][0][0],
s->mv[0][0][1]);
3054 s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3055 s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3057 &dmin, &next_block,
s->mv[1][0][0],
s->mv[1][0][1]);
3063 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3064 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3065 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3066 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3068 &dmin, &next_block, 0, 0);
3075 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3076 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3077 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3080 &dmin, &next_block, 0, 0);
3087 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3088 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3089 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3092 &dmin, &next_block, 0, 0);
3098 for(dir=0; dir<2; dir++){
3100 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3101 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3102 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3106 &dmin, &next_block, 0, 0);
3115 &dmin, &next_block, 0, 0);
3116 if(
s->h263_pred ||
s->h263_aic){
3118 s->mbintra_table[
mb_x +
mb_y*
s->mb_stride]=1;
3126 const int last_qp= backup_s.qscale;
3130 static const int dquant_tab[4]={-1,1,-2,2};
3131 int storecoefs =
s->mb_intra &&
s->dc_val[0];
3139 s->mv[0][0][0] = best_s.
mv[0][0][0];
3140 s->mv[0][0][1] = best_s.
mv[0][0][1];
3141 s->mv[1][0][0] = best_s.
mv[1][0][0];
3142 s->mv[1][0][1] = best_s.
mv[1][0][1];
3145 for(; qpi<4; qpi++){
3146 int dquant= dquant_tab[qpi];
3148 if(qp < s->
avctx->
qmin || qp >
s->avctx->qmax)
3153 dc[
i]=
s->dc_val[0][
s->block_index[
i] ];
3154 memcpy(ac[
i],
s->ac_val[0][
s->block_index[
i]],
sizeof(int16_t)*16);
3159 &dmin, &next_block,
s->mv[mvdir][0][0],
s->mv[mvdir][0][1]);
3163 s->dc_val[0][
s->block_index[
i] ]=
dc[
i];
3164 memcpy(
s->ac_val[0][
s->block_index[
i]], ac[
i],
sizeof(int16_t)*16);
3172 int mx=
s->b_direct_mv_table[xy][0];
3173 int my=
s->b_direct_mv_table[xy][1];
3175 backup_s.dquant = 0;
3180 &dmin, &next_block, mx, my);
3183 backup_s.dquant = 0;
3188 &dmin, &next_block, 0, 0);
3193 coded |=
s->block_last_index[
i];
3196 memcpy(
s->mv, best_s.
mv,
sizeof(
s->mv));
3218 &dmin, &next_block, mx, my);
3223 s->current_picture.qscale_table[xy] = best_s.
qscale;
3229 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count);
3232 if(
s->data_partitioning){
3235 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count);
3236 s->pb2= backup_s.pb2;
3240 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count);
3241 s->tex_pb= backup_s.tex_pb;
3245 if (CONFIG_H263_ENCODER &&
3250 s->hdsp.put_pixels_tab[0][0](
s->dest[0],
s->sc.rd_scratchpad ,
s->linesize ,16);
3251 s->hdsp.put_pixels_tab[1][0](
s->dest[1],
s->sc.rd_scratchpad + 16*
s->linesize ,
s->uvlinesize, 8);
3252 s->hdsp.put_pixels_tab[1][0](
s->dest[2],
s->sc.rd_scratchpad + 16*
s->linesize + 8,
s->uvlinesize, 8);
3258 int motion_x = 0, motion_y = 0;
3266 motion_x=
s->mv[0][0][0] = 0;
3267 motion_y=
s->mv[0][0][1] = 0;
3272 motion_x=
s->mv[0][0][0] =
s->p_mv_table[xy][0];
3273 motion_y=
s->mv[0][0][1] =
s->p_mv_table[xy][1];
3280 j=
s->field_select[0][
i] =
s->p_field_select_table[
i][xy];
3281 s->mv[0][
i][0] =
s->p_field_mv_table[
i][j][xy][0];
3282 s->mv[0][
i][1] =
s->p_field_mv_table[
i][j][xy][1];
3290 s->mv[0][
i][0] =
s->current_picture.motion_val[0][
s->block_index[
i]][0];
3291 s->mv[0][
i][1] =
s->current_picture.motion_val[0][
s->block_index[
i]][1];
3295 if (CONFIG_MPEG4_ENCODER) {
3298 motion_x=
s->b_direct_mv_table[xy][0];
3299 motion_y=
s->b_direct_mv_table[xy][1];
3304 if (CONFIG_MPEG4_ENCODER) {
3313 s->mv[0][0][0] =
s->b_bidir_forw_mv_table[xy][0];
3314 s->mv[0][0][1] =
s->b_bidir_forw_mv_table[xy][1];
3315 s->mv[1][0][0] =
s->b_bidir_back_mv_table[xy][0];
3316 s->mv[1][0][1] =
s->b_bidir_back_mv_table[xy][1];
3321 motion_x=
s->mv[1][0][0] =
s->b_back_mv_table[xy][0];
3322 motion_y=
s->mv[1][0][1] =
s->b_back_mv_table[xy][1];
3327 motion_x=
s->mv[0][0][0] =
s->b_forw_mv_table[xy][0];
3328 motion_y=
s->mv[0][0][1] =
s->b_forw_mv_table[xy][1];
3335 j=
s->field_select[0][
i] =
s->b_field_select_table[0][
i][xy];
3336 s->mv[0][
i][0] =
s->b_field_mv_table[0][
i][j][xy][0];
3337 s->mv[0][
i][1] =
s->b_field_mv_table[0][
i][j][xy][1];
3345 j=
s->field_select[1][
i] =
s->b_field_select_table[1][
i][xy];
3346 s->mv[1][
i][0] =
s->b_field_mv_table[1][
i][j][xy][0];
3347 s->mv[1][
i][1] =
s->b_field_mv_table[1][
i][j][xy][1];
3354 for(dir=0; dir<2; dir++){
3356 j=
s->field_select[dir][
i] =
s->b_field_select_table[dir][
i][xy];
3357 s->mv[dir][
i][0] =
s->b_field_mv_table[dir][
i][j][xy][0];
3358 s->mv[dir][
i][1] =
s->b_field_mv_table[dir][
i][j][xy][1];
3369 s->last_mv_dir =
s->mv_dir;
3371 if (CONFIG_H263_ENCODER &&
3380 s->p_mv_table[xy][0]=0;
3381 s->p_mv_table[xy][1]=0;
3388 if(
s->mb_x*16 + 16 >
s->width )
w=
s->width -
s->mb_x*16;
3389 if(
s->mb_y*16 + 16 >
s->height)
h=
s->height-
s->mb_y*16;
3391 s->current_picture.encoding_error[0] +=
sse(
3392 s,
s->new_picture.f->data[0] +
s->mb_x*16 +
s->mb_y*
s->linesize*16,
3393 s->dest[0],
w,
h,
s->linesize);
3394 s->current_picture.encoding_error[1] +=
sse(
3395 s,
s->new_picture.f->data[1] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3396 s->dest[1],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3397 s->current_picture.encoding_error[2] +=
sse(
3398 s,
s->new_picture.f->data[2] +
s->mb_x*8 +
s->mb_y*
s->uvlinesize*chr_h,
3399 s->dest[2],
w>>1,
h>>
s->chroma_y_shift,
s->uvlinesize);
3402 if(CONFIG_H263_ENCODER &&
s->out_format ==
FMT_H263)
3405 ff_dlog(
s->avctx,
"MB %d %d bits\n",
3419 #define MERGE(field) dst->field += src->field; src->field=0
3446 for(
i=0;
i<64;
i++){
3459 if (
s->next_lambda){
3460 s->current_picture_ptr->f->quality =
3461 s->current_picture.f->quality =
s->next_lambda;
3462 if(!dry_run)
s->next_lambda= 0;
3463 }
else if (!
s->fixed_qscale) {
3465 s->current_picture_ptr->f->quality =
3466 s->current_picture.f->quality =
quality;
3467 if (
s->current_picture.f->quality < 0)
3471 if(
s->adaptive_quant){
3472 switch(
s->codec_id){
3474 if (CONFIG_MPEG4_ENCODER)
3480 if (CONFIG_H263_ENCODER)
3487 s->lambda=
s->lambda_table[0];
3490 s->lambda =
s->current_picture.f->quality;
3498 s->time =
s->current_picture_ptr->f->pts *
s->avctx->time_base.num;
3501 s->pb_time=
s->pp_time - (
s->last_non_b_time -
s->time);
3504 s->pp_time=
s->time -
s->last_non_b_time;
3505 s->last_non_b_time=
s->time;
3514 int context_count =
s->slice_context_count;
3519 s->me.mb_var_sum_temp =
3520 s->me.mc_mb_var_sum_temp = 0;
3524 if (
s->out_format ==
FMT_MPEG1 || (
s->h263_pred && !
s->msmpeg4_version))
3529 s->me.scene_change_score=0;
3534 if(
s->msmpeg4_version >= 3)
s->no_rounding=1;
3535 else s->no_rounding=0;
3538 s->no_rounding ^= 1;
3547 s->lambda=
s->last_lambda_for[
s->pict_type];
3549 s->lambda=
s->last_lambda_for[
s->last_non_b_pict_type];
3554 if(
s->q_chroma_intra_matrix !=
s->q_intra_matrix )
av_freep(&
s->q_chroma_intra_matrix);
3555 if(
s->q_chroma_intra_matrix16 !=
s->q_intra_matrix16)
av_freep(&
s->q_chroma_intra_matrix16);
3556 s->q_chroma_intra_matrix =
s->q_intra_matrix;
3557 s->q_chroma_intra_matrix16 =
s->q_intra_matrix16;
3561 for(
i=1;
i<context_count;
i++){
3572 s->lambda = (
s->lambda *
s->me_penalty_compensation + 128) >> 8;
3573 s->lambda2 = (
s->lambda2 * (int64_t)
s->me_penalty_compensation + 128) >> 8;
3584 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3587 if(!
s->fixed_qscale){
3589 s->avctx->execute(
s->avctx,
mb_var_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3592 for(
i=1;
i<context_count;
i++){
3595 s->current_picture.mc_mb_var_sum=
s->current_picture_ptr->mc_mb_var_sum=
s->me.mc_mb_var_sum_temp;
3596 s->current_picture. mb_var_sum=
s->current_picture_ptr-> mb_var_sum=
s->me. mb_var_sum_temp;
3599 if (
s->me.scene_change_score >
s->scenechange_threshold &&
3602 for(
i=0;
i<
s->mb_stride*
s->mb_height;
i++)
3604 if(
s->msmpeg4_version >= 3)
3606 ff_dlog(
s,
"Scene change detected, encoding as I Frame %"PRId64
" %"PRId64
"\n",
3607 s->current_picture.mb_var_sum,
s->current_picture.mc_mb_var_sum);
3650 for(dir=0; dir<2; dir++){
3656 s->b_field_mv_table[dir][
i][j], dir ?
s->b_code :
s->f_code,
type, 1);
3667 if (
s->qscale < 3 &&
s->max_qcoeff <= 128 &&
3676 if (
s->avctx->intra_matrix) {
3678 luma_matrix =
s->avctx->intra_matrix;
3680 if (
s->avctx->chroma_intra_matrix)
3681 chroma_matrix =
s->avctx->chroma_intra_matrix;
3685 int j =
s->idsp.idct_permutation[
i];
3687 s->chroma_intra_matrix[j] =
av_clip_uint8((chroma_matrix[
i] *
s->qscale) >> 3);
3690 s->y_dc_scale_table=
3692 s->chroma_intra_matrix[0] =
3695 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3697 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3701 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};
3702 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};
3703 for (
int i = 1;
i < 64;
i++) {
3709 s->y_dc_scale_table = y;
3710 s->c_dc_scale_table =
c;
3711 s->intra_matrix[0] = 13;
3712 s->chroma_intra_matrix[0] = 14;
3714 s->intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3716 s->chroma_intra_matrix,
s->intra_quant_bias, 8, 8, 1);
3720 s->y_dc_scale_table=
3725 s->current_picture_ptr->f->key_frame =
3727 s->current_picture_ptr->f->pict_type =
3728 s->current_picture.f->pict_type =
s->pict_type;
3730 if (
s->current_picture.f->key_frame)
3731 s->picture_in_gop_number=0;
3733 s->mb_x =
s->mb_y = 0;
3735 switch(
s->out_format) {
3736 #if CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER
3741 s->pred,
s->intra_matrix,
s->chroma_intra_matrix);
3745 if (CONFIG_SPEEDHQ_ENCODER)
3749 if (CONFIG_H261_ENCODER)
3757 else if (CONFIG_MPEG4_ENCODER &&
s->h263_pred) {
3770 else if (CONFIG_H263_ENCODER)
3774 if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3781 s->header_bits=
bits -
s->last_bits;
3783 for(
i=1;
i<context_count;
i++){
3786 s->avctx->execute(
s->avctx,
encode_thread, &
s->thread_context[0],
NULL, context_count,
sizeof(
void*));
3787 for(
i=1;
i<context_count;
i++){
3788 if (
s->pb.buf_end ==
s->thread_context[
i]->pb.buf)
3797 const int intra=
s->mb_intra;
3800 s->dct_count[intra]++;
3802 for(
i=0;
i<64;
i++){
3807 s->dct_error_sum[intra][
i] +=
level;
3808 level -=
s->dct_offset[intra][
i];
3811 s->dct_error_sum[intra][
i] -=
level;
3812 level +=
s->dct_offset[intra][
i];
3821 int16_t *
block,
int n,
3824 const uint16_t *matrix;
3825 const uint8_t *scantable;
3826 const uint8_t *perm_scantable;
3828 unsigned int threshold1, threshold2;
3840 int coeff_count[64];
3841 int qmul, qadd, start_i, last_non_zero,
i,
dc;
3842 const int esc_length=
s->ac_esc_length;
3844 uint8_t * last_length;
3850 if(
s->dct_error_sum)
3856 else mpeg2_qscale =
qscale << 1;
3860 scantable=
s->intra_scantable.scantable;
3861 perm_scantable=
s->intra_scantable.permutated;
3878 qmat = n < 4 ?
s->q_intra_matrix[
qscale] :
s->q_chroma_intra_matrix[
qscale];
3879 matrix = n < 4 ?
s->intra_matrix :
s->chroma_intra_matrix;
3883 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
3884 length =
s->intra_chroma_ac_vlc_length;
3885 last_length=
s->intra_chroma_ac_vlc_last_length;
3887 length =
s->intra_ac_vlc_length;
3888 last_length=
s->intra_ac_vlc_last_length;
3891 scantable=
s->inter_scantable.scantable;
3892 perm_scantable=
s->inter_scantable.permutated;
3895 qmat =
s->q_inter_matrix[
qscale];
3896 matrix =
s->inter_matrix;
3897 length =
s->inter_ac_vlc_length;
3898 last_length=
s->inter_ac_vlc_last_length;
3903 threshold2= (threshold1<<1);
3905 for(
i=63;
i>=start_i;
i--) {
3906 const int j = scantable[
i];
3909 if(((
unsigned)(
level+threshold1))>threshold2){
3915 for(
i=start_i;
i<=last_non_zero;
i++) {
3916 const int j = scantable[
i];
3921 if(((
unsigned)(
level+threshold1))>threshold2){
3944 if(last_non_zero < start_i){
3945 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
3946 return last_non_zero;
3949 score_tab[start_i]= 0;
3950 survivor[0]= start_i;
3953 for(
i=start_i;
i<=last_non_zero;
i++){
3954 int level_index, j, zero_distortion;
3956 int best_score=256*256*256*120;
3960 zero_distortion= dct_coeff*dct_coeff;
3962 for(level_index=0; level_index < coeff_count[
i]; level_index++){
3971 unquant_coeff= alevel*qmul + qadd;
3973 j =
s->idsp.idct_permutation[scantable[
i]];
3974 unquant_coeff = alevel * matrix[j] * 8;
3976 j =
s->idsp.idct_permutation[scantable[
i]];
3978 unquant_coeff = (
int)( alevel * mpeg2_qscale * matrix[j]) >> 4;
3979 unquant_coeff = (unquant_coeff - 1) | 1;
3981 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int) matrix[j])) >> 5;
3982 unquant_coeff = (unquant_coeff - 1) | 1;
3987 distortion= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff) - zero_distortion;
3989 if((
level&(~127)) == 0){
3990 for(j=survivor_count-1; j>=0; j--){
3991 int run=
i - survivor[j];
3993 score += score_tab[
i-
run];
3995 if(score < best_score){
3998 level_tab[
i+1]=
level-64;
4003 for(j=survivor_count-1; j>=0; j--){
4004 int run=
i - survivor[j];
4006 score += score_tab[
i-
run];
4007 if(score < last_score){
4010 last_level=
level-64;
4016 distortion += esc_length*
lambda;
4017 for(j=survivor_count-1; j>=0; j--){
4018 int run=
i - survivor[j];
4019 int score= distortion + score_tab[
i-
run];
4021 if(score < best_score){
4024 level_tab[
i+1]=
level-64;
4029 for(j=survivor_count-1; j>=0; j--){
4030 int run=
i - survivor[j];
4031 int score= distortion + score_tab[
i-
run];
4032 if(score < last_score){
4035 last_level=
level-64;
4043 score_tab[
i+1]= best_score;
4046 if(last_non_zero <= 27){
4047 for(; survivor_count; survivor_count--){
4048 if(score_tab[ survivor[survivor_count-1] ] <= best_score)
4052 for(; survivor_count; survivor_count--){
4053 if(score_tab[ survivor[survivor_count-1] ] <= best_score +
lambda)
4058 survivor[ survivor_count++ ]=
i+1;
4062 last_score= 256*256*256*120;
4063 for(
i= survivor[0];
i<=last_non_zero + 1;
i++){
4064 int score= score_tab[
i];
4068 if(score < last_score){
4071 last_level= level_tab[
i];
4072 last_run= run_tab[
i];
4077 s->coded_score[n] = last_score;
4080 last_non_zero= last_i - 1;
4081 memset(
block + start_i, 0, (64-start_i)*
sizeof(int16_t));
4083 if(last_non_zero < start_i)
4084 return last_non_zero;
4086 if(last_non_zero == 0 && start_i == 0){
4088 int best_score=
dc *
dc;
4090 for(
i=0;
i<coeff_count[0];
i++){
4093 int unquant_coeff, score, distortion;
4096 unquant_coeff= (alevel*qmul + qadd)>>3;
4098 unquant_coeff = ((( alevel << 1) + 1) * mpeg2_qscale * ((
int) matrix[0])) >> 5;
4099 unquant_coeff = (unquant_coeff - 1) | 1;
4101 unquant_coeff = (unquant_coeff + 4) >> 3;
4102 unquant_coeff<<= 3 + 3;
4104 distortion= (unquant_coeff -
dc) * (unquant_coeff -
dc);
4107 else score= distortion + esc_length*
lambda;
4109 if(score < best_score){
4111 best_level=
level - 64;
4114 block[0]= best_level;
4115 s->coded_score[n] = best_score -
dc*
dc;
4116 if(best_level == 0)
return -1;
4117 else return last_non_zero;
4123 block[ perm_scantable[last_non_zero] ]= last_level;
4126 for(;
i>start_i;
i -= run_tab[
i] + 1){
4127 block[ perm_scantable[
i-1] ]= level_tab[
i];
4130 return last_non_zero;
4145 if(
i==0)
s*= sqrt(0.5);
4146 if(j==0)
s*= sqrt(0.5);
4159 const uint8_t *scantable;
4160 const uint8_t *perm_scantable;
4166 int qmul, qadd, start_i, last_non_zero,
i,
dc;
4168 uint8_t * last_length;
4170 int rle_index,
run, q = 1, sum;
4172 if(
basis[0][0] == 0)
4178 scantable=
s->intra_scantable.scantable;
4179 perm_scantable=
s->intra_scantable.permutated;
4197 if (n > 3 &&
s->intra_chroma_ac_vlc_length) {
4198 length =
s->intra_chroma_ac_vlc_length;
4199 last_length=
s->intra_chroma_ac_vlc_last_length;
4201 length =
s->intra_ac_vlc_length;
4202 last_length=
s->intra_ac_vlc_last_length;
4205 scantable=
s->inter_scantable.scantable;
4206 perm_scantable=
s->inter_scantable.permutated;
4209 length =
s->inter_ac_vlc_length;
4210 last_length=
s->inter_ac_vlc_last_length;
4212 last_non_zero =
s->block_last_index[n];
4215 for(
i=0;
i<64;
i++){