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