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.

2319 lines
71KB

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