~ubuntu-dev/mplayer/ubuntu-feisty

« back to all changes in this revision

Viewing changes to libavcodec/cavs.c

  • Committer: William Grant
  • Date: 2007-02-03 03:16:07 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: william.grant@ubuntu.org.au-20070203031607-08gc2ompbz6spt9i
Update to 1.0rc1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
 
3
 * Copyright (c) 2006  Stefan Gehrer <stefan.gehrer@gmx.de>
 
4
 *
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 *
 
12
 * FFmpeg is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
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
 
20
 */
 
21
 
 
22
/**
 
23
 * @file cavs.c
 
24
 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
 
25
 * @author Stefan Gehrer <stefan.gehrer@gmx.de>
 
26
 */
 
27
 
 
28
#include "avcodec.h"
 
29
#include "bitstream.h"
 
30
#include "golomb.h"
 
31
#include "mpegvideo.h"
 
32
#include "cavsdata.h"
 
33
 
 
34
typedef struct {
 
35
    MpegEncContext s;
 
36
    Picture picture; ///< currently decoded frame
 
37
    Picture DPB[2];  ///< reference frames
 
38
    int dist[2];     ///< temporal distances from current frame to ref frames
 
39
    int profile, level;
 
40
    int aspect_ratio;
 
41
    int mb_width, mb_height;
 
42
    int pic_type;
 
43
    int progressive;
 
44
    int pic_structure;
 
45
    int skip_mode_flag; ///< select between skip_count or one skip_flag per MB
 
46
    int loop_filter_disable;
 
47
    int alpha_offset, beta_offset;
 
48
    int ref_flag;
 
49
    int mbx, mby;      ///< macroblock coordinates
 
50
    int flags;         ///< availability flags of neighbouring macroblocks
 
51
    int stc;           ///< last start code
 
52
    uint8_t *cy, *cu, *cv; ///< current MB sample pointers
 
53
    int left_qp;
 
54
    uint8_t *top_qp;
 
55
 
 
56
    /** mv motion vector cache
 
57
       0:    D3  B2  B3  C2
 
58
       4:    A1  X0  X1   -
 
59
       8:    A3  X2  X3   -
 
60
 
 
61
       X are the vectors in the current macroblock (5,6,9,10)
 
62
       A is the macroblock to the left (4,8)
 
63
       B is the macroblock to the top (1,2)
 
64
       C is the macroblock to the top-right (3)
 
65
       D is the macroblock to the top-left (0)
 
66
 
 
67
       the same is repeated for backward motion vectors */
 
68
    vector_t mv[2*4*3];
 
69
    vector_t *top_mv[2];
 
70
    vector_t *col_mv;
 
71
 
 
72
    /** luma pred mode cache
 
73
       0:    --  B2  B3
 
74
       3:    A1  X0  X1
 
75
       6:    A3  X2  X3   */
 
76
    int pred_mode_Y[3*3];
 
77
    int *top_pred_Y;
 
78
    int l_stride, c_stride;
 
79
    int luma_scan[4];
 
80
    int qp;
 
81
    int qp_fixed;
 
82
    int cbp;
 
83
    ScanTable scantable;
 
84
 
 
85
    /** intra prediction is done with un-deblocked samples
 
86
     they are saved here before deblocking the MB  */
 
87
    uint8_t *top_border_y, *top_border_u, *top_border_v;
 
88
    uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
 
89
    uint8_t intern_border_y[26];
 
90
    uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
 
91
 
 
92
    void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
 
93
    void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
 
94
    uint8_t *col_type_base;
 
95
    uint8_t *col_type;
 
96
 
 
97
    /* scaling factors for MV prediction */
 
98
    int sym_factor;    ///< for scaling in symmetrical B block
 
99
    int direct_den[2]; ///< for scaling in direct B block
 
100
    int scale_den[2];  ///< for scaling neighbouring MVs
 
101
 
 
102
    int got_keyframe;
 
103
    DCTELEM *block;
 
104
} AVSContext;
 
105
 
 
106
/*****************************************************************************
 
107
 *
 
108
 * in-loop deblocking filter
 
109
 *
 
110
 ****************************************************************************/
 
111
 
 
112
static inline int get_bs(vector_t *mvP, vector_t *mvQ, int b) {
 
113
    if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA))
 
114
        return 2;
 
115
    if( (abs(mvP->x - mvQ->x) >= 4) ||  (abs(mvP->y - mvQ->y) >= 4) )
 
116
        return 1;
 
117
    if(b){
 
118
        mvP += MV_BWD_OFFS;
 
119
        mvQ += MV_BWD_OFFS;
 
120
        if( (abs(mvP->x - mvQ->x) >= 4) ||  (abs(mvP->y - mvQ->y) >= 4) )
 
121
            return 1;
 
122
    }else{
 
123
        if(mvP->ref != mvQ->ref)
 
124
            return 1;
 
125
    }
 
126
    return 0;
 
127
}
 
128
 
 
129
#define SET_PARAMS                                            \
 
130
    alpha = alpha_tab[clip(qp_avg + h->alpha_offset,0,63)];   \
 
131
    beta  =  beta_tab[clip(qp_avg + h->beta_offset, 0,63)];   \
 
132
    tc    =    tc_tab[clip(qp_avg + h->alpha_offset,0,63)];
 
133
 
 
134
/**
 
135
 * in-loop deblocking filter for a single macroblock
 
136
 *
 
137
 * boundary strength (bs) mapping:
 
138
 *
 
139
 * --4---5--
 
140
 * 0   2   |
 
141
 * | 6 | 7 |
 
142
 * 1   3   |
 
143
 * ---------
 
144
 *
 
145
 */
 
146
static void filter_mb(AVSContext *h, enum mb_t mb_type) {
 
147
    DECLARE_ALIGNED_8(uint8_t, bs[8]);
 
148
    int qp_avg, alpha, beta, tc;
 
149
    int i;
 
150
 
 
151
    /* save un-deblocked lines */
 
152
    h->topleft_border_y = h->top_border_y[h->mbx*16+15];
 
153
    h->topleft_border_u = h->top_border_u[h->mbx*10+8];
 
154
    h->topleft_border_v = h->top_border_v[h->mbx*10+8];
 
155
    memcpy(&h->top_border_y[h->mbx*16], h->cy + 15* h->l_stride,16);
 
156
    memcpy(&h->top_border_u[h->mbx*10+1], h->cu +  7* h->c_stride,8);
 
157
    memcpy(&h->top_border_v[h->mbx*10+1], h->cv +  7* h->c_stride,8);
 
158
    for(i=0;i<8;i++) {
 
159
        h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+0)*h->l_stride);
 
160
        h->left_border_y[i*2+2] = *(h->cy + 15 + (i*2+1)*h->l_stride);
 
161
        h->left_border_u[i+1] = *(h->cu + 7 + i*h->c_stride);
 
162
        h->left_border_v[i+1] = *(h->cv + 7 + i*h->c_stride);
 
163
    }
 
164
    if(!h->loop_filter_disable) {
 
165
        /* determine bs */
 
166
        if(mb_type == I_8X8)
 
167
            *((uint64_t *)bs) = 0x0202020202020202ULL;
 
168
        else{
 
169
            *((uint64_t *)bs) = 0;
 
170
            if(partition_flags[mb_type] & SPLITV){
 
171
                bs[2] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1], mb_type > P_8X8);
 
172
                bs[3] = get_bs(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3], mb_type > P_8X8);
 
173
            }
 
174
            if(partition_flags[mb_type] & SPLITH){
 
175
                bs[6] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2], mb_type > P_8X8);
 
176
                bs[7] = get_bs(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3], mb_type > P_8X8);
 
177
            }
 
178
            bs[0] = get_bs(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0], mb_type > P_8X8);
 
179
            bs[1] = get_bs(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2], mb_type > P_8X8);
 
180
            bs[4] = get_bs(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0], mb_type > P_8X8);
 
181
            bs[5] = get_bs(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1], mb_type > P_8X8);
 
182
        }
 
183
        if( *((uint64_t *)bs) ) {
 
184
            if(h->flags & A_AVAIL) {
 
185
                qp_avg = (h->qp + h->left_qp + 1) >> 1;
 
186
                SET_PARAMS;
 
187
                h->s.dsp.cavs_filter_lv(h->cy,h->l_stride,alpha,beta,tc,bs[0],bs[1]);
 
188
                h->s.dsp.cavs_filter_cv(h->cu,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
 
189
                h->s.dsp.cavs_filter_cv(h->cv,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
 
190
            }
 
191
            qp_avg = h->qp;
 
192
            SET_PARAMS;
 
193
            h->s.dsp.cavs_filter_lv(h->cy + 8,h->l_stride,alpha,beta,tc,bs[2],bs[3]);
 
194
            h->s.dsp.cavs_filter_lh(h->cy + 8*h->l_stride,h->l_stride,alpha,beta,tc,
 
195
                           bs[6],bs[7]);
 
196
 
 
197
            if(h->flags & B_AVAIL) {
 
198
                qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1;
 
199
                SET_PARAMS;
 
200
                h->s.dsp.cavs_filter_lh(h->cy,h->l_stride,alpha,beta,tc,bs[4],bs[5]);
 
201
                h->s.dsp.cavs_filter_ch(h->cu,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
 
202
                h->s.dsp.cavs_filter_ch(h->cv,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
 
203
            }
 
204
        }
 
205
    }
 
206
    h->left_qp = h->qp;
 
207
    h->top_qp[h->mbx] = h->qp;
 
208
}
 
209
 
 
210
#undef SET_PARAMS
 
211
 
 
212
/*****************************************************************************
 
213
 *
 
214
 * spatial intra prediction
 
215
 *
 
216
 ****************************************************************************/
 
217
 
 
218
static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top,
 
219
                                        uint8_t **left, int block) {
 
220
    int i;
 
221
 
 
222
    switch(block) {
 
223
    case 0:
 
224
        *left = h->left_border_y;
 
225
        h->left_border_y[0] = h->left_border_y[1];
 
226
        memset(&h->left_border_y[17],h->left_border_y[16],9);
 
227
        memcpy(&top[1],&h->top_border_y[h->mbx*16],16);
 
228
        top[17] = top[16];
 
229
        top[0] = top[1];
 
230
        if((h->flags & A_AVAIL) && (h->flags & B_AVAIL))
 
231
            h->left_border_y[0] = top[0] = h->topleft_border_y;
 
232
        break;
 
233
    case 1:
 
234
        *left = h->intern_border_y;
 
235
        for(i=0;i<8;i++)
 
236
            h->intern_border_y[i+1] = *(h->cy + 7 + i*h->l_stride);
 
237
        memset(&h->intern_border_y[9],h->intern_border_y[8],9);
 
238
        h->intern_border_y[0] = h->intern_border_y[1];
 
239
        memcpy(&top[1],&h->top_border_y[h->mbx*16+8],8);
 
240
        if(h->flags & C_AVAIL)
 
241
            memcpy(&top[9],&h->top_border_y[(h->mbx + 1)*16],8);
 
242
        else
 
243
            memset(&top[9],top[8],9);
 
244
        top[17] = top[16];
 
245
        top[0] = top[1];
 
246
        if(h->flags & B_AVAIL)
 
247
            h->intern_border_y[0] = top[0] = h->top_border_y[h->mbx*16+7];
 
248
        break;
 
249
    case 2:
 
250
        *left = &h->left_border_y[8];
 
251
        memcpy(&top[1],h->cy + 7*h->l_stride,16);
 
252
        top[17] = top[16];
 
253
        top[0] = top[1];
 
254
        if(h->flags & A_AVAIL)
 
255
            top[0] = h->left_border_y[8];
 
256
        break;
 
257
    case 3:
 
258
        *left = &h->intern_border_y[8];
 
259
        for(i=0;i<8;i++)
 
260
            h->intern_border_y[i+9] = *(h->cy + 7 + (i+8)*h->l_stride);
 
261
        memset(&h->intern_border_y[17],h->intern_border_y[16],9);
 
262
        memcpy(&top[0],h->cy + 7 + 7*h->l_stride,9);
 
263
        memset(&top[9],top[8],9);
 
264
        break;
 
265
    }
 
266
}
 
267
 
 
268
static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
 
269
    int y;
 
270
    uint64_t a = unaligned64(&top[1]);
 
271
    for(y=0;y<8;y++) {
 
272
        *((uint64_t *)(d+y*stride)) = a;
 
273
    }
 
274
}
 
275
 
 
276
static void intra_pred_horiz(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
 
277
    int y;
 
278
    uint64_t a;
 
279
    for(y=0;y<8;y++) {
 
280
        a = left[y+1] * 0x0101010101010101ULL;
 
281
        *((uint64_t *)(d+y*stride)) = a;
 
282
    }
 
283
}
 
284
 
 
285
static void intra_pred_dc_128(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
 
286
    int y;
 
287
    uint64_t a = 0x8080808080808080ULL;
 
288
    for(y=0;y<8;y++)
 
289
        *((uint64_t *)(d+y*stride)) = a;
 
290
}
 
291
 
 
292
static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
 
293
    int x,y,ia;
 
294
    int ih = 0;
 
295
    int iv = 0;
 
296
    uint8_t *cm = cropTbl + MAX_NEG_CROP;
 
297
 
 
298
    for(x=0; x<4; x++) {
 
299
        ih += (x+1)*(top[5+x]-top[3-x]);
 
300
        iv += (x+1)*(left[5+x]-left[3-x]);
 
301
    }
 
302
    ia = (top[8]+left[8])<<4;
 
303
    ih = (17*ih+16)>>5;
 
304
    iv = (17*iv+16)>>5;
 
305
    for(y=0; y<8; y++)
 
306
        for(x=0; x<8; x++)
 
307
            d[y*stride+x] = cm[(ia+(x-3)*ih+(y-3)*iv+16)>>5];
 
308
}
 
309
 
 
310
#define LOWPASS(ARRAY,INDEX)                                            \
 
311
    (( ARRAY[(INDEX)-1] + 2*ARRAY[(INDEX)] + ARRAY[(INDEX)+1] + 2) >> 2)
 
312
 
 
313
static void intra_pred_lp(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
 
314
    int x,y;
 
315
    for(y=0; y<8; y++)
 
316
        for(x=0; x<8; x++)
 
317
            d[y*stride+x] = (LOWPASS(top,x+1) + LOWPASS(left,y+1)) >> 1;
 
318
}
 
319
 
 
320
static void intra_pred_down_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
 
321
    int x,y;
 
322
    for(y=0; y<8; y++)
 
323
        for(x=0; x<8; x++)
 
324
            d[y*stride+x] = (LOWPASS(top,x+y+2) + LOWPASS(left,x+y+2)) >> 1;
 
325
}
 
326
 
 
327
static void intra_pred_down_right(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
 
328
    int x,y;
 
329
    for(y=0; y<8; y++)
 
330
        for(x=0; x<8; x++)
 
331
            if(x==y)
 
332
                d[y*stride+x] = (left[1]+2*top[0]+top[1]+2)>>2;
 
333
            else if(x>y)
 
334
                d[y*stride+x] = LOWPASS(top,x-y);
 
335
            else
 
336
                d[y*stride+x] = LOWPASS(left,y-x);
 
337
}
 
338
 
 
339
static void intra_pred_lp_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
 
340
    int x,y;
 
341
    for(y=0; y<8; y++)
 
342
        for(x=0; x<8; x++)
 
343
            d[y*stride+x] = LOWPASS(left,y+1);
 
344
}
 
345
 
 
346
static void intra_pred_lp_top(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
 
347
    int x,y;
 
348
    for(y=0; y<8; y++)
 
349
        for(x=0; x<8; x++)
 
350
            d[y*stride+x] = LOWPASS(top,x+1);
 
351
}
 
352
 
 
353
#undef LOWPASS
 
354
 
 
355
static inline void modify_pred(const int_fast8_t *mod_table, int *mode) {
 
356
    *mode = mod_table[*mode];
 
357
    if(*mode < 0) {
 
358
        av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
 
359
        *mode = 0;
 
360
    }
 
361
}
 
362
 
 
363
/*****************************************************************************
 
364
 *
 
365
 * motion compensation
 
366
 *
 
367
 ****************************************************************************/
 
368
 
 
369
static inline void mc_dir_part(AVSContext *h,Picture *pic,int square,
 
370
                        int chroma_height,int delta,int list,uint8_t *dest_y,
 
371
                        uint8_t *dest_cb,uint8_t *dest_cr,int src_x_offset,
 
372
                        int src_y_offset,qpel_mc_func *qpix_op,
 
373
                        h264_chroma_mc_func chroma_op,vector_t *mv){
 
374
    MpegEncContext * const s = &h->s;
 
375
    const int mx= mv->x + src_x_offset*8;
 
376
    const int my= mv->y + src_y_offset*8;
 
377
    const int luma_xy= (mx&3) + ((my&3)<<2);
 
378
    uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->l_stride;
 
379
    uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->c_stride;
 
380
    uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->c_stride;
 
381
    int extra_width= 0; //(s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
 
382
    int extra_height= extra_width;
 
383
    int emu=0;
 
384
    const int full_mx= mx>>2;
 
385
    const int full_my= my>>2;
 
386
    const int pic_width  = 16*h->mb_width;
 
387
    const int pic_height = 16*h->mb_height;
 
388
 
 
389
    if(!pic->data[0])
 
390
        return;
 
391
    if(mx&7) extra_width -= 3;
 
392
    if(my&7) extra_height -= 3;
 
393
 
 
394
    if(   full_mx < 0-extra_width
 
395
          || full_my < 0-extra_height
 
396
          || full_mx + 16/*FIXME*/ > pic_width + extra_width
 
397
          || full_my + 16/*FIXME*/ > pic_height + extra_height){
 
398
        ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->l_stride, h->l_stride,
 
399
                            16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
 
400
        src_y= s->edge_emu_buffer + 2 + 2*h->l_stride;
 
401
        emu=1;
 
402
    }
 
403
 
 
404
    qpix_op[luma_xy](dest_y, src_y, h->l_stride); //FIXME try variable height perhaps?
 
405
    if(!square){
 
406
        qpix_op[luma_xy](dest_y + delta, src_y + delta, h->l_stride);
 
407
    }
 
408
 
 
409
    if(emu){
 
410
        ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->c_stride,
 
411
                            9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
 
412
        src_cb= s->edge_emu_buffer;
 
413
    }
 
414
    chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx&7, my&7);
 
415
 
 
416
    if(emu){
 
417
        ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->c_stride,
 
418
                            9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
 
419
        src_cr= s->edge_emu_buffer;
 
420
    }
 
421
    chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx&7, my&7);
 
422
}
 
423
 
 
424
static inline void mc_part_std(AVSContext *h,int square,int chroma_height,int delta,
 
425
                        uint8_t *dest_y,uint8_t *dest_cb,uint8_t *dest_cr,
 
426
                        int x_offset, int y_offset,qpel_mc_func *qpix_put,
 
427
                        h264_chroma_mc_func chroma_put,qpel_mc_func *qpix_avg,
 
428
                        h264_chroma_mc_func chroma_avg, vector_t *mv){
 
429
    qpel_mc_func *qpix_op=  qpix_put;
 
430
    h264_chroma_mc_func chroma_op= chroma_put;
 
431
 
 
432
    dest_y  += 2*x_offset + 2*y_offset*h->l_stride;
 
433
    dest_cb +=   x_offset +   y_offset*h->c_stride;
 
434
    dest_cr +=   x_offset +   y_offset*h->c_stride;
 
435
    x_offset += 8*h->mbx;
 
436
    y_offset += 8*h->mby;
 
437
 
 
438
    if(mv->ref >= 0){
 
439
        Picture *ref= &h->DPB[mv->ref];
 
440
        mc_dir_part(h, ref, square, chroma_height, delta, 0,
 
441
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
 
442
                    qpix_op, chroma_op, mv);
 
443
 
 
444
        qpix_op=  qpix_avg;
 
445
        chroma_op= chroma_avg;
 
446
    }
 
447
 
 
448
    if((mv+MV_BWD_OFFS)->ref >= 0){
 
449
        Picture *ref= &h->DPB[0];
 
450
        mc_dir_part(h, ref, square, chroma_height, delta, 1,
 
451
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
 
452
                    qpix_op, chroma_op, mv+MV_BWD_OFFS);
 
453
    }
 
454
}
 
455
 
 
456
static void inter_pred(AVSContext *h, enum mb_t mb_type) {
 
457
    if(partition_flags[mb_type] == 0){ // 16x16
 
458
        mc_part_std(h, 1, 8, 0, h->cy, h->cu, h->cv, 0, 0,
 
459
                h->s.dsp.put_cavs_qpel_pixels_tab[0],
 
460
                h->s.dsp.put_h264_chroma_pixels_tab[0],
 
461
                h->s.dsp.avg_cavs_qpel_pixels_tab[0],
 
462
                h->s.dsp.avg_h264_chroma_pixels_tab[0],&h->mv[MV_FWD_X0]);
 
463
    }else{
 
464
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0,
 
465
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
 
466
                h->s.dsp.put_h264_chroma_pixels_tab[1],
 
467
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
 
468
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]);
 
469
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0,
 
470
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
 
471
                h->s.dsp.put_h264_chroma_pixels_tab[1],
 
472
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
 
473
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]);
 
474
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4,
 
475
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
 
476
                h->s.dsp.put_h264_chroma_pixels_tab[1],
 
477
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
 
478
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]);
 
479
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4,
 
480
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
 
481
                h->s.dsp.put_h264_chroma_pixels_tab[1],
 
482
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
 
483
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
 
484
    }
 
485
    /* set intra prediction modes to default values */
 
486
    h->pred_mode_Y[3] =  h->pred_mode_Y[6] = INTRA_L_LP;
 
487
    h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
 
488
}
 
489
 
 
490
/*****************************************************************************
 
491
 *
 
492
 * motion vector prediction
 
493
 *
 
494
 ****************************************************************************/
 
495
 
 
496
static inline void set_mvs(vector_t *mv, enum block_t size) {
 
497
    switch(size) {
 
498
    case BLK_16X16:
 
499
        mv[MV_STRIDE  ] = mv[0];
 
500
        mv[MV_STRIDE+1] = mv[0];
 
501
    case BLK_16X8:
 
502
        mv[1] = mv[0];
 
503
        break;
 
504
    case BLK_8X16:
 
505
        mv[MV_STRIDE] = mv[0];
 
506
        break;
 
507
    }
 
508
}
 
509
 
 
510
static inline void store_mvs(AVSContext *h) {
 
511
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0];
 
512
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1];
 
513
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2];
 
514
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3];
 
515
}
 
516
 
 
517
static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) {
 
518
    int den = h->scale_den[src->ref];
 
519
 
 
520
    *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9;
 
521
    *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9;
 
522
}
 
523
 
 
524
static inline void mv_pred_median(AVSContext *h, vector_t *mvP, vector_t *mvA, vector_t *mvB, vector_t *mvC) {
 
525
    int ax, ay, bx, by, cx, cy;
 
526
    int len_ab, len_bc, len_ca, len_mid;
 
527
 
 
528
    /* scale candidates according to their temporal span */
 
529
    scale_mv(h, &ax, &ay, mvA, mvP->dist);
 
530
    scale_mv(h, &bx, &by, mvB, mvP->dist);
 
531
    scale_mv(h, &cx, &cy, mvC, mvP->dist);
 
532
    /* find the geometrical median of the three candidates */
 
533
    len_ab = abs(ax - bx) + abs(ay - by);
 
534
    len_bc = abs(bx - cx) + abs(by - cy);
 
535
    len_ca = abs(cx - ax) + abs(cy - ay);
 
536
    len_mid = mid_pred(len_ab, len_bc, len_ca);
 
537
    if(len_mid == len_ab) {
 
538
        mvP->x = cx;
 
539
        mvP->y = cy;
 
540
    } else if(len_mid == len_bc) {
 
541
        mvP->x = ax;
 
542
        mvP->y = ay;
 
543
    } else {
 
544
        mvP->x = bx;
 
545
        mvP->y = by;
 
546
    }
 
547
}
 
548
 
 
549
static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw,
 
550
                                  vector_t *col_mv) {
 
551
    vector_t *pmv_bw = pmv_fw + MV_BWD_OFFS;
 
552
    int den = h->direct_den[col_mv->ref];
 
553
    int m = col_mv->x >> 31;
 
554
 
 
555
    pmv_fw->dist = h->dist[1];
 
556
    pmv_bw->dist = h->dist[0];
 
557
    pmv_fw->ref = 1;
 
558
    pmv_bw->ref = 0;
 
559
    /* scale the co-located motion vector according to its temporal span */
 
560
    pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
 
561
    pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
 
562
    m = col_mv->y >> 31;
 
563
    pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
 
564
    pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
 
565
}
 
566
 
 
567
static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) {
 
568
    vector_t *dst = src + MV_BWD_OFFS;
 
569
 
 
570
    /* backward mv is the scaled and negated forward mv */
 
571
    dst->x = -((src->x * h->sym_factor + 256) >> 9);
 
572
    dst->y = -((src->y * h->sym_factor + 256) >> 9);
 
573
    dst->ref = 0;
 
574
    dst->dist = h->dist[0];
 
575
    set_mvs(dst, size);
 
576
}
 
577
 
 
578
static void mv_pred(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
 
579
                    enum mv_pred_t mode, enum block_t size, int ref) {
 
580
    vector_t *mvP = &h->mv[nP];
 
581
    vector_t *mvA = &h->mv[nP-1];
 
582
    vector_t *mvB = &h->mv[nP-4];
 
583
    vector_t *mvC = &h->mv[nC];
 
584
    const vector_t *mvP2 = NULL;
 
585
 
 
586
    mvP->ref = ref;
 
587
    mvP->dist = h->dist[mvP->ref];
 
588
    if(mvC->ref == NOT_AVAIL)
 
589
        mvC = &h->mv[nP-5]; // set to top-left (mvD)
 
590
    if((mode == MV_PRED_PSKIP) &&
 
591
       ((mvA->ref == NOT_AVAIL) || (mvB->ref == NOT_AVAIL) ||
 
592
           ((mvA->x | mvA->y | mvA->ref) == 0)  ||
 
593
           ((mvB->x | mvB->y | mvB->ref) == 0) )) {
 
594
        mvP2 = &un_mv;
 
595
    /* if there is only one suitable candidate, take it */
 
596
    } else if((mvA->ref >= 0) && (mvB->ref < 0) && (mvC->ref < 0)) {
 
597
        mvP2= mvA;
 
598
    } else if((mvA->ref < 0) && (mvB->ref >= 0) && (mvC->ref < 0)) {
 
599
        mvP2= mvB;
 
600
    } else if((mvA->ref < 0) && (mvB->ref < 0) && (mvC->ref >= 0)) {
 
601
        mvP2= mvC;
 
602
    } else if(mode == MV_PRED_LEFT     && mvA->ref == ref){
 
603
        mvP2= mvA;
 
604
    } else if(mode == MV_PRED_TOP      && mvB->ref == ref){
 
605
        mvP2= mvB;
 
606
    } else if(mode == MV_PRED_TOPRIGHT && mvC->ref == ref){
 
607
        mvP2= mvC;
 
608
    }
 
609
    if(mvP2){
 
610
        mvP->x = mvP2->x;
 
611
        mvP->y = mvP2->y;
 
612
    }else
 
613
        mv_pred_median(h, mvP, mvA, mvB, mvC);
 
614
 
 
615
    if(mode < MV_PRED_PSKIP) {
 
616
        mvP->x += get_se_golomb(&h->s.gb);
 
617
        mvP->y += get_se_golomb(&h->s.gb);
 
618
    }
 
619
    set_mvs(mvP,size);
 
620
}
 
621
 
 
622
/*****************************************************************************
 
623
 *
 
624
 * residual data decoding
 
625
 *
 
626
 ****************************************************************************/
 
627
 
 
628
/** kth-order exponential golomb code */
 
629
static inline int get_ue_code(GetBitContext *gb, int order) {
 
630
    if(order) {
 
631
        int ret = get_ue_golomb(gb) << order;
 
632
        return ret + get_bits(gb,order);
 
633
    }
 
634
    return get_ue_golomb(gb);
 
635
}
 
636
 
 
637
/**
 
638
 * decode coefficients from one 8x8 block, dequantize, inverse transform
 
639
 *  and add them to sample block
 
640
 * @param r pointer to 2D VLC table
 
641
 * @param esc_golomb_order escape codes are k-golomb with this order k
 
642
 * @param qp quantizer
 
643
 * @param dst location of sample block
 
644
 * @param stride line stride in frame buffer
 
645
 */
 
646
static int decode_residual_block(AVSContext *h, GetBitContext *gb,
 
647
                                 const residual_vlc_t *r, int esc_golomb_order,
 
648
                                 int qp, uint8_t *dst, int stride) {
 
649
    int i,pos = -1;
 
650
    int level_code, esc_code, level, run, mask;
 
651
    int level_buf[64];
 
652
    int run_buf[64];
 
653
    int dqm = dequant_mul[qp];
 
654
    int dqs = dequant_shift[qp];
 
655
    int dqa = 1 << (dqs - 1);
 
656
    const uint8_t *scantab = h->scantable.permutated;
 
657
    DCTELEM *block = h->block;
 
658
 
 
659
    for(i=0;i<65;i++) {
 
660
        level_code = get_ue_code(gb,r->golomb_order);
 
661
        if(level_code >= ESCAPE_CODE) {
 
662
            run = ((level_code - ESCAPE_CODE) >> 1) + 1;
 
663
            esc_code = get_ue_code(gb,esc_golomb_order);
 
664
            level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
 
665
            while(level > r->inc_limit)
 
666
                r++;
 
667
            mask = -(level_code & 1);
 
668
            level = (level^mask) - mask;
 
669
        } else {
 
670
            level = r->rltab[level_code][0];
 
671
            if(!level) //end of block signal
 
672
                break;
 
673
            run   = r->rltab[level_code][1];
 
674
            r += r->rltab[level_code][2];
 
675
        }
 
676
        level_buf[i] = level;
 
677
        run_buf[i] = run;
 
678
    }
 
679
    /* inverse scan and dequantization */
 
680
    while(--i >= 0){
 
681
        pos += run_buf[i];
 
682
        if(pos > 63) {
 
683
            av_log(h->s.avctx, AV_LOG_ERROR,
 
684
                   "position out of block bounds at pic %d MB(%d,%d)\n",
 
685
                   h->picture.poc, h->mbx, h->mby);
 
686
            return -1;
 
687
        }
 
688
        block[scantab[pos]] = (level_buf[i]*dqm + dqa) >> dqs;
 
689
    }
 
690
    h->s.dsp.cavs_idct8_add(dst,block,stride);
 
691
    return 0;
 
692
}
 
693
 
 
694
 
 
695
static inline void decode_residual_chroma(AVSContext *h) {
 
696
    if(h->cbp & (1<<4))
 
697
        decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
 
698
                              h->cu,h->c_stride);
 
699
    if(h->cbp & (1<<5))
 
700
        decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
 
701
                              h->cv,h->c_stride);
 
702
}
 
703
 
 
704
static inline int decode_residual_inter(AVSContext *h) {
 
705
    int block;
 
706
 
 
707
    /* get coded block pattern */
 
708
    int cbp= get_ue_golomb(&h->s.gb);
 
709
    if(cbp > 63){
 
710
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
 
711
        return -1;
 
712
    }
 
713
    h->cbp = cbp_tab[cbp][1];
 
714
 
 
715
    /* get quantizer */
 
716
    if(h->cbp && !h->qp_fixed)
 
717
        h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
 
718
    for(block=0;block<4;block++)
 
719
        if(h->cbp & (1<<block))
 
720
            decode_residual_block(h,&h->s.gb,inter_2dvlc,0,h->qp,
 
721
                                  h->cy + h->luma_scan[block], h->l_stride);
 
722
    decode_residual_chroma(h);
 
723
 
 
724
    return 0;
 
725
}
 
726
 
 
727
/*****************************************************************************
 
728
 *
 
729
 * macroblock level
 
730
 *
 
731
 ****************************************************************************/
 
732
 
 
733
/**
 
734
 * initialise predictors for motion vectors and intra prediction
 
735
 */
 
736
static inline void init_mb(AVSContext *h) {
 
737
    int i;
 
738
 
 
739
    /* copy predictors from top line (MB B and C) into cache */
 
740
    for(i=0;i<3;i++) {
 
741
        h->mv[MV_FWD_B2+i] = h->top_mv[0][h->mbx*2+i];
 
742
        h->mv[MV_BWD_B2+i] = h->top_mv[1][h->mbx*2+i];
 
743
    }
 
744
    h->pred_mode_Y[1] = h->top_pred_Y[h->mbx*2+0];
 
745
    h->pred_mode_Y[2] = h->top_pred_Y[h->mbx*2+1];
 
746
    /* clear top predictors if MB B is not available */
 
747
    if(!(h->flags & B_AVAIL)) {
 
748
        h->mv[MV_FWD_B2] = un_mv;
 
749
        h->mv[MV_FWD_B3] = un_mv;
 
750
        h->mv[MV_BWD_B2] = un_mv;
 
751
        h->mv[MV_BWD_B3] = un_mv;
 
752
        h->pred_mode_Y[1] = h->pred_mode_Y[2] = NOT_AVAIL;
 
753
        h->flags &= ~(C_AVAIL|D_AVAIL);
 
754
    } else if(h->mbx) {
 
755
        h->flags |= D_AVAIL;
 
756
    }
 
757
    if(h->mbx == h->mb_width-1) //MB C not available
 
758
        h->flags &= ~C_AVAIL;
 
759
    /* clear top-right predictors if MB C is not available */
 
760
    if(!(h->flags & C_AVAIL)) {
 
761
        h->mv[MV_FWD_C2] = un_mv;
 
762
        h->mv[MV_BWD_C2] = un_mv;
 
763
    }
 
764
    /* clear top-left predictors if MB D is not available */
 
765
    if(!(h->flags & D_AVAIL)) {
 
766
        h->mv[MV_FWD_D3] = un_mv;
 
767
        h->mv[MV_BWD_D3] = un_mv;
 
768
    }
 
769
    /* set pointer for co-located macroblock type */
 
770
    h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx];
 
771
}
 
772
 
 
773
static inline void check_for_slice(AVSContext *h);
 
774
 
 
775
/**
 
776
 * save predictors for later macroblocks and increase
 
777
 * macroblock address
 
778
 * @returns 0 if end of frame is reached, 1 otherwise
 
779
 */
 
780
static inline int next_mb(AVSContext *h) {
 
781
    int i;
 
782
 
 
783
    h->flags |= A_AVAIL;
 
784
    h->cy += 16;
 
785
    h->cu += 8;
 
786
    h->cv += 8;
 
787
    /* copy mvs as predictors to the left */
 
788
    for(i=0;i<=20;i+=4)
 
789
        h->mv[i] = h->mv[i+2];
 
790
    /* copy bottom mvs from cache to top line */
 
791
    h->top_mv[0][h->mbx*2+0] = h->mv[MV_FWD_X2];
 
792
    h->top_mv[0][h->mbx*2+1] = h->mv[MV_FWD_X3];
 
793
    h->top_mv[1][h->mbx*2+0] = h->mv[MV_BWD_X2];
 
794
    h->top_mv[1][h->mbx*2+1] = h->mv[MV_BWD_X3];
 
795
    /* next MB address */
 
796
    h->mbx++;
 
797
    if(h->mbx == h->mb_width) { //new mb line
 
798
        h->flags = B_AVAIL|C_AVAIL;
 
799
        /* clear left pred_modes */
 
800
        h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
 
801
        /* clear left mv predictors */
 
802
        for(i=0;i<=20;i+=4)
 
803
            h->mv[i] = un_mv;
 
804
        h->mbx = 0;
 
805
        h->mby++;
 
806
        /* re-calculate sample pointers */
 
807
        h->cy = h->picture.data[0] + h->mby*16*h->l_stride;
 
808
        h->cu = h->picture.data[1] + h->mby*8*h->c_stride;
 
809
        h->cv = h->picture.data[2] + h->mby*8*h->c_stride;
 
810
        if(h->mby == h->mb_height) { //frame end
 
811
            return 0;
 
812
        } else {
 
813
            //check_for_slice(h);
 
814
        }
 
815
    }
 
816
    return 1;
 
817
}
 
818
 
 
819
static int decode_mb_i(AVSContext *h, int cbp_code) {
 
820
    GetBitContext *gb = &h->s.gb;
 
821
    int block, pred_mode_uv;
 
822
    uint8_t top[18];
 
823
    uint8_t *left = NULL;
 
824
    uint8_t *d;
 
825
 
 
826
    init_mb(h);
 
827
 
 
828
    /* get intra prediction modes from stream */
 
829
    for(block=0;block<4;block++) {
 
830
        int nA,nB,predpred;
 
831
        int pos = scan3x3[block];
 
832
 
 
833
        nA = h->pred_mode_Y[pos-1];
 
834
        nB = h->pred_mode_Y[pos-3];
 
835
        predpred = FFMIN(nA,nB);
 
836
        if(predpred == NOT_AVAIL) // if either is not available
 
837
            predpred = INTRA_L_LP;
 
838
        if(!get_bits1(gb)){
 
839
            int rem_mode= get_bits(gb, 2);
 
840
            predpred = rem_mode + (rem_mode >= predpred);
 
841
        }
 
842
        h->pred_mode_Y[pos] = predpred;
 
843
    }
 
844
    pred_mode_uv = get_ue_golomb(gb);
 
845
    if(pred_mode_uv > 6) {
 
846
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
 
847
        return -1;
 
848
    }
 
849
 
 
850
    /* save pred modes before they get modified */
 
851
    h->pred_mode_Y[3] =  h->pred_mode_Y[5];
 
852
    h->pred_mode_Y[6] =  h->pred_mode_Y[8];
 
853
    h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
 
854
    h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
 
855
 
 
856
    /* modify pred modes according to availability of neighbour samples */
 
857
    if(!(h->flags & A_AVAIL)) {
 
858
        modify_pred(left_modifier_l, &h->pred_mode_Y[4] );
 
859
        modify_pred(left_modifier_l, &h->pred_mode_Y[7] );
 
860
        modify_pred(left_modifier_c, &pred_mode_uv );
 
861
    }
 
862
    if(!(h->flags & B_AVAIL)) {
 
863
        modify_pred(top_modifier_l, &h->pred_mode_Y[4] );
 
864
        modify_pred(top_modifier_l, &h->pred_mode_Y[5] );
 
865
        modify_pred(top_modifier_c, &pred_mode_uv );
 
866
    }
 
867
 
 
868
    /* get coded block pattern */
 
869
    if(h->pic_type == FF_I_TYPE)
 
870
        cbp_code = get_ue_golomb(gb);
 
871
    if(cbp_code > 63){
 
872
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
 
873
        return -1;
 
874
    }
 
875
    h->cbp = cbp_tab[cbp_code][0];
 
876
    if(h->cbp && !h->qp_fixed)
 
877
        h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
 
878
 
 
879
    /* luma intra prediction interleaved with residual decode/transform/add */
 
880
    for(block=0;block<4;block++) {
 
881
        d = h->cy + h->luma_scan[block];
 
882
        load_intra_pred_luma(h, top, &left, block);
 
883
        h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
 
884
            (d, top, left, h->l_stride);
 
885
        if(h->cbp & (1<<block))
 
886
            decode_residual_block(h,gb,intra_2dvlc,1,h->qp,d,h->l_stride);
 
887
    }
 
888
 
 
889
    /* chroma intra prediction */
 
890
    /* extend borders by one pixel */
 
891
    h->left_border_u[9] = h->left_border_u[8];
 
892
    h->left_border_v[9] = h->left_border_v[8];
 
893
    h->top_border_u[h->mbx*10+9] = h->top_border_u[h->mbx*10+8];
 
894
    h->top_border_v[h->mbx*10+9] = h->top_border_v[h->mbx*10+8];
 
895
    if(h->mbx && h->mby) {
 
896
        h->top_border_u[h->mbx*10] = h->left_border_u[0] = h->topleft_border_u;
 
897
        h->top_border_v[h->mbx*10] = h->left_border_v[0] = h->topleft_border_v;
 
898
    } else {
 
899
        h->left_border_u[0] = h->left_border_u[1];
 
900
        h->left_border_v[0] = h->left_border_v[1];
 
901
        h->top_border_u[h->mbx*10] = h->top_border_u[h->mbx*10+1];
 
902
        h->top_border_v[h->mbx*10] = h->top_border_v[h->mbx*10+1];
 
903
    }
 
904
    h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
 
905
                                  h->left_border_u, h->c_stride);
 
906
    h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
 
907
                                  h->left_border_v, h->c_stride);
 
908
 
 
909
    decode_residual_chroma(h);
 
910
    filter_mb(h,I_8X8);
 
911
 
 
912
    /* mark motion vectors as intra */
 
913
    h->mv[MV_FWD_X0] = intra_mv;
 
914
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
 
915
    h->mv[MV_BWD_X0] = intra_mv;
 
916
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
 
917
    if(h->pic_type != FF_B_TYPE)
 
918
        *h->col_type = I_8X8;
 
919
 
 
920
    return 0;
 
921
}
 
922
 
 
923
static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
 
924
    GetBitContext *gb = &h->s.gb;
 
925
    int ref[4];
 
926
 
 
927
    init_mb(h);
 
928
    switch(mb_type) {
 
929
    case P_SKIP:
 
930
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
 
931
        break;
 
932
    case P_16X16:
 
933
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
 
934
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_16X16,ref[0]);
 
935
        break;
 
936
    case P_16X8:
 
937
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
 
938
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
 
939
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,      BLK_16X8, ref[0]);
 
940
        mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT,     BLK_16X8, ref[2]);
 
941
        break;
 
942
    case P_8X16:
 
943
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
 
944
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
 
945
        mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT,     BLK_8X16, ref[0]);
 
946
        mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
 
947
        break;
 
948
    case P_8X8:
 
949
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
 
950
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
 
951
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
 
952
        ref[3] = h->ref_flag ? 0 : get_bits1(gb);
 
953
        mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN,   BLK_8X8, ref[0]);
 
954
        mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_8X8, ref[1]);
 
955
        mv_pred(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN,   BLK_8X8, ref[2]);
 
956
        mv_pred(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN,   BLK_8X8, ref[3]);
 
957
    }
 
958
    inter_pred(h, mb_type);
 
959
    store_mvs(h);
 
960
    if(mb_type != P_SKIP)
 
961
        decode_residual_inter(h);
 
962
    filter_mb(h,mb_type);
 
963
    *h->col_type = mb_type;
 
964
}
 
965
 
 
966
static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
 
967
    int block;
 
968
    enum sub_mb_t sub_type[4];
 
969
    int flags;
 
970
 
 
971
    init_mb(h);
 
972
 
 
973
    /* reset all MVs */
 
974
    h->mv[MV_FWD_X0] = dir_mv;
 
975
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
 
976
    h->mv[MV_BWD_X0] = dir_mv;
 
977
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
 
978
    switch(mb_type) {
 
979
    case B_SKIP:
 
980
    case B_DIRECT:
 
981
        if(!(*h->col_type)) {
 
982
            /* intra MB at co-location, do in-plane prediction */
 
983
            mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
 
984
            mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
 
985
        } else
 
986
            /* direct prediction from co-located P MB, block-wise */
 
987
            for(block=0;block<4;block++)
 
988
                mv_pred_direct(h,&h->mv[mv_scan[block]],
 
989
                            &h->col_mv[(h->mby*h->mb_width+h->mbx)*4 + block]);
 
990
        break;
 
991
    case B_FWD_16X16:
 
992
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
 
993
        break;
 
994
    case B_SYM_16X16:
 
995
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
 
996
        mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
 
997
        break;
 
998
    case B_BWD_16X16:
 
999
        mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
 
1000
        break;
 
1001
    case B_8X8:
 
1002
        for(block=0;block<4;block++)
 
1003
            sub_type[block] = get_bits(&h->s.gb,2);
 
1004
        for(block=0;block<4;block++) {
 
1005
            switch(sub_type[block]) {
 
1006
            case B_SUB_DIRECT:
 
1007
                if(!(*h->col_type)) {
 
1008
                    /* intra MB at co-location, do in-plane prediction */
 
1009
                    mv_pred(h, mv_scan[block], mv_scan[block]-3,
 
1010
                            MV_PRED_BSKIP, BLK_8X8, 1);
 
1011
                    mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
 
1012
                            mv_scan[block]-3+MV_BWD_OFFS,
 
1013
                            MV_PRED_BSKIP, BLK_8X8, 0);
 
1014
                } else
 
1015
                    mv_pred_direct(h,&h->mv[mv_scan[block]],
 
1016
                                   &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]);
 
1017
                break;
 
1018
            case B_SUB_FWD:
 
1019
                mv_pred(h, mv_scan[block], mv_scan[block]-3,
 
1020
                        MV_PRED_MEDIAN, BLK_8X8, 1);
 
1021
                break;
 
1022
            case B_SUB_SYM:
 
1023
                mv_pred(h, mv_scan[block], mv_scan[block]-3,
 
1024
                        MV_PRED_MEDIAN, BLK_8X8, 1);
 
1025
                mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
 
1026
                break;
 
1027
            }
 
1028
        }
 
1029
        for(block=0;block<4;block++) {
 
1030
            if(sub_type[block] == B_SUB_BWD)
 
1031
                mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
 
1032
                        mv_scan[block]+MV_BWD_OFFS-3,
 
1033
                        MV_PRED_MEDIAN, BLK_8X8, 0);
 
1034
        }
 
1035
        break;
 
1036
    default:
 
1037
        assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
 
1038
        flags = partition_flags[mb_type];
 
1039
        if(mb_type & 1) { /* 16x8 macroblock types */
 
1040
            if(flags & FWD0)
 
1041
                mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,  BLK_16X8, 1);
 
1042
            if(flags & SYM0)
 
1043
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
 
1044
            if(flags & FWD1)
 
1045
                mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
 
1046
            if(flags & SYM1)
 
1047
                mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
 
1048
            if(flags & BWD0)
 
1049
                mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP,  BLK_16X8, 0);
 
1050
            if(flags & BWD1)
 
1051
                mv_pred(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
 
1052
        } else {          /* 8x16 macroblock types */
 
1053
            if(flags & FWD0)
 
1054
                mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
 
1055
            if(flags & SYM0)
 
1056
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
 
1057
            if(flags & FWD1)
 
1058
                mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
 
1059
            if(flags & SYM1)
 
1060
                mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
 
1061
            if(flags & BWD0)
 
1062
                mv_pred(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
 
1063
            if(flags & BWD1)
 
1064
                mv_pred(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 0);
 
1065
        }
 
1066
    }
 
1067
    inter_pred(h, mb_type);
 
1068
    if(mb_type != B_SKIP)
 
1069
        decode_residual_inter(h);
 
1070
    filter_mb(h,mb_type);
 
1071
}
 
1072
 
 
1073
/*****************************************************************************
 
1074
 *
 
1075
 * slice level
 
1076
 *
 
1077
 ****************************************************************************/
 
1078
 
 
1079
static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
 
1080
    if(h->stc > 0xAF)
 
1081
        av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
 
1082
    h->mby = h->stc;
 
1083
    if((h->mby == 0) && (!h->qp_fixed)){
 
1084
        h->qp_fixed = get_bits1(gb);
 
1085
        h->qp = get_bits(gb,6);
 
1086
    }
 
1087
    /* inter frame or second slice can have weighting params */
 
1088
    if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
 
1089
        if(get_bits1(gb)) { //slice_weighting_flag
 
1090
            av_log(h->s.avctx, AV_LOG_ERROR,
 
1091
                   "weighted prediction not yet supported\n");
 
1092
        }
 
1093
    return 0;
 
1094
}
 
1095
 
 
1096
static inline void check_for_slice(AVSContext *h) {
 
1097
    GetBitContext *gb = &h->s.gb;
 
1098
    int align;
 
1099
    align = (-get_bits_count(gb)) & 7;
 
1100
    if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
 
1101
        get_bits_long(gb,24+align);
 
1102
        h->stc = get_bits(gb,8);
 
1103
        decode_slice_header(h,gb);
 
1104
    }
 
1105
}
 
1106
 
 
1107
/*****************************************************************************
 
1108
 *
 
1109
 * frame level
 
1110
 *
 
1111
 ****************************************************************************/
 
1112
 
 
1113
static void init_pic(AVSContext *h) {
 
1114
    int i;
 
1115
 
 
1116
    /* clear some predictors */
 
1117
    for(i=0;i<=20;i+=4)
 
1118
        h->mv[i] = un_mv;
 
1119
    h->mv[MV_BWD_X0] = dir_mv;
 
1120
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
 
1121
    h->mv[MV_FWD_X0] = dir_mv;
 
1122
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
 
1123
    h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
 
1124
    h->cy = h->picture.data[0];
 
1125
    h->cu = h->picture.data[1];
 
1126
    h->cv = h->picture.data[2];
 
1127
    h->l_stride = h->picture.linesize[0];
 
1128
    h->c_stride = h->picture.linesize[1];
 
1129
    h->luma_scan[2] = 8*h->l_stride;
 
1130
    h->luma_scan[3] = 8*h->l_stride+8;
 
1131
    h->mbx = h->mby = 0;
 
1132
    h->flags = 0;
 
1133
}
 
1134
 
 
1135
static int decode_pic(AVSContext *h) {
 
1136
    MpegEncContext *s = &h->s;
 
1137
    int skip_count;
 
1138
    enum mb_t mb_type;
 
1139
 
 
1140
    if (!s->context_initialized) {
 
1141
        s->avctx->idct_algo = FF_IDCT_CAVS;
 
1142
        if (MPV_common_init(s) < 0)
 
1143
            return -1;
 
1144
        ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
 
1145
    }
 
1146
    get_bits(&s->gb,16);//bbv_dwlay
 
1147
    if(h->stc == PIC_PB_START_CODE) {
 
1148
        h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
 
1149
        if(h->pic_type > FF_B_TYPE) {
 
1150
            av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
 
1151
            return -1;
 
1152
        }
 
1153
        /* make sure we have the reference frames we need */
 
1154
        if(!h->DPB[0].data[0] ||
 
1155
          (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
 
1156
            return -1;
 
1157
    } else {
 
1158
        h->pic_type = FF_I_TYPE;
 
1159
        if(get_bits1(&s->gb))
 
1160
            get_bits(&s->gb,16);//time_code
 
1161
    }
 
1162
    /* release last B frame */
 
1163
    if(h->picture.data[0])
 
1164
        s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
 
1165
 
 
1166
    s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
 
1167
    init_pic(h);
 
1168
    h->picture.poc = get_bits(&s->gb,8)*2;
 
1169
 
 
1170
    /* get temporal distances and MV scaling factors */
 
1171
    if(h->pic_type != FF_B_TYPE) {
 
1172
        h->dist[0] = (h->picture.poc - h->DPB[0].poc  + 512) % 512;
 
1173
    } else {
 
1174
        h->dist[0] = (h->DPB[0].poc  - h->picture.poc + 512) % 512;
 
1175
    }
 
1176
    h->dist[1] = (h->picture.poc - h->DPB[1].poc  + 512) % 512;
 
1177
    h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
 
1178
    h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
 
1179
    if(h->pic_type == FF_B_TYPE) {
 
1180
        h->sym_factor = h->dist[0]*h->scale_den[1];
 
1181
    } else {
 
1182
        h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
 
1183
        h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
 
1184
    }
 
1185
 
 
1186
    if(s->low_delay)
 
1187
        get_ue_golomb(&s->gb); //bbv_check_times
 
1188
    h->progressive             = get_bits1(&s->gb);
 
1189
    if(h->progressive)
 
1190
        h->pic_structure = 1;
 
1191
    else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) )
 
1192
        get_bits1(&s->gb);     //advanced_pred_mode_disable
 
1193
    skip_bits1(&s->gb);        //top_field_first
 
1194
    skip_bits1(&s->gb);        //repeat_first_field
 
1195
    h->qp_fixed                = get_bits1(&s->gb);
 
1196
    h->qp                      = get_bits(&s->gb,6);
 
1197
    if(h->pic_type == FF_I_TYPE) {
 
1198
        if(!h->progressive && !h->pic_structure)
 
1199
            skip_bits1(&s->gb);//what is this?
 
1200
        skip_bits(&s->gb,4);   //reserved bits
 
1201
    } else {
 
1202
        if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
 
1203
            h->ref_flag        = get_bits1(&s->gb);
 
1204
        skip_bits(&s->gb,4);   //reserved bits
 
1205
        h->skip_mode_flag      = get_bits1(&s->gb);
 
1206
    }
 
1207
    h->loop_filter_disable     = get_bits1(&s->gb);
 
1208
    if(!h->loop_filter_disable && get_bits1(&s->gb)) {
 
1209
        h->alpha_offset        = get_se_golomb(&s->gb);
 
1210
        h->beta_offset         = get_se_golomb(&s->gb);
 
1211
    } else {
 
1212
        h->alpha_offset = h->beta_offset  = 0;
 
1213
    }
 
1214
    check_for_slice(h);
 
1215
    if(h->pic_type == FF_I_TYPE) {
 
1216
        do {
 
1217
            decode_mb_i(h, 0);
 
1218
        } while(next_mb(h));
 
1219
    } else if(h->pic_type == FF_P_TYPE) {
 
1220
        do {
 
1221
            if(h->skip_mode_flag) {
 
1222
                skip_count = get_ue_golomb(&s->gb);
 
1223
                while(skip_count--) {
 
1224
                    decode_mb_p(h,P_SKIP);
 
1225
                    if(!next_mb(h))
 
1226
                        goto done;
 
1227
                }
 
1228
                mb_type = get_ue_golomb(&s->gb) + P_16X16;
 
1229
            } else
 
1230
                mb_type = get_ue_golomb(&s->gb) + P_SKIP;
 
1231
            if(mb_type > P_8X8) {
 
1232
                decode_mb_i(h, mb_type - P_8X8 - 1);
 
1233
            } else
 
1234
                decode_mb_p(h,mb_type);
 
1235
        } while(next_mb(h));
 
1236
    } else { /* FF_B_TYPE */
 
1237
        do {
 
1238
            if(h->skip_mode_flag) {
 
1239
                skip_count = get_ue_golomb(&s->gb);
 
1240
                while(skip_count--) {
 
1241
                    decode_mb_b(h,B_SKIP);
 
1242
                    if(!next_mb(h))
 
1243
                        goto done;
 
1244
                }
 
1245
                mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
 
1246
            } else
 
1247
                mb_type = get_ue_golomb(&s->gb) + B_SKIP;
 
1248
            if(mb_type > B_8X8) {
 
1249
                decode_mb_i(h, mb_type - B_8X8 - 1);
 
1250
            } else
 
1251
                decode_mb_b(h,mb_type);
 
1252
        } while(next_mb(h));
 
1253
    }
 
1254
 done:
 
1255
    if(h->pic_type != FF_B_TYPE) {
 
1256
        if(h->DPB[1].data[0])
 
1257
            s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
 
1258
        memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture));
 
1259
        memcpy(&h->DPB[0], &h->picture, sizeof(Picture));
 
1260
        memset(&h->picture,0,sizeof(Picture));
 
1261
    }
 
1262
    return 0;
 
1263
}
 
1264
 
 
1265
/*****************************************************************************
 
1266
 *
 
1267
 * headers and interface
 
1268
 *
 
1269
 ****************************************************************************/
 
1270
 
 
1271
/**
 
1272
 * some predictions require data from the top-neighbouring macroblock.
 
1273
 * this data has to be stored for one complete row of macroblocks
 
1274
 * and this storage space is allocated here
 
1275
 */
 
1276
static void init_top_lines(AVSContext *h) {
 
1277
    /* alloc top line of predictors */
 
1278
    h->top_qp       = av_malloc( h->mb_width);
 
1279
    h->top_mv[0]    = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
 
1280
    h->top_mv[1]    = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
 
1281
    h->top_pred_Y   = av_malloc( h->mb_width*2*sizeof(*h->top_pred_Y));
 
1282
    h->top_border_y = av_malloc((h->mb_width+1)*16);
 
1283
    h->top_border_u = av_malloc((h->mb_width)*10);
 
1284
    h->top_border_v = av_malloc((h->mb_width)*10);
 
1285
 
 
1286
    /* alloc space for co-located MVs and types */
 
1287
    h->col_mv       = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t));
 
1288
    h->col_type_base = av_malloc(h->mb_width*h->mb_height);
 
1289
    h->block        = av_mallocz(64*sizeof(DCTELEM));
 
1290
}
 
1291
 
 
1292
static int decode_seq_header(AVSContext *h) {
 
1293
    MpegEncContext *s = &h->s;
 
1294
    extern const AVRational ff_frame_rate_tab[];
 
1295
    int frame_rate_code;
 
1296
 
 
1297
    h->profile =         get_bits(&s->gb,8);
 
1298
    h->level =           get_bits(&s->gb,8);
 
1299
    skip_bits1(&s->gb); //progressive sequence
 
1300
    s->width =           get_bits(&s->gb,14);
 
1301
    s->height =          get_bits(&s->gb,14);
 
1302
    skip_bits(&s->gb,2); //chroma format
 
1303
    skip_bits(&s->gb,3); //sample_precision
 
1304
    h->aspect_ratio =    get_bits(&s->gb,4);
 
1305
    frame_rate_code =    get_bits(&s->gb,4);
 
1306
    skip_bits(&s->gb,18);//bit_rate_lower
 
1307
    skip_bits1(&s->gb);  //marker_bit
 
1308
    skip_bits(&s->gb,12);//bit_rate_upper
 
1309
    s->low_delay =       get_bits1(&s->gb);
 
1310
    h->mb_width  = (s->width  + 15) >> 4;
 
1311
    h->mb_height = (s->height + 15) >> 4;
 
1312
    h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
 
1313
    h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
 
1314
    h->s.avctx->width  = s->width;
 
1315
    h->s.avctx->height = s->height;
 
1316
    if(!h->top_qp)
 
1317
        init_top_lines(h);
 
1318
    return 0;
 
1319
}
 
1320
 
 
1321
/**
 
1322
 * finds the end of the current frame in the bitstream.
 
1323
 * @return the position of the first byte of the next frame, or -1
 
1324
 */
 
1325
int ff_cavs_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) {
 
1326
    int pic_found, i;
 
1327
    uint32_t state;
 
1328
 
 
1329
    pic_found= pc->frame_start_found;
 
1330
    state= pc->state;
 
1331
 
 
1332
    i=0;
 
1333
    if(!pic_found){
 
1334
        for(i=0; i<buf_size; i++){
 
1335
            state= (state<<8) | buf[i];
 
1336
            if(state == PIC_I_START_CODE || state == PIC_PB_START_CODE){
 
1337
                i++;
 
1338
                pic_found=1;
 
1339
                break;
 
1340
            }
 
1341
        }
 
1342
    }
 
1343
 
 
1344
    if(pic_found){
 
1345
        /* EOF considered as end of frame */
 
1346
        if (buf_size == 0)
 
1347
            return 0;
 
1348
        for(; i<buf_size; i++){
 
1349
            state= (state<<8) | buf[i];
 
1350
            if((state&0xFFFFFF00) == 0x100){
 
1351
                if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
 
1352
                    pc->frame_start_found=0;
 
1353
                    pc->state=-1;
 
1354
                    return i-3;
 
1355
                }
 
1356
            }
 
1357
        }
 
1358
    }
 
1359
    pc->frame_start_found= pic_found;
 
1360
    pc->state= state;
 
1361
    return END_NOT_FOUND;
 
1362
}
 
1363
 
 
1364
void ff_cavs_flush(AVCodecContext * avctx) {
 
1365
    AVSContext *h = avctx->priv_data;
 
1366
    h->got_keyframe = 0;
 
1367
}
 
1368
 
 
1369
static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
 
1370
                             uint8_t * buf, int buf_size) {
 
1371
    AVSContext *h = avctx->priv_data;
 
1372
    MpegEncContext *s = &h->s;
 
1373
    int input_size;
 
1374
    const uint8_t *buf_end;
 
1375
    const uint8_t *buf_ptr;
 
1376
    AVFrame *picture = data;
 
1377
    uint32_t stc;
 
1378
 
 
1379
    s->avctx = avctx;
 
1380
 
 
1381
    if (buf_size == 0) {
 
1382
        if(!s->low_delay && h->DPB[0].data[0]) {
 
1383
            *data_size = sizeof(AVPicture);
 
1384
            *picture = *(AVFrame *) &h->DPB[0];
 
1385
        }
 
1386
        return 0;
 
1387
    }
 
1388
 
 
1389
    buf_ptr = buf;
 
1390
    buf_end = buf + buf_size;
 
1391
    for(;;) {
 
1392
        buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
 
1393
        if(stc & 0xFFFFFE00)
 
1394
            return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
 
1395
        input_size = (buf_end - buf_ptr)*8;
 
1396
        switch(stc) {
 
1397
        case SEQ_START_CODE:
 
1398
            init_get_bits(&s->gb, buf_ptr, input_size);
 
1399
            decode_seq_header(h);
 
1400
            break;
 
1401
        case PIC_I_START_CODE:
 
1402
            if(!h->got_keyframe) {
 
1403
                if(h->DPB[0].data[0])
 
1404
                    avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
 
1405
                if(h->DPB[1].data[0])
 
1406
                    avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
 
1407
                h->got_keyframe = 1;
 
1408
            }
 
1409
        case PIC_PB_START_CODE:
 
1410
            *data_size = 0;
 
1411
            if(!h->got_keyframe)
 
1412
                break;
 
1413
            init_get_bits(&s->gb, buf_ptr, input_size);
 
1414
            h->stc = stc;
 
1415
            if(decode_pic(h))
 
1416
                break;
 
1417
            *data_size = sizeof(AVPicture);
 
1418
            if(h->pic_type != FF_B_TYPE) {
 
1419
                if(h->DPB[1].data[0]) {
 
1420
                    *picture = *(AVFrame *) &h->DPB[1];
 
1421
                } else {
 
1422
                    *data_size = 0;
 
1423
                }
 
1424
            } else
 
1425
                *picture = *(AVFrame *) &h->picture;
 
1426
            break;
 
1427
        case EXT_START_CODE:
 
1428
            //mpeg_decode_extension(avctx,buf_ptr, input_size);
 
1429
            break;
 
1430
        case USER_START_CODE:
 
1431
            //mpeg_decode_user_data(avctx,buf_ptr, input_size);
 
1432
            break;
 
1433
        default:
 
1434
            if (stc >= SLICE_MIN_START_CODE &&
 
1435
                stc <= SLICE_MAX_START_CODE) {
 
1436
                init_get_bits(&s->gb, buf_ptr, input_size);
 
1437
                decode_slice_header(h, &s->gb);
 
1438
            }
 
1439
            break;
 
1440
        }
 
1441
    }
 
1442
}
 
1443
 
 
1444
static int cavs_decode_init(AVCodecContext * avctx) {
 
1445
    AVSContext *h = avctx->priv_data;
 
1446
    MpegEncContext * const s = &h->s;
 
1447
 
 
1448
    MPV_decode_defaults(s);
 
1449
    s->avctx = avctx;
 
1450
 
 
1451
    avctx->pix_fmt= PIX_FMT_YUV420P;
 
1452
 
 
1453
    h->luma_scan[0] = 0;
 
1454
    h->luma_scan[1] = 8;
 
1455
    h->intra_pred_l[      INTRA_L_VERT] = intra_pred_vert;
 
1456
    h->intra_pred_l[     INTRA_L_HORIZ] = intra_pred_horiz;
 
1457
    h->intra_pred_l[        INTRA_L_LP] = intra_pred_lp;
 
1458
    h->intra_pred_l[ INTRA_L_DOWN_LEFT] = intra_pred_down_left;
 
1459
    h->intra_pred_l[INTRA_L_DOWN_RIGHT] = intra_pred_down_right;
 
1460
    h->intra_pred_l[   INTRA_L_LP_LEFT] = intra_pred_lp_left;
 
1461
    h->intra_pred_l[    INTRA_L_LP_TOP] = intra_pred_lp_top;
 
1462
    h->intra_pred_l[    INTRA_L_DC_128] = intra_pred_dc_128;
 
1463
    h->intra_pred_c[        INTRA_C_LP] = intra_pred_lp;
 
1464
    h->intra_pred_c[     INTRA_C_HORIZ] = intra_pred_horiz;
 
1465
    h->intra_pred_c[      INTRA_C_VERT] = intra_pred_vert;
 
1466
    h->intra_pred_c[     INTRA_C_PLANE] = intra_pred_plane;
 
1467
    h->intra_pred_c[   INTRA_C_LP_LEFT] = intra_pred_lp_left;
 
1468
    h->intra_pred_c[    INTRA_C_LP_TOP] = intra_pred_lp_top;
 
1469
    h->intra_pred_c[    INTRA_C_DC_128] = intra_pred_dc_128;
 
1470
    h->mv[ 7] = un_mv;
 
1471
    h->mv[19] = un_mv;
 
1472
    return 0;
 
1473
}
 
1474
 
 
1475
static int cavs_decode_end(AVCodecContext * avctx) {
 
1476
    AVSContext *h = avctx->priv_data;
 
1477
 
 
1478
    av_free(h->top_qp);
 
1479
    av_free(h->top_mv[0]);
 
1480
    av_free(h->top_mv[1]);
 
1481
    av_free(h->top_pred_Y);
 
1482
    av_free(h->top_border_y);
 
1483
    av_free(h->top_border_u);
 
1484
    av_free(h->top_border_v);
 
1485
    av_free(h->col_mv);
 
1486
    av_free(h->col_type_base);
 
1487
    av_free(h->block);
 
1488
    return 0;
 
1489
}
 
1490
 
 
1491
AVCodec cavs_decoder = {
 
1492
    "cavs",
 
1493
    CODEC_TYPE_VIDEO,
 
1494
    CODEC_ID_CAVS,
 
1495
    sizeof(AVSContext),
 
1496
    cavs_decode_init,
 
1497
    NULL,
 
1498
    cavs_decode_end,
 
1499
    cavs_decode_frame,
 
1500
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
 
1501
    .flush= ff_cavs_flush,
 
1502
};