ffmpeg4教程12:intel media sdk(qsv)硬解碼的使用方法

討論羣261074724

1.安裝intel media sdk 請對於處理器的版本代號

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

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

 

 


#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左右

不曉得爲啥?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章