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