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 Libav.
6 *
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef AVCODEC_CAVS_H
23#define AVCODEC_CAVS_H
24
25#include "dsputil.h"
26#include "mpegvideo.h"
27#include "cavsdsp.h"
28
29#define SLICE_MAX_START_CODE    0x000001af
30#define EXT_START_CODE          0x000001b5
31#define USER_START_CODE         0x000001b2
32#define CAVS_START_CODE         0x000001b0
33#define PIC_I_START_CODE        0x000001b3
34#define PIC_PB_START_CODE       0x000001b6
35
36#define A_AVAIL                          1
37#define B_AVAIL                          2
38#define C_AVAIL                          4
39#define D_AVAIL                          8
40#define NOT_AVAIL                       -1
41#define REF_INTRA                       -2
42#define REF_DIR                         -3
43
44#define ESCAPE_CODE                     59
45
46#define FWD0                          0x01
47#define FWD1                          0x02
48#define BWD0                          0x04
49#define BWD1                          0x08
50#define SYM0                          0x10
51#define SYM1                          0x20
52#define SPLITH                        0x40
53#define SPLITV                        0x80
54
55#define MV_BWD_OFFS                     12
56#define MV_STRIDE                        4
57
58enum cavs_mb {
59  I_8X8 = 0,
60  P_SKIP,
61  P_16X16,
62  P_16X8,
63  P_8X16,
64  P_8X8,
65  B_SKIP,
66  B_DIRECT,
67  B_FWD_16X16,
68  B_BWD_16X16,
69  B_SYM_16X16,
70  B_8X8 = 29
71};
72
73enum cavs_sub_mb {
74  B_SUB_DIRECT,
75  B_SUB_FWD,
76  B_SUB_BWD,
77  B_SUB_SYM
78};
79
80enum cavs_intra_luma {
81  INTRA_L_VERT,
82  INTRA_L_HORIZ,
83  INTRA_L_LP,
84  INTRA_L_DOWN_LEFT,
85  INTRA_L_DOWN_RIGHT,
86  INTRA_L_LP_LEFT,
87  INTRA_L_LP_TOP,
88  INTRA_L_DC_128
89};
90
91enum cavs_intra_chroma {
92  INTRA_C_LP,
93  INTRA_C_HORIZ,
94  INTRA_C_VERT,
95  INTRA_C_PLANE,
96  INTRA_C_LP_LEFT,
97  INTRA_C_LP_TOP,
98  INTRA_C_DC_128,
99};
100
101enum cavs_mv_pred {
102  MV_PRED_MEDIAN,
103  MV_PRED_LEFT,
104  MV_PRED_TOP,
105  MV_PRED_TOPRIGHT,
106  MV_PRED_PSKIP,
107  MV_PRED_BSKIP
108};
109
110enum cavs_block {
111  BLK_16X16,
112  BLK_16X8,
113  BLK_8X16,
114  BLK_8X8
115};
116
117enum cavs_mv_loc {
118  MV_FWD_D3 = 0,
119  MV_FWD_B2,
120  MV_FWD_B3,
121  MV_FWD_C2,
122  MV_FWD_A1,
123  MV_FWD_X0,
124  MV_FWD_X1,
125  MV_FWD_A3 = 8,
126  MV_FWD_X2,
127  MV_FWD_X3,
128  MV_BWD_D3 = MV_BWD_OFFS,
129  MV_BWD_B2,
130  MV_BWD_B3,
131  MV_BWD_C2,
132  MV_BWD_A1,
133  MV_BWD_X0,
134  MV_BWD_X1,
135  MV_BWD_A3 = MV_BWD_OFFS+8,
136  MV_BWD_X2,
137  MV_BWD_X3
138};
139
140DECLARE_ALIGNED(8, typedef, struct) {
141    int16_t x;
142    int16_t y;
143    int16_t dist;
144    int16_t ref;
145} cavs_vector;
146
147struct dec_2dvlc {
148  int8_t rltab[59][3];
149  int8_t level_add[27];
150  int8_t golomb_order;
151  int inc_limit;
152  int8_t max_run;
153};
154
155typedef struct {
156    MpegEncContext s;
157    CAVSDSPContext cdsp;
158    Picture picture; ///< currently decoded frame
159    Picture DPB[2];  ///< reference frames
160    int dist[2];     ///< temporal distances from current frame to ref frames
161    int profile, level;
162    int aspect_ratio;
163    int mb_width, mb_height;
164    int pic_type;
165    int stream_revision; ///<0 for samples from 2006, 1 for rm52j encoder
166    int progressive;
167    int pic_structure;
168    int skip_mode_flag; ///< select between skip_count or one skip_flag per MB
169    int loop_filter_disable;
170    int alpha_offset, beta_offset;
171    int ref_flag;
172    int mbx, mby, mbidx; ///< macroblock coordinates
173    int flags;         ///< availability flags of neighbouring macroblocks
174    int stc;           ///< last start code
175    uint8_t *cy, *cu, *cv; ///< current MB sample pointers
176    int left_qp;
177    uint8_t *top_qp;
178
179    /** mv motion vector cache
180       0:    D3  B2  B3  C2
181       4:    A1  X0  X1   -
182       8:    A3  X2  X3   -
183
184       X are the vectors in the current macroblock (5,6,9,10)
185       A is the macroblock to the left (4,8)
186       B is the macroblock to the top (1,2)
187       C is the macroblock to the top-right (3)
188       D is the macroblock to the top-left (0)
189
190       the same is repeated for backward motion vectors */
191    cavs_vector mv[2*4*3];
192    cavs_vector *top_mv[2];
193    cavs_vector *col_mv;
194
195    /** luma pred mode cache
196       0:    --  B2  B3
197       3:    A1  X0  X1
198       6:    A3  X2  X3   */
199    int pred_mode_Y[3*3];
200    int *top_pred_Y;
201    int l_stride, c_stride;
202    int luma_scan[4];
203    int qp;
204    int qp_fixed;
205    int cbp;
206    ScanTable scantable;
207
208    /** intra prediction is done with un-deblocked samples
209     they are saved here before deblocking the MB  */
210    uint8_t *top_border_y, *top_border_u, *top_border_v;
211    uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
212    uint8_t intern_border_y[26];
213    uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
214
215    void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
216    void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
217    uint8_t *col_type_base;
218
219    /* scaling factors for MV prediction */
220    int sym_factor;    ///< for scaling in symmetrical B block
221    int direct_den[2]; ///< for scaling in direct B block
222    int scale_den[2];  ///< for scaling neighbouring MVs
223
224    int got_keyframe;
225    DCTELEM *block;
226} AVSContext;
227
228extern const uint8_t     ff_cavs_dequant_shift[64];
229extern const uint16_t    ff_cavs_dequant_mul[64];
230extern const struct dec_2dvlc ff_cavs_intra_dec[7];
231extern const struct dec_2dvlc ff_cavs_inter_dec[7];
232extern const struct dec_2dvlc ff_cavs_chroma_dec[5];
233extern const uint8_t     ff_cavs_chroma_qp[64];
234extern const uint8_t     ff_cavs_scan3x3[4];
235extern const uint8_t     ff_cavs_partition_flags[30];
236extern const int8_t      ff_left_modifier_l[8];
237extern const int8_t      ff_top_modifier_l[8];
238extern const int8_t      ff_left_modifier_c[7];
239extern const int8_t      ff_top_modifier_c[7];
240extern const cavs_vector ff_cavs_intra_mv;
241extern const cavs_vector ff_cavs_un_mv;
242extern const cavs_vector ff_cavs_dir_mv;
243
244static inline void modify_pred(const int8_t *mod_table, int *mode)
245{
246    *mode = mod_table[*mode];
247    if(*mode < 0) {
248        av_log(NULL, AV_LOG_ERROR, "Illegal intra prediction mode\n");
249        *mode = 0;
250    }
251}
252
253static inline void set_intra_mode_default(AVSContext *h) {
254    if(h->stream_revision > 0) {
255        h->pred_mode_Y[3] =  h->pred_mode_Y[6] = NOT_AVAIL;
256        h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = NOT_AVAIL;
257    } else {
258        h->pred_mode_Y[3] =  h->pred_mode_Y[6] = INTRA_L_LP;
259        h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
260    }
261}
262
263static inline void set_mvs(cavs_vector *mv, enum cavs_block size) {
264    switch(size) {
265    case BLK_16X16:
266        mv[MV_STRIDE  ] = mv[0];
267        mv[MV_STRIDE+1] = mv[0];
268    case BLK_16X8:
269        mv[1] = mv[0];
270        break;
271    case BLK_8X16:
272        mv[MV_STRIDE] = mv[0];
273        break;
274    }
275}
276
277static inline void set_mv_intra(AVSContext *h) {
278    h->mv[MV_FWD_X0] = ff_cavs_intra_mv;
279    set_mvs(&h->mv[MV_FWD_X0], BLK_16X16);
280    h->mv[MV_BWD_X0] = ff_cavs_intra_mv;
281    set_mvs(&h->mv[MV_BWD_X0], BLK_16X16);
282    if(h->pic_type != AV_PICTURE_TYPE_B)
283        h->col_type_base[h->mbidx] = I_8X8;
284}
285
286static inline int dequant(AVSContext *h, DCTELEM *level_buf, uint8_t *run_buf,
287                          DCTELEM *dst, int mul, int shift, int coeff_num) {
288    int round = 1 << (shift - 1);
289    int pos = -1;
290    const uint8_t *scantab = h->scantable.permutated;
291
292    /* inverse scan and dequantization */
293    while(--coeff_num >= 0){
294        pos += run_buf[coeff_num];
295        if(pos > 63) {
296            av_log(h->s.avctx, AV_LOG_ERROR,
297                "position out of block bounds at pic %d MB(%d,%d)\n",
298                h->picture.poc, h->mbx, h->mby);
299            return -1;
300        }
301        dst[scantab[pos]] = (level_buf[coeff_num]*mul + round) >> shift;
302    }
303    return 0;
304}
305
306void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type);
307void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top, uint8_t **left,
308                                  int block);
309void ff_cavs_load_intra_pred_chroma(AVSContext *h);
310void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv);
311void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type);
312void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum cavs_mv_loc nC,
313                enum cavs_mv_pred mode, enum cavs_block size, int ref);
314void ff_cavs_init_mb(AVSContext *h);
315int  ff_cavs_next_mb(AVSContext *h);
316void ff_cavs_init_pic(AVSContext *h);
317void ff_cavs_init_top_lines(AVSContext *h);
318int ff_cavs_init(AVCodecContext *avctx);
319int ff_cavs_end (AVCodecContext *avctx);
320
321#endif /* AVCODEC_CAVS_H */
322