filtering.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2010 Nicolas George
  3. * Copyright (c) 2011 Stefano Sabatini
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. /**
  24. * @file
  25. * API example for decoding and filtering
  26. */
  27. #define _XOPEN_SOURCE 600 /* for usleep */
  28. #include <libavcodec/avcodec.h>
  29. #include <libavformat/avformat.h>
  30. #include <libavfilter/avfiltergraph.h>
  31. #include <libavfilter/vsink_buffer.h>
  32. #include <libavfilter/vsrc_buffer.h>
  33. const char *filter_descr = "scale=78:24";
  34. static AVFormatContext *fmt_ctx;
  35. static AVCodecContext *dec_ctx;
  36. AVFilterContext *buffersink_ctx;
  37. AVFilterContext *buffersrc_ctx;
  38. AVFilterGraph *filter_graph;
  39. static int video_stream_index = -1;
  40. static int64_t last_pts = AV_NOPTS_VALUE;
  41. static int open_input_file(const char *filename)
  42. {
  43. int ret, i;
  44. AVCodec *dec;
  45. if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
  46. av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
  47. return ret;
  48. }
  49. if ((ret = av_find_stream_info(fmt_ctx)) < 0) {
  50. av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
  51. return ret;
  52. }
  53. /* select the video stream */
  54. ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
  55. if (ret < 0) {
  56. av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
  57. return ret;
  58. }
  59. video_stream_index = ret;
  60. dec_ctx = fmt_ctx->streams[video_stream_index]->codec;
  61. /* init the video decoder */
  62. if ((ret = avcodec_open(dec_ctx, dec)) < 0) {
  63. av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
  64. return ret;
  65. }
  66. return 0;
  67. }
  68. static int init_filters(const char *filters_descr)
  69. {
  70. char args[512];
  71. int ret;
  72. AVFilter *buffersrc = avfilter_get_by_name("buffer");
  73. AVFilter *buffersink = avfilter_get_by_name("buffersink");
  74. AVFilterInOut *outputs = avfilter_inout_alloc();
  75. AVFilterInOut *inputs = avfilter_inout_alloc();
  76. enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, PIX_FMT_NONE };
  77. filter_graph = avfilter_graph_alloc();
  78. /* buffer video source: the decoded frames from the decoder will be inserted here. */
  79. snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d",
  80. dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
  81. dec_ctx->time_base.num, dec_ctx->time_base.den,
  82. dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
  83. ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
  84. args, NULL, filter_graph);
  85. if (ret < 0) {
  86. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
  87. return ret;
  88. }
  89. /* buffer video sink: to terminate the filter chain. */
  90. ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
  91. NULL, pix_fmts, filter_graph);
  92. if (ret < 0) {
  93. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
  94. return ret;
  95. }
  96. /* Endpoints for the filter graph. */
  97. outputs->name = av_strdup("in");
  98. outputs->filter_ctx = buffersrc_ctx;
  99. outputs->pad_idx = 0;
  100. outputs->next = NULL;
  101. inputs->name = av_strdup("out");
  102. inputs->filter_ctx = buffersink_ctx;
  103. inputs->pad_idx = 0;
  104. inputs->next = NULL;
  105. if ((ret = avfilter_graph_parse(filter_graph, filter_descr,
  106. &inputs, &outputs, NULL)) < 0)
  107. return ret;
  108. if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
  109. return ret;
  110. }
  111. static void display_picref(AVFilterBufferRef *picref, AVRational time_base)
  112. {
  113. int x, y;
  114. uint8_t *p0, *p;
  115. int64_t delay;
  116. if (picref->pts != AV_NOPTS_VALUE) {
  117. if (last_pts != AV_NOPTS_VALUE) {
  118. /* sleep roughly the right amount of time;
  119. * usleep is in microseconds, just like AV_TIME_BASE. */
  120. delay = av_rescale_q(picref->pts - last_pts,
  121. time_base, AV_TIME_BASE_Q);
  122. if (delay > 0 && delay < 1000000)
  123. usleep(delay);
  124. }
  125. last_pts = picref->pts;
  126. }
  127. /* Trivial ASCII grayscale display. */
  128. p0 = picref->data[0];
  129. puts("\033c");
  130. for (y = 0; y < picref->video->h; y++) {
  131. p = p0;
  132. for (x = 0; x < picref->video->w; x++)
  133. putchar(" .-+#"[*(p++) / 52]);
  134. putchar('\n');
  135. p0 += picref->linesize[0];
  136. }
  137. fflush(stdout);
  138. }
  139. int main(int argc, char **argv)
  140. {
  141. int ret;
  142. AVPacket packet;
  143. AVFrame frame;
  144. int got_frame;
  145. if (argc != 2) {
  146. fprintf(stderr, "Usage: %s file\n", argv[0]);
  147. exit(1);
  148. }
  149. avcodec_register_all();
  150. av_register_all();
  151. avfilter_register_all();
  152. if ((ret = open_input_file(argv[1]) < 0))
  153. goto end;
  154. if ((ret = init_filters(filter_descr)) < 0)
  155. goto end;
  156. /* read all packets */
  157. while (1) {
  158. AVFilterBufferRef *picref;
  159. if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
  160. break;
  161. if (packet.stream_index == video_stream_index) {
  162. avcodec_get_frame_defaults(&frame);
  163. got_frame = 0;
  164. ret = avcodec_decode_video2(dec_ctx, &frame, &got_frame, &packet);
  165. av_free_packet(&packet);
  166. if (ret < 0) {
  167. av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
  168. break;
  169. }
  170. if (got_frame) {
  171. if (frame.pts == AV_NOPTS_VALUE)
  172. frame.pts = frame.pkt_dts == AV_NOPTS_VALUE ?
  173. frame.pkt_dts : frame.pkt_pts;
  174. /* push the decoded frame into the filtergraph */
  175. av_vsrc_buffer_add_frame(buffersrc_ctx, &frame);
  176. /* pull filtered pictures from the filtergraph */
  177. while (avfilter_poll_frame(buffersink_ctx->inputs[0])) {
  178. av_vsink_buffer_get_video_buffer_ref(buffersink_ctx, &picref, 0);
  179. if (picref) {
  180. display_picref(picref, buffersink_ctx->inputs[0]->time_base);
  181. avfilter_unref_buffer(picref);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. end:
  188. avfilter_graph_free(&filter_graph);
  189. if (dec_ctx)
  190. avcodec_close(dec_ctx);
  191. av_close_input_file(fmt_ctx);
  192. if (ret < 0 && ret != AVERROR_EOF) {
  193. char buf[1024];
  194. av_strerror(ret, buf, sizeof(buf));
  195. fprintf(stderr, "Error occurred: %s\n", buf);
  196. exit(1);
  197. }
  198. exit(0);
  199. }