sdl.c 7.1 KB

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