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.

598 lines
21KB

  1. /*
  2. * Copyright (c) 2010 Nicolas George
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2014 Andrey Utkin
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /**
  25. * @file
  26. * API example for demuxing, decoding, filtering, encoding and muxing
  27. * @example transcoding.c
  28. */
  29. #include <libavcodec/avcodec.h>
  30. #include <libavformat/avformat.h>
  31. #include <libavfilter/avfiltergraph.h>
  32. #include <libavfilter/avcodec.h>
  33. #include <libavfilter/buffersink.h>
  34. #include <libavfilter/buffersrc.h>
  35. #include <libavutil/opt.h>
  36. #include <libavutil/pixdesc.h>
  37. static AVFormatContext *ifmt_ctx;
  38. static AVFormatContext *ofmt_ctx;
  39. typedef struct FilteringContext {
  40. AVFilterContext *buffersink_ctx;
  41. AVFilterContext *buffersrc_ctx;
  42. AVFilterGraph *filter_graph;
  43. } FilteringContext;
  44. static FilteringContext *filter_ctx;
  45. static int open_input_file(const char *filename)
  46. {
  47. int ret;
  48. unsigned int i;
  49. ifmt_ctx = NULL;
  50. if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
  51. av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
  52. return ret;
  53. }
  54. if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
  55. av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
  56. return ret;
  57. }
  58. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  59. AVStream *stream;
  60. AVCodecContext *codec_ctx;
  61. stream = ifmt_ctx->streams[i];
  62. codec_ctx = stream->codec;
  63. /* Reencode video & audio and remux subtitles etc. */
  64. if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
  65. || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  66. /* Open decoder */
  67. ret = avcodec_open2(codec_ctx,
  68. avcodec_find_decoder(codec_ctx->codec_id), NULL);
  69. if (ret < 0) {
  70. av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
  71. return ret;
  72. }
  73. }
  74. }
  75. av_dump_format(ifmt_ctx, 0, filename, 0);
  76. return 0;
  77. }
  78. static int open_output_file(const char *filename)
  79. {
  80. AVStream *out_stream;
  81. AVStream *in_stream;
  82. AVCodecContext *dec_ctx, *enc_ctx;
  83. AVCodec *encoder;
  84. int ret;
  85. unsigned int i;
  86. ofmt_ctx = NULL;
  87. avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
  88. if (!ofmt_ctx) {
  89. av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
  90. return AVERROR_UNKNOWN;
  91. }
  92. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  93. out_stream = avformat_new_stream(ofmt_ctx, NULL);
  94. if (!out_stream) {
  95. av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
  96. return AVERROR_UNKNOWN;
  97. }
  98. in_stream = ifmt_ctx->streams[i];
  99. dec_ctx = in_stream->codec;
  100. enc_ctx = out_stream->codec;
  101. if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
  102. || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  103. /* in this example, we choose transcoding to same codec */
  104. encoder = avcodec_find_encoder(dec_ctx->codec_id);
  105. /* In this example, we transcode to same properties (picture size,
  106. * sample rate etc.). These properties can be changed for output
  107. * streams easily using filters */
  108. if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  109. enc_ctx->height = dec_ctx->height;
  110. enc_ctx->width = dec_ctx->width;
  111. enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
  112. /* take first format from list of supported formats */
  113. enc_ctx->pix_fmt = encoder->pix_fmts[0];
  114. /* video time_base can be set to whatever is handy and supported by encoder */
  115. enc_ctx->time_base = dec_ctx->time_base;
  116. } else {
  117. enc_ctx->sample_rate = dec_ctx->sample_rate;
  118. enc_ctx->channel_layout = dec_ctx->channel_layout;
  119. enc_ctx->channels = av_get_channel_layout_nb_channels(enc_ctx->channel_layout);
  120. /* take first format from list of supported formats */
  121. enc_ctx->sample_fmt = encoder->sample_fmts[0];
  122. enc_ctx->time_base = (AVRational){1, enc_ctx->sample_rate};
  123. }
  124. /* Third parameter can be used to pass settings to encoder */
  125. ret = avcodec_open2(enc_ctx, encoder, NULL);
  126. if (ret < 0) {
  127. av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
  128. return ret;
  129. }
  130. } else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
  131. av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
  132. return AVERROR_INVALIDDATA;
  133. } else {
  134. /* if this stream must be remuxed */
  135. ret = avcodec_copy_context(ofmt_ctx->streams[i]->codec,
  136. ifmt_ctx->streams[i]->codec);
  137. if (ret < 0) {
  138. av_log(NULL, AV_LOG_ERROR, "Copying stream context failed\n");
  139. return ret;
  140. }
  141. }
  142. if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
  143. enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
  144. }
  145. av_dump_format(ofmt_ctx, 0, filename, 1);
  146. if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
  147. ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
  148. if (ret < 0) {
  149. av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
  150. return ret;
  151. }
  152. }
  153. /* init muxer, write output file header */
  154. ret = avformat_write_header(ofmt_ctx, NULL);
  155. if (ret < 0) {
  156. av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. static int init_filter(FilteringContext* fctx, AVCodecContext *dec_ctx,
  162. AVCodecContext *enc_ctx, const char *filter_spec)
  163. {
  164. char args[512];
  165. int ret = 0;
  166. AVFilter *buffersrc = NULL;
  167. AVFilter *buffersink = NULL;
  168. AVFilterContext *buffersrc_ctx = NULL;
  169. AVFilterContext *buffersink_ctx = NULL;
  170. AVFilterInOut *outputs = avfilter_inout_alloc();
  171. AVFilterInOut *inputs = avfilter_inout_alloc();
  172. AVFilterGraph *filter_graph = avfilter_graph_alloc();
  173. if (!outputs || !inputs || !filter_graph) {
  174. ret = AVERROR(ENOMEM);
  175. goto end;
  176. }
  177. if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
  178. buffersrc = avfilter_get_by_name("buffer");
  179. buffersink = avfilter_get_by_name("buffersink");
  180. if (!buffersrc || !buffersink) {
  181. av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
  182. ret = AVERROR_UNKNOWN;
  183. goto end;
  184. }
  185. snprintf(args, sizeof(args),
  186. "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
  187. dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
  188. dec_ctx->time_base.num, dec_ctx->time_base.den,
  189. dec_ctx->sample_aspect_ratio.num,
  190. dec_ctx->sample_aspect_ratio.den);
  191. ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
  192. args, NULL, filter_graph);
  193. if (ret < 0) {
  194. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
  195. goto end;
  196. }
  197. ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
  198. NULL, NULL, filter_graph);
  199. if (ret < 0) {
  200. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
  201. goto end;
  202. }
  203. ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
  204. (uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
  205. AV_OPT_SEARCH_CHILDREN);
  206. if (ret < 0) {
  207. av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
  208. goto end;
  209. }
  210. } else if (dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
  211. buffersrc = avfilter_get_by_name("abuffer");
  212. buffersink = avfilter_get_by_name("abuffersink");
  213. if (!buffersrc || !buffersink) {
  214. av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
  215. ret = AVERROR_UNKNOWN;
  216. goto end;
  217. }
  218. if (!dec_ctx->channel_layout)
  219. dec_ctx->channel_layout =
  220. av_get_default_channel_layout(dec_ctx->channels);
  221. snprintf(args, sizeof(args),
  222. "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%"PRIx64,
  223. dec_ctx->time_base.num, dec_ctx->time_base.den, dec_ctx->sample_rate,
  224. av_get_sample_fmt_name(dec_ctx->sample_fmt),
  225. dec_ctx->channel_layout);
  226. ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
  227. args, NULL, filter_graph);
  228. if (ret < 0) {
  229. av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer source\n");
  230. goto end;
  231. }
  232. ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
  233. NULL, NULL, filter_graph);
  234. if (ret < 0) {
  235. av_log(NULL, AV_LOG_ERROR, "Cannot create audio buffer sink\n");
  236. goto end;
  237. }
  238. ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
  239. (uint8_t*)&enc_ctx->sample_fmt, sizeof(enc_ctx->sample_fmt),
  240. AV_OPT_SEARCH_CHILDREN);
  241. if (ret < 0) {
  242. av_log(NULL, AV_LOG_ERROR, "Cannot set output sample format\n");
  243. goto end;
  244. }
  245. ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
  246. (uint8_t*)&enc_ctx->channel_layout,
  247. sizeof(enc_ctx->channel_layout), AV_OPT_SEARCH_CHILDREN);
  248. if (ret < 0) {
  249. av_log(NULL, AV_LOG_ERROR, "Cannot set output channel layout\n");
  250. goto end;
  251. }
  252. ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
  253. (uint8_t*)&enc_ctx->sample_rate, sizeof(enc_ctx->sample_rate),
  254. AV_OPT_SEARCH_CHILDREN);
  255. if (ret < 0) {
  256. av_log(NULL, AV_LOG_ERROR, "Cannot set output sample rate\n");
  257. goto end;
  258. }
  259. } else {
  260. ret = AVERROR_UNKNOWN;
  261. goto end;
  262. }
  263. /* Endpoints for the filter graph. */
  264. outputs->name = av_strdup("in");
  265. outputs->filter_ctx = buffersrc_ctx;
  266. outputs->pad_idx = 0;
  267. outputs->next = NULL;
  268. inputs->name = av_strdup("out");
  269. inputs->filter_ctx = buffersink_ctx;
  270. inputs->pad_idx = 0;
  271. inputs->next = NULL;
  272. if (!outputs->name || !inputs->name) {
  273. ret = AVERROR(ENOMEM);
  274. goto end;
  275. }
  276. if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
  277. &inputs, &outputs, NULL)) < 0)
  278. goto end;
  279. if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
  280. goto end;
  281. /* Fill FilteringContext */
  282. fctx->buffersrc_ctx = buffersrc_ctx;
  283. fctx->buffersink_ctx = buffersink_ctx;
  284. fctx->filter_graph = filter_graph;
  285. end:
  286. avfilter_inout_free(&inputs);
  287. avfilter_inout_free(&outputs);
  288. return ret;
  289. }
  290. static int init_filters(void)
  291. {
  292. const char *filter_spec;
  293. unsigned int i;
  294. int ret;
  295. filter_ctx = av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
  296. if (!filter_ctx)
  297. return AVERROR(ENOMEM);
  298. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  299. filter_ctx[i].buffersrc_ctx = NULL;
  300. filter_ctx[i].buffersink_ctx = NULL;
  301. filter_ctx[i].filter_graph = NULL;
  302. if (!(ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO
  303. || ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO))
  304. continue;
  305. if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  306. filter_spec = "null"; /* passthrough (dummy) filter for video */
  307. else
  308. filter_spec = "anull"; /* passthrough (dummy) filter for audio */
  309. ret = init_filter(&filter_ctx[i], ifmt_ctx->streams[i]->codec,
  310. ofmt_ctx->streams[i]->codec, filter_spec);
  311. if (ret)
  312. return ret;
  313. }
  314. return 0;
  315. }
  316. static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame) {
  317. int ret;
  318. int got_frame_local;
  319. AVPacket enc_pkt;
  320. int (*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
  321. (ifmt_ctx->streams[stream_index]->codec->codec_type ==
  322. AVMEDIA_TYPE_VIDEO) ? avcodec_encode_video2 : avcodec_encode_audio2;
  323. if (!got_frame)
  324. got_frame = &got_frame_local;
  325. av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
  326. /* encode filtered frame */
  327. enc_pkt.data = NULL;
  328. enc_pkt.size = 0;
  329. av_init_packet(&enc_pkt);
  330. ret = enc_func(ofmt_ctx->streams[stream_index]->codec, &enc_pkt,
  331. filt_frame, got_frame);
  332. av_frame_free(&filt_frame);
  333. if (ret < 0)
  334. return ret;
  335. if (!(*got_frame))
  336. return 0;
  337. /* prepare packet for muxing */
  338. enc_pkt.stream_index = stream_index;
  339. enc_pkt.dts = av_rescale_q_rnd(enc_pkt.dts,
  340. ofmt_ctx->streams[stream_index]->codec->time_base,
  341. ofmt_ctx->streams[stream_index]->time_base,
  342. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  343. enc_pkt.pts = av_rescale_q_rnd(enc_pkt.pts,
  344. ofmt_ctx->streams[stream_index]->codec->time_base,
  345. ofmt_ctx->streams[stream_index]->time_base,
  346. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  347. enc_pkt.duration = av_rescale_q(enc_pkt.duration,
  348. ofmt_ctx->streams[stream_index]->codec->time_base,
  349. ofmt_ctx->streams[stream_index]->time_base);
  350. av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
  351. /* mux encoded frame */
  352. ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
  353. return ret;
  354. }
  355. static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
  356. {
  357. int ret;
  358. AVFrame *filt_frame;
  359. av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
  360. /* push the decoded frame into the filtergraph */
  361. ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
  362. frame, 0);
  363. if (ret < 0) {
  364. av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
  365. return ret;
  366. }
  367. /* pull filtered frames from the filtergraph */
  368. while (1) {
  369. filt_frame = av_frame_alloc();
  370. if (!filt_frame) {
  371. ret = AVERROR(ENOMEM);
  372. break;
  373. }
  374. av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
  375. ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
  376. filt_frame);
  377. if (ret < 0) {
  378. /* if no more frames for output - returns AVERROR(EAGAIN)
  379. * if flushed and no more frames for output - returns AVERROR_EOF
  380. * rewrite retcode to 0 to show it as normal procedure completion
  381. */
  382. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  383. ret = 0;
  384. av_frame_free(&filt_frame);
  385. break;
  386. }
  387. filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
  388. ret = encode_write_frame(filt_frame, stream_index, NULL);
  389. if (ret < 0)
  390. break;
  391. }
  392. return ret;
  393. }
  394. static int flush_encoder(unsigned int stream_index)
  395. {
  396. int ret;
  397. int got_frame;
  398. if (!(ofmt_ctx->streams[stream_index]->codec->codec->capabilities &
  399. CODEC_CAP_DELAY))
  400. return 0;
  401. while (1) {
  402. av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
  403. ret = encode_write_frame(NULL, stream_index, &got_frame);
  404. if (ret < 0)
  405. break;
  406. if (!got_frame)
  407. return 0;
  408. }
  409. return ret;
  410. }
  411. int main(int argc, char **argv)
  412. {
  413. int ret;
  414. AVPacket packet = { .data = NULL, .size = 0 };
  415. AVFrame *frame = NULL;
  416. enum AVMediaType type;
  417. unsigned int stream_index;
  418. unsigned int i;
  419. int got_frame;
  420. int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
  421. if (argc != 3) {
  422. av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
  423. return 1;
  424. }
  425. av_register_all();
  426. avfilter_register_all();
  427. if ((ret = open_input_file(argv[1])) < 0)
  428. goto end;
  429. if ((ret = open_output_file(argv[2])) < 0)
  430. goto end;
  431. if ((ret = init_filters()) < 0)
  432. goto end;
  433. /* read all packets */
  434. while (1) {
  435. if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
  436. break;
  437. stream_index = packet.stream_index;
  438. type = ifmt_ctx->streams[packet.stream_index]->codec->codec_type;
  439. av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
  440. stream_index);
  441. if (filter_ctx[stream_index].filter_graph) {
  442. av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
  443. frame = av_frame_alloc();
  444. if (!frame) {
  445. ret = AVERROR(ENOMEM);
  446. break;
  447. }
  448. packet.dts = av_rescale_q_rnd(packet.dts,
  449. ifmt_ctx->streams[stream_index]->time_base,
  450. ifmt_ctx->streams[stream_index]->codec->time_base,
  451. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  452. packet.pts = av_rescale_q_rnd(packet.pts,
  453. ifmt_ctx->streams[stream_index]->time_base,
  454. ifmt_ctx->streams[stream_index]->codec->time_base,
  455. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  456. dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
  457. avcodec_decode_audio4;
  458. ret = dec_func(ifmt_ctx->streams[stream_index]->codec, frame,
  459. &got_frame, &packet);
  460. if (ret < 0) {
  461. av_frame_free(&frame);
  462. av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
  463. break;
  464. }
  465. if (got_frame) {
  466. frame->pts = av_frame_get_best_effort_timestamp(frame);
  467. ret = filter_encode_write_frame(frame, stream_index);
  468. av_frame_free(&frame);
  469. if (ret < 0)
  470. goto end;
  471. } else {
  472. av_frame_free(&frame);
  473. }
  474. } else {
  475. /* remux this frame without reencoding */
  476. packet.dts = av_rescale_q_rnd(packet.dts,
  477. ifmt_ctx->streams[stream_index]->time_base,
  478. ofmt_ctx->streams[stream_index]->time_base,
  479. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  480. packet.pts = av_rescale_q_rnd(packet.pts,
  481. ifmt_ctx->streams[stream_index]->time_base,
  482. ofmt_ctx->streams[stream_index]->time_base,
  483. AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  484. ret = av_interleaved_write_frame(ofmt_ctx, &packet);
  485. if (ret < 0)
  486. goto end;
  487. }
  488. av_free_packet(&packet);
  489. }
  490. /* flush filters and encoders */
  491. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  492. /* flush filter */
  493. if (!filter_ctx[i].filter_graph)
  494. continue;
  495. ret = filter_encode_write_frame(NULL, i);
  496. if (ret < 0) {
  497. av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
  498. goto end;
  499. }
  500. /* flush encoder */
  501. ret = flush_encoder(i);
  502. if (ret < 0) {
  503. av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
  504. goto end;
  505. }
  506. }
  507. av_write_trailer(ofmt_ctx);
  508. end:
  509. av_free_packet(&packet);
  510. av_frame_free(&frame);
  511. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  512. avcodec_close(ifmt_ctx->streams[i]->codec);
  513. if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && ofmt_ctx->streams[i]->codec)
  514. avcodec_close(ofmt_ctx->streams[i]->codec);
  515. if (filter_ctx && filter_ctx[i].filter_graph)
  516. avfilter_graph_free(&filter_ctx[i].filter_graph);
  517. }
  518. av_free(filter_ctx);
  519. avformat_close_input(&ifmt_ctx);
  520. if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
  521. avio_close(ofmt_ctx->pb);
  522. avformat_free_context(ofmt_ctx);
  523. if (ret < 0)
  524. av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
  525. return ret ? 1 : 0;
  526. }