1 /*****************************************************************************
2 * subpicture.c: Subpicture functions
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ 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 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_image.h>
35 #include <vlc_subpicture.h>
36 #include "subpicture.h"
38 struct subpicture_private_t
44 subpicture_t *subpicture_New( const subpicture_updater_t *p_upd )
46 subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
50 p_subpic->i_order = 0;
51 p_subpic->b_absolute = true;
52 p_subpic->b_fade = false;
53 p_subpic->b_subtitle = false;
54 p_subpic->i_alpha = 0xFF;
55 p_subpic->p_region = NULL;
59 subpicture_private_t *p_private = malloc( sizeof(*p_private) );
65 video_format_Init( &p_private->src, 0 );
66 video_format_Init( &p_private->dst, 0 );
68 p_subpic->updater = *p_upd;
69 p_subpic->p_private = p_private;
73 p_subpic->p_private = NULL;
75 p_subpic->updater.pf_validate = NULL;
76 p_subpic->updater.pf_update = NULL;
77 p_subpic->updater.pf_destroy = NULL;
78 p_subpic->updater.p_sys = NULL;
83 void subpicture_Delete( subpicture_t *p_subpic )
85 subpicture_region_ChainDelete( p_subpic->p_region );
86 p_subpic->p_region = NULL;
88 if( p_subpic->updater.pf_destroy )
89 p_subpic->updater.pf_destroy( p_subpic );
91 free( p_subpic->p_private );
95 subpicture_t *subpicture_NewFromPicture( vlc_object_t *p_obj,
96 picture_t *p_picture, vlc_fourcc_t i_chroma )
99 video_format_t fmt_in = p_picture->format;
102 video_format_t fmt_out;
104 fmt_out.i_chroma = i_chroma;
107 image_handler_t *p_image = image_HandlerCreate( p_obj );
111 picture_t *p_pip = image_Convert( p_image, p_picture, &fmt_in, &fmt_out );
113 image_HandlerDelete( p_image );
118 subpicture_t *p_subpic = subpicture_New( NULL );
121 picture_Release( p_pip );
125 p_subpic->i_original_picture_width = fmt_out.i_width;
126 p_subpic->i_original_picture_height = fmt_out.i_height;
129 fmt_out.i_sar_den = 0;
131 p_subpic->p_region = subpicture_region_New( &fmt_out );
132 if( p_subpic->p_region )
134 picture_Release( p_subpic->p_region->p_picture );
135 p_subpic->p_region->p_picture = p_pip;
139 picture_Release( p_pip );
144 void subpicture_Update( subpicture_t *p_subpicture,
145 const video_format_t *p_fmt_src,
146 const video_format_t *p_fmt_dst,
149 subpicture_updater_t *p_upd = &p_subpicture->updater;
150 subpicture_private_t *p_private = p_subpicture->p_private;
152 if( !p_upd->pf_validate )
154 if( !p_upd->pf_validate( p_subpicture,
155 !video_format_IsSimilar( p_fmt_src,
156 &p_private->src ), p_fmt_src,
157 !video_format_IsSimilar( p_fmt_dst,
158 &p_private->dst ), p_fmt_dst,
162 subpicture_region_ChainDelete( p_subpicture->p_region );
163 p_subpicture->p_region = NULL;
165 p_upd->pf_update( p_subpicture, p_fmt_src, p_fmt_dst, i_ts );
167 video_format_Clean( &p_private->src );
168 video_format_Clean( &p_private->dst );
170 video_format_Copy( &p_private->src, p_fmt_src );
171 video_format_Copy( &p_private->dst, p_fmt_dst );
175 subpicture_region_private_t *subpicture_region_private_New( video_format_t *p_fmt )
177 subpicture_region_private_t *p_private = malloc( sizeof(*p_private) );
182 p_private->fmt = *p_fmt;
183 if( p_fmt->p_palette )
185 p_private->fmt.p_palette = malloc( sizeof(*p_private->fmt.p_palette) );
186 if( p_private->fmt.p_palette )
187 *p_private->fmt.p_palette = *p_fmt->p_palette;
189 p_private->p_picture = NULL;
194 void subpicture_region_private_Delete( subpicture_region_private_t *p_private )
196 if( p_private->p_picture )
197 picture_Release( p_private->p_picture );
198 free( p_private->fmt.p_palette );
202 subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
204 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
208 p_region->fmt = *p_fmt;
209 p_region->fmt.p_palette = NULL;
210 if( p_fmt->i_chroma == VLC_CODEC_YUVP )
212 p_region->fmt.p_palette = calloc( 1, sizeof(*p_region->fmt.p_palette) );
213 if( p_fmt->p_palette )
214 *p_region->fmt.p_palette = *p_fmt->p_palette;
216 p_region->i_alpha = 0xff;
217 p_region->p_next = NULL;
218 p_region->p_private = NULL;
219 p_region->psz_text = NULL;
220 p_region->p_style = NULL;
221 p_region->p_picture = NULL;
223 if( p_fmt->i_chroma == VLC_CODEC_TEXT )
226 p_region->p_picture = picture_NewFromFormat( p_fmt );
227 if( !p_region->p_picture )
229 free( p_region->fmt.p_palette );
237 void subpicture_region_Delete( subpicture_region_t *p_region )
242 if( p_region->p_private )
243 subpicture_region_private_Delete( p_region->p_private );
245 if( p_region->p_picture )
246 picture_Release( p_region->p_picture );
248 free( p_region->fmt.p_palette );
250 free( p_region->psz_text );
251 free( p_region->psz_html );
252 if( p_region->p_style )
253 text_style_Delete( p_region->p_style );
257 void subpicture_region_ChainDelete( subpicture_region_t *p_head )
261 subpicture_region_t *p_next = p_head->p_next;
263 subpicture_region_Delete( p_head );