2011-05-27 01:34:35 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Stefano Sabatini
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* libSDL output device
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SDL.h>
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "avdevice.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
AVClass *class;
|
|
|
|
SDL_Surface *surface;
|
|
|
|
SDL_Overlay *overlay;
|
|
|
|
char *window_title;
|
|
|
|
char *icon_title;
|
2012-06-09 12:28:44 +02:00
|
|
|
int window_width, window_height; /**< size of the window */
|
|
|
|
int overlay_width, overlay_height; /**< size of the video in the window */
|
2012-06-09 21:03:18 +02:00
|
|
|
int overlay_x, overlay_y;
|
2011-05-27 01:34:35 +02:00
|
|
|
int overlay_fmt;
|
|
|
|
int sdl_was_already_inited;
|
|
|
|
} SDLContext;
|
|
|
|
|
2011-11-05 15:21:42 +01:00
|
|
|
static const struct sdl_overlay_pix_fmt_entry {
|
2012-10-08 20:54:00 +02:00
|
|
|
enum AVPixelFormat pix_fmt; int overlay_fmt;
|
2011-05-27 01:34:35 +02:00
|
|
|
} sdl_overlay_pix_fmt_map[] = {
|
2012-10-08 20:54:00 +02:00
|
|
|
{ AV_PIX_FMT_YUV420P, SDL_IYUV_OVERLAY },
|
|
|
|
{ AV_PIX_FMT_YUYV422, SDL_YUY2_OVERLAY },
|
|
|
|
{ AV_PIX_FMT_UYVY422, SDL_UYVY_OVERLAY },
|
|
|
|
{ AV_PIX_FMT_NONE, 0 },
|
2011-05-27 01:34:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int sdl_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
SDLContext *sdl = s->priv_data;
|
|
|
|
|
|
|
|
av_freep(&sdl->window_title);
|
|
|
|
av_freep(&sdl->icon_title);
|
|
|
|
|
|
|
|
if (sdl->overlay) {
|
|
|
|
SDL_FreeYUVOverlay(sdl->overlay);
|
|
|
|
sdl->overlay = NULL;
|
|
|
|
}
|
|
|
|
if (!sdl->sdl_was_already_inited)
|
|
|
|
SDL_Quit();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdl_write_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
SDLContext *sdl = s->priv_data;
|
|
|
|
AVStream *st = s->streams[0];
|
|
|
|
AVCodecContext *encctx = st->codec;
|
2012-06-09 21:03:18 +02:00
|
|
|
AVRational sar, dar; /* sample and display aspect ratios */
|
2011-05-27 01:34:35 +02:00
|
|
|
int i, ret;
|
|
|
|
|
2011-06-05 00:57:05 +02:00
|
|
|
if (!sdl->window_title)
|
|
|
|
sdl->window_title = av_strdup(s->filename);
|
2011-05-27 01:34:35 +02:00
|
|
|
if (!sdl->icon_title)
|
|
|
|
sdl->icon_title = av_strdup(sdl->window_title);
|
|
|
|
|
|
|
|
if (SDL_WasInit(SDL_INIT_VIDEO)) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
2012-09-07 13:58:31 +02:00
|
|
|
"SDL video subsystem was already inited, aborting\n");
|
2011-05-27 01:34:35 +02:00
|
|
|
sdl->sdl_was_already_inited = 1;
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unable to initialize SDL: %s\n", SDL_GetError());
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( s->nb_streams > 1
|
|
|
|
|| encctx->codec_type != AVMEDIA_TYPE_VIDEO
|
2012-08-07 22:45:46 +02:00
|
|
|
|| encctx->codec_id != AV_CODEC_ID_RAWVIDEO) {
|
2011-05-27 01:34:35 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "Only supports one rawvideo stream\n");
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-10-08 20:54:00 +02:00
|
|
|
for (i = 0; sdl_overlay_pix_fmt_map[i].pix_fmt != AV_PIX_FMT_NONE; i++) {
|
2011-05-27 01:34:35 +02:00
|
|
|
if (sdl_overlay_pix_fmt_map[i].pix_fmt == encctx->pix_fmt) {
|
|
|
|
sdl->overlay_fmt = sdl_overlay_pix_fmt_map[i].overlay_fmt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sdl->overlay_fmt) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
2012-09-07 13:58:31 +02:00
|
|
|
"Unsupported pixel format '%s', choose one of yuv420p, yuyv422, or uyvy422\n",
|
2011-05-27 01:34:35 +02:00
|
|
|
av_get_pix_fmt_name(encctx->pix_fmt));
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* compute overlay width and height from the codec context information */
|
2012-06-09 21:03:18 +02:00
|
|
|
sar = st->sample_aspect_ratio.num ? st->sample_aspect_ratio : (AVRational){ 1, 1 };
|
|
|
|
dar = av_mul_q(sar, (AVRational){ encctx->width, encctx->height });
|
2011-05-27 01:34:35 +02:00
|
|
|
|
|
|
|
/* we suppose the screen has a 1/1 sample aspect ratio */
|
2012-06-09 21:03:18 +02:00
|
|
|
if (sdl->window_width && sdl->window_height) {
|
|
|
|
/* fit in the window */
|
|
|
|
if (av_cmp_q(dar, (AVRational){ sdl->window_width, sdl->window_height }) > 0) {
|
|
|
|
/* fit in width */
|
|
|
|
sdl->overlay_width = sdl->window_width;
|
|
|
|
sdl->overlay_height = av_rescale(sdl->overlay_width, dar.den, dar.num);
|
|
|
|
} else {
|
|
|
|
/* fit in height */
|
|
|
|
sdl->overlay_height = sdl->window_height;
|
|
|
|
sdl->overlay_width = av_rescale(sdl->overlay_height, dar.num, dar.den);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sar.num > sar.den) {
|
|
|
|
sdl->overlay_width = encctx->width;
|
|
|
|
sdl->overlay_height = av_rescale(sdl->overlay_width, dar.den, dar.num);
|
|
|
|
} else {
|
|
|
|
sdl->overlay_height = encctx->height;
|
|
|
|
sdl->overlay_width = av_rescale(sdl->overlay_height, dar.num, dar.den);
|
|
|
|
}
|
2011-05-27 01:34:35 +02:00
|
|
|
sdl->window_width = sdl->overlay_width;
|
|
|
|
sdl->window_height = sdl->overlay_height;
|
|
|
|
}
|
2012-06-09 21:03:18 +02:00
|
|
|
sdl->overlay_x = (sdl->window_width - sdl->overlay_width ) / 2;
|
|
|
|
sdl->overlay_y = (sdl->window_height - sdl->overlay_height) / 2;
|
2011-05-27 01:34:35 +02:00
|
|
|
|
|
|
|
SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
|
|
|
|
sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
|
|
|
|
24, SDL_SWSURFACE);
|
|
|
|
if (!sdl->surface) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-06-09 12:28:44 +02:00
|
|
|
sdl->overlay = SDL_CreateYUVOverlay(encctx->width, encctx->height,
|
2011-05-27 01:34:35 +02:00
|
|
|
sdl->overlay_fmt, sdl->surface);
|
2012-06-09 12:28:44 +02:00
|
|
|
if (!sdl->overlay || sdl->overlay->pitches[0] < encctx->width) {
|
2011-05-27 01:34:35 +02:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
2012-09-07 13:58:31 +02:00
|
|
|
"SDL does not support an overlay with size of %dx%d pixels\n",
|
2012-06-09 12:28:44 +02:00
|
|
|
encctx->width, encctx->height);
|
2011-05-27 01:34:35 +02:00
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2012-09-07 14:04:04 +02:00
|
|
|
av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d\n",
|
2012-06-09 21:03:18 +02:00
|
|
|
encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt), sar.num, sar.den,
|
|
|
|
sdl->overlay_width, sdl->overlay_height);
|
2011-05-27 01:34:35 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
sdl_write_trailer(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
SDLContext *sdl = s->priv_data;
|
|
|
|
AVCodecContext *encctx = s->streams[0]->codec;
|
2012-06-09 21:03:18 +02:00
|
|
|
SDL_Rect rect = { sdl->overlay_x, sdl->overlay_y, sdl->overlay_width, sdl->overlay_height };
|
2011-05-27 01:34:35 +02:00
|
|
|
AVPicture pict;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
|
|
|
|
|
|
|
|
SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
|
|
|
|
SDL_MapRGB(sdl->surface->format, 0, 0, 0));
|
|
|
|
SDL_LockYUVOverlay(sdl->overlay);
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
sdl->overlay->pixels [i] = pict.data [i];
|
|
|
|
sdl->overlay->pitches[i] = pict.linesize[i];
|
|
|
|
}
|
|
|
|
SDL_DisplayYUVOverlay(sdl->overlay, &rect);
|
|
|
|
SDL_UnlockYUVOverlay(sdl->overlay);
|
|
|
|
|
2012-06-09 21:03:18 +02:00
|
|
|
SDL_UpdateRect(sdl->surface, rect.x, rect.y, rect.w, rect.h);
|
2011-05-27 01:34:35 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(SDLContext,x)
|
|
|
|
|
|
|
|
static const AVOption options[] = {
|
2012-07-19 23:51:56 +02:00
|
|
|
{ "window_title", "set SDL window title", OFFSET(window_title), AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
|
|
|
|
{ "icon_title", "set SDL iconified window title", OFFSET(icon_title) , AV_OPT_TYPE_STRING, {.str = NULL }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
|
|
|
|
{ "window_size", "set SDL window forced size", OFFSET(window_width), AV_OPT_TYPE_IMAGE_SIZE,{.str=NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
|
2011-05-27 01:34:35 +02:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass sdl_class = {
|
|
|
|
.class_name = "sdl outdev",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVOutputFormat ff_sdl_muxer = {
|
|
|
|
.name = "sdl",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("SDL output device"),
|
|
|
|
.priv_data_size = sizeof(SDLContext),
|
2012-08-07 22:45:46 +02:00
|
|
|
.audio_codec = AV_CODEC_ID_NONE,
|
|
|
|
.video_codec = AV_CODEC_ID_RAWVIDEO,
|
2011-05-27 01:34:35 +02:00
|
|
|
.write_header = sdl_write_header,
|
|
|
|
.write_packet = sdl_write_packet,
|
|
|
|
.write_trailer = sdl_write_trailer,
|
2012-06-09 11:28:44 +02:00
|
|
|
.flags = AVFMT_NOFILE | AVFMT_VARIABLE_FPS | AVFMT_NOTIMESTAMPS,
|
2011-05-27 01:34:35 +02:00
|
|
|
.priv_class = &sdl_class,
|
|
|
|
};
|