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.

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