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.

229 lines
7.4KB

  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. char *window_size;
  37. int window_width, window_height;
  38. int overlay_width, overlay_height;
  39. int overlay_fmt;
  40. int sdl_was_already_inited;
  41. } SDLContext;
  42. struct sdl_overlay_pix_fmt_entry {
  43. enum PixelFormat pix_fmt; int overlay_fmt;
  44. } sdl_overlay_pix_fmt_map[] = {
  45. { PIX_FMT_YUV420P, SDL_IYUV_OVERLAY },
  46. { PIX_FMT_YUYV422, SDL_YUY2_OVERLAY },
  47. { PIX_FMT_UYVY422, SDL_UYVY_OVERLAY },
  48. { PIX_FMT_NONE, 0 },
  49. };
  50. static int sdl_write_trailer(AVFormatContext *s)
  51. {
  52. SDLContext *sdl = s->priv_data;
  53. av_freep(&sdl->window_title);
  54. av_freep(&sdl->icon_title);
  55. av_freep(&sdl->window_size);
  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. float sar, dar; /* sample and display aspect ratios */
  70. int i, ret;
  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. if (sdl->window_size) {
  106. if (av_parse_video_size(&sdl->window_width, &sdl->window_height,
  107. sdl->window_size) < 0) {
  108. av_log(s, AV_LOG_ERROR, "Invalid window size '%s'\n", sdl->window_size);
  109. ret = AVERROR(EINVAL);
  110. goto fail;
  111. }
  112. }
  113. /* compute overlay width and height from the codec context information */
  114. sar = st->sample_aspect_ratio.num ? av_q2d(st->sample_aspect_ratio) : 1;
  115. dar = sar * (float)encctx->width / (float)encctx->height;
  116. /* we suppose the screen has a 1/1 sample aspect ratio */
  117. sdl->overlay_height = encctx->height;
  118. sdl->overlay_width = ((int)rint(sdl->overlay_height * dar));
  119. if (sdl->overlay_width > encctx->width) {
  120. sdl->overlay_width = encctx->width;
  121. sdl->overlay_height = ((int)rint(sdl->overlay_width / dar));
  122. }
  123. if (!sdl->window_width || !sdl->window_height) {
  124. sdl->window_width = sdl->overlay_width;
  125. sdl->window_height = sdl->overlay_height;
  126. }
  127. SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
  128. sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
  129. 24, SDL_SWSURFACE);
  130. if (!sdl->surface) {
  131. av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
  132. ret = AVERROR(EINVAL);
  133. goto fail;
  134. }
  135. sdl->overlay = SDL_CreateYUVOverlay(sdl->overlay_width, sdl->overlay_height,
  136. sdl->overlay_fmt, sdl->surface);
  137. if (!sdl->overlay || sdl->overlay->pitches[0] < sdl->overlay_width) {
  138. av_log(s, AV_LOG_ERROR,
  139. "SDL does not support an overlay with size of %dx%d pixels.\n",
  140. sdl->overlay_width, sdl->overlay_height);
  141. ret = AVERROR(EINVAL);
  142. goto fail;
  143. }
  144. av_log(s, AV_LOG_INFO, "w:%d h:%d fmt:%s sar:%f -> w:%d h:%d\n",
  145. encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt), sar,
  146. sdl->window_width, sdl->window_height);
  147. return 0;
  148. fail:
  149. sdl_write_trailer(s);
  150. return ret;
  151. }
  152. static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
  153. {
  154. SDLContext *sdl = s->priv_data;
  155. AVCodecContext *encctx = s->streams[0]->codec;
  156. SDL_Rect rect = { 0, 0, sdl->window_width, sdl->window_height };
  157. AVPicture pict;
  158. int i;
  159. avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
  160. SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
  161. SDL_MapRGB(sdl->surface->format, 0, 0, 0));
  162. SDL_LockYUVOverlay(sdl->overlay);
  163. for (i = 0; i < 3; i++) {
  164. sdl->overlay->pixels [i] = pict.data [i];
  165. sdl->overlay->pitches[i] = pict.linesize[i];
  166. }
  167. SDL_DisplayYUVOverlay(sdl->overlay, &rect);
  168. SDL_UnlockYUVOverlay(sdl->overlay);
  169. SDL_UpdateRect(sdl->surface, 0, 0, sdl->overlay_width, sdl->overlay_height);
  170. return 0;
  171. }
  172. #define OFFSET(x) offsetof(SDLContext,x)
  173. static const AVOption options[] = {
  174. { "window_title", "SDL window title", OFFSET(window_title), FF_OPT_TYPE_STRING, {.str = "SDL video outdev" }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  175. { "icon_title", "SDL iconified window title", OFFSET(icon_title) , FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  176. { "window_size", "SDL window forced size", OFFSET(window_size) , FF_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  177. { NULL },
  178. };
  179. static const AVClass sdl_class = {
  180. .class_name = "sdl outdev",
  181. .item_name = av_default_item_name,
  182. .option = options,
  183. .version = LIBAVUTIL_VERSION_INT,
  184. };
  185. AVOutputFormat ff_sdl_muxer = {
  186. .name = "sdl",
  187. .long_name = NULL_IF_CONFIG_SMALL("SDL output device"),
  188. .priv_data_size = sizeof(SDLContext),
  189. .audio_codec = CODEC_ID_NONE,
  190. .video_codec = CODEC_ID_RAWVIDEO,
  191. .write_header = sdl_write_header,
  192. .write_packet = sdl_write_packet,
  193. .write_trailer = sdl_write_trailer,
  194. .flags = AVFMT_NOFILE,
  195. .priv_class = &sdl_class,
  196. };