ffmpeg linesize注意事项

2024-04-27 16:48

本文主要是介绍ffmpeg linesize注意事项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最近在做视频混合,为此本人写了两篇博客,ffmpeg利用滤镜合并两个视频,一左一右
和ffmpeg利用滤镜合并四个视频,左一右三

在本人台式机上(19201080)上混合是ok的,但是在笔记本电脑上(28801800)上混合产生了问题,图像变成了下面这种样子。
在这里插入图片描述
我一度以为是滤镜那块出了问题,幸好不是。
在排查过程中,本人写了一个例子,通过ffmpeg读取本地视频文件,然后再经过编码,写入文件,结果发现,写入的文件播放时,就出现上面这个样子。

经过排查,发现,读取到的AVFrame的linesize为2944,1472,1472;而之前想的应该是2880,1440,1440。
所以读取到的数据其实多了一些冗余信息,拿Y分量而言,第一行到第1800行,每行中的第2881个字节数据到第2994个字节数据是多余的,需要去掉。

话说linesize需要是2944,1472,1472,有人说是为了对齐,分量需要是64的倍数,而1440不是64的倍数,在此先记录下。

在这里插入图片描述

下面罗列下处理,首先构建了三个变量pY,pU,pV,这里面的y_size是2880x1800

int y_size = m_pReadCodecCtx_VideoA->width * m_pReadCodecCtx_VideoA->height;char *pY = new char[y_size];char *pU = new char[y_size / 4];char *pV = new char[y_size / 4];

然后重新给这三个分量赋值,去掉每行中冗余的部分

 ///Y
int contY = 0;
for (int i = 0; i < pFrame->height; i++)
{memcpy(pY + contY, pFrame->data[0] + i * pFrame->linesize[0], pFrame->width);contY += pFrame->width;
}///U
int contU = 0;
for (int i = 0; i < pFrame->height / 2; i++)
{memcpy(pU + contU, pFrame->data[1] + i * pFrame->linesize[1], pFrame->width / 2);contU += pFrame->width / 2;
}///V
int contV = 0;
for (int i = 0; i < pFrame->height / 2; i++)
{memcpy(pV + contV, pFrame->data[2] + i * pFrame->linesize[2], pFrame->width / 2);contV += pFrame->width / 2;
}

最后是将处理后的分量送入队列

EnterCriticalSection(&m_csVideoASection);
av_fifo_generic_write(m_pVideoAFifo, pY, y_size, NULL);
av_fifo_generic_write(m_pVideoAFifo, pU, y_size / 4, NULL);
av_fifo_generic_write(m_pVideoAFifo, pV, y_size / 4, NULL);
LeaveCriticalSection(&m_csVideoASection);

工程目录如下:
在这里插入图片描述
其中main所在文件FfmpegCopyFileTest.cpp的代码如下:

#include <iostream>
#include "CopyFile.h"#ifdef	__cplusplus
extern "C"
{
#endif#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")#ifdef __cplusplus
};
#endifint main()
{CCopyFile cVideoCopy;const char *pFileA = "E:\\learn\\ffmpeg\\FfmpegFilterTest\\x64\\Release\\in-desktop-2880x1800.mp4";const char *pFileOut = "E:\\learn\\ffmpeg\\FfmpegFilterTest\\x64\\Release\\out-copy.mp4";cVideoCopy.StartCopy(pFileA, pFileOut);cVideoCopy.WaitFinish();return 0;
}

CopyFile.h的代码如下:

#pragma once#include <Windows.h>#ifdef	__cplusplus
extern "C"
{
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavdevice/avdevice.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/avutil.h"
#include "libavutil/fifo.h"
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"#ifdef __cplusplus
};
#endifclass CCopyFile
{
public:CCopyFile();~CCopyFile();
public:int StartCopy(const char *pFileA, const char *pFileOut);int WaitFinish();
private:int OpenFileA(const char *pFileA);int OpenOutPut(const char *pFileOut);
private:static DWORD WINAPI VideoAReadProc(LPVOID lpParam);void VideoARead();static DWORD WINAPI VideoCopyProc(LPVOID lpParam);void VideoCopy();
private:AVFormatContext *m_pFormatCtx_FileA = NULL;AVCodecContext *m_pReadCodecCtx_VideoA = NULL;AVCodec *m_pReadCodec_VideoA = NULL;AVCodecContext	*m_pCodecEncodeCtx_Video = NULL;AVFormatContext *m_pFormatCtx_Out = NULL;AVFifoBuffer *m_pVideoAFifo = NULL;int m_iMergeWidth = 1920;int m_iMergeHeight = 1080;int m_iYuv420FrameSize = 0;
private:CRITICAL_SECTION m_csVideoASection;HANDLE m_hVideoAReadThread = NULL;HANDLE m_hVideoCopyhread = NULL;
};

CopyFile.cpp的代码如下:


#include "CopyFile.h"
//#include "log/log.h"CCopyFile::CCopyFile()
{InitializeCriticalSection(&m_csVideoASection);
}CCopyFile::~CCopyFile()
{DeleteCriticalSection(&m_csVideoASection);
}int CCopyFile::StartCopy(const char *pFileA, const char *pFileOut)
{int ret = -1;do {ret = OpenFileA(pFileA);if (ret != 0){break;}ret = OpenOutPut(pFileOut);if (ret != 0){break;}m_iYuv420FrameSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, m_pReadCodecCtx_VideoA->width, m_pReadCodecCtx_VideoA->height, 1);//申请30帧缓存m_pVideoAFifo = av_fifo_alloc(30 * m_iYuv420FrameSize);m_hVideoAReadThread = CreateThread(NULL, 0, VideoAReadProc, this, 0, NULL);m_hVideoCopyhread = CreateThread(NULL, 0, VideoCopyProc, this, 0, NULL);} while (0);return ret;
}int CCopyFile::WaitFinish()
{int ret = 0;do {if (NULL == m_hVideoAReadThread){break;}WaitForSingleObject(m_hVideoAReadThread, INFINITE);CloseHandle(m_hVideoAReadThread);m_hVideoAReadThread = NULL;WaitForSingleObject(m_hVideoCopyhread, INFINITE);CloseHandle(m_hVideoCopyhread);m_hVideoCopyhread = NULL;} while (0);return ret;
}int CCopyFile::OpenFileA(const char *pFileA)
{int ret = -1;do{if ((ret = avformat_open_input(&m_pFormatCtx_FileA, pFileA, 0, 0)) < 0) {printf("Could not open input file.");break;}if ((ret = avformat_find_stream_info(m_pFormatCtx_FileA, 0)) < 0) {printf("Failed to retrieve input stream information");break;}if (m_pFormatCtx_FileA->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO){break;}m_pReadCodec_VideoA = (AVCodec *)avcodec_find_decoder(m_pFormatCtx_FileA->streams[0]->codecpar->codec_id);m_pReadCodecCtx_VideoA = avcodec_alloc_context3(m_pReadCodec_VideoA);if (m_pReadCodecCtx_VideoA == NULL){break;}avcodec_parameters_to_context(m_pReadCodecCtx_VideoA, m_pFormatCtx_FileA->streams[0]->codecpar);m_iMergeWidth = m_pReadCodecCtx_VideoA->width;m_iMergeHeight = m_pReadCodecCtx_VideoA->height;m_pReadCodecCtx_VideoA->framerate = m_pFormatCtx_FileA->streams[0]->r_frame_rate;if (avcodec_open2(m_pReadCodecCtx_VideoA, m_pReadCodec_VideoA, NULL) < 0){break;}ret = 0;} while (0);return ret;
}int CCopyFile::OpenOutPut(const char *pFileOut)
{int iRet = -1;AVStream *pAudioStream = NULL;AVStream *pVideoStream = NULL;do{avformat_alloc_output_context2(&m_pFormatCtx_Out, NULL, NULL, pFileOut);{AVCodec* pCodecEncode_Video = (AVCodec *)avcodec_find_encoder(m_pFormatCtx_Out->oformat->video_codec);m_pCodecEncodeCtx_Video = avcodec_alloc_context3(pCodecEncode_Video);if (!m_pCodecEncodeCtx_Video){break;}pVideoStream = avformat_new_stream(m_pFormatCtx_Out, pCodecEncode_Video);if (!pVideoStream){break;}int frameRate = 10;m_pCodecEncodeCtx_Video->flags |= AV_CODEC_FLAG_QSCALE;m_pCodecEncodeCtx_Video->bit_rate = 4000000;m_pCodecEncodeCtx_Video->rc_min_rate = 4000000;m_pCodecEncodeCtx_Video->rc_max_rate = 4000000;m_pCodecEncodeCtx_Video->bit_rate_tolerance = 4000000;m_pCodecEncodeCtx_Video->time_base.den = frameRate;m_pCodecEncodeCtx_Video->time_base.num = 1;m_pCodecEncodeCtx_Video->width = m_iMergeWidth;m_pCodecEncodeCtx_Video->height = m_iMergeHeight;//pH264Encoder->pCodecCtx->frame_number = 1;m_pCodecEncodeCtx_Video->gop_size = 12;m_pCodecEncodeCtx_Video->max_b_frames = 0;m_pCodecEncodeCtx_Video->thread_count = 4;m_pCodecEncodeCtx_Video->pix_fmt = AV_PIX_FMT_YUV420P;m_pCodecEncodeCtx_Video->codec_id = AV_CODEC_ID_H264;m_pCodecEncodeCtx_Video->codec_type = AVMEDIA_TYPE_VIDEO;av_opt_set(m_pCodecEncodeCtx_Video->priv_data, "b-pyramid", "none", 0);av_opt_set(m_pCodecEncodeCtx_Video->priv_data, "preset", "superfast", 0);av_opt_set(m_pCodecEncodeCtx_Video->priv_data, "tune", "zerolatency", 0);if (m_pFormatCtx_Out->oformat->flags & AVFMT_GLOBALHEADER)m_pCodecEncodeCtx_Video->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;if (avcodec_open2(m_pCodecEncodeCtx_Video, pCodecEncode_Video, 0) < 0){//编码器打开失败,退出程序break;}}if (!(m_pFormatCtx_Out->oformat->flags & AVFMT_NOFILE)){if (avio_open(&m_pFormatCtx_Out->pb, pFileOut, AVIO_FLAG_WRITE) < 0){break;}}avcodec_parameters_from_context(pVideoStream->codecpar, m_pCodecEncodeCtx_Video);if (avformat_write_header(m_pFormatCtx_Out, NULL) < 0){break;}iRet = 0;} while (0);if (iRet != 0){if (m_pCodecEncodeCtx_Video != NULL){avcodec_free_context(&m_pCodecEncodeCtx_Video);m_pCodecEncodeCtx_Video = NULL;}if (m_pFormatCtx_Out != NULL){avformat_free_context(m_pFormatCtx_Out);m_pFormatCtx_Out = NULL;}}return iRet;
}DWORD WINAPI CCopyFile::VideoAReadProc(LPVOID lpParam)
{CCopyFile *pVideoMerge = (CCopyFile *)lpParam;if (pVideoMerge != NULL){pVideoMerge->VideoARead();}return 0;
}void CCopyFile::VideoARead()
{AVFrame *pFrame;pFrame = av_frame_alloc();int y_size = m_pReadCodecCtx_VideoA->width * m_pReadCodecCtx_VideoA->height;char *pY = new char[y_size];char *pU = new char[y_size / 4];char *pV = new char[y_size / 4];AVPacket packet = { 0 };int ret = 0;while (1){av_packet_unref(&packet);ret = av_read_frame(m_pFormatCtx_FileA, &packet);if (ret == AVERROR(EAGAIN)){continue;}else if (ret == AVERROR_EOF){break;}else if (ret < 0) {break;}ret = avcodec_send_packet(m_pReadCodecCtx_VideoA, &packet);if (ret >= 0){ret = avcodec_receive_frame(m_pReadCodecCtx_VideoA, pFrame);if (ret == AVERROR(EAGAIN)){continue;}else if (ret == AVERROR_EOF){break;}else if (ret < 0) {break;}while (1){if (av_fifo_space(m_pVideoAFifo) >= m_iYuv420FrameSize){///Yint contY = 0;for (int i = 0; i < pFrame->height; i++){memcpy(pY + contY, pFrame->data[0] + i * pFrame->linesize[0], pFrame->width);contY += pFrame->width;}///Uint contU = 0;for (int i = 0; i < pFrame->height / 2; i++){memcpy(pU + contU, pFrame->data[1] + i * pFrame->linesize[1], pFrame->width / 2);contU += pFrame->width / 2;}///Vint contV = 0;for (int i = 0; i < pFrame->height / 2; i++){memcpy(pV + contV, pFrame->data[2] + i * pFrame->linesize[2], pFrame->width / 2);contV += pFrame->width / 2;}EnterCriticalSection(&m_csVideoASection);av_fifo_generic_write(m_pVideoAFifo, pY, y_size, NULL);av_fifo_generic_write(m_pVideoAFifo, pU, y_size / 4, NULL);av_fifo_generic_write(m_pVideoAFifo, pV, y_size / 4, NULL);LeaveCriticalSection(&m_csVideoASection);break;}else{Sleep(100);}}}if (ret == AVERROR(EAGAIN)){continue;}}av_frame_free(&pFrame);delete[] pY;delete[] pU;delete[] pV;
}DWORD WINAPI CCopyFile::VideoCopyProc(LPVOID lpParam)
{CCopyFile *pVideoMerge = (CCopyFile *)lpParam;if (pVideoMerge != NULL){pVideoMerge->VideoCopy();}return 0;
}void CCopyFile::VideoCopy()
{int ret = 0;AVFrame *pFrameVideoA = av_frame_alloc();uint8_t *videoA_buffer_yuv420 = (uint8_t *)av_malloc(m_iYuv420FrameSize);av_image_fill_arrays(pFrameVideoA->data, pFrameVideoA->linesize, videoA_buffer_yuv420, AV_PIX_FMT_YUV420P, m_pReadCodecCtx_VideoA->width, m_pReadCodecCtx_VideoA->height, 1);int iOutVideoWidth = m_pReadCodecCtx_VideoA->width;int iOutVideoHeight = m_pReadCodecCtx_VideoA->height;AVPacket packet = { 0 };int iPicCount = 0;while (1){if (NULL == m_pVideoAFifo){break;}int iVideoASize = av_fifo_size(m_pVideoAFifo);if (iVideoASize >= m_iYuv420FrameSize){EnterCriticalSection(&m_csVideoASection);av_fifo_generic_read(m_pVideoAFifo, videoA_buffer_yuv420, m_iYuv420FrameSize, NULL);LeaveCriticalSection(&m_csVideoASection);pFrameVideoA->pkt_dts = pFrameVideoA->pts = av_rescale_q_rnd(iPicCount, m_pCodecEncodeCtx_Video->time_base, m_pFormatCtx_Out->streams[0]->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));pFrameVideoA->pkt_duration = 0;pFrameVideoA->pkt_pos = -1;pFrameVideoA->width = iOutVideoWidth;pFrameVideoA->height = iOutVideoHeight;pFrameVideoA->format = AV_PIX_FMT_YUV420P;ret = avcodec_send_frame(m_pCodecEncodeCtx_Video, pFrameVideoA);ret = avcodec_receive_packet(m_pCodecEncodeCtx_Video, &packet);av_write_frame(m_pFormatCtx_Out, &packet);iPicCount++;}else{if (m_hVideoAReadThread == NULL){break;}Sleep(1);}}av_write_trailer(m_pFormatCtx_Out);avio_close(m_pFormatCtx_Out->pb);av_frame_free(&pFrameVideoA);
}

这篇关于ffmpeg linesize注意事项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

电脑死机无反应怎么强制重启? 一文读懂方法及注意事项

《电脑死机无反应怎么强制重启?一文读懂方法及注意事项》在日常使用电脑的过程中,我们难免会遇到电脑无法正常启动的情况,本文将详细介绍几种常见的电脑强制开机方法,并探讨在强制开机后应注意的事项,以及如何... 在日常生活和工作中,我们经常会遇到电脑突然无反应的情况,这时候强制重启就成了解决问题的“救命稻草”。那

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Python中__new__()方法适应及注意事项详解

《Python中__new__()方法适应及注意事项详解》:本文主要介绍Python中__new__()方法适应及注意事项的相关资料,new()方法是Python中的一个特殊构造方法,用于在创建对... 目录前言基本用法返回值单例模式自定义对象创建注意事项总结前言new() 方法在 python 中是一个

Springboot的自动配置是什么及注意事项

《Springboot的自动配置是什么及注意事项》SpringBoot的自动配置(Auto-configuration)是指框架根据项目的依赖和应用程序的环境自动配置Spring应用上下文中的Bean... 目录核心概念:自动配置的关键特点:自动配置工作原理:示例:需要注意的点1.默认配置可能不适合所有场景

Spring Cloud Hystrix原理与注意事项小结

《SpringCloudHystrix原理与注意事项小结》本文介绍了Hystrix的基本概念、工作原理以及其在实际开发中的应用方式,通过对Hystrix的深入学习,开发者可以在分布式系统中实现精细... 目录一、Spring Cloud Hystrix概述和设计目标(一)Spring Cloud Hystr

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

Idea调用WebService的关键步骤和注意事项

《Idea调用WebService的关键步骤和注意事项》:本文主要介绍如何在Idea中调用WebService,包括理解WebService的基本概念、获取WSDL文件、阅读和理解WSDL文件、选... 目录前言一、理解WebService的基本概念二、获取WSDL文件三、阅读和理解WSDL文件四、选择对接

python安装完成后可以进行的后续步骤和注意事项小结

《python安装完成后可以进行的后续步骤和注意事项小结》本文详细介绍了安装Python3后的后续步骤,包括验证安装、配置环境、安装包、创建和运行脚本,以及使用虚拟环境,还强调了注意事项,如系统更新、... 目录验证安装配置环境(可选)安装python包创建和运行Python脚本虚拟环境(可选)注意事项安装

JAVA中while循环的使用与注意事项

《JAVA中while循环的使用与注意事项》:本文主要介绍while循环在编程中的应用,包括其基本结构、语句示例、适用场景以及注意事项,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录while循环1. 什么是while循环2. while循环的语句3.while循环的适用场景以及优势4. 注意

使用Spring Cache时设置缓存键的注意事项详解

《使用SpringCache时设置缓存键的注意事项详解》在现代的Web应用中,缓存是提高系统性能和响应速度的重要手段之一,Spring框架提供了强大的缓存支持,通过​​@Cacheable​​、​​... 目录引言1. 缓存键的基本概念2. 默认缓存键生成器3. 自定义缓存键3.1 使用​​@Cacheab