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