[xiph-commits] r3362 - liboggz/trunk/src/examples

conrad at svn.annodex.net conrad at svn.annodex.net
Wed Jan 16 23:49:46 PST 2008


Author: conrad
Date: 2008-01-16 23:49:41 -0800 (Wed, 16 Jan 2008)
New Revision: 3362

Added:
   liboggz/trunk/src/examples/modify-headers.c
Modified:
   liboggz/trunk/src/examples/Makefile.am
Log:
add basic example for modifying headers. It currently makes an identical file,
processing the headers by packets and the rest by copying page data.


Modified: liboggz/trunk/src/examples/Makefile.am
===================================================================
--- liboggz/trunk/src/examples/Makefile.am	2008-01-13 13:26:12 UTC (rev 3361)
+++ liboggz/trunk/src/examples/Makefile.am	2008-01-17 07:49:41 UTC (rev 3362)
@@ -7,7 +7,7 @@
 
 if OGGZ_CONFIG_READ
 if OGGZ_CONFIG_WRITE
-oggz_rw_programs = identity rewrite-pages fix-eos
+oggz_rw_programs = identity rewrite-pages fix-eos modify-headers
 endif
 endif
 
@@ -26,6 +26,9 @@
 identity_SOURCES = identity.c
 identity_LDADD = $(OGGZ_LIBS)
 
+modify_headers_SOURCES = modify-headers.c
+modify_headers_LDADD = $(OGGZ_LIBS)
+
 rewrite_pages_SOURCES = rewrite-pages.c
 rewrite_pages_LDADD = $(OGGZ_LIBS)
 

Added: liboggz/trunk/src/examples/modify-headers.c
===================================================================
--- liboggz/trunk/src/examples/modify-headers.c	                        (rev 0)
+++ liboggz/trunk/src/examples/modify-headers.c	2008-01-17 07:49:41 UTC (rev 3362)
@@ -0,0 +1,172 @@
+/*
+   Copyright (C) 2007 Annodex Association
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+   - Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+   - Neither the name of the Annodex Association nor the names of its
+   contributors may be used to endorse or promote products derived from
+   this software without specific prior written permission.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+   PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ASSOCIATION OR
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <oggz/oggz.h>
+
+#define ID_WRITE_DIRECT
+/* define USE_FLUSH_NEXT */
+
+#ifdef USE_FLUSH_NEXT
+static int flush_next = 0;
+#endif
+
+typedef struct {
+  OggzTable * tracks;
+  OGGZ * reader;
+  OGGZ * writer;
+  FILE * outfile;
+} MHData;
+
+static int
+read_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
+{
+  MHData * mhdata = (MHData *)user_data;
+
+  fwrite (og->header, 1, og->header_len, mhdata->outfile);
+  fwrite (og->body, 1, og->body_len, mhdata->outfile);
+
+  return OGGZ_CONTINUE;
+}
+
+static int
+read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
+{
+  MHData * mhdata = (MHData *)user_data;
+  ogg_packet * new_op = op;
+  int flush;
+  int ret;
+
+  if (op->b_o_s) {
+    /* Record this track in the tracks table. We don't need to store any
+     * information about the track, just the fact that it exists.
+     * Store a dummy value (as NULL is not allowed in an OggzTable).
+     */
+    oggz_table_insert (mhdata->tracks, serialno, (void *)0x01);
+  }
+
+#ifdef USE_FLUSH_NEXT
+  flush = flush_next;
+  if (op->granulepos == -1) {
+    flush_next = 0;
+  } else {
+    flush_next = OGGZ_FLUSH_BEFORE;
+  }
+#else
+  if (op->granulepos == -1) {
+    flush = 0;
+  } else {
+    flush = OGGZ_FLUSH_AFTER;
+  }
+#endif
+
+  /* Do something with the packet data */
+
+  /* Feed the packet into the writer */
+  if ((ret = oggz_write_feed (mhdata->writer, new_op, serialno, flush, NULL)) != 0) {
+    printf ("oggz_write_feed: %d\n", ret);
+  }
+
+  /* Determine if we're finished processing headers */
+  if (op->packetno >= 2) {
+    /* If this was the last header for this track, remove it from the
+       track list */
+    oggz_table_remove (mhdata->tracks, serialno);
+
+    /* If no more tracks are left in the track list, then we have processed
+     * all the headers; stop processing of packets. */
+    if (oggz_table_size (mhdata->tracks) == 0) {
+      return OGGZ_STOP_ERR;
+    }
+  }
+
+  return OGGZ_CONTINUE;
+}
+
+
+int
+main (int argc, char ** argv)
+{
+  char * infilename, * outfilename;
+  MHData mhdata;
+  unsigned char buf[1024];
+  long n;
+
+  if (argc < 3) {
+    printf ("usage: %s infile outfile\n", argv[0]);
+    exit (1);
+  }
+
+  infilename = argv[1];
+  outfilename = argv[2];
+
+  /* Set up reader */
+  if ((mhdata.reader = oggz_open (infilename, OGGZ_READ | OGGZ_AUTO)) == NULL) {
+    printf ("unable to open file %s\n", infilename);
+    exit (1);
+  }
+
+  /* Set up writer, filling in mhdata for callbacks */
+  mhdata.tracks = oggz_table_new ();
+  if ((mhdata.writer = oggz_new (OGGZ_WRITE)) == NULL) {
+    printf ("Unable to create new writer\n");
+  }
+  mhdata.outfile = fopen (outfilename, "w");
+
+  /* First, process headers packet-by-packet. */
+  oggz_set_read_callback (mhdata.reader, -1, read_packet, &mhdata);
+  while ((n = oggz_read (mhdata.reader, 1024)) > 0) {
+    while (oggz_write_output (mhdata.writer, buf, n) > 0) {
+      fwrite (buf, 1, n, mhdata.outfile);
+    }
+  }
+
+  /* We actually don't use the writer any more from here, so close it */
+  oggz_close (mhdata.writer);
+
+  /* Now, the headers are processed. We deregister the packet reading
+   * callback. */
+  oggz_set_read_callback (mhdata.reader, -1, NULL, NULL);
+
+  /* We deal with the rest of the file as pages. */
+  /* Register a callbak that copies page data directly across to outfile */
+  oggz_set_read_page (mhdata.reader, -1, read_page, &mhdata);
+  while ((n = oggz_read (mhdata.reader, 1024)) > 0);
+
+  oggz_close (mhdata.reader);
+
+  oggz_table_delete (mhdata.tracks);
+
+  fclose (mhdata.outfile);
+
+  exit (0);
+}



More information about the commits mailing list