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.

2495 lines
78KB

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