sdl2.c 12 KB

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