subtitles: move global ass_track to struct osd_state
[mplayer/glamo.git] / libmpcodecs / vf_ass.c
blobf1a43b52bd33f04845908b84b41ad2f9edea8d50
1 /*
2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <assert.h>
29 #include "config.h"
30 #include "mp_msg.h"
31 #include "options.h"
33 #include "img_format.h"
34 #include "mp_image.h"
35 #include "vf.h"
36 #include "libvo/sub.h"
38 #include "libvo/fastmemcpy.h"
40 #include "m_option.h"
41 #include "m_struct.h"
43 #include "ass_mp.h"
45 #define _r(c) ((c)>>24)
46 #define _g(c) (((c)>>16)&0xFF)
47 #define _b(c) (((c)>>8)&0xFF)
48 #define _a(c) ((c)&0xFF)
49 #define rgba2y(c) ( (( 263*_r(c) + 516*_g(c) + 100*_b(c)) >> 10) + 16 )
50 #define rgba2u(c) ( ((-152*_r(c) - 298*_g(c) + 450*_b(c)) >> 10) + 128 )
51 #define rgba2v(c) ( (( 450*_r(c) - 376*_g(c) - 73*_b(c)) >> 10) + 128 )
54 static const struct vf_priv_s {
55 int outh, outw;
57 unsigned int outfmt;
59 // 1 = auto-added filter: insert only if chain does not support EOSD already
60 // 0 = insert always
61 int auto_insert;
63 struct osd_state *osd;
64 ASS_Renderer *ass_priv;
66 unsigned char *planes[3];
67 struct line_limits {
68 uint16_t start;
69 uint16_t end;
70 } *line_limits;
71 } vf_priv_dflt;
73 extern float sub_delay;
75 static int config(struct vf_instance *vf,
76 int width, int height, int d_width, int d_height,
77 unsigned int flags, unsigned int outfmt)
79 struct MPOpts *opts = vf->opts;
80 if (outfmt == IMGFMT_IF09)
81 return 0;
83 vf->priv->outh = height + ass_top_margin + ass_bottom_margin;
84 vf->priv->outw = width;
86 if (!opts->screen_size_x && !opts->screen_size_y) {
87 d_width = d_width * vf->priv->outw / width;
88 d_height = d_height * vf->priv->outh / height;
91 vf->priv->planes[1] = malloc(vf->priv->outw * vf->priv->outh);
92 vf->priv->planes[2] = malloc(vf->priv->outw * vf->priv->outh);
93 vf->priv->line_limits = malloc((vf->priv->outh + 1) / 2 * sizeof(*vf->priv->line_limits));
95 if (vf->priv->ass_priv) {
96 ass_configure(vf->priv->ass_priv, vf->priv->outw, vf->priv->outh, 0);
97 ass_set_aspect_ratio(vf->priv->ass_priv, 1, 1);
100 return vf_next_config(vf, vf->priv->outw, vf->priv->outh, d_width,
101 d_height, flags, outfmt);
104 static void get_image(struct vf_instance *vf, mp_image_t *mpi)
106 if (mpi->type == MP_IMGTYPE_IPB)
107 return;
108 if (mpi->flags & MP_IMGFLAG_PRESERVE)
109 return;
110 if (mpi->imgfmt != vf->priv->outfmt)
111 return; // colorspace differ
113 // width never changes, always try full DR
114 mpi->priv = vf->dmpi = vf_get_image(vf->next, mpi->imgfmt, mpi->type,
115 mpi->flags | MP_IMGFLAG_READABLE,
116 vf->priv->outw, vf->priv->outh);
118 if ((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
119 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)) {
120 mp_tmsg(MSGT_ASS, MSGL_INFO, "Full DR not possible, trying SLICES instead!\n");
121 return;
124 // set up mpi as a cropped-down image of dmpi:
125 if (mpi->flags & MP_IMGFLAG_PLANAR) {
126 mpi->planes[0] = vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
127 mpi->planes[1] = vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1];
128 mpi->planes[2] = vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2];
129 mpi->stride[1] = vf->dmpi->stride[1];
130 mpi->stride[2] = vf->dmpi->stride[2];
131 } else {
132 mpi->planes[0] = vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
134 mpi->stride[0] = vf->dmpi->stride[0];
135 mpi->width = vf->dmpi->width;
136 mpi->flags |= MP_IMGFLAG_DIRECT;
137 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
138 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
141 static void blank(mp_image_t *mpi, int y1, int y2)
143 int color[3] = {16, 128, 128}; // black (YUV)
144 int y;
145 unsigned char *dst;
146 int chroma_rows = (y2 - y1) >> mpi->chroma_y_shift;
148 dst = mpi->planes[0] + y1 * mpi->stride[0];
149 for (y = 0; y < y2 - y1; ++y) {
150 memset(dst, color[0], mpi->w);
151 dst += mpi->stride[0];
153 dst = mpi->planes[1] + (y1 >> mpi->chroma_y_shift) * mpi->stride[1];
154 for (y = 0; y < chroma_rows; ++y) {
155 memset(dst, color[1], mpi->chroma_width);
156 dst += mpi->stride[1];
158 dst = mpi->planes[2] + (y1 >> mpi->chroma_y_shift) * mpi->stride[2];
159 for (y = 0; y < chroma_rows; ++y) {
160 memset(dst, color[2], mpi->chroma_width);
161 dst += mpi->stride[2];
165 static int prepare_image(struct vf_instance *vf, mp_image_t *mpi)
167 if (mpi->flags & MP_IMGFLAG_DIRECT
168 || mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) {
169 vf->dmpi = mpi->priv;
170 if (!vf->dmpi) {
171 mp_tmsg(MSGT_ASS, MSGL_WARN, "Why do we get NULL??\n");
172 return 0;
174 mpi->priv = NULL;
175 // we've used DR, so we're ready...
176 if (ass_top_margin)
177 blank(vf->dmpi, 0, ass_top_margin);
178 if (ass_bottom_margin)
179 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
180 if (!(mpi->flags & MP_IMGFLAG_PLANAR))
181 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
182 return 0;
185 // hope we'll get DR buffer:
186 vf->dmpi = vf_get_image(vf->next, vf->priv->outfmt, MP_IMGTYPE_TEMP,
187 MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_READABLE,
188 vf->priv->outw, vf->priv->outh);
190 // copy mpi->dmpi...
191 if (mpi->flags & MP_IMGFLAG_PLANAR) {
192 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
193 mpi->planes[0],
194 mpi->w,
195 mpi->h,
196 vf->dmpi->stride[0],
197 mpi->stride[0]);
198 memcpy_pic(vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1],
199 mpi->planes[1],
200 mpi->w >> mpi->chroma_x_shift,
201 mpi->h >> mpi->chroma_y_shift,
202 vf->dmpi->stride[1],
203 mpi->stride[1]);
204 memcpy_pic(vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2],
205 mpi->planes[2],
206 mpi->w >> mpi->chroma_x_shift,
207 mpi->h >> mpi->chroma_y_shift,
208 vf->dmpi->stride[2],
209 mpi->stride[2]);
210 } else {
211 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
212 mpi->planes[0],
213 mpi->w * (vf->dmpi->bpp / 8),
214 mpi->h,
215 vf->dmpi->stride[0],
216 mpi->stride[0]);
217 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
219 if (ass_top_margin)
220 blank(vf->dmpi, 0, ass_top_margin);
221 if (ass_bottom_margin)
222 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
223 return 0;
226 static void update_limits(struct vf_instance *vf, int starty, int endy,
227 int startx, int endx)
229 starty >>= 1;
230 endy = (endy + 1) >> 1;
231 startx >>= 1;
232 endx = (endx + 1) >> 1;
233 for (int i = starty; i < endy; i++) {
234 struct line_limits *ll = vf->priv->line_limits + i;
235 if (startx < ll->start)
236 ll->start = startx;
237 if (endx > ll->end)
238 ll->end = endx;
243 * \brief Copy specified rows from render_context.dmpi to render_context.planes, upsampling to 4:4:4
245 static void copy_from_image(struct vf_instance *vf)
247 int pl;
249 for (pl = 1; pl < 3; ++pl) {
250 int dst_stride = vf->priv->outw;
251 int src_stride = vf->dmpi->stride[pl];
253 unsigned char *src = vf->dmpi->planes[pl];
254 unsigned char *dst = vf->priv->planes[pl];
255 for (int i = 0; i < (vf->priv->outh + 1) / 2; i++) {
256 struct line_limits *ll = vf->priv->line_limits + i;
257 unsigned char *dst_next = dst + dst_stride;
258 for (int j = ll->start; j < ll->end; j++) {
259 unsigned char val = src[j];
260 dst[j << 1] = val;
261 dst[(j << 1) + 1] = val;
262 dst_next[j << 1] = val;
263 dst_next[(j << 1) + 1] = val;
265 src += src_stride;
266 dst = dst_next + dst_stride;
272 * \brief Copy all previously copied rows back to render_context.dmpi
274 static void copy_to_image(struct vf_instance *vf)
276 int pl;
277 int i, j;
278 for (pl = 1; pl < 3; ++pl) {
279 int dst_stride = vf->dmpi->stride[pl];
280 int src_stride = vf->priv->outw;
282 unsigned char *dst = vf->dmpi->planes[pl];
283 unsigned char *src = vf->priv->planes[pl];
284 unsigned char *src_next = vf->priv->planes[pl] + src_stride;
285 for (i = 0; i < vf->dmpi->chroma_height; ++i) {
286 for (j = vf->priv->line_limits[i].start; j < vf->priv->line_limits[i].end; j++) {
287 unsigned val = 0;
288 val += src[j << 1];
289 val += src[(j << 1) + 1];
290 val += src_next[j << 1];
291 val += src_next[(j << 1) + 1];
292 dst[j] = val >> 2;
294 dst += dst_stride;
295 src = src_next + src_stride;
296 src_next = src + src_stride;
301 static void my_draw_bitmap(struct vf_instance *vf, unsigned char *bitmap,
302 int bitmap_w, int bitmap_h, int stride,
303 int dst_x, int dst_y, unsigned color)
305 unsigned char y = rgba2y(color);
306 unsigned char u = rgba2u(color);
307 unsigned char v = rgba2v(color);
308 unsigned char opacity = 255 - _a(color);
309 unsigned char *src, *dsty, *dstu, *dstv;
310 int i, j;
311 mp_image_t *dmpi = vf->dmpi;
313 src = bitmap;
314 dsty = dmpi->planes[0] + dst_x + dst_y * dmpi->stride[0];
315 dstu = vf->priv->planes[1] + dst_x + dst_y * vf->priv->outw;
316 dstv = vf->priv->planes[2] + dst_x + dst_y * vf->priv->outw;
317 for (i = 0; i < bitmap_h; ++i) {
318 for (j = 0; j < bitmap_w; ++j) {
319 unsigned k = (src[j] * opacity + 255) >> 8;
320 dsty[j] = (k * y + (255 - k) * dsty[j] + 255) >> 8;
321 dstu[j] = (k * u + (255 - k) * dstu[j] + 255) >> 8;
322 dstv[j] = (k * v + (255 - k) * dstv[j] + 255) >> 8;
324 src += stride;
325 dsty += dmpi->stride[0];
326 dstu += vf->priv->outw;
327 dstv += vf->priv->outw;
331 static int render_frame(struct vf_instance *vf, mp_image_t *mpi,
332 const ASS_Image *img)
334 if (img) {
335 for (int i = 0; i < (vf->priv->outh + 1) / 2; i++)
336 vf->priv->line_limits[i] = (struct line_limits){65535, 0};
337 for (const ASS_Image *im = img; im; im = im->next)
338 update_limits(vf, im->dst_y, im->dst_y + im->h,
339 im->dst_x, im->dst_x + im->w);
340 copy_from_image(vf);
341 while (img) {
342 my_draw_bitmap(vf, img->bitmap, img->w, img->h, img->stride,
343 img->dst_x, img->dst_y, img->color);
344 img = img->next;
346 copy_to_image(vf);
348 return 0;
351 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
353 ASS_Image *images = 0;
354 if (sub_visibility && vf->priv->ass_priv && vf->priv->osd->ass_track
355 && (pts != MP_NOPTS_VALUE))
356 images = ass_mp_render_frame(vf->priv->ass_priv,
357 vf->priv->osd->ass_track,
358 (pts + sub_delay) * 1000 + .5, NULL);
360 prepare_image(vf, mpi);
361 if (images)
362 render_frame(vf, mpi, images);
364 return vf_next_put_image(vf, vf->dmpi, pts);
367 static int query_format(struct vf_instance *vf, unsigned int fmt)
369 switch (fmt) {
370 case IMGFMT_YV12:
371 case IMGFMT_I420:
372 case IMGFMT_IYUV:
373 return vf_next_query_format(vf, vf->priv->outfmt);
375 return 0;
378 static int control(vf_instance_t *vf, int request, void *data)
380 switch (request) {
381 case VFCTRL_SET_OSD_OBJ:
382 vf->priv->osd = data;
383 break;
384 case VFCTRL_INIT_EOSD:
385 vf->priv->ass_priv = ass_renderer_init((ASS_Library *)data);
386 if (!vf->priv->ass_priv)
387 return CONTROL_FALSE;
388 ass_configure_fonts(vf->priv->ass_priv);
389 return CONTROL_TRUE;
390 case VFCTRL_DRAW_EOSD:
391 if (vf->priv->ass_priv)
392 return CONTROL_TRUE;
393 break;
395 return vf_next_control(vf, request, data);
398 static void uninit(struct vf_instance *vf)
400 if (vf->priv->ass_priv)
401 ass_renderer_done(vf->priv->ass_priv);
402 free(vf->priv->planes[1]);
403 free(vf->priv->planes[2]);
404 free(vf->priv->line_limits);
405 free(vf->priv);
408 static const unsigned int fmt_list[] = {
409 IMGFMT_YV12,
410 IMGFMT_I420,
411 IMGFMT_IYUV,
415 static int vf_open(vf_instance_t *vf, char *args)
417 int flags;
418 vf->priv->outfmt = vf_match_csp(&vf->next, fmt_list, IMGFMT_YV12);
419 if (vf->priv->outfmt)
420 flags = vf_next_query_format(vf, vf->priv->outfmt);
421 if (!vf->priv->outfmt) {
422 uninit(vf);
423 return 0;
424 } else if (vf->priv->auto_insert && flags & VFCAP_EOSD) {
425 uninit(vf);
426 return -1;
429 if (vf->priv->auto_insert)
430 mp_msg(MSGT_ASS, MSGL_INFO, "[ass] auto-open\n");
432 vf->config = config;
433 vf->query_format = query_format;
434 vf->uninit = uninit;
435 vf->control = control;
436 vf->get_image = get_image;
437 vf->put_image = put_image;
438 vf->default_caps = VFCAP_EOSD;
439 return 1;
442 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s, f)
443 static const m_option_t vf_opts_fields[] = {
444 {"auto", ST_OFF(auto_insert), CONF_TYPE_FLAG, 0, 0, 1, NULL},
445 {NULL, NULL, 0, 0, 0, 0, NULL}
448 static const m_struct_t vf_opts = {
449 "ass",
450 sizeof(struct vf_priv_s),
451 &vf_priv_dflt,
452 vf_opts_fields
455 const vf_info_t vf_info_ass = {
456 "Render ASS/SSA subtitles",
457 "ass",
458 "Evgeniy Stepanov",
460 vf_open,
461 &vf_opts