[SDL] SDl 2.0 and ffmpeg

Gabriele Greco gabriele.greco at darts.it
Fri Mar 9 07:15:41 PST 2012


On Fri, Mar 9, 2012 at 3:02 PM, David martin <vilanew at gmail.com> wrote:

> Thanks for the link.
> I'm reading a video streaming (not a single BMp image) so not sure how
> this applies to my code.
> ffmpeg reads in in yuvp42 format (see output from ffmpeg dump_format)
>
> > ~/workspace/test1/Release $ ./test2 toto.mpeg
> Input #0, mpeg, from 'toto.mpeg':
>  Duration: 00:00:04.83, start: 1.000000, bitrate: 606 kb/s
>    Stream #0:0[0x1e0]: Video: mpeg1video, yuv420p, 352x288 [SAR 178:163
> DAR 1958:1467], 104857 kb/s, 60 fps, 60 tbr, 90k tbn, 60 tbc
>
>
> So the question is do i need to create a surface from each image read
> frame ? How would you do that ?
>


You should create only a streaming texture, with a command like this:

texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12,
SDL_TEXTUREACCESS_STREAMING, frame_width, frame_height))

Then you should copy the avpicture you get from ffmpeg with a function
similat to this one (was a test with an older 1.3 version of SDL I don't
know if it still works with recent 2.0 builds):

void display_picture(AVPicture *picture)
{
    unsigned char *pixels;
    int pitch;

    if(texture && !SDL_LockTexture(texture, NULL, (void **)&pixels, &pitch)
) {

        if(pitch == picture->linesize[0]) {
            int size = pitch * frame_height;

            memcpy(pixels, picture->data[0], size);
            memcpy(pixels + size, picture->data[2], size / 4);
            memcpy(pixels + size * 5 / 4, picture->data[1], size / 4);
        }
        else {
            register unsigned char *y1,*y2,*y3,*i1,*i2,*i3;
            int i;
            y1 = pixels;
            y3 = pixels + pitch * frame_height; // invertiti xche' avevo i
colori sballati!
            y2 = pixels + pitch * frame_height * 5 / 4;

            i1=picture->data[0];
            i2=picture->data[1];
            i3=picture->data[2];

            for (i = 0; i<(frame_height/2); i++) {
                memcpy(y1,i1,pitch);
                i1+=picture->linesize[0];
                y1+=pitch;
                memcpy(y1,i1,pitch);

                memcpy(y2,i2,pitch / 2);
                memcpy(y3,i3,pitch / 2);

                y1+=pitch;
                y2+=pitch / 2;
                y3+=pitch / 2;
                i1+=picture->linesize[0];
                i2+=picture->linesize[1];
                i3+=picture->linesize[2];
            }
        }
        SDL_UnlockTexture(texture);
    }

    SDL_RenderCopy(renderer, texture, NULL, &rect);
    SDL_RenderPresent(renderer);
}



-- 
Bye,
 Gabry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20120309/cd75815d/attachment.htm>


More information about the SDL mailing list