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.

2223 lines
68KB

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