~ppsspp/ppsspp/ffmpeg

« back to all changes in this revision

Viewing changes to libavcodec/mpegvideo_enc.c

  • Committer: Henrik Rydgård
  • Date: 2014-01-03 10:44:32 UTC
  • Revision ID: git-v1:87c6c126784b1718bfa448ecf2e6a9fef781eb4e
Update our ffmpeg snapshot to a clone of the official repository.

This is because Maxim's at3plus support has been officially merged!

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 * The simplest mpeg encoder (well, it was the simplest!).
28
28
 */
29
29
 
 
30
#include <stdint.h>
 
31
 
30
32
#include "libavutil/internal.h"
31
33
#include "libavutil/intmath.h"
32
34
#include "libavutil/mathematics.h"
215
217
    }
216
218
    s->me.mv_penalty = default_mv_penalty;
217
219
    s->fcode_tab     = default_fcode_tab;
 
220
 
 
221
    s->input_picture_number  = 0;
 
222
    s->picture_in_gop_number = 0;
218
223
}
219
224
 
220
225
av_cold int ff_dct_encode_init(MpegEncContext *s) {
221
226
    if (ARCH_X86)
222
227
        ff_dct_encode_init_x86(s);
223
228
 
 
229
    ff_h263dsp_init(&s->h263dsp);
224
230
    if (!s->dct_quantize)
225
231
        s->dct_quantize = ff_dct_quantize_c;
226
232
    if (!s->denoise_dct)
236
242
av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
237
243
{
238
244
    MpegEncContext *s = avctx->priv_data;
239
 
    int i;
240
 
    int chroma_h_shift, chroma_v_shift;
 
245
    int i, ret;
241
246
 
242
247
    MPV_encode_defaults(s);
243
248
 
250
255
            return -1;
251
256
        }
252
257
        break;
253
 
    case AV_CODEC_ID_LJPEG:
254
 
        if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
255
 
            avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
256
 
            avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
257
 
            avctx->pix_fmt != AV_PIX_FMT_BGR0     &&
258
 
            avctx->pix_fmt != AV_PIX_FMT_BGRA     &&
259
 
            avctx->pix_fmt != AV_PIX_FMT_BGR24    &&
260
 
            ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
261
 
              avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
262
 
              avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
263
 
             avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
264
 
            av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
265
 
            return -1;
266
 
        }
267
 
        break;
268
258
    case AV_CODEC_ID_MJPEG:
269
259
    case AV_CODEC_ID_AMV:
270
260
        if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
315
305
    s->avctx        = avctx;
316
306
    s->flags        = avctx->flags;
317
307
    s->flags2       = avctx->flags2;
 
308
    if (avctx->max_b_frames > MAX_B_FRAMES) {
 
309
        av_log(avctx, AV_LOG_ERROR, "Too many B-frames requested, maximum "
 
310
               "is %d.\n", MAX_B_FRAMES);
 
311
        avctx->max_b_frames = MAX_B_FRAMES;
 
312
    }
318
313
    s->max_b_frames = avctx->max_b_frames;
319
314
    s->codec_id     = avctx->codec->id;
320
315
    s->strict_std_compliance = avctx->strict_std_compliance;
452
447
        av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
453
448
        return -1;
454
449
    }
 
450
    if (s->max_b_frames < 0) {
 
451
        av_log(avctx, AV_LOG_ERROR,
 
452
               "max b frames must be 0 or positive for mpegvideo based encoders\n");
 
453
        return -1;
 
454
    }
455
455
 
456
456
    if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
457
457
         s->codec_id == AV_CODEC_ID_H263  ||
641
641
 
642
642
    av_log(avctx, AV_LOG_DEBUG, "intra_quant_bias = %d inter_quant_bias = %d\n",s->intra_quant_bias,s->inter_quant_bias);
643
643
 
644
 
    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift, &chroma_v_shift);
645
 
 
646
644
    if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
647
645
        s->avctx->time_base.den > (1 << 16) - 1) {
648
646
        av_log(avctx, AV_LOG_ERROR,
666
664
        avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
667
665
        s->rtp_mode   = 1;
668
666
        break;
669
 
    case AV_CODEC_ID_LJPEG:
670
667
    case AV_CODEC_ID_MJPEG:
671
668
    case AV_CODEC_ID_AMV:
672
669
        s->out_format = FMT_MJPEG;
673
670
        s->intra_only = 1; /* force intra only for jpeg */
674
 
        if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
675
 
            (avctx->pix_fmt == AV_PIX_FMT_BGR0
676
 
             || s->avctx->pix_fmt == AV_PIX_FMT_BGRA
677
 
             || s->avctx->pix_fmt == AV_PIX_FMT_BGR24)) {
678
 
            s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
679
 
            s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
680
 
            s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
681
 
        } else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P || avctx->pix_fmt == AV_PIX_FMT_YUVJ444P) {
682
 
            s->mjpeg_vsample[0] = s->mjpeg_vsample[1] = s->mjpeg_vsample[2] = 2;
683
 
            s->mjpeg_hsample[0] = s->mjpeg_hsample[1] = s->mjpeg_hsample[2] = 1;
684
 
        } else {
685
 
            s->mjpeg_vsample[0] = 2;
686
 
            s->mjpeg_vsample[1] = 2 >> chroma_v_shift;
687
 
            s->mjpeg_vsample[2] = 2 >> chroma_v_shift;
688
 
            s->mjpeg_hsample[0] = 2;
689
 
            s->mjpeg_hsample[1] = 2 >> chroma_h_shift;
690
 
            s->mjpeg_hsample[2] = 2 >> chroma_h_shift;
691
 
        }
692
 
        if (!(CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) ||
 
671
        if (!CONFIG_MJPEG_ENCODER ||
693
672
            ff_mjpeg_encode_init(s) < 0)
694
673
            return -1;
695
674
        avctx->delay = 0;
821
800
    if (ff_MPV_common_init(s) < 0)
822
801
        return -1;
823
802
 
 
803
    s->avctx->coded_frame = &s->current_picture.f;
 
804
 
 
805
    if (s->msmpeg4_version) {
 
806
        FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
 
807
                          2 * 2 * (MAX_LEVEL + 1) *
 
808
                          (MAX_RUN + 1) * 2 * sizeof(int), fail);
 
809
    }
 
810
    FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
 
811
 
 
812
    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,   64 * 32 * sizeof(int), fail);
 
813
    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix, 64 * 32 * sizeof(int), fail);
 
814
    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,   64 * 32 * sizeof(int), fail);
 
815
    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
 
816
    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
 
817
    FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail);
 
818
    FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
 
819
                      MAX_PICTURE_COUNT * sizeof(Picture *), fail);
 
820
    FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
 
821
                      MAX_PICTURE_COUNT * sizeof(Picture *), fail);
 
822
 
 
823
    if (s->avctx->noise_reduction) {
 
824
        FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
 
825
                          2 * 64 * sizeof(uint16_t), fail);
 
826
    }
 
827
 
824
828
    ff_dct_encode_init(s);
825
829
 
826
830
    if ((CONFIG_H263P_ENCODER || CONFIG_RV20_ENCODER) && s->modified_quant)
876
880
    if (ff_rate_control_init(s) < 0)
877
881
        return -1;
878
882
 
 
883
#if FF_API_ERROR_RATE
 
884
    FF_DISABLE_DEPRECATION_WARNINGS
 
885
    if (avctx->error_rate)
 
886
        s->error_rate = avctx->error_rate;
 
887
    FF_ENABLE_DEPRECATION_WARNINGS;
 
888
#endif
 
889
 
 
890
    if (avctx->b_frame_strategy == 2) {
 
891
        for (i = 0; i < s->max_b_frames + 2; i++) {
 
892
            s->tmp_frames[i] = av_frame_alloc();
 
893
            if (!s->tmp_frames[i])
 
894
                return AVERROR(ENOMEM);
 
895
 
 
896
            s->tmp_frames[i]->format = AV_PIX_FMT_YUV420P;
 
897
            s->tmp_frames[i]->width  = s->width  >> avctx->brd_scale;
 
898
            s->tmp_frames[i]->height = s->height >> avctx->brd_scale;
 
899
 
 
900
            ret = av_frame_get_buffer(s->tmp_frames[i], 32);
 
901
            if (ret < 0)
 
902
                return ret;
 
903
        }
 
904
    }
 
905
 
879
906
    return 0;
 
907
fail:
 
908
    ff_MPV_encode_end(avctx);
 
909
    return AVERROR_UNKNOWN;
880
910
}
881
911
 
882
912
av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
883
913
{
884
914
    MpegEncContext *s = avctx->priv_data;
 
915
    int i;
885
916
 
886
917
    ff_rate_control_uninit(s);
887
918
 
888
919
    ff_MPV_common_end(s);
889
 
    if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
 
920
    if (CONFIG_MJPEG_ENCODER &&
890
921
        s->out_format == FMT_MJPEG)
891
922
        ff_mjpeg_encode_close(s);
892
923
 
893
924
    av_freep(&avctx->extradata);
894
925
 
 
926
    for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
 
927
        av_frame_free(&s->tmp_frames[i]);
 
928
 
 
929
    ff_free_picture_tables(&s->new_picture);
 
930
    ff_mpeg_unref_picture(s, &s->new_picture);
 
931
 
 
932
    av_freep(&s->avctx->stats_out);
 
933
    av_freep(&s->ac_stats);
 
934
 
 
935
    if(s->q_chroma_intra_matrix   != s->q_intra_matrix  ) av_freep(&s->q_chroma_intra_matrix);
 
936
    if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
 
937
    s->q_chroma_intra_matrix=   NULL;
 
938
    s->q_chroma_intra_matrix16= NULL;
 
939
    av_freep(&s->q_intra_matrix);
 
940
    av_freep(&s->q_inter_matrix);
 
941
    av_freep(&s->q_intra_matrix16);
 
942
    av_freep(&s->q_inter_matrix16);
 
943
    av_freep(&s->input_picture);
 
944
    av_freep(&s->reordered_input_picture);
 
945
    av_freep(&s->dct_offset);
 
946
 
895
947
    return 0;
896
948
}
897
949
 
984
1036
        if (pic_arg->linesize[2] != s->uvlinesize)
985
1037
            direct = 0;
986
1038
 
987
 
        av_dlog(s->avctx, "%d %d %d %d\n", pic_arg->linesize[0],
988
 
                pic_arg->linesize[1], (int) s->linesize, (int) s->uvlinesize);
 
1039
        av_dlog(s->avctx, "%d %d %td %td\n", pic_arg->linesize[0],
 
1040
                pic_arg->linesize[1], s->linesize, s->uvlinesize);
989
1041
 
990
1042
        if (direct) {
991
1043
            i = ff_find_unused_picture(s, 1);
1093
1145
                uint8_t *rptr = ref->f.data[plane] + 8 * (x + y * stride);
1094
1146
                int v   = s->dsp.frame_skip_cmp[1](s, dptr, rptr, stride, 8);
1095
1147
 
1096
 
                switch (s->avctx->frame_skip_exp) {
 
1148
                switch (FFABS(s->avctx->frame_skip_exp)) {
1097
1149
                case 0: score    =  FFMAX(score, v);          break;
1098
1150
                case 1: score   += FFABS(v);                  break;
1099
 
                case 2: score   += v * v;                     break;
1100
 
                case 3: score64 += FFABS(v * v * (int64_t)v); break;
1101
 
                case 4: score64 += v * v * (int64_t)(v * v);  break;
 
1151
                case 2: score64 += v * (int64_t)v;                       break;
 
1152
                case 3: score64 += FFABS(v * (int64_t)v * v);            break;
 
1153
                case 4: score64 += (v * (int64_t)v) * (v * (int64_t)v);  break;
1102
1154
                }
1103
1155
            }
1104
1156
        }
1105
1157
    }
 
1158
    emms_c();
1106
1159
 
1107
1160
    if (score)
1108
1161
        score64 = score;
 
1162
    if (s->avctx->frame_skip_exp < 0)
 
1163
        score64 = pow(score64 / (double)(s->mb_width * s->mb_height),
 
1164
                      -1.0/s->avctx->frame_skip_exp);
1109
1165
 
1110
1166
    if (score64 < s->avctx->frame_skip_threshold)
1111
1167
        return 1;
1133
1189
{
1134
1190
    AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);
1135
1191
    AVCodecContext *c = avcodec_alloc_context3(NULL);
1136
 
    AVFrame input[FF_MAX_B_FRAMES + 2];
1137
1192
    const int scale = s->avctx->brd_scale;
1138
1193
    int i, j, out_size, p_lambda, b_lambda, lambda2;
1139
1194
    int64_t best_rd  = INT64_MAX;
1168
1223
        return -1;
1169
1224
 
1170
1225
    for (i = 0; i < s->max_b_frames + 2; i++) {
1171
 
        int ysize = c->width * c->height;
1172
 
        int csize = (c->width / 2) * (c->height / 2);
1173
1226
        Picture pre_input, *pre_input_ptr = i ? s->input_picture[i - 1] :
1174
1227
                                                s->next_picture_ptr;
1175
1228
 
1176
 
        avcodec_get_frame_defaults(&input[i]);
1177
 
        input[i].data[0]     = av_malloc(ysize + 2 * csize);
1178
 
        input[i].data[1]     = input[i].data[0] + ysize;
1179
 
        input[i].data[2]     = input[i].data[1] + csize;
1180
 
        input[i].linesize[0] = c->width;
1181
 
        input[i].linesize[1] =
1182
 
        input[i].linesize[2] = c->width / 2;
1183
 
 
1184
1229
        if (pre_input_ptr && (!i || s->input_picture[i - 1])) {
1185
1230
            pre_input = *pre_input_ptr;
1186
1231
 
1190
1235
                pre_input.f.data[2] += INPLACE_OFFSET;
1191
1236
            }
1192
1237
 
1193
 
            s->dsp.shrink[scale](input[i].data[0], input[i].linesize[0],
 
1238
            s->dsp.shrink[scale](s->tmp_frames[i]->data[0], s->tmp_frames[i]->linesize[0],
1194
1239
                                 pre_input.f.data[0], pre_input.f.linesize[0],
1195
1240
                                 c->width,      c->height);
1196
 
            s->dsp.shrink[scale](input[i].data[1], input[i].linesize[1],
 
1241
            s->dsp.shrink[scale](s->tmp_frames[i]->data[1], s->tmp_frames[i]->linesize[1],
1197
1242
                                 pre_input.f.data[1], pre_input.f.linesize[1],
1198
1243
                                 c->width >> 1, c->height >> 1);
1199
 
            s->dsp.shrink[scale](input[i].data[2], input[i].linesize[2],
 
1244
            s->dsp.shrink[scale](s->tmp_frames[i]->data[2], s->tmp_frames[i]->linesize[2],
1200
1245
                                 pre_input.f.data[2], pre_input.f.linesize[2],
1201
1246
                                 c->width >> 1, c->height >> 1);
1202
1247
        }
1210
1255
 
1211
1256
        c->error[0] = c->error[1] = c->error[2] = 0;
1212
1257
 
1213
 
        input[0].pict_type = AV_PICTURE_TYPE_I;
1214
 
        input[0].quality   = 1 * FF_QP2LAMBDA;
 
1258
        s->tmp_frames[0]->pict_type = AV_PICTURE_TYPE_I;
 
1259
        s->tmp_frames[0]->quality   = 1 * FF_QP2LAMBDA;
1215
1260
 
1216
 
        out_size = encode_frame(c, &input[0]);
 
1261
        out_size = encode_frame(c, s->tmp_frames[0]);
1217
1262
 
1218
1263
        //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1219
1264
 
1220
1265
        for (i = 0; i < s->max_b_frames + 1; i++) {
1221
1266
            int is_p = i % (j + 1) == j || i == s->max_b_frames;
1222
1267
 
1223
 
            input[i + 1].pict_type = is_p ?
 
1268
            s->tmp_frames[i + 1]->pict_type = is_p ?
1224
1269
                                     AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1225
 
            input[i + 1].quality   = is_p ? p_lambda : b_lambda;
 
1270
            s->tmp_frames[i + 1]->quality   = is_p ? p_lambda : b_lambda;
1226
1271
 
1227
 
            out_size = encode_frame(c, &input[i + 1]);
 
1272
            out_size = encode_frame(c, s->tmp_frames[i + 1]);
1228
1273
 
1229
1274
            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1230
1275
        }
1246
1291
    avcodec_close(c);
1247
1292
    av_freep(&c);
1248
1293
 
1249
 
    for (i = 0; i < s->max_b_frames + 2; i++) {
1250
 
        av_freep(&input[i].data[0]);
1251
 
    }
1252
 
 
1253
1294
    return best_b_count;
1254
1295
}
1255
1296
 
1263
1304
 
1264
1305
    /* set next picture type & ordering */
1265
1306
    if (s->reordered_input_picture[0] == NULL && s->input_picture[0]) {
 
1307
        if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
 
1308
            if (s->picture_in_gop_number < s->gop_size &&
 
1309
                s->next_picture_ptr &&
 
1310
                skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
 
1311
                // FIXME check that te gop check above is +-1 correct
 
1312
                av_frame_unref(&s->input_picture[0]->f);
 
1313
 
 
1314
                ff_vbv_update(s, 0);
 
1315
 
 
1316
                goto no_output_pic;
 
1317
            }
 
1318
        }
 
1319
 
1266
1320
        if (/*s->picture_in_gop_number >= s->gop_size ||*/
1267
1321
            s->next_picture_ptr == NULL || s->intra_only) {
1268
1322
            s->reordered_input_picture[0] = s->input_picture[0];
1272
1326
        } else {
1273
1327
            int b_frames;
1274
1328
 
1275
 
            if (s->avctx->frame_skip_threshold || s->avctx->frame_skip_factor) {
1276
 
                if (s->picture_in_gop_number < s->gop_size &&
1277
 
                    skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1278
 
                    // FIXME check that te gop check above is +-1 correct
1279
 
                    av_frame_unref(&s->input_picture[0]->f);
1280
 
 
1281
 
                    emms_c();
1282
 
                    ff_vbv_update(s, 0);
1283
 
 
1284
 
                    goto no_output_pic;
1285
 
                }
1286
 
            }
1287
 
 
1288
1329
            if (s->flags & CODEC_FLAG_PASS2) {
1289
1330
                for (i = 0; i < s->max_b_frames + 1; i++) {
1290
1331
                    int pict_num = s->input_picture[0]->f.display_picture_number + i;
1431
1472
    return 0;
1432
1473
}
1433
1474
 
 
1475
static void frame_end(MpegEncContext *s)
 
1476
{
 
1477
    if (s->unrestricted_mv &&
 
1478
        s->current_picture.reference &&
 
1479
        !s->intra_only) {
 
1480
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
 
1481
        int hshift = desc->log2_chroma_w;
 
1482
        int vshift = desc->log2_chroma_h;
 
1483
        s->dsp.draw_edges(s->current_picture.f.data[0], s->current_picture.f.linesize[0],
 
1484
                          s->h_edge_pos, s->v_edge_pos,
 
1485
                          EDGE_WIDTH, EDGE_WIDTH,
 
1486
                          EDGE_TOP | EDGE_BOTTOM);
 
1487
        s->dsp.draw_edges(s->current_picture.f.data[1], s->current_picture.f.linesize[1],
 
1488
                          s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
 
1489
                          EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
 
1490
                          EDGE_TOP | EDGE_BOTTOM);
 
1491
        s->dsp.draw_edges(s->current_picture.f.data[2], s->current_picture.f.linesize[2],
 
1492
                          s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
 
1493
                          EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
 
1494
                          EDGE_TOP | EDGE_BOTTOM);
 
1495
    }
 
1496
 
 
1497
    emms_c();
 
1498
 
 
1499
    s->last_pict_type                 = s->pict_type;
 
1500
    s->last_lambda_for [s->pict_type] = s->current_picture_ptr->f.quality;
 
1501
    if (s->pict_type!= AV_PICTURE_TYPE_B)
 
1502
        s->last_non_b_pict_type = s->pict_type;
 
1503
 
 
1504
    s->avctx->coded_frame = &s->current_picture_ptr->f;
 
1505
 
 
1506
}
 
1507
 
1434
1508
int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
1435
1509
                          AVFrame *pic_arg, int *got_packet)
1436
1510
{
1486
1560
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count;
1487
1561
        avctx->skip_count  = s->skip_count;
1488
1562
 
1489
 
        ff_MPV_frame_end(s);
 
1563
        frame_end(s);
1490
1564
 
1491
1565
        if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1492
 
            ff_mjpeg_encode_picture_trailer(s);
 
1566
            ff_mjpeg_encode_picture_trailer(&s->pb, s->header_bits);
1493
1567
 
1494
1568
        if (avctx->rc_buffer_size) {
1495
1569
            RateControlContext *rcc = &s->rc_context;
1815
1889
    ptr_cr = s->new_picture.f.data[2] +
1816
1890
             (mb_y * mb_block_height * wrap_c) + mb_x * mb_block_width;
1817
1891
 
1818
 
    if((mb_x*16+16 > s->width || mb_y*16+16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
 
1892
    if((mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) && s->codec_id != AV_CODEC_ID_AMV){
1819
1893
        uint8_t *ebuf = s->edge_emu_buffer + 32;
1820
1894
        int cw = (s->width  + s->chroma_x_shift) >> s->chroma_x_shift;
1821
1895
        int ch = (s->height + s->chroma_y_shift) >> s->chroma_y_shift;
1822
 
        s->vdsp.emulated_edge_mc(ebuf, wrap_y, ptr_y, wrap_y, 16, 16, mb_x * 16,
1823
 
                                 mb_y * 16, s->width, s->height);
 
1896
        s->vdsp.emulated_edge_mc(ebuf, ptr_y,
 
1897
                                 wrap_y, wrap_y,
 
1898
                                 16, 16, mb_x * 16, mb_y * 16,
 
1899
                                 s->width, s->height);
1824
1900
        ptr_y = ebuf;
1825
 
        s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, wrap_c, ptr_cb, wrap_c, mb_block_width,
1826
 
                                 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
 
1901
        s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb,
 
1902
                                 wrap_c, wrap_c,
 
1903
                                 mb_block_width, mb_block_height,
 
1904
                                 mb_x * mb_block_width, mb_y * mb_block_height,
1827
1905
                                 cw, ch);
1828
1906
        ptr_cb = ebuf + 18 * wrap_y;
1829
 
        s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 16, wrap_c, ptr_cr, wrap_c, mb_block_width,
1830
 
                                 mb_block_height, mb_x * mb_block_width, mb_y * mb_block_height,
 
1907
        s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 16, ptr_cr,
 
1908
                                 wrap_c, wrap_c,
 
1909
                                 mb_block_width, mb_block_height,
 
1910
                                 mb_x * mb_block_width, mb_y * mb_block_height,
1831
1911
                                 cw, ch);
1832
1912
        ptr_cr = ebuf + 18 * wrap_y + 16;
1833
1913
    }
2567
2647
                if(is_gob_start){
2568
2648
                    if(s->start_mb_y != mb_y || mb_x!=0){
2569
2649
                        write_slice_end(s);
 
2650
 
2570
2651
                        if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2571
2652
                            ff_mpeg4_init_partitions(s);
2572
2653
                        }
2575
2656
                    av_assert2((put_bits_count(&s->pb)&7) == 0);
2576
2657
                    current_packet_size= put_bits_ptr(&s->pb) - s->ptr_lastgob;
2577
2658
 
2578
 
                    if(s->avctx->error_rate && s->resync_mb_x + s->resync_mb_y > 0){
 
2659
                    if (s->error_rate && s->resync_mb_x + s->resync_mb_y > 0) {
2579
2660
                        int r= put_bits_count(&s->pb)/8 + s->picture_number + 16 + s->mb_x + s->mb_y;
2580
 
                        int d= 100 / s->avctx->error_rate;
 
2661
                        int d = 100 / s->error_rate;
2581
2662
                        if(r % d == 0){
2582
2663
                            current_packet_size=0;
2583
2664
                            s->pb.buf_ptr= s->ptr_lastgob;
3372
3453
    switch(s->out_format) {
3373
3454
    case FMT_MJPEG:
3374
3455
        if (CONFIG_MJPEG_ENCODER)
3375
 
            ff_mjpeg_encode_picture_header(s);
 
3456
            ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
 
3457
                                           s->intra_matrix);
3376
3458
        break;
3377
3459
    case FMT_H261:
3378
3460
        if (CONFIG_H261_ENCODER)
3572
3654
 
3573
3655
            av_assert2(level);
3574
3656
 
3575
 
            if(s->out_format == FMT_H263){
 
3657
            if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
3576
3658
                unquant_coeff= alevel*qmul + qadd;
3577
3659
            }else{ //MPEG1
3578
3660
                j= s->dsp.idct_permutation[ scantable[i] ]; //FIXME optimize
3601
3683
                    }
3602
3684
                }
3603
3685
 
3604
 
                if(s->out_format == FMT_H263){
 
3686
                if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
3605
3687
                    for(j=survivor_count-1; j>=0; j--){
3606
3688
                        int run= i - survivor[j];
3607
3689
                        int score= distortion + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda;
3627
3709
                    }
3628
3710
                }
3629
3711
 
3630
 
                if(s->out_format == FMT_H263){
 
3712
                if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
3631
3713
                  for(j=survivor_count-1; j>=0; j--){
3632
3714
                        int run= i - survivor[j];
3633
3715
                        int score= distortion + score_tab[i-run];
3660
3742
        survivor[ survivor_count++ ]= i+1;
3661
3743
    }
3662
3744
 
3663
 
    if(s->out_format != FMT_H263){
 
3745
    if(s->out_format != FMT_H263 && s->out_format != FMT_H261){
3664
3746
        last_score= 256*256*256*120;
3665
3747
        for(i= survivor[0]; i<=last_non_zero + 1; i++){
3666
3748
            int score= score_tab[i];
3693
3775
            int alevel= FFABS(level);
3694
3776
            int unquant_coeff, score, distortion;
3695
3777
 
3696
 
            if(s->out_format == FMT_H263){
 
3778
            if(s->out_format == FMT_H263 || s->out_format == FMT_H261){
3697
3779
                    unquant_coeff= (alevel*qmul + qadd)>>3;
3698
3780
            }else{ //MPEG1
3699
3781
                    unquant_coeff = (((  alevel  << 1) + 1) * qscale * ((int) s->inter_matrix[0])) >> 4;