FFmpeg  5.1
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 
25 #include <libavcodec/avcodec.h>
26 #include <libavformat/avformat.h>
27 
28 static AVFormatContext *fmt_ctx = NULL;
30 static AVStream *video_stream = NULL;
31 static const char *src_filename = NULL;
32 
33 static int video_stream_idx = -1;
34 static AVFrame *frame = NULL;
35 static int video_frame_count = 0;
36 
37 static int decode_packet(const AVPacket *pkt)
38 {
40  if (ret < 0) {
41  fprintf(stderr, "Error while sending a packet to the decoder: %s\n", av_err2str(ret));
42  return ret;
43  }
44 
45  while (ret >= 0) {
47  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
48  break;
49  } else if (ret < 0) {
50  fprintf(stderr, "Error while receiving a frame from the decoder: %s\n", av_err2str(ret));
51  return ret;
52  }
53 
54  if (ret >= 0) {
55  int i;
56  AVFrameSideData *sd;
57 
60  if (sd) {
61  const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
62  for (i = 0; i < sd->size / sizeof(*mvs); i++) {
63  const AVMotionVector *mv = &mvs[i];
64  printf("%d,%2d,%2d,%2d,%4d,%4d,%4d,%4d,0x%"PRIx64"\n",
66  mv->w, mv->h, mv->src_x, mv->src_y,
67  mv->dst_x, mv->dst_y, mv->flags);
68  }
69  }
71  }
72  }
73 
74  return 0;
75 }
76 
78 {
79  int ret;
80  AVStream *st;
81  AVCodecContext *dec_ctx = NULL;
82  const AVCodec *dec = NULL;
83  AVDictionary *opts = NULL;
84 
85  ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
86  if (ret < 0) {
87  fprintf(stderr, "Could not find %s stream in input file '%s'\n",
89  return ret;
90  } else {
91  int stream_idx = ret;
92  st = fmt_ctx->streams[stream_idx];
93 
95  if (!dec_ctx) {
96  fprintf(stderr, "Failed to allocate codec\n");
97  return AVERROR(EINVAL);
98  }
99 
101  if (ret < 0) {
102  fprintf(stderr, "Failed to copy codec parameters to codec context\n");
103  return ret;
104  }
105 
106  /* Init the video decoder */
107  av_dict_set(&opts, "flags2", "+export_mvs", 0);
108  ret = avcodec_open2(dec_ctx, dec, &opts);
109  av_dict_free(&opts);
110  if (ret < 0) {
111  fprintf(stderr, "Failed to open %s codec\n",
113  return ret;
114  }
115 
116  video_stream_idx = stream_idx;
119  }
120 
121  return 0;
122 }
123 
124 int main(int argc, char **argv)
125 {
126  int ret = 0;
127  AVPacket *pkt = NULL;
128 
129  if (argc != 2) {
130  fprintf(stderr, "Usage: %s <video>\n", argv[0]);
131  exit(1);
132  }
133  src_filename = argv[1];
134 
135  if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
136  fprintf(stderr, "Could not open source file %s\n", src_filename);
137  exit(1);
138  }
139 
140  if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
141  fprintf(stderr, "Could not find stream information\n");
142  exit(1);
143  }
144 
146 
148 
149  if (!video_stream) {
150  fprintf(stderr, "Could not find video stream in the input, aborting\n");
151  ret = 1;
152  goto end;
153  }
154 
155  frame = av_frame_alloc();
156  if (!frame) {
157  fprintf(stderr, "Could not allocate frame\n");
158  ret = AVERROR(ENOMEM);
159  goto end;
160  }
161 
162  pkt = av_packet_alloc();
163  if (!pkt) {
164  fprintf(stderr, "Could not allocate AVPacket\n");
165  ret = AVERROR(ENOMEM);
166  goto end;
167  }
168 
169  printf("framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,flags\n");
170 
171  /* read frames from the file */
172  while (av_read_frame(fmt_ctx, pkt) >= 0) {
174  ret = decode_packet(pkt);
176  if (ret < 0)
177  break;
178  }
179 
180  /* flush cached frames */
181  decode_packet(NULL);
182 
183 end:
188  return ret < 0;
189 }
Libavcodec external API header.
Main libavformat public API header.
static AVPacket * pkt
static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
Definition: extract_mvs.c:77
static AVCodecContext * video_dec_ctx
Definition: extract_mvs.c:29
static int decode_packet(const AVPacket *pkt)
Definition: extract_mvs.c:37
int main(int argc, char **argv)
Definition: extract_mvs.c:124
static AVFormatContext * fmt_ctx
Definition: extract_mvs.c:28
static AVStream * video_stream
Definition: extract_mvs.c:30
static int video_frame_count
Definition: extract_mvs.c:35
static const char * src_filename
Definition: extract_mvs.c:31
static AVFrame * frame
Definition: extract_mvs.c:34
static int video_stream_idx
Definition: extract_mvs.c:33
static AVCodecContext * dec_ctx
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
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.
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 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:84
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:121
#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.
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.
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
@ 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
AVMediaType
Definition: avutil.h:199
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.
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
main external API structure.
Definition: avcodec.h:389
AVCodec.
Definition: codec.h:196
Format I/O context.
Definition: avformat.h:1213
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1281
Structure to hold side data for an AVFrame.
Definition: frame.h:231
size_t size
Definition: frame.h:234
uint8_t * data
Definition: frame.h:233
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
int32_t source
Where the current macroblock comes from; negative value when it comes from the past,...
Definition: motion_vector.h:30
int16_t dst_x
Absolute destination position.
Definition: motion_vector.h:42
uint64_t flags
Extra flag information.
Definition: motion_vector.h:47
uint8_t w
Width and height of the block.
Definition: motion_vector.h:34
int16_t src_x
Absolute source position.
Definition: motion_vector.h:38
This structure stores compressed data.
Definition: packet.h:351
int stream_index
Definition: packet.h:376
Stream structure.
Definition: avformat.h:948
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108