2 * Copyright (c) 2012 Stefano Sabatini
3 * Copyright (c) 2014 Clément Bœsch
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
25 * @file libavcodec motion vectors extraction API usage example
26 * @example extract_mvs.c
28 * Read from input file, decode video stream and print a motion vectors
29 * representation to stdout.
32 #include <libavutil/motion_vector.h>
33 #include <libavcodec/avcodec.h>
34 #include <libavformat/avformat.h>
36 static AVFormatContext *fmt_ctx = NULL;
37 static AVCodecContext *video_dec_ctx = NULL;
38 static AVStream *video_stream = NULL;
39 static const char *src_filename = NULL;
41 static int video_stream_idx = -1;
42 static AVFrame *frame = NULL;
43 static int video_frame_count = 0;
45 static int decode_packet(const AVPacket *pkt)
47 int ret = avcodec_send_packet(video_dec_ctx, pkt);
49 fprintf(stderr, "Error while sending a packet to the decoder: %s\n", av_err2str(ret));
54 ret = avcodec_receive_frame(video_dec_ctx, frame);
55 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
58 fprintf(stderr, "Error while receiving a frame from the decoder: %s\n", av_err2str(ret));
67 sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
69 const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
70 for (i = 0; i < sd->size / sizeof(*mvs); i++) {
71 const AVMotionVector *mv = &mvs[i];
72 printf("%d,%2d,%2d,%2d,%4d,%4d,%4d,%4d,0x%"PRIx64",%4d,%4d,%4d\n",
73 video_frame_count, mv->source,
74 mv->w, mv->h, mv->src_x, mv->src_y,
75 mv->dst_x, mv->dst_y, mv->flags,
76 mv->motion_x, mv->motion_y, mv->motion_scale);
79 av_frame_unref(frame);
86 static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
90 AVCodecContext *dec_ctx = NULL;
91 const AVCodec *dec = NULL;
92 AVDictionary *opts = NULL;
94 ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
96 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
97 av_get_media_type_string(type), src_filename);
100 int stream_idx = ret;
101 st = fmt_ctx->streams[stream_idx];
103 dec_ctx = avcodec_alloc_context3(dec);
105 fprintf(stderr, "Failed to allocate codec\n");
106 return AVERROR(EINVAL);
109 ret = avcodec_parameters_to_context(dec_ctx, st->codecpar);
111 fprintf(stderr, "Failed to copy codec parameters to codec context\n");
115 /* Init the video decoder */
116 av_dict_set(&opts, "flags2", "+export_mvs", 0);
117 ret = avcodec_open2(dec_ctx, dec, &opts);
120 fprintf(stderr, "Failed to open %s codec\n",
121 av_get_media_type_string(type));
125 video_stream_idx = stream_idx;
126 video_stream = fmt_ctx->streams[video_stream_idx];
127 video_dec_ctx = dec_ctx;
133 int main(int argc, char **argv)
136 AVPacket *pkt = NULL;
139 fprintf(stderr, "Usage: %s <video>\n", argv[0]);
142 src_filename = argv[1];
144 if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
145 fprintf(stderr, "Could not open source file %s\n", src_filename);
149 if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
150 fprintf(stderr, "Could not find stream information\n");
154 open_codec_context(fmt_ctx, AVMEDIA_TYPE_VIDEO);
156 av_dump_format(fmt_ctx, 0, src_filename, 0);
159 fprintf(stderr, "Could not find video stream in the input, aborting\n");
164 frame = av_frame_alloc();
166 fprintf(stderr, "Could not allocate frame\n");
167 ret = AVERROR(ENOMEM);
171 pkt = av_packet_alloc();
173 fprintf(stderr, "Could not allocate AVPacket\n");
174 ret = AVERROR(ENOMEM);
178 printf("framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,flags,motion_x,motion_y,motion_scale\n");
180 /* read frames from the file */
181 while (av_read_frame(fmt_ctx, pkt) >= 0) {
182 if (pkt->stream_index == video_stream_idx)
183 ret = decode_packet(pkt);
184 av_packet_unref(pkt);
189 /* flush cached frames */
193 avcodec_free_context(&video_dec_ctx);
194 avformat_close_input(&fmt_ctx);
195 av_frame_free(&frame);
196 av_packet_free(&pkt);