Use I420 image buffers instead of YUVA for mosaic bridge. Impact on CPU usage for...
[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_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 #if 0
442             fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
443 #else
444             fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
445 #endif
446
447             if ( !p_sys->i_height )
448             {
449                 fmt_out.i_width = p_sys->i_width;
450                 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
451                     * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
452                       & ~0x1;
453             }
454             else if ( !p_sys->i_width )
455             {
456                 fmt_out.i_height = p_sys->i_height;
457                 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
458                     * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
459                       & ~0x1;
460             }
461             else
462             {
463                 fmt_out.i_width = p_sys->i_width;
464                 fmt_out.i_height = p_sys->i_height;
465             }
466             fmt_out.i_visible_width = fmt_out.i_width;
467             fmt_out.i_visible_height = fmt_out.i_height;
468
469             p_new_pic = image_Convert( p_sys->p_image,
470                                        p_pic, &fmt_in, &fmt_out );
471             if ( p_new_pic == NULL )
472             {
473                 msg_Err( p_stream, "image conversion failed" );
474                 continue;
475             }
476         }
477         else
478         {
479             p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
480             vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
481                                   p_pic->format.i_width, p_pic->format.i_height,
482                                   p_sys->p_decoder->fmt_out.video.i_aspect );
483
484             vout_CopyPicture( p_stream, p_new_pic, p_pic );
485         }
486
487         p_new_pic->i_refcount = 1;
488         p_new_pic->i_status = DESTROYED_PICTURE;
489         p_new_pic->i_type   = DIRECT_PICTURE;
490         p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
491         p_new_pic->pf_release = ReleasePicture;
492         p_new_pic->date = p_pic->date;
493
494         p_pic->pf_release( p_pic );
495         PushPicture( p_stream, p_new_pic );
496     }
497
498     return VLC_SUCCESS;
499 }
500
501 struct picture_sys_t
502 {
503     vlc_object_t *p_owner;
504     vlc_bool_t b_dead;
505 };
506
507 static void video_release_buffer( picture_t *p_pic )
508 {
509     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
510     {
511         video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
512     }
513     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
514 }
515
516 static picture_t *video_new_buffer( decoder_t *p_dec )
517 {
518     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
519     picture_t **pp_ring = p_dec->p_owner->pp_pics;
520     picture_t *p_pic;
521     int i;
522
523     if( p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
524         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
525         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
526         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
527     {
528         if( !p_dec->fmt_out.video.i_sar_num ||
529             !p_dec->fmt_out.video.i_sar_den )
530         {
531             p_dec->fmt_out.video.i_sar_num =
532               p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
533
534             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
535               p_dec->fmt_out.video.i_width;
536         }
537
538         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
539                      &p_dec->fmt_out.video.i_sar_den,
540                      p_dec->fmt_out.video.i_sar_num,
541                      p_dec->fmt_out.video.i_sar_den, 0 );
542
543         if( !p_dec->fmt_out.video.i_visible_width ||
544             !p_dec->fmt_out.video.i_visible_height )
545         {
546             p_dec->fmt_out.video.i_visible_width =
547                 p_dec->fmt_out.video.i_width;
548             p_dec->fmt_out.video.i_visible_height =
549                 p_dec->fmt_out.video.i_height;
550         }
551
552         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
553         p_sys->video = p_dec->fmt_out.video;
554
555         for( i = 0; i < PICTURE_RING_SIZE; i++ )
556         {
557             if ( pp_ring[i] != NULL )
558             {
559                 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
560                 {
561                     if ( pp_ring[i]->p_data_orig != NULL )
562                         free( pp_ring[i]->p_data_orig );
563                     free( pp_ring[i]->p_sys );
564                     free( pp_ring[i] );
565                 }
566                 else
567                 {
568                     pp_ring[i]->p_sys->b_dead = VLC_TRUE;
569                 }
570                 pp_ring[i] = NULL;
571             }
572         }
573     }
574
575     /* Find an empty space in the picture ring buffer */
576     for( i = 0; i < PICTURE_RING_SIZE; i++ )
577     {
578         if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
579         {
580             pp_ring[i]->i_status = RESERVED_PICTURE;
581             return pp_ring[i];
582         }
583     }
584     for( i = 0; i < PICTURE_RING_SIZE; i++ )
585     {
586         if( pp_ring[i] == NULL ) break;
587     }
588
589     if( i == PICTURE_RING_SIZE )
590     {
591         msg_Err( p_dec, "decoder/filter is leaking pictures, "
592                  "resetting its ring buffer" );
593
594         for( i = 0; i < PICTURE_RING_SIZE; i++ )
595         {
596             pp_ring[i]->pf_release( pp_ring[i] );
597         }
598
599         i = 0;
600     }
601
602     p_pic = malloc( sizeof(picture_t) );
603     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
604     vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
605                           p_dec->fmt_out.video.i_chroma,
606                           p_dec->fmt_out.video.i_width,
607                           p_dec->fmt_out.video.i_height,
608                           p_dec->fmt_out.video.i_aspect );
609
610     if( !p_pic->i_planes )
611     {
612         free( p_pic );
613         return NULL;
614     }
615
616     p_pic->pf_release = video_release_buffer;
617     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
618     p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
619     p_pic->p_sys->b_dead = VLC_FALSE;
620     p_pic->i_status = RESERVED_PICTURE;
621
622     pp_ring[i] = p_pic;
623
624     return p_pic;
625 }
626
627 static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
628 {
629     p_pic->i_refcount = 0;
630     p_pic->i_status = DESTROYED_PICTURE;
631     if ( p_pic->p_sys->b_dead )
632     {
633         if ( p_pic->p_data_orig != NULL )
634             free( p_pic->p_data_orig );
635         free( p_pic->p_sys );
636         free( p_pic );
637     }
638 }
639
640 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
641 {
642     p_pic->i_refcount++;
643 }
644
645 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
646 {
647     video_release_buffer( p_pic );
648 }
649