transcode.c 24 KB

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