cocoa_common: add HiDPI/retina support
[mplayer.git] / libmpcodecs / vf_ass.c
blob82351500b361345726608c44d92182edb92ca4af
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 + opts->ass_top_margin + opts->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, opts,
98 vf->priv->outw, vf->priv->outh, 0);
99 mp_ass_configure(vf->priv->renderer_vsfilter, opts,
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 int tmargin = vf->opts->ass_top_margin;
131 // set up mpi as a cropped-down image of dmpi:
132 if (mpi->flags & MP_IMGFLAG_PLANAR) {
133 mpi->planes[0] = vf->dmpi->planes[0] + tmargin * vf->dmpi->stride[0];
134 mpi->planes[1] = vf->dmpi->planes[1] + (tmargin >> mpi->chroma_y_shift) * vf->dmpi->stride[1];
135 mpi->planes[2] = vf->dmpi->planes[2] + (tmargin >> mpi->chroma_y_shift) * vf->dmpi->stride[2];
136 mpi->stride[1] = vf->dmpi->stride[1];
137 mpi->stride[2] = vf->dmpi->stride[2];
138 } else {
139 mpi->planes[0] = vf->dmpi->planes[0] + tmargin * vf->dmpi->stride[0];
141 mpi->stride[0] = vf->dmpi->stride[0];
142 mpi->width = vf->dmpi->width;
143 mpi->flags |= MP_IMGFLAG_DIRECT;
144 mpi->flags &= ~MP_IMGFLAG_DRAW_CALLBACK;
145 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
148 static void blank(mp_image_t *mpi, int y1, int y2)
150 int color[3] = {16, 128, 128}; // black (YUV)
151 int y;
152 unsigned char *dst;
153 int chroma_rows = (y2 - y1) >> mpi->chroma_y_shift;
155 dst = mpi->planes[0] + y1 * mpi->stride[0];
156 for (y = 0; y < y2 - y1; ++y) {
157 memset(dst, color[0], mpi->w);
158 dst += mpi->stride[0];
160 dst = mpi->planes[1] + (y1 >> mpi->chroma_y_shift) * mpi->stride[1];
161 for (y = 0; y < chroma_rows; ++y) {
162 memset(dst, color[1], mpi->chroma_width);
163 dst += mpi->stride[1];
165 dst = mpi->planes[2] + (y1 >> mpi->chroma_y_shift) * mpi->stride[2];
166 for (y = 0; y < chroma_rows; ++y) {
167 memset(dst, color[2], mpi->chroma_width);
168 dst += mpi->stride[2];
172 static int prepare_image(struct vf_instance *vf, mp_image_t *mpi)
174 struct MPOpts *opts = vf->opts;
175 int tmargin = opts->ass_top_margin;
176 if (mpi->flags & MP_IMGFLAG_DIRECT
177 || mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) {
178 vf->dmpi = mpi->priv;
179 if (!vf->dmpi) {
180 mp_tmsg(MSGT_ASS, MSGL_WARN, "Why do we get NULL??\n");
181 return 0;
183 mpi->priv = NULL;
184 // we've used DR, so we're ready...
185 if (tmargin)
186 blank(vf->dmpi, 0, tmargin);
187 if (opts->ass_bottom_margin)
188 blank(vf->dmpi, vf->priv->outh - opts->ass_bottom_margin,
189 vf->priv->outh);
190 if (!(mpi->flags & MP_IMGFLAG_PLANAR))
191 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
192 return 0;
195 // hope we'll get DR buffer:
196 vf->dmpi = vf_get_image(vf->next, vf->priv->outfmt, MP_IMGTYPE_TEMP,
197 MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_READABLE,
198 vf->priv->outw, vf->priv->outh);
200 // copy mpi->dmpi...
201 if (mpi->flags & MP_IMGFLAG_PLANAR) {
202 memcpy_pic(vf->dmpi->planes[0] + tmargin * vf->dmpi->stride[0],
203 mpi->planes[0],
204 mpi->w,
205 mpi->h,
206 vf->dmpi->stride[0],
207 mpi->stride[0]);
208 memcpy_pic(vf->dmpi->planes[1] + (tmargin >> mpi->chroma_y_shift) * vf->dmpi->stride[1],
209 mpi->planes[1],
210 mpi->w >> mpi->chroma_x_shift,
211 mpi->h >> mpi->chroma_y_shift,
212 vf->dmpi->stride[1],
213 mpi->stride[1]);
214 memcpy_pic(vf->dmpi->planes[2] + (tmargin >> mpi->chroma_y_shift) * vf->dmpi->stride[2],
215 mpi->planes[2],
216 mpi->w >> mpi->chroma_x_shift,
217 mpi->h >> mpi->chroma_y_shift,
218 vf->dmpi->stride[2],
219 mpi->stride[2]);
220 } else {
221 memcpy_pic(vf->dmpi->planes[0] + tmargin * vf->dmpi->stride[0],
222 mpi->planes[0],
223 mpi->w * (vf->dmpi->bpp / 8),
224 mpi->h,
225 vf->dmpi->stride[0],
226 mpi->stride[0]);
227 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
229 if (tmargin)
230 blank(vf->dmpi, 0, tmargin);
231 if (opts->ass_bottom_margin)
232 blank(vf->dmpi, vf->priv->outh - opts->ass_bottom_margin,
233 vf->priv->outh);
234 return 0;
237 static void update_limits(struct vf_instance *vf, int starty, int endy,
238 int startx, int endx)
240 starty >>= 1;
241 endy = (endy + 1) >> 1;
242 startx >>= 1;
243 endx = (endx + 1) >> 1;
244 for (int i = starty; i < endy; i++) {
245 struct line_limits *ll = vf->priv->line_limits + i;
246 if (startx < ll->start)
247 ll->start = startx;
248 if (endx > ll->end)
249 ll->end = endx;
254 * \brief Copy specified rows from render_context.dmpi to render_context.planes, upsampling to 4:4:4
256 static void copy_from_image(struct vf_instance *vf)
258 int pl;
260 for (pl = 1; pl < 3; ++pl) {
261 int dst_stride = vf->priv->outw;
262 int src_stride = vf->dmpi->stride[pl];
264 unsigned char *src = vf->dmpi->planes[pl];
265 unsigned char *dst = vf->priv->planes[pl];
266 for (int i = 0; i < (vf->priv->outh + 1) / 2; i++) {
267 struct line_limits *ll = vf->priv->line_limits + i;
268 unsigned char *dst_next = dst + dst_stride;
269 for (int j = ll->start; j < ll->end; j++) {
270 unsigned char val = src[j];
271 dst[j << 1] = val;
272 dst[(j << 1) + 1] = val;
273 dst_next[j << 1] = val;
274 dst_next[(j << 1) + 1] = val;
276 src += src_stride;
277 dst = dst_next + dst_stride;
283 * \brief Copy all previously copied rows back to render_context.dmpi
285 static void copy_to_image(struct vf_instance *vf)
287 int pl;
288 int i, j;
289 for (pl = 1; pl < 3; ++pl) {
290 int dst_stride = vf->dmpi->stride[pl];
291 int src_stride = vf->priv->outw;
293 unsigned char *dst = vf->dmpi->planes[pl];
294 unsigned char *src = vf->priv->planes[pl];
295 unsigned char *src_next = vf->priv->planes[pl] + src_stride;
296 for (i = 0; i < vf->dmpi->chroma_height; ++i) {
297 for (j = vf->priv->line_limits[i].start; j < vf->priv->line_limits[i].end; j++) {
298 unsigned val = 0;
299 val += src[j << 1];
300 val += src[(j << 1) + 1];
301 val += src_next[j << 1];
302 val += src_next[(j << 1) + 1];
303 dst[j] = val >> 2;
305 dst += dst_stride;
306 src = src_next + src_stride;
307 src_next = src + src_stride;
312 static void my_draw_bitmap(struct vf_instance *vf, unsigned char *bitmap,
313 int bitmap_w, int bitmap_h, int stride,
314 int dst_x, int dst_y, unsigned color)
316 unsigned char y = rgba2y(color);
317 unsigned char u = rgba2u(color);
318 unsigned char v = rgba2v(color);
319 unsigned char opacity = 255 - _a(color);
320 unsigned char *src, *dsty, *dstu, *dstv;
321 int i, j;
322 mp_image_t *dmpi = vf->dmpi;
324 src = bitmap;
325 dsty = dmpi->planes[0] + dst_x + dst_y * dmpi->stride[0];
326 dstu = vf->priv->planes[1] + dst_x + dst_y * vf->priv->outw;
327 dstv = vf->priv->planes[2] + dst_x + dst_y * vf->priv->outw;
328 for (i = 0; i < bitmap_h; ++i) {
329 for (j = 0; j < bitmap_w; ++j) {
330 unsigned k = (src[j] * opacity + 255) >> 8;
331 dsty[j] = (k * y + (255 - k) * dsty[j] + 255) >> 8;
332 dstu[j] = (k * u + (255 - k) * dstu[j] + 255) >> 8;
333 dstv[j] = (k * v + (255 - k) * dstv[j] + 255) >> 8;
335 src += stride;
336 dsty += dmpi->stride[0];
337 dstu += vf->priv->outw;
338 dstv += vf->priv->outw;
342 static int render_frame(struct vf_instance *vf, mp_image_t *mpi,
343 const ASS_Image *img)
345 if (img) {
346 for (int i = 0; i < (vf->priv->outh + 1) / 2; i++)
347 vf->priv->line_limits[i] = (struct line_limits){65535, 0};
348 for (const ASS_Image *im = img; im; im = im->next)
349 update_limits(vf, im->dst_y, im->dst_y + im->h,
350 im->dst_x, im->dst_x + im->w);
351 copy_from_image(vf);
352 while (img) {
353 my_draw_bitmap(vf, img->bitmap, img->w, img->h, img->stride,
354 img->dst_x, img->dst_y, img->color);
355 img = img->next;
357 copy_to_image(vf);
359 return 0;
362 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts)
364 struct osd_state *osd = vf->priv->osd;
365 ASS_Image *images = 0;
366 ASS_Renderer *renderer = osd->vsfilter_aspect
367 && vf->opts->ass_vsfilter_aspect_compat
368 ? vf->priv->renderer_vsfilter : vf->priv->renderer_realaspect;
369 if (sub_visibility && renderer && osd->ass_track
370 && (pts != MP_NOPTS_VALUE)) {
371 if (osd->ass_force_reload) {
372 mp_ass_reload_options(vf->priv->renderer_realaspect, vf->opts);
373 mp_ass_reload_options(vf->priv->renderer_vsfilter, vf->opts);
375 osd->ass_force_reload = false;
376 images = ass_render_frame(renderer, osd->ass_track,
377 (pts - osd->sub_offset + sub_delay) * 1000 + .5, NULL);
380 prepare_image(vf, mpi);
381 if (images)
382 render_frame(vf, mpi, images);
384 return vf_next_put_image(vf, vf->dmpi, pts);
387 static int query_format(struct vf_instance *vf, unsigned int fmt)
389 switch (fmt) {
390 case IMGFMT_YV12:
391 case IMGFMT_I420:
392 case IMGFMT_IYUV:
393 return vf_next_query_format(vf, vf->priv->outfmt);
395 return 0;
398 static int control(vf_instance_t *vf, int request, void *data)
400 switch (request) {
401 case VFCTRL_SET_OSD_OBJ:
402 vf->priv->osd = data;
403 break;
404 case VFCTRL_INIT_EOSD:
405 vf->priv->renderer_realaspect = ass_renderer_init((ASS_Library *)data);
406 if (!vf->priv->renderer_realaspect)
407 return CONTROL_FALSE;
408 vf->priv->renderer_vsfilter = ass_renderer_init((ASS_Library *)data);
409 if (!vf->priv->renderer_vsfilter) {
410 ass_renderer_done(vf->priv->renderer_realaspect);
411 return CONTROL_FALSE;
413 mp_ass_configure_fonts(vf->priv->renderer_realaspect);
414 mp_ass_configure_fonts(vf->priv->renderer_vsfilter);
415 return CONTROL_TRUE;
416 case VFCTRL_DRAW_EOSD:
417 if (vf->priv->renderer_realaspect)
418 return CONTROL_TRUE;
419 break;
421 return vf_next_control(vf, request, data);
424 static void uninit(struct vf_instance *vf)
426 if (vf->priv->renderer_realaspect) {
427 ass_renderer_done(vf->priv->renderer_realaspect);
428 ass_renderer_done(vf->priv->renderer_vsfilter);
430 free(vf->priv->planes[1]);
431 free(vf->priv->planes[2]);
432 free(vf->priv->line_limits);
433 free(vf->priv);
436 static const unsigned int fmt_list[] = {
437 IMGFMT_YV12,
438 IMGFMT_I420,
439 IMGFMT_IYUV,
443 static int vf_open(vf_instance_t *vf, char *args)
445 int flags;
446 vf->priv->outfmt = vf_match_csp(&vf->next, fmt_list, IMGFMT_YV12);
447 if (vf->priv->outfmt)
448 flags = vf_next_query_format(vf, vf->priv->outfmt);
449 if (!vf->priv->outfmt) {
450 uninit(vf);
451 return 0;
452 } else if (vf->priv->auto_insert && flags & VFCAP_EOSD) {
453 uninit(vf);
454 return -1;
457 if (vf->priv->auto_insert)
458 mp_msg(MSGT_ASS, MSGL_INFO, "[ass] auto-open\n");
460 vf->config = config;
461 vf->query_format = query_format;
462 vf->uninit = uninit;
463 vf->control = control;
464 vf->get_image = get_image;
465 vf->put_image = put_image;
466 vf->default_caps = VFCAP_EOSD | VFCAP_EOSD_FILTER;
467 return 1;
470 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s, f)
471 static const m_option_t vf_opts_fields[] = {
472 {"auto", ST_OFF(auto_insert), CONF_TYPE_FLAG, 0, 0, 1, NULL},
473 {NULL, NULL, 0, 0, 0, 0, NULL}
476 static const m_struct_t vf_opts = {
477 "ass",
478 sizeof(struct vf_priv_s),
479 &vf_priv_dflt,
480 vf_opts_fields
483 const vf_info_t vf_info_ass = {
484 "Render ASS/SSA subtitles",
485 "ass",
486 "Evgeniy Stepanov",
488 vf_open,
489 &vf_opts