[FFmpeg-devel] [PATCH 1/4] lavd: Add SDL2 output device

Lukasz Marek lukasz.m.luki2 at gmail.com
Sun Sep 18 20:32:11 EEST 2016


On 15.09.2016 00:27, Josh de Kock wrote:
> Acked-by: Michael Niedermayer
> Signed-off-by: Josh de Kock <josh at itanimul.li>
> ---
>  configure                |  28 +++-
>  libavdevice/Makefile     |   1 +
>  libavdevice/alldevices.c |   1 +
>  libavdevice/sdl2.c       | 377 +++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 406 insertions(+), 1 deletion(-)
>  create mode 100644 libavdevice/sdl2.c

> +typedef struct {
> +    AVClass *class;
> +    SDL_Window *window;
> +    SDL_Renderer *renderer;
> +    char *window_title;
> +    int window_width, window_height;  /**< size of the window */
> +    int window_fullscreen;
> +    int window_borderless;
> +
> +    SDL_Texture *texture;
> +    int texture_fmt;
> +    SDL_Rect texture_rect;
> +
> +    int inited;
> +    SDL_Thread *event_thread;
> +    SDL_mutex *mutex;
> +    SDL_cond *init_cond;

these 3 are unused

> +    int quit;

this also can be removed

> +} SDLContext;
> +
> +
> +static int sdl2_write_trailer(AVFormatContext *s)
> +{
> +    SDLContext *sdl = s->priv_data;
> +
> +    sdl->quit = 1;

as there is no other thread then it is redundant

> +    if (sdl->texture)
> +        SDL_DestroyTexture(sdl->texture);
> +    sdl->texture = NULL;
> +    if (sdl->event_thread)
> +        SDL_WaitThread(sdl->event_thread, NULL);
> +    sdl->event_thread = NULL;
> +    if (sdl->mutex)
> +        SDL_DestroyMutex(sdl->mutex);
> +    sdl->mutex = NULL;
> +    if (sdl->init_cond)
> +        SDL_DestroyCond(sdl->init_cond);
> +    sdl->init_cond = NULL;
> +
> +    if (sdl->renderer)
> +        SDL_DestroyRenderer(sdl->renderer);
> +    sdl->renderer = NULL;
> +
> +    if (sdl->window)
> +        SDL_DestroyWindow(sdl->window);
> +    sdl->window = NULL;
> +
> +    if (!sdl->inited)
> +        SDL_Quit();

are you sure this is OK?

> +
> +    return 0;
> +}
> +
> +#define SDL_BASE_FLAGS (SDL_SWSURFACE|SDL_WINDOW_RESIZABLE)
> +
> +static int sdl2_write_header(AVFormatContext *s)
> +{
> +    SDLContext *sdl = s->priv_data;
> +    AVStream *st = s->streams[0];
> +    AVCodecParameters *codecpar = st->codecpar;
> +    int i, ret = 0;
> +
> +    if (!sdl->window_title)
> +        sdl->window_title = av_strdup(s->filename);
> +
> +    if (SDL_WasInit(SDL_INIT_VIDEO)) {
> +        av_log(s, AV_LOG_WARNING,
> +               "SDL video subsystem was already inited, you could have multiple SDL outputs. This may cause unknown behaviour.\n");
> +        sdl->inited = 1;
> +    }
> +
> +    if (   s->nb_streams > 1
> +        || codecpar->codec_type != AVMEDIA_TYPE_VIDEO
> +        || codecpar->codec_id   != AV_CODEC_ID_RAWVIDEO) {
> +        av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
> +        goto fail;
> +    }
> +
> +    for (i = 0; sdl_texture_format_map[i].format != AV_PIX_FMT_NONE; i++) {
> +        if (sdl_texture_format_map[i].format == codecpar->format) {
> +            sdl->texture_fmt = sdl_texture_format_map[i].texture_fmt;
> +            break;
> +        }
> +    }
> +
> +    if (!sdl->texture_fmt) {
> +        av_log(s, AV_LOG_ERROR,
> +               "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, uyvy422, BGRA\n",

This log message is not clear. It made sense where there was only 3 
planar format. I would remove second part

> +               av_get_pix_fmt_name(codecpar->format));
> +        goto fail;
> +    }
> +
> +    /* resize texture to width and height from the codec context information */
> +    int flags;
> +    flags = SDL_BASE_FLAGS | (sdl->window_fullscreen ? SDL_WINDOW_FULLSCREEN : 0) |
> +                             (sdl->window_borderless ? SDL_WINDOW_BORDERLESS : 0);
> +
> +    /* initialization */
> +    if (!sdl->inited){
> +        if (SDL_Init(SDL_INIT_VIDEO) != 0) {
> +            av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
> +            goto fail;
> +        }
> +    }
> +
> +    sdl->window_width = sdl->texture_rect.w = codecpar->width;
> +    sdl->window_height = sdl->texture_rect.h = codecpar->height;
> +    sdl->texture_rect.x = sdl->texture_rect.y = 0;
> +
> +    if (SDL_CreateWindowAndRenderer(sdl->window_width, sdl->window_height,
> +                                    flags, &sdl->window, &sdl->renderer) != 0){
> +        av_log(sdl, AV_LOG_ERROR, "Couldn't create window and renderer: %s\n", SDL_GetError());
> +        goto fail;
> +    }
> +
> +    SDL_SetWindowTitle(sdl->window, sdl->window_title);
> +
> +    sdl->texture = SDL_CreateTexture(sdl->renderer, sdl->texture_fmt, SDL_TEXTUREACCESS_STREAMING,
> +                                     sdl->window_width, sdl->window_height);
> +
> +    if (!sdl->texture) {
> +        av_log(sdl, AV_LOG_ERROR, "Unable to set create mode: %s\n", SDL_GetError());
> +        goto fail;
> +    }
> +
> +    av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d\n",
> +           codecpar->width, codecpar->height, av_get_pix_fmt_name(codecpar->format),
> +           sdl->window_width, sdl->window_height);
> +
> +    sdl->inited = 1;
> +
> +    return 0;
> +fail:
> +    sdl2_write_trailer(s);
> +    return ret;
> +}
> +
> +static int sdl2_write_packet(AVFormatContext *s, AVPacket *pkt)
> +{
> +    int ret = 0;
> +    SDLContext *sdl = s->priv_data;
> +    AVCodecParameters *codecpar = s->streams[0]->codecpar;
> +    uint8_t *data[4];
> +    int linesize[4];
> +
> +    SDL_Event event;
> +    if (SDL_PollEvent(&event)){

while (SDL_PollEvent(&event)){

> +        switch (event.type) {
> +        case SDL_KEYDOWN:
> +            switch (event.key.keysym.sym) {
> +            case SDLK_ESCAPE:
> +            case SDLK_q:
> +                sdl->quit = 1;

quit may be local variable

> +                break;
> +            default:
> +                break;
> +            }
> +            break;
> +        case SDL_QUIT:
> +            sdl->quit = 1;
> +            break;
> +        case SDL_WINDOWEVENT:
> +            switch(event.window.event){
> +            case SDL_WINDOWEVENT_RESIZED:
> +            case SDL_WINDOWEVENT_SIZE_CHANGED:
> +                sdl->window_width  = event.window.data1;
> +                sdl->window_height = event.window.data2;
> +                compute_texture_rect(s);
> +                break;
> +            default:
> +                break;
> +            }
> +            break;
> +        default:
> +            break;
> +        }
> +    }
> +
> +    if (sdl->quit) {
> +        sdl2_write_trailer(s);
> +        return AVERROR(EIO);
> +    }
> +
> +    av_image_fill_arrays(data, linesize, pkt->data, codecpar->format, codecpar->width, codecpar->height, 1);
> +    SDL_LockMutex(sdl->mutex);

this mutex is not created and make no sense when there is no other thread

> +    switch (sdl->texture_fmt) {
> +    /* case SDL_PIXELFORMAT_ARGB4444:
> +     * case SDL_PIXELFORMAT_RGBA4444:
> +     * case SDL_PIXELFORMAT_ABGR4444:
> +     * case SDL_PIXELFORMAT_BGRA4444:
> +     * case SDL_PIXELFORMAT_ARGB1555:
> +     * case SDL_PIXELFORMAT_RGBA5551:
> +     * case SDL_PIXELFORMAT_ABGR1555:
> +     * case SDL_PIXELFORMAT_BGRA5551:
> +     * case SDL_PIXELFORMAT_ARGB2101010:
> +     */
> +    case SDL_PIXELFORMAT_IYUV:
> +    case SDL_PIXELFORMAT_YUY2:
> +    case SDL_PIXELFORMAT_UYVY:
> +        ret = SDL_UpdateYUVTexture(sdl->texture, NULL,
> +                                   data[0], linesize[0],
> +                                   data[1], linesize[1],
> +                                   data[2], linesize[2]);
> +        break;
> +    case SDL_PIXELFORMAT_RGB332:
> +    case SDL_PIXELFORMAT_RGB444:
> +    case SDL_PIXELFORMAT_RGB555:
> +    case SDL_PIXELFORMAT_BGR555:
> +    case SDL_PIXELFORMAT_RGB565:
> +    case SDL_PIXELFORMAT_BGR565:
> +    case SDL_PIXELFORMAT_RGB24:
> +    case SDL_PIXELFORMAT_BGR24:
> +    case SDL_PIXELFORMAT_RGB888:
> +    case SDL_PIXELFORMAT_RGBX8888:
> +    case SDL_PIXELFORMAT_BGR888:
> +    case SDL_PIXELFORMAT_BGRX8888:
> +    case SDL_PIXELFORMAT_ARGB8888:
> +    case SDL_PIXELFORMAT_RGBA8888:
> +    case SDL_PIXELFORMAT_ABGR8888:
> +    case SDL_PIXELFORMAT_BGRA8888:
> +        ret = SDL_UpdateTexture(sdl->texture, NULL, data[0], linesize[0]);
> +        break;
> +    default:
> +        av_log(NULL, AV_LOG_FATAL, "Unsupported pixel format\n");
> +        ret = -1;
> +        break;
> +    }
> +    SDL_RenderClear(sdl->renderer);
> +    SDL_RenderCopy(sdl->renderer, sdl->texture, NULL, &sdl->texture_rect);
> +    SDL_RenderPresent(sdl->renderer);
> +    SDL_UnlockMutex(sdl->mutex);
> +    return ret;
> +}
> +
> +#define OFFSET(x) offsetof(SDLContext,x)
> +
> +static const AVOption options[] = {
> +    { "window_title",      "set SDL window title",       OFFSET(window_title), AV_OPT_TYPE_STRING,     { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
> +    { "window_size",       "set SDL window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
> +    { "window_fullscreen", "set SDL window fullscreen",  OFFSET(window_fullscreen), AV_OPT_TYPE_BOOL,  { .i64 = 0 },    0, 1, AV_OPT_FLAG_ENCODING_PARAM },
> +    { "window_borderless", "set SDL window border off",  OFFSET(window_borderless), AV_OPT_TYPE_BOOL,  { .i64 = 0 },    0, 1, AV_OPT_FLAG_ENCODING_PARAM },
> +    { NULL },
> +};
> +
> +static const AVClass sdl2_class = {
> +    .class_name = "sdl2 outdev",
> +    .item_name  = av_default_item_name,
> +    .option     = options,
> +    .version    = LIBAVUTIL_VERSION_INT,
> +    .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
> +};
> +
> +AVOutputFormat ff_sdl2_muxer = {
> +    .name           = "sdl2",

just my opinion, but in case sdl1 is dropped I would leave short name as 
"sdl"

> +    .long_name      = NULL_IF_CONFIG_SMALL("SDL2 output device"),
> +    .priv_data_size = sizeof(SDLContext),
> +    .audio_codec    = AV_CODEC_ID_NONE,
> +    .video_codec    = AV_CODEC_ID_RAWVIDEO,
> +    .write_header   = sdl2_write_header,
> +    .write_packet   = sdl2_write_packet,
> +    .write_trailer  = sdl2_write_trailer,
> +    .flags          = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
> +    .priv_class     = &sdl2_class,
> +};
>

In general it is working. Valgrind shows leaks, but I don't see them 
here, so probably inside sdl. My implemenation of sdl window for opengl 
also have leaks from sdl.


More information about the ffmpeg-devel mailing list