Mercurial > libavcodec.hg
comparison cook.c @ 4424:8c830bde8006 libavcodec
Cook stereo (MONO_COOK2) bugfix, by Ian Braithwaite.
ian at braithwaite dot dk.
author | banan |
---|---|
date | Sun, 28 Jan 2007 10:05:21 +0000 |
parents | 96a09fef9a67 |
children | aca324d5bd58 |
comparison
equal
deleted
inserted
replaced
4423:96a09fef9a67 | 4424:8c830bde8006 |
---|---|
275 /*************** init functions end ***********/ | 275 /*************** init functions end ***********/ |
276 | 276 |
277 /** | 277 /** |
278 * Cook indata decoding, every 32 bits are XORed with 0x37c511f2. | 278 * Cook indata decoding, every 32 bits are XORed with 0x37c511f2. |
279 * Why? No idea, some checksum/error detection method maybe. | 279 * Why? No idea, some checksum/error detection method maybe. |
280 * | |
281 * Out buffer size: extra bytes are needed to cope with | |
282 * padding/missalignment. | |
283 * Subpackets passed to the decoder can contain two, consecutive | |
284 * half-subpackets, of identical but arbitrary size. | |
285 * 1234 1234 1234 1234 extraA extraB | |
286 * Case 1: AAAA BBBB 0 0 | |
287 * Case 2: AAAA ABBB BB-- 3 3 | |
288 * Case 3: AAAA AABB BBBB 2 2 | |
289 * Case 4: AAAA AAAB BBBB BB-- 1 5 | |
290 * | |
280 * Nice way to waste CPU cycles. | 291 * Nice way to waste CPU cycles. |
281 * | 292 * |
282 * @param in pointer to 32bit array of indata | 293 * @param inbuffer pointer to byte array of indata |
283 * @param bits amount of bits | 294 * @param out pointer to byte array of outdata |
284 * @param out pointer to 32bit array of outdata | 295 * @param bytes number of bytes |
285 */ | 296 */ |
286 | 297 #define DECODE_BYTES_PAD1(bytes) (3 - ((bytes)+3) % 4) |
287 static inline void decode_bytes(uint8_t* inbuffer, uint8_t* out, int bytes){ | 298 #define DECODE_BYTES_PAD2(bytes) ((bytes) % 4 + DECODE_BYTES_PAD1(2 * (bytes))) |
288 int i; | 299 |
289 uint32_t* buf = (uint32_t*) inbuffer; | 300 static inline int decode_bytes(uint8_t* inbuffer, uint8_t* out, int bytes){ |
301 int i, off; | |
302 uint32_t c; | |
303 uint32_t* buf; | |
290 uint32_t* obuf = (uint32_t*) out; | 304 uint32_t* obuf = (uint32_t*) out; |
291 /* FIXME: 64 bit platforms would be able to do 64 bits at a time. | 305 /* FIXME: 64 bit platforms would be able to do 64 bits at a time. |
292 * I'm too lazy though, should be something like | 306 * I'm too lazy though, should be something like |
293 * for(i=0 ; i<bitamount/64 ; i++) | 307 * for(i=0 ; i<bitamount/64 ; i++) |
294 * (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]); | 308 * (int64_t)out[i] = 0x37c511f237c511f2^be2me_64(int64_t)in[i]); |
295 * Buffer alignment needs to be checked. */ | 309 * Buffer alignment needs to be checked. */ |
296 | 310 |
297 | 311 off = (uint32_t)inbuffer % 4; |
298 for(i=0 ; i<bytes/4 ; i++){ | 312 buf = (uint32_t*) (inbuffer - off); |
299 #ifdef WORDS_BIGENDIAN | 313 c = be2me_32((0x37c511f2 >> (off*8)) | (0x37c511f2 << (32-(off*8)))); |
300 obuf[i] = 0x37c511f2^buf[i]; | 314 bytes += 3 + off; |
301 #else | 315 for (i = 0; i < bytes/4; i++) |
302 obuf[i] = 0xf211c537^buf[i]; | 316 obuf[i] = c ^ buf[i]; |
303 #endif | 317 |
304 } | 318 return off; |
305 } | 319 } |
306 | 320 |
307 /** | 321 /** |
308 * Cook uninit | 322 * Cook uninit |
309 */ | 323 */ |
946 idx = (1 << q->js_vlc_bits) - 1; | 960 idx = (1 << q->js_vlc_bits) - 1; |
947 } | 961 } |
948 } | 962 } |
949 | 963 |
950 /** | 964 /** |
965 * First part of subpacket decoding: | |
966 * decode raw stream bytes and read gain info. | |
967 * | |
968 * @param q pointer to the COOKContext | |
969 * @param inbuffer pointer to raw stream data | |
970 * @param gain_ptr array of current/prev gain pointers | |
971 */ | |
972 | |
973 static inline void | |
974 decode_bytes_and_gain(COOKContext *q, uint8_t *inbuffer, COOKgain *gain_ptr) | |
975 { | |
976 int offset; | |
977 | |
978 offset = decode_bytes(inbuffer, q->decoded_bytes_buffer, | |
979 q->bits_per_subpacket/8); | |
980 init_get_bits(&q->gb, q->decoded_bytes_buffer + offset, | |
981 q->bits_per_subpacket); | |
982 decode_gain_info(&q->gb, gain_ptr); | |
983 } | |
984 | |
985 | |
986 /** | |
951 * Cook subpacket decoding. This function returns one decoded subpacket, | 987 * Cook subpacket decoding. This function returns one decoded subpacket, |
952 * usually 1024 samples per channel. | 988 * usually 1024 samples per channel. |
953 * | 989 * |
954 * @param q pointer to the COOKContext | 990 * @param q pointer to the COOKContext |
955 * @param inbuffer pointer to the inbuffer | 991 * @param inbuffer pointer to the inbuffer |
968 // for (i=0 ; i<sub_packet_size ; i++) { | 1004 // for (i=0 ; i<sub_packet_size ; i++) { |
969 // av_log(NULL, AV_LOG_ERROR, "%02x", inbuffer[i]); | 1005 // av_log(NULL, AV_LOG_ERROR, "%02x", inbuffer[i]); |
970 // } | 1006 // } |
971 // av_log(NULL, AV_LOG_ERROR, "\n"); | 1007 // av_log(NULL, AV_LOG_ERROR, "\n"); |
972 | 1008 |
973 decode_bytes(inbuffer, q->decoded_bytes_buffer, sub_packet_size); | |
974 init_get_bits(&q->gb, q->decoded_bytes_buffer, sub_packet_size*8); | |
975 decode_gain_info(&q->gb, &q->gain_current); | |
976 | |
977 if(q->nb_channels==2 && q->joint_stereo==1){ | 1009 if(q->nb_channels==2 && q->joint_stereo==1){ |
1010 decode_bytes_and_gain(q, inbuffer, &q->gain_current); | |
1011 | |
978 joint_decode(q, q->decode_buf_ptr[0], q->decode_buf_ptr[2]); | 1012 joint_decode(q, q->decode_buf_ptr[0], q->decode_buf_ptr[2]); |
979 | 1013 |
980 /* Swap buffer pointers. */ | 1014 /* Swap buffer pointers. */ |
981 tmp_ptr = q->decode_buf_ptr[1]; | 1015 tmp_ptr = q->decode_buf_ptr[1]; |
982 q->decode_buf_ptr[1] = q->decode_buf_ptr[0]; | 1016 q->decode_buf_ptr[1] = q->decode_buf_ptr[0]; |
1012 memcpy(&q->gain_now, &q->gain_previous, sizeof(COOKgain)); | 1046 memcpy(&q->gain_now, &q->gain_previous, sizeof(COOKgain)); |
1013 memcpy(&q->gain_previous, &q->gain_current, sizeof(COOKgain)); | 1047 memcpy(&q->gain_previous, &q->gain_current, sizeof(COOKgain)); |
1014 | 1048 |
1015 } else if (q->nb_channels==2 && q->joint_stereo==0) { | 1049 } else if (q->nb_channels==2 && q->joint_stereo==0) { |
1016 /* channel 0 */ | 1050 /* channel 0 */ |
1051 decode_bytes_and_gain(q, inbuffer, &q->gain_current); | |
1052 | |
1017 mono_decode(q, q->decode_buf_ptr2[0]); | 1053 mono_decode(q, q->decode_buf_ptr2[0]); |
1018 | 1054 |
1019 tmp_ptr = q->decode_buf_ptr2[0]; | 1055 tmp_ptr = q->decode_buf_ptr2[0]; |
1020 q->decode_buf_ptr2[0] = q->decode_buf_ptr2[1]; | 1056 q->decode_buf_ptr2[0] = q->decode_buf_ptr2[1]; |
1021 q->decode_buf_ptr2[1] = tmp_ptr; | 1057 q->decode_buf_ptr2[1] = tmp_ptr; |
1028 gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, | 1064 gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, |
1029 q->gain_previous_ptr, q->mono_previous_buffer1); | 1065 q->gain_previous_ptr, q->mono_previous_buffer1); |
1030 | 1066 |
1031 memcpy(&q->gain_channel1[1], &q->gain_channel1[0],sizeof(COOKgain)); | 1067 memcpy(&q->gain_channel1[1], &q->gain_channel1[0],sizeof(COOKgain)); |
1032 | 1068 |
1069 | |
1070 for (j=0 ; j<q->samples_per_frame ; j++){ | |
1071 value = lrintf(q->mono_mdct_output[j]); | |
1072 if(value < -32768) value = -32768; | |
1073 else if(value > 32767) value = 32767; | |
1074 outbuffer[2*j] = value; | |
1075 } | |
1076 | |
1077 /* channel 1 */ | |
1078 //av_log(NULL,AV_LOG_ERROR,"bits = %d\n",get_bits_count(&q->gb)); | |
1079 decode_bytes_and_gain(q, inbuffer + sub_packet_size/2, | |
1080 &q->gain_channel2[0]); | |
1081 | |
1082 q->gain_now_ptr = &q->gain_channel2[0]; | |
1083 q->gain_previous_ptr = &q->gain_channel2[1]; | |
1084 | |
1085 mono_decode(q, q->decode_buf_ptr[0]); | |
1086 | |
1087 tmp_ptr = q->decode_buf_ptr[0]; | |
1088 q->decode_buf_ptr[0] = q->decode_buf_ptr[1]; | |
1089 q->decode_buf_ptr[1] = tmp_ptr; | |
1090 | |
1091 cook_imlt(q, q->decode_buf_ptr[0], q->mono_mdct_output,q->mlt_tmp); | |
1092 gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, | |
1093 q->gain_previous_ptr, q->mono_previous_buffer2); | |
1094 | |
1095 /* Swap out the previous buffer. */ | |
1096 tmp_ptr = q->previous_buffer_ptr[0]; | |
1097 q->previous_buffer_ptr[0] = q->previous_buffer_ptr[1]; | |
1098 q->previous_buffer_ptr[1] = tmp_ptr; | |
1099 | |
1100 memcpy(&q->gain_channel2[1], &q->gain_channel2[0] ,sizeof(COOKgain)); | |
1033 | 1101 |
1034 for (j=0 ; j<q->samples_per_frame ; j++){ | 1102 for (j=0 ; j<q->samples_per_frame ; j++){ |
1035 value = lrintf(q->mono_mdct_output[j]); | 1103 value = lrintf(q->mono_mdct_output[j]); |
1036 if(value < -32768) value = -32768; | 1104 if(value < -32768) value = -32768; |
1037 else if(value > 32767) value = 32767; | 1105 else if(value > 32767) value = 32767; |
1038 outbuffer[2*j+1] = value; | 1106 outbuffer[2*j+1] = value; |
1039 } | 1107 } |
1040 | 1108 |
1041 /* channel 1 */ | |
1042 //av_log(NULL,AV_LOG_ERROR,"bits = %d\n",get_bits_count(&q->gb)); | |
1043 init_get_bits(&q->gb, q->decoded_bytes_buffer, sub_packet_size*8+q->bits_per_subpacket); | |
1044 | |
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]); | |
1049 mono_decode(q, q->decode_buf_ptr[0]); | |
1050 | |
1051 tmp_ptr = q->decode_buf_ptr[0]; | |
1052 q->decode_buf_ptr[0] = q->decode_buf_ptr[1]; | |
1053 q->decode_buf_ptr[1] = tmp_ptr; | |
1054 | |
1055 cook_imlt(q, q->decode_buf_ptr[0], q->mono_mdct_output,q->mlt_tmp); | |
1056 gain_compensate(q, q->mono_mdct_output, q->gain_now_ptr, | |
1057 q->gain_previous_ptr, q->mono_previous_buffer2); | |
1058 | |
1059 /* Swap out the previous buffer. */ | |
1060 tmp_ptr = q->previous_buffer_ptr[0]; | |
1061 q->previous_buffer_ptr[0] = q->previous_buffer_ptr[1]; | |
1062 q->previous_buffer_ptr[1] = tmp_ptr; | |
1063 | |
1064 memcpy(&q->gain_channel2[1], &q->gain_channel2[0] ,sizeof(COOKgain)); | |
1065 | |
1066 for (j=0 ; j<q->samples_per_frame ; j++){ | |
1067 value = lrintf(q->mono_mdct_output[j]); | |
1068 if(value < -32768) value = -32768; | |
1069 else if(value > 32767) value = 32767; | |
1070 outbuffer[2*j] = value; | |
1071 } | |
1072 | |
1073 } else { | 1109 } else { |
1110 decode_bytes_and_gain(q, inbuffer, &q->gain_current); | |
1111 | |
1074 mono_decode(q, q->decode_buf_ptr[0]); | 1112 mono_decode(q, q->decode_buf_ptr[0]); |
1075 | 1113 |
1076 /* Swap buffer pointers. */ | 1114 /* Swap buffer pointers. */ |
1077 tmp_ptr = q->decode_buf_ptr[1]; | 1115 tmp_ptr = q->decode_buf_ptr[1]; |
1078 q->decode_buf_ptr[1] = q->decode_buf_ptr[0]; | 1116 q->decode_buf_ptr[1] = q->decode_buf_ptr[0]; |
1256 | 1294 |
1257 | 1295 |
1258 if(avctx->block_align >= UINT_MAX/2) | 1296 if(avctx->block_align >= UINT_MAX/2) |
1259 return -1; | 1297 return -1; |
1260 | 1298 |
1261 /* Pad the databuffer with FF_INPUT_BUFFER_PADDING_SIZE, | 1299 /* Pad the databuffer with: |
1262 this is for the bitstreamreader. */ | 1300 DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(), |
1263 if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-avctx->block_align%4) + FF_INPUT_BUFFER_PADDING_SIZE)*sizeof(uint8_t))) == NULL) | 1301 FF_INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */ |
1302 if (q->nb_channels==2 && q->joint_stereo==0) { | |
1303 q->decoded_bytes_buffer = | |
1304 av_mallocz(avctx->block_align/2 | |
1305 + DECODE_BYTES_PAD2(avctx->block_align/2) | |
1306 + FF_INPUT_BUFFER_PADDING_SIZE); | |
1307 } else { | |
1308 q->decoded_bytes_buffer = | |
1309 av_mallocz(avctx->block_align | |
1310 + DECODE_BYTES_PAD1(avctx->block_align) | |
1311 + FF_INPUT_BUFFER_PADDING_SIZE); | |
1312 } | |
1313 if (q->decoded_bytes_buffer == NULL) | |
1264 return -1; | 1314 return -1; |
1265 | 1315 |
1266 q->decode_buf_ptr[0] = q->decode_buffer_1; | 1316 q->decode_buf_ptr[0] = q->decode_buffer_1; |
1267 q->decode_buf_ptr[1] = q->decode_buffer_2; | 1317 q->decode_buf_ptr[1] = q->decode_buffer_2; |
1268 q->decode_buf_ptr[2] = q->decode_buffer_3; | 1318 q->decode_buf_ptr[2] = q->decode_buffer_3; |