Update to MPlayer SVN rev 30134 and FFmpeg SVN rev 20947.
[vaapi:mplayer.git] / libmpcodecs / .svn / text-base / vf_ass.c.svn-base
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>
5  *
6  * This file is part of MPlayer.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <inttypes.h>
29 #include <assert.h>
30
31 #include "config.h"
32 #include "mp_msg.h"
33 #include "help_mp.h"
34
35 #include "img_format.h"
36 #include "mp_image.h"
37 #include "vf.h"
38
39 #include "libvo/fastmemcpy.h"
40
41 #include "m_option.h"
42 #include "m_struct.h"
43
44 #include "libass/ass_mp.h"
45
46 #define _r(c)  ((c)>>24)
47 #define _g(c)  (((c)>>16)&0xFF)
48 #define _b(c)  (((c)>>8)&0xFF)
49 #define _a(c)  ((c)&0xFF)
50 #define rgba2y(c)  ( (( 263*_r(c)  + 516*_g(c) + 100*_b(c)) >> 10) + 16  )
51 #define rgba2u(c)  ( ((-152*_r(c) - 298*_g(c) + 450*_b(c)) >> 10) + 128 )
52 #define rgba2v(c)  ( (( 450*_r(c) - 376*_g(c) -  73*_b(c)) >> 10) + 128 )
53
54
55 static const struct vf_priv_s {
56         int outh, outw;
57
58         unsigned int outfmt;
59
60         // 1 = auto-added filter: insert only if chain does not support EOSD already
61         // 0 = insert always
62         int auto_insert;
63
64         ass_renderer_t* ass_priv;
65
66         unsigned char* planes[3];
67         unsigned char* dirty_rows;
68 } vf_priv_dflt;
69
70 extern int opt_screen_size_x;
71 extern int opt_screen_size_y;
72
73 extern ass_track_t* ass_track;
74 extern float sub_delay;
75 extern int sub_visibility;
76
77 static int config(struct vf_instance_s* vf,
78         int width, int height, int d_width, int d_height,
79         unsigned int flags, unsigned int outfmt)
80 {
81         if (outfmt == IMGFMT_IF09) return 0;
82
83         vf->priv->outh = height + ass_top_margin + ass_bottom_margin;
84         vf->priv->outw = width;
85
86         if(!opt_screen_size_x && !opt_screen_size_y){
87                 d_width = d_width * vf->priv->outw / width;
88                 d_height = d_height * vf->priv->outh / height;
89         }
90
91         vf->priv->planes[1] = malloc(vf->priv->outw * vf->priv->outh);
92         vf->priv->planes[2] = malloc(vf->priv->outw * vf->priv->outh);
93         vf->priv->dirty_rows = malloc(vf->priv->outh);
94
95         if (vf->priv->ass_priv) {
96                 ass_configure(vf->priv->ass_priv, vf->priv->outw, vf->priv->outh, 0);
97 #if defined(LIBASS_VERSION) && LIBASS_VERSION >= 0x00908000
98                 ass_set_aspect_ratio(vf->priv->ass_priv, ((double)d_width) / d_height, ((double)width) / height);
99 #else
100                 ass_set_aspect_ratio(vf->priv->ass_priv, ((double)d_width) / d_height);
101 #endif
102         }
103
104         return vf_next_config(vf, vf->priv->outw, vf->priv->outh, d_width, d_height, flags, outfmt);
105 }
106
107 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi)
108 {
109         if(mpi->type == MP_IMGTYPE_IPB) return;
110         if(mpi->flags & MP_IMGFLAG_PRESERVE) return;
111         if(mpi->imgfmt != vf->priv->outfmt) return; // colorspace differ
112
113         // width never changes, always try full DR
114         mpi->priv = vf->dmpi = vf_get_image(vf->next, mpi->imgfmt,
115                         mpi->type, mpi->flags | MP_IMGFLAG_READABLE,
116                         vf->priv->outw,
117                         vf->priv->outh);
118
119         if((vf->dmpi->flags & MP_IMGFLAG_DRAW_CALLBACK) &&
120                         !(vf->dmpi->flags & MP_IMGFLAG_DIRECT)){
121                 mp_msg(MSGT_ASS, MSGL_INFO, MSGTR_MPCODECS_FullDRNotPossible);
122                 return;
123         }
124
125         // set up mpi as a cropped-down image of dmpi:
126         if(mpi->flags&MP_IMGFLAG_PLANAR){
127                 mpi->planes[0]=vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
128                 mpi->planes[1]=vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1];
129                 mpi->planes[2]=vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2];
130                 mpi->stride[1]=vf->dmpi->stride[1];
131                 mpi->stride[2]=vf->dmpi->stride[2];
132         } else {
133                 mpi->planes[0]=vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0];
134         }
135         mpi->stride[0]=vf->dmpi->stride[0];
136         mpi->width=vf->dmpi->width;
137         mpi->flags|=MP_IMGFLAG_DIRECT;
138         mpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
139 //      vf->dmpi->flags&=~MP_IMGFLAG_DRAW_CALLBACK;
140 }
141
142 static void blank(mp_image_t *mpi, int y1, int y2)
143 {
144         int color[3] = {16, 128, 128}; // black (YUV)
145         int y;
146         unsigned char* dst;
147         int chroma_rows = (y2 - y1) >> mpi->chroma_y_shift;
148
149         dst = mpi->planes[0] + y1 * mpi->stride[0];
150         for (y = 0; y < y2 - y1; ++y) {
151                 memset(dst, color[0], mpi->w);
152                 dst += mpi->stride[0];
153         }
154         dst = mpi->planes[1] + (y1 >> mpi->chroma_y_shift) * mpi->stride[1];
155         for (y = 0; y < chroma_rows ; ++y) {
156                 memset(dst, color[1], mpi->chroma_width);
157                 dst += mpi->stride[1];
158         }
159         dst = mpi->planes[2] + (y1 >> mpi->chroma_y_shift) * mpi->stride[2];
160         for (y = 0; y < chroma_rows ; ++y) {
161                 memset(dst, color[2], mpi->chroma_width);
162                 dst += mpi->stride[2];
163         }
164 }
165
166 static int prepare_image(struct vf_instance_s* vf, mp_image_t *mpi)
167 {
168         if(mpi->flags&MP_IMGFLAG_DIRECT || mpi->flags&MP_IMGFLAG_DRAW_CALLBACK){
169                 vf->dmpi = mpi->priv;
170                 if (!vf->dmpi) { mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_MPCODECS_FunWhydowegetNULL); return 0; }
171                 mpi->priv = NULL;
172                 // we've used DR, so we're ready...
173                 if (ass_top_margin)
174                         blank(vf->dmpi, 0, ass_top_margin);
175                 if (ass_bottom_margin)
176                         blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
177                 if(!(mpi->flags&MP_IMGFLAG_PLANAR))
178                         vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
179                 return 0;
180         }
181
182         // hope we'll get DR buffer:
183         vf->dmpi = vf_get_image(vf->next, vf->priv->outfmt,
184                         MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_READABLE,
185                         vf->priv->outw, vf->priv->outh);
186
187         // copy mpi->dmpi...
188         if(mpi->flags&MP_IMGFLAG_PLANAR){
189                 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
190                                 mpi->planes[0], mpi->w, mpi->h,
191                                 vf->dmpi->stride[0], mpi->stride[0]);
192                 memcpy_pic(vf->dmpi->planes[1] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[1],
193                                 mpi->planes[1], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift,
194                                 vf->dmpi->stride[1], mpi->stride[1]);
195                 memcpy_pic(vf->dmpi->planes[2] + (ass_top_margin >> mpi->chroma_y_shift) * vf->dmpi->stride[2],
196                                 mpi->planes[2], mpi->w >> mpi->chroma_x_shift, mpi->h >> mpi->chroma_y_shift,
197                                 vf->dmpi->stride[2], mpi->stride[2]);
198         } else {
199                 memcpy_pic(vf->dmpi->planes[0] + ass_top_margin * vf->dmpi->stride[0],
200                                 mpi->planes[0], mpi->w*(vf->dmpi->bpp/8), mpi->h,
201                                 vf->dmpi->stride[0], mpi->stride[0]);
202                 vf->dmpi->planes[1] = mpi->planes[1]; // passthrough rgb8 palette
203         }
204         if (ass_top_margin)
205                 blank(vf->dmpi, 0, ass_top_margin);
206         if (ass_bottom_margin)
207                 blank(vf->dmpi, vf->priv->outh - ass_bottom_margin, vf->priv->outh);
208         return 0;
209 }
210
211 /**
212  * \brief Copy specified rows from render_context.dmpi to render_context.planes, upsampling to 4:4:4
213  */
214 static void copy_from_image(struct vf_instance_s* vf, int first_row, int last_row)
215 {
216         int pl;
217         int i, j, k;
218         unsigned char val;
219         int chroma_rows;
220
221         first_row -= (first_row % 2);
222         last_row += (last_row % 2);
223         chroma_rows = (last_row - first_row) / 2;
224
225         assert(first_row >= 0);
226         assert(first_row <= last_row);
227         assert(last_row <= vf->priv->outh);
228
229         for (pl = 1; pl < 3; ++pl) {
230                 int dst_stride = vf->priv->outw;
231                 int src_stride = vf->dmpi->stride[pl];
232
233                 unsigned char* src = vf->dmpi->planes[pl] + (first_row/2) * src_stride;
234                 unsigned char* dst = vf->priv->planes[pl] + first_row * dst_stride;
235                 unsigned char* dst_next = dst + dst_stride;
236                 for(i = 0; i < chroma_rows; ++i)
237                 {
238                         if ((vf->priv->dirty_rows[first_row + i*2] == 0) ||
239                                 (vf->priv->dirty_rows[first_row + i*2 + 1] == 0)) {
240                                 for (j = 0, k = 0; j < vf->dmpi->chroma_width; ++j, k+=2) {
241                                         val = *(src + j);
242                                         *(dst + k) = val;
243                                         *(dst + k + 1) = val;
244                                         *(dst_next + k) = val;
245                                         *(dst_next + k + 1) = val;
246                                 }
247                         }
248                         src += src_stride;
249                         dst = dst_next + dst_stride;
250                         dst_next = dst + dst_stride;
251                 }
252         }
253         for (i = first_row; i < last_row; ++i)
254                 vf->priv->dirty_rows[i] = 1;
255 }
256
257 /**
258  * \brief Copy all previously copied rows back to render_context.dmpi
259  */
260 static void copy_to_image(struct vf_instance_s* vf)
261 {
262         int pl;
263         int i, j, k;
264         for (pl = 1; pl < 3; ++pl) {
265                 int dst_stride = vf->dmpi->stride[pl];
266                 int src_stride = vf->priv->outw;
267
268                 unsigned char* dst = vf->dmpi->planes[pl];
269                 unsigned char* src = vf->priv->planes[pl];
270                 unsigned char* src_next = vf->priv->planes[pl] + src_stride;
271                 for(i = 0; i < vf->dmpi->chroma_height; ++i)
272                 {
273                         if ((vf->priv->dirty_rows[i*2] == 1)) {
274                                 assert(vf->priv->dirty_rows[i*2 + 1] == 1);
275                                 for (j = 0, k = 0; j < vf->dmpi->chroma_width; ++j, k+=2) {
276                                         unsigned val = 0;
277                                         val += *(src + k);
278                                         val += *(src + k + 1);
279                                         val += *(src_next + k);
280                                         val += *(src_next + k + 1);
281                                         *(dst + j) = val >> 2;
282                                 }
283                         }
284                         dst += dst_stride;
285                         src = src_next + src_stride;
286                         src_next = src + src_stride;
287                 }
288         }
289 }
290
291 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)
292 {
293         unsigned char y = rgba2y(color);
294         unsigned char u = rgba2u(color);
295         unsigned char v = rgba2v(color);
296         unsigned char opacity = 255 - _a(color);
297         unsigned char *src, *dsty, *dstu, *dstv;
298         int i, j;
299         mp_image_t* dmpi = vf->dmpi;
300
301         src = bitmap;
302         dsty = dmpi->planes[0] + dst_x + dst_y * dmpi->stride[0];
303         dstu = vf->priv->planes[1] + dst_x + dst_y * vf->priv->outw;
304         dstv = vf->priv->planes[2] + dst_x + dst_y * vf->priv->outw;
305         for (i = 0; i < bitmap_h; ++i) {
306                 for (j = 0; j < bitmap_w; ++j) {
307                         unsigned k = ((unsigned)src[j]) * opacity / 255;
308                         dsty[j] = (k*y + (255-k)*dsty[j]) / 255;
309                         dstu[j] = (k*u + (255-k)*dstu[j]) / 255;
310                         dstv[j] = (k*v + (255-k)*dstv[j]) / 255;
311                 }
312                 src += stride;
313                 dsty += dmpi->stride[0];
314                 dstu += vf->priv->outw;
315                 dstv += vf->priv->outw;
316         }
317 }
318
319 static int render_frame(struct vf_instance_s* vf, mp_image_t *mpi, const ass_image_t* img)
320 {
321         if (img) {
322                 memset(vf->priv->dirty_rows, 0, vf->priv->outh); // reset dirty rows
323                 while (img) {
324                         copy_from_image(vf, img->dst_y, img->dst_y + img->h);
325                         my_draw_bitmap(vf, img->bitmap, img->w, img->h, img->stride,
326                                         img->dst_x, img->dst_y, img->color);
327                         img = img->next;
328                 }
329                 copy_to_image(vf);
330         }
331         return 0;
332 }
333
334 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts)
335 {
336         ass_image_t* images = 0;
337         if (sub_visibility && vf->priv->ass_priv && ass_track && (pts != MP_NOPTS_VALUE))
338                 images = ass_mp_render_frame(vf->priv->ass_priv, ass_track, (pts+sub_delay) * 1000 + .5, NULL);
339
340         prepare_image(vf, mpi);
341         if (images) render_frame(vf, mpi, images);
342
343         return vf_next_put_image(vf, vf->dmpi, pts);
344 }
345
346 static int query_format(struct vf_instance_s* vf, unsigned int fmt)
347 {
348         switch(fmt){
349         case IMGFMT_YV12:
350         case IMGFMT_I420:
351         case IMGFMT_IYUV:
352                 return vf_next_query_format(vf, vf->priv->outfmt);
353         }
354         return 0;
355 }
356
357 static int control(vf_instance_t *vf, int request, void *data)
358 {
359         switch (request) {
360         case VFCTRL_INIT_EOSD:
361                 vf->priv->ass_priv = ass_renderer_init((ass_library_t*)data);
362                 if (!vf->priv->ass_priv) return CONTROL_FALSE;
363                 ass_configure_fonts(vf->priv->ass_priv);
364                 return CONTROL_TRUE;
365         case VFCTRL_DRAW_EOSD:
366                 if (vf->priv->ass_priv) return CONTROL_TRUE;
367                 break;
368         }
369         return vf_next_control(vf, request, data);
370 }
371
372 static void uninit(struct vf_instance_s* vf)
373 {
374         if (vf->priv->ass_priv)
375                 ass_renderer_done(vf->priv->ass_priv);
376         if (vf->priv->planes[1])
377                 free(vf->priv->planes[1]);
378         if (vf->priv->planes[2])
379                 free(vf->priv->planes[2]);
380         if (vf->priv->dirty_rows)
381                 free(vf->priv->dirty_rows);
382 }
383
384 static const unsigned int fmt_list[]={
385         IMGFMT_YV12,
386         IMGFMT_I420,
387         IMGFMT_IYUV,
388         0
389 };
390
391 static int open(vf_instance_t *vf, char* args)
392 {
393         int flags;
394         vf->priv->outfmt = vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
395         if (vf->priv->outfmt)
396                 flags = vf_next_query_format(vf, vf->priv->outfmt);
397         if (!vf->priv->outfmt || (vf->priv->auto_insert && flags&VFCAP_EOSD))
398         {
399                 uninit(vf);
400                 return 0;
401         }
402
403         if (vf->priv->auto_insert)
404                 mp_msg(MSGT_ASS, MSGL_INFO, "[ass] auto-open\n");
405
406         vf->config = config;
407         vf->query_format = query_format;
408         vf->uninit = uninit;
409         vf->control = control;
410         vf->get_image = get_image;
411         vf->put_image = put_image;
412         vf->default_caps=VFCAP_EOSD;
413         return 1;
414 }
415
416 #define ST_OFF(f) M_ST_OFF(struct vf_priv_s,f)
417 static const m_option_t vf_opts_fields[] = {
418         {"auto", ST_OFF(auto_insert), CONF_TYPE_FLAG, 0 , 0, 1, NULL},
419         { NULL, NULL, 0, 0, 0, 0,  NULL }
420 };
421
422 static const m_struct_t vf_opts = {
423         "ass",
424         sizeof(struct vf_priv_s),
425         &vf_priv_dflt,
426         vf_opts_fields
427 };
428
429 const vf_info_t vf_info_ass = {
430         "Render ASS/SSA subtitles",
431         "ass",
432         "Evgeniy Stepanov",
433         "",
434         open,
435         &vf_opts
436 };
437