FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 <libavformat/avformat.h>
26 
30 static const char *src_filename = NULL;
31 
32 static int video_stream_idx = -1;
33 static AVFrame *frame = NULL;
34 static AVPacket pkt;
35 static int video_frame_count = 0;
36 
37 static int decode_packet(int *got_frame, int cached)
38 {
39  int decoded = pkt.size;
40 
41  *got_frame = 0;
42 
43  if (pkt.stream_index == video_stream_idx) {
44  int ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
45  if (ret < 0) {
46  fprintf(stderr, "Error decoding video frame (%s)\n", av_err2str(ret));
47  return ret;
48  }
49 
50  if (*got_frame) {
51  int i;
52  AVFrameSideData *sd;
53 
56  if (sd) {
57  const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
58  for (i = 0; i < sd->size / sizeof(*mvs); i++) {
59  const AVMotionVector *mv = &mvs[i];
60  printf("%d,%2d,%2d,%2d,%4d,%4d,%4d,%4d,0x%"PRIx64"\n",
62  mv->w, mv->h, mv->src_x, mv->src_y,
63  mv->dst_x, mv->dst_y, mv->flags);
64  }
65  }
66  }
67  }
68 
69  return decoded;
70 }
71 
73 {
74  int ret;
75  AVStream *st;
77  AVCodec *dec = NULL;
79 
80  ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
81  if (ret < 0) {
82  fprintf(stderr, "Could not find %s stream in input file '%s'\n",
84  return ret;
85  } else {
86  int stream_idx = ret;
87  st = fmt_ctx->streams[stream_idx];
88 
89  dec_ctx = avcodec_alloc_context3(dec);
90  if (!dec_ctx) {
91  fprintf(stderr, "Failed to allocate codec\n");
92  return AVERROR(EINVAL);
93  }
94 
95  ret = avcodec_parameters_to_context(dec_ctx, st->codecpar);
96  if (ret < 0) {
97  fprintf(stderr, "Failed to copy codec parameters to codec context\n");
98  return ret;
99  }
100 
101  /* Init the video decoder */
102  av_dict_set(&opts, "flags2", "+export_mvs", 0);
103  if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) {
104  fprintf(stderr, "Failed to open %s codec\n",
106  return ret;
107  }
108 
109  video_stream_idx = stream_idx;
110  video_stream = fmt_ctx->streams[video_stream_idx];
111  video_dec_ctx = dec_ctx;
112  }
113 
114  return 0;
115 }
116 
117 int main(int argc, char **argv)
118 {
119  int ret = 0, got_frame;
120 
121  if (argc != 2) {
122  fprintf(stderr, "Usage: %s <video>\n", argv[0]);
123  exit(1);
124  }
125  src_filename = argv[1];
126 
127  av_register_all();
128 
129  if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
130  fprintf(stderr, "Could not open source file %s\n", src_filename);
131  exit(1);
132  }
133 
134  if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
135  fprintf(stderr, "Could not find stream information\n");
136  exit(1);
137  }
138 
140 
141  av_dump_format(fmt_ctx, 0, src_filename, 0);
142 
143  if (!video_stream) {
144  fprintf(stderr, "Could not find video stream in the input, aborting\n");
145  ret = 1;
146  goto end;
147  }
148 
149  frame = av_frame_alloc();
150  if (!frame) {
151  fprintf(stderr, "Could not allocate frame\n");
152  ret = AVERROR(ENOMEM);
153  goto end;
154  }
155 
156  printf("framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,flags\n");
157 
158  /* initialize packet, set data to NULL, let the demuxer fill it */
159  av_init_packet(&pkt);
160  pkt.data = NULL;
161  pkt.size = 0;
162 
163  /* read frames from the file */
164  while (av_read_frame(fmt_ctx, &pkt) >= 0) {
165  AVPacket orig_pkt = pkt;
166  do {
167  ret = decode_packet(&got_frame, 0);
168  if (ret < 0)
169  break;
170  pkt.data += ret;
171  pkt.size -= ret;
172  } while (pkt.size > 0);
173  av_packet_unref(&orig_pkt);
174  }
175 
176  /* flush cached frames */
177  pkt.data = NULL;
178  pkt.size = 0;
179  do {
180  decode_packet(&got_frame, 1);
181  } while (got_frame);
182 
183 end:
184  avcodec_free_context(&video_dec_ctx);
185  avformat_close_input(&fmt_ctx);
186  av_frame_free(&frame);
187  return ret < 0;
188 }
#define NULL
Definition: coverity.c:32
static AVFrame * frame
Definition: extract_mvs.c:33
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
int16_t src_x
Absolute source position.
Definition: motion_vector.h:38
int main(int argc, char **argv)
Definition: extract_mvs.c:117
int size
Definition: avcodec.h:1658
static int video_stream_idx
Definition: extract_mvs.c:32
AVCodec.
Definition: avcodec.h:3681
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:675
Format I/O context.
Definition: avformat.h:1349
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int16_t dst_x
Absolute destination position.
Definition: motion_vector.h:42
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1417
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:4264
Structure to hold side data for an AVFrame.
Definition: frame.h:149
uint8_t * data
Definition: avcodec.h:1657
int32_t source
Where the current macroblock comes from; negative value when it comes from the past, positive value when it comes from the future.
Definition: motion_vector.h:30
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, streams, container, programs, metadata, side data, codec and time base.
Definition: dump.c:544
int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream, AVCodec **decoder_ret, int flags)
Find the "best" stream in the file.
Definition: utils.c:3973
static AVStream * video_stream
Definition: extract_mvs.c:29
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
uint8_t w
Width and height of the block.
Definition: motion_vector.h:34
static int video_frame_count
Definition: extract_mvs.c:35
static AVFormatContext * fmt_ctx
Definition: extract_mvs.c:27
static int decode_packet(int *got_frame, int cached)
Definition: extract_mvs.c:37
static AVCodecContext * video_dec_ctx
Definition: extract_mvs.c:28
AVDictionary * opts
Definition: movenc.c:50
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:157
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
Motion vectors exported by some codecs (on demand through the export_mvs flag set in the libavcodec A...
Definition: frame.h:95
Stream structure.
Definition: avformat.h:889
static const int8_t mv[256][2]
Definition: 4xm.c:77
attribute_deprecated int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt)
Decode the video frame of size avpkt->size from avpkt->data into picture.
Definition: utils.c:2227
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer...
Definition: options.c:172
main external API structure.
Definition: avcodec.h:1732
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:589
uint8_t * data
Definition: frame.h:151
GLint GLenum type
Definition: opengl_enc.c:105
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
AVMediaType
Definition: avutil.h:199
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:1242
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1710
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.
Definition: utils.c:79
Main libavformat public API header.
uint64_t flags
Extra flag information.
Definition: motion_vector.h:47
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:3374
static AVCodecContext * dec_ctx
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:33
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:4203
static AVPacket pkt
Definition: extract_mvs.c:34
static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
Definition: extract_mvs.c:72
int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:510
static const char * src_filename
Definition: extract_mvs.c:30
AVCodecParameters * codecpar
Definition: avformat.h:1252
int stream_index
Definition: avcodec.h:1659
This structure stores compressed data.
Definition: avcodec.h:1634
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:385
for(j=16;j >0;--j)