Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / doc / examples / extract_mvs.c
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 /**
25  * @file libavcodec motion vectors extraction API usage example
26  * @example extract_mvs.c
27  *
28  * Read from input file, decode video stream and print a motion vectors
29  * representation to stdout.
30  */
31
32 #include <libavutil/motion_vector.h>
33 #include <libavcodec/avcodec.h>
34 #include <libavformat/avformat.h>
35
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;
40
41 static int video_stream_idx = -1;
42 static AVFrame *frame = NULL;
43 static int video_frame_count = 0;
44
45 static int decode_packet(const AVPacket *pkt)
46 {
47     int ret = avcodec_send_packet(video_dec_ctx, pkt);
48     if (ret < 0) {
49         fprintf(stderr, "Error while sending a packet to the decoder: %s\n", av_err2str(ret));
50         return ret;
51     }
52
53     while (ret >= 0)  {
54         ret = avcodec_receive_frame(video_dec_ctx, frame);
55         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
56             break;
57         } else if (ret < 0) {
58             fprintf(stderr, "Error while receiving a frame from the decoder: %s\n", av_err2str(ret));
59             return ret;
60         }
61
62         if (ret >= 0) {
63             int i;
64             AVFrameSideData *sd;
65
66             video_frame_count++;
67             sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
68             if (sd) {
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);
77                 }
78             }
79             av_frame_unref(frame);
80         }
81     }
82
83     return 0;
84 }
85
86 static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
87 {
88     int ret;
89     AVStream *st;
90     AVCodecContext *dec_ctx = NULL;
91     const AVCodec *dec = NULL;
92     AVDictionary *opts = NULL;
93
94     ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
95     if (ret < 0) {
96         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
97                 av_get_media_type_string(type), src_filename);
98         return ret;
99     } else {
100         int stream_idx = ret;
101         st = fmt_ctx->streams[stream_idx];
102
103         dec_ctx = avcodec_alloc_context3(dec);
104         if (!dec_ctx) {
105             fprintf(stderr, "Failed to allocate codec\n");
106             return AVERROR(EINVAL);
107         }
108
109         ret = avcodec_parameters_to_context(dec_ctx, st->codecpar);
110         if (ret < 0) {
111             fprintf(stderr, "Failed to copy codec parameters to codec context\n");
112             return ret;
113         }
114
115         /* Init the video decoder */
116         av_dict_set(&opts, "flags2", "+export_mvs", 0);
117         ret = avcodec_open2(dec_ctx, dec, &opts);
118         av_dict_free(&opts);
119         if (ret < 0) {
120             fprintf(stderr, "Failed to open %s codec\n",
121                     av_get_media_type_string(type));
122             return ret;
123         }
124
125         video_stream_idx = stream_idx;
126         video_stream = fmt_ctx->streams[video_stream_idx];
127         video_dec_ctx = dec_ctx;
128     }
129
130     return 0;
131 }
132
133 int main(int argc, char **argv)
134 {
135     int ret = 0;
136     AVPacket *pkt = NULL;
137
138     if (argc != 2) {
139         fprintf(stderr, "Usage: %s <video>\n", argv[0]);
140         exit(1);
141     }
142     src_filename = argv[1];
143
144     if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
145         fprintf(stderr, "Could not open source file %s\n", src_filename);
146         exit(1);
147     }
148
149     if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
150         fprintf(stderr, "Could not find stream information\n");
151         exit(1);
152     }
153
154     open_codec_context(fmt_ctx, AVMEDIA_TYPE_VIDEO);
155
156     av_dump_format(fmt_ctx, 0, src_filename, 0);
157
158     if (!video_stream) {
159         fprintf(stderr, "Could not find video stream in the input, aborting\n");
160         ret = 1;
161         goto end;
162     }
163
164     frame = av_frame_alloc();
165     if (!frame) {
166         fprintf(stderr, "Could not allocate frame\n");
167         ret = AVERROR(ENOMEM);
168         goto end;
169     }
170
171     pkt = av_packet_alloc();
172     if (!pkt) {
173         fprintf(stderr, "Could not allocate AVPacket\n");
174         ret = AVERROR(ENOMEM);
175         goto end;
176     }
177
178     printf("framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,flags,motion_x,motion_y,motion_scale\n");
179
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);
185         if (ret < 0)
186             break;
187     }
188
189     /* flush cached frames */
190     decode_packet(NULL);
191
192 end:
193     avcodec_free_context(&video_dec_ctx);
194     avformat_close_input(&fmt_ctx);
195     av_frame_free(&frame);
196     av_packet_free(&pkt);
197     return ret < 0;
198 }