1 /*****************************************************************************
2 * subsdec.c : text subtitle decoder
3 *****************************************************************************
4 * Copyright © 2011-2015 VLC authors and VideoLAN
6 * Authors: Laurent Aimer <fenrir@videolan.org>
7 * Jean-Baptiste Kempf <jb@videolan.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 #include <vlc_strings.h>
25 #include <vlc_text_style.h>
26 #include <vlc_subpicture.h>
28 typedef struct substext_updater_region_t substext_updater_region_t
;
30 enum substext_updater_region_flags_e
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,
41 struct substext_updater_region_t
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
;
59 /* a min of one region */
60 substext_updater_region_t region
;
63 text_style_t
*p_default_style
; /* decoder (full or partial) defaults */
65 vlc_tick_t i_next_update
;
67 } subtext_updater_sys_t
;
69 static inline void SubpictureUpdaterSysRegionClean(substext_updater_region_t
*p_updtregion
)
71 text_segment_ChainDelete( p_updtregion
->p_segments
);
72 text_style_Delete( p_updtregion
->p_region_style
);
75 static inline void SubpictureUpdaterSysRegionInit(substext_updater_region_t
*p_updtregion
)
77 memset(p_updtregion
, 0, sizeof(*p_updtregion
));
78 p_updtregion
->align
= SUBPICTURE_ALIGN_BOTTOM
;
79 p_updtregion
->inner_align
= 0;
82 static inline substext_updater_region_t
*SubpictureUpdaterSysRegionNew( )
84 substext_updater_region_t
*p_region
= malloc(sizeof(*p_region
));
86 SubpictureUpdaterSysRegionInit(p_region
);
90 static inline void SubpictureUpdaterSysRegionAdd(substext_updater_region_t
*p_prev
,
91 substext_updater_region_t
*p_new
)
93 substext_updater_region_t
**pp_next
= &p_prev
->p_next
;
94 for(; *pp_next
; pp_next
= &(*pp_next
)->p_next
);
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
,
103 subtext_updater_sys_t
*sys
= subpic
->updater
.p_sys
;
104 VLC_UNUSED(fmt_src
); VLC_UNUSED(fmt_dst
);
106 if (!has_src_changed
&& !has_dst_changed
&&
107 (sys
->i_next_update
== VLC_TICK_INVALID
|| sys
->i_next_update
> ts
))
110 substext_updater_region_t
*p_updtregion
= &sys
->region
;
112 if (!(p_updtregion
->flags
& UPDT_REGION_FIXED_DONE
) &&
113 subpic
->b_absolute
&& subpic
->p_region
&&
114 subpic
->i_original_picture_width
> 0 &&
115 subpic
->i_original_picture_height
> 0)
117 p_updtregion
->flags
|= UPDT_REGION_FIXED_DONE
;
118 p_updtregion
->origin
.x
= subpic
->p_region
->i_x
;
119 p_updtregion
->origin
.y
= subpic
->p_region
->i_y
;
120 p_updtregion
->extent
.x
= subpic
->i_original_picture_width
;
121 p_updtregion
->extent
.y
= subpic
->i_original_picture_height
;
122 p_updtregion
->flags
&= ~(UPDT_REGION_ORIGIN_X_IS_RATIO
|UPDT_REGION_ORIGIN_Y_IS_RATIO
|
123 UPDT_REGION_EXTENT_X_IS_RATIO
|UPDT_REGION_EXTENT_Y_IS_RATIO
);
129 static void SubpictureTextUpdate(subpicture_t
*subpic
,
130 const video_format_t
*fmt_src
,
131 const video_format_t
*fmt_dst
,
134 subtext_updater_sys_t
*sys
= subpic
->updater
.p_sys
;
137 if (fmt_dst
->i_sar_num
<= 0 || fmt_dst
->i_sar_den
<= 0)
141 video_format_Init(&fmt
, VLC_CODEC_TEXT
);
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 */
150 if( sys
->region
.flags
& UPDT_REGION_USES_GRID_COORDINATES
)
154 subpic
->i_original_picture_width
= fmt_dst
->i_visible_height
* fmt
.i_sar_num
/ fmt
.i_sar_den
;
155 subpic
->i_original_picture_height
= fmt_dst
->i_visible_height
;
159 subpic
->i_original_picture_width
= fmt_dst
->i_width
* fmt_dst
->i_sar_num
/ fmt_dst
->i_sar_den
;
160 subpic
->i_original_picture_height
= fmt_dst
->i_height
;
165 bool b_schedule_blink_update
= false;
166 subpicture_region_t
**pp_last_region
= &subpic
->p_region
;
168 for( substext_updater_region_t
*p_updtregion
= &sys
->region
;
169 p_updtregion
; p_updtregion
= p_updtregion
->p_next
)
171 subpicture_region_t
*r
= *pp_last_region
= subpicture_region_New(&fmt
);
174 pp_last_region
= &r
->p_next
;
176 r
->p_text
= text_segment_Copy( p_updtregion
->p_segments
);
177 r
->i_align
= p_updtregion
->align
;
178 r
->i_text_align
= p_updtregion
->inner_align
;
179 r
->b_noregionbg
= p_updtregion
->flags
& UPDT_REGION_IGNORE_BACKGROUND
;
180 r
->b_gridmode
= p_updtregion
->flags
& UPDT_REGION_USES_GRID_COORDINATES
;
182 if (!(p_updtregion
->flags
& UPDT_REGION_FIXED_DONE
))
184 const float margin_ratio
= sys
->margin_ratio
;
185 const int margin_h
= margin_ratio
* (( r
->b_gridmode
) ? (unsigned) subpic
->i_original_picture_width
186 : fmt_dst
->i_visible_width
);
187 const int margin_v
= margin_ratio
* fmt_dst
->i_visible_height
;
189 /* subpic invisible margins sizes */
190 const int outerright_h
= fmt_dst
->i_width
- (fmt_dst
->i_visible_width
+ fmt_dst
->i_x_offset
);
191 const int outerbottom_v
= fmt_dst
->i_height
- (fmt_dst
->i_visible_height
+ fmt_dst
->i_y_offset
);
193 const int inner_w
= fmt_dst
->i_visible_width
- margin_h
* 2;
194 const int inner_h
= fmt_dst
->i_visible_height
- margin_v
* 2;
196 if (r
->i_align
& SUBPICTURE_ALIGN_LEFT
)
197 r
->i_x
= margin_h
+ fmt_dst
->i_x_offset
;
198 else if (r
->i_align
& SUBPICTURE_ALIGN_RIGHT
)
199 r
->i_x
= margin_h
+ outerright_h
;
201 if (r
->i_align
& SUBPICTURE_ALIGN_TOP
)
202 r
->i_y
= margin_v
+ fmt_dst
->i_y_offset
;
203 else if (r
->i_align
& SUBPICTURE_ALIGN_BOTTOM
)
204 r
->i_y
= margin_v
+ outerbottom_v
;
206 if( p_updtregion
->flags
& UPDT_REGION_ORIGIN_X_IS_RATIO
)
207 r
->i_x
+= p_updtregion
->origin
.x
* inner_w
;
209 r
->i_x
+= p_updtregion
->origin
.x
;
211 if( p_updtregion
->flags
& UPDT_REGION_ORIGIN_Y_IS_RATIO
)
212 r
->i_y
+= p_updtregion
->origin
.y
* inner_h
;
214 r
->i_y
+= p_updtregion
->origin
.y
;
216 if( p_updtregion
->flags
& UPDT_REGION_EXTENT_X_IS_RATIO
)
217 r
->i_max_width
+= p_updtregion
->extent
.x
* inner_w
;
219 r
->i_max_width
+= p_updtregion
->extent
.x
;
221 if( p_updtregion
->flags
& UPDT_REGION_EXTENT_Y_IS_RATIO
)
222 r
->i_max_height
+= p_updtregion
->extent
.y
* inner_h
;
224 r
->i_max_height
+= p_updtregion
->extent
.y
;
227 /* FIXME it doesn't adapt on crop settings changes */
228 r
->i_x
= p_updtregion
->origin
.x
* fmt_dst
->i_width
/ p_updtregion
->extent
.x
;
229 r
->i_y
= p_updtregion
->origin
.y
* fmt_dst
->i_height
/ p_updtregion
->extent
.y
;
232 /* Add missing default style, if any, to all segments */
233 for ( text_segment_t
* p_segment
= r
->p_text
; p_segment
; p_segment
= p_segment
->p_next
)
235 /* Add decoder defaults */
236 if( p_segment
->style
)
237 text_style_Merge( p_segment
->style
, sys
->p_default_style
, false );
239 p_segment
->style
= text_style_Duplicate( sys
->p_default_style
);
241 if( p_segment
->style
)
243 /* Update all segments font sizes in video source %,
244 * so we can handle HiDPI properly and have consistent rendering limits */
245 if( p_segment
->style
->i_font_size
> 0 && fmt_src
->i_visible_height
> 0 )
247 p_segment
->style
->f_font_relsize
= 100.0 * p_segment
->style
->i_font_size
/ fmt_src
->i_visible_height
;
248 p_segment
->style
->i_font_size
= 0;
251 if( p_segment
->style
->i_style_flags
& (STYLE_BLINK_BACKGROUND
|STYLE_BLINK_FOREGROUND
) )
253 if( sys
->b_blink_even
) /* do nothing at first */
255 if( p_segment
->style
->i_style_flags
& STYLE_BLINK_BACKGROUND
)
256 p_segment
->style
->i_background_alpha
=
257 (~p_segment
->style
->i_background_alpha
) & 0xFF;
258 if( p_segment
->style
->i_style_flags
& STYLE_BLINK_FOREGROUND
)
259 p_segment
->style
->i_font_alpha
=
260 (~p_segment
->style
->i_font_alpha
) & 0xFF;
262 b_schedule_blink_update
= true;
268 if( b_schedule_blink_update
&&
269 (sys
->i_next_update
== VLC_TICK_INVALID
|| sys
->i_next_update
< ts
) )
271 sys
->i_next_update
= ts
+ VLC_TICK_FROM_SEC(1);
272 sys
->b_blink_even
= !sys
->b_blink_even
;
275 static void SubpictureTextDestroy(subpicture_t
*subpic
)
277 subtext_updater_sys_t
*sys
= subpic
->updater
.p_sys
;
279 SubpictureUpdaterSysRegionClean( &sys
->region
);
280 substext_updater_region_t
*p_region
= sys
->region
.p_next
;
283 substext_updater_region_t
*p_next
= p_region
->p_next
;
284 SubpictureUpdaterSysRegionClean( p_region
);
288 text_style_Delete( sys
->p_default_style
);
292 static inline subpicture_t
*decoder_NewSubpictureText(decoder_t
*decoder
)
294 subtext_updater_sys_t
*sys
= calloc(1, sizeof(*sys
));
295 subpicture_updater_t updater
= {
296 .pf_validate
= SubpictureTextValidate
,
297 .pf_update
= SubpictureTextUpdate
,
298 .pf_destroy
= SubpictureTextDestroy
,
301 SubpictureUpdaterSysRegionInit( &sys
->region
);
302 sys
->margin_ratio
= 0.04;
303 sys
->p_default_style
= text_style_Create( STYLE_NO_DEFAULTS
);
304 if(unlikely(!sys
->p_default_style
))
309 subpicture_t
*subpic
= decoder_NewSubpicture(decoder
, &updater
);
312 text_style_Delete(sys
->p_default_style
);