Debian Codegraph @

encodedv/insert_audio.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * insert_audio.c
3 *
4 * Copyright (C) Peter Schlaile - January 2001
5 *
6 * This file is part of libdv, a free DV (IEC 61834/SMPTE 314M)
7 * codec.
8 *
9 * libdv is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser Public License as published by
11 * the Free Software Foundation; either version 2.1, or (at your
12 * option) any later version.
13 *
14 * libdv is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser Public License
20 * along with libdv; see the file COPYING. If not, write to
21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * The libdv homepage is http://libdv.sourceforge.net/.
24 */
25
26 #include "libdv/dv_types.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <time.h>
37
38 #include <libdv/headers.h>
39 #include <libdv/enc_audio_input.h>
40 #include <libdv/enc_input.h>
41 #include <libdv/enc_output.h>
42
43 void generate_empty_frame(unsigned char* frame_buf, int isPAL)
44 {
45 static time_t t = 0;
46 static int frame_count = -1;
47 if (!t) {
48 t = time(NULL);
49 }
50 if (frame_count == -1) {
51 frame_count = isPAL ? 25 : 30;
52 }
53 if (!--frame_count) {
54 frame_count = isPAL ? 25 : 30;
55 t++;
56 }
57 memset(frame_buf, 0, isPAL ? 144000 : 120000);
58 _dv_write_meta_data(frame_buf, 0, isPAL, 0, &t);
59 }
60
61 int read_frame(FILE* in_vid, unsigned char* frame_buf, int * isPAL)
62 {
63 if (fread(frame_buf, 1, 120000, in_vid) != 120000) {
64 generate_empty_frame(frame_buf, *isPAL);
65 return 0;
66 }
67
68 *isPAL = (frame_buf[3] & 0x80);
69
70 if (*isPAL) {
71 if (fread(frame_buf + 120000, 1, 144000 - 120000, in_vid) !=
72 144000 - 120000) {
73 generate_empty_frame(frame_buf, *isPAL);
74 return 0;
75 }
76 }
77 return 1;
78 }
79
80 #define OPT_VERSION 0
81 #define OPT_VERBOSE 1
82 #define OPT_AUDIO_INPUT 2
83 #define OPT_OUTPUT 3
84 #define OPT_TRUNCATE 4
85 #define OPT_REPEAT 5
86 #define OPT_AUTOHELP 6
87 #define NUM_OPTS 7
88
89 int main(int argc, const char** argv)
90 {
91 FILE* in_vid;
92
93 const char* filename = NULL;
94 const char* audio_filename = NULL;
95
96 unsigned char frame_buf[144000];
97 int isPAL = 1;
98 int gotframe = 0;
99 int have_pipes = 0;
100 int verbose_mode = 0;
101 int truncate_mode = 0;
102 int repeat_count = 0;
103 int count;
104 const char* audio_input_filter_str = "wav";
105 const char* output_filter_str = "raw";
106
107 dv_enc_audio_info_t audio_info_;
108 dv_enc_audio_info_t* audio_info;
109
110 dv_enc_audio_input_filter_t * audio_input_filter;
111 dv_enc_output_filter_t * output_filter;
112
113 time_t now;
114
115 #if HAVE_LIBPOPT
116 struct poptOption option_table[NUM_OPTS+1];
117 int rc; /* return code from popt */
118 poptContext optCon; /* context for parsing command-line options */
119 option_table[OPT_VERSION] = (struct poptOption) {
120 longName: "version",
121 val: 'v',
122 descrip: "show dubdv version number"
123 }; /* version */
124
125 option_table[OPT_VERBOSE] = (struct poptOption) {
126 longName: "verbose",
127 shortName: 'v',
128 arg: &verbose_mode,
129 descrip: "show encoder statistics / status information"
130 }; /* verbose mode */
131
132 option_table[OPT_AUDIO_INPUT] = (struct poptOption) {
133 longName: "audio-input",
134 shortName: 'a',
135 arg: &audio_input_filter_str,
136 argInfo: POPT_ARG_STRING,
137 descrip: "choose audio-input-filter [none, >wav<, dsp]"
138 }; /* audio input */
139
140 option_table[OPT_OUTPUT] = (struct poptOption) {
141 longName: "output",
142 shortName: 'o',
143 arg: &output_filter_str,
144 argInfo: POPT_ARG_STRING,
145 descrip: "choose output-filter [>raw<]"
146 }; /* output */
147
148 option_table[OPT_TRUNCATE] = (struct poptOption) {
149 longName: "truncate",
150 shortName: 't',
151 arg: &truncate_mode,
152 descrip: "truncate output at end of input"
153 }; /* truncate */
154
155 option_table[OPT_REPEAT] = (struct poptOption) {
156 longName: "repeat",
157 shortName: 'r',
158 arg: &repeat_count,
159 argInfo: POPT_ARG_INT,
160 descrip: "repeat the first video frame this many times"
161 }; /* repeat */
162
163 option_table[OPT_AUTOHELP] = (struct poptOption) {
164 argInfo: POPT_ARG_INCLUDE_TABLE,
165 arg: poptHelpOptions,
166 descrip: "Help options",
167 }; /* autohelp */
168
169 option_table[NUM_OPTS] = (struct poptOption) {
170 NULL, 0, 0, NULL, 0 };
171
172 optCon = poptGetContext(NULL, argc,
173 (const char **)argv, option_table, 0);
174 poptSetOtherOptionHelp(optCon,
175 "<audio input> "
176 "[<filename or - for stdin>]");
177
178 while ((rc = poptGetNextOpt(optCon)) > 0) {
179 switch (rc) {
180 case 'v':
181 fprintf(stderr,"insert_audio: version 0.0.2 "
182 "http://libdv.sourceforge.net/\n");
183 exit(0);
184 break;
185 default:
186 break;
187 } /* switch */
188 } /* while */
189
190 if (rc < -1) {
191 /* an error occurred during option processing */
192 fprintf(stderr, "%s: %s\n",
193 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
194 poptStrerror(rc));
195 exit(-1);
196 }
197
198 audio_filename = poptGetArg(optCon);
199 if(audio_filename == NULL) {
200 poptPrintUsage(optCon, stderr, 0);
201 fprintf(stderr,
202 "\nYou have to specify at least a audio input file\n");
203 exit(-1);
204 }
205 filename = poptGetArg(optCon);
206 poptFreeContext(optCon);
207
208 #else
209 #warning dubdv does not work without libpopt!
210
211 #endif
212
213 if (strcmp(audio_input_filter_str, "none") != 0) {
214 dv_enc_get_audio_input_filters(&audio_input_filter, &count);
215 while (count
216 && strcmp(audio_input_filter->filter_name,
217 audio_input_filter_str) != 0) {
218 audio_input_filter++;
219 count--;
220 }
221 if (!count) {
222 fprintf(stderr,
223 "Unknown audio input filter selected: %s!\n"
224 "The following filters are supported:\n",
225 audio_input_filter_str);
226 dv_enc_get_audio_input_filters(&audio_input_filter,
227 &count);
228 while (count--) {
229 fprintf(stderr, "%s\n",
230 audio_input_filter->filter_name);
231 audio_input_filter++;
232 }
233 return(-1);
234 }
235 if (!audio_filename) {
236 fprintf(stderr, "Audio input selected but no filename "
237 "or device given!\n");
238 return(-1);
239 }
240 }
241
242 dv_enc_get_output_filters(&output_filter, &count);
243 while (count &&
244 strcmp(output_filter->filter_name, output_filter_str) != 0) {
245 output_filter++;
246 count--;
247 }
248 if (!count) {
249 fprintf(stderr, "Unknown output filter selected: %s!\n"
250 "The following filters are supported:\n",
251 output_filter_str);
252 dv_enc_get_output_filters(&output_filter, &count);
253 while (count--) {
254 fprintf(stderr, "%s\n", output_filter->filter_name);
255 output_filter++;
256 }
257 return(-1);
258 }
259
260 audio_info = &audio_info_;
261 if (audio_input_filter->init(audio_filename, audio_info) != 0) {
262 return(-1);
263 }
264 if (verbose_mode) {
265 fprintf(stderr, "Opening audio source with:\n"
266 "Channels: %d\n"
267 "Frequency: %d\n"
268 "Bytes per second: %d\n"
269 "Byte alignment: %d\n"
270 "Bits per sample: %d\n",
271 audio_info->channels, audio_info->frequency,
272 audio_info->bytespersecond,
273 audio_info->bytealignment,
274 audio_info->bitspersample);
275 }
276
277 have_pipes = (filename == NULL || strcmp(filename, "-") == 0);
278
279 if (have_pipes) {
280 if (output_filter->init(stdout) != 0) {
281 return -1;
282 }
283 in_vid = stdin;
284 } else {
285 FILE* out_vid = fopen(filename, "r+");
286 in_vid = fopen(filename, "r");
287 if (output_filter->init(out_vid) != 0) {
288 return -1;
289 }
290
291 }
292
293 now = time(NULL);
294
295 /* repeat the first frame if needed */
296 gotframe = read_frame(in_vid, frame_buf, &isPAL);
297 if (gotframe)
298 while (repeat_count--)
299 if (output_filter->store(frame_buf, audio_info, 1,
300 isPAL, 0, now) != 0)
301 return -1;
302
303 for (;;) {
304 if (!gotframe && truncate_mode)
305 return 0; /* truncate audio at end of video */
306
307 if (audio_input_filter->load(audio_info, isPAL) != 0) {
308 /* end of audio stream */
309 if (!truncate_mode) {
310 /* copy the rest with original audio */
311 while (gotframe) {
312 fwrite(frame_buf, 1,
313 isPAL ? 144000 : 120000,
314 stdout);
315 gotframe = read_frame(
316 in_vid, frame_buf, &isPAL);
317 }
318 }
319 return 0;
320 }
321 if (output_filter->store(frame_buf, audio_info, 1,
322 isPAL, 0, now) != 0) {
323 return -1;
324 }
325 gotframe = read_frame(in_vid, frame_buf, &isPAL);
326 }
327 }
328
329
330
331