Mercurial > mplayer.hg
view libmpcodecs/vf_ass.c @ 35070:ff4758e8cfba
Change geometry for corevideo vo to not take dock into account.
It makes the % placements behave non-intuitively, particularly
with an auto-hide dock where it would depend on whether the
dock is visible right when MPlayer starts up.
author | reimar |
---|---|
date | Tue, 11 Sep 2012 20:03:52 +0000 |
parents | 848835e1b053 |
children | 222713e4d79f |
line wrap: on
line source
/* * 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 "config.h" #include "mp_msg.h" #include "help_mp.h" #include "mpcommon.h" #include "img_format.h" #include "mp_image.h" #include "vd.h" #include "vf.h" #include "libvo/fastmemcpy.h" #include "libavutil/intreadwrite.h" #include "sub/sub.h" #include "m_option.h" #include "m_struct.h" #include "sub/ass_mp.h" #include "sub/eosd.h" #define _r(c) ((c)>>24) #define _g(c) (((c)>>16)&0xFF) #define _b(c) (((c)>>8)&0xFF) #define _a(c) ((c)&0xFF) #define rgba2y(c) ( (( 263*_r(c) + 516*_g(c) + 100*_b(c)) >> 10) + 16 ) #define rgba2u(c) ( ((-152*_r(c) - 298*_g(c) + 450*_b(c)) >> 10) + 128 ) #define rgba2v(c) ( (( 450*_r(c) - 376*_g(c) - 73*_b(c)) >> 10) + 128 ) typedef void (*copy_from_image_func)(struct vf_instance *vf, int first_row, int last_row); typedef void (*copy_to_image_func)(struct vf_instance *vf); static const struct vf_priv_s { int outh, outw; int is_planar; unsigned int outfmt; // 1 = auto-added filter: insert only if chain does not support EOSD already // 0 = insert always int auto_insert; unsigned char *planes[3]; unsigned char *dirty_rows; copy_from_image_func copy_from_image; copy_to_image_func copy_to_image; } vf_priv_dflt; static void copy_from_image_yuv420p(struct vf_instance *, int, int); static void copy_to_image_yuv420p(struct vf_instance *); static void copy_from_image_yuv422(struct vf_instance *, int, int); static void copy_to_image_yuv422(struct vf_instance *); static int config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt) { struct mp_eosd_settings res = {0}; switch (outfmt) { case IMGFMT_YV12: case IMGFMT_I420: case IMGFMT_IYUV: vf->priv->is_planar = 1; vf->priv->copy_from_image = copy_from_image_yuv420p; vf->priv->copy_to_image = copy_to_image_yuv420p; break; case IMGFMT_UYVY: case IMGFMT_YUY2: vf->priv->is_planar = 0; vf->priv->copy_from_image = copy_from_image_yuv422; vf->priv->copy_to_image = copy_to_image_yuv422; break; default: return 0; } vf->priv->outfmt = outfmt; vf->priv->outh = height + ass_top_margin + ass_bottom_margin; vf->priv->outw = width; if (!opt_screen_size_x && !opt_screen_size_y) { d_width = d_width * vf->priv->outw / width; d_height = d_height * vf->priv->outh / height; } if (!vf->priv->is_planar) vf->priv->planes[0] = malloc(vf->priv->outw * vf->priv->outh); vf->priv->planes[1] = malloc(vf->priv->outw * vf->priv->outh); vf->priv->planes[2] = malloc(vf->priv->outw * vf->priv->outh); vf->priv->dirty_rows = malloc(vf->priv->outh); res.w = vf->priv->outw; res.h = vf->priv->outh; res.srcw = width; res.srch = height; res.mt = ass_top_margin; res.mb = ass_bottom_margin; eosd_configure(&res); return vf_next_config(vf, vf->priv->outw, vf->priv->outh, d_width, d_height, flags, outfmt); } static void get_image(struct vf_instance *vf, mp_image_t *mpi) { if (mpi->type == MP_IMGTYPE_IPB) return; if (mpi->flags & MP_IMGFLAG_PRESERVE) return; if (mpi->imgfmt != vf->priv->outfmt) return; // colorspace differ // width never changes, always try full DR mpi->priv = vf->dmpi = vf_get_image(vf->next, mpi->imgfmt, mpi->type, mpi->flags | MP_IMGFLAG_READABLE, FFMAX(mpi->width, vf->priv->outw), FFMAX(mpi->height, vf->priv->outh)); if ( (vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) && !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)) { mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_MPCODECS_FullDRNotPossible); return; } // set up mpi as a cropped-down image of dmpi: if (mpi->flags & MP_IMGFLAG_PLANAR) { mpi->planes[0] = vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0]; mpi->planes[1] = vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1]; mpi->planes[2] = vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2]; mpi->stride[1] = vf->dmpi->stride[1]; mpi->stride[2] = vf->dmpi->stride[2]; } else { mpi->planes[0] = vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0]; } mpi->stride[0] = vf->dmpi->stride[0]; mpi->width = vf->dmpi->width; mpi->flags |= MP_IMGFLAG_DIRECT; mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK; // vf->dmpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK; } 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; if (mpi->flags & MP_IMGFLAG_PLANAR) { 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]; } } else { unsigned char packed_color[4]; int x; if (mpi->imgfmt == IMGFMT_UYVY) { packed_color[0] = color[1]; packed_color[1] = color[0]; packed_color[2] = color[2]; packed_color[3] = color[0]; } else { packed_color[0] = color[0]; packed_color[1] = color[1]; packed_color[2] = color[0]; packed_color[3] = color[2]; } dst = mpi->planes[0] + y1 * mpi->stride[0]; for (y = y1; y < y2; ++y) { for (x = 0; x < mpi->w / 2; ++x) AV_COPY32(dst + 4 * x, packed_color); dst += mpi->stride[0]; } } } static int prepare_image(struct vf_instance *vf, mp_image_t *mpi) { if (mpi->flags & MP_IMGFLAG_DIRECT || mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) { vf->dmpi = mpi->priv; if (!vf->dmpi) { mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_MPCODECS_FunWhydowegetNULL); return 0; } mpi->priv = NULL; // we've used DR, so we're ready... if (ass_top_margin) blank(vf->dmpi, 0, ass_top_margin); if (ass_bottom_margin) blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh); if (!(mpi->flags & MP_IMGFLAG_PLANAR)) vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette return 0; } // hope we'll get DR buffer: vf->dmpi = vf_get_image(vf->next, vf->priv->outfmt, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_READABLE, vf->priv->outw, vf->priv->outh); // copy mpi->dmpi... if (mpi->flags & MP_IMGFLAG_PLANAR) { memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0], mpi->planes[0], mpi->w, mpi->h, vf->dmpi->stride[0], mpi->stride[0]); memcpy_pic(vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1], mpi->planes[1], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift, vf->dmpi->stride[1], mpi->stride[1]); memcpy_pic(vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2], mpi->planes[2], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift, vf->dmpi->stride[2], mpi->stride[2]); } else { memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0], mpi->planes[0], mpi->w * (vf->dmpi->bpp / 8), mpi->h, vf->dmpi->stride[0], mpi->stride[0]); vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette } if (ass_top_margin) blank(vf->dmpi, 0, ass_top_margin); if (ass_bottom_margin) blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh); return 0; } /** * \brief Copy specified rows from render_context.dmpi to render_context.planes, upsampling to 4:4:4 */ static void copy_from_image_yuv420p(struct vf_instance *vf, int first_row, int last_row) { int pl; int i, j, k; unsigned char val; int chroma_rows; first_row -= (first_row % 2); last_row += (last_row % 2); chroma_rows = (last_row - first_row) / 2; assert(first_row >= 0); assert(first_row <= last_row); assert(last_row <= vf->priv->outh); for (pl = 1; pl < 3; ++pl) { int dst_stride = vf->priv->outw; int src_stride = vf->dmpi->stride[pl]; unsigned char *src = vf->dmpi->planes[pl] + (first_row / 2) * src_stride; unsigned char *dst = vf->priv->planes[pl] + first_row * dst_stride; unsigned char *dst_next = dst + dst_stride; for (i = 0; i < chroma_rows; ++i) { if ((vf->priv->dirty_rows[first_row + i * 2 ] == 0) || (vf->priv->dirty_rows[first_row + i * 2 + 1] == 0)) { for (j = 0, k = 0; j < vf->dmpi->chroma_width; ++j, k += 2) { val = *(src + j); *(dst + k ) = val; *(dst + k + 1) = val; *(dst_next + k ) = val; *(dst_next + k + 1) = val; } } src += src_stride; dst = dst_next + dst_stride; dst_next = dst + dst_stride; } } for (i = first_row; i < last_row; ++i) vf->priv->dirty_rows[i] = 1; } /** * \brief Copy all previously copied rows back to render_context.dmpi */ static void copy_to_image_yuv420p(struct vf_instance *vf) { int pl; int i, j, k; for (pl = 1; pl < 3; ++pl) { int dst_stride = vf->dmpi->stride[pl]; int src_stride = vf->priv->outw; unsigned char *dst = vf->dmpi->planes[pl]; unsigned char *src = vf->priv->planes[pl]; unsigned char *src_next = vf->priv->planes[pl] + src_stride; for (i = 0; i < vf->priv->outh / 2; ++i) { if ((vf->priv->dirty_rows[i * 2] == 1)) { assert(vf->priv->dirty_rows[i * 2 + 1] == 1); for (j = 0, k = 0; j < vf->dmpi->chroma_width; ++j, k += 2) { unsigned val = 0; val += *(src + k); val += *(src + k + 1); val += *(src_next + k); val += *(src_next + k + 1); *(dst + j) = val >> 2; } } dst += dst_stride; src = src_next + src_stride; src_next = src + src_stride; } } } static void copy_from_image_yuv422(struct vf_instance *vf, int first_row, int last_row) { unsigned char *dirty_rows = vf->priv->dirty_rows; int src_stride = vf->dmpi->stride[0]; int dst_stride = vf->priv->outw; unsigned char *src = vf->dmpi->planes[0] + first_row * src_stride; unsigned char **dst = vf->priv->planes; int dst_off = first_row * dst_stride; int is_uyvy = vf->priv->outfmt == IMGFMT_UYVY; int i, j, k; for (i = first_row; i < last_row; ++i) { int next_off = dst_off + dst_stride; if (!dirty_rows[i]) { if (is_uyvy) { for (j = dst_off, k = 0; j < next_off; j += 2, k += 4) { dst[0][j ] = src[k + 1]; dst[0][j + 1] = src[k + 3]; dst[1][j ] = src[k ]; dst[1][j + 1] = src[k ]; dst[2][j ] = src[k + 2]; dst[2][j + 1] = src[k + 2]; } } else { for (j = dst_off, k = 0; j < next_off; j += 2, k += 4) { dst[0][j ] = src[k ]; dst[0][j + 1] = src[k + 2]; dst[1][j ] = src[k + 1]; dst[1][j + 1] = src[k + 1]; dst[2][j ] = src[k + 3]; dst[2][j + 1] = src[k + 3]; } } } src += src_stride; dst_off = next_off; } for (i = first_row; i < last_row; ++i) dirty_rows[i] = 1; } static void copy_to_image_yuv422(struct vf_instance *vf) { unsigned char *dirty_rows = vf->priv->dirty_rows; int src_stride = vf->priv->outw; int dst_stride = vf->dmpi->stride[0]; int height = vf->priv->outh; unsigned char **src = vf->priv->planes; unsigned char *dst = vf->dmpi->planes[0]; int src_off = 0; int is_uyvy = vf->priv->outfmt == IMGFMT_UYVY; int i, j, k; for (i = 0; i < height; ++i) { int next_off = src_off + src_stride; if (*dirty_rows++) { #define AVERAGE(a, b) (((unsigned)(a) + (unsigned)(b)) >> 1) if (is_uyvy) { for (j = src_off, k = 0; j < next_off; j += 2, k += 4) { dst[k ] = AVERAGE(src[1][j], src[1][j + 1]); dst[k + 1] = src[0][j]; dst[k + 2] = AVERAGE(src[2][j], src[2][j + 1]); dst[k + 3] = src[0][j + 1]; } } else { for (j = src_off, k = 0; j < next_off; j += 2, k += 4) { dst[k ] = src[0][j]; dst[k + 1] = AVERAGE(src[1][j], src[1][j + 1]); dst[k + 2] = src[0][j + 1]; dst[k + 3] = AVERAGE(src[2][j], src[2][j + 1]); } } #undef AVERAGE } src_off = next_off; dst += dst_stride; } } static void my_draw_bitmap(struct vf_instance *vf, unsigned char *bitmap, int bitmap_w, int bitmap_h, int stride, int dst_x, int dst_y, unsigned color) { unsigned char y = rgba2y(color); unsigned char u = rgba2u(color); unsigned char v = rgba2v(color); unsigned opacity = 255 - _a(color); unsigned char *src, *dsty, *dstu, *dstv; int i, j; mp_image_t *dmpi = vf->dmpi; int stride_y = vf->priv->is_planar ? dmpi->stride[0] : vf->priv->outw; opacity = (0x10203 * opacity + 0x80) >> 8; /* 0x10203 = (1<<32)/(255*255) */ /* 0 <= opacity <= 0x10101 */ src = bitmap; dsty = vf->priv->is_planar ? dmpi->planes[0] : vf->priv->planes[0]; dsty += dst_x + dst_y * stride_y; dstu = vf->priv->planes[1] + dst_x + dst_y * vf->priv->outw; dstv = vf->priv->planes[2] + dst_x + dst_y * vf->priv->outw; for (i = 0; i < bitmap_h; ++i) { for (j = 0; j < bitmap_w; ++j) { unsigned k = src[j]; if (!k) continue; k *= opacity; /* 0 <= k <= 0xFFFFFF */ dsty[j] = (k * y + (0xFFFFFF - k) * dsty[j] + 0x800000) >> 24; dstu[j] = (k * u + (0xFFFFFF - k) * dstu[j] + 0x800000) >> 24; dstv[j] = (k * v + (0xFFFFFF - k) * dstv[j] + 0x800000) >> 24; } src += stride; dsty += stride_y; dstu += vf->priv->outw; dstv += vf->priv->outw; } } static void render_frame(struct vf_instance *vf, mp_image_t *mpi, struct mp_eosd_image_list *images) { struct mp_eosd_image *img; copy_from_image_func copy_from_image = vf->priv->copy_from_image; copy_to_image_func copy_to_image = vf->priv->copy_to_image; img = eosd_image_first(images); if (!img) return; memset(vf->priv->dirty_rows, 0, vf->priv->outh); // reset dirty rows while (img) { copy_from_image(vf, img->dst_y, img->dst_y + img->h); my_draw_bitmap(vf, img->bitmap, img->w, img->h, img->stride, img->dst_x, img->dst_y, img->color); img = eosd_image_next(images); } copy_to_image(vf); } static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts) { struct mp_eosd_image_list images; eosd_render_frame(pts, &images); prepare_image(vf, mpi); render_frame(vf, mpi, &images); return vf_next_put_image(vf, vf->dmpi, pts); } static int query_format(struct vf_instance *vf, unsigned int fmt) { switch (fmt) { case IMGFMT_YV12: case IMGFMT_I420: case IMGFMT_IYUV: case IMGFMT_UYVY: case IMGFMT_YUY2: return vf_next_query_format(vf, fmt) | VFCAP_EOSD; } return 0; } static int control(vf_instance_t *vf, int request, void *data) { switch (request) { case VFCTRL_INIT_EOSD: return CONTROL_TRUE; case VFCTRL_DRAW_EOSD: return CONTROL_TRUE; } return vf_next_control(vf, request, data); } static void uninit(struct vf_instance *vf) { if (!vf->priv->is_planar) free(vf->priv->planes[0]); free(vf->priv->planes[1]); free(vf->priv->planes[2]); free(vf->priv->dirty_rows); } static const unsigned int fmt_list[] = { IMGFMT_YV12, IMGFMT_I420, IMGFMT_IYUV, IMGFMT_UYVY, IMGFMT_YUY2, 0 }; static int vf_open(vf_instance_t *vf, char *args) { int flags; unsigned outfmt = vf_match_csp(&vf->next, fmt_list, IMGFMT_YV12); if (outfmt) flags = vf_next_query_format(vf, outfmt); if (!outfmt || (vf->priv->auto_insert && flags & VFCAP_EOSD)) { uninit(vf); return 0; } if (vf->priv->auto_insert) mp_msg(MSGT_ASS, MSGL_INFO, "[ass] auto-open\n"); vf->config = config; vf->query_format = query_format; vf->uninit = uninit; vf->control = control; vf->get_image = get_image; vf->put_image = put_image; vf->default_caps = VFCAP_EOSD; return 1; } #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f) static const m_option_t vf_opts_fields[] = { {"auto", ST_OFF(auto_insert), CONF_TYPE_FLAG, 0, 0, 1, NULL}, {NULL, NULL, 0, 0, 0, 0, NULL} }; static const m_struct_t vf_opts = { "ass", sizeof(struct vf_priv_s), &vf_priv_dflt, vf_opts_fields }; const vf_info_t vf_info_ass = { "Render ASS/SSA subtitles", "ass", "Evgeniy Stepanov", "", vf_open, &vf_opts };