Statistics
| Branch: | Revision:

ffmpeg / libavcodec / cavs.c @ ceb48b46

History | View | Annotate | Download (41.4 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 Street, 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 "cavs.h"
32
#include "cavsdata.h"
33

    
34
/*****************************************************************************
35
 *
36
 * in-loop deblocking filter
37
 *
38
 ****************************************************************************/
39

    
40
static inline int get_bs(vector_t *mvP, vector_t *mvQ, int b) {
41
    if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA))
42
        return 2;
43
    if( (abs(mvP->x - mvQ->x) >= 4) ||  (abs(mvP->y - mvQ->y) >= 4) )
44
        return 1;
45
    if(b){
46
        mvP += MV_BWD_OFFS;
47
        mvQ += MV_BWD_OFFS;
48
        if( (abs(mvP->x - mvQ->x) >= 4) ||  (abs(mvP->y - mvQ->y) >= 4) )
49
            return 1;
50
    }else{
51
        if(mvP->ref != mvQ->ref)
52
            return 1;
53
    }
54
    return 0;
55
}
56

    
57
#define SET_PARAMS                                            \
58
    alpha = alpha_tab[av_clip(qp_avg + h->alpha_offset,0,63)];   \
59
    beta  =  beta_tab[av_clip(qp_avg + h->beta_offset, 0,63)];   \
60
    tc    =    tc_tab[av_clip(qp_avg + h->alpha_offset,0,63)];
61

    
62
/**
63
 * in-loop deblocking filter for a single macroblock
64
 *
65
 * boundary strength (bs) mapping:
66
 *
67
 * --4---5--
68
 * 0   2   |
69
 * | 6 | 7 |
70
 * 1   3   |
71
 * ---------
72
 *
73
 */
74
void ff_cavs_filter(AVSContext *h, enum mb_t mb_type) {
75
    DECLARE_ALIGNED_8(uint8_t, bs[8]);
76
    int qp_avg, alpha, beta, tc;
77
    int i;
78

    
79
    /* save un-deblocked lines */
80
    h->topleft_border_y = h->top_border_y[h->mbx*16+15];
81
    h->topleft_border_u = h->top_border_u[h->mbx*10+8];
82
    h->topleft_border_v = h->top_border_v[h->mbx*10+8];
83
    memcpy(&h->top_border_y[h->mbx*16], h->cy + 15* h->l_stride,16);
84
    memcpy(&h->top_border_u[h->mbx*10+1], h->cu +  7* h->c_stride,8);
85
    memcpy(&h->top_border_v[h->mbx*10+1], h->cv +  7* h->c_stride,8);
86
    for(i=0;i<8;i++) {
87
        h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+0)*h->l_stride);
88
        h->left_border_y[i*2+2] = *(h->cy + 15 + (i*2+1)*h->l_stride);
89
        h->left_border_u[i+1] = *(h->cu + 7 + i*h->c_stride);
90
        h->left_border_v[i+1] = *(h->cv + 7 + i*h->c_stride);
91
    }
92
    if(!h->loop_filter_disable) {
93
        /* determine bs */
94
        if(mb_type == I_8X8)
95
            *((uint64_t *)bs) = 0x0202020202020202ULL;
96
        else{
97
            *((uint64_t *)bs) = 0;
98
            if(partition_flags[mb_type] & SPLITV){
99
                bs[2] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1], mb_type > P_8X8);
100
                bs[3] = get_bs(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3], mb_type > P_8X8);
101
            }
102
            if(partition_flags[mb_type] & SPLITH){
103
                bs[6] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2], mb_type > P_8X8);
104
                bs[7] = get_bs(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3], mb_type > P_8X8);
105
            }
106
            bs[0] = get_bs(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0], mb_type > P_8X8);
107
            bs[1] = get_bs(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2], mb_type > P_8X8);
108
            bs[4] = get_bs(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0], mb_type > P_8X8);
109
            bs[5] = get_bs(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1], mb_type > P_8X8);
110
        }
111
        if( *((uint64_t *)bs) ) {
112
            if(h->flags & A_AVAIL) {
113
                qp_avg = (h->qp + h->left_qp + 1) >> 1;
114
                SET_PARAMS;
115
                h->s.dsp.cavs_filter_lv(h->cy,h->l_stride,alpha,beta,tc,bs[0],bs[1]);
116
                h->s.dsp.cavs_filter_cv(h->cu,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
117
                h->s.dsp.cavs_filter_cv(h->cv,h->c_stride,alpha,beta,tc,bs[0],bs[1]);
118
            }
119
            qp_avg = h->qp;
120
            SET_PARAMS;
121
            h->s.dsp.cavs_filter_lv(h->cy + 8,h->l_stride,alpha,beta,tc,bs[2],bs[3]);
122
            h->s.dsp.cavs_filter_lh(h->cy + 8*h->l_stride,h->l_stride,alpha,beta,tc,
123
                           bs[6],bs[7]);
124

    
125
            if(h->flags & B_AVAIL) {
126
                qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1;
127
                SET_PARAMS;
128
                h->s.dsp.cavs_filter_lh(h->cy,h->l_stride,alpha,beta,tc,bs[4],bs[5]);
129
                h->s.dsp.cavs_filter_ch(h->cu,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
130
                h->s.dsp.cavs_filter_ch(h->cv,h->c_stride,alpha,beta,tc,bs[4],bs[5]);
131
            }
132
        }
133
    }
134
    h->left_qp = h->qp;
135
    h->top_qp[h->mbx] = h->qp;
136
}
137

    
138
#undef SET_PARAMS
139

    
140
/*****************************************************************************
141
 *
142
 * spatial intra prediction
143
 *
144
 ****************************************************************************/
145

    
146
static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
147
    int y;
148
    uint64_t a = unaligned64(&top[1]);
149
    for(y=0;y<8;y++) {
150
        *((uint64_t *)(d+y*stride)) = a;
151
    }
152
}
153

    
154
static void intra_pred_horiz(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
155
    int y;
156
    uint64_t a;
157
    for(y=0;y<8;y++) {
158
        a = left[y+1] * 0x0101010101010101ULL;
159
        *((uint64_t *)(d+y*stride)) = a;
160
    }
161
}
162

    
163
static void intra_pred_dc_128(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
164
    int y;
165
    uint64_t a = 0x8080808080808080ULL;
166
    for(y=0;y<8;y++)
167
        *((uint64_t *)(d+y*stride)) = a;
168
}
169

    
170
static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
171
    int x,y,ia;
172
    int ih = 0;
173
    int iv = 0;
174
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
175

    
176
    for(x=0; x<4; x++) {
177
        ih += (x+1)*(top[5+x]-top[3-x]);
178
        iv += (x+1)*(left[5+x]-left[3-x]);
179
    }
180
    ia = (top[8]+left[8])<<4;
181
    ih = (17*ih+16)>>5;
182
    iv = (17*iv+16)>>5;
183
    for(y=0; y<8; y++)
184
        for(x=0; x<8; x++)
185
            d[y*stride+x] = cm[(ia+(x-3)*ih+(y-3)*iv+16)>>5];
186
}
187

    
188
#define LOWPASS(ARRAY,INDEX)                                            \
189
    (( ARRAY[(INDEX)-1] + 2*ARRAY[(INDEX)] + ARRAY[(INDEX)+1] + 2) >> 2)
190

    
191
static void intra_pred_lp(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
192
    int x,y;
193
    for(y=0; y<8; y++)
194
        for(x=0; x<8; x++)
195
            d[y*stride+x] = (LOWPASS(top,x+1) + LOWPASS(left,y+1)) >> 1;
196
}
197

    
198
static void intra_pred_down_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
199
    int x,y;
200
    for(y=0; y<8; y++)
201
        for(x=0; x<8; x++)
202
            d[y*stride+x] = (LOWPASS(top,x+y+2) + LOWPASS(left,x+y+2)) >> 1;
203
}
204

    
205
static void intra_pred_down_right(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
206
    int x,y;
207
    for(y=0; y<8; y++)
208
        for(x=0; x<8; x++)
209
            if(x==y)
210
                d[y*stride+x] = (left[1]+2*top[0]+top[1]+2)>>2;
211
            else if(x>y)
212
                d[y*stride+x] = LOWPASS(top,x-y);
213
            else
214
                d[y*stride+x] = LOWPASS(left,y-x);
215
}
216

    
217
static void intra_pred_lp_left(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
218
    int x,y;
219
    for(y=0; y<8; y++)
220
        for(x=0; x<8; x++)
221
            d[y*stride+x] = LOWPASS(left,y+1);
222
}
223

    
224
static void intra_pred_lp_top(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
225
    int x,y;
226
    for(y=0; y<8; y++)
227
        for(x=0; x<8; x++)
228
            d[y*stride+x] = LOWPASS(top,x+1);
229
}
230

    
231
#undef LOWPASS
232

    
233
/*****************************************************************************
234
 *
235
 * motion compensation
236
 *
237
 ****************************************************************************/
238

    
239
static inline void mc_dir_part(AVSContext *h,Picture *pic,int square,
240
                        int chroma_height,int delta,int list,uint8_t *dest_y,
241
                        uint8_t *dest_cb,uint8_t *dest_cr,int src_x_offset,
242
                        int src_y_offset,qpel_mc_func *qpix_op,
243
                        h264_chroma_mc_func chroma_op,vector_t *mv){
244
    MpegEncContext * const s = &h->s;
245
    const int mx= mv->x + src_x_offset*8;
246
    const int my= mv->y + src_y_offset*8;
247
    const int luma_xy= (mx&3) + ((my&3)<<2);
248
    uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*h->l_stride;
249
    uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*h->c_stride;
250
    uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*h->c_stride;
251
    int extra_width= 0; //(s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16;
252
    int extra_height= extra_width;
253
    int emu=0;
254
    const int full_mx= mx>>2;
255
    const int full_my= my>>2;
256
    const int pic_width  = 16*h->mb_width;
257
    const int pic_height = 16*h->mb_height;
258

    
259
    if(!pic->data[0])
260
        return;
261
    if(mx&7) extra_width -= 3;
262
    if(my&7) extra_height -= 3;
263

    
264
    if(   full_mx < 0-extra_width
265
          || full_my < 0-extra_height
266
          || full_mx + 16/*FIXME*/ > pic_width + extra_width
267
          || full_my + 16/*FIXME*/ > pic_height + extra_height){
268
        ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*h->l_stride, h->l_stride,
269
                            16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, pic_width, pic_height);
270
        src_y= s->edge_emu_buffer + 2 + 2*h->l_stride;
271
        emu=1;
272
    }
273

    
274
    qpix_op[luma_xy](dest_y, src_y, h->l_stride); //FIXME try variable height perhaps?
275
    if(!square){
276
        qpix_op[luma_xy](dest_y + delta, src_y + delta, h->l_stride);
277
    }
278

    
279
    if(emu){
280
        ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, h->c_stride,
281
                            9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
282
        src_cb= s->edge_emu_buffer;
283
    }
284
    chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx&7, my&7);
285

    
286
    if(emu){
287
        ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, h->c_stride,
288
                            9, 9/*FIXME*/, (mx>>3), (my>>3), pic_width>>1, pic_height>>1);
289
        src_cr= s->edge_emu_buffer;
290
    }
291
    chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx&7, my&7);
292
}
293

    
294
static inline void mc_part_std(AVSContext *h,int square,int chroma_height,int delta,
295
                        uint8_t *dest_y,uint8_t *dest_cb,uint8_t *dest_cr,
296
                        int x_offset, int y_offset,qpel_mc_func *qpix_put,
297
                        h264_chroma_mc_func chroma_put,qpel_mc_func *qpix_avg,
298
                        h264_chroma_mc_func chroma_avg, vector_t *mv){
299
    qpel_mc_func *qpix_op=  qpix_put;
300
    h264_chroma_mc_func chroma_op= chroma_put;
301

    
302
    dest_y  += 2*x_offset + 2*y_offset*h->l_stride;
303
    dest_cb +=   x_offset +   y_offset*h->c_stride;
304
    dest_cr +=   x_offset +   y_offset*h->c_stride;
305
    x_offset += 8*h->mbx;
306
    y_offset += 8*h->mby;
307

    
308
    if(mv->ref >= 0){
309
        Picture *ref= &h->DPB[mv->ref];
310
        mc_dir_part(h, ref, square, chroma_height, delta, 0,
311
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
312
                    qpix_op, chroma_op, mv);
313

    
314
        qpix_op=  qpix_avg;
315
        chroma_op= chroma_avg;
316
    }
317

    
318
    if((mv+MV_BWD_OFFS)->ref >= 0){
319
        Picture *ref= &h->DPB[0];
320
        mc_dir_part(h, ref, square, chroma_height, delta, 1,
321
                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
322
                    qpix_op, chroma_op, mv+MV_BWD_OFFS);
323
    }
324
}
325

    
326
void ff_cavs_inter(AVSContext *h, enum mb_t mb_type) {
327
    if(partition_flags[mb_type] == 0){ // 16x16
328
        mc_part_std(h, 1, 8, 0, h->cy, h->cu, h->cv, 0, 0,
329
                h->s.dsp.put_cavs_qpel_pixels_tab[0],
330
                h->s.dsp.put_h264_chroma_pixels_tab[0],
331
                h->s.dsp.avg_cavs_qpel_pixels_tab[0],
332
                h->s.dsp.avg_h264_chroma_pixels_tab[0],&h->mv[MV_FWD_X0]);
333
    }else{
334
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0,
335
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
336
                h->s.dsp.put_h264_chroma_pixels_tab[1],
337
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
338
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]);
339
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0,
340
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
341
                h->s.dsp.put_h264_chroma_pixels_tab[1],
342
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
343
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]);
344
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4,
345
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
346
                h->s.dsp.put_h264_chroma_pixels_tab[1],
347
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
348
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]);
349
        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4,
350
                h->s.dsp.put_cavs_qpel_pixels_tab[1],
351
                h->s.dsp.put_h264_chroma_pixels_tab[1],
352
                h->s.dsp.avg_cavs_qpel_pixels_tab[1],
353
                h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
354
    }
355
}
356

    
357
/*****************************************************************************
358
 *
359
 * motion vector prediction
360
 *
361
 ****************************************************************************/
362

    
363
static inline void store_mvs(AVSContext *h) {
364
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 0] = h->mv[MV_FWD_X0];
365
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 1] = h->mv[MV_FWD_X1];
366
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 2] = h->mv[MV_FWD_X2];
367
    h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + 3] = h->mv[MV_FWD_X3];
368
}
369

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

    
373
    *d_x = (src->x*distp*den + 256 + (src->x>>31)) >> 9;
374
    *d_y = (src->y*distp*den + 256 + (src->y>>31)) >> 9;
375
}
376

    
377
static inline void mv_pred_median(AVSContext *h, vector_t *mvP, vector_t *mvA, vector_t *mvB, vector_t *mvC) {
378
    int ax, ay, bx, by, cx, cy;
379
    int len_ab, len_bc, len_ca, len_mid;
380

    
381
    /* scale candidates according to their temporal span */
382
    scale_mv(h, &ax, &ay, mvA, mvP->dist);
383
    scale_mv(h, &bx, &by, mvB, mvP->dist);
384
    scale_mv(h, &cx, &cy, mvC, mvP->dist);
385
    /* find the geometrical median of the three candidates */
386
    len_ab = abs(ax - bx) + abs(ay - by);
387
    len_bc = abs(bx - cx) + abs(by - cy);
388
    len_ca = abs(cx - ax) + abs(cy - ay);
389
    len_mid = mid_pred(len_ab, len_bc, len_ca);
390
    if(len_mid == len_ab) {
391
        mvP->x = cx;
392
        mvP->y = cy;
393
    } else if(len_mid == len_bc) {
394
        mvP->x = ax;
395
        mvP->y = ay;
396
    } else {
397
        mvP->x = bx;
398
        mvP->y = by;
399
    }
400
}
401

    
402
static inline void mv_pred_direct(AVSContext *h, vector_t *pmv_fw,
403
                                  vector_t *col_mv) {
404
    vector_t *pmv_bw = pmv_fw + MV_BWD_OFFS;
405
    int den = h->direct_den[col_mv->ref];
406
    int m = col_mv->x >> 31;
407

    
408
    pmv_fw->dist = h->dist[1];
409
    pmv_bw->dist = h->dist[0];
410
    pmv_fw->ref = 1;
411
    pmv_bw->ref = 0;
412
    /* scale the co-located motion vector according to its temporal span */
413
    pmv_fw->x = (((den+(den*col_mv->x*pmv_fw->dist^m)-m-1)>>14)^m)-m;
414
    pmv_bw->x = m-(((den+(den*col_mv->x*pmv_bw->dist^m)-m-1)>>14)^m);
415
    m = col_mv->y >> 31;
416
    pmv_fw->y = (((den+(den*col_mv->y*pmv_fw->dist^m)-m-1)>>14)^m)-m;
417
    pmv_bw->y = m-(((den+(den*col_mv->y*pmv_bw->dist^m)-m-1)>>14)^m);
418
}
419

    
420
static inline void mv_pred_sym(AVSContext *h, vector_t *src, enum block_t size) {
421
    vector_t *dst = src + MV_BWD_OFFS;
422

    
423
    /* backward mv is the scaled and negated forward mv */
424
    dst->x = -((src->x * h->sym_factor + 256) >> 9);
425
    dst->y = -((src->y * h->sym_factor + 256) >> 9);
426
    dst->ref = 0;
427
    dst->dist = h->dist[0];
428
    set_mvs(dst, size);
429
}
430

    
431
void ff_cavs_mv(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
432
                enum mv_pred_t mode, enum block_t size, int ref) {
433
    vector_t *mvP = &h->mv[nP];
434
    vector_t *mvA = &h->mv[nP-1];
435
    vector_t *mvB = &h->mv[nP-4];
436
    vector_t *mvC = &h->mv[nC];
437
    const vector_t *mvP2 = NULL;
438

    
439
    mvP->ref = ref;
440
    mvP->dist = h->dist[mvP->ref];
441
    if(mvC->ref == NOT_AVAIL)
442
        mvC = &h->mv[nP-5]; // set to top-left (mvD)
443
    if((mode == MV_PRED_PSKIP) &&
444
       ((mvA->ref == NOT_AVAIL) || (mvB->ref == NOT_AVAIL) ||
445
           ((mvA->x | mvA->y | mvA->ref) == 0)  ||
446
           ((mvB->x | mvB->y | mvB->ref) == 0) )) {
447
        mvP2 = &ff_cavs_un_mv;
448
    /* if there is only one suitable candidate, take it */
449
    } else if((mvA->ref >= 0) && (mvB->ref < 0) && (mvC->ref < 0)) {
450
        mvP2= mvA;
451
    } else if((mvA->ref < 0) && (mvB->ref >= 0) && (mvC->ref < 0)) {
452
        mvP2= mvB;
453
    } else if((mvA->ref < 0) && (mvB->ref < 0) && (mvC->ref >= 0)) {
454
        mvP2= mvC;
455
    } else if(mode == MV_PRED_LEFT     && mvA->ref == ref){
456
        mvP2= mvA;
457
    } else if(mode == MV_PRED_TOP      && mvB->ref == ref){
458
        mvP2= mvB;
459
    } else if(mode == MV_PRED_TOPRIGHT && mvC->ref == ref){
460
        mvP2= mvC;
461
    }
462
    if(mvP2){
463
        mvP->x = mvP2->x;
464
        mvP->y = mvP2->y;
465
    }else
466
        mv_pred_median(h, mvP, mvA, mvB, mvC);
467

    
468
    if(mode < MV_PRED_PSKIP) {
469
        mvP->x += get_se_golomb(&h->s.gb);
470
        mvP->y += get_se_golomb(&h->s.gb);
471
    }
472
    set_mvs(mvP,size);
473
}
474

    
475
/*****************************************************************************
476
 *
477
 * residual data decoding
478
 *
479
 ****************************************************************************/
480

    
481
/** kth-order exponential golomb code */
482
static inline int get_ue_code(GetBitContext *gb, int order) {
483
    if(order) {
484
        int ret = get_ue_golomb(gb) << order;
485
        return ret + get_bits(gb,order);
486
    }
487
    return get_ue_golomb(gb);
488
}
489

    
490
/**
491
 * decode coefficients from one 8x8 block, dequantize, inverse transform
492
 *  and add them to sample block
493
 * @param r pointer to 2D VLC table
494
 * @param esc_golomb_order escape codes are k-golomb with this order k
495
 * @param qp quantizer
496
 * @param dst location of sample block
497
 * @param stride line stride in frame buffer
498
 */
499
static int decode_residual_block(AVSContext *h, GetBitContext *gb,
500
                                 const dec_2dvlc_t *r, int esc_golomb_order,
501
                                 int qp, uint8_t *dst, int stride) {
502
    int i, level_code, esc_code, level, run, mask;
503
    DCTELEM level_buf[64];
504
    uint8_t run_buf[64];
505
    DCTELEM *block = h->block;
506

    
507
    for(i=0;i<65;i++) {
508
        level_code = get_ue_code(gb,r->golomb_order);
509
        if(level_code >= ESCAPE_CODE) {
510
            run = ((level_code - ESCAPE_CODE) >> 1) + 1;
511
            esc_code = get_ue_code(gb,esc_golomb_order);
512
            level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
513
            while(level > r->inc_limit)
514
                r++;
515
            mask = -(level_code & 1);
516
            level = (level^mask) - mask;
517
        } else {
518
            level = r->rltab[level_code][0];
519
            if(!level) //end of block signal
520
                break;
521
            run   = r->rltab[level_code][1];
522
            r += r->rltab[level_code][2];
523
        }
524
        level_buf[i] = level;
525
        run_buf[i] = run;
526
    }
527
    if(dequant(h,level_buf, run_buf, block, dequant_mul[qp],
528
               dequant_shift[qp], i))
529
        return -1;
530
    h->s.dsp.cavs_idct8_add(dst,block,stride);
531
    return 0;
532
}
533

    
534

    
535
static inline void decode_residual_chroma(AVSContext *h) {
536
    if(h->cbp & (1<<4))
537
        decode_residual_block(h,&h->s.gb,chroma_dec,0, chroma_qp[h->qp],
538
                              h->cu,h->c_stride);
539
    if(h->cbp & (1<<5))
540
        decode_residual_block(h,&h->s.gb,chroma_dec,0, chroma_qp[h->qp],
541
                              h->cv,h->c_stride);
542
}
543

    
544
static inline int decode_residual_inter(AVSContext *h) {
545
    int block;
546

    
547
    /* get coded block pattern */
548
    int cbp= get_ue_golomb(&h->s.gb);
549
    if(cbp > 63){
550
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal inter cbp\n");
551
        return -1;
552
    }
553
    h->cbp = cbp_tab[cbp][1];
554

    
555
    /* get quantizer */
556
    if(h->cbp && !h->qp_fixed)
557
        h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
558
    for(block=0;block<4;block++)
559
        if(h->cbp & (1<<block))
560
            decode_residual_block(h,&h->s.gb,inter_dec,0,h->qp,
561
                                  h->cy + h->luma_scan[block], h->l_stride);
562
    decode_residual_chroma(h);
563

    
564
    return 0;
565
}
566

    
567
/*****************************************************************************
568
 *
569
 * macroblock level
570
 *
571
 ****************************************************************************/
572

    
573
static int decode_mb_i(AVSContext *h, int cbp_code) {
574
    GetBitContext *gb = &h->s.gb;
575
    int block, pred_mode_uv;
576
    uint8_t top[18];
577
    uint8_t *left = NULL;
578
    uint8_t *d;
579

    
580
    init_mb(h);
581

    
582
    /* get intra prediction modes from stream */
583
    for(block=0;block<4;block++) {
584
        int nA,nB,predpred;
585
        int pos = scan3x3[block];
586

    
587
        nA = h->pred_mode_Y[pos-1];
588
        nB = h->pred_mode_Y[pos-3];
589
        predpred = FFMIN(nA,nB);
590
        if(predpred == NOT_AVAIL) // if either is not available
591
            predpred = INTRA_L_LP;
592
        if(!get_bits1(gb)){
593
            int rem_mode= get_bits(gb, 2);
594
            predpred = rem_mode + (rem_mode >= predpred);
595
        }
596
        h->pred_mode_Y[pos] = predpred;
597
    }
598
    pred_mode_uv = get_ue_golomb(gb);
599
    if(pred_mode_uv > 6) {
600
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra chroma pred mode\n");
601
        return -1;
602
    }
603
    modify_mb_i(h, &pred_mode_uv);
604

    
605
    /* get coded block pattern */
606
    if(h->pic_type == FF_I_TYPE)
607
        cbp_code = get_ue_golomb(gb);
608
    if(cbp_code > 63){
609
        av_log(h->s.avctx, AV_LOG_ERROR, "illegal intra cbp\n");
610
        return -1;
611
    }
612
    h->cbp = cbp_tab[cbp_code][0];
613
    if(h->cbp && !h->qp_fixed)
614
        h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
615

    
616
    /* luma intra prediction interleaved with residual decode/transform/add */
617
    for(block=0;block<4;block++) {
618
        d = h->cy + h->luma_scan[block];
619
        load_intra_pred_luma(h, top, &left, block);
620
        h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
621
            (d, top, left, h->l_stride);
622
        if(h->cbp & (1<<block))
623
            decode_residual_block(h,gb,intra_dec,1,h->qp,d,h->l_stride);
624
    }
625

    
626
    /* chroma intra prediction */
627
    load_intra_pred_chroma(h);
628
    h->intra_pred_c[pred_mode_uv](h->cu, &h->top_border_u[h->mbx*10],
629
                                  h->left_border_u, h->c_stride);
630
    h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx*10],
631
                                  h->left_border_v, h->c_stride);
632

    
633
    decode_residual_chroma(h);
634
    ff_cavs_filter(h,I_8X8);
635
    set_mv_intra(h);
636
    return 0;
637
}
638

    
639
static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
640
    GetBitContext *gb = &h->s.gb;
641
    int ref[4];
642

    
643
    init_mb(h);
644
    switch(mb_type) {
645
    case P_SKIP:
646
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_PSKIP,  BLK_16X16, 0);
647
        break;
648
    case P_16X16:
649
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
650
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16,ref[0]);
651
        break;
652
    case P_16X8:
653
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
654
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
655
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,    BLK_16X8, ref[0]);
656
        ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT,   BLK_16X8, ref[2]);
657
        break;
658
    case P_8X16:
659
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
660
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
661
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT,   BLK_8X16, ref[0]);
662
        ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, ref[1]);
663
        break;
664
    case P_8X8:
665
        ref[0] = h->ref_flag ? 0 : get_bits1(gb);
666
        ref[1] = h->ref_flag ? 0 : get_bits1(gb);
667
        ref[2] = h->ref_flag ? 0 : get_bits1(gb);
668
        ref[3] = h->ref_flag ? 0 : get_bits1(gb);
669
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_MEDIAN,   BLK_8X8, ref[0]);
670
        ff_cavs_mv(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_MEDIAN,   BLK_8X8, ref[1]);
671
        ff_cavs_mv(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN,   BLK_8X8, ref[2]);
672
        ff_cavs_mv(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN,   BLK_8X8, ref[3]);
673
    }
674
    ff_cavs_inter(h, mb_type);
675
    set_intra_mode_default(h);
676
    store_mvs(h);
677
    if(mb_type != P_SKIP)
678
        decode_residual_inter(h);
679
    ff_cavs_filter(h,mb_type);
680
    *h->col_type = mb_type;
681
}
682

    
683
static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
684
    int block;
685
    enum sub_mb_t sub_type[4];
686
    int flags;
687

    
688
    init_mb(h);
689

    
690
    /* reset all MVs */
691
    h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
692
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
693
    h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
694
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
695
    switch(mb_type) {
696
    case B_SKIP:
697
    case B_DIRECT:
698
        if(!(*h->col_type)) {
699
            /* intra MB at co-location, do in-plane prediction */
700
            ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_BSKIP, BLK_16X16, 1);
701
            ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_BSKIP, BLK_16X16, 0);
702
        } else
703
            /* direct prediction from co-located P MB, block-wise */
704
            for(block=0;block<4;block++)
705
                mv_pred_direct(h,&h->mv[mv_scan[block]],
706
                            &h->col_mv[(h->mby*h->mb_width+h->mbx)*4 + block]);
707
        break;
708
    case B_FWD_16X16:
709
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
710
        break;
711
    case B_SYM_16X16:
712
        ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_MEDIAN, BLK_16X16, 1);
713
        mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X16);
714
        break;
715
    case B_BWD_16X16:
716
        ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_MEDIAN, BLK_16X16, 0);
717
        break;
718
    case B_8X8:
719
        for(block=0;block<4;block++)
720
            sub_type[block] = get_bits(&h->s.gb,2);
721
        for(block=0;block<4;block++) {
722
            switch(sub_type[block]) {
723
            case B_SUB_DIRECT:
724
                if(!(*h->col_type)) {
725
                    /* intra MB at co-location, do in-plane prediction */
726
                    ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
727
                            MV_PRED_BSKIP, BLK_8X8, 1);
728
                    ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
729
                            mv_scan[block]-3+MV_BWD_OFFS,
730
                            MV_PRED_BSKIP, BLK_8X8, 0);
731
                } else
732
                    mv_pred_direct(h,&h->mv[mv_scan[block]],
733
                                   &h->col_mv[(h->mby*h->mb_width + h->mbx)*4 + block]);
734
                break;
735
            case B_SUB_FWD:
736
                ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
737
                        MV_PRED_MEDIAN, BLK_8X8, 1);
738
                break;
739
            case B_SUB_SYM:
740
                ff_cavs_mv(h, mv_scan[block], mv_scan[block]-3,
741
                        MV_PRED_MEDIAN, BLK_8X8, 1);
742
                mv_pred_sym(h, &h->mv[mv_scan[block]], BLK_8X8);
743
                break;
744
            }
745
        }
746
        for(block=0;block<4;block++) {
747
            if(sub_type[block] == B_SUB_BWD)
748
                ff_cavs_mv(h, mv_scan[block]+MV_BWD_OFFS,
749
                        mv_scan[block]+MV_BWD_OFFS-3,
750
                        MV_PRED_MEDIAN, BLK_8X8, 0);
751
        }
752
        break;
753
    default:
754
        assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
755
        flags = partition_flags[mb_type];
756
        if(mb_type & 1) { /* 16x8 macroblock types */
757
            if(flags & FWD0)
758
                ff_cavs_mv(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,  BLK_16X8, 1);
759
            if(flags & SYM0)
760
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
761
            if(flags & FWD1)
762
                ff_cavs_mv(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
763
            if(flags & SYM1)
764
                mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
765
            if(flags & BWD0)
766
                ff_cavs_mv(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP,  BLK_16X8, 0);
767
            if(flags & BWD1)
768
                ff_cavs_mv(h, MV_BWD_X2, MV_BWD_A1, MV_PRED_LEFT, BLK_16X8, 0);
769
        } else {          /* 8x16 macroblock types */
770
            if(flags & FWD0)
771
                ff_cavs_mv(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
772
            if(flags & SYM0)
773
                mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
774
            if(flags & FWD1)
775
                ff_cavs_mv(h,MV_FWD_X1,MV_FWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,1);
776
            if(flags & SYM1)
777
                mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
778
            if(flags & BWD0)
779
                ff_cavs_mv(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
780
            if(flags & BWD1)
781
                ff_cavs_mv(h,MV_BWD_X1,MV_BWD_C2,MV_PRED_TOPRIGHT,BLK_8X16,0);
782
        }
783
    }
784
    ff_cavs_inter(h, mb_type);
785
    set_intra_mode_default(h);
786
    if(mb_type != B_SKIP)
787
        decode_residual_inter(h);
788
    ff_cavs_filter(h,mb_type);
789
}
790

    
791
/*****************************************************************************
792
 *
793
 * slice level
794
 *
795
 ****************************************************************************/
796

    
797
static inline int decode_slice_header(AVSContext *h, GetBitContext *gb) {
798
    if(h->stc > 0xAF)
799
        av_log(h->s.avctx, AV_LOG_ERROR, "unexpected start code 0x%02x\n", h->stc);
800
    h->mby = h->stc;
801
    if((h->mby == 0) && (!h->qp_fixed)){
802
        h->qp_fixed = get_bits1(gb);
803
        h->qp = get_bits(gb,6);
804
    }
805
    /* inter frame or second slice can have weighting params */
806
    if((h->pic_type != FF_I_TYPE) || (!h->pic_structure && h->mby >= h->mb_width/2))
807
        if(get_bits1(gb)) { //slice_weighting_flag
808
            av_log(h->s.avctx, AV_LOG_ERROR,
809
                   "weighted prediction not yet supported\n");
810
        }
811
    return 0;
812
}
813

    
814
static inline void check_for_slice(AVSContext *h) {
815
    GetBitContext *gb = &h->s.gb;
816
    int align;
817
    align = (-get_bits_count(gb)) & 7;
818
    if((show_bits_long(gb,24+align) & 0xFFFFFF) == 0x000001) {
819
        get_bits_long(gb,24+align);
820
        h->stc = get_bits(gb,8);
821
        decode_slice_header(h,gb);
822
    }
823
}
824

    
825
/*****************************************************************************
826
 *
827
 * frame level
828
 *
829
 ****************************************************************************/
830

    
831
void ff_cavs_init_pic(AVSContext *h) {
832
    int i;
833

    
834
    /* clear some predictors */
835
    for(i=0;i<=20;i+=4)
836
        h->mv[i] = ff_cavs_un_mv;
837
    h->mv[MV_BWD_X0] = ff_cavs_dir_mv;
838
    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
839
    h->mv[MV_FWD_X0] = ff_cavs_dir_mv;
840
    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
841
    h->pred_mode_Y[3] = h->pred_mode_Y[6] = NOT_AVAIL;
842
    h->cy = h->picture.data[0];
843
    h->cu = h->picture.data[1];
844
    h->cv = h->picture.data[2];
845
    h->l_stride = h->picture.linesize[0];
846
    h->c_stride = h->picture.linesize[1];
847
    h->luma_scan[2] = 8*h->l_stride;
848
    h->luma_scan[3] = 8*h->l_stride+8;
849
    h->mbx = h->mby = 0;
850
    h->flags = 0;
851
}
852

    
853
static int decode_pic(AVSContext *h) {
854
    MpegEncContext *s = &h->s;
855
    int skip_count;
856
    enum mb_t mb_type;
857

    
858
    if (!s->context_initialized) {
859
        s->avctx->idct_algo = FF_IDCT_CAVS;
860
        if (MPV_common_init(s) < 0)
861
            return -1;
862
        ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
863
    }
864
    get_bits(&s->gb,16);//bbv_dwlay
865
    if(h->stc == PIC_PB_START_CODE) {
866
        h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
867
        if(h->pic_type > FF_B_TYPE) {
868
            av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
869
            return -1;
870
        }
871
        /* make sure we have the reference frames we need */
872
        if(!h->DPB[0].data[0] ||
873
          (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
874
            return -1;
875
    } else {
876
        h->pic_type = FF_I_TYPE;
877
        if(get_bits1(&s->gb))
878
            get_bits(&s->gb,16);//time_code
879
    }
880
    /* release last B frame */
881
    if(h->picture.data[0])
882
        s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);
883

    
884
    s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);
885
    ff_cavs_init_pic(h);
886
    h->picture.poc = get_bits(&s->gb,8)*2;
887

    
888
    /* get temporal distances and MV scaling factors */
889
    if(h->pic_type != FF_B_TYPE) {
890
        h->dist[0] = (h->picture.poc - h->DPB[0].poc  + 512) % 512;
891
    } else {
892
        h->dist[0] = (h->DPB[0].poc  - h->picture.poc + 512) % 512;
893
    }
894
    h->dist[1] = (h->picture.poc - h->DPB[1].poc  + 512) % 512;
895
    h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;
896
    h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;
897
    if(h->pic_type == FF_B_TYPE) {
898
        h->sym_factor = h->dist[0]*h->scale_den[1];
899
    } else {
900
        h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;
901
        h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;
902
    }
903

    
904
    if(s->low_delay)
905
        get_ue_golomb(&s->gb); //bbv_check_times
906
    h->progressive             = get_bits1(&s->gb);
907
    if(h->progressive)
908
        h->pic_structure = 1;
909
    else if(!(h->pic_structure = get_bits1(&s->gb) && (h->stc == PIC_PB_START_CODE)) )
910
        get_bits1(&s->gb);     //advanced_pred_mode_disable
911
    skip_bits1(&s->gb);        //top_field_first
912
    skip_bits1(&s->gb);        //repeat_first_field
913
    h->qp_fixed                = get_bits1(&s->gb);
914
    h->qp                      = get_bits(&s->gb,6);
915
    if(h->pic_type == FF_I_TYPE) {
916
        if(!h->progressive && !h->pic_structure)
917
            skip_bits1(&s->gb);//what is this?
918
        skip_bits(&s->gb,4);   //reserved bits
919
    } else {
920
        if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))
921
            h->ref_flag        = get_bits1(&s->gb);
922
        skip_bits(&s->gb,4);   //reserved bits
923
        h->skip_mode_flag      = get_bits1(&s->gb);
924
    }
925
    h->loop_filter_disable     = get_bits1(&s->gb);
926
    if(!h->loop_filter_disable && get_bits1(&s->gb)) {
927
        h->alpha_offset        = get_se_golomb(&s->gb);
928
        h->beta_offset         = get_se_golomb(&s->gb);
929
    } else {
930
        h->alpha_offset = h->beta_offset  = 0;
931
    }
932
    check_for_slice(h);
933
    if(h->pic_type == FF_I_TYPE) {
934
        do {
935
            decode_mb_i(h, 0);
936
        } while(next_mb(h));
937
    } else if(h->pic_type == FF_P_TYPE) {
938
        do {
939
            if(h->skip_mode_flag) {
940
                skip_count = get_ue_golomb(&s->gb);
941
                while(skip_count--) {
942
                    decode_mb_p(h,P_SKIP);
943
                    if(!next_mb(h))
944
                        goto done;
945
                }
946
                mb_type = get_ue_golomb(&s->gb) + P_16X16;
947
            } else
948
                mb_type = get_ue_golomb(&s->gb) + P_SKIP;
949
            if(mb_type > P_8X8) {
950
                decode_mb_i(h, mb_type - P_8X8 - 1);
951
            } else
952
                decode_mb_p(h,mb_type);
953
        } while(next_mb(h));
954
    } else { /* FF_B_TYPE */
955
        do {
956
            if(h->skip_mode_flag) {
957
                skip_count = get_ue_golomb(&s->gb);
958
                while(skip_count--) {
959
                    decode_mb_b(h,B_SKIP);
960
                    if(!next_mb(h))
961
                        goto done;
962
                }
963
                mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
964
            } else
965
                mb_type = get_ue_golomb(&s->gb) + B_SKIP;
966
            if(mb_type > B_8X8) {
967
                decode_mb_i(h, mb_type - B_8X8 - 1);
968
            } else
969
                decode_mb_b(h,mb_type);
970
        } while(next_mb(h));
971
    }
972
 done:
973
    if(h->pic_type != FF_B_TYPE) {
974
        if(h->DPB[1].data[0])
975
            s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);
976
        memcpy(&h->DPB[1], &h->DPB[0], sizeof(Picture));
977
        memcpy(&h->DPB[0], &h->picture, sizeof(Picture));
978
        memset(&h->picture,0,sizeof(Picture));
979
    }
980
    return 0;
981
}
982

    
983
/*****************************************************************************
984
 *
985
 * headers and interface
986
 *
987
 ****************************************************************************/
988

    
989
/**
990
 * some predictions require data from the top-neighbouring macroblock.
991
 * this data has to be stored for one complete row of macroblocks
992
 * and this storage space is allocated here
993
 */
994
void ff_cavs_init_top_lines(AVSContext *h) {
995
    /* alloc top line of predictors */
996
    h->top_qp       = av_malloc( h->mb_width);
997
    h->top_mv[0]    = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
998
    h->top_mv[1]    = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
999
    h->top_pred_Y   = av_malloc( h->mb_width*2*sizeof(*h->top_pred_Y));
1000
    h->top_border_y = av_malloc((h->mb_width+1)*16);
1001
    h->top_border_u = av_malloc((h->mb_width)*10);
1002
    h->top_border_v = av_malloc((h->mb_width)*10);
1003

    
1004
    /* alloc space for co-located MVs and types */
1005
    h->col_mv       = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t));
1006
    h->col_type_base = av_malloc(h->mb_width*h->mb_height);
1007
    h->block        = av_mallocz(64*sizeof(DCTELEM));
1008
}
1009

    
1010
static int decode_seq_header(AVSContext *h) {
1011
    MpegEncContext *s = &h->s;
1012
    int frame_rate_code;
1013

    
1014
    h->profile =         get_bits(&s->gb,8);
1015
    h->level =           get_bits(&s->gb,8);
1016
    skip_bits1(&s->gb); //progressive sequence
1017
    s->width =           get_bits(&s->gb,14);
1018
    s->height =          get_bits(&s->gb,14);
1019
    skip_bits(&s->gb,2); //chroma format
1020
    skip_bits(&s->gb,3); //sample_precision
1021
    h->aspect_ratio =    get_bits(&s->gb,4);
1022
    frame_rate_code =    get_bits(&s->gb,4);
1023
    skip_bits(&s->gb,18);//bit_rate_lower
1024
    skip_bits1(&s->gb);  //marker_bit
1025
    skip_bits(&s->gb,12);//bit_rate_upper
1026
    s->low_delay =       get_bits1(&s->gb);
1027
    h->mb_width  = (s->width  + 15) >> 4;
1028
    h->mb_height = (s->height + 15) >> 4;
1029
    h->s.avctx->time_base.den = ff_frame_rate_tab[frame_rate_code].num;
1030
    h->s.avctx->time_base.num = ff_frame_rate_tab[frame_rate_code].den;
1031
    h->s.avctx->width  = s->width;
1032
    h->s.avctx->height = s->height;
1033
    if(!h->top_qp)
1034
        ff_cavs_init_top_lines(h);
1035
    return 0;
1036
}
1037

    
1038
static void cavs_flush(AVCodecContext * avctx) {
1039
    AVSContext *h = avctx->priv_data;
1040
    h->got_keyframe = 0;
1041
}
1042

    
1043
static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
1044
                             uint8_t * buf, int buf_size) {
1045
    AVSContext *h = avctx->priv_data;
1046
    MpegEncContext *s = &h->s;
1047
    int input_size;
1048
    const uint8_t *buf_end;
1049
    const uint8_t *buf_ptr;
1050
    AVFrame *picture = data;
1051
    uint32_t stc;
1052

    
1053
    s->avctx = avctx;
1054

    
1055
    if (buf_size == 0) {
1056
        if(!s->low_delay && h->DPB[0].data[0]) {
1057
            *data_size = sizeof(AVPicture);
1058
            *picture = *(AVFrame *) &h->DPB[0];
1059
        }
1060
        return 0;
1061
    }
1062

    
1063
    buf_ptr = buf;
1064
    buf_end = buf + buf_size;
1065
    for(;;) {
1066
        buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
1067
        if(stc & 0xFFFFFE00)
1068
            return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
1069
        input_size = (buf_end - buf_ptr)*8;
1070
        switch(stc) {
1071
        case CAVS_START_CODE:
1072
            init_get_bits(&s->gb, buf_ptr, input_size);
1073
            decode_seq_header(h);
1074
            break;
1075
        case PIC_I_START_CODE:
1076
            if(!h->got_keyframe) {
1077
                if(h->DPB[0].data[0])
1078
                    avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
1079
                if(h->DPB[1].data[0])
1080
                    avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
1081
                h->got_keyframe = 1;
1082
            }
1083
        case PIC_PB_START_CODE:
1084
            *data_size = 0;
1085
            if(!h->got_keyframe)
1086
                break;
1087
            init_get_bits(&s->gb, buf_ptr, input_size);
1088
            h->stc = stc;
1089
            if(decode_pic(h))
1090
                break;
1091
            *data_size = sizeof(AVPicture);
1092
            if(h->pic_type != FF_B_TYPE) {
1093
                if(h->DPB[1].data[0]) {
1094
                    *picture = *(AVFrame *) &h->DPB[1];
1095
                } else {
1096
                    *data_size = 0;
1097
                }
1098
            } else
1099
                *picture = *(AVFrame *) &h->picture;
1100
            break;
1101
        case EXT_START_CODE:
1102
            //mpeg_decode_extension(avctx,buf_ptr, input_size);
1103
            break;
1104
        case USER_START_CODE:
1105
            //mpeg_decode_user_data(avctx,buf_ptr, input_size);
1106
            break;
1107
        default:
1108
            if (stc >= SLICE_MIN_START_CODE &&
1109
                stc <= SLICE_MAX_START_CODE) {
1110
                init_get_bits(&s->gb, buf_ptr, input_size);
1111
                decode_slice_header(h, &s->gb);
1112
            }
1113
            break;
1114
        }
1115
    }
1116
}
1117

    
1118
int ff_cavs_init(AVCodecContext *avctx) {
1119
    AVSContext *h = avctx->priv_data;
1120
    MpegEncContext * const s = &h->s;
1121

    
1122
    MPV_decode_defaults(s);
1123
    s->avctx = avctx;
1124

    
1125
    avctx->pix_fmt= PIX_FMT_YUV420P;
1126

    
1127
    h->luma_scan[0] = 0;
1128
    h->luma_scan[1] = 8;
1129
    h->intra_pred_l[      INTRA_L_VERT] = intra_pred_vert;
1130
    h->intra_pred_l[     INTRA_L_HORIZ] = intra_pred_horiz;
1131
    h->intra_pred_l[        INTRA_L_LP] = intra_pred_lp;
1132
    h->intra_pred_l[ INTRA_L_DOWN_LEFT] = intra_pred_down_left;
1133
    h->intra_pred_l[INTRA_L_DOWN_RIGHT] = intra_pred_down_right;
1134
    h->intra_pred_l[   INTRA_L_LP_LEFT] = intra_pred_lp_left;
1135
    h->intra_pred_l[    INTRA_L_LP_TOP] = intra_pred_lp_top;
1136
    h->intra_pred_l[    INTRA_L_DC_128] = intra_pred_dc_128;
1137
    h->intra_pred_c[        INTRA_C_LP] = intra_pred_lp;
1138
    h->intra_pred_c[     INTRA_C_HORIZ] = intra_pred_horiz;
1139
    h->intra_pred_c[      INTRA_C_VERT] = intra_pred_vert;
1140
    h->intra_pred_c[     INTRA_C_PLANE] = intra_pred_plane;
1141
    h->intra_pred_c[   INTRA_C_LP_LEFT] = intra_pred_lp_left;
1142
    h->intra_pred_c[    INTRA_C_LP_TOP] = intra_pred_lp_top;
1143
    h->intra_pred_c[    INTRA_C_DC_128] = intra_pred_dc_128;
1144
    h->mv[ 7] = ff_cavs_un_mv;
1145
    h->mv[19] = ff_cavs_un_mv;
1146
    return 0;
1147
}
1148

    
1149
int ff_cavs_end(AVCodecContext *avctx) {
1150
    AVSContext *h = avctx->priv_data;
1151

    
1152
    av_free(h->top_qp);
1153
    av_free(h->top_mv[0]);
1154
    av_free(h->top_mv[1]);
1155
    av_free(h->top_pred_Y);
1156
    av_free(h->top_border_y);
1157
    av_free(h->top_border_u);
1158
    av_free(h->top_border_v);
1159
    av_free(h->col_mv);
1160
    av_free(h->col_type_base);
1161
    av_free(h->block);
1162
    return 0;
1163
}
1164

    
1165
AVCodec cavs_decoder = {
1166
    "cavs",
1167
    CODEC_TYPE_VIDEO,
1168
    CODEC_ID_CAVS,
1169
    sizeof(AVSContext),
1170
    ff_cavs_init,
1171
    NULL,
1172
    ff_cavs_end,
1173
    cavs_decode_frame,
1174
    CODEC_CAP_DR1 | CODEC_CAP_DELAY,
1175
    .flush= cavs_flush,
1176
};