2 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
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.
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.
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
23 * H.264 / AVC / MPEG4 part10 codec.
24 * @author Michael Niedermayer <michaelni@gmx.at>
30 #include "mpegvideo.h"
39 #define interlaced_dct interlaced_dct_is_a_bad_name
40 #define mb_intra mb_intra_isnt_initalized_see_mb_type
42 #define LUMA_DC_BLOCK_INDEX 25
43 #define CHROMA_DC_BLOCK_INDEX 26
45 #define CHROMA_DC_COEFF_TOKEN_VLC_BITS 8
46 #define COEFF_TOKEN_VLC_BITS 8
47 #define TOTAL_ZEROS_VLC_BITS 9
48 #define CHROMA_DC_TOTAL_ZEROS_VLC_BITS 3
49 #define RUN_VLC_BITS 3
50 #define RUN7_VLC_BITS 6
52 #define MAX_SPS_COUNT 32
53 #define MAX_PPS_COUNT 256
55 #define MAX_MMCO_COUNT 66
58 * Sequence parameter set
64 int log2_max_frame_num; ///< log2_max_frame_num_minus4 + 4
65 int poc_type; ///< pic_order_cnt_type
66 int log2_max_poc_lsb; ///< log2_max_pic_order_cnt_lsb_minus4
67 int delta_pic_order_always_zero_flag;
68 int offset_for_non_ref_pic;
69 int offset_for_top_to_bottom_field;
70 int poc_cycle_length; ///< num_ref_frames_in_pic_order_cnt_cycle
71 int ref_frame_count; ///< num_ref_frames
72 int gaps_in_frame_num_allowed_flag;
73 int mb_width; ///< frame_width_in_mbs_minus1 + 1
74 int mb_height; ///< frame_height_in_mbs_minus1 + 1
75 int frame_mbs_only_flag;
76 int mb_aff; ///<mb_adaptive_frame_field_flag
77 int direct_8x8_inference_flag;
78 int crop; ///< frame_cropping_flag
79 int crop_left; ///< frame_cropping_rect_left_offset
80 int crop_right; ///< frame_cropping_rect_right_offset
81 int crop_top; ///< frame_cropping_rect_top_offset
82 int crop_bottom; ///< frame_cropping_rect_bottom_offset
83 int vui_parameters_present_flag;
85 int timing_info_present_flag;
86 uint32_t num_units_in_tick;
88 int fixed_frame_rate_flag;
89 short offset_for_ref_frame[256]; //FIXME dyn aloc?
93 * Picture parameter set
97 int cabac; ///< entropy_coding_mode_flag
98 int pic_order_present; ///< pic_order_present_flag
99 int slice_group_count; ///< num_slice_groups_minus1 + 1
100 int mb_slice_group_map_type;
101 int ref_count[2]; ///< num_ref_idx_l0/1_active_minus1 + 1
102 int weighted_pred; ///< weighted_pred_flag
103 int weighted_bipred_idc;
104 int init_qp; ///< pic_init_qp_minus26 + 26
105 int init_qs; ///< pic_init_qs_minus26 + 26
106 int chroma_qp_index_offset;
107 int deblocking_filter_parameters_present; ///< deblocking_filter_parameters_present_flag
108 int constrained_intra_pred; ///< constrained_intra_pred_flag
109 int redundant_pic_cnt_present; ///< redundant_pic_cnt_present_flag
113 * Memory management control operation opcode.
115 typedef enum MMCOOpcode{
126 * Memory management control operation.
137 typedef struct H264Context{
145 #define NAL_IDR_SLICE 5
149 #define NAL_PICTURE_DELIMITER 9
150 #define NAL_FILTER_DATA 10
151 uint8_t *rbsp_buffer;
152 int rbsp_buffer_size;
155 * Used to parse AVC variant of h264
157 int is_avc; ///< this flag is != 0 if codec is avc1
158 int got_avcC; ///< flag used to parse avcC data only once
159 int nal_length_size; ///< Number of bytes used for nal length (1, 2 or 4)
163 int prev_mb_skiped; //FIXME remove (IMHO not used)
166 int chroma_pred_mode;
167 int intra16x16_pred_mode;
169 int8_t intra4x4_pred_mode_cache[5*8];
170 int8_t (*intra4x4_pred_mode)[8];
171 void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
172 void (*pred8x8 [4+3])(uint8_t *src, int stride);
173 void (*pred16x16[4+3])(uint8_t *src, int stride);
174 unsigned int topleft_samples_available;
175 unsigned int top_samples_available;
176 unsigned int topright_samples_available;
177 unsigned int left_samples_available;
178 uint8_t (*top_border)[16+2*8];
179 uint8_t left_border[17+2*9];
182 * non zero coeff count cache.
183 * is 64 if not available.
185 uint8_t non_zero_count_cache[6*8] __align8;
186 uint8_t (*non_zero_count)[16];
189 * Motion vector cache.
191 int16_t mv_cache[2][5*8][2] __align8;
192 int8_t ref_cache[2][5*8] __align8;
193 #define LIST_NOT_USED -1 //FIXME rename?
194 #define PART_NOT_AVAILABLE -2
197 * is 1 if the specific list MV&references are set to 0,0,-2.
199 int mv_cache_clean[2];
201 int block_offset[16+8];
202 int chroma_subblock_offset[16]; //FIXME remove
204 uint16_t *mb2b_xy; //FIXME are these 4 a good idea?
206 int b_stride; //FIXME use s->b4_stride
212 int unknown_svq3_flag;
213 int next_slice_index;
215 SPS sps_buffer[MAX_SPS_COUNT];
216 SPS sps; ///< current sps
218 PPS pps_buffer[MAX_PPS_COUNT];
222 PPS pps; //FIXME move tp Picture perhaps? (->no) do we need that?
225 uint8_t *slice_table_base;
226 uint8_t *slice_table; ///< slice_table_base + mb_stride + 1
228 int slice_type_fixed;
230 //interlacing specific flags
231 int mb_field_decoding_flag;
238 int delta_poc_bottom;
241 int prev_poc_msb; ///< poc_msb of the last reference pic for POC type 0
242 int prev_poc_lsb; ///< poc_lsb of the last reference pic for POC type 0
243 int frame_num_offset; ///< for POC type 2
244 int prev_frame_num_offset; ///< for POC type 2
245 int prev_frame_num; ///< frame_num of the last pic for POC type 1/2
248 * frame_num for frames or 2*frame_num for field pics.
253 * max_frame_num or 2*max_frame_num for field pics.
257 //Weighted pred stuff
259 int use_weight_chroma;
260 int luma_log2_weight_denom;
261 int chroma_log2_weight_denom;
262 int luma_weight[2][16];
263 int luma_offset[2][16];
264 int chroma_weight[2][16][2];
265 int chroma_offset[2][16][2];
266 int implicit_weight[16][16];
269 int deblocking_filter; ///< disable_deblocking_filter_idc with 1<->0
270 int slice_alpha_c0_offset;
271 int slice_beta_offset;
273 int redundant_pic_count;
275 int direct_spatial_mv_pred;
276 int dist_scale_factor[16];
277 int map_col_to_list0[2][16];
280 * num_ref_idx_l0/1_active_minus1 + 1
282 int ref_count[2];// FIXME split for AFF
283 Picture *short_ref[16];
284 Picture *long_ref[16];
285 Picture default_ref_list[2][32];
286 Picture ref_list[2][32]; //FIXME size?
287 Picture field_ref_list[2][32]; //FIXME size?
288 Picture *delayed_pic[16]; //FIXME size?
289 int delayed_output_poc;
292 * memory management control operations buffer.
294 MMCO mmco[MAX_MMCO_COUNT];
297 int long_ref_count; ///< number of actual long term references
298 int short_ref_count; ///< number of actual short term references
301 GetBitContext intra_gb;
302 GetBitContext inter_gb;
303 GetBitContext *intra_gb_ptr;
304 GetBitContext *inter_gb_ptr;
306 DCTELEM mb[16*24] __align8;
312 uint8_t cabac_state[399];
315 /* 0x100 -> non null luma_dc, 0x80/0x40 -> non null chroma_dc (cb/cr), 0x?0 -> chroma_cbp(0,1,2), 0x0? luma_cbp */
319 /* chroma_pred_mode for i4x4 or i16x16, else 0 */
320 uint8_t *chroma_pred_mode_table;
321 int last_qscale_diff;
322 int16_t (*mvd_table[2])[2];
323 int16_t mvd_cache[2][5*8][2] __align8;
324 uint8_t *direct_table;
325 uint8_t direct_cache[5*8];
329 static VLC coeff_token_vlc[4];
330 static VLC chroma_dc_coeff_token_vlc;
332 static VLC total_zeros_vlc[15];
333 static VLC chroma_dc_total_zeros_vlc[3];
335 static VLC run_vlc[6];
338 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
339 static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
340 static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr);
342 static inline uint32_t pack16to32(int a, int b){
343 #ifdef WORDS_BIGENDIAN
344 return (b&0xFFFF) + (a<<16);
346 return (a&0xFFFF) + (b<<16);
352 * @param h height of the rectangle, should be a constant
353 * @param w width of the rectangle, should be a constant
354 * @param size the size of val (1 or 4), should be a constant
356 static inline void fill_rectangle(void *vp, int w, int h, int stride, uint32_t val, int size){ //FIXME ensure this IS inlined
357 uint8_t *p= (uint8_t*)vp;
358 assert(size==1 || size==4);
363 assert((((int)vp)&(FFMIN(w, STRIDE_ALIGN)-1)) == 0);
364 //FIXME check what gcc generates for 64 bit on x86 and possible write a 32 bit ver of it
367 *(uint16_t*)(p + stride)= size==4 ? val : val*0x0101;
368 }else if(w==2 && h==4){
369 *(uint16_t*)(p + 0*stride)=
370 *(uint16_t*)(p + 1*stride)=
371 *(uint16_t*)(p + 2*stride)=
372 *(uint16_t*)(p + 3*stride)= size==4 ? val : val*0x0101;
373 }else if(w==4 && h==1){
374 *(uint32_t*)(p + 0*stride)= size==4 ? val : val*0x01010101;
375 }else if(w==4 && h==2){
376 *(uint32_t*)(p + 0*stride)=
377 *(uint32_t*)(p + 1*stride)= size==4 ? val : val*0x01010101;
378 }else if(w==4 && h==4){
379 *(uint32_t*)(p + 0*stride)=
380 *(uint32_t*)(p + 1*stride)=
381 *(uint32_t*)(p + 2*stride)=
382 *(uint32_t*)(p + 3*stride)= size==4 ? val : val*0x01010101;
383 }else if(w==8 && h==1){
385 *(uint32_t*)(p + 4)= size==4 ? val : val*0x01010101;
386 }else if(w==8 && h==2){
387 *(uint32_t*)(p + 0 + 0*stride)=
388 *(uint32_t*)(p + 4 + 0*stride)=
389 *(uint32_t*)(p + 0 + 1*stride)=
390 *(uint32_t*)(p + 4 + 1*stride)= size==4 ? val : val*0x01010101;
391 }else if(w==8 && h==4){
392 *(uint64_t*)(p + 0*stride)=
393 *(uint64_t*)(p + 1*stride)=
394 *(uint64_t*)(p + 2*stride)=
395 *(uint64_t*)(p + 3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
396 }else if(w==16 && h==2){
397 *(uint64_t*)(p + 0+0*stride)=
398 *(uint64_t*)(p + 8+0*stride)=
399 *(uint64_t*)(p + 0+1*stride)=
400 *(uint64_t*)(p + 8+1*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
401 }else if(w==16 && h==4){
402 *(uint64_t*)(p + 0+0*stride)=
403 *(uint64_t*)(p + 8+0*stride)=
404 *(uint64_t*)(p + 0+1*stride)=
405 *(uint64_t*)(p + 8+1*stride)=
406 *(uint64_t*)(p + 0+2*stride)=
407 *(uint64_t*)(p + 8+2*stride)=
408 *(uint64_t*)(p + 0+3*stride)=
409 *(uint64_t*)(p + 8+3*stride)= size==4 ? val*0x0100000001ULL : val*0x0101010101010101ULL;
414 static inline void fill_caches(H264Context *h, int mb_type, int for_deblock){
415 MpegEncContext * const s = &h->s;
416 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
417 int topleft_xy, top_xy, topright_xy, left_xy[2];
418 int topleft_type, top_type, topright_type, left_type[2];
422 //wow what a mess, why didnt they simplify the interlacing&intra stuff, i cant imagine that these complex rules are worth it
426 topleft_xy = 0; /* avoid warning */
427 top_xy = 0; /* avoid warning */
428 topright_xy = 0; /* avoid warning */
430 topleft_xy = mb_xy-1 - s->mb_stride;
431 top_xy = mb_xy - s->mb_stride;
432 topright_xy= mb_xy+1 - s->mb_stride;
433 left_xy[0] = mb_xy-1;
434 left_xy[1] = mb_xy-1;
442 topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0;
443 top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
444 topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0;
445 left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
446 left_type[1] = h->slice_table[left_xy[1] ] < 255 ? s->current_picture.mb_type[left_xy[1]] : 0;
448 topleft_type = h->slice_table[topleft_xy ] == h->slice_num ? s->current_picture.mb_type[topleft_xy] : 0;
449 top_type = h->slice_table[top_xy ] == h->slice_num ? s->current_picture.mb_type[top_xy] : 0;
450 topright_type= h->slice_table[topright_xy] == h->slice_num ? s->current_picture.mb_type[topright_xy]: 0;
451 left_type[0] = h->slice_table[left_xy[0] ] == h->slice_num ? s->current_picture.mb_type[left_xy[0]] : 0;
452 left_type[1] = h->slice_table[left_xy[1] ] == h->slice_num ? s->current_picture.mb_type[left_xy[1]] : 0;
455 if(IS_INTRA(mb_type)){
456 h->topleft_samples_available=
457 h->top_samples_available=
458 h->left_samples_available= 0xFFFF;
459 h->topright_samples_available= 0xEEEA;
461 if(!IS_INTRA(top_type) && (top_type==0 || h->pps.constrained_intra_pred)){
462 h->topleft_samples_available= 0xB3FF;
463 h->top_samples_available= 0x33FF;
464 h->topright_samples_available= 0x26EA;
467 if(!IS_INTRA(left_type[i]) && (left_type[i]==0 || h->pps.constrained_intra_pred)){
468 h->topleft_samples_available&= 0xDF5F;
469 h->left_samples_available&= 0x5F5F;
473 if(!IS_INTRA(topleft_type) && (topleft_type==0 || h->pps.constrained_intra_pred))
474 h->topleft_samples_available&= 0x7FFF;
476 if(!IS_INTRA(topright_type) && (topright_type==0 || h->pps.constrained_intra_pred))
477 h->topright_samples_available&= 0xFBFF;
479 if(IS_INTRA4x4(mb_type)){
480 if(IS_INTRA4x4(top_type)){
481 h->intra4x4_pred_mode_cache[4+8*0]= h->intra4x4_pred_mode[top_xy][4];
482 h->intra4x4_pred_mode_cache[5+8*0]= h->intra4x4_pred_mode[top_xy][5];
483 h->intra4x4_pred_mode_cache[6+8*0]= h->intra4x4_pred_mode[top_xy][6];
484 h->intra4x4_pred_mode_cache[7+8*0]= h->intra4x4_pred_mode[top_xy][3];
487 if(!top_type || (IS_INTER(top_type) && h->pps.constrained_intra_pred))
492 h->intra4x4_pred_mode_cache[4+8*0]=
493 h->intra4x4_pred_mode_cache[5+8*0]=
494 h->intra4x4_pred_mode_cache[6+8*0]=
495 h->intra4x4_pred_mode_cache[7+8*0]= pred;
498 if(IS_INTRA4x4(left_type[i])){
499 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[0+2*i]];
500 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= h->intra4x4_pred_mode[left_xy[i]][left_block[1+2*i]];
503 if(!left_type[i] || (IS_INTER(left_type[i]) && h->pps.constrained_intra_pred))
508 h->intra4x4_pred_mode_cache[3+8*1 + 2*8*i]=
509 h->intra4x4_pred_mode_cache[3+8*2 + 2*8*i]= pred;
524 //FIXME constraint_intra_pred & partitioning & nnz (lets hope this is just a typo in the spec)
526 h->non_zero_count_cache[4+8*0]= h->non_zero_count[top_xy][0];
527 h->non_zero_count_cache[5+8*0]= h->non_zero_count[top_xy][1];
528 h->non_zero_count_cache[6+8*0]= h->non_zero_count[top_xy][2];
529 h->non_zero_count_cache[7+8*0]= h->non_zero_count[top_xy][3];
531 h->non_zero_count_cache[1+8*0]= h->non_zero_count[top_xy][7];
532 h->non_zero_count_cache[2+8*0]= h->non_zero_count[top_xy][8];
534 h->non_zero_count_cache[1+8*3]= h->non_zero_count[top_xy][10];
535 h->non_zero_count_cache[2+8*3]= h->non_zero_count[top_xy][11];
537 h->top_cbp= h->cbp_table[top_xy];
539 h->non_zero_count_cache[4+8*0]=
540 h->non_zero_count_cache[5+8*0]=
541 h->non_zero_count_cache[6+8*0]=
542 h->non_zero_count_cache[7+8*0]=
544 h->non_zero_count_cache[1+8*0]=
545 h->non_zero_count_cache[2+8*0]=
547 h->non_zero_count_cache[1+8*3]=
548 h->non_zero_count_cache[2+8*3]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
550 if(IS_INTRA(mb_type)) h->top_cbp= 0x1C0;
555 h->non_zero_count_cache[3+8*1]= h->non_zero_count[left_xy[0]][6];
556 h->non_zero_count_cache[3+8*2]= h->non_zero_count[left_xy[0]][5];
557 h->non_zero_count_cache[0+8*1]= h->non_zero_count[left_xy[0]][9]; //FIXME left_block
558 h->non_zero_count_cache[0+8*4]= h->non_zero_count[left_xy[0]][12];
559 h->left_cbp= h->cbp_table[left_xy[0]]; //FIXME interlacing
561 h->non_zero_count_cache[3+8*1]=
562 h->non_zero_count_cache[3+8*2]=
563 h->non_zero_count_cache[0+8*1]=
564 h->non_zero_count_cache[0+8*4]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
566 if(IS_INTRA(mb_type)) h->left_cbp= 0x1C0;//FIXME interlacing
571 h->non_zero_count_cache[3+8*3]= h->non_zero_count[left_xy[1]][4];
572 h->non_zero_count_cache[3+8*4]= h->non_zero_count[left_xy[1]][3];
573 h->non_zero_count_cache[0+8*2]= h->non_zero_count[left_xy[1]][8];
574 h->non_zero_count_cache[0+8*5]= h->non_zero_count[left_xy[1]][11];
576 h->non_zero_count_cache[3+8*3]=
577 h->non_zero_count_cache[3+8*4]=
578 h->non_zero_count_cache[0+8*2]=
579 h->non_zero_count_cache[0+8*5]= h->pps.cabac && !IS_INTRA(mb_type) ? 0 : 64;
583 //FIXME direct mb can skip much of this
584 if(IS_INTER(mb_type) || (IS_DIRECT(mb_type) && h->direct_spatial_mv_pred)){
586 for(list=0; list<2; list++){
587 if(!USES_LIST(mb_type, list) && !IS_DIRECT(mb_type) && !for_deblock){
588 /*if(!h->mv_cache_clean[list]){
589 memset(h->mv_cache [list], 0, 8*5*2*sizeof(int16_t)); //FIXME clean only input? clean at all?
590 memset(h->ref_cache[list], PART_NOT_AVAILABLE, 8*5*sizeof(int8_t));
591 h->mv_cache_clean[list]= 1;
595 h->mv_cache_clean[list]= 0;
597 if(IS_INTER(topleft_type)){
598 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
599 const int b8_xy= h->mb2b8_xy[topleft_xy] + 1 + h->b8_stride;
600 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
601 h->ref_cache[list][scan8[0] - 1 - 1*8]= s->current_picture.ref_index[list][b8_xy];
603 *(uint32_t*)h->mv_cache[list][scan8[0] - 1 - 1*8]= 0;
604 h->ref_cache[list][scan8[0] - 1 - 1*8]= topleft_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
607 if(IS_INTER(top_type)){
608 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
609 const int b8_xy= h->mb2b8_xy[top_xy] + h->b8_stride;
610 *(uint32_t*)h->mv_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 0];
611 *(uint32_t*)h->mv_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 1];
612 *(uint32_t*)h->mv_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 2];
613 *(uint32_t*)h->mv_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy + 3];
614 h->ref_cache[list][scan8[0] + 0 - 1*8]=
615 h->ref_cache[list][scan8[0] + 1 - 1*8]= s->current_picture.ref_index[list][b8_xy + 0];
616 h->ref_cache[list][scan8[0] + 2 - 1*8]=
617 h->ref_cache[list][scan8[0] + 3 - 1*8]= s->current_picture.ref_index[list][b8_xy + 1];
619 *(uint32_t*)h->mv_cache [list][scan8[0] + 0 - 1*8]=
620 *(uint32_t*)h->mv_cache [list][scan8[0] + 1 - 1*8]=
621 *(uint32_t*)h->mv_cache [list][scan8[0] + 2 - 1*8]=
622 *(uint32_t*)h->mv_cache [list][scan8[0] + 3 - 1*8]= 0;
623 *(uint32_t*)&h->ref_cache[list][scan8[0] + 0 - 1*8]= ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE)&0xFF)*0x01010101;
626 if(IS_INTER(topright_type)){
627 const int b_xy= h->mb2b_xy[topright_xy] + 3*h->b_stride;
628 const int b8_xy= h->mb2b8_xy[topright_xy] + h->b8_stride;
629 *(uint32_t*)h->mv_cache[list][scan8[0] + 4 - 1*8]= *(uint32_t*)s->current_picture.motion_val[list][b_xy];
630 h->ref_cache[list][scan8[0] + 4 - 1*8]= s->current_picture.ref_index[list][b8_xy];
632 *(uint32_t*)h->mv_cache [list][scan8[0] + 4 - 1*8]= 0;
633 h->ref_cache[list][scan8[0] + 4 - 1*8]= topright_type ? LIST_NOT_USED : PART_NOT_AVAILABLE;
636 //FIXME unify cleanup or sth
637 if(IS_INTER(left_type[0])){
638 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
639 const int b8_xy= h->mb2b8_xy[left_xy[0]] + 1;
640 *(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]];
641 *(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]];
642 h->ref_cache[list][scan8[0] - 1 + 0*8]=
643 h->ref_cache[list][scan8[0] - 1 + 1*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[0]>>1)];
645 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 0*8]=
646 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 1*8]= 0;
647 h->ref_cache[list][scan8[0] - 1 + 0*8]=
648 h->ref_cache[list][scan8[0] - 1 + 1*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
651 if(IS_INTER(left_type[1])){
652 const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
653 const int b8_xy= h->mb2b8_xy[left_xy[1]] + 1;
654 *(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]];
655 *(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]];
656 h->ref_cache[list][scan8[0] - 1 + 2*8]=
657 h->ref_cache[list][scan8[0] - 1 + 3*8]= s->current_picture.ref_index[list][b8_xy + h->b8_stride*(left_block[2]>>1)];
659 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 2*8]=
660 *(uint32_t*)h->mv_cache [list][scan8[0] - 1 + 3*8]= 0;
661 h->ref_cache[list][scan8[0] - 1 + 2*8]=
662 h->ref_cache[list][scan8[0] - 1 + 3*8]= left_type[0] ? LIST_NOT_USED : PART_NOT_AVAILABLE;
668 h->ref_cache[list][scan8[5 ]+1] =
669 h->ref_cache[list][scan8[7 ]+1] =
670 h->ref_cache[list][scan8[13]+1] = //FIXME remove past 3 (init somewher else)
671 h->ref_cache[list][scan8[4 ]] =
672 h->ref_cache[list][scan8[12]] = PART_NOT_AVAILABLE;
673 *(uint32_t*)h->mv_cache [list][scan8[5 ]+1]=
674 *(uint32_t*)h->mv_cache [list][scan8[7 ]+1]=
675 *(uint32_t*)h->mv_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewher else)
676 *(uint32_t*)h->mv_cache [list][scan8[4 ]]=
677 *(uint32_t*)h->mv_cache [list][scan8[12]]= 0;
680 /* XXX beurk, Load mvd */
681 if(IS_INTER(topleft_type)){
682 const int b_xy = h->mb2b_xy[topleft_xy] + 3 + 3*h->b_stride;
683 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy];
685 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 - 1*8]= 0;
688 if(IS_INTER(top_type)){
689 const int b_xy= h->mb2b_xy[top_xy] + 3*h->b_stride;
690 *(uint32_t*)h->mvd_cache[list][scan8[0] + 0 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 0];
691 *(uint32_t*)h->mvd_cache[list][scan8[0] + 1 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 1];
692 *(uint32_t*)h->mvd_cache[list][scan8[0] + 2 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 2];
693 *(uint32_t*)h->mvd_cache[list][scan8[0] + 3 - 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + 3];
695 *(uint32_t*)h->mvd_cache [list][scan8[0] + 0 - 1*8]=
696 *(uint32_t*)h->mvd_cache [list][scan8[0] + 1 - 1*8]=
697 *(uint32_t*)h->mvd_cache [list][scan8[0] + 2 - 1*8]=
698 *(uint32_t*)h->mvd_cache [list][scan8[0] + 3 - 1*8]= 0;
700 if(IS_INTER(left_type[0])){
701 const int b_xy= h->mb2b_xy[left_xy[0]] + 3;
702 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 0*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[0]];
703 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 1*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[1]];
705 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 0*8]=
706 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 1*8]= 0;
708 if(IS_INTER(left_type[1])){
709 const int b_xy= h->mb2b_xy[left_xy[1]] + 3;
710 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 2*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[2]];
711 *(uint32_t*)h->mvd_cache[list][scan8[0] - 1 + 3*8]= *(uint32_t*)h->mvd_table[list][b_xy + h->b_stride*left_block[3]];
713 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 2*8]=
714 *(uint32_t*)h->mvd_cache [list][scan8[0] - 1 + 3*8]= 0;
716 *(uint32_t*)h->mvd_cache [list][scan8[5 ]+1]=
717 *(uint32_t*)h->mvd_cache [list][scan8[7 ]+1]=
718 *(uint32_t*)h->mvd_cache [list][scan8[13]+1]= //FIXME remove past 3 (init somewher else)
719 *(uint32_t*)h->mvd_cache [list][scan8[4 ]]=
720 *(uint32_t*)h->mvd_cache [list][scan8[12]]= 0;
722 if(h->slice_type == B_TYPE){
723 fill_rectangle(&h->direct_cache[scan8[0]], 4, 4, 8, 0, 1);
725 if(IS_DIRECT(top_type)){
726 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0x01010101;
727 }else if(IS_8X8(top_type)){
728 int b8_xy = h->mb2b8_xy[top_xy] + h->b8_stride;
729 h->direct_cache[scan8[0] + 0 - 1*8]= h->direct_table[b8_xy];
730 h->direct_cache[scan8[0] + 2 - 1*8]= h->direct_table[b8_xy + 1];
732 *(uint32_t*)&h->direct_cache[scan8[0] - 1*8]= 0;
736 if(IS_DIRECT(left_type[0])){
737 h->direct_cache[scan8[0] - 1 + 0*8]=
738 h->direct_cache[scan8[0] - 1 + 2*8]= 1;
739 }else if(IS_8X8(left_type[0])){
740 int b8_xy = h->mb2b8_xy[left_xy[0]] + 1;
741 h->direct_cache[scan8[0] - 1 + 0*8]= h->direct_table[b8_xy];
742 h->direct_cache[scan8[0] - 1 + 2*8]= h->direct_table[b8_xy + h->b8_stride];
744 h->direct_cache[scan8[0] - 1 + 0*8]=
745 h->direct_cache[scan8[0] - 1 + 2*8]= 0;
754 static inline void write_back_intra_pred_mode(H264Context *h){
755 MpegEncContext * const s = &h->s;
756 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
758 h->intra4x4_pred_mode[mb_xy][0]= h->intra4x4_pred_mode_cache[7+8*1];
759 h->intra4x4_pred_mode[mb_xy][1]= h->intra4x4_pred_mode_cache[7+8*2];
760 h->intra4x4_pred_mode[mb_xy][2]= h->intra4x4_pred_mode_cache[7+8*3];
761 h->intra4x4_pred_mode[mb_xy][3]= h->intra4x4_pred_mode_cache[7+8*4];
762 h->intra4x4_pred_mode[mb_xy][4]= h->intra4x4_pred_mode_cache[4+8*4];
763 h->intra4x4_pred_mode[mb_xy][5]= h->intra4x4_pred_mode_cache[5+8*4];
764 h->intra4x4_pred_mode[mb_xy][6]= h->intra4x4_pred_mode_cache[6+8*4];
768 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
770 static inline int check_intra4x4_pred_mode(H264Context *h){
771 MpegEncContext * const s = &h->s;
772 static const int8_t top [12]= {-1, 0,LEFT_DC_PRED,-1,-1,-1,-1,-1, 0};
773 static const int8_t left[12]= { 0,-1, TOP_DC_PRED, 0,-1,-1,-1, 0,-1,DC_128_PRED};
776 if(!(h->top_samples_available&0x8000)){
778 int status= top[ h->intra4x4_pred_mode_cache[scan8[0] + i] ];
780 av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
783 h->intra4x4_pred_mode_cache[scan8[0] + i]= status;
788 if(!(h->left_samples_available&0x8000)){
790 int status= left[ h->intra4x4_pred_mode_cache[scan8[0] + 8*i] ];
792 av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra4x4 mode %d at %d %d\n", status, s->mb_x, s->mb_y);
795 h->intra4x4_pred_mode_cache[scan8[0] + 8*i]= status;
801 } //FIXME cleanup like next
804 * checks if the top & left blocks are available if needed & changes the dc mode so it only uses the available blocks.
806 static inline int check_intra_pred_mode(H264Context *h, int mode){
807 MpegEncContext * const s = &h->s;
808 static const int8_t top [7]= {LEFT_DC_PRED8x8, 1,-1,-1};
809 static const int8_t left[7]= { TOP_DC_PRED8x8,-1, 2,-1,DC_128_PRED8x8};
811 if(mode < 0 || mode > 6) {
812 av_log(h->s.avctx, AV_LOG_ERROR, "out of range intra chroma pred mode at %d %d\n", s->mb_x, s->mb_y);
816 if(!(h->top_samples_available&0x8000)){
819 av_log(h->s.avctx, AV_LOG_ERROR, "top block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
824 if(!(h->left_samples_available&0x8000)){
827 av_log(h->s.avctx, AV_LOG_ERROR, "left block unavailable for requested intra mode at %d %d\n", s->mb_x, s->mb_y);
836 * gets the predicted intra4x4 prediction mode.
838 static inline int pred_intra_mode(H264Context *h, int n){
839 const int index8= scan8[n];
840 const int left= h->intra4x4_pred_mode_cache[index8 - 1];
841 const int top = h->intra4x4_pred_mode_cache[index8 - 8];
842 const int min= FFMIN(left, top);
844 tprintf("mode:%d %d min:%d\n", left ,top, min);
846 if(min<0) return DC_PRED;
850 static inline void write_back_non_zero_count(H264Context *h){
851 MpegEncContext * const s = &h->s;
852 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
854 h->non_zero_count[mb_xy][0]= h->non_zero_count_cache[4+8*4];
855 h->non_zero_count[mb_xy][1]= h->non_zero_count_cache[5+8*4];
856 h->non_zero_count[mb_xy][2]= h->non_zero_count_cache[6+8*4];
857 h->non_zero_count[mb_xy][3]= h->non_zero_count_cache[7+8*4];
858 h->non_zero_count[mb_xy][4]= h->non_zero_count_cache[7+8*3];
859 h->non_zero_count[mb_xy][5]= h->non_zero_count_cache[7+8*2];
860 h->non_zero_count[mb_xy][6]= h->non_zero_count_cache[7+8*1];
862 h->non_zero_count[mb_xy][7]= h->non_zero_count_cache[1+8*2];
863 h->non_zero_count[mb_xy][8]= h->non_zero_count_cache[2+8*2];
864 h->non_zero_count[mb_xy][9]= h->non_zero_count_cache[2+8*1];
866 h->non_zero_count[mb_xy][10]=h->non_zero_count_cache[1+8*5];
867 h->non_zero_count[mb_xy][11]=h->non_zero_count_cache[2+8*5];
868 h->non_zero_count[mb_xy][12]=h->non_zero_count_cache[2+8*4];
872 * gets the predicted number of non zero coefficients.
873 * @param n block index
875 static inline int pred_non_zero_count(H264Context *h, int n){
876 const int index8= scan8[n];
877 const int left= h->non_zero_count_cache[index8 - 1];
878 const int top = h->non_zero_count_cache[index8 - 8];
881 if(i<64) i= (i+1)>>1;
883 tprintf("pred_nnz L%X T%X n%d s%d P%X\n", left, top, n, scan8[n], i&31);
888 static inline int fetch_diagonal_mv(H264Context *h, const int16_t **C, int i, int list, int part_width){
889 const int topright_ref= h->ref_cache[list][ i - 8 + part_width ];
891 if(topright_ref != PART_NOT_AVAILABLE){
892 *C= h->mv_cache[list][ i - 8 + part_width ];
895 tprintf("topright MV not available\n");
897 *C= h->mv_cache[list][ i - 8 - 1 ];
898 return h->ref_cache[list][ i - 8 - 1 ];
903 * gets the predicted MV.
904 * @param n the block index
905 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
906 * @param mx the x component of the predicted motion vector
907 * @param my the y component of the predicted motion vector
909 static inline void pred_motion(H264Context * const h, int n, int part_width, int list, int ref, int * const mx, int * const my){
910 const int index8= scan8[n];
911 const int top_ref= h->ref_cache[list][ index8 - 8 ];
912 const int left_ref= h->ref_cache[list][ index8 - 1 ];
913 const int16_t * const A= h->mv_cache[list][ index8 - 1 ];
914 const int16_t * const B= h->mv_cache[list][ index8 - 8 ];
916 int diagonal_ref, match_count;
918 assert(part_width==1 || part_width==2 || part_width==4);
928 diagonal_ref= fetch_diagonal_mv(h, &C, index8, list, part_width);
929 match_count= (diagonal_ref==ref) + (top_ref==ref) + (left_ref==ref);
930 tprintf("pred_motion match_count=%d\n", match_count);
931 if(match_count > 1){ //most common
932 *mx= mid_pred(A[0], B[0], C[0]);
933 *my= mid_pred(A[1], B[1], C[1]);
934 }else if(match_count==1){
938 }else if(top_ref==ref){
946 if(top_ref == PART_NOT_AVAILABLE && diagonal_ref == PART_NOT_AVAILABLE && left_ref != PART_NOT_AVAILABLE){
950 *mx= mid_pred(A[0], B[0], C[0]);
951 *my= mid_pred(A[1], B[1], C[1]);
955 tprintf("pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref, A[0], A[1], ref, *mx, *my, h->s.mb_x, h->s.mb_y, n, list);
959 * gets the directionally predicted 16x8 MV.
960 * @param n the block index
961 * @param mx the x component of the predicted motion vector
962 * @param my the y component of the predicted motion vector
964 static inline void pred_16x8_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
966 const int top_ref= h->ref_cache[list][ scan8[0] - 8 ];
967 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
969 tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", top_ref, B[0], B[1], h->s.mb_x, h->s.mb_y, n, list);
977 const int left_ref= h->ref_cache[list][ scan8[8] - 1 ];
978 const int16_t * const A= h->mv_cache[list][ scan8[8] - 1 ];
980 tprintf("pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
990 pred_motion(h, n, 4, list, ref, mx, my);
994 * gets the directionally predicted 8x16 MV.
995 * @param n the block index
996 * @param mx the x component of the predicted motion vector
997 * @param my the y component of the predicted motion vector
999 static inline void pred_8x16_motion(H264Context * const h, int n, int list, int ref, int * const mx, int * const my){
1001 const int left_ref= h->ref_cache[list][ scan8[0] - 1 ];
1002 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
1004 tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", left_ref, A[0], A[1], h->s.mb_x, h->s.mb_y, n, list);
1006 if(left_ref == ref){
1015 diagonal_ref= fetch_diagonal_mv(h, &C, scan8[4], list, 2);
1017 tprintf("pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n", diagonal_ref, C[0], C[1], h->s.mb_x, h->s.mb_y, n, list);
1019 if(diagonal_ref == ref){
1027 pred_motion(h, n, 2, list, ref, mx, my);
1030 static inline void pred_pskip_motion(H264Context * const h, int * const mx, int * const my){
1031 const int top_ref = h->ref_cache[0][ scan8[0] - 8 ];
1032 const int left_ref= h->ref_cache[0][ scan8[0] - 1 ];
1034 tprintf("pred_pskip: (%d) (%d) at %2d %2d\n", top_ref, left_ref, h->s.mb_x, h->s.mb_y);
1036 if(top_ref == PART_NOT_AVAILABLE || left_ref == PART_NOT_AVAILABLE
1037 || (top_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 8 ] == 0)
1038 || (left_ref == 0 && *(uint32_t*)h->mv_cache[0][ scan8[0] - 1 ] == 0)){
1044 pred_motion(h, 0, 4, 0, 0, mx, my);
1049 static inline void direct_dist_scale_factor(H264Context * const h){
1050 const int poc = h->s.current_picture_ptr->poc;
1051 const int poc1 = h->ref_list[1][0].poc;
1053 for(i=0; i<h->ref_count[0]; i++){
1054 int poc0 = h->ref_list[0][i].poc;
1055 int td = clip(poc1 - poc0, -128, 127);
1056 if(td == 0 /* FIXME || pic0 is a long-term ref */){
1057 h->dist_scale_factor[i] = 256;
1059 int tb = clip(poc - poc0, -128, 127);
1060 int tx = (16384 + (ABS(td) >> 1)) / td;
1061 h->dist_scale_factor[i] = clip((tb*tx + 32) >> 6, -1024, 1023);
1065 static inline void direct_ref_list_init(H264Context * const h){
1066 MpegEncContext * const s = &h->s;
1067 Picture * const ref1 = &h->ref_list[1][0];
1068 Picture * const cur = s->current_picture_ptr;
1070 if(cur->pict_type == I_TYPE)
1071 cur->ref_count[0] = 0;
1072 if(cur->pict_type != B_TYPE)
1073 cur->ref_count[1] = 0;
1074 for(list=0; list<2; list++){
1075 cur->ref_count[list] = h->ref_count[list];
1076 for(j=0; j<h->ref_count[list]; j++)
1077 cur->ref_poc[list][j] = h->ref_list[list][j].poc;
1079 if(cur->pict_type != B_TYPE || h->direct_spatial_mv_pred)
1081 for(list=0; list<2; list++){
1082 for(i=0; i<ref1->ref_count[list]; i++){
1083 const int poc = ref1->ref_poc[list][i];
1084 h->map_col_to_list0[list][i] = PART_NOT_AVAILABLE;
1085 for(j=0; j<h->ref_count[list]; j++)
1086 if(h->ref_list[list][j].poc == poc){
1087 h->map_col_to_list0[list][i] = j;
1094 static inline void pred_direct_motion(H264Context * const h, int *mb_type){
1095 MpegEncContext * const s = &h->s;
1096 const int mb_xy = s->mb_x + s->mb_y*s->mb_stride;
1097 const int b8_xy = 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1098 const int b4_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1099 const int mb_type_col = h->ref_list[1][0].mb_type[mb_xy];
1100 const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->ref_list[1][0].motion_val[0][b4_xy];
1101 const int8_t *l1ref0 = &h->ref_list[1][0].ref_index[0][b8_xy];
1102 const int8_t *l1ref1 = &h->ref_list[1][0].ref_index[1][b8_xy];
1103 const int is_b8x8 = IS_8X8(*mb_type);
1107 if(IS_8X8(mb_type_col) && !h->sps.direct_8x8_inference_flag){
1108 /* FIXME save sub mb types from previous frames (or derive from MVs)
1109 * so we know exactly what block size to use */
1110 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
1111 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
1112 }else if(!is_b8x8 && (IS_16X16(mb_type_col) || IS_INTRA(mb_type_col))){
1113 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
1114 *mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
1116 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
1117 *mb_type = MB_TYPE_8x8|MB_TYPE_L0L1;
1120 *mb_type |= MB_TYPE_DIRECT2;
1122 tprintf("mb_type = %08x, sub_mb_type = %08x, is_b8x8 = %d, mb_type_col = %08x\n", *mb_type, sub_mb_type, is_b8x8, mb_type_col);
1124 if(h->direct_spatial_mv_pred){
1129 /* ref = min(neighbors) */
1130 for(list=0; list<2; list++){
1131 int refa = h->ref_cache[list][scan8[0] - 1];
1132 int refb = h->ref_cache[list][scan8[0] - 8];
1133 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
1135 refc = h->ref_cache[list][scan8[0] - 8 - 1];
1137 if(ref[list] < 0 || (refb < ref[list] && refb >= 0))
1139 if(ref[list] < 0 || (refc < ref[list] && refc >= 0))
1145 if(ref[0] < 0 && ref[1] < 0){
1146 ref[0] = ref[1] = 0;
1147 mv[0][0] = mv[0][1] =
1148 mv[1][0] = mv[1][1] = 0;
1150 for(list=0; list<2; list++){
1152 pred_motion(h, 0, 4, list, ref[list], &mv[list][0], &mv[list][1]);
1154 mv[list][0] = mv[list][1] = 0;
1159 *mb_type &= ~MB_TYPE_P0L1;
1160 sub_mb_type &= ~MB_TYPE_P0L1;
1161 }else if(ref[0] < 0){
1162 *mb_type &= ~MB_TYPE_P0L0;
1163 sub_mb_type &= ~MB_TYPE_P0L0;
1166 if(IS_16X16(*mb_type)){
1167 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref[0], 1);
1168 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, ref[1], 1);
1169 if(!IS_INTRA(mb_type_col) && l1ref0[0] == 0 &&
1170 ABS(l1mv0[0][0]) <= 1 && ABS(l1mv0[0][1]) <= 1){
1172 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1174 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
1176 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1178 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
1180 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1181 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1184 for(i8=0; i8<4; i8++){
1185 const int x8 = i8&1;
1186 const int y8 = i8>>1;
1188 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1190 h->sub_mb_type[i8] = sub_mb_type;
1192 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mv[0][0],mv[0][1]), 4);
1193 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mv[1][0],mv[1][1]), 4);
1194 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref[0], 1);
1195 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, ref[1], 1);
1198 if(!IS_INTRA(mb_type_col) && l1ref0[x8 + y8*h->b8_stride] == 0){
1199 for(i4=0; i4<4; i4++){
1200 const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
1201 if(ABS(mv_col[0]) <= 1 && ABS(mv_col[1]) <= 1){
1203 *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
1205 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
1211 }else{ /* direct temporal mv pred */
1212 if(IS_16X16(*mb_type)){
1213 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
1214 if(IS_INTRA(mb_type_col)){
1215 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
1216 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
1217 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
1219 const int ref0 = l1ref0[0] >= 0 ? h->map_col_to_list0[0][l1ref0[0]]
1220 : h->map_col_to_list0[1][l1ref1[0]];
1222 const int dist_scale_factor = h->dist_scale_factor[ref0];
1223 const int16_t *mv_col = l1mv0[0];
1225 mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
1226 mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
1227 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref0, 1);
1228 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0],mv_l0[1]), 4);
1229 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]), 4);
1232 for(i8=0; i8<4; i8++){
1233 const int x8 = i8&1;
1234 const int y8 = i8>>1;
1235 int ref0, dist_scale_factor;
1237 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
1239 h->sub_mb_type[i8] = sub_mb_type;
1240 if(IS_INTRA(mb_type_col)){
1241 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
1242 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1243 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
1244 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
1248 ref0 = l1ref0[x8 + y8*h->b8_stride];
1250 ref0 = h->map_col_to_list0[0][ref0];
1252 ref0 = h->map_col_to_list0[1][l1ref1[x8 + y8*h->b8_stride]];
1254 dist_scale_factor = h->dist_scale_factor[ref0];
1256 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
1257 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
1258 for(i4=0; i4<4; i4++){
1259 const int16_t *mv_col = l1mv0[x8*2 + (i4&1) + (y8*2 + (i4>>1))*h->b_stride];
1260 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
1261 mv_l0[0] = (dist_scale_factor * mv_col[0] + 128) >> 8;
1262 mv_l0[1] = (dist_scale_factor * mv_col[1] + 128) >> 8;
1263 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
1264 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
1271 static inline void write_back_motion(H264Context *h, int mb_type){
1272 MpegEncContext * const s = &h->s;
1273 const int b_xy = 4*s->mb_x + 4*s->mb_y*h->b_stride;
1274 const int b8_xy= 2*s->mb_x + 2*s->mb_y*h->b8_stride;
1277 for(list=0; list<2; list++){
1279 if(!USES_LIST(mb_type, list)){
1280 if(1){ //FIXME skip or never read if mb_type doesnt use it
1282 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 0 + y*h->b_stride]=
1283 *(uint64_t*)s->current_picture.motion_val[list][b_xy + 2 + y*h->b_stride]= 0;
1285 if( h->pps.cabac ) {
1286 /* FIXME needed ? */
1288 *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]=
1289 *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= 0;
1293 *(uint16_t*)&s->current_picture.ref_index[list][b8_xy + y*h->b8_stride]= (LIST_NOT_USED&0xFF)*0x0101;
1300 *(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];
1301 *(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];
1303 if( h->pps.cabac ) {
1305 *(uint64_t*)h->mvd_table[list][b_xy + 0 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+0 + 8*y];
1306 *(uint64_t*)h->mvd_table[list][b_xy + 2 + y*h->b_stride]= *(uint64_t*)h->mvd_cache[list][scan8[0]+2 + 8*y];
1310 s->current_picture.ref_index[list][b8_xy + 0 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+0 + 16*y];
1311 s->current_picture.ref_index[list][b8_xy + 1 + y*h->b8_stride]= h->ref_cache[list][scan8[0]+2 + 16*y];
1315 if(h->slice_type == B_TYPE && h->pps.cabac){
1316 if(IS_8X8(mb_type)){
1317 h->direct_table[b8_xy+1+0*h->b8_stride] = IS_DIRECT(h->sub_mb_type[1]) ? 1 : 0;
1318 h->direct_table[b8_xy+0+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[2]) ? 1 : 0;
1319 h->direct_table[b8_xy+1+1*h->b8_stride] = IS_DIRECT(h->sub_mb_type[3]) ? 1 : 0;
1325 * Decodes a network abstraction layer unit.
1326 * @param consumed is the number of bytes used as input
1327 * @param length is the length of the array
1328 * @param dst_length is the number of decoded bytes FIXME here or a decode rbsp ttailing?
1329 * @returns decoded bytes, might be src+1 if no escapes
1331 static uint8_t *decode_nal(H264Context *h, uint8_t *src, int *dst_length, int *consumed, int length){
1335 // src[0]&0x80; //forbidden bit
1336 h->nal_ref_idc= src[0]>>5;
1337 h->nal_unit_type= src[0]&0x1F;
1341 for(i=0; i<length; i++)
1342 printf("%2X ", src[i]);
1344 for(i=0; i+1<length; i+=2){
1345 if(src[i]) continue;
1346 if(i>0 && src[i-1]==0) i--;
1347 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
1349 /* startcode, so we must be past the end */
1356 if(i>=length-1){ //no escaped 0
1357 *dst_length= length;
1358 *consumed= length+1; //+1 for the header
1362 h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length);
1363 dst= h->rbsp_buffer;
1365 //printf("deoding esc\n");
1368 //remove escapes (very rare 1:2^22)
1369 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
1370 if(src[si+2]==3){ //escape
1375 }else //next start code
1379 dst[di++]= src[si++];
1383 *consumed= si + 1;//+1 for the header
1384 //FIXME store exact number of bits in the getbitcontext (its needed for decoding)
1390 * @param src the data which should be escaped
1391 * @param dst the target buffer, dst+1 == src is allowed as a special case
1392 * @param length the length of the src data
1393 * @param dst_length the length of the dst array
1394 * @returns length of escaped data in bytes or -1 if an error occured
1396 static int encode_nal(H264Context *h, uint8_t *dst, uint8_t *src, int length, int dst_length){
1397 int i, escape_count, si, di;
1401 assert(dst_length>0);
1403 dst[0]= (h->nal_ref_idc<<5) + h->nal_unit_type;
1405 if(length==0) return 1;
1408 for(i=0; i<length; i+=2){
1409 if(src[i]) continue;
1410 if(i>0 && src[i-1]==0)
1412 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
1418 if(escape_count==0){
1420 memcpy(dst+1, src, length);
1424 if(length + escape_count + 1> dst_length)
1427 //this should be damn rare (hopefully)
1429 h->rbsp_buffer= av_fast_realloc(h->rbsp_buffer, &h->rbsp_buffer_size, length + escape_count);
1430 temp= h->rbsp_buffer;
1431 //printf("encoding esc\n");
1436 if(si+2<length && src[si]==0 && src[si+1]==0 && src[si+2]<=3){
1437 temp[di++]= 0; si++;
1438 temp[di++]= 0; si++;
1440 temp[di++]= src[si++];
1443 temp[di++]= src[si++];
1445 memcpy(dst+1, temp, length+escape_count);
1447 assert(di == length+escape_count);
1453 * write 1,10,100,1000,... for alignment, yes its exactly inverse to mpeg4
1455 static void encode_rbsp_trailing(PutBitContext *pb){
1458 length= (-put_bits_count(pb))&7;
1459 if(length) put_bits(pb, length, 0);
1464 * identifies the exact end of the bitstream
1465 * @return the length of the trailing, or 0 if damaged
1467 static int decode_rbsp_trailing(uint8_t *src){
1471 tprintf("rbsp trailing %X\n", v);
1481 * idct tranforms the 16 dc values and dequantize them.
1482 * @param qp quantization parameter
1484 static void h264_luma_dc_dequant_idct_c(DCTELEM *block, int qp){
1485 const int qmul= dequant_coeff[qp][0];
1488 int temp[16]; //FIXME check if this is a good idea
1489 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
1490 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1492 //memset(block, 64, 2*256);
1495 const int offset= y_offset[i];
1496 const int z0= block[offset+stride*0] + block[offset+stride*4];
1497 const int z1= block[offset+stride*0] - block[offset+stride*4];
1498 const int z2= block[offset+stride*1] - block[offset+stride*5];
1499 const int z3= block[offset+stride*1] + block[offset+stride*5];
1508 const int offset= x_offset[i];
1509 const int z0= temp[4*0+i] + temp[4*2+i];
1510 const int z1= temp[4*0+i] - temp[4*2+i];
1511 const int z2= temp[4*1+i] - temp[4*3+i];
1512 const int z3= temp[4*1+i] + temp[4*3+i];
1514 block[stride*0 +offset]= ((z0 + z3)*qmul + 2)>>2; //FIXME think about merging this into decode_resdual
1515 block[stride*2 +offset]= ((z1 + z2)*qmul + 2)>>2;
1516 block[stride*8 +offset]= ((z1 - z2)*qmul + 2)>>2;
1517 block[stride*10+offset]= ((z0 - z3)*qmul + 2)>>2;
1523 * dct tranforms the 16 dc values.
1524 * @param qp quantization parameter ??? FIXME
1526 static void h264_luma_dc_dct_c(DCTELEM *block/*, int qp*/){
1527 // const int qmul= dequant_coeff[qp][0];
1529 int temp[16]; //FIXME check if this is a good idea
1530 static const int x_offset[4]={0, 1*stride, 4* stride, 5*stride};
1531 static const int y_offset[4]={0, 2*stride, 8* stride, 10*stride};
1534 const int offset= y_offset[i];
1535 const int z0= block[offset+stride*0] + block[offset+stride*4];
1536 const int z1= block[offset+stride*0] - block[offset+stride*4];
1537 const int z2= block[offset+stride*1] - block[offset+stride*5];
1538 const int z3= block[offset+stride*1] + block[offset+stride*5];
1547 const int offset= x_offset[i];
1548 const int z0= temp[4*0+i] + temp[4*2+i];
1549 const int z1= temp[4*0+i] - temp[4*2+i];
1550 const int z2= temp[4*1+i] - temp[4*3+i];
1551 const int z3= temp[4*1+i] + temp[4*3+i];
1553 block[stride*0 +offset]= (z0 + z3)>>1;
1554 block[stride*2 +offset]= (z1 + z2)>>1;
1555 block[stride*8 +offset]= (z1 - z2)>>1;
1556 block[stride*10+offset]= (z0 - z3)>>1;
1564 static void chroma_dc_dequant_idct_c(DCTELEM *block, int qp){
1565 const int qmul= dequant_coeff[qp][0];
1566 const int stride= 16*2;
1567 const int xStride= 16;
1570 a= block[stride*0 + xStride*0];
1571 b= block[stride*0 + xStride*1];
1572 c= block[stride*1 + xStride*0];
1573 d= block[stride*1 + xStride*1];
1580 block[stride*0 + xStride*0]= ((a+c)*qmul + 0)>>1;
1581 block[stride*0 + xStride*1]= ((e+b)*qmul + 0)>>1;
1582 block[stride*1 + xStride*0]= ((a-c)*qmul + 0)>>1;
1583 block[stride*1 + xStride*1]= ((e-b)*qmul + 0)>>1;
1587 static void chroma_dc_dct_c(DCTELEM *block){
1588 const int stride= 16*2;
1589 const int xStride= 16;
1592 a= block[stride*0 + xStride*0];
1593 b= block[stride*0 + xStride*1];
1594 c= block[stride*1 + xStride*0];
1595 d= block[stride*1 + xStride*1];
1602 block[stride*0 + xStride*0]= (a+c);
1603 block[stride*0 + xStride*1]= (e+b);
1604 block[stride*1 + xStride*0]= (a-c);
1605 block[stride*1 + xStride*1]= (e-b);
1610 * gets the chroma qp.
1612 static inline int get_chroma_qp(H264Context *h, int qscale){
1614 return chroma_qp[clip(qscale + h->pps.chroma_qp_index_offset, 0, 51)];
1619 static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
1621 //FIXME try int temp instead of block
1624 const int d0= src1[0 + i*stride] - src2[0 + i*stride];
1625 const int d1= src1[1 + i*stride] - src2[1 + i*stride];
1626 const int d2= src1[2 + i*stride] - src2[2 + i*stride];
1627 const int d3= src1[3 + i*stride] - src2[3 + i*stride];
1628 const int z0= d0 + d3;
1629 const int z3= d0 - d3;
1630 const int z1= d1 + d2;
1631 const int z2= d1 - d2;
1633 block[0 + 4*i]= z0 + z1;
1634 block[1 + 4*i]= 2*z3 + z2;
1635 block[2 + 4*i]= z0 - z1;
1636 block[3 + 4*i]= z3 - 2*z2;
1640 const int z0= block[0*4 + i] + block[3*4 + i];
1641 const int z3= block[0*4 + i] - block[3*4 + i];
1642 const int z1= block[1*4 + i] + block[2*4 + i];
1643 const int z2= block[1*4 + i] - block[2*4 + i];
1645 block[0*4 + i]= z0 + z1;
1646 block[1*4 + i]= 2*z3 + z2;
1647 block[2*4 + i]= z0 - z1;
1648 block[3*4 + i]= z3 - 2*z2;
1653 //FIXME need to check that this doesnt overflow signed 32 bit for low qp, iam not sure, its very close
1654 //FIXME check that gcc inlines this (and optimizes intra & seperate_dc stuff away)
1655 static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale, int intra, int seperate_dc){
1657 const int * const quant_table= quant_coeff[qscale];
1658 const int bias= intra ? (1<<QUANT_SHIFT)/3 : (1<<QUANT_SHIFT)/6;
1659 const unsigned int threshold1= (1<<QUANT_SHIFT) - bias - 1;
1660 const unsigned int threshold2= (threshold1<<1);
1666 const int dc_bias= intra ? (1<<(QUANT_SHIFT-2))/3 : (1<<(QUANT_SHIFT-2))/6;
1667 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT-2)) - dc_bias - 1;
1668 const unsigned int dc_threshold2= (dc_threshold1<<1);
1670 int level= block[0]*quant_coeff[qscale+18][0];
1671 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
1673 level= (dc_bias + level)>>(QUANT_SHIFT-2);
1676 level= (dc_bias - level)>>(QUANT_SHIFT-2);
1679 // last_non_zero = i;
1684 const int dc_bias= intra ? (1<<(QUANT_SHIFT+1))/3 : (1<<(QUANT_SHIFT+1))/6;
1685 const unsigned int dc_threshold1= (1<<(QUANT_SHIFT+1)) - dc_bias - 1;
1686 const unsigned int dc_threshold2= (dc_threshold1<<1);
1688 int level= block[0]*quant_table[0];
1689 if(((unsigned)(level+dc_threshold1))>dc_threshold2){
1691 level= (dc_bias + level)>>(QUANT_SHIFT+1);
1694 level= (dc_bias - level)>>(QUANT_SHIFT+1);
1697 // last_non_zero = i;
1710 const int j= scantable[i];
1711 int level= block[j]*quant_table[j];
1713 // if( bias+level >= (1<<(QMAT_SHIFT - 3))
1714 // || bias-level >= (1<<(QMAT_SHIFT - 3))){
1715 if(((unsigned)(level+threshold1))>threshold2){
1717 level= (bias + level)>>QUANT_SHIFT;
1720 level= (bias - level)>>QUANT_SHIFT;
1729 return last_non_zero;
1732 static void pred4x4_vertical_c(uint8_t *src, uint8_t *topright, int stride){
1733 const uint32_t a= ((uint32_t*)(src-stride))[0];
1734 ((uint32_t*)(src+0*stride))[0]= a;
1735 ((uint32_t*)(src+1*stride))[0]= a;
1736 ((uint32_t*)(src+2*stride))[0]= a;
1737 ((uint32_t*)(src+3*stride))[0]= a;
1740 static void pred4x4_horizontal_c(uint8_t *src, uint8_t *topright, int stride){
1741 ((uint32_t*)(src+0*stride))[0]= src[-1+0*stride]*0x01010101;
1742 ((uint32_t*)(src+1*stride))[0]= src[-1+1*stride]*0x01010101;
1743 ((uint32_t*)(src+2*stride))[0]= src[-1+2*stride]*0x01010101;
1744 ((uint32_t*)(src+3*stride))[0]= src[-1+3*stride]*0x01010101;
1747 static void pred4x4_dc_c(uint8_t *src, uint8_t *topright, int stride){
1748 const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
1749 + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
1751 ((uint32_t*)(src+0*stride))[0]=
1752 ((uint32_t*)(src+1*stride))[0]=
1753 ((uint32_t*)(src+2*stride))[0]=
1754 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
1757 static void pred4x4_left_dc_c(uint8_t *src, uint8_t *topright, int stride){
1758 const int dc= ( src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
1760 ((uint32_t*)(src+0*stride))[0]=
1761 ((uint32_t*)(src+1*stride))[0]=
1762 ((uint32_t*)(src+2*stride))[0]=
1763 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
1766 static void pred4x4_top_dc_c(uint8_t *src, uint8_t *topright, int stride){
1767 const int dc= ( src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
1769 ((uint32_t*)(src+0*stride))[0]=
1770 ((uint32_t*)(src+1*stride))[0]=
1771 ((uint32_t*)(src+2*stride))[0]=
1772 ((uint32_t*)(src+3*stride))[0]= dc* 0x01010101;
1775 static void pred4x4_128_dc_c(uint8_t *src, uint8_t *topright, int stride){
1776 ((uint32_t*)(src+0*stride))[0]=
1777 ((uint32_t*)(src+1*stride))[0]=
1778 ((uint32_t*)(src+2*stride))[0]=
1779 ((uint32_t*)(src+3*stride))[0]= 128U*0x01010101U;
1783 #define LOAD_TOP_RIGHT_EDGE\
1784 const int t4= topright[0];\
1785 const int t5= topright[1];\
1786 const int t6= topright[2];\
1787 const int t7= topright[3];\
1789 #define LOAD_LEFT_EDGE\
1790 const int l0= src[-1+0*stride];\
1791 const int l1= src[-1+1*stride];\
1792 const int l2= src[-1+2*stride];\
1793 const int l3= src[-1+3*stride];\
1795 #define LOAD_TOP_EDGE\
1796 const int t0= src[ 0-1*stride];\
1797 const int t1= src[ 1-1*stride];\
1798 const int t2= src[ 2-1*stride];\
1799 const int t3= src[ 3-1*stride];\
1801 static void pred4x4_down_right_c(uint8_t *src, uint8_t *topright, int stride){
1802 const int lt= src[-1-1*stride];
1806 src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
1808 src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
1811 src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
1815 src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
1818 src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
1820 src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1821 src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
1824 static void pred4x4_down_left_c(uint8_t *src, uint8_t *topright, int stride){
1829 src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
1831 src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
1834 src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
1838 src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
1841 src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
1843 src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
1844 src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
1847 static void pred4x4_vertical_right_c(uint8_t *src, uint8_t *topright, int stride){
1848 const int lt= src[-1-1*stride];
1851 const __attribute__((unused)) int unu= l3;
1854 src[1+2*stride]=(lt + t0 + 1)>>1;
1856 src[2+2*stride]=(t0 + t1 + 1)>>1;
1858 src[3+2*stride]=(t1 + t2 + 1)>>1;
1859 src[3+0*stride]=(t2 + t3 + 1)>>1;
1861 src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
1863 src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
1865 src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1866 src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
1867 src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
1868 src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
1871 static void pred4x4_vertical_left_c(uint8_t *src, uint8_t *topright, int stride){
1874 const __attribute__((unused)) int unu= t7;
1876 src[0+0*stride]=(t0 + t1 + 1)>>1;
1878 src[0+2*stride]=(t1 + t2 + 1)>>1;
1880 src[1+2*stride]=(t2 + t3 + 1)>>1;
1882 src[2+2*stride]=(t3 + t4+ 1)>>1;
1883 src[3+2*stride]=(t4 + t5+ 1)>>1;
1884 src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1886 src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
1888 src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
1890 src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
1891 src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
1894 static void pred4x4_horizontal_up_c(uint8_t *src, uint8_t *topright, int stride){
1897 src[0+0*stride]=(l0 + l1 + 1)>>1;
1898 src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
1900 src[0+1*stride]=(l1 + l2 + 1)>>1;
1902 src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
1904 src[0+2*stride]=(l2 + l3 + 1)>>1;
1906 src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
1915 static void pred4x4_horizontal_down_c(uint8_t *src, uint8_t *topright, int stride){
1916 const int lt= src[-1-1*stride];
1919 const __attribute__((unused)) int unu= t3;
1922 src[2+1*stride]=(lt + l0 + 1)>>1;
1924 src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
1925 src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
1926 src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
1928 src[2+2*stride]=(l0 + l1 + 1)>>1;
1930 src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
1932 src[2+3*stride]=(l1 + l2+ 1)>>1;
1934 src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
1935 src[0+3*stride]=(l2 + l3 + 1)>>1;
1936 src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
1939 static void pred16x16_vertical_c(uint8_t *src, int stride){
1941 const uint32_t a= ((uint32_t*)(src-stride))[0];
1942 const uint32_t b= ((uint32_t*)(src-stride))[1];
1943 const uint32_t c= ((uint32_t*)(src-stride))[2];
1944 const uint32_t d= ((uint32_t*)(src-stride))[3];
1946 for(i=0; i<16; i++){
1947 ((uint32_t*)(src+i*stride))[0]= a;
1948 ((uint32_t*)(src+i*stride))[1]= b;
1949 ((uint32_t*)(src+i*stride))[2]= c;
1950 ((uint32_t*)(src+i*stride))[3]= d;
1954 static void pred16x16_horizontal_c(uint8_t *src, int stride){
1957 for(i=0; i<16; i++){
1958 ((uint32_t*)(src+i*stride))[0]=
1959 ((uint32_t*)(src+i*stride))[1]=
1960 ((uint32_t*)(src+i*stride))[2]=
1961 ((uint32_t*)(src+i*stride))[3]= src[-1+i*stride]*0x01010101;
1965 static void pred16x16_dc_c(uint8_t *src, int stride){
1969 dc+= src[-1+i*stride];
1976 dc= 0x01010101*((dc + 16)>>5);
1978 for(i=0; i<16; i++){
1979 ((uint32_t*)(src+i*stride))[0]=
1980 ((uint32_t*)(src+i*stride))[1]=
1981 ((uint32_t*)(src+i*stride))[2]=
1982 ((uint32_t*)(src+i*stride))[3]= dc;
1986 static void pred16x16_left_dc_c(uint8_t *src, int stride){
1990 dc+= src[-1+i*stride];
1993 dc= 0x01010101*((dc + 8)>>4);
1995 for(i=0; i<16; i++){
1996 ((uint32_t*)(src+i*stride))[0]=
1997 ((uint32_t*)(src+i*stride))[1]=
1998 ((uint32_t*)(src+i*stride))[2]=
1999 ((uint32_t*)(src+i*stride))[3]= dc;
2003 static void pred16x16_top_dc_c(uint8_t *src, int stride){
2009 dc= 0x01010101*((dc + 8)>>4);
2011 for(i=0; i<16; i++){
2012 ((uint32_t*)(src+i*stride))[0]=
2013 ((uint32_t*)(src+i*stride))[1]=
2014 ((uint32_t*)(src+i*stride))[2]=
2015 ((uint32_t*)(src+i*stride))[3]= dc;
2019 static void pred16x16_128_dc_c(uint8_t *src, int stride){
2022 for(i=0; i<16; i++){
2023 ((uint32_t*)(src+i*stride))[0]=
2024 ((uint32_t*)(src+i*stride))[1]=
2025 ((uint32_t*)(src+i*stride))[2]=
2026 ((uint32_t*)(src+i*stride))[3]= 0x01010101U*128U;
2030 static inline void pred16x16_plane_compat_c(uint8_t *src, int stride, const int svq3){
2033 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2034 const uint8_t * const src0 = src+7-stride;
2035 const uint8_t *src1 = src+8*stride-1;
2036 const uint8_t *src2 = src1-2*stride; // == src+6*stride-1;
2037 int H = src0[1] - src0[-1];
2038 int V = src1[0] - src2[ 0];
2039 for(k=2; k<=8; ++k) {
2040 src1 += stride; src2 -= stride;
2041 H += k*(src0[k] - src0[-k]);
2042 V += k*(src1[0] - src2[ 0]);
2045 H = ( 5*(H/4) ) / 16;
2046 V = ( 5*(V/4) ) / 16;
2048 /* required for 100% accuracy */
2049 i = H; H = V; V = i;
2051 H = ( 5*H+32 ) >> 6;
2052 V = ( 5*V+32 ) >> 6;
2055 a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
2056 for(j=16; j>0; --j) {
2059 for(i=-16; i<0; i+=4) {
2060 src[16+i] = cm[ (b ) >> 5 ];
2061 src[17+i] = cm[ (b+ H) >> 5 ];
2062 src[18+i] = cm[ (b+2*H) >> 5 ];
2063 src[19+i] = cm[ (b+3*H) >> 5 ];
2070 static void pred16x16_plane_c(uint8_t *src, int stride){
2071 pred16x16_plane_compat_c(src, stride, 0);
2074 static void pred8x8_vertical_c(uint8_t *src, int stride){
2076 const uint32_t a= ((uint32_t*)(src-stride))[0];
2077 const uint32_t b= ((uint32_t*)(src-stride))[1];
2080 ((uint32_t*)(src+i*stride))[0]= a;
2081 ((uint32_t*)(src+i*stride))[1]= b;
2085 static void pred8x8_horizontal_c(uint8_t *src, int stride){
2089 ((uint32_t*)(src+i*stride))[0]=
2090 ((uint32_t*)(src+i*stride))[1]= src[-1+i*stride]*0x01010101;
2094 static void pred8x8_128_dc_c(uint8_t *src, int stride){
2098 ((uint32_t*)(src+i*stride))[0]=
2099 ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
2102 ((uint32_t*)(src+i*stride))[0]=
2103 ((uint32_t*)(src+i*stride))[1]= 0x01010101U*128U;
2107 static void pred8x8_left_dc_c(uint8_t *src, int stride){
2113 dc0+= src[-1+i*stride];
2114 dc2+= src[-1+(i+4)*stride];
2116 dc0= 0x01010101*((dc0 + 2)>>2);
2117 dc2= 0x01010101*((dc2 + 2)>>2);
2120 ((uint32_t*)(src+i*stride))[0]=
2121 ((uint32_t*)(src+i*stride))[1]= dc0;
2124 ((uint32_t*)(src+i*stride))[0]=
2125 ((uint32_t*)(src+i*stride))[1]= dc2;
2129 static void pred8x8_top_dc_c(uint8_t *src, int stride){
2135 dc0+= src[i-stride];
2136 dc1+= src[4+i-stride];
2138 dc0= 0x01010101*((dc0 + 2)>>2);
2139 dc1= 0x01010101*((dc1 + 2)>>2);
2142 ((uint32_t*)(src+i*stride))[0]= dc0;
2143 ((uint32_t*)(src+i*stride))[1]= dc1;
2146 ((uint32_t*)(src+i*stride))[0]= dc0;
2147 ((uint32_t*)(src+i*stride))[1]= dc1;
2152 static void pred8x8_dc_c(uint8_t *src, int stride){
2154 int dc0, dc1, dc2, dc3;
2158 dc0+= src[-1+i*stride] + src[i-stride];
2159 dc1+= src[4+i-stride];
2160 dc2+= src[-1+(i+4)*stride];
2162 dc3= 0x01010101*((dc1 + dc2 + 4)>>3);
2163 dc0= 0x01010101*((dc0 + 4)>>3);
2164 dc1= 0x01010101*((dc1 + 2)>>2);
2165 dc2= 0x01010101*((dc2 + 2)>>2);
2168 ((uint32_t*)(src+i*stride))[0]= dc0;
2169 ((uint32_t*)(src+i*stride))[1]= dc1;
2172 ((uint32_t*)(src+i*stride))[0]= dc2;
2173 ((uint32_t*)(src+i*stride))[1]= dc3;
2177 static void pred8x8_plane_c(uint8_t *src, int stride){
2180 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2181 const uint8_t * const src0 = src+3-stride;
2182 const uint8_t *src1 = src+4*stride-1;
2183 const uint8_t *src2 = src1-2*stride; // == src+2*stride-1;
2184 int H = src0[1] - src0[-1];
2185 int V = src1[0] - src2[ 0];
2186 for(k=2; k<=4; ++k) {
2187 src1 += stride; src2 -= stride;
2188 H += k*(src0[k] - src0[-k]);
2189 V += k*(src1[0] - src2[ 0]);
2191 H = ( 17*H+16 ) >> 5;
2192 V = ( 17*V+16 ) >> 5;
2194 a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
2195 for(j=8; j>0; --j) {
2198 src[0] = cm[ (b ) >> 5 ];
2199 src[1] = cm[ (b+ H) >> 5 ];
2200 src[2] = cm[ (b+2*H) >> 5 ];
2201 src[3] = cm[ (b+3*H) >> 5 ];
2202 src[4] = cm[ (b+4*H) >> 5 ];
2203 src[5] = cm[ (b+5*H) >> 5 ];
2204 src[6] = cm[ (b+6*H) >> 5 ];
2205 src[7] = cm[ (b+7*H) >> 5 ];
2210 static inline void mc_dir_part(H264Context *h, Picture *pic, int n, int square, int chroma_height, int delta, int list,
2211 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2212 int src_x_offset, int src_y_offset,
2213 qpel_mc_func *qpix_op, h264_chroma_mc_func chroma_op){
2214 MpegEncContext * const s = &h->s;
2215 const int mx= h->mv_cache[list][ scan8[n] ][0] + src_x_offset*8;
2216 const int my= h->mv_cache[list][ scan8[n] ][1] + src_y_offset*8;
2217 const int luma_xy= (mx&3) + ((my&3)<<2);
2218 uint8_t * src_y = pic->data[0] + (mx>>2) + (my>>2)*s->linesize;
2219 uint8_t * src_cb= pic->data[1] + (mx>>3) + (my>>3)*s->uvlinesize;
2220 uint8_t * src_cr= pic->data[2] + (mx>>3) + (my>>3)*s->uvlinesize;
2221 int extra_width= (s->flags&CODEC_FLAG_EMU_EDGE) ? 0 : 16; //FIXME increase edge?, IMHO not worth it
2222 int extra_height= extra_width;
2224 const int full_mx= mx>>2;
2225 const int full_my= my>>2;
2227 assert(pic->data[0]);
2229 if(mx&7) extra_width -= 3;
2230 if(my&7) extra_height -= 3;
2232 if( full_mx < 0-extra_width
2233 || full_my < 0-extra_height
2234 || full_mx + 16/*FIXME*/ > s->width + extra_width
2235 || full_my + 16/*FIXME*/ > s->height + extra_height){
2236 ff_emulated_edge_mc(s->edge_emu_buffer, src_y - 2 - 2*s->linesize, s->linesize, 16+5, 16+5/*FIXME*/, full_mx-2, full_my-2, s->width, s->height);
2237 src_y= s->edge_emu_buffer + 2 + 2*s->linesize;
2241 qpix_op[luma_xy](dest_y, src_y, s->linesize); //FIXME try variable height perhaps?
2243 qpix_op[luma_xy](dest_y + delta, src_y + delta, s->linesize);
2246 if(s->flags&CODEC_FLAG_GRAY) return;
2249 ff_emulated_edge_mc(s->edge_emu_buffer, src_cb, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
2250 src_cb= s->edge_emu_buffer;
2252 chroma_op(dest_cb, src_cb, s->uvlinesize, chroma_height, mx&7, my&7);
2255 ff_emulated_edge_mc(s->edge_emu_buffer, src_cr, s->uvlinesize, 9, 9/*FIXME*/, (mx>>3), (my>>3), s->width>>1, s->height>>1);
2256 src_cr= s->edge_emu_buffer;
2258 chroma_op(dest_cr, src_cr, s->uvlinesize, chroma_height, mx&7, my&7);
2261 static inline void mc_part_std(H264Context *h, int n, int square, int chroma_height, int delta,
2262 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2263 int x_offset, int y_offset,
2264 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
2265 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
2266 int list0, int list1){
2267 MpegEncContext * const s = &h->s;
2268 qpel_mc_func *qpix_op= qpix_put;
2269 h264_chroma_mc_func chroma_op= chroma_put;
2271 dest_y += 2*x_offset + 2*y_offset*s-> linesize;
2272 dest_cb += x_offset + y_offset*s->uvlinesize;
2273 dest_cr += x_offset + y_offset*s->uvlinesize;
2274 x_offset += 8*s->mb_x;
2275 y_offset += 8*s->mb_y;
2278 Picture *ref= &h->ref_list[0][ h->ref_cache[0][ scan8[n] ] ];
2279 mc_dir_part(h, ref, n, square, chroma_height, delta, 0,
2280 dest_y, dest_cb, dest_cr, x_offset, y_offset,
2281 qpix_op, chroma_op);
2284 chroma_op= chroma_avg;
2288 Picture *ref= &h->ref_list[1][ h->ref_cache[1][ scan8[n] ] ];
2289 mc_dir_part(h, ref, n, square, chroma_height, delta, 1,
2290 dest_y, dest_cb, dest_cr, x_offset, y_offset,
2291 qpix_op, chroma_op);
2295 static inline void mc_part_weighted(H264Context *h, int n, int square, int chroma_height, int delta,
2296 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2297 int x_offset, int y_offset,
2298 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
2299 h264_weight_func luma_weight_op, h264_weight_func chroma_weight_op,
2300 h264_biweight_func luma_weight_avg, h264_biweight_func chroma_weight_avg,
2301 int list0, int list1){
2302 MpegEncContext * const s = &h->s;
2304 dest_y += 2*x_offset + 2*y_offset*s-> linesize;
2305 dest_cb += x_offset + y_offset*s->uvlinesize;
2306 dest_cr += x_offset + y_offset*s->uvlinesize;
2307 x_offset += 8*s->mb_x;
2308 y_offset += 8*s->mb_y;
2311 /* don't optimize for luma-only case, since B-frames usually
2312 * use implicit weights => chroma too. */
2313 uint8_t *tmp_cb = s->obmc_scratchpad;
2314 uint8_t *tmp_cr = tmp_cb + 8*s->uvlinesize;
2315 uint8_t *tmp_y = tmp_cr + 8*s->uvlinesize;
2316 int refn0 = h->ref_cache[0][ scan8[n] ];
2317 int refn1 = h->ref_cache[1][ scan8[n] ];
2319 mc_dir_part(h, &h->ref_list[0][refn0], n, square, chroma_height, delta, 0,
2320 dest_y, dest_cb, dest_cr,
2321 x_offset, y_offset, qpix_put, chroma_put);
2322 mc_dir_part(h, &h->ref_list[1][refn1], n, square, chroma_height, delta, 1,
2323 tmp_y, tmp_cb, tmp_cr,
2324 x_offset, y_offset, qpix_put, chroma_put);
2326 if(h->use_weight == 2){
2327 int weight0 = h->implicit_weight[refn0][refn1];
2328 int weight1 = 64 - weight0;
2329 luma_weight_avg( dest_y, tmp_y, s-> linesize, 5, weight0, weight1, 0, 0);
2330 chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, 5, weight0, weight1, 0, 0);
2331 chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, 5, weight0, weight1, 0, 0);
2333 luma_weight_avg(dest_y, tmp_y, s->linesize, h->luma_log2_weight_denom,
2334 h->luma_weight[0][refn0], h->luma_weight[1][refn1],
2335 h->luma_offset[0][refn0], h->luma_offset[1][refn1]);
2336 chroma_weight_avg(dest_cb, tmp_cb, s->uvlinesize, h->chroma_log2_weight_denom,
2337 h->chroma_weight[0][refn0][0], h->chroma_weight[1][refn1][0],
2338 h->chroma_offset[0][refn0][0], h->chroma_offset[1][refn1][0]);
2339 chroma_weight_avg(dest_cr, tmp_cr, s->uvlinesize, h->chroma_log2_weight_denom,
2340 h->chroma_weight[0][refn0][1], h->chroma_weight[1][refn1][1],
2341 h->chroma_offset[0][refn0][1], h->chroma_offset[1][refn1][1]);
2344 int list = list1 ? 1 : 0;
2345 int refn = h->ref_cache[list][ scan8[n] ];
2346 Picture *ref= &h->ref_list[list][refn];
2347 mc_dir_part(h, ref, n, square, chroma_height, delta, list,
2348 dest_y, dest_cb, dest_cr, x_offset, y_offset,
2349 qpix_put, chroma_put);
2351 luma_weight_op(dest_y, s->linesize, h->luma_log2_weight_denom,
2352 h->luma_weight[list][refn], h->luma_offset[list][refn]);
2353 if(h->use_weight_chroma){
2354 chroma_weight_op(dest_cb, s->uvlinesize, h->chroma_log2_weight_denom,
2355 h->chroma_weight[list][refn][0], h->chroma_offset[list][refn][0]);
2356 chroma_weight_op(dest_cr, s->uvlinesize, h->chroma_log2_weight_denom,
2357 h->chroma_weight[list][refn][1], h->chroma_offset[list][refn][1]);
2362 static inline void mc_part(H264Context *h, int n, int square, int chroma_height, int delta,
2363 uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2364 int x_offset, int y_offset,
2365 qpel_mc_func *qpix_put, h264_chroma_mc_func chroma_put,
2366 qpel_mc_func *qpix_avg, h264_chroma_mc_func chroma_avg,
2367 h264_weight_func *weight_op, h264_biweight_func *weight_avg,
2368 int list0, int list1){
2369 if((h->use_weight==2 && list0 && list1
2370 && (h->implicit_weight[ h->ref_cache[0][scan8[n]] ][ h->ref_cache[1][scan8[n]] ] != 32))
2371 || h->use_weight==1)
2372 mc_part_weighted(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
2373 x_offset, y_offset, qpix_put, chroma_put,
2374 weight_op[0], weight_op[3], weight_avg[0], weight_avg[3], list0, list1);
2376 mc_part_std(h, n, square, chroma_height, delta, dest_y, dest_cb, dest_cr,
2377 x_offset, y_offset, qpix_put, chroma_put, qpix_avg, chroma_avg, list0, list1);
2380 static void hl_motion(H264Context *h, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr,
2381 qpel_mc_func (*qpix_put)[16], h264_chroma_mc_func (*chroma_put),
2382 qpel_mc_func (*qpix_avg)[16], h264_chroma_mc_func (*chroma_avg),
2383 h264_weight_func *weight_op, h264_biweight_func *weight_avg){
2384 MpegEncContext * const s = &h->s;
2385 const int mb_xy= s->mb_x + s->mb_y*s->mb_stride;
2386 const int mb_type= s->current_picture.mb_type[mb_xy];
2388 assert(IS_INTER(mb_type));
2390 if(IS_16X16(mb_type)){
2391 mc_part(h, 0, 1, 8, 0, dest_y, dest_cb, dest_cr, 0, 0,
2392 qpix_put[0], chroma_put[0], qpix_avg[0], chroma_avg[0],
2393 &weight_op[0], &weight_avg[0],
2394 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2395 }else if(IS_16X8(mb_type)){
2396 mc_part(h, 0, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 0,
2397 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
2398 &weight_op[1], &weight_avg[1],
2399 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2400 mc_part(h, 8, 0, 4, 8, dest_y, dest_cb, dest_cr, 0, 4,
2401 qpix_put[1], chroma_put[0], qpix_avg[1], chroma_avg[0],
2402 &weight_op[1], &weight_avg[1],
2403 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
2404 }else if(IS_8X16(mb_type)){
2405 mc_part(h, 0, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 0, 0,
2406 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
2407 &weight_op[2], &weight_avg[2],
2408 IS_DIR(mb_type, 0, 0), IS_DIR(mb_type, 0, 1));
2409 mc_part(h, 4, 0, 8, 8*s->linesize, dest_y, dest_cb, dest_cr, 4, 0,
2410 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
2411 &weight_op[2], &weight_avg[2],
2412 IS_DIR(mb_type, 1, 0), IS_DIR(mb_type, 1, 1));
2416 assert(IS_8X8(mb_type));
2419 const int sub_mb_type= h->sub_mb_type[i];
2421 int x_offset= (i&1)<<2;
2422 int y_offset= (i&2)<<1;
2424 if(IS_SUB_8X8(sub_mb_type)){
2425 mc_part(h, n, 1, 4, 0, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2426 qpix_put[1], chroma_put[1], qpix_avg[1], chroma_avg[1],
2427 &weight_op[3], &weight_avg[3],
2428 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2429 }else if(IS_SUB_8X4(sub_mb_type)){
2430 mc_part(h, n , 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2431 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
2432 &weight_op[4], &weight_avg[4],
2433 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2434 mc_part(h, n+2, 0, 2, 4, dest_y, dest_cb, dest_cr, x_offset, y_offset+2,
2435 qpix_put[2], chroma_put[1], qpix_avg[2], chroma_avg[1],
2436 &weight_op[4], &weight_avg[4],
2437 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2438 }else if(IS_SUB_4X8(sub_mb_type)){
2439 mc_part(h, n , 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset, y_offset,
2440 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
2441 &weight_op[5], &weight_avg[5],
2442 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2443 mc_part(h, n+1, 0, 4, 4*s->linesize, dest_y, dest_cb, dest_cr, x_offset+2, y_offset,
2444 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
2445 &weight_op[5], &weight_avg[5],
2446 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2449 assert(IS_SUB_4X4(sub_mb_type));
2451 int sub_x_offset= x_offset + 2*(j&1);
2452 int sub_y_offset= y_offset + (j&2);
2453 mc_part(h, n+j, 1, 2, 0, dest_y, dest_cb, dest_cr, sub_x_offset, sub_y_offset,
2454 qpix_put[2], chroma_put[2], qpix_avg[2], chroma_avg[2],
2455 &weight_op[6], &weight_avg[6],
2456 IS_DIR(sub_mb_type, 0, 0), IS_DIR(sub_mb_type, 0, 1));
2463 static void decode_init_vlc(H264Context *h){
2464 static int done = 0;
2470 init_vlc(&chroma_dc_coeff_token_vlc, CHROMA_DC_COEFF_TOKEN_VLC_BITS, 4*5,
2471 &chroma_dc_coeff_token_len [0], 1, 1,
2472 &chroma_dc_coeff_token_bits[0], 1, 1, 1);
2475 init_vlc(&coeff_token_vlc[i], COEFF_TOKEN_VLC_BITS, 4*17,
2476 &coeff_token_len [i][0], 1, 1,
2477 &coeff_token_bits[i][0], 1, 1, 1);
2481 init_vlc(&chroma_dc_total_zeros_vlc[i], CHROMA_DC_TOTAL_ZEROS_VLC_BITS, 4,
2482 &chroma_dc_total_zeros_len [i][0], 1, 1,
2483 &chroma_dc_total_zeros_bits[i][0], 1, 1, 1);
2485 for(i=0; i<15; i++){
2486 init_vlc(&total_zeros_vlc[i], TOTAL_ZEROS_VLC_BITS, 16,
2487 &total_zeros_len [i][0], 1, 1,
2488 &total_zeros_bits[i][0], 1, 1, 1);
2492 init_vlc(&run_vlc[i], RUN_VLC_BITS, 7,
2493 &run_len [i][0], 1, 1,
2494 &run_bits[i][0], 1, 1, 1);
2496 init_vlc(&run7_vlc, RUN7_VLC_BITS, 16,
2497 &run_len [6][0], 1, 1,
2498 &run_bits[6][0], 1, 1, 1);
2503 * Sets the intra prediction function pointers.
2505 static void init_pred_ptrs(H264Context *h){
2506 // MpegEncContext * const s = &h->s;
2508 h->pred4x4[VERT_PRED ]= pred4x4_vertical_c;
2509 h->pred4x4[HOR_PRED ]= pred4x4_horizontal_c;
2510 h->pred4x4[DC_PRED ]= pred4x4_dc_c;
2511 h->pred4x4[DIAG_DOWN_LEFT_PRED ]= pred4x4_down_left_c;
2512 h->pred4x4[DIAG_DOWN_RIGHT_PRED]= pred4x4_down_right_c;
2513 h->pred4x4[VERT_RIGHT_PRED ]= pred4x4_vertical_right_c;
2514 h->pred4x4[HOR_DOWN_PRED ]= pred4x4_horizontal_down_c;
2515 h->pred4x4[VERT_LEFT_PRED ]= pred4x4_vertical_left_c;
2516 h->pred4x4[HOR_UP_PRED ]= pred4x4_horizontal_up_c;
2517 h->pred4x4[LEFT_DC_PRED ]= pred4x4_left_dc_c;
2518 h->pred4x4[TOP_DC_PRED ]= pred4x4_top_dc_c;
2519 h->pred4x4[DC_128_PRED ]= pred4x4_128_dc_c;
2521 h->pred8x8[DC_PRED8x8 ]= pred8x8_dc_c;
2522 h->pred8x8[VERT_PRED8x8 ]= pred8x8_vertical_c;
2523 h->pred8x8[HOR_PRED8x8 ]= pred8x8_horizontal_c;
2524 h->pred8x8[PLANE_PRED8x8 ]= pred8x8_plane_c;
2525 h->pred8x8[LEFT_DC_PRED8x8]= pred8x8_left_dc_c;
2526 h->pred8x8[TOP_DC_PRED8x8 ]= pred8x8_top_dc_c;
2527 h->pred8x8[DC_128_PRED8x8 ]= pred8x8_128_dc_c;
2529 h->pred16x16[DC_PRED8x8 ]= pred16x16_dc_c;
2530 h->pred16x16[VERT_PRED8x8 ]= pred16x16_vertical_c;
2531 h->pred16x16[HOR_PRED8x8 ]= pred16x16_horizontal_c;
2532 h->pred16x16[PLANE_PRED8x8 ]= pred16x16_plane_c;
2533 h->pred16x16[LEFT_DC_PRED8x8]= pred16x16_left_dc_c;
2534 h->pred16x16[TOP_DC_PRED8x8 ]= pred16x16_top_dc_c;
2535 h->pred16x16[DC_128_PRED8x8 ]= pred16x16_128_dc_c;
2538 static void free_tables(H264Context *h){
2539 av_freep(&h->intra4x4_pred_mode);
2540 av_freep(&h->chroma_pred_mode_table);
2541 av_freep(&h->cbp_table);
2542 av_freep(&h->mvd_table[0]);
2543 av_freep(&h->mvd_table[1]);
2544 av_freep(&h->direct_table);
2545 av_freep(&h->non_zero_count);
2546 av_freep(&h->slice_table_base);
2547 av_freep(&h->top_border);
2548 h->slice_table= NULL;
2550 av_freep(&h->mb2b_xy);
2551 av_freep(&h->mb2b8_xy);
2553 av_freep(&h->s.obmc_scratchpad);
2558 * needs widzh/height
2560 static int alloc_tables(H264Context *h){
2561 MpegEncContext * const s = &h->s;
2562 const int big_mb_num= s->mb_stride * (s->mb_height+1);
2565 CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
2567 CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
2568 CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
2569 CHECKED_ALLOCZ(h->top_border , s->mb_width * (16+8+8) * sizeof(uint8_t))
2570 CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
2572 if( h->pps.cabac ) {
2573 CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
2574 CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
2575 CHECKED_ALLOCZ(h->mvd_table[1], 32*big_mb_num * sizeof(uint16_t));
2576 CHECKED_ALLOCZ(h->direct_table, 32*big_mb_num * sizeof(uint8_t));
2579 memset(h->slice_table_base, -1, big_mb_num * sizeof(uint8_t));
2580 h->slice_table= h->slice_table_base + s->mb_stride + 1;
2582 CHECKED_ALLOCZ(h->mb2b_xy , big_mb_num * sizeof(uint16_t));
2583 CHECKED_ALLOCZ(h->mb2b8_xy , big_mb_num * sizeof(uint16_t));
2584 for(y=0; y<s->mb_height; y++){
2585 for(x=0; x<s->mb_width; x++){
2586 const int mb_xy= x + y*s->mb_stride;
2587 const int b_xy = 4*x + 4*y*h->b_stride;
2588 const int b8_xy= 2*x + 2*y*h->b8_stride;
2590 h->mb2b_xy [mb_xy]= b_xy;
2591 h->mb2b8_xy[mb_xy]= b8_xy;
2595 s->obmc_scratchpad = NULL;
2603 static void common_init(H264Context *h){
2604 MpegEncContext * const s = &h->s;
2606 s->width = s->avctx->width;
2607 s->height = s->avctx->height;
2608 s->codec_id= s->avctx->codec->id;
2612 s->unrestricted_mv=1;
2613 s->decode=1; //FIXME
2616 static int decode_init(AVCodecContext *avctx){
2617 H264Context *h= avctx->priv_data;
2618 MpegEncContext * const s = &h->s;
2620 MPV_decode_defaults(s);
2625 s->out_format = FMT_H264;
2626 s->workaround_bugs= avctx->workaround_bugs;
2629 // s->decode_mb= ff_h263_decode_mb;
2631 avctx->pix_fmt= PIX_FMT_YUV420P;
2635 if(avctx->extradata_size > 0 && avctx->extradata &&
2636 *(char *)avctx->extradata == 1){
2646 static void frame_start(H264Context *h){
2647 MpegEncContext * const s = &h->s;
2650 MPV_frame_start(s, s->avctx);
2651 ff_er_frame_start(s);
2653 assert(s->linesize && s->uvlinesize);
2655 for(i=0; i<16; i++){
2656 h->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*s->linesize*((scan8[i] - scan8[0])>>3);
2657 h->chroma_subblock_offset[i]= 2*((scan8[i] - scan8[0])&7) + 2*s->uvlinesize*((scan8[i] - scan8[0])>>3);
2660 h->block_offset[16+i]=
2661 h->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*s->uvlinesize*((scan8[i] - scan8[0])>>3);
2664 /* can't be in alloc_tables because linesize isn't known there.
2665 * FIXME: redo bipred weight to not require extra buffer? */
2666 if(!s->obmc_scratchpad)
2667 s->obmc_scratchpad = av_malloc(16*s->linesize + 2*8*s->uvlinesize);
2669 // s->decode= (s->flags&CODEC_FLAG_PSNR) || !s->encoding || s->current_picture.reference /*|| h->contains_intra*/ || 1;
2672 static inline void backup_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
2673 MpegEncContext * const s = &h->s;
2677 src_cb -= uvlinesize;
2678 src_cr -= uvlinesize;
2680 h->left_border[0]= h->top_border[s->mb_x][15];
2681 for(i=1; i<17; i++){
2682 h->left_border[i]= src_y[15+i* linesize];
2685 *(uint64_t*)(h->top_border[s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
2686 *(uint64_t*)(h->top_border[s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
2688 if(!(s->flags&CODEC_FLAG_GRAY)){
2689 h->left_border[17 ]= h->top_border[s->mb_x][16+7];
2690 h->left_border[17+9]= h->top_border[s->mb_x][24+7];
2692 h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
2693 h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
2695 *(uint64_t*)(h->top_border[s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
2696 *(uint64_t*)(h->top_border[s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
2700 static inline void xchg_mb_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
2701 MpegEncContext * const s = &h->s;
2704 int deblock_left = (s->mb_x > 0);
2705 int deblock_top = (s->mb_y > 0);
2707 src_y -= linesize + 1;
2708 src_cb -= uvlinesize + 1;
2709 src_cr -= uvlinesize + 1;
2711 #define XCHG(a,b,t,xchg)\
2718 for(i = !deblock_top; i<17; i++){
2719 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
2724 XCHG(*(uint64_t*)(h->top_border[s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
2725 XCHG(*(uint64_t*)(h->top_border[s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
2728 if(!(s->flags&CODEC_FLAG_GRAY)){
2730 for(i = !deblock_top; i<9; i++){
2731 XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
2732 XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
2736 XCHG(*(uint64_t*)(h->top_border[s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
2737 XCHG(*(uint64_t*)(h->top_border[s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
2742 static void hl_decode_mb(H264Context *h){
2743 MpegEncContext * const s = &h->s;
2744 const int mb_x= s->mb_x;
2745 const int mb_y= s->mb_y;
2746 const int mb_xy= mb_x + mb_y*s->mb_stride;
2747 const int mb_type= s->current_picture.mb_type[mb_xy];
2748 uint8_t *dest_y, *dest_cb, *dest_cr;
2749 int linesize, uvlinesize /*dct_offset*/;
2758 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
2759 dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2760 dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
2762 if (h->mb_field_decoding_flag) {
2763 linesize = s->linesize * 2;
2764 uvlinesize = s->uvlinesize * 2;
2765 if(mb_y&1){ //FIXME move out of this func?
2766 dest_y -= s->linesize*15;
2767 dest_cb-= s->linesize*7;
2768 dest_cr-= s->linesize*7;
2771 linesize = s->linesize;
2772 uvlinesize = s->uvlinesize;
2773 // dct_offset = s->linesize * 16;
2776 if (IS_INTRA_PCM(mb_type)) {
2779 // The pixels are stored in h->mb array in the same order as levels,
2780 // copy them in output in the correct order.
2781 for(i=0; i<16; i++) {
2782 for (y=0; y<4; y++) {
2783 for (x=0; x<4; x++) {
2784 *(dest_y + h->block_offset[i] + y*linesize + x) = h->mb[i*16+y*4+x];
2788 for(i=16; i<16+4; i++) {
2789 for (y=0; y<4; y++) {
2790 for (x=0; x<4; x++) {
2791 *(dest_cb + h->block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
2795 for(i=20; i<20+4; i++) {
2796 for (y=0; y<4; y++) {
2797 for (x=0; x<4; x++) {
2798 *(dest_cr + h->block_offset[i] + y*uvlinesize + x) = h->mb[i*16+y*4+x];
2803 if(IS_INTRA(mb_type)){
2804 if(h->deblocking_filter)
2805 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
2807 if(!(s->flags&CODEC_FLAG_GRAY)){
2808 h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
2809 h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
2812 if(IS_INTRA4x4(mb_type)){
2814 for(i=0; i<16; i++){
2815 uint8_t * const ptr= dest_y + h->block_offset[i];
2817 const int dir= h->intra4x4_pred_mode_cache[ scan8[i] ];
2820 if(dir == DIAG_DOWN_LEFT_PRED || dir == VERT_LEFT_PRED){
2821 const int topright_avail= (h->topright_samples_available<<i)&0x8000;
2822 assert(mb_y || linesize <= h->block_offset[i]);
2823 if(!topright_avail){
2824 tr= ptr[3 - linesize]*0x01010101;
2825 topright= (uint8_t*) &tr;
2826 }else if(i==5 && h->deblocking_filter){
2827 tr= *(uint32_t*)h->top_border[mb_x+1];
2828 topright= (uint8_t*) &tr;
2830 topright= ptr + 4 - linesize;
2834 h->pred4x4[ dir ](ptr, topright, linesize);
2835 if(h->non_zero_count_cache[ scan8[i] ]){
2836 if(s->codec_id == CODEC_ID_H264)
2837 s->dsp.h264_idct_add(ptr, h->mb + i*16, linesize);
2839 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, 0);
2844 h->pred16x16[ h->intra16x16_pred_mode ](dest_y , linesize);
2845 if(s->codec_id == CODEC_ID_H264)
2846 h264_luma_dc_dequant_idct_c(h->mb, s->qscale);
2848 svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
2850 if(h->deblocking_filter)
2851 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
2852 }else if(s->codec_id == CODEC_ID_H264){
2853 hl_motion(h, dest_y, dest_cb, dest_cr,
2854 s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
2855 s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
2856 s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
2860 if(!IS_INTRA4x4(mb_type)){
2861 if(s->codec_id == CODEC_ID_H264){
2862 for(i=0; i<16; i++){
2863 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
2864 uint8_t * const ptr= dest_y + h->block_offset[i];
2865 s->dsp.h264_idct_add(ptr, h->mb + i*16, linesize);
2869 for(i=0; i<16; i++){
2870 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){ //FIXME benchmark weird rule, & below
2871 uint8_t * const ptr= dest_y + h->block_offset[i];
2872 svq3_add_idct_c(ptr, h->mb + i*16, linesize, s->qscale, IS_INTRA(mb_type) ? 1 : 0);
2878 if(!(s->flags&CODEC_FLAG_GRAY)){
2879 chroma_dc_dequant_idct_c(h->mb + 16*16, h->chroma_qp);
2880 chroma_dc_dequant_idct_c(h->mb + 16*16+4*16, h->chroma_qp);
2881 if(s->codec_id == CODEC_ID_H264){
2882 for(i=16; i<16+4; i++){
2883 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
2884 uint8_t * const ptr= dest_cb + h->block_offset[i];
2885 s->dsp.h264_idct_add(ptr, h->mb + i*16, uvlinesize);
2888 for(i=20; i<20+4; i++){
2889 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
2890 uint8_t * const ptr= dest_cr + h->block_offset[i];
2891 s->dsp.h264_idct_add(ptr, h->mb + i*16, uvlinesize);
2895 for(i=16; i<16+4; i++){
2896 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
2897 uint8_t * const ptr= dest_cb + h->block_offset[i];
2898 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
2901 for(i=20; i<20+4; i++){
2902 if(h->non_zero_count_cache[ scan8[i] ] || h->mb[i*16]){
2903 uint8_t * const ptr= dest_cr + h->block_offset[i];
2904 svq3_add_idct_c(ptr, h->mb + i*16, uvlinesize, chroma_qp[s->qscale + 12] - 12, 2);
2910 if(h->deblocking_filter) {
2911 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
2912 fill_caches(h, mb_type, 1); //FIXME dont fill stuff which isnt used by filter_mb
2913 filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr);
2918 * fills the default_ref_list.
2920 static int fill_default_ref_list(H264Context *h){
2921 MpegEncContext * const s = &h->s;
2923 int smallest_poc_greater_than_current = -1;
2924 Picture sorted_short_ref[16];
2926 if(h->slice_type==B_TYPE){
2930 /* sort frame according to poc in B slice */
2931 for(out_i=0; out_i<h->short_ref_count; out_i++){
2933 int best_poc=INT_MAX;
2935 for(i=0; i<h->short_ref_count; i++){
2936 const int poc= h->short_ref[i]->poc;
2937 if(poc > limit && poc < best_poc){
2943 assert(best_i != -1);
2946 sorted_short_ref[out_i]= *h->short_ref[best_i];
2947 tprintf("sorted poc: %d->%d poc:%d fn:%d\n", best_i, out_i, sorted_short_ref[out_i].poc, sorted_short_ref[out_i].frame_num);
2948 if (-1 == smallest_poc_greater_than_current) {
2949 if (h->short_ref[best_i]->poc >= s->current_picture_ptr->poc) {
2950 smallest_poc_greater_than_current = out_i;
2956 if(s->picture_structure == PICT_FRAME){
2957 if(h->slice_type==B_TYPE){
2959 tprintf("current poc: %d, smallest_poc_greater_than_current: %d\n", s->current_picture_ptr->poc, smallest_poc_greater_than_current);
2961 // find the largest poc
2962 for(list=0; list<2; list++){
2965 int step= list ? -1 : 1;
2967 for(i=0; i<h->short_ref_count && index < h->ref_count[list]; i++, j+=step) {
2968 while(j<0 || j>= h->short_ref_count){
2970 j= smallest_poc_greater_than_current + (step>>1);
2972 if(sorted_short_ref[j].reference != 3) continue;
2973 h->default_ref_list[list][index ]= sorted_short_ref[j];
2974 h->default_ref_list[list][index++].pic_id= sorted_short_ref[j].frame_num;
2977 for(i = 0; i < 16 && index < h->ref_count[ list ]; i++){
2978 if(h->long_ref[i] == NULL) continue;
2979 if(h->long_ref[i]->reference != 3) continue;
2981 h->default_ref_list[ list ][index ]= *h->long_ref[i];
2982 h->default_ref_list[ list ][index++].pic_id= i;;
2985 if(list && (smallest_poc_greater_than_current<=0 || smallest_poc_greater_than_current>=h->short_ref_count) && (1 < index)){
2986 // swap the two first elements of L1 when
2987 // L0 and L1 are identical
2988 Picture temp= h->default_ref_list[1][0];
2989 h->default_ref_list[1][0] = h->default_ref_list[1][1];
2990 h->default_ref_list[1][0] = temp;
2993 if(index < h->ref_count[ list ])
2994 memset(&h->default_ref_list[list][index], 0, sizeof(Picture)*(h->ref_count[ list ] - index));
2998 for(i=0; i<h->short_ref_count; i++){
2999 if(h->short_ref[i]->reference != 3) continue; //FIXME refernce field shit
3000 h->default_ref_list[0][index ]= *h->short_ref[i];
3001 h->default_ref_list[0][index++].pic_id= h->short_ref[i]->frame_num;
3003 for(i = 0; i < 16; i++){
3004 if(h->long_ref[i] == NULL) continue;
3005 if(h->long_ref[i]->reference != 3) continue;
3006 h->default_ref_list[0][index ]= *h->long_ref[i];
3007 h->default_ref_list[0][index++].pic_id= i;;
3009 if(index < h->ref_count[0])
3010 memset(&h->default_ref_list[0][index], 0, sizeof(Picture)*(h->ref_count[0] - index));
3013 if(h->slice_type==B_TYPE){
3015 //FIXME second field balh
3019 for (i=0; i<h->ref_count[0]; i++) {
3020 tprintf("List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
3022 if(h->slice_type==B_TYPE){
3023 for (i=0; i<h->ref_count[1]; i++) {
3024 tprintf("List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[0][i].data[0]);
3031 static void print_short_term(H264Context *h);
3032 static void print_long_term(H264Context *h);
3034 static int decode_ref_pic_list_reordering(H264Context *h){
3035 MpegEncContext * const s = &h->s;
3038 print_short_term(h);
3040 if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move beofre func
3042 for(list=0; list<2; list++){
3043 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
3045 if(get_bits1(&s->gb)){
3046 int pred= h->curr_pic_num;
3049 for(index=0; ; index++){
3050 int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);
3053 Picture *ref = NULL;
3055 if(reordering_of_pic_nums_idc==3)
3058 if(index >= h->ref_count[list]){
3059 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
3063 if(reordering_of_pic_nums_idc<3){
3064 if(reordering_of_pic_nums_idc<2){
3065 const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
3067 if(abs_diff_pic_num >= h->max_pic_num){
3068 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
3072 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
3073 else pred+= abs_diff_pic_num;
3074 pred &= h->max_pic_num - 1;
3076 for(i= h->short_ref_count-1; i>=0; i--){
3077 ref = h->short_ref[i];
3078 if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer
3082 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
3083 ref = h->long_ref[pic_id];
3087 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
3088 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
3090 h->ref_list[list][index]= *ref;
3093 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
3099 if(h->slice_type!=B_TYPE) break;
3102 if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)
3103 direct_dist_scale_factor(h);
3104 direct_ref_list_init(h);
3108 static int pred_weight_table(H264Context *h){
3109 MpegEncContext * const s = &h->s;
3111 int luma_def, chroma_def;
3114 h->use_weight_chroma= 0;
3115 h->luma_log2_weight_denom= get_ue_golomb(&s->gb);
3116 h->chroma_log2_weight_denom= get_ue_golomb(&s->gb);
3117 luma_def = 1<<h->luma_log2_weight_denom;
3118 chroma_def = 1<<h->chroma_log2_weight_denom;
3120 for(list=0; list<2; list++){
3121 for(i=0; i<h->ref_count[list]; i++){
3122 int luma_weight_flag, chroma_weight_flag;
3124 luma_weight_flag= get_bits1(&s->gb);
3125 if(luma_weight_flag){
3126 h->luma_weight[list][i]= get_se_golomb(&s->gb);
3127 h->luma_offset[list][i]= get_se_golomb(&s->gb);
3128 if( h->luma_weight[list][i] != luma_def
3129 || h->luma_offset[list][i] != 0)
3132 h->luma_weight[list][i]= luma_def;
3133 h->luma_offset[list][i]= 0;
3136 chroma_weight_flag= get_bits1(&s->gb);
3137 if(chroma_weight_flag){
3140 h->chroma_weight[list][i][j]= get_se_golomb(&s->gb);
3141 h->chroma_offset[list][i][j]= get_se_golomb(&s->gb);
3142 if( h->chroma_weight[list][i][j] != chroma_def
3143 || h->chroma_offset[list][i][j] != 0)
3144 h->use_weight_chroma= 1;
3149 h->chroma_weight[list][i][j]= chroma_def;
3150 h->chroma_offset[list][i][j]= 0;
3154 if(h->slice_type != B_TYPE) break;
3156 h->use_weight= h->use_weight || h->use_weight_chroma;
3160 static void implicit_weight_table(H264Context *h){
3161 MpegEncContext * const s = &h->s;
3163 int cur_poc = s->current_picture_ptr->poc;
3165 if( h->ref_count[0] == 1 && h->ref_count[1] == 1
3166 && h->ref_list[0][0].poc + h->ref_list[1][0].poc == 2*cur_poc){
3168 h->use_weight_chroma= 0;
3173 h->use_weight_chroma= 2;
3174 h->luma_log2_weight_denom= 5;
3175 h->chroma_log2_weight_denom= 5;
3178 for(ref0=0; ref0 < h->ref_count[0]; ref0++){
3179 int poc0 = h->ref_list[0][ref0].poc;
3180 for(ref1=0; ref1 < h->ref_count[1]; ref1++){
3181 int poc1 = h->ref_list[1][ref1].poc;
3182 int td = clip(poc1 - poc0, -128, 127);
3184 int tb = clip(cur_poc - poc0, -128, 127);
3185 int tx = (16384 + (ABS(td) >> 1)) / td;
3186 int dist_scale_factor = clip((tb*tx + 32) >> 6, -1024, 1023) >> 2;
3187 if(dist_scale_factor < -64 || dist_scale_factor > 128)
3188 h->implicit_weight[ref0][ref1] = 32;
3190 h->implicit_weight[ref0][ref1] = 64 - dist_scale_factor;
3192 h->implicit_weight[ref0][ref1] = 32;
3198 * instantaneous decoder refresh.
3200 static void idr(H264Context *h){
3203 #define CHECK_DELAY(pic) \
3204 for(j = 0; h->delayed_pic[j]; j++) \
3205 if(pic == h->delayed_pic[j]){ \
3210 for(i=0; i<16; i++){
3211 if (h->long_ref[i] != NULL) {
3212 h->long_ref[i]->reference=0;
3213 CHECK_DELAY(h->long_ref[i]);
3214 h->long_ref[i]= NULL;
3217 h->long_ref_count=0;
3219 for(i=0; i<h->short_ref_count; i++){
3220 h->short_ref[i]->reference=0;
3221 CHECK_DELAY(h->short_ref[i]);
3222 h->short_ref[i]= NULL;
3224 h->short_ref_count=0;
3230 * @return the removed picture or NULL if an error occures
3232 static Picture * remove_short(H264Context *h, int frame_num){
3233 MpegEncContext * const s = &h->s;
3236 if(s->avctx->debug&FF_DEBUG_MMCO)
3237 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
3239 for(i=0; i<h->short_ref_count; i++){
3240 Picture *pic= h->short_ref[i];
3241 if(s->avctx->debug&FF_DEBUG_MMCO)
3242 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
3243 if(pic->frame_num == frame_num){
3244 h->short_ref[i]= NULL;
3245 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i - 1)*sizeof(Picture*));
3246 h->short_ref_count--;
3255 * @return the removed picture or NULL if an error occures
3257 static Picture * remove_long(H264Context *h, int i){
3260 pic= h->long_ref[i];
3261 h->long_ref[i]= NULL;
3262 if(pic) h->long_ref_count--;
3268 * print short term list
3270 static void print_short_term(H264Context *h) {
3272 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
3273 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
3274 for(i=0; i<h->short_ref_count; i++){
3275 Picture *pic= h->short_ref[i];
3276 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
3282 * print long term list
3284 static void print_long_term(H264Context *h) {
3286 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
3287 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
3288 for(i = 0; i < 16; i++){
3289 Picture *pic= h->long_ref[i];
3291 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
3298 * Executes the reference picture marking (memory management control operations).
3300 static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
3301 MpegEncContext * const s = &h->s;
3303 int current_is_long=0;
3306 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
3307 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
3309 for(i=0; i<mmco_count; i++){
3310 if(s->avctx->debug&FF_DEBUG_MMCO)
3311 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_frame_num, h->mmco[i].long_index);
3313 switch(mmco[i].opcode){
3314 case MMCO_SHORT2UNUSED:
3315 pic= remove_short(h, mmco[i].short_frame_num);
3316 if(pic==NULL) return -1;
3319 case MMCO_SHORT2LONG:
3320 pic= remove_long(h, mmco[i].long_index);
3321 if(pic) pic->reference=0;
3323 h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
3324 h->long_ref[ mmco[i].long_index ]->long_ref=1;
3325 h->long_ref_count++;
3327 case MMCO_LONG2UNUSED:
3328 pic= remove_long(h, mmco[i].long_index);
3329 if(pic==NULL) return -1;
3333 pic= remove_long(h, mmco[i].long_index);
3334 if(pic) pic->reference=0;
3336 h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
3337 h->long_ref[ mmco[i].long_index ]->long_ref=1;
3338 h->long_ref_count++;
3342 case MMCO_SET_MAX_LONG:
3343 assert(mmco[i].long_index <= 16);
3344 // just remove the long term which index is greater than new max
3345 for(j = mmco[i].long_index; j<16; j++){
3346 pic = remove_long(h, j);
3347 if (pic) pic->reference=0;
3351 while(h->short_ref_count){
3352 pic= remove_short(h, h->short_ref[0]->frame_num);
3355 for(j = 0; j < 16; j++) {
3356 pic= remove_long(h, j);
3357 if(pic) pic->reference=0;
3364 if(!current_is_long){
3365 pic= remove_short(h, s->current_picture_ptr->frame_num);
3368 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
3371 if(h->short_ref_count)
3372 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
3374 h->short_ref[0]= s->current_picture_ptr;
3375 h->short_ref[0]->long_ref=0;
3376 h->short_ref_count++;
3379 print_short_term(h);
3384 static int decode_ref_pic_marking(H264Context *h){
3385 MpegEncContext * const s = &h->s;
3388 if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
3389 s->broken_link= get_bits1(&s->gb) -1;
3390 h->mmco[0].long_index= get_bits1(&s->gb) - 1; // current_long_term_idx
3391 if(h->mmco[0].long_index == -1)
3394 h->mmco[0].opcode= MMCO_LONG;
3398 if(get_bits1(&s->gb)){ // adaptive_ref_pic_marking_mode_flag
3399 for(i= 0; i<MAX_MMCO_COUNT; i++) {
3400 MMCOOpcode opcode= get_ue_golomb(&s->gb);;
3402 h->mmco[i].opcode= opcode;
3403 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
3404 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
3405 /* if(h->mmco[i].short_frame_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_frame_num ] == NULL){
3406 fprintf(stderr, "illegal short ref in memory management control operation %d\n", mmco);
3410 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
3411 h->mmco[i].long_index= get_ue_golomb(&s->gb);
3412 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){
3413 av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
3418 if(opcode > MMCO_LONG){
3419 av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
3422 if(opcode == MMCO_END)
3427 assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
3429 if(h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count){ //FIXME fields
3430 h->mmco[0].opcode= MMCO_SHORT2UNUSED;
3431 h->mmco[0].short_frame_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
3441 static int init_poc(H264Context *h){
3442 MpegEncContext * const s = &h->s;
3443 const int max_frame_num= 1<<h->sps.log2_max_frame_num;
3446 if(h->nal_unit_type == NAL_IDR_SLICE){
3447 h->frame_num_offset= 0;
3449 if(h->frame_num < h->prev_frame_num)
3450 h->frame_num_offset= h->prev_frame_num_offset + max_frame_num;
3452 h->frame_num_offset= h->prev_frame_num_offset;
3455 if(h->sps.poc_type==0){
3456 const int max_poc_lsb= 1<<h->sps.log2_max_poc_lsb;
3458 if (h->poc_lsb < h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb >= max_poc_lsb/2)
3459 h->poc_msb = h->prev_poc_msb + max_poc_lsb;
3460 else if(h->poc_lsb > h->prev_poc_lsb && h->prev_poc_lsb - h->poc_lsb < -max_poc_lsb/2)
3461 h->poc_msb = h->prev_poc_msb - max_poc_lsb;
3463 h->poc_msb = h->prev_poc_msb;
3464 //printf("poc: %d %d\n", h->poc_msb, h->poc_lsb);
3466 field_poc[1] = h->poc_msb + h->poc_lsb;
3467 if(s->picture_structure == PICT_FRAME)
3468 field_poc[1] += h->delta_poc_bottom;
3469 }else if(h->sps.poc_type==1){
3470 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
3473 if(h->sps.poc_cycle_length != 0)
3474 abs_frame_num = h->frame_num_offset + h->frame_num;
3478 if(h->nal_ref_idc==0 && abs_frame_num > 0)
3481 expected_delta_per_poc_cycle = 0;
3482 for(i=0; i < h->sps.poc_cycle_length; i++)
3483 expected_delta_per_poc_cycle += h->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
3485 if(abs_frame_num > 0){
3486 int poc_cycle_cnt = (abs_frame_num - 1) / h->sps.poc_cycle_length;
3487 int frame_num_in_poc_cycle = (abs_frame_num - 1) % h->sps.poc_cycle_length;
3489 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
3490 for(i = 0; i <= frame_num_in_poc_cycle; i++)
3491 expectedpoc = expectedpoc + h->sps.offset_for_ref_frame[ i ];
3495 if(h->nal_ref_idc == 0)
3496 expectedpoc = expectedpoc + h->sps.offset_for_non_ref_pic;
3498 field_poc[0] = expectedpoc + h->delta_poc[0];
3499 field_poc[1] = field_poc[0] + h->sps.offset_for_top_to_bottom_field;
3501 if(s->picture_structure == PICT_FRAME)
3502 field_poc[1] += h->delta_poc[1];
3505 if(h->nal_unit_type == NAL_IDR_SLICE){
3508 if(h->nal_ref_idc) poc= 2*(h->frame_num_offset + h->frame_num);
3509 else poc= 2*(h->frame_num_offset + h->frame_num) - 1;
3515 if(s->picture_structure != PICT_BOTTOM_FIELD)
3516 s->current_picture_ptr->field_poc[0]= field_poc[0];
3517 if(s->picture_structure != PICT_TOP_FIELD)
3518 s->current_picture_ptr->field_poc[1]= field_poc[1];
3519 if(s->picture_structure == PICT_FRAME) // FIXME field pix?
3520 s->current_picture_ptr->poc= FFMIN(field_poc[0], field_poc[1]);
3526 * decodes a slice header.
3527 * this will allso call MPV_common_init() and frame_start() as needed
3529 static int decode_slice_header(H264Context *h){
3530 MpegEncContext * const s = &h->s;
3531 int first_mb_in_slice, pps_id;
3532 int num_ref_idx_active_override_flag;
3533 static const uint8_t slice_type_map[5]= {P_TYPE, B_TYPE, I_TYPE, SP_TYPE, SI_TYPE};
3535 int default_ref_list_done = 0;
3537 s->current_picture.reference= h->nal_ref_idc != 0;
3538 s->dropable= h->nal_ref_idc == 0;
3540 first_mb_in_slice= get_ue_golomb(&s->gb);
3542 slice_type= get_ue_golomb(&s->gb);
3544 av_log(h->s.avctx, AV_LOG_ERROR, "slice type too large (%d) at %d %d\n", h->slice_type, s->mb_x, s->mb_y);
3549 h->slice_type_fixed=1;
3551 h->slice_type_fixed=0;
3553 slice_type= slice_type_map[ slice_type ];
3554 if (slice_type == I_TYPE
3555 || (h->slice_num != 0 && slice_type == h->slice_type) ) {
3556 default_ref_list_done = 1;
3558 h->slice_type= slice_type;
3560 s->pict_type= h->slice_type; // to make a few old func happy, its wrong though
3562 pps_id= get_ue_golomb(&s->gb);
3564 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
3567 h->pps= h->pps_buffer[pps_id];
3568 if(h->pps.slice_group_count == 0){
3569 av_log(h->s.avctx, AV_LOG_ERROR, "non existing PPS referenced\n");
3573 h->sps= h->sps_buffer[ h->pps.sps_id ];
3574 if(h->sps.log2_max_frame_num == 0){
3575 av_log(h->s.avctx, AV_LOG_ERROR, "non existing SPS referenced\n");