sdl2.c 13 KB

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