1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2007 VLC authors and VideoLAN
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 /*****************************************************************************
28 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_modules.h>
38 #include <vlc_input.h>
40 #include <vlc_filter.h>
43 #include "../libvlc.h"
44 #include "vout_internal.h"
45 #include "../misc/subpicture.h"
47 /*****************************************************************************
49 *****************************************************************************/
51 /* Number of simultaneous subpictures */
52 #define VOUT_MAX_SUBPICTURES (__MAX(VOUT_MAX_PICTURES, SPU_MAX_PREPARE_TIME/5000))
56 subpicture_t *subpicture;
61 spu_heap_entry_t entry[VOUT_MAX_SUBPICTURES];
64 struct spu_private_t {
65 vlc_mutex_t lock; /* lock to protect all followings fields */
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 */
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;
95 mtime_t last_sort_date;
98 /*****************************************************************************
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;
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];
119 e->subpicture = subpic;
126 static void SpuHeapDeleteAt(spu_heap_t *heap, int index)
128 spu_heap_entry_t *e = &heap->entry[index];
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)
144 SpuHeapDeleteAt(heap, i);
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];
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;
181 ret = input_Control((input_thread_t*)spu->p->input,
182 INPUT_GET_ATTACHMENTS,
183 attachment_ptr, attachment_count);
187 static filter_t *SpuRenderCreateAndLoadText(spu_t *spu)
189 filter_t *text = vlc_custom_create(spu, sizeof(*text), "spu text");
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_TIME);
209 var_Create(text, "text-rerender", VLC_VAR_BOOL);
214 static filter_t *SpuRenderCreateAndLoadScale(vlc_object_t *object,
215 vlc_fourcc_t src_chroma,
216 vlc_fourcc_t dst_chroma,
219 filter_t *scale = vlc_custom_create(object, sizeof(*scale), "scale");
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);
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)
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_SetTime(text, "spu-elapsed", elapsed_time);
276 var_SetBool(text, "text-rerender", false);
278 if (text->pf_render_html && region->psz_html)
279 text->pf_render_html(text, region, region, chroma_list);
280 else if (text->pf_render_text)
281 text->pf_render_text(text, region, region, chroma_list);
282 *rerender_text = var_GetBool(text, "text-rerender");
286 * A few scale functions helpers.
289 #define SCALE_UNIT (1000)
295 static spu_scale_t spu_scale_create(int w, int h)
297 spu_scale_t s = { .w = w, .h = h };
304 static spu_scale_t spu_scale_unit(void)
306 return spu_scale_create(SCALE_UNIT, SCALE_UNIT);
308 static spu_scale_t spu_scale_createq(int64_t wn, int64_t wd, int64_t hn, int64_t hd)
310 return spu_scale_create(wn * SCALE_UNIT / wd,
311 hn * SCALE_UNIT / hd);
313 static int spu_scale_w(int v, const spu_scale_t s)
315 return v * s.w / SCALE_UNIT;
317 static int spu_scale_h(int v, const spu_scale_t s)
319 return v * s.h / SCALE_UNIT;
321 static int spu_invscale_w(int v, const spu_scale_t s)
323 return v * SCALE_UNIT / s.w;
325 static int spu_invscale_h(int v, const spu_scale_t s)
327 return v * SCALE_UNIT / s.h;
331 * A few area functions helpers
342 static spu_area_t spu_area_create(int x, int y, int w, int h, spu_scale_t s)
344 spu_area_t a = { .x = x, .y = y, .width = w, .height = h, .scale = s };
347 static spu_area_t spu_area_scaled(spu_area_t a)
349 if (a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT)
352 a.x = spu_scale_w(a.x, a.scale);
353 a.y = spu_scale_h(a.y, a.scale);
354 a.width = spu_scale_w(a.width, a.scale);
355 a.height = spu_scale_h(a.height, a.scale);
357 a.scale = spu_scale_unit();
360 static spu_area_t spu_area_unscaled(spu_area_t a, spu_scale_t s)
362 if (a.scale.w == s.w && a.scale.h == s.h)
365 a = spu_area_scaled(a);
367 a.x = spu_invscale_w(a.x, s);
368 a.y = spu_invscale_h(a.y, s);
369 a.width = spu_invscale_w(a.width, s);
370 a.height = spu_invscale_h(a.height, s);
375 static bool spu_area_overlap(spu_area_t a, spu_area_t b)
380 a = spu_area_scaled(a);
381 b = spu_area_scaled(b);
383 return __MAX(a.x - dx, b.x) < __MIN(a.x + a.width + dx, b.x + b.width ) &&
384 __MAX(a.y - dy, b.y) < __MIN(a.y + a.height + dy, b.y + b.height);
388 * Avoid area overlapping
390 static void SpuAreaFixOverlap(spu_area_t *dst,
391 const spu_area_t *sub_array, int sub_count, int align)
393 spu_area_t a = spu_area_scaled(*dst);
394 bool is_moved = false;
398 * XXX It is not fast O(n^2) but we should not have a lot of region */
401 for (int i = 0; i < sub_count; i++) {
402 spu_area_t sub = spu_area_scaled(sub_array[i]);
404 if (!spu_area_overlap(a, sub))
407 if (align & SUBPICTURE_ALIGN_TOP) {
409 int i_y = sub.y + sub.height;
412 } else if (align & SUBPICTURE_ALIGN_BOTTOM) {
414 int i_y = sub.y - a.height;
418 /* TODO what to do in this case? */
419 //fprintf(stderr, "Overlap with unsupported alignment\n");
429 *dst = spu_area_unscaled(a, dst->scale);
433 static void SpuAreaFitInside(spu_area_t *area, const spu_area_t *boundary)
435 spu_area_t a = spu_area_scaled(*area);
437 const int i_error_x = (a.x + a.width) - boundary->width;
443 const int i_error_y = (a.y + a.height) - boundary->height;
449 *area = spu_area_unscaled(a, area->scale);
455 static void SpuRegionPlace(int *x, int *y,
456 const subpicture_t *subpic,
457 const subpicture_region_t *region)
459 assert(region->i_x != INT_MAX && region->i_y != INT_MAX);
460 if (subpic->b_absolute) {
464 if (region->i_align & SUBPICTURE_ALIGN_TOP)
466 else if (region->i_align & SUBPICTURE_ALIGN_BOTTOM)
467 *y = subpic->i_original_picture_height - region->fmt.i_visible_height - region->i_y;
469 *y = subpic->i_original_picture_height / 2 - region->fmt.i_visible_height / 2;
471 if (region->i_align & SUBPICTURE_ALIGN_LEFT)
473 else if (region->i_align & SUBPICTURE_ALIGN_RIGHT)
474 *x = subpic->i_original_picture_width - region->fmt.i_visible_width - region->i_x;
476 *x = subpic->i_original_picture_width / 2 - region->fmt.i_visible_width / 2;
481 * This function compares two 64 bits integers.
482 * It can be used by qsort.
484 static int IntegerCmp(int64_t i0, int64_t i1)
486 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
489 * This function compares 2 subpictures using the following properties
490 * (ordered by priority)
491 * 1. absolute positionning
493 * 3. creation order (per channel)
495 * It can be used by qsort.
497 * XXX spu_RenderSubpictures depends heavily on this order.
499 static int SubpictureCmp(const void *s0, const void *s1)
501 subpicture_t *subpic0 = *(subpicture_t**)s0;
502 subpicture_t *subpic1 = *(subpicture_t**)s1;
505 r = IntegerCmp(!subpic0->b_absolute, !subpic1->b_absolute);
507 r = IntegerCmp(subpic0->i_start, subpic1->i_start);
509 r = IntegerCmp(subpic0->i_channel, subpic1->i_channel);
511 r = IntegerCmp(subpic0->i_order, subpic1->i_order);
515 /*****************************************************************************
516 * SpuSelectSubpictures: find the subpictures to display
517 *****************************************************************************
518 * This function parses all subpictures and decides which ones need to be
519 * displayed. If no picture has been selected, display_date will depend on
521 * We also check for ephemer DVD subpictures (subpictures that have
522 * to be removed if a newer one is available), which makes it a lot
523 * more difficult to guess if a subpicture has to be rendered or not.
524 *****************************************************************************/
525 static void SpuSelectSubpictures(spu_t *spu,
526 unsigned int *subpicture_count,
527 subpicture_t **subpicture_array,
528 mtime_t render_subtitle_date,
529 mtime_t render_osd_date,
532 spu_private_t *sys = spu->p;
535 *subpicture_count = 0;
537 /* Create a list of channels */
538 int channel[VOUT_MAX_SUBPICTURES];
539 int channel_count = 0;
541 for (int index = 0; index < VOUT_MAX_SUBPICTURES; index++) {
542 spu_heap_entry_t *entry = &sys->heap.entry[index];
543 if (!entry->subpicture || entry->reject)
545 const int i_channel = entry->subpicture->i_channel;
547 for (i = 0; i < channel_count; i++) {
548 if (channel[i] == i_channel)
551 if (channel_count <= i)
552 channel[channel_count++] = i_channel;
555 /* Fill up the subpicture_array arrays with relevent pictures */
556 for (int i = 0; i < channel_count; i++) {
557 subpicture_t *available_subpic[VOUT_MAX_SUBPICTURES];
558 bool is_available_late[VOUT_MAX_SUBPICTURES];
559 int available_count = 0;
561 mtime_t start_date = render_subtitle_date;
562 mtime_t ephemer_subtitle_date = 0;
563 mtime_t ephemer_osd_date = 0;
564 int64_t ephemer_subtitle_order = INT64_MIN;
565 int64_t ephemer_system_order = INT64_MIN;
567 /* Select available pictures */
568 for (int index = 0; index < VOUT_MAX_SUBPICTURES; index++) {
569 spu_heap_entry_t *entry = &sys->heap.entry[index];
570 subpicture_t *current = entry->subpicture;
574 if (!current || entry->reject) {
576 SpuHeapDeleteAt(&sys->heap, index);
580 if (current->i_channel != channel[i] ||
581 (ignore_osd && !current->b_subtitle))
584 const mtime_t render_date = current->b_subtitle ? render_subtitle_date : render_osd_date;
586 render_date < current->i_start) {
587 /* Too early, come back next monday */
591 mtime_t *ephemer_date_ptr = current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
592 int64_t *ephemer_order_ptr = current->b_subtitle ? &ephemer_subtitle_order : &ephemer_system_order;
593 if (current->i_start >= *ephemer_date_ptr) {
594 *ephemer_date_ptr = current->i_start;
595 if (current->i_order > *ephemer_order_ptr)
596 *ephemer_order_ptr = current->i_order;
599 is_stop_valid = !current->b_ephemer || current->i_stop > current->i_start;
601 is_late = is_stop_valid && current->i_stop <= render_date;
603 /* start_date will be used for correct automatic overlap support
604 * in case picture that should not be displayed anymore (display_time)
605 * overlap with a picture to be displayed (current->i_start) */
606 if (current->b_subtitle && !is_late && !current->b_ephemer)
607 start_date = current->i_start;
610 available_subpic[available_count] = current;
611 is_available_late[available_count] = is_late;
615 /* Only forced old picture display at the transition */
616 if (start_date < sys->last_sort_date)
617 start_date = sys->last_sort_date;
619 start_date = INT64_MAX;
621 /* Select pictures to be displayed */
622 for (int index = 0; index < available_count; index++) {
623 subpicture_t *current = available_subpic[index];
624 bool is_late = is_available_late[index];
626 const mtime_t stop_date = current->b_subtitle ? __MAX(start_date, sys->last_sort_date) : render_osd_date;
627 const mtime_t ephemer_date = current->b_subtitle ? ephemer_subtitle_date : ephemer_osd_date;
628 const int64_t ephemer_order = current->b_subtitle ? ephemer_subtitle_order : ephemer_system_order;
630 /* Destroy late and obsolete ephemer subpictures */
631 bool is_rejeted = is_late && current->i_stop <= stop_date;
632 if (current->b_ephemer) {
633 if (current->i_start < ephemer_date)
635 else if (current->i_start == ephemer_date &&
636 current->i_order < ephemer_order)
641 SpuHeapDeleteSubpicture(&sys->heap, current);
643 subpicture_array[(*subpicture_count)++] = current;
647 sys->last_sort_date = render_subtitle_date;
653 * It will transform the provided region into another region suitable for rendering.
655 static void SpuRenderRegion(spu_t *spu,
656 subpicture_region_t **dst_ptr, spu_area_t *dst_area,
657 subpicture_t *subpic, subpicture_region_t *region,
658 const spu_scale_t scale_size,
659 const vlc_fourcc_t *chroma_list,
660 const video_format_t *fmt,
661 const spu_area_t *subtitle_area, int subtitle_area_count,
664 spu_private_t *sys = spu->p;
666 video_format_t fmt_original = region->fmt;
667 bool restore_text = false;
671 video_format_t region_fmt;
672 picture_t *region_picture;
674 /* Invalidate area by default */
675 *dst_area = spu_area_create(0,0, 0,0, scale_size);
678 /* Render text region */
679 if (region->fmt.i_chroma == VLC_CODEC_TEXT) {
680 SpuRenderText(spu, &restore_text, region,
682 render_date - subpic->i_start);
684 /* Check if the rendering has failed ... */
685 if (region->fmt.i_chroma == VLC_CODEC_TEXT)
689 /* Force palette if requested
690 * FIXME b_force_palette and force_crop are applied to all subpictures using palette
691 * instead of only the right one (being the dvd spu).
693 const bool using_palette = region->fmt.i_chroma == VLC_CODEC_YUVP;
694 const bool force_palette = using_palette && sys->force_palette;
695 const bool force_crop = force_palette && sys->force_crop;
696 bool changed_palette = false;
698 /* Compute the margin which is expressed in destination pixel unit
699 * The margin is applied only to subtitle and when no forced crop is
700 * requested (dvd menu) */
702 if (!force_crop && subpic->b_subtitle)
703 y_margin = spu_invscale_h(sys->margin, scale_size);
706 * We compute the position in the rendered size */
707 SpuRegionPlace(&x_offset, &y_offset,
710 /* Save this position for subtitle overlap support
711 * it is really important that there are given without scale_size applied */
712 *dst_area = spu_area_create(x_offset, y_offset,
713 region->fmt.i_visible_width,
714 region->fmt.i_visible_height,
717 /* Handle overlapping subtitles when possible */
718 if (subpic->b_subtitle && !subpic->b_absolute)
719 SpuAreaFixOverlap(dst_area, subtitle_area, subtitle_area_count,
722 /* we copy the area: for the subtitle overlap support we want
723 * to only save the area without margin applied */
724 spu_area_t restrained = *dst_area;
726 /* apply margin to subtitles and correct if they go over the picture edge */
727 if (subpic->b_subtitle)
728 restrained.y -= y_margin;
730 spu_area_t display = spu_area_create(0, 0, fmt->i_visible_width,
731 fmt->i_visible_height,
734 SpuAreaFitInside(&restrained, &display);
736 /* Fix the position for the current scale_size */
737 x_offset = spu_scale_w(restrained.x, restrained.scale);
738 y_offset = spu_scale_h(restrained.y, restrained.scale);
742 video_palette_t *old_palette = region->fmt.p_palette;
743 video_palette_t new_palette;
745 /* We suppose DVD palette here */
746 new_palette.i_entries = 4;
747 for (int i = 0; i < 4; i++)
748 for (int j = 0; j < 4; j++)
749 new_palette.palette[i][j] = sys->palette[i][j];
751 if (old_palette->i_entries == new_palette.i_entries) {
752 for (int i = 0; i < old_palette->i_entries; i++)
753 for (int j = 0; j < 4; j++)
754 changed_palette |= old_palette->palette[i][j] != new_palette.palette[i][j];
756 changed_palette = true;
758 *old_palette = new_palette;
762 region_fmt = region->fmt;
763 region_picture = region->p_picture;
765 bool convert_chroma = true;
766 for (int i = 0; chroma_list[i] && convert_chroma; i++) {
767 if (region_fmt.i_chroma == chroma_list[i])
768 convert_chroma = false;
771 /* Scale from rendered size to destination size */
772 if (sys->scale && sys->scale->p_module &&
773 (!using_palette || (sys->scale_yuvp && sys->scale_yuvp->p_module)) &&
774 (scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT ||
775 using_palette || convert_chroma)) {
776 const unsigned dst_width = spu_scale_w(region->fmt.i_visible_width, scale_size);
777 const unsigned dst_height = spu_scale_h(region->fmt.i_visible_height, scale_size);
779 /* Destroy the cache if unusable */
780 if (region->p_private) {
781 subpicture_region_private_t *private = region->p_private;
782 bool is_changed = false;
784 /* Check resize changes */
785 if (dst_width != private->fmt.i_visible_width ||
786 dst_height != private->fmt.i_visible_height)
789 /* Check forced palette changes */
793 if (convert_chroma && private->fmt.i_chroma != chroma_list[0])
797 subpicture_region_private_Delete(private);
798 region->p_private = NULL;
802 /* Scale if needed into cache */
803 if (!region->p_private && dst_width > 0 && dst_height > 0) {
804 filter_t *scale = sys->scale;
806 picture_t *picture = region->p_picture;
807 picture_Hold(picture);
809 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
811 filter_t *scale_yuvp = sys->scale_yuvp;
813 scale_yuvp->fmt_in.video = region->fmt;
815 scale_yuvp->fmt_out.video = region->fmt;
816 scale_yuvp->fmt_out.video.i_chroma = chroma_list[0];
818 picture = scale_yuvp->pf_video_filter(scale_yuvp, picture);
820 /* Well we will try conversion+scaling */
821 msg_Warn(spu, "%4.4s to %4.4s conversion failed",
822 (const char*)&scale_yuvp->fmt_in.video.i_chroma,
823 (const char*)&scale_yuvp->fmt_out.video.i_chroma);
827 /* Conversion(except from YUVP)/Scaling */
829 (picture->format.i_visible_width != dst_width ||
830 picture->format.i_visible_height != dst_height ||
831 (convert_chroma && !using_palette)))
833 scale->fmt_in.video = picture->format;
834 scale->fmt_out.video = picture->format;
836 scale->fmt_in.video.i_chroma = chroma_list[0];
838 scale->fmt_out.i_codec =
839 scale->fmt_out.video.i_chroma = chroma_list[0];
841 scale->fmt_out.video.i_width = dst_width;
842 scale->fmt_out.video.i_height = dst_height;
844 scale->fmt_out.video.i_visible_width =
845 spu_scale_w(region->fmt.i_visible_width, scale_size);
846 scale->fmt_out.video.i_visible_height =
847 spu_scale_h(region->fmt.i_visible_height, scale_size);
849 picture = scale->pf_video_filter(scale, picture);
851 msg_Err(spu, "scaling failed");
856 region->p_private = subpicture_region_private_New(&picture->format);
857 if (region->p_private) {
858 region->p_private->p_picture = picture;
859 if (!region->p_private->p_picture) {
860 subpicture_region_private_Delete(region->p_private);
861 region->p_private = NULL;
864 picture_Release(picture);
869 /* And use the scaled picture */
870 if (region->p_private) {
871 region_fmt = region->p_private->fmt;
872 region_picture = region->p_private->p_picture;
876 /* Force cropping if requested */
878 int crop_x = spu_scale_w(sys->crop.x, scale_size);
879 int crop_y = spu_scale_h(sys->crop.y, scale_size);
880 int crop_width = spu_scale_w(sys->crop.width, scale_size);
881 int crop_height= spu_scale_h(sys->crop.height,scale_size);
883 /* Find the intersection */
884 if (crop_x + crop_width <= x_offset ||
885 x_offset + (int)region_fmt.i_visible_width < crop_x ||
886 crop_y + crop_height <= y_offset ||
887 y_offset + (int)region_fmt.i_visible_height < crop_y) {
888 /* No intersection */
889 region_fmt.i_visible_width =
890 region_fmt.i_visible_height = 0;
892 int x, y, x_end, y_end;
893 x = __MAX(crop_x, x_offset);
894 y = __MAX(crop_y, y_offset);
895 x_end = __MIN(crop_x + crop_width,
896 x_offset + (int)region_fmt.i_visible_width);
897 y_end = __MIN(crop_y + crop_height,
898 y_offset + (int)region_fmt.i_visible_height);
900 region_fmt.i_x_offset = x - x_offset;
901 region_fmt.i_y_offset = y - y_offset;
902 region_fmt.i_visible_width = x_end - x;
903 region_fmt.i_visible_height = y_end - y;
905 x_offset = __MAX(x, 0);
906 y_offset = __MAX(y, 0);
910 subpicture_region_t *dst = *dst_ptr = subpicture_region_New(®ion_fmt);
916 picture_Release(dst->p_picture);
917 dst->p_picture = picture_Hold(region_picture);
918 int fade_alpha = 255;
919 if (subpic->b_fade) {
920 mtime_t fade_start = subpic->i_start + 3 * (subpic->i_stop - subpic->i_start) / 4;
922 if (fade_start <= render_date && fade_start < subpic->i_stop)
923 fade_alpha = 255 * (subpic->i_stop - render_date) /
924 (subpic->i_stop - fade_start);
926 dst->i_alpha = fade_alpha * subpic->i_alpha * region->i_alpha / 65025;
931 /* Some forms of subtitles need to be re-rendered more than
932 * once, eg. karaoke. We therefore restore the region to its
933 * pre-rendered state, so the next time through everything is
936 if (region->p_picture) {
937 picture_Release(region->p_picture);
938 region->p_picture = NULL;
940 if (region->p_private) {
941 subpicture_region_private_Delete(region->p_private);
942 region->p_private = NULL;
944 region->fmt = fmt_original;
949 * This function renders all sub picture units in the list.
951 static subpicture_t *SpuRenderSubpictures(spu_t *spu,
952 unsigned int i_subpicture,
953 subpicture_t **pp_subpicture,
954 const vlc_fourcc_t *chroma_list,
955 const video_format_t *fmt_dst,
956 const video_format_t *fmt_src,
957 mtime_t render_subtitle_date,
958 mtime_t render_osd_date)
960 spu_private_t *sys = spu->p;
962 /* Count the number of regions and subtitle regions */
963 unsigned int subtitle_region_count = 0;
964 unsigned int region_count = 0;
965 for (unsigned i = 0; i < i_subpicture; i++) {
966 const subpicture_t *subpic = pp_subpicture[i];
969 for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
972 if (subpic->b_subtitle)
973 subtitle_region_count += count;
974 region_count += count;
976 if (region_count <= 0)
979 /* Create the output subpicture */
980 subpicture_t *output = subpicture_New(NULL);
983 output->i_order = pp_subpicture[i_subpicture - 1]->i_order;
984 output->i_original_picture_width = fmt_dst->i_visible_width;
985 output->i_original_picture_height = fmt_dst->i_visible_height;
986 subpicture_region_t **output_last_ptr = &output->p_region;
988 /* Allocate area array for subtitle overlap */
989 spu_area_t subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
990 spu_area_t *subtitle_area;
991 int subtitle_area_count;
993 subtitle_area_count = 0;
994 subtitle_area = subtitle_area_buffer;
995 if (subtitle_region_count > sizeof(subtitle_area_buffer)/sizeof(*subtitle_area_buffer))
996 subtitle_area = calloc(subtitle_region_count, sizeof(*subtitle_area));
998 /* Process all subpictures and regions (in the right order) */
999 for (unsigned int index = 0; index < i_subpicture; index++) {
1000 subpicture_t *subpic = pp_subpicture[index];
1001 subpicture_region_t *region;
1003 if (!subpic->p_region)
1006 if (subpic->i_original_picture_width <= 0 ||
1007 subpic->i_original_picture_height <= 0) {
1008 if (subpic->i_original_picture_width > 0 ||
1009 subpic->i_original_picture_height > 0)
1010 msg_Err(spu, "original picture size %dx%d is unsupported",
1011 subpic->i_original_picture_width,
1012 subpic->i_original_picture_height);
1014 msg_Warn(spu, "original picture size is undefined");
1016 subpic->i_original_picture_width = fmt_src->i_visible_width;
1017 subpic->i_original_picture_height = fmt_src->i_visible_height;
1021 /* FIXME aspect ratio ? */
1022 sys->text->fmt_out.video.i_width =
1023 sys->text->fmt_out.video.i_visible_width = subpic->i_original_picture_width;
1025 sys->text->fmt_out.video.i_height =
1026 sys->text->fmt_out.video.i_visible_height = subpic->i_original_picture_height;
1029 /* Render all regions
1030 * We always transform non absolute subtitle into absolute one on the
1031 * first rendering to allow good subtitle overlap support.
1033 for (region = subpic->p_region; region != NULL; region = region->p_next) {
1036 /* Compute region scale AR */
1037 video_format_t region_fmt = region->fmt;
1038 if (region_fmt.i_sar_num <= 0 || region_fmt.i_sar_den <= 0) {
1039 region_fmt.i_sar_num = (int64_t)fmt_dst->i_visible_width * fmt_dst->i_sar_num * subpic->i_original_picture_height;
1040 region_fmt.i_sar_den = (int64_t)fmt_dst->i_visible_height * fmt_dst->i_sar_den * subpic->i_original_picture_width;
1041 vlc_ureduce(®ion_fmt.i_sar_num, ®ion_fmt.i_sar_den,
1042 region_fmt.i_sar_num, region_fmt.i_sar_den, 65536);
1045 /* Compute scaling from original size to destination size
1046 * FIXME The current scaling ensure that the heights match, the width being
1049 spu_scale_t scale = spu_scale_createq((int64_t)fmt_dst->i_visible_height * fmt_dst->i_sar_den * region_fmt.i_sar_num,
1050 (int64_t)subpic->i_original_picture_height * fmt_dst->i_sar_num * region_fmt.i_sar_den,
1051 fmt_dst->i_visible_height,
1052 subpic->i_original_picture_height);
1054 /* Check scale validity */
1055 if (scale.w <= 0 || scale.h <= 0)
1059 SpuRenderRegion(spu, output_last_ptr, &area,
1060 subpic, region, scale,
1061 chroma_list, fmt_dst,
1062 subtitle_area, subtitle_area_count,
1063 subpic->b_subtitle ? render_subtitle_date : render_osd_date);
1064 if (*output_last_ptr)
1065 output_last_ptr = &(*output_last_ptr)->p_next;
1067 if (subpic->b_subtitle) {
1068 area = spu_area_unscaled(area, scale);
1069 if (!subpic->b_absolute && area.width > 0 && area.height > 0) {
1070 region->i_x = area.x;
1071 region->i_y = area.y;
1074 subtitle_area[subtitle_area_count++] = area;
1077 if (subpic->b_subtitle && subpic->p_region)
1078 subpic->b_absolute = true;
1082 if (subtitle_area != subtitle_area_buffer)
1083 free(subtitle_area);
1088 /*****************************************************************************
1089 * Object variables callbacks
1090 *****************************************************************************/
1092 /*****************************************************************************
1093 * UpdateSPU: update subpicture settings
1094 *****************************************************************************
1095 * This function is called from CropCallback and at initialization time, to
1096 * retrieve crop information from the input.
1097 *****************************************************************************/
1098 static void UpdateSPU(spu_t *spu, vlc_object_t *object)
1100 spu_private_t *sys = spu->p;
1103 vlc_mutex_lock(&sys->lock);
1105 sys->force_palette = false;
1106 sys->force_crop = false;
1108 if (var_Get(object, "highlight", &val) || !val.b_bool) {
1109 vlc_mutex_unlock(&sys->lock);
1113 sys->force_crop = true;
1114 sys->crop.x = var_GetInteger(object, "x-start");
1115 sys->crop.y = var_GetInteger(object, "y-start");
1116 sys->crop.width = var_GetInteger(object, "x-end") - sys->crop.x;
1117 sys->crop.height = var_GetInteger(object, "y-end") - sys->crop.y;
1119 if (var_Get(object, "menu-palette", &val) == VLC_SUCCESS) {
1120 memcpy(sys->palette, val.p_address, 16);
1121 sys->force_palette = true;
1123 vlc_mutex_unlock(&sys->lock);
1125 msg_Dbg(object, "crop: %i,%i,%i,%i, palette forced: %i",
1126 sys->crop.x, sys->crop.y,
1127 sys->crop.width, sys->crop.height,
1128 sys->force_palette);
1131 /*****************************************************************************
1132 * CropCallback: called when the highlight properties are changed
1133 *****************************************************************************
1134 * This callback is called from the input thread when we need cropping
1135 *****************************************************************************/
1136 static int CropCallback(vlc_object_t *object, char const *var,
1137 vlc_value_t oldval, vlc_value_t newval, void *data)
1139 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(var);
1141 UpdateSPU((spu_t *)data, object);
1145 /*****************************************************************************
1146 * Buffers allocation callbacks for the filters
1147 *****************************************************************************/
1149 static subpicture_t *sub_new_buffer(filter_t *filter)
1151 int channel = (intptr_t)filter->owner.sys;
1153 subpicture_t *subpicture = subpicture_New(NULL);
1155 subpicture->i_channel = channel;
1159 static int SubSourceInit(filter_t *filter, void *data)
1162 int channel = spu_RegisterChannel(spu);
1164 filter->owner.sys = (void *)(intptr_t)channel;
1165 filter->owner.sub.buffer_new = sub_new_buffer;
1169 static int SubSourceClean(filter_t *filter, void *data)
1172 int channel = (intptr_t)filter->owner.sys;
1174 spu_ClearChannel(spu, channel);
1178 /*****************************************************************************
1180 *****************************************************************************/
1184 * Creates the subpicture unit
1186 * \param p_this the parent object which creates the subpicture unit
1188 spu_t *spu_Create(vlc_object_t *object)
1190 spu_t *spu = vlc_custom_create(object,
1191 sizeof(spu_t) + sizeof(spu_private_t),
1196 /* Initialize spu fields */
1197 spu_private_t *sys = spu->p = (spu_private_t*)&spu[1];
1199 /* Initialize private fields */
1200 vlc_mutex_init(&sys->lock);
1202 SpuHeapInit(&sys->heap);
1206 sys->scale_yuvp = NULL;
1208 sys->margin = var_InheritInteger(spu, "sub-margin");
1210 /* Register the default subpicture channel */
1211 sys->channel = SPU_DEFAULT_CHANNEL + 1;
1213 sys->source_chain_update = NULL;
1214 sys->filter_chain_update = NULL;
1215 vlc_mutex_init(&sys->source_chain_lock);
1216 vlc_mutex_init(&sys->filter_chain_lock);
1217 sys->source_chain = filter_chain_New(spu, "sub source", false);
1218 sys->filter_chain = filter_chain_New(spu, "sub filter", false);
1220 /* Load text and scale module */
1221 sys->text = SpuRenderCreateAndLoadText(spu);
1223 /* XXX spu->p_scale is used for all conversion/scaling except yuvp to
1225 sys->scale = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1226 VLC_CODEC_YUVA, VLC_CODEC_RGBA, true);
1228 /* This one is used for YUVP to YUVA/RGBA without scaling
1229 * FIXME rename it */
1230 sys->scale_yuvp = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1231 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false);
1234 sys->last_sort_date = -1;
1240 * Destroy the subpicture unit
1242 * \param p_this the parent object which destroys the subpicture unit
1244 void spu_Destroy(spu_t *spu)
1246 spu_private_t *sys = spu->p;
1249 FilterRelease(sys->text);
1251 if (sys->scale_yuvp)
1252 FilterRelease(sys->scale_yuvp);
1255 FilterRelease(sys->scale);
1257 filter_chain_ForEach(sys->source_chain, SubSourceClean, spu);
1258 filter_chain_Delete(sys->source_chain);
1259 filter_chain_Delete(sys->filter_chain);
1260 vlc_mutex_destroy(&sys->source_chain_lock);
1261 vlc_mutex_destroy(&sys->filter_chain_lock);
1262 free(sys->source_chain_update);
1263 free(sys->filter_chain_update);
1265 /* Destroy all remaining subpictures */
1266 SpuHeapClean(&sys->heap);
1268 vlc_mutex_destroy(&sys->lock);
1270 vlc_object_release(spu);
1274 * Attach/Detach the SPU from any input
1276 * \param p_this the object in which to destroy the subpicture unit
1277 * \param b_attach to select attach or detach
1279 void spu_Attach(spu_t *spu, vlc_object_t *input, bool attach)
1282 UpdateSPU(spu, input);
1283 var_Create(input, "highlight", VLC_VAR_BOOL);
1284 var_AddCallback(input, "highlight", CropCallback, spu);
1286 vlc_mutex_lock(&spu->p->lock);
1287 spu->p->input = input;
1290 FilterRelease(spu->p->text);
1291 spu->p->text = SpuRenderCreateAndLoadText(spu);
1293 vlc_mutex_unlock(&spu->p->lock);
1295 vlc_mutex_lock(&spu->p->lock);
1296 spu->p->input = NULL;
1297 vlc_mutex_unlock(&spu->p->lock);
1299 /* Delete callbacks */
1300 var_DelCallback(input, "highlight", CropCallback, spu);
1301 var_Destroy(input, "highlight");
1306 * Inform the SPU filters of mouse event
1308 int spu_ProcessMouse(spu_t *spu,
1309 const vlc_mouse_t *mouse,
1310 const video_format_t *fmt)
1312 spu_private_t *sys = spu->p;
1314 vlc_mutex_lock(&sys->source_chain_lock);
1315 filter_chain_MouseEvent(sys->source_chain, mouse, fmt);
1316 vlc_mutex_unlock(&sys->source_chain_lock);
1322 * Display a subpicture
1324 * Remove the reservation flag of a subpicture, which will cause it to be
1325 * ready for display.
1326 * \param spu the subpicture unit object
1327 * \param subpic the subpicture to display
1329 void spu_PutSubpicture(spu_t *spu, subpicture_t *subpic)
1331 spu_private_t *sys = spu->p;
1333 /* Update sub-filter chain */
1334 vlc_mutex_lock(&sys->lock);
1335 char *chain_update = sys->filter_chain_update;
1336 sys->filter_chain_update = NULL;
1337 vlc_mutex_unlock(&sys->lock);
1339 bool is_left_empty = false;
1341 vlc_mutex_lock(&sys->filter_chain_lock);
1343 if (*chain_update) {
1344 filter_chain_Reset(sys->filter_chain, NULL, NULL);
1346 filter_chain_AppendFromString(spu->p->filter_chain, chain_update);
1348 else if (filter_chain_GetLength(spu->p->filter_chain) > 0)
1349 filter_chain_Reset(sys->filter_chain, NULL, NULL);
1351 /* "sub-source" was formerly "sub-filter", so now the "sub-filter"
1352 configuration may contain sub-filters or sub-sources configurations.
1353 if the filters chain was left empty it may indicate that it's a sub-source configuration */
1354 is_left_empty = (filter_chain_GetLength(spu->p->filter_chain) == 0);
1356 vlc_mutex_unlock(&sys->filter_chain_lock);
1358 if (is_left_empty) {
1359 /* try to use the configuration as a sub-source configuration,
1360 but only if there is no 'source_chain_update' value and
1361 if only if 'chain_update' has a value */
1362 if (chain_update && *chain_update) {
1363 vlc_mutex_lock(&sys->lock);
1364 if (!sys->source_chain_update || !*sys->source_chain_update) {
1365 if (sys->source_chain_update)
1366 free(sys->source_chain_update);
1367 sys->source_chain_update = chain_update;
1368 chain_update = NULL;
1370 vlc_mutex_unlock(&sys->lock);
1376 /* Run filter chain on the new subpicture */
1377 vlc_mutex_lock(&sys->filter_chain_lock);
1378 subpic = filter_chain_SubFilter(spu->p->filter_chain, subpic);
1379 vlc_mutex_unlock(&sys->filter_chain_lock);
1383 /* SPU_DEFAULT_CHANNEL always reset itself */
1384 if (subpic->i_channel == SPU_DEFAULT_CHANNEL)
1385 spu_ClearChannel(spu, SPU_DEFAULT_CHANNEL);
1387 /* p_private is for spu only and cannot be non NULL here */
1388 for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
1389 assert(r->p_private == NULL);
1392 vlc_mutex_lock(&sys->lock);
1393 if (SpuHeapPush(&sys->heap, subpic)) {
1394 vlc_mutex_unlock(&sys->lock);
1395 msg_Err(spu, "subpicture heap full");
1396 subpicture_Delete(subpic);
1399 vlc_mutex_unlock(&sys->lock);
1402 subpicture_t *spu_Render(spu_t *spu,
1403 const vlc_fourcc_t *chroma_list,
1404 const video_format_t *fmt_dst,
1405 const video_format_t *fmt_src,
1406 mtime_t render_subtitle_date,
1407 mtime_t render_osd_date,
1410 spu_private_t *sys = spu->p;
1412 /* Update sub-source chain */
1413 vlc_mutex_lock(&sys->lock);
1414 char *chain_update = sys->source_chain_update;
1415 sys->source_chain_update = NULL;
1416 vlc_mutex_unlock(&sys->lock);
1418 vlc_mutex_lock(&sys->source_chain_lock);
1420 filter_chain_ForEach(sys->source_chain, SubSourceClean, spu);
1421 filter_chain_Reset(sys->source_chain, NULL, NULL);
1423 filter_chain_AppendFromString(spu->p->source_chain, chain_update);
1424 filter_chain_ForEach(sys->source_chain, SubSourceInit, spu);
1428 /* Run subpicture sources */
1429 filter_chain_SubSource(sys->source_chain, spu, render_osd_date);
1430 vlc_mutex_unlock(&sys->source_chain_lock);
1432 static const vlc_fourcc_t chroma_list_default_yuv[] = {
1439 static const vlc_fourcc_t chroma_list_default_rgb[] = {
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);
1464 /* Updates the subpictures */
1465 for (unsigned i = 0; i < subpicture_count; i++) {
1466 subpicture_t *subpic = subpicture_array[i];
1467 subpicture_Update(subpic,
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,
1482 render_subtitle_date,
1484 vlc_mutex_unlock(&sys->lock);
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);
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;
1531 if (subpic->i_channel != channel && (channel != -1 || subpic->i_channel == SPU_DEFAULT_CHANNEL))
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);