comparison h263.c @ 359:df0736462b9f libavcodec

move ABS() to common.h print mb_types in nice looking format support
author michaelni
date Sat, 04 May 2002 18:58:07 +0000
parents 2066dc543be4
children 71792167020c
comparison
equal deleted inserted replaced
358:2066dc543be4 359:df0736462b9f
28 #include "mpegvideo.h" 28 #include "mpegvideo.h"
29 #include "h263data.h" 29 #include "h263data.h"
30 #include "mpeg4data.h" 30 #include "mpeg4data.h"
31 31
32 //rounded divison & shift 32 //rounded divison & shift
33 #define RDIV(a,b) ((a) > 0 ? ((a)+((b)>>1))/(b) : ((a)-((b)>>1))/(b))
34 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b)) 33 #define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
35 #define ABS(a) (((a)>=0)?(a):(-(a)))
36 #define MAX(a,b) ((a) > (b) ? (a) : (b)) 34 #define MAX(a,b) ((a) > (b) ? (a) : (b))
37 #define MIN(a,b) ((a) < (b) ? (a) : (b)) 35 #define MIN(a,b) ((a) < (b) ? (a) : (b))
36
37 #define PRINT_MB_TYPE(a) ;
38 //#define PRINT_MB_TYPE(a) printf(a);
38 39
39 static void h263_encode_block(MpegEncContext * s, DCTELEM * block, 40 static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
40 int n); 41 int n);
41 static void h263_encode_motion(MpegEncContext * s, int val, int fcode); 42 static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
42 static void h263p_encode_umotion(MpegEncContext * s, int val); 43 static void h263p_encode_umotion(MpegEncContext * s, int val);
1763 { 1764 {
1764 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant; 1765 int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant;
1765 INT16 *mot_val; 1766 INT16 *mot_val;
1766 static INT8 quant_tab[4] = { -1, -2, 1, 2 }; 1767 static INT8 quant_tab[4] = { -1, -2, 1, 2 };
1767 1768
1769 if(s->mb_x==0) PRINT_MB_TYPE("\n")
1770
1768 if(s->resync_marker){ 1771 if(s->resync_marker){
1769 if( s->resync_x_pos == s->mb_x+1 1772 if( s->resync_x_pos == s->mb_x+1
1770 || s->resync_x_pos == s->mb_x){ 1773 || s->resync_x_pos == s->mb_x){
1771 /* f*ck mpeg4 1774 /* f*ck mpeg4
1772 this is here so we dont need to slowdown h263_pred_motion with it */ 1775 this is here so we dont need to slowdown h263_pred_motion with it */
1795 s->mv_dir = MV_DIR_FORWARD; 1798 s->mv_dir = MV_DIR_FORWARD;
1796 s->mv_type = MV_TYPE_16X16; 1799 s->mv_type = MV_TYPE_16X16;
1797 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){ 1800 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
1798 const int a= s->sprite_warping_accuracy; 1801 const int a= s->sprite_warping_accuracy;
1799 // int l = (1 << (s->f_code - 1)) * 32; 1802 // int l = (1 << (s->f_code - 1)) * 32;
1800 1803 PRINT_MB_TYPE("G");
1801 s->mcsel=1; 1804 s->mcsel=1;
1802 if(s->divx_version==500 && s->divx_build==413){ 1805 if(s->divx_version==500 && s->divx_build==413){
1803 s->mv[0][0][0] = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample)); 1806 s->mv[0][0][0] = s->sprite_offset[0][0] / (1<<(a-s->quarter_sample));
1804 s->mv[0][0][1] = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample)); 1807 s->mv[0][0][1] = s->sprite_offset[0][1] / (1<<(a-s->quarter_sample));
1805 }else{ 1808 }else{
1811 if (s->mv[0][0][1] < -l) s->mv[0][0][1]= -l; 1814 if (s->mv[0][0][1] < -l) s->mv[0][0][1]= -l;
1812 else if (s->mv[0][0][1] >= l) s->mv[0][0][1]= l-1;*/ 1815 else if (s->mv[0][0][1] >= l) s->mv[0][0][1]= l-1;*/
1813 1816
1814 s->mb_skiped = 0; 1817 s->mb_skiped = 0;
1815 }else{ 1818 }else{
1819 PRINT_MB_TYPE("S");
1816 s->mcsel=0; 1820 s->mcsel=0;
1817 s->mv[0][0][0] = 0; 1821 s->mv[0][0][0] = 0;
1818 s->mv[0][0][1] = 0; 1822 s->mv[0][0][1] = 0;
1819 s->mb_skiped = 1; 1823 s->mb_skiped = 1;
1820 } 1824 }
1846 s->qscale = 31; 1850 s->qscale = 31;
1847 h263_dc_scale(s); 1851 h263_dc_scale(s);
1848 } 1852 }
1849 s->mv_dir = MV_DIR_FORWARD; 1853 s->mv_dir = MV_DIR_FORWARD;
1850 if ((cbpc & 16) == 0) { 1854 if ((cbpc & 16) == 0) {
1855 PRINT_MB_TYPE("P");
1851 /* 16x16 motion prediction */ 1856 /* 16x16 motion prediction */
1852 s->mv_type = MV_TYPE_16X16; 1857 s->mv_type = MV_TYPE_16X16;
1853 h263_pred_motion(s, 0, &pred_x, &pred_y); 1858 h263_pred_motion(s, 0, &pred_x, &pred_y);
1854 if (s->umvplus_dec) 1859 if (s->umvplus_dec)
1855 mx = h263p_decode_umotion(s, pred_x); 1860 mx = h263p_decode_umotion(s, pred_x);
1893 fprintf(stderr, "\n\tmvy: %d\t\tpredy: %d", my, pred_y);*/ 1898 fprintf(stderr, "\n\tmvy: %d\t\tpredy: %d", my, pred_y);*/
1894 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1) 1899 if (s->umvplus_dec && (mx - pred_x) == 1 && (my - pred_y) == 1)
1895 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */ 1900 skip_bits1(&s->gb); /* Bit stuffing to prevent PSC */
1896 1901
1897 } else { 1902 } else {
1903 PRINT_MB_TYPE("4");
1898 s->mv_type = MV_TYPE_8X8; 1904 s->mv_type = MV_TYPE_8X8;
1899 for(i=0;i<4;i++) { 1905 for(i=0;i<4;i++) {
1900 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y); 1906 mot_val = h263_pred_motion(s, i, &pred_x, &pred_y);
1901 if (s->umvplus_dec) 1907 if (s->umvplus_dec)
1902 mx = h263p_decode_umotion(s, pred_x); 1908 mx = h263p_decode_umotion(s, pred_x);
1933 if(s->mb_x==0){ 1939 if(s->mb_x==0){
1934 s->last_mv[0][0][0]= 1940 s->last_mv[0][0][0]=
1935 s->last_mv[0][0][1]= 1941 s->last_mv[0][0][1]=
1936 s->last_mv[1][0][0]= 1942 s->last_mv[1][0][0]=
1937 s->last_mv[1][0][1]= 0; 1943 s->last_mv[1][0][1]= 0;
1938 // printf("\n");
1939 } 1944 }
1940 1945
1941 /* if we skipped it in the future P Frame than skip it now too */ 1946 /* if we skipped it in the future P Frame than skip it now too */
1942 s->mb_skiped= s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; // Note, skiptab=0 if last was GMC 1947 s->mb_skiped= s->mbskip_table[s->mb_y * s->mb_width + s->mb_x]; // Note, skiptab=0 if last was GMC
1943 1948
1953 s->mv[1][0][0] = 0; 1958 s->mv[1][0][0] = 0;
1954 s->mv[1][0][1] = 0; 1959 s->mv[1][0][1] = 0;
1955 //FIXME is this correct? 1960 //FIXME is this correct?
1956 /* s->last_mv[0][0][0]= 1961 /* s->last_mv[0][0][0]=
1957 s->last_mv[0][0][1]=0;*/ 1962 s->last_mv[0][0][1]=0;*/
1958 // printf("S"); 1963 PRINT_MB_TYPE("s")
1959 return 0; 1964 return 0;
1960 } 1965 }
1961 1966
1962 modb1= get_bits1(&s->gb); 1967 modb1= get_bits1(&s->gb);
1963 if(modb1==0){ 1968 if(modb1==0){
2002 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp + my; 2007 : s->motion_val[xy][1]*(time_pb - time_pp)/time_pp + my;
2003 /* s->mv[0][0][0] = 2008 /* s->mv[0][0][0] =
2004 s->mv[0][0][1] = 2009 s->mv[0][0][1] =
2005 s->mv[1][0][0] = 2010 s->mv[1][0][0] =
2006 s->mv[1][0][1] = 1000;*/ 2011 s->mv[1][0][1] = 1000;*/
2007 // printf("D"); 2012 PRINT_MB_TYPE("D");
2008 break; 2013 break;
2009 case 1: 2014 case 1:
2010 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; 2015 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD;
2011 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code); 2016 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
2012 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code); 2017 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
2015 2020
2016 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code); 2021 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
2017 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code); 2022 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
2018 s->last_mv[1][0][0]= s->mv[1][0][0] = mx; 2023 s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
2019 s->last_mv[1][0][1]= s->mv[1][0][1] = my; 2024 s->last_mv[1][0][1]= s->mv[1][0][1] = my;
2020 // printf("I"); 2025 PRINT_MB_TYPE("i");
2021 break; 2026 break;
2022 case 2: 2027 case 2:
2023 s->mv_dir = MV_DIR_BACKWARD; 2028 s->mv_dir = MV_DIR_BACKWARD;
2024 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code); 2029 mx = h263_decode_motion(s, s->last_mv[1][0][0], s->b_code);
2025 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code); 2030 my = h263_decode_motion(s, s->last_mv[1][0][1], s->b_code);
2026 s->last_mv[1][0][0]= s->mv[1][0][0] = mx; 2031 s->last_mv[1][0][0]= s->mv[1][0][0] = mx;
2027 s->last_mv[1][0][1]= s->mv[1][0][1] = my; 2032 s->last_mv[1][0][1]= s->mv[1][0][1] = my;
2028 // printf("B"); 2033 PRINT_MB_TYPE("B");
2029 break; 2034 break;
2030 case 3: 2035 case 3:
2031 s->mv_dir = MV_DIR_FORWARD; 2036 s->mv_dir = MV_DIR_FORWARD;
2032 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code); 2037 mx = h263_decode_motion(s, s->last_mv[0][0][0], s->f_code);
2033 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code); 2038 my = h263_decode_motion(s, s->last_mv[0][0][1], s->f_code);
2034 s->last_mv[0][0][0]= s->mv[0][0][0] = mx; 2039 s->last_mv[0][0][0]= s->mv[0][0][0] = mx;
2035 s->last_mv[0][0][1]= s->mv[0][0][1] = my; 2040 s->last_mv[0][0][1]= s->mv[0][0][1] = my;
2036 // printf("F"); 2041 PRINT_MB_TYPE("F");
2037 break; 2042 break;
2038 default: return -1; 2043 default: return -1;
2039 } 2044 }
2040 } else { /* I-Frame */ 2045 } else { /* I-Frame */
2041 cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc); 2046 cbpc = get_vlc(&s->gb, &intra_MCBPC_vlc);
2042 if (cbpc < 0) 2047 if (cbpc < 0)
2043 return -1; 2048 return -1;
2044 dquant = cbpc & 4; 2049 dquant = cbpc & 4;
2045 s->mb_intra = 1; 2050 s->mb_intra = 1;
2046 intra: 2051 intra:
2052 PRINT_MB_TYPE("I");
2047 s->ac_pred = 0; 2053 s->ac_pred = 0;
2048 if (s->h263_pred || s->h263_aic) { 2054 if (s->h263_pred || s->h263_aic) {
2049 s->ac_pred = get_bits1(&s->gb); 2055 s->ac_pred = get_bits1(&s->gb);
2050 if (s->ac_pred && s->h263_aic) 2056 if (s->ac_pred && s->h263_aic)
2051 s->h263_aic_dir = get_bits1(&s->gb); 2057 s->h263_aic_dir = get_bits1(&s->gb);
2604 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...) 2610 // this is mostly identical to the mpeg4 std (and is totally unreadable because of that ...)
2605 // perhaps it should be reordered to be more readable ... 2611 // perhaps it should be reordered to be more readable ...
2606 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides 2612 // the idea behind this virtual_ref mess is to be able to use shifts later per pixel instead of divides
2607 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form 2613 // so the distance between points is converted from w&h based to w2&h2 based which are of the 2^x form
2608 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2) 2614 virtual_ref[0][0]= 16*(vop_ref[0][0] + w2)
2609 + RDIV(((w - w2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + w2*(r*sprite_ref[1][0] - 16*vop_ref[1][0])),w); 2615 + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + w2*(r*sprite_ref[1][0] - 16*vop_ref[1][0])),w);
2610 virtual_ref[0][1]= 16*vop_ref[0][1] 2616 virtual_ref[0][1]= 16*vop_ref[0][1]
2611 + RDIV(((w - w2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + w2*(r*sprite_ref[1][1] - 16*vop_ref[1][1])),w); 2617 + ROUNDED_DIV(((w - w2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + w2*(r*sprite_ref[1][1] - 16*vop_ref[1][1])),w);
2612 virtual_ref[1][0]= 16*vop_ref[0][0] 2618 virtual_ref[1][0]= 16*vop_ref[0][0]
2613 + RDIV(((h - h2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + h2*(r*sprite_ref[2][0] - 16*vop_ref[2][0])),h); 2619 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][0] - 16*vop_ref[0][0]) + h2*(r*sprite_ref[2][0] - 16*vop_ref[2][0])),h);
2614 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2) 2620 virtual_ref[1][1]= 16*(vop_ref[0][1] + h2)
2615 + RDIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h); 2621 + ROUNDED_DIV(((h - h2)*(r*sprite_ref[0][1] - 16*vop_ref[0][1]) + h2*(r*sprite_ref[2][1] - 16*vop_ref[2][1])),h);
2616 2622
2617 switch(s->num_sprite_warping_points) 2623 switch(s->num_sprite_warping_points)
2618 { 2624 {
2619 case 0: 2625 case 0:
2620 s->sprite_offset[0][0]= 0; 2626 s->sprite_offset[0][0]= 0;