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