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 subpicture_updater_sys_region_t subpicture_updater_sys_region_t
;
30 enum subpicture_updater_sys_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 subpicture_updater_sys_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 subpicture_updater_sys_region_t
*p_next
;
57 struct subpicture_updater_sys_t
{
59 /* a min of one region */
60 subpicture_updater_sys_region_t region
;
63 text_style_t
*p_default_style
; /* decoder (full or partial) defaults */
65 mtime_t i_next_update
;
69 static inline void SubpictureUpdaterSysRegionClean(subpicture_updater_sys_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(subpicture_updater_sys_region_t
*p_updtregion
)
77 memset(p_updtregion
, 0, sizeof(*p_updtregion
));
80 static inline subpicture_updater_sys_region_t
*SubpictureUpdaterSysRegionNew( )
82 subpicture_updater_sys_region_t
*p_region
= malloc(sizeof(*p_region
));
84 SubpictureUpdaterSysRegionInit(p_region
);
88 static inline void SubpictureUpdaterSysRegionAdd(subpicture_updater_sys_region_t
*p_prev
,
89 subpicture_updater_sys_region_t
*p_new
)
91 subpicture_updater_sys_region_t
**pp_next
= &p_prev
->p_next
;
92 for(; *pp_next
; pp_next
= &(*pp_next
)->p_next
);
96 static int SubpictureTextValidate(subpicture_t
*subpic
,
97 bool has_src_changed
, const video_format_t
*fmt_src
,
98 bool has_dst_changed
, const video_format_t
*fmt_dst
,
101 subpicture_updater_sys_t
*sys
= subpic
->updater
.p_sys
;
102 VLC_UNUSED(fmt_src
); VLC_UNUSED(fmt_dst
);
104 if (!has_src_changed
&& !has_dst_changed
&&
105 (sys
->i_next_update
== VLC_TS_INVALID
|| sys
->i_next_update
> ts
))
108 subpicture_updater_sys_region_t
*p_updtregion
= &sys
->region
;
110 if (!(p_updtregion
->flags
& UPDT_REGION_FIXED_DONE
) &&
111 subpic
->b_absolute
&& subpic
->p_region
&&
112 subpic
->i_original_picture_width
> 0 &&
113 subpic
->i_original_picture_height
> 0)
115 p_updtregion
->flags
|= UPDT_REGION_FIXED_DONE
;
116 p_updtregion
->origin
.x
= subpic
->p_region
->i_x
;
117 p_updtregion
->origin
.y
= subpic
->p_region
->i_y
;
118 p_updtregion
->extent
.x
= subpic
->i_original_picture_width
;
119 p_updtregion
->extent
.y
= subpic
->i_original_picture_height
;
120 p_updtregion
->flags
&= ~(UPDT_REGION_ORIGIN_X_IS_RATIO
|UPDT_REGION_ORIGIN_Y_IS_RATIO
|
121 UPDT_REGION_EXTENT_X_IS_RATIO
|UPDT_REGION_EXTENT_Y_IS_RATIO
);
127 static void SubpictureTextUpdate(subpicture_t
*subpic
,
128 const video_format_t
*fmt_src
,
129 const video_format_t
*fmt_dst
,
132 subpicture_updater_sys_t
*sys
= subpic
->updater
.p_sys
;
135 if (fmt_dst
->i_sar_num
<= 0 || fmt_dst
->i_sar_den
<= 0)
139 video_format_Init(&fmt
, VLC_CODEC_TEXT
);
141 if( sys
->region
.flags
& UPDT_REGION_USES_GRID_COORDINATES
)
145 subpic
->i_original_picture_width
= fmt_dst
->i_visible_height
* fmt
.i_sar_num
/ fmt
.i_sar_den
;
146 subpic
->i_original_picture_height
= fmt_dst
->i_visible_height
;
150 subpic
->i_original_picture_width
= fmt_dst
->i_width
* fmt_dst
->i_sar_num
/ fmt_dst
->i_sar_den
;
151 subpic
->i_original_picture_height
= fmt_dst
->i_height
;
156 bool b_schedule_blink_update
= false;
157 subpicture_region_t
**pp_last_region
= &subpic
->p_region
;
159 for( subpicture_updater_sys_region_t
*p_updtregion
= &sys
->region
;
160 p_updtregion
; p_updtregion
= p_updtregion
->p_next
)
162 subpicture_region_t
*r
= *pp_last_region
= subpicture_region_New(&fmt
);
165 pp_last_region
= &r
->p_next
;
167 r
->p_text
= text_segment_Copy( p_updtregion
->p_segments
);
168 r
->i_align
= p_updtregion
->inner_align
| p_updtregion
->align
; /* we do not support text align by itself */
169 r
->b_noregionbg
= p_updtregion
->flags
& UPDT_REGION_IGNORE_BACKGROUND
;
170 r
->b_gridmode
= p_updtregion
->flags
& UPDT_REGION_USES_GRID_COORDINATES
;
172 if (!(p_updtregion
->flags
& UPDT_REGION_FIXED_DONE
))
174 const float margin_ratio
= sys
->margin_ratio
;
175 const int margin_h
= margin_ratio
* (( r
->b_gridmode
) ? (unsigned) subpic
->i_original_picture_width
176 : fmt_dst
->i_visible_width
);
177 const int margin_v
= margin_ratio
* fmt_dst
->i_visible_height
;
179 /* subpic invisible margins sizes */
180 const int outerright_h
= fmt_dst
->i_width
- (fmt_dst
->i_visible_width
+ fmt_dst
->i_x_offset
);
181 const int outerbottom_v
= fmt_dst
->i_height
- (fmt_dst
->i_visible_height
+ fmt_dst
->i_y_offset
);
183 const int inner_w
= fmt_dst
->i_visible_width
- margin_h
* 2;
184 const int inner_h
= fmt_dst
->i_visible_height
- margin_v
* 2;
186 if (r
->i_align
& SUBPICTURE_ALIGN_LEFT
)
187 r
->i_x
= margin_h
+ fmt_dst
->i_x_offset
;
188 else if (r
->i_align
& SUBPICTURE_ALIGN_RIGHT
)
189 r
->i_x
= margin_h
+ outerright_h
;
191 if (r
->i_align
& SUBPICTURE_ALIGN_TOP
)
192 r
->i_y
= margin_v
+ fmt_dst
->i_y_offset
;
193 else if (r
->i_align
& SUBPICTURE_ALIGN_BOTTOM
)
194 r
->i_y
= margin_v
+ outerbottom_v
;
196 if( p_updtregion
->flags
& UPDT_REGION_ORIGIN_X_IS_RATIO
)
197 r
->i_x
+= p_updtregion
->origin
.x
* inner_w
;
199 r
->i_x
+= p_updtregion
->origin
.x
;
201 if( p_updtregion
->flags
& UPDT_REGION_ORIGIN_Y_IS_RATIO
)
202 r
->i_y
+= p_updtregion
->origin
.y
* inner_h
;
204 r
->i_y
+= p_updtregion
->origin
.y
;
207 /* FIXME it doesn't adapt on crop settings changes */
208 r
->i_x
= p_updtregion
->origin
.x
* fmt_dst
->i_width
/ p_updtregion
->extent
.x
;
209 r
->i_y
= p_updtregion
->origin
.y
* fmt_dst
->i_height
/ p_updtregion
->extent
.y
;
212 /* Add missing default style, if any, to all segments */
213 for ( text_segment_t
* p_segment
= r
->p_text
; p_segment
; p_segment
= p_segment
->p_next
)
215 /* Add decoder defaults */
216 if( p_segment
->style
)
217 text_style_Merge( p_segment
->style
, sys
->p_default_style
, false );
219 p_segment
->style
= text_style_Duplicate( sys
->p_default_style
);
221 if( p_segment
->style
)
223 /* Update all segments font sizes in pixels, *** metric used by renderers *** */
224 /* We only do this when a fixed font size isn't set */
225 if( p_segment
->style
&& p_segment
->style
->f_font_relsize
&& !p_segment
->style
->i_font_size
)
227 p_segment
->style
->i_font_size
= p_segment
->style
->f_font_relsize
*
228 subpic
->i_original_picture_height
/ 100;
231 if( p_segment
->style
->i_style_flags
& (STYLE_BLINK_BACKGROUND
|STYLE_BLINK_FOREGROUND
) )
233 if( sys
->b_blink_even
) /* do nothing at first */
235 if( p_segment
->style
->i_style_flags
& STYLE_BLINK_BACKGROUND
)
236 p_segment
->style
->i_background_alpha
=
237 (~p_segment
->style
->i_background_alpha
) & 0xFF;
238 if( p_segment
->style
->i_style_flags
& STYLE_BLINK_FOREGROUND
)
239 p_segment
->style
->i_font_alpha
=
240 (~p_segment
->style
->i_font_alpha
) & 0xFF;
242 b_schedule_blink_update
= true;
248 if( b_schedule_blink_update
&&
249 (sys
->i_next_update
== VLC_TS_INVALID
|| sys
->i_next_update
< ts
) )
251 sys
->i_next_update
= ts
+ CLOCK_FREQ
;
252 sys
->b_blink_even
= !sys
->b_blink_even
;
255 static void SubpictureTextDestroy(subpicture_t
*subpic
)
257 subpicture_updater_sys_t
*sys
= subpic
->updater
.p_sys
;
259 SubpictureUpdaterSysRegionClean( &sys
->region
);
260 subpicture_updater_sys_region_t
*p_region
= sys
->region
.p_next
;
263 subpicture_updater_sys_region_t
*p_next
= p_region
->p_next
;
264 SubpictureUpdaterSysRegionClean( p_region
);
268 text_style_Delete( sys
->p_default_style
);
272 static inline subpicture_t
*decoder_NewSubpictureText(decoder_t
*decoder
)
274 subpicture_updater_sys_t
*sys
= calloc(1, sizeof(*sys
));
275 subpicture_updater_t updater
= {
276 .pf_validate
= SubpictureTextValidate
,
277 .pf_update
= SubpictureTextUpdate
,
278 .pf_destroy
= SubpictureTextDestroy
,
281 SubpictureUpdaterSysRegionInit( &sys
->region
);
282 sys
->margin_ratio
= 0.04;
283 sys
->p_default_style
= text_style_Create( STYLE_NO_DEFAULTS
);
284 if(unlikely(!sys
->p_default_style
))
289 subpicture_t
*subpic
= decoder_NewSubpicture(decoder
, &updater
);
292 text_style_Delete(sys
->p_default_style
);