transcoding.c 20 KB

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