FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavdevice/sdl2.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 140 0.0%
Functions: 0 4 0.0%
Branches: 0 65 0.0%

Line Branch Exec Source
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/imgutils.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "libavformat/mux.h"
34
35 typedef struct {
36 AVClass *class;
37 SDL_Window *window;
38 SDL_Renderer *renderer;
39 char *window_title;
40 int window_width, window_height; /**< size of the window */
41 int window_x, window_y; /**< position of the window */
42 int window_fullscreen;
43 int window_borderless;
44 int enable_quit_action;
45
46 SDL_Texture *texture;
47 int texture_fmt;
48 SDL_Rect texture_rect;
49
50 int inited;
51 int warned;
52 } SDLContext;
53
54 static const struct sdl_texture_format_entry {
55 enum AVPixelFormat format; int texture_fmt;
56 } sdl_texture_format_map[] = {
57 /*
58 * Not implemented in FFmpeg, but leaving here for completeness.
59 * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ARGB4444 },
60 * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_RGBA4444 },
61 * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ABGR4444 },
62 * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_BGRA4444 },
63 * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ARGB1555 },
64 * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_RGBA5551 },
65 * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ABGR1555 },
66 * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_BGRA5551 },
67 * { AV_PIX_FMT_NONE, SDL_PIXELFORMAT_ARGB2101010 },
68 */
69 { AV_PIX_FMT_RGB8, SDL_PIXELFORMAT_RGB332 },
70 { AV_PIX_FMT_RGB444, SDL_PIXELFORMAT_RGB444 },
71 { AV_PIX_FMT_RGB555, SDL_PIXELFORMAT_RGB555 },
72 { AV_PIX_FMT_BGR555, SDL_PIXELFORMAT_BGR555 },
73 { AV_PIX_FMT_RGB565, SDL_PIXELFORMAT_RGB565 },
74 { AV_PIX_FMT_BGR565, SDL_PIXELFORMAT_BGR565 },
75 { AV_PIX_FMT_RGB24, SDL_PIXELFORMAT_RGB24 },
76 { AV_PIX_FMT_BGR24, SDL_PIXELFORMAT_BGR24 },
77 { AV_PIX_FMT_0RGB32, SDL_PIXELFORMAT_RGB888 },
78 { AV_PIX_FMT_0BGR32, SDL_PIXELFORMAT_BGR888 },
79 #if HAVE_BIGENDIAN
80 { AV_PIX_FMT_RGB0, SDL_PIXELFORMAT_RGBX8888 },
81 { AV_PIX_FMT_BGR0, SDL_PIXELFORMAT_BGRX8888 },
82 #else
83 { AV_PIX_FMT_0BGR, SDL_PIXELFORMAT_RGBX8888 },
84 { AV_PIX_FMT_0RGB, SDL_PIXELFORMAT_BGRX8888 },
85 #endif
86 { AV_PIX_FMT_RGB32, SDL_PIXELFORMAT_ARGB8888 },
87 { AV_PIX_FMT_RGB32_1, SDL_PIXELFORMAT_RGBA8888 },
88 { AV_PIX_FMT_BGR32, SDL_PIXELFORMAT_ABGR8888 },
89 { AV_PIX_FMT_BGR32_1, SDL_PIXELFORMAT_BGRA8888 },
90 { AV_PIX_FMT_YUV420P, SDL_PIXELFORMAT_IYUV },
91 { AV_PIX_FMT_YUYV422, SDL_PIXELFORMAT_YUY2 },
92 { AV_PIX_FMT_UYVY422, SDL_PIXELFORMAT_UYVY },
93 { AV_PIX_FMT_NONE, 0 },
94 };
95
96 static void compute_texture_rect(AVFormatContext *s)
97 {
98 AVRational sar, dar; /* sample and display aspect ratios */
99 SDLContext *sdl = s->priv_data;
100 AVStream *st = s->streams[0];
101 AVCodecParameters *codecpar = st->codecpar;
102 SDL_Rect *texture_rect = &sdl->texture_rect;
103
104 /* compute texture width and height from the codec context information */
105 sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
106 dar = av_mul_q(sar, (AVRational){ codecpar->width, codecpar->height });
107
108 /* we suppose the screen has a 1/1 sample aspect ratio */
109 if (sdl->window_width && sdl->window_height) {
110 /* fit in the window */
111 if (av_cmp_q(dar, (AVRational){ sdl->window_width, sdl->window_height }) > 0) {
112 /* fit in width */
113 texture_rect->w = sdl->window_width;
114 texture_rect->h = av_rescale(texture_rect->w, dar.den, dar.num);
115 } else {
116 /* fit in height */
117 texture_rect->h = sdl->window_height;
118 texture_rect->w = av_rescale(texture_rect->h, dar.num, dar.den);
119 }
120 } else {
121 if (sar.num > sar.den) {
122 texture_rect->w = codecpar->width;
123 texture_rect->h = av_rescale(texture_rect->w, dar.den, dar.num);
124 } else {
125 texture_rect->h = codecpar->height;
126 texture_rect->w = av_rescale(texture_rect->h, dar.num, dar.den);
127 }
128 sdl->window_width = texture_rect->w;
129 sdl->window_height = texture_rect->h;
130 }
131
132 texture_rect->x = (sdl->window_width - texture_rect->w) / 2;
133 texture_rect->y = (sdl->window_height - texture_rect->h) / 2;
134 }
135
136 static int sdl2_write_trailer(AVFormatContext *s)
137 {
138 SDLContext *sdl = s->priv_data;
139
140 if (sdl->texture)
141 SDL_DestroyTexture(sdl->texture);
142 sdl->texture = NULL;
143
144 if (sdl->renderer)
145 SDL_DestroyRenderer(sdl->renderer);
146 sdl->renderer = NULL;
147
148 if (sdl->window)
149 SDL_DestroyWindow(sdl->window);
150 sdl->window = NULL;
151
152 if (!sdl->inited)
153 SDL_Quit();
154
155 return 0;
156 }
157
158 static int sdl2_write_header(AVFormatContext *s)
159 {
160 SDLContext *sdl = s->priv_data;
161 AVStream *st = s->streams[0];
162 AVCodecParameters *codecpar = st->codecpar;
163 int i, ret = 0;
164 int flags = 0;
165
166 if (!sdl->warned) {
167 av_log(sdl, AV_LOG_WARNING,
168 "The sdl output device is deprecated due to being fundamentally incompatible with libavformat API. "
169 "For monitoring purposes in ffmpeg you can output to a file or use pipes and a video player.\n"
170 "Example: ffmpeg -i INPUT -f nut -c:v rawvideo - | ffplay -loglevel warning -vf setpts=0 -\n"
171 );
172 sdl->warned = 1;
173 }
174
175 if (!sdl->window_title)
176 sdl->window_title = av_strdup(s->url);
177
178 if (SDL_WasInit(SDL_INIT_VIDEO)) {
179 av_log(s, AV_LOG_WARNING,
180 "SDL video subsystem was already inited, you could have multiple SDL outputs. This may cause unknown behaviour.\n");
181 sdl->inited = 1;
182 }
183
184 if ( s->nb_streams > 1
185 || codecpar->codec_type != AVMEDIA_TYPE_VIDEO
186 || codecpar->codec_id != AV_CODEC_ID_RAWVIDEO) {
187 av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
188 goto fail;
189 }
190
191 for (i = 0; sdl_texture_format_map[i].format != AV_PIX_FMT_NONE; i++) {
192 if (sdl_texture_format_map[i].format == codecpar->format) {
193 sdl->texture_fmt = sdl_texture_format_map[i].texture_fmt;
194 break;
195 }
196 }
197
198 if (!sdl->texture_fmt) {
199 av_log(s, AV_LOG_ERROR,
200 "Unsupported pixel format '%s'.\n",
201 av_get_pix_fmt_name(codecpar->format));
202 goto fail;
203 }
204
205 /* resize texture to width and height from the codec context information */
206 flags = SDL_WINDOW_HIDDEN |
207 (sdl->window_fullscreen ? SDL_WINDOW_FULLSCREEN : 0) |
208 (sdl->window_borderless ? SDL_WINDOW_BORDERLESS : SDL_WINDOW_RESIZABLE);
209
210 /* initialization */
211 if (!sdl->inited){
212 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
213 av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
214 goto fail;
215 }
216 }
217
218 compute_texture_rect(s);
219
220 if (SDL_CreateWindowAndRenderer(sdl->window_width, sdl->window_height,
221 flags, &sdl->window, &sdl->renderer) != 0){
222 av_log(sdl, AV_LOG_ERROR, "Couldn't create window and renderer: %s\n", SDL_GetError());
223 goto fail;
224 }
225
226 SDL_SetWindowTitle(sdl->window, sdl->window_title);
227 SDL_SetWindowPosition(sdl->window, sdl->window_x, sdl->window_y);
228 SDL_ShowWindow(sdl->window);
229
230 sdl->texture = SDL_CreateTexture(sdl->renderer, sdl->texture_fmt, SDL_TEXTUREACCESS_STREAMING,
231 codecpar->width, codecpar->height);
232
233 if (!sdl->texture) {
234 av_log(sdl, AV_LOG_ERROR, "Unable to set create mode: %s\n", SDL_GetError());
235 goto fail;
236 }
237
238 av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d\n",
239 codecpar->width, codecpar->height, av_get_pix_fmt_name(codecpar->format),
240 sdl->window_width, sdl->window_height);
241
242 sdl->inited = 1;
243
244 return 0;
245 fail:
246 sdl2_write_trailer(s);
247 return ret;
248 }
249
250 static int sdl2_write_packet(AVFormatContext *s, AVPacket *pkt)
251 {
252 int ret, quit = 0;
253 SDLContext *sdl = s->priv_data;
254 AVCodecParameters *codecpar = s->streams[0]->codecpar;
255 uint8_t *data[4];
256 int linesize[4];
257
258 SDL_Event event;
259 if (SDL_PollEvent(&event)){
260 switch (event.type) {
261 case SDL_KEYDOWN:
262 switch (event.key.keysym.sym) {
263 case SDLK_ESCAPE:
264 case SDLK_q:
265 quit = 1;
266 break;
267 default:
268 break;
269 }
270 break;
271 case SDL_QUIT:
272 quit = 1;
273 break;
274 case SDL_WINDOWEVENT:
275 switch(event.window.event){
276 case SDL_WINDOWEVENT_RESIZED:
277 case SDL_WINDOWEVENT_SIZE_CHANGED:
278 sdl->window_width = event.window.data1;
279 sdl->window_height = event.window.data2;
280 compute_texture_rect(s);
281 break;
282 default:
283 break;
284 }
285 break;
286 default:
287 break;
288 }
289 }
290
291 if (quit && sdl->enable_quit_action) {
292 sdl2_write_trailer(s);
293 return AVERROR(EIO);
294 }
295
296 av_image_fill_arrays(data, linesize, pkt->data, codecpar->format, codecpar->width, codecpar->height, 1);
297 switch (sdl->texture_fmt) {
298 /* case SDL_PIXELFORMAT_ARGB4444:
299 * case SDL_PIXELFORMAT_RGBA4444:
300 * case SDL_PIXELFORMAT_ABGR4444:
301 * case SDL_PIXELFORMAT_BGRA4444:
302 * case SDL_PIXELFORMAT_ARGB1555:
303 * case SDL_PIXELFORMAT_RGBA5551:
304 * case SDL_PIXELFORMAT_ABGR1555:
305 * case SDL_PIXELFORMAT_BGRA5551:
306 * case SDL_PIXELFORMAT_ARGB2101010:
307 */
308 case SDL_PIXELFORMAT_IYUV:
309 case SDL_PIXELFORMAT_YUY2:
310 case SDL_PIXELFORMAT_UYVY:
311 ret = SDL_UpdateYUVTexture(sdl->texture, NULL,
312 data[0], linesize[0],
313 data[1], linesize[1],
314 data[2], linesize[2]);
315 break;
316 case SDL_PIXELFORMAT_RGB332:
317 case SDL_PIXELFORMAT_RGB444:
318 case SDL_PIXELFORMAT_RGB555:
319 case SDL_PIXELFORMAT_BGR555:
320 case SDL_PIXELFORMAT_RGB565:
321 case SDL_PIXELFORMAT_BGR565:
322 case SDL_PIXELFORMAT_RGB24:
323 case SDL_PIXELFORMAT_BGR24:
324 case SDL_PIXELFORMAT_RGB888:
325 case SDL_PIXELFORMAT_RGBX8888:
326 case SDL_PIXELFORMAT_BGR888:
327 case SDL_PIXELFORMAT_BGRX8888:
328 case SDL_PIXELFORMAT_ARGB8888:
329 case SDL_PIXELFORMAT_RGBA8888:
330 case SDL_PIXELFORMAT_ABGR8888:
331 case SDL_PIXELFORMAT_BGRA8888:
332 ret = SDL_UpdateTexture(sdl->texture, NULL, data[0], linesize[0]);
333 break;
334 default:
335 av_log(NULL, AV_LOG_FATAL, "Unsupported pixel format\n");
336 ret = -1;
337 break;
338 }
339 SDL_RenderClear(sdl->renderer);
340 SDL_RenderCopy(sdl->renderer, sdl->texture, NULL, &sdl->texture_rect);
341 SDL_RenderPresent(sdl->renderer);
342 return ret;
343 }
344
345 #define OFFSET(x) offsetof(SDLContext,x)
346
347 static const AVOption options[] = {
348 { "window_title", "set SDL window title", OFFSET(window_title), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
349 { "window_size", "set SDL window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
350 { "window_x", "set SDL window x position", OFFSET(window_x), AV_OPT_TYPE_INT, { .i64 = SDL_WINDOWPOS_CENTERED }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
351 { "window_y", "set SDL window y position", OFFSET(window_y), AV_OPT_TYPE_INT, { .i64 = SDL_WINDOWPOS_CENTERED }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
352 { "window_fullscreen", "set SDL window fullscreen", OFFSET(window_fullscreen), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
353 { "window_borderless", "set SDL window border off", OFFSET(window_borderless), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
354 { "window_enable_quit", "set if quit action is available", OFFSET(enable_quit_action), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM },
355 { NULL },
356 };
357
358 static const AVClass sdl2_class = {
359 .class_name = "sdl2 outdev",
360 .item_name = av_default_item_name,
361 .option = options,
362 .version = LIBAVUTIL_VERSION_INT,
363 .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
364 };
365
366 const FFOutputFormat ff_sdl2_muxer = {
367 .p.name = "sdl,sdl2",
368 .p.long_name = NULL_IF_CONFIG_SMALL("SDL2 output device"),
369 .priv_data_size = sizeof(SDLContext),
370 .p.audio_codec = AV_CODEC_ID_NONE,
371 .p.video_codec = AV_CODEC_ID_RAWVIDEO,
372 .write_header = sdl2_write_header,
373 .write_packet = sdl2_write_packet,
374 .write_trailer = sdl2_write_trailer,
375 .p.flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
376 .p.priv_class = &sdl2_class,
377 };
378