Mercurial > libavcodec.hg
annotate h264.c @ 1264:2fa34e615c76 libavcodec
cleanup
author | michaelni |
---|---|
date | Wed, 14 May 2003 23:08:01 +0000 |
parents | 67ee8bab0f28 |
children | ec946cb74397 |
rev | line source |
---|---|
1168 | 1 /* |
2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder | |
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
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 | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 * | |
19 */ | |
20 | |
21 /** | |
22 * @file h264.c | |
23 * H.264 / AVC / MPEG4 part10 codec. | |
24 * @author Michael Niedermayer <michaelni@gmx.at> | |
25 */ | |
26 | |
27 #include "common.h" | |
28 #include "dsputil.h" | |
29 #include "avcodec.h" | |
30 #include "mpegvideo.h" | |
31 #include "h264data.h" | |
32 #include "golomb.h" | |
33 | |
34 #undef NDEBUG | |
35 #include <assert.h> | |
36 | |
37 #define interlaced_dct interlaced_dct_is_a_bad_name | |
38 #define mb_intra mb_intra_isnt_initalized_see_mb_type | |
39 | |
40 #define LUMA_DC_BLOCK_INDEX 25 | |
41 #define CHROMA_DC_BLOCK_INDEX 26 | |
42 | |
43 #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8 | |
44 #define COEFF_TOKEN_VLC_BITS 8 | |
45 #define TOTAL_ZEROS_VLC_BITS 9 | |
46 #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3 | |
47 #define RUN_VLC_BITS 3 | |
48 #define RUN7_VLC_BITS 6 | |
49 | |
50 #define MAX_SPS_COUNT 32 | |
51 #define MAX_PPS_COUNT 256 | |
52 | |
53 #define MAX_MMCO_COUNT 66 | |
54 | |
55 /** | |
56 * Sequence parameter set | |
57 */ | |
58 typedef struct SPS{ | |
59 | |
60 int profile_idc; | |
61 int level_idc; | |
62 int multiple_slice_groups; ///< more_than_one_slice_group_allowed_flag | |
63 int arbitrary_slice_order; ///< arbitrary_slice_order_allowed_flag | |
64 int redundant_slices; ///< redundant_slices_allowed_flag | |
65 int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4 | |
66 int poc_type; ///< pic_order_cnt_type | |
67 int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4 | |
68 int delta_pic_order_always_zero_flag; | |
69 int offset_for_non_ref_pic; | |
70 int offset_for_top_to_bottom_field; | |
71 int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle | |
72 int ref_frame_count; ///< num_ref_frames | |
73 int required_frame_num_update_behaviour_flag; | |
74 int mb_width; ///< frame_width_in_mbs_minus1 + 1 | |
75 int mb_height; ///< frame_height_in_mbs_minus1 + 1 | |
76 int frame_mbs_only_flag; | |
77 int mb_aff; ///<mb_adaptive_frame_field_flag | |
78 int direct_8x8_inference_flag; | |
79 int vui_parameters_present_flag; | |
80 int sar_width; | |
81 int sar_height; | |
82 short offset_for_ref_frame[256]; //FIXME dyn aloc? | |
83 }SPS; | |
84 | |
85 /** | |
86 * Picture parameter set | |
87 */ | |
88 typedef struct PPS{ | |
89 int sps_id; | |
90 int cabac; ///< entropy_coding_mode_flag | |
91 int pic_order_present; ///< pic_order_present_flag | |
92 int slice_group_count; ///< num_slice_groups_minus1 + 1 | |
93 int mb_slice_group_map_type; | |
94 int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1 | |
95 int weighted_pred; ///< weighted_pred_flag | |
96 int weighted_bipred_idc; | |
97 int init_qp; ///< pic_init_qp_minus26 + 26 | |
98 int init_qs; ///< pic_init_qs_minus26 + 26 | |
99 int chroma_qp_index_offset; | |
100 int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag | |
101 int constrained_intra_pred; ///< constrained_intra_pred_flag | |
102 int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag | |
103 int crop; ///< frame_cropping_flag | |
104 int crop_left; ///< frame_cropping_rect_left_offset | |
105 int crop_right; ///< frame_cropping_rect_right_offset | |
106 int crop_top; ///< frame_cropping_rect_top_offset | |
107 int crop_bottom; ///< frame_cropping_rect_bottom_offset | |
108 }PPS; | |
109 | |
110 /** | |
111 * Memory management control operation opcode. | |
112 */ | |
113 typedef enum MMCOOpcode{ | |
114 MMCO_END=0, | |
115 MMCO_SHORT2UNUSED, | |
116 MMCO_LONG2UNUSED, | |
117 MMCO_SHORT2LONG, | |
118 MMCO_SET_MAX_LONG, | |
119 MMCO_RESET, | |
120 MMCO_LONG, | |
121 } MMCOOpcode; | |
122 | |
123 /** | |
124 * Memory management control operation. | |
125 */ | |
126 typedef struct MMCO{ | |
127 MMCOOpcode opcode; | |
128 int short_frame_num; | |
129 int long_index; | |
130 } MMCO; | |
131 | |
132 /** | |
133 * H264Context | |
134 */ | |
135 typedef struct H264Context{ | |
136 MpegEncContext s; | |
137 int nal_ref_idc; | |
138 int nal_unit_type; | |
139 #define NAL_SLICE 1 | |
140 #define NAL_DPA 2 | |
141 #define NAL_DPB 3 | |
142 #define NAL_DPC 4 | |
143 #define NAL_IDR_SLICE 5 | |
144 #define NAL_SEI 6 | |
145 #define NAL_SPS 7 | |
146 #define NAL_PPS 8 | |
147 #define NAL_PICTURE_DELIMITER 9 | |
148 #define NAL_FILTER_DATA 10 | |
149 uint8_t *rbsp_buffer; | |
150 int rbsp_buffer_size; | |
151 | |
152 int chroma_qp; //QPc | |
153 | |
154 int prev_mb_skiped; //FIXME remove (IMHO not used) | |
155 | |
156 //prediction stuff | |
157 int chroma_pred_mode; | |
158 int intra16x16_pred_mode; | |
159 | |
160 int8_t intra4x4_pred_mode_cache[5*8]; | |
161 int8_t (*intra4x4_pred_mode)[8]; | |
162 void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp? | |
163 void (*pred8x8 [4+3])(uint8_t *src, int stride); | |
164 void (*pred16x16[4+3])(uint8_t *src, int stride); | |
165 unsigned int topleft_samples_available; | |
166 unsigned int top_samples_available; | |
167 unsigned int topright_samples_available; | |
168 unsigned int left_samples_available; | |
169 | |
170 /** | |
171 * non zero coeff count cache. | |
172 * is 64 if not available. | |
173 */ | |
174 uint8_t non_zero_count_cache[6*8]; | |
175 uint8_t (*non_zero_count)[16]; | |
176 | |
177 /** | |
178 * Motion vector cache. | |
179 */ | |
180 int16_t mv_cache[2][5*8][2]; | |
181 int8_t ref_cache[2][5*8]; | |
182 #define LIST_NOT_USED -1 //FIXME rename? | |
183 #define PART_NOT_AVAILABLE -2 | |
184 | |
185 /** | |
186 * is 1 if the specific list MV&references are set to 0,0,-2. | |
187 */ | |
188 int mv_cache_clean[2]; | |
189 | |
190 int block_offset[16+8]; | |
191 int chroma_subblock_offset[16]; //FIXME remove | |
192 | |
193 uint16_t *mb2b_xy; //FIXME are these 4 a good idea? | |
194 uint16_t *mb2b8_xy; | |
195 int b_stride; | |
196 int b8_stride; | |
197 | |
1234 | 198 int halfpel_flag; |
199 int thirdpel_flag; | |
200 | |
1168 | 201 SPS sps_buffer[MAX_SPS_COUNT]; |
202 SPS sps; ///< current sps | |
203 | |
204 PPS pps_buffer[MAX_PPS_COUNT]; | |
205 /** | |
206 * current pps | |
207 */ | |
208 PPS pps; //FIXME move tp Picture perhaps? (->no) do we need that? | |
209 | |
210 int slice_num; | |
211 uint8_t *slice_table_base; | |
212 uint8_t *slice_table; ///< slice_table_base + mb_stride + 1 | |
213 int slice_type; | |
214 int slice_type_fixed; | |
215 | |
216 //interlacing specific flags | |
217 int mb_field_decoding_flag; | |
218 | |
219 int sub_mb_type[4]; | |
220 | |
221 //POC stuff | |
222 int poc_lsb; | |
223 int poc_msb; | |
224 int delta_poc_bottom; | |
225 int delta_poc[2]; | |
226 int frame_num; | |
227 int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0 | |
228 int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0 | |
229 int frame_num_offset; ///< for POC type 2 | |
230 int prev_frame_num_offset; ///< for POC type 2 | |
231 int prev_frame_num; ///< frame_num of the last pic for POC type 1/2 | |
232 | |
233 /** | |
234 * frame_num for frames or 2*frame_num for field pics. | |
235 */ | |
236 int curr_pic_num; | |
237 | |
238 /** | |
239 * max_frame_num or 2*max_frame_num for field pics. | |
240 */ | |
241 int max_pic_num; | |
242 | |
243 //Weighted pred stuff | |
244 int luma_log2_weight_denom; | |
245 int chroma_log2_weight_denom; | |
246 int luma_weight[2][16]; | |
247 int luma_offset[2][16]; | |
248 int chroma_weight[2][16][2]; | |
249 int chroma_offset[2][16][2]; | |
250 | |
251 //deblock | |
252 int disable_deblocking_filter_idc; | |
253 int slice_alpha_c0_offset_div2; | |
254 int slice_beta_offset_div2; | |
255 | |
256 int redundant_pic_count; | |
257 | |
258 int direct_spatial_mv_pred; | |
259 | |
260 /** | |
261 * num_ref_idx_l0/1_active_minus1 + 1 | |
262 */ | |
263 int ref_count[2];// FIXME split for AFF | |
264 Picture *short_ref[16]; | |
265 Picture *long_ref[16]; | |
266 Picture default_ref_list[2][32]; | |
267 Picture ref_list[2][32]; //FIXME size? | |
268 Picture field_ref_list[2][32]; //FIXME size? | |
269 | |
270 /** | |
271 * memory management control operations buffer. | |
272 */ | |
273 MMCO mmco[MAX_MMCO_COUNT]; | |
274 int mmco_index; | |
275 | |
276 int long_ref_count; ///< number of actual long term references | |
277 int short_ref_count; ///< number of actual short term references | |
278 | |
279 //data partitioning | |
280 GetBitContext intra_gb; | |
281 GetBitContext inter_gb; | |
282 GetBitContext *intra_gb_ptr; | |
283 GetBitContext *inter_gb_ptr; | |
284 | |
285 DCTELEM mb[16*24] __align8; | |
286 }H264Context; | |
287 | |
288 static VLC coeff_token_vlc[4]; | |
289 static VLC chroma_dc_coeff_token_vlc; | |
290 | |
291 static VLC total_zeros_vlc[15]; | |
292 static VLC chroma_dc_total_zeros_vlc[3]; | |
293 | |
294 static VLC run_vlc[6]; | |
295 static VLC run7_vlc; | |
296 | |
1234 | 297 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp); |
298 static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc); | |
299 | |
1168 | 300 /** |
301 * fill a rectangle. | |
302 * @param h height of the recatangle, should be a constant | |
303 * @param w width of the recatangle, should be a constant | |
304 * @param size the size of val (1 or 4), should be a constant | |
305 */ | |
1187 | 306 static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined |
307 uint8_t *p= (uint8_t*)vp; | |
1168 | 308 assert(size==1 || size==4); |
309 | |
310 w *= size; | |
311 stride *= size; | |
312 | |
313 //FIXME check what gcc generates for 64 bit on x86 and possible write a 32 bit ver of it | |
314 if(w==2 && h==2){ | |
315 *(uint16_t*)(p + 0)= | |
316 *(uint16_t*)(p + stride)= size==4 ? val : val*0x0101; | |
317 }else if(w==2 && h==4){ | |
318 *(uint16_t*)(p + 0*stride)= | |
319 *(uint16_t*)(p + 1*stride)= | |
320 *(uint16_t*)(p + 2*stride)= | |
321 *(uint16_t*)(p + 3*stride)= size==4 ? val : val*0x0101; | |
1252 | 322 }else if(w==4 && h==1){ |
323 *(uint32_t*)(p + 0*stride)= size==4 ? val : val*0x01010101; | |
1168 | 324 }else if(w==4 && h==2){ |
325 *(uint32_t*)(p + 0*stride)= | |
326 *(uint32_t*)(p + 1*stride)= size==4 ? val : val*0x01010101; | |
327 }else if(w==4 && h==4){ | |
328 *(uint32_t*)(p + 0*stride)= | |
329 *(uint32_t*)(p + 1*stride)= | |
330 *(uint32_t*)(p + 2*stride)= | |
331 *(uint32_t*)(p + 3*stride)= size==4 ? val : val*0x01010101; | |
332 }else if(w==8 && h==1){ | |
333 *(uint32_t*)(p + 0)= | |
334 *(uint32_t*)(p + 4)= size==4 ? val : val*0x01010101; | |
335 }else if(w==8 && h==2){ | |
336 *(uint32_t*)(p + 0 + 0*stride)= | |
337 *(uint32_t*)(p + 4 + 0*stride)= | |
338 *(uint32_t*)(p + 0 + 1*stride)= | |
339 *(uint32_t*)(p + 4 + 1*stride)= size==4 ? val : val*0x01010101; | |
340 }else if(w==8 && h==4){ | |
341 *(uint64_t*)(p + 0*stride)= | |
342 *(uint64_t*)(p + 1*stride)= | |
343 *(uint64_t*)(p + 2*stride)= | |
344 *(uint64_t*)(p + 3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL; | |
345 }else if(w==16 && h==2){ | |
346 *(uint64_t*)(p + 0+0*stride)= | |
347 *(uint64_t*)(p + 8+0*stride)= | |
348 *(uint64_t*)(p + 0+1*stride)= | |
349 *(uint64_t*)(p + 8+1*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL; | |
350 }else if(w==16 && h==4){ | |
351 *(uint64_t*)(p + 0+0*stride)= | |
352 *(uint64_t*)(p + 8+0*stride)= | |
353 *(uint64_t*)(p + 0+1*stride)= | |
354 *(uint64_t*)(p + 8+1*stride)= | |
355 *(uint64_t*)(p + 0+2*stride)= | |
356 *(uint64_t*)(p + 8+2*stride)= | |
357 *(uint64_t*)(p + 0+3*stride)= | |
358 *(uint64_t*)(p + 8+3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL; | |
359 }else | |
360 assert(0); | |
361 } | |
362 | |
363 static inline void fill_caches(H264Context *h, int mb_type){ | |
364 MpegEncContext * const s = &h->s; | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
365 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; |
1168 | 366 int topleft_xy, top_xy, topright_xy, left_xy[2]; |
367 int topleft_type, top_type, topright_type, left_type[2]; | |
368 int left_block[4]; | |
369 int i; | |
370 | |
371 //wow what a mess, why didnt they simplify the interlacing&intra stuff, i cant imagine that these complex rules are worth it | |
372 | |
373 if(h->sps.mb_aff){ | |
374 //FIXME | |
375 }else{ | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
376 topleft_xy = mb_xy-1 - s->mb_stride; |
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
377 top_xy = mb_xy - s->mb_stride; |
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
378 topright_xy= mb_xy+1 - s->mb_stride; |
1168 | 379 left_xy[0] = mb_xy-1; |
380 left_xy[1] = mb_xy-1; | |
381 left_block[0]= 0; | |
382 left_block[1]= 1; | |
383 left_block[2]= 2; | |
384 left_block[3]= 3; | |
385 } | |
386 | |
387 topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0; | |
388 top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0; | |
389 topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0; | |
390 left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0; | |
391 left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0; | |
392 | |
393 if(IS_INTRA(mb_type)){ | |
394 h->topleft_samples_available= | |
395 h->top_samples_available= | |
396 h->left_samples_available= 0xFFFF; | |
397 h->topright_samples_available= 0xEEEA; | |
398 | |
399 if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){ | |
400 h->topleft_samples_available= 0xB3FF; | |
401 h->top_samples_available= 0x33FF; | |
402 h->topright_samples_available= 0x26EA; | |
403 } | |
404 for(i=0; i<2; i++){ | |
405 if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){ | |
406 h->topleft_samples_available&= 0xDF5F; | |
407 h->left_samples_available&= 0x5F5F; | |
408 } | |
409 } | |
410 | |
411 if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred)) | |
412 h->topleft_samples_available&= 0x7FFF; | |
413 | |
414 if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred)) | |
415 h->topright_samples_available&= 0xFBFF; | |
416 | |
417 if(IS_INTRA4x4(mb_type)){ | |
418 if(IS_INTRA4x4(top_type)){ | |
419 h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4]; | |
420 h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5]; | |
421 h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6]; | |
422 h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3]; | |
423 }else{ | |
424 int pred; | |
425 if(IS_INTRA16x16(top_type) || (IS_INTER(top_type) && !h->pps.constrained_intra_pred)) | |
426 pred= 2; | |
427 else{ | |
428 pred= -1; | |
429 } | |
430 h->intra4x4_pred_mode_cache[4+8*0]= | |
431 h->intra4x4_pred_mode_cache[5+8*0]= | |
432 h->intra4x4_pred_mode_cache[6+8*0]= | |
433 h->intra4x4_pred_mode_cache[7+8*0]= pred; | |
434 } | |
435 for(i=0; i<2; i++){ | |
436 if(IS_INTRA4x4(left_type[i])){ | |
437 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]]; | |
438 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]]; | |
439 }else{ | |
440 int pred; | |
441 if(IS_INTRA16x16(left_type[i]) || (IS_INTER(left_type[i]) && !h->pps.constrained_intra_pred)) | |
442 pred= 2; | |
443 else{ | |
444 pred= -1; | |
445 } | |
446 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= | |
447 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred; | |
448 } | |
449 } | |
450 } | |
451 } | |
452 | |
453 | |
454 /* | |
455 0 . T T. T T T T | |
456 1 L . .L . . . . | |
457 2 L . .L . . . . | |
458 3 . T TL . . . . | |
459 4 L . .L . . . . | |
460 5 L . .. . . . . | |
461 */ | |
462 //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec) | |
463 if(top_type){ | |
464 h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][0]; | |
465 h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][1]; | |
466 h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][2]; | |
467 h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3]; | |
468 | |
469 h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][7]; | |
470 h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8]; | |
471 | |
472 h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][10]; | |
473 h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11]; | |
474 }else{ | |
475 h->non_zero_count_cache[4+8*0]= | |
476 h->non_zero_count_cache[5+8*0]= | |
477 h->non_zero_count_cache[6+8*0]= | |
478 h->non_zero_count_cache[7+8*0]= | |
479 | |
480 h->non_zero_count_cache[1+8*0]= | |
481 h->non_zero_count_cache[2+8*0]= | |
482 | |
483 h->non_zero_count_cache[1+8*3]= | |
484 h->non_zero_count_cache[2+8*3]= 64; | |
485 } | |
486 | |
487 if(left_type[0]){ | |
488 h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][6]; | |
489 h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][5]; | |
490 h->non_zero_count_cache[0+8*1]= h->non_zero_count[left_xy[0]][9]; //FIXME left_block | |
491 h->non_zero_count_cache[0+8*4]= h->non_zero_count[left_xy[0]][12]; | |
492 }else{ | |
493 h->non_zero_count_cache[3+8*1]= | |
494 h->non_zero_count_cache[3+8*2]= | |
495 h->non_zero_count_cache[0+8*1]= | |
496 h->non_zero_count_cache[0+8*4]= 64; | |
497 } | |
498 | |
499 if(left_type[1]){ | |
500 h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[1]][4]; | |
501 h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[1]][3]; | |
502 h->non_zero_count_cache[0+8*2]= h->non_zero_count[left_xy[1]][8]; | |
503 h->non_zero_count_cache[0+8*5]= h->non_zero_count[left_xy[1]][11]; | |
504 }else{ | |
505 h->non_zero_count_cache[3+8*3]= | |
506 h->non_zero_count_cache[3+8*4]= | |
507 h->non_zero_count_cache[0+8*2]= | |
508 h->non_zero_count_cache[0+8*5]= 64; | |
509 } | |
510 | |
511 #if 1 | |
512 if(IS_INTER(mb_type)){ | |
513 int list; | |
514 for(list=0; list<2; list++){ | |
515 if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list)){ | |
516 /*if(!h->mv_cache_clean[list]){ | |
517 memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all? | |
518 memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t)); | |
519 h->mv_cache_clean[list]= 1; | |
520 }*/ | |
521 continue; //FIXME direct mode ... | |
522 } | |
523 h->mv_cache_clean[list]= 0; | |
524 | |
525 if(IS_INTER(topleft_type)){ | |
526 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride; | |
527 const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride; | |
528 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy]; | |
529 h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy]; | |
530 }else{ | |
531 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0; | |
532 h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE; | |
533 } | |
534 | |
535 if(IS_INTER(top_type)){ | |
536 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride; | |
537 const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride; | |
538 *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0]; | |
539 *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1]; | |
540 *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2]; | |
541 *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3]; | |
542 h->ref_cache[list][scan8[0] + 0 - 1*8]= | |
543 h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0]; | |
544 h->ref_cache[list][scan8[0] + 2 - 1*8]= | |
545 h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1]; | |
546 }else{ | |
547 *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]= | |
548 *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]= | |
549 *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]= | |
550 *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0; | |
551 *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101; | |
552 } | |
553 | |
554 if(IS_INTER(topright_type)){ | |
555 const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride; | |
556 const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride; | |
557 *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy]; | |
558 h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy]; | |
559 }else{ | |
560 *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0; | |
561 h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE; | |
562 } | |
563 | |
564 //FIXME unify cleanup or sth | |
565 if(IS_INTER(left_type[0])){ | |
566 const int b_xy= h->mb2b_xy[left_xy[0]] + 3; | |
567 const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1; | |
568 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[0]]; | |
569 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[1]]; | |
570 h->ref_cache[list][scan8[0] - 1 + 0*8]= | |
571 h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)]; | |
572 }else{ | |
573 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]= | |
574 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0; | |
575 h->ref_cache[list][scan8[0] - 1 + 0*8]= | |
576 h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE; | |
577 } | |
578 | |
579 if(IS_INTER(left_type[1])){ | |
580 const int b_xy= h->mb2b_xy[left_xy[1]] + 3; | |
581 const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1; | |
582 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[2]]; | |
583 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + h->b_stride*left_block[3]]; | |
584 h->ref_cache[list][scan8[0] - 1 + 2*8]= | |
585 h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)]; | |
586 }else{ | |
587 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]= | |
588 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0; | |
589 h->ref_cache[list][scan8[0] - 1 + 2*8]= | |
590 h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE; | |
591 } | |
592 | |
593 h->ref_cache[list][scan8[5 ]+1] = | |
594 h->ref_cache[list][scan8[7 ]+1] = | |
595 h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewher else) | |
596 h->ref_cache[list][scan8[4 ]] = | |
597 h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE; | |
598 *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]= | |
599 *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]= | |
600 *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewher else) | |
601 *(uint32_t*)h->mv_cache [list][scan8[4 ]]= | |
602 *(uint32_t*)h->mv_cache [list][scan8[12]]= 0; | |
603 } | |
604 //FIXME | |
605 | |
606 } | |
607 #endif | |
608 } | |
609 | |
610 static inline void write_back_intra_pred_mode(H264Context *h){ | |
611 MpegEncContext * const s = &h->s; | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
612 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; |
1168 | 613 |
614 h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1]; | |
615 h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2]; | |
616 h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3]; | |
617 h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4]; | |
618 h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4]; | |
619 h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4]; | |
620 h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4]; | |
621 } | |
622 | |
623 /** | |
624 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks. | |
625 */ | |
626 static inline int check_intra4x4_pred_mode(H264Context *h){ | |
627 MpegEncContext * const s = &h->s; | |
628 static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0}; | |
629 static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED}; | |
630 int i; | |
631 | |
632 if(!(h->top_samples_available&0x8000)){ | |
633 for(i=0; i<4; i++){ | |
634 int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ]; | |
635 if(status<0){ | |
636 fprintf(stderr, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y); | |
637 return -1; | |
638 } else if(status){ | |
639 h->intra4x4_pred_mode_cache[scan8[0] + i]= status; | |
640 } | |
641 } | |
642 } | |
643 | |
644 if(!(h->left_samples_available&0x8000)){ | |
645 for(i=0; i<4; i++){ | |
646 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ]; | |
647 if(status<0){ | |
648 fprintf(stderr, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y); | |
649 return -1; | |
650 } else if(status){ | |
651 h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status; | |
652 } | |
653 } | |
654 } | |
655 | |
656 return 0; | |
657 } //FIXME cleanup like next | |
658 | |
659 /** | |
660 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks. | |
661 */ | |
662 static inline int check_intra_pred_mode(H264Context *h, int mode){ | |
663 MpegEncContext * const s = &h->s; | |
664 static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1}; | |
665 static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8}; | |
666 | |
667 if(!(h->top_samples_available&0x8000)){ | |
668 mode= top[ mode ]; | |
669 if(mode<0){ | |
670 fprintf(stderr, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y); | |
671 return -1; | |
672 } | |
673 } | |
674 | |
675 if(!(h->left_samples_available&0x8000)){ | |
676 mode= left[ mode ]; | |
677 if(mode<0){ | |
678 fprintf(stderr, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y); | |
679 return -1; | |
680 } | |
681 } | |
682 | |
683 return mode; | |
684 } | |
685 | |
686 /** | |
687 * gets the predicted intra4x4 prediction mode. | |
688 */ | |
689 static inline int pred_intra_mode(H264Context *h, int n){ | |
690 const int index8= scan8[n]; | |
691 const int left= h->intra4x4_pred_mode_cache[index8 - 1]; | |
692 const int top = h->intra4x4_pred_mode_cache[index8 - 8]; | |
693 const int min= FFMIN(left, top); | |
694 | |
1170 | 695 tprintf("mode:%d %d min:%d\n", left ,top, min); |
1168 | 696 |
697 if(min<0) return DC_PRED; | |
698 else return min; | |
699 } | |
700 | |
701 static inline void write_back_non_zero_count(H264Context *h){ | |
702 MpegEncContext * const s = &h->s; | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
703 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; |
1168 | 704 |
705 h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[4+8*4]; | |
706 h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[5+8*4]; | |
707 h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[6+8*4]; | |
708 h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4]; | |
709 h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[7+8*3]; | |
710 h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[7+8*2]; | |
711 h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[7+8*1]; | |
712 | |
713 h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[1+8*2]; | |
714 h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2]; | |
715 h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[2+8*1]; | |
716 | |
717 h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[1+8*5]; | |
718 h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5]; | |
719 h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[2+8*4]; | |
720 } | |
721 | |
722 /** | |
723 * gets the predicted number of non zero coefficients. | |
724 * @param n block index | |
725 */ | |
726 static inline int pred_non_zero_count(H264Context *h, int n){ | |
727 const int index8= scan8[n]; | |
728 const int left= h->non_zero_count_cache[index8 - 1]; | |
729 const int top = h->non_zero_count_cache[index8 - 8]; | |
730 int i= left + top; | |
731 | |
732 if(i<64) i= (i+1)>>1; | |
733 | |
1170 | 734 tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31); |
1168 | 735 |
736 return i&31; | |
737 } | |
738 | |
1169 | 739 static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){ |
740 const int topright_ref= h->ref_cache[list][ i - 8 + part_width ]; | |
741 | |
742 if(topright_ref != PART_NOT_AVAILABLE){ | |
743 *C= h->mv_cache[list][ i - 8 + part_width ]; | |
744 return topright_ref; | |
745 }else{ | |
1170 | 746 tprintf("topright MV not available\n"); |
747 | |
1169 | 748 *C= h->mv_cache[list][ i - 8 - 1 ]; |
749 return h->ref_cache[list][ i - 8 - 1 ]; | |
750 } | |
751 } | |
752 | |
1168 | 753 /** |
754 * gets the predicted MV. | |
755 * @param n the block index | |
756 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4) | |
757 * @param mx the x component of the predicted motion vector | |
758 * @param my the y component of the predicted motion vector | |
759 */ | |
760 static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){ | |
761 const int index8= scan8[n]; | |
762 const int top_ref= h->ref_cache[list][ index8 - 8 ]; | |
763 const int left_ref= h->ref_cache[list][ index8 - 1 ]; | |
764 const int16_t * const A= h->mv_cache[list][ index8 - 1 ]; | |
765 const int16_t * const B= h->mv_cache[list][ index8 - 8 ]; | |
1169 | 766 const int16_t * C; |
767 int diagonal_ref, match_count; | |
768 | |
1168 | 769 assert(part_width==1 || part_width==2 || part_width==4); |
1169 | 770 |
1168 | 771 /* mv_cache |
772 B . . A T T T T | |
773 U . . L . . , . | |
774 U . . L . . . . | |
775 U . . L . . , . | |
776 . . . L . . . . | |
777 */ | |
1169 | 778 |
779 diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width); | |
780 match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref); | |
781 | |
782 if(match_count > 1){ //most common | |
783 *mx= mid_pred(A[0], B[0], C[0]); | |
784 *my= mid_pred(A[1], B[1], C[1]); | |
785 }else if(match_count==1){ | |
786 if(left_ref==ref){ | |
787 *mx= A[0]; | |
788 *my= A[1]; | |
789 }else if(top_ref==ref){ | |
790 *mx= B[0]; | |
791 *my= B[1]; | |
792 }else{ | |
793 *mx= C[0]; | |
794 *my= C[1]; | |
795 } | |
796 }else{ | |
797 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){ | |
798 *mx= A[0]; | |
799 *my= A[1]; | |
1168 | 800 }else{ |
801 *mx= mid_pred(A[0], B[0], C[0]); | |
802 *my= mid_pred(A[1], B[1], C[1]); | |
803 } | |
1169 | 804 } |
1168 | 805 |
1187 | 806 tprintf("pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list); |
1168 | 807 } |
808 | |
809 /** | |
810 * gets the directionally predicted 16x8 MV. | |
811 * @param n the block index | |
812 * @param mx the x component of the predicted motion vector | |
813 * @param my the y component of the predicted motion vector | |
814 */ | |
815 static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){ | |
816 if(n==0){ | |
817 const int top_ref= h->ref_cache[list][ scan8[0] - 8 ]; | |
818 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ]; | |
819 | |
1187 | 820 tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list); |
1168 | 821 |
822 if(top_ref == ref){ | |
823 *mx= B[0]; | |
824 *my= B[1]; | |
825 return; | |
826 } | |
827 }else{ | |
828 const int left_ref= h->ref_cache[list][ scan8[8] - 1 ]; | |
829 const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ]; | |
830 | |
1187 | 831 tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list); |
1168 | 832 |
833 if(left_ref == ref){ | |
834 *mx= A[0]; | |
835 *my= A[1]; | |
836 return; | |
837 } | |
838 } | |
839 | |
840 //RARE | |
841 pred_motion(h, n, 4, list, ref, mx, my); | |
842 } | |
843 | |
844 /** | |
845 * gets the directionally predicted 8x16 MV. | |
846 * @param n the block index | |
847 * @param mx the x component of the predicted motion vector | |
848 * @param my the y component of the predicted motion vector | |
849 */ | |
850 static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){ | |
851 if(n==0){ | |
852 const int left_ref= h->ref_cache[list][ scan8[0] - 1 ]; | |
853 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ]; | |
854 | |
1187 | 855 tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list); |
1168 | 856 |
857 if(left_ref == ref){ | |
858 *mx= A[0]; | |
859 *my= A[1]; | |
860 return; | |
861 } | |
862 }else{ | |
1169 | 863 const int16_t * C; |
864 int diagonal_ref; | |
865 | |
866 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2); | |
1168 | 867 |
1187 | 868 tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list); |
1168 | 869 |
1169 | 870 if(diagonal_ref == ref){ |
1168 | 871 *mx= C[0]; |
872 *my= C[1]; | |
873 return; | |
874 } | |
875 } | |
876 | |
877 //RARE | |
878 pred_motion(h, n, 2, list, ref, mx, my); | |
879 } | |
880 | |
881 static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){ | |
882 const int top_ref = h->ref_cache[0][ scan8[0] - 8 ]; | |
883 const int left_ref= h->ref_cache[0][ scan8[0] - 1 ]; | |
884 | |
1187 | 885 tprintf("pred_pskip: (%d) (%d) at %2d %2d", top_ref, left_ref, h->s.mb_x, h->s.mb_y); |
1168 | 886 |
887 if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE | |
888 || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0) | |
889 || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){ | |
890 | |
891 *mx = *my = 0; | |
892 return; | |
893 } | |
894 | |
895 pred_motion(h, 0, 4, 0, 0, mx, my); | |
896 | |
897 return; | |
898 } | |
899 | |
900 static inline void write_back_motion(H264Context *h, int mb_type){ | |
901 MpegEncContext * const s = &h->s; | |
902 const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride; | |
903 const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride; | |
904 int list; | |
905 | |
906 for(list=0; list<2; list++){ | |
907 int y; | |
908 if((!IS_8X8(mb_type)) && !USES_LIST(mb_type, list)){ | |
909 if(1){ //FIXME skip or never read if mb_type doesnt use it | |
910 for(y=0; y<4; y++){ | |
911 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= | |
912 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= 0; | |
913 } | |
914 for(y=0; y<2; y++){ | |
915 *(uint16_t*)s->current_picture.motion_val[list][b8_xy + y*h->b8_stride]= (LIST_NOT_USED&0xFF)*0x0101; | |
916 } | |
917 } | |
918 continue; //FIXME direct mode ... | |
919 } | |
920 | |
921 for(y=0; y<4; y++){ | |
922 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+0 + 8*y]; | |
923 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mv_cache[list][scan8[0]+2 + 8*y]; | |
924 } | |
925 for(y=0; y<2; y++){ | |
926 s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+0 + 16*y]; | |
927 s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+2 + 16*y]; | |
928 } | |
929 } | |
930 } | |
931 | |
932 /** | |
933 * Decodes a network abstraction layer unit. | |
934 * @param consumed is the number of bytes used as input | |
935 * @param length is the length of the array | |
936 * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp ttailing? | |
937 * @returns decoded bytes, might be src+1 if no escapes | |
938 */ | |
939 static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){ | |
940 int i, si, di; | |
941 uint8_t *dst; | |
942 | |
943 // src[0]&0x80; //forbidden bit | |
944 h->nal_ref_idc= src[0]>>5; | |
945 h->nal_unit_type= src[0]&0x1F; | |
946 | |
947 src++; length--; | |
948 #if 0 | |
949 for(i=0; i<length; i++) | |
950 printf("%2X ", src[i]); | |
951 #endif | |
952 for(i=0; i+1<length; i+=2){ | |
953 if(src[i]) continue; | |
954 if(i>0 && src[i-1]==0) i--; | |
955 if(i+2<length && src[i+1]==0 && src[i+2]<=3){ | |
956 if(src[i+2]!=3){ | |
957 /* startcode, so we must be past the end */ | |
958 length=i; | |
959 } | |
960 break; | |
961 } | |
962 } | |
963 | |
964 if(i>=length-1){ //no escaped 0 | |
965 *dst_length= length; | |
966 *consumed= length+1; //+1 for the header | |
967 return src; | |
968 } | |
969 | |
970 h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length); | |
971 dst= h->rbsp_buffer; | |
972 | |
973 //printf("deoding esc\n"); | |
974 si=di=0; | |
975 while(si<length){ | |
976 //remove escapes (very rare 1:2^22) | |
977 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){ | |
978 if(src[si+2]==3){ //escape | |
979 dst[di++]= 0; | |
980 dst[di++]= 0; | |
981 si+=3; | |
982 }else //next start code | |
983 break; | |
984 } | |
985 | |
986 dst[di++]= src[si++]; | |
987 } | |
988 | |
989 *dst_length= di; | |
990 *consumed= si + 1;//+1 for the header | |
991 //FIXME store exact number of bits in the getbitcontext (its needed for decoding) | |
992 return dst; | |
993 } | |
994 | |
995 /** | |
996 * @param src the data which should be escaped | |
997 * @param dst the target buffer, dst+1 == src is allowed as a special case | |
998 * @param length the length of the src data | |
999 * @param dst_length the length of the dst array | |
1000 * @returns length of escaped data in bytes or -1 if an error occured | |
1001 */ | |
1002 static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){ | |
1003 int i, escape_count, si, di; | |
1004 uint8_t *temp; | |
1005 | |
1006 assert(length>=0); | |
1007 assert(dst_length>0); | |
1008 | |
1009 dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type; | |
1010 | |
1011 if(length==0) return 1; | |
1012 | |
1013 escape_count= 0; | |
1014 for(i=0; i<length; i+=2){ | |
1015 if(src[i]) continue; | |
1016 if(i>0 && src[i-1]==0) | |
1017 i--; | |
1018 if(i+2<length && src[i+1]==0 && src[i+2]<=3){ | |
1019 escape_count++; | |
1020 i+=2; | |
1021 } | |
1022 } | |
1023 | |
1024 if(escape_count==0){ | |
1025 if(dst+1 != src) | |
1026 memcpy(dst+1, src, length); | |
1027 return length + 1; | |
1028 } | |
1029 | |
1030 if(length + escape_count + 1> dst_length) | |
1031 return -1; | |
1032 | |
1033 //this should be damn rare (hopefully) | |
1034 | |
1035 h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count); | |
1036 temp= h->rbsp_buffer; | |
1037 //printf("encoding esc\n"); | |
1038 | |
1039 si= 0; | |
1040 di= 0; | |
1041 while(si < length){ | |
1042 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){ | |
1043 temp[di++]= 0; si++; | |
1044 temp[di++]= 0; si++; | |
1045 temp[di++]= 3; | |
1046 temp[di++]= src[si++]; | |
1047 } | |
1048 else | |
1049 temp[di++]= src[si++]; | |
1050 } | |
1051 memcpy(dst+1, temp, length+escape_count); | |
1052 | |
1053 assert(di == length+escape_count); | |
1054 | |
1055 return di + 1; | |
1056 } | |
1057 | |
1058 /** | |
1059 * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4 | |
1060 */ | |
1061 static void encode_rbsp_trailing(PutBitContext *pb){ | |
1062 int length; | |
1063 put_bits(pb, 1, 1); | |
1064 length= (-get_bit_count(pb))&7; | |
1065 if(length) put_bits(pb, length, 0); | |
1066 } | |
1067 | |
1068 /** | |
1069 * identifies the exact end of the bitstream | |
1070 * @return the length of the trailing, or 0 if damaged | |
1071 */ | |
1072 static int decode_rbsp_trailing(uint8_t *src){ | |
1073 int v= *src; | |
1074 int r; | |
1075 | |
1170 | 1076 tprintf("rbsp trailing %X\n", v); |
1168 | 1077 |
1078 for(r=1; r<9; r++){ | |
1079 if(v&1) return r; | |
1080 v>>=1; | |
1081 } | |
1082 return 0; | |
1083 } | |
1084 | |
1085 /** | |
1086 * idct tranforms the 16 dc values and dequantize them. | |
1087 * @param qp quantization parameter | |
1088 */ | |
1089 static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp){ | |
1090 const int qmul= dequant_coeff[qp][0]; | |
1091 #define stride 16 | |
1092 int i; | |
1093 int temp[16]; //FIXME check if this is a good idea | |
1094 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride}; | |
1095 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride}; | |
1096 | |
1097 //memset(block, 64, 2*256); | |
1098 //return; | |
1099 for(i=0; i<4; i++){ | |
1100 const int offset= y_offset[i]; | |
1101 const int z0= block[offset+stride*0] + block[offset+stride*4]; | |
1102 const int z1= block[offset+stride*0] - block[offset+stride*4]; | |
1103 const int z2= block[offset+stride*1] - block[offset+stride*5]; | |
1104 const int z3= block[offset+stride*1] + block[offset+stride*5]; | |
1105 | |
1106 temp[4*i+0]= z0+z3; | |
1107 temp[4*i+1]= z1+z2; | |
1108 temp[4*i+2]= z1-z2; | |
1109 temp[4*i+3]= z0-z3; | |
1110 } | |
1111 | |
1112 for(i=0; i<4; i++){ | |
1113 const int offset= x_offset[i]; | |
1114 const int z0= temp[4*0+i] + temp[4*2+i]; | |
1115 const int z1= temp[4*0+i] - temp[4*2+i]; | |
1116 const int z2= temp[4*1+i] - temp[4*3+i]; | |
1117 const int z3= temp[4*1+i] + temp[4*3+i]; | |
1118 | |
1119 block[stride*0 +offset]= ((z0 + z3)*qmul + 2)>>2; //FIXME think about merging this into decode_resdual | |
1120 block[stride*2 +offset]= ((z1 + z2)*qmul + 2)>>2; | |
1121 block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2; | |
1122 block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2; | |
1123 } | |
1124 } | |
1125 | |
1126 /** | |
1127 * dct tranforms the 16 dc values. | |
1128 * @param qp quantization parameter ??? FIXME | |
1129 */ | |
1130 static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){ | |
1131 // const int qmul= dequant_coeff[qp][0]; | |
1132 int i; | |
1133 int temp[16]; //FIXME check if this is a good idea | |
1134 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride}; | |
1135 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride}; | |
1136 | |
1137 for(i=0; i<4; i++){ | |
1138 const int offset= y_offset[i]; | |
1139 const int z0= block[offset+stride*0] + block[offset+stride*4]; | |
1140 const int z1= block[offset+stride*0] - block[offset+stride*4]; | |
1141 const int z2= block[offset+stride*1] - block[offset+stride*5]; | |
1142 const int z3= block[offset+stride*1] + block[offset+stride*5]; | |
1143 | |
1144 temp[4*i+0]= z0+z3; | |
1145 temp[4*i+1]= z1+z2; | |
1146 temp[4*i+2]= z1-z2; | |
1147 temp[4*i+3]= z0-z3; | |
1148 } | |
1149 | |
1150 for(i=0; i<4; i++){ | |
1151 const int offset= x_offset[i]; | |
1152 const int z0= temp[4*0+i] + temp[4*2+i]; | |
1153 const int z1= temp[4*0+i] - temp[4*2+i]; | |
1154 const int z2= temp[4*1+i] - temp[4*3+i]; | |
1155 const int z3= temp[4*1+i] + temp[4*3+i]; | |
1156 | |
1157 block[stride*0 +offset]= (z0 + z3)>>1; | |
1158 block[stride*2 +offset]= (z1 + z2)>>1; | |
1159 block[stride*8 +offset]= (z1 - z2)>>1; | |
1160 block[stride*10+offset]= (z0 - z3)>>1; | |
1161 } | |
1162 } | |
1163 #undef xStride | |
1164 #undef stride | |
1165 | |
1166 static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){ | |
1167 const int qmul= dequant_coeff[qp][0]; | |
1168 const int stride= 16*2; | |
1169 const int xStride= 16; | |
1170 int a,b,c,d,e; | |
1171 | |
1172 a= block[stride*0 + xStride*0]; | |
1173 b= block[stride*0 + xStride*1]; | |
1174 c= block[stride*1 + xStride*0]; | |
1175 d= block[stride*1 + xStride*1]; | |
1176 | |
1177 e= a-b; | |
1178 a= a+b; | |
1179 b= c-d; | |
1180 c= c+d; | |
1181 | |
1182 block[stride*0 + xStride*0]= ((a+c)*qmul + 0)>>1; | |
1183 block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1; | |
1184 block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1; | |
1185 block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1; | |
1186 } | |
1187 | |
1188 static void chroma_dc_dct_c(DCTELEM *block){ | |
1189 const int stride= 16*2; | |
1190 const int xStride= 16; | |
1191 int a,b,c,d,e; | |
1192 | |
1193 a= block[stride*0 + xStride*0]; | |
1194 b= block[stride*0 + xStride*1]; | |
1195 c= block[stride*1 + xStride*0]; | |
1196 d= block[stride*1 + xStride*1]; | |
1197 | |
1198 e= a-b; | |
1199 a= a+b; | |
1200 b= c-d; | |
1201 c= c+d; | |
1202 | |
1203 block[stride*0 + xStride*0]= (a+c); | |
1204 block[stride*0 + xStride*1]= (e+b); | |
1205 block[stride*1 + xStride*0]= (a-c); | |
1206 block[stride*1 + xStride*1]= (e-b); | |
1207 } | |
1208 | |
1209 /** | |
1210 * gets the chroma qp. | |
1211 */ | |
1212 static inline int get_chroma_qp(H264Context *h, int qscale){ | |
1213 | |
1214 return chroma_qp[clip(qscale + h->pps.chroma_qp_index_offset, 0, 51)]; | |
1215 } | |
1216 | |
1217 | |
1218 /** | |
1219 * | |
1220 */ | |
1221 static void h264_add_idct_c(uint8_t *dst, DCTELEM *block, int stride){ | |
1222 int i; | |
1223 uint8_t *cm = cropTbl + MAX_NEG_CROP; | |
1224 | |
1225 block[0] += 32; | |
1226 #if 1 | |
1227 for(i=0; i<4; i++){ | |
1228 const int z0= block[i + 4*0] + block[i + 4*2]; | |
1229 const int z1= block[i + 4*0] - block[i + 4*2]; | |
1230 const int z2= (block[i + 4*1]>>1) - block[i + 4*3]; | |
1231 const int z3= block[i + 4*1] + (block[i + 4*3]>>1); | |
1232 | |
1233 block[i + 4*0]= z0 + z3; | |
1234 block[i + 4*1]= z1 + z2; | |
1235 block[i + 4*2]= z1 - z2; | |
1236 block[i + 4*3]= z0 - z3; | |
1237 } | |
1238 | |
1239 for(i=0; i<4; i++){ | |
1240 const int z0= block[0 + 4*i] + block[2 + 4*i]; | |
1241 const int z1= block[0 + 4*i] - block[2 + 4*i]; | |
1242 const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i]; | |
1243 const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1); | |
1244 | |
1245 dst[0 + i*stride]= cm[ dst[0 + i*stride] + ((z0 + z3) >> 6) ]; | |
1246 dst[1 + i*stride]= cm[ dst[1 + i*stride] + ((z1 + z2) >> 6) ]; | |
1247 dst[2 + i*stride]= cm[ dst[2 + i*stride] + ((z1 - z2) >> 6) ]; | |
1248 dst[3 + i*stride]= cm[ dst[3 + i*stride] + ((z0 - z3) >> 6) ]; | |
1249 } | |
1250 #else | |
1251 for(i=0; i<4; i++){ | |
1252 const int z0= block[0 + 4*i] + block[2 + 4*i]; | |
1253 const int z1= block[0 + 4*i] - block[2 + 4*i]; | |
1254 const int z2= (block[1 + 4*i]>>1) - block[3 + 4*i]; | |
1255 const int z3= block[1 + 4*i] + (block[3 + 4*i]>>1); | |
1256 | |
1257 block[0 + 4*i]= z0 + z3; | |
1258 block[1 + 4*i]= z1 + z2; | |
1259 block[2 + 4*i]= z1 - z2; | |
1260 block[3 + 4*i]= z0 - z3; | |
1261 } | |
1262 | |
1263 for(i=0; i<4; i++){ | |
1264 const int z0= block[i + 4*0] + block[i + 4*2]; | |
1265 const int z1= block[i + 4*0] - block[i + 4*2]; | |
1266 const int z2= (block[i + 4*1]>>1) - block[i + 4*3]; | |
1267 const int z3= block[i + 4*1] + (block[i + 4*3]>>1); | |
1268 | |
1269 dst[i + 0*stride]= cm[ dst[i + 0*stride] + ((z0 + z3) >> 6) ]; | |
1270 dst[i + 1*stride]= cm[ dst[i + 1*stride] + ((z1 + z2) >> 6) ]; | |
1271 dst[i + 2*stride]= cm[ dst[i + 2*stride] + ((z1 - z2) >> 6) ]; | |
1272 dst[i + 3*stride]= cm[ dst[i + 3*stride] + ((z0 - z3) >> 6) ]; | |
1273 } | |
1274 #endif | |
1275 } | |
1276 | |
1277 static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){ | |
1278 int i; | |
1279 //FIXME try int temp instead of block | |
1280 | |
1281 for(i=0; i<4; i++){ | |
1282 const int d0= src1[0 + i*stride] - src2[0 + i*stride]; | |
1283 const int d1= src1[1 + i*stride] - src2[1 + i*stride]; | |
1284 const int d2= src1[2 + i*stride] - src2[2 + i*stride]; | |
1285 const int d3= src1[3 + i*stride] - src2[3 + i*stride]; | |
1286 const int z0= d0 + d3; | |
1287 const int z3= d0 - d3; | |
1288 const int z1= d1 + d2; | |
1289 const int z2= d1 - d2; | |
1290 | |
1291 block[0 + 4*i]= z0 + z1; | |
1292 block[1 + 4*i]= 2*z3 + z2; | |
1293 block[2 + 4*i]= z0 - z1; | |
1294 block[3 + 4*i]= z3 - 2*z2; | |
1295 } | |
1296 | |
1297 for(i=0; i<4; i++){ | |
1298 const int z0= block[0*4 + i] + block[3*4 + i]; | |
1299 const int z3= block[0*4 + i] - block[3*4 + i]; | |
1300 const int z1= block[1*4 + i] + block[2*4 + i]; | |
1301 const int z2= block[1*4 + i] - block[2*4 + i]; | |
1302 | |
1303 block[0*4 + i]= z0 + z1; | |
1304 block[1*4 + i]= 2*z3 + z2; | |
1305 block[2*4 + i]= z0 - z1; | |
1306 block[3*4 + i]= z3 - 2*z2; | |
1307 } | |
1308 } | |
1309 | |
1310 //FIXME need to check that this doesnt overflow signed 32 bit for low qp, iam not sure, its very close | |
1311 //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away) | |
1312 static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){ | |
1313 int i; | |
1314 const int * const quant_table= quant_coeff[qscale]; | |
1315 const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6; | |
1316 const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1; | |
1317 const unsigned int threshold2= (threshold1<<1); | |
1318 int last_non_zero; | |
1319 | |
1320 if(seperate_dc){ | |
1321 if(qscale<=18){ | |
1322 //avoid overflows | |
1323 const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6; | |
1324 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1; | |
1325 const unsigned int dc_threshold2= (dc_threshold1<<1); | |
1326 | |
1327 int level= block[0]*quant_coeff[qscale+18][0]; | |
1328 if(((unsigned)(level+dc_threshold1))>dc_threshold2){ | |
1329 if(level>0){ | |
1330 level= (dc_bias + level)>>(QUANT_SHIFT-2); | |
1331 block[0]= level; | |
1332 }else{ | |
1333 level= (dc_bias - level)>>(QUANT_SHIFT-2); | |
1334 block[0]= -level; | |
1335 } | |
1336 // last_non_zero = i; | |
1337 }else{ | |
1338 block[0]=0; | |
1339 } | |
1340 }else{ | |
1341 const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6; | |
1342 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1; | |
1343 const unsigned int dc_threshold2= (dc_threshold1<<1); | |
1344 | |
1345 int level= block[0]*quant_table[0]; | |
1346 if(((unsigned)(level+dc_threshold1))>dc_threshold2){ | |
1347 if(level>0){ | |
1348 level= (dc_bias + level)>>(QUANT_SHIFT+1); | |
1349 block[0]= level; | |
1350 }else{ | |
1351 level= (dc_bias - level)>>(QUANT_SHIFT+1); | |
1352 block[0]= -level; | |
1353 } | |
1354 // last_non_zero = i; | |
1355 }else{ | |
1356 block[0]=0; | |
1357 } | |
1358 } | |
1359 last_non_zero= 0; | |
1360 i=1; | |
1361 }else{ | |
1362 last_non_zero= -1; | |
1363 i=0; | |
1364 } | |
1365 | |
1366 for(; i<16; i++){ | |
1367 const int j= scantable[i]; | |
1368 int level= block[j]*quant_table[j]; | |
1369 | |
1370 // if( bias+level >= (1<<(QMAT_SHIFT - 3)) | |
1371 // || bias-level >= (1<<(QMAT_SHIFT - 3))){ | |
1372 if(((unsigned)(level+threshold1))>threshold2){ | |
1373 if(level>0){ | |
1374 level= (bias + level)>>QUANT_SHIFT; | |
1375 block[j]= level; | |
1376 }else{ | |
1377 level= (bias - level)>>QUANT_SHIFT; | |
1378 block[j]= -level; | |
1379 } | |
1380 last_non_zero = i; | |
1381 }else{ | |
1382 block[j]=0; | |
1383 } | |
1384 } | |
1385 | |
1386 return last_non_zero; | |
1387 } | |
1388 | |
1389 static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){ | |
1390 const uint32_t a= ((uint32_t*)(src-stride))[0]; | |
1391 ((uint32_t*)(src+0*stride))[0]= a; | |
1392 ((uint32_t*)(src+1*stride))[0]= a; | |
1393 ((uint32_t*)(src+2*stride))[0]= a; | |
1394 ((uint32_t*)(src+3*stride))[0]= a; | |
1395 } | |
1396 | |
1397 static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){ | |
1398 ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101; | |
1399 ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101; | |
1400 ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101; | |
1401 ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101; | |
1402 } | |
1403 | |
1404 static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){ | |
1405 const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] | |
1406 + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3; | |
1407 | |
1408 ((uint32_t*)(src+0*stride))[0]= | |
1409 ((uint32_t*)(src+1*stride))[0]= | |
1410 ((uint32_t*)(src+2*stride))[0]= | |
1411 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101; | |
1412 } | |
1413 | |
1414 static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){ | |
1415 const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2; | |
1416 | |
1417 ((uint32_t*)(src+0*stride))[0]= | |
1418 ((uint32_t*)(src+1*stride))[0]= | |
1419 ((uint32_t*)(src+2*stride))[0]= | |
1420 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101; | |
1421 } | |
1422 | |
1423 static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){ | |
1424 const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2; | |
1425 | |
1426 ((uint32_t*)(src+0*stride))[0]= | |
1427 ((uint32_t*)(src+1*stride))[0]= | |
1428 ((uint32_t*)(src+2*stride))[0]= | |
1429 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101; | |
1430 } | |
1431 | |
1432 static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){ | |
1433 ((uint32_t*)(src+0*stride))[0]= | |
1434 ((uint32_t*)(src+1*stride))[0]= | |
1435 ((uint32_t*)(src+2*stride))[0]= | |
1436 ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U; | |
1437 } | |
1438 | |
1439 | |
1440 #define LOAD_TOP_RIGHT_EDGE\ | |
1441 const int t4= topright[0];\ | |
1442 const int t5= topright[1];\ | |
1443 const int t6= topright[2];\ | |
1444 const int t7= topright[3];\ | |
1445 | |
1446 #define LOAD_LEFT_EDGE\ | |
1447 const int l0= src[-1+0*stride];\ | |
1448 const int l1= src[-1+1*stride];\ | |
1449 const int l2= src[-1+2*stride];\ | |
1450 const int l3= src[-1+3*stride];\ | |
1451 | |
1452 #define LOAD_TOP_EDGE\ | |
1453 const int t0= src[ 0-1*stride];\ | |
1454 const int t1= src[ 1-1*stride];\ | |
1455 const int t2= src[ 2-1*stride];\ | |
1456 const int t3= src[ 3-1*stride];\ | |
1457 | |
1458 static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){ | |
1459 const int lt= src[-1-1*stride]; | |
1460 LOAD_TOP_EDGE | |
1461 LOAD_LEFT_EDGE | |
1462 | |
1463 src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2; | |
1464 src[0+2*stride]= | |
1465 src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2; | |
1466 src[0+1*stride]= | |
1467 src[1+2*stride]= | |
1468 src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2; | |
1469 src[0+0*stride]= | |
1470 src[1+1*stride]= | |
1471 src[2+2*stride]= | |
1472 src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2; | |
1473 src[1+0*stride]= | |
1474 src[2+1*stride]= | |
1475 src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2; | |
1476 src[2+0*stride]= | |
1477 src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2; | |
1478 src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2; | |
1479 }; | |
1480 | |
1481 static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){ | |
1482 LOAD_TOP_EDGE | |
1483 LOAD_TOP_RIGHT_EDGE | |
1484 // LOAD_LEFT_EDGE | |
1485 | |
1486 src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2; | |
1487 src[1+0*stride]= | |
1488 src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2; | |
1489 src[2+0*stride]= | |
1490 src[1+1*stride]= | |
1491 src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2; | |
1492 src[3+0*stride]= | |
1493 src[2+1*stride]= | |
1494 src[1+2*stride]= | |
1495 src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2; | |
1496 src[3+1*stride]= | |
1497 src[2+2*stride]= | |
1498 src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2; | |
1499 src[3+2*stride]= | |
1500 src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2; | |
1501 src[3+3*stride]=(t6 + 3*t7 + 2)>>2; | |
1502 }; | |
1503 | |
1504 static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){ | |
1505 const int lt= src[-1-1*stride]; | |
1506 LOAD_TOP_EDGE | |
1507 LOAD_LEFT_EDGE | |
1508 const __attribute__((unused)) int unu= l3; | |
1509 | |
1510 src[0+0*stride]= | |
1511 src[1+2*stride]=(lt + t0 + 1)>>1; | |
1512 src[1+0*stride]= | |
1513 src[2+2*stride]=(t0 + t1 + 1)>>1; | |
1514 src[2+0*stride]= | |
1515 src[3+2*stride]=(t1 + t2 + 1)>>1; | |
1516 src[3+0*stride]=(t2 + t3 + 1)>>1; | |
1517 src[0+1*stride]= | |
1518 src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2; | |
1519 src[1+1*stride]= | |
1520 src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2; | |
1521 src[2+1*stride]= | |
1522 src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2; | |
1523 src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2; | |
1524 src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2; | |
1525 src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2; | |
1526 }; | |
1527 | |
1528 static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){ | |
1529 LOAD_TOP_EDGE | |
1530 LOAD_TOP_RIGHT_EDGE | |
1531 const __attribute__((unused)) int unu= t7; | |
1532 | |
1533 src[0+0*stride]=(t0 + t1 + 1)>>1; | |
1534 src[1+0*stride]= | |
1535 src[0+2*stride]=(t1 + t2 + 1)>>1; | |
1536 src[2+0*stride]= | |
1537 src[1+2*stride]=(t2 + t3 + 1)>>1; | |
1538 src[3+0*stride]= | |
1539 src[2+2*stride]=(t3 + t4+ 1)>>1; | |
1540 src[3+2*stride]=(t4 + t5+ 1)>>1; | |
1541 src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2; | |
1542 src[1+1*stride]= | |
1543 src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2; | |
1544 src[2+1*stride]= | |
1545 src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2; | |
1546 src[3+1*stride]= | |
1547 src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2; | |
1548 src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2; | |
1549 }; | |
1550 | |
1551 static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){ | |
1552 LOAD_LEFT_EDGE | |
1553 | |
1554 src[0+0*stride]=(l0 + l1 + 1)>>1; | |
1555 src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2; | |
1556 src[2+0*stride]= | |
1557 src[0+1*stride]=(l1 + l2 + 1)>>1; | |
1558 src[3+0*stride]= | |
1559 src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2; | |
1560 src[2+1*stride]= | |
1561 src[0+2*stride]=(l2 + l3 + 1)>>1; | |
1562 src[3+1*stride]= | |
1563 src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2; | |
1564 src[3+2*stride]= | |
1565 src[1+3*stride]= | |
1566 src[0+3*stride]= | |
1567 src[2+2*stride]= | |
1568 src[2+3*stride]= | |
1569 src[3+3*stride]=l3; | |
1570 }; | |
1571 | |
1572 static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){ | |
1573 const int lt= src[-1-1*stride]; | |
1574 LOAD_TOP_EDGE | |
1575 LOAD_LEFT_EDGE | |
1576 const __attribute__((unused)) int unu= t3; | |
1577 | |
1578 src[0+0*stride]= | |
1579 src[2+1*stride]=(lt + l0 + 1)>>1; | |
1580 src[1+0*stride]= | |
1581 src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2; | |
1582 src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2; | |
1583 src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2; | |
1584 src[0+1*stride]= | |
1585 src[2+2*stride]=(l0 + l1 + 1)>>1; | |
1586 src[1+1*stride]= | |
1587 src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2; | |
1588 src[0+2*stride]= | |
1589 src[2+3*stride]=(l1 + l2+ 1)>>1; | |
1590 src[1+2*stride]= | |
1591 src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2; | |
1592 src[0+3*stride]=(l2 + l3 + 1)>>1; | |
1593 src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2; | |
1594 }; | |
1595 | |
1596 static void pred16x16_vertical_c(uint8_t *src, int stride){ | |
1597 int i; | |
1598 const uint32_t a= ((uint32_t*)(src-stride))[0]; | |
1599 const uint32_t b= ((uint32_t*)(src-stride))[1]; | |
1600 const uint32_t c= ((uint32_t*)(src-stride))[2]; | |
1601 const uint32_t d= ((uint32_t*)(src-stride))[3]; | |
1602 | |
1603 for(i=0; i<16; i++){ | |
1604 ((uint32_t*)(src+i*stride))[0]= a; | |
1605 ((uint32_t*)(src+i*stride))[1]= b; | |
1606 ((uint32_t*)(src+i*stride))[2]= c; | |
1607 ((uint32_t*)(src+i*stride))[3]= d; | |
1608 } | |
1609 } | |
1610 | |
1611 static void pred16x16_horizontal_c(uint8_t *src, int stride){ | |
1612 int i; | |
1613 | |
1614 for(i=0; i<16; i++){ | |
1615 ((uint32_t*)(src+i*stride))[0]= | |
1616 ((uint32_t*)(src+i*stride))[1]= | |
1617 ((uint32_t*)(src+i*stride))[2]= | |
1618 ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101; | |
1619 } | |
1620 } | |
1621 | |
1622 static void pred16x16_dc_c(uint8_t *src, int stride){ | |
1623 int i, dc=0; | |
1624 | |
1625 for(i=0;i<16; i++){ | |
1626 dc+= src[-1+i*stride]; | |
1627 } | |
1628 | |
1629 for(i=0;i<16; i++){ | |
1630 dc+= src[i-stride]; | |
1631 } | |
1632 | |
1633 dc= 0x01010101*((dc + 16)>>5); | |
1634 | |
1635 for(i=0; i<16; i++){ | |
1636 ((uint32_t*)(src+i*stride))[0]= | |
1637 ((uint32_t*)(src+i*stride))[1]= | |
1638 ((uint32_t*)(src+i*stride))[2]= | |
1639 ((uint32_t*)(src+i*stride))[3]= dc; | |
1640 } | |
1641 } | |
1642 | |
1643 static void pred16x16_left_dc_c(uint8_t *src, int stride){ | |
1644 int i, dc=0; | |
1645 | |
1646 for(i=0;i<16; i++){ | |
1647 dc+= src[-1+i*stride]; | |
1648 } | |
1649 | |
1650 dc= 0x01010101*((dc + 8)>>4); | |
1651 | |
1652 for(i=0; i<16; i++){ | |
1653 ((uint32_t*)(src+i*stride))[0]= | |
1654 ((uint32_t*)(src+i*stride))[1]= | |
1655 ((uint32_t*)(src+i*stride))[2]= | |
1656 ((uint32_t*)(src+i*stride))[3]= dc; | |
1657 } | |
1658 } | |
1659 | |
1660 static void pred16x16_top_dc_c(uint8_t *src, int stride){ | |
1661 int i, dc=0; | |
1662 | |
1663 for(i=0;i<16; i++){ | |
1664 dc+= src[i-stride]; | |
1665 } | |
1666 dc= 0x01010101*((dc + 8)>>4); | |
1667 | |
1668 for(i=0; i<16; i++){ | |
1669 ((uint32_t*)(src+i*stride))[0]= | |
1670 ((uint32_t*)(src+i*stride))[1]= | |
1671 ((uint32_t*)(src+i*stride))[2]= | |
1672 ((uint32_t*)(src+i*stride))[3]= dc; | |
1673 } | |
1674 } | |
1675 | |
1676 static void pred16x16_128_dc_c(uint8_t *src, int stride){ | |
1677 int i; | |
1678 | |
1679 for(i=0; i<16; i++){ | |
1680 ((uint32_t*)(src+i*stride))[0]= | |
1681 ((uint32_t*)(src+i*stride))[1]= | |
1682 ((uint32_t*)(src+i*stride))[2]= | |
1683 ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U; | |
1684 } | |
1685 } | |
1686 | |
1234 | 1687 static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){ |
1184
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1688 int i, j, k; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1689 int a; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1690 uint8_t *cm = cropTbl + MAX_NEG_CROP; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1691 const uint8_t * const src0 = src+7-stride; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1692 const uint8_t *src1 = src+8*stride-1; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1693 const uint8_t *src2 = src1-2*stride; // == src+6*stride-1; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1694 int H = src0[1] - src0[-1]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1695 int V = src1[0] - src2[ 0]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1696 for(k=2; k<=8; ++k) { |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1697 src1 += stride; src2 -= stride; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1698 H += k*(src0[k] - src0[-k]); |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1699 V += k*(src1[0] - src2[ 0]); |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1700 } |
1234 | 1701 if(svq3){ |
1702 H = ( 5*(H/4) ) / 16; | |
1703 V = ( 5*(V/4) ) / 16; | |
1704 }else{ | |
1705 H = ( 5*H+32 ) >> 6; | |
1706 V = ( 5*V+32 ) >> 6; | |
1707 } | |
1184
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1708 |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1709 a = 16*(src1[0] + src2[16] + 1) - 7*(V+H); |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1710 for(j=16; j>0; --j) { |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1711 int b = a; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1712 a += V; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1713 for(i=-16; i<0; i+=4) { |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1714 src[16+i] = cm[ (b ) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1715 src[17+i] = cm[ (b+ H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1716 src[18+i] = cm[ (b+2*H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1717 src[19+i] = cm[ (b+3*H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1718 b += 4*H; |
1168 | 1719 } |
1184
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1720 src += stride; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1721 } |
1168 | 1722 } |
1723 | |
1234 | 1724 static void pred16x16_plane_c(uint8_t *src, int stride){ |
1725 pred16x16_plane_compat_c(src, stride, 0); | |
1726 } | |
1727 | |
1168 | 1728 static void pred8x8_vertical_c(uint8_t *src, int stride){ |
1729 int i; | |
1730 const uint32_t a= ((uint32_t*)(src-stride))[0]; | |
1731 const uint32_t b= ((uint32_t*)(src-stride))[1]; | |
1732 | |
1733 for(i=0; i<8; i++){ | |
1734 ((uint32_t*)(src+i*stride))[0]= a; | |
1735 ((uint32_t*)(src+i*stride))[1]= b; | |
1736 } | |
1737 } | |
1738 | |
1739 static void pred8x8_horizontal_c(uint8_t *src, int stride){ | |
1740 int i; | |
1741 | |
1742 for(i=0; i<8; i++){ | |
1743 ((uint32_t*)(src+i*stride))[0]= | |
1744 ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101; | |
1745 } | |
1746 } | |
1747 | |
1748 static void pred8x8_128_dc_c(uint8_t *src, int stride){ | |
1749 int i; | |
1750 | |
1751 for(i=0; i<4; i++){ | |
1752 ((uint32_t*)(src+i*stride))[0]= | |
1753 ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U; | |
1754 } | |
1755 for(i=4; i<8; i++){ | |
1756 ((uint32_t*)(src+i*stride))[0]= | |
1757 ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U; | |
1758 } | |
1759 } | |
1760 | |
1761 static void pred8x8_left_dc_c(uint8_t *src, int stride){ | |
1762 int i; | |
1763 int dc0, dc2; | |
1764 | |
1765 dc0=dc2=0; | |
1766 for(i=0;i<4; i++){ | |
1767 dc0+= src[-1+i*stride]; | |
1768 dc2+= src[-1+(i+4)*stride]; | |
1769 } | |
1770 dc0= 0x01010101*((dc0 + 2)>>2); | |
1771 dc2= 0x01010101*((dc2 + 2)>>2); | |
1772 | |
1773 for(i=0; i<4; i++){ | |
1774 ((uint32_t*)(src+i*stride))[0]= | |
1775 ((uint32_t*)(src+i*stride))[1]= dc0; | |
1776 } | |
1777 for(i=4; i<8; i++){ | |
1778 ((uint32_t*)(src+i*stride))[0]= | |
1779 ((uint32_t*)(src+i*stride))[1]= dc2; | |
1780 } | |
1781 } | |
1782 | |
1783 static void pred8x8_top_dc_c(uint8_t *src, int stride){ | |
1784 int i; | |
1785 int dc0, dc1; | |
1786 | |
1787 dc0=dc1=0; | |
1788 for(i=0;i<4; i++){ | |
1789 dc0+= src[i-stride]; | |
1790 dc1+= src[4+i-stride]; | |
1791 } | |
1792 dc0= 0x01010101*((dc0 + 2)>>2); | |
1793 dc1= 0x01010101*((dc1 + 2)>>2); | |
1794 | |
1795 for(i=0; i<4; i++){ | |
1796 ((uint32_t*)(src+i*stride))[0]= dc0; | |
1797 ((uint32_t*)(src+i*stride))[1]= dc1; | |
1798 } | |
1799 for(i=4; i<8; i++){ | |
1800 ((uint32_t*)(src+i*stride))[0]= dc0; | |
1801 ((uint32_t*)(src+i*stride))[1]= dc1; | |
1802 } | |
1803 } | |
1804 | |
1805 | |
1806 static void pred8x8_dc_c(uint8_t *src, int stride){ | |
1807 int i; | |
1808 int dc0, dc1, dc2, dc3; | |
1809 | |
1810 dc0=dc1=dc2=0; | |
1811 for(i=0;i<4; i++){ | |
1812 dc0+= src[-1+i*stride] + src[i-stride]; | |
1813 dc1+= src[4+i-stride]; | |
1814 dc2+= src[-1+(i+4)*stride]; | |
1815 } | |
1816 dc3= 0x01010101*((dc1 + dc2 + 4)>>3); | |
1817 dc0= 0x01010101*((dc0 + 4)>>3); | |
1818 dc1= 0x01010101*((dc1 + 2)>>2); | |
1819 dc2= 0x01010101*((dc2 + 2)>>2); | |
1820 | |
1821 for(i=0; i<4; i++){ | |
1822 ((uint32_t*)(src+i*stride))[0]= dc0; | |
1823 ((uint32_t*)(src+i*stride))[1]= dc1; | |
1824 } | |
1825 for(i=4; i<8; i++){ | |
1826 ((uint32_t*)(src+i*stride))[0]= dc2; | |
1827 ((uint32_t*)(src+i*stride))[1]= dc3; | |
1828 } | |
1829 } | |
1830 | |
1831 static void pred8x8_plane_c(uint8_t *src, int stride){ | |
1184
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1832 int j, k; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1833 int a; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1834 uint8_t *cm = cropTbl + MAX_NEG_CROP; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1835 const uint8_t * const src0 = src+3-stride; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1836 const uint8_t *src1 = src+4*stride-1; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1837 const uint8_t *src2 = src1-2*stride; // == src+2*stride-1; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1838 int H = src0[1] - src0[-1]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1839 int V = src1[0] - src2[ 0]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1840 for(k=2; k<=4; ++k) { |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1841 src1 += stride; src2 -= stride; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1842 H += k*(src0[k] - src0[-k]); |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1843 V += k*(src1[0] - src2[ 0]); |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1844 } |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1845 H = ( 17*H+16 ) >> 5; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1846 V = ( 17*V+16 ) >> 5; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1847 |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1848 a = 16*(src1[0] + src2[8]+1) - 3*(V+H); |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1849 for(j=8; j>0; --j) { |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1850 int b = a; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1851 a += V; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1852 src[0] = cm[ (b ) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1853 src[1] = cm[ (b+ H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1854 src[2] = cm[ (b+2*H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1855 src[3] = cm[ (b+3*H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1856 src[4] = cm[ (b+4*H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1857 src[5] = cm[ (b+5*H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1858 src[6] = cm[ (b+6*H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1859 src[7] = cm[ (b+7*H) >> 5 ]; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1860 src += stride; |
05a2ac8978ad
faster 8x8 & 16x16 plane prediction by skal (massimin at planet-d dot net)
michaelni
parents:
1177
diff
changeset
|
1861 } |
1168 | 1862 } |
1863 | |
1864 static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list, | |
1865 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | |
1866 int src_x_offset, int src_y_offset, | |
1867 qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){ | |
1868 MpegEncContext * const s = &h->s; | |
1869 const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8; | |
1870 const int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8; | |
1871 const int luma_xy= (mx&3) + ((my&3)<<2); | |
1872 uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*s->linesize; | |
1873 uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*s->uvlinesize; | |
1874 uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*s->uvlinesize; | |
1875 int extra_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; //FIXME increase edge?, IMHO not worth it | |
1876 int extra_height= extra_width; | |
1877 int emu=0; | |
1878 const int full_mx= mx>>2; | |
1879 const int full_my= my>>2; | |
1880 | |
1881 assert(pic->data[0]); | |
1882 | |
1883 if(mx&7) extra_width -= 3; | |
1884 if(my&7) extra_height -= 3; | |
1885 | |
1886 if( full_mx < 0-extra_width | |
1887 || full_my < 0-extra_height | |
1888 || full_mx + 16/*FIXME*/ > s->width + extra_width | |
1889 || full_my + 16/*FIXME*/ > s->height + extra_height){ | |
1890 ff_emulated_edge_mc(s, src_y - 2 - 2*s->linesize, s->linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, s->width, s->height); | |
1891 src_y= s->edge_emu_buffer + 2 + 2*s->linesize; | |
1892 emu=1; | |
1893 } | |
1894 | |
1895 qpix_op[luma_xy](dest_y, src_y, s->linesize); //FIXME try variable height perhaps? | |
1896 if(!square){ | |
1897 qpix_op[luma_xy](dest_y + delta, src_y + delta, s->linesize); | |
1898 } | |
1899 | |
1900 if(s->flags&CODEC_FLAG_GRAY) return; | |
1901 | |
1902 if(emu){ | |
1903 ff_emulated_edge_mc(s, src_cb, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1); | |
1904 src_cb= s->edge_emu_buffer; | |
1905 } | |
1906 chroma_op(dest_cb, src_cb, s->uvlinesize, chroma_height, mx&7, my&7); | |
1907 | |
1908 if(emu){ | |
1909 ff_emulated_edge_mc(s, src_cr, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1); | |
1910 src_cr= s->edge_emu_buffer; | |
1911 } | |
1912 chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7); | |
1913 } | |
1914 | |
1915 static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta, | |
1916 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | |
1917 int x_offset, int y_offset, | |
1918 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put, | |
1919 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg, | |
1920 int list0, int list1){ | |
1921 MpegEncContext * const s = &h->s; | |
1922 qpel_mc_func *qpix_op= qpix_put; | |
1923 h264_chroma_mc_func chroma_op= chroma_put; | |
1924 | |
1925 dest_y += 2*x_offset + 2*y_offset*s-> linesize; | |
1926 dest_cb += x_offset + y_offset*s->uvlinesize; | |
1927 dest_cr += x_offset + y_offset*s->uvlinesize; | |
1928 x_offset += 8*s->mb_x; | |
1929 y_offset += 8*s->mb_y; | |
1930 | |
1931 if(list0){ | |
1169 | 1932 Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ]; |
1168 | 1933 mc_dir_part(h, ref, n, square, chroma_height, delta, 0, |
1934 dest_y, dest_cb, dest_cr, x_offset, y_offset, | |
1935 qpix_op, chroma_op); | |
1936 | |
1937 qpix_op= qpix_avg; | |
1938 chroma_op= chroma_avg; | |
1939 } | |
1940 | |
1941 if(list1){ | |
1169 | 1942 Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ]; |
1168 | 1943 mc_dir_part(h, ref, n, square, chroma_height, delta, 1, |
1944 dest_y, dest_cb, dest_cr, x_offset, y_offset, | |
1945 qpix_op, chroma_op); | |
1946 } | |
1947 } | |
1948 | |
1949 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | |
1950 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put), | |
1951 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg)){ | |
1952 MpegEncContext * const s = &h->s; | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
1953 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; |
1168 | 1954 const int mb_type= s->current_picture.mb_type[mb_xy]; |
1955 | |
1956 assert(IS_INTER(mb_type)); | |
1957 | |
1958 if(IS_16X16(mb_type)){ | |
1959 mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0, | |
1960 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0], | |
1961 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1)); | |
1962 }else if(IS_16X8(mb_type)){ | |
1963 mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0, | |
1964 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0], | |
1965 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1)); | |
1966 mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4, | |
1967 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0], | |
1968 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1)); | |
1969 }else if(IS_8X16(mb_type)){ | |
1970 mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0, | |
1971 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1], | |
1972 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1)); | |
1973 mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0, | |
1974 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1], | |
1975 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1)); | |
1976 }else{ | |
1977 int i; | |
1978 | |
1979 assert(IS_8X8(mb_type)); | |
1980 | |
1981 for(i=0; i<4; i++){ | |
1982 const int sub_mb_type= h->sub_mb_type[i]; | |
1983 const int n= 4*i; | |
1984 int x_offset= (i&1)<<2; | |
1985 int y_offset= (i&2)<<1; | |
1986 | |
1987 if(IS_SUB_8X8(sub_mb_type)){ | |
1988 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset, | |
1989 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1], | |
1990 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); | |
1991 }else if(IS_SUB_8X4(sub_mb_type)){ | |
1992 mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset, | |
1993 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1], | |
1994 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); | |
1995 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2, | |
1996 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1], | |
1997 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); | |
1998 }else if(IS_SUB_4X8(sub_mb_type)){ | |
1999 mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset, | |
2000 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2], | |
2001 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); | |
2002 mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset, | |
2003 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2], | |
2004 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); | |
2005 }else{ | |
2006 int j; | |
2007 assert(IS_SUB_4X4(sub_mb_type)); | |
2008 for(j=0; j<4; j++){ | |
2009 int sub_x_offset= x_offset + 2*(j&1); | |
2010 int sub_y_offset= y_offset + (j&2); | |
2011 mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset, | |
2012 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2], | |
2013 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1)); | |
2014 } | |
2015 } | |
2016 } | |
2017 } | |
2018 } | |
2019 | |
2020 static void decode_init_vlc(H264Context *h){ | |
2021 static int done = 0; | |
2022 | |
2023 if (!done) { | |
2024 int i; | |
2025 done = 1; | |
2026 | |
2027 init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5, | |
2028 &chroma_dc_coeff_token_len [0], 1, 1, | |
2029 &chroma_dc_coeff_token_bits[0], 1, 1); | |
2030 | |
2031 for(i=0; i<4; i++){ | |
2032 init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17, | |
2033 &coeff_token_len [i][0], 1, 1, | |
2034 &coeff_token_bits[i][0], 1, 1); | |
2035 } | |
2036 | |
2037 for(i=0; i<3; i++){ | |
2038 init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4, | |
2039 &chroma_dc_total_zeros_len [i][0], 1, 1, | |
2040 &chroma_dc_total_zeros_bits[i][0], 1, 1); | |
2041 } | |
2042 for(i=0; i<15; i++){ | |
2043 init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16, | |
2044 &total_zeros_len [i][0], 1, 1, | |
2045 &total_zeros_bits[i][0], 1, 1); | |
2046 } | |
2047 | |
2048 for(i=0; i<6; i++){ | |
2049 init_vlc(&run_vlc[i], RUN_VLC_BITS, 7, | |
2050 &run_len [i][0], 1, 1, | |
2051 &run_bits[i][0], 1, 1); | |
2052 } | |
2053 init_vlc(&run7_vlc, RUN7_VLC_BITS, 16, | |
2054 &run_len [6][0], 1, 1, | |
2055 &run_bits[6][0], 1, 1); | |
2056 } | |
2057 } | |
2058 | |
2059 /** | |
2060 * Sets the intra prediction function pointers. | |
2061 */ | |
2062 static void init_pred_ptrs(H264Context *h){ | |
2063 // MpegEncContext * const s = &h->s; | |
2064 | |
2065 h->pred4x4[VERT_PRED ]= pred4x4_vertical_c; | |
2066 h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c; | |
2067 h->pred4x4[DC_PRED ]= pred4x4_dc_c; | |
2068 h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c; | |
2069 h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c; | |
2070 h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c; | |
2071 h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c; | |
2072 h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c; | |
2073 h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c; | |
2074 h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c; | |
2075 h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c; | |
2076 h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c; | |
2077 | |
2078 h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c; | |
2079 h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c; | |
2080 h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c; | |
2081 h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c; | |
2082 h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c; | |
2083 h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c; | |
2084 h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c; | |
2085 | |
2086 h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c; | |
2087 h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c; | |
2088 h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c; | |
2089 h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c; | |
2090 h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c; | |
2091 h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c; | |
2092 h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c; | |
2093 } | |
2094 | |
2095 //FIXME factorize | |
2096 #define CHECKED_ALLOCZ(p, size)\ | |
2097 {\ | |
2098 p= av_mallocz(size);\ | |
2099 if(p==NULL){\ | |
2100 perror("malloc");\ | |
2101 goto fail;\ | |
2102 }\ | |
2103 } | |
2104 | |
2105 static void free_tables(H264Context *h){ | |
2106 av_freep(&h->intra4x4_pred_mode); | |
2107 av_freep(&h->non_zero_count); | |
2108 av_freep(&h->slice_table_base); | |
2109 h->slice_table= NULL; | |
2110 | |
2111 av_freep(&h->mb2b_xy); | |
2112 av_freep(&h->mb2b8_xy); | |
2113 } | |
2114 | |
2115 /** | |
2116 * allocates tables. | |
2117 * needs widzh/height | |
2118 */ | |
2119 static int alloc_tables(H264Context *h){ | |
2120 MpegEncContext * const s = &h->s; | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
2121 const int big_mb_num= s->mb_stride * (s->mb_height+1); |
1168 | 2122 int x,y; |
2123 | |
2124 CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t)) | |
2125 CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t)) | |
2126 CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t)) | |
2127 | |
2128 memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t)); | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
2129 h->slice_table= h->slice_table_base + s->mb_stride + 1; |
1168 | 2130 |
2131 CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint16_t)); | |
2132 CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint16_t)); | |
2133 for(y=0; y<s->mb_height; y++){ | |
2134 for(x=0; x<s->mb_width; x++){ | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
2135 const int mb_xy= x + y*s->mb_stride; |
1168 | 2136 const int b_xy = 4*x + 4*y*h->b_stride; |
2137 const int b8_xy= 2*x + 2*y*h->b8_stride; | |
2138 | |
2139 h->mb2b_xy [mb_xy]= b_xy; | |
2140 h->mb2b8_xy[mb_xy]= b8_xy; | |
2141 } | |
2142 } | |
2143 | |
2144 return 0; | |
2145 fail: | |
2146 free_tables(h); | |
2147 return -1; | |
2148 } | |
2149 | |
2150 static void common_init(H264Context *h){ | |
2151 MpegEncContext * const s = &h->s; | |
2152 | |
2153 s->width = s->avctx->width; | |
2154 s->height = s->avctx->height; | |
2155 s->codec_id= s->avctx->codec->id; | |
2156 | |
2157 init_pred_ptrs(h); | |
2158 | |
2159 s->decode=1; //FIXME | |
2160 } | |
2161 | |
2162 static int decode_init(AVCodecContext *avctx){ | |
2163 H264Context *h= avctx->priv_data; | |
2164 MpegEncContext * const s = &h->s; | |
2165 | |
2166 s->avctx = avctx; | |
2167 common_init(h); | |
2168 | |
2169 s->out_format = FMT_H264; | |
2170 s->workaround_bugs= avctx->workaround_bugs; | |
2171 | |
2172 // set defaults | |
2173 s->progressive_sequence=1; | |
2174 // s->decode_mb= ff_h263_decode_mb; | |
2175 s->low_delay= 1; | |
2176 avctx->pix_fmt= PIX_FMT_YUV420P; | |
2177 | |
2178 decode_init_vlc(h); | |
2179 | |
2180 return 0; | |
2181 } | |
2182 | |
2183 static void frame_start(H264Context *h){ | |
2184 MpegEncContext * const s = &h->s; | |
2185 int i; | |
2186 | |
2187 MPV_frame_start(s, s->avctx); | |
2188 ff_er_frame_start(s); | |
2189 h->mmco_index=0; | |
2190 | |
2191 assert(s->linesize && s->uvlinesize); | |
2192 | |
2193 for(i=0; i<16; i++){ | |
2194 h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3); | |
2195 h->chroma_subblock_offset[i]= 2*((scan8[i] - scan8[0])&7) + 2*s->uvlinesize*((scan8[i] - scan8[0])>>3); | |
2196 } | |
2197 for(i=0; i<4; i++){ | |
2198 h->block_offset[16+i]= | |
2199 h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3); | |
2200 } | |
2201 | |
2202 // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1; | |
2203 } | |
2204 | |
2205 static void hl_decode_mb(H264Context *h){ | |
2206 MpegEncContext * const s = &h->s; | |
2207 const int mb_x= s->mb_x; | |
2208 const int mb_y= s->mb_y; | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
2209 const int mb_xy= mb_x + mb_y*s->mb_stride; |
1168 | 2210 const int mb_type= s->current_picture.mb_type[mb_xy]; |
2211 uint8_t *dest_y, *dest_cb, *dest_cr; | |
2212 int linesize, uvlinesize /*dct_offset*/; | |
2213 int i; | |
2214 | |
2215 if(!s->decode) | |
2216 return; | |
2217 | |
2218 if(s->mb_skiped){ | |
2219 } | |
2220 | |
2221 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16; | |
2222 dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; | |
2223 dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; | |
2224 | |
2225 if (h->mb_field_decoding_flag) { | |
2226 linesize = s->linesize * 2; | |
2227 uvlinesize = s->uvlinesize * 2; | |
2228 if(mb_y&1){ //FIXME move out of this func? | |
2229 dest_y -= s->linesize*15; | |
2230 dest_cb-= s->linesize*7; | |
2231 dest_cr-= s->linesize*7; | |
2232 } | |
2233 } else { | |
2234 linesize = s->linesize; | |
2235 uvlinesize = s->uvlinesize; | |
2236 // dct_offset = s->linesize * 16; | |
2237 } | |
2238 | |
2239 if(IS_INTRA(mb_type)){ | |
2240 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
2241 h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize); | |
2242 h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize); | |
2243 } | |
2244 | |
2245 if(IS_INTRA4x4(mb_type)){ | |
2246 if(!s->encoding){ | |
2247 for(i=0; i<16; i++){ | |
2248 uint8_t * const ptr= dest_y + h->block_offset[i]; | |
2249 uint8_t *topright= ptr + 4 - linesize; | |
2250 const int topright_avail= (h->topright_samples_available<<i)&0x8000; | |
2251 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ]; | |
2252 int tr; | |
2253 | |
2254 if(!topright_avail){ | |
2255 tr= ptr[3 - linesize]*0x01010101; | |
2256 topright= (uint8_t*) &tr; | |
2257 } | |
2258 | |
2259 h->pred4x4[ dir ](ptr, topright, linesize); | |
1234 | 2260 if(h->non_zero_count_cache[ scan8[i] ]){ |
2261 if(s->codec_id == CODEC_ID_H264) | |
2262 h264_add_idct_c(ptr, h->mb + i*16, linesize); | |
2263 else | |
2264 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0); | |
2265 } | |
1168 | 2266 } |
2267 } | |
2268 }else{ | |
2269 h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize); | |
1234 | 2270 if(s->codec_id == CODEC_ID_H264) |
2271 h264_luma_dc_dequant_idct_c(h->mb, s->qscale); | |
2272 else | |
2273 svq3_luma_dc_dequant_idct_c(h->mb, s->qscale); | |
1168 | 2274 } |
1234 | 2275 }else if(s->codec_id == CODEC_ID_H264){ |
1168 | 2276 hl_motion(h, dest_y, dest_cb, dest_cr, |
2277 s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab, | |
2278 s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab); | |
2279 } | |
2280 | |
2281 | |
2282 if(!IS_INTRA4x4(mb_type)){ | |
1250 | 2283 if(s->codec_id == CODEC_ID_H264){ |
2284 for(i=0; i<16; i++){ | |
2285 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below | |
2286 uint8_t * const ptr= dest_y + h->block_offset[i]; | |
1234 | 2287 h264_add_idct_c(ptr, h->mb + i*16, linesize); |
1250 | 2288 } |
2289 } | |
2290 }else{ | |
2291 for(i=0; i<16; i++){ | |
2292 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below | |
2293 uint8_t * const ptr= dest_y + h->block_offset[i]; | |
1234 | 2294 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0); |
1250 | 2295 } |
1168 | 2296 } |
2297 } | |
2298 } | |
2299 | |
2300 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
2301 chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp); | |
2302 chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp); | |
1250 | 2303 if(s->codec_id == CODEC_ID_H264){ |
2304 for(i=16; i<16+4; i++){ | |
2305 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ | |
2306 uint8_t * const ptr= dest_cb + h->block_offset[i]; | |
1234 | 2307 h264_add_idct_c(ptr, h->mb + i*16, uvlinesize); |
1250 | 2308 } |
2309 } | |
2310 for(i=20; i<20+4; i++){ | |
2311 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ | |
2312 uint8_t * const ptr= dest_cr + h->block_offset[i]; | |
2313 h264_add_idct_c(ptr, h->mb + i*16, uvlinesize); | |
2314 } | |
1168 | 2315 } |
1250 | 2316 }else{ |
2317 for(i=16; i<16+4; i++){ | |
2318 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ | |
2319 uint8_t * const ptr= dest_cb + h->block_offset[i]; | |
1234 | 2320 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2); |
1250 | 2321 } |
2322 } | |
2323 for(i=20; i<20+4; i++){ | |
2324 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ | |
2325 uint8_t * const ptr= dest_cr + h->block_offset[i]; | |
2326 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2); | |
2327 } | |
1168 | 2328 } |
2329 } | |
2330 } | |
2331 } | |
2332 | |
2333 static void decode_mb_cabac(H264Context *h){ | |
2334 // MpegEncContext * const s = &h->s; | |
2335 } | |
2336 | |
2337 /** | |
2338 * fills the default_ref_list. | |
2339 */ | |
2340 static int fill_default_ref_list(H264Context *h){ | |
2341 MpegEncContext * const s = &h->s; | |
2342 int i; | |
2343 Picture sorted_short_ref[16]; | |
2344 | |
2345 if(h->slice_type==B_TYPE){ | |
2346 int out_i; | |
2347 int limit= -1; | |
2348 | |
2349 for(out_i=0; out_i<h->short_ref_count; out_i++){ | |
2350 int best_i=-1; | |
2351 int best_poc=-1; | |
2352 | |
2353 for(i=0; i<h->short_ref_count; i++){ | |
2354 const int poc= h->short_ref[i]->poc; | |
2355 if(poc > limit && poc < best_poc){ | |
2356 best_poc= poc; | |
2357 best_i= i; | |
2358 } | |
2359 } | |
2360 | |
2361 assert(best_i != -1); | |
2362 | |
2363 limit= best_poc; | |
2364 sorted_short_ref[out_i]= *h->short_ref[best_i]; | |
2365 } | |
2366 } | |
2367 | |
2368 if(s->picture_structure == PICT_FRAME){ | |
2369 if(h->slice_type==B_TYPE){ | |
2370 const int current_poc= s->current_picture_ptr->poc; | |
2371 int list; | |
2372 | |
2373 for(list=0; list<2; list++){ | |
2374 int index=0; | |
2375 | |
2376 for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++){ | |
2377 const int i2= list ? h->short_ref_count - i - 1 : i; | |
2378 const int poc= sorted_short_ref[i2].poc; | |
2379 | |
2380 if(sorted_short_ref[i2].reference != 3) continue; //FIXME refernce field shit | |
2381 | |
2382 if((list==1 && poc > current_poc) || (list==0 && poc < current_poc)){ | |
2383 h->default_ref_list[list][index ]= sorted_short_ref[i2]; | |
2384 h->default_ref_list[list][index++].pic_id= sorted_short_ref[i2].frame_num; | |
2385 } | |
2386 } | |
2387 | |
2388 for(i=0; i<h->long_ref_count && index < h->ref_count[ list ]; i++){ | |
2389 if(h->long_ref[i]->reference != 3) continue; | |
2390 | |
2391 h->default_ref_list[ list ][index ]= *h->long_ref[i]; | |
2392 h->default_ref_list[ list ][index++].pic_id= i;; | |
2393 } | |
2394 | |
2395 if(h->long_ref_count > 1 && h->short_ref_count==0){ | |
2396 Picture temp= h->default_ref_list[1][0]; | |
2397 h->default_ref_list[1][0] = h->default_ref_list[1][1]; | |
2398 h->default_ref_list[1][0] = temp; | |
2399 } | |
2400 | |
2401 if(index < h->ref_count[ list ]) | |
2402 memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index)); | |
2403 } | |
2404 }else{ | |
2405 int index=0; | |
2406 for(i=0; i<h->short_ref_count && index < h->ref_count[0]; i++){ | |
2407 if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit | |
2408 h->default_ref_list[0][index ]= *h->short_ref[i]; | |
2409 h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num; | |
2410 } | |
2411 for(i=0; i<h->long_ref_count && index < h->ref_count[0]; i++){ | |
2412 if(h->long_ref[i]->reference != 3) continue; | |
2413 h->default_ref_list[0][index ]= *h->long_ref[i]; | |
2414 h->default_ref_list[0][index++].pic_id= i;; | |
2415 } | |
2416 if(index < h->ref_count[0]) | |
2417 memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index)); | |
2418 } | |
2419 }else{ //FIELD | |
2420 if(h->slice_type==B_TYPE){ | |
2421 }else{ | |
2422 //FIXME second field balh | |
2423 } | |
2424 } | |
2425 return 0; | |
2426 } | |
2427 | |
2428 static int decode_ref_pic_list_reordering(H264Context *h){ | |
2429 MpegEncContext * const s = &h->s; | |
2430 int list; | |
2431 | |
2432 if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move beofre func | |
2433 | |
2434 for(list=0; list<2; list++){ | |
2435 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]); | |
2436 | |
2437 if(get_bits1(&s->gb)){ | |
2438 int pred= h->curr_pic_num; | |
2439 int index; | |
2440 | |
2441 for(index=0; ; index++){ | |
2442 int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb); | |
2443 int pic_id; | |
2444 int i; | |
2445 | |
2446 | |
2447 if(index >= h->ref_count[list]){ | |
2448 fprintf(stderr, "reference count overflow\n"); | |
2449 return -1; | |
2450 } | |
2451 | |
2452 if(reordering_of_pic_nums_idc<3){ | |
2453 if(reordering_of_pic_nums_idc<2){ | |
2454 const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1; | |
2455 | |
2456 if(abs_diff_pic_num >= h->max_pic_num){ | |
2457 fprintf(stderr, "abs_diff_pic_num overflow\n"); | |
2458 return -1; | |
2459 } | |
2460 | |
2461 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num; | |
2462 else pred+= abs_diff_pic_num; | |
2463 pred &= h->max_pic_num - 1; | |
2464 | |
2465 for(i= h->ref_count[list]-1; i>=index; i--){ | |
2466 if(h->ref_list[list][i].pic_id == pred && h->ref_list[list][i].long_ref==0) | |
2467 break; | |
2468 } | |
2469 }else{ | |
2470 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx | |
2471 | |
2472 for(i= h->ref_count[list]-1; i>=index; i--){ | |
2473 if(h->ref_list[list][i].pic_id == pic_id && h->ref_list[list][i].long_ref==1) | |
2474 break; | |
2475 } | |
2476 } | |
2477 | |
2478 if(i < index){ | |
2479 fprintf(stderr, "reference picture missing during reorder\n"); | |
2480 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME | |
2481 }else if(i > index){ | |
2482 Picture tmp= h->ref_list[list][i]; | |
2483 for(; i>index; i--){ | |
2484 h->ref_list[list][i]= h->ref_list[list][i-1]; | |
2485 } | |
2486 h->ref_list[list][index]= tmp; | |
2487 } | |
2488 }else if(reordering_of_pic_nums_idc==3) | |
2489 break; | |
2490 else{ | |
2491 fprintf(stderr, "illegal reordering_of_pic_nums_idc\n"); | |
2492 return -1; | |
2493 } | |
2494 } | |
2495 } | |
2496 | |
2497 if(h->slice_type!=B_TYPE) break; | |
2498 } | |
2499 return 0; | |
2500 } | |
2501 | |
2502 static int pred_weight_table(H264Context *h){ | |
2503 MpegEncContext * const s = &h->s; | |
2504 int list, i; | |
2505 | |
2506 h->luma_log2_weight_denom= get_ue_golomb(&s->gb); | |
2507 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb); | |
2508 | |
2509 for(list=0; list<2; list++){ | |
2510 for(i=0; i<h->ref_count[list]; i++){ | |
2511 int luma_weight_flag, chroma_weight_flag; | |
2512 | |
2513 luma_weight_flag= get_bits1(&s->gb); | |
2514 if(luma_weight_flag){ | |
2515 h->luma_weight[list][i]= get_se_golomb(&s->gb); | |
2516 h->luma_offset[list][i]= get_se_golomb(&s->gb); | |
2517 } | |
2518 | |
2519 chroma_weight_flag= get_bits1(&s->gb); | |
2520 if(chroma_weight_flag){ | |
2521 int j; | |
2522 for(j=0; j<2; j++){ | |
2523 h->chroma_weight[list][i][j]= get_se_golomb(&s->gb); | |
2524 h->chroma_offset[list][i][j]= get_se_golomb(&s->gb); | |
2525 } | |
2526 } | |
2527 } | |
2528 if(h->slice_type != B_TYPE) break; | |
2529 } | |
2530 return 0; | |
2531 } | |
2532 | |
2533 /** | |
2534 * instantaneos decoder refresh. | |
2535 */ | |
2536 static void idr(H264Context *h){ | |
2537 int i; | |
2538 | |
2539 for(i=0; i<h->long_ref_count; i++){ | |
2540 h->long_ref[i]->reference=0; | |
2541 h->long_ref[i]= NULL; | |
2542 } | |
2543 h->long_ref_count=0; | |
2544 | |
2545 for(i=0; i<h->short_ref_count; i++){ | |
2546 h->short_ref[i]->reference=0; | |
2547 h->short_ref[i]= NULL; | |
2548 } | |
2549 h->short_ref_count=0; | |
2550 } | |
2551 | |
2552 /** | |
2553 * | |
2554 * @return the removed picture or NULL if an error occures | |
2555 */ | |
2556 static Picture * remove_short(H264Context *h, int frame_num){ | |
1169 | 2557 MpegEncContext * const s = &h->s; |
1168 | 2558 int i; |
2559 | |
1169 | 2560 if(s->avctx->debug&FF_DEBUG_MMCO) |
2561 printf("remove short %d count %d\n", frame_num, h->short_ref_count); | |
2562 | |
1168 | 2563 for(i=0; i<h->short_ref_count; i++){ |
2564 Picture *pic= h->short_ref[i]; | |
1169 | 2565 if(s->avctx->debug&FF_DEBUG_MMCO) |
2566 printf("%d %d %X\n", i, pic->frame_num, (int)pic); | |
1168 | 2567 if(pic->frame_num == frame_num){ |
2568 h->short_ref[i]= NULL; | |
2569 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*)); | |
2570 h->short_ref_count--; | |
2571 return pic; | |
2572 } | |
2573 } | |
2574 return NULL; | |
2575 } | |
2576 | |
2577 /** | |
2578 * | |
2579 * @return the removed picture or NULL if an error occures | |
2580 */ | |
2581 static Picture * remove_long(H264Context *h, int i){ | |
2582 Picture *pic; | |
2583 | |
2584 if(i >= h->long_ref_count) return NULL; | |
2585 pic= h->long_ref[i]; | |
2586 if(pic==NULL) return NULL; | |
2587 | |
2588 h->long_ref[i]= NULL; | |
2589 memmove(&h->long_ref[i], &h->long_ref[i+1], (h->long_ref_count - i - 1)*sizeof(Picture*)); | |
2590 h->long_ref_count--; | |
2591 | |
2592 return pic; | |
2593 } | |
2594 | |
2595 /** | |
2596 * Executes the reference picture marking (memory management control operations). | |
2597 */ | |
2598 static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){ | |
2599 MpegEncContext * const s = &h->s; | |
2600 int i; | |
2601 int current_is_long=0; | |
2602 Picture *pic; | |
2603 | |
2604 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0) | |
2605 printf("no mmco here\n"); | |
2606 | |
2607 for(i=0; i<mmco_count; i++){ | |
2608 if(s->avctx->debug&FF_DEBUG_MMCO) | |
2609 printf("mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index); | |
2610 | |
2611 switch(mmco[i].opcode){ | |
2612 case MMCO_SHORT2UNUSED: | |
2613 pic= remove_short(h, mmco[i].short_frame_num); | |
2614 if(pic==NULL) return -1; | |
2615 pic->reference= 0; | |
2616 break; | |
2617 case MMCO_SHORT2LONG: | |
2618 pic= remove_long(h, mmco[i].long_index); | |
2619 if(pic) pic->reference=0; | |
2620 | |
2621 h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num); | |
2622 h->long_ref[ mmco[i].long_index ]->long_ref=1; | |
2623 break; | |
2624 case MMCO_LONG2UNUSED: | |
2625 pic= remove_long(h, mmco[i].long_index); | |
2626 if(pic==NULL) return -1; | |
2627 pic->reference= 0; | |
2628 break; | |
2629 case MMCO_LONG: | |
2630 pic= remove_long(h, mmco[i].long_index); | |
2631 if(pic) pic->reference=0; | |
2632 | |
2633 h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr; | |
2634 h->long_ref[ mmco[i].long_index ]->long_ref=1; | |
2635 h->long_ref_count++; | |
2636 | |
2637 current_is_long=1; | |
2638 break; | |
2639 case MMCO_SET_MAX_LONG: | |
2640 assert(mmco[i].long_index <= 16); | |
2641 while(mmco[i].long_index < h->long_ref_count){ | |
2642 pic= remove_long(h, mmco[i].long_index); | |
2643 pic->reference=0; | |
2644 } | |
2645 while(mmco[i].long_index > h->long_ref_count){ | |
2646 h->long_ref[ h->long_ref_count++ ]= NULL; | |
2647 } | |
2648 break; | |
2649 case MMCO_RESET: | |
2650 while(h->short_ref_count){ | |
2651 pic= remove_short(h, h->short_ref[0]->frame_num); | |
2652 pic->reference=0; | |
2653 } | |
2654 while(h->long_ref_count){ | |
2655 pic= remove_long(h, h->long_ref_count-1); | |
2656 pic->reference=0; | |
2657 } | |
2658 break; | |
2659 default: assert(0); | |
2660 } | |
2661 } | |
2662 | |
2663 if(!current_is_long){ | |
2664 pic= remove_short(h, s->current_picture_ptr->frame_num); | |
2665 if(pic){ | |
2666 pic->reference=0; | |
2667 fprintf(stderr, "illegal short term buffer state detected\n"); | |
2668 } | |
2669 | |
2670 if(h->short_ref_count) | |
1169 | 2671 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*)); |
2672 | |
2673 h->short_ref[0]= s->current_picture_ptr; | |
1168 | 2674 h->short_ref[0]->long_ref=0; |
2675 h->short_ref_count++; | |
2676 } | |
2677 | |
2678 return 0; | |
2679 } | |
2680 | |
2681 static int decode_ref_pic_marking(H264Context *h){ | |
2682 MpegEncContext * const s = &h->s; | |
2683 int i; | |
2684 | |
2685 if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields | |
2686 s->broken_link= get_bits1(&s->gb) -1; | |
2687 h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx | |
2688 if(h->mmco[0].long_index == -1) | |
2689 h->mmco_index= 0; | |
2690 else{ | |
2691 h->mmco[0].opcode= MMCO_LONG; | |
2692 h->mmco_index= 1; | |
2693 } | |
2694 }else{ | |
2695 if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag | |
2696 for(i= h->mmco_index; i<MAX_MMCO_COUNT; i++) { | |
2697 MMCOOpcode opcode= get_ue_golomb(&s->gb);; | |
2698 | |
2699 h->mmco[i].opcode= opcode; | |
2700 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){ | |
2701 h->mmco[i].short_frame_num= (h->frame_num - get_ue_golomb(&s->gb) - 1) & ((1<<h->sps.log2_max_frame_num)-1); //FIXME fields | |
2702 /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){ | |
2703 fprintf(stderr, "illegal short ref in memory management control operation %d\n", mmco); | |
2704 return -1; | |
2705 }*/ | |
2706 } | |
2707 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){ | |
2708 h->mmco[i].long_index= get_ue_golomb(&s->gb); | |
2709 if(/*h->mmco[i].long_index >= h->long_ref_count || h->long_ref[ h->mmco[i].long_index ] == NULL*/ h->mmco[i].long_index >= 16){ | |
2710 fprintf(stderr, "illegal long ref in memory management control operation %d\n", opcode); | |
2711 return -1; | |
2712 } | |
2713 } | |
2714 | |
2715 if(opcode > MMCO_LONG){ | |
2716 fprintf(stderr, "illegal memory management control operation %d\n", opcode); | |
2717 return -1; | |
2718 } | |
2719 } | |
2720 h->mmco_index= i; | |
2721 }else{ | |
2722 assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count); | |
2723 | |
2724 if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields | |
2725 h->mmco[0].opcode= MMCO_SHORT2UNUSED; | |
2726 h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num; | |
2727 h->mmco_index= 1; | |
2728 }else | |
2729 h->mmco_index= 0; | |
2730 } | |
2731 } | |
2732 | |
2733 return 0; | |
2734 } | |
2735 | |
2736 static int init_poc(H264Context *h){ | |
2737 MpegEncContext * const s = &h->s; | |
2738 const int max_frame_num= 1<<h->sps.log2_max_frame_num; | |
2739 int field_poc[2]; | |
2740 | |
2741 if(h->nal_unit_type == NAL_IDR_SLICE){ | |
2742 h->frame_num_offset= 0; | |
2743 }else{ | |
2744 if(h->frame_num < h->prev_frame_num) | |
2745 h->frame_num_offset= h->prev_frame_num_offset + max_frame_num; | |
2746 else | |
2747 h->frame_num_offset= h->prev_frame_num_offset; | |
2748 } | |
2749 | |
2750 if(h->sps.poc_type==0){ | |
2751 const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb; | |
2752 | |
2753 if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2) | |
2754 h->poc_msb = h->prev_poc_msb + max_poc_lsb; | |
2755 else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2) | |
2756 h->poc_msb = h->prev_poc_msb - max_poc_lsb; | |
2757 else | |
2758 h->poc_msb = h->prev_poc_msb; | |
2759 //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb); | |
2760 field_poc[0] = | |
2761 field_poc[1] = h->poc_msb + h->poc_lsb; | |
2762 if(s->picture_structure == PICT_FRAME) | |
2763 field_poc[1] += h->delta_poc_bottom; | |
2764 }else if(h->sps.poc_type==1){ | |
2765 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc; | |
2766 int i; | |
2767 | |
2768 if(h->sps.poc_cycle_length != 0) | |
2769 abs_frame_num = h->frame_num_offset + h->frame_num; | |
2770 else | |
2771 abs_frame_num = 0; | |
2772 | |
2773 if(h->nal_ref_idc==0 && abs_frame_num > 0) | |
2774 abs_frame_num--; | |
2775 | |
2776 expected_delta_per_poc_cycle = 0; | |
2777 for(i=0; i < h->sps.poc_cycle_length; i++) | |
2778 expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse | |
2779 | |
2780 if(abs_frame_num > 0){ | |
2781 int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length; | |
2782 int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length; | |
2783 | |
2784 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle; | |
2785 for(i = 0; i <= frame_num_in_poc_cycle; i++) | |
2786 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ]; | |
2787 } else | |
2788 expectedpoc = 0; | |
2789 | |
2790 if(h->nal_ref_idc == 0) | |
2791 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic; | |
2792 | |
2793 field_poc[0] = expectedpoc + h->delta_poc[0]; | |
2794 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field; | |
2795 | |
2796 if(s->picture_structure == PICT_FRAME) | |
2797 field_poc[1] += h->delta_poc[1]; | |
2798 }else{ | |
2799 int poc; | |
2800 if(h->nal_unit_type == NAL_IDR_SLICE){ | |
2801 poc= 0; | |
2802 }else{ | |
2803 if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num); | |
2804 else poc= 2*(h->frame_num_offset + h->frame_num) - 1; | |
2805 } | |
2806 field_poc[0]= poc; | |
2807 field_poc[1]= poc; | |
2808 } | |
2809 | |
2810 if(s->picture_structure != PICT_BOTTOM_FIELD) | |
2811 s->current_picture_ptr->field_poc[0]= field_poc[0]; | |
2812 if(s->picture_structure != PICT_TOP_FIELD) | |
2813 s->current_picture_ptr->field_poc[1]= field_poc[1]; | |
2814 if(s->picture_structure == PICT_FRAME) // FIXME field pix? | |
2815 s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]); | |
2816 | |
2817 return 0; | |
2818 } | |
2819 | |
2820 /** | |
2821 * decodes a slice header. | |
2822 * this will allso call MPV_common_init() and frame_start() as needed | |
2823 */ | |
2824 static int decode_slice_header(H264Context *h){ | |
2825 MpegEncContext * const s = &h->s; | |
2826 int first_mb_in_slice, pps_id; | |
2827 int num_ref_idx_active_override_flag; | |
2828 static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE}; | |
2829 float new_aspect; | |
2830 | |
2831 s->current_picture.reference= h->nal_ref_idc != 0; | |
2832 | |
2833 first_mb_in_slice= get_ue_golomb(&s->gb); | |
2834 | |
2835 h->slice_type= get_ue_golomb(&s->gb); | |
2836 if(h->slice_type > 9){ | |
2837 fprintf(stderr, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y); | |
2838 } | |
2839 if(h->slice_type > 4){ | |
2840 h->slice_type -= 5; | |
2841 h->slice_type_fixed=1; | |
2842 }else | |
2843 h->slice_type_fixed=0; | |
2844 | |
2845 h->slice_type= slice_type_map[ h->slice_type ]; | |
2846 | |
2847 s->pict_type= h->slice_type; // to make a few old func happy, its wrong though | |
2848 | |
2849 pps_id= get_ue_golomb(&s->gb); | |
2850 if(pps_id>255){ | |
2851 fprintf(stderr, "pps_id out of range\n"); | |
2852 return -1; | |
2853 } | |
2854 h->pps= h->pps_buffer[pps_id]; | |
1174 | 2855 if(h->pps.slice_group_count == 0){ |
2856 fprintf(stderr, "non existing PPS referenced\n"); | |
2857 return -1; | |
2858 } | |
2859 | |
1168 | 2860 h->sps= h->sps_buffer[ h->pps.sps_id ]; |
1174 | 2861 if(h->sps.log2_max_frame_num == 0){ |
2862 fprintf(stderr, "non existing SPS referenced\n"); | |
2863 return -1; | |
2864 } | |
1168 | 2865 |
2866 s->mb_width= h->sps.mb_width; | |
2867 s->mb_height= h->sps.mb_height; | |
2868 | |
2869 h->b_stride= s->mb_width*4; | |
2870 h->b8_stride= s->mb_width*2; | |
2871 | |
2872 s->mb_x = first_mb_in_slice % s->mb_width; | |
2873 s->mb_y = first_mb_in_slice / s->mb_width; //FIXME AFFW | |
2874 | |
2875 s->width = 16*s->mb_width - 2*(h->pps.crop_left + h->pps.crop_right ); | |
2876 if(h->sps.frame_mbs_only_flag) | |
2877 s->height= 16*s->mb_height - 2*(h->pps.crop_top + h->pps.crop_bottom); | |
2878 else | |
2879 s->height= 16*s->mb_height - 4*(h->pps.crop_top + h->pps.crop_bottom); //FIXME recheck | |
2880 | |
2881 if(h->pps.crop_left || h->pps.crop_top){ | |
2882 fprintf(stderr, "insane croping not completly supported, this could look slightly wrong ...\n"); | |
2883 } | |
2884 | |
2885 if(s->aspected_height) //FIXME emms at end of slice ? | |
2886 new_aspect= h->sps.sar_width*s->width / (float)(s->height*h->sps.sar_height); | |
2887 else | |
2888 new_aspect=0; | |
2889 | |
2890 if (s->context_initialized | |
2891 && ( s->width != s->avctx->width || s->height != s->avctx->height | |
2892 || ABS(new_aspect - s->avctx->aspect_ratio) > 0.001)) { | |
2893 free_tables(h); | |
2894 MPV_common_end(s); | |
2895 } | |
2896 if (!s->context_initialized) { | |
2897 if (MPV_common_init(s) < 0) | |
2898 return -1; | |
2899 | |
2900 alloc_tables(h); | |
2901 | |
2902 s->avctx->width = s->width; | |
2903 s->avctx->height = s->height; | |
2904 s->avctx->aspect_ratio= new_aspect; | |
2905 } | |
2906 | |
2907 if(first_mb_in_slice == 0){ | |
2908 frame_start(h); | |
2909 } | |
2910 | |
1169 | 2911 s->current_picture_ptr->frame_num= //FIXME frame_num cleanup |
1168 | 2912 h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num); |
2913 | |
2914 if(h->sps.frame_mbs_only_flag){ | |
2915 s->picture_structure= PICT_FRAME; | |
2916 }else{ | |
2917 if(get_bits1(&s->gb)) //field_pic_flag | |
2918 s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag | |
2919 else | |
2920 s->picture_structure= PICT_FRAME; | |
2921 } | |
2922 | |
2923 if(s->picture_structure==PICT_FRAME){ | |
2924 h->curr_pic_num= h->frame_num; | |
2925 h->max_pic_num= 1<< h->sps.log2_max_frame_num; | |
2926 }else{ | |
2927 h->curr_pic_num= 2*h->frame_num; | |
2928 h->max_pic_num= 1<<(h->sps.log2_max_frame_num + 1); | |
2929 } | |
2930 | |
2931 if(h->nal_unit_type == NAL_IDR_SLICE){ | |
2932 int idr_pic_id= get_ue_golomb(&s->gb); | |
2933 } | |
2934 | |
2935 if(h->sps.poc_type==0){ | |
2936 h->poc_lsb= get_bits(&s->gb, h->sps.log2_max_poc_lsb); | |
2937 | |
2938 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME){ | |
2939 h->delta_poc_bottom= get_se_golomb(&s->gb); | |
2940 } | |
2941 } | |
2942 | |
2943 if(h->sps.poc_type==1 && !h->sps.delta_pic_order_always_zero_flag){ | |
2944 h->delta_poc[0]= get_se_golomb(&s->gb); | |
2945 | |
2946 if(h->pps.pic_order_present==1 && s->picture_structure==PICT_FRAME) | |
2947 h->delta_poc[1]= get_se_golomb(&s->gb); | |
2948 } | |
2949 | |
2950 init_poc(h); | |
2951 | |
2952 if(h->pps.redundant_pic_cnt_present){ | |
2953 h->redundant_pic_count= get_ue_golomb(&s->gb); | |
2954 } | |
2955 | |
2956 //set defaults, might be overriden a few line later | |
2957 h->ref_count[0]= h->pps.ref_count[0]; | |
2958 h->ref_count[1]= h->pps.ref_count[1]; | |
2959 | |
2960 if(h->slice_type == P_TYPE || h->slice_type == SP_TYPE || h->slice_type == B_TYPE){ | |
2961 if(h->slice_type == B_TYPE){ | |
2962 h->direct_spatial_mv_pred= get_bits1(&s->gb); | |
2963 } | |
2964 num_ref_idx_active_override_flag= get_bits1(&s->gb); | |
2965 | |
2966 if(num_ref_idx_active_override_flag){ | |
2967 h->ref_count[0]= get_ue_golomb(&s->gb) + 1; | |
2968 if(h->slice_type==B_TYPE) | |
2969 h->ref_count[1]= get_ue_golomb(&s->gb) + 1; | |
2970 | |
2971 if(h->ref_count[0] > 32 || h->ref_count[1] > 32){ | |
2972 fprintf(stderr, "reference overflow\n"); | |
2973 return -1; | |
2974 } | |
2975 } | |
2976 } | |
2977 | |
2978 if(first_mb_in_slice == 0){ | |
2979 fill_default_ref_list(h); | |
2980 } | |
2981 | |
2982 decode_ref_pic_list_reordering(h); | |
2983 | |
2984 if( (h->pps.weighted_pred && (h->slice_type == P_TYPE || h->slice_type == SP_TYPE )) | |
2985 || (h->pps.weighted_bipred_idc==1 && h->slice_type==B_TYPE ) ) | |
2986 pred_weight_table(h); | |
2987 | |
2988 if(s->current_picture.reference) | |
2989 decode_ref_pic_marking(h); | |
2990 //FIXME CABAC stuff | |
2991 | |
2992 s->qscale = h->pps.init_qp + get_se_golomb(&s->gb); //slice_qp_delta | |
2993 //FIXME qscale / qp ... stuff | |
2994 if(h->slice_type == SP_TYPE){ | |
2995 int sp_for_switch_flag= get_bits1(&s->gb); | |
2996 } | |
2997 if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){ | |
2998 int slice_qs_delta= get_se_golomb(&s->gb); | |
2999 } | |
3000 | |
3001 if( h->pps.deblocking_filter_parameters_present ) { | |
3002 h->disable_deblocking_filter_idc= get_ue_golomb(&s->gb); | |
3003 if( h->disable_deblocking_filter_idc != 1 ) { | |
3004 h->slice_alpha_c0_offset_div2= get_se_golomb(&s->gb); | |
3005 h->slice_beta_offset_div2= get_se_golomb(&s->gb); | |
3006 } | |
3007 }else | |
3008 h->disable_deblocking_filter_idc= 0; | |
3009 | |
3010 #if 0 //FMO | |
3011 if( h->pps.num_slice_groups > 1 && h->pps.mb_slice_group_map_type >= 3 && h->pps.mb_slice_group_map_type <= 5) | |
3012 slice_group_change_cycle= get_bits(&s->gb, ?); | |
3013 #endif | |
3014 | |
3015 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
3016 printf("mb:%d %c pps:%d frame:%d poc:%d/%d ref:%d/%d qp:%d loop:%d\n", | |
3017 first_mb_in_slice, | |
1264 | 3018 av_get_pict_type_char(h->slice_type), |
1168 | 3019 pps_id, h->frame_num, |
3020 s->current_picture_ptr->field_poc[0], s->current_picture_ptr->field_poc[1], | |
3021 h->ref_count[0], h->ref_count[1], | |
3022 s->qscale, | |
3023 h->disable_deblocking_filter_idc | |
3024 ); | |
3025 } | |
3026 | |
3027 return 0; | |
3028 } | |
3029 | |
3030 /** | |
3031 * | |
3032 */ | |
3033 static inline int get_level_prefix(GetBitContext *gb){ | |
3034 unsigned int buf; | |
3035 int log; | |
3036 | |
3037 OPEN_READER(re, gb); | |
3038 UPDATE_CACHE(re, gb); | |
3039 buf=GET_CACHE(re, gb); | |
3040 | |
3041 log= 32 - av_log2(buf); | |
3042 #ifdef TRACE | |
3043 print_bin(buf>>(32-log), log); | |
3044 printf("%5d %2d %3d lpr @%5d in %s get_level_prefix\n", buf>>(32-log), log, log-1, get_bits_count(gb), __FILE__); | |
3045 #endif | |
3046 | |
3047 LAST_SKIP_BITS(re, gb, log); | |
3048 CLOSE_READER(re, gb); | |
3049 | |
3050 return log-1; | |
3051 } | |
3052 | |
3053 /** | |
3054 * decodes a residual block. | |
3055 * @param n block index | |
3056 * @param scantable scantable | |
3057 * @param max_coeff number of coefficients in the block | |
3058 * @return <0 if an error occured | |
3059 */ | |
3060 static int decode_residual(H264Context *h, GetBitContext *gb, DCTELEM *block, int n, const uint8_t *scantable, int qp, int max_coeff){ | |
3061 MpegEncContext * const s = &h->s; | |
3062 const uint16_t *qmul= dequant_coeff[qp]; | |
3063 static const int coeff_token_table_index[17]= {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3}; | |
3064 int level[16], run[16]; | |
3065 int suffix_length, zeros_left, coeff_num, coeff_token, total_coeff, i, trailing_ones; | |
3066 | |
3067 //FIXME put trailing_onex into the context | |
3068 | |
3069 if(n == CHROMA_DC_BLOCK_INDEX){ | |
3070 coeff_token= get_vlc2(gb, chroma_dc_coeff_token_vlc.table, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 1); | |
3071 total_coeff= coeff_token>>2; | |
3072 }else{ | |
3073 if(n == LUMA_DC_BLOCK_INDEX){ | |
3074 total_coeff= pred_non_zero_count(h, 0); | |
3075 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2); | |
3076 total_coeff= coeff_token>>2; | |
3077 }else{ | |
3078 total_coeff= pred_non_zero_count(h, n); | |
3079 coeff_token= get_vlc2(gb, coeff_token_vlc[ coeff_token_table_index[total_coeff] ].table, COEFF_TOKEN_VLC_BITS, 2); | |
3080 total_coeff= coeff_token>>2; | |
3081 h->non_zero_count_cache[ scan8[n] ]= total_coeff; | |
3082 } | |
3083 } | |
3084 | |
3085 //FIXME set last_non_zero? | |
3086 | |
3087 if(total_coeff==0) | |
3088 return 0; | |
3089 | |
3090 trailing_ones= coeff_token&3; | |
1170 | 3091 tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff); |
1168 | 3092 assert(total_coeff<=16); |
3093 | |
3094 for(i=0; i<trailing_ones; i++){ | |
3095 level[i]= 1 - 2*get_bits1(gb); | |
3096 } | |
3097 | |
3098 suffix_length= total_coeff > 10 && trailing_ones < 3; | |
3099 | |
3100 for(; i<total_coeff; i++){ | |
3101 const int prefix= get_level_prefix(gb); | |
3102 int level_code, mask; | |
3103 | |
3104 if(prefix<14){ //FIXME try to build a large unified VLC table for all this | |
3105 if(suffix_length) | |
3106 level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part | |
3107 else | |
3108 level_code= (prefix<<suffix_length); //part | |
3109 }else if(prefix==14){ | |
3110 if(suffix_length) | |
3111 level_code= (prefix<<suffix_length) + get_bits(gb, suffix_length); //part | |
3112 else | |
3113 level_code= prefix + get_bits(gb, 4); //part | |
3114 }else if(prefix==15){ | |
3115 level_code= (prefix<<suffix_length) + get_bits(gb, 12); //part | |
3116 if(suffix_length==0) level_code+=15; //FIXME doesnt make (much)sense | |
3117 }else{ | |
3118 fprintf(stderr, "prefix too large at %d %d\n", s->mb_x, s->mb_y); | |
3119 return -1; | |
3120 } | |
3121 | |
3122 if(i==trailing_ones && i<3) level_code+= 2; //FIXME split first iteration | |
3123 | |
3124 mask= -(level_code&1); | |
3125 level[i]= (((2+level_code)>>1) ^ mask) - mask; | |
3126 | |
3127 if(suffix_length==0) suffix_length=1; //FIXME split first iteration | |
3128 | |
3129 #if 1 | |
3130 if(ABS(level[i]) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++; | |
3131 #else | |
3132 if((2+level_code)>>1) > (3<<(suffix_length-1)) && suffix_length<6) suffix_length++; | |
3133 ? == prefix > 2 or sth | |
3134 #endif | |
1170 | 3135 tprintf("level: %d suffix_length:%d\n", level[i], suffix_length); |
1168 | 3136 } |
3137 | |
3138 if(total_coeff == max_coeff) | |
3139 zeros_left=0; | |
3140 else{ | |
3141 if(n == CHROMA_DC_BLOCK_INDEX) | |
3142 zeros_left= get_vlc2(gb, chroma_dc_total_zeros_vlc[ total_coeff-1 ].table, CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 1); | |
3143 else | |
3144 zeros_left= get_vlc2(gb, total_zeros_vlc[ total_coeff-1 ].table, TOTAL_ZEROS_VLC_BITS, 1); | |
3145 } | |
3146 | |
3147 for(i=0; i<total_coeff-1; i++){ | |
3148 if(zeros_left <=0) | |
3149 break; | |
3150 else if(zeros_left < 7){ | |
3151 run[i]= get_vlc2(gb, run_vlc[zeros_left-1].table, RUN_VLC_BITS, 1); | |
3152 }else{ | |
3153 run[i]= get_vlc2(gb, run7_vlc.table, RUN7_VLC_BITS, 2); | |
3154 } | |
3155 zeros_left -= run[i]; | |
3156 } | |
3157 | |
3158 if(zeros_left<0){ | |
3159 fprintf(stderr, "negative number of zero coeffs at %d %d\n", s->mb_x, s->mb_y); | |
3160 return -1; | |
3161 } | |
3162 | |
3163 for(; i<total_coeff-1; i++){ | |
3164 run[i]= 0; | |
3165 } | |
3166 | |
3167 run[i]= zeros_left; | |
3168 | |
3169 coeff_num=-1; | |
3170 if(n > 24){ | |
3171 for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode? | |
3172 int j; | |
3173 | |
3174 coeff_num += run[i] + 1; //FIXME add 1 earlier ? | |
3175 j= scantable[ coeff_num ]; | |
3176 | |
3177 block[j]= level[i]; | |
3178 } | |
3179 }else{ | |
3180 for(i=total_coeff-1; i>=0; i--){ //FIXME merge into rundecode? | |
3181 int j; | |
3182 | |
3183 coeff_num += run[i] + 1; //FIXME add 1 earlier ? | |
3184 j= scantable[ coeff_num ]; | |
3185 | |
3186 block[j]= level[i] * qmul[j]; | |
3187 // printf("%d %d ", block[j], qmul[j]); | |
3188 } | |
3189 } | |
3190 return 0; | |
3191 } | |
3192 | |
3193 /** | |
3194 * decodes a macroblock | |
3195 * @returns 0 if ok, AC_ERROR / DC_ERROR / MV_ERROR if an error is noticed | |
3196 */ | |
3197 static int decode_mb(H264Context *h){ | |
3198 MpegEncContext * const s = &h->s; | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
3199 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; |
1169 | 3200 int mb_type, partition_count, cbp; |
1168 | 3201 |
1252 | 3202 s->dsp.clear_blocks(h->mb); //FIXME avoid if allready clear (move after skip handlong? |
1168 | 3203 |
1170 | 3204 tprintf("pic:%d mb:%d/%d\n", h->frame_num, s->mb_x, s->mb_y); |
1168 | 3205 |
3206 if(h->slice_type != I_TYPE && h->slice_type != SI_TYPE){ | |
3207 if(s->mb_skip_run==-1) | |
3208 s->mb_skip_run= get_ue_golomb(&s->gb); | |
3209 | |
3210 if (s->mb_skip_run--) { | |
1187 | 3211 int mx, my; |
1168 | 3212 /* skip mb */ |
3213 //FIXME b frame | |
3214 mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0; | |
3215 | |
3216 memset(h->non_zero_count[mb_xy], 0, 16); | |
3217 memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui | |
3218 | |
3219 if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){ | |
3220 h->mb_field_decoding_flag= get_bits1(&s->gb); | |
3221 } | |
3222 | |
3223 if(h->mb_field_decoding_flag) | |
3224 mb_type|= MB_TYPE_INTERLACED; | |
3225 | |
3226 fill_caches(h, mb_type); //FIXME check what is needed and what not ... | |
3227 pred_pskip_motion(h, &mx, &my); | |
3228 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1); | |
3229 fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, (mx&0xFFFF)+(my<<16), 4); | |
3230 write_back_motion(h, mb_type); | |
3231 | |
3232 s->current_picture.mb_type[mb_xy]= mb_type; //FIXME SKIP type | |
3233 h->slice_table[ mb_xy ]= h->slice_num; | |
3234 | |
3235 h->prev_mb_skiped= 1; | |
3236 return 0; | |
3237 } | |
3238 } | |
3239 if(h->sps.mb_aff /* && !field pic FIXME needed? */){ | |
3240 if((s->mb_y&1)==0) | |
3241 h->mb_field_decoding_flag = get_bits1(&s->gb); | |
3242 }else | |
3243 h->mb_field_decoding_flag=0; //FIXME som ed note ?! | |
3244 | |
3245 h->prev_mb_skiped= 0; | |
3246 | |
3247 mb_type= get_ue_golomb(&s->gb); | |
3248 if(h->slice_type == B_TYPE){ | |
3249 if(mb_type < 23){ | |
3250 partition_count= b_mb_type_info[mb_type].partition_count; | |
3251 mb_type= b_mb_type_info[mb_type].type; | |
3252 }else{ | |
3253 mb_type -= 23; | |
3254 goto decode_intra_mb; | |
3255 } | |
3256 }else if(h->slice_type == P_TYPE /*|| h->slice_type == SP_TYPE */){ | |
3257 if(mb_type < 5){ | |
3258 partition_count= p_mb_type_info[mb_type].partition_count; | |
3259 mb_type= p_mb_type_info[mb_type].type; | |
3260 }else{ | |
3261 mb_type -= 5; | |
3262 goto decode_intra_mb; | |
3263 } | |
3264 }else{ | |
3265 assert(h->slice_type == I_TYPE); | |
3266 decode_intra_mb: | |
3267 if(mb_type > 25){ | |
1264 | 3268 fprintf(stderr, "mb_type %d in %c slice to large at %d %d\n", mb_type, av_get_pict_type_char(h->slice_type), s->mb_x, s->mb_y); |
1168 | 3269 return -1; |
3270 } | |
3271 partition_count=0; | |
3272 cbp= i_mb_type_info[mb_type].cbp; | |
3273 h->intra16x16_pred_mode= i_mb_type_info[mb_type].pred_mode; | |
3274 mb_type= i_mb_type_info[mb_type].type; | |
3275 } | |
3276 | |
3277 if(h->mb_field_decoding_flag) | |
3278 mb_type |= MB_TYPE_INTERLACED; | |
3279 | |
3280 s->current_picture.mb_type[mb_xy]= mb_type; | |
3281 h->slice_table[ mb_xy ]= h->slice_num; | |
3282 | |
3283 if(IS_INTRA_PCM(mb_type)){ | |
3284 const uint8_t *ptr; | |
1187 | 3285 int x, y; |
1168 | 3286 |
3287 // we assume these blocks are very rare so we dont optimize it | |
3288 align_get_bits(&s->gb); | |
3289 | |
3290 ptr= s->gb.buffer + get_bits_count(&s->gb); | |
3291 | |
3292 for(y=0; y<16; y++){ | |
3293 const int index= 4*(y&3) + 64*(y>>2); | |
3294 for(x=0; x<16; x++){ | |
3295 h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++); | |
3296 } | |
3297 } | |
3298 for(y=0; y<8; y++){ | |
3299 const int index= 256 + 4*(y&3) + 32*(y>>2); | |
3300 for(x=0; x<8; x++){ | |
3301 h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++); | |
3302 } | |
3303 } | |
3304 for(y=0; y<8; y++){ | |
3305 const int index= 256 + 64 + 4*(y&3) + 32*(y>>2); | |
3306 for(x=0; x<8; x++){ | |
3307 h->mb[index + (x&3) + 16*(x>>2)]= *(ptr++); | |
3308 } | |
3309 } | |
3310 | |
3311 skip_bits(&s->gb, 384); //FIXME check /fix the bitstream readers | |
3312 | |
3313 memset(h->non_zero_count[mb_xy], 16, 16); | |
3314 | |
3315 return 0; | |
3316 } | |
3317 | |
3318 fill_caches(h, mb_type); | |
3319 | |
3320 //mb_pred | |
3321 if(IS_INTRA(mb_type)){ | |
3322 // init_top_left_availability(h); | |
3323 if(IS_INTRA4x4(mb_type)){ | |
3324 int i; | |
3325 | |
3326 // fill_intra4x4_pred_table(h); | |
3327 for(i=0; i<16; i++){ | |
3328 const int mode_coded= !get_bits1(&s->gb); | |
3329 const int predicted_mode= pred_intra_mode(h, i); | |
3330 int mode; | |
3331 | |
3332 if(mode_coded){ | |
3333 const int rem_mode= get_bits(&s->gb, 3); | |
3334 if(rem_mode<predicted_mode) | |
3335 mode= rem_mode; | |
3336 else | |
3337 mode= rem_mode + 1; | |
3338 }else{ | |
3339 mode= predicted_mode; | |
3340 } | |
3341 | |
3342 h->intra4x4_pred_mode_cache[ scan8[i] ] = mode; | |
3343 } | |
3344 write_back_intra_pred_mode(h); | |
3345 if( check_intra4x4_pred_mode(h) < 0) | |
3346 return -1; | |
3347 }else{ | |
3348 h->intra16x16_pred_mode= check_intra_pred_mode(h, h->intra16x16_pred_mode); | |
3349 if(h->intra16x16_pred_mode < 0) | |
3350 return -1; | |
3351 } | |
3352 h->chroma_pred_mode= get_ue_golomb(&s->gb); | |
3353 | |
3354 h->chroma_pred_mode= check_intra_pred_mode(h, h->chroma_pred_mode); | |
3355 if(h->chroma_pred_mode < 0) | |
3356 return -1; | |
3357 }else if(partition_count==4){ | |
3358 int i, j, sub_partition_count[4], list, ref[2][4]; | |
3359 | |
3360 if(h->slice_type == B_TYPE){ | |
3361 for(i=0; i<4; i++){ | |
3362 h->sub_mb_type[i]= get_ue_golomb(&s->gb); | |
3363 if(h->sub_mb_type[i] >=13){ | |
3364 fprintf(stderr, "B sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y); | |
3365 return -1; | |
3366 } | |
3367 sub_partition_count[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count; | |
3368 h->sub_mb_type[i]= b_sub_mb_type_info[ h->sub_mb_type[i] ].type; | |
3369 } | |
3370 }else{ | |
3371 assert(h->slice_type == P_TYPE || h->slice_type == SP_TYPE); //FIXME SP correct ? | |
3372 for(i=0; i<4; i++){ | |
3373 h->sub_mb_type[i]= get_ue_golomb(&s->gb); | |
3374 if(h->sub_mb_type[i] >=4){ | |
3375 fprintf(stderr, "P sub_mb_type %d out of range at %d %d\n", h->sub_mb_type[i], s->mb_x, s->mb_y); | |
3376 return -1; | |
3377 } | |
3378 sub_partition_count[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].partition_count; | |
3379 h->sub_mb_type[i]= p_sub_mb_type_info[ h->sub_mb_type[i] ].type; | |
3380 } | |
3381 } | |
3382 | |
3383 for(list=0; list<2; list++){ | |
3384 const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list]; | |
3385 if(ref_count == 0) continue; | |
3386 for(i=0; i<4; i++){ | |
3387 if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){ | |
3388 ref[list][i] = get_te0_golomb(&s->gb, ref_count); //FIXME init to 0 before and skip? | |
3389 }else{ | |
3390 //FIXME | |
3391 ref[list][i] = -1; | |
3392 } | |
3393 } | |
3394 } | |
3395 | |
3396 for(list=0; list<2; list++){ | |
3397 const int ref_count= IS_REF0(mb_type) ? 1 : h->ref_count[list]; | |
3398 if(ref_count == 0) continue; | |
3399 | |
3400 for(i=0; i<4; i++){ | |
3401 h->ref_cache[list][ scan8[4*i] ]=h->ref_cache[list][ scan8[4*i]+1 ]= | |
3402 h->ref_cache[list][ scan8[4*i]+8 ]=h->ref_cache[list][ scan8[4*i]+9 ]= ref[list][i]; | |
3403 | |
3404 if(IS_DIR(h->sub_mb_type[i], 0, list) && !IS_DIRECT(h->sub_mb_type[i])){ | |
3405 const int sub_mb_type= h->sub_mb_type[i]; | |
3406 const int block_width= (sub_mb_type & (MB_TYPE_16x16|MB_TYPE_16x8)) ? 2 : 1; | |
3407 for(j=0; j<sub_partition_count[i]; j++){ | |
3408 int mx, my; | |
3409 const int index= 4*i + block_width*j; | |
3410 int16_t (* mv_cache)[2]= &h->mv_cache[list][ scan8[index] ]; | |
3411 pred_motion(h, index, block_width, list, h->ref_cache[list][ scan8[index] ], &mx, &my); | |
3412 mx += get_se_golomb(&s->gb); | |
3413 my += get_se_golomb(&s->gb); | |
1170 | 3414 tprintf("final mv:%d %d\n", mx, my); |
3415 | |
1168 | 3416 if(IS_SUB_8X8(sub_mb_type)){ |
3417 mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= | |
3418 mv_cache[ 8 ][0]= mv_cache[ 9 ][0]= mx; | |
3419 mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= | |
3420 mv_cache[ 8 ][1]= mv_cache[ 9 ][1]= my; | |
3421 }else if(IS_SUB_8X4(sub_mb_type)){ | |
3422 mv_cache[ 0 ][0]= mv_cache[ 1 ][0]= mx; | |
3423 mv_cache[ 0 ][1]= mv_cache[ 1 ][1]= my; | |
3424 }else if(IS_SUB_4X8(sub_mb_type)){ | |
3425 mv_cache[ 0 ][0]= mv_cache[ 8 ][0]= mx; | |
3426 mv_cache[ 0 ][1]= mv_cache[ 8 ][1]= my; | |
3427 }else{ | |
3428 assert(IS_SUB_4X4(sub_mb_type)); | |
3429 mv_cache[ 0 ][0]= mx; | |
3430 mv_cache[ 0 ][1]= my; | |
3431 } | |
3432 } | |
3433 }else{ | |
3434 uint32_t *p= (uint32_t *)&h->mv_cache[list][ scan8[4*i] ][0]; | |
3435 p[0] = p[1]= | |
3436 p[8] = p[9]= 0; | |
3437 } | |
3438 } | |
3439 } | |
3440 }else if(!IS_DIRECT(mb_type)){ | |
3441 int list, mx, my, i; | |
3442 //FIXME we should set ref_idx_l? to 0 if we use that later ... | |
3443 if(IS_16X16(mb_type)){ | |
3444 for(list=0; list<2; list++){ | |
3445 if(h->ref_count[0]>0){ | |
3446 if(IS_DIR(mb_type, 0, list)){ | |
3447 const int val= get_te0_golomb(&s->gb, h->ref_count[list]); | |
3448 fill_rectangle(&h->ref_cache[list][ scan8[0] ], 4, 4, 8, val, 1); | |
3449 } | |
3450 } | |
3451 } | |
3452 for(list=0; list<2; list++){ | |
3453 if(IS_DIR(mb_type, 0, list)){ | |
3454 pred_motion(h, 0, 4, list, h->ref_cache[list][ scan8[0] ], &mx, &my); | |
3455 mx += get_se_golomb(&s->gb); | |
3456 my += get_se_golomb(&s->gb); | |
1170 | 3457 tprintf("final mv:%d %d\n", mx, my); |
3458 | |
1168 | 3459 fill_rectangle(h->mv_cache[list][ scan8[0] ], 4, 4, 8, (mx&0xFFFF) + (my<<16), 4); |
3460 } | |
3461 } | |
3462 } | |
3463 else if(IS_16X8(mb_type)){ | |
3464 for(list=0; list<2; list++){ | |
3465 if(h->ref_count[list]>0){ | |
3466 for(i=0; i<2; i++){ | |
3467 if(IS_DIR(mb_type, i, list)){ | |
3468 const int val= get_te0_golomb(&s->gb, h->ref_count[list]); | |
3469 fill_rectangle(&h->ref_cache[list][ scan8[0] + 16*i ], 4, 2, 8, val, 1); | |
3470 } | |
3471 } | |
3472 } | |
3473 } | |
3474 for(list=0; list<2; list++){ | |
3475 for(i=0; i<2; i++){ | |
3476 if(IS_DIR(mb_type, i, list)){ | |
3477 pred_16x8_motion(h, 8*i, list, h->ref_cache[list][scan8[0] + 16*i], &mx, &my); | |
3478 mx += get_se_golomb(&s->gb); | |
3479 my += get_se_golomb(&s->gb); | |
1170 | 3480 tprintf("final mv:%d %d\n", mx, my); |
3481 | |
1168 | 3482 fill_rectangle(h->mv_cache[list][ scan8[0] + 16*i ], 4, 2, 8, (mx&0xFFFF) + (my<<16), 4); |
3483 } | |
3484 } | |
3485 } | |
3486 }else{ | |
3487 assert(IS_8X16(mb_type)); | |
3488 for(list=0; list<2; list++){ | |
3489 if(h->ref_count[list]>0){ | |
3490 for(i=0; i<2; i++){ | |
3491 if(IS_DIR(mb_type, i, list)){ //FIXME optimize | |
3492 const int val= get_te0_golomb(&s->gb, h->ref_count[list]); | |
3493 fill_rectangle(&h->ref_cache[list][ scan8[0] + 2*i ], 2, 4, 8, val, 1); | |
3494 } | |
3495 } | |
3496 } | |
3497 } | |
3498 for(list=0; list<2; list++){ | |
3499 for(i=0; i<2; i++){ | |
3500 if(IS_DIR(mb_type, i, list)){ | |
3501 pred_8x16_motion(h, i*4, list, h->ref_cache[list][ scan8[0] + 2*i ], &mx, &my); | |
3502 mx += get_se_golomb(&s->gb); | |
3503 my += get_se_golomb(&s->gb); | |
1170 | 3504 tprintf("final mv:%d %d\n", mx, my); |
3505 | |
1168 | 3506 fill_rectangle(h->mv_cache[list][ scan8[0] + 2*i ], 2, 4, 8, (mx&0xFFFF) + (my<<16), 4); |
3507 } | |
3508 } | |
3509 } | |
3510 } | |
3511 } | |
3512 | |
3513 if(IS_INTER(mb_type)) | |
3514 write_back_motion(h, mb_type); | |
3515 | |
3516 if(!IS_INTRA16x16(mb_type)){ | |
3517 cbp= get_ue_golomb(&s->gb); | |
3518 if(cbp > 47){ | |
3519 fprintf(stderr, "cbp too large (%d) at %d %d\n", cbp, s->mb_x, s->mb_y); | |
3520 return -1; | |
3521 } | |
3522 | |
3523 if(IS_INTRA4x4(mb_type)) | |
3524 cbp= golomb_to_intra4x4_cbp[cbp]; | |
3525 else | |
3526 cbp= golomb_to_inter_cbp[cbp]; | |
3527 } | |
3528 | |
3529 if(cbp || IS_INTRA16x16(mb_type)){ | |
3530 int i8x8, i4x4, chroma_idx; | |
3531 int chroma_qp, dquant; | |
3532 GetBitContext *gb= IS_INTRA(mb_type) ? h->intra_gb_ptr : h->inter_gb_ptr; | |
3533 const uint8_t *scan, *dc_scan; | |
3534 | |
3535 // fill_non_zero_count_cache(h); | |
3536 | |
3537 if(IS_INTERLACED(mb_type)){ | |
3538 scan= field_scan; | |
3539 dc_scan= luma_dc_field_scan; | |
3540 }else{ | |
3541 scan= zigzag_scan; | |
3542 dc_scan= luma_dc_zigzag_scan; | |
3543 } | |
3544 | |
3545 dquant= get_se_golomb(&s->gb); | |
3546 | |
3547 if( dquant > 25 || dquant < -26 ){ | |
3548 fprintf(stderr, "dquant out of range (%d) at %d %d\n", dquant, s->mb_x, s->mb_y); | |
3549 return -1; | |
3550 } | |
3551 | |
3552 s->qscale += dquant; | |
3553 if(((unsigned)s->qscale) > 51){ | |
3554 if(s->qscale<0) s->qscale+= 52; | |
3555 else s->qscale-= 52; | |
3556 } | |
3557 | |
3558 h->chroma_qp= chroma_qp= get_chroma_qp(h, s->qscale); | |
3559 if(IS_INTRA16x16(mb_type)){ | |
3560 if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){ | |
3561 return -1; //FIXME continue if partotioned and other retirn -1 too | |
3562 } | |
3563 | |
3564 assert((cbp&15) == 0 || (cbp&15) == 15); | |
3565 | |
3566 if(cbp&15){ | |
3567 for(i8x8=0; i8x8<4; i8x8++){ | |
3568 for(i4x4=0; i4x4<4; i4x4++){ | |
3569 const int index= i4x4 + 4*i8x8; | |
3570 if( decode_residual(h, h->intra_gb_ptr, h->mb + 16*index, index, scan + 1, s->qscale, 15) < 0 ){ | |
3571 return -1; | |
3572 } | |
3573 } | |
3574 } | |
3575 }else{ | |
3576 memset(&h->non_zero_count_cache[8], 0, 8*4); //FIXME stupid & slow | |
3577 } | |
3578 }else{ | |
3579 for(i8x8=0; i8x8<4; i8x8++){ | |
3580 if(cbp & (1<<i8x8)){ | |
3581 for(i4x4=0; i4x4<4; i4x4++){ | |
3582 const int index= i4x4 + 4*i8x8; | |
3583 | |
3584 if( decode_residual(h, gb, h->mb + 16*index, index, scan, s->qscale, 16) <0 ){ | |
3585 return -1; | |
3586 } | |
3587 } | |
3588 }else{ | |
3589 uint8_t * const nnz= &h->non_zero_count_cache[ scan8[4*i8x8] ]; | |
3590 nnz[0] = nnz[1] = nnz[8] = nnz[9] = 0; | |
3591 } | |
3592 } | |
3593 } | |
3594 | |
3595 if(cbp&0x30){ | |
3596 for(chroma_idx=0; chroma_idx<2; chroma_idx++) | |
3597 if( decode_residual(h, gb, h->mb + 256 + 16*4*chroma_idx, CHROMA_DC_BLOCK_INDEX, chroma_dc_scan, chroma_qp, 4) < 0){ | |
3598 return -1; | |
3599 } | |
3600 } | |
3601 | |
3602 if(cbp&0x20){ | |
3603 for(chroma_idx=0; chroma_idx<2; chroma_idx++){ | |
3604 for(i4x4=0; i4x4<4; i4x4++){ | |
3605 const int index= 16 + 4*chroma_idx + i4x4; | |
3606 if( decode_residual(h, gb, h->mb + 16*index, index, scan + 1, chroma_qp, 15) < 0){ | |
3607 return -1; | |
3608 } | |
3609 } | |
3610 } | |
3611 }else{ | |
3612 uint8_t * const nnz= &h->non_zero_count_cache[0]; | |
3613 nnz[ scan8[16]+0 ] = nnz[ scan8[16]+1 ] =nnz[ scan8[16]+8 ] =nnz[ scan8[16]+9 ] = | |
3614 nnz[ scan8[20]+0 ] = nnz[ scan8[20]+1 ] =nnz[ scan8[20]+8 ] =nnz[ scan8[20]+9 ] = 0; | |
3615 } | |
3616 }else{ | |
3617 memset(&h->non_zero_count_cache[8], 0, 8*5); | |
3618 } | |
3619 write_back_non_zero_count(h); | |
3620 | |
3621 return 0; | |
3622 } | |
3623 | |
3624 static int decode_slice(H264Context *h){ | |
3625 MpegEncContext * const s = &h->s; | |
3626 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F; | |
3627 | |
3628 s->mb_skip_run= -1; | |
3629 | |
3630 #if 1 | |
3631 for(;;){ | |
3632 int ret= decode_mb(h); | |
3633 | |
3634 hl_decode_mb(h); | |
3635 | |
3636 if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ? | |
3637 s->mb_y++; | |
3638 ret= decode_mb(h); | |
3639 | |
3640 hl_decode_mb(h); | |
3641 s->mb_y--; | |
3642 } | |
3643 | |
3644 if(ret<0){ | |
3645 fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y); | |
3646 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask); | |
3647 | |
3648 return -1; | |
3649 } | |
3650 | |
3651 if(++s->mb_x >= s->mb_width){ | |
3652 s->mb_x=0; | |
3653 ff_draw_horiz_band(s, 16*s->mb_y, 16); | |
3654 if(++s->mb_y >= s->mb_height){ | |
1170 | 3655 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits); |
3656 | |
1168 | 3657 if(get_bits_count(&s->gb) == s->gb.size_in_bits){ |
3658 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); | |
3659 | |
3660 return 0; | |
3661 }else{ | |
3662 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); | |
3663 | |
3664 return -1; | |
3665 } | |
3666 } | |
3667 } | |
3668 | |
3669 if(get_bits_count(&s->gb) >= s->gb.size_in_bits && s->mb_skip_run<=0){ | |
3670 if(get_bits_count(&s->gb) == s->gb.size_in_bits){ | |
3671 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); | |
3672 | |
3673 return 0; | |
3674 }else{ | |
3675 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask); | |
3676 | |
3677 return -1; | |
3678 } | |
3679 } | |
3680 } | |
3681 #endif | |
3682 #if 0 | |
3683 for(;s->mb_y < s->mb_height; s->mb_y++){ | |
3684 for(;s->mb_x < s->mb_width; s->mb_x++){ | |
3685 int ret= decode_mb(h); | |
3686 | |
3687 hl_decode_mb(h); | |
3688 | |
3689 if(ret<0){ | |
3690 fprintf(stderr, "error while decoding MB %d %d\n", s->mb_x, s->mb_y); | |
3691 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask); | |
3692 | |
3693 return -1; | |
3694 } | |
3695 | |
3696 if(++s->mb_x >= s->mb_width){ | |
3697 s->mb_x=0; | |
3698 if(++s->mb_y >= s->mb_height){ | |
3699 if(get_bits_count(s->gb) == s->gb.size_in_bits){ | |
3700 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); | |
3701 | |
3702 return 0; | |
3703 }else{ | |
3704 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); | |
3705 | |
3706 return -1; | |
3707 } | |
3708 } | |
3709 } | |
3710 | |
3711 if(get_bits_count(s->?gb) >= s->gb?.size_in_bits){ | |
3712 if(get_bits_count(s->gb) == s->gb.size_in_bits){ | |
3713 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); | |
3714 | |
3715 return 0; | |
3716 }else{ | |
3717 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask); | |
3718 | |
3719 return -1; | |
3720 } | |
3721 } | |
3722 } | |
3723 s->mb_x=0; | |
3724 ff_draw_horiz_band(s, 16*s->mb_y, 16); | |
3725 } | |
3726 #endif | |
3727 return -1; //not reached | |
3728 } | |
3729 | |
3730 static inline int decode_vui_parameters(H264Context *h, SPS *sps){ | |
3731 MpegEncContext * const s = &h->s; | |
3732 int aspect_ratio_info_present_flag, aspect_ratio_idc; | |
3733 | |
3734 aspect_ratio_info_present_flag= get_bits1(&s->gb); | |
3735 | |
3736 if( aspect_ratio_info_present_flag ) { | |
3737 aspect_ratio_idc= get_bits(&s->gb, 8); | |
3738 if( aspect_ratio_idc == EXTENDED_SAR ) { | |
3739 sps->sar_width= get_bits(&s->gb, 16); | |
3740 sps->sar_height= get_bits(&s->gb, 16); | |
3741 }else if(aspect_ratio_idc < 16){ | |
3742 sps->sar_width= pixel_aspect[aspect_ratio_idc][0]; | |
3743 sps->sar_height= pixel_aspect[aspect_ratio_idc][1]; | |
3744 }else{ | |
3745 fprintf(stderr, "illegal aspect ratio\n"); | |
3746 return -1; | |
3747 } | |
3748 }else{ | |
3749 sps->sar_width= | |
3750 sps->sar_height= 0; | |
3751 } | |
3752 // s->avctx->aspect_ratio= sar_width*s->width / (float)(s->height*sar_height); | |
3753 #if 0 | |
3754 | overscan_info_present_flag |0 |u(1) | | |
3755 | if( overscan_info_present_flag ) | | | | |
3756 | overscan_appropriate_flag |0 |u(1) | | |
3757 | video_signal_type_present_flag |0 |u(1) | | |
3758 | if( video_signal_type_present_flag ) { | | | | |
3759 | video_format |0 |u(3) | | |
3760 | video_full_range_flag |0 |u(1) | | |
3761 | colour_description_present_flag |0 |u(1) | | |
3762 | if( colour_description_present_flag ) { | | | | |
3763 | colour_primaries |0 |u(8) | | |
3764 | transfer_characteristics |0 |u(8) | | |
3765 | matrix_coefficients |0 |u(8) | | |
3766 | } | | | | |
3767 | } | | | | |
3768 | chroma_location_info_present_flag |0 |u(1) | | |
3769 | if ( chroma_location_info_present_flag ) { | | | | |
3770 | chroma_sample_location_type_top_field |0 |ue(v) | | |
3771 | chroma_sample_location_type_bottom_field |0 |ue(v) | | |
3772 | } | | | | |
3773 | timing_info_present_flag |0 |u(1) | | |
3774 | if( timing_info_present_flag ) { | | | | |
3775 | num_units_in_tick |0 |u(32) | | |
3776 | time_scale |0 |u(32) | | |
3777 | fixed_frame_rate_flag |0 |u(1) | | |
3778 | } | | | | |
3779 | nal_hrd_parameters_present_flag |0 |u(1) | | |
3780 | if( nal_hrd_parameters_present_flag = = 1) | | | | |
3781 | hrd_parameters( ) | | | | |
3782 | vcl_hrd_parameters_present_flag |0 |u(1) | | |
3783 | if( vcl_hrd_parameters_present_flag = = 1) | | | | |
3784 | hrd_parameters( ) | | | | |
3785 | if( ( nal_hrd_parameters_present_flag = = 1 | || | | | |
3786 | | | | | |
3787 |( vcl_hrd_parameters_present_flag = = 1 ) ) | | | | |
3788 | low_delay_hrd_flag |0 |u(1) | | |
3789 | bitstream_restriction_flag |0 |u(1) | | |
3790 | if( bitstream_restriction_flag ) { |0 |u(1) | | |
3791 | motion_vectors_over_pic_boundaries_flag |0 |u(1) | | |
3792 | max_bytes_per_pic_denom |0 |ue(v) | | |
3793 | max_bits_per_mb_denom |0 |ue(v) | | |
3794 | log2_max_mv_length_horizontal |0 |ue(v) | | |
3795 | log2_max_mv_length_vertical |0 |ue(v) | | |
3796 | num_reorder_frames |0 |ue(v) | | |
3797 | max_dec_frame_buffering |0 |ue(v) | | |
3798 | } | | | | |
3799 |} | | | | |
3800 #endif | |
3801 return 0; | |
3802 } | |
3803 | |
3804 static inline int decode_seq_parameter_set(H264Context *h){ | |
3805 MpegEncContext * const s = &h->s; | |
3806 int profile_idc, level_idc, multiple_slice_groups, arbitrary_slice_order, redundant_slices; | |
3807 int sps_id, i; | |
3808 SPS *sps; | |
3809 | |
3810 profile_idc= get_bits(&s->gb, 8); | |
3811 level_idc= get_bits(&s->gb, 8); | |
3812 multiple_slice_groups= get_bits1(&s->gb); | |
3813 arbitrary_slice_order= get_bits1(&s->gb); | |
3814 redundant_slices= get_bits1(&s->gb); | |
3815 | |
3816 sps_id= get_ue_golomb(&s->gb); | |
3817 | |
3818 sps= &h->sps_buffer[ sps_id ]; | |
3819 | |
3820 sps->profile_idc= profile_idc; | |
3821 sps->level_idc= level_idc; | |
3822 sps->multiple_slice_groups= multiple_slice_groups; | |
3823 sps->arbitrary_slice_order= arbitrary_slice_order; | |
3824 sps->redundant_slices= redundant_slices; | |
3825 | |
3826 sps->log2_max_frame_num= get_ue_golomb(&s->gb) + 4; | |
3827 | |
3828 sps->poc_type= get_ue_golomb(&s->gb); | |
3829 | |
3830 if(sps->poc_type == 0){ //FIXME #define | |
3831 sps->log2_max_poc_lsb= get_ue_golomb(&s->gb) + 4; | |
3832 } else if(sps->poc_type == 1){//FIXME #define | |
3833 sps->delta_pic_order_always_zero_flag= get_bits1(&s->gb); | |
3834 sps->offset_for_non_ref_pic= get_se_golomb(&s->gb); | |
3835 sps->offset_for_top_to_bottom_field= get_se_golomb(&s->gb); | |
3836 sps->poc_cycle_length= get_ue_golomb(&s->gb); | |
3837 | |
3838 for(i=0; i<sps->poc_cycle_length; i++) | |
3839 sps->offset_for_ref_frame[i]= get_se_golomb(&s->gb); | |
3840 } | |
3841 if(sps->poc_type > 2){ | |
3842 fprintf(stderr, "illegal POC type %d\n", sps->poc_type); | |
3843 return -1; | |
3844 } | |
3845 | |
3846 sps->ref_frame_count= get_ue_golomb(&s->gb); | |
3847 sps->required_frame_num_update_behaviour_flag= get_bits1(&s->gb); | |
3848 sps->mb_width= get_ue_golomb(&s->gb) + 1; | |
3849 sps->mb_height= get_ue_golomb(&s->gb) + 1; | |
3850 sps->frame_mbs_only_flag= get_bits1(&s->gb); | |
3851 if(!sps->frame_mbs_only_flag) | |
3852 sps->mb_aff= get_bits1(&s->gb); | |
3853 else | |
3854 sps->mb_aff= 0; | |
3855 | |
3856 sps->direct_8x8_inference_flag= get_bits1(&s->gb); | |
3857 | |
3858 sps->vui_parameters_present_flag= get_bits1(&s->gb); | |
3859 if( sps->vui_parameters_present_flag ) | |
3860 decode_vui_parameters(h, sps); | |
3861 | |
3862 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
3863 printf("sps:%d profile:%d/%d poc:%d ref:%d %dx%d %s %s %s\n", | |
3864 sps_id, sps->profile_idc, sps->level_idc, | |
3865 sps->poc_type, | |
3866 sps->ref_frame_count, | |
3867 sps->mb_width, sps->mb_height, | |
3868 sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"), | |
3869 sps->direct_8x8_inference_flag ? "8B8" : "", | |
3870 sps->vui_parameters_present_flag ? "VUI" : "" | |
3871 ); | |
3872 } | |
3873 return 0; | |
3874 } | |
3875 | |
3876 static inline int decode_picture_parameter_set(H264Context *h){ | |
3877 MpegEncContext * const s = &h->s; | |
3878 int pps_id= get_ue_golomb(&s->gb); | |
3879 PPS *pps= &h->pps_buffer[pps_id]; | |
3880 | |
3881 pps->sps_id= get_ue_golomb(&s->gb); | |
3882 pps->cabac= get_bits1(&s->gb); | |
3883 pps->pic_order_present= get_bits1(&s->gb); | |
3884 pps->slice_group_count= get_ue_golomb(&s->gb) + 1; | |
3885 if(pps->slice_group_count > 1 ){ | |
3886 pps->mb_slice_group_map_type= get_ue_golomb(&s->gb); | |
3887 fprintf(stderr, "FMO not supported\n"); | |
3888 switch(pps->mb_slice_group_map_type){ | |
3889 case 0: | |
3890 #if 0 | |
3891 | for( i = 0; i <= num_slice_groups_minus1; i++ ) | | | | |
3892 | run_length[ i ] |1 |ue(v) | | |
3893 #endif | |
3894 break; | |
3895 case 2: | |
3896 #if 0 | |
3897 | for( i = 0; i < num_slice_groups_minus1; i++ ) | | | | |
3898 |{ | | | | |
3899 | top_left_mb[ i ] |1 |ue(v) | | |
3900 | bottom_right_mb[ i ] |1 |ue(v) | | |
3901 | } | | | | |
3902 #endif | |
3903 break; | |
3904 case 3: | |
3905 case 4: | |
3906 case 5: | |
3907 #if 0 | |
3908 | slice_group_change_direction_flag |1 |u(1) | | |
3909 | slice_group_change_rate_minus1 |1 |ue(v) | | |
3910 #endif | |
3911 break; | |
3912 case 6: | |
3913 #if 0 | |
3914 | slice_group_id_cnt_minus1 |1 |ue(v) | | |
3915 | for( i = 0; i <= slice_group_id_cnt_minus1; i++ | | | | |
3916 |) | | | | |
3917 | slice_group_id[ i ] |1 |u(v) | | |
3918 #endif | |
1214 | 3919 break; |
1168 | 3920 } |
3921 } | |
3922 pps->ref_count[0]= get_ue_golomb(&s->gb) + 1; | |
3923 pps->ref_count[1]= get_ue_golomb(&s->gb) + 1; | |
3924 if(pps->ref_count[0] > 32 || pps->ref_count[1] > 32){ | |
3925 fprintf(stderr, "reference overflow (pps)\n"); | |
3926 return -1; | |
3927 } | |
3928 | |
3929 pps->weighted_pred= get_bits1(&s->gb); | |
3930 pps->weighted_bipred_idc= get_bits(&s->gb, 2); | |
3931 pps->init_qp= get_se_golomb(&s->gb) + 26; | |
3932 pps->init_qs= get_se_golomb(&s->gb) + 26; | |
3933 pps->chroma_qp_index_offset= get_se_golomb(&s->gb); | |
3934 pps->deblocking_filter_parameters_present= get_bits1(&s->gb); | |
3935 pps->constrained_intra_pred= get_bits1(&s->gb); | |
3936 pps->redundant_pic_cnt_present = get_bits1(&s->gb); | |
3937 pps->crop= get_bits1(&s->gb); | |
3938 if(pps->crop){ | |
3939 pps->crop_left = get_ue_golomb(&s->gb); | |
3940 pps->crop_right = get_ue_golomb(&s->gb); | |
3941 pps->crop_top = get_ue_golomb(&s->gb); | |
3942 pps->crop_bottom= get_ue_golomb(&s->gb); | |
3943 }else{ | |
3944 pps->crop_left = | |
3945 pps->crop_right = | |
3946 pps->crop_top = | |
3947 pps->crop_bottom= 0; | |
3948 } | |
3949 | |
3950 if(s->avctx->debug&FF_DEBUG_PICT_INFO){ | |
3951 printf("pps:%d sps:%d %s slice_groups:%d ref:%d/%d %s qp:%d/%d/%d %s %s %s crop:%d/%d/%d/%d\n", | |
3952 pps_id, pps->sps_id, | |
3953 pps->cabac ? "CABAC" : "CAVLC", | |
3954 pps->slice_group_count, | |
3955 pps->ref_count[0], pps->ref_count[1], | |
3956 pps->weighted_pred ? "weighted" : "", | |
3957 pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset, | |
3958 pps->deblocking_filter_parameters_present ? "LPAR" : "", | |
3959 pps->constrained_intra_pred ? "CONSTR" : "", | |
3960 pps->redundant_pic_cnt_present ? "REDU" : "", | |
3961 pps->crop_left, pps->crop_right, | |
3962 pps->crop_top, pps->crop_bottom | |
3963 ); | |
3964 } | |
3965 | |
3966 return 0; | |
3967 } | |
3968 | |
3969 /** | |
3970 * finds the end of the current frame in the bitstream. | |
3971 * @return the position of the first byte of the next frame, or -1 | |
3972 */ | |
3973 static int find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){ | |
3974 ParseContext *pc= &s->parse_context; | |
1187 | 3975 int i; |
1168 | 3976 uint32_t state; |
3977 //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]); | |
3978 // mb_addr= pc->mb_addr - 1; | |
3979 state= pc->state; | |
3980 //FIXME this will fail with slices | |
3981 for(i=0; i<buf_size; i++){ | |
3982 state= (state<<8) | buf[i]; | |
3983 if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){ | |
3984 if(pc->frame_start_found){ | |
3985 pc->state=-1; | |
3986 pc->frame_start_found= 0; | |
3987 return i-3; | |
3988 } | |
3989 pc->frame_start_found= 1; | |
3990 } | |
3991 } | |
3992 | |
3993 pc->state= state; | |
1219 | 3994 return END_NOT_FOUND; |
1168 | 3995 } |
3996 | |
3997 static int decode_nal_units(H264Context *h, uint8_t *buf, int buf_size){ | |
3998 MpegEncContext * const s = &h->s; | |
3999 AVCodecContext * const avctx= s->avctx; | |
4000 int buf_index=0; | |
4001 int i; | |
4002 #if 0 | |
4003 for(i=0; i<32; i++){ | |
4004 printf("%X ", buf[i]); | |
4005 } | |
4006 #endif | |
4007 for(;;){ | |
4008 int consumed; | |
4009 int dst_length; | |
4010 int bit_length; | |
4011 uint8_t *ptr; | |
4012 | |
4013 // start code prefix search | |
4014 for(; buf_index + 3 < buf_size; buf_index++){ | |
4015 // this should allways succeed in the first iteration | |
4016 if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1) | |
4017 break; | |
4018 } | |
4019 | |
4020 if(buf_index+3 >= buf_size) break; | |
4021 | |
4022 buf_index+=3; | |
4023 | |
4024 ptr= decode_nal(h, buf + buf_index, &dst_length, &consumed, buf_size - buf_index); | |
4025 if(ptr[dst_length - 1] == 0) dst_length--; | |
4026 bit_length= 8*dst_length - decode_rbsp_trailing(ptr + dst_length - 1); | |
4027 | |
4028 if(s->avctx->debug&FF_DEBUG_STARTCODE){ | |
4029 printf("NAL %d at %d length %d\n", h->nal_unit_type, buf_index, dst_length); | |
4030 } | |
4031 | |
4032 buf_index += consumed; | |
4033 | |
4034 if(h->nal_ref_idc < s->hurry_up) | |
4035 continue; | |
4036 | |
4037 switch(h->nal_unit_type){ | |
4038 case NAL_IDR_SLICE: | |
4039 idr(h); //FIXME ensure we dont loose some frames if there is reordering | |
4040 case NAL_SLICE: | |
4041 init_get_bits(&s->gb, ptr, bit_length); | |
4042 h->intra_gb_ptr= | |
4043 h->inter_gb_ptr= &s->gb; | |
4044 s->data_partitioning = 0; | |
4045 | |
4046 if(decode_slice_header(h) < 0) return -1; | |
4047 if(h->redundant_pic_count==0) | |
4048 decode_slice(h); | |
4049 break; | |
4050 case NAL_DPA: | |
4051 init_get_bits(&s->gb, ptr, bit_length); | |
4052 h->intra_gb_ptr= | |
4053 h->inter_gb_ptr= NULL; | |
4054 s->data_partitioning = 1; | |
4055 | |
4056 if(decode_slice_header(h) < 0) return -1; | |
4057 break; | |
4058 case NAL_DPB: | |
4059 init_get_bits(&h->intra_gb, ptr, bit_length); | |
4060 h->intra_gb_ptr= &h->intra_gb; | |
4061 break; | |
4062 case NAL_DPC: | |
4063 init_get_bits(&h->inter_gb, ptr, bit_length); | |
4064 h->inter_gb_ptr= &h->inter_gb; | |
1174 | 4065 |
4066 if(h->redundant_pic_count==0 && h->intra_gb_ptr && s->data_partitioning) | |
1168 | 4067 decode_slice(h); |
4068 break; | |
4069 case NAL_SEI: | |
4070 break; | |
4071 case NAL_SPS: | |
4072 init_get_bits(&s->gb, ptr, bit_length); | |
4073 decode_seq_parameter_set(h); | |
4074 | |
4075 if(s->flags& CODEC_FLAG_LOW_DELAY) | |
4076 s->low_delay=1; | |
4077 | |
4078 avctx->has_b_frames= !s->low_delay; | |
4079 break; | |
4080 case NAL_PPS: | |
4081 init_get_bits(&s->gb, ptr, bit_length); | |
4082 | |
4083 decode_picture_parameter_set(h); | |
4084 | |
4085 break; | |
4086 case NAL_PICTURE_DELIMITER: | |
4087 break; | |
4088 case NAL_FILTER_DATA: | |
4089 break; | |
4090 } | |
4091 | |
4092 //FIXME move after where irt is set | |
4093 s->current_picture.pict_type= s->pict_type; | |
4094 s->current_picture.key_frame= s->pict_type == I_TYPE; | |
4095 } | |
4096 | |
1174 | 4097 if(!s->current_picture_ptr) return buf_index; //no frame |
4098 | |
1168 | 4099 h->prev_frame_num_offset= h->frame_num_offset; |
4100 h->prev_frame_num= h->frame_num; | |
4101 if(s->current_picture_ptr->reference){ | |
4102 h->prev_poc_msb= h->poc_msb; | |
4103 h->prev_poc_lsb= h->poc_lsb; | |
4104 } | |
4105 if(s->current_picture_ptr->reference) | |
4106 execute_ref_pic_marking(h, h->mmco, h->mmco_index); | |
4107 else | |
1169 | 4108 assert(h->mmco_index==0); |
1168 | 4109 |
4110 ff_er_frame_end(s); | |
4111 MPV_frame_end(s); | |
4112 | |
4113 return buf_index; | |
4114 } | |
4115 | |
4116 /** | |
4117 * retunrs the number of bytes consumed for building the current frame | |
4118 */ | |
4119 static int get_consumed_bytes(MpegEncContext *s, int pos, int buf_size){ | |
4120 if(s->flags&CODEC_FLAG_TRUNCATED){ | |
4121 pos -= s->parse_context.last_index; | |
4122 if(pos<0) pos=0; // FIXME remove (uneeded?) | |
4123 | |
4124 return pos; | |
4125 }else{ | |
4126 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...) | |
4127 if(pos+10>buf_size) pos=buf_size; // oops ;) | |
4128 | |
4129 return pos; | |
4130 } | |
4131 } | |
4132 | |
4133 static int decode_frame(AVCodecContext *avctx, | |
4134 void *data, int *data_size, | |
4135 uint8_t *buf, int buf_size) | |
4136 { | |
4137 H264Context *h = avctx->priv_data; | |
4138 MpegEncContext *s = &h->s; | |
4139 AVFrame *pict = data; | |
4140 int buf_index; | |
4141 | |
4142 s->flags= avctx->flags; | |
4143 | |
4144 *data_size = 0; | |
4145 | |
4146 /* no supplementary picture */ | |
4147 if (buf_size == 0) { | |
4148 return 0; | |
4149 } | |
4150 | |
4151 if(s->flags&CODEC_FLAG_TRUNCATED){ | |
4152 int next= find_frame_end(s, buf, buf_size); | |
4153 | |
4154 if( ff_combine_frame(s, next, &buf, &buf_size) < 0 ) | |
4155 return buf_size; | |
4156 //printf("next:%d buf_size:%d last_index:%d\n", next, buf_size, s->parse_context.last_index); | |
4157 } | |
4158 | |
4159 if(s->avctx->extradata_size && s->picture_number==0){ | |
4160 if(0 < decode_nal_units(h, s->avctx->extradata, s->avctx->extradata_size) ) | |
4161 return -1; | |
4162 } | |
4163 | |
4164 buf_index=decode_nal_units(h, buf, buf_size); | |
4165 if(buf_index < 0) | |
4166 return -1; | |
4167 | |
4168 //FIXME do something with unavailable reference frames | |
4169 | |
4170 // if(ret==FRAME_SKIPED) return get_consumed_bytes(s, buf_index, buf_size); | |
4171 #if 0 | |
4172 if(s->pict_type==B_TYPE || s->low_delay){ | |
4173 *pict= *(AVFrame*)&s->current_picture; | |
4174 } else { | |
4175 *pict= *(AVFrame*)&s->last_picture; | |
4176 } | |
4177 #endif | |
1174 | 4178 if(!s->current_picture_ptr){ |
4179 fprintf(stderr, "error, NO frame\n"); | |
4180 return -1; | |
4181 } | |
4182 | |
1168 | 4183 *pict= *(AVFrame*)&s->current_picture; //FIXME |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
4184 ff_print_debug_info(s, s->current_picture_ptr); |
1168 | 4185 assert(pict->data[0]); |
4186 //printf("out %d\n", (int)pict->data[0]); | |
4187 #if 0 //? | |
4188 | |
4189 /* Return the Picture timestamp as the frame number */ | |
4190 /* we substract 1 because it is added on utils.c */ | |
4191 avctx->frame_number = s->picture_number - 1; | |
4192 #endif | |
4193 #if 0 | |
4194 /* dont output the last pic after seeking */ | |
4195 if(s->last_picture_ptr || s->low_delay) | |
4196 //Note this isnt a issue as a IDR pic should flush teh buffers | |
4197 #endif | |
4198 *data_size = sizeof(AVFrame); | |
4199 return get_consumed_bytes(s, buf_index, buf_size); | |
4200 } | |
4201 #if 0 | |
4202 static inline void fill_mb_avail(H264Context *h){ | |
4203 MpegEncContext * const s = &h->s; | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
4204 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride; |
1168 | 4205 |
4206 if(s->mb_y){ | |
1177
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
4207 h->mb_avail[0]= s->mb_x && h->slice_table[mb_xy - s->mb_stride - 1] == h->slice_num; |
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
4208 h->mb_avail[1]= h->slice_table[mb_xy - s->mb_stride ] == h->slice_num; |
fea03d2c4946
simplified adressing of most mb based arrays (mb_x + mb_y*s->mb_stride) now instead of mb_x + mb_y*mb_width and 1+mb_x + (1+mb_y)*(mb_width+2) and ... mixture
michaelni
parents:
1174
diff
changeset
|
4209 h->mb_avail[2]= s->mb_x+1 < s->mb_width && h->slice_table[mb_xy - s->mb_stride + 1] == h->slice_num; |
1168 | 4210 }else{ |
4211 h->mb_avail[0]= | |
4212 h->mb_avail[1]= | |
4213 h->mb_avail[2]= 0; | |
4214 } | |
4215 h->mb_avail[3]= s->mb_x && h->slice_table[mb_xy - 1] == h->slice_num; | |
4216 h->mb_avail[4]= 1; //FIXME move out | |
4217 h->mb_avail[5]= 0; //FIXME move out | |
4218 } | |
4219 #endif | |
4220 | |
4221 #if 0 //selftest | |
4222 #define COUNT 8000 | |
4223 #define SIZE (COUNT*40) | |
4224 int main(){ | |
4225 int i; | |
4226 uint8_t temp[SIZE]; | |
4227 PutBitContext pb; | |
4228 GetBitContext gb; | |
4229 // int int_temp[10000]; | |
4230 DSPContext dsp; | |
4231 AVCodecContext avctx; | |
4232 | |
4233 dsputil_init(&dsp, &avctx); | |
4234 | |
4235 init_put_bits(&pb, temp, SIZE, NULL, NULL); | |
4236 printf("testing unsigned exp golomb\n"); | |
4237 for(i=0; i<COUNT; i++){ | |
4238 START_TIMER | |
4239 set_ue_golomb(&pb, i); | |
4240 STOP_TIMER("set_ue_golomb"); | |
4241 } | |
4242 flush_put_bits(&pb); | |
4243 | |
4244 init_get_bits(&gb, temp, 8*SIZE); | |
4245 for(i=0; i<COUNT; i++){ | |
4246 int j, s; | |
4247 | |
4248 s= show_bits(&gb, 24); | |
4249 | |
4250 START_TIMER | |
4251 j= get_ue_golomb(&gb); | |
4252 if(j != i){ | |
4253 printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s); | |
4254 // return -1; | |
4255 } | |
4256 STOP_TIMER("get_ue_golomb"); | |
4257 } | |
4258 | |
4259 | |
4260 init_put_bits(&pb, temp, SIZE, NULL, NULL); | |
4261 printf("testing signed exp golomb\n"); | |
4262 for(i=0; i<COUNT; i++){ | |
4263 START_TIMER | |
4264 set_se_golomb(&pb, i - COUNT/2); | |
4265 STOP_TIMER("set_se_golomb"); | |
4266 } | |
4267 flush_put_bits(&pb); | |
4268 | |
4269 init_get_bits(&gb, temp, 8*SIZE); | |
4270 for(i=0; i<COUNT; i++){ | |
4271 int j, s; | |
4272 | |
4273 s= show_bits(&gb, 24); | |
4274 | |
4275 START_TIMER | |
4276 j= get_se_golomb(&gb); | |
4277 if(j != i - COUNT/2){ | |
4278 printf("missmatch! at %d (%d should be %d) bits:%6X\n", i, j, i, s); | |
4279 // return -1; | |
4280 } | |
4281 STOP_TIMER("get_se_golomb"); | |
4282 } | |
4283 | |
4284 printf("testing 4x4 (I)DCT\n"); | |
4285 | |
4286 DCTELEM block[16]; | |
4287 uint8_t src[16], ref[16]; | |
4288 uint64_t error= 0, max_error=0; | |
4289 | |
4290 for(i=0; i<COUNT; i++){ | |
4291 int j; | |
4292 // printf("%d %d %d\n", r1, r2, (r2-r1)*16); | |
4293 for(j=0; j<16; j++){ | |
4294 ref[j]= random()%255; | |
4295 src[j]= random()%255; | |
4296 } | |
4297 | |
4298 h264_diff_dct_c(block, src, ref, 4); | |
4299 | |
4300 //normalize | |
4301 for(j=0; j<16; j++){ | |
4302 // printf("%d ", block[j]); | |
4303 block[j]= block[j]*4; | |
4304 if(j&1) block[j]= (block[j]*4 + 2)/5; | |
4305 if(j&4) block[j]= (block[j]*4 + 2)/5; | |
4306 } | |
4307 // printf("\n"); | |
4308 | |
4309 h264_add_idct_c(ref, block, 4); | |
4310 /* for(j=0; j<16; j++){ | |
4311 printf("%d ", ref[j]); | |
4312 } | |
4313 printf("\n");*/ | |
4314 | |
4315 for(j=0; j<16; j++){ | |
4316 int diff= ABS(src[j] - ref[j]); | |
4317 | |
4318 error+= diff*diff; | |
4319 max_error= FFMAX(max_error, diff); | |
4320 } | |
4321 } | |
4322 printf("error=%f max_error=%d\n", ((float)error)/COUNT/16, (int)max_error ); | |
4323 #if 0 | |
4324 printf("testing quantizer\n"); | |
4325 for(qp=0; qp<52; qp++){ | |
4326 for(i=0; i<16; i++) | |
4327 src1_block[i]= src2_block[i]= random()%255; | |
4328 | |
4329 } | |
4330 #endif | |
4331 printf("Testing NAL layer\n"); | |
4332 | |
4333 uint8_t bitstream[COUNT]; | |
4334 uint8_t nal[COUNT*2]; | |
4335 H264Context h; | |
4336 memset(&h, 0, sizeof(H264Context)); | |
4337 | |
4338 for(i=0; i<COUNT; i++){ | |
4339 int zeros= i; | |
4340 int nal_length; | |
4341 int consumed; | |
4342 int out_length; | |
4343 uint8_t *out; | |
4344 int j; | |
4345 | |
4346 for(j=0; j<COUNT; j++){ | |
4347 bitstream[j]= (random() % 255) + 1; | |
4348 } | |
4349 | |
4350 for(j=0; j<zeros; j++){ | |
4351 int pos= random() % COUNT; | |
4352 while(bitstream[pos] == 0){ | |
4353 pos++; | |
4354 pos %= COUNT; | |
4355 } | |
4356 bitstream[pos]=0; | |
4357 } | |
4358 | |
4359 START_TIMER | |
4360 | |
4361 nal_length= encode_nal(&h, nal, bitstream, COUNT, COUNT*2); | |
4362 if(nal_length<0){ | |
4363 printf("encoding failed\n"); | |
4364 return -1; | |
4365 } | |
4366 | |
4367 out= decode_nal(&h, nal, &out_length, &consumed, nal_length); | |
4368 | |
4369 STOP_TIMER("NAL") | |
4370 | |
4371 if(out_length != COUNT){ | |
4372 printf("incorrect length %d %d\n", out_length, COUNT); | |
4373 return -1; | |
4374 } | |
4375 | |
4376 if(consumed != nal_length){ | |
4377 printf("incorrect consumed length %d %d\n", nal_length, consumed); | |
4378 return -1; | |
4379 } | |
4380 | |
4381 if(memcmp(bitstream, out, COUNT)){ | |
4382 printf("missmatch\n"); | |
4383 return -1; | |
4384 } | |
4385 } | |
4386 | |
4387 printf("Testing RBSP\n"); | |
4388 | |
4389 | |
4390 return 0; | |
4391 } | |
4392 #endif | |
4393 | |
4394 | |
4395 static int decode_end(AVCodecContext *avctx) | |
4396 { | |
4397 H264Context *h = avctx->priv_data; | |
4398 MpegEncContext *s = &h->s; | |
4399 | |
4400 free_tables(h); //FIXME cleanup init stuff perhaps | |
4401 MPV_common_end(s); | |
4402 | |
4403 // memset(h, 0, sizeof(H264Context)); | |
4404 | |
4405 return 0; | |
4406 } | |
4407 | |
4408 | |
4409 AVCodec h264_decoder = { | |
4410 "h264", | |
4411 CODEC_TYPE_VIDEO, | |
4412 CODEC_ID_H264, | |
4413 sizeof(H264Context), | |
4414 decode_init, | |
4415 NULL, | |
4416 decode_end, | |
4417 decode_frame, | |
1214 | 4418 /*CODEC_CAP_DRAW_HORIZ_BAND |*/ CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED, |
1168 | 4419 }; |
4420 | |
1234 | 4421 #include "svq3.c" |