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