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
) {
33 fd
=open(filename
,O_CREAT
|O_WRONLY
|O_TRUNC
,S_IRUSR
|S_IWUSR
);
35 res
= write(fd
,wav_header
,sizeof(wav_header
));
41 void close_wav(int fd
, RMContext
*rmctx
, ATRAC3Context
*q
) {
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
);
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;
61 wav_header
[22]=nb_channels
;
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;
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;
77 wav_header
[32]=rmctx
->block_align
;//2*rmctx->nb_channels;
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;
90 res
= write(fd
,wav_header
,sizeof(wav_header
));
94 int main(int argc
, char *argv
[])
97 int res
, i
, datasize
= 0;
99 #ifdef DUMP_RAW_FRAMES
103 int16_t outbuf
[2048];
105 uint32_t packet_count
;
109 memset(&q
,0,sizeof(ATRAC3Context
));
110 memset(&rmctx
,0,sizeof(RMContext
));
111 memset(&pkt
,0,sizeof(RMPacket
));
114 DEBUGF("Incorrect number of arguments\n");
118 fd
= open(argv
[1],O_RDONLY
);
120 DEBUGF("Error opening file %s\n", argv
[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");
130 DEBUGF("Error creating output file\n");
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
);
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
);
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
);