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 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
40 #include <vlc_input.h>
42 #include <csri/csri.h>
43 #include <csri/stream.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Create ( vlc_object_t * );
49 static void Destroy( vlc_object_t * );
52 set_shortname( N_("Subtitles (advanced)"));
53 set_description( N_("Wrapper for subtitle renderers using CSRI/asa") );
54 set_capability( "decoder", 60 );
55 set_category( CAT_INPUT );
56 set_subcategory( SUBCAT_INPUT_SCODEC );
57 set_callbacks( Create, Destroy );
60 /*****************************************************************************
62 *****************************************************************************/
63 static subpicture_t *DecodeBlock( decoder_t *, block_t ** );
64 static void DestroySubpicture( subpicture_t * );
65 static void PreRender( video_format_t *, spu_t *, subpicture_t *, mtime_t );
66 static subpicture_region_t *UpdateRegions( video_format_t *, spu_t *,
67 subpicture_t *, mtime_t );
69 /*****************************************************************************
70 * decoder_sys_t: decoder data
71 *****************************************************************************/
74 subpicture_t *p_spu_final;
75 video_format_t fmt_cached;
76 csri_inst *p_instance;
78 struct csri_stream_ext *p_stream_ext;
80 void (*pf_push_packet)(csri_inst *inst, const void *packet,
81 size_t packetlen, double pts_start,
85 struct subpicture_sys_t
92 /*****************************************************************************
93 * Create: Open CSRI renderer
94 *****************************************************************************
96 *****************************************************************************/
97 static int Create( vlc_object_t *p_this )
99 decoder_t *p_dec = (decoder_t *)p_this;
100 decoder_sys_t *p_sys;
102 struct csri_stream_ext *p_stream_ext;
104 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
107 p_render = csri_renderer_default();
110 msg_Err( p_dec, "can't load csri renderer" );
113 p_stream_ext = csri_query_ext(p_render, CSRI_EXT_STREAM_ASS);
116 msg_Err( p_dec, "csri renderer does not support ASS streaming" );
120 p_dec->pf_decode_sub = DecodeBlock;
122 p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
125 memset( &p_sys->fmt_cached, 0, sizeof( p_sys->fmt_cached ) );
127 p_sys->p_stream_ext = p_stream_ext;
128 p_sys->pf_push_packet = p_stream_ext->push_packet;
129 p_sys->p_instance = p_stream_ext->init_stream( p_render,
130 p_dec->fmt_in.p_extra,
131 p_dec->fmt_in.p_extra ? strnlen( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ) : 0,
136 /*****************************************************************************
138 *****************************************************************************
140 *****************************************************************************/
141 static void Destroy( vlc_object_t *p_this )
143 decoder_t *p_dec = (decoder_t *)p_this;
144 decoder_sys_t *p_sys = p_dec->p_sys;
146 if( p_sys->p_stream_ext->discard )
147 p_sys->p_stream_ext->discard( p_sys->p_instance, true );
151 /****************************************************************************
152 * DecodeBlock: the whole thing
153 ****************************************************************************
154 * This function must be fed with complete subtitles units.
155 ****************************************************************************/
156 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
158 decoder_sys_t *p_sys = p_dec->p_sys;
160 subpicture_t *p_spu = NULL;
163 //msg_Dbg( p_dec, "DecodeBlock %p", (void *)pp_block);
164 if( !pp_block || *pp_block == NULL )
168 if( p_block->i_rate != 0 )
169 p_block->i_length = p_block->i_length * p_block->i_rate / INPUT_RATE_DEFAULT;
172 if( p_block->i_buffer == 0 || p_block->p_buffer[0] == '\0' )
174 block_Release( p_block );
178 p_spu = p_dec->pf_spu_buffer_new( p_dec );
181 msg_Warn( p_dec, "can't get spu buffer" );
182 block_Release( p_block );
186 p_spu->p_sys = malloc( sizeof( subpicture_sys_t ));
189 p_dec->pf_spu_buffer_del( p_dec, p_spu );
190 block_Release( p_block );
193 p_spu->p_sys->p_dec = p_dec;
195 p_spu->p_sys->i_subs_len = p_block->i_buffer;
196 p_spu->p_sys->p_subs_data = malloc( p_block->i_buffer );
197 if( !p_spu->p_sys->p_subs_data )
199 free( p_spu->p_sys );
200 p_dec->pf_spu_buffer_del( p_dec, p_spu );
201 block_Release( p_block );
204 memcpy( p_spu->p_sys->p_subs_data, p_block->p_buffer,
209 p_spu->i_start = p_block->i_pts;
210 p_spu->i_stop = p_block->i_pts + p_block->i_length;
211 p_spu->b_ephemer = false;
212 p_spu->b_absolute = false;
213 p_spu->b_pausable = true;
215 //msg_Dbg( p_dec, "BS %lf..%lf", p_spu->i_start * 0.000001, p_spu->i_stop * 0.000001);
216 p_sys->pf_push_packet( p_sys->p_instance,
217 p_spu->p_sys->p_subs_data, p_spu->p_sys->i_subs_len,
218 p_spu->i_start * 0.000001,
219 p_spu->i_stop * 0.000001);
221 p_spu->pf_pre_render = PreRender;
222 p_spu->pf_update_regions = UpdateRegions;
223 p_spu->pf_destroy = DestroySubpicture;
225 block_Release( p_block );
230 static void DestroySubpicture( subpicture_t *p_subpic )
232 //msg_Dbg( p_subpic->p_sys->p_dec, "drop spu %p", (void *)p_subpic );
233 free( p_subpic->p_sys->p_subs_data );
234 free( p_subpic->p_sys );
237 static void PreRender( video_format_t *p_fmt, spu_t *p_spu,
238 subpicture_t *p_subpic, mtime_t ts )
240 decoder_t *p_dec = p_subpic->p_sys->p_dec;
241 p_dec->p_sys->p_spu_final = p_subpic;
244 static subpicture_region_t *UpdateRegions( video_format_t *p_fmt, spu_t *p_spu,
245 subpicture_t *p_subpic, mtime_t ts )
247 decoder_t *p_dec = p_subpic->p_sys->p_dec;
248 decoder_sys_t *p_sys = p_dec->p_sys;
250 subpicture_region_t *p_spu_region;
253 if( p_subpic != p_sys->p_spu_final )
257 msg_Warn( p_dec, "---- fmt: %dx%d %dx%d chroma=%4.4s",
258 p_fmt->i_width, p_fmt->i_height,
259 p_fmt->i_visible_width, p_fmt->i_visible_height,
260 (const char*)&p_fmt->i_chroma );
262 /* XXX On x86 at least our RGBA is mapped to their BGRA
263 * TODO confirm that is the same on big endian cpu */
265 fmt.i_chroma = VLC_FOURCC('R','G','B','A');
266 fmt.i_width = fmt.i_visible_width;
267 fmt.i_height = fmt.i_visible_height;
268 fmt.i_bits_per_pixel = 0;
269 fmt.i_x_offset = fmt.i_y_offset = 0;
273 if( memcmp(&fmt, &p_sys->fmt_cached, sizeof(fmt)) )
275 //msg_Warn( p_dec, "---- fmt: new %dx%d", fmt.i_width, fmt.i_height );
277 struct csri_fmt csri_fmt;
278 memset(&csri_fmt, 0, sizeof(csri_fmt));
279 csri_fmt.pixfmt = CSRI_F_BGRA;
280 csri_fmt.width = fmt.i_width;
281 csri_fmt.height = fmt.i_height;
282 if( csri_request_fmt( p_sys->p_instance, &csri_fmt ) )
283 msg_Dbg( p_dec, "csri error: format not supported" );
285 p_sys->fmt_cached = fmt;
288 p_subpic->i_original_picture_height = fmt.i_height;
289 p_subpic->i_original_picture_width = fmt.i_width;
291 p_spu_region = p_subpic->pf_create_region( VLC_OBJECT(p_dec), &fmt );
295 struct csri_frame csri_frame;
298 p_spu_region->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
299 memset( p_spu_region->picture.Y_PIXELS, 0x00, p_spu_region->picture.Y_PITCH * p_sys->fmt_cached.i_height );
302 //msg_Dbg( p_dec, "TS %lf", ts * 0.000001 );
303 memset( &csri_frame, 0, sizeof(csri_frame) );
304 csri_frame.pixfmt = CSRI_F_BGRA;
305 csri_frame.planes[0] = (unsigned char*)p_spu_region->picture.Y_PIXELS;
306 csri_frame.strides[0] = p_spu_region->picture.Y_PITCH;
307 csri_render( p_sys->p_instance, &csri_frame, ts * 0.000001 );