subassconvert: do not escape likely ASS override tags
[mplayer.git] / libmpdemux / demux_vqf.c
blob051e912339913e709b208644ea3d14bb850ba97e
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 <libavutil/intreadwrite.h>
26 #include "stream/stream.h"
27 #include "demuxer.h"
28 #include "stheader.h"
29 #include "libmpcodecs/vqf.h"
31 static int demux_probe_vqf(demuxer_t* demuxer)
33 char buf[KEYWORD_BYTES];
34 stream_t *s;
35 s = demuxer->stream;
36 if(stream_read(s,buf,KEYWORD_BYTES)!=KEYWORD_BYTES)
37 return 0;
38 if(memcmp(buf,"TWIN",KEYWORD_BYTES)==0) return DEMUXER_TYPE_VQF; /*version: 97012000*/
39 return 0;
42 static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
43 sh_audio_t* sh_audio;
44 WAVEFORMATEX* w;
45 stream_t *s;
46 headerInfo *hi;
48 s = demuxer->stream;
50 sh_audio = new_sh_audio(demuxer,0);
51 sh_audio->wf = w = calloc(1, sizeof(*sh_audio->wf)+sizeof(headerInfo));
52 hi = (headerInfo *)&w[1];
53 w->wFormatTag = 0x1;
54 sh_audio->format = mmioFOURCC('T','W','I','N'); /* TWinVQ */
55 w->nChannels = sh_audio->channels = 2;
56 w->nSamplesPerSec = sh_audio->samplerate = 44100;
57 w->nAvgBytesPerSec = w->nSamplesPerSec*sh_audio->channels*2;
58 w->nBlockAlign = 0;
59 sh_audio->samplesize = 2;
60 w->wBitsPerSample = 8*sh_audio->samplesize;
61 w->cbSize = 0;
62 strcpy(hi->ID,"TWIN");
63 stream_read(s,hi->ID+KEYWORD_BYTES,VERSION_BYTES); /* fourcc+version_id */
64 while(1)
66 char chunk_id[4];
67 unsigned chunk_size;
68 hi->size=chunk_size=stream_read_dword(s); /* include itself */
69 stream_read(s,chunk_id,4);
70 if (chunk_size < 8) return NULL;
71 chunk_size -= 8;
72 if(AV_RL32(chunk_id)==mmioFOURCC('C','O','M','M'))
74 char buf[BUFSIZ];
75 unsigned i,subchunk_size;
76 if (chunk_size > sizeof(buf) || chunk_size < 20) return NULL;
77 if(stream_read(s,buf,chunk_size)!=chunk_size) return NULL;
78 i=0;
79 subchunk_size = AV_RB32(buf);
80 hi->channelMode = AV_RB32(buf + 4);
81 w->nChannels=sh_audio->channels=hi->channelMode+1; /*0-mono;1-stereo*/
82 hi->bitRate = AV_RB32(buf + 8);
83 sh_audio->i_bps=hi->bitRate*1000/8; /* bitrate kbit/s */
84 w->nAvgBytesPerSec = sh_audio->i_bps;
85 hi->samplingRate = AV_RB32(buf + 12);
86 switch(hi->samplingRate){
87 case 44:
88 w->nSamplesPerSec=44100;
89 break;
90 case 22:
91 w->nSamplesPerSec=22050;
92 break;
93 case 11:
94 w->nSamplesPerSec=11025;
95 break;
96 default:
97 w->nSamplesPerSec=hi->samplingRate*1000;
98 break;
100 sh_audio->samplerate=w->nSamplesPerSec;
101 hi->securityLevel = AV_RB32(buf + 16);
102 w->nBlockAlign = 0;
103 sh_audio->samplesize = 4;
104 w->wBitsPerSample = 8*sh_audio->samplesize;
105 w->cbSize = 0;
106 if (subchunk_size > chunk_size - 4) continue;
107 i+=subchunk_size+4;
108 while(i + 8 < chunk_size)
110 unsigned slen,sid;
111 char sdata[BUFSIZ];
112 sid = AV_RL32(buf + i); i+=4;
113 slen = AV_RB32(buf + i); i+=4;
114 if (slen > sizeof(sdata) - 1 || slen > chunk_size - i) break;
115 if(sid==mmioFOURCC('D','S','I','Z'))
117 hi->Dsiz=AV_RB32(buf + i);
118 continue; /* describes the same info as size of DATA chunk */
120 memcpy(sdata,&buf[i],slen); sdata[slen]=0; i+=slen;
121 if(sid==mmioFOURCC('N','A','M','E'))
123 memcpy(hi->Name,sdata,FFMIN(BUFSIZ,slen));
124 demux_info_add(demuxer,"Title",sdata);
126 else
127 if(sid==mmioFOURCC('A','U','T','H'))
129 memcpy(hi->Auth,sdata,FFMIN(BUFSIZ,slen));
130 demux_info_add(demuxer,"Author",sdata);
132 else
133 if(sid==mmioFOURCC('C','O','M','T'))
135 memcpy(hi->Comt,sdata,FFMIN(BUFSIZ,slen));
136 demux_info_add(demuxer,"Comment",sdata);
138 else
139 if(sid==mmioFOURCC('(','c',')',' '))
141 memcpy(hi->Cpyr,sdata,FFMIN(BUFSIZ,slen));
142 demux_info_add(demuxer,"Copyright",sdata);
144 else
145 if(sid==mmioFOURCC('F','I','L','E'))
147 memcpy(hi->File,sdata,FFMIN(BUFSIZ,slen));
149 else
150 if(sid==mmioFOURCC('A','L','B','M')) demux_info_add(demuxer,"Album",sdata);
151 else
152 if(sid==mmioFOURCC('Y','E','A','R')) demux_info_add(demuxer,"Date",sdata);
153 else
154 if(sid==mmioFOURCC('T','R','A','C')) demux_info_add(demuxer,"Track",sdata);
155 else
156 if(sid==mmioFOURCC('E','N','C','D')) demux_info_add(demuxer,"Encoder",sdata);
157 else
158 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);
159 /* rest not recognized due to untranslatable Japanese expressions */
162 else
163 if(AV_RL32(chunk_id)==mmioFOURCC('D','A','T','A'))
165 demuxer->movi_start=stream_tell(s);
166 demuxer->movi_end=demuxer->movi_start+chunk_size;
167 mp_msg(MSGT_DEMUX, MSGL_V, "Found data at %"PRIX64" size %"PRIu64"\n",demuxer->movi_start,demuxer->movi_end);
168 /* Done! play it */
169 break;
171 else
173 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);
174 stream_skip(s,chunk_size); /*unknown chunk type */
178 demuxer->audio->id = 0;
179 demuxer->audio->sh = sh_audio;
180 sh_audio->ds = demuxer->audio;
181 stream_seek(s,demuxer->movi_start);
182 demuxer->seekable=0;
183 return demuxer;
186 static int demux_vqf_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
187 sh_audio_t* sh_audio = demuxer->audio->sh;
188 int l = sh_audio->wf->nAvgBytesPerSec;
189 off_t spos = stream_tell(demuxer->stream);
190 demux_packet_t* dp;
192 if(stream_eof(demuxer->stream))
193 return 0;
195 dp = new_demux_packet(l);
196 ds->pts = spos / (float)(sh_audio->wf->nAvgBytesPerSec);
197 ds->pos = spos;
199 l=stream_read(demuxer->stream,dp->buffer,l);
200 resize_demux_packet(dp,l);
201 ds_add_packet(ds,dp);
203 return 1;
206 static void demux_seek_vqf(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
207 #if 0
208 stream_t* s = demuxer->stream;
209 sh_audio_t* sh_audio = demuxer->audio->sh;
210 off_t base,pos;
212 base = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : stream_tell(s);
213 if(flags & SEEK_FACTOR)
214 pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
215 else
216 pos = base + (rel_seek_secs*sh_audio->i_bps);
218 pos -= (pos % (sh_audio->channels * sh_audio->samplesize) );
219 stream_seek(s,pos);
220 #endif
223 static void demux_close_vqf(demuxer_t* demuxer) {}
226 const demuxer_desc_t demuxer_desc_vqf = {
227 "TwinVQ demuxer",
228 "vqf",
229 "VQF",
230 "Nick Kurshev",
231 "ported from MPlayerXP",
232 DEMUXER_TYPE_VQF,
233 1, // safe autodetect
234 demux_probe_vqf,
235 demux_vqf_fill_buffer,
236 demux_open_vqf,
237 demux_close_vqf,
238 demux_seek_vqf,
239 NULL