Ignore svn changes up to r30324
[mplayer/glamo.git] / libmpdemux / demux_vqf.c
blob9a90f33fb3445bfa6d33b1a91062577e820c3c90
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "libavutil/common.h"
24 #include "ffmpeg_files/intreadwrite.h"
25 #include "mpbswap.h"
27 #include "stream/stream.h"
28 #include "demuxer.h"
29 #include "stheader.h"
30 #include "libmpcodecs/vqf.h"
32 static int demux_probe_vqf(demuxer_t* demuxer)
34 char buf[KEYWORD_BYTES];
35 stream_t *s;
36 s = demuxer->stream;
37 if(stream_read(s,buf,KEYWORD_BYTES)!=KEYWORD_BYTES)
38 return 0;
39 if(memcmp(buf,"TWIN",KEYWORD_BYTES)==0) return DEMUXER_TYPE_VQF; /*version: 97012000*/
40 return 0;
43 static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
44 sh_audio_t* sh_audio;
45 WAVEFORMATEX* w;
46 stream_t *s;
47 headerInfo *hi;
49 s = demuxer->stream;
51 sh_audio = new_sh_audio(demuxer,0);
52 sh_audio->wf = w = malloc(sizeof(WAVEFORMATEX)+sizeof(headerInfo));
53 hi = (headerInfo *)&w[1];
54 memset(hi,0,sizeof(headerInfo));
55 w->wFormatTag = 0x1;
56 sh_audio->format = mmioFOURCC('T','W','I','N'); /* TWinVQ */
57 w->nChannels = sh_audio->channels = 2;
58 w->nSamplesPerSec = sh_audio->samplerate = 44100;
59 w->nAvgBytesPerSec = w->nSamplesPerSec*sh_audio->channels*2;
60 w->nBlockAlign = 0;
61 sh_audio->samplesize = 2;
62 w->wBitsPerSample = 8*sh_audio->samplesize;
63 w->cbSize = 0;
64 strcpy(hi->ID,"TWIN");
65 stream_read(s,hi->ID+KEYWORD_BYTES,VERSION_BYTES); /* fourcc+version_id */
66 while(1)
68 char chunk_id[4];
69 unsigned chunk_size;
70 hi->size=chunk_size=stream_read_dword(s); /* include itself */
71 stream_read(s,chunk_id,4);
72 if (chunk_size < 8) return NULL;
73 chunk_size -= 8;
74 if(AV_RL32(chunk_id)==mmioFOURCC('C','O','M','M'))
76 char buf[BUFSIZ];
77 unsigned i,subchunk_size;
78 if (chunk_size > sizeof(buf) || chunk_size < 20) return NULL;
79 if(stream_read(s,buf,chunk_size)!=chunk_size) return NULL;
80 i=0;
81 subchunk_size = AV_RB32(buf);
82 hi->channelMode = AV_RB32(buf + 4);
83 w->nChannels=sh_audio->channels=hi->channelMode+1; /*0-mono;1-stereo*/
84 hi->bitRate = AV_RB32(buf + 8);
85 sh_audio->i_bps=hi->bitRate*1000/8; /* bitrate kbit/s */
86 w->nAvgBytesPerSec = sh_audio->i_bps;
87 hi->samplingRate = AV_RB32(buf + 12);
88 switch(hi->samplingRate){
89 case 44:
90 w->nSamplesPerSec=44100;
91 break;
92 case 22:
93 w->nSamplesPerSec=22050;
94 break;
95 case 11:
96 w->nSamplesPerSec=11025;
97 break;
98 default:
99 w->nSamplesPerSec=hi->samplingRate*1000;
100 break;
102 sh_audio->samplerate=w->nSamplesPerSec;
103 hi->securityLevel = AV_RB32(buf + 16);
104 w->nBlockAlign = 0;
105 sh_audio->samplesize = 4;
106 w->wBitsPerSample = 8*sh_audio->samplesize;
107 w->cbSize = 0;
108 if (subchunk_size > chunk_size - 4) continue;
109 i+=subchunk_size+4;
110 while(i + 8 < chunk_size)
112 unsigned slen,sid;
113 char sdata[BUFSIZ];
114 sid = AV_RL32(buf + i); i+=4;
115 slen = AV_RB32(buf + i); i+=4;
116 if (slen > sizeof(sdata) - 1 || slen > chunk_size - i) break;
117 if(sid==mmioFOURCC('D','S','I','Z'))
119 hi->Dsiz=AV_RB32(buf + i);
120 continue; /* describes the same info as size of DATA chunk */
122 memcpy(sdata,&buf[i],slen); sdata[slen]=0; i+=slen;
123 if(sid==mmioFOURCC('N','A','M','E'))
125 memcpy(hi->Name,sdata,FFMIN(BUFSIZ,slen));
126 demux_info_add(demuxer,"Title",sdata);
128 else
129 if(sid==mmioFOURCC('A','U','T','H'))
131 memcpy(hi->Auth,sdata,FFMIN(BUFSIZ,slen));
132 demux_info_add(demuxer,"Author",sdata);
134 else
135 if(sid==mmioFOURCC('C','O','M','T'))
137 memcpy(hi->Comt,sdata,FFMIN(BUFSIZ,slen));
138 demux_info_add(demuxer,"Comment",sdata);
140 else
141 if(sid==mmioFOURCC('(','c',')',' '))
143 memcpy(hi->Cpyr,sdata,FFMIN(BUFSIZ,slen));
144 demux_info_add(demuxer,"Copyright",sdata);
146 else
147 if(sid==mmioFOURCC('F','I','L','E'))
149 memcpy(hi->File,sdata,FFMIN(BUFSIZ,slen));
151 else
152 if(sid==mmioFOURCC('A','L','B','M')) demux_info_add(demuxer,"Album",sdata);
153 else
154 if(sid==mmioFOURCC('Y','E','A','R')) demux_info_add(demuxer,"Date",sdata);
155 else
156 if(sid==mmioFOURCC('T','R','A','C')) demux_info_add(demuxer,"Track",sdata);
157 else
158 if(sid==mmioFOURCC('E','N','C','D')) demux_info_add(demuxer,"Encoder",sdata);
159 else
160 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);
161 /* other stuff is unrecognized due untranslatable japan's idiomatics */
164 else
165 if(AV_RL32(chunk_id)==mmioFOURCC('D','A','T','A'))
167 demuxer->movi_start=stream_tell(s);
168 demuxer->movi_end=demuxer->movi_start+chunk_size;
169 mp_msg(MSGT_DEMUX, MSGL_V, "Found data at %"PRIX64" size %"PRIu64"\n",demuxer->movi_start,demuxer->movi_end);
170 /* Done! play it */
171 break;
173 else
175 mp_msg(MSGT_DEMUX, MSGL_V, "Unhandled chunk '%c%c%c%c' %u bytes\n",chunk_id[0],chunk_id[1],chunk_id[2],chunk_id[3],chunk_size);
176 stream_skip(s,chunk_size); /*unknown chunk type */
180 demuxer->audio->id = 0;
181 demuxer->audio->sh = sh_audio;
182 sh_audio->ds = demuxer->audio;
183 stream_seek(s,demuxer->movi_start);
184 demuxer->seekable=0;
185 return demuxer;
188 static int demux_vqf_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
189 sh_audio_t* sh_audio = demuxer->audio->sh;
190 int l = sh_audio->wf->nAvgBytesPerSec;
191 off_t spos = stream_tell(demuxer->stream);
192 demux_packet_t* dp;
194 if(stream_eof(demuxer->stream))
195 return 0;
197 dp = new_demux_packet(l);
198 ds->pts = spos / (float)(sh_audio->wf->nAvgBytesPerSec);
199 ds->pos = spos;
201 l=stream_read(demuxer->stream,dp->buffer,l);
202 resize_demux_packet(dp,l);
203 ds_add_packet(ds,dp);
205 return 1;
208 static void demux_seek_vqf(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
209 #if 0
210 stream_t* s = demuxer->stream;
211 sh_audio_t* sh_audio = demuxer->audio->sh;
212 off_t base,pos;
214 base = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : stream_tell(s);
215 if(flags & SEEK_FACTOR)
216 pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
217 else
218 pos = base + (rel_seek_secs*sh_audio->i_bps);
220 pos -= (pos % (sh_audio->channels * sh_audio->samplesize) );
221 stream_seek(s,pos);
222 #endif
225 static void demux_close_vqf(demuxer_t* demuxer) {}
228 const demuxer_desc_t demuxer_desc_vqf = {
229 "TwinVQ demuxer",
230 "vqf",
231 "VQF",
232 "Nick Kurshev",
233 "ported frm MPlayerXP",
234 DEMUXER_TYPE_VQF,
235 1, // safe autodetect
236 demux_probe_vqf,
237 demux_vqf_fill_buffer,
238 demux_open_vqf,
239 demux_close_vqf,
240 demux_seek_vqf,
241 NULL