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

相关文章

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

浅谈主机加固,六种有效的主机加固方法

在数字化时代,数据的价值不言而喻,但随之而来的安全威胁也日益严峻。从勒索病毒到内部泄露,企业的数据安全面临着前所未有的挑战。为了应对这些挑战,一种全新的主机加固解决方案应运而生。 MCK主机加固解决方案,采用先进的安全容器中间件技术,构建起一套内核级的纵深立体防护体系。这一体系突破了传统安全防护的局限,即使在管理员权限被恶意利用的情况下,也能确保服务器的安全稳定运行。 普适主机加固措施:

webm怎么转换成mp4?这几种方法超多人在用!

webm怎么转换成mp4?WebM作为一种新兴的视频编码格式,近年来逐渐进入大众视野,其背后承载着诸多优势,但同时也伴随着不容忽视的局限性,首要挑战在于其兼容性边界,尽管WebM已广泛适应于众多网站与软件平台,但在特定应用环境或老旧设备上,其兼容难题依旧凸显,为用户体验带来不便,再者,WebM格式的非普适性也体现在编辑流程上,由于它并非行业内的通用标准,编辑过程中可能会遭遇格式不兼容的障碍,导致操

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

【北交大信息所AI-Max2】使用方法

BJTU信息所集群AI_MAX2使用方法 使用的前提是预约到相应的算力卡,拥有登录权限的账号密码,一般为导师组共用一个。 有浏览器、ssh工具就可以。 1.新建集群Terminal 浏览器登陆10.126.62.75 (如果是1集群把75改成66) 交互式开发 执行器选Terminal 密码随便设一个(需记住) 工作空间:私有数据、全部文件 加速器选GeForce_RTX_2080_Ti

【VUE】跨域问题的概念,以及解决方法。

目录 1.跨域概念 2.解决方法 2.1 配置网络请求代理 2.2 使用@CrossOrigin 注解 2.3 通过配置文件实现跨域 2.4 添加 CorsWebFilter 来解决跨域问题 1.跨域概念 跨域问题是由于浏览器实施了同源策略,该策略要求请求的域名、协议和端口必须与提供资源的服务相同。如果不相同,则需要服务器显式地允许这种跨域请求。一般在springbo

AI(文生语音)-TTS 技术线路探索学习:从拼接式参数化方法到Tacotron端到端输出

AI(文生语音)-TTS 技术线路探索学习:从拼接式参数化方法到Tacotron端到端输出 在数字化时代,文本到语音(Text-to-Speech, TTS)技术已成为人机交互的关键桥梁,无论是为视障人士提供辅助阅读,还是为智能助手注入声音的灵魂,TTS 技术都扮演着至关重要的角色。从最初的拼接式方法到参数化技术,再到现今的深度学习解决方案,TTS 技术经历了一段长足的进步。这篇文章将带您穿越时

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

音视频入门基础:WAV专题(10)——FFmpeg源码中计算WAV音频文件每个packet的pts、dts的实现

一、引言 从文章《音视频入门基础:WAV专题(6)——通过FFprobe显示WAV音频文件每个数据包的信息》中我们可以知道,通过FFprobe命令可以打印WAV音频文件每个packet(也称为数据包或多媒体包)的信息,这些信息包含该packet的pts、dts: 打印出来的“pts”实际是AVPacket结构体中的成员变量pts,是以AVStream->time_base为单位的显

使用JS/Jquery获得父窗口的几个方法(笔记)

<pre name="code" class="javascript">取父窗口的元素方法:$(selector, window.parent.document);那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent.parent.document);如题: $(selector, window.top.document);//获得顶级窗口里面的元素 $(