FFmpeg 提取运动矢量表extract_mvs方法

2023-11-09 05:32

本文主要是介绍FFmpeg 提取运动矢量表extract_mvs方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述
FFmpeg提供了获取编码的运动矢量的方法。

打开解码器的时候设置参数:av_dict_set(&opts, “flags2”, “+export_mvs”, 0)。
使用av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS)来获取解码frame中的运动矢量。
av_frame_get_side_data返回的数据类型为AVFrameSideData*,AVFrameSideData定义在libavutil/frame.h,如下所示。

/*** Structure to hold side data for an AVFrame.** sizeof(AVFrameSideData) is not a part of the public ABI, so new fields may be added* to the end with a minor bump.*/
typedef struct AVFrameSideData {enum AVFrameSideDataType type;uint8_t *data;int      size;AVDictionary *metadata;AVBufferRef *buf;
} AVFrameSideData;

AVFrameSideDataType:表示数据的类型,用来存储运动矢量数据时,AVFrameSideDataType 为AV_FRAME_DATA_MOTION_VECTORS。
data:指向数据buffer的指针,AVFrameSideDataType 为AV_FRAME_DATA_MOTION_VECTORS,data指向的地址存储的是AVMotionVector类型的数据。
size:data指向的数据buffer的大小。
AVMotionVector是表示运动矢量的数据结构,定义在libavutil/motion_vector.h,如下所示:

typedef struct AVMotionVector {/*** Where the current macroblock comes from; negative value when it comes* from the past, positive value when it comes from the future.* XXX: set exact relative ref frame reference instead of a +/- 1 "direction".*/int32_t source;/*** Width and height of the block.*/uint8_t w, h;/*** Absolute source position. Can be outside the frame area.*/int16_t src_x, src_y;/*** Absolute destination position. Can be outside the frame area.*/int16_t dst_x, dst_y;/*** Extra flag information.* Currently unused.*/uint64_t flags;/*** Motion vector* src_x = dst_x + motion_x / motion_scale* src_y = dst_y + motion_y / motion_scale*/int32_t motion_x, motion_y;uint16_t motion_scale;
} AVMotionVector;

参数说明:
int32_t source:当前像素参考的帧来源,负值表示时参考过去的帧,正值表示参考未来的帧。
uint8_t w, h:block的宽和高。
int16_t src_x, src_y:源的绝对位置。可能在frame之外。
int16_t dst_x, dst_y:目的的绝对位置。可能在frame之外。
uint16_t motion_scale:运动矢量的像素精度,4则表示1/4像素。
int32_t motion_x, motion_y: 运动的矢量。满足下面的等式:

src_x = dst_x + motion_x / motion_scale
src_y = dst_y + motion_y / motion_scale

示例代码:

/** 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.*/#include <libavutil/motion_vector.h>
#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;static int decode_packet(const AVPacket *pkt)
{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));return ret;}if (ret >= 0) {int i;AVFrameSideData *sd;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];printf("%d,%2d,%2d,%2d,%4d,%4d,%4d,%4d,%d,0x%"PRIx64"\n",video_frame_count, mv->source,mv->w, mv->h, mv->src_x, mv->src_y,mv->dst_x, mv->dst_y, mv->motion_scale, mv->flags);}}av_frame_unref(frame);}}return 0;
}static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
{int ret;AVStream *st;AVCodecContext *dec_ctx = NULL;AVCodec *dec = NULL;AVDictionary *opts = NULL;ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);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 {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");return AVERROR(EINVAL);}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;}/* Init the video decoder */av_dict_set(&opts, "flags2", "+export_mvs", 0);if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) {fprintf(stderr, "Failed to open %s codec\n",av_get_media_type_string(type));return ret;}video_stream_idx = stream_idx;video_stream = fmt_ctx->streams[video_stream_idx];video_dec_ctx = dec_ctx;}return 0;
}int main(int argc, char **argv)
{int ret = 0;AVPacket pkt = { 0 };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);}open_codec_context(fmt_ctx, AVMEDIA_TYPE_VIDEO);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;}printf("framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,motion_scale,flags\n");/* read frames from the file */while (av_read_frame(fmt_ctx, &pkt) >= 0) {if (pkt.stream_index == video_stream_idx)ret = decode_packet(&pkt);av_packet_unref(&pkt);if (ret < 0)break;}/* flush cached frames */decode_packet(NULL);end:avcodec_free_context(&video_dec_ctx);avformat_close_input(&fmt_ctx);av_frame_free(&frame);return ret < 0;
}

部分结果如下所示:

framenum,source,blockw,blockh,srcx,srcy,dstx,dsty,motion_scale,flags
2,-1,16,16,   8,   8,   8,   8,4,0x0
2, 1,16,16,   8,   8,   8,   8,4,0x0
2,-1,16,16,  24,   8,  24,   8,4,0x0
2, 1,16,16,  24,   8,  24,   8,4,0x0
2,-1,16,16,  40,   8,  40,   8,4,0x0
2, 1,16,16,  40,   8,  40,   8,4,0x0
2,-1,16,16,  56,   8,  56,   8,4,0x0
2, 1,16,16,  56,   8,  56,   8,4,0x0
2,-1,16,16,  72,   8,  72,   8,4,0x0
2, 1,16,16,  72,   8,  72,   8,4,0x0
2,-1,16,16,  88,   8,  88,   8,4,0x0
2, 1,16,16,  88,   8,  88,   8,4,0x0
2,-1,16,16, 104,   8, 104,   8,4,0x0
2, 1,16,16, 104,   8, 104,   8,4,0x0
2,-1,16,16, 120,   8, 120,   8,4,0x0
2, 1,16,16, 120,   8, 120,   8,4,0x0
2,-1,16,16, 136,   8, 136,   8,4,0x0

这篇关于FFmpeg 提取运动矢量表extract_mvs方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/374235

相关文章

Springboot控制反转与Bean对象的方法

《Springboot控制反转与Bean对象的方法》文章介绍了SpringBoot中的控制反转(IoC)概念,描述了IoC容器如何管理Bean的生命周期和依赖关系,它详细讲解了Bean的注册过程,包括... 目录1 控制反转1.1 什么是控制反转1.2 SpringBoot中的控制反转2 Ioc容器对Bea

C++实现回文串判断的两种高效方法

《C++实现回文串判断的两种高效方法》文章介绍了两种判断回文串的方法:解法一通过创建新字符串来处理,解法二在原字符串上直接筛选判断,两种方法都使用了双指针法,文中通过代码示例讲解的非常详细,需要的朋友... 目录一、问题描述示例二、解法一:将字母数字连接到新的 string思路代码实现代码解释复杂度分析三、

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

Spring Boot 中正确地在异步线程中使用 HttpServletRequest的方法

《SpringBoot中正确地在异步线程中使用HttpServletRequest的方法》文章讨论了在SpringBoot中如何在异步线程中正确使用HttpServletRequest的问题,... 目录前言一、问题的来源:为什么异步线程中无法访问 HttpServletRequest?1. 请求上下文与线

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

python 3.8 的anaconda下载方法

《python3.8的anaconda下载方法》本文详细介绍了如何下载和安装带有Python3.8的Anaconda发行版,包括Anaconda简介、下载步骤、安装指南以及验证安装结果,此外,还介... 目录python3.8 版本的 Anaconda 下载与安装指南一、Anaconda 简介二、下载 An

Java中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键