Debian Codegraph @

encodedv/insert_audio.c

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