Get rid of completely pointless vt_doit variable
[mplayer/glamo.git] / libmpdemux / demux_realaud.c
blobfab567239237047c69bcfe5f74754fe615b39a5b
1 /*
2 * Realaudio demuxer
3 * copyright (c) 2003, 2005 Roberto Togni
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 #include "config.h"
27 #include "mp_msg.h"
28 #include "help_mp.h"
30 #include "stream/stream.h"
31 #include "demuxer.h"
32 #include "stheader.h"
35 #define FOURCC_DOTRA mmioFOURCC('.','r','a', 0xfd)
36 #define FOURCC_144 mmioFOURCC('1','4','_','4')
37 #define FOURCC_288 mmioFOURCC('2','8','_','8')
38 #define FOURCC_DNET mmioFOURCC('d','n','e','t')
39 #define FOURCC_LPCJ mmioFOURCC('l','p','c','J')
40 #define FOURCC_SIPR mmioFOURCC('s','i','p','r')
41 #define INTLID_INT4 mmioFOURCC('I','n','t','4')
42 #define INTLID_SIPR mmioFOURCC('s','i','p','r')
45 static unsigned char sipr_swaps[38][2]={
46 {0,63},{1,22},{2,44},{3,90},{5,81},{7,31},{8,86},{9,58},{10,36},{12,68},
47 {13,39},{14,73},{15,53},{16,69},{17,57},{19,88},{20,34},{21,71},{24,46},
48 {25,94},{26,54},{28,75},{29,50},{32,70},{33,92},{35,74},{38,85},{40,56},
49 {42,87},{43,65},{45,59},{48,79},{49,93},{51,89},{55,95},{61,76},{67,83},
50 {77,80} };
52 // Map flavour to bytes per second
53 static int sipr_fl2bps[4] = {813, 1062, 625, 2000}; // 6.5, 8.5, 5, 16 kbit per second
56 typedef struct {
57 unsigned short version;
58 unsigned int dotranum;
59 unsigned int data_size;
60 unsigned short version2;
61 unsigned int hdr_size;
62 unsigned short codec_flavor;
63 unsigned int coded_framesize;
64 unsigned short sub_packet_h;
65 unsigned short frame_size;
66 unsigned short sub_packet_size;
67 unsigned intl_id;
68 unsigned char *audio_buf;
69 } ra_priv_t;
73 static int ra_check_file(demuxer_t* demuxer)
75 unsigned int chunk_id;
77 chunk_id = stream_read_dword_le(demuxer->stream);
78 if (chunk_id == FOURCC_DOTRA)
79 return DEMUXER_TYPE_REALAUDIO;
80 else
81 return 0;
86 // return value:
87 // 0 = EOF or no stream found
88 // 1 = successfully read a packet
89 static int demux_ra_fill_buffer(demuxer_t *demuxer, demux_stream_t *dsds)
91 ra_priv_t *ra_priv = demuxer->priv;
92 int len;
93 demux_stream_t *ds = demuxer->audio;
94 sh_audio_t *sh = ds->sh;
95 WAVEFORMATEX *wf = sh->wf;
96 demux_packet_t *dp;
97 int x, y;
99 if (demuxer->stream->eof)
100 return 0;
102 len = wf->nBlockAlign;
103 demuxer->filepos = stream_tell(demuxer->stream);
105 if ((ra_priv->intl_id == INTLID_INT4) || (ra_priv->intl_id == INTLID_SIPR)) {
106 if (ra_priv->intl_id == INTLID_SIPR) {
107 int n;
108 int bs = ra_priv->sub_packet_h * ra_priv->frame_size * 2 / 96; // nibbles per subpacket
109 stream_read(demuxer->stream, ra_priv->audio_buf, ra_priv->sub_packet_h * ra_priv->frame_size);
110 // Perform reordering
111 for(n = 0; n < 38; n++) {
112 int j;
113 int i = bs * sipr_swaps[n][0];
114 int o = bs * sipr_swaps[n][1];
115 // swap nibbles of block 'i' with 'o' TODO: optimize
116 for(j = 0; j < bs; j++) {
117 int x = (i & 1) ? (ra_priv->audio_buf[i >> 1] >> 4) : (ra_priv->audio_buf[i >> 1] & 0x0F);
118 int y = (o & 1) ? (ra_priv->audio_buf[o >> 1] >> 4) : (ra_priv->audio_buf[o >> 1] & 0x0F);
119 if(o & 1)
120 ra_priv->audio_buf[o >> 1] = (ra_priv->audio_buf[o >> 1] & 0x0F) | (x << 4);
121 else
122 ra_priv->audio_buf[o >> 1] = (ra_priv->audio_buf[o >> 1] & 0xF0) | x;
123 if(i & 1)
124 ra_priv->audio_buf[i >> 1] = (ra_priv->audio_buf[i >> 1] & 0x0F) | (y << 4);
125 else
126 ra_priv->audio_buf[i >> 1] = (ra_priv->audio_buf[i >> 1] & 0xF0) | y;
127 ++i; ++o;
130 } else {
131 for (y = 0; y < ra_priv->sub_packet_h; y++)
132 for (x = 0; x < ra_priv->sub_packet_h / 2; x++)
133 stream_read(demuxer->stream, ra_priv->audio_buf + x * 2 *ra_priv->frame_size +
134 y * ra_priv->coded_framesize, ra_priv->coded_framesize);
136 // Release all the audio packets
137 for (x = 0; x < ra_priv->sub_packet_h * ra_priv->frame_size / len; x++) {
138 dp = new_demux_packet(len);
139 memcpy(dp->buffer, ra_priv->audio_buf + x * len, len);
140 dp->pts = x ? 0 : demuxer->filepos / ra_priv->data_size;
141 dp->pos = demuxer->filepos; // all equal
142 dp->flags = x ? 0 : 0x10; // Mark first packet as keyframe
143 ds_add_packet(ds, dp);
145 } else {
146 dp = new_demux_packet(len);
147 stream_read(demuxer->stream, dp->buffer, len);
149 dp->pts = demuxer->filepos / ra_priv->data_size;
150 dp->pos = demuxer->filepos;
151 dp->flags = 0;
152 ds_add_packet(ds, dp);
155 return 1;
160 void print_wave_header(WAVEFORMATEX *h, int verbose_level);
164 static demuxer_t* demux_open_ra(demuxer_t* demuxer)
166 ra_priv_t* ra_priv = demuxer->priv;
167 sh_audio_t *sh;
168 int i;
169 char *buf;
171 if ((ra_priv = malloc(sizeof(ra_priv_t))) == NULL) {
172 mp_msg(MSGT_DEMUX, MSGL_ERR, "[RealAudio] Can't allocate memory for private data.\n");
173 return 0;
175 memset(ra_priv, 0, sizeof(ra_priv_t));
177 demuxer->priv = ra_priv;
178 sh = new_sh_audio(demuxer, 0);
179 demuxer->audio->id = 0;
180 sh->ds=demuxer->audio;
181 demuxer->audio->sh = sh;
183 ra_priv->version = stream_read_word(demuxer->stream);
184 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] File version: %d\n", ra_priv->version);
185 if ((ra_priv->version < 3) || (ra_priv->version > 4)) {
186 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] ra version %d is not supported yet, please "
187 "contact MPlayer developers\n", ra_priv->version);
188 return 0;
190 if (ra_priv->version == 3) {
191 ra_priv->hdr_size = stream_read_word(demuxer->stream);
192 stream_skip(demuxer->stream, 10);
193 ra_priv->data_size = stream_read_dword(demuxer->stream);
194 } else {
195 stream_skip(demuxer->stream, 2);
196 ra_priv->dotranum = stream_read_dword(demuxer->stream);
197 ra_priv->data_size = stream_read_dword(demuxer->stream);
198 ra_priv->version2 = stream_read_word(demuxer->stream);
199 ra_priv->hdr_size = stream_read_dword(demuxer->stream);
200 ra_priv->codec_flavor = stream_read_word(demuxer->stream);
201 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Flavor: %d\n", ra_priv->codec_flavor);
202 ra_priv->coded_framesize = stream_read_dword(demuxer->stream);
203 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Coded frame size: %d\n", ra_priv->coded_framesize);
204 stream_skip(demuxer->stream, 4); // data size?
205 stream_skip(demuxer->stream, 8);
206 ra_priv->sub_packet_h = stream_read_word(demuxer->stream);
207 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Sub packet h: %d\n", ra_priv->sub_packet_h);
208 ra_priv->frame_size = stream_read_word(demuxer->stream);
209 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Frame size: %d\n", ra_priv->frame_size);
210 ra_priv->sub_packet_size = stream_read_word(demuxer->stream);
211 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Sub packet size: %d\n", ra_priv->sub_packet_size);
212 stream_skip(demuxer->stream, 2);
213 sh->samplerate = stream_read_word(demuxer->stream);
214 stream_skip(demuxer->stream, 2);
215 sh->samplesize = stream_read_word(demuxer->stream);
216 sh->channels = stream_read_word(demuxer->stream);
217 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] %d channel, %d bit, %dHz\n", sh->channels,
218 sh->samplesize, sh->samplerate);
219 i = stream_read_char(demuxer->stream);
220 ra_priv->intl_id = stream_read_dword_le(demuxer->stream);
221 if (i != 4) {
222 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] Interleaver Id size is not 4 (%d), please report to "
223 "MPlayer developers\n", i);
224 stream_skip(demuxer->stream, i - 4);
226 i = stream_read_char(demuxer->stream);
227 sh->format = stream_read_dword_le(demuxer->stream);
228 if (i != 4) {
229 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] FourCC size is not 4 (%d), please report to "
230 "MPlayer developers\n", i);
231 stream_skip(demuxer->stream, i - 4);
233 stream_skip(demuxer->stream, 3);
236 if ((i = stream_read_char(demuxer->stream)) != 0) {
237 buf = malloc(i+1);
238 stream_read(demuxer->stream, buf, i);
239 buf[i] = 0;
240 demux_info_add(demuxer, "Title", buf);
241 free(buf);
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, "Author", buf);
248 free(buf);
250 if ((i = stream_read_char(demuxer->stream)) != 0) {
251 buf = malloc(i+1);
252 stream_read(demuxer->stream, buf, i);
253 buf[i] = 0;
254 demux_info_add(demuxer, "Copyright", buf);
255 free(buf);
258 if ((i = stream_read_char(demuxer->stream)) != 0) {
259 buf = malloc(i+1);
260 stream_read(demuxer->stream, buf, i);
261 buf[i] = 0;
262 demux_info_add(demuxer, "Comment", buf);
263 free(buf);
266 if (ra_priv->version == 3) {
267 if(ra_priv->hdr_size + 8 > stream_tell(demuxer->stream)) {
268 stream_skip(demuxer->stream, 1);
269 i = stream_read_char(demuxer->stream);
270 sh->format = stream_read_dword_le(demuxer->stream);
271 if (i != 4) {
272 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] FourCC size is not 4 (%d), please report to "
273 "MPlayer developers\n", i);
274 stream_skip(demuxer->stream, i - 4);
277 if (sh->format != FOURCC_LPCJ) {
278 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] Version 3 with FourCC %8x, please report to "
279 "MPlayer developers\n", sh->format);
281 } else
282 // If a stream does not have fourcc, let's assume it's 14.4
283 sh->format = FOURCC_LPCJ;
285 sh->channels = 1;
286 sh->samplesize = 16;
287 sh->samplerate = 8000;
288 ra_priv->frame_size = 240;
289 sh->format = FOURCC_144;
292 /* Fill WAVEFORMATEX */
293 sh->wf = malloc(sizeof(WAVEFORMATEX));
294 memset(sh->wf, 0, sizeof(WAVEFORMATEX));
295 sh->wf->nChannels = sh->channels;
296 sh->wf->wBitsPerSample = sh->samplesize;
297 sh->wf->nSamplesPerSec = sh->samplerate;
298 sh->wf->nAvgBytesPerSec = sh->samplerate*sh->samplesize/8;
299 sh->wf->nBlockAlign = ra_priv->frame_size;
300 sh->wf->cbSize = 0;
301 sh->wf->wFormatTag = sh->format;
303 switch (sh->format) {
304 case FOURCC_144:
305 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: 14_4\n");
306 sh->wf->nBlockAlign = 0x14;
307 break;
308 case FOURCC_288:
309 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: 28_8\n");
310 sh->wf->nBlockAlign = ra_priv->coded_framesize;
311 ra_priv->audio_buf = calloc(ra_priv->sub_packet_h, ra_priv->frame_size);
312 break;
313 case FOURCC_DNET:
314 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: DNET -> AC3\n");
315 break;
316 case FOURCC_SIPR:
317 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: SIPR\n");
318 sh->wf->nBlockAlign = ra_priv->coded_framesize;
319 sh->wf->nAvgBytesPerSec = sipr_fl2bps[ra_priv->codec_flavor];
320 ra_priv->audio_buf = calloc(ra_priv->sub_packet_h, ra_priv->frame_size);
321 break;
322 default:
323 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: Unknown (%d)\n", sh->format);
326 print_wave_header(sh->wf, MSGL_V);
328 /* disable seeking */
329 demuxer->seekable = 0;
331 if(!ds_fill_buffer(demuxer->audio))
332 mp_msg(MSGT_DEMUXER,MSGL_INFO,"[RealAudio] No data.\n");
334 return demuxer;
339 static void demux_close_ra(demuxer_t *demuxer)
341 ra_priv_t* ra_priv = demuxer->priv;
343 if (ra_priv) {
344 if (ra_priv->audio_buf)
345 free (ra_priv->audio_buf);
346 free(ra_priv);
348 return;
352 #if 0
353 /* please upload RV10 samples WITH INDEX CHUNK */
354 int demux_seek_ra(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
356 real_priv_t *priv = demuxer->priv;
357 demux_stream_t *d_audio = demuxer->audio;
358 sh_audio_t *sh_audio = d_audio->sh;
359 int aid = d_audio->id;
360 int next_offset = 0;
361 int rel_seek_frames = 0;
362 int streams = 0;
364 return stream_seek(demuxer->stream, next_offset);
366 #endif
369 const demuxer_desc_t demuxer_desc_realaudio = {
370 "Realaudio demuxer",
371 "realaudio",
372 "REALAUDIO",
373 "Roberto Togni",
374 "handles old audio only .ra files",
375 DEMUXER_TYPE_REALAUDIO,
376 1, // safe autodetect
377 ra_check_file,
378 demux_ra_fill_buffer,
379 demux_open_ra,
380 demux_close_ra,
381 NULL,
382 NULL