sdl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  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. * libSDL output device
  23. */
  24. #include <SDL.h>
  25. #include <SDL_thread.h>
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "libavutil/time.h"
  31. #include "avdevice.h"
  32. typedef struct {
  33. AVClass *class;
  34. SDL_Surface *surface;
  35. SDL_Overlay *overlay;
  36. char *window_title;
  37. char *icon_title;
  38. int window_width, window_height; /**< size of the window */
  39. int window_fullscreen;
  40. SDL_Rect overlay_rect;
  41. int overlay_fmt;
  42. int sdl_was_already_inited;
  43. SDL_Thread *event_thread;
  44. SDL_mutex *mutex;
  45. SDL_cond *init_cond;
  46. int init_ret; /* return code used to signal initialization errors */
  47. int inited;
  48. int quit;
  49. } SDLContext;
  50. static const struct sdl_overlay_pix_fmt_entry {
  51. enum AVPixelFormat pix_fmt; int overlay_fmt;
  52. } sdl_overlay_pix_fmt_map[] = {
  53. { AV_PIX_FMT_YUV420P, SDL_IYUV_OVERLAY },
  54. { AV_PIX_FMT_YUYV422, SDL_YUY2_OVERLAY },
  55. { AV_PIX_FMT_UYVY422, SDL_UYVY_OVERLAY },
  56. { AV_PIX_FMT_NONE, 0 },
  57. };
  58. static int sdl_write_trailer(AVFormatContext *s)
  59. {
  60. SDLContext *sdl = s->priv_data;
  61. sdl->quit = 1;
  62. if (sdl->overlay)
  63. SDL_FreeYUVOverlay(sdl->overlay);
  64. sdl->overlay = NULL;
  65. if (sdl->event_thread)
  66. SDL_WaitThread(sdl->event_thread, NULL);
  67. sdl->event_thread = NULL;
  68. if (sdl->mutex)
  69. SDL_DestroyMutex(sdl->mutex);
  70. sdl->mutex = NULL;
  71. if (sdl->init_cond)
  72. SDL_DestroyCond(sdl->init_cond);
  73. sdl->init_cond = NULL;
  74. if (!sdl->sdl_was_already_inited)
  75. SDL_Quit();
  76. return 0;
  77. }
  78. static void compute_overlay_rect(AVFormatContext *s)
  79. {
  80. AVRational sar, dar; /* sample and display aspect ratios */
  81. SDLContext *sdl = s->priv_data;
  82. AVStream *st = s->streams[0];
  83. AVCodecContext *encctx = st->codec;
  84. SDL_Rect *overlay_rect = &sdl->overlay_rect;
  85. /* compute overlay width and height from the codec context information */
  86. sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
  87. dar = av_mul_q(sar, (AVRational){ encctx->width, encctx->height });
  88. /* we suppose the screen has a 1/1 sample aspect ratio */
  89. if (sdl->window_width && sdl->window_height) {
  90. /* fit in the window */
  91. if (av_cmp_q(dar, (AVRational){ sdl->window_width, sdl->window_height }) > 0) {
  92. /* fit in width */
  93. overlay_rect->w = sdl->window_width;
  94. overlay_rect->h = av_rescale(overlay_rect->w, dar.den, dar.num);
  95. } else {
  96. /* fit in height */
  97. overlay_rect->h = sdl->window_height;
  98. overlay_rect->w = av_rescale(overlay_rect->h, dar.num, dar.den);
  99. }
  100. } else {
  101. if (sar.num > sar.den) {
  102. overlay_rect->w = encctx->width;
  103. overlay_rect->h = av_rescale(overlay_rect->w, dar.den, dar.num);
  104. } else {
  105. overlay_rect->h = encctx->height;
  106. overlay_rect->w = av_rescale(overlay_rect->h, dar.num, dar.den);
  107. }
  108. sdl->window_width = overlay_rect->w;
  109. sdl->window_height = overlay_rect->h;
  110. }
  111. overlay_rect->x = (sdl->window_width - overlay_rect->w) / 2;
  112. overlay_rect->y = (sdl->window_height - overlay_rect->h) / 2;
  113. }
  114. #define SDL_BASE_FLAGS (SDL_SWSURFACE|SDL_RESIZABLE)
  115. static int event_thread(void *arg)
  116. {
  117. AVFormatContext *s = arg;
  118. SDLContext *sdl = s->priv_data;
  119. int flags = SDL_BASE_FLAGS | (sdl->window_fullscreen ? SDL_FULLSCREEN : 0);
  120. AVStream *st = s->streams[0];
  121. AVCodecContext *encctx = st->codec;
  122. /* initialization */
  123. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  124. av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
  125. sdl->init_ret = AVERROR(EINVAL);
  126. goto init_end;
  127. }
  128. SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
  129. sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
  130. 24, flags);
  131. if (!sdl->surface) {
  132. av_log(sdl, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
  133. sdl->init_ret = AVERROR(EINVAL);
  134. goto init_end;
  135. }
  136. sdl->overlay = SDL_CreateYUVOverlay(encctx->width, encctx->height,
  137. sdl->overlay_fmt, sdl->surface);
  138. if (!sdl->overlay || sdl->overlay->pitches[0] < encctx->width) {
  139. av_log(s, AV_LOG_ERROR,
  140. "SDL does not support an overlay with size of %dx%d pixels\n",
  141. encctx->width, encctx->height);
  142. sdl->init_ret = AVERROR(EINVAL);
  143. goto init_end;
  144. }
  145. sdl->init_ret = 0;
  146. av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d\n",
  147. encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt),
  148. sdl->overlay_rect.w, sdl->overlay_rect.h);
  149. init_end:
  150. SDL_LockMutex(sdl->mutex);
  151. sdl->inited = 1;
  152. SDL_UnlockMutex(sdl->mutex);
  153. SDL_CondSignal(sdl->init_cond);
  154. if (sdl->init_ret < 0)
  155. return sdl->init_ret;
  156. /* event loop */
  157. while (!sdl->quit) {
  158. int ret;
  159. SDL_Event event;
  160. SDL_PumpEvents();
  161. ret = SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS);
  162. if (ret < 0) {
  163. av_log(s, AV_LOG_ERROR, "Error when getting SDL event: %s\n", SDL_GetError());
  164. continue;
  165. }
  166. if (ret == 0) {
  167. SDL_Delay(10);
  168. continue;
  169. }
  170. switch (event.type) {
  171. case SDL_KEYDOWN:
  172. switch (event.key.keysym.sym) {
  173. case SDLK_ESCAPE:
  174. case SDLK_q:
  175. sdl->quit = 1;
  176. break;
  177. }
  178. break;
  179. case SDL_QUIT:
  180. sdl->quit = 1;
  181. break;
  182. case SDL_VIDEORESIZE:
  183. sdl->window_width = event.resize.w;
  184. sdl->window_height = event.resize.h;
  185. SDL_LockMutex(sdl->mutex);
  186. sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height, 24, SDL_BASE_FLAGS);
  187. if (!sdl->surface) {
  188. av_log(s, AV_LOG_ERROR, "Failed to set SDL video mode: %s\n", SDL_GetError());
  189. sdl->quit = 1;
  190. } else {
  191. compute_overlay_rect(s);
  192. }
  193. SDL_UnlockMutex(sdl->mutex);
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. return 0;
  200. }
  201. static int sdl_write_header(AVFormatContext *s)
  202. {
  203. SDLContext *sdl = s->priv_data;
  204. AVStream *st = s->streams[0];
  205. AVCodecContext *encctx = st->codec;
  206. int i, ret;
  207. if (!sdl->window_title)
  208. sdl->window_title = av_strdup(s->filename);
  209. if (!sdl->icon_title)
  210. sdl->icon_title = av_strdup(sdl->window_title);
  211. if (SDL_WasInit(SDL_INIT_VIDEO)) {
  212. av_log(s, AV_LOG_ERROR,
  213. "SDL video subsystem was already inited, aborting\n");
  214. sdl->sdl_was_already_inited = 1;
  215. ret = AVERROR(EINVAL);
  216. goto fail;
  217. }
  218. if ( s->nb_streams > 1
  219. || encctx->codec_type != AVMEDIA_TYPE_VIDEO
  220. || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
  221. av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
  222. ret = AVERROR(EINVAL);
  223. goto fail;
  224. }
  225. for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
  226. if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) {
  227. sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt;
  228. break;
  229. }
  230. }
  231. if (!sdl->overlay_fmt) {
  232. av_log(s, AV_LOG_ERROR,
  233. "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422\n",
  234. av_get_pix_fmt_name(encctx->pix_fmt));
  235. ret = AVERROR(EINVAL);
  236. goto fail;
  237. }
  238. /* compute overlay width and height from the codec context information */
  239. compute_overlay_rect(s);
  240. sdl->init_cond = SDL_CreateCond();
  241. if (!sdl->init_cond) {
  242. av_log(s, AV_LOG_ERROR, "Could not create SDL condition variable: %s\n", SDL_GetError());
  243. ret = AVERROR_EXTERNAL;
  244. goto fail;
  245. }
  246. sdl->mutex = SDL_CreateMutex();
  247. if (!sdl->mutex) {
  248. av_log(s, AV_LOG_ERROR, "Could not create SDL mutex: %s\n", SDL_GetError());
  249. ret = AVERROR_EXTERNAL;
  250. goto fail;
  251. }
  252. sdl->event_thread = SDL_CreateThread(event_thread, s);
  253. if (!sdl->event_thread) {
  254. av_log(s, AV_LOG_ERROR, "Could not create SDL event thread: %s\n", SDL_GetError());
  255. ret = AVERROR_EXTERNAL;
  256. goto fail;
  257. }
  258. /* wait until the video system has been inited */
  259. SDL_LockMutex(sdl->mutex);
  260. while (!sdl->inited) {
  261. SDL_CondWait(sdl->init_cond, sdl->mutex);
  262. }
  263. SDL_UnlockMutex(sdl->mutex);
  264. if (sdl->init_ret < 0) {
  265. ret = sdl->init_ret;
  266. goto fail;
  267. }
  268. return 0;
  269. fail:
  270. sdl_write_trailer(s);
  271. return ret;
  272. }
  273. static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
  274. {
  275. SDLContext *sdl = s->priv_data;
  276. AVCodecContext *encctx = s->streams[0]->codec;
  277. AVPicture pict;
  278. int i;
  279. if (sdl->quit) {
  280. sdl_write_trailer(s);
  281. return AVERROR(EIO);
  282. }
  283. avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
  284. SDL_LockMutex(sdl->mutex);
  285. SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
  286. SDL_MapRGB(sdl->surface->format, 0, 0, 0));
  287. SDL_LockYUVOverlay(sdl->overlay);
  288. for (i = 0; i < 3; i++) {
  289. sdl->overlay->pixels [i] = pict.data [i];
  290. sdl->overlay->pitches[i] = pict.linesize[i];
  291. }
  292. SDL_DisplayYUVOverlay(sdl->overlay, &sdl->overlay_rect);
  293. SDL_UnlockYUVOverlay(sdl->overlay);
  294. SDL_UpdateRect(sdl->surface,
  295. sdl->overlay_rect.x, sdl->overlay_rect.y,
  296. sdl->overlay_rect.w, sdl->overlay_rect.h);
  297. SDL_UnlockMutex(sdl->mutex);
  298. return 0;
  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. { "icon_title", "set SDL iconified window title", OFFSET(icon_title) , AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  304. { "window_size", "set SDL window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  305. { "window_fullscreen", "set SDL window fullscreen", OFFSET(window_fullscreen), AV_OPT_TYPE_INT, { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  306. { NULL },
  307. };
  308. static const AVClass sdl_class = {
  309. .class_name = "sdl outdev",
  310. .item_name = av_default_item_name,
  311. .option = options,
  312. .version = LIBAVUTIL_VERSION_INT,
  313. .category = AV_CLASS_CATEGORY_DEVICE_VIDEO_OUTPUT,
  314. };
  315. AVOutputFormat ff_sdl_muxer = {
  316. .name = "sdl",
  317. .long_name = NULL_IF_CONFIG_SMALL("SDL output device"),
  318. .priv_data_size = sizeof(SDLContext),
  319. .audio_codec = AV_CODEC_ID_NONE,
  320. .video_codec = AV_CODEC_ID_RAWVIDEO,
  321. .write_header = sdl_write_header,
  322. .write_packet = sdl_write_packet,
  323. .write_trailer = sdl_write_trailer,
  324. .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  325. .priv_class = &sdl_class,
  326. };