changeset 17992:2545bbd91450

Move global vars used for header parsing, etc to dewux->priv as it should be. Also cleanup a bit signedness.
author albeu
date Wed, 29 Mar 2006 23:25:09 +0000
parents 4d96d2130172
children 98eb966a4024
files libmpdemux/asf.h libmpdemux/asfheader.c libmpdemux/demux_asf.c
diffstat 3 files changed, 79 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/libmpdemux/asf.h	Wed Mar 29 21:54:53 2006 +0000
+++ b/libmpdemux/asf.h	Wed Mar 29 23:25:09 2006 +0000
@@ -212,4 +212,16 @@
 #define le2me_ASF_stream_chunck_t(h) /**/
 #endif
 
+// priv struct for the demuxer
+struct asf_priv {
+    ASF_header_t header;
+    unsigned char* packet;
+    unsigned scrambling_h;
+    unsigned scrambling_w;
+    unsigned scrambling_b;
+    unsigned packetsize;
+    double   packetrate;
+    unsigned movielength;
+};
+
 #endif
--- a/libmpdemux/asfheader.c	Wed Mar 29 21:54:53 2006 +0000
+++ b/libmpdemux/asfheader.c	Wed Mar 29 23:25:09 2006 +0000
@@ -52,17 +52,6 @@
 const char asf_data_chunk_guid[16] = {0x36, 0x26, 0xb2, 0x75,
   0x8e, 0x66, 0xcf, 0x11, 0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c};
 
-static ASF_header_t asfh;
-
-unsigned char* asf_packet=NULL;
-int asf_scrambling_h=1;
-int asf_scrambling_w=1;
-int asf_scrambling_b=1;
-int asf_packetsize=0;
-double asf_packetrate=0;
-int asf_movielength=0;
-
-//int i;
 
 // the variable string is modify in this function
 void pack_asf_string(char* string, int length) {
@@ -122,18 +111,23 @@
 
 int asf_check_header(demuxer_t *demuxer){
   unsigned char asfhdrguid[16]={0x30,0x26,0xB2,0x75,0x8E,0x66,0xCF,0x11,0xA6,0xD9,0x00,0xAA,0x00,0x62,0xCE,0x6C};
-  stream_read(demuxer->stream,(char*) &asfh,sizeof(asfh)); // header obj
-  le2me_ASF_header_t(&asfh);			// swap to machine endian
+  struct asf_priv* asf = calloc(1,sizeof(*asf));
+  asf->scrambling_h=asf->scrambling_w=asf->scrambling_b=1;
+  stream_read(demuxer->stream,(char*) &asf->header,sizeof(asf->header)); // header obj
+  le2me_ASF_header_t(&asf->header);			// swap to machine endian
 //  for(i=0;i<16;i++) printf(" %02X",temp[i]);printf("\n");
 //  for(i=0;i<16;i++) printf(" %02X",asfhdrguid[i]);printf("\n");
-  if(memcmp(asfhdrguid,asfh.objh.guid,16)){
+  if(memcmp(asfhdrguid,asf->header.objh.guid,16)){
     mp_msg(MSGT_HEADER,MSGL_V,"ASF_check: not ASF guid!\n");
+    free(asf);
     return 0; // not ASF guid
   }
-  if(asfh.cno>256){
-    mp_msg(MSGT_HEADER,MSGL_V,"ASF_check: invalid subchunks_no %d\n",(int) asfh.cno);
+  if(asf->header.cno>256){
+    free(asf);
+    mp_msg(MSGT_HEADER,MSGL_V,"ASF_check: invalid subchunks_no %d\n",(int) asf->header.cno);
     return 0; // invalid header???
   }
+  demuxer->priv = asf;
   return DEMUXER_TYPE_ASF;
 }
 
@@ -150,8 +144,8 @@
   return -1;
 }
 
-int read_asf_header(demuxer_t *demuxer){
-  unsigned int hdr_len = asfh.objh.size - sizeof(asfh);
+int read_asf_header(demuxer_t *demuxer,struct asf_priv* asf){
+  int hdr_len = asf->header.objh.size - sizeof(asf->header);
   char *hdr = NULL;
   char guid_buffer[16];
   int pos, start = stream_tell(demuxer->stream);
@@ -163,6 +157,11 @@
   int best_audio = -1;
   uint64_t data_len;
 
+  if(hdr_len < 0) {
+    mp_msg(MSGT_HEADER, MSGL_FATAL, "Header size is too small.\n");
+    return 0;
+  }
+    
   if (hdr_len > 1024 * 1024) {
     mp_msg(MSGT_HEADER, MSGL_FATAL, MSGTR_MPDEMUX_ASFHDR_HeaderSizeOver1MB,
 			hdr_len);
@@ -215,14 +214,14 @@
           buffer = &hdr[pos];
           pos += streamh->stream_size;
           if (pos > hdr_len) goto len_err_out;
-          asf_scrambling_h=buffer[0];
-          asf_scrambling_w=(buffer[2]<<8)|buffer[1];
-          asf_scrambling_b=(buffer[4]<<8)|buffer[3];
-  	  asf_scrambling_w/=asf_scrambling_b;
+          asf->scrambling_h=buffer[0];
+          asf->scrambling_w=(buffer[2]<<8)|buffer[1];
+          asf->scrambling_b=(buffer[4]<<8)|buffer[3];
+  	  asf->scrambling_w/=asf->scrambling_b;
 	} else {
-	  asf_scrambling_b=asf_scrambling_h=asf_scrambling_w=1;
+	  asf->scrambling_b=asf->scrambling_h=asf->scrambling_w=1;
 	}
-	mp_msg(MSGT_HEADER,MSGL_V,"ASF: audio scrambling: %d x %d x %d\n",asf_scrambling_h,asf_scrambling_w,asf_scrambling_b);
+	mp_msg(MSGT_HEADER,MSGL_V,"ASF: audio scrambling: %d x %d x %d\n",asf->scrambling_h,asf->scrambling_w,asf->scrambling_b);
 	//if(demuxer->audio->id==-1) demuxer->audio->id=streamh.stream_no & 0x7F;
         break;
         }
@@ -261,10 +260,10 @@
               (int)fileh->num_packets, (int)fileh->flags, 
               (int)fileh->min_packet_size, (int)fileh->max_packet_size,
               (int)fileh->max_bitrate, (int)fileh->preroll);
-      asf_packetsize=fileh->max_packet_size;
-      asf_packet=malloc(asf_packetsize); // !!!
-      asf_packetrate=fileh->max_bitrate/8.0/(double)asf_packetsize;
-      asf_movielength=fileh->send_duration/10000000LL;
+      asf->packetsize=fileh->max_packet_size;
+      asf->packet=malloc(asf->packetsize); // !!!
+      asf->packetrate=fileh->max_bitrate/8.0/(double)asf->packetsize;
+      asf->movielength=fileh->send_duration/10000000LL;
   }
 
   // find content header
--- a/libmpdemux/demux_asf.c	Wed Mar 29 21:54:53 2006 +0000
+++ b/libmpdemux/demux_asf.c	Wed Mar 29 23:25:09 2006 +0000
@@ -31,31 +31,25 @@
 #endif
 
 // defined at asfheader.c:
-extern unsigned char* asf_packet;
-extern int asf_scrambling_h;
-extern int asf_scrambling_w;
-extern int asf_scrambling_b;
-extern int asf_packetsize;
-extern double asf_packetrate;
-extern int asf_movielength;
+
 extern int asf_check_header(demuxer_t *demuxer);
-extern int read_asf_header(demuxer_t *demuxer);
+extern int read_asf_header(demuxer_t *demuxer,struct asf_priv* asf);
 
 // based on asf file-format doc by Eugene [http://divx.euro.ru]
 
-static void asf_descrambling(unsigned char **src,int len){
+static void asf_descrambling(unsigned char **src,unsigned len, struct asf_priv* asf){
   unsigned char *dst=malloc(len);
   unsigned char *s2=*src;
-  int i=0,x,y;
-  while(len-i>=asf_scrambling_h*asf_scrambling_w*asf_scrambling_b){
+  unsigned i=0,x,y;
+  while(len>=asf->scrambling_h*asf->scrambling_w*asf->scrambling_b+i){
 //    mp_msg(MSGT_DEMUX,MSGL_DBG4,"descrambling! (w=%d  b=%d)\n",w,asf_scrambling_b);
 	//i+=asf_scrambling_h*asf_scrambling_w;
-	for(x=0;x<asf_scrambling_w;x++)
-	  for(y=0;y<asf_scrambling_h;y++){
-	    memcpy(dst+i,s2+(y*asf_scrambling_w+x)*asf_scrambling_b,asf_scrambling_b);
-		i+=asf_scrambling_b;
+	for(x=0;x<asf->scrambling_w;x++)
+	  for(y=0;y<asf->scrambling_h;y++){
+	    memcpy(dst+i,s2+(y*asf->scrambling_w+x)*asf->scrambling_b,asf->scrambling_b);
+		i+=asf->scrambling_b;
 	  }
-	s2+=asf_scrambling_h*asf_scrambling_w*asf_scrambling_b;
+	s2+=asf->scrambling_h*asf->scrambling_w*asf->scrambling_b;
   }
   //if(i<len) memcpy(dst+i,src+i,len-i);
   free(*src);
@@ -71,6 +65,7 @@
 #endif
 
 static int demux_asf_read_packet(demuxer_t *demux,unsigned char *data,int len,int id,int seq,unsigned long time,unsigned short dur,int offs,int keyframe){
+  struct asf_priv* asf = demux->priv;
   demux_stream_t *ds=NULL;
   
   mp_dbg(MSGT_DEMUX,MSGL_DBG4,"demux_asf.read_packet: id=%d seq=%d len=%d\n",id,seq,len);
@@ -105,8 +100,8 @@
       if(ds->asf_seq!=seq){
         // closed segment, finalize packet:
 		if(ds==demux->audio)
-		  if(asf_scrambling_h>1 && asf_scrambling_w>1)
-		    asf_descrambling(&ds->asf_packet->buffer,ds->asf_packet->len);
+		  if(asf->scrambling_h>1 && asf->scrambling_w>1)
+		    asf_descrambling(&ds->asf_packet->buffer,ds->asf_packet->len,asf);
         ds_add_packet(ds,ds->asf_packet);
         ds->asf_packet=NULL;
       } else {
@@ -151,6 +146,7 @@
 //     0 = EOF or no stream found
 //     1 = successfully read a packet
 static int demux_asf_fill_buffer(demuxer_t *demux, demux_stream_t *ds){
+  struct asf_priv* asf = demux->priv;
 
   demux->filepos=stream_tell(demux->stream);
   // Brodcast stream have movi_start==movi_end
@@ -160,17 +156,17 @@
           return 0;
   }
 
-    stream_read(demux->stream,asf_packet,asf_packetsize);
+    stream_read(demux->stream,asf->packet,asf->packetsize);
     if(demux->stream->eof) return 0; // EOF
     
     {
-	    unsigned char* p=asf_packet;
-            unsigned char* p_end=asf_packet+asf_packetsize;
+	    unsigned char* p=asf->packet;
+            unsigned char* p_end=asf->packet+asf->packetsize;
             unsigned char flags=p[0];
             unsigned char segtype=p[1];
-            int padding;
-            int plen;
-	    int sequence;
+            unsigned padding;
+            unsigned plen;
+	    unsigned sequence;
             unsigned long time=0;
             unsigned short duration=0;
 
@@ -180,7 +176,7 @@
             
             if( mp_msg_test(MSGT_DEMUX,MSGL_DBG2) ){
                 int i;
-                for(i=0;i<16;i++) printf(" %02X",asf_packet[i]);
+                for(i=0;i<16;i++) printf(" %02X",asf->packet[i]);
                 printf("\n");
             }
             
@@ -227,10 +223,10 @@
 	    if(((flags>>5)&3)!=0){
               // Explicit (absoulte) packet size
               mp_dbg(MSGT_DEMUX,MSGL_DBG2,"Explicit packet size specified: %d  \n",plen);
-              if(plen>asf_packetsize) mp_msg(MSGT_DEMUX,MSGL_V,"Warning! plen>packetsize! (%d>%d)  \n",plen,asf_packetsize);
+              if(plen>asf->packetsize) mp_msg(MSGT_DEMUX,MSGL_V,"Warning! plen>packetsize! (%d>%d)  \n",plen,asf->packetsize);
 	    } else {
               // Padding (relative) size
-              plen=asf_packetsize-padding;
+              plen=asf->packetsize-padding;
 	    }
 
 	    // Read time & duration:
@@ -244,8 +240,8 @@
               segs=p[0] & 0x3F;
               ++p;
             }
-            mp_dbg(MSGT_DEMUX,MSGL_DBG4,"%08X:  flag=%02X  segs=%d  seq=%d  plen=%d  pad=%d  time=%ld  dur=%d\n",
-              demux->filepos,flags,segs,sequence,plen,padding,time,duration);
+            mp_dbg(MSGT_DEMUX,MSGL_DBG4,"%08"PRIu64":  flag=%02X  segs=%d  seq=%u  plen=%u  pad=%u  time=%ld  dur=%d\n",
+              (uint64_t)demux->filepos,flags,segs,sequence,plen,padding,time,duration);
 
             for(seg=0;seg<segs;seg++){
               //ASF_segmhdr_t* sh;
@@ -320,11 +316,11 @@
 	          case 3: len=LOAD_LE32(p);p+=4;break;	// dword
 	          case 2: len=LOAD_LE16(p);p+=2;break;	// word
 	          case 1: len=p[0];p++;break;		// byte
-	          default: len=plen-(p-asf_packet); // ???
+	          default: len=plen-(p-asf->packet); // ???
 		}
               } else {
                 // single segment
-                len=plen-(p-asf_packet);
+                len=plen-(p-asf->packet);
               }
               if(len<0 || (p+len)>p_end){
                 mp_msg(MSGT_DEMUX,MSGL_V,"ASF_parser: warning! segment len=%d\n",len);
@@ -361,7 +357,7 @@
             return 1; // success
     }
     
-    mp_msg(MSGT_DEMUX,MSGL_V,"%08"PRIX64":  UNKNOWN TYPE  %02X %02X %02X %02X %02X...\n",(int64_t)demux->filepos,asf_packet[0],asf_packet[1],asf_packet[2],asf_packet[3],asf_packet[4]);
+    mp_msg(MSGT_DEMUX,MSGL_V,"%08"PRIX64":  UNKNOWN TYPE  %02X %02X %02X %02X %02X...\n",(int64_t)demux->filepos,asf->packet[0],asf->packet[1],asf->packet[2],asf->packet[3],asf->packet[4]);
     return 0;
 }
 
@@ -370,6 +366,7 @@
 extern void skip_audio_frame(sh_audio_t *sh_audio);
 
 static void demux_seek_asf(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
+    struct asf_priv* asf = demuxer->priv;
     demux_stream_t *d_audio=demuxer->audio;
     demux_stream_t *d_video=demuxer->video;
     sh_audio_t *sh_audio=d_audio->sh;
@@ -379,11 +376,11 @@
   //FIXME: reports good or bad to steve@daviesfam.org please
 
   //================= seek in ASF ==========================
-    float p_rate=asf_packetrate; // packets / sec
+    float p_rate=asf->packetrate; // packets / sec
     off_t rel_seek_packs=(flags&2)?	 // FIXME: int may be enough?
-	(rel_seek_secs*(demuxer->movi_end-demuxer->movi_start)/asf_packetsize):
+	(rel_seek_secs*(demuxer->movi_end-demuxer->movi_start)/asf->packetsize):
 	(rel_seek_secs*p_rate);
-    off_t rel_seek_bytes=rel_seek_packs*asf_packetsize;
+    off_t rel_seek_bytes=rel_seek_packs*asf->packetsize;
     off_t newpos;
     //printf("ASF: packs: %d  duration: %d  \n",(int)fileh.packets,*((int*)&fileh.duration));
 //    printf("ASF_seek: %d secs -> %d packs -> %d bytes  \n",
@@ -421,6 +418,7 @@
 }
 
 static int demux_asf_control(demuxer_t *demuxer,int cmd, void *arg){
+    struct asf_priv* asf = demuxer->priv;
 /*  demux_stream_t *d_audio=demuxer->audio;
     demux_stream_t *d_video=demuxer->video;
     sh_audio_t *sh_audio=d_audio->sh;
@@ -428,7 +426,7 @@
 */
     switch(cmd) {
 	case DEMUXER_CTRL_GET_TIME_LENGTH:
-	    *((double *)arg)=(double)(asf_movielength);
+	    *((double *)arg)=(double)(asf->movielength);
 	    return DEMUXER_CTRL_OK;
 
 	case DEMUXER_CTRL_GET_PERCENT_POS:
@@ -442,12 +440,16 @@
 
 static demuxer_t* demux_open_asf(demuxer_t* demuxer)
 {
+    struct asf_priv* asf = demuxer->priv;
     sh_audio_t *sh_audio=NULL;
     sh_video_t *sh_video=NULL;
 
     //---- ASF header:
-    if (!read_asf_header(demuxer))
+    if(!asf) return NULL;
+    if (!read_asf_header(demuxer,asf)) {
+        free(asf);
         return NULL;
+    }
     stream_reset(demuxer->stream);
     stream_seek(demuxer->stream,demuxer->movi_start);
 //    demuxer->idx_pos=0;
@@ -460,7 +462,7 @@
         } else {
             sh_video=demuxer->video->sh;sh_video->ds=demuxer->video;
             sh_video->fps=1000.0f; sh_video->frametime=0.001f; // 1ms
-            //sh_video->i_bps=10*asf_packetsize; // FIXME!
+            //sh_video->i_bps=10*asf->packetsize; // FIXME!
         }
     }