5 #include "libavutil/common.h"
11 #include "libmpcodecs/vqf.h"
13 static int demux_probe_vqf(demuxer_t
* demuxer
)
15 char buf
[KEYWORD_BYTES
];
18 if(stream_read(s
,buf
,KEYWORD_BYTES
)!=KEYWORD_BYTES
)
20 if(memcmp(buf
,"TWIN",KEYWORD_BYTES
)==0) return DEMUXER_TYPE_VQF
; /*version: 97012000*/
24 static demuxer_t
* demux_open_vqf(demuxer_t
* demuxer
) {
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
));
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;
42 sh_audio
->samplesize
= 2;
43 w
->wBitsPerSample
= 8*sh_audio
->samplesize
;
45 strcpy(hi
->ID
,"TWIN");
46 stream_read(s
,hi
->ID
+KEYWORD_BYTES
,VERSION_BYTES
); /* fourcc+version_id */
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
;
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
){
68 w
->nSamplesPerSec
=44100;
71 w
->nSamplesPerSec
=22050;
74 w
->nSamplesPerSec
=11025;
77 w
->nSamplesPerSec
=hi
->samplingRate
*1000;
80 sh_audio
->samplerate
=w
->nSamplesPerSec
;
81 hi
->securityLevel
=be2me_32(*((uint32_t *)&buf
[16]));
83 sh_audio
->samplesize
= 4;
84 w
->wBitsPerSample
= 8*sh_audio
->samplesize
;
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
);
105 if(sid
==mmioFOURCC('A','U','T','H'))
107 memcpy(hi
->Auth
,sdata
,FFMIN(BUFSIZ
,slen
));
108 demux_info_add(demuxer
,"Author",sdata
);
111 if(sid
==mmioFOURCC('C','O','M','T'))
113 memcpy(hi
->Comt
,sdata
,FFMIN(BUFSIZ
,slen
));
114 demux_info_add(demuxer
,"Comment",sdata
);
117 if(sid
==mmioFOURCC('(','c',')',' '))
119 memcpy(hi
->Cpyr
,sdata
,FFMIN(BUFSIZ
,slen
));
120 demux_info_add(demuxer
,"Copyright",sdata
);
123 if(sid
==mmioFOURCC('F','I','L','E'))
125 memcpy(hi
->File
,sdata
,FFMIN(BUFSIZ
,slen
));
128 if(sid
==mmioFOURCC('A','L','B','M')) demux_info_add(demuxer
,"Album",sdata
);
130 if(sid
==mmioFOURCC('Y','E','A','R')) demux_info_add(demuxer
,"Date",sdata
);
132 if(sid
==mmioFOURCC('T','R','A','C')) demux_info_add(demuxer
,"Track",sdata
);
134 if(sid
==mmioFOURCC('E','N','C','D')) demux_info_add(demuxer
,"Encoder",sdata
);
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 */
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
);
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
);
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
);
169 if(stream_eof(demuxer
->stream
))
172 dp
= new_demux_packet(l
);
173 ds
->pts
= spos
/ (float)(sh_audio
->wf
->nAvgBytesPerSec
);
176 l
=stream_read(demuxer
->stream
,dp
->buffer
,l
);
177 resize_demux_packet(dp
,l
);
178 ds_add_packet(ds
,dp
);
183 static void demux_seek_vqf(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
185 stream_t
* s
= demuxer
->stream
;
186 sh_audio_t
* sh_audio
= demuxer
->audio
->sh
;
189 base
= (flags
& 1) ? demuxer
->movi_start
: stream_tell(s
);
191 pos
= base
+ ((demuxer
->movi_end
- demuxer
->movi_start
)*rel_seek_secs
);
193 pos
= base
+ (rel_seek_secs
*sh_audio
->i_bps
);
195 pos
-= (pos
% (sh_audio
->channels
* sh_audio
->samplesize
) );
200 static void demux_close_vqf(demuxer_t
* demuxer
) {}
203 demuxer_desc_t demuxer_desc_vqf
= {
208 "ported frm MPlayerXP",
210 1, // safe autodetect
212 demux_vqf_fill_buffer
,