You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
12KB

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