identity.c 3.28 KB
Newer Older
andre's avatar
andre 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 <stdio.h>
#include <stdlib.h>
#include <oggz/oggz.h>

conrad's avatar
conrad committed
37 38
#define ID_WRITE_DIRECT

andre's avatar
andre committed
39 40 41 42 43 44 45 46 47
static int flush_next = 0;

static int
read_packet (OGGZ * oggz, ogg_packet * op, long serialno, void * user_data)
{
  OGGZ * writer = (OGGZ *)user_data;
  int flush;
  int ret;

conrad's avatar
conrad committed
48
#if 0
andre's avatar
andre committed
49 50 51 52 53 54
  flush = flush_next;
  if (op->granulepos == -1) {
    flush_next = 0;
  } else {
    flush_next = OGGZ_FLUSH_BEFORE;
  }
conrad's avatar
conrad committed
55 56 57 58 59 60 61
#else
  if (op->granulepos == -1) {
    flush = 0;
  } else {
    flush = OGGZ_FLUSH_AFTER;
  }
#endif
andre's avatar
andre committed
62 63 64 65 66 67 68 69 70 71 72 73 74

  if ((ret = oggz_write_feed (writer, op, serialno, flush, NULL)) != 0) {
    printf ("oggz_write_feed: %d\n", ret);
  }

  return 0;
}

int
main (int argc, char ** argv)
{
  char * infilename, * outfilename;
  OGGZ * reader, * writer;
conrad's avatar
conrad committed
75 76
  FILE * outfile;
  unsigned char buf[1024];
andre's avatar
andre committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90
  long n;

  if (argc < 3) {
    printf ("usage: %s infile outfile\n", argv[0]);
  }

  infilename = argv[1];
  outfilename = argv[2];

  if ((reader = oggz_open (infilename, OGGZ_READ)) == NULL) {
    printf ("unable to open file %s\n", infilename);
    exit (1);
  }

conrad's avatar
conrad committed
91
#ifdef ID_WRITE_DIRECT
andre's avatar
andre committed
92 93 94 95
  if ((writer = oggz_open (outfilename, OGGZ_WRITE)) == NULL) {
    printf ("unable to open file %s\n", outfilename);
    exit (1);
  }
conrad's avatar
conrad committed
96 97 98 99 100 101
#else
  if ((writer = oggz_new (OGGZ_WRITE)) == NULL) {
    printf ("Unable to create new writer\n");
  }
  outfile = fopen (outfilename, "w");
#endif
andre's avatar
andre committed
102 103 104 105

  oggz_set_read_callback (reader, -1, read_packet, writer);

  while ((n = oggz_read (reader, 1024)) > 0) {
conrad's avatar
conrad committed
106
#ifdef ID_WRITE_DIRECT
andre's avatar
andre committed
107
    while (oggz_write (writer, n) > 0);
conrad's avatar
conrad committed
108 109 110 111 112
#else
    while (oggz_write_output (writer, buf, n) > 0) {
      fwrite (buf, 1, n, outfile);
    }
#endif
andre's avatar
andre committed
113 114
  }

conrad's avatar
conrad committed
115 116 117 118
#ifndef ID_WRITE_DIRECT
  fclose (outfile);
#endif

andre's avatar
andre committed
119 120 121 122 123
  oggz_close (writer);
  oggz_close (reader);

  exit (0);
}