1 /*****************************************************************************
2 * vout_subpictures.c : subpicture management functions
3 *****************************************************************************
4 * Copyright (C) 2000-2007 the VideoLAN team
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
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 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 General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, 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 struct filter_owner_sys_t {
164 static void FilterRelease(filter_t *filter)
166 if (filter->p_module)
167 module_unneed(filter, filter->p_module);
169 free(filter->p_owner);
171 vlc_object_release(filter);
174 static picture_t *spu_new_video_buffer(filter_t *filter)
176 const video_format_t *fmt = &filter->fmt_out.video;
179 return picture_NewFromFormat(fmt);
181 static void spu_del_video_buffer(filter_t *filter, picture_t *picture)
184 picture_Release(picture);
187 static int spu_get_attachments(filter_t *filter,
188 input_attachment_t ***attachment_ptr,
189 int *attachment_count)
191 spu_t *spu = filter->p_owner->spu;
193 int ret = VLC_EGENERIC;
195 ret = input_Control((input_thread_t*)spu->p->input,
196 INPUT_GET_ATTACHMENTS,
197 attachment_ptr, attachment_count);
201 static filter_t *SpuRenderCreateAndLoadText(spu_t *spu)
203 filter_t *text = vlc_custom_create(spu, sizeof(*text), "spu text");
207 text->p_owner = xmalloc(sizeof(*text->p_owner));
208 text->p_owner->spu = spu;
210 es_format_Init(&text->fmt_in, VIDEO_ES, 0);
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;
218 text->pf_get_attachments = spu_get_attachments;
220 text->p_module = module_need(text, "text renderer", "$text-renderer", false);
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);
229 static filter_t *SpuRenderCreateAndLoadScale(vlc_object_t *object,
230 vlc_fourcc_t src_chroma,
231 vlc_fourcc_t dst_chroma,
234 filter_t *scale = vlc_custom_create(object, sizeof(*scale), "scale");
238 es_format_Init(&scale->fmt_in, VIDEO_ES, 0);
239 scale->fmt_in.video.i_chroma = src_chroma;
240 scale->fmt_in.video.i_width =
241 scale->fmt_in.video.i_height = 32;
243 es_format_Init(&scale->fmt_out, VIDEO_ES, 0);
244 scale->fmt_out.video.i_chroma = dst_chroma;
245 scale->fmt_out.video.i_width =
246 scale->fmt_out.video.i_height = require_resize ? 16 : 32;
248 scale->pf_video_buffer_new = spu_new_video_buffer;
249 scale->pf_video_buffer_del = spu_del_video_buffer;
251 scale->p_module = module_need(scale, "video filter2", NULL, false);
256 static void SpuRenderText(spu_t *spu, bool *rerender_text,
257 subpicture_region_t *region,
258 const vlc_fourcc_t *chroma_list,
259 mtime_t elapsed_time)
261 filter_t *text = spu->p->text;
263 assert(region->fmt.i_chroma == VLC_CODEC_TEXT);
265 if (!text || !text->p_module)
268 /* Setup 3 variables which can be used to render
269 * time-dependent text (and effects). The first indicates
270 * the total amount of time the text will be on screen,
271 * the second the amount of time it has already been on
272 * screen (can be a negative value as text is layed out
273 * before it is rendered) and the third is a feedback
274 * variable from the renderer - if the renderer sets it
275 * then this particular text is time-dependent, eg. the
276 * visual progress bar inside the text in karaoke and the
277 * text needs to be rendered multiple times in order for
278 * the effect to work - we therefore need to return the
279 * region to its original state at the end of the loop,
280 * instead of leaving it in YUVA or YUVP.
281 * Any renderer which is unaware of how to render
282 * time-dependent text can happily ignore the variables
283 * and render the text the same as usual - it should at
284 * least show up on screen, but the effect won't change
285 * the text over time.
287 var_SetTime(text, "spu-elapsed", elapsed_time);
288 var_SetBool(text, "text-rerender", false);
290 if (text->pf_render_html && region->psz_html)
291 text->pf_render_html(text, region, region, chroma_list);
292 else if (text->pf_render_text)
293 text->pf_render_text(text, region, region, chroma_list);
294 *rerender_text = var_GetBool(text, "text-rerender");
298 * A few scale functions helpers.
301 #define SCALE_UNIT (1000)
307 static spu_scale_t spu_scale_create(int w, int h)
309 spu_scale_t s = { .w = w, .h = h };
316 static spu_scale_t spu_scale_unit(void)
318 return spu_scale_create(SCALE_UNIT, SCALE_UNIT);
320 static spu_scale_t spu_scale_createq(int64_t wn, int64_t wd, int64_t hn, int64_t hd)
322 return spu_scale_create(wn * SCALE_UNIT / wd,
323 hn * SCALE_UNIT / hd);
325 static int spu_scale_w(int v, const spu_scale_t s)
327 return v * s.w / SCALE_UNIT;
329 static int spu_scale_h(int v, const spu_scale_t s)
331 return v * s.h / SCALE_UNIT;
333 static int spu_invscale_w(int v, const spu_scale_t s)
335 return v * SCALE_UNIT / s.w;
337 static int spu_invscale_h(int v, const spu_scale_t s)
339 return v * SCALE_UNIT / s.h;
343 * A few area functions helpers
354 static spu_area_t spu_area_create(int x, int y, int w, int h, spu_scale_t s)
356 spu_area_t a = { .x = x, .y = y, .width = w, .height = h, .scale = s };
359 static spu_area_t spu_area_scaled(spu_area_t a)
361 if (a.scale.w == SCALE_UNIT && a.scale.h == SCALE_UNIT)
364 a.x = spu_scale_w(a.x, a.scale);
365 a.y = spu_scale_h(a.y, a.scale);
366 a.width = spu_scale_w(a.width, a.scale);
367 a.height = spu_scale_h(a.height, a.scale);
369 a.scale = spu_scale_unit();
372 static spu_area_t spu_area_unscaled(spu_area_t a, spu_scale_t s)
374 if (a.scale.w == s.w && a.scale.h == s.h)
377 a = spu_area_scaled(a);
379 a.x = spu_invscale_w(a.x, s);
380 a.y = spu_invscale_h(a.y, s);
381 a.width = spu_invscale_w(a.width, s);
382 a.height = spu_invscale_h(a.height, s);
387 static bool spu_area_overlap(spu_area_t a, spu_area_t b)
392 a = spu_area_scaled(a);
393 b = spu_area_scaled(b);
395 return __MAX(a.x - dx, b.x) < __MIN(a.x + a.width + dx, b.x + b.width ) &&
396 __MAX(a.y - dy, b.y) < __MIN(a.y + a.height + dy, b.y + b.height);
400 * Avoid area overlapping
402 static void SpuAreaFixOverlap(spu_area_t *dst,
403 const spu_area_t *sub_array, int sub_count, int align)
405 spu_area_t a = spu_area_scaled(*dst);
406 bool is_moved = false;
410 * XXX It is not fast O(n^2) but we should not have a lot of region */
413 for (int i = 0; i < sub_count; i++) {
414 spu_area_t sub = spu_area_scaled(sub_array[i]);
416 if (!spu_area_overlap(a, sub))
419 if (align & SUBPICTURE_ALIGN_TOP) {
421 int i_y = sub.y + sub.height;
424 } else if (align & SUBPICTURE_ALIGN_BOTTOM) {
426 int i_y = sub.y - a.height;
430 /* TODO what to do in this case? */
431 //fprintf(stderr, "Overlap with unsupported alignment\n");
441 *dst = spu_area_unscaled(a, dst->scale);
445 static void SpuAreaFitInside(spu_area_t *area, const spu_area_t *boundary)
447 spu_area_t a = spu_area_scaled(*area);
449 const int i_error_x = (a.x + a.width) - boundary->width;
455 const int i_error_y = (a.y + a.height) - boundary->height;
461 *area = spu_area_unscaled(a, area->scale);
467 static void SpuRegionPlace(int *x, int *y,
468 const subpicture_t *subpic,
469 const subpicture_region_t *region)
471 assert(region->i_x != INT_MAX && region->i_y != INT_MAX);
472 if (subpic->b_absolute) {
476 if (region->i_align & SUBPICTURE_ALIGN_TOP)
478 else if (region->i_align & SUBPICTURE_ALIGN_BOTTOM)
479 *y = subpic->i_original_picture_height - region->fmt.i_height - region->i_y;
481 *y = subpic->i_original_picture_height / 2 - region->fmt.i_height / 2;
483 if (region->i_align & SUBPICTURE_ALIGN_LEFT)
485 else if (region->i_align & SUBPICTURE_ALIGN_RIGHT)
486 *x = subpic->i_original_picture_width - region->fmt.i_width - region->i_x;
488 *x = subpic->i_original_picture_width / 2 - region->fmt.i_width / 2;
493 * This function compares two 64 bits integers.
494 * It can be used by qsort.
496 static int IntegerCmp(int64_t i0, int64_t i1)
498 return i0 < i1 ? -1 : i0 > i1 ? 1 : 0;
501 * This function compares 2 subpictures using the following properties
502 * (ordered by priority)
503 * 1. absolute positionning
505 * 3. creation order (per channel)
507 * It can be used by qsort.
509 * XXX spu_RenderSubpictures depends heavily on this order.
511 static int SubpictureCmp(const void *s0, const void *s1)
513 subpicture_t *subpic0 = *(subpicture_t**)s0;
514 subpicture_t *subpic1 = *(subpicture_t**)s1;
517 r = IntegerCmp(!subpic0->b_absolute, !subpic1->b_absolute);
519 r = IntegerCmp(subpic0->i_start, subpic1->i_start);
521 r = IntegerCmp(subpic0->i_channel, subpic1->i_channel);
523 r = IntegerCmp(subpic0->i_order, subpic1->i_order);
527 /*****************************************************************************
528 * SpuSelectSubpictures: find the subpictures to display
529 *****************************************************************************
530 * This function parses all subpictures and decides which ones need to be
531 * displayed. If no picture has been selected, display_date will depend on
533 * We also check for ephemer DVD subpictures (subpictures that have
534 * to be removed if a newer one is available), which makes it a lot
535 * more difficult to guess if a subpicture has to be rendered or not.
536 *****************************************************************************/
537 static void SpuSelectSubpictures(spu_t *spu,
538 unsigned int *subpicture_count,
539 subpicture_t **subpicture_array,
540 mtime_t render_subtitle_date,
541 mtime_t render_osd_date,
544 spu_private_t *sys = spu->p;
547 *subpicture_count = 0;
549 /* Create a list of channels */
550 int channel[VOUT_MAX_SUBPICTURES];
551 int channel_count = 0;
553 for (int index = 0; index < VOUT_MAX_SUBPICTURES; index++) {
554 spu_heap_entry_t *entry = &sys->heap.entry[index];
555 if (!entry->subpicture || entry->reject)
557 const int i_channel = entry->subpicture->i_channel;
559 for (i = 0; i < channel_count; i++) {
560 if (channel[i] == i_channel)
563 if (channel_count <= i)
564 channel[channel_count++] = i_channel;
567 /* Fill up the subpicture_array arrays with relevent pictures */
568 for (int i = 0; i < channel_count; i++) {
569 subpicture_t *available_subpic[VOUT_MAX_SUBPICTURES];
570 bool is_available_late[VOUT_MAX_SUBPICTURES];
571 int available_count = 0;
573 mtime_t start_date = render_subtitle_date;
574 mtime_t ephemer_subtitle_date = 0;
575 mtime_t ephemer_osd_date = 0;
576 int64_t ephemer_subtitle_order = INT64_MIN;
577 int64_t ephemer_system_order = INT64_MIN;
579 /* Select available pictures */
580 for (int index = 0; index < VOUT_MAX_SUBPICTURES; index++) {
581 spu_heap_entry_t *entry = &sys->heap.entry[index];
582 subpicture_t *current = entry->subpicture;
586 if (!current || entry->reject) {
588 SpuHeapDeleteAt(&sys->heap, index);
592 if (current->i_channel != channel[i] ||
593 (ignore_osd && !current->b_subtitle))
596 const mtime_t render_date = current->b_subtitle ? render_subtitle_date : render_osd_date;
598 render_date < current->i_start) {
599 /* Too early, come back next monday */
603 mtime_t *ephemer_date_ptr = current->b_subtitle ? &ephemer_subtitle_date : &ephemer_osd_date;
604 int64_t *ephemer_order_ptr = current->b_subtitle ? &ephemer_subtitle_order : &ephemer_system_order;
605 if (current->i_start >= *ephemer_date_ptr) {
606 *ephemer_date_ptr = current->i_start;
607 if (current->i_order > *ephemer_order_ptr)
608 *ephemer_order_ptr = current->i_order;
611 is_stop_valid = !current->b_ephemer || current->i_stop > current->i_start;
613 is_late = is_stop_valid && current->i_stop <= render_date;
615 /* start_date will be used for correct automatic overlap support
616 * in case picture that should not be displayed anymore (display_time)
617 * overlap with a picture to be displayed (current->i_start) */
618 if (current->b_subtitle && !is_late && !current->b_ephemer)
619 start_date = current->i_start;
622 available_subpic[available_count] = current;
623 is_available_late[available_count] = is_late;
627 /* Only forced old picture display at the transition */
628 if (start_date < sys->last_sort_date)
629 start_date = sys->last_sort_date;
631 start_date = INT64_MAX;
633 /* Select pictures to be displayed */
634 for (int index = 0; index < available_count; index++) {
635 subpicture_t *current = available_subpic[index];
636 bool is_late = is_available_late[index];
638 const mtime_t stop_date = current->b_subtitle ? __MAX(start_date, sys->last_sort_date) : render_osd_date;
639 const mtime_t ephemer_date = current->b_subtitle ? ephemer_subtitle_date : ephemer_osd_date;
640 const int64_t ephemer_order = current->b_subtitle ? ephemer_subtitle_order : ephemer_system_order;
642 /* Destroy late and obsolete ephemer subpictures */
643 bool is_rejeted = is_late && current->i_stop <= stop_date;
644 if (current->b_ephemer) {
645 if (current->i_start < ephemer_date)
647 else if (current->i_start == ephemer_date &&
648 current->i_order < ephemer_order)
653 SpuHeapDeleteSubpicture(&sys->heap, current);
655 subpicture_array[(*subpicture_count)++] = current;
659 sys->last_sort_date = render_subtitle_date;
665 * It will transform the provided region into another region suitable for rendering.
667 static void SpuRenderRegion(spu_t *spu,
668 subpicture_region_t **dst_ptr, spu_area_t *dst_area,
669 subpicture_t *subpic, subpicture_region_t *region,
670 const spu_scale_t scale_size,
671 const vlc_fourcc_t *chroma_list,
672 const video_format_t *fmt,
673 const spu_area_t *subtitle_area, int subtitle_area_count,
676 spu_private_t *sys = spu->p;
678 video_format_t fmt_original = region->fmt;
679 bool restore_text = false;
683 video_format_t region_fmt;
684 picture_t *region_picture;
686 /* Invalidate area by default */
687 *dst_area = spu_area_create(0,0, 0,0, scale_size);
690 /* Render text region */
691 if (region->fmt.i_chroma == VLC_CODEC_TEXT) {
692 SpuRenderText(spu, &restore_text, region,
694 render_date - subpic->i_start);
696 /* Check if the rendering has failed ... */
697 if (region->fmt.i_chroma == VLC_CODEC_TEXT)
701 /* Force palette if requested
702 * FIXME b_force_palette and force_crop are applied to all subpictures using palette
703 * instead of only the right one (being the dvd spu).
705 const bool using_palette = region->fmt.i_chroma == VLC_CODEC_YUVP;
706 const bool force_palette = using_palette && sys->force_palette;
707 const bool force_crop = force_palette && sys->force_crop;
708 bool changed_palette = false;
710 /* Compute the margin which is expressed in destination pixel unit
711 * The margin is applied only to subtitle and when no forced crop is
712 * requested (dvd menu) */
714 if (!force_crop && subpic->b_subtitle)
715 y_margin = spu_invscale_h(sys->margin, scale_size);
718 * We compute the position in the rendered size */
719 SpuRegionPlace(&x_offset, &y_offset,
722 /* Save this position for subtitle overlap support
723 * it is really important that there are given without scale_size applied */
724 *dst_area = spu_area_create(x_offset, y_offset,
725 region->fmt.i_width, region->fmt.i_height,
728 /* Handle overlapping subtitles when possible */
729 if (subpic->b_subtitle && !subpic->b_absolute)
730 SpuAreaFixOverlap(dst_area, subtitle_area, subtitle_area_count,
733 /* we copy the area: for the subtitle overlap support we want
734 * to only save the area without margin applied */
735 spu_area_t restrained = *dst_area;
737 /* apply margin to subtitles and correct if they go over the picture edge */
738 if (subpic->b_subtitle)
739 restrained.y -= y_margin;
741 spu_area_t display = spu_area_create(0, 0, fmt->i_width, fmt->i_height,
743 SpuAreaFitInside(&restrained, &display);
745 /* Fix the position for the current scale_size */
746 x_offset = spu_scale_w(restrained.x, restrained.scale);
747 y_offset = spu_scale_h(restrained.y, restrained.scale);
751 video_palette_t *old_palette = region->fmt.p_palette;
752 video_palette_t new_palette;
754 /* We suppose DVD palette here */
755 new_palette.i_entries = 4;
756 for (int i = 0; i < 4; i++)
757 for (int j = 0; j < 4; j++)
758 new_palette.palette[i][j] = sys->palette[i][j];
760 if (old_palette->i_entries == new_palette.i_entries) {
761 for (int i = 0; i < old_palette->i_entries; i++)
762 for (int j = 0; j < 4; j++)
763 changed_palette |= old_palette->palette[i][j] != new_palette.palette[i][j];
765 changed_palette = true;
767 *old_palette = new_palette;
771 region_fmt = region->fmt;
772 region_picture = region->p_picture;
774 bool convert_chroma = true;
775 for (int i = 0; chroma_list[i] && convert_chroma; i++) {
776 if (region_fmt.i_chroma == chroma_list[i])
777 convert_chroma = false;
780 /* Scale from rendered size to destination size */
781 if (sys->scale && sys->scale->p_module &&
782 (!using_palette || (sys->scale_yuvp && sys->scale_yuvp->p_module)) &&
783 (scale_size.w != SCALE_UNIT || scale_size.h != SCALE_UNIT ||
784 using_palette || convert_chroma)) {
785 const unsigned dst_width = spu_scale_w(region->fmt.i_width, scale_size);
786 const unsigned dst_height = spu_scale_h(region->fmt.i_height, scale_size);
788 /* Destroy the cache if unusable */
789 if (region->p_private) {
790 subpicture_region_private_t *private = region->p_private;
791 bool is_changed = false;
793 /* Check resize changes */
794 if (dst_width != private->fmt.i_width ||
795 dst_height != private->fmt.i_height)
798 /* Check forced palette changes */
802 if (convert_chroma && private->fmt.i_chroma != chroma_list[0])
806 subpicture_region_private_Delete(private);
807 region->p_private = NULL;
811 /* Scale if needed into cache */
812 if (!region->p_private && dst_width > 0 && dst_height > 0) {
813 filter_t *scale = sys->scale;
815 picture_t *picture = region->p_picture;
816 picture_Hold(picture);
818 /* Convert YUVP to YUVA/RGBA first for better scaling quality */
820 filter_t *scale_yuvp = sys->scale_yuvp;
822 scale_yuvp->fmt_in.video = region->fmt;
824 scale_yuvp->fmt_out.video = region->fmt;
825 scale_yuvp->fmt_out.video.i_chroma = chroma_list[0];
827 picture = scale_yuvp->pf_video_filter(scale_yuvp, picture);
829 /* Well we will try conversion+scaling */
830 msg_Warn(spu, "%4.4s to %4.4s conversion failed",
831 (const char*)&scale_yuvp->fmt_in.video.i_chroma,
832 (const char*)&scale_yuvp->fmt_out.video.i_chroma);
836 /* Conversion(except from YUVP)/Scaling */
838 (picture->format.i_width != dst_width ||
839 picture->format.i_height != dst_height ||
840 (convert_chroma && !using_palette)))
842 scale->fmt_in.video = picture->format;
843 scale->fmt_out.video = picture->format;
845 scale->fmt_out.i_codec =
846 scale->fmt_out.video.i_chroma = chroma_list[0];
848 scale->fmt_out.video.i_width = dst_width;
849 scale->fmt_out.video.i_height = dst_height;
851 scale->fmt_out.video.i_visible_width =
852 spu_scale_w(region->fmt.i_visible_width, scale_size);
853 scale->fmt_out.video.i_visible_height =
854 spu_scale_h(region->fmt.i_visible_height, scale_size);
856 picture = scale->pf_video_filter(scale, picture);
858 msg_Err(spu, "scaling failed");
863 region->p_private = subpicture_region_private_New(&picture->format);
864 if (region->p_private) {
865 region->p_private->p_picture = picture;
866 if (!region->p_private->p_picture) {
867 subpicture_region_private_Delete(region->p_private);
868 region->p_private = NULL;
871 picture_Release(picture);
876 /* And use the scaled picture */
877 if (region->p_private) {
878 region_fmt = region->p_private->fmt;
879 region_picture = region->p_private->p_picture;
883 /* Force cropping if requested */
885 int crop_x = spu_scale_w(sys->crop.x, scale_size);
886 int crop_y = spu_scale_h(sys->crop.y, scale_size);
887 int crop_width = spu_scale_w(sys->crop.width, scale_size);
888 int crop_height= spu_scale_h(sys->crop.height,scale_size);
890 /* Find the intersection */
891 if (crop_x + crop_width <= x_offset ||
892 x_offset + (int)region_fmt.i_visible_width < crop_x ||
893 crop_y + crop_height <= y_offset ||
894 y_offset + (int)region_fmt.i_visible_height < crop_y) {
895 /* No intersection */
896 region_fmt.i_visible_width =
897 region_fmt.i_visible_height = 0;
899 int x, y, x_end, y_end;
900 x = __MAX(crop_x, x_offset);
901 y = __MAX(crop_y, y_offset);
902 x_end = __MIN(crop_x + crop_width,
903 x_offset + (int)region_fmt.i_visible_width);
904 y_end = __MIN(crop_y + crop_height,
905 y_offset + (int)region_fmt.i_visible_height);
907 region_fmt.i_x_offset = x - x_offset;
908 region_fmt.i_y_offset = y - y_offset;
909 region_fmt.i_visible_width = x_end - x;
910 region_fmt.i_visible_height = y_end - y;
912 x_offset = __MAX(x, 0);
913 y_offset = __MAX(y, 0);
917 subpicture_region_t *dst = *dst_ptr = subpicture_region_New(®ion_fmt);
923 picture_Release(dst->p_picture);
924 dst->p_picture = picture_Hold(region_picture);
925 int fade_alpha = 255;
926 if (subpic->b_fade) {
927 mtime_t fade_start = subpic->i_start + 3 * (subpic->i_stop - subpic->i_start) / 4;
929 if (fade_start <= render_date && fade_start < subpic->i_stop)
930 fade_alpha = 255 * (subpic->i_stop - render_date) /
931 (subpic->i_stop - fade_start);
933 dst->i_alpha = fade_alpha * subpic->i_alpha * region->i_alpha / 65025;
938 /* Some forms of subtitles need to be re-rendered more than
939 * once, eg. karaoke. We therefore restore the region to its
940 * pre-rendered state, so the next time through everything is
943 if (region->p_picture) {
944 picture_Release(region->p_picture);
945 region->p_picture = NULL;
947 if (region->p_private) {
948 subpicture_region_private_Delete(region->p_private);
949 region->p_private = NULL;
951 region->fmt = fmt_original;
956 * This function renders all sub picture units in the list.
958 static subpicture_t *SpuRenderSubpictures(spu_t *spu,
959 unsigned int i_subpicture,
960 subpicture_t **pp_subpicture,
961 const vlc_fourcc_t *chroma_list,
962 const video_format_t *fmt_dst,
963 const video_format_t *fmt_src,
964 mtime_t render_subtitle_date,
965 mtime_t render_osd_date)
967 spu_private_t *sys = spu->p;
969 /* Count the number of regions and subtitle regions */
970 unsigned int subtitle_region_count = 0;
971 unsigned int region_count = 0;
972 for (unsigned i = 0; i < i_subpicture; i++) {
973 const subpicture_t *subpic = pp_subpicture[i];
976 for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
979 if (subpic->b_subtitle)
980 subtitle_region_count += count;
981 region_count += count;
983 if (region_count <= 0)
986 /* Create the output subpicture */
987 subpicture_t *output = subpicture_New(NULL);
990 output->i_original_picture_width = fmt_dst->i_width;
991 output->i_original_picture_height = fmt_dst->i_height;
992 subpicture_region_t **output_last_ptr = &output->p_region;
994 /* Allocate area array for subtitle overlap */
995 spu_area_t subtitle_area_buffer[VOUT_MAX_SUBPICTURES];
996 spu_area_t *subtitle_area;
997 int subtitle_area_count;
999 subtitle_area_count = 0;
1000 subtitle_area = subtitle_area_buffer;
1001 if (subtitle_region_count > sizeof(subtitle_area_buffer)/sizeof(*subtitle_area_buffer))
1002 subtitle_area = calloc(subtitle_region_count, sizeof(*subtitle_area));
1004 /* Process all subpictures and regions (in the right order) */
1005 for (unsigned int index = 0; index < i_subpicture; index++) {
1006 subpicture_t *subpic = pp_subpicture[index];
1007 subpicture_region_t *region;
1009 if (!subpic->p_region)
1012 if (subpic->i_original_picture_width <= 0 ||
1013 subpic->i_original_picture_height <= 0) {
1014 if (subpic->i_original_picture_width > 0 ||
1015 subpic->i_original_picture_height > 0)
1016 msg_Err(spu, "original picture size %dx%d is unsupported",
1017 subpic->i_original_picture_width,
1018 subpic->i_original_picture_height);
1020 msg_Warn(spu, "original picture size is undefined");
1022 subpic->i_original_picture_width = fmt_src->i_width;
1023 subpic->i_original_picture_height = fmt_src->i_height;
1027 /* FIXME aspect ratio ? */
1028 sys->text->fmt_out.video.i_width =
1029 sys->text->fmt_out.video.i_visible_width = subpic->i_original_picture_width;
1031 sys->text->fmt_out.video.i_height =
1032 sys->text->fmt_out.video.i_visible_height = subpic->i_original_picture_height;
1035 /* Render all regions
1036 * We always transform non absolute subtitle into absolute one on the
1037 * first rendering to allow good subtitle overlap support.
1039 for (region = subpic->p_region; region != NULL; region = region->p_next) {
1042 /* Compute region scale AR */
1043 video_format_t region_fmt = region->fmt;
1044 if (region_fmt.i_sar_num <= 0 || region_fmt.i_sar_den <= 0) {
1045 region_fmt.i_sar_num = fmt_src->i_sar_num;
1046 region_fmt.i_sar_den = fmt_src->i_sar_den;
1049 /* Compute scaling from original size to destination size
1050 * FIXME The current scaling ensure that the heights match, the width being
1053 spu_scale_t scale = spu_scale_createq((int64_t)fmt_dst->i_height * fmt_dst->i_sar_den * region_fmt.i_sar_num,
1054 (int64_t)subpic->i_original_picture_height * fmt_dst->i_sar_num * region_fmt.i_sar_den,
1056 subpic->i_original_picture_height);
1058 /* Check scale validity */
1059 if (scale.w <= 0 || scale.h <= 0)
1063 SpuRenderRegion(spu, output_last_ptr, &area,
1064 subpic, region, scale,
1065 chroma_list, fmt_dst,
1066 subtitle_area, subtitle_area_count,
1067 subpic->b_subtitle ? render_subtitle_date : render_osd_date);
1068 if (*output_last_ptr)
1069 output_last_ptr = &(*output_last_ptr)->p_next;
1071 if (subpic->b_subtitle) {
1072 area = spu_area_unscaled(area, scale);
1073 if (!subpic->b_absolute && area.width > 0 && area.height > 0) {
1074 region->i_x = area.x;
1075 region->i_y = area.y;
1078 subtitle_area[subtitle_area_count++] = area;
1081 if (subpic->b_subtitle && subpic->p_region)
1082 subpic->b_absolute = true;
1086 if (subtitle_area != subtitle_area_buffer)
1087 free(subtitle_area);
1092 /*****************************************************************************
1093 * Object variables callbacks
1094 *****************************************************************************/
1096 /*****************************************************************************
1097 * UpdateSPU: update subpicture settings
1098 *****************************************************************************
1099 * This function is called from CropCallback and at initialization time, to
1100 * retrieve crop information from the input.
1101 *****************************************************************************/
1102 static void UpdateSPU(spu_t *spu, vlc_object_t *object)
1104 spu_private_t *sys = spu->p;
1107 vlc_mutex_lock(&sys->lock);
1109 sys->force_palette = false;
1110 sys->force_crop = false;
1112 if (var_Get(object, "highlight", &val) || !val.b_bool) {
1113 vlc_mutex_unlock(&sys->lock);
1117 sys->force_crop = true;
1118 sys->crop.x = var_GetInteger(object, "x-start");
1119 sys->crop.y = var_GetInteger(object, "y-start");
1120 sys->crop.width = var_GetInteger(object, "x-end") - sys->crop.x;
1121 sys->crop.height = var_GetInteger(object, "y-end") - sys->crop.y;
1123 if (var_Get(object, "menu-palette", &val) == VLC_SUCCESS) {
1124 memcpy(sys->palette, val.p_address, 16);
1125 sys->force_palette = true;
1127 vlc_mutex_unlock(&sys->lock);
1129 msg_Dbg(object, "crop: %i,%i,%i,%i, palette forced: %i",
1130 sys->crop.x, sys->crop.y,
1131 sys->crop.width, sys->crop.height,
1132 sys->force_palette);
1135 /*****************************************************************************
1136 * CropCallback: called when the highlight properties are changed
1137 *****************************************************************************
1138 * This callback is called from the input thread when we need cropping
1139 *****************************************************************************/
1140 static int CropCallback(vlc_object_t *object, char const *var,
1141 vlc_value_t oldval, vlc_value_t newval, void *data)
1143 VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(var);
1145 UpdateSPU((spu_t *)data, object);
1149 /*****************************************************************************
1150 * Buffers allocation callbacks for the filters
1151 *****************************************************************************/
1153 static subpicture_t *sub_new_buffer(filter_t *filter)
1155 filter_owner_sys_t *sys = filter->p_owner;
1157 subpicture_t *subpicture = subpicture_New(NULL);
1159 subpicture->i_channel = sys->channel;
1162 static void sub_del_buffer(filter_t *filter, subpicture_t *subpic)
1165 subpicture_Delete(subpic);
1168 static int SubSourceAllocationInit(filter_t *filter, void *data)
1172 filter_owner_sys_t *sys = malloc(sizeof(*sys));
1174 return VLC_EGENERIC;
1176 filter->pf_sub_buffer_new = sub_new_buffer;
1177 filter->pf_sub_buffer_del = sub_del_buffer;
1179 filter->p_owner = sys;
1180 sys->channel = spu_RegisterChannel(spu);
1186 static void SubSourceAllocationClean(filter_t *filter)
1188 filter_owner_sys_t *sys = filter->p_owner;
1190 spu_ClearChannel(sys->spu, sys->channel);
1191 free(filter->p_owner);
1194 /*****************************************************************************
1196 *****************************************************************************/
1200 * Creates the subpicture unit
1202 * \param p_this the parent object which creates the subpicture unit
1204 spu_t *spu_Create(vlc_object_t *object)
1206 spu_t *spu = vlc_custom_create(object,
1207 sizeof(spu_t) + sizeof(spu_private_t),
1212 /* Initialize spu fields */
1213 spu_private_t *sys = spu->p = (spu_private_t*)&spu[1];
1215 /* Initialize private fields */
1216 vlc_mutex_init(&sys->lock);
1218 SpuHeapInit(&sys->heap);
1222 sys->scale_yuvp = NULL;
1224 sys->margin = var_InheritInteger(spu, "sub-margin");
1226 /* Register the default subpicture channel */
1227 sys->channel = SPU_DEFAULT_CHANNEL + 1;
1229 sys->source_chain_update = NULL;
1230 sys->filter_chain_update = NULL;
1231 vlc_mutex_init(&sys->source_chain_lock);
1232 vlc_mutex_init(&sys->filter_chain_lock);
1233 sys->source_chain = filter_chain_New(spu, "sub source", false,
1234 SubSourceAllocationInit,
1235 SubSourceAllocationClean,
1237 sys->filter_chain = filter_chain_New(spu, "sub filter", false,
1242 /* Load text and scale module */
1243 sys->text = SpuRenderCreateAndLoadText(spu);
1245 /* XXX spu->p_scale is used for all conversion/scaling except yuvp to
1247 sys->scale = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1248 VLC_CODEC_YUVA, VLC_CODEC_RGBA, true);
1250 /* This one is used for YUVP to YUVA/RGBA without scaling
1251 * FIXME rename it */
1252 sys->scale_yuvp = SpuRenderCreateAndLoadScale(VLC_OBJECT(spu),
1253 VLC_CODEC_YUVP, VLC_CODEC_YUVA, false);
1256 sys->last_sort_date = -1;
1262 * Destroy the subpicture unit
1264 * \param p_this the parent object which destroys the subpicture unit
1266 void spu_Destroy(spu_t *spu)
1268 spu_private_t *sys = spu->p;
1271 FilterRelease(sys->text);
1273 if (sys->scale_yuvp)
1274 FilterRelease(sys->scale_yuvp);
1277 FilterRelease(sys->scale);
1279 filter_chain_Delete(sys->source_chain);
1280 filter_chain_Delete(sys->filter_chain);
1281 vlc_mutex_destroy(&sys->source_chain_lock);
1282 vlc_mutex_destroy(&sys->filter_chain_lock);
1283 free(sys->source_chain_update);
1284 free(sys->filter_chain_update);
1286 /* Destroy all remaining subpictures */
1287 SpuHeapClean(&sys->heap);
1289 vlc_mutex_destroy(&sys->lock);
1291 vlc_object_release(spu);
1295 * Attach/Detach the SPU from any input
1297 * \param p_this the object in which to destroy the subpicture unit
1298 * \param b_attach to select attach or detach
1300 void spu_Attach(spu_t *spu, vlc_object_t *input, bool attach)
1303 UpdateSPU(spu, input);
1304 var_Create(input, "highlight", VLC_VAR_BOOL);
1305 var_AddCallback(input, "highlight", CropCallback, spu);
1307 vlc_mutex_lock(&spu->p->lock);
1308 spu->p->input = input;
1311 FilterRelease(spu->p->text);
1312 spu->p->text = SpuRenderCreateAndLoadText(spu);
1314 vlc_mutex_unlock(&spu->p->lock);
1316 vlc_mutex_lock(&spu->p->lock);
1317 spu->p->input = NULL;
1318 vlc_mutex_unlock(&spu->p->lock);
1320 /* Delete callbacks */
1321 var_DelCallback(input, "highlight", CropCallback, spu);
1322 var_Destroy(input, "highlight");
1327 * Inform the SPU filters of mouse event
1329 int spu_ProcessMouse(spu_t *spu,
1330 const vlc_mouse_t *mouse,
1331 const video_format_t *fmt)
1333 spu_private_t *sys = spu->p;
1335 vlc_mutex_lock(&sys->source_chain_lock);
1336 filter_chain_MouseEvent(sys->source_chain, mouse, fmt);
1337 vlc_mutex_unlock(&sys->source_chain_lock);
1343 * Display a subpicture
1345 * Remove the reservation flag of a subpicture, which will cause it to be
1346 * ready for display.
1347 * \param spu the subpicture unit object
1348 * \param subpic the subpicture to display
1350 void spu_PutSubpicture(spu_t *spu, subpicture_t *subpic)
1352 spu_private_t *sys = spu->p;
1354 /* Update sub-filter chain */
1355 vlc_mutex_lock(&sys->lock);
1356 char *chain_update = sys->filter_chain_update;
1357 sys->filter_chain_update = NULL;
1358 vlc_mutex_unlock(&sys->lock);
1360 bool is_left_empty = false;
1362 vlc_mutex_lock(&sys->filter_chain_lock);
1364 filter_chain_Reset(sys->filter_chain, NULL, NULL);
1366 filter_chain_AppendFromString(spu->p->filter_chain, chain_update);
1368 /* "sub-source" was formerly "sub-filter", so now the "sub-filter"
1369 configuration may contain sub-filters or sub-sources configurations.
1370 if the filters chain was left empty it may indicate that it's a sub-source configuration */
1371 is_left_empty = (filter_chain_GetLength(spu->p->filter_chain) == 0);
1373 vlc_mutex_unlock(&sys->filter_chain_lock);
1376 if (is_left_empty) {
1377 /* try to use the configuration as a sub-source configuration */
1379 vlc_mutex_lock(&sys->lock);
1380 if (!sys->source_chain_update || !*sys->source_chain_update) {
1381 free(sys->source_chain_update);
1382 sys->source_chain_update = chain_update;
1383 chain_update = NULL;
1385 vlc_mutex_unlock(&sys->lock);
1390 /* Run filter chain on the new subpicture */
1391 subpic = filter_chain_SubFilter(spu->p->filter_chain, subpic);
1395 /* SPU_DEFAULT_CHANNEL always reset itself */
1396 if (subpic->i_channel == SPU_DEFAULT_CHANNEL)
1397 spu_ClearChannel(spu, SPU_DEFAULT_CHANNEL);
1399 /* p_private is for spu only and cannot be non NULL here */
1400 for (subpicture_region_t *r = subpic->p_region; r != NULL; r = r->p_next)
1401 assert(r->p_private == NULL);
1404 vlc_mutex_lock(&sys->lock);
1405 if (SpuHeapPush(&sys->heap, subpic)) {
1406 vlc_mutex_unlock(&sys->lock);
1407 msg_Err(spu, "subpicture heap full");
1408 subpicture_Delete(subpic);
1411 vlc_mutex_unlock(&sys->lock);
1414 subpicture_t *spu_Render(spu_t *spu,
1415 const vlc_fourcc_t *chroma_list,
1416 const video_format_t *fmt_dst,
1417 const video_format_t *fmt_src,
1418 mtime_t render_subtitle_date,
1419 mtime_t render_osd_date,
1422 spu_private_t *sys = spu->p;
1424 /* Update sub-source chain */
1425 vlc_mutex_lock(&sys->lock);
1426 char *chain_update = sys->source_chain_update;
1427 sys->source_chain_update = NULL;
1428 vlc_mutex_unlock(&sys->lock);
1430 vlc_mutex_lock(&sys->source_chain_lock);
1432 filter_chain_Reset(sys->source_chain, NULL, NULL);
1434 filter_chain_AppendFromString(spu->p->source_chain, chain_update);
1438 /* Run subpicture sources */
1439 filter_chain_SubSource(sys->source_chain, render_osd_date);
1440 vlc_mutex_unlock(&sys->source_chain_lock);
1442 static const vlc_fourcc_t chroma_list_default_yuv[] = {
1448 static const vlc_fourcc_t chroma_list_default_rgb[] = {
1455 if (!chroma_list || *chroma_list == 0)
1456 chroma_list = vlc_fourcc_IsYUV(fmt_dst->i_chroma) ? chroma_list_default_yuv
1457 : chroma_list_default_rgb;
1459 vlc_mutex_lock(&sys->lock);
1461 unsigned int subpicture_count;
1462 subpicture_t *subpicture_array[VOUT_MAX_SUBPICTURES];
1464 /* Get an array of subpictures to render */
1465 SpuSelectSubpictures(spu, &subpicture_count, subpicture_array,
1466 render_subtitle_date, render_osd_date, ignore_osd);
1467 if (subpicture_count <= 0) {
1468 vlc_mutex_unlock(&sys->lock);
1472 /* Updates the subpictures */
1473 for (unsigned i = 0; i < subpicture_count; i++) {
1474 subpicture_t *subpic = subpicture_array[i];
1475 subpicture_Update(subpic,
1477 subpic->b_subtitle ? render_subtitle_date : render_osd_date);
1480 /* Now order the subpicture array
1481 * XXX The order is *really* important for overlap subtitles positionning */
1482 qsort(subpicture_array, subpicture_count, sizeof(*subpicture_array), SubpictureCmp);
1484 /* Render the subpictures */
1485 subpicture_t *render = SpuRenderSubpictures(spu,
1486 subpicture_count, subpicture_array,
1490 render_subtitle_date,
1492 vlc_mutex_unlock(&sys->lock);
1497 void spu_OffsetSubtitleDate(spu_t *spu, mtime_t duration)
1499 spu_private_t *sys = spu->p;
1501 vlc_mutex_lock(&sys->lock);
1502 for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
1503 spu_heap_entry_t *entry = &sys->heap.entry[i];
1504 subpicture_t *current = entry->subpicture;
1506 if (current && current->b_subtitle) {
1507 if (current->i_start > 0)
1508 current->i_start += duration;
1509 if (current->i_stop > 0)
1510 current->i_stop += duration;
1513 vlc_mutex_unlock(&sys->lock);
1516 int spu_RegisterChannel(spu_t *spu)
1518 spu_private_t *sys = spu->p;
1520 vlc_mutex_lock(&sys->lock);
1521 int channel = sys->channel++;
1522 vlc_mutex_unlock(&sys->lock);
1527 void spu_ClearChannel(spu_t *spu, int channel)
1529 spu_private_t *sys = spu->p;
1531 vlc_mutex_lock(&sys->lock);
1533 for (int i = 0; i < VOUT_MAX_SUBPICTURES; i++) {
1534 spu_heap_entry_t *entry = &sys->heap.entry[i];
1535 subpicture_t *subpic = entry->subpicture;
1539 if (subpic->i_channel != channel && (channel != -1 || subpic->i_channel == SPU_DEFAULT_CHANNEL))
1542 /* You cannot delete subpicture outside of spu_SortSubpictures */
1543 entry->reject = true;
1546 vlc_mutex_unlock(&sys->lock);
1549 void spu_ChangeSources(spu_t *spu, const char *filters)
1551 spu_private_t *sys = spu->p;
1553 vlc_mutex_lock(&sys->lock);
1555 free(sys->source_chain_update);
1556 sys->source_chain_update = strdup(filters);
1558 vlc_mutex_unlock(&sys->lock);
1561 void spu_ChangeFilters(spu_t *spu, const char *filters)
1563 spu_private_t *sys = spu->p;
1565 vlc_mutex_lock(&sys->lock);
1567 free(sys->filter_chain_update);
1568 sys->filter_chain_update = strdup(filters);
1570 vlc_mutex_unlock(&sys->lock);
1573 void spu_ChangeMargin(spu_t *spu, int margin)
1575 spu_private_t *sys = spu->p;
1577 vlc_mutex_lock(&sys->lock);
1578 sys->margin = margin;
1579 vlc_mutex_unlock(&sys->lock);