[xiph-commits] r16278 - experimental/giles/rogg

giles at svn.xiph.org giles at svn.xiph.org
Tue Jul 14 15:04:07 PDT 2009


Author: giles
Date: 2009-07-14 15:04:06 -0700 (Tue, 14 Jul 2009)
New Revision: 16278

Added:
   experimental/giles/rogg/rogg_theora.c
Removed:
   experimental/giles/rogg/rogg_aspect.c
   experimental/giles/rogg/rogg_fps.c
Modified:
   experimental/giles/rogg/Makefile
Log:
Combine the aspect and fps scripts into a single rogg_theora utility.

Also fixes a bug in the option parsing that caused it to fail for more
than one argument.


Modified: experimental/giles/rogg/Makefile
===================================================================
--- experimental/giles/rogg/Makefile	2009-07-14 11:59:11 UTC (rev 16277)
+++ experimental/giles/rogg/Makefile	2009-07-14 22:04:06 UTC (rev 16278)
@@ -6,9 +6,10 @@
 prefix = /usr/local
 
 OPTS = -g -O2 -Wall
+OPTS = -g -O0 -Wall
 
 rogg_UTILS = rogg_pagedump rogg_eosfix rogg_crcfix \
-	rogg_aspect rogg_fps rogg_stats rogg_serial
+	rogg_stats rogg_serial rogg_theora
 
 all : librogg.a $(rogg_UTILS)
 
@@ -27,18 +28,15 @@
 rogg_pagedump : rogg_pagedump.o librogg.a
 	$(CC) $(CFLAGS) -o $@ $^
 
-rogg_aspect : rogg_aspect.o librogg.a
-	$(CC) $(CFLAGS) -o $@ $^
-
-rogg_fps : rogg_fps.o librogg.a
-	$(CC) $(CFLAGS) -o $@ $^
-
 rogg_stats : rogg_stats.o librogg.a
 	$(CC) $(CFLAGS) -o $@ $^
 
 rogg_serial : rogg_serial.o librogg.a
 	$(CC) $(CFLAGS) -o $@ $^
 
+rogg_theora : rogg_theora.o librogg.a
+	$(CC) $(CFLAGS) -o $@ $^
+
 check : all
 
 clean :

Deleted: experimental/giles/rogg/rogg_aspect.c
===================================================================
--- experimental/giles/rogg/rogg_aspect.c	2009-07-14 11:59:11 UTC (rev 16277)
+++ experimental/giles/rogg/rogg_aspect.c	2009-07-14 22:04:06 UTC (rev 16278)
@@ -1,241 +0,0 @@
-/*
-   Copyright (C) 2007 Xiph.org Foundation
-
-   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.
-
-   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 ORGANISATION 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.
-*/
-
-/* theora aspect-ratio mod script using the rogg library */
-
-/* compile with
-   gcc -O2 -g -Wall -I. -o rogg_aspect rogg.c rogg_aspect.c
-*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <rogg.h>
-
-int verbose = 0;
-int aspect_set = 0;
-int aspect_num = 0;
-int aspect_den = 0;
-
-/* big endian accessors for the theora header data */
-int get16(unsigned char *data)
-{
-  return (data[0] << 8) | data[1];
-}
-int get24(unsigned char *data)
-{
-  return (data[0] << 16) | (data[1] << 8) | data[2];
-}
-int get32(unsigned char *data)
-{
-  return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
-}
-void put16(unsigned char *data, int v)
-{
-  data[0] = (v >> 8) & 0xFF;
-  data[1] = v & 0xFF;
-}
-void put24(unsigned char *data, int v)
-{
-  data[0] = (v >> 16) & 0xFF;
-  data[1] = (v >>  8) & 0xFF;
-  data[2] = v & 0xFF;
-}
-void put32(unsigned char *data, int v)
-{
-  data[0] = (v >> 24) & 0xFF;
-  data[1] = (v >> 16) & 0xFF;
-  data[2] = (v >>  8) & 0xFF;
-  data[3] = v & 0xFF;
-}
-
-void print_header_info(FILE *out, rogg_page_header *header)
-{
-  fprintf(out, " Ogg page serial %08x seq %d (%5d bytes)",
-	header->serialno, header->sequenceno, header->length);
-  fprintf(out, (header->continued) ? " c" : "  ");
-  fprintf(out, " %lld", (long long int)header->granulepos);
-  fprintf(out, (header->bos) ? " bos" : "");
-  fprintf(out, (header->eos) ? " eos" : "");
-  fprintf(out, "\n");
-}
-
-void print_theora_info(FILE *out, unsigned char *data)
-{
-  fprintf(out, "  Theora info header version %d %d %d\n",
-	data[7], data[8], data[9]);
-  fprintf(out, "   encoded image %dx%d\n", 
-	get16(data+10)<<4, get16(data+12)<<4);
-  fprintf(out, "   display image %dx%d at (%d,%d)\n",
-	get24(data+14), get24(data+17), data[20], data[21]);
-  fprintf(out, "   frame rate %d/%d\n", get32(data+22), get32(data+26));
-  fprintf(out, "   pixel aspect %d:%d\n", get24(data+30), get24(data+33));
-  fprintf(out, "   colourspace %d\n", data[36]);
-  fprintf(out, "   target bitrate %d\n", get24(data+37));
-  fprintf(out, "   quality %d\n", data[40] >> 2);
-}
-
-void print_usage(FILE *out, char *name)
-{
-  fprintf(stderr, "Script for editing theora headers\n");
-  fprintf(stderr, "%s [-v] [-a num:den] <file1.ogg> [<file2.ogg>...]\n",
-	name);
-  fprintf(stderr, "    -v          print more information\n"
-		  "    -a num:den  set the pixel aspect ratio\n"
-		  "                common values: \n"
-		  "                        59:54 PAL  (4:3 frame)\n"
-		  "                        10:11 NTSC (4:3 frame)\n"
-		  "                       118:81 PAL  (16:9 frame)\n"
-		  "                        40:33 NTSC (16:9 frame)\n"
-		  "                         1:1  computer source\n");
-}
-
-int parse_args(int *argc, char *argv[])
-{
-  int arg = 1;
-  int shift;
-
-  while (arg < *argc) {
-    shift = 0;
-    if (argv[arg][0] == '-') {
-      switch (argv[arg][1]) {
-	case 'v':
-	  verbose = 1;
-	  shift = 1; 
-	  break;
-	case 'a':
-	  aspect_set = 1;
-	  /* read aspect from the next arg */
-	  if (sscanf(argv[arg+1], "%d:%d", &aspect_num, &aspect_den) !=2 ) {
-	    fprintf(stderr, "Could not parse aspect ratio '%s'.\n", argv[arg+1]);
-	    fprintf(stderr, "Try something like 59:54 (PAL) or 10:11 (NTSC)\n\n");
-	    aspect_set = 0;
-	  }
-	  shift = 2;
-	  break;
-      }
-    }
-    if (shift) {
-      memmove(&argv[arg],&argv[arg+shift],shift*sizeof(*argv));
-      *argc -= shift;
-    } else {
-      arg++;
-    }
-  }
-
-  return 0;
-}
-
-int main(int argc, char *argv[])
-{
-  int f, i;
-  unsigned char *p, *q, *o, *e;
-  struct stat s;
-  rogg_page_header header;
-
-  parse_args(&argc, argv);
-  if (argc < 2) {
-    print_usage(stderr, argv[0]);
-    exit(1);
-  }
-
-  for (i = 1; i < argc; i++) {
-    f = open(argv[i], O_RDWR);
-    if (f < 0) {
-	fprintf(stderr, "couldn't open '%s'\n", argv[i]);
-	continue;
-    }
-    if (fstat(f, &s) < 0) {
-	fprintf(stderr, "couldn't stat '%s'\n", argv[i]);
-	close(f);
-	continue;
-    }
-    p = mmap(0, s.st_size, PROT_READ|PROT_WRITE,
-	MAP_SHARED, f, 0);
-    if (p == MAP_FAILED) {
-	fprintf(stderr, "couldn't mmap '%s'\n", argv[i]);
-	close(f);
-	continue;
-    }
-    fprintf(stdout, "Checking Ogg file '%s'\n", argv[i]);
-    e = p + s.st_size; /* pointer to the end of the file */
-    q = rogg_scan(p, s.st_size); /* scan for an Ogg page */
-    if (q == NULL) {
-	fprintf(stdout, "couldn't find ogg data!\n");
-    } else {
-      if (q > p) {
-	fprintf(stdout, "Skipped %d garbage bytes at the start\n", (int)(q-p));
-      } 
-      while (q < e) {
-	o = rogg_scan(q, e-q); /* find the next Ogg page */
-	if (o > q) {
-	  fprintf(stdout, "Hole in data! skipped %d bytes\n", (int)(o-q));
-	   q = o;
-	} else if (o == NULL) {
-	  fprintf(stdout, "Skipped %d garbage bytes as the end\n", (int)(e-q));
-	  break;
-	}
-	rogg_page_parse(q, &header);
-	if (!header.bos) break; /* only look at the initial bos pages */
-	if (verbose) {
-	  print_header_info(stdout, &header);
-	  int j;
-	  for (j = 0; j < header.length; j++) {
-	    fprintf(stdout, " %02x", header.data[j]);
-	    if (!((j+1)%4)) fprintf(stdout, " ");
-	    if (!((j+1)%16)) fprintf(stdout, "\n");
-	  }
-	  fprintf(stdout, "\n");
-	}
-	if (!memcmp(header.data, "\x80theora", 7)) {
-	  print_theora_info(stdout, header.data);
-	  if (aspect_set) {
-	    fprintf(stdout, "Setting aspect ratio to %d:%d\n",
-		aspect_num, aspect_den);
-	    put24(header.data+30, aspect_num); /* numerator */
-	    put24(header.data+33, aspect_den); /* denominator */
-	    rogg_page_update_crc(q);
-	    fprintf(stdout, "New settings:\n");
-	    print_theora_info(stdout, header.data); 
-	  }
-        }
-	q += header.length;
-      }
-    }
-    munmap(p, s.st_size);
-    close(f);
-  }
-  return 0;
-}

Deleted: experimental/giles/rogg/rogg_fps.c
===================================================================
--- experimental/giles/rogg/rogg_fps.c	2009-07-14 11:59:11 UTC (rev 16277)
+++ experimental/giles/rogg/rogg_fps.c	2009-07-14 22:04:06 UTC (rev 16278)
@@ -1,239 +0,0 @@
-/*
-   Copyright (C) 2009 Xiph.org Foundation
-
-   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.
-
-   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 ORGANISATION 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.
-*/
-
-/* theora frame rate mod script using the rogg library */
-
-/* compile with
-   gcc -O2 -g -Wall -I. -o rogg_framerate rogg.c rogg_framerate.c
-*/
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include <unistd.h>
-#include <fcntl.h>
-
-#include <rogg.h>
-
-int verbose = 0;
-int fps_set = 0;
-int fps_num = 0;
-int fps_den = 0;
-
-/* big endian accessors for the theora header data */
-int get16(unsigned char *data)
-{
-  return (data[0] << 8) | data[1];
-}
-int get24(unsigned char *data)
-{
-  return (data[0] << 16) | (data[1] << 8) | data[2];
-}
-int get32(unsigned char *data)
-{
-  return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
-}
-void put16(unsigned char *data, int v)
-{
-  data[0] = (v >> 8) & 0xFF;
-  data[1] = v & 0xFF;
-}
-void put24(unsigned char *data, int v)
-{
-  data[0] = (v >> 16) & 0xFF;
-  data[1] = (v >>  8) & 0xFF;
-  data[2] = v & 0xFF;
-}
-void put32(unsigned char *data, int v)
-{
-  data[0] = (v >> 24) & 0xFF;
-  data[1] = (v >> 16) & 0xFF;
-  data[2] = (v >>  8) & 0xFF;
-  data[3] = v & 0xFF;
-}
-
-void print_header_info(FILE *out, rogg_page_header *header)
-{
-  fprintf(out, " Ogg page serial %08x seq %d (%5d bytes)",
-	header->serialno, header->sequenceno, header->length);
-  fprintf(out, (header->continued) ? " c" : "  ");
-  fprintf(out, " %lld", (long long int)header->granulepos);
-  fprintf(out, (header->bos) ? " bos" : "");
-  fprintf(out, (header->eos) ? " eos" : "");
-  fprintf(out, "\n");
-}
-
-void print_theora_info(FILE *out, unsigned char *data)
-{
-  fprintf(out, "  Theora info header version %d %d %d\n",
-	data[7], data[8], data[9]);
-  fprintf(out, "   encoded image %dx%d\n", 
-	get16(data+10)<<4, get16(data+12)<<4);
-  fprintf(out, "   display image %dx%d at (%d,%d)\n",
-	get24(data+14), get24(data+17), data[20], data[21]);
-  fprintf(out, "   frame rate %d/%d\n", get32(data+22), get32(data+26));
-  fprintf(out, "   pixel aspect %d:%d\n", get24(data+30), get24(data+33));
-  fprintf(out, "   colourspace %d\n", data[36]);
-  fprintf(out, "   target bitrate %d\n", get24(data+37));
-  fprintf(out, "   quality %d\n", data[40] >> 2);
-}
-
-void print_usage(FILE *out, char *name)
-{
-  fprintf(stderr, "Script for editing theora headers\n");
-  fprintf(stderr, "%s [-v] [-f num:den] <file1.ogg> [<file2.ogg>...]\n",
-	name);
-  fprintf(stderr, "    -v          print more information\n"
-		  "    -f num:den  set the frame rate\n"
-		  "                common values: \n"
-		  "                        25:1 PAL\n"
-		  "                        30000:1001 NTSC\n"
-		  "                        24:1 film\n");
-}
-
-int parse_args(int *argc, char *argv[])
-{
-  int arg = 1;
-  int shift;
-
-  while (arg < *argc) {
-    shift = 0;
-    if (argv[arg][0] == '-') {
-      switch (argv[arg][1]) {
-	case 'v':
-	  verbose = 1;
-	  shift = 1; 
-	  break;
-	case 'f':
-	  fps_set = 1;
-	  /* read frame rate from the next arg */
-	  if (sscanf(argv[arg+1], "%d:%d", &fps_num, &fps_den) !=2 ) {
-	    fprintf(stderr, "Could not parse frame rate '%s'.\n", argv[arg+1]);
-	    fprintf(stderr, "Try something like 25:1 (PAL) or 30000:1001 (NTSC)\n\n");
-	    fps_set = 0;
-	  }
-	  shift = 2;
-	  break;
-      }
-    }
-    if (shift) {
-      memmove(&argv[arg],&argv[arg+shift],shift*sizeof(*argv));
-      *argc -= shift;
-    } else {
-      arg++;
-    }
-  }
-
-  return 0;
-}
-
-int main(int argc, char *argv[])
-{
-  int f, i;
-  unsigned char *p, *q, *o, *e;
-  struct stat s;
-  rogg_page_header header;
-
-  parse_args(&argc, argv);
-  if (argc < 2) {
-    print_usage(stderr, argv[0]);
-    exit(1);
-  }
-
-  for (i = 1; i < argc; i++) {
-    f = open(argv[i], O_RDWR);
-    if (f < 0) {
-	fprintf(stderr, "couldn't open '%s'\n", argv[i]);
-	continue;
-    }
-    if (fstat(f, &s) < 0) {
-	fprintf(stderr, "couldn't stat '%s'\n", argv[i]);
-	close(f);
-	continue;
-    }
-    p = mmap(0, s.st_size, PROT_READ|PROT_WRITE,
-	MAP_SHARED, f, 0);
-    if (p == MAP_FAILED) {
-	fprintf(stderr, "couldn't mmap '%s'\n", argv[i]);
-	close(f);
-	continue;
-    }
-    fprintf(stdout, "Checking Ogg file '%s'\n", argv[i]);
-    e = p + s.st_size; /* pointer to the end of the file */
-    q = rogg_scan(p, s.st_size); /* scan for an Ogg page */
-    if (q == NULL) {
-	fprintf(stdout, "couldn't find ogg data!\n");
-    } else {
-      if (q > p) {
-	fprintf(stdout, "Skipped %d garbage bytes at the start\n", (int)(q-p));
-      } 
-      while (q < e) {
-	o = rogg_scan(q, e-q); /* find the next Ogg page */
-	if (o > q) {
-	  fprintf(stdout, "Hole in data! skipped %d bytes\n", (int)(o-q));
-	   q = o;
-	} else if (o == NULL) {
-	  fprintf(stdout, "Skipped %d garbage bytes as the end\n", (int)(e-q));
-	  break;
-	}
-	rogg_page_parse(q, &header);
-	if (!header.bos) break; /* only look at the initial bos pages */
-	if (verbose) {
-	  print_header_info(stdout, &header);
-	  int j;
-	  for (j = 0; j < header.length; j++) {
-	    fprintf(stdout, " %02x", header.data[j]);
-	    if (!((j+1)%4)) fprintf(stdout, " ");
-	    if (!((j+1)%16)) fprintf(stdout, "\n");
-	  }
-	  fprintf(stdout, "\n");
-	}
-	if (!memcmp(header.data, "\x80theora", 7)) {
-	  print_theora_info(stdout, header.data);
-	  if (fps_set) {
-	    fprintf(stdout, "Setting frame rate to %d:%d\n",
-		fps_num, fps_den);
-	    put32(header.data+22, fps_num); /* numerator */
-	    put32(header.data+26, fps_den); /* denominator */
-	    rogg_page_update_crc(q);
-	    fprintf(stdout, "New settings:\n");
-	    print_theora_info(stdout, header.data); 
-	  }
-        }
-	q += header.length;
-      }
-    }
-    munmap(p, s.st_size);
-    close(f);
-  }
-  return 0;
-}

Copied: experimental/giles/rogg/rogg_theora.c (from rev 16238, experimental/giles/rogg/rogg_aspect.c)
===================================================================
--- experimental/giles/rogg/rogg_theora.c	                        (rev 0)
+++ experimental/giles/rogg/rogg_theora.c	2009-07-14 22:04:06 UTC (rev 16278)
@@ -0,0 +1,275 @@
+/*
+   Copyright (C) 2009 Xiph.org Foundation
+
+   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.
+
+   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 ORGANISATION 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.
+*/
+
+/* theora header modification script using the rogg library */
+
+/* compile with
+   gcc -O2 -g -Wall -I. -o rogg_theora rogg.c rogg_theora.c
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <rogg.h>
+
+int verbose = 0;
+int aspect_set = 0;
+int aspect_num = 0;
+int aspect_den = 0;
+int fps_set = 0;
+int fps_num = 0;
+int fps_den = 0;
+
+/* big endian accessors for the theora header data */
+int get16(unsigned char *data)
+{
+  return (data[0] << 8) | data[1];
+}
+int get24(unsigned char *data)
+{
+  return (data[0] << 16) | (data[1] << 8) | data[2];
+}
+int get32(unsigned char *data)
+{
+  return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
+}
+void put16(unsigned char *data, int v)
+{
+  data[0] = (v >> 8) & 0xFF;
+  data[1] = v & 0xFF;
+}
+void put24(unsigned char *data, int v)
+{
+  data[0] = (v >> 16) & 0xFF;
+  data[1] = (v >>  8) & 0xFF;
+  data[2] = v & 0xFF;
+}
+void put32(unsigned char *data, int v)
+{
+  data[0] = (v >> 24) & 0xFF;
+  data[1] = (v >> 16) & 0xFF;
+  data[2] = (v >>  8) & 0xFF;
+  data[3] = v & 0xFF;
+}
+
+void print_header_info(FILE *out, rogg_page_header *header)
+{
+  fprintf(out, " Ogg page serial %08x seq %d (%5d bytes)",
+	header->serialno, header->sequenceno, header->length);
+  fprintf(out, (header->continued) ? " c" : "  ");
+  fprintf(out, " %lld", (long long int)header->granulepos);
+  fprintf(out, (header->bos) ? " bos" : "");
+  fprintf(out, (header->eos) ? " eos" : "");
+  fprintf(out, "\n");
+}
+
+void print_theora_info(FILE *out, unsigned char *data)
+{
+  fprintf(out, "  Theora info header version %d %d %d\n",
+	data[7], data[8], data[9]);
+  fprintf(out, "   encoded image %dx%d\n", 
+	get16(data+10)<<4, get16(data+12)<<4);
+  fprintf(out, "   display image %dx%d at (%d,%d)\n",
+	get24(data+14), get24(data+17), data[20], data[21]);
+  fprintf(out, "   frame rate %d/%d\n", get32(data+22), get32(data+26));
+  fprintf(out, "   pixel aspect %d:%d\n", get24(data+30), get24(data+33));
+  fprintf(out, "   colourspace %d\n", data[36]);
+  fprintf(out, "   target bitrate %d\n", get24(data+37));
+  fprintf(out, "   quality %d\n", data[40] >> 2);
+}
+
+void print_usage(FILE *out, char *name)
+{
+  fprintf(stderr, "Script for editing theora headers, in place.\n");
+  fprintf(stderr, "%s [-v] [-a num:den] [-f num:den] <file1.ogg> [<file2.ogg>...]\n",
+	name);
+  fprintf(stderr, "    -v          print more information\n"
+		  "    -a num:den  set the pixel aspect ratio\n"
+		  "                common values: \n"
+		  "                        59:54 PAL  (4:3 frame)\n"
+		  "                        10:11 NTSC (4:3 frame)\n"
+		  "                       118:81 PAL  (16:9 frame)\n"
+		  "                        40:33 NTSC (16:9 frame)\n"
+		  "                         1:1  computer source\n"
+		  "    -f num:den  set frame rate\n"
+		  "                common values: \n"
+		  "                        25:1 PAL\n"
+		  "                     30000:1001 NTSC\n"
+		  "                        24:1 film\n");
+}
+
+int parse_args(int *argc, char *argv[])
+{
+  int arg = 1;
+  int shift;
+
+  while (arg < *argc) {
+    /* parse recognized options */
+    shift = 0;
+    if (argv[arg][0] == '-') {
+      switch (argv[arg][1]) {
+	case 'v':
+	  verbose = 1;
+	  shift = 1; 
+	  break;
+	case 'a':
+	  aspect_set = 1;
+	  /* read aspect from the next arg */
+	  if (sscanf(argv[arg+1], "%d:%d", &aspect_num, &aspect_den) !=2 ) {
+	    fprintf(stderr, "Could not parse aspect ratio '%s'.\n", argv[arg+1]);
+	    fprintf(stderr, "Try something like 59:54 (PAL) or 10:11 (NTSC)\n\n");
+	    aspect_set = 0;
+	  }
+	  shift = 2;
+	  break;
+        case 'f':
+	  fps_set = 1;
+	  /* read frame rate from the next arg */
+	  if (sscanf(argv[arg+1], "%d:%d", &fps_num, &fps_den) !=2 ) {
+	    fprintf(stderr, "Could not parse frame rate '%s'.\n", argv[arg+1]);
+	    fprintf(stderr, "Try something like 25:1 (PAL) or 30000:1001 (NTSC)\n\n");
+	    fps_set = 0;
+	  }
+	  shift = 2;
+	  break;
+      }
+    }
+
+    /* consume the arguments we've parsed */
+    if (shift) {
+      int left = *argc - arg - shift;
+      if (left < 0) {
+	fprintf(stderr, "Interal error parsing argument '%s'.\n", argv[arg]);
+	exit(1);
+      }
+      memmove(&argv[arg], &argv[arg+shift], left*sizeof(*argv));
+      *argc -= shift;
+    } else {
+      arg++;
+    }
+  }
+
+  return 0;
+}
+
+int main(int argc, char *argv[])
+{
+  int f, i;
+  unsigned char *p, *q, *o, *e;
+  struct stat s;
+  rogg_page_header header;
+
+  parse_args(&argc, argv);
+  if (argc < 2) {
+    print_usage(stderr, argv[0]);
+    exit(1);
+  }
+
+  for (i = 1; i < argc; i++) {
+    f = open(argv[i], O_RDWR);
+    if (f < 0) {
+	fprintf(stderr, "couldn't open '%s'\n", argv[i]);
+	continue;
+    }
+    if (fstat(f, &s) < 0) {
+	fprintf(stderr, "couldn't stat '%s'\n", argv[i]);
+	close(f);
+	continue;
+    }
+    p = mmap(0, s.st_size, PROT_READ|PROT_WRITE,
+	MAP_SHARED, f, 0);
+    if (p == MAP_FAILED) {
+	fprintf(stderr, "couldn't mmap '%s'\n", argv[i]);
+	close(f);
+	continue;
+    }
+    fprintf(stdout, "Checking Ogg file '%s'\n", argv[i]);
+    e = p + s.st_size; /* pointer to the end of the file */
+    q = rogg_scan(p, s.st_size); /* scan for an Ogg page */
+    if (q == NULL) {
+	fprintf(stdout, "couldn't find ogg data!\n");
+    } else {
+      if (q > p) {
+	fprintf(stdout, "Skipped %d garbage bytes at the start\n", (int)(q-p));
+      } 
+      while (q < e) {
+	o = rogg_scan(q, e-q); /* find the next Ogg page */
+	if (o > q) {
+	  fprintf(stdout, "Hole in data! skipped %d bytes\n", (int)(o-q));
+	   q = o;
+	} else if (o == NULL) {
+	  fprintf(stdout, "Skipped %d garbage bytes as the end\n", (int)(e-q));
+	  break;
+	}
+	rogg_page_parse(q, &header);
+	if (!header.bos) break; /* only look at the initial bos pages */
+	if (verbose) {
+	  print_header_info(stdout, &header);
+	  int j;
+	  for (j = 0; j < header.length; j++) {
+	    fprintf(stdout, " %02x", header.data[j]);
+	    if (!((j+1)%4)) fprintf(stdout, " ");
+	    if (!((j+1)%16)) fprintf(stdout, "\n");
+	  }
+	  fprintf(stdout, "\n");
+	}
+	if (!memcmp(header.data, "\x80theora", 7)) {
+	  print_theora_info(stdout, header.data);
+	  if (aspect_set) {
+	    fprintf(stdout, "Setting aspect ratio to %d:%d\n",
+		aspect_num, aspect_den);
+	    put24(header.data+30, aspect_num); /* numerator */
+	    put24(header.data+33, aspect_den); /* denominator */
+	  }
+	  if (fps_set) {
+	    fprintf(stdout, "Setting frame rate to %d:%d\n",
+		fps_num, fps_den);
+	    put32(header.data+22, fps_num); /* numerator */
+	    put32(header.data+26, fps_den); /* denominator */
+	  }
+	  if (aspect_set || fps_set) {
+	    rogg_page_update_crc(q);
+	    fprintf(stdout, "New settings:\n");
+	    print_theora_info(stdout, header.data); 
+	  }
+        }
+	q += header.length;
+      }
+    }
+    munmap(p, s.st_size);
+    close(f);
+  }
+  return 0;
+}


Property changes on: experimental/giles/rogg/rogg_theora.c
___________________________________________________________________
Added: svn:mergeinfo
   + 



More information about the commits mailing list