~ubuntu-branches/ubuntu/saucy/gst-libav1.0/saucy-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/libavcodec/mpegvideo_enc.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-07-30 09:00:15 UTC
  • mfrom: (1.1.16) (7.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20130730090015-sc1ou2yssu7q5w4e
Tags: 1.1.3-1
* New upstream development snapshot:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.1.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
#include "libavutil/intmath.h"
31
31
#include "libavutil/mathematics.h"
 
32
#include "libavutil/pixdesc.h"
32
33
#include "libavutil/opt.h"
33
34
#include "avcodec.h"
34
35
#include "dsputil.h"
35
36
#include "mpegvideo.h"
36
 
#include "mpegvideo_common.h"
37
37
#include "h263.h"
 
38
#include "mathops.h"
38
39
#include "mjpegenc.h"
39
40
#include "msmpeg4.h"
40
41
#include "faandct.h"
43
44
#include "flv.h"
44
45
#include "mpeg4video.h"
45
46
#include "internal.h"
 
47
#include "bytestream.h"
46
48
#include <limits.h>
47
49
 
48
50
//#undef NDEBUG
62
64
static uint8_t default_mv_penalty[MAX_FCODE + 1][MAX_MV * 2 + 1];
63
65
static uint8_t default_fcode_tab[MAX_MV * 2 + 1];
64
66
 
 
67
const AVOption ff_mpv_generic_options[] = {
 
68
    FF_MPV_COMMON_OPTS
 
69
    { NULL },
 
70
};
 
71
 
65
72
void ff_convert_matrix(DSPContext *dsp, int (*qmat)[64],
66
73
                       uint16_t (*qmat16)[2][64],
67
74
                       const uint16_t *quant_matrix,
73
80
    for (qscale = qmin; qscale <= qmax; qscale++) {
74
81
        int i;
75
82
        if (dsp->fdct == ff_jpeg_fdct_islow_8 ||
76
 
            dsp->fdct == ff_jpeg_fdct_islow_10
77
 
#ifdef FAAN_POSTSCALE
78
 
            || dsp->fdct == ff_faandct
79
 
#endif
80
 
            ) {
 
83
            dsp->fdct == ff_jpeg_fdct_islow_10 ||
 
84
            dsp->fdct == ff_faandct) {
81
85
            for (i = 0; i < 64; i++) {
82
86
                const int j = dsp->idct_permutation[i];
83
87
                /* 16 <= qscale * quant_matrix[i] <= 7905
89
93
                qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) /
90
94
                                        (qscale * quant_matrix[j]));
91
95
            }
92
 
        } else if (dsp->fdct == fdct_ifast
93
 
#ifndef FAAN_POSTSCALE
94
 
                   || dsp->fdct == ff_faandct
95
 
#endif
96
 
                   ) {
 
96
        } else if (dsp->fdct == ff_fdct_ifast) {
97
97
            for (i = 0; i < 64; i++) {
98
98
                const int j = dsp->idct_permutation[i];
99
99
                /* 16 <= qscale * quant_matrix[i] <= 7905
132
132
 
133
133
        for (i = intra; i < 64; i++) {
134
134
            int64_t max = 8191;
135
 
            if (dsp->fdct == fdct_ifast
136
 
#ifndef FAAN_POSTSCALE
137
 
                || dsp->fdct == ff_faandct
138
 
#endif
139
 
               ) {
 
135
            if (dsp->fdct == ff_fdct_ifast) {
140
136
                max = (8191LL * ff_aanscales[i]) >> 14;
141
137
            }
142
138
            while (((max * qmat[qscale][i]) >> shift) > INT_MAX) {
264
260
static void MPV_encode_defaults(MpegEncContext *s)
265
261
{
266
262
    int i;
267
 
    MPV_common_defaults(s);
 
263
    ff_MPV_common_defaults(s);
268
264
 
269
265
    for (i = -16; i < 16; i++) {
270
266
        default_fcode_tab[i + MAX_MV] = 1;
274
270
}
275
271
 
276
272
/* init video encoder */
277
 
av_cold int MPV_encode_init(AVCodecContext *avctx)
 
273
av_cold int ff_MPV_encode_init(AVCodecContext *avctx)
278
274
{
279
275
    MpegEncContext *s = avctx->priv_data;
280
276
    int i;
283
279
    MPV_encode_defaults(s);
284
280
 
285
281
    switch (avctx->codec_id) {
286
 
    case CODEC_ID_MPEG2VIDEO:
287
 
        if (avctx->pix_fmt != PIX_FMT_YUV420P &&
288
 
            avctx->pix_fmt != PIX_FMT_YUV422P) {
 
282
    case AV_CODEC_ID_MPEG2VIDEO:
 
283
        if (avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
 
284
            avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
289
285
            av_log(avctx, AV_LOG_ERROR,
290
286
                   "only YUV420 and YUV422 are supported\n");
291
287
            return -1;
292
288
        }
293
289
        break;
294
 
    case CODEC_ID_LJPEG:
295
 
        if (avctx->pix_fmt != PIX_FMT_YUVJ420P &&
296
 
            avctx->pix_fmt != PIX_FMT_YUVJ422P &&
297
 
            avctx->pix_fmt != PIX_FMT_YUVJ444P &&
298
 
            avctx->pix_fmt != PIX_FMT_BGRA     &&
299
 
            ((avctx->pix_fmt != PIX_FMT_YUV420P &&
300
 
              avctx->pix_fmt != PIX_FMT_YUV422P &&
301
 
              avctx->pix_fmt != PIX_FMT_YUV444P) ||
 
290
    case AV_CODEC_ID_LJPEG:
 
291
        if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
 
292
            avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
 
293
            avctx->pix_fmt != AV_PIX_FMT_YUVJ444P &&
 
294
            avctx->pix_fmt != AV_PIX_FMT_BGRA     &&
 
295
            ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
 
296
              avctx->pix_fmt != AV_PIX_FMT_YUV422P &&
 
297
              avctx->pix_fmt != AV_PIX_FMT_YUV444P) ||
302
298
             avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
303
299
            av_log(avctx, AV_LOG_ERROR, "colorspace not supported in LJPEG\n");
304
300
            return -1;
305
301
        }
306
302
        break;
307
 
    case CODEC_ID_MJPEG:
308
 
        if (avctx->pix_fmt != PIX_FMT_YUVJ420P &&
309
 
            avctx->pix_fmt != PIX_FMT_YUVJ422P &&
310
 
            ((avctx->pix_fmt != PIX_FMT_YUV420P &&
311
 
              avctx->pix_fmt != PIX_FMT_YUV422P) ||
 
303
    case AV_CODEC_ID_MJPEG:
 
304
        if (avctx->pix_fmt != AV_PIX_FMT_YUVJ420P &&
 
305
            avctx->pix_fmt != AV_PIX_FMT_YUVJ422P &&
 
306
            ((avctx->pix_fmt != AV_PIX_FMT_YUV420P &&
 
307
              avctx->pix_fmt != AV_PIX_FMT_YUV422P) ||
312
308
             avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL)) {
313
309
            av_log(avctx, AV_LOG_ERROR, "colorspace not supported in jpeg\n");
314
310
            return -1;
315
311
        }
316
312
        break;
317
313
    default:
318
 
        if (avctx->pix_fmt != PIX_FMT_YUV420P) {
 
314
        if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
319
315
            av_log(avctx, AV_LOG_ERROR, "only YUV420 is supported\n");
320
316
            return -1;
321
317
        }
322
318
    }
323
319
 
324
320
    switch (avctx->pix_fmt) {
325
 
    case PIX_FMT_YUVJ422P:
326
 
    case PIX_FMT_YUV422P:
 
321
    case AV_PIX_FMT_YUVJ422P:
 
322
    case AV_PIX_FMT_YUV422P:
327
323
        s->chroma_format = CHROMA_422;
328
324
        break;
329
 
    case PIX_FMT_YUVJ420P:
330
 
    case PIX_FMT_YUV420P:
 
325
    case AV_PIX_FMT_YUVJ420P:
 
326
    case AV_PIX_FMT_YUV420P:
331
327
    default:
332
328
        s->chroma_format = CHROMA_420;
333
329
        break;
348
344
    s->flags2       = avctx->flags2;
349
345
    s->max_b_frames = avctx->max_b_frames;
350
346
    s->codec_id     = avctx->codec->id;
351
 
    s->luma_elim_threshold   = avctx->luma_elim_threshold;
352
 
    s->chroma_elim_threshold = avctx->chroma_elim_threshold;
 
347
#if FF_API_MPV_GLOBAL_OPTS
 
348
    if (avctx->luma_elim_threshold)
 
349
        s->luma_elim_threshold   = avctx->luma_elim_threshold;
 
350
    if (avctx->chroma_elim_threshold)
 
351
        s->chroma_elim_threshold = avctx->chroma_elim_threshold;
 
352
#endif
353
353
    s->strict_std_compliance = avctx->strict_std_compliance;
354
 
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
355
 
    if (avctx->flags & CODEC_FLAG_PART)
356
 
        s->data_partitioning = 1;
357
 
#endif
358
354
    s->quarter_sample     = (avctx->flags & CODEC_FLAG_QPEL) != 0;
359
355
    s->mpeg_quant         = avctx->mpeg_quant;
360
356
    s->rtp_mode           = !!avctx->rtp_payload_size;
373
369
    /* Fixed QSCALE */
374
370
    s->fixed_qscale = !!(avctx->flags & CODEC_FLAG_QSCALE);
375
371
 
 
372
#if FF_API_MPV_GLOBAL_OPTS
 
373
    if (s->flags & CODEC_FLAG_QP_RD)
 
374
        s->mpv_flags |= FF_MPV_FLAG_QP_RD;
 
375
#endif
 
376
 
376
377
    s->adaptive_quant = (s->avctx->lumi_masking ||
377
378
                         s->avctx->dark_masking ||
378
379
                         s->avctx->temporal_cplx_masking ||
379
380
                         s->avctx->spatial_cplx_masking  ||
380
381
                         s->avctx->p_masking      ||
381
382
                         s->avctx->border_masking ||
382
 
                         (s->flags & CODEC_FLAG_QP_RD)) &&
 
383
                         (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
383
384
                        !s->fixed_qscale;
384
385
 
385
386
    s->loop_filter      = !!(s->flags & CODEC_FLAG_LOOP_FILTER);
386
 
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
387
 
    s->alternate_scan   = !!(s->flags  & CODEC_FLAG_ALT_SCAN);
388
 
    s->intra_vlc_format = !!(s->flags2 & CODEC_FLAG2_INTRA_VLC);
389
 
    s->q_scale_type     = !!(s->flags2 & CODEC_FLAG2_NON_LINEAR_QUANT);
390
 
    s->obmc             = !!(s->flags  & CODEC_FLAG_OBMC);
391
 
#endif
392
387
 
393
388
    if (avctx->rc_max_rate && !avctx->rc_buffer_size) {
394
389
        av_log(avctx, AV_LOG_ERROR,
436
431
 
437
432
    if (s->avctx->rc_max_rate &&
438
433
        s->avctx->rc_min_rate == s->avctx->rc_max_rate &&
439
 
        (s->codec_id == CODEC_ID_MPEG1VIDEO ||
440
 
         s->codec_id == CODEC_ID_MPEG2VIDEO) &&
 
434
        (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
 
435
         s->codec_id == AV_CODEC_ID_MPEG2VIDEO) &&
441
436
        90000LL * (avctx->rc_buffer_size - 1) >
442
437
            s->avctx->rc_max_rate * 0xFFFFLL) {
443
438
        av_log(avctx, AV_LOG_INFO,
445
440
               "specified vbv buffer is too large for the given bitrate!\n");
446
441
    }
447
442
 
448
 
    if ((s->flags & CODEC_FLAG_4MV)  && s->codec_id != CODEC_ID_MPEG4 &&
449
 
        s->codec_id != CODEC_ID_H263 && s->codec_id != CODEC_ID_H263P &&
450
 
        s->codec_id != CODEC_ID_FLV1) {
 
443
    if ((s->flags & CODEC_FLAG_4MV)  && s->codec_id != AV_CODEC_ID_MPEG4 &&
 
444
        s->codec_id != AV_CODEC_ID_H263 && s->codec_id != AV_CODEC_ID_H263P &&
 
445
        s->codec_id != AV_CODEC_ID_FLV1) {
451
446
        av_log(avctx, AV_LOG_ERROR, "4MV not supported by codec\n");
452
447
        return -1;
453
448
    }
458
453
        return -1;
459
454
    }
460
455
 
461
 
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
462
 
    if (s->obmc && s->codec_id != CODEC_ID_H263 &&
463
 
        s->codec_id != CODEC_ID_H263P) {
464
 
        av_log(avctx, AV_LOG_ERROR, "OBMC is only supported with H263(+)\n");
465
 
        return -1;
466
 
    }
467
 
#endif
468
 
 
469
 
    if (s->quarter_sample && s->codec_id != CODEC_ID_MPEG4) {
 
456
    if (s->quarter_sample && s->codec_id != AV_CODEC_ID_MPEG4) {
470
457
        av_log(avctx, AV_LOG_ERROR, "qpel not supported by codec\n");
471
458
        return -1;
472
459
    }
473
460
 
474
 
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
475
 
    if (s->data_partitioning && s->codec_id != CODEC_ID_MPEG4) {
476
 
        av_log(avctx, AV_LOG_ERROR,
477
 
               "data partitioning not supported by codec\n");
478
 
        return -1;
479
 
    }
480
 
#endif
481
 
 
482
461
    if (s->max_b_frames                    &&
483
 
        s->codec_id != CODEC_ID_MPEG4      &&
484
 
        s->codec_id != CODEC_ID_MPEG1VIDEO &&
485
 
        s->codec_id != CODEC_ID_MPEG2VIDEO) {
 
462
        s->codec_id != AV_CODEC_ID_MPEG4      &&
 
463
        s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
 
464
        s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
486
465
        av_log(avctx, AV_LOG_ERROR, "b frames not supported by codec\n");
487
466
        return -1;
488
467
    }
489
468
 
490
 
    if ((s->codec_id == CODEC_ID_MPEG4 ||
491
 
         s->codec_id == CODEC_ID_H263  ||
492
 
         s->codec_id == CODEC_ID_H263P) &&
 
469
    if ((s->codec_id == AV_CODEC_ID_MPEG4 ||
 
470
         s->codec_id == AV_CODEC_ID_H263  ||
 
471
         s->codec_id == AV_CODEC_ID_H263P) &&
493
472
        (avctx->sample_aspect_ratio.num > 255 ||
494
473
         avctx->sample_aspect_ratio.den > 255)) {
495
474
        av_log(avctx, AV_LOG_ERROR,
498
477
        return -1;
499
478
    }
500
479
 
501
 
    if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME
502
 
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
503
 
                    | CODEC_FLAG_ALT_SCAN
504
 
#endif
505
 
        )) &&
506
 
        s->codec_id != CODEC_ID_MPEG4 && s->codec_id != CODEC_ID_MPEG2VIDEO) {
 
480
    if ((s->flags & (CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME)) &&
 
481
        s->codec_id != AV_CODEC_ID_MPEG4 && s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
507
482
        av_log(avctx, AV_LOG_ERROR, "interlacing not supported by codec\n");
508
483
        return -1;
509
484
    }
510
485
 
511
486
    // FIXME mpeg2 uses that too
512
 
    if (s->mpeg_quant && s->codec_id != CODEC_ID_MPEG4) {
 
487
    if (s->mpeg_quant && s->codec_id != AV_CODEC_ID_MPEG4) {
513
488
        av_log(avctx, AV_LOG_ERROR,
514
489
               "mpeg2 style quantization not supported by codec\n");
515
490
        return -1;
516
491
    }
517
492
 
518
 
    if ((s->flags & CODEC_FLAG_CBP_RD) && !avctx->trellis) {
 
493
#if FF_API_MPV_GLOBAL_OPTS
 
494
    if (s->flags & CODEC_FLAG_CBP_RD)
 
495
        s->mpv_flags |= FF_MPV_FLAG_CBP_RD;
 
496
#endif
 
497
 
 
498
    if ((s->mpv_flags & FF_MPV_FLAG_CBP_RD) && !avctx->trellis) {
519
499
        av_log(avctx, AV_LOG_ERROR, "CBP RD needs trellis quant\n");
520
500
        return -1;
521
501
    }
522
502
 
523
 
    if ((s->flags & CODEC_FLAG_QP_RD) &&
 
503
    if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) &&
524
504
        s->avctx->mb_decision != FF_MB_DECISION_RD) {
525
505
        av_log(avctx, AV_LOG_ERROR, "QP RD needs mbd=2\n");
526
506
        return -1;
534
514
        return -1;
535
515
    }
536
516
 
537
 
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
538
 
    if ((s->flags2 & CODEC_FLAG2_INTRA_VLC) &&
539
 
        s->codec_id != CODEC_ID_MPEG2VIDEO) {
540
 
        av_log(avctx, AV_LOG_ERROR,
541
 
               "intra vlc table not supported by codec\n");
542
 
        return -1;
543
 
    }
544
 
#endif
545
 
 
546
517
    if (s->flags & CODEC_FLAG_LOW_DELAY) {
547
 
        if (s->codec_id != CODEC_ID_MPEG2VIDEO) {
 
518
        if (s->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
548
519
            av_log(avctx, AV_LOG_ERROR,
549
520
                  "low delay forcing is only available for mpeg2\n");
550
521
            return -1;
557
528
    }
558
529
 
559
530
    if (s->q_scale_type == 1) {
560
 
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
561
 
        if (s->codec_id != CODEC_ID_MPEG2VIDEO) {
562
 
            av_log(avctx, AV_LOG_ERROR,
563
 
                   "non linear quant is only available for mpeg2\n");
564
 
            return -1;
565
 
        }
566
 
#endif
567
531
        if (avctx->qmax > 12) {
568
532
            av_log(avctx, AV_LOG_ERROR,
569
533
                   "non linear quant only supports qmax <= 12 currently\n");
572
536
    }
573
537
 
574
538
    if (s->avctx->thread_count > 1         &&
575
 
        s->codec_id != CODEC_ID_MPEG4      &&
576
 
        s->codec_id != CODEC_ID_MPEG1VIDEO &&
577
 
        s->codec_id != CODEC_ID_MPEG2VIDEO &&
578
 
        (s->codec_id != CODEC_ID_H263P
579
 
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
580
 
         || !(s->flags & CODEC_FLAG_H263P_SLICE_STRUCT)
581
 
#endif
582
 
         )) {
 
539
        s->codec_id != AV_CODEC_ID_MPEG4      &&
 
540
        s->codec_id != AV_CODEC_ID_MPEG1VIDEO &&
 
541
        s->codec_id != AV_CODEC_ID_MPEG2VIDEO &&
 
542
        (s->codec_id != AV_CODEC_ID_H263P)) {
583
543
        av_log(avctx, AV_LOG_ERROR,
584
544
               "multi threaded encoding not supported by codec\n");
585
545
        return -1;
626
586
        //return -1;
627
587
    }
628
588
 
629
 
    if (s->mpeg_quant || s->codec_id == CODEC_ID_MPEG1VIDEO ||
630
 
        s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG) {
 
589
    if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
 
590
        s->codec_id == AV_CODEC_ID_MPEG2VIDEO || s->codec_id == AV_CODEC_ID_MJPEG) {
631
591
        // (a + x * 3 / 8) / x
632
592
        s->intra_quant_bias = 3 << (QUANT_BIAS_SHIFT - 3);
633
593
        s->inter_quant_bias = 0;
642
602
    if (avctx->inter_quant_bias != FF_DEFAULT_QUANT_BIAS)
643
603
        s->inter_quant_bias = avctx->inter_quant_bias;
644
604
 
645
 
    avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
646
 
                                  &chroma_v_shift);
 
605
    av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &chroma_h_shift,
 
606
                                     &chroma_v_shift);
647
607
 
648
 
    if (avctx->codec_id == CODEC_ID_MPEG4 &&
 
608
    if (avctx->codec_id == AV_CODEC_ID_MPEG4 &&
649
609
        s->avctx->time_base.den > (1 << 16) - 1) {
650
610
        av_log(avctx, AV_LOG_ERROR,
651
611
               "timebase %d/%d not supported by MPEG 4 standard, "
656
616
    }
657
617
    s->time_increment_bits = av_log2(s->avctx->time_base.den - 1) + 1;
658
618
 
 
619
#if FF_API_MPV_GLOBAL_OPTS
 
620
    if (avctx->flags2 & CODEC_FLAG2_SKIP_RD)
 
621
        s->mpv_flags |= FF_MPV_FLAG_SKIP_RD;
 
622
    if (avctx->flags2 & CODEC_FLAG2_STRICT_GOP)
 
623
        s->mpv_flags |= FF_MPV_FLAG_STRICT_GOP;
 
624
    if (avctx->quantizer_noise_shaping)
 
625
        s->quantizer_noise_shaping = avctx->quantizer_noise_shaping;
 
626
#endif
 
627
 
659
628
    switch (avctx->codec->id) {
660
 
    case CODEC_ID_MPEG1VIDEO:
 
629
    case AV_CODEC_ID_MPEG1VIDEO:
661
630
        s->out_format = FMT_MPEG1;
662
631
        s->low_delay  = !!(s->flags & CODEC_FLAG_LOW_DELAY);
663
632
        avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
664
633
        break;
665
 
    case CODEC_ID_MPEG2VIDEO:
 
634
    case AV_CODEC_ID_MPEG2VIDEO:
666
635
        s->out_format = FMT_MPEG1;
667
636
        s->low_delay  = !!(s->flags & CODEC_FLAG_LOW_DELAY);
668
637
        avctx->delay  = s->low_delay ? 0 : (s->max_b_frames + 1);
669
638
        s->rtp_mode   = 1;
670
639
        break;
671
 
    case CODEC_ID_LJPEG:
672
 
    case CODEC_ID_MJPEG:
 
640
    case AV_CODEC_ID_LJPEG:
 
641
    case AV_CODEC_ID_MJPEG:
673
642
        s->out_format = FMT_MJPEG;
674
643
        s->intra_only = 1; /* force intra only for jpeg */
675
 
        if (avctx->codec->id == CODEC_ID_LJPEG &&
676
 
            avctx->pix_fmt   == PIX_FMT_BGRA) {
 
644
        if (avctx->codec->id == AV_CODEC_ID_LJPEG &&
 
645
            avctx->pix_fmt   == AV_PIX_FMT_BGRA) {
677
646
            s->mjpeg_vsample[0] = s->mjpeg_hsample[0] =
678
647
            s->mjpeg_vsample[1] = s->mjpeg_hsample[1] =
679
648
            s->mjpeg_vsample[2] = s->mjpeg_hsample[2] = 1;
691
660
        avctx->delay = 0;
692
661
        s->low_delay = 1;
693
662
        break;
694
 
    case CODEC_ID_H261:
 
663
    case AV_CODEC_ID_H261:
695
664
        if (!CONFIG_H261_ENCODER)
696
665
            return -1;
697
666
        if (ff_h261_get_picture_format(s->width, s->height) < 0) {
705
674
        avctx->delay  = 0;
706
675
        s->low_delay  = 1;
707
676
        break;
708
 
    case CODEC_ID_H263:
 
677
    case AV_CODEC_ID_H263:
709
678
        if (!CONFIG_H263_ENCODER)
710
679
        return -1;
711
680
        if (ff_match_2uint16(ff_h263_format, FF_ARRAY_ELEMS(ff_h263_format),
721
690
        avctx->delay  = 0;
722
691
        s->low_delay  = 1;
723
692
        break;
724
 
    case CODEC_ID_H263P:
 
693
    case AV_CODEC_ID_H263P:
725
694
        s->out_format = FMT_H263;
726
695
        s->h263_plus  = 1;
727
696
        /* Fx */
728
 
#if FF_API_MPEGVIDEO_GLOBAL_OPTS
729
 
        if (avctx->flags & CODEC_FLAG_H263P_UMV)
730
 
            s->umvplus = 1;
731
 
        if (avctx->flags & CODEC_FLAG_H263P_AIV)
732
 
            s->alt_inter_vlc = 1;
733
 
        if (avctx->flags & CODEC_FLAG_H263P_SLICE_STRUCT)
734
 
            s->h263_slice_structured = 1;
735
 
#endif
736
697
        s->h263_aic        = (avctx->flags & CODEC_FLAG_AC_PRED) ? 1 : 0;
737
698
        s->modified_quant  = s->h263_aic;
738
699
        s->loop_filter     = (avctx->flags & CODEC_FLAG_LOOP_FILTER) ? 1 : 0;
743
704
        avctx->delay = 0;
744
705
        s->low_delay = 1;
745
706
        break;
746
 
    case CODEC_ID_FLV1:
 
707
    case AV_CODEC_ID_FLV1:
747
708
        s->out_format      = FMT_H263;
748
709
        s->h263_flv        = 2; /* format = 1; 11-bit codes */
749
710
        s->unrestricted_mv = 1;
751
712
        avctx->delay = 0;
752
713
        s->low_delay = 1;
753
714
        break;
754
 
    case CODEC_ID_RV10:
 
715
    case AV_CODEC_ID_RV10:
755
716
        s->out_format = FMT_H263;
756
717
        avctx->delay  = 0;
757
718
        s->low_delay  = 1;
758
719
        break;
759
 
    case CODEC_ID_RV20:
 
720
    case AV_CODEC_ID_RV20:
760
721
        s->out_format      = FMT_H263;
761
722
        avctx->delay       = 0;
762
723
        s->low_delay       = 1;
766
727
        s->loop_filter     = 1;
767
728
        s->unrestricted_mv = 0;
768
729
        break;
769
 
    case CODEC_ID_MPEG4:
 
730
    case AV_CODEC_ID_MPEG4:
770
731
        s->out_format      = FMT_H263;
771
732
        s->h263_pred       = 1;
772
733
        s->unrestricted_mv = 1;
773
734
        s->low_delay       = s->max_b_frames ? 0 : 1;
774
735
        avctx->delay       = s->low_delay ? 0 : (s->max_b_frames + 1);
775
736
        break;
776
 
    case CODEC_ID_MSMPEG4V2:
 
737
    case AV_CODEC_ID_MSMPEG4V2:
777
738
        s->out_format      = FMT_H263;
778
739
        s->h263_pred       = 1;
779
740
        s->unrestricted_mv = 1;
781
742
        avctx->delay       = 0;
782
743
        s->low_delay       = 1;
783
744
        break;
784
 
    case CODEC_ID_MSMPEG4V3:
 
745
    case AV_CODEC_ID_MSMPEG4V3:
785
746
        s->out_format        = FMT_H263;
786
747
        s->h263_pred         = 1;
787
748
        s->unrestricted_mv   = 1;
790
751
        avctx->delay         = 0;
791
752
        s->low_delay         = 1;
792
753
        break;
793
 
    case CODEC_ID_WMV1:
 
754
    case AV_CODEC_ID_WMV1:
794
755
        s->out_format        = FMT_H263;
795
756
        s->h263_pred         = 1;
796
757
        s->unrestricted_mv   = 1;
799
760
        avctx->delay         = 0;
800
761
        s->low_delay         = 1;
801
762
        break;
802
 
    case CODEC_ID_WMV2:
 
763
    case AV_CODEC_ID_WMV2:
803
764
        s->out_format        = FMT_H263;
804
765
        s->h263_pred         = 1;
805
766
        s->unrestricted_mv   = 1;
822
783
                                s->alternate_scan);
823
784
 
824
785
    /* init */
825
 
    if (MPV_common_init(s) < 0)
 
786
    if (ff_MPV_common_init(s) < 0)
826
787
        return -1;
827
788
 
 
789
    if (ARCH_X86)
 
790
        ff_MPV_encode_init_x86(s);
 
791
 
828
792
    if (!s->dct_quantize)
829
 
        s->dct_quantize = dct_quantize_c;
 
793
        s->dct_quantize = ff_dct_quantize_c;
830
794
    if (!s->denoise_dct)
831
795
        s->denoise_dct  = denoise_dct_c;
832
796
    s->fast_dct_quantize = s->dct_quantize;
854
818
    /* init q matrix */
855
819
    for (i = 0; i < 64; i++) {
856
820
        int j = s->dsp.idct_permutation[i];
857
 
        if (CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4 &&
 
821
        if (CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4 &&
858
822
            s->mpeg_quant) {
859
823
            s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i];
860
824
            s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i];
889
853
    return 0;
890
854
}
891
855
 
892
 
av_cold int MPV_encode_end(AVCodecContext *avctx)
 
856
av_cold int ff_MPV_encode_end(AVCodecContext *avctx)
893
857
{
894
858
    MpegEncContext *s = avctx->priv_data;
895
859
 
896
860
    ff_rate_control_uninit(s);
897
861
 
898
 
    MPV_common_end(s);
 
862
    ff_MPV_common_end(s);
899
863
    if ((CONFIG_MJPEG_ENCODER || CONFIG_LJPEG_ENCODER) &&
900
864
        s->out_format == FMT_MJPEG)
901
865
        ff_mjpeg_encode_close(s);
948
912
    AVFrame *pic = NULL;
949
913
    int64_t pts;
950
914
    int i;
951
 
    const int encoding_delay = s->max_b_frames;
 
915
    const int encoding_delay = s->max_b_frames ? s->max_b_frames :
 
916
                                                 (s->low_delay ? 0 : 1);
952
917
    int direct = 1;
953
918
 
954
919
    if (pic_arg) {
966
931
                           "last=%"PRId64"\n", pts, s->user_specified_pts);
967
932
                    return -1;
968
933
                }
 
934
 
 
935
                if (!s->low_delay && pic_arg->display_picture_number == 1)
 
936
                    s->dts_delta = time - last;
969
937
            }
970
938
            s->user_specified_pts = pts;
971
939
        } else {
991
959
    if (pic_arg->linesize[2] != s->uvlinesize)
992
960
        direct = 0;
993
961
 
994
 
    //av_log(AV_LOG_DEBUG, "%d %d %d %d\n",pic_arg->linesize[0],
995
 
    //       pic_arg->linesize[1], s->linesize, s->uvlinesize);
 
962
    av_dlog(s->avctx, "%d %d %d %d\n", pic_arg->linesize[0],
 
963
            pic_arg->linesize[1], s->linesize, s->uvlinesize);
996
964
 
997
965
    if (direct) {
998
966
        i = ff_find_unused_picture(s, 1);
999
967
        if (i < 0)
1000
968
            return i;
1001
969
 
1002
 
        pic = (AVFrame *) &s->picture[i];
 
970
        pic = &s->picture[i].f;
1003
971
        pic->reference = 3;
1004
972
 
1005
973
        for (i = 0; i < 4; i++) {
1014
982
        if (i < 0)
1015
983
            return i;
1016
984
 
1017
 
        pic = (AVFrame *) &s->picture[i];
 
985
        pic = &s->picture[i].f;
1018
986
        pic->reference = 3;
1019
987
 
1020
988
        if (ff_alloc_picture(s, (Picture *) pic, 0) < 0) {
1027
995
            // empty
1028
996
        } else {
1029
997
            int h_chroma_shift, v_chroma_shift;
1030
 
            avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift,
1031
 
                                          &v_chroma_shift);
 
998
            av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
 
999
                                             &h_chroma_shift,
 
1000
                                             &v_chroma_shift);
1032
1001
 
1033
1002
            for (i = 0; i < 3; i++) {
1034
1003
                int src_stride = pic_arg->linesize[i];
1105
1074
    return 0;
1106
1075
}
1107
1076
 
 
1077
static int encode_frame(AVCodecContext *c, AVFrame *frame)
 
1078
{
 
1079
    AVPacket pkt = { 0 };
 
1080
    int ret, got_output;
 
1081
 
 
1082
    av_init_packet(&pkt);
 
1083
    ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
 
1084
    if (ret < 0)
 
1085
        return ret;
 
1086
 
 
1087
    ret = pkt.size;
 
1088
    av_free_packet(&pkt);
 
1089
    return ret;
 
1090
}
 
1091
 
1108
1092
static int estimate_best_b_count(MpegEncContext *s)
1109
1093
{
1110
1094
    AVCodec *codec    = avcodec_find_encoder(s->avctx->codec_id);
1112
1096
    AVFrame input[FF_MAX_B_FRAMES + 2];
1113
1097
    const int scale = s->avctx->brd_scale;
1114
1098
    int i, j, out_size, p_lambda, b_lambda, lambda2;
1115
 
    int outbuf_size  = s->width * s->height; // FIXME
1116
 
    uint8_t *outbuf  = av_malloc(outbuf_size);
1117
1099
    int64_t best_rd  = INT64_MAX;
1118
1100
    int best_b_count = -1;
1119
1101
 
1138
1120
    c->me_cmp       = s->avctx->me_cmp;
1139
1121
    c->mb_cmp       = s->avctx->mb_cmp;
1140
1122
    c->me_sub_cmp   = s->avctx->me_sub_cmp;
1141
 
    c->pix_fmt      = PIX_FMT_YUV420P;
 
1123
    c->pix_fmt      = AV_PIX_FMT_YUV420P;
1142
1124
    c->time_base    = s->avctx->time_base;
1143
1125
    c->max_b_frames = s->max_b_frames;
1144
1126
 
1190
1172
 
1191
1173
        input[0].pict_type = AV_PICTURE_TYPE_I;
1192
1174
        input[0].quality   = 1 * FF_QP2LAMBDA;
1193
 
        out_size           = avcodec_encode_video(c, outbuf,
1194
 
                                                  outbuf_size, &input[0]);
 
1175
 
 
1176
        out_size = encode_frame(c, &input[0]);
 
1177
 
1195
1178
        //rd += (out_size * lambda2) >> FF_LAMBDA_SHIFT;
1196
1179
 
1197
1180
        for (i = 0; i < s->max_b_frames + 1; i++) {
1200
1183
            input[i + 1].pict_type = is_p ?
1201
1184
                                     AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_B;
1202
1185
            input[i + 1].quality   = is_p ? p_lambda : b_lambda;
1203
 
            out_size = avcodec_encode_video(c, outbuf, outbuf_size,
1204
 
                                            &input[i + 1]);
 
1186
 
 
1187
            out_size = encode_frame(c, &input[i + 1]);
 
1188
 
1205
1189
            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1206
1190
        }
1207
1191
 
1208
1192
        /* get the delayed frames */
1209
1193
        while (out_size) {
1210
 
            out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
 
1194
            out_size = encode_frame(c, NULL);
1211
1195
            rd += (out_size * lambda2) >> (FF_LAMBDA_SHIFT - 3);
1212
1196
        }
1213
1197
 
1219
1203
        }
1220
1204
    }
1221
1205
 
1222
 
    av_freep(&outbuf);
1223
1206
    avcodec_close(c);
1224
1207
    av_freep(&c);
1225
1208
 
1253
1236
                if (s->picture_in_gop_number < s->gop_size &&
1254
1237
                    skip_check(s, s->input_picture[0], s->next_picture_ptr)) {
1255
1238
                    // FIXME check that te gop check above is +-1 correct
1256
 
                    //av_log(NULL, AV_LOG_DEBUG, "skip %p %"PRId64"\n",
1257
 
                    //       s->input_picture[0]->f.data[0],
1258
 
                    //       s->input_picture[0]->pts);
1259
 
 
1260
1239
                    if (s->input_picture[0]->f.type == FF_BUFFER_TYPE_SHARED) {
1261
1240
                        for (i = 0; i < 4; i++)
1262
1241
                            s->input_picture[0]->f.data[i] = NULL;
1266
1245
                               s->input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL);
1267
1246
 
1268
1247
                        s->avctx->release_buffer(s->avctx,
1269
 
                                                 (AVFrame *) s->input_picture[0]);
 
1248
                                                 &s->input_picture[0]->f);
1270
1249
                    }
1271
1250
 
1272
1251
                    emms_c();
1328
1307
            }
1329
1308
 
1330
1309
            emms_c();
1331
 
            //static int b_count = 0;
1332
 
            //b_count += b_frames;
1333
 
            //av_log(s->avctx, AV_LOG_DEBUG, "b_frames: %d\n", b_count);
1334
1310
 
1335
1311
            for (i = b_frames - 1; i >= 0; i--) {
1336
1312
                int type = s->input_picture[i]->f.pict_type;
1344
1320
            }
1345
1321
 
1346
1322
            if (s->picture_in_gop_number + b_frames >= s->gop_size) {
1347
 
                if ((s->flags2 & CODEC_FLAG2_STRICT_GOP) &&
 
1323
                if ((s->mpv_flags & FF_MPV_FLAG_STRICT_GOP) &&
1348
1324
                    s->gop_size > s->picture_in_gop_number) {
1349
1325
                    b_frames = s->gop_size - s->picture_in_gop_number - 1;
1350
1326
                } else {
1399
1375
            /* mark us unused / free shared pic */
1400
1376
            if (s->reordered_input_picture[0]->f.type == FF_BUFFER_TYPE_INTERNAL)
1401
1377
                s->avctx->release_buffer(s->avctx,
1402
 
                                         (AVFrame *) s->reordered_input_picture[0]);
 
1378
                                         &s->reordered_input_picture[0]->f);
1403
1379
            for (i = 0; i < 4; i++)
1404
1380
                s->reordered_input_picture[0]->f.data[i] = NULL;
1405
1381
            s->reordered_input_picture[0]->f.type = 0;
1406
1382
 
1407
 
            copy_picture_attributes(s, (AVFrame *) pic,
1408
 
                                    (AVFrame *) s->reordered_input_picture[0]);
 
1383
            copy_picture_attributes(s, &pic->f,
 
1384
                                    &s->reordered_input_picture[0]->f);
1409
1385
 
1410
1386
            s->current_picture_ptr = pic;
1411
1387
        } else {
1424
1400
        ff_copy_picture(&s->current_picture, s->current_picture_ptr);
1425
1401
 
1426
1402
        s->picture_number = s->new_picture.f.display_picture_number;
1427
 
        //printf("dpn:%d\n", s->picture_number);
1428
1403
    } else {
1429
1404
        memset(&s->new_picture, 0, sizeof(Picture));
1430
1405
    }
1431
1406
    return 0;
1432
1407
}
1433
1408
 
1434
 
int MPV_encode_picture(AVCodecContext *avctx,
1435
 
                       unsigned char *buf, int buf_size, void *data)
 
1409
int ff_MPV_encode_picture(AVCodecContext *avctx, AVPacket *pkt,
 
1410
                          const AVFrame *pic_arg, int *got_packet)
1436
1411
{
1437
1412
    MpegEncContext *s = avctx->priv_data;
1438
 
    AVFrame *pic_arg  = data;
1439
 
    int i, stuffing_count;
 
1413
    int i, stuffing_count, ret;
1440
1414
    int context_count = s->slice_context_count;
1441
1415
 
1442
 
    for (i = 0; i < context_count; i++) {
1443
 
        int start_y = s->thread_context[i]->start_mb_y;
1444
 
        int   end_y = s->thread_context[i]->  end_mb_y;
1445
 
        int h       = s->mb_height;
1446
 
        uint8_t *start = buf + (size_t)(((int64_t) buf_size) * start_y / h);
1447
 
        uint8_t *end   = buf + (size_t)(((int64_t) buf_size) *   end_y / h);
1448
 
 
1449
 
        init_put_bits(&s->thread_context[i]->pb, start, end - start);
1450
 
    }
1451
 
 
1452
1416
    s->picture_in_gop_number++;
1453
1417
 
1454
1418
    if (load_input_picture(s, pic_arg) < 0)
1460
1424
 
1461
1425
    /* output? */
1462
1426
    if (s->new_picture.f.data[0]) {
 
1427
        if (!pkt->data &&
 
1428
            (ret = ff_alloc_packet(pkt, s->mb_width*s->mb_height*MAX_MB_BYTES)) < 0)
 
1429
            return ret;
 
1430
        if (s->mb_info) {
 
1431
            s->mb_info_ptr = av_packet_new_side_data(pkt,
 
1432
                                 AV_PKT_DATA_H263_MB_INFO,
 
1433
                                 s->mb_width*s->mb_height*12);
 
1434
            s->prev_mb_info = s->last_mb_info = s->mb_info_size = 0;
 
1435
        }
 
1436
 
 
1437
        for (i = 0; i < context_count; i++) {
 
1438
            int start_y = s->thread_context[i]->start_mb_y;
 
1439
            int   end_y = s->thread_context[i]->  end_mb_y;
 
1440
            int h       = s->mb_height;
 
1441
            uint8_t *start = pkt->data + (size_t)(((int64_t) pkt->size) * start_y / h);
 
1442
            uint8_t *end   = pkt->data + (size_t)(((int64_t) pkt->size) *   end_y / h);
 
1443
 
 
1444
            init_put_bits(&s->thread_context[i]->pb, start, end - start);
 
1445
        }
 
1446
 
1463
1447
        s->pict_type = s->new_picture.f.pict_type;
1464
1448
        //emms_c();
1465
 
        //printf("qs:%f %f %d\n", s->new_picture.quality,
1466
 
        //       s->current_picture.quality, s->qscale);
1467
 
        MPV_frame_start(s, avctx);
 
1449
        ff_MPV_frame_start(s, avctx);
1468
1450
vbv_retry:
1469
1451
        if (encode_picture(s, s->picture_number) < 0)
1470
1452
            return -1;
1479
1461
        avctx->p_count     = s->mb_num - s->i_count - s->skip_count;
1480
1462
        avctx->skip_count  = s->skip_count;
1481
1463
 
1482
 
        MPV_frame_end(s);
 
1464
        ff_MPV_frame_end(s);
1483
1465
 
1484
1466
        if (CONFIG_MJPEG_ENCODER && s->out_format == FMT_MJPEG)
1485
1467
            ff_mjpeg_encode_picture_trailer(s);
1504
1486
                // done in encode_picture() so we must undo it
1505
1487
                if (s->pict_type == AV_PICTURE_TYPE_P) {
1506
1488
                    if (s->flipflop_rounding          ||
1507
 
                        s->codec_id == CODEC_ID_H263P ||
1508
 
                        s->codec_id == CODEC_ID_MPEG4)
 
1489
                        s->codec_id == AV_CODEC_ID_H263P ||
 
1490
                        s->codec_id == AV_CODEC_ID_MPEG4)
1509
1491
                        s->no_rounding ^= 1;
1510
1492
                }
1511
1493
                if (s->pict_type != AV_PICTURE_TYPE_B) {
1512
1494
                    s->time_base       = s->last_time_base;
1513
1495
                    s->last_non_b_time = s->time - s->pp_time;
1514
1496
                }
1515
 
                //av_log(NULL, AV_LOG_ERROR, "R:%d ", s->next_lambda);
1516
1497
                for (i = 0; i < context_count; i++) {
1517
1498
                    PutBitContext *pb = &s->thread_context[i]->pb;
1518
1499
                    init_put_bits(pb, pb->buf, pb->buf_end - pb->buf);
1547
1528
            }
1548
1529
 
1549
1530
            switch (s->codec_id) {
1550
 
            case CODEC_ID_MPEG1VIDEO:
1551
 
            case CODEC_ID_MPEG2VIDEO:
 
1531
            case AV_CODEC_ID_MPEG1VIDEO:
 
1532
            case AV_CODEC_ID_MPEG2VIDEO:
1552
1533
                while (stuffing_count--) {
1553
1534
                    put_bits(&s->pb, 8, 0);
1554
1535
                }
1555
1536
            break;
1556
 
            case CODEC_ID_MPEG4:
 
1537
            case AV_CODEC_ID_MPEG4:
1557
1538
                put_bits(&s->pb, 16, 0);
1558
1539
                put_bits(&s->pb, 16, 0x1C3);
1559
1540
                stuffing_count -= 4;
1604
1585
        }
1605
1586
        s->total_bits     += s->frame_bits;
1606
1587
        avctx->frame_bits  = s->frame_bits;
 
1588
 
 
1589
        pkt->pts = s->current_picture.f.pts;
 
1590
        if (!s->low_delay) {
 
1591
            if (!s->current_picture.f.coded_picture_number)
 
1592
                pkt->dts = pkt->pts - s->dts_delta;
 
1593
            else
 
1594
                pkt->dts = s->reordered_pts;
 
1595
            s->reordered_pts = s->input_picture[0]->f.pts;
 
1596
        } else
 
1597
            pkt->dts = pkt->pts;
 
1598
        if (s->current_picture.f.key_frame)
 
1599
            pkt->flags |= AV_PKT_FLAG_KEY;
 
1600
        if (s->mb_info)
 
1601
            av_packet_shrink_side_data(pkt, AV_PKT_DATA_H263_MB_INFO, s->mb_info_size);
1607
1602
    } else {
1608
 
        assert((put_bits_ptr(&s->pb) == s->pb.buf));
1609
1603
        s->frame_bits = 0;
1610
1604
    }
1611
1605
    assert((s->frame_bits & 7) == 0);
1612
1606
 
1613
 
    return s->frame_bits / 8;
 
1607
    pkt->size = s->frame_bits / 8;
 
1608
    *got_packet = !!pkt->size;
 
1609
    return 0;
1614
1610
}
1615
1611
 
1616
1612
static inline void dct_single_coeff_elimination(MpegEncContext *s,
1752
1748
        s->lambda = s->lambda_table[mb_xy];
1753
1749
        update_qscale(s);
1754
1750
 
1755
 
        if (!(s->flags & CODEC_FLAG_QP_RD)) {
 
1751
        if (!(s->mpv_flags & FF_MPV_FLAG_QP_RD)) {
1756
1752
            s->qscale = s->current_picture_ptr->f.qscale_table[mb_xy];
1757
1753
            s->dquant = s->qscale - last_qp;
1758
1754
 
1759
1755
            if (s->out_format == FMT_H263) {
1760
1756
                s->dquant = av_clip(s->dquant, -2, 2);
1761
1757
 
1762
 
                if (s->codec_id == CODEC_ID_MPEG4) {
 
1758
                if (s->codec_id == AV_CODEC_ID_MPEG4) {
1763
1759
                    if (!s->mb_intra) {
1764
1760
                        if (s->pict_type == AV_PICTURE_TYPE_B) {
1765
1761
                            if (s->dquant & 1 || s->mv_dir & MV_DIRECT)
1772
1768
            }
1773
1769
        }
1774
1770
        ff_set_qscale(s, last_qp + s->dquant);
1775
 
    } else if (s->flags & CODEC_FLAG_QP_RD)
 
1771
    } else if (s->mpv_flags & FF_MPV_FLAG_QP_RD)
1776
1772
        ff_set_qscale(s, s->qscale + s->dquant);
1777
1773
 
1778
1774
    wrap_y = s->linesize;
1786
1782
 
1787
1783
    if (mb_x * 16 + 16 > s->width || mb_y * 16 + 16 > s->height) {
1788
1784
        uint8_t *ebuf = s->edge_emu_buffer + 32;
1789
 
        s->dsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,
1790
 
                                mb_y * 16, s->width, s->height);
 
1785
        s->vdsp.emulated_edge_mc(ebuf, ptr_y, wrap_y, 16, 16, mb_x * 16,
 
1786
                                 mb_y * 16, s->width, s->height);
1791
1787
        ptr_y = ebuf;
1792
 
        s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8,
1793
 
                                mb_block_height, mb_x * 8, mb_y * 8,
1794
 
                                s->width >> 1, s->height >> 1);
 
1788
        s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y, ptr_cb, wrap_c, 8,
 
1789
                                 mb_block_height, mb_x * 8, mb_y * 8,
 
1790
                                 s->width >> 1, s->height >> 1);
1795
1791
        ptr_cb = ebuf + 18 * wrap_y;
1796
 
        s->dsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8,
1797
 
                                mb_block_height, mb_x * 8, mb_y * 8,
1798
 
                                s->width >> 1, s->height >> 1);
 
1792
        s->vdsp.emulated_edge_mc(ebuf + 18 * wrap_y + 8, ptr_cr, wrap_c, 8,
 
1793
                                 mb_block_height, mb_x * 8, mb_y * 8,
 
1794
                                 s->width >> 1, s->height >> 1);
1799
1795
        ptr_cr = ebuf + 18 * wrap_y + 8;
1800
1796
    }
1801
1797
 
1861
1857
        }
1862
1858
 
1863
1859
        if (s->mv_dir & MV_DIR_FORWARD) {
1864
 
            MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data,
1865
 
                       op_pix, op_qpix);
 
1860
            ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0,
 
1861
                          s->last_picture.f.data,
 
1862
                          op_pix, op_qpix);
1866
1863
            op_pix  = s->dsp.avg_pixels_tab;
1867
1864
            op_qpix = s->dsp.avg_qpel_pixels_tab;
1868
1865
        }
1869
1866
        if (s->mv_dir & MV_DIR_BACKWARD) {
1870
 
            MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data,
1871
 
                       op_pix, op_qpix);
 
1867
            ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1,
 
1868
                          s->next_picture.f.data,
 
1869
                          op_pix, op_qpix);
1872
1870
        }
1873
1871
 
1874
1872
        if (s->flags & CODEC_FLAG_INTERLACED_DCT) {
1960
1958
        }
1961
1959
    }
1962
1960
 
1963
 
    if (s->avctx->quantizer_noise_shaping) {
 
1961
    if (s->quantizer_noise_shaping) {
1964
1962
        if (!skip_dct[0])
1965
1963
            get_visual_weight(weight[0], ptr_y                 , wrap_y);
1966
1964
        if (!skip_dct[1])
2001
1999
            } else
2002
2000
                s->block_last_index[i] = -1;
2003
2001
        }
2004
 
        if (s->avctx->quantizer_noise_shaping) {
 
2002
        if (s->quantizer_noise_shaping) {
2005
2003
            for (i = 0; i < mb_block_count; i++) {
2006
2004
                if (!skip_dct[i]) {
2007
2005
                    s->block_last_index[i] =
2018
2016
            for (i = 4; i < mb_block_count; i++)
2019
2017
                dct_single_coeff_elimination(s, i, s->chroma_elim_threshold);
2020
2018
 
2021
 
        if (s->flags & CODEC_FLAG_CBP_RD) {
 
2019
        if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
2022
2020
            for (i = 0; i < mb_block_count; i++) {
2023
2021
                if (s->block_last_index[i] == -1)
2024
2022
                    s->coded_score[i] = INT_MAX / 256;
2034
2032
    }
2035
2033
 
2036
2034
    // non c quantize code returns incorrect block_last_index FIXME
2037
 
    if (s->alternate_scan && s->dct_quantize != dct_quantize_c) {
 
2035
    if (s->alternate_scan && s->dct_quantize != ff_dct_quantize_c) {
2038
2036
        for (i = 0; i < mb_block_count; i++) {
2039
2037
            int j;
2040
2038
            if (s->block_last_index[i] > 0) {
2049
2047
 
2050
2048
    /* huffman encode */
2051
2049
    switch(s->codec_id){ //FIXME funct ptr could be slightly faster
2052
 
    case CODEC_ID_MPEG1VIDEO:
2053
 
    case CODEC_ID_MPEG2VIDEO:
 
2050
    case AV_CODEC_ID_MPEG1VIDEO:
 
2051
    case AV_CODEC_ID_MPEG2VIDEO:
2054
2052
        if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
2055
 
            mpeg1_encode_mb(s, s->block, motion_x, motion_y);
 
2053
            ff_mpeg1_encode_mb(s, s->block, motion_x, motion_y);
2056
2054
        break;
2057
 
    case CODEC_ID_MPEG4:
 
2055
    case AV_CODEC_ID_MPEG4:
2058
2056
        if (CONFIG_MPEG4_ENCODER)
2059
 
            mpeg4_encode_mb(s, s->block, motion_x, motion_y);
 
2057
            ff_mpeg4_encode_mb(s, s->block, motion_x, motion_y);
2060
2058
        break;
2061
 
    case CODEC_ID_MSMPEG4V2:
2062
 
    case CODEC_ID_MSMPEG4V3:
2063
 
    case CODEC_ID_WMV1:
 
2059
    case AV_CODEC_ID_MSMPEG4V2:
 
2060
    case AV_CODEC_ID_MSMPEG4V3:
 
2061
    case AV_CODEC_ID_WMV1:
2064
2062
        if (CONFIG_MSMPEG4_ENCODER)
2065
 
            msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
 
2063
            ff_msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
2066
2064
        break;
2067
 
    case CODEC_ID_WMV2:
 
2065
    case AV_CODEC_ID_WMV2:
2068
2066
        if (CONFIG_WMV2_ENCODER)
2069
2067
            ff_wmv2_encode_mb(s, s->block, motion_x, motion_y);
2070
2068
        break;
2071
 
    case CODEC_ID_H261:
 
2069
    case AV_CODEC_ID_H261:
2072
2070
        if (CONFIG_H261_ENCODER)
2073
2071
            ff_h261_encode_mb(s, s->block, motion_x, motion_y);
2074
2072
        break;
2075
 
    case CODEC_ID_H263:
2076
 
    case CODEC_ID_H263P:
2077
 
    case CODEC_ID_FLV1:
2078
 
    case CODEC_ID_RV10:
2079
 
    case CODEC_ID_RV20:
 
2073
    case AV_CODEC_ID_H263:
 
2074
    case AV_CODEC_ID_H263P:
 
2075
    case AV_CODEC_ID_FLV1:
 
2076
    case AV_CODEC_ID_RV10:
 
2077
    case AV_CODEC_ID_RV20:
2080
2078
        if (CONFIG_H263_ENCODER)
2081
2079
            ff_h263_encode_mb(s, s->block, motion_x, motion_y);
2082
2080
        break;
2083
 
    case CODEC_ID_MJPEG:
 
2081
    case AV_CODEC_ID_MJPEG:
2084
2082
        if (CONFIG_MJPEG_ENCODER)
2085
2083
            ff_mjpeg_encode_mb(s, s->block);
2086
2084
        break;
2195
2193
    }
2196
2194
 
2197
2195
    if(s->avctx->mb_decision == FF_MB_DECISION_RD){
2198
 
        MPV_decode_mb(s, s->block);
 
2196
        ff_MPV_decode_mb(s, s->block);
2199
2197
 
2200
2198
        score *= s->lambda2;
2201
2199
        score += sse_mb(s) << FF_LAMBDA_SHIFT;
2328
2326
}
2329
2327
 
2330
2328
static void write_slice_end(MpegEncContext *s){
2331
 
    if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4){
 
2329
    if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4){
2332
2330
        if(s->partitioned_frame){
2333
2331
            ff_mpeg4_merge_partitions(s);
2334
2332
        }
2345
2343
        s->misc_bits+= get_bits_diff(s);
2346
2344
}
2347
2345
 
 
2346
static void write_mb_info(MpegEncContext *s)
 
2347
{
 
2348
    uint8_t *ptr = s->mb_info_ptr + s->mb_info_size - 12;
 
2349
    int offset = put_bits_count(&s->pb);
 
2350
    int mba  = s->mb_x + s->mb_width * (s->mb_y % s->gob_index);
 
2351
    int gobn = s->mb_y / s->gob_index;
 
2352
    int pred_x, pred_y;
 
2353
    if (CONFIG_H263_ENCODER)
 
2354
        ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
 
2355
    bytestream_put_le32(&ptr, offset);
 
2356
    bytestream_put_byte(&ptr, s->qscale);
 
2357
    bytestream_put_byte(&ptr, gobn);
 
2358
    bytestream_put_le16(&ptr, mba);
 
2359
    bytestream_put_byte(&ptr, pred_x); /* hmv1 */
 
2360
    bytestream_put_byte(&ptr, pred_y); /* vmv1 */
 
2361
    /* 4MV not implemented */
 
2362
    bytestream_put_byte(&ptr, 0); /* hmv2 */
 
2363
    bytestream_put_byte(&ptr, 0); /* vmv2 */
 
2364
}
 
2365
 
 
2366
static void update_mb_info(MpegEncContext *s, int startcode)
 
2367
{
 
2368
    if (!s->mb_info)
 
2369
        return;
 
2370
    if (put_bits_count(&s->pb) - s->prev_mb_info*8 >= s->mb_info*8) {
 
2371
        s->mb_info_size += 12;
 
2372
        s->prev_mb_info = s->last_mb_info;
 
2373
    }
 
2374
    if (startcode) {
 
2375
        s->prev_mb_info = put_bits_count(&s->pb)/8;
 
2376
        /* This might have incremented mb_info_size above, and we return without
 
2377
         * actually writing any info into that slot yet. But in that case,
 
2378
         * this will be called again at the start of the after writing the
 
2379
         * start code, actually writing the mb info. */
 
2380
        return;
 
2381
    }
 
2382
 
 
2383
    s->last_mb_info = put_bits_count(&s->pb)/8;
 
2384
    if (!s->mb_info_size)
 
2385
        s->mb_info_size += 12;
 
2386
    write_mb_info(s);
 
2387
}
 
2388
 
2348
2389
static int encode_thread(AVCodecContext *c, void *arg){
2349
2390
    MpegEncContext *s= *(void**)arg;
2350
2391
    int mb_x, mb_y, pdif = 0;
2355
2396
    uint8_t bit_buf2[2][MAX_MB_BYTES];
2356
2397
    uint8_t bit_buf_tex[2][MAX_MB_BYTES];
2357
2398
    PutBitContext pb[2], pb2[2], tex_pb[2];
2358
 
//printf("%d->%d\n", s->resync_mb_y, s->end_mb_y);
2359
2399
 
2360
2400
    ff_check_alignment();
2361
2401
 
2388
2428
    s->last_mv_dir = 0;
2389
2429
 
2390
2430
    switch(s->codec_id){
2391
 
    case CODEC_ID_H263:
2392
 
    case CODEC_ID_H263P:
2393
 
    case CODEC_ID_FLV1:
 
2431
    case AV_CODEC_ID_H263:
 
2432
    case AV_CODEC_ID_H263P:
 
2433
    case AV_CODEC_ID_FLV1:
2394
2434
        if (CONFIG_H263_ENCODER)
2395
2435
            s->gob_index = ff_h263_get_gob_height(s);
2396
2436
        break;
2397
 
    case CODEC_ID_MPEG4:
 
2437
    case AV_CODEC_ID_MPEG4:
2398
2438
        if(CONFIG_MPEG4_ENCODER && s->partitioned_frame)
2399
2439
            ff_mpeg4_init_partitions(s);
2400
2440
        break;
2405
2445
    s->first_slice_line = 1;
2406
2446
    s->ptr_lastgob = s->pb.buf;
2407
2447
    for(mb_y= s->start_mb_y; mb_y < s->end_mb_y; mb_y++) {
2408
 
//    printf("row %d at %X\n", s->mb_y, (int)s);
2409
2448
        s->mb_x=0;
2410
2449
        s->mb_y= mb_y;
2411
2450
 
2435
2474
            s->mb_y = mb_y;  // moved into loop, can get changed by H.261
2436
2475
            ff_update_block_index(s);
2437
2476
 
2438
 
            if(CONFIG_H261_ENCODER && s->codec_id == CODEC_ID_H261){
 
2477
            if(CONFIG_H261_ENCODER && s->codec_id == AV_CODEC_ID_H261){
2439
2478
                ff_h261_reorder_mb_index(s);
2440
2479
                xy= s->mb_y*s->mb_stride + s->mb_x;
2441
2480
                mb_type= s->mb_type[xy];
2452
2491
                if(s->start_mb_y == mb_y && mb_y > 0 && mb_x==0) is_gob_start=1;
2453
2492
 
2454
2493
                switch(s->codec_id){
2455
 
                case CODEC_ID_H263:
2456
 
                case CODEC_ID_H263P:
 
2494
                case AV_CODEC_ID_H263:
 
2495
                case AV_CODEC_ID_H263P:
2457
2496
                    if(!s->h263_slice_structured)
2458
2497
                        if(s->mb_x || s->mb_y%s->gob_index) is_gob_start=0;
2459
2498
                    break;
2460
 
                case CODEC_ID_MPEG2VIDEO:
 
2499
                case AV_CODEC_ID_MPEG2VIDEO:
2461
2500
                    if(s->mb_x==0 && s->mb_y!=0) is_gob_start=1;
2462
 
                case CODEC_ID_MPEG1VIDEO:
 
2501
                case AV_CODEC_ID_MPEG1VIDEO:
2463
2502
                    if(s->mb_skip_run) is_gob_start=0;
2464
2503
                    break;
2465
2504
                }
2468
2507
                    if(s->start_mb_y != mb_y || mb_x!=0){
2469
2508
                        write_slice_end(s);
2470
2509
 
2471
 
                        if(CONFIG_MPEG4_ENCODER && s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame){
 
2510
                        if(CONFIG_MPEG4_ENCODER && s->codec_id==AV_CODEC_ID_MPEG4 && s->partitioned_frame){
2472
2511
                            ff_mpeg4_init_partitions(s);
2473
2512
                        }
2474
2513
                    }
2490
2529
                        int number_mb = (mb_y - s->resync_mb_y)*s->mb_width + mb_x - s->resync_mb_x;
2491
2530
                        s->avctx->rtp_callback(s->avctx, s->ptr_lastgob, current_packet_size, number_mb);
2492
2531
                    }
 
2532
                    update_mb_info(s, 1);
2493
2533
 
2494
2534
                    switch(s->codec_id){
2495
 
                    case CODEC_ID_MPEG4:
 
2535
                    case AV_CODEC_ID_MPEG4:
2496
2536
                        if (CONFIG_MPEG4_ENCODER) {
2497
2537
                            ff_mpeg4_encode_video_packet_header(s);
2498
2538
                            ff_mpeg4_clean_buffers(s);
2499
2539
                        }
2500
2540
                    break;
2501
 
                    case CODEC_ID_MPEG1VIDEO:
2502
 
                    case CODEC_ID_MPEG2VIDEO:
 
2541
                    case AV_CODEC_ID_MPEG1VIDEO:
 
2542
                    case AV_CODEC_ID_MPEG2VIDEO:
2503
2543
                        if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER) {
2504
2544
                            ff_mpeg1_encode_slice_header(s);
2505
2545
                            ff_mpeg1_clean_buffers(s);
2506
2546
                        }
2507
2547
                    break;
2508
 
                    case CODEC_ID_H263:
2509
 
                    case CODEC_ID_H263P:
 
2548
                    case AV_CODEC_ID_H263:
 
2549
                    case AV_CODEC_ID_H263P:
2510
2550
                        if (CONFIG_H263_ENCODER)
2511
2551
                            ff_h263_encode_gob_header(s, mb_y);
2512
2552
                    break;
2533
2573
            s->mb_skipped=0;
2534
2574
            s->dquant=0; //only for QP_RD
2535
2575
 
2536
 
            if(mb_type & (mb_type-1) || (s->flags & CODEC_FLAG_QP_RD)){ // more than 1 MB type possible or CODEC_FLAG_QP_RD
 
2576
            update_mb_info(s, 0);
 
2577
 
 
2578
            if (mb_type & (mb_type-1) || (s->mpv_flags & FF_MPV_FLAG_QP_RD)) { // more than 1 MB type possible or FF_MPV_FLAG_QP_RD
2537
2579
                int next_block=0;
2538
2580
                int pb_bits_count, pb2_bits_count, tex_pb_bits_count;
2539
2581
 
2670
2712
                    }
2671
2713
                }
2672
2714
 
2673
 
                if((s->flags & CODEC_FLAG_QP_RD) && dmin < INT_MAX){
 
2715
                if ((s->mpv_flags & FF_MPV_FLAG_QP_RD) && dmin < INT_MAX) {
2674
2716
                    if(best_s.mv_type==MV_TYPE_16X16){ //FIXME move 4mv after QPRD
2675
2717
                        const int last_qp= backup_s.qscale;
2676
2718
                        int qpi, qp, dc[6];
2735
2777
                    encode_mb_hq(s, &backup_s, &best_s, CANDIDATE_MB_TYPE_DIRECT, pb, pb2, tex_pb,
2736
2778
                                 &dmin, &next_block, 0, 0);
2737
2779
                }
2738
 
                if(!best_s.mb_intra && s->flags2&CODEC_FLAG2_SKIP_RD){
 
2780
                if (!best_s.mb_intra && s->mpv_flags & FF_MPV_FLAG_SKIP_RD) {
2739
2781
                    int coded=0;
2740
2782
                    for(i=0; i<6; i++)
2741
2783
                        coded |= s->block_last_index[i];
2801
2843
                }
2802
2844
 
2803
2845
                if(s->avctx->mb_decision == FF_MB_DECISION_BITS)
2804
 
                    MPV_decode_mb(s, s->block);
 
2846
                    ff_MPV_decode_mb(s, s->block);
2805
2847
            } else {
2806
2848
                int motion_x = 0, motion_y = 0;
2807
2849
                s->mv_type=MV_TYPE_16X16;
2874
2916
                    s->mb_intra= 0;
2875
2917
                    motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0];
2876
2918
                    motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1];
2877
 
//                    printf(" %d %d ", motion_x, motion_y);
2878
2919
                    break;
2879
2920
                case CANDIDATE_MB_TYPE_FORWARD_I:
2880
2921
                    s->mv_dir = MV_DIR_FORWARD;
2921
2962
                    s->out_format == FMT_H263 && s->pict_type!=AV_PICTURE_TYPE_B)
2922
2963
                    ff_h263_update_motion_val(s);
2923
2964
 
2924
 
                MPV_decode_mb(s, s->block);
 
2965
                ff_MPV_decode_mb(s, s->block);
2925
2966
            }
2926
2967
 
2927
2968
            /* clean the MV table in IPS frames for direct mode in B frames */
2951
2992
                if(CONFIG_H263_ENCODER && s->out_format == FMT_H263)
2952
2993
                    ff_h263_loop_filter(s);
2953
2994
            }
2954
 
//printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_stride, put_bits_count(&s->pb));
 
2995
            av_dlog(s->avctx, "MB %d %d bits\n",
 
2996
                    s->mb_x + s->mb_y * s->mb_stride, put_bits_count(&s->pb));
2955
2997
        }
2956
2998
    }
2957
2999
 
2958
3000
    //not beautiful here but we must write it before flushing so it has to be here
2959
3001
    if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == AV_PICTURE_TYPE_I)
2960
 
        msmpeg4_encode_ext_header(s);
 
3002
        ff_msmpeg4_encode_ext_header(s);
2961
3003
 
2962
3004
    write_slice_end(s);
2963
3005
 
3026
3068
 
3027
3069
    if(s->adaptive_quant){
3028
3070
        switch(s->codec_id){
3029
 
        case CODEC_ID_MPEG4:
 
3071
        case AV_CODEC_ID_MPEG4:
3030
3072
            if (CONFIG_MPEG4_ENCODER)
3031
3073
                ff_clean_mpeg4_qscales(s);
3032
3074
            break;
3033
 
        case CODEC_ID_H263:
3034
 
        case CODEC_ID_H263P:
3035
 
        case CODEC_ID_FLV1:
 
3075
        case AV_CODEC_ID_H263:
 
3076
        case AV_CODEC_ID_H263P:
 
3077
        case AV_CODEC_ID_FLV1:
3036
3078
            if (CONFIG_H263_ENCODER)
3037
3079
                ff_clean_h263_qscales(s);
3038
3080
            break;
3044
3086
        //FIXME broken
3045
3087
    }else
3046
3088
        s->lambda = s->current_picture.f.quality;
3047
 
//printf("%d %d\n", s->avctx->global_quality, s->current_picture.quality);
3048
3089
    update_qscale(s);
3049
3090
    return 0;
3050
3091
}
3066
3107
 
3067
3108
static int encode_picture(MpegEncContext *s, int picture_number)
3068
3109
{
3069
 
    int i;
 
3110
    int i, ret;
3070
3111
    int bits;
3071
3112
    int context_count = s->slice_context_count;
3072
3113
 
3078
3119
 
3079
3120
    /* we need to initialize some time vars before we can encode b-frames */
3080
3121
    // RAL: Condition added for MPEG1VIDEO
3081
 
    if (s->codec_id == CODEC_ID_MPEG1VIDEO || s->codec_id == CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
 
3122
    if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO || s->codec_id == AV_CODEC_ID_MPEG2VIDEO || (s->h263_pred && !s->msmpeg4_version))
3082
3123
        set_frame_distances(s);
3083
 
    if(CONFIG_MPEG4_ENCODER && s->codec_id == CODEC_ID_MPEG4)
 
3124
    if(CONFIG_MPEG4_ENCODER && s->codec_id == AV_CODEC_ID_MPEG4)
3084
3125
        ff_set_mpeg4_time(s);
3085
3126
 
3086
3127
    s->me.scene_change_score=0;
3091
3132
        if(s->msmpeg4_version >= 3) s->no_rounding=1;
3092
3133
        else                        s->no_rounding=0;
3093
3134
    }else if(s->pict_type!=AV_PICTURE_TYPE_B){
3094
 
        if(s->flipflop_rounding || s->codec_id == CODEC_ID_H263P || s->codec_id == CODEC_ID_MPEG4)
 
3135
        if(s->flipflop_rounding || s->codec_id == AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_MPEG4)
3095
3136
            s->no_rounding ^= 1;
3096
3137
    }
3097
3138
 
3109
3150
 
3110
3151
    s->mb_intra=0; //for the rate distortion & bit compare functions
3111
3152
    for(i=1; i<context_count; i++){
3112
 
        ff_update_duplicate_context(s->thread_context[i], s);
 
3153
        ret = ff_update_duplicate_context(s->thread_context[i], s);
 
3154
        if (ret < 0)
 
3155
            return ret;
3113
3156
    }
3114
3157
 
3115
3158
    if(ff_init_me(s)<0)
3147
3190
        s->pict_type= AV_PICTURE_TYPE_I;
3148
3191
        for(i=0; i<s->mb_stride*s->mb_height; i++)
3149
3192
            s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
3150
 
//printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
 
3193
        av_dlog(s, "Scene change detected, encoding as I Frame %d %d\n",
 
3194
                s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
3151
3195
    }
3152
3196
 
3153
3197
    if(!s->umvplus){
3245
3289
            ff_h261_encode_picture_header(s, picture_number);
3246
3290
        break;
3247
3291
    case FMT_H263:
3248
 
        if (CONFIG_WMV2_ENCODER && s->codec_id == CODEC_ID_WMV2)
 
3292
        if (CONFIG_WMV2_ENCODER && s->codec_id == AV_CODEC_ID_WMV2)
3249
3293
            ff_wmv2_encode_picture_header(s, picture_number);
3250
3294
        else if (CONFIG_MSMPEG4_ENCODER && s->msmpeg4_version)
3251
 
            msmpeg4_encode_picture_header(s, picture_number);
 
3295
            ff_msmpeg4_encode_picture_header(s, picture_number);
3252
3296
        else if (CONFIG_MPEG4_ENCODER && s->h263_pred)
3253
 
            mpeg4_encode_picture_header(s, picture_number);
3254
 
        else if (CONFIG_RV10_ENCODER && s->codec_id == CODEC_ID_RV10)
3255
 
            rv10_encode_picture_header(s, picture_number);
3256
 
        else if (CONFIG_RV20_ENCODER && s->codec_id == CODEC_ID_RV20)
3257
 
            rv20_encode_picture_header(s, picture_number);
3258
 
        else if (CONFIG_FLV_ENCODER && s->codec_id == CODEC_ID_FLV1)
 
3297
            ff_mpeg4_encode_picture_header(s, picture_number);
 
3298
        else if (CONFIG_RV10_ENCODER && s->codec_id == AV_CODEC_ID_RV10)
 
3299
            ff_rv10_encode_picture_header(s, picture_number);
 
3300
        else if (CONFIG_RV20_ENCODER && s->codec_id == AV_CODEC_ID_RV20)
 
3301
            ff_rv20_encode_picture_header(s, picture_number);
 
3302
        else if (CONFIG_FLV_ENCODER && s->codec_id == AV_CODEC_ID_FLV1)
3259
3303
            ff_flv_encode_picture_header(s, picture_number);
3260
3304
        else if (CONFIG_H263_ENCODER)
3261
3305
            ff_h263_encode_picture_header(s, picture_number);
3262
3306
        break;
3263
3307
    case FMT_MPEG1:
3264
3308
        if (CONFIG_MPEG1VIDEO_ENCODER || CONFIG_MPEG2VIDEO_ENCODER)
3265
 
            mpeg1_encode_picture_header(s, picture_number);
 
3309
            ff_mpeg1_encode_picture_header(s, picture_number);
3266
3310
        break;
3267
3311
    case FMT_H264:
3268
3312
        break;
3428
3472
        int dct_coeff= FFABS(block[ scantable[i] ]);
3429
3473
        int best_score=256*256*256*120;
3430
3474
 
3431
 
        if (   s->dsp.fdct == fdct_ifast
3432
 
#ifndef FAAN_POSTSCALE
3433
 
            || s->dsp.fdct == ff_faandct
3434
 
#endif
3435
 
           )
 
3475
        if (s->dsp.fdct == ff_fdct_ifast)
3436
3476
            dct_coeff= (dct_coeff*ff_inv_aanscales[ scantable[i] ]) >> 12;
3437
3477
        zero_distortion= dct_coeff*dct_coeff;
3438
3478
 
3749
3789
#ifdef REFINE_STATS
3750
3790
{START_TIMER
3751
3791
#endif
3752
 
        analyze_gradient = last_non_zero > 2 || s->avctx->quantizer_noise_shaping >= 3;
 
3792
        analyze_gradient = last_non_zero > 2 || s->quantizer_noise_shaping >= 3;
3753
3793
 
3754
3794
        if(analyze_gradient){
3755
3795
#ifdef REFINE_STATS
3807
3847
            const int level= block[j];
3808
3848
            int change, old_coeff;
3809
3849
 
3810
 
            if(s->avctx->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
 
3850
            if(s->quantizer_noise_shaping < 3 && i > last_non_zero + 1)
3811
3851
                break;
3812
3852
 
3813
3853
            if(level){
3825
3865
                int score, new_coeff, unquant_change;
3826
3866
 
3827
3867
                score=0;
3828
 
                if(s->avctx->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
 
3868
                if(s->quantizer_noise_shaping < 2 && FFABS(new_level) > FFABS(level))
3829
3869
                   continue;
3830
3870
 
3831
3871
                if(new_level){
3998
4038
    return last_non_zero;
3999
4039
}
4000
4040
 
4001
 
int dct_quantize_c(MpegEncContext *s,
 
4041
int ff_dct_quantize_c(MpegEncContext *s,
4002
4042
                        DCTELEM *block, int n,
4003
4043
                        int qscale, int *overflow)
4004
4044
{
4081
4121
#define OFFSET(x) offsetof(MpegEncContext, x)
4082
4122
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
4083
4123
static const AVOption h263_options[] = {
4084
 
    { "obmc",         "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
4085
 
    { "structured_slices","Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE},
 
4124
    { "obmc",         "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
 
4125
    { "structured_slices","Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE},
 
4126
    { "mb_info",      "emit macroblock info for RFC 2190 packetization, the parameter value is the maximum payload size", OFFSET(mb_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
 
4127
    FF_MPV_COMMON_OPTS
4086
4128
    { NULL },
4087
4129
};
4088
4130
 
4096
4138
AVCodec ff_h263_encoder = {
4097
4139
    .name           = "h263",
4098
4140
    .type           = AVMEDIA_TYPE_VIDEO,
4099
 
    .id             = CODEC_ID_H263,
 
4141
    .id             = AV_CODEC_ID_H263,
4100
4142
    .priv_data_size = sizeof(MpegEncContext),
4101
 
    .init           = MPV_encode_init,
4102
 
    .encode         = MPV_encode_picture,
4103
 
    .close          = MPV_encode_end,
4104
 
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
 
4143
    .init           = ff_MPV_encode_init,
 
4144
    .encode2        = ff_MPV_encode_picture,
 
4145
    .close          = ff_MPV_encode_end,
 
4146
    .pix_fmts= (const enum AVPixelFormat[]){AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE},
4105
4147
    .long_name= NULL_IF_CONFIG_SMALL("H.263 / H.263-1996"),
4106
4148
    .priv_class     = &h263_class,
4107
4149
};
4108
4150
 
4109
4151
static const AVOption h263p_options[] = {
4110
 
    { "umv",        "Use unlimited motion vectors.",    OFFSET(umvplus), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
4111
 
    { "aiv",        "Use alternative inter VLC.",       OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
4112
 
    { "obmc",       "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE },
4113
 
    { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_INT, { 0 }, 0, 1, VE},
 
4152
    { "umv",        "Use unlimited motion vectors.",    OFFSET(umvplus), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
 
4153
    { "aiv",        "Use alternative inter VLC.",       OFFSET(alt_inter_vlc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
 
4154
    { "obmc",       "use overlapped block motion compensation.", OFFSET(obmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
 
4155
    { "structured_slices", "Write slice start position at every GOB header instead of just GOB number.", OFFSET(h263_slice_structured), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE},
 
4156
    FF_MPV_COMMON_OPTS
4114
4157
    { NULL },
4115
4158
};
4116
4159
static const AVClass h263p_class = {
4123
4166
AVCodec ff_h263p_encoder = {
4124
4167
    .name           = "h263p",
4125
4168
    .type           = AVMEDIA_TYPE_VIDEO,
4126
 
    .id             = CODEC_ID_H263P,
 
4169
    .id             = AV_CODEC_ID_H263P,
4127
4170
    .priv_data_size = sizeof(MpegEncContext),
4128
 
    .init           = MPV_encode_init,
4129
 
    .encode         = MPV_encode_picture,
4130
 
    .close          = MPV_encode_end,
4131
 
    .capabilities = CODEC_CAP_SLICE_THREADS,
4132
 
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
4133
 
    .long_name= NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
 
4171
    .init           = ff_MPV_encode_init,
 
4172
    .encode2        = ff_MPV_encode_picture,
 
4173
    .close          = ff_MPV_encode_end,
 
4174
    .capabilities   = CODEC_CAP_SLICE_THREADS,
 
4175
    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
 
4176
    .long_name      = NULL_IF_CONFIG_SMALL("H.263+ / H.263-1998 / H.263 version 2"),
4134
4177
    .priv_class     = &h263p_class,
4135
4178
};
4136
4179
 
 
4180
FF_MPV_GENERIC_CLASS(msmpeg4v2)
 
4181
 
4137
4182
AVCodec ff_msmpeg4v2_encoder = {
4138
4183
    .name           = "msmpeg4v2",
4139
4184
    .type           = AVMEDIA_TYPE_VIDEO,
4140
 
    .id             = CODEC_ID_MSMPEG4V2,
 
4185
    .id             = AV_CODEC_ID_MSMPEG4V2,
4141
4186
    .priv_data_size = sizeof(MpegEncContext),
4142
 
    .init           = MPV_encode_init,
4143
 
    .encode         = MPV_encode_picture,
4144
 
    .close          = MPV_encode_end,
4145
 
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
4146
 
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
 
4187
    .init           = ff_MPV_encode_init,
 
4188
    .encode2        = ff_MPV_encode_picture,
 
4189
    .close          = ff_MPV_encode_end,
 
4190
    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
 
4191
    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 2"),
 
4192
    .priv_class     = &msmpeg4v2_class,
4147
4193
};
4148
4194
 
 
4195
FF_MPV_GENERIC_CLASS(msmpeg4v3)
 
4196
 
4149
4197
AVCodec ff_msmpeg4v3_encoder = {
4150
4198
    .name           = "msmpeg4",
4151
4199
    .type           = AVMEDIA_TYPE_VIDEO,
4152
 
    .id             = CODEC_ID_MSMPEG4V3,
 
4200
    .id             = AV_CODEC_ID_MSMPEG4V3,
4153
4201
    .priv_data_size = sizeof(MpegEncContext),
4154
 
    .init           = MPV_encode_init,
4155
 
    .encode         = MPV_encode_picture,
4156
 
    .close          = MPV_encode_end,
4157
 
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
4158
 
    .long_name= NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
 
4202
    .init           = ff_MPV_encode_init,
 
4203
    .encode2        = ff_MPV_encode_picture,
 
4204
    .close          = ff_MPV_encode_end,
 
4205
    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
 
4206
    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2 Microsoft variant version 3"),
 
4207
    .priv_class     = &msmpeg4v3_class,
4159
4208
};
4160
4209
 
 
4210
FF_MPV_GENERIC_CLASS(wmv1)
 
4211
 
4161
4212
AVCodec ff_wmv1_encoder = {
4162
4213
    .name           = "wmv1",
4163
4214
    .type           = AVMEDIA_TYPE_VIDEO,
4164
 
    .id             = CODEC_ID_WMV1,
 
4215
    .id             = AV_CODEC_ID_WMV1,
4165
4216
    .priv_data_size = sizeof(MpegEncContext),
4166
 
    .init           = MPV_encode_init,
4167
 
    .encode         = MPV_encode_picture,
4168
 
    .close          = MPV_encode_end,
4169
 
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
4170
 
    .long_name= NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
 
4217
    .init           = ff_MPV_encode_init,
 
4218
    .encode2        = ff_MPV_encode_picture,
 
4219
    .close          = ff_MPV_encode_end,
 
4220
    .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
 
4221
    .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 7"),
 
4222
    .priv_class     = &wmv1_class,
4171
4223
};