comparison mpeg12.c @ 3305:ceb221c4eca7 libavcodec

add MPEG-2 intra vlc support
author bcoudurier
date Sat, 13 May 2006 17:16:58 +0000
parents 25d315fcca5a
children 5e2c69db0ef7
comparison
equal deleted inserted replaced
3304:25d315fcca5a 3305:ceb221c4eca7
93 #ifdef CONFIG_ENCODERS 93 #ifdef CONFIG_ENCODERS
94 static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL; 94 static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
95 static uint8_t fcode_tab[MAX_MV*2+1]; 95 static uint8_t fcode_tab[MAX_MV*2+1];
96 96
97 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2]; 97 static uint8_t uni_mpeg1_ac_vlc_len [64*64*2];
98 static uint8_t uni_mpeg2_ac_vlc_len [64*64*2];
98 99
99 /* simple include everything table for dc, first byte is bits number next 3 are code*/ 100 /* simple include everything table for dc, first byte is bits number next 3 are code*/
100 static uint32_t mpeg1_lum_dc_uni[512]; 101 static uint32_t mpeg1_lum_dc_uni[512];
101 static uint32_t mpeg1_chr_dc_uni[512]; 102 static uint32_t mpeg1_chr_dc_uni[512];
102 103
164 else 165 else
165 code= rl->index_run[0][run] + alevel - 1; 166 code= rl->index_run[0][run] + alevel - 1;
166 167
167 if (code < 111 /* rl->n */) { 168 if (code < 111 /* rl->n */) {
168 /* store the vlc & sign at once */ 169 /* store the vlc & sign at once */
169 len= mpeg1_vlc[code][1]+1; 170 len= rl->table_vlc[code][1]+1;
170 bits= (mpeg1_vlc[code][0]<<1) + sign; 171 bits= (rl->table_vlc[code][0]<<1) + sign;
171 } else { 172 } else {
172 len= mpeg1_vlc[111/*rl->n*/][1]+6; 173 len= rl->table_vlc[111/*rl->n*/][1]+6;
173 bits= mpeg1_vlc[111/*rl->n*/][0]<<6; 174 bits= rl->table_vlc[111/*rl->n*/][0]<<6;
174 175
175 bits|= run; 176 bits|= run;
176 if (alevel < 128) { 177 if (alevel < 128) {
177 bits<<=8; len+=8; 178 bits<<=8; len+=8;
178 bits|= level & 0xff; 179 bits|= level & 0xff;
764 int mv; 765 int mv;
765 int i; 766 int i;
766 767
767 done=1; 768 done=1;
768 init_rl(&rl_mpeg1, 1); 769 init_rl(&rl_mpeg1, 1);
770 if(s->intra_vlc_format)
771 init_rl(&rl_mpeg2, 1);
769 772
770 for(i=0; i<64; i++) 773 for(i=0; i<64; i++)
771 { 774 {
772 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i]; 775 mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
773 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i]; 776 mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
774 } 777 }
775 778
776 init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_len); 779 init_uni_ac_vlc(&rl_mpeg1, uni_mpeg1_ac_vlc_len);
780 if(s->intra_vlc_format)
781 init_uni_ac_vlc(&rl_mpeg2, uni_mpeg2_ac_vlc_len);
777 782
778 /* build unified dc encoding tables */ 783 /* build unified dc encoding tables */
779 for(i=-255; i<256; i++) 784 for(i=-255; i<256; i++)
780 { 785 {
781 int adiff, index; 786 int adiff, index;
838 s->max_qcoeff= 255; 843 s->max_qcoeff= 255;
839 }else{ 844 }else{
840 s->min_qcoeff=-2047; 845 s->min_qcoeff=-2047;
841 s->max_qcoeff= 2047; 846 s->max_qcoeff= 2047;
842 } 847 }
843 s->intra_ac_vlc_length= 848 if (s->intra_vlc_format) {
849 s->intra_ac_vlc_length=
850 s->intra_ac_vlc_last_length= uni_mpeg2_ac_vlc_len;
851 } else {
852 s->intra_ac_vlc_length=
853 s->intra_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
854 }
844 s->inter_ac_vlc_length= 855 s->inter_ac_vlc_length=
845 s->intra_ac_vlc_last_length=
846 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len; 856 s->inter_ac_vlc_last_length= uni_mpeg1_ac_vlc_len;
847 } 857 }
848 858
849 static inline void encode_dc(MpegEncContext *s, int diff, int component) 859 static inline void encode_dc(MpegEncContext *s, int diff, int component)
850 { 860 {
887 DCTELEM *block, 897 DCTELEM *block,
888 int n) 898 int n)
889 { 899 {
890 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign; 900 int alevel, level, last_non_zero, dc, diff, i, j, run, last_index, sign;
891 int code, component; 901 int code, component;
892 // RLTable *rl = &rl_mpeg1; 902 const uint16_t (*table_vlc)[2] = rl_mpeg1.table_vlc;
893 903
894 last_index = s->block_last_index[n]; 904 last_index = s->block_last_index[n];
895 905
896 /* DC coef */ 906 /* DC coef */
897 if (s->mb_intra) { 907 if (s->mb_intra) {
899 dc = block[0]; /* overflow is impossible */ 909 dc = block[0]; /* overflow is impossible */
900 diff = dc - s->last_dc[component]; 910 diff = dc - s->last_dc[component];
901 encode_dc(s, diff, component); 911 encode_dc(s, diff, component);
902 s->last_dc[component] = dc; 912 s->last_dc[component] = dc;
903 i = 1; 913 i = 1;
904 /*
905 if (s->intra_vlc_format) 914 if (s->intra_vlc_format)
906 rl = &rl_mpeg2; 915 table_vlc = rl_mpeg2.table_vlc;
907 else
908 rl = &rl_mpeg1;
909 */
910 } else { 916 } else {
911 /* encode the first coefficient : needs to be done here because 917 /* encode the first coefficient : needs to be done here because
912 it is handled slightly differently */ 918 it is handled slightly differently */
913 level = block[0]; 919 level = block[0];
914 if (abs(level) == 1) { 920 if (abs(level) == 1) {
939 945
940 alevel= level; 946 alevel= level;
941 MASK_ABS(sign, alevel) 947 MASK_ABS(sign, alevel)
942 sign&=1; 948 sign&=1;
943 949
944 // code = get_rl_index(rl, 0, run, alevel);
945 if (alevel <= mpeg1_max_level[0][run]){ 950 if (alevel <= mpeg1_max_level[0][run]){
946 code= mpeg1_index_run[0][run] + alevel - 1; 951 code= mpeg1_index_run[0][run] + alevel - 1;
947 /* store the vlc & sign at once */ 952 /* store the vlc & sign at once */
948 put_bits(&s->pb, mpeg1_vlc[code][1]+1, (mpeg1_vlc[code][0]<<1) + sign); 953 put_bits(&s->pb, table_vlc[code][1]+1, (table_vlc[code][0]<<1) + sign);
949 } else { 954 } else {
950 /* escape seems to be pretty rare <5% so i dont optimize it */ 955 /* escape seems to be pretty rare <5% so i dont optimize it */
951 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]); 956 put_bits(&s->pb, table_vlc[111][1], table_vlc[111][0]);
952 /* escape: only clip in this case */ 957 /* escape: only clip in this case */
953 put_bits(&s->pb, 6, run); 958 put_bits(&s->pb, 6, run);
954 if(s->codec_id == CODEC_ID_MPEG1VIDEO){ 959 if(s->codec_id == CODEC_ID_MPEG1VIDEO){
955 if (alevel < 128) { 960 if (alevel < 128) {
956 put_bits(&s->pb, 8, level & 0xff); 961 put_bits(&s->pb, 8, level & 0xff);
967 } 972 }
968 last_non_zero = i; 973 last_non_zero = i;
969 } 974 }
970 } 975 }
971 /* end of block */ 976 /* end of block */
972 put_bits(&s->pb, 2, 0x2); 977 put_bits(&s->pb, table_vlc[112][1], table_vlc[112][0]);
973 } 978 }
974 #endif //CONFIG_ENCODERS 979 #endif //CONFIG_ENCODERS
975 980
976 /******************************************/ 981 /******************************************/
977 /* decoding */ 982 /* decoding */