~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/h264.c

  • Committer: Gauvain Pocentek
  • Date: 2012-03-06 11:59:12 UTC
  • mfrom: (66.1.15 precise)
  • Revision ID: gauvain@pocentek.net-20120306115912-h9d6kt9j0l532oo5
* Merge from Ubuntu:
  - put back faac support
  - recommends apport-hooks-medibuntu
  - change Maintainer, Uploaders & Vcs-* fields.
* New upstream snapshot
* upload to unstable
* Build against external libmpeg2
* drop 51_FTBFS_arm.patch again
* no longer build depend on libcdparanoia-dev on the Hurd
* Fix FTBFS on the hurd.
  Thanks to Samuel Thibault <sthibault@debian.org> (Closes: #654974)
* Fix FTBFS on arm
* New upstream snapshot, Closes: #650339, #643621, #481807
* Imported Upstream version 1.0~rc4+svn34492
* Bump standards version
* Bump dependency on libav >= 4:0.8~, Closes: #653887
* Fix build-indep
* Build mplayer-gui again, Closes: #568514
* Drop debian/all-lang-config-mak.sh, no longer needed
* include .dfsg1 in version number
* remove get-orig-source target
* no longer prune compiler flags from the environment
* No longer advertise nor build 3fdx, mga and dxr3 backends,
  Closes: #496106, #442181, #533546
* beautify mplayer version identification string
* Brown paperbag upload.
* Next try to fix build failure on sparce after recent binutils change.
* Brown paperbag upload.
* Really fix build failure on sparc after recent binutils change.
* Properly set Replaces/Conflicts on mplayer2{,-dbg} to avoid
  file overwrite errors.
* Adjust versioning of mplayer listed in the mplayer-dbg's Depends field.
* Fix build failure on sparc after recent binutils change.
* Urgency medium bumped because of RC-level bugfix
  and speeding up x264 transition.
* Update to my @debian.org email.
* Upload to unstable
* Enable joystick support on Linux only, Closes: #638408
* Rebuild fixes toolchain issue on arm, Closes: #637077
* New upstream snapshot
* following the discussion started by Diego Biurrun <diego@biurrun.de>
  in debian-devel, I have prepared a new packaging of 'mplayer'
  (with code that comes from CVS)
* the upstream tar.bz cannot be distributed by Debian, since it contains
   CSS code; so I am repackaging it 
* I have tried my best to address all known issues:
  - the package contains the detailed Copyright made by Diego Biurrun 
  - the package does not contain CSS code, or  AFAIK other code on which 
     there is active patent enforcement
  - there is a script  debian/cvs-changelog.sh  that shows all changes
     done to files included in this source.
    This should comply with GPLv2 sec 2.a  (in spirit if not in letter)
    For this reason, the source code contains CVS directories.
* needs   make (>= 3.80) for 'html-chunked-$(1)' in DOCS/xml/Makefile

* some corrections, as suggested Diego Biurrun
  - binary codecs should go into /usr/lib/codecs (upstream default)
  - better template 'mplayer/install_codecs'
  - an empty 'font=' in mplayer.conf breaks mplayer: postinst corrected
* correction in 'mplayer/cfgnote'
* better mplayer.postinst and mplayer.config

* New upstream release
* better debian/copyright file
* do not ship a skin
* New upstream release
* changed DEB_BUILD_OPTIONS to DEB_BUILD_CONFIGURE ,
  DEB_BUILD_OPTIONS is used as in debian policy
* use gcc-3.4
* changed xlibs-dev to a long list of dependencies, for Debian/etch
* try to adhere to  http://www.mplayerhq.hu/DOCS/tech/binary-packaging.txt
  (see README.Debian for details)
* removed dependency on xlibmesa-dev, disabled opengl
* New upstream release
* Simon McVittie <hacks@pseudorandom.co.uk> wonderful work:
- Work around Debian bug #267442 (glibc's sys/uio.h and gcc's altivec.h have
  conflicting uses for __vector) by re-ordering #includes
- Fix potential symlink attack in ./configure
- Disable support for binary codecs on platforms for which those codecs
  aren't available; also disable the corresponding Debconf note when it's
  inappropriate
- Changed Build-Depends: so it works in pbuilder
- Explicitly build-depend on libjpeg62-dev, libfontconfig1-dev,
  libungif4-dev 
- Tweak debian/rules to avoid certain errors being ignored
- Use --language=all
* provide a target  'debian/rules get-orig-source' 
  that recreates the orig.tar.gz ; then use the above orig.tar.gz
* rewrote some parts of debian/rules
* don't clean and recompile docs if upstream ships them
* mplayer-doc was shipping too much stuff
* translated man pages where not installed properly
* compile with libdv4-dev
* correct README.Debian
* Forgot build-dep on libtheora
* Must not depend on libxvidcore
* New upstream release
* new release.
* rc1 to become 0.90
* new pre-release
* new pre-release
* gtk bug fixed.
* new release.
* version bumped
* 0.60 pre2 release
* 0.60 pre-release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
static const enum PixelFormat hwaccel_pixfmt_list_h264_jpeg_420[] = {
57
57
    PIX_FMT_DXVA2_VLD,
58
58
    PIX_FMT_VAAPI_VLD,
 
59
    PIX_FMT_VDA_VLD,
59
60
    PIX_FMT_YUVJ420P,
60
61
    PIX_FMT_NONE
61
62
};
62
63
 
63
 
void ff_h264_write_back_intra_pred_mode(H264Context *h){
64
 
    int8_t *mode= h->intra4x4_pred_mode + h->mb2br_xy[h->mb_xy];
65
 
 
66
 
    AV_COPY32(mode, h->intra4x4_pred_mode_cache + 4 + 8*4);
67
 
    mode[4]= h->intra4x4_pred_mode_cache[7+8*3];
68
 
    mode[5]= h->intra4x4_pred_mode_cache[7+8*2];
69
 
    mode[6]= h->intra4x4_pred_mode_cache[7+8*1];
70
 
}
71
 
 
72
64
/**
73
 
 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
 
65
 * Check if the top & left blocks are available if needed and
 
66
 * change the dc mode so it only uses the available blocks.
74
67
 */
75
68
int ff_h264_check_intra4x4_pred_mode(H264Context *h){
76
69
    MpegEncContext * const s = &h->s;
109
102
} //FIXME cleanup like ff_h264_check_intra_pred_mode
110
103
 
111
104
/**
112
 
 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
 
105
 * Check if the top & left blocks are available if needed and
 
106
 * change the dc mode so it only uses the available blocks.
113
107
 */
114
108
int ff_h264_check_intra_pred_mode(H264Context *h, int mode){
115
109
    MpegEncContext * const s = &h->s;
270
264
        // Error resilience puts the current picture in the ref list.
271
265
        // Don't try to wait on these as it will cause a deadlock.
272
266
        // Fields can wait on each other, though.
273
 
        if(ref->thread_opaque != s->current_picture.thread_opaque ||
274
 
           (ref->reference&3) != s->picture_structure) {
 
267
        if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
 
268
           (ref->f.reference & 3) != s->picture_structure) {
275
269
            my = get_lowest_part_list_y(h, ref, n, height, y_offset, 0);
276
270
            if (refs[0][ref_n] < 0) nrefs[0] += 1;
277
271
            refs[0][ref_n] = FFMAX(refs[0][ref_n], my);
282
276
        int ref_n = h->ref_cache[1][ scan8[n] ];
283
277
        Picture *ref= &h->ref_list[1][ref_n];
284
278
 
285
 
        if(ref->thread_opaque != s->current_picture.thread_opaque ||
286
 
           (ref->reference&3) != s->picture_structure) {
 
279
        if (ref->f.thread_opaque != s->current_picture.f.thread_opaque ||
 
280
           (ref->f.reference & 3) != s->picture_structure) {
287
281
            my = get_lowest_part_list_y(h, ref, n, height, y_offset, 1);
288
282
            if (refs[1][ref_n] < 0) nrefs[1] += 1;
289
283
            refs[1][ref_n] = FFMAX(refs[1][ref_n], my);
299
293
static void await_references(H264Context *h){
300
294
    MpegEncContext * const s = &h->s;
301
295
    const int mb_xy= h->mb_xy;
302
 
    const int mb_type= s->current_picture.mb_type[mb_xy];
 
296
    const int mb_type = s->current_picture.f.mb_type[mb_xy];
303
297
    int refs[2][48];
304
298
    int nrefs[2] = {0};
305
299
    int ref, list;
359
353
            int row = refs[list][ref];
360
354
            if(row >= 0){
361
355
                Picture *ref_pic = &h->ref_list[list][ref];
362
 
                int ref_field = ref_pic->reference - 1;
 
356
                int ref_field = ref_pic->f.reference - 1;
363
357
                int ref_field_picture = ref_pic->field_picture;
364
358
                int pic_height = 16*s->mb_height >> ref_field_picture;
365
359
 
447
441
}
448
442
#endif
449
443
 
450
 
static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
451
 
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
452
 
                           int src_x_offset, int src_y_offset,
453
 
                           qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op,
454
 
                           int pixel_shift, int chroma444){
 
444
static av_always_inline void
 
445
mc_dir_part(H264Context *h, Picture *pic, int n, int square,
 
446
            int height, int delta, int list,
 
447
            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
 
448
            int src_x_offset, int src_y_offset,
 
449
            qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op,
 
450
            int pixel_shift, int chroma_idc)
 
451
{
455
452
    MpegEncContext * const s = &h->s;
456
453
    const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
457
454
    int my=       h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
458
455
    const int luma_xy= (mx&3) + ((my&3)<<2);
459
456
    int offset = ((mx>>2) << pixel_shift) + (my>>2)*h->mb_linesize;
460
 
    uint8_t * src_y = pic->data[0] + offset;
 
457
    uint8_t * src_y = pic->f.data[0] + offset;
461
458
    uint8_t * src_cb, * src_cr;
462
459
    int extra_width= h->emu_edge_width;
463
460
    int extra_height= h->emu_edge_height;
466
463
    const int full_my= my>>2;
467
464
    const int pic_width  = 16*s->mb_width;
468
465
    const int pic_height = 16*s->mb_height >> MB_FIELD;
 
466
    int ysh;
469
467
 
470
468
    if(mx&7) extra_width -= 3;
471
469
    if(my&7) extra_height -= 3;
474
472
       || full_my < 0-extra_height
475
473
       || full_mx + 16/*FIXME*/ > pic_width + extra_width
476
474
       || full_my + 16/*FIXME*/ > pic_height + extra_height){
477
 
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_y - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
 
475
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_y - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize,
 
476
                                16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
478
477
            src_y= s->edge_emu_buffer + (2 << pixel_shift) + 2*h->mb_linesize;
479
478
        emu=1;
480
479
    }
486
485
 
487
486
    if(CONFIG_GRAY && s->flags&CODEC_FLAG_GRAY) return;
488
487
 
489
 
    if(chroma444){
490
 
        src_cb = pic->data[1] + offset;
 
488
    if(chroma_idc == 3 /* yuv444 */){
 
489
        src_cb = pic->f.data[1] + offset;
491
490
        if(emu){
492
491
            s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize,
493
492
                                    16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
498
497
            qpix_op[luma_xy](dest_cb + delta, src_cb + delta, h->mb_linesize);
499
498
        }
500
499
 
501
 
        src_cr = pic->data[2] + offset;
 
500
        src_cr = pic->f.data[2] + offset;
502
501
        if(emu){
503
502
            s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr - (2 << pixel_shift) - 2*h->mb_linesize, h->mb_linesize,
504
503
                                    16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
511
510
        return;
512
511
    }
513
512
 
514
 
    if(MB_FIELD){
 
513
    ysh = 3 - (chroma_idc == 2 /* yuv422 */);
 
514
    if(chroma_idc == 1 /* yuv420 */ && MB_FIELD){
515
515
        // chroma offset when predicting from a field of opposite parity
516
 
        my += 2 * ((s->mb_y & 1) - (pic->reference - 1));
 
516
        my += 2 * ((s->mb_y & 1) - (pic->f.reference - 1));
517
517
        emu |= (my>>3) < 0 || (my>>3) + 8 >= (pic_height>>1);
518
518
    }
519
 
    src_cb= pic->data[1] + ((mx>>3) << pixel_shift) + (my>>3)*h->mb_uvlinesize;
520
 
    src_cr= pic->data[2] + ((mx>>3) << pixel_shift) + (my>>3)*h->mb_uvlinesize;
 
519
 
 
520
    src_cb = pic->f.data[1] + ((mx >> 3) << pixel_shift) + (my >> ysh) * h->mb_uvlinesize;
 
521
    src_cr = pic->f.data[2] + ((mx >> 3) << pixel_shift) + (my >> ysh) * h->mb_uvlinesize;
521
522
 
522
523
    if(emu){
523
 
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
 
524
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cb, h->mb_uvlinesize,
 
525
                                9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
 
526
                                pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
524
527
            src_cb= s->edge_emu_buffer;
525
528
    }
526
 
    chroma_op(dest_cb, src_cb, h->mb_uvlinesize, chroma_height, mx&7, my&7);
 
529
    chroma_op(dest_cb, src_cb, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
 
530
              mx&7, (my << (chroma_idc == 2 /* yuv422 */)) &7);
527
531
 
528
532
    if(emu){
529
 
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
 
533
        s->dsp.emulated_edge_mc(s->edge_emu_buffer, src_cr, h->mb_uvlinesize,
 
534
                                9, 8 * chroma_idc + 1, (mx >> 3), (my >> ysh),
 
535
                                pic_width >> 1, pic_height >> (chroma_idc == 1 /* yuv420 */));
530
536
            src_cr= s->edge_emu_buffer;
531
537
    }
532
 
    chroma_op(dest_cr, src_cr, h->mb_uvlinesize, chroma_height, mx&7, my&7);
 
538
    chroma_op(dest_cr, src_cr, h->mb_uvlinesize, height >> (chroma_idc == 1 /* yuv420 */),
 
539
              mx&7, (my << (chroma_idc == 2 /* yuv422 */)) &7);
533
540
}
534
541
 
535
 
static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
536
 
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
537
 
                           int x_offset, int y_offset,
538
 
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
539
 
                           qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
540
 
                           int list0, int list1, int pixel_shift, int chroma444){
 
542
static av_always_inline void
 
543
mc_part_std(H264Context *h, int n, int square, int height, int delta,
 
544
            uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
 
545
            int x_offset, int y_offset,
 
546
            qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
 
547
            qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
 
548
            int list0, int list1, int pixel_shift, int chroma_idc)
 
549
{
541
550
    MpegEncContext * const s = &h->s;
542
551
    qpel_mc_func *qpix_op=  qpix_put;
543
552
    h264_chroma_mc_func chroma_op= chroma_put;
544
553
 
545
554
    dest_y  += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
546
 
    if(chroma444){
 
555
    if (chroma_idc == 3 /* yuv444 */) {
547
556
        dest_cb += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
548
557
        dest_cr += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
549
 
    }else{
 
558
    } else if (chroma_idc == 2 /* yuv422 */) {
 
559
        dest_cb += (  x_offset << pixel_shift) + 2*y_offset*h->mb_uvlinesize;
 
560
        dest_cr += (  x_offset << pixel_shift) + 2*y_offset*h->mb_uvlinesize;
 
561
    } else /* yuv420 */ {
550
562
        dest_cb += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
551
563
        dest_cr += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
552
564
    }
555
567
 
556
568
    if(list0){
557
569
        Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
558
 
        mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
 
570
        mc_dir_part(h, ref, n, square, height, delta, 0,
559
571
                           dest_y, dest_cb, dest_cr, x_offset, y_offset,
560
 
                           qpix_op, chroma_op, pixel_shift, chroma444);
 
572
                           qpix_op, chroma_op, pixel_shift, chroma_idc);
561
573
 
562
574
        qpix_op=  qpix_avg;
563
575
        chroma_op= chroma_avg;
565
577
 
566
578
    if(list1){
567
579
        Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
568
 
        mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
 
580
        mc_dir_part(h, ref, n, square, height, delta, 1,
569
581
                           dest_y, dest_cb, dest_cr, x_offset, y_offset,
570
 
                           qpix_op, chroma_op, pixel_shift, chroma444);
 
582
                           qpix_op, chroma_op, pixel_shift, chroma_idc);
571
583
    }
572
584
}
573
585
 
574
 
static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
575
 
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
576
 
                           int x_offset, int y_offset,
577
 
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
578
 
                           h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
579
 
                           h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
580
 
                           int list0, int list1, int pixel_shift, int chroma444){
 
586
static av_always_inline void
 
587
mc_part_weighted(H264Context *h, int n, int square, int height, int delta,
 
588
                 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
 
589
                 int x_offset, int y_offset,
 
590
                 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
 
591
                 h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
 
592
                 h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
 
593
                 int list0, int list1, int pixel_shift, int chroma_idc){
581
594
    MpegEncContext * const s = &h->s;
 
595
    int chroma_height;
582
596
 
583
597
    dest_y += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
584
 
    if(chroma444){
 
598
    if (chroma_idc == 3 /* yuv444 */) {
 
599
        chroma_height = height;
585
600
        chroma_weight_avg = luma_weight_avg;
586
601
        chroma_weight_op = luma_weight_op;
587
602
        dest_cb += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
588
603
        dest_cr += (2*x_offset << pixel_shift) + 2*y_offset*h->mb_linesize;
589
 
    }else{
 
604
    } else if (chroma_idc == 2 /* yuv422 */) {
 
605
        chroma_height = height;
 
606
        dest_cb += (  x_offset << pixel_shift) + 2*y_offset*h->mb_uvlinesize;
 
607
        dest_cr += (  x_offset << pixel_shift) + 2*y_offset*h->mb_uvlinesize;
 
608
    } else /* yuv420 */ {
 
609
        chroma_height = height >> 1;
590
610
        dest_cb += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
591
611
        dest_cr += (  x_offset << pixel_shift) +   y_offset*h->mb_uvlinesize;
592
612
    }
602
622
        int refn0 = h->ref_cache[0][ scan8[n] ];
603
623
        int refn1 = h->ref_cache[1][ scan8[n] ];
604
624
 
605
 
        mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
 
625
        mc_dir_part(h, &h->ref_list[0][refn0], n, square, height, delta, 0,
606
626
                    dest_y, dest_cb, dest_cr,
607
 
                    x_offset, y_offset, qpix_put, chroma_put, pixel_shift, chroma444);
608
 
        mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
 
627
                    x_offset, y_offset, qpix_put, chroma_put,
 
628
                    pixel_shift, chroma_idc);
 
629
        mc_dir_part(h, &h->ref_list[1][refn1], n, square, height, delta, 1,
609
630
                    tmp_y, tmp_cb, tmp_cr,
610
 
                    x_offset, y_offset, qpix_put, chroma_put, pixel_shift, chroma444);
 
631
                    x_offset, y_offset, qpix_put, chroma_put,
 
632
                    pixel_shift, chroma_idc);
611
633
 
612
634
        if(h->use_weight == 2){
613
635
            int weight0 = h->implicit_weight[refn0][refn1][s->mb_y&1];
614
636
            int weight1 = 64 - weight0;
615
 
            luma_weight_avg(  dest_y,  tmp_y,  h->  mb_linesize, 5, weight0, weight1, 0);
616
 
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, 5, weight0, weight1, 0);
617
 
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, 5, weight0, weight1, 0);
 
637
            luma_weight_avg(  dest_y,  tmp_y,  h->  mb_linesize,
 
638
                              height,        5, weight0, weight1, 0);
 
639
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize,
 
640
                              chroma_height, 5, weight0, weight1, 0);
 
641
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize,
 
642
                              chroma_height, 5, weight0, weight1, 0);
618
643
        }else{
619
 
            luma_weight_avg(dest_y, tmp_y, h->mb_linesize, h->luma_log2_weight_denom,
 
644
            luma_weight_avg(dest_y, tmp_y, h->mb_linesize, height, h->luma_log2_weight_denom,
620
645
                            h->luma_weight[refn0][0][0] , h->luma_weight[refn1][1][0],
621
646
                            h->luma_weight[refn0][0][1] + h->luma_weight[refn1][1][1]);
622
 
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
 
647
            chroma_weight_avg(dest_cb, tmp_cb, h->mb_uvlinesize, chroma_height, h->chroma_log2_weight_denom,
623
648
                            h->chroma_weight[refn0][0][0][0] , h->chroma_weight[refn1][1][0][0],
624
649
                            h->chroma_weight[refn0][0][0][1] + h->chroma_weight[refn1][1][0][1]);
625
 
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
 
650
            chroma_weight_avg(dest_cr, tmp_cr, h->mb_uvlinesize, chroma_height, h->chroma_log2_weight_denom,
626
651
                            h->chroma_weight[refn0][0][1][0] , h->chroma_weight[refn1][1][1][0],
627
652
                            h->chroma_weight[refn0][0][1][1] + h->chroma_weight[refn1][1][1][1]);
628
653
        }
630
655
        int list = list1 ? 1 : 0;
631
656
        int refn = h->ref_cache[list][ scan8[n] ];
632
657
        Picture *ref= &h->ref_list[list][refn];
633
 
        mc_dir_part(h, ref, n, square, chroma_height, delta, list,
 
658
        mc_dir_part(h, ref, n, square, height, delta, list,
634
659
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
635
 
                    qpix_put, chroma_put, pixel_shift, chroma444);
 
660
                    qpix_put, chroma_put, pixel_shift, chroma_idc);
636
661
 
637
 
        luma_weight_op(dest_y, h->mb_linesize, h->luma_log2_weight_denom,
 
662
        luma_weight_op(dest_y, h->mb_linesize, height, h->luma_log2_weight_denom,
638
663
                       h->luma_weight[refn][list][0], h->luma_weight[refn][list][1]);
639
664
        if(h->use_weight_chroma){
640
 
            chroma_weight_op(dest_cb, h->mb_uvlinesize, h->chroma_log2_weight_denom,
 
665
            chroma_weight_op(dest_cb, h->mb_uvlinesize, chroma_height, h->chroma_log2_weight_denom,
641
666
                             h->chroma_weight[refn][list][0][0], h->chroma_weight[refn][list][0][1]);
642
 
            chroma_weight_op(dest_cr, h->mb_uvlinesize, h->chroma_log2_weight_denom,
 
667
            chroma_weight_op(dest_cr, h->mb_uvlinesize, chroma_height, h->chroma_log2_weight_denom,
643
668
                             h->chroma_weight[refn][list][1][0], h->chroma_weight[refn][list][1][1]);
644
669
        }
645
670
    }
646
671
}
647
672
 
648
 
static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
649
 
                           uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
650
 
                           int x_offset, int y_offset,
651
 
                           qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
652
 
                           qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
653
 
                           h264_weight_func *weight_op, h264_biweight_func *weight_avg,
654
 
                           int list0, int list1, int pixel_shift, int chroma444){
 
673
static av_always_inline void
 
674
mc_part(H264Context *h, int n, int square, int height, int delta,
 
675
        uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
 
676
        int x_offset, int y_offset,
 
677
        qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
 
678
        qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
 
679
        h264_weight_func *weight_op, h264_biweight_func *weight_avg,
 
680
        int list0, int list1, int pixel_shift, int chroma_idc)
 
681
{
655
682
    if((h->use_weight==2 && list0 && list1
656
683
        && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ][h->s.mb_y&1] != 32))
657
684
       || h->use_weight==1)
658
 
        mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
 
685
        mc_part_weighted(h, n, square, height, delta, dest_y, dest_cb, dest_cr,
659
686
                         x_offset, y_offset, qpix_put, chroma_put,
660
 
                         weight_op[0], weight_op[3], weight_avg[0],
661
 
                         weight_avg[3], list0, list1, pixel_shift, chroma444);
 
687
                         weight_op[0], weight_op[1], weight_avg[0],
 
688
                         weight_avg[1], list0, list1, pixel_shift, chroma_idc);
662
689
    else
663
 
        mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
 
690
        mc_part_std(h, n, square, height, delta, dest_y, dest_cb, dest_cr,
664
691
                    x_offset, y_offset, qpix_put, chroma_put, qpix_avg,
665
 
                    chroma_avg, list0, list1, pixel_shift, chroma444);
 
692
                    chroma_avg, list0, list1, pixel_shift, chroma_idc);
666
693
}
667
694
 
668
 
static inline void prefetch_motion(H264Context *h, int list, int pixel_shift, int chroma444){
 
695
static av_always_inline void
 
696
prefetch_motion(H264Context *h, int list, int pixel_shift, int chroma_idc)
 
697
{
669
698
    /* fetch pixels for estimated mv 4 macroblocks ahead
670
699
     * optimized for 64byte cache lines */
671
700
    MpegEncContext * const s = &h->s;
673
702
    if(refn >= 0){
674
703
        const int mx= (h->mv_cache[list][scan8[0]][0]>>2) + 16*s->mb_x + 8;
675
704
        const int my= (h->mv_cache[list][scan8[0]][1]>>2) + 16*s->mb_y;
676
 
        uint8_t **src= h->ref_list[list][refn].data;
 
705
        uint8_t **src = h->ref_list[list][refn].f.data;
677
706
        int off= (mx << pixel_shift) + (my + (s->mb_x&3)*4)*h->mb_linesize + (64 << pixel_shift);
678
707
        s->dsp.prefetch(src[0]+off, s->linesize, 4);
679
 
        if(chroma444){
 
708
        if (chroma_idc == 3 /* yuv444 */) {
680
709
            s->dsp.prefetch(src[1]+off, s->linesize, 4);
681
710
            s->dsp.prefetch(src[2]+off, s->linesize, 4);
682
711
        }else{
690
719
                      qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
691
720
                      qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
692
721
                      h264_weight_func *weight_op, h264_biweight_func *weight_avg,
693
 
                      int pixel_shift, int chroma444){
 
722
                      int pixel_shift, int chroma_idc)
 
723
{
694
724
    MpegEncContext * const s = &h->s;
695
725
    const int mb_xy= h->mb_xy;
696
 
    const int mb_type= s->current_picture.mb_type[mb_xy];
 
726
    const int mb_type = s->current_picture.f.mb_type[mb_xy];
697
727
 
698
728
    assert(IS_INTER(mb_type));
699
729
 
700
 
    if(HAVE_PTHREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
 
730
    if(HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
701
731
        await_references(h);
702
 
    prefetch_motion(h, 0, pixel_shift, chroma444);
 
732
    prefetch_motion(h, 0, pixel_shift, chroma_idc);
703
733
 
704
734
    if(IS_16X16(mb_type)){
705
 
        mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
 
735
        mc_part(h, 0, 1, 16, 0, dest_y, dest_cb, dest_cr, 0, 0,
706
736
                qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
707
737
                weight_op, weight_avg,
708
738
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
709
 
                pixel_shift, chroma444);
 
739
                pixel_shift, chroma_idc);
710
740
    }else if(IS_16X8(mb_type)){
711
 
        mc_part(h, 0, 0, 4, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 0,
 
741
        mc_part(h, 0, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 0,
712
742
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
713
 
                &weight_op[1], &weight_avg[1],
 
743
                weight_op, weight_avg,
714
744
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
715
 
                pixel_shift, chroma444);
716
 
        mc_part(h, 8, 0, 4, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 4,
 
745
                pixel_shift, chroma_idc);
 
746
        mc_part(h, 8, 0, 8, 8 << pixel_shift, dest_y, dest_cb, dest_cr, 0, 4,
717
747
                qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
718
 
                &weight_op[1], &weight_avg[1],
 
748
                weight_op, weight_avg,
719
749
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
720
 
                pixel_shift, chroma444);
 
750
                pixel_shift, chroma_idc);
721
751
    }else if(IS_8X16(mb_type)){
722
 
        mc_part(h, 0, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
 
752
        mc_part(h, 0, 0, 16, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 0, 0,
723
753
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
724
 
                &weight_op[2], &weight_avg[2],
 
754
                &weight_op[1], &weight_avg[1],
725
755
                IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1),
726
 
                pixel_shift, chroma444);
727
 
        mc_part(h, 4, 0, 8, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
 
756
                pixel_shift, chroma_idc);
 
757
        mc_part(h, 4, 0, 16, 8*h->mb_linesize, dest_y, dest_cb, dest_cr, 4, 0,
728
758
                qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
729
 
                &weight_op[2], &weight_avg[2],
 
759
                &weight_op[1], &weight_avg[1],
730
760
                IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1),
731
 
                pixel_shift, chroma444);
 
761
                pixel_shift, chroma_idc);
732
762
    }else{
733
763
        int i;
734
764
 
741
771
            int y_offset= (i&2)<<1;
742
772
 
743
773
            if(IS_SUB_8X8(sub_mb_type)){
744
 
                mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
 
774
                mc_part(h, n, 1, 8, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
745
775
                    qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
746
 
                    &weight_op[3], &weight_avg[3],
 
776
                    &weight_op[1], &weight_avg[1],
747
777
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
748
 
                    pixel_shift, chroma444);
 
778
                    pixel_shift, chroma_idc);
749
779
            }else if(IS_SUB_8X4(sub_mb_type)){
750
 
                mc_part(h, n  , 0, 2, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset,
751
 
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
752
 
                    &weight_op[4], &weight_avg[4],
753
 
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
754
 
                    pixel_shift, chroma444);
755
 
                mc_part(h, n+2, 0, 2, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
756
 
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
757
 
                    &weight_op[4], &weight_avg[4],
758
 
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
759
 
                    pixel_shift, chroma444);
 
780
                mc_part(h, n  , 0, 4, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset,
 
781
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
 
782
                    &weight_op[1], &weight_avg[1],
 
783
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
 
784
                    pixel_shift, chroma_idc);
 
785
                mc_part(h, n+2, 0, 4, 4 << pixel_shift, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
 
786
                    qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
 
787
                    &weight_op[1], &weight_avg[1],
 
788
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
 
789
                    pixel_shift, chroma_idc);
760
790
            }else if(IS_SUB_4X8(sub_mb_type)){
761
 
                mc_part(h, n  , 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
762
 
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
763
 
                    &weight_op[5], &weight_avg[5],
764
 
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
765
 
                    pixel_shift, chroma444);
766
 
                mc_part(h, n+1, 0, 4, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
767
 
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
768
 
                    &weight_op[5], &weight_avg[5],
769
 
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
770
 
                    pixel_shift, chroma444);
 
791
                mc_part(h, n  , 0, 8, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
 
792
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
 
793
                    &weight_op[2], &weight_avg[2],
 
794
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
 
795
                    pixel_shift, chroma_idc);
 
796
                mc_part(h, n+1, 0, 8, 4*h->mb_linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
 
797
                    qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
 
798
                    &weight_op[2], &weight_avg[2],
 
799
                    IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
 
800
                    pixel_shift, chroma_idc);
771
801
            }else{
772
802
                int j;
773
803
                assert(IS_SUB_4X4(sub_mb_type));
774
804
                for(j=0; j<4; j++){
775
805
                    int sub_x_offset= x_offset + 2*(j&1);
776
806
                    int sub_y_offset= y_offset +   (j&2);
777
 
                    mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
 
807
                    mc_part(h, n+j, 1, 4, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
778
808
                        qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
779
 
                        &weight_op[6], &weight_avg[6],
 
809
                        &weight_op[2], &weight_avg[2],
780
810
                        IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1),
781
 
                        pixel_shift, chroma444);
 
811
                        pixel_shift, chroma_idc);
782
812
                }
783
813
            }
784
814
        }
785
815
    }
786
816
 
787
 
    prefetch_motion(h, 1, pixel_shift, chroma444);
788
 
}
789
 
 
790
 
#define hl_motion_fn(sh, bits) \
791
 
static av_always_inline void hl_motion_ ## bits(H264Context *h, \
792
 
                                       uint8_t *dest_y, \
793
 
                                       uint8_t *dest_cb, uint8_t *dest_cr, \
794
 
                                       qpel_mc_func (*qpix_put)[16], \
795
 
                                       h264_chroma_mc_func (*chroma_put), \
796
 
                                       qpel_mc_func (*qpix_avg)[16], \
797
 
                                       h264_chroma_mc_func (*chroma_avg), \
798
 
                                       h264_weight_func *weight_op, \
799
 
                                       h264_biweight_func *weight_avg, \
800
 
                                       int chroma444) \
801
 
{ \
802
 
    hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put, \
803
 
              qpix_avg, chroma_avg, weight_op, weight_avg, sh, chroma444); \
804
 
}
805
 
hl_motion_fn(0, 8);
806
 
hl_motion_fn(1, 16);
 
817
    prefetch_motion(h, 1, pixel_shift, chroma_idc);
 
818
}
 
819
 
 
820
static av_always_inline void
 
821
hl_motion_420(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
 
822
              qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
 
823
              qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
 
824
              h264_weight_func *weight_op, h264_biweight_func *weight_avg,
 
825
              int pixel_shift)
 
826
{
 
827
    hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put,
 
828
              qpix_avg, chroma_avg, weight_op, weight_avg, pixel_shift, 1);
 
829
}
 
830
 
 
831
static av_always_inline void
 
832
hl_motion_422(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
 
833
              qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
 
834
              qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
 
835
              h264_weight_func *weight_op, h264_biweight_func *weight_avg,
 
836
              int pixel_shift)
 
837
{
 
838
    hl_motion(h, dest_y, dest_cb, dest_cr, qpix_put, chroma_put,
 
839
              qpix_avg, chroma_avg, weight_op, weight_avg, pixel_shift, 2);
 
840
}
807
841
 
808
842
static void free_tables(H264Context *h, int free_rbsp){
809
843
    int i;
969
1003
    dst->list_counts              = src->list_counts;
970
1004
 
971
1005
    dst->s.obmc_scratchpad = NULL;
972
 
    ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma);
 
1006
    ff_h264_pred_init(&dst->hpc, src->s.codec_id, src->sps.bit_depth_luma, src->sps.chroma_format_idc);
973
1007
}
974
1008
 
975
1009
/**
997
1031
    s->height = s->avctx->height;
998
1032
    s->codec_id= s->avctx->codec->id;
999
1033
 
1000
 
    ff_h264dsp_init(&h->h264dsp, 8);
1001
 
    ff_h264_pred_init(&h->hpc, s->codec_id, 8);
 
1034
    ff_h264dsp_init(&h->h264dsp, 8, 1);
 
1035
    ff_h264_pred_init(&h->hpc, s->codec_id, 8, 1);
1002
1036
 
1003
1037
    h->dequant_coeff_pps= -1;
1004
1038
    s->unrestricted_mv=1;
1005
 
    s->decode=1; //FIXME
1006
1039
 
1007
1040
    dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
1008
1041
 
1032
1065
        p += 6;
1033
1066
        for (i = 0; i < cnt; i++) {
1034
1067
            nalsize = AV_RB16(p) + 2;
 
1068
            if (p - avctx->extradata + nalsize > avctx->extradata_size)
 
1069
                return -1;
1035
1070
            if(decode_nal_units(h, p, nalsize) < 0) {
1036
1071
                av_log(avctx, AV_LOG_ERROR, "Decoding sps %d from avcC failed\n", i);
1037
1072
                return -1;
1042
1077
        cnt = *(p++); // Number of pps
1043
1078
        for (i = 0; i < cnt; i++) {
1044
1079
            nalsize = AV_RB16(p) + 2;
 
1080
            if (p - avctx->extradata + nalsize > avctx->extradata_size)
 
1081
                return -1;
1045
1082
            if (decode_nal_units(h, p, nalsize) < 0) {
1046
1083
                av_log(avctx, AV_LOG_ERROR, "Decoding pps %d from avcC failed\n", i);
1047
1084
                return -1;
1061
1098
av_cold int ff_h264_decode_init(AVCodecContext *avctx){
1062
1099
    H264Context *h= avctx->priv_data;
1063
1100
    MpegEncContext * const s = &h->s;
 
1101
    int i;
1064
1102
 
1065
1103
    MPV_decode_defaults(s);
1066
1104
 
1085
1123
 
1086
1124
    h->thread_context[0] = h;
1087
1125
    h->outputed_poc = h->next_outputed_poc = INT_MIN;
 
1126
    for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
 
1127
        h->last_pocs[i] = INT_MIN;
1088
1128
    h->prev_poc_msb= 1<<16;
1089
1129
    h->x264_build = -1;
1090
1130
    ff_h264_reset_sei(h);
1135
1175
static int decode_init_thread_copy(AVCodecContext *avctx){
1136
1176
    H264Context *h= avctx->priv_data;
1137
1177
 
1138
 
    if (!avctx->is_copy) return 0;
 
1178
    if (!avctx->internal->is_copy)
 
1179
        return 0;
1139
1180
    memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1140
1181
    memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1141
1182
 
1165
1206
        memcpy(&h->s + 1, &h1->s + 1, sizeof(H264Context) - sizeof(MpegEncContext)); //copy all fields after MpegEnc
1166
1207
        memset(h->sps_buffers, 0, sizeof(h->sps_buffers));
1167
1208
        memset(h->pps_buffers, 0, sizeof(h->pps_buffers));
1168
 
        ff_h264_alloc_tables(h);
 
1209
        if (ff_h264_alloc_tables(h) < 0) {
 
1210
            av_log(dst, AV_LOG_ERROR, "Could not allocate memory for h264\n");
 
1211
            return AVERROR(ENOMEM);
 
1212
        }
1169
1213
        context_init(h);
1170
1214
 
1171
1215
        for(i=0; i<2; i++){
1221
1265
    if(!s->current_picture_ptr) return 0;
1222
1266
 
1223
1267
    if(!s->dropable) {
1224
 
        ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
 
1268
        err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
1225
1269
        h->prev_poc_msb     = h->poc_msb;
1226
1270
        h->prev_poc_lsb     = h->poc_lsb;
1227
1271
    }
1229
1273
    h->prev_frame_num       = h->frame_num;
1230
1274
    h->outputed_poc         = h->next_outputed_poc;
1231
1275
 
1232
 
    return 0;
 
1276
    return err;
1233
1277
}
1234
1278
 
1235
1279
int ff_h264_frame_start(H264Context *h){
1236
1280
    MpegEncContext * const s = &h->s;
1237
1281
    int i;
1238
1282
    const int pixel_shift = h->pixel_shift;
1239
 
    int thread_count = (s->avctx->active_thread_type & FF_THREAD_SLICE) ? s->avctx->thread_count : 1;
1240
1283
 
1241
1284
    if(MPV_frame_start(s, s->avctx) < 0)
1242
1285
        return -1;
1247
1290
     * Zero here; IDR markings per slice in frame or fields are ORed in later.
1248
1291
     * See decode_nal_units().
1249
1292
     */
1250
 
    s->current_picture_ptr->key_frame= 0;
 
1293
    s->current_picture_ptr->f.key_frame = 0;
1251
1294
    s->current_picture_ptr->mmco_reset= 0;
1252
1295
 
1253
1296
    assert(s->linesize && s->uvlinesize);
1265
1308
 
1266
1309
    /* can't be in alloc_tables because linesize isn't known there.
1267
1310
     * FIXME: redo bipred weight to not require extra buffer? */
1268
 
    for(i = 0; i < thread_count; i++)
 
1311
    for(i = 0; i < s->slice_context_count; i++)
1269
1312
        if(h->thread_context[i] && !h->thread_context[i]->s.obmc_scratchpad)
1270
1313
            h->thread_context[i]->s.obmc_scratchpad = av_malloc(16*6*s->linesize);
1271
1314
 
1272
1315
    /* some macroblocks can be accessed before they're available in case of lost slices, mbaff or threading*/
1273
1316
    memset(h->slice_table, -1, (s->mb_height*s->mb_stride-1) * sizeof(*h->slice_table));
1274
1317
 
1275
 
//    s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
 
1318
//    s->decode = (s->flags & CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.f.reference /*|| h->contains_intra*/ || 1;
1276
1319
 
1277
1320
    // We mark the current picture as non-reference after allocating it, so
1278
1321
    // that if we break out due to an error it can be released automatically
1281
1324
    // get released even with set reference, besides SVQ3 and others do not
1282
1325
    // mark frames as reference later "naturally".
1283
1326
    if(s->codec_id != CODEC_ID_SVQ3)
1284
 
        s->current_picture_ptr->reference= 0;
 
1327
        s->current_picture_ptr->f.reference = 0;
1285
1328
 
1286
1329
    s->current_picture_ptr->field_poc[0]=
1287
1330
    s->current_picture_ptr->field_poc[1]= INT_MAX;
1306
1349
    Picture *out = s->current_picture_ptr;
1307
1350
    Picture *cur = s->current_picture_ptr;
1308
1351
    int i, pics, out_of_order, out_idx;
 
1352
    int invalid = 0, cnt = 0;
1309
1353
 
1310
 
    s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_H264;
1311
 
    s->current_picture_ptr->pict_type= s->pict_type;
 
1354
    s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_H264;
 
1355
    s->current_picture_ptr->f.pict_type   = s->pict_type;
1312
1356
 
1313
1357
    if (h->next_output_pic) return;
1314
1358
 
1321
1365
        return;
1322
1366
    }
1323
1367
 
1324
 
    cur->interlaced_frame = 0;
1325
 
    cur->repeat_pict = 0;
 
1368
    cur->f.interlaced_frame = 0;
 
1369
    cur->f.repeat_pict      = 0;
1326
1370
 
1327
1371
    /* Signal interlacing information externally. */
1328
1372
    /* Prioritize picture timing SEI information over used decoding process if it exists. */
1334
1378
            break;
1335
1379
        case SEI_PIC_STRUCT_TOP_FIELD:
1336
1380
        case SEI_PIC_STRUCT_BOTTOM_FIELD:
1337
 
            cur->interlaced_frame = 1;
 
1381
            cur->f.interlaced_frame = 1;
1338
1382
            break;
1339
1383
        case SEI_PIC_STRUCT_TOP_BOTTOM:
1340
1384
        case SEI_PIC_STRUCT_BOTTOM_TOP:
1341
1385
            if (FIELD_OR_MBAFF_PICTURE)
1342
 
                cur->interlaced_frame = 1;
 
1386
                cur->f.interlaced_frame = 1;
1343
1387
            else
1344
1388
                // try to flag soft telecine progressive
1345
 
                cur->interlaced_frame = h->prev_interlaced_frame;
 
1389
                cur->f.interlaced_frame = h->prev_interlaced_frame;
1346
1390
            break;
1347
1391
        case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
1348
1392
        case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
1349
1393
            // Signal the possibility of telecined film externally (pic_struct 5,6)
1350
1394
            // From these hints, let the applications decide if they apply deinterlacing.
1351
 
            cur->repeat_pict = 1;
 
1395
            cur->f.repeat_pict = 1;
1352
1396
            break;
1353
1397
        case SEI_PIC_STRUCT_FRAME_DOUBLING:
1354
1398
            // Force progressive here, as doubling interlaced frame is a bad idea.
1355
 
            cur->repeat_pict = 2;
 
1399
            cur->f.repeat_pict = 2;
1356
1400
            break;
1357
1401
        case SEI_PIC_STRUCT_FRAME_TRIPLING:
1358
 
            cur->repeat_pict = 4;
 
1402
            cur->f.repeat_pict = 4;
1359
1403
            break;
1360
1404
        }
1361
1405
 
1362
1406
        if ((h->sei_ct_type & 3) && h->sei_pic_struct <= SEI_PIC_STRUCT_BOTTOM_TOP)
1363
 
            cur->interlaced_frame = (h->sei_ct_type & (1<<1)) != 0;
 
1407
            cur->f.interlaced_frame = (h->sei_ct_type & (1 << 1)) != 0;
1364
1408
    }else{
1365
1409
        /* Derive interlacing flag from used decoding process. */
1366
 
        cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
 
1410
        cur->f.interlaced_frame = FIELD_OR_MBAFF_PICTURE;
1367
1411
    }
1368
 
    h->prev_interlaced_frame = cur->interlaced_frame;
 
1412
    h->prev_interlaced_frame = cur->f.interlaced_frame;
1369
1413
 
1370
1414
    if (cur->field_poc[0] != cur->field_poc[1]){
1371
1415
        /* Derive top_field_first from field pocs. */
1372
 
        cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
 
1416
        cur->f.top_field_first = cur->field_poc[0] < cur->field_poc[1];
1373
1417
    }else{
1374
 
        if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
 
1418
        if (cur->f.interlaced_frame || h->sps.pic_struct_present_flag) {
1375
1419
            /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
1376
1420
            if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
1377
1421
              || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
1378
 
                cur->top_field_first = 1;
 
1422
                cur->f.top_field_first = 1;
1379
1423
            else
1380
 
                cur->top_field_first = 0;
 
1424
                cur->f.top_field_first = 0;
1381
1425
        }else{
1382
1426
            /* Most likely progressive */
1383
 
            cur->top_field_first = 0;
 
1427
            cur->f.top_field_first = 0;
1384
1428
        }
1385
1429
    }
1386
1430
 
1396
1440
 
1397
1441
    if(   s->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT
1398
1442
       && !h->sps.bitstream_restriction_flag){
1399
 
        s->avctx->has_b_frames= MAX_DELAYED_PIC_COUNT;
 
1443
        s->avctx->has_b_frames = MAX_DELAYED_PIC_COUNT - 1;
1400
1444
        s->low_delay= 0;
1401
1445
    }
1402
1446
 
1406
1450
    assert(pics <= MAX_DELAYED_PIC_COUNT);
1407
1451
 
1408
1452
    h->delayed_pic[pics++] = cur;
1409
 
    if(cur->reference == 0)
1410
 
        cur->reference = DELAYED_PIC_REF;
 
1453
    if (cur->f.reference == 0)
 
1454
        cur->f.reference = DELAYED_PIC_REF;
1411
1455
 
 
1456
    /* Frame reordering. This code takes pictures from coding order and sorts
 
1457
     * them by their incremental POC value into display order. It supports POC
 
1458
     * gaps, MMCO reset codes and random resets.
 
1459
     * A "display group" can start either with a IDR frame (f.key_frame = 1),
 
1460
     * and/or can be closed down with a MMCO reset code. In sequences where
 
1461
     * there is no delay, we can't detect that (since the frame was already
 
1462
     * output to the user), so we also set h->mmco_reset to detect the MMCO
 
1463
     * reset code.
 
1464
     * FIXME: if we detect insufficient delays (as per s->avctx->has_b_frames),
 
1465
     * we increase the delay between input and output. All frames affected by
 
1466
     * the lag (e.g. those that should have been output before another frame
 
1467
     * that we already returned to the user) will be dropped. This is a bug
 
1468
     * that we will fix later. */
 
1469
    for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++) {
 
1470
        cnt     += out->poc < h->last_pocs[i];
 
1471
        invalid += out->poc == INT_MIN;
 
1472
    }
 
1473
    if (!h->mmco_reset && !cur->f.key_frame && cnt + invalid == MAX_DELAYED_PIC_COUNT && cnt > 0) {
 
1474
        h->mmco_reset = 2;
 
1475
        if (pics > 1)
 
1476
            h->delayed_pic[pics - 2]->mmco_reset = 2;
 
1477
    }
 
1478
    if (h->mmco_reset || cur->f.key_frame) {
 
1479
        for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
 
1480
            h->last_pocs[i] = INT_MIN;
 
1481
        cnt     = 0;
 
1482
        invalid = MAX_DELAYED_PIC_COUNT;
 
1483
    }
1412
1484
    out = h->delayed_pic[0];
1413
1485
    out_idx = 0;
1414
 
    for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
 
1486
    for (i = 1; i < MAX_DELAYED_PIC_COUNT && h->delayed_pic[i] &&
 
1487
         !h->delayed_pic[i-1]->mmco_reset && !h->delayed_pic[i]->f.key_frame; i++)
 
1488
    {
1415
1489
        if(h->delayed_pic[i]->poc < out->poc){
1416
1490
            out = h->delayed_pic[i];
1417
1491
            out_idx = i;
1418
1492
        }
1419
 
    if(s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset))
1420
 
        h->next_outputed_poc= INT_MIN;
1421
 
    out_of_order = out->poc < h->next_outputed_poc;
 
1493
    }
 
1494
    if (s->avctx->has_b_frames == 0 && (h->delayed_pic[0]->f.key_frame || h->mmco_reset))
 
1495
        h->next_outputed_poc = INT_MIN;
 
1496
    out_of_order = !out->f.key_frame && !h->mmco_reset && (out->poc < h->next_outputed_poc);
1422
1497
 
1423
1498
    if(h->sps.bitstream_restriction_flag && s->avctx->has_b_frames >= h->sps.num_reorder_frames)
1424
1499
        { }
1425
 
    else if((out_of_order && pics-1 == s->avctx->has_b_frames && s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT)
1426
 
       || (s->low_delay &&
1427
 
        ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2)
1428
 
         || cur->pict_type == AV_PICTURE_TYPE_B)))
1429
 
    {
 
1500
    else if (out_of_order && pics-1 == s->avctx->has_b_frames &&
 
1501
             s->avctx->has_b_frames < MAX_DELAYED_PIC_COUNT) {
 
1502
        if (invalid + cnt < MAX_DELAYED_PIC_COUNT) {
 
1503
            s->avctx->has_b_frames = FFMAX(s->avctx->has_b_frames, cnt);
 
1504
        }
 
1505
        s->low_delay = 0;
 
1506
    } else if (s->low_delay &&
 
1507
               ((h->next_outputed_poc != INT_MIN && out->poc > h->next_outputed_poc + 2) ||
 
1508
                cur->f.pict_type == AV_PICTURE_TYPE_B)) {
1430
1509
        s->low_delay = 0;
1431
1510
        s->avctx->has_b_frames++;
1432
1511
    }
1433
1512
 
1434
 
    if(out_of_order || pics > s->avctx->has_b_frames){
1435
 
        out->reference &= ~DELAYED_PIC_REF;
 
1513
    if(pics > s->avctx->has_b_frames){
 
1514
        out->f.reference &= ~DELAYED_PIC_REF;
1436
1515
        out->owner2 = s; // for frame threading, the owner must be the second field's thread
1437
1516
                         // or else the first thread can release the picture and reuse it unsafely
1438
1517
        for(i=out_idx; h->delayed_pic[i]; i++)
1439
1518
            h->delayed_pic[i] = h->delayed_pic[i+1];
1440
1519
    }
 
1520
    memmove(h->last_pocs, &h->last_pocs[1], sizeof(*h->last_pocs) * (MAX_DELAYED_PIC_COUNT - 1));
 
1521
    h->last_pocs[MAX_DELAYED_PIC_COUNT - 1] = cur->poc;
1441
1522
    if(!out_of_order && pics > s->avctx->has_b_frames){
1442
1523
        h->next_output_pic = out;
1443
 
        if(out_idx==0 && h->delayed_pic[0] && (h->delayed_pic[0]->key_frame || h->delayed_pic[0]->mmco_reset)) {
1444
 
            h->next_outputed_poc = INT_MIN;
1445
 
        } else
1446
 
            h->next_outputed_poc = out->poc;
 
1524
        if (out->mmco_reset) {
 
1525
            if (out_idx > 0) {
 
1526
                h->next_outputed_poc = out->poc;
 
1527
                h->delayed_pic[out_idx - 1]->mmco_reset = out->mmco_reset;
 
1528
            } else {
 
1529
                h->next_outputed_poc = INT_MIN;
 
1530
            }
 
1531
        } else {
 
1532
            if (out_idx == 0 && pics > 1 && h->delayed_pic[0]->f.key_frame) {
 
1533
                h->next_outputed_poc = INT_MIN;
 
1534
            } else {
 
1535
                h->next_outputed_poc = out->poc;
 
1536
            }
 
1537
        }
 
1538
        h->mmco_reset = 0;
1447
1539
    }else{
1448
1540
        av_log(s->avctx, AV_LOG_DEBUG, "no picture\n");
1449
1541
    }
1452
1544
        ff_thread_finish_setup(s->avctx);
1453
1545
}
1454
1546
 
1455
 
static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int chroma444, int simple){
 
1547
static av_always_inline void backup_mb_border(H264Context *h, uint8_t *src_y,
 
1548
                                              uint8_t *src_cb, uint8_t *src_cr,
 
1549
                                              int linesize, int uvlinesize, int simple)
 
1550
{
1456
1551
    MpegEncContext * const s = &h->s;
1457
1552
    uint8_t *top_border;
1458
1553
    int top_idx = 1;
1459
1554
    const int pixel_shift = h->pixel_shift;
 
1555
    int chroma444 = CHROMA444;
 
1556
    int chroma422 = CHROMA422;
1460
1557
 
1461
1558
    src_y  -=   linesize;
1462
1559
    src_cb -= uvlinesize;
1480
1577
                            AV_COPY128(top_border+16, src_cb + 15*uvlinesize);
1481
1578
                            AV_COPY128(top_border+32, src_cr + 15*uvlinesize);
1482
1579
                        }
 
1580
                    } else if(chroma422) {
 
1581
                        if (pixel_shift) {
 
1582
                            AV_COPY128(top_border+32, src_cb + 15*uvlinesize);
 
1583
                            AV_COPY128(top_border+48, src_cr + 15*uvlinesize);
 
1584
                        } else {
 
1585
                            AV_COPY64(top_border+16, src_cb +  15*uvlinesize);
 
1586
                            AV_COPY64(top_border+24, src_cr +  15*uvlinesize);
 
1587
                        }
1483
1588
                    } else {
1484
1589
                        if (pixel_shift) {
1485
1590
                            AV_COPY128(top_border+32, src_cb+7*uvlinesize);
1515
1620
                AV_COPY128(top_border+16, src_cb + 16*linesize);
1516
1621
                AV_COPY128(top_border+32, src_cr + 16*linesize);
1517
1622
            }
 
1623
        } else if(chroma422) {
 
1624
            if (pixel_shift) {
 
1625
                AV_COPY128(top_border+32, src_cb+16*uvlinesize);
 
1626
                AV_COPY128(top_border+48, src_cr+16*uvlinesize);
 
1627
            } else {
 
1628
                AV_COPY64(top_border+16, src_cb+16*uvlinesize);
 
1629
                AV_COPY64(top_border+24, src_cr+16*uvlinesize);
 
1630
            }
1518
1631
        } else {
1519
1632
            if (pixel_shift) {
1520
1633
                AV_COPY128(top_border+32, src_cb+8*uvlinesize);
1527
1640
    }
1528
1641
}
1529
1642
 
1530
 
static inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
 
1643
static av_always_inline void xchg_mb_border(H264Context *h, uint8_t *src_y,
1531
1644
                                  uint8_t *src_cb, uint8_t *src_cr,
1532
1645
                                  int linesize, int uvlinesize,
1533
1646
                                  int xchg, int chroma444,
1688
1801
                                    tr_high= ((uint16_t*)ptr)[3 - linesize/2]*0x0001000100010001ULL;
1689
1802
                                    topright= (uint8_t*) &tr_high;
1690
1803
                                } else {
1691
 
                                    tr= ptr[3 - linesize]*0x01010101;
 
1804
                                    tr= ptr[3 - linesize]*0x01010101u;
1692
1805
                                    topright= (uint8_t*) &tr;
1693
1806
                                }
1694
1807
                            }else
1704
1817
                                    idct_dc_add(ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
1705
1818
                                else
1706
1819
                                    idct_add   (ptr, h->mb + (i*16+p*256 << pixel_shift), linesize);
1707
 
                            }else
 
1820
                            } else if (CONFIG_SVQ3_DECODER)
1708
1821
                                ff_svq3_add_idct_c(ptr, h->mb + i*16+p*256, linesize, qscale, 0);
1709
1822
                        }
1710
1823
                    }
1721
1834
                    static const uint8_t dc_mapping[16] = { 0*16, 1*16, 4*16, 5*16, 2*16, 3*16, 6*16, 7*16,
1722
1835
                                                            8*16, 9*16,12*16,13*16,10*16,11*16,14*16,15*16};
1723
1836
                    for(i = 0; i < 16; i++)
1724
 
                        dctcoef_set(h->mb+p*256, pixel_shift, dc_mapping[i], dctcoef_get(h->mb_luma_dc[p], pixel_shift, i));
 
1837
                        dctcoef_set(h->mb+(p*256 << pixel_shift), pixel_shift, dc_mapping[i], dctcoef_get(h->mb_luma_dc[p], pixel_shift, i));
1725
1838
                }
1726
1839
            }
1727
 
        }else
 
1840
        } else if (CONFIG_SVQ3_DECODER)
1728
1841
            ff_svq3_luma_dc_dequant_idct_c(h->mb+p*256, h->mb_luma_dc[p], qscale);
1729
1842
    }
1730
1843
}
1768
1881
                    }
1769
1882
                }
1770
1883
            }
1771
 
        }else{
 
1884
        } else if (CONFIG_SVQ3_DECODER) {
1772
1885
            for(i=0; i<16; i++){
1773
1886
                if(h->non_zero_count_cache[ scan8[i+p*16] ] || h->mb[i*16+p*256]){ //FIXME benchmark weird rule, & below
1774
1887
                    uint8_t * const ptr= dest_y + block_offset[i];
1779
1892
    }
1780
1893
}
1781
1894
 
1782
 
static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, int pixel_shift){
 
1895
static av_always_inline void hl_decode_mb_internal(H264Context *h, int simple, int pixel_shift)
 
1896
{
1783
1897
    MpegEncContext * const s = &h->s;
1784
1898
    const int mb_x= s->mb_x;
1785
1899
    const int mb_y= s->mb_y;
1786
1900
    const int mb_xy= h->mb_xy;
1787
 
    const int mb_type= s->current_picture.mb_type[mb_xy];
 
1901
    const int mb_type = s->current_picture.f.mb_type[mb_xy];
1788
1902
    uint8_t  *dest_y, *dest_cb, *dest_cr;
1789
1903
    int linesize, uvlinesize /*dct_offset*/;
1790
1904
    int i, j;
1793
1907
    /* is_h264 should always be true if SVQ3 is disabled. */
1794
1908
    const int is_h264 = !CONFIG_SVQ3_DECODER || simple || s->codec_id == CODEC_ID_H264;
1795
1909
    void (*idct_add)(uint8_t *dst, DCTELEM *block, int stride);
 
1910
    const int block_h = 16 >> s->chroma_y_shift;
 
1911
    const int chroma422 = CHROMA422;
1796
1912
 
1797
 
    dest_y  = s->current_picture.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize  ) * 16;
1798
 
    dest_cb = s->current_picture.data[1] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
1799
 
    dest_cr = s->current_picture.data[2] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * 8;
 
1913
    dest_y  = s->current_picture.f.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize  ) * 16;
 
1914
    dest_cb = s->current_picture.f.data[1] + (mb_x << pixel_shift)*8 + mb_y * s->uvlinesize * block_h;
 
1915
    dest_cr = s->current_picture.f.data[2] + (mb_x << pixel_shift)*8 + mb_y * s->uvlinesize * block_h;
1800
1916
 
1801
1917
    s->dsp.prefetch(dest_y + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
1802
1918
    s->dsp.prefetch(dest_cb + (s->mb_x&7)*s->uvlinesize + (64 << pixel_shift), dest_cr - dest_cb, 2);
1809
1925
        block_offset = &h->block_offset[48];
1810
1926
        if(mb_y&1){ //FIXME move out of this function?
1811
1927
            dest_y -= s->linesize*15;
1812
 
            dest_cb-= s->uvlinesize*7;
1813
 
            dest_cr-= s->uvlinesize*7;
 
1928
            dest_cb-= s->uvlinesize * (block_h - 1);
 
1929
            dest_cr-= s->uvlinesize * (block_h - 1);
1814
1930
        }
1815
1931
        if(FRAME_MBAFF) {
1816
1932
            int list;
1848
1964
                    tmp_y[j] = get_bits(&gb, bit_depth);
1849
1965
            }
1850
1966
            if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1851
 
                for (i = 0; i < 8; i++) {
1852
 
                    uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
1853
 
                    for (j = 0; j < 8; j++)
1854
 
                        tmp_cb[j] = get_bits(&gb, bit_depth);
1855
 
                }
1856
 
                for (i = 0; i < 8; i++) {
1857
 
                    uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
1858
 
                    for (j = 0; j < 8; j++)
1859
 
                        tmp_cr[j] = get_bits(&gb, bit_depth);
 
1967
                if (!h->sps.chroma_format_idc) {
 
1968
                    for (i = 0; i < block_h; i++) {
 
1969
                        uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
 
1970
                        for (j = 0; j < 8; j++) {
 
1971
                            tmp_cb[j] = 1 << (bit_depth - 1);
 
1972
                        }
 
1973
                    }
 
1974
                    for (i = 0; i < block_h; i++) {
 
1975
                        uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
 
1976
                        for (j = 0; j < 8; j++) {
 
1977
                            tmp_cr[j] = 1 << (bit_depth - 1);
 
1978
                        }
 
1979
                    }
 
1980
                } else {
 
1981
                    for (i = 0; i < block_h; i++) {
 
1982
                        uint16_t *tmp_cb = (uint16_t*)(dest_cb + i*uvlinesize);
 
1983
                        for (j = 0; j < 8; j++)
 
1984
                            tmp_cb[j] = get_bits(&gb, bit_depth);
 
1985
                    }
 
1986
                    for (i = 0; i < block_h; i++) {
 
1987
                        uint16_t *tmp_cr = (uint16_t*)(dest_cr + i*uvlinesize);
 
1988
                        for (j = 0; j < 8; j++)
 
1989
                            tmp_cr[j] = get_bits(&gb, bit_depth);
 
1990
                    }
1860
1991
                }
1861
1992
            }
1862
1993
        } else {
1864
1995
                memcpy(dest_y + i*  linesize, h->mb       + i*8, 16);
1865
1996
            }
1866
1997
            if(simple || !CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
1867
 
                for (i=0; i<8; i++) {
1868
 
                    memcpy(dest_cb+ i*uvlinesize, h->mb + 128 + i*4,  8);
1869
 
                    memcpy(dest_cr+ i*uvlinesize, h->mb + 160 + i*4,  8);
 
1998
                if (!h->sps.chroma_format_idc) {
 
1999
                    for (i = 0; i < block_h; i++) {
 
2000
                        memset(dest_cb + i*uvlinesize, 128, 8);
 
2001
                        memset(dest_cr + i*uvlinesize, 128, 8);
 
2002
                    }
 
2003
                } else {
 
2004
                    for (i = 0; i < block_h; i++) {
 
2005
                        memcpy(dest_cb + i*uvlinesize, h->mb + 128 + i*4,  8);
 
2006
                        memcpy(dest_cr + i*uvlinesize, h->mb + 160 + i*4,  8);
 
2007
                    }
1870
2008
                }
1871
2009
            }
1872
2010
        }
1885
2023
            if(h->deblocking_filter)
1886
2024
                xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0, 0, simple, pixel_shift);
1887
2025
        }else if(is_h264){
1888
 
            if (pixel_shift) {
1889
 
                hl_motion_16(h, dest_y, dest_cb, dest_cr,
1890
 
                             s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
1891
 
                             s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
1892
 
                             h->h264dsp.weight_h264_pixels_tab,
1893
 
                             h->h264dsp.biweight_h264_pixels_tab, 0);
1894
 
            } else
1895
 
                hl_motion_8(h, dest_y, dest_cb, dest_cr,
1896
 
                            s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
1897
 
                            s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
1898
 
                            h->h264dsp.weight_h264_pixels_tab,
1899
 
                            h->h264dsp.biweight_h264_pixels_tab, 0);
 
2026
            if (chroma422) {
 
2027
                hl_motion_422(h, dest_y, dest_cb, dest_cr,
 
2028
                              s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
 
2029
                              s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
 
2030
                              h->h264dsp.weight_h264_pixels_tab,
 
2031
                              h->h264dsp.biweight_h264_pixels_tab,
 
2032
                              pixel_shift);
 
2033
            } else {
 
2034
                hl_motion_420(h, dest_y, dest_cb, dest_cr,
 
2035
                              s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
 
2036
                              s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
 
2037
                              h->h264dsp.weight_h264_pixels_tab,
 
2038
                              h->h264dsp.biweight_h264_pixels_tab,
 
2039
                              pixel_shift);
 
2040
            }
1900
2041
        }
1901
2042
 
1902
2043
        hl_decode_mb_idct_luma(h, mb_type, is_h264, simple, transform_bypass, pixel_shift, block_offset, linesize, dest_y, 0);
1914
2055
                            if(h->non_zero_count_cache[ scan8[i] ] || dctcoef_get(h->mb, pixel_shift, i*16))
1915
2056
                                idct_add   (dest[j-1] + block_offset[i], h->mb + (i*16 << pixel_shift), uvlinesize);
1916
2057
                        }
 
2058
                        if (chroma422) {
 
2059
                            for(i=j*16+4; i<j*16+8; i++){
 
2060
                                if(h->non_zero_count_cache[ scan8[i+4] ] || dctcoef_get(h->mb, pixel_shift, i*16))
 
2061
                                    idct_add   (dest[j-1] + block_offset[i+4], h->mb + (i*16 << pixel_shift), uvlinesize);
 
2062
                            }
 
2063
                        }
1917
2064
                    }
1918
2065
                }
1919
2066
            }else{
1920
2067
                if(is_h264){
 
2068
                    int qp[2];
 
2069
                    if (chroma422) {
 
2070
                        qp[0] = h->chroma_qp[0] + 3;
 
2071
                        qp[1] = h->chroma_qp[1] + 3;
 
2072
                    } else {
 
2073
                        qp[0] = h->chroma_qp[0];
 
2074
                        qp[1] = h->chroma_qp[1];
 
2075
                    }
1921
2076
                    if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+0] ])
1922
 
                        h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16*1 << pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
 
2077
                        h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16*1 << pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][qp[0]][0]);
1923
2078
                    if(h->non_zero_count_cache[ scan8[CHROMA_DC_BLOCK_INDEX+1] ])
1924
 
                        h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16*2 << pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
 
2079
                        h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + (16*16*2 << pixel_shift), h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][qp[1]][0]);
1925
2080
                    h->h264dsp.h264_idct_add8(dest, block_offset,
1926
2081
                                              h->mb, uvlinesize,
1927
2082
                                              h->non_zero_count_cache);
1928
 
                }else{
 
2083
                } else if (CONFIG_SVQ3_DECODER) {
1929
2084
                    h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16*1, h->dequant4_coeff[IS_INTRA(mb_type) ? 1:4][h->chroma_qp[0]][0]);
1930
2085
                    h->h264dsp.h264_chroma_dc_dequant_idct(h->mb + 16*16*2, h->dequant4_coeff[IS_INTRA(mb_type) ? 2:5][h->chroma_qp[1]][0]);
1931
2086
                    for(j=1; j<3; j++){
1952
2107
    const int mb_x= s->mb_x;
1953
2108
    const int mb_y= s->mb_y;
1954
2109
    const int mb_xy= h->mb_xy;
1955
 
    const int mb_type= s->current_picture.mb_type[mb_xy];
 
2110
    const int mb_type = s->current_picture.f.mb_type[mb_xy];
1956
2111
    uint8_t  *dest[3];
1957
2112
    int linesize;
1958
2113
    int i, j, p;
1962
2117
 
1963
2118
    for (p = 0; p < plane_count; p++)
1964
2119
    {
1965
 
        dest[p] = s->current_picture.data[p] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
 
2120
        dest[p] = s->current_picture.f.data[p] + ((mb_x << pixel_shift) + mb_y * s->linesize) * 16;
1966
2121
        s->dsp.prefetch(dest[p] + (s->mb_x&3)*4*s->linesize + (64 << pixel_shift), s->linesize, 4);
1967
2122
    }
1968
2123
 
2026
2181
            if(h->deblocking_filter)
2027
2182
                xchg_mb_border(h, dest[0], dest[1], dest[2], linesize, linesize, 0, 1, simple, pixel_shift);
2028
2183
        }else{
2029
 
            if (pixel_shift) {
2030
 
                hl_motion_16(h, dest[0], dest[1], dest[2],
2031
 
                             s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
2032
 
                             s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
2033
 
                             h->h264dsp.weight_h264_pixels_tab,
2034
 
                             h->h264dsp.biweight_h264_pixels_tab, 1);
2035
 
            } else
2036
 
                hl_motion_8(h, dest[0], dest[1], dest[2],
2037
 
                            s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
2038
 
                            s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
2039
 
                            h->h264dsp.weight_h264_pixels_tab,
2040
 
                            h->h264dsp.biweight_h264_pixels_tab, 1);
 
2184
            hl_motion(h, dest[0], dest[1], dest[2],
 
2185
                      s->me.qpel_put, s->dsp.put_h264_chroma_pixels_tab,
 
2186
                      s->me.qpel_avg, s->dsp.avg_h264_chroma_pixels_tab,
 
2187
                      h->h264dsp.weight_h264_pixels_tab,
 
2188
                      h->h264dsp.biweight_h264_pixels_tab, pixel_shift, 3);
2041
2189
        }
2042
2190
 
2043
2191
        for (p = 0; p < plane_count; p++)
2057
2205
static void hl_decode_mb_simple_ ## bits(H264Context *h){ \
2058
2206
    hl_decode_mb_internal(h, 1, sh); \
2059
2207
}
2060
 
hl_decode_mb_simple(0, 8);
2061
 
hl_decode_mb_simple(1, 16);
 
2208
hl_decode_mb_simple(0, 8)
 
2209
hl_decode_mb_simple(1, 16)
2062
2210
 
2063
2211
/**
2064
2212
 * Process a macroblock; this handles edge cases, such as interlacing.
2078
2226
void ff_h264_hl_decode_mb(H264Context *h){
2079
2227
    MpegEncContext * const s = &h->s;
2080
2228
    const int mb_xy= h->mb_xy;
2081
 
    const int mb_type= s->current_picture.mb_type[mb_xy];
 
2229
    const int mb_type = s->current_picture.f.mb_type[mb_xy];
2082
2230
    int is_complex = CONFIG_SMALL || h->is_complex || IS_INTRA_PCM(mb_type) || s->qscale == 0;
2083
2231
 
2084
2232
    if (CHROMA444) {
2170
2318
    }
2171
2319
 
2172
2320
    if(field < 0){
2173
 
        cur_poc = s->current_picture_ptr->poc;
 
2321
        if (s->picture_structure == PICT_FRAME) {
 
2322
            cur_poc = s->current_picture_ptr->poc;
 
2323
        } else {
 
2324
            cur_poc = s->current_picture_ptr->field_poc[s->picture_structure - 1];
 
2325
        }
2174
2326
    if(   h->ref_count[0] == 1 && h->ref_count[1] == 1 && !FRAME_MBAFF
2175
2327
       && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
2176
2328
        h->use_weight= 0;
2195
2347
    for(ref0=ref_start; ref0 < ref_count0; ref0++){
2196
2348
        int poc0 = h->ref_list[0][ref0].poc;
2197
2349
        for(ref1=ref_start; ref1 < ref_count1; ref1++){
2198
 
            int poc1 = h->ref_list[1][ref1].poc;
2199
 
            int td = av_clip(poc1 - poc0, -128, 127);
2200
 
            int w= 32;
2201
 
            if(td){
2202
 
                int tb = av_clip(cur_poc - poc0, -128, 127);
2203
 
                int tx = (16384 + (FFABS(td) >> 1)) / td;
2204
 
                int dist_scale_factor = (tb*tx + 32) >> 8;
2205
 
                if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
2206
 
                    w = 64 - dist_scale_factor;
 
2350
            int w = 32;
 
2351
            if (!h->ref_list[0][ref0].long_ref && !h->ref_list[1][ref1].long_ref) {
 
2352
                int poc1 = h->ref_list[1][ref1].poc;
 
2353
                int td = av_clip(poc1 - poc0, -128, 127);
 
2354
                if(td){
 
2355
                    int tb = av_clip(cur_poc - poc0, -128, 127);
 
2356
                    int tx = (16384 + (FFABS(td) >> 1)) / td;
 
2357
                    int dist_scale_factor = (tb*tx + 32) >> 8;
 
2358
                    if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
 
2359
                        w = 64 - dist_scale_factor;
 
2360
                }
2207
2361
            }
2208
2362
            if(field<0){
2209
2363
                h->implicit_weight[ref0][ref1][0]=
2232
2386
    int i;
2233
2387
    for(i=0; i<MAX_DELAYED_PIC_COUNT; i++) {
2234
2388
        if(h->delayed_pic[i])
2235
 
            h->delayed_pic[i]->reference= 0;
 
2389
            h->delayed_pic[i]->f.reference = 0;
2236
2390
        h->delayed_pic[i]= NULL;
2237
2391
    }
 
2392
    for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
 
2393
        h->last_pocs[i] = INT_MIN;
2238
2394
    h->outputed_poc=h->next_outputed_poc= INT_MIN;
2239
2395
    h->prev_interlaced_frame = 1;
2240
2396
    idr(h);
2241
2397
    if(h->s.current_picture_ptr)
2242
 
        h->s.current_picture_ptr->reference= 0;
 
2398
        h->s.current_picture_ptr->f.reference = 0;
2243
2399
    h->s.first_field= 0;
2244
2400
    ff_h264_reset_sei(h);
2245
2401
    ff_mpeg_flush(avctx);
2359
2515
    }
2360
2516
}
2361
2517
 
2362
 
static void field_end(H264Context *h, int in_setup){
 
2518
static int field_end(H264Context *h, int in_setup){
2363
2519
    MpegEncContext * const s = &h->s;
2364
2520
    AVCodecContext * const avctx= s->avctx;
 
2521
    int err = 0;
2365
2522
    s->mb_y= 0;
2366
2523
 
2367
2524
    if (!in_setup && !s->dropable)
2373
2530
 
2374
2531
    if(in_setup || !(avctx->active_thread_type&FF_THREAD_FRAME)){
2375
2532
        if(!s->dropable) {
2376
 
            ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
 
2533
            err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
2377
2534
            h->prev_poc_msb= h->poc_msb;
2378
2535
            h->prev_poc_lsb= h->poc_lsb;
2379
2536
        }
2408
2565
    MPV_frame_end(s);
2409
2566
 
2410
2567
    h->current_slice=0;
 
2568
 
 
2569
    return err;
2411
2570
}
2412
2571
 
2413
2572
/**
2438
2597
}
2439
2598
 
2440
2599
/**
2441
 
 * computes profile from profile_idc and constraint_set?_flags
 
2600
 * Compute profile from profile_idc and constraint_set?_flags.
2442
2601
 *
2443
2602
 * @param sps SPS
2444
2603
 *
2465
2624
}
2466
2625
 
2467
2626
/**
2468
 
 * decodes a slice header.
 
2627
 * Decode a slice header.
2469
2628
 * This will also call MPV_common_init() and frame_start() as needed.
2470
2629
 *
2471
2630
 * @param h h264context
2485
2644
 
2486
2645
    s->dropable= h->nal_ref_idc == 0;
2487
2646
 
2488
 
    if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc){
 
2647
    /* FIXME: 2tap qpel isn't implemented for high bit depth. */
 
2648
    if((s->avctx->flags2 & CODEC_FLAG2_FAST) && !h->nal_ref_idc && !h->pixel_shift){
2489
2649
        s->me.qpel_put= s->dsp.put_2tap_qpel_pixels_tab;
2490
2650
        s->me.qpel_avg= s->dsp.avg_2tap_qpel_pixels_tab;
2491
2651
    }else{
2557
2717
 
2558
2718
    h->b_stride=  s->mb_width*4;
2559
2719
 
 
2720
    s->chroma_y_shift = h->sps.chroma_format_idc <= 1; // 400 uses yuv420p
 
2721
 
2560
2722
    s->width = 16*s->mb_width - (2>>CHROMA444)*FFMIN(h->sps.crop_right, (8<<CHROMA444)-1);
2561
2723
    if(h->sps.frame_mbs_only_flag)
2562
 
        s->height= 16*s->mb_height - (2>>CHROMA444)*FFMIN(h->sps.crop_bottom, (8<<CHROMA444)-1);
 
2724
        s->height= 16*s->mb_height - (1<<s->chroma_y_shift)*FFMIN(h->sps.crop_bottom, (16>>s->chroma_y_shift)-1);
2563
2725
    else
2564
 
        s->height= 16*s->mb_height - (4>>CHROMA444)*FFMIN(h->sps.crop_bottom, (8<<CHROMA444)-1);
 
2726
        s->height= 16*s->mb_height - (2<<s->chroma_y_shift)*FFMIN(h->sps.crop_bottom, (16>>s->chroma_y_shift)-1);
2565
2727
 
2566
2728
    if (s->context_initialized
2567
2729
        && (   s->width != s->avctx->width || s->height != s->avctx->height
2584
2746
        s->avctx->sample_aspect_ratio= h->sps.sar;
2585
2747
        av_assert0(s->avctx->sample_aspect_ratio.den);
2586
2748
 
2587
 
        h->s.avctx->coded_width = 16*s->mb_width;
2588
 
        h->s.avctx->coded_height = 16*s->mb_height;
2589
 
 
2590
2749
        if(h->sps.video_signal_type_present_flag){
2591
2750
            s->avctx->color_range = h->sps.full_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
2592
2751
            if(h->sps.colour_description_present_flag){
2606
2765
 
2607
2766
        switch (h->sps.bit_depth_luma) {
2608
2767
            case 9 :
2609
 
                s->avctx->pix_fmt = CHROMA444 ? PIX_FMT_YUV444P9 : PIX_FMT_YUV420P9;
 
2768
                if (CHROMA444) {
 
2769
                    if (s->avctx->colorspace == AVCOL_SPC_RGB) {
 
2770
                        s->avctx->pix_fmt = PIX_FMT_GBRP9;
 
2771
                    } else
 
2772
                        s->avctx->pix_fmt = PIX_FMT_YUV444P9;
 
2773
                } else if (CHROMA422)
 
2774
                    s->avctx->pix_fmt = PIX_FMT_YUV422P9;
 
2775
                else
 
2776
                    s->avctx->pix_fmt = PIX_FMT_YUV420P9;
2610
2777
                break;
2611
2778
            case 10 :
2612
 
                s->avctx->pix_fmt = CHROMA444 ? PIX_FMT_YUV444P10 : PIX_FMT_YUV420P10;
 
2779
                if (CHROMA444) {
 
2780
                    if (s->avctx->colorspace == AVCOL_SPC_RGB) {
 
2781
                        s->avctx->pix_fmt = PIX_FMT_GBRP10;
 
2782
                    } else
 
2783
                        s->avctx->pix_fmt = PIX_FMT_YUV444P10;
 
2784
                } else if (CHROMA422)
 
2785
                    s->avctx->pix_fmt = PIX_FMT_YUV422P10;
 
2786
                else
 
2787
                    s->avctx->pix_fmt = PIX_FMT_YUV420P10;
2613
2788
                break;
2614
2789
            default:
2615
2790
                if (CHROMA444){
2616
 
                    s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ444P : PIX_FMT_YUV444P;
 
2791
                    if (s->avctx->colorspace == AVCOL_SPC_RGB) {
 
2792
                        s->avctx->pix_fmt = PIX_FMT_GBRP;
 
2793
                    } else
 
2794
                        s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ444P : PIX_FMT_YUV444P;
 
2795
                } else if (CHROMA422) {
 
2796
                    s->avctx->pix_fmt = s->avctx->color_range == AVCOL_RANGE_JPEG ? PIX_FMT_YUVJ422P : PIX_FMT_YUV422P;
2617
2797
                }else{
2618
2798
                    s->avctx->pix_fmt = s->avctx->get_format(s->avctx,
2619
2799
                                                             s->avctx->codec->pix_fmts ?
2634
2814
        h->prev_interlaced_frame = 1;
2635
2815
 
2636
2816
        init_scan_tables(h);
2637
 
        ff_h264_alloc_tables(h);
 
2817
        if (ff_h264_alloc_tables(h) < 0) {
 
2818
            av_log(h->s.avctx, AV_LOG_ERROR, "Could not allocate memory for h264\n");
 
2819
            return AVERROR(ENOMEM);
 
2820
        }
2638
2821
 
2639
2822
        if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_SLICE)) {
2640
2823
            if (context_init(h) < 0) {
2642
2825
                return -1;
2643
2826
            }
2644
2827
        } else {
2645
 
            for(i = 1; i < s->avctx->thread_count; i++) {
 
2828
            for(i = 1; i < s->slice_context_count; i++) {
2646
2829
                H264Context *c;
2647
2830
                c = h->thread_context[i] = av_malloc(sizeof(H264Context));
2648
2831
                memcpy(c, h->s.thread_context[i], sizeof(MpegEncContext));
2655
2838
                clone_tables(c, h, i);
2656
2839
            }
2657
2840
 
2658
 
            for(i = 0; i < s->avctx->thread_count; i++)
 
2841
            for(i = 0; i < s->slice_context_count; i++)
2659
2842
                if (context_init(h->thread_context[i]) < 0) {
2660
2843
                    av_log(h->s.avctx, AV_LOG_ERROR, "context_init() failed.\n");
2661
2844
                    return -1;
2708
2891
            ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 0);
2709
2892
            ff_thread_report_progress((AVFrame*)s->current_picture_ptr, INT_MAX, 1);
2710
2893
            ff_generate_sliding_window_mmcos(h);
2711
 
            ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
 
2894
            if (ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index) < 0 &&
 
2895
                (s->avctx->err_recognition & AV_EF_EXPLODE))
 
2896
                return AVERROR_INVALIDDATA;
2712
2897
            /* Error concealment: if a ref is missing, copy the previous ref in its place.
2713
2898
             * FIXME: avoiding a memcpy would be nice, but ref handling makes many assumptions
2714
2899
             * about there being no actual duplicates.
2715
2900
             * FIXME: this doesn't copy padding for out-of-frame motion vectors.  Given we're
2716
 
             * concealing a lost frame, this probably isn't noticable by comparison, but it should
 
2901
             * concealing a lost frame, this probably isn't noticeable by comparison, but it should
2717
2902
             * be fixed. */
2718
2903
            if (h->short_ref_count) {
2719
2904
                if (prev) {
2720
 
                    av_image_copy(h->short_ref[0]->data, h->short_ref[0]->linesize,
2721
 
                                  (const uint8_t**)prev->data, prev->linesize,
 
2905
                    av_image_copy(h->short_ref[0]->f.data, h->short_ref[0]->f.linesize,
 
2906
                                  (const uint8_t**)prev->f.data, prev->f.linesize,
2722
2907
                                  s->avctx->pix_fmt, s->mb_width*16, s->mb_height*16);
2723
2908
                    h->short_ref[0]->poc = prev->poc+2;
2724
2909
                }
2729
2914
        /* See if we have a decoded first field looking for a pair... */
2730
2915
        if (s0->first_field) {
2731
2916
            assert(s0->current_picture_ptr);
2732
 
            assert(s0->current_picture_ptr->data[0]);
2733
 
            assert(s0->current_picture_ptr->reference != DELAYED_PIC_REF);
 
2917
            assert(s0->current_picture_ptr->f.data[0]);
 
2918
            assert(s0->current_picture_ptr->f.reference != DELAYED_PIC_REF);
2734
2919
 
2735
2920
            /* figure out if we have a complementary field pair */
2736
2921
            if (!FIELD_PICTURE || s->picture_structure == last_pic_structure) {
2743
2928
 
2744
2929
            } else {
2745
2930
                if (h->nal_ref_idc &&
2746
 
                        s0->current_picture_ptr->reference &&
 
2931
                        s0->current_picture_ptr->f.reference &&
2747
2932
                        s0->current_picture_ptr->frame_num != h->frame_num) {
2748
2933
                    /*
2749
2934
                     * This and previous field were reference, but had
2857
3042
        ff_h264_fill_default_ref_list(h);
2858
3043
    }
2859
3044
 
2860
 
    if(h->slice_type_nos!=AV_PICTURE_TYPE_I && ff_h264_decode_ref_pic_list_reordering(h) < 0)
 
3045
    if(h->slice_type_nos!=AV_PICTURE_TYPE_I && ff_h264_decode_ref_pic_list_reordering(h) < 0) {
 
3046
        h->ref_count[1]= h->ref_count[0]= 0;
2861
3047
        return -1;
 
3048
    }
2862
3049
 
2863
3050
    if(h->slice_type_nos!=AV_PICTURE_TYPE_I){
2864
3051
        s->last_picture_ptr= &h->ref_list[0][0];
2882
3069
        }
2883
3070
    }
2884
3071
 
2885
 
    if(h->nal_ref_idc)
2886
 
        ff_h264_decode_ref_pic_marking(h0, &s->gb);
 
3072
    if(h->nal_ref_idc && ff_h264_decode_ref_pic_marking(h0, &s->gb) < 0 &&
 
3073
       (s->avctx->err_recognition & AV_EF_EXPLODE))
 
3074
        return AVERROR_INVALIDDATA;
2887
3075
 
2888
3076
    if(FRAME_MBAFF){
2889
3077
        ff_h264_fill_mbaff_ref_list(h);
2971
3159
            }
2972
3160
        }
2973
3161
    }
2974
 
    h->qp_thresh= 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset) - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1]);
 
3162
    h->qp_thresh = 15 + 52 - FFMIN(h->slice_alpha_c0_offset, h->slice_beta_offset)
 
3163
                 - FFMAX3(0, h->pps.chroma_qp_index_offset[0], h->pps.chroma_qp_index_offset[1])
 
3164
                 + 6 * (h->sps.bit_depth_luma - 8);
2975
3165
 
2976
3166
#if 0 //FMO
2977
3167
    if( h->pps.num_slice_groups > 1  && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5)
2989
3179
        int *ref2frm= h->ref2frm[h->slice_num&(MAX_SLICES-1)][j];
2990
3180
        for(i=0; i<16; i++){
2991
3181
            id_list[i]= 60;
2992
 
            if(h->ref_list[j][i].data[0]){
 
3182
            if (h->ref_list[j][i].f.data[0]) {
2993
3183
                int k;
2994
 
                uint8_t *base= h->ref_list[j][i].base[0];
 
3184
                uint8_t *base = h->ref_list[j][i].f.base[0];
2995
3185
                for(k=0; k<h->short_ref_count; k++)
2996
 
                    if(h->short_ref[k]->base[0] == base){
 
3186
                    if (h->short_ref[k]->f.base[0] == base) {
2997
3187
                        id_list[i]= k;
2998
3188
                        break;
2999
3189
                    }
3000
3190
                for(k=0; k<h->long_ref_count; k++)
3001
 
                    if(h->long_ref[k] && h->long_ref[k]->base[0] == base){
 
3191
                    if (h->long_ref[k] && h->long_ref[k]->f.base[0] == base) {
3002
3192
                        id_list[i]= h->short_ref_count + k;
3003
3193
                        break;
3004
3194
                    }
3009
3199
        ref2frm[1]= -1;
3010
3200
        for(i=0; i<16; i++)
3011
3201
            ref2frm[i+2]= 4*id_list[i]
3012
 
                          +(h->ref_list[j][i].reference&3);
 
3202
                          + (h->ref_list[j][i].f.reference & 3);
3013
3203
        ref2frm[18+0]=
3014
3204
        ref2frm[18+1]= -1;
3015
3205
        for(i=16; i<48; i++)
3016
3206
            ref2frm[i+4]= 4*id_list[(i-16)>>1]
3017
 
                          +(h->ref_list[j][i].reference&3);
 
3207
                          + (h->ref_list[j][i].f.reference & 3);
3018
3208
    }
3019
3209
 
3020
3210
    //FIXME: fix draw_edges+PAFF+frame threads
3053
3243
    }
3054
3244
}
3055
3245
 
 
3246
static av_always_inline void fill_filter_caches_inter(H264Context *h, MpegEncContext * const s, int mb_type, int top_xy,
 
3247
                                                      int left_xy[LEFT_MBS], int top_type, int left_type[LEFT_MBS], int mb_xy, int list)
 
3248
{
 
3249
    int b_stride = h->b_stride;
 
3250
    int16_t (*mv_dst)[2] = &h->mv_cache[list][scan8[0]];
 
3251
    int8_t *ref_cache = &h->ref_cache[list][scan8[0]];
 
3252
    if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
 
3253
        if(USES_LIST(top_type, list)){
 
3254
            const int b_xy= h->mb2b_xy[top_xy] + 3*b_stride;
 
3255
            const int b8_xy= 4*top_xy + 2;
 
3256
            int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
 
3257
            AV_COPY128(mv_dst - 1*8, s->current_picture.f.motion_val[list][b_xy + 0]);
 
3258
            ref_cache[0 - 1*8]=
 
3259
            ref_cache[1 - 1*8]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 0]];
 
3260
            ref_cache[2 - 1*8]=
 
3261
            ref_cache[3 - 1*8]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 1]];
 
3262
        }else{
 
3263
            AV_ZERO128(mv_dst - 1*8);
 
3264
            AV_WN32A(&ref_cache[0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
 
3265
        }
 
3266
 
 
3267
        if(!IS_INTERLACED(mb_type^left_type[LTOP])){
 
3268
            if(USES_LIST(left_type[LTOP], list)){
 
3269
                const int b_xy= h->mb2b_xy[left_xy[LTOP]] + 3;
 
3270
                const int b8_xy= 4*left_xy[LTOP] + 1;
 
3271
                int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[LTOP]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
 
3272
                AV_COPY32(mv_dst - 1 +  0, s->current_picture.f.motion_val[list][b_xy + b_stride*0]);
 
3273
                AV_COPY32(mv_dst - 1 +  8, s->current_picture.f.motion_val[list][b_xy + b_stride*1]);
 
3274
                AV_COPY32(mv_dst - 1 + 16, s->current_picture.f.motion_val[list][b_xy + b_stride*2]);
 
3275
                AV_COPY32(mv_dst - 1 + 24, s->current_picture.f.motion_val[list][b_xy + b_stride*3]);
 
3276
                ref_cache[-1 +  0]=
 
3277
                ref_cache[-1 +  8]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2*0]];
 
3278
                ref_cache[-1 + 16]=
 
3279
                ref_cache[-1 + 24]= ref2frm[list][s->current_picture.f.ref_index[list][b8_xy + 2*1]];
 
3280
            }else{
 
3281
                AV_ZERO32(mv_dst - 1 + 0);
 
3282
                AV_ZERO32(mv_dst - 1 + 8);
 
3283
                AV_ZERO32(mv_dst - 1 +16);
 
3284
                AV_ZERO32(mv_dst - 1 +24);
 
3285
                ref_cache[-1 +  0]=
 
3286
                ref_cache[-1 +  8]=
 
3287
                ref_cache[-1 + 16]=
 
3288
                ref_cache[-1 + 24]= LIST_NOT_USED;
 
3289
            }
 
3290
        }
 
3291
    }
 
3292
 
 
3293
    if(!USES_LIST(mb_type, list)){
 
3294
        fill_rectangle(mv_dst, 4, 4, 8, pack16to32(0,0), 4);
 
3295
        AV_WN32A(&ref_cache[0*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
 
3296
        AV_WN32A(&ref_cache[1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
 
3297
        AV_WN32A(&ref_cache[2*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
 
3298
        AV_WN32A(&ref_cache[3*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
 
3299
        return;
 
3300
    }
 
3301
 
 
3302
    {
 
3303
        int8_t *ref = &s->current_picture.f.ref_index[list][4*mb_xy];
 
3304
        int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
 
3305
        uint32_t ref01 = (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101;
 
3306
        uint32_t ref23 = (pack16to32(ref2frm[list][ref[2]],ref2frm[list][ref[3]])&0x00FF00FF)*0x0101;
 
3307
        AV_WN32A(&ref_cache[0*8], ref01);
 
3308
        AV_WN32A(&ref_cache[1*8], ref01);
 
3309
        AV_WN32A(&ref_cache[2*8], ref23);
 
3310
        AV_WN32A(&ref_cache[3*8], ref23);
 
3311
    }
 
3312
 
 
3313
    {
 
3314
        int16_t (*mv_src)[2] = &s->current_picture.f.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
 
3315
        AV_COPY128(mv_dst + 8*0, mv_src + 0*b_stride);
 
3316
        AV_COPY128(mv_dst + 8*1, mv_src + 1*b_stride);
 
3317
        AV_COPY128(mv_dst + 8*2, mv_src + 2*b_stride);
 
3318
        AV_COPY128(mv_dst + 8*3, mv_src + 3*b_stride);
 
3319
    }
 
3320
}
 
3321
 
3056
3322
/**
3057
3323
 *
3058
 
 * @return non zero if the loop filter can be skiped
 
3324
 * @return non zero if the loop filter can be skipped
3059
3325
 */
3060
3326
static int fill_filter_caches(H264Context *h, int mb_type){
3061
3327
    MpegEncContext * const s = &h->s;
3062
3328
    const int mb_xy= h->mb_xy;
3063
 
    int top_xy, left_xy[2];
3064
 
    int top_type, left_type[2];
 
3329
    int top_xy, left_xy[LEFT_MBS];
 
3330
    int top_type, left_type[LEFT_MBS];
 
3331
    uint8_t *nnz;
 
3332
    uint8_t *nnz_cache;
3065
3333
 
3066
3334
    top_xy     = mb_xy  - (s->mb_stride << MB_FIELD);
3067
3335
 
3068
 
    //FIXME deblocking could skip the intra and nnz parts.
3069
 
 
3070
3336
    /* Wow, what a mess, why didn't they simplify the interlacing & intra
3071
3337
     * stuff, I can't imagine that these complex rules are worth it. */
3072
3338
 
3073
 
    left_xy[1] = left_xy[0] = mb_xy-1;
 
3339
    left_xy[LBOT] = left_xy[LTOP] = mb_xy-1;
3074
3340
    if(FRAME_MBAFF){
3075
 
        const int left_mb_field_flag     = IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]);
 
3341
        const int left_mb_field_flag     = IS_INTERLACED(s->current_picture.f.mb_type[mb_xy - 1]);
3076
3342
        const int curr_mb_field_flag     = IS_INTERLACED(mb_type);
3077
3343
        if(s->mb_y&1){
3078
3344
            if (left_mb_field_flag != curr_mb_field_flag) {
3079
 
                left_xy[0] -= s->mb_stride;
 
3345
                left_xy[LTOP] -= s->mb_stride;
3080
3346
            }
3081
3347
        }else{
3082
3348
            if(curr_mb_field_flag){
3083
 
                top_xy      += s->mb_stride & (((s->current_picture.mb_type[top_xy    ]>>7)&1)-1);
 
3349
                top_xy += s->mb_stride & (((s->current_picture.f.mb_type[top_xy] >> 7) & 1) - 1);
3084
3350
            }
3085
3351
            if (left_mb_field_flag != curr_mb_field_flag) {
3086
 
                left_xy[1] += s->mb_stride;
 
3352
                left_xy[LBOT] += s->mb_stride;
3087
3353
            }
3088
3354
        }
3089
3355
    }
3090
3356
 
3091
3357
    h->top_mb_xy = top_xy;
3092
 
    h->left_mb_xy[0] = left_xy[0];
3093
 
    h->left_mb_xy[1] = left_xy[1];
 
3358
    h->left_mb_xy[LTOP] = left_xy[LTOP];
 
3359
    h->left_mb_xy[LBOT] = left_xy[LBOT];
3094
3360
    {
3095
3361
        //for sufficiently low qp, filtering wouldn't do anything
3096
3362
        //this is a conservative estimate: could also check beta_offset and more accurate chroma_qp
3097
3363
        int qp_thresh = h->qp_thresh; //FIXME strictly we should store qp_thresh for each mb of a slice
3098
 
        int qp = s->current_picture.qscale_table[mb_xy];
 
3364
        int qp = s->current_picture.f.qscale_table[mb_xy];
3099
3365
        if(qp <= qp_thresh
3100
 
           && (left_xy[0]<0 || ((qp + s->current_picture.qscale_table[left_xy[0]] + 1)>>1) <= qp_thresh)
3101
 
           && (top_xy   < 0 || ((qp + s->current_picture.qscale_table[top_xy    ] + 1)>>1) <= qp_thresh)){
 
3366
           && (left_xy[LTOP] < 0 || ((qp + s->current_picture.f.qscale_table[left_xy[LTOP]] + 1) >> 1) <= qp_thresh)
 
3367
           && (top_xy        < 0 || ((qp + s->current_picture.f.qscale_table[top_xy       ] + 1) >> 1) <= qp_thresh)) {
3102
3368
            if(!FRAME_MBAFF)
3103
3369
                return 1;
3104
 
            if(   (left_xy[0]< 0            || ((qp + s->current_picture.qscale_table[left_xy[1]             ] + 1)>>1) <= qp_thresh)
3105
 
               && (top_xy    < s->mb_stride || ((qp + s->current_picture.qscale_table[top_xy    -s->mb_stride] + 1)>>1) <= qp_thresh))
 
3370
            if ((left_xy[LTOP] < 0            || ((qp + s->current_picture.f.qscale_table[left_xy[LBOT]        ] + 1) >> 1) <= qp_thresh) &&
 
3371
                (top_xy        < s->mb_stride || ((qp + s->current_picture.f.qscale_table[top_xy - s->mb_stride] + 1) >> 1) <= qp_thresh))
3106
3372
                return 1;
3107
3373
        }
3108
3374
    }
3109
3375
 
3110
 
    top_type     = s->current_picture.mb_type[top_xy]    ;
3111
 
    left_type[0] = s->current_picture.mb_type[left_xy[0]];
3112
 
    left_type[1] = s->current_picture.mb_type[left_xy[1]];
 
3376
    top_type        = s->current_picture.f.mb_type[top_xy];
 
3377
    left_type[LTOP] = s->current_picture.f.mb_type[left_xy[LTOP]];
 
3378
    left_type[LBOT] = s->current_picture.f.mb_type[left_xy[LBOT]];
3113
3379
    if(h->deblocking_filter == 2){
3114
 
        if(h->slice_table[top_xy     ] != h->slice_num) top_type= 0;
3115
 
        if(h->slice_table[left_xy[0] ] != h->slice_num) left_type[0]= left_type[1]= 0;
 
3380
        if(h->slice_table[top_xy       ] != h->slice_num) top_type= 0;
 
3381
        if(h->slice_table[left_xy[LBOT]] != h->slice_num) left_type[LTOP]= left_type[LBOT]= 0;
3116
3382
    }else{
3117
 
        if(h->slice_table[top_xy     ] == 0xFFFF) top_type= 0;
3118
 
        if(h->slice_table[left_xy[0] ] == 0xFFFF) left_type[0]= left_type[1] =0;
 
3383
        if(h->slice_table[top_xy       ] == 0xFFFF) top_type= 0;
 
3384
        if(h->slice_table[left_xy[LBOT]] == 0xFFFF) left_type[LTOP]= left_type[LBOT] =0;
3119
3385
    }
3120
 
    h->top_type    = top_type    ;
3121
 
    h->left_type[0]= left_type[0];
3122
 
    h->left_type[1]= left_type[1];
 
3386
    h->top_type       = top_type;
 
3387
    h->left_type[LTOP]= left_type[LTOP];
 
3388
    h->left_type[LBOT]= left_type[LBOT];
3123
3389
 
3124
3390
    if(IS_INTRA(mb_type))
3125
3391
        return 0;
3126
3392
 
3127
 
    AV_COPY32(&h->non_zero_count_cache[4+8* 1], &h->non_zero_count[mb_xy][ 0]);
3128
 
    AV_COPY32(&h->non_zero_count_cache[4+8* 2], &h->non_zero_count[mb_xy][ 4]);
3129
 
    AV_COPY32(&h->non_zero_count_cache[4+8* 3], &h->non_zero_count[mb_xy][ 8]);
3130
 
    AV_COPY32(&h->non_zero_count_cache[4+8* 4], &h->non_zero_count[mb_xy][12]);
 
3393
    fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy, top_type, left_type, mb_xy, 0);
 
3394
    if(h->list_count == 2)
 
3395
        fill_filter_caches_inter(h, s, mb_type, top_xy, left_xy, top_type, left_type, mb_xy, 1);
3131
3396
 
 
3397
    nnz = h->non_zero_count[mb_xy];
 
3398
    nnz_cache = h->non_zero_count_cache;
 
3399
    AV_COPY32(&nnz_cache[4+8*1], &nnz[ 0]);
 
3400
    AV_COPY32(&nnz_cache[4+8*2], &nnz[ 4]);
 
3401
    AV_COPY32(&nnz_cache[4+8*3], &nnz[ 8]);
 
3402
    AV_COPY32(&nnz_cache[4+8*4], &nnz[12]);
3132
3403
    h->cbp= h->cbp_table[mb_xy];
3133
3404
 
3134
 
    {
3135
 
        int list;
3136
 
        for(list=0; list<h->list_count; list++){
3137
 
            int8_t *ref;
3138
 
            int y, b_stride;
3139
 
            int16_t (*mv_dst)[2];
3140
 
            int16_t (*mv_src)[2];
3141
 
 
3142
 
            if(!USES_LIST(mb_type, list)){
3143
 
                fill_rectangle(  h->mv_cache[list][scan8[0]], 4, 4, 8, pack16to32(0,0), 4);
3144
 
                AV_WN32A(&h->ref_cache[list][scan8[ 0]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3145
 
                AV_WN32A(&h->ref_cache[list][scan8[ 2]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3146
 
                AV_WN32A(&h->ref_cache[list][scan8[ 8]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3147
 
                AV_WN32A(&h->ref_cache[list][scan8[10]], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3148
 
                continue;
3149
 
            }
3150
 
 
3151
 
            ref = &s->current_picture.ref_index[list][4*mb_xy];
3152
 
            {
3153
 
                int (*ref2frm)[64] = h->ref2frm[ h->slice_num&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
3154
 
                AV_WN32A(&h->ref_cache[list][scan8[ 0]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
3155
 
                AV_WN32A(&h->ref_cache[list][scan8[ 2]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
3156
 
                ref += 2;
3157
 
                AV_WN32A(&h->ref_cache[list][scan8[ 8]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
3158
 
                AV_WN32A(&h->ref_cache[list][scan8[10]], (pack16to32(ref2frm[list][ref[0]],ref2frm[list][ref[1]])&0x00FF00FF)*0x0101);
3159
 
            }
3160
 
 
3161
 
            b_stride = h->b_stride;
3162
 
            mv_dst   = &h->mv_cache[list][scan8[0]];
3163
 
            mv_src   = &s->current_picture.motion_val[list][4*s->mb_x + 4*s->mb_y*b_stride];
3164
 
            for(y=0; y<4; y++){
3165
 
                AV_COPY128(mv_dst + 8*y, mv_src + y*b_stride);
3166
 
            }
3167
 
 
3168
 
        }
3169
 
    }
3170
 
 
3171
 
 
3172
 
/*
3173
 
0 . T T. T T T T
3174
 
1 L . .L . . . .
3175
 
2 L . .L . . . .
3176
 
3 . T TL . . . .
3177
 
4 L . .L . . . .
3178
 
5 L . .. . . . .
3179
 
*/
3180
 
//FIXME constraint_intra_pred & partitioning & nnz (let us hope this is just a typo in the spec)
3181
3405
    if(top_type){
3182
 
        AV_COPY32(&h->non_zero_count_cache[4+8*0], &h->non_zero_count[top_xy][3*4]);
 
3406
        nnz = h->non_zero_count[top_xy];
 
3407
        AV_COPY32(&nnz_cache[4+8*0], &nnz[3*4]);
3183
3408
    }
3184
3409
 
3185
 
    if(left_type[0]){
3186
 
        h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][3+0*4];
3187
 
        h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][3+1*4];
3188
 
        h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[0]][3+2*4];
3189
 
        h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[0]][3+3*4];
 
3410
    if(left_type[LTOP]){
 
3411
        nnz = h->non_zero_count[left_xy[LTOP]];
 
3412
        nnz_cache[3+8*1]= nnz[3+0*4];
 
3413
        nnz_cache[3+8*2]= nnz[3+1*4];
 
3414
        nnz_cache[3+8*3]= nnz[3+2*4];
 
3415
        nnz_cache[3+8*4]= nnz[3+3*4];
3190
3416
    }
3191
3417
 
3192
3418
    // CAVLC 8x8dct requires NNZ values for residual decoding that differ from what the loop filter needs
3193
3419
    if(!CABAC && h->pps.transform_8x8_mode){
3194
3420
        if(IS_8x8DCT(top_type)){
3195
 
            h->non_zero_count_cache[4+8*0]=
3196
 
            h->non_zero_count_cache[5+8*0]= (h->cbp_table[top_xy] & 0x4000) >> 12;
3197
 
            h->non_zero_count_cache[6+8*0]=
3198
 
            h->non_zero_count_cache[7+8*0]= (h->cbp_table[top_xy] & 0x8000) >> 12;
3199
 
        }
3200
 
        if(IS_8x8DCT(left_type[0])){
3201
 
            h->non_zero_count_cache[3+8*1]=
3202
 
            h->non_zero_count_cache[3+8*2]= (h->cbp_table[left_xy[0]]&0x2000) >> 12; //FIXME check MBAFF
3203
 
        }
3204
 
        if(IS_8x8DCT(left_type[1])){
3205
 
            h->non_zero_count_cache[3+8*3]=
3206
 
            h->non_zero_count_cache[3+8*4]= (h->cbp_table[left_xy[1]]&0x8000) >> 12; //FIXME check MBAFF
 
3421
            nnz_cache[4+8*0]=
 
3422
            nnz_cache[5+8*0]= (h->cbp_table[top_xy] & 0x4000) >> 12;
 
3423
            nnz_cache[6+8*0]=
 
3424
            nnz_cache[7+8*0]= (h->cbp_table[top_xy] & 0x8000) >> 12;
 
3425
        }
 
3426
        if(IS_8x8DCT(left_type[LTOP])){
 
3427
            nnz_cache[3+8*1]=
 
3428
            nnz_cache[3+8*2]= (h->cbp_table[left_xy[LTOP]]&0x2000) >> 12; //FIXME check MBAFF
 
3429
        }
 
3430
        if(IS_8x8DCT(left_type[LBOT])){
 
3431
            nnz_cache[3+8*3]=
 
3432
            nnz_cache[3+8*4]= (h->cbp_table[left_xy[LBOT]]&0x8000) >> 12; //FIXME check MBAFF
3207
3433
        }
3208
3434
 
3209
3435
        if(IS_8x8DCT(mb_type)){
3210
 
            h->non_zero_count_cache[scan8[0   ]]= h->non_zero_count_cache[scan8[1   ]]=
3211
 
            h->non_zero_count_cache[scan8[2   ]]= h->non_zero_count_cache[scan8[3   ]]= (h->cbp & 0x1000) >> 12;
3212
 
 
3213
 
            h->non_zero_count_cache[scan8[0+ 4]]= h->non_zero_count_cache[scan8[1+ 4]]=
3214
 
            h->non_zero_count_cache[scan8[2+ 4]]= h->non_zero_count_cache[scan8[3+ 4]]= (h->cbp & 0x2000) >> 12;
3215
 
 
3216
 
            h->non_zero_count_cache[scan8[0+ 8]]= h->non_zero_count_cache[scan8[1+ 8]]=
3217
 
            h->non_zero_count_cache[scan8[2+ 8]]= h->non_zero_count_cache[scan8[3+ 8]]= (h->cbp & 0x4000) >> 12;
3218
 
 
3219
 
            h->non_zero_count_cache[scan8[0+12]]= h->non_zero_count_cache[scan8[1+12]]=
3220
 
            h->non_zero_count_cache[scan8[2+12]]= h->non_zero_count_cache[scan8[3+12]]= (h->cbp & 0x8000) >> 12;
3221
 
        }
3222
 
    }
3223
 
 
3224
 
    if(IS_INTER(mb_type) || IS_DIRECT(mb_type)){
3225
 
        int list;
3226
 
        for(list=0; list<h->list_count; list++){
3227
 
            if(USES_LIST(top_type, list)){
3228
 
                const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
3229
 
                const int b8_xy= 4*top_xy + 2;
3230
 
                int (*ref2frm)[64] = h->ref2frm[ h->slice_table[top_xy]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
3231
 
                AV_COPY128(h->mv_cache[list][scan8[0] + 0 - 1*8], s->current_picture.motion_val[list][b_xy + 0]);
3232
 
                h->ref_cache[list][scan8[0] + 0 - 1*8]=
3233
 
                h->ref_cache[list][scan8[0] + 1 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 0]];
3234
 
                h->ref_cache[list][scan8[0] + 2 - 1*8]=
3235
 
                h->ref_cache[list][scan8[0] + 3 - 1*8]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 1]];
3236
 
            }else{
3237
 
                AV_ZERO128(h->mv_cache[list][scan8[0] + 0 - 1*8]);
3238
 
                AV_WN32A(&h->ref_cache[list][scan8[0] + 0 - 1*8], ((LIST_NOT_USED)&0xFF)*0x01010101u);
3239
 
            }
3240
 
 
3241
 
            if(!IS_INTERLACED(mb_type^left_type[0])){
3242
 
                if(USES_LIST(left_type[0], list)){
3243
 
                    const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
3244
 
                    const int b8_xy= 4*left_xy[0] + 1;
3245
 
                    int (*ref2frm)[64] = h->ref2frm[ h->slice_table[left_xy[0]]&(MAX_SLICES-1) ][0] + (MB_MBAFF ? 20 : 2);
3246
 
                    AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 0 ], s->current_picture.motion_val[list][b_xy + h->b_stride*0]);
3247
 
                    AV_COPY32(h->mv_cache[list][scan8[0] - 1 + 8 ], s->current_picture.motion_val[list][b_xy + h->b_stride*1]);
3248
 
                    AV_COPY32(h->mv_cache[list][scan8[0] - 1 +16 ], s->current_picture.motion_val[list][b_xy + h->b_stride*2]);
3249
 
                    AV_COPY32(h->mv_cache[list][scan8[0] - 1 +24 ], s->current_picture.motion_val[list][b_xy + h->b_stride*3]);
3250
 
                    h->ref_cache[list][scan8[0] - 1 + 0 ]=
3251
 
                    h->ref_cache[list][scan8[0] - 1 + 8 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*0]];
3252
 
                    h->ref_cache[list][scan8[0] - 1 +16 ]=
3253
 
                    h->ref_cache[list][scan8[0] - 1 +24 ]= ref2frm[list][s->current_picture.ref_index[list][b8_xy + 2*1]];
3254
 
                }else{
3255
 
                    AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 0 ]);
3256
 
                    AV_ZERO32(h->mv_cache [list][scan8[0] - 1 + 8 ]);
3257
 
                    AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +16 ]);
3258
 
                    AV_ZERO32(h->mv_cache [list][scan8[0] - 1 +24 ]);
3259
 
                    h->ref_cache[list][scan8[0] - 1 + 0  ]=
3260
 
                    h->ref_cache[list][scan8[0] - 1 + 8  ]=
3261
 
                    h->ref_cache[list][scan8[0] - 1 + 16 ]=
3262
 
                    h->ref_cache[list][scan8[0] - 1 + 24 ]= LIST_NOT_USED;
3263
 
                }
3264
 
            }
 
3436
            nnz_cache[scan8[0   ]]= nnz_cache[scan8[1   ]]=
 
3437
            nnz_cache[scan8[2   ]]= nnz_cache[scan8[3   ]]= (h->cbp & 0x1000) >> 12;
 
3438
 
 
3439
            nnz_cache[scan8[0+ 4]]= nnz_cache[scan8[1+ 4]]=
 
3440
            nnz_cache[scan8[2+ 4]]= nnz_cache[scan8[3+ 4]]= (h->cbp & 0x2000) >> 12;
 
3441
 
 
3442
            nnz_cache[scan8[0+ 8]]= nnz_cache[scan8[1+ 8]]=
 
3443
            nnz_cache[scan8[2+ 8]]= nnz_cache[scan8[3+ 8]]= (h->cbp & 0x4000) >> 12;
 
3444
 
 
3445
            nnz_cache[scan8[0+12]]= nnz_cache[scan8[1+12]]=
 
3446
            nnz_cache[scan8[2+12]]= nnz_cache[scan8[3+12]]= (h->cbp & 0x8000) >> 12;
3265
3447
        }
3266
3448
    }
3267
3449
 
3275
3457
    const int end_mb_y= s->mb_y + FRAME_MBAFF;
3276
3458
    const int old_slice_type= h->slice_type;
3277
3459
    const int pixel_shift = h->pixel_shift;
 
3460
    const int block_h = 16 >> s->chroma_y_shift;
3278
3461
 
3279
3462
    if(h->deblocking_filter) {
3280
3463
        for(mb_x= start_x; mb_x<end_x; mb_x++){
3282
3465
                int mb_xy, mb_type;
3283
3466
                mb_xy = h->mb_xy = mb_x + mb_y*s->mb_stride;
3284
3467
                h->slice_num= h->slice_table[mb_xy];
3285
 
                mb_type= s->current_picture.mb_type[mb_xy];
 
3468
                mb_type = s->current_picture.f.mb_type[mb_xy];
3286
3469
                h->list_count= h->list_counts[mb_xy];
3287
3470
 
3288
3471
                if(FRAME_MBAFF)
3290
3473
 
3291
3474
                s->mb_x= mb_x;
3292
3475
                s->mb_y= mb_y;
3293
 
                dest_y  = s->current_picture.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize  ) * 16;
3294
 
                dest_cb = s->current_picture.data[1] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * (8 << CHROMA444);
3295
 
                dest_cr = s->current_picture.data[2] + ((mb_x << pixel_shift) + mb_y * s->uvlinesize) * (8 << CHROMA444);
 
3476
                dest_y  = s->current_picture.f.data[0] + ((mb_x << pixel_shift) + mb_y * s->linesize  ) * 16;
 
3477
                dest_cb = s->current_picture.f.data[1] + (mb_x << pixel_shift) * (8 << CHROMA444) + mb_y * s->uvlinesize * block_h;
 
3478
                dest_cr = s->current_picture.f.data[2] + (mb_x << pixel_shift) * (8 << CHROMA444) + mb_y * s->uvlinesize * block_h;
3296
3479
                    //FIXME simplify above
3297
3480
 
3298
3481
                if (MB_FIELD) {
3300
3483
                    uvlinesize = h->mb_uvlinesize = s->uvlinesize * 2;
3301
3484
                    if(mb_y&1){ //FIXME move out of this function?
3302
3485
                        dest_y -= s->linesize*15;
3303
 
                        dest_cb-= s->uvlinesize*((8 << CHROMA444)-1);
3304
 
                        dest_cr-= s->uvlinesize*((8 << CHROMA444)-1);
 
3486
                        dest_cb-= s->uvlinesize * (block_h - 1);
 
3487
                        dest_cr-= s->uvlinesize * (block_h - 1);
3305
3488
                    }
3306
3489
                } else {
3307
3490
                    linesize   = h->mb_linesize   = s->linesize;
3308
3491
                    uvlinesize = h->mb_uvlinesize = s->uvlinesize;
3309
3492
                }
3310
 
                backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, CHROMA444, 0);
 
3493
                backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
3311
3494
                if(fill_filter_caches(h, mb_type))
3312
3495
                    continue;
3313
 
                h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.qscale_table[mb_xy]);
3314
 
                h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.qscale_table[mb_xy]);
 
3496
                h->chroma_qp[0] = get_chroma_qp(h, 0, s->current_picture.f.qscale_table[mb_xy]);
 
3497
                h->chroma_qp[1] = get_chroma_qp(h, 1, s->current_picture.f.qscale_table[mb_xy]);
3315
3498
 
3316
3499
                if (FRAME_MBAFF) {
3317
3500
                    ff_h264_filter_mb     (h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3332
3515
    MpegEncContext * const s = &h->s;
3333
3516
    const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
3334
3517
    int mb_type = (h->slice_table[mb_xy-1] == h->slice_num)
3335
 
                ? s->current_picture.mb_type[mb_xy-1]
 
3518
                ? s->current_picture.f.mb_type[mb_xy - 1]
3336
3519
                : (h->slice_table[mb_xy-s->mb_stride] == h->slice_num)
3337
 
                ? s->current_picture.mb_type[mb_xy-s->mb_stride]
 
3520
                ? s->current_picture.f.mb_type[mb_xy - s->mb_stride]
3338
3521
                : 0;
3339
3522
    h->mb_mbaff = h->mb_field_decoding_flag = IS_INTERLACED(mb_type) ? 1 : 0;
3340
3523
}
3376
3559
static int decode_slice(struct AVCodecContext *avctx, void *arg){
3377
3560
    H264Context *h = *(void**)arg;
3378
3561
    MpegEncContext * const s = &h->s;
3379
 
    const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
 
3562
    const int part_mask= s->partitioned_frame ? (ER_AC_END|ER_AC_ERROR) : 0x7F;
3380
3563
    int lf_x_start = s->mb_x;
3381
3564
 
3382
3565
    s->mb_skip_run= -1;
3415
3598
            eos = get_cabac_terminate( &h->cabac );
3416
3599
 
3417
3600
            if((s->workaround_bugs & FF_BUG_TRUNCATED) && h->cabac.bytestream > h->cabac.bytestream_end + 2){
3418
 
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
 
3601
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END&part_mask);
3419
3602
                if (s->mb_x >= lf_x_start) loop_filter(h, lf_x_start, s->mb_x + 1);
3420
3603
                return 0;
3421
3604
            }
3422
3605
            if( ret < 0 || h->cabac.bytestream > h->cabac.bytestream_end + 2) {
3423
3606
                av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d, bytestream (%td)\n", s->mb_x, s->mb_y, h->cabac.bytestream_end - h->cabac.bytestream);
3424
 
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
 
3607
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
3425
3608
                return -1;
3426
3609
            }
3427
3610
 
3439
3622
 
3440
3623
            if( eos || s->mb_y >= s->mb_height ) {
3441
3624
                tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
3442
 
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
 
3625
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END&part_mask);
3443
3626
                if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
3444
3627
                return 0;
3445
3628
            }
3461
3644
 
3462
3645
            if(ret<0){
3463
3646
                av_log(h->s.avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
3464
 
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
 
3647
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
3465
3648
                return -1;
3466
3649
            }
3467
3650
 
3479
3662
                    tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
3480
3663
 
3481
3664
                    if(get_bits_count(&s->gb) == s->gb.size_in_bits ) {
3482
 
                        ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
 
3665
                        ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END&part_mask);
3483
3666
 
3484
3667
                        return 0;
3485
3668
                    }else{
3486
 
                        ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
 
3669
                        ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
3487
3670
 
3488
3671
                        return -1;
3489
3672
                    }
3493
3676
            if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){
3494
3677
                tprintf(s->avctx, "slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
3495
3678
                if(get_bits_count(&s->gb) == s->gb.size_in_bits ){
3496
 
                    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
 
3679
                    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END&part_mask);
3497
3680
                    if (s->mb_x > lf_x_start) loop_filter(h, lf_x_start, s->mb_x);
3498
3681
 
3499
3682
                    return 0;
3500
3683
                }else{
3501
 
                    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
3502
 
 
3503
 
                    return -1;
3504
 
                }
3505
 
            }
3506
 
        }
3507
 
    }
3508
 
 
3509
 
#if 0
3510
 
    for(;s->mb_y < s->mb_height; s->mb_y++){
3511
 
        for(;s->mb_x < s->mb_width; s->mb_x++){
3512
 
            int ret= decode_mb(h);
3513
 
 
3514
 
            ff_h264_hl_decode_mb(h);
3515
 
 
3516
 
            if(ret<0){
3517
 
                av_log(s->avctx, AV_LOG_ERROR, "error while decoding MB %d %d\n", s->mb_x, s->mb_y);
3518
 
                ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
3519
 
 
3520
 
                return -1;
3521
 
            }
3522
 
 
3523
 
            if(++s->mb_x >= s->mb_width){
3524
 
                s->mb_x=0;
3525
 
                if(++s->mb_y >= s->mb_height){
3526
 
                    if(get_bits_count(s->gb) == s->gb.size_in_bits){
3527
 
                        ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
3528
 
 
3529
 
                        return 0;
3530
 
                    }else{
3531
 
                        ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
3532
 
 
3533
 
                        return -1;
3534
 
                    }
3535
 
                }
3536
 
            }
3537
 
 
3538
 
            if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){
3539
 
                if(get_bits_count(s->gb) == s->gb.size_in_bits){
3540
 
                    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
3541
 
 
3542
 
                    return 0;
3543
 
                }else{
3544
 
                    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
3545
 
 
3546
 
                    return -1;
3547
 
                }
3548
 
            }
3549
 
        }
3550
 
        s->mb_x=0;
3551
 
        ff_draw_horiz_band(s, 16*s->mb_y, 16);
3552
 
    }
3553
 
#endif
3554
 
    return -1; //not reached
 
3684
                    ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
 
3685
 
 
3686
                    return -1;
 
3687
                }
 
3688
            }
 
3689
        }
 
3690
    }
3555
3691
}
3556
3692
 
3557
3693
/**
3560
3696
 * @param h h264 master context
3561
3697
 * @param context_count number of contexts to execute
3562
3698
 */
3563
 
static void execute_decode_slices(H264Context *h, int context_count){
 
3699
static int execute_decode_slices(H264Context *h, int context_count){
3564
3700
    MpegEncContext * const s = &h->s;
3565
3701
    AVCodecContext * const avctx= s->avctx;
3566
3702
    H264Context *hx;
3567
3703
    int i;
3568
3704
 
3569
 
    if (s->avctx->hwaccel)
3570
 
        return;
3571
 
    if(s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3572
 
        return;
 
3705
    if (s->avctx->hwaccel || s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
 
3706
        return 0;
3573
3707
    if(context_count == 1) {
3574
 
        decode_slice(avctx, &h);
 
3708
        return decode_slice(avctx, &h);
3575
3709
    } else {
3576
3710
        for(i = 1; i < context_count; i++) {
3577
3711
            hx = h->thread_context[i];
3578
 
            hx->s.error_recognition = avctx->error_recognition;
 
3712
            hx->s.err_recognition = avctx->err_recognition;
3579
3713
            hx->s.error_count = 0;
3580
3714
        }
3581
3715
 
3582
 
        avctx->execute(avctx, (void *)decode_slice,
 
3716
        avctx->execute(avctx, decode_slice,
3583
3717
                       h->thread_context, NULL, context_count, sizeof(void*));
3584
3718
 
3585
3719
        /* pull back stuff from slices to master context */
3591
3725
        for(i = 1; i < context_count; i++)
3592
3726
            h->s.error_count += h->thread_context[i]->s.error_count;
3593
3727
    }
 
3728
 
 
3729
    return 0;
3594
3730
}
3595
3731
 
3596
3732
 
3605
3741
    int nals_needed=0; ///< number of NALs that need decoding before the next frame thread starts
3606
3742
    int nal_index;
3607
3743
 
3608
 
    h->max_contexts = (HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_SLICE)) ? avctx->thread_count : 1;
 
3744
    h->max_contexts = s->slice_context_count;
3609
3745
    if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
3610
3746
        h->current_slice = 0;
3611
3747
        if (!s->first_field)
3622
3758
        int consumed;
3623
3759
        int dst_length;
3624
3760
        int bit_length;
3625
 
        const uint8_t *ptr;
 
3761
        uint8_t *ptr;
3626
3762
        int i, nalsize = 0;
3627
3763
        int err;
3628
3764
 
3629
3765
        if(buf_index >= next_avc) {
3630
 
            if(buf_index >= buf_size) break;
 
3766
            if (buf_index >= buf_size - h->nal_length_size) break;
3631
3767
            nalsize = 0;
3632
3768
            for(i = 0; i < h->nal_length_size; i++)
3633
3769
                nalsize = (nalsize << 8) | buf[buf_index++];
3672
3808
        }
3673
3809
 
3674
3810
        if (h->is_avc && (nalsize != consumed) && nalsize){
 
3811
            // set trailing bits in the last partial byte to zero
 
3812
            if (bit_length & 7)
 
3813
                ptr[bit_length >> 3] = ptr[bit_length >> 3] & (0xff << 8 - (bit_length & 7));
3675
3814
            av_log(h->s.avctx, AV_LOG_DEBUG, "AVC: Consumed only %d bytes instead of %d\n", consumed, nalsize);
3676
3815
        }
3677
3816
 
3685
3824
            switch (hx->nal_unit_type) {
3686
3825
                case NAL_SPS:
3687
3826
                case NAL_PPS:
 
3827
                    nals_needed = nal_index;
 
3828
                    break;
3688
3829
                case NAL_IDR_SLICE:
3689
3830
                case NAL_SLICE:
3690
 
                    nals_needed = nal_index;
 
3831
                    init_get_bits(&hx->s.gb, ptr, bit_length);
 
3832
                    if (!get_ue_golomb(&hx->s.gb))
 
3833
                        nals_needed = nal_index;
3691
3834
            }
3692
3835
            continue;
3693
3836
        }
3704
3847
                av_log(h->s.avctx, AV_LOG_ERROR, "Invalid mix of idr and non-idr slices");
3705
3848
                return -1;
3706
3849
            }
3707
 
            idr(h); //FIXME ensure we don't loose some frames if there is reordering
 
3850
            idr(h); // FIXME ensure we don't lose some frames if there is reordering
3708
3851
        case NAL_SLICE:
3709
3852
            init_get_bits(&hx->s.gb, ptr, bit_length);
3710
3853
            hx->intra_gb_ptr=
3714
3857
            if((err = decode_slice_header(hx, h)))
3715
3858
               break;
3716
3859
 
3717
 
            s->current_picture_ptr->key_frame |=
 
3860
            s->current_picture_ptr->f.key_frame |=
3718
3861
                    (hx->nal_unit_type == NAL_IDR_SLICE) ||
3719
3862
                    (h->sei_recovery_frame_cnt >= 0);
3720
3863
 
3788
3931
            if(avctx->has_b_frames < 2)
3789
3932
                avctx->has_b_frames= !s->low_delay;
3790
3933
 
3791
 
            if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma) {
 
3934
            if (avctx->bits_per_raw_sample != h->sps.bit_depth_luma ||
 
3935
                h->cur_chroma_format_idc != h->sps.chroma_format_idc) {
3792
3936
                if (h->sps.bit_depth_luma >= 8 && h->sps.bit_depth_luma <= 10) {
3793
3937
                    avctx->bits_per_raw_sample = h->sps.bit_depth_luma;
 
3938
                    h->cur_chroma_format_idc = h->sps.chroma_format_idc;
3794
3939
                    h->pixel_shift = h->sps.bit_depth_luma > 8;
3795
3940
 
3796
 
                    ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma);
3797
 
                    ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma);
 
3941
                    ff_h264dsp_init(&h->h264dsp, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
 
3942
                    ff_h264_pred_init(&h->hpc, s->codec_id, h->sps.bit_depth_luma, h->sps.chroma_format_idc);
 
3943
                    s->dsp.dct_bits = h->sps.bit_depth_luma > 8 ? 32 : 16;
3798
3944
                    dsputil_init(&s->dsp, s->avctx);
3799
3945
                } else {
3800
 
                    av_log(avctx, AV_LOG_DEBUG, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
 
3946
                    av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", h->sps.bit_depth_luma);
3801
3947
                    return -1;
3802
3948
                }
3803
3949
            }
3844
3990
}
3845
3991
 
3846
3992
/**
3847
 
 * returns the number of bytes consumed for building the current frame
 
3993
 * Return the number of bytes consumed for building the current frame.
3848
3994
 */
3849
3995
static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){
3850
3996
        if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
3862
4008
    H264Context *h = avctx->priv_data;
3863
4009
    MpegEncContext *s = &h->s;
3864
4010
    AVFrame *pict = data;
3865
 
    int buf_index;
 
4011
    int buf_index = 0;
3866
4012
 
3867
4013
    s->flags= avctx->flags;
3868
4014
    s->flags2= avctx->flags2;
3878
4024
//FIXME factorize this with the output code below
3879
4025
        out = h->delayed_pic[0];
3880
4026
        out_idx = 0;
3881
 
        for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame && !h->delayed_pic[i]->mmco_reset; i++)
 
4027
        for (i = 1; h->delayed_pic[i] && !h->delayed_pic[i]->f.key_frame && !h->delayed_pic[i]->mmco_reset; i++)
3882
4028
            if(h->delayed_pic[i]->poc < out->poc){
3883
4029
                out = h->delayed_pic[i];
3884
4030
                out_idx = i;
3892
4038
            *pict= *(AVFrame*)out;
3893
4039
        }
3894
4040
 
3895
 
        return 0;
 
4041
        return buf_index;
3896
4042
    }
3897
4043
 
3898
4044
    buf_index=decode_nal_units(h, buf, buf_size);
3963
4109
    uint8_t temp[SIZE];
3964
4110
    PutBitContext pb;
3965
4111
    GetBitContext gb;
3966
 
//    int int_temp[10000];
3967
4112
    DSPContext dsp;
3968
4113
    AVCodecContext avctx;
3969
4114
 
 
4115
    avctx.av_class = avcodec_get_class();
3970
4116
    dsputil_init(&dsp, &avctx);
3971
4117
 
3972
4118
    init_put_bits(&pb, temp, SIZE);
3980
4126
 
3981
4127
    init_get_bits(&gb, temp, 8*SIZE);
3982
4128
    for(i=0; i<COUNT; i++){
3983
 
        int j, s;
3984
 
 
3985
 
        s= show_bits(&gb, 24);
 
4129
        int j, s = show_bits(&gb, 24);
3986
4130
 
3987
4131
        START_TIMER
3988
4132
        j= get_ue_golomb(&gb);
4005
4149
 
4006
4150
    init_get_bits(&gb, temp, 8*SIZE);
4007
4151
    for(i=0; i<COUNT; i++){
4008
 
        int j, s;
4009
 
 
4010
 
        s= show_bits(&gb, 24);
 
4152
        int j, s = show_bits(&gb, 24);
4011
4153
 
4012
4154
        START_TIMER
4013
4155
        j= get_se_golomb(&gb);
4018
4160
        STOP_TIMER("get_se_golomb");
4019
4161
    }
4020
4162
 
4021
 
#if 0
4022
 
    printf("testing 4x4 (I)DCT\n");
4023
 
 
4024
 
    DCTELEM block[16];
4025
 
    uint8_t src[16], ref[16];
4026
 
    uint64_t error= 0, max_error=0;
4027
 
 
4028
 
    for(i=0; i<COUNT; i++){
4029
 
        int j;
4030
 
//        printf("%d %d %d\n", r1, r2, (r2-r1)*16);
4031
 
        for(j=0; j<16; j++){
4032
 
            ref[j]= random()%255;
4033
 
            src[j]= random()%255;
4034
 
        }
4035
 
 
4036
 
        h264_diff_dct_c(block, src, ref, 4);
4037
 
 
4038
 
        //normalize
4039
 
        for(j=0; j<16; j++){
4040
 
//            printf("%d ", block[j]);
4041
 
            block[j]= block[j]*4;
4042
 
            if(j&1) block[j]= (block[j]*4 + 2)/5;
4043
 
            if(j&4) block[j]= (block[j]*4 + 2)/5;
4044
 
        }
4045
 
//        printf("\n");
4046
 
 
4047
 
        h->h264dsp.h264_idct_add(ref, block, 4);
4048
 
/*        for(j=0; j<16; j++){
4049
 
            printf("%d ", ref[j]);
4050
 
        }
4051
 
        printf("\n");*/
4052
 
 
4053
 
        for(j=0; j<16; j++){
4054
 
            int diff= FFABS(src[j] - ref[j]);
4055
 
 
4056
 
            error+= diff*diff;
4057
 
            max_error= FFMAX(max_error, diff);
4058
 
        }
4059
 
    }
4060
 
    printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error );
4061
 
    printf("testing quantizer\n");
4062
 
    for(qp=0; qp<52; qp++){
4063
 
        for(i=0; i<16; i++)
4064
 
            src1_block[i]= src2_block[i]= random()%255;
4065
 
 
4066
 
    }
4067
 
    printf("Testing NAL layer\n");
4068
 
 
4069
 
    uint8_t bitstream[COUNT];
4070
 
    uint8_t nal[COUNT*2];
4071
 
    H264Context h;
4072
 
    memset(&h, 0, sizeof(H264Context));
4073
 
 
4074
 
    for(i=0; i<COUNT; i++){
4075
 
        int zeros= i;
4076
 
        int nal_length;
4077
 
        int consumed;
4078
 
        int out_length;
4079
 
        uint8_t *out;
4080
 
        int j;
4081
 
 
4082
 
        for(j=0; j<COUNT; j++){
4083
 
            bitstream[j]= (random() % 255) + 1;
4084
 
        }
4085
 
 
4086
 
        for(j=0; j<zeros; j++){
4087
 
            int pos= random() % COUNT;
4088
 
            while(bitstream[pos] == 0){
4089
 
                pos++;
4090
 
                pos %= COUNT;
4091
 
            }
4092
 
            bitstream[pos]=0;
4093
 
        }
4094
 
 
4095
 
        START_TIMER
4096
 
 
4097
 
        nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2);
4098
 
        if(nal_length<0){
4099
 
            printf("encoding failed\n");
4100
 
            return -1;
4101
 
        }
4102
 
 
4103
 
        out= ff_h264_decode_nal(&h, nal, &out_length, &consumed, nal_length);
4104
 
 
4105
 
        STOP_TIMER("NAL")
4106
 
 
4107
 
        if(out_length != COUNT){
4108
 
            printf("incorrect length %d %d\n", out_length, COUNT);
4109
 
            return -1;
4110
 
        }
4111
 
 
4112
 
        if(consumed != nal_length){
4113
 
            printf("incorrect consumed length %d %d\n", nal_length, consumed);
4114
 
            return -1;
4115
 
        }
4116
 
 
4117
 
        if(memcmp(bitstream, out, COUNT)){
4118
 
            printf("mismatch\n");
4119
 
            return -1;
4120
 
        }
4121
 
    }
4122
 
#endif
4123
 
 
4124
4163
    printf("Testing RBSP\n");
4125
4164
 
4126
4165
 
4174
4213
};
4175
4214
 
4176
4215
AVCodec ff_h264_decoder = {
4177
 
    "h264",
4178
 
    AVMEDIA_TYPE_VIDEO,
4179
 
    CODEC_ID_H264,
4180
 
    sizeof(H264Context),
4181
 
    ff_h264_decode_init,
4182
 
    NULL,
4183
 
    ff_h264_decode_end,
4184
 
    decode_frame,
4185
 
    /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY |
4186
 
        CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
 
4216
    .name           = "h264",
 
4217
    .type           = AVMEDIA_TYPE_VIDEO,
 
4218
    .id             = CODEC_ID_H264,
 
4219
    .priv_data_size = sizeof(H264Context),
 
4220
    .init           = ff_h264_decode_init,
 
4221
    .close          = ff_h264_decode_end,
 
4222
    .decode         = decode_frame,
 
4223
    .capabilities   = /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_DELAY |
 
4224
                      CODEC_CAP_SLICE_THREADS | CODEC_CAP_FRAME_THREADS,
4187
4225
    .flush= flush_dpb,
4188
4226
    .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
4189
4227
    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
4193
4231
 
4194
4232
#if CONFIG_H264_VDPAU_DECODER
4195
4233
AVCodec ff_h264_vdpau_decoder = {
4196
 
    "h264_vdpau",
4197
 
    AVMEDIA_TYPE_VIDEO,
4198
 
    CODEC_ID_H264,
4199
 
    sizeof(H264Context),
4200
 
    ff_h264_decode_init,
4201
 
    NULL,
4202
 
    ff_h264_decode_end,
4203
 
    decode_frame,
4204
 
    CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
 
4234
    .name           = "h264_vdpau",
 
4235
    .type           = AVMEDIA_TYPE_VIDEO,
 
4236
    .id             = CODEC_ID_H264,
 
4237
    .priv_data_size = sizeof(H264Context),
 
4238
    .init           = ff_h264_decode_init,
 
4239
    .close          = ff_h264_decode_end,
 
4240
    .decode         = decode_frame,
 
4241
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
4205
4242
    .flush= flush_dpb,
4206
4243
    .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
4207
4244
    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_H264, PIX_FMT_NONE},