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

相关文章

FFmpeg IOS 解封装

解封装流程: Demo 引入头文件: //核心库-音视频编解码库#import <libavcodec/avcodec.h>//封装格式库#import <libavformat/avformat.h> (1)av_register_all() 注册所有的封装格式、加封装格式;也可以在使用之前单个注册。(新版本该方法弃用了) 在我最新使用的4.0.2版本中,已经不需要调用了。

Mac下vscode配置FFmpeg调试

最近在用vscode 写FFmpeg相关的代码,如果用命令行运行的话,有时候语法错误报的是段错误(segmentation fault),所以找了一下配置vscode 调试的方法,记录一下: 1,装一点小插件: 2,按照官方文档新建对应的配置文件: 官方文档 3,修改tasks.json配置文件: {"version": "2.0.0","tasks": [{"label"

FFmpeg 'avcodec_copy_context' deprecated (视频裁剪)

在学习一些代码的时候发现有些已经弃用了:记录一下新的写法: 1,avcodec_copy_context 以前的写法: ret = avcodec_copy_context(outStream->codec, inStream->codec);if (ret < 0){fprintf(stderr, "Failed to copy context from input to output

wordpress外贸网站建设主机选择的注意事项

在为WordPress外贸网站建设选择主机时,您需要注意以下几点: 服务器地理位置: 选择一个靠近目标客户群的服务器位置,这将有助于提高网站的加载速度和可靠性。通常,如果您的目标客户群是国外用户,建议选择美国、欧洲或亚洲的数据中心。 主机类型: 根据您的需求和预算,选择合适的主机类型。常见的主机类型有虚拟主机(Shared Hosting)、VPS(Virtual Private Serv

Python学习注意事项

Python的布尔类型 boolean 类型 在运算中,如何判断 Python 字符串处理 Python容器 列表 list,元祖 tuple,字典dict,集合 set List List可以放入数字,字符串,等数据类型,list不对放入的类型进行判断,list可以放入任意类型的数据 可以正序遍历,也可以倒叙遍历 最后一个元素是 -1 Turple tuple 和list

SQL 性能优化,开发注意事项

对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。 应尽量避免在 where 子句中对字段进行 null 值判断,创建表时NULL是默认值,但大多数时候应该使用NOT NULL,或者使用一个特殊的值,如0,-1作为默 认值。 应尽量避免在 where 子句中使用!=或<>操作符, MySQL只有对以下操作符才使用索引:<,<=,=,>,>

灌溉排涝设计乙级升甲级全程跟踪:关键节点与注意事项

灌溉排涝设计乙级升甲级的过程中,需要关注一些关键节点和注意事项,以确保升级过程的顺利进行。以下是对这一过程的全程跟踪及关键节点和注意事项的概述: 一、前期准备阶段 了解资质要求:深入研究并理解甲级资质的具体要求,包括技术、管理、业绩等多个方面。对比乙级与甲级的差异,明确需要提升和完善的关键点。自我评估:对照甲级资质要求,对企业现有的技术人员配备、技术水平、管理能力、业绩等进行全面评估,找出需要

QtConcurrent::run操作界面ui的注意事项(1)

先说结论:QtConcurrent::run启动的耗时处理函数,不允许处理ui界面对象,如控件,如进度条等等! QtConcurrent::run非常好用,胜过QThead的两种方式(run和moveToThread),例如下面是非常直观和简单的使用方式: QT += concurrent#include <QtConcurrent>#include <QThread>void MainWi

FFmpeg学习记录(四)——SDL音视频渲染实战

1.SDL使用的基本步骤 SDL Init/sDL _Quit()SDL_CreateWindow()/SDL_DestoryWindow()SDL CreateRender() SDL_Windows *windows = NULL;SDL_Init(SDL_INIT_VIDEO);window = SDL_CreateWindow("SDL2 Windows",200,200, 6

自然语言处理(NLP)原理、用法、案例、注意事项

自然语言处理(Natural Language Processing,简称NLP)是人工智能(Artificial Intelligence,简称AI)领域的一个重要分支,旨在让计算机能够理解、理解和生成人类语言。 NLP的原理是基于统计建模和机器学习技术,通过对大量文本数据的分析和处理,从中提取语言规则、语义信息和模式,以实现对自然语言的处理。 NLP的用法非常广泛,涵盖了文本分类、