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.

2463 lines
81KB

  1. /*
  2. * avconv main
  3. * Copyright (c) 2000-2011 The libav developers.
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include <ctype.h>
  23. #include <string.h>
  24. #include <math.h>
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include <signal.h>
  28. #include <limits.h>
  29. #include "libavformat/avformat.h"
  30. #include "libavdevice/avdevice.h"
  31. #include "libswscale/swscale.h"
  32. #include "libavresample/avresample.h"
  33. #include "libavutil/opt.h"
  34. #include "libavutil/audioconvert.h"
  35. #include "libavutil/parseutils.h"
  36. #include "libavutil/samplefmt.h"
  37. #include "libavutil/colorspace.h"
  38. #include "libavutil/fifo.h"
  39. #include "libavutil/intreadwrite.h"
  40. #include "libavutil/dict.h"
  41. #include "libavutil/mathematics.h"
  42. #include "libavutil/pixdesc.h"
  43. #include "libavutil/avstring.h"
  44. #include "libavutil/libm.h"
  45. #include "libavutil/imgutils.h"
  46. #include "libavutil/time.h"
  47. #include "libavformat/os_support.h"
  48. # include "libavfilter/avfilter.h"
  49. # include "libavfilter/avfiltergraph.h"
  50. # include "libavfilter/buffersrc.h"
  51. # include "libavfilter/buffersink.h"
  52. #if HAVE_SYS_RESOURCE_H
  53. #include <sys/types.h>
  54. #include <sys/resource.h>
  55. #elif HAVE_GETPROCESSTIMES
  56. #include <windows.h>
  57. #endif
  58. #if HAVE_GETPROCESSMEMORYINFO
  59. #include <windows.h>
  60. #include <psapi.h>
  61. #endif
  62. #if HAVE_SYS_SELECT_H
  63. #include <sys/select.h>
  64. #endif
  65. #if HAVE_PTHREADS
  66. #include <pthread.h>
  67. #endif
  68. #include <time.h>
  69. #include "avconv.h"
  70. #include "cmdutils.h"
  71. #include "libavutil/avassert.h"
  72. const char program_name[] = "avconv";
  73. const int program_birth_year = 2000;
  74. static FILE *vstats_file;
  75. static int64_t video_size = 0;
  76. static int64_t audio_size = 0;
  77. static int64_t extra_size = 0;
  78. static int nb_frames_dup = 0;
  79. static int nb_frames_drop = 0;
  80. #if HAVE_PTHREADS
  81. /* signal to input threads that they should exit; set by the main thread */
  82. static int transcoding_finished;
  83. #endif
  84. #define DEFAULT_PASS_LOGFILENAME_PREFIX "av2pass"
  85. InputStream **input_streams = NULL;
  86. int nb_input_streams = 0;
  87. InputFile **input_files = NULL;
  88. int nb_input_files = 0;
  89. OutputStream **output_streams = NULL;
  90. int nb_output_streams = 0;
  91. OutputFile **output_files = NULL;
  92. int nb_output_files = 0;
  93. FilterGraph **filtergraphs;
  94. int nb_filtergraphs;
  95. static void term_exit(void)
  96. {
  97. av_log(NULL, AV_LOG_QUIET, "");
  98. }
  99. static volatile int received_sigterm = 0;
  100. static volatile int received_nb_signals = 0;
  101. static void
  102. sigterm_handler(int sig)
  103. {
  104. received_sigterm = sig;
  105. received_nb_signals++;
  106. term_exit();
  107. }
  108. static void term_init(void)
  109. {
  110. signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
  111. signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
  112. #ifdef SIGXCPU
  113. signal(SIGXCPU, sigterm_handler);
  114. #endif
  115. }
  116. static int decode_interrupt_cb(void *ctx)
  117. {
  118. return received_nb_signals > 1;
  119. }
  120. const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
  121. static void exit_program(void)
  122. {
  123. int i, j;
  124. for (i = 0; i < nb_filtergraphs; i++) {
  125. avfilter_graph_free(&filtergraphs[i]->graph);
  126. for (j = 0; j < filtergraphs[i]->nb_inputs; j++) {
  127. av_freep(&filtergraphs[i]->inputs[j]->name);
  128. av_freep(&filtergraphs[i]->inputs[j]);
  129. }
  130. av_freep(&filtergraphs[i]->inputs);
  131. for (j = 0; j < filtergraphs[i]->nb_outputs; j++) {
  132. av_freep(&filtergraphs[i]->outputs[j]->name);
  133. av_freep(&filtergraphs[i]->outputs[j]);
  134. }
  135. av_freep(&filtergraphs[i]->outputs);
  136. av_freep(&filtergraphs[i]);
  137. }
  138. av_freep(&filtergraphs);
  139. /* close files */
  140. for (i = 0; i < nb_output_files; i++) {
  141. AVFormatContext *s = output_files[i]->ctx;
  142. if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
  143. avio_close(s->pb);
  144. avformat_free_context(s);
  145. av_dict_free(&output_files[i]->opts);
  146. av_freep(&output_files[i]);
  147. }
  148. for (i = 0; i < nb_output_streams; i++) {
  149. AVBitStreamFilterContext *bsfc = output_streams[i]->bitstream_filters;
  150. while (bsfc) {
  151. AVBitStreamFilterContext *next = bsfc->next;
  152. av_bitstream_filter_close(bsfc);
  153. bsfc = next;
  154. }
  155. output_streams[i]->bitstream_filters = NULL;
  156. avcodec_free_frame(&output_streams[i]->filtered_frame);
  157. av_freep(&output_streams[i]->forced_keyframes);
  158. av_freep(&output_streams[i]->avfilter);
  159. av_freep(&output_streams[i]->logfile_prefix);
  160. av_freep(&output_streams[i]);
  161. }
  162. for (i = 0; i < nb_input_files; i++) {
  163. avformat_close_input(&input_files[i]->ctx);
  164. av_freep(&input_files[i]);
  165. }
  166. for (i = 0; i < nb_input_streams; i++) {
  167. avcodec_free_frame(&input_streams[i]->decoded_frame);
  168. av_dict_free(&input_streams[i]->opts);
  169. free_buffer_pool(&input_streams[i]->buffer_pool);
  170. av_freep(&input_streams[i]->filters);
  171. av_freep(&input_streams[i]);
  172. }
  173. if (vstats_file)
  174. fclose(vstats_file);
  175. av_free(vstats_filename);
  176. av_freep(&input_streams);
  177. av_freep(&input_files);
  178. av_freep(&output_streams);
  179. av_freep(&output_files);
  180. uninit_opts();
  181. avfilter_uninit();
  182. avformat_network_deinit();
  183. if (received_sigterm) {
  184. av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n",
  185. (int) received_sigterm);
  186. exit (255);
  187. }
  188. }
  189. void assert_avoptions(AVDictionary *m)
  190. {
  191. AVDictionaryEntry *t;
  192. if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
  193. av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
  194. exit(1);
  195. }
  196. }
  197. static void assert_codec_experimental(AVCodecContext *c, int encoder)
  198. {
  199. const char *codec_string = encoder ? "encoder" : "decoder";
  200. AVCodec *codec;
  201. if (c->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
  202. c->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
  203. av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
  204. "results.\nAdd '-strict experimental' if you want to use it.\n",
  205. codec_string, c->codec->name);
  206. codec = encoder ? avcodec_find_encoder(c->codec->id) : avcodec_find_decoder(c->codec->id);
  207. if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL))
  208. av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
  209. codec_string, codec->name);
  210. exit(1);
  211. }
  212. }
  213. /*
  214. * Update the requested input sample format based on the output sample format.
  215. * This is currently only used to request float output from decoders which
  216. * support multiple sample formats, one of which is AV_SAMPLE_FMT_FLT.
  217. * Ideally this will be removed in the future when decoders do not do format
  218. * conversion and only output in their native format.
  219. */
  220. static void update_sample_fmt(AVCodecContext *dec, AVCodec *dec_codec,
  221. AVCodecContext *enc)
  222. {
  223. /* if sample formats match or a decoder sample format has already been
  224. requested, just return */
  225. if (enc->sample_fmt == dec->sample_fmt ||
  226. dec->request_sample_fmt > AV_SAMPLE_FMT_NONE)
  227. return;
  228. /* if decoder supports more than one output format */
  229. if (dec_codec && dec_codec->sample_fmts &&
  230. dec_codec->sample_fmts[0] != AV_SAMPLE_FMT_NONE &&
  231. dec_codec->sample_fmts[1] != AV_SAMPLE_FMT_NONE) {
  232. const enum AVSampleFormat *p;
  233. int min_dec = INT_MAX, min_inc = INT_MAX;
  234. enum AVSampleFormat dec_fmt = AV_SAMPLE_FMT_NONE;
  235. enum AVSampleFormat inc_fmt = AV_SAMPLE_FMT_NONE;
  236. /* find a matching sample format in the encoder */
  237. for (p = dec_codec->sample_fmts; *p != AV_SAMPLE_FMT_NONE; p++) {
  238. if (*p == enc->sample_fmt) {
  239. dec->request_sample_fmt = *p;
  240. return;
  241. } else {
  242. enum AVSampleFormat dfmt = av_get_packed_sample_fmt(*p);
  243. enum AVSampleFormat efmt = av_get_packed_sample_fmt(enc->sample_fmt);
  244. int fmt_diff = 32 * abs(dfmt - efmt);
  245. if (av_sample_fmt_is_planar(*p) !=
  246. av_sample_fmt_is_planar(enc->sample_fmt))
  247. fmt_diff++;
  248. if (dfmt == efmt) {
  249. min_inc = fmt_diff;
  250. inc_fmt = *p;
  251. } else if (dfmt > efmt) {
  252. if (fmt_diff < min_inc) {
  253. min_inc = fmt_diff;
  254. inc_fmt = *p;
  255. }
  256. } else {
  257. if (fmt_diff < min_dec) {
  258. min_dec = fmt_diff;
  259. dec_fmt = *p;
  260. }
  261. }
  262. }
  263. }
  264. /* if none match, provide the one that matches quality closest */
  265. dec->request_sample_fmt = min_inc != INT_MAX ? inc_fmt : dec_fmt;
  266. }
  267. }
  268. static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
  269. {
  270. AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
  271. AVCodecContext *avctx = ost->st->codec;
  272. int ret;
  273. /*
  274. * Audio encoders may split the packets -- #frames in != #packets out.
  275. * But there is no reordering, so we can limit the number of output packets
  276. * by simply dropping them here.
  277. * Counting encoded video frames needs to be done separately because of
  278. * reordering, see do_video_out()
  279. */
  280. if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) {
  281. if (ost->frame_number >= ost->max_frames) {
  282. av_free_packet(pkt);
  283. return;
  284. }
  285. ost->frame_number++;
  286. }
  287. while (bsfc) {
  288. AVPacket new_pkt = *pkt;
  289. int a = av_bitstream_filter_filter(bsfc, avctx, NULL,
  290. &new_pkt.data, &new_pkt.size,
  291. pkt->data, pkt->size,
  292. pkt->flags & AV_PKT_FLAG_KEY);
  293. if (a > 0) {
  294. av_free_packet(pkt);
  295. new_pkt.destruct = av_destruct_packet;
  296. } else if (a < 0) {
  297. av_log(NULL, AV_LOG_ERROR, "%s failed for stream %d, codec %s",
  298. bsfc->filter->name, pkt->stream_index,
  299. avctx->codec ? avctx->codec->name : "copy");
  300. print_error("", a);
  301. if (exit_on_error)
  302. exit(1);
  303. }
  304. *pkt = new_pkt;
  305. bsfc = bsfc->next;
  306. }
  307. pkt->stream_index = ost->index;
  308. ret = av_interleaved_write_frame(s, pkt);
  309. if (ret < 0) {
  310. print_error("av_interleaved_write_frame()", ret);
  311. exit(1);
  312. }
  313. }
  314. static int check_recording_time(OutputStream *ost)
  315. {
  316. OutputFile *of = output_files[ost->file_index];
  317. if (of->recording_time != INT64_MAX &&
  318. av_compare_ts(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, of->recording_time,
  319. AV_TIME_BASE_Q) >= 0) {
  320. ost->finished = 1;
  321. return 0;
  322. }
  323. return 1;
  324. }
  325. static void do_audio_out(AVFormatContext *s, OutputStream *ost,
  326. AVFrame *frame)
  327. {
  328. AVCodecContext *enc = ost->st->codec;
  329. AVPacket pkt;
  330. int got_packet = 0;
  331. av_init_packet(&pkt);
  332. pkt.data = NULL;
  333. pkt.size = 0;
  334. if (!check_recording_time(ost))
  335. return;
  336. if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
  337. frame->pts = ost->sync_opts;
  338. ost->sync_opts = frame->pts + frame->nb_samples;
  339. if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
  340. av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
  341. exit(1);
  342. }
  343. if (got_packet) {
  344. if (pkt.pts != AV_NOPTS_VALUE)
  345. pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
  346. if (pkt.dts != AV_NOPTS_VALUE)
  347. pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
  348. if (pkt.duration > 0)
  349. pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);
  350. write_frame(s, &pkt, ost);
  351. audio_size += pkt.size;
  352. }
  353. }
  354. static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void **bufp)
  355. {
  356. AVCodecContext *dec;
  357. AVPicture *picture2;
  358. AVPicture picture_tmp;
  359. uint8_t *buf = 0;
  360. dec = ist->st->codec;
  361. /* deinterlace : must be done before any resize */
  362. if (do_deinterlace) {
  363. int size;
  364. /* create temporary picture */
  365. size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
  366. buf = av_malloc(size);
  367. if (!buf)
  368. return;
  369. picture2 = &picture_tmp;
  370. avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
  371. if (avpicture_deinterlace(picture2, picture,
  372. dec->pix_fmt, dec->width, dec->height) < 0) {
  373. /* if error, do not deinterlace */
  374. av_log(NULL, AV_LOG_WARNING, "Deinterlacing failed\n");
  375. av_free(buf);
  376. buf = NULL;
  377. picture2 = picture;
  378. }
  379. } else {
  380. picture2 = picture;
  381. }
  382. if (picture != picture2)
  383. *picture = *picture2;
  384. *bufp = buf;
  385. }
  386. static void do_subtitle_out(AVFormatContext *s,
  387. OutputStream *ost,
  388. InputStream *ist,
  389. AVSubtitle *sub,
  390. int64_t pts)
  391. {
  392. static uint8_t *subtitle_out = NULL;
  393. int subtitle_out_max_size = 1024 * 1024;
  394. int subtitle_out_size, nb, i;
  395. AVCodecContext *enc;
  396. AVPacket pkt;
  397. if (pts == AV_NOPTS_VALUE) {
  398. av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
  399. if (exit_on_error)
  400. exit(1);
  401. return;
  402. }
  403. enc = ost->st->codec;
  404. if (!subtitle_out) {
  405. subtitle_out = av_malloc(subtitle_out_max_size);
  406. }
  407. /* Note: DVB subtitle need one packet to draw them and one other
  408. packet to clear them */
  409. /* XXX: signal it in the codec context ? */
  410. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
  411. nb = 2;
  412. else
  413. nb = 1;
  414. for (i = 0; i < nb; i++) {
  415. ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base);
  416. if (!check_recording_time(ost))
  417. return;
  418. sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
  419. // start_display_time is required to be 0
  420. sub->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
  421. sub->end_display_time -= sub->start_display_time;
  422. sub->start_display_time = 0;
  423. subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
  424. subtitle_out_max_size, sub);
  425. if (subtitle_out_size < 0) {
  426. av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
  427. exit(1);
  428. }
  429. av_init_packet(&pkt);
  430. pkt.data = subtitle_out;
  431. pkt.size = subtitle_out_size;
  432. pkt.pts = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
  433. if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
  434. /* XXX: the pts correction is handled here. Maybe handling
  435. it in the codec would be better */
  436. if (i == 0)
  437. pkt.pts += 90 * sub->start_display_time;
  438. else
  439. pkt.pts += 90 * sub->end_display_time;
  440. }
  441. write_frame(s, &pkt, ost);
  442. }
  443. }
  444. static void do_video_out(AVFormatContext *s,
  445. OutputStream *ost,
  446. AVFrame *in_picture,
  447. int *frame_size)
  448. {
  449. int ret, format_video_sync;
  450. AVPacket pkt;
  451. AVCodecContext *enc = ost->st->codec;
  452. *frame_size = 0;
  453. format_video_sync = video_sync_method;
  454. if (format_video_sync == VSYNC_AUTO)
  455. format_video_sync = (s->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH :
  456. (s->oformat->flags & AVFMT_VARIABLE_FPS) ? VSYNC_VFR : VSYNC_CFR;
  457. if (format_video_sync != VSYNC_PASSTHROUGH &&
  458. ost->frame_number &&
  459. in_picture->pts != AV_NOPTS_VALUE &&
  460. in_picture->pts < ost->sync_opts) {
  461. nb_frames_drop++;
  462. av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");
  463. return;
  464. }
  465. if (in_picture->pts == AV_NOPTS_VALUE)
  466. in_picture->pts = ost->sync_opts;
  467. ost->sync_opts = in_picture->pts;
  468. if (!ost->frame_number)
  469. ost->first_pts = in_picture->pts;
  470. av_init_packet(&pkt);
  471. pkt.data = NULL;
  472. pkt.size = 0;
  473. if (!check_recording_time(ost) ||
  474. ost->frame_number >= ost->max_frames)
  475. return;
  476. if (s->oformat->flags & AVFMT_RAWPICTURE &&
  477. enc->codec->id == AV_CODEC_ID_RAWVIDEO) {
  478. /* raw pictures are written as AVPicture structure to
  479. avoid any copies. We support temporarily the older
  480. method. */
  481. enc->coded_frame->interlaced_frame = in_picture->interlaced_frame;
  482. enc->coded_frame->top_field_first = in_picture->top_field_first;
  483. pkt.data = (uint8_t *)in_picture;
  484. pkt.size = sizeof(AVPicture);
  485. pkt.pts = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base);
  486. pkt.flags |= AV_PKT_FLAG_KEY;
  487. write_frame(s, &pkt, ost);
  488. } else {
  489. int got_packet;
  490. AVFrame big_picture;
  491. big_picture = *in_picture;
  492. /* better than nothing: use input picture interlaced
  493. settings */
  494. big_picture.interlaced_frame = in_picture->interlaced_frame;
  495. if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {
  496. if (ost->top_field_first == -1)
  497. big_picture.top_field_first = in_picture->top_field_first;
  498. else
  499. big_picture.top_field_first = !!ost->top_field_first;
  500. }
  501. big_picture.quality = ost->st->codec->global_quality;
  502. if (!enc->me_threshold)
  503. big_picture.pict_type = 0;
  504. if (ost->forced_kf_index < ost->forced_kf_count &&
  505. big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
  506. big_picture.pict_type = AV_PICTURE_TYPE_I;
  507. ost->forced_kf_index++;
  508. }
  509. ret = avcodec_encode_video2(enc, &pkt, &big_picture, &got_packet);
  510. if (ret < 0) {
  511. av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
  512. exit(1);
  513. }
  514. if (got_packet) {
  515. if (pkt.pts != AV_NOPTS_VALUE)
  516. pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
  517. if (pkt.dts != AV_NOPTS_VALUE)
  518. pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
  519. write_frame(s, &pkt, ost);
  520. *frame_size = pkt.size;
  521. video_size += pkt.size;
  522. /* if two pass, output log */
  523. if (ost->logfile && enc->stats_out) {
  524. fprintf(ost->logfile, "%s", enc->stats_out);
  525. }
  526. }
  527. }
  528. ost->sync_opts++;
  529. /*
  530. * For video, number of frames in == number of packets out.
  531. * But there may be reordering, so we can't throw away frames on encoder
  532. * flush, we need to limit them here, before they go into encoder.
  533. */
  534. ost->frame_number++;
  535. }
  536. static double psnr(double d)
  537. {
  538. return -10.0 * log(d) / log(10.0);
  539. }
  540. static void do_video_stats(AVFormatContext *os, OutputStream *ost,
  541. int frame_size)
  542. {
  543. AVCodecContext *enc;
  544. int frame_number;
  545. double ti1, bitrate, avg_bitrate;
  546. /* this is executed just the first time do_video_stats is called */
  547. if (!vstats_file) {
  548. vstats_file = fopen(vstats_filename, "w");
  549. if (!vstats_file) {
  550. perror("fopen");
  551. exit(1);
  552. }
  553. }
  554. enc = ost->st->codec;
  555. if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  556. frame_number = ost->frame_number;
  557. fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA);
  558. if (enc->flags&CODEC_FLAG_PSNR)
  559. fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
  560. fprintf(vstats_file,"f_size= %6d ", frame_size);
  561. /* compute pts value */
  562. ti1 = ost->sync_opts * av_q2d(enc->time_base);
  563. if (ti1 < 0.01)
  564. ti1 = 0.01;
  565. bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
  566. avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
  567. fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
  568. (double)video_size / 1024, ti1, bitrate, avg_bitrate);
  569. fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
  570. }
  571. }
  572. /*
  573. * Read one frame for lavfi output for ost and encode it.
  574. */
  575. static int poll_filter(OutputStream *ost)
  576. {
  577. OutputFile *of = output_files[ost->file_index];
  578. AVFilterBufferRef *picref;
  579. AVFrame *filtered_frame = NULL;
  580. int frame_size, ret;
  581. if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) {
  582. return AVERROR(ENOMEM);
  583. } else
  584. avcodec_get_frame_defaults(ost->filtered_frame);
  585. filtered_frame = ost->filtered_frame;
  586. if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
  587. !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
  588. ret = av_buffersink_read_samples(ost->filter->filter, &picref,
  589. ost->st->codec->frame_size);
  590. else
  591. ret = av_buffersink_read(ost->filter->filter, &picref);
  592. if (ret < 0)
  593. return ret;
  594. avfilter_copy_buf_props(filtered_frame, picref);
  595. if (picref->pts != AV_NOPTS_VALUE) {
  596. filtered_frame->pts = av_rescale_q(picref->pts,
  597. ost->filter->filter->inputs[0]->time_base,
  598. ost->st->codec->time_base) -
  599. av_rescale_q(of->start_time,
  600. AV_TIME_BASE_Q,
  601. ost->st->codec->time_base);
  602. if (of->start_time && filtered_frame->pts < 0) {
  603. avfilter_unref_buffer(picref);
  604. return 0;
  605. }
  606. }
  607. switch (ost->filter->filter->inputs[0]->type) {
  608. case AVMEDIA_TYPE_VIDEO:
  609. if (!ost->frame_aspect_ratio)
  610. ost->st->codec->sample_aspect_ratio = picref->video->pixel_aspect;
  611. do_video_out(of->ctx, ost, filtered_frame, &frame_size);
  612. if (vstats_filename && frame_size)
  613. do_video_stats(of->ctx, ost, frame_size);
  614. break;
  615. case AVMEDIA_TYPE_AUDIO:
  616. do_audio_out(of->ctx, ost, filtered_frame);
  617. break;
  618. default:
  619. // TODO support subtitle filters
  620. av_assert0(0);
  621. }
  622. avfilter_unref_buffer(picref);
  623. return 0;
  624. }
  625. /*
  626. * Read as many frames from possible from lavfi and encode them.
  627. *
  628. * Always read from the active stream with the lowest timestamp. If no frames
  629. * are available for it then return EAGAIN and wait for more input. This way we
  630. * can use lavfi sources that generate unlimited amount of frames without memory
  631. * usage exploding.
  632. */
  633. static int poll_filters(void)
  634. {
  635. int i, j, ret = 0;
  636. while (ret >= 0 && !received_sigterm) {
  637. OutputStream *ost = NULL;
  638. int64_t min_pts = INT64_MAX;
  639. /* choose output stream with the lowest timestamp */
  640. for (i = 0; i < nb_output_streams; i++) {
  641. int64_t pts = output_streams[i]->sync_opts;
  642. if (!output_streams[i]->filter || output_streams[i]->finished)
  643. continue;
  644. pts = av_rescale_q(pts, output_streams[i]->st->codec->time_base,
  645. AV_TIME_BASE_Q);
  646. if (pts < min_pts) {
  647. min_pts = pts;
  648. ost = output_streams[i];
  649. }
  650. }
  651. if (!ost)
  652. break;
  653. ret = poll_filter(ost);
  654. if (ret == AVERROR_EOF) {
  655. OutputFile *of = output_files[ost->file_index];
  656. ost->finished = 1;
  657. if (of->shortest) {
  658. for (j = 0; j < of->ctx->nb_streams; j++)
  659. output_streams[of->ost_index + j]->finished = 1;
  660. }
  661. ret = 0;
  662. } else if (ret == AVERROR(EAGAIN))
  663. return 0;
  664. }
  665. return ret;
  666. }
  667. static void print_report(int is_last_report, int64_t timer_start)
  668. {
  669. char buf[1024];
  670. OutputStream *ost;
  671. AVFormatContext *oc;
  672. int64_t total_size;
  673. AVCodecContext *enc;
  674. int frame_number, vid, i;
  675. double bitrate, ti1, pts;
  676. static int64_t last_time = -1;
  677. static int qp_histogram[52];
  678. if (!print_stats && !is_last_report)
  679. return;
  680. if (!is_last_report) {
  681. int64_t cur_time;
  682. /* display the report every 0.5 seconds */
  683. cur_time = av_gettime();
  684. if (last_time == -1) {
  685. last_time = cur_time;
  686. return;
  687. }
  688. if ((cur_time - last_time) < 500000)
  689. return;
  690. last_time = cur_time;
  691. }
  692. oc = output_files[0]->ctx;
  693. total_size = avio_size(oc->pb);
  694. if (total_size < 0) // FIXME improve avio_size() so it works with non seekable output too
  695. total_size = avio_tell(oc->pb);
  696. buf[0] = '\0';
  697. ti1 = 1e10;
  698. vid = 0;
  699. for (i = 0; i < nb_output_streams; i++) {
  700. float q = -1;
  701. ost = output_streams[i];
  702. enc = ost->st->codec;
  703. if (!ost->stream_copy && enc->coded_frame)
  704. q = enc->coded_frame->quality / (float)FF_QP2LAMBDA;
  705. if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  706. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
  707. }
  708. if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  709. float t = (av_gettime() - timer_start) / 1000000.0;
  710. frame_number = ost->frame_number;
  711. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ",
  712. frame_number, (t > 1) ? (int)(frame_number / t + 0.5) : 0, q);
  713. if (is_last_report)
  714. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
  715. if (qp_hist) {
  716. int j;
  717. int qp = lrintf(q);
  718. if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
  719. qp_histogram[qp]++;
  720. for (j = 0; j < 32; j++)
  721. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log2(qp_histogram[j] + 1)));
  722. }
  723. if (enc->flags&CODEC_FLAG_PSNR) {
  724. int j;
  725. double error, error_sum = 0;
  726. double scale, scale_sum = 0;
  727. char type[3] = { 'Y','U','V' };
  728. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
  729. for (j = 0; j < 3; j++) {
  730. if (is_last_report) {
  731. error = enc->error[j];
  732. scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
  733. } else {
  734. error = enc->coded_frame->error[j];
  735. scale = enc->width * enc->height * 255.0 * 255.0;
  736. }
  737. if (j)
  738. scale /= 4;
  739. error_sum += error;
  740. scale_sum += scale;
  741. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error / scale));
  742. }
  743. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum));
  744. }
  745. vid = 1;
  746. }
  747. /* compute min output value */
  748. pts = (double)ost->st->pts.val * av_q2d(ost->st->time_base);
  749. if ((pts < ti1) && (pts > 0))
  750. ti1 = pts;
  751. }
  752. if (ti1 < 0.01)
  753. ti1 = 0.01;
  754. bitrate = (double)(total_size * 8) / ti1 / 1000.0;
  755. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
  756. "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
  757. (double)total_size / 1024, ti1, bitrate);
  758. if (nb_frames_dup || nb_frames_drop)
  759. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
  760. nb_frames_dup, nb_frames_drop);
  761. av_log(NULL, AV_LOG_INFO, "%s \r", buf);
  762. fflush(stderr);
  763. if (is_last_report) {
  764. int64_t raw= audio_size + video_size + extra_size;
  765. av_log(NULL, AV_LOG_INFO, "\n");
  766. av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
  767. video_size / 1024.0,
  768. audio_size / 1024.0,
  769. extra_size / 1024.0,
  770. 100.0 * (total_size - raw) / raw
  771. );
  772. }
  773. }
  774. static void flush_encoders(void)
  775. {
  776. int i, ret;
  777. for (i = 0; i < nb_output_streams; i++) {
  778. OutputStream *ost = output_streams[i];
  779. AVCodecContext *enc = ost->st->codec;
  780. AVFormatContext *os = output_files[ost->file_index]->ctx;
  781. int stop_encoding = 0;
  782. if (!ost->encoding_needed)
  783. continue;
  784. if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
  785. continue;
  786. if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == AV_CODEC_ID_RAWVIDEO)
  787. continue;
  788. for (;;) {
  789. int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL;
  790. const char *desc;
  791. int64_t *size;
  792. switch (ost->st->codec->codec_type) {
  793. case AVMEDIA_TYPE_AUDIO:
  794. encode = avcodec_encode_audio2;
  795. desc = "Audio";
  796. size = &audio_size;
  797. break;
  798. case AVMEDIA_TYPE_VIDEO:
  799. encode = avcodec_encode_video2;
  800. desc = "Video";
  801. size = &video_size;
  802. break;
  803. default:
  804. stop_encoding = 1;
  805. }
  806. if (encode) {
  807. AVPacket pkt;
  808. int got_packet;
  809. av_init_packet(&pkt);
  810. pkt.data = NULL;
  811. pkt.size = 0;
  812. ret = encode(enc, &pkt, NULL, &got_packet);
  813. if (ret < 0) {
  814. av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
  815. exit(1);
  816. }
  817. *size += ret;
  818. if (ost->logfile && enc->stats_out) {
  819. fprintf(ost->logfile, "%s", enc->stats_out);
  820. }
  821. if (!got_packet) {
  822. stop_encoding = 1;
  823. break;
  824. }
  825. if (pkt.pts != AV_NOPTS_VALUE)
  826. pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
  827. if (pkt.dts != AV_NOPTS_VALUE)
  828. pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
  829. write_frame(os, &pkt, ost);
  830. }
  831. if (stop_encoding)
  832. break;
  833. }
  834. }
  835. }
  836. /*
  837. * Check whether a packet from ist should be written into ost at this time
  838. */
  839. static int check_output_constraints(InputStream *ist, OutputStream *ost)
  840. {
  841. OutputFile *of = output_files[ost->file_index];
  842. int ist_index = input_files[ist->file_index]->ist_index + ist->st->index;
  843. if (ost->source_index != ist_index)
  844. return 0;
  845. if (of->start_time && ist->last_dts < of->start_time)
  846. return 0;
  847. return 1;
  848. }
  849. static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
  850. {
  851. OutputFile *of = output_files[ost->file_index];
  852. int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
  853. AVPacket opkt;
  854. av_init_packet(&opkt);
  855. if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
  856. !ost->copy_initial_nonkeyframes)
  857. return;
  858. if (of->recording_time != INT64_MAX &&
  859. ist->last_dts >= of->recording_time + of->start_time) {
  860. ost->finished = 1;
  861. return;
  862. }
  863. /* force the input stream PTS */
  864. if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  865. audio_size += pkt->size;
  866. else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  867. video_size += pkt->size;
  868. ost->sync_opts++;
  869. }
  870. if (pkt->pts != AV_NOPTS_VALUE)
  871. opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
  872. else
  873. opkt.pts = AV_NOPTS_VALUE;
  874. if (pkt->dts == AV_NOPTS_VALUE)
  875. opkt.dts = av_rescale_q(ist->last_dts, AV_TIME_BASE_Q, ost->st->time_base);
  876. else
  877. opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
  878. opkt.dts -= ost_tb_start_time;
  879. opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
  880. opkt.flags = pkt->flags;
  881. // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
  882. if ( ost->st->codec->codec_id != AV_CODEC_ID_H264
  883. && ost->st->codec->codec_id != AV_CODEC_ID_MPEG1VIDEO
  884. && ost->st->codec->codec_id != AV_CODEC_ID_MPEG2VIDEO
  885. && ost->st->codec->codec_id != AV_CODEC_ID_VC1
  886. ) {
  887. if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY))
  888. opkt.destruct = av_destruct_packet;
  889. } else {
  890. opkt.data = pkt->data;
  891. opkt.size = pkt->size;
  892. }
  893. write_frame(of->ctx, &opkt, ost);
  894. ost->st->codec->frame_number++;
  895. av_free_packet(&opkt);
  896. }
  897. static void rate_emu_sleep(InputStream *ist)
  898. {
  899. if (input_files[ist->file_index]->rate_emu) {
  900. int64_t pts = av_rescale(ist->last_dts, 1000000, AV_TIME_BASE);
  901. int64_t now = av_gettime() - ist->start;
  902. if (pts > now)
  903. av_usleep(pts - now);
  904. }
  905. }
  906. int guess_input_channel_layout(InputStream *ist)
  907. {
  908. AVCodecContext *dec = ist->st->codec;
  909. if (!dec->channel_layout) {
  910. char layout_name[256];
  911. dec->channel_layout = av_get_default_channel_layout(dec->channels);
  912. if (!dec->channel_layout)
  913. return 0;
  914. av_get_channel_layout_string(layout_name, sizeof(layout_name),
  915. dec->channels, dec->channel_layout);
  916. av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for Input Stream "
  917. "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
  918. }
  919. return 1;
  920. }
  921. static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
  922. {
  923. AVFrame *decoded_frame;
  924. AVCodecContext *avctx = ist->st->codec;
  925. int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
  926. int i, ret, resample_changed;
  927. if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
  928. return AVERROR(ENOMEM);
  929. else
  930. avcodec_get_frame_defaults(ist->decoded_frame);
  931. decoded_frame = ist->decoded_frame;
  932. ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt);
  933. if (!*got_output || ret < 0) {
  934. if (!pkt->size) {
  935. for (i = 0; i < ist->nb_filters; i++)
  936. av_buffersrc_buffer(ist->filters[i]->filter, NULL);
  937. }
  938. return ret;
  939. }
  940. /* if the decoder provides a pts, use it instead of the last packet pts.
  941. the decoder could be delaying output by a packet or more. */
  942. if (decoded_frame->pts != AV_NOPTS_VALUE)
  943. ist->next_dts = decoded_frame->pts;
  944. else if (pkt->pts != AV_NOPTS_VALUE) {
  945. decoded_frame->pts = pkt->pts;
  946. pkt->pts = AV_NOPTS_VALUE;
  947. }
  948. // preprocess audio (volume)
  949. if (audio_volume != 256) {
  950. int decoded_data_size = decoded_frame->nb_samples * avctx->channels * bps;
  951. void *samples = decoded_frame->data[0];
  952. switch (avctx->sample_fmt) {
  953. case AV_SAMPLE_FMT_U8:
  954. {
  955. uint8_t *volp = samples;
  956. for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
  957. int v = (((*volp - 128) * audio_volume + 128) >> 8) + 128;
  958. *volp++ = av_clip_uint8(v);
  959. }
  960. break;
  961. }
  962. case AV_SAMPLE_FMT_S16:
  963. {
  964. int16_t *volp = samples;
  965. for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
  966. int v = ((*volp) * audio_volume + 128) >> 8;
  967. *volp++ = av_clip_int16(v);
  968. }
  969. break;
  970. }
  971. case AV_SAMPLE_FMT_S32:
  972. {
  973. int32_t *volp = samples;
  974. for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
  975. int64_t v = (((int64_t)*volp * audio_volume + 128) >> 8);
  976. *volp++ = av_clipl_int32(v);
  977. }
  978. break;
  979. }
  980. case AV_SAMPLE_FMT_FLT:
  981. {
  982. float *volp = samples;
  983. float scale = audio_volume / 256.f;
  984. for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
  985. *volp++ *= scale;
  986. }
  987. break;
  988. }
  989. case AV_SAMPLE_FMT_DBL:
  990. {
  991. double *volp = samples;
  992. double scale = audio_volume / 256.;
  993. for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
  994. *volp++ *= scale;
  995. }
  996. break;
  997. }
  998. default:
  999. av_log(NULL, AV_LOG_FATAL,
  1000. "Audio volume adjustment on sample format %s is not supported.\n",
  1001. av_get_sample_fmt_name(ist->st->codec->sample_fmt));
  1002. exit(1);
  1003. }
  1004. }
  1005. rate_emu_sleep(ist);
  1006. resample_changed = ist->resample_sample_fmt != decoded_frame->format ||
  1007. ist->resample_channels != avctx->channels ||
  1008. ist->resample_channel_layout != decoded_frame->channel_layout ||
  1009. ist->resample_sample_rate != decoded_frame->sample_rate;
  1010. if (resample_changed) {
  1011. char layout1[64], layout2[64];
  1012. if (!guess_input_channel_layout(ist)) {
  1013. av_log(NULL, AV_LOG_FATAL, "Unable to find default channel "
  1014. "layout for Input Stream #%d.%d\n", ist->file_index,
  1015. ist->st->index);
  1016. exit(1);
  1017. }
  1018. decoded_frame->channel_layout = avctx->channel_layout;
  1019. av_get_channel_layout_string(layout1, sizeof(layout1), ist->resample_channels,
  1020. ist->resample_channel_layout);
  1021. av_get_channel_layout_string(layout2, sizeof(layout2), avctx->channels,
  1022. decoded_frame->channel_layout);
  1023. av_log(NULL, AV_LOG_INFO,
  1024. "Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d chl:%s to rate:%d fmt:%s ch:%d chl:%s\n",
  1025. ist->file_index, ist->st->index,
  1026. ist->resample_sample_rate, av_get_sample_fmt_name(ist->resample_sample_fmt),
  1027. ist->resample_channels, layout1,
  1028. decoded_frame->sample_rate, av_get_sample_fmt_name(decoded_frame->format),
  1029. avctx->channels, layout2);
  1030. ist->resample_sample_fmt = decoded_frame->format;
  1031. ist->resample_sample_rate = decoded_frame->sample_rate;
  1032. ist->resample_channel_layout = decoded_frame->channel_layout;
  1033. ist->resample_channels = avctx->channels;
  1034. for (i = 0; i < nb_filtergraphs; i++)
  1035. if (ist_in_filtergraph(filtergraphs[i], ist) &&
  1036. configure_filtergraph(filtergraphs[i]) < 0) {
  1037. av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
  1038. exit(1);
  1039. }
  1040. }
  1041. if (decoded_frame->pts != AV_NOPTS_VALUE)
  1042. decoded_frame->pts = av_rescale_q(decoded_frame->pts,
  1043. ist->st->time_base,
  1044. (AVRational){1, ist->st->codec->sample_rate});
  1045. for (i = 0; i < ist->nb_filters; i++)
  1046. av_buffersrc_write_frame(ist->filters[i]->filter, decoded_frame);
  1047. return ret;
  1048. }
  1049. static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
  1050. {
  1051. AVFrame *decoded_frame;
  1052. void *buffer_to_free = NULL;
  1053. int i, ret = 0, resample_changed;
  1054. if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
  1055. return AVERROR(ENOMEM);
  1056. else
  1057. avcodec_get_frame_defaults(ist->decoded_frame);
  1058. decoded_frame = ist->decoded_frame;
  1059. ret = avcodec_decode_video2(ist->st->codec,
  1060. decoded_frame, got_output, pkt);
  1061. if (!*got_output || ret < 0) {
  1062. if (!pkt->size) {
  1063. for (i = 0; i < ist->nb_filters; i++)
  1064. av_buffersrc_buffer(ist->filters[i]->filter, NULL);
  1065. }
  1066. return ret;
  1067. }
  1068. decoded_frame->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts,
  1069. decoded_frame->pkt_dts);
  1070. pkt->size = 0;
  1071. pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
  1072. rate_emu_sleep(ist);
  1073. if (ist->st->sample_aspect_ratio.num)
  1074. decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
  1075. resample_changed = ist->resample_width != decoded_frame->width ||
  1076. ist->resample_height != decoded_frame->height ||
  1077. ist->resample_pix_fmt != decoded_frame->format;
  1078. if (resample_changed) {
  1079. av_log(NULL, AV_LOG_INFO,
  1080. "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
  1081. ist->file_index, ist->st->index,
  1082. ist->resample_width, ist->resample_height, av_get_pix_fmt_name(ist->resample_pix_fmt),
  1083. decoded_frame->width, decoded_frame->height, av_get_pix_fmt_name(decoded_frame->format));
  1084. ret = poll_filters();
  1085. if (ret < 0 && (ret != AVERROR_EOF && ret != AVERROR(EAGAIN)))
  1086. av_log(NULL, AV_LOG_ERROR, "Error while filtering.\n");
  1087. ist->resample_width = decoded_frame->width;
  1088. ist->resample_height = decoded_frame->height;
  1089. ist->resample_pix_fmt = decoded_frame->format;
  1090. for (i = 0; i < nb_filtergraphs; i++)
  1091. if (ist_in_filtergraph(filtergraphs[i], ist) &&
  1092. configure_filtergraph(filtergraphs[i]) < 0) {
  1093. av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
  1094. exit(1);
  1095. }
  1096. }
  1097. for (i = 0; i < ist->nb_filters; i++) {
  1098. if (ist->st->codec->codec->capabilities & CODEC_CAP_DR1) {
  1099. FrameBuffer *buf = decoded_frame->opaque;
  1100. AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
  1101. decoded_frame->data, decoded_frame->linesize,
  1102. AV_PERM_READ | AV_PERM_PRESERVE,
  1103. ist->st->codec->width, ist->st->codec->height,
  1104. ist->st->codec->pix_fmt);
  1105. avfilter_copy_frame_props(fb, decoded_frame);
  1106. fb->buf->priv = buf;
  1107. fb->buf->free = filter_release_buffer;
  1108. buf->refcount++;
  1109. av_buffersrc_buffer(ist->filters[i]->filter, fb);
  1110. } else
  1111. av_buffersrc_write_frame(ist->filters[i]->filter, decoded_frame);
  1112. }
  1113. av_free(buffer_to_free);
  1114. return ret;
  1115. }
  1116. static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
  1117. {
  1118. AVSubtitle subtitle;
  1119. int i, ret = avcodec_decode_subtitle2(ist->st->codec,
  1120. &subtitle, got_output, pkt);
  1121. if (ret < 0)
  1122. return ret;
  1123. if (!*got_output)
  1124. return ret;
  1125. rate_emu_sleep(ist);
  1126. for (i = 0; i < nb_output_streams; i++) {
  1127. OutputStream *ost = output_streams[i];
  1128. if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
  1129. continue;
  1130. do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle, pkt->pts);
  1131. }
  1132. avsubtitle_free(&subtitle);
  1133. return ret;
  1134. }
  1135. /* pkt = NULL means EOF (needed to flush decoder buffers) */
  1136. static int output_packet(InputStream *ist, const AVPacket *pkt)
  1137. {
  1138. int i;
  1139. int got_output;
  1140. AVPacket avpkt;
  1141. if (ist->next_dts == AV_NOPTS_VALUE)
  1142. ist->next_dts = ist->last_dts;
  1143. if (pkt == NULL) {
  1144. /* EOF handling */
  1145. av_init_packet(&avpkt);
  1146. avpkt.data = NULL;
  1147. avpkt.size = 0;
  1148. goto handle_eof;
  1149. } else {
  1150. avpkt = *pkt;
  1151. }
  1152. if (pkt->dts != AV_NOPTS_VALUE)
  1153. ist->next_dts = ist->last_dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
  1154. // while we have more to decode or while the decoder did output something on EOF
  1155. while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
  1156. int ret = 0;
  1157. handle_eof:
  1158. ist->last_dts = ist->next_dts;
  1159. if (avpkt.size && avpkt.size != pkt->size) {
  1160. av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
  1161. "Multiple frames in a packet from stream %d\n", pkt->stream_index);
  1162. ist->showed_multi_packet_warning = 1;
  1163. }
  1164. switch (ist->st->codec->codec_type) {
  1165. case AVMEDIA_TYPE_AUDIO:
  1166. ret = decode_audio (ist, &avpkt, &got_output);
  1167. break;
  1168. case AVMEDIA_TYPE_VIDEO:
  1169. ret = decode_video (ist, &avpkt, &got_output);
  1170. if (avpkt.duration)
  1171. ist->next_dts += av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q);
  1172. else if (ist->st->avg_frame_rate.num)
  1173. ist->next_dts += av_rescale_q(1, av_inv_q(ist->st->avg_frame_rate),
  1174. AV_TIME_BASE_Q);
  1175. else if (ist->st->codec->time_base.num != 0) {
  1176. int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 :
  1177. ist->st->codec->ticks_per_frame;
  1178. ist->next_dts += av_rescale_q(ticks, ist->st->codec->time_base, AV_TIME_BASE_Q);
  1179. }
  1180. break;
  1181. case AVMEDIA_TYPE_SUBTITLE:
  1182. ret = transcode_subtitles(ist, &avpkt, &got_output);
  1183. break;
  1184. default:
  1185. return -1;
  1186. }
  1187. if (ret < 0)
  1188. return ret;
  1189. // touch data and size only if not EOF
  1190. if (pkt) {
  1191. avpkt.data += ret;
  1192. avpkt.size -= ret;
  1193. }
  1194. if (!got_output) {
  1195. continue;
  1196. }
  1197. }
  1198. /* handle stream copy */
  1199. if (!ist->decoding_needed) {
  1200. rate_emu_sleep(ist);
  1201. ist->last_dts = ist->next_dts;
  1202. switch (ist->st->codec->codec_type) {
  1203. case AVMEDIA_TYPE_AUDIO:
  1204. ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
  1205. ist->st->codec->sample_rate;
  1206. break;
  1207. case AVMEDIA_TYPE_VIDEO:
  1208. if (ist->st->codec->time_base.num != 0) {
  1209. int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;
  1210. ist->next_dts += ((int64_t)AV_TIME_BASE *
  1211. ist->st->codec->time_base.num * ticks) /
  1212. ist->st->codec->time_base.den;
  1213. }
  1214. break;
  1215. }
  1216. }
  1217. for (i = 0; pkt && i < nb_output_streams; i++) {
  1218. OutputStream *ost = output_streams[i];
  1219. if (!check_output_constraints(ist, ost) || ost->encoding_needed)
  1220. continue;
  1221. do_streamcopy(ist, ost, pkt);
  1222. }
  1223. return 0;
  1224. }
  1225. static void print_sdp(void)
  1226. {
  1227. char sdp[2048];
  1228. int i;
  1229. AVFormatContext **avc = av_malloc(sizeof(*avc) * nb_output_files);
  1230. if (!avc)
  1231. exit(1);
  1232. for (i = 0; i < nb_output_files; i++)
  1233. avc[i] = output_files[i]->ctx;
  1234. av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));
  1235. printf("SDP:\n%s\n", sdp);
  1236. fflush(stdout);
  1237. av_freep(&avc);
  1238. }
  1239. static int init_input_stream(int ist_index, char *error, int error_len)
  1240. {
  1241. int i;
  1242. InputStream *ist = input_streams[ist_index];
  1243. if (ist->decoding_needed) {
  1244. AVCodec *codec = ist->dec;
  1245. if (!codec) {
  1246. snprintf(error, error_len, "Decoder (codec id %d) not found for input stream #%d:%d",
  1247. ist->st->codec->codec_id, ist->file_index, ist->st->index);
  1248. return AVERROR(EINVAL);
  1249. }
  1250. /* update requested sample format for the decoder based on the
  1251. corresponding encoder sample format */
  1252. for (i = 0; i < nb_output_streams; i++) {
  1253. OutputStream *ost = output_streams[i];
  1254. if (ost->source_index == ist_index) {
  1255. update_sample_fmt(ist->st->codec, codec, ost->st->codec);
  1256. break;
  1257. }
  1258. }
  1259. if (codec->type == AVMEDIA_TYPE_VIDEO && codec->capabilities & CODEC_CAP_DR1) {
  1260. ist->st->codec->get_buffer = codec_get_buffer;
  1261. ist->st->codec->release_buffer = codec_release_buffer;
  1262. ist->st->codec->opaque = &ist->buffer_pool;
  1263. }
  1264. if (!av_dict_get(ist->opts, "threads", NULL, 0))
  1265. av_dict_set(&ist->opts, "threads", "auto", 0);
  1266. if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
  1267. snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
  1268. ist->file_index, ist->st->index);
  1269. return AVERROR(EINVAL);
  1270. }
  1271. assert_codec_experimental(ist->st->codec, 0);
  1272. assert_avoptions(ist->opts);
  1273. }
  1274. ist->last_dts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
  1275. ist->next_dts = AV_NOPTS_VALUE;
  1276. init_pts_correction(&ist->pts_ctx);
  1277. ist->is_start = 1;
  1278. return 0;
  1279. }
  1280. static InputStream *get_input_stream(OutputStream *ost)
  1281. {
  1282. if (ost->source_index >= 0)
  1283. return input_streams[ost->source_index];
  1284. if (ost->filter) {
  1285. FilterGraph *fg = ost->filter->graph;
  1286. int i;
  1287. for (i = 0; i < fg->nb_inputs; i++)
  1288. if (fg->inputs[i]->ist->st->codec->codec_type == ost->st->codec->codec_type)
  1289. return fg->inputs[i]->ist;
  1290. }
  1291. return NULL;
  1292. }
  1293. static void parse_forced_key_frames(char *kf, OutputStream *ost,
  1294. AVCodecContext *avctx)
  1295. {
  1296. char *p;
  1297. int n = 1, i;
  1298. int64_t t;
  1299. for (p = kf; *p; p++)
  1300. if (*p == ',')
  1301. n++;
  1302. ost->forced_kf_count = n;
  1303. ost->forced_kf_pts = av_malloc(sizeof(*ost->forced_kf_pts) * n);
  1304. if (!ost->forced_kf_pts) {
  1305. av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
  1306. exit(1);
  1307. }
  1308. p = kf;
  1309. for (i = 0; i < n; i++) {
  1310. char *next = strchr(p, ',');
  1311. if (next)
  1312. *next++ = 0;
  1313. t = parse_time_or_die("force_key_frames", p, 1);
  1314. ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
  1315. p = next;
  1316. }
  1317. }
  1318. static int transcode_init(void)
  1319. {
  1320. int ret = 0, i, j, k;
  1321. AVFormatContext *oc;
  1322. AVCodecContext *codec;
  1323. OutputStream *ost;
  1324. InputStream *ist;
  1325. char error[1024];
  1326. int want_sdp = 1;
  1327. /* init framerate emulation */
  1328. for (i = 0; i < nb_input_files; i++) {
  1329. InputFile *ifile = input_files[i];
  1330. if (ifile->rate_emu)
  1331. for (j = 0; j < ifile->nb_streams; j++)
  1332. input_streams[j + ifile->ist_index]->start = av_gettime();
  1333. }
  1334. /* output stream init */
  1335. for (i = 0; i < nb_output_files; i++) {
  1336. oc = output_files[i]->ctx;
  1337. if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
  1338. av_dump_format(oc, i, oc->filename, 1);
  1339. av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i);
  1340. return AVERROR(EINVAL);
  1341. }
  1342. }
  1343. /* init complex filtergraphs */
  1344. for (i = 0; i < nb_filtergraphs; i++)
  1345. if ((ret = avfilter_graph_config(filtergraphs[i]->graph, NULL)) < 0)
  1346. return ret;
  1347. /* for each output stream, we compute the right encoding parameters */
  1348. for (i = 0; i < nb_output_streams; i++) {
  1349. AVCodecContext *icodec = NULL;
  1350. ost = output_streams[i];
  1351. oc = output_files[ost->file_index]->ctx;
  1352. ist = get_input_stream(ost);
  1353. if (ost->attachment_filename)
  1354. continue;
  1355. codec = ost->st->codec;
  1356. if (ist) {
  1357. icodec = ist->st->codec;
  1358. ost->st->disposition = ist->st->disposition;
  1359. codec->bits_per_raw_sample = icodec->bits_per_raw_sample;
  1360. codec->chroma_sample_location = icodec->chroma_sample_location;
  1361. }
  1362. if (ost->stream_copy) {
  1363. uint64_t extra_size;
  1364. av_assert0(ist && !ost->filter);
  1365. extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
  1366. if (extra_size > INT_MAX) {
  1367. return AVERROR(EINVAL);
  1368. }
  1369. /* if stream_copy is selected, no need to decode or encode */
  1370. codec->codec_id = icodec->codec_id;
  1371. codec->codec_type = icodec->codec_type;
  1372. if (!codec->codec_tag) {
  1373. if (!oc->oformat->codec_tag ||
  1374. av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
  1375. av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)
  1376. codec->codec_tag = icodec->codec_tag;
  1377. }
  1378. codec->bit_rate = icodec->bit_rate;
  1379. codec->rc_max_rate = icodec->rc_max_rate;
  1380. codec->rc_buffer_size = icodec->rc_buffer_size;
  1381. codec->field_order = icodec->field_order;
  1382. codec->extradata = av_mallocz(extra_size);
  1383. if (!codec->extradata) {
  1384. return AVERROR(ENOMEM);
  1385. }
  1386. memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
  1387. codec->extradata_size = icodec->extradata_size;
  1388. if (!copy_tb) {
  1389. codec->time_base = icodec->time_base;
  1390. codec->time_base.num *= icodec->ticks_per_frame;
  1391. av_reduce(&codec->time_base.num, &codec->time_base.den,
  1392. codec->time_base.num, codec->time_base.den, INT_MAX);
  1393. } else
  1394. codec->time_base = ist->st->time_base;
  1395. switch (codec->codec_type) {
  1396. case AVMEDIA_TYPE_AUDIO:
  1397. if (audio_volume != 256) {
  1398. av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
  1399. exit(1);
  1400. }
  1401. codec->channel_layout = icodec->channel_layout;
  1402. codec->sample_rate = icodec->sample_rate;
  1403. codec->channels = icodec->channels;
  1404. codec->frame_size = icodec->frame_size;
  1405. codec->audio_service_type = icodec->audio_service_type;
  1406. codec->block_align = icodec->block_align;
  1407. break;
  1408. case AVMEDIA_TYPE_VIDEO:
  1409. codec->pix_fmt = icodec->pix_fmt;
  1410. codec->width = icodec->width;
  1411. codec->height = icodec->height;
  1412. codec->has_b_frames = icodec->has_b_frames;
  1413. if (!codec->sample_aspect_ratio.num) {
  1414. codec->sample_aspect_ratio =
  1415. ost->st->sample_aspect_ratio =
  1416. ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
  1417. ist->st->codec->sample_aspect_ratio.num ?
  1418. ist->st->codec->sample_aspect_ratio : (AVRational){0, 1};
  1419. }
  1420. break;
  1421. case AVMEDIA_TYPE_SUBTITLE:
  1422. codec->width = icodec->width;
  1423. codec->height = icodec->height;
  1424. break;
  1425. case AVMEDIA_TYPE_DATA:
  1426. case AVMEDIA_TYPE_ATTACHMENT:
  1427. break;
  1428. default:
  1429. abort();
  1430. }
  1431. } else {
  1432. if (!ost->enc) {
  1433. /* should only happen when a default codec is not present. */
  1434. snprintf(error, sizeof(error), "Automatic encoder selection "
  1435. "failed for output stream #%d:%d. Default encoder for "
  1436. "format %s is probably disabled. Please choose an "
  1437. "encoder manually.\n", ost->file_index, ost->index,
  1438. oc->oformat->name);
  1439. ret = AVERROR(EINVAL);
  1440. goto dump_format;
  1441. }
  1442. if (ist)
  1443. ist->decoding_needed = 1;
  1444. ost->encoding_needed = 1;
  1445. /*
  1446. * We want CFR output if and only if one of those is true:
  1447. * 1) user specified output framerate with -r
  1448. * 2) user specified -vsync cfr
  1449. * 3) output format is CFR and the user didn't force vsync to
  1450. * something else than CFR
  1451. *
  1452. * in such a case, set ost->frame_rate
  1453. */
  1454. if (codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  1455. !ost->frame_rate.num && ist &&
  1456. (video_sync_method == VSYNC_CFR ||
  1457. (video_sync_method == VSYNC_AUTO &&
  1458. !(oc->oformat->flags & (AVFMT_NOTIMESTAMPS | AVFMT_VARIABLE_FPS))))) {
  1459. ost->frame_rate = ist->framerate.num ? ist->framerate :
  1460. ist->st->avg_frame_rate.num ?
  1461. ist->st->avg_frame_rate :
  1462. (AVRational){25, 1};
  1463. if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
  1464. int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
  1465. ost->frame_rate = ost->enc->supported_framerates[idx];
  1466. }
  1467. }
  1468. if (!ost->filter &&
  1469. (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  1470. codec->codec_type == AVMEDIA_TYPE_AUDIO)) {
  1471. FilterGraph *fg;
  1472. fg = init_simple_filtergraph(ist, ost);
  1473. if (configure_filtergraph(fg)) {
  1474. av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
  1475. exit(1);
  1476. }
  1477. }
  1478. switch (codec->codec_type) {
  1479. case AVMEDIA_TYPE_AUDIO:
  1480. codec->sample_fmt = ost->filter->filter->inputs[0]->format;
  1481. codec->sample_rate = ost->filter->filter->inputs[0]->sample_rate;
  1482. codec->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
  1483. codec->channels = av_get_channel_layout_nb_channels(codec->channel_layout);
  1484. codec->time_base = (AVRational){ 1, codec->sample_rate };
  1485. break;
  1486. case AVMEDIA_TYPE_VIDEO:
  1487. codec->time_base = ost->filter->filter->inputs[0]->time_base;
  1488. codec->width = ost->filter->filter->inputs[0]->w;
  1489. codec->height = ost->filter->filter->inputs[0]->h;
  1490. codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
  1491. ost->frame_aspect_ratio ? // overridden by the -aspect cli option
  1492. av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :
  1493. ost->filter->filter->inputs[0]->sample_aspect_ratio;
  1494. codec->pix_fmt = ost->filter->filter->inputs[0]->format;
  1495. if (icodec &&
  1496. (codec->width != icodec->width ||
  1497. codec->height != icodec->height ||
  1498. codec->pix_fmt != icodec->pix_fmt)) {
  1499. codec->bits_per_raw_sample = 0;
  1500. }
  1501. if (ost->forced_keyframes)
  1502. parse_forced_key_frames(ost->forced_keyframes, ost,
  1503. ost->st->codec);
  1504. break;
  1505. case AVMEDIA_TYPE_SUBTITLE:
  1506. codec->time_base = (AVRational){1, 1000};
  1507. break;
  1508. default:
  1509. abort();
  1510. break;
  1511. }
  1512. /* two pass mode */
  1513. if ((codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
  1514. char logfilename[1024];
  1515. FILE *f;
  1516. snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
  1517. ost->logfile_prefix ? ost->logfile_prefix :
  1518. DEFAULT_PASS_LOGFILENAME_PREFIX,
  1519. i);
  1520. if (!strcmp(ost->enc->name, "libx264")) {
  1521. av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
  1522. } else {
  1523. if (codec->flags & CODEC_FLAG_PASS1) {
  1524. f = fopen(logfilename, "wb");
  1525. if (!f) {
  1526. av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
  1527. logfilename, strerror(errno));
  1528. exit(1);
  1529. }
  1530. ost->logfile = f;
  1531. } else {
  1532. char *logbuffer;
  1533. size_t logbuffer_size;
  1534. if (cmdutils_read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
  1535. av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
  1536. logfilename);
  1537. exit(1);
  1538. }
  1539. codec->stats_in = logbuffer;
  1540. }
  1541. }
  1542. }
  1543. }
  1544. }
  1545. /* open each encoder */
  1546. for (i = 0; i < nb_output_streams; i++) {
  1547. ost = output_streams[i];
  1548. if (ost->encoding_needed) {
  1549. AVCodec *codec = ost->enc;
  1550. AVCodecContext *dec = NULL;
  1551. if ((ist = get_input_stream(ost)))
  1552. dec = ist->st->codec;
  1553. if (dec && dec->subtitle_header) {
  1554. ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
  1555. if (!ost->st->codec->subtitle_header) {
  1556. ret = AVERROR(ENOMEM);
  1557. goto dump_format;
  1558. }
  1559. memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
  1560. ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
  1561. }
  1562. if (!av_dict_get(ost->opts, "threads", NULL, 0))
  1563. av_dict_set(&ost->opts, "threads", "auto", 0);
  1564. if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {
  1565. snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
  1566. ost->file_index, ost->index);
  1567. ret = AVERROR(EINVAL);
  1568. goto dump_format;
  1569. }
  1570. assert_codec_experimental(ost->st->codec, 1);
  1571. assert_avoptions(ost->opts);
  1572. if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
  1573. av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
  1574. "It takes bits/s as argument, not kbits/s\n");
  1575. extra_size += ost->st->codec->extradata_size;
  1576. if (ost->st->codec->me_threshold)
  1577. input_streams[ost->source_index]->st->codec->debug |= FF_DEBUG_MV;
  1578. }
  1579. }
  1580. /* init input streams */
  1581. for (i = 0; i < nb_input_streams; i++)
  1582. if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
  1583. goto dump_format;
  1584. /* discard unused programs */
  1585. for (i = 0; i < nb_input_files; i++) {
  1586. InputFile *ifile = input_files[i];
  1587. for (j = 0; j < ifile->ctx->nb_programs; j++) {
  1588. AVProgram *p = ifile->ctx->programs[j];
  1589. int discard = AVDISCARD_ALL;
  1590. for (k = 0; k < p->nb_stream_indexes; k++)
  1591. if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) {
  1592. discard = AVDISCARD_DEFAULT;
  1593. break;
  1594. }
  1595. p->discard = discard;
  1596. }
  1597. }
  1598. /* open files and write file headers */
  1599. for (i = 0; i < nb_output_files; i++) {
  1600. oc = output_files[i]->ctx;
  1601. oc->interrupt_callback = int_cb;
  1602. if ((ret = avformat_write_header(oc, &output_files[i]->opts)) < 0) {
  1603. char errbuf[128];
  1604. const char *errbuf_ptr = errbuf;
  1605. if (av_strerror(ret, errbuf, sizeof(errbuf)) < 0)
  1606. errbuf_ptr = strerror(AVUNERROR(ret));
  1607. snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?): %s", i, errbuf_ptr);
  1608. ret = AVERROR(EINVAL);
  1609. goto dump_format;
  1610. }
  1611. assert_avoptions(output_files[i]->opts);
  1612. if (strcmp(oc->oformat->name, "rtp")) {
  1613. want_sdp = 0;
  1614. }
  1615. }
  1616. dump_format:
  1617. /* dump the file output parameters - cannot be done before in case
  1618. of stream copy */
  1619. for (i = 0; i < nb_output_files; i++) {
  1620. av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
  1621. }
  1622. /* dump the stream mapping */
  1623. av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
  1624. for (i = 0; i < nb_input_streams; i++) {
  1625. ist = input_streams[i];
  1626. for (j = 0; j < ist->nb_filters; j++) {
  1627. if (ist->filters[j]->graph->graph_desc) {
  1628. av_log(NULL, AV_LOG_INFO, " Stream #%d:%d (%s) -> %s",
  1629. ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
  1630. ist->filters[j]->name);
  1631. if (nb_filtergraphs > 1)
  1632. av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
  1633. av_log(NULL, AV_LOG_INFO, "\n");
  1634. }
  1635. }
  1636. }
  1637. for (i = 0; i < nb_output_streams; i++) {
  1638. ost = output_streams[i];
  1639. if (ost->attachment_filename) {
  1640. /* an attached file */
  1641. av_log(NULL, AV_LOG_INFO, " File %s -> Stream #%d:%d\n",
  1642. ost->attachment_filename, ost->file_index, ost->index);
  1643. continue;
  1644. }
  1645. if (ost->filter && ost->filter->graph->graph_desc) {
  1646. /* output from a complex graph */
  1647. av_log(NULL, AV_LOG_INFO, " %s", ost->filter->name);
  1648. if (nb_filtergraphs > 1)
  1649. av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
  1650. av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
  1651. ost->index, ost->enc ? ost->enc->name : "?");
  1652. continue;
  1653. }
  1654. av_log(NULL, AV_LOG_INFO, " Stream #%d:%d -> #%d:%d",
  1655. input_streams[ost->source_index]->file_index,
  1656. input_streams[ost->source_index]->st->index,
  1657. ost->file_index,
  1658. ost->index);
  1659. if (ost->sync_ist != input_streams[ost->source_index])
  1660. av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
  1661. ost->sync_ist->file_index,
  1662. ost->sync_ist->st->index);
  1663. if (ost->stream_copy)
  1664. av_log(NULL, AV_LOG_INFO, " (copy)");
  1665. else
  1666. av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index]->dec ?
  1667. input_streams[ost->source_index]->dec->name : "?",
  1668. ost->enc ? ost->enc->name : "?");
  1669. av_log(NULL, AV_LOG_INFO, "\n");
  1670. }
  1671. if (ret) {
  1672. av_log(NULL, AV_LOG_ERROR, "%s\n", error);
  1673. return ret;
  1674. }
  1675. if (want_sdp) {
  1676. print_sdp();
  1677. }
  1678. return 0;
  1679. }
  1680. /* Return 1 if there remain streams where more output is wanted, 0 otherwise. */
  1681. static int need_output(void)
  1682. {
  1683. int i;
  1684. for (i = 0; i < nb_output_streams; i++) {
  1685. OutputStream *ost = output_streams[i];
  1686. OutputFile *of = output_files[ost->file_index];
  1687. AVFormatContext *os = output_files[ost->file_index]->ctx;
  1688. if (ost->finished ||
  1689. (os->pb && avio_tell(os->pb) >= of->limit_filesize))
  1690. continue;
  1691. if (ost->frame_number >= ost->max_frames) {
  1692. int j;
  1693. for (j = 0; j < of->ctx->nb_streams; j++)
  1694. output_streams[of->ost_index + j]->finished = 1;
  1695. continue;
  1696. }
  1697. return 1;
  1698. }
  1699. return 0;
  1700. }
  1701. static InputFile *select_input_file(void)
  1702. {
  1703. InputFile *ifile = NULL;
  1704. int64_t ipts_min = INT64_MAX;
  1705. int i;
  1706. for (i = 0; i < nb_input_streams; i++) {
  1707. InputStream *ist = input_streams[i];
  1708. int64_t ipts = ist->last_dts;
  1709. if (ist->discard || input_files[ist->file_index]->eagain)
  1710. continue;
  1711. if (!input_files[ist->file_index]->eof_reached) {
  1712. if (ipts < ipts_min) {
  1713. ipts_min = ipts;
  1714. ifile = input_files[ist->file_index];
  1715. }
  1716. }
  1717. }
  1718. return ifile;
  1719. }
  1720. #if HAVE_PTHREADS
  1721. static void *input_thread(void *arg)
  1722. {
  1723. InputFile *f = arg;
  1724. int ret = 0;
  1725. while (!transcoding_finished && ret >= 0) {
  1726. AVPacket pkt;
  1727. ret = av_read_frame(f->ctx, &pkt);
  1728. if (ret == AVERROR(EAGAIN)) {
  1729. av_usleep(10000);
  1730. ret = 0;
  1731. continue;
  1732. } else if (ret < 0)
  1733. break;
  1734. pthread_mutex_lock(&f->fifo_lock);
  1735. while (!av_fifo_space(f->fifo))
  1736. pthread_cond_wait(&f->fifo_cond, &f->fifo_lock);
  1737. av_dup_packet(&pkt);
  1738. av_fifo_generic_write(f->fifo, &pkt, sizeof(pkt), NULL);
  1739. pthread_mutex_unlock(&f->fifo_lock);
  1740. }
  1741. f->finished = 1;
  1742. return NULL;
  1743. }
  1744. static void free_input_threads(void)
  1745. {
  1746. int i;
  1747. if (nb_input_files == 1)
  1748. return;
  1749. transcoding_finished = 1;
  1750. for (i = 0; i < nb_input_files; i++) {
  1751. InputFile *f = input_files[i];
  1752. AVPacket pkt;
  1753. if (!f->fifo || f->joined)
  1754. continue;
  1755. pthread_mutex_lock(&f->fifo_lock);
  1756. while (av_fifo_size(f->fifo)) {
  1757. av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
  1758. av_free_packet(&pkt);
  1759. }
  1760. pthread_cond_signal(&f->fifo_cond);
  1761. pthread_mutex_unlock(&f->fifo_lock);
  1762. pthread_join(f->thread, NULL);
  1763. f->joined = 1;
  1764. while (av_fifo_size(f->fifo)) {
  1765. av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
  1766. av_free_packet(&pkt);
  1767. }
  1768. av_fifo_free(f->fifo);
  1769. }
  1770. }
  1771. static int init_input_threads(void)
  1772. {
  1773. int i, ret;
  1774. if (nb_input_files == 1)
  1775. return 0;
  1776. for (i = 0; i < nb_input_files; i++) {
  1777. InputFile *f = input_files[i];
  1778. if (!(f->fifo = av_fifo_alloc(8*sizeof(AVPacket))))
  1779. return AVERROR(ENOMEM);
  1780. pthread_mutex_init(&f->fifo_lock, NULL);
  1781. pthread_cond_init (&f->fifo_cond, NULL);
  1782. if ((ret = pthread_create(&f->thread, NULL, input_thread, f)))
  1783. return AVERROR(ret);
  1784. }
  1785. return 0;
  1786. }
  1787. static int get_input_packet_mt(InputFile *f, AVPacket *pkt)
  1788. {
  1789. int ret = 0;
  1790. pthread_mutex_lock(&f->fifo_lock);
  1791. if (av_fifo_size(f->fifo)) {
  1792. av_fifo_generic_read(f->fifo, pkt, sizeof(*pkt), NULL);
  1793. pthread_cond_signal(&f->fifo_cond);
  1794. } else {
  1795. if (f->finished)
  1796. ret = AVERROR_EOF;
  1797. else
  1798. ret = AVERROR(EAGAIN);
  1799. }
  1800. pthread_mutex_unlock(&f->fifo_lock);
  1801. return ret;
  1802. }
  1803. #endif
  1804. static int get_input_packet(InputFile *f, AVPacket *pkt)
  1805. {
  1806. #if HAVE_PTHREADS
  1807. if (nb_input_files > 1)
  1808. return get_input_packet_mt(f, pkt);
  1809. #endif
  1810. return av_read_frame(f->ctx, pkt);
  1811. }
  1812. static int got_eagain(void)
  1813. {
  1814. int i;
  1815. for (i = 0; i < nb_input_files; i++)
  1816. if (input_files[i]->eagain)
  1817. return 1;
  1818. return 0;
  1819. }
  1820. static void reset_eagain(void)
  1821. {
  1822. int i;
  1823. for (i = 0; i < nb_input_files; i++)
  1824. input_files[i]->eagain = 0;
  1825. }
  1826. /*
  1827. * Read one packet from an input file and send it for
  1828. * - decoding -> lavfi (audio/video)
  1829. * - decoding -> encoding -> muxing (subtitles)
  1830. * - muxing (streamcopy)
  1831. *
  1832. * Return
  1833. * - 0 -- one packet was read and processed
  1834. * - AVERROR(EAGAIN) -- no packets were available for selected file,
  1835. * this function should be called again
  1836. * - AVERROR_EOF -- this function should not be called again
  1837. */
  1838. static int process_input(void)
  1839. {
  1840. InputFile *ifile;
  1841. AVFormatContext *is;
  1842. InputStream *ist;
  1843. AVPacket pkt;
  1844. int ret, i, j;
  1845. /* select the stream that we must read now */
  1846. ifile = select_input_file();
  1847. /* if none, if is finished */
  1848. if (!ifile) {
  1849. if (got_eagain()) {
  1850. reset_eagain();
  1851. av_usleep(10000);
  1852. return AVERROR(EAGAIN);
  1853. }
  1854. av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from.\n");
  1855. return AVERROR_EOF;
  1856. }
  1857. is = ifile->ctx;
  1858. ret = get_input_packet(ifile, &pkt);
  1859. if (ret == AVERROR(EAGAIN)) {
  1860. ifile->eagain = 1;
  1861. return ret;
  1862. }
  1863. if (ret < 0) {
  1864. if (ret != AVERROR_EOF) {
  1865. print_error(is->filename, ret);
  1866. if (exit_on_error)
  1867. exit(1);
  1868. }
  1869. ifile->eof_reached = 1;
  1870. for (i = 0; i < ifile->nb_streams; i++) {
  1871. ist = input_streams[ifile->ist_index + i];
  1872. if (ist->decoding_needed)
  1873. output_packet(ist, NULL);
  1874. /* mark all outputs that don't go through lavfi as finished */
  1875. for (j = 0; j < nb_output_streams; j++) {
  1876. OutputStream *ost = output_streams[j];
  1877. if (ost->source_index == ifile->ist_index + i &&
  1878. (ost->stream_copy || ost->enc->type == AVMEDIA_TYPE_SUBTITLE))
  1879. ost->finished= 1;
  1880. }
  1881. }
  1882. return AVERROR(EAGAIN);
  1883. }
  1884. reset_eagain();
  1885. if (do_pkt_dump) {
  1886. av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
  1887. is->streams[pkt.stream_index]);
  1888. }
  1889. /* the following test is needed in case new streams appear
  1890. dynamically in stream : we ignore them */
  1891. if (pkt.stream_index >= ifile->nb_streams)
  1892. goto discard_packet;
  1893. ist = input_streams[ifile->ist_index + pkt.stream_index];
  1894. if (ist->discard)
  1895. goto discard_packet;
  1896. if (pkt.dts != AV_NOPTS_VALUE)
  1897. pkt.dts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
  1898. if (pkt.pts != AV_NOPTS_VALUE)
  1899. pkt.pts += av_rescale_q(ifile->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
  1900. if (pkt.pts != AV_NOPTS_VALUE)
  1901. pkt.pts *= ist->ts_scale;
  1902. if (pkt.dts != AV_NOPTS_VALUE)
  1903. pkt.dts *= ist->ts_scale;
  1904. if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE &&
  1905. (is->iformat->flags & AVFMT_TS_DISCONT)) {
  1906. int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
  1907. int64_t delta = pkt_dts - ist->next_dts;
  1908. if ((FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE || pkt_dts + 1 < ist->last_dts) && !copy_ts) {
  1909. ifile->ts_offset -= delta;
  1910. av_log(NULL, AV_LOG_DEBUG,
  1911. "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
  1912. delta, ifile->ts_offset);
  1913. pkt.dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
  1914. if (pkt.pts != AV_NOPTS_VALUE)
  1915. pkt.pts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
  1916. }
  1917. }
  1918. ret = output_packet(ist, &pkt);
  1919. if (ret < 0) {
  1920. av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
  1921. ist->file_index, ist->st->index);
  1922. if (exit_on_error)
  1923. exit(1);
  1924. }
  1925. discard_packet:
  1926. av_free_packet(&pkt);
  1927. return 0;
  1928. }
  1929. /*
  1930. * The following code is the main loop of the file converter
  1931. */
  1932. static int transcode(void)
  1933. {
  1934. int ret, i, need_input = 1;
  1935. AVFormatContext *os;
  1936. OutputStream *ost;
  1937. InputStream *ist;
  1938. int64_t timer_start;
  1939. ret = transcode_init();
  1940. if (ret < 0)
  1941. goto fail;
  1942. av_log(NULL, AV_LOG_INFO, "Press ctrl-c to stop encoding\n");
  1943. term_init();
  1944. timer_start = av_gettime();
  1945. #if HAVE_PTHREADS
  1946. if ((ret = init_input_threads()) < 0)
  1947. goto fail;
  1948. #endif
  1949. while (!received_sigterm) {
  1950. /* check if there's any stream where output is still needed */
  1951. if (!need_output()) {
  1952. av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
  1953. break;
  1954. }
  1955. /* read and process one input packet if needed */
  1956. if (need_input) {
  1957. ret = process_input();
  1958. if (ret == AVERROR_EOF)
  1959. need_input = 0;
  1960. }
  1961. ret = poll_filters();
  1962. if (ret < 0) {
  1963. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  1964. continue;
  1965. av_log(NULL, AV_LOG_ERROR, "Error while filtering.\n");
  1966. break;
  1967. }
  1968. /* dump report by using the output first video and audio streams */
  1969. print_report(0, timer_start);
  1970. }
  1971. #if HAVE_PTHREADS
  1972. free_input_threads();
  1973. #endif
  1974. /* at the end of stream, we must flush the decoder buffers */
  1975. for (i = 0; i < nb_input_streams; i++) {
  1976. ist = input_streams[i];
  1977. if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
  1978. output_packet(ist, NULL);
  1979. }
  1980. }
  1981. poll_filters();
  1982. flush_encoders();
  1983. term_exit();
  1984. /* write the trailer if needed and close file */
  1985. for (i = 0; i < nb_output_files; i++) {
  1986. os = output_files[i]->ctx;
  1987. av_write_trailer(os);
  1988. }
  1989. /* dump report by using the first video and audio streams */
  1990. print_report(1, timer_start);
  1991. /* close each encoder */
  1992. for (i = 0; i < nb_output_streams; i++) {
  1993. ost = output_streams[i];
  1994. if (ost->encoding_needed) {
  1995. av_freep(&ost->st->codec->stats_in);
  1996. avcodec_close(ost->st->codec);
  1997. }
  1998. }
  1999. /* close each decoder */
  2000. for (i = 0; i < nb_input_streams; i++) {
  2001. ist = input_streams[i];
  2002. if (ist->decoding_needed) {
  2003. avcodec_close(ist->st->codec);
  2004. }
  2005. }
  2006. /* finished ! */
  2007. ret = 0;
  2008. fail:
  2009. #if HAVE_PTHREADS
  2010. free_input_threads();
  2011. #endif
  2012. if (output_streams) {
  2013. for (i = 0; i < nb_output_streams; i++) {
  2014. ost = output_streams[i];
  2015. if (ost) {
  2016. if (ost->stream_copy)
  2017. av_freep(&ost->st->codec->extradata);
  2018. if (ost->logfile) {
  2019. fclose(ost->logfile);
  2020. ost->logfile = NULL;
  2021. }
  2022. av_freep(&ost->st->codec->subtitle_header);
  2023. av_free(ost->forced_kf_pts);
  2024. av_dict_free(&ost->opts);
  2025. }
  2026. }
  2027. }
  2028. return ret;
  2029. }
  2030. static int64_t getutime(void)
  2031. {
  2032. #if HAVE_GETRUSAGE
  2033. struct rusage rusage;
  2034. getrusage(RUSAGE_SELF, &rusage);
  2035. return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
  2036. #elif HAVE_GETPROCESSTIMES
  2037. HANDLE proc;
  2038. FILETIME c, e, k, u;
  2039. proc = GetCurrentProcess();
  2040. GetProcessTimes(proc, &c, &e, &k, &u);
  2041. return ((int64_t) u.dwHighDateTime << 32 | u.dwLowDateTime) / 10;
  2042. #else
  2043. return av_gettime();
  2044. #endif
  2045. }
  2046. static int64_t getmaxrss(void)
  2047. {
  2048. #if HAVE_GETRUSAGE && HAVE_STRUCT_RUSAGE_RU_MAXRSS
  2049. struct rusage rusage;
  2050. getrusage(RUSAGE_SELF, &rusage);
  2051. return (int64_t)rusage.ru_maxrss * 1024;
  2052. #elif HAVE_GETPROCESSMEMORYINFO
  2053. HANDLE proc;
  2054. PROCESS_MEMORY_COUNTERS memcounters;
  2055. proc = GetCurrentProcess();
  2056. memcounters.cb = sizeof(memcounters);
  2057. GetProcessMemoryInfo(proc, &memcounters, sizeof(memcounters));
  2058. return memcounters.PeakPagefileUsage;
  2059. #else
  2060. return 0;
  2061. #endif
  2062. }
  2063. static void parse_cpuflags(int argc, char **argv, const OptionDef *options)
  2064. {
  2065. int idx = locate_option(argc, argv, options, "cpuflags");
  2066. if (idx && argv[idx + 1])
  2067. opt_cpuflags(NULL, "cpuflags", argv[idx + 1]);
  2068. }
  2069. int main(int argc, char **argv)
  2070. {
  2071. OptionsContext o = { 0 };
  2072. int64_t ti;
  2073. atexit(exit_program);
  2074. reset_options(&o);
  2075. av_log_set_flags(AV_LOG_SKIP_REPEATED);
  2076. parse_loglevel(argc, argv, options);
  2077. avcodec_register_all();
  2078. #if CONFIG_AVDEVICE
  2079. avdevice_register_all();
  2080. #endif
  2081. avfilter_register_all();
  2082. av_register_all();
  2083. avformat_network_init();
  2084. show_banner();
  2085. parse_cpuflags(argc, argv, options);
  2086. /* parse options */
  2087. parse_options(&o, argc, argv, options, opt_output_file);
  2088. if (nb_output_files <= 0 && nb_input_files == 0) {
  2089. show_usage();
  2090. av_log(NULL, AV_LOG_WARNING, "Use -h to get full help or, even better, run 'man %s'\n", program_name);
  2091. exit(1);
  2092. }
  2093. /* file converter / grab */
  2094. if (nb_output_files <= 0) {
  2095. fprintf(stderr, "At least one output file must be specified\n");
  2096. exit(1);
  2097. }
  2098. ti = getutime();
  2099. if (transcode() < 0)
  2100. exit(1);
  2101. ti = getutime() - ti;
  2102. if (do_benchmark) {
  2103. int maxrss = getmaxrss() / 1024;
  2104. printf("bench: utime=%0.3fs maxrss=%ikB\n", ti / 1000000.0, maxrss);
  2105. }
  2106. exit(0);
  2107. return 0;
  2108. }