(转译)用FFmpeg和SDL写播放器08软件缩放

指导 8:软件缩放
 
软件缩放库 libswscale
 
  近来 ffmpeg 添加了新的接口:libswscale 来处理图像缩放。 但是在前面我们使用 img_convert 来把 RGB 转换成 YUV12,我们现在使用新的接口。新接口更加标准和快速,而且我相信里面有了 MMX 优化代码。换句话说,它是做更好的缩放方式。
  我们将用来缩放的基本函数是 sws_scale。但一开始,我们必需建立一个 SwsContext 的概念。这将让我们进行想要的转换,然后把它传递给 sws_scale函数。类似于在 SQL 中的预备阶段或者是在 Python 中编译的规则表达式 regexp。 要准备这个上下文,我们使sws_getContext 函数,它需要我们源的宽度和高度,我们想要缩放的宽度和高度,源的格式和想要转换成的格式,同时还有一些其它的参数和标志。然后我们像使用 img_convert 一样来使用 sws_scale 函数,唯 一不同的是我们传递给的是 SwsContext:
 
#include <ffmpeg/swscale.h> // include the header!

int queue_picture(VideoState *is, AVFrame *pFrame, double pts) {

  static struct SwsContext *img_convert_ctx;
  ...

  if(vp->bmp) {

    SDL_LockYUVOverlay(vp->bmp);
    
    dst_pix_fmt = PIX_FMT_YUV420P;
    /* point pict at the queue */

    pict.data[0] = vp->bmp->pixels[0];
    pict.data[1] = vp->bmp->pixels[2];
    pict.data[2] = vp->bmp->pixels[1];
    
    pict.linesize[0] = vp->bmp->pitches[0];
    pict.linesize[1] = vp->bmp->pitches[2];
    pict.linesize[2] = vp->bmp->pitches[1];
    
    // Convert the image into YUV format that SDL uses
    if(img_convert_ctx == NULL) {
      int w = is->video_st->codec->width;
      int h = is->video_st->codec->height;
      img_convert_ctx = sws_getContext(w, h, 
                        is->video_st->codec->pix_fmt, 
                        w, h, dst_pix_fmt, SWS_BICUBIC, 
                        NULL, NULL, NULL);
      if(img_convert_ctx == NULL) {
    fprintf(stderr, "Cannot initialize the conversion context!\n");
    exit(1);
      }
    }
    sws_scale(img_convert_ctx, pFrame->data, 
              pFrame->linesize, 0, 
              is->video_st->codec->height, 
              pict.data, pict.linesize);

  我们把新的缩放器放到了合适的位置。希望这会让你知道 libswscale能做什么。 就这样,我们做完了!编译我们的播放器:

gcc -o tutorial08 tutorial08.c -lavutil -lavformat -lavcodec -lz -lm `sdl-config --cflags --libs`


作者:半山
出处:http://www.cnblogs.com/xdao/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/xdao/p/ffmpeg_tutor08.html