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)
104 s->h264chroma.avg_h264_chroma_pixels_tab[3] = avg_h264_chroma_mc1;
105 s->h264chroma.put_h264_chroma_pixels_tab[3] = put_h264_chroma_mc1;
113 if (!
s->picture_pool)
131 if (
s->height != s1->
height ||
s->width != s1->
width ||
s->context_reinit) {
179 if (!
s->context_initialized)
188 if ((
s->width ||
s->height) &&
202 memset(
s->thread_context, 0,
sizeof(
s->thread_context));
203 s->thread_context[0] =
s;
205 if (
s->width &&
s->height) {
210 s->context_reinit = 0;
215 s->context_reinit = 1;
256 av_assert1(
s->mb_width ==
s->buffer_pools.alloc_mb_width);
257 av_assert1(
s->mb_height ==
s->buffer_pools.alloc_mb_height ||
258 FFALIGN(
s->mb_height, 2) ==
s->buffer_pools.alloc_mb_height);
259 av_assert1(
s->mb_stride ==
s->buffer_pools.alloc_mb_stride);
261 &
s->buffer_pools,
s->mb_height);
288 int h_chroma_shift, v_chroma_shift;
290 for (
int i = 0;
i <
frame->height;
i++)
309 av_assert1(!
s->last_pic.ptr ||
s->last_pic.ptr->f->buf[0]);
310 av_assert1(!
s->next_pic.ptr ||
s->next_pic.ptr->f->buf[0]);
314 "allocating dummy last picture for B frame\n");
318 "warning: first frame is no keyframe\n");
338 s->last_pic.ptr->f->buf[0]));
366 (!
s->progressive_frame && !
s->progressive_sequence);
367 s->cur_pic.ptr->field_picture =
s->picture_structure !=
PICT_FRAME;
369 s->cur_pic.ptr->f->pict_type =
s->pict_type;
380 ff_dlog(
s->avctx,
"L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
381 (
void*)
s->last_pic.ptr, (
void*)
s->next_pic.ptr, (
void*)
s->cur_pic.ptr,
382 s->last_pic.ptr ?
s->last_pic.ptr->f->data[0] :
NULL,
383 s->next_pic.ptr ?
s->next_pic.ptr->f->data[0] :
NULL,
384 s->cur_pic.ptr ?
s->cur_pic.ptr->f->data[0] :
NULL,
385 s->pict_type,
s->droppable);
402 if (
s->cur_pic.reference)
409 p->qscale_table,
p->motion_val,
410 p->mb_width,
p->mb_height,
p->mb_stride,
s->quarter_sample);
418 unsigned int nb_mb =
p->mb_height *
p->mb_width;
427 for (
unsigned y = 0; y <
p->mb_height; y++)
428 for (
unsigned x = 0; x <
p->mb_width; x++) {
429 const unsigned int block_idx = y *
p->mb_width + x;
430 const unsigned int mb_xy = y *
p->mb_stride + x;
438 b->delta_qp =
p->qscale_table[mb_xy] *
mult;
447 s->last_pic.ptr ?
s->last_pic.ptr->f :
NULL,
448 y,
h,
s->picture_structure,
449 s->first_field,
s->low_delay);
460 s->mb_x =
s->mb_y = 0;
466 uint8_t *
dest,
const uint8_t *
src,
468 int src_x,
int src_y,
472 int motion_x,
int motion_y)
474 const int lowres =
s->avctx->lowres;
475 const int op_index =
lowres;
476 const int s_mask = (2 <<
lowres) - 1;
482 if (
s->quarter_sample) {
487 sx = motion_x & s_mask;
488 sy = motion_y & s_mask;
489 src_x += motion_x >>
lowres + 1;
490 src_y += motion_y >>
lowres + 1;
496 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer,
src,
497 s->linesize,
s->linesize,
498 w + 1, (
h + 1) << field_based,
499 src_x, src_y * (1 << field_based),
501 src =
s->sc.edge_emu_buffer;
521 uint8_t *
const *ref_picture,
523 int motion_x,
int motion_y,
526 const uint8_t *ptr_y, *ptr_cb, *ptr_cr;
527 int mx,
my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
529 const int lowres =
s->avctx->lowres;
530 const int op_index =
lowres - 1 +
s->chroma_x_shift;
531 const int block_s = 8 >>
lowres;
532 const int s_mask = (2 <<
lowres) - 1;
535 int hc =
s->chroma_y_shift ? (
h+1-bottom_field)>>1 :
h;
539 linesize =
s->cur_pic.linesize[0] << field_based;
540 uvlinesize =
s->cur_pic.linesize[1] << field_based;
543 if (
s->quarter_sample) {
552 sx = motion_x & s_mask;
553 sy = motion_y & s_mask;
554 src_x =
s->mb_x * 2 * block_s + (motion_x >>
lowres + 1);
555 src_y = (
mb_y * 2 * block_s >> field_based) + (motion_y >>
lowres + 1);
558 uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
559 uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
560 uvsrc_x = src_x >> 1;
561 uvsrc_y = src_y >> 1;
566 uvsx = (2 *
mx) & s_mask;
567 uvsy = (2 *
my) & s_mask;
568 uvsrc_x =
s->mb_x * block_s + (
mx >>
lowres);
571 if (
s->chroma_y_shift) {
576 uvsrc_x =
s->mb_x * block_s + (
mx >>
lowres + 1);
577 uvsrc_y = (
mb_y * block_s >> field_based) + (
my >>
lowres + 1);
579 if (
s->chroma_x_shift) {
583 uvsy = motion_y & s_mask;
585 uvsrc_x =
s->mb_x*block_s + (
mx >> (
lowres+1));
588 uvsx = motion_x & s_mask;
589 uvsy = motion_y & s_mask;
596 ptr_y = ref_picture[0] + src_y *
linesize + src_x;
597 ptr_cb = ref_picture[1] + uvsrc_y *
uvlinesize + uvsrc_x;
598 ptr_cr = ref_picture[2] + uvsrc_y *
uvlinesize + uvsrc_x;
600 if ((
unsigned) src_x >
FFMAX(
h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
602 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr_y,
604 17, 17 + field_based,
605 src_x, src_y * (1 << field_based),
h_edge_pos,
607 ptr_y =
s->sc.edge_emu_buffer;
609 uint8_t *ubuf =
s->sc.edge_emu_buffer + 18 *
s->linesize;
610 uint8_t *vbuf =ubuf + 10 *
s->uvlinesize;
612 vbuf -=
s->uvlinesize;
613 s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
616 uvsrc_x, uvsrc_y * (1 << field_based),
618 s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
621 uvsrc_x, uvsrc_y * (1 << field_based),
630 dest_y +=
s->linesize;
631 dest_cb +=
s->uvlinesize;
632 dest_cr +=
s->uvlinesize;
636 ptr_y +=
s->linesize;
637 ptr_cb +=
s->uvlinesize;
638 ptr_cr +=
s->uvlinesize;
646 uvsx = (uvsx << 2) >>
lowres;
647 uvsy = (uvsy << 2) >>
lowres;
649 pix_op[op_index](dest_cb, ptr_cb,
uvlinesize, hc, uvsx, uvsy);
650 pix_op[op_index](dest_cr, ptr_cr,
uvlinesize, hc, uvsx, uvsy);
657 uint8_t *dest_cb, uint8_t *dest_cr,
658 uint8_t *
const *ref_picture,
662 const int lowres =
s->avctx->lowres;
663 const int op_index =
lowres;
664 const int block_s = 8 >>
lowres;
665 const int s_mask = (2 <<
lowres) - 1;
668 int emu = 0, src_x, src_y, sx, sy;
674 if (
s->quarter_sample) {
686 src_x =
s->mb_x * block_s + (
mx >>
lowres + 1);
687 src_y =
s->mb_y * block_s + (
my >>
lowres + 1);
689 offset = src_y *
s->uvlinesize + src_x;
690 ptr = ref_picture[1] +
offset;
693 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
694 s->uvlinesize,
s->uvlinesize,
697 ptr =
s->sc.edge_emu_buffer;
702 pix_op[op_index](dest_cb, ptr,
s->uvlinesize, block_s, sx, sy);
704 ptr = ref_picture[2] +
offset;
706 s->vdsp.emulated_edge_mc(
s->sc.edge_emu_buffer, ptr,
707 s->uvlinesize,
s->uvlinesize,
710 ptr =
s->sc.edge_emu_buffer;
712 pix_op[op_index](dest_cr, ptr,
s->uvlinesize, block_s, sx, sy);
727 uint8_t *dest_y, uint8_t *dest_cb,
729 int dir, uint8_t *
const *ref_picture,
734 const int lowres =
s->avctx->lowres;
735 const int block_s = 8 >>
lowres;
740 switch (
s->mv_type) {
745 s->mv[dir][0][0],
s->mv[dir][0][1],
751 for (
int i = 0;
i < 4;
i++) {
753 s->linesize) * block_s,
754 ref_picture[0], 0, 0,
755 (2 *
mb_x + (
i & 1)) * block_s,
756 (2 *
mb_y + (
i >> 1)) * block_s,
757 s->width,
s->height,
s->linesize,
759 block_s, block_s, pix_op,
760 s->mv[dir][
i][0],
s->mv[dir][
i][1]);
762 mx +=
s->mv[dir][
i][0];
763 my +=
s->mv[dir][
i][1];
774 1, 0,
s->field_select[dir][0],
776 s->mv[dir][0][0],
s->mv[dir][0][1],
780 1, 1,
s->field_select[dir][1],
782 s->mv[dir][1][0],
s->mv[dir][1][1],
785 if (
s->picture_structure !=
s->field_select[dir][0] + 1 &&
787 ref_picture =
s->cur_pic.ptr->f->data;
790 0, 0,
s->field_select[dir][0],
793 s->mv[dir][0][1], 2 * block_s,
mb_y >> 1);
797 for (
int i = 0;
i < 2;
i++) {
798 uint8_t *
const *ref2picture;
800 if (
s->picture_structure ==
s->field_select[dir][
i] + 1 ||
802 ref2picture = ref_picture;
804 ref2picture =
s->cur_pic.ptr->f->data;
808 0, 0,
s->field_select[dir][
i],
810 s->mv[dir][
i][0],
s->mv[dir][
i][1] +
811 2 * block_s *
i, block_s,
mb_y >> 1);
813 dest_y += 2 * block_s *
s->linesize;
814 dest_cb += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
815 dest_cr += (2 * block_s >>
s->chroma_y_shift) *
s->uvlinesize;
820 for (
int i = 0;
i < 2;
i++) {
821 for (
int j = 0; j < 2; j++) {
825 s->mv[dir][2 *
i + j][0],
826 s->mv[dir][2 *
i + j][1],
829 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
832 for (
int i = 0;
i < 2;
i++) {
834 0, 0,
s->picture_structure !=
i + 1,
836 s->mv[dir][2 *
i][0],
s->mv[dir][2 *
i][1],
837 2 * block_s,
mb_y >> 1);
840 pix_op =
s->h264chroma.avg_h264_chroma_pixels_tab;
844 if (!
s->first_field) {
845 ref_picture =
s->cur_pic.ptr->f->data;
860 int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !
s->quarter_sample;
866 switch (
s->mv_type) {
880 for (
int i = 0;
i < mvs;
i++) {
881 int my =
s->mv[dir][
i][1];
886 off = ((
FFMAX(-my_min, my_max) << qpel_shift) + 63) >> 6;
888 return av_clip(
s->mb_y + off, 0,
s->mb_height - 1);
890 return s->mb_height - 1;
895 int16_t
block[][64],
int i, uint8_t *
dest,
int line_size)
897 if (
s->block_last_index[
i] >= 0) {
913 if (
s->block_last_index[
i] >= 0) {
920 #define NOT_MPEG12_H261 0
921 #define MAY_BE_MPEG12_H261 1
922 #define DEFINITELY_MPEG12_H261 2
935 int lowres_flag,
int is_mpeg12)
937 #define IS_MPEG12_H261(s) (is_mpeg12 == MAY_BE_MPEG12_H261 ? ((s)->out_format <= FMT_H261) : is_mpeg12)
938 uint8_t *dest_y =
s->dest[0], *dest_cb =
s->dest[1], *dest_cr =
s->dest[2];
939 int dct_linesize, dct_offset;
940 const int linesize =
s->cur_pic.linesize[0];
942 const int block_size = lowres_flag ? 8 >>
s->avctx->lowres : 8;
944 dct_linesize =
linesize <<
s->interlaced_dct;
966 op_pix =
s->h264chroma.avg_h264_chroma_pixels_tab;
976 op_pix =
s->hdsp.put_pixels_tab;
977 op_qpix =
s->qdsp.put_qpel_pixels_tab;
979 op_pix =
s->hdsp.put_no_rnd_pixels_tab;
980 op_qpix =
s->qdsp.put_no_rnd_qpel_pixels_tab;
983 ff_mpv_motion(
s, dest_y, dest_cb, dest_cr, 0,
s->last_pic.data, op_pix, op_qpix);
984 op_pix =
s->hdsp.avg_pixels_tab;
985 op_qpix =
s->qdsp.avg_qpel_pixels_tab;
988 ff_mpv_motion(
s, dest_y, dest_cb, dest_cr, 1,
s->next_pic.data, op_pix, op_qpix);
993 if (
s->avctx->skip_idct) {
1021 add_dct(
s,
block, 3, dest_y + dct_offset + block_size, dct_linesize);
1024 if (
s->chroma_y_shift) {
1036 if (!
s->chroma_x_shift) {
1039 add_dct(
s,
block, 10, dest_cb + block_size + dct_offset, dct_linesize);
1040 add_dct(
s,
block, 11, dest_cr + block_size + dct_offset, dct_linesize);
1044 }
else if (CONFIG_WMV2_DECODER) {
1052 s->avctx->bits_per_raw_sample > 8) {
1058 put_dct(
s,
block[1], 1, dest_y + block_size, dct_linesize,
s->qscale);
1059 put_dct(
s,
block[2], 2, dest_y + dct_offset , dct_linesize,
s->qscale);
1060 put_dct(
s,
block[3], 3, dest_y + dct_offset + block_size, dct_linesize,
s->qscale);
1063 if (
s->chroma_y_shift) {
1069 put_dct(
s,
block[4], 4, dest_cb, dct_linesize,
s->chroma_qscale);
1070 put_dct(
s,
block[5], 5, dest_cr, dct_linesize,
s->chroma_qscale);
1071 put_dct(
s,
block[6], 6, dest_cb + dct_offset, dct_linesize,
s->chroma_qscale);
1072 put_dct(
s,
block[7], 7, dest_cr + dct_offset, dct_linesize,
s->chroma_qscale);
1076 s->idsp.idct_put(dest_y, dct_linesize,
block[0]);
1077 s->idsp.idct_put(dest_y + block_size, dct_linesize,
block[1]);
1078 s->idsp.idct_put(dest_y + dct_offset, dct_linesize,
block[2]);
1079 s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize,
block[3]);
1082 if (
s->chroma_y_shift) {
1089 s->idsp.idct_put(dest_cb, dct_linesize,
block[4]);
1090 s->idsp.idct_put(dest_cr, dct_linesize,
block[5]);
1091 s->idsp.idct_put(dest_cb + dct_offset, dct_linesize,
block[6]);
1092 s->idsp.idct_put(dest_cr + dct_offset, dct_linesize,
block[7]);
1093 if (!
s->chroma_x_shift) {
1094 s->idsp.idct_put(dest_cb + block_size, dct_linesize,
block[8]);
1095 s->idsp.idct_put(dest_cr + block_size, dct_linesize,
block[9]);
1096 s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize,
block[10]);
1097 s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize,
block[11]);
1110 void *
const logctx =
s->avctx;
1111 const uint8_t *
const idct_permutation =
s->idsp.idct_permutation;
1115 for (
int i = 0;
i < 6;
i++) {
1116 for (
int j = 0; j < 64; j++) {
1118 block[
i][idct_permutation[j]]);
1126 const int mb_xy =
s->mb_y *
s->mb_stride +
s->mb_x;
1127 uint8_t *mbskip_ptr = &
s->mbskip_table[mb_xy];
1129 s->cur_pic.qscale_table[mb_xy] =
s->qscale;
1132 if (
s->mb_skipped) {
1136 }
else if (!
s->cur_pic.reference) {
1146 if (!
s->avctx->lowres) {