qt: playlist: use item title if available
[vlc.git] / src / video_output / video_text.c
blobd11e4078890d4620f32a4ba780eedb87c5399067
1 /*****************************************************************************
2 * video_text.c : OSD text manipulation functions
3 *****************************************************************************
4 * Copyright (C) 1999-2010 VLC authors and VideoLAN
6 * Author: Sigmund Augdal Helberg <dnumgis@videolan.org>
7 * Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27 #include <assert.h>
29 #include <vlc_common.h>
30 #include <vlc_vout.h>
31 #include <vlc_vout_osd.h>
33 typedef struct {
34 int position;
35 char *text;
36 } osd_spu_updater_sys_t;
38 static int OSDTextValidate(subpicture_t *subpic,
39 bool has_src_changed, const video_format_t *fmt_src,
40 bool has_dst_changed, const video_format_t *fmt_dst,
41 vlc_tick_t ts)
43 VLC_UNUSED(subpic); VLC_UNUSED(ts);
44 VLC_UNUSED(fmt_src); VLC_UNUSED(has_src_changed);
45 VLC_UNUSED(fmt_dst);
47 if( !has_dst_changed )
48 return VLC_SUCCESS;
49 return VLC_EGENERIC;
52 static void OSDTextUpdate(subpicture_t *subpic,
53 const video_format_t *fmt_src,
54 const video_format_t *fmt_dst,
55 vlc_tick_t ts)
57 osd_spu_updater_sys_t *sys = subpic->updater.p_sys;
58 VLC_UNUSED(fmt_src); VLC_UNUSED(ts);
60 if( fmt_dst->i_sar_num <= 0 || fmt_dst->i_sar_den <= 0 )
61 return;
63 subpic->b_absolute = false;
64 subpic->i_original_picture_width = fmt_dst->i_visible_width * fmt_dst->i_sar_num / fmt_dst->i_sar_den;
65 subpic->i_original_picture_height = fmt_dst->i_visible_height;
67 video_format_t fmt;
68 video_format_Init( &fmt, VLC_CODEC_TEXT);
69 fmt.i_sar_num = 1;
70 fmt.i_sar_den = 1;
72 subpicture_region_t *r = subpic->p_region = subpicture_region_New(&fmt);
73 if (!r)
74 return;
76 r->p_text = text_segment_New( sys->text );
78 const float margin_ratio = 0.04;
79 const int margin_h = margin_ratio * fmt_dst->i_visible_width;
80 const int margin_v = margin_ratio * fmt_dst->i_visible_height;
82 r->i_text_align = sys->position;
83 r->i_align = sys->position;
84 r->i_x = 0;
85 if (r->i_align & SUBPICTURE_ALIGN_LEFT)
86 r->i_x += margin_h + fmt_dst->i_x_offset;
87 else if (r->i_align & SUBPICTURE_ALIGN_RIGHT)
88 r->i_x += margin_h - fmt_dst->i_x_offset;
90 r->i_y = 0;
91 if (r->i_align & SUBPICTURE_ALIGN_TOP )
92 r->i_y += margin_v + fmt_dst->i_y_offset;
93 else if (r->i_align & SUBPICTURE_ALIGN_BOTTOM )
94 r->i_y += margin_v - fmt_dst->i_y_offset;
97 static void OSDTextDestroy(subpicture_t *subpic)
99 osd_spu_updater_sys_t *sys = subpic->updater.p_sys;
101 free(sys->text);
102 free(sys);
105 void vout_OSDText(vout_thread_t *vout, int channel,
106 int position, vlc_tick_t duration, const char *text)
108 assert( (position & ~SUBPICTURE_ALIGN_MASK) == 0);
109 if (!var_InheritBool(vout, "osd") || duration <= 0)
110 return;
112 osd_spu_updater_sys_t *sys = malloc(sizeof(*sys));
113 if (!sys)
114 return;
115 sys->position = position;
116 sys->text = strdup(text);
118 subpicture_updater_t updater = {
119 .pf_validate = OSDTextValidate,
120 .pf_update = OSDTextUpdate,
121 .pf_destroy = OSDTextDestroy,
122 .p_sys = sys,
124 subpicture_t *subpic = subpicture_New(&updater);
125 if (!subpic) {
126 free(sys->text);
127 free(sys);
128 return;
131 subpic->i_channel = channel;
132 subpic->i_start = vlc_tick_now();
133 subpic->i_stop = subpic->i_start + duration;
134 subpic->b_ephemer = true;
135 subpic->b_absolute = false;
136 subpic->b_fade = true;
138 vout_PutSubpicture(vout, subpic);
141 void vout_OSDMessageVa(vout_thread_t *vout, int channel,
142 const char *format, va_list args)
144 char *string;
145 if (vasprintf(&string, format, args) != -1) {
146 vout_OSDText(vout, channel,
147 SUBPICTURE_ALIGN_TOP|SUBPICTURE_ALIGN_RIGHT, VLC_TICK_FROM_SEC(1),
148 string);
149 free(string);