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.

580 lines
20KB

  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. av_packet_rescale_ts(&enc_pkt,
  340. ofmt_ctx->streams[stream_index]->codec->time_base,
  341. ofmt_ctx->streams[stream_index]->time_base);
  342. av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
  343. /* mux encoded frame */
  344. ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
  345. return ret;
  346. }
  347. static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index)
  348. {
  349. int ret;
  350. AVFrame *filt_frame;
  351. av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
  352. /* push the decoded frame into the filtergraph */
  353. ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
  354. frame, 0);
  355. if (ret < 0) {
  356. av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
  357. return ret;
  358. }
  359. /* pull filtered frames from the filtergraph */
  360. while (1) {
  361. filt_frame = av_frame_alloc();
  362. if (!filt_frame) {
  363. ret = AVERROR(ENOMEM);
  364. break;
  365. }
  366. av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
  367. ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
  368. filt_frame);
  369. if (ret < 0) {
  370. /* if no more frames for output - returns AVERROR(EAGAIN)
  371. * if flushed and no more frames for output - returns AVERROR_EOF
  372. * rewrite retcode to 0 to show it as normal procedure completion
  373. */
  374. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  375. ret = 0;
  376. av_frame_free(&filt_frame);
  377. break;
  378. }
  379. filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
  380. ret = encode_write_frame(filt_frame, stream_index, NULL);
  381. if (ret < 0)
  382. break;
  383. }
  384. return ret;
  385. }
  386. static int flush_encoder(unsigned int stream_index)
  387. {
  388. int ret;
  389. int got_frame;
  390. if (!(ofmt_ctx->streams[stream_index]->codec->codec->capabilities &
  391. CODEC_CAP_DELAY))
  392. return 0;
  393. while (1) {
  394. av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
  395. ret = encode_write_frame(NULL, stream_index, &got_frame);
  396. if (ret < 0)
  397. break;
  398. if (!got_frame)
  399. return 0;
  400. }
  401. return ret;
  402. }
  403. int main(int argc, char **argv)
  404. {
  405. int ret;
  406. AVPacket packet = { .data = NULL, .size = 0 };
  407. AVFrame *frame = NULL;
  408. enum AVMediaType type;
  409. unsigned int stream_index;
  410. unsigned int i;
  411. int got_frame;
  412. int (*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
  413. if (argc != 3) {
  414. av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file> <output file>\n", argv[0]);
  415. return 1;
  416. }
  417. av_register_all();
  418. avfilter_register_all();
  419. if ((ret = open_input_file(argv[1])) < 0)
  420. goto end;
  421. if ((ret = open_output_file(argv[2])) < 0)
  422. goto end;
  423. if ((ret = init_filters()) < 0)
  424. goto end;
  425. /* read all packets */
  426. while (1) {
  427. if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
  428. break;
  429. stream_index = packet.stream_index;
  430. type = ifmt_ctx->streams[packet.stream_index]->codec->codec_type;
  431. av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
  432. stream_index);
  433. if (filter_ctx[stream_index].filter_graph) {
  434. av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
  435. frame = av_frame_alloc();
  436. if (!frame) {
  437. ret = AVERROR(ENOMEM);
  438. break;
  439. }
  440. av_packet_rescale_ts(&packet,
  441. ifmt_ctx->streams[stream_index]->time_base,
  442. ifmt_ctx->streams[stream_index]->codec->time_base);
  443. dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
  444. avcodec_decode_audio4;
  445. ret = dec_func(ifmt_ctx->streams[stream_index]->codec, frame,
  446. &got_frame, &packet);
  447. if (ret < 0) {
  448. av_frame_free(&frame);
  449. av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
  450. break;
  451. }
  452. if (got_frame) {
  453. frame->pts = av_frame_get_best_effort_timestamp(frame);
  454. ret = filter_encode_write_frame(frame, stream_index);
  455. av_frame_free(&frame);
  456. if (ret < 0)
  457. goto end;
  458. } else {
  459. av_frame_free(&frame);
  460. }
  461. } else {
  462. /* remux this frame without reencoding */
  463. av_packet_rescale_ts(&packet,
  464. ifmt_ctx->streams[stream_index]->time_base,
  465. ofmt_ctx->streams[stream_index]->time_base);
  466. ret = av_interleaved_write_frame(ofmt_ctx, &packet);
  467. if (ret < 0)
  468. goto end;
  469. }
  470. av_free_packet(&packet);
  471. }
  472. /* flush filters and encoders */
  473. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  474. /* flush filter */
  475. if (!filter_ctx[i].filter_graph)
  476. continue;
  477. ret = filter_encode_write_frame(NULL, i);
  478. if (ret < 0) {
  479. av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
  480. goto end;
  481. }
  482. /* flush encoder */
  483. ret = flush_encoder(i);
  484. if (ret < 0) {
  485. av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
  486. goto end;
  487. }
  488. }
  489. av_write_trailer(ofmt_ctx);
  490. end:
  491. av_free_packet(&packet);
  492. av_frame_free(&frame);
  493. for (i = 0; i < ifmt_ctx->nb_streams; i++) {
  494. avcodec_close(ifmt_ctx->streams[i]->codec);
  495. if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && ofmt_ctx->streams[i]->codec)
  496. avcodec_close(ofmt_ctx->streams[i]->codec);
  497. if (filter_ctx && filter_ctx[i].filter_graph)
  498. avfilter_graph_free(&filter_ctx[i].filter_graph);
  499. }
  500. av_free(filter_ctx);
  501. avformat_close_input(&ifmt_ctx);
  502. if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
  503. avio_close(ofmt_ctx->pb);
  504. avformat_free_context(ofmt_ctx);
  505. if (ret < 0)
  506. av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));
  507. return ret ? 1 : 0;
  508. }