Mercurial > libavcodec.hg
comparison h264.c @ 4975:9a6a0818e93f libavcodec
split h264.c to move parser in its own file
author | aurel |
---|---|
date | Thu, 10 May 2007 22:26:44 +0000 |
parents | 0d1cc37d9430 |
children | 7df30f671791 |
comparison
equal
deleted
inserted
replaced
4974:a2e489e40ea3 | 4975:9a6a0818e93f |
---|---|
28 | 28 |
29 #include "common.h" | 29 #include "common.h" |
30 #include "dsputil.h" | 30 #include "dsputil.h" |
31 #include "avcodec.h" | 31 #include "avcodec.h" |
32 #include "mpegvideo.h" | 32 #include "mpegvideo.h" |
33 #include "h264.h" | |
33 #include "h264data.h" | 34 #include "h264data.h" |
35 #include "h264_parser.h" | |
34 #include "golomb.h" | 36 #include "golomb.h" |
35 | 37 |
36 #include "cabac.h" | 38 #include "cabac.h" |
37 | 39 |
38 //#undef NDEBUG | 40 //#undef NDEBUG |
39 #include <assert.h> | 41 #include <assert.h> |
40 | |
41 #define interlaced_dct interlaced_dct_is_a_bad_name | |
42 #define mb_intra mb_intra_isnt_initalized_see_mb_type | |
43 | |
44 #define LUMA_DC_BLOCK_INDEX 25 | |
45 #define CHROMA_DC_BLOCK_INDEX 26 | |
46 | |
47 #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8 | |
48 #define COEFF_TOKEN_VLC_BITS 8 | |
49 #define TOTAL_ZEROS_VLC_BITS 9 | |
50 #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3 | |
51 #define RUN_VLC_BITS 3 | |
52 #define RUN7_VLC_BITS 6 | |
53 | |
54 #define MAX_SPS_COUNT 32 | |
55 #define MAX_PPS_COUNT 256 | |
56 | |
57 #define MAX_MMCO_COUNT 66 | |
58 | |
59 /* Compiling in interlaced support reduces the speed | |
60 * of progressive decoding by about 2%. */ | |
61 #define ALLOW_INTERLACE | |
62 | |
63 #ifdef ALLOW_INTERLACE | |
64 #define MB_MBAFF h->mb_mbaff | |
65 #define MB_FIELD h->mb_field_decoding_flag | |
66 #define FRAME_MBAFF h->mb_aff_frame | |
67 #else | |
68 #define MB_MBAFF 0 | |
69 #define MB_FIELD 0 | |
70 #define FRAME_MBAFF 0 | |
71 #undef IS_INTERLACED | |
72 #define IS_INTERLACED(mb_type) 0 | |
73 #endif | |
74 | |
75 /** | |
76 * Sequence parameter set | |
77 */ | |
78 typedef struct SPS{ | |
79 | |
80 int profile_idc; | |
81 int level_idc; | |
82 int transform_bypass; ///< qpprime_y_zero_transform_bypass_flag | |
83 int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4 | |
84 int poc_type; ///< pic_order_cnt_type | |
85 int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4 | |
86 int delta_pic_order_always_zero_flag; | |
87 int offset_for_non_ref_pic; | |
88 int offset_for_top_to_bottom_field; | |
89 int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle | |
90 int ref_frame_count; ///< num_ref_frames | |
91 int gaps_in_frame_num_allowed_flag; | |
92 int mb_width; ///< frame_width_in_mbs_minus1 + 1 | |
93 int mb_height; ///< frame_height_in_mbs_minus1 + 1 | |
94 int frame_mbs_only_flag; | |
95 int mb_aff; ///<mb_adaptive_frame_field_flag | |
96 int direct_8x8_inference_flag; | |
97 int crop; ///< frame_cropping_flag | |
98 int crop_left; ///< frame_cropping_rect_left_offset | |
99 int crop_right; ///< frame_cropping_rect_right_offset | |
100 int crop_top; ///< frame_cropping_rect_top_offset | |
101 int crop_bottom; ///< frame_cropping_rect_bottom_offset | |
102 int vui_parameters_present_flag; | |
103 AVRational sar; | |
104 int timing_info_present_flag; | |
105 uint32_t num_units_in_tick; | |
106 uint32_t time_scale; | |
107 int fixed_frame_rate_flag; | |
108 short offset_for_ref_frame[256]; //FIXME dyn aloc? | |
109 int bitstream_restriction_flag; | |
110 int num_reorder_frames; | |
111 int scaling_matrix_present; | |
112 uint8_t scaling_matrix4[6][16]; | |
113 uint8_t scaling_matrix8[2][64]; | |
114 }SPS; | |
115 | |
116 /** | |
117 * Picture parameter set | |
118 */ | |
119 typedef struct PPS{ | |
120 unsigned int sps_id; | |
121 int cabac; ///< entropy_coding_mode_flag | |
122 int pic_order_present; ///< pic_order_present_flag | |
123 int slice_group_count; ///< num_slice_groups_minus1 + 1 | |
124 int mb_slice_group_map_type; | |
125 unsigned int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1 | |
126 int weighted_pred; ///< weighted_pred_flag | |
127 int weighted_bipred_idc; | |
128 int init_qp; ///< pic_init_qp_minus26 + 26 | |
129 int init_qs; ///< pic_init_qs_minus26 + 26 | |
130 int chroma_qp_index_offset; | |
131 int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag | |
132 int constrained_intra_pred; ///< constrained_intra_pred_flag | |
133 int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag | |
134 int transform_8x8_mode; ///< transform_8x8_mode_flag | |
135 uint8_t scaling_matrix4[6][16]; | |
136 uint8_t scaling_matrix8[2][64]; | |
137 }PPS; | |
138 | |
139 /** | |
140 * Memory management control operation opcode. | |
141 */ | |
142 typedef enum MMCOOpcode{ | |
143 MMCO_END=0, | |
144 MMCO_SHORT2UNUSED, | |
145 MMCO_LONG2UNUSED, | |
146 MMCO_SHORT2LONG, | |
147 MMCO_SET_MAX_LONG, | |
148 MMCO_RESET, | |
149 MMCO_LONG, | |
150 } MMCOOpcode; | |
151 | |
152 /** | |
153 * Memory management control operation. | |
154 */ | |
155 typedef struct MMCO{ | |
156 MMCOOpcode opcode; | |
157 int short_frame_num; | |
158 int long_index; | |
159 } MMCO; | |
160 | |
161 /** | |
162 * H264Context | |
163 */ | |
164 typedef struct H264Context{ | |
165 MpegEncContext s; | |
166 int nal_ref_idc; | |
167 int nal_unit_type; | |
168 uint8_t *rbsp_buffer; | |
169 unsigned int rbsp_buffer_size; | |
170 | |
171 /** | |
172 * Used to parse AVC variant of h264 | |
173 */ | |
174 int is_avc; ///< this flag is != 0 if codec is avc1 | |
175 int got_avcC; ///< flag used to parse avcC data only once | |
176 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4) | |
177 | |
178 int chroma_qp; //QPc | |
179 | |
180 int prev_mb_skipped; | |
181 int next_mb_skipped; | |
182 | |
183 //prediction stuff | |
184 int chroma_pred_mode; | |
185 int intra16x16_pred_mode; | |
186 | |
187 int top_mb_xy; | |
188 int left_mb_xy[2]; | |
189 | |
190 int8_t intra4x4_pred_mode_cache[5*8]; | |
191 int8_t (*intra4x4_pred_mode)[8]; | |
192 void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp? | |
193 void (*pred8x8l [9+3])(uint8_t *src, int topleft, int topright, int stride); | |
194 void (*pred8x8 [4+3])(uint8_t *src, int stride); | |
195 void (*pred16x16[4+3])(uint8_t *src, int stride); | |
196 unsigned int topleft_samples_available; | |
197 unsigned int top_samples_available; | |
198 unsigned int topright_samples_available; | |
199 unsigned int left_samples_available; | |
200 uint8_t (*top_borders[2])[16+2*8]; | |
201 uint8_t left_border[2*(17+2*9)]; | |
202 | |
203 /** | |
204 * non zero coeff count cache. | |
205 * is 64 if not available. | |
206 */ | |
207 DECLARE_ALIGNED_8(uint8_t, non_zero_count_cache[6*8]); | |
208 uint8_t (*non_zero_count)[16]; | |
209 | |
210 /** | |
211 * Motion vector cache. | |
212 */ | |
213 DECLARE_ALIGNED_8(int16_t, mv_cache[2][5*8][2]); | |
214 DECLARE_ALIGNED_8(int8_t, ref_cache[2][5*8]); | |
215 #define LIST_NOT_USED -1 //FIXME rename? | |
216 #define PART_NOT_AVAILABLE -2 | |
217 | |
218 /** | |
219 * is 1 if the specific list MV&references are set to 0,0,-2. | |
220 */ | |
221 int mv_cache_clean[2]; | |
222 | |
223 /** | |
224 * number of neighbors (top and/or left) that used 8x8 dct | |
225 */ | |
226 int neighbor_transform_size; | |
227 | |
228 /** | |
229 * block_offset[ 0..23] for frame macroblocks | |
230 * block_offset[24..47] for field macroblocks | |
231 */ | |
232 int block_offset[2*(16+8)]; | |
233 | |
234 uint32_t *mb2b_xy; //FIXME are these 4 a good idea? | |
235 uint32_t *mb2b8_xy; | |
236 int b_stride; //FIXME use s->b4_stride | |
237 int b8_stride; | |
238 | |
239 int mb_linesize; ///< may be equal to s->linesize or s->linesize*2, for mbaff | |
240 int mb_uvlinesize; | |
241 | |
242 int emu_edge_width; | |
243 int emu_edge_height; | |
244 | |
245 int halfpel_flag; | |
246 int thirdpel_flag; | |
247 | |
248 int unknown_svq3_flag; | |
249 int next_slice_index; | |
250 | |
251 SPS sps_buffer[MAX_SPS_COUNT]; | |
252 SPS sps; ///< current sps | |
253 | |
254 PPS pps_buffer[MAX_PPS_COUNT]; | |
255 /** | |
256 * current pps | |
257 */ | |
258 PPS pps; //FIXME move to Picture perhaps? (->no) do we need that? | |
259 | |
260 uint32_t dequant4_buffer[6][52][16]; | |
261 uint32_t dequant8_buffer[2][52][64]; | |
262 uint32_t (*dequant4_coeff[6])[16]; | |
263 uint32_t (*dequant8_coeff[2])[64]; | |
264 int dequant_coeff_pps; ///< reinit tables when pps changes | |
265 | |
266 int slice_num; | |
267 uint8_t *slice_table_base; | |
268 uint8_t *slice_table; ///< slice_table_base + 2*mb_stride + 1 | |
269 int slice_type; | |
270 int slice_type_fixed; | |
271 | |
272 //interlacing specific flags | |
273 int mb_aff_frame; | |
274 int mb_field_decoding_flag; | |
275 int mb_mbaff; ///< mb_aff_frame && mb_field_decoding_flag | |
276 | |
277 unsigned int sub_mb_type[4]; | |
278 | |
279 //POC stuff | |
280 int poc_lsb; | |
281 int poc_msb; | |
282 int delta_poc_bottom; | |
283 int delta_poc[2]; | |
284 int frame_num; | |
285 int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0 | |
286 int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0 | |
287 int frame_num_offset; ///< for POC type 2 | |
288 int prev_frame_num_offset; ///< for POC type 2 | |
289 int prev_frame_num; ///< frame_num of the last pic for POC type 1/2 | |
290 | |
291 /** | |
292 * frame_num for frames or 2*frame_num for field pics. | |
293 */ | |
294 int curr_pic_num; | |
295 | |
296 /** | |
297 * max_frame_num or 2*max_frame_num for field pics. | |
298 */ | |
299 int max_pic_num; | |
300 | |
301 //Weighted pred stuff | |
302 int use_weight; | |
303 int use_weight_chroma; | |
304 int luma_log2_weight_denom; | |
305 int chroma_log2_weight_denom; | |
306 int luma_weight[2][48]; | |
307 int luma_offset[2][48]; | |
308 int chroma_weight[2][48][2]; | |
309 int chroma_offset[2][48][2]; | |
310 int implicit_weight[48][48]; | |
311 | |
312 //deblock | |
313 int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0 | |
314 int slice_alpha_c0_offset; | |
315 int slice_beta_offset; | |
316 | |
317 int redundant_pic_count; | |
318 | |
319 int direct_spatial_mv_pred; | |
320 int dist_scale_factor[16]; | |
321 int dist_scale_factor_field[32]; | |
322 int map_col_to_list0[2][16]; | |
323 int map_col_to_list0_field[2][32]; | |
324 | |
325 /** | |
326 * num_ref_idx_l0/1_active_minus1 + 1 | |
327 */ | |
328 unsigned int ref_count[2]; ///< counts frames or fields, depending on current mb mode | |
329 unsigned int list_count; | |
330 Picture *short_ref[32]; | |
331 Picture *long_ref[32]; | |
332 Picture default_ref_list[2][32]; | |
333 Picture ref_list[2][48]; ///< 0..15: frame refs, 16..47: mbaff field refs | |
334 Picture *delayed_pic[18]; //FIXME size? | |
335 Picture *delayed_output_pic; | |
336 | |
337 /** | |
338 * memory management control operations buffer. | |
339 */ | |
340 MMCO mmco[MAX_MMCO_COUNT]; | |
341 int mmco_index; | |
342 | |
343 int long_ref_count; ///< number of actual long term references | |
344 int short_ref_count; ///< number of actual short term references | |
345 | |
346 //data partitioning | |
347 GetBitContext intra_gb; | |
348 GetBitContext inter_gb; | |
349 GetBitContext *intra_gb_ptr; | |
350 GetBitContext *inter_gb_ptr; | |
351 | |
352 DECLARE_ALIGNED_8(DCTELEM, mb[16*24]); | |
353 DCTELEM mb_padding[256]; ///< as mb is addressed by scantable[i] and scantable is uint8_t we can either check that i is not to large or ensure that there is some unused stuff after mb | |
354 | |
355 /** | |
356 * Cabac | |
357 */ | |
358 CABACContext cabac; | |
359 uint8_t cabac_state[460]; | |
360 int cabac_init_idc; | |
361 | |
362 /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */ | |
363 uint16_t *cbp_table; | |
364 int cbp; | |
365 int top_cbp; | |
366 int left_cbp; | |
367 /* chroma_pred_mode for i4x4 or i16x16, else 0 */ | |
368 uint8_t *chroma_pred_mode_table; | |
369 int last_qscale_diff; | |
370 int16_t (*mvd_table[2])[2]; | |
371 DECLARE_ALIGNED_8(int16_t, mvd_cache[2][5*8][2]); | |
372 uint8_t *direct_table; | |
373 uint8_t direct_cache[5*8]; | |
374 | |
375 uint8_t zigzag_scan[16]; | |
376 uint8_t zigzag_scan8x8[64]; | |
377 uint8_t zigzag_scan8x8_cavlc[64]; | |
378 uint8_t field_scan[16]; | |
379 uint8_t field_scan8x8[64]; | |
380 uint8_t field_scan8x8_cavlc[64]; | |
381 const uint8_t *zigzag_scan_q0; | |
382 const uint8_t *zigzag_scan8x8_q0; | |
383 const uint8_t *zigzag_scan8x8_cavlc_q0; | |
384 const uint8_t *field_scan_q0; | |
385 const uint8_t *field_scan8x8_q0; | |
386 const uint8_t *field_scan8x8_cavlc_q0; | |
387 | |
388 int x264_build; | |
389 }H264Context; | |
390 | 42 |
391 static VLC coeff_token_vlc[4]; | 43 static VLC coeff_token_vlc[4]; |
392 static VLC chroma_dc_coeff_token_vlc; | 44 static VLC chroma_dc_coeff_token_vlc; |
393 | 45 |
394 static VLC total_zeros_vlc[15]; | 46 static VLC total_zeros_vlc[15]; |
7980 } | 7632 } |
7981 | 7633 |
7982 return 0; | 7634 return 0; |
7983 } | 7635 } |
7984 | 7636 |
7985 /** | |
7986 * finds the end of the current frame in the bitstream. | |
7987 * @return the position of the first byte of the next frame, or -1 | |
7988 */ | |
7989 static int find_frame_end(H264Context *h, const uint8_t *buf, int buf_size){ | |
7990 int i; | |
7991 uint32_t state; | |
7992 ParseContext *pc = &(h->s.parse_context); | |
7993 //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]); | |
7994 // mb_addr= pc->mb_addr - 1; | |
7995 state= pc->state; | |
7996 if(state>13) | |
7997 state= 7; | |
7998 | |
7999 for(i=0; i<buf_size; i++){ | |
8000 if(state==7){ | |
8001 for(; i<buf_size; i++){ | |
8002 if(!buf[i]){ | |
8003 state=2; | |
8004 break; | |
8005 } | |
8006 } | |
8007 }else if(state<=2){ | |
8008 if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5 | |
8009 else if(buf[i]) state = 7; | |
8010 else state>>=1; //2->1, 1->0, 0->0 | |
8011 }else if(state<=5){ | |
8012 int v= buf[i] & 0x1F; | |
8013 if(v==7 || v==8 || v==9){ | |
8014 if(pc->frame_start_found){ | |
8015 i++; | |
8016 found: | |
8017 pc->state=7; | |
8018 pc->frame_start_found= 0; | |
8019 return i-(state&5); | |
8020 } | |
8021 }else if(v==1 || v==2 || v==5){ | |
8022 if(pc->frame_start_found){ | |
8023 state+=8; | |
8024 continue; | |
8025 }else | |
8026 pc->frame_start_found = 1; | |
8027 } | |
8028 state= 7; | |
8029 }else{ | |
8030 if(buf[i] & 0x80) | |
8031 goto found; | |
8032 state= 7; | |
8033 } | |
8034 } | |
8035 pc->state= state; | |
8036 return END_NOT_FOUND; | |
8037 } | |
8038 | |
8039 #ifdef CONFIG_H264_PARSER | |
8040 static int h264_parse(AVCodecParserContext *s, | |
8041 AVCodecContext *avctx, | |
8042 const uint8_t **poutbuf, int *poutbuf_size, | |
8043 const uint8_t *buf, int buf_size) | |
8044 { | |
8045 H264Context *h = s->priv_data; | |
8046 ParseContext *pc = &h->s.parse_context; | |
8047 int next; | |
8048 | |
8049 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ | |
8050 next= buf_size; | |
8051 }else{ | |
8052 next= find_frame_end(h, buf, buf_size); | |
8053 | |
8054 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { | |
8055 *poutbuf = NULL; | |
8056 *poutbuf_size = 0; | |
8057 return buf_size; | |
8058 } | |
8059 | |
8060 if(next<0 && next != END_NOT_FOUND){ | |
8061 assert(pc->last_index + next >= 0 ); | |
8062 find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state | |
8063 } | |
8064 } | |
8065 | |
8066 *poutbuf = buf; | |
8067 *poutbuf_size = buf_size; | |
8068 return next; | |
8069 } | |
8070 | |
8071 static int h264_split(AVCodecContext *avctx, | |
8072 const uint8_t *buf, int buf_size) | |
8073 { | |
8074 int i; | |
8075 uint32_t state = -1; | |
8076 int has_sps= 0; | |
8077 | |
8078 for(i=0; i<=buf_size; i++){ | |
8079 if((state&0xFFFFFF1F) == 0x107) | |
8080 has_sps=1; | |
8081 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){ | |
8082 }*/ | |
8083 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){ | |
8084 if(has_sps){ | |
8085 while(i>4 && buf[i-5]==0) i--; | |
8086 return i-4; | |
8087 } | |
8088 } | |
8089 if (i<buf_size) | |
8090 state= (state<<8) | buf[i]; | |
8091 } | |
8092 return 0; | |
8093 } | |
8094 #endif /* CONFIG_H264_PARSER */ | |
8095 | |
8096 static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){ | 7637 static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){ |
8097 MpegEncContext * const s = &h->s; | 7638 MpegEncContext * const s = &h->s; |
8098 AVCodecContext * const avctx= s->avctx; | 7639 AVCodecContext * const avctx= s->avctx; |
8099 int buf_index=0; | 7640 int buf_index=0; |
8100 #if 0 | 7641 #if 0 |
8299 | 7840 |
8300 return 0; | 7841 return 0; |
8301 } | 7842 } |
8302 | 7843 |
8303 if(s->flags&CODEC_FLAG_TRUNCATED){ | 7844 if(s->flags&CODEC_FLAG_TRUNCATED){ |
8304 int next= find_frame_end(h, buf, buf_size); | 7845 int next= ff_h264_find_frame_end(h, buf, buf_size); |
8305 | 7846 |
8306 if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 ) | 7847 if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 ) |
8307 return buf_size; | 7848 return buf_size; |
8308 //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index); | 7849 //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index); |
8309 } | 7850 } |
8690 decode_frame, | 8231 decode_frame, |
8691 /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY, | 8232 /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY, |
8692 .flush= flush_dpb, | 8233 .flush= flush_dpb, |
8693 }; | 8234 }; |
8694 | 8235 |
8695 #ifdef CONFIG_H264_PARSER | |
8696 AVCodecParser h264_parser = { | |
8697 { CODEC_ID_H264 }, | |
8698 sizeof(H264Context), | |
8699 NULL, | |
8700 h264_parse, | |
8701 ff_parse_close, | |
8702 h264_split, | |
8703 }; | |
8704 #endif | |
8705 | |
8706 #include "svq3.c" | 8236 #include "svq3.c" |