oggz-basetime.c 7.24 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
#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));
92 93
  if (ort == NULL) return NULL;

conrad's avatar
conrad committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  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));
114 115
  if (ord == NULL) return NULL;

conrad's avatar
conrad committed
116 117
  ord->base_units = -1;
  ord->tracks = oggz_table_new ();
118 119 120 121
  if (ord->tracks == NULL) {
    free (ord);
    return NULL;
  }
conrad's avatar
conrad committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

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

140 141 142 143 144 145 146 147 148 149 150 151
/********** checked_fwrite **********/

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

conrad's avatar
conrad committed
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
/********** 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
190

conrad's avatar
conrad committed
191 192 193 194 195 196
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;
197
  int numheaders;
conrad's avatar
conrad committed
198 199

  if (ogg_page_bos ((ogg_page *)og)) {
200 201
    if ((ort = or_track_data_new ()) == NULL)
      return OGGZ_STOP_ERR;
conrad's avatar
conrad committed
202 203 204 205 206 207 208 209 210
    oggz_table_insert (ord->tracks, serialno, ort);
  } else {
    ort = oggz_table_lookup (ord->tracks, serialno);
  }

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

211 212
  numheaders = oggz_stream_get_numheaders (oggz, serialno);

213 214 215 216 217
  /* 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.
   */
218
  if (ord->base_units == -1 && ort->nr_packets >= numheaders) {
conrad's avatar
conrad committed
219 220 221 222 223 224
    ord->base_units = oggz_tell_units (oggz);
#ifdef DEBUG
    fprintf (stderr, "BASE UNITS: %lld\n", ord->base_units);
#endif
  }

225 226
  /* If this is the first data page of a track, calculate the delta
   * by which to shift all pages of this track */
227
  if (ord->base_units != -1 && ort->nr_packets >= numheaders && ort->delta == -1) {
conrad's avatar
conrad committed
228 229 230
    oggz_get_granulerate (oggz, serialno, &gr_n, &gr_d);
    ort->delta = (ord->base_units * gr_n) / (gr_d);
#ifdef DEBUG
conrad's avatar
conrad committed
231
    fprintf (stderr, "%010lu: DELTA %lld (gr: %lld/%lld)\n",
conrad's avatar
conrad committed
232 233 234 235
	     serialno, ort->delta, gr_n, gr_d);
#endif
  }

thomasvs's avatar
 
thomasvs committed
236 237 238 239
  /* 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
240 241 242 243

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

#ifdef DEBUG
conrad's avatar
conrad committed
244
  fprintf (stderr, "%010lu: %d packets, gp %lld\n",
conrad's avatar
conrad committed
245 246 247
	   serialno, ort->nr_packets, ogg_page_granulepos ((ogg_page *)og));
#endif

248 249
  checked_fwrite (og->header, 1, og->header_len, stdout);
  checked_fwrite (og->body, 1, og->body_len, stdout);
conrad's avatar
conrad committed
250 251 252 253

  return 0;
}

254 255 256 257 258
int
usage (char * progname)
{
  printf ("usage: %s filename\n", progname);
  printf ("Shift timestamps on all pages such that the stream starts at 0.\n");
259 260

  return 0;
261 262
}

conrad's avatar
conrad committed
263 264 265
int
main (int argc, char ** argv)
{
266
  char * progname = argv[0];
conrad's avatar
conrad committed
267 268
  OGGZ * oggz;
  OBData * ord;
269
  int ret;
conrad's avatar
conrad committed
270 271

  if (argc < 2) {
272 273 274 275
    usage (progname);
    return (1);
  }

276 277 278 279 280
  if (!strncmp (argv[1], "-?", 2)) {
    printf ("-h --help\n");
    exit (0);
  }

281 282 283
  if (!strcmp (argv[1], "-h") || !strcmp (argv[1], "--help")) {
    usage (progname);
    return (0);
conrad's avatar
conrad committed
284 285 286
  }

  ord = or_data_new ();
287
  if (ord == NULL) goto oom;
conrad's avatar
conrad committed
288 289 290 291 292 293 294 295

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

296
  ret = oggz_run (oggz);
conrad's avatar
conrad committed
297 298 299 300 301

  oggz_close (oggz);

  or_data_delete (ord);

302 303
  if (ret == OGGZ_ERR_STOP_ERR) goto oom;

conrad's avatar
conrad committed
304
  exit (0);
305 306 307 308

oom:
  fprintf (stderr, "%s: out of memory\n", progname);
  exit (1);
conrad's avatar
conrad committed
309
}