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.

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