filtering_video.c 8.0 KB

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