646
709
* the changed fields will not depend upon
647
710
* the prior state of the MpegEncContext.
649
void MPV_decode_defaults(MpegEncContext *s)
651
MPV_common_defaults(s);
712
void ff_MPV_decode_defaults(MpegEncContext *s)
714
ff_MPV_common_defaults(s);
718
* Initialize and allocates MpegEncContext fields dependent on the resolution.
720
static int init_context_frame(MpegEncContext *s)
722
int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
724
s->mb_width = (s->width + 15) / 16;
725
s->mb_stride = s->mb_width + 1;
726
s->b8_stride = s->mb_width * 2 + 1;
727
s->b4_stride = s->mb_width * 4 + 1;
728
mb_array_size = s->mb_height * s->mb_stride;
729
mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
731
/* set default edge pos, will be overriden
732
* in decode_header if needed */
733
s->h_edge_pos = s->mb_width * 16;
734
s->v_edge_pos = s->mb_height * 16;
736
s->mb_num = s->mb_width * s->mb_height;
741
s->block_wrap[3] = s->b8_stride;
743
s->block_wrap[5] = s->mb_stride;
745
y_size = s->b8_stride * (2 * s->mb_height + 1);
746
c_size = s->mb_stride * (s->mb_height + 1);
747
yc_size = y_size + 2 * c_size;
749
FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
750
fail); // error ressilience code looks cleaner with this
751
for (y = 0; y < s->mb_height; y++)
752
for (x = 0; x < s->mb_width; x++)
753
s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
755
s->mb_index2xy[s->mb_height * s->mb_width] =
756
(s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
759
/* Allocate MV tables */
760
FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
761
mv_table_size * 2 * sizeof(int16_t), fail);
762
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
763
mv_table_size * 2 * sizeof(int16_t), fail);
764
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
765
mv_table_size * 2 * sizeof(int16_t), fail);
766
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
767
mv_table_size * 2 * sizeof(int16_t), fail);
768
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
769
mv_table_size * 2 * sizeof(int16_t), fail);
770
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
771
mv_table_size * 2 * sizeof(int16_t), fail);
772
s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
773
s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1;
774
s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1;
775
s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
777
s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
779
s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1;
781
/* Allocate MB type table */
782
FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
783
sizeof(uint16_t), fail); // needed for encoding
785
FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
788
FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
789
mb_array_size * sizeof(float), fail);
790
FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
791
mb_array_size * sizeof(float), fail);
795
FF_ALLOC_OR_GOTO(s->avctx, s->er_temp_buffer,
796
mb_array_size * sizeof(uint8_t), fail);
797
FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
798
mb_array_size * sizeof(uint8_t), fail);
800
if (s->codec_id == AV_CODEC_ID_MPEG4 ||
801
(s->flags & CODEC_FLAG_INTERLACED_ME)) {
802
/* interlaced direct mode decoding tables */
803
for (i = 0; i < 2; i++) {
805
for (j = 0; j < 2; j++) {
806
for (k = 0; k < 2; k++) {
807
FF_ALLOCZ_OR_GOTO(s->avctx,
808
s->b_field_mv_table_base[i][j][k],
809
mv_table_size * 2 * sizeof(int16_t),
811
s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
814
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
815
mb_array_size * 2 * sizeof(uint8_t), fail);
816
FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
817
mv_table_size * 2 * sizeof(int16_t), fail);
818
s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
821
FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
822
mb_array_size * 2 * sizeof(uint8_t), fail);
825
if (s->out_format == FMT_H263) {
827
FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
828
s->coded_block = s->coded_block_base + s->b8_stride + 1;
830
/* cbp, ac_pred, pred_dir */
831
FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
832
mb_array_size * sizeof(uint8_t), fail);
833
FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
834
mb_array_size * sizeof(uint8_t), fail);
837
if (s->h263_pred || s->h263_plus || !s->encoding) {
839
// MN: we need these for error resilience of intra-frames
840
FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
841
yc_size * sizeof(int16_t), fail);
842
s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
843
s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
844
s->dc_val[2] = s->dc_val[1] + c_size;
845
for (i = 0; i < yc_size; i++)
846
s->dc_val_base[i] = 1024;
849
/* which mb is a intra block */
850
FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
851
memset(s->mbintra_table, 1, mb_array_size);
853
/* init macroblock skip table */
854
FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
855
// Note the + 1 is for a quicker mpeg4 slice_end detection
857
if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
858
s->avctx->debug_mv) {
859
s->visualization_buffer[0] = av_malloc((s->mb_width * 16 +
860
2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
861
s->visualization_buffer[1] = av_malloc((s->mb_width * 16 +
862
2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
863
s->visualization_buffer[2] = av_malloc((s->mb_width * 16 +
864
2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
869
return AVERROR(ENOMEM);
655
873
* init common structure for both encoder and decoder.
656
874
* this assumes that some variables like width/height are already set
658
av_cold int MPV_common_init(MpegEncContext *s)
876
av_cold int ff_MPV_common_init(MpegEncContext *s)
660
int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
661
879
int nb_slices = (HAVE_THREADS &&
662
880
s->avctx->active_thread_type & FF_THREAD_SLICE) ?
663
881
s->avctx->thread_count : 1;
697
915
s->flags2 = s->avctx->flags2;
699
917
if (s->width && s->height) {
700
s->mb_width = (s->width + 15) / 16;
701
s->mb_stride = s->mb_width + 1;
702
s->b8_stride = s->mb_width * 2 + 1;
703
s->b4_stride = s->mb_width * 4 + 1;
704
mb_array_size = s->mb_height * s->mb_stride;
705
mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
707
918
/* set chroma shifts */
708
avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift,
711
/* set default edge pos, will be overriden
712
* in decode_header if needed */
713
s->h_edge_pos = s->mb_width * 16;
714
s->v_edge_pos = s->mb_height * 16;
716
s->mb_num = s->mb_width * s->mb_height;
721
s->block_wrap[3] = s->b8_stride;
723
s->block_wrap[5] = s->mb_stride;
725
y_size = s->b8_stride * (2 * s->mb_height + 1);
726
c_size = s->mb_stride * (s->mb_height + 1);
727
yc_size = y_size + 2 * c_size;
919
av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
729
923
/* convert fourcc to upper case */
730
924
s->codec_tag = avpriv_toupper4(s->avctx->codec_tag);
732
926
s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
734
s->avctx->coded_frame = (AVFrame *)&s->current_picture;
736
FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
737
fail); // error ressilience code looks cleaner with this
738
for (y = 0; y < s->mb_height; y++)
739
for (x = 0; x < s->mb_width; x++)
740
s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
742
s->mb_index2xy[s->mb_height * s->mb_width] =
743
(s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
928
s->avctx->coded_frame = &s->current_picture.f;
745
930
if (s->encoding) {
746
/* Allocate MV tables */
747
FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
748
mv_table_size * 2 * sizeof(int16_t), fail);
749
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
750
mv_table_size * 2 * sizeof(int16_t), fail);
751
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
752
mv_table_size * 2 * sizeof(int16_t), fail);
753
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
754
mv_table_size * 2 * sizeof(int16_t), fail);
755
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
756
mv_table_size * 2 * sizeof(int16_t), fail);
757
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
758
mv_table_size * 2 * sizeof(int16_t), fail);
759
s->p_mv_table = s->p_mv_table_base +
761
s->b_forw_mv_table = s->b_forw_mv_table_base +
763
s->b_back_mv_table = s->b_back_mv_table_base +
765
s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
767
s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
769
s->b_direct_mv_table = s->b_direct_mv_table_base +
772
931
if (s->msmpeg4_version) {
773
932
FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
774
933
2 * 2 * (MAX_LEVEL + 1) *
807
959
FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
808
960
s->picture_count * sizeof(Picture), fail);
809
961
for (i = 0; i < s->picture_count; i++) {
810
avcodec_get_frame_defaults((AVFrame *) &s->picture[i]);
962
avcodec_get_frame_defaults(&s->picture[i].f);
813
965
if (s->width && s->height) {
814
FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
815
mb_array_size * sizeof(uint8_t), fail);
817
if (s->codec_id == CODEC_ID_MPEG4 ||
818
(s->flags & CODEC_FLAG_INTERLACED_ME)) {
819
/* interlaced direct mode decoding tables */
820
for (i = 0; i < 2; i++) {
822
for (j = 0; j < 2; j++) {
823
for (k = 0; k < 2; k++) {
824
FF_ALLOCZ_OR_GOTO(s->avctx,
825
s->b_field_mv_table_base[i][j][k],
826
mv_table_size * 2 * sizeof(int16_t),
828
s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
831
FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
832
mb_array_size * 2 * sizeof(uint8_t),
834
FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
835
mv_table_size * 2 * sizeof(int16_t),
837
s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
840
FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
841
mb_array_size * 2 * sizeof(uint8_t),
845
if (s->out_format == FMT_H263) {
847
FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
848
s->coded_block = s->coded_block_base + s->b8_stride + 1;
850
/* cbp, ac_pred, pred_dir */
851
FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
852
mb_array_size * sizeof(uint8_t), fail);
853
FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
854
mb_array_size * sizeof(uint8_t), fail);
857
if (s->h263_pred || s->h263_plus || !s->encoding) {
859
// MN: we need these for error resilience of intra-frames
860
FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
861
yc_size * sizeof(int16_t), fail);
862
s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
863
s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
864
s->dc_val[2] = s->dc_val[1] + c_size;
865
for (i = 0; i < yc_size; i++)
866
s->dc_val_base[i] = 1024;
869
/* which mb is a intra block */
870
FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
871
memset(s->mbintra_table, 1, mb_array_size);
873
/* init macroblock skip table */
874
FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
875
// Note the + 1 is for a quicker mpeg4 slice_end detection
966
if (init_context_frame(s))
877
969
s->parse_context.state = -1;
878
if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
879
s->avctx->debug_mv) {
880
s->visualization_buffer[0] = av_malloc((s->mb_width * 16 +
881
2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
882
s->visualization_buffer[1] = av_malloc((s->mb_width * 16 +
883
2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
884
s->visualization_buffer[2] = av_malloc((s->mb_width * 16 +
885
2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
889
972
s->context_initialized = 1;
970
1044
av_freep(&s->pred_dir_table);
972
1046
av_freep(&s->mbskip_table);
1048
av_freep(&s->error_status_table);
1049
av_freep(&s->er_temp_buffer);
1050
av_freep(&s->mb_index2xy);
1051
av_freep(&s->lambda_table);
1052
av_freep(&s->cplx_tab);
1053
av_freep(&s->bits_tab);
1055
s->linesize = s->uvlinesize = 0;
1057
for (i = 0; i < 3; i++)
1058
av_freep(&s->visualization_buffer[i]);
1063
int ff_MPV_common_frame_size_change(MpegEncContext *s)
1067
if (s->slice_context_count > 1) {
1068
for (i = 0; i < s->slice_context_count; i++) {
1069
free_duplicate_context(s->thread_context[i]);
1071
for (i = 1; i < s->slice_context_count; i++) {
1072
av_freep(&s->thread_context[i]);
1075
free_duplicate_context(s);
1077
free_context_frame(s);
1080
for (i = 0; i < s->picture_count; i++) {
1081
s->picture[i].needs_realloc = 1;
1084
s->last_picture_ptr =
1085
s->next_picture_ptr =
1086
s->current_picture_ptr = NULL;
1089
if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
1090
s->mb_height = (s->height + 31) / 32 * 2;
1091
else if (s->codec_id != AV_CODEC_ID_H264)
1092
s->mb_height = (s->height + 15) / 16;
1094
if ((s->width || s->height) &&
1095
av_image_check_size(s->width, s->height, 0, s->avctx))
1096
return AVERROR_INVALIDDATA;
1098
if ((err = init_context_frame(s)))
1101
s->thread_context[0] = s;
1103
if (s->width && s->height) {
1104
int nb_slices = s->slice_context_count;
1105
if (nb_slices > 1) {
1106
for (i = 1; i < nb_slices; i++) {
1107
s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1108
memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1111
for (i = 0; i < nb_slices; i++) {
1112
if (init_duplicate_context(s->thread_context[i], s) < 0)
1114
s->thread_context[i]->start_mb_y =
1115
(s->mb_height * (i) + nb_slices / 2) / nb_slices;
1116
s->thread_context[i]->end_mb_y =
1117
(s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1120
if (init_duplicate_context(s, s) < 0)
1123
s->end_mb_y = s->mb_height;
1125
s->slice_context_count = nb_slices;
1130
ff_MPV_common_end(s);
1134
/* init common structure for both encoder and decoder */
1135
void ff_MPV_common_end(MpegEncContext *s)
1139
if (s->slice_context_count > 1) {
1140
for (i = 0; i < s->slice_context_count; i++) {
1141
free_duplicate_context(s->thread_context[i]);
1143
for (i = 1; i < s->slice_context_count; i++) {
1144
av_freep(&s->thread_context[i]);
1146
s->slice_context_count = 1;
1147
} else free_duplicate_context(s);
1149
av_freep(&s->parse_context.buffer);
1150
s->parse_context.buffer_size = 0;
973
1152
av_freep(&s->bitstream_buffer);
974
1153
s->allocated_bitstream_buffer_size = 0;
976
1155
av_freep(&s->avctx->stats_out);
977
1156
av_freep(&s->ac_stats);
978
av_freep(&s->error_status_table);
979
av_freep(&s->mb_index2xy);
980
av_freep(&s->lambda_table);
981
1158
av_freep(&s->q_intra_matrix);
982
1159
av_freep(&s->q_inter_matrix);
983
1160
av_freep(&s->q_intra_matrix16);
1818
static inline int hpel_motion_lowres(MpegEncContext *s,
1819
uint8_t *dest, uint8_t *src,
1820
int field_based, int field_select,
1821
int src_x, int src_y,
1822
int width, int height, int stride,
1823
int h_edge_pos, int v_edge_pos,
1824
int w, int h, h264_chroma_mc_func *pix_op,
1825
int motion_x, int motion_y)
1827
const int lowres = s->avctx->lowres;
1828
const int op_index = FFMIN(lowres, 2);
1829
const int s_mask = (2 << lowres) - 1;
1833
if (s->quarter_sample) {
1838
sx = motion_x & s_mask;
1839
sy = motion_y & s_mask;
1840
src_x += motion_x >> lowres + 1;
1841
src_y += motion_y >> lowres + 1;
1843
src += src_y * stride + src_x;
1845
if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
1846
(unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
1847
s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w + 1,
1848
(h + 1) << field_based, src_x,
1849
src_y << field_based,
1852
src = s->edge_emu_buffer;
1856
sx = (sx << 2) >> lowres;
1857
sy = (sy << 2) >> lowres;
1860
pix_op[op_index](dest, src, stride, h, sx, sy);
1864
/* apply one mpeg motion vector to the three components */
1865
static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
1872
uint8_t **ref_picture,
1873
h264_chroma_mc_func *pix_op,
1874
int motion_x, int motion_y,
1877
uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1878
int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,
1880
const int lowres = s->avctx->lowres;
1881
const int op_index = FFMIN(lowres, 2);
1882
const int block_s = 8>>lowres;
1883
const int s_mask = (2 << lowres) - 1;
1884
const int h_edge_pos = s->h_edge_pos >> lowres;
1885
const int v_edge_pos = s->v_edge_pos >> lowres;
1886
linesize = s->current_picture.f.linesize[0] << field_based;
1887
uvlinesize = s->current_picture.f.linesize[1] << field_based;
1889
// FIXME obviously not perfect but qpel will not work in lowres anyway
1890
if (s->quarter_sample) {
1896
motion_y += (bottom_field - field_select) * (1 << lowres - 1);
1899
sx = motion_x & s_mask;
1900
sy = motion_y & s_mask;
1901
src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
1902
src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
1904
if (s->out_format == FMT_H263) {
1905
uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
1906
uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
1907
uvsrc_x = src_x >> 1;
1908
uvsrc_y = src_y >> 1;
1909
} else if (s->out_format == FMT_H261) {
1910
// even chroma mv's are full pel in H261
1913
uvsx = (2 * mx) & s_mask;
1914
uvsy = (2 * my) & s_mask;
1915
uvsrc_x = s->mb_x * block_s + (mx >> lowres);
1916
uvsrc_y = mb_y * block_s + (my >> lowres);
1922
uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
1923
uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
1926
ptr_y = ref_picture[0] + src_y * linesize + src_x;
1927
ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
1928
ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
1930
if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) ||
1931
(unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
1932
s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
1933
s->linesize, 17, 17 + field_based,
1934
src_x, src_y << field_based, h_edge_pos,
1936
ptr_y = s->edge_emu_buffer;
1937
if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
1938
uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
1939
s->dsp.emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9,
1941
uvsrc_x, uvsrc_y << field_based,
1942
h_edge_pos >> 1, v_edge_pos >> 1);
1943
s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9,
1945
uvsrc_x, uvsrc_y << field_based,
1946
h_edge_pos >> 1, v_edge_pos >> 1);
1948
ptr_cr = uvbuf + 16;
1952
// FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
1954
dest_y += s->linesize;
1955
dest_cb += s->uvlinesize;
1956
dest_cr += s->uvlinesize;
1960
ptr_y += s->linesize;
1961
ptr_cb += s->uvlinesize;
1962
ptr_cr += s->uvlinesize;
1965
sx = (sx << 2) >> lowres;
1966
sy = (sy << 2) >> lowres;
1967
pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
1969
if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
1970
uvsx = (uvsx << 2) >> lowres;
1971
uvsy = (uvsy << 2) >> lowres;
1972
pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift,
1974
pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift,
1977
// FIXME h261 lowres loop filter
1980
static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
1981
uint8_t *dest_cb, uint8_t *dest_cr,
1982
uint8_t **ref_picture,
1983
h264_chroma_mc_func * pix_op,
1986
const int lowres = s->avctx->lowres;
1987
const int op_index = FFMIN(lowres, 2);
1988
const int block_s = 8 >> lowres;
1989
const int s_mask = (2 << lowres) - 1;
1990
const int h_edge_pos = s->h_edge_pos >> lowres + 1;
1991
const int v_edge_pos = s->v_edge_pos >> lowres + 1;
1992
int emu = 0, src_x, src_y, offset, sx, sy;
1995
if (s->quarter_sample) {
2000
/* In case of 8X8, we construct a single chroma motion vector
2001
with a special rounding */
2002
mx = ff_h263_round_chroma(mx);
2003
my = ff_h263_round_chroma(my);
2007
src_x = s->mb_x * block_s + (mx >> lowres + 1);
2008
src_y = s->mb_y * block_s + (my >> lowres + 1);
2010
offset = src_y * s->uvlinesize + src_x;
2011
ptr = ref_picture[1] + offset;
2012
if (s->flags & CODEC_FLAG_EMU_EDGE) {
2013
if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
2014
(unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
2015
s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
2016
9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
2017
ptr = s->edge_emu_buffer;
2021
sx = (sx << 2) >> lowres;
2022
sy = (sy << 2) >> lowres;
2023
pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
2025
ptr = ref_picture[2] + offset;
2027
s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
2028
src_x, src_y, h_edge_pos, v_edge_pos);
2029
ptr = s->edge_emu_buffer;
2031
pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
2035
* motion compensation of a single macroblock
2037
* @param dest_y luma destination pointer
2038
* @param dest_cb chroma cb/u destination pointer
2039
* @param dest_cr chroma cr/v destination pointer
2040
* @param dir direction (0->forward, 1->backward)
2041
* @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2042
* @param pix_op halfpel motion compensation function (average or put normally)
2043
* the motion vectors are taken from s->mv and the MV type from s->mv_type
2045
static inline void MPV_motion_lowres(MpegEncContext *s,
2046
uint8_t *dest_y, uint8_t *dest_cb,
2048
int dir, uint8_t **ref_picture,
2049
h264_chroma_mc_func *pix_op)
2053
const int lowres = s->avctx->lowres;
2054
const int block_s = 8 >>lowres;
2059
switch (s->mv_type) {
2061
mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2063
ref_picture, pix_op,
2064
s->mv[dir][0][0], s->mv[dir][0][1],
2070
for (i = 0; i < 4; i++) {
2071
hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
2072
s->linesize) * block_s,
2073
ref_picture[0], 0, 0,
2074
(2 * mb_x + (i & 1)) * block_s,
2075
(2 * mb_y + (i >> 1)) * block_s,
2076
s->width, s->height, s->linesize,
2077
s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
2078
block_s, block_s, pix_op,
2079
s->mv[dir][i][0], s->mv[dir][i][1]);
2081
mx += s->mv[dir][i][0];
2082
my += s->mv[dir][i][1];
2085
if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
2086
chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
2090
if (s->picture_structure == PICT_FRAME) {
2092
mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2093
1, 0, s->field_select[dir][0],
2094
ref_picture, pix_op,
2095
s->mv[dir][0][0], s->mv[dir][0][1],
2098
mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2099
1, 1, s->field_select[dir][1],
2100
ref_picture, pix_op,
2101
s->mv[dir][1][0], s->mv[dir][1][1],
2104
if (s->picture_structure != s->field_select[dir][0] + 1 &&
2105
s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
2106
ref_picture = s->current_picture_ptr->f.data;
2109
mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2110
0, 0, s->field_select[dir][0],
2111
ref_picture, pix_op,
2113
s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
2117
for (i = 0; i < 2; i++) {
2118
uint8_t **ref2picture;
2120
if (s->picture_structure == s->field_select[dir][i] + 1 ||
2121
s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
2122
ref2picture = ref_picture;
2124
ref2picture = s->current_picture_ptr->f.data;
2127
mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2128
0, 0, s->field_select[dir][i],
2129
ref2picture, pix_op,
2130
s->mv[dir][i][0], s->mv[dir][i][1] +
2131
2 * block_s * i, block_s, mb_y >> 1);
2133
dest_y += 2 * block_s * s->linesize;
2134
dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2135
dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2139
if (s->picture_structure == PICT_FRAME) {
2140
for (i = 0; i < 2; i++) {
2142
for (j = 0; j < 2; j++) {
2143
mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2145
ref_picture, pix_op,
2146
s->mv[dir][2 * i + j][0],
2147
s->mv[dir][2 * i + j][1],
2150
pix_op = s->dsp.avg_h264_chroma_pixels_tab;
2153
for (i = 0; i < 2; i++) {
2154
mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2155
0, 0, s->picture_structure != i + 1,
2156
ref_picture, pix_op,
2157
s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
2158
2 * block_s, mb_y >> 1);
2160
// after put we make avg of the same block
2161
pix_op = s->dsp.avg_h264_chroma_pixels_tab;
2163
// opposite parity is always in the same
2164
// frame if this is second field
2165
if (!s->first_field) {
2166
ref_picture = s->current_picture_ptr->f.data;
2177
2030
* find the lowest MB row referenced in the MVs
2179
int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
2032
int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
2181
2034
int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
2182
2035
int my, off, i, mvs;
2184
if (s->picture_structure != PICT_FRAME) goto unhandled;
2037
if (s->picture_structure != PICT_FRAME || s->mcsel)
2186
2040
switch (s->mv_type) {
2187
2041
case MV_TYPE_16X16:
2367
2221
if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2368
2222
if (s->mv_dir & MV_DIR_FORWARD) {
2369
ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
2223
ff_thread_await_progress(&s->last_picture_ptr->f,
2224
ff_MPV_lowest_referenced_row(s, 0),
2371
2227
if (s->mv_dir & MV_DIR_BACKWARD) {
2372
ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
2228
ff_thread_await_progress(&s->next_picture_ptr->f,
2229
ff_MPV_lowest_referenced_row(s, 1),
2377
h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
2379
if (s->mv_dir & MV_DIR_FORWARD) {
2380
MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
2381
op_pix = s->dsp.avg_h264_chroma_pixels_tab;
2383
if (s->mv_dir & MV_DIR_BACKWARD) {
2384
MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
2234
op_qpix= s->me.qpel_put;
2235
if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2236
op_pix = s->dsp.put_pixels_tab;
2387
op_qpix= s->me.qpel_put;
2388
if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2389
op_pix = s->dsp.put_pixels_tab;
2391
op_pix = s->dsp.put_no_rnd_pixels_tab;
2393
if (s->mv_dir & MV_DIR_FORWARD) {
2394
MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
2395
op_pix = s->dsp.avg_pixels_tab;
2396
op_qpix= s->me.qpel_avg;
2398
if (s->mv_dir & MV_DIR_BACKWARD) {
2399
MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
2238
op_pix = s->dsp.put_no_rnd_pixels_tab;
2240
if (s->mv_dir & MV_DIR_FORWARD) {
2241
ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
2242
op_pix = s->dsp.avg_pixels_tab;
2243
op_qpix= s->me.qpel_avg;
2245
if (s->mv_dir & MV_DIR_BACKWARD) {
2246
ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);