[xiph-commits] r14459 - trunk/vorbis-tools/vorbiscomment

ivo at svn.xiph.org ivo at svn.xiph.org
Wed Feb 6 14:31:36 PST 2008


Author: ivo
Date: 2008-02-06 14:31:35 -0800 (Wed, 06 Feb 2008)
New Revision: 14459

Added:
   trunk/vorbis-tools/vorbiscomment/vceditaux.h
Modified:
   trunk/vorbis-tools/vorbiscomment/Makefile.am
   trunk/vorbis-tools/vorbiscomment/vcedit.c
   trunk/vorbis-tools/vorbiscomment/vcedit.h
Log:
Large patch by Ian Malone to support multiplexed Vorbis.

Modified: trunk/vorbis-tools/vorbiscomment/Makefile.am
===================================================================
--- trunk/vorbis-tools/vorbiscomment/Makefile.am	2008-02-05 07:59:02 UTC (rev 14458)
+++ trunk/vorbis-tools/vorbiscomment/Makefile.am	2008-02-06 22:31:35 UTC (rev 14459)
@@ -1,7 +1,7 @@
 ## Process this file with automake to produce Makefile.in
 
 mans = vorbiscomment.1
-vorbiscommentsources = vcedit.c vcedit.h vcomment.c
+vorbiscommentsources = vcedit.c vcedit.h vcomment.c vceditaux.h
 
 datadir = @datadir@
 localedir = $(datadir)/locale

Modified: trunk/vorbis-tools/vorbiscomment/vcedit.c
===================================================================
--- trunk/vorbis-tools/vorbiscomment/vcedit.c	2008-02-05 07:59:02 UTC (rev 14458)
+++ trunk/vorbis-tools/vorbiscomment/vcedit.c	2008-02-06 22:31:35 UTC (rev 14459)
@@ -9,6 +9,25 @@
  * last modified: $Id: vcedit.c,v 1.23 2003/09/03 07:58:05 calc Exp $
  */
 
+/* Handle muxed streams and the Vorbis renormalization without having
+ * to understand remuxing:
+ * Linked list of buffers (buffer_chain).  Start a link and whenever
+ * you encounter an unknown page from the current stream (ie we found
+ * its bos in the bos section) push it onto the current buffer.  Whenever
+ * you encounter the stream being renormalized create a new link in the
+ * chain.
+ * On writing, write the contents of the first link before every Vorbis
+ * page written, and move to the next link.  Assuming the Vorbis pages
+ * in match vorbis pages out, the order of pages from different logical
+ * streams will be unchanged.
+ * Special case: header. After writing the vorbis headers, and before
+ * starting renormalization, flush accumulated links (takes care of
+ * situations where number of secondary vorbis header pages changes due
+ * to remuxing.  Similarly flush links at the end of renormalization
+ * and before the start of the next chain is written.
+ *
+ */
+
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
@@ -21,47 +40,276 @@
 #include <vorbis/codec.h>
 
 #include "vcedit.h"
+#include "vceditaux.h"
 #include "i18n.h"
 
 
 #define CHUNKSIZE 4096
+#define BUFFERCHUNK CHUNKSIZE
 
-vcedit_state *vcedit_new_state(void)
-{
+/* Helper function, shouldn't need to call directly */
+static int page_buffer_push(vcedit_buffer_chain *bufferlink, ogg_page *og) {
+	int result=0;
+	char *tmp;
+	vcedit_page_buffer *buffer;
+	
+	buffer = &bufferlink->buffer;
+	tmp = realloc(buffer->data,
+		      buffer->data_len + og->header_len + og->body_len);
+	if(tmp) {
+		buffer->data = tmp;
+		memcpy(buffer->data + buffer->data_len, og->header,
+		       og->header_len);
+		buffer->data_len += og->header_len;
+		memcpy(buffer->data + buffer->data_len, og->body,
+	       og->body_len);
+		result = 1;
+		buffer->data_len += og->body_len;
+	} else {
+		result = -1;
+	}
+	
+	return result;
+}
+
+/* Write and free the first link using callbacks */
+static int buffer_chain_writelink(vcedit_state *state, void *out) {
+	int result = 0;
+	vcedit_buffer_chain *tmpchain;
+	vcedit_page_buffer *tmpbuffer;
+
+	tmpchain = state->sidebuf;
+	tmpbuffer = &tmpchain->buffer;
+	if(tmpbuffer->data_len)
+	{
+		if(state->write(tmpbuffer->data,1,tmpbuffer->data_len, out) !=
+		   (size_t) tmpbuffer->data_len)
+			result = -1;
+		else 
+			result = 1;
+	}
+
+	free(tmpbuffer->data);
+	state->sidebuf = tmpchain->next;
+	free(tmpchain);
+	return result;
+}
+
+
+static int buffer_chain_newlink(vcedit_state *state) {
+	int result = 1;
+	vcedit_buffer_chain *bufferlink;
+	
+	if(!state->sidebuf) {
+		state->sidebuf = malloc (sizeof *state->sidebuf);
+		if(state->sidebuf) {
+			bufferlink = state->sidebuf;
+		} else {
+			result = -1;
+		}
+	} else {
+		bufferlink=state->sidebuf;
+		while(bufferlink->next) {
+			bufferlink = bufferlink->next;
+		}
+		bufferlink->next =  malloc (sizeof *bufferlink->next);
+		if(bufferlink->next) {
+			bufferlink = bufferlink->next;
+		} else {
+			result = -1;
+		}
+	}
+
+	if(result > 0 ) {
+		bufferlink->next = 0;
+		bufferlink->buffer.data = 0;
+		bufferlink->buffer.data_len = 0;
+	}
+	else 
+		state->lasterror =
+			_("Couldn't get enough memory for input buffering.");
+
+  return result;
+}
+
+
+/* Push page onto the end of the buffer chain */
+static int buffer_chain_push(vcedit_state *state, ogg_page *og) {
+	/* If there is no sidebuffer yet we need to create one, otherwise
+	 * traverse to the last buffer and push the new page onto it. */
+	int result=1;
+	vcedit_buffer_chain *bufferlink;
+	if(!state->sidebuf) {
+		result = buffer_chain_newlink(state);
+	}
+	
+	if(result > 0) {
+		bufferlink = state->sidebuf;
+		while(bufferlink->next) {
+			bufferlink = bufferlink->next;
+	}
+		result = page_buffer_push(bufferlink, og);
+	}
+
+	if(result < 0)
+		state->lasterror =
+			_("Couldn't get enough memory for input buffering.");
+
+	return result;
+}
+
+
+
+static int vcedit_supported_stream(vcedit_state *state, ogg_page *og) {
+	ogg_stream_state os;
+	vorbis_info vi;
+	vorbis_comment vc;
+	ogg_packet header;
+	int result = 0;
+	
+	ogg_stream_init(&os, ogg_page_serialno(og));
+	vorbis_info_init(&vi);
+	vorbis_comment_init(&vc);
+	
+	if( !ogg_page_bos(og) )
+                result = -1;
+
+	if(result >= 0 && ogg_stream_pagein(&os, og) < 0)
+	{
+		state->lasterror =
+			_("Error reading first page of Ogg bitstream.");
+		result = -1;
+	}
+
+	if(result >= 0 && ogg_stream_packetout(&os, &header) != 1)
+	{
+		state->lasterror = _("Error reading initial header packet.");
+		result = -1;
+	}
+
+	if(result >= 0 && vorbis_synthesis_headerin(&vi, &vc, &header) >= 0)
+	{
+                result = 1;
+	} else {
+		/* Not vorbis, may eventually become a chain of checks (Speex,
+		 * Theora), but for the moment return 0, bos scan will push
+                 * the current page onto the buffer.
+		 */
+	}
+
+	ogg_stream_clear(&os);
+	vorbis_info_clear(&vi);
+	vorbis_comment_clear(&vc);
+        return result;
+}
+
+
+static int vcedit_contains_serial (vcedit_state *state, int serialno) {
+	int result = 0;
+	size_t count;
+	for( count=0; count < state->serials.streams_len; count++ ) {
+		if ( *(state->serials.streams + count ) == serialno )
+			result = 1;
+	}
+	
+	return result;
+}
+
+
+static int vcedit_add_serial (vcedit_state *state, long serial) {
+        int result = 0;
+        long *tmp;
+	
+	
+	if( vcedit_contains_serial(state, serial) )
+        {
+		result = 1;
+	} else {
+		tmp   = realloc(state->serials.streams,
+				(state->serials.streams_len + 1) * sizeof *tmp);
+		if(tmp) {
+			state->serials.streams = tmp;
+			*(state->serials.streams +
+			  state->serials.streams_len) = serial;
+			state->serials.streams_len += 1;
+			result = 1;
+		} else {
+			state->lasterror =
+				_("Couldn't get enough memory to register new stream serial number.");
+		        result = -1;
+		}
+	} 
+	return result;
+}
+
+
+/* For the benefit of the secondary header read only.  Quietly creates
+ * newlinks and pushes pages onto the buffer in the right way */
+static int vcedit_target_pageout (vcedit_state *state, ogg_page *og) {
+	int result = 0;
+	int pageout_result;
+	pageout_result = ogg_sync_pageout(state->oy, og);
+	if(pageout_result > 0)
+	{
+		if(state->serial == ogg_page_serialno(og))
+			result = buffer_chain_newlink(state);
+		else
+			result = buffer_chain_push(state, og);
+	} else if (pageout_result < 0) {
+		/* Vorbis comment traditionally ignores the not-synced
+		 * error from pageout, so give it a different code. */
+		result = -2;
+	}
+	return result;
+}
+
+
+/* (I'm paranoid about memset(x,0,len) not giving null pointers */
+vcedit_state *vcedit_new_state(void) {
 	vcedit_state *state = malloc(sizeof(vcedit_state));
-	memset(state, 0, sizeof(vcedit_state));
-
+	if(state) {
+		memset(state, 0, sizeof(vcedit_state));
+		state->sidebuf = 0;
+		state->serials.streams = 0;
+		state->serials.streams_len = 0;
+	}
 	return state;
 }
 
-char *vcedit_error(vcedit_state *state)
-{
+char *vcedit_error(vcedit_state *state) {
 	return state->lasterror;
 }
 
-vorbis_comment *vcedit_comments(vcedit_state *state)
-{
+vorbis_comment *vcedit_comments(vcedit_state *state) {
 	return state->vc;
 }
 
-static void vcedit_clear_internals(vcedit_state *state)
-{
+static void vcedit_clear_internals(vcedit_state *state) {
     char *tmp;
-	if(state->vc)
-	{
+	if(state->vc) {
 		vorbis_comment_clear(state->vc);
 		free(state->vc);
 	}
-	if(state->os)
-	{
+	if(state->os) {
 		ogg_stream_clear(state->os);
 		free(state->os);
 	}
-	if(state->oy)
-	{
+	if(state->oy) {
 		ogg_sync_clear(state->oy);
 		free(state->oy);
 	}
+	if(state->serials.streams_len) {
+		free(state->serials.streams);
+		state->serials.streams_len = 0;
+		state->serials.streams = 0;
+	}
+	while(state->sidebuf) {
+		vcedit_buffer_chain *tmpbuffer;
+		tmpbuffer = state->sidebuf;
+		state->sidebuf = tmpbuffer->next;
+		free(tmpbuffer->buffer.data);
+		free(tmpbuffer);
+	}
 	if(state->vendor)
 		free(state->vendor);
     if(state->mainbuf)
@@ -160,34 +408,57 @@
 	int result;
 	char *buffer;
 	int bytes;
+	int serialno;
 
 	result = ogg_stream_packetout(s->os, p);
 
 	if(result > 0)
 		return 1;
-	else
-	{
-		if(s->eosin)
-			return 0;
-		while(ogg_sync_pageout(s->oy, page) <= 0)
-		{
-			buffer = ogg_sync_buffer(s->oy, CHUNKSIZE);
-			bytes = s->read(buffer,1, CHUNKSIZE, s->in);
-			ogg_sync_wrote(s->oy, bytes);
-			if(bytes == 0) 
+	else {
+		while(1) {
+			if(s->eosin)
 				return 0;
+
+			while(ogg_sync_pageout(s->oy, page) <= 0)
+			{
+				buffer = ogg_sync_buffer(s->oy, CHUNKSIZE);
+				bytes = s->read(buffer,1, CHUNKSIZE, s->in);
+				ogg_sync_wrote(s->oy, bytes);
+				if(bytes == 0)
+				return 0;
+			}
+
+			serialno = ogg_page_serialno(page);
+			if(ogg_page_serialno(page) != s->serial)
+			{
+				if(vcedit_contains_serial(s, serialno)) {
+					result = buffer_chain_push(s, page);
+					if(result < 0)
+						return result;
+				}
+				else
+				{
+					s->eosin = 1;
+					s->extrapage = 1;
+					return 0;
+				}
+			} 
+			else
+			{
+			  ogg_stream_pagein(s->os, page);
+			  result = buffer_chain_newlink(s);
+			  if (result < 0)
+				  return result;
+
+			  if(ogg_page_eos(page))
+				s->eosin = 1;
+			}
+			result = ogg_stream_packetout(s->os, p);
+			if(result > 0)
+				return 1;
 		}
-		if(ogg_page_eos(page))
-			s->eosin = 1;
-		else if(ogg_page_serialno(page) != s->serial)
-		{
-			s->eosin = 1;
-			s->extrapage = 1;
-			return 0;
-		}
-
-		ogg_stream_pagein(s->os, page);
-		return _fetch_next_packet(s, p, page);
+		/* Here == trouble */
+		return 0;
 	}
 }
 
@@ -203,7 +474,9 @@
 
 	char *buffer;
 	int bytes,i;
-    int chunks = 0;
+	int chunks = 0;
+	int read_bos, test_supported, page_pending;
+	int have_vorbis;
 	ogg_packet *header;
 	ogg_packet	header_main;
 	ogg_packet  header_comments;
@@ -233,49 +506,127 @@
 			    state->lasterror = _("Input truncated or empty.");
     		else
 	    		state->lasterror = _("Input is not an Ogg bitstream.");
-		    goto err;
-    	}
+	    goto err;
+	}
     }
 
-	state->serial = ogg_page_serialno(&og);
+    /* BOS loop, starting with a loaded ogg page. */
+    if(buffer_chain_newlink(state) < 0)
+	    goto err;
 
-	state->os = malloc(sizeof(ogg_stream_state));
-	ogg_stream_init(state->os, state->serial);
+    for( read_bos = 1, have_vorbis = 0 ; read_bos; )
+    {
+	    test_supported = vcedit_supported_stream(state, &og);
+	    if(test_supported < 0)
+	    {
+		    goto err;
+	    }
+	    else if (test_supported == 0 || have_vorbis )
+	    {
+		    if(vcedit_add_serial ( state, ogg_page_serialno(&og)) < 0)
+			    goto err;
+		    if( buffer_chain_push(state, &og) < 0)
+			    goto err;
+	    }
+	    else if (test_supported > 0)
+	    {
+		    if(buffer_chain_newlink(state) < 0)
+			    goto err;
+		    state->serial = ogg_page_serialno(&og);
+		    if(vcedit_add_serial ( state, ogg_page_serialno(&og)) < 0)
+		       goto err;
+ 
+		    state->os = malloc(sizeof(ogg_stream_state));
+		    ogg_stream_init(state->os, state->serial);
 
-    state->vi = malloc(sizeof(vorbis_info));
-	vorbis_info_init(state->vi);
+		    state->vi = malloc(sizeof(vorbis_info));
+		    vorbis_info_init(state->vi);
 
-	state->vc = malloc(sizeof(vorbis_comment));
-	vorbis_comment_init(state->vc);
+		    state->vc = malloc(sizeof(vorbis_comment));
+		    vorbis_comment_init(state->vc);
 
-	if(ogg_stream_pagein(state->os, &og) < 0)
-	{
-		state->lasterror = _("Error reading first page of Ogg bitstream.");
+		    if(ogg_stream_pagein(state->os, &og) < 0)
+		    {
+			    state->lasterror = 
+				    _("Error reading first page of Ogg bitstream.");
+			    goto err;
+		    }
+
+		    if(ogg_stream_packetout(state->os, &header_main) != 1)
+		    {
+			    state->lasterror =
+				    _("Error reading initial header packet.");
+			    goto err;
+		    }
+
+		    if(vorbis_synthesis_headerin(state->vi, state->vc,
+						 &header_main) < 0)
+		    {
+			    state->lasterror =
+				    _("Ogg bitstream does not contain vorbis data.");
+			    goto err;
+		    }
+		    have_vorbis = 1;
+	    }
+	    while(1)
+	    {
+		    buffer = ogg_sync_buffer(state->oy, CHUNKSIZE);
+		    bytes = state->read(buffer, 1, CHUNKSIZE, state->in);
+
+		    if(bytes == 0)
+		    {
+			    state->lasterror =
+				    _("EOF before recognised stream.");
+			    goto err;
+		    }
+
+		    ogg_sync_wrote(state->oy, bytes);
+
+		    if(ogg_sync_pageout(state->oy, &og) == 1)
+			    break;
+	    }
+	    if(!ogg_page_bos(&og)) {
+		    read_bos = 0;
+		    page_pending = 1;
+	    }
+    }
+
+        if(!state->os) {
+		state->lasterror = _("Ogg bitstream does not contain a supported data-type.");
 		goto err;
 	}
 
-	if(ogg_stream_packetout(state->os, &header_main) != 1)
+	state->mainlen = header_main.bytes;
+	state->mainbuf = malloc(state->mainlen);
+	memcpy(state->mainbuf, header_main.packet, header_main.bytes);
+
+	if(ogg_page_serialno(&og) == state->serial)
 	{
-		state->lasterror = _("Error reading initial header packet.");
-		goto err;
+		if(buffer_chain_newlink(state) < 0)
+			goto err;
 	}
 
-	if(vorbis_synthesis_headerin(state->vi, state->vc, &header_main) < 0)
+	else
 	{
-		state->lasterror = _("Ogg bitstream does not contain vorbis data.");
-		goto err;
+		if(buffer_chain_push(state, &og) < 0)
+			goto err;
+		page_pending = 0;
 	}
 
-	state->mainlen = header_main.bytes;
-	state->mainbuf = malloc(state->mainlen);
-	memcpy(state->mainbuf, header_main.packet, header_main.bytes);
-
 	i = 0;
 	header = &header_comments;
 	while(i<2) {
 		while(i<2) {
-			int result = ogg_sync_pageout(state->oy, &og);
-			if(result == 0) break; /* Too little data so far */
+			int result;
+			if(!page_pending)
+				result = vcedit_target_pageout(state, &og);
+			else
+			{
+				result = 1;
+				page_pending = 0;
+			}
+			if(result == 0 || result == -2) break; /* Too little data so far */
+			else if(result == -1) goto err;
 			else if(result == 1)
 			{
 				ogg_stream_pagein(state->os, &og);
@@ -364,6 +715,8 @@
 
 	while((result = ogg_stream_flush(&streamout, &ogout)))
 	{
+		if(state->sidebuf && buffer_chain_writelink(state, out) < 0)
+			goto cleanup;
 		if(state->write(ogout.header,1,ogout.header_len, out) !=
 				(size_t) ogout.header_len)
 			goto cleanup;
@@ -372,6 +725,13 @@
 			goto cleanup;
 	}
 
+	while(state->sidebuf) {
+	  if(buffer_chain_writelink(state, out) < 0)
+	    goto cleanup;
+	}
+	if(buffer_chain_newlink(state) < 0)
+		goto cleanup;
+
 	while(_fetch_next_packet(state, &op, &ogin))
 	{
 		int size;
@@ -382,6 +742,9 @@
 		{
 			if(ogg_stream_flush(&streamout, &ogout))
 			{
+				if(state->sidebuf &&
+				   buffer_chain_writelink(state, out) < 0)
+					goto cleanup;
 				if(state->write(ogout.header,1,ogout.header_len, 
 							out) != (size_t) ogout.header_len)
 					goto cleanup;
@@ -394,6 +757,9 @@
 		{
 			if(ogg_stream_pageout(&streamout, &ogout))
 			{
+				if(state->sidebuf &&
+				   buffer_chain_writelink(state, out) < 0)
+					goto cleanup;
 				if(state->write(ogout.header,1,ogout.header_len, 
 							out) != (size_t) ogout.header_len)
 					goto cleanup;
@@ -430,6 +796,8 @@
 	streamout.e_o_s = 1;
 	while(ogg_stream_flush(&streamout, &ogout))
 	{
+		if(state->sidebuf && buffer_chain_writelink(state, out) < 0)
+			goto cleanup;
 		if(state->write(ogout.header,1,ogout.header_len, 
 					out) != (size_t) ogout.header_len)
 			goto cleanup;
@@ -440,6 +808,11 @@
 
 	if (state->extrapage)
 	{
+		/* This is the first page of a new chain, get rid of the
+		 * sidebuffer */
+		while(state->sidebuf)
+			if(buffer_chain_writelink(state, out) < 0)
+				goto cleanup;
 		if(state->write(ogin.header,1,ogin.header_len,
 		                out) != (size_t) ogin.header_len)
 			goto cleanup;

Modified: trunk/vorbis-tools/vorbiscomment/vcedit.h
===================================================================
--- trunk/vorbis-tools/vorbiscomment/vcedit.h	2008-02-05 07:59:02 UTC (rev 14458)
+++ trunk/vorbis-tools/vorbiscomment/vcedit.h	2008-02-06 22:31:35 UTC (rev 14459)
@@ -23,6 +23,11 @@
 typedef size_t (*vcedit_write_func)(const void *, size_t, size_t, void *);
 
 typedef struct {
+	long *streams;
+	size_t streams_len;
+} vcedit_serial_nos;
+
+typedef struct {
 	ogg_sync_state		*oy;
 	ogg_stream_state	*os;
 
@@ -32,17 +37,19 @@
 	vcedit_read_func read;
 	vcedit_write_func write;
 
-	void		*in;
-	long		serial;
-	unsigned char	*mainbuf;
-	unsigned char	*bookbuf;
-	int		mainlen;
-	int		booklen;
-	char 	    *lasterror;
+	void		  *in;
+	int serial;
+	vcedit_serial_nos serials;
+	unsigned char	  *mainbuf;
+	unsigned char	  *bookbuf;
+	int	          mainlen;
+	int	          booklen;
+	char   *lasterror;
 	char   *vendor;
 	int prevW;
 	int extrapage;
 	int eosin;
+        struct vcedit_buffer_chain *sidebuf;
 } vcedit_state;
 
 extern vcedit_state *	vcedit_new_state(void);

Added: trunk/vorbis-tools/vorbiscomment/vceditaux.h
===================================================================
--- trunk/vorbis-tools/vorbiscomment/vceditaux.h	                        (rev 0)
+++ trunk/vorbis-tools/vorbiscomment/vceditaux.h	2008-02-06 22:31:35 UTC (rev 14459)
@@ -0,0 +1,9 @@
+typedef struct vcedit_page_buffer {
+	char *data;
+	size_t data_len;
+} vcedit_page_buffer;
+	
+typedef struct vcedit_buffer_chain {
+	struct vcedit_buffer_chain *next;
+	struct vcedit_page_buffer buffer;
+} vcedit_buffer_chain;



More information about the commits mailing list