1 /*****************************************************************************
2 * csri.c : Load CSRI subtitle renderer
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: David Lamparter <equinox@videolan.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 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
36 #include <vlc_block.h>
37 #include <vlc_filter.h>
38 #include <vlc_stream.h>
47 #include <csri/csri.h>
48 #include <csri/stream.h>
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Create ( vlc_object_t * );
54 static void Destroy( vlc_object_t * );
56 static subpicture_t *DecodeBlock( decoder_t *, block_t ** );
57 static void DestroySubpicture( subpicture_t * );
58 static void PreRender( video_format_t *, spu_t *, subpicture_t *, mtime_t );
59 static subpicture_region_t *UpdateRegions( video_format_t *, spu_t *,
60 subpicture_t *, mtime_t );
62 /*****************************************************************************
64 *****************************************************************************/
67 set_shortname( _("Subtitles (advanced)"));
68 set_description( _("Wrapper for subtitle renderers using CSRI/asa") );
69 set_capability( "decoder", 60 );
70 set_category( CAT_INPUT );
71 set_subcategory( SUBCAT_INPUT_SCODEC );
72 set_callbacks( Create, Destroy );
75 /*****************************************************************************
76 * decoder_sys_t: filter data
77 *****************************************************************************/
80 subpicture_t *p_spu_final;
81 video_format_t fmt_cached;
82 csri_inst *p_instance;
83 void (*pf_push_packet)(csri_inst *inst, const void *packet,
84 size_t packetlen, double pts_start,
88 struct subpicture_sys_t
95 /*****************************************************************************
96 * Create: Open CSRI renderer
97 *****************************************************************************
99 *****************************************************************************/
100 static int Create( vlc_object_t *p_this )
102 decoder_t *p_dec = (decoder_t *)p_this;
103 decoder_sys_t *p_sys;
105 struct csri_stream_ext *p_streamext;
107 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
112 p_render = csri_renderer_default();
115 msg_Err( p_dec, "can't load csri renderer" );
118 p_streamext = csri_query_ext(p_render, CSRI_EXT_STREAM_ASS);
121 msg_Err( p_dec, "csri renderer does not support ASS streaming" );
125 p_dec->pf_decode_sub = DecodeBlock;
127 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
130 memset( &p_dec->p_sys->fmt_cached, 0, sizeof( p_dec->p_sys->fmt_cached ) );
132 p_sys->pf_push_packet = p_streamext->push_packet;
133 p_sys->p_instance = p_streamext->init_stream(p_render,
134 p_dec->fmt_in.p_extra,
135 p_dec->fmt_in.i_extra - 1,
140 /*****************************************************************************
142 *****************************************************************************
144 *****************************************************************************/
145 static void Destroy( vlc_object_t *p_this )
147 filter_t *p_filter = (filter_t *)p_this;
148 filter_sys_t *p_sys = p_filter->p_sys;
153 /****************************************************************************
154 * DecodeBlock: the whole thing
155 ****************************************************************************
156 * This function must be fed with complete subtitles units.
157 ****************************************************************************/
158 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
160 subpicture_t *p_spu = NULL;
163 msg_Dbg( p_dec, "DecodeBlock %p", (void *)pp_block);
164 if( !pp_block || *pp_block == NULL ) return NULL;
166 if( p_block->i_buffer == 0 || p_block->p_buffer[0] == '\0' ) return NULL;
168 p_spu = p_dec->pf_spu_buffer_new( p_dec );
171 msg_Warn( p_dec, "can't get spu buffer" );
172 block_Release( *pp_block );
177 p_spu->p_sys = malloc( sizeof( subpicture_sys_t ));
180 p_dec->pf_spu_buffer_del( p_dec, p_spu );
181 block_Release( *pp_block );
185 p_spu->p_sys->p_dec = p_dec;
187 p_spu->p_sys->i_subs_len = p_block->i_buffer;
188 p_spu->p_sys->p_subs_data = malloc( p_block->i_buffer );
189 if( !p_spu->p_sys->p_subs_data )
191 free( p_spu->p_sys );
192 p_dec->pf_spu_buffer_del( p_dec, p_spu );
193 block_Release( *pp_block );
197 memcpy( p_spu->p_sys->p_subs_data, p_block->p_buffer,
202 p_spu->i_start = p_block->i_pts;
203 p_spu->i_stop = p_block->i_pts + p_block->i_length;
204 p_spu->b_ephemer = false;
205 p_spu->b_absolute = false;
206 p_spu->b_pausable = true;
208 msg_Dbg( p_dec, "BS %lf..%lf", p_spu->i_start * 0.000001, p_spu->i_stop * 0.000001);
209 p_dec->p_sys->pf_push_packet(p_dec->p_sys->p_instance,
210 p_spu->p_sys->p_subs_data, p_block->i_buffer,
211 p_spu->i_start * 0.000001,
212 p_spu->i_stop * 0.000001);
214 p_spu->pf_pre_render = PreRender;
215 p_spu->pf_update_regions = UpdateRegions;
216 p_spu->pf_destroy = DestroySubpicture;
218 block_Release( *pp_block );
224 static void DestroySubpicture( subpicture_t *p_subpic )
226 msg_Dbg( p_subpic->p_sys->p_dec, "drop spu %p", (void *)p_subpic );
227 free( p_subpic->p_sys->p_subs_data );
228 free( p_subpic->p_sys );
231 static void PreRender( video_format_t *p_fmt, spu_t *p_spu,
232 subpicture_t *p_subpic, mtime_t ts )
234 decoder_t *p_dec = p_subpic->p_sys->p_dec;
235 p_dec->p_sys->p_spu_final = p_subpic;
238 static subpicture_region_t *UpdateRegions( video_format_t *p_fmt, spu_t *p_spu,
239 subpicture_t *p_subpic, mtime_t ts )
241 decoder_t *p_dec = p_subpic->p_sys->p_dec;
242 subpicture_region_t *p_spu_region;
244 struct csri_frame csri_frame;
246 if (p_subpic != p_dec->p_sys->p_spu_final)
251 memcpy( &fmt, p_fmt, sizeof( fmt ) );
252 fmt.i_chroma = VLC_FOURCC('R','G','B','A');
253 fmt.i_width = fmt.i_visible_width;
254 fmt.i_height = fmt.i_visible_height;
255 fmt.i_bits_per_pixel = 0;
256 fmt.i_x_offset = fmt.i_y_offset = 0;
258 if (memcmp(&fmt, &p_dec->p_sys->fmt_cached, sizeof(fmt)))
260 struct csri_fmt csri_fmt;
261 memset(&csri_fmt, 0, sizeof(csri_fmt));
262 csri_fmt.pixfmt = CSRI_F_RGBA;
263 csri_fmt.width = fmt.i_width;
264 csri_fmt.height = fmt.i_height;
265 if( csri_request_fmt(p_dec->p_sys->p_instance, &csri_fmt) )
266 msg_Dbg( p_dec, "csri error: format not supported" );
268 memcpy(&p_dec->p_sys->fmt_cached, &fmt, sizeof(fmt));
271 p_spu_region = p_subpic->pf_create_region( VLC_OBJECT(p_dec), &fmt );
272 p_spu_region->i_align = SUBPICTURE_ALIGN_TOP;
274 csri_frame.pixfmt = CSRI_F_RGBA;
275 csri_frame.planes[0] = (unsigned char*)p_spu_region->picture.Y_PIXELS;
276 csri_frame.strides[0] = p_spu_region->picture.Y_PITCH;
278 msg_Dbg( p_dec, "TS %lf", ts * 0.000001 );
279 csri_render( p_dec->p_sys->p_instance, &csri_frame, ts * 0.000001 );
280 memset(p_spu_region->picture.Y_PIXELS, 0xff, fmt.i_width);
281 memset(p_spu_region->picture.Y_PIXELS + p_spu_region->picture.Y_PITCH
282 * (fmt.i_height - 1), 0x88, fmt.i_width);
284 p_subpic->p_region = p_spu_region;