https: add support for HTTPS proxies
[vlc.git] / src / video_output / vout_subpictures.c
blob196038a61786a619e4eda383541873517c70e035
1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <assert.h>
34 #include <limits.h>
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>
43 #include "../libvlc.h"
44 #include "vout_internal.h"
45 #include "../misc/subpicture.h"
47 /*****************************************************************************
48 * Local prototypes
49 *****************************************************************************/
51 /* Number of simultaneous subpictures */
52 #define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
54 /* */
55 typedef struct {
56 subpicture_t *subpicture;
57 bool reject;
58 } spu_heap_entry_t;
60 typedef struct {
61 spu_heap_entry_t entry[VOUT_MAX_SUBPICTURES];
62 } spu_heap_t;
64 struct spu_private_t {
65 vlc_mutex_t lock; /* lock to protect all followings fields */
66 vlc_object_t *input;
68 spu_heap_t heap;
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 */
82 int margin; /**< force position of a subpicture */
83 bool force_palette; /**< force palette of subpicture */
84 uint8_t palette[4][4]; /**< forced palette */
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;
94 /* */
95 mtime_t last_sort_date;
98 /*****************************************************************************
99 * heap managment
100 *****************************************************************************/
101 static void SpuHeapInit(spu_heap_t *heap)
103 for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
104 spu_heap_entry_t *e = &heap->entry[i];
106 e->subpicture = NULL;
107 e->reject = false;
111 static int SpuHeapPush(spu_heap_t *heap, subpicture_t *subpic)
113 for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
114 spu_heap_entry_t *e = &heap->entry[i];
116 if (e->subpicture)
117 continue;
119 e->subpicture = subpic;
120 e->reject = false;
121 return VLC_SUCCESS;
123 return VLC_EGENERIC;
126 static void SpuHeapDeleteAt(spu_heap_t *heap, int index)
128 spu_heap_entry_t *e = &heap->entry[index];
130 if (e->subpicture)
131 subpicture_Delete(e->subpicture);
133 e->subpicture = NULL;
136 static int SpuHeapDeleteSubpicture(spu_heap_t *heap, subpicture_t *subpic)
138 for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
139 spu_heap_entry_t *e = &heap->entry[i];
141 if (e->subpicture != subpic)
142 continue;
144 SpuHeapDeleteAt(heap, i);
145 return VLC_SUCCESS;
147 return VLC_EGENERIC;
150 static void SpuHeapClean(spu_heap_t *heap)
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);
159 static void FilterRelease(filter_t *filter)
161 if (filter->p_module)
162 module_unneed(filter, filter->p_module);
163 vlc_object_release(filter);
166 static picture_t *spu_new_video_buffer(filter_t *filter)
168 const video_format_t *fmt = &filter->fmt_out.video;
170 return picture_NewFromFormat(fmt);
173 static int spu_get_attachments(filter_t *filter,
174 input_attachment_t ***attachment_ptr,
175 int *attachment_count)
177 spu_t *spu = filter->owner.sys;
179 int ret = VLC_EGENERIC;
180 if (spu->p->input)
181 ret = input_Control((input_thread_t*)spu->p->input,
182 INPUT_GET_ATTACHMENTS,
183 attachment_ptr, attachment_count);
184 return ret;
187 static filter_t *SpuRenderCreateAndLoadText(spu_t *spu)
189 filter_t *text = vlc_custom_create(spu, sizeof(*text), "spu text");
190 if (!text)
191 return NULL;
193 text->owner.sys = spu;
195 es_format_Init(&text->fmt_in, VIDEO_ES, 0);
197 es_format_Init(&text->fmt_out, VIDEO_ES, 0);
198 text->fmt_out.video.i_width =
199 text->fmt_out.video.i_visible_width = 32;
200 text->fmt_out.video.i_height =
201 text->fmt_out.video.i_visible_height = 32;
203 text->pf_get_attachments = spu_get_attachments;
205 text->p_module = module_need(text, "text renderer", "$text-renderer", false);
207 /* Create a few variables used for enhanced text rendering */
208 var_Create(text, "spu-elapsed", VLC_VAR_INTEGER);
209 var_Create(text, "text-rerender", VLC_VAR_BOOL);
211 return text;
214 static filter_t *SpuRenderCreateAndLoadScale(vlc_object_t *object,
215 vlc_fourcc_t src_chroma,
216 vlc_fourcc_t dst_chroma,
217 bool require_resize)
219 filter_t *scale = vlc_custom_create(object, sizeof(*scale), "scale");
220 if (!scale)
221 return NULL;
223 es_format_Init(&scale->fmt_in, VIDEO_ES, 0);
224 scale->fmt_in.video.i_chroma = src_chroma;
225 scale->fmt_in.video.i_width =
226 scale->fmt_in.video.i_visible_width =
227 scale->fmt_in.video.i_height =
228 scale->fmt_in.video.i_visible_height = 32;
230 es_format_Init(&scale->fmt_out, VIDEO_ES, 0);
231 scale->fmt_out.video.i_chroma = dst_chroma;
232 scale->fmt_out.video.i_width =
233 scale->fmt_out.video.i_visible_width =
234 scale->fmt_out.video.i_height =
235 scale->fmt_out.video.i_visible_height = require_resize ? 16 : 32;
237 scale->owner.video.buffer_new = spu_new_video_buffer;
239 scale->p_module = module_need(scale, "video filter2", NULL, false);
241 return scale;
244 static void SpuRenderText(spu_t *spu, bool *rerender_text,
245 subpicture_region_t *region,
246 const vlc_fourcc_t *chroma_list,
247 mtime_t elapsed_time)
249 filter_t *text = spu->p->text;
251 assert(region->fmt.i_chroma == VLC_CODEC_TEXT);
253 if (!text || !text->p_module)
254 return;
256 /* Setup 3 variables which can be used to render
257 * time-dependent text (and effects). The first indicates
258 * the total amount of time the text will be on screen,
259 * the second the amount of time it has already been on
260 * screen (can be a negative value as text is layed out
261 * before it is rendered) and the third is a feedback
262 * variable from the renderer - if the renderer sets it
263 * then this particular text is time-dependent, eg. the
264 * visual progress bar inside the text in karaoke and the
265 * text needs to be rendered multiple times in order for
266 * the effect to work - we therefore need to return the
267 * region to its original state at the end of the loop,
268 * instead of leaving it in YUVA or YUVP.
269 * Any renderer which is unaware of how to render
270 * time-dependent text can happily ignore the variables
271 * and render the text the same as usual - it should at
272 * least show up on screen, but the effect won't change
273 * the text over time.
275 var_SetInteger(text, "spu-elapsed", elapsed_time);
276 var_SetBool(text, "text-rerender", false);
278 if ( region->p_text )
279 text->pf_render(text, region, region, chroma_list);
280 *rerender_text = var_GetBool(text, "text-rerender");
284 * A few scale functions helpers.
287 #define SCALE_UNIT (1000)
288 typedef struct {
289 int w;
290 int h;
291 } spu_scale_t;
293 static spu_scale_t spu_scale_create(int w, int h)
295 spu_scale_t s = { .w = w, .h = h };
296 if (s.w <= 0)
297 s.w = SCALE_UNIT;
298 if (s.h <= 0)
299 s.h = SCALE_UNIT;
300 return s;
302 static spu_scale_t spu_scale_unit(void)
304 return spu_scale_create(SCALE_UNIT, SCALE_UNIT);
306 static spu_scale_t spu_scale_createq(int64_t wn, int64_t wd, int64_t hn, int64_t hd)
308 return spu_scale_create(wn * SCALE_UNIT / wd,
309 hn * SCALE_UNIT / hd);
311 static int spu_scale_w(int v, const spu_scale_t s)
313 return v * s.w / SCALE_UNIT;
315 static int spu_scale_h(int v, const spu_scale_t s)
317 return v * s.h / SCALE_UNIT;
319 static int spu_invscale_w(int v, const spu_scale_t s)
321 return v * SCALE_UNIT / s.w;
323 static int spu_invscale_h(int v, const spu_scale_t s)
325 return v * SCALE_UNIT / s.h;
329 * A few area functions helpers
331 typedef struct {
332 int x;
333 int y;
334 int width;
335 int height;
337 spu_scale_t scale;
338 } spu_area_t;
340 static spu_area_t spu_area_create(int x, int y, int w, int h, spu_scale_t s)
342 spu_area_t a = { .x = x, .y = y, .width = w, .height = h, .scale = s };
343 return a;
345 static spu_area_t spu_area_scaled(spu_area_t a)
347 if (a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT)
348 return a;
350 a.x = spu_scale_w(a.x, a.scale);
351 a.y = spu_scale_h(a.y, a.scale);
352 a.width = spu_scale_w(a.width, a.scale);
353 a.height = spu_scale_h(a.height, a.scale);
355 a.scale = spu_scale_unit();
356 return a;
358 static spu_area_t spu_area_unscaled(spu_area_t a, spu_scale_t s)
360 if (a.scale.w == s.w && a.scale.h == s.h)
361 return a;
363 a = spu_area_scaled(a);
365 a.x = spu_invscale_w(a.x, s);
366 a.y = spu_invscale_h(a.y, s);
367 a.width = spu_invscale_w(a.width, s);
368 a.height = spu_invscale_h(a.height, s);
370 a.scale = s;
371 return a;
373 static bool spu_area_overlap(spu_area_t a, spu_area_t b)
375 const int dx = 0;
376 const int dy = 0;
378 a = spu_area_scaled(a);
379 b = spu_area_scaled(b);
381 return __MAX(a.x - dx, b.x) < __MIN(a.x + a.width + dx, b.x + b.width ) &&
382 __MAX(a.y - dy, b.y) < __MIN(a.y + a.height + dy, b.y + b.height);
386 * Avoid area overlapping
388 static void SpuAreaFixOverlap(spu_area_t *dst,
389 const spu_area_t *sub_array, int sub_count, int align)
391 spu_area_t a = spu_area_scaled(*dst);
392 bool is_moved = false;
393 bool is_ok;
395 /* Check for overlap
396 * XXX It is not fast O(n^2) but we should not have a lot of region */
397 do {
398 is_ok = true;
399 for (int i = 0; i < sub_count; i++) {
400 spu_area_t sub = spu_area_scaled(sub_array[i]);
402 if (!spu_area_overlap(a, sub))
403 continue;
405 if (align & SUBPICTURE_ALIGN_TOP) {
406 /* We go down */
407 int i_y = sub.y + sub.height;
408 a.y = i_y;
409 is_moved = true;
410 } else if (align & SUBPICTURE_ALIGN_BOTTOM) {
411 /* We go up */
412 int i_y = sub.y - a.height;
413 a.y = i_y;
414 is_moved = true;
415 } else {
416 /* TODO what to do in this case? */
417 //fprintf(stderr, "Overlap with unsupported alignment\n");
418 break;
421 is_ok = false;
422 break;
424 } while (!is_ok);
426 if (is_moved)
427 *dst = spu_area_unscaled(a, dst->scale);
431 static void SpuAreaFitInside(spu_area_t *area, const spu_area_t *boundary)
433 spu_area_t a = spu_area_scaled(*area);
435 const int i_error_x = (a.x + a.width) - boundary->width;
436 if (i_error_x > 0)
437 a.x -= i_error_x;
438 if (a.x < 0)
439 a.x = 0;
441 const int i_error_y = (a.y + a.height) - boundary->height;
442 if (i_error_y > 0)
443 a.y -= i_error_y;
444 if (a.y < 0)
445 a.y = 0;
447 *area = spu_area_unscaled(a, area->scale);
451 * Place a region
453 static void SpuRegionPlace(int *x, int *y,
454 const subpicture_t *subpic,
455 const subpicture_region_t *region)
457 assert(region->i_x != INT_MAX && region->i_y != INT_MAX);
458 if (subpic->b_absolute) {
459 *x = region->i_x;
460 *y = region->i_y;
461 } else {
462 if (region->i_align & SUBPICTURE_ALIGN_TOP)
463 *y = region->i_y;
464 else if (region->i_align & SUBPICTURE_ALIGN_BOTTOM)
465 *y = subpic->i_original_picture_height - region->fmt.i_visible_height - region->i_y;
466 else
467 *y = subpic->i_original_picture_height / 2 - region->fmt.i_visible_height / 2;
469 if (region->i_align & SUBPICTURE_ALIGN_LEFT)
470 *x = region->i_x;
471 else if (region->i_align & SUBPICTURE_ALIGN_RIGHT)
472 *x = subpic->i_original_picture_width - region->fmt.i_visible_width - region->i_x;
473 else
474 *x = subpic->i_original_picture_width / 2 - region->fmt.i_visible_width / 2;
479 * This function compares two 64 bits integers.
480 * It can be used by qsort.
482 static int IntegerCmp(int64_t i0, int64_t i1)
484 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
487 * This function compares 2 subpictures using the following properties
488 * (ordered by priority)
489 * 1. absolute positionning
490 * 2. start time
491 * 3. creation order (per channel)
493 * It can be used by qsort.
495 * XXX spu_RenderSubpictures depends heavily on this order.
497 static int SubpictureCmp(const void *s0, const void *s1)
499 subpicture_t *subpic0 = *(subpicture_t**)s0;
500 subpicture_t *subpic1 = *(subpicture_t**)s1;
501 int r;
503 r = IntegerCmp(!subpic0->b_absolute, !subpic1->b_absolute);
504 if (!r)
505 r = IntegerCmp(subpic0->i_start, subpic1->i_start);
506 if (!r)
507 r = IntegerCmp(subpic0->i_channel, subpic1->i_channel);
508 if (!r)
509 r = IntegerCmp(subpic0->i_order, subpic1->i_order);
510 return r;
513 /*****************************************************************************
514 * SpuSelectSubpictures: find the subpictures to display
515 *****************************************************************************
516 * This function parses all subpictures and decides which ones need to be
517 * displayed. If no picture has been selected, display_date will depend on
518 * the subpicture.
519 * We also check for ephemer DVD subpictures (subpictures that have
520 * to be removed if a newer one is available), which makes it a lot
521 * more difficult to guess if a subpicture has to be rendered or not.
522 *****************************************************************************/
523 static void SpuSelectSubpictures(spu_t *spu,
524 unsigned int *subpicture_count,
525 subpicture_t **subpicture_array,
526 mtime_t render_subtitle_date,
527 mtime_t render_osd_date,
528 bool ignore_osd)
530 spu_private_t *sys = spu->p;
532 /* */
533 *subpicture_count = 0;
535 /* Create a list of channels */
536 int channel[VOUT_MAX_SUBPICTURES];
537 int channel_count = 0;
539 for (int index = 0; index < VOUT_MAX_SUBPICTURES; index++) {
540 spu_heap_entry_t *entry = &sys->heap.entry[index];
541 if (!entry->subpicture || entry->reject)
542 continue;
543 const int i_channel = entry->subpicture->i_channel;
544 int i;
545 for (i = 0; i < channel_count; i++) {
546 if (channel[i] == i_channel)
547 break;
549 if (channel_count <= i)
550 channel[channel_count++] = i_channel;
553 /* Fill up the subpicture_array arrays with relevent pictures */
554 for (int i = 0; i < channel_count; i++) {
555 subpicture_t *available_subpic[VOUT_MAX_SUBPICTURES];
556 bool is_available_late[VOUT_MAX_SUBPICTURES];
557 int available_count = 0;
559 mtime_t start_date = render_subtitle_date;
560 mtime_t ephemer_subtitle_date = 0;
561 mtime_t ephemer_osd_date = 0;
562 int64_t ephemer_subtitle_order = INT64_MIN;
563 int64_t ephemer_system_order = INT64_MIN;
565 /* Select available pictures */
566 for (int index = 0; index < VOUT_MAX_SUBPICTURES; index++) {
567 spu_heap_entry_t *entry = &sys->heap.entry[index];
568 subpicture_t *current = entry->subpicture;
569 bool is_stop_valid;
570 bool is_late;
572 if (!current || entry->reject) {
573 if (entry->reject)
574 SpuHeapDeleteAt(&sys->heap, index);
575 continue;
578 if (current->i_channel != channel[i] ||
579 (ignore_osd && !current->b_subtitle))
580 continue;
582 const mtime_t render_date = current->b_subtitle ? render_subtitle_date : render_osd_date;
583 if (render_date &&
584 render_date < current->i_start) {
585 /* Too early, come back next monday */
586 continue;
589 mtime_t *ephemer_date_ptr = current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
590 int64_t *ephemer_order_ptr = current->b_subtitle ? &ephemer_subtitle_order : &ephemer_system_order;
591 if (current->i_start >= *ephemer_date_ptr) {
592 *ephemer_date_ptr = current->i_start;
593 if (current->i_order > *ephemer_order_ptr)
594 *ephemer_order_ptr = current->i_order;
597 is_stop_valid = !current->b_ephemer || current->i_stop > current->i_start;
599 is_late = is_stop_valid && current->i_stop <= render_date;
601 /* start_date will be used for correct automatic overlap support
602 * in case picture that should not be displayed anymore (display_time)
603 * overlap with a picture to be displayed (current->i_start) */
604 if (current->b_subtitle && !is_late && !current->b_ephemer)
605 start_date = current->i_start;
607 /* */
608 available_subpic[available_count] = current;
609 is_available_late[available_count] = is_late;
610 available_count++;
613 /* Only forced old picture display at the transition */
614 if (start_date < sys->last_sort_date)
615 start_date = sys->last_sort_date;
616 if (start_date <= 0)
617 start_date = INT64_MAX;
619 /* Select pictures to be displayed */
620 for (int index = 0; index < available_count; index++) {
621 subpicture_t *current = available_subpic[index];
622 bool is_late = is_available_late[index];
624 const mtime_t stop_date = current->b_subtitle ? __MAX(start_date, sys->last_sort_date) : render_osd_date;
625 const mtime_t ephemer_date = current->b_subtitle ? ephemer_subtitle_date : ephemer_osd_date;
626 const int64_t ephemer_order = current->b_subtitle ? ephemer_subtitle_order : ephemer_system_order;
628 /* Destroy late and obsolete ephemer subpictures */
629 bool is_rejeted = is_late && current->i_stop <= stop_date;
630 if (current->b_ephemer) {
631 if (current->i_start < ephemer_date)
632 is_rejeted = true;
633 else if (current->i_start == ephemer_date &&
634 current->i_order < ephemer_order)
635 is_rejeted = true;
638 if (is_rejeted)
639 SpuHeapDeleteSubpicture(&sys->heap, current);
640 else
641 subpicture_array[(*subpicture_count)++] = current;
645 sys->last_sort_date = render_subtitle_date;
651 * It will transform the provided region into another region suitable for rendering.
653 static void SpuRenderRegion(spu_t *spu,
654 subpicture_region_t **dst_ptr, spu_area_t *dst_area,
655 subpicture_t *subpic, subpicture_region_t *region,
656 const spu_scale_t scale_size,
657 const vlc_fourcc_t *chroma_list,
658 const video_format_t *fmt,
659 const spu_area_t *subtitle_area, int subtitle_area_count,
660 mtime_t render_date)
662 spu_private_t *sys = spu->p;
664 video_format_t fmt_original = region->fmt;
665 bool restore_text = false;
666 int x_offset;
667 int y_offset;
669 video_format_t region_fmt;
670 picture_t *region_picture;
672 /* Invalidate area by default */
673 *dst_area = spu_area_create(0,0, 0,0, scale_size);
674 *dst_ptr = NULL;
676 /* Render text region */
677 if (region->fmt.i_chroma == VLC_CODEC_TEXT) {
678 SpuRenderText(spu, &restore_text, region,
679 chroma_list,
680 render_date - subpic->i_start);
682 /* Check if the rendering has failed ... */
683 if (region->fmt.i_chroma == VLC_CODEC_TEXT)
684 goto exit;
687 /* Force palette if requested
688 * FIXME b_force_palette and force_crop are applied to all subpictures using palette
689 * instead of only the right one (being the dvd spu).
691 const bool using_palette = region->fmt.i_chroma == VLC_CODEC_YUVP;
692 const bool force_palette = using_palette && sys->force_palette;
693 const bool force_crop = force_palette && sys->force_crop;
694 bool changed_palette = false;
696 /* Compute the margin which is expressed in destination pixel unit
697 * The margin is applied only to subtitle and when no forced crop is
698 * requested (dvd menu) */
699 int y_margin = 0;
700 if (!force_crop && subpic->b_subtitle)
701 y_margin = spu_invscale_h(sys->margin, scale_size);
703 /* Place the picture
704 * We compute the position in the rendered size */
705 SpuRegionPlace(&x_offset, &y_offset,
706 subpic, region);
708 /* Save this position for subtitle overlap support
709 * it is really important that there are given without scale_size applied */
710 *dst_area = spu_area_create(x_offset, y_offset,
711 region->fmt.i_visible_width,
712 region->fmt.i_visible_height,
713 scale_size);
715 /* Handle overlapping subtitles when possible */
716 if (subpic->b_subtitle && !subpic->b_absolute)
717 SpuAreaFixOverlap(dst_area, subtitle_area, subtitle_area_count,
718 region->i_align);
720 /* we copy the area: for the subtitle overlap support we want
721 * to only save the area without margin applied */
722 spu_area_t restrained = *dst_area;
724 /* apply margin to subtitles and correct if they go over the picture edge */
725 if (subpic->b_subtitle)
726 restrained.y -= y_margin;
728 spu_area_t display = spu_area_create(0, 0, fmt->i_visible_width,
729 fmt->i_visible_height,
730 spu_scale_unit());
731 //fprintf("
732 SpuAreaFitInside(&restrained, &display);
734 /* Fix the position for the current scale_size */
735 x_offset = spu_scale_w(restrained.x, restrained.scale);
736 y_offset = spu_scale_h(restrained.y, restrained.scale);
738 /* */
739 if (force_palette) {
740 video_palette_t *old_palette = region->fmt.p_palette;
741 video_palette_t new_palette;
743 /* We suppose DVD palette here */
744 new_palette.i_entries = 4;
745 for (int i = 0; i < 4; i++)
746 for (int j = 0; j < 4; j++)
747 new_palette.palette[i][j] = sys->palette[i][j];
749 if (old_palette->i_entries == new_palette.i_entries) {
750 for (int i = 0; i < old_palette->i_entries; i++)
751 for (int j = 0; j < 4; j++)
752 changed_palette |= old_palette->palette[i][j] != new_palette.palette[i][j];
753 } else {
754 changed_palette = true;
756 *old_palette = new_palette;
759 /* */
760 region_fmt = region->fmt;
761 region_picture = region->p_picture;
763 bool convert_chroma = true;
764 for (int i = 0; chroma_list[i] && convert_chroma; i++) {
765 if (region_fmt.i_chroma == chroma_list[i])
766 convert_chroma = false;
769 /* Scale from rendered size to destination size */
770 if (sys->scale && sys->scale->p_module &&
771 (!using_palette || (sys->scale_yuvp && sys->scale_yuvp->p_module)) &&
772 (scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT ||
773 using_palette || convert_chroma)) {
774 const unsigned dst_width = spu_scale_w(region->fmt.i_visible_width, scale_size);
775 const unsigned dst_height = spu_scale_h(region->fmt.i_visible_height, scale_size);
777 /* Destroy the cache if unusable */
778 if (region->p_private) {
779 subpicture_region_private_t *private = region->p_private;
780 bool is_changed = false;
782 /* Check resize changes */
783 if (dst_width != private->fmt.i_visible_width ||
784 dst_height != private->fmt.i_visible_height)
785 is_changed = true;
787 /* Check forced palette changes */
788 if (changed_palette)
789 is_changed = true;
791 if (convert_chroma && private->fmt.i_chroma != chroma_list[0])
792 is_changed = true;
794 if (is_changed) {
795 subpicture_region_private_Delete(private);
796 region->p_private = NULL;
800 /* Scale if needed into cache */
801 if (!region->p_private && dst_width > 0 && dst_height > 0) {
802 filter_t *scale = sys->scale;
804 picture_t *picture = region->p_picture;
805 picture_Hold(picture);
807 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
808 if (using_palette) {
809 filter_t *scale_yuvp = sys->scale_yuvp;
811 scale_yuvp->fmt_in.video = region->fmt;
813 scale_yuvp->fmt_out.video = region->fmt;
814 scale_yuvp->fmt_out.video.i_chroma = chroma_list[0];
816 picture = scale_yuvp->pf_video_filter(scale_yuvp, picture);
817 if (!picture) {
818 /* Well we will try conversion+scaling */
819 msg_Warn(spu, "%4.4s to %4.4s conversion failed",
820 (const char*)&scale_yuvp->fmt_in.video.i_chroma,
821 (const char*)&scale_yuvp->fmt_out.video.i_chroma);
825 /* Conversion(except from YUVP)/Scaling */
826 if (picture &&
827 (picture->format.i_visible_width != dst_width ||
828 picture->format.i_visible_height != dst_height ||
829 (convert_chroma && !using_palette)))
831 scale->fmt_in.video = picture->format;
832 scale->fmt_out.video = picture->format;
833 if (using_palette)
834 scale->fmt_in.video.i_chroma = chroma_list[0];
835 if (convert_chroma)
836 scale->fmt_out.i_codec =
837 scale->fmt_out.video.i_chroma = chroma_list[0];
839 scale->fmt_out.video.i_width = dst_width;
840 scale->fmt_out.video.i_height = dst_height;
842 scale->fmt_out.video.i_visible_width =
843 spu_scale_w(region->fmt.i_visible_width, scale_size);
844 scale->fmt_out.video.i_visible_height =
845 spu_scale_h(region->fmt.i_visible_height, scale_size);
847 picture = scale->pf_video_filter(scale, picture);
848 if (!picture)
849 msg_Err(spu, "scaling failed");
852 /* */
853 if (picture) {
854 region->p_private = subpicture_region_private_New(&picture->format);
855 if (region->p_private) {
856 region->p_private->p_picture = picture;
857 if (!region->p_private->p_picture) {
858 subpicture_region_private_Delete(region->p_private);
859 region->p_private = NULL;
861 } else {
862 picture_Release(picture);
867 /* And use the scaled picture */
868 if (region->p_private) {
869 region_fmt = region->p_private->fmt;
870 region_picture = region->p_private->p_picture;
874 /* Force cropping if requested */
875 if (force_crop) {
876 int crop_x = spu_scale_w(sys->crop.x, scale_size);
877 int crop_y = spu_scale_h(sys->crop.y, scale_size);
878 int crop_width = spu_scale_w(sys->crop.width, scale_size);
879 int crop_height= spu_scale_h(sys->crop.height,scale_size);
881 /* Find the intersection */
882 if (crop_x + crop_width <= x_offset ||
883 x_offset + (int)region_fmt.i_visible_width < crop_x ||
884 crop_y + crop_height <= y_offset ||
885 y_offset + (int)region_fmt.i_visible_height < crop_y) {
886 /* No intersection */
887 region_fmt.i_visible_width =
888 region_fmt.i_visible_height = 0;
889 } else {
890 int x, y, x_end, y_end;
891 x = __MAX(crop_x, x_offset);
892 y = __MAX(crop_y, y_offset);
893 x_end = __MIN(crop_x + crop_width,
894 x_offset + (int)region_fmt.i_visible_width);
895 y_end = __MIN(crop_y + crop_height,
896 y_offset + (int)region_fmt.i_visible_height);
898 region_fmt.i_x_offset = x - x_offset;
899 region_fmt.i_y_offset = y - y_offset;
900 region_fmt.i_visible_width = x_end - x;
901 region_fmt.i_visible_height = y_end - y;
903 x_offset = __MAX(x, 0);
904 y_offset = __MAX(y, 0);
908 subpicture_region_t *dst = *dst_ptr = subpicture_region_New(&region_fmt);
909 if (dst) {
910 dst->i_x = x_offset;
911 dst->i_y = y_offset;
912 dst->i_align = 0;
913 if (dst->p_picture)
914 picture_Release(dst->p_picture);
915 dst->p_picture = picture_Hold(region_picture);
916 int fade_alpha = 255;
917 if (subpic->b_fade) {
918 mtime_t fade_start = subpic->i_start + 3 * (subpic->i_stop - subpic->i_start) / 4;
920 if (fade_start <= render_date && fade_start < subpic->i_stop)
921 fade_alpha = 255 * (subpic->i_stop - render_date) /
922 (subpic->i_stop - fade_start);
924 dst->i_alpha = fade_alpha * subpic->i_alpha * region->i_alpha / 65025;
927 exit:
928 if (restore_text) {
929 /* Some forms of subtitles need to be re-rendered more than
930 * once, eg. karaoke. We therefore restore the region to its
931 * pre-rendered state, so the next time through everything is
932 * calculated again.
934 if (region->p_picture) {
935 picture_Release(region->p_picture);
936 region->p_picture = NULL;
938 if (region->p_private) {
939 subpicture_region_private_Delete(region->p_private);
940 region->p_private = NULL;
942 region->fmt = fmt_original;
947 * This function renders all sub picture units in the list.
949 static subpicture_t *SpuRenderSubpictures(spu_t *spu,
950 unsigned int i_subpicture,
951 subpicture_t **pp_subpicture,
952 const vlc_fourcc_t *chroma_list,
953 const video_format_t *fmt_dst,
954 const video_format_t *fmt_src,
955 mtime_t render_subtitle_date,
956 mtime_t render_osd_date)
958 spu_private_t *sys = spu->p;
960 /* Count the number of regions and subtitle regions */
961 unsigned int subtitle_region_count = 0;
962 unsigned int region_count = 0;
963 for (unsigned i = 0; i < i_subpicture; i++) {
964 const subpicture_t *subpic = pp_subpicture[i];
966 unsigned count = 0;
967 for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
968 count++;
970 if (subpic->b_subtitle)
971 subtitle_region_count += count;
972 region_count += count;
974 if (region_count <= 0)
975 return NULL;
977 /* Create the output subpicture */
978 subpicture_t *output = subpicture_New(NULL);
979 if (!output)
980 return NULL;
981 output->i_order = pp_subpicture[i_subpicture - 1]->i_order;
982 output->i_original_picture_width = fmt_dst->i_visible_width;
983 output->i_original_picture_height = fmt_dst->i_visible_height;
984 subpicture_region_t **output_last_ptr = &output->p_region;
986 /* Allocate area array for subtitle overlap */
987 spu_area_t subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
988 spu_area_t *subtitle_area;
989 int subtitle_area_count;
991 subtitle_area_count = 0;
992 subtitle_area = subtitle_area_buffer;
993 if (subtitle_region_count > sizeof(subtitle_area_buffer)/sizeof(*subtitle_area_buffer))
994 subtitle_area = calloc(subtitle_region_count, sizeof(*subtitle_area));
996 /* Process all subpictures and regions (in the right order) */
997 for (unsigned int index = 0; index < i_subpicture; index++) {
998 subpicture_t *subpic = pp_subpicture[index];
999 subpicture_region_t *region;
1001 if (!subpic->p_region)
1002 continue;
1004 if (subpic->i_original_picture_width <= 0 ||
1005 subpic->i_original_picture_height <= 0) {
1006 if (subpic->i_original_picture_width > 0 ||
1007 subpic->i_original_picture_height > 0)
1008 msg_Err(spu, "original picture size %dx%d is unsupported",
1009 subpic->i_original_picture_width,
1010 subpic->i_original_picture_height);
1011 else
1012 msg_Warn(spu, "original picture size is undefined");
1014 subpic->i_original_picture_width = fmt_src->i_visible_width;
1015 subpic->i_original_picture_height = fmt_src->i_visible_height;
1018 if (sys->text) {
1019 /* FIXME aspect ratio ? */
1020 sys->text->fmt_out.video.i_width =
1021 sys->text->fmt_out.video.i_visible_width = subpic->i_original_picture_width;
1023 sys->text->fmt_out.video.i_height =
1024 sys->text->fmt_out.video.i_visible_height = subpic->i_original_picture_height;
1027 /* Render all regions
1028 * We always transform non absolute subtitle into absolute one on the
1029 * first rendering to allow good subtitle overlap support.
1031 for (region = subpic->p_region; region != NULL; region = region->p_next) {
1032 spu_area_t area;
1034 /* Compute region scale AR */
1035 video_format_t region_fmt = region->fmt;
1036 if (region_fmt.i_sar_num <= 0 || region_fmt.i_sar_den <= 0) {
1037 region_fmt.i_sar_num = (int64_t)fmt_dst->i_visible_width * fmt_dst->i_sar_num * subpic->i_original_picture_height;
1038 region_fmt.i_sar_den = (int64_t)fmt_dst->i_visible_height * fmt_dst->i_sar_den * subpic->i_original_picture_width;
1039 vlc_ureduce(&region_fmt.i_sar_num, &region_fmt.i_sar_den,
1040 region_fmt.i_sar_num, region_fmt.i_sar_den, 65536);
1043 /* Compute scaling from original size to destination size
1044 * FIXME The current scaling ensure that the heights match, the width being
1045 * cropped.
1047 spu_scale_t scale = spu_scale_createq((int64_t)fmt_dst->i_visible_height * fmt_dst->i_sar_den * region_fmt.i_sar_num,
1048 (int64_t)subpic->i_original_picture_height * fmt_dst->i_sar_num * region_fmt.i_sar_den,
1049 fmt_dst->i_visible_height,
1050 subpic->i_original_picture_height);
1052 /* Check scale validity */
1053 if (scale.w <= 0 || scale.h <= 0)
1054 continue;
1056 /* */
1057 SpuRenderRegion(spu, output_last_ptr, &area,
1058 subpic, region, scale,
1059 chroma_list, fmt_dst,
1060 subtitle_area, subtitle_area_count,
1061 subpic->b_subtitle ? render_subtitle_date : render_osd_date);
1062 if (*output_last_ptr)
1063 output_last_ptr = &(*output_last_ptr)->p_next;
1065 if (subpic->b_subtitle) {
1066 area = spu_area_unscaled(area, scale);
1067 if (!subpic->b_absolute && area.width > 0 && area.height > 0) {
1068 region->i_x = area.x;
1069 region->i_y = area.y;
1071 if (subtitle_area)
1072 subtitle_area[subtitle_area_count++] = area;
1075 if (subpic->b_subtitle && subpic->p_region)
1076 subpic->b_absolute = true;
1079 /* */
1080 if (subtitle_area != subtitle_area_buffer)
1081 free(subtitle_area);
1083 return output;
1086 /*****************************************************************************
1087 * Object variables callbacks
1088 *****************************************************************************/
1090 /*****************************************************************************
1091 * UpdateSPU: update subpicture settings
1092 *****************************************************************************
1093 * This function is called from CropCallback and at initialization time, to
1094 * retrieve crop information from the input.
1095 *****************************************************************************/
1096 static void UpdateSPU(spu_t *spu, vlc_object_t *object)
1098 spu_private_t *sys = spu->p;
1099 vlc_value_t val;
1101 vlc_mutex_lock(&sys->lock);
1103 sys->force_palette = false;
1104 sys->force_crop = false;
1106 if (var_Get(object, "highlight", &val) || !val.b_bool) {
1107 vlc_mutex_unlock(&sys->lock);
1108 return;
1111 sys->force_crop = true;
1112 sys->crop.x = var_GetInteger(object, "x-start");
1113 sys->crop.y = var_GetInteger(object, "y-start");
1114 sys->crop.width = var_GetInteger(object, "x-end") - sys->crop.x;
1115 sys->crop.height = var_GetInteger(object, "y-end") - sys->crop.y;
1117 if (var_Get(object, "menu-palette", &val) == VLC_SUCCESS) {
1118 memcpy(sys->palette, val.p_address, 16);
1119 sys->force_palette = true;
1121 vlc_mutex_unlock(&sys->lock);
1123 msg_Dbg(object, "crop: %i,%i,%i,%i, palette forced: %i",
1124 sys->crop.x, sys->crop.y,
1125 sys->crop.width, sys->crop.height,
1126 sys->force_palette);
1129 /*****************************************************************************
1130 * CropCallback: called when the highlight properties are changed
1131 *****************************************************************************
1132 * This callback is called from the input thread when we need cropping
1133 *****************************************************************************/
1134 static int CropCallback(vlc_object_t *object, char const *var,
1135 vlc_value_t oldval, vlc_value_t newval, void *data)
1137 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(var);
1139 UpdateSPU((spu_t *)data, object);
1140 return VLC_SUCCESS;
1143 /*****************************************************************************
1144 * Buffers allocation callbacks for the filters
1145 *****************************************************************************/
1147 static subpicture_t *sub_new_buffer(filter_t *filter)
1149 int channel = (intptr_t)filter->owner.sys;
1151 subpicture_t *subpicture = subpicture_New(NULL);
1152 if (subpicture)
1153 subpicture->i_channel = channel;
1154 return subpicture;
1157 static int SubSourceInit(filter_t *filter, void *data)
1159 spu_t *spu = data;
1160 int channel = spu_RegisterChannel(spu);
1162 filter->owner.sys = (void *)(intptr_t)channel;
1163 filter->owner.sub.buffer_new = sub_new_buffer;
1164 return VLC_SUCCESS;
1167 static int SubSourceClean(filter_t *filter, void *data)
1169 spu_t *spu = data;
1170 int channel = (intptr_t)filter->owner.sys;
1172 spu_ClearChannel(spu, channel);
1173 return VLC_SUCCESS;
1176 /*****************************************************************************
1177 * Public API
1178 *****************************************************************************/
1180 #undef spu_Create
1182 * Creates the subpicture unit
1184 * \param p_this the parent object which creates the subpicture unit
1186 spu_t *spu_Create(vlc_object_t *object)
1188 spu_t *spu = vlc_custom_create(object,
1189 sizeof(spu_t) + sizeof(spu_private_t),
1190 "subpicture");
1191 if (!spu)
1192 return NULL;
1194 /* Initialize spu fields */
1195 spu_private_t *sys = spu->p = (spu_private_t*)&spu[1];
1197 /* Initialize private fields */
1198 vlc_mutex_init(&sys->lock);
1200 SpuHeapInit(&sys->heap);
1202 sys->text = NULL;
1203 sys->scale = NULL;
1204 sys->scale_yuvp = NULL;
1206 sys->margin = var_InheritInteger(spu, "sub-margin");
1208 /* Register the default subpicture channel */
1209 sys->channel = SPU_DEFAULT_CHANNEL + 1;
1211 sys->source_chain_update = NULL;
1212 sys->filter_chain_update = NULL;
1213 vlc_mutex_init(&sys->source_chain_lock);
1214 vlc_mutex_init(&sys->filter_chain_lock);
1215 sys->source_chain = filter_chain_New(spu, "sub source", false);
1216 sys->filter_chain = filter_chain_New(spu, "sub filter", false);
1218 /* Load text and scale module */
1219 sys->text = SpuRenderCreateAndLoadText(spu);
1221 /* XXX spu->p_scale is used for all conversion/scaling except yuvp to
1222 * yuva/rgba */
1223 sys->scale = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1224 VLC_CODEC_YUVA, VLC_CODEC_RGBA, true);
1226 /* This one is used for YUVP to YUVA/RGBA without scaling
1227 * FIXME rename it */
1228 sys->scale_yuvp = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1229 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false);
1231 /* */
1232 sys->last_sort_date = -1;
1234 return spu;
1238 * Destroy the subpicture unit
1240 * \param p_this the parent object which destroys the subpicture unit
1242 void spu_Destroy(spu_t *spu)
1244 spu_private_t *sys = spu->p;
1246 if (sys->text)
1247 FilterRelease(sys->text);
1249 if (sys->scale_yuvp)
1250 FilterRelease(sys->scale_yuvp);
1252 if (sys->scale)
1253 FilterRelease(sys->scale);
1255 filter_chain_ForEach(sys->source_chain, SubSourceClean, spu);
1256 filter_chain_Delete(sys->source_chain);
1257 filter_chain_Delete(sys->filter_chain);
1258 vlc_mutex_destroy(&sys->source_chain_lock);
1259 vlc_mutex_destroy(&sys->filter_chain_lock);
1260 free(sys->source_chain_update);
1261 free(sys->filter_chain_update);
1263 /* Destroy all remaining subpictures */
1264 SpuHeapClean(&sys->heap);
1266 vlc_mutex_destroy(&sys->lock);
1268 vlc_object_release(spu);
1272 * Attach/Detach the SPU from any input
1274 * \param p_this the object in which to destroy the subpicture unit
1275 * \param b_attach to select attach or detach
1277 void spu_Attach(spu_t *spu, vlc_object_t *input, bool attach)
1279 if (attach) {
1280 UpdateSPU(spu, input);
1281 var_Create(input, "highlight", VLC_VAR_BOOL);
1282 var_AddCallback(input, "highlight", CropCallback, spu);
1284 vlc_mutex_lock(&spu->p->lock);
1285 spu->p->input = input;
1287 if (spu->p->text)
1288 FilterRelease(spu->p->text);
1289 spu->p->text = SpuRenderCreateAndLoadText(spu);
1291 vlc_mutex_unlock(&spu->p->lock);
1292 } else {
1293 vlc_mutex_lock(&spu->p->lock);
1294 spu->p->input = NULL;
1295 vlc_mutex_unlock(&spu->p->lock);
1297 /* Delete callbacks */
1298 var_DelCallback(input, "highlight", CropCallback, spu);
1299 var_Destroy(input, "highlight");
1304 * Inform the SPU filters of mouse event
1306 int spu_ProcessMouse(spu_t *spu,
1307 const vlc_mouse_t *mouse,
1308 const video_format_t *fmt)
1310 spu_private_t *sys = spu->p;
1312 vlc_mutex_lock(&sys->source_chain_lock);
1313 filter_chain_MouseEvent(sys->source_chain, mouse, fmt);
1314 vlc_mutex_unlock(&sys->source_chain_lock);
1316 return VLC_SUCCESS;
1320 * Display a subpicture
1322 * Remove the reservation flag of a subpicture, which will cause it to be
1323 * ready for display.
1324 * \param spu the subpicture unit object
1325 * \param subpic the subpicture to display
1327 void spu_PutSubpicture(spu_t *spu, subpicture_t *subpic)
1329 spu_private_t *sys = spu->p;
1331 /* Update sub-filter chain */
1332 vlc_mutex_lock(&sys->lock);
1333 char *chain_update = sys->filter_chain_update;
1334 sys->filter_chain_update = NULL;
1335 vlc_mutex_unlock(&sys->lock);
1337 bool is_left_empty = false;
1339 vlc_mutex_lock(&sys->filter_chain_lock);
1340 if (chain_update) {
1341 if (*chain_update) {
1342 filter_chain_Reset(sys->filter_chain, NULL, NULL);
1344 filter_chain_AppendFromString(spu->p->filter_chain, chain_update);
1346 else if (filter_chain_GetLength(spu->p->filter_chain) > 0)
1347 filter_chain_Reset(sys->filter_chain, NULL, NULL);
1349 /* "sub-source" was formerly "sub-filter", so now the "sub-filter"
1350 configuration may contain sub-filters or sub-sources configurations.
1351 if the filters chain was left empty it may indicate that it's a sub-source configuration */
1352 is_left_empty = (filter_chain_GetLength(spu->p->filter_chain) == 0);
1354 vlc_mutex_unlock(&sys->filter_chain_lock);
1356 if (is_left_empty) {
1357 /* try to use the configuration as a sub-source configuration,
1358 but only if there is no 'source_chain_update' value and
1359 if only if 'chain_update' has a value */
1360 if (chain_update && *chain_update) {
1361 vlc_mutex_lock(&sys->lock);
1362 if (!sys->source_chain_update || !*sys->source_chain_update) {
1363 if (sys->source_chain_update)
1364 free(sys->source_chain_update);
1365 sys->source_chain_update = chain_update;
1366 chain_update = NULL;
1368 vlc_mutex_unlock(&sys->lock);
1372 free(chain_update);
1374 /* Run filter chain on the new subpicture */
1375 vlc_mutex_lock(&sys->filter_chain_lock);
1376 subpic = filter_chain_SubFilter(spu->p->filter_chain, subpic);
1377 vlc_mutex_unlock(&sys->filter_chain_lock);
1378 if (!subpic)
1379 return;
1381 /* SPU_DEFAULT_CHANNEL always reset itself */
1382 if (subpic->i_channel == SPU_DEFAULT_CHANNEL)
1383 spu_ClearChannel(spu, SPU_DEFAULT_CHANNEL);
1385 /* p_private is for spu only and cannot be non NULL here */
1386 for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
1387 assert(r->p_private == NULL);
1389 /* */
1390 vlc_mutex_lock(&sys->lock);
1391 if (SpuHeapPush(&sys->heap, subpic)) {
1392 vlc_mutex_unlock(&sys->lock);
1393 msg_Err(spu, "subpicture heap full");
1394 subpicture_Delete(subpic);
1395 return;
1397 vlc_mutex_unlock(&sys->lock);
1400 subpicture_t *spu_Render(spu_t *spu,
1401 const vlc_fourcc_t *chroma_list,
1402 const video_format_t *fmt_dst,
1403 const video_format_t *fmt_src,
1404 mtime_t render_subtitle_date,
1405 mtime_t render_osd_date,
1406 bool ignore_osd)
1408 spu_private_t *sys = spu->p;
1410 /* Update sub-source chain */
1411 vlc_mutex_lock(&sys->lock);
1412 char *chain_update = sys->source_chain_update;
1413 sys->source_chain_update = NULL;
1414 vlc_mutex_unlock(&sys->lock);
1416 vlc_mutex_lock(&sys->source_chain_lock);
1417 if (chain_update) {
1418 filter_chain_ForEach(sys->source_chain, SubSourceClean, spu);
1419 filter_chain_Reset(sys->source_chain, NULL, NULL);
1421 filter_chain_AppendFromString(spu->p->source_chain, chain_update);
1422 filter_chain_ForEach(sys->source_chain, SubSourceInit, spu);
1424 free(chain_update);
1426 /* Run subpicture sources */
1427 filter_chain_SubSource(sys->source_chain, spu, render_osd_date);
1428 vlc_mutex_unlock(&sys->source_chain_lock);
1430 static const vlc_fourcc_t chroma_list_default_yuv[] = {
1431 VLC_CODEC_YUVA,
1432 VLC_CODEC_RGBA,
1433 VLC_CODEC_ARGB,
1434 VLC_CODEC_BGRA,
1435 VLC_CODEC_YUVP,
1438 static const vlc_fourcc_t chroma_list_default_rgb[] = {
1439 VLC_CODEC_RGBA,
1440 VLC_CODEC_ARGB,
1441 VLC_CODEC_BGRA,
1442 VLC_CODEC_YUVA,
1443 VLC_CODEC_YUVP,
1447 if (!chroma_list || *chroma_list == 0)
1448 chroma_list = vlc_fourcc_IsYUV(fmt_dst->i_chroma) ? chroma_list_default_yuv
1449 : chroma_list_default_rgb;
1451 vlc_mutex_lock(&sys->lock);
1453 unsigned int subpicture_count;
1454 subpicture_t *subpicture_array[VOUT_MAX_SUBPICTURES];
1456 /* Get an array of subpictures to render */
1457 SpuSelectSubpictures(spu, &subpicture_count, subpicture_array,
1458 render_subtitle_date, render_osd_date, ignore_osd);
1459 if (subpicture_count <= 0) {
1460 vlc_mutex_unlock(&sys->lock);
1461 return NULL;
1464 /* Updates the subpictures */
1465 for (unsigned i = 0; i < subpicture_count; i++) {
1466 subpicture_t *subpic = subpicture_array[i];
1467 subpicture_Update(subpic,
1468 fmt_src, fmt_dst,
1469 subpic->b_subtitle ? render_subtitle_date : render_osd_date);
1472 /* Now order the subpicture array
1473 * XXX The order is *really* important for overlap subtitles positionning */
1474 qsort(subpicture_array, subpicture_count, sizeof(*subpicture_array), SubpictureCmp);
1476 /* Render the subpictures */
1477 subpicture_t *render = SpuRenderSubpictures(spu,
1478 subpicture_count, subpicture_array,
1479 chroma_list,
1480 fmt_dst,
1481 fmt_src,
1482 render_subtitle_date,
1483 render_osd_date);
1484 vlc_mutex_unlock(&sys->lock);
1486 return render;
1489 void spu_OffsetSubtitleDate(spu_t *spu, mtime_t duration)
1491 spu_private_t *sys = spu->p;
1493 vlc_mutex_lock(&sys->lock);
1494 for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
1495 spu_heap_entry_t *entry = &sys->heap.entry[i];
1496 subpicture_t *current = entry->subpicture;
1498 if (current && current->b_subtitle) {
1499 if (current->i_start > 0)
1500 current->i_start += duration;
1501 if (current->i_stop > 0)
1502 current->i_stop += duration;
1505 vlc_mutex_unlock(&sys->lock);
1508 int spu_RegisterChannel(spu_t *spu)
1510 spu_private_t *sys = spu->p;
1512 vlc_mutex_lock(&sys->lock);
1513 int channel = sys->channel++;
1514 vlc_mutex_unlock(&sys->lock);
1516 return channel;
1519 void spu_ClearChannel(spu_t *spu, int channel)
1521 spu_private_t *sys = spu->p;
1523 vlc_mutex_lock(&sys->lock);
1525 for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
1526 spu_heap_entry_t *entry = &sys->heap.entry[i];
1527 subpicture_t *subpic = entry->subpicture;
1529 if (!subpic)
1530 continue;
1531 if (subpic->i_channel != channel && (channel != -1 || subpic->i_channel == SPU_DEFAULT_CHANNEL))
1532 continue;
1534 /* You cannot delete subpicture outside of spu_SortSubpictures */
1535 entry->reject = true;
1538 vlc_mutex_unlock(&sys->lock);
1541 void spu_ChangeSources(spu_t *spu, const char *filters)
1543 spu_private_t *sys = spu->p;
1545 vlc_mutex_lock(&sys->lock);
1547 free(sys->source_chain_update);
1548 sys->source_chain_update = strdup(filters);
1550 vlc_mutex_unlock(&sys->lock);
1553 void spu_ChangeFilters(spu_t *spu, const char *filters)
1555 spu_private_t *sys = spu->p;
1557 vlc_mutex_lock(&sys->lock);
1559 free(sys->filter_chain_update);
1560 sys->filter_chain_update = strdup(filters);
1562 vlc_mutex_unlock(&sys->lock);
1565 void spu_ChangeMargin(spu_t *spu, int margin)
1567 spu_private_t *sys = spu->p;
1569 vlc_mutex_lock(&sys->lock);
1570 sys->margin = margin;
1571 vlc_mutex_unlock(&sys->lock);