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.

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