comparison cavs.c @ 3402:34d3e497e310 libavcodec

cosmetic patch: Doxygen-style comments added some more comments
author stefang
date Mon, 03 Jul 2006 17:37:57 +0000
parents adccbf4a1040
children fc0eb7836ccb
comparison
equal deleted inserted replaced
3401:e88c3bc82b15 3402:34d3e497e310
13 * Lesser General Public License for more details. 13 * Lesser General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU Lesser General Public 15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software 16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /**
21 * @file cavs.c
22 * Chinese AVS video (AVS1-P2, JiZhun profile) decoder
23 * @author Stefan Gehrer <stefan.gehrer@gmx.de>
18 */ 24 */
19 25
20 #include "avcodec.h" 26 #include "avcodec.h"
21 #include "bitstream.h" 27 #include "bitstream.h"
22 #include "golomb.h" 28 #include "golomb.h"
23 #include "mpegvideo.h" 29 #include "mpegvideo.h"
24 #include "cavsdata.h" 30 #include "cavsdata.h"
25 31
26 typedef struct { 32 typedef struct {
27 MpegEncContext s; 33 MpegEncContext s;
28 Picture picture; //currently decoded frame 34 Picture picture; ///< currently decoded frame
29 Picture DPB[2]; //reference frames 35 Picture DPB[2]; ///< reference frames
30 int dist[2]; //temporal distances from current frame to ref frames 36 int dist[2]; ///< temporal distances from current frame to ref frames
31 int profile, level; 37 int profile, level;
32 int aspect_ratio; 38 int aspect_ratio;
33 int mb_width, mb_height; 39 int mb_width, mb_height;
34 int pic_type; 40 int pic_type;
35 int progressive; 41 int progressive;
36 int pic_structure; 42 int pic_structure;
37 int skip_mode_flag; 43 int skip_mode_flag; ///< select between skip_count or one skip_flag per MB
38 int loop_filter_disable; 44 int loop_filter_disable;
39 int alpha_offset, beta_offset; 45 int alpha_offset, beta_offset;
40 int ref_flag; 46 int ref_flag;
41 int mbx, mby; 47 int mbx, mby; ///< macroblock coordinates
42 int flags; 48 int flags; ///< availability flags of neighbouring macroblocks
43 int stc; 49 int stc; ///< last start code
44 uint8_t *cy, *cu, *cv; 50 uint8_t *cy, *cu, *cv; ///< current MB sample pointers
45 int left_qp; 51 int left_qp;
46 uint8_t *top_qp; 52 uint8_t *top_qp;
47 53
48 /* mv motion vector cache 54 /** mv motion vector cache
49 0: D3 B2 B3 C2 55 0: D3 B2 B3 C2
50 4: A1 X0 X1 - 56 4: A1 X0 X1 -
51 8: A3 X2 X3 - 57 8: A3 X2 X3 -
52 58
53 X are the vectors in the current macroblock (5,6,9,10) 59 X are the vectors in the current macroblock (5,6,9,10)
59 the same is repeated for backward motion vectors */ 65 the same is repeated for backward motion vectors */
60 vector_t mv[2*4*3]; 66 vector_t mv[2*4*3];
61 vector_t *top_mv[2]; 67 vector_t *top_mv[2];
62 vector_t *col_mv; 68 vector_t *col_mv;
63 69
64 /* luma pred mode cache 70 /** luma pred mode cache
65 0: -- B2 B3 71 0: -- B2 B3
66 3: A1 X0 X1 72 3: A1 X0 X1
67 6: A3 X2 X3 */ 73 6: A3 X2 X3 */
68 int pred_mode_Y[3*3]; 74 int pred_mode_Y[3*3];
69 int *top_pred_Y; 75 int *top_pred_Y;
71 int luma_scan[4]; 77 int luma_scan[4];
72 int qp; 78 int qp;
73 int qp_fixed; 79 int qp_fixed;
74 int cbp; 80 int cbp;
75 81
76 /* intra prediction is done with un-deblocked samples 82 /** intra prediction is done with un-deblocked samples
77 they are saved here before deblocking the MB */ 83 they are saved here before deblocking the MB */
78 uint8_t *top_border_y, *top_border_u, *top_border_v; 84 uint8_t *top_border_y, *top_border_u, *top_border_v;
79 uint8_t left_border_y[16], left_border_u[10], left_border_v[10]; 85 uint8_t left_border_y[16], left_border_u[10], left_border_v[10];
80 uint8_t topleft_border_y, topleft_border_u, topleft_border_v; 86 uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
81 87
82 void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride); 88 void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
83 void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride); 89 void (*intra_pred_c[7])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
84 uint8_t *col_type_base; 90 uint8_t *col_type_base;
85 uint8_t *col_type; 91 uint8_t *col_type;
86 int sym_factor; 92
87 int direct_den[2]; 93 /* scaling factors for MV prediction */
88 int scale_den[2]; 94 int sym_factor; ///< for scaling in symmetrical B block
95 int direct_den[2]; ///< for scaling in direct B block
96 int scale_den[2]; ///< for scaling neighbouring MVs
97
89 int got_keyframe; 98 int got_keyframe;
90 } AVSContext; 99 } AVSContext;
91 100
92 /***************************************************************************** 101 /*****************************************************************************
93 * 102 *
118 return 1; 127 return 1;
119 } 128 }
120 return 0; 129 return 0;
121 } 130 }
122 131
123 /* boundary strength (bs) mapping: 132 #define SET_PARAMS \
133 alpha = alpha_tab[clip(qp_avg + h->alpha_offset,0,63)]; \
134 beta = beta_tab[clip(qp_avg + h->beta_offset, 0,63)]; \
135 tc = tc_tab[clip(qp_avg + h->alpha_offset,0,63)];
136
137 /**
138 * in-loop deblocking filter for a single macroblock
139 *
140 * boundary strength (bs) mapping:
124 * 141 *
125 * --4---5-- 142 * --4---5--
126 * 0 2 | 143 * 0 2 |
127 * | 6 | 7 | 144 * | 6 | 7 |
128 * 1 3 | 145 * 1 3 |
129 * --------- 146 * ---------
130 * 147 *
131 */ 148 */
132
133 #define SET_PARAMS \
134 alpha = alpha_tab[clip(qp_avg + h->alpha_offset,0,63)]; \
135 beta = beta_tab[clip(qp_avg + h->beta_offset, 0,63)]; \
136 tc = tc_tab[clip(qp_avg + h->alpha_offset,0,63)];
137
138 static void filter_mb(AVSContext *h, enum mb_t mb_type) { 149 static void filter_mb(AVSContext *h, enum mb_t mb_type) {
139 DECLARE_ALIGNED_8(uint8_t, bs[8]); 150 DECLARE_ALIGNED_8(uint8_t, bs[8]);
140 int qp_avg, alpha, beta, tc; 151 int qp_avg, alpha, beta, tc;
141 int i; 152 int i;
142 153
663 * 674 *
664 * residual data decoding 675 * residual data decoding
665 * 676 *
666 ****************************************************************************/ 677 ****************************************************************************/
667 678
668 /* kth-order exponential golomb code */ 679 /** kth-order exponential golomb code */
669 static inline int get_ue_code(GetBitContext *gb, int order) { 680 static inline int get_ue_code(GetBitContext *gb, int order) {
670 if(order) { 681 if(order) {
671 int ret = get_ue_golomb(gb) << order; 682 int ret = get_ue_golomb(gb) << order;
672 return ret + get_bits(gb,order); 683 return ret + get_bits(gb,order);
673 } 684 }
674 return get_ue_golomb(gb); 685 return get_ue_golomb(gb);
675 } 686 }
676 687
688 /**
689 * decode coefficients from one 8x8 block, dequantize, inverse transform
690 * and add them to sample block
691 * @param r pointer to 2D VLC table
692 * @param esc_golomb_order escape codes are k-golomb with this order k
693 * @param qp quantizer
694 * @param dst location of sample block
695 * @param stride line stride in frame buffer
696 */
677 static int decode_residual_block(AVSContext *h, GetBitContext *gb, 697 static int decode_residual_block(AVSContext *h, GetBitContext *gb,
678 const residual_vlc_t *r, int esc_golomb_order, 698 const residual_vlc_t *r, int esc_golomb_order,
679 int qp, uint8_t *dst, int stride) { 699 int qp, uint8_t *dst, int stride) {
680 int i,pos = -1; 700 int i,pos = -1;
681 int level_code, esc_code, level, run, mask; 701 int level_code, esc_code, level, run, mask;
754 * 774 *
755 * macroblock level 775 * macroblock level
756 * 776 *
757 ****************************************************************************/ 777 ****************************************************************************/
758 778
779 /**
780 * initialise predictors for motion vectors and intra prediction
781 */
759 static inline void init_mb(AVSContext *h) { 782 static inline void init_mb(AVSContext *h) {
760 int i; 783 int i;
761 784
762 /* copy predictors from top line (MB B and C) into cache */ 785 /* copy predictors from top line (MB B and C) into cache */
763 for(i=0;i<3;i++) { 786 for(i=0;i<3;i++) {
793 h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx]; 816 h->col_type = &h->col_type_base[h->mby*h->mb_width + h->mbx];
794 } 817 }
795 818
796 static inline void check_for_slice(AVSContext *h); 819 static inline void check_for_slice(AVSContext *h);
797 820
821 /**
822 * save predictors for later macroblocks and increase
823 * macroblock address
824 * @returns 0 if end of frame is reached, 1 otherwise
825 */
798 static inline int next_mb(AVSContext *h) { 826 static inline int next_mb(AVSContext *h) {
799 int i; 827 int i;
800 828
801 h->flags |= A_AVAIL; 829 h->flags |= A_AVAIL;
802 h->cy += 16; 830 h->cy += 16;
1257 mb_skip_p(h); 1285 mb_skip_p(h);
1258 if(!next_mb(h)) 1286 if(!next_mb(h))
1259 goto done; 1287 goto done;
1260 } 1288 }
1261 mb_type = get_ue_golomb(&s->gb) + P_16X16; 1289 mb_type = get_ue_golomb(&s->gb) + P_16X16;
1262 } else { 1290 } else
1263 mb_type = get_ue_golomb(&s->gb) + P_SKIP; 1291 mb_type = get_ue_golomb(&s->gb) + P_SKIP;
1264 }
1265 init_mb(h); 1292 init_mb(h);
1266 if(mb_type > P_8X8) { 1293 if(mb_type > P_8X8) {
1267 h->cbp = cbp_tab[mb_type - P_8X8 - 1][0]; 1294 h->cbp = cbp_tab[mb_type - P_8X8 - 1][0];
1268 decode_mb_i(h,0); 1295 decode_mb_i(h,0);
1269 } else { 1296 } else
1270 decode_mb_p(h,mb_type); 1297 decode_mb_p(h,mb_type);
1271 }
1272 } while(next_mb(h)); 1298 } while(next_mb(h));
1273 } else { //FF_B_TYPE 1299 } else { /* FF_B_TYPE */
1274 do { 1300 do {
1275 if(h->skip_mode_flag) { 1301 if(h->skip_mode_flag) {
1276 skip_count = get_ue_golomb(&s->gb); 1302 skip_count = get_ue_golomb(&s->gb);
1277 for(i=0;i<skip_count;i++) { 1303 for(i=0;i<skip_count;i++) {
1278 init_mb(h); 1304 init_mb(h);
1281 filter_mb(h,B_SKIP); 1307 filter_mb(h,B_SKIP);
1282 if(!next_mb(h)) 1308 if(!next_mb(h))
1283 goto done; 1309 goto done;
1284 } 1310 }
1285 mb_type = get_ue_golomb(&s->gb) + B_DIRECT; 1311 mb_type = get_ue_golomb(&s->gb) + B_DIRECT;
1286 } else { 1312 } else
1287 mb_type = get_ue_golomb(&s->gb) + B_SKIP; 1313 mb_type = get_ue_golomb(&s->gb) + B_SKIP;
1288 }
1289 init_mb(h); 1314 init_mb(h);
1290 if(mb_type > B_8X8) { 1315 if(mb_type > B_8X8) {
1291 h->cbp = cbp_tab[mb_type - B_8X8 - 1][0]; 1316 h->cbp = cbp_tab[mb_type - B_8X8 - 1][0];
1292 decode_mb_i(h,0); 1317 decode_mb_i(h,0);
1293 } else { 1318 } else
1294 decode_mb_b(h,mb_type); 1319 decode_mb_b(h,mb_type);
1295 }
1296 } while(next_mb(h)); 1320 } while(next_mb(h));
1297 } 1321 }
1298 done: 1322 done:
1299 if(h->pic_type != FF_B_TYPE) { 1323 if(h->pic_type != FF_B_TYPE) {
1300 if(h->DPB[1].data[0]) 1324 if(h->DPB[1].data[0])
1310 * 1334 *
1311 * headers and interface 1335 * headers and interface
1312 * 1336 *
1313 ****************************************************************************/ 1337 ****************************************************************************/
1314 1338
1339 /**
1340 * some predictions require data from the top-neighbouring macroblock.
1341 * this data has to be stored for one complete row of macroblocks
1342 * and this storage space is allocated here
1343 */
1315 static void init_top_lines(AVSContext *h) { 1344 static void init_top_lines(AVSContext *h) {
1316 /* alloc top line of predictors */ 1345 /* alloc top line of predictors */
1317 h->top_qp = av_malloc( h->mb_width); 1346 h->top_qp = av_malloc( h->mb_width);
1318 h->top_mv[0] = av_malloc((h->mb_width*2+1)*sizeof(vector_t)); 1347 h->top_mv[0] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));
1319 h->top_mv[1] = av_malloc((h->mb_width*2+1)*sizeof(vector_t)); 1348 h->top_mv[1] = av_malloc((h->mb_width*2+1)*sizeof(vector_t));