[MPlayer-cvslog] CVS: main/libmpdemux muxer.c, 1.10, 1.11 muxer.h, 1.15, 1.16 muxer_avi.c, 1.31, 1.32 muxer_lavf.c, 1.12, 1.13 muxer_mpeg.c, 1.22, 1.23 muxer_rawaudio.c, 1.2, 1.3 muxer_rawvideo.c, 1.5, 1.6

Nico Sabbi CVS syncmail at mplayerhq.hu
Mon Nov 21 23:53:16 CET 2005


CVS change done by Nico Sabbi CVS

Update of /cvsroot/mplayer/main/libmpdemux
In directory mail:/var2/tmp/cvs-serv4140/libmpdemux

Modified Files:
	muxer.c muxer.h muxer_avi.c muxer_lavf.c muxer_mpeg.c 
	muxer_rawaudio.c muxer_rawvideo.c 
Log Message:
buffering in the muxer layer; patch by Corey Hickey (bugfood-ml ad fatooh punctum org) plus small fixes by me

Index: muxer.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/muxer.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- muxer.c	18 Nov 2005 14:39:19 -0000	1.10
+++ muxer.c	21 Nov 2005 22:53:14 -0000	1.11
@@ -48,3 +48,105 @@
     }
     return muxer;
 }
+
+/* buffer frames until we either:
+ * (a) have at least one frame from each stream
+ * (b) run out of memory */
+void muxer_write_chunk(muxer_stream_t *s, size_t len, unsigned int flags) {
+    if (s->muxer->muxbuf_skip_buffer) {
+      s->muxer->cont_write_chunk(s, len, flags);
+    }
+    else {
+      int num = s->muxer->muxbuf_num++;
+      muxbuf_t *buf, *tmp;
+      
+      tmp = realloc(s->muxer->muxbuf, (num+1) * sizeof(muxbuf_t));
+      if(!tmp) {
+        mp_msg(MSGT_MUXER, MSGL_FATAL, "Muxer frame buffer cannot reallocate memory!\n");
+        return;
+      }
+      s->muxer->muxbuf = tmp;
+      buf = s->muxer->muxbuf + num;
+      
+      /* buffer this frame */
+      buf->stream = s;
+      buf->timer = s->timer;
+      buf->len = len;
+      buf->flags = flags;
+      buf->buffer = malloc(len * sizeof (unsigned char));
+      if (!buf->buffer) {
+        mp_msg(MSGT_MUXER, MSGL_FATAL, "Muxer frame buffer cannot allocate memory!\n");
+        return;
+      }
+      memcpy(buf->buffer, s->buffer, buf->len);
+      s->muxbuf_seen = 1;
+
+      /* see if we need to keep buffering */
+      s->muxer->muxbuf_skip_buffer = 1;
+      for (num = 0; s->muxer->streams[num]; ++num)
+        if (!s->muxer->streams[num]->muxbuf_seen)
+          s->muxer->muxbuf_skip_buffer = 0;
+      
+      /* see if we can flush buffer now */
+      if (s->muxer->muxbuf_skip_buffer) {
+        muxbuf_t *tmp_buf = malloc(sizeof(muxbuf_t));
+        if (!tmp_buf) {
+          mp_msg(MSGT_MUXER, MSGL_FATAL, "Muxer frame buffer cannot allocate memory!\n");
+          return;
+        }
+        mp_msg(MSGT_MUXER, MSGL_V, "Muxer frame buffer sending %d frame(s) to muxer.\n",
+                        s->muxer->muxbuf_num);
+        
+        /* fix parameters for all streams */
+        for (num = 0; s->muxer->streams[num]; ++num) {
+          muxer_stream_t *str = s->muxer->streams[num];
+          if(str->muxer->fix_stream_parameters)
+            muxer_stream_fix_parameters(str->muxer, str);
+        }
+        
+        /* write header */
+        mp_msg(MSGT_MUXER, MSGL_INFO, MSGTR_WritingAVIHeader);
+        if (s->muxer->cont_write_header)
+          muxer_write_header(s->muxer);
+        
+        /* send all buffered frames to muxer */
+        for (num = 0; num < s->muxer->muxbuf_num; ++num) {
+          buf = s->muxer->muxbuf + num;
+          s = buf->stream;
+          
+          /* 1. save timer and buffer (might have changed by now) */
+          tmp_buf->timer = s->timer;
+          tmp_buf->buffer = s->buffer;
+          
+          /* 2. move stored timer and buffer into stream and mux it */
+          s->timer = buf->timer;
+          s->buffer = buf->buffer;
+          s->muxer->cont_write_chunk(s, buf->len, buf->flags);
+          
+          /* 3. restore saved timer and buffer */
+          s->timer = tmp_buf->timer;
+          s->buffer = tmp_buf->buffer;
+        }
+        free(tmp_buf);
+        
+        free(s->muxer->muxbuf);
+        s->muxer->muxbuf_num = 0;
+      }
+    }
+    
+    /* this code moved directly from muxer_avi.c */
+    // alter counters:
+    if(s->h.dwSampleSize){
+      // CBR
+      s->h.dwLength+=len/s->h.dwSampleSize;
+      if(len%s->h.dwSampleSize) mp_msg(MSGT_MUXER, MSGL_WARN, "Warning! len isn't divisable by samplesize!\n");
+    } else {
+      // VBR
+      s->h.dwLength++;
+    }
+    s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
+    s->size+=len;
+    
+    return;
+}
+

Index: muxer.h
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/muxer.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- muxer.h	19 Jun 2005 09:12:43 -0000	1.15
+++ muxer.h	21 Nov 2005 22:53:14 -0000	1.16
@@ -28,6 +28,8 @@
   unsigned int b_buffer_size;	//size of b_buffer
   unsigned int b_buffer_ptr;	//index to next data to write
   unsigned int b_buffer_len;	//len of next data to write
+  // muxer frame buffer:
+  unsigned int muxbuf_seen;
   // source stream:
   void* source; // sh_audio or sh_video
   int codec; // codec used for encoding. 0 means copy
@@ -37,7 +39,6 @@
   WAVEFORMATEX *wf;
   BITMAPINFOHEADER *bih;   // in format
   // mpeg specific:
-  unsigned int gop_start; // frame number of this GOP start
   size_t ipb[3]; // sizes of I/P/B frames
   // muxer of that stream
   struct muxer_t *muxer;
@@ -66,6 +67,11 @@
   //int num_streams;
   muxer_stream_t* def_v;  // default video stream (for general headers)
   muxer_stream_t* streams[MUXER_MAX_STREAMS];
+  // muxer frame buffer:
+  struct muxbuf_t * muxbuf;
+  int muxbuf_num;
+  int muxbuf_skip_buffer;
+  // functions:
   void (*fix_stream_parameters)(muxer_stream_t *);
   void (*cont_write_chunk)(muxer_stream_t *,size_t,unsigned int);
   void (*cont_write_header)(struct muxer_t *);
@@ -75,10 +81,20 @@
   void *priv;
 } muxer_t;
 
+/* muxer frame buffer */
+typedef struct muxbuf_t {
+  muxer_stream_t *stream; /* pointer back to corresponding stream */
+  double timer; /* snapshot of stream timer */
+  unsigned char *buffer;
+  size_t len;
+  unsigned int flags;
+} muxbuf_t;
+
+
 muxer_t *muxer_new_muxer(int type,FILE *);
 #define muxer_new_stream(muxer,a) muxer->cont_new_stream(muxer,a)
 #define muxer_stream_fix_parameters(muxer, a) muxer->fix_stream_parameters(a)
-#define muxer_write_chunk(a,b,c) a->muxer->cont_write_chunk(a,b,c)
+void muxer_write_chunk(muxer_stream_t *s, size_t len, unsigned int flags);
 #define muxer_write_header(muxer) muxer->cont_write_header(muxer)
 #define muxer_write_index(muxer) muxer->cont_write_index(muxer)
 

Index: muxer_avi.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/muxer_avi.c,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -r1.31 -r1.32
--- muxer_avi.c	18 Nov 2005 14:39:19 -0000	1.31
+++ muxer_avi.c	21 Nov 2005 22:53:14 -0000	1.32
@@ -241,20 +241,9 @@
     // write out the chunk:
     write_avi_chunk(muxer->file,s->ckid,len,s->buffer); /* unsigned char */
 
-    // alter counters:
     if (len > s->h.dwSuggestedBufferSize){
 	s->h.dwSuggestedBufferSize = len;
     }
-    if(s->h.dwSampleSize){
-	// CBR
-	s->h.dwLength+=len/s->h.dwSampleSize;
-	if(len%s->h.dwSampleSize) mp_msg(MSGT_MUXER, MSGL_WARN, "Warning! len isn't divisable by samplesize!\n");
-    } else {
-	// VBR
-	s->h.dwLength++;
-    }
-    s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
-    s->size+=len;
     if((unsigned int)len>s->h.dwSuggestedBufferSize) s->h.dwSuggestedBufferSize=len;
 
     muxer->file_end += 8 + paddedlen;

Index: muxer_lavf.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/muxer_lavf.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- muxer_lavf.c	18 Nov 2005 14:39:19 -0000	1.12
+++ muxer_lavf.c	21 Nov 2005 22:53:14 -0000	1.13
@@ -103,6 +103,7 @@
 		mp_msg(MSGT_MUXER, MSGL_ERR, "Could not alloc muxer_stream, EXIT\n");
 		return NULL;
 	}
+	muxer->streams[muxer->avih.dwStreams] = stream;
 	stream->b_buffer = (unsigned char *)malloc(2048);
 	if(!stream->b_buffer)
 	{
@@ -230,8 +231,6 @@
 	muxer_stream_priv_t *spriv = (muxer_stream_priv_t *) stream->priv;
 	AVPacket pkt;
 	
-	stream->size += len;
-	
 	if(len)
 	{
 	av_init_packet(&pkt);
@@ -259,12 +258,6 @@
 	}
 	}
 	
-	if(stream->h.dwSampleSize) 	// CBR
-		stream->h.dwLength += len / stream->h.dwSampleSize;
-	else				// VBR
-		stream->h.dwLength++;
-	
-	stream->timer = (double) stream->h.dwLength * stream->h.dwScale / stream->h.dwRate;
 	return;
 }
 

Index: muxer_mpeg.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/muxer_mpeg.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -r1.22 -r1.23
--- muxer_mpeg.c	18 Nov 2005 14:39:19 -0000	1.22
+++ muxer_mpeg.c	21 Nov 2005 22:53:14 -0000	1.23
@@ -1991,7 +1991,6 @@
 
 		if(s->buffer[3])
 		{	// Sequence or GOP -- scan for Picture
-			s->gop_start = s->h.dwLength;
 			while (ptr < len-5 && 
 				(s->buffer[ptr] != 0 || s->buffer[ptr+1] != 0 || s->buffer[ptr+2] != 1 || s->buffer[ptr+3] != 0)) 
 				ptr++;
@@ -2454,10 +2453,8 @@
     priv->vbytes += len;
     
     sz <<= 1;
-    s->h.dwLength++;
-    s->size += len;
-    s->timer = (double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
   } else { // MUXER_TYPE_AUDIO
+  	double fake_timer;
   	spriv->type = 0;
 	stream_format = s->wf->wFormatTag;
 	
@@ -2522,9 +2519,7 @@
 		}
 	}
 	
-	parse_audio(s, 0, &nf, &(s->timer));
-	s->h.dwLength += nf;
-	s->size += len;
+	parse_audio(s, 0, &nf, &fake_timer);
 	sz = max(len, 2 * priv->packet_size);
   }
   
@@ -2557,12 +2552,13 @@
 static void mpegfile_write_index(muxer_t *muxer)
 {
 	int i, nf;
+	double fake_timer;
 	muxer_priv_t *priv = (muxer_priv_t *) muxer->priv;
 
 	for(i = 0; i < muxer->avih.dwStreams; i++)
 	{
 		if(muxer->streams[i]->type == MUXER_TYPE_AUDIO)
-			parse_audio(muxer->streams[i], 1, &nf, &(muxer->streams[i]->timer));
+			parse_audio(muxer->streams[i], 1, &nf, &fake_timer);
 	}
 	while(flush_buffers(muxer, 0) > 0);
 	flush_buffers(muxer, 1);

Index: muxer_rawaudio.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/muxer_rawaudio.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- muxer_rawaudio.c	20 Jun 2005 00:30:18 -0000	1.2
+++ muxer_rawaudio.c	21 Nov 2005 22:53:14 -0000	1.3
@@ -51,19 +51,6 @@
     // write out the chunk:
     if (s->type==MUXER_TYPE_AUDIO)
         fwrite(s->buffer,len,1,muxer->file);
-
-    // alter counters:
-    if(s->h.dwSampleSize){
-        // CBR
-        s->h.dwLength+=len/s->h.dwSampleSize;
-        if(len%s->h.dwSampleSize)
-            mp_msg(MSGT_MUXER,MSGL_WARN,MSGTR_WarningLenIsntDivisible);
-    } else {
-        // VBR
-        s->h.dwLength++;
-    }
-    s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
-    s->size+=len;
 }
 
 static void rawaudiofile_write_header(muxer_t *muxer){

Index: muxer_rawvideo.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/muxer_rawvideo.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- muxer_rawvideo.c	18 Nov 2005 14:39:19 -0000	1.5
+++ muxer_rawvideo.c	21 Nov 2005 22:53:14 -0000	1.6
@@ -57,17 +57,6 @@
     if (s->type == MUXER_TYPE_VIDEO)
     write_rawvideo_chunk(muxer->file,len,s->buffer); /* unsigned char */
 
-    // alter counters:
-    if(s->h.dwSampleSize){
-	// CBR
-	s->h.dwLength+=len/s->h.dwSampleSize;
-	if(len%s->h.dwSampleSize) printf("Warning! len isn't divisable by samplesize!\n");
-    } else {
-	// VBR
-	s->h.dwLength++;
-    }
-    s->timer=(double)s->h.dwLength*s->h.dwScale/s->h.dwRate;
-    s->size+=len;
     // if((unsigned int)len>s->h.dwSuggestedBufferSize) s->h.dwSuggestedBufferSize=len;
 
 }




More information about the MPlayer-cvslog mailing list