whitespace cosmetics: Remove all trailing whitespace.
[mplayer.git] / libmpdemux / demux_vqf.c
index a4ab0f6..4ec8865 100644 (file)
@@ -1,17 +1,35 @@
+/*
+ * This file is part of MPlayer.
+ *
+ * MPlayer is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * MPlayer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with MPlayer; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
 #include "config.h"
 
 #include <stdlib.h>
 #include <stdio.h>
-#include "bswap.h"
+#include "libavutil/common.h"
+#include "libavutil/intreadwrite.h"
+#include "mpbswap.h"
 
-#include "stream.h"
+#include "stream/stream.h"
 #include "demuxer.h"
 #include "stheader.h"
-#include "../libmpcodecs/vqf.h"
-
-extern void resync_audio_stream(sh_audio_t *sh_audio);
+#include "libmpcodecs/vqf.h"
 
-static int demux_probe_vqf(demuxer_t* demuxer) 
+static int demux_probe_vqf(demuxer_t* demuxer)
 {
   char buf[KEYWORD_BYTES];
   stream_t *s;
@@ -31,7 +49,7 @@ static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
   s = demuxer->stream;
 
   sh_audio = new_sh_audio(demuxer,0);
-  sh_audio->wf = w = (WAVEFORMATEX*)malloc(sizeof(WAVEFORMATEX)+sizeof(headerInfo));
+  sh_audio->wf = w = malloc(sizeof(WAVEFORMATEX)+sizeof(headerInfo));
   hi = (headerInfo *)&w[1];
   memset(hi,0,sizeof(headerInfo));
   w->wFormatTag = 0x1;
@@ -51,19 +69,22 @@ static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
     unsigned chunk_size;
     hi->size=chunk_size=stream_read_dword(s); /* include itself */
     stream_read(s,chunk_id,4);
-    if(*((uint32_t *)&chunk_id[0])==mmioFOURCC('C','O','M','M'))
+    if (chunk_size < 8) return NULL;
+    chunk_size -= 8;
+    if(AV_RL32(chunk_id)==mmioFOURCC('C','O','M','M'))
     {
-    char buf[chunk_size-8];
+    char buf[BUFSIZ];
     unsigned i,subchunk_size;
-    if(stream_read(s,buf,chunk_size-8)!=chunk_size-8) return NULL;
+    if (chunk_size > sizeof(buf) || chunk_size < 20) return NULL;
+    if(stream_read(s,buf,chunk_size)!=chunk_size) return NULL;
     i=0;
-    subchunk_size=be2me_32(*((uint32_t *)&buf[0]));
-    hi->channelMode=be2me_32(*((uint32_t *)&buf[4]));
+    subchunk_size      = AV_RB32(buf);
+    hi->channelMode    = AV_RB32(buf + 4);
     w->nChannels=sh_audio->channels=hi->channelMode+1; /*0-mono;1-stereo*/
-    hi->bitRate=be2me_32(*((uint32_t *)&buf[8]));
+    hi->bitRate        = AV_RB32(buf + 8);
     sh_audio->i_bps=hi->bitRate*1000/8; /* bitrate kbit/s */
     w->nAvgBytesPerSec = sh_audio->i_bps;
-    hi->samplingRate=be2me_32(*((uint32_t *)&buf[12]));
+    hi->samplingRate   = AV_RB32(buf + 12);
     switch(hi->samplingRate){
     case 44:
         w->nSamplesPerSec=44100;
@@ -79,51 +100,53 @@ static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
         break;
     }
     sh_audio->samplerate=w->nSamplesPerSec;
-    hi->securityLevel=be2me_32(*((uint32_t *)&buf[16]));
+    hi->securityLevel  = AV_RB32(buf + 16);
     w->nBlockAlign = 0;
     sh_audio->samplesize = 4;
     w->wBitsPerSample = 8*sh_audio->samplesize;
     w->cbSize = 0;
+    if (subchunk_size > chunk_size - 4) continue;
     i+=subchunk_size+4;
-    while(i<chunk_size-8)
+    while(i + 8 < chunk_size)
     {
         unsigned slen,sid;
-        char sdata[chunk_size];
-        sid=*((uint32_t *)&buf[i]); i+=4;
-        slen=be2me_32(*((uint32_t *)&buf[i])); i+=4;
+        char sdata[BUFSIZ];
+        sid  = AV_RL32(buf + i); i+=4;
+        slen = AV_RB32(buf + i); i+=4;
+        if (slen > sizeof(sdata) - 1 || slen > chunk_size - i) break;
         if(sid==mmioFOURCC('D','S','I','Z'))
         {
-        hi->Dsiz=be2me_32(*((uint32_t *)&buf[i]));
+        hi->Dsiz=AV_RB32(buf + i);
         continue; /* describes the same info as size of DATA chunk */
         }
         memcpy(sdata,&buf[i],slen); sdata[slen]=0; i+=slen;
         if(sid==mmioFOURCC('N','A','M','E'))
         {
-        memcpy(hi->Name,sdata,min(BUFSIZ,slen));
+        memcpy(hi->Name,sdata,FFMIN(BUFSIZ,slen));
         demux_info_add(demuxer,"Title",sdata);
         }
         else
         if(sid==mmioFOURCC('A','U','T','H'))
         {
-        memcpy(hi->Auth,sdata,min(BUFSIZ,slen));
+        memcpy(hi->Auth,sdata,FFMIN(BUFSIZ,slen));
         demux_info_add(demuxer,"Author",sdata);
         }
         else
         if(sid==mmioFOURCC('C','O','M','T'))
         {
-        memcpy(hi->Comt,sdata,min(BUFSIZ,slen));
+        memcpy(hi->Comt,sdata,FFMIN(BUFSIZ,slen));
         demux_info_add(demuxer,"Comment",sdata);
         }
         else
         if(sid==mmioFOURCC('(','c',')',' '))
         {
-        memcpy(hi->Cpyr,sdata,min(BUFSIZ,slen));
+        memcpy(hi->Cpyr,sdata,FFMIN(BUFSIZ,slen));
         demux_info_add(demuxer,"Copyright",sdata);
         }
         else
         if(sid==mmioFOURCC('F','I','L','E'))
         {
-        memcpy(hi->File,sdata,min(BUFSIZ,slen));
+        memcpy(hi->File,sdata,FFMIN(BUFSIZ,slen));
         }
         else
         if(sid==mmioFOURCC('A','L','B','M')) demux_info_add(demuxer,"Album",sdata);
@@ -139,21 +162,22 @@ static demuxer_t* demux_open_vqf(demuxer_t* demuxer) {
     }
     }
     else
-    if(*((uint32_t *)&chunk_id[0])==mmioFOURCC('D','A','T','A'))
+    if(AV_RL32(chunk_id)==mmioFOURCC('D','A','T','A'))
     {
     demuxer->movi_start=stream_tell(s);
-    demuxer->movi_end=demuxer->movi_start+chunk_size-8;
-    mp_msg(MSGT_DEMUX, MSGL_V, "Found data at %llX size %llu\n",demuxer->movi_start,demuxer->movi_end);
+    demuxer->movi_end=demuxer->movi_start+chunk_size;
+    mp_msg(MSGT_DEMUX, MSGL_V, "Found data at %"PRIX64" size %"PRIu64"\n",demuxer->movi_start,demuxer->movi_end);
     /* Done! play it */
     break;
     }
     else
     {
-    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);
-    stream_skip(s,chunk_size-8); /*unknown chunk type */
+    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);
+    stream_skip(s,chunk_size); /*unknown chunk type */
     }
   }
 
+  demuxer->audio->id = 0;
   demuxer->audio->sh = sh_audio;
   sh_audio->ds = demuxer->audio;
   stream_seek(s,demuxer->movi_start);
@@ -181,28 +205,27 @@ static int demux_vqf_fill_buffer(demuxer_t* demuxer, demux_stream_t *ds) {
   return 1;
 }
 
-static void demux_seek_vqf(demuxer_t *demuxer,float rel_seek_secs,int flags){
+static void demux_seek_vqf(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
 #if 0
   stream_t* s = demuxer->stream;
   sh_audio_t* sh_audio = demuxer->audio->sh;
   off_t base,pos;
 
-  base = (flags & 1) ? demuxer->movi_start : stream_tell(s);
-  if(flags & 2)
+  base = (flags & SEEK_ABSOLUTE) ? demuxer->movi_start : stream_tell(s);
+  if(flags & SEEK_FACTOR)
     pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
   else
     pos = base + (rel_seek_secs*sh_audio->i_bps);
 
   pos -= (pos % (sh_audio->channels * sh_audio->samplesize) );
   stream_seek(s,pos);
-  resync_audio_stream(sh_audio);
 #endif
 }
 
 static void demux_close_vqf(demuxer_t* demuxer) {}
 
 
-demuxer_desc_t demuxer_desc_vqf = {
+const demuxer_desc_t demuxer_desc_vqf = {
   "TwinVQ demuxer",
   "vqf",
   "VQF",