]> git.sesse.net Git - vlc/blob - modules/stream_out/mosaic_bridge.c
Copyright fixes
[vlc] / modules / stream_out / mosaic_bridge.c
1 /*****************************************************************************
2  * mosaic_bridge.c:
3  *****************************************************************************
4  * Copyright (C) 2004-2005 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <stdlib.h>                                                /* free() */
30 #include <string.h>                                            /* strerror() */
31
32 #include <vlc/vlc.h>
33 #include <vlc/sout.h>
34 #include <vlc/decoder.h>
35
36 #include "vlc_image.h"
37
38 #include "../video_filter/mosaic.h"
39
40 /*****************************************************************************
41  * Local structures
42  *****************************************************************************/
43 struct sout_stream_sys_t
44 {
45     bridged_es_t *p_es;
46     vlc_mutex_t *p_lock;
47
48     decoder_t       *p_decoder;
49     image_handler_t *p_image; /* filter for resizing */
50     int i_height, i_width;
51     int i_sar_num, i_sar_den;
52     char *psz_id;
53     vlc_bool_t b_inited;
54 };
55
56 #define PICTURE_RING_SIZE 4
57 struct decoder_owner_sys_t
58 {
59     picture_t *pp_pics[PICTURE_RING_SIZE];
60 };
61
62 typedef void (* pf_release_t)( picture_t * );
63 static void ReleasePicture( picture_t *p_pic )
64 {
65     p_pic->i_refcount--;
66
67     if ( p_pic->i_refcount <= 0 )
68     {
69         if ( p_pic->p_sys != NULL )
70         {
71             pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
72             p_pic->p_sys = NULL;
73             pf_release( p_pic );
74         }
75         else
76         {
77             if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
78             if( p_pic ) free( p_pic );
79         }
80     }
81 }
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 static int  Open    ( vlc_object_t * );
87 static void Close   ( vlc_object_t * );
88 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
89 static int               Del ( sout_stream_t *, sout_stream_id_t * );
90 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t * );
91
92 static void video_del_buffer( decoder_t *, picture_t * );
93 static picture_t *video_new_buffer( decoder_t * );
94 static void video_link_picture_decoder( decoder_t *, picture_t * );
95 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
96
97 /*****************************************************************************
98  * Module descriptor
99  *****************************************************************************/
100 #define ID_TEXT N_("ID")
101 #define ID_LONGTEXT N_( \
102     "Specify an identifier string for this subpicture" )
103
104 #define WIDTH_TEXT N_("Video width")
105 #define WIDTH_LONGTEXT N_( \
106     "Allows you to specify the output video width." )
107 #define HEIGHT_TEXT N_("Video height")
108 #define HEIGHT_LONGTEXT N_( \
109     "Allows you to specify the output video height." )
110 #define RATIO_TEXT N_("Sample aspect ratio")
111 #define RATIO_LONGTEXT N_( \
112     "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
113
114 #define SOUT_CFG_PREFIX "sout-mosaic-bridge-"
115
116 vlc_module_begin();
117     set_shortname( _( "Mosaic bridge" ) );
118     set_description(_("Mosaic bridge stream output") );
119     set_capability( "sout stream", 0 );
120     add_shortcut( "mosaic-bridge" );
121
122     add_string( SOUT_CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
123                 VLC_FALSE );
124     add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
125                  WIDTH_LONGTEXT, VLC_TRUE );
126     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
127                  HEIGHT_LONGTEXT, VLC_TRUE );
128     add_string( SOUT_CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
129                 RATIO_LONGTEXT, VLC_FALSE );
130
131     set_callbacks( Open, Close );
132
133     var_Create( p_module->p_libvlc, "mosaic-lock", VLC_VAR_MUTEX );
134 vlc_module_end();
135
136 static const char *ppsz_sout_options[] = {
137     "id", "width", "height", "sar", NULL
138 };
139
140 /*****************************************************************************
141  * Open
142  *****************************************************************************/
143 static int Open( vlc_object_t *p_this )
144 {
145     sout_stream_t     *p_stream = (sout_stream_t *)p_this;
146     sout_stream_sys_t *p_sys;
147     libvlc_t *p_libvlc = p_this->p_libvlc;
148     vlc_value_t val;
149
150     sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
151                    p_stream->p_cfg );
152
153     p_sys          = malloc( sizeof( sout_stream_sys_t ) );
154     p_stream->p_sys = p_sys;
155     p_sys->b_inited = VLC_FALSE;
156
157     var_Get( p_libvlc, "mosaic-lock", &val );
158     p_sys->p_lock = val.p_address;
159
160     var_Get( p_stream, SOUT_CFG_PREFIX "id", &val );
161     p_sys->psz_id = val.psz_string;
162
163     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
164     p_sys->i_height = val.i_int; 
165
166     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
167     p_sys->i_width = val.i_int; 
168
169     var_Get( p_stream, SOUT_CFG_PREFIX "sar", &val );
170     if ( val.psz_string )
171     {
172         char *psz_parser = strchr( val.psz_string, ':' );
173
174         if( psz_parser )
175         {
176             *psz_parser++ = '\0';
177             p_sys->i_sar_num = atoi( val.psz_string );
178             p_sys->i_sar_den = atoi( psz_parser );
179             vlc_reduce( &p_sys->i_sar_num, &p_sys->i_sar_den,
180                         p_sys->i_sar_num, p_sys->i_sar_den, 0 );
181         }
182         else
183         {
184             msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
185             p_sys->i_sar_num = p_sys->i_sar_den = 1;
186         }
187
188         free( val.psz_string );
189     }
190     else
191     {
192         p_sys->i_sar_num = p_sys->i_sar_den = 1;
193     }
194
195     p_stream->pf_add    = Add;
196     p_stream->pf_del    = Del;
197     p_stream->pf_send   = Send;
198
199     p_stream->p_sout->i_out_pace_nocontrol++;
200
201     return VLC_SUCCESS;
202 }
203
204 /*****************************************************************************
205  * Close
206  *****************************************************************************/
207 static void Close( vlc_object_t * p_this )
208 {
209     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
210     sout_stream_sys_t *p_sys = p_stream->p_sys;
211
212     p_stream->p_sout->i_out_pace_nocontrol--;
213
214     if ( p_sys->psz_id )
215         free( p_sys->psz_id );
216
217     free( p_sys );
218 }
219
220 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
221 {
222     sout_stream_sys_t *p_sys = p_stream->p_sys;
223     bridge_t *p_bridge;
224     bridged_es_t *p_es;
225     int i;
226
227     if ( p_sys->b_inited )
228     {
229         return NULL;
230     }
231
232     /* Create decoder object */
233     p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
234     vlc_object_attach( p_sys->p_decoder, p_stream );
235     p_sys->p_decoder->p_module = NULL;
236     p_sys->p_decoder->fmt_in = *p_fmt;
237     p_sys->p_decoder->b_pace_control = VLC_FALSE;
238     p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
239     p_sys->p_decoder->fmt_out.i_extra = 0;
240     p_sys->p_decoder->fmt_out.p_extra = 0;
241     p_sys->p_decoder->pf_decode_video = 0;
242     p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer;
243     p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer;
244     p_sys->p_decoder->pf_picture_link    = video_link_picture_decoder;
245     p_sys->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
246     p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
247     for( i = 0; i < PICTURE_RING_SIZE; i++ )
248         p_sys->p_decoder->p_owner->pp_pics[i] = 0;
249     //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
250
251     p_sys->p_decoder->p_module =
252         module_Need( p_sys->p_decoder, "decoder", "$codec", 0 );
253
254     if( !p_sys->p_decoder->p_module )
255     {
256         msg_Err( p_stream, "cannot find decoder" );
257         vlc_object_detach( p_sys->p_decoder );
258         vlc_object_destroy( p_sys->p_decoder );
259         return NULL;
260     }
261
262     p_sys->b_inited = VLC_TRUE;
263     vlc_mutex_lock( p_sys->p_lock );
264
265     p_bridge = GetBridge( p_stream );
266     if ( p_bridge == NULL )
267     {
268         libvlc_t *p_libvlc = p_stream->p_libvlc;
269         vlc_value_t val;
270
271         p_bridge = malloc( sizeof( bridge_t ) );
272
273         var_Create( p_libvlc, "mosaic-struct", VLC_VAR_ADDRESS );
274         val.p_address = p_bridge;
275         var_Set( p_libvlc, "mosaic-struct", val );
276
277         p_bridge->i_es_num = 0;
278         p_bridge->pp_es = NULL;
279     }
280
281     for ( i = 0; i < p_bridge->i_es_num; i++ )
282     {
283         if ( p_bridge->pp_es[i]->b_empty )
284             break;
285     }
286
287     if ( i == p_bridge->i_es_num )
288     {
289         p_bridge->pp_es = realloc( p_bridge->pp_es,
290                                    (p_bridge->i_es_num + 1)
291                                      * sizeof(bridged_es_t *) );
292         p_bridge->i_es_num++;
293         p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
294     }
295
296     p_sys->p_es = p_es = p_bridge->pp_es[i];
297
298     //p_es->fmt = *p_fmt;
299     p_es->psz_id = p_sys->psz_id;
300     p_es->p_picture = NULL;
301     p_es->pp_last = &p_es->p_picture;
302     p_es->b_empty = VLC_FALSE;
303
304     vlc_mutex_unlock( p_sys->p_lock );
305
306     if ( p_sys->i_height || p_sys->i_width )
307     {
308         p_sys->p_image = image_HandlerCreate( p_stream );
309     }
310
311     msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
312
313     return (sout_stream_id_t *)p_sys;
314 }
315
316 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
317 {
318     sout_stream_sys_t *p_sys = p_stream->p_sys;
319     bridge_t *p_bridge;
320     bridged_es_t *p_es;
321     vlc_bool_t b_last_es = VLC_TRUE;
322     int i;
323
324     if ( !p_sys->b_inited )
325     {
326         return VLC_SUCCESS;
327     }
328
329     if ( p_sys->p_decoder )
330     {
331         if( p_sys->p_decoder->p_module )
332             module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
333         vlc_object_detach( p_sys->p_decoder );
334         vlc_object_destroy( p_sys->p_decoder );
335     }
336
337     vlc_mutex_lock( p_sys->p_lock );
338
339     p_bridge = GetBridge( p_stream );
340     p_es = p_sys->p_es;
341
342     p_es->b_empty = VLC_TRUE;
343     while ( p_es->p_picture )
344     {
345         picture_t *p_next = p_es->p_picture->p_next;
346         p_es->p_picture->pf_release( p_es->p_picture );
347         p_es->p_picture = p_next;
348     }
349
350     for ( i = 0; i < p_bridge->i_es_num; i++ )
351     {
352         if ( !p_bridge->pp_es[i]->b_empty )
353         {
354             b_last_es = VLC_FALSE;
355             break;
356         }
357     }
358
359     if ( b_last_es )
360     {
361         libvlc_t *p_libvlc = p_stream->p_libvlc;
362         for ( i = 0; i < p_bridge->i_es_num; i++ )
363             free( p_bridge->pp_es[i] );
364         free( p_bridge->pp_es );
365         free( p_bridge );
366         var_Destroy( p_libvlc, "mosaic-struct" );
367     }
368
369     vlc_mutex_unlock( p_sys->p_lock );
370
371     if ( p_sys->i_height || p_sys->i_width )
372     {
373         image_HandlerDelete( p_sys->p_image );
374     }
375
376     p_sys->b_inited = VLC_FALSE;
377
378     return VLC_SUCCESS;
379 }
380
381 /*****************************************************************************
382  * PushPicture : push a picture in the mosaic-struct structure
383  *****************************************************************************/
384 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
385 {
386     sout_stream_sys_t *p_sys = p_stream->p_sys;
387     bridged_es_t *p_es = p_sys->p_es;
388
389     vlc_mutex_lock( p_sys->p_lock );
390
391     *p_es->pp_last = p_picture;
392     p_picture->p_next = NULL;
393     p_es->pp_last = &p_picture->p_next;
394
395     vlc_mutex_unlock( p_sys->p_lock );
396 }
397
398 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
399                  block_t *p_buffer )
400 {
401     sout_stream_sys_t *p_sys = p_stream->p_sys;
402     picture_t *p_pic;
403
404     if ( (sout_stream_sys_t *)id != p_sys )
405     {
406         block_ChainRelease( p_buffer );
407         return VLC_SUCCESS;
408     }
409
410     while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
411                                                         &p_buffer )) )
412     {
413         picture_t *p_new_pic;
414
415         if ( p_sys->i_height || p_sys->i_width )
416         {
417             video_format_t fmt_out = {0}, fmt_in = {0};
418             fmt_in = p_sys->p_decoder->fmt_out.video;
419
420             fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
421
422             if ( !p_sys->i_height )
423             {
424                 fmt_out.i_width = p_sys->i_width;
425                 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
426                     * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
427                       & ~0x1;
428             }
429             else if ( !p_sys->i_width )
430             {
431                 fmt_out.i_height = p_sys->i_height;
432                 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
433                     * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
434                       & ~0x1;
435             }
436             else
437             {
438                 fmt_out.i_width = p_sys->i_width;
439                 fmt_out.i_height = p_sys->i_height;
440             }
441             fmt_out.i_visible_width = fmt_out.i_width;
442             fmt_out.i_visible_height = fmt_out.i_height;
443
444             p_new_pic = image_Convert( p_sys->p_image,
445                                        p_pic, &fmt_in, &fmt_out );
446             if ( p_new_pic == NULL )
447             {
448                 msg_Err( p_stream, "image conversion failed" );
449                 continue;
450             }
451         }
452         else
453         {
454             p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
455             vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
456                                   p_pic->format.i_width, p_pic->format.i_height,
457                                   p_sys->p_decoder->fmt_out.video.i_aspect );
458
459             vout_CopyPicture( p_stream, p_new_pic, p_pic );
460         }
461
462         p_new_pic->i_refcount = 1;
463         p_new_pic->i_status = DESTROYED_PICTURE;
464         p_new_pic->i_type   = DIRECT_PICTURE;
465         p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
466         p_new_pic->pf_release = ReleasePicture;
467         p_new_pic->date = p_pic->date;
468
469         p_pic->pf_release( p_pic );
470         PushPicture( p_stream, p_new_pic );
471     }
472
473     return VLC_SUCCESS;
474 }
475
476 struct picture_sys_t
477 {
478     vlc_object_t *p_owner;
479 };
480
481 static void video_release_buffer( picture_t *p_pic )
482 {
483     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
484     {
485         video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
486     }
487     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
488 }
489
490 static picture_t *video_new_buffer( decoder_t *p_dec )
491 {
492     picture_t **pp_ring = p_dec->p_owner->pp_pics;
493     picture_t *p_pic;
494     int i;
495
496     /* Find an empty space in the picture ring buffer */
497     for( i = 0; i < PICTURE_RING_SIZE; i++ )
498     {
499         if( pp_ring[i] != 0 && pp_ring[i]->i_status == DESTROYED_PICTURE )
500         {
501             pp_ring[i]->i_status = RESERVED_PICTURE;
502             return pp_ring[i];
503         }
504     }
505     for( i = 0; i < PICTURE_RING_SIZE; i++ )
506     {
507         if( pp_ring[i] == 0 ) break;
508     }
509
510     if( i == PICTURE_RING_SIZE )
511     {
512         msg_Err( p_dec, "decoder/filter is leaking pictures, "
513                  "resetting its ring buffer" );
514
515         for( i = 0; i < PICTURE_RING_SIZE; i++ )
516         {
517             pp_ring[i]->pf_release( pp_ring[i] );
518         }
519
520         i = 0;
521     }
522
523     p_pic = malloc( sizeof(picture_t) );
524     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
525     vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
526                           p_dec->fmt_out.video.i_chroma,
527                           p_dec->fmt_out.video.i_width,
528                           p_dec->fmt_out.video.i_height,
529                           p_dec->fmt_out.video.i_aspect );
530
531     if( !p_pic->i_planes )
532     {
533         free( p_pic );
534         return 0;
535     }
536
537     p_pic->pf_release = video_release_buffer;
538     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
539     p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
540     p_pic->i_status = RESERVED_PICTURE;
541
542     pp_ring[i] = p_pic;
543
544     return p_pic;
545 }
546
547 static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
548 {
549     p_pic->i_refcount = 0;
550     p_pic->i_status = DESTROYED_PICTURE;
551 }
552
553 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
554 {
555     p_pic->i_refcount++;
556 }
557
558 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
559 {
560     video_release_buffer( p_pic );
561 }
562