FFmpeg 7.1.1
Loading...
Searching...
No Matches
extract_mvs.c
Go to the documentation of this file.
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
33#include <libavcodec/avcodec.h>
35
36static AVFormatContext *fmt_ctx = NULL;
38static AVStream *video_stream = NULL;
39static const char *src_filename = NULL;
40
41static int video_stream_idx = -1;
42static AVFrame *frame = NULL;
43static int video_frame_count = 0;
44
45static int decode_packet(const AVPacket *pkt)
46{
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) {
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;
65
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",
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 }
80 }
81 }
82
83 return 0;
84}
85
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",
98 return ret;
99 } else {
100 int stream_idx = ret;
101 st = fmt_ctx->streams[stream_idx];
102
104 if (!dec_ctx) {
105 fprintf(stderr, "Failed to allocate codec\n");
106 return AVERROR(EINVAL);
107 }
108
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",
122 return ret;
123 }
124
125 video_stream_idx = stream_idx;
128 }
129
130 return 0;
131}
132
133int 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
155
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
165 if (!frame) {
166 fprintf(stderr, "Could not allocate frame\n");
167 ret = AVERROR(ENOMEM);
168 goto end;
169 }
170
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) {
183 ret = decode_packet(pkt);
185 if (ret < 0)
186 break;
187 }
188
189 /* flush cached frames */
190 decode_packet(NULL);
191
192end:
197 return ret < 0;
198}
Libavcodec external API header.
Main libavformat public API header.
int main(int argc, char *argv[])
static AVFormatContext * fmt_ctx
static AVCodecContext * dec_ctx
static AVCodecContext * video_dec_ctx
static AVPacket * pkt
static AVStream * video_stream
static int decode_packet(AVCodecContext *dec, const AVPacket *pkt)
static int open_codec_context(int *stream_idx, AVCodecContext **dec_ctx, AVFormatContext *fmt_ctx, enum AVMediaType type)
static int video_frame_count
static const char * src_filename
static AVFrame * frame
static int video_stream_idx
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
int avcodec_parameters_to_context(AVCodecContext *codec, const struct AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream, const struct AVCodec **decoder_ret, int flags)
Find the "best" stream in the file.
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)
Print detailed information about the input or output format, such as duration, bitrate,...
void av_dict_free(AVDictionary **m)
Free all the memory allocated for an AVDictionary struct and all keys and values.
struct AVDictionary AVDictionary
Definition dict.h:94
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
@ AV_FRAME_DATA_MOTION_VECTORS
Motion vectors exported by some codecs (on demand through the export_mvs flag set in the libavcodec A...
Definition frame.h:97
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
AVMediaType
Definition avutil.h:199
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:201
main external API structure.
Definition avcodec.h:451
AVCodec.
Definition codec.h:187
Format I/O context.
Definition avformat.h:1287
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1355
Structure to hold side data for an AVFrame.
Definition frame.h:265
size_t size
Definition frame.h:268
uint8_t * data
Definition frame.h:267
This structure describes decoded (raw) audio or video data.
Definition frame.h:389
int32_t source
Where the current macroblock comes from; negative value when it comes from the past,...
int16_t dst_x
Absolute destination position.
uint64_t flags
Extra flag information.
uint16_t motion_scale
int32_t motion_x
Motion vector src_x = dst_x + motion_x / motion_scale src_y = dst_y + motion_y / motion_scale.
uint8_t w
Width and height of the block.
int16_t src_x
Absolute source position.
This structure stores compressed data.
Definition packet.h:516
int stream_index
Definition packet.h:541
Stream structure.
Definition avformat.h:748
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:771