Mercurial > mplayer.hg
annotate libmpdemux/demux_realaud.c @ 17026:3c4b93bc0db3
10l
author | lorenm |
---|---|
date | Tue, 22 Nov 2005 05:32:28 +0000 |
parents | 9cb2b9b6a9b7 |
children | 9a0a376a54b1 |
rev | line source |
---|---|
10034 | 1 /* |
2 Realaudio demuxer for MPlayer | |
14908 | 3 (c) 2003, 2005 Roberto Togni |
10034 | 4 */ |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <unistd.h> | |
9 | |
10 #include "config.h" | |
11 #include "mp_msg.h" | |
12 #include "help_mp.h" | |
13 | |
14 #include "stream.h" | |
15 #include "demuxer.h" | |
16 #include "stheader.h" | |
17 #include "bswap.h" | |
18 | |
19 | |
20 #define FOURCC_DOTRA mmioFOURCC('.','r','a', 0xfd) | |
21 #define FOURCC_144 mmioFOURCC('1','4','_','4') | |
22 #define FOURCC_288 mmioFOURCC('2','8','_','8') | |
23 #define FOURCC_DNET mmioFOURCC('d','n','e','t') | |
24 #define FOURCC_LPCJ mmioFOURCC('l','p','c','J') | |
25 | |
26 | |
27 typedef struct { | |
28 unsigned short version; | |
29 unsigned int dotranum; | |
30 unsigned int data_size; | |
31 unsigned short version2; | |
32 unsigned int hdr_size; | |
33 unsigned short codec_flavor; | |
34 unsigned int coded_framesize; | |
35 unsigned short sub_packet_h; | |
36 unsigned short frame_size; | |
37 unsigned short sub_packet_size; | |
38 char genr[4]; | |
39 } ra_priv_t; | |
40 | |
41 | |
42 | |
16175 | 43 static int ra_check_file(demuxer_t* demuxer) |
10034 | 44 { |
45 unsigned int chunk_id; | |
46 | |
47 chunk_id = stream_read_dword_le(demuxer->stream); | |
48 if (chunk_id == FOURCC_DOTRA) | |
16175 | 49 return DEMUXER_TYPE_REALAUDIO; |
10034 | 50 else |
51 return 0; | |
52 } | |
53 | |
54 | |
55 | |
56 void hexdump(char *, unsigned long); | |
57 | |
58 // return value: | |
59 // 0 = EOF or no stream found | |
60 // 1 = successfully read a packet | |
16175 | 61 static int demux_ra_fill_buffer(demuxer_t *demuxer, demux_stream_t *dsds) |
10034 | 62 { |
63 ra_priv_t *ra_priv = demuxer->priv; | |
64 int len; | |
65 int timestamp; | |
66 int flags; | |
67 demux_stream_t *ds = demuxer->audio; | |
68 sh_audio_t *sh = ds->sh; | |
69 WAVEFORMATEX *wf = sh->wf; | |
70 demux_packet_t *dp; | |
71 | |
72 if (demuxer->stream->eof) | |
73 return 0; | |
74 | |
75 len = wf->nBlockAlign; | |
76 demuxer->filepos = stream_tell(demuxer->stream); | |
77 | |
78 dp = new_demux_packet(len); | |
79 stream_read(demuxer->stream, dp->buffer, len); | |
80 | |
10035 | 81 dp->pts = demuxer->filepos / ra_priv->data_size; |
10034 | 82 dp->pos = demuxer->filepos; |
83 dp->flags = 0; | |
84 ds_add_packet(ds, dp); | |
85 | |
86 return 1; | |
87 } | |
88 | |
89 | |
90 | |
91 extern void print_wave_header(WAVEFORMATEX *h); | |
92 | |
93 | |
94 | |
16175 | 95 static demuxer_t* demux_open_ra(demuxer_t* demuxer) |
10034 | 96 { |
97 ra_priv_t* ra_priv = demuxer->priv; | |
98 sh_audio_t *sh; | |
99 int i; | |
100 char *buf; | |
101 | |
102 if ((ra_priv = (ra_priv_t *)malloc(sizeof(ra_priv_t))) == NULL) { | |
103 mp_msg(MSGT_DEMUX, MSGL_ERR, "[RealAudio] Can't allocate memory for private data.\n"); | |
10232 | 104 return 0; |
10034 | 105 } |
106 memset(ra_priv, 0, sizeof(ra_priv_t)); | |
107 | |
108 demuxer->priv = ra_priv; | |
109 sh = new_sh_audio(demuxer, 0); | |
110 demuxer->audio->id = 0; | |
111 sh->ds=demuxer->audio; | |
112 demuxer->audio->sh = sh; | |
113 | |
114 ra_priv->version = stream_read_word(demuxer->stream); | |
115 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] File version: %d\n", ra_priv->version); | |
116 if ((ra_priv->version < 3) || (ra_priv->version > 4)) { | |
117 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] ra version %d is not supported yet, please " | |
118 "contact MPlayer developers\n", ra_priv->version); | |
10232 | 119 return 0; |
10034 | 120 } |
121 if (ra_priv->version == 3) { | |
122 ra_priv->hdr_size = stream_read_word(demuxer->stream); | |
123 stream_skip(demuxer->stream, 10); | |
124 ra_priv->data_size = stream_read_dword(demuxer->stream); | |
125 } else { | |
126 stream_skip(demuxer->stream, 2); | |
127 ra_priv->dotranum = stream_read_dword(demuxer->stream); | |
128 ra_priv->data_size = stream_read_dword(demuxer->stream); | |
129 ra_priv->version2 = stream_read_word(demuxer->stream); | |
130 ra_priv->hdr_size = stream_read_dword(demuxer->stream); | |
131 ra_priv->codec_flavor = stream_read_word(demuxer->stream); | |
132 ra_priv->coded_framesize = stream_read_dword(demuxer->stream); | |
133 stream_skip(demuxer->stream, 4); // data size? | |
134 stream_skip(demuxer->stream, 8); | |
135 ra_priv->sub_packet_h = stream_read_word(demuxer->stream); | |
136 ra_priv->frame_size = stream_read_word(demuxer->stream); | |
137 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Frame size: %d\n", ra_priv->frame_size); | |
138 ra_priv->sub_packet_size = stream_read_word(demuxer->stream); | |
139 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Sub packet size: %d\n", ra_priv->sub_packet_size); | |
140 stream_skip(demuxer->stream, 2); | |
141 sh->samplerate = stream_read_word(demuxer->stream); | |
142 stream_skip(demuxer->stream, 2); | |
143 sh->samplesize = stream_read_word(demuxer->stream); | |
144 sh->channels = stream_read_word(demuxer->stream); | |
145 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] %d channel, %d bit, %dHz\n", sh->channels, | |
146 sh->samplesize, sh->samplerate); | |
147 i = stream_read_char(demuxer->stream); | |
16322
9cb2b9b6a9b7
Sparcs do not like wild pointer typecasting (unaligned access).
reimar
parents:
16175
diff
changeset
|
148 ra_priv->genr[0] = stream_read_char(demuxer->stream); |
9cb2b9b6a9b7
Sparcs do not like wild pointer typecasting (unaligned access).
reimar
parents:
16175
diff
changeset
|
149 ra_priv->genr[1] = stream_read_char(demuxer->stream); |
9cb2b9b6a9b7
Sparcs do not like wild pointer typecasting (unaligned access).
reimar
parents:
16175
diff
changeset
|
150 ra_priv->genr[2] = stream_read_char(demuxer->stream); |
9cb2b9b6a9b7
Sparcs do not like wild pointer typecasting (unaligned access).
reimar
parents:
16175
diff
changeset
|
151 ra_priv->genr[3] = stream_read_char(demuxer->stream); |
10034 | 152 if (i != 4) { |
153 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] Genr size is not 4 (%d), please report to " | |
154 "MPlayer developers\n", i); | |
155 stream_skip(demuxer->stream, i - 4); | |
156 } | |
157 i = stream_read_char(demuxer->stream); | |
158 sh->format = stream_read_dword_le(demuxer->stream); | |
159 if (i != 4) { | |
160 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] FourCC size is not 4 (%d), please report to " | |
161 "MPlayer developers\n", i); | |
162 stream_skip(demuxer->stream, i - 4); | |
163 } | |
164 stream_skip(demuxer->stream, 3); | |
165 } | |
166 | |
167 if ((i = stream_read_char(demuxer->stream)) != 0) { | |
168 buf = malloc(i+1); | |
169 stream_read(demuxer->stream, buf, i); | |
170 buf[i] = 0; | |
171 demux_info_add(demuxer, "Title", buf); | |
172 free(buf); | |
173 } | |
174 if ((i = stream_read_char(demuxer->stream)) != 0) { | |
175 buf = malloc(i+1); | |
176 stream_read(demuxer->stream, buf, i); | |
177 buf[i] = 0; | |
178 demux_info_add(demuxer, "Author", buf); | |
179 free(buf); | |
180 } | |
181 if ((i = stream_read_char(demuxer->stream)) != 0) { | |
182 buf = malloc(i+1); | |
183 stream_read(demuxer->stream, buf, i); | |
184 buf[i] = 0; | |
185 demux_info_add(demuxer, "Copyright", buf); | |
186 free(buf); | |
187 } | |
188 | |
14908 | 189 if ((i = stream_read_char(demuxer->stream)) != 0) { |
190 buf = malloc(i+1); | |
191 stream_read(demuxer->stream, buf, i); | |
192 buf[i] = 0; | |
193 demux_info_add(demuxer, "Comment", buf); | |
194 free(buf); | |
195 } | |
10034 | 196 |
197 if (ra_priv->version == 3) { | |
14909 | 198 if(ra_priv->hdr_size + 8 > stream_tell(demuxer->stream)) { |
10034 | 199 stream_skip(demuxer->stream, 1); |
200 i = stream_read_char(demuxer->stream); | |
201 sh->format = stream_read_dword_le(demuxer->stream); | |
202 if (i != 4) { | |
203 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] FourCC size is not 4 (%d), please report to " | |
204 "MPlayer developers\n", i); | |
205 stream_skip(demuxer->stream, i - 4); | |
206 } | |
207 | |
208 if (sh->format != FOURCC_LPCJ) { | |
209 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] Version 3 with FourCC %8x, please report to " | |
210 "MPlayer developers\n", sh->format); | |
211 } | |
14909 | 212 } else |
213 // If a stream does not have fourcc, let's assume it's 14.4 | |
214 sh->format = FOURCC_LPCJ; | |
10034 | 215 |
216 sh->channels = 1; | |
217 sh->samplesize = 16; | |
218 sh->samplerate = 8000; | |
219 ra_priv->frame_size = 240; | |
220 sh->format = FOURCC_144; | |
221 } | |
222 | |
223 /* Fill WAVEFORMATEX */ | |
224 sh->wf = malloc(sizeof(WAVEFORMATEX)); | |
225 memset(sh->wf, 0, sizeof(WAVEFORMATEX)); | |
226 sh->wf->nChannels = sh->channels; | |
227 sh->wf->wBitsPerSample = sh->samplesize; | |
228 sh->wf->nSamplesPerSec = sh->samplerate; | |
229 sh->wf->nAvgBytesPerSec = sh->samplerate*sh->samplesize/8; | |
230 sh->wf->nBlockAlign = ra_priv->frame_size; | |
231 sh->wf->cbSize = 0; | |
232 sh->wf->wFormatTag = sh->format; | |
233 | |
234 switch (sh->format) { | |
235 case FOURCC_144: | |
236 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: 14_4\n"); | |
237 sh->wf->cbSize = 10/*+codecdata_length*/; | |
238 sh->wf = realloc(sh->wf, sizeof(WAVEFORMATEX)+sh->wf->cbSize); | |
239 ((short*)(sh->wf+1))[0]=0; | |
240 ((short*)(sh->wf+1))[1]=240; | |
241 ((short*)(sh->wf+1))[2]=0; | |
242 ((short*)(sh->wf+1))[3]=0x14; | |
243 ((short*)(sh->wf+1))[4]=0; | |
244 break; | |
245 case FOURCC_288: | |
246 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: 28_8\n"); | |
11544 | 247 sh->wf->cbSize = 10/*+codecdata_length*/; |
10034 | 248 sh->wf = realloc(sh->wf, sizeof(WAVEFORMATEX)+sh->wf->cbSize); |
249 ((short*)(sh->wf+1))[0]=0; | |
250 ((short*)(sh->wf+1))[1]=ra_priv->sub_packet_h; | |
251 ((short*)(sh->wf+1))[2]=ra_priv->codec_flavor; | |
252 ((short*)(sh->wf+1))[3]=ra_priv->coded_framesize; | |
253 ((short*)(sh->wf+1))[4]=0; | |
254 break; | |
255 case FOURCC_DNET: | |
256 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: DNET -> AC3\n"); | |
257 break; | |
258 default: | |
259 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: Unknown (%d)\n", sh->format); | |
260 } | |
261 | |
262 print_wave_header(sh->wf); | |
263 | |
264 /* disable seeking */ | |
265 demuxer->seekable = 0; | |
266 | |
267 if(!ds_fill_buffer(demuxer->audio)) | |
268 mp_msg(MSGT_DEMUXER,MSGL_INFO,"[RealAudio] No data.\n"); | |
10232 | 269 |
16175 | 270 return demuxer; |
10034 | 271 } |
272 | |
273 | |
274 | |
16175 | 275 static void demux_close_ra(demuxer_t *demuxer) |
10034 | 276 { |
277 ra_priv_t* ra_priv = demuxer->priv; | |
278 | |
279 if (ra_priv) | |
280 free(ra_priv); | |
281 | |
282 return; | |
283 } | |
284 | |
285 | |
286 #if 0 | |
287 /* please upload RV10 samples WITH INDEX CHUNK */ | |
288 int demux_seek_ra(demuxer_t *demuxer, float rel_seek_secs, int flags) | |
289 { | |
290 real_priv_t *priv = demuxer->priv; | |
291 demux_stream_t *d_audio = demuxer->audio; | |
292 sh_audio_t *sh_audio = d_audio->sh; | |
293 int aid = d_audio->id; | |
294 int next_offset = 0; | |
295 int rel_seek_frames = 0; | |
296 int streams = 0; | |
297 | |
298 return stream_seek(demuxer->stream, next_offset); | |
299 } | |
300 #endif | |
16175 | 301 |
302 | |
303 demuxer_desc_t demuxer_desc_realaudio = { | |
304 "Realaudio demuxer", | |
305 "realaudio", | |
306 "REALAUDIO", | |
307 "Roberto Togni", | |
308 "handles old audio only .ra files", | |
309 DEMUXER_TYPE_REALAUDIO, | |
310 1, // safe autodetect | |
311 ra_check_file, | |
312 demux_ra_fill_buffer, | |
313 demux_open_ra, | |
314 demux_close_ra, | |
315 NULL, | |
316 NULL | |
317 }; |