2b10010eea37a8a2f4f7af774ad3dbcef8b67bf1
[vlc.git] / src / input / input_dec.c
1 /*****************************************************************************
2  * input_dec.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: input_dec.c,v 1.79 2003/11/24 23:22:01 gbazin Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <string.h>                                    /* memcpy(), memset() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/decoder.h>
33 #include <vlc/vout.h>
34
35 #include "stream_output.h"
36
37 #include "input_ext-intf.h"
38 #include "input_ext-plugins.h"
39
40 #include "codecs.h"
41
42 static void input_NullPacket( input_thread_t *, es_descriptor_t * );
43
44 static decoder_t * CreateDecoder( input_thread_t *, es_descriptor_t *, int );
45 static int         DecoderThread( decoder_t * );
46 static void        DeleteDecoder( decoder_t * );
47
48 /* Buffers allocation callbacks for the decoders */
49 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
50 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
51
52 static picture_t *vout_new_buffer( decoder_t * );
53 static void vout_del_buffer( decoder_t *, picture_t * );
54 static void vout_link_picture( decoder_t *, picture_t * );
55 static void vout_unlink_picture( decoder_t *, picture_t * );
56
57 static es_format_t null_es_format = {0};
58
59 struct decoder_owner_sys_t
60 {
61     aout_instance_t *p_aout;
62     aout_input_t    *p_aout_input;
63
64     vout_thread_t   *p_vout;
65
66     sout_packetizer_input_t *p_sout;
67
68     /* Current format in use by the output */
69     video_format_t video;
70     audio_format_t audio;
71     es_format_t    sout;
72
73     /* fifo */
74     block_fifo_t *p_fifo;
75
76     /* */
77     input_buffers_t *p_method_data;
78 };
79
80
81 /*****************************************************************************
82  * input_RunDecoder: spawns a new decoder thread
83  *****************************************************************************/
84 decoder_t * input_RunDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
85 {
86     decoder_t      *p_dec = NULL;
87     vlc_value_t    val;
88     int            i_priority;
89
90     /* If we are in sout mode, search for packetizer module */
91     var_Get( p_input, "sout", &val );
92     if( !p_es->b_force_decoder && val.psz_string && *val.psz_string )
93     {
94         free( val.psz_string );
95         val.b_bool = VLC_TRUE;
96
97         if( p_es->i_cat == AUDIO_ES )
98         {
99             var_Get( p_input, "sout-audio", &val );
100         }
101         else if( p_es->i_cat == VIDEO_ES )
102         {
103             var_Get( p_input, "sout-video", &val );
104         }
105
106         if( val.b_bool )
107         {
108             /* Create the decoder configuration structure */
109             p_dec = CreateDecoder( p_input, p_es, VLC_OBJECT_PACKETIZER );
110             if( p_dec == NULL )
111             {
112                 msg_Err( p_input, "could not create packetizer" );
113                 return NULL;
114             }
115
116             p_dec->p_module =
117                 module_Need( p_dec, "packetizer", "$packetizer" );
118         }
119     }
120     else
121     {
122         /* Create the decoder configuration structure */
123         p_dec = CreateDecoder( p_input, p_es, VLC_OBJECT_DECODER );
124         if( p_dec == NULL )
125         {
126             msg_Err( p_input, "could not create decoder" );
127             return NULL;
128         }
129
130         /* default Get a suitable decoder module */
131         p_dec->p_module = module_Need( p_dec, "decoder", "$codec" );
132
133         if( val.psz_string ) free( val.psz_string );
134     }
135
136     if( !p_dec || !p_dec->p_module )
137     {
138         msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
139                  "VLC probably does not support this sound or video format.",
140                  (char*)&p_dec->fmt_in.i_codec );
141
142         DeleteDecoder( p_dec );
143         vlc_object_destroy( p_dec );
144         return NULL;
145     }
146
147     if ( p_es->i_cat == AUDIO_ES )
148     {
149         i_priority = VLC_THREAD_PRIORITY_AUDIO;
150     }
151     else
152     {
153         i_priority = VLC_THREAD_PRIORITY_VIDEO;
154     }
155
156     /* Spawn the decoder thread */
157     if( vlc_thread_create( p_dec, "decoder", DecoderThread,
158                            i_priority, VLC_FALSE ) )
159     {
160         msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
161                          p_dec->p_module->psz_object_name );
162         module_Unneed( p_dec, p_dec->p_module );
163         DeleteDecoder( p_dec );
164         vlc_object_destroy( p_dec );
165         return NULL;
166     }
167
168     p_input->stream.b_changed = 1;
169
170     return p_dec;
171 }
172
173 /*****************************************************************************
174  * input_EndDecoder: kills a decoder thread and waits until it's finished
175  *****************************************************************************/
176 void input_EndDecoder( input_thread_t * p_input, es_descriptor_t * p_es )
177 {
178     decoder_t *p_dec = p_es->p_dec;
179     int i_dummy;
180
181     p_dec->b_die = VLC_TRUE;
182
183     /* Make sure the thread leaves the NextDataPacket() function by
184      * sending it a few null packets. */
185     for( i_dummy = 0; i_dummy < PADDING_PACKET_NUMBER; i_dummy++ )
186     {
187         input_NullPacket( p_input, p_es );
188     }
189
190     if( p_es->p_pes != NULL )
191     {
192         input_DecodePES( p_es->p_dec, p_es->p_pes );
193     }
194
195     /* Waiting for the thread to exit */
196     /* I thought that unlocking was better since thread join can be long
197      * but it actually creates late pictures and freezes --stef */
198     /* vlc_mutex_unlock( &p_input->stream.stream_lock ); */
199     vlc_thread_join( p_dec );
200     /* vlc_mutex_lock( &p_input->stream.stream_lock ); */
201
202 #if 0
203     /* XXX We don't do it here because of dll loader that want close in the
204      * same thread than open/decode */
205     /* Unneed module */
206     module_Unneed( p_dec, p_dec->p_module );
207 #endif
208
209     /* Delete decoder configuration */
210     DeleteDecoder( p_dec );
211
212     /* Delete the decoder */
213     vlc_object_destroy( p_dec );
214
215     /* Tell the input there is no more decoder */
216     p_es->p_dec = NULL;
217
218     p_input->stream.b_changed = 1;
219 }
220
221 /*****************************************************************************
222  * input_DecodePES
223  *****************************************************************************
224  * Put a PES in the decoder's fifo.
225  *****************************************************************************/
226 void input_DecodePES( decoder_t * p_dec, pes_packet_t * p_pes )
227 {
228     data_packet_t *p_data;
229     int     i_size = 0;
230
231     for( p_data = p_pes->p_first; p_data != NULL; p_data = p_data->p_next )
232     {
233         i_size += p_data->p_payload_end - p_data->p_payload_start;
234     }
235     if( i_size > 0 )
236     {
237         block_t *p_block = block_New( p_dec, i_size );
238         if( p_block )
239         {
240             uint8_t *p_buffer = p_block->p_buffer;
241
242             for( p_data = p_pes->p_first; p_data != NULL; p_data = p_data->p_next )
243             {
244                 int i_copy = p_data->p_payload_end - p_data->p_payload_start;
245
246                 memcpy( p_buffer, p_data->p_payload_start, i_copy );
247
248                 p_buffer += i_copy;
249             }
250             p_block->i_pts = p_pes->i_pts;
251             p_block->i_dts = p_pes->i_dts;
252             p_block->b_discontinuity = p_pes->b_discontinuity;
253
254             block_FifoPut( p_dec->p_owner->p_fifo, p_block );
255         }
256     }
257
258     input_DeletePES( p_dec->p_owner->p_method_data, p_pes );
259 }
260 /*****************************************************************************
261  * input_DecodeBlock
262  *****************************************************************************
263  * Put a block_t in the decoder's fifo.
264  *****************************************************************************/
265 void input_DecodeBlock( decoder_t * p_dec, block_t *p_block )
266 {
267     block_FifoPut( p_dec->p_owner->p_fifo, p_block );
268 }
269
270 /*****************************************************************************
271  * Create a NULL packet for padding in case of a data loss
272  *****************************************************************************/
273 static void input_NullPacket( input_thread_t * p_input,
274                               es_descriptor_t * p_es )
275 {
276     block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
277
278     if( p_block )
279     {
280         memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
281         p_block->b_discontinuity = 1;
282
283         block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
284     }
285 }
286
287 /*****************************************************************************
288  * input_EscapeDiscontinuity: send a NULL packet to the decoders
289  *****************************************************************************/
290 void input_EscapeDiscontinuity( input_thread_t * p_input )
291 {
292     unsigned int i_es, i;
293
294     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
295     {
296         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
297
298         if( p_es->p_dec != NULL )
299         {
300             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
301             {
302                 input_NullPacket( p_input, p_es );
303             }
304         }
305     }
306 }
307
308 /*****************************************************************************
309  * input_EscapeAudioDiscontinuity: send a NULL packet to the audio decoders
310  *****************************************************************************/
311 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
312 {
313     unsigned int i_es, i;
314
315     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
316     {
317         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
318
319         if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
320         {
321             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
322             {
323                 input_NullPacket( p_input, p_es );
324             }
325         }
326     }
327 }
328
329 /*****************************************************************************
330  * CreateDecoder: create a decoder object
331  *****************************************************************************/
332 static decoder_t * CreateDecoder( input_thread_t * p_input,
333                                   es_descriptor_t * p_es, int i_object_type )
334 {
335     decoder_t *p_dec;
336
337     p_dec = vlc_object_create( p_input, i_object_type );
338     if( p_dec == NULL )
339     {
340         msg_Err( p_input, "out of memory" );
341         return NULL;
342     }
343
344     p_dec->pf_decode_audio = 0;
345     p_dec->pf_decode_video = 0;
346     p_dec->pf_decode_sub = 0;
347     p_dec->pf_packetize = 0;
348
349     /* Select a new ES */
350     INSERT_ELEM( p_input->stream.pp_selected_es,
351                  p_input->stream.i_selected_es_number,
352                  p_input->stream.i_selected_es_number,
353                  p_es );
354
355     /* Initialize the decoder fifo */
356     p_dec->p_module = NULL;
357
358     p_dec->fmt_in = p_es->fmt;
359
360     if( p_es->p_waveformatex )
361     {
362 #define p_wf ((WAVEFORMATEX *)p_es->p_waveformatex)
363         p_dec->fmt_in.audio.i_channels = p_wf->nChannels;
364         p_dec->fmt_in.audio.i_rate = p_wf->nSamplesPerSec;
365         p_dec->fmt_in.i_bitrate = p_wf->nAvgBytesPerSec * 8;
366         p_dec->fmt_in.audio.i_blockalign = p_wf->nBlockAlign;
367         p_dec->fmt_in.audio.i_bitspersample = p_wf->wBitsPerSample;
368         p_dec->fmt_in.i_extra = p_wf->cbSize;
369         p_dec->fmt_in.p_extra = NULL;
370         if( p_wf->cbSize )
371         {
372             p_dec->fmt_in.p_extra = malloc( p_wf->cbSize );
373             memcpy( p_dec->fmt_in.p_extra, &p_wf[1], p_wf->cbSize );
374         }
375     }
376
377     if( p_es->p_bitmapinfoheader )
378     {
379 #define p_bih ((BITMAPINFOHEADER *) p_es->p_bitmapinfoheader)
380         p_dec->fmt_in.i_extra = p_bih->biSize - sizeof(BITMAPINFOHEADER);
381         p_dec->fmt_in.p_extra = NULL;
382         if( p_dec->fmt_in.i_extra )
383         {
384             p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
385             memcpy( p_dec->fmt_in.p_extra, &p_bih[1], p_dec->fmt_in.i_extra );
386         }
387
388         p_dec->fmt_in.video.i_width = p_bih->biWidth;
389         p_dec->fmt_in.video.i_height = p_bih->biHeight;
390     }
391
392     /* FIXME
393      *  - 1: beurk
394      *  - 2: I'm not sure there isn't any endian problem here (spu)... */
395     if( p_es->i_cat == SPU_ES && p_es->p_demux_data )
396     {
397         if( ( p_es->i_fourcc == VLC_FOURCC( 's', 'p', 'u', ' ' ) ||
398               p_es->i_fourcc == VLC_FOURCC( 's', 'p', 'u', 'b' ) ) &&
399             *((uint32_t*)p_es->p_demux_data) == 0xBeef )
400         {
401             memcpy( p_dec->fmt_in.subs.spu.palette,
402                     p_es->p_demux_data, 17 * 4 );
403         }
404         else if( p_es->i_fourcc == VLC_FOURCC( 'd', 'v', 'b', 's' ) )
405         {
406             dvb_spuinfo_t *p_dvbs = (dvb_spuinfo_t*)p_es->p_demux_data;
407
408             p_dec->fmt_in.subs.dvb.i_id = p_dvbs->i_id;
409         }
410     }
411
412     p_dec->fmt_in.i_cat = p_es->i_cat;
413     p_dec->fmt_in.i_codec = p_es->i_fourcc;
414
415     p_dec->fmt_out = null_es_format;
416
417     /* Allocate our private structure for the decoder */
418     p_dec->p_owner = (decoder_owner_sys_t*)malloc(sizeof(decoder_owner_sys_t));
419     if( p_dec->p_owner == NULL )
420     {
421         msg_Err( p_dec, "out of memory" );
422         return NULL;
423     }
424     p_dec->p_owner->p_aout = NULL;
425     p_dec->p_owner->p_aout_input = NULL;
426     p_dec->p_owner->p_vout = NULL;
427     p_dec->p_owner->p_sout = NULL;
428     /* decoder fifo */
429     if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
430     {
431         msg_Err( p_dec, "out of memory" );
432         return NULL;
433     }
434     p_dec->p_owner->p_method_data = p_input->p_method_data;
435
436     /* Set buffers allocation callbacks for the decoders */
437     p_dec->pf_aout_buffer_new = aout_new_buffer;
438     p_dec->pf_aout_buffer_del = aout_del_buffer;
439     p_dec->pf_vout_buffer_new = vout_new_buffer;
440     p_dec->pf_vout_buffer_del = vout_del_buffer;
441     p_dec->pf_picture_link    = vout_link_picture;
442     p_dec->pf_picture_unlink  = vout_unlink_picture;
443
444     vlc_object_attach( p_dec, p_input );
445
446     return p_dec;
447 }
448
449 /*****************************************************************************
450  * DecoderThread: the decoding main loop
451  *****************************************************************************/
452 static int DecoderThread( decoder_t * p_dec )
453 {
454     block_t       *p_block;
455
456     /* The decoder's main loop */
457     while( !p_dec->b_die && !p_dec->b_error )
458     {
459         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
460         {
461             p_dec->b_error = 1;
462             break;
463         }
464
465         if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
466         {
467             block_t *p_sout_block;
468
469             while( (p_sout_block = p_dec->pf_packetize( p_dec, &p_block )) )
470             {
471                 if( !p_dec->p_owner->p_sout )
472                 {
473                     es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
474
475                     p_dec->p_owner->p_sout =
476                         sout_InputNew( p_dec, &p_dec->p_owner->sout );
477
478                     if( p_dec->p_owner->p_sout == NULL )
479                     {
480                         msg_Err( p_dec, "cannot create packetizer output" );
481                         p_dec->b_error = VLC_TRUE;
482
483                         while( p_sout_block )
484                         {
485                             block_t       *p_next = p_sout_block->p_next;
486                             block_Release( p_sout_block );
487                             p_sout_block = p_next;
488                         }
489                         break;
490                     }
491                 }
492
493                 while( p_sout_block )
494                 {
495                     block_t       *p_next = p_sout_block->p_next;
496                     sout_buffer_t *p_sout_buffer;
497
498                     p_sout_buffer =
499                         sout_BufferNew( p_dec->p_owner->p_sout->p_sout,
500                                         p_sout_block->i_buffer );
501                     if( p_sout_buffer == NULL )
502                     {
503                         msg_Err( p_dec, "cannot get sout buffer" );
504                         break;
505                     }
506
507                     memcpy( p_sout_buffer->p_buffer, p_sout_block->p_buffer,
508                             p_sout_block->i_buffer );
509
510                     p_sout_buffer->i_pts = p_sout_block->i_pts;
511                     p_sout_buffer->i_dts = p_sout_block->i_dts;
512                     p_sout_buffer->i_length = p_sout_block->i_length;
513
514                     block_Release( p_sout_block );
515
516                     sout_InputSendBuffer( p_dec->p_owner->p_sout, p_sout_buffer );
517
518                     p_sout_block = p_next;
519                 }
520             }
521         }
522         else if( p_dec->fmt_in.i_cat == AUDIO_ES )
523         {
524             aout_buffer_t *p_aout_buf;
525
526             while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
527             {
528                 aout_DecPlay( p_dec->p_owner->p_aout,
529                               p_dec->p_owner->p_aout_input, p_aout_buf );
530             }
531         }
532         else if( p_dec->fmt_in.i_cat == VIDEO_ES )
533         {
534             picture_t *p_pic;
535
536             while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
537             {
538                 vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
539                 vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
540             }
541         }
542         else if( p_dec->fmt_in.i_cat == SPU_ES )
543         {
544             p_dec->pf_decode_sub( p_dec, &p_block );
545         }
546         else
547         {
548             msg_Err( p_dec, "unknown ES format !!" );
549             p_dec->b_error = 1;
550             break;
551         }
552     }
553
554     while( !p_dec->b_die )
555     {
556         /* Trash all received PES packets */
557         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
558         if( p_block )
559         {
560             block_Release( p_block );
561         }
562     }
563
564     /* XXX We do it here because of dll loader that want close in the
565      * same thread than open/decode */
566     /* Unneed module */
567     module_Unneed( p_dec, p_dec->p_module );
568
569     return 0;
570 }
571
572 /*****************************************************************************
573  * DeleteDecoder: destroys a decoder object
574  *****************************************************************************/
575 static void DeleteDecoder( decoder_t * p_dec )
576 {
577     vlc_object_detach( p_dec );
578
579     msg_Dbg( p_dec,
580              "killing decoder fourcc `%4.4s', %d PES in FIFO",
581              (char*)&p_dec->fmt_in.i_codec,
582              p_dec->p_owner->p_fifo->i_depth );
583
584     /* Free all packets still in the decoder fifo. */
585     block_FifoEmpty( p_dec->p_owner->p_fifo );
586     block_FifoRelease( p_dec->p_owner->p_fifo );
587
588    /* Cleanup */
589     if( p_dec->p_owner->p_aout_input )
590         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
591
592     if( p_dec->p_owner->p_vout )
593     {
594         int i_pic;
595
596 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
597         /* Hack to make sure all the the pictures are freed by the decoder */
598         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
599              i_pic++ )
600         {
601             if( p_pic->i_status == RESERVED_PICTURE )
602                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
603             if( p_pic->i_refcount > 0 )
604                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
605         }
606 #undef p_pic
607
608         /* We are about to die. Reattach video output to p_vlc. */
609         vout_Request( p_dec, p_dec->p_owner->p_vout, 0, 0, 0, 0 );
610     }
611
612     if( p_dec->p_owner->p_sout )
613         sout_InputDelete( p_dec->p_owner->p_sout );
614
615     free( p_dec->p_owner );
616 }
617
618 /*****************************************************************************
619  * Buffers allocation callbacks for the decoders
620  *****************************************************************************/
621 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
622 {
623     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
624     aout_buffer_t *p_buffer;
625
626     if( p_sys->p_aout_input != NULL &&
627         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
628           p_dec->fmt_out.audio.i_original_channels !=
629               p_sys->audio.i_original_channels ||
630           p_dec->fmt_out.audio.i_bytes_per_frame !=
631               p_sys->audio.i_bytes_per_frame ) )
632     {
633         /* Parameters changed, restart the aout */
634         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
635         p_sys->p_aout_input = NULL;
636     }
637
638     if( p_sys->p_aout_input == NULL )
639     {
640         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
641         p_sys->audio = p_dec->fmt_out.audio;
642         p_sys->p_aout_input =
643             aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->audio );
644         if( p_sys->p_aout_input == NULL )
645         {
646             msg_Err( p_dec, "failed to create audio output" );
647             p_dec->b_error = VLC_TRUE;
648             return NULL;
649         }
650         p_dec->fmt_out.audio.i_bytes_per_frame =
651             p_sys->audio.i_bytes_per_frame;
652     }
653
654     p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
655                                   i_samples );
656
657     return p_buffer;
658 }
659
660 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
661 {
662     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
663                           p_dec->p_owner->p_aout_input, p_buffer );
664 }
665
666 static picture_t *vout_new_buffer( decoder_t *p_dec )
667 {
668     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
669     picture_t *p_pic;
670
671     if( p_sys->p_vout == NULL ||
672         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
673         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
674         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
675         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
676     {
677         if( !p_dec->fmt_out.video.i_width ||
678             !p_dec->fmt_out.video.i_height )
679         {
680             /* Can't create a new vout without display size */
681             return NULL;
682         }
683
684         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
685         p_sys->video = p_dec->fmt_out.video;
686
687         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
688                                       p_sys->video.i_width,
689                                       p_sys->video.i_height,
690                                       p_sys->video.i_chroma,
691                                       p_sys->video.i_aspect );
692
693         if( p_sys->p_vout == NULL )
694         {
695             msg_Err( p_dec, "failed to create video output" );
696             p_dec->b_error = VLC_TRUE;
697             return NULL;
698         }
699     }
700
701     /* Get a new picture */
702     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
703     {
704         int i_pic;
705
706         if( p_dec->b_die || p_dec->b_error )
707         {
708             return NULL;
709         }
710
711 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
712         /* Check the decoder doesn't leak pictures */
713         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
714              i_pic++ )
715         {
716             if( p_pic->i_status != DISPLAYED_PICTURE &&
717                 p_pic->i_status != RESERVED_PICTURE ) break;
718
719             if( !p_pic->i_refcount ) break;
720         }
721         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
722         {
723             msg_Err( p_dec, "decoder is leaking pictures, reseting the heap" );
724
725             /* Just free all the pictures */
726             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
727                  i_pic++ )
728             {
729                 if( p_pic->i_status == RESERVED_PICTURE )
730                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
731                 if( p_pic->i_refcount > 0 )
732                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
733             }
734         }
735 #undef p_pic
736
737         msleep( VOUT_OUTMEM_SLEEP );
738     }
739
740     return p_pic;
741 }
742
743 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
744 {
745     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
746 }
747
748 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
749 {
750     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
751 }
752
753 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
754 {
755     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
756 }