da7e264df03928afd0c4e5b7cdf26aaa2c1730be
[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     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     if( p_stream->p_sys->p_mask )
258         p_stream->p_sys->p_mask->pf_release( p_stream->p_sys->p_mask );
259
260     free( p_sys );
261 }
262
263 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
264 {
265     sout_stream_sys_t *p_sys = p_stream->p_sys;
266     bridge_t *p_bridge;
267     bridged_es_t *p_es;
268     int i;
269
270     if ( p_sys->b_inited )
271     {
272         return NULL;
273     }
274
275     /* Create decoder object */
276     p_sys->p_decoder = vlc_object_create( p_stream, VLC_OBJECT_DECODER );
277     vlc_object_attach( p_sys->p_decoder, p_stream );
278     p_sys->p_decoder->p_module = NULL;
279     p_sys->p_decoder->fmt_in = *p_fmt;
280     p_sys->p_decoder->b_pace_control = VLC_FALSE;
281     p_sys->p_decoder->fmt_out = p_sys->p_decoder->fmt_in;
282     p_sys->p_decoder->fmt_out.i_extra = 0;
283     p_sys->p_decoder->fmt_out.p_extra = 0;
284     p_sys->p_decoder->pf_decode_video = 0;
285     p_sys->p_decoder->pf_vout_buffer_new = video_new_buffer;
286     p_sys->p_decoder->pf_vout_buffer_del = video_del_buffer;
287     p_sys->p_decoder->pf_picture_link    = video_link_picture_decoder;
288     p_sys->p_decoder->pf_picture_unlink  = video_unlink_picture_decoder;
289     p_sys->p_decoder->p_owner = malloc( sizeof(decoder_owner_sys_t) );
290     for( i = 0; i < PICTURE_RING_SIZE; i++ )
291         p_sys->p_decoder->p_owner->pp_pics[i] = 0;
292     p_sys->p_decoder->p_owner->video = p_fmt->video;
293     //p_sys->p_decoder->p_cfg = p_sys->p_video_cfg;
294
295     p_sys->p_decoder->p_module =
296         module_Need( p_sys->p_decoder, "decoder", "$codec", 0 );
297
298     if( !p_sys->p_decoder->p_module )
299     {
300         msg_Err( p_stream, "cannot find decoder" );
301         vlc_object_detach( p_sys->p_decoder );
302         vlc_object_destroy( p_sys->p_decoder );
303         return NULL;
304     }
305
306     p_sys->b_inited = VLC_TRUE;
307     vlc_mutex_lock( p_sys->p_lock );
308
309     p_bridge = GetBridge( p_stream );
310     if ( p_bridge == NULL )
311     {
312         libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
313         vlc_value_t val;
314
315         p_bridge = malloc( sizeof( bridge_t ) );
316
317         var_Create( p_libvlc_global, "mosaic-struct", VLC_VAR_ADDRESS );
318         val.p_address = p_bridge;
319         var_Set( p_libvlc_global, "mosaic-struct", val );
320
321         p_bridge->i_es_num = 0;
322         p_bridge->pp_es = NULL;
323     }
324
325     for ( i = 0; i < p_bridge->i_es_num; i++ )
326     {
327         if ( p_bridge->pp_es[i]->b_empty )
328             break;
329     }
330
331     if ( i == p_bridge->i_es_num )
332     {
333         p_bridge->pp_es = realloc( p_bridge->pp_es,
334                                    (p_bridge->i_es_num + 1)
335                                      * sizeof(bridged_es_t *) );
336         p_bridge->i_es_num++;
337         p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
338     }
339
340     p_sys->p_es = p_es = p_bridge->pp_es[i];
341
342     //p_es->fmt = *p_fmt;
343     p_es->psz_id = p_sys->psz_id;
344     p_es->p_picture = NULL;
345     p_es->pp_last = &p_es->p_picture;
346     p_es->b_empty = VLC_FALSE;
347
348     vlc_mutex_unlock( p_sys->p_lock );
349
350     if ( p_sys->i_height || p_sys->i_width )
351     {
352         p_sys->p_image = image_HandlerCreate( p_stream );
353     }
354
355     msg_Dbg( p_stream, "mosaic bridge id=%s pos=%d", p_es->psz_id, i );
356
357     return (sout_stream_id_t *)p_sys;
358 }
359
360 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
361 {
362     sout_stream_sys_t *p_sys = p_stream->p_sys;
363     bridge_t *p_bridge;
364     bridged_es_t *p_es;
365     vlc_bool_t b_last_es = VLC_TRUE;
366     int i;
367
368     if ( !p_sys->b_inited )
369     {
370         return VLC_SUCCESS;
371     }
372
373     if ( p_sys->p_decoder != NULL )
374     {
375         picture_t **pp_ring = p_sys->p_decoder->p_owner->pp_pics;
376
377         if( p_sys->p_decoder->p_module )
378             module_Unneed( p_sys->p_decoder, p_sys->p_decoder->p_module );
379         vlc_object_detach( p_sys->p_decoder );
380         vlc_object_destroy( p_sys->p_decoder );
381
382         for( i = 0; i < PICTURE_RING_SIZE; i++ )
383         {
384             if ( pp_ring[i] != NULL )
385             {
386                 if ( pp_ring[i]->p_data_orig != NULL )
387                     free( pp_ring[i]->p_data_orig );
388                 free( pp_ring[i]->p_sys );
389                 free( pp_ring[i] );
390             }
391         }
392     }
393
394     vlc_mutex_lock( p_sys->p_lock );
395
396     p_bridge = GetBridge( p_stream );
397     p_es = p_sys->p_es;
398
399     p_es->b_empty = VLC_TRUE;
400     while ( p_es->p_picture )
401     {
402         picture_t *p_next = p_es->p_picture->p_next;
403         p_es->p_picture->pf_release( p_es->p_picture );
404         p_es->p_picture = p_next;
405     }
406
407     for ( i = 0; i < p_bridge->i_es_num; i++ )
408     {
409         if ( !p_bridge->pp_es[i]->b_empty )
410         {
411             b_last_es = VLC_FALSE;
412             break;
413         }
414     }
415
416     if ( b_last_es )
417     {
418         libvlc_global_data_t *p_libvlc_global = p_stream->p_libvlc_global;
419         for ( i = 0; i < p_bridge->i_es_num; i++ )
420             free( p_bridge->pp_es[i] );
421         free( p_bridge->pp_es );
422         free( p_bridge );
423         var_Destroy( p_libvlc_global, "mosaic-struct" );
424     }
425
426     vlc_mutex_unlock( p_sys->p_lock );
427
428     if ( p_sys->i_height || p_sys->i_width )
429     {
430         image_HandlerDelete( p_sys->p_image );
431     }
432
433     p_sys->b_inited = VLC_FALSE;
434
435     return VLC_SUCCESS;
436 }
437
438 /*****************************************************************************
439  * PushPicture : push a picture in the mosaic-struct structure
440  *****************************************************************************/
441 static void PushPicture( sout_stream_t *p_stream, picture_t *p_picture )
442 {
443     sout_stream_sys_t *p_sys = p_stream->p_sys;
444     bridged_es_t *p_es = p_sys->p_es;
445
446     vlc_mutex_lock( p_sys->p_lock );
447
448     *p_es->pp_last = p_picture;
449     p_picture->p_next = NULL;
450     p_es->pp_last = &p_picture->p_next;
451
452     vlc_mutex_unlock( p_sys->p_lock );
453 }
454
455 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
456                  block_t *p_buffer )
457 {
458     sout_stream_sys_t *p_sys = p_stream->p_sys;
459     picture_t *p_pic;
460
461     if ( (sout_stream_sys_t *)id != p_sys )
462     {
463         block_ChainRelease( p_buffer );
464         return VLC_SUCCESS;
465     }
466
467     while ( (p_pic = p_sys->p_decoder->pf_decode_video( p_sys->p_decoder,
468                                                         &p_buffer )) )
469     {
470         picture_t *p_new_pic;
471
472         if( p_sys->i_height || p_sys->i_width )
473         {
474             video_format_t fmt_out, fmt_in;
475
476             memset( &fmt_in, 0, sizeof(video_format_t) );
477             memset( &fmt_out, 0, sizeof(video_format_t) );
478             fmt_in = p_sys->p_decoder->fmt_out.video;
479
480             if( p_sys->p_mask )
481                 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
482             else
483                 fmt_out.i_chroma = VLC_FOURCC('I','4','2','0');
484
485             if ( !p_sys->i_height )
486             {
487                 fmt_out.i_width = p_sys->i_width;
488                 fmt_out.i_height = (p_sys->i_width * VOUT_ASPECT_FACTOR
489                     * p_sys->i_sar_num / p_sys->i_sar_den / fmt_in.i_aspect)
490                       & ~0x1;
491             }
492             else if ( !p_sys->i_width )
493             {
494                 fmt_out.i_height = p_sys->i_height;
495                 fmt_out.i_width = (p_sys->i_height * fmt_in.i_aspect
496                     * p_sys->i_sar_den / p_sys->i_sar_num / VOUT_ASPECT_FACTOR)
497                       & ~0x1;
498             }
499             else
500             {
501                 fmt_out.i_width = p_sys->i_width;
502                 fmt_out.i_height = p_sys->i_height;
503             }
504             fmt_out.i_visible_width = fmt_out.i_width;
505             fmt_out.i_visible_height = fmt_out.i_height;
506
507             p_new_pic = image_Convert( p_sys->p_image,
508                                        p_pic, &fmt_in, &fmt_out );
509             if ( p_new_pic == NULL )
510             {
511                 msg_Err( p_stream, "image conversion failed" );
512                 continue;
513             }
514
515             if( p_sys->p_mask )
516             {
517                 plane_t *p_mask = p_sys->p_mask->p+A_PLANE;
518                 plane_t *p_apic = p_new_pic->p+A_PLANE;
519                 if(    p_mask->i_visible_pitch
520                     != p_apic->i_visible_pitch
521                     || p_mask->i_visible_lines
522                     != p_apic->i_visible_lines )
523                 {
524                     msg_Warn( p_stream,
525                               "Mask size (%d x %d) and image size (%d x %d) "
526                               "don't match. The mask will not be applied.",
527                               p_mask->i_visible_pitch,
528                               p_mask->i_visible_lines,
529                               p_apic->i_visible_pitch,
530                               p_apic->i_visible_lines );
531                 }
532                 else
533                 {
534                     if( p_mask->i_pitch != p_apic->i_pitch
535                     ||  p_mask->i_lines != p_apic->i_lines )
536                     {
537                         /* visible plane sizes match ... but not the undelying
538                          * buffer. I'm not sure that this can happen,
539                          * but better safe than sorry. */
540                         int i_line;
541                         int i_lines = p_mask->i_visible_lines;
542                         uint8_t *p_src = p_mask->p_pixels;
543                         uint8_t *p_dst = p_apic->p_pixels;
544                         int i_src_pitch = p_mask->i_pitch;
545                         int i_dst_pitch = p_apic->i_pitch;
546                         int i_visible_pitch = p_mask->i_visible_pitch;
547                         for( i_line = 0; i_line < i_lines; i_line++,
548                              p_src += i_src_pitch, p_dst += i_dst_pitch )
549                         {
550                             p_stream->p_libvlc->pf_memcpy(
551                                 p_dst, p_src, i_visible_pitch );
552                         }
553                     }
554                     else
555                     {
556                         /* plane sizes match */
557                         p_stream->p_libvlc->pf_memcpy(
558                             p_apic->p_pixels, p_mask->p_pixels,
559                             p_mask->i_pitch * p_mask->i_lines );
560                     }
561                 }
562             }
563         }
564         else
565         {
566             p_new_pic = (picture_t*)malloc( sizeof(picture_t) );
567             vout_AllocatePicture( p_stream, p_new_pic, p_pic->format.i_chroma,
568                                   p_pic->format.i_width, p_pic->format.i_height,
569                                   p_sys->p_decoder->fmt_out.video.i_aspect );
570
571             vout_CopyPicture( p_stream, p_new_pic, p_pic );
572         }
573
574         p_new_pic->i_refcount = 1;
575         p_new_pic->i_status = DESTROYED_PICTURE;
576         p_new_pic->i_type   = DIRECT_PICTURE;
577         p_new_pic->p_sys = (picture_sys_t *)p_new_pic->pf_release;
578         p_new_pic->pf_release = ReleasePicture;
579         p_new_pic->date = p_pic->date;
580
581         p_pic->pf_release( p_pic );
582         PushPicture( p_stream, p_new_pic );
583     }
584
585     return VLC_SUCCESS;
586 }
587
588 struct picture_sys_t
589 {
590     vlc_object_t *p_owner;
591     vlc_bool_t b_dead;
592 };
593
594 static void video_release_buffer( picture_t *p_pic )
595 {
596     if( p_pic && !p_pic->i_refcount && p_pic->pf_release && p_pic->p_sys )
597     {
598         video_del_buffer( (decoder_t *)p_pic->p_sys->p_owner, p_pic );
599     }
600     else if( p_pic && p_pic->i_refcount > 0 ) p_pic->i_refcount--;
601 }
602
603 static picture_t *video_new_buffer( decoder_t *p_dec )
604 {
605     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
606     picture_t **pp_ring = p_dec->p_owner->pp_pics;
607     picture_t *p_pic;
608     int i;
609
610     if( p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
611         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
612         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
613         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
614     {
615         if( !p_dec->fmt_out.video.i_sar_num ||
616             !p_dec->fmt_out.video.i_sar_den )
617         {
618             p_dec->fmt_out.video.i_sar_num =
619               p_dec->fmt_out.video.i_aspect * p_dec->fmt_out.video.i_height;
620
621             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
622               p_dec->fmt_out.video.i_width;
623         }
624
625         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
626                      &p_dec->fmt_out.video.i_sar_den,
627                      p_dec->fmt_out.video.i_sar_num,
628                      p_dec->fmt_out.video.i_sar_den, 0 );
629
630         if( !p_dec->fmt_out.video.i_visible_width ||
631             !p_dec->fmt_out.video.i_visible_height )
632         {
633             p_dec->fmt_out.video.i_visible_width =
634                 p_dec->fmt_out.video.i_width;
635             p_dec->fmt_out.video.i_visible_height =
636                 p_dec->fmt_out.video.i_height;
637         }
638
639         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
640         p_sys->video = p_dec->fmt_out.video;
641
642         for( i = 0; i < PICTURE_RING_SIZE; i++ )
643         {
644             if ( pp_ring[i] != NULL )
645             {
646                 if ( pp_ring[i]->i_status == DESTROYED_PICTURE )
647                 {
648                     if ( pp_ring[i]->p_data_orig != NULL )
649                         free( pp_ring[i]->p_data_orig );
650                     free( pp_ring[i]->p_sys );
651                     free( pp_ring[i] );
652                 }
653                 else
654                 {
655                     pp_ring[i]->p_sys->b_dead = VLC_TRUE;
656                 }
657                 pp_ring[i] = NULL;
658             }
659         }
660     }
661
662     /* Find an empty space in the picture ring buffer */
663     for( i = 0; i < PICTURE_RING_SIZE; i++ )
664     {
665         if( pp_ring[i] != NULL && pp_ring[i]->i_status == DESTROYED_PICTURE )
666         {
667             pp_ring[i]->i_status = RESERVED_PICTURE;
668             return pp_ring[i];
669         }
670     }
671     for( i = 0; i < PICTURE_RING_SIZE; i++ )
672     {
673         if( pp_ring[i] == NULL ) break;
674     }
675
676     if( i == PICTURE_RING_SIZE )
677     {
678         msg_Err( p_dec, "decoder/filter is leaking pictures, "
679                  "resetting its ring buffer" );
680
681         for( i = 0; i < PICTURE_RING_SIZE; i++ )
682         {
683             pp_ring[i]->pf_release( pp_ring[i] );
684         }
685
686         i = 0;
687     }
688
689     p_pic = malloc( sizeof(picture_t) );
690     p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
691     vout_AllocatePicture( VLC_OBJECT(p_dec), p_pic,
692                           p_dec->fmt_out.video.i_chroma,
693                           p_dec->fmt_out.video.i_width,
694                           p_dec->fmt_out.video.i_height,
695                           p_dec->fmt_out.video.i_aspect );
696
697     if( !p_pic->i_planes )
698     {
699         free( p_pic );
700         return NULL;
701     }
702
703     p_pic->pf_release = video_release_buffer;
704     p_pic->p_sys = malloc( sizeof(picture_sys_t) );
705     p_pic->p_sys->p_owner = VLC_OBJECT(p_dec);
706     p_pic->p_sys->b_dead = VLC_FALSE;
707     p_pic->i_status = RESERVED_PICTURE;
708
709     pp_ring[i] = p_pic;
710
711     return p_pic;
712 }
713
714 static void video_del_buffer( decoder_t *p_this, picture_t *p_pic )
715 {
716     p_pic->i_refcount = 0;
717     p_pic->i_status = DESTROYED_PICTURE;
718     if ( p_pic->p_sys->b_dead )
719     {
720         if ( p_pic->p_data_orig != NULL )
721             free( p_pic->p_data_orig );
722         free( p_pic->p_sys );
723         free( p_pic );
724     }
725 }
726
727 static void video_link_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
728 {
729     p_pic->i_refcount++;
730 }
731
732 static void video_unlink_picture_decoder( decoder_t *p_dec, picture_t *p_pic )
733 {
734     video_release_buffer( p_pic );
735 }
736