ad7b2416e804cbe09a850427a7e7c55884315605
[libav.git] / libavcodec / h264.c
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;
356 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
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{
367 topleft_xy = mb_xy-1 - s->mb_stride;
368 top_xy = mb_xy - s->mb_stride;
369 topright_xy= mb_xy+1 - s->mb_stride;
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;
603 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
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
686 tprintf("mode:%d %d min:%d\n", left ,top, min);
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;
694 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
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
725 tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
726
727 return i&31;
728 }
729
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{
737 tprintf("topright MV not available\n");
738
739 *C= h->mv_cache[list][ i - 8 - 1 ];
740 return h->ref_cache[list][ i - 8 - 1 ];
741 }
742 }
743
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 ];
758 const int16_t * C;
759 int diagonal_ref, match_count;
760
761 assert(part_width==1 || part_width==2 || part_width==4);
762
763 /* mv_cache
764 B . . A T T T T
765 U . . L . . , .
766 U . . L . . . .
767 U . . L . . , .
768 . . . L . . . .
769 */
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];
792 }else{
793 *mx= mid_pred(A[0], B[0], C[0]);
794 *my= mid_pred(A[1], B[1], C[1]);
795 }
796 }
797
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);
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
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);
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
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);
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
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);
850
851 if(left_ref == ref){
852 *mx= A[0];
853 *my= A[1];
854 return;
855 }
856 }else{
857 const int16_t * C;
858 int diagonal_ref;
859
860 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
861
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);
863
864 if(diagonal_ref == ref){
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
880 tprintf("pred_pskip: (%d) (%d) at %2d %2d", top_ref, left_ref, s->mb_x, s->mb_y);
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;
897 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
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
1072 tprintf("rbsp trailing %X\n", v);
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){
1684 int i, j, k;
1685 int a;
1686 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1687 const uint8_t * const src0 = src+7-stride;
1688 const uint8_t *src1 = src+8*stride-1;
1689 const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
1690 int H = src0[1] - src0[-1];
1691 int V = src1[0] - src2[ 0];
1692 for(k=2; k<=8; ++k) {
1693 src1 += stride; src2 -= stride;
1694 H += k*(src0[k] - src0[-k]);
1695 V += k*(src1[0] - src2[ 0]);
1696 }
1697 H = ( 5*H+32 ) >> 6;
1698 V = ( 5*V+32 ) >> 6;
1699
1700 a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
1701 for(j=16; j>0; --j) {
1702 int b = a;
1703 a += V;
1704 for(i=-16; i<0; i+=4) {
1705 src[16+i] = cm[ (b ) >> 5 ];
1706 src[17+i] = cm[ (b+ H) >> 5 ];
1707 src[18+i] = cm[ (b+2*H) >> 5 ];
1708 src[19+i] = cm[ (b+3*H) >> 5 ];
1709 b += 4*H;
1710 }
1711 src += stride;
1712 }
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){
1819 int j, k;
1820 int a;
1821 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1822 const uint8_t * const src0 = src+3-stride;
1823 const uint8_t *src1 = src+4*stride-1;
1824 const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
1825 int H = src0[1] - src0[-1];
1826 int V = src1[0] - src2[ 0];
1827 for(k=2; k<=4; ++k) {
1828 src1 += stride; src2 -= stride;
1829 H += k*(src0[k] - src0[-k]);
1830 V += k*(src1[0] - src2[ 0]);
1831 }
1832 H = ( 17*H+16 ) >> 5;
1833 V = ( 17*V+16 ) >> 5;
1834
1835 a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
1836 for(j=8; j>0; --j) {
1837 int b = a;
1838 a += V;
1839 src[0] = cm[ (b ) >> 5 ];
1840 src[1] = cm[ (b+ H) >> 5 ];
1841 src[2] = cm[ (b+2*H) >> 5 ];
1842 src[3] = cm[ (b+3*H) >> 5 ];
1843 src[4] = cm[ (b+4*H) >> 5 ];
1844 src[5] = cm[ (b+5*H) >> 5 ];
1845 src[6] = cm[ (b+6*H) >> 5 ];
1846 src[7] = cm[ (b+7*H) >> 5 ];
1847 src += stride;
1848 }
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){
1919 Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
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){
1929 Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
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;
1940 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
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;
2110 const int big_mb_num= s->mb_stride * (s->mb_height+1);
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));
2118 h->slice_table= h->slice_table_base + s->mb_stride + 1;
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++){
2124 const int mb_xy= x + y*s->mb_stride;
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;
2199 const int mb_xy= mb_x + mb_y*s->mb_stride;
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){
2516 MpegEncContext * const s = &h->s;
2517 int i;
2518
2519 if(s->avctx->debug&FF_DEBUG_MMCO)
2520 printf("remove short %d count %d\n", frame_num, h->short_ref_count);
2521
2522 for(i=0; i<h->short_ref_count; i++){
2523 Picture *pic= h->short_ref[i];
2524 if(s->avctx->debug&FF_DEBUG_MMCO)
2525 printf("%d %d %X\n", i, pic->frame_num, (int)pic);
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)
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;
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];
2814 if(h->pps.slice_group_count == 0){
2815 fprintf(stderr, "non existing PPS referenced\n");
2816 return -1;
2817 }
2818
2819 h->sps= h->sps_buffer[ h->pps.sps_id ];
2820 if(h->sps.log2_max_frame_num == 0){
2821 fprintf(stderr, "non existing SPS referenced\n");
2822 return -1;
2823 }
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
2870 s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
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;
3050 tprintf("trailing:%d, total:%d\n", trailing_ones, total_coeff);
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
3094 tprintf("level: %d suffix_length:%d\n", level[i], suffix_length);
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,