ffplay学习记录02

2017-08-07  本文已影响69人  zjunchao

使用SDL2.0在mac OS上渲染视频画面

  1. SDL下载:
    使用 SDL 来渲染视频到屏幕。SDL 是 Simple Direct Layer 的缩写,是一个优秀的跨平台多媒体库,你可以从 http://www.libsdl.org 下载 SDL 的库。需要讲SDL导入/Library/Frameworks目录下,否则在SDL编译时会提示Image no found.

  2. SDL 渲染的相关代码

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
        fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
        exit(1);
    }
//2. create window, Make a screen to put our video
    screen = SDL_CreateWindow(
                              "FFmpeg Tutorial",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              pCodecCtx->width,
                              pCodecCtx->height,
                              0
                              );
    
    if (!screen) {
        fprintf(stderr, "SDL: could not create window - exiting\n");
        exit(1);
    }
    // 3. create Render
    renderer = SDL_CreateRenderer(screen, -1, 0);
    if (!renderer) {
        fprintf(stderr, "SDL: could not create renderer - exiting\n");
        exit(1);
    }
    
    //4. create texture, Allocate a place to put our YUV image on that screen
    texture = SDL_CreateTexture(
                                renderer,
                                SDL_PIXELFORMAT_YV12,
                                SDL_TEXTUREACCESS_STREAMING,
                                pCodecCtx->width,
                                pCodecCtx->height
                                );
    if (!texture) {
        fprintf(stderr, "SDL: could not create texture - exiting\n");
        exit(1);
    }
sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
                             pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
                             PIX_FMT_YUV420P,
                             SWS_BILINEAR,
                             NULL,
                             NULL,
                             NULL);
    
    // set up YV12 pixel array (12 bits per pixel)
    yPlaneSz = pCodecCtx->width * pCodecCtx->height;
    uvPlaneSz = pCodecCtx->width * pCodecCtx->height / 4;
    yPlane = (Uint8*)malloc(yPlaneSz);
    uPlane = (Uint8*)malloc(uvPlaneSz);
    vPlane = (Uint8*)malloc(uvPlaneSz);
    if (!yPlane || !uPlane || !vPlane) {
        fprintf(stderr, "Could not allocate pixel buffers - exiting\n");
        exit(1);
    }

 AVPicture pict;
 uvPitch = pCodecCtx->width / 2;

 pict.data[0] = yPlane;
 pict.data[1] = uPlane;
 pict.data[2] = vPlane;
 pict.linesize[0] = pCodecCtx->width;
 pict.linesize[1] = uvPitch;
 pict.linesize[2] = uvPitch;
 
 // Convert the image into YUV format that SDL uses
 sws_scale(sws_ctx, (uint8_t const * const *) pFrame->data,
           pFrame->linesize, 0, pCodecCtx->height, pict.data,
           pict.linesize);

                // 纹理绘制
                SDL_UpdateYUVTexture(
                                     texture,
                                     NULL,
                                     yPlane,
                                     pCodecCtx->width,
                                     uPlane,
                                     uvPitch,
                                     vPlane,
                                     uvPitch
                                     );
                
                SDL_RenderClear(renderer);
                SDL_RenderCopy(renderer, texture, NULL, NULL);
                // 纹理填充到屏幕
                SDL_RenderPresent(renderer);

测试代码:https://github.com/zjunchao/ffmpeg_tutorial/tree/master/tutorial02

上一篇 下一篇

猜你喜欢

热点阅读