synced with r21612
[mplayer/greg.git] / libmpdemux / demux_vqf.c
blob3aa0c204e03f54812bb968fcaa2ebffd4a730843
1 #include "config.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include "libavutil/common.h"
6 #include "mpbswap.h"
8 #include "stream.h"
9 #include "demuxer.h"
10 #include "stheader.h"
11 #include "libmpcodecs/vqf.h"
13 static int demux_probe_vqf(demuxer_t* demuxer)
15 char buf[KEYWORD_BYTES];
16 stream_t *s;
17 s = demuxer->stream;
18 if(stream_read(s,buf,KEYWORD_BYTES)!=KEYWORD_BYTES)
19 return 0;
20 if(memcmp(buf,"TWIN",KEYWORD_BYTES)==0) return DEMUXER_TYPE_VQF; /*version: 97012000*/
21 return 0;
24 static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
25 sh_audio_t* sh_audio;
26 WAVEFORMATEX* w;
27 stream_t *s;
28 headerInfo *hi;
30 s = demuxer->stream;
32 sh_audio = new_sh_audio(demuxer,0);
33 sh_audio->wf = w = malloc(sizeof(WAVEFORMATEX)+sizeof(headerInfo));
34 hi = (headerInfo *)&w[1];
35 memset(hi,0,sizeof(headerInfo));
36 w->wFormatTag = 0x1;
37 sh_audio->format = mmioFOURCC('T','W','I','N'); /* TWinVQ */
38 w->nChannels = sh_audio->channels = 2;
39 w->nSamplesPerSec = sh_audio->samplerate = 44100;
40 w->nAvgBytesPerSec = w->nSamplesPerSec*sh_audio->channels*2;
41 w->nBlockAlign = 0;
42 sh_audio->samplesize = 2;
43 w->wBitsPerSample = 8*sh_audio->samplesize;
44 w->cbSize = 0;
45 strcpy(hi->ID,"TWIN");
46 stream_read(s,hi->ID+KEYWORD_BYTES,VERSION_BYTES); /* fourcc+version_id */
47 while(1)
49 char chunk_id[4];
50 unsigned chunk_size;
51 hi->size=chunk_size=stream_read_dword(s); /* include itself */
52 stream_read(s,chunk_id,4);
53 if(*((uint32_t *)&chunk_id[0])==mmioFOURCC('C','O','M','M'))
55 char buf[chunk_size-8];
56 unsigned i,subchunk_size;
57 if(stream_read(s,buf,chunk_size-8)!=chunk_size-8) return NULL;
58 i=0;
59 subchunk_size=be2me_32(*((uint32_t *)&buf[0]));
60 hi->channelMode=be2me_32(*((uint32_t *)&buf[4]));
61 w->nChannels=sh_audio->channels=hi->channelMode+1; /*0-mono;1-stereo*/
62 hi->bitRate=be2me_32(*((uint32_t *)&buf[8]));
63 sh_audio->i_bps=hi->bitRate*1000/8; /* bitrate kbit/s */
64 w->nAvgBytesPerSec = sh_audio->i_bps;
65 hi->samplingRate=be2me_32(*((uint32_t *)&buf[12]));
66 switch(hi->samplingRate){
67 case 44:
68 w->nSamplesPerSec=44100;
69 break;
70 case 22:
71 w->nSamplesPerSec=22050;
72 break;
73 case 11:
74 w->nSamplesPerSec=11025;
75 break;
76 default:
77 w->nSamplesPerSec=hi->samplingRate*1000;
78 break;
80 sh_audio->samplerate=w->nSamplesPerSec;
81 hi->securityLevel=be2me_32(*((uint32_t *)&buf[16]));
82 w->nBlockAlign = 0;
83 sh_audio->samplesize = 4;
84 w->wBitsPerSample = 8*sh_audio->samplesize;
85 w->cbSize = 0;
86 i+=subchunk_size+4;
87 while(i<chunk_size-8)
89 unsigned slen,sid;
90 char sdata[chunk_size];
91 sid=*((uint32_t *)&buf[i]); i+=4;
92 slen=be2me_32(*((uint32_t *)&buf[i])); i+=4;
93 if(sid==mmioFOURCC('D','S','I','Z'))
95 hi->Dsiz=be2me_32(*((uint32_t *)&buf[i]));
96 continue; /* describes the same info as size of DATA chunk */
98 memcpy(sdata,&buf[i],slen); sdata[slen]=0; i+=slen;
99 if(sid==mmioFOURCC('N','A','M','E'))
101 memcpy(hi->Name,sdata,FFMIN(BUFSIZ,slen));
102 demux_info_add(demuxer,"Title",sdata);
104 else
105 if(sid==mmioFOURCC('A','U','T','H'))
107 memcpy(hi->Auth,sdata,FFMIN(BUFSIZ,slen));
108 demux_info_add(demuxer,"Author",sdata);
110 else
111 if(sid==mmioFOURCC('C','O','M','T'))
113 memcpy(hi->Comt,sdata,FFMIN(BUFSIZ,slen));
114 demux_info_add(demuxer,"Comment",sdata);
116 else
117 if(sid==mmioFOURCC('(','c',')',' '))
119 memcpy(hi->Cpyr,sdata,FFMIN(BUFSIZ,slen));
120 demux_info_add(demuxer,"Copyright",sdata);
122 else
123 if(sid==mmioFOURCC('F','I','L','E'))
125 memcpy(hi->File,sdata,FFMIN(BUFSIZ,slen));
127 else
128 if(sid==mmioFOURCC('A','L','B','M')) demux_info_add(demuxer,"Album",sdata);
129 else
130 if(sid==mmioFOURCC('Y','E','A','R')) demux_info_add(demuxer,"Date",sdata);
131 else
132 if(sid==mmioFOURCC('T','R','A','C')) demux_info_add(demuxer,"Track",sdata);
133 else
134 if(sid==mmioFOURCC('E','N','C','D')) demux_info_add(demuxer,"Encoder",sdata);
135 else
136 mp_msg(MSGT_DEMUX, MSGL_V, "Unhandled subchunk '%c%c%c%c'='%s'\n",((char *)&sid)[0],((char *)&sid)[1],((char *)&sid)[2],((char *)&sid)[3],sdata);
137 /* other stuff is unrecognized due untranslatable japan's idiomatics */
140 else
141 if(*((uint32_t *)&chunk_id[0])==mmioFOURCC('D','A','T','A'))
143 demuxer->movi_start=stream_tell(s);
144 demuxer->movi_end=demuxer->movi_start+chunk_size-8;
145 mp_msg(MSGT_DEMUX, MSGL_V, "Found data at %"PRIX64" size %"PRIu64"\n",demuxer->movi_start,demuxer->movi_end);
146 /* Done! play it */
147 break;
149 else
151 mp_msg(MSGT_DEMUX, MSGL_V, "Unhandled chunk '%c%c%c%c' %u bytes\n",((char *)&chunk_id)[0],((char *)&chunk_id)[1],((char *)&chunk_id)[2],((char *)&chunk_id)[3],chunk_size);
152 stream_skip(s,chunk_size-8); /*unknown chunk type */
156 demuxer->audio->sh = sh_audio;
157 sh_audio->ds = demuxer->audio;
158 stream_seek(s,demuxer->movi_start);
159 demuxer->seekable=0;
160 return demuxer;
163 static int demux_vqf_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
164 sh_audio_t* sh_audio = demuxer->audio->sh;
165 int l = sh_audio->wf->nAvgBytesPerSec;
166 off_t spos = stream_tell(demuxer->stream);
167 demux_packet_t* dp;
169 if(stream_eof(demuxer->stream))
170 return 0;
172 dp = new_demux_packet(l);
173 ds->pts = spos / (float)(sh_audio->wf->nAvgBytesPerSec);
174 ds->pos = spos;
176 l=stream_read(demuxer->stream,dp->buffer,l);
177 resize_demux_packet(dp,l);
178 ds_add_packet(ds,dp);
180 return 1;
183 static void demux_seek_vqf(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
184 #if 0
185 stream_t* s = demuxer->stream;
186 sh_audio_t* sh_audio = demuxer->audio->sh;
187 off_t base,pos;
189 base = (flags & 1) ? demuxer->movi_start : stream_tell(s);
190 if(flags & 2)
191 pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
192 else
193 pos = base + (rel_seek_secs*sh_audio->i_bps);
195 pos -= (pos % (sh_audio->channels * sh_audio->samplesize) );
196 stream_seek(s,pos);
197 #endif
200 static void demux_close_vqf(demuxer_t* demuxer) {}
203 demuxer_desc_t demuxer_desc_vqf = {
204 "TwinVQ demuxer",
205 "vqf",
206 "VQF",
207 "Nick Kurshev",
208 "ported frm MPlayerXP",
209 DEMUXER_TYPE_VQF,
210 1, // safe autodetect
211 demux_probe_vqf,
212 demux_vqf_fill_buffer,
213 demux_open_vqf,
214 demux_close_vqf,
215 demux_seek_vqf,
216 NULL