5 #include "libavutil/common.h"
6 #include "libavutil/intreadwrite.h"
9 #include "stream/stream.h"
12 #include "libmpcodecs/vqf.h"
14 static int demux_probe_vqf(demuxer_t
* demuxer
)
16 char buf
[KEYWORD_BYTES
];
19 if(stream_read(s
,buf
,KEYWORD_BYTES
)!=KEYWORD_BYTES
)
21 if(memcmp(buf
,"TWIN",KEYWORD_BYTES
)==0) return DEMUXER_TYPE_VQF
; /*version: 97012000*/
25 static demuxer_t
* demux_open_vqf(demuxer_t
* demuxer
) {
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
));
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;
43 sh_audio
->samplesize
= 2;
44 w
->wBitsPerSample
= 8*sh_audio
->samplesize
;
46 strcpy(hi
->ID
,"TWIN");
47 stream_read(s
,hi
->ID
+KEYWORD_BYTES
,VERSION_BYTES
); /* fourcc+version_id */
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
;
56 if(AV_RL32(chunk_id
)==mmioFOURCC('C','O','M','M'))
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
;
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
){
72 w
->nSamplesPerSec
=44100;
75 w
->nSamplesPerSec
=22050;
78 w
->nSamplesPerSec
=11025;
81 w
->nSamplesPerSec
=hi
->samplingRate
*1000;
84 sh_audio
->samplerate
=w
->nSamplesPerSec
;
85 hi
->securityLevel
= AV_RB32(buf
+ 16);
87 sh_audio
->samplesize
= 4;
88 w
->wBitsPerSample
= 8*sh_audio
->samplesize
;
90 if (subchunk_size
> chunk_size
- 4) continue;
92 while(i
+ 8 < chunk_size
)
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
);
111 if(sid
==mmioFOURCC('A','U','T','H'))
113 memcpy(hi
->Auth
,sdata
,FFMIN(BUFSIZ
,slen
));
114 demux_info_add(demuxer
,"Author",sdata
);
117 if(sid
==mmioFOURCC('C','O','M','T'))
119 memcpy(hi
->Comt
,sdata
,FFMIN(BUFSIZ
,slen
));
120 demux_info_add(demuxer
,"Comment",sdata
);
123 if(sid
==mmioFOURCC('(','c',')',' '))
125 memcpy(hi
->Cpyr
,sdata
,FFMIN(BUFSIZ
,slen
));
126 demux_info_add(demuxer
,"Copyright",sdata
);
129 if(sid
==mmioFOURCC('F','I','L','E'))
131 memcpy(hi
->File
,sdata
,FFMIN(BUFSIZ
,slen
));
134 if(sid
==mmioFOURCC('A','L','B','M')) demux_info_add(demuxer
,"Album",sdata
);
136 if(sid
==mmioFOURCC('Y','E','A','R')) demux_info_add(demuxer
,"Date",sdata
);
138 if(sid
==mmioFOURCC('T','R','A','C')) demux_info_add(demuxer
,"Track",sdata
);
140 if(sid
==mmioFOURCC('E','N','C','D')) demux_info_add(demuxer
,"Encoder",sdata
);
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 */
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
);
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
);
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
);
176 if(stream_eof(demuxer
->stream
))
179 dp
= new_demux_packet(l
);
180 ds
->pts
= spos
/ (float)(sh_audio
->wf
->nAvgBytesPerSec
);
183 l
=stream_read(demuxer
->stream
,dp
->buffer
,l
);
184 resize_demux_packet(dp
,l
);
185 ds_add_packet(ds
,dp
);
190 static void demux_seek_vqf(demuxer_t
*demuxer
,float rel_seek_secs
,float audio_delay
,int flags
){
192 stream_t
* s
= demuxer
->stream
;
193 sh_audio_t
* sh_audio
= demuxer
->audio
->sh
;
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
);
200 pos
= base
+ (rel_seek_secs
*sh_audio
->i_bps
);
202 pos
-= (pos
% (sh_audio
->channels
* sh_audio
->samplesize
) );
207 static void demux_close_vqf(demuxer_t
* demuxer
) {}
210 const demuxer_desc_t demuxer_desc_vqf
= {
215 "ported frm MPlayerXP",
217 1, // safe autodetect
219 demux_vqf_fill_buffer
,