You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2410 lines
76KB

  1. /*
  2. * FFmpeg main
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #define HAVE_AV_CONFIG_H
  20. #include "avformat.h"
  21. #include "tick.h"
  22. #ifndef CONFIG_WIN32
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/time.h>
  27. #include <termios.h>
  28. #include <sys/resource.h>
  29. #endif
  30. #ifdef __BEOS__
  31. /* for snooze() */
  32. #include <OS.h>
  33. #endif
  34. #include <time.h>
  35. #include <ctype.h>
  36. #define MAXINT64 INT64_C(0x7fffffffffffffff)
  37. typedef struct {
  38. const char *name;
  39. int flags;
  40. #define HAS_ARG 0x0001
  41. #define OPT_BOOL 0x0002
  42. #define OPT_EXPERT 0x0004
  43. #define OPT_STRING 0x0008
  44. union {
  45. void (*func_arg)();
  46. int *int_arg;
  47. char **str_arg;
  48. } u;
  49. const char *help;
  50. const char *argname;
  51. } OptionDef;
  52. /* select an input stream for an output stream */
  53. typedef struct AVStreamMap {
  54. int file_index;
  55. int stream_index;
  56. } AVStreamMap;
  57. extern const OptionDef options[];
  58. void show_help(void);
  59. #define MAX_FILES 20
  60. static AVFormatContext *input_files[MAX_FILES];
  61. static int nb_input_files = 0;
  62. static AVFormatContext *output_files[MAX_FILES];
  63. static int nb_output_files = 0;
  64. static AVStreamMap stream_maps[MAX_FILES];
  65. static int nb_stream_maps;
  66. static AVInputFormat *file_iformat;
  67. static AVOutputFormat *file_oformat;
  68. static int frame_width = 160;
  69. static int frame_height = 128;
  70. static int frame_topBand = 0;
  71. static int frame_bottomBand = 0;
  72. static int frame_leftBand = 0;
  73. static int frame_rightBand = 0;
  74. static int frame_rate = 25 * FRAME_RATE_BASE;
  75. static int video_bit_rate = 200*1000;
  76. static int video_bit_rate_tolerance = 4000*1000;
  77. static int video_qscale = 0;
  78. static int video_qmin = 2;
  79. static int video_qmax = 31;
  80. static int video_qdiff = 3;
  81. static float video_qblur = 0.5;
  82. static float video_qcomp = 0.5;
  83. static float video_rc_qsquish=1.0;
  84. static float video_rc_qmod_amp=0;
  85. static int video_rc_qmod_freq=0;
  86. static char *video_rc_override_string=NULL;
  87. static char *video_rc_eq="tex^qComp";
  88. static int video_rc_buffer_size=0;
  89. static float video_rc_buffer_aggressivity=1.0;
  90. static int video_rc_max_rate=0;
  91. static int video_rc_min_rate=0;
  92. static float video_rc_initial_cplx=0;
  93. static float video_b_qfactor = 1.25;
  94. static float video_b_qoffset = 1.25;
  95. static float video_i_qfactor = -0.8;
  96. static float video_i_qoffset = 0.0;
  97. static int me_method = 0;
  98. static int video_disable = 0;
  99. static int video_codec_id = CODEC_ID_NONE;
  100. static int same_quality = 0;
  101. static int b_frames = 0;
  102. static int use_hq = 0;
  103. static int use_4mv = 0;
  104. static int do_deinterlace = 0;
  105. static int workaround_bugs = 0;
  106. static int error_resilience = 0;
  107. static int dct_algo = 0;
  108. static int use_part = 0;
  109. static int packet_size = 0;
  110. static int gop_size = 12;
  111. static int intra_only = 0;
  112. static int audio_sample_rate = 44100;
  113. static int audio_bit_rate = 64000;
  114. static int audio_disable = 0;
  115. static int audio_channels = 1;
  116. static int audio_codec_id = CODEC_ID_NONE;
  117. static INT64 recording_time = 0;
  118. static int file_overwrite = 0;
  119. static char *str_title = NULL;
  120. static char *str_author = NULL;
  121. static char *str_copyright = NULL;
  122. static char *str_comment = NULL;
  123. static int do_benchmark = 0;
  124. static int do_hex_dump = 0;
  125. static int do_play = 0;
  126. static int do_psnr = 0;
  127. static int do_vstats = 0;
  128. static int mpeg_vcd = 0;
  129. #ifndef CONFIG_AUDIO_OSS
  130. const char *audio_device = "none";
  131. #endif
  132. #ifndef CONFIG_VIDEO4LINUX
  133. const char *v4l_device = "none";
  134. #endif
  135. typedef struct AVOutputStream {
  136. int file_index; /* file index */
  137. int index; /* stream index in the output file */
  138. int source_index; /* AVInputStream index */
  139. AVStream *st; /* stream in the output file */
  140. int encoding_needed; /* true if encoding needed for this stream */
  141. /* video only */
  142. AVPicture pict_tmp; /* temporary image for resizing */
  143. int video_resample;
  144. ImgReSampleContext *img_resample_ctx; /* for image resampling */
  145. /* audio only */
  146. int audio_resample;
  147. ReSampleContext *resample; /* for audio resampling */
  148. FifoBuffer fifo; /* for compression: one audio fifo per codec */
  149. } AVOutputStream;
  150. typedef struct AVInputStream {
  151. int file_index;
  152. int index;
  153. AVStream *st;
  154. int discard; /* true if stream data should be discarded */
  155. int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
  156. Ticker pts_ticker; /* Ticker for PTS calculation */
  157. int ticker_inited; /* to signal if the ticker was initialized */
  158. INT64 pts; /* current pts */
  159. int pts_increment; /* expected pts increment for next packet */
  160. int frame_number; /* current frame */
  161. INT64 sample_index; /* current sample */
  162. } AVInputStream;
  163. typedef struct AVInputFile {
  164. int eof_reached; /* true if eof reached */
  165. int ist_index; /* index of first stream in ist_table */
  166. int buffer_size; /* current total buffer size */
  167. int buffer_size_max; /* buffer size at which we consider we can stop
  168. buffering */
  169. int nb_streams; /* nb streams we are aware of */
  170. } AVInputFile;
  171. #ifndef CONFIG_WIN32
  172. /* init terminal so that we can grab keys */
  173. static struct termios oldtty;
  174. static void term_exit(void)
  175. {
  176. tcsetattr (0, TCSANOW, &oldtty);
  177. }
  178. static void term_init(void)
  179. {
  180. struct termios tty;
  181. tcgetattr (0, &tty);
  182. oldtty = tty;
  183. tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  184. |INLCR|IGNCR|ICRNL|IXON);
  185. tty.c_oflag |= OPOST;
  186. tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
  187. tty.c_cflag &= ~(CSIZE|PARENB);
  188. tty.c_cflag |= CS8;
  189. tty.c_cc[VMIN] = 1;
  190. tty.c_cc[VTIME] = 0;
  191. tcsetattr (0, TCSANOW, &tty);
  192. atexit(term_exit);
  193. }
  194. /* read a key without blocking */
  195. static int read_key(void)
  196. {
  197. struct timeval tv;
  198. int n;
  199. unsigned char ch;
  200. fd_set rfds;
  201. FD_ZERO(&rfds);
  202. FD_SET(0, &rfds);
  203. tv.tv_sec = 0;
  204. tv.tv_usec = 0;
  205. n = select(1, &rfds, NULL, NULL, &tv);
  206. if (n > 0) {
  207. n = read(0, &ch, 1);
  208. if (n == 1)
  209. return ch;
  210. return n;
  211. }
  212. return -1;
  213. }
  214. #else
  215. /* no interactive support */
  216. static void term_exit(void)
  217. {
  218. }
  219. static void term_init(void)
  220. {
  221. }
  222. static int read_key(void)
  223. {
  224. return 0;
  225. }
  226. #endif
  227. int read_ffserver_streams(AVFormatContext *s, const char *filename)
  228. {
  229. int i, err;
  230. AVFormatContext *ic;
  231. err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
  232. if (err < 0)
  233. return err;
  234. /* copy stream format */
  235. s->nb_streams = ic->nb_streams;
  236. for(i=0;i<ic->nb_streams;i++) {
  237. AVStream *st;
  238. st = av_mallocz(sizeof(AVFormatContext));
  239. memcpy(st, ic->streams[i], sizeof(AVStream));
  240. s->streams[i] = st;
  241. }
  242. av_close_input_file(ic);
  243. return 0;
  244. }
  245. #define MAX_AUDIO_PACKET_SIZE 16384
  246. static void do_audio_out(AVFormatContext *s,
  247. AVOutputStream *ost,
  248. AVInputStream *ist,
  249. unsigned char *buf, int size)
  250. {
  251. UINT8 *buftmp;
  252. UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
  253. UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
  254. int size_out, frame_bytes, ret;
  255. AVCodecContext *enc;
  256. enc = &ost->st->codec;
  257. if (ost->audio_resample) {
  258. buftmp = audio_buf;
  259. size_out = audio_resample(ost->resample,
  260. (short *)buftmp, (short *)buf,
  261. size / (ist->st->codec.channels * 2));
  262. size_out = size_out * enc->channels * 2;
  263. } else {
  264. buftmp = buf;
  265. size_out = size;
  266. }
  267. /* now encode as many frames as possible */
  268. if (enc->frame_size > 1) {
  269. /* output resampled raw samples */
  270. fifo_write(&ost->fifo, buftmp, size_out,
  271. &ost->fifo.wptr);
  272. frame_bytes = enc->frame_size * 2 * enc->channels;
  273. while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
  274. &ost->fifo.rptr) == 0) {
  275. ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
  276. (short *)audio_buf);
  277. s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
  278. }
  279. } else {
  280. /* output a pcm frame */
  281. /* XXX: change encoding codec API to avoid this ? */
  282. switch(enc->codec->id) {
  283. case CODEC_ID_PCM_S16LE:
  284. case CODEC_ID_PCM_S16BE:
  285. case CODEC_ID_PCM_U16LE:
  286. case CODEC_ID_PCM_U16BE:
  287. break;
  288. default:
  289. size_out = size_out >> 1;
  290. break;
  291. }
  292. ret = avcodec_encode_audio(enc, audio_out, size_out,
  293. (short *)buftmp);
  294. s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
  295. }
  296. }
  297. /* write a picture to a raw mux */
  298. static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
  299. int pix_fmt, int w, int h)
  300. {
  301. UINT8 *buf, *src, *dest;
  302. int size, j, i;
  303. /* XXX: not efficient, should add test if we can take
  304. directly the AVPicture */
  305. switch(pix_fmt) {
  306. case PIX_FMT_YUV420P:
  307. size = avpicture_get_size(pix_fmt, w, h);
  308. buf = av_malloc(size);
  309. if (!buf)
  310. return;
  311. dest = buf;
  312. for(i=0;i<3;i++) {
  313. if (i == 1) {
  314. w >>= 1;
  315. h >>= 1;
  316. }
  317. src = picture->data[i];
  318. for(j=0;j<h;j++) {
  319. memcpy(dest, src, w);
  320. dest += w;
  321. src += picture->linesize[i];
  322. }
  323. }
  324. break;
  325. case PIX_FMT_YUV422P:
  326. size = (w * h) * 2;
  327. buf = av_malloc(size);
  328. if (!buf)
  329. return;
  330. dest = buf;
  331. for(i=0;i<3;i++) {
  332. if (i == 1) {
  333. w >>= 1;
  334. }
  335. src = picture->data[i];
  336. for(j=0;j<h;j++) {
  337. memcpy(dest, src, w);
  338. dest += w;
  339. src += picture->linesize[i];
  340. }
  341. }
  342. break;
  343. case PIX_FMT_YUV444P:
  344. size = (w * h) * 3;
  345. buf = av_malloc(size);
  346. if (!buf)
  347. return;
  348. dest = buf;
  349. for(i=0;i<3;i++) {
  350. src = picture->data[i];
  351. for(j=0;j<h;j++) {
  352. memcpy(dest, src, w);
  353. dest += w;
  354. src += picture->linesize[i];
  355. }
  356. }
  357. break;
  358. case PIX_FMT_YUV422:
  359. size = (w * h) * 2;
  360. buf = av_malloc(size);
  361. if (!buf)
  362. return;
  363. dest = buf;
  364. src = picture->data[0];
  365. for(j=0;j<h;j++) {
  366. memcpy(dest, src, w * 2);
  367. dest += w * 2;
  368. src += picture->linesize[0];
  369. }
  370. break;
  371. case PIX_FMT_RGB24:
  372. case PIX_FMT_BGR24:
  373. size = (w * h) * 3;
  374. buf = av_malloc(size);
  375. if (!buf)
  376. return;
  377. dest = buf;
  378. src = picture->data[0];
  379. for(j=0;j<h;j++) {
  380. memcpy(dest, src, w * 3);
  381. dest += w * 3;
  382. src += picture->linesize[0];
  383. }
  384. break;
  385. default:
  386. return;
  387. }
  388. s->oformat->write_packet(s, index, buf, size, 0);
  389. av_free(buf);
  390. }
  391. static void do_video_out(AVFormatContext *s,
  392. AVOutputStream *ost,
  393. AVInputStream *ist,
  394. AVPicture *picture1,
  395. int *frame_size)
  396. {
  397. int n1, n2, nb, i, ret, frame_number, dec_frame_rate;
  398. AVPicture *picture, *picture2, *pict;
  399. AVPicture picture_tmp1, picture_tmp2;
  400. static UINT8 *video_buffer;
  401. UINT8 *buf = NULL, *buf1 = NULL;
  402. AVCodecContext *enc, *dec;
  403. #define VIDEO_BUFFER_SIZE (1024*1024)
  404. enc = &ost->st->codec;
  405. dec = &ist->st->codec;
  406. frame_number = ist->frame_number;
  407. dec_frame_rate = ist->st->r_frame_rate;
  408. // fprintf(stderr, "\n%d", dec_frame_rate);
  409. /* first drop frame if needed */
  410. n1 = ((INT64)frame_number * enc->frame_rate) / dec_frame_rate;
  411. n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec_frame_rate;
  412. nb = n2 - n1;
  413. if (nb <= 0)
  414. return;
  415. if (!video_buffer)
  416. video_buffer= av_malloc(VIDEO_BUFFER_SIZE);
  417. if(!video_buffer) return;
  418. /* deinterlace : must be done before any resize */
  419. if (do_deinterlace) {
  420. int size;
  421. /* create temporary picture */
  422. size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
  423. buf1 = av_malloc(size);
  424. if (!buf1)
  425. return;
  426. picture2 = &picture_tmp2;
  427. avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
  428. if (avpicture_deinterlace(picture2, picture1,
  429. dec->pix_fmt, dec->width, dec->height) < 0) {
  430. /* if error, do not deinterlace */
  431. av_free(buf1);
  432. buf1 = NULL;
  433. picture2 = picture1;
  434. }
  435. } else {
  436. picture2 = picture1;
  437. }
  438. /* convert pixel format if needed */
  439. if (enc->pix_fmt != dec->pix_fmt) {
  440. int size;
  441. /* create temporary picture */
  442. size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
  443. buf = av_malloc(size);
  444. if (!buf)
  445. return;
  446. pict = &picture_tmp1;
  447. avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
  448. if (img_convert(pict, enc->pix_fmt,
  449. picture2, dec->pix_fmt,
  450. dec->width, dec->height) < 0) {
  451. fprintf(stderr, "pixel format conversion not handled\n");
  452. goto the_end;
  453. }
  454. } else {
  455. pict = picture2;
  456. }
  457. /* XXX: resampling could be done before raw format convertion in
  458. some cases to go faster */
  459. /* XXX: only works for YUV420P */
  460. if (ost->video_resample) {
  461. picture = &ost->pict_tmp;
  462. img_resample(ost->img_resample_ctx, picture, pict);
  463. } else {
  464. picture = pict;
  465. }
  466. nb=1;
  467. /* duplicates frame if needed */
  468. /* XXX: pb because no interleaving */
  469. for(i=0;i<nb;i++) {
  470. if (enc->codec_id != CODEC_ID_RAWVIDEO) {
  471. /* handles sameq here. This is not correct because it may
  472. not be a global option */
  473. if (same_quality) {
  474. enc->quality = dec->quality;
  475. }
  476. ret = avcodec_encode_video(enc,
  477. video_buffer, VIDEO_BUFFER_SIZE,
  478. picture);
  479. //enc->frame_number = enc->real_pict_num;
  480. s->oformat->write_packet(s, ost->index, video_buffer, ret, 0);
  481. *frame_size = ret;
  482. //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
  483. // enc->frame_number-1, enc->real_pict_num, ret,
  484. // enc->pict_type);
  485. } else {
  486. write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
  487. }
  488. }
  489. the_end:
  490. av_free(buf);
  491. av_free(buf1);
  492. }
  493. static void do_video_stats(AVOutputStream *ost,
  494. AVInputStream *ist,
  495. int frame_size)
  496. {
  497. static FILE *fvstats=NULL;
  498. static INT64 total_size = 0;
  499. char filename[40];
  500. time_t today2;
  501. struct tm *today;
  502. AVCodecContext *enc;
  503. int frame_number;
  504. INT64 ti;
  505. double ti1, bitrate, avg_bitrate;
  506. if (!fvstats) {
  507. today2 = time(NULL);
  508. today = localtime(&today2);
  509. sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
  510. today->tm_min,
  511. today->tm_sec);
  512. fvstats = fopen(filename,"w");
  513. if (!fvstats) {
  514. perror("fopen");
  515. exit(1);
  516. }
  517. }
  518. ti = MAXINT64;
  519. enc = &ost->st->codec;
  520. total_size += frame_size;
  521. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  522. frame_number = ist->frame_number;
  523. fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
  524. if (do_psnr)
  525. fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
  526. fprintf(fvstats,"f_size= %6d ", frame_size);
  527. /* compute min pts value */
  528. if (!ist->discard && ist->pts < ti) {
  529. ti = ist->pts;
  530. }
  531. ti1 = (double)ti / 1000000.0;
  532. if (ti1 < 0.01)
  533. ti1 = 0.01;
  534. bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
  535. avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  536. fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
  537. (double)total_size / 1024, ti1, bitrate, avg_bitrate);
  538. fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
  539. }
  540. }
  541. /*
  542. * The following code is the main loop of the file converter
  543. */
  544. static int av_encode(AVFormatContext **output_files,
  545. int nb_output_files,
  546. AVFormatContext **input_files,
  547. int nb_input_files,
  548. AVStreamMap *stream_maps, int nb_stream_maps)
  549. {
  550. int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
  551. AVFormatContext *is, *os;
  552. AVCodecContext *codec, *icodec;
  553. AVOutputStream *ost, **ost_table = NULL;
  554. AVInputStream *ist, **ist_table = NULL;
  555. INT64 min_pts, start_time;
  556. AVInputFile *file_table;
  557. AVFormatContext *stream_no_data;
  558. int key;
  559. file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
  560. if (!file_table)
  561. goto fail;
  562. /* input stream init */
  563. j = 0;
  564. for(i=0;i<nb_input_files;i++) {
  565. is = input_files[i];
  566. file_table[i].ist_index = j;
  567. file_table[i].nb_streams = is->nb_streams;
  568. j += is->nb_streams;
  569. }
  570. nb_istreams = j;
  571. ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
  572. if (!ist_table)
  573. goto fail;
  574. for(i=0;i<nb_istreams;i++) {
  575. ist = av_mallocz(sizeof(AVInputStream));
  576. if (!ist)
  577. goto fail;
  578. ist_table[i] = ist;
  579. }
  580. j = 0;
  581. for(i=0;i<nb_input_files;i++) {
  582. is = input_files[i];
  583. for(k=0;k<is->nb_streams;k++) {
  584. ist = ist_table[j++];
  585. ist->st = is->streams[k];
  586. ist->file_index = i;
  587. ist->index = k;
  588. ist->discard = 1; /* the stream is discarded by default
  589. (changed later) */
  590. }
  591. }
  592. /* output stream init */
  593. nb_ostreams = 0;
  594. for(i=0;i<nb_output_files;i++) {
  595. os = output_files[i];
  596. nb_ostreams += os->nb_streams;
  597. if (mpeg_vcd)
  598. os->flags |= AVF_FLAG_VCD;
  599. }
  600. if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
  601. fprintf(stderr, "Number of stream maps must match number of output streams\n");
  602. exit(1);
  603. }
  604. ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
  605. if (!ost_table)
  606. goto fail;
  607. for(i=0;i<nb_ostreams;i++) {
  608. ost = av_mallocz(sizeof(AVOutputStream));
  609. if (!ost)
  610. goto fail;
  611. ost_table[i] = ost;
  612. }
  613. n = 0;
  614. for(k=0;k<nb_output_files;k++) {
  615. os = output_files[k];
  616. for(i=0;i<os->nb_streams;i++) {
  617. int found;
  618. ost = ost_table[n++];
  619. ost->file_index = k;
  620. ost->index = i;
  621. ost->st = os->streams[i];
  622. if (nb_stream_maps > 0) {
  623. ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
  624. stream_maps[n-1].stream_index;
  625. } else {
  626. /* get corresponding input stream index : we select the first one with the right type */
  627. found = 0;
  628. for(j=0;j<nb_istreams;j++) {
  629. ist = ist_table[j];
  630. if (ist->discard &&
  631. ist->st->codec.codec_type == ost->st->codec.codec_type) {
  632. ost->source_index = j;
  633. found = 1;
  634. }
  635. }
  636. if (!found) {
  637. /* try again and reuse existing stream */
  638. for(j=0;j<nb_istreams;j++) {
  639. ist = ist_table[j];
  640. if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
  641. ost->source_index = j;
  642. found = 1;
  643. }
  644. }
  645. if (!found) {
  646. fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
  647. ost->file_index, ost->index);
  648. exit(1);
  649. }
  650. }
  651. }
  652. ist = ist_table[ost->source_index];
  653. ist->discard = 0;
  654. }
  655. }
  656. /* dump the stream mapping */
  657. fprintf(stderr, "Stream mapping:\n");
  658. for(i=0;i<nb_ostreams;i++) {
  659. ost = ost_table[i];
  660. fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
  661. ist_table[ost->source_index]->file_index,
  662. ist_table[ost->source_index]->index,
  663. ost->file_index,
  664. ost->index);
  665. }
  666. /* for each output stream, we compute the right encoding parameters */
  667. for(i=0;i<nb_ostreams;i++) {
  668. ost = ost_table[i];
  669. ist = ist_table[ost->source_index];
  670. codec = &ost->st->codec;
  671. icodec = &ist->st->codec;
  672. switch(codec->codec_type) {
  673. case CODEC_TYPE_AUDIO:
  674. /* check if same codec with same parameters. If so, no
  675. reencoding is needed */
  676. if (codec->codec_id == icodec->codec_id &&
  677. codec->bit_rate == icodec->bit_rate &&
  678. codec->sample_rate == icodec->sample_rate &&
  679. codec->channels == icodec->channels) {
  680. /* no reencoding */
  681. /* use the same frame size */
  682. codec->frame_size = icodec->frame_size;
  683. //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
  684. // icodec->bit_rate;
  685. //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
  686. } else {
  687. if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
  688. goto fail;
  689. if (codec->channels == icodec->channels &&
  690. codec->sample_rate == icodec->sample_rate) {
  691. ost->audio_resample = 0;
  692. } else {
  693. if (codec->channels != icodec->channels &&
  694. icodec->codec_id == CODEC_ID_AC3) {
  695. /* Special case for 5:1 AC3 input */
  696. /* and mono or stereo output */
  697. /* Request specific number of channels */
  698. icodec->channels = codec->channels;
  699. if (codec->sample_rate == icodec->sample_rate)
  700. ost->audio_resample = 0;
  701. else {
  702. ost->audio_resample = 1;
  703. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  704. codec->sample_rate,
  705. icodec->sample_rate);
  706. }
  707. /* Request specific number of channels */
  708. icodec->channels = codec->channels;
  709. } else {
  710. ost->audio_resample = 1;
  711. ost->resample = audio_resample_init(codec->channels, icodec->channels,
  712. codec->sample_rate,
  713. icodec->sample_rate);
  714. }
  715. }
  716. ist->decoding_needed = 1;
  717. ost->encoding_needed = 1;
  718. }
  719. break;
  720. case CODEC_TYPE_VIDEO:
  721. /* check if same codec with same parameters. If so, no
  722. reencoding is needed */
  723. if (codec->codec_id == icodec->codec_id &&
  724. codec->bit_rate == icodec->bit_rate &&
  725. codec->frame_rate == icodec->frame_rate &&
  726. codec->width == icodec->width &&
  727. codec->height == icodec->height) {
  728. /* no reencoding */
  729. } else {
  730. if (codec->width == icodec->width &&
  731. codec->height == icodec->height) {
  732. ost->video_resample = 0;
  733. } else {
  734. UINT8 *buf;
  735. ost->video_resample = 1;
  736. buf = av_malloc((codec->width * codec->height * 3) / 2);
  737. if (!buf)
  738. goto fail;
  739. ost->pict_tmp.data[0] = buf;
  740. ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
  741. ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
  742. ost->pict_tmp.linesize[0] = codec->width;
  743. ost->pict_tmp.linesize[1] = codec->width / 2;
  744. ost->pict_tmp.linesize[2] = codec->width / 2;
  745. ost->img_resample_ctx = img_resample_full_init(
  746. ost->st->codec.width, ost->st->codec.height,
  747. ist->st->codec.width, ist->st->codec.height,
  748. frame_topBand, frame_bottomBand,
  749. frame_leftBand, frame_rightBand);
  750. }
  751. ost->encoding_needed = 1;
  752. ist->decoding_needed = 1;
  753. }
  754. break;
  755. default:
  756. av_abort();
  757. }
  758. }
  759. /* open each encoder */
  760. for(i=0;i<nb_ostreams;i++) {
  761. ost = ost_table[i];
  762. if (ost->encoding_needed) {
  763. AVCodec *codec;
  764. codec = avcodec_find_encoder(ost->st->codec.codec_id);
  765. if (!codec) {
  766. fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
  767. ost->file_index, ost->index);
  768. exit(1);
  769. }
  770. if (avcodec_open(&ost->st->codec, codec) < 0) {
  771. fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
  772. ost->file_index, ost->index);
  773. exit(1);
  774. }
  775. }
  776. }
  777. /* open each decoder */
  778. for(i=0;i<nb_istreams;i++) {
  779. ist = ist_table[i];
  780. if (ist->decoding_needed) {
  781. AVCodec *codec;
  782. codec = avcodec_find_decoder(ist->st->codec.codec_id);
  783. if (!codec) {
  784. fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
  785. ist->file_index, ist->index);
  786. exit(1);
  787. }
  788. if (avcodec_open(&ist->st->codec, codec) < 0) {
  789. fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
  790. ist->file_index, ist->index);
  791. exit(1);
  792. }
  793. //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
  794. // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
  795. }
  796. }
  797. /* init pts */
  798. for(i=0;i<nb_istreams;i++) {
  799. ist = ist_table[i];
  800. ist->pts = 0;
  801. ist->frame_number = 0;
  802. }
  803. /* compute buffer size max (should use a complete heuristic) */
  804. for(i=0;i<nb_input_files;i++) {
  805. file_table[i].buffer_size_max = 2048;
  806. }
  807. /* open files and write file headers */
  808. for(i=0;i<nb_output_files;i++) {
  809. os = output_files[i];
  810. if (av_write_header(os) < 0) {
  811. fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
  812. ret = -EINVAL;
  813. goto fail;
  814. }
  815. }
  816. #ifndef CONFIG_WIN32
  817. if (!do_play) {
  818. fprintf(stderr, "Press [q] to stop encoding\n");
  819. } else {
  820. fprintf(stderr, "Press [q] to stop playing\n");
  821. }
  822. #endif
  823. term_init();
  824. start_time = av_gettime();
  825. min_pts = 0;
  826. stream_no_data = 0;
  827. key = -1;
  828. for(;;) {
  829. int file_index, ist_index;
  830. AVPacket pkt;
  831. UINT8 *ptr;
  832. int len;
  833. UINT8 *data_buf;
  834. int data_size, got_picture;
  835. AVPicture picture;
  836. short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
  837. redo:
  838. /* if 'q' pressed, exits */
  839. if (key) {
  840. /* read_key() returns 0 on EOF */
  841. key = read_key();
  842. if (key == 'q')
  843. break;
  844. }
  845. /* select the input file with the smallest pts */
  846. file_index = -1;
  847. min_pts = MAXINT64;
  848. for(i=0;i<nb_istreams;i++) {
  849. ist = ist_table[i];
  850. /* For some reason, the pts_increment code breaks q estimation?!? */
  851. if (!ist->discard && !file_table[ist->file_index].eof_reached &&
  852. ist->pts /* + ist->pts_increment */ < min_pts && input_files[ist->file_index] != stream_no_data) {
  853. min_pts = ist->pts /* + ist->pts_increment */;
  854. file_index = ist->file_index;
  855. }
  856. }
  857. /* if none, if is finished */
  858. if (file_index < 0) {
  859. if (stream_no_data) {
  860. #ifndef CONFIG_WIN32 /* no usleep in VisualC ? */
  861. #ifdef __BEOS__
  862. snooze(10 * 1000); /* mmu_man */ /* in microsec */
  863. #elif defined(__CYGWIN__)
  864. usleep(10 * 1000);
  865. #else
  866. struct timespec ts;
  867. ts.tv_sec = 0;
  868. ts.tv_nsec = 1000 * 1000 * 10;
  869. nanosleep(&ts, 0);
  870. #endif
  871. #endif
  872. stream_no_data = 0;
  873. continue;
  874. }
  875. break;
  876. }
  877. /* finish if recording time exhausted */
  878. if (recording_time > 0 && min_pts >= recording_time)
  879. break;
  880. /* read a packet from it and output it in the fifo */
  881. is = input_files[file_index];
  882. if (av_read_packet(is, &pkt) < 0) {
  883. file_table[file_index].eof_reached = 1;
  884. continue;
  885. }
  886. if (!pkt.size) {
  887. stream_no_data = is;
  888. } else {
  889. stream_no_data = 0;
  890. }
  891. /* the following test is needed in case new streams appear
  892. dynamically in stream : we ignore them */
  893. if (pkt.stream_index >= file_table[file_index].nb_streams)
  894. goto discard_packet;
  895. ist_index = file_table[file_index].ist_index + pkt.stream_index;
  896. ist = ist_table[ist_index];
  897. if (ist->discard)
  898. goto discard_packet;
  899. if (pkt.flags & PKT_FLAG_DROPPED_FRAME)
  900. ist->frame_number++;
  901. if (do_hex_dump) {
  902. printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
  903. av_hex_dump(pkt.data, pkt.size);
  904. }
  905. // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
  906. len = pkt.size;
  907. ptr = pkt.data;
  908. while (len > 0) {
  909. /* decode the packet if needed */
  910. data_buf = NULL; /* fail safe */
  911. data_size = 0;
  912. if (ist->decoding_needed) {
  913. switch(ist->st->codec.codec_type) {
  914. case CODEC_TYPE_AUDIO:
  915. /* XXX: could avoid copy if PCM 16 bits with same
  916. endianness as CPU */
  917. ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
  918. ptr, len);
  919. if (ret < 0)
  920. goto fail_decode;
  921. /* Some bug in mpeg audio decoder gives */
  922. /* data_size < 0, it seems they are overflows */
  923. if (data_size <= 0) {
  924. /* no audio frame */
  925. ptr += ret;
  926. len -= ret;
  927. continue;
  928. }
  929. data_buf = (UINT8 *)samples;
  930. break;
  931. case CODEC_TYPE_VIDEO:
  932. if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
  933. int size;
  934. size = (ist->st->codec.width * ist->st->codec.height);
  935. avpicture_fill(&picture, ptr,
  936. ist->st->codec.pix_fmt,
  937. ist->st->codec.width,
  938. ist->st->codec.height);
  939. ret = len;
  940. } else {
  941. data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
  942. ret = avcodec_decode_video(&ist->st->codec,
  943. &picture, &got_picture, ptr, len);
  944. if (ret < 0) {
  945. fail_decode:
  946. fprintf(stderr, "Error while decoding stream #%d.%d\n",
  947. ist->file_index, ist->index);
  948. av_free_packet(&pkt);
  949. goto redo;
  950. }
  951. if (!got_picture) {
  952. /* no picture yet */
  953. ptr += ret;
  954. len -= ret;
  955. continue;
  956. }
  957. }
  958. break;
  959. default:
  960. goto fail_decode;
  961. }
  962. } else {
  963. data_buf = ptr;
  964. data_size = len;
  965. ret = len;
  966. }
  967. /* init tickers */
  968. if (!ist->ticker_inited) {
  969. switch (ist->st->codec.codec_type) {
  970. case CODEC_TYPE_AUDIO:
  971. ticker_init(&ist->pts_ticker,
  972. (INT64)ist->st->codec.sample_rate,
  973. (INT64)(1000000));
  974. ist->ticker_inited = 1;
  975. break;
  976. case CODEC_TYPE_VIDEO:
  977. ticker_init(&ist->pts_ticker,
  978. (INT64)ist->st->r_frame_rate,
  979. ((INT64)1000000 * FRAME_RATE_BASE));
  980. ist->ticker_inited = 1;
  981. break;
  982. default:
  983. av_abort();
  984. }
  985. }
  986. /* update pts */
  987. switch(ist->st->codec.codec_type) {
  988. case CODEC_TYPE_AUDIO:
  989. //ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
  990. ist->pts = ticker_abs(&ist->pts_ticker, ist->sample_index);
  991. ist->sample_index += data_size / (2 * ist->st->codec.channels);
  992. ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
  993. break;
  994. case CODEC_TYPE_VIDEO:
  995. ist->frame_number++;
  996. //ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
  997. // ist->st->codec.frame_rate;
  998. ist->pts = ticker_abs(&ist->pts_ticker, ist->frame_number);
  999. ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) /
  1000. ist->st->codec.frame_rate;
  1001. break;
  1002. default:
  1003. av_abort();
  1004. }
  1005. ptr += ret;
  1006. len -= ret;
  1007. /* transcode raw format, encode packets and output them */
  1008. for(i=0;i<nb_ostreams;i++) {
  1009. int frame_size;
  1010. ost = ost_table[i];
  1011. if (ost->source_index == ist_index) {
  1012. os = output_files[ost->file_index];
  1013. if (ost->encoding_needed) {
  1014. switch(ost->st->codec.codec_type) {
  1015. case CODEC_TYPE_AUDIO:
  1016. do_audio_out(os, ost, ist, data_buf, data_size);
  1017. break;
  1018. case CODEC_TYPE_VIDEO:
  1019. do_video_out(os, ost, ist, &picture, &frame_size);
  1020. if (do_vstats)
  1021. do_video_stats(ost, ist, frame_size);
  1022. break;
  1023. default:
  1024. av_abort();
  1025. }
  1026. } else {
  1027. /* no reencoding needed : output the packet directly */
  1028. /* force the input stream PTS */
  1029. os->oformat->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
  1030. }
  1031. }
  1032. }
  1033. }
  1034. discard_packet:
  1035. av_free_packet(&pkt);
  1036. /* dump report by using the first video and audio streams */
  1037. {
  1038. char buf[1024];
  1039. AVFormatContext *oc;
  1040. INT64 total_size, ti;
  1041. AVCodecContext *enc;
  1042. int frame_number, vid;
  1043. double bitrate, ti1;
  1044. static INT64 last_time;
  1045. if ((min_pts - last_time) >= 500000) {
  1046. last_time = min_pts;
  1047. oc = output_files[0];
  1048. total_size = url_ftell(&oc->pb);
  1049. buf[0] = '\0';
  1050. ti = MAXINT64;
  1051. vid = 0;
  1052. for(i=0;i<nb_ostreams;i++) {
  1053. ost = ost_table[i];
  1054. enc = &ost->st->codec;
  1055. ist = ist_table[ost->source_index];
  1056. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  1057. frame_number = ist->frame_number;
  1058. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  1059. frame_number, enc->quality);
  1060. if (do_psnr)
  1061. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  1062. vid = 1;
  1063. }
  1064. /* compute min pts value */
  1065. if (!ist->discard && ist->pts < ti) {
  1066. ti = ist->pts;
  1067. }
  1068. }
  1069. ti1 = (double)ti / 1000000.0;
  1070. if (ti1 < 0.01)
  1071. ti1 = 0.01;
  1072. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  1073. sprintf(buf + strlen(buf),
  1074. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  1075. (double)total_size / 1024, ti1, bitrate);
  1076. fprintf(stderr, "%s \r", buf);
  1077. fflush(stderr);
  1078. }
  1079. }
  1080. }
  1081. term_exit();
  1082. /* dump report by using the first video and audio streams */
  1083. {
  1084. char buf[1024];
  1085. AVFormatContext *oc;
  1086. INT64 total_size, ti;
  1087. AVCodecContext *enc;
  1088. int frame_number, vid;
  1089. double bitrate, ti1;
  1090. oc = output_files[0];
  1091. total_size = url_ftell(&oc->pb);
  1092. buf[0] = '\0';
  1093. ti = MAXINT64;
  1094. vid = 0;
  1095. for(i=0;i<nb_ostreams;i++) {
  1096. ost = ost_table[i];
  1097. enc = &ost->st->codec;
  1098. ist = ist_table[ost->source_index];
  1099. if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
  1100. frame_number = ist->frame_number;
  1101. sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
  1102. frame_number, enc->quality);
  1103. if (do_psnr)
  1104. sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
  1105. vid = 1;
  1106. }
  1107. /* compute min pts value */
  1108. if (!ist->discard && ist->pts < ti) {
  1109. ti = ist->pts;
  1110. }
  1111. }
  1112. ti1 = ti / 1000000.0;
  1113. if (ti1 < 0.01)
  1114. ti1 = 0.01;
  1115. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  1116. sprintf(buf + strlen(buf),
  1117. "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
  1118. (double)total_size / 1024, ti1, bitrate);
  1119. fprintf(stderr, "%s \n", buf);
  1120. }
  1121. /* close each encoder */
  1122. for(i=0;i<nb_ostreams;i++) {
  1123. ost = ost_table[i];
  1124. if (ost->encoding_needed) {
  1125. avcodec_close(&ost->st->codec);
  1126. }
  1127. }
  1128. /* close each decoder */
  1129. for(i=0;i<nb_istreams;i++) {
  1130. ist = ist_table[i];
  1131. if (ist->decoding_needed) {
  1132. avcodec_close(&ist->st->codec);
  1133. }
  1134. }
  1135. /* write the trailer if needed and close file */
  1136. for(i=0;i<nb_output_files;i++) {
  1137. os = output_files[i];
  1138. av_write_trailer(os);
  1139. }
  1140. /* finished ! */
  1141. ret = 0;
  1142. fail1:
  1143. av_free(file_table);
  1144. if (ist_table) {
  1145. for(i=0;i<nb_istreams;i++) {
  1146. ist = ist_table[i];
  1147. av_free(ist);
  1148. }
  1149. av_free(ist_table);
  1150. }
  1151. if (ost_table) {
  1152. for(i=0;i<nb_ostreams;i++) {
  1153. ost = ost_table[i];
  1154. if (ost) {
  1155. fifo_free(&ost->fifo); /* works even if fifo is not
  1156. initialized but set to zero */
  1157. av_free(ost->pict_tmp.data[0]);
  1158. if (ost->video_resample)
  1159. img_resample_close(ost->img_resample_ctx);
  1160. if (ost->audio_resample)
  1161. audio_resample_close(ost->resample);
  1162. av_free(ost);
  1163. }
  1164. }
  1165. av_free(ost_table);
  1166. }
  1167. return ret;
  1168. fail:
  1169. ret = -ENOMEM;
  1170. goto fail1;
  1171. }
  1172. #if 0
  1173. int file_read(const char *filename)
  1174. {
  1175. URLContext *h;
  1176. unsigned char buffer[1024];
  1177. int len, i;
  1178. if (url_open(&h, filename, O_RDONLY) < 0) {
  1179. printf("could not open '%s'\n", filename);
  1180. return -1;
  1181. }
  1182. for(;;) {
  1183. len = url_read(h, buffer, sizeof(buffer));
  1184. if (len <= 0)
  1185. break;
  1186. for(i=0;i<len;i++) putchar(buffer[i]);
  1187. }
  1188. url_close(h);
  1189. return 0;
  1190. }
  1191. #endif
  1192. void show_licence(void)
  1193. {
  1194. printf(
  1195. "ffmpeg version " FFMPEG_VERSION "\n"
  1196. "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
  1197. "This library is free software; you can redistribute it and/or\n"
  1198. "modify it under the terms of the GNU Lesser General Public\n"
  1199. "License as published by the Free Software Foundation; either\n"
  1200. "version 2 of the License, or (at your option) any later version.\n"
  1201. "\n"
  1202. "This library is distributed in the hope that it will be useful,\n"
  1203. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1204. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  1205. "Lesser General Public License for more details.\n"
  1206. "\n"
  1207. "You should have received a copy of the GNU Lesser General Public\n"
  1208. "License along with this library; if not, write to the Free Software\n"
  1209. "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
  1210. );
  1211. exit(1);
  1212. }
  1213. void opt_format(const char *arg)
  1214. {
  1215. file_iformat = av_find_input_format(arg);
  1216. file_oformat = guess_format(arg, NULL, NULL);
  1217. if (!file_iformat && !file_oformat) {
  1218. fprintf(stderr, "Unknown input or output format: %s\n", arg);
  1219. exit(1);
  1220. }
  1221. }
  1222. void opt_video_bitrate(const char *arg)
  1223. {
  1224. video_bit_rate = atoi(arg) * 1000;
  1225. }
  1226. void opt_video_bitrate_tolerance(const char *arg)
  1227. {
  1228. video_bit_rate_tolerance = atoi(arg) * 1000;
  1229. }
  1230. void opt_video_bitrate_max(const char *arg)
  1231. {
  1232. video_rc_max_rate = atoi(arg) * 1000;
  1233. }
  1234. void opt_video_bitrate_min(const char *arg)
  1235. {
  1236. video_rc_min_rate = atoi(arg) * 1000;
  1237. }
  1238. void opt_video_buffer_size(const char *arg)
  1239. {
  1240. video_rc_buffer_size = atoi(arg) * 1000;
  1241. }
  1242. void opt_video_rc_eq(char *arg)
  1243. {
  1244. video_rc_eq = arg;
  1245. }
  1246. void opt_workaround_bugs(const char *arg)
  1247. {
  1248. workaround_bugs = atoi(arg);
  1249. }
  1250. void opt_dct_algo(const char *arg)
  1251. {
  1252. dct_algo = atoi(arg);
  1253. }
  1254. void opt_error_resilience(const char *arg)
  1255. {
  1256. error_resilience = atoi(arg);
  1257. }
  1258. void opt_frame_rate(const char *arg)
  1259. {
  1260. frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
  1261. }
  1262. void opt_frame_crop_top(const char *arg)
  1263. {
  1264. frame_topBand = atoi(arg);
  1265. if (frame_topBand < 0) {
  1266. fprintf(stderr, "Incorrect top crop size\n");
  1267. exit(1);
  1268. }
  1269. if ((frame_topBand % 2) != 0) {
  1270. fprintf(stderr, "Top crop size must be a multiple of 2\n");
  1271. exit(1);
  1272. }
  1273. if ((frame_topBand) >= frame_height){
  1274. fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1275. exit(1);
  1276. }
  1277. frame_height -= frame_topBand;
  1278. }
  1279. void opt_frame_crop_bottom(const char *arg)
  1280. {
  1281. frame_bottomBand = atoi(arg);
  1282. if (frame_bottomBand < 0) {
  1283. fprintf(stderr, "Incorrect bottom crop size\n");
  1284. exit(1);
  1285. }
  1286. if ((frame_bottomBand % 2) != 0) {
  1287. fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
  1288. exit(1);
  1289. }
  1290. if ((frame_bottomBand) >= frame_height){
  1291. fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1292. exit(1);
  1293. }
  1294. frame_height -= frame_bottomBand;
  1295. }
  1296. void opt_frame_crop_left(const char *arg)
  1297. {
  1298. frame_leftBand = atoi(arg);
  1299. if (frame_leftBand < 0) {
  1300. fprintf(stderr, "Incorrect left crop size\n");
  1301. exit(1);
  1302. }
  1303. if ((frame_leftBand % 2) != 0) {
  1304. fprintf(stderr, "Left crop size must be a multiple of 2\n");
  1305. exit(1);
  1306. }
  1307. if ((frame_leftBand) >= frame_width){
  1308. fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1309. exit(1);
  1310. }
  1311. frame_width -= frame_leftBand;
  1312. }
  1313. void opt_frame_crop_right(const char *arg)
  1314. {
  1315. frame_rightBand = atoi(arg);
  1316. if (frame_rightBand < 0) {
  1317. fprintf(stderr, "Incorrect right crop size\n");
  1318. exit(1);
  1319. }
  1320. if ((frame_rightBand % 2) != 0) {
  1321. fprintf(stderr, "Right crop size must be a multiple of 2\n");
  1322. exit(1);
  1323. }
  1324. if ((frame_rightBand) >= frame_width){
  1325. fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
  1326. exit(1);
  1327. }
  1328. frame_width -= frame_rightBand;
  1329. }
  1330. void opt_frame_size(const char *arg)
  1331. {
  1332. parse_image_size(&frame_width, &frame_height, arg);
  1333. if (frame_width <= 0 || frame_height <= 0) {
  1334. fprintf(stderr, "Incorrect frame size\n");
  1335. exit(1);
  1336. }
  1337. if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
  1338. fprintf(stderr, "Frame size must be a multiple of 2\n");
  1339. exit(1);
  1340. }
  1341. }
  1342. void opt_gop_size(const char *arg)
  1343. {
  1344. gop_size = atoi(arg);
  1345. }
  1346. void opt_b_frames(const char *arg)
  1347. {
  1348. b_frames = atoi(arg);
  1349. if (b_frames > FF_MAX_B_FRAMES) {
  1350. fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
  1351. exit(1);
  1352. } else if (b_frames < 1) {
  1353. fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
  1354. exit(1);
  1355. }
  1356. }
  1357. void opt_qscale(const char *arg)
  1358. {
  1359. video_qscale = atoi(arg);
  1360. if (video_qscale < 0 ||
  1361. video_qscale > 31) {
  1362. fprintf(stderr, "qscale must be >= 1 and <= 31\n");
  1363. exit(1);
  1364. }
  1365. }
  1366. void opt_qmin(const char *arg)
  1367. {
  1368. video_qmin = atoi(arg);
  1369. if (video_qmin < 0 ||
  1370. video_qmin > 31) {
  1371. fprintf(stderr, "qmin must be >= 1 and <= 31\n");
  1372. exit(1);
  1373. }
  1374. }
  1375. void opt_qmax(const char *arg)
  1376. {
  1377. video_qmax = atoi(arg);
  1378. if (video_qmax < 0 ||
  1379. video_qmax > 31) {
  1380. fprintf(stderr, "qmax must be >= 1 and <= 31\n");
  1381. exit(1);
  1382. }
  1383. }
  1384. void opt_qdiff(const char *arg)
  1385. {
  1386. video_qdiff = atoi(arg);
  1387. if (video_qdiff < 0 ||
  1388. video_qdiff > 31) {
  1389. fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
  1390. exit(1);
  1391. }
  1392. }
  1393. void opt_qblur(const char *arg)
  1394. {
  1395. video_qblur = atof(arg);
  1396. }
  1397. void opt_qcomp(const char *arg)
  1398. {
  1399. video_qcomp = atof(arg);
  1400. }
  1401. void opt_b_qfactor(const char *arg)
  1402. {
  1403. video_b_qfactor = atof(arg);
  1404. }
  1405. void opt_i_qfactor(const char *arg)
  1406. {
  1407. video_i_qfactor = atof(arg);
  1408. }
  1409. void opt_b_qoffset(const char *arg)
  1410. {
  1411. video_b_qoffset = atof(arg);
  1412. }
  1413. void opt_i_qoffset(const char *arg)
  1414. {
  1415. video_i_qoffset = atof(arg);
  1416. }
  1417. void opt_packet_size(const char *arg)
  1418. {
  1419. packet_size= atoi(arg);
  1420. }
  1421. void opt_audio_bitrate(const char *arg)
  1422. {
  1423. audio_bit_rate = atoi(arg) * 1000;
  1424. }
  1425. void opt_audio_rate(const char *arg)
  1426. {
  1427. audio_sample_rate = atoi(arg);
  1428. }
  1429. void opt_audio_channels(const char *arg)
  1430. {
  1431. audio_channels = atoi(arg);
  1432. }
  1433. void opt_video_device(const char *arg)
  1434. {
  1435. v4l_device = strdup(arg);
  1436. }
  1437. void opt_audio_device(const char *arg)
  1438. {
  1439. audio_device = strdup(arg);
  1440. }
  1441. void opt_audio_codec(const char *arg)
  1442. {
  1443. AVCodec *p;
  1444. p = first_avcodec;
  1445. while (p) {
  1446. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1447. break;
  1448. p = p->next;
  1449. }
  1450. if (p == NULL) {
  1451. fprintf(stderr, "Unknown audio codec '%s'\n", arg);
  1452. exit(1);
  1453. } else {
  1454. audio_codec_id = p->id;
  1455. }
  1456. }
  1457. const char *motion_str[] = {
  1458. "zero",
  1459. "full",
  1460. "log",
  1461. "phods",
  1462. "epzs",
  1463. "x1",
  1464. NULL,
  1465. };
  1466. void opt_motion_estimation(const char *arg)
  1467. {
  1468. const char **p;
  1469. p = motion_str;
  1470. for(;;) {
  1471. if (!*p) {
  1472. fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
  1473. exit(1);
  1474. }
  1475. if (!strcmp(*p, arg))
  1476. break;
  1477. p++;
  1478. }
  1479. me_method = (p - motion_str) + 1;
  1480. }
  1481. void opt_video_codec(const char *arg)
  1482. {
  1483. AVCodec *p;
  1484. p = first_avcodec;
  1485. while (p) {
  1486. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1487. break;
  1488. p = p->next;
  1489. }
  1490. if (p == NULL) {
  1491. fprintf(stderr, "Unknown video codec '%s'\n", arg);
  1492. exit(1);
  1493. } else {
  1494. video_codec_id = p->id;
  1495. }
  1496. }
  1497. void opt_map(const char *arg)
  1498. {
  1499. AVStreamMap *m;
  1500. const char *p;
  1501. p = arg;
  1502. m = &stream_maps[nb_stream_maps++];
  1503. m->file_index = strtol(arg, (char **)&p, 0);
  1504. if (*p)
  1505. p++;
  1506. m->stream_index = strtol(p, (char **)&p, 0);
  1507. }
  1508. void opt_recording_time(const char *arg)
  1509. {
  1510. recording_time = parse_date(arg, 1);
  1511. }
  1512. void print_error(const char *filename, int err)
  1513. {
  1514. switch(err) {
  1515. case AVERROR_NUMEXPECTED:
  1516. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  1517. "Use '%%d' to specify the image number:\n"
  1518. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  1519. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  1520. filename);
  1521. break;
  1522. case AVERROR_INVALIDDATA:
  1523. fprintf(stderr, "%s: Error while parsing header\n", filename);
  1524. break;
  1525. case AVERROR_NOFMT:
  1526. fprintf(stderr, "%s: Unknown format\n", filename);
  1527. break;
  1528. default:
  1529. fprintf(stderr, "%s: Error while opening file\n", filename);
  1530. break;
  1531. }
  1532. }
  1533. void opt_input_file(const char *filename)
  1534. {
  1535. AVFormatContext *ic;
  1536. AVFormatParameters params, *ap = &params;
  1537. int err, i, ret, rfps;
  1538. /* get default parameters from command line */
  1539. memset(ap, 0, sizeof(*ap));
  1540. ap->sample_rate = audio_sample_rate;
  1541. ap->channels = audio_channels;
  1542. ap->frame_rate = frame_rate;
  1543. ap->width = frame_width;
  1544. ap->height = frame_height;
  1545. /* open the input file with generic libav function */
  1546. err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
  1547. if (err < 0) {
  1548. print_error(filename, err);
  1549. exit(1);
  1550. }
  1551. /* If not enough info to get the stream parameters, we decode the
  1552. first frames to get it. (used in mpeg case for example) */
  1553. ret = av_find_stream_info(ic);
  1554. if (ret < 0) {
  1555. fprintf(stderr, "%s: could not find codec parameters\n", filename);
  1556. exit(1);
  1557. }
  1558. /* update the current parameters so that they match the one of the input stream */
  1559. for(i=0;i<ic->nb_streams;i++) {
  1560. AVCodecContext *enc = &ic->streams[i]->codec;
  1561. switch(enc->codec_type) {
  1562. case CODEC_TYPE_AUDIO:
  1563. //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
  1564. audio_channels = enc->channels;
  1565. audio_sample_rate = enc->sample_rate;
  1566. break;
  1567. case CODEC_TYPE_VIDEO:
  1568. frame_height = enc->height;
  1569. frame_width = enc->width;
  1570. rfps = ic->streams[i]->r_frame_rate;
  1571. enc->workaround_bugs = workaround_bugs;
  1572. enc->error_resilience = error_resilience;
  1573. if (enc->frame_rate != rfps) {
  1574. fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
  1575. i, (float)enc->frame_rate / FRAME_RATE_BASE,
  1576. (float)rfps / FRAME_RATE_BASE);
  1577. }
  1578. /* update the current frame rate to match the stream frame rate */
  1579. frame_rate = rfps;
  1580. break;
  1581. default:
  1582. av_abort();
  1583. }
  1584. }
  1585. input_files[nb_input_files] = ic;
  1586. /* dump the file content */
  1587. dump_format(ic, nb_input_files, filename, 0);
  1588. nb_input_files++;
  1589. file_iformat = NULL;
  1590. file_oformat = NULL;
  1591. }
  1592. void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
  1593. {
  1594. int has_video, has_audio, i, j;
  1595. AVFormatContext *ic;
  1596. has_video = 0;
  1597. has_audio = 0;
  1598. for(j=0;j<nb_input_files;j++) {
  1599. ic = input_files[j];
  1600. for(i=0;i<ic->nb_streams;i++) {
  1601. AVCodecContext *enc = &ic->streams[i]->codec;
  1602. switch(enc->codec_type) {
  1603. case CODEC_TYPE_AUDIO:
  1604. has_audio = 1;
  1605. break;
  1606. case CODEC_TYPE_VIDEO:
  1607. has_video = 1;
  1608. break;
  1609. default:
  1610. av_abort();
  1611. }
  1612. }
  1613. }
  1614. *has_video_ptr = has_video;
  1615. *has_audio_ptr = has_audio;
  1616. }
  1617. void opt_output_file(const char *filename)
  1618. {
  1619. AVStream *st;
  1620. AVFormatContext *oc;
  1621. int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
  1622. int codec_id;
  1623. if (!strcmp(filename, "-"))
  1624. filename = "pipe:";
  1625. oc = av_mallocz(sizeof(AVFormatContext));
  1626. if (!file_oformat) {
  1627. file_oformat = guess_format(NULL, filename, NULL);
  1628. if (!file_oformat) {
  1629. fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
  1630. filename);
  1631. exit(1);
  1632. }
  1633. }
  1634. oc->oformat = file_oformat;
  1635. if (!strcmp(file_oformat->name, "ffm") &&
  1636. strstart(filename, "http:", NULL)) {
  1637. /* special case for files sent to ffserver: we get the stream
  1638. parameters from ffserver */
  1639. if (read_ffserver_streams(oc, filename) < 0) {
  1640. fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
  1641. exit(1);
  1642. }
  1643. } else {
  1644. use_video = file_oformat->video_codec != CODEC_ID_NONE;
  1645. use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
  1646. /* disable if no corresponding type found and at least one
  1647. input file */
  1648. if (nb_input_files > 0) {
  1649. check_audio_video_inputs(&input_has_video, &input_has_audio);
  1650. if (!input_has_video)
  1651. use_video = 0;
  1652. if (!input_has_audio)
  1653. use_audio = 0;
  1654. }
  1655. /* manual disable */
  1656. if (audio_disable) {
  1657. use_audio = 0;
  1658. }
  1659. if (video_disable) {
  1660. use_video = 0;
  1661. }
  1662. nb_streams = 0;
  1663. if (use_video) {
  1664. AVCodecContext *video_enc;
  1665. st = av_mallocz(sizeof(AVStream));
  1666. if (!st) {
  1667. fprintf(stderr, "Could not alloc stream\n");
  1668. exit(1);
  1669. }
  1670. video_enc = &st->codec;
  1671. codec_id = file_oformat->video_codec;
  1672. if (video_codec_id != CODEC_ID_NONE)
  1673. codec_id = video_codec_id;
  1674. video_enc->codec_id = codec_id;
  1675. video_enc->codec_type = CODEC_TYPE_VIDEO;
  1676. video_enc->bit_rate = video_bit_rate;
  1677. video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
  1678. video_enc->frame_rate = frame_rate;
  1679. video_enc->width = frame_width;
  1680. video_enc->height = frame_height;
  1681. if (!intra_only)
  1682. video_enc->gop_size = gop_size;
  1683. else
  1684. video_enc->gop_size = 0;
  1685. if (video_qscale || same_quality) {
  1686. video_enc->flags |= CODEC_FLAG_QSCALE;
  1687. video_enc->quality = video_qscale;
  1688. }
  1689. if (use_hq) {
  1690. video_enc->flags |= CODEC_FLAG_HQ;
  1691. }
  1692. if (use_4mv) {
  1693. video_enc->flags |= CODEC_FLAG_HQ;
  1694. video_enc->flags |= CODEC_FLAG_4MV;
  1695. }
  1696. if(use_part)
  1697. video_enc->flags |= CODEC_FLAG_PART;
  1698. if (b_frames) {
  1699. if (codec_id != CODEC_ID_MPEG4) {
  1700. fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
  1701. exit(1);
  1702. }
  1703. video_enc->max_b_frames = b_frames;
  1704. video_enc->b_frame_strategy = 0;
  1705. video_enc->b_quant_factor = 2.0;
  1706. }
  1707. video_enc->qmin = video_qmin;
  1708. video_enc->qmax = video_qmax;
  1709. video_enc->max_qdiff = video_qdiff;
  1710. video_enc->qblur = video_qblur;
  1711. video_enc->qcompress = video_qcomp;
  1712. video_enc->rc_eq = video_rc_eq;
  1713. video_enc->rc_max_rate = video_rc_max_rate;
  1714. video_enc->rc_min_rate = video_rc_min_rate;
  1715. video_enc->rc_buffer_size = video_rc_buffer_size;
  1716. video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
  1717. video_enc->i_quant_factor = video_i_qfactor;
  1718. video_enc->b_quant_factor = video_b_qfactor;
  1719. video_enc->i_quant_offset = video_i_qoffset;
  1720. video_enc->b_quant_offset = video_b_qoffset;
  1721. video_enc->dct_algo = dct_algo;
  1722. if(packet_size){
  1723. video_enc->rtp_mode= 1;
  1724. video_enc->rtp_payload_size= packet_size;
  1725. }
  1726. if (do_psnr)
  1727. video_enc->get_psnr = 1;
  1728. else
  1729. video_enc->get_psnr = 0;
  1730. video_enc->me_method = me_method;
  1731. /* XXX: need to find a way to set codec parameters */
  1732. if (oc->oformat->flags & AVFMT_RGB24) {
  1733. video_enc->pix_fmt = PIX_FMT_RGB24;
  1734. }
  1735. oc->streams[nb_streams] = st;
  1736. nb_streams++;
  1737. }
  1738. if (use_audio) {
  1739. AVCodecContext *audio_enc;
  1740. st = av_mallocz(sizeof(AVStream));
  1741. if (!st) {
  1742. fprintf(stderr, "Could not alloc stream\n");
  1743. exit(1);
  1744. }
  1745. audio_enc = &st->codec;
  1746. codec_id = file_oformat->audio_codec;
  1747. if (audio_codec_id != CODEC_ID_NONE)
  1748. codec_id = audio_codec_id;
  1749. audio_enc->codec_id = codec_id;
  1750. audio_enc->codec_type = CODEC_TYPE_AUDIO;
  1751. audio_enc->bit_rate = audio_bit_rate;
  1752. audio_enc->sample_rate = audio_sample_rate;
  1753. /* For audio codecs other than AC3 we limit */
  1754. /* the number of coded channels to stereo */
  1755. if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
  1756. audio_enc->channels = 2;
  1757. } else
  1758. audio_enc->channels = audio_channels;
  1759. oc->streams[nb_streams] = st;
  1760. nb_streams++;
  1761. }
  1762. oc->nb_streams = nb_streams;
  1763. if (!nb_streams) {
  1764. fprintf(stderr, "No audio or video streams available\n");
  1765. exit(1);
  1766. }
  1767. if (str_title)
  1768. pstrcpy(oc->title, sizeof(oc->title), str_title);
  1769. if (str_author)
  1770. pstrcpy(oc->author, sizeof(oc->author), str_author);
  1771. if (str_copyright)
  1772. pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
  1773. if (str_comment)
  1774. pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
  1775. }
  1776. output_files[nb_output_files] = oc;
  1777. /* dump the file content */
  1778. dump_format(oc, nb_output_files, filename, 1);
  1779. nb_output_files++;
  1780. strcpy(oc->filename, filename);
  1781. /* check filename in case of an image number is expected */
  1782. if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
  1783. if (filename_number_test(oc->filename) < 0) {
  1784. print_error(oc->filename, AVERROR_NUMEXPECTED);
  1785. exit(1);
  1786. }
  1787. }
  1788. if (!(oc->oformat->flags & AVFMT_NOFILE)) {
  1789. /* test if it already exists to avoid loosing precious files */
  1790. if (!file_overwrite &&
  1791. (strchr(filename, ':') == NULL ||
  1792. strstart(filename, "file:", NULL))) {
  1793. if (url_exist(filename)) {
  1794. int c;
  1795. printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
  1796. fflush(stdout);
  1797. c = getchar();
  1798. if (toupper(c) != 'Y') {
  1799. fprintf(stderr, "Not overwriting - exiting\n");
  1800. exit(1);
  1801. }
  1802. }
  1803. }
  1804. /* open the file */
  1805. if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
  1806. fprintf(stderr, "Could not open '%s'\n", filename);
  1807. exit(1);
  1808. }
  1809. }
  1810. /* reset some options */
  1811. file_oformat = NULL;
  1812. file_iformat = NULL;
  1813. audio_disable = 0;
  1814. video_disable = 0;
  1815. audio_codec_id = CODEC_ID_NONE;
  1816. video_codec_id = CODEC_ID_NONE;
  1817. }
  1818. /* prepare dummy protocols for grab */
  1819. void prepare_grab(void)
  1820. {
  1821. int has_video, has_audio, i, j;
  1822. AVFormatContext *oc;
  1823. AVFormatContext *ic;
  1824. AVFormatParameters ap1, *ap = &ap1;
  1825. /* see if audio/video inputs are needed */
  1826. has_video = 0;
  1827. has_audio = 0;
  1828. memset(ap, 0, sizeof(*ap));
  1829. for(j=0;j<nb_output_files;j++) {
  1830. oc = output_files[j];
  1831. for(i=0;i<oc->nb_streams;i++) {
  1832. AVCodecContext *enc = &oc->streams[i]->codec;
  1833. switch(enc->codec_type) {
  1834. case CODEC_TYPE_AUDIO:
  1835. if (enc->sample_rate > ap->sample_rate)
  1836. ap->sample_rate = enc->sample_rate;
  1837. if (enc->channels > ap->channels)
  1838. ap->channels = enc->channels;
  1839. has_audio = 1;
  1840. break;
  1841. case CODEC_TYPE_VIDEO:
  1842. if (enc->width > ap->width)
  1843. ap->width = enc->width;
  1844. if (enc->height > ap->height)
  1845. ap->height = enc->height;
  1846. if (enc->frame_rate > ap->frame_rate)
  1847. ap->frame_rate = enc->frame_rate;
  1848. has_video = 1;
  1849. break;
  1850. default:
  1851. av_abort();
  1852. }
  1853. }
  1854. }
  1855. if (has_video == 0 && has_audio == 0) {
  1856. fprintf(stderr, "Output file must have at least one audio or video stream\n");
  1857. exit(1);
  1858. }
  1859. if (has_video) {
  1860. AVInputFormat *fmt1;
  1861. fmt1 = av_find_input_format("video_grab_device");
  1862. if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
  1863. fprintf(stderr, "Could not find video grab device\n");
  1864. exit(1);
  1865. }
  1866. /* by now video grab has one stream */
  1867. ic->streams[0]->r_frame_rate = ap->frame_rate;
  1868. input_files[nb_input_files] = ic;
  1869. dump_format(ic, nb_input_files, v4l_device, 0);
  1870. nb_input_files++;
  1871. }
  1872. if (has_audio) {
  1873. AVInputFormat *fmt1;
  1874. fmt1 = av_find_input_format("audio_device");
  1875. if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
  1876. fprintf(stderr, "Could not find audio grab device\n");
  1877. exit(1);
  1878. }
  1879. input_files[nb_input_files] = ic;
  1880. dump_format(ic, nb_input_files, audio_device, 0);
  1881. nb_input_files++;
  1882. }
  1883. }
  1884. /* open the necessary output devices for playing */
  1885. void prepare_play(void)
  1886. {
  1887. file_iformat = NULL;
  1888. file_oformat = guess_format("audio_device", NULL, NULL);
  1889. if (!file_oformat) {
  1890. fprintf(stderr, "Could not find audio device\n");
  1891. exit(1);
  1892. }
  1893. opt_output_file(audio_device);
  1894. }
  1895. #ifndef CONFIG_WIN32
  1896. INT64 getutime(void)
  1897. {
  1898. struct rusage rusage;
  1899. getrusage(RUSAGE_SELF, &rusage);
  1900. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  1901. }
  1902. #else
  1903. INT64 getutime(void)
  1904. {
  1905. return av_gettime();
  1906. }
  1907. #endif
  1908. extern int ffm_nopts;
  1909. void opt_bitexact(void)
  1910. {
  1911. avcodec_set_bit_exact();
  1912. /* disable generate of real time pts in ffm (need to be supressed anyway) */
  1913. ffm_nopts = 1;
  1914. }
  1915. void show_formats(void)
  1916. {
  1917. AVInputFormat *ifmt;
  1918. AVOutputFormat *ofmt;
  1919. URLProtocol *up;
  1920. AVCodec *p;
  1921. const char **pp;
  1922. printf("File formats:\n");
  1923. printf(" Encoding:");
  1924. for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
  1925. printf(" %s", ofmt->name);
  1926. }
  1927. printf("\n");
  1928. printf(" Decoding:");
  1929. for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
  1930. printf(" %s", ifmt->name);
  1931. }
  1932. printf("\n");
  1933. printf("Codecs:\n");
  1934. printf(" Encoders:");
  1935. for(p = first_avcodec; p != NULL; p = p->next) {
  1936. if (p->encode)
  1937. printf(" %s", p->name);
  1938. }
  1939. printf("\n");
  1940. printf(" Decoders:");
  1941. for(p = first_avcodec; p != NULL; p = p->next) {
  1942. if (p->decode)
  1943. printf(" %s", p->name);
  1944. }
  1945. printf("\n");
  1946. printf("Supported file protocols:");
  1947. for(up = first_protocol; up != NULL; up = up->next)
  1948. printf(" %s:", up->name);
  1949. printf("\n");
  1950. printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
  1951. printf("Motion estimation methods:");
  1952. pp = motion_str;
  1953. while (*pp) {
  1954. printf(" %s", *pp);
  1955. if ((pp - motion_str + 1) == ME_ZERO)
  1956. printf("(fastest)");
  1957. else if ((pp - motion_str + 1) == ME_FULL)
  1958. printf("(slowest)");
  1959. else if ((pp - motion_str + 1) == ME_EPZS)
  1960. printf("(default)");
  1961. pp++;
  1962. }
  1963. printf("\n");
  1964. exit(1);
  1965. }
  1966. void show_help(void)
  1967. {
  1968. const char *prog;
  1969. const OptionDef *po;
  1970. int i, expert;
  1971. prog = do_play ? "ffplay" : "ffmpeg";
  1972. printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
  1973. prog);
  1974. if (!do_play) {
  1975. printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
  1976. "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
  1977. } else {
  1978. printf("usage: ffplay [options] input_file...\n"
  1979. "Simple audio player\n");
  1980. }
  1981. printf("\n"
  1982. "Main options are:\n");
  1983. for(i=0;i<2;i++) {
  1984. if (i == 1)
  1985. printf("\nAdvanced options are:\n");
  1986. for(po = options; po->name != NULL; po++) {
  1987. char buf[64];
  1988. expert = (po->flags & OPT_EXPERT) != 0;
  1989. if (expert == i) {
  1990. strcpy(buf, po->name);
  1991. if (po->flags & HAS_ARG) {
  1992. strcat(buf, " ");
  1993. strcat(buf, po->argname);
  1994. }
  1995. printf("-%-17s %s\n", buf, po->help);
  1996. }
  1997. }
  1998. }
  1999. exit(1);
  2000. }
  2001. const OptionDef options[] = {
  2002. { "L", 0, {(void*)show_licence}, "show license" },
  2003. { "h", 0, {(void*)show_help}, "show help" },
  2004. { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
  2005. { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
  2006. { "vcd", OPT_BOOL, {(void*)&mpeg_vcd}, "output Video CD MPEG-PS compliant file" },
  2007. { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
  2008. { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
  2009. { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
  2010. { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
  2011. { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
  2012. { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
  2013. { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
  2014. { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
  2015. /* video options */
  2016. { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
  2017. { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
  2018. { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
  2019. { "croptop", HAS_ARG, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
  2020. { "cropbottom", HAS_ARG, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
  2021. { "cropleft", HAS_ARG, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
  2022. { "cropright", HAS_ARG, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
  2023. { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
  2024. { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
  2025. { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
  2026. { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
  2027. { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
  2028. { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
  2029. { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
  2030. { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
  2031. { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
  2032. { "b_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
  2033. { "i_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
  2034. { "b_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
  2035. { "i_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
  2036. { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
  2037. { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
  2038. { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
  2039. { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
  2040. { "bufsize", HAS_ARG, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
  2041. { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
  2042. { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
  2043. { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
  2044. "method" },
  2045. { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo", "algo" },
  2046. { "er", HAS_ARG | OPT_EXPERT, {(void*)opt_error_resilience}, "set error resilience", "" },
  2047. { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
  2048. { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
  2049. { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
  2050. { "part", OPT_BOOL | OPT_EXPERT, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
  2051. { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
  2052. { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "packet size", "size in bits" },
  2053. { "sameq", OPT_BOOL, {(void*)&same_quality},
  2054. "use same video quality as source (implies VBR)" },
  2055. /* audio options */
  2056. { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
  2057. { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
  2058. { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
  2059. { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
  2060. { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
  2061. { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
  2062. { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
  2063. "deinterlace pictures" },
  2064. { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
  2065. "add timings for benchmarking" },
  2066. { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
  2067. "dump each input packet" },
  2068. { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
  2069. { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
  2070. { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
  2071. { NULL, },
  2072. };
  2073. int main(int argc, char **argv)
  2074. {
  2075. int optindex, i;
  2076. const char *opt, *arg;
  2077. const OptionDef *po;
  2078. INT64 ti;
  2079. av_register_all();
  2080. /* detect if invoked as player */
  2081. i = strlen(argv[0]);
  2082. if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
  2083. do_play = 1;
  2084. if (argc <= 1)
  2085. show_help();
  2086. /* parse options */
  2087. optindex = 1;
  2088. while (optindex < argc) {
  2089. opt = argv[optindex++];
  2090. if (opt[0] == '-' && opt[1] != '\0') {
  2091. po = options;
  2092. while (po->name != NULL) {
  2093. if (!strcmp(opt + 1, po->name))
  2094. break;
  2095. po++;
  2096. }
  2097. if (!po->name) {
  2098. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  2099. exit(1);
  2100. }
  2101. arg = NULL;
  2102. if (po->flags & HAS_ARG)
  2103. arg = argv[optindex++];
  2104. if (po->flags & OPT_STRING) {
  2105. char *str;
  2106. str = strdup(arg);
  2107. *po->u.str_arg = str;
  2108. } else if (po->flags & OPT_BOOL) {
  2109. *po->u.int_arg = 1;
  2110. } else {
  2111. po->u.func_arg(arg);
  2112. }
  2113. } else {
  2114. if (!do_play) {
  2115. opt_output_file(opt);
  2116. } else {
  2117. opt_input_file(opt);
  2118. }
  2119. }
  2120. }
  2121. if (!do_play) {
  2122. /* file converter / grab */
  2123. if (nb_output_files <= 0) {
  2124. fprintf(stderr, "Must supply at least one output file\n");
  2125. exit(1);
  2126. }
  2127. if (nb_input_files == 0) {
  2128. prepare_grab();
  2129. }
  2130. } else {
  2131. /* player */
  2132. if (nb_input_files <= 0) {
  2133. fprintf(stderr, "Must supply at least one input file\n");
  2134. exit(1);
  2135. }
  2136. prepare_play();
  2137. }
  2138. ti = getutime();
  2139. av_encode(output_files, nb_output_files, input_files, nb_input_files,
  2140. stream_maps, nb_stream_maps);
  2141. ti = getutime() - ti;
  2142. if (do_benchmark) {
  2143. printf("bench: utime=%0.3fs\n", ti / 1000000.0);
  2144. }
  2145. /* close files */
  2146. for(i=0;i<nb_output_files;i++) {
  2147. if (!(output_files[i]->oformat->flags & AVFMT_NOFILE))
  2148. url_fclose(&output_files[i]->pb);
  2149. }
  2150. for(i=0;i<nb_input_files;i++)
  2151. av_close_input_file(input_files[i]);
  2152. return 0;
  2153. }