Merge svn changes up to r30876
[mplayer/kovensky.git] / libmpcodecs / vf_ass.c
blob427129484d5a90485d616e9358e8e74a440af699
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
3 /*
4 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <inttypes.h>
29 #include <assert.h>
31 #include "config.h"
32 #include "mp_msg.h"
33 #include "help_mp.h"
34 #include "options.h"
36 #include "img_format.h"
37 #include "mp_image.h"
38 #include "vf.h"
40 #include "libvo/fastmemcpy.h"
42 #include "m_option.h"
43 #include "m_struct.h"
45 #include "ass_mp.h"
47 #define _r(c) ((c)>>24)
48 #define _g(c) (((c)>>16)&0xFF)
49 #define _b(c) (((c)>>8)&0xFF)
50 #define _a(c) ((c)&0xFF)
51 #define rgba2y(c) ( (( 263*_r(c) + 516*_g(c) + 100*_b(c)) >> 10) + 16 )
52 #define rgba2u(c) ( ((-152*_r(c) - 298*_g(c) + 450*_b(c)) >> 10) + 128 )
53 #define rgba2v(c) ( (( 450*_r(c) - 376*_g(c) - 73*_b(c)) >> 10) + 128 )
56 static const struct vf_priv_s {
57 int outh, outw;
59 unsigned int outfmt;
61 // 1 = auto-added filter: insert only if chain does not support EOSD already
62 // 0 = insert always
63 int auto_insert;
65 ASS_Renderer* ass_priv;
67 unsigned char* planes[3];
68 struct line_limits {
69 uint16_t start;
70 uint16_t end;
71 } *line_limits;
72 } vf_priv_dflt;
74 extern ASS_Track *ass_track;
75 extern float sub_delay;
76 extern int sub_visibility;
78 static int config(struct vf_instance* vf,
79 int width, int height, int d_width, int d_height,
80 unsigned int flags, unsigned int outfmt)
82 struct MPOpts *opts = vf->opts;
83 if (outfmt == IMGFMT_IF09) return 0;
85 vf->priv->outh = height + ass_top_margin + ass_bottom_margin;
86 vf->priv->outw = width;
88 if (!opts->screen_size_x && !opts->screen_size_y) {
89 d_width = d_width * vf->priv->outw / width;
90 d_height = d_height * vf->priv->outh / height;
93 vf->priv->planes[1] = malloc(vf->priv->outw * vf->priv->outh);
94 vf->priv->planes[2] = malloc(vf->priv->outw * vf->priv->outh);
95 vf->priv->line_limits = malloc((vf->priv->outh + 1) / 2 * sizeof(*vf->priv->line_limits));
97 if (vf->priv->ass_priv) {
98 ass_configure(vf->priv->ass_priv, vf->priv->outw, vf->priv->outh, 0);
99 ass_set_aspect_ratio(vf->priv->ass_priv, 1, 1);
102 return vf_next_config(vf, vf->priv->outw, vf->priv->outh, d_width, d_height, flags, outfmt);
105 static void get_image(struct vf_instance* vf, mp_image_t *mpi)
107 if(mpi->type == MP_IMGTYPE_IPB) return;
108 if(mpi->flags & MP_IMGFLAG_PRESERVE) return;
109 if(mpi->imgfmt != vf->priv->outfmt) return; // colorspace differ
111 // width never changes, always try full DR
112 mpi->priv = vf->dmpi = vf_get_image(vf->next, mpi->imgfmt,
113 mpi->type, mpi->flags | MP_IMGFLAG_READABLE,
114 vf->priv->outw,
115 vf->priv->outh);
117 if((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
118 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)){
119 mp_tmsg(MSGT_ASS, MSGL_INFO, "Full DR not possible, trying SLICES instead!\n");
120 return;
123 // set up mpi as a cropped-down image of dmpi:
124 if(mpi->flags&MP_IMGFLAG_PLANAR){
125 mpi->planes[0]=vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
126 mpi->planes[1]=vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1];
127 mpi->planes[2]=vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2];
128 mpi->stride[1]=vf->dmpi->stride[1];
129 mpi->stride[2]=vf->dmpi->stride[2];
130 } else {
131 mpi->planes[0]=vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
133 mpi->stride[0]=vf->dmpi->stride[0];
134 mpi->width=vf->dmpi->width;
135 mpi->flags|=MP_IMGFLAG_DIRECT;
136 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
137 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
140 static void blank(mp_image_t *mpi, int y1, int y2)
142 int color[3] = {16, 128, 128}; // black (YUV)
143 int y;
144 unsigned char* dst;
145 int chroma_rows = (y2 - y1) >> mpi->chroma_y_shift;
147 dst = mpi->planes[0] + y1 * mpi->stride[0];
148 for (y = 0; y < y2 - y1; ++y) {
149 memset(dst, color[0], mpi->w);
150 dst += mpi->stride[0];
152 dst = mpi->planes[1] + (y1 >> mpi->chroma_y_shift) * mpi->stride[1];
153 for (y = 0; y < chroma_rows ; ++y) {
154 memset(dst, color[1], mpi->chroma_width);
155 dst += mpi->stride[1];
157 dst = mpi->planes[2] + (y1 >> mpi->chroma_y_shift) * mpi->stride[2];
158 for (y = 0; y < chroma_rows ; ++y) {
159 memset(dst, color[2], mpi->chroma_width);
160 dst += mpi->stride[2];
164 static int prepare_image(struct vf_instance* vf, mp_image_t *mpi)
166 if(mpi->flags&MP_IMGFLAG_DIRECT || mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){
167 vf->dmpi = mpi->priv;
168 if (!vf->dmpi) { mp_tmsg(MSGT_ASS, MSGL_WARN, "Why do we get NULL??\n"); return 0; }
169 mpi->priv = NULL;
170 // we've used DR, so we're ready...
171 if (ass_top_margin)
172 blank(vf->dmpi, 0, ass_top_margin);
173 if (ass_bottom_margin)
174 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
175 if(!(mpi->flags&MP_IMGFLAG_PLANAR))
176 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
177 return 0;
180 // hope we'll get DR buffer:
181 vf->dmpi = vf_get_image(vf->next, vf->priv->outfmt,
182 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_READABLE,
183 vf->priv->outw, vf->priv->outh);
185 // copy mpi->dmpi...
186 if(mpi->flags&MP_IMGFLAG_PLANAR){
187 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
188 mpi->planes[0], mpi->w, mpi->h,
189 vf->dmpi->stride[0], mpi->stride[0]);
190 memcpy_pic(vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1],
191 mpi->planes[1], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift,
192 vf->dmpi->stride[1], mpi->stride[1]);
193 memcpy_pic(vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2],
194 mpi->planes[2], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift,
195 vf->dmpi->stride[2], mpi->stride[2]);
196 } else {
197 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
198 mpi->planes[0], mpi->w*(vf->dmpi->bpp/8), mpi->h,
199 vf->dmpi->stride[0], mpi->stride[0]);
200 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
202 if (ass_top_margin)
203 blank(vf->dmpi, 0, ass_top_margin);
204 if (ass_bottom_margin)
205 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
206 return 0;
209 static void update_limits(struct vf_instance *vf, int starty, int endy,
210 int startx, int endx)
212 starty >>= 1;
213 endy = (endy + 1) >> 1;
214 startx >>= 1;
215 endx = (endx + 1) >> 1;
216 for (int i = starty; i < endy; i++) {
217 struct line_limits *ll = vf->priv->line_limits + i;
218 if (startx < ll->start)
219 ll->start = startx;
220 if (endx > ll->end)
221 ll->end = endx;
226 * \brief Copy specified rows from render_context.dmpi to render_context.planes, upsampling to 4:4:4
228 static void copy_from_image(struct vf_instance* vf)
230 int pl;
232 for (pl = 1; pl < 3; ++pl) {
233 int dst_stride = vf->priv->outw;
234 int src_stride = vf->dmpi->stride[pl];
236 unsigned char* src = vf->dmpi->planes[pl];
237 unsigned char* dst = vf->priv->planes[pl];
238 for (int i = 0; i < (vf->priv->outh + 1) / 2; i++) {
239 struct line_limits *ll = vf->priv->line_limits + i;
240 unsigned char* dst_next = dst + dst_stride;
241 for (int j = ll->start; j < ll->end; j++) {
242 unsigned char val = src[j];
243 dst[j << 1] = val;
244 dst[(j << 1) + 1] = val;
245 dst_next[j << 1] = val;
246 dst_next[(j << 1) + 1] = val;
248 src += src_stride;
249 dst = dst_next + dst_stride;
255 * \brief Copy all previously copied rows back to render_context.dmpi
257 static void copy_to_image(struct vf_instance* vf)
259 int pl;
260 int i, j;
261 for (pl = 1; pl < 3; ++pl) {
262 int dst_stride = vf->dmpi->stride[pl];
263 int src_stride = vf->priv->outw;
265 unsigned char* dst = vf->dmpi->planes[pl];
266 unsigned char* src = vf->priv->planes[pl];
267 unsigned char* src_next = vf->priv->planes[pl] + src_stride;
268 for (i = 0; i < vf->dmpi->chroma_height; ++i) {
269 for (j = vf->priv->line_limits[i].start; j < vf->priv->line_limits[i].end; j++) {
270 unsigned val = 0;
271 val += src[j << 1];
272 val += src[(j << 1) + 1];
273 val += src_next[j << 1];
274 val += src_next[(j << 1) + 1];
275 dst[j] = val >> 2;
277 dst += dst_stride;
278 src = src_next + src_stride;
279 src_next = src + src_stride;
284 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)
286 unsigned char y = rgba2y(color);
287 unsigned char u = rgba2u(color);
288 unsigned char v = rgba2v(color);
289 unsigned char opacity = 255 - _a(color);
290 unsigned char *src, *dsty, *dstu, *dstv;
291 int i, j;
292 mp_image_t* dmpi = vf->dmpi;
294 src = bitmap;
295 dsty = dmpi->planes[0] + dst_x + dst_y * dmpi->stride[0];
296 dstu = vf->priv->planes[1] + dst_x + dst_y * vf->priv->outw;
297 dstv = vf->priv->planes[2] + dst_x + dst_y * vf->priv->outw;
298 for (i = 0; i < bitmap_h; ++i) {
299 for (j = 0; j < bitmap_w; ++j) {
300 unsigned k = (src[j] * opacity + 255) >> 8;
301 dsty[j] = (k*y + (255-k)*dsty[j] + 255) >> 8;
302 dstu[j] = (k*u + (255-k)*dstu[j] + 255) >> 8;
303 dstv[j] = (k*v + (255-k)*dstv[j] + 255) >> 8;
305 src += stride;
306 dsty += dmpi->stride[0];
307 dstu += vf->priv->outw;
308 dstv += vf->priv->outw;
312 static int render_frame(struct vf_instance* vf, mp_image_t *mpi, const ASS_Image *img)
314 if (img) {
315 for (int i = 0; i < (vf->priv->outh + 1) / 2; i++)
316 vf->priv->line_limits[i] = (struct line_limits){65535, 0};
317 for (const ASS_Image *im = img; im; im = im->next)
318 update_limits(vf, im->dst_y, im->dst_y + im->h, im->dst_x, im->dst_x + im->w);
319 copy_from_image(vf);
320 while (img) {
321 my_draw_bitmap(vf, img->bitmap, img->w, img->h, img->stride,
322 img->dst_x, img->dst_y, img->color);
323 img = img->next;
325 copy_to_image(vf);
327 return 0;
330 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts)
332 ASS_Image* images = 0;
333 if (sub_visibility && vf->priv->ass_priv && ass_track && (pts != MP_NOPTS_VALUE))
334 images = ass_mp_render_frame(vf->priv->ass_priv, ass_track, (pts+sub_delay) * 1000 + .5, NULL);
336 prepare_image(vf, mpi);
337 if (images) render_frame(vf, mpi, images);
339 return vf_next_put_image(vf, vf->dmpi, pts);
342 static int query_format(struct vf_instance* vf, unsigned int fmt)
344 switch(fmt){
345 case IMGFMT_YV12:
346 case IMGFMT_I420:
347 case IMGFMT_IYUV:
348 return vf_next_query_format(vf, vf->priv->outfmt);
350 return 0;
353 static int control(vf_instance_t *vf, int request, void *data)
355 switch (request) {
356 case VFCTRL_INIT_EOSD:
357 vf->priv->ass_priv = ass_renderer_init((ASS_Library*)data);
358 if (!vf->priv->ass_priv) return CONTROL_FALSE;
359 ass_configure_fonts(vf->priv->ass_priv);
360 return CONTROL_TRUE;
361 case VFCTRL_DRAW_EOSD:
362 if (vf->priv->ass_priv) return CONTROL_TRUE;
363 break;
365 return vf_next_control(vf, request, data);
368 static void uninit(struct vf_instance* vf)
370 if (vf->priv->ass_priv)
371 ass_renderer_done(vf->priv->ass_priv);
372 free(vf->priv->planes[1]);
373 free(vf->priv->planes[2]);
374 free(vf->priv->line_limits);
375 free(vf->priv);
378 static const unsigned int fmt_list[]={
379 IMGFMT_YV12,
380 IMGFMT_I420,
381 IMGFMT_IYUV,
385 static int vf_open(vf_instance_t *vf, char *args)
387 int flags;
388 vf->priv->outfmt = vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
389 if (vf->priv->outfmt)
390 flags = vf_next_query_format(vf, vf->priv->outfmt);
391 if (!vf->priv->outfmt) {
392 uninit(vf);
393 return 0;
394 } else if (vf->priv->auto_insert && flags&VFCAP_EOSD) {
395 uninit(vf);
396 return -1;
399 if (vf->priv->auto_insert)
400 mp_msg(MSGT_ASS, MSGL_INFO, "[ass] auto-open\n");
402 vf->config = config;
403 vf->query_format = query_format;
404 vf->uninit = uninit;
405 vf->control = control;
406 vf->get_image = get_image;
407 vf->put_image = put_image;
408 vf->default_caps=VFCAP_EOSD;
409 return 1;
412 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
413 static const m_option_t vf_opts_fields[] = {
414 {"auto", ST_OFF(auto_insert), CONF_TYPE_FLAG, 0 , 0, 1, NULL},
415 { NULL, NULL, 0, 0, 0, 0, NULL }
418 static const m_struct_t vf_opts = {
419 "ass",
420 sizeof(struct vf_priv_s),
421 &vf_priv_dflt,
422 vf_opts_fields
425 const vf_info_t vf_info_ass = {
426 "Render ASS/SSA subtitles",
427 "ass",
428 "Evgeniy Stepanov",
430 vf_open,
431 &vf_opts