Statistics
| Branch: | Revision:

ffmpeg / libavcodec / cavs.c @ b88e2114

History | View | Annotate | Download (52.7 KB)

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 "cavs.h"
33
#ifdef CONFIG_CAVS_DECODER
34
#include "cavsdata.h"
35

    
36
typedef struct {
37
    MpegEncContext s;
38
    Picture picture; ///< currently decoded frame
39
    Picture DPB[2];  ///< reference frames
40
    int dist[2];     ///< temporal distances from current frame to ref frames
41
    int profile, level;
42
    int aspect_ratio;
43
    int mb_width, mb_height;
44
    int pic_type;
45
    int progressive;
46
    int pic_structure;
47
    int skip_mode_flag; ///< select between skip_count or one skip_flag per MB
48
    int loop_filter_disable;
49
    int alpha_offset, beta_offset;
50
    int ref_flag;
51
    int mbx, mby;      ///< macroblock coordinates
52
    int flags;         ///< availability flags of neighbouring macroblocks
53
    int stc;           ///< last start code
54
    uint8_t *cy, *cu, *cv; ///< current MB sample pointers
55
    int left_qp;
56
    uint8_t *top_qp;
57

    
58
    /** mv motion vector cache
59
       0:    D3  B2  B3  C2
60
       4:    A1  X0  X1   -
61
       8:    A3  X2  X3   -
62

63
       X are the vectors in the current macroblock (5,6,9,10)
64
       A is the macroblock to the left (4,8)
65
       B is the macroblock to the top (1,2)
66
       C is the macroblock to the top-right (3)
67
       D is the macroblock to the top-left (0)
68

69
       the same is repeated for backward motion vectors */
70
    vector_t mv[2*4*3];
71
    vector_t *top_mv[2];
72
    vector_t *col_mv;
73

    
74
    /** luma pred mode cache
75
       0:    --  B2  B3
76
       3:    A1  X0  X1
77
       6:    A3  X2  X3   */
78
    int pred_mode_Y[3*3];
79
    int *top_pred_Y;
80
    int l_stride, c_stride;
81
    int luma_scan[4];
82
    int qp;
83
    int qp_fixed;
84
    int cbp;
85
    ScanTable scantable;
86

    
87
    /** intra prediction is done with un-deblocked samples
88
     they are saved here before deblocking the MB  */
89
    uint8_t *top_border_y, *top_border_u, *top_border_v;
90
    uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
91
    uint8_t intern_border_y[26];
92
    uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
93

    
94
    void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
95
    void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
96
    uint8_t *col_type_base;
97
    uint8_t *col_type;
98

    
99
    /* scaling factors for MV prediction */
100
    int sym_factor;    ///< for scaling in symmetrical B block
101
    int direct_den[2]; ///< for scaling in direct B block
102
    int scale_den[2];  ///< for scaling neighbouring MVs
103

    
104
    int got_keyframe;
105
    DCTELEM *block;
106
} AVSContext;
107

    
108
/*****************************************************************************
109
 *
110
 * in-loop deblocking filter
111
 *
112
 ****************************************************************************/
113

    
114
static inline int get_bs(vector_t *mvP, vector_t *mvQ, int b) {
115
    if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA))
116
        return 2;
117
    if( (abs(mvP->x - mvQ->x) >= 4) ||  (abs(mvP->y - mvQ->y) >= 4) )
118
        return 1;
119
    if(b){
120
        mvP += MV_BWD_OFFS;
121
        mvQ += MV_BWD_OFFS;
122
        if( (abs(mvP->x - mvQ->x) >= 4) ||  (abs(mvP->y - mvQ->y) >= 4) )
123
            return 1;
124
    }else{
125
        if(mvP->ref != mvQ->ref)
126
            return 1;
127
    }
128
    return 0;
129
}
130

    
131
#define SET_PARAMS                                            \
132
    alpha = alpha_tab[av_clip(qp_avg + h->alpha_offset,0,63)];   \
133
    beta  =  beta_tab[av_clip(qp_avg + h->beta_offset, 0,63)];   \
134
    tc    =    tc_tab[av_clip(qp_avg + h->alpha_offset,0,63)];
135

    
136
/**
137
 * in-loop deblocking filter for a single macroblock
138
 *
139
 * boundary strength (bs) mapping:
140
 *
141
 * --4---5--
142
 * 0   2   |
143
 * | 6 | 7 |
144
 * 1   3   |
145
 * ---------
146
 *
147
 */
148
static void filter_mb(AVSContext *h, enum mb_t mb_type) {
149
    DECLARE_ALIGNED_8(uint8_t, bs[8]);
150
    int qp_avg, alpha, beta, tc;
151
    int i;
152

    
153
    /* save un-deblocked lines */
154
    h->topleft_border_y = h->top_border_y[h->mbx*16+15];
155
    h->topleft_border_u = h->top_border_u[h->mbx*10+8];
156
    h->topleft_border_v = h->top_border_v[h->mbx*10+8];
157
    memcpy(&h->top_border_y[h->mbx*16], h->cy + 15* h->l_stride,16);
158
    memcpy(&h->top_border_u[h->mbx*10+1], h->cu +  7* h->c_stride,8);
159
    memcpy(&h->top_border_v[h->mbx*10+1], h->cv +  7* h->c_stride,8);
160
    for(i=0;i<8;i++) {
161
        h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+0)*h->l_stride);
162
        h->left_border_y[i*2+2] = *(h->cy + 15 + (i*2+1)*h->l_stride);
163
        h->left_border_u[i+1] = *(h->cu + 7 + i*h->c_stride);
164
        h->left_border_v[i+1] = *(h->cv + 7 + i*h->c_stride);
165
    }
166
    if(!h->loop_filter_disable) {
167
        /* determine bs */
168
        if(mb_type == I_8X8)
169
            *((uint64_t *)bs) = 0x0202020202020202ULL;
170
        else{
171
            *((uint64_t *)bs) = 0;
172
            if(partition_flags[mb_type] & SPLITV){
173
                bs[2] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1], mb_type > P_8X8);
174
                bs[3] = get_bs(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3], mb_type > P_8X8);
175
            }
176
            if(partition_flags[mb_type] & SPLITH){
177
                bs[6] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2], mb_type > P_8X8);
178
                bs[7] = get_bs(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3], mb_type > P_8X8);
179
            }
180
            bs[0] = get_bs(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0], mb_type > P_8X8);
181
            bs[1] = get_bs(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2], mb_type > P_8X8);
182
            bs[4] = get_bs(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0], mb_type > P_8X8);
183
            bs[5] = get_bs(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1], mb_type > P_8X8);
184
        }
185
        if( *((uint64_t *)bs) ) {
186
            if(h->flags & A_AVAIL) {
187
                qp_avg = (h->qp + h->left_qp + 1) >> 1;
188
                SET_PARAMS;
189
                h->s.dsp.cavs_filter_lv(h->cy,h->l_stride,alpha,beta,tc,bs[0],bs[1]);
190
                h->s.dsp.cavs_filter_cv(h->cu,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
191
                h->s.dsp.cavs_filter_cv(h->cv,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
192
            }
193
            qp_avg = h->qp;
194
            SET_PARAMS;
195
            h->s.dsp.cavs_filter_lv(h->cy + 8,h->l_stride,alpha,beta,tc,bs[2],bs[3]);
196
            h->s.dsp.cavs_filter_lh(h->cy + 8*h->l_stride,h->l_stride,alpha,beta,tc,
197
                           bs[6],bs[7]);
198

    
199
            if(h->flags & B_AVAIL) {
200
                qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1;
201
                SET_PARAMS;
202
                h->s.dsp.cavs_filter_lh(h->cy,h->l_stride,alpha,beta,tc,bs[4],bs[5]);
203
                h->s.dsp.cavs_filter_ch(h->cu,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
204
                h->s.dsp.cavs_filter_ch(h->cv,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
205
            }
206
        }
207
    }
208
    h->left_qp = h->qp;
209
    h->top_qp[h->mbx] = h->qp;
210
}
211

    
212
#undef SET_PARAMS
213

    
214
/*****************************************************************************
215
 *
216
 * spatial intra prediction
217
 *
218
 ****************************************************************************/
219

    
220
static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top,
221
                                        uint8_t **left, int block) {
222
    int i;
223

    
224
    switch(block) {
225
    case 0:
226
        *left = h->left_border_y;
227
        h->left_border_y[0] = h->left_border_y[1];
228
        memset(&h->left_border_y[17],h->left_border_y[16],9);
229
        memcpy(&top[1],&h->top_border_y[h->mbx*16],16);
230
        top[17] = top[16];
231
        top[0] = top[1];
232
        if((h->flags & A_AVAIL) && (h->flags & B_AVAIL))
233
            h->left_border_y[0] = top[0] = h->topleft_border_y;
234
        break;
235
    case 1:
236
        *left = h->intern_border_y;
237
        for(i=0;i<8;i++)
238
            h->intern_border_y[i+1] = *(h->cy + 7 + i*h->l_stride);
239
        memset(&h->intern_border_y[9],h->intern_border_y[8],9);
240
        h->intern_border_y[0] = h->intern_border_y[1];
241
        memcpy(&top[1],&h->top_border_y[h->mbx*16+8],8);
242
        if(h->flags & C_AVAIL)
243
            memcpy(&top[9],&h->top_border_y[(h->mbx + 1)*16],8);
244
        else
245
            memset(&top[9],top[8],9);
246
        top[17] = top[16];
247
        top[0] = top[1];
248
        if(h->flags & B_AVAIL)
249
            h->intern_border_y[0] = top[0] = h->top_border_y[h->mbx*16+7];
250
        break;
251
    case 2:
252
        *left = &h->left_border_y[8];
253
        memcpy(&top[1],h->cy + 7*h->l_stride,16);
254
        top[17] = top[16];
255
        top[0] = top[1];
256
        if(h->flags & A_AVAIL)
257
            top[0] = h->left_border_y[8];
258
        break;
259
    case 3:
260
        *left = &h->intern_border_y[8];
261
        for(i=0;i<8;i++)
262
            h->intern_border_y[i+9] = *(h->cy + 7 + (i+8)*h->l_stride);
263
        memset(&h->intern_border_y[17],h->intern_border_y[16],9);
264
        memcpy(&top[0],h->cy + 7 + 7*h->l_stride,9);
265
        memset(&top[9],top[8],9);
266
        break;
267
    }
268
}
269

    
270
static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
271
    int y;
272
    uint64_t a = unaligned64(&top[1]);
273
    for(y=0;y<8;y++) {
274
        *((uint64_t *)(d+y*stride)) = a;
275
    }
276
}
277

    
278
static void intra_pred_horiz(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
279
    int y;
280
    uint64_t a;
281
    for(y=0;y<8;y++) {
282
        a = left[y+1] * 0x0101010101010101ULL;
283
        *((uint64_t *)(d+y*stride)) = a;
284
    }
285
}
286

    
287
static void intra_pred_dc_128(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
288
    int y;
289
    uint64_t a = 0x8080808080808080ULL;
290
    for(y=0;y<8;y++)
291
        *((uint64_t *)(d+y*stride)) = a;
292
}
293

    
294
static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
295
    int x,y,ia;
296
    int ih = 0;
297
    int iv = 0;
298
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
299

    
300
    for(x=0; x<4; x++) {
301
        ih += (x+1)*(top[5+x]-top[3-x]);
302
        iv += (x+1)*(left[5+x]-left[3-x]);
303
    }
304
    ia = (top[8]+left[8])<<4;
305
    ih = (17*ih+16)>>5;
306
    iv = (17*iv+16)>>5;
307
    for(y=0; y<8; y++)
308
        for(x=0; x<8; x++)
309
            d[y*stride+x] = cm[(ia+(x-3)*ih+(y-3)*iv+16)>>5];
310
}
311

    
312
#define LOWPASS(ARRAY,INDEX)                                            \
313
    (( ARRAY[(INDEX)-1] + 2*ARRAY[(INDEX)] + ARRAY[(INDEX)+1] + 2) >> 2)
314

    
315
static void intra_pred_lp(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
316
    int x,y;
317
    for(y=0; y<8; y++)
318
        for(x=0; x<8; x++)
319
            d[y*stride+x] = (LOWPASS(top,x+1) + LOWPASS(left,y+1)) >> 1;
320
}
321

    
322
static void intra_pred_down_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
323
    int x,y;
324
    for(y=0; y<8; y++)
325
        for(x=0; x<8; x++)
326
            d[y*stride+x] = (LOWPASS(top,x+y+2) + LOWPASS(left,x+y+2)) >> 1;
327
}
328

    
329
static void intra_pred_down_right(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
330
    int x,y;
331
    for(y=0; y<8; y++)
332
        for(x=0; x<8; x++)
333
            if(x==y)
334
                d[y*stride+x] = (left[1]+2*top[0]+top[1]+2)>>2;
335
            else if(x>y)
336
                d[y*stride+x] = LOWPASS(top,x-y);
337
            else
338
                d[y*stride+x] = LOWPASS(left,y-x);
339
}
340

    
341
static void intra_pred_lp_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
342
    int x,y;
343
    for(y=0; y<8; y++)
344
        for(x=0; x<8; x++)
345
            d[y*stride+x] = LOWPASS(left,y+1);
346
}
347

    
348
static void intra_pred_lp_top(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
349
    int x,y;
350
    for(y=0; y<8; y++)
351
        for(x=0; x<8; x++)
352
            d[y*stride+x] = LOWPASS(top,x+1);
353
}
354

    
355
#undef LOWPASS
356

    
357
static inline void modify_pred(const int_fast8_t *mod_table, int *mode) {
358
    *mode = mod_table[*mode];
359
    if(*mode < 0) {
360
        av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
361
        *mode = 0;
362
    }
363
}
364

    
365
/*****************************************************************************
366
 *
367
 * motion compensation
368
 *
369
 ****************************************************************************/
370

    
371
static inline void mc_dir_part(AVSContext *h,Picture *pic,int square,
372
                        int chroma_height,int delta,int list,uint8_t *dest_y,
373
                        uint8_t *dest_cb,uint8_t *dest_cr,int src_x_offset,
374
                        int src_y_offset,qpel_mc_func *qpix_op,
375
                        h264_chroma_mc_func chroma_op,vector_t *mv){
376
    MpegEncContext * const s = &h->s;
377
    const int mx= mv->x + src_x_offset*8;
378
    const int my= mv->y + src_y_offset*8;
379
    const int luma_xy= (mx&3) + ((my&3)<<2);
380
    uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->l_stride;
381
    uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->c_stride;
382
    uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->c_stride;
383
    int extra_width= 0; //(s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
384
    int extra_height= extra_width;
385
    int emu=0;
386
    const int full_mx= mx>>2;
387
    const int full_my= my>>2;
388
    const int pic_width  = 16*h->mb_width;
389
    const int pic_height = 16*h->mb_height;
390

    
391
    if(!pic->data[0])
392
        return;
393
    if(mx&7) extra_width -= 3;
394
    if(my&7) extra_height -= 3;
395

    
396
    if(   full_mx < 0-extra_width
397
          || full_my < 0-extra_height
398
          || full_mx + 16/*FIXME*/ > pic_width + extra_width
399
          || full_my + 16/*FIXME*/ > pic_height + extra_height){
400
        ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->l_stride, h->l_stride,
401
                            16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
402
        src_y= s->edge_emu_buffer + 2 + 2*h->l_stride;
403
        emu=1;
404
    }
405

    
406
    qpix_op[luma_xy](dest_y, src_y, h->l_stride); //FIXME try variable height perhaps?
407
    if(!square){
408
        qpix_op[luma_xy](dest_y + delta, src_y + delta, h->l_stride);
409
    }
410

    
411
    if(emu){
412
        ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->c_stride,
413
                            9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
414
        src_cb= s->edge_emu_buffer;
415
    }
416
    chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx&7, my&7);
417

    
418
    if(emu){
419
        ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->c_stride,
420
                            9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
421
        src_cr= s->edge_emu_buffer;
422
    }
423
    chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx&7, my&7);
424
}
425

    
426
static inline void mc_part_std(AVSContext *h,int square,int chroma_height,int delta,
427
                        uint8_t *dest_y,uint8_t *dest_cb,uint8_t *dest_cr,
428
                        int x_offset, int y_offset,qpel_mc_func *qpix_put,
429
                        h264_chroma_mc_func chroma_put,qpel_mc_func *qpix_avg,
430
                        h264_chroma_mc_func chroma_avg, vector_t *mv){
431
    qpel_mc_func *qpix_op=  qpix_put;
432
    h264_chroma_mc_func chroma_op= chroma_put;
433

    
434
    dest_y  += 2*x_offset + 2*y_offset*h->l_stride;
435
    dest_cb +=   x_offset +   y_offset*h->c_stride;
436
    dest_cr +=   x_offset +   y_offset*h->c_stride;
437
    x_offset += 8*h->mbx;
438
    y_offset += 8*h->mby;
439

    
440
    if(mv->ref >= 0){
441
        Picture *ref= &h->DPB[mv->ref];
442
        mc_dir_part(h, ref, square, chroma_height, delta, 0,
443
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
444
                    qpix_op, chroma_op, mv);
445

    
446
        qpix_op=  qpix_avg;
447
        chroma_op= chroma_avg;
448
    }
449

    
450
    if((mv+MV_BWD_OFFS)->ref >= 0){
451
        Picture *ref= &h->DPB[0];
452
        mc_dir_part(h, ref, square, chroma_height, delta, 1,
453
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
454
                    qpix_op, chroma_op, mv+MV_BWD_OFFS);
455
    }
456
}
457

    
458
static void inter_pred(AVSContext *h, enum mb_t mb_type) {
459
    if(partition_flags[mb_type] == 0){ // 16x16
460
        mc_part_std(h, 1, 8, 0, h->cy, h->cu, h->cv, 0, 0,
461
                h->s.dsp.put_cavs_qpel_pixels_tab[0],
462
                h->s.dsp.put_h264_chroma_pixels_tab[0],
463
                h->s.dsp.avg_cavs_qpel_pixels_tab[0],
464
                h->s.dsp.avg_h264_chroma_pixels_tab[0],&h->mv[MV_FWD_X0]);
465
    }else{
466
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0,
467
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
468
                h->s.dsp.put_h264_chroma_pixels_tab[1],
469
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
470
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]);
471
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0,
472
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
473
                h->s.dsp.put_h264_chroma_pixels_tab[1],
474
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
475
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]);
476
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4,
477
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
478
                h->s.dsp.put_h264_chroma_pixels_tab[1],
479
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
480
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]);
481
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4,
482
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
483
                h->s.dsp.put_h264_chroma_pixels_tab[1],
484
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
485
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
486
    }
487
    /* set intra prediction modes to default values */
488
    h->pred_mode_Y[3] =  h->pred_mode_Y[6] = INTRA_L_LP;
489
    h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
490
}
491

    
492
/*****************************************************************************
493
 *
494
 * motion vector prediction
495
 *
496
 ****************************************************************************/
497

    
498
static inline void set_mvs(vector_t *mv, enum block_t size) {
499
    switch(size) {
500
    case BLK_16X16:
501
        mv[MV_STRIDE  ] = mv[0];
502
        mv[MV_STRIDE+1] = mv[0];
503
    case BLK_16X8:
504
        mv[1] = mv[0];
505
        break;
506
    case BLK_8X16:
507
        mv[MV_STRIDE] = mv[0];
508
        break;
509
    }
510
}
511

    
512
static inline void store_mvs(AVSContext *h) {
513
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0];
514
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1];
515
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2];
516
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3];
517
}
518

    
519
static inline void scale_mv(AVSContext *h, int *d_x, int *d_y, vector_t *src, int distp) {
520
    int den = h->scale_den[src->ref];
521

    
522
    *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9;
523
    *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9;
524
}
525

    
526
static inline void mv_pred_median(AVSContext *h, vector_t *mvP, vector_t *mvA, vector_t *mvB, vector_t *mvC) {
527
    int ax, ay, bx, by, cx, cy;
528
    int len_ab, len_bc, len_ca, len_mid;
529

    
530
    /* scale candidates according to their temporal span */
531
    scale_mv(h, &ax, &ay, mvA, mvP->dist);
532
    scale_mv(h, &bx, &by, mvB, mvP->dist);
533
    scale_mv(h, &cx, &cy, mvC, mvP->dist);
534
    /* find the geometrical median of the three candidates */
535
    len_ab = abs(ax - bx) + abs(ay - by);
536
    len_bc = abs(bx - cx) + abs(by - cy);
537
    len_ca = abs(cx - ax) + abs(cy - ay);
538
    len_mid = mid_pred(len_ab, len_bc, len_ca);
539
    if(len_mid == len_ab) {
540
        mvP->x = cx;
541
        mvP->y = cy;
542
    } else if(len_mid == len_bc) {
543
        mvP->x = ax;
544
        mvP->y = ay;
545
    } else {
546
        mvP->x = bx;
547
        mvP->y = by;
548
    }
549
}
550

    
551
static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw,
552
                                  vector_t *col_mv) {
553
    vector_t *pmv_bw = pmv_fw + MV_BWD_OFFS;
554
    int den = h->direct_den[col_mv->ref];
555
    int m = col_mv->x >> 31;
556

    
557
    pmv_fw->dist = h->dist[1];
558
    pmv_bw->dist = h->dist[0];
559
    pmv_fw->ref = 1;
560
    pmv_bw->ref = 0;
561
    /* scale the co-located motion vector according to its temporal span */
562
    pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
563
    pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
564
    m = col_mv->y >> 31;
565
    pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
566
    pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
567
}
568

    
569
static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) {
570
    vector_t *dst = src + MV_BWD_OFFS;
571

    
572
    /* backward mv is the scaled and negated forward mv */
573
    dst->x = -((src->x * h->sym_factor + 256) >> 9);
574
    dst->y = -((src->y * h->sym_factor + 256) >> 9);
575
    dst->ref = 0;
576
    dst->dist = h->dist[0];
577
    set_mvs(dst, size);
578
}
579

    
580
static void mv_pred(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
581
                    enum mv_pred_t mode, enum block_t size, int ref) {
582
    vector_t *mvP = &h->mv[nP];
583
    vector_t *mvA = &h->mv[nP-1];
584
    vector_t *mvB = &h->mv[nP-4];
585
    vector_t *mvC = &h->mv[nC];
586
    const vector_t *mvP2 = NULL;
587

    
588
    mvP->ref = ref;
589
    mvP->dist = h->dist[mvP->ref];
590
    if(mvC->ref == NOT_AVAIL)
591
        mvC = &h->mv[nP-5]; // set to top-left (mvD)
592
    if((mode == MV_PRED_PSKIP) &&
593
       ((mvA->ref == NOT_AVAIL) || (mvB->ref == NOT_AVAIL) ||
594
           ((mvA->x | mvA->y | mvA->ref) == 0)  ||
595
           ((mvB->x | mvB->y | mvB->ref) == 0) )) {
596
        mvP2 = &un_mv;
597
    /* if there is only one suitable candidate, take it */
598
    } else if((mvA->ref >= 0) && (mvB->ref < 0) && (mvC->ref < 0)) {
599
        mvP2= mvA;
600
    } else if((mvA->ref < 0) && (mvB->ref >= 0) && (mvC->ref < 0)) {
601
        mvP2= mvB;
602
    } else if((mvA->ref < 0) && (mvB->ref < 0) && (mvC->ref >= 0)) {
603
        mvP2= mvC;
604
    } else if(mode == MV_PRED_LEFT     && mvA->ref == ref){
605
        mvP2= mvA;
606
    } else if(mode == MV_PRED_TOP      && mvB->ref == ref){
607
        mvP2= mvB;
608
    } else if(mode == MV_PRED_TOPRIGHT && mvC->ref == ref){
609
        mvP2= mvC;
610
    }
611
    if(mvP2){
612
        mvP->x = mvP2->x;
613
        mvP->y = mvP2->y;
614
    }else
615
        mv_pred_median(h, mvP, mvA, mvB, mvC);
616

    
617
    if(mode < MV_PRED_PSKIP) {
618
        mvP->x += get_se_golomb(&h->s.gb);
619
        mvP->y += get_se_golomb(&h->s.gb);
620
    }
621
    set_mvs(mvP,size);
622
}
623

    
624
/*****************************************************************************
625
 *
626
 * residual data decoding
627
 *
628
 ****************************************************************************/
629

    
630
/** kth-order exponential golomb code */
631
static inline int get_ue_code(GetBitContext *gb, int order) {
632
    if(order) {
633
        int ret = get_ue_golomb(gb) << order;
634
        return ret + get_bits(gb,order);
635
    }
636
    return get_ue_golomb(gb);
637
}
638

    
639
/**
640
 * decode coefficients from one 8x8 block, dequantize, inverse transform
641
 *  and add them to sample block
642
 * @param r pointer to 2D VLC table
643
 * @param esc_golomb_order escape codes are k-golomb with this order k
644
 * @param qp quantizer
645
 * @param dst location of sample block
646
 * @param stride line stride in frame buffer
647
 */
648
static int decode_residual_block(AVSContext *h, GetBitContext *gb,
649
                                 const residual_vlc_t *r, int esc_golomb_order,
650
                                 int qp, uint8_t *dst, int stride) {
651
    int i,pos = -1;
652
    int level_code, esc_code, level, run, mask;
653
    int level_buf[64];
654
    int run_buf[64];
655
    int dqm = dequant_mul[qp];
656
    int dqs = dequant_shift[qp];
657
    int dqa = 1 << (dqs - 1);
658
    const uint8_t *scantab = h->scantable.permutated;
659
    DCTELEM *block = h->block;
660

    
661
    for(i=0;i<65;i++) {
662
        level_code = get_ue_code(gb,r->golomb_order);
663
        if(level_code >= ESCAPE_CODE) {
664
            run = ((level_code - ESCAPE_CODE) >> 1) + 1;
665
            esc_code = get_ue_code(gb,esc_golomb_order);
666
            level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
667
            while(level > r->inc_limit)
668
                r++;
669
            mask = -(level_code & 1);
670
            level = (level^mask) - mask;
671
        } else {
672
            level = r->rltab[level_code][0];
673
            if(!level) //end of block signal
674
                break;
675
            run   = r->rltab[level_code][1];
676
            r += r->rltab[level_code][2];
677
        }
678
        level_buf[i] = level;
679
        run_buf[i] = run;
680
    }
681
    /* inverse scan and dequantization */
682
    while(--i >= 0){
683
        pos += run_buf[i];
684
        if(pos > 63) {
685
            av_log(h->s.avctx, AV_LOG_ERROR,
686
                   "position out of block bounds at pic %d MB(%d,%d)\n",
687
                   h->picture.poc, h->mbx, h->mby);
688
            return -1;
689
        }
690
        block[scantab[pos]] = (level_buf[i]*dqm + dqa) >> dqs;
691
    }
692
    h->s.dsp.cavs_idct8_add(dst,block,stride);
693
    return 0;
694
}
695

    
696

    
697
static inline void decode_residual_chroma(AVSContext *h) {
698
    if(h->cbp & (1<<4))
699
        decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
700
                              h->cu,h->c_stride);
701
    if(h->cbp & (1<<5))
702
        decode_residual_block(h,&h->s.gb,chroma_2dvlc,0, chroma_qp[h->qp],
703
                              h->cv,h->c_stride);
704
}
705

    
706
static inline int decode_residual_inter(AVSContext *h) {
707
    int block;
708

    
709
    /* get coded block pattern */
710
    int cbp= get_ue_golomb(&h->s.gb);
711
    if(cbp > 63){
712
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
713
        return -1;
714
    }
715
    h->cbp = cbp_tab[cbp][1];
716

    
717
    /* get quantizer */
718
    if(h->cbp && !h->qp_fixed)
719
        h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
720
    for(block=0;block<4;block++)
721
        if(h->cbp & (1<<block))
722
            decode_residual_block(h,&h->s.gb,inter_2dvlc,0,h->qp,
723
                                  h->cy + h->luma_scan[block], h->l_stride);
724
    decode_residual_chroma(h);
725

    
726
    return 0;
727
}
728

    
729
/*****************************************************************************
730
 *
731
 * macroblock level
732
 *
733
 ****************************************************************************/
734

    
735
/**
736
 * initialise predictors for motion vectors and intra prediction
737
 */
738
static inline void init_mb(AVSContext *h) {
739
    int i;
740

    
741
    /* copy predictors from top line (MB B and C) into cache */
742
    for(i=0;i<3;i++) {
743
        h->mv[MV_FWD_B2+i] = h->top_mv[0][h->mbx*2+i];
744
        h->mv[MV_BWD_B2+i] = h->top_mv[1][h->mbx*2+i];
745
    }
746
    h->pred_mode_Y[1] = h->top_pred_Y[h->mbx*2+0];
747
    h->pred_mode_Y[2] = h->top_pred_Y[h->mbx*2+1];
748
    /* clear top predictors if MB B is not available */
749
    if(!(h->flags & B_AVAIL)) {
750
        h->mv[MV_FWD_B2] = un_mv;
751
        h->mv[MV_FWD_B3] = un_mv;
752
        h->mv[MV_BWD_B2] = un_mv;
753
        h->mv[MV_BWD_B3] = un_mv;
754
        h->pred_mode_Y[1] = h->pred_mode_Y[2] = NOT_AVAIL;
755
        h->flags &= ~(C_AVAIL|D_AVAIL);
756
    } else if(h->mbx) {
757
        h->flags |= D_AVAIL;
758
    }
759
    if(h->mbx == h->mb_width-1) //MB C not available
760
        h->flags &= ~C_AVAIL;
761
    /* clear top-right predictors if MB C is not available */
762
    if(!(h->flags & C_AVAIL)) {
763
        h->mv[MV_FWD_C2] = un_mv;
764
        h->mv[MV_BWD_C2] = un_mv;
765
    }
766
    /* clear top-left predictors if MB D is not available */
767
    if(!(h->flags & D_AVAIL)) {
768
        h->mv[MV_FWD_D3] = un_mv;
769
        h->mv[MV_BWD_D3] = un_mv;
770
    }
771
    /* set pointer for co-located macroblock type */
772
    h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx];
773
}
774

    
775
static inline void check_for_slice(AVSContext *h);
776

    
777
/**
778
 * save predictors for later macroblocks and increase
779
 * macroblock address
780
 * @returns 0 if end of frame is reached, 1 otherwise
781
 */
782
static inline int next_mb(AVSContext *h) {
783
    int i;
784

    
785
    h->flags |= A_AVAIL;
786
    h->cy += 16;
787
    h->cu += 8;
788
    h->cv += 8;
789
    /* copy mvs as predictors to the left */
790
    for(i=0;i<=20;i+=4)
791
        h->mv[i] = h->mv[i+2];
792
    /* copy bottom mvs from cache to top line */
793
    h->top_mv[0][h->mbx*2+0] = h->mv[MV_FWD_X2];
794
    h->top_mv[0][h->mbx*2+1] = h->mv[MV_FWD_X3];
795
    h->top_mv[1][h->mbx*2+0] = h->mv[MV_BWD_X2];
796
    h->top_mv[1][h->mbx*2+1] = h->mv[MV_BWD_X3];
797
    /* next MB address */
798
    h->mbx++;
799
    if(h->mbx == h->mb_width) { //new mb line
800
        h->flags = B_AVAIL|C_AVAIL;
801
        /* clear left pred_modes */
802
        h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
803
        /* clear left mv predictors */
804
        for(i=0;i<=20;i+=4)
805
            h->mv[i] = un_mv;
806
        h->mbx = 0;
807
        h->mby++;
808
        /* re-calculate sample pointers */
809
        h->cy = h->picture.data[0] + h->mby*16*h->l_stride;
810
        h->cu = h->picture.data[1] + h->mby*8*h->c_stride;
811
        h->cv = h->picture.data[2] + h->mby*8*h->c_stride;
812
        if(h->mby == h->mb_height) { //frame end
813
            return 0;
814
        } else {
815
            //check_for_slice(h);
816
        }
817
    }
818
    return 1;
819
}
820

    
821
static int decode_mb_i(AVSContext *h, int cbp_code) {
822
    GetBitContext *gb = &h->s.gb;
823
    int block, pred_mode_uv;
824
    uint8_t top[18];
825
    uint8_t *left = NULL;
826
    uint8_t *d;
827

    
828
    init_mb(h);
829

    
830
    /* get intra prediction modes from stream */
831
    for(block=0;block<4;block++) {
832
        int nA,nB,predpred;
833
        int pos = scan3x3[block];
834

    
835
        nA = h->pred_mode_Y[pos-1];
836
        nB = h->pred_mode_Y[pos-3];
837
        predpred = FFMIN(nA,nB);
838
        if(predpred == NOT_AVAIL) // if either is not available
839
            predpred = INTRA_L_LP;
840
        if(!get_bits1(gb)){
841
            int rem_mode= get_bits(gb, 2);
842
            predpred = rem_mode + (rem_mode >= predpred);
843
        }
844
        h->pred_mode_Y[pos] = predpred;
845
    }
846
    pred_mode_uv = get_ue_golomb(gb);
847
    if(pred_mode_uv > 6) {
848
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
849
        return -1;
850
    }
851

    
852
    /* save pred modes before they get modified */
853
    h->pred_mode_Y[3] =  h->pred_mode_Y[5];
854
    h->pred_mode_Y[6] =  h->pred_mode_Y[8];
855
    h->top_pred_Y[h->mbx*2+0] = h->pred_mode_Y[7];
856
    h->top_pred_Y[h->mbx*2+1] = h->pred_mode_Y[8];
857

    
858
    /* modify pred modes according to availability of neighbour samples */
859
    if(!(h->flags & A_AVAIL)) {
860
        modify_pred(left_modifier_l, &h->pred_mode_Y[4] );
861
        modify_pred(left_modifier_l, &h->pred_mode_Y[7] );
862
        modify_pred(left_modifier_c, &pred_mode_uv );
863
    }
864
    if(!(h->flags & B_AVAIL)) {
865
        modify_pred(top_modifier_l, &h->pred_mode_Y[4] );
866
        modify_pred(top_modifier_l, &h->pred_mode_Y[5] );
867
        modify_pred(top_modifier_c, &pred_mode_uv );
868
    }
869

    
870
    /* get coded block pattern */
871
    if(h->pic_type == FF_I_TYPE)
872
        cbp_code = get_ue_golomb(gb);
873
    if(cbp_code > 63){
874
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
875
        return -1;
876
    }
877
    h->cbp = cbp_tab[cbp_code][0];
878
    if(h->cbp && !h->qp_fixed)
879
        h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
880

    
881
    /* luma intra prediction interleaved with residual decode/transform/add */
882
    for(block=0;block<4;block++) {
883
        d = h->cy + h->luma_scan[block];
884
        load_intra_pred_luma(h, top, &left, block);
885
        h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
886
            (d, top, left, h->l_stride);
887
        if(h->cbp & (1<<block))
888
            decode_residual_block(h,gb,intra_2dvlc,1,h->qp,d,h->l_stride);
889
    }
890

    
891
    /* chroma intra prediction */
892
    /* extend borders by one pixel */
893
    h->left_border_u[9] = h->left_border_u[8];
894
    h->left_border_v[9] = h->left_border_v[8];
895
    h->top_border_u[h->mbx*10+9] = h->top_border_u[h->mbx*10+8];
896
    h->top_border_v[h->mbx*10+9] = h->top_border_v[h->mbx*10+8];
897
    if(h->mbx && h->mby) {
898
        h->top_border_u[h->mbx*10] = h->left_border_u[0] = h->topleft_border_u;
899
        h->top_border_v[h->mbx*10] = h->left_border_v[0] = h->topleft_border_v;
900
    } else {
901
        h->left_border_u[0] = h->left_border_u[1];
902
        h->left_border_v[0] = h->left_border_v[1];
903
        h->top_border_u[h->mbx*10] = h->top_border_u[h->mbx*10+1];
904
        h->top_border_v[h->mbx*10] = h->top_border_v[h->mbx*10+1];
905
    }
906
    h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
907
                                  h->left_border_u, h->c_stride);
908
    h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
909
                                  h->left_border_v, h->c_stride);
910

    
911
    decode_residual_chroma(h);
912
    filter_mb(h,I_8X8);
913

    
914
    /* mark motion vectors as intra */
915
    h->mv[MV_FWD_X0] = intra_mv;
916
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
917
    h->mv[MV_BWD_X0] = intra_mv;
918
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
919
    if(h->pic_type != FF_B_TYPE)
920
        *h->col_type = I_8X8;
921

    
922
    return 0;
923
}
924

    
925
static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
926
    GetBitContext *gb = &h->s.gb;
927
    int ref[4];
928

    
929
    init_mb(h);
930
    switch(mb_type) {
931
    case P_SKIP:
932
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP, BLK_16X16, 0);
933
        break;
934
    case P_16X16:
935
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
936
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_16X16,ref[0]);
937
        break;
938
    case P_16X8:
939
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
940
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
941
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,      BLK_16X8, ref[0]);
942
        mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT,     BLK_16X8, ref[2]);
943
        break;
944
    case P_8X16:
945
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
946
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
947
        mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT,     BLK_8X16, ref[0]);
948
        mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, ref[1]);
949
        break;
950
    case P_8X8:
951
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
952
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
953
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
954
        ref[3] = h->ref_flag ? 0 : get_bits1(gb);
955
        mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN,   BLK_8X8, ref[0]);
956
        mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_8X8, ref[1]);
957
        mv_pred(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN,   BLK_8X8, ref[2]);
958
        mv_pred(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN,   BLK_8X8, ref[3]);
959
    }
960
    inter_pred(h, mb_type);
961
    store_mvs(h);
962
    if(mb_type != P_SKIP)
963
        decode_residual_inter(h);
964
    filter_mb(h,mb_type);
965
    *h->col_type = mb_type;
966
}
967

    
968
static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
969
    int block;
970
    enum sub_mb_t sub_type[4];
971
    int flags;
972

    
973
    init_mb(h);
974

    
975
    /* reset all MVs */
976
    h->mv[MV_FWD_X0] = dir_mv;
977
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
978
    h->mv[MV_BWD_X0] = dir_mv;
979
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
980
    switch(mb_type) {
981
    case B_SKIP:
982
    case B_DIRECT:
983
        if(!(*h->col_type)) {
984
            /* intra MB at co-location, do in-plane prediction */
985
            mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
986
            mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
987
        } else
988
            /* direct prediction from co-located P MB, block-wise */
989
            for(block=0;block<4;block++)
990
                mv_pred_direct(h,&h->mv[mv_scan[block]],
991
                            &h->col_mv[(h->mby*h->mb_width+h->mbx)*4 + block]);
992
        break;
993
    case B_FWD_16X16:
994
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
995
        break;
996
    case B_SYM_16X16:
997
        mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
998
        mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
999
        break;
1000
    case B_BWD_16X16:
1001
        mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
1002
        break;
1003
    case B_8X8:
1004
        for(block=0;block<4;block++)
1005
            sub_type[block] = get_bits(&h->s.gb,2);
1006
        for(block=0;block<4;block++) {
1007
            switch(sub_type[block]) {
1008
            case B_SUB_DIRECT:
1009
                if(!(*h->col_type)) {
1010
                    /* intra MB at co-location, do in-plane prediction */
1011
                    mv_pred(h, mv_scan[block], mv_scan[block]-3,
1012
                            MV_PRED_BSKIP, BLK_8X8, 1);
1013
                    mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
1014
                            mv_scan[block]-3+MV_BWD_OFFS,
1015
                            MV_PRED_BSKIP, BLK_8X8, 0);
1016
                } else
1017
                    mv_pred_direct(h,&h->mv[mv_scan[block]],
1018
                                   &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]);
1019
                break;
1020
            case B_SUB_FWD:
1021
                mv_pred(h, mv_scan[block], mv_scan[block]-3,
1022
                        MV_PRED_MEDIAN, BLK_8X8, 1);
1023
                break;
1024
            case B_SUB_SYM:
1025
                mv_pred(h, mv_scan[block], mv_scan[block]-3,
1026
                        MV_PRED_MEDIAN, BLK_8X8, 1);
1027
                mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
1028
                break;
1029
            }
1030
        }
1031
        for(block=0;block<4;block++) {
1032
            if(sub_type[block] == B_SUB_BWD)
1033
                mv_pred(h, mv_scan[block]+MV_BWD_OFFS,
1034
                        mv_scan[block]+MV_BWD_OFFS-3,
1035
                        MV_PRED_MEDIAN, BLK_8X8, 0);
1036
        }
1037
        break;
1038
    default:
1039
        assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
1040
        flags = partition_flags[mb_type];
1041
        if(mb_type & 1) { /* 16x8 macroblock types */
1042
            if(flags & FWD0)
1043
                mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,  BLK_16X8, 1);
1044
            if(flags & SYM0)
1045
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
1046
            if(flags & FWD1)
1047
                mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
1048
            if(flags & SYM1)
1049
                mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
1050
            if(flags & BWD0)
1051
                mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP,  BLK_16X8, 0);
1052
            if(flags & BWD1)
1053
                mv_pred(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
1054
        } else {          /* 8x16 macroblock types */
1055
            if(flags & FWD0)
1056
                mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
1057
            if(flags & SYM0)
1058
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
1059
            if(flags & FWD1)
1060
                mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
1061
            if(flags & SYM1)
1062
                mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
1063
            if(flags & BWD0)
1064
                mv_pred(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
1065
            if(flags & BWD1)
1066
                mv_pred(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 0);
1067
        }
1068
    }
1069
    inter_pred(h, mb_type);
1070
    if(mb_type != B_SKIP)
1071
        decode_residual_inter(h);
1072
    filter_mb(h,mb_type);
1073
}
1074

    
1075
/*****************************************************************************
1076
 *
1077
 * slice level
1078
 *
1079
 ****************************************************************************/
1080

    
1081
static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
1082
    if(h->stc > 0xAF)
1083
        av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
1084
    h->mby = h->stc;
1085
    if((h->mby == 0) && (!h->qp_fixed)){
1086
        h->qp_fixed = get_bits1(gb);
1087
        h->qp = get_bits(gb,6);
1088
    }
1089
    /* inter frame or second slice can have weighting params */
1090
    if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
1091
        if(get_bits1(gb)) { //slice_weighting_flag
1092
            av_log(h->s.avctx, AV_LOG_ERROR,
1093
                   "weighted prediction not yet supported\n");
1094
        }
1095
    return 0;
1096
}
1097

    
1098
static inline void check_for_slice(AVSContext *h) {
1099
    GetBitContext *gb = &h->s.gb;
1100
    int align;
1101
    align = (-get_bits_count(gb)) & 7;
1102
    if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
1103
        get_bits_long(gb,24+align);
1104
        h->stc = get_bits(gb,8);
1105
        decode_slice_header(h,gb);
1106
    }
1107
}
1108

    
1109
/*****************************************************************************
1110
 *
1111
 * frame level
1112
 *
1113
 ****************************************************************************/
1114

    
1115
static void init_pic(AVSContext *h) {
1116
    int i;
1117

    
1118
    /* clear some predictors */
1119
    for(i=0;i<=20;i+=4)
1120
        h->mv[i] = un_mv;
1121
    h->mv[MV_BWD_X0] = dir_mv;
1122
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
1123
    h->mv[MV_FWD_X0] = dir_mv;
1124
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
1125
    h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
1126
    h->cy = h->picture.data[0];
1127
    h->cu = h->picture.data[1];
1128
    h->cv = h->picture.data[2];
1129
    h->l_stride = h->picture.linesize[0];
1130
    h->c_stride = h->picture.linesize[1];
1131
    h->luma_scan[2] = 8*h->l_stride;
1132
    h->luma_scan[3] = 8*h->l_stride+8;
1133
    h->mbx = h->mby = 0;
1134
    h->flags = 0;
1135
}
1136

    
1137
static int decode_pic(AVSContext *h) {
1138
    MpegEncContext *s = &h->s;
1139
    int skip_count;
1140
    enum mb_t mb_type;
1141

    
1142
    if (!s->context_initialized) {
1143
        s->avctx->idct_algo = FF_IDCT_CAVS;
1144
        if (MPV_common_init(s) < 0)
1145
            return -1;
1146
        ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
1147
    }
1148
    get_bits(&s->gb,16);//bbv_dwlay
1149
    if(h->stc == PIC_PB_START_CODE) {
1150
        h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
1151
        if(h->pic_type > FF_B_TYPE) {
1152
            av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
1153
            return -1;
1154
        }
1155
        /* make sure we have the reference frames we need */
1156
        if(!h->DPB[0].data[0] ||
1157
          (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
1158
            return -1;
1159
    } else {
1160
        h->pic_type = FF_I_TYPE;
1161
        if(get_bits1(&s->gb))
1162
            get_bits(&s->gb,16);//time_code
1163
    }
1164
    /* release last B frame */
1165
    if(h->picture.data[0])
1166
        s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
1167

    
1168
    s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
1169
    init_pic(h);
1170
    h->picture.poc = get_bits(&s->gb,8)*2;
1171

    
1172
    /* get temporal distances and MV scaling factors */
1173
    if(h->pic_type != FF_B_TYPE) {
1174
        h->dist[0] = (h->picture.poc - h->DPB[0].poc  + 512) % 512;
1175
    } else {
1176
        h->dist[0] = (h->DPB[0].poc  - h->picture.poc + 512) % 512;
1177
    }
1178
    h->dist[1] = (h->picture.poc - h->DPB[1].poc  + 512) % 512;
1179
    h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
1180
    h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
1181
    if(h->pic_type == FF_B_TYPE) {
1182
        h->sym_factor = h->dist[0]*h->scale_den[1];
1183
    } else {
1184
        h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
1185
        h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
1186
    }
1187

    
1188
    if(s->low_delay)
1189
        get_ue_golomb(&s->gb); //bbv_check_times
1190
    h->progressive             = get_bits1(&s->gb);
1191
    if(h->progressive)
1192
        h->pic_structure = 1;
1193
    else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) )
1194
        get_bits1(&s->gb);     //advanced_pred_mode_disable
1195
    skip_bits1(&s->gb);        //top_field_first
1196
    skip_bits1(&s->gb);        //repeat_first_field
1197
    h->qp_fixed                = get_bits1(&s->gb);
1198
    h->qp                      = get_bits(&s->gb,6);
1199
    if(h->pic_type == FF_I_TYPE) {
1200
        if(!h->progressive && !h->pic_structure)
1201
            skip_bits1(&s->gb);//what is this?
1202
        skip_bits(&s->gb,4);   //reserved bits
1203
    } else {
1204
        if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
1205
            h->ref_flag        = get_bits1(&s->gb);
1206
        skip_bits(&s->gb,4);   //reserved bits
1207
        h->skip_mode_flag      = get_bits1(&s->gb);
1208
    }
1209
    h->loop_filter_disable     = get_bits1(&s->gb);
1210
    if(!h->loop_filter_disable && get_bits1(&s->gb)) {
1211
        h->alpha_offset        = get_se_golomb(&s->gb);
1212
        h->beta_offset         = get_se_golomb(&s->gb);
1213
    } else {
1214
        h->alpha_offset = h->beta_offset  = 0;
1215
    }
1216
    check_for_slice(h);
1217
    if(h->pic_type == FF_I_TYPE) {
1218
        do {
1219
            decode_mb_i(h, 0);
1220
        } while(next_mb(h));
1221
    } else if(h->pic_type == FF_P_TYPE) {
1222
        do {
1223
            if(h->skip_mode_flag) {
1224
                skip_count = get_ue_golomb(&s->gb);
1225
                while(skip_count--) {
1226
                    decode_mb_p(h,P_SKIP);
1227
                    if(!next_mb(h))
1228
                        goto done;
1229
                }
1230
                mb_type = get_ue_golomb(&s->gb) + P_16X16;
1231
            } else
1232
                mb_type = get_ue_golomb(&s->gb) + P_SKIP;
1233
            if(mb_type > P_8X8) {
1234
                decode_mb_i(h, mb_type - P_8X8 - 1);
1235
            } else
1236
                decode_mb_p(h,mb_type);
1237
        } while(next_mb(h));
1238
    } else { /* FF_B_TYPE */
1239
        do {
1240
            if(h->skip_mode_flag) {
1241
                skip_count = get_ue_golomb(&s->gb);
1242
                while(skip_count--) {
1243
                    decode_mb_b(h,B_SKIP);
1244
                    if(!next_mb(h))
1245
                        goto done;
1246
                }
1247
                mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
1248
            } else
1249
                mb_type = get_ue_golomb(&s->gb) + B_SKIP;
1250
            if(mb_type > B_8X8) {
1251
                decode_mb_i(h, mb_type - B_8X8 - 1);
1252
            } else
1253
                decode_mb_b(h,mb_type);
1254
        } while(next_mb(h));
1255
    }
1256
 done:
1257
    if(h->pic_type != FF_B_TYPE) {
1258
        if(h->DPB[1].data[0])
1259
            s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
1260
        memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture));
1261
        memcpy(&h->DPB[0], &h->picture, sizeof(Picture));
1262
        memset(&h->picture,0,sizeof(Picture));
1263
    }
1264
    return 0;
1265
}
1266

    
1267
/*****************************************************************************
1268
 *
1269
 * headers and interface
1270
 *
1271
 ****************************************************************************/
1272

    
1273
/**
1274
 * some predictions require data from the top-neighbouring macroblock.
1275
 * this data has to be stored for one complete row of macroblocks
1276
 * and this storage space is allocated here
1277
 */
1278
static void init_top_lines(AVSContext *h) {
1279
    /* alloc top line of predictors */
1280
    h->top_qp       = av_malloc( h->mb_width);
1281
    h->top_mv[0]    = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
1282
    h->top_mv[1]    = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
1283
    h->top_pred_Y   = av_malloc( h->mb_width*2*sizeof(*h->top_pred_Y));
1284
    h->top_border_y = av_malloc((h->mb_width+1)*16);
1285
    h->top_border_u = av_malloc((h->mb_width)*10);
1286
    h->top_border_v = av_malloc((h->mb_width)*10);
1287

    
1288
    /* alloc space for co-located MVs and types */
1289
    h->col_mv       = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t));
1290
    h->col_type_base = av_malloc(h->mb_width*h->mb_height);
1291
    h->block        = av_mallocz(64*sizeof(DCTELEM));
1292
}
1293

    
1294
static int decode_seq_header(AVSContext *h) {
1295
    MpegEncContext *s = &h->s;
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 CAVS_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) {
1403
    AVSContext *h = avctx->priv_data;
1404
    MpegEncContext * const s = &h->s;
1405

    
1406
    MPV_decode_defaults(s);
1407
    s->avctx = avctx;
1408

    
1409
    avctx->pix_fmt= PIX_FMT_YUV420P;
1410

    
1411
    h->luma_scan[0] = 0;
1412
    h->luma_scan[1] = 8;
1413
    h->intra_pred_l[      INTRA_L_VERT] = intra_pred_vert;
1414
    h->intra_pred_l[     INTRA_L_HORIZ] = intra_pred_horiz;
1415
    h->intra_pred_l[        INTRA_L_LP] = intra_pred_lp;
1416
    h->intra_pred_l[ INTRA_L_DOWN_LEFT] = intra_pred_down_left;
1417
    h->intra_pred_l[INTRA_L_DOWN_RIGHT] = intra_pred_down_right;
1418
    h->intra_pred_l[   INTRA_L_LP_LEFT] = intra_pred_lp_left;
1419
    h->intra_pred_l[    INTRA_L_LP_TOP] = intra_pred_lp_top;
1420
    h->intra_pred_l[    INTRA_L_DC_128] = intra_pred_dc_128;
1421
    h->intra_pred_c[        INTRA_C_LP] = intra_pred_lp;
1422
    h->intra_pred_c[     INTRA_C_HORIZ] = intra_pred_horiz;
1423
    h->intra_pred_c[      INTRA_C_VERT] = intra_pred_vert;
1424
    h->intra_pred_c[     INTRA_C_PLANE] = intra_pred_plane;
1425
    h->intra_pred_c[   INTRA_C_LP_LEFT] = intra_pred_lp_left;
1426
    h->intra_pred_c[    INTRA_C_LP_TOP] = intra_pred_lp_top;
1427
    h->intra_pred_c[    INTRA_C_DC_128] = intra_pred_dc_128;
1428
    h->mv[ 7] = un_mv;
1429
    h->mv[19] = un_mv;
1430
    return 0;
1431
}
1432

    
1433
static int cavs_decode_end(AVCodecContext * avctx) {
1434
    AVSContext *h = avctx->priv_data;
1435

    
1436
    av_free(h->top_qp);
1437
    av_free(h->top_mv[0]);
1438
    av_free(h->top_mv[1]);
1439
    av_free(h->top_pred_Y);
1440
    av_free(h->top_border_y);
1441
    av_free(h->top_border_u);
1442
    av_free(h->top_border_v);
1443
    av_free(h->col_mv);
1444
    av_free(h->col_type_base);
1445
    av_free(h->block);
1446
    return 0;
1447
}
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
                           const 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, &buf, &buf_size) < 0) {
1522
            *poutbuf = NULL;
1523
            *poutbuf_size = 0;
1524
            return buf_size;
1525
        }
1526
    }
1527
    *poutbuf = 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 */