sdl2.c 13 KB

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