filtering.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/vsrc_buffer.h>
  32. const char *filter_descr = "scale=78:24";
  33. static AVFormatContext *fmt_ctx;
  34. static AVCodecContext *dec_ctx;
  35. AVFilterContext *buffersink_ctx;
  36. AVFilterContext *buffersrc_ctx;
  37. AVFilterGraph *filter_graph;
  38. static int video_stream_index = -1;
  39. static int64_t last_pts = AV_NOPTS_VALUE;
  40. static int open_input_file(const char *filename)
  41. {
  42. int ret, i;
  43. AVCodec *dec;
  44. if ((ret = avformat_open_input(&fmt_ctx, filename, NULL, NULL)) < 0) {
  45. av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
  46. return ret;
  47. }
  48. if ((ret = av_find_stream_info(fmt_ctx)) < 0) {
  49. av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
  50. return ret;
  51. }
  52. /* select the video stream */
  53. ret = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &dec, 0);
  54. if (ret < 0) {
  55. av_log(NULL, AV_LOG_ERROR, "Cannot find a video stream in the input file\n");
  56. return ret;
  57. }
  58. video_stream_index = ret;
  59. dec_ctx = fmt_ctx->streams[video_stream_index]->codec;
  60. /* init the video decoder */
  61. if ((ret = avcodec_open(dec_ctx, dec)) < 0) {
  62. av_log(NULL, AV_LOG_ERROR, "Cannot open video decoder\n");
  63. return ret;
  64. }
  65. return 0;
  66. }
  67. static int init_filters(const char *filters_descr)
  68. {
  69. char args[512];
  70. int ret;
  71. AVFilter *buffersrc = avfilter_get_by_name("buffer");
  72. AVFilter *buffersink = avfilter_get_by_name("buffersink");
  73. AVFilterInOut *outputs = avfilter_inout_alloc();
  74. AVFilterInOut *inputs = avfilter_inout_alloc();
  75. enum PixelFormat pix_fmts[] = { PIX_FMT_GRAY8, PIX_FMT_NONE };
  76. filter_graph = avfilter_graph_alloc();
  77. /* buffer video source: the decoded frames from the decoder will be inserted here. */
  78. snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d",
  79. dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
  80. dec_ctx->time_base.num, dec_ctx->time_base.den,
  81. dec_ctx->sample_aspect_ratio.num, dec_ctx->sample_aspect_ratio.den);
  82. ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
  83. args, NULL, filter_graph);
  84. if (ret < 0) {
  85. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
  86. return ret;
  87. }
  88. /* buffer video sink: to terminate the filter chain. */
  89. ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
  90. NULL, pix_fmts, filter_graph);
  91. if (ret < 0) {
  92. av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
  93. return ret;
  94. }
  95. /* Endpoints for the filter graph. */
  96. outputs->name = av_strdup("in");
  97. outputs->filter_ctx = buffersrc_ctx;
  98. outputs->pad_idx = 0;
  99. outputs->next = NULL;
  100. inputs->name = av_strdup("out");
  101. inputs->filter_ctx = buffersink_ctx;
  102. inputs->pad_idx = 0;
  103. inputs->next = NULL;
  104. if ((ret = avfilter_graph_parse(filter_graph, filter_descr,
  105. &inputs, &outputs, NULL)) < 0)
  106. return ret;
  107. if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
  108. return ret;
  109. }
  110. static void display_picref(AVFilterBufferRef *picref, AVRational time_base)
  111. {
  112. int x, y;
  113. uint8_t *p0, *p;
  114. int64_t delay;
  115. if (picref->pts != AV_NOPTS_VALUE) {
  116. if (last_pts != AV_NOPTS_VALUE) {
  117. /* sleep roughly the right amount of time;
  118. * usleep is in microseconds, just like AV_TIME_BASE. */
  119. delay = av_rescale_q(picref->pts - last_pts,
  120. time_base, AV_TIME_BASE_Q);
  121. if (delay > 0 && delay < 1000000)
  122. usleep(delay);
  123. }
  124. last_pts = picref->pts;
  125. }
  126. /* Trivial ASCII grayscale display. */
  127. p0 = picref->data[0];
  128. puts("\033c");
  129. for (y = 0; y < picref->video->h; y++) {
  130. p = p0;
  131. for (x = 0; x < picref->video->w; x++)
  132. putchar(" .-+#"[*(p++) / 52]);
  133. putchar('\n');
  134. p0 += picref->linesize[0];
  135. }
  136. fflush(stdout);
  137. }
  138. int main(int argc, char **argv)
  139. {
  140. int ret;
  141. AVPacket packet;
  142. AVFrame frame;
  143. int got_frame;
  144. if (argc != 2) {
  145. fprintf(stderr, "Usage: %s file\n", argv[0]);
  146. exit(1);
  147. }
  148. avcodec_register_all();
  149. av_register_all();
  150. avfilter_register_all();
  151. if ((ret = open_input_file(argv[1])) < 0)
  152. goto end;
  153. if ((ret = init_filters(filter_descr)) < 0)
  154. goto end;
  155. /* read all packets */
  156. while (1) {
  157. AVFilterBufferRef *picref;
  158. if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
  159. break;
  160. if (packet.stream_index == video_stream_index) {
  161. avcodec_get_frame_defaults(&frame);
  162. got_frame = 0;
  163. ret = avcodec_decode_video2(dec_ctx, &frame, &got_frame, &packet);
  164. av_free_packet(&packet);
  165. if (ret < 0) {
  166. av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
  167. break;
  168. }
  169. if (got_frame) {
  170. if (frame.pts == AV_NOPTS_VALUE)
  171. frame.pts = frame.pkt_dts == AV_NOPTS_VALUE ?
  172. frame.pkt_dts : frame.pkt_pts;
  173. /* push the decoded frame into the filtergraph */
  174. av_vsrc_buffer_add_frame(buffersrc_ctx, &frame);
  175. /* pull filtered pictures from the filtergraph */
  176. while (avfilter_poll_frame(buffersink_ctx->inputs[0])) {
  177. av_vsink_buffer_get_video_buffer_ref(buffersink_ctx, &picref, 0);
  178. if (picref) {
  179. display_picref(picref, buffersink_ctx->inputs[0]->time_base);
  180. avfilter_unref_buffer(picref);
  181. }
  182. }
  183. }
  184. }
  185. }
  186. end:
  187. avfilter_graph_free(&filter_graph);
  188. if (dec_ctx)
  189. avcodec_close(dec_ctx);
  190. av_close_input_file(fmt_ctx);
  191. if (ret < 0 && ret != AVERROR_EOF) {
  192. char buf[1024];
  193. av_strerror(ret, buf, sizeof(buf));
  194. fprintf(stderr, "Error occurred: %s\n", buf);
  195. exit(1);
  196. }
  197. exit(0);
  198. }