oggz-sort.c 9.59 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 48 49 50
/*
   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 "config.h"

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

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

#include <oggz/oggz.h>
#include "oggz_tools.h"

#define READ_SIZE 4096

static void
usage (char * progname)
{
  printf ("Usage: %s [options] filename ...\n", progname);
51
  printf ("Sort the pages of an Ogg file in order of presentation time.\n");
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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
  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          Verbose operation\n");
  printf ("\n");
  printf ("Please report bugs to <ogg-dev@xiph.org>\n");
}

typedef struct _OSData OSData;
typedef struct _OSInput OSInput;
typedef struct _OSITrack OSITrack;

struct _OSData {
  char * infilename;
  OggzTable * inputs;
  int verbose;
};

struct _OSInput {
  OSData * osdata;
  OGGZ * reader;
  long serialno;
  const ogg_page * og;
};

struct _OSITrack {
  long output_serialno;
};

static ogg_page *
_ogg_page_copy (const ogg_page * og)
{
  ogg_page * new_og;

  new_og = malloc (sizeof (*og));
  new_og->header = malloc (og->header_len);
  new_og->header_len = og->header_len;
  memcpy (new_og->header, og->header, og->header_len);
  new_og->body = malloc (og->body_len);
  new_og->body_len = og->body_len;
  memcpy (new_og->body, og->body, og->body_len);

  return new_og;
}

static int
_ogg_page_free (const ogg_page * og)
{
  free (og->header);
  free (og->body);
  free ((ogg_page *)og);
  return 0;
}

static void
osinput_delete (OSInput * input)
{
  oggz_close (input->reader);

  free (input);
}

static OSData *
osdata_new (void)
{
  OSData * osdata;

  osdata = (OSData *) malloc (sizeof (OSData));

  osdata->inputs = oggz_table_new ();
  osdata->verbose = 0;

  return osdata;
}

static void
osdata_delete (OSData * osdata)
{
  OSInput * input;
  int i, ninputs;

  ninputs = oggz_table_size (osdata->inputs);
  for (i = 0; i < ninputs; i++) {
    input = (OSInput *) oggz_table_nth (osdata->inputs, i, NULL);
    osinput_delete (input);
  }
  oggz_table_delete (osdata->inputs);

  free (osdata);
}

static int
read_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
{
  OSInput * input = (OSInput *) user_data;

  /* If this is the serialno that this input is tracking, stash it;
   * otherwise continue scanning the file */
  if (serialno == input->serialno) {
    input->og = _ogg_page_copy (og);
    return OGGZ_STOP_OK;
  } else {
    return OGGZ_CONTINUE;
  }
}

static int
read_page_add_input (OGGZ * oggz, const ogg_page * og, long serialno,
                     void * user_data)
{
  OSData * osdata = (OSData *)user_data;
  OSInput * input;
  int nfiles;

  if (ogg_page_bos(og)) {
    input = (OSInput *) malloc (sizeof (OSInput));
    if (input == NULL) return -1;

    input->osdata = osdata;
    input->reader = oggz_open (osdata->infilename, OGGZ_READ|OGGZ_AUTO);
    input->serialno = serialno;
    input->og = NULL;

    oggz_set_read_page (input->reader, -1, read_page, input);

    nfiles = oggz_table_size (osdata->inputs);
    if (!oggz_table_insert (osdata->inputs, nfiles++, input)) {
      osinput_delete (input);
      return -1;
    }

    return OGGZ_CONTINUE;
  } else {
    return OGGZ_STOP_OK;
  }
}

static int
osdata_add_file (OSData * osdata, char * infilename)
{
  OGGZ * reader;

  osdata->infilename = infilename;

  if ((reader = oggz_open (infilename, OGGZ_READ|OGGZ_AUTO)) != NULL) {
    oggz_set_read_page (reader, -1, read_page_add_input, osdata);
    oggz_run (reader);
    oggz_close (reader);
    return 0;
  } else {
    return -1;
  }
}

static int
oggz_sort (OSData * osdata, FILE * outfile)
{
  OSInput * input;
  int ninputs, i, min_i;
  long key, n;
  ogg_int64_t units, min_units;
  const ogg_page * og;
  int active;

  /* For theora+vorbis, ensure theora bos is first */
  int careful_for_theora = 0;

  int v;

  if (oggz_table_size (osdata->inputs) == 2)
    careful_for_theora = 1;

  while ((ninputs = oggz_table_size (osdata->inputs)) > 0) {
    min_units = -1;
    min_i = -1;
    active = 1;

    if (osdata->verbose)
      printf ("------------------------------------------------------------\n");

    /* Reload all pages, and find the min (earliest) */
    for (i = 0; active && i < oggz_table_size (osdata->inputs); i++) {
      input = (OSInput *) oggz_table_nth (osdata->inputs, i, &key);
      if (input != NULL) {
	while (input && input->og == NULL) {
	  n = oggz_read (input->reader, READ_SIZE);
	  if (n == 0) {
	    oggz_table_remove (osdata->inputs, key);
	    osinput_delete (input);
	    input = NULL;
	  }
	}
	if (input && input->og) {
	  if (ogg_page_bos ((ogg_page *)input->og)) {
	    min_i = i;

	    if (careful_for_theora) {
	      const char * codec_name;
	      int is_vorbis = 0;

	      if ((codec_name = 
                  ot_page_identify (input->reader, input->og, NULL)) != NULL)
		is_vorbis = !strcmp (codec_name, "Vorbis");

	      if (i == 0 && is_vorbis)
		careful_for_theora = 0;
	      else
		active = 0;

	    } else {
	      active = 0;
	    }
          }
	  units = oggz_tell_units (input->reader);

	  if (osdata->verbose) {
	    ot_fprint_time (stdout, (double)units/1000);
	    printf (": Got index %d serialno %010d %lld units: ",
272
		    i, ogg_page_serialno ((ogg_page *)input->og), (long long) units);
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 317 318 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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 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
	  }

	  if (min_units == -1 || units == 0 ||
	      (units > -1 && units < min_units)) {
	    min_units = units;
	    min_i = i;
	    if (osdata->verbose)
	      printf ("Min\n");
	  } else {
	    if (osdata->verbose)
	      printf ("Moo\n");
	  }
	} else if (osdata->verbose) {
	  if (input == NULL) {
	    printf ("*** index %d NULL\n", i);
	  } else {
	    printf ("*** No page from index %d\n", i);
	  }
	}
      }
    }

    if (osdata->verbose)
      printf ("Min index %d\n", min_i);

    /* Write the earliest page */
    if (min_i != -1) {
      input = (OSInput *) oggz_table_nth (osdata->inputs, min_i, &key);
      og = input->og;
      fwrite (og->header, 1, og->header_len, outfile);
      fwrite (og->body, 1, og->body_len, outfile);

      _ogg_page_free (og);
      input->og = NULL;
    }
  }

  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, * outfile = NULL;
  OSData * osdata;
  int i;

  ot_init ();

  progname = argv[0];

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

  osdata = osdata_new();

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

#ifdef HAVE_GETOPT_LONG
    static struct option long_options[] = {
      {"help", no_argument, 0, 'h'},
      {"version", no_argument, 0, 'v'},
      {"verbose", no_argument, 0, 'V'},
      {"output", required_argument, 0, 'o'},
      {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 */
      osdata->verbose = 1;
    default:
      break;
    }
  }

  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) {
    usage (progname);
    goto exit_err;
  }

  if (optind >= argc) {
    usage (progname);
    goto exit_err;
  }

  infilename = argv[optind++];
  osdata_add_file (osdata, infilename);

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

  oggz_sort (osdata, outfile);

 exit_ok:
  osdata_delete (osdata);
  exit (0);

 exit_err:
  osdata_delete (osdata);
  exit (1);
}