25 #include "config_components.h"
47 #define H264_CHROMA_MC(OPNAME, OP)\
48 static void OPNAME ## h264_chroma_mc1(uint8_t *dst , const uint8_t *src , ptrdiff_t stride, int h, int x, int y)\
50 const int A = (8-x) * (8-y);\
51 const int B = ( x) * (8-y);\
52 const int C = (8-x) * ( y);\
53 const int D = ( x) * ( y);\
55 av_assert2(x < 8 && y < 8 && x >= 0 && y >= 0);\
58 for (int i = 0; i < h; ++i) {\
59 OP(dst[0], (A*src[0] + B*src[1] + C*src[stride+0] + D*src[stride+1]));\
65 const int step = C ? stride : 1;\
66 for (int i = 0; i < h; ++i) {\
67 OP(dst[0], (A*src[0] + E*src[step+0]));\
72 for (int i = 0; i < h; ++i) {\
73 OP(dst[0], (A*src[0]));\
80 #define op_avg(a, b) a = (((a)+(((b) + 32)>>6)+1)>>1)
81 #define op_put(a, b) a = (((b) + 32)>>6)
93 s->width = avctx->coded_width;
94 s->height = avctx->coded_height;
95 s->codec_id = avctx->codec->id;
96 s->workaround_bugs = avctx->workaround_bugs;
110 s->h264chroma.avg_h264_chroma_pixels_tab[3] = avg_h264_chroma_mc1;
111 s->h264chroma.put_h264_chroma_pixels_tab[3] = put_h264_chroma_mc1;
119 if (!
s->picture_pool)
137 if (
s->height != s1->
height ||
s->width != s1->
width ||
s->context_reinit) {
185 if (!
s->context_initialized)
194 if ((
s->width ||
s->height) &&
208 memset(
s->thread_context, 0,
sizeof(
s->thread_context));
209 s->thread_context[0] =
s;
211 if (
s->width &&
s->height) {
216 s->context_reinit = 0;
221 s->context_reinit = 1;
262 av_assert1(
s->mb_width ==
s->buffer_pools.alloc_mb_width);
263 av_assert1(
s->mb_height ==
s->buffer_pools.alloc_mb_height ||
264 FFALIGN(
s->mb_height, 2) ==
s->buffer_pools.alloc_mb_height);
265 av_assert1(
s->mb_stride ==
s->buffer_pools.alloc_mb_stride);
267 &
s->buffer_pools,
s->mb_height);
294 int h_chroma_shift, v_chroma_shift;
296 for (
int i = 0;
i <
frame->height;
i++)
315 av_assert1(!
s->last_pic.ptr ||
s->last_pic.ptr->f->buf[0]);
316 av_assert1(!
s->next_pic.ptr ||
s->next_pic.ptr->f->buf[0]);
320 "allocating dummy last picture for B frame\n");
324 "warning: first frame is no keyframe\n");
344 s->last_pic.ptr->f->buf[0]));
372 (!
s->progressive_frame && !
s->progressive_sequence);
373 s->cur_pic.ptr->field_picture =
s->picture_structure !=
PICT_FRAME;
375 s->cur_pic.ptr->f->pict_type =
s->pict_type;
386 ff_dlog(
s->avctx,
"L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
387 (
void*)
s->last_pic.ptr, (
void*)
s->next_pic.ptr, (
void*)
s->cur_pic.ptr,
388 s->last_pic.ptr ?
s->last_pic.ptr->f->data[0] :
NULL,
389 s->next_pic.ptr ?
s->next_pic.ptr->f->data[0] :
NULL,
390 s->cur_pic.ptr ?
s->cur_pic.ptr->f->data[0] :
NULL,
391 s->pict_type,
s->droppable);
408 if (
s->cur_pic.reference)
415 p->qscale_table,
p->motion_val,
416 p->mb_width,
p->mb_height,
p->mb_stride,
s->quarter_sample);
424 unsigned int nb_mb =
p->mb_height *
p->mb_width;
433 for (
unsigned y = 0; y <
p->mb_height; y++)
434 for (
unsigned x = 0; x <
p->mb_width; x++) {
435 const unsigned int block_idx = y *
p->mb_width + x;
436 const unsigned int mb_xy = y *
p->mb_stride + x;
444 b->delta_qp =
p->qscale_table[mb_xy] *
mult;
453 s->last_pic.ptr ?
s->last_pic.ptr->f :
NULL,
454 y,
h,
s->picture_structure,
455 s->first_field,
s->low_delay);
466 s->mb_x =
s->mb_y = 0;
472 uint8_t *dest,
const uint8_t *
src,
473 int field_based,
int field_select,
474 int src_x,
int src_y,
476 int h_edge_pos,
int v_edge_pos,
478 int motion_x,
int motion_y)
480 const int lowres =
s->avctx->lowres;
481 const int op_index =
lowres;
482 const int s_mask = (2 <<
lowres) - 1;
488 if (
s->quarter_sample) {
493 sx = motion_x & s_mask;
494 sy = motion_y & s_mask;
495 src_x += motion_x >>
lowres + 1;
496 src_y += motion_y >>
lowres + 1;
500 if ((
unsigned)src_x >
FFMAX( h_edge_pos - (!!sx) -
w, 0) ||
501 (
unsigned)src_y >
FFMAX((v_edge_pos >> field_based) - (!!sy) -
h, 0)) {
502 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer,
src,
503 s->linesize,
s->linesize,
504 w + 1, (
h + 1) << field_based,
505 src_x, src_y * (1 << field_based),
506 h_edge_pos, v_edge_pos);
507 src =
s->sc.edge_emu_buffer;
515 pix_op[op_index](dest,
src,
stride,
h, sx, sy);
527 uint8_t *
const *ref_picture,
529 int motion_x,
int motion_y,
532 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
533 int mx,
my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
534 ptrdiff_t uvlinesize, linesize;
535 const int lowres =
s->avctx->lowres;
536 const int op_index =
lowres - 1 +
s->chroma_x_shift;
537 const int block_s = 8 >>
lowres;
538 const int s_mask = (2 <<
lowres) - 1;
539 const int h_edge_pos =
s->h_edge_pos >>
lowres;
540 const int v_edge_pos =
s->v_edge_pos >>
lowres;
541 int hc =
s->chroma_y_shift ? (
h+1-bottom_field)>>1 :
h;
545 linesize =
s->cur_pic.linesize[0] << field_based;
546 uvlinesize =
s->cur_pic.linesize[1] << field_based;
549 if (
s->quarter_sample) {
555 motion_y += (bottom_field - field_select)*((1 <<
lowres)-1);
558 sx = motion_x & s_mask;
559 sy = motion_y & s_mask;
560 src_x =
s->mb_x * 2 * block_s + (motion_x >>
lowres + 1);
561 src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >>
lowres + 1);
564 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
565 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
566 uvsrc_x = src_x >> 1;
567 uvsrc_y = src_y >> 1;
572 uvsx = (2 *
mx) & s_mask;
573 uvsy = (2 *
my) & s_mask;
574 uvsrc_x =
s->mb_x * block_s + (
mx >>
lowres);
575 uvsrc_y = mb_y * block_s + (
my >>
lowres);
577 if (
s->chroma_y_shift) {
582 uvsrc_x =
s->mb_x * block_s + (
mx >>
lowres + 1);
583 uvsrc_y = (mb_y * block_s >> field_based) + (
my >>
lowres + 1);
585 if (
s->chroma_x_shift) {
589 uvsy = motion_y & s_mask;
591 uvsrc_x =
s->mb_x*block_s + (
mx >> (
lowres+1));
594 uvsx = motion_x & s_mask;
595 uvsy = motion_y & s_mask;
602 ptr_y = ref_picture[0] + src_y * linesize + src_x;
603 ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
604 ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
606 if ((
unsigned) src_x >
FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
607 (
unsigned) src_y >
FFMAX((v_edge_pos >> field_based) - (!!sy) -
FFMAX(
h, field_select + hc<<
s->chroma_y_shift), 0)) {
608 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr_y,
609 linesize >> field_based, linesize >> field_based,
610 17, 17 + field_based,
611 src_x, src_y * (1 << field_based), h_edge_pos,
613 ptr_y =
s->sc.edge_emu_buffer;
615 uint8_t *ubuf =
s->sc.edge_emu_buffer + 18 *
s->linesize;
616 uint8_t *vbuf =ubuf + 10 *
s->uvlinesize;
618 vbuf -=
s->uvlinesize;
619 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
620 uvlinesize >> field_based, uvlinesize >> field_based,
622 uvsrc_x, uvsrc_y * (1 << field_based),
623 h_edge_pos >> 1, v_edge_pos >> 1);
624 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
625 uvlinesize >> field_based,uvlinesize >> field_based,
627 uvsrc_x, uvsrc_y * (1 << field_based),
628 h_edge_pos >> 1, v_edge_pos >> 1);
636 dest_y +=
s->linesize;
637 dest_cb +=
s->uvlinesize;
638 dest_cr +=
s->uvlinesize;
642 ptr_y +=
s->linesize;
643 ptr_cb +=
s->uvlinesize;
644 ptr_cr +=
s->uvlinesize;
649 pix_op[
lowres - 1](dest_y, ptr_y, linesize,
h, sx, sy);
652 uvsx = (uvsx << 2) >>
lowres;
653 uvsy = (uvsy << 2) >>
lowres;
655 pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
656 pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
663 uint8_t *dest_cb, uint8_t *dest_cr,
664 uint8_t *
const *ref_picture,
668 const int lowres =
s->avctx->lowres;
669 const int op_index =
lowres;
670 const int block_s = 8 >>
lowres;
671 const int s_mask = (2 <<
lowres) - 1;
672 const int h_edge_pos =
s->h_edge_pos >>
lowres + 1;
673 const int v_edge_pos =
s->v_edge_pos >>
lowres + 1;
674 int emu = 0, src_x, src_y, sx, sy;
680 if (
s->quarter_sample) {
692 src_x =
s->mb_x * block_s + (
mx >>
lowres + 1);
693 src_y =
s->mb_y * block_s + (
my >>
lowres + 1);
695 offset = src_y *
s->uvlinesize + src_x;
696 ptr = ref_picture[1] +
offset;
697 if ((
unsigned) src_x >
FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
698 (unsigned) src_y >
FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
699 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
700 s->uvlinesize,
s->uvlinesize,
702 src_x, src_y, h_edge_pos, v_edge_pos);
703 ptr =
s->sc.edge_emu_buffer;
708 pix_op[op_index](dest_cb, ptr,
s->uvlinesize, block_s, sx, sy);
710 ptr = ref_picture[2] +
offset;
712 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
713 s->uvlinesize,
s->uvlinesize,
715 src_x, src_y, h_edge_pos, v_edge_pos);
716 ptr =
s->sc.edge_emu_buffer;
718 pix_op[op_index](dest_cr, ptr,
s->uvlinesize, block_s, sx, sy);
733 uint8_t *dest_y, uint8_t *dest_cb,
735 int dir, uint8_t *
const *ref_picture,
740 const int lowres =
s->avctx->lowres;
741 const int block_s = 8 >>
lowres;
746 switch (
s->mv_type) {
751 s->mv[dir][0][0],
s->mv[dir][0][1],
757 for (
int i = 0;
i < 4;
i++) {
759 s->linesize) * block_s,
760 ref_picture[0], 0, 0,
761 (2 * mb_x + (
i & 1)) * block_s,
762 (2 * mb_y + (
i >> 1)) * block_s,
763 s->width,
s->height,
s->linesize,
765 block_s, block_s, pix_op,
766 s->mv[dir][
i][0],
s->mv[dir][
i][1]);
768 mx +=
s->mv[dir][
i][0];
769 my +=
s->mv[dir][
i][1];
780 1, 0,
s->field_select[dir][0],
782 s->mv[dir][0][0],
s->mv[dir][0][1],
786 1, 1,
s->field_select[dir][1],
788 s->mv[dir][1][0],
s->mv[dir][1][1],
791 if (
s->picture_structure !=
s->field_select[dir][0] + 1 &&
793 ref_picture =
s->cur_pic.ptr->f->data;
796 0, 0,
s->field_select[dir][0],
799 s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
803 for (
int i = 0;
i < 2;
i++) {
804 uint8_t *
const *ref2picture;
806 if (
s->picture_structure ==
s->field_select[dir][
i] + 1 ||
808 ref2picture = ref_picture;
810 ref2picture =
s->cur_pic.ptr->f->data;
814 0, 0,
s->field_select[dir][
i],
816 s->mv[dir][
i][0],
s->mv[dir][
i][1] +
817 2 * block_s *
i, block_s, mb_y >> 1);
819 dest_y += 2 * block_s *
s->linesize;
820 dest_cb += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
821 dest_cr += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
826 for (
int i = 0;
i < 2;
i++) {
827 for (
int j = 0; j < 2; j++) {
831 s->mv[dir][2 *
i + j][0],
832 s->mv[dir][2 *
i + j][1],
835 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
838 for (
int i = 0;
i < 2;
i++) {
840 0, 0,
s->picture_structure !=
i + 1,
842 s->mv[dir][2 *
i][0],
s->mv[dir][2 *
i][1],
843 2 * block_s, mb_y >> 1);
846 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
850 if (!
s->first_field) {
851 ref_picture =
s->cur_pic.ptr->f->data;
866 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !
s->quarter_sample;
872 switch (
s->mv_type) {
886 for (
int i = 0;
i < mvs;
i++) {
887 int my =
s->mv[dir][
i][1];
892 off = ((
FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
894 return av_clip(
s->mb_y + off, 0,
s->mb_height - 1);
896 return s->mb_height - 1;
901 int16_t
block[][64],
int i, uint8_t *dest,
int line_size)
903 if (
s->block_last_index[
i] >= 0) {
904 s->idsp.idct_add(dest, line_size,
block[
i]);
910 int16_t *
block,
int i, uint8_t *dest,
int line_size,
int qscale)
912 s->dct_unquantize_intra(
s,
block,
i, qscale);
913 s->idsp.idct_put(dest, line_size,
block);
917 int16_t
block[][64],
int i, uint8_t *dest,
int line_size,
int qscale)
919 if (
s->block_last_index[
i] >= 0) {
920 s->dct_unquantize_inter(
s,
block[
i],
i, qscale);
922 s->idsp.idct_add(dest, line_size,
block[
i]);
926 #define NOT_MPEG12_H261 0
927 #define MAY_BE_MPEG12_H261 1
928 #define DEFINITELY_MPEG12_H261 2
941 int lowres_flag,
int is_mpeg12)
943 #define IS_MPEG12_H261(s) (is_mpeg12 == MAY_BE_MPEG12_H261 ? ((s)->out_format <= FMT_H261) : is_mpeg12)
944 uint8_t *dest_y =
s->dest[0], *dest_cb =
s->dest[1], *dest_cr =
s->dest[2];
946 const int linesize =
s->cur_pic.linesize[0];
947 const int uvlinesize =
s->cur_pic.linesize[1];
948 const int block_size = lowres_flag ? 8 >>
s->avctx->lowres : 8;
950 dct_linesize = linesize <<
s->interlaced_dct;
951 dct_offset =
s->interlaced_dct ? linesize : linesize * block_size;
972 op_pix =
s->h264chroma.avg_h264_chroma_pixels_tab;
982 op_pix =
s->hdsp.put_pixels_tab;
983 op_qpix =
s->qdsp.put_qpel_pixels_tab;
985 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
986 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
989 ff_mpv_motion(
s, dest_y, dest_cb, dest_cr, 0,
s->last_pic.data, op_pix, op_qpix);
990 op_pix =
s->hdsp.avg_pixels_tab;
991 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
994 ff_mpv_motion(
s, dest_y, dest_cb, dest_cr, 1,
s->next_pic.data, op_pix, op_qpix);
999 if (
s->avctx->skip_idct) {
1030 if (
s->chroma_y_shift) {
1035 dct_linesize = uvlinesize <<
s->interlaced_dct;
1036 dct_offset =
s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
1042 if (!
s->chroma_x_shift) {
1050 }
else if (CONFIG_WMV2_DECODER) {
1058 s->avctx->bits_per_raw_sample > 8) {
1064 put_dct(
s,
block[1], 1, dest_y + block_size, dct_linesize,
s->qscale);
1069 if (
s->chroma_y_shift) {
1075 put_dct(
s,
block[4], 4, dest_cb, dct_linesize,
s->chroma_qscale);
1076 put_dct(
s,
block[5], 5, dest_cr, dct_linesize,
s->chroma_qscale);
1082 s->idsp.idct_put(dest_y, dct_linesize,
block[0]);
1083 s->idsp.idct_put(dest_y + block_size, dct_linesize,
block[1]);
1085 s->idsp.idct_put(dest_y +
dct_offset + block_size, dct_linesize,
block[3]);
1088 if (
s->chroma_y_shift) {
1089 s->idsp.idct_put(dest_cb, uvlinesize,
block[4]);
1090 s->idsp.idct_put(dest_cr, uvlinesize,
block[5]);
1092 dct_linesize = uvlinesize <<
s->interlaced_dct;
1093 dct_offset =
s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
1095 s->idsp.idct_put(dest_cb, dct_linesize,
block[4]);
1096 s->idsp.idct_put(dest_cr, dct_linesize,
block[5]);
1099 if (!
s->chroma_x_shift) {
1100 s->idsp.idct_put(dest_cb + block_size, dct_linesize,
block[8]);
1101 s->idsp.idct_put(dest_cr + block_size, dct_linesize,
block[9]);
1102 s->idsp.idct_put(dest_cb + block_size +
dct_offset, dct_linesize,
block[10]);
1103 s->idsp.idct_put(dest_cr + block_size +
dct_offset, dct_linesize,
block[11]);
1116 void *
const logctx =
s->avctx;
1117 const uint8_t *
const idct_permutation =
s->idsp.idct_permutation;
1121 for (
int i = 0;
i < 6;
i++) {
1122 for (
int j = 0; j < 64; j++) {
1124 block[
i][idct_permutation[j]]);
1132 const int mb_xy =
s->mb_y *
s->mb_stride +
s->mb_x;
1133 uint8_t *mbskip_ptr = &
s->mbskip_table[mb_xy];
1135 s->cur_pic.qscale_table[mb_xy] =
s->qscale;
1138 if (
s->mb_skipped) {
1142 }
else if (!
s->cur_pic.reference) {
1152 if (!
s->avctx->lowres) {