oggz-scan.c 9.3 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 51 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
/*
   Copyright (C) 2003 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.
*/

#include "config.h"

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

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

#ifdef HAVE_INTTYPES_H
#  include <inttypes.h>
#else
#  define PRId64 "I64d"
#endif

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

/* #define DEBUG */

typedef struct {
  OggzReadPacket read_packet;
  int clipcount;
  int pktssincekey;
  int granuleshift;
  int keyframes;
  int cmml;
  int html;
} OSData;

static char * progname;
static FILE * outfile = NULL;

#define HTML_HEAD "<html>\n<head>\n<title>OGGZ_SCAN OUTPUT</title>\n</head>\n\n<body>\n<h1>OGGZ_SCAN OUTPUT for %s</h1>\n\n"

#define HTML_END "<hr/>\n</body>\n</html>"

#define HTML_CLIP "<p>Clip No %i\tat t=%lf.</p>\n\n"


#define CMML_HEAD "<cmml>\n<stream>\n<import src=\"%s\"/>\n</stream>\n\n<head>\n<title>OGGZ_SCAN OUTPUT for %s</title>\n</head>\n\n"

#define CMML_END "</cmml>"

#define CMML_CLIP "<clip id=\"clip-%i\" start=\"%lf\">\n<desc>Enter description.</desc>\n</clip>\n\n"

static void
usage (char * progname)
{
  printf ("Usage: %s [options] filename\n", progname);
83 84
  printf ("Scan an Ogg file and output characteristic landmarks.\n");
  printf ("\nOutput options\n");
85 86
  printf ("  -o filename, --output filename\n");
  printf ("                         Specify output filename\n");
87 88 89 90 91 92
  printf ("  -f format, --format format\n");
  printf ("                         Specify output format. Supported formats are plain,\n");
  printf ("                         cmml, and html. (Default: plain)\n");
  printf ("\nFeature options\n");
  printf ("  -k, --keyframe         Display timestamps of unforced theora keyframes\n");
  printf ("\nMiscellaneous options\n");
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
  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");
}

static int
filter_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
{
  OSData * osdata = (OSData *) user_data;
  const char * ident;

  /* set scanning callback for keyframe calculation on theora pages only */
  if (osdata->keyframes && ogg_page_bos ((ogg_page *)og)) {
    ident = ot_page_identify (og, NULL);
    if (strcasecmp ("theora", ident) == 0) {
       oggz_set_read_callback (oggz, serialno, osdata->read_packet, osdata);
    }
  }

  return 0;
}

/* FIXME: on Mac OS X, off_t is 64-bits.  Obviously we want a nicer
 * way to do it than this, but a quick fix is a good fix */
#ifdef __APPLE__
#  define PRI_off_t "q"
#else
#  define PRI_off_t "l"
#endif

static int
read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
  OSData * osdata = (OSData *) user_data;
  double time_offset;

  /* calculate granuleshift for theora track */
  if (osdata->granuleshift == 0) {
    osdata->granuleshift = 1 << oggz_get_granuleshift (oggz, serialno);
    osdata->granuleshift--;
#ifdef DEBUG
    fprintf(outfile, "Granuleshift = %d\n", osdata->granuleshift);
#endif
  }

  /* don't do anything on bos page */
  if (op->b_o_s) {
    return 0;
  }

  /* calculate the keyframes if requested */
  if (osdata->keyframes) {
    /* increase number of packets seen since the last intra frame */
    osdata->pktssincekey++;

    /* does the current packet contain a keyframe? */
    if(!(op->packet[0] & 0x80) /* data packet */ &&
       !(op->packet[0] & 0x40) /* intra frame */ ) {
      ogg_int64_t units;

#ifdef DEBUG
      fprintf(outfile, "Keyframe found: packetno=%" PRId64 
              "\t pktssincekey=%d\n", op->packetno, osdata->pktssincekey);
#endif

      /* if the keyframe is on the granuleshift position, ignore it */
      if (osdata->pktssincekey >= osdata->granuleshift) {
        osdata->pktssincekey=0;
        return 0;
      }
      osdata->pktssincekey=0;

      /* new shot boundary found: calculate time */
      units = oggz_tell_units (oggz);
      if (units == -1) {
        time_offset = oggz_tell(oggz);
      } else {
        time_offset = (double)units / 1000.0;
      }

      /* output in requested format */
      if (osdata->html) {
        fprintf(outfile, HTML_CLIP, osdata->clipcount, time_offset);
      }
      if (osdata->cmml) {
        fprintf(outfile, CMML_CLIP, osdata->clipcount, time_offset);
      }
      osdata->clipcount++;
      if (!osdata->html && !osdata->cmml) {
183 184
	ot_fprint_time (outfile, time_offset);
	fputc ('\n', outfile);
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
      }
    }
  }

#ifdef DEBUG
  fprintf (outfile, "%ld bytes pktno=%" PRId64 "\n", op->bytes, op->packetno);
#endif

  return 0;
}

int
main (int argc, char ** argv)
{
  int show_version = 0;
  int show_help = 0;
  int output_cmml = 0;
  int output_html = 0;
  int scan_keyframes = 0;

  OSData * osdata = NULL;
  OGGZ * oggz;
  char * infilename = NULL, * outfilename = NULL;
  int i;
  long n;

  progname = argv[0];

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

  while (1) {
219
    char * optstring = "f:khvo:";
220 221 222 223

#ifdef HAVE_GETOPT_LONG
    static struct option long_options[] = {
      {"output",   required_argument, 0, 'o'},
224
      {"format",   required_argument, 0, 'f'},
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      {"keyframe", no_argument, 0, 'k'},
      {"help",     no_argument, 0, 'h'},
      {"version",  no_argument, 0, 'v'},
      {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) {
242 243 244 245 246 247 248 249 250 251 252
    case 'f': /* format */
      if (!strcmp (optarg, "cmml")) {
        output_cmml = 1;
	output_html = 0;
      } else if (!strcmp (optarg, "html")) {
        output_cmml = 0;
	output_html = 1;
      } else {
        output_cmml = 0;
	output_html = 0;
      }
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 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
      break;
    case 'w': /* html */
      output_html = 1;
      break;
    case 'k': /* keyframe */
      scan_keyframes = 1;
      break;
    case 'h': /* help */
      show_help = 1;
      break;
    case 'v': /* version */
      show_version = 1;
      break;
    case 'o': /* output */
      outfilename = optarg;
      break;
    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;
  }

  infilename = argv[optind++];

  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;
    }
  }

  errno = 0;

  if (strcmp (infilename, "-") == 0) {
    oggz = oggz_open_stdio (stdin, OGGZ_READ|OGGZ_AUTO);
  } else {
    oggz = oggz_open (infilename, OGGZ_READ|OGGZ_AUTO);
  }

  if (oggz == 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;
  }

  /* init osdata */
  osdata = malloc (sizeof (OSData));
  memset (osdata, 0, sizeof (OSData));
  osdata->read_packet = read_packet;
  if (scan_keyframes) osdata->keyframes = 1;
  if (output_cmml)    osdata->cmml = 1;
  if (output_html)    osdata->html = 1;

  /* set up the right filters on the tracks */
  oggz_set_read_page (oggz, -1, filter_page, osdata);

  /* correct output format */
  if (output_html) {
    fprintf(outfile, HTML_HEAD, infilename);
  }
  if (output_cmml) {
    fprintf(outfile, CMML_HEAD, infilename, infilename);
  }
 
  while ((n = oggz_read (oggz, 1024)) > 0);

  /* finish output */
  if (output_html) {
    fprintf(outfile, HTML_END);
  }
  if (output_cmml) {
    fprintf(outfile, CMML_END);
  }

  oggz_close (oggz);

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

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