mpv/video/filter/vf_sub.c

253 lines
6.8 KiB
C
Raw Normal View History

/*
* Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include <libavutil/common.h>
#include "config.h"
#include "core/mp_msg.h"
#include "core/options.h"
#include "video/img_format.h"
#include "video/mp_image.h"
#include "vf.h"
#include "sub/sub.h"
#include "sub/dec_sub.h"
#include "video/memcpy_pic.h"
#include "video/csputils.h"
#include "core/m_option.h"
#include "core/m_struct.h"
static const struct vf_priv_s {
int opt_top_margin, opt_bottom_margin;
int outh, outw;
unsigned int outfmt;
struct mp_csp_details csp;
struct osd_state *osd;
struct mp_osd_res dim;
} vf_priv_dflt = {
.csp = MP_CSP_DETAILS_DEFAULTS,
};
static int config(struct vf_instance *vf,
int width, int height, int d_width, int d_height,
unsigned int flags, unsigned int outfmt)
{
struct MPOpts *opts = vf->opts;
vf->priv->outh = height + vf->priv->opt_top_margin +
vf->priv->opt_bottom_margin;
vf->priv->outw = width;
double dar = (double)d_width / d_height;
double sar = (double)width / height;
if (!opts->screen_size_x && !opts->screen_size_y) {
d_width = d_width * vf->priv->outw / width;
d_height = d_height * vf->priv->outh / height;
}
vf->priv->dim = (struct mp_osd_res) {
.w = vf->priv->outw,
.h = vf->priv->outh,
.mt = vf->priv->opt_top_margin,
.mb = vf->priv->opt_bottom_margin,
.display_par = sar / dar,
.video_par = dar / sar,
};
return vf_next_config(vf, vf->priv->outw, vf->priv->outh, d_width,
d_height, flags, outfmt);
}
static void blank(mp_image_t *mpi, int y1, int y2)
{
int color[3] = {16, 128, 128}; // black (YUV)
int y;
unsigned char *dst;
int chroma_rows = (y2 - y1) >> mpi->chroma_y_shift;
dst = mpi->planes[0] + y1 * mpi->stride[0];
for (y = 0; y < y2 - y1; ++y) {
memset(dst, color[0], mpi->w);
dst += mpi->stride[0];
}
dst = mpi->planes[1] + (y1 >> mpi->chroma_y_shift) * mpi->stride[1];
for (y = 0; y < chroma_rows; ++y) {
memset(dst, color[1], mpi->chroma_width);
dst += mpi->stride[1];
}
dst = mpi->planes[2] + (y1 >> mpi->chroma_y_shift) * mpi->stride[2];
for (y = 0; y < chroma_rows; ++y) {
memset(dst, color[2], mpi->chroma_width);
dst += mpi->stride[2];
}
}
static void prepare_image(struct vf_instance *vf, struct mp_image *dmpi,
struct mp_image *mpi)
{
int tmargin = vf->priv->opt_top_margin;
// copy mpi->dmpi...
if (mpi->flags & MP_IMGFLAG_PLANAR) {
memcpy_pic(dmpi->planes[0] + tmargin * dmpi->stride[0],
mpi->planes[0],
mpi->w,
mpi->h,
dmpi->stride[0],
mpi->stride[0]);
memcpy_pic(dmpi->planes[1] + (tmargin >> mpi->chroma_y_shift) * dmpi->stride[1],
mpi->planes[1],
mpi->w >> mpi->chroma_x_shift,
mpi->h >> mpi->chroma_y_shift,
dmpi->stride[1],
mpi->stride[1]);
memcpy_pic(dmpi->planes[2] + (tmargin >> mpi->chroma_y_shift) * dmpi->stride[2],
mpi->planes[2],
mpi->w >> mpi->chroma_x_shift,
mpi->h >> mpi->chroma_y_shift,
dmpi->stride[2],
mpi->stride[2]);
} else {
memcpy_pic(dmpi->planes[0] + tmargin * dmpi->stride[0],
mpi->planes[0],
mpi->w * (dmpi->bpp / 8),
mpi->h,
dmpi->stride[0],
mpi->stride[0]);
}
if (tmargin)
blank(dmpi, 0, tmargin);
if (vf->priv->opt_bottom_margin)
blank(dmpi, vf->priv->outh - vf->priv->opt_bottom_margin,
vf->priv->outh);
}
static struct mp_image *filter(struct vf_instance *vf, struct mp_image *mpi)
{
struct vf_priv_s *priv = vf->priv;
struct osd_state *osd = priv->osd;
if (vf->priv->opt_top_margin || vf->priv->opt_bottom_margin ||
!mp_image_is_writeable(mpi))
{
struct mp_image *dmpi = vf_alloc_out_image(vf);
mp_image_copy_attributes(dmpi, mpi);
prepare_image(vf, dmpi, mpi);
talloc_free(mpi);
mpi = dmpi;
}
mp_image_set_colorspace_details(mpi, &priv->csp);
if (mpi->pts != MP_NOPTS_VALUE)
osd_draw_on_image(osd, priv->dim, mpi->pts, OSD_DRAW_SUB_FILTER, mpi);
return mpi;
}
static int query_format(struct vf_instance *vf, unsigned int fmt)
{
switch (fmt) {
video: decouple internal pixel formats from FourCCs mplayer's video chain traditionally used FourCCs for pixel formats. For example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the string 'YV12' interpreted as unsigned int. Additionally, it used to encode information into the numeric values of some formats. The RGB formats had their bit depth and endian encoded into the least significant byte. Extended planar formats (420P10 etc.) had chroma shift, endian, and component bit depth encoded. (This has been removed in recent commits.) Replace the FourCC mess with a simple enum. Remove all the redundant formats like YV12/I420/IYUV. Replace some image format names by something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P. Add img_fourcc.h, which contains the old IDs for code that actually uses FourCCs. Change the way demuxers, that output raw video, identify the video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to request the rawvideo decoder, and sh_video->imgfmt specifies the pixel format. Like the previous hack, this is supposed to avoid the need for a complete codecs.cfg entry per format, or other lookup tables. (Note that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT raw video, but this is still considered better than adding a raw video decoder - even if trivial, it would be full of annoying lookup tables.) The TV code has not been tested. Some corrective changes regarding endian and other image format flags creep in.
2012-12-23 19:03:30 +00:00
case IMGFMT_420P:
return vf_next_query_format(vf, vf->priv->outfmt);
}
return 0;
}
static int control(vf_instance_t *vf, int request, void *data)
{
switch (request) {
case VFCTRL_SET_OSD_OBJ:
vf->priv->osd = data;
break;
case VFCTRL_INIT_OSD:
subs: libass: use a single persistent renderer for subtitles To draw libass subtitles, the code used ASS_Renderer objects created in vf_vo (VO rendering) or vf_ass. They were destroyed and recreated together with the video filter chain. Change the code to use a single persistent renderer instance stored in the main osd_state struct. Because libass seems to misbehave if fonts are changed while a renderer exists (even if ass_set_fonts() is called on the renderer afterwards), the renderer is recreated after adding embedded fonts. The known benefits are simpler code and avoiding delays when switching between timeline parts from different files (libass fontconfig initialization, needed when creating a new renderer, can take a long time in some cases; switching between files rebuilds the video filter chain, and this required recreating the renderers). On the other hand, I'm not sure whether this could cause inefficient bitmap caching in libass; explicitly resetting the renderer in some cases could be beneficial. The new code does not keep the distinction of separate renderers for vsfilter munged aspect vs normal; this means that changing subtitle tracks can lose cache for the previous track. The new code always sets some libass parameters on each rendering call, which were previously only set if they had potentially changed. This should be harmless as libass itself has checks to see if the values differ from previous ones. Conflicts: command.c libmpcodecs/vf_ass.c libmpcodecs/vf_vo.c mplayer.c sub/ass_mp.c
2012-08-25 14:47:50 +00:00
return CONTROL_TRUE;
case VFCTRL_SET_YUV_COLORSPACE: {
struct mp_csp_details colorspace = *(struct mp_csp_details *)data;
vf->priv->csp = colorspace;
break;
}
}
return vf_next_control(vf, request, data);
}
static void uninit(struct vf_instance *vf)
{
free(vf->priv);
}
static const unsigned int fmt_list[] = {
video: decouple internal pixel formats from FourCCs mplayer's video chain traditionally used FourCCs for pixel formats. For example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the string 'YV12' interpreted as unsigned int. Additionally, it used to encode information into the numeric values of some formats. The RGB formats had their bit depth and endian encoded into the least significant byte. Extended planar formats (420P10 etc.) had chroma shift, endian, and component bit depth encoded. (This has been removed in recent commits.) Replace the FourCC mess with a simple enum. Remove all the redundant formats like YV12/I420/IYUV. Replace some image format names by something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P. Add img_fourcc.h, which contains the old IDs for code that actually uses FourCCs. Change the way demuxers, that output raw video, identify the video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to request the rawvideo decoder, and sh_video->imgfmt specifies the pixel format. Like the previous hack, this is supposed to avoid the need for a complete codecs.cfg entry per format, or other lookup tables. (Note that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT raw video, but this is still considered better than adding a raw video decoder - even if trivial, it would be full of annoying lookup tables.) The TV code has not been tested. Some corrective changes regarding endian and other image format flags creep in.
2012-12-23 19:03:30 +00:00
IMGFMT_420P,
0
};
static int vf_open(vf_instance_t *vf, char *args)
{
video: decouple internal pixel formats from FourCCs mplayer's video chain traditionally used FourCCs for pixel formats. For example, it used IMGFMT_YV12 for 4:2:0 YUV, which was defined to the string 'YV12' interpreted as unsigned int. Additionally, it used to encode information into the numeric values of some formats. The RGB formats had their bit depth and endian encoded into the least significant byte. Extended planar formats (420P10 etc.) had chroma shift, endian, and component bit depth encoded. (This has been removed in recent commits.) Replace the FourCC mess with a simple enum. Remove all the redundant formats like YV12/I420/IYUV. Replace some image format names by something more intuitive, most importantly IMGFMT_YV12 -> IMGFMT_420P. Add img_fourcc.h, which contains the old IDs for code that actually uses FourCCs. Change the way demuxers, that output raw video, identify the video format: they set either MP_FOURCC_RAWVIDEO or MP_FOURCC_IMGFMT to request the rawvideo decoder, and sh_video->imgfmt specifies the pixel format. Like the previous hack, this is supposed to avoid the need for a complete codecs.cfg entry per format, or other lookup tables. (Note that the RGB raw video FourCCs mostly rely on ffmpeg's mappings for NUT raw video, but this is still considered better than adding a raw video decoder - even if trivial, it would be full of annoying lookup tables.) The TV code has not been tested. Some corrective changes regarding endian and other image format flags creep in.
2012-12-23 19:03:30 +00:00
vf->priv->outfmt = vf_match_csp(&vf->next, fmt_list, IMGFMT_420P);
if (!vf->priv->outfmt) {
uninit(vf);
return 0;
}
vf->config = config;
vf->query_format = query_format;
vf->uninit = uninit;
vf->control = control;
vf->filter = filter;
vf->default_caps = VFCAP_OSD;
return 1;
}
#define ST_OFF(f) M_ST_OFF(struct vf_priv_s, f)
static const m_option_t vf_opts_fields[] = {
{"bottom-margin", ST_OFF(opt_bottom_margin),
CONF_TYPE_INT, M_OPT_RANGE, 0, 2000},
{"top-margin", ST_OFF(opt_top_margin),
CONF_TYPE_INT, M_OPT_RANGE, 0, 2000},
{NULL, NULL, 0, 0, 0, 0, NULL}
};
static const m_struct_t vf_opts = {
"sub",
sizeof(struct vf_priv_s),
&vf_priv_dflt,
vf_opts_fields
};
const vf_info_t vf_info_sub = {
"Render subtitles",
"sub",
"Evgeniy Stepanov",
"",
vf_open,
&vf_opts
};