oggz-comment.c 14.2 KB
Newer Older
1
/*
2
   Copyright (C) 2008 Annodex Association
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

   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.
*/

/* Kangyuan Niu: original version (Aug 2007) */
33
/* Conrad Parker: modified based on modify-headers example (January 2008) */
34 35 36 37

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
38 39 40
#include <assert.h> /* , ja */
#include <errno.h>
#include <getopt.h>
41 42

#include <oggz/oggz.h>
43

44 45
#include "oggz_tools.h"

46 47 48 49 50 51 52 53 54 55 56 57
#define ID_WRITE_DIRECT
/* define USE_FLUSH_NEXT */

#ifdef USE_FLUSH_NEXT
static int flush_next = 0;
#endif

#define S_SERIALNO 0x7

typedef struct {
  int do_delete;
  int do_all;
58
  int got_non_bos;
59 60 61 62 63 64 65 66
  OGGZ * reader;
  OGGZ * writer;
  OGGZ * storer; /* Just used for storing comments from commandline */
  FILE * outfile;
  OggzTable * seen_tracks;
  OggzTable * serialno_table;
  OggzTable * content_types_table;
} OCData;
67 68 69 70 71 72

static char * progname;

static void
usage (char * progname)
{
73
  printf ("Usage: %s filename [options] tagname=tagvalue ...\n", progname);
74 75
  printf ("List or edit comments in an Ogg file.\n");
  printf ("\nOutput options\n");
76
  printf ("  -l, --list             List the comments in the given file.\n");
77 78 79
  printf ("\nEditing options\n");
  printf ("  -o filename, --output filename\n");
  printf ("                         Specify output filename\n");
80
  printf ("  -d, --delete           Delete comments before editing\n");
81
  printf ("  -a, --all              Edit comments for all logical bitstreams\n");
82
  printf ("  -c content-type, --content-type content-type\n");
83 84
  printf ("                         Edit comments of the logical bitstreams with\n");
  printf ("                         specified content-type\n");
85 86 87
  printf ("  -s serialno, --serialno serialno\n");
  printf ("                         Edit comments of the logical bitstream with\n");
  printf ("                         specified serialno\n");
88 89 90 91 92 93 94
  printf ("\nMiscellaneous options\n");
  printf ("  -h, --help             Display this help and exit\n");
  printf ("  -v, --version          Output version information and exit\n");
  printf ("\n");
  printf ("Please report bugs to <ogg-dev@xiph.org>\n");
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static OCData *
ocdata_new ()
{
  OCData *ocdata = malloc (sizeof (OCData));
  assert (ocdata != NULL);
  memset (ocdata, 0, sizeof (OCData));

  ocdata->do_all = 1;

  ocdata->storer = oggz_new (OGGZ_WRITE);
  
  ocdata->seen_tracks = oggz_table_new ();
  assert (ocdata->seen_tracks != NULL);

  ocdata->serialno_table = oggz_table_new();
  assert (ocdata->serialno_table != NULL);

  ocdata->content_types_table = oggz_table_new();
  assert (ocdata->content_types_table != NULL);
  
  return ocdata;
116 117
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static void 
ocdata_delete (OCData *ocdata)
{
  oggz_table_delete (ocdata->seen_tracks);
  oggz_table_delete (ocdata->serialno_table);
  oggz_table_delete (ocdata->content_types_table);
  
  if (ocdata->reader)
    oggz_close (ocdata->reader);
  if (ocdata->storer)
    oggz_close (ocdata->storer);
  if (ocdata->outfile)
    fclose (ocdata->outfile);
  
  free (ocdata);
}

static int
filter_stream_p (const OCData *ocdata, long serialno)
{
138 139 140
  if (oggz_table_lookup (ocdata->seen_tracks, serialno) == NULL)
    return 0;

141 142 143 144 145 146
  if (ocdata->do_all)
    return 1;

  if (oggz_table_lookup (ocdata->serialno_table, serialno) != NULL)
    return 1;

147 148 149
  return 0;
}

150 151 152 153 154 155
static int
read_bos (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
{
  OCData * ocdata = (OCData *)user_data;
  const char * ident;
  int i, n;
156
  OggzStreamContent content;
157

158 159 160 161
  if (ogg_page_bos ((ogg_page *)og)) {
    ocdata->got_non_bos = 0;
  } else {
    ocdata->got_non_bos = 1;
162
    return OGGZ_CONTINUE;
163
  }
164

165 166 167
  /* Record this track in the seen_tracks table, unless it is skeleton.
   * We don't need to store any information about the track, just the fact
   * that it exists.
168 169
   * Store a dummy value (as NULL is not allowed in an OggzTable).
   */
170 171 172
  content = oggz_stream_get_content (oggz, serialno);
  if (content != OGGZ_CONTENT_SKELETON)
    oggz_table_insert (ocdata->seen_tracks, serialno, (void *)0x01);
173 174 175 176 177 178 179 180 181 182

  ident = ot_page_identify (oggz, og, NULL);
  if (ident != NULL) {
    n = oggz_table_size (ocdata->content_types_table);
    for (i = 0; i < n; i++) {
      char * c = oggz_table_nth (ocdata->content_types_table, i, NULL);
      if (strcasecmp (c, ident) == 0) {
        oggz_table_insert (ocdata->serialno_table, serialno, (void *)0x7);
      }
    }
183
  }
184 185

  return OGGZ_CONTINUE;
186 187
}

188 189 190
static int
more_headers (OCData * ocdata, ogg_packet * op, long serialno)
{
191 192
  if (!ocdata->got_non_bos) return OGGZ_CONTINUE;

193 194 195 196 197 198 199 200 201 202 203
  /* Determine if we're finished processing headers */
  if (op->packetno+1 >= oggz_stream_get_numheaders (ocdata->reader, serialno)) {
    /* If this was the last header for this track, remove it from the
       track list */
    oggz_table_remove (ocdata->seen_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 (ocdata->seen_tracks) == 0) {
      return OGGZ_STOP_ERR;
    }
204
  }
205 206 207 208 209 210 211 212 213 214 215 216 217

  return OGGZ_CONTINUE;
}

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

  fwrite (og->header, 1, og->header_len, ocdata->outfile);
  fwrite (og->body, 1, og->body_len, ocdata->outfile);

  return OGGZ_CONTINUE;
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
static int
read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
  OCData * ocdata = (OCData *)user_data;
  const char * vendor;
  int flush;
  int ret;

#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

  /* Edit the packet data if required */
  if (filter_stream_p (ocdata, serialno) && op->packetno == 1) {
    vendor = oggz_comment_get_vendor (ocdata->reader, serialno);

    /* Copy across the comments, unless "delete comments before editing" */
    if (!ocdata->do_delete)
      oggz_comments_copy (ocdata->reader, serialno, ocdata->writer, serialno);

    /* Add stored comments from commandline */
    oggz_comments_copy (ocdata->storer, S_SERIALNO, ocdata->writer, serialno);

    /* Ensure the original vendor is preserved */
    oggz_comment_set_vendor (ocdata->writer, serialno, vendor);

    /* Generate the replacement comments packet */
    op = oggz_comments_generate (ocdata->writer, serialno, 0);
259
  }
260 261 262 263 264 265

  /* Feed the packet into the writer */
  if ((ret = oggz_write_feed (ocdata->writer, op, serialno, flush, NULL)) != 0) 
    fprintf (stderr, "oggz_write_feed: %d\n", ret);

  return more_headers (ocdata, op, serialno);
266 267
}

268 269 270 271 272 273 274 275
static int
edit_comments (OCData * ocdata, char * outfilename)
{
  unsigned char buf[1024];
  long n;

  if (outfilename == NULL) {
    ocdata->outfile = stdout;
276
  } else {
277 278 279 280 281
    ocdata->outfile = fopen (outfilename, "wb");
    if (ocdata->outfile == NULL) {
      fprintf (stderr, "%s: unable to open output file %s\n",
	       progname, outfilename);
      return -1;
282 283
    }
  }
284 285 286 287 288 289 290 291 292 293 294 295

  /* Set up writer, filling in ocdata for callbacks */
  if ((ocdata->writer = oggz_new (OGGZ_WRITE)) == NULL) {
    printf ("Unable to create new writer\n");
  }

  /* Set a page reader to process bos pages */
  oggz_set_read_page (ocdata->reader, -1, read_bos, ocdata);

  /* First, process headers packet-by-packet. */
  oggz_set_read_callback (ocdata->reader, -1, read_packet, ocdata);
  while ((n = oggz_read (ocdata->reader, 1024)) > 0) {
296 297 298
    long nn;
    while ((nn=oggz_write_output (ocdata->writer, buf, n)) > 0) {
      fwrite (buf, 1, nn, ocdata->outfile);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    }
  }

  /* We actually don't use the writer any more from here, so close it */
  oggz_close (ocdata->writer);

  /* Now, the headers are processed. We deregister the packet reading
   * callback. */
  oggz_set_read_callback (ocdata->reader, -1, NULL, NULL);

  /* We deal with the rest of the file as pages. */
  /* Register a callback that copies page data directly across to outfile */
  oggz_set_read_page (ocdata->reader, -1, read_page, ocdata);

  if (oggz_run (ocdata->reader) == OGGZ_ERR_OK)
    return 0;
  else
    return 1;
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
static int
read_comments(OGGZ *oggz, ogg_packet *op, long serialno, void *user_data)
{
  OCData * ocdata = (OCData *)user_data;
  const OggzComment * comment;
  const char * codec_name;

  if (filter_stream_p (ocdata, serialno) && op->packetno == 1) {
    codec_name = oggz_stream_get_content_type(oggz, serialno);
    if (codec_name) {
      printf ("%s: serialno %010ld\n", codec_name, serialno);
    } else {
      printf ("???: serialno %010ld\n", serialno);
    }

    printf("\tVendor: %s\n", oggz_comment_get_vendor(oggz, serialno));

    for (comment = oggz_comment_first(oggz, serialno); comment;
         comment = oggz_comment_next(oggz, serialno, comment))
      printf ("\t%s: %s\n", comment->name, comment->value);
  }

  return more_headers (ocdata, op, serialno);
342 343
}

344 345 346 347 348 349 350 351 352 353 354 355 356
static int
list_comments (OCData * ocdata)
{
  /* Set a page reader to process bos pages */
  oggz_set_read_page (ocdata->reader, -1, read_bos, ocdata);

  /* First, process headers packet-by-packet. */
  oggz_set_read_callback (ocdata->reader, -1, read_comments, ocdata);

  if (oggz_run (ocdata->reader) == OGGZ_ERR_STOP_OK)
    return 0;
  else
    return 1;
357 358
}

359 360 361
static void
store_comment (OCData * ocdata, char * s)
{
conrad's avatar
conrad committed
362
  char * c, * name, * value = NULL;
363

conrad's avatar
conrad committed
364
  if (s == NULL) return;
365 366

  name = s;
conrad's avatar
conrad committed
367 368 369 370 371 372

  c = strchr (s, '=');
  if (c != NULL) {
    *c = '\0';
    value = c+1;
  }
373 374

  oggz_comment_add_byname (ocdata->storer, S_SERIALNO, name, value);
375 376
}

377 378 379 380 381
int
main (int argc, char ** argv)
{
  char * infilename = NULL, * outfilename = NULL;
  OCData * ocdata;
382

383 384 385 386 387 388 389 390 391 392
  int filter_serialnos = 0;
  int filter_content_types = 0;

  int show_version = 0;
  int show_help = 0;
  int do_list = 0;

  long serialno;
  long n;
  int i = 1;
393

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
  char * optstring = "lo:dac:s:hv";

#ifdef HAVE_GETOPT_LONG
  static struct option long_options[] = {
    {"list",     no_argument, 0, 'l'},
    {"output",   required_argument, 0, 'o'},
    {"delete",   no_argument, 0, 'd'},
    {"all",      no_argument, 0, 'a'},
    {"content-type", required_argument, 0, 'c'},
    {"serialno", required_argument, 0, 's'},
    {"help",     no_argument, 0, 'h'},
    {"version",  no_argument, 0, 'v'},
    {0,0,0,0}
  };
#endif

410 411
  ot_init ();

412 413
  progname = argv[0];

414 415 416 417 418 419 420 421 422
  if (argc == 2 && !strncmp (argv[1], "-?", 2)) {
#ifdef HAVE_GETOPT_LONG
    ot_print_options (long_options, optstring);
#else
    ot_print_short_options (optstring);
#endif
    exit (0);
  }

423
  if (argc < 3) {
424
    usage (progname);
425
    return (1);
426 427
  }

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
  ocdata = ocdata_new ();

  while (1) {
#ifdef HAVE_GETOPT_LONG
    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 'l': /* list */
      do_list = 1;
      break;
    case 'd': /* delete */
      ocdata->do_delete = 1;
      break;
    case 'a': /* all */
      ocdata->do_all = 1;
      break;
    case 's': /* serialno */
      filter_serialnos = 1;
      ocdata->do_all = 0;
      serialno = atol (optarg);
      oggz_table_insert (ocdata->serialno_table, serialno, (void *)0x7);
      break;
    case 'c': /* content-type */
      filter_content_types = 1;
      ocdata->do_all = 0;
      n = oggz_table_size (ocdata->content_types_table);
      oggz_table_insert (ocdata->content_types_table, n, optarg);
      break;
    case 'h': /* help */
      show_help = 1;
      break;
    case 'v': /* version */
      show_version = 1;
      break;
    case 'o': /* output */
      outfilename = optarg;
      break;
    default:
      break;
    }
476 477
  }

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  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;
  }

  /* Parse out new comments and infilename */
  for (; optind < argc; optind++) {
    char * s = argv[optind];

    if(strchr(s, '=') != NULL) {
      if (!do_list) store_comment (ocdata, s);
501
    } else {
502
      infilename = s;
503 504 505
    }
  }

506 507 508
  /* Set up reader */
  if (infilename == NULL || strcmp (infilename, "-") == 0) {
    ocdata->reader = oggz_open_stdio (stdin, OGGZ_READ|OGGZ_AUTO);
509
  } else {
510
    ocdata->reader = oggz_open (infilename, OGGZ_READ|OGGZ_AUTO);
511 512
  }

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
  if (ocdata->reader == NULL) {
    if (errno == 0) {
      fprintf (stderr, "%s: %s: error opening input file\n",
	      progname, infilename);
    } else {
      fprintf (stderr, "%s: %s: %s\n",
	       progname, infilename, strerror (errno));
    }
    goto exit_err;
  }

  if (do_list) {
    if (list_comments (ocdata) == 0)
      goto exit_ok;
    else
      goto exit_err;
  }

  if (edit_comments (ocdata, outfilename) == 0)
    goto exit_ok;
  else
    goto exit_err;

exit_ok:
  ocdata_delete (ocdata);
  exit (0);

exit_err:
  ocdata_delete (ocdata);
  exit (1);
543
}