comparison h264.c @ 2581:ae72796e722f libavcodec

This is the second patch for MBAFF support, this adds the deblocking filter code for the I slice. With this patch, 4 more frames are decoded correctly from the conformance streams. patch by (Lo«Ác Le Loarer <lll+ffmpeg m4x org>)
author michael
date Sun, 27 Mar 2005 00:44:11 +0000
parents b4f6f89ec2f6
children 3c27a074de7a
comparison
equal deleted inserted replaced
2580:b4f6f89ec2f6 2581:ae72796e722f
165 int prev_mb_skiped; //FIXME remove (IMHO not used) 165 int prev_mb_skiped; //FIXME remove (IMHO not used)
166 166
167 //prediction stuff 167 //prediction stuff
168 int chroma_pred_mode; 168 int chroma_pred_mode;
169 int intra16x16_pred_mode; 169 int intra16x16_pred_mode;
170
171 int top_mb_xy;
172 int left_mb_xy[2];
170 173
171 int8_t intra4x4_pred_mode_cache[5*8]; 174 int8_t intra4x4_pred_mode_cache[5*8];
172 int8_t (*intra4x4_pred_mode)[8]; 175 int8_t (*intra4x4_pred_mode)[8];
173 void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp? 176 void (*pred4x4 [9+3])(uint8_t *src, uint8_t *topright, int stride);//FIXME move to dsp?
174 void (*pred8x8 [4+3])(uint8_t *src, int stride); 177 void (*pred8x8 [4+3])(uint8_t *src, int stride);
175 void (*pred16x16[4+3])(uint8_t *src, int stride); 178 void (*pred16x16[4+3])(uint8_t *src, int stride);
176 unsigned int topleft_samples_available; 179 unsigned int topleft_samples_available;
177 unsigned int top_samples_available; 180 unsigned int top_samples_available;
178 unsigned int topright_samples_available; 181 unsigned int topright_samples_available;
179 unsigned int left_samples_available; 182 unsigned int left_samples_available;
180 uint8_t (*top_border)[16+2*8]; 183 uint8_t (*top_borders[2])[16+2*8];
181 uint8_t left_border[17+2*9]; 184 uint8_t left_border[2*(17+2*9)];
182 185
183 /** 186 /**
184 * non zero coeff count cache. 187 * non zero coeff count cache.
185 * is 64 if not available. 188 * is 64 if not available.
186 */ 189 */
231 uint8_t *slice_table; ///< slice_table_base + mb_stride + 1 234 uint8_t *slice_table; ///< slice_table_base + mb_stride + 1
232 int slice_type; 235 int slice_type;
233 int slice_type_fixed; 236 int slice_type_fixed;
234 237
235 //interlacing specific flags 238 //interlacing specific flags
239 int mb_aff_frame;
236 int mb_field_decoding_flag; 240 int mb_field_decoding_flag;
237 241
238 int sub_mb_type[4]; 242 int sub_mb_type[4];
239 243
240 //POC stuff 244 //POC stuff
340 static VLC run_vlc[6]; 344 static VLC run_vlc[6];
341 static VLC run7_vlc; 345 static VLC run7_vlc;
342 346
343 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp); 347 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
344 static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc); 348 static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
345 static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr); 349 static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize);
346 350
347 static inline uint32_t pack16to32(int a, int b){ 351 static inline uint32_t pack16to32(int a, int b){
348 #ifdef WORDS_BIGENDIAN 352 #ifdef WORDS_BIGENDIAN
349 return (b&0xFFFF) + (a<<16); 353 return (b&0xFFFF) + (a<<16);
350 #else 354 #else
436 left_block[3]= 3; 440 left_block[3]= 3;
437 left_block[4]= 7; 441 left_block[4]= 7;
438 left_block[5]= 10; 442 left_block[5]= 10;
439 left_block[6]= 8; 443 left_block[6]= 8;
440 left_block[7]= 11; 444 left_block[7]= 11;
441 if(h->sps.mb_aff){ 445 if(h->mb_aff_frame){
442 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride; 446 const int pair_xy = s->mb_x + (s->mb_y & ~1)*s->mb_stride;
443 const int top_pair_xy = pair_xy - s->mb_stride; 447 const int top_pair_xy = pair_xy - s->mb_stride;
444 const int topleft_pair_xy = top_pair_xy - 1; 448 const int topleft_pair_xy = top_pair_xy - 1;
445 const int topright_pair_xy = top_pair_xy + 1; 449 const int topright_pair_xy = top_pair_xy + 1;
446 const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]); 450 const int topleft_mb_frame_flag = !IS_INTERLACED(s->current_picture.mb_type[topleft_pair_xy]);
502 left_block[7]= 10; 506 left_block[7]= 10;
503 } 507 }
504 } 508 }
505 } 509 }
506 510
511 if(for_deblock){
512 h->top_mb_xy = top_xy;
513 h->left_mb_xy[0] = left_xy[0];
514 h->left_mb_xy[1] = left_xy[1];
515 }
507 if(for_deblock){ 516 if(for_deblock){
508 topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0; 517 topleft_type = h->slice_table[topleft_xy ] < 255 ? s->current_picture.mb_type[topleft_xy] : 0;
509 top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0; 518 top_type = h->slice_table[top_xy ] < 255 ? s->current_picture.mb_type[top_xy] : 0;
510 topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0; 519 topright_type= h->slice_table[topright_xy] < 255 ? s->current_picture.mb_type[topright_xy]: 0;
511 left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0; 520 left_type[0] = h->slice_table[left_xy[0] ] < 255 ? s->current_picture.mb_type[left_xy[0]] : 0;
1661 #endif 1670 #endif
1662 1671
1663 /** 1672 /**
1664 * gets the chroma qp. 1673 * gets the chroma qp.
1665 */ 1674 */
1666 static inline int get_chroma_qp(H264Context *h, int qscale){ 1675 static inline int get_chroma_qp(int chroma_qp_index_offset, int qscale){
1667 1676
1668 return chroma_qp[clip(qscale + h->pps.chroma_qp_index_offset, 0, 51)]; 1677 return chroma_qp[clip(qscale + chroma_qp_index_offset, 0, 51)];
1669 } 1678 }
1670 1679
1671 1680
1672 #if 0 1681 #if 0
1673 static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){ 1682 static void h264_diff_dct_c(DCTELEM *block, uint8_t *src1, uint8_t *src2, int stride){
2596 av_freep(&h->mvd_table[0]); 2605 av_freep(&h->mvd_table[0]);
2597 av_freep(&h->mvd_table[1]); 2606 av_freep(&h->mvd_table[1]);
2598 av_freep(&h->direct_table); 2607 av_freep(&h->direct_table);
2599 av_freep(&h->non_zero_count); 2608 av_freep(&h->non_zero_count);
2600 av_freep(&h->slice_table_base); 2609 av_freep(&h->slice_table_base);
2601 av_freep(&h->top_border); 2610 av_freep(&h->top_borders[1]);
2611 av_freep(&h->top_borders[0]);
2602 h->slice_table= NULL; 2612 h->slice_table= NULL;
2603 2613
2604 av_freep(&h->mb2b_xy); 2614 av_freep(&h->mb2b_xy);
2605 av_freep(&h->mb2b8_xy); 2615 av_freep(&h->mb2b8_xy);
2606 2616
2618 2628
2619 CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t)) 2629 CHECKED_ALLOCZ(h->intra4x4_pred_mode, big_mb_num * 8 * sizeof(uint8_t))
2620 2630
2621 CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t)) 2631 CHECKED_ALLOCZ(h->non_zero_count , big_mb_num * 16 * sizeof(uint8_t))
2622 CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t)) 2632 CHECKED_ALLOCZ(h->slice_table_base , big_mb_num * sizeof(uint8_t))
2623 CHECKED_ALLOCZ(h->top_border , s->mb_width * (16+8+8) * sizeof(uint8_t)) 2633 CHECKED_ALLOCZ(h->top_borders[0] , s->mb_width * (16+8+8) * sizeof(uint8_t))
2634 CHECKED_ALLOCZ(h->top_borders[1] , s->mb_width * (16+8+8) * sizeof(uint8_t))
2624 CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t)) 2635 CHECKED_ALLOCZ(h->cbp_table, big_mb_num * sizeof(uint16_t))
2625 2636
2626 if( h->pps.cabac ) { 2637 if( h->pps.cabac ) {
2627 CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t)) 2638 CHECKED_ALLOCZ(h->chroma_pred_mode_table, big_mb_num * sizeof(uint8_t))
2628 CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t)); 2639 CHECKED_ALLOCZ(h->mvd_table[0], 32*big_mb_num * sizeof(uint16_t));
2733 src_cb -= uvlinesize; 2744 src_cb -= uvlinesize;
2734 src_cr -= uvlinesize; 2745 src_cr -= uvlinesize;
2735 2746
2736 // There is two lines saved, the line above the the top macroblock of a pair, 2747 // There is two lines saved, the line above the the top macroblock of a pair,
2737 // and the line above the bottom macroblock 2748 // and the line above the bottom macroblock
2738 h->left_border[0]= h->top_border[s->mb_x][15]; 2749 h->left_border[0]= h->top_borders[0][s->mb_x][15];
2739 for(i=1; i<17; i++){ 2750 for(i=1; i<17; i++){
2740 h->left_border[i]= src_y[15+i* linesize]; 2751 h->left_border[i]= src_y[15+i* linesize];
2741 } 2752 }
2742 2753
2743 *(uint64_t*)(h->top_border[s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize); 2754 *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 16*linesize);
2744 *(uint64_t*)(h->top_border[s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize); 2755 *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+16*linesize);
2745 2756
2746 if(!(s->flags&CODEC_FLAG_GRAY)){ 2757 if(!(s->flags&CODEC_FLAG_GRAY)){
2747 h->left_border[17 ]= h->top_border[s->mb_x][16+7]; 2758 h->left_border[17 ]= h->top_borders[0][s->mb_x][16+7];
2748 h->left_border[17+9]= h->top_border[s->mb_x][24+7]; 2759 h->left_border[17+9]= h->top_borders[0][s->mb_x][24+7];
2749 for(i=1; i<9; i++){ 2760 for(i=1; i<9; i++){
2750 h->left_border[i+17 ]= src_cb[7+i*uvlinesize]; 2761 h->left_border[i+17 ]= src_cb[7+i*uvlinesize];
2751 h->left_border[i+17+9]= src_cr[7+i*uvlinesize]; 2762 h->left_border[i+17+9]= src_cr[7+i*uvlinesize];
2752 } 2763 }
2753 *(uint64_t*)(h->top_border[s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize); 2764 *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+8*uvlinesize);
2754 *(uint64_t*)(h->top_border[s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize); 2765 *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+8*uvlinesize);
2755 } 2766 }
2756 } 2767 }
2757 2768
2758 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){ 2769 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){
2759 MpegEncContext * const s = &h->s; 2770 MpegEncContext * const s = &h->s;
2777 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg); 2788 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
2778 } 2789 }
2779 } 2790 }
2780 2791
2781 if(deblock_top){ 2792 if(deblock_top){
2782 XCHG(*(uint64_t*)(h->top_border[s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg); 2793 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
2783 XCHG(*(uint64_t*)(h->top_border[s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1); 2794 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
2784 } 2795 }
2785 2796
2786 if(!(s->flags&CODEC_FLAG_GRAY)){ 2797 if(!(s->flags&CODEC_FLAG_GRAY)){
2787 if(deblock_left){ 2798 if(deblock_left){
2788 for(i = !deblock_top; i<9; i++){ 2799 for(i = !deblock_top; i<9; i++){
2789 XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg); 2800 XCHG(h->left_border[i+17 ], src_cb[i*uvlinesize], temp8, xchg);
2790 XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg); 2801 XCHG(h->left_border[i+17+9], src_cr[i*uvlinesize], temp8, xchg);
2791 } 2802 }
2792 } 2803 }
2793 if(deblock_top){ 2804 if(deblock_top){
2794 XCHG(*(uint64_t*)(h->top_border[s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1); 2805 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
2795 XCHG(*(uint64_t*)(h->top_border[s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1); 2806 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
2807 }
2808 }
2809 }
2810
2811 static inline void backup_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize){
2812 MpegEncContext * const s = &h->s;
2813 int i;
2814
2815 src_y -= 2 * linesize;
2816 src_cb -= 2 * uvlinesize;
2817 src_cr -= 2 * uvlinesize;
2818
2819 // There is two lines saved, the line above the the top macroblock of a pair,
2820 // and the line above the bottom macroblock
2821 h->left_border[0]= h->top_borders[0][s->mb_x][15];
2822 h->left_border[1]= h->top_borders[1][s->mb_x][15];
2823 for(i=2; i<34; i++){
2824 h->left_border[i]= src_y[15+i* linesize];
2825 }
2826
2827 *(uint64_t*)(h->top_borders[0][s->mb_x]+0)= *(uint64_t*)(src_y + 32*linesize);
2828 *(uint64_t*)(h->top_borders[0][s->mb_x]+8)= *(uint64_t*)(src_y +8+32*linesize);
2829 *(uint64_t*)(h->top_borders[1][s->mb_x]+0)= *(uint64_t*)(src_y + 33*linesize);
2830 *(uint64_t*)(h->top_borders[1][s->mb_x]+8)= *(uint64_t*)(src_y +8+33*linesize);
2831
2832 if(!(s->flags&CODEC_FLAG_GRAY)){
2833 h->left_border[34 ]= h->top_borders[0][s->mb_x][16+7];
2834 h->left_border[34+ 1]= h->top_borders[1][s->mb_x][16+7];
2835 h->left_border[34+18 ]= h->top_borders[0][s->mb_x][24+7];
2836 h->left_border[34+18+1]= h->top_borders[1][s->mb_x][24+7];
2837 for(i=2; i<18; i++){
2838 h->left_border[i+34 ]= src_cb[7+i*uvlinesize];
2839 h->left_border[i+34+18]= src_cr[7+i*uvlinesize];
2840 }
2841 *(uint64_t*)(h->top_borders[0][s->mb_x]+16)= *(uint64_t*)(src_cb+16*uvlinesize);
2842 *(uint64_t*)(h->top_borders[0][s->mb_x]+24)= *(uint64_t*)(src_cr+16*uvlinesize);
2843 *(uint64_t*)(h->top_borders[1][s->mb_x]+16)= *(uint64_t*)(src_cb+17*uvlinesize);
2844 *(uint64_t*)(h->top_borders[1][s->mb_x]+24)= *(uint64_t*)(src_cr+17*uvlinesize);
2845 }
2846 }
2847
2848 static inline void xchg_pair_border(H264Context *h, uint8_t *src_y, uint8_t *src_cb, uint8_t *src_cr, int linesize, int uvlinesize, int xchg){
2849 MpegEncContext * const s = &h->s;
2850 int temp8, i;
2851 uint64_t temp64;
2852 int deblock_left = (s->mb_x > 0);
2853 int deblock_top = (s->mb_y > 0);
2854
2855 tprintf("xchg_pair_border: src_y:%p src_cb:%p src_cr:%p ls:%d uvls:%d\n", src_y, src_cb, src_cr, linesize, uvlinesize);
2856
2857 src_y -= 2 * linesize + 1;
2858 src_cb -= 2 * uvlinesize + 1;
2859 src_cr -= 2 * uvlinesize + 1;
2860
2861 #define XCHG(a,b,t,xchg)\
2862 t= a;\
2863 if(xchg)\
2864 a= b;\
2865 b= t;
2866
2867 if(deblock_left){
2868 for(i = (!deblock_top)<<1; i<34; i++){
2869 XCHG(h->left_border[i ], src_y [i* linesize], temp8, xchg);
2870 }
2871 }
2872
2873 if(deblock_top){
2874 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+0), *(uint64_t*)(src_y +1), temp64, xchg);
2875 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+8), *(uint64_t*)(src_y +9), temp64, 1);
2876 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+0), *(uint64_t*)(src_y +1 +linesize), temp64, xchg);
2877 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+8), *(uint64_t*)(src_y +9 +linesize), temp64, 1);
2878 }
2879
2880 if(!(s->flags&CODEC_FLAG_GRAY)){
2881 if(deblock_left){
2882 for(i = (!deblock_top) << 1; i<18; i++){
2883 XCHG(h->left_border[i+34 ], src_cb[i*uvlinesize], temp8, xchg);
2884 XCHG(h->left_border[i+34+18], src_cr[i*uvlinesize], temp8, xchg);
2885 }
2886 }
2887 if(deblock_top){
2888 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+16), *(uint64_t*)(src_cb+1), temp64, 1);
2889 XCHG(*(uint64_t*)(h->top_borders[0][s->mb_x]+24), *(uint64_t*)(src_cr+1), temp64, 1);
2890 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+16), *(uint64_t*)(src_cb+1 +uvlinesize), temp64, 1);
2891 XCHG(*(uint64_t*)(h->top_borders[1][s->mb_x]+24), *(uint64_t*)(src_cr+1 +uvlinesize), temp64, 1);
2796 } 2892 }
2797 } 2893 }
2798 } 2894 }
2799 2895
2800 static void hl_decode_mb(H264Context *h){ 2896 static void hl_decode_mb(H264Context *h){
2805 const int mb_type= s->current_picture.mb_type[mb_xy]; 2901 const int mb_type= s->current_picture.mb_type[mb_xy];
2806 uint8_t *dest_y, *dest_cb, *dest_cr; 2902 uint8_t *dest_y, *dest_cb, *dest_cr;
2807 int linesize, uvlinesize /*dct_offset*/; 2903 int linesize, uvlinesize /*dct_offset*/;
2808 int i; 2904 int i;
2809 int *block_offset = &h->block_offset[0]; 2905 int *block_offset = &h->block_offset[0];
2906 const unsigned int bottom = mb_y & 1;
2810 2907
2811 if(!s->decode) 2908 if(!s->decode)
2812 return; 2909 return;
2813 2910
2814 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16; 2911 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
2856 } 2953 }
2857 } 2954 }
2858 } 2955 }
2859 } else { 2956 } else {
2860 if(IS_INTRA(mb_type)){ 2957 if(IS_INTRA(mb_type)){
2861 if(h->deblocking_filter) 2958 if(h->deblocking_filter) {
2862 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1); 2959 if (h->mb_aff_frame) {
2960 if (!bottom)
2961 xchg_pair_border(h, dest_y, dest_cb, dest_cr, s->linesize, s->uvlinesize, 1);
2962 } else {
2963 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 1);
2964 }
2965 }
2863 2966
2864 if(!(s->flags&CODEC_FLAG_GRAY)){ 2967 if(!(s->flags&CODEC_FLAG_GRAY)){
2865 h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize); 2968 h->pred8x8[ h->chroma_pred_mode ](dest_cb, uvlinesize);
2866 h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize); 2969 h->pred8x8[ h->chroma_pred_mode ](dest_cr, uvlinesize);
2867 } 2970 }
2879 assert(mb_y || linesize <= block_offset[i]); 2982 assert(mb_y || linesize <= block_offset[i]);
2880 if(!topright_avail){ 2983 if(!topright_avail){
2881 tr= ptr[3 - linesize]*0x01010101; 2984 tr= ptr[3 - linesize]*0x01010101;
2882 topright= (uint8_t*) &tr; 2985 topright= (uint8_t*) &tr;
2883 }else if(i==5 && h->deblocking_filter){ 2986 }else if(i==5 && h->deblocking_filter){
2884 tr= *(uint32_t*)h->top_border[mb_x+1]; 2987 tr= *(uint32_t*)h->top_borders[h->mb_aff_frame ? IS_INTERLACED(mb_type) ? bottom : 1 : 0][mb_x+1];
2885 topright= (uint8_t*) &tr; 2988 topright= (uint8_t*) &tr;
2886 }else 2989 }else
2887 topright= ptr + 4 - linesize; 2990 topright= ptr + 4 - linesize;
2888 }else 2991 }else
2889 topright= NULL; 2992 topright= NULL;
2902 if(s->codec_id == CODEC_ID_H264) 3005 if(s->codec_id == CODEC_ID_H264)
2903 h264_luma_dc_dequant_idct_c(h->mb, s->qscale); 3006 h264_luma_dc_dequant_idct_c(h->mb, s->qscale);
2904 else 3007 else
2905 svq3_luma_dc_dequant_idct_c(h->mb, s->qscale); 3008 svq3_luma_dc_dequant_idct_c(h->mb, s->qscale);
2906 } 3009 }
2907 if(h->deblocking_filter) 3010 if(h->deblocking_filter) {
2908 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0); 3011 if (h->mb_aff_frame) {
3012 if (bottom) {
3013 uint8_t *pair_dest_y = s->current_picture.data[0] + ((mb_y-1) * 16* s->linesize ) + mb_x * 16;
3014 uint8_t *pair_dest_cb = s->current_picture.data[1] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
3015 uint8_t *pair_dest_cr = s->current_picture.data[2] + ((mb_y-1) * 8 * s->uvlinesize) + mb_x * 8;
3016 s->mb_y--;
3017 xchg_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize, 0);
3018 s->mb_y++;
3019 }
3020 } else {
3021 xchg_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize, 0);
3022 }
3023 }
2909 }else if(s->codec_id == CODEC_ID_H264){ 3024 }else if(s->codec_id == CODEC_ID_H264){
2910 hl_motion(h, dest_y, dest_cb, dest_cr, 3025 hl_motion(h, dest_y, dest_cb, dest_cr,
2911 s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab, 3026 s->dsp.put_h264_qpel_pixels_tab, s->dsp.put_h264_chroma_pixels_tab,
2912 s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab, 3027 s->dsp.avg_h264_qpel_pixels_tab, s->dsp.avg_h264_chroma_pixels_tab,
2913 s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab); 3028 s->dsp.weight_h264_pixels_tab, s->dsp.biweight_h264_pixels_tab);
2963 } 3078 }
2964 } 3079 }
2965 } 3080 }
2966 } 3081 }
2967 if(h->deblocking_filter) { 3082 if(h->deblocking_filter) {
2968 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize); 3083 if (h->mb_aff_frame) {
2969 fill_caches(h, mb_type, 1); //FIXME dont fill stuff which isnt used by filter_mb 3084 const int mb_y = s->mb_y - 1;
2970 filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr); 3085 uint8_t *pair_dest_y, *pair_dest_cb, *pair_dest_cr;
3086 const int mb_xy= mb_x + mb_y*s->mb_stride;
3087 const int mb_type_top = s->current_picture.mb_type[mb_xy];
3088 const int mb_type_bottom= s->current_picture.mb_type[mb_xy+s->mb_stride];
3089 uint8_t tmp = s->current_picture.data[1][384];
3090 if (!bottom) return;
3091 pair_dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16;
3092 pair_dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
3093 pair_dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8;
3094
3095 backup_pair_border(h, pair_dest_y, pair_dest_cb, pair_dest_cr, s->linesize, s->uvlinesize);
3096 // TODO deblock a pair
3097 // top
3098 s->mb_y--;
3099 tprintf("call mbaff filter_mb mb_x:%d mb_y:%d pair_dest_y = %p, dest_y = %p\n", mb_x, mb_y, pair_dest_y, dest_y);
3100 fill_caches(h, mb_type_top, 1); //FIXME dont fill stuff which isnt used by filter_mb
3101 filter_mb(h, mb_x, mb_y, pair_dest_y, pair_dest_cb, pair_dest_cr, linesize, uvlinesize);
3102 if (tmp != s->current_picture.data[1][384]) {
3103 tprintf("modified pixel 8,1 (1)\n");
3104 }
3105 // bottom
3106 s->mb_y++;
3107 tprintf("call mbaff filter_mb\n");
3108 fill_caches(h, mb_type_bottom, 1); //FIXME dont fill stuff which isnt used by filter_mb
3109 filter_mb(h, mb_x, mb_y+1, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3110 if (tmp != s->current_picture.data[1][384]) {
3111 tprintf("modified pixel 8,1 (2)\n");
3112 }
3113 } else {
3114 tprintf("call filter_mb\n");
3115 backup_mb_border(h, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3116 fill_caches(h, mb_type, 1); //FIXME dont fill stuff which isnt used by filter_mb
3117 filter_mb(h, mb_x, mb_y, dest_y, dest_cb, dest_cr, linesize, uvlinesize);
3118 }
2971 } 3119 }
2972 } 3120 }
2973 3121
2974 /** 3122 /**
2975 * fills the default_ref_list. 3123 * fills the default_ref_list.
3677 } 3825 }
3678 3826
3679 s->current_picture_ptr->frame_num= //FIXME frame_num cleanup 3827 s->current_picture_ptr->frame_num= //FIXME frame_num cleanup
3680 h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num); 3828 h->frame_num= get_bits(&s->gb, h->sps.log2_max_frame_num);
3681 3829
3830 h->mb_aff_frame = 0;
3682 if(h->sps.frame_mbs_only_flag){ 3831 if(h->sps.frame_mbs_only_flag){
3683 s->picture_structure= PICT_FRAME; 3832 s->picture_structure= PICT_FRAME;
3684 }else{ 3833 }else{
3685 if(get_bits1(&s->gb)) //field_pic_flag 3834 if(get_bits1(&s->gb)) { //field_pic_flag
3686 s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag 3835 s->picture_structure= PICT_TOP_FIELD + get_bits1(&s->gb); //bottom_field_flag
3687 else { 3836 } else {
3688 s->picture_structure= PICT_FRAME; 3837 s->picture_structure= PICT_FRAME;
3689 first_mb_in_slice <<= 1; 3838 first_mb_in_slice <<= 1;
3839 h->mb_aff_frame = h->sps.mb_aff;
3690 } 3840 }
3691 } 3841 }
3692 3842
3693 s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width; 3843 s->resync_mb_x = s->mb_x = first_mb_in_slice % s->mb_width;
3694 s->resync_mb_y = s->mb_y = first_mb_in_slice / s->mb_width; //FIXME AFFW 3844 s->resync_mb_y = s->mb_y = first_mb_in_slice / s->mb_width;
3695 3845
3696 if(s->picture_structure==PICT_FRAME){ 3846 if(s->picture_structure==PICT_FRAME){
3697 h->curr_pic_num= h->frame_num; 3847 h->curr_pic_num= h->frame_num;
3698 h->max_pic_num= 1<< h->sps.log2_max_frame_num; 3848 h->max_pic_num= 1<< h->sps.log2_max_frame_num;
3699 }else{ 3849 }else{
3772 s->qscale = h->pps.init_qp + get_se_golomb(&s->gb); 3922 s->qscale = h->pps.init_qp + get_se_golomb(&s->gb);
3773 if(s->qscale<0 || s->qscale>51){ 3923 if(s->qscale<0 || s->qscale>51){
3774 av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale); 3924 av_log(s->avctx, AV_LOG_ERROR, "QP %d out of range\n", s->qscale);
3775 return -1; 3925 return -1;
3776 } 3926 }
3777 h->chroma_qp = get_chroma_qp(h, s->qscale); 3927 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
3778 //FIXME qscale / qp ... stuff 3928 //FIXME qscale / qp ... stuff
3779 if(h->slice_type == SP_TYPE){ 3929 if(h->slice_type == SP_TYPE){
3780 get_bits1(&s->gb); /* sp_for_switch_flag */ 3930 get_bits1(&s->gb); /* sp_for_switch_flag */
3781 } 3931 }
3782 if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){ 3932 if(h->slice_type==SP_TYPE || h->slice_type == SI_TYPE){
3995 int mb_type; 4145 int mb_type;
3996 4146
3997 memset(h->non_zero_count[mb_xy], 0, 16); 4147 memset(h->non_zero_count[mb_xy], 0, 16);
3998 memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui 4148 memset(h->non_zero_count_cache + 8, 0, 8*5); //FIXME ugly, remove pfui
3999 4149
4150 if(h->mb_aff_frame && s->mb_skip_run==0 && (s->mb_y&1)==0){
4151 h->mb_field_decoding_flag= get_bits1(&s->gb);
4152 }
4153 if(h->mb_field_decoding_flag)
4154 mb_type|= MB_TYPE_INTERLACED;
4155
4000 if( h->slice_type == B_TYPE ) 4156 if( h->slice_type == B_TYPE )
4001 { 4157 {
4002 // just for fill_caches. pred_direct_motion will set the real mb_type 4158 // just for fill_caches. pred_direct_motion will set the real mb_type
4003 mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP; 4159 mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2|MB_TYPE_SKIP;
4004 //FIXME mbaff
4005 4160
4006 fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ... 4161 fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
4007 pred_direct_motion(h, &mb_type); 4162 pred_direct_motion(h, &mb_type);
4008 if(h->pps.cabac){ 4163 if(h->pps.cabac){
4009 fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4); 4164 fill_rectangle(h->mvd_cache[0][scan8[0]], 4, 4, 8, 0, 4);
4013 else 4168 else
4014 { 4169 {
4015 int mx, my; 4170 int mx, my;
4016 mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP; 4171 mb_type= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P1L0|MB_TYPE_SKIP;
4017 4172
4018 if(h->sps.mb_aff && s->mb_skip_run==0 && (s->mb_y&1)==0){
4019 h->mb_field_decoding_flag= get_bits1(&s->gb);
4020 }
4021 if(h->mb_field_decoding_flag)
4022 mb_type|= MB_TYPE_INTERLACED;
4023
4024 fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ... 4173 fill_caches(h, mb_type, 0); //FIXME check what is needed and what not ...
4025 pred_pskip_motion(h, &mx, &my); 4174 pred_pskip_motion(h, &mx, &my);
4026 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1); 4175 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
4027 fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4); 4176 fill_rectangle( h->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx,my), 4);
4028 if(h->pps.cabac) 4177 if(h->pps.cabac)
4057 if (s->mb_skip_run--) { 4206 if (s->mb_skip_run--) {
4058 decode_mb_skip(h); 4207 decode_mb_skip(h);
4059 return 0; 4208 return 0;
4060 } 4209 }
4061 } 4210 }
4062 if(h->sps.mb_aff && s->picture_structure==PICT_FRAME ){ 4211 if(h->mb_aff_frame){
4063 if ( ((s->mb_y&1) == 0) || h->prev_mb_skiped) 4212 if ( ((s->mb_y&1) == 0) || h->prev_mb_skiped)
4064 h->mb_field_decoding_flag = get_bits1(&s->gb); 4213 h->mb_field_decoding_flag = get_bits1(&s->gb);
4065 }else 4214 }else
4066 h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME); 4215 h->mb_field_decoding_flag= (s->picture_structure!=PICT_FRAME);
4067 4216
4132 } 4281 }
4133 } 4282 }
4134 4283
4135 // In deblocking, the quantiser is 0 4284 // In deblocking, the quantiser is 0
4136 s->current_picture.qscale_table[mb_xy]= 0; 4285 s->current_picture.qscale_table[mb_xy]= 0;
4137 h->chroma_qp = get_chroma_qp(h, 0); 4286 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
4138 // All coeffs are presents 4287 // All coeffs are presents
4139 memset(h->non_zero_count[mb_xy], 16, 16); 4288 memset(h->non_zero_count[mb_xy], 16, 16);
4140 4289
4141 return 0; 4290 return 0;
4142 } 4291 }
4393 if(((unsigned)s->qscale) > 51){ 4542 if(((unsigned)s->qscale) > 51){
4394 if(s->qscale<0) s->qscale+= 52; 4543 if(s->qscale<0) s->qscale+= 52;
4395 else s->qscale-= 52; 4544 else s->qscale-= 52;
4396 } 4545 }
4397 4546
4398 h->chroma_qp= chroma_qp= get_chroma_qp(h, s->qscale); 4547 h->chroma_qp= chroma_qp= get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
4399 if(IS_INTRA16x16(mb_type)){ 4548 if(IS_INTRA16x16(mb_type)){
4400 if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){ 4549 if( decode_residual(h, h->intra_gb_ptr, h->mb, LUMA_DC_BLOCK_INDEX, dc_scan, s->qscale, 16) < 0){
4401 return -1; //FIXME continue if partotioned and other retirn -1 too 4550 return -1; //FIXME continue if partotioned and other retirn -1 too
4402 } 4551 }
4403 4552
5080 // All blocks are presents 5229 // All blocks are presents
5081 h->cbp_table[mb_xy] = 0x1ef; 5230 h->cbp_table[mb_xy] = 0x1ef;
5082 h->chroma_pred_mode_table[mb_xy] = 0; 5231 h->chroma_pred_mode_table[mb_xy] = 0;
5083 // In deblocking, the quantiser is 0 5232 // In deblocking, the quantiser is 0
5084 s->current_picture.qscale_table[mb_xy]= 0; 5233 s->current_picture.qscale_table[mb_xy]= 0;
5085 h->chroma_qp = get_chroma_qp(h, 0); 5234 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, 0);
5086 // All coeffs are presents 5235 // All coeffs are presents
5087 memset(h->non_zero_count[mb_xy], 16, 16); 5236 memset(h->non_zero_count[mb_xy], 16, 16);
5088 return 0; 5237 return 0;
5089 } 5238 }
5090 5239
5335 s->qscale += dqp; 5484 s->qscale += dqp;
5336 if(((unsigned)s->qscale) > 51){ 5485 if(((unsigned)s->qscale) > 51){
5337 if(s->qscale<0) s->qscale+= 52; 5486 if(s->qscale<0) s->qscale+= 52;
5338 else s->qscale-= 52; 5487 else s->qscale-= 52;
5339 } 5488 }
5340 h->chroma_qp = get_chroma_qp(h, s->qscale); 5489 h->chroma_qp = get_chroma_qp(h->pps.chroma_qp_index_offset, s->qscale);
5341 5490
5342 if( IS_INTRA16x16( mb_type ) ) { 5491 if( IS_INTRA16x16( mb_type ) ) {
5343 int i; 5492 int i;
5344 //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" ); 5493 //av_log( s->avctx, AV_LOG_ERROR, "INTRA16x16 DC\n" );
5345 if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, s->qscale, 16) < 0) 5494 if( decode_cabac_residual( h, h->mb, 0, 0, dc_scan, s->qscale, 16) < 0)
5447 } 5596 }
5448 5597
5449 i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc ); 5598 i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
5450 pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */ 5599 pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
5451 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ 5600 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
5601 tprintf("filter_mb_edgev i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], pix[-3], p1, p0, q0, q1, pix[2], pix[-2], pix[-1], pix[0], pix[1]);
5452 } 5602 }
5453 pix += stride; 5603 pix += stride;
5454 } 5604 }
5455 }else{ 5605 }else{
5456 /* 4px edge length */ 5606 /* 4px edge length */
5493 }else{ 5643 }else{
5494 /* p0', q0' */ 5644 /* p0', q0' */
5495 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; 5645 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
5496 pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; 5646 pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
5497 } 5647 }
5648 tprintf("filter_mb_edgev i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, p2, p1, p0, q0, q1, q2, pix[-2], pix[-1], pix[0], pix[1]);
5498 } 5649 }
5499 pix += stride; 5650 pix += stride;
5500 } 5651 }
5501 } 5652 }
5502 } 5653 }
5527 ABS( q1 - q0 ) < beta ) { 5678 ABS( q1 - q0 ) < beta ) {
5528 const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc ); 5679 const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
5529 5680
5530 pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */ 5681 pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
5531 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ 5682 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
5532 //tprintf("filter_mb_edgecv i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1); 5683 tprintf("filter_mb_edgecv i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
5533 } 5684 }
5534 pix += stride; 5685 pix += stride;
5535 } 5686 }
5536 }else{ 5687 }else{
5537 /* 2px edge length (because we use same bS than the one for luma) */ 5688 /* 2px edge length (because we use same bS than the one for luma) */
5545 ABS( p1 - p0 ) < beta && 5696 ABS( p1 - p0 ) < beta &&
5546 ABS( q1 - q0 ) < beta ) { 5697 ABS( q1 - q0 ) < beta ) {
5547 5698
5548 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */ 5699 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
5549 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */ 5700 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
5550 //tprintf("filter_mb_edgecv i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1); 5701 tprintf("filter_mb_edgecv i:%d d:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
5551 } 5702 }
5552 pix += stride; 5703 pix += stride;
5704 }
5705 }
5706 }
5707 }
5708
5709 static void filter_mb_mbaff_edgev( H264Context *h, uint8_t *pix, int stride, int bS[8], int qp[2] ) {
5710 int i;
5711 for( i = 0; i < 16; i++, pix += stride) {
5712 int index_a;
5713 int alpha;
5714 int beta;
5715
5716 int qp_index;
5717 int bS_index = (i >> 1);
5718 if (h->mb_field_decoding_flag) {
5719 bS_index &= ~1;
5720 bS_index |= (i & 1);
5721 }
5722
5723 if( bS[bS_index] == 0 ) {
5724 continue;
5725 }
5726
5727 qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
5728 index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
5729 alpha = alpha_table[index_a];
5730 beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
5731
5732
5733 if( bS[bS_index] < 4 ) {
5734 const int tc0 = tc0_table[index_a][bS[bS_index] - 1];
5735 /* 4px edge length */
5736 const int p0 = pix[-1];
5737 const int p1 = pix[-2];
5738 const int p2 = pix[-3];
5739 const int q0 = pix[0];
5740 const int q1 = pix[1];
5741 const int q2 = pix[2];
5742
5743 if( ABS( p0 - q0 ) < alpha &&
5744 ABS( p1 - p0 ) < beta &&
5745 ABS( q1 - q0 ) < beta ) {
5746 int tc = tc0;
5747 int i_delta;
5748
5749 if( ABS( p2 - p0 ) < beta ) {
5750 pix[-2] = p1 + clip( ( p2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( p1 << 1 ) ) >> 1, -tc0, tc0 );
5751 tc++;
5752 }
5753 if( ABS( q2 - q0 ) < beta ) {
5754 pix[1] = q1 + clip( ( q2 + ( ( p0 + q0 + 1 ) >> 1 ) - ( q1 << 1 ) ) >> 1, -tc0, tc0 );
5755 tc++;
5756 }
5757
5758 i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
5759 pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
5760 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
5761 tprintf("filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
5762 }
5763 }else{
5764 /* 4px edge length */
5765 const int p0 = pix[-1];
5766 const int p1 = pix[-2];
5767 const int p2 = pix[-3];
5768
5769 const int q0 = pix[0];
5770 const int q1 = pix[1];
5771 const int q2 = pix[2];
5772
5773 if( ABS( p0 - q0 ) < alpha &&
5774 ABS( p1 - p0 ) < beta &&
5775 ABS( q1 - q0 ) < beta ) {
5776
5777 if(ABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
5778 if( ABS( p2 - p0 ) < beta)
5779 {
5780 const int p3 = pix[-4];
5781 /* p0', p1', p2' */
5782 pix[-1] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
5783 pix[-2] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
5784 pix[-3] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
5785 } else {
5786 /* p0' */
5787 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
5788 }
5789 if( ABS( q2 - q0 ) < beta)
5790 {
5791 const int q3 = pix[3];
5792 /* q0', q1', q2' */
5793 pix[0] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
5794 pix[1] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
5795 pix[2] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
5796 } else {
5797 /* q0' */
5798 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
5799 }
5800 }else{
5801 /* p0', q0' */
5802 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
5803 pix[ 0] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
5804 }
5805 tprintf("filter_mb_mbaff_edgev i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, p2, p1, p0, q0, q1, q2, pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
5806 }
5807 }
5808 }
5809 }
5810 static void filter_mb_mbaff_edgecv( H264Context *h, uint8_t *pix, int stride, int bS[4], int qp[2] ) {
5811 int i;
5812 for( i = 0; i < 8; i++, pix += stride) {
5813 int index_a;
5814 int alpha;
5815 int beta;
5816
5817 int qp_index;
5818 int bS_index = i;
5819
5820 if( bS[bS_index] == 0 ) {
5821 continue;
5822 }
5823
5824 qp_index = h->mb_field_decoding_flag ? (i & 1) : (i >> 3);
5825 index_a = clip( qp[qp_index] + h->slice_alpha_c0_offset, 0, 51 );
5826 alpha = alpha_table[index_a];
5827 beta = beta_table[clip( qp[qp_index] + h->slice_beta_offset, 0, 51 )];
5828 if( bS[bS_index] < 4 ) {
5829 const int tc = tc0_table[index_a][bS[bS_index] - 1] + 1;
5830 /* 2px edge length (because we use same bS than the one for luma) */
5831 const int p0 = pix[-1];
5832 const int p1 = pix[-2];
5833 const int q0 = pix[0];
5834 const int q1 = pix[1];
5835
5836 if( ABS( p0 - q0 ) < alpha &&
5837 ABS( p1 - p0 ) < beta &&
5838 ABS( q1 - q0 ) < beta ) {
5839 const int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
5840
5841 pix[-1] = clip_uint8( p0 + i_delta ); /* p0' */
5842 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
5843 tprintf("filter_mb_mbaff_edgecv i:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, qp[qp_index], index_a, alpha, beta, tc, bS[bS_index], pix[-3], p1, p0, q0, q1, pix[2], p1, pix[-1], pix[0], q1);
5844 }
5845 }else{
5846 const int p0 = pix[-1];
5847 const int p1 = pix[-2];
5848 const int q0 = pix[0];
5849 const int q1 = pix[1];
5850
5851 if( ABS( p0 - q0 ) < alpha &&
5852 ABS( p1 - p0 ) < beta &&
5853 ABS( q1 - q0 ) < beta ) {
5854
5855 pix[-1] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
5856 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
5857 tprintf("filter_mb_mbaff_edgecv i:%d\n# bS:4 -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x, %02x, %02x]\n", i, pix[-3], p1, p0, q0, q1, pix[2], pix[-3], pix[-2], pix[-1], pix[0], pix[1], pix[2]);
5553 } 5858 }
5554 } 5859 }
5555 } 5860 }
5556 } 5861 }
5557 5862
5596 } 5901 }
5597 5902
5598 i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc ); 5903 i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
5599 pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */ 5904 pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */
5600 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ 5905 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
5906 tprintf("filter_mb_edgeh i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], p2, p1, p0, q0, q1, q2, pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
5601 } 5907 }
5602 pix++; 5908 pix++;
5603 } 5909 }
5604 }else{ 5910 }else{
5605 /* 4px edge length */ 5911 /* 4px edge length */
5640 }else{ 5946 }else{
5641 /* p0', q0' */ 5947 /* p0', q0' */
5642 pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2; 5948 pix[-1*pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
5643 pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2; 5949 pix[ 0*pix_next] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
5644 } 5950 }
5951 tprintf("filter_mb_edgeh i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, bS[i], p2, p1, p0, q0, q1, q2, pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
5645 } 5952 }
5646 pix++; 5953 pix++;
5647 } 5954 }
5648 } 5955 }
5649 } 5956 }
5678 5985
5679 int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc ); 5986 int i_delta = clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
5680 5987
5681 pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */ 5988 pix[-pix_next] = clip_uint8( p0 + i_delta ); /* p0' */
5682 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */ 5989 pix[0] = clip_uint8( q0 - i_delta ); /* q0' */
5990 tprintf("filter_mb_edgech i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d, tc:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, tc, bS[i], pix[-3*pix_next], p1, p0, q0, q1, pix[2*pix_next], pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
5683 } 5991 }
5684 pix++; 5992 pix++;
5685 } 5993 }
5686 }else{ 5994 }else{
5687 /* 2px edge length (see deblocking_filter_edgecv) */ 5995 /* 2px edge length (see deblocking_filter_edgecv) */
5695 ABS( p1 - p0 ) < beta && 6003 ABS( p1 - p0 ) < beta &&
5696 ABS( q1 - q0 ) < beta ) { 6004 ABS( q1 - q0 ) < beta ) {
5697 6005
5698 pix[-pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */ 6006 pix[-pix_next] = ( 2*p1 + p0 + q1 + 2 ) >> 2; /* p0' */
5699 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */ 6007 pix[0] = ( 2*q1 + q0 + p1 + 2 ) >> 2; /* q0' */
6008 tprintf("filter_mb_edgech i:%d d:%d, qp:%d, indexA:%d, alpha:%d, beta:%d\n# bS:%d -> [%02x, %02x, %02x, %02x, %02x, %02x] =>[%02x, %02x, %02x, %02x]\n", i, d, qp, index_a, alpha, beta, bS[i], pix[-3*pix_next], p1, p0, q0, q1, pix[2*pix_next], pix[-2*pix_next], pix[-pix_next], pix[0], pix[pix_next]);
5700 } 6009 }
5701 pix++; 6010 pix++;
5702 } 6011 }
5703 } 6012 }
5704 } 6013 }
5705 } 6014 }
5706 6015
5707 static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr) { 6016 static void filter_mb( H264Context *h, int mb_x, int mb_y, uint8_t *img_y, uint8_t *img_cb, uint8_t *img_cr, unsigned int linesize, unsigned int uvlinesize) {
5708 MpegEncContext * const s = &h->s; 6017 MpegEncContext * const s = &h->s;
5709 const int mb_xy= mb_x + mb_y*s->mb_stride; 6018 const int mb_xy= mb_x + mb_y*s->mb_stride;
5710 int linesize, uvlinesize; 6019 int first_vertical_edge_done = 0;
5711 int dir; 6020 int dir;
5712 6021
5713 /* FIXME Implement deblocking filter for field MB */ 6022 if (h->mb_aff_frame
5714 if( h->sps.mb_aff ) { 6023 // left mb is in picture
5715 return; 6024 && h->slice_table[mb_xy-1] != 255
5716 } 6025 // and current and left pair do not have the same interlaced type
5717 linesize = s->linesize; 6026 && (IS_INTERLACED(s->current_picture.mb_type[mb_xy]) != IS_INTERLACED(s->current_picture.mb_type[mb_xy-1]))
5718 uvlinesize = s->uvlinesize; 6027 // and left mb is in the same slice if deblocking_filter == 2
5719 6028 && (h->deblocking_filter!=2 || h->slice_table[mb_xy-1] == h->slice_table[mb_xy])) {
6029 /* First vertical edge is different in MBAFF frames
6030 * There are 8 differents bS to compute and 2 differents Qp
6031 */
6032 int bS[8];
6033 int qp[2];
6034 int chroma_qp[2];
6035
6036 int i;
6037 first_vertical_edge_done = 1;
6038 for( i = 0; i < 8; i++ ) {
6039 int y = i>>1;
6040 int b_idx= 8 + 4 + 8*y;
6041 int bn_idx= b_idx - 1;
6042
6043 int mbn_xy = h->mb_field_decoding_flag ? h->left_mb_xy[i>>2] : h->left_mb_xy[i&1];
6044
6045 if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
6046 IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
6047 bS[i] = 4;
6048 } else if( h->non_zero_count_cache[b_idx] != 0 ||
6049 h->non_zero_count_cache[bn_idx] != 0 ) {
6050 bS[i] = 2;
6051 } else {
6052 /* FIXME: A given frame may occupy more than one position in
6053 * the reference list. So we should compare the frame numbers,
6054 * not the indices in the ref list. */
6055 int l;
6056 bS[i] = 0;
6057 for( l = 0; l < 1 + (h->slice_type == B_TYPE); l++ ) {
6058 if( h->ref_cache[l][b_idx] != h->ref_cache[l][bn_idx] ||
6059 ABS( h->mv_cache[l][b_idx][0] - h->mv_cache[l][bn_idx][0] ) >= 4 ||
6060 ABS( h->mv_cache[l][b_idx][1] - h->mv_cache[l][bn_idx][1] ) >= 4 ) {
6061 bS[i] = 1;
6062 break;
6063 }
6064 }
6065 }
6066 }
6067 if(bS[0]+bS[1]+bS[2]+bS[3] != 0) {
6068 // Do not use s->qscale as luma quantiser because it has not the same
6069 // value in IPCM macroblocks.
6070 qp[0] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[0]] + 1 ) >> 1;
6071 chroma_qp[0] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
6072 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[0]] ) + 1 ) >> 1;
6073 qp[1] = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[h->left_mb_xy[1]] + 1 ) >> 1;
6074 chroma_qp[1] = ( get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mb_xy] ) +
6075 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[h->left_mb_xy[1]] ) + 1 ) >> 1;
6076
6077 /* Filter edge */
6078 tprintf("filter mb:%d/%d MBAFF, QPy:%d/%d, QPc:%d/%d ls:%d uvls:%d", mb_x, mb_y, qp[0], qp[1], chroma_qp[0], chroma_qp[1], linesize, uvlinesize);
6079 { int i; for (i = 0; i < 8; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
6080 filter_mb_mbaff_edgev ( h, &img_y [0], linesize, bS, qp );
6081 filter_mb_mbaff_edgecv( h, &img_cb[0], uvlinesize, bS, chroma_qp );
6082 filter_mb_mbaff_edgecv( h, &img_cr[0], uvlinesize, bS, chroma_qp );
6083 }
6084 }
5720 /* dir : 0 -> vertical edge, 1 -> horizontal edge */ 6085 /* dir : 0 -> vertical edge, 1 -> horizontal edge */
5721 for( dir = 0; dir < 2; dir++ ) 6086 for( dir = 0; dir < 2; dir++ )
5722 { 6087 {
5723 int edge; 6088 int edge;
5724 const int mbm_xy = dir == 0 ? mb_xy -1 : mb_xy - s->mb_stride; 6089 const int mbm_xy = dir == 0 ? mb_xy -1 : h->top_mb_xy;
5725 int start = h->slice_table[mbm_xy] == 255 ? 1 : 0; 6090 int start = h->slice_table[mbm_xy] == 255 ? 1 : 0;
6091
6092 if (first_vertical_edge_done) {
6093 start = 1;
6094 first_vertical_edge_done = 0;
6095 }
5726 6096
5727 if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy]) 6097 if (h->deblocking_filter==2 && h->slice_table[mbm_xy] != h->slice_table[mb_xy])
5728 start = 1; 6098 start = 1;
5729 6099
5730 /* Calculate bS */ 6100 /* Calculate bS */
5731 for( edge = start; edge < 4; edge++ ) { 6101 for( edge = start; edge < 4; edge++ ) {
5732 /* mbn_xy: neighbour macroblock (how that works for field ?) */ 6102 /* mbn_xy: neighbour macroblock */
5733 int mbn_xy = edge > 0 ? mb_xy : mbm_xy; 6103 int mbn_xy = edge > 0 ? mb_xy : mbm_xy;
5734 int bS[4]; 6104 int bS[4];
5735 int qp; 6105 int qp;
5736 6106
6107 if (h->mb_aff_frame && (dir == 1) && (edge == 0) && ((mb_y & 1) == 0)
6108 && !IS_INTERLACED(s->current_picture.mb_type[mb_xy])
6109 && IS_INTERLACED(s->current_picture.mb_type[mbn_xy])
6110 ) {
6111 // This is a special case in the norm where the filtering must
6112 // be done twice (one each of the field) even if we are in a
6113 // frame macroblock.
6114 //
6115 unsigned int tmp_linesize = 2 * linesize;
6116 unsigned int tmp_uvlinesize = 2 * uvlinesize;
6117 int mbn_xy = mb_xy - 2 * s->mb_stride;
6118 int qp, chroma_qp;
6119
6120 // first filtering
6121 if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
6122 IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
6123 bS[0] = bS[1] = bS[2] = bS[3] = 3;
6124 } else {
6125 // TODO
6126 assert(0);
6127 }
6128 /* Filter edge */
6129 // Do not use s->qscale as luma quantiser because it has not the same
6130 // value in IPCM macroblocks.
6131 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
6132 tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
6133 { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
6134 filter_mb_edgeh( h, &img_y[0], tmp_linesize, bS, qp );
6135 chroma_qp = ( h->chroma_qp +
6136 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
6137 filter_mb_edgech( h, &img_cb[0], tmp_uvlinesize, bS, chroma_qp );
6138 filter_mb_edgech( h, &img_cr[0], tmp_uvlinesize, bS, chroma_qp );
6139
6140 // second filtering
6141 mbn_xy += s->mb_stride;
6142 if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
6143 IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
6144 bS[0] = bS[1] = bS[2] = bS[3] = 3;
6145 } else {
6146 // TODO
6147 assert(0);
6148 }
6149 /* Filter edge */
6150 // Do not use s->qscale as luma quantiser because it has not the same
6151 // value in IPCM macroblocks.
6152 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
6153 tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, tmp_linesize, tmp_uvlinesize);
6154 { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
6155 filter_mb_edgeh( h, &img_y[linesize], tmp_linesize, bS, qp );
6156 chroma_qp = ( h->chroma_qp +
6157 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
6158 filter_mb_edgech( h, &img_cb[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
6159 filter_mb_edgech( h, &img_cr[uvlinesize], tmp_uvlinesize, bS, chroma_qp );
6160 continue;
6161 }
5737 if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) || 6162 if( IS_INTRA( s->current_picture.mb_type[mb_xy] ) ||
5738 IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) { 6163 IS_INTRA( s->current_picture.mb_type[mbn_xy] ) ) {
5739 bS[0] = bS[1] = bS[2] = bS[3] = ( edge == 0 ? 4 : 3 ); 6164 int value;
6165 if (edge == 0) {
6166 if ( (!IS_INTERLACED(s->current_picture.mb_type[mb_xy]) && !IS_INTERLACED(s->current_picture.mb_type[mbm_xy]))
6167 || ((h->mb_aff_frame || (s->picture_structure != PICT_FRAME)) && (dir == 0))
6168 ) {
6169 value = 4;
6170 } else {
6171 value = 3;
6172 }
6173 } else {
6174 value = 3;
6175 }
6176 bS[0] = bS[1] = bS[2] = bS[3] = value;
5740 } else { 6177 } else {
5741 int i; 6178 int i;
5742 for( i = 0; i < 4; i++ ) { 6179 for( i = 0; i < 4; i++ ) {
5743 int x = dir == 0 ? edge : i; 6180 int x = dir == 0 ? edge : i;
5744 int y = dir == 0 ? i : edge; 6181 int y = dir == 0 ? i : edge;
5774 /* Filter edge */ 6211 /* Filter edge */
5775 // Do not use s->qscale as luma quantiser because it has not the same 6212 // Do not use s->qscale as luma quantiser because it has not the same
5776 // value in IPCM macroblocks. 6213 // value in IPCM macroblocks.
5777 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1; 6214 qp = ( s->current_picture.qscale_table[mb_xy] + s->current_picture.qscale_table[mbn_xy] + 1 ) >> 1;
5778 //tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]); 6215 //tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d, QPc:%d, QPcn:%d\n", mb_x, mb_y, dir, edge, qp, h->chroma_qp, s->current_picture.qscale_table[mbn_xy]);
6216 tprintf("filter mb:%d/%d dir:%d edge:%d, QPy:%d ls:%d uvls:%d", mb_x, mb_y, dir, edge, qp, linesize, uvlinesize);
6217 { int i; for (i = 0; i < 4; i++) tprintf(" bS[%d]:%d", i, bS[i]); tprintf("\n"); }
5779 if( dir == 0 ) { 6218 if( dir == 0 ) {
5780 filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp ); 6219 filter_mb_edgev( h, &img_y[4*edge], linesize, bS, qp );
5781 if( (edge&1) == 0 ) { 6220 if( (edge&1) == 0 ) {
5782 int chroma_qp = ( h->chroma_qp + 6221 int chroma_qp = ( h->chroma_qp +
5783 get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; 6222 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
5784 filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp ); 6223 filter_mb_edgecv( h, &img_cb[2*edge], uvlinesize, bS, chroma_qp );
5785 filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp ); 6224 filter_mb_edgecv( h, &img_cr[2*edge], uvlinesize, bS, chroma_qp );
5786 } 6225 }
5787 } else { 6226 } else {
5788 filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp ); 6227 filter_mb_edgeh( h, &img_y[4*edge*linesize], linesize, bS, qp );
5789 if( (edge&1) == 0 ) { 6228 if( (edge&1) == 0 ) {
5790 int chroma_qp = ( h->chroma_qp + 6229 int chroma_qp = ( h->chroma_qp +
5791 get_chroma_qp( h, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1; 6230 get_chroma_qp( h->pps.chroma_qp_index_offset, s->current_picture.qscale_table[mbn_xy] ) + 1 ) >> 1;
5792 filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp ); 6231 filter_mb_edgech( h, &img_cb[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
5793 filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp ); 6232 filter_mb_edgech( h, &img_cr[2*edge*uvlinesize], uvlinesize, bS, chroma_qp );
5794 } 6233 }
5795 } 6234 }
5796 } 6235 }
5833 int eos; 6272 int eos;
5834 6273
5835 if(ret>=0) hl_decode_mb(h); 6274 if(ret>=0) hl_decode_mb(h);
5836 6275
5837 /* XXX: useless as decode_mb_cabac it doesn't support that ... */ 6276 /* XXX: useless as decode_mb_cabac it doesn't support that ... */
5838 if( ret >= 0 && h->sps.mb_aff ) { //FIXME optimal? or let mb_decode decode 16x32 ? 6277 if( ret >= 0 && h->mb_aff_frame ) { //FIXME optimal? or let mb_decode decode 16x32 ?
5839 s->mb_y++; 6278 s->mb_y++;
5840 6279
5841 if(ret>=0) ret = decode_mb_cabac(h); 6280 if(ret>=0) ret = decode_mb_cabac(h);
5842 6281
5843 hl_decode_mb(h); 6282 hl_decode_mb(h);
5853 6292
5854 if( ++s->mb_x >= s->mb_width ) { 6293 if( ++s->mb_x >= s->mb_width ) {
5855 s->mb_x = 0; 6294 s->mb_x = 0;
5856 ff_draw_horiz_band(s, 16*s->mb_y, 16); 6295 ff_draw_horiz_band(s, 16*s->mb_y, 16);
5857 ++s->mb_y; 6296 ++s->mb_y;
5858 if(h->sps.mb_aff && s->picture_structure==PICT_FRAME ) { 6297 if(h->mb_aff_frame) {
5859 ++s->mb_y; 6298 ++s->mb_y;
5860 } 6299 }
5861 } 6300 }
5862 6301
5863 if( eos || s->mb_y >= s->mb_height ) { 6302 if( eos || s->mb_y >= s->mb_height ) {
5864 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits); 6303 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
5865 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask); 6304 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
5866 return 0; 6305 return 0;
5867 } 6306 }
5868 #if 0
5869 /* TODO test over-reading in cabac code */
5870 else if( read too much in h->cabac ) {
5871 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
5872 return -1;
5873 }
5874 #endif
5875 } 6307 }
5876 6308
5877 } else { 6309 } else {
5878 for(;;){ 6310 for(;;){
5879 int ret = decode_mb_cavlc(h); 6311 int ret = decode_mb_cavlc(h);
5880 6312
5881 if(ret>=0) hl_decode_mb(h); 6313 if(ret>=0) hl_decode_mb(h);
5882 6314
5883 if(ret>=0 && h->sps.mb_aff){ //FIXME optimal? or let mb_decode decode 16x32 ? 6315 if(ret>=0 && h->mb_aff_frame){ //FIXME optimal? or let mb_decode decode 16x32 ?
5884 s->mb_y++; 6316 s->mb_y++;
5885 ret = decode_mb_cavlc(h); 6317 ret = decode_mb_cavlc(h);
5886 6318
5887 if(ret>=0) hl_decode_mb(h); 6319 if(ret>=0) hl_decode_mb(h);
5888 s->mb_y--; 6320 s->mb_y--;
5897 6329
5898 if(++s->mb_x >= s->mb_width){ 6330 if(++s->mb_x >= s->mb_width){
5899 s->mb_x=0; 6331 s->mb_x=0;
5900 ff_draw_horiz_band(s, 16*s->mb_y, 16); 6332 ff_draw_horiz_band(s, 16*s->mb_y, 16);
5901 ++s->mb_y; 6333 ++s->mb_y;
5902 if(h->sps.mb_aff && s->picture_structure==PICT_FRAME ) { 6334 if(h->mb_aff_frame) {
5903 ++s->mb_y; 6335 ++s->mb_y;
5904 } 6336 }
5905 if(s->mb_y >= s->mb_height){ 6337 if(s->mb_y >= s->mb_height){
5906 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits); 6338 tprintf("slice end %d %d\n", get_bits_count(&s->gb), s->gb.size_in_bits);
5907 6339