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.

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