comparison cook.c @ 3014:959b8ad880dc libavcodec

Dual mono stereo strems sound ok now, added sanity checks and removed some unused variables and redundant memsets. Patch by Benjamin Larsson
author rtognimp
date Fri, 06 Jan 2006 12:41:57 +0000
parents f5898b9b8a8a
children 08da190c65e2
comparison
equal deleted inserted replaced
3013:b6a90fabaa76 3014:959b8ad880dc
111 int mlt_size; //modulated lapped transform size 111 int mlt_size; //modulated lapped transform size
112 112
113 /* gain buffers */ 113 /* gain buffers */
114 COOKgain* gain_now_ptr; 114 COOKgain* gain_now_ptr;
115 COOKgain* gain_previous_ptr; 115 COOKgain* gain_previous_ptr;
116 COOKgain gain_copy;
117 COOKgain gain_current; 116 COOKgain gain_current;
118 COOKgain gain_now; 117 COOKgain gain_now;
119 COOKgain gain_previous; 118 COOKgain gain_previous;
119 COOKgain gain_channel1[2];
120 COOKgain gain_channel2[2];
120 121
121 /* VLC data */ 122 /* VLC data */
122 int js_vlc_bits; 123 int js_vlc_bits;
123 VLC envelope_quant_index[13]; 124 VLC envelope_quant_index[13];
124 VLC sqvh[7]; //scalar quantization 125 VLC sqvh[7]; //scalar quantization
136 float mono_mdct_output[2048] __attribute__((aligned(16))); 137 float mono_mdct_output[2048] __attribute__((aligned(16)));
137 float* previous_buffer_ptr[2]; 138 float* previous_buffer_ptr[2];
138 float mono_previous_buffer1[1024]; 139 float mono_previous_buffer1[1024];
139 float mono_previous_buffer2[1024]; 140 float mono_previous_buffer2[1024];
140 float* decode_buf_ptr[4]; 141 float* decode_buf_ptr[4];
142 float* decode_buf_ptr2[2];
141 float decode_buffer_1[1024]; 143 float decode_buffer_1[1024];
142 float decode_buffer_2[1024]; 144 float decode_buffer_2[1024];
143 float decode_buffer_3[1024]; 145 float decode_buffer_3[1024];
144 float decode_buffer_4[1024]; 146 float decode_buffer_4[1024];
145 } COOKContext; 147 } COOKContext;
204 q->gain_size_factor = q->samples_per_channel/8; 206 q->gain_size_factor = q->samples_per_channel/8;
205 for (i=0 ; i<23 ; i++) { 207 for (i=0 ; i<23 ; i++) {
206 q->gain_table[i] = pow((double)q->pow2tab[i+52] , 208 q->gain_table[i] = pow((double)q->pow2tab[i+52] ,
207 (1.0/(double)q->gain_size_factor)); 209 (1.0/(double)q->gain_size_factor));
208 } 210 }
209 memset(&q->gain_copy, 0, sizeof(COOKgain));
210 memset(&q->gain_current, 0, sizeof(COOKgain));
211 memset(&q->gain_now, 0, sizeof(COOKgain));
212 memset(&q->gain_previous, 0, sizeof(COOKgain));
213 } 211 }
214 212
215 213
216 static int init_cook_vlc_tables(COOKContext *q) { 214 static int init_cook_vlc_tables(COOKContext *q) {
217 int i, result; 215 int i, result;
973 // av_log(NULL, AV_LOG_ERROR, "\n"); 971 // av_log(NULL, AV_LOG_ERROR, "\n");
974 972
975 decode_bytes(inbuffer, q->decoded_bytes_buffer, sub_packet_size); 973 decode_bytes(inbuffer, q->decoded_bytes_buffer, sub_packet_size);
976 init_get_bits(&q->gb, q->decoded_bytes_buffer, sub_packet_size*8); 974 init_get_bits(&q->gb, q->decoded_bytes_buffer, sub_packet_size*8);
977 decode_gain_info(&q->gb, &q->gain_current); 975 decode_gain_info(&q->gb, &q->gain_current);
978 memcpy(&q->gain_copy, &q->gain_current ,sizeof(COOKgain)); //This copy does not seem to be used. FIXME
979 976
980 if(q->nb_channels==2 && q->joint_stereo==1){ 977 if(q->nb_channels==2 && q->joint_stereo==1){
981 joint_decode(q, q->decode_buf_ptr[0], q->decode_buf_ptr[2]); 978 joint_decode(q, q->decode_buf_ptr[0], q->decode_buf_ptr[2]);
982 979
983 /* Swap buffer pointers. */ 980 /* Swap buffer pointers. */
1015 memcpy(&q->gain_now, &q->gain_previous, sizeof(COOKgain)); 1012 memcpy(&q->gain_now, &q->gain_previous, sizeof(COOKgain));
1016 memcpy(&q->gain_previous, &q->gain_current, sizeof(COOKgain)); 1013 memcpy(&q->gain_previous, &q->gain_current, sizeof(COOKgain));
1017 1014
1018 } else if (q->nb_channels==2 && q->joint_stereo==0) { 1015 } else if (q->nb_channels==2 && q->joint_stereo==0) {
1019 /* channel 0 */ 1016 /* channel 0 */
1020 mono_decode(q, q->decode_buf_ptr[0]); 1017 mono_decode(q, q->decode_buf_ptr2[0]);
1021 1018
1022 tmp_ptr = q->decode_buf_ptr[0]; 1019 tmp_ptr = q->decode_buf_ptr2[0];
1023 q->decode_buf_ptr[0] = q->decode_buf_ptr[1]; 1020 q->decode_buf_ptr2[0] = q->decode_buf_ptr2[1];
1024 q->decode_buf_ptr[1] = q->decode_buf_ptr[2]; 1021 q->decode_buf_ptr2[1] = tmp_ptr;
1025 q->decode_buf_ptr[2] = q->decode_buf_ptr[3]; 1022
1026 q->decode_buf_ptr[3] = tmp_ptr; 1023 memcpy(&q->gain_channel1[0], &q->gain_current ,sizeof(COOKgain));
1027 1024 q->gain_now_ptr = &q->gain_channel1[0];
1028 q->gain_now_ptr = &q->gain_now; 1025 q->gain_previous_ptr = &q->gain_channel1[1];
1029 q->gain_previous_ptr = &q->gain_previous; 1026
1030 1027 cook_imlt(q, q->decode_buf_ptr2[0], q->mono_mdct_output,q->mlt_tmp);
1031 cook_imlt(q, q->decode_buf_ptr[0], q->mono_mdct_output,q->mlt_tmp);
1032 gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, 1028 gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr,
1033 q->gain_previous_ptr, q->previous_buffer_ptr[0]); 1029 q->gain_previous_ptr, q->mono_previous_buffer1);
1034 /* Swap out the previous buffer. */ 1030
1035 tmp_ptr = q->previous_buffer_ptr[0]; 1031 memcpy(&q->gain_channel1[1], &q->gain_channel1[0],sizeof(COOKgain));
1036 q->previous_buffer_ptr[0] = q->previous_buffer_ptr[1]; 1032
1037 q->previous_buffer_ptr[1] = tmp_ptr;
1038 1033
1039 for (j=0 ; j<q->samples_per_frame ; j++){ 1034 for (j=0 ; j<q->samples_per_frame ; j++){
1040 value = lrintf(q->mono_mdct_output[j]); 1035 value = lrintf(q->mono_mdct_output[j]);
1041 if(value < -32768) value = -32768; 1036 if(value < -32768) value = -32768;
1042 else if(value > 32767) value = 32767; 1037 else if(value > 32767) value = 32767;
1044 } 1039 }
1045 1040
1046 /* channel 1 */ 1041 /* channel 1 */
1047 //av_log(NULL,AV_LOG_ERROR,"bits = %d\n",get_bits_count(&q->gb)); 1042 //av_log(NULL,AV_LOG_ERROR,"bits = %d\n",get_bits_count(&q->gb));
1048 init_get_bits(&q->gb, q->decoded_bytes_buffer, sub_packet_size*8+q->bits_per_subpacket); 1043 init_get_bits(&q->gb, q->decoded_bytes_buffer, sub_packet_size*8+q->bits_per_subpacket);
1049 decode_gain_info(&q->gb, &q->gain_current); 1044
1050 //memcpy(&q->gain_copy, &q->gain_current ,sizeof(COOKgain)); 1045 q->gain_now_ptr = &q->gain_channel2[0];
1046 q->gain_previous_ptr = &q->gain_channel2[1];
1047
1048 decode_gain_info(&q->gb, &q->gain_channel2[0]);
1051 mono_decode(q, q->decode_buf_ptr[0]); 1049 mono_decode(q, q->decode_buf_ptr[0]);
1050
1052 tmp_ptr = q->decode_buf_ptr[0]; 1051 tmp_ptr = q->decode_buf_ptr[0];
1053 q->decode_buf_ptr[1] = q->decode_buf_ptr[2]; 1052 q->decode_buf_ptr[0] = q->decode_buf_ptr[1];
1054 q->decode_buf_ptr[2] = q->decode_buf_ptr[3]; 1053 q->decode_buf_ptr[1] = tmp_ptr;
1055 q->decode_buf_ptr[3] = tmp_ptr;
1056
1057 q->gain_now_ptr = &q->gain_now;
1058 q->gain_previous_ptr = &q->gain_previous;
1059 1054
1060 cook_imlt(q, q->decode_buf_ptr[0], q->mono_mdct_output,q->mlt_tmp); 1055 cook_imlt(q, q->decode_buf_ptr[0], q->mono_mdct_output,q->mlt_tmp);
1061 gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, q->gain_previous_ptr, q->previous_buffer_ptr[0]); 1056 gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr,
1057 q->gain_previous_ptr, q->mono_previous_buffer2);
1062 1058
1063 /* Swap out the previous buffer. */ 1059 /* Swap out the previous buffer. */
1064 tmp_ptr = q->previous_buffer_ptr[0]; 1060 tmp_ptr = q->previous_buffer_ptr[0];
1065 q->previous_buffer_ptr[0] = q->previous_buffer_ptr[1]; 1061 q->previous_buffer_ptr[0] = q->previous_buffer_ptr[1];
1066 q->previous_buffer_ptr[1] = tmp_ptr; 1062 q->previous_buffer_ptr[1] = tmp_ptr;
1063
1064 memcpy(&q->gain_channel2[1], &q->gain_channel2[0] ,sizeof(COOKgain));
1067 1065
1068 for (j=0 ; j<q->samples_per_frame ; j++){ 1066 for (j=0 ; j<q->samples_per_frame ; j++){
1069 value = lrintf(q->mono_mdct_output[j]); 1067 value = lrintf(q->mono_mdct_output[j]);
1070 if(value < -32768) value = -32768; 1068 if(value < -32768) value = -32768;
1071 else if(value > 32767) value = 32767; 1069 else if(value > 32767) value = 32767;
1072 outbuffer[2*j] = value; 1070 outbuffer[2*j] = value;
1073 } 1071 }
1074
1075
1076 /* Swap out the previous buffer. */
1077 memcpy(&q->gain_now, &q->gain_previous, sizeof(COOKgain));
1078 memcpy(&q->gain_previous, &q->gain_current, sizeof(COOKgain));
1079 1072
1080 } else { 1073 } else {
1081 mono_decode(q, q->decode_buf_ptr[0]); 1074 mono_decode(q, q->decode_buf_ptr[0]);
1082 1075
1083 /* Swap buffer pointers. */ 1076 /* Swap buffer pointers. */
1212 av_log(NULL,AV_LOG_DEBUG,"MONO_COOK1\n"); 1205 av_log(NULL,AV_LOG_DEBUG,"MONO_COOK1\n");
1213 break; 1206 break;
1214 case MONO_COOK2: 1207 case MONO_COOK2:
1215 if (q->nb_channels != 1) { 1208 if (q->nb_channels != 1) {
1216 q->joint_stereo = 0; 1209 q->joint_stereo = 0;
1217 av_log(NULL,AV_LOG_ERROR,"Non-joint-stereo files are decoded with wrong gain at the moment!\n");
1218 q->bits_per_subpacket = q->bits_per_subpacket/2; 1210 q->bits_per_subpacket = q->bits_per_subpacket/2;
1219
1220 } 1211 }
1221 av_log(NULL,AV_LOG_DEBUG,"MONO_COOK2\n"); 1212 av_log(NULL,AV_LOG_DEBUG,"MONO_COOK2\n");
1222 break; 1213 break;
1223 case JOINT_STEREO: 1214 case JOINT_STEREO:
1224 if (q->nb_channels != 2) { 1215 if (q->nb_channels != 2) {
1269 q->decode_buf_ptr[0] = q->decode_buffer_1; 1260 q->decode_buf_ptr[0] = q->decode_buffer_1;
1270 q->decode_buf_ptr[1] = q->decode_buffer_2; 1261 q->decode_buf_ptr[1] = q->decode_buffer_2;
1271 q->decode_buf_ptr[2] = q->decode_buffer_3; 1262 q->decode_buf_ptr[2] = q->decode_buffer_3;
1272 q->decode_buf_ptr[3] = q->decode_buffer_4; 1263 q->decode_buf_ptr[3] = q->decode_buffer_4;
1273 1264
1265 q->decode_buf_ptr2[0] = q->decode_buffer_3;
1266 q->decode_buf_ptr2[1] = q->decode_buffer_4;
1267
1274 q->previous_buffer_ptr[0] = q->mono_previous_buffer1; 1268 q->previous_buffer_ptr[0] = q->mono_previous_buffer1;
1275 q->previous_buffer_ptr[1] = q->mono_previous_buffer2; 1269 q->previous_buffer_ptr[1] = q->mono_previous_buffer2;
1276
1277 memset(q->decode_buffer_1,0,1024*sizeof(float));
1278 memset(q->decode_buffer_2,0,1024*sizeof(float));
1279 memset(q->decode_buffer_3,0,1024*sizeof(float));
1280 memset(q->decode_buffer_4,0,1024*sizeof(float));
1281 1270
1282 /* Initialize transform. */ 1271 /* Initialize transform. */
1283 if ( init_cook_mlt(q) == 0 ) 1272 if ( init_cook_mlt(q) == 0 )
1284 return -1; 1273 return -1;
1274
1275 /* Try to catch some obviously faulty streams, othervise it might be exploitable */
1276 if (q->total_subbands > 53) {
1277 av_log(NULL,AV_LOG_ERROR,"total_subbands > 53, report sample!\n");
1278 return -1;
1279 }
1280 if (((q->subbands > 34) || (q->js_subband_start > 19)) && (q->joint_stereo)) {
1281 av_log(NULL,AV_LOG_ERROR,"subbands > 34 or js_subband_start > 19, report sample!\n");
1282 return -1;
1283 }
1284 if (q->subbands > 50) {
1285 av_log(NULL,AV_LOG_ERROR,"subbands > 50, report sample!\n");
1286 return -1;
1287 }
1288
1285 #ifdef COOKDEBUG 1289 #ifdef COOKDEBUG
1286 dump_cook_context(q,e); 1290 dump_cook_context(q,e);
1287 #endif 1291 #endif
1288 return 0; 1292 return 0;
1289 } 1293 }