1 /*****************************************************************************
2 * substx3gsub.c : MP4 tx3g subtitles decoder
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation, Inc.,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_codec.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static int Open ( vlc_object_t * );
36 static subpicture_t *Decode( decoder_t *, block_t ** );
39 set_description( N_("tx3g subtitles decoder") )
40 set_shortname( N_("tx3g subtitles") )
41 set_capability( "decoder", 100 )
42 set_category( CAT_INPUT )
43 set_subcategory( SUBCAT_INPUT_SCODEC )
44 set_callbacks( Open, NULL )
47 /****************************************************************************
49 ****************************************************************************/
51 /*****************************************************************************
52 * Open: probe the decoder and return score
53 *****************************************************************************/
54 static int Open( vlc_object_t *p_this )
56 decoder_t *p_dec = (decoder_t *) p_this;
58 if( p_dec->fmt_in.i_codec != VLC_CODEC_TX3G )
61 p_dec->pf_decode_sub = Decode;
63 p_dec->fmt_out.i_cat = SPU_ES;
64 p_dec->fmt_out.i_codec = 0;
69 /*****************************************************************************
71 *****************************************************************************/
73 #define FONT_FACE_BOLD 0x1
74 #define FONT_FACE_ITALIC 0x2
75 #define FONT_FACE_UNDERLINE 0x4
77 static int ConvertFlags( int i_atomflags )
79 int i_vlcstyles_flags = 0;
80 if ( i_atomflags & FONT_FACE_BOLD )
81 i_vlcstyles_flags |= STYLE_BOLD;
82 else if ( i_atomflags & FONT_FACE_ITALIC )
83 i_vlcstyles_flags |= STYLE_ITALIC;
84 else if ( i_atomflags & FONT_FACE_UNDERLINE )
85 i_vlcstyles_flags |= STYLE_UNDERLINE;
86 return i_vlcstyles_flags;
89 static void SegmentDoSplit( segment_t *p_segment, uint16_t i_start, uint16_t i_end,
90 segment_t **pp_segment_left,
91 segment_t **pp_segment_middle,
92 segment_t **pp_segment_right )
94 segment_t *p_segment_left = *pp_segment_left;
95 segment_t *p_segment_right = *pp_segment_right;
96 segment_t *p_segment_middle = *pp_segment_middle;
97 p_segment_left = p_segment_middle = p_segment_right = NULL;
99 if ( (p_segment->i_size - i_start < 1) || (p_segment->i_size - i_end < 1) )
104 p_segment_left = calloc( 1, sizeof(segment_t) );
105 if ( !p_segment_left ) goto error;
106 memcpy( &p_segment_left->styles, &p_segment->styles, sizeof(segment_style_t) );
107 p_segment_left->psz_string = strndup( p_segment->psz_string, i_start );
108 p_segment_left->i_size = strlen( p_segment_left->psz_string );
111 p_segment_middle = calloc( 1, sizeof(segment_t) );
112 if ( !p_segment_middle ) goto error;
113 memcpy( &p_segment_middle->styles, &p_segment->styles, sizeof(segment_style_t) );
114 p_segment_middle->psz_string = strndup( p_segment->psz_string + i_start, i_end - i_start + 1 );
115 p_segment_middle->i_size = strlen( p_segment_middle->psz_string );
117 if ( i_end < (p_segment->i_size - 1) )
119 p_segment_right = calloc( 1, sizeof(segment_t) );
120 if ( !p_segment_right ) goto error;
121 memcpy( &p_segment_right->styles, &p_segment->styles, sizeof(segment_style_t) );
122 p_segment_right->psz_string = strndup( p_segment->psz_string + i_end + 1, p_segment->i_size - i_end - 1 );
123 p_segment_right->i_size = strlen( p_segment_right->psz_string );
126 if ( p_segment_left ) p_segment_left->p_next = p_segment_middle;
127 if ( p_segment_right ) p_segment_middle->p_next = p_segment_right;
129 *pp_segment_left = p_segment_left;
130 *pp_segment_middle = p_segment_middle;
131 *pp_segment_right = p_segment_right;
136 SegmentFree( p_segment_left );
137 SegmentFree( p_segment_middle );
138 SegmentFree( p_segment_right );
141 static bool SegmentSplit( segment_t *p_prev, segment_t **pp_segment,
142 const uint16_t i_start, const uint16_t i_end,
143 const segment_style_t *p_styles )
145 segment_t *p_segment_left = NULL, *p_segment_middle = NULL, *p_segment_right = NULL;
147 if ( (*pp_segment)->i_size == 0 ) return false;
148 if ( i_start > i_end ) return false;
149 if ( (size_t)(i_end - i_start) > (*pp_segment)->i_size - 1 ) return false;
150 if ( i_end > (*pp_segment)->i_size - 1 ) return false;
152 SegmentDoSplit( *pp_segment, i_start, i_end, &p_segment_left, &p_segment_middle, &p_segment_right );
153 if ( !p_segment_middle )
156 SegmentFree( p_segment_left );
157 SegmentFree( p_segment_right );
161 segment_t *p_next = (*pp_segment)->p_next;
162 SegmentFree( *pp_segment );
163 *pp_segment = ( p_segment_left ) ? p_segment_left : p_segment_middle ;
164 if ( p_prev ) p_prev->p_next = *pp_segment;
166 if ( p_segment_right )
167 p_segment_right->p_next = p_next;
169 p_segment_middle->p_next = p_next;
171 p_segment_middle->styles = *p_styles;
176 /* Creates a new segment using the given style and split existing ones according
177 to the start & end offsets */
178 static void ApplySegmentStyle( segment_t **pp_segment, const uint16_t i_absstart,
179 const uint16_t i_absend, const segment_style_t *p_styles )
181 /* find the matching segment */
182 uint16_t i_curstart = 0;
183 segment_t *p_prev = NULL;
184 segment_t *p_cur = *pp_segment;
187 uint16_t i_curend = i_curstart + p_cur->i_size - 1;
188 if ( (i_absstart >= i_curstart) && (i_absend <= i_curend) )
191 if ( !SegmentSplit( p_prev, &p_cur, i_absstart - i_curstart,
192 i_absend - i_curstart, p_styles ) )
194 if ( !p_prev ) *pp_segment = p_cur;
199 i_curstart += p_cur->i_size;
201 p_cur = p_cur->p_next;
206 /*****************************************************************************
208 *****************************************************************************/
209 static subpicture_t *Decode( decoder_t *p_dec, block_t **pp_block )
212 subpicture_t *p_spu = NULL;
214 if( ( pp_block == NULL ) || ( *pp_block == NULL ) ) return NULL;
218 if( ( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) ) ||
219 p_block->i_buffer < sizeof(uint16_t) )
221 block_Release( p_block );
225 uint8_t *p_buf = p_block->p_buffer;
227 /* Read our raw string and create the styled segment for HTML */
228 uint16_t i_psz_length = GetWBE( p_buf );
229 char *psz_subtitle = malloc( i_psz_length + 1 );
230 if ( !psz_subtitle ) return NULL;
231 memcpy( psz_subtitle, p_block->p_buffer + sizeof(uint16_t), i_psz_length );
232 psz_subtitle[ i_psz_length ] = '\0';
233 p_buf += i_psz_length + sizeof(uint16_t);
235 for( uint16_t i=0; i < i_psz_length; i++ )
236 if ( psz_subtitle[i] == '\r' ) psz_subtitle[i] = '\n';
238 segment_t *p_segment = calloc( 1, sizeof(segment_t) );
241 free( psz_subtitle );
244 p_segment->psz_string = strdup( psz_subtitle );
245 p_segment->i_size = strlen( psz_subtitle );
246 if ( p_dec->fmt_in.subs.p_style )
248 p_segment->styles.i_color = p_dec->fmt_in.subs.p_style->i_font_color;
249 p_segment->styles.i_color |= p_dec->fmt_in.subs.p_style->i_font_alpha << 24;
250 if ( p_dec->fmt_in.subs.p_style->i_style_flags )
251 p_segment->styles.i_flags = p_dec->fmt_in.subs.p_style->i_style_flags;
252 p_segment->styles.i_fontsize = p_dec->fmt_in.subs.p_style->i_font_size;
255 if ( !p_segment->psz_string )
257 SegmentFree( p_segment );
258 free( psz_subtitle );
262 /* Create the subpicture unit */
263 p_spu = decoder_NewSubpictureText( p_dec );
266 free( psz_subtitle );
267 SegmentFree( p_segment );
270 subpicture_updater_sys_t *p_spu_sys = p_spu->updater.p_sys;
272 /* Parse our styles */
273 while( (size_t)(p_buf - p_block->p_buffer) + 8 < p_block->i_buffer )
275 uint32_t i_atomsize = GetDWBE( p_buf );
276 vlc_fourcc_t i_atomtype = VLC_FOURCC(p_buf[4],p_buf[5],p_buf[6],p_buf[7]);
281 case VLC_FOURCC('s','t','y','l'):
283 if ( (size_t)(p_buf - p_block->p_buffer) < 14 ) break;
284 uint16_t i_nbrecords = GetWBE(p_buf);
285 uint16_t i_cur_record = 0;
287 while( i_cur_record++ < i_nbrecords )
289 if ( (size_t)(p_buf - p_block->p_buffer) < 12 ) break;
290 uint16_t i_start = __MIN( GetWBE(p_buf), i_psz_length - 1 );
291 uint16_t i_end = __MIN( GetWBE(p_buf + 2), i_psz_length - 1 );
293 segment_style_t style;
294 style.i_flags = ConvertFlags( p_buf[6] );
295 style.i_fontsize = p_buf[7];
296 style.i_color = GetDWBE(p_buf+8) >> 8;// RGBA -> ARGB
297 style.i_color |= (GetDWBE(p_buf+8) & 0xFF) << 24;
298 ApplySegmentStyle( &p_segment, i_start, i_end, &style );
300 if ( i_nbrecords == 1 )
304 p_spu_sys->style_flags.i_value = ConvertFlags( p_buf[6] );
305 p_spu_sys->style_flags.b_set = true;
307 p_spu_sys->i_font_height_abs_to_src = p_buf[7];
308 p_spu_sys->font_color.i_value = GetDWBE(p_buf+8) >> 8;// RGBA -> ARGB
309 p_spu_sys->font_color.i_value |= (GetDWBE(p_buf+8) & 0xFF) << 24;
310 p_spu_sys->font_color.b_set = true;
317 case VLC_FOURCC('d','r','p','o'):
318 if ( (size_t)(p_buf - p_block->p_buffer) < 4 ) break;
319 p_spu_sys->i_drop_shadow = __MAX( GetWBE(p_buf), GetWBE(p_buf+2) );
322 case VLC_FOURCC('d','r','p','t'):
323 if ( (size_t)(p_buf - p_block->p_buffer) < 2 ) break;
324 p_spu_sys->i_drop_shadow_alpha = GetWBE(p_buf);
334 p_spu->i_start = p_block->i_pts;
335 p_spu->i_stop = p_block->i_pts + p_block->i_length;
336 p_spu->b_ephemer = (p_block->i_length == 0);
337 p_spu->b_absolute = false;
339 p_spu_sys->align = SUBPICTURE_ALIGN_BOTTOM;
340 p_spu_sys->text = psz_subtitle;
341 p_spu_sys->p_htmlsegments = p_segment;
343 block_Release( p_block );