/src/vlc/modules/codec/substext.h
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * subsdec.c : text subtitle decoder |
3 | | ***************************************************************************** |
4 | | * Copyright © 2011-2015 VLC authors and VideoLAN |
5 | | * |
6 | | * Authors: Laurent Aimer <fenrir@videolan.org> |
7 | | * Jean-Baptiste Kempf <jb@videolan.org> |
8 | | * |
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. |
13 | | * |
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. |
18 | | * |
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 | | *****************************************************************************/ |
23 | | |
24 | | #include <vlc_strings.h> |
25 | | #include <vlc_text_style.h> |
26 | | #include <vlc_subpicture.h> |
27 | | |
28 | | typedef struct substext_updater_region_t substext_updater_region_t; |
29 | | |
30 | | enum substext_updater_region_flags_e |
31 | | { |
32 | | UPDT_REGION_ORIGIN_X_IS_RATIO = 1 << 0, |
33 | | UPDT_REGION_ORIGIN_Y_IS_RATIO = 1 << 1, |
34 | | UPDT_REGION_EXTENT_X_IS_RATIO = 1 << 2, |
35 | | UPDT_REGION_EXTENT_Y_IS_RATIO = 1 << 3, |
36 | | UPDT_REGION_IGNORE_BACKGROUND = 1 << 4, |
37 | | UPDT_REGION_USES_GRID_COORDINATES = 1 << 5, |
38 | | UPDT_REGION_FIXED_DONE = 1 << 31, |
39 | | }; |
40 | | |
41 | | struct substext_updater_region_t |
42 | | { |
43 | | struct |
44 | | { |
45 | | float x; |
46 | | float y; |
47 | | } origin, extent; |
48 | | /* store above percentile meanings as modifier flags */ |
49 | | int flags; /* subpicture_updater_sys_region_flags_e */ |
50 | | int align; /* alignment of the region itself */ |
51 | | int inner_align; /* alignment of content inside the region */ |
52 | | text_style_t *p_region_style; |
53 | | text_segment_t *p_segments; |
54 | | substext_updater_region_t *p_next; |
55 | | }; |
56 | | |
57 | | typedef struct |
58 | | { |
59 | | /* a min of one region */ |
60 | | substext_updater_region_t region; |
61 | | |
62 | | /* styling */ |
63 | | text_style_t *p_default_style; /* decoder (full or partial) defaults */ |
64 | | float margin_ratio; |
65 | | vlc_tick_t i_next_update; |
66 | | bool b_blink_even; |
67 | | } subtext_updater_sys_t; |
68 | | |
69 | | static inline void SubpictureUpdaterSysRegionClean(substext_updater_region_t *p_updtregion) |
70 | 0 | { |
71 | 0 | text_segment_ChainDelete( p_updtregion->p_segments ); |
72 | 0 | text_style_Delete( p_updtregion->p_region_style ); |
73 | 0 | } Unexecuted instantiation: substtml.c:SubpictureUpdaterSysRegionClean Unexecuted instantiation: subsvtt.c:SubpictureUpdaterSysRegionClean |
74 | | |
75 | | static inline void SubpictureUpdaterSysRegionInit(substext_updater_region_t *p_updtregion) |
76 | 0 | { |
77 | 0 | memset(p_updtregion, 0, sizeof(*p_updtregion)); |
78 | 0 | p_updtregion->align = SUBPICTURE_ALIGN_BOTTOM; |
79 | 0 | p_updtregion->inner_align = 0; |
80 | 0 | } Unexecuted instantiation: substtml.c:SubpictureUpdaterSysRegionInit Unexecuted instantiation: subsvtt.c:SubpictureUpdaterSysRegionInit |
81 | | |
82 | | static inline substext_updater_region_t *SubpictureUpdaterSysRegionNew( void ) |
83 | 0 | { |
84 | 0 | substext_updater_region_t *p_region = malloc(sizeof(*p_region)); |
85 | 0 | if(p_region) |
86 | 0 | SubpictureUpdaterSysRegionInit(p_region); |
87 | 0 | return p_region; |
88 | 0 | } Unexecuted instantiation: substtml.c:SubpictureUpdaterSysRegionNew Unexecuted instantiation: subsvtt.c:SubpictureUpdaterSysRegionNew |
89 | | |
90 | | static inline void SubpictureUpdaterSysRegionAdd(substext_updater_region_t *p_prev, |
91 | | substext_updater_region_t *p_new) |
92 | 0 | { |
93 | 0 | substext_updater_region_t **pp_next = &p_prev->p_next; |
94 | 0 | for(; *pp_next; pp_next = &(*pp_next)->p_next); |
95 | 0 | *pp_next = p_new; |
96 | 0 | } Unexecuted instantiation: substtml.c:SubpictureUpdaterSysRegionAdd Unexecuted instantiation: subsvtt.c:SubpictureUpdaterSysRegionAdd |
97 | | |
98 | | static int SubpictureTextValidate(subpicture_t *subpic, |
99 | | bool has_src_changed, const video_format_t *fmt_src, |
100 | | bool has_dst_changed, const video_format_t *fmt_dst, |
101 | | vlc_tick_t ts) |
102 | 0 | { |
103 | 0 | subtext_updater_sys_t *sys = subpic->updater.p_sys; |
104 | 0 | VLC_UNUSED(fmt_src); VLC_UNUSED(fmt_dst); |
105 | |
|
106 | 0 | if (!has_src_changed && !has_dst_changed && |
107 | 0 | (sys->i_next_update == VLC_TICK_INVALID || sys->i_next_update > ts)) |
108 | 0 | return VLC_SUCCESS; |
109 | | |
110 | 0 | substext_updater_region_t *p_updtregion = &sys->region; |
111 | |
|
112 | 0 | if (!(p_updtregion->flags & UPDT_REGION_FIXED_DONE) && |
113 | 0 | subpic->b_absolute && subpic->p_region && |
114 | 0 | subpic->i_original_picture_width > 0 && |
115 | 0 | subpic->i_original_picture_height > 0) |
116 | 0 | { |
117 | 0 | p_updtregion->flags |= UPDT_REGION_FIXED_DONE; |
118 | 0 | p_updtregion->origin.x = subpic->p_region->i_x; |
119 | 0 | p_updtregion->origin.y = subpic->p_region->i_y; |
120 | 0 | p_updtregion->extent.x = subpic->i_original_picture_width; |
121 | 0 | p_updtregion->extent.y = subpic->i_original_picture_height; |
122 | 0 | p_updtregion->flags &= ~(UPDT_REGION_ORIGIN_X_IS_RATIO|UPDT_REGION_ORIGIN_Y_IS_RATIO| |
123 | 0 | UPDT_REGION_EXTENT_X_IS_RATIO|UPDT_REGION_EXTENT_Y_IS_RATIO); |
124 | 0 | } |
125 | |
|
126 | 0 | return VLC_EGENERIC; |
127 | 0 | } Unexecuted instantiation: substtml.c:SubpictureTextValidate Unexecuted instantiation: subsvtt.c:SubpictureTextValidate |
128 | | |
129 | | static void SubpictureTextUpdate(subpicture_t *subpic, |
130 | | const video_format_t *fmt_src, |
131 | | const video_format_t *fmt_dst, |
132 | | vlc_tick_t ts) |
133 | 0 | { |
134 | 0 | subtext_updater_sys_t *sys = subpic->updater.p_sys; |
135 | 0 | VLC_UNUSED(fmt_src); |
136 | |
|
137 | 0 | if (fmt_dst->i_sar_num <= 0 || fmt_dst->i_sar_den <= 0) |
138 | 0 | return; |
139 | | |
140 | 0 | video_format_t fmt; |
141 | 0 | video_format_Init(&fmt, VLC_CODEC_TEXT); |
142 | | |
143 | | /* NOTE about fmt_dst: |
144 | | * fmt_dst area and A/R will change to display once WxH of the |
145 | | * display is greater than the source video in direct rendering. |
146 | | * This will cause weird sudded region "moves" or "linebreaks" when |
147 | | * resizing window, mostly vertically. |
148 | | * see changes by 4a49754d943560fe79bc42f107d8ce566ea24898 */ |
149 | |
|
150 | 0 | if( sys->region.flags & UPDT_REGION_USES_GRID_COORDINATES ) |
151 | 0 | { |
152 | 0 | fmt.i_sar_num = 4; |
153 | 0 | fmt.i_sar_den = 3; |
154 | 0 | subpic->i_original_picture_width = fmt_dst->i_visible_height * fmt.i_sar_num / fmt.i_sar_den; |
155 | 0 | subpic->i_original_picture_height = fmt_dst->i_visible_height; |
156 | 0 | } |
157 | 0 | else |
158 | 0 | { |
159 | 0 | subpic->i_original_picture_width = fmt_dst->i_width * fmt_dst->i_sar_num / fmt_dst->i_sar_den; |
160 | 0 | subpic->i_original_picture_height = fmt_dst->i_height; |
161 | 0 | fmt.i_sar_num = 1; |
162 | 0 | fmt.i_sar_den = 1; |
163 | 0 | } |
164 | |
|
165 | 0 | bool b_schedule_blink_update = false; |
166 | 0 | subpicture_region_t **pp_last_region = &subpic->p_region; |
167 | |
|
168 | 0 | for( substext_updater_region_t *p_updtregion = &sys->region; |
169 | 0 | p_updtregion; p_updtregion = p_updtregion->p_next ) |
170 | 0 | { |
171 | 0 | subpicture_region_t *r = *pp_last_region = subpicture_region_New(&fmt); |
172 | 0 | if (!r) |
173 | 0 | return; |
174 | 0 | pp_last_region = &r->p_next; |
175 | |
|
176 | 0 | r->p_text = text_segment_Copy( p_updtregion->p_segments ); |
177 | 0 | r->i_align = p_updtregion->align; |
178 | 0 | r->i_text_align = p_updtregion->inner_align; |
179 | 0 | r->b_noregionbg = p_updtregion->flags & UPDT_REGION_IGNORE_BACKGROUND; |
180 | 0 | r->b_gridmode = p_updtregion->flags & UPDT_REGION_USES_GRID_COORDINATES; |
181 | |
|
182 | 0 | if (!(p_updtregion->flags & UPDT_REGION_FIXED_DONE)) |
183 | 0 | { |
184 | 0 | const float margin_ratio = sys->margin_ratio; |
185 | 0 | const int margin_h = margin_ratio * (( r->b_gridmode ) ? (unsigned) subpic->i_original_picture_width |
186 | 0 | : fmt_dst->i_visible_width ); |
187 | 0 | const int margin_v = margin_ratio * fmt_dst->i_visible_height; |
188 | | |
189 | | /* subpic invisible margins sizes */ |
190 | 0 | const int outerright_h = fmt_dst->i_width - (fmt_dst->i_visible_width + fmt_dst->i_x_offset); |
191 | 0 | const int outerbottom_v = fmt_dst->i_height - (fmt_dst->i_visible_height + fmt_dst->i_y_offset); |
192 | | /* regions usable */ |
193 | 0 | const int inner_w = fmt_dst->i_visible_width - margin_h * 2; |
194 | 0 | const int inner_h = fmt_dst->i_visible_height - margin_v * 2; |
195 | |
|
196 | 0 | if (r->i_align & SUBPICTURE_ALIGN_LEFT) |
197 | 0 | r->i_x = margin_h + fmt_dst->i_x_offset; |
198 | 0 | else if (r->i_align & SUBPICTURE_ALIGN_RIGHT) |
199 | 0 | r->i_x = margin_h + outerright_h; |
200 | |
|
201 | 0 | if (r->i_align & SUBPICTURE_ALIGN_TOP ) |
202 | 0 | r->i_y = margin_v + fmt_dst->i_y_offset; |
203 | 0 | else if (r->i_align & SUBPICTURE_ALIGN_BOTTOM ) |
204 | 0 | r->i_y = margin_v + outerbottom_v; |
205 | |
|
206 | 0 | if( p_updtregion->flags & UPDT_REGION_ORIGIN_X_IS_RATIO ) |
207 | 0 | r->i_x += p_updtregion->origin.x * inner_w; |
208 | 0 | else |
209 | 0 | r->i_x += p_updtregion->origin.x; |
210 | |
|
211 | 0 | if( p_updtregion->flags & UPDT_REGION_ORIGIN_Y_IS_RATIO ) |
212 | 0 | r->i_y += p_updtregion->origin.y * inner_h; |
213 | 0 | else |
214 | 0 | r->i_y += p_updtregion->origin.y; |
215 | |
|
216 | 0 | if( p_updtregion->flags & UPDT_REGION_EXTENT_X_IS_RATIO ) |
217 | 0 | r->i_max_width += p_updtregion->extent.x * inner_w; |
218 | 0 | else |
219 | 0 | r->i_max_width += p_updtregion->extent.x; |
220 | |
|
221 | 0 | if( p_updtregion->flags & UPDT_REGION_EXTENT_Y_IS_RATIO ) |
222 | 0 | r->i_max_height += p_updtregion->extent.y * inner_h; |
223 | 0 | else |
224 | 0 | r->i_max_height += p_updtregion->extent.y; |
225 | |
|
226 | 0 | } else { |
227 | | /* FIXME it doesn't adapt on crop settings changes */ |
228 | 0 | r->i_x = p_updtregion->origin.x * fmt_dst->i_width / p_updtregion->extent.x; |
229 | 0 | r->i_y = p_updtregion->origin.y * fmt_dst->i_height / p_updtregion->extent.y; |
230 | 0 | } |
231 | | |
232 | | /* Add missing default style, if any, to all segments */ |
233 | 0 | for ( text_segment_t* p_segment = r->p_text; p_segment; p_segment = p_segment->p_next ) |
234 | 0 | { |
235 | | /* Add decoder defaults */ |
236 | 0 | if( p_segment->style ) |
237 | 0 | text_style_Merge( p_segment->style, sys->p_default_style, false ); |
238 | 0 | else |
239 | 0 | p_segment->style = text_style_Duplicate( sys->p_default_style ); |
240 | |
|
241 | 0 | if( p_segment->style ) |
242 | 0 | { |
243 | | /* Update all segments font sizes in video source %, |
244 | | * so we can handle HiDPI properly and have consistent rendering limits */ |
245 | 0 | if( p_segment->style->i_font_size > 0 && fmt_src->i_visible_height > 0 ) |
246 | 0 | { |
247 | 0 | p_segment->style->f_font_relsize = 100.0 * p_segment->style->i_font_size / fmt_src->i_visible_height; |
248 | 0 | p_segment->style->i_font_size = 0; |
249 | 0 | } |
250 | |
|
251 | 0 | if( p_segment->style->i_style_flags & (STYLE_BLINK_BACKGROUND|STYLE_BLINK_FOREGROUND) ) |
252 | 0 | { |
253 | 0 | if( sys->b_blink_even ) /* do nothing at first */ |
254 | 0 | { |
255 | 0 | if( p_segment->style->i_style_flags & STYLE_BLINK_BACKGROUND ) |
256 | 0 | p_segment->style->i_background_alpha = |
257 | 0 | (~p_segment->style->i_background_alpha) & 0xFF; |
258 | 0 | if( p_segment->style->i_style_flags & STYLE_BLINK_FOREGROUND ) |
259 | 0 | p_segment->style->i_font_alpha = |
260 | 0 | (~p_segment->style->i_font_alpha) & 0xFF; |
261 | 0 | } |
262 | 0 | b_schedule_blink_update = true; |
263 | 0 | } |
264 | 0 | } |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | 0 | if( b_schedule_blink_update && |
269 | 0 | (sys->i_next_update == VLC_TICK_INVALID || sys->i_next_update < ts) ) |
270 | 0 | { |
271 | 0 | sys->i_next_update = ts + VLC_TICK_FROM_SEC(1); |
272 | 0 | sys->b_blink_even = !sys->b_blink_even; |
273 | 0 | } |
274 | 0 | } Unexecuted instantiation: substtml.c:SubpictureTextUpdate Unexecuted instantiation: subsvtt.c:SubpictureTextUpdate |
275 | | static void SubpictureTextDestroy(subpicture_t *subpic) |
276 | 0 | { |
277 | 0 | subtext_updater_sys_t *sys = subpic->updater.p_sys; |
278 | |
|
279 | 0 | SubpictureUpdaterSysRegionClean( &sys->region ); |
280 | 0 | substext_updater_region_t *p_region = sys->region.p_next; |
281 | 0 | while( p_region ) |
282 | 0 | { |
283 | 0 | substext_updater_region_t *p_next = p_region->p_next; |
284 | 0 | SubpictureUpdaterSysRegionClean( p_region ); |
285 | 0 | free( p_region ); |
286 | 0 | p_region = p_next; |
287 | 0 | } |
288 | 0 | text_style_Delete( sys->p_default_style ); |
289 | 0 | free(sys); |
290 | 0 | } Unexecuted instantiation: substtml.c:SubpictureTextDestroy Unexecuted instantiation: subsvtt.c:SubpictureTextDestroy |
291 | | |
292 | | static inline subpicture_t *decoder_NewSubpictureText(decoder_t *decoder) |
293 | 0 | { |
294 | 0 | subtext_updater_sys_t *sys = calloc(1, sizeof(*sys)); |
295 | 0 | subpicture_updater_t updater = { |
296 | 0 | .pf_validate = SubpictureTextValidate, |
297 | 0 | .pf_update = SubpictureTextUpdate, |
298 | 0 | .pf_destroy = SubpictureTextDestroy, |
299 | 0 | .p_sys = sys, |
300 | 0 | }; |
301 | 0 | SubpictureUpdaterSysRegionInit( &sys->region ); |
302 | 0 | sys->margin_ratio = 0.04f; |
303 | 0 | sys->p_default_style = text_style_Create( STYLE_NO_DEFAULTS ); |
304 | 0 | if(unlikely(!sys->p_default_style)) |
305 | 0 | { |
306 | 0 | free(sys); |
307 | 0 | return NULL; |
308 | 0 | } |
309 | 0 | subpicture_t *subpic = decoder_NewSubpicture(decoder, &updater); |
310 | 0 | if (!subpic) |
311 | 0 | { |
312 | 0 | text_style_Delete(sys->p_default_style); |
313 | 0 | free(sys); |
314 | 0 | } |
315 | 0 | return subpic; |
316 | 0 | } Unexecuted instantiation: substtml.c:decoder_NewSubpictureText Unexecuted instantiation: subsvtt.c:decoder_NewSubpictureText |