]> git.sesse.net Git - vlc/blob - src/video_output/vout_subpictures.c
LGPL
[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 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     if (filter->p_owner)
169         free(filter->p_owner);
170
171     vlc_object_release(filter);
172 }
173
174 static picture_t *spu_new_video_buffer(filter_t *filter)
175 {
176     const video_format_t *fmt = &filter->fmt_out.video;
177
178     VLC_UNUSED(filter);
179     return picture_NewFromFormat(fmt);
180 }
181 static void spu_del_video_buffer(filter_t *filter, picture_t *picture)
182 {
183     VLC_UNUSED(filter);
184     picture_Release(picture);
185 }
186
187 static int spu_get_attachments(filter_t *filter,
188                                input_attachment_t ***attachment_ptr,
189                                int *attachment_count)
190 {
191     spu_t *spu = filter->p_owner->spu;
192
193     int ret = VLC_EGENERIC;
194     if (spu->p->input)
195         ret = input_Control((input_thread_t*)spu->p->input,
196                             INPUT_GET_ATTACHMENTS,
197                             attachment_ptr, attachment_count);
198     return ret;
199 }
200
201 static filter_t *SpuRenderCreateAndLoadText(spu_t *spu)
202 {
203     filter_t *text = vlc_custom_create(spu, sizeof(*text), "spu text");
204     if (!text)
205         return NULL;
206
207     text->p_owner = xmalloc(sizeof(*text->p_owner));
208     text->p_owner->spu = spu;
209
210     es_format_Init(&text->fmt_in, VIDEO_ES, 0);
211
212     es_format_Init(&text->fmt_out, VIDEO_ES, 0);
213     text->fmt_out.video.i_width          =
214     text->fmt_out.video.i_visible_width  = 32;
215     text->fmt_out.video.i_height         =
216     text->fmt_out.video.i_visible_height = 32;
217
218     text->pf_get_attachments = spu_get_attachments;
219
220     text->p_module = module_need(text, "text renderer", "$text-renderer", false);
221
222     /* Create a few variables used for enhanced text rendering */
223     var_Create(text, "spu-elapsed",   VLC_VAR_TIME);
224     var_Create(text, "text-rerender", VLC_VAR_BOOL);
225
226     return text;
227 }
228
229 static filter_t *SpuRenderCreateAndLoadScale(vlc_object_t *object,
230                                              vlc_fourcc_t src_chroma,
231                                              vlc_fourcc_t dst_chroma,
232                                              bool require_resize)
233 {
234     filter_t *scale = vlc_custom_create(object, sizeof(*scale), "scale");
235     if (!scale)
236         return NULL;
237
238     es_format_Init(&scale->fmt_in, VIDEO_ES, 0);
239     scale->fmt_in.video.i_chroma = src_chroma;
240     scale->fmt_in.video.i_width =
241     scale->fmt_in.video.i_height = 32;
242
243     es_format_Init(&scale->fmt_out, VIDEO_ES, 0);
244     scale->fmt_out.video.i_chroma = dst_chroma;
245     scale->fmt_out.video.i_width =
246     scale->fmt_out.video.i_height = require_resize ? 16 : 32;
247
248     scale->pf_video_buffer_new = spu_new_video_buffer;
249     scale->pf_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_height - region->i_y;
480         else
481             *y = subpic->i_original_picture_height / 2 - region->fmt.i_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_width - region->i_x;
487         else
488             *x = subpic->i_original_picture_width / 2 - region->fmt.i_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_width, region->fmt.i_height,
726                                 scale_size);
727
728     /* Handle overlapping subtitles when possible */
729     if (subpic->b_subtitle && !subpic->b_absolute)
730         SpuAreaFixOverlap(dst_area, subtitle_area, subtitle_area_count,
731                           region->i_align);
732
733     /* we copy the area: for the subtitle overlap support we want
734      * to only save the area without margin applied */
735     spu_area_t restrained = *dst_area;
736
737     /* apply margin to subtitles and correct if they go over the picture edge */
738     if (subpic->b_subtitle)
739         restrained.y -= y_margin;
740
741     spu_area_t display = spu_area_create(0, 0, fmt->i_width, fmt->i_height,
742                                          spu_scale_unit());
743     SpuAreaFitInside(&restrained, &display);
744
745     /* Fix the position for the current scale_size */
746     x_offset = spu_scale_w(restrained.x, restrained.scale);
747     y_offset = spu_scale_h(restrained.y, restrained.scale);
748
749     /* */
750     if (force_palette) {
751         video_palette_t *old_palette = region->fmt.p_palette;
752         video_palette_t new_palette;
753
754         /* We suppose DVD palette here */
755         new_palette.i_entries = 4;
756         for (int i = 0; i < 4; i++)
757             for (int j = 0; j < 4; j++)
758                 new_palette.palette[i][j] = sys->palette[i][j];
759
760         if (old_palette->i_entries == new_palette.i_entries) {
761             for (int i = 0; i < old_palette->i_entries; i++)
762                 for (int j = 0; j < 4; j++)
763                     changed_palette |= old_palette->palette[i][j] != new_palette.palette[i][j];
764         } else {
765             changed_palette = true;
766         }
767         *old_palette = new_palette;
768     }
769
770     /* */
771     region_fmt = region->fmt;
772     region_picture = region->p_picture;
773
774     bool convert_chroma = true;
775     for (int i = 0; chroma_list[i] && convert_chroma; i++) {
776         if (region_fmt.i_chroma == chroma_list[i])
777             convert_chroma = false;
778     }
779
780     /* Scale from rendered size to destination size */
781     if (sys->scale && sys->scale->p_module &&
782         (!using_palette || (sys->scale_yuvp && sys->scale_yuvp->p_module)) &&
783         (scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT ||
784         using_palette || convert_chroma)) {
785         const unsigned dst_width  = spu_scale_w(region->fmt.i_width,  scale_size);
786         const unsigned dst_height = spu_scale_h(region->fmt.i_height, scale_size);
787
788         /* Destroy the cache if unusable */
789         if (region->p_private) {
790             subpicture_region_private_t *private = region->p_private;
791             bool is_changed = false;
792
793             /* Check resize changes */
794             if (dst_width  != private->fmt.i_width ||
795                 dst_height != private->fmt.i_height)
796                 is_changed = true;
797
798             /* Check forced palette changes */
799             if (changed_palette)
800                 is_changed = true;
801
802             if (convert_chroma && private->fmt.i_chroma != chroma_list[0])
803                 is_changed = true;
804
805             if (is_changed) {
806                 subpicture_region_private_Delete(private);
807                 region->p_private = NULL;
808             }
809         }
810
811         /* Scale if needed into cache */
812         if (!region->p_private && dst_width > 0 && dst_height > 0) {
813             filter_t *scale = sys->scale;
814
815             picture_t *picture = region->p_picture;
816             picture_Hold(picture);
817
818             /* Convert YUVP to YUVA/RGBA first for better scaling quality */
819             if (using_palette) {
820                 filter_t *scale_yuvp = sys->scale_yuvp;
821
822                 scale_yuvp->fmt_in.video = region->fmt;
823
824                 scale_yuvp->fmt_out.video = region->fmt;
825                 scale_yuvp->fmt_out.video.i_chroma = chroma_list[0];
826
827                 picture = scale_yuvp->pf_video_filter(scale_yuvp, picture);
828                 if (!picture) {
829                     /* Well we will try conversion+scaling */
830                     msg_Warn(spu, "%4.4s to %4.4s conversion failed",
831                              (const char*)&scale_yuvp->fmt_in.video.i_chroma,
832                              (const char*)&scale_yuvp->fmt_out.video.i_chroma);
833                 }
834             }
835
836             /* Conversion(except from YUVP)/Scaling */
837             if (picture &&
838                 (picture->format.i_width  != dst_width ||
839                  picture->format.i_height != dst_height ||
840                  (convert_chroma && !using_palette)))
841             {
842                 scale->fmt_in.video  = picture->format;
843                 scale->fmt_out.video = picture->format;
844                 if (convert_chroma)
845                     scale->fmt_out.i_codec        =
846                     scale->fmt_out.video.i_chroma = chroma_list[0];
847
848                 scale->fmt_out.video.i_width  = dst_width;
849                 scale->fmt_out.video.i_height = dst_height;
850
851                 scale->fmt_out.video.i_visible_width =
852                     spu_scale_w(region->fmt.i_visible_width, scale_size);
853                 scale->fmt_out.video.i_visible_height =
854                     spu_scale_h(region->fmt.i_visible_height, scale_size);
855
856                 picture = scale->pf_video_filter(scale, picture);
857                 if (!picture)
858                     msg_Err(spu, "scaling failed");
859             }
860
861             /* */
862             if (picture) {
863                 region->p_private = subpicture_region_private_New(&picture->format);
864                 if (region->p_private) {
865                     region->p_private->p_picture = picture;
866                     if (!region->p_private->p_picture) {
867                         subpicture_region_private_Delete(region->p_private);
868                         region->p_private = NULL;
869                     }
870                 } else {
871                     picture_Release(picture);
872                 }
873             }
874         }
875
876         /* And use the scaled picture */
877         if (region->p_private) {
878             region_fmt     = region->p_private->fmt;
879             region_picture = region->p_private->p_picture;
880         }
881     }
882
883     /* Force cropping if requested */
884     if (force_crop) {
885         int crop_x     = spu_scale_w(sys->crop.x,     scale_size);
886         int crop_y     = spu_scale_h(sys->crop.y,     scale_size);
887         int crop_width = spu_scale_w(sys->crop.width, scale_size);
888         int crop_height= spu_scale_h(sys->crop.height,scale_size);
889
890         /* Find the intersection */
891         if (crop_x + crop_width <= x_offset ||
892             x_offset + (int)region_fmt.i_visible_width  < crop_x ||
893             crop_y + crop_height <= y_offset ||
894             y_offset + (int)region_fmt.i_visible_height < crop_y) {
895             /* No intersection */
896             region_fmt.i_visible_width  =
897             region_fmt.i_visible_height = 0;
898         } else {
899             int x, y, x_end, y_end;
900             x = __MAX(crop_x, x_offset);
901             y = __MAX(crop_y, y_offset);
902             x_end = __MIN(crop_x + crop_width,
903                           x_offset + (int)region_fmt.i_visible_width);
904             y_end = __MIN(crop_y + crop_height,
905                           y_offset + (int)region_fmt.i_visible_height);
906
907             region_fmt.i_x_offset       = x - x_offset;
908             region_fmt.i_y_offset       = y - y_offset;
909             region_fmt.i_visible_width  = x_end - x;
910             region_fmt.i_visible_height = y_end - y;
911
912             x_offset = __MAX(x, 0);
913             y_offset = __MAX(y, 0);
914         }
915     }
916
917     subpicture_region_t *dst = *dst_ptr = subpicture_region_New(&region_fmt);
918     if (dst) {
919         dst->i_x       = x_offset;
920         dst->i_y       = y_offset;
921         dst->i_align   = 0;
922         if (dst->p_picture)
923             picture_Release(dst->p_picture);
924         dst->p_picture = picture_Hold(region_picture);
925         int fade_alpha = 255;
926         if (subpic->b_fade) {
927             mtime_t fade_start = subpic->i_start + 3 * (subpic->i_stop - subpic->i_start) / 4;
928
929             if (fade_start <= render_date && fade_start < subpic->i_stop)
930                 fade_alpha = 255 * (subpic->i_stop - render_date) /
931                                    (subpic->i_stop - fade_start);
932         }
933         dst->i_alpha   = fade_alpha * subpic->i_alpha * region->i_alpha / 65025;
934     }
935
936 exit:
937     if (restore_text) {
938         /* Some forms of subtitles need to be re-rendered more than
939          * once, eg. karaoke. We therefore restore the region to its
940          * pre-rendered state, so the next time through everything is
941          * calculated again.
942          */
943         if (region->p_picture) {
944             picture_Release(region->p_picture);
945             region->p_picture = NULL;
946         }
947         if (region->p_private) {
948             subpicture_region_private_Delete(region->p_private);
949             region->p_private = NULL;
950         }
951         region->fmt = fmt_original;
952     }
953 }
954
955 /**
956  * This function renders all sub picture units in the list.
957  */
958 static subpicture_t *SpuRenderSubpictures(spu_t *spu,
959                                           unsigned int i_subpicture,
960                                           subpicture_t **pp_subpicture,
961                                           const vlc_fourcc_t *chroma_list,
962                                           const video_format_t *fmt_dst,
963                                           const video_format_t *fmt_src,
964                                           mtime_t render_subtitle_date,
965                                           mtime_t render_osd_date)
966 {
967     spu_private_t *sys = spu->p;
968
969     /* Count the number of regions and subtitle regions */
970     unsigned int subtitle_region_count = 0;
971     unsigned int region_count          = 0;
972     for (unsigned i = 0; i < i_subpicture; i++) {
973         const subpicture_t *subpic = pp_subpicture[i];
974
975         unsigned count = 0;
976         for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
977             count++;
978
979         if (subpic->b_subtitle)
980             subtitle_region_count += count;
981         region_count += count;
982     }
983     if (region_count <= 0)
984         return NULL;
985
986     /* Create the output subpicture */
987     subpicture_t *output = subpicture_New(NULL);
988     if (!output)
989         return NULL;
990     output->i_original_picture_width  = fmt_dst->i_width;
991     output->i_original_picture_height = fmt_dst->i_height;
992     subpicture_region_t **output_last_ptr = &output->p_region;
993
994     /* Allocate area array for subtitle overlap */
995     spu_area_t subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
996     spu_area_t *subtitle_area;
997     int subtitle_area_count;
998
999     subtitle_area_count = 0;
1000     subtitle_area = subtitle_area_buffer;
1001     if (subtitle_region_count > sizeof(subtitle_area_buffer)/sizeof(*subtitle_area_buffer))
1002         subtitle_area = calloc(subtitle_region_count, sizeof(*subtitle_area));
1003
1004     /* Process all subpictures and regions (in the right order) */
1005     for (unsigned int index = 0; index < i_subpicture; index++) {
1006         subpicture_t        *subpic = pp_subpicture[index];
1007         subpicture_region_t *region;
1008
1009         if (!subpic->p_region)
1010             continue;
1011
1012         if (subpic->i_original_picture_width  <= 0 ||
1013             subpic->i_original_picture_height <= 0) {
1014             if (subpic->i_original_picture_width  > 0 ||
1015                 subpic->i_original_picture_height > 0)
1016                 msg_Err(spu, "original picture size %dx%d is unsupported",
1017                          subpic->i_original_picture_width,
1018                          subpic->i_original_picture_height);
1019             else
1020                 msg_Warn(spu, "original picture size is undefined");
1021
1022             subpic->i_original_picture_width  = fmt_src->i_width;
1023             subpic->i_original_picture_height = fmt_src->i_height;
1024         }
1025
1026         if (sys->text) {
1027             /* FIXME aspect ratio ? */
1028             sys->text->fmt_out.video.i_width          =
1029             sys->text->fmt_out.video.i_visible_width  = subpic->i_original_picture_width;
1030
1031             sys->text->fmt_out.video.i_height         =
1032             sys->text->fmt_out.video.i_visible_height = subpic->i_original_picture_height;
1033         }
1034
1035         /* Render all regions
1036          * We always transform non absolute subtitle into absolute one on the
1037          * first rendering to allow good subtitle overlap support.
1038          */
1039         for (region = subpic->p_region; region != NULL; region = region->p_next) {
1040             spu_area_t area;
1041
1042             /* Compute region scale AR */
1043             video_format_t region_fmt = region->fmt;
1044             if (region_fmt.i_sar_num <= 0 || region_fmt.i_sar_den <= 0) {
1045                 region_fmt.i_sar_num = fmt_src->i_sar_num;
1046                 region_fmt.i_sar_den = fmt_src->i_sar_den;
1047             }
1048
1049             /* Compute scaling from original size to destination size
1050              * FIXME The current scaling ensure that the heights match, the width being
1051              * cropped.
1052              */
1053             spu_scale_t scale = spu_scale_createq((int64_t)fmt_dst->i_height                 * fmt_dst->i_sar_den * region_fmt.i_sar_num,
1054                                                   (int64_t)subpic->i_original_picture_height * fmt_dst->i_sar_num * region_fmt.i_sar_den,
1055                                                   fmt_dst->i_height,
1056                                                   subpic->i_original_picture_height);
1057
1058             /* Check scale validity */
1059             if (scale.w <= 0 || scale.h <= 0)
1060                 continue;
1061
1062             /* */
1063             SpuRenderRegion(spu, output_last_ptr, &area,
1064                             subpic, region, scale,
1065                             chroma_list, fmt_dst,
1066                             subtitle_area, subtitle_area_count,
1067                             subpic->b_subtitle ? render_subtitle_date : render_osd_date);
1068             if (*output_last_ptr)
1069                 output_last_ptr = &(*output_last_ptr)->p_next;
1070
1071             if (subpic->b_subtitle) {
1072                 area = spu_area_unscaled(area, scale);
1073                 if (!subpic->b_absolute && area.width > 0 && area.height > 0) {
1074                     region->i_x = area.x;
1075                     region->i_y = area.y;
1076                 }
1077                 if (subtitle_area)
1078                     subtitle_area[subtitle_area_count++] = area;
1079             }
1080         }
1081         if (subpic->b_subtitle && subpic->p_region)
1082             subpic->b_absolute = true;
1083     }
1084
1085     /* */
1086     if (subtitle_area != subtitle_area_buffer)
1087         free(subtitle_area);
1088
1089     return output;
1090 }
1091
1092 /*****************************************************************************
1093  * Object variables callbacks
1094  *****************************************************************************/
1095
1096 /*****************************************************************************
1097  * UpdateSPU: update subpicture settings
1098  *****************************************************************************
1099  * This function is called from CropCallback and at initialization time, to
1100  * retrieve crop information from the input.
1101  *****************************************************************************/
1102 static void UpdateSPU(spu_t *spu, vlc_object_t *object)
1103 {
1104     spu_private_t *sys = spu->p;
1105     vlc_value_t val;
1106
1107     vlc_mutex_lock(&sys->lock);
1108
1109     sys->force_palette = false;
1110     sys->force_crop = false;
1111
1112     if (var_Get(object, "highlight", &val) || !val.b_bool) {
1113         vlc_mutex_unlock(&sys->lock);
1114         return;
1115     }
1116
1117     sys->force_crop = true;
1118     sys->crop.x      = var_GetInteger(object, "x-start");
1119     sys->crop.y      = var_GetInteger(object, "y-start");
1120     sys->crop.width  = var_GetInteger(object, "x-end") - sys->crop.x;
1121     sys->crop.height = var_GetInteger(object, "y-end") - sys->crop.y;
1122
1123     if (var_Get(object, "menu-palette", &val) == VLC_SUCCESS) {
1124         memcpy(sys->palette, val.p_address, 16);
1125         sys->force_palette = true;
1126     }
1127     vlc_mutex_unlock(&sys->lock);
1128
1129     msg_Dbg(object, "crop: %i,%i,%i,%i, palette forced: %i",
1130             sys->crop.x, sys->crop.y,
1131             sys->crop.width, sys->crop.height,
1132             sys->force_palette);
1133 }
1134
1135 /*****************************************************************************
1136  * CropCallback: called when the highlight properties are changed
1137  *****************************************************************************
1138  * This callback is called from the input thread when we need cropping
1139  *****************************************************************************/
1140 static int CropCallback(vlc_object_t *object, char const *var,
1141                         vlc_value_t oldval, vlc_value_t newval, void *data)
1142 {
1143     VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(var);
1144
1145     UpdateSPU((spu_t *)data, object);
1146     return VLC_SUCCESS;
1147 }
1148
1149 /*****************************************************************************
1150  * Buffers allocation callbacks for the filters
1151  *****************************************************************************/
1152
1153 static subpicture_t *sub_new_buffer(filter_t *filter)
1154 {
1155     filter_owner_sys_t *sys = filter->p_owner;
1156
1157     subpicture_t *subpicture = subpicture_New(NULL);
1158     if (subpicture)
1159         subpicture->i_channel = sys->channel;
1160     return subpicture;
1161 }
1162 static void sub_del_buffer(filter_t *filter, subpicture_t *subpic)
1163 {
1164     VLC_UNUSED(filter);
1165     subpicture_Delete(subpic);
1166 }
1167
1168 static int SubSourceAllocationInit(filter_t *filter, void *data)
1169 {
1170     spu_t *spu = data;
1171
1172     filter_owner_sys_t *sys = malloc(sizeof(*sys));
1173     if (!sys)
1174         return VLC_EGENERIC;
1175
1176     filter->pf_sub_buffer_new = sub_new_buffer;
1177     filter->pf_sub_buffer_del = sub_del_buffer;
1178
1179     filter->p_owner = sys;
1180     sys->channel = spu_RegisterChannel(spu);
1181     sys->spu     = spu;
1182
1183     return VLC_SUCCESS;
1184 }
1185
1186 static void SubSourceAllocationClean(filter_t *filter)
1187 {
1188     filter_owner_sys_t *sys = filter->p_owner;
1189
1190     spu_ClearChannel(sys->spu, sys->channel);
1191     free(filter->p_owner);
1192 }
1193
1194 /*****************************************************************************
1195  * Public API
1196  *****************************************************************************/
1197
1198 #undef spu_Create
1199 /**
1200  * Creates the subpicture unit
1201  *
1202  * \param p_this the parent object which creates the subpicture unit
1203  */
1204 spu_t *spu_Create(vlc_object_t *object)
1205 {
1206     spu_t *spu = vlc_custom_create(object,
1207                                    sizeof(spu_t) + sizeof(spu_private_t),
1208                                    "subpicture");
1209     if (!spu)
1210         return NULL;
1211
1212     /* Initialize spu fields */
1213     spu_private_t *sys = spu->p = (spu_private_t*)&spu[1];
1214
1215     /* Initialize private fields */
1216     vlc_mutex_init(&sys->lock);
1217
1218     SpuHeapInit(&sys->heap);
1219
1220     sys->text = NULL;
1221     sys->scale = NULL;
1222     sys->scale_yuvp = NULL;
1223
1224     sys->margin = var_InheritInteger(spu, "sub-margin");
1225
1226     /* Register the default subpicture channel */
1227     sys->channel = SPU_DEFAULT_CHANNEL + 1;
1228
1229     sys->source_chain_update = NULL;
1230     sys->filter_chain_update = NULL;
1231     vlc_mutex_init(&sys->source_chain_lock);
1232     vlc_mutex_init(&sys->filter_chain_lock);
1233     sys->source_chain = filter_chain_New(spu, "sub source", false,
1234                                          SubSourceAllocationInit,
1235                                          SubSourceAllocationClean,
1236                                          spu);
1237     sys->filter_chain = filter_chain_New(spu, "sub filter", false,
1238                                          NULL,
1239                                          NULL,
1240                                          spu);
1241
1242     /* Load text and scale module */
1243     sys->text = SpuRenderCreateAndLoadText(spu);
1244
1245     /* XXX spu->p_scale is used for all conversion/scaling except yuvp to
1246      * yuva/rgba */
1247     sys->scale = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1248                                              VLC_CODEC_YUVA, VLC_CODEC_RGBA, true);
1249
1250     /* This one is used for YUVP to YUVA/RGBA without scaling
1251      * FIXME rename it */
1252     sys->scale_yuvp = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1253                                                   VLC_CODEC_YUVP, VLC_CODEC_YUVA, false);
1254
1255     /* */
1256     sys->last_sort_date = -1;
1257
1258     return spu;
1259 }
1260
1261 /**
1262  * Destroy the subpicture unit
1263  *
1264  * \param p_this the parent object which destroys the subpicture unit
1265  */
1266 void spu_Destroy(spu_t *spu)
1267 {
1268     spu_private_t *sys = spu->p;
1269
1270     if (sys->text)
1271         FilterRelease(sys->text);
1272
1273     if (sys->scale_yuvp)
1274         FilterRelease(sys->scale_yuvp);
1275
1276     if (sys->scale)
1277         FilterRelease(sys->scale);
1278
1279     filter_chain_Delete(sys->source_chain);
1280     filter_chain_Delete(sys->filter_chain);
1281     vlc_mutex_destroy(&sys->source_chain_lock);
1282     vlc_mutex_destroy(&sys->filter_chain_lock);
1283     free(sys->source_chain_update);
1284     free(sys->filter_chain_update);
1285
1286     /* Destroy all remaining subpictures */
1287     SpuHeapClean(&sys->heap);
1288
1289     vlc_mutex_destroy(&sys->lock);
1290
1291     vlc_object_release(spu);
1292 }
1293
1294 /**
1295  * Attach/Detach the SPU from any input
1296  *
1297  * \param p_this the object in which to destroy the subpicture unit
1298  * \param b_attach to select attach or detach
1299  */
1300 void spu_Attach(spu_t *spu, vlc_object_t *input, bool attach)
1301 {
1302     if (attach) {
1303         UpdateSPU(spu, input);
1304         var_Create(input, "highlight", VLC_VAR_BOOL);
1305         var_AddCallback(input, "highlight", CropCallback, spu);
1306
1307         vlc_mutex_lock(&spu->p->lock);
1308         spu->p->input = input;
1309
1310         if (spu->p->text)
1311             FilterRelease(spu->p->text);
1312         spu->p->text = SpuRenderCreateAndLoadText(spu);
1313
1314         vlc_mutex_unlock(&spu->p->lock);
1315     } else {
1316         vlc_mutex_lock(&spu->p->lock);
1317         spu->p->input = NULL;
1318         vlc_mutex_unlock(&spu->p->lock);
1319
1320         /* Delete callbacks */
1321         var_DelCallback(input, "highlight", CropCallback, spu);
1322         var_Destroy(input, "highlight");
1323     }
1324 }
1325
1326 /**
1327  * Inform the SPU filters of mouse event
1328  */
1329 int spu_ProcessMouse(spu_t *spu,
1330                      const vlc_mouse_t *mouse,
1331                      const video_format_t *fmt)
1332 {
1333     spu_private_t *sys = spu->p;
1334
1335     vlc_mutex_lock(&sys->source_chain_lock);
1336     filter_chain_MouseEvent(sys->source_chain, mouse, fmt);
1337     vlc_mutex_unlock(&sys->source_chain_lock);
1338
1339     return VLC_SUCCESS;
1340 }
1341
1342 /**
1343  * Display a subpicture
1344  *
1345  * Remove the reservation flag of a subpicture, which will cause it to be
1346  * ready for display.
1347  * \param spu the subpicture unit object
1348  * \param subpic the subpicture to display
1349  */
1350 void spu_PutSubpicture(spu_t *spu, subpicture_t *subpic)
1351 {
1352     spu_private_t *sys = spu->p;
1353
1354     /* Update sub-filter chain */
1355     vlc_mutex_lock(&sys->lock);
1356     char *chain_update = sys->filter_chain_update;
1357     sys->filter_chain_update = NULL;
1358     vlc_mutex_unlock(&sys->lock);
1359
1360     bool is_left_empty = false;
1361
1362     vlc_mutex_lock(&sys->filter_chain_lock);
1363     if (chain_update) {
1364         filter_chain_Reset(sys->filter_chain, NULL, NULL);
1365
1366         filter_chain_AppendFromString(spu->p->filter_chain, chain_update);
1367
1368         /* "sub-source"  was formerly "sub-filter", so now the "sub-filter"
1369         configuration may contain sub-filters or sub-sources configurations.
1370         if the filters chain was left empty it may indicate that it's a sub-source configuration */
1371         is_left_empty = (filter_chain_GetLength(spu->p->filter_chain) == 0);
1372     }
1373     vlc_mutex_unlock(&sys->filter_chain_lock);
1374
1375
1376     if (is_left_empty) {
1377         /* try to use the configuration as a sub-source configuration */
1378
1379         vlc_mutex_lock(&sys->lock);
1380         if (!sys->source_chain_update || !*sys->source_chain_update) {
1381             free(sys->source_chain_update);
1382             sys->source_chain_update = chain_update;
1383             chain_update = NULL;
1384         }
1385         vlc_mutex_unlock(&sys->lock);
1386     }
1387
1388     free(chain_update);
1389
1390     /* Run filter chain on the new subpicture */
1391     subpic = filter_chain_SubFilter(spu->p->filter_chain, subpic);
1392     if (!subpic)
1393         return;
1394
1395     /* SPU_DEFAULT_CHANNEL always reset itself */
1396     if (subpic->i_channel == SPU_DEFAULT_CHANNEL)
1397         spu_ClearChannel(spu, SPU_DEFAULT_CHANNEL);
1398
1399     /* p_private is for spu only and cannot be non NULL here */
1400     for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
1401         assert(r->p_private == NULL);
1402
1403     /* */
1404     vlc_mutex_lock(&sys->lock);
1405     if (SpuHeapPush(&sys->heap, subpic)) {
1406         vlc_mutex_unlock(&sys->lock);
1407         msg_Err(spu, "subpicture heap full");
1408         subpicture_Delete(subpic);
1409         return;
1410     }
1411     vlc_mutex_unlock(&sys->lock);
1412 }
1413
1414 subpicture_t *spu_Render(spu_t *spu,
1415                          const vlc_fourcc_t *chroma_list,
1416                          const video_format_t *fmt_dst,
1417                          const video_format_t *fmt_src,
1418                          mtime_t render_subtitle_date,
1419                          mtime_t render_osd_date,
1420                          bool ignore_osd)
1421 {
1422     spu_private_t *sys = spu->p;
1423
1424     /* Update sub-source chain */
1425     vlc_mutex_lock(&sys->lock);
1426     char *chain_update = sys->source_chain_update;
1427     sys->source_chain_update = NULL;
1428     vlc_mutex_unlock(&sys->lock);
1429
1430     vlc_mutex_lock(&sys->source_chain_lock);
1431     if (chain_update) {
1432         filter_chain_Reset(sys->source_chain, NULL, NULL);
1433
1434         filter_chain_AppendFromString(spu->p->source_chain, chain_update);
1435
1436         free(chain_update);
1437     }
1438     /* Run subpicture sources */
1439     filter_chain_SubSource(sys->source_chain, render_osd_date);
1440     vlc_mutex_unlock(&sys->source_chain_lock);
1441
1442     static const vlc_fourcc_t chroma_list_default_yuv[] = {
1443         VLC_CODEC_YUVA,
1444         VLC_CODEC_RGBA,
1445         VLC_CODEC_YUVP,
1446         0,
1447     };
1448     static const vlc_fourcc_t chroma_list_default_rgb[] = {
1449         VLC_CODEC_RGBA,
1450         VLC_CODEC_YUVA,
1451         VLC_CODEC_YUVP,
1452         0,
1453     };
1454
1455     if (!chroma_list || *chroma_list == 0)
1456         chroma_list = vlc_fourcc_IsYUV(fmt_dst->i_chroma) ? chroma_list_default_yuv
1457                                                           : chroma_list_default_rgb;
1458
1459     vlc_mutex_lock(&sys->lock);
1460
1461     unsigned int subpicture_count;
1462     subpicture_t *subpicture_array[VOUT_MAX_SUBPICTURES];
1463
1464     /* Get an array of subpictures to render */
1465     SpuSelectSubpictures(spu, &subpicture_count, subpicture_array,
1466                          render_subtitle_date, render_osd_date, ignore_osd);
1467     if (subpicture_count <= 0) {
1468         vlc_mutex_unlock(&sys->lock);
1469         return NULL;
1470     }
1471
1472     /* Updates the subpictures */
1473     for (unsigned i = 0; i < subpicture_count; i++) {
1474         subpicture_t *subpic = subpicture_array[i];
1475         subpicture_Update(subpic,
1476                           fmt_src, fmt_dst,
1477                           subpic->b_subtitle ? render_subtitle_date : render_osd_date);
1478     }
1479
1480     /* Now order the subpicture array
1481      * XXX The order is *really* important for overlap subtitles positionning */
1482     qsort(subpicture_array, subpicture_count, sizeof(*subpicture_array), SubpictureCmp);
1483
1484     /* Render the subpictures */
1485     subpicture_t *render = SpuRenderSubpictures(spu,
1486                                                 subpicture_count, subpicture_array,
1487                                                 chroma_list,
1488                                                 fmt_dst,
1489                                                 fmt_src,
1490                                                 render_subtitle_date,
1491                                                 render_osd_date);
1492     vlc_mutex_unlock(&sys->lock);
1493
1494     return render;
1495 }
1496
1497 void spu_OffsetSubtitleDate(spu_t *spu, mtime_t duration)
1498 {
1499     spu_private_t *sys = spu->p;
1500
1501     vlc_mutex_lock(&sys->lock);
1502     for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
1503         spu_heap_entry_t *entry = &sys->heap.entry[i];
1504         subpicture_t *current = entry->subpicture;
1505
1506         if (current && current->b_subtitle) {
1507             if (current->i_start > 0)
1508                 current->i_start += duration;
1509             if (current->i_stop > 0)
1510                 current->i_stop  += duration;
1511         }
1512     }
1513     vlc_mutex_unlock(&sys->lock);
1514 }
1515
1516 int spu_RegisterChannel(spu_t *spu)
1517 {
1518     spu_private_t *sys = spu->p;
1519
1520     vlc_mutex_lock(&sys->lock);
1521     int channel = sys->channel++;
1522     vlc_mutex_unlock(&sys->lock);
1523
1524     return channel;
1525 }
1526
1527 void spu_ClearChannel(spu_t *spu, int channel)
1528 {
1529     spu_private_t *sys = spu->p;
1530
1531     vlc_mutex_lock(&sys->lock);
1532
1533     for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
1534         spu_heap_entry_t *entry = &sys->heap.entry[i];
1535         subpicture_t *subpic = entry->subpicture;
1536
1537         if (!subpic)
1538             continue;
1539         if (subpic->i_channel != channel && (channel != -1 || subpic->i_channel == SPU_DEFAULT_CHANNEL))
1540             continue;
1541
1542         /* You cannot delete subpicture outside of spu_SortSubpictures */
1543         entry->reject = true;
1544     }
1545
1546     vlc_mutex_unlock(&sys->lock);
1547 }
1548
1549 void spu_ChangeSources(spu_t *spu, const char *filters)
1550 {
1551     spu_private_t *sys = spu->p;
1552
1553     vlc_mutex_lock(&sys->lock);
1554
1555     free(sys->source_chain_update);
1556     sys->source_chain_update = strdup(filters);
1557
1558     vlc_mutex_unlock(&sys->lock);
1559 }
1560
1561 void spu_ChangeFilters(spu_t *spu, const char *filters)
1562 {
1563     spu_private_t *sys = spu->p;
1564
1565     vlc_mutex_lock(&sys->lock);
1566
1567     free(sys->filter_chain_update);
1568     sys->filter_chain_update = strdup(filters);
1569
1570     vlc_mutex_unlock(&sys->lock);
1571 }
1572
1573 void spu_ChangeMargin(spu_t *spu, int margin)
1574 {
1575     spu_private_t *sys = spu->p;
1576
1577     vlc_mutex_lock(&sys->lock);
1578     sys->margin = margin;
1579     vlc_mutex_unlock(&sys->lock);
1580 }
1581