]> git.sesse.net Git - vlc/blob - modules/codec/csri.c
update module LIST file.
[vlc] / modules / codec / csri.c
1 /*****************************************************************************
2  * csri.c : Load CSRI subtitle renderer
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: David Lamparter <equinox@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "config.h"
31 #include <vlc/vlc.h>
32 #include <vlc_vout.h>
33 #include <vlc_codec.h>
34 #include <vlc_osd.h>
35 #include <vlc_block.h>
36 #include <vlc_filter.h>
37 #include <vlc_stream.h>
38 #include <vlc_xml.h>
39
40 #include <math.h>
41
42 #ifdef HAVE_ERRNO_H
43 #   include <errno.h>
44 #endif
45
46 #include <csri/csri.h>
47 #include <csri/stream.h>
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  Create ( vlc_object_t * );
53 static void Destroy( vlc_object_t * );
54
55 static subpicture_t *DecodeBlock( decoder_t *, block_t ** );
56 static void DestroySubpicture( subpicture_t * );
57 static void PreRender( video_format_t *, spu_t *, subpicture_t *, mtime_t );
58 static subpicture_region_t *UpdateRegions( video_format_t *, spu_t *,
59                                            subpicture_t *, mtime_t );
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64
65 vlc_module_begin();
66     set_shortname( _("Subtitles (advanced)"));
67     set_description( _("Wrapper for subtitle renderers using CSRI/asa") );
68     set_capability( "decoder", 60 );
69     set_category( CAT_INPUT );
70     set_subcategory( SUBCAT_INPUT_SCODEC );
71     set_callbacks( Create, Destroy );
72 vlc_module_end();
73
74 /*****************************************************************************
75  * decoder_sys_t: filter data
76  *****************************************************************************/
77 struct decoder_sys_t
78 {
79     subpicture_t *p_spu_final;
80     video_format_t fmt_cached;
81     csri_inst *p_instance;
82     void (*pf_push_packet)(csri_inst *inst, const void *packet,
83                            size_t packetlen, double pts_start,
84                            double pts_end);
85 };
86
87 struct subpicture_sys_t
88 {
89     decoder_t *p_dec;
90     void *p_subs_data;
91     int i_subs_len;
92 };
93
94 /*****************************************************************************
95  * Create: Open CSRI renderer
96  *****************************************************************************
97  * Comment me.
98  *****************************************************************************/
99 static int Create( vlc_object_t *p_this )
100 {
101     decoder_t *p_dec = (decoder_t *)p_this;
102     decoder_sys_t *p_sys;
103     csri_rend *p_render;
104     struct csri_stream_ext *p_streamext;
105
106     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
107     {
108         return VLC_EGENERIC;
109     }
110
111     p_render = csri_renderer_default();
112     if (!p_render)
113     {
114         msg_Err( p_dec, "can't load csri renderer" );
115         return VLC_EGENERIC;
116     }
117     p_streamext = csri_query_ext(p_render, CSRI_EXT_STREAM_ASS);
118     if (!p_streamext)
119     {
120         msg_Err( p_dec, "csri renderer does not support ASS streaming" );
121         return VLC_EGENERIC;
122     }
123
124     p_dec->pf_decode_sub = DecodeBlock;
125
126     p_dec->p_sys = p_sys = malloc( sizeof( decoder_sys_t ) );
127     if( !p_sys )
128     {
129         msg_Err( p_dec, "out of memory" );
130         return VLC_ENOMEM;
131     }
132     memset( &p_dec->p_sys->fmt_cached, 0, sizeof( p_dec->p_sys->fmt_cached ) );
133
134     p_sys->pf_push_packet = p_streamext->push_packet;
135     p_sys->p_instance = p_streamext->init_stream(p_render,
136                                                  p_dec->fmt_in.p_extra,
137                                                  p_dec->fmt_in.i_extra - 1,
138                                                  NULL);
139     return VLC_SUCCESS;
140 }
141
142 /*****************************************************************************
143  * Destroy: finish
144  *****************************************************************************
145  * Comment me.
146  *****************************************************************************/
147 static void Destroy( vlc_object_t *p_this )
148 {
149     filter_t *p_filter = (filter_t *)p_this;
150     filter_sys_t *p_sys = p_filter->p_sys;
151
152     free( p_sys );
153 }
154
155 /****************************************************************************
156  * DecodeBlock: the whole thing
157  ****************************************************************************
158  * This function must be fed with complete subtitles units.
159  ****************************************************************************/
160 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
161 {
162     subpicture_t *p_spu = NULL;
163     block_t *p_block;
164
165     msg_Dbg( p_dec, "DecodeBlock %p", (void *)pp_block);
166     if( !pp_block || *pp_block == NULL ) return NULL;
167     p_block = *pp_block;
168     if( p_block->i_buffer == 0 || p_block->p_buffer[0] == '\0' ) return NULL;
169
170     p_spu = p_dec->pf_spu_buffer_new( p_dec );
171     if( !p_spu )
172     {
173         msg_Warn( p_dec, "can't get spu buffer" );
174         block_Release( *pp_block );
175         *pp_block = NULL;
176         return NULL;
177     }
178
179     p_spu->p_sys = malloc( sizeof( subpicture_sys_t ));
180     if( !p_spu->p_sys )
181     {
182         msg_Err( p_dec, "out of memory" );
183         p_dec->pf_spu_buffer_del( p_dec, p_spu );
184         block_Release( *pp_block );
185         *pp_block = NULL;
186         return NULL;
187     }
188     p_spu->p_sys->p_dec = p_dec;
189
190     p_spu->p_sys->i_subs_len = p_block->i_buffer;
191     p_spu->p_sys->p_subs_data = malloc( p_block->i_buffer );
192     if( !p_spu->p_sys->p_subs_data )
193     {
194         msg_Err( p_dec, "out of memory" );
195         free( p_spu->p_sys );
196         p_dec->pf_spu_buffer_del( p_dec, p_spu );
197         block_Release( *pp_block );
198         *pp_block = NULL;
199         return NULL;
200     }
201     memcpy( p_spu->p_sys->p_subs_data, p_block->p_buffer,
202             p_block->i_buffer );
203
204     p_spu->i_x = 0;
205     p_spu->i_y = 0;
206     p_spu->i_start = p_block->i_pts;
207     p_spu->i_stop = p_block->i_pts + p_block->i_length;
208     p_spu->b_ephemer = VLC_FALSE;
209     p_spu->b_absolute = VLC_FALSE;
210     p_spu->b_pausable = VLC_TRUE;
211
212     msg_Dbg( p_dec, "BS %lf..%lf", p_spu->i_start * 0.000001, p_spu->i_stop * 0.000001);
213     p_dec->p_sys->pf_push_packet(p_dec->p_sys->p_instance,
214                                  p_spu->p_sys->p_subs_data, p_block->i_buffer,
215                                  p_spu->i_start * 0.000001,
216                                  p_spu->i_stop * 0.000001);
217
218     p_spu->pf_pre_render = PreRender;
219     p_spu->pf_update_regions = UpdateRegions;
220     p_spu->pf_destroy = DestroySubpicture;
221
222     block_Release( *pp_block );
223     *pp_block = NULL;
224
225     return p_spu;
226 }
227
228 static void DestroySubpicture( subpicture_t *p_subpic )
229 {
230     msg_Dbg( p_subpic->p_sys->p_dec, "drop spu %p", (void *)p_subpic );
231     free( p_subpic->p_sys->p_subs_data );
232     free( p_subpic->p_sys );
233 }
234
235 static void PreRender( video_format_t *p_fmt, spu_t *p_spu,
236                        subpicture_t *p_subpic, mtime_t ts )
237 {
238     decoder_t *p_dec = p_subpic->p_sys->p_dec;
239     p_dec->p_sys->p_spu_final = p_subpic;
240 }
241
242 static subpicture_region_t *UpdateRegions( video_format_t *p_fmt, spu_t *p_spu,
243                                            subpicture_t *p_subpic, mtime_t ts )
244 {
245     decoder_t *p_dec = p_subpic->p_sys->p_dec;
246     subpicture_region_t *p_spu_region;
247     video_format_t fmt;
248     struct csri_frame csri_frame;
249
250     if (p_subpic != p_dec->p_sys->p_spu_final)
251     {
252         return NULL;
253     }
254
255     memcpy( &fmt, p_fmt, sizeof( fmt ) );
256     fmt.i_chroma = VLC_FOURCC('R','G','B','A');
257     fmt.i_width = fmt.i_visible_width;
258     fmt.i_height = fmt.i_visible_height;
259     fmt.i_bits_per_pixel = 0;
260     fmt.i_x_offset = fmt.i_y_offset = 0;
261
262     if (memcmp(&fmt, &p_dec->p_sys->fmt_cached, sizeof(fmt)))
263     {
264         struct csri_fmt csri_fmt;
265         memset(&csri_fmt, 0, sizeof(csri_fmt));
266         csri_fmt.pixfmt = CSRI_F_RGBA;
267         csri_fmt.width = fmt.i_width;
268         csri_fmt.height = fmt.i_height;
269         if( csri_request_fmt(p_dec->p_sys->p_instance, &csri_fmt) )
270             msg_Dbg( p_dec, "csri error: format not supported" );
271
272         memcpy(&p_dec->p_sys->fmt_cached, &fmt, sizeof(fmt));
273     }
274
275     p_spu_region = p_subpic->pf_create_region( VLC_OBJECT(p_dec), &fmt );
276     p_spu_region->i_align = SUBPICTURE_ALIGN_TOP;
277
278     csri_frame.pixfmt = CSRI_F_RGBA;
279     csri_frame.planes[0] = (unsigned char*)p_spu_region->picture.Y_PIXELS;
280     csri_frame.strides[0] = p_spu_region->picture.Y_PITCH;
281
282     msg_Dbg( p_dec, "TS %lf", ts * 0.000001 );
283     csri_render( p_dec->p_sys->p_instance, &csri_frame, ts * 0.000001 );
284     memset(p_spu_region->picture.Y_PIXELS, 0xff, fmt.i_width);
285     memset(p_spu_region->picture.Y_PIXELS + p_spu_region->picture.Y_PITCH
286                     * (fmt.i_height - 1), 0x88, fmt.i_width);
287
288     p_subpic->p_region = p_spu_region;
289     return p_spu_region;
290 }
291