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>
27 typedef struct subpicture_updater_sys_region_t subpicture_updater_sys_region_t
;
29 enum subpicture_updater_sys_region_flags_e
31 UPDT_REGION_ORIGIN_X_IS_PERCENTILE
= 1 << 0,
32 UPDT_REGION_ORIGIN_Y_IS_PERCENTILE
= 1 << 1,
33 UPDT_REGION_EXTENT_X_IS_PERCENTILE
= 1 << 2,
34 UPDT_REGION_EXTENT_Y_IS_PERCENTILE
= 1 << 3,
35 UPDT_REGION_IGNORE_BACKGROUND
= 1 << 4,
36 UPDT_REGION_USES_GRID_COORDINATES
= 1 << 5,
37 UPDT_REGION_FIX_DONE
= 1 << 31,
40 #define EIA608_MARGIN 0.10
41 #define EIA608_VISIBLE 0.80
42 #define EIA608_ROWS 15
44 #define FONT_TO_LINE_HEIGHT_RATIO 1.06
46 struct subpicture_updater_sys_region_t
53 /* store above percentile meanings as modifier flags */
54 int flags
; /* subpicture_updater_sys_region_flags_e */
55 int align
; /* alignment of the region itself */
56 int inner_align
; /* alignment of content inside the region */
57 text_style_t
*p_region_style
;
58 text_segment_t
*p_segments
;
59 subpicture_updater_sys_region_t
*p_next
;
62 struct subpicture_updater_sys_t
{
64 /* a min of one region */
65 subpicture_updater_sys_region_t region
;
68 text_style_t
*p_default_style
; /* decoder (full or partial) defaults */
72 static inline void SubpictureUpdaterSysRegionClean(subpicture_updater_sys_region_t
*p_updtregion
)
74 text_segment_ChainDelete( p_updtregion
->p_segments
);
75 text_style_Delete( p_updtregion
->p_region_style
);
78 static inline void SubpictureUpdaterSysRegionInit(subpicture_updater_sys_region_t
*p_updtregion
)
80 memset(p_updtregion
, 0, sizeof(*p_updtregion
));
83 static inline subpicture_updater_sys_region_t
*SubpictureUpdaterSysRegionNew( )
85 subpicture_updater_sys_region_t
*p_region
= malloc(sizeof(*p_region
));
87 SubpictureUpdaterSysRegionInit(p_region
);
91 static inline void SubpictureUpdaterSysRegionAdd(subpicture_updater_sys_region_t
*p_prev
,
92 subpicture_updater_sys_region_t
*p_new
)
94 subpicture_updater_sys_region_t
**pp_next
= &p_prev
->p_next
;
95 for(; *pp_next
; pp_next
= &(*pp_next
)->p_next
);
99 static int SubpictureTextValidate(subpicture_t
*subpic
,
100 bool has_src_changed
, const video_format_t
*fmt_src
,
101 bool has_dst_changed
, const video_format_t
*fmt_dst
,
104 subpicture_updater_sys_t
*sys
= subpic
->updater
.p_sys
;
105 VLC_UNUSED(fmt_src
); VLC_UNUSED(fmt_dst
); VLC_UNUSED(ts
);
107 if (!has_src_changed
&& !has_dst_changed
)
110 subpicture_updater_sys_region_t
*p_updtregion
= &sys
->region
;
112 if (!(p_updtregion
->flags
& UPDT_REGION_FIX_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_FIX_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_PERCENTILE
|UPDT_REGION_ORIGIN_Y_IS_PERCENTILE
|
123 UPDT_REGION_EXTENT_X_IS_PERCENTILE
|UPDT_REGION_EXTENT_Y_IS_PERCENTILE
);
129 static void SubpictureTextUpdate(subpicture_t
*subpic
,
130 const video_format_t
*fmt_src
,
131 const video_format_t
*fmt_dst
,
134 subpicture_updater_sys_t
*sys
= subpic
->updater
.p_sys
;
135 VLC_UNUSED(fmt_src
); VLC_UNUSED(ts
);
137 if (fmt_dst
->i_sar_num
<= 0 || fmt_dst
->i_sar_den
<= 0)
141 video_format_Init(&fmt
, VLC_CODEC_TEXT
);
143 if( sys
->region
.flags
& UPDT_REGION_USES_GRID_COORDINATES
)
147 subpic
->i_original_picture_width
= fmt_dst
->i_visible_height
* fmt
.i_sar_num
/ fmt
.i_sar_den
;
148 subpic
->i_original_picture_height
= fmt_dst
->i_visible_height
;
152 subpic
->i_original_picture_width
= fmt_dst
->i_width
* fmt_dst
->i_sar_num
/ fmt_dst
->i_sar_den
;
153 subpic
->i_original_picture_height
= fmt_dst
->i_height
;
158 subpicture_region_t
**pp_last_region
= &subpic
->p_region
;
160 for( subpicture_updater_sys_region_t
*p_updtregion
= &sys
->region
;
161 p_updtregion
; p_updtregion
= p_updtregion
->p_next
)
163 subpicture_region_t
*r
= *pp_last_region
= subpicture_region_New(&fmt
);
166 pp_last_region
= &r
->p_next
;
168 r
->p_text
= text_segment_Copy( p_updtregion
->p_segments
);
169 r
->i_align
= p_updtregion
->inner_align
| p_updtregion
->align
; /* we do not support text align by itself */
170 r
->b_noregionbg
= p_updtregion
->flags
& UPDT_REGION_IGNORE_BACKGROUND
;
171 r
->b_gridmode
= p_updtregion
->flags
& UPDT_REGION_USES_GRID_COORDINATES
;
175 /* Ensure correct flags are set */
176 r
->i_align
&= ~(SUBPICTURE_ALIGN_RIGHT
| SUBPICTURE_ALIGN_BOTTOM
);
177 r
->i_align
|= (SUBPICTURE_ALIGN_LEFT
| SUBPICTURE_ALIGN_TOP
);
180 if (!(p_updtregion
->flags
& UPDT_REGION_FIX_DONE
))
182 const float margin_ratio
= sys
->margin_ratio
;
183 const int margin_h
= margin_ratio
* (( r
->b_gridmode
) ? (unsigned) subpic
->i_original_picture_width
184 : fmt_dst
->i_visible_width
);
185 const int margin_v
= margin_ratio
* fmt_dst
->i_visible_height
;
187 if (r
->i_align
& SUBPICTURE_ALIGN_LEFT
)
188 r
->i_x
= margin_h
+ fmt_dst
->i_x_offset
;
189 else if (r
->i_align
& SUBPICTURE_ALIGN_RIGHT
)
190 r
->i_x
= margin_h
+ fmt_dst
->i_width
- (fmt_dst
->i_visible_width
+ fmt_dst
->i_x_offset
);
192 if (r
->i_align
& SUBPICTURE_ALIGN_TOP
)
193 r
->i_y
= margin_v
+ fmt_dst
->i_y_offset
;
194 else if (r
->i_align
& SUBPICTURE_ALIGN_BOTTOM
)
195 r
->i_y
= margin_v
+ fmt_dst
->i_height
- (fmt_dst
->i_visible_height
+ fmt_dst
->i_y_offset
);
199 r
->i_y
+= p_updtregion
->origin
.y
* /* row number */
200 (EIA608_VISIBLE
/ EIA608_ROWS
) *
201 (fmt_dst
->i_visible_height
- r
->i_y
) * FONT_TO_LINE_HEIGHT_RATIO
;
205 /* FIXME it doesn't adapt on crop settings changes */
206 r
->i_x
= p_updtregion
->origin
.x
* fmt_dst
->i_width
/ p_updtregion
->extent
.x
;
207 r
->i_y
= p_updtregion
->origin
.y
* fmt_dst
->i_height
/ p_updtregion
->extent
.y
;
210 /* Add missing default style, if any, to all segments */
211 for ( text_segment_t
* p_segment
= r
->p_text
; p_segment
; p_segment
= p_segment
->p_next
)
213 /* Add decoder defaults */
214 if( p_segment
->style
)
215 text_style_Merge( p_segment
->style
, sys
->p_default_style
, false );
217 p_segment
->style
= text_style_Duplicate( sys
->p_default_style
);
218 /* Update all segments font sizes in pixels, *** metric used by renderers *** */
219 /* We only do this when a fixed font size isn't set */
220 if( p_segment
->style
&& p_segment
->style
->f_font_relsize
&& !p_segment
->style
->i_font_size
)
222 p_segment
->style
->i_font_size
= p_segment
->style
->f_font_relsize
*
223 subpic
->i_original_picture_height
/ 100;
229 static void SubpictureTextDestroy(subpicture_t
*subpic
)
231 subpicture_updater_sys_t
*sys
= subpic
->updater
.p_sys
;
233 SubpictureUpdaterSysRegionClean( &sys
->region
);
234 subpicture_updater_sys_region_t
*p_region
= sys
->region
.p_next
;
237 subpicture_updater_sys_region_t
*p_next
= p_region
->p_next
;
238 SubpictureUpdaterSysRegionClean( p_region
);
241 text_style_Delete( sys
->p_default_style
);
245 static inline subpicture_t
*decoder_NewSubpictureText(decoder_t
*decoder
)
247 subpicture_updater_sys_t
*sys
= calloc(1, sizeof(*sys
));
248 subpicture_updater_t updater
= {
249 .pf_validate
= SubpictureTextValidate
,
250 .pf_update
= SubpictureTextUpdate
,
251 .pf_destroy
= SubpictureTextDestroy
,
254 SubpictureUpdaterSysRegionInit( &sys
->region
);
255 sys
->margin_ratio
= 0.04;
256 sys
->p_default_style
= text_style_Create( STYLE_NO_DEFAULTS
);
257 if(unlikely(!sys
->p_default_style
))
262 subpicture_t
*subpic
= decoder_NewSubpicture(decoder
, &updater
);
265 text_style_Delete(sys
->p_default_style
);