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.

2516 lines
79KB

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