10034
|
1 /*
|
|
2 Realaudio demuxer for MPlayer
|
|
3 (c) 2003 Roberto Togni
|
|
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
|
|
43 int ra_check_file(demuxer_t* demuxer)
|
|
44 {
|
|
45 unsigned int chunk_id;
|
|
46
|
|
47 chunk_id = stream_read_dword_le(demuxer->stream);
|
|
48 if (chunk_id == FOURCC_DOTRA)
|
|
49 return 1;
|
|
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
|
|
61 int demux_ra_fill_buffer(demuxer_t *demuxer)
|
|
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
|
|
81 if (sh->format == 0x2000) {
|
|
82 // if DNET, swap bytes, as DNET is byte-swapped AC3:
|
|
83 char *ptr = dp->buffer;
|
|
84 int i;
|
|
85 for (i = 0; i < len; i += 2) {
|
|
86 const char tmp = ptr[0];
|
|
87 ptr[0] = ptr[1];
|
|
88 ptr[1] = tmp;
|
|
89 ptr += 2;
|
|
90 }
|
|
91 }
|
10035
|
92 dp->pts = demuxer->filepos / ra_priv->data_size;
|
10034
|
93 dp->pos = demuxer->filepos;
|
|
94 dp->flags = 0;
|
|
95 ds_add_packet(ds, dp);
|
|
96
|
|
97 return 1;
|
|
98 }
|
|
99
|
|
100
|
|
101
|
|
102 extern void print_wave_header(WAVEFORMATEX *h);
|
|
103
|
|
104
|
|
105
|
|
106 void demux_open_ra(demuxer_t* demuxer)
|
|
107 {
|
|
108 ra_priv_t* ra_priv = demuxer->priv;
|
|
109 sh_audio_t *sh;
|
|
110 int i;
|
|
111 char *buf;
|
|
112
|
|
113 if ((ra_priv = (ra_priv_t *)malloc(sizeof(ra_priv_t))) == NULL) {
|
|
114 mp_msg(MSGT_DEMUX, MSGL_ERR, "[RealAudio] Can't allocate memory for private data.\n");
|
|
115 return NULL;
|
|
116 }
|
|
117 memset(ra_priv, 0, sizeof(ra_priv_t));
|
|
118
|
|
119 demuxer->priv = ra_priv;
|
|
120 sh = new_sh_audio(demuxer, 0);
|
|
121 demuxer->audio->id = 0;
|
|
122 sh->ds=demuxer->audio;
|
|
123 demuxer->audio->sh = sh;
|
|
124
|
|
125 ra_priv->version = stream_read_word(demuxer->stream);
|
|
126 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] File version: %d\n", ra_priv->version);
|
|
127 if ((ra_priv->version < 3) || (ra_priv->version > 4)) {
|
|
128 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] ra version %d is not supported yet, please "
|
|
129 "contact MPlayer developers\n", ra_priv->version);
|
|
130 return NULL;
|
|
131 }
|
|
132 if (ra_priv->version == 3) {
|
|
133 ra_priv->hdr_size = stream_read_word(demuxer->stream);
|
|
134 stream_skip(demuxer->stream, 10);
|
|
135 ra_priv->data_size = stream_read_dword(demuxer->stream);
|
|
136 } else {
|
|
137 stream_skip(demuxer->stream, 2);
|
|
138 ra_priv->dotranum = stream_read_dword(demuxer->stream);
|
|
139 ra_priv->data_size = stream_read_dword(demuxer->stream);
|
|
140 ra_priv->version2 = stream_read_word(demuxer->stream);
|
|
141 ra_priv->hdr_size = stream_read_dword(demuxer->stream);
|
|
142 ra_priv->codec_flavor = stream_read_word(demuxer->stream);
|
|
143 ra_priv->coded_framesize = stream_read_dword(demuxer->stream);
|
|
144 stream_skip(demuxer->stream, 4); // data size?
|
|
145 stream_skip(demuxer->stream, 8);
|
|
146 ra_priv->sub_packet_h = stream_read_word(demuxer->stream);
|
|
147 ra_priv->frame_size = stream_read_word(demuxer->stream);
|
|
148 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Frame size: %d\n", ra_priv->frame_size);
|
|
149 ra_priv->sub_packet_size = stream_read_word(demuxer->stream);
|
|
150 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] Sub packet size: %d\n", ra_priv->sub_packet_size);
|
|
151 stream_skip(demuxer->stream, 2);
|
|
152 sh->samplerate = stream_read_word(demuxer->stream);
|
|
153 stream_skip(demuxer->stream, 2);
|
|
154 sh->samplesize = stream_read_word(demuxer->stream);
|
|
155 sh->channels = stream_read_word(demuxer->stream);
|
|
156 mp_msg(MSGT_DEMUX,MSGL_V,"[RealAudio] %d channel, %d bit, %dHz\n", sh->channels,
|
|
157 sh->samplesize, sh->samplerate);
|
|
158 i = stream_read_char(demuxer->stream);
|
|
159 *((unsigned int *)(ra_priv->genr)) = stream_read_dword(demuxer->stream);
|
|
160 if (i != 4) {
|
|
161 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] Genr size is not 4 (%d), please report to "
|
|
162 "MPlayer developers\n", i);
|
|
163 stream_skip(demuxer->stream, i - 4);
|
|
164 }
|
|
165 i = stream_read_char(demuxer->stream);
|
|
166 sh->format = stream_read_dword_le(demuxer->stream);
|
|
167 if (i != 4) {
|
|
168 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] FourCC size is not 4 (%d), please report to "
|
|
169 "MPlayer developers\n", i);
|
|
170 stream_skip(demuxer->stream, i - 4);
|
|
171 }
|
|
172 stream_skip(demuxer->stream, 3);
|
|
173 }
|
|
174
|
|
175 if ((i = stream_read_char(demuxer->stream)) != 0) {
|
|
176 buf = malloc(i+1);
|
|
177 stream_read(demuxer->stream, buf, i);
|
|
178 buf[i] = 0;
|
|
179 demux_info_add(demuxer, "Title", buf);
|
|
180 free(buf);
|
|
181 }
|
|
182 if ((i = stream_read_char(demuxer->stream)) != 0) {
|
|
183 buf = malloc(i+1);
|
|
184 stream_read(demuxer->stream, buf, i);
|
|
185 buf[i] = 0;
|
|
186 demux_info_add(demuxer, "Author", buf);
|
|
187 free(buf);
|
|
188 }
|
|
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, "Copyright", buf);
|
|
194 free(buf);
|
|
195 }
|
|
196
|
|
197 if ((i = stream_read_char(demuxer->stream)) != 0)
|
|
198 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] Last header byte is not zero!\n");
|
|
199
|
|
200 if (ra_priv->version == 3) {
|
|
201 stream_skip(demuxer->stream, 1);
|
|
202 i = stream_read_char(demuxer->stream);
|
|
203 sh->format = stream_read_dword_le(demuxer->stream);
|
|
204 if (i != 4) {
|
|
205 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] FourCC size is not 4 (%d), please report to "
|
|
206 "MPlayer developers\n", i);
|
|
207 stream_skip(demuxer->stream, i - 4);
|
|
208 }
|
|
209 // stream_skip(demuxer->stream, 3);
|
|
210
|
|
211 if (sh->format != FOURCC_LPCJ) {
|
|
212 mp_msg(MSGT_DEMUX,MSGL_WARN,"[RealAudio] Version 3 with FourCC %8x, please report to "
|
|
213 "MPlayer developers\n", sh->format);
|
|
214 }
|
|
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");
|
|
247 sh->wf->cbSize = 8/*+codecdata_length*/;
|
|
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 sh->format = 0x2000;
|
|
258 break;
|
|
259 default:
|
|
260 mp_msg(MSGT_DEMUX,MSGL_V,"Audio: Unknown (%d)\n", sh->format);
|
|
261 }
|
|
262
|
|
263 print_wave_header(sh->wf);
|
|
264
|
|
265 /* disable seeking */
|
|
266 demuxer->seekable = 0;
|
|
267
|
|
268 if(!ds_fill_buffer(demuxer->audio))
|
|
269 mp_msg(MSGT_DEMUXER,MSGL_INFO,"[RealAudio] No data.\n");
|
|
270 }
|
|
271
|
|
272
|
|
273
|
|
274 void demux_close_ra(demuxer_t *demuxer)
|
|
275 {
|
|
276 ra_priv_t* ra_priv = demuxer->priv;
|
|
277
|
|
278 if (ra_priv)
|
|
279 free(ra_priv);
|
|
280
|
|
281 return;
|
|
282 }
|
|
283
|
|
284
|
|
285 #if 0
|
|
286 /* please upload RV10 samples WITH INDEX CHUNK */
|
|
287 int demux_seek_ra(demuxer_t *demuxer, float rel_seek_secs, int flags)
|
|
288 {
|
|
289 real_priv_t *priv = demuxer->priv;
|
|
290 demux_stream_t *d_audio = demuxer->audio;
|
|
291 sh_audio_t *sh_audio = d_audio->sh;
|
|
292 int aid = d_audio->id;
|
|
293 int next_offset = 0;
|
|
294 int rel_seek_frames = 0;
|
|
295 int streams = 0;
|
|
296
|
|
297 return stream_seek(demuxer->stream, next_offset);
|
|
298 }
|
|
299 #endif
|