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.
33 #include "img_format.h"
38 #include "libvo/fastmemcpy.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
{
59 // 1 = auto-added filter: insert only if chain does not support EOSD already
63 struct osd_state
*osd
;
64 ASS_Renderer
*renderer_realaspect
;
65 ASS_Renderer
*renderer_vsfilter
;
67 unsigned char *planes
[3];
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
)
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
)
114 if (mpi
->flags
& MP_IMGFLAG_PRESERVE
)
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");
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];
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)
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
;
177 mp_tmsg(MSGT_ASS
, MSGL_WARN
, "Why do we get NULL??\n");
181 // we've used DR, so we're ready...
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
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
);
197 if (mpi
->flags
& MP_IMGFLAG_PLANAR
) {
198 memcpy_pic(vf
->dmpi
->planes
[0] + ass_top_margin
* vf
->dmpi
->stride
[0],
204 memcpy_pic(vf
->dmpi
->planes
[1] + (ass_top_margin
>> mpi
->chroma_y_shift
) * vf
->dmpi
->stride
[1],
206 mpi
->w
>> mpi
->chroma_x_shift
,
207 mpi
->h
>> mpi
->chroma_y_shift
,
210 memcpy_pic(vf
->dmpi
->planes
[2] + (ass_top_margin
>> mpi
->chroma_y_shift
) * vf
->dmpi
->stride
[2],
212 mpi
->w
>> mpi
->chroma_x_shift
,
213 mpi
->h
>> mpi
->chroma_y_shift
,
217 memcpy_pic(vf
->dmpi
->planes
[0] + ass_top_margin
* vf
->dmpi
->stride
[0],
219 mpi
->w
* (vf
->dmpi
->bpp
/ 8),
223 vf
->dmpi
->planes
[1] = mpi
->planes
[1]; // passthrough rgb8 palette
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
);
232 static void update_limits(struct vf_instance
*vf
, int starty
, int endy
,
233 int startx
, int endx
)
236 endy
= (endy
+ 1) >> 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
)
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
)
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
];
267 dst
[(j
<< 1) + 1] = val
;
268 dst_next
[j
<< 1] = val
;
269 dst_next
[(j
<< 1) + 1] = val
;
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
)
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
++) {
295 val
+= src
[(j
<< 1) + 1];
296 val
+= src_next
[j
<< 1];
297 val
+= src_next
[(j
<< 1) + 1];
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
;
317 mp_image_t
*dmpi
= vf
->dmpi
;
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;
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
)
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
);
348 my_draw_bitmap(vf
, img
->bitmap
, img
->w
, img
->h
, img
->stride
,
349 img
->dst_x
, img
->dst_y
, img
->color
);
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
);
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
)
381 return vf_next_query_format(vf
, vf
->priv
->outfmt
);
386 static int control(vf_instance_t
*vf
, int request
, void *data
)
389 case VFCTRL_SET_OSD_OBJ
:
390 vf
->priv
->osd
= data
;
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
);
404 case VFCTRL_DRAW_EOSD
:
405 if (vf
->priv
->renderer_realaspect
)
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
);
424 static const unsigned int fmt_list
[] = {
431 static int vf_open(vf_instance_t
*vf
, char *args
)
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
) {
440 } else if (vf
->priv
->auto_insert
&& flags
& VFCAP_EOSD
) {
445 if (vf
->priv
->auto_insert
)
446 mp_msg(MSGT_ASS
, MSGL_INFO
, "[ass] auto-open\n");
449 vf
->query_format
= query_format
;
451 vf
->control
= control
;
452 vf
->get_image
= get_image
;
453 vf
->put_image
= put_image
;
454 vf
->default_caps
= VFCAP_EOSD
;
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
= {
466 sizeof(struct vf_priv_s
),
471 const vf_info_t vf_info_ass
= {
472 "Render ASS/SSA subtitles",