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.

348 lines
11KB

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