~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2004-08-29 10:53:42 UTC
  • Revision ID: james.westby@ubuntu.com-20040829105342-qgmnry37eadfkoxx
Tags: upstream-1.1.3
ImportĀ upstreamĀ versionĀ 1.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * main.c,
 
3
 *
 
4
 * Copyright (C) 1997,98 Rasca, Berlin
 
5
 * Copyright (C) 2003,04 Karl, Frankfurt
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 */
 
21
 
 
22
#include "../config.h"  /* autoconf output */
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <getopt.h>
 
27
#include <signal.h>
 
28
#include <X11/Intrinsic.h>
 
29
#include <X11/StringDefs.h>
 
30
#include <X11/Shell.h>
 
31
#ifdef HAVE_SHMAT
 
32
#include <X11/extensions/XShm.h>
 
33
#endif // HAVE_SHMAT
 
34
#ifdef HasDGA
 
35
#include <X11/extensions/xf86dga.h>
 
36
#endif //HasDGA
 
37
#ifdef HasVideo4Linux
 
38
#include <video.h>
 
39
#endif //HasVideo4Linux
 
40
#include "main.h"
 
41
#include "control.h"
 
42
#include "frame.h"
 
43
#include "codecs.h"
 
44
#include "job.h"
 
45
 
 
46
static AppData app;
 
47
 
 
48
void
 
49
usage(char *prog) {
 
50
    printf("Usage: %s, ver %s, (c) rasca, berlin 1997,98,99, khb (c) 2003\n", prog, VERSION);
 
51
    printf("[--fps #.#]         frames per second (float)\n");
 
52
    printf("[--verbose #]       verbose level, '-v' is --verbose 1\n");
 
53
    printf("[--time #.#]        time to record in seconds (float)\n");
 
54
    printf("[--frames #]        frames to record, don't use it with --time\n");
 
55
    printf("[--continue [yes|no]]        autocontinue after maximum frames/time\n");
 
56
    printf("[--cap_geometry #x#[+#+#]]    size of the capture window (WIDTHxHEIGHT+X+Y)\n");
 
57
    printf("[--start_no #]      start number for the file names\n");
 
58
    #ifdef HAVE_LIBZ
 
59
    printf("[--compress <0-9>]  save the frames compressed, '.gz' is added\n");
 
60
    #endif
 
61
    printf("[--use_clone]       use the CLON chunk for MNGs if needed\n");
 
62
    printf("[--expand_to_24bit] expand 15/16bpp to 24bpp (only for PPM)\n");
 
63
    #ifdef HAVE_LIBJPEG
 
64
    printf("[--quality #]       quality value for JPEG files (100 - 0)\n");
 
65
    #endif
 
66
    printf("[--source <src>]    select input source: x11, shm\n");
 
67
    printf("[--file <file>]     file pattern, e.g. out%%03d.xwd\n");
 
68
    printf("[--gui [yes|no]]    turn on/off gui\n");
 
69
#ifdef HAVE_FFMPEG_AUDIO
 
70
    printf("[--audio [yes|no]]  turn on/off audio capture\n");
 
71
    printf("[--audio_in <src>]  specify audio input device or '-' for pipe input\n");
 
72
    printf("[--audio_rate #]    sample rate for audio capture\n");
 
73
    printf("[--audio_bits #]    bit rate for audio capture\n");
 
74
    printf("[--audio_channels #]    number of audio channels\n");
 
75
#endif // HAVE_FFMPEG_AUDIO
 
76
    printf("Supported output formats:\n");
 
77
    printf(" X window dump              (.xwd)\n");
 
78
    printf(" Portable anymap            (.pnm, .pgm, .ppm)\n");
 
79
    #ifdef HAVE_LIBPNG
 
80
    printf(" Portable network graphics  (.png)\n");
 
81
    printf(" Multiple network graphics  (.mng)\n");
 
82
    #endif
 
83
    #ifdef HAVE_LIBJPEG
 
84
    printf(" Joint picture expert group (.jpg, .jpeg)\n");
 
85
    #endif
 
86
    printf(" MPEG                       (.mpeg)\n");
 
87
    exit(1);
 
88
}
 
89
 
 
90
 
 
91
void
 
92
cleanup_when_interrupted (int signal) {
 
93
    Job *job = job_ptr();
 
94
    
 
95
    XVC_StopCapture (job);
 
96
}
 
97
 
 
98
 
 
99
/*
 
100
 * main()
 
101
 */
 
102
int
 
103
main(int argc, char **argv) {
 
104
 
 
105
    struct option options[] = {
 
106
        {"fps",      required_argument, NULL, 0},
 
107
        {"file",     required_argument, NULL, 0},
 
108
        {"verbose",  required_argument, NULL, 0},
 
109
        {"time",     required_argument, NULL, 0},
 
110
        {"frames",   required_argument, NULL, 0},
 
111
        {"cap_geometry", required_argument, NULL, 0},
 
112
        {"start_no", required_argument, NULL, 0},
 
113
        {"compress", required_argument, NULL, 0},
 
114
        {"use_clone", no_argument,              NULL, 0},
 
115
        {"expand_to_24bit", no_argument, NULL, 0},
 
116
        {"quality",  required_argument, NULL, 0},
 
117
        {"bpp",          required_argument, NULL, 0},
 
118
        {"source",       required_argument, NULL, 0},
 
119
        {"sync",         no_argument,           NULL, 0},
 
120
        {"step",         required_argument,     NULL, 0},
 
121
        {"gui",     optional_argument, NULL, 0},
 
122
        {"audio",     optional_argument, NULL, 0},
 
123
        {"audio_in", required_argument, NULL, 0},
 
124
        {"audio_rate", required_argument, NULL, 0},
 
125
        {"audio_bits", required_argument, NULL, 0},
 
126
        {"audio_channels", required_argument, NULL, 0},
 
127
        {"continue", optional_argument, NULL, 0},
 
128
        {NULL, 0, NULL, 0},
 
129
    };
 
130
 
 
131
    int opt_index = 0, c, resultCode; //, flags =0;
 
132
    #ifdef HasDGA
 
133
    int dga_evb, dga_errb;
 
134
    #endif //HasDGA
 
135
    Job main_job;
 
136
    
 
137
    app.cap_pos_x = app.cap_pos_y = -1;
 
138
    
 
139
    app.flags = 0;
 
140
    app.use_clone = 0;
 
141
    app.device = "/dev/video0";
 
142
    
 
143
    app.quality = 99;
 
144
    app.source = NULL;
 
145
    app.step = 1;
 
146
    app.bpp = 0;
 
147
    app.targetCodec = CODEC_MPEG1;
 
148
    app.mouseWanted = 1;
 
149
    
 
150
    app.snddev = "/dev/dsp";
 
151
    app.sndrate = 22050;
 
152
    app.sndsize = 32000;
 
153
    app.sndchannels = 1; 
 
154
//    app.flags |= FLG_AUDIO_WANTED;
 
155
    
 
156
    // this is a hook for a GUI to do some pre-init functions ...
 
157
    // possibly to set some fallback options read from a rc file or Xdefaults
 
158
    if (! XVC_PreInit( argc, argv, &app ) ) {
 
159
        fprintf(stderr, "Can't do GUI pre_init ... aborting\n");
 
160
        exit(2);
 
161
    }
 
162
    
 
163
    /* read options file now */
 
164
    XVC_ReadOptionsFile( &app );
 
165
    
 
166
    if (app.source) {
 
167
        if (strstr(app.source, "v4l") != NULL) {
 
168
            app.flags |= FLG_USE_V4L;
 
169
            if (strchr(app.source, ':') != NULL) {
 
170
                app.device = strchr(app.source, ':') + 1;
 
171
            }
 
172
        } else if (strstr(app.source, "dga") != NULL)
 
173
            app.flags |= FLG_USE_DGA;
 
174
        else if (strstr(app.source, "shm") != NULL)
 
175
            app.flags |= FLG_USE_SHM;
 
176
    }
 
177
    while ((c = getopt_long(argc, argv, "v", options, &opt_index)) != -1) {
 
178
        switch (c) {
 
179
            case 0: /* is a long option */
 
180
                switch (opt_index) {
 
181
                    case 0: /* frames per second */
 
182
                        app.fps = atof(optarg);
 
183
                        break;
 
184
                    case 1: /* file for saving */
 
185
                        app.file = strdup(optarg);
 
186
                        break;
 
187
                    case 2:
 
188
                        app.verbose = atoi(optarg);
 
189
                        break;
 
190
                    case 3: /* time to record */
 
191
                        app.time = atof(optarg);
 
192
                        if ( app.time != 0 ) app.frames = 0;
 
193
                        break;
 
194
                    case 4:
 
195
                        app.frames = atoi(optarg);
 
196
                        if ( app.frames != 0 ) app.time = 0;
 
197
                        break;
 
198
                    case 5: /* cap_geometry */
 
199
                        sscanf(optarg, "%dx%d+%d+%d", &app.cap_width, &app.cap_height, &app.cap_pos_x, &app.cap_pos_y);
 
200
                        break;
 
201
                    case 6:
 
202
                        app.start_no = atoi(optarg);
 
203
                        break;
 
204
                    case 7:
 
205
                        app.compress = atoi(optarg);
 
206
                        break;
 
207
                    case 8:
 
208
                        app.use_clone = 1;
 
209
                        break;
 
210
                    case 9:
 
211
                        app.flags |= FLG_EXPAND_TO_24BIT;
 
212
                        break;
 
213
                    case 10:
 
214
                        app.quality = atoi(optarg);
 
215
                        if ((app.quality < 0) || (app.quality > 100))
 
216
                            app.quality = 75; /* reset to default */
 
217
                        break;
 
218
                    case 11:
 
219
                        app.bpp = atoi(optarg);
 
220
                        break;
 
221
                    case 12:
 
222
                        app.source = strdup(optarg);
 
223
                        if (strstr(optarg, "v4l") != NULL) {
 
224
                            app.flags &= ~FLG_SOURCE;
 
225
                            app.flags |= FLG_USE_V4L;
 
226
                            if (strchr(optarg, ':') != NULL) {
 
227
                                app.device = strchr(optarg, ':')+1;
 
228
                            }
 
229
                        } else if (strstr(optarg, "dga") != NULL) {
 
230
                            app.flags &= ~FLG_SOURCE;
 
231
                            app.flags |= FLG_USE_DGA;
 
232
                        } else if (strstr(optarg, "shm") != NULL) {
 
233
                            app.flags &= ~FLG_SOURCE;
 
234
                            app.flags |= FLG_USE_SHM;
 
235
                        } else {
 
236
                            app.flags &= ~FLG_SOURCE;
 
237
                            printf("using normal x server access ..\n");
 
238
                        }
 
239
                        break;
 
240
                    case 13:
 
241
                        app.flags |= FLG_SYNC;
 
242
                        break;
 
243
                    case 14:
 
244
                        app.step = atoi(optarg);
 
245
                        break;
 
246
                    case 15: // gui
 
247
                        {
 
248
                            char *tmp;
 
249
                            if (! optarg) {
 
250
                                if (optind < argc) {
 
251
                                    tmp = (argv[optind][0] == '-')?"yes":argv[optind++];
 
252
                                } else {
 
253
                                    tmp = "yes";
 
254
                                }
 
255
                            } else {
 
256
                                tmp = strdup(optarg);
 
257
                            }
 
258
                            if (strstr(tmp, "no") != NULL) {
 
259
                                app.flags |= FLG_NOGUI;
 
260
                            } else {
 
261
                                app.flags &= ~FLG_NOGUI;
 
262
                            }
 
263
                        }
 
264
                        break;
 
265
                    case 16: //audio
 
266
                        {
 
267
                            char *tmp;
 
268
                            if (! optarg) {
 
269
                                if (optind < argc) {
 
270
                                    tmp = (argv[optind][0] == '-')?"yes":argv[optind++];
 
271
                                } else {
 
272
                                    tmp = "yes";
 
273
                                }
 
274
                            } else {
 
275
                                tmp = strdup(optarg);
 
276
                            }
 
277
                            if (strstr(tmp, "no") != NULL) {
 
278
                                app.flags &= ~FLG_AUDIO_WANTED;
 
279
                            } else {
 
280
                                app.flags |= FLG_AUDIO_WANTED;
 
281
                            }
 
282
                        }
 
283
                        break;
 
284
                    case 17: //audio_in
 
285
                        app.flags |= FLG_AUDIO_WANTED;
 
286
                        app.snddev = strdup(optarg);
 
287
                        break;
 
288
                    case 18: //audio_rate
 
289
                        app.flags |= FLG_AUDIO_WANTED;
 
290
                        app.sndrate = atoi(optarg);
 
291
                        break;
 
292
                    case 19: //audio_bits
 
293
                        app.flags |= FLG_AUDIO_WANTED;
 
294
                        app.sndsize = atoi(optarg);
 
295
                        break;
 
296
                    case 20: //audio_channels
 
297
                        app.flags |= FLG_AUDIO_WANTED;
 
298
                        app.sndchannels = atoi(optarg);
 
299
                        break;
 
300
                    case 21: //continue
 
301
                        {
 
302
                            char *tmp;
 
303
                            if (! optarg) {
 
304
                                if (optind < argc) {
 
305
                                    tmp = (argv[optind][0] == '-')?"yes":argv[optind++];
 
306
                                } else {
 
307
                                    tmp = "yes";
 
308
                                }
 
309
                            } else {
 
310
                                tmp = strdup(optarg);
 
311
                            }
 
312
                            if (strstr(tmp, "no") != NULL) {
 
313
                                app.flags &= ~FLG_AUTO_CONTINUE;
 
314
                            } else {
 
315
                                app.flags |= FLG_AUTO_CONTINUE;
 
316
                            }
 
317
                        }
 
318
                        break;
 
319
                    default:
 
320
                        usage(argv[0]);
 
321
                        break;
 
322
                }
 
323
                break;
 
324
                
 
325
            case 'v':
 
326
                app.verbose++;
 
327
                break;
 
328
                
 
329
            default:
 
330
                usage(argv[0]);
 
331
                break;
 
332
        }
 
333
    }
 
334
    if (argc != optind)
 
335
        usage(argv[0]);
 
336
    
 
337
    if (app.verbose)
 
338
        app.flags |= FLG_RUN_VERBOSE;
 
339
    if (app.use_clone)
 
340
        app.flags |= FLG_MNG_CLONE;
 
341
    
 
342
    if (app.flags & FLG_USE_V4L) {
 
343
        #ifndef HasVideo4Linux
 
344
        app.flags &= ~FLG_USE_V4L;
 
345
        #else //HasVideo4Linux
 
346
        VIDEO *video;
 
347
        
 
348
        video = video_open(app.device, O_RDWR);
 
349
        if (!video)
 
350
            exit(3);
 
351
        if (app.verbose) {
 
352
            printf("%s:\n cctm=%d channels=%d audios=%d maxwidth=%d "
 
353
            "maxheight=%d minwidth=%d minheight=%d\n",
 
354
            video->name, video->type&VID_TYPE_CAPTURE,
 
355
            video->channels, video->audios, video->maxwidth,
 
356
            video->maxheight, video->minwidth, video->minheight);
 
357
        }
 
358
        if (!(video->type & VID_TYPE_CAPTURE)) {
 
359
            fprintf(stderr, "video device can't capture to memory\n");
 
360
            exit(3);
 
361
        }
 
362
        app.cap_width = min(app.cap_width, video->maxwidth);
 
363
        app.cap_height = min(app.cap_height, video->maxheight);
 
364
        app.cap_width = max(app.cap_width, video->minwidth);
 
365
        app.cap_height = max(app.cap_height, video->minheight);
 
366
        video_close(video);
 
367
        #endif //HasVideo4Linux
 
368
    }
 
369
    
 
370
    #ifndef HAVE_LIBZ
 
371
    app.compress = 0;
 
372
    #else //HAVE_LIBZ
 
373
    if (app.compress > 9)
 
374
        app.compress = 9;
 
375
    #endif //HAVE_LIBZ
 
376
    
 
377
 
 
378
    // these are the hooks for a GUI to create the GUI,
 
379
    // the selection frame, and do some initialization ...
 
380
    if ( ! XVC_CreateGUI(&app) ) {
 
381
        fprintf(stderr, "Can't create GUI ... aborting\n");
 
382
        exit(2);
 
383
    }
 
384
    if ( ! XVC_CreateFrame(&app) ) {
 
385
        fprintf(stderr, "Can't create selection Frame ... aborting\n");
 
386
        exit(2);
 
387
    }
 
388
    if ( ! XVC_InitGUI(&app) ) {
 
389
        fprintf(stderr, "Can't initialize GUI ... aborting\n");
 
390
        exit(2);
 
391
    }
 
392
    if ( app.cap_pos_x >= 0 || app.cap_pos_y >= 0 ) {
 
393
        XVC_ChangeFrame ( app.cap_pos_x, app.cap_pos_y, app.cap_width, app.cap_height, TRUE );
 
394
    }
 
395
    
 
396
    
 
397
    if (app.verbose) {
 
398
        char *mp;
 
399
        switch (app.mouseWanted) {
 
400
            case 2:
 
401
                mp = "black";
 
402
                break;
 
403
            case 1:
 
404
                mp = "white";
 
405
                break;
 
406
            default:
 
407
                mp = "none";
 
408
        }
 
409
        
 
410
        printf("Current settings:\n");
 
411
        printf("  frames per second = %f\n", app.fps);
 
412
        printf("  video encoding    = %s\n", tCodecNames[app.targetCodec]);
 
413
        printf("  file pattern      = %s\n", app.file);
 
414
        printf("  verbose level     = %d\n", app.verbose);
 
415
        printf("  frames to store   = %d\n", app.frames);
 
416
        printf("  time to capture   = %f sec\n", app.time);
 
417
        printf("  autocontinue      = %s\n", ((app.flags & FLG_AUTO_CONTINUE)?"yes":"no"));
 
418
        #ifdef HAVE_LIBZ
 
419
        printf("  compression level = %d\n", app.compress);
 
420
        #endif
 
421
        #ifdef HAVE_LIBJPEG
 
422
        printf("  quality (jpeg)    = %d\n", app.quality);
 
423
        #endif
 
424
        printf("  input source      = %s (%d)\n", app.source, app.flags & FLG_SOURCE);
 
425
        printf("  capture pointer   = %s\n", mp);
 
426
#ifdef HAVE_FFMPEG_AUDIO
 
427
        printf("  capture audio     = %s\n", ((app.flags & FLG_AUDIO_WANTED)?"yes":"no"));
 
428
        printf("   - input          = %s\n", app.snddev);
 
429
        printf("   - sample rate    = %i\n", app.sndrate);
 
430
        printf("   - bit rate       = %i\n", app.sndsize);
 
431
        printf("   - channels       = %i\n", app.sndchannels);
 
432
#endif // HAVE_FFMPEG_AUDIO
 
433
        printf("  animate command   = %s\n", app.play_cmd);
 
434
        printf("  make video command= %s\n", app.video_cmd);
 
435
        printf("  edit frame command= %s\n", app.edit_cmd);
 
436
        printf("  help command      = %s\n", app.help_cmd);
 
437
    }
 
438
    
 
439
    signal(SIGINT, cleanup_when_interrupted);
 
440
    
 
441
    // this is a hook for the GUI's main loop
 
442
    resultCode = XVC_RunGUI();
 
443
    return (resultCode);
 
444
}
 
445
 
 
446
 
 
447