~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.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:
27
27
 * The simplest mpeg encoder (well, it was the simplest!).
28
28
 */
29
29
 
30
 
#include "libavutil/intmath.h"
31
30
#include "libavutil/imgutils.h"
32
31
#include "avcodec.h"
33
32
#include "dsputil.h"
34
33
#include "internal.h"
 
34
#include "mathops.h"
35
35
#include "mpegvideo.h"
36
 
#include "mpegvideo_common.h"
37
36
#include "mjpegenc.h"
38
37
#include "msmpeg4.h"
39
 
#include "faandct.h"
40
38
#include "xvmc_internal.h"
41
39
#include "thread.h"
42
40
#include <limits.h>
127
125
    mpeg2_dc_scale_table3,
128
126
};
129
127
 
130
 
const enum PixelFormat ff_pixfmt_list_420[] = {
131
 
    PIX_FMT_YUV420P,
132
 
    PIX_FMT_NONE
 
128
const enum AVPixelFormat ff_pixfmt_list_420[] = {
 
129
    AV_PIX_FMT_YUV420P,
 
130
    AV_PIX_FMT_NONE
133
131
};
134
132
 
135
 
const enum PixelFormat ff_hwaccel_pixfmt_list_420[] = {
136
 
    PIX_FMT_DXVA2_VLD,
137
 
    PIX_FMT_VAAPI_VLD,
138
 
    PIX_FMT_VDA_VLD,
139
 
    PIX_FMT_YUV420P,
140
 
    PIX_FMT_NONE
 
133
const enum AVPixelFormat ff_hwaccel_pixfmt_list_420[] = {
 
134
    AV_PIX_FMT_DXVA2_VLD,
 
135
    AV_PIX_FMT_VAAPI_VLD,
 
136
    AV_PIX_FMT_VDA_VLD,
 
137
    AV_PIX_FMT_YUV420P,
 
138
    AV_PIX_FMT_NONE
141
139
};
142
140
 
143
141
const uint8_t *avpriv_mpv_find_start_code(const uint8_t *restrict p,
176
174
/* init common dct for both encoder and decoder */
177
175
av_cold int ff_dct_common_init(MpegEncContext *s)
178
176
{
179
 
    dsputil_init(&s->dsp, s->avctx);
 
177
    ff_dsputil_init(&s->dsp, s->avctx);
 
178
    ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
180
179
 
181
180
    s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
182
181
    s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
187
186
        s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
188
187
    s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
189
188
 
190
 
#if HAVE_MMX
191
 
    MPV_common_init_mmx(s);
 
189
#if ARCH_X86
 
190
    ff_MPV_common_init_x86(s);
192
191
#elif ARCH_ALPHA
193
 
    MPV_common_init_axp(s);
194
 
#elif CONFIG_MLIB
195
 
    MPV_common_init_mlib(s);
196
 
#elif HAVE_MMI
197
 
    MPV_common_init_mmi(s);
 
192
    ff_MPV_common_init_axp(s);
198
193
#elif ARCH_ARM
199
 
    MPV_common_init_arm(s);
 
194
    ff_MPV_common_init_arm(s);
200
195
#elif HAVE_ALTIVEC
201
 
    MPV_common_init_altivec(s);
 
196
    ff_MPV_common_init_altivec(s);
202
197
#elif ARCH_BFIN
203
 
    MPV_common_init_bfin(s);
 
198
    ff_MPV_common_init_bfin(s);
204
199
#endif
205
200
 
206
201
    /* load & permutate scantables
230
225
 */
231
226
static void free_frame_buffer(MpegEncContext *s, Picture *pic)
232
227
{
233
 
    /* Windows Media Image codecs allocate internal buffers with different
234
 
     * dimensions; ignore user defined callbacks for these
235
 
     */
236
 
    if (s->codec_id != CODEC_ID_WMV3IMAGE && s->codec_id != CODEC_ID_VC1IMAGE)
237
 
        ff_thread_release_buffer(s->avctx, (AVFrame *) pic);
 
228
    /* WM Image / Screen codecs allocate internal buffers with different
 
229
     * dimensions / colorspaces; ignore user-defined callbacks for these. */
 
230
    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
 
231
        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
 
232
        s->codec_id != AV_CODEC_ID_MSS2)
 
233
        ff_thread_release_buffer(s->avctx, &pic->f);
238
234
    else
239
 
        avcodec_default_release_buffer(s->avctx, (AVFrame *) pic);
 
235
        avcodec_default_release_buffer(s->avctx, &pic->f);
240
236
    av_freep(&pic->f.hwaccel_picture_private);
241
237
}
242
238
 
 
239
int ff_mpv_frame_size_alloc(MpegEncContext *s, int linesize)
 
240
{
 
241
    int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
 
242
 
 
243
    // edge emu needs blocksize + filter length - 1
 
244
    // (= 17x17 for  halfpel / 21x21 for  h264)
 
245
    // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
 
246
    // at uvlinesize. It supports only YUV420 so 24x24 is enough
 
247
    // linesize * interlaced * MBsize
 
248
    FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24,
 
249
                      fail);
 
250
 
 
251
    FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 3,
 
252
                      fail)
 
253
    s->me.temp         = s->me.scratchpad;
 
254
    s->rd_scratchpad   = s->me.scratchpad;
 
255
    s->b_scratchpad    = s->me.scratchpad;
 
256
    s->obmc_scratchpad = s->me.scratchpad + 16;
 
257
 
 
258
    return 0;
 
259
fail:
 
260
    av_freep(&s->edge_emu_buffer);
 
261
    return AVERROR(ENOMEM);
 
262
}
 
263
 
243
264
/**
244
265
 * Allocate a frame buffer
245
266
 */
246
267
static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
247
268
{
248
 
    int r;
 
269
    int r, ret;
249
270
 
250
271
    if (s->avctx->hwaccel) {
251
272
        assert(!pic->f.hwaccel_picture_private);
258
279
        }
259
280
    }
260
281
 
261
 
    if (s->codec_id != CODEC_ID_WMV3IMAGE && s->codec_id != CODEC_ID_VC1IMAGE)
262
 
        r = ff_thread_get_buffer(s->avctx, (AVFrame *) pic);
 
282
    if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
 
283
        s->codec_id != AV_CODEC_ID_VC1IMAGE  &&
 
284
        s->codec_id != AV_CODEC_ID_MSS2)
 
285
        r = ff_thread_get_buffer(s->avctx, &pic->f);
263
286
    else
264
 
        r = avcodec_default_get_buffer(s->avctx, (AVFrame *) pic);
 
287
        r = avcodec_default_get_buffer(s->avctx, &pic->f);
265
288
 
266
289
    if (r < 0 || !pic->f.type || !pic->f.data[0]) {
267
290
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %d %p)\n",
285
308
        return -1;
286
309
    }
287
310
 
 
311
    if (!s->edge_emu_buffer &&
 
312
        (ret = ff_mpv_frame_size_alloc(s, pic->f.linesize[0])) < 0) {
 
313
        av_log(s->avctx, AV_LOG_ERROR,
 
314
               "get_buffer() failed to allocate context scratch buffers.\n");
 
315
        free_frame_buffer(s, pic);
 
316
        return ret;
 
317
    }
 
318
 
288
319
    return 0;
289
320
}
290
321
 
394
425
    av_freep(&pic->mb_mean);
395
426
    av_freep(&pic->f.mbskip_table);
396
427
    av_freep(&pic->qscale_table_base);
 
428
    pic->f.qscale_table = NULL;
397
429
    av_freep(&pic->mb_type_base);
 
430
    pic->f.mb_type = NULL;
398
431
    av_freep(&pic->f.dct_coeff);
399
432
    av_freep(&pic->f.pan_scan);
400
433
    pic->f.mb_type = NULL;
401
434
    for (i = 0; i < 2; i++) {
402
435
        av_freep(&pic->motion_val_base[i]);
403
436
        av_freep(&pic->f.ref_index[i]);
 
437
        pic->f.motion_val[i] = NULL;
404
438
    }
405
439
 
406
440
    if (pic->f.type == FF_BUFFER_TYPE_SHARED) {
419
453
    int yc_size = y_size + 2 * c_size;
420
454
    int i;
421
455
 
422
 
    // edge emu needs blocksize + filter length - 1
423
 
    // (= 17x17 for  halfpel / 21x21 for  h264)
424
 
    FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer,
425
 
                      (s->width + 64) * 2 * 21 * 2, fail);    // (width + edge + align)*interlaced*MBsize*tolerance
 
456
    s->edge_emu_buffer =
 
457
    s->me.scratchpad   =
 
458
    s->me.temp         =
 
459
    s->rd_scratchpad   =
 
460
    s->b_scratchpad    =
 
461
    s->obmc_scratchpad = NULL;
426
462
 
427
 
    // FIXME should be linesize instead of s->width * 2
428
 
    // but that is not known before get_buffer()
429
 
    FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad,
430
 
                      (s->width + 64) * 4 * 16 * 2 * sizeof(uint8_t), fail)
431
 
    s->me.temp         = s->me.scratchpad;
432
 
    s->rd_scratchpad   = s->me.scratchpad;
433
 
    s->b_scratchpad    = s->me.scratchpad;
434
 
    s->obmc_scratchpad = s->me.scratchpad + 16;
435
463
    if (s->encoding) {
436
464
        FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
437
465
                          ME_MAP_SIZE * sizeof(uint32_t), fail)
460
488
 
461
489
    return 0;
462
490
fail:
463
 
    return -1; // free() through MPV_common_end()
 
491
    return -1; // free() through ff_MPV_common_end()
464
492
}
465
493
 
466
494
static void free_duplicate_context(MpegEncContext *s)
510
538
#undef COPY
511
539
}
512
540
 
513
 
void ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
 
541
int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
514
542
{
515
543
    MpegEncContext bak;
516
 
    int i;
 
544
    int i, ret;
517
545
    // FIXME copy only needed parts
518
546
    // START_TIMER
519
547
    backup_duplicate_context(&bak, dst);
522
550
    for (i = 0; i < 12; i++) {
523
551
        dst->pblocks[i] = &dst->block[i];
524
552
    }
 
553
    if (!dst->edge_emu_buffer &&
 
554
        (ret = ff_mpv_frame_size_alloc(dst, dst->linesize)) < 0) {
 
555
        av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
 
556
               "scratch buffers.\n");
 
557
        return ret;
 
558
    }
525
559
    // STOP_TIMER("update_duplicate_context")
526
560
    // about 10k cycles / 0.01 sec for  1000frames on 1ghz with 2 threads
 
561
    return 0;
527
562
}
528
563
 
529
564
int ff_mpeg_update_thread_context(AVCodecContext *dst,
530
565
                                  const AVCodecContext *src)
531
566
{
 
567
    int i;
532
568
    MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
533
569
 
534
570
    if (dst == src || !s1->context_initialized)
545
581
        s->bitstream_buffer      = NULL;
546
582
        s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
547
583
 
548
 
        MPV_common_init(s);
 
584
        ff_MPV_common_init(s);
 
585
    }
 
586
 
 
587
    if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
 
588
        int err;
 
589
        s->context_reinit = 0;
 
590
        s->height = s1->height;
 
591
        s->width  = s1->width;
 
592
        if ((err = ff_MPV_common_frame_size_change(s)) < 0)
 
593
            return err;
549
594
    }
550
595
 
551
596
    s->avctx->coded_height  = s1->avctx->coded_height;
561
606
    memcpy(&s->last_picture, &s1->last_picture,
562
607
           (char *) &s1->last_picture_ptr - (char *) &s1->last_picture);
563
608
 
 
609
    // reset s->picture[].f.extended_data to s->picture[].f.data
 
610
    for (i = 0; i < s->picture_count; i++)
 
611
        s->picture[i].f.extended_data = s->picture[i].f.data;
 
612
 
564
613
    s->last_picture_ptr    = REBASE_PICTURE(s1->last_picture_ptr,    s, s1);
565
614
    s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
566
615
    s->next_picture_ptr    = REBASE_PICTURE(s1->next_picture_ptr,    s, s1);
576
625
    // B-frame info
577
626
    s->max_b_frames = s1->max_b_frames;
578
627
    s->low_delay    = s1->low_delay;
579
 
    s->dropable     = s1->dropable;
 
628
    s->droppable    = s1->droppable;
580
629
 
581
630
    // DivX handling (doesn't work)
582
631
    s->divx_packed  = s1->divx_packed;
594
643
               FF_INPUT_BUFFER_PADDING_SIZE);
595
644
    }
596
645
 
 
646
    // linesize dependend scratch buffer allocation
 
647
    if (!s->edge_emu_buffer)
 
648
        if (s1->linesize) {
 
649
            if (ff_mpv_frame_size_alloc(s, s1->linesize) < 0) {
 
650
                av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
 
651
                       "scratch buffers.\n");
 
652
                return AVERROR(ENOMEM);
 
653
            }
 
654
        } else {
 
655
            av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
 
656
                   "be allocated due to unknown size.\n");
 
657
            return AVERROR_BUG;
 
658
        }
 
659
 
597
660
    // MPEG2/interlacing info
598
661
    memcpy(&s->progressive_sequence, &s1->progressive_sequence,
599
662
           (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
617
680
 * The changed fields will not depend upon the
618
681
 * prior state of the MpegEncContext.
619
682
 */
620
 
void MPV_common_defaults(MpegEncContext *s)
 
683
void ff_MPV_common_defaults(MpegEncContext *s)
621
684
{
622
685
    s->y_dc_scale_table      =
623
686
    s->c_dc_scale_table      = ff_mpeg1_dc_scale_table;
646
709
 * the changed fields will not depend upon
647
710
 * the prior state of the MpegEncContext.
648
711
 */
649
 
void MPV_decode_defaults(MpegEncContext *s)
650
 
{
651
 
    MPV_common_defaults(s);
 
712
void ff_MPV_decode_defaults(MpegEncContext *s)
 
713
{
 
714
    ff_MPV_common_defaults(s);
 
715
}
 
716
 
 
717
/**
 
718
 * Initialize and allocates MpegEncContext fields dependent on the resolution.
 
719
 */
 
720
static int init_context_frame(MpegEncContext *s)
 
721
{
 
722
    int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
 
723
 
 
724
    s->mb_width   = (s->width + 15) / 16;
 
725
    s->mb_stride  = s->mb_width + 1;
 
726
    s->b8_stride  = s->mb_width * 2 + 1;
 
727
    s->b4_stride  = s->mb_width * 4 + 1;
 
728
    mb_array_size = s->mb_height * s->mb_stride;
 
729
    mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
 
730
 
 
731
    /* set default edge pos, will be overriden
 
732
     * in decode_header if needed */
 
733
    s->h_edge_pos = s->mb_width * 16;
 
734
    s->v_edge_pos = s->mb_height * 16;
 
735
 
 
736
    s->mb_num     = s->mb_width * s->mb_height;
 
737
 
 
738
    s->block_wrap[0] =
 
739
    s->block_wrap[1] =
 
740
    s->block_wrap[2] =
 
741
    s->block_wrap[3] = s->b8_stride;
 
742
    s->block_wrap[4] =
 
743
    s->block_wrap[5] = s->mb_stride;
 
744
 
 
745
    y_size  = s->b8_stride * (2 * s->mb_height + 1);
 
746
    c_size  = s->mb_stride * (s->mb_height + 1);
 
747
    yc_size = y_size + 2   * c_size;
 
748
 
 
749
    FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
 
750
                      fail); // error ressilience code looks cleaner with this
 
751
    for (y = 0; y < s->mb_height; y++)
 
752
        for (x = 0; x < s->mb_width; x++)
 
753
            s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
 
754
 
 
755
    s->mb_index2xy[s->mb_height * s->mb_width] =
 
756
        (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
 
757
 
 
758
    if (s->encoding) {
 
759
        /* Allocate MV tables */
 
760
        FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
 
761
                          mv_table_size * 2 * sizeof(int16_t), fail);
 
762
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
 
763
                          mv_table_size * 2 * sizeof(int16_t), fail);
 
764
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
 
765
                          mv_table_size * 2 * sizeof(int16_t), fail);
 
766
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
 
767
                          mv_table_size * 2 * sizeof(int16_t), fail);
 
768
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
 
769
                          mv_table_size * 2 * sizeof(int16_t), fail);
 
770
        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
 
771
                          mv_table_size * 2 * sizeof(int16_t), fail);
 
772
        s->p_mv_table            = s->p_mv_table_base + s->mb_stride + 1;
 
773
        s->b_forw_mv_table       = s->b_forw_mv_table_base + s->mb_stride + 1;
 
774
        s->b_back_mv_table       = s->b_back_mv_table_base + s->mb_stride + 1;
 
775
        s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
 
776
                                   s->mb_stride + 1;
 
777
        s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
 
778
                                   s->mb_stride + 1;
 
779
        s->b_direct_mv_table     = s->b_direct_mv_table_base + s->mb_stride + 1;
 
780
 
 
781
        /* Allocate MB type table */
 
782
        FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
 
783
                          sizeof(uint16_t), fail); // needed for encoding
 
784
 
 
785
        FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
 
786
                          sizeof(int), fail);
 
787
 
 
788
        FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
 
789
                         mb_array_size * sizeof(float), fail);
 
790
        FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
 
791
                         mb_array_size * sizeof(float), fail);
 
792
 
 
793
    }
 
794
 
 
795
    FF_ALLOC_OR_GOTO(s->avctx, s->er_temp_buffer,
 
796
                     mb_array_size * sizeof(uint8_t), fail);
 
797
    FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
 
798
                      mb_array_size * sizeof(uint8_t), fail);
 
799
 
 
800
    if (s->codec_id == AV_CODEC_ID_MPEG4 ||
 
801
        (s->flags & CODEC_FLAG_INTERLACED_ME)) {
 
802
        /* interlaced direct mode decoding tables */
 
803
        for (i = 0; i < 2; i++) {
 
804
            int j, k;
 
805
            for (j = 0; j < 2; j++) {
 
806
                for (k = 0; k < 2; k++) {
 
807
                    FF_ALLOCZ_OR_GOTO(s->avctx,
 
808
                                      s->b_field_mv_table_base[i][j][k],
 
809
                                      mv_table_size * 2 * sizeof(int16_t),
 
810
                                      fail);
 
811
                    s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
 
812
                                                   s->mb_stride + 1;
 
813
                }
 
814
                FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
 
815
                                  mb_array_size * 2 * sizeof(uint8_t), fail);
 
816
                FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
 
817
                                  mv_table_size * 2 * sizeof(int16_t), fail);
 
818
                s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
 
819
                                            + s->mb_stride + 1;
 
820
            }
 
821
            FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
 
822
                              mb_array_size * 2 * sizeof(uint8_t), fail);
 
823
        }
 
824
    }
 
825
    if (s->out_format == FMT_H263) {
 
826
        /* cbp values */
 
827
        FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
 
828
        s->coded_block = s->coded_block_base + s->b8_stride + 1;
 
829
 
 
830
        /* cbp, ac_pred, pred_dir */
 
831
        FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
 
832
                          mb_array_size * sizeof(uint8_t), fail);
 
833
        FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
 
834
                          mb_array_size * sizeof(uint8_t), fail);
 
835
    }
 
836
 
 
837
    if (s->h263_pred || s->h263_plus || !s->encoding) {
 
838
        /* dc values */
 
839
        // MN: we need these for  error resilience of intra-frames
 
840
        FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
 
841
                          yc_size * sizeof(int16_t), fail);
 
842
        s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
 
843
        s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
 
844
        s->dc_val[2] = s->dc_val[1] + c_size;
 
845
        for (i = 0; i < yc_size; i++)
 
846
            s->dc_val_base[i] = 1024;
 
847
    }
 
848
 
 
849
    /* which mb is a intra block */
 
850
    FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
 
851
    memset(s->mbintra_table, 1, mb_array_size);
 
852
 
 
853
    /* init macroblock skip table */
 
854
    FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
 
855
    // Note the + 1 is for  a quicker mpeg4 slice_end detection
 
856
 
 
857
    if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
 
858
        s->avctx->debug_mv) {
 
859
        s->visualization_buffer[0] = av_malloc((s->mb_width * 16 +
 
860
                    2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
 
861
        s->visualization_buffer[1] = av_malloc((s->mb_width * 16 +
 
862
                    2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
 
863
        s->visualization_buffer[2] = av_malloc((s->mb_width * 16 +
 
864
                    2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
 
865
    }
 
866
 
 
867
    return 0;
 
868
fail:
 
869
    return AVERROR(ENOMEM);
652
870
}
653
871
 
654
872
/**
655
873
 * init common structure for both encoder and decoder.
656
874
 * this assumes that some variables like width/height are already set
657
875
 */
658
 
av_cold int MPV_common_init(MpegEncContext *s)
 
876
av_cold int ff_MPV_common_init(MpegEncContext *s)
659
877
{
660
 
    int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
 
878
    int i;
661
879
    int nb_slices = (HAVE_THREADS &&
662
880
                     s->avctx->active_thread_type & FF_THREAD_SLICE) ?
663
881
                    s->avctx->thread_count : 1;
665
883
    if (s->encoding && s->avctx->slices)
666
884
        nb_slices = s->avctx->slices;
667
885
 
668
 
    if (s->codec_id == CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
 
886
    if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
669
887
        s->mb_height = (s->height + 31) / 32 * 2;
670
 
    else if (s->codec_id != CODEC_ID_H264)
 
888
    else if (s->codec_id != AV_CODEC_ID_H264)
671
889
        s->mb_height = (s->height + 15) / 16;
672
890
 
673
 
    if (s->avctx->pix_fmt == PIX_FMT_NONE) {
 
891
    if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
674
892
        av_log(s->avctx, AV_LOG_ERROR,
675
 
               "decoding to PIX_FMT_NONE is not supported.\n");
 
893
               "decoding to AV_PIX_FMT_NONE is not supported.\n");
676
894
        return -1;
677
895
    }
678
896
 
697
915
    s->flags2 = s->avctx->flags2;
698
916
 
699
917
    if (s->width && s->height) {
700
 
        s->mb_width   = (s->width + 15) / 16;
701
 
        s->mb_stride  = s->mb_width + 1;
702
 
        s->b8_stride  = s->mb_width * 2 + 1;
703
 
        s->b4_stride  = s->mb_width * 4 + 1;
704
 
        mb_array_size = s->mb_height * s->mb_stride;
705
 
        mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
706
 
 
707
918
        /* set chroma shifts */
708
 
        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift,
709
 
                                      &s->chroma_y_shift);
710
 
 
711
 
        /* set default edge pos, will be overriden
712
 
         * in decode_header if needed */
713
 
        s->h_edge_pos = s->mb_width * 16;
714
 
        s->v_edge_pos = s->mb_height * 16;
715
 
 
716
 
        s->mb_num     = s->mb_width * s->mb_height;
717
 
 
718
 
        s->block_wrap[0] =
719
 
        s->block_wrap[1] =
720
 
        s->block_wrap[2] =
721
 
        s->block_wrap[3] = s->b8_stride;
722
 
        s->block_wrap[4] =
723
 
        s->block_wrap[5] = s->mb_stride;
724
 
 
725
 
        y_size  = s->b8_stride * (2 * s->mb_height + 1);
726
 
        c_size  = s->mb_stride * (s->mb_height + 1);
727
 
        yc_size = y_size + 2   * c_size;
 
919
        av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
 
920
                                         &s->chroma_x_shift,
 
921
                                         &s->chroma_y_shift);
728
922
 
729
923
        /* convert fourcc to upper case */
730
924
        s->codec_tag          = avpriv_toupper4(s->avctx->codec_tag);
731
925
 
732
926
        s->stream_codec_tag   = avpriv_toupper4(s->avctx->stream_codec_tag);
733
927
 
734
 
        s->avctx->coded_frame = (AVFrame *)&s->current_picture;
735
 
 
736
 
        FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
737
 
                          fail); // error ressilience code looks cleaner with this
738
 
        for (y = 0; y < s->mb_height; y++)
739
 
            for (x = 0; x < s->mb_width; x++)
740
 
                s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
741
 
 
742
 
        s->mb_index2xy[s->mb_height * s->mb_width] =
743
 
                       (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
 
928
        s->avctx->coded_frame = &s->current_picture.f;
744
929
 
745
930
        if (s->encoding) {
746
 
            /* Allocate MV tables */
747
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
748
 
                              mv_table_size * 2 * sizeof(int16_t), fail);
749
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
750
 
                              mv_table_size * 2 * sizeof(int16_t), fail);
751
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
752
 
                              mv_table_size * 2 * sizeof(int16_t), fail);
753
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
754
 
                              mv_table_size * 2 * sizeof(int16_t), fail);
755
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
756
 
                              mv_table_size * 2 * sizeof(int16_t), fail);
757
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
758
 
                              mv_table_size * 2 * sizeof(int16_t), fail);
759
 
            s->p_mv_table            = s->p_mv_table_base +
760
 
                                       s->mb_stride + 1;
761
 
            s->b_forw_mv_table       = s->b_forw_mv_table_base +
762
 
                                       s->mb_stride + 1;
763
 
            s->b_back_mv_table       = s->b_back_mv_table_base +
764
 
                                       s->mb_stride + 1;
765
 
            s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
766
 
                                       s->mb_stride + 1;
767
 
            s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
768
 
                                       s->mb_stride + 1;
769
 
            s->b_direct_mv_table     = s->b_direct_mv_table_base +
770
 
                                       s->mb_stride + 1;
771
 
 
772
931
            if (s->msmpeg4_version) {
773
932
                FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
774
933
                                  2 * 2 * (MAX_LEVEL + 1) *
776
935
            }
777
936
            FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
778
937
 
779
 
            /* Allocate MB type table */
780
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
781
 
                              sizeof(uint16_t), fail); // needed for encoding
782
 
 
783
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
784
 
                              sizeof(int), fail);
785
 
 
786
938
            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,
787
939
                              64 * 32   * sizeof(int), fail);
788
940
            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,
807
959
    FF_ALLOCZ_OR_GOTO(s->avctx, s->picture,
808
960
                      s->picture_count * sizeof(Picture), fail);
809
961
    for (i = 0; i < s->picture_count; i++) {
810
 
        avcodec_get_frame_defaults((AVFrame *) &s->picture[i]);
 
962
        avcodec_get_frame_defaults(&s->picture[i].f);
811
963
    }
812
964
 
813
965
    if (s->width && s->height) {
814
 
        FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
815
 
                          mb_array_size * sizeof(uint8_t), fail);
816
 
 
817
 
        if (s->codec_id == CODEC_ID_MPEG4 ||
818
 
            (s->flags & CODEC_FLAG_INTERLACED_ME)) {
819
 
            /* interlaced direct mode decoding tables */
820
 
            for (i = 0; i < 2; i++) {
821
 
                int j, k;
822
 
                for (j = 0; j < 2; j++) {
823
 
                    for (k = 0; k < 2; k++) {
824
 
                        FF_ALLOCZ_OR_GOTO(s->avctx,
825
 
                                          s->b_field_mv_table_base[i][j][k],
826
 
                                          mv_table_size * 2 * sizeof(int16_t),
827
 
                                          fail);
828
 
                        s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
829
 
                                                       s->mb_stride + 1;
830
 
                    }
831
 
                    FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
832
 
                                      mb_array_size * 2 * sizeof(uint8_t),
833
 
                                      fail);
834
 
                    FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
835
 
                                      mv_table_size * 2 * sizeof(int16_t),
836
 
                                      fail);
837
 
                    s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
838
 
                                                + s->mb_stride + 1;
839
 
                }
840
 
                FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
841
 
                                  mb_array_size * 2 * sizeof(uint8_t),
842
 
                                  fail);
843
 
            }
844
 
        }
845
 
        if (s->out_format == FMT_H263) {
846
 
            /* cbp values */
847
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
848
 
            s->coded_block = s->coded_block_base + s->b8_stride + 1;
849
 
 
850
 
            /* cbp, ac_pred, pred_dir */
851
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
852
 
                              mb_array_size * sizeof(uint8_t), fail);
853
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
854
 
                              mb_array_size * sizeof(uint8_t), fail);
855
 
        }
856
 
 
857
 
        if (s->h263_pred || s->h263_plus || !s->encoding) {
858
 
            /* dc values */
859
 
            // MN: we need these for  error resilience of intra-frames
860
 
            FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
861
 
                              yc_size * sizeof(int16_t), fail);
862
 
            s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
863
 
            s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
864
 
            s->dc_val[2] = s->dc_val[1] + c_size;
865
 
            for (i = 0; i < yc_size; i++)
866
 
                s->dc_val_base[i] = 1024;
867
 
        }
868
 
 
869
 
        /* which mb is a intra block */
870
 
        FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
871
 
        memset(s->mbintra_table, 1, mb_array_size);
872
 
 
873
 
        /* init macroblock skip table */
874
 
        FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
875
 
        // Note the + 1 is for  a quicker mpeg4 slice_end detection
 
966
        if (init_context_frame(s))
 
967
            goto fail;
876
968
 
877
969
        s->parse_context.state = -1;
878
 
        if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
879
 
            s->avctx->debug_mv) {
880
 
            s->visualization_buffer[0] = av_malloc((s->mb_width * 16 +
881
 
                        2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
882
 
            s->visualization_buffer[1] = av_malloc((s->mb_width * 16 +
883
 
                        2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
884
 
            s->visualization_buffer[2] = av_malloc((s->mb_width * 16 +
885
 
                        2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
886
 
        }
887
970
    }
888
971
 
889
972
    s->context_initialized = 1;
915
998
 
916
999
    return 0;
917
1000
 fail:
918
 
    MPV_common_end(s);
 
1001
    ff_MPV_common_end(s);
919
1002
    return -1;
920
1003
}
921
1004
 
922
 
/* init common structure for both encoder and decoder */
923
 
void MPV_common_end(MpegEncContext *s)
 
1005
/**
 
1006
 * Frees and resets MpegEncContext fields depending on the resolution.
 
1007
 * Is used during resolution changes to avoid a full reinitialization of the
 
1008
 * codec.
 
1009
 */
 
1010
static int free_context_frame(MpegEncContext *s)
924
1011
{
925
1012
    int i, j, k;
926
1013
 
927
 
    if (s->slice_context_count > 1) {
928
 
        for (i = 0; i < s->slice_context_count; i++) {
929
 
            free_duplicate_context(s->thread_context[i]);
930
 
        }
931
 
        for (i = 1; i < s->slice_context_count; i++) {
932
 
            av_freep(&s->thread_context[i]);
933
 
        }
934
 
        s->slice_context_count = 1;
935
 
    } else free_duplicate_context(s);
936
 
 
937
 
    av_freep(&s->parse_context.buffer);
938
 
    s->parse_context.buffer_size = 0;
939
 
 
940
1014
    av_freep(&s->mb_type);
941
1015
    av_freep(&s->p_mv_table_base);
942
1016
    av_freep(&s->b_forw_mv_table_base);
970
1044
    av_freep(&s->pred_dir_table);
971
1045
 
972
1046
    av_freep(&s->mbskip_table);
 
1047
 
 
1048
    av_freep(&s->error_status_table);
 
1049
    av_freep(&s->er_temp_buffer);
 
1050
    av_freep(&s->mb_index2xy);
 
1051
    av_freep(&s->lambda_table);
 
1052
    av_freep(&s->cplx_tab);
 
1053
    av_freep(&s->bits_tab);
 
1054
 
 
1055
    s->linesize = s->uvlinesize = 0;
 
1056
 
 
1057
    for (i = 0; i < 3; i++)
 
1058
        av_freep(&s->visualization_buffer[i]);
 
1059
 
 
1060
    return 0;
 
1061
}
 
1062
 
 
1063
int ff_MPV_common_frame_size_change(MpegEncContext *s)
 
1064
{
 
1065
    int i, err = 0;
 
1066
 
 
1067
    if (s->slice_context_count > 1) {
 
1068
        for (i = 0; i < s->slice_context_count; i++) {
 
1069
            free_duplicate_context(s->thread_context[i]);
 
1070
        }
 
1071
        for (i = 1; i < s->slice_context_count; i++) {
 
1072
            av_freep(&s->thread_context[i]);
 
1073
        }
 
1074
    } else
 
1075
        free_duplicate_context(s);
 
1076
 
 
1077
    free_context_frame(s);
 
1078
 
 
1079
    if (s->picture)
 
1080
        for (i = 0; i < s->picture_count; i++) {
 
1081
                s->picture[i].needs_realloc = 1;
 
1082
        }
 
1083
 
 
1084
    s->last_picture_ptr         =
 
1085
    s->next_picture_ptr         =
 
1086
    s->current_picture_ptr      = NULL;
 
1087
 
 
1088
    // init
 
1089
    if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
 
1090
        s->mb_height = (s->height + 31) / 32 * 2;
 
1091
    else if (s->codec_id != AV_CODEC_ID_H264)
 
1092
        s->mb_height = (s->height + 15) / 16;
 
1093
 
 
1094
    if ((s->width || s->height) &&
 
1095
        av_image_check_size(s->width, s->height, 0, s->avctx))
 
1096
        return AVERROR_INVALIDDATA;
 
1097
 
 
1098
    if ((err = init_context_frame(s)))
 
1099
        goto fail;
 
1100
 
 
1101
    s->thread_context[0]   = s;
 
1102
 
 
1103
    if (s->width && s->height) {
 
1104
        int nb_slices = s->slice_context_count;
 
1105
        if (nb_slices > 1) {
 
1106
            for (i = 1; i < nb_slices; i++) {
 
1107
                s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
 
1108
                memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
 
1109
            }
 
1110
 
 
1111
            for (i = 0; i < nb_slices; i++) {
 
1112
                if (init_duplicate_context(s->thread_context[i], s) < 0)
 
1113
                    goto fail;
 
1114
                    s->thread_context[i]->start_mb_y =
 
1115
                        (s->mb_height * (i) + nb_slices / 2) / nb_slices;
 
1116
                    s->thread_context[i]->end_mb_y   =
 
1117
                        (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
 
1118
            }
 
1119
        } else {
 
1120
            if (init_duplicate_context(s, s) < 0)
 
1121
                goto fail;
 
1122
            s->start_mb_y = 0;
 
1123
            s->end_mb_y   = s->mb_height;
 
1124
        }
 
1125
        s->slice_context_count = nb_slices;
 
1126
    }
 
1127
 
 
1128
    return 0;
 
1129
 fail:
 
1130
    ff_MPV_common_end(s);
 
1131
    return err;
 
1132
}
 
1133
 
 
1134
/* init common structure for both encoder and decoder */
 
1135
void ff_MPV_common_end(MpegEncContext *s)
 
1136
{
 
1137
    int i;
 
1138
 
 
1139
    if (s->slice_context_count > 1) {
 
1140
        for (i = 0; i < s->slice_context_count; i++) {
 
1141
            free_duplicate_context(s->thread_context[i]);
 
1142
        }
 
1143
        for (i = 1; i < s->slice_context_count; i++) {
 
1144
            av_freep(&s->thread_context[i]);
 
1145
        }
 
1146
        s->slice_context_count = 1;
 
1147
    } else free_duplicate_context(s);
 
1148
 
 
1149
    av_freep(&s->parse_context.buffer);
 
1150
    s->parse_context.buffer_size = 0;
 
1151
 
973
1152
    av_freep(&s->bitstream_buffer);
974
1153
    s->allocated_bitstream_buffer_size = 0;
975
1154
 
976
1155
    av_freep(&s->avctx->stats_out);
977
1156
    av_freep(&s->ac_stats);
978
 
    av_freep(&s->error_status_table);
979
 
    av_freep(&s->mb_index2xy);
980
 
    av_freep(&s->lambda_table);
 
1157
 
981
1158
    av_freep(&s->q_intra_matrix);
982
1159
    av_freep(&s->q_inter_matrix);
983
1160
    av_freep(&s->q_intra_matrix16);
992
1169
        }
993
1170
    }
994
1171
    av_freep(&s->picture);
995
 
    s->context_initialized      = 0;
996
 
    s->last_picture_ptr         =
997
 
    s->next_picture_ptr         =
998
 
    s->current_picture_ptr      = NULL;
999
 
    s->linesize = s->uvlinesize = 0;
1000
1172
 
1001
 
    for (i = 0; i < 3; i++)
1002
 
        av_freep(&s->visualization_buffer[i]);
 
1173
    free_context_frame(s);
1003
1174
 
1004
1175
    if (!(s->avctx->active_thread_type & FF_THREAD_FRAME))
1005
1176
        avcodec_default_free_buffers(s->avctx);
 
1177
 
 
1178
    s->context_initialized      = 0;
 
1179
    s->last_picture_ptr         =
 
1180
    s->next_picture_ptr         =
 
1181
    s->current_picture_ptr      = NULL;
 
1182
    s->linesize = s->uvlinesize = 0;
1006
1183
}
1007
1184
 
1008
1185
void ff_init_rl(RLTable *rl,
1112
1289
    }
1113
1290
}
1114
1291
 
1115
 
int ff_find_unused_picture(MpegEncContext *s, int shared)
 
1292
static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
 
1293
{
 
1294
    if (pic->f.data[0] == NULL)
 
1295
        return 1;
 
1296
    if (pic->needs_realloc && !(pic->f.reference & DELAYED_PIC_REF))
 
1297
        if (!pic->owner2 || pic->owner2 == s)
 
1298
            return 1;
 
1299
    return 0;
 
1300
}
 
1301
 
 
1302
static int find_unused_picture(MpegEncContext *s, int shared)
1116
1303
{
1117
1304
    int i;
1118
1305
 
1123
1310
        }
1124
1311
    } else {
1125
1312
        for (i = s->picture_range_start; i < s->picture_range_end; i++) {
1126
 
            if (s->picture[i].f.data[0] == NULL && s->picture[i].f.type != 0)
 
1313
            if (pic_is_unused(s, &s->picture[i]) && s->picture[i].f.type != 0)
1127
1314
                return i; // FIXME
1128
1315
        }
1129
1316
        for (i = s->picture_range_start; i < s->picture_range_end; i++) {
1130
 
            if (s->picture[i].f.data[0] == NULL)
 
1317
            if (pic_is_unused(s, &s->picture[i]))
1131
1318
                return i;
1132
1319
        }
1133
1320
    }
1135
1322
    return AVERROR_INVALIDDATA;
1136
1323
}
1137
1324
 
 
1325
int ff_find_unused_picture(MpegEncContext *s, int shared)
 
1326
{
 
1327
    int ret = find_unused_picture(s, shared);
 
1328
 
 
1329
    if (ret >= 0 && ret < s->picture_range_end) {
 
1330
        if (s->picture[ret].needs_realloc) {
 
1331
            s->picture[ret].needs_realloc = 0;
 
1332
            free_picture(s, &s->picture[ret]);
 
1333
            avcodec_get_frame_defaults(&s->picture[ret].f);
 
1334
        }
 
1335
    }
 
1336
    return ret;
 
1337
}
 
1338
 
1138
1339
static void update_noise_reduction(MpegEncContext *s)
1139
1340
{
1140
1341
    int intra, i;
1160
1361
 * generic function for encode/decode called after coding/decoding
1161
1362
 * the header and before a frame is coded/decoded.
1162
1363
 */
1163
 
int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
 
1364
int ff_MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx)
1164
1365
{
1165
1366
    int i;
1166
1367
    Picture *pic;
1167
1368
    s->mb_skipped = 0;
1168
1369
 
1169
 
    assert(s->last_picture_ptr == NULL || s->out_format != FMT_H264 ||
1170
 
           s->codec_id == CODEC_ID_SVQ3);
1171
 
 
1172
1370
    /* mark & release old frames */
1173
 
    if (s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3) {
 
1371
    if (s->out_format != FMT_H264 || s->codec_id == AV_CODEC_ID_SVQ3) {
1174
1372
        if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1175
1373
            s->last_picture_ptr != s->next_picture_ptr &&
1176
1374
            s->last_picture_ptr->f.data[0]) {
1185
1383
                if (s->picture[i].owner2 == s && s->picture[i].f.data[0] &&
1186
1384
                    &s->picture[i] != s->last_picture_ptr &&
1187
1385
                    &s->picture[i] != s->next_picture_ptr &&
1188
 
                    s->picture[i].f.reference) {
 
1386
                    s->picture[i].f.reference && !s->picture[i].needs_realloc) {
1189
1387
                    if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1190
1388
                        av_log(avctx, AV_LOG_ERROR,
1191
1389
                               "releasing zombie picture\n");
1205
1403
            pic = s->current_picture_ptr;
1206
1404
        } else {
1207
1405
            i   = ff_find_unused_picture(s, 0);
 
1406
            if (i < 0) {
 
1407
                av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
 
1408
                return i;
 
1409
            }
1208
1410
            pic = &s->picture[i];
1209
1411
        }
1210
1412
 
1211
1413
        pic->f.reference = 0;
1212
 
        if (!s->dropable) {
1213
 
            if (s->codec_id == CODEC_ID_H264)
 
1414
        if (!s->droppable) {
 
1415
            if (s->codec_id == AV_CODEC_ID_H264)
1214
1416
                pic->f.reference = s->picture_structure;
1215
1417
            else if (s->pict_type != AV_PICTURE_TYPE_B)
1216
1418
                pic->f.reference = 3;
1224
1426
        s->current_picture_ptr = pic;
1225
1427
        // FIXME use only the vars from current_pic
1226
1428
        s->current_picture_ptr->f.top_field_first = s->top_field_first;
1227
 
        if (s->codec_id == CODEC_ID_MPEG1VIDEO ||
1228
 
            s->codec_id == CODEC_ID_MPEG2VIDEO) {
 
1429
        if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
 
1430
            s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1229
1431
            if (s->picture_structure != PICT_FRAME)
1230
1432
                s->current_picture_ptr->f.top_field_first =
1231
1433
                    (s->picture_structure == PICT_TOP_FIELD) == s->first_field;
1244
1446
 
1245
1447
    if (s->pict_type != AV_PICTURE_TYPE_B) {
1246
1448
        s->last_picture_ptr = s->next_picture_ptr;
1247
 
        if (!s->dropable)
 
1449
        if (!s->droppable)
1248
1450
            s->next_picture_ptr = s->current_picture_ptr;
1249
1451
    }
1250
 
    /* av_log(s->avctx, AV_LOG_DEBUG, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1251
 
           s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
1252
 
           s->last_picture_ptr    ? s->last_picture_ptr->f.data[0]    : NULL,
1253
 
           s->next_picture_ptr    ? s->next_picture_ptr->f.data[0]    : NULL,
1254
 
           s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
1255
 
           s->pict_type, s->dropable); */
 
1452
    av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
 
1453
            s->last_picture_ptr, s->next_picture_ptr,s->current_picture_ptr,
 
1454
            s->last_picture_ptr    ? s->last_picture_ptr->f.data[0]    : NULL,
 
1455
            s->next_picture_ptr    ? s->next_picture_ptr->f.data[0]    : NULL,
 
1456
            s->current_picture_ptr ? s->current_picture_ptr->f.data[0] : NULL,
 
1457
            s->pict_type, s->droppable);
1256
1458
 
1257
 
    if (s->codec_id != CODEC_ID_H264) {
 
1459
    if (s->codec_id != AV_CODEC_ID_H264) {
1258
1460
        if ((s->last_picture_ptr == NULL ||
1259
1461
             s->last_picture_ptr->f.data[0] == NULL) &&
1260
1462
            (s->pict_type != AV_PICTURE_TYPE_I ||
1268
1470
 
1269
1471
            /* Allocate a dummy frame */
1270
1472
            i = ff_find_unused_picture(s, 0);
 
1473
            if (i < 0) {
 
1474
                av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
 
1475
                return i;
 
1476
            }
1271
1477
            s->last_picture_ptr = &s->picture[i];
1272
 
            if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0)
 
1478
            if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
 
1479
                s->last_picture_ptr = NULL;
1273
1480
                return -1;
1274
 
            ff_thread_report_progress((AVFrame *) s->last_picture_ptr,
1275
 
                                      INT_MAX, 0);
1276
 
            ff_thread_report_progress((AVFrame *) s->last_picture_ptr,
1277
 
                                      INT_MAX, 1);
 
1481
            }
 
1482
            ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 0);
 
1483
            ff_thread_report_progress(&s->last_picture_ptr->f, INT_MAX, 1);
 
1484
            s->last_picture_ptr->f.reference = 3;
1278
1485
        }
1279
1486
        if ((s->next_picture_ptr == NULL ||
1280
1487
             s->next_picture_ptr->f.data[0] == NULL) &&
1281
1488
            s->pict_type == AV_PICTURE_TYPE_B) {
1282
1489
            /* Allocate a dummy frame */
1283
1490
            i = ff_find_unused_picture(s, 0);
 
1491
            if (i < 0) {
 
1492
                av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
 
1493
                return i;
 
1494
            }
1284
1495
            s->next_picture_ptr = &s->picture[i];
1285
 
            if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0)
 
1496
            if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
 
1497
                s->next_picture_ptr = NULL;
1286
1498
                return -1;
1287
 
            ff_thread_report_progress((AVFrame *) s->next_picture_ptr,
1288
 
                                      INT_MAX, 0);
1289
 
            ff_thread_report_progress((AVFrame *) s->next_picture_ptr,
1290
 
                                      INT_MAX, 1);
 
1499
            }
 
1500
            ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 0);
 
1501
            ff_thread_report_progress(&s->next_picture_ptr->f, INT_MAX, 1);
 
1502
            s->next_picture_ptr->f.reference = 3;
1291
1503
        }
1292
1504
    }
1293
1505
 
1296
1508
    if (s->next_picture_ptr)
1297
1509
        ff_copy_picture(&s->next_picture, s->next_picture_ptr);
1298
1510
 
1299
 
    if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME) &&
1300
 
        (s->out_format != FMT_H264 || s->codec_id == CODEC_ID_SVQ3)) {
 
1511
    if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME)) {
1301
1512
        if (s->next_picture_ptr)
1302
1513
            s->next_picture_ptr->owner2 = s;
1303
1514
        if (s->last_picture_ptr)
1325
1536
    /* set dequantizer, we can't do it during init as
1326
1537
     * it might change for mpeg4 and we can't do it in the header
1327
1538
     * decode as init is not called for mpeg4 there yet */
1328
 
    if (s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO) {
 
1539
    if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1329
1540
        s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1330
1541
        s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1331
1542
    } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1349
1560
 
1350
1561
/* generic function for encode/decode called after a
1351
1562
 * frame has been coded/decoded. */
1352
 
void MPV_frame_end(MpegEncContext *s)
 
1563
void ff_MPV_frame_end(MpegEncContext *s)
1353
1564
{
1354
1565
    int i;
1355
1566
    /* redraw edges for the frame if decoding didn't complete */
1363
1574
              s->current_picture.f.reference &&
1364
1575
              !s->intra_only &&
1365
1576
              !(s->flags & CODEC_FLAG_EMU_EDGE)) {
1366
 
        int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
1367
 
        int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
1368
 
        s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
1369
 
                          s->h_edge_pos, s->v_edge_pos,
1370
 
                          EDGE_WIDTH, EDGE_WIDTH,
1371
 
                          EDGE_TOP | EDGE_BOTTOM);
1372
 
        s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
1373
 
                          s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1374
 
                          EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1375
 
                          EDGE_TOP | EDGE_BOTTOM);
1376
 
        s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
1377
 
                          s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
1378
 
                          EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
1379
 
                          EDGE_TOP | EDGE_BOTTOM);
 
1577
       const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
 
1578
       int hshift = desc->log2_chroma_w;
 
1579
       int vshift = desc->log2_chroma_h;
 
1580
       s->dsp.draw_edges(s->current_picture.f.data[0], s->linesize,
 
1581
                         s->h_edge_pos, s->v_edge_pos,
 
1582
                         EDGE_WIDTH, EDGE_WIDTH,
 
1583
                         EDGE_TOP | EDGE_BOTTOM);
 
1584
       s->dsp.draw_edges(s->current_picture.f.data[1], s->uvlinesize,
 
1585
                         s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
 
1586
                         EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
 
1587
                         EDGE_TOP | EDGE_BOTTOM);
 
1588
       s->dsp.draw_edges(s->current_picture.f.data[2], s->uvlinesize,
 
1589
                         s->h_edge_pos >> hshift, s->v_edge_pos >> vshift,
 
1590
                         EDGE_WIDTH >> hshift, EDGE_WIDTH >> vshift,
 
1591
                         EDGE_TOP | EDGE_BOTTOM);
1380
1592
    }
1381
1593
 
1382
1594
    emms_c();
1412
1624
    memset(&s->next_picture,    0, sizeof(Picture));
1413
1625
    memset(&s->current_picture, 0, sizeof(Picture));
1414
1626
#endif
1415
 
    s->avctx->coded_frame = (AVFrame *) s->current_picture_ptr;
 
1627
    s->avctx->coded_frame = &s->current_picture_ptr->f;
1416
1628
 
1417
 
    if (s->codec_id != CODEC_ID_H264 && s->current_picture.f.reference) {
1418
 
        ff_thread_report_progress((AVFrame *) s->current_picture_ptr, INT_MAX, 0);
 
1629
    if (s->codec_id != AV_CODEC_ID_H264 && s->current_picture.f.reference) {
 
1630
        ff_thread_report_progress(&s->current_picture_ptr->f, INT_MAX, 0);
1419
1631
    }
1420
1632
}
1421
1633
 
1446
1658
        buf += sx + sy * stride;
1447
1659
        ex  -= sx;
1448
1660
        f    = ((ey - sy) << 16) / ex;
1449
 
        for (x = 0; x = ex; x++) {
 
1661
        for (x = 0; x <= ex; x++) {
1450
1662
            y  = (x * f) >> 16;
1451
1663
            fr = (x * f) & 0xFFFF;
1452
1664
            buf[y * stride + x]       += (color * (0x10000 - fr)) >> 16;
1599
1811
                    else
1600
1812
                        av_log(s->avctx, AV_LOG_DEBUG, " ");
1601
1813
                }
1602
 
                // av_log(s->avctx, AV_LOG_DEBUG, " ");
1603
1814
            }
1604
1815
            av_log(s->avctx, AV_LOG_DEBUG, "\n");
1605
1816
        }
1616
1827
        const int height         = s->avctx->height;
1617
1828
        const int mv_sample_log2 = 4 - pict->motion_subsample_log2;
1618
1829
        const int mv_stride      = (s->mb_width << mv_sample_log2) +
1619
 
                                   (s->codec_id == CODEC_ID_H264 ? 0 : 1);
 
1830
                                   (s->codec_id == AV_CODEC_ID_H264 ? 0 : 1);
1620
1831
        s->low_delay = 0; // needed to see the vectors without trashing the buffers
1621
1832
 
1622
 
        avcodec_get_chroma_sub_sample(s->avctx->pix_fmt,
1623
 
                                      &h_chroma_shift, &v_chroma_shift);
 
1833
        av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
 
1834
                                         &h_chroma_shift, &v_chroma_shift);
1624
1835
        for (i = 0; i < 3; i++) {
1625
1836
            memcpy(s->visualization_buffer[i], pict->data[i],
1626
1837
                   (i == 0) ? pict->linesize[i] * height:
1805
2016
                    }
1806
2017
 
1807
2018
                    if (IS_INTERLACED(mb_type) &&
1808
 
                        s->codec_id == CODEC_ID_H264) {
 
2019
                        s->codec_id == AV_CODEC_ID_H264) {
1809
2020
                        // hmm
1810
2021
                    }
1811
2022
                }
1815
2026
    }
1816
2027
}
1817
2028
 
1818
 
static inline int hpel_motion_lowres(MpegEncContext *s,
1819
 
                                     uint8_t *dest, uint8_t *src,
1820
 
                                     int field_based, int field_select,
1821
 
                                     int src_x, int src_y,
1822
 
                                     int width, int height, int stride,
1823
 
                                     int h_edge_pos, int v_edge_pos,
1824
 
                                     int w, int h, h264_chroma_mc_func *pix_op,
1825
 
                                     int motion_x, int motion_y)
1826
 
{
1827
 
    const int lowres   = s->avctx->lowres;
1828
 
    const int op_index = FFMIN(lowres, 2);
1829
 
    const int s_mask   = (2 << lowres) - 1;
1830
 
    int emu = 0;
1831
 
    int sx, sy;
1832
 
 
1833
 
    if (s->quarter_sample) {
1834
 
        motion_x /= 2;
1835
 
        motion_y /= 2;
1836
 
    }
1837
 
 
1838
 
    sx = motion_x & s_mask;
1839
 
    sy = motion_y & s_mask;
1840
 
    src_x += motion_x >> lowres + 1;
1841
 
    src_y += motion_y >> lowres + 1;
1842
 
 
1843
 
    src   += src_y * stride + src_x;
1844
 
 
1845
 
    if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w,                 0) ||
1846
 
        (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
1847
 
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src, s->linesize, w + 1,
1848
 
                                (h + 1) << field_based, src_x,
1849
 
                                src_y   << field_based,
1850
 
                                h_edge_pos,
1851
 
                                v_edge_pos);
1852
 
        src = s->edge_emu_buffer;
1853
 
        emu = 1;
1854
 
    }
1855
 
 
1856
 
    sx = (sx << 2) >> lowres;
1857
 
    sy = (sy << 2) >> lowres;
1858
 
    if (field_select)
1859
 
        src += s->linesize;
1860
 
    pix_op[op_index](dest, src, stride, h, sx, sy);
1861
 
    return emu;
1862
 
}
1863
 
 
1864
 
/* apply one mpeg motion vector to the three components */
1865
 
static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,
1866
 
                                                uint8_t *dest_y,
1867
 
                                                uint8_t *dest_cb,
1868
 
                                                uint8_t *dest_cr,
1869
 
                                                int field_based,
1870
 
                                                int bottom_field,
1871
 
                                                int field_select,
1872
 
                                                uint8_t **ref_picture,
1873
 
                                                h264_chroma_mc_func *pix_op,
1874
 
                                                int motion_x, int motion_y,
1875
 
                                                int h, int mb_y)
1876
 
{
1877
 
    uint8_t *ptr_y, *ptr_cb, *ptr_cr;
1878
 
    int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,
1879
 
        uvsx, uvsy;
1880
 
    const int lowres     = s->avctx->lowres;
1881
 
    const int op_index   = FFMIN(lowres, 2);
1882
 
    const int block_s    = 8>>lowres;
1883
 
    const int s_mask     = (2 << lowres) - 1;
1884
 
    const int h_edge_pos = s->h_edge_pos >> lowres;
1885
 
    const int v_edge_pos = s->v_edge_pos >> lowres;
1886
 
    linesize   = s->current_picture.f.linesize[0] << field_based;
1887
 
    uvlinesize = s->current_picture.f.linesize[1] << field_based;
1888
 
 
1889
 
    // FIXME obviously not perfect but qpel will not work in lowres anyway
1890
 
    if (s->quarter_sample) {
1891
 
        motion_x /= 2;
1892
 
        motion_y /= 2;
1893
 
    }
1894
 
 
1895
 
    if (field_based) {
1896
 
        motion_y += (bottom_field - field_select) * (1 << lowres - 1);
1897
 
    }
1898
 
 
1899
 
    sx = motion_x & s_mask;
1900
 
    sy = motion_y & s_mask;
1901
 
    src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
1902
 
    src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
1903
 
 
1904
 
    if (s->out_format == FMT_H263) {
1905
 
        uvsx    = ((motion_x >> 1) & s_mask) | (sx & 1);
1906
 
        uvsy    = ((motion_y >> 1) & s_mask) | (sy & 1);
1907
 
        uvsrc_x = src_x >> 1;
1908
 
        uvsrc_y = src_y >> 1;
1909
 
    } else if (s->out_format == FMT_H261) {
1910
 
        // even chroma mv's are full pel in H261
1911
 
        mx      = motion_x / 4;
1912
 
        my      = motion_y / 4;
1913
 
        uvsx    = (2 * mx) & s_mask;
1914
 
        uvsy    = (2 * my) & s_mask;
1915
 
        uvsrc_x = s->mb_x * block_s + (mx >> lowres);
1916
 
        uvsrc_y =    mb_y * block_s + (my >> lowres);
1917
 
    } else {
1918
 
        mx      = motion_x / 2;
1919
 
        my      = motion_y / 2;
1920
 
        uvsx    = mx & s_mask;
1921
 
        uvsy    = my & s_mask;
1922
 
        uvsrc_x = s->mb_x * block_s                 + (mx >> lowres + 1);
1923
 
        uvsrc_y =   (mb_y * block_s >> field_based) + (my >> lowres + 1);
1924
 
    }
1925
 
 
1926
 
    ptr_y  = ref_picture[0] + src_y   * linesize   + src_x;
1927
 
    ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
1928
 
    ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
1929
 
 
1930
 
    if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s,       0) ||
1931
 
        (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
1932
 
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
1933
 
                                s->linesize, 17, 17 + field_based,
1934
 
                                src_x, src_y << field_based, h_edge_pos,
1935
 
                                v_edge_pos);
1936
 
        ptr_y = s->edge_emu_buffer;
1937
 
        if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
1938
 
            uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;
1939
 
            s->dsp.emulated_edge_mc(uvbuf , ptr_cb, s->uvlinesize, 9,
1940
 
                                    9 + field_based,
1941
 
                                    uvsrc_x, uvsrc_y << field_based,
1942
 
                                    h_edge_pos >> 1, v_edge_pos >> 1);
1943
 
            s->dsp.emulated_edge_mc(uvbuf + 16, ptr_cr, s->uvlinesize, 9,
1944
 
                                    9 + field_based,
1945
 
                                    uvsrc_x, uvsrc_y << field_based,
1946
 
                                    h_edge_pos >> 1, v_edge_pos >> 1);
1947
 
            ptr_cb = uvbuf;
1948
 
            ptr_cr = uvbuf + 16;
1949
 
        }
1950
 
    }
1951
 
 
1952
 
    // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data
1953
 
    if (bottom_field) {
1954
 
        dest_y  += s->linesize;
1955
 
        dest_cb += s->uvlinesize;
1956
 
        dest_cr += s->uvlinesize;
1957
 
    }
1958
 
 
1959
 
    if (field_select) {
1960
 
        ptr_y   += s->linesize;
1961
 
        ptr_cb  += s->uvlinesize;
1962
 
        ptr_cr  += s->uvlinesize;
1963
 
    }
1964
 
 
1965
 
    sx = (sx << 2) >> lowres;
1966
 
    sy = (sy << 2) >> lowres;
1967
 
    pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
1968
 
 
1969
 
    if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
1970
 
        uvsx = (uvsx << 2) >> lowres;
1971
 
        uvsy = (uvsy << 2) >> lowres;
1972
 
        pix_op[op_index](dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift,
1973
 
                         uvsx, uvsy);
1974
 
        pix_op[op_index](dest_cr, ptr_cr, uvlinesize, h >> s->chroma_y_shift,
1975
 
                         uvsx, uvsy);
1976
 
    }
1977
 
    // FIXME h261 lowres loop filter
1978
 
}
1979
 
 
1980
 
static inline void chroma_4mv_motion_lowres(MpegEncContext *s,
1981
 
                                            uint8_t *dest_cb, uint8_t *dest_cr,
1982
 
                                            uint8_t **ref_picture,
1983
 
                                            h264_chroma_mc_func * pix_op,
1984
 
                                            int mx, int my)
1985
 
{
1986
 
    const int lowres     = s->avctx->lowres;
1987
 
    const int op_index   = FFMIN(lowres, 2);
1988
 
    const int block_s    = 8 >> lowres;
1989
 
    const int s_mask     = (2 << lowres) - 1;
1990
 
    const int h_edge_pos = s->h_edge_pos >> lowres + 1;
1991
 
    const int v_edge_pos = s->v_edge_pos >> lowres + 1;
1992
 
    int emu = 0, src_x, src_y, offset, sx, sy;
1993
 
    uint8_t *ptr;
1994
 
 
1995
 
    if (s->quarter_sample) {
1996
 
        mx /= 2;
1997
 
        my /= 2;
1998
 
    }
1999
 
 
2000
 
    /* In case of 8X8, we construct a single chroma motion vector
2001
 
       with a special rounding */
2002
 
    mx = ff_h263_round_chroma(mx);
2003
 
    my = ff_h263_round_chroma(my);
2004
 
 
2005
 
    sx = mx & s_mask;
2006
 
    sy = my & s_mask;
2007
 
    src_x = s->mb_x * block_s + (mx >> lowres + 1);
2008
 
    src_y = s->mb_y * block_s + (my >> lowres + 1);
2009
 
 
2010
 
    offset = src_y * s->uvlinesize + src_x;
2011
 
    ptr = ref_picture[1] + offset;
2012
 
    if (s->flags & CODEC_FLAG_EMU_EDGE) {
2013
 
        if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
2014
 
            (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
2015
 
            s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize,
2016
 
                                    9, 9, src_x, src_y, h_edge_pos, v_edge_pos);
2017
 
            ptr = s->edge_emu_buffer;
2018
 
            emu = 1;
2019
 
        }
2020
 
    }
2021
 
    sx = (sx << 2) >> lowres;
2022
 
    sy = (sy << 2) >> lowres;
2023
 
    pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
2024
 
 
2025
 
    ptr = ref_picture[2] + offset;
2026
 
    if (emu) {
2027
 
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, ptr, s->uvlinesize, 9, 9,
2028
 
                                src_x, src_y, h_edge_pos, v_edge_pos);
2029
 
        ptr = s->edge_emu_buffer;
2030
 
    }
2031
 
    pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
2032
 
}
2033
 
 
2034
 
/**
2035
 
 * motion compensation of a single macroblock
2036
 
 * @param s context
2037
 
 * @param dest_y luma destination pointer
2038
 
 * @param dest_cb chroma cb/u destination pointer
2039
 
 * @param dest_cr chroma cr/v destination pointer
2040
 
 * @param dir direction (0->forward, 1->backward)
2041
 
 * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2042
 
 * @param pix_op halfpel motion compensation function (average or put normally)
2043
 
 * the motion vectors are taken from s->mv and the MV type from s->mv_type
2044
 
 */
2045
 
static inline void MPV_motion_lowres(MpegEncContext *s,
2046
 
                                     uint8_t *dest_y, uint8_t *dest_cb,
2047
 
                                     uint8_t *dest_cr,
2048
 
                                     int dir, uint8_t **ref_picture,
2049
 
                                     h264_chroma_mc_func *pix_op)
2050
 
{
2051
 
    int mx, my;
2052
 
    int mb_x, mb_y, i;
2053
 
    const int lowres  = s->avctx->lowres;
2054
 
    const int block_s = 8 >>lowres;
2055
 
 
2056
 
    mb_x = s->mb_x;
2057
 
    mb_y = s->mb_y;
2058
 
 
2059
 
    switch (s->mv_type) {
2060
 
    case MV_TYPE_16X16:
2061
 
        mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2062
 
                           0, 0, 0,
2063
 
                           ref_picture, pix_op,
2064
 
                           s->mv[dir][0][0], s->mv[dir][0][1],
2065
 
                           2 * block_s, mb_y);
2066
 
        break;
2067
 
    case MV_TYPE_8X8:
2068
 
        mx = 0;
2069
 
        my = 0;
2070
 
        for (i = 0; i < 4; i++) {
2071
 
            hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
2072
 
                               s->linesize) * block_s,
2073
 
                               ref_picture[0], 0, 0,
2074
 
                               (2 * mb_x + (i & 1)) * block_s,
2075
 
                               (2 * mb_y + (i >> 1)) * block_s,
2076
 
                               s->width, s->height, s->linesize,
2077
 
                               s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
2078
 
                               block_s, block_s, pix_op,
2079
 
                               s->mv[dir][i][0], s->mv[dir][i][1]);
2080
 
 
2081
 
            mx += s->mv[dir][i][0];
2082
 
            my += s->mv[dir][i][1];
2083
 
        }
2084
 
 
2085
 
        if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
2086
 
            chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
2087
 
                                     pix_op, mx, my);
2088
 
        break;
2089
 
    case MV_TYPE_FIELD:
2090
 
        if (s->picture_structure == PICT_FRAME) {
2091
 
            /* top field */
2092
 
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2093
 
                               1, 0, s->field_select[dir][0],
2094
 
                               ref_picture, pix_op,
2095
 
                               s->mv[dir][0][0], s->mv[dir][0][1],
2096
 
                               block_s, mb_y);
2097
 
            /* bottom field */
2098
 
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2099
 
                               1, 1, s->field_select[dir][1],
2100
 
                               ref_picture, pix_op,
2101
 
                               s->mv[dir][1][0], s->mv[dir][1][1],
2102
 
                               block_s, mb_y);
2103
 
        } else {
2104
 
            if (s->picture_structure != s->field_select[dir][0] + 1 &&
2105
 
                s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
2106
 
                ref_picture = s->current_picture_ptr->f.data;
2107
 
 
2108
 
            }
2109
 
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2110
 
                               0, 0, s->field_select[dir][0],
2111
 
                               ref_picture, pix_op,
2112
 
                               s->mv[dir][0][0],
2113
 
                               s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
2114
 
            }
2115
 
        break;
2116
 
    case MV_TYPE_16X8:
2117
 
        for (i = 0; i < 2; i++) {
2118
 
            uint8_t **ref2picture;
2119
 
 
2120
 
            if (s->picture_structure == s->field_select[dir][i] + 1 ||
2121
 
                s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
2122
 
                ref2picture = ref_picture;
2123
 
            } else {
2124
 
                ref2picture = s->current_picture_ptr->f.data;
2125
 
            }
2126
 
 
2127
 
            mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2128
 
                               0, 0, s->field_select[dir][i],
2129
 
                               ref2picture, pix_op,
2130
 
                               s->mv[dir][i][0], s->mv[dir][i][1] +
2131
 
                               2 * block_s * i, block_s, mb_y >> 1);
2132
 
 
2133
 
            dest_y  +=  2 * block_s *  s->linesize;
2134
 
            dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2135
 
            dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2136
 
        }
2137
 
        break;
2138
 
    case MV_TYPE_DMV:
2139
 
        if (s->picture_structure == PICT_FRAME) {
2140
 
            for (i = 0; i < 2; i++) {
2141
 
                int j;
2142
 
                for (j = 0; j < 2; j++) {
2143
 
                    mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2144
 
                                       1, j, j ^ i,
2145
 
                                       ref_picture, pix_op,
2146
 
                                       s->mv[dir][2 * i + j][0],
2147
 
                                       s->mv[dir][2 * i + j][1],
2148
 
                                       block_s, mb_y);
2149
 
                }
2150
 
                pix_op = s->dsp.avg_h264_chroma_pixels_tab;
2151
 
            }
2152
 
        } else {
2153
 
            for (i = 0; i < 2; i++) {
2154
 
                mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2155
 
                                   0, 0, s->picture_structure != i + 1,
2156
 
                                   ref_picture, pix_op,
2157
 
                                   s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
2158
 
                                   2 * block_s, mb_y >> 1);
2159
 
 
2160
 
                // after put we make avg of the same block
2161
 
                pix_op = s->dsp.avg_h264_chroma_pixels_tab;
2162
 
 
2163
 
                // opposite parity is always in the same
2164
 
                // frame if this is second field
2165
 
                if (!s->first_field) {
2166
 
                    ref_picture = s->current_picture_ptr->f.data;
2167
 
                }
2168
 
            }
2169
 
        }
2170
 
        break;
2171
 
    default:
2172
 
        assert(0);
2173
 
    }
2174
 
}
2175
 
 
2176
2029
/**
2177
2030
 * find the lowest MB row referenced in the MVs
2178
2031
 */
2179
 
int MPV_lowest_referenced_row(MpegEncContext *s, int dir)
 
2032
int ff_MPV_lowest_referenced_row(MpegEncContext *s, int dir)
2180
2033
{
2181
2034
    int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
2182
2035
    int my, off, i, mvs;
2183
2036
 
2184
 
    if (s->picture_structure != PICT_FRAME) goto unhandled;
 
2037
    if (s->picture_structure != PICT_FRAME || s->mcsel)
 
2038
        goto unhandled;
2185
2039
 
2186
2040
    switch (s->mv_type) {
2187
2041
        case MV_TYPE_16X16:
2282
2136
 */
2283
2137
static av_always_inline
2284
2138
void MPV_decode_mb_internal(MpegEncContext *s, DCTELEM block[12][64],
2285
 
                            int lowres_flag, int is_mpeg12)
 
2139
                            int is_mpeg12)
2286
2140
{
2287
2141
    const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2288
2142
    if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
2327
2181
        qpel_mc_func (*op_qpix)[16];
2328
2182
        const int linesize   = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
2329
2183
        const int uvlinesize = s->current_picture.f.linesize[1];
2330
 
        const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
2331
 
        const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
 
2184
        const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
 
2185
        const int block_size = 8;
2332
2186
 
2333
2187
        /* avoid copy if macroblock skipped in last frame too */
2334
2188
        /* skip only during decoding as we might trash the buffers during encoding a bit */
2366
2220
 
2367
2221
                if(HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
2368
2222
                    if (s->mv_dir & MV_DIR_FORWARD) {
2369
 
                        ff_thread_await_progress((AVFrame*)s->last_picture_ptr, MPV_lowest_referenced_row(s, 0), 0);
 
2223
                        ff_thread_await_progress(&s->last_picture_ptr->f,
 
2224
                                                 ff_MPV_lowest_referenced_row(s, 0),
 
2225
                                                 0);
2370
2226
                    }
2371
2227
                    if (s->mv_dir & MV_DIR_BACKWARD) {
2372
 
                        ff_thread_await_progress((AVFrame*)s->next_picture_ptr, MPV_lowest_referenced_row(s, 1), 0);
 
2228
                        ff_thread_await_progress(&s->next_picture_ptr->f,
 
2229
                                                 ff_MPV_lowest_referenced_row(s, 1),
 
2230
                                                 0);
2373
2231
                    }
2374
2232
                }
2375
2233
 
2376
 
                if(lowres_flag){
2377
 
                    h264_chroma_mc_func *op_pix = s->dsp.put_h264_chroma_pixels_tab;
2378
 
 
2379
 
                    if (s->mv_dir & MV_DIR_FORWARD) {
2380
 
                        MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix);
2381
 
                        op_pix = s->dsp.avg_h264_chroma_pixels_tab;
2382
 
                    }
2383
 
                    if (s->mv_dir & MV_DIR_BACKWARD) {
2384
 
                        MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix);
2385
 
                    }
 
2234
                op_qpix= s->me.qpel_put;
 
2235
                if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
 
2236
                    op_pix = s->dsp.put_pixels_tab;
2386
2237
                }else{
2387
 
                    op_qpix= s->me.qpel_put;
2388
 
                    if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2389
 
                        op_pix = s->dsp.put_pixels_tab;
2390
 
                    }else{
2391
 
                        op_pix = s->dsp.put_no_rnd_pixels_tab;
2392
 
                    }
2393
 
                    if (s->mv_dir & MV_DIR_FORWARD) {
2394
 
                        MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
2395
 
                        op_pix = s->dsp.avg_pixels_tab;
2396
 
                        op_qpix= s->me.qpel_avg;
2397
 
                    }
2398
 
                    if (s->mv_dir & MV_DIR_BACKWARD) {
2399
 
                        MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
2400
 
                    }
 
2238
                    op_pix = s->dsp.put_no_rnd_pixels_tab;
 
2239
                }
 
2240
                if (s->mv_dir & MV_DIR_FORWARD) {
 
2241
                    ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f.data, op_pix, op_qpix);
 
2242
                    op_pix = s->dsp.avg_pixels_tab;
 
2243
                    op_qpix= s->me.qpel_avg;
 
2244
                }
 
2245
                if (s->mv_dir & MV_DIR_BACKWARD) {
 
2246
                    ff_MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f.data, op_pix, op_qpix);
2401
2247
                }
2402
2248
            }
2403
2249
 
2410
2256
            }
2411
2257
 
2412
2258
            /* add dct residue */
2413
 
            if(s->encoding || !(   s->msmpeg4_version || s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO
2414
 
                                || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){
 
2259
            if(s->encoding || !(   s->msmpeg4_version || s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO
 
2260
                                || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
2415
2261
                add_dequant_dct(s, block[0], 0, dest_y                          , dct_linesize, s->qscale);
2416
2262
                add_dequant_dct(s, block[1], 1, dest_y              + block_size, dct_linesize, s->qscale);
2417
2263
                add_dequant_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize, s->qscale);
2430
2276
                        add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2431
2277
                    }
2432
2278
                }
2433
 
            } else if(is_mpeg12 || (s->codec_id != CODEC_ID_WMV2)){
 
2279
            } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
2434
2280
                add_dct(s, block[0], 0, dest_y                          , dct_linesize);
2435
2281
                add_dct(s, block[1], 1, dest_y              + block_size, dct_linesize);
2436
2282
                add_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize);
2463
2309
            }
2464
2310
        } else {
2465
2311
            /* dct only in intra block */
2466
 
            if(s->encoding || !(s->codec_id==CODEC_ID_MPEG1VIDEO || s->codec_id==CODEC_ID_MPEG2VIDEO)){
 
2312
            if(s->encoding || !(s->codec_id==AV_CODEC_ID_MPEG1VIDEO || s->codec_id==AV_CODEC_ID_MPEG2VIDEO)){
2467
2313
                put_dct(s, block[0], 0, dest_y                          , dct_linesize, s->qscale);
2468
2314
                put_dct(s, block[1], 1, dest_y              + block_size, dct_linesize, s->qscale);
2469
2315
                put_dct(s, block[2], 2, dest_y + dct_offset             , dct_linesize, s->qscale);
2520
2366
    }
2521
2367
}
2522
2368
 
2523
 
void MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
 
2369
void ff_MPV_decode_mb(MpegEncContext *s, DCTELEM block[12][64]){
2524
2370
#if !CONFIG_SMALL
2525
2371
    if(s->out_format == FMT_MPEG1) {
2526
 
        if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 1);
2527
 
        else                 MPV_decode_mb_internal(s, block, 0, 1);
 
2372
        MPV_decode_mb_internal(s, block, 1);
2528
2373
    } else
2529
2374
#endif
2530
 
    if(s->avctx->lowres) MPV_decode_mb_internal(s, block, 1, 0);
2531
 
    else                  MPV_decode_mb_internal(s, block, 0, 0);
 
2375
        MPV_decode_mb_internal(s, block, 0);
2532
2376
}
2533
2377
 
2534
2378
/**
2547
2391
       && s->current_picture.f.reference
2548
2392
       && !s->intra_only
2549
2393
       && !(s->flags&CODEC_FLAG_EMU_EDGE)) {
 
2394
        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(s->avctx->pix_fmt);
2550
2395
        int sides = 0, edge_h;
2551
 
        int hshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_w;
2552
 
        int vshift = av_pix_fmt_descriptors[s->avctx->pix_fmt].log2_chroma_h;
 
2396
        int hshift = desc->log2_chroma_w;
 
2397
        int vshift = desc->log2_chroma_h;
2553
2398
        if (y==0) sides |= EDGE_TOP;
2554
2399
        if (y + h >= s->v_edge_pos) sides |= EDGE_BOTTOM;
2555
2400
 
2576
2421
        int i;
2577
2422
 
2578
2423
        if(s->pict_type==AV_PICTURE_TYPE_B || s->low_delay || (s->avctx->slice_flags&SLICE_FLAG_CODED_ORDER))
2579
 
            src= (AVFrame*)s->current_picture_ptr;
 
2424
            src = &s->current_picture_ptr->f;
2580
2425
        else if(s->last_picture_ptr)
2581
 
            src= (AVFrame*)s->last_picture_ptr;
 
2426
            src = &s->last_picture_ptr->f;
2582
2427
        else
2583
2428
            return;
2584
2429
 
2603
2448
void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
2604
2449
    const int linesize   = s->current_picture.f.linesize[0]; //not s->linesize as this would be wrong for field pics
2605
2450
    const int uvlinesize = s->current_picture.f.linesize[1];
2606
 
    const int mb_size= 4 - s->avctx->lowres;
 
2451
    const int mb_size= 4;
2607
2452
 
2608
2453
    s->block_index[0]= s->b8_stride*(s->mb_y*2    ) - 2 + s->mb_x*2;
2609
2454
    s->block_index[1]= s->b8_stride*(s->mb_y*2    ) - 1 + s->mb_x*2;
2894
2739
    s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
2895
2740
}
2896
2741
 
2897
 
void MPV_report_decode_progress(MpegEncContext *s)
 
2742
void ff_MPV_report_decode_progress(MpegEncContext *s)
2898
2743
{
2899
2744
    if (s->pict_type != AV_PICTURE_TYPE_B && !s->partitioned_frame && !s->error_occurred)
2900
 
        ff_thread_report_progress((AVFrame*)s->current_picture_ptr, s->mb_y, 0);
 
2745
        ff_thread_report_progress(&s->current_picture_ptr->f, s->mb_y, 0);
2901
2746
}