configure: fix build
[ffmpeg.git] / libavdevice / sdl2.c
1 /*
2  * Copyright (c) 2016 Josh de Kock
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * libSDL2 output device
24  */
25
26 #include <SDL.h>
27 #include <SDL_thread.h>
28
29 #include "libavutil/avstring.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/time.h"
35 #include "avdevice.h"
36
37 typedef struct {
38     AVClass *class;
39     SDL_Window *window;
40     SDL_Renderer *renderer;
41     char *window_title;
42     int window_width, window_height;  /**< size of the window */
43     int window_fullscreen;
44     int window_borderless;
45
46     SDL_Texture *texture;
47     int texture_fmt;
48     SDL_Rect texture_rect;
49
50     int inited;
51 } SDLContext;
52
53 static const struct sdl_texture_format_entry {
54     enum AVPixelFormat format; int texture_fmt;
55 } sdl_texture_format_map[] = {
56     /*
57      * Not implemented in FFmpeg, but leaving here for completeness.
58      * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ARGB4444 },
59      * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_RGBA4444 },
60      * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ABGR4444 },
61      * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_BGRA4444 },
62      * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ARGB1555 },
63      * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_RGBA5551 },
64      * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ABGR1555 },
65      * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_BGRA5551 },
66      * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ARGB2101010 },
67     */
68     { AV_PIX_FMT_RGB8,    SDL_PIXELFORMAT_RGB332 },
69     { AV_PIX_FMT_RGB444,  SDL_PIXELFORMAT_RGB444 },
70     { AV_PIX_FMT_RGB555,  SDL_PIXELFORMAT_RGB555 },
71     { AV_PIX_FMT_BGR555,  SDL_PIXELFORMAT_BGR555 },
72     { AV_PIX_FMT_RGB565,  SDL_PIXELFORMAT_RGB565 },
73     { AV_PIX_FMT_BGR565,  SDL_PIXELFORMAT_BGR565 },
74     { AV_PIX_FMT_RGB24,   SDL_PIXELFORMAT_RGB24 },
75     { AV_PIX_FMT_BGR24,   SDL_PIXELFORMAT_BGR24 },
76     { AV_PIX_FMT_0RGB32,  SDL_PIXELFORMAT_RGB888 },
77     { AV_PIX_FMT_0BGR32,  SDL_PIXELFORMAT_BGR888 },
78 #if HAVE_BIGENDIAN
79     { AV_PIX_FMT_RGB0,    SDL_PIXELFORMAT_RGBX8888 },
80     { AV_PIX_FMT_BGR0,    SDL_PIXELFORMAT_BGRX8888 },
81 #else
82     { AV_PIX_FMT_0BGR,    SDL_PIXELFORMAT_RGBX8888 },
83     { AV_PIX_FMT_0RGB,    SDL_PIXELFORMAT_BGRX8888 },
84 #endif
85     { AV_PIX_FMT_RGB32,   SDL_PIXELFORMAT_ARGB8888 },
86     { AV_PIX_FMT_RGB32_1, SDL_PIXELFORMAT_RGBA8888 },
87     { AV_PIX_FMT_BGR32,   SDL_PIXELFORMAT_ABGR8888 },
88     { AV_PIX_FMT_BGR32_1, SDL_PIXELFORMAT_BGRA8888 },
89     { AV_PIX_FMT_YUV420P, SDL_PIXELFORMAT_IYUV },
90     { AV_PIX_FMT_YUYV422, SDL_PIXELFORMAT_YUY2 },
91     { AV_PIX_FMT_UYVY422, SDL_PIXELFORMAT_UYVY },
92     { AV_PIX_FMT_NONE,    0                },
93 };
94
95 static void compute_texture_rect(AVFormatContext *s)
96 {
97     AVRational sar, dar; /* sample and display aspect ratios */
98     SDLContext *sdl = s->priv_data;
99     AVStream *st = s->streams[0];
100     AVCodecParameters *codecpar = st->codecpar;
101     SDL_Rect *texture_rect = &sdl->texture_rect;
102
103     /* compute texture width and height from the codec context information */
104     sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
105     dar = av_mul_q(sar, (AVRational){ codecpar->width, codecpar->height });
106
107     /* we suppose the screen has a 1/1 sample aspect ratio */
108     if (sdl->window_width && sdl->window_height) {
109         /* fit in the window */
110         if (av_cmp_q(dar, (AVRational){ sdl->window_width, sdl->window_height }) > 0) {
111             /* fit in width */
112             texture_rect->w = sdl->window_width;
113             texture_rect->h = av_rescale(texture_rect->w, dar.den, dar.num);
114         } else {
115             /* fit in height */
116             texture_rect->h = sdl->window_height;
117             texture_rect->w = av_rescale(texture_rect->h, dar.num, dar.den);
118         }
119     } else {
120         if (sar.num > sar.den) {
121             texture_rect->w = codecpar->width;
122             texture_rect->h = av_rescale(texture_rect->w, dar.den, dar.num);
123         } else {
124             texture_rect->h = codecpar->height;
125             texture_rect->w = av_rescale(texture_rect->h, dar.num, dar.den);
126         }
127         sdl->window_width  = texture_rect->w;
128         sdl->window_height = texture_rect->h;
129     }
130
131     texture_rect->x = (sdl->window_width  - texture_rect->w) / 2;
132     texture_rect->y = (sdl->window_height - texture_rect->h) / 2;
133 }
134
135 static int sdl2_write_trailer(AVFormatContext *s)
136 {
137     SDLContext *sdl = s->priv_data;
138
139     if (sdl->texture)
140         SDL_DestroyTexture(sdl->texture);
141     sdl->texture = NULL;
142
143     if (sdl->renderer)
144         SDL_DestroyRenderer(sdl->renderer);
145     sdl->renderer = NULL;
146
147     if (sdl->window)
148         SDL_DestroyWindow(sdl->window);
149     sdl->window = NULL;
150
151     if (!sdl->inited)
152         SDL_Quit();
153
154     return 0;
155 }
156
157 #define SDL_BASE_FLAGS (SDL_SWSURFACE|SDL_WINDOW_RESIZABLE)
158
159 static int sdl2_write_header(AVFormatContext *s)
160 {
161     SDLContext *sdl = s->priv_data;
162     AVStream *st = s->streams[0];
163     AVCodecParameters *codecpar = st->codecpar;
164     int i, ret = 0;
165     int flags  = 0;
166
167     if (!sdl->window_title)
168         sdl->window_title = av_strdup(s->url);
169
170     if (SDL_WasInit(SDL_INIT_VIDEO)) {
171         av_log(s, AV_LOG_WARNING,
172                "SDL video subsystem was already inited, you could have multiple SDL outputs. This may cause unknown behaviour.\n");
173         sdl->inited = 1;
174     }
175
176     if (   s->nb_streams > 1
177         || codecpar->codec_type != AVMEDIA_TYPE_VIDEO
178         || codecpar->codec_id   != AV_CODEC_ID_RAWVIDEO) {
179         av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
180         goto fail;
181     }
182
183     for (i = 0; sdl_texture_format_map[i].format != AV_PIX_FMT_NONE; i++) {
184         if (sdl_texture_format_map[i].format == codecpar->format) {
185             sdl->texture_fmt = sdl_texture_format_map[i].texture_fmt;
186             break;
187         }
188     }
189
190     if (!sdl->texture_fmt) {
191         av_log(s, AV_LOG_ERROR,
192                "Unsupported pixel format '%s'.\n",
193                av_get_pix_fmt_name(codecpar->format));
194         goto fail;
195     }
196
197     /* resize texture to width and height from the codec context information */
198     flags = SDL_BASE_FLAGS | (sdl->window_fullscreen ? SDL_WINDOW_FULLSCREEN : 0) |
199                              (sdl->window_borderless ? SDL_WINDOW_BORDERLESS : 0);
200
201     /* initialization */
202     if (!sdl->inited){
203         if (SDL_Init(SDL_INIT_VIDEO) != 0) {
204             av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
205             goto fail;
206         }
207     }
208
209     sdl->window_width = sdl->texture_rect.w = codecpar->width;
210     sdl->window_height = sdl->texture_rect.h = codecpar->height;
211     sdl->texture_rect.x = sdl->texture_rect.y = 0;
212
213     if (SDL_CreateWindowAndRenderer(sdl->window_width, sdl->window_height,
214                                     flags, &sdl->window, &sdl->renderer) != 0){
215         av_log(sdl, AV_LOG_ERROR, "Couldn't create window and renderer: %s\n", SDL_GetError());
216         goto fail;
217     }
218
219     SDL_SetWindowTitle(sdl->window, sdl->window_title);
220
221     sdl->texture = SDL_CreateTexture(sdl->renderer, sdl->texture_fmt, SDL_TEXTUREACCESS_STREAMING,
222                                      sdl->window_width, sdl->window_height);
223
224     if (!sdl->texture) {
225         av_log(sdl, AV_LOG_ERROR, "Unable to set create mode: %s\n", SDL_GetError());
226         goto fail;
227     }
228
229     av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d\n",
230            codecpar->width, codecpar->height, av_get_pix_fmt_name(codecpar->format),
231            sdl->window_width, sdl->window_height);
232
233     sdl->inited = 1;
234
235     return 0;
236 fail:
237     sdl2_write_trailer(s);
238     return ret;
239 }
240
241 static int sdl2_write_packet(AVFormatContext *s, AVPacket *pkt)
242 {
243     int ret, quit = 0;
244     SDLContext *sdl = s->priv_data;
245     AVCodecParameters *codecpar = s->streams[0]->codecpar;
246     uint8_t *data[4];
247     int linesize[4];
248
249     SDL_Event event;
250     if (SDL_PollEvent(&event)){
251         switch (event.type) {
252         case SDL_KEYDOWN:
253             switch (event.key.keysym.sym) {
254             case SDLK_ESCAPE:
255             case SDLK_q:
256                 quit = 1;
257                 break;
258             default:
259                 break;
260             }
261             break;
262         case SDL_QUIT:
263             quit = 1;
264             break;
265         case SDL_WINDOWEVENT:
266             switch(event.window.event){
267             case SDL_WINDOWEVENT_RESIZED:
268             case SDL_WINDOWEVENT_SIZE_CHANGED:
269                 sdl->window_width  = event.window.data1;
270                 sdl->window_height = event.window.data2;
271                 compute_texture_rect(s);
272                 break;
273             default:
274                 break;
275             }
276             break;
277         default:
278             break;
279         }
280     }
281
282     if (quit) {
283         sdl2_write_trailer(s);
284         return AVERROR(EIO);
285     }
286
287     av_image_fill_arrays(data, linesize, pkt->data, codecpar->format, codecpar->width, codecpar->height, 1);
288     switch (sdl->texture_fmt) {
289     /* case SDL_PIXELFORMAT_ARGB4444:
290      * case SDL_PIXELFORMAT_RGBA4444:
291      * case SDL_PIXELFORMAT_ABGR4444:
292      * case SDL_PIXELFORMAT_BGRA4444:
293      * case SDL_PIXELFORMAT_ARGB1555:
294      * case SDL_PIXELFORMAT_RGBA5551:
295      * case SDL_PIXELFORMAT_ABGR1555:
296      * case SDL_PIXELFORMAT_BGRA5551:
297      * case SDL_PIXELFORMAT_ARGB2101010:
298      */
299     case SDL_PIXELFORMAT_IYUV:
300     case SDL_PIXELFORMAT_YUY2:
301     case SDL_PIXELFORMAT_UYVY:
302         ret = SDL_UpdateYUVTexture(sdl->texture, NULL,
303                                    data[0], linesize[0],
304                                    data[1], linesize[1],
305                                    data[2], linesize[2]);
306         break;
307     case SDL_PIXELFORMAT_RGB332:
308     case SDL_PIXELFORMAT_RGB444:
309     case SDL_PIXELFORMAT_RGB555:
310     case SDL_PIXELFORMAT_BGR555:
311     case SDL_PIXELFORMAT_RGB565:
312     case SDL_PIXELFORMAT_BGR565:
313     case SDL_PIXELFORMAT_RGB24:
314     case SDL_PIXELFORMAT_BGR24:
315     case SDL_PIXELFORMAT_RGB888:
316     case SDL_PIXELFORMAT_RGBX8888:
317     case SDL_PIXELFORMAT_BGR888:
318     case SDL_PIXELFORMAT_BGRX8888:
319     case SDL_PIXELFORMAT_ARGB8888:
320     case SDL_PIXELFORMAT_RGBA8888:
321     case SDL_PIXELFORMAT_ABGR8888:
322     case SDL_PIXELFORMAT_BGRA8888:
323         ret = SDL_UpdateTexture(sdl->texture, NULL, data[0], linesize[0]);
324         break;
325     default:
326         av_log(NULL, AV_LOG_FATAL, "Unsupported pixel format\n");
327         ret = -1;
328         break;
329     }
330     SDL_RenderClear(sdl->renderer);
331     SDL_RenderCopy(sdl->renderer, sdl->texture, NULL, &sdl->texture_rect);
332     SDL_RenderPresent(sdl->renderer);
333     return ret;
334 }
335
336 #define OFFSET(x) offsetof(SDLContext,x)
337
338 static const AVOption options[] = {
339     { "window_title",      "set SDL window title",       OFFSET(window_title), AV_OPT_TYPE_STRING,     { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
340     { "window_size",       "set SDL window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
341     { "window_fullscreen", "set SDL window fullscreen",  OFFSET(window_fullscreen), AV_OPT_TYPE_BOOL,  { .i64 = 0 },    0, 1, AV_OPT_FLAG_ENCODING_PARAM },
342     { "window_borderless", "set SDL window border off",  OFFSET(window_borderless), AV_OPT_TYPE_BOOL,  { .i64 = 0 },    0, 1, AV_OPT_FLAG_ENCODING_PARAM },
343     { NULL },
344 };
345
346 static const AVClass sdl2_class = {
347     .class_name = "sdl2 outdev",
348     .item_name  = av_default_item_name,
349     .option     = options,
350     .version    = LIBAVUTIL_VERSION_INT,
351     .category   = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
352 };
353
354 AVOutputFormat ff_sdl2_muxer = {
355     .name           = "sdl,sdl2",
356     .long_name      = NULL_IF_CONFIG_SMALL("SDL2 output device"),
357     .priv_data_size = sizeof(SDLContext),
358     .audio_codec    = AV_CODEC_ID_NONE,
359     .video_codec    = AV_CODEC_ID_RAWVIDEO,
360     .write_header   = sdl2_write_header,
361     .write_packet   = sdl2_write_packet,
362     .write_trailer  = sdl2_write_trailer,
363     .flags          = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
364     .priv_class     = &sdl2_class,
365 };