oggz-basetime.c 6.65 KB
Newer Older
conrad's avatar
conrad committed
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
/*
   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>
37
#include <string.h>
conrad's avatar
conrad committed
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 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
#include <oggz/oggz.h>

#include "oggz_tools.h"

/* #define DEBUG */

typedef struct {
  ogg_int64_t delta;
  int nr_packets;
} OBTrackData;

typedef struct {
  ogg_int64_t base_units;
  OggzTable * tracks;
} OBData;

static  ogg_int64_t
_le_64 (ogg_int64_t l)
{
  ogg_int64_t ret=l;
  unsigned char *ucptr = (unsigned char *)&ret;
#ifdef WORDS_BIGENDIAN
  unsigned char temp;

  temp = ucptr [0] ;
  ucptr [0] = ucptr [7] ;
  ucptr [7] = temp ;

  temp = ucptr [1] ;
  ucptr [1] = ucptr [6] ;
  ucptr [6] = temp ;

  temp = ucptr [2] ;
  ucptr [2] = ucptr [5] ;
  ucptr [5] = temp ;

  temp = ucptr [3] ;
  ucptr [3] = ucptr [4] ;
  ucptr [4] = temp ;

#endif
  return (*(ogg_int64_t *)ucptr);
}

#define INT64_LE_AT(x) _le_64((*(ogg_int64_t *)(x)))

/********** OBTrackData **********/

static OBTrackData *
or_track_data_new (void)
{
  OBTrackData * ort;

  ort = malloc (sizeof (OBTrackData));
  ort->delta = -1;
  ort->nr_packets = 0;

  return ort;
}

static void
or_track_data_delete (OBTrackData * ort)
{
  free (ort);
}

/********** OBData **********/

static OBData *
or_data_new (void)
{
  OBData * ord;

  ord = malloc (sizeof (OBData));
  ord->base_units = -1;
  ord->tracks = oggz_table_new ();

  return ord;
}

static void
or_data_delete (OBData * ord)
{
  int i, n;
  OBTrackData * ort;

  n = oggz_table_size (ord->tracks);
  for (i=0; i<n; i++) {
    ort = oggz_table_nth (ord->tracks, i, NULL);
    or_track_data_delete (ort);
  }
  oggz_table_delete (ord->tracks);
}

/********** Filter **********/

static int
filter_page (OGGZ * oggz, const ogg_page * og, long serialno, OBData * ord)
{
  OBTrackData * ort;
  ogg_int64_t granulepos, new_granulepos;
  ogg_int64_t iframe, pframe;
  int granuleshift;

  ort = oggz_table_lookup (ord->tracks, serialno);

  granulepos = ogg_page_granulepos ((ogg_page *)og);
  granuleshift = oggz_get_granuleshift (oggz, serialno);

  iframe = granulepos >> granuleshift;
  pframe = granulepos - (iframe << granuleshift);

  iframe -= ort->delta;

  new_granulepos = (iframe << granuleshift) + pframe;

#ifdef DEBUG
    fprintf (stderr, "old gp %lld, new gp %lld", granulepos, new_granulepos);
#endif

  *(ogg_int64_t *)(&og->header[6]) = _le_64(new_granulepos);

#ifdef DEBUG
  fprintf (stderr, ", totally new gp %lld\n", INT64_LE_AT(&og->header[6]));
#endif

  /* AFTER making any changes to the page, recalculate the page checksum */
  ogg_page_checksum_set ((ogg_page *)og);

  return 0;
}

thomasvs's avatar
 
thomasvs committed
170

conrad's avatar
conrad committed
171 172 173 174 175 176
static int
read_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
{
  OBData * ord = (OBData *)user_data;
  OBTrackData * ort;
  ogg_int64_t gr_n, gr_d;
177
  int numheaders;
conrad's avatar
conrad committed
178 179 180 181 182 183 184 185 186 187 188 189

  if (ogg_page_bos ((ogg_page *)og)) {
    ort = or_track_data_new ();
    oggz_table_insert (ord->tracks, serialno, ort);
  } else {
    ort = oggz_table_lookup (ord->tracks, serialno);
  }

#ifdef DEBUG
  fprintf (stderr, "--------\n");
#endif

190 191
  numheaders = oggz_stream_get_numheaders (oggz, serialno);

192 193 194 195 196
  /* If this is the first data page of any track, use its timestamp
   * as the delta by which to shift all tracks. Hence this page will
   * get shifted to timestamp 0, and all other pages in the stream will
   * be shifted relatively.
   */
197
  if (ord->base_units == -1 && ort->nr_packets >= numheaders) {
conrad's avatar
conrad committed
198 199 200 201 202 203
    ord->base_units = oggz_tell_units (oggz);
#ifdef DEBUG
    fprintf (stderr, "BASE UNITS: %lld\n", ord->base_units);
#endif
  }

204 205
  /* If this is the first data page of a track, calculate the delta
   * by which to shift all pages of this track */
206
  if (ord->base_units != -1 && ort->nr_packets >= numheaders && ort->delta == -1) {
conrad's avatar
conrad committed
207 208 209 210 211 212 213 214
    oggz_get_granulerate (oggz, serialno, &gr_n, &gr_d);
    ort->delta = (ord->base_units * gr_n) / (gr_d);
#ifdef DEBUG
    fprintf (stderr, "%010ld: DELTA %lld (gr: %lld/%lld)\n",
	     serialno, ort->delta, gr_n, gr_d);
#endif
  }

thomasvs's avatar
 
thomasvs committed
215 216 217 218
  /* header pages have a granulepos 0 and should not have it changed */
  if (ogg_page_granulepos ((ogg_page *) og) != 0) {
    filter_page (oggz, og, serialno, ord);
  }
conrad's avatar
conrad committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232

  ort->nr_packets += ogg_page_packets ((ogg_page *)og);

#ifdef DEBUG
  fprintf (stderr, "%010ld: %d packets, gp %lld\n",
	   serialno, ort->nr_packets, ogg_page_granulepos ((ogg_page *)og));
#endif

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

  return 0;
}

233 234 235 236 237
int
usage (char * progname)
{
  printf ("usage: %s filename\n", progname);
  printf ("Shift timestamps on all pages such that the stream starts at 0.\n");
238 239

  return 0;
240 241
}

conrad's avatar
conrad committed
242 243 244
int
main (int argc, char ** argv)
{
245
  char * progname = argv[0];
conrad's avatar
conrad committed
246 247 248 249
  OGGZ * oggz;
  OBData * ord;

  if (argc < 2) {
250 251 252 253
    usage (progname);
    return (1);
  }

254 255 256 257 258
  if (!strncmp (argv[1], "-?", 2)) {
    printf ("-h --help\n");
    exit (0);
  }

259 260 261
  if (!strcmp (argv[1], "-h") || !strcmp (argv[1], "--help")) {
    usage (progname);
    return (0);
conrad's avatar
conrad committed
262 263 264 265 266 267 268 269 270 271 272
  }

  ord = or_data_new ();

  if ((oggz = oggz_open ((char *)argv[1], OGGZ_READ | OGGZ_AUTO)) == NULL) {
    printf ("unable to open file %s\n", argv[1]);
    exit (1);
  }

  oggz_set_read_page (oggz, -1, read_page, ord);

273
  oggz_run (oggz);
conrad's avatar
conrad committed
274 275 276 277 278 279 280

  oggz_close (oggz);

  or_data_delete (ord);

  exit (0);
}