Fix some typos.
[vlc/asuraparaju-public.git] / src / misc / subpicture.c
blob3e84a27a049e46752bc05768960ad0b9f103d9da
1 /*****************************************************************************
2 * subpicture.c: Subpicture functions
3 *****************************************************************************
4 * Copyright (C) 2010 Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
5 * $Id$
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <assert.h>
33 #include <vlc_common.h>
34 #include <vlc_image.h>
35 #include <vlc_subpicture.h>
36 #include "subpicture.h"
38 struct subpicture_private_t
40 video_format_t src;
41 video_format_t dst;
44 subpicture_t *subpicture_New( const subpicture_updater_t *p_upd )
46 subpicture_t *p_subpic = calloc( 1, sizeof(*p_subpic) );
47 if( !p_subpic )
48 return NULL;
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;
57 if( p_upd )
59 subpicture_private_t *p_private = malloc( sizeof(*p_private) );
60 if( !p_private )
62 free( p_subpic );
63 return NULL;
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;
71 else
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;
80 return p_subpic;
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 );
92 free( p_subpic );
95 subpicture_t *subpicture_NewFromPicture( vlc_object_t *p_obj,
96 picture_t *p_picture, vlc_fourcc_t i_chroma )
98 /* */
99 video_format_t fmt_in = p_picture->format;
101 /* */
102 video_format_t fmt_out;
103 fmt_out = fmt_in;
104 fmt_out.i_chroma = i_chroma;
106 /* */
107 image_handler_t *p_image = image_HandlerCreate( p_obj );
108 if( !p_image )
109 return NULL;
111 picture_t *p_pip = image_Convert( p_image, p_picture, &fmt_in, &fmt_out );
113 image_HandlerDelete( p_image );
115 if( !p_pip )
116 return NULL;
118 subpicture_t *p_subpic = subpicture_New( NULL );
119 if( !p_subpic )
121 picture_Release( p_pip );
122 return NULL;
125 p_subpic->i_original_picture_width = fmt_out.i_width;
126 p_subpic->i_original_picture_height = fmt_out.i_height;
128 fmt_out.i_sar_num =
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;
137 else
139 picture_Release( p_pip );
141 return p_subpic;
144 void subpicture_Update( subpicture_t *p_subpicture,
145 const video_format_t *p_fmt_src,
146 const video_format_t *p_fmt_dst,
147 mtime_t i_ts )
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 )
153 return;
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,
159 i_ts ) )
160 return;
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) );
179 if( !p_private )
180 return NULL;
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;
191 return p_private;
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 );
199 free( p_private );
202 subpicture_region_t *subpicture_region_New( const video_format_t *p_fmt )
204 subpicture_region_t *p_region = calloc( 1, sizeof(*p_region ) );
205 if( !p_region )
206 return NULL;
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 )
224 return p_region;
226 p_region->p_picture = picture_NewFromFormat( p_fmt );
227 if( !p_region->p_picture )
229 free( p_region->fmt.p_palette );
230 free( p_region );
231 return NULL;
234 return p_region;
237 void subpicture_region_Delete( subpicture_region_t *p_region )
239 if( !p_region )
240 return;
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 );
254 free( p_region );
257 void subpicture_region_ChainDelete( subpicture_region_t *p_head )
259 while( p_head )
261 subpicture_region_t *p_next = p_head->p_next;
263 subpicture_region_Delete( p_head );
265 p_head = p_next;