demux: heif: send extradata with avif
[vlc.git] / modules / codec / substext.h
blob50ca70a907d6596b0ec568398ace93caba26dcad
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
43 struct
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;
57 typedef struct
59 /* a min of one region */
60 substext_updater_region_t region;
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;
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));
85 if(p_region)
86 SubpictureUpdaterSysRegionInit(p_region);
87 return 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);
95 *pp_next = p_new;
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)
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))
108 return VLC_SUCCESS;
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);
126 return VLC_EGENERIC;
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)
134 subtext_updater_sys_t *sys = subpic->updater.p_sys;
135 VLC_UNUSED(fmt_src);
137 if (fmt_dst->i_sar_num <= 0 || fmt_dst->i_sar_den <= 0)
138 return;
140 video_format_t fmt;
141 video_format_Init(&fmt, VLC_CODEC_TEXT);
143 if( sys->region.flags & UPDT_REGION_USES_GRID_COORDINATES )
145 fmt.i_sar_num = 4;
146 fmt.i_sar_den = 3;
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;
150 else
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;
154 fmt.i_sar_num = 1;
155 fmt.i_sar_den = 1;
158 bool b_schedule_blink_update = false;
159 subpicture_region_t **pp_last_region = &subpic->p_region;
161 for( substext_updater_region_t *p_updtregion = &sys->region;
162 p_updtregion; p_updtregion = p_updtregion->p_next )
164 subpicture_region_t *r = *pp_last_region = subpicture_region_New(&fmt);
165 if (!r)
166 return;
167 pp_last_region = &r->p_next;
169 r->p_text = text_segment_Copy( p_updtregion->p_segments );
170 r->i_align = p_updtregion->align;
171 r->i_text_align = p_updtregion->inner_align;
172 r->b_noregionbg = p_updtregion->flags & UPDT_REGION_IGNORE_BACKGROUND;
173 r->b_gridmode = p_updtregion->flags & UPDT_REGION_USES_GRID_COORDINATES;
175 if (!(p_updtregion->flags & UPDT_REGION_FIXED_DONE))
177 const float margin_ratio = sys->margin_ratio;
178 const int margin_h = margin_ratio * (( r->b_gridmode ) ? (unsigned) subpic->i_original_picture_width
179 : fmt_dst->i_visible_width );
180 const int margin_v = margin_ratio * fmt_dst->i_visible_height;
182 /* subpic invisible margins sizes */
183 const int outerright_h = fmt_dst->i_width - (fmt_dst->i_visible_width + fmt_dst->i_x_offset);
184 const int outerbottom_v = fmt_dst->i_height - (fmt_dst->i_visible_height + fmt_dst->i_y_offset);
185 /* regions usable */
186 const int inner_w = fmt_dst->i_visible_width - margin_h * 2;
187 const int inner_h = fmt_dst->i_visible_height - margin_v * 2;
189 if (r->i_align & SUBPICTURE_ALIGN_LEFT)
190 r->i_x = margin_h + fmt_dst->i_x_offset;
191 else if (r->i_align & SUBPICTURE_ALIGN_RIGHT)
192 r->i_x = margin_h + outerright_h;
194 if (r->i_align & SUBPICTURE_ALIGN_TOP )
195 r->i_y = margin_v + fmt_dst->i_y_offset;
196 else if (r->i_align & SUBPICTURE_ALIGN_BOTTOM )
197 r->i_y = margin_v + outerbottom_v;
199 if( p_updtregion->flags & UPDT_REGION_ORIGIN_X_IS_RATIO )
200 r->i_x += p_updtregion->origin.x * inner_w;
201 else
202 r->i_x += p_updtregion->origin.x;
204 if( p_updtregion->flags & UPDT_REGION_ORIGIN_Y_IS_RATIO )
205 r->i_y += p_updtregion->origin.y * inner_h;
206 else
207 r->i_y += p_updtregion->origin.y;
209 if( p_updtregion->flags & UPDT_REGION_EXTENT_X_IS_RATIO )
210 r->i_max_width += p_updtregion->extent.x * inner_w;
211 else
212 r->i_max_width += p_updtregion->extent.x;
214 if( p_updtregion->flags & UPDT_REGION_EXTENT_Y_IS_RATIO )
215 r->i_max_height += p_updtregion->extent.y * inner_h;
216 else
217 r->i_max_height += p_updtregion->extent.y;
219 } else {
220 /* FIXME it doesn't adapt on crop settings changes */
221 r->i_x = p_updtregion->origin.x * fmt_dst->i_width / p_updtregion->extent.x;
222 r->i_y = p_updtregion->origin.y * fmt_dst->i_height / p_updtregion->extent.y;
225 /* Add missing default style, if any, to all segments */
226 for ( text_segment_t* p_segment = r->p_text; p_segment; p_segment = p_segment->p_next )
228 /* Add decoder defaults */
229 if( p_segment->style )
230 text_style_Merge( p_segment->style, sys->p_default_style, false );
231 else
232 p_segment->style = text_style_Duplicate( sys->p_default_style );
234 if( p_segment->style )
236 /* Update all segments font sizes in video source %,
237 * so we can handle HiDPI properly and have consistent rendering limits */
238 if( p_segment->style->i_font_size > 0 && fmt_src->i_visible_height > 0 )
240 p_segment->style->f_font_relsize = 100.0 * p_segment->style->i_font_size / fmt_src->i_visible_height;
241 p_segment->style->i_font_size = 0;
244 if( p_segment->style->i_style_flags & (STYLE_BLINK_BACKGROUND|STYLE_BLINK_FOREGROUND) )
246 if( sys->b_blink_even ) /* do nothing at first */
248 if( p_segment->style->i_style_flags & STYLE_BLINK_BACKGROUND )
249 p_segment->style->i_background_alpha =
250 (~p_segment->style->i_background_alpha) & 0xFF;
251 if( p_segment->style->i_style_flags & STYLE_BLINK_FOREGROUND )
252 p_segment->style->i_font_alpha =
253 (~p_segment->style->i_font_alpha) & 0xFF;
255 b_schedule_blink_update = true;
261 if( b_schedule_blink_update &&
262 (sys->i_next_update == VLC_TICK_INVALID || sys->i_next_update < ts) )
264 sys->i_next_update = ts + VLC_TICK_FROM_SEC(1);
265 sys->b_blink_even = !sys->b_blink_even;
268 static void SubpictureTextDestroy(subpicture_t *subpic)
270 subtext_updater_sys_t *sys = subpic->updater.p_sys;
272 SubpictureUpdaterSysRegionClean( &sys->region );
273 substext_updater_region_t *p_region = sys->region.p_next;
274 while( p_region )
276 substext_updater_region_t *p_next = p_region->p_next;
277 SubpictureUpdaterSysRegionClean( p_region );
278 free( p_region );
279 p_region = p_next;
281 text_style_Delete( sys->p_default_style );
282 free(sys);
285 static inline subpicture_t *decoder_NewSubpictureText(decoder_t *decoder)
287 subtext_updater_sys_t *sys = calloc(1, sizeof(*sys));
288 subpicture_updater_t updater = {
289 .pf_validate = SubpictureTextValidate,
290 .pf_update = SubpictureTextUpdate,
291 .pf_destroy = SubpictureTextDestroy,
292 .p_sys = sys,
294 SubpictureUpdaterSysRegionInit( &sys->region );
295 sys->margin_ratio = 0.04;
296 sys->p_default_style = text_style_Create( STYLE_NO_DEFAULTS );
297 if(unlikely(!sys->p_default_style))
299 free(sys);
300 return NULL;
302 subpicture_t *subpic = decoder_NewSubpicture(decoder, &updater);
303 if (!subpic)
305 text_style_Delete(sys->p_default_style);
306 free(sys);
308 return subpic;