]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
sftp: change item b_net
[vlc] / src / video_output / vout_subpictures.c
1 /*****************************************************************************
2  * vout_subpictures.c : subpicture management functions
3  *****************************************************************************
4  * Copyright (C) 2000-2007 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <assert.h>
34 #include <limits.h>
35
36 #include <vlc_common.h>
37 #include <vlc_modules.h>
38 #include <vlc_input.h>
39 #include <vlc_vout.h>
40 #include <vlc_filter.h>
41 #include <vlc_spu.h>
42
43 #include "../libvlc.h"
44 #include "vout_internal.h"
45 #include "../misc/subpicture.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50
51 /* Number of simultaneous subpictures */
52 #define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
53
54 /* */
55 typedef struct {
56     subpicture_t *subpicture;
57     bool          reject;
58 } spu_heap_entry_t;
59
60 typedef struct {
61     spu_heap_entry_t entry[VOUT_MAX_SUBPICTURES];
62 } spu_heap_t;
63
64 struct spu_private_t {
65     vlc_mutex_t  lock;            /* lock to protect all followings fields */
66     vlc_object_t *input;
67
68     spu_heap_t   heap;
69
70     int channel;             /**< number of subpicture channels registered */
71     filter_t *text;                              /**< text renderer module */
72     filter_t *scale_yuvp;                     /**< scaling module for YUVP */
73     filter_t *scale;                    /**< scaling module (all but YUVP) */
74     bool force_crop;                     /**< force cropping of subpicture */
75     struct {
76         int x;
77         int y;
78         int width;
79         int height;
80     } crop;                                                  /**< cropping */
81
82     int     margin;                    /**< force position of a subpicture */
83     bool    force_palette;                /**< force palette of subpicture */
84     uint8_t palette[4][4];                             /**< forced palette */
85
86     /* Subpiture filters */
87     char           *source_chain_update;
88     vlc_mutex_t    source_chain_lock;
89     filter_chain_t *source_chain;
90     char           *filter_chain_update;
91     vlc_mutex_t    filter_chain_lock;
92     filter_chain_t *filter_chain;
93
94     /* */
95     mtime_t last_sort_date;
96 };
97
98 /*****************************************************************************
99  * heap managment
100  *****************************************************************************/
101 static void SpuHeapInit(spu_heap_t *heap)
102 {
103     for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
104         spu_heap_entry_t *e = &heap->entry[i];
105
106         e->subpicture = NULL;
107         e->reject     = false;
108     }
109 }
110
111 static int SpuHeapPush(spu_heap_t *heap, subpicture_t *subpic)
112 {
113     for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
114         spu_heap_entry_t *e = &heap->entry[i];
115
116         if (e->subpicture)
117             continue;
118
119         e->subpicture = subpic;
120         e->reject     = false;
121         return VLC_SUCCESS;
122     }
123     return VLC_EGENERIC;
124 }
125
126 static void SpuHeapDeleteAt(spu_heap_t *heap, int index)
127 {
128     spu_heap_entry_t *e = &heap->entry[index];
129
130     if (e->subpicture)
131         subpicture_Delete(e->subpicture);
132
133     e->subpicture = NULL;
134 }
135
136 static int SpuHeapDeleteSubpicture(spu_heap_t *heap, subpicture_t *subpic)
137 {
138     for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
139         spu_heap_entry_t *e = &heap->entry[i];
140
141         if (e->subpicture != subpic)
142             continue;
143
144         SpuHeapDeleteAt(heap, i);
145         return VLC_SUCCESS;
146     }
147     return VLC_EGENERIC;
148 }
149
150 static void SpuHeapClean(spu_heap_t *heap)
151 {
152     for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
153         spu_heap_entry_t *e = &heap->entry[i];
154         if (e->subpicture)
155             subpicture_Delete(e->subpicture);
156     }
157 }
158
159 static void FilterRelease(filter_t *filter)
160 {
161     if (filter->p_module)
162         module_unneed(filter, filter->p_module);
163     vlc_object_release(filter);
164 }
165
166 static picture_t *spu_new_video_buffer(filter_t *filter)
167 {
168     const video_format_t *fmt = &filter->fmt_out.video;
169
170     return picture_NewFromFormat(fmt);
171 }
172
173 static int spu_get_attachments(filter_t *filter,
174                                input_attachment_t ***attachment_ptr,
175                                int *attachment_count)
176 {
177     spu_t *spu = filter->owner.sys;
178
179     int ret = VLC_EGENERIC;
180     if (spu->p->input)
181         ret = input_Control((input_thread_t*)spu->p->input,
182                             INPUT_GET_ATTACHMENTS,
183                             attachment_ptr, attachment_count);
184     return ret;
185 }
186
187 static filter_t *SpuRenderCreateAndLoadText(spu_t *spu)
188 {
189     filter_t *text = vlc_custom_create(spu, sizeof(*text), "spu text");
190     if (!text)
191         return NULL;
192
193     text->owner.sys = spu;
194
195     es_format_Init(&text->fmt_in, VIDEO_ES, 0);
196
197     es_format_Init(&text->fmt_out, VIDEO_ES, 0);
198     text->fmt_out.video.i_width          =
199     text->fmt_out.video.i_visible_width  = 32;
200     text->fmt_out.video.i_height         =
201     text->fmt_out.video.i_visible_height = 32;
202
203     text->pf_get_attachments = spu_get_attachments;
204
205     text->p_module = module_need(text, "text renderer", "$text-renderer", false);
206
207     /* Create a few variables used for enhanced text rendering */
208     var_Create(text, "spu-elapsed",   VLC_VAR_TIME);
209     var_Create(text, "text-rerender", VLC_VAR_BOOL);
210
211     return text;
212 }
213
214 static filter_t *SpuRenderCreateAndLoadScale(vlc_object_t *object,
215                                              vlc_fourcc_t src_chroma,
216                                              vlc_fourcc_t dst_chroma,
217                                              bool require_resize)
218 {
219     filter_t *scale = vlc_custom_create(object, sizeof(*scale), "scale");
220     if (!scale)
221         return NULL;
222
223     es_format_Init(&scale->fmt_in, VIDEO_ES, 0);
224     scale->fmt_in.video.i_chroma = src_chroma;
225     scale->fmt_in.video.i_width =
226     scale->fmt_in.video.i_visible_width =
227     scale->fmt_in.video.i_height =
228     scale->fmt_in.video.i_visible_height = 32;
229
230     es_format_Init(&scale->fmt_out, VIDEO_ES, 0);
231     scale->fmt_out.video.i_chroma = dst_chroma;
232     scale->fmt_out.video.i_width =
233     scale->fmt_out.video.i_visible_width =
234     scale->fmt_out.video.i_height =
235     scale->fmt_out.video.i_visible_height = require_resize ? 16 : 32;
236
237     scale->owner.video.buffer_new = spu_new_video_buffer;
238
239     scale->p_module = module_need(scale, "video filter2", NULL, false);
240
241     return scale;
242 }
243
244 static void SpuRenderText(spu_t *spu, bool *rerender_text,
245                           subpicture_region_t *region,
246                           const vlc_fourcc_t *chroma_list,
247                           mtime_t elapsed_time)
248 {
249     filter_t *text = spu->p->text;
250
251     assert(region->fmt.i_chroma == VLC_CODEC_TEXT);
252
253     if (!text || !text->p_module)
254         return;
255
256     /* Setup 3 variables which can be used to render
257      * time-dependent text (and effects). The first indicates
258      * the total amount of time the text will be on screen,
259      * the second the amount of time it has already been on
260      * screen (can be a negative value as text is layed out
261      * before it is rendered) and the third is a feedback
262      * variable from the renderer - if the renderer sets it
263      * then this particular text is time-dependent, eg. the
264      * visual progress bar inside the text in karaoke and the
265      * text needs to be rendered multiple times in order for
266      * the effect to work - we therefore need to return the
267      * region to its original state at the end of the loop,
268      * instead of leaving it in YUVA or YUVP.
269      * Any renderer which is unaware of how to render
270      * time-dependent text can happily ignore the variables
271      * and render the text the same as usual - it should at
272      * least show up on screen, but the effect won't change
273      * the text over time.
274      */
275     var_SetTime(text, "spu-elapsed", elapsed_time);
276     var_SetBool(text, "text-rerender", false);
277
278     if (text->pf_render_html && region->psz_html)
279         text->pf_render_html(text, region, region, chroma_list);
280     else if (text->pf_render_text)
281         text->pf_render_text(text, region, region, chroma_list);
282     *rerender_text = var_GetBool(text, "text-rerender");
283 }
284
285 /**
286  * A few scale functions helpers.
287  */
288
289 #define SCALE_UNIT (1000)
290 typedef struct {
291     int w;
292     int h;
293 } spu_scale_t;
294
295 static spu_scale_t spu_scale_create(int w, int h)
296 {
297     spu_scale_t s = { .w = w, .h = h };
298     if (s.w <= 0)
299         s.w = SCALE_UNIT;
300     if (s.h <= 0)
301         s.h = SCALE_UNIT;
302     return s;
303 }
304 static spu_scale_t spu_scale_unit(void)
305 {
306     return spu_scale_create(SCALE_UNIT, SCALE_UNIT);
307 }
308 static spu_scale_t spu_scale_createq(int64_t wn, int64_t wd, int64_t hn, int64_t hd)
309 {
310     return spu_scale_create(wn * SCALE_UNIT / wd,
311                             hn * SCALE_UNIT / hd);
312 }
313 static int spu_scale_w(int v, const spu_scale_t s)
314 {
315     return v * s.w / SCALE_UNIT;
316 }
317 static int spu_scale_h(int v, const spu_scale_t s)
318 {
319     return v * s.h / SCALE_UNIT;
320 }
321 static int spu_invscale_w(int v, const spu_scale_t s)
322 {
323     return v * SCALE_UNIT / s.w;
324 }
325 static int spu_invscale_h(int v, const spu_scale_t s)
326 {
327     return v * SCALE_UNIT / s.h;
328 }
329
330 /**
331  * A few area functions helpers
332  */
333 typedef struct {
334     int x;
335     int y;
336     int width;
337     int height;
338
339     spu_scale_t scale;
340 } spu_area_t;
341
342 static spu_area_t spu_area_create(int x, int y, int w, int h, spu_scale_t s)
343 {
344     spu_area_t a = { .x = x, .y = y, .width = w, .height = h, .scale = s };
345     return a;
346 }
347 static spu_area_t spu_area_scaled(spu_area_t a)
348 {
349     if (a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT)
350         return a;
351
352     a.x      = spu_scale_w(a.x,      a.scale);
353     a.y      = spu_scale_h(a.y,      a.scale);
354     a.width  = spu_scale_w(a.width,  a.scale);
355     a.height = spu_scale_h(a.height, a.scale);
356
357     a.scale = spu_scale_unit();
358     return a;
359 }
360 static spu_area_t spu_area_unscaled(spu_area_t a, spu_scale_t s)
361 {
362     if (a.scale.w == s.w && a.scale.h == s.h)
363         return a;
364
365     a = spu_area_scaled(a);
366
367     a.x      = spu_invscale_w(a.x,      s);
368     a.y      = spu_invscale_h(a.y,      s);
369     a.width  = spu_invscale_w(a.width,  s);
370     a.height = spu_invscale_h(a.height, s);
371
372     a.scale = s;
373     return a;
374 }
375 static bool spu_area_overlap(spu_area_t a, spu_area_t b)
376 {
377     const int dx = 0;
378     const int dy = 0;
379
380     a = spu_area_scaled(a);
381     b = spu_area_scaled(b);
382
383     return __MAX(a.x - dx, b.x) < __MIN(a.x + a.width  + dx, b.x + b.width ) &&
384            __MAX(a.y - dy, b.y) < __MIN(a.y + a.height + dy, b.y + b.height);
385 }
386
387 /**
388  * Avoid area overlapping
389  */
390 static void SpuAreaFixOverlap(spu_area_t *dst,
391                               const spu_area_t *sub_array, int sub_count, int align)
392 {
393     spu_area_t a = spu_area_scaled(*dst);
394     bool is_moved = false;
395     bool is_ok;
396
397     /* Check for overlap
398      * XXX It is not fast O(n^2) but we should not have a lot of region */
399     do {
400         is_ok = true;
401         for (int i = 0; i < sub_count; i++) {
402             spu_area_t sub = spu_area_scaled(sub_array[i]);
403
404             if (!spu_area_overlap(a, sub))
405                 continue;
406
407             if (align & SUBPICTURE_ALIGN_TOP) {
408                 /* We go down */
409                 int i_y = sub.y + sub.height;
410                 a.y = i_y;
411                 is_moved = true;
412             } else if (align & SUBPICTURE_ALIGN_BOTTOM) {
413                 /* We go up */
414                 int i_y = sub.y - a.height;
415                 a.y = i_y;
416                 is_moved = true;
417             } else {
418                 /* TODO what to do in this case? */
419                 //fprintf(stderr, "Overlap with unsupported alignment\n");
420                 break;
421             }
422
423             is_ok = false;
424             break;
425         }
426     } while (!is_ok);
427
428     if (is_moved)
429         *dst = spu_area_unscaled(a, dst->scale);
430 }
431
432
433 static void SpuAreaFitInside(spu_area_t *area, const spu_area_t *boundary)
434 {
435     spu_area_t a = spu_area_scaled(*area);
436
437     const int i_error_x = (a.x + a.width) - boundary->width;
438     if (i_error_x > 0)
439         a.x -= i_error_x;
440     if (a.x < 0)
441         a.x = 0;
442
443     const int i_error_y = (a.y + a.height) - boundary->height;
444     if (i_error_y > 0)
445         a.y -= i_error_y;
446     if (a.y < 0)
447         a.y = 0;
448
449     *area = spu_area_unscaled(a, area->scale);
450 }
451
452 /**
453  * Place a region
454  */
455 static void SpuRegionPlace(int *x, int *y,
456                            const subpicture_t *subpic,
457                            const subpicture_region_t *region)
458 {
459     assert(region->i_x != INT_MAX && region->i_y != INT_MAX);
460     if (subpic->b_absolute) {
461         *x = region->i_x;
462         *y = region->i_y;
463     } else {
464         if (region->i_align & SUBPICTURE_ALIGN_TOP)
465             *y = region->i_y;
466         else if (region->i_align & SUBPICTURE_ALIGN_BOTTOM)
467             *y = subpic->i_original_picture_height - region->fmt.i_visible_height - region->i_y;
468         else
469             *y = subpic->i_original_picture_height / 2 - region->fmt.i_visible_height / 2;
470
471         if (region->i_align & SUBPICTURE_ALIGN_LEFT)
472             *x = region->i_x;
473         else if (region->i_align & SUBPICTURE_ALIGN_RIGHT)
474             *x = subpic->i_original_picture_width - region->fmt.i_visible_width - region->i_x;
475         else
476             *x = subpic->i_original_picture_width / 2 - region->fmt.i_visible_width / 2;
477     }
478 }
479
480 /**
481  * This function compares two 64 bits integers.
482  * It can be used by qsort.
483  */
484 static int IntegerCmp(int64_t i0, int64_t i1)
485 {
486     return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
487 }
488 /**
489  * This function compares 2 subpictures using the following properties
490  * (ordered by priority)
491  * 1. absolute positionning
492  * 2. start time
493  * 3. creation order (per channel)
494  *
495  * It can be used by qsort.
496  *
497  * XXX spu_RenderSubpictures depends heavily on this order.
498  */
499 static int SubpictureCmp(const void *s0, const void *s1)
500 {
501     subpicture_t *subpic0 = *(subpicture_t**)s0;
502     subpicture_t *subpic1 = *(subpicture_t**)s1;
503     int r;
504
505     r = IntegerCmp(!subpic0->b_absolute, !subpic1->b_absolute);
506     if (!r)
507         r = IntegerCmp(subpic0->i_start, subpic1->i_start);
508     if (!r)
509         r = IntegerCmp(subpic0->i_channel, subpic1->i_channel);
510     if (!r)
511         r = IntegerCmp(subpic0->i_order, subpic1->i_order);
512     return r;
513 }
514
515 /*****************************************************************************
516  * SpuSelectSubpictures: find the subpictures to display
517  *****************************************************************************
518  * This function parses all subpictures and decides which ones need to be
519  * displayed. If no picture has been selected, display_date will depend on
520  * the subpicture.
521  * We also check for ephemer DVD subpictures (subpictures that have
522  * to be removed if a newer one is available), which makes it a lot
523  * more difficult to guess if a subpicture has to be rendered or not.
524  *****************************************************************************/
525 static void SpuSelectSubpictures(spu_t *spu,
526                                  unsigned int *subpicture_count,
527                                  subpicture_t **subpicture_array,
528                                  mtime_t render_subtitle_date,
529                                  mtime_t render_osd_date,
530                                  bool ignore_osd)
531 {
532     spu_private_t *sys = spu->p;
533
534     /* */
535     *subpicture_count = 0;
536
537     /* Create a list of channels */
538     int channel[VOUT_MAX_SUBPICTURES];
539     int channel_count = 0;
540
541     for (int index = 0; index < VOUT_MAX_SUBPICTURES; index++) {
542         spu_heap_entry_t *entry = &sys->heap.entry[index];
543         if (!entry->subpicture || entry->reject)
544             continue;
545         const int i_channel = entry->subpicture->i_channel;
546         int i;
547         for (i = 0; i < channel_count; i++) {
548             if (channel[i] == i_channel)
549                 break;
550         }
551         if (channel_count <= i)
552             channel[channel_count++] = i_channel;
553     }
554
555     /* Fill up the subpicture_array arrays with relevent pictures */
556     for (int i = 0; i < channel_count; i++) {
557         subpicture_t *available_subpic[VOUT_MAX_SUBPICTURES];
558         bool         is_available_late[VOUT_MAX_SUBPICTURES];
559         int          available_count = 0;
560
561         mtime_t      start_date = render_subtitle_date;
562         mtime_t      ephemer_subtitle_date = 0;
563         mtime_t      ephemer_osd_date = 0;
564         int64_t      ephemer_subtitle_order = INT64_MIN;
565         int64_t      ephemer_system_order = INT64_MIN;
566
567         /* Select available pictures */
568         for (int index = 0; index < VOUT_MAX_SUBPICTURES; index++) {
569             spu_heap_entry_t *entry = &sys->heap.entry[index];
570             subpicture_t *current = entry->subpicture;
571             bool is_stop_valid;
572             bool is_late;
573
574             if (!current || entry->reject) {
575                 if (entry->reject)
576                     SpuHeapDeleteAt(&sys->heap, index);
577                 continue;
578             }
579
580             if (current->i_channel != channel[i] ||
581                (ignore_osd && !current->b_subtitle))
582                 continue;
583
584             const mtime_t render_date = current->b_subtitle ? render_subtitle_date : render_osd_date;
585             if (render_date &&
586                 render_date < current->i_start) {
587                 /* Too early, come back next monday */
588                 continue;
589             }
590
591             mtime_t *ephemer_date_ptr  = current->b_subtitle ? &ephemer_subtitle_date  : &ephemer_osd_date;
592             int64_t *ephemer_order_ptr = current->b_subtitle ? &ephemer_subtitle_order : &ephemer_system_order;
593             if (current->i_start >= *ephemer_date_ptr) {
594                 *ephemer_date_ptr = current->i_start;
595                 if (current->i_order > *ephemer_order_ptr)
596                     *ephemer_order_ptr = current->i_order;
597             }
598
599             is_stop_valid = !current->b_ephemer || current->i_stop > current->i_start;
600
601             is_late = is_stop_valid && current->i_stop <= render_date;
602
603             /* start_date will be used for correct automatic overlap support
604              * in case picture that should not be displayed anymore (display_time)
605              * overlap with a picture to be displayed (current->i_start)  */
606             if (current->b_subtitle && !is_late && !current->b_ephemer)
607                 start_date = current->i_start;
608
609             /* */
610             available_subpic[available_count] = current;
611             is_available_late[available_count] = is_late;
612             available_count++;
613         }
614
615         /* Only forced old picture display at the transition */
616         if (start_date < sys->last_sort_date)
617             start_date = sys->last_sort_date;
618         if (start_date <= 0)
619             start_date = INT64_MAX;
620
621         /* Select pictures to be displayed */
622         for (int index = 0; index < available_count; index++) {
623             subpicture_t *current = available_subpic[index];
624             bool is_late = is_available_late[index];
625
626             const mtime_t stop_date = current->b_subtitle ? __MAX(start_date, sys->last_sort_date) : render_osd_date;
627             const mtime_t ephemer_date  = current->b_subtitle ? ephemer_subtitle_date  : ephemer_osd_date;
628             const int64_t ephemer_order = current->b_subtitle ? ephemer_subtitle_order : ephemer_system_order;
629
630             /* Destroy late and obsolete ephemer subpictures */
631             bool is_rejeted = is_late && current->i_stop <= stop_date;
632             if (current->b_ephemer) {
633                 if (current->i_start < ephemer_date)
634                     is_rejeted = true;
635                 else if (current->i_start == ephemer_date &&
636                          current->i_order < ephemer_order)
637                     is_rejeted = true;
638             }
639
640             if (is_rejeted)
641                 SpuHeapDeleteSubpicture(&sys->heap, current);
642             else
643                 subpicture_array[(*subpicture_count)++] = current;
644         }
645     }
646
647     sys->last_sort_date = render_subtitle_date;
648 }
649
650
651
652 /**
653  * It will transform the provided region into another region suitable for rendering.
654  */
655 static void SpuRenderRegion(spu_t *spu,
656                             subpicture_region_t **dst_ptr, spu_area_t *dst_area,
657                             subpicture_t *subpic, subpicture_region_t *region,
658                             const spu_scale_t scale_size,
659                             const vlc_fourcc_t *chroma_list,
660                             const video_format_t *fmt,
661                             const spu_area_t *subtitle_area, int subtitle_area_count,
662                             mtime_t render_date)
663 {
664     spu_private_t *sys = spu->p;
665
666     video_format_t fmt_original = region->fmt;
667     bool restore_text = false;
668     int x_offset;
669     int y_offset;
670
671     video_format_t region_fmt;
672     picture_t *region_picture;
673
674     /* Invalidate area by default */
675     *dst_area = spu_area_create(0,0, 0,0, scale_size);
676     *dst_ptr  = NULL;
677
678     /* Render text region */
679     if (region->fmt.i_chroma == VLC_CODEC_TEXT) {
680         SpuRenderText(spu, &restore_text, region,
681                       chroma_list,
682                       render_date - subpic->i_start);
683
684         /* Check if the rendering has failed ... */
685         if (region->fmt.i_chroma == VLC_CODEC_TEXT)
686             goto exit;
687     }
688
689     /* Force palette if requested
690      * FIXME b_force_palette and force_crop are applied to all subpictures using palette
691      * instead of only the right one (being the dvd spu).
692      */
693     const bool using_palette = region->fmt.i_chroma == VLC_CODEC_YUVP;
694     const bool force_palette = using_palette && sys->force_palette;
695     const bool force_crop    = force_palette && sys->force_crop;
696     bool changed_palette     = false;
697
698     /* Compute the margin which is expressed in destination pixel unit
699      * The margin is applied only to subtitle and when no forced crop is
700      * requested (dvd menu) */
701     int y_margin = 0;
702     if (!force_crop && subpic->b_subtitle)
703         y_margin = spu_invscale_h(sys->margin, scale_size);
704
705     /* Place the picture
706      * We compute the position in the rendered size */
707     SpuRegionPlace(&x_offset, &y_offset,
708                    subpic, region);
709
710     /* Save this position for subtitle overlap support
711      * it is really important that there are given without scale_size applied */
712     *dst_area = spu_area_create(x_offset, y_offset,
713                                 region->fmt.i_visible_width,
714                                 region->fmt.i_visible_height,
715                                 scale_size);
716
717     /* Handle overlapping subtitles when possible */
718     if (subpic->b_subtitle && !subpic->b_absolute)
719         SpuAreaFixOverlap(dst_area, subtitle_area, subtitle_area_count,
720                           region->i_align);
721
722     /* we copy the area: for the subtitle overlap support we want
723      * to only save the area without margin applied */
724     spu_area_t restrained = *dst_area;
725
726     /* apply margin to subtitles and correct if they go over the picture edge */
727     if (subpic->b_subtitle)
728         restrained.y -= y_margin;
729
730     spu_area_t display = spu_area_create(0, 0, fmt->i_visible_width,
731                                          fmt->i_visible_height,
732                                          spu_scale_unit());
733     //fprintf("
734     SpuAreaFitInside(&restrained, &display);
735
736     /* Fix the position for the current scale_size */
737     x_offset = spu_scale_w(restrained.x, restrained.scale);
738     y_offset = spu_scale_h(restrained.y, restrained.scale);
739
740     /* */
741     if (force_palette) {
742         video_palette_t *old_palette = region->fmt.p_palette;
743         video_palette_t new_palette;
744
745         /* We suppose DVD palette here */
746         new_palette.i_entries = 4;
747         for (int i = 0; i < 4; i++)
748             for (int j = 0; j < 4; j++)
749                 new_palette.palette[i][j] = sys->palette[i][j];
750
751         if (old_palette->i_entries == new_palette.i_entries) {
752             for (int i = 0; i < old_palette->i_entries; i++)
753                 for (int j = 0; j < 4; j++)
754                     changed_palette |= old_palette->palette[i][j] != new_palette.palette[i][j];
755         } else {
756             changed_palette = true;
757         }
758         *old_palette = new_palette;
759     }
760
761     /* */
762     region_fmt = region->fmt;
763     region_picture = region->p_picture;
764
765     bool convert_chroma = true;
766     for (int i = 0; chroma_list[i] && convert_chroma; i++) {
767         if (region_fmt.i_chroma == chroma_list[i])
768             convert_chroma = false;
769     }
770
771     /* Scale from rendered size to destination size */
772     if (sys->scale && sys->scale->p_module &&
773         (!using_palette || (sys->scale_yuvp && sys->scale_yuvp->p_module)) &&
774         (scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT ||
775         using_palette || convert_chroma)) {
776         const unsigned dst_width  = spu_scale_w(region->fmt.i_visible_width,  scale_size);
777         const unsigned dst_height = spu_scale_h(region->fmt.i_visible_height, scale_size);
778
779         /* Destroy the cache if unusable */
780         if (region->p_private) {
781             subpicture_region_private_t *private = region->p_private;
782             bool is_changed = false;
783
784             /* Check resize changes */
785             if (dst_width  != private->fmt.i_visible_width ||
786                 dst_height != private->fmt.i_visible_height)
787                 is_changed = true;
788
789             /* Check forced palette changes */
790             if (changed_palette)
791                 is_changed = true;
792
793             if (convert_chroma && private->fmt.i_chroma != chroma_list[0])
794                 is_changed = true;
795
796             if (is_changed) {
797                 subpicture_region_private_Delete(private);
798                 region->p_private = NULL;
799             }
800         }
801
802         /* Scale if needed into cache */
803         if (!region->p_private && dst_width > 0 && dst_height > 0) {
804             filter_t *scale = sys->scale;
805
806             picture_t *picture = region->p_picture;
807             picture_Hold(picture);
808
809             /* Convert YUVP to YUVA/RGBA first for better scaling quality */
810             if (using_palette) {
811                 filter_t *scale_yuvp = sys->scale_yuvp;
812
813                 scale_yuvp->fmt_in.video = region->fmt;
814
815                 scale_yuvp->fmt_out.video = region->fmt;
816                 scale_yuvp->fmt_out.video.i_chroma = chroma_list[0];
817
818                 picture = scale_yuvp->pf_video_filter(scale_yuvp, picture);
819                 if (!picture) {
820                     /* Well we will try conversion+scaling */
821                     msg_Warn(spu, "%4.4s to %4.4s conversion failed",
822                              (const char*)&scale_yuvp->fmt_in.video.i_chroma,
823                              (const char*)&scale_yuvp->fmt_out.video.i_chroma);
824                 }
825             }
826
827             /* Conversion(except from YUVP)/Scaling */
828             if (picture &&
829                 (picture->format.i_visible_width  != dst_width ||
830                  picture->format.i_visible_height != dst_height ||
831                  (convert_chroma && !using_palette)))
832             {
833                 scale->fmt_in.video  = picture->format;
834                 scale->fmt_out.video = picture->format;
835                 if (using_palette)
836                     scale->fmt_in.video.i_chroma = chroma_list[0];
837                 if (convert_chroma)
838                     scale->fmt_out.i_codec        =
839                     scale->fmt_out.video.i_chroma = chroma_list[0];
840
841                 scale->fmt_out.video.i_width  = dst_width;
842                 scale->fmt_out.video.i_height = dst_height;
843
844                 scale->fmt_out.video.i_visible_width =
845                     spu_scale_w(region->fmt.i_visible_width, scale_size);
846                 scale->fmt_out.video.i_visible_height =
847                     spu_scale_h(region->fmt.i_visible_height, scale_size);
848
849                 picture = scale->pf_video_filter(scale, picture);
850                 if (!picture)
851                     msg_Err(spu, "scaling failed");
852             }
853
854             /* */
855             if (picture) {
856                 region->p_private = subpicture_region_private_New(&picture->format);
857                 if (region->p_private) {
858                     region->p_private->p_picture = picture;
859                     if (!region->p_private->p_picture) {
860                         subpicture_region_private_Delete(region->p_private);
861                         region->p_private = NULL;
862                     }
863                 } else {
864                     picture_Release(picture);
865                 }
866             }
867         }
868
869         /* And use the scaled picture */
870         if (region->p_private) {
871             region_fmt     = region->p_private->fmt;
872             region_picture = region->p_private->p_picture;
873         }
874     }
875
876     /* Force cropping if requested */
877     if (force_crop) {
878         int crop_x     = spu_scale_w(sys->crop.x,     scale_size);
879         int crop_y     = spu_scale_h(sys->crop.y,     scale_size);
880         int crop_width = spu_scale_w(sys->crop.width, scale_size);
881         int crop_height= spu_scale_h(sys->crop.height,scale_size);
882
883         /* Find the intersection */
884         if (crop_x + crop_width <= x_offset ||
885             x_offset + (int)region_fmt.i_visible_width  < crop_x ||
886             crop_y + crop_height <= y_offset ||
887             y_offset + (int)region_fmt.i_visible_height < crop_y) {
888             /* No intersection */
889             region_fmt.i_visible_width  =
890             region_fmt.i_visible_height = 0;
891         } else {
892             int x, y, x_end, y_end;
893             x = __MAX(crop_x, x_offset);
894             y = __MAX(crop_y, y_offset);
895             x_end = __MIN(crop_x + crop_width,
896                           x_offset + (int)region_fmt.i_visible_width);
897             y_end = __MIN(crop_y + crop_height,
898                           y_offset + (int)region_fmt.i_visible_height);
899
900             region_fmt.i_x_offset       = x - x_offset;
901             region_fmt.i_y_offset       = y - y_offset;
902             region_fmt.i_visible_width  = x_end - x;
903             region_fmt.i_visible_height = y_end - y;
904
905             x_offset = __MAX(x, 0);
906             y_offset = __MAX(y, 0);
907         }
908     }
909
910     subpicture_region_t *dst = *dst_ptr = subpicture_region_New(&region_fmt);
911     if (dst) {
912         dst->i_x       = x_offset;
913         dst->i_y       = y_offset;
914         dst->i_align   = 0;
915         if (dst->p_picture)
916             picture_Release(dst->p_picture);
917         dst->p_picture = picture_Hold(region_picture);
918         int fade_alpha = 255;
919         if (subpic->b_fade) {
920             mtime_t fade_start = subpic->i_start + 3 * (subpic->i_stop - subpic->i_start) / 4;
921
922             if (fade_start <= render_date && fade_start < subpic->i_stop)
923                 fade_alpha = 255 * (subpic->i_stop - render_date) /
924                                    (subpic->i_stop - fade_start);
925         }
926         dst->i_alpha   = fade_alpha * subpic->i_alpha * region->i_alpha / 65025;
927     }
928
929 exit:
930     if (restore_text) {
931         /* Some forms of subtitles need to be re-rendered more than
932          * once, eg. karaoke. We therefore restore the region to its
933          * pre-rendered state, so the next time through everything is
934          * calculated again.
935          */
936         if (region->p_picture) {
937             picture_Release(region->p_picture);
938             region->p_picture = NULL;
939         }
940         if (region->p_private) {
941             subpicture_region_private_Delete(region->p_private);
942             region->p_private = NULL;
943         }
944         region->fmt = fmt_original;
945     }
946 }
947
948 /**
949  * This function renders all sub picture units in the list.
950  */
951 static subpicture_t *SpuRenderSubpictures(spu_t *spu,
952                                           unsigned int i_subpicture,
953                                           subpicture_t **pp_subpicture,
954                                           const vlc_fourcc_t *chroma_list,
955                                           const video_format_t *fmt_dst,
956                                           const video_format_t *fmt_src,
957                                           mtime_t render_subtitle_date,
958                                           mtime_t render_osd_date)
959 {
960     spu_private_t *sys = spu->p;
961
962     /* Count the number of regions and subtitle regions */
963     unsigned int subtitle_region_count = 0;
964     unsigned int region_count          = 0;
965     for (unsigned i = 0; i < i_subpicture; i++) {
966         const subpicture_t *subpic = pp_subpicture[i];
967
968         unsigned count = 0;
969         for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
970             count++;
971
972         if (subpic->b_subtitle)
973             subtitle_region_count += count;
974         region_count += count;
975     }
976     if (region_count <= 0)
977         return NULL;
978
979     /* Create the output subpicture */
980     subpicture_t *output = subpicture_New(NULL);
981     if (!output)
982         return NULL;
983     output->i_order = pp_subpicture[i_subpicture - 1]->i_order;
984     output->i_original_picture_width  = fmt_dst->i_visible_width;
985     output->i_original_picture_height = fmt_dst->i_visible_height;
986     subpicture_region_t **output_last_ptr = &output->p_region;
987
988     /* Allocate area array for subtitle overlap */
989     spu_area_t subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
990     spu_area_t *subtitle_area;
991     int subtitle_area_count;
992
993     subtitle_area_count = 0;
994     subtitle_area = subtitle_area_buffer;
995     if (subtitle_region_count > sizeof(subtitle_area_buffer)/sizeof(*subtitle_area_buffer))
996         subtitle_area = calloc(subtitle_region_count, sizeof(*subtitle_area));
997
998     /* Process all subpictures and regions (in the right order) */
999     for (unsigned int index = 0; index < i_subpicture; index++) {
1000         subpicture_t        *subpic = pp_subpicture[index];
1001         subpicture_region_t *region;
1002
1003         if (!subpic->p_region)
1004             continue;
1005
1006         if (subpic->i_original_picture_width  <= 0 ||
1007             subpic->i_original_picture_height <= 0) {
1008             if (subpic->i_original_picture_width  > 0 ||
1009                 subpic->i_original_picture_height > 0)
1010                 msg_Err(spu, "original picture size %dx%d is unsupported",
1011                          subpic->i_original_picture_width,
1012                          subpic->i_original_picture_height);
1013             else
1014                 msg_Warn(spu, "original picture size is undefined");
1015
1016             subpic->i_original_picture_width  = fmt_src->i_visible_width;
1017             subpic->i_original_picture_height = fmt_src->i_visible_height;
1018         }
1019
1020         if (sys->text) {
1021             /* FIXME aspect ratio ? */
1022             sys->text->fmt_out.video.i_width          =
1023             sys->text->fmt_out.video.i_visible_width  = subpic->i_original_picture_width;
1024
1025             sys->text->fmt_out.video.i_height         =
1026             sys->text->fmt_out.video.i_visible_height = subpic->i_original_picture_height;
1027         }
1028
1029         /* Render all regions
1030          * We always transform non absolute subtitle into absolute one on the
1031          * first rendering to allow good subtitle overlap support.
1032          */
1033         for (region = subpic->p_region; region != NULL; region = region->p_next) {
1034             spu_area_t area;
1035
1036             /* Compute region scale AR */
1037             video_format_t region_fmt = region->fmt;
1038             if (region_fmt.i_sar_num <= 0 || region_fmt.i_sar_den <= 0) {
1039                 region_fmt.i_sar_num = (int64_t)fmt_dst->i_visible_width  * fmt_dst->i_sar_num * subpic->i_original_picture_height;
1040                 region_fmt.i_sar_den = (int64_t)fmt_dst->i_visible_height * fmt_dst->i_sar_den * subpic->i_original_picture_width;
1041                 vlc_ureduce(&region_fmt.i_sar_num, &region_fmt.i_sar_den,
1042                             region_fmt.i_sar_num, region_fmt.i_sar_den, 65536);
1043             }
1044
1045             /* Compute scaling from original size to destination size
1046              * FIXME The current scaling ensure that the heights match, the width being
1047              * cropped.
1048              */
1049             spu_scale_t scale = spu_scale_createq((int64_t)fmt_dst->i_visible_height                 * fmt_dst->i_sar_den * region_fmt.i_sar_num,
1050                                                   (int64_t)subpic->i_original_picture_height * fmt_dst->i_sar_num * region_fmt.i_sar_den,
1051                                                   fmt_dst->i_visible_height,
1052                                                   subpic->i_original_picture_height);
1053
1054             /* Check scale validity */
1055             if (scale.w <= 0 || scale.h <= 0)
1056                 continue;
1057
1058             /* */
1059             SpuRenderRegion(spu, output_last_ptr, &area,
1060                             subpic, region, scale,
1061                             chroma_list, fmt_dst,
1062                             subtitle_area, subtitle_area_count,
1063                             subpic->b_subtitle ? render_subtitle_date : render_osd_date);
1064             if (*output_last_ptr)
1065                 output_last_ptr = &(*output_last_ptr)->p_next;
1066
1067             if (subpic->b_subtitle) {
1068                 area = spu_area_unscaled(area, scale);
1069                 if (!subpic->b_absolute && area.width > 0 && area.height > 0) {
1070                     region->i_x = area.x;
1071                     region->i_y = area.y;
1072                 }
1073                 if (subtitle_area)
1074                     subtitle_area[subtitle_area_count++] = area;
1075             }
1076         }
1077         if (subpic->b_subtitle && subpic->p_region)
1078             subpic->b_absolute = true;
1079     }
1080
1081     /* */
1082     if (subtitle_area != subtitle_area_buffer)
1083         free(subtitle_area);
1084
1085     return output;
1086 }
1087
1088 /*****************************************************************************
1089  * Object variables callbacks
1090  *****************************************************************************/
1091
1092 /*****************************************************************************
1093  * UpdateSPU: update subpicture settings
1094  *****************************************************************************
1095  * This function is called from CropCallback and at initialization time, to
1096  * retrieve crop information from the input.
1097  *****************************************************************************/
1098 static void UpdateSPU(spu_t *spu, vlc_object_t *object)
1099 {
1100     spu_private_t *sys = spu->p;
1101     vlc_value_t val;
1102
1103     vlc_mutex_lock(&sys->lock);
1104
1105     sys->force_palette = false;
1106     sys->force_crop = false;
1107
1108     if (var_Get(object, "highlight", &val) || !val.b_bool) {
1109         vlc_mutex_unlock(&sys->lock);
1110         return;
1111     }
1112
1113     sys->force_crop = true;
1114     sys->crop.x      = var_GetInteger(object, "x-start");
1115     sys->crop.y      = var_GetInteger(object, "y-start");
1116     sys->crop.width  = var_GetInteger(object, "x-end") - sys->crop.x;
1117     sys->crop.height = var_GetInteger(object, "y-end") - sys->crop.y;
1118
1119     if (var_Get(object, "menu-palette", &val) == VLC_SUCCESS) {
1120         memcpy(sys->palette, val.p_address, 16);
1121         sys->force_palette = true;
1122     }
1123     vlc_mutex_unlock(&sys->lock);
1124
1125     msg_Dbg(object, "crop: %i,%i,%i,%i, palette forced: %i",
1126             sys->crop.x, sys->crop.y,
1127             sys->crop.width, sys->crop.height,
1128             sys->force_palette);
1129 }
1130
1131 /*****************************************************************************
1132  * CropCallback: called when the highlight properties are changed
1133  *****************************************************************************
1134  * This callback is called from the input thread when we need cropping
1135  *****************************************************************************/
1136 static int CropCallback(vlc_object_t *object, char const *var,
1137                         vlc_value_t oldval, vlc_value_t newval, void *data)
1138 {
1139     VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(var);
1140
1141     UpdateSPU((spu_t *)data, object);
1142     return VLC_SUCCESS;
1143 }
1144
1145 /*****************************************************************************
1146  * Buffers allocation callbacks for the filters
1147  *****************************************************************************/
1148
1149 static subpicture_t *sub_new_buffer(filter_t *filter)
1150 {
1151     int channel = (intptr_t)filter->owner.sys;
1152
1153     subpicture_t *subpicture = subpicture_New(NULL);
1154     if (subpicture)
1155         subpicture->i_channel = channel;
1156     return subpicture;
1157 }
1158
1159 static int SubSourceInit(filter_t *filter, void *data)
1160 {
1161     spu_t *spu = data;
1162     int channel = spu_RegisterChannel(spu);
1163
1164     filter->owner.sys = (void *)(intptr_t)channel;
1165     filter->owner.sub.buffer_new = sub_new_buffer;
1166     return VLC_SUCCESS;
1167 }
1168
1169 static int SubSourceClean(filter_t *filter, void *data)
1170 {
1171     spu_t *spu = data;
1172     int channel = (intptr_t)filter->owner.sys;
1173
1174     spu_ClearChannel(spu, channel);
1175     return VLC_SUCCESS;
1176 }
1177
1178 /*****************************************************************************
1179  * Public API
1180  *****************************************************************************/
1181
1182 #undef spu_Create
1183 /**
1184  * Creates the subpicture unit
1185  *
1186  * \param p_this the parent object which creates the subpicture unit
1187  */
1188 spu_t *spu_Create(vlc_object_t *object)
1189 {
1190     spu_t *spu = vlc_custom_create(object,
1191                                    sizeof(spu_t) + sizeof(spu_private_t),
1192                                    "subpicture");
1193     if (!spu)
1194         return NULL;
1195
1196     /* Initialize spu fields */
1197     spu_private_t *sys = spu->p = (spu_private_t*)&spu[1];
1198
1199     /* Initialize private fields */
1200     vlc_mutex_init(&sys->lock);
1201
1202     SpuHeapInit(&sys->heap);
1203
1204     sys->text = NULL;
1205     sys->scale = NULL;
1206     sys->scale_yuvp = NULL;
1207
1208     sys->margin = var_InheritInteger(spu, "sub-margin");
1209
1210     /* Register the default subpicture channel */
1211     sys->channel = SPU_DEFAULT_CHANNEL + 1;
1212
1213     sys->source_chain_update = NULL;
1214     sys->filter_chain_update = NULL;
1215     vlc_mutex_init(&sys->source_chain_lock);
1216     vlc_mutex_init(&sys->filter_chain_lock);
1217     sys->source_chain = filter_chain_New(spu, "sub source", false);
1218     sys->filter_chain = filter_chain_New(spu, "sub filter", false);
1219
1220     /* Load text and scale module */
1221     sys->text = SpuRenderCreateAndLoadText(spu);
1222
1223     /* XXX spu->p_scale is used for all conversion/scaling except yuvp to
1224      * yuva/rgba */
1225     sys->scale = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1226                                              VLC_CODEC_YUVA, VLC_CODEC_RGBA, true);
1227
1228     /* This one is used for YUVP to YUVA/RGBA without scaling
1229      * FIXME rename it */
1230     sys->scale_yuvp = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1231                                                   VLC_CODEC_YUVP, VLC_CODEC_YUVA, false);
1232
1233     /* */
1234     sys->last_sort_date = -1;
1235
1236     return spu;
1237 }
1238
1239 /**
1240  * Destroy the subpicture unit
1241  *
1242  * \param p_this the parent object which destroys the subpicture unit
1243  */
1244 void spu_Destroy(spu_t *spu)
1245 {
1246     spu_private_t *sys = spu->p;
1247
1248     if (sys->text)
1249         FilterRelease(sys->text);
1250
1251     if (sys->scale_yuvp)
1252         FilterRelease(sys->scale_yuvp);
1253
1254     if (sys->scale)
1255         FilterRelease(sys->scale);
1256
1257     filter_chain_ForEach(sys->source_chain, SubSourceClean, spu);
1258     filter_chain_Delete(sys->source_chain);
1259     filter_chain_Delete(sys->filter_chain);
1260     vlc_mutex_destroy(&sys->source_chain_lock);
1261     vlc_mutex_destroy(&sys->filter_chain_lock);
1262     free(sys->source_chain_update);
1263     free(sys->filter_chain_update);
1264
1265     /* Destroy all remaining subpictures */
1266     SpuHeapClean(&sys->heap);
1267
1268     vlc_mutex_destroy(&sys->lock);
1269
1270     vlc_object_release(spu);
1271 }
1272
1273 /**
1274  * Attach/Detach the SPU from any input
1275  *
1276  * \param p_this the object in which to destroy the subpicture unit
1277  * \param b_attach to select attach or detach
1278  */
1279 void spu_Attach(spu_t *spu, vlc_object_t *input, bool attach)
1280 {
1281     if (attach) {
1282         UpdateSPU(spu, input);
1283         var_Create(input, "highlight", VLC_VAR_BOOL);
1284         var_AddCallback(input, "highlight", CropCallback, spu);
1285
1286         vlc_mutex_lock(&spu->p->lock);
1287         spu->p->input = input;
1288
1289         if (spu->p->text)
1290             FilterRelease(spu->p->text);
1291         spu->p->text = SpuRenderCreateAndLoadText(spu);
1292
1293         vlc_mutex_unlock(&spu->p->lock);
1294     } else {
1295         vlc_mutex_lock(&spu->p->lock);
1296         spu->p->input = NULL;
1297         vlc_mutex_unlock(&spu->p->lock);
1298
1299         /* Delete callbacks */
1300         var_DelCallback(input, "highlight", CropCallback, spu);
1301         var_Destroy(input, "highlight");
1302     }
1303 }
1304
1305 /**
1306  * Inform the SPU filters of mouse event
1307  */
1308 int spu_ProcessMouse(spu_t *spu,
1309                      const vlc_mouse_t *mouse,
1310                      const video_format_t *fmt)
1311 {
1312     spu_private_t *sys = spu->p;
1313
1314     vlc_mutex_lock(&sys->source_chain_lock);
1315     filter_chain_MouseEvent(sys->source_chain, mouse, fmt);
1316     vlc_mutex_unlock(&sys->source_chain_lock);
1317
1318     return VLC_SUCCESS;
1319 }
1320
1321 /**
1322  * Display a subpicture
1323  *
1324  * Remove the reservation flag of a subpicture, which will cause it to be
1325  * ready for display.
1326  * \param spu the subpicture unit object
1327  * \param subpic the subpicture to display
1328  */
1329 void spu_PutSubpicture(spu_t *spu, subpicture_t *subpic)
1330 {
1331     spu_private_t *sys = spu->p;
1332
1333     /* Update sub-filter chain */
1334     vlc_mutex_lock(&sys->lock);
1335     char *chain_update = sys->filter_chain_update;
1336     sys->filter_chain_update = NULL;
1337     vlc_mutex_unlock(&sys->lock);
1338
1339     bool is_left_empty = false;
1340
1341     vlc_mutex_lock(&sys->filter_chain_lock);
1342     if (chain_update) {
1343         if (*chain_update) {
1344             filter_chain_Reset(sys->filter_chain, NULL, NULL);
1345
1346             filter_chain_AppendFromString(spu->p->filter_chain, chain_update);
1347         }
1348         else if (filter_chain_GetLength(spu->p->filter_chain) > 0)
1349             filter_chain_Reset(sys->filter_chain, NULL, NULL);
1350
1351         /* "sub-source"  was formerly "sub-filter", so now the "sub-filter"
1352         configuration may contain sub-filters or sub-sources configurations.
1353         if the filters chain was left empty it may indicate that it's a sub-source configuration */
1354         is_left_empty = (filter_chain_GetLength(spu->p->filter_chain) == 0);
1355     }
1356     vlc_mutex_unlock(&sys->filter_chain_lock);
1357
1358     if (is_left_empty) {
1359         /* try to use the configuration as a sub-source configuration,
1360            but only if there is no 'source_chain_update' value and
1361            if only if 'chain_update' has a value */
1362         if (chain_update && *chain_update) {
1363             vlc_mutex_lock(&sys->lock);
1364             if (!sys->source_chain_update || !*sys->source_chain_update) {
1365                 if (sys->source_chain_update)
1366                     free(sys->source_chain_update);
1367                 sys->source_chain_update = chain_update;
1368                 chain_update = NULL;
1369             }
1370             vlc_mutex_unlock(&sys->lock);
1371         }
1372     }
1373
1374     free(chain_update);
1375
1376     /* Run filter chain on the new subpicture */
1377     vlc_mutex_lock(&sys->filter_chain_lock);
1378     subpic = filter_chain_SubFilter(spu->p->filter_chain, subpic);
1379     vlc_mutex_unlock(&sys->filter_chain_lock);
1380     if (!subpic)
1381         return;
1382
1383     /* SPU_DEFAULT_CHANNEL always reset itself */
1384     if (subpic->i_channel == SPU_DEFAULT_CHANNEL)
1385         spu_ClearChannel(spu, SPU_DEFAULT_CHANNEL);
1386
1387     /* p_private is for spu only and cannot be non NULL here */
1388     for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
1389         assert(r->p_private == NULL);
1390
1391     /* */
1392     vlc_mutex_lock(&sys->lock);
1393     if (SpuHeapPush(&sys->heap, subpic)) {
1394         vlc_mutex_unlock(&sys->lock);
1395         msg_Err(spu, "subpicture heap full");
1396         subpicture_Delete(subpic);
1397         return;
1398     }
1399     vlc_mutex_unlock(&sys->lock);
1400 }
1401
1402 subpicture_t *spu_Render(spu_t *spu,
1403                          const vlc_fourcc_t *chroma_list,
1404                          const video_format_t *fmt_dst,
1405                          const video_format_t *fmt_src,
1406                          mtime_t render_subtitle_date,
1407                          mtime_t render_osd_date,
1408                          bool ignore_osd)
1409 {
1410     spu_private_t *sys = spu->p;
1411
1412     /* Update sub-source chain */
1413     vlc_mutex_lock(&sys->lock);
1414     char *chain_update = sys->source_chain_update;
1415     sys->source_chain_update = NULL;
1416     vlc_mutex_unlock(&sys->lock);
1417
1418     vlc_mutex_lock(&sys->source_chain_lock);
1419     if (chain_update) {
1420         filter_chain_ForEach(sys->source_chain, SubSourceClean, spu);
1421         filter_chain_Reset(sys->source_chain, NULL, NULL);
1422
1423         filter_chain_AppendFromString(spu->p->source_chain, chain_update);
1424         filter_chain_ForEach(sys->source_chain, SubSourceInit, spu);
1425
1426         free(chain_update);
1427     }
1428     /* Run subpicture sources */
1429     filter_chain_SubSource(sys->source_chain, spu, render_osd_date);
1430     vlc_mutex_unlock(&sys->source_chain_lock);
1431
1432     static const vlc_fourcc_t chroma_list_default_yuv[] = {
1433         VLC_CODEC_YUVA,
1434         VLC_CODEC_RGBA,
1435         VLC_CODEC_ARGB,
1436         VLC_CODEC_YUVP,
1437         0,
1438     };
1439     static const vlc_fourcc_t chroma_list_default_rgb[] = {
1440         VLC_CODEC_RGBA,
1441         VLC_CODEC_ARGB,
1442         VLC_CODEC_YUVA,
1443         VLC_CODEC_YUVP,
1444         0,
1445     };
1446
1447     if (!chroma_list || *chroma_list == 0)
1448         chroma_list = vlc_fourcc_IsYUV(fmt_dst->i_chroma) ? chroma_list_default_yuv
1449                                                           : chroma_list_default_rgb;
1450
1451     vlc_mutex_lock(&sys->lock);
1452
1453     unsigned int subpicture_count;
1454     subpicture_t *subpicture_array[VOUT_MAX_SUBPICTURES];
1455
1456     /* Get an array of subpictures to render */
1457     SpuSelectSubpictures(spu, &subpicture_count, subpicture_array,
1458                          render_subtitle_date, render_osd_date, ignore_osd);
1459     if (subpicture_count <= 0) {
1460         vlc_mutex_unlock(&sys->lock);
1461         return NULL;
1462     }
1463
1464     /* Updates the subpictures */
1465     for (unsigned i = 0; i < subpicture_count; i++) {
1466         subpicture_t *subpic = subpicture_array[i];
1467         subpicture_Update(subpic,
1468                           fmt_src, fmt_dst,
1469                           subpic->b_subtitle ? render_subtitle_date : render_osd_date);
1470     }
1471
1472     /* Now order the subpicture array
1473      * XXX The order is *really* important for overlap subtitles positionning */
1474     qsort(subpicture_array, subpicture_count, sizeof(*subpicture_array), SubpictureCmp);
1475
1476     /* Render the subpictures */
1477     subpicture_t *render = SpuRenderSubpictures(spu,
1478                                                 subpicture_count, subpicture_array,
1479                                                 chroma_list,
1480                                                 fmt_dst,
1481                                                 fmt_src,
1482                                                 render_subtitle_date,
1483                                                 render_osd_date);
1484     vlc_mutex_unlock(&sys->lock);
1485
1486     return render;
1487 }
1488
1489 void spu_OffsetSubtitleDate(spu_t *spu, mtime_t duration)
1490 {
1491     spu_private_t *sys = spu->p;
1492
1493     vlc_mutex_lock(&sys->lock);
1494     for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
1495         spu_heap_entry_t *entry = &sys->heap.entry[i];
1496         subpicture_t *current = entry->subpicture;
1497
1498         if (current && current->b_subtitle) {
1499             if (current->i_start > 0)
1500                 current->i_start += duration;
1501             if (current->i_stop > 0)
1502                 current->i_stop  += duration;
1503         }
1504     }
1505     vlc_mutex_unlock(&sys->lock);
1506 }
1507
1508 int spu_RegisterChannel(spu_t *spu)
1509 {
1510     spu_private_t *sys = spu->p;
1511
1512     vlc_mutex_lock(&sys->lock);
1513     int channel = sys->channel++;
1514     vlc_mutex_unlock(&sys->lock);
1515
1516     return channel;
1517 }
1518
1519 void spu_ClearChannel(spu_t *spu, int channel)
1520 {
1521     spu_private_t *sys = spu->p;
1522
1523     vlc_mutex_lock(&sys->lock);
1524
1525     for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
1526         spu_heap_entry_t *entry = &sys->heap.entry[i];
1527         subpicture_t *subpic = entry->subpicture;
1528
1529         if (!subpic)
1530             continue;
1531         if (subpic->i_channel != channel && (channel != -1 || subpic->i_channel == SPU_DEFAULT_CHANNEL))
1532             continue;
1533
1534         /* You cannot delete subpicture outside of spu_SortSubpictures */
1535         entry->reject = true;
1536     }
1537
1538     vlc_mutex_unlock(&sys->lock);
1539 }
1540
1541 void spu_ChangeSources(spu_t *spu, const char *filters)
1542 {
1543     spu_private_t *sys = spu->p;
1544
1545     vlc_mutex_lock(&sys->lock);
1546
1547     free(sys->source_chain_update);
1548     sys->source_chain_update = strdup(filters);
1549
1550     vlc_mutex_unlock(&sys->lock);
1551 }
1552
1553 void spu_ChangeFilters(spu_t *spu, const char *filters)
1554 {
1555     spu_private_t *sys = spu->p;
1556
1557     vlc_mutex_lock(&sys->lock);
1558
1559     free(sys->filter_chain_update);
1560     sys->filter_chain_update = strdup(filters);
1561
1562     vlc_mutex_unlock(&sys->lock);
1563 }
1564
1565 void spu_ChangeMargin(spu_t *spu, int margin)
1566 {
1567     spu_private_t *sys = spu->p;
1568
1569     vlc_mutex_lock(&sys->lock);
1570     sys->margin = margin;
1571     vlc_mutex_unlock(&sys->lock);
1572 }
1573