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