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.

2254 lines
69KB

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