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.

189 lines
5.6KB

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