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.

2525 lines
80KB

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