An ffmpeg and SDL Tutorial
Tutorial 08: Software ScalingCode:
libswscale
ffmpeg has recently added a new interface, libswscale to handle image scaling. Whereas before in our player we would use to go from RGB to YUV12, we now use the new interface. This new interface is more modular, faster, and I believe has MMX optimization stuff. In other words, it's the preferred way to do scaling.
The basic function we're going to use to scale is . But first, we're going to have to set up what's called an SwsContext. This allows us to compile the conversion we want, and then pass that in later to sws_scale. It's kind of like a prepared statement in SQL or a compiled regexp in Python. To prepare this context, we use the function, which is going to want our source width and height, our desired width and height, the source format and desired format, along with some other options and flags. Then we use sws_scale the same way as except we pass it our SwsContext:
#include // include the header! int queue_picture(VideoState *is, *pFrame, double pts) { static struct SwsContext *_ctx; ... if(vp->bmp) { (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(_ctx == NULL) { int w = is->video_st->codec->width; int h = is->video_st->codec->height; _ctx = (w, h, is->video_st->codec->pix_fmt, w, h, dst_pix_fmt, SWS_BICUBIC, NULL, NULL, NULL); if(_ctx == NULL) { fprintf(stderr, "Cannot initialize the conversion context!\n"); exit(1); } } (_ctx, pFrame->data, pFrame->linesize, 0, is->video_st->codec->height, pict.data, pict.linesize); and we have our new scaler in place. Hopefully this gives you a good idea of what libswscale can do.
That's it! We're done! Go ahead and compile your player:
gcc -o tutorial08 tutorial08.c -lavutil -lavformat -lavcodec -lz -lm `sdl-config --cflags --libs` and enjoy your movie player made in less than 1000 lines of C!
Of course, there's a lot of things we glanced over that we could add.
阅读(1252) | 评论(0) | 转发(0) |