comparison mpegvideo.c @ 1689:1a2db2073848 libavcodec

split intra / inter dequantization
author michael
date Sun, 14 Dec 2003 02:57:00 +0000
parents 04b759af8bd4
children f3287b5d8d9f
comparison
equal deleted inserted replaced
1688:04b759af8bd4 1689:1a2db2073848
38 //#include <assert.h> 38 //#include <assert.h>
39 39
40 #ifdef CONFIG_ENCODERS 40 #ifdef CONFIG_ENCODERS
41 static void encode_picture(MpegEncContext *s, int picture_number); 41 static void encode_picture(MpegEncContext *s, int picture_number);
42 #endif //CONFIG_ENCODERS 42 #endif //CONFIG_ENCODERS
43 static void dct_unquantize_mpeg1_c(MpegEncContext *s, 43 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
44 DCTELEM *block, int n, int qscale); 44 DCTELEM *block, int n, int qscale);
45 static void dct_unquantize_mpeg2_c(MpegEncContext *s, 45 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
46 DCTELEM *block, int n, int qscale); 46 DCTELEM *block, int n, int qscale);
47 static void dct_unquantize_h263_c(MpegEncContext *s, 47 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
48 DCTELEM *block, int n, int qscale);
49 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
50 DCTELEM *block, int n, int qscale);
51 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
52 DCTELEM *block, int n, int qscale);
53 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
48 DCTELEM *block, int n, int qscale); 54 DCTELEM *block, int n, int qscale);
49 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w); 55 static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w);
50 #ifdef CONFIG_ENCODERS 56 #ifdef CONFIG_ENCODERS
51 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); 57 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
52 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); 58 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow);
202 #endif //CONFIG_ENCODERS 208 #endif //CONFIG_ENCODERS
203 209
204 /* init common dct for both encoder and decoder */ 210 /* init common dct for both encoder and decoder */
205 int DCT_common_init(MpegEncContext *s) 211 int DCT_common_init(MpegEncContext *s)
206 { 212 {
207 s->dct_unquantize_h263 = dct_unquantize_h263_c; 213 s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
208 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c; 214 s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
209 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c; 215 s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
216 s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
217 s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
218 s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
210 219
211 #ifdef CONFIG_ENCODERS 220 #ifdef CONFIG_ENCODERS
212 s->dct_quantize= dct_quantize_c; 221 s->dct_quantize= dct_quantize_c;
213 #endif 222 #endif
214 223
1184 s->hurry_up= s->avctx->hurry_up; 1193 s->hurry_up= s->avctx->hurry_up;
1185 s->error_resilience= avctx->error_resilience; 1194 s->error_resilience= avctx->error_resilience;
1186 1195
1187 /* set dequantizer, we cant do it during init as it might change for mpeg4 1196 /* set dequantizer, we cant do it during init as it might change for mpeg4
1188 and we cant do it in the header decode as init isnt called for mpeg4 there yet */ 1197 and we cant do it in the header decode as init isnt called for mpeg4 there yet */
1189 if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO) 1198 if(s->mpeg_quant || s->codec_id == CODEC_ID_MPEG2VIDEO){
1190 s->dct_unquantize = s->dct_unquantize_mpeg2; 1199 s->dct_unquantize_intra = s->dct_unquantize_mpeg2_intra;
1191 else if(s->out_format == FMT_H263) 1200 s->dct_unquantize_inter = s->dct_unquantize_mpeg2_inter;
1192 s->dct_unquantize = s->dct_unquantize_h263; 1201 }else if(s->out_format == FMT_H263){
1193 else 1202 s->dct_unquantize_intra = s->dct_unquantize_h263_intra;
1194 s->dct_unquantize = s->dct_unquantize_mpeg1; 1203 s->dct_unquantize_inter = s->dct_unquantize_h263_inter;
1204 }else{
1205 s->dct_unquantize_intra = s->dct_unquantize_mpeg1_intra;
1206 s->dct_unquantize_inter = s->dct_unquantize_mpeg1_inter;
1207 }
1195 1208
1196 if(s->dct_error_sum){ 1209 if(s->dct_error_sum){
1197 assert(s->avctx->noise_reduction && s->encoding); 1210 assert(s->avctx->noise_reduction && s->encoding);
1198 1211
1199 update_noise_reduction(s); 1212 update_noise_reduction(s);
2744 2757
2745 /* put block[] to dest[] */ 2758 /* put block[] to dest[] */
2746 static inline void put_dct(MpegEncContext *s, 2759 static inline void put_dct(MpegEncContext *s,
2747 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale) 2760 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
2748 { 2761 {
2749 s->dct_unquantize(s, block, i, qscale); 2762 s->dct_unquantize_intra(s, block, i, qscale);
2750 s->dsp.idct_put (dest, line_size, block); 2763 s->dsp.idct_put (dest, line_size, block);
2751 } 2764 }
2752 2765
2753 /* add block[] to dest[] */ 2766 /* add block[] to dest[] */
2754 static inline void add_dct(MpegEncContext *s, 2767 static inline void add_dct(MpegEncContext *s,
2761 2774
2762 static inline void add_dequant_dct(MpegEncContext *s, 2775 static inline void add_dequant_dct(MpegEncContext *s,
2763 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale) 2776 DCTELEM *block, int i, uint8_t *dest, int line_size, int qscale)
2764 { 2777 {
2765 if (s->block_last_index[i] >= 0) { 2778 if (s->block_last_index[i] >= 0) {
2766 s->dct_unquantize(s, block, i, qscale); 2779 s->dct_unquantize_inter(s, block, i, qscale);
2767 2780
2768 s->dsp.idct_add (dest, line_size, block); 2781 s->dsp.idct_add (dest, line_size, block);
2769 } 2782 }
2770 } 2783 }
2771 2784
4779 return last_non_zero; 4792 return last_non_zero;
4780 } 4793 }
4781 4794
4782 #endif //CONFIG_ENCODERS 4795 #endif //CONFIG_ENCODERS
4783 4796
4784 static void dct_unquantize_mpeg1_c(MpegEncContext *s, 4797 static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s,
4785 DCTELEM *block, int n, int qscale) 4798 DCTELEM *block, int n, int qscale)
4786 { 4799 {
4787 int i, level, nCoeffs; 4800 int i, level, nCoeffs;
4788 const uint16_t *quant_matrix; 4801 const uint16_t *quant_matrix;
4789 4802
4790 nCoeffs= s->block_last_index[n]; 4803 nCoeffs= s->block_last_index[n];
4791 4804
4792 if (s->mb_intra) { 4805 if (n < 4)
4806 block[0] = block[0] * s->y_dc_scale;
4807 else
4808 block[0] = block[0] * s->c_dc_scale;
4809 /* XXX: only mpeg1 */
4810 quant_matrix = s->intra_matrix;
4811 for(i=1;i<=nCoeffs;i++) {
4812 int j= s->intra_scantable.permutated[i];
4813 level = block[j];
4814 if (level) {
4815 if (level < 0) {
4816 level = -level;
4817 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4818 level = (level - 1) | 1;
4819 level = -level;
4820 } else {
4821 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4822 level = (level - 1) | 1;
4823 }
4824 block[j] = level;
4825 }
4826 }
4827 }
4828
4829 static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s,
4830 DCTELEM *block, int n, int qscale)
4831 {
4832 int i, level, nCoeffs;
4833 const uint16_t *quant_matrix;
4834
4835 nCoeffs= s->block_last_index[n];
4836
4837 quant_matrix = s->inter_matrix;
4838 for(i=0; i<=nCoeffs; i++) {
4839 int j= s->intra_scantable.permutated[i];
4840 level = block[j];
4841 if (level) {
4842 if (level < 0) {
4843 level = -level;
4844 level = (((level << 1) + 1) * qscale *
4845 ((int) (quant_matrix[j]))) >> 4;
4846 level = (level - 1) | 1;
4847 level = -level;
4848 } else {
4849 level = (((level << 1) + 1) * qscale *
4850 ((int) (quant_matrix[j]))) >> 4;
4851 level = (level - 1) | 1;
4852 }
4853 block[j] = level;
4854 }
4855 }
4856 }
4857
4858 static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s,
4859 DCTELEM *block, int n, int qscale)
4860 {
4861 int i, level, nCoeffs;
4862 const uint16_t *quant_matrix;
4863
4864 if(s->alternate_scan) nCoeffs= 63;
4865 else nCoeffs= s->block_last_index[n];
4866
4867 if (n < 4)
4868 block[0] = block[0] * s->y_dc_scale;
4869 else
4870 block[0] = block[0] * s->c_dc_scale;
4871 quant_matrix = s->intra_matrix;
4872 for(i=1;i<=nCoeffs;i++) {
4873 int j= s->intra_scantable.permutated[i];
4874 level = block[j];
4875 if (level) {
4876 if (level < 0) {
4877 level = -level;
4878 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4879 level = -level;
4880 } else {
4881 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4882 }
4883 block[j] = level;
4884 }
4885 }
4886 }
4887
4888 static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s,
4889 DCTELEM *block, int n, int qscale)
4890 {
4891 int i, level, nCoeffs;
4892 const uint16_t *quant_matrix;
4893 int sum=-1;
4894
4895 if(s->alternate_scan) nCoeffs= 63;
4896 else nCoeffs= s->block_last_index[n];
4897
4898 quant_matrix = s->inter_matrix;
4899 for(i=0; i<=nCoeffs; i++) {
4900 int j= s->intra_scantable.permutated[i];
4901 level = block[j];
4902 if (level) {
4903 if (level < 0) {
4904 level = -level;
4905 level = (((level << 1) + 1) * qscale *
4906 ((int) (quant_matrix[j]))) >> 4;
4907 level = -level;
4908 } else {
4909 level = (((level << 1) + 1) * qscale *
4910 ((int) (quant_matrix[j]))) >> 4;
4911 }
4912 block[j] = level;
4913 sum+=level;
4914 }
4915 }
4916 block[63]^=sum&1;
4917 }
4918
4919 static void dct_unquantize_h263_intra_c(MpegEncContext *s,
4920 DCTELEM *block, int n, int qscale)
4921 {
4922 int i, level, qmul, qadd;
4923 int nCoeffs;
4924
4925 assert(s->block_last_index[n]>=0);
4926
4927 qmul = qscale << 1;
4928
4929 if (!s->h263_aic) {
4793 if (n < 4) 4930 if (n < 4)
4794 block[0] = block[0] * s->y_dc_scale; 4931 block[0] = block[0] * s->y_dc_scale;
4795 else 4932 else
4796 block[0] = block[0] * s->c_dc_scale; 4933 block[0] = block[0] * s->c_dc_scale;
4797 /* XXX: only mpeg1 */ 4934 qadd = (qscale - 1) | 1;
4798 quant_matrix = s->intra_matrix; 4935 }else{
4799 for(i=1;i<=nCoeffs;i++) { 4936 qadd = 0;
4800 int j= s->intra_scantable.permutated[i]; 4937 }
4801 level = block[j]; 4938 if(s->ac_pred)
4802 if (level) { 4939 nCoeffs=63;
4803 if (level < 0) { 4940 else
4804 level = -level;
4805 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4806 level = (level - 1) | 1;
4807 level = -level;
4808 } else {
4809 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4810 level = (level - 1) | 1;
4811 }
4812 #ifdef PARANOID
4813 if (level < -2048 || level > 2047)
4814 fprintf(stderr, "unquant error %d %d\n", i, level);
4815 #endif
4816 block[j] = level;
4817 }
4818 }
4819 } else {
4820 i = 0;
4821 quant_matrix = s->inter_matrix;
4822 for(;i<=nCoeffs;i++) {
4823 int j= s->intra_scantable.permutated[i];
4824 level = block[j];
4825 if (level) {
4826 if (level < 0) {
4827 level = -level;
4828 level = (((level << 1) + 1) * qscale *
4829 ((int) (quant_matrix[j]))) >> 4;
4830 level = (level - 1) | 1;
4831 level = -level;
4832 } else {
4833 level = (((level << 1) + 1) * qscale *
4834 ((int) (quant_matrix[j]))) >> 4;
4835 level = (level - 1) | 1;
4836 }
4837 #ifdef PARANOID
4838 if (level < -2048 || level > 2047)
4839 fprintf(stderr, "unquant error %d %d\n", i, level);
4840 #endif
4841 block[j] = level;
4842 }
4843 }
4844 }
4845 }
4846
4847 static void dct_unquantize_mpeg2_c(MpegEncContext *s,
4848 DCTELEM *block, int n, int qscale)
4849 {
4850 int i, level, nCoeffs;
4851 const uint16_t *quant_matrix;
4852
4853 if(s->alternate_scan) nCoeffs= 63;
4854 else nCoeffs= s->block_last_index[n];
4855
4856 if (s->mb_intra) {
4857 if (n < 4)
4858 block[0] = block[0] * s->y_dc_scale;
4859 else
4860 block[0] = block[0] * s->c_dc_scale;
4861 quant_matrix = s->intra_matrix;
4862 for(i=1;i<=nCoeffs;i++) {
4863 int j= s->intra_scantable.permutated[i];
4864 level = block[j];
4865 if (level) {
4866 if (level < 0) {
4867 level = -level;
4868 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4869 level = -level;
4870 } else {
4871 level = (int)(level * qscale * quant_matrix[j]) >> 3;
4872 }
4873 #ifdef PARANOID
4874 if (level < -2048 || level > 2047)
4875 fprintf(stderr, "unquant error %d %d\n", i, level);
4876 #endif
4877 block[j] = level;
4878 }
4879 }
4880 } else {
4881 int sum=-1;
4882 i = 0;
4883 quant_matrix = s->inter_matrix;
4884 for(;i<=nCoeffs;i++) {
4885 int j= s->intra_scantable.permutated[i];
4886 level = block[j];
4887 if (level) {
4888 if (level < 0) {
4889 level = -level;
4890 level = (((level << 1) + 1) * qscale *
4891 ((int) (quant_matrix[j]))) >> 4;
4892 level = -level;
4893 } else {
4894 level = (((level << 1) + 1) * qscale *
4895 ((int) (quant_matrix[j]))) >> 4;
4896 }
4897 #ifdef PARANOID
4898 if (level < -2048 || level > 2047)
4899 fprintf(stderr, "unquant error %d %d\n", i, level);
4900 #endif
4901 block[j] = level;
4902 sum+=level;
4903 }
4904 }
4905 block[63]^=sum&1;
4906 }
4907 }
4908
4909
4910 static void dct_unquantize_h263_c(MpegEncContext *s,
4911 DCTELEM *block, int n, int qscale)
4912 {
4913 int i, level, qmul, qadd;
4914 int nCoeffs;
4915
4916 assert(s->block_last_index[n]>=0);
4917
4918 qadd = (qscale - 1) | 1;
4919 qmul = qscale << 1;
4920
4921 if (s->mb_intra) {
4922 if (!s->h263_aic) {
4923 if (n < 4)
4924 block[0] = block[0] * s->y_dc_scale;
4925 else
4926 block[0] = block[0] * s->c_dc_scale;
4927 }else
4928 qadd = 0;
4929 i = 1;
4930 nCoeffs= 63; //does not allways use zigzag table
4931 } else {
4932 i = 0;
4933 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ]; 4941 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
4934 } 4942
4935 4943 for(i=1; i<=nCoeffs; i++) {
4936 for(;i<=nCoeffs;i++) {
4937 level = block[i]; 4944 level = block[i];
4938 if (level) { 4945 if (level) {
4939 if (level < 0) { 4946 if (level < 0) {
4940 level = level * qmul - qadd; 4947 level = level * qmul - qadd;
4941 } else { 4948 } else {
4942 level = level * qmul + qadd; 4949 level = level * qmul + qadd;
4943 } 4950 }
4944 #ifdef PARANOID
4945 if (level < -2048 || level > 2047)
4946 fprintf(stderr, "unquant error %d %d\n", i, level);
4947 #endif
4948 block[i] = level; 4951 block[i] = level;
4949 } 4952 }
4950 } 4953 }
4951 } 4954 }
4952 4955
4956 static void dct_unquantize_h263_inter_c(MpegEncContext *s,
4957 DCTELEM *block, int n, int qscale)
4958 {
4959 int i, level, qmul, qadd;
4960 int nCoeffs;
4961
4962 assert(s->block_last_index[n]>=0);
4963
4964 qadd = (qscale - 1) | 1;
4965 qmul = qscale << 1;
4966
4967 nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
4968
4969 for(i=0; i<=nCoeffs; i++) {
4970 level = block[i];
4971 if (level) {
4972 if (level < 0) {
4973 level = level * qmul - qadd;
4974 } else {
4975 level = level * qmul + qadd;
4976 }
4977 block[i] = level;
4978 }
4979 }
4980 }
4953 4981
4954 static const AVOption mpeg4_options[] = 4982 static const AVOption mpeg4_options[] =
4955 { 4983 {
4956 AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000), 4984 AVOPTION_CODEC_INT("bitrate", "desired video bitrate", bit_rate, 4, 240000000, 800000),
4957 AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference" 4985 AVOPTION_CODEC_INT("ratetol", "number of bits the bitstream is allowed to diverge from the reference"