diff mbox

[FFmpeg-devel] avdev: Add SDL2 output device

Message ID 1473608292-70980-1-git-send-email-josh@itanimul.li
State Changes Requested
Headers show

Commit Message

Josh Dekker Sept. 11, 2016, 3:38 p.m. UTC
This actually seems to work now, it didn't in the past, so maybe SDL2
was patched or something changed in FFmpeg.

I will make an avfilter of this as well as the outdev.

Signed-off-by: Josh de Kock <josh@itanimul.li>
---
 configure                |  24 ++-
 libavdevice/Makefile     |   1 +
 libavdevice/alldevices.c |   1 +
 libavdevice/sdl2.c       | 373 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 398 insertions(+), 1 deletion(-)
 create mode 100644 libavdevice/sdl2.c

Comments

Michael Niedermayer Sept. 11, 2016, 6:02 p.m. UTC | #1
On Sun, Sep 11, 2016 at 04:38:12PM +0100, Josh de Kock wrote:
> This actually seems to work now, it didn't in the past, so maybe SDL2
> was patched or something changed in FFmpeg.
> 
> I will make an avfilter of this as well as the outdev.
> 
> Signed-off-by: Josh de Kock <josh@itanimul.li>
> ---
>  configure                |  24 ++-
>  libavdevice/Makefile     |   1 +
>  libavdevice/alldevices.c |   1 +
>  libavdevice/sdl2.c       | 373 +++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 398 insertions(+), 1 deletion(-)
>  create mode 100644 libavdevice/sdl2.c

very nice!

Acked-by: Michael

[...]
Clément Bœsch Sept. 11, 2016, 6:04 p.m. UTC | #2
On Sun, Sep 11, 2016 at 08:02:12PM +0200, Michael Niedermayer wrote:
> On Sun, Sep 11, 2016 at 04:38:12PM +0100, Josh de Kock wrote:
> > This actually seems to work now, it didn't in the past, so maybe SDL2
> > was patched or something changed in FFmpeg.
> > 
> > I will make an avfilter of this as well as the outdev.
> > 
> > Signed-off-by: Josh de Kock <josh@itanimul.li>
> > ---
> >  configure                |  24 ++-
> >  libavdevice/Makefile     |   1 +
> >  libavdevice/alldevices.c |   1 +
> >  libavdevice/sdl2.c       | 373 +++++++++++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 398 insertions(+), 1 deletion(-)
> >  create mode 100644 libavdevice/sdl2.c
> 
> very nice!
> 
> Acked-by: Michael
> 

Note that it disables ffplay by default; so I'd better wait for a
consensus with that regard before
Josh Dekker Sept. 11, 2016, 6:05 p.m. UTC | #3
On 11/09/2016 19:04, Clément Bœsch wrote:
> On Sun, Sep 11, 2016 at 08:02:12PM +0200, Michael Niedermayer wrote:
>> On Sun, Sep 11, 2016 at 04:38:12PM +0100, Josh de Kock wrote:
>>> This actually seems to work now, it didn't in the past, so maybe SDL2
>>> was patched or something changed in FFmpeg.
>>>
>>> I will make an avfilter of this as well as the outdev.
>>>
>>> Signed-off-by: Josh de Kock <josh@itanimul.li>
>>> ---
>>>  configure                |  24 ++-
>>>  libavdevice/Makefile     |   1 +
>>>  libavdevice/alldevices.c |   1 +
>>>  libavdevice/sdl2.c       | 373 +++++++++++++++++++++++++++++++++++++++++++++++
>>>  4 files changed, 398 insertions(+), 1 deletion(-)
>>>  create mode 100644 libavdevice/sdl2.c
>>
>> very nice!
>>
>> Acked-by: Michael
>>
>
> Note that it disables ffplay by default; so I'd better wait for a
> consensus with that regard before
>
I will convert ffplay to sdl2, and then resubmit the patchset (probably 
later this week).
Clément Bœsch Sept. 11, 2016, 6:34 p.m. UTC | #4
On Sun, Sep 11, 2016 at 07:05:38PM +0100, Josh de Kock wrote:
> On 11/09/2016 19:04, Clément Bœsch wrote:
> > On Sun, Sep 11, 2016 at 08:02:12PM +0200, Michael Niedermayer wrote:
> > > On Sun, Sep 11, 2016 at 04:38:12PM +0100, Josh de Kock wrote:
> > > > This actually seems to work now, it didn't in the past, so maybe SDL2
> > > > was patched or something changed in FFmpeg.
> > > > 
> > > > I will make an avfilter of this as well as the outdev.
> > > > 
> > > > Signed-off-by: Josh de Kock <josh@itanimul.li>
> > > > ---
> > > >  configure                |  24 ++-
> > > >  libavdevice/Makefile     |   1 +
> > > >  libavdevice/alldevices.c |   1 +
> > > >  libavdevice/sdl2.c       | 373 +++++++++++++++++++++++++++++++++++++++++++++++
> > > >  4 files changed, 398 insertions(+), 1 deletion(-)
> > > >  create mode 100644 libavdevice/sdl2.c
> > > 
> > > very nice!
> > > 
> > > Acked-by: Michael
> > > 
> > 
> > Note that it disables ffplay by default; so I'd better wait for a
> > consensus with that regard before
> > 
> I will convert ffplay to sdl2, and then resubmit the patchset (probably
> later this week).

You can also just disable sdl2 by default until every code relying on sdl1
has a sdl2 alternative so this can be applied earlier.
Moritz Barsnick Sept. 11, 2016, 7 p.m. UTC | #5
Nice, seeing how often this has been pitched in the past.

On Sun, Sep 11, 2016 at 16:38:12 +0100, Josh de Kock wrote:
> This actually seems to work now, it didn't in the past, so maybe SDL2
> was patched or something changed in FFmpeg.

Interesting...

> + * Copyright (c) 2011 Stefano Sabatini

Does this show the age of the patch, or is this from the original sdl1
implementation?

> +    { "window_fullscreen", "set SDL window fullscreen", OFFSET(window_fullscreen), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
> +    { "window_borderless", "set SDL window border off", OFFSET(window_fullscreen), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },

a) These should be _BOOL nowadays.
b) There's a copy/paste error there, disabling window_borderless.

Moritz
Josh Dekker Sept. 11, 2016, 7:11 p.m. UTC | #6
On 11/09/2016 20:00, Moritz Barsnick wrote:
> Nice, seeing how often this has been pitched in the past.
>
> On Sun, Sep 11, 2016 at 16:38:12 +0100, Josh de Kock wrote:
>> This actually seems to work now, it didn't in the past, so maybe SDL2
>> was patched or something changed in FFmpeg.
>
> Interesting...
>
>> + * Copyright (c) 2011 Stefano Sabatini
>
> Does this show the age of the patch, or is this from the original sdl1
> implementation?
>
I started with the SDL1 implementation then after a few failed attempts 
to convert it over I deleted everything except for the settings and the 
header, then rewrote everything, I've updated the header in the latest 
iteration.

>> +    { "window_fullscreen", "set SDL window fullscreen", OFFSET(window_fullscreen), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
>> +    { "window_borderless", "set SDL window border off", OFFSET(window_fullscreen), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
>
> a) These should be _BOOL nowadays.
Ok.
> b) There's a copy/paste error there, disabling window_borderless.
Thanks!

--
Josh
diff mbox

Patch

diff --git a/configure b/configure
index b11ca7f..b3b43a9 100755
--- a/configure
+++ b/configure
@@ -292,6 +292,7 @@  External library support:
   --disable-schannel       disable SChannel SSP, needed for TLS support on
                            Windows if openssl and gnutls are not used [autodetect]
   --disable-sdl            disable sdl [autodetect]
+  --disable-sdl2           disable sdl2 [autodetect]
   --disable-securetransport disable Secure Transport, needed for TLS support
                            on OSX if openssl and gnutls are not used [autodetect]
   --enable-x11grab         enable X11 grabbing (legacy) [no]
@@ -1548,6 +1549,7 @@  EXTERNAL_LIBRARY_LIST="
     openssl
     schannel
     sdl
+    sdl2
     securetransport
     videotoolbox
     x11grab
@@ -2022,6 +2024,7 @@  HAVE_LIST="
     perl
     pod2man
     sdl
+    sdl2
     section_data_rel_ro
     texi2html
     threads
@@ -2945,6 +2948,7 @@  pulse_outdev_deps="libpulse"
 qtkit_indev_extralibs="-framework QTKit -framework Foundation -framework QuartzCore"
 qtkit_indev_select="qtkit"
 sdl_outdev_deps="sdl"
+sdl2_outdev_deps="sdl2"
 sndio_indev_deps="sndio_h"
 sndio_outdev_deps="sndio_h"
 v4l_indev_deps="linux_videodev_h"
@@ -5850,7 +5854,24 @@  if enabled gcrypt; then
     fi
 fi
 
-if ! disabled sdl; then
+
+SDL2_CONFIG="${cross_prefix}sdl2-config"
+if check_pkg_config sdl2 SDL_events.h SDL_PollEvent; then
+    check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) >= 0x020001" $sdl2_cflags &&
+    check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) < 0x020100" $sdl2_cflags &&
+    enable sdl2
+else
+  if "${SDL2_CONFIG}" --version > /dev/null 2>&1; then
+    sdl2_cflags=$("${SDL2_CONFIG}" --cflags)
+    sdl2_libs=$("${SDL2_CONFIG}" --libs)
+    check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) >= 0x020001" $sdl2_cflags &&
+    check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) < 0x020100" $sdl2_cflags &&
+    enable sdl2
+  fi
+fi
+enabled sdl2 && add_cflags $sdl2_cflags && add_extralibs $sdl2_libs
+
+if ! disabled sdl && ! enabled sdl2; then
     SDL_CONFIG="${cross_prefix}sdl-config"
     if check_pkg_config sdl SDL_events.h SDL_PollEvent; then
         check_cpp_condition SDL.h "(SDL_MAJOR_VERSION<<16 | SDL_MINOR_VERSION<<8 | SDL_PATCHLEVEL) >= 0x010201" $sdl_cflags &&
@@ -6480,6 +6501,7 @@  echo "network support           ${network-no}"
 echo "threading support         ${thread_type-no}"
 echo "safe bitstream reader     ${safe_bitstream_reader-no}"
 echo "SDL support               ${sdl-no}"
+echo "SDL2 support              ${sdl2-no}"
 echo "opencl enabled            ${opencl-no}"
 echo "JNI support               ${jni-no}"
 echo "texi2html enabled         ${texi2html-no}"
diff --git a/libavdevice/Makefile b/libavdevice/Makefile
index 585827b..1c4b4d3 100644
--- a/libavdevice/Makefile
+++ b/libavdevice/Makefile
@@ -41,6 +41,7 @@  OBJS-$(CONFIG_PULSE_OUTDEV)              += pulse_audio_enc.o \
                                             pulse_audio_common.o
 OBJS-$(CONFIG_QTKIT_INDEV)               += qtkit.o
 OBJS-$(CONFIG_SDL_OUTDEV)                += sdl.o
+OBJS-$(CONFIG_SDL2_OUTDEV)               += sdl2.o
 OBJS-$(CONFIG_SNDIO_INDEV)               += sndio_dec.o sndio.o
 OBJS-$(CONFIG_SNDIO_OUTDEV)              += sndio_enc.o sndio.o
 OBJS-$(CONFIG_V4L2_INDEV)                += v4l2.o v4l2-common.o timefilter.o
diff --git a/libavdevice/alldevices.c b/libavdevice/alldevices.c
index 26aecf2..c0a9d9a 100644
--- a/libavdevice/alldevices.c
+++ b/libavdevice/alldevices.c
@@ -64,6 +64,7 @@  void avdevice_register_all(void)
     REGISTER_INOUTDEV(PULSE,            pulse);
     REGISTER_INDEV   (QTKIT,            qtkit);
     REGISTER_OUTDEV  (SDL,              sdl);
+    REGISTER_OUTDEV  (SDL2,             sdl2);
     REGISTER_INOUTDEV(SNDIO,            sndio);
     REGISTER_INOUTDEV(V4L2,             v4l2);
 //    REGISTER_INDEV   (V4L,              v4l
diff --git a/libavdevice/sdl2.c b/libavdevice/sdl2.c
new file mode 100644
index 0000000..8959ebc
--- /dev/null
+++ b/libavdevice/sdl2.c
@@ -0,0 +1,373 @@ 
+/*
+ * Copyright (c) 2011 Stefano Sabatini
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * libSDL output device
+ */
+
+#include <SDL.h>
+#include <SDL_thread.h>
+
+#include "libavutil/avstring.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/time.h"
+#include "avdevice.h"
+
+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;
+    int quit;
+} SDLContext;
+
+static const struct sdl_texture_format_entry {
+    enum AVPixelFormat format; int texture_fmt;
+} sdl_texture_format_map[] = {
+    { AV_PIX_FMT_RGB8, SDL_PIXELFORMAT_RGB332 },
+    { AV_PIX_FMT_RGB444, SDL_PIXELFORMAT_RGB444 },
+    { AV_PIX_FMT_RGB555, SDL_PIXELFORMAT_RGB555 },
+    { AV_PIX_FMT_BGR555, SDL_PIXELFORMAT_BGR555 },
+    // { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ARGB4444 }, // these aren't implemented in FFmpeg
+    // { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_RGBA4444 }, // but keeping them here in-case they
+    // { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ABGR4444 }, // are
+    // { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_BGRA4444 },
+    // { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ARGB1555 },
+    // { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_RGBA5551 },
+    // { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ABGR1555 },
+    // { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_BGRA5551 },
+    { AV_PIX_FMT_RGB565, SDL_PIXELFORMAT_RGB565 },
+    { AV_PIX_FMT_BGR565, SDL_PIXELFORMAT_BGR565 },
+    { AV_PIX_FMT_RGB24, SDL_PIXELFORMAT_RGB24 },
+    { AV_PIX_FMT_BGR24, SDL_PIXELFORMAT_BGR24 },
+    { AV_PIX_FMT_RGB24, SDL_PIXELFORMAT_RGB888 }, // is this right?
+    { AV_PIX_FMT_RGB0, SDL_PIXELFORMAT_RGBX8888 },
+    { AV_PIX_FMT_BGR24, SDL_PIXELFORMAT_BGR888 }, // is this right?
+    { AV_PIX_FMT_BGR0, SDL_PIXELFORMAT_BGRX8888 },
+    { AV_PIX_FMT_ARGB, SDL_PIXELFORMAT_ARGB8888 },
+    { AV_PIX_FMT_RGBA, SDL_PIXELFORMAT_RGBA8888 },
+    { AV_PIX_FMT_ABGR, SDL_PIXELFORMAT_ABGR8888 },
+    { AV_PIX_FMT_BGRA, SDL_PIXELFORMAT_BGRA8888 },
+    // { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ARGB2101010 }, // not implemented 
+    { AV_PIX_FMT_YUV420P, SDL_PIXELFORMAT_IYUV },
+    { AV_PIX_FMT_YUYV422, SDL_PIXELFORMAT_YUY2 },
+    { AV_PIX_FMT_UYVY422, SDL_PIXELFORMAT_UYVY },
+    { AV_PIX_FMT_NONE,    0                },
+};
+
+static void compute_texture_rect(AVFormatContext *s)
+{
+    AVRational sar, dar; /* sample and display aspect ratios */
+    SDLContext *sdl = s->priv_data;
+    AVStream *st = s->streams[0];
+    AVCodecParameters *codecpar = st->codecpar;
+    SDL_Rect *texture_rect = &sdl->texture_rect;
+
+    /* compute texture width and height from the codec context information */
+    sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
+    dar = av_mul_q(sar, (AVRational){ codecpar->width, codecpar->height });
+
+    /* we suppose the screen has a 1/1 sample aspect ratio */
+    if (sdl->window_width && sdl->window_height) {
+        /* fit in the window */
+        if (av_cmp_q(dar, (AVRational){ sdl->window_width, sdl->window_height }) > 0) {
+            /* fit in width */
+            texture_rect->w = sdl->window_width;
+            texture_rect->h = av_rescale(texture_rect->w, dar.den, dar.num);
+        } else {
+            /* fit in height */
+            texture_rect->h = sdl->window_height;
+            texture_rect->w = av_rescale(texture_rect->h, dar.num, dar.den);
+        }
+    } else {
+        if (sar.num > sar.den) {
+            texture_rect->w = codecpar->width;
+            texture_rect->h = av_rescale(texture_rect->w, dar.den, dar.num);
+        } else {
+            texture_rect->h = codecpar->height;
+            texture_rect->w = av_rescale(texture_rect->h, dar.num, dar.den);
+        }
+        sdl->window_width  = texture_rect->w;
+        sdl->window_height = texture_rect->h;
+    }
+
+    texture_rect->x = (sdl->window_width  - texture_rect->w) / 2;
+    texture_rect->y = (sdl->window_height - texture_rect->h) / 2;
+}
+
+static int sdl2_write_trailer(AVFormatContext *s)
+{
+    SDLContext *sdl = s->priv_data;
+
+    sdl->quit = 1;
+
+    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();
+
+    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",
+               av_get_pix_fmt_name(codecpar->format));
+        goto fail;
+    }
+
+    /* resize texture to width and height from the codec context information */
+
+    int 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)){
+        switch (event.type) {
+            case SDL_KEYDOWN:
+                switch (event.key.keysym.sym) {
+                    case SDLK_ESCAPE:
+                    case SDLK_q:
+                        sdl->quit = 1;
+                        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);
+    switch (sdl->texture_fmt) {
+        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_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_RGB565:
+        case SDL_PIXELFORMAT_BGR565:
+        case SDL_PIXELFORMAT_RGB24:
+        case SDL_PIXELFORMAT_BGR24:
+        case SDL_PIXELFORMAT_RGB888: // is this right?
+        case SDL_PIXELFORMAT_RGBX8888:
+        case SDL_PIXELFORMAT_BGR888: // is this right?
+        case SDL_PIXELFORMAT_BGRX8888:
+        case SDL_PIXELFORMAT_ARGB8888:
+        case SDL_PIXELFORMAT_RGBA8888:
+        case SDL_PIXELFORMAT_ABGR8888:
+        case SDL_PIXELFORMAT_BGRA8888:
+        // case SDL_PIXELFORMAT_ARGB2101010:
+            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_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
+    { "window_borderless", "set SDL window border off", OFFSET(window_fullscreen), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, 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",
+    .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,
+};