~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/cavs.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc, Andrew Starr-Bochicchio, Lionel Le Folgoc
  • Date: 2008-12-26 00:10:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081226001006-2040ls9680bd1blt
Tags: 1.1.7-0.2ubuntu1
[ Andrew Starr-Bochicchio ]
* Merge from debian-multimedia (LP: #298547), Ubuntu Changes:
 - For ffmpeg-related build-deps, fix versionized dependencies
   as the ubuntu versioning is different than debian-multimedia's.

[ Lionel Le Folgoc ]
* LP: #311412 is fixed since the 1.1.7~rc1-0.1 revision.
* debian/patches/03_ffmpeg.diff: updated to fix FTBFS due to libswscale API
  change (cherry-pick from Gentoo #234383).

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 *
17
17
 * You should have received a copy of the GNU Lesser General Public
18
18
 * License along with FFmpeg; if not, write to the Free Software
19
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
20
 */
21
21
 
22
22
/**
28
28
#include "avcodec.h"
29
29
#include "bitstream.h"
30
30
#include "golomb.h"
31
 
#include "mpegvideo.h"
 
31
#include "cavs.h"
32
32
#include "cavsdata.h"
33
33
 
34
 
#ifdef CONFIG_CAVS_DECODER
35
 
typedef struct {
36
 
    MpegEncContext s;
37
 
    Picture picture; ///< currently decoded frame
38
 
    Picture DPB[2];  ///< reference frames
39
 
    int dist[2];     ///< temporal distances from current frame to ref frames
40
 
    int profile, level;
41
 
    int aspect_ratio;
42
 
    int mb_width, mb_height;
43
 
    int pic_type;
44
 
    int progressive;
45
 
    int pic_structure;
46
 
    int skip_mode_flag; ///< select between skip_count or one skip_flag per MB
47
 
    int loop_filter_disable;
48
 
    int alpha_offset, beta_offset;
49
 
    int ref_flag;
50
 
    int mbx, mby;      ///< macroblock coordinates
51
 
    int flags;         ///< availability flags of neighbouring macroblocks
52
 
    int stc;           ///< last start code
53
 
    uint8_t *cy, *cu, *cv; ///< current MB sample pointers
54
 
    int left_qp;
55
 
    uint8_t *top_qp;
56
 
 
57
 
    /** mv motion vector cache
58
 
       0:    D3  B2  B3  C2
59
 
       4:    A1  X0  X1   -
60
 
       8:    A3  X2  X3   -
61
 
 
62
 
       X are the vectors in the current macroblock (5,6,9,10)
63
 
       A is the macroblock to the left (4,8)
64
 
       B is the macroblock to the top (1,2)
65
 
       C is the macroblock to the top-right (3)
66
 
       D is the macroblock to the top-left (0)
67
 
 
68
 
       the same is repeated for backward motion vectors */
69
 
    vector_t mv[2*4*3];
70
 
    vector_t *top_mv[2];
71
 
    vector_t *col_mv;
72
 
 
73
 
    /** luma pred mode cache
74
 
       0:    --  B2  B3
75
 
       3:    A1  X0  X1
76
 
       6:    A3  X2  X3   */
77
 
    int pred_mode_Y[3*3];
78
 
    int *top_pred_Y;
79
 
    int l_stride, c_stride;
80
 
    int luma_scan[4];
81
 
    int qp;
82
 
    int qp_fixed;
83
 
    int cbp;
84
 
    ScanTable scantable;
85
 
 
86
 
    /** intra prediction is done with un-deblocked samples
87
 
     they are saved here before deblocking the MB  */
88
 
    uint8_t *top_border_y, *top_border_u, *top_border_v;
89
 
    uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
90
 
    uint8_t intern_border_y[26];
91
 
    uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
92
 
 
93
 
    void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
94
 
    void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
95
 
    uint8_t *col_type_base;
96
 
    uint8_t *col_type;
97
 
 
98
 
    /* scaling factors for MV prediction */
99
 
    int sym_factor;    ///< for scaling in symmetrical B block
100
 
    int direct_den[2]; ///< for scaling in direct B block
101
 
    int scale_den[2];  ///< for scaling neighbouring MVs
102
 
 
103
 
    int got_keyframe;
104
 
    DCTELEM *block;
105
 
} AVSContext;
106
 
 
107
34
/*****************************************************************************
108
35
 *
109
36
 * in-loop deblocking filter
144
71
 * ---------
145
72
 *
146
73
 */
147
 
static void filter_mb(AVSContext *h, enum mb_t mb_type) {
 
74
void ff_cavs_filter(AVSContext *h, enum mb_t mb_type) {
148
75
    DECLARE_ALIGNED_8(uint8_t, bs[8]);
149
76
    int qp_avg, alpha, beta, tc;
150
77
    int i;
168
95
            *((uint64_t *)bs) = 0x0202020202020202ULL;
169
96
        else{
170
97
            *((uint64_t *)bs) = 0;
171
 
            if(partition_flags[mb_type] & SPLITV){
 
98
            if(ff_cavs_partition_flags[mb_type] & SPLITV){
172
99
                bs[2] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1], mb_type > P_8X8);
173
100
                bs[3] = get_bs(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3], mb_type > P_8X8);
174
101
            }
175
 
            if(partition_flags[mb_type] & SPLITH){
 
102
            if(ff_cavs_partition_flags[mb_type] & SPLITH){
176
103
                bs[6] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2], mb_type > P_8X8);
177
104
                bs[7] = get_bs(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3], mb_type > P_8X8);
178
105
            }
216
143
 *
217
144
 ****************************************************************************/
218
145
 
219
 
static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top,
 
146
void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top,
220
147
                                        uint8_t **left, int block) {
221
148
    int i;
222
149
 
266
193
    }
267
194
}
268
195
 
 
196
void ff_cavs_load_intra_pred_chroma(AVSContext *h) {
 
197
    /* extend borders by one pixel */
 
198
    h->left_border_u[9] = h->left_border_u[8];
 
199
    h->left_border_v[9] = h->left_border_v[8];
 
200
    h->top_border_u[h->mbx*10+9] = h->top_border_u[h->mbx*10+8];
 
201
    h->top_border_v[h->mbx*10+9] = h->top_border_v[h->mbx*10+8];
 
202
    if(h->mbx && h->mby) {
 
203
        h->top_border_u[h->mbx*10] = h->left_border_u[0] = h->topleft_border_u;
 
204
        h->top_border_v[h->mbx*10] = h->left_border_v[0] = h->topleft_border_v;
 
205
    } else {
 
206
        h->left_border_u[0] = h->left_border_u[1];
 
207
        h->left_border_v[0] = h->left_border_v[1];
 
208
        h->top_border_u[h->mbx*10] = h->top_border_u[h->mbx*10+1];
 
209
        h->top_border_v[h->mbx*10] = h->top_border_v[h->mbx*10+1];
 
210
    }
 
211
}
 
212
 
269
213
static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
270
214
    int y;
271
 
    uint64_t a = unaligned64(&top[1]);
 
215
    uint64_t a = AV_RN64(&top[1]);
272
216
    for(y=0;y<8;y++) {
273
217
        *((uint64_t *)(d+y*stride)) = a;
274
218
    }
353
297
 
354
298
#undef LOWPASS
355
299
 
356
 
static inline void modify_pred(const int_fast8_t *mod_table, int *mode) {
357
 
    *mode = mod_table[*mode];
358
 
    if(*mode < 0) {
359
 
        av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
360
 
        *mode = 0;
 
300
void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv) {
 
301
    /* save pred modes before they get modified */
 
302
    h->pred_mode_Y[3] =  h->pred_mode_Y[5];
 
303
    h->pred_mode_Y[6] =  h->pred_mode_Y[8];
 
304
    h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
 
305
    h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
 
306
 
 
307
    /* modify pred modes according to availability of neighbour samples */
 
308
    if(!(h->flags & A_AVAIL)) {
 
309
        modify_pred(ff_left_modifier_l, &h->pred_mode_Y[4] );
 
310
        modify_pred(ff_left_modifier_l, &h->pred_mode_Y[7] );
 
311
        modify_pred(ff_left_modifier_c, pred_mode_uv );
 
312
    }
 
313
    if(!(h->flags & B_AVAIL)) {
 
314
        modify_pred(ff_top_modifier_l, &h->pred_mode_Y[4] );
 
315
        modify_pred(ff_top_modifier_l, &h->pred_mode_Y[5] );
 
316
        modify_pred(ff_top_modifier_c, pred_mode_uv );
361
317
    }
362
318
}
363
319
 
454
410
    }
455
411
}
456
412
 
457
 
static void inter_pred(AVSContext *h, enum mb_t mb_type) {
458
 
    if(partition_flags[mb_type] == 0){ // 16x16
 
413
void ff_cavs_inter(AVSContext *h, enum mb_t mb_type) {
 
414
    if(ff_cavs_partition_flags[mb_type] == 0){ // 16x16
459
415
        mc_part_std(h, 1, 8, 0, h->cy, h->cu, h->cv, 0, 0,
460
416
                h->s.dsp.put_cavs_qpel_pixels_tab[0],
461
417
                h->s.dsp.put_h264_chroma_pixels_tab[0],
483
439
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
484
440
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
485
441
    }
486
 
    /* set intra prediction modes to default values */
487
 
    h->pred_mode_Y[3] =  h->pred_mode_Y[6] = INTRA_L_LP;
488
 
    h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
489
442
}
490
443
 
491
444
/*****************************************************************************
494
447
 *
495
448
 ****************************************************************************/
496
449
 
497
 
static inline void set_mvs(vector_t *mv, enum block_t size) {
498
 
    switch(size) {
499
 
    case BLK_16X16:
500
 
        mv[MV_STRIDE  ] = mv[0];
501
 
        mv[MV_STRIDE+1] = mv[0];
502
 
    case BLK_16X8:
503
 
        mv[1] = mv[0];
504
 
        break;
505
 
    case BLK_8X16:
506
 
        mv[MV_STRIDE] = mv[0];
507
 
        break;
508
 
    }
509
 
}
510
 
 
511
 
static inline void store_mvs(AVSContext *h) {
512
 
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0];
513
 
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1];
514
 
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2];
515
 
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3];
516
 
}
517
 
 
518
450
static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) {
519
451
    int den = h->scale_den[src->ref];
520
452
 
547
479
    }
548
480
}
549
481
 
550
 
static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw,
551
 
                                  vector_t *col_mv) {
552
 
    vector_t *pmv_bw = pmv_fw + MV_BWD_OFFS;
553
 
    int den = h->direct_den[col_mv->ref];
554
 
    int m = col_mv->x >> 31;
555
 
 
556
 
    pmv_fw->dist = h->dist[1];
557
 
    pmv_bw->dist = h->dist[0];
558
 
    pmv_fw->ref = 1;
559
 
    pmv_bw->ref = 0;
560
 
    /* scale the co-located motion vector according to its temporal span */
561
 
    pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
562
 
    pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
563
 
    m = col_mv->y >> 31;
564
 
    pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
565
 
    pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
566
 
}
567
 
 
568
 
static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) {
569
 
    vector_t *dst = src + MV_BWD_OFFS;
570
 
 
571
 
    /* backward mv is the scaled and negated forward mv */
572
 
    dst->x = -((src->x * h->sym_factor + 256) >> 9);
573
 
    dst->y = -((src->y * h->sym_factor + 256) >> 9);
574
 
    dst->ref = 0;
575
 
    dst->dist = h->dist[0];
576
 
    set_mvs(dst, size);
577
 
}
578
 
 
579
 
static void mv_pred(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
580
 
                    enum mv_pred_t mode, enum block_t size, int ref) {
 
482
void ff_cavs_mv(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
 
483
                enum mv_pred_t mode, enum block_t size, int ref) {
581
484
    vector_t *mvP = &h->mv[nP];
582
485
    vector_t *mvA = &h->mv[nP-1];
583
486
    vector_t *mvB = &h->mv[nP-4];
592
495
       ((mvA->ref == NOT_AVAIL) || (mvB->ref == NOT_AVAIL) ||
593
496
           ((mvA->x | mvA->y | mvA->ref) == 0)  ||
594
497
           ((mvB->x | mvB->y | mvB->ref) == 0) )) {
595
 
        mvP2 = &un_mv;
 
498
        mvP2 = &ff_cavs_un_mv;
596
499
    /* if there is only one suitable candidate, take it */
597
500
    } else if((mvA->ref >= 0) && (mvB->ref < 0) && (mvC->ref < 0)) {
598
501
        mvP2= mvA;
622
525
 
623
526
/*****************************************************************************
624
527
 *
625
 
 * residual data decoding
626
 
 *
627
 
 ****************************************************************************/
628
 
 
629
 
/** kth-order exponential golomb code */
630
 
static inline int get_ue_code(GetBitContext *gb, int order) {
631
 
    if(order) {
632
 
        int ret = get_ue_golomb(gb) << order;
633
 
        return ret + get_bits(gb,order);
634
 
    }
635
 
    return get_ue_golomb(gb);
636
 
}
637
 
 
638
 
/**
639
 
 * decode coefficients from one 8x8 block, dequantize, inverse transform
640
 
 *  and add them to sample block
641
 
 * @param r pointer to 2D VLC table
642
 
 * @param esc_golomb_order escape codes are k-golomb with this order k
643
 
 * @param qp quantizer
644
 
 * @param dst location of sample block
645
 
 * @param stride line stride in frame buffer
646
 
 */
647
 
static int decode_residual_block(AVSContext *h, GetBitContext *gb,
648
 
                                 const residual_vlc_t *r, int esc_golomb_order,
649
 
                                 int qp, uint8_t *dst, int stride) {
650
 
    int i,pos = -1;
651
 
    int level_code, esc_code, level, run, mask;
652
 
    int level_buf[64];
653
 
    int run_buf[64];
654
 
    int dqm = dequant_mul[qp];
655
 
    int dqs = dequant_shift[qp];
656
 
    int dqa = 1 << (dqs - 1);
657
 
    const uint8_t *scantab = h->scantable.permutated;
658
 
    DCTELEM *block = h->block;
659
 
 
660
 
    for(i=0;i<65;i++) {
661
 
        level_code = get_ue_code(gb,r->golomb_order);
662
 
        if(level_code >= ESCAPE_CODE) {
663
 
            run = ((level_code - ESCAPE_CODE) >> 1) + 1;
664
 
            esc_code = get_ue_code(gb,esc_golomb_order);
665
 
            level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
666
 
            while(level > r->inc_limit)
667
 
                r++;
668
 
            mask = -(level_code & 1);
669
 
            level = (level^mask) - mask;
670
 
        } else {
671
 
            level = r->rltab[level_code][0];
672
 
            if(!level) //end of block signal
673
 
                break;
674
 
            run   = r->rltab[level_code][1];
675
 
            r += r->rltab[level_code][2];
676
 
        }
677
 
        level_buf[i] = level;
678
 
        run_buf[i] = run;
679
 
    }
680
 
    /* inverse scan and dequantization */
681
 
    while(--i >= 0){
682
 
        pos += run_buf[i];
683
 
        if(pos > 63) {
684
 
            av_log(h->s.avctx, AV_LOG_ERROR,
685
 
                   "position out of block bounds at pic %d MB(%d,%d)\n",
686
 
                   h->picture.poc, h->mbx, h->mby);
687
 
            return -1;
688
 
        }
689
 
        block[scantab[pos]] = (level_buf[i]*dqm + dqa) >> dqs;
690
 
    }
691
 
    h->s.dsp.cavs_idct8_add(dst,block,stride);
692
 
    return 0;
693
 
}
694
 
 
695
 
 
696
 
static inline void decode_residual_chroma(AVSContext *h) {
697
 
    if(h->cbp & (1<<4))
698
 
        decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
699
 
                              h->cu,h->c_stride);
700
 
    if(h->cbp & (1<<5))
701
 
        decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
702
 
                              h->cv,h->c_stride);
703
 
}
704
 
 
705
 
static inline int decode_residual_inter(AVSContext *h) {
706
 
    int block;
707
 
 
708
 
    /* get coded block pattern */
709
 
    int cbp= get_ue_golomb(&h->s.gb);
710
 
    if(cbp > 63){
711
 
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
712
 
        return -1;
713
 
    }
714
 
    h->cbp = cbp_tab[cbp][1];
715
 
 
716
 
    /* get quantizer */
717
 
    if(h->cbp && !h->qp_fixed)
718
 
        h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
719
 
    for(block=0;block<4;block++)
720
 
        if(h->cbp & (1<<block))
721
 
            decode_residual_block(h,&h->s.gb,inter_2dvlc,0,h->qp,
722
 
                                  h->cy + h->luma_scan[block], h->l_stride);
723
 
    decode_residual_chroma(h);
724
 
 
725
 
    return 0;
726
 
}
727
 
 
728
 
/*****************************************************************************
729
 
 *
730
528
 * macroblock level
731
529
 *
732
530
 ****************************************************************************/
734
532
/**
735
533
 * initialise predictors for motion vectors and intra prediction
736
534
 */
737
 
static inline void init_mb(AVSContext *h) {
 
535
void ff_cavs_init_mb(AVSContext *h) {
738
536
    int i;
739
537
 
740
538
    /* copy predictors from top line (MB B and C) into cache */
746
544
    h->pred_mode_Y[2] = h->top_pred_Y[h->mbx*2+1];
747
545
    /* clear top predictors if MB B is not available */
748
546
    if(!(h->flags & B_AVAIL)) {
749
 
        h->mv[MV_FWD_B2] = un_mv;
750
 
        h->mv[MV_FWD_B3] = un_mv;
751
 
        h->mv[MV_BWD_B2] = un_mv;
752
 
        h->mv[MV_BWD_B3] = un_mv;
 
547
        h->mv[MV_FWD_B2] = ff_cavs_un_mv;
 
548
        h->mv[MV_FWD_B3] = ff_cavs_un_mv;
 
549
        h->mv[MV_BWD_B2] = ff_cavs_un_mv;
 
550
        h->mv[MV_BWD_B3] = ff_cavs_un_mv;
753
551
        h->pred_mode_Y[1] = h->pred_mode_Y[2] = NOT_AVAIL;
754
552
        h->flags &= ~(C_AVAIL|D_AVAIL);
755
553
    } else if(h->mbx) {
759
557
        h->flags &= ~C_AVAIL;
760
558
    /* clear top-right predictors if MB C is not available */
761
559
    if(!(h->flags & C_AVAIL)) {
762
 
        h->mv[MV_FWD_C2] = un_mv;
763
 
        h->mv[MV_BWD_C2] = un_mv;
 
560
        h->mv[MV_FWD_C2] = ff_cavs_un_mv;
 
561
        h->mv[MV_BWD_C2] = ff_cavs_un_mv;
764
562
    }
765
563
    /* clear top-left predictors if MB D is not available */
766
564
    if(!(h->flags & D_AVAIL)) {
767
 
        h->mv[MV_FWD_D3] = un_mv;
768
 
        h->mv[MV_BWD_D3] = un_mv;
 
565
        h->mv[MV_FWD_D3] = ff_cavs_un_mv;
 
566
        h->mv[MV_BWD_D3] = ff_cavs_un_mv;
769
567
    }
770
568
    /* set pointer for co-located macroblock type */
771
569
    h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx];
772
570
}
773
571
 
774
 
static inline void check_for_slice(AVSContext *h);
775
 
 
776
572
/**
777
573
 * save predictors for later macroblocks and increase
778
574
 * macroblock address
779
575
 * @returns 0 if end of frame is reached, 1 otherwise
780
576
 */
781
 
static inline int next_mb(AVSContext *h) {
 
577
int ff_cavs_next_mb(AVSContext *h) {
782
578
    int i;
783
579
 
784
580
    h->flags |= A_AVAIL;
801
597
        h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
802
598
        /* clear left mv predictors */
803
599
        for(i=0;i<=20;i+=4)
804
 
            h->mv[i] = un_mv;
 
600
            h->mv[i] = ff_cavs_un_mv;
805
601
        h->mbx = 0;
806
602
        h->mby++;
807
603
        /* re-calculate sample pointers */
817
613
    return 1;
818
614
}
819
615
 
820
 
static int decode_mb_i(AVSContext *h, int cbp_code) {
821
 
    GetBitContext *gb = &h->s.gb;
822
 
    int block, pred_mode_uv;
823
 
    uint8_t top[18];
824
 
    uint8_t *left = NULL;
825
 
    uint8_t *d;
826
 
 
827
 
    init_mb(h);
828
 
 
829
 
    /* get intra prediction modes from stream */
830
 
    for(block=0;block<4;block++) {
831
 
        int nA,nB,predpred;
832
 
        int pos = scan3x3[block];
833
 
 
834
 
        nA = h->pred_mode_Y[pos-1];
835
 
        nB = h->pred_mode_Y[pos-3];
836
 
        predpred = FFMIN(nA,nB);
837
 
        if(predpred == NOT_AVAIL) // if either is not available
838
 
            predpred = INTRA_L_LP;
839
 
        if(!get_bits1(gb)){
840
 
            int rem_mode= get_bits(gb, 2);
841
 
            predpred = rem_mode + (rem_mode >= predpred);
842
 
        }
843
 
        h->pred_mode_Y[pos] = predpred;
844
 
    }
845
 
    pred_mode_uv = get_ue_golomb(gb);
846
 
    if(pred_mode_uv > 6) {
847
 
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
848
 
        return -1;
849
 
    }
850
 
 
851
 
    /* save pred modes before they get modified */
852
 
    h->pred_mode_Y[3] =  h->pred_mode_Y[5];
853
 
    h->pred_mode_Y[6] =  h->pred_mode_Y[8];
854
 
    h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
855
 
    h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
856
 
 
857
 
    /* modify pred modes according to availability of neighbour samples */
858
 
    if(!(h->flags & A_AVAIL)) {
859
 
        modify_pred(left_modifier_l, &h->pred_mode_Y[4] );
860
 
        modify_pred(left_modifier_l, &h->pred_mode_Y[7] );
861
 
        modify_pred(left_modifier_c, &pred_mode_uv );
862
 
    }
863
 
    if(!(h->flags & B_AVAIL)) {
864
 
        modify_pred(top_modifier_l, &h->pred_mode_Y[4] );
865
 
        modify_pred(top_modifier_l, &h->pred_mode_Y[5] );
866
 
        modify_pred(top_modifier_c, &pred_mode_uv );
867
 
    }
868
 
 
869
 
    /* get coded block pattern */
870
 
    if(h->pic_type == FF_I_TYPE)
871
 
        cbp_code = get_ue_golomb(gb);
872
 
    if(cbp_code > 63){
873
 
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
874
 
        return -1;
875
 
    }
876
 
    h->cbp = cbp_tab[cbp_code][0];
877
 
    if(h->cbp && !h->qp_fixed)
878
 
        h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
879
 
 
880
 
    /* luma intra prediction interleaved with residual decode/transform/add */
881
 
    for(block=0;block<4;block++) {
882
 
        d = h->cy + h->luma_scan[block];
883
 
        load_intra_pred_luma(h, top, &left, block);
884
 
        h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
885
 
            (d, top, left, h->l_stride);
886
 
        if(h->cbp & (1<<block))
887
 
            decode_residual_block(h,gb,intra_2dvlc,1,h->qp,d,h->l_stride);
888
 
    }
889
 
 
890
 
    /* chroma intra prediction */
891
 
    /* extend borders by one pixel */
892
 
    h->left_border_u[9] = h->left_border_u[8];
893
 
    h->left_border_v[9] = h->left_border_v[8];
894
 
    h->top_border_u[h->mbx*10+9] = h->top_border_u[h->mbx*10+8];
895
 
    h->top_border_v[h->mbx*10+9] = h->top_border_v[h->mbx*10+8];
896
 
    if(h->mbx && h->mby) {
897
 
        h->top_border_u[h->mbx*10] = h->left_border_u[0] = h->topleft_border_u;
898
 
        h->top_border_v[h->mbx*10] = h->left_border_v[0] = h->topleft_border_v;
899
 
    } else {
900
 
        h->left_border_u[0] = h->left_border_u[1];
901
 
        h->left_border_v[0] = h->left_border_v[1];
902
 
        h->top_border_u[h->mbx*10] = h->top_border_u[h->mbx*10+1];
903
 
        h->top_border_v[h->mbx*10] = h->top_border_v[h->mbx*10+1];
904
 
    }
905
 
    h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
906
 
                                  h->left_border_u, h->c_stride);
907
 
    h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
908
 
                                  h->left_border_v, h->c_stride);
909
 
 
910
 
    decode_residual_chroma(h);
911
 
    filter_mb(h,I_8X8);
912
 
 
913
 
    /* mark motion vectors as intra */
914
 
    h->mv[MV_FWD_X0] = intra_mv;
915
 
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
916
 
    h->mv[MV_BWD_X0] = intra_mv;
917
 
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
918
 
    if(h->pic_type != FF_B_TYPE)
919
 
        *h->col_type = I_8X8;
920
 
 
921
 
    return 0;
922
 
}
923
 
 
924
 
static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
925
 
    GetBitContext *gb = &h->s.gb;
926
 
    int ref[4];
927
 
 
928
 
    init_mb(h);
929
 
    switch(mb_type) {
930
 
    case P_SKIP:
931
 
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
932
 
        break;
933
 
    case P_16X16:
934
 
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
935
 
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_16X16,ref[0]);
936
 
        break;
937
 
    case P_16X8:
938
 
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
939
 
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
940
 
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,      BLK_16X8, ref[0]);
941
 
        mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT,     BLK_16X8, ref[2]);
942
 
        break;
943
 
    case P_8X16:
944
 
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
945
 
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
946
 
        mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT,     BLK_8X16, ref[0]);
947
 
        mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
948
 
        break;
949
 
    case P_8X8:
950
 
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
951
 
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
952
 
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
953
 
        ref[3] = h->ref_flag ? 0 : get_bits1(gb);
954
 
        mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN,   BLK_8X8, ref[0]);
955
 
        mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_8X8, ref[1]);
956
 
        mv_pred(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN,   BLK_8X8, ref[2]);
957
 
        mv_pred(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN,   BLK_8X8, ref[3]);
958
 
    }
959
 
    inter_pred(h, mb_type);
960
 
    store_mvs(h);
961
 
    if(mb_type != P_SKIP)
962
 
        decode_residual_inter(h);
963
 
    filter_mb(h,mb_type);
964
 
    *h->col_type = mb_type;
965
 
}
966
 
 
967
 
static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
968
 
    int block;
969
 
    enum sub_mb_t sub_type[4];
970
 
    int flags;
971
 
 
972
 
    init_mb(h);
973
 
 
974
 
    /* reset all MVs */
975
 
    h->mv[MV_FWD_X0] = dir_mv;
976
 
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
977
 
    h->mv[MV_BWD_X0] = dir_mv;
978
 
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
979
 
    switch(mb_type) {
980
 
    case B_SKIP:
981
 
    case B_DIRECT:
982
 
        if(!(*h->col_type)) {
983
 
            /* intra MB at co-location, do in-plane prediction */
984
 
            mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
985
 
            mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
986
 
        } else
987
 
            /* direct prediction from co-located P MB, block-wise */
988
 
            for(block=0;block<4;block++)
989
 
                mv_pred_direct(h,&h->mv[mv_scan[block]],
990
 
                            &h->col_mv[(h->mby*h->mb_width+h->mbx)*4 + block]);
991
 
        break;
992
 
    case B_FWD_16X16:
993
 
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
994
 
        break;
995
 
    case B_SYM_16X16:
996
 
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
997
 
        mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
998
 
        break;
999
 
    case B_BWD_16X16:
1000
 
        mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
1001
 
        break;
1002
 
    case B_8X8:
1003
 
        for(block=0;block<4;block++)
1004
 
            sub_type[block] = get_bits(&h->s.gb,2);
1005
 
        for(block=0;block<4;block++) {
1006
 
            switch(sub_type[block]) {
1007
 
            case B_SUB_DIRECT:
1008
 
                if(!(*h->col_type)) {
1009
 
                    /* intra MB at co-location, do in-plane prediction */
1010
 
                    mv_pred(h, mv_scan[block], mv_scan[block]-3,
1011
 
                            MV_PRED_BSKIP, BLK_8X8, 1);
1012
 
                    mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
1013
 
                            mv_scan[block]-3+MV_BWD_OFFS,
1014
 
                            MV_PRED_BSKIP, BLK_8X8, 0);
1015
 
                } else
1016
 
                    mv_pred_direct(h,&h->mv[mv_scan[block]],
1017
 
                                   &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]);
1018
 
                break;
1019
 
            case B_SUB_FWD:
1020
 
                mv_pred(h, mv_scan[block], mv_scan[block]-3,
1021
 
                        MV_PRED_MEDIAN, BLK_8X8, 1);
1022
 
                break;
1023
 
            case B_SUB_SYM:
1024
 
                mv_pred(h, mv_scan[block], mv_scan[block]-3,
1025
 
                        MV_PRED_MEDIAN, BLK_8X8, 1);
1026
 
                mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
1027
 
                break;
1028
 
            }
1029
 
        }
1030
 
        for(block=0;block<4;block++) {
1031
 
            if(sub_type[block] == B_SUB_BWD)
1032
 
                mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
1033
 
                        mv_scan[block]+MV_BWD_OFFS-3,
1034
 
                        MV_PRED_MEDIAN, BLK_8X8, 0);
1035
 
        }
1036
 
        break;
1037
 
    default:
1038
 
        assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
1039
 
        flags = partition_flags[mb_type];
1040
 
        if(mb_type & 1) { /* 16x8 macroblock types */
1041
 
            if(flags & FWD0)
1042
 
                mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,  BLK_16X8, 1);
1043
 
            if(flags & SYM0)
1044
 
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
1045
 
            if(flags & FWD1)
1046
 
                mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
1047
 
            if(flags & SYM1)
1048
 
                mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
1049
 
            if(flags & BWD0)
1050
 
                mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP,  BLK_16X8, 0);
1051
 
            if(flags & BWD1)
1052
 
                mv_pred(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
1053
 
        } else {          /* 8x16 macroblock types */
1054
 
            if(flags & FWD0)
1055
 
                mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
1056
 
            if(flags & SYM0)
1057
 
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
1058
 
            if(flags & FWD1)
1059
 
                mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
1060
 
            if(flags & SYM1)
1061
 
                mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
1062
 
            if(flags & BWD0)
1063
 
                mv_pred(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
1064
 
            if(flags & BWD1)
1065
 
                mv_pred(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 0);
1066
 
        }
1067
 
    }
1068
 
    inter_pred(h, mb_type);
1069
 
    if(mb_type != B_SKIP)
1070
 
        decode_residual_inter(h);
1071
 
    filter_mb(h,mb_type);
1072
 
}
1073
 
 
1074
 
/*****************************************************************************
1075
 
 *
1076
 
 * slice level
1077
 
 *
1078
 
 ****************************************************************************/
1079
 
 
1080
 
static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
1081
 
    if(h->stc > 0xAF)
1082
 
        av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
1083
 
    h->mby = h->stc;
1084
 
    if((h->mby == 0) && (!h->qp_fixed)){
1085
 
        h->qp_fixed = get_bits1(gb);
1086
 
        h->qp = get_bits(gb,6);
1087
 
    }
1088
 
    /* inter frame or second slice can have weighting params */
1089
 
    if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
1090
 
        if(get_bits1(gb)) { //slice_weighting_flag
1091
 
            av_log(h->s.avctx, AV_LOG_ERROR,
1092
 
                   "weighted prediction not yet supported\n");
1093
 
        }
1094
 
    return 0;
1095
 
}
1096
 
 
1097
 
static inline void check_for_slice(AVSContext *h) {
1098
 
    GetBitContext *gb = &h->s.gb;
1099
 
    int align;
1100
 
    align = (-get_bits_count(gb)) & 7;
1101
 
    if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
1102
 
        get_bits_long(gb,24+align);
1103
 
        h->stc = get_bits(gb,8);
1104
 
        decode_slice_header(h,gb);
1105
 
    }
1106
 
}
1107
 
 
1108
616
/*****************************************************************************
1109
617
 *
1110
618
 * frame level
1111
619
 *
1112
620
 ****************************************************************************/
1113
621
 
1114
 
static void init_pic(AVSContext *h) {
 
622
void ff_cavs_init_pic(AVSContext *h) {
1115
623
    int i;
1116
624
 
1117
625
    /* clear some predictors */
1118
626
    for(i=0;i<=20;i+=4)
1119
 
        h->mv[i] = un_mv;
1120
 
    h->mv[MV_BWD_X0] = dir_mv;
 
627
        h->mv[i] = ff_cavs_un_mv;
 
628
    h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
1121
629
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
1122
 
    h->mv[MV_FWD_X0] = dir_mv;
 
630
    h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
1123
631
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
1124
632
    h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
1125
633
    h->cy = h->picture.data[0];
1133
641
    h->flags = 0;
1134
642
}
1135
643
 
1136
 
static int decode_pic(AVSContext *h) {
1137
 
    MpegEncContext *s = &h->s;
1138
 
    int skip_count;
1139
 
    enum mb_t mb_type;
1140
 
 
1141
 
    if (!s->context_initialized) {
1142
 
        s->avctx->idct_algo = FF_IDCT_CAVS;
1143
 
        if (MPV_common_init(s) < 0)
1144
 
            return -1;
1145
 
        ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
1146
 
    }
1147
 
    get_bits(&s->gb,16);//bbv_dwlay
1148
 
    if(h->stc == PIC_PB_START_CODE) {
1149
 
        h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
1150
 
        if(h->pic_type > FF_B_TYPE) {
1151
 
            av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
1152
 
            return -1;
1153
 
        }
1154
 
        /* make sure we have the reference frames we need */
1155
 
        if(!h->DPB[0].data[0] ||
1156
 
          (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
1157
 
            return -1;
1158
 
    } else {
1159
 
        h->pic_type = FF_I_TYPE;
1160
 
        if(get_bits1(&s->gb))
1161
 
            get_bits(&s->gb,16);//time_code
1162
 
    }
1163
 
    /* release last B frame */
1164
 
    if(h->picture.data[0])
1165
 
        s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
1166
 
 
1167
 
    s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
1168
 
    init_pic(h);
1169
 
    h->picture.poc = get_bits(&s->gb,8)*2;
1170
 
 
1171
 
    /* get temporal distances and MV scaling factors */
1172
 
    if(h->pic_type != FF_B_TYPE) {
1173
 
        h->dist[0] = (h->picture.poc - h->DPB[0].poc  + 512) % 512;
1174
 
    } else {
1175
 
        h->dist[0] = (h->DPB[0].poc  - h->picture.poc + 512) % 512;
1176
 
    }
1177
 
    h->dist[1] = (h->picture.poc - h->DPB[1].poc  + 512) % 512;
1178
 
    h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1179
 
    h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1180
 
    if(h->pic_type == FF_B_TYPE) {
1181
 
        h->sym_factor = h->dist[0]*h->scale_den[1];
1182
 
    } else {
1183
 
        h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
1184
 
        h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
1185
 
    }
1186
 
 
1187
 
    if(s->low_delay)
1188
 
        get_ue_golomb(&s->gb); //bbv_check_times
1189
 
    h->progressive             = get_bits1(&s->gb);
1190
 
    if(h->progressive)
1191
 
        h->pic_structure = 1;
1192
 
    else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) )
1193
 
        get_bits1(&s->gb);     //advanced_pred_mode_disable
1194
 
    skip_bits1(&s->gb);        //top_field_first
1195
 
    skip_bits1(&s->gb);        //repeat_first_field
1196
 
    h->qp_fixed                = get_bits1(&s->gb);
1197
 
    h->qp                      = get_bits(&s->gb,6);
1198
 
    if(h->pic_type == FF_I_TYPE) {
1199
 
        if(!h->progressive && !h->pic_structure)
1200
 
            skip_bits1(&s->gb);//what is this?
1201
 
        skip_bits(&s->gb,4);   //reserved bits
1202
 
    } else {
1203
 
        if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
1204
 
            h->ref_flag        = get_bits1(&s->gb);
1205
 
        skip_bits(&s->gb,4);   //reserved bits
1206
 
        h->skip_mode_flag      = get_bits1(&s->gb);
1207
 
    }
1208
 
    h->loop_filter_disable     = get_bits1(&s->gb);
1209
 
    if(!h->loop_filter_disable && get_bits1(&s->gb)) {
1210
 
        h->alpha_offset        = get_se_golomb(&s->gb);
1211
 
        h->beta_offset         = get_se_golomb(&s->gb);
1212
 
    } else {
1213
 
        h->alpha_offset = h->beta_offset  = 0;
1214
 
    }
1215
 
    check_for_slice(h);
1216
 
    if(h->pic_type == FF_I_TYPE) {
1217
 
        do {
1218
 
            decode_mb_i(h, 0);
1219
 
        } while(next_mb(h));
1220
 
    } else if(h->pic_type == FF_P_TYPE) {
1221
 
        do {
1222
 
            if(h->skip_mode_flag) {
1223
 
                skip_count = get_ue_golomb(&s->gb);
1224
 
                while(skip_count--) {
1225
 
                    decode_mb_p(h,P_SKIP);
1226
 
                    if(!next_mb(h))
1227
 
                        goto done;
1228
 
                }
1229
 
                mb_type = get_ue_golomb(&s->gb) + P_16X16;
1230
 
            } else
1231
 
                mb_type = get_ue_golomb(&s->gb) + P_SKIP;
1232
 
            if(mb_type > P_8X8) {
1233
 
                decode_mb_i(h, mb_type - P_8X8 - 1);
1234
 
            } else
1235
 
                decode_mb_p(h,mb_type);
1236
 
        } while(next_mb(h));
1237
 
    } else { /* FF_B_TYPE */
1238
 
        do {
1239
 
            if(h->skip_mode_flag) {
1240
 
                skip_count = get_ue_golomb(&s->gb);
1241
 
                while(skip_count--) {
1242
 
                    decode_mb_b(h,B_SKIP);
1243
 
                    if(!next_mb(h))
1244
 
                        goto done;
1245
 
                }
1246
 
                mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
1247
 
            } else
1248
 
                mb_type = get_ue_golomb(&s->gb) + B_SKIP;
1249
 
            if(mb_type > B_8X8) {
1250
 
                decode_mb_i(h, mb_type - B_8X8 - 1);
1251
 
            } else
1252
 
                decode_mb_b(h,mb_type);
1253
 
        } while(next_mb(h));
1254
 
    }
1255
 
 done:
1256
 
    if(h->pic_type != FF_B_TYPE) {
1257
 
        if(h->DPB[1].data[0])
1258
 
            s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
1259
 
        memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture));
1260
 
        memcpy(&h->DPB[0], &h->picture, sizeof(Picture));
1261
 
        memset(&h->picture,0,sizeof(Picture));
1262
 
    }
1263
 
    return 0;
1264
 
}
1265
 
 
1266
644
/*****************************************************************************
1267
645
 *
1268
646
 * headers and interface
1274
652
 * this data has to be stored for one complete row of macroblocks
1275
653
 * and this storage space is allocated here
1276
654
 */
1277
 
static void init_top_lines(AVSContext *h) {
 
655
void ff_cavs_init_top_lines(AVSContext *h) {
1278
656
    /* alloc top line of predictors */
1279
657
    h->top_qp       = av_malloc( h->mb_width);
1280
658
    h->top_mv[0]    = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
1290
668
    h->block        = av_mallocz(64*sizeof(DCTELEM));
1291
669
}
1292
670
 
1293
 
static int decode_seq_header(AVSContext *h) {
1294
 
    MpegEncContext *s = &h->s;
1295
 
    extern const AVRational ff_frame_rate_tab[];
1296
 
    int frame_rate_code;
1297
 
 
1298
 
    h->profile =         get_bits(&s->gb,8);
1299
 
    h->level =           get_bits(&s->gb,8);
1300
 
    skip_bits1(&s->gb); //progressive sequence
1301
 
    s->width =           get_bits(&s->gb,14);
1302
 
    s->height =          get_bits(&s->gb,14);
1303
 
    skip_bits(&s->gb,2); //chroma format
1304
 
    skip_bits(&s->gb,3); //sample_precision
1305
 
    h->aspect_ratio =    get_bits(&s->gb,4);
1306
 
    frame_rate_code =    get_bits(&s->gb,4);
1307
 
    skip_bits(&s->gb,18);//bit_rate_lower
1308
 
    skip_bits1(&s->gb);  //marker_bit
1309
 
    skip_bits(&s->gb,12);//bit_rate_upper
1310
 
    s->low_delay =       get_bits1(&s->gb);
1311
 
    h->mb_width  = (s->width  + 15) >> 4;
1312
 
    h->mb_height = (s->height + 15) >> 4;
1313
 
    h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
1314
 
    h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
1315
 
    h->s.avctx->width  = s->width;
1316
 
    h->s.avctx->height = s->height;
1317
 
    if(!h->top_qp)
1318
 
        init_top_lines(h);
1319
 
    return 0;
1320
 
}
1321
 
 
1322
 
static void cavs_flush(AVCodecContext * avctx) {
1323
 
    AVSContext *h = avctx->priv_data;
1324
 
    h->got_keyframe = 0;
1325
 
}
1326
 
 
1327
 
static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
1328
 
                             uint8_t * buf, int buf_size) {
1329
 
    AVSContext *h = avctx->priv_data;
1330
 
    MpegEncContext *s = &h->s;
1331
 
    int input_size;
1332
 
    const uint8_t *buf_end;
1333
 
    const uint8_t *buf_ptr;
1334
 
    AVFrame *picture = data;
1335
 
    uint32_t stc;
1336
 
 
1337
 
    s->avctx = avctx;
1338
 
 
1339
 
    if (buf_size == 0) {
1340
 
        if(!s->low_delay && h->DPB[0].data[0]) {
1341
 
            *data_size = sizeof(AVPicture);
1342
 
            *picture = *(AVFrame *) &h->DPB[0];
1343
 
        }
1344
 
        return 0;
1345
 
    }
1346
 
 
1347
 
    buf_ptr = buf;
1348
 
    buf_end = buf + buf_size;
1349
 
    for(;;) {
1350
 
        buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
1351
 
        if(stc & 0xFFFFFE00)
1352
 
            return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
1353
 
        input_size = (buf_end - buf_ptr)*8;
1354
 
        switch(stc) {
1355
 
        case SEQ_START_CODE:
1356
 
            init_get_bits(&s->gb, buf_ptr, input_size);
1357
 
            decode_seq_header(h);
1358
 
            break;
1359
 
        case PIC_I_START_CODE:
1360
 
            if(!h->got_keyframe) {
1361
 
                if(h->DPB[0].data[0])
1362
 
                    avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
1363
 
                if(h->DPB[1].data[0])
1364
 
                    avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
1365
 
                h->got_keyframe = 1;
1366
 
            }
1367
 
        case PIC_PB_START_CODE:
1368
 
            *data_size = 0;
1369
 
            if(!h->got_keyframe)
1370
 
                break;
1371
 
            init_get_bits(&s->gb, buf_ptr, input_size);
1372
 
            h->stc = stc;
1373
 
            if(decode_pic(h))
1374
 
                break;
1375
 
            *data_size = sizeof(AVPicture);
1376
 
            if(h->pic_type != FF_B_TYPE) {
1377
 
                if(h->DPB[1].data[0]) {
1378
 
                    *picture = *(AVFrame *) &h->DPB[1];
1379
 
                } else {
1380
 
                    *data_size = 0;
1381
 
                }
1382
 
            } else
1383
 
                *picture = *(AVFrame *) &h->picture;
1384
 
            break;
1385
 
        case EXT_START_CODE:
1386
 
            //mpeg_decode_extension(avctx,buf_ptr, input_size);
1387
 
            break;
1388
 
        case USER_START_CODE:
1389
 
            //mpeg_decode_user_data(avctx,buf_ptr, input_size);
1390
 
            break;
1391
 
        default:
1392
 
            if (stc >= SLICE_MIN_START_CODE &&
1393
 
                stc <= SLICE_MAX_START_CODE) {
1394
 
                init_get_bits(&s->gb, buf_ptr, input_size);
1395
 
                decode_slice_header(h, &s->gb);
1396
 
            }
1397
 
            break;
1398
 
        }
1399
 
    }
1400
 
}
1401
 
 
1402
 
static int cavs_decode_init(AVCodecContext * avctx) {
 
671
av_cold int ff_cavs_init(AVCodecContext *avctx) {
1403
672
    AVSContext *h = avctx->priv_data;
1404
673
    MpegEncContext * const s = &h->s;
1405
674
 
1425
694
    h->intra_pred_c[   INTRA_C_LP_LEFT] = intra_pred_lp_left;
1426
695
    h->intra_pred_c[    INTRA_C_LP_TOP] = intra_pred_lp_top;
1427
696
    h->intra_pred_c[    INTRA_C_DC_128] = intra_pred_dc_128;
1428
 
    h->mv[ 7] = un_mv;
1429
 
    h->mv[19] = un_mv;
 
697
    h->mv[ 7] = ff_cavs_un_mv;
 
698
    h->mv[19] = ff_cavs_un_mv;
1430
699
    return 0;
1431
700
}
1432
701
 
1433
 
static int cavs_decode_end(AVCodecContext * avctx) {
 
702
av_cold int ff_cavs_end(AVCodecContext *avctx) {
1434
703
    AVSContext *h = avctx->priv_data;
1435
704
 
1436
705
    av_free(h->top_qp);
1445
714
    av_free(h->block);
1446
715
    return 0;
1447
716
}
1448
 
 
1449
 
AVCodec cavs_decoder = {
1450
 
    "cavs",
1451
 
    CODEC_TYPE_VIDEO,
1452
 
    CODEC_ID_CAVS,
1453
 
    sizeof(AVSContext),
1454
 
    cavs_decode_init,
1455
 
    NULL,
1456
 
    cavs_decode_end,
1457
 
    cavs_decode_frame,
1458
 
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1459
 
    .flush= cavs_flush,
1460
 
};
1461
 
#endif /* CONFIG_CAVS_DECODER */
1462
 
 
1463
 
#ifdef CONFIG_CAVSVIDEO_PARSER
1464
 
/**
1465
 
 * finds the end of the current frame in the bitstream.
1466
 
 * @return the position of the first byte of the next frame, or -1
1467
 
 */
1468
 
static int cavs_find_frame_end(ParseContext *pc, const uint8_t *buf,
1469
 
                               int buf_size) {
1470
 
    int pic_found, i;
1471
 
    uint32_t state;
1472
 
 
1473
 
    pic_found= pc->frame_start_found;
1474
 
    state= pc->state;
1475
 
 
1476
 
    i=0;
1477
 
    if(!pic_found){
1478
 
        for(i=0; i<buf_size; i++){
1479
 
            state= (state<<8) | buf[i];
1480
 
            if(state == PIC_I_START_CODE || state == PIC_PB_START_CODE){
1481
 
                i++;
1482
 
                pic_found=1;
1483
 
                break;
1484
 
            }
1485
 
        }
1486
 
    }
1487
 
 
1488
 
    if(pic_found){
1489
 
        /* EOF considered as end of frame */
1490
 
        if (buf_size == 0)
1491
 
            return 0;
1492
 
        for(; i<buf_size; i++){
1493
 
            state= (state<<8) | buf[i];
1494
 
            if((state&0xFFFFFF00) == 0x100){
1495
 
                if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
1496
 
                    pc->frame_start_found=0;
1497
 
                    pc->state=-1;
1498
 
                    return i-3;
1499
 
                }
1500
 
            }
1501
 
        }
1502
 
    }
1503
 
    pc->frame_start_found= pic_found;
1504
 
    pc->state= state;
1505
 
    return END_NOT_FOUND;
1506
 
}
1507
 
 
1508
 
static int cavsvideo_parse(AVCodecParserContext *s,
1509
 
                           AVCodecContext *avctx,
1510
 
                           uint8_t **poutbuf, int *poutbuf_size,
1511
 
                           const uint8_t *buf, int buf_size)
1512
 
{
1513
 
    ParseContext *pc = s->priv_data;
1514
 
    int next;
1515
 
 
1516
 
    if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
1517
 
        next= buf_size;
1518
 
    }else{
1519
 
        next= cavs_find_frame_end(pc, buf, buf_size);
1520
 
 
1521
 
        if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
1522
 
            *poutbuf = NULL;
1523
 
            *poutbuf_size = 0;
1524
 
            return buf_size;
1525
 
        }
1526
 
    }
1527
 
    *poutbuf = (uint8_t *)buf;
1528
 
    *poutbuf_size = buf_size;
1529
 
    return next;
1530
 
}
1531
 
 
1532
 
AVCodecParser cavsvideo_parser = {
1533
 
    { CODEC_ID_CAVS },
1534
 
    sizeof(ParseContext1),
1535
 
    NULL,
1536
 
    cavsvideo_parse,
1537
 
    ff_parse1_close,
1538
 
    ff_mpeg4video_split,
1539
 
};
1540
 
#endif /* CONFIG_CAVSVIDEO_PARSER */