c++ 从HWND创建窗口时,SDL显示h264视频不清晰

sz81bmfz  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(212)

在Windows开发环境中:
1.如果您使用SDL_CreateWindow()创建窗口,作为控制台程序运行,并渲染h264视频,则很清楚。
1.但是如果你用SDL_CreateWindowFrom()绑定MFC窗口,渲染h264就不清楚了,这让我很困惑。
我该怎么测试呢?

  • 我将h264帧数据的每一帧保存为**.264**文件,每个文件都有0x00000001 start_code。
  • 然后我通过程序读取h264文件并将其转换为AVFrame
  • 然后将其交给SDL进行渲染。

下面是有关SDL的代码:

class EMSDL {
public:
    EMSDL() {}
    ~EMSDL() {}

    static void delay(long ms) {
        SDL_Delay(ms);
    }

    void initSDL(int width, int height) {
        
        int ret = SDL_Init(SDL_INIT_VIDEO);
        if (ret)
        {
            LOG << "Failed";
            return;
        }

        //for MFC HWND
        //pSDLWindow = SDL_CreateWindowFrom(hWnd);

        //for console
        pSDLWindow = SDL_CreateWindow("EMEET_SDL",
            0,
            0,
            width,
            height,
            SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
        if (!pSDLWindow)
        {
            LOG << "Failed";
            return;
        }

        pSDLRenderer = SDL_CreateRenderer(pSDLWindow, -1, SDL_RENDERER_PRESENTVSYNC);
        if (!pSDLRenderer)
        {
            LOG << "Failed";
            return;
        }

        pSDLTexture = SDL_CreateTexture(pSDLRenderer,
            SDL_PIXELFORMAT_IYUV,//SDL_PIXELFORMAT_YV12
            SDL_TEXTUREACCESS_STREAMING,
            width,
            height);

        if (!pSDLTexture)
        {
            LOG << "Failed";
            return;
        }
    }

    void uninitSDL() {

        if (pSDLTexture) {
            SDL_DestroyTexture(pSDLTexture);
            pSDLTexture = nullptr;
        }
        if (pSDLRenderer) {
            SDL_DestroyRenderer(pSDLRenderer);
            pSDLRenderer = nullptr;
        }
        if (pSDLWindow) {
            SDL_DestroyWindow(pSDLWindow);
            pSDLWindow = nullptr;
        }
        SDL_Quit();
    }

    void render(AVFrame* frame) {

        SDL_UpdateYUVTexture(pSDLTexture,
            NULL,
            frame->data[0], frame->linesize[0],
            frame->data[1], frame->linesize[1],
            frame->data[2], frame->linesize[2]);

        SDL_RenderClear(pSDLRenderer);
        // Texture to Renderer
        SDL_Rect        sdlRect;
        sdlRect.x = 0;
        sdlRect.y = 0;
        sdlRect.w = frame->width;
        sdlRect.h = frame->height;

        SDL_RenderCopy(pSDLRenderer, pSDLTexture, &sdlRect, &sdlRect);

        // update and show
        SDL_RenderPresent(pSDLRenderer);
        //
        //SDL_PollEvent(&event);
    } 

    bool checkQuitEvent() {

        //handle events
        int myEvent = SDL_PollEvent(&event);
        //SDL_WaitEvent(&event);
        switch (event.type) {
        case SDL_KEYDOWN:
        {
            printf("key down! key code = %d, key name= %s\n", event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
            if (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q) {
                return true;
            }
        }break;
        case SDL_QUIT: {
            return true;
        }break;
        }

        return false;
    }

protected:
    SDL_Window* pSDLWindow = nullptr;
    SDL_Renderer* pSDLRenderer = nullptr;
    SDL_Surface* pSDLSurface = nullptr;
    SDL_Texture* pSDLTexture = nullptr;
    SDL_Event event;
};

最明显的区别是在握手的地方,图像不清晰(SDL_CreateWindowFrom)。

wkyowqbh

wkyowqbh1#

我发现问题不出在SDL上,而是我在反复调用avcodec_send_packet和avcodec_receive_frame。
这个问题已经解决了。

相关问题