Copyright: delete removed library copies from list
[mplayer/greg.git] / libmpdemux / demux_vqf.c
blob94d7cf40963efa21bd1c58ef05a6572f25bc19d6
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 = calloc(1, sizeof(*sh_audio->wf)+sizeof(headerInfo));
53 hi = (headerInfo *)&w[1];
54 w->wFormatTag = 0x1;
55 sh_audio->format = mmioFOURCC('T','W','I','N'); /* TWinVQ */
56 w->nChannels = sh_audio->channels = 2;
57 w->nSamplesPerSec = sh_audio->samplerate = 44100;
58 w->nAvgBytesPerSec = w->nSamplesPerSec*sh_audio->channels*2;
59 w->nBlockAlign = 0;
60 sh_audio->samplesize = 2;
61 w->wBitsPerSample = 8*sh_audio->samplesize;
62 w->cbSize = 0;
63 strcpy(hi->ID,"TWIN");
64 stream_read(s,hi->ID+KEYWORD_BYTES,VERSION_BYTES); /* fourcc+version_id */
65 while(1)
67 char chunk_id[4];
68 unsigned chunk_size;
69 hi->size=chunk_size=stream_read_dword(s); /* include itself */
70 stream_read(s,chunk_id,4);
71 if (chunk_size < 8) return NULL;
72 chunk_size -= 8;
73 if(AV_RL32(chunk_id)==mmioFOURCC('C','O','M','M'))
75 char buf[BUFSIZ];
76 unsigned i,subchunk_size;
77 if (chunk_size > sizeof(buf) || chunk_size < 20) return NULL;
78 if(stream_read(s,buf,chunk_size)!=chunk_size) return NULL;
79 i=0;
80 subchunk_size = AV_RB32(buf);
81 hi->channelMode = AV_RB32(buf + 4);
82 w->nChannels=sh_audio->channels=hi->channelMode+1; /*0-mono;1-stereo*/
83 hi->bitRate = AV_RB32(buf + 8);
84 sh_audio->i_bps=hi->bitRate*1000/8; /* bitrate kbit/s */
85 w->nAvgBytesPerSec = sh_audio->i_bps;
86 hi->samplingRate = AV_RB32(buf + 12);
87 switch(hi->samplingRate){
88 case 44:
89 w->nSamplesPerSec=44100;
90 break;
91 case 22:
92 w->nSamplesPerSec=22050;
93 break;
94 case 11:
95 w->nSamplesPerSec=11025;
96 break;
97 default:
98 w->nSamplesPerSec=hi->samplingRate*1000;
99 break;
101 sh_audio->samplerate=w->nSamplesPerSec;
102 hi->securityLevel = AV_RB32(buf + 16);
103 w->nBlockAlign = 0;
104 sh_audio->samplesize = 4;
105 w->wBitsPerSample = 8*sh_audio->samplesize;
106 w->cbSize = 0;
107 if (subchunk_size > chunk_size - 4) continue;
108 i+=subchunk_size+4;
109 while(i + 8 < chunk_size)
111 unsigned slen,sid;
112 char sdata[BUFSIZ];
113 sid = AV_RL32(buf + i); i+=4;
114 slen = AV_RB32(buf + i); i+=4;
115 if (slen > sizeof(sdata) - 1 || slen > chunk_size - i) break;
116 if(sid==mmioFOURCC('D','S','I','Z'))
118 hi->Dsiz=AV_RB32(buf + i);
119 continue; /* describes the same info as size of DATA chunk */
121 memcpy(sdata,&buf[i],slen); sdata[slen]=0; i+=slen;
122 if(sid==mmioFOURCC('N','A','M','E'))
124 memcpy(hi->Name,sdata,FFMIN(BUFSIZ,slen));
125 demux_info_add(demuxer,"Title",sdata);
127 else
128 if(sid==mmioFOURCC('A','U','T','H'))
130 memcpy(hi->Auth,sdata,FFMIN(BUFSIZ,slen));
131 demux_info_add(demuxer,"Author",sdata);
133 else
134 if(sid==mmioFOURCC('C','O','M','T'))
136 memcpy(hi->Comt,sdata,FFMIN(BUFSIZ,slen));
137 demux_info_add(demuxer,"Comment",sdata);
139 else
140 if(sid==mmioFOURCC('(','c',')',' '))
142 memcpy(hi->Cpyr,sdata,FFMIN(BUFSIZ,slen));
143 demux_info_add(demuxer,"Copyright",sdata);
145 else
146 if(sid==mmioFOURCC('F','I','L','E'))
148 memcpy(hi->File,sdata,FFMIN(BUFSIZ,slen));
150 else
151 if(sid==mmioFOURCC('A','L','B','M')) demux_info_add(demuxer,"Album",sdata);
152 else
153 if(sid==mmioFOURCC('Y','E','A','R')) demux_info_add(demuxer,"Date",sdata);
154 else
155 if(sid==mmioFOURCC('T','R','A','C')) demux_info_add(demuxer,"Track",sdata);
156 else
157 if(sid==mmioFOURCC('E','N','C','D')) demux_info_add(demuxer,"Encoder",sdata);
158 else
159 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);
160 /* other stuff is unrecognized due untranslatable japan's idiomatics */
163 else
164 if(AV_RL32(chunk_id)==mmioFOURCC('D','A','T','A'))
166 demuxer->movi_start=stream_tell(s);
167 demuxer->movi_end=demuxer->movi_start+chunk_size;
168 mp_msg(MSGT_DEMUX, MSGL_V, "Found data at %"PRIX64" size %"PRIu64"\n",demuxer->movi_start,demuxer->movi_end);
169 /* Done! play it */
170 break;
172 else
174 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);
175 stream_skip(s,chunk_size); /*unknown chunk type */
179 demuxer->audio->id = 0;
180 demuxer->audio->sh = sh_audio;
181 sh_audio->ds = demuxer->audio;
182 stream_seek(s,demuxer->movi_start);
183 demuxer->seekable=0;
184 return demuxer;
187 static int demux_vqf_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
188 sh_audio_t* sh_audio = demuxer->audio->sh;
189 int l = sh_audio->wf->nAvgBytesPerSec;
190 off_t spos = stream_tell(demuxer->stream);
191 demux_packet_t* dp;
193 if(stream_eof(demuxer->stream))
194 return 0;
196 dp = new_demux_packet(l);
197 ds->pts = spos / (float)(sh_audio->wf->nAvgBytesPerSec);
198 ds->pos = spos;
200 l=stream_read(demuxer->stream,dp->buffer,l);
201 resize_demux_packet(dp,l);
202 ds_add_packet(ds,dp);
204 return 1;
207 static void demux_seek_vqf(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
208 #if 0
209 stream_t* s = demuxer->stream;
210 sh_audio_t* sh_audio = demuxer->audio->sh;
211 off_t base,pos;
213 base = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : stream_tell(s);
214 if(flags & SEEK_FACTOR)
215 pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
216 else
217 pos = base + (rel_seek_secs*sh_audio->i_bps);
219 pos -= (pos % (sh_audio->channels * sh_audio->samplesize) );
220 stream_seek(s,pos);
221 #endif
224 static void demux_close_vqf(demuxer_t* demuxer) {}
227 const demuxer_desc_t demuxer_desc_vqf = {
228 "TwinVQ demuxer",
229 "vqf",
230 "VQF",
231 "Nick Kurshev",
232 "ported frm MPlayerXP",
233 DEMUXER_TYPE_VQF,
234 1, // safe autodetect
235 demux_probe_vqf,
236 demux_vqf_fill_buffer,
237 demux_open_vqf,
238 demux_close_vqf,
239 demux_seek_vqf,
240 NULL