]> git.sesse.net Git - vlc/blob - modules/codec/csri.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[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_vout.h>
38 #include <vlc_codec.h>
39 #include <vlc_osd.h>
40 #include <vlc_input.h>
41
42 #include <csri/csri.h>
43 #include <csri/stream.h>
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 static int  Create ( vlc_object_t * );
49 static void Destroy( vlc_object_t * );
50
51 vlc_module_begin ()
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 )
58 vlc_module_end ()
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static subpicture_t *DecodeBlock( decoder_t *, block_t ** );
64 static void DestroySubpicture( subpicture_t * );
65 static void PreRender( spu_t *, subpicture_t *, const video_format_t * );
66 static void UpdateRegions( spu_t *, subpicture_t *,
67                            const video_format_t *,  mtime_t );
68
69 /*****************************************************************************
70  * decoder_sys_t: decoder data
71  *****************************************************************************/
72 struct decoder_sys_t
73 {
74     subpicture_t *p_spu_final;
75     video_format_t fmt_cached;
76     csri_inst *p_instance;
77
78     struct csri_stream_ext *p_stream_ext;
79
80     void (*pf_push_packet)(csri_inst *inst, const void *packet,
81                            size_t packetlen, double pts_start,
82                            double pts_end);
83 };
84
85 struct subpicture_sys_t
86 {
87     decoder_t *p_dec;
88     void      *p_subs_data;
89     int       i_subs_len;
90     mtime_t   i_pts;
91 };
92
93 /*****************************************************************************
94  * Create: Open CSRI renderer
95  *****************************************************************************
96  * Comment me.
97  *****************************************************************************/
98 static int Create( vlc_object_t *p_this )
99 {
100     decoder_t *p_dec = (decoder_t *)p_this;
101     decoder_sys_t *p_sys;
102     csri_rend *p_render;
103     struct csri_stream_ext *p_stream_ext;
104
105     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','s','a',' ') )
106         return VLC_EGENERIC;
107
108     p_render = csri_renderer_default();
109     if (!p_render)
110     {
111         msg_Err( p_dec, "can't load csri renderer" );
112         return VLC_EGENERIC;
113     }
114     p_stream_ext = csri_query_ext(p_render, CSRI_EXT_STREAM_ASS);
115     if (!p_stream_ext)
116     {
117         msg_Err( p_dec, "csri renderer does not support ASS streaming" );
118         return VLC_EGENERIC;
119     }
120
121     p_dec->pf_decode_sub = DecodeBlock;
122
123     p_dec->p_sys = p_sys = calloc( 1, sizeof( decoder_sys_t ) );
124     if( !p_sys )
125         return VLC_ENOMEM;
126
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,
132                                                    NULL);
133     p_dec->fmt_out.i_cat = SPU_ES;
134     p_dec->fmt_out.i_codec = VLC_FOURCC('R','G','B','A');
135
136     return VLC_SUCCESS;
137 }
138
139 /*****************************************************************************
140  * Destroy: finish
141  *****************************************************************************
142  * Comment me.
143  *****************************************************************************/
144 static void Destroy( vlc_object_t *p_this )
145 {
146     decoder_t *p_dec = (decoder_t *)p_this;
147     decoder_sys_t *p_sys = p_dec->p_sys;
148
149     if( p_sys->p_stream_ext->discard )
150         p_sys->p_stream_ext->discard( p_sys->p_instance, true );
151     free( p_sys );
152 }
153
154 /****************************************************************************
155  * DecodeBlock: the whole thing
156  ****************************************************************************
157  * This function must be fed with complete subtitles units.
158  ****************************************************************************/
159 static subpicture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
160 {
161     decoder_sys_t *p_sys = p_dec->p_sys;
162
163     subpicture_t *p_spu = NULL;
164     block_t *p_block;
165
166     //msg_Dbg( p_dec, "DecodeBlock %p", (void *)pp_block);
167     if( !pp_block || *pp_block == NULL )
168         return NULL;
169
170     p_block = *pp_block;
171     *pp_block = NULL;
172
173     if( p_block->i_buffer == 0 || p_block->p_buffer[0] == '\0' )
174     {
175         block_Release( p_block );
176         return NULL;
177     }
178
179     p_spu = decoder_NewSubpicture( p_dec );
180     if( !p_spu )
181     {
182         msg_Warn( p_dec, "can't get spu buffer" );
183         block_Release( p_block );
184         return NULL;
185     }
186
187     p_spu->p_sys = malloc( sizeof( subpicture_sys_t ));
188     if( !p_spu->p_sys )
189     {
190         decoder_DeleteSubpicture( p_dec, p_spu );
191         block_Release( p_block );
192         return NULL;
193     }
194     p_spu->p_sys->p_dec = p_dec;
195
196     p_spu->p_sys->i_subs_len = p_block->i_buffer;
197     p_spu->p_sys->p_subs_data = malloc( p_block->i_buffer );
198     if( !p_spu->p_sys->p_subs_data )
199     {
200         free( p_spu->p_sys );
201         decoder_DeleteSubpicture( p_dec, p_spu );
202         block_Release( p_block );
203         return NULL;
204     }
205     memcpy( p_spu->p_sys->p_subs_data, p_block->p_buffer,
206             p_block->i_buffer );
207     p_spu->p_sys->i_pts = p_block->i_pts;
208
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
214     //msg_Dbg( p_dec, "BS %lf..%lf", p_spu->i_start * 0.000001, p_spu->i_stop * 0.000001);
215     p_sys->pf_push_packet( p_sys->p_instance,
216                            p_spu->p_sys->p_subs_data, p_spu->p_sys->i_subs_len,
217                            p_spu->i_start * 0.000001,
218                            p_spu->i_stop * 0.000001);
219
220     p_spu->pf_pre_render = PreRender;
221     p_spu->pf_update_regions = UpdateRegions;
222     p_spu->pf_destroy = DestroySubpicture;
223
224     block_Release( p_block );
225
226     return p_spu;
227 }
228
229 static void DestroySubpicture( subpicture_t *p_subpic )
230 {
231     //msg_Dbg( p_subpic->p_sys->p_dec, "drop spu %p", (void *)p_subpic );
232     free( p_subpic->p_sys->p_subs_data );
233     free( p_subpic->p_sys );
234 }
235
236 static void PreRender( spu_t *p_spu, subpicture_t *p_subpic,
237                        const video_format_t *p_fmt )
238 {
239     decoder_t *p_dec = p_subpic->p_sys->p_dec;
240     p_dec->p_sys->p_spu_final = p_subpic;
241     VLC_UNUSED(p_fmt);
242     VLC_UNUSED(p_spu);
243 }
244
245 static void UpdateRegions( spu_t *p_spu, subpicture_t *p_subpic,
246                            const video_format_t *p_fmt, mtime_t i_ts )
247 {
248     decoder_t *p_dec = p_subpic->p_sys->p_dec;
249     decoder_sys_t *p_sys = p_dec->p_sys;
250
251     subpicture_region_t *p_spu_region;
252     video_format_t fmt;
253
254     /* TODO maybe checking if we really need redrawing */
255     subpicture_region_ChainDelete( p_subpic->p_region );
256     p_subpic->p_region = NULL;
257
258     /* FIXME check why this is needed */
259     if( p_subpic != p_sys->p_spu_final )
260         return;
261
262 #if 0
263     msg_Warn( p_dec, "---- fmt: %dx%d %dx%d chroma=%4.4s",
264             p_fmt->i_width, p_fmt->i_height,
265             p_fmt->i_visible_width, p_fmt->i_visible_height,
266             (const char*)&p_fmt->i_chroma );
267 #endif
268     /* XXX On x86 at least our RGBA is mapped to their BGRA
269      * TODO confirm that is the same on big endian cpu */
270     fmt = *p_fmt;
271     fmt.i_chroma = VLC_FOURCC('R','G','B','A');
272     fmt.i_width = fmt.i_visible_width;
273     fmt.i_height = fmt.i_visible_height;
274     fmt.i_bits_per_pixel = 0;
275     fmt.i_x_offset = fmt.i_y_offset = 0;
276     fmt.i_sar_num = 1;
277     fmt.i_sar_den = 1;
278
279     if( memcmp(&fmt, &p_sys->fmt_cached, sizeof(fmt)) )
280     {
281         //msg_Warn( p_dec, "---- fmt: new %dx%d", fmt.i_width, fmt.i_height );
282
283         struct csri_fmt csri_fmt;
284         memset(&csri_fmt, 0, sizeof(csri_fmt));
285         csri_fmt.pixfmt = CSRI_F_BGRA;
286         csri_fmt.width = fmt.i_width;
287         csri_fmt.height = fmt.i_height;
288         if( csri_request_fmt( p_sys->p_instance, &csri_fmt ) )
289             msg_Dbg( p_dec, "csri error: format not supported" );
290
291         p_sys->fmt_cached = fmt;
292     }
293
294     p_subpic->i_original_picture_height = fmt.i_height;
295     p_subpic->i_original_picture_width = fmt.i_width;
296
297     p_spu_region = p_subpic->p_region = subpicture_region_New( &fmt );
298
299     if( p_spu_region )
300     {
301         struct csri_frame csri_frame;
302
303         /* */
304         p_spu_region->i_align = SUBPICTURE_ALIGN_TOP | SUBPICTURE_ALIGN_LEFT;
305         memset( p_spu_region->p_picture->Y_PIXELS, 0x00, p_spu_region->p_picture->Y_PITCH * p_sys->fmt_cached.i_height );
306
307         /* */
308         const mtime_t i_stream_date = p_subpic->p_sys->i_pts + (i_ts - p_subpic->i_start);
309
310         //msg_Dbg( p_dec, "TS %lf", ts * 0.000001 );
311         memset( &csri_frame, 0, sizeof(csri_frame) );
312         csri_frame.pixfmt = CSRI_F_BGRA;
313         csri_frame.planes[0] = (unsigned char*)p_spu_region->p_picture->Y_PIXELS;
314         csri_frame.strides[0] = p_spu_region->p_picture->Y_PITCH;
315         csri_render( p_sys->p_instance, &csri_frame, i_stream_date * 0.000001 );
316     }
317 }
318