sws_scale解出来的RGB图像颠倒问题

本文介绍如何使用FFmpeg库进行视频解码,并将解码后的YUV格式转换为RGB格式,涉及AVFrame结构体的data和linesize字段操作及SwsContext上下文设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  

AVFrame的data和linesize:

YUV: linesize[0] =  width + padding size(16+16),linesize[1]=linesize[0]/2

        data[0],data[1],data[2]分别代表yuv 

RGB: linesize[0] = width*pixel_size  for RGB
        data[0]为packet rgb


  pFrame->data[0] = pFrame->data[0]+pFrame->linesize[0]*(pContext->height-1);
  pFrame->data[1] = pFrame->data[1]+pFrame->linesize[1]*(pContext->height/2-1);
  pFrame->data[2] = pFrame->data[2]+pFrame->linesize[2]*(pContext->height/2-1);

 

#include "decode_file.h" #include <QDebug> #include <QImage> Decode_file::Decode_file(QObject *parent) : QObject{parent} { // 初始化FFmpeg avformat_network_init(); decode_video(); } void Decode_file::decode_video() { // 打开文件 if(avformat_open_input(&pFormatCtx, filename, NULL, NULL) != 0) { qDebug() << "无法打开文件"; return; } // 获取流信息 if(avformat_find_stream_info(pFormatCtx, NULL) < 0) { qDebug() << "无法获取流信息"; return; } // 查找视频流 for(unsigned int i=0; i<pFormatCtx->nb_streams; i++) { if(pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; origin_par = pFormatCtx->streams[i]->codecpar; break; } } if(video_stream_index == -1) { qDebug() << "未找到视频流"; return; } // 查找解码器(需确认AV_CODEC_ID_CAVS是否支持) codec = avcodec_find_decoder(origin_par->codec_id); if(!codec) { qDebug() << "找不到解码器"; return; } // 创建解码器上下文 codecCtx = avcodec_alloc_context3(codec); avcodec_parameters_to_context(codecCtx, origin_par); // 打开解码器 if(avcodec_open2(codecCtx, codec, NULL) < 0) { qDebug() << "无法打开解码器"; return; } // 分配帧和包内存 frame = av_frame_alloc(); packet = av_packet_alloc(); AVRational time_base = {1, 90000}; // 根据帧率动态计算 int64_t pts_counter = 0; // 解码循环 while (av_read_frame(pFormatCtx, packet) >= 0) { qDebug() << "video_stream_index:" << video_stream_index; if (packet->stream_index == video_stream_index) { // 重建时间戳 packet->pts = pts_counter; packet->dts = pts_counter; pts_counter += 3600; // 根据帧率调整 // 发送数据包 int ret = avcodec_send_packet(codecCtx, packet); if (ret < 0 && ret != AVERROR(EAGAIN)) { char err_msg[AV_ERROR_MAX_STRING_SIZE]; if (av_strerror(ret, err_msg, sizeof(err_msg)) < 0) { snprintf(err_msg, AV_ERROR_MAX_STRING_SIZE, "Unknown error %d", ret); } fprintf(stderr, "Error sending packet: %s\n", err_msg); continue; } // 接收帧 while (ret >= 0) { ret = avcodec_receive_frame(codecCtx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) break; else if (ret < 0) { char err_msg[AV_ERROR_MAX_STRING_SIZE]; if (av_strerror(ret, err_msg, sizeof(err_msg)) < 0) { snprintf(err_msg, AV_ERROR_MAX_STRING_SIZE, "Unknown error %d", ret); } fprintf(stderr, "Error sending packet: %s\n", err_msg); break; } // 处理帧(如转换为QImage) convert_frame_to_qimage(frame); av_frame_unref(frame); } } av_packet_unref(packet); } // 刷新解码器 avcodec_send_packet(codecCtx, NULL); while (avcodec_receive_frame(codecCtx, frame) >= 0) { convert_frame_to_qimage(frame); av_frame_unref(frame); } av_packet_free(&packet); av_frame_free(&frame); // 清理资源 av_packet_free(&packet); av_frame_free(&frame); avcodec_free_context(&codecCtx); avformat_close_input(&pFormatCtx); } 完善convert_frame_to_qimage和convert_frame_to_qimage函数
最新发布
03-11
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值