Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / codecs / libatrac / main.c
blob30307c2946f1c45bc82ad78ace891116abdfa3a5
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <inttypes.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <string.h>
9 #include "atrac3.h"
10 #include "../librm/rm.h"
12 ATRAC3Context q IBSS_ATTR;
14 static unsigned char wav_header[44]={
15 'R','I','F','F',// 0 - ChunkID
16 0,0,0,0, // 4 - ChunkSize (filesize-8)
17 'W','A','V','E',// 8 - Format
18 'f','m','t',' ',// 12 - SubChunkID
19 16,0,0,0, // 16 - SubChunk1ID // 16 for PCM
20 1,0, // 20 - AudioFormat (1=Uncompressed)
21 2,0, // 22 - NumChannels
22 0,0,0,0, // 24 - SampleRate in Hz
23 0,0,0,0, // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
24 4,0, // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
25 16,0, // 34 - BitsPerSample
26 'd','a','t','a',// 36 - Subchunk2ID
27 0,0,0,0 // 40 - Subchunk2Size
30 int open_wav(char* filename) {
31 int fd,res;
33 fd=open(filename,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR);
34 if (fd >= 0) {
35 res = write(fd,wav_header,sizeof(wav_header));
38 return(fd);
41 void close_wav(int fd, RMContext *rmctx, ATRAC3Context *q) {
42 int x,res;
43 int filesize;
44 int bytes_per_sample = 2;
45 int samples_per_frame = q->samples_per_frame;
46 int nb_channels = rmctx->nb_channels;
47 int sample_rate = rmctx->sample_rate;
48 int nb_frames = rmctx->audio_framesize/rmctx->block_align * rmctx->nb_packets - 2; // first 2 frames have no valid audio; skipped in output
50 filesize= samples_per_frame*bytes_per_sample*nb_frames +44;
51 printf("Filesize = %d\n",filesize);
53 // ChunkSize
54 x=filesize-8;
55 wav_header[4]=(x&0xff);
56 wav_header[5]=(x&0xff00)>>8;
57 wav_header[6]=(x&0xff0000)>>16;
58 wav_header[7]=(x&0xff000000)>>24;
60 // Number of channels
61 wav_header[22]=nb_channels;
63 // Samplerate
64 wav_header[24]=sample_rate&0xff;
65 wav_header[25]=(sample_rate&0xff00)>>8;
66 wav_header[26]=(sample_rate&0xff0000)>>16;
67 wav_header[27]=(sample_rate&0xff000000)>>24;
69 // ByteRate
70 x=sample_rate*bytes_per_sample*nb_channels;
71 wav_header[28]=(x&0xff);
72 wav_header[29]=(x&0xff00)>>8;
73 wav_header[30]=(x&0xff0000)>>16;
74 wav_header[31]=(x&0xff000000)>>24;
76 // BlockAlign
77 wav_header[32]=rmctx->block_align;//2*rmctx->nb_channels;
79 // Bits per sample
80 wav_header[34]=16;
82 // Subchunk2Size
83 x=filesize-44;
84 wav_header[40]=(x&0xff);
85 wav_header[41]=(x&0xff00)>>8;
86 wav_header[42]=(x&0xff0000)>>16;
87 wav_header[43]=(x&0xff000000)>>24;
89 lseek(fd,0,SEEK_SET);
90 res = write(fd,wav_header,sizeof(wav_header));
91 close(fd);
94 int main(int argc, char *argv[])
96 int fd, fd_dec;
97 int res, i, datasize = 0;
99 #ifdef DUMP_RAW_FRAMES
100 char filename[15];
101 int fd_out;
102 #endif
103 int16_t outbuf[2048];
104 uint16_t fs,sps,h;
105 uint32_t packet_count;
106 RMContext rmctx;
107 RMPacket pkt;
109 memset(&q,0,sizeof(ATRAC3Context));
110 memset(&rmctx,0,sizeof(RMContext));
111 memset(&pkt,0,sizeof(RMPacket));
113 if (argc != 2) {
114 DEBUGF("Incorrect number of arguments\n");
115 return -1;
118 fd = open(argv[1],O_RDONLY);
119 if (fd < 0) {
120 DEBUGF("Error opening file %s\n", argv[1]);
121 return -1;
124 /* copy the input rm file to a memory buffer */
125 uint8_t * filebuf = (uint8_t *)calloc((int)filesize(fd),sizeof(uint8_t));
126 res = read(fd,filebuf,filesize(fd));
128 fd_dec = open_wav("output.wav");
129 if (fd_dec < 0) {
130 DEBUGF("Error creating output file\n");
131 return -1;
133 res = real_parse_header(fd, &rmctx);
134 packet_count = rmctx.nb_packets;
135 rmctx.audio_framesize = rmctx.block_align;
136 rmctx.block_align = rmctx.sub_packet_size;
137 fs = rmctx.audio_framesize;
138 sps= rmctx.block_align;
139 h = rmctx.sub_packet_h;
140 atrac3_decode_init(&q,&rmctx);
142 /* change the buffer pointer to point at the first audio frame */
143 advance_buffer(&filebuf, rmctx.data_offset + DATA_HEADER_SIZE);
144 while(packet_count)
146 rm_get_packet(&filebuf, &rmctx, &pkt);
147 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
149 /* output raw audio frames that are sent to the decoder into separate files */
150 #ifdef DUMP_RAW_FRAMES
151 snprintf(filename,sizeof(filename),"dump%d.raw",++x);
152 fd_out = open(filename,O_WRONLY|O_CREAT|O_APPEND);
153 write(fd_out,pkt.frames[i],sps);
154 close(fd_out);
155 #endif
156 if(pkt.length > 0)
157 res = atrac3_decode_frame(&rmctx,&q, outbuf, &datasize, pkt.frames[i] , rmctx.block_align);
158 rmctx.frame_number++;
159 res = write(fd_dec,outbuf,datasize);
161 packet_count -= rmctx.audio_pkt_cnt;
162 rmctx.audio_pkt_cnt = 0;
164 close_wav(fd_dec, &rmctx, &q);
165 close(fd);
167 return 0;