oggzrip.c 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* -*- c-file-style: "gnu" -*- */
/*
  Copyright (C) 2005 Commonwealth Scientific and Industrial Research
  Organisation (CSIRO) Australia

  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 CSIRO Australia 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 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.

  Author: David Kuehling <dvdkhlng@gmx.de>
  Created: 20041231
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <getopt.h>
#include <errno.h>

#include <oggz/oggz.h>
48
#include "oggz_tools.h"
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

#ifdef _WIN32
/* Supply missing headers and functions for Win32 */

#include <fcntl.h>
#endif

#define READ_SIZE 4096
#define WRITE_SIZE 4096

#define MAX_FILTER 64

typedef struct {
  OGGZ *reader;
  FILE *outfile;
  int numwrite;
  OggzTable *streams;
  int verbose;
  int num_serialnos;
  long serialnos[MAX_FILTER];
  int num_streamids;
  long streamids[MAX_FILTER];
71 72
  int num_content_types;
  const char *content_types[MAX_FILTER];
73 74 75 76 77
} ORData;

typedef struct {
  long serialno;
  int streamid;
78
  const char *content_type;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  int bos;
} ORStream;

static int streamid_count = 0;

static void
usage (char * progname)
{
  printf ("Usage: %s [options] filename ...\n", progname);
  printf ("\nMiscellaneous options\n");
  printf ("  -o filename, --output filename\n");
  printf ("                         Specify output filename\n");
  printf ("  -h, --help             Display this help and exit\n");
  printf ("  -v, --version          Output version information and exit\n");
  printf ("  -V                     Verbose operation\n");
  printf ("\nFilter options\n");
  printf ("  These options can be used multiple times. Pages matching ANY of\n");
  printf ("  the filter options will be included into the output.\n\n");
  printf ("  -s serialno, --serialno serialno\n");
  printf ("                         Output streams with given serialno.\n");
99 100
  printf ("  -i index, --stream-index index\n");
  printf ("                         Filter by stream index. These are assigned to\n");
101 102
  printf ("                         streams in the order of their BOS pages,\n");
  printf ("                         starting at 0.\n");
103 104 105
  printf ("  -c content-type --content-type content-type\n");
  printf ("                         Filter by content-type.  The following codec\n");
  printf ("                         names are currently detected: \"theora\",\n");
106
  printf ("                         \"vorbis\", \"speex\", \"annodex\"\n");
conrad's avatar
conrad committed
107 108
  printf ("\n");
  printf ("Please report bugs to <ogg-dev@xiph.org>\n");
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
}

static ORData *
ordata_new ()
{
  ORData *ordata = malloc (sizeof (ordata));
  assert (ordata != NULL);
  memset (ordata, 0, sizeof (ORData));
  
  ordata->streams = oggz_table_new ();
  assert (ordata->streams != NULL);
  
  return ordata;
}

static void 
ordata_delete (ORData *ordata)
{
  oggz_table_delete (ordata->streams);
  
  if (ordata->reader)
    oggz_close (ordata->reader);
  if (ordata->outfile)
    fclose (ordata->outfile);
  
  free (ordata);
}

static int
filter_stream_p (const ORData *ordata, ORStream *stream, 
		 const ogg_page *og, long serialno)
{
  int i;

  for (i = 0; i < ordata->num_serialnos; i++) {
    if (ordata->serialnos[i] == serialno)
      return 1;
  }

  if (stream == NULL)
    return 0;
  
  for (i = 0; i < ordata->num_streamids; i++) {
    if (ordata->streamids[i] == stream->streamid)
      return 1;
  }

156 157
  for (i = 0; i < ordata->num_content_types; i++) {
    if (strcmp (ordata->content_types[i], stream->content_type) == 0)
158 159 160 161 162 163 164 165 166
      return 1;
  }

  return 0;
}

static ORStream *
orstream_new (const ORData *ordata, const ogg_page *og, long serialno)
{
167 168
  const char * ident;
  
169 170 171 172 173
  ORStream *stream = malloc (sizeof (ORStream));
  assert (stream != NULL);

  stream->serialno = serialno;
  stream->streamid = streamid_count++;
174
  stream->content_type = "unknown";
175

176 177
  ident = ot_page_identify (og);
  if (ident != NULL) stream->content_type = ident;
178 179 180 181
   
  if (ordata->verbose)
    fprintf (stderr, 
	     "New logical stream, serialno %li, id %i, codec %s, will be %s\n",
182
	     stream->serialno, stream->streamid, stream->content_type,
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	     (filter_stream_p (ordata, stream, og, serialno) ? 
	      "copied" :"dropped"));

  return stream;
}

static void 
orstream_delete (ORData *ordata, ORStream *stream)
{
  if (ordata->verbose)
    fprintf (stderr, "End of logical stream %li   \n", stream->serialno);

  free (stream);
}

static void
checked_fwrite (const void *data, size_t size, size_t count, FILE *stream)
{
  int n = fwrite (data, size, count, stream);
  if (n != count) {
    perror ("write failed");
    exit (1);
  }
}

static int
read_page (OGGZ *oggz, const ogg_page *og, long serialno, void *user_data)
{
  ORData *ordata = (ORData *) user_data;
  ORStream *stream = oggz_table_lookup (ordata->streams, serialno);

  if (ogg_page_bos ((ogg_page *)og)) {
    stream = orstream_new (ordata, og, serialno);
    stream = oggz_table_insert (ordata->streams, serialno, stream);
    assert (stream != NULL);
  } else if (stream == NULL) {
    fprintf (stderr, "WARNING: found page for nonexistent stream %li\n",
	     serialno);
  }

  if (filter_stream_p (ordata, stream, og, serialno)) {
    checked_fwrite (og->header, 1, og->header_len, ordata->outfile);
    checked_fwrite (og->body, 1, og->body_len, ordata->outfile);
  }

  if (ogg_page_eos ((ogg_page *)og) && stream != NULL) {
    oggz_table_remove (ordata->streams, serialno);
    orstream_delete (ordata, stream);
  }

  return 0;
}

static int
oggz_rip (ORData * ordata)
{
  long n;

  oggz_set_read_page (ordata->reader, -1, read_page, ordata);
  
  while ((n = oggz_read (ordata->reader, READ_SIZE))) {

    if (n <= 0)
      return n;

    if (ordata->verbose) {
      fprintf (stderr, "\r Read %li k, wrote %li k ...\r",
	       (long) (oggz_tell (ordata->reader)/1024),
	       (long) (ftell (ordata->outfile)/1024));
    }
  }

  if (ordata->verbose) 
    fprintf (stderr, "\r Done.                                 \n");

  return 0;
}

static int 
or_get_long (const char *optarg, const char *currentopt,
			long *value)
{
  char *tailptr;

  *value = strtol (optarg, &tailptr, 10);

  if (*tailptr != '\0') {
    fprintf (stderr, "ERROR: non-integer argument to option `%s': %s\n",
	     currentopt, optarg);
    return -1;
  }

  return 0;
}

int
main (int argc, char * argv[]) 
{
  int show_version = 0;
  int show_help = 0;

  char * progname;
  char * infilename = NULL, * outfilename = NULL;
  FILE * infile = NULL;
  const char *currentopt = argv[1];
  ORData * ordata;
  int i;

#ifdef _WIN32
  /* We need to set stdin/stdout to binary mode */

  _setmode( _fileno( stdin ), _O_BINARY );
  _setmode( _fileno( stdout ), _O_BINARY );
#endif

  progname = argv[0];

  if (argc < 2) {
    usage (progname);
    return (1);
  }

  ordata = ordata_new ();

  while (1) {
    char * optstring = "hvVo:s:i:c:";

#ifdef HAVE_GETOPT_LONG
    static struct option long_options[] = {
      {"help", no_argument, 0, 'h'},
      {"version", no_argument, 0, 'v'},
      {"output", required_argument, 0, 'o'},
      {"verbose", no_argument, 0, 'V'},
      {"serialno", required_argument, 0, 's'},
317
      {"stream-index", required_argument, 0, 'i'},
318
      {"content-type", required_argument, 0, 'c'},
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
      {0,0,0,0}
    };

    i = getopt_long (argc, argv, optstring, long_options, NULL);
#else
    i = getopt (argc, argv, optstring);
#endif
    if (i == -1) break;
    if (i == ':') {
      usage (progname);
      goto exit_err;
    }

    switch (i) {
    case 'h': /* help */
      show_help = 1;
      break;
    case 'v': /* version */
      show_version = 1;
      break;
    case 'o': /* output */
      outfilename = optarg;
      break;
    case 'V': /* verbose */
      ordata->verbose = 1;
      break;
    case 's': /* serialno */
      if (ordata->num_serialnos >= MAX_FILTER) {
	fprintf (stderr, "ERROR: too many serialnos on command line\n");
	goto exit_err;
      }
      if (or_get_long (optarg, currentopt, 
		       &ordata->serialnos[ordata->num_serialnos++]))
	goto exit_err;

      break;
355
    case 'i': /* stream index */
356
      if (ordata->num_streamids >= MAX_FILTER) {
357
	fprintf (stderr, "ERROR: too many stream indexes on command line\n");
358 359 360 361 362
	goto exit_err;
      }
      if (or_get_long (optarg, currentopt, 
		       &ordata->streamids[ordata->num_streamids++]))
	goto exit_err;
363 364 365
    case 'c': /* content-type */
      if (ordata->num_content_types >= MAX_FILTER) {
	fprintf (stderr, "ERROR: too many content-types on command line\n");
366 367
	goto exit_err;
      }
368
      ordata->content_types[ordata->num_content_types++] = optarg;	     
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
      break;
	  
    default:
      break;
    }

    currentopt = argv[optind];
  }

  if (show_version) {
    printf ("%s version " VERSION "\n", progname);
  }

  if (show_help) {
    usage (progname);
  }

  if (show_version || show_help) {
    goto exit_ok;
  }

  if (optind != argc-1) {
    usage (progname);
    goto exit_err;
  }

  infilename = argv[optind];
  infile = fopen (infilename, "rb");
  if (infile == NULL) {
    fprintf (stderr, "%s: unable to open input file %s : %s\n", progname,
	     infilename, strerror (errno));
    goto exit_err;
  } else {
    ordata->reader = oggz_open_stdio (infile, OGGZ_READ|OGGZ_AUTO);
  }

  if (outfilename == NULL) {
    ordata->outfile = stdout;
  } else {
    ordata->outfile = fopen (outfilename, "wb");
    if (ordata->outfile == NULL) {
      fprintf (stderr, "%s: unable to open output file %s : %s\n",
	       progname, outfilename, strerror (errno));
      goto exit_err;
    }
  }

  oggz_rip (ordata);

 exit_ok:
  ordata_delete (ordata);
  exit (0);

 exit_err:
  ordata_delete (ordata);
  exit (1);
}