Makefile: use $(RM) builtin
[mplayer.git] / libmpcodecs / vf_ass.c
blob4047316a506f1795ac503f1f3fa58337c46a7809
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 "sub/sub.h"
38 #include "libvo/fastmemcpy.h"
40 #include "m_option.h"
41 #include "m_struct.h"
43 #include "sub/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 *renderer_realaspect;
65 ASS_Renderer *renderer_vsfilter;
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 float sub_delay;
76 static int config(struct vf_instance *vf,
77 int width, int height, int d_width, int d_height,
78 unsigned int flags, unsigned int outfmt)
80 struct MPOpts *opts = vf->opts;
81 if (outfmt == IMGFMT_IF09)
82 return 0;
84 vf->priv->outh = height + ass_top_margin + ass_bottom_margin;
85 vf->priv->outw = width;
87 if (!opts->screen_size_x && !opts->screen_size_y) {
88 d_width = d_width * vf->priv->outw / width;
89 d_height = d_height * vf->priv->outh / height;
92 vf->priv->planes[1] = malloc(vf->priv->outw * vf->priv->outh);
93 vf->priv->planes[2] = malloc(vf->priv->outw * vf->priv->outh);
94 vf->priv->line_limits = malloc((vf->priv->outh + 1) / 2 * sizeof(*vf->priv->line_limits));
96 if (vf->priv->renderer_realaspect) {
97 mp_ass_configure(vf->priv->renderer_realaspect,
98 vf->priv->outw, vf->priv->outh, 0);
99 mp_ass_configure(vf->priv->renderer_vsfilter,
100 vf->priv->outw, vf->priv->outh, 0);
101 ass_set_aspect_ratio(vf->priv->renderer_realaspect,
102 (double)width / height * d_height / d_width, 1);
103 ass_set_aspect_ratio(vf->priv->renderer_vsfilter, 1, 1);
106 return vf_next_config(vf, vf->priv->outw, vf->priv->outh, d_width,
107 d_height, flags, outfmt);
110 static void get_image(struct vf_instance *vf, mp_image_t *mpi)
112 if (mpi->type == MP_IMGTYPE_IPB)
113 return;
114 if (mpi->flags & MP_IMGFLAG_PRESERVE)
115 return;
116 if (mpi->imgfmt != vf->priv->outfmt)
117 return; // colorspace differ
119 // width never changes, always try full DR
120 mpi->priv = vf->dmpi = vf_get_image(vf->next, mpi->imgfmt, mpi->type,
121 mpi->flags | MP_IMGFLAG_READABLE,
122 vf->priv->outw, vf->priv->outh);
124 if ((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
125 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)) {
126 mp_tmsg(MSGT_ASS, MSGL_INFO, "Full DR not possible, trying SLICES instead!\n");
127 return;
130 // set up mpi as a cropped-down image of dmpi:
131 if (mpi->flags & MP_IMGFLAG_PLANAR) {
132 mpi->planes[0] = vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
133 mpi->planes[1] = vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1];
134 mpi->planes[2] = vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2];
135 mpi->stride[1] = vf->dmpi->stride[1];
136 mpi->stride[2] = vf->dmpi->stride[2];
137 } else {
138 mpi->planes[0] = vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
140 mpi->stride[0] = vf->dmpi->stride[0];
141 mpi->width = vf->dmpi->width;
142 mpi->flags |= MP_IMGFLAG_DIRECT;
143 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
144 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
147 static void blank(mp_image_t *mpi, int y1, int y2)
149 int color[3] = {16, 128, 128}; // black (YUV)
150 int y;
151 unsigned char *dst;
152 int chroma_rows = (y2 - y1) >> mpi->chroma_y_shift;
154 dst = mpi->planes[0] + y1 * mpi->stride[0];
155 for (y = 0; y < y2 - y1; ++y) {
156 memset(dst, color[0], mpi->w);
157 dst += mpi->stride[0];
159 dst = mpi->planes[1] + (y1 >> mpi->chroma_y_shift) * mpi->stride[1];
160 for (y = 0; y < chroma_rows; ++y) {
161 memset(dst, color[1], mpi->chroma_width);
162 dst += mpi->stride[1];
164 dst = mpi->planes[2] + (y1 >> mpi->chroma_y_shift) * mpi->stride[2];
165 for (y = 0; y < chroma_rows; ++y) {
166 memset(dst, color[2], mpi->chroma_width);
167 dst += mpi->stride[2];
171 static int prepare_image(struct vf_instance *vf, mp_image_t *mpi)
173 if (mpi->flags & MP_IMGFLAG_DIRECT
174 || mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) {
175 vf->dmpi = mpi->priv;
176 if (!vf->dmpi) {
177 mp_tmsg(MSGT_ASS, MSGL_WARN, "Why do we get NULL??\n");
178 return 0;
180 mpi->priv = NULL;
181 // we've used DR, so we're ready...
182 if (ass_top_margin)
183 blank(vf->dmpi, 0, ass_top_margin);
184 if (ass_bottom_margin)
185 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
186 if (!(mpi->flags & MP_IMGFLAG_PLANAR))
187 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
188 return 0;
191 // hope we'll get DR buffer:
192 vf->dmpi = vf_get_image(vf->next, vf->priv->outfmt, MP_IMGTYPE_TEMP,
193 MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_READABLE,
194 vf->priv->outw, vf->priv->outh);
196 // copy mpi->dmpi...
197 if (mpi->flags & MP_IMGFLAG_PLANAR) {
198 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
199 mpi->planes[0],
200 mpi->w,
201 mpi->h,
202 vf->dmpi->stride[0],
203 mpi->stride[0]);
204 memcpy_pic(vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1],
205 mpi->planes[1],
206 mpi->w >> mpi->chroma_x_shift,
207 mpi->h >> mpi->chroma_y_shift,
208 vf->dmpi->stride[1],
209 mpi->stride[1]);
210 memcpy_pic(vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2],
211 mpi->planes[2],
212 mpi->w >> mpi->chroma_x_shift,
213 mpi->h >> mpi->chroma_y_shift,
214 vf->dmpi->stride[2],
215 mpi->stride[2]);
216 } else {
217 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
218 mpi->planes[0],
219 mpi->w * (vf->dmpi->bpp / 8),
220 mpi->h,
221 vf->dmpi->stride[0],
222 mpi->stride[0]);
223 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
225 if (ass_top_margin)
226 blank(vf->dmpi, 0, ass_top_margin);
227 if (ass_bottom_margin)
228 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
229 return 0;
232 static void update_limits(struct vf_instance *vf, int starty, int endy,
233 int startx, int endx)
235 starty >>= 1;
236 endy = (endy + 1) >> 1;
237 startx >>= 1;
238 endx = (endx + 1) >> 1;
239 for (int i = starty; i < endy; i++) {
240 struct line_limits *ll = vf->priv->line_limits + i;
241 if (startx < ll->start)
242 ll->start = startx;
243 if (endx > ll->end)
244 ll->end = endx;
249 * \brief Copy specified rows from render_context.dmpi to render_context.planes, upsampling to 4:4:4
251 static void copy_from_image(struct vf_instance *vf)
253 int pl;
255 for (pl = 1; pl < 3; ++pl) {
256 int dst_stride = vf->priv->outw;
257 int src_stride = vf->dmpi->stride[pl];
259 unsigned char *src = vf->dmpi->planes[pl];
260 unsigned char *dst = vf->priv->planes[pl];
261 for (int i = 0; i < (vf->priv->outh + 1) / 2; i++) {
262 struct line_limits *ll = vf->priv->line_limits + i;
263 unsigned char *dst_next = dst + dst_stride;
264 for (int j = ll->start; j < ll->end; j++) {
265 unsigned char val = src[j];
266 dst[j << 1] = val;
267 dst[(j << 1) + 1] = val;
268 dst_next[j << 1] = val;
269 dst_next[(j << 1) + 1] = val;
271 src += src_stride;
272 dst = dst_next + dst_stride;
278 * \brief Copy all previously copied rows back to render_context.dmpi
280 static void copy_to_image(struct vf_instance *vf)
282 int pl;
283 int i, j;
284 for (pl = 1; pl < 3; ++pl) {
285 int dst_stride = vf->dmpi->stride[pl];
286 int src_stride = vf->priv->outw;
288 unsigned char *dst = vf->dmpi->planes[pl];
289 unsigned char *src = vf->priv->planes[pl];
290 unsigned char *src_next = vf->priv->planes[pl] + src_stride;
291 for (i = 0; i < vf->dmpi->chroma_height; ++i) {
292 for (j = vf->priv->line_limits[i].start; j < vf->priv->line_limits[i].end; j++) {
293 unsigned val = 0;
294 val += src[j << 1];
295 val += src[(j << 1) + 1];
296 val += src_next[j << 1];
297 val += src_next[(j << 1) + 1];
298 dst[j] = val >> 2;
300 dst += dst_stride;
301 src = src_next + src_stride;
302 src_next = src + src_stride;
307 static void my_draw_bitmap(struct vf_instance *vf, unsigned char *bitmap,
308 int bitmap_w, int bitmap_h, int stride,
309 int dst_x, int dst_y, unsigned color)
311 unsigned char y = rgba2y(color);
312 unsigned char u = rgba2u(color);
313 unsigned char v = rgba2v(color);
314 unsigned char opacity = 255 - _a(color);
315 unsigned char *src, *dsty, *dstu, *dstv;
316 int i, j;
317 mp_image_t *dmpi = vf->dmpi;
319 src = bitmap;
320 dsty = dmpi->planes[0] + dst_x + dst_y * dmpi->stride[0];
321 dstu = vf->priv->planes[1] + dst_x + dst_y * vf->priv->outw;
322 dstv = vf->priv->planes[2] + dst_x + dst_y * vf->priv->outw;
323 for (i = 0; i < bitmap_h; ++i) {
324 for (j = 0; j < bitmap_w; ++j) {
325 unsigned k = (src[j] * opacity + 255) >> 8;
326 dsty[j] = (k * y + (255 - k) * dsty[j] + 255) >> 8;
327 dstu[j] = (k * u + (255 - k) * dstu[j] + 255) >> 8;
328 dstv[j] = (k * v + (255 - k) * dstv[j] + 255) >> 8;
330 src += stride;
331 dsty += dmpi->stride[0];
332 dstu += vf->priv->outw;
333 dstv += vf->priv->outw;
337 static int render_frame(struct vf_instance *vf, mp_image_t *mpi,
338 const ASS_Image *img)
340 if (img) {
341 for (int i = 0; i < (vf->priv->outh + 1) / 2; i++)
342 vf->priv->line_limits[i] = (struct line_limits){65535, 0};
343 for (const ASS_Image *im = img; im; im = im->next)
344 update_limits(vf, im->dst_y, im->dst_y + im->h,
345 im->dst_x, im->dst_x + im->w);
346 copy_from_image(vf);
347 while (img) {
348 my_draw_bitmap(vf, img->bitmap, img->w, img->h, img->stride,
349 img->dst_x, img->dst_y, img->color);
350 img = img->next;
352 copy_to_image(vf);
354 return 0;
357 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
359 ASS_Image *images = 0;
360 ASS_Renderer *renderer = vf->priv->osd->vsfilter_aspect ?
361 vf->priv->renderer_vsfilter : vf->priv->renderer_realaspect;
362 if (sub_visibility && renderer && vf->priv->osd->ass_track
363 && (pts != MP_NOPTS_VALUE))
364 images = mp_ass_render_frame(renderer,
365 vf->priv->osd->ass_track,
366 (pts + sub_delay) * 1000 + .5, NULL);
368 prepare_image(vf, mpi);
369 if (images)
370 render_frame(vf, mpi, images);
372 return vf_next_put_image(vf, vf->dmpi, pts);
375 static int query_format(struct vf_instance *vf, unsigned int fmt)
377 switch (fmt) {
378 case IMGFMT_YV12:
379 case IMGFMT_I420:
380 case IMGFMT_IYUV:
381 return vf_next_query_format(vf, vf->priv->outfmt);
383 return 0;
386 static int control(vf_instance_t *vf, int request, void *data)
388 switch (request) {
389 case VFCTRL_SET_OSD_OBJ:
390 vf->priv->osd = data;
391 break;
392 case VFCTRL_INIT_EOSD:
393 vf->priv->renderer_realaspect = ass_renderer_init((ASS_Library *)data);
394 if (!vf->priv->renderer_realaspect)
395 return CONTROL_FALSE;
396 vf->priv->renderer_vsfilter = ass_renderer_init((ASS_Library *)data);
397 if (!vf->priv->renderer_vsfilter) {
398 ass_renderer_done(vf->priv->renderer_realaspect);
399 return CONTROL_FALSE;
401 mp_ass_configure_fonts(vf->priv->renderer_realaspect);
402 mp_ass_configure_fonts(vf->priv->renderer_vsfilter);
403 return CONTROL_TRUE;
404 case VFCTRL_DRAW_EOSD:
405 if (vf->priv->renderer_realaspect)
406 return CONTROL_TRUE;
407 break;
409 return vf_next_control(vf, request, data);
412 static void uninit(struct vf_instance *vf)
414 if (vf->priv->renderer_realaspect) {
415 ass_renderer_done(vf->priv->renderer_realaspect);
416 ass_renderer_done(vf->priv->renderer_vsfilter);
418 free(vf->priv->planes[1]);
419 free(vf->priv->planes[2]);
420 free(vf->priv->line_limits);
421 free(vf->priv);
424 static const unsigned int fmt_list[] = {
425 IMGFMT_YV12,
426 IMGFMT_I420,
427 IMGFMT_IYUV,
431 static int vf_open(vf_instance_t *vf, char *args)
433 int flags;
434 vf->priv->outfmt = vf_match_csp(&vf->next, fmt_list, IMGFMT_YV12);
435 if (vf->priv->outfmt)
436 flags = vf_next_query_format(vf, vf->priv->outfmt);
437 if (!vf->priv->outfmt) {
438 uninit(vf);
439 return 0;
440 } else if (vf->priv->auto_insert && flags & VFCAP_EOSD) {
441 uninit(vf);
442 return -1;
445 if (vf->priv->auto_insert)
446 mp_msg(MSGT_ASS, MSGL_INFO, "[ass] auto-open\n");
448 vf->config = config;
449 vf->query_format = query_format;
450 vf->uninit = uninit;
451 vf->control = control;
452 vf->get_image = get_image;
453 vf->put_image = put_image;
454 vf->default_caps = VFCAP_EOSD;
455 return 1;
458 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s, f)
459 static const m_option_t vf_opts_fields[] = {
460 {"auto", ST_OFF(auto_insert), CONF_TYPE_FLAG, 0, 0, 1, NULL},
461 {NULL, NULL, 0, 0, 0, 0, NULL}
464 static const m_struct_t vf_opts = {
465 "ass",
466 sizeof(struct vf_priv_s),
467 &vf_priv_dflt,
468 vf_opts_fields
471 const vf_info_t vf_info_ass = {
472 "Render ASS/SSA subtitles",
473 "ass",
474 "Evgeniy Stepanov",
476 vf_open,
477 &vf_opts