Mercurial > mplayer.hg
annotate libmpdemux/demux_realaud.c @ 21555:ababf115f90d
Avoid deprecated _sleep, instead include windows.h and use Sleep just like
osdep/timer-win2.c does.
Patch by Zuxy Meng [zuxy meng (at) gmail com]
author | reimar |
---|---|
date | Sun, 10 Dec 2006 14:04:27 +0000 |
parents | d7b2fa4c39da |
children | fed73db9ddb1 |
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 | |
18 | |
19 #define FOURCC_DOTRA mmioFOURCC('.','r','a', 0xfd) | |
20 #define FOURCC_144 mmioFOURCC('1','4','_','4') | |
21 #define FOURCC_288 mmioFOURCC('2','8','_','8') | |
22 #define FOURCC_DNET mmioFOURCC('d','n','e','t') | |
23 #define FOURCC_LPCJ mmioFOURCC('l','p','c','J') | |
17400 | 24 #define FOURCC_SIPR mmioFOURCC('s','i','p','r') |
25 | |
26 | |
27 static unsigned char sipr_swaps[38][2]={ | |
28 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68}, | |
29 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46}, | |
30 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56}, | |
31 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83}, | |
32 {77,80} }; | |
33 | |
34 // Map flavour to bytes per second | |
35 static int sipr_fl2bps[4] = {813, 1062, 625, 2000}; // 6.5, 8.5, 5, 16 kbit per second | |
10034 | 36 |
37 | |
38 typedef struct { | |
39 unsigned short version; | |
40 unsigned int dotranum; | |
41 unsigned int data_size; | |
42 unsigned short version2; | |
43 unsigned int hdr_size; | |
44 unsigned short codec_flavor; | |
45 unsigned int coded_framesize; | |
46 unsigned short sub_packet_h; | |
47 unsigned short frame_size; | |
48 unsigned short sub_packet_size; | |
49 char genr[4]; | |
17400 | 50 unsigned char *audio_buf; |
10034 | 51 } ra_priv_t; |
52 | |
53 | |
54 | |
16175 | 55 static int ra_check_file(demuxer_t* demuxer) |
10034 | 56 { |
57 unsigned int chunk_id; | |
58 | |
59 chunk_id = stream_read_dword_le(demuxer->stream); | |
60 if (chunk_id == FOURCC_DOTRA) | |
16175 | 61 return DEMUXER_TYPE_REALAUDIO; |
10034 | 62 else |
63 return 0; | |
64 } | |
65 | |
66 | |
67 | |
68 // return value: | |
69 // 0 = EOF or no stream found | |
70 // 1 = successfully read a packet | |
16175 | 71 static int demux_ra_fill_buffer(demuxer_t *demuxer, demux_stream_t *dsds) |
10034 | 72 { |
73 ra_priv_t *ra_priv = demuxer->priv; | |
74 int len; | |
75 demux_stream_t *ds = demuxer->audio; | |
76 sh_audio_t *sh = ds->sh; | |
77 WAVEFORMATEX *wf = sh->wf; | |
78 demux_packet_t *dp; | |
17149
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
79 int x, y; |
10034 | 80 |
81 if (demuxer->stream->eof) | |
82 return 0; | |
83 | |
84 len = wf->nBlockAlign; | |
85 demuxer->filepos = stream_tell(demuxer->stream); | |
86 | |
17400 | 87 if ((sh->format == FOURCC_288) || (sh->format == FOURCC_SIPR)) { |
88 if (sh->format == FOURCC_SIPR) { | |
89 int n; | |
90 int bs = ra_priv->sub_packet_h * ra_priv->frame_size * 2 / 96; // nibbles per subpacket | |
91 stream_read(demuxer->stream, ra_priv->audio_buf, ra_priv->sub_packet_h * ra_priv->frame_size); | |
92 // Perform reordering | |
93 for(n = 0; n < 38; n++) { | |
94 int j; | |
95 int i = bs * sipr_swaps[n][0]; | |
96 int o = bs * sipr_swaps[n][1]; | |
97 // swap nibbles of block 'i' with 'o' TODO: optimize | |
98 for(j = 0; j < bs; j++) { | |
99 int x = (i & 1) ? (ra_priv->audio_buf[i >> 1] >> 4) : (ra_priv->audio_buf[i >> 1] & 0x0F); | |
100 int y = (o & 1) ? (ra_priv->audio_buf[o >> 1] >> 4) : (ra_priv->audio_buf[o >> 1] & 0x0F); | |
101 if(o & 1) | |
102 ra_priv->audio_buf[o >> 1] = (ra_priv->audio_buf[o >> 1] & 0x0F) | (x << 4); | |
103 else | |
104 ra_priv->audio_buf[o >> 1] = (ra_priv->audio_buf[o >> 1] & 0xF0) | x; | |
105 if(i & 1) | |
106 ra_priv->audio_buf[i >> 1] = (ra_priv->audio_buf[i >> 1] & 0x0F) | (y << 4); | |
107 else | |
108 ra_priv->audio_buf[i >> 1] = (ra_priv->audio_buf[i >> 1] & 0xF0) | y; | |
109 ++i; ++o; | |
110 } | |
111 } | |
112 } else { | |
17149
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
113 for (y = 0; y < ra_priv->sub_packet_h; y++) |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
114 for (x = 0; x < ra_priv->sub_packet_h / 2; x++) |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
115 stream_read(demuxer->stream, ra_priv->audio_buf + x * 2 *ra_priv->frame_size + |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
116 y * ra_priv->coded_framesize, ra_priv->coded_framesize); |
17400 | 117 } |
17149
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
118 // Release all the audio packets |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
119 for (x = 0; x < ra_priv->sub_packet_h * ra_priv->frame_size / len; x++) { |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
120 dp = new_demux_packet(len); |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
121 memcpy(dp->buffer, ra_priv->audio_buf + x * len, len); |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
122 dp->pts = x ? 0 : demuxer->filepos / ra_priv->data_size; |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
123 dp->pos = demuxer->filepos; // all equal |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
124 dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
125 ds_add_packet(ds, dp); |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
126 } |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
127 } else { |
10034 | 128 dp = new_demux_packet(len); |
129 stream_read(demuxer->stream, dp->buffer, len); | |
130 | |
10035 | 131 dp->pts = demuxer->filepos / ra_priv->data_size; |
10034 | 132 dp->pos = demuxer->filepos; |
133 dp->flags = 0; | |
134 ds_add_packet(ds, dp); | |
17149
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
135 } |
10034 | 136 |
137 return 1; | |
138 } | |
139 | |
140 | |
141 | |
17977
f70772d02eaa
Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents:
17636
diff
changeset
|
142 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level); |
10034 | 143 |
144 | |
145 | |
16175 | 146 static demuxer_t* demux_open_ra(demuxer_t* demuxer) |
10034 | 147 { |
148 ra_priv_t* ra_priv = demuxer->priv; | |
149 sh_audio_t *sh; | |
150 int i; | |
151 char *buf; | |
152 | |
19062
83c3afeab35d
drops casts from void * on malloc/calloc from libmpdemux code
reynaldo
parents:
18958
diff
changeset
|
153 if ((ra_priv = malloc(sizeof(ra_priv_t))) == NULL) { |
10034 | 154 mp_msg(MSGT_DEMUX, MSGL_ERR, "[RealAudio] Can't allocate memory for private data.\n"); |
10232 | 155 return 0; |
10034 | 156 } |
157 memset(ra_priv, 0, sizeof(ra_priv_t)); | |
158 | |
159 demuxer->priv = ra_priv; | |
160 sh = new_sh_audio(demuxer, 0); | |
161 demuxer->audio->id = 0; | |
162 sh->ds=demuxer->audio; | |
163 demuxer->audio->sh = sh; | |
164 | |
165 ra_priv->version = stream_read_word(demuxer->stream); | |
166 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] File version: %d\n", ra_priv->version); | |
167 if ((ra_priv->version < 3) || (ra_priv->version > 4)) { | |
168 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] ra version %d is not supported yet, please " | |
169 "contact MPlayer developers\n", ra_priv->version); | |
10232 | 170 return 0; |
10034 | 171 } |
172 if (ra_priv->version == 3) { | |
173 ra_priv->hdr_size = stream_read_word(demuxer->stream); | |
174 stream_skip(demuxer->stream, 10); | |
175 ra_priv->data_size = stream_read_dword(demuxer->stream); | |
176 } else { | |
177 stream_skip(demuxer->stream, 2); | |
178 ra_priv->dotranum = stream_read_dword(demuxer->stream); | |
179 ra_priv->data_size = stream_read_dword(demuxer->stream); | |
180 ra_priv->version2 = stream_read_word(demuxer->stream); | |
181 ra_priv->hdr_size = stream_read_dword(demuxer->stream); | |
182 ra_priv->codec_flavor = stream_read_word(demuxer->stream); | |
17400 | 183 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Flavor: %d\n", ra_priv->codec_flavor); |
10034 | 184 ra_priv->coded_framesize = stream_read_dword(demuxer->stream); |
17400 | 185 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Coded frame size: %d\n", ra_priv->coded_framesize); |
10034 | 186 stream_skip(demuxer->stream, 4); // data size? |
187 stream_skip(demuxer->stream, 8); | |
188 ra_priv->sub_packet_h = stream_read_word(demuxer->stream); | |
17400 | 189 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Sub packet h: %d\n", ra_priv->sub_packet_h); |
10034 | 190 ra_priv->frame_size = stream_read_word(demuxer->stream); |
191 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Frame size: %d\n", ra_priv->frame_size); | |
192 ra_priv->sub_packet_size = stream_read_word(demuxer->stream); | |
193 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Sub packet size: %d\n", ra_priv->sub_packet_size); | |
194 stream_skip(demuxer->stream, 2); | |
195 sh->samplerate = stream_read_word(demuxer->stream); | |
196 stream_skip(demuxer->stream, 2); | |
197 sh->samplesize = stream_read_word(demuxer->stream); | |
198 sh->channels = stream_read_word(demuxer->stream); | |
199 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] %d channel, %d bit, %dHz\n", sh->channels, | |
200 sh->samplesize, sh->samplerate); | |
201 i = stream_read_char(demuxer->stream); | |
16322
9cb2b9b6a9b7
Sparcs do not like wild pointer typecasting (unaligned access).
reimar
parents:
16175
diff
changeset
|
202 ra_priv->genr[0] = stream_read_char(demuxer->stream); |
9cb2b9b6a9b7
Sparcs do not like wild pointer typecasting (unaligned access).
reimar
parents:
16175
diff
changeset
|
203 ra_priv->genr[1] = stream_read_char(demuxer->stream); |
9cb2b9b6a9b7
Sparcs do not like wild pointer typecasting (unaligned access).
reimar
parents:
16175
diff
changeset
|
204 ra_priv->genr[2] = stream_read_char(demuxer->stream); |
9cb2b9b6a9b7
Sparcs do not like wild pointer typecasting (unaligned access).
reimar
parents:
16175
diff
changeset
|
205 ra_priv->genr[3] = stream_read_char(demuxer->stream); |
10034 | 206 if (i != 4) { |
207 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] Genr size is not 4 (%d), please report to " | |
208 "MPlayer developers\n", i); | |
209 stream_skip(demuxer->stream, i - 4); | |
210 } | |
211 i = stream_read_char(demuxer->stream); | |
212 sh->format = stream_read_dword_le(demuxer->stream); | |
213 if (i != 4) { | |
214 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] FourCC size is not 4 (%d), please report to " | |
215 "MPlayer developers\n", i); | |
216 stream_skip(demuxer->stream, i - 4); | |
217 } | |
218 stream_skip(demuxer->stream, 3); | |
219 } | |
220 | |
221 if ((i = stream_read_char(demuxer->stream)) != 0) { | |
222 buf = malloc(i+1); | |
223 stream_read(demuxer->stream, buf, i); | |
224 buf[i] = 0; | |
225 demux_info_add(demuxer, "Title", buf); | |
226 free(buf); | |
227 } | |
228 if ((i = stream_read_char(demuxer->stream)) != 0) { | |
229 buf = malloc(i+1); | |
230 stream_read(demuxer->stream, buf, i); | |
231 buf[i] = 0; | |
232 demux_info_add(demuxer, "Author", buf); | |
233 free(buf); | |
234 } | |
235 if ((i = stream_read_char(demuxer->stream)) != 0) { | |
236 buf = malloc(i+1); | |
237 stream_read(demuxer->stream, buf, i); | |
238 buf[i] = 0; | |
239 demux_info_add(demuxer, "Copyright", buf); | |
240 free(buf); | |
241 } | |
242 | |
14908 | 243 if ((i = stream_read_char(demuxer->stream)) != 0) { |
244 buf = malloc(i+1); | |
245 stream_read(demuxer->stream, buf, i); | |
246 buf[i] = 0; | |
247 demux_info_add(demuxer, "Comment", buf); | |
248 free(buf); | |
249 } | |
10034 | 250 |
251 if (ra_priv->version == 3) { | |
14909 | 252 if(ra_priv->hdr_size + 8 > stream_tell(demuxer->stream)) { |
10034 | 253 stream_skip(demuxer->stream, 1); |
254 i = stream_read_char(demuxer->stream); | |
255 sh->format = stream_read_dword_le(demuxer->stream); | |
256 if (i != 4) { | |
257 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] FourCC size is not 4 (%d), please report to " | |
258 "MPlayer developers\n", i); | |
259 stream_skip(demuxer->stream, i - 4); | |
260 } | |
261 | |
262 if (sh->format != FOURCC_LPCJ) { | |
263 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] Version 3 with FourCC %8x, please report to " | |
264 "MPlayer developers\n", sh->format); | |
265 } | |
14909 | 266 } else |
267 // If a stream does not have fourcc, let's assume it's 14.4 | |
268 sh->format = FOURCC_LPCJ; | |
10034 | 269 |
270 sh->channels = 1; | |
271 sh->samplesize = 16; | |
272 sh->samplerate = 8000; | |
273 ra_priv->frame_size = 240; | |
274 sh->format = FOURCC_144; | |
275 } | |
276 | |
277 /* Fill WAVEFORMATEX */ | |
278 sh->wf = malloc(sizeof(WAVEFORMATEX)); | |
279 memset(sh->wf, 0, sizeof(WAVEFORMATEX)); | |
280 sh->wf->nChannels = sh->channels; | |
281 sh->wf->wBitsPerSample = sh->samplesize; | |
282 sh->wf->nSamplesPerSec = sh->samplerate; | |
283 sh->wf->nAvgBytesPerSec = sh->samplerate*sh->samplesize/8; | |
284 sh->wf->nBlockAlign = ra_priv->frame_size; | |
285 sh->wf->cbSize = 0; | |
286 sh->wf->wFormatTag = sh->format; | |
287 | |
288 switch (sh->format) { | |
289 case FOURCC_144: | |
290 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: 14_4\n"); | |
17149
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
291 sh->wf->nBlockAlign = 0x14; |
10034 | 292 break; |
293 case FOURCC_288: | |
294 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: 28_8\n"); | |
17149
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
295 sh->wf->nBlockAlign = ra_priv->coded_framesize; |
18505
01b9f29c2fb5
Fix some potential integer overflow in memory allocation (mot of these
rtognimp
parents:
17977
diff
changeset
|
296 ra_priv->audio_buf = calloc(ra_priv->sub_packet_h, ra_priv->frame_size); |
10034 | 297 break; |
298 case FOURCC_DNET: | |
299 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: DNET -> AC3\n"); | |
300 break; | |
17400 | 301 case FOURCC_SIPR: |
302 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: SIPR\n"); | |
303 sh->wf->nBlockAlign = ra_priv->coded_framesize; | |
304 sh->wf->nAvgBytesPerSec = sipr_fl2bps[ra_priv->codec_flavor]; | |
18505
01b9f29c2fb5
Fix some potential integer overflow in memory allocation (mot of these
rtognimp
parents:
17977
diff
changeset
|
305 ra_priv->audio_buf = calloc(ra_priv->sub_packet_h, ra_priv->frame_size); |
17400 | 306 break; |
10034 | 307 default: |
308 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: Unknown (%d)\n", sh->format); | |
309 } | |
310 | |
17977
f70772d02eaa
Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents:
17636
diff
changeset
|
311 print_wave_header(sh->wf, MSGL_V); |
10034 | 312 |
313 /* disable seeking */ | |
314 demuxer->seekable = 0; | |
315 | |
316 if(!ds_fill_buffer(demuxer->audio)) | |
317 mp_msg(MSGT_DEMUXER,MSGL_INFO,"[RealAudio] No data.\n"); | |
10232 | 318 |
16175 | 319 return demuxer; |
10034 | 320 } |
321 | |
322 | |
323 | |
16175 | 324 static void demux_close_ra(demuxer_t *demuxer) |
10034 | 325 { |
326 ra_priv_t* ra_priv = demuxer->priv; | |
327 | |
17149
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
328 if (ra_priv) { |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
329 if (ra_priv->audio_buf) |
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
330 free (ra_priv->audio_buf); |
10034 | 331 free(ra_priv); |
17149
9a0a376a54b1
Move audio packets reordering from codec interface to demuxers for real
rtognimp
parents:
16322
diff
changeset
|
332 } |
10034 | 333 return; |
334 } | |
335 | |
336 | |
337 #if 0 | |
338 /* please upload RV10 samples WITH INDEX CHUNK */ | |
17636 | 339 int demux_seek_ra(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags) |
10034 | 340 { |
341 real_priv_t *priv = demuxer->priv; | |
342 demux_stream_t *d_audio = demuxer->audio; | |
343 sh_audio_t *sh_audio = d_audio->sh; | |
344 int aid = d_audio->id; | |
345 int next_offset = 0; | |
346 int rel_seek_frames = 0; | |
347 int streams = 0; | |
348 | |
349 return stream_seek(demuxer->stream, next_offset); | |
350 } | |
351 #endif | |
16175 | 352 |
353 | |
354 demuxer_desc_t demuxer_desc_realaudio = { | |
355 "Realaudio demuxer", | |
356 "realaudio", | |
357 "REALAUDIO", | |
358 "Roberto Togni", | |
359 "handles old audio only .ra files", | |
360 DEMUXER_TYPE_REALAUDIO, | |
361 1, // safe autodetect | |
362 ra_check_file, | |
363 demux_ra_fill_buffer, | |
364 demux_open_ra, | |
365 demux_close_ra, | |
366 NULL, | |
367 NULL | |
368 }; |