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