extract_mvs.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2012 Stefano Sabatini
  3. * Copyright (c) 2014 Clément Bœsch
  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 libavcodec motion vectors extraction API usage example
  25. * @example extract_mvs.c
  26. *
  27. * Read from input file, decode video stream and print a motion vectors
  28. * representation to stdout.
  29. */
  30. #include <libavutil/motion_vector.h>
  31. #include <libavcodec/avcodec.h>
  32. #include <libavformat/avformat.h>
  33. static AVFormatContext *fmt_ctx = NULL;
  34. static AVCodecContext *video_dec_ctx = NULL;
  35. static AVStream *video_stream = NULL;
  36. static const char *src_filename = NULL;
  37. static int video_stream_idx = -1;
  38. static AVFrame *frame = NULL;
  39. static int video_frame_count = 0;
  40. static int decode_packet(const AVPacket *pkt)
  41. {
  42. int ret = avcodec_send_packet(video_dec_ctx, pkt);
  43. if (ret < 0) {
  44. fprintf(stderr, "Error while sending a packet to the decoder: %s\n", av_err2str(ret));
  45. return ret;
  46. }
  47. while (ret >= 0) {
  48. ret = avcodec_receive_frame(video_dec_ctx, frame);
  49. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  50. break;
  51. } else if (ret < 0) {
  52. fprintf(stderr, "Error while receiving a frame from the decoder: %s\n", av_err2str(ret));
  53. return ret;
  54. }
  55. if (ret >= 0) {
  56. int i;
  57. AVFrameSideData *sd;
  58. video_frame_count++;
  59. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
  60. if (sd) {
  61. const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
  62. for (i = 0; i < sd->size / sizeof(*mvs); i++) {
  63. const AVMotionVector *mv = &mvs[i];
  64. printf("%d,%2d,%2d,%2d,%4d,%4d,%4d,%4d,0x%"PRIx64",%4d,%4d,%4d\n",
  65. video_frame_count, mv->source,
  66. mv->w, mv->h, mv->src_x, mv->src_y,
  67. mv->dst_x, mv->dst_y, mv->flags,
  68. mv->motion_x, mv->motion_y, mv->motion_scale);
  69. }
  70. }
  71. av_frame_unref(frame);
  72. }
  73. }
  74. return 0;
  75. }
  76. static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
  77. {
  78. int ret;
  79. AVStream *st;
  80. AVCodecContext *dec_ctx = NULL;
  81. const AVCodec *dec = NULL;
  82. AVDictionary *opts = NULL;
  83. ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
  84. if (ret < 0) {
  85. fprintf(stderr, "Could not find %s stream in input file '%s'\n",
  86. av_get_media_type_string(type), src_filename);
  87. return ret;
  88. } else {
  89. int stream_idx = ret;
  90. st = fmt_ctx->streams[stream_idx];
  91. dec_ctx = avcodec_alloc_context3(dec);
  92. if (!dec_ctx) {
  93. fprintf(stderr, "Failed to allocate codec\n");
  94. return AVERROR(EINVAL);
  95. }
  96. ret = avcodec_parameters_to_context(dec_ctx, st->codecpar);
  97. if (ret < 0) {
  98. fprintf(stderr, "Failed to copy codec parameters to codec context\n");
  99. return ret;
  100. }
  101. /* Init the video decoder */
  102. av_dict_set(&opts, "flags2", "+export_mvs", 0);
  103. ret = avcodec_open2(dec_ctx, dec, &opts);
  104. av_dict_free(&opts);
  105. if (ret < 0) {
  106. fprintf(stderr, "Failed to open %s codec\n",
  107. av_get_media_type_string(type));
  108. return ret;
  109. }
  110. video_stream_idx = stream_idx;
  111. video_stream = fmt_ctx->streams[video_stream_idx];
  112. video_dec_ctx = dec_ctx;
  113. }
  114. return 0;
  115. }
  116. int main(int argc, char **argv)
  117. {
  118. int ret = 0;
  119. AVPacket *pkt = NULL;
  120. if (argc != 2) {
  121. fprintf(stderr, "Usage: %s <video>\n", argv[0]);
  122. exit(1);
  123. }
  124. src_filename = argv[1];
  125. if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
  126. fprintf(stderr, "Could not open source file %s\n", src_filename);
  127. exit(1);
  128. }
  129. if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
  130. fprintf(stderr, "Could not find stream information\n");
  131. exit(1);
  132. }
  133. open_codec_context(fmt_ctx, AVMEDIA_TYPE_VIDEO);
  134. av_dump_format(fmt_ctx, 0, src_filename, 0);
  135. if (!video_stream) {
  136. fprintf(stderr, "Could not find video stream in the input, aborting\n");
  137. ret = 1;
  138. goto end;
  139. }
  140. frame = av_frame_alloc();
  141. if (!frame) {
  142. fprintf(stderr, "Could not allocate frame\n");
  143. ret = AVERROR(ENOMEM);
  144. goto end;
  145. }
  146. pkt = av_packet_alloc();
  147. if (!pkt) {
  148. fprintf(stderr, "Could not allocate AVPacket\n");
  149. ret = AVERROR(ENOMEM);
  150. goto end;
  151. }
  152. printf("framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,flags,motion_x,motion_y,motion_scale\n");
  153. /* read frames from the file */
  154. while (av_read_frame(fmt_ctx, pkt) >= 0) {
  155. if (pkt->stream_index == video_stream_idx)
  156. ret = decode_packet(pkt);
  157. av_packet_unref(pkt);
  158. if (ret < 0)
  159. break;
  160. }
  161. /* flush cached frames */
  162. decode_packet(NULL);
  163. end:
  164. avcodec_free_context(&video_dec_ctx);
  165. avformat_close_input(&fmt_ctx);
  166. av_frame_free(&frame);
  167. av_packet_free(&pkt);
  168. return ret < 0;
  169. }