sdl2.c 13 KB

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