transcoding.c 20 KB

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