2014-07-16 14:42:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Stefano Sabatini
|
|
|
|
* Copyright (c) 2014 Clément Bœsch
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2023-01-15 01:57:14 +00:00
|
|
|
/**
|
|
|
|
* @file libavcodec motion vectors extraction API usage example
|
|
|
|
* @example extract_mvs.c
|
|
|
|
*
|
|
|
|
* Read from input file, decode video stream and print a motion vectors
|
|
|
|
* representation to stdout.
|
|
|
|
*/
|
|
|
|
|
2014-07-16 14:42:42 +00:00
|
|
|
#include <libavutil/motion_vector.h>
|
2021-06-11 00:19:28 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
2014-07-16 14:42:42 +00:00
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
|
|
|
static AVFormatContext *fmt_ctx = NULL;
|
|
|
|
static AVCodecContext *video_dec_ctx = NULL;
|
|
|
|
static AVStream *video_stream = NULL;
|
|
|
|
static const char *src_filename = NULL;
|
|
|
|
|
|
|
|
static int video_stream_idx = -1;
|
|
|
|
static AVFrame *frame = NULL;
|
|
|
|
static int video_frame_count = 0;
|
|
|
|
|
2017-04-03 14:15:58 +00:00
|
|
|
static int decode_packet(const AVPacket *pkt)
|
2014-07-16 14:42:42 +00:00
|
|
|
{
|
2017-04-03 14:32:50 +00:00
|
|
|
int ret = avcodec_send_packet(video_dec_ctx, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error while sending a packet to the decoder: %s\n", av_err2str(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (ret >= 0) {
|
|
|
|
ret = avcodec_receive_frame(video_dec_ctx, frame);
|
|
|
|
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
|
|
|
|
break;
|
|
|
|
} else if (ret < 0) {
|
|
|
|
fprintf(stderr, "Error while receiving a frame from the decoder: %s\n", av_err2str(ret));
|
2014-07-16 14:42:42 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-03 14:32:50 +00:00
|
|
|
if (ret >= 0) {
|
|
|
|
int i;
|
|
|
|
AVFrameSideData *sd;
|
2017-04-03 13:25:09 +00:00
|
|
|
|
2017-04-03 14:32:50 +00:00
|
|
|
video_frame_count++;
|
|
|
|
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
|
|
|
|
if (sd) {
|
|
|
|
const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
|
|
|
|
for (i = 0; i < sd->size / sizeof(*mvs); i++) {
|
|
|
|
const AVMotionVector *mv = &mvs[i];
|
doc/examples/extract_mvs: add motion information
Note that the motion information includes subpel motion information
This was likely forgotten in 56bdf61baa04c4fd8d165f34499115ce0aa97c43.
Tested:
```
$ make examples -j
...
$ doc/examples/extract_mvs in.264 | head -40 | \
csvcut -C framenum,source,flags |csvlook
| blockw | blockh | srcx | srcy | dstx | dsty | motion_x | motion_y | motion_scale |
| ------ | ------ | ----- | ---- | ----- | ---- | -------- | -------- | ------------ |
| 16 | 16 | 20 | 26 | 8 | 8 | 49 | 72 | 4 |
| 16 | 16 | 152 | 15 | 136 | 8 | 65 | 28 | 4 |
| 16 | 8 | 360 | 3 | 360 | 4 | 1 | -6 | 4 |
| 16 | 8 | 360 | 13 | 360 | 12 | -1 | 4 | 4 |
| 16 | 16 | 440 | 10 | 440 | 8 | 3 | 10 | 4 |
| 8 | 16 | 829 | 7 | 836 | 8 | -31 | -6 | 4 |
| 8 | 16 | 844 | 7 | 844 | 8 | -1 | -4 | 4 |
| 16 | 16 | 1,004 | 14 | 1,048 | 8 | -177 | 24 | 4 |
| 16 | 16 | 1,096 | 8 | 1,096 | 8 | -1 | 0 | 4 |
| 16 | 8 | 1,417 | 24 | 1,416 | 4 | 7 | 82 | 4 |
| 16 | 8 | 1,416 | 13 | 1,416 | 12 | 0 | 6 | 4 |
| 16 | 8 | 87 | 20 | 88 | 20 | -7 | 0 | 4 |
| 16 | 8 | 99 | 44 | 88 | 28 | 45 | 66 | 4 |
...
```
Also:
```
$ make fate -j
...
```
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2022-09-12 23:49:56 +00:00
|
|
|
printf("%d,%2d,%2d,%2d,%4d,%4d,%4d,%4d,0x%"PRIx64",%4d,%4d,%4d\n",
|
2017-04-03 14:32:50 +00:00
|
|
|
video_frame_count, mv->source,
|
|
|
|
mv->w, mv->h, mv->src_x, mv->src_y,
|
doc/examples/extract_mvs: add motion information
Note that the motion information includes subpel motion information
This was likely forgotten in 56bdf61baa04c4fd8d165f34499115ce0aa97c43.
Tested:
```
$ make examples -j
...
$ doc/examples/extract_mvs in.264 | head -40 | \
csvcut -C framenum,source,flags |csvlook
| blockw | blockh | srcx | srcy | dstx | dsty | motion_x | motion_y | motion_scale |
| ------ | ------ | ----- | ---- | ----- | ---- | -------- | -------- | ------------ |
| 16 | 16 | 20 | 26 | 8 | 8 | 49 | 72 | 4 |
| 16 | 16 | 152 | 15 | 136 | 8 | 65 | 28 | 4 |
| 16 | 8 | 360 | 3 | 360 | 4 | 1 | -6 | 4 |
| 16 | 8 | 360 | 13 | 360 | 12 | -1 | 4 | 4 |
| 16 | 16 | 440 | 10 | 440 | 8 | 3 | 10 | 4 |
| 8 | 16 | 829 | 7 | 836 | 8 | -31 | -6 | 4 |
| 8 | 16 | 844 | 7 | 844 | 8 | -1 | -4 | 4 |
| 16 | 16 | 1,004 | 14 | 1,048 | 8 | -177 | 24 | 4 |
| 16 | 16 | 1,096 | 8 | 1,096 | 8 | -1 | 0 | 4 |
| 16 | 8 | 1,417 | 24 | 1,416 | 4 | 7 | 82 | 4 |
| 16 | 8 | 1,416 | 13 | 1,416 | 12 | 0 | 6 | 4 |
| 16 | 8 | 87 | 20 | 88 | 20 | -7 | 0 | 4 |
| 16 | 8 | 99 | 44 | 88 | 28 | 45 | 66 | 4 |
...
```
Also:
```
$ make fate -j
...
```
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2022-09-12 23:49:56 +00:00
|
|
|
mv->dst_x, mv->dst_y, mv->flags,
|
|
|
|
mv->motion_x, mv->motion_y, mv->motion_scale);
|
2014-07-16 14:42:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-03 14:32:50 +00:00
|
|
|
av_frame_unref(frame);
|
2014-07-16 14:42:42 +00:00
|
|
|
}
|
2017-04-03 14:32:50 +00:00
|
|
|
}
|
2014-07-16 14:42:42 +00:00
|
|
|
|
2017-04-03 13:25:09 +00:00
|
|
|
return 0;
|
2014-07-16 14:42:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 11:46:36 +00:00
|
|
|
static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
|
2014-07-16 14:42:42 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
AVStream *st;
|
|
|
|
AVCodecContext *dec_ctx = NULL;
|
2021-02-25 00:53:40 +00:00
|
|
|
const AVCodec *dec = NULL;
|
2014-07-16 14:42:42 +00:00
|
|
|
AVDictionary *opts = NULL;
|
|
|
|
|
2017-03-28 11:46:36 +00:00
|
|
|
ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
|
2014-07-16 14:42:42 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Could not find %s stream in input file '%s'\n",
|
|
|
|
av_get_media_type_string(type), src_filename);
|
|
|
|
return ret;
|
|
|
|
} else {
|
2017-03-28 11:46:36 +00:00
|
|
|
int stream_idx = ret;
|
|
|
|
st = fmt_ctx->streams[stream_idx];
|
|
|
|
|
|
|
|
dec_ctx = avcodec_alloc_context3(dec);
|
|
|
|
if (!dec_ctx) {
|
|
|
|
fprintf(stderr, "Failed to allocate codec\n");
|
2014-07-16 14:42:42 +00:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2017-03-28 11:46:36 +00:00
|
|
|
ret = avcodec_parameters_to_context(dec_ctx, st->codecpar);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to copy codec parameters to codec context\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:42:42 +00:00
|
|
|
/* Init the video decoder */
|
|
|
|
av_dict_set(&opts, "flags2", "+export_mvs", 0);
|
2021-05-15 06:27:45 +00:00
|
|
|
ret = avcodec_open2(dec_ctx, dec, &opts);
|
|
|
|
av_dict_free(&opts);
|
|
|
|
if (ret < 0) {
|
2014-07-16 14:42:42 +00:00
|
|
|
fprintf(stderr, "Failed to open %s codec\n",
|
|
|
|
av_get_media_type_string(type));
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-28 11:46:36 +00:00
|
|
|
|
|
|
|
video_stream_idx = stream_idx;
|
|
|
|
video_stream = fmt_ctx->streams[video_stream_idx];
|
|
|
|
video_dec_ctx = dec_ctx;
|
2014-07-16 14:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2017-04-03 13:25:09 +00:00
|
|
|
int ret = 0;
|
2021-09-03 21:04:50 +00:00
|
|
|
AVPacket *pkt = NULL;
|
2014-07-16 14:42:42 +00:00
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s <video>\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
src_filename = argv[1];
|
|
|
|
|
|
|
|
if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
|
|
|
|
fprintf(stderr, "Could not open source file %s\n", src_filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
|
|
|
|
fprintf(stderr, "Could not find stream information\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-03-28 11:46:36 +00:00
|
|
|
open_codec_context(fmt_ctx, AVMEDIA_TYPE_VIDEO);
|
2014-07-16 14:42:42 +00:00
|
|
|
|
|
|
|
av_dump_format(fmt_ctx, 0, src_filename, 0);
|
|
|
|
|
|
|
|
if (!video_stream) {
|
|
|
|
fprintf(stderr, "Could not find video stream in the input, aborting\n");
|
|
|
|
ret = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame = av_frame_alloc();
|
|
|
|
if (!frame) {
|
|
|
|
fprintf(stderr, "Could not allocate frame\n");
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2021-09-03 21:04:50 +00:00
|
|
|
pkt = av_packet_alloc();
|
|
|
|
if (!pkt) {
|
|
|
|
fprintf(stderr, "Could not allocate AVPacket\n");
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
doc/examples/extract_mvs: add motion information
Note that the motion information includes subpel motion information
This was likely forgotten in 56bdf61baa04c4fd8d165f34499115ce0aa97c43.
Tested:
```
$ make examples -j
...
$ doc/examples/extract_mvs in.264 | head -40 | \
csvcut -C framenum,source,flags |csvlook
| blockw | blockh | srcx | srcy | dstx | dsty | motion_x | motion_y | motion_scale |
| ------ | ------ | ----- | ---- | ----- | ---- | -------- | -------- | ------------ |
| 16 | 16 | 20 | 26 | 8 | 8 | 49 | 72 | 4 |
| 16 | 16 | 152 | 15 | 136 | 8 | 65 | 28 | 4 |
| 16 | 8 | 360 | 3 | 360 | 4 | 1 | -6 | 4 |
| 16 | 8 | 360 | 13 | 360 | 12 | -1 | 4 | 4 |
| 16 | 16 | 440 | 10 | 440 | 8 | 3 | 10 | 4 |
| 8 | 16 | 829 | 7 | 836 | 8 | -31 | -6 | 4 |
| 8 | 16 | 844 | 7 | 844 | 8 | -1 | -4 | 4 |
| 16 | 16 | 1,004 | 14 | 1,048 | 8 | -177 | 24 | 4 |
| 16 | 16 | 1,096 | 8 | 1,096 | 8 | -1 | 0 | 4 |
| 16 | 8 | 1,417 | 24 | 1,416 | 4 | 7 | 82 | 4 |
| 16 | 8 | 1,416 | 13 | 1,416 | 12 | 0 | 6 | 4 |
| 16 | 8 | 87 | 20 | 88 | 20 | -7 | 0 | 4 |
| 16 | 8 | 99 | 44 | 88 | 28 | 45 | 66 | 4 |
...
```
Also:
```
$ make fate -j
...
```
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2022-09-12 23:49:56 +00:00
|
|
|
printf("framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,flags,motion_x,motion_y,motion_scale\n");
|
2014-07-16 14:42:42 +00:00
|
|
|
|
|
|
|
/* read frames from the file */
|
2021-09-03 21:04:50 +00:00
|
|
|
while (av_read_frame(fmt_ctx, pkt) >= 0) {
|
|
|
|
if (pkt->stream_index == video_stream_idx)
|
|
|
|
ret = decode_packet(pkt);
|
|
|
|
av_packet_unref(pkt);
|
2017-04-03 13:25:09 +00:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
2014-07-16 14:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* flush cached frames */
|
2017-04-03 14:15:58 +00:00
|
|
|
decode_packet(NULL);
|
2014-07-16 14:42:42 +00:00
|
|
|
|
|
|
|
end:
|
2017-03-28 11:46:36 +00:00
|
|
|
avcodec_free_context(&video_dec_ctx);
|
2014-07-16 14:42:42 +00:00
|
|
|
avformat_close_input(&fmt_ctx);
|
|
|
|
av_frame_free(&frame);
|
2021-09-03 21:04:50 +00:00
|
|
|
av_packet_free(&pkt);
|
2014-07-16 14:42:42 +00:00
|
|
|
return ret < 0;
|
|
|
|
}
|