transcode.c 22 KB

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