]> git.sesse.net Git - vlc/blob - modules/codec/libass.c
Cosmetics + removed a few debugs.
[vlc] / modules / codec / libass.c
1 /*****************************************************************************
2  * SSA/ASS subtitle decoder using libass.
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 #   include "config.h"
30 #endif
31
32 #include <string.h>
33 #include <limits.h>
34 #include <assert.h>
35 #include <math.h>
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_vout.h>
40 #include <vlc_codec.h>
41 #include <vlc_osd.h>
42 #include <vlc_input.h>
43
44 #include <ass/ass.h>
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  Create ( vlc_object_t * );
50 static void Destroy( vlc_object_t * );
51
52 vlc_module_begin();
53     set_shortname( N_("Subtitles (advanced)"));
54     set_description( N_("Subtitle renderers using libass") );
55     set_capability( "decoder", 100 );
56     set_category( CAT_INPUT );
57     set_subcategory( SUBCAT_INPUT_SCODEC );
58     set_callbacks( Create, Destroy );
59 vlc_module_end();
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static subpicture_t *DecodeBlock( decoder_t *, block_t ** );
65 static void DestroySubpicture( subpicture_t * );
66 static void PreRender( video_format_t *, spu_t *, subpicture_t * );
67 static void UpdateRegions( video_format_t *, spu_t *,
68                            subpicture_t *, mtime_t );
69
70 /* Yes libass sux with threads */
71 typedef struct 
72 {
73     vlc_object_t   *p_libvlc;
74
75     vlc_mutex_t     *p_lock;
76     int             i_refcount;
77     ass_library_t   *p_library;
78     ass_renderer_t  *p_renderer;
79     video_format_t  fmt;
80 } ass_handle_t;
81 static ass_handle_t *AssHandleYield( decoder_t *p_dec );
82 static void AssHandleRelease( ass_handle_t * );
83
84 /* */
85 struct decoder_sys_t
86 {
87     /* decoder_sys_t is shared between decoder and spu units */
88     vlc_mutex_t  lock;
89     int          i_refcount;
90
91     /* */
92     ass_handle_t *p_ass;
93
94     /* */
95     ass_track_t  *p_track;
96
97     /* */
98     subpicture_t *p_spu_final;
99 };
100 static void DecSysRelease( decoder_sys_t *p_sys );
101 static void DecSysYield( decoder_sys_t *p_sys );
102
103 struct subpicture_sys_t
104 {
105     decoder_sys_t *p_dec_sys;
106     void *p_subs_data;
107     int i_subs_len;
108 };
109
110 typedef struct
111 {
112     int x0;
113     int y0;
114     int x1;
115     int y1;
116 } rectangle_t;
117
118 static int BuildRegions( spu_t *p_spu, rectangle_t *p_region, int i_max_region, ass_image_t *p_img_list, int i_width, int i_height );
119 static void SubpictureReleaseRegions( spu_t *p_spu, subpicture_t *p_subpic );
120 static void RegionDraw( subpicture_region_t *p_region, ass_image_t *p_img );
121
122 //#define DEBUG_REGION
123
124 /*****************************************************************************
125  * Create: Open libass decoder.
126  *****************************************************************************/
127 static int Create( vlc_object_t *p_this )
128 {
129     decoder_t *p_dec = (decoder_t *)p_this;
130     decoder_sys_t *p_sys;
131     ass_track_t *p_track;
132
133     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
134         return VLC_EGENERIC;
135
136     p_dec->pf_decode_sub = DecodeBlock;
137
138     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
139     if( !p_sys )
140         return VLC_ENOMEM;
141
142     /* */
143     vlc_mutex_init( &p_sys->lock );
144     p_sys->i_refcount = 1;
145
146     p_sys->p_ass = AssHandleYield( p_dec );
147
148     /* load attachments */
149     input_attachment_t  **pp_attachments;
150     int                   i_attachments;
151
152     if( decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments ))
153     {
154         i_attachments = 0;
155         pp_attachments = NULL;
156     }
157     for( int k = 0; k < i_attachments; k++ )
158     {
159         input_attachment_t *p_attach = pp_attachments[k];
160
161         if( !strcasecmp( p_attach->psz_mime, "application/x-truetype-font" ) )
162         {
163             msg_Dbg( p_dec, "adding embedded font %s\n", p_attach->psz_name );
164
165             vlc_mutex_lock( p_sys->p_ass->p_lock );
166             ass_add_font( p_sys->p_ass->p_library, p_attach->psz_name, p_attach->p_data, p_attach->i_data );
167             vlc_mutex_unlock( p_sys->p_ass->p_lock );
168         }
169         vlc_input_attachment_Delete( p_attach );
170     }
171     free( pp_attachments );
172
173     /* Add a track */
174     vlc_mutex_lock( p_sys->p_ass->p_lock );
175     p_sys->p_track = p_track = ass_new_track( p_sys->p_ass->p_library );
176     ass_process_codec_private( p_track, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
177     vlc_mutex_unlock( p_sys->p_ass->p_lock );
178
179     return VLC_SUCCESS;
180 }
181
182 /*****************************************************************************
183  * Destroy: finish
184  *****************************************************************************/
185 static void Destroy( vlc_object_t *p_this )
186 {
187     decoder_t *p_dec = (decoder_t *)p_this;
188
189     DecSysRelease( p_dec->p_sys );
190 }
191
192 static void DecSysYield( decoder_sys_t *p_sys )
193 {
194     vlc_mutex_lock( &p_sys->lock );
195     p_sys->i_refcount++;
196     vlc_mutex_unlock( &p_sys->lock );
197 }
198 static void DecSysRelease( decoder_sys_t *p_sys )
199 {
200     /* */
201     vlc_mutex_lock( &p_sys->lock );
202     p_sys->i_refcount--;
203     if( p_sys->i_refcount > 0 )
204     {
205         vlc_mutex_unlock( &p_sys->lock );
206         return;
207     }
208     vlc_mutex_unlock( &p_sys->lock );
209     vlc_mutex_destroy( &p_sys->lock );
210
211     vlc_mutex_lock( p_sys->p_ass->p_lock );
212     ass_free_track( p_sys->p_track );
213     vlc_mutex_unlock( p_sys->p_ass->p_lock );
214
215     AssHandleRelease( p_sys->p_ass );
216     free( p_sys );
217 }
218
219 /****************************************************************************
220  * DecodeBlock:
221  ****************************************************************************/
222 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
223 {
224     decoder_sys_t *p_sys = p_dec->p_sys;
225
226     subpicture_t *p_spu = NULL;
227     block_t *p_block;
228
229     //msg_Dbg( p_dec, "DecodeBlock %p", (void *)pp_block);
230     if( !pp_block || *pp_block == NULL )
231         return NULL;
232
233     p_block = *pp_block;
234     if( p_block->i_rate != 0 )
235         p_block->i_length = p_block->i_length * p_block->i_rate / INPUT_RATE_DEFAULT;
236     *pp_block = NULL;
237
238     if( p_block->i_buffer == 0 || p_block->p_buffer[0] == '\0' )
239     {
240         block_Release( p_block );
241         return NULL;
242     }
243
244     p_spu = p_dec->pf_spu_buffer_new( p_dec );
245     if( !p_spu )
246     {
247         msg_Warn( p_dec, "can't get spu buffer" );
248         block_Release( p_block );
249         return NULL;
250     }
251
252     p_spu->p_sys = malloc( sizeof( subpicture_sys_t ));
253     if( !p_spu->p_sys )
254     {
255         p_dec->pf_spu_buffer_del( p_dec, p_spu );
256         block_Release( p_block );
257         return NULL;
258     }
259
260     p_spu->p_sys->i_subs_len = p_block->i_buffer;
261     p_spu->p_sys->p_subs_data = malloc( p_block->i_buffer );
262     if( !p_spu->p_sys->p_subs_data )
263     {
264         free( p_spu->p_sys );
265         p_dec->pf_spu_buffer_del( p_dec, p_spu );
266         block_Release( p_block );
267         return NULL;
268     }
269     memcpy( p_spu->p_sys->p_subs_data, p_block->p_buffer,
270             p_block->i_buffer );
271
272     p_spu->i_x = 0;
273     p_spu->i_y = 0;
274     p_spu->i_start = p_block->i_pts;
275     p_spu->i_stop = p_block->i_pts + p_block->i_length;
276     p_spu->b_ephemer = true;
277     p_spu->b_absolute = true;
278     p_spu->b_pausable = true; /* ? */
279
280     vlc_mutex_lock( p_sys->p_ass->p_lock );
281     ass_process_chunk( p_sys->p_track, p_spu->p_sys->p_subs_data, p_spu->p_sys->i_subs_len,
282                        p_spu->i_start / 1000, (p_spu->i_stop-p_spu->i_start) / 1000 );
283     vlc_mutex_unlock( p_sys->p_ass->p_lock );
284
285     p_spu->pf_pre_render = PreRender;
286     p_spu->pf_update_regions = UpdateRegions;
287     p_spu->pf_destroy = DestroySubpicture;
288     p_spu->p_sys->p_dec_sys = p_sys;
289
290     DecSysYield( p_sys );
291
292     block_Release( p_block );
293
294     return p_spu;
295 }
296
297 /****************************************************************************
298  *
299  ****************************************************************************/
300 static void DestroySubpicture( subpicture_t *p_subpic )
301 {
302     DecSysRelease( p_subpic->p_sys->p_dec_sys );
303
304     free( p_subpic->p_sys->p_subs_data );
305     free( p_subpic->p_sys );
306 }
307
308 static void PreRender( video_format_t *p_fmt, spu_t *p_spu,
309                        subpicture_t *p_subpic )
310 {
311     decoder_sys_t *p_dec_sys = p_subpic->p_sys->p_dec_sys;
312
313     p_dec_sys->p_spu_final = p_subpic;
314     VLC_UNUSED(p_fmt);
315     VLC_UNUSED(p_spu);
316 }
317
318 static void UpdateRegions( video_format_t *p_fmt, spu_t *p_spu,
319                            subpicture_t *p_subpic, mtime_t i_ts )
320 {
321     decoder_sys_t *p_sys = p_subpic->p_sys->p_dec_sys;
322     ass_handle_t *p_ass = p_sys->p_ass;
323
324     video_format_t fmt;
325     bool b_fmt_changed;
326
327     if( p_subpic != p_sys->p_spu_final )
328     {
329         SubpictureReleaseRegions( p_spu, p_subpic );
330         return;
331     }
332
333     vlc_mutex_lock( p_ass->p_lock );
334
335     /* */
336     fmt = *p_fmt;
337     fmt.i_chroma = VLC_FOURCC('R','G','B','A');
338     fmt.i_width = fmt.i_visible_width;
339     fmt.i_height = fmt.i_visible_height;
340     fmt.i_bits_per_pixel = 0;
341     fmt.i_x_offset = fmt.i_y_offset = 0;
342     fmt.i_sar_num = 1;
343     fmt.i_sar_den = 1;
344
345     b_fmt_changed = memcmp( &fmt, &p_ass->fmt, sizeof(fmt) ) != 0;
346     if( b_fmt_changed )
347     {
348         ass_set_frame_size( p_ass->p_renderer, fmt.i_width, fmt.i_height );
349         ass_set_aspect_ratio( p_ass->p_renderer, 1.0 ); // TODO ?
350
351         p_ass->fmt = fmt;
352     }
353
354     /* */
355     int i_changed;
356     ass_image_t *p_img = ass_render_frame( p_ass->p_renderer, p_sys->p_track, i_ts/1000, &i_changed );
357
358     if( !i_changed && !b_fmt_changed )
359     {
360         vlc_mutex_unlock( p_ass->p_lock );
361         return;
362     }
363
364     /* */
365     p_subpic->i_original_picture_height = fmt.i_height;
366     p_subpic->i_original_picture_width = fmt.i_width;
367     SubpictureReleaseRegions( p_spu, p_subpic );
368
369     /* XXX to improve efficiency we merge regions that are close minimizing
370      * the lost surface.
371      * libass tends to create a lot of small regions and thus spu engine
372      * reinstanciate a lot the scaler, and as we do not support subpel blending
373      * it looks ugly (text unaligned).
374      */
375     const int i_max_region = 4;
376     rectangle_t region[i_max_region];
377     const int i_region = BuildRegions( p_spu, region, i_max_region, p_img, fmt.i_width, fmt.i_height );
378
379     if( i_region <= 0 )
380     {
381         vlc_mutex_unlock( p_ass->p_lock );
382         return;
383     }
384
385     /* Allocate the regions and draw them */
386     subpicture_region_t *pp_region[i_max_region];
387     subpicture_region_t **pp_region_last = &p_subpic->p_region;
388
389     for( int i = 0; i < i_region; i++ )
390     {
391         subpicture_region_t *r;
392         video_format_t fmt_region;
393
394         /* */
395         fmt_region = fmt;
396         fmt_region.i_width =
397         fmt_region.i_visible_width  = region[i].x1 - region[i].x0;
398         fmt_region.i_height =
399         fmt_region.i_visible_height = region[i].y1 - region[i].y0;
400
401         pp_region[i] = r = p_subpic->pf_create_region( VLC_OBJECT(p_spu), &fmt_region );
402         if( !r )
403             break;
404         r->i_x = region[i].x0;
405         r->i_y = region[i].y0;
406         r->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
407
408         /* */
409         RegionDraw( r, p_img );
410
411         /* */
412         *pp_region_last = r;
413         pp_region_last = &r->p_next;
414     }
415     vlc_mutex_unlock( p_ass->p_lock );
416 }
417
418 static rectangle_t r_create( int x0, int y0, int x1, int y1 )
419 {
420     rectangle_t r = { x0, y0, x1, y1 };
421     return r;
422 }
423 static rectangle_t r_img( const ass_image_t *p_img )
424 {
425     return r_create( p_img->dst_x, p_img->dst_y, p_img->dst_x+p_img->w, p_img->dst_y+p_img->h );
426 }
427 static void r_add( rectangle_t *r, const rectangle_t *n )
428 {
429     r->x0 = __MIN( r->x0, n->x0 );
430     r->y0 = __MIN( r->y0, n->y0 );
431     r->x1 = __MAX( r->x1, n->x1 );
432     r->y1 = __MAX( r->y1, n->y1 );
433 }
434 static int r_surface( const rectangle_t *r )
435 {
436     return (r->x1-r->x0) * (r->y1-r->y0);
437 }
438 static bool r_overlap( const rectangle_t *a, const rectangle_t *b, int i_dx, int i_dy )
439 {
440     return  __MAX(a->x0-i_dx, b->x0) < __MIN( a->x1+i_dx, b->x1 ) &&
441             __MAX(a->y0-i_dy, b->y0) < __MIN( a->y1+i_dy, b->y1 );
442 }
443
444 static int BuildRegions( spu_t *p_spu, rectangle_t *p_region, int i_max_region, ass_image_t *p_img_list, int i_width, int i_height )
445 {
446     ass_image_t *p_tmp;
447     int i_count;
448
449 #ifdef DEBUG_REGION
450     int64_t i_ck_start = mdate();
451 #endif
452
453     for( p_tmp = p_img_list, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->next )
454         i_count++;
455     if( i_count <= 0 )
456         return 0;
457
458     ass_image_t *pp_img[i_count];   // TODO move to context ?
459     for( p_tmp = p_img_list, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->next, i_count++ )
460         pp_img[i_count] = p_tmp;
461
462     /* */
463     const int i_w_inc = __MAX( ( i_width + 49 ) / 50, 32 );
464     const int i_h_inc = __MAX( ( i_height + 99 ) / 100, 32 );
465     int i_maxh = i_w_inc;
466     int i_maxw = i_h_inc;
467     int i_region;
468     rectangle_t region[i_max_region+1];
469
470     i_region = 0;
471     for( int i_used = 0; i_used < i_count; )
472     {
473         int n;
474         for( n = 0; n < i_count; n++ )
475         {
476             if( pp_img[n] )
477                 break;
478         }
479         assert( i_region < i_max_region + 1 );
480         region[i_region++] = r_img( pp_img[n] );
481         pp_img[n] = NULL; i_used++;
482
483         bool b_ok;
484         do {
485             b_ok = false;
486             for( n = 0; n < i_count; n++ )
487             {
488                 ass_image_t *p_img = pp_img[n];
489                 if( !p_img )
490                     continue;
491                 rectangle_t r = r_img( p_img );
492
493                 int k;
494                 int i_best = -1;
495                 int i_best_s = INT_MAX;
496                 for( k = 0; k < i_region; k++ )
497                 {
498                     if( !r_overlap( &region[k], &r, i_maxw, i_maxh ) )
499                         continue;
500                     int s = r_surface( &r );
501                     if( s < i_best_s )
502                     {
503                         i_best_s = s;
504                         i_best = k;
505                     }
506                 }
507                 if( i_best >= 0 )
508                 {
509                     r_add( &region[i_best], &r );
510                     pp_img[n] = NULL; i_used++;
511                     b_ok = true;
512                 }
513             }
514         } while( b_ok );
515
516         if( i_region > i_max_region )
517         {
518             int i_best_i = -1;
519             int i_best_j = -1;
520             int i_best_ds = INT_MAX;
521
522             /* merge best */
523             for( int i = 0; i < i_region; i++ )
524             {
525                 for( int j = i+1; j < i_region; j++ )
526                 {
527                     rectangle_t n = region[i];
528                     r_add( &n, &region[j] );
529                     int ds = r_surface( &n ) - r_surface( &region[i] ) - r_surface( &region[j] );
530
531                     if( ds < i_best_ds )
532                     {
533                         i_best_i = i;
534                         i_best_j = j;
535                         i_best_ds = ds;
536                     }
537                 }
538             }
539 #ifdef DEBUG_REGION
540             msg_Err( p_spu, "Merging %d and %d", i_best_i, i_best_j );
541 #endif
542             r_add( &region[i_best_i], &region[i_best_j] );
543
544             if( i_best_j+1 < i_region )
545                 memmove( &region[i_best_j], &region[i_best_j+1], sizeof(*region) * ( i_region - (i_best_j+1)  ) );
546             i_region--;
547         }
548     }
549
550     /* */
551     for( int n = 0; n < i_region; n++ )
552         p_region[n] = region[n];
553
554 #ifdef DEBUG_REGION
555     int64_t i_ck_time = mdate() - i_ck_start;
556     msg_Err( p_spu, "ASS: %d objects merged into %d region in %d micros", i_count, i_region, (int)(i_ck_time) );
557 #endif
558
559     return i_region;
560 }
561
562 static void RegionDraw( subpicture_region_t *p_region, ass_image_t *p_img )
563 {
564     const plane_t *p = &p_region->picture.p[0];
565     const int i_x = p_region->i_x;
566     const int i_y = p_region->i_y;
567     const int i_width  = p_region->fmt.i_width;
568     const int i_height = p_region->fmt.i_height;
569
570     memset( p->p_pixels, 0x00, p->i_pitch * p->i_lines );
571     for( ; p_img != NULL; p_img = p_img->next )
572     {
573         if( p_img->dst_x < i_x || p_img->dst_x + p_img->w > i_x + i_width ||
574             p_img->dst_y < i_y || p_img->dst_y + p_img->h > i_y + i_height )
575             continue;
576
577         const int r = (p_img->color >> 24)&0xff;
578         const int g = (p_img->color >> 16)&0xff;
579         const int b = (p_img->color >>  8)&0xff;
580         const int a = (p_img->color      )&0xff;
581         int x, y;
582
583         for( y = 0; y < p_img->h; y++ )
584         {
585             for( x = 0; x < p_img->w; x++ )
586             {
587                 const int alpha = p_img->bitmap[y*p_img->stride+x];
588                 const int an = (255 - a) * alpha / 255;
589
590                 uint8_t *p_rgba = &p->p_pixels[(y+p_img->dst_y-i_y) * p->i_pitch + 4 * (x+p_img->dst_x-i_x)];
591 #if 0
592                 p_rgba[0] = b;
593                 p_rgba[1] = g;
594                 p_rgba[2] = r;
595                 p_rgba[3] = an;
596 #else
597                 // FIXME it is not right
598                 p_rgba[0] = ( p_rgba[0] * (255-an) + b * an ) / 255;
599                 p_rgba[1] = ( p_rgba[1] * (255-an) + g * an ) / 255;
600                 p_rgba[2] = ( p_rgba[2] * (255-an) + r * an ) / 255;
601                 p_rgba[3] = 255 - ( 255 - p_rgba[3] ) * ( 255 - an ) / 255;
602 #endif
603             }
604         }
605     }
606
607 #ifdef DEBUG_REGION
608     /* XXX Draw a box for debug */
609 #define P(x,y) ((uint32_t*)&p->p_pixels[(y)*p->i_pitch + 4*(x)])
610     for( int y = 0; y < p->i_lines; y++ )
611         *P(0,y) = *P(p->i_visible_pitch/4-1,y) = 0xff000000;
612     for( int x = 0; x < p->i_visible_pitch; x++ )
613         *P(x/4,0) = *P(x/4,p->i_visible_lines-1) = 0xff000000;
614 #undef P
615 #endif
616 }
617
618
619 static void SubpictureReleaseRegions( spu_t *p_spu, subpicture_t *p_subpic )
620 {
621     while( p_subpic->p_region )
622     {
623         subpicture_region_t *p_region = p_subpic->p_region;
624         p_subpic->p_region = p_region->p_next;
625         spu_DestroyRegion( p_spu, p_region );
626     }
627     p_subpic->p_region = NULL;
628 }
629
630 /* */
631 static ass_handle_t *AssHandleYield( decoder_t *p_dec )
632 {
633     vlc_mutex_t *p_lock = var_AcquireMutex( "libass" );
634     if( !p_lock )
635         return NULL;
636
637     ass_handle_t *p_ass = NULL;
638     ass_library_t *p_library = NULL;
639     ass_renderer_t *p_renderer = NULL;
640     vlc_value_t val;
641
642     var_Create( p_dec->p_libvlc, "libass-handle", VLC_VAR_ADDRESS );
643     if( var_Get( p_dec->p_libvlc, "libass-handle", &val ) )
644         val.p_address = NULL;
645
646     if( val.p_address )
647     {
648         p_ass = val.p_address;
649
650         p_ass->i_refcount++;
651
652         vlc_mutex_unlock( p_lock );
653         return p_ass;
654     }
655
656     /* */
657     p_ass = malloc( sizeof(*p_ass) );
658     if( !p_ass )
659         goto error;
660
661     /* */
662     p_ass->p_libvlc = VLC_OBJECT(p_dec->p_libvlc);
663     p_ass->p_lock = p_lock;
664     p_ass->i_refcount = 1;
665
666     /* Create libass library */
667     p_ass->p_library = p_library = ass_library_init();
668     if( !p_library )
669         goto error;
670
671     ass_set_fonts_dir( p_library, "/usr/share/fonts" ); // FIXME
672     ass_set_extract_fonts( p_library, true );
673     ass_set_style_overrides( p_library, NULL );
674
675     /* Create the renderer */
676     p_ass->p_renderer = p_renderer = ass_renderer_init( p_library );
677     if( !p_renderer )
678         goto error;
679
680     ass_set_use_margins( p_renderer, false);
681     //if( false )
682     //    ass_set_margins( p_renderer, int t, int b, int l, int r);
683     ass_set_hinting( p_renderer, ASS_HINTING_NATIVE ); // No idea
684     ass_set_font_scale( p_renderer, 1.0 );
685     ass_set_line_spacing( p_renderer, 0.0 );
686
687     const char *psz_font = "";//"/usr/src/videolan/vlc-libass.git/share/skins2/fonts/FreeSans.ttf"; // FIXME
688     const char *psz_family = "";//"Arial"; // FIXME
689 #ifdef HAVE_FONTCONFIG
690     ass_set_fonts( p_renderer, psz_font, psz_family );  // setup default font/family
691 #else
692     /* FIXME you HAVE to give him a font if no fontconfig */
693     ass_set_fonts_nofc( p_renderer, psz_font, psz_family );
694 #endif
695     memset( &p_ass->fmt, 0, sizeof(p_ass->fmt) );
696
697     /* */
698     val.p_address = p_ass;
699     var_Set( p_dec->p_libvlc, "libass-handle", val );
700
701     /* */
702     vlc_mutex_unlock( p_ass->p_lock );
703     return p_ass;
704
705 error:
706     if( p_renderer )
707         ass_renderer_done( p_renderer );
708     if( p_library )
709         ass_library_done( p_library );
710
711     free( p_ass );
712     vlc_mutex_unlock( p_lock );
713     return NULL;
714 }
715 static void AssHandleRelease( ass_handle_t *p_ass )
716 {
717     vlc_mutex_lock( p_ass->p_lock );
718     p_ass->i_refcount--;
719     if( p_ass->i_refcount > 0 )
720     {
721         vlc_mutex_unlock( p_ass->p_lock );
722         return;
723     }
724
725     ass_renderer_done( p_ass->p_renderer );
726     ass_library_done( p_ass->p_library );
727
728     vlc_value_t val;
729     val.p_address = NULL;
730     var_Set( p_ass->p_libvlc, "libass-handle", val );
731
732     vlc_mutex_unlock( p_ass->p_lock );
733     free( p_ass );
734 }
735