Use proper length specifiers in mp_msg calls, fixes the warnings:
[mplayer/greg.git] / libmpcodecs / vf_ass.c
blob9f4c91961a848c62a228a2f8a5fb240e77650a45
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
3 /*
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
6 This program 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 This program 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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, 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 "help_mp.h"
33 #include "img_format.h"
34 #include "mp_image.h"
35 #include "vf.h"
37 #include "libvo/fastmemcpy.h"
39 #include "m_option.h"
40 #include "m_struct.h"
42 #include "libass/ass.h"
43 #include "libass/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 ass_renderer_t* ass_priv;
65 unsigned char* planes[3];
66 unsigned char* dirty_rows;
67 } vf_priv_dflt;
69 extern int opt_screen_size_x;
70 extern int opt_screen_size_y;
72 extern ass_track_t* ass_track;
73 extern float sub_delay;
74 extern int sub_visibility;
76 static int config(struct vf_instance_s* vf,
77 int width, int height, int d_width, int d_height,
78 unsigned int flags, unsigned int outfmt)
80 if (outfmt == IMGFMT_IF09) return 0;
82 vf->priv->outh = height + ass_top_margin + ass_bottom_margin;
83 vf->priv->outw = width;
85 if(!opt_screen_size_x && !opt_screen_size_y){
86 d_width = d_width * vf->priv->outw / width;
87 d_height = d_height * vf->priv->outh / height;
90 vf->priv->planes[1] = malloc(vf->priv->outw * vf->priv->outh);
91 vf->priv->planes[2] = malloc(vf->priv->outw * vf->priv->outh);
92 vf->priv->dirty_rows = malloc(vf->priv->outh);
94 if (vf->priv->ass_priv) {
95 ass_configure(vf->priv->ass_priv, vf->priv->outw, vf->priv->outh, 0);
96 ass_set_aspect_ratio(vf->priv->ass_priv, ((double)d_width) / d_height);
99 return vf_next_config(vf, vf->priv->outw, vf->priv->outh, d_width, d_height, flags, outfmt);
102 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi)
104 if(mpi->type == MP_IMGTYPE_IPB) return;
105 if(mpi->flags & MP_IMGFLAG_PRESERVE) return;
106 if(mpi->imgfmt != vf->priv->outfmt) return; // colorspace differ
108 // width never changes, always try full DR
109 mpi->priv = vf->dmpi = vf_get_image(vf->next, mpi->imgfmt,
110 mpi->type, mpi->flags | MP_IMGFLAG_READABLE,
111 vf->priv->outw,
112 vf->priv->outh);
114 if((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
115 !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)){
116 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_MPCODECS_FullDRNotPossible);
117 return;
120 // set up mpi as a cropped-down image of dmpi:
121 if(mpi->flags&MP_IMGFLAG_PLANAR){
122 mpi->planes[0]=vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
123 mpi->planes[1]=vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1];
124 mpi->planes[2]=vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2];
125 mpi->stride[1]=vf->dmpi->stride[1];
126 mpi->stride[2]=vf->dmpi->stride[2];
127 } else {
128 mpi->planes[0]=vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
130 mpi->stride[0]=vf->dmpi->stride[0];
131 mpi->width=vf->dmpi->width;
132 mpi->flags|=MP_IMGFLAG_DIRECT;
133 mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
134 // vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
137 static void blank(mp_image_t *mpi, int y1, int y2)
139 int color[3] = {16, 128, 128}; // black (YUV)
140 int y;
141 unsigned char* dst;
142 int chroma_rows = (y2 - y1) >> mpi->chroma_y_shift;
144 dst = mpi->planes[0] + y1 * mpi->stride[0];
145 for (y = 0; y < y2 - y1; ++y) {
146 memset(dst, color[0], mpi->w);
147 dst += mpi->stride[0];
149 dst = mpi->planes[1] + (y1 >> mpi->chroma_y_shift) * mpi->stride[1];
150 for (y = 0; y < chroma_rows ; ++y) {
151 memset(dst, color[1], mpi->chroma_width);
152 dst += mpi->stride[1];
154 dst = mpi->planes[2] + (y1 >> mpi->chroma_y_shift) * mpi->stride[2];
155 for (y = 0; y < chroma_rows ; ++y) {
156 memset(dst, color[2], mpi->chroma_width);
157 dst += mpi->stride[2];
161 static int prepare_image(struct vf_instance_s* vf, mp_image_t *mpi)
163 if(mpi->flags&MP_IMGFLAG_DIRECT || mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){
164 vf->dmpi = mpi->priv;
165 if (!vf->dmpi) { mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_MPCODECS_FunWhydowegetNULL); return 0; }
166 mpi->priv = NULL;
167 // we've used DR, so we're ready...
168 if (ass_top_margin)
169 blank(vf->dmpi, 0, ass_top_margin);
170 if (ass_bottom_margin)
171 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
172 if(!(mpi->flags&MP_IMGFLAG_PLANAR))
173 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
174 return 0;
177 // hope we'll get DR buffer:
178 vf->dmpi = vf_get_image(vf->next, vf->priv->outfmt,
179 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_READABLE,
180 vf->priv->outw, vf->priv->outh);
182 // copy mpi->dmpi...
183 if(mpi->flags&MP_IMGFLAG_PLANAR){
184 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
185 mpi->planes[0], mpi->w, mpi->h,
186 vf->dmpi->stride[0], mpi->stride[0]);
187 memcpy_pic(vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1],
188 mpi->planes[1], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift,
189 vf->dmpi->stride[1], mpi->stride[1]);
190 memcpy_pic(vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2],
191 mpi->planes[2], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift,
192 vf->dmpi->stride[2], mpi->stride[2]);
193 } else {
194 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
195 mpi->planes[0], mpi->w*(vf->dmpi->bpp/8), mpi->h,
196 vf->dmpi->stride[0], mpi->stride[0]);
197 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
199 if (ass_top_margin)
200 blank(vf->dmpi, 0, ass_top_margin);
201 if (ass_bottom_margin)
202 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
203 return 0;
207 * \brief Copy specified rows from render_context.dmpi to render_context.planes, upsampling to 4:4:4
209 static void copy_from_image(struct vf_instance_s* vf, int first_row, int last_row)
211 int pl;
212 int i, j, k;
213 unsigned char val;
214 int chroma_rows;
216 first_row -= (first_row % 2);
217 last_row += (last_row % 2);
218 chroma_rows = (last_row - first_row) / 2;
220 for (pl = 1; pl < 3; ++pl) {
221 int dst_stride = vf->priv->outw;
222 int src_stride = vf->dmpi->stride[pl];
224 unsigned char* src = vf->dmpi->planes[pl] + (first_row/2) * src_stride;
225 unsigned char* dst = vf->priv->planes[pl] + first_row * dst_stride;
226 unsigned char* dst_next = dst + dst_stride;
227 for(i = 0; i < chroma_rows; ++i)
229 if ((vf->priv->dirty_rows[first_row + i*2] == 0) ||
230 (vf->priv->dirty_rows[first_row + i*2 + 1] == 0)) {
231 for (j = 0, k = 0; j < vf->dmpi->chroma_width; ++j, k+=2) {
232 val = *(src + j);
233 *(dst + k) = val;
234 *(dst + k + 1) = val;
235 *(dst_next + k) = val;
236 *(dst_next + k + 1) = val;
239 src += src_stride;
240 dst = dst_next + dst_stride;
241 dst_next = dst + dst_stride;
244 for (i = first_row; i < last_row; ++i)
245 vf->priv->dirty_rows[i] = 1;
249 * \brief Copy all previously copied rows back to render_context.dmpi
251 static void copy_to_image(struct vf_instance_s* vf)
253 int pl;
254 int i, j, k;
255 for (pl = 1; pl < 3; ++pl) {
256 int dst_stride = vf->dmpi->stride[pl];
257 int src_stride = vf->priv->outw;
259 unsigned char* dst = vf->dmpi->planes[pl];
260 unsigned char* src = vf->priv->planes[pl];
261 unsigned char* src_next = vf->priv->planes[pl] + src_stride;
262 for(i = 0; i < vf->dmpi->chroma_height; ++i)
264 if ((vf->priv->dirty_rows[i*2] == 1)) {
265 assert(vf->priv->dirty_rows[i*2 + 1] == 1);
266 for (j = 0, k = 0; j < vf->dmpi->chroma_width; ++j, k+=2) {
267 unsigned val = 0;
268 val += *(src + k);
269 val += *(src + k + 1);
270 val += *(src_next + k);
271 val += *(src_next + k + 1);
272 *(dst + j) = val >> 2;
275 dst += dst_stride;
276 src = src_next + src_stride;
277 src_next = src + src_stride;
282 static void my_draw_bitmap(struct vf_instance_s* vf, unsigned char* bitmap, int bitmap_w, int bitmap_h, int stride, int dst_x, int dst_y, unsigned color)
284 unsigned char y = rgba2y(color);
285 unsigned char u = rgba2u(color);
286 unsigned char v = rgba2v(color);
287 unsigned char opacity = 255 - _a(color);
288 unsigned char *src, *dsty, *dstu, *dstv;
289 int i, j;
290 mp_image_t* dmpi = vf->dmpi;
292 src = bitmap;
293 dsty = dmpi->planes[0] + dst_x + dst_y * dmpi->stride[0];
294 dstu = vf->priv->planes[1] + dst_x + dst_y * vf->priv->outw;
295 dstv = vf->priv->planes[2] + dst_x + dst_y * vf->priv->outw;
296 for (i = 0; i < bitmap_h; ++i) {
297 for (j = 0; j < bitmap_w; ++j) {
298 unsigned k = ((unsigned)src[j]) * opacity / 255;
299 dsty[j] = (k*y + (255-k)*dsty[j]) / 255;
300 dstu[j] = (k*u + (255-k)*dstu[j]) / 255;
301 dstv[j] = (k*v + (255-k)*dstv[j]) / 255;
303 src += stride;
304 dsty += dmpi->stride[0];
305 dstu += vf->priv->outw;
306 dstv += vf->priv->outw;
310 static int render_frame(struct vf_instance_s* vf, mp_image_t *mpi, const ass_image_t* img)
312 if (img) {
313 memset(vf->priv->dirty_rows, 0, vf->priv->outh); // reset dirty rows
314 while (img) {
315 copy_from_image(vf, img->dst_y, img->dst_y + img->h);
316 my_draw_bitmap(vf, img->bitmap, img->w, img->h, img->stride,
317 img->dst_x, img->dst_y, img->color);
318 img = img->next;
320 copy_to_image(vf);
322 return 0;
325 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
327 ass_image_t* images = 0;
328 if (sub_visibility && vf->priv->ass_priv && ass_track && (pts != MP_NOPTS_VALUE))
329 images = ass_render_frame(vf->priv->ass_priv, ass_track, (pts+sub_delay) * 1000 + .5, NULL);
331 prepare_image(vf, mpi);
332 if (images) render_frame(vf, mpi, images);
334 return vf_next_put_image(vf, vf->dmpi, pts);
337 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
339 switch(fmt){
340 case IMGFMT_YV12:
341 case IMGFMT_I420:
342 case IMGFMT_IYUV:
343 return vf_next_query_format(vf, vf->priv->outfmt);
345 return 0;
348 static int control(vf_instance_t *vf, int request, void *data)
350 switch (request) {
351 case VFCTRL_INIT_EOSD:
352 vf->priv->ass_priv = ass_renderer_init((ass_library_t*)data);
353 if (!vf->priv->ass_priv) return CONTROL_FALSE;
354 ass_configure_fonts(vf->priv->ass_priv);
355 return CONTROL_TRUE;
356 case VFCTRL_DRAW_EOSD:
357 if (vf->priv->ass_priv) return CONTROL_TRUE;
358 break;
360 return vf_next_control(vf, request, data);
363 static void uninit(struct vf_instance_s* vf)
365 if (vf->priv->ass_priv)
366 ass_renderer_done(vf->priv->ass_priv);
367 if (vf->priv->planes[1])
368 free(vf->priv->planes[1]);
369 if (vf->priv->planes[2])
370 free(vf->priv->planes[2]);
371 if (vf->priv->dirty_rows)
372 free(vf->priv->dirty_rows);
375 static const unsigned int fmt_list[]={
376 IMGFMT_YV12,
377 IMGFMT_I420,
378 IMGFMT_IYUV,
382 static int open(vf_instance_t *vf, char* args)
384 int flags;
385 vf->priv->outfmt = vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
386 if (vf->priv->outfmt)
387 flags = vf_next_query_format(vf, vf->priv->outfmt);
388 if (!vf->priv->outfmt || (vf->priv->auto_insert && flags&VFCAP_EOSD))
390 uninit(vf);
391 return 0;
394 if (vf->priv->auto_insert)
395 mp_msg(MSGT_ASS, MSGL_INFO, "[ass] auto-open\n");
397 vf->config = config;
398 vf->query_format = query_format;
399 vf->uninit = uninit;
400 vf->control = control;
401 vf->get_image = get_image;
402 vf->put_image = put_image;
403 vf->default_caps=VFCAP_EOSD;
404 return 1;
407 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
408 static const m_option_t vf_opts_fields[] = {
409 {"auto", ST_OFF(auto_insert), CONF_TYPE_FLAG, 0 , 0, 1, NULL},
410 { NULL, NULL, 0, 0, 0, 0, NULL }
413 static const m_struct_t vf_opts = {
414 "ass",
415 sizeof(struct vf_priv_s),
416 &vf_priv_dflt,
417 vf_opts_fields
420 const vf_info_t vf_info_ass = {
421 "Render ASS/SSA subtitles",
422 "ass",
423 "Evgeniy Stepanov",
425 open,
426 &vf_opts