ffmpeg编程(二)

这篇主要讲如何把视频文件播放出来

如果对YUV没有基础的可以看下:http://www.cnblogs.com/nanguabing/archive/2012/04/12/2443485.html

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) {
                fprintf(stderr, "Could not initialize SDL - %s/n",
                        SDL_GetError());
                exit(1);
            }

SDL_Init()函数告诉了SDL库,哪些特性我们将要用到,SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER分别是视频,音频和时间。

 

            SDL_Surface *screen;
            screen = SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0,
                    0);
            if (!screen) {
                fprintf(stderr, "SDL: could not set video mode - exiting/n");
                exit(1);
            }

SDL_Surface是显示图像的容器。
函数SDL_SetVideoMode(pCodecCtx->width, pCodecCtx->height, 0,0);来创建一个窗口,这个函数包含四个参数,依次是窗口的宽度、高度、单个像素占用位数和一个标记变量。其中第三个参数最为简单的方法是设置为0,这样它就为当前默认的显示设置)。最后一个参数用SDL_HWSURFACE和SDL_DOUBLEBUF.联起来使用要用或操作符: SDL_HWSURFACE | SDL_DOUBLEBUF。
SDL_SetVideoMode()函数不仅仅是创建了一个窗口,它还创建了一块内存区域叫"screen buffer",用来显示图像。这块区域负责显示画面到屏幕,标记变量SDL_HWSURFACE表示在显存里头创建缓存;SDL_DOUBLEBUF表示我要创建两个缓存区域,一个用作前端缓存,这里存放的就是我们正在显示到屏幕的内容;另一个用作后端缓存,这里存放的是我们将要显示到屏幕上的内容。当我们显示将要显示的内容时候,只需要交换前端缓存和后端缓存,这样后端缓存的内容就被显示出来。(译者注:也就是说前端缓存又变成了后端缓存,可以用来存放下一幅要显示的画面)这项技术叫做双缓存,用来加速图像的渲染过程。

  SDL_Overlay *bmp;
    bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,
                    SDL_YV12_OVERLAY, screen);

SDL_Overlay用于存储YUV。 

函数SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen);负责创建YUV,参数分别是宽度,高度,YUV格式和SDL_Surface。

SDL_Rect rect;
            if (frameFinished) {
                SDL_LockYUVOverlay(bmp);
                AVPicture pict;
         //把YUV赋值给AVPicture pict.data[
0] = bmp->pixels[0]; pict.data[1] = bmp->pixels[2]; pict.data[2] = bmp->pixels[1]; pict.linesize[0] = bmp->pitches[0]; pict.linesize[1] = bmp->pitches[2]; pict.linesize[2] = bmp->pitches[1]; // Convert the image into YUV format that SDL uses img_convert(&pict, PIX_FMT_YUV420P, (AVPicture *) pFrame, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height); SDL_UnlockYUVOverlay(bmp); rect.x = 0; rect.y = 0; rect.w = pCodecCtx->width; rect.h = pCodecCtx->height; SDL_DisplayYUVOverlay(bmp, &rect); }

SDL_Rect定义了屏幕上的一个矩形区域。它被 SDL_BlitSurface() 等视频函数用来定义贴图区域。

 SDL_LockYUVOverlay(bmp);对YUV加锁。

自定义函数img_convert(&pict, PIX_FMT_YUV420P, (AVPicture *) pFrame, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);负责把YUV转换成image。

SDL_UnlockYUVOverlay(bmp);对YUV解锁。

函数SDL_DisplayYUVOverlay(bmp, &rect);负责显示图片。

 

SDL_Event event;
            av_free_packet(&packet);
            SDL_PollEvent(&event);
            switch (event.type) {
            case SDL_QUIT:
                SDL_Quit();
                exit(0);
                break;
            default:
                break;
            }

 

视频退出功能。

c文件下载

http://download.csdn.net/detail/wenwei19861106/4220091

posted on 2012-04-12 13:48  南瓜饼  阅读(4217)  评论(4编辑  收藏  举报

导航