Rename all sout_Cfg* stuff to config_Chain* (as it isn't really sout specific)
[vlc.git] / modules / stream_out / mosaic_bridge.c
1 /*****************************************************************************
2  * mosaic_bridge.c:
3  *****************************************************************************
4  * Copyright (C) 2004-2005 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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     /* Current format in use by the output */
62     video_format_t video;
63 };
64
65 typedef void (* pf_release_t)( picture_t * );
66 static void ReleasePicture( picture_t *p_pic )
67 {
68     p_pic->i_refcount--;
69
70     if ( p_pic->i_refcount <= 0 )
71     {
72         if ( p_pic->p_sys != NULL )
73         {
74             pf_release_t pf_release = (pf_release_t)p_pic->p_sys;
75             p_pic->p_sys = NULL;
76             pf_release( p_pic );
77         }
78         else
79         {
80             if( p_pic && p_pic->p_data_orig ) free( p_pic->p_data_orig );
81             if( p_pic ) free( p_pic );
82         }
83     }
84 }
85
86 /*****************************************************************************
87  * Local prototypes
88  *****************************************************************************/
89 static int  Open    ( vlc_object_t * );
90 static void Close   ( vlc_object_t * );
91 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
92 static int               Del ( sout_stream_t *, sout_stream_id_t * );
93 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t * );
94
95 static void video_del_buffer( decoder_t *, picture_t * );
96 static picture_t *video_new_buffer( decoder_t * );
97 static void video_link_picture_decoder( decoder_t *, picture_t * );
98 static void video_unlink_picture_decoder( decoder_t *, picture_t * );
99
100 /*****************************************************************************
101  * Module descriptor
102  *****************************************************************************/
103 #define ID_TEXT N_("ID")
104 #define ID_LONGTEXT N_( \
105     "Specify an identifier string for this subpicture" )
106
107 #define WIDTH_TEXT N_("Video width")
108 #define WIDTH_LONGTEXT N_( \
109     "Output video width." )
110 #define HEIGHT_TEXT N_("Video height")
111 #define HEIGHT_LONGTEXT N_( \
112     "Output video height." )
113 #define RATIO_TEXT N_("Sample aspect ratio")
114 #define RATIO_LONGTEXT N_( \
115     "Sample aspect ratio of the destination (1:1, 3:4, 2:3)." )
116
117 #define SOUT_CFG_PREFIX "sout-mosaic-bridge-"
118
119 vlc_module_begin();
120     set_shortname( _( "Mosaic bridge" ) );
121     set_description(_("Mosaic bridge stream output") );
122     set_capability( "sout stream", 0 );
123     add_shortcut( "mosaic-bridge" );
124
125     add_string( SOUT_CFG_PREFIX "id", "Id", NULL, ID_TEXT, ID_LONGTEXT,
126                 VLC_FALSE );
127     add_integer( SOUT_CFG_PREFIX "width", 0, NULL, WIDTH_TEXT,
128                  WIDTH_LONGTEXT, VLC_TRUE );
129     add_integer( SOUT_CFG_PREFIX "height", 0, NULL, HEIGHT_TEXT,
130                  HEIGHT_LONGTEXT, VLC_TRUE );
131     add_string( SOUT_CFG_PREFIX "sar", "1:1", NULL, RATIO_TEXT,
132                 RATIO_LONGTEXT, VLC_FALSE );
133
134     set_callbacks( Open, Close );
135
136     var_Create( p_module->p_libvlc_global, "mosaic-lock", VLC_VAR_MUTEX );
137 vlc_module_end();
138
139 static const char *ppsz_sout_options[] = {
140     "id", "width", "height", "sar", NULL
141 };
142
143 /*****************************************************************************
144  * Open
145  *****************************************************************************/
146 static int Open( vlc_object_t *p_this )
147 {
148     sout_stream_t     *p_stream = (sout_stream_t *)p_this;
149     sout_stream_sys_t *p_sys;
150     libvlc_global_data_t *p_libvlc_global = p_this->p_libvlc_global;
151     vlc_value_t val;
152
153     config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
154                    p_stream->p_cfg );
155
156     p_sys          = malloc( sizeof( sout_stream_sys_t ) );
157     p_stream->p_sys = p_sys;
158     p_sys->b_inited = VLC_FALSE;
159
160     var_Get( p_libvlc_global, "mosaic-lock", &val );
161     p_sys->p_lock = val.p_address;
162
163     var_Get( p_stream, SOUT_CFG_PREFIX "id", &val );
164     p_sys->psz_id = val.psz_string;
165
166     var_Get( p_stream, SOUT_CFG_PREFIX "height", &val );
167     p_sys->i_height = val.i_int;
168
169     var_Get( p_stream, SOUT_CFG_PREFIX "width", &val );
170     p_sys->i_width = val.i_int;
171
172     var_Get( p_stream, SOUT_CFG_PREFIX "sar", &val );
173     if ( val.psz_string )
174     {
175         char *psz_parser = strchr( val.psz_string, ':' );
176
177         if( psz_parser )
178         {
179             *psz_parser++ = '\0';
180             p_sys->i_sar_num = atoi( val.psz_string );
181             p_sys->i_sar_den = atoi( psz_parser );
182             vlc_ureduce( &p_sys->i_sar_num, &p_sys->i_sar_den,
183                          p_sys->i_sar_num, p_sys->i_sar_den, 0 );
184         }
185         else
186         {
187             msg_Warn( p_stream, "bad aspect ratio %s", val.psz_string );
188             p_sys->i_sar_num = p_sys->i_sar_den = 1;
189         }
190
191         free( val.psz_string );
192     }
193     else
194     {
195         p_sys->i_sar_num = p_sys->i_sar_den = 1;
196     }
197
198     p_stream->pf_add    = Add;
199     p_stream->pf_del    = Del;
200     p_stream->pf_send   = Send;
201
202     p_stream->p_sout->i_out_pace_nocontrol++;
203
204     return VLC_SUCCESS;
205 }
206
207 /*****************************************************************************
208  * Close
209  *****************************************************************************/
210 static void Close( vlc_object_t * p_this )
211 {
212     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
213     sout_stream_sys_t *p_sys = p_stream->p_sys;
214
215     p_stream->p_sout->i_out_pace_nocontrol--;
216
217     if ( p_sys->psz_id )
218         free( p_sys->psz_id );
219
220     free( p_sys );
221 }
222
223 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
224 {
225     sout_stream_sys_t *p_sys = p_stream->p_sys;
226     bridge_t *p_bridge;
227     bridged_es_t *p_es;
228     int i;
229
230     if ( p_sys->b_inited )
231     {
232         return NULL;
233     }
234
235     /* Create decoder object */
236     p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
237     vlc_object_attach( p_sys->p_decoder, p_stream );
238     p_sys->p_decoder->p_module = NULL;
239     p_sys->p_decoder->fmt_in = *p_fmt;
240     p_sys->p_decoder->b_pace_control = VLC_FALSE;
241     p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
242     p_sys->p_decoder->fmt_out.i_extra = 0;
243     p_sys->p_decoder->fmt_out.p_extra = 0;
244     p_sys->p_decoder->pf_decode_video = 0;
245     p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer;
246     p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer;
247     p_sys->p_decoder->pf_picture_link    = video_link_picture_decoder;
248     p_sys->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
249     p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
250     for( i = 0; i < PICTURE_RING_SIZE; i++ )
251         p_sys->p_decoder->p_owner->pp_pics[i] = 0;
252     p_sys->p_decoder->p_owner->video = p_fmt->video;
253     //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
254
255     p_sys->p_decoder->p_module =
256         module_Need( p_sys->p_decoder, "decoder", "$codec", 0 );
257
258     if( !p_sys->p_decoder->p_module )
259     {
260         msg_Err( p_stream, "cannot find decoder" );
261         vlc_object_detach( p_sys->p_decoder );
262         vlc_object_destroy( p_sys->p_decoder );
263         return NULL;
264     }
265
266     p_sys->b_inited = VLC_TRUE;
267     vlc_mutex_lock( p_sys->p_lock );
268
269     p_bridge = GetBridge( p_stream );
270     if ( p_bridge == NULL )
271     {
272         libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
273         vlc_value_t val;
274
275         p_bridge = malloc( sizeof( bridge_t ) );
276
277         var_Create( p_libvlc_global, "mosaic-struct", VLC_VAR_ADDRESS );
278         val.p_address = p_bridge;
279         var_Set( p_libvlc_global, "mosaic-struct", val );
280
281         p_bridge->i_es_num = 0;
282         p_bridge->pp_es = NULL;
283     }
284
285     for ( i = 0; i < p_bridge->i_es_num; i++ )
286     {
287         if ( p_bridge->pp_es[i]->b_empty )
288             break;
289     }
290
291     if ( i == p_bridge->i_es_num )
292     {
293         p_bridge->pp_es = realloc( p_bridge->pp_es,
294                                    (p_bridge->i_es_num + 1)
295                                      * sizeof(bridged_es_t *) );
296         p_bridge->i_es_num++;
297         p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
298     }
299
300     p_sys->p_es = p_es = p_bridge->pp_es[i];
301
302     //p_es->fmt = *p_fmt;
303     p_es->psz_id = p_sys->psz_id;
304     p_es->p_picture = NULL;
305     p_es->pp_last = &p_es->p_picture;
306     p_es->b_empty = VLC_FALSE;
307
308     vlc_mutex_unlock( p_sys->p_lock );
309
310     if ( p_sys->i_height || p_sys->i_width )
311     {
312         p_sys->p_image = image_HandlerCreate( p_stream );
313     }
314
315     msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
316
317     return (sout_stream_id_t *)p_sys;
318 }
319
320 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
321 {
322     sout_stream_sys_t *p_sys = p_stream->p_sys;
323     bridge_t *p_bridge;
324     bridged_es_t *p_es;
325     vlc_bool_t b_last_es = VLC_TRUE;
326     int i;
327
328     if ( !p_sys->b_inited )
329     {
330         return VLC_SUCCESS;
331     }
332
333     if ( p_sys->p_decoder != NULL )
334     {
335         picture_t **pp_ring = p_sys->p_decoder->p_owner->pp_pics;
336
337         if( p_sys->p_decoder->p_module )
338             module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
339         vlc_object_detach( p_sys->p_decoder );
340         vlc_object_destroy( p_sys->p_decoder );
341
342         for( i = 0; i < PICTURE_RING_SIZE; i++ )
343         {
344             if ( pp_ring[i] != NULL )
345             {
346                 if ( pp_ring[i]->p_data_orig != NULL )
347                     free( pp_ring[i]->p_data_orig );
348                 free( pp_ring[i]->p_sys );
349                 free( pp_ring[i] );
350             }
351         }
352     }
353
354     vlc_mutex_lock( p_sys->p_lock );
355
356     p_bridge = GetBridge( p_stream );
357     p_es = p_sys->p_es;
358
359     p_es->b_empty = VLC_TRUE;
360     while ( p_es->p_picture )
361     {
362         picture_t *p_next = p_es->p_picture->p_next;
363         p_es->p_picture->pf_release( p_es->p_picture );
364         p_es->p_picture = p_next;
365     }
366
367     for ( i = 0; i < p_bridge->i_es_num; i++ )
368     {
369         if ( !p_bridge->pp_es[i]->b_empty )
370         {
371             b_last_es = VLC_FALSE;
372             break;
373         }
374     }
375
376     if ( b_last_es )
377     {
378         libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
379         for ( i = 0; i < p_bridge->i_es_num; i++ )
380             free( p_bridge->pp_es[i] );
381         free( p_bridge->pp_es );
382         free( p_bridge );
383         var_Destroy( p_libvlc_global, "mosaic-struct" );
384     }
385
386     vlc_mutex_unlock( p_sys->p_lock );
387
388     if ( p_sys->i_height || p_sys->i_width )
389     {
390         image_HandlerDelete( p_sys->p_image );
391     }
392
393     p_sys->b_inited = VLC_FALSE;
394
395     return VLC_SUCCESS;
396 }
397
398 /*****************************************************************************
399  * PushPicture : push a picture in the mosaic-struct structure
400  *****************************************************************************/
401 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
402 {
403     sout_stream_sys_t *p_sys = p_stream->p_sys;
404     bridged_es_t *p_es = p_sys->p_es;
405
406     vlc_mutex_lock( p_sys->p_lock );
407
408     *p_es->pp_last = p_picture;
409     p_picture->p_next = NULL;
410     p_es->pp_last = &p_picture->p_next;
411
412     vlc_mutex_unlock( p_sys->p_lock );
413 }
414
415 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
416                  block_t *p_buffer )
417 {
418     sout_stream_sys_t *p_sys = p_stream->p_sys;
419     picture_t *p_pic;
420
421     if ( (sout_stream_sys_t *)id != p_sys )
422     {
423         block_ChainRelease( p_buffer );
424         return VLC_SUCCESS;
425     }
426
427     while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
428                                                         &p_buffer )) )
429     {
430         picture_t *p_new_pic;
431
432         if ( p_sys->i_height || p_sys->i_width )
433         {
434             video_format_t fmt_out, fmt_in;
435
436             memset( &fmt_in, 0, sizeof(video_format_t) );
437             memset( &fmt_out, 0, sizeof(video_format_t) );
438             fmt_in = p_sys->p_decoder->fmt_out.video;
439
440             fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
441
442             if ( !p_sys->i_height )
443             {
444                 fmt_out.i_width = p_sys->i_width;
445                 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
446                     * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
447                       & ~0x1;
448             }
449             else if ( !p_sys->i_width )
450             {
451                 fmt_out.i_height = p_sys->i_height;
452                 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
453                     * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
454                       & ~0x1;
455             }
456             else
457             {
458                 fmt_out.i_width = p_sys->i_width;
459                 fmt_out.i_height = p_sys->i_height;
460             }
461             fmt_out.i_visible_width = fmt_out.i_width;
462             fmt_out.i_visible_height = fmt_out.i_height;
463
464             p_new_pic = image_Convert( p_sys->p_image,
465                                        p_pic, &fmt_in, &fmt_out );
466             if ( p_new_pic == NULL )
467             {
468                 msg_Err( p_stream, "image conversion failed" );
469                 continue;
470             }
471         }
472         else
473         {
474             p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
475             vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
476                                   p_pic->format.i_width, p_pic->format.i_height,
477                                   p_sys->p_decoder->fmt_out.video.i_aspect );
478
479             vout_CopyPicture( p_stream, p_new_pic, p_pic );
480         }
481
482         p_new_pic->i_refcount = 1;
483         p_new_pic->i_status = DESTROYED_PICTURE;
484         p_new_pic->i_type   = DIRECT_PICTURE;
485         p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
486         p_new_pic->pf_release = ReleasePicture;
487         p_new_pic->date = p_pic->date;
488
489         p_pic->pf_release( p_pic );
490         PushPicture( p_stream, p_new_pic );
491     }
492
493     return VLC_SUCCESS;
494 }
495
496 struct picture_sys_t
497 {
498     vlc_object_t *p_owner;
499     vlc_bool_t b_dead;
500 };
501
502 static void video_release_buffer( picture_t *p_pic )
503 {
504     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
505     {
506         video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
507     }
508     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
509 }
510
511 static picture_t *video_new_buffer( decoder_t *p_dec )
512 {
513     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
514     picture_t **pp_ring = p_dec->p_owner->pp_pics;
515     picture_t *p_pic;
516     int i;
517
518     if( p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
519         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
520         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
521         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
522     {
523         if( !p_dec->fmt_out.video.i_sar_num ||
524             !p_dec->fmt_out.video.i_sar_den )
525         {
526             p_dec->fmt_out.video.i_sar_num =
527               p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
528
529             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
530               p_dec->fmt_out.video.i_width;
531         }
532
533         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
534                      &p_dec->fmt_out.video.i_sar_den,
535                      p_dec->fmt_out.video.i_sar_num,
536                      p_dec->fmt_out.video.i_sar_den, 0 );
537
538         if( !p_dec->fmt_out.video.i_visible_width ||
539             !p_dec->fmt_out.video.i_visible_height )
540         {
541             p_dec->fmt_out.video.i_visible_width =
542                 p_dec->fmt_out.video.i_width;
543             p_dec->fmt_out.video.i_visible_height =
544                 p_dec->fmt_out.video.i_height;
545         }
546
547         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
548         p_sys->video = p_dec->fmt_out.video;
549
550         for( i = 0; i < PICTURE_RING_SIZE; i++ )
551         {
552             if ( pp_ring[i] != NULL )
553             {
554                 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
555                 {
556                     if ( pp_ring[i]->p_data_orig != NULL )
557                         free( pp_ring[i]->p_data_orig );
558                     free( pp_ring[i]->p_sys );
559                     free( pp_ring[i] );
560                 }
561                 else
562                 {
563                     pp_ring[i]->p_sys->b_dead = VLC_TRUE;
564                 }
565                 pp_ring[i] = NULL;
566             }
567         }
568     }
569
570     /* Find an empty space in the picture ring buffer */
571     for( i = 0; i < PICTURE_RING_SIZE; i++ )
572     {
573         if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
574         {
575             pp_ring[i]->i_status = RESERVED_PICTURE;
576             return pp_ring[i];
577         }
578     }
579     for( i = 0; i < PICTURE_RING_SIZE; i++ )
580     {
581         if( pp_ring[i] == NULL ) break;
582     }
583
584     if( i == PICTURE_RING_SIZE )
585     {
586         msg_Err( p_dec, "decoder/filter is leaking pictures, "
587                  "resetting its ring buffer" );
588
589         for( i = 0; i < PICTURE_RING_SIZE; i++ )
590         {
591             pp_ring[i]->pf_release( pp_ring[i] );
592         }
593
594         i = 0;
595     }
596
597     p_pic = malloc( sizeof(picture_t) );
598     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
599     vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
600                           p_dec->fmt_out.video.i_chroma,
601                           p_dec->fmt_out.video.i_width,
602                           p_dec->fmt_out.video.i_height,
603                           p_dec->fmt_out.video.i_aspect );
604
605     if( !p_pic->i_planes )
606     {
607         free( p_pic );
608         return NULL;
609     }
610
611     p_pic->pf_release = video_release_buffer;
612     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
613     p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
614     p_pic->p_sys->b_dead = VLC_FALSE;
615     p_pic->i_status = RESERVED_PICTURE;
616
617     pp_ring[i] = p_pic;
618
619     return p_pic;
620 }
621
622 static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
623 {
624     p_pic->i_refcount = 0;
625     p_pic->i_status = DESTROYED_PICTURE;
626     if ( p_pic->p_sys->b_dead )
627     {
628         if ( p_pic->p_data_orig != NULL )
629             free( p_pic->p_data_orig );
630         free( p_pic->p_sys );
631         free( p_pic );
632     }
633 }
634
635 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
636 {
637     p_pic->i_refcount++;
638 }
639
640 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
641 {
642     video_release_buffer( p_pic );
643 }
644