sdl.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "libavutil/avstring.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/parseutils.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "avdevice.h"
  30. typedef struct {
  31. AVClass *class;
  32. SDL_Surface *surface;
  33. SDL_Overlay *overlay;
  34. char *window_title;
  35. char *icon_title;
  36. int window_width, window_height; /**< size of the window */
  37. int window_fullscreen;
  38. int overlay_width, overlay_height; /**< size of the video in the window */
  39. int overlay_x, overlay_y;
  40. int overlay_fmt;
  41. int sdl_was_already_inited;
  42. } SDLContext;
  43. static const struct sdl_overlay_pix_fmt_entry {
  44. enum AVPixelFormat pix_fmt; int overlay_fmt;
  45. } sdl_overlay_pix_fmt_map[] = {
  46. { AV_PIX_FMT_YUV420P, SDL_IYUV_OVERLAY },
  47. { AV_PIX_FMT_YUYV422, SDL_YUY2_OVERLAY },
  48. { AV_PIX_FMT_UYVY422, SDL_UYVY_OVERLAY },
  49. { AV_PIX_FMT_NONE, 0 },
  50. };
  51. static int sdl_write_trailer(AVFormatContext *s)
  52. {
  53. SDLContext *sdl = s->priv_data;
  54. av_freep(&sdl->window_title);
  55. av_freep(&sdl->icon_title);
  56. if (sdl->overlay) {
  57. SDL_FreeYUVOverlay(sdl->overlay);
  58. sdl->overlay = NULL;
  59. }
  60. if (!sdl->sdl_was_already_inited)
  61. SDL_Quit();
  62. return 0;
  63. }
  64. static int sdl_write_header(AVFormatContext *s)
  65. {
  66. SDLContext *sdl = s->priv_data;
  67. AVStream *st = s->streams[0];
  68. AVCodecContext *encctx = st->codec;
  69. AVRational sar, dar; /* sample and display aspect ratios */
  70. int i, ret;
  71. int flags = SDL_SWSURFACE | sdl->window_fullscreen ? SDL_FULLSCREEN : 0;
  72. if (!sdl->window_title)
  73. sdl->window_title = av_strdup(s->filename);
  74. if (!sdl->icon_title)
  75. sdl->icon_title = av_strdup(sdl->window_title);
  76. if (SDL_WasInit(SDL_INIT_VIDEO)) {
  77. av_log(s, AV_LOG_ERROR,
  78. "SDL video subsystem was already inited, aborting\n");
  79. sdl->sdl_was_already_inited = 1;
  80. ret = AVERROR(EINVAL);
  81. goto fail;
  82. }
  83. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  84. av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
  85. ret = AVERROR(EINVAL);
  86. goto fail;
  87. }
  88. if ( s->nb_streams > 1
  89. || encctx->codec_type != AVMEDIA_TYPE_VIDEO
  90. || encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
  91. av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
  92. ret = AVERROR(EINVAL);
  93. goto fail;
  94. }
  95. for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
  96. if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) {
  97. sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt;
  98. break;
  99. }
  100. }
  101. if (!sdl->overlay_fmt) {
  102. av_log(s, AV_LOG_ERROR,
  103. "Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422\n",
  104. av_get_pix_fmt_name(encctx->pix_fmt));
  105. ret = AVERROR(EINVAL);
  106. goto fail;
  107. }
  108. /* compute overlay width and height from the codec context information */
  109. sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
  110. dar = av_mul_q(sar, (AVRational){ encctx->width, encctx->height });
  111. /* we suppose the screen has a 1/1 sample aspect ratio */
  112. if (sdl->window_width && sdl->window_height) {
  113. /* fit in the window */
  114. if (av_cmp_q(dar, (AVRational){ sdl->window_width, sdl->window_height }) > 0) {
  115. /* fit in width */
  116. sdl->overlay_width = sdl->window_width;
  117. sdl->overlay_height = av_rescale(sdl->overlay_width, dar.den, dar.num);
  118. } else {
  119. /* fit in height */
  120. sdl->overlay_height = sdl->window_height;
  121. sdl->overlay_width = av_rescale(sdl->overlay_height, dar.num, dar.den);
  122. }
  123. } else {
  124. if (sar.num > sar.den) {
  125. sdl->overlay_width = encctx->width;
  126. sdl->overlay_height = av_rescale(sdl->overlay_width, dar.den, dar.num);
  127. } else {
  128. sdl->overlay_height = encctx->height;
  129. sdl->overlay_width = av_rescale(sdl->overlay_height, dar.num, dar.den);
  130. }
  131. sdl->window_width = sdl->overlay_width;
  132. sdl->window_height = sdl->overlay_height;
  133. }
  134. sdl->overlay_x = (sdl->window_width - sdl->overlay_width ) / 2;
  135. sdl->overlay_y = (sdl->window_height - sdl->overlay_height) / 2;
  136. SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
  137. sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
  138. 24, flags);
  139. if (!sdl->surface) {
  140. av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
  141. ret = AVERROR(EINVAL);
  142. goto fail;
  143. }
  144. sdl->overlay = SDL_CreateYUVOverlay(encctx->width, encctx->height,
  145. sdl->overlay_fmt, sdl->surface);
  146. if (!sdl->overlay || sdl->overlay->pitches[0] < encctx->width) {
  147. av_log(s, AV_LOG_ERROR,
  148. "SDL does not support an overlay with size of %dx%d pixels\n",
  149. encctx->width, encctx->height);
  150. ret = AVERROR(EINVAL);
  151. goto fail;
  152. }
  153. av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d\n",
  154. encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt), sar.num, sar.den,
  155. sdl->overlay_width, sdl->overlay_height);
  156. return 0;
  157. fail:
  158. sdl_write_trailer(s);
  159. return ret;
  160. }
  161. static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
  162. {
  163. SDLContext *sdl = s->priv_data;
  164. AVCodecContext *encctx = s->streams[0]->codec;
  165. SDL_Rect rect = { sdl->overlay_x, sdl->overlay_y, sdl->overlay_width, sdl->overlay_height };
  166. AVPicture pict;
  167. int i;
  168. avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
  169. SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
  170. SDL_MapRGB(sdl->surface->format, 0, 0, 0));
  171. SDL_LockYUVOverlay(sdl->overlay);
  172. for (i = 0; i < 3; i++) {
  173. sdl->overlay->pixels [i] = pict.data [i];
  174. sdl->overlay->pitches[i] = pict.linesize[i];
  175. }
  176. SDL_DisplayYUVOverlay(sdl->overlay, &rect);
  177. SDL_UnlockYUVOverlay(sdl->overlay);
  178. SDL_UpdateRect(sdl->surface, rect.x, rect.y, rect.w, rect.h);
  179. return 0;
  180. }
  181. #define OFFSET(x) offsetof(SDLContext,x)
  182. static const AVOption options[] = {
  183. { "window_title", "set SDL window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  184. { "icon_title", "set SDL iconified window title", OFFSET(icon_title) , AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  185. { "window_size", "set SDL window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE,{.str=NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  186. { "window_fullscreen", "set SDL window fullscreen", OFFSET(window_fullscreen), AV_OPT_TYPE_INT,{.i64=0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  187. { NULL },
  188. };
  189. static const AVClass sdl_class = {
  190. .class_name = "sdl outdev",
  191. .item_name = av_default_item_name,
  192. .option = options,
  193. .version = LIBAVUTIL_VERSION_INT,
  194. };
  195. AVOutputFormat ff_sdl_muxer = {
  196. .name = "sdl",
  197. .long_name = NULL_IF_CONFIG_SMALL("SDL output device"),
  198. .priv_data_size = sizeof(SDLContext),
  199. .audio_codec = AV_CODEC_ID_NONE,
  200. .video_codec = AV_CODEC_ID_RAWVIDEO,
  201. .write_header = sdl_write_header,
  202. .write_packet = sdl_write_packet,
  203. .write_trailer = sdl_write_trailer,
  204. .flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
  205. .priv_class = &sdl_class,
  206. };