~ubuntu-branches/debian/experimental/libav/experimental

« back to all changes in this revision

Viewing changes to libavcodec/mpegvideo_enc.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2014-08-10 09:45:02 UTC
  • mfrom: (1.1.28) (2.1.45 sid)
  • Revision ID: package-import@ubuntu.com-20140810094502-p8pds4kq0zpig5oq
Tags: 6:11~alpha1-1
* New upstream Release v11
  - Fixes Unchecked conversion from double to enum (Closes: #749164)
* Add some post v11_alpha1 patches from upstream
* All SONAMEs bumped because of internal changes, but external API is
  promised to have not changed

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
#include "libavutil/timer.h"
38
38
#include "avcodec.h"
39
39
#include "dct.h"
40
 
#include "dsputil.h"
 
40
#include "idctdsp.h"
41
41
#include "mpeg12.h"
42
42
#include "mpegvideo.h"
43
43
#include "h261.h"
44
44
#include "h263.h"
 
45
#include "mjpegenc_common.h"
45
46
#include "mathops.h"
 
47
#include "mpegutils.h"
46
48
#include "mjpegenc.h"
47
49
#include "msmpeg4.h"
 
50
#include "pixblockdsp.h"
 
51
#include "qpeldsp.h"
48
52
#include "faandct.h"
49
53
#include "thread.h"
50
54
#include "aandcttab.h"
54
58
#include "bytestream.h"
55
59
#include <limits.h>
56
60
 
 
61
#define QUANT_BIAS_SHIFT 8
 
62
 
 
63
#define QMAT_SHIFT_MMX 16
 
64
#define QMAT_SHIFT 22
 
65
 
57
66
static int encode_picture(MpegEncContext *s, int picture_number);
58
67
static int dct_quantize_refine(MpegEncContext *s, int16_t *block, int16_t *weight, int16_t *orig, int n, int qscale);
59
68
static int sse_mb(MpegEncContext *s);
68
77
    { NULL },
69
78
};
70
79
 
71
 
void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64],
 
80
void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64],
72
81
                       uint16_t (*qmat16)[2][64],
73
82
                       const uint16_t *quant_matrix,
74
83
                       int bias, int qmin, int qmax, int intra)
75
84
{
 
85
    FDCTDSPContext *fdsp = &s->fdsp;
76
86
    int qscale;
77
87
    int shift = 0;
78
88
 
79
89
    for (qscale = qmin; qscale <= qmax; qscale++) {
80
90
        int i;
81
 
        if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
82
 
            dsp->fdct == ff_jpeg_fdct_islow_10 ||
83
 
            dsp->fdct == ff_faandct) {
 
91
        if (fdsp->fdct == ff_jpeg_fdct_islow_8  ||
 
92
            fdsp->fdct == ff_jpeg_fdct_islow_10 ||
 
93
            fdsp->fdct == ff_faandct) {
84
94
            for (i = 0; i < 64; i++) {
85
 
                const int j = dsp->idct_permutation[i];
 
95
                const int j = s->idsp.idct_permutation[i];
86
96
                /* 16 <= qscale * quant_matrix[i] <= 7905
87
97
                 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
88
98
                 *             19952 <=              x  <= 249205026
92
102
                qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
93
103
                                        (qscale * quant_matrix[j]));
94
104
            }
95
 
        } else if (dsp->fdct == ff_fdct_ifast) {
 
105
        } else if (fdsp->fdct == ff_fdct_ifast) {
96
106
            for (i = 0; i < 64; i++) {
97
 
                const int j = dsp->idct_permutation[i];
 
107
                const int j = s->idsp.idct_permutation[i];
98
108
                /* 16 <= qscale * quant_matrix[i] <= 7905
99
109
                 * Assume x = ff_aanscales[i] * qscale * quant_matrix[i]
100
110
                 *             19952 <=              x  <= 249205026
107
117
            }
108
118
        } else {
109
119
            for (i = 0; i < 64; i++) {
110
 
                const int j = dsp->idct_permutation[i];
 
120
                const int j = s->idsp.idct_permutation[i];
111
121
                /* We can safely suppose that 16 <= quant_matrix[i] <= 255
112
122
                 * Assume x = qscale * quant_matrix[i]
113
123
                 * So             16 <=              x  <= 7905
131
141
 
132
142
        for (i = intra; i < 64; i++) {
133
143
            int64_t max = 8191;
134
 
            if (dsp->fdct == ff_fdct_ifast) {
 
144
            if (fdsp->fdct == ff_fdct_ifast) {
135
145
                max = (8191LL * ff_aanscales[i]) >> 14;
136
146
            }
137
147
            while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
227
237
av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
228
238
{
229
239
    MpegEncContext *s = avctx->priv_data;
230
 
    int i, ret;
 
240
    int i, ret, format_supported;
231
241
 
232
242
    MPV_encode_defaults(s);
233
243
 
241
251
        }
242
252
        break;
243
253
    case AV_CODEC_ID_MJPEG:
244
 
        if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
245
 
            avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
246
 
            ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
247
 
              avctx->pix_fmt != AV_PIX_FMT_YUV422P) ||
248
 
             avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
 
254
        format_supported = 0;
 
255
        /* JPEG color space */
 
256
        if (avctx->pix_fmt == AV_PIX_FMT_YUVJ420P ||
 
257
            avctx->pix_fmt == AV_PIX_FMT_YUVJ422P ||
 
258
            (avctx->color_range == AVCOL_RANGE_JPEG &&
 
259
             (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
 
260
              avctx->pix_fmt == AV_PIX_FMT_YUV422P)))
 
261
            format_supported = 1;
 
262
        /* MPEG color space */
 
263
        else if (avctx->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL &&
 
264
                 (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
 
265
                  avctx->pix_fmt == AV_PIX_FMT_YUV422P))
 
266
            format_supported = 1;
 
267
 
 
268
        if (!format_supported) {
249
269
            av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
250
270
            return -1;
251
271
        }
686
706
    if (ARCH_X86)
687
707
        ff_MPV_encode_init_x86(s);
688
708
 
689
 
    s->avctx->coded_frame = &s->current_picture.f;
 
709
    ff_fdctdsp_init(&s->fdsp, avctx);
 
710
    ff_me_cmp_init(&s->mecc, avctx);
 
711
    ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
 
712
    ff_pixblockdsp_init(&s->pdsp, avctx);
 
713
    ff_qpeldsp_init(&s->qdsp);
 
714
 
 
715
    s->avctx->coded_frame = s->current_picture.f;
690
716
 
691
717
    if (s->msmpeg4_version) {
692
718
        FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
724
750
 
725
751
    s->quant_precision = 5;
726
752
 
727
 
    ff_set_cmp(&s->dsp, s->dsp.ildct_cmp, s->avctx->ildct_cmp);
728
 
    ff_set_cmp(&s->dsp, s->dsp.frame_skip_cmp, s->avctx->frame_skip_cmp);
 
753
    ff_set_cmp(&s->mecc, s->mecc.ildct_cmp,      s->avctx->ildct_cmp);
 
754
    ff_set_cmp(&s->mecc, s->mecc.frame_skip_cmp, s->avctx->frame_skip_cmp);
729
755
 
730
756
    if (CONFIG_H261_ENCODER && s->out_format == FMT_H261)
731
757
        ff_h261_encode_init(s);
739
765
 
740
766
    /* init q matrix */
741
767
    for (i = 0; i < 64; i++) {
742
 
        int j = s->dsp.idct_permutation[i];
 
768
        int j = s->idsp.idct_permutation[i];
743
769
        if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
744
770
            s->mpeg_quant) {
745
771
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
761
787
    /* precompute matrix */
762
788
    /* for mjpeg, we do include qscale in the matrix */
763
789
    if (s->out_format != FMT_MJPEG) {
764
 
        ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
 
790
        ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
765
791
                          s->intra_matrix, s->intra_quant_bias, avctx->qmin,
766
792
                          31, 1);
767
 
        ff_convert_matrix(&s->dsp, s->q_inter_matrix, s->q_inter_matrix16,
 
793
        ff_convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16,
768
794
                          s->inter_matrix, s->inter_quant_bias, avctx->qmin,
769
795
                          31, 0);
770
796
    }
779
805
    FF_ENABLE_DEPRECATION_WARNINGS;
780
806
#endif
781
807
 
 
808
#if FF_API_NORMALIZE_AQP
 
809
    FF_DISABLE_DEPRECATION_WARNINGS
 
810
    if (avctx->flags & CODEC_FLAG_NORMALIZE_AQP)
 
811
        s->mpv_flags |= FF_MPV_FLAG_NAQ;
 
812
    FF_ENABLE_DEPRECATION_WARNINGS;
 
813
#endif
 
814
 
 
815
#if FF_API_MV0
 
816
    FF_DISABLE_DEPRECATION_WARNINGS
 
817
    if (avctx->flags & CODEC_FLAG_MV0)
 
818
        s->mpv_flags |= FF_MPV_FLAG_MV0;
 
819
    FF_ENABLE_DEPRECATION_WARNINGS
 
820
#endif
 
821
 
782
822
    if (avctx->b_frame_strategy == 2) {
783
823
        for (i = 0; i < s->max_b_frames + 2; i++) {
784
824
            s->tmp_frames[i] = av_frame_alloc();
861
901
    for (y = 0; y < h; y += 16) {
862
902
        for (x = 0; x < w; x += 16) {
863
903
            int offset = x + y * stride;
864
 
            int sad  = s->dsp.sad[0](NULL, src + offset, ref + offset, stride,
865
 
                                     16);
866
 
            int mean = (s->dsp.pix_sum(src + offset, stride) + 128) >> 8;
 
904
            int sad  = s->mecc.sad[0](NULL, src + offset, ref + offset,
 
905
                                      stride, 16);
 
906
            int mean = (s->mpvencdsp.pix_sum(src + offset, stride) + 128) >> 8;
867
907
            int sae  = get_sae(src + offset, mean, stride);
868
908
 
869
909
            acc += sae + 500 < sad;
936
976
            pic = &s->picture[i];
937
977
            pic->reference = 3;
938
978
 
939
 
            if ((ret = av_frame_ref(&pic->f, pic_arg)) < 0)
 
979
            if ((ret = av_frame_ref(pic->f, pic_arg)) < 0)
940
980
                return ret;
941
981
            if (ff_alloc_picture(s, pic, 1) < 0) {
942
982
                return -1;
953
993
                return -1;
954
994
            }
955
995
 
956
 
            if (pic->f.data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
957
 
                pic->f.data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
958
 
                pic->f.data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
 
996
            if (pic->f->data[0] + INPLACE_OFFSET == pic_arg->data[0] &&
 
997
                pic->f->data[1] + INPLACE_OFFSET == pic_arg->data[1] &&
 
998
                pic->f->data[2] + INPLACE_OFFSET == pic_arg->data[2]) {
959
999
                // empty
960
1000
            } else {
961
1001
                int h_chroma_shift, v_chroma_shift;
971
1011
                    int w = s->width  >> h_shift;
972
1012
                    int h = s->height >> v_shift;
973
1013
                    uint8_t *src = pic_arg->data[i];
974
 
                    uint8_t *dst = pic->f.data[i];
 
1014
                    uint8_t *dst = pic->f->data[i];
975
1015
 
976
1016
                    if (!s->avctx->rc_buffer_size)
977
1017
                        dst += INPLACE_OFFSET;
988
1028
                }
989
1029
            }
990
1030
        }
991
 
        ret = av_frame_copy_props(&pic->f, pic_arg);
 
1031
        ret = av_frame_copy_props(pic->f, pic_arg);
992
1032
        if (ret < 0)
993
1033
            return ret;
994
1034
 
995
 
        pic->f.display_picture_number = display_picture_number;
996
 
        pic->f.pts = pts; // we set this here to avoid modifiying pic_arg
 
1035
        pic->f->display_picture_number = display_picture_number;
 
1036
        pic->f->pts = pts; // we set this here to avoid modifiying pic_arg
997
1037
    }
998
1038
 
999
1039
    /* shift buffer entries */
1012
1052
    int64_t score64 = 0;
1013
1053
 
1014
1054
    for (plane = 0; plane < 3; plane++) {
1015
 
        const int stride = p->f.linesize[plane];
 
1055
        const int stride = p->f->linesize[plane];
1016
1056
        const int bw = plane ? 1 : 2;
1017
1057
        for (y = 0; y < s->mb_height * bw; y++) {
1018
1058
            for (x = 0; x < s->mb_width * bw; x++) {
1019
1059
                int off = p->shared ? 0 : 16;
1020
 
                uint8_t *dptr = p->f.data[plane] + 8 * (x + y * stride) + off;
1021
 
                uint8_t *rptr = ref->f.data[plane] + 8 * (x + y * stride);
1022
 
                int v   = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
 
1060
                uint8_t *dptr = p->f->data[plane] + 8 * (x + y * stride) + off;
 
1061
                uint8_t *rptr = ref->f->data[plane] + 8 * (x + y * stride);
 
1062
                int v = s->mecc.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1023
1063
 
1024
1064
                switch (s->avctx->frame_skip_exp) {
1025
1065
                case 0: score    =  FFMAX(score, v);          break;
1080
1120
 
1081
1121
    c->width        = s->width  >> scale;
1082
1122
    c->height       = s->height >> scale;
1083
 
    c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR |
1084
 
                      CODEC_FLAG_INPUT_PRESERVED;
 
1123
    c->flags        = CODEC_FLAG_QSCALE | CODEC_FLAG_PSNR;
1085
1124
    c->flags       |= s->avctx->flags & CODEC_FLAG_QPEL;
1086
1125
    c->mb_decision  = s->avctx->mb_decision;
1087
1126
    c->me_cmp       = s->avctx->me_cmp;
1102
1141
            pre_input = *pre_input_ptr;
1103
1142
 
1104
1143
            if (!pre_input.shared && i) {
1105
 
                pre_input.f.data[0] += INPLACE_OFFSET;
1106
 
                pre_input.f.data[1] += INPLACE_OFFSET;
1107
 
                pre_input.f.data[2] += INPLACE_OFFSET;
 
1144
                pre_input.f->data[0] += INPLACE_OFFSET;
 
1145
                pre_input.f->data[1] += INPLACE_OFFSET;
 
1146
                pre_input.f->data[2] += INPLACE_OFFSET;
1108
1147
            }
1109
1148
 
1110
 
            s->dsp.shrink[scale](s->tmp_frames[i]->data[0], s->tmp_frames[i]->linesize[0],
1111
 
                                 pre_input.f.data[0], pre_input.f.linesize[0],
1112
 
                                 c->width,      c->height);
1113
 
            s->dsp.shrink[scale](s->tmp_frames[i]->data[1], s->tmp_frames[i]->linesize[1],
1114
 
                                 pre_input.f.data[1], pre_input.f.linesize[1],
1115
 
                                 c->width >> 1, c->height >> 1);
1116
 
            s->dsp.shrink[scale](s->tmp_frames[i]->data[2], s->tmp_frames[i]->linesize[2],
1117
 
                                 pre_input.f.data[2], pre_input.f.linesize[2],
1118
 
                                 c->width >> 1, c->height >> 1);
 
1149
            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[0],
 
1150
                                       s->tmp_frames[i]->linesize[0],
 
1151
                                       pre_input.f->data[0],
 
1152
                                       pre_input.f->linesize[0],
 
1153
                                       c->width, c->height);
 
1154
            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[1],
 
1155
                                       s->tmp_frames[i]->linesize[1],
 
1156
                                       pre_input.f->data[1],
 
1157
                                       pre_input.f->linesize[1],
 
1158
                                       c->width >> 1, c->height >> 1);
 
1159
            s->mpvencdsp.shrink[scale](s->tmp_frames[i]->data[2],
 
1160
                                       s->tmp_frames[i]->linesize[2],
 
1161
                                       pre_input.f->data[2],
 
1162
                                       pre_input.f->linesize[2],
 
1163
                                       c->width >> 1, c->height >> 1);
1119
1164
        }
1120
1165
    }
1121
1166
 
1179
1224
        if (/*s->picture_in_gop_number >= s->gop_size ||*/
1180
1225
            s->next_picture_ptr == NULL || s->intra_only) {
1181
1226
            s->reordered_input_picture[0] = s->input_picture[0];
1182
 
            s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_I;
1183
 
            s->reordered_input_picture[0]->f.coded_picture_number =
 
1227
            s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_I;
 
1228
            s->reordered_input_picture[0]->f->coded_picture_number =
1184
1229
                s->coded_picture_number++;
1185
1230
        } else {
1186
1231
            int b_frames;
1189
1234
                if (s->picture_in_gop_number < s->gop_size &&
1190
1235
                    skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1191
1236
                    // FIXME check that te gop check above is +-1 correct
1192
 
                    av_frame_unref(&s->input_picture[0]->f);
 
1237
                    av_frame_unref(s->input_picture[0]->f);
1193
1238
 
1194
1239
                    emms_c();
1195
1240
                    ff_vbv_update(s, 0);
1200
1245
 
1201
1246
            if (s->flags & CODEC_FLAG_PASS2) {
1202
1247
                for (i = 0; i < s->max_b_frames + 1; i++) {
1203
 
                    int pict_num = s->input_picture[0]->f.display_picture_number + i;
 
1248
                    int pict_num = s->input_picture[0]->f->display_picture_number + i;
1204
1249
 
1205
1250
                    if (pict_num >= s->rc_context.num_entries)
1206
1251
                        break;
1209
1254
                        break;
1210
1255
                    }
1211
1256
 
1212
 
                    s->input_picture[i]->f.pict_type =
 
1257
                    s->input_picture[i]->f->pict_type =
1213
1258
                        s->rc_context.entry[pict_num].new_pict_type;
1214
1259
                }
1215
1260
            }
1224
1269
                        s->input_picture[i]->b_frame_score == 0) {
1225
1270
                        s->input_picture[i]->b_frame_score =
1226
1271
                            get_intra_count(s,
1227
 
                                            s->input_picture[i    ]->f.data[0],
1228
 
                                            s->input_picture[i - 1]->f.data[0],
 
1272
                                            s->input_picture[i    ]->f->data[0],
 
1273
                                            s->input_picture[i - 1]->f->data[0],
1229
1274
                                            s->linesize) + 1;
1230
1275
                    }
1231
1276
                }
1252
1297
            emms_c();
1253
1298
 
1254
1299
            for (i = b_frames - 1; i >= 0; i--) {
1255
 
                int type = s->input_picture[i]->f.pict_type;
 
1300
                int type = s->input_picture[i]->f->pict_type;
1256
1301
                if (type && type != AV_PICTURE_TYPE_B)
1257
1302
                    b_frames = i;
1258
1303
            }
1259
 
            if (s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_B &&
 
1304
            if (s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_B &&
1260
1305
                b_frames == s->max_b_frames) {
1261
1306
                av_log(s->avctx, AV_LOG_ERROR,
1262
1307
                       "warning, too many b frames in a row\n");
1269
1314
                } else {
1270
1315
                    if (s->flags & CODEC_FLAG_CLOSED_GOP)
1271
1316
                        b_frames = 0;
1272
 
                    s->input_picture[b_frames]->f.pict_type = AV_PICTURE_TYPE_I;
 
1317
                    s->input_picture[b_frames]->f->pict_type = AV_PICTURE_TYPE_I;
1273
1318
                }
1274
1319
            }
1275
1320
 
1276
1321
            if ((s->flags & CODEC_FLAG_CLOSED_GOP) && b_frames &&
1277
 
                s->input_picture[b_frames]->f.pict_type == AV_PICTURE_TYPE_I)
 
1322
                s->input_picture[b_frames]->f->pict_type == AV_PICTURE_TYPE_I)
1278
1323
                b_frames--;
1279
1324
 
1280
1325
            s->reordered_input_picture[0] = s->input_picture[b_frames];
1281
 
            if (s->reordered_input_picture[0]->f.pict_type != AV_PICTURE_TYPE_I)
1282
 
                s->reordered_input_picture[0]->f.pict_type = AV_PICTURE_TYPE_P;
1283
 
            s->reordered_input_picture[0]->f.coded_picture_number =
 
1326
            if (s->reordered_input_picture[0]->f->pict_type != AV_PICTURE_TYPE_I)
 
1327
                s->reordered_input_picture[0]->f->pict_type = AV_PICTURE_TYPE_P;
 
1328
            s->reordered_input_picture[0]->f->coded_picture_number =
1284
1329
                s->coded_picture_number++;
1285
1330
            for (i = 0; i < b_frames; i++) {
1286
1331
                s->reordered_input_picture[i + 1] = s->input_picture[i];
1287
 
                s->reordered_input_picture[i + 1]->f.pict_type =
 
1332
                s->reordered_input_picture[i + 1]->f->pict_type =
1288
1333
                    AV_PICTURE_TYPE_B;
1289
 
                s->reordered_input_picture[i + 1]->f.coded_picture_number =
 
1334
                s->reordered_input_picture[i + 1]->f->coded_picture_number =
1290
1335
                    s->coded_picture_number++;
1291
1336
            }
1292
1337
        }
1294
1339
no_output_pic:
1295
1340
    if (s->reordered_input_picture[0]) {
1296
1341
        s->reordered_input_picture[0]->reference =
1297
 
           s->reordered_input_picture[0]->f.pict_type !=
 
1342
           s->reordered_input_picture[0]->f->pict_type !=
1298
1343
               AV_PICTURE_TYPE_B ? 3 : 0;
1299
1344
 
1300
1345
        ff_mpeg_unref_picture(s, &s->new_picture);
1316
1361
                return -1;
1317
1362
            }
1318
1363
 
1319
 
            ret = av_frame_copy_props(&pic->f, &s->reordered_input_picture[0]->f);
 
1364
            ret = av_frame_copy_props(pic->f, s->reordered_input_picture[0]->f);
1320
1365
            if (ret < 0)
1321
1366
                return ret;
1322
1367
 
1323
1368
            /* mark us unused / free shared pic */
1324
 
            av_frame_unref(&s->reordered_input_picture[0]->f);
 
1369
            av_frame_unref(s->reordered_input_picture[0]->f);
1325
1370
            s->reordered_input_picture[0]->shared = 0;
1326
1371
 
1327
1372
            s->current_picture_ptr = pic;
1329
1374
            // input is not a shared pix -> reuse buffer for current_pix
1330
1375
            s->current_picture_ptr = s->reordered_input_picture[0];
1331
1376
            for (i = 0; i < 4; i++) {
1332
 
                s->new_picture.f.data[i] += INPLACE_OFFSET;
 
1377
                s->new_picture.f->data[i] += INPLACE_OFFSET;
1333
1378
            }
1334
1379
        }
1335
1380
        ff_mpeg_unref_picture(s, &s->current_picture);
1337
1382
                                       s->current_picture_ptr)) < 0)
1338
1383
            return ret;
1339
1384
 
1340
 
        s->picture_number = s->new_picture.f.display_picture_number;
 
1385
        s->picture_number = s->new_picture.f->display_picture_number;
1341
1386
    } else {
1342
1387
        ff_mpeg_unref_picture(s, &s->new_picture);
1343
1388
    }
1354
1399
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
1355
1400
        int hshift = desc->log2_chroma_w;
1356
1401
        int vshift = desc->log2_chroma_h;
1357
 
        s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
1358
 
                          s->h_edge_pos, s->v_edge_pos,
1359
 
                          EDGE_WIDTH, EDGE_WIDTH,
1360
 
                          EDGE_TOP | EDGE_BOTTOM);
1361
 
        s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
1362
 
                          s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1363
 
                          EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1364
 
                          EDGE_TOP | EDGE_BOTTOM);
1365
 
        s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
1366
 
                          s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1367
 
                          EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1368
 
                          EDGE_TOP | EDGE_BOTTOM);
 
1402
        s->mpvencdsp.draw_edges(s->current_picture.f->data[0], s->linesize,
 
1403
                                s->h_edge_pos, s->v_edge_pos,
 
1404
                                EDGE_WIDTH, EDGE_WIDTH,
 
1405
                                EDGE_TOP | EDGE_BOTTOM);
 
1406
        s->mpvencdsp.draw_edges(s->current_picture.f->data[1], s->uvlinesize,
 
1407
                                s->h_edge_pos >> hshift,
 
1408
                                s->v_edge_pos >> vshift,
 
1409
                                EDGE_WIDTH >> hshift,
 
1410
                                EDGE_WIDTH >> vshift,
 
1411
                                EDGE_TOP | EDGE_BOTTOM);
 
1412
        s->mpvencdsp.draw_edges(s->current_picture.f->data[2], s->uvlinesize,
 
1413
                                s->h_edge_pos >> hshift,
 
1414
                                s->v_edge_pos >> vshift,
 
1415
                                EDGE_WIDTH >> hshift,
 
1416
                                EDGE_WIDTH >> vshift,
 
1417
                                EDGE_TOP | EDGE_BOTTOM);
1369
1418
    }
1370
1419
 
1371
1420
    emms_c();
1372
1421
 
1373
1422
    s->last_pict_type                 = s->pict_type;
1374
 
    s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
 
1423
    s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f->quality;
1375
1424
    if (s->pict_type!= AV_PICTURE_TYPE_B)
1376
1425
        s->last_non_b_pict_type = s->pict_type;
1377
1426
 
1383
1432
        }
1384
1433
    }
1385
1434
 
1386
 
    s->avctx->coded_frame = &s->current_picture_ptr->f;
 
1435
    s->avctx->coded_frame = s->current_picture_ptr->f;
1387
1436
 
1388
1437
}
1389
1438
 
1415
1464
    /* mark & release old frames */
1416
1465
    if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1417
1466
        s->last_picture_ptr != s->next_picture_ptr &&
1418
 
        s->last_picture_ptr->f.buf[0]) {
 
1467
        s->last_picture_ptr->f->buf[0]) {
1419
1468
        ff_mpeg_unref_picture(s, s->last_picture_ptr);
1420
1469
    }
1421
1470
 
1422
 
    s->current_picture_ptr->f.pict_type = s->pict_type;
1423
 
    s->current_picture_ptr->f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
 
1471
    s->current_picture_ptr->f->pict_type = s->pict_type;
 
1472
    s->current_picture_ptr->f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
1424
1473
 
1425
1474
    ff_mpeg_unref_picture(s, &s->current_picture);
1426
1475
    if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1435
1484
 
1436
1485
    if (s->last_picture_ptr) {
1437
1486
        ff_mpeg_unref_picture(s, &s->last_picture);
1438
 
        if (s->last_picture_ptr->f.buf[0] &&
 
1487
        if (s->last_picture_ptr->f->buf[0] &&
1439
1488
            (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1440
1489
                                       s->last_picture_ptr)) < 0)
1441
1490
            return ret;
1442
1491
    }
1443
1492
    if (s->next_picture_ptr) {
1444
1493
        ff_mpeg_unref_picture(s, &s->next_picture);
1445
 
        if (s->next_picture_ptr->f.buf[0] &&
 
1494
        if (s->next_picture_ptr->f->buf[0] &&
1446
1495
            (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1447
1496
                                       s->next_picture_ptr)) < 0)
1448
1497
            return ret;
1452
1501
        int i;
1453
1502
        for (i = 0; i < 4; i++) {
1454
1503
            if (s->picture_structure == PICT_BOTTOM_FIELD) {
1455
 
                s->current_picture.f.data[i] +=
1456
 
                    s->current_picture.f.linesize[i];
 
1504
                s->current_picture.f->data[i] +=
 
1505
                    s->current_picture.f->linesize[i];
1457
1506
            }
1458
 
            s->current_picture.f.linesize[i] *= 2;
1459
 
            s->last_picture.f.linesize[i]    *= 2;
1460
 
            s->next_picture.f.linesize[i]    *= 2;
 
1507
            s->current_picture.f->linesize[i] *= 2;
 
1508
            s->last_picture.f->linesize[i]    *= 2;
 
1509
            s->next_picture.f->linesize[i]    *= 2;
1461
1510
        }
1462
1511
    }
1463
1512
 
1497
1546
    }
1498
1547
 
1499
1548
    /* output? */
1500
 
    if (s->new_picture.f.data[0]) {
 
1549
    if (s->new_picture.f->data[0]) {
1501
1550
        if (!pkt->data &&
1502
1551
            (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
1503
1552
            return ret;
1518
1567
            init_put_bits(&s->thread_context[i]->pb, start, end - start);
1519
1568
        }
1520
1569
 
1521
 
        s->pict_type = s->new_picture.f.pict_type;
 
1570
        s->pict_type = s->new_picture.f->pict_type;
1522
1571
        //emms_c();
1523
1572
        ret = frame_start(s);
1524
1573
        if (ret < 0)
1584
1633
            ff_write_pass1_stats(s);
1585
1634
 
1586
1635
        for (i = 0; i < 4; i++) {
1587
 
            s->current_picture_ptr->f.error[i] = s->current_picture.f.error[i];
1588
 
            avctx->error[i] += s->current_picture_ptr->f.error[i];
 
1636
            s->current_picture_ptr->f->error[i] = s->current_picture.f->error[i];
 
1637
            avctx->error[i] += s->current_picture_ptr->f->error[i];
1589
1638
        }
1590
1639
 
1591
1640
        if (s->flags & CODEC_FLAG_PASS1)
1662
1711
        s->total_bits     += s->frame_bits;
1663
1712
        avctx->frame_bits  = s->frame_bits;
1664
1713
 
1665
 
        pkt->pts = s->current_picture.f.pts;
1666
 
        if (!s->low_delay) {
1667
 
            if (!s->current_picture.f.coded_picture_number)
 
1714
        pkt->pts = s->current_picture.f->pts;
 
1715
        if (!s->low_delay && s->pict_type != AV_PICTURE_TYPE_B) {
 
1716
            if (!s->current_picture.f->coded_picture_number)
1668
1717
                pkt->dts = pkt->pts - s->dts_delta;
1669
1718
            else
1670
1719
                pkt->dts = s->reordered_pts;
1671
 
            s->reordered_pts = s->input_picture[0]->f.pts;
 
1720
            s->reordered_pts = pkt->pts;
1672
1721
        } else
1673
1722
            pkt->dts = pkt->pts;
1674
 
        if (s->current_picture.f.key_frame)
 
1723
        if (s->current_picture.f->key_frame)
1675
1724
            pkt->flags |= AV_PKT_FLAG_KEY;
1676
1725
        if (s->mb_info)
1677
1726
            av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1849
1898
 
1850
1899
    wrap_y = s->linesize;
1851
1900
    wrap_c = s->uvlinesize;
1852
 
    ptr_y  = s->new_picture.f.data[0] +
 
1901
    ptr_y  = s->new_picture.f->data[0] +
1853
1902
             (mb_y * 16 * wrap_y)              + mb_x * 16;
1854
 
    ptr_cb = s->new_picture.f.data[1] +
 
1903
    ptr_cb = s->new_picture.f->data[1] +
1855
1904
             (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1856
 
    ptr_cr = s->new_picture.f.data[2] +
 
1905
    ptr_cr = s->new_picture.f->data[2] +
1857
1906
             (mb_y * mb_block_height * wrap_c) + mb_x * 8;
1858
1907
 
1859
1908
    if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
1880
1929
            int progressive_score, interlaced_score;
1881
1930
 
1882
1931
            s->interlaced_dct = 0;
1883
 
            progressive_score = s->dsp.ildct_cmp[4](s, ptr_y,
1884
 
                                                    NULL, wrap_y, 8) +
1885
 
                                s->dsp.ildct_cmp[4](s, ptr_y + wrap_y * 8,
1886
 
                                                    NULL, wrap_y, 8) - 400;
 
1932
            progressive_score = s->mecc.ildct_cmp[4](s, ptr_y, NULL, wrap_y, 8) +
 
1933
                                s->mecc.ildct_cmp[4](s, ptr_y + wrap_y * 8,
 
1934
                                                     NULL, wrap_y, 8) - 400;
1887
1935
 
1888
1936
            if (progressive_score > 0) {
1889
 
                interlaced_score = s->dsp.ildct_cmp[4](s, ptr_y,
1890
 
                                                       NULL, wrap_y * 2, 8) +
1891
 
                                   s->dsp.ildct_cmp[4](s, ptr_y + wrap_y,
1892
 
                                                       NULL, wrap_y * 2, 8);
 
1937
                interlaced_score = s->mecc.ildct_cmp[4](s, ptr_y,
 
1938
                                                        NULL, wrap_y * 2, 8) +
 
1939
                                   s->mecc.ildct_cmp[4](s, ptr_y + wrap_y,
 
1940
                                                        NULL, wrap_y * 2, 8);
1893
1941
                if (progressive_score > interlaced_score) {
1894
1942
                    s->interlaced_dct = 1;
1895
1943
 
1901
1949
            }
1902
1950
        }
1903
1951
 
1904
 
        s->dsp.get_pixels(s->block[0], ptr_y                  , wrap_y);
1905
 
        s->dsp.get_pixels(s->block[1], ptr_y              + 8 , wrap_y);
1906
 
        s->dsp.get_pixels(s->block[2], ptr_y + dct_offset     , wrap_y);
1907
 
        s->dsp.get_pixels(s->block[3], ptr_y + dct_offset + 8 , wrap_y);
 
1952
        s->pdsp.get_pixels(s->block[0], ptr_y,                  wrap_y);
 
1953
        s->pdsp.get_pixels(s->block[1], ptr_y + 8,              wrap_y);
 
1954
        s->pdsp.get_pixels(s->block[2], ptr_y + dct_offset,     wrap_y);
 
1955
        s->pdsp.get_pixels(s->block[3], ptr_y + dct_offset + 8, wrap_y);
1908
1956
 
1909
1957
        if (s->flags & CODEC_FLAG_GRAY) {
1910
1958
            skip_dct[4] = 1;
1911
1959
            skip_dct[5] = 1;
1912
1960
        } else {
1913
 
            s->dsp.get_pixels(s->block[4], ptr_cb, wrap_c);
1914
 
            s->dsp.get_pixels(s->block[5], ptr_cr, wrap_c);
 
1961
            s->pdsp.get_pixels(s->block[4], ptr_cb, wrap_c);
 
1962
            s->pdsp.get_pixels(s->block[5], ptr_cr, wrap_c);
1915
1963
            if (!s->chroma_y_shift) { /* 422 */
1916
 
                s->dsp.get_pixels(s->block[6],
1917
 
                                  ptr_cb + (dct_offset >> 1), wrap_c);
1918
 
                s->dsp.get_pixels(s->block[7],
1919
 
                                  ptr_cr + (dct_offset >> 1), wrap_c);
 
1964
                s->pdsp.get_pixels(s->block[6],
 
1965
                                   ptr_cb + (dct_offset >> 1), wrap_c);
 
1966
                s->pdsp.get_pixels(s->block[7],
 
1967
                                   ptr_cr + (dct_offset >> 1), wrap_c);
1920
1968
            }
1921
1969
        }
1922
1970
    } else {
1930
1978
 
1931
1979
        if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
1932
1980
            op_pix  = s->hdsp.put_pixels_tab;
1933
 
            op_qpix = s->dsp.put_qpel_pixels_tab;
 
1981
            op_qpix = s->qdsp.put_qpel_pixels_tab;
1934
1982
        } else {
1935
1983
            op_pix  = s->hdsp.put_no_rnd_pixels_tab;
1936
 
            op_qpix = s->dsp.put_no_rnd_qpel_pixels_tab;
 
1984
            op_qpix = s->qdsp.put_no_rnd_qpel_pixels_tab;
1937
1985
        }
1938
1986
 
1939
1987
        if (s->mv_dir & MV_DIR_FORWARD) {
1940
1988
            ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0,
1941
 
                          s->last_picture.f.data,
 
1989
                          s->last_picture.f->data,
1942
1990
                          op_pix, op_qpix);
1943
1991
            op_pix  = s->hdsp.avg_pixels_tab;
1944
 
            op_qpix = s->dsp.avg_qpel_pixels_tab;
 
1992
            op_qpix = s->qdsp.avg_qpel_pixels_tab;
1945
1993
        }
1946
1994
        if (s->mv_dir & MV_DIR_BACKWARD) {
1947
1995
            ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1,
1948
 
                          s->next_picture.f.data,
 
1996
                          s->next_picture.f->data,
1949
1997
                          op_pix, op_qpix);
1950
1998
        }
1951
1999
 
1953
2001
            int progressive_score, interlaced_score;
1954
2002
 
1955
2003
            s->interlaced_dct = 0;
1956
 
            progressive_score = s->dsp.ildct_cmp[0](s, dest_y,
1957
 
                                                    ptr_y,              wrap_y,
1958
 
                                                    8) +
1959
 
                                s->dsp.ildct_cmp[0](s, dest_y + wrap_y * 8,
1960
 
                                                    ptr_y + wrap_y * 8, wrap_y,
1961
 
                                                    8) - 400;
 
2004
            progressive_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y, wrap_y, 8) +
 
2005
                                s->mecc.ildct_cmp[0](s, dest_y + wrap_y * 8,
 
2006
                                                     ptr_y + wrap_y * 8,
 
2007
                                                     wrap_y, 8) - 400;
1962
2008
 
1963
2009
            if (s->avctx->ildct_cmp == FF_CMP_VSSE)
1964
2010
                progressive_score -= 400;
1965
2011
 
1966
2012
            if (progressive_score > 0) {
1967
 
                interlaced_score = s->dsp.ildct_cmp[0](s, dest_y,
1968
 
                                                       ptr_y,
1969
 
                                                       wrap_y * 2, 8) +
1970
 
                                   s->dsp.ildct_cmp[0](s, dest_y + wrap_y,
1971
 
                                                       ptr_y + wrap_y,
1972
 
                                                       wrap_y * 2, 8);
 
2013
                interlaced_score = s->mecc.ildct_cmp[0](s, dest_y, ptr_y,
 
2014
                                                        wrap_y * 2, 8) +
 
2015
                                   s->mecc.ildct_cmp[0](s, dest_y + wrap_y,
 
2016
                                                        ptr_y + wrap_y,
 
2017
                                                        wrap_y * 2, 8);
1973
2018
 
1974
2019
                if (progressive_score > interlaced_score) {
1975
2020
                    s->interlaced_dct = 1;
1982
2027
            }
1983
2028
        }
1984
2029
 
1985
 
        s->dsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
1986
 
        s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
1987
 
        s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset,
1988
 
                           dest_y + dct_offset, wrap_y);
1989
 
        s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
1990
 
                           dest_y + dct_offset + 8, wrap_y);
 
2030
        s->pdsp.diff_pixels(s->block[0], ptr_y, dest_y, wrap_y);
 
2031
        s->pdsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y);
 
2032
        s->pdsp.diff_pixels(s->block[2], ptr_y + dct_offset,
 
2033
                            dest_y + dct_offset, wrap_y);
 
2034
        s->pdsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8,
 
2035
                            dest_y + dct_offset + 8, wrap_y);
1991
2036
 
1992
2037
        if (s->flags & CODEC_FLAG_GRAY) {
1993
2038
            skip_dct[4] = 1;
1994
2039
            skip_dct[5] = 1;
1995
2040
        } else {
1996
 
            s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
1997
 
            s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
 
2041
            s->pdsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c);
 
2042
            s->pdsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c);
1998
2043
            if (!s->chroma_y_shift) { /* 422 */
1999
 
                s->dsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
2000
 
                                   dest_cb + (dct_offset >> 1), wrap_c);
2001
 
                s->dsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
2002
 
                                   dest_cr + (dct_offset >> 1), wrap_c);
 
2044
                s->pdsp.diff_pixels(s->block[6], ptr_cb + (dct_offset >> 1),
 
2045
                                    dest_cb + (dct_offset >> 1), wrap_c);
 
2046
                s->pdsp.diff_pixels(s->block[7], ptr_cr + (dct_offset >> 1),
 
2047
                                    dest_cr + (dct_offset >> 1), wrap_c);
2003
2048
            }
2004
2049
        }
2005
2050
        /* pre quantization */
2006
2051
        if (s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] <
2007
2052
                2 * s->qscale * s->qscale) {
2008
2053
            // FIXME optimize
2009
 
            if (s->dsp.sad[1](NULL, ptr_y , dest_y,
2010
 
                              wrap_y, 8) < 20 * s->qscale)
 
2054
            if (s->mecc.sad[1](NULL, ptr_y, dest_y, wrap_y, 8) < 20 * s->qscale)
2011
2055
                skip_dct[0] = 1;
2012
 
            if (s->dsp.sad[1](NULL, ptr_y + 8,
2013
 
                              dest_y + 8, wrap_y, 8) < 20 * s->qscale)
 
2056
            if (s->mecc.sad[1](NULL, ptr_y + 8, dest_y + 8, wrap_y, 8) < 20 * s->qscale)
2014
2057
                skip_dct[1] = 1;
2015
 
            if (s->dsp.sad[1](NULL, ptr_y + dct_offset,
2016
 
                              dest_y + dct_offset, wrap_y, 8) < 20 * s->qscale)
 
2058
            if (s->mecc.sad[1](NULL, ptr_y + dct_offset, dest_y + dct_offset,
 
2059
                               wrap_y, 8) < 20 * s->qscale)
2017
2060
                skip_dct[2] = 1;
2018
 
            if (s->dsp.sad[1](NULL, ptr_y + dct_offset + 8,
2019
 
                              dest_y + dct_offset + 8,
2020
 
                              wrap_y, 8) < 20 * s->qscale)
 
2061
            if (s->mecc.sad[1](NULL, ptr_y + dct_offset + 8, dest_y + dct_offset + 8,
 
2062
                               wrap_y, 8) < 20 * s->qscale)
2021
2063
                skip_dct[3] = 1;
2022
 
            if (s->dsp.sad[1](NULL, ptr_cb, dest_cb,
2023
 
                              wrap_c, 8) < 20 * s->qscale)
 
2064
            if (s->mecc.sad[1](NULL, ptr_cb, dest_cb, wrap_c, 8) < 20 * s->qscale)
2024
2065
                skip_dct[4] = 1;
2025
 
            if (s->dsp.sad[1](NULL, ptr_cr, dest_cr,
2026
 
                              wrap_c, 8) < 20 * s->qscale)
 
2066
            if (s->mecc.sad[1](NULL, ptr_cr, dest_cr, wrap_c, 8) < 20 * s->qscale)
2027
2067
                skip_dct[5] = 1;
2028
2068
            if (!s->chroma_y_shift) { /* 422 */
2029
 
                if (s->dsp.sad[1](NULL, ptr_cb + (dct_offset >> 1),
2030
 
                                  dest_cb + (dct_offset >> 1),
2031
 
                                  wrap_c, 8) < 20 * s->qscale)
 
2069
                if (s->mecc.sad[1](NULL, ptr_cb + (dct_offset >> 1),
 
2070
                                   dest_cb + (dct_offset >> 1),
 
2071
                                   wrap_c, 8) < 20 * s->qscale)
2032
2072
                    skip_dct[6] = 1;
2033
 
                if (s->dsp.sad[1](NULL, ptr_cr + (dct_offset >> 1),
2034
 
                                  dest_cr + (dct_offset >> 1),
2035
 
                                  wrap_c, 8) < 20 * s->qscale)
 
2073
                if (s->mecc.sad[1](NULL, ptr_cr + (dct_offset >> 1),
 
2074
                                   dest_cr + (dct_offset >> 1),
 
2075
                                   wrap_c, 8) < 20 * s->qscale)
2036
2076
                    skip_dct[7] = 1;
2037
2077
            }
2038
2078
        }
2292
2332
}
2293
2333
 
2294
2334
static int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){
2295
 
    uint32_t *sq = ff_squareTbl + 256;
 
2335
    uint32_t *sq = ff_square_tab + 256;
2296
2336
    int acc=0;
2297
2337
    int x,y;
2298
2338
 
2299
2339
    if(w==16 && h==16)
2300
 
        return s->dsp.sse[0](NULL, src1, src2, stride, 16);
 
2340
        return s->mecc.sse[0](NULL, src1, src2, stride, 16);
2301
2341
    else if(w==8 && h==8)
2302
 
        return s->dsp.sse[1](NULL, src1, src2, stride, 8);
 
2342
        return s->mecc.sse[1](NULL, src1, src2, stride, 8);
2303
2343
 
2304
2344
    for(y=0; y<h; y++){
2305
2345
        for(x=0; x<w; x++){
2321
2361
 
2322
2362
    if(w==16 && h==16)
2323
2363
      if(s->avctx->mb_cmp == FF_CMP_NSSE){
2324
 
        return  s->dsp.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)
2325
 
               +s->dsp.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)
2326
 
               +s->dsp.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);
 
2364
        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) +
 
2365
               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) +
 
2366
               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);
2327
2367
      }else{
2328
 
        return  s->dsp.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)
2329
 
               +s->dsp.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)
2330
 
               +s->dsp.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);
 
2368
        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) +
 
2369
               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) +
 
2370
               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);
2331
2371
      }
2332
2372
    else
2333
 
        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)
2334
 
               +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)
2335
 
               +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);
 
2373
        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)
 
2374
               +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)
 
2375
               +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);
2336
2376
}
2337
2377
 
2338
2378
static int pre_estimate_motion_thread(AVCodecContext *c, void *arg){
2357
2397
static int estimate_motion_thread(AVCodecContext *c, void *arg){
2358
2398
    MpegEncContext *s= *(void**)arg;
2359
2399
 
2360
 
    ff_check_alignment();
2361
 
 
2362
2400
    s->me.dia_size= s->avctx->dia_size;
2363
2401
    s->first_slice_line=1;
2364
2402
    for(s->mb_y= s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2385
2423
    MpegEncContext *s= *(void**)arg;
2386
2424
    int mb_x, mb_y;
2387
2425
 
2388
 
    ff_check_alignment();
2389
 
 
2390
2426
    for(mb_y=s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2391
2427
        for(mb_x=0; mb_x < s->mb_width; mb_x++) {
2392
2428
            int xx = mb_x * 16;
2393
2429
            int yy = mb_y * 16;
2394
 
            uint8_t *pix = s->new_picture.f.data[0] + (yy * s->linesize) + xx;
 
2430
            uint8_t *pix = s->new_picture.f->data[0] + (yy * s->linesize) + xx;
2395
2431
            int varc;
2396
 
            int sum = s->dsp.pix_sum(pix, s->linesize);
 
2432
            int sum = s->mpvencdsp.pix_sum(pix, s->linesize);
2397
2433
 
2398
 
            varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500 + 128)>>8;
 
2434
            varc = (s->mpvencdsp.pix_norm1(pix, s->linesize) -
 
2435
                    (((unsigned) sum * sum) >> 8) + 500 + 128) >> 8;
2399
2436
 
2400
2437
            s->current_picture.mb_var [s->mb_stride * mb_y + mb_x] = varc;
2401
2438
            s->current_picture.mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
2477
2514
    uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2478
2515
    PutBitContext pb[2], pb2[2], tex_pb[2];
2479
2516
 
2480
 
    ff_check_alignment();
2481
 
 
2482
2517
    for(i=0; i<2; i++){
2483
2518
        init_put_bits(&pb    [i], bit_buf    [i], MAX_MB_BYTES);
2484
2519
        init_put_bits(&pb2   [i], bit_buf2   [i], MAX_MB_BYTES);
2500
2535
        /* note: quant matrix value (8) is implied here */
2501
2536
        s->last_dc[i] = 128 << s->intra_dc_precision;
2502
2537
 
2503
 
        s->current_picture.f.error[i] = 0;
 
2538
        s->current_picture.f->error[i] = 0;
2504
2539
    }
2505
2540
    s->mb_skip_run = 0;
2506
2541
    memset(s->last_mv, 0, sizeof(s->last_mv));
3058
3093
                if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
3059
3094
                if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
3060
3095
 
3061
 
                s->current_picture.f.error[0] += sse(
3062
 
                    s, s->new_picture.f.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
 
3096
                s->current_picture.f->error[0] += sse(
 
3097
                    s, s->new_picture.f->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
3063
3098
                    s->dest[0], w, h, s->linesize);
3064
 
                s->current_picture.f.error[1] += sse(
3065
 
                    s, s->new_picture.f.data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
 
3099
                s->current_picture.f->error[1] += sse(
 
3100
                    s, s->new_picture.f->data[1] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3066
3101
                    s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3067
 
                s->current_picture.f.error[2] += sse(
3068
 
                    s, s->new_picture.f.data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
 
3102
                s->current_picture.f->error[2] += sse(
 
3103
                    s, s->new_picture.f->data[2] + s->mb_x*8  + s->mb_y*s->uvlinesize*chr_h,
3069
3104
                    s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
3070
3105
            }
3071
3106
            if(s->loop_filter){
3117
3152
    MERGE(misc_bits);
3118
3153
    MERGE(er.error_count);
3119
3154
    MERGE(padding_bug_score);
3120
 
    MERGE(current_picture.f.error[0]);
3121
 
    MERGE(current_picture.f.error[1]);
3122
 
    MERGE(current_picture.f.error[2]);
 
3155
    MERGE(current_picture.f->error[0]);
 
3156
    MERGE(current_picture.f->error[1]);
 
3157
    MERGE(current_picture.f->error[2]);
3123
3158
 
3124
3159
    if(dst->avctx->noise_reduction){
3125
3160
        for(i=0; i<64; i++){
3136
3171
 
3137
3172
static int estimate_qp(MpegEncContext *s, int dry_run){
3138
3173
    if (s->next_lambda){
3139
 
        s->current_picture_ptr->f.quality =
3140
 
        s->current_picture.f.quality = s->next_lambda;
 
3174
        s->current_picture_ptr->f->quality =
 
3175
        s->current_picture.f->quality = s->next_lambda;
3141
3176
        if(!dry_run) s->next_lambda= 0;
3142
3177
    } else if (!s->fixed_qscale) {
3143
 
        s->current_picture_ptr->f.quality =
3144
 
        s->current_picture.f.quality = ff_rate_estimate_qscale(s, dry_run);
3145
 
        if (s->current_picture.f.quality < 0)
 
3178
        s->current_picture_ptr->f->quality =
 
3179
        s->current_picture.f->quality = ff_rate_estimate_qscale(s, dry_run);
 
3180
        if (s->current_picture.f->quality < 0)
3146
3181
            return -1;
3147
3182
    }
3148
3183
 
3165
3200
        s->lambda= s->lambda_table[0];
3166
3201
        //FIXME broken
3167
3202
    }else
3168
 
        s->lambda = s->current_picture.f.quality;
 
3203
        s->lambda = s->current_picture.f->quality;
3169
3204
    update_qscale(s);
3170
3205
    return 0;
3171
3206
}
3172
3207
 
3173
3208
/* must be called before writing the header */
3174
3209
static void set_frame_distances(MpegEncContext * s){
3175
 
    assert(s->current_picture_ptr->f.pts != AV_NOPTS_VALUE);
3176
 
    s->time = s->current_picture_ptr->f.pts * s->avctx->time_base.num;
 
3210
    assert(s->current_picture_ptr->f->pts != AV_NOPTS_VALUE);
 
3211
    s->time = s->current_picture_ptr->f->pts * s->avctx->time_base.num;
3177
3212
 
3178
3213
    if(s->pict_type==AV_PICTURE_TYPE_B){
3179
3214
        s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
3337
3372
    if (s->out_format == FMT_MJPEG) {
3338
3373
        /* for mjpeg, we do include qscale in the matrix */
3339
3374
        for(i=1;i<64;i++){
3340
 
            int j= s->dsp.idct_permutation[i];
 
3375
            int j = s->idsp.idct_permutation[i];
3341
3376
 
3342
3377
            s->intra_matrix[j] = av_clip_uint8((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3);
3343
3378
        }
3344
3379
        s->y_dc_scale_table=
3345
3380
        s->c_dc_scale_table= ff_mpeg2_dc_scale_table[s->intra_dc_precision];
3346
3381
        s->intra_matrix[0] = ff_mpeg2_dc_scale_table[s->intra_dc_precision][8];
3347
 
        ff_convert_matrix(&s->dsp, s->q_intra_matrix, s->q_intra_matrix16,
 
3382
        ff_convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16,
3348
3383
                       s->intra_matrix, s->intra_quant_bias, 8, 8, 1);
3349
3384
        s->qscale= 8;
3350
3385
    }
3351
3386
 
3352
3387
    //FIXME var duplication
3353
 
    s->current_picture_ptr->f.key_frame =
3354
 
    s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
3355
 
    s->current_picture_ptr->f.pict_type =
3356
 
    s->current_picture.f.pict_type = s->pict_type;
 
3388
    s->current_picture_ptr->f->key_frame =
 
3389
    s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I; //FIXME pic_ptr
 
3390
    s->current_picture_ptr->f->pict_type =
 
3391
    s->current_picture.f->pict_type = s->pict_type;
3357
3392
 
3358
 
    if (s->current_picture.f.key_frame)
 
3393
    if (s->current_picture.f->key_frame)
3359
3394
        s->picture_in_gop_number=0;
3360
3395
 
3361
3396
    s->last_bits= put_bits_count(&s->pb);
3456
3491
    uint8_t * last_length;
3457
3492
    const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
3458
3493
 
3459
 
    s->dsp.fdct (block);
 
3494
    s->fdsp.fdct(block);
3460
3495
 
3461
3496
    if(s->dct_error_sum)
3462
3497
        s->denoise_dct(s, block);
3551
3586
        int dct_coeff= FFABS(block[ scantable[i] ]);
3552
3587
        int best_score=256*256*256*120;
3553
3588
 
3554
 
        if (s->dsp.fdct == ff_fdct_ifast)
 
3589
        if (s->fdsp.fdct == ff_fdct_ifast)
3555
3590
            dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3556
3591
        zero_distortion= dct_coeff*dct_coeff;
3557
3592
 
3566
3601
            if(s->out_format == FMT_H263){
3567
3602
                unquant_coeff= alevel*qmul + qadd;
3568
3603
            }else{ //MPEG1
3569
 
                j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
 
3604
                j = s->idsp.idct_permutation[scantable[i]]; // FIXME: optimize
3570
3605
                if(s->mb_intra){
3571
3606
                        unquant_coeff = (int)(  alevel  * qscale * s->intra_matrix[j]) >> 3;
3572
3607
                        unquant_coeff =   (unquant_coeff - 1) | 1;
3772
3807
#endif
3773
3808
 
3774
3809
    if(basis[0][0] == 0)
3775
 
        build_basis(s->dsp.idct_permutation);
 
3810
        build_basis(s->idsp.idct_permutation);
3776
3811
 
3777
3812
    qmul= qscale*2;
3778
3813
    qadd= (qscale-1)|1;
3847
3882
            run_tab[rle_index++]=run;
3848
3883
            run=0;
3849
3884
 
3850
 
            s->dsp.add_8x8basis(rem, basis[j], coeff);
 
3885
            s->mpvencdsp.add_8x8basis(rem, basis[j], coeff);
3851
3886
        }else{
3852
3887
            run++;
3853
3888
        }
3861
3896
{START_TIMER
3862
3897
#endif
3863
3898
    for(;;){
3864
 
        int best_score=s->dsp.try_8x8basis(rem, weight, basis[0], 0);
 
3899
        int best_score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0], 0);
3865
3900
        int best_coeff=0;
3866
3901
        int best_change=0;
3867
3902
        int run2, best_unquant_change=0, analyze_gradient;
3883
3918
STOP_TIMER("rem*w*w")}
3884
3919
{START_TIMER
3885
3920
#endif
3886
 
            s->dsp.fdct(d1);
 
3921
            s->fdsp.fdct(d1);
3887
3922
#ifdef REFINE_STATS
3888
3923
STOP_TIMER("dct")}
3889
3924
#endif
3905
3940
                if(new_coeff >= 2048 || new_coeff < 0)
3906
3941
                    continue;
3907
3942
 
3908
 
                score= s->dsp.try_8x8basis(rem, weight, basis[0], new_coeff - old_coeff);
 
3943
                score = s->mpvencdsp.try_8x8basis(rem, weight, basis[0],
 
3944
                                                  new_coeff - old_coeff);
3909
3945
                if(score<best_score){
3910
3946
                    best_score= score;
3911
3947
                    best_coeff= 0;
4028
4064
                unquant_change= new_coeff - old_coeff;
4029
4065
                assert((score < 100*lambda && score > -100*lambda) || lambda==0);
4030
4066
 
4031
 
                score+= s->dsp.try_8x8basis(rem, weight, basis[j], unquant_change);
 
4067
                score += s->mpvencdsp.try_8x8basis(rem, weight, basis[j],
 
4068
                                                   unquant_change);
4032
4069
                if(score<best_score){
4033
4070
                    best_score= score;
4034
4071
                    best_coeff= i;
4102
4139
                 }
4103
4140
            }
4104
4141
 
4105
 
            s->dsp.add_8x8basis(rem, basis[j], best_unquant_change);
 
4142
            s->mpvencdsp.add_8x8basis(rem, basis[j], best_unquant_change);
4106
4143
        }else{
4107
4144
            break;
4108
4145
        }
4128
4165
    int max=0;
4129
4166
    unsigned int threshold1, threshold2;
4130
4167
 
4131
 
    s->dsp.fdct (block);
 
4168
    s->fdsp.fdct(block);
4132
4169
 
4133
4170
    if(s->dct_error_sum)
4134
4171
        s->denoise_dct(s, block);
4191
4228
    *overflow= s->max_qcoeff < max; //overflow might have happened
4192
4229
 
4193
4230
    /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */
4194
 
    if (s->dsp.idct_permutation_type != FF_NO_IDCT_PERM)
4195
 
        ff_block_permute(block, s->dsp.idct_permutation, scantable, last_non_zero);
 
4231
    if (s->idsp.perm_type != FF_IDCT_PERM_NONE)
 
4232
        ff_block_permute(block, s->idsp.idct_permutation,
 
4233
                         scantable, last_non_zero);
4196
4234
 
4197
4235
    return last_non_zero;
4198
4236
}