]> git.sesse.net Git - vlc/blob - modules/codec/libass.c
Simplify the creation of subpicture with dynamic content.
[vlc] / modules / codec / libass.c
1 /*****************************************************************************
2  * SSA/ASS subtitle decoder using libass.
3  *****************************************************************************
4  * Copyright (C) 2008-2009 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_codec.h>
40 #include <vlc_osd.h>
41 #include <vlc_input.h>
42 #include <vlc_dialog.h>
43
44 #include <ass/ass.h>
45
46 #if defined(WIN32)
47 #   include <vlc_charset.h>
48 #endif
49
50 /* Compatibility with old libass */
51 #if !defined(LIBASS_VERSION) || LIBASS_VERSION < 0x00907010
52 #   define ASS_Renderer    ass_renderer_t
53 #   define ASS_Library     ass_library_t
54 #   define ASS_Track       ass_track_t
55 #   define ASS_Image       ass_image_t
56 #endif
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 static int  Create ( vlc_object_t * );
62 static void Destroy( vlc_object_t * );
63
64 vlc_module_begin ()
65     set_shortname( N_("Subtitles (advanced)"))
66     set_description( N_("Subtitle renderers using libass") )
67     set_capability( "decoder", 100 )
68     set_category( CAT_INPUT )
69     set_subcategory( SUBCAT_INPUT_SCODEC )
70     set_callbacks( Create, Destroy )
71 vlc_module_end ()
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static subpicture_t *DecodeBlock( decoder_t *, block_t ** );
77
78 /* Yes libass sux with threads */
79 typedef struct
80 {
81     vlc_object_t   *p_libvlc;
82
83     int             i_refcount;
84     ASS_Library     *p_library;
85     ASS_Renderer    *p_renderer;
86     video_format_t  fmt;
87 } ass_handle_t;
88 static ass_handle_t *AssHandleHold( decoder_t *p_dec );
89 static void AssHandleRelease( ass_handle_t * );
90
91 /* */
92 struct decoder_sys_t
93 {
94     mtime_t      i_max_stop;
95
96     /* decoder_sys_t is shared between decoder and spu units */
97     vlc_mutex_t  lock;
98     int          i_refcount;
99
100     /* */
101     ass_handle_t *p_ass;
102
103     /* */
104     ASS_Track    *p_track;
105 };
106 static void DecSysRelease( decoder_sys_t *p_sys );
107 static void DecSysHold( decoder_sys_t *p_sys );
108
109 /* */
110 static int SubpictureValidate( subpicture_t *,
111                                bool, const video_format_t *,
112                                bool, const video_format_t *,
113                                mtime_t );
114 static void SubpictureUpdate( subpicture_t *,
115                               const video_format_t *,
116                               const video_format_t *,
117                               mtime_t );
118 static void SubpictureDestroy( subpicture_t * );
119
120 struct subpicture_updater_sys_t
121 {
122     decoder_sys_t *p_dec_sys;
123     void          *p_subs_data;
124     int           i_subs_len;
125     mtime_t       i_pts;
126
127     ASS_Image     *p_img;
128 };
129
130 typedef struct
131 {
132     int x0;
133     int y0;
134     int x1;
135     int y1;
136 } rectangle_t;
137
138 static int BuildRegions( rectangle_t *p_region, int i_max_region, ASS_Image *p_img_list, int i_width, int i_height );
139 static void RegionDraw( subpicture_region_t *p_region, ASS_Image *p_img );
140
141 static vlc_mutex_t libass_lock = VLC_STATIC_MUTEX;
142
143 //#define DEBUG_REGION
144
145 /*****************************************************************************
146  * Create: Open libass decoder.
147  *****************************************************************************/
148 static int Create( vlc_object_t *p_this )
149 {
150     decoder_t *p_dec = (decoder_t *)p_this;
151     decoder_sys_t *p_sys;
152     ASS_Track *p_track;
153
154     if( p_dec->fmt_in.i_codec != VLC_CODEC_SSA )
155         return VLC_EGENERIC;
156
157     p_dec->pf_decode_sub = DecodeBlock;
158
159     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
160     if( !p_sys )
161         return VLC_ENOMEM;
162
163     /* */
164     p_sys->i_max_stop = VLC_TS_INVALID;
165     p_sys->p_ass = AssHandleHold( p_dec );
166     if( !p_sys->p_ass )
167     {
168         free( p_sys );
169         return VLC_EGENERIC;
170     }
171     vlc_mutex_init( &p_sys->lock );
172     p_sys->i_refcount = 1;
173
174     /* Add a track */
175     vlc_mutex_lock( &libass_lock );
176     p_sys->p_track = p_track = ass_new_track( p_sys->p_ass->p_library );
177     if( !p_track )
178     {
179         vlc_mutex_unlock( &libass_lock );
180         DecSysRelease( p_sys );
181         return VLC_EGENERIC;
182     }
183     ass_process_codec_private( p_track, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
184     vlc_mutex_unlock( &libass_lock );
185
186     p_dec->fmt_out.i_cat = SPU_ES;
187     p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
188
189     return VLC_SUCCESS;
190 }
191
192 /*****************************************************************************
193  * Destroy: finish
194  *****************************************************************************/
195 static void Destroy( vlc_object_t *p_this )
196 {
197     decoder_t *p_dec = (decoder_t *)p_this;
198
199     DecSysRelease( p_dec->p_sys );
200 }
201
202 static void DecSysHold( decoder_sys_t *p_sys )
203 {
204     vlc_mutex_lock( &p_sys->lock );
205     p_sys->i_refcount++;
206     vlc_mutex_unlock( &p_sys->lock );
207 }
208 static void DecSysRelease( decoder_sys_t *p_sys )
209 {
210     /* */
211     vlc_mutex_lock( &p_sys->lock );
212     p_sys->i_refcount--;
213     if( p_sys->i_refcount > 0 )
214     {
215         vlc_mutex_unlock( &p_sys->lock );
216         return;
217     }
218     vlc_mutex_unlock( &p_sys->lock );
219     vlc_mutex_destroy( &p_sys->lock );
220
221     vlc_mutex_lock( &libass_lock );
222     if( p_sys->p_track )
223         ass_free_track( p_sys->p_track );
224     vlc_mutex_unlock( &libass_lock );
225
226     AssHandleRelease( p_sys->p_ass );
227     free( p_sys );
228 }
229
230 /****************************************************************************
231  * DecodeBlock:
232  ****************************************************************************/
233 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
234 {
235     decoder_sys_t *p_sys = p_dec->p_sys;
236
237     subpicture_t *p_spu = NULL;
238     block_t *p_block;
239
240     if( !pp_block || *pp_block == NULL )
241         return NULL;
242
243     p_block = *pp_block;
244     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
245     {
246         p_sys->i_max_stop = VLC_TS_INVALID;
247         block_Release( p_block );
248         return NULL;
249     }
250     *pp_block = NULL;
251
252     if( p_block->i_buffer == 0 || p_block->p_buffer[0] == '\0' )
253     {
254         block_Release( p_block );
255         return NULL;
256     }
257
258     subpicture_updater_sys_t *p_spu_sys = malloc( sizeof(*p_spu_sys) );
259     if( !p_spu_sys )
260     {
261         block_Release( p_block );
262         return NULL;
263     }
264
265     subpicture_updater_t updater = {
266         .pf_validate = SubpictureValidate,
267         .pf_update   = SubpictureUpdate,
268         .pf_destroy  = SubpictureDestroy,
269         .p_sys       = p_spu_sys,
270     };
271     p_spu = decoder_NewSubpicture( p_dec, &updater );
272     if( !p_spu )
273     {
274         msg_Warn( p_dec, "can't get spu buffer" );
275         free( p_spu_sys );
276         block_Release( p_block );
277         return NULL;
278     }
279
280     p_spu_sys->p_img = NULL;
281     p_spu_sys->p_dec_sys = p_sys;
282     p_spu_sys->i_subs_len = p_block->i_buffer;
283     p_spu_sys->p_subs_data = malloc( p_block->i_buffer );
284     p_spu_sys->i_pts = p_block->i_pts;
285     if( !p_spu_sys->p_subs_data )
286     {
287         decoder_DeleteSubpicture( p_dec, p_spu );
288         block_Release( p_block );
289         return NULL;
290     }
291     memcpy( p_spu_sys->p_subs_data, p_block->p_buffer,
292             p_block->i_buffer );
293
294     p_spu->i_start = p_block->i_pts;
295     p_spu->i_stop = __MAX( p_sys->i_max_stop, p_block->i_pts + p_block->i_length );
296     p_spu->b_ephemer = true;
297     p_spu->b_absolute = true;
298
299     p_sys->i_max_stop = p_spu->i_stop;
300
301     vlc_mutex_lock( &libass_lock );
302     if( p_sys->p_track )
303     {
304         ass_process_chunk( p_sys->p_track, p_spu_sys->p_subs_data, p_spu_sys->i_subs_len,
305                            p_block->i_pts / 1000, p_block->i_length / 1000 );
306     }
307     vlc_mutex_unlock( &libass_lock );
308
309     DecSysHold( p_sys ); /* Keep a reference for the returned subpicture */
310
311     block_Release( p_block );
312
313     return p_spu;
314 }
315
316 /****************************************************************************
317  *
318  ****************************************************************************/
319 static int SubpictureValidate( subpicture_t *p_subpic,
320                                bool b_fmt_src, const video_format_t *p_fmt_src,
321                                bool b_fmt_dst, const video_format_t *p_fmt_dst,
322                                mtime_t i_ts )
323 {
324     decoder_sys_t *p_sys = p_subpic->updater.p_sys->p_dec_sys;
325     ass_handle_t *p_ass = p_sys->p_ass;
326
327     vlc_mutex_lock( &libass_lock );
328
329     /* FIXME why this mix of src/dst */
330     video_format_t fmt = *p_fmt_dst;
331     fmt.i_chroma         = VLC_CODEC_RGBA;
332     fmt.i_bits_per_pixel = 0;
333     fmt.i_width          =
334     fmt.i_visible_width  = p_fmt_src->i_width;
335     fmt.i_height         =
336     fmt.i_visible_height = p_fmt_src->i_height;
337     fmt.i_x_offset       =
338     fmt.i_y_offset       = 0;
339
340     if( b_fmt_src || b_fmt_dst )
341     {
342         ass_set_frame_size( p_ass->p_renderer, fmt.i_width, fmt.i_height );
343 #if defined( LIBASS_VERSION ) && LIBASS_VERSION >= 0x00907000
344         ass_set_aspect_ratio( p_ass->p_renderer, 1.0, 1.0 ); // TODO ?
345 #else
346         ass_set_aspect_ratio( p_ass->p_renderer, 1.0 ); // TODO ?
347 #endif
348         p_ass->fmt = fmt;
349     }
350
351     /* */
352     const mtime_t i_stream_date = p_subpic->updater.p_sys->i_pts + (i_ts - p_subpic->i_start);
353     int i_changed;
354     ASS_Image *p_img = ass_render_frame( p_ass->p_renderer, p_sys->p_track,
355                                          i_stream_date/1000, &i_changed );
356
357     if( !i_changed && !b_fmt_src && !b_fmt_dst &&
358         (p_img != NULL) == (p_subpic->p_region != NULL) )
359     {
360         vlc_mutex_unlock( &libass_lock );
361         return VLC_SUCCESS;
362     }
363     p_subpic->updater.p_sys->p_img = p_img;
364
365     /* The lock is released by SubpictureUpdate */
366     return VLC_EGENERIC;
367 }
368
369 static void SubpictureUpdate( subpicture_t *p_subpic,
370                               const video_format_t *p_fmt_src,
371                               const video_format_t *p_fmt_dst,
372                               mtime_t i_ts )
373 {
374     decoder_sys_t *p_sys = p_subpic->updater.p_sys->p_dec_sys;
375     ass_handle_t *p_ass = p_sys->p_ass;
376
377     video_format_t fmt = p_ass->fmt;
378     ASS_Image *p_img = p_subpic->updater.p_sys->p_img;
379     //vlc_assert_locked( &libass_lock );
380
381     /* */
382     p_subpic->i_original_picture_height = fmt.i_height;
383     p_subpic->i_original_picture_width = fmt.i_width;
384
385     /* XXX to improve efficiency we merge regions that are close minimizing
386      * the lost surface.
387      * libass tends to create a lot of small regions and thus spu engine
388      * reinstanciate a lot the scaler, and as we do not support subpel blending
389      * it looks ugly (text unaligned).
390      */
391     const int i_max_region = 4;
392     rectangle_t region[i_max_region];
393     const int i_region = BuildRegions( region, i_max_region, p_img, fmt.i_width, fmt.i_height );
394
395     if( i_region <= 0 )
396     {
397         vlc_mutex_unlock( &libass_lock );
398         return;
399     }
400
401     /* Allocate the regions and draw them */
402     subpicture_region_t *pp_region[i_max_region];
403     subpicture_region_t **pp_region_last = &p_subpic->p_region;
404
405     for( int i = 0; i < i_region; i++ )
406     {
407         subpicture_region_t *r;
408         video_format_t fmt_region;
409
410         /* */
411         fmt_region = fmt;
412         fmt_region.i_width =
413         fmt_region.i_visible_width  = region[i].x1 - region[i].x0;
414         fmt_region.i_height =
415         fmt_region.i_visible_height = region[i].y1 - region[i].y0;
416
417         pp_region[i] = r = subpicture_region_New( &fmt_region );
418         if( !r )
419             break;
420         r->i_x = region[i].x0;
421         r->i_y = region[i].y0;
422         r->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
423
424         /* */
425         RegionDraw( r, p_img );
426
427         /* */
428         *pp_region_last = r;
429         pp_region_last = &r->p_next;
430     }
431     vlc_mutex_unlock( &libass_lock );
432
433 }
434 static void SubpictureDestroy( subpicture_t *p_subpic )
435 {
436     subpicture_updater_sys_t *p_sys = p_subpic->updater.p_sys;
437
438     DecSysRelease( p_sys->p_dec_sys );
439     free( p_sys->p_subs_data );
440     free( p_sys );
441 }
442
443 static rectangle_t r_create( int x0, int y0, int x1, int y1 )
444 {
445     rectangle_t r = { x0, y0, x1, y1 };
446     return r;
447 }
448 static rectangle_t r_img( const ASS_Image *p_img )
449 {
450     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 );
451 }
452 static void r_add( rectangle_t *r, const rectangle_t *n )
453 {
454     r->x0 = __MIN( r->x0, n->x0 );
455     r->y0 = __MIN( r->y0, n->y0 );
456     r->x1 = __MAX( r->x1, n->x1 );
457     r->y1 = __MAX( r->y1, n->y1 );
458 }
459 static int r_surface( const rectangle_t *r )
460 {
461     return (r->x1-r->x0) * (r->y1-r->y0);
462 }
463 static bool r_overlap( const rectangle_t *a, const rectangle_t *b, int i_dx, int i_dy )
464 {
465     return  __MAX(a->x0-i_dx, b->x0) < __MIN( a->x1+i_dx, b->x1 ) &&
466             __MAX(a->y0-i_dy, b->y0) < __MIN( a->y1+i_dy, b->y1 );
467 }
468
469 static int BuildRegions( rectangle_t *p_region, int i_max_region, ASS_Image *p_img_list, int i_width, int i_height )
470 {
471     ASS_Image *p_tmp;
472     int i_count;
473
474 #ifdef DEBUG_REGION
475     int64_t i_ck_start = mdate();
476 #endif
477
478     for( p_tmp = p_img_list, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->next )
479         if( p_tmp->w > 0 && p_tmp->h > 0 )
480             i_count++;
481     if( i_count <= 0 )
482         return 0;
483
484     ASS_Image **pp_img = calloc( i_count, sizeof(*pp_img) );
485     if( !pp_img )
486         return 0;
487
488     for( p_tmp = p_img_list, i_count = 0; p_tmp != NULL; p_tmp = p_tmp->next )
489         if( p_tmp->w > 0 && p_tmp->h > 0 )
490             pp_img[i_count++] = p_tmp;
491
492     /* */
493     const int i_w_inc = __MAX( ( i_width + 49 ) / 50, 32 );
494     const int i_h_inc = __MAX( ( i_height + 99 ) / 100, 32 );
495     int i_maxh = i_w_inc;
496     int i_maxw = i_h_inc;
497     int i_region;
498     rectangle_t region[i_max_region+1];
499
500     i_region = 0;
501     for( int i_used = 0; i_used < i_count; )
502     {
503         int n;
504         for( n = 0; n < i_count; n++ )
505         {
506             if( pp_img[n] )
507                 break;
508         }
509         assert( i_region < i_max_region + 1 );
510         region[i_region++] = r_img( pp_img[n] );
511         pp_img[n] = NULL; i_used++;
512
513         bool b_ok;
514         do {
515             b_ok = false;
516             for( n = 0; n < i_count; n++ )
517             {
518                 ASS_Image *p_img = pp_img[n];
519                 if( !p_img )
520                     continue;
521                 rectangle_t r = r_img( p_img );
522
523                 int k;
524                 int i_best = -1;
525                 int i_best_s = INT_MAX;
526                 for( k = 0; k < i_region; k++ )
527                 {
528                     if( !r_overlap( &region[k], &r, i_maxw, i_maxh ) )
529                         continue;
530                     int s = r_surface( &r );
531                     if( s < i_best_s )
532                     {
533                         i_best_s = s;
534                         i_best = k;
535                     }
536                 }
537                 if( i_best >= 0 )
538                 {
539                     r_add( &region[i_best], &r );
540                     pp_img[n] = NULL; i_used++;
541                     b_ok = true;
542                 }
543             }
544         } while( b_ok );
545
546         if( i_region > i_max_region )
547         {
548             int i_best_i = -1;
549             int i_best_j = -1;
550             int i_best_ds = INT_MAX;
551
552             /* merge best */
553             for( int i = 0; i < i_region; i++ )
554             {
555                 for( int j = i+1; j < i_region; j++ )
556                 {
557                     rectangle_t n = region[i];
558                     r_add( &n, &region[j] );
559                     int ds = r_surface( &n ) - r_surface( &region[i] ) - r_surface( &region[j] );
560
561                     if( ds < i_best_ds )
562                     {
563                         i_best_i = i;
564                         i_best_j = j;
565                         i_best_ds = ds;
566                     }
567                 }
568             }
569 #ifdef DEBUG_REGION
570             msg_Err( p_spu, "Merging %d and %d", i_best_i, i_best_j );
571 #endif
572             r_add( &region[i_best_i], &region[i_best_j] );
573
574             if( i_best_j+1 < i_region )
575                 memmove( &region[i_best_j], &region[i_best_j+1], sizeof(*region) * ( i_region - (i_best_j+1)  ) );
576             i_region--;
577         }
578     }
579
580     /* */
581     for( int n = 0; n < i_region; n++ )
582         p_region[n] = region[n];
583
584 #ifdef DEBUG_REGION
585     int64_t i_ck_time = mdate() - i_ck_start;
586     msg_Err( p_spu, "ASS: %d objects merged into %d region in %d micros", i_count, i_region, (int)(i_ck_time) );
587 #endif
588
589     free( pp_img );
590
591     return i_region;
592 }
593
594 static void RegionDraw( subpicture_region_t *p_region, ASS_Image *p_img )
595 {
596     const plane_t *p = &p_region->p_picture->p[0];
597     const int i_x = p_region->i_x;
598     const int i_y = p_region->i_y;
599     const int i_width  = p_region->fmt.i_width;
600     const int i_height = p_region->fmt.i_height;
601
602     memset( p->p_pixels, 0x00, p->i_pitch * p->i_lines );
603     for( ; p_img != NULL; p_img = p_img->next )
604     {
605         if( p_img->dst_x < i_x || p_img->dst_x + p_img->w > i_x + i_width ||
606             p_img->dst_y < i_y || p_img->dst_y + p_img->h > i_y + i_height )
607             continue;
608
609         const unsigned r = (p_img->color >> 24)&0xff;
610         const unsigned g = (p_img->color >> 16)&0xff;
611         const unsigned b = (p_img->color >>  8)&0xff;
612         const unsigned a = (p_img->color      )&0xff;
613         int x, y;
614
615         for( y = 0; y < p_img->h; y++ )
616         {
617             for( x = 0; x < p_img->w; x++ )
618             {
619                 const unsigned alpha = p_img->bitmap[y*p_img->stride+x];
620                 const unsigned an = (255 - a) * alpha / 255;
621
622                 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)];
623                 const unsigned ao = p_rgba[3];
624
625                 /* Native endianness, but RGBA ordering */
626                 if( ao == 0 )
627                 {
628                     /* Optimized but the else{} will produce the same result */
629                     p_rgba[0] = r;
630                     p_rgba[1] = g;
631                     p_rgba[2] = b;
632                     p_rgba[3] = an;
633                 }
634                 else
635                 {
636                     p_rgba[3] = 255 - ( 255 - p_rgba[3] ) * ( 255 - an ) / 255;
637                     if( p_rgba[3] != 0 )
638                     {
639                         p_rgba[0] = ( p_rgba[0] * ao * (255-an) / 255 + r * an ) / p_rgba[3];
640                         p_rgba[1] = ( p_rgba[1] * ao * (255-an) / 255 + g * an ) / p_rgba[3];
641                         p_rgba[2] = ( p_rgba[2] * ao * (255-an) / 255 + b * an ) / p_rgba[3];
642                     }
643                 }
644             }
645         }
646     }
647
648 #ifdef DEBUG_REGION
649     /* XXX Draw a box for debug */
650 #define P(x,y) ((uint32_t*)&p->p_pixels[(y)*p->i_pitch + 4*(x)])
651     for( int y = 0; y < p->i_lines; y++ )
652         *P(0,y) = *P(p->i_visible_pitch/4-1,y) = 0xff000000;
653     for( int x = 0; x < p->i_visible_pitch; x++ )
654         *P(x/4,0) = *P(x/4,p->i_visible_lines-1) = 0xff000000;
655 #undef P
656 #endif
657 }
658
659 /* */
660 static ass_handle_t *AssHandleHold( decoder_t *p_dec )
661 {
662     vlc_mutex_lock( &libass_lock );
663
664     ass_handle_t *p_ass = NULL;
665     ASS_Library *p_library = NULL;
666     ASS_Renderer *p_renderer = NULL;
667     vlc_value_t val;
668
669     var_Create( p_dec->p_libvlc, "libass-handle", VLC_VAR_ADDRESS );
670     if( var_Get( p_dec->p_libvlc, "libass-handle", &val ) )
671         val.p_address = NULL;
672
673     if( val.p_address )
674     {
675         p_ass = val.p_address;
676
677         p_ass->i_refcount++;
678
679         vlc_mutex_unlock( &libass_lock );
680         return p_ass;
681     }
682
683     /* */
684     p_ass = malloc( sizeof(*p_ass) );
685     if( !p_ass )
686         goto error;
687
688     /* */
689     p_ass->p_libvlc = VLC_OBJECT(p_dec->p_libvlc);
690     p_ass->i_refcount = 1;
691
692     /* Create libass library */
693     p_ass->p_library = p_library = ass_library_init();
694     if( !p_library )
695         goto error;
696
697     /* load attachments */
698     input_attachment_t  **pp_attachments;
699     int                   i_attachments;
700
701     if( decoder_GetInputAttachments( p_dec, &pp_attachments, &i_attachments ))
702     {
703         i_attachments = 0;
704         pp_attachments = NULL;
705     }
706     for( int k = 0; k < i_attachments; k++ )
707     {
708         input_attachment_t *p_attach = pp_attachments[k];
709
710         if( !strcasecmp( p_attach->psz_mime, "application/x-truetype-font" ) )
711         {
712             msg_Dbg( p_dec, "adding embedded font %s", p_attach->psz_name );
713
714             ass_add_font( p_ass->p_library, p_attach->psz_name, p_attach->p_data, p_attach->i_data );
715         }
716         vlc_input_attachment_Delete( p_attach );
717     }
718     free( pp_attachments );
719
720     ass_set_extract_fonts( p_library, true );
721     ass_set_style_overrides( p_library, NULL );
722
723     /* Create the renderer */
724     p_ass->p_renderer = p_renderer = ass_renderer_init( p_library );
725     if( !p_renderer )
726         goto error;
727
728     ass_set_use_margins( p_renderer, false);
729     //if( false )
730     //    ass_set_margins( p_renderer, int t, int b, int l, int r);
731     ass_set_hinting( p_renderer, ASS_HINTING_LIGHT );
732     ass_set_font_scale( p_renderer, 1.0 );
733     ass_set_line_spacing( p_renderer, 0.0 );
734
735     const char *psz_font = NULL; /* We don't ship a default font with VLC */
736     const char *psz_family = "Arial"; /* Use Arial if we can't find anything more suitable */
737
738 #ifdef HAVE_FONTCONFIG
739 #if defined(WIN32)
740     dialog_progress_bar_t *p_dialog = dialog_ProgressCreate( p_dec,
741         _("Building font cache"),
742         _( "Please wait while your font cache is rebuilt.\n"
743         "This should take less than a minute." ), NULL );
744     if( p_dialog )
745         dialog_ProgressSet( p_dialog, NULL, 0.2 );
746 #endif
747 #if defined( LIBASS_VERSION ) && LIBASS_VERSION >= 0x00907000
748     ass_set_fonts( p_renderer, psz_font, psz_family, true, NULL, 1 );  // setup default font/family
749 #else
750     ass_set_fonts( p_renderer, psz_font, psz_family );  // setup default font/family
751 #endif
752 #ifdef WIN32
753     if( p_dialog )
754     {
755         dialog_ProgressSet( p_dialog, NULL, 1.0 );
756         dialog_ProgressDestroy( p_dialog );
757         p_dialog = NULL;
758     }
759 #endif
760 #else
761     /* FIXME you HAVE to give him a font if no fontconfig */
762 #if defined( LIBASS_VERSION ) && LIBASS_VERSION >= 0x00907000
763     ass_set_fonts( p_renderer, psz_font, psz_family, false, NULL, 1 );
764 #else
765     ass_set_fonts_nofc( p_renderer, psz_font, psz_family );
766 #endif
767 #endif
768     memset( &p_ass->fmt, 0, sizeof(p_ass->fmt) );
769
770     /* */
771     val.p_address = p_ass;
772     var_Set( p_dec->p_libvlc, "libass-handle", val );
773
774     /* */
775     vlc_mutex_unlock( &libass_lock );
776     return p_ass;
777
778 error:
779     if( p_renderer )
780         ass_renderer_done( p_renderer );
781     if( p_library )
782         ass_library_done( p_library );
783
784     msg_Warn( p_dec, "Libass creation failed" );
785
786     free( p_ass );
787     vlc_mutex_unlock( &libass_lock );
788     return NULL;
789 }
790 static void AssHandleRelease( ass_handle_t *p_ass )
791 {
792     vlc_mutex_lock( &libass_lock );
793     p_ass->i_refcount--;
794     if( p_ass->i_refcount > 0 )
795     {
796         vlc_mutex_unlock( &libass_lock );
797         return;
798     }
799
800     ass_renderer_done( p_ass->p_renderer );
801     ass_library_done( p_ass->p_library );
802
803     vlc_value_t val;
804     val.p_address = NULL;
805     var_Set( p_ass->p_libvlc, "libass-handle", val );
806
807     vlc_mutex_unlock( &libass_lock );
808     free( p_ass );
809 }