summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohamed Tarek <mt@rockbox.org>2009-08-14 08:24:39 +0000
committerMohamed Tarek <mt@rockbox.org>2009-08-14 08:24:39 +0000
commit0b6aa3851631eacba413d965f931f59cc8e37773 (patch)
treea21580e6bc4ecfb8524c42e7517bc27bf19f53e6
parentebd67b1c6f06b93235700bfef0011b331669ee2f (diff)
downloadrockbox-0b6aa3851631eacba413d965f931f59cc8e37773.zip
rockbox-0b6aa3851631eacba413d965f931f59cc8e37773.tar.gz
rockbox-0b6aa3851631eacba413d965f931f59cc8e37773.tar.bz2
rockbox-0b6aa3851631eacba413d965f931f59cc8e37773.tar.xz
Move main() outside atrac3.c and create atrac3.h
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@22307 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/codecs/libatrac/Makefile.test2
-rw-r--r--apps/codecs/libatrac/atrac3.c246
-rw-r--r--apps/codecs/libatrac/atrac3.h80
-rw-r--r--apps/codecs/libatrac/main.c167
4 files changed, 251 insertions, 244 deletions
diff --git a/apps/codecs/libatrac/Makefile.test b/apps/codecs/libatrac/Makefile.test
index 13809d1..f9a1c2f 100644
--- a/apps/codecs/libatrac/Makefile.test
+++ b/apps/codecs/libatrac/Makefile.test
@@ -1,5 +1,5 @@
CFLAGS = -Wall -O3 -DTEST -D"DEBUGF=printf" -D"ROCKBOX_LITTLE_ENDIAN=1" -D"ICONST_ATTR=" -D"ICODE_ATTR="
-OBJS = atrac3.o bitstream.o ../librm/rm.o fixp_math.o ../lib/mdct2.o ../lib/mdct_lookup.o
+OBJS = atrac3.o bitstream.o ../librm/rm.o fixp_math.o ../lib/mdct2.o ../lib/mdct_lookup.o main.o
atractest: $(OBJS)
gcc -o atractest $(OBJS)
diff --git a/apps/codecs/libatrac/atrac3.c b/apps/codecs/libatrac/atrac3.c
index f7181f7..26bffd4 100644
--- a/apps/codecs/libatrac/atrac3.c
+++ b/apps/codecs/libatrac/atrac3.c
@@ -36,21 +36,11 @@
#include <stddef.h>
#include <stdio.h>
-#include "bitstream.h"
#include "bytestream.h"
-
-#include <stdint.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <string.h>
-
-#include "../librm/rm.h"
+#include "atrac3.h"
#include "atrac3data.h"
#include "atrac3data_fixed.h"
#include "fixp_math.h"
-//#include "fixp_mdct.h"
#include "../lib/mdct2.h"
#define JOINT_STEREO 0x12
@@ -58,77 +48,6 @@
#define AVERROR(...) -1
-/* These structures are needed to store the parsed gain control data. */
-typedef struct {
- int num_gain_data;
- int levcode[8];
- int loccode[8];
-} gain_info;
-
-typedef struct {
- gain_info gBlock[4];
-} gain_block;
-
-typedef struct {
- int pos;
- int numCoefs;
- int32_t coef[8];
-} tonal_component;
-
-typedef struct {
- int bandsCoded;
- int numComponents;
- tonal_component components[64];
- int32_t prevFrame[1024];
- int gcBlkSwitch;
- gain_block gainBlock[2];
-
- int32_t spectrum[1024] __attribute__((aligned(16)));
- int32_t IMDCT_buf[1024] __attribute__((aligned(16)));
-
- int32_t delayBuf1[46]; ///<qmf delay buffers
- int32_t delayBuf2[46];
- int32_t delayBuf3[46];
-} channel_unit;
-
-typedef struct {
- GetBitContext gb;
- //@{
- /** stream data */
- int channels;
- int codingMode;
- int bit_rate;
- int sample_rate;
- int samples_per_channel;
- int samples_per_frame;
-
- int bits_per_frame;
- int bytes_per_frame;
- int pBs;
- channel_unit* pUnits;
- //@}
- //@{
- /** joint-stereo related variables */
- int matrix_coeff_index_prev[4];
- int matrix_coeff_index_now[4];
- int matrix_coeff_index_next[4];
- int weighting_delay[6];
- //@}
- //@{
- /** data buffers */
- int32_t outSamples[2048];
- uint8_t decoded_bytes_buffer[1024];
- int32_t tempBuf[1070];
- //@}
- //@{
- /** extradata */
- int atrac3version;
- int delay;
- int scrambled_stream;
- int frame_factor;
- //@}
-} ATRAC3Context;
-
static int32_t qmf_window[48];
static VLC spectral_coeff_tab[7];
static channel_unit channel_units[2];
@@ -859,7 +778,7 @@ static int decodeFrame(ATRAC3Context *q, const uint8_t* databuf)
* @param rmctx pointer to the AVCodecContext
*/
-static int atrac3_decode_frame(RMContext *rmctx, ATRAC3Context *q,
+int atrac3_decode_frame(RMContext *rmctx, ATRAC3Context *q,
void *data, int *data_size,
const uint8_t *buf, int buf_size) {
int result = 0, i;
@@ -908,7 +827,7 @@ static int atrac3_decode_frame(RMContext *rmctx, ATRAC3Context *q,
* @param rmctx pointer to the RMContext
*/
-static av_cold int atrac3_decode_init(ATRAC3Context *q, RMContext *rmctx)
+int atrac3_decode_init(ATRAC3Context *q, RMContext *rmctx)
{
int i;
const uint8_t *edata_ptr = rmctx->codec_extradata;
@@ -1032,162 +951,3 @@ static av_cold int atrac3_decode_init(ATRAC3Context *q, RMContext *rmctx)
return 0;
}
-/***************************************************************
- * Following is a test program to convert from atrac/rm to wav *
- ***************************************************************/
-static unsigned char wav_header[44]={
- 'R','I','F','F',// 0 - ChunkID
- 0,0,0,0, // 4 - ChunkSize (filesize-8)
- 'W','A','V','E',// 8 - Format
- 'f','m','t',' ',// 12 - SubChunkID
- 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
- 1,0, // 20 - AudioFormat (1=Uncompressed)
- 2,0, // 22 - NumChannels
- 0,0,0,0, // 24 - SampleRate in Hz
- 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
- 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
- 16,0, // 34 - BitsPerSample
- 'd','a','t','a',// 36 - Subchunk2ID
- 0,0,0,0 // 40 - Subchunk2Size
-};
-
-int open_wav(char* filename) {
- int fd,res;
-
- fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR);
- if (fd >= 0) {
- res = write(fd,wav_header,sizeof(wav_header));
- }
-
- return(fd);
-}
-
-void close_wav(int fd, RMContext *rmctx, ATRAC3Context *q) {
- int x,res;
- int filesize;
- int bytes_per_sample = 2;
- int samples_per_frame = q->samples_per_frame;
- int nb_channels = rmctx->nb_channels;
- int sample_rate = rmctx->sample_rate;
- int nb_frames = rmctx->audio_framesize/rmctx->block_align * rmctx->nb_packets - 2; // first 2 frames have no valid audio; skipped in output
-
- filesize= samples_per_frame*bytes_per_sample*nb_frames +44;
- printf("Filesize = %d\n",filesize);
-
- // ChunkSize
- x=filesize-8;
- wav_header[4]=(x&0xff);
- wav_header[5]=(x&0xff00)>>8;
- wav_header[6]=(x&0xff0000)>>16;
- wav_header[7]=(x&0xff000000)>>24;
-
- // Number of channels
- wav_header[22]=nb_channels;
-
- // Samplerate
- wav_header[24]=sample_rate&0xff;
- wav_header[25]=(sample_rate&0xff00)>>8;
- wav_header[26]=(sample_rate&0xff0000)>>16;
- wav_header[27]=(sample_rate&0xff000000)>>24;
-
- // ByteRate
- x=sample_rate*bytes_per_sample*nb_channels;
- wav_header[28]=(x&0xff);
- wav_header[29]=(x&0xff00)>>8;
- wav_header[30]=(x&0xff0000)>>16;
- wav_header[31]=(x&0xff000000)>>24;
-
- // BlockAlign
- wav_header[32]=rmctx->block_align;//2*rmctx->nb_channels;
-
- // Bits per sample
- wav_header[34]=16;
-
- // Subchunk2Size
- x=filesize-44;
- wav_header[40]=(x&0xff);
- wav_header[41]=(x&0xff00)>>8;
- wav_header[42]=(x&0xff0000)>>16;
- wav_header[43]=(x&0xff000000)>>24;
-
- lseek(fd,0,SEEK_SET);
- res = write(fd,wav_header,sizeof(wav_header));
- close(fd);
-}
-
-int main(int argc, char *argv[])
-{
- int fd, fd_dec;
- int res, i, datasize = 0;
-
-#ifdef DUMP_RAW_FRAMES
- char filename[15];
- int fd_out;
-#endif
- int16_t outbuf[2048];
- uint16_t fs,sps,h;
- uint32_t packet_count;
- ATRAC3Context q;
- RMContext rmctx;
- RMPacket pkt;
-
- memset(&q,0,sizeof(ATRAC3Context));
- memset(&rmctx,0,sizeof(RMContext));
- memset(&pkt,0,sizeof(RMPacket));
-
- if (argc != 2) {
- DEBUGF("Incorrect number of arguments\n");
- return -1;
- }
-
- fd = open(argv[1],O_RDONLY);
- if (fd < 0) {
- DEBUGF("Error opening file %s\n", argv[1]);
- return -1;
- }
-
- /* copy the input rm file to a memory buffer */
- uint8_t * filebuf = (uint8_t *)calloc((int)filesize(fd),sizeof(uint8_t));
- res = read(fd,filebuf,filesize(fd));
-
- fd_dec = open_wav("output.wav");
- if (fd_dec < 0) {
- DEBUGF("Error creating output file\n");
- return -1;
- }
- res = real_parse_header(fd, &rmctx);
- packet_count = rmctx.nb_packets;
- rmctx.audio_framesize = rmctx.block_align;
- rmctx.block_align = rmctx.sub_packet_size;
- fs = rmctx.audio_framesize;
- sps= rmctx.block_align;
- h = rmctx.sub_packet_h;
- atrac3_decode_init(&q,&rmctx);
-
- /* change the buffer pointer to point at the first audio frame */
- advance_buffer(&filebuf, rmctx.data_offset + DATA_HEADER_SIZE);
- while(packet_count)
- {
- rm_get_packet(&filebuf, &rmctx, &pkt);
- for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
- {
- /* output raw audio frames that are sent to the decoder into separate files */
-#ifdef DUMP_RAW_FRAMES
- snprintf(filename,sizeof(filename),"dump%d.raw",++x);
- fd_out = open(filename,O_WRONLY|O_CREAT|O_APPEND);
- write(fd_out,pkt.frames[i],sps);
- close(fd_out);
-#endif
- if(pkt.length > 0)
- res = atrac3_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i] , rmctx.block_align);
- rmctx.frame_number++;
- res = write(fd_dec,outbuf,datasize);
- }
- packet_count -= rmctx.audio_pkt_cnt;
- rmctx.audio_pkt_cnt = 0;
- }
- close_wav(fd_dec, &rmctx, &q);
- close(fd);
-
- return 0;
-}
diff --git a/apps/codecs/libatrac/atrac3.h b/apps/codecs/libatrac/atrac3.h
new file mode 100644
index 0000000..e4121d7
--- /dev/null
+++ b/apps/codecs/libatrac/atrac3.h
@@ -0,0 +1,80 @@
+#include "bitstream.h"
+#include "../librm/rm.h"
+
+/* These structures are needed to store the parsed gain control data. */
+typedef struct {
+ int num_gain_data;
+ int levcode[8];
+ int loccode[8];
+} gain_info;
+
+typedef struct {
+ gain_info gBlock[4];
+} gain_block;
+
+typedef struct {
+ int pos;
+ int numCoefs;
+ int32_t coef[8];
+} tonal_component;
+
+typedef struct {
+ int bandsCoded;
+ int numComponents;
+ tonal_component components[64];
+ int32_t prevFrame[1024];
+ int gcBlkSwitch;
+ gain_block gainBlock[2];
+
+ int32_t spectrum[1024] __attribute__((aligned(16)));
+ int32_t IMDCT_buf[1024] __attribute__((aligned(16)));
+
+ int32_t delayBuf1[46]; ///<qmf delay buffers
+ int32_t delayBuf2[46];
+ int32_t delayBuf3[46];
+} channel_unit;
+
+typedef struct {
+ GetBitContext gb;
+ //@{
+ /** stream data */
+ int channels;
+ int codingMode;
+ int bit_rate;
+ int sample_rate;
+ int samples_per_channel;
+ int samples_per_frame;
+
+ int bits_per_frame;
+ int bytes_per_frame;
+ int pBs;
+ channel_unit* pUnits;
+ //@}
+ //@{
+ /** joint-stereo related variables */
+ int matrix_coeff_index_prev[4];
+ int matrix_coeff_index_now[4];
+ int matrix_coeff_index_next[4];
+ int weighting_delay[6];
+ //@}
+ //@{
+ /** data buffers */
+ int32_t outSamples[2048];
+ uint8_t decoded_bytes_buffer[1024];
+ int32_t tempBuf[1070];
+ //@}
+ //@{
+ /** extradata */
+ int atrac3version;
+ int delay;
+ int scrambled_stream;
+ int frame_factor;
+ //@}
+} ATRAC3Context;
+
+int atrac3_decode_init(ATRAC3Context *q, RMContext *rmctx);
+
+int atrac3_decode_frame(RMContext *rmctx, ATRAC3Context *q,
+ void *data, int *data_size,
+ const uint8_t *buf, int buf_size);
+
diff --git a/apps/codecs/libatrac/main.c b/apps/codecs/libatrac/main.c
new file mode 100644
index 0000000..826dacf
--- /dev/null
+++ b/apps/codecs/libatrac/main.c
@@ -0,0 +1,167 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "atrac3.h"
+#include "../librm/rm.h"
+
+static unsigned char wav_header[44]={
+ 'R','I','F','F',// 0 - ChunkID
+ 0,0,0,0, // 4 - ChunkSize (filesize-8)
+ 'W','A','V','E',// 8 - Format
+ 'f','m','t',' ',// 12 - SubChunkID
+ 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
+ 1,0, // 20 - AudioFormat (1=Uncompressed)
+ 2,0, // 22 - NumChannels
+ 0,0,0,0, // 24 - SampleRate in Hz
+ 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
+ 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
+ 16,0, // 34 - BitsPerSample
+ 'd','a','t','a',// 36 - Subchunk2ID
+ 0,0,0,0 // 40 - Subchunk2Size
+};
+
+int open_wav(char* filename) {
+ int fd,res;
+
+ fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR);
+ if (fd >= 0) {
+ res = write(fd,wav_header,sizeof(wav_header));
+ }
+
+ return(fd);
+}
+
+void close_wav(int fd, RMContext *rmctx, ATRAC3Context *q) {
+ int x,res;
+ int filesize;
+ int bytes_per_sample = 2;
+ int samples_per_frame = q->samples_per_frame;
+ int nb_channels = rmctx->nb_channels;
+ int sample_rate = rmctx->sample_rate;
+ int nb_frames = rmctx->audio_framesize/rmctx->block_align * rmctx->nb_packets - 2; // first 2 frames have no valid audio; skipped in output
+
+ filesize= samples_per_frame*bytes_per_sample*nb_frames +44;
+ printf("Filesize = %d\n",filesize);
+
+ // ChunkSize
+ x=filesize-8;
+ wav_header[4]=(x&0xff);
+ wav_header[5]=(x&0xff00)>>8;
+ wav_header[6]=(x&0xff0000)>>16;
+ wav_header[7]=(x&0xff000000)>>24;
+
+ // Number of channels
+ wav_header[22]=nb_channels;
+
+ // Samplerate
+ wav_header[24]=sample_rate&0xff;
+ wav_header[25]=(sample_rate&0xff00)>>8;
+ wav_header[26]=(sample_rate&0xff0000)>>16;
+ wav_header[27]=(sample_rate&0xff000000)>>24;
+
+ // ByteRate
+ x=sample_rate*bytes_per_sample*nb_channels;
+ wav_header[28]=(x&0xff);
+ wav_header[29]=(x&0xff00)>>8;
+ wav_header[30]=(x&0xff0000)>>16;
+ wav_header[31]=(x&0xff000000)>>24;
+
+ // BlockAlign
+ wav_header[32]=rmctx->block_align;//2*rmctx->nb_channels;
+
+ // Bits per sample
+ wav_header[34]=16;
+
+ // Subchunk2Size
+ x=filesize-44;
+ wav_header[40]=(x&0xff);
+ wav_header[41]=(x&0xff00)>>8;
+ wav_header[42]=(x&0xff0000)>>16;
+ wav_header[43]=(x&0xff000000)>>24;
+
+ lseek(fd,0,SEEK_SET);
+ res = write(fd,wav_header,sizeof(wav_header));
+ close(fd);
+}
+
+int main(int argc, char *argv[])
+{
+ int fd, fd_dec;
+ int res, i, datasize = 0;
+
+#ifdef DUMP_RAW_FRAMES
+ char filename[15];
+ int fd_out;
+#endif
+ int16_t outbuf[2048];
+ uint16_t fs,sps,h;
+ uint32_t packet_count;
+ ATRAC3Context q;
+ RMContext rmctx;
+ RMPacket pkt;
+
+ memset(&q,0,sizeof(ATRAC3Context));
+ memset(&rmctx,0,sizeof(RMContext));
+ memset(&pkt,0,sizeof(RMPacket));
+
+ if (argc != 2) {
+ DEBUGF("Incorrect number of arguments\n");
+ return -1;
+ }
+
+ fd = open(argv[1],O_RDONLY);
+ if (fd < 0) {
+ DEBUGF("Error opening file %s\n", argv[1]);
+ return -1;
+ }
+
+ /* copy the input rm file to a memory buffer */
+ uint8_t * filebuf = (uint8_t *)calloc((int)filesize(fd),sizeof(uint8_t));
+ res = read(fd,filebuf,filesize(fd));
+
+ fd_dec = open_wav("output.wav");
+ if (fd_dec < 0) {
+ DEBUGF("Error creating output file\n");
+ return -1;
+ }
+ res = real_parse_header(fd, &rmctx);
+ packet_count = rmctx.nb_packets;
+ rmctx.audio_framesize = rmctx.block_align;
+ rmctx.block_align = rmctx.sub_packet_size;
+ fs = rmctx.audio_framesize;
+ sps= rmctx.block_align;
+ h = rmctx.sub_packet_h;
+ atrac3_decode_init(&q,&rmctx);
+
+ /* change the buffer pointer to point at the first audio frame */
+ advance_buffer(&filebuf, rmctx.data_offset + DATA_HEADER_SIZE);
+ while(packet_count)
+ {
+ rm_get_packet(&filebuf, &rmctx, &pkt);
+ for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
+ {
+ /* output raw audio frames that are sent to the decoder into separate files */
+#ifdef DUMP_RAW_FRAMES
+ snprintf(filename,sizeof(filename),"dump%d.raw",++x);
+ fd_out = open(filename,O_WRONLY|O_CREAT|O_APPEND);
+ write(fd_out,pkt.frames[i],sps);
+ close(fd_out);
+#endif
+ if(pkt.length > 0)
+ res = atrac3_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i] , rmctx.block_align);
+ rmctx.frame_number++;
+ res = write(fd_dec,outbuf,datasize);
+ }
+ packet_count -= rmctx.audio_pkt_cnt;
+ rmctx.audio_pkt_cnt = 0;
+ }
+ close_wav(fd_dec, &rmctx, &q);
+ close(fd);
+
+ return 0;
+}