]> git.sesse.net Git - vlc/blob - modules/codec/csri.c
d685ac0e58c1338b20a93660542a71a947b8ea0d
[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
28 #ifdef HAVE_CONFIG_H
29 #   include "config.h"
30 #endif
31
32 #include <string.h>
33 #include <math.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_codec.h>
38 #include <vlc_osd.h>
39 #include <vlc_input.h>
40
41 #include <csri/csri.h>
42 #include <csri/stream.h>
43
44 /*****************************************************************************
45  * Module descriptor
46  *****************************************************************************/
47 static int  Create ( vlc_object_t * );
48 static void Destroy( vlc_object_t * );
49
50 vlc_module_begin ()
51     set_shortname( N_("Subtitles (advanced)"))
52     set_description( N_("Wrapper for subtitle renderers using CSRI/asa") )
53     set_capability( "decoder", 60 )
54     set_category( CAT_INPUT )
55     set_subcategory( SUBCAT_INPUT_SCODEC )
56     set_callbacks( Create, Destroy )
57 vlc_module_end ()
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static subpicture_t *DecodeBlock( decoder_t *, block_t ** );
63 static void DestroySubpicture( subpicture_t * );
64 static void UpdateRegions( spu_t *, subpicture_t *,
65                            const video_format_t *,  mtime_t );
66
67 /*****************************************************************************
68  * decoder_sys_t: decoder data
69  *****************************************************************************/
70 struct decoder_sys_t
71 {
72     video_format_t fmt_cached;
73     csri_inst *p_instance;
74
75     struct csri_stream_ext *p_stream_ext;
76
77     void (*pf_push_packet)(csri_inst *inst, const void *packet,
78                            size_t packetlen, double pts_start,
79                            double pts_end);
80     mtime_t i_max_stop;
81 };
82
83 struct subpicture_sys_t
84 {
85     decoder_t *p_dec;
86     void      *p_subs_data;
87     int       i_subs_len;
88     mtime_t   i_pts;
89 };
90
91 /*****************************************************************************
92  * Create: Open CSRI renderer
93  *****************************************************************************
94  * Comment me.
95  *****************************************************************************/
96 static int Create( vlc_object_t *p_this )
97 {
98     decoder_t *p_dec = (decoder_t *)p_this;
99     decoder_sys_t *p_sys;
100     csri_rend *p_render;
101     struct csri_stream_ext *p_stream_ext;
102
103     if( p_dec->fmt_in.i_codec != VLC_CODEC_SSA )
104         return VLC_EGENERIC;
105
106     p_render = csri_renderer_default();
107     if (!p_render)
108     {
109         msg_Err( p_dec, "can't load csri renderer" );
110         return VLC_EGENERIC;
111     }
112     p_stream_ext = csri_query_ext(p_render, CSRI_EXT_STREAM_ASS);
113     if (!p_stream_ext)
114     {
115         msg_Err( p_dec, "csri renderer does not support ASS streaming" );
116         return VLC_EGENERIC;
117     }
118
119     p_dec->pf_decode_sub = DecodeBlock;
120
121     p_dec->p_sys = p_sys = calloc( 1, sizeof( decoder_sys_t ) );
122     if( !p_sys )
123         return VLC_ENOMEM;
124
125     p_sys->p_stream_ext = p_stream_ext;
126     p_sys->pf_push_packet = p_stream_ext->push_packet;
127     p_sys->p_instance = p_stream_ext->init_stream( p_render,
128                                                    p_dec->fmt_in.p_extra,
129                                                    p_dec->fmt_in.p_extra ? strnlen( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra ) : 0,
130                                                    NULL);
131     p_sys->i_max_stop = VLC_TS_INVALID;
132
133     p_dec->fmt_out.i_cat = SPU_ES;
134     p_dec->fmt_out.i_codec = VLC_CODEC_RGBA;
135
136
137     return VLC_SUCCESS;
138 }
139
140 /*****************************************************************************
141  * Destroy: finish
142  *****************************************************************************
143  * Comment me.
144  *****************************************************************************/
145 static void Destroy( vlc_object_t *p_this )
146 {
147     decoder_t *p_dec = (decoder_t *)p_this;
148     decoder_sys_t *p_sys = p_dec->p_sys;
149
150     if( p_sys->p_stream_ext->discard )
151         p_sys->p_stream_ext->discard( p_sys->p_instance, true );
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     decoder_sys_t *p_sys = p_dec->p_sys;
163
164     subpicture_t *p_spu = NULL;
165     block_t *p_block;
166
167     //msg_Dbg( p_dec, "DecodeBlock %p", (void *)pp_block);
168     if( !pp_block || *pp_block == NULL )
169         return NULL;
170
171     p_block = *pp_block;
172     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
173     {
174         p_sys->i_max_stop = VLC_TS_INVALID;
175         block_Release( p_block );
176         return NULL;
177     }
178     *pp_block = NULL;
179
180     if( p_block->i_buffer == 0 || p_block->p_buffer[0] == '\0' )
181     {
182         block_Release( p_block );
183         return NULL;
184     }
185
186     p_spu = decoder_NewSubpicture( p_dec );
187     if( !p_spu )
188     {
189         msg_Warn( p_dec, "can't get spu buffer" );
190         block_Release( p_block );
191         return NULL;
192     }
193
194     p_spu->p_sys = malloc( sizeof( subpicture_sys_t ));
195     if( !p_spu->p_sys )
196     {
197         decoder_DeleteSubpicture( p_dec, p_spu );
198         block_Release( p_block );
199         return NULL;
200     }
201     p_spu->p_sys->p_dec = p_dec;
202
203     p_spu->p_sys->i_subs_len = p_block->i_buffer;
204     p_spu->p_sys->p_subs_data = malloc( p_block->i_buffer );
205     if( !p_spu->p_sys->p_subs_data )
206     {
207         free( p_spu->p_sys );
208         decoder_DeleteSubpicture( p_dec, p_spu );
209         block_Release( p_block );
210         return NULL;
211     }
212     memcpy( p_spu->p_sys->p_subs_data, p_block->p_buffer,
213             p_block->i_buffer );
214     p_spu->p_sys->i_pts = p_block->i_pts;
215
216     p_spu->i_start = p_block->i_pts;
217     p_spu->i_stop = __MAX( p_sys->i_max_stop, p_block->i_pts + p_block->i_length );
218     p_spu->b_ephemer = true;
219     p_spu->b_absolute = true;
220
221     p_sys->i_max_stop = p_spu->i_stop;
222
223     //msg_Dbg( p_dec, "BS %lf..%lf", p_spu->i_start * 0.000001, p_spu->i_stop * 0.000001);
224     p_sys->pf_push_packet( p_sys->p_instance,
225                            p_spu->p_sys->p_subs_data, p_spu->p_sys->i_subs_len,
226                            p_block->i_pts * 0.000001,
227                            (p_block->i_pts + p_block->i_length) * 0.000001);
228
229     p_spu->pf_update_regions = UpdateRegions;
230     p_spu->pf_destroy = DestroySubpicture;
231
232     block_Release( p_block );
233
234     return p_spu;
235 }
236
237 static void DestroySubpicture( subpicture_t *p_subpic )
238 {
239     //msg_Dbg( p_subpic->p_sys->p_dec, "drop spu %p", (void *)p_subpic );
240     free( p_subpic->p_sys->p_subs_data );
241     free( p_subpic->p_sys );
242 }
243
244 static void UpdateRegions( spu_t *p_spu, subpicture_t *p_subpic,
245                            const video_format_t *p_fmt, mtime_t i_ts )
246 {
247     decoder_t *p_dec = p_subpic->p_sys->p_dec;
248     decoder_sys_t *p_sys = p_dec->p_sys;
249
250     subpicture_region_t *p_spu_region;
251     video_format_t fmt;
252
253     /* TODO maybe checking if we really need redrawing */
254     subpicture_region_ChainDelete( p_subpic->p_region );
255     p_subpic->p_region = NULL;
256
257 #if 0
258     msg_Warn( p_dec, "---- fmt: %dx%d %dx%d chroma=%4.4s",
259             p_fmt->i_width, p_fmt->i_height,
260             p_fmt->i_visible_width, p_fmt->i_visible_height,
261             (const char*)&p_fmt->i_chroma );
262 #endif
263     /* XXX On x86 at least our RGBA is mapped to their BGRA
264      * TODO confirm that is the same on big endian cpu */
265     fmt = *p_fmt;
266     fmt.i_chroma = VLC_CODEC_RGBA;
267     fmt.i_width = fmt.i_visible_width;
268     fmt.i_height = fmt.i_visible_height;
269     fmt.i_bits_per_pixel = 0;
270     fmt.i_x_offset = fmt.i_y_offset = 0;
271     fmt.i_sar_num = 1;
272     fmt.i_sar_den = 1;
273
274     if( memcmp(&fmt, &p_sys->fmt_cached, sizeof(fmt)) )
275     {
276         //msg_Warn( p_dec, "---- fmt: new %dx%d", fmt.i_width, fmt.i_height );
277
278         struct csri_fmt csri_fmt;
279         memset(&csri_fmt, 0, sizeof(csri_fmt));
280         csri_fmt.pixfmt = CSRI_F_BGRA;
281         csri_fmt.width = fmt.i_width;
282         csri_fmt.height = fmt.i_height;
283         if( csri_request_fmt( p_sys->p_instance, &csri_fmt ) )
284             msg_Dbg( p_dec, "csri error: format not supported" );
285
286         p_sys->fmt_cached = fmt;
287     }
288
289     p_subpic->i_original_picture_height = fmt.i_height;
290     p_subpic->i_original_picture_width = fmt.i_width;
291
292     p_spu_region = p_subpic->p_region = subpicture_region_New( &fmt );
293
294     if( p_spu_region )
295     {
296         struct csri_frame csri_frame;
297
298         /* */
299         p_spu_region->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
300         memset( p_spu_region->p_picture->Y_PIXELS, 0x00, p_spu_region->p_picture->Y_PITCH * p_sys->fmt_cached.i_height );
301
302         /* */
303         const mtime_t i_stream_date = p_subpic->p_sys->i_pts + (i_ts - p_subpic->i_start);
304
305         //msg_Dbg( p_dec, "TS %lf", ts * 0.000001 );
306         memset( &csri_frame, 0, sizeof(csri_frame) );
307         csri_frame.pixfmt = CSRI_F_BGRA;
308         csri_frame.planes[0] = (unsigned char*)p_spu_region->p_picture->Y_PIXELS;
309         csri_frame.strides[0] = p_spu_region->p_picture->Y_PITCH;
310         csri_render( p_sys->p_instance, &csri_frame, i_stream_date * 0.000001 );
311     }
312 }
313