You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.4KB

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