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