extract_mvs.c 5.8 KB

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