ffmpeg4教程12:intel media sdk(qsv)硬解码的使用方法+qt5 openggl显示AV_PIX_FMT_NV12

讨论群261074724

1.安装intel media sdk 请对于处理器的版本代号

2.参考ffmpeg官方的examples下的 qsvdec.c改造

如果差#include <mfx/mfxvideo.h>头文件需要将 文件下的include 重命名为mfx 考到工程

 

主要加了一个转换城rgb24的窗口显示


#include "pch.h"
#include <iostream>
#include <opencv2/core/utility.hpp> 
#include <opencv2/opencv.hpp>
#include <windows.h>

extern "C" {
#include "libavcodec/avcodec.h" 
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libavdevice/avdevice.h"
#include "libavutil/imgutils.h"
#include "libavutil/audio_fifo.h"  
#include "libavutil/time.h"
#include "libavutil/mathematics.h"
#include "libavutil/channel_layout.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavutil/opt.h"  

#include "libavutil/mem.h"
#include "libavutil/buffer.h"
#include "libavutil/error.h"
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_qsv.h"
#define HAVE_STRUCT_TIMESPEC
#include "pthread.h" 
}
 
typedef struct DecodeContext {
    AVBufferRef *hw_device_ref;
} DecodeContext;
static AVPixelFormat get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
{
    while (*pix_fmts != AV_PIX_FMT_NONE) {
        if (*pix_fmts == AV_PIX_FMT_QSV) {
            DecodeContext *decode =(DecodeContext *) avctx->opaque;
            AVHWFramesContext  *frames_ctx;
            AVQSVFramesContext *frames_hwctx;
            int ret;

            /* create a pool of surfaces to be used by the decoder */
            avctx->hw_frames_ctx = av_hwframe_ctx_alloc(decode->hw_device_ref);
            if (!avctx->hw_frames_ctx)
                return AV_PIX_FMT_NONE;
            frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
            frames_hwctx =(AVQSVFramesContext *) frames_ctx->hwctx;

            frames_ctx->format = AV_PIX_FMT_QSV;
            frames_ctx->sw_format = avctx->sw_pix_fmt;
            frames_ctx->width = FFALIGN(avctx->coded_width, 32);
            frames_ctx->height = FFALIGN(avctx->coded_height, 32);
            frames_ctx->initial_pool_size = 32;

            frames_hwctx->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;

            ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
            if (ret < 0)
                return AV_PIX_FMT_NONE;

            return AV_PIX_FMT_QSV;
        }

        pix_fmts++;
    }

    fprintf(stderr, "The QSV pixel format not offered in get_format()\n");

    return AV_PIX_FMT_NONE;
}
static void Show(HWND hwnd, unsigned char* rgb, int w, int h, bool fill)
{
    HDC hdc = GetDC(hwnd);//获取当前的显示设备上下文

    RECT rect;
    GetClientRect(hwnd, &rect);
    int cxClient = rect.right;
    int cyClient = rect.bottom;

    if (cxClient <= 0 || cyClient <= 0) {
        return;
    }

    HDC  hdcsource = CreateCompatibleDC(NULL);//创建存放图象的显示缓冲
    HBITMAP bitmap = CreateCompatibleBitmap(hdc, cxClient, cyClient);

    SelectObject(hdcsource, bitmap);    //将位图资源装入显示缓冲


    SetStretchBltMode(hdcsource, COLORONCOLOR);

    BITMAPINFO  bmi;
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = w;
    bmi.bmiHeader.biHeight = -h;
    bmi.bmiHeader.biCompression = BI_RGB;
    bmi.bmiHeader.biBitCount = 24;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biClrUsed = 0;
    bmi.bmiHeader.biClrImportant = 0;
    bmi.bmiHeader.biSizeImage = 0;


    if (!fill) {

        int des_x = 0;
        int des_y = 0;
        int des_w = 0;
        int des_h = 0;


        if (1.0*cxClient / cyClient > 1.0*w / h) {
            des_h = cyClient;
            des_w = des_h * w / h;
            des_x = (cxClient - des_w) / 2;
            des_y = 0;
        }
        else {
            des_w = cxClient;
            des_h = des_w * h / w;
            des_x = 0;
            des_y = (cyClient - des_h) / 2;
        }


        BitBlt(hdcsource, 0, 0, cxClient, cyClient, hdcsource, 0, 0, SRCCOPY);
        StretchDIBits(hdcsource, des_x, des_y, des_w, des_h, \
            0, 0, w, h, rgb, &bmi, DIB_RGB_COLORS, SRCCOPY);
        BitBlt(hdc, 0, 0, cxClient, cyClient, hdcsource, 0, 0, SRCCOPY);
    }
    else {
        StretchDIBits(hdcsource, 0, 0, rect.right - rect.left, rect.bottom - rect.top, \
            0, 0, w, h, rgb, &bmi, DIB_RGB_COLORS, SRCCOPY);

        BitBlt(hdc, 0, 0, cxClient, cyClient, hdcsource, 0, 0, SRCCOPY);//将图象显示缓冲的内容直接显示到屏幕
    }

    DeleteObject(bitmap);
    DeleteDC(hdcsource);
    ReleaseDC(hwnd, hdc);
}

LRESULT CALLBACK WinProc(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)

    switch (umsg)
    {
     
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, umsg, wparam, lparam);
}  


static int decode_packet(DecodeContext *decode, AVCodecContext *decoder_ctx,
    AVFrame *frame, AVFrame *sw_frame,
    AVPacket *pkt, AVIOContext *output_ctx,HWND hwnd , SwsContext *img_convert_ctx, AVFrame *bgrFrame)
{
    int ret = 0;
    ret = avcodec_send_packet(decoder_ctx, pkt);
    if (ret < 0) {
        fprintf(stderr, "Error during decoding\n");
        return ret;
    }
    while (ret >= 0) {
        int i, j;
        ret = avcodec_receive_frame(decoder_ctx, frame);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            break;
        else if (ret < 0) {
            fprintf(stderr, "Error during decoding\n");
            return ret;
        } 
         
        /* AV_PIX_FMT_QSV到 AV_PIX_FMT_NV12*/ 
        ret = av_hwframe_transfer_data(sw_frame, frame, 0);
        if (ret < 0) {
            fprintf(stderr, "Error transferring the data to system memory\n");
            goto fail;
        }    
        sws_scale(img_convert_ctx, (const unsigned char* const*)sw_frame->data, sw_frame->linesize, 0, sw_frame->height, bgrFrame->data, bgrFrame->linesize);
         
        Show(hwnd, bgrFrame->data[0], bgrFrame->width, bgrFrame->height, true);
        
         
    fail:
        av_frame_unref(sw_frame);
        av_frame_unref(frame);
        if (ret < 0)
            return ret;
    }
    return 0;
}

static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
{
    AVFrame *picture;
    int ret;

    picture = av_frame_alloc();
    if (!picture)
        return NULL;

    picture->format = pix_fmt;
    picture->width = width;
    picture->height = height;

    /* allocate the buffers for the frame data */
    ret = av_frame_get_buffer(picture, 4);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate frame data.\n");
        return NULL;
    }

    return picture;
}
void *VideoReadThread1(void*p) { 
    HWND hwnd =(HWND) p;
    int ret = 0;
    const char*url = "F:\\source\\ffmpge\\mux\\1.mp4";
    AVFormatContext* pInputFormatCtx = avformat_alloc_context();
    AVStream *video_st = NULL;

    DecodeContext decode = { NULL };
    const AVCodec *decoder;
    AVCodecContext *decoder_ctx = NULL;
    AVPacket pkt = { 0 }; av_init_packet(&pkt);
    if ((ret = avformat_open_input(&pInputFormatCtx, url, NULL, NULL)) < 0) {
        printf("Could not open input file.");
        return 0;
    }
    if ((ret = avformat_find_stream_info(pInputFormatCtx, 0)) < 0) {
        printf("Failed to retrieve input stream information");
        return 0;
    }
     
    /* find the first H.264 video stream */
    for (int i = 0; i < pInputFormatCtx->nb_streams; i++) {
        AVStream *st = pInputFormatCtx->streams[i];
        if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !video_st)
        { 
            video_st = st;
        }
        else
            st->discard = AVDISCARD_ALL;
    }
 
    AVCodecContext *envideocodecCtx = NULL;
    AVIOContext *output_ctx = NULL;

    AVFrame *frame = NULL, *sw_frame = NULL;

    SwsContext *img_convert_ctx = sws_getContext(pInputFormatCtx->streams[video_st->index]->codecpar->width, pInputFormatCtx->streams[video_st->index]->codecpar->height,
        AV_PIX_FMT_NV12, pInputFormatCtx->streams[video_st->index]->codecpar->width, pInputFormatCtx->streams[video_st->index]->codecpar->height, AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL, NULL);
    AVFrame *bgrFrame = alloc_picture(AV_PIX_FMT_BGR24, pInputFormatCtx->streams[video_st->index]->codecpar->width, pInputFormatCtx->streams[video_st->index]->codecpar->height);
    

    if (!video_st) {
        fprintf(stderr, "No H.264 video stream in the input file\n");
        goto finish;
    }
    /* open the hardware device */
    ret = av_hwdevice_ctx_create(&decode.hw_device_ref, AV_HWDEVICE_TYPE_QSV, "auto", NULL, 0);
    if (ret < 0) {
        fprintf(stderr, "Cannot open the hardware device\n");
        goto finish;
    }
    /* initialize the decoder */
    decoder = avcodec_find_decoder_by_name("h264_qsv");
    if (!decoder) {
        fprintf(stderr, "The QSV decoder is not present in libavcodec\n");
        goto finish;
    }
    decoder_ctx = avcodec_alloc_context3(decoder);
    if (!decoder_ctx) {
        ret = AVERROR(ENOMEM);
        goto finish;
    }
    decoder_ctx->codec_id = AV_CODEC_ID_H264;
    if (video_st->codecpar->extradata_size) {
        decoder_ctx->extradata = (uint8_t *)av_mallocz(video_st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
        if (!decoder_ctx->extradata) {
            ret = AVERROR(ENOMEM);
            goto finish;
        }
        memcpy(decoder_ctx->extradata, video_st->codecpar->extradata,
            video_st->codecpar->extradata_size);
        decoder_ctx->extradata_size = video_st->codecpar->extradata_size;
    }

    decoder_ctx->opaque = &decode;
    decoder_ctx->get_format = get_format;

    ret = avcodec_open2(decoder_ctx, NULL, NULL);
    if (ret < 0) {
        fprintf(stderr, "Error opening the decoder: ");
        goto finish;
    }
    av_dump_format(pInputFormatCtx, 0, url, 0);
     
    frame = av_frame_alloc();
    sw_frame = av_frame_alloc();
    if (!frame || !sw_frame) {
        ret = AVERROR(ENOMEM);
        goto finish;
    }


    time_t tt;//这句返回的只是一个时间cuo
    struct tm t;
    time(&tt);
    localtime_s(&t, &tt);
    printf("%d-%02d-%02d %02d:%02d:%02d\n",
        t.tm_year + 1900,
        t.tm_mon + 1,
        t.tm_mday,
        t.tm_hour,
        t.tm_min,
        t.tm_sec);

    
    /* actual decoding */ 
        while (ret >= 0) {
            ret = av_read_frame(pInputFormatCtx, &pkt);
            if (ret < 0)
                break;
            if (pkt.stream_index == video_st->index)
                ret = decode_packet(&decode, decoder_ctx, frame, sw_frame, &pkt, output_ctx, hwnd, img_convert_ctx, bgrFrame);
            av_packet_unref(&pkt);
        }
        /* flush the decoder */


        time_t tt1;//这句返回的只是一个时间cuo
        struct tm t1;
        time(&tt1);
        localtime_s(&t1, &tt1);
        printf("%d-%02d-%02d %02d:%02d:%02d\n",
            t1.tm_year + 1900,
            t1.tm_mon + 1,
            t1.tm_mday,
            t1.tm_hour,
            t1.tm_min,
            t1.tm_sec);
    finish:
        if (ret < 0) {
            char buf[1024];
            av_strerror(ret, buf, sizeof(buf));
            fprintf(stderr, "%s\n", buf);
        }

        avformat_close_input(&pInputFormatCtx);

        av_frame_free(&frame);
        av_frame_free(&sw_frame);

        avcodec_free_context(&decoder_ctx);

        av_buffer_unref(&decode.hw_device_ref);

        avio_close(output_ctx);
 
    return 0;
}

int main()
{
    HINSTANCE hInstance;
    hInstance = GetModuleHandle(NULL);
    WNDCLASSEX wce = { 0 };
    wce.cbSize = sizeof(wce);
    wce.cbClsExtra = 0;
    wce.cbWndExtra = 0;
    wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wce.hCursor = NULL;
    wce.hIcon = NULL;
    wce.hIconSm = NULL;
    wce.hInstance = hInstance;
    wce.lpfnWndProc = WinProc;
    wce.lpszClassName = L"Main";
    wce.lpszMenuName = NULL;
    wce.style = CS_HREDRAW | CS_VREDRAW;
    ATOM nAtom = RegisterClassEx(&wce);
    if (!nAtom)
    {
        MessageBox(NULL, L"RegisterClassEx失败", L"错误", MB_OK);
        return 0;
    }
    const char*  szStr1 = "Main";
    WCHAR wszClassName[256];
    memset(wszClassName, 0, sizeof(wszClassName));
    MultiByteToWideChar(CP_ACP, 0, szStr1, strlen(szStr1) + 1, wszClassName,
        sizeof(wszClassName) / sizeof(wszClassName[0]));
    HWND hwnd = CreateWindow(wszClassName, L"视频播放", WS_OVERLAPPEDWINDOW, 38, 20, 640, 480, NULL, NULL, hInstance, NULL);
    // 显示窗口  
    ShowWindow(hwnd, SW_SHOW);
    // 更新窗口  
    UpdateWindow(hwnd);

    //init

    pthread_t t1;
    pthread_create(&t1, NULL, VideoReadThread1, hwnd);

    // 消息循环  
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    } 
    return 0;

测试结果

88m 1280*720的硬解码 1分41s 内存250m左右
88m 1280*720的软解 1分31s 内存90m左右

如果在qt内可以不用转换之间用openggl显示nv12的 

 

 

讨论群261074724

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的示例代码,可以将 OpenCV 的 `cv::Mat` 对象转换为 FFmpeg 的 `AVFrame`,并将其编码为 YUV 420P 格式。 ```c++ #include <opencv2/opencv.hpp> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> int main() { // 初始化 FFmpeg av_register_all(); // 创建格式上下文 AVFormatContext* format_ctx = avformat_alloc_context(); if (!format_ctx) { std::cerr << "Failed to allocate format context" << std::endl; return -1; } // 设置输出格式 AVOutputFormat* output_fmt = av_guess_format("mp4", nullptr, nullptr); if (!output_fmt) { std::cerr << "Failed to guess output format" << std::endl; return -1; } format_ctx->oformat = output_fmt; // 打开输出文件 AVIOContext* io_ctx = nullptr; if (avio_open(&io_ctx, "output.mp4", AVIO_FLAG_WRITE) < 0) { std::cerr << "Failed to open output file" << std::endl; return -1; } format_ctx->pb = io_ctx; // 创建视频流 AVStream* video_stream = avformat_new_stream(format_ctx, nullptr); if (!video_stream) { std::cerr << "Failed to create video stream" << std::endl; return -1; } // 设置编码器参数 AVCodecParameters* codec_params = video_stream->codecpar; codec_params->codec_type = AVMEDIA_TYPE_VIDEO; codec_params->codec_id = output_fmt->video_codec; codec_params->width = 640; codec_params->height = 480; codec_params->format = AV_PIX_FMT_YUV420P; // 查找编码器 AVCodec* codec = avcodec_find_encoder(output_fmt->video_codec); if (!codec) { std::cerr << "Failed to find encoder" << std::endl; return -1; } // 创建编码器上下文 AVCodecContext* codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { std::cerr << "Failed to allocate codec context" << std::endl; return -1; } codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO; codec_ctx->width = codec_params->width; codec_ctx->height = codec_params->height; codec_ctx->pix_fmt = codec_params->format; codec_ctx->time_base = {1, 25}; // 打开编码器 if (avcodec_open2(codec_ctx, codec, nullptr) < 0) { std::cerr << "Failed to open codec" << std::endl; return -1; } // 创建帧 AVFrame* frame = av_frame_alloc(); if (!frame) { std::cerr << "Failed to allocate frame" << std::endl; return -1; } frame->format = codec_ctx->pix_fmt; frame->width = codec_ctx->width; frame->height = codec_ctx->height; // 分配帧数据空间 if (av_frame_get_buffer(frame, 0) < 0) { std::cerr << "Failed to allocate frame data" << std::endl; return -1; } // 创建格式转换器 SwsContext* sws_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, AV_PIX_FMT_BGR24, codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr); if (!sws_ctx) { std::cerr << "Failed to create format converter" << std::endl; return -1; } // 读取输入帧 cv::Mat input_frame = cv::imread("input.jpg"); if (input_frame.empty()) { std::cerr << "Failed to read input frame" << std::endl; return -1; } // 转换输入帧 uint8_t* input_data[AV_NUM_DATA_POINTERS] = {0}; input_data[0] = input_frame.data; int input_linesize[AV_NUM_DATA_POINTERS] = {0}; input_linesize[0] = input_frame.step; sws_scale(sws_ctx, input_data, input_linesize, 0, codec_ctx->height, frame->data, frame->linesize); // 编码帧 AVPacket pkt; av_init_packet(&pkt); pkt.data = nullptr; pkt.size = 0; int got_packet = 0; if (avcodec_encode_video2(codec_ctx, &pkt, frame, &got_packet) < 0) { std::cerr << "Failed to encode frame" << std::endl; return -1; } // 写入输出文件 if (got_packet) { av_packet_rescale_ts(&pkt, codec_ctx->time_base, video_stream->time_base); pkt.stream_index = video_stream->index; if (av_interleaved_write_frame(format_ctx, &pkt) < 0) { std::cerr << "Failed to write packet" << std::endl; return -1; } av_packet_unref(&pkt); } // 写入文件尾 av_write_trailer(format_ctx); // 释放资源 avcodec_free_context(&codec_ctx); av_frame_free(&frame); avio_closep(&format_ctx->pb); avformat_free_context(format_ctx); sws_freeContext(sws_ctx); return 0; } ``` 需要注意的是,上述代码中的 `AV_PIX_FMT_BGR24` 表示输入图像的像素格式,如果您的输入图像格式不是 BGR24,需要相应地修改代码。另外,上述代码中的像素格式编码为 YUV420P,如果您需要使用其他像素格式,也需要相应地修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值