2
Copyright (C) 2003 Commonwealth Scientific and Industrial Research
3
Organisation (CSIRO) Australia
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions
9
- Redistributions of source code must retain the above copyright
10
notice, this list of conditions and the following disclaimer.
12
- Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in the
14
documentation and/or other materials provided with the distribution.
16
- Neither the name of CSIRO Australia nor the names of its
17
contributors may be used to endorse or promote products derived from
18
this software without specific prior written permission.
20
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR
24
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
#include <oggz/oggz.h>
44
#include "oggz_tools.h"
46
#define READ_SIZE 4096
49
usage (char * progname)
51
printf ("Usage: %s [options] filename ...\n", progname);
52
printf ("Merge Ogg files together, interleaving pages in order of presentation time.\n");
53
printf ("\nMiscellaneous options\n");
54
printf (" -o filename, --output filename\n");
55
printf (" Specify output filename\n");
56
printf (" -h, --help Display this help and exit\n");
57
printf (" -v, --version Output version information and exit\n");
59
printf ("Please report bugs to <ogg-dev@xiph.org>\n");
62
typedef struct _OMData OMData;
63
typedef struct _OMInput OMInput;
64
typedef struct _OMITrack OMITrack;
81
_ogg_page_copy (const ogg_page * og)
85
new_og = malloc (sizeof (*og));
86
new_og->header = malloc (og->header_len);
87
new_og->header_len = og->header_len;
88
memcpy (new_og->header, og->header, og->header_len);
89
new_og->body = malloc (og->body_len);
90
new_og->body_len = og->body_len;
91
memcpy (new_og->body, og->body, og->body_len);
97
_ogg_page_free (const ogg_page * og)
101
free ((ogg_page *)og);
106
ominput_delete (OMInput * input)
108
oggz_close (input->reader);
118
omdata = (OMData *) malloc (sizeof (OMData));
120
omdata->inputs = oggz_table_new ();
126
omdata_delete (OMData * omdata)
131
ninputs = oggz_table_size (omdata->inputs);
132
for (i = 0; i < ninputs; i++) {
133
input = (OMInput *) oggz_table_nth (omdata->inputs, i, NULL);
134
ominput_delete (input);
136
oggz_table_delete (omdata->inputs);
142
read_page (OGGZ * oggz, const ogg_page * og, long serialno, void * user_data)
144
OMInput * input = (OMInput *) user_data;
146
input->og = _ogg_page_copy (og);
152
omdata_add_input (OMData * omdata, FILE * infile)
157
input = (OMInput *) malloc (sizeof (OMInput));
158
if (input == NULL) return -1;
160
input->omdata = omdata;
161
input->reader = oggz_open_stdio (infile, OGGZ_READ|OGGZ_AUTO);
164
oggz_set_read_page (input->reader, -1, read_page, input);
166
nfiles = oggz_table_size (omdata->inputs);
167
if (!oggz_table_insert (omdata->inputs, nfiles++, input)) {
168
ominput_delete (input);
176
oggz_merge (OMData * omdata, FILE * outfile)
179
int ninputs, i, min_i;
181
ogg_int64_t units, min_units;
185
while ((ninputs = oggz_table_size (omdata->inputs)) > 0) {
190
/* Reload all pages, and find the min (earliest) */
191
for (i = 0; active && i < oggz_table_size (omdata->inputs); i++) {
192
input = (OMInput *) oggz_table_nth (omdata->inputs, i, &key);
194
if (input->og == NULL) {
195
n = oggz_read (input->reader, READ_SIZE);
197
oggz_table_remove (omdata->inputs, key);
198
ominput_delete (input);
202
if (input && input->og) {
203
if (ogg_page_bos ((ogg_page *)input->og)) {
207
units = oggz_tell_units (input->reader);
208
if (min_units == -1 || units == 0 ||
209
(units > -1 && units < min_units)) {
217
/* Write the earliest page */
219
input = (OMInput *) oggz_table_nth (omdata->inputs, min_i, &key);
221
fwrite (og->header, 1, og->header_len, outfile);
222
fwrite (og->body, 1, og->body_len, outfile);
233
main (int argc, char * argv[])
235
int show_version = 0;
239
char * infilename = NULL, * outfilename = NULL;
240
FILE * infile = NULL, * outfile = NULL;
253
omdata = omdata_new();
256
char * optstring = "hvo:";
258
#ifdef HAVE_GETOPT_LONG
259
static struct option long_options[] = {
260
{"help", no_argument, 0, 'h'},
261
{"version", no_argument, 0, 'v'},
262
{"output", required_argument, 0, 'o'},
266
i = getopt_long (argc, argv, optstring, long_options, NULL);
268
i = getopt (argc, argv, optstring);
280
case 'v': /* version */
283
case 'o': /* output */
284
outfilename = optarg;
292
printf ("%s version " VERSION "\n", progname);
299
if (show_version || show_help) {
303
if (optind >= argc) {
308
if (optind >= argc) {
313
while (optind < argc) {
314
infilename = argv[optind++];
315
infile = fopen (infilename, "rb");
316
if (infile == NULL) {
317
fprintf (stderr, "%s: unable to open input file %s\n", progname,
320
omdata_add_input (omdata, infile);
324
if (outfilename == NULL) {
327
outfile = fopen (outfilename, "wb");
328
if (outfile == NULL) {
329
fprintf (stderr, "%s: unable to open output file %s\n",
330
progname, outfilename);
335
oggz_merge (omdata, outfile);
338
omdata_delete (omdata);
342
omdata_delete (omdata);