sdl.c 12 KB

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