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