File: insert_audio.c

package info (click to toggle)
libdv 1.0.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 2,848 kB
  • ctags: 1,750
  • sloc: ansic: 14,651; sh: 8,816; asm: 7,250; makefile: 198
file content (331 lines) | stat: -rw-r--r-- 8,320 bytes parent folder | download | duplicates (9)
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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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
/* 
 *  insert_audio.c
 *
 *     Copyright (C) Peter Schlaile - January 2001
 *
 *  This file is part of libdv, a free DV (IEC 61834/SMPTE 314M)
 *  codec.
 *
 *  libdv is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU Lesser Public License as published by
 *  the Free Software Foundation; either version 2.1, or (at your
 *  option) any later version.
 *   
 *  libdv is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser Public License for more details.
 *   
 *  You should have received a copy of the GNU Lesser Public License
 *  along with libdv; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *  The libdv homepage is http://libdv.sourceforge.net/.  
 */

#include "libdv/dv_types.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#include <libdv/headers.h>
#include <libdv/enc_audio_input.h>
#include <libdv/enc_input.h>
#include <libdv/enc_output.h>

void generate_empty_frame(unsigned char* frame_buf, int isPAL)
{
	static time_t t = 0;
	static int frame_count = -1;
	if (!t) {
		t = time(NULL);
	}
	if (frame_count == -1) {
		frame_count = isPAL ? 25 : 30;
	}
	if (!--frame_count) {
		frame_count = isPAL ? 25 : 30;
		t++;
	}
	memset(frame_buf, 0, isPAL ? 144000 : 120000);
	_dv_write_meta_data(frame_buf, 0, isPAL, 0, &t);
}

int read_frame(FILE* in_vid, unsigned char* frame_buf, int * isPAL)
{
	if (fread(frame_buf, 1, 120000, in_vid) != 120000) {
		generate_empty_frame(frame_buf, *isPAL);
		return 0;
	}

	*isPAL = (frame_buf[3] & 0x80);

	if (*isPAL) {
		if (fread(frame_buf + 120000, 1, 144000 - 120000, in_vid) !=
		    144000 - 120000) {
			generate_empty_frame(frame_buf, *isPAL);
			return 0;
		}
	}
	return 1;
}

#define OPT_VERSION         0
#define OPT_VERBOSE         1
#define OPT_AUDIO_INPUT     2
#define OPT_OUTPUT          3
#define OPT_TRUNCATE        4
#define OPT_REPEAT          5
#define OPT_AUTOHELP        6
#define NUM_OPTS            7

int main(int argc, const char** argv)
{
	FILE* in_vid;

	const char* filename = NULL;
	const char* audio_filename = NULL;

	unsigned char frame_buf[144000];
	int isPAL = 1;
	int gotframe = 0;
	int have_pipes = 0;
	int verbose_mode = 0;
	int truncate_mode = 0;
	int repeat_count = 0;
	int count;
	const char* audio_input_filter_str = "wav";
	const char* output_filter_str = "raw";

	dv_enc_audio_info_t audio_info_;
	dv_enc_audio_info_t* audio_info;

	dv_enc_audio_input_filter_t * audio_input_filter;
	dv_enc_output_filter_t * output_filter;

	time_t now;

#if HAVE_LIBPOPT
	struct poptOption option_table[NUM_OPTS+1]; 
	int rc;             /* return code from popt */
	poptContext optCon; /* context for parsing command-line options */
	option_table[OPT_VERSION] = (struct poptOption) {
		longName: "version", 
		val: 'v',
		descrip: "show dubdv version number"
	}; /* version */

	option_table[OPT_VERBOSE] = (struct poptOption) {
		longName:   "verbose", 
		shortName:  'v', 
		arg:        &verbose_mode,
		descrip:    "show encoder statistics / status information"
	}; /* verbose mode */

	option_table[OPT_AUDIO_INPUT] = (struct poptOption) {
		longName:   "audio-input", 
		shortName:  'a', 
		arg:        &audio_input_filter_str,
		argInfo:    POPT_ARG_STRING, 
		descrip:    "choose audio-input-filter [none, >wav<, dsp]"
	}; /* audio input */

	option_table[OPT_OUTPUT] = (struct poptOption) {
		longName:   "output", 
		shortName:  'o', 
		arg:        &output_filter_str,
		argInfo:    POPT_ARG_STRING, 
		descrip:    "choose output-filter [>raw<]"
	}; /* output */

 	option_table[OPT_TRUNCATE] = (struct poptOption) {
 		longName:   "truncate", 
 		shortName:  't', 
 		arg:        &truncate_mode,
 		descrip:    "truncate output at end of input"
 	}; /* truncate */

 	option_table[OPT_REPEAT] = (struct poptOption) {
 		longName:   "repeat", 
 		shortName:  'r', 
 		arg:        &repeat_count,
 		argInfo:    POPT_ARG_INT,
 		descrip:    "repeat the first video frame this many times"
 	}; /* repeat */

	option_table[OPT_AUTOHELP] = (struct poptOption) {
		argInfo: POPT_ARG_INCLUDE_TABLE,
		arg:     poptHelpOptions,
		descrip: "Help options",
	}; /* autohelp */

	option_table[NUM_OPTS] = (struct poptOption) { 
		NULL, 0, 0, NULL, 0 };

	optCon = poptGetContext(NULL, argc, 
				(const char **)argv, option_table, 0);
	poptSetOtherOptionHelp(optCon, 
		"<audio input> "
		"[<filename or - for stdin>]");

	while ((rc = poptGetNextOpt(optCon)) > 0) {
		switch (rc) {
		case 'v':
			fprintf(stderr,"insert_audio: version 0.0.2 "
				"http://libdv.sourceforge.net/\n");
			exit(0);
			break;
		default:
			break;
		} /* switch */
	} /* while */

	if (rc < -1) {
		/* an error occurred during option processing */
		fprintf(stderr, "%s: %s\n",
			poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
			poptStrerror(rc));
		exit(-1);
	}

	audio_filename = poptGetArg(optCon);
	if(audio_filename == NULL) {
		poptPrintUsage(optCon, stderr, 0);
		fprintf(stderr, 
			"\nYou have to specify at least a audio input file\n");
		exit(-1);
	}
	filename = poptGetArg(optCon);
	poptFreeContext(optCon);

#else
#warning dubdv does not work without libpopt!

#endif

	if (strcmp(audio_input_filter_str, "none") != 0) {
		dv_enc_get_audio_input_filters(&audio_input_filter, &count);
		while (count 
		       && strcmp(audio_input_filter->filter_name, 
				 audio_input_filter_str) != 0) {
			audio_input_filter++;
			count--;
		}
		if (!count) {
			fprintf(stderr, 
				"Unknown audio input filter selected: %s!\n"
				"The following filters are supported:\n",
				audio_input_filter_str);
			dv_enc_get_audio_input_filters(&audio_input_filter, 
						       &count);
			while (count--) {
				fprintf(stderr, "%s\n", 
					audio_input_filter->filter_name);
				audio_input_filter++;
			}
			return(-1);
		}
		if (!audio_filename) {
			fprintf(stderr, "Audio input selected but no filename "
				"or device given!\n");
			return(-1);
		}
	}

	dv_enc_get_output_filters(&output_filter, &count);
	while (count && 
	       strcmp(output_filter->filter_name, output_filter_str) != 0) {
		output_filter++;
		count--;
	}
	if (!count) {
		fprintf(stderr, "Unknown output filter selected: %s!\n"
			"The following filters are supported:\n",
			output_filter_str);
		dv_enc_get_output_filters(&output_filter, &count);
		while (count--) {
			fprintf(stderr, "%s\n", output_filter->filter_name);
			output_filter++;
		}
		return(-1);
	}

	audio_info = &audio_info_;
	if (audio_input_filter->init(audio_filename, audio_info) != 0) {
		return(-1);
	}
	if (verbose_mode) {
		fprintf(stderr, "Opening audio source with:\n"
			"Channels: %d\n"
			"Frequency: %d\n"
			"Bytes per second: %d\n"
			"Byte alignment: %d\n"
			"Bits per sample: %d\n",
			audio_info->channels, audio_info->frequency, 
			audio_info->bytespersecond,
			audio_info->bytealignment, 
			audio_info->bitspersample);
	}

	have_pipes = (filename == NULL || strcmp(filename, "-") == 0);

	if (have_pipes) {
		if (output_filter->init(stdout) != 0) {
			return -1;
		}
		in_vid = stdin;
	} else {
		FILE* out_vid = fopen(filename, "r+");
		in_vid = fopen(filename, "r");
		if (output_filter->init(out_vid) != 0) {
			return -1;
		}

	}

	now = time(NULL);

 	/* repeat the first frame if needed */
 	gotframe = read_frame(in_vid, frame_buf, &isPAL);
 	if (gotframe)
		while (repeat_count--)
			if (output_filter->store(frame_buf, audio_info, 1,
 						isPAL, 0, now) != 0)
 				return -1;
 
	for (;;) {
		if (!gotframe && truncate_mode)
			return 0; /* truncate audio at end of video */

		if (audio_input_filter->load(audio_info, isPAL) != 0) {
			/* end of audio stream */
			if (!truncate_mode) {
				/* copy the rest with original audio */
				while (gotframe) {
					fwrite(frame_buf, 1,
					       isPAL ? 144000 : 120000,
					       stdout);
					gotframe = read_frame(
						in_vid, frame_buf, &isPAL);
				}
			}
			return 0;
		}
		if (output_filter->store(frame_buf, audio_info, 1,
					 isPAL, 0, now) != 0) {
			return -1;
		}
		gotframe = read_frame(in_vid, frame_buf, &isPAL);
	}
}