1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
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.
36 #include "img_format.h"
40 #include "libvo/fastmemcpy.h"
45 #include "libass/ass.h"
46 #include "libass/ass_mp.h"
48 #define _r(c) ((c)>>24)
49 #define _g(c) (((c)>>16)&0xFF)
50 #define _b(c) (((c)>>8)&0xFF)
51 #define _a(c) ((c)&0xFF)
52 #define rgba2y(c) ( (( 263*_r(c) + 516*_g(c) + 100*_b(c)) >> 10) + 16 )
53 #define rgba2u(c) ( ((-152*_r(c) - 298*_g(c) + 450*_b(c)) >> 10) + 128 )
54 #define rgba2v(c) ( (( 450*_r(c) - 376*_g(c) - 73*_b(c)) >> 10) + 128 )
57 static const struct vf_priv_s
{
62 // 1 = auto-added filter: insert only if chain does not support EOSD already
66 ass_renderer_t
* ass_priv
;
68 unsigned char* planes
[3];
75 extern ass_track_t
* ass_track
;
76 extern float sub_delay
;
77 extern int sub_visibility
;
79 static int config(struct vf_instance
* vf
,
80 int width
, int height
, int d_width
, int d_height
,
81 unsigned int flags
, unsigned int outfmt
)
83 struct MPOpts
*opts
= vf
->opts
;
84 if (outfmt
== IMGFMT_IF09
) return 0;
86 vf
->priv
->outh
= height
+ ass_top_margin
+ ass_bottom_margin
;
87 vf
->priv
->outw
= width
;
89 if (!opts
->screen_size_x
&& !opts
->screen_size_y
) {
90 d_width
= d_width
* vf
->priv
->outw
/ width
;
91 d_height
= d_height
* vf
->priv
->outh
/ height
;
94 vf
->priv
->planes
[1] = malloc(vf
->priv
->outw
* vf
->priv
->outh
);
95 vf
->priv
->planes
[2] = malloc(vf
->priv
->outw
* vf
->priv
->outh
);
96 vf
->priv
->line_limits
= malloc((vf
->priv
->outh
+ 1) / 2 * sizeof(*vf
->priv
->line_limits
));
98 if (vf
->priv
->ass_priv
) {
99 ass_configure(vf
->priv
->ass_priv
, vf
->priv
->outw
, vf
->priv
->outh
, 0);
100 ass_set_aspect_ratio(vf
->priv
->ass_priv
, ((double)d_width
) / d_height
);
103 return vf_next_config(vf
, vf
->priv
->outw
, vf
->priv
->outh
, d_width
, d_height
, flags
, outfmt
);
106 static void get_image(struct vf_instance
* vf
, mp_image_t
*mpi
)
108 if(mpi
->type
== MP_IMGTYPE_IPB
) return;
109 if(mpi
->flags
& MP_IMGFLAG_PRESERVE
) return;
110 if(mpi
->imgfmt
!= vf
->priv
->outfmt
) return; // colorspace differ
112 // width never changes, always try full DR
113 mpi
->priv
= vf
->dmpi
= vf_get_image(vf
->next
, mpi
->imgfmt
,
114 mpi
->type
, mpi
->flags
| MP_IMGFLAG_READABLE
,
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");
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];
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)
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
|| mpi
->flags
&MP_IMGFLAG_DRAW_CALLBACK
){
168 vf
->dmpi
= mpi
->priv
;
169 if (!vf
->dmpi
) { mp_tmsg(MSGT_ASS
, MSGL_WARN
, "Why do we get NULL??\n"); return 0; }
171 // we've used DR, so we're ready...
173 blank(vf
->dmpi
, 0, ass_top_margin
);
174 if (ass_bottom_margin
)
175 blank(vf
->dmpi
, vf
->priv
->outh
- ass_bottom_margin
, vf
->priv
->outh
);
176 if(!(mpi
->flags
&MP_IMGFLAG_PLANAR
))
177 vf
->dmpi
->planes
[1] = mpi
->planes
[1]; // passthrough rgb8 palette
181 // hope we'll get DR buffer:
182 vf
->dmpi
= vf_get_image(vf
->next
, vf
->priv
->outfmt
,
183 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
| MP_IMGFLAG_READABLE
,
184 vf
->priv
->outw
, vf
->priv
->outh
);
187 if(mpi
->flags
&MP_IMGFLAG_PLANAR
){
188 memcpy_pic(vf
->dmpi
->planes
[0] + ass_top_margin
* vf
->dmpi
->stride
[0],
189 mpi
->planes
[0], mpi
->w
, mpi
->h
,
190 vf
->dmpi
->stride
[0], mpi
->stride
[0]);
191 memcpy_pic(vf
->dmpi
->planes
[1] + (ass_top_margin
>> mpi
->chroma_y_shift
) * vf
->dmpi
->stride
[1],
192 mpi
->planes
[1], mpi
->w
>> mpi
->chroma_x_shift
, mpi
->h
>> mpi
->chroma_y_shift
,
193 vf
->dmpi
->stride
[1], mpi
->stride
[1]);
194 memcpy_pic(vf
->dmpi
->planes
[2] + (ass_top_margin
>> mpi
->chroma_y_shift
) * vf
->dmpi
->stride
[2],
195 mpi
->planes
[2], mpi
->w
>> mpi
->chroma_x_shift
, mpi
->h
>> mpi
->chroma_y_shift
,
196 vf
->dmpi
->stride
[2], mpi
->stride
[2]);
198 memcpy_pic(vf
->dmpi
->planes
[0] + ass_top_margin
* vf
->dmpi
->stride
[0],
199 mpi
->planes
[0], mpi
->w
*(vf
->dmpi
->bpp
/8), mpi
->h
,
200 vf
->dmpi
->stride
[0], mpi
->stride
[0]);
201 vf
->dmpi
->planes
[1] = mpi
->planes
[1]; // passthrough rgb8 palette
204 blank(vf
->dmpi
, 0, ass_top_margin
);
205 if (ass_bottom_margin
)
206 blank(vf
->dmpi
, vf
->priv
->outh
- ass_bottom_margin
, vf
->priv
->outh
);
210 static void update_limits(struct vf_instance
*vf
, int starty
, int endy
,
211 int startx
, int endx
)
214 endy
= (endy
+ 1) >> 1;
216 endx
= (endx
+ 1) >> 1;
217 for (int i
= starty
; i
< endy
; i
++) {
218 struct line_limits
*ll
= vf
->priv
->line_limits
+ i
;
219 if (startx
< ll
->start
)
227 * \brief Copy specified rows from render_context.dmpi to render_context.planes, upsampling to 4:4:4
229 static void copy_from_image(struct vf_instance
* vf
)
233 for (pl
= 1; pl
< 3; ++pl
) {
234 int dst_stride
= vf
->priv
->outw
;
235 int src_stride
= vf
->dmpi
->stride
[pl
];
237 unsigned char* src
= vf
->dmpi
->planes
[pl
];
238 unsigned char* dst
= vf
->priv
->planes
[pl
];
239 for (int i
= 0; i
< (vf
->priv
->outh
+ 1) / 2; i
++) {
240 struct line_limits
*ll
= vf
->priv
->line_limits
+ i
;
241 unsigned char* dst_next
= dst
+ dst_stride
;
242 for (int j
= ll
->start
; j
< ll
->end
; j
++) {
243 unsigned char val
= src
[j
];
245 dst
[(j
<< 1) + 1] = val
;
246 dst_next
[j
<< 1] = val
;
247 dst_next
[(j
<< 1) + 1] = val
;
250 dst
= dst_next
+ dst_stride
;
256 * \brief Copy all previously copied rows back to render_context.dmpi
258 static void copy_to_image(struct vf_instance
* vf
)
262 for (pl
= 1; pl
< 3; ++pl
) {
263 int dst_stride
= vf
->dmpi
->stride
[pl
];
264 int src_stride
= vf
->priv
->outw
;
266 unsigned char* dst
= vf
->dmpi
->planes
[pl
];
267 unsigned char* src
= vf
->priv
->planes
[pl
];
268 unsigned char* src_next
= vf
->priv
->planes
[pl
] + src_stride
;
269 for (i
= 0; i
< vf
->dmpi
->chroma_height
; ++i
) {
270 for (j
= vf
->priv
->line_limits
[i
].start
; j
< vf
->priv
->line_limits
[i
].end
; j
++) {
273 val
+= src
[(j
<< 1) + 1];
274 val
+= src_next
[j
<< 1];
275 val
+= src_next
[(j
<< 1) + 1];
279 src
= src_next
+ src_stride
;
280 src_next
= src
+ src_stride
;
285 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
)
287 unsigned char y
= rgba2y(color
);
288 unsigned char u
= rgba2u(color
);
289 unsigned char v
= rgba2v(color
);
290 unsigned char opacity
= 255 - _a(color
);
291 unsigned char *src
, *dsty
, *dstu
, *dstv
;
293 mp_image_t
* dmpi
= vf
->dmpi
;
296 dsty
= dmpi
->planes
[0] + dst_x
+ dst_y
* dmpi
->stride
[0];
297 dstu
= vf
->priv
->planes
[1] + dst_x
+ dst_y
* vf
->priv
->outw
;
298 dstv
= vf
->priv
->planes
[2] + dst_x
+ dst_y
* vf
->priv
->outw
;
299 for (i
= 0; i
< bitmap_h
; ++i
) {
300 for (j
= 0; j
< bitmap_w
; ++j
) {
301 unsigned k
= (src
[j
] * opacity
+ 255) >> 8;
302 dsty
[j
] = (k
*y
+ (255-k
)*dsty
[j
] + 255) >> 8;
303 dstu
[j
] = (k
*u
+ (255-k
)*dstu
[j
] + 255) >> 8;
304 dstv
[j
] = (k
*v
+ (255-k
)*dstv
[j
] + 255) >> 8;
307 dsty
+= dmpi
->stride
[0];
308 dstu
+= vf
->priv
->outw
;
309 dstv
+= vf
->priv
->outw
;
313 static int render_frame(struct vf_instance
* vf
, mp_image_t
*mpi
, const ass_image_t
* img
)
316 for (int i
= 0; i
< (vf
->priv
->outh
+ 1) / 2; i
++)
317 vf
->priv
->line_limits
[i
] = (struct line_limits
){65535, 0};
318 for (const ass_image_t
*im
= img
; im
; im
= im
->next
)
319 update_limits(vf
, im
->dst_y
, im
->dst_y
+ im
->h
, im
->dst_x
, im
->dst_x
+ im
->w
);
322 my_draw_bitmap(vf
, img
->bitmap
, img
->w
, img
->h
, img
->stride
,
323 img
->dst_x
, img
->dst_y
, img
->color
);
331 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
)
333 ass_image_t
* images
= 0;
334 if (sub_visibility
&& vf
->priv
->ass_priv
&& ass_track
&& (pts
!= MP_NOPTS_VALUE
))
335 images
= ass_mp_render_frame(vf
->priv
->ass_priv
, ass_track
, (pts
+sub_delay
) * 1000 + .5, NULL
);
337 prepare_image(vf
, mpi
);
338 if (images
) render_frame(vf
, mpi
, images
);
340 return vf_next_put_image(vf
, vf
->dmpi
, pts
);
343 static int query_format(struct vf_instance
* vf
, unsigned int fmt
)
349 return vf_next_query_format(vf
, vf
->priv
->outfmt
);
354 static int control(vf_instance_t
*vf
, int request
, void *data
)
357 case VFCTRL_INIT_EOSD
:
358 vf
->priv
->ass_priv
= ass_renderer_init((ass_library_t
*)data
);
359 if (!vf
->priv
->ass_priv
) return CONTROL_FALSE
;
360 ass_configure_fonts(vf
->priv
->ass_priv
);
362 case VFCTRL_DRAW_EOSD
:
363 if (vf
->priv
->ass_priv
) return CONTROL_TRUE
;
366 return vf_next_control(vf
, request
, data
);
369 static void uninit(struct vf_instance
* vf
)
371 if (vf
->priv
->ass_priv
)
372 ass_renderer_done(vf
->priv
->ass_priv
);
373 free(vf
->priv
->planes
[1]);
374 free(vf
->priv
->planes
[2]);
375 free(vf
->priv
->line_limits
);
379 static const unsigned int fmt_list
[]={
386 static int open(vf_instance_t
*vf
, char* args
)
389 vf
->priv
->outfmt
= vf_match_csp(&vf
->next
,fmt_list
,IMGFMT_YV12
);
390 if (vf
->priv
->outfmt
)
391 flags
= vf_next_query_format(vf
, vf
->priv
->outfmt
);
392 if (!vf
->priv
->outfmt
|| (vf
->priv
->auto_insert
&& flags
&VFCAP_EOSD
))
398 if (vf
->priv
->auto_insert
)
399 mp_msg(MSGT_ASS
, MSGL_INFO
, "[ass] auto-open\n");
402 vf
->query_format
= query_format
;
404 vf
->control
= control
;
405 vf
->get_image
= get_image
;
406 vf
->put_image
= put_image
;
407 vf
->default_caps
=VFCAP_EOSD
;
411 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
412 static const m_option_t vf_opts_fields
[] = {
413 {"auto", ST_OFF(auto_insert
), CONF_TYPE_FLAG
, 0 , 0, 1, NULL
},
414 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
417 static const m_struct_t vf_opts
= {
419 sizeof(struct vf_priv_s
),
424 const vf_info_t vf_info_ass
= {
425 "Render ASS/SSA subtitles",