Debian Codegraph @

samples/qtvidcap/avirec.cpp

raw
1 /* avirec
2 Copyright (C) 1999-2001 Oliver Kurth
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: avirec.cpp,v 1.31 2009/03/31 20:12:43 kabi Exp $ */
20
21
22 #include <avifile.h>
23 #include <videoencoder.h>
24 #include <avm_creators.h>
25 #include <avm_except.h>
26 #include <avm_cpuinfo.h>
27
28 #include "v4lxif.h"
29 #include "capproc.h"
30
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE
33 #endif
34 #include <signal.h> // sighandler_t
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <unistd.h>
40
41 #include <getopt.h>
42
43 #ifdef HAVE_SYSINFO
44 #include <sys/sysinfo.h>
45 #endif
46
47
48 using namespace std;
49
50 // for now same name as avicap to share settings
51 const char* g_pcProgramName = "AviCap"; //"AviRec";
52 unsigned int m_iMemory;
53
54
55 struct VRConf
56 {
57 avm::string codec_string;
58 int attr_count;
59 char *attributes[10];
60 int width;
61 int height;
62 int over_l;
63 int over_r;
64 int over_t;
65 int over_b;
66 int quality;
67 int keyframes;
68 int audio_channels;
69 int audio_compressor;
70 int audio_bitrate;
71 int timelimit;
72 float fps;
73 bool deinterlace;
74 bool rgb;
75 int norm;
76 int input;
77 const char *device;
78 const char *audio_device;
79 };
80
81 static const struct STRTAB norms_bttv[] = {
82 { 0, "PAL" },
83 { 1, "NTSC" },
84 { 2, "SECAM" },
85 { 3, "PAL-NC" },
86 { 4, "PAL-M" },
87 { 5, "PAL-N" },
88 { 6, "NTSC-JP" },
89 { -1, NULL }
90 };
91
92
93 static struct VRConf conf = {
94 "",
95 0,
96 { 0 },
97 384,
98 288,
99 0,
100 0,
101 0,
102 0,
103 95,
104 15,
105 2,
106 1, // PCM
107 0,
108 60,
109 25,
110 false,
111 false,
112 -1,
113 0,
114 V4L_DEVICE,
115 "/dev/dsp",
116 };
117
118 volatile int sig_int_seen = 0;
119 static void sig_int(int)
120 {
121 sig_int_seen = 1;
122 }
123
124 avm::vector<avm::CodecInfo>::iterator GetCodecID(const avm::string& name)
125 {
126 avm::vector<avm::CodecInfo>::iterator it;
127 int i=0;
128 int sel=0;
129
130 for (it = video_codecs.begin(); it != video_codecs.end(); it++)
131 {
132 if(!(it->direction & avm::CodecInfo::Encode))
133 continue;
134
135 if(name == it->GetName()) break;
136 }
137 return it;
138 }
139
140 void ListCodecs(avm::vector<avm::CodecInfo>& codec_list)
141 {
142 avm::vector<avm::CodecInfo>::iterator it;
143 int i=0;
144 int sel=0;
145
146 for (it = codec_list.begin(); it != codec_list.end(); it++)
147 {
148 if ( it->kind == avm::CodecInfo::DShow_Dec)
149 continue; //probably not a usable codec..
150
151 if(!(it->direction & avm::CodecInfo::Encode))
152 continue;
153
154 printf("%s\n", it->GetName());
155
156 avm::vector<avm::AttributeInfo> encinfo = it->encoder_info;
157 avm::vector<avm::AttributeInfo>::const_iterator inf_it;
158
159 for(inf_it = encinfo.begin(); inf_it != encinfo.end(); inf_it++)
160 {
161 switch(inf_it->kind)
162 {
163 case avm::AttributeInfo::Integer:
164 {
165 int defval;
166 if (avm::CodecGetAttr(*it, inf_it->GetName(), &defval) == 0)
167 printf(" %s=%d\n", inf_it->GetName(), defval);
168 else
169 printf(" %s=(no default)\n", inf_it->GetName());
170 }
171 break;
172 case avm::AttributeInfo::Select:
173 {
174 int defval;
175 avm::vector<avm::string>::const_iterator sit;
176 avm::CodecGetAttr(*it, inf_it->GetName(), &defval);
177 printf(" %s = ", inf_it->GetName());
178 printf(" %s ", (defval < (int)inf_it->options.size()) ?
179 inf_it->options[defval].c_str() : "unknown");
180
181 printf(" (");
182 for (sit = (inf_it->options).begin(); sit != (inf_it->options).end(); sit++)
183 printf("%s ", sit->c_str());
184
185 printf(")\n");
186 }
187 break;
188 case avm::AttributeInfo::String:
189 {
190 const char* def_str;
191 avm::CodecGetAttr(*it, it->GetName(), &def_str);
192 printf(" %s = '%s'\n", inf_it->GetName(), def_str);
193 }
194 break;
195 }
196 }
197
198 i++;
199 }
200 }
201
202 void Usage(const char *arg0)
203 {
204 printf("Usage: %s [options] [filename]\n"
205 "Options:\n"
206 "opt | long option arg default\n"
207 "-----+--------------------------------------------------\n"
208 " -h | --help display usage\n"
209 " -l | --list list available video codecs\n"
210 //" -L | --list-audio list available audio codecs\n";
211 " -a | --codec-attribute attr set a codec attribute\n"
212 " -b | --audiobitrate rate 0 set audio bitrate (for mp3)\n"
213 " -c | --codec codec set video codec\n"
214 " -d | --deinterlace off deinterlace picture\n"
215 " -F | --fps rate 25 set framerate\n"
216 " -g | --grabdevice device "V4L_DEVICE" set video device\n"
217 " -G | --audiodevice device /dev/dsp set audio device\n"
218 " -i | --input input 0 set v4l input\n"
219 " -k | --keyframes rate 15 set key frame rate\n"
220 " -m | --audiomode val s audio mode (n=none, m=mono, s=stereo)\n"
221 " -n | --norm norm (none) set tuner norm (pal,ntsc,secam,pal-nc,pal-m,ntsc-jp)\n"
222 " -q | --quality val 95 set quality 1..100\n"
223 " -r | --rgb off capture in rgb color space\n"
224 " -t | --rectime time 60s set time limit (s,m,h,d,w)\n"
225 " -x | --width cols 384 set capture width\n"
226 " -y | --height rows 288 set capture height\n"
227 " -L | --over-l cols 0 set left overscan\n"
228 " -R | --over-r cols 0 set right overscan\n"
229 " -T | --over-t rows 0 set top overscan\n"
230 " -B | --over-b rows 0 set bottom overscan\n"
231 , arg0);
232 }
233
234 int time_interval(char *str)
235 {
236 char buf[16];
237 char *p = str, *q = buf;
238 int factor = 1, val;
239
240 while(*p && isdigit(*p))
241 *(q++) = *(p++);
242
243 *q = 0;
244 val = atoi(buf);
245
246 /* fall through: */
247 switch(*p)
248 {
249 case 'w':
250 factor *= 7;
251 case 'd':
252 factor *=24;
253 case 'h':
254 factor *= 60;
255 case 'm':
256 factor *= 60;
257 case 's':
258 case 0:
259 break;
260 default:
261 return -1;
262 }
263 return val * factor;
264 }
265
266 static void iMemory()
267 {
268 #ifdef HAVE_SYSINFO
269 struct sysinfo s_info;
270 sysinfo(&s_info);
271 m_iMemory=s_info.totalram;
272 if(m_iMemory<2*1048576)
273 m_iMemory=1048576;
274 else
275 m_iMemory/=2;
276 #else
277 m_iMemory = 16 * 1024 * 1024;
278 #endif
279 printf("Using %d Mb of memory for frame caching\n", m_iMemory/1048576);
280 }
281
282 struct CaptureConfig *CreateCapConf(VRConf *pconf, const avm::string& filename)
283 {
284 CaptureConfig *pcc = new CaptureConfig;
285
286 if(pcc)
287 {
288 pcc->filename = filename;
289 pcc->segment_size = -1;
290
291 avm::vector<avm::CodecInfo>::iterator it = GetCodecID(pconf->codec_string);
292 if(it != video_codecs.end())
293 {
294 pcc->codec.compressor = it->fourcc;
295 pcc->codec.cname=it->GetName();
296 printf("using codec '%s' (%u)\n", it->GetName(), it->fourcc);
297 }
298 else
299 {
300 fprintf(stderr, "invalid codec: %s\nuse -l for a list\n", pconf->codec_string.c_str());
301 return NULL;
302 }
303
304 int i;
305 for(i = 0; i < conf.attr_count; i++)
306 {
307 char name[21], val[21];
308 sscanf(conf.attributes[i], "%20[^=]=%s", name, val);
309 printf("setting '%s' = '%s'\n", name, val);
310 avm::CodecSetAttr(*it, name, atoi(val)); // FIXME: do not assume integer attribute...
311 }
312
313 pcc->codec.quality = pconf->quality * 100;
314 pcc->codec.keyfreq = pconf->keyframes;
315 pcc->frequency = 44100; // make configurable
316
317 pcc->chan = pconf->audio_channels;
318
319 if(pcc->chan > 0)
320 {
321 if(pconf->audio_bitrate)
322 {
323 pcc->audio_bitrate = pconf->audio_bitrate;
324 pcc->audio_compressor = 0x55; // MP3
325 }
326 else
327 pcc->audio_compressor = 1; // PCM
328 }else
329 pcc->frequency = 0; // tells CaptureProcess to not use audio
330
331 pcc->samplesize = 16; // make configurable
332 pcc->res_w = pconf->width;
333 pcc->res_h = pconf->height;
334
335 pcc->over_l = pconf->over_l;
336 pcc->over_r = pconf->over_r;
337 pcc->over_t = pconf->over_t;
338 pcc->over_b = pconf->over_b;
339
340 pcc->timelimit = pconf->timelimit;
341 pcc->sizelimit = -1; // make configurable
342 pcc->fps = pconf->fps;
343 if(pconf->rgb)
344 pcc->colorspace = cspRGB24;
345 else
346 pcc->colorspace = cspYUY2;
347 pcc->deinterlace = pconf->deinterlace;
348 pcc->audio_device = pconf->audio_device;
349 }
350 return pcc;
351 }
352
353 void ReadEnv()
354 {
355 char *val;
356
357 val = getenv("AVIREC_CODEC");
358 if (val) conf.codec_string = val;
359 val = getenv("AVIREC_ATTRIBUTES");
360 if (val) conf.attributes[conf.attr_count++] = strdup(val);
361 val = getenv("AVIREC_WIDTH");
362 if (val) conf.width = atoi(val);
363 val = getenv("AVIREC_HEIGHT");
364 if (val) conf.height = atoi(val);
365 val = getenv("AVIREC_QUALITY");
366 if (val) conf.quality = atoi(val);
367 val = getenv("AVIREC_KEYFRAMES");
368 if (val) conf.keyframes = atoi(val);
369
370 val = getenv("AVIREC_AUDIOBITRATE");
371 if (val) conf.audio_bitrate = atoi(val);
372 val = getenv("AVIREC_TIMELIMIT");
373 if (val) conf.timelimit = atoi(val);
374 val = getenv("AVIREC_FPS");
375 if (val) conf.fps = atof(val);
376
377 val = getenv("AVIREC_DEINTERLACE");
378 if (val && strncasecmp(val, "true", 4) == 0) conf.deinterlace = true;
379
380 val = getenv("AVIREC_RGB");
381 if (val && strncasecmp(val, "true", 4) == 0) conf.rgb = true;
382
383
384 val = getenv("AVIREC_NORM");
385 if(val)
386 {
387 int i = 0;
388 while(norms_bttv[i].str){
389 if(strcasecmp(norms_bttv[i].str, val) == 0) break;
390 i++;
391 }
392 if(norms_bttv[i].str)
393 conf.norm = norms_bttv[i].nr;
394 }
395 val = getenv("AVIREC_DEVICE");
396 if (val) conf.device = strdup(getenv("AVIREC_DEVICE"));
397
398 val = getenv("AVIREC_AUDIODEVICE");
399 if (val) conf.audio_device = strdup(getenv("AVIREC_AUDIODEVICE"));
400 }
401
402 int main(int argc, char *argv[])
403 {
404 int c;
405 int digit_optind = 0;
406 int do_rec = 1;
407
408 {
409 BITMAPINFOHEADER bih;
410 bih.biCompression = 0xffffffff;
411 // just to fill video_codecs list
412 avm::CreateDecoderVideo(bih, 0, 0);
413 }
414
415 ReadEnv();
416
417 while (1)
418 {
419 static const struct option long_options[] =
420 {
421 {"codec-attribute", 0, NULL, 'a'},
422 {"audiobitrate", 0, NULL, 'b'},
423 {"codec", 0, NULL, 'c'},
424 {"deinterlace", 0, NULL, 'd'},
425 {"framerate", 0, NULL, 'F'},
426 {"fps", 0, NULL, 'F'},
427 {"grabdevice", 0, NULL, 'g'},
428 {"audiodevice", 0, NULL, 'G'},
429 {"help", 0, NULL, 'h'},
430 {"input", 0, NULL, 'i'},
431 {"keyframes", 0, NULL, 'k'},
432 {"list", 0, NULL, 'l'},
433 {"list-audio", 0, NULL, 'C'},
434 {"audiomode", 0, NULL, 'm'},
435 {"quality", 0, NULL, 'q'},
436 {"rgb", 0, NULL, 'r'},
437 {"rectime", 0, NULL, 't'},
438 {"width", 0, NULL, 'x'},
439 {"height", 0, NULL, 'y'}, // -h already given for help...
440 {"over-l", 0, NULL, 'L'},
441 {"over-r", 0, NULL, 'R'},
442 {"over-t", 0, NULL, 'T'},
443 {"over-b", 0, NULL, 'B'},
444
445 {NULL, 0, NULL, 0 }
446 };
447 int this_option_optind = optind ? optind : 1;
448 int option_index = 0;
449
450 c = getopt_long (argc, argv, "a:b:Cc:dF:g:G:hi:k:lm:n:q:rt:x:y:L:R:T:B:",
451 long_options, &option_index);
452 if (c == -1)
453 break;
454
455 switch (c)
456 {
457 case 'a':
458 conf.attributes[conf.attr_count++] = strdup(optarg);
459 break;
460 case 'b':
461 conf.audio_bitrate = atoi(optarg);
462 break;
463 case 'c':
464 conf.codec_string = optarg;
465 break;
466 case 'd':
467 conf.deinterlace = true;
468 break;
469 case 'F':
470 conf.fps = atof(optarg);
471 break;
472 case 'g':
473 conf.device = strdup(optarg);
474 break;
475 case 'G':
476 conf.audio_device = strdup(optarg);
477 break;
478 case '?':
479 case 'h':
480 Usage(argv[0]);
481 do_rec = 0;
482 break;
483 case 'k':
484 conf.keyframes = atoi(optarg);
485 break;
486 case 'l':
487 ListCodecs(video_codecs);
488 do_rec = 0;
489 break;
490 case 'C':
491 ListCodecs(audio_codecs);
492 do_rec = 0;
493 break;
494 case 'm':
495 switch (optarg[0]){
496 case 'n':
497 conf.audio_channels = 0;
498 break;
499 case 'm':
500 conf.audio_channels = 1;
501 break;
502 case 's':
503 conf.audio_channels = 2;
504 break;
505 }
506 break;
507 case 'n':
508 {
509 int i = 0;
510 while(norms_bttv[i].str)
511 {
512 if(strcasecmp(norms_bttv[i].str, optarg) == 0) break;
513 i++;
514 }
515 if(norms_bttv[i].str)
516 conf.norm = norms_bttv[i].nr;
517 }
518 break;
519 case 'i':
520 conf.input = atoi(optarg);
521 break;
522 case 'q':
523 conf.quality = atoi(optarg);
524 break;
525 case 'r':
526 conf.rgb = true;
527 break;
528 case 't':
529 conf.timelimit = time_interval(optarg);
530 break;
531 case 'x':
532 conf.width = atoi(optarg);
533 break;
534 case 'y':
535 conf.height = atoi(optarg);
536 break;
537 case 'L':
538 conf.over_l = atoi(optarg);
539 break;
540 case 'R':
541 conf.over_r = atoi(optarg);
542 break;
543 case 'T':
544 conf.over_t = atoi(optarg);
545 break;
546 case 'B':
547 conf.over_b = atoi(optarg);
548 break;
549 }
550 }
551
552 if (optind < argc)
553 {
554 if(do_rec)
555 {
556 iMemory();
557
558 avm::string filename = argv[optind++];
559 CaptureConfig *capconf = CreateCapConf(&conf, filename);
560
561 if(!capconf) exit(EXIT_FAILURE);
562
563 v4lxif* v4l;
564 try
565 {
566 v4l = new v4l1baseif(0, conf.device);
567
568 if(conf.audio_channels > 0)
569 {
570 v4l->setAudioMode(conf.audio_channels == 2 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO);
571 v4l->setAudioMute(false);
572 v4l->setAudioVolume(65535);
573 }else
574 v4l->setAudioMute(true);
575
576 if(conf.norm > 0)
577 {
578 // twice? don't ask me why...
579 v4l->setChannelNorm(conf.norm);
580 v4l->setChannelNorm(conf.norm);
581 }
582 if(conf.input > 0)
583 v4l->setChannel(conf.input);
584 }
585 catch(BaseError &e)
586 {
587 e.Print();
588 exit(EXIT_FAILURE);
589 }
590
591 CaptureProcess *capproc;
592 try
593 {
594 capproc = new CaptureProcess(v4l, capconf, NULL);
595 }
596 catch(FatalError& e)
597 {
598 fprintf(stderr, "Error: %s\n", e.GetDesc());
599 exit(EXIT_FAILURE);
600 }
601
602 sighandler_t old_int = signal(SIGINT, sig_int);
603
604 CaptureProgress *progress = new CaptureProgress();
605 capproc->setMessenger(progress);
606
607 // now sleep until it's over...
608
609 int i = 0;
610 while(1)
611 {
612 int finished;
613 avm::string error;
614 capproc->getState(finished, error);
615
616 if(finished) break;
617 if(sig_int_seen)
618 {
619 fprintf(stderr, "Ctrl-C from keyboard -- please wait\n");
620 break;
621 }
622
623 if(isatty(1))
624 {
625 progress->lock();
626 int drop_enc = progress->dropped_enc;
627 int drop_cap = progress->dropped_cap;
628 progress->unlock();
629
630 double elapsed_float=progress->enc_time/1000000.0;
631
632 printf("\r time: %d frames: %d cap drop: %d enc drop: %d",
633 (int)elapsed_float,
634 progress->video_frames,
635 progress->dropped_cap,
636 progress->dropped_enc);
637 fflush(stdout);
638 }
639
640 sleep(1);
641 i++;
642 }
643 delete capproc;
644
645 signal(SIGINT, old_int);
646
647 if(conf.audio_channels > 0)
648 {
649 v4l->setAudioMute(true);
650 }
651 }
652 }
653 }