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

相关文章

Oracle 通过 ROWID 批量更新表的方法

《Oracle通过ROWID批量更新表的方法》在Oracle数据库中,使用ROWID进行批量更新是一种高效的更新方法,因为它直接定位到物理行位置,避免了通过索引查找的开销,下面给大家介绍Orac... 目录oracle 通过 ROWID 批量更新表ROWID 基本概念性能优化建议性能UoTrFPH优化建议注

Pandas进行周期与时间戳转换的方法

《Pandas进行周期与时间戳转换的方法》本教程将深入讲解如何在pandas中使用to_period()和to_timestamp()方法,完成时间戳与周期之间的转换,并结合实际应用场景展示这些方法的... 目录to_period() 时间戳转周期基本操作应用示例to_timestamp() 周期转时间戳基

在 PyQt 加载 UI 三种常见方法

《在PyQt加载UI三种常见方法》在PyQt中,加载UI文件通常指的是使用QtDesigner设计的.ui文件,并将其转换为Python代码,以便在PyQt应用程序中使用,这篇文章给大家介绍在... 目录方法一:使用 uic 模块动态加载 (不推荐用于大型项目)方法二:将 UI 文件编译为 python 模

Python将字库文件打包成可执行文件的常见方法

《Python将字库文件打包成可执行文件的常见方法》在Python打包时,如果你想将字库文件一起打包成一个可执行文件,有几种常见的方法,具体取决于你使用的打包工具,下面就跟随小编一起了解下具体的实现方... 目录使用 PyInstaller基本方法 - 使用 --add-data 参数使用 spec 文件(

Python的pip在命令行无法使用问题的解决方法

《Python的pip在命令行无法使用问题的解决方法》PIP是通用的Python包管理工具,提供了对Python包的查找、下载、安装、卸载、更新等功能,安装诸如Pygame、Pymysql等Pyt... 目录前言一. pip是什么?二. 为什么无法使用?1. 当我们在命令行输入指令并回车时,一般主要是出现以

通过C#获取Excel单元格的数据类型的方法详解

《通过C#获取Excel单元格的数据类型的方法详解》在处理Excel文件时,了解单元格的数据类型有助于我们正确地解析和处理数据,本文将详细介绍如何使用FreeSpire.XLS来获取Excel单元格的... 目录引言环境配置6种常见数据类型C# 读取单元格数据类型引言在处理 Excel 文件时,了解单元格

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

MySQL连接池(Pool)常用方法详解

《MySQL连接池(Pool)常用方法详解》本文详细介绍了MySQL连接池的常用方法,包括创建连接池、核心方法连接对象的方法、连接池管理方法以及事务处理,同时,还提供了最佳实践和性能提示,帮助开发者构... 目录mysql 连接池 (Pool) 常用方法详解1. 创建连接池2. 核心方法2.1 pool.q

Spring Boot Controller处理HTTP请求体的方法

《SpringBootController处理HTTP请求体的方法》SpringBoot提供了强大的机制来处理不同Content-Type​的HTTP请求体,这主要依赖于HttpMessageCo... 目录一、核心机制:HttpMessageConverter​二、按Content-Type​处理详解1.

查看MySQL数据库版本的四种方法

《查看MySQL数据库版本的四种方法》查看MySQL数据库的版本信息可以通过多种方法实现,包括使用命令行工具、SQL查询语句和图形化管理工具等,以下是详细的步骤和示例代码,需要的朋友可以参考下... 目录方法一:使用命令行工具1. 使用 mysql 命令示例:方法二:使用 mysqladmin 命令示例:方