]> git.sesse.net Git - vlc/blob - src/input/decoder.c
* sub ->subtitle : converted sub.c to a 'real' demux2.
[vlc] / src / input / decoder.c
1 /*****************************************************************************
2  * decoder.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>
30 #include <vlc/vlc.h>
31
32 #include <vlc/decoder.h>
33 #include <vlc/vout.h>
34 #include <vlc/input.h>
35
36 #include "stream_output.h"
37 #include "input_internal.h"
38
39 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
40 static void        DeleteDecoder( decoder_t * );
41
42 static int         DecoderThread( decoder_t * );
43 static int         DecoderDecode( decoder_t * p_dec, block_t *p_block );
44
45 /* Buffers allocation callbacks for the decoders */
46 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
47 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
48
49 static picture_t *vout_new_buffer( decoder_t * );
50 static void vout_del_buffer( decoder_t *, picture_t * );
51 static void vout_link_picture( decoder_t *, picture_t * );
52 static void vout_unlink_picture( decoder_t *, picture_t * );
53
54 static es_format_t null_es_format = {0};
55
56 struct decoder_owner_sys_t
57 {
58     vlc_bool_t      b_own_thread;
59
60     input_thread_t  *p_input;
61
62     aout_instance_t *p_aout;
63     aout_input_t    *p_aout_input;
64
65     vout_thread_t   *p_vout;
66
67     sout_instance_t         *p_sout;
68     sout_packetizer_input_t *p_sout_input;
69
70     /* Some decoders require already packetized data (ie. not truncated) */
71     decoder_t *p_packetizer;
72
73     /* Current format in use by the output */
74     video_format_t video;
75     audio_format_t audio;
76     es_format_t    sout;
77
78     /* fifo */
79     block_fifo_t *p_fifo;
80 };
81
82
83 /**
84  * Spawns a new decoder thread
85  *
86  * \param p_input the input thread
87  * \param p_es the es descriptor
88  * \return the spawned decoder object
89  */
90 decoder_t *input_DecoderNew( input_thread_t *p_input,
91                              es_format_t *fmt, vlc_bool_t b_force_decoder )
92 {
93     decoder_t   *p_dec = NULL;
94     vlc_value_t val;
95
96     /* If we are in sout mode, search for packetizer module */
97     if( p_input->p_sout && !b_force_decoder )
98     {
99         /* Create the decoder configuration structure */
100         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
101         if( p_dec == NULL )
102         {
103             msg_Err( p_input, "could not create packetizer" );
104             return NULL;
105         }
106     }
107     else
108     {
109         /* Create the decoder configuration structure */
110         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
111         if( p_dec == NULL )
112         {
113             msg_Err( p_input, "could not create decoder" );
114             return NULL;
115         }
116     }
117
118     if( !p_dec->p_module )
119     {
120         msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
121                  "VLC probably does not support this sound or video format.",
122                  (char*)&p_dec->fmt_in.i_codec );
123
124         DeleteDecoder( p_dec );
125         vlc_object_destroy( p_dec );
126         return NULL;
127     }
128
129     if( p_input->p_sout && p_input->input.b_can_pace_control &&
130         !b_force_decoder )
131     {
132         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
133         p_dec->p_owner->b_own_thread = VLC_FALSE;
134     }
135     else
136     {
137         var_Get( p_input, "minimize-threads", &val );
138         p_dec->p_owner->b_own_thread = !val.b_bool;
139     }
140
141     if( p_dec->p_owner->b_own_thread )
142     {
143         int i_priority;
144         if( fmt->i_cat == AUDIO_ES )
145             i_priority = VLC_THREAD_PRIORITY_AUDIO;
146         else
147             i_priority = VLC_THREAD_PRIORITY_VIDEO;
148
149         /* Spawn the decoder thread */
150         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
151                                i_priority, VLC_FALSE ) )
152         {
153             msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
154                              p_dec->p_module->psz_object_name );
155             module_Unneed( p_dec, p_dec->p_module );
156             DeleteDecoder( p_dec );
157             vlc_object_destroy( p_dec );
158             return NULL;
159         }
160     }
161
162     return p_dec;
163 }
164
165 /**
166  * Kills a decoder thread and waits until it's finished
167  *
168  * \param p_input the input thread
169  * \param p_es the es descriptor
170  * \return nothing
171  */
172 void input_DecoderDelete( decoder_t *p_dec )
173 {
174     p_dec->b_die = VLC_TRUE;
175
176     if( p_dec->p_owner->b_own_thread )
177     {
178         /* Make sure the thread leaves the function by
179          * sending it an empty block. */
180         block_t *p_block = block_New( p_dec, 0 );
181         input_DecoderDecode( p_dec, p_block );
182
183         vlc_thread_join( p_dec );
184
185         /* Don't module_Unneed() here because of the dll loader that wants
186          * close() in the same thread than open()/decode() */
187     }
188     else
189     {
190         module_Unneed( p_dec, p_dec->p_module );
191     }
192
193     /* Delete decoder configuration */
194     DeleteDecoder( p_dec );
195
196     /* Delete the decoder */
197     vlc_object_destroy( p_dec );
198 }
199
200 /**
201  * Put a block_t in the decoder's fifo.
202  *
203  * \param p_dec the decoder object
204  * \param p_block the data block
205  */
206 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
207 {
208     if( p_dec->p_owner->b_own_thread )
209     {
210         block_FifoPut( p_dec->p_owner->p_fifo, p_block );
211
212         if( p_dec->p_owner->p_input->b_out_pace_control )
213         {
214             /* FIXME !!!!! */
215             while( !p_dec->b_die && !p_dec->b_error &&
216                    p_dec->p_owner->p_fifo->i_depth > 10 )
217             {
218                 msleep( 1000 );
219             }
220         }
221     }
222     else
223     {
224         if( p_dec->b_error || p_block->i_buffer <= 0 )
225         {
226             block_Release( p_block );
227         }
228         else
229         {
230             DecoderDecode( p_dec, p_block );
231         }
232     }
233 }
234
235 void input_DecoderDiscontinuity( decoder_t * p_dec )
236 {
237     block_t *p_null;
238
239     /* Empty the fifo */
240     if( p_dec->p_owner->b_own_thread )
241     {
242         block_FifoEmpty( p_dec->p_owner->p_fifo );
243     }
244
245     /* Send a special block */
246     p_null = block_New( p_dec, 128 );
247     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
248     memset( p_null->p_buffer, 0, p_null->i_buffer );
249
250     input_DecoderDecode( p_dec, p_null );
251 }
252
253 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
254 {
255     if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
256     {
257         return VLC_FALSE;
258     }
259     return VLC_TRUE;
260 }
261
262 #if 0
263 /**
264  * Create a NULL packet for padding in case of a data loss
265  *
266  * \param p_input the input thread
267  * \param p_es es descriptor
268  * \return nothing
269  */
270 static void input_NullPacket( input_thread_t * p_input,
271                               es_descriptor_t * p_es )
272 {
273 #if 0
274     block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
275     if( p_block )
276     {
277         memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
278         p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
279
280         block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
281     }
282 #endif
283 }
284
285 /**
286  * Send a NULL packet to the decoders
287  *
288  * \param p_input the input thread
289  * \return nothing
290  */
291 void input_EscapeDiscontinuity( input_thread_t * p_input )
292 {
293 #if 0
294     unsigned int i_es, i;
295
296     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
297     {
298         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
299
300         if( p_es->p_dec != NULL )
301         {
302             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
303             {
304                 input_NullPacket( p_input, p_es );
305             }
306         }
307     }
308 #endif
309 }
310
311 /**
312  * Send a NULL packet to the audio decoders
313  *
314  * \param p_input the input thread
315  * \return nothing
316  */
317 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
318 {
319 #if 0
320     unsigned int i_es, i;
321
322     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
323     {
324         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
325
326         if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
327         {
328             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
329             {
330                 input_NullPacket( p_input, p_es );
331             }
332         }
333     }
334 #endif
335 }
336 #endif
337
338 /**
339  * Create a decoder object
340  *
341  * \param p_input the input thread
342  * \param p_es the es descriptor
343  * \param i_object_type Object type as define in include/vlc_objects.h
344  * \return the decoder object
345  */
346 static decoder_t * CreateDecoder( input_thread_t *p_input,
347                                   es_format_t *fmt, int i_object_type )
348 {
349     decoder_t *p_dec;
350
351     p_dec = vlc_object_create( p_input, i_object_type );
352     if( p_dec == NULL )
353     {
354         msg_Err( p_input, "out of memory" );
355         return NULL;
356     }
357
358     p_dec->pf_decode_audio = 0;
359     p_dec->pf_decode_video = 0;
360     p_dec->pf_decode_sub = 0;
361     p_dec->pf_packetize = 0;
362
363     /* Initialize the decoder fifo */
364     p_dec->p_module = NULL;
365
366
367     es_format_Copy( &p_dec->fmt_in, fmt );
368     es_format_Copy( &p_dec->fmt_out, &null_es_format );
369
370     /* Allocate our private structure for the decoder */
371     p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
372     if( p_dec->p_owner == NULL )
373     {
374         msg_Err( p_dec, "out of memory" );
375         return NULL;
376     }
377     p_dec->p_owner->b_own_thread = VLC_TRUE;
378     p_dec->p_owner->p_input = p_input;
379     p_dec->p_owner->p_aout = NULL;
380     p_dec->p_owner->p_aout_input = NULL;
381     p_dec->p_owner->p_vout = NULL;
382     p_dec->p_owner->p_sout = p_input->p_sout;
383     p_dec->p_owner->p_sout_input = NULL;
384     p_dec->p_owner->p_packetizer = NULL;
385
386
387     /* decoder fifo */
388     if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
389     {
390         msg_Err( p_dec, "out of memory" );
391         return NULL;
392     }
393     /* Set buffers allocation callbacks for the decoders */
394     p_dec->pf_aout_buffer_new = aout_new_buffer;
395     p_dec->pf_aout_buffer_del = aout_del_buffer;
396     p_dec->pf_vout_buffer_new = vout_new_buffer;
397     p_dec->pf_vout_buffer_del = vout_del_buffer;
398     p_dec->pf_picture_link    = vout_link_picture;
399     p_dec->pf_picture_unlink  = vout_unlink_picture;
400
401     vlc_object_attach( p_dec, p_input );
402
403     /* Find a suitable decoder/packetizer module */
404     if( i_object_type == VLC_OBJECT_DECODER )
405         p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
406     else
407         p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
408
409     /* Check if decoder requires already packetized data */
410     if( i_object_type == VLC_OBJECT_DECODER &&
411         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
412     {
413         p_dec->p_owner->p_packetizer =
414             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
415         if( p_dec->p_owner->p_packetizer )
416         {
417             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
418                             &p_dec->fmt_in );
419
420             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
421                             &null_es_format );
422
423             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
424
425             p_dec->p_owner->p_packetizer->p_module =
426                 module_Need( p_dec->p_owner->p_packetizer,
427                              "packetizer", "$packetizer", 0 );
428
429             if( !p_dec->p_owner->p_packetizer->p_module )
430             {
431                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
432                 vlc_object_detach( p_dec->p_owner->p_packetizer );
433                 vlc_object_destroy( p_dec->p_owner->p_packetizer );
434             }
435         }
436     }
437
438     return p_dec;
439 }
440
441 /**
442  * The decoding main loop
443  *
444  * \param p_dec the decoder
445  * \return 0
446  */
447 static int DecoderThread( decoder_t * p_dec )
448 {
449     block_t *p_block;
450
451     /* The decoder's main loop */
452     while( !p_dec->b_die && !p_dec->b_error )
453     {
454         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
455         {
456             p_dec->b_error = 1;
457             break;
458         }
459         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
460         {
461             break;
462         }
463     }
464
465     while( !p_dec->b_die )
466     {
467         /* Trash all received PES packets */
468         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
469         if( p_block ) block_Release( p_block );
470     }
471
472     /* We do it here because of the dll loader that wants close() in the
473      * same thread than open()/decode() */
474     module_Unneed( p_dec, p_dec->p_module );
475
476     return 0;
477 }
478
479 /**
480  * Decode a block
481  *
482  * \param p_dec the decoder object
483  * \param p_block the block to decode
484  * \return VLC_SUCCESS or an error code
485  */
486 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
487 {
488     if( p_block->i_buffer <= 0 )
489     {
490         block_Release( p_block );
491         return VLC_SUCCESS;
492     }
493
494     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
495     {
496         block_t *p_sout_block;
497
498         while( (p_sout_block = p_dec->pf_packetize( p_dec, &p_block )) )
499         {
500             if( !p_dec->p_owner->p_sout_input )
501             {
502                 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
503
504                 p_dec->p_owner->sout.i_group =p_dec->fmt_in.i_group;
505                 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
506                 if( p_dec->fmt_in.psz_language )
507                 {
508                     p_dec->p_owner->sout.psz_language =
509                         strdup( p_dec->fmt_in.psz_language );
510                 }
511
512                 p_dec->p_owner->p_sout_input =
513                     sout_InputNew( p_dec->p_owner->p_sout,
514                                    &p_dec->p_owner->sout );
515
516                 if( p_dec->p_owner->p_sout_input == NULL )
517                 {
518                     msg_Err( p_dec, "cannot create packetizer output" );
519                     p_dec->b_error = VLC_TRUE;
520
521                     while( p_sout_block )
522                     {
523                         block_t *p_next = p_sout_block->p_next;
524                         block_Release( p_sout_block );
525                         p_sout_block = p_next;
526                     }
527                     break;
528                 }
529             }
530
531             while( p_sout_block )
532             {
533                 block_t       *p_next = p_sout_block->p_next;
534
535                 p_sout_block->p_next = NULL;
536
537                 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
538                                       p_sout_block );
539
540                 p_sout_block = p_next;
541             }
542
543             /* For now it's enough, as only sout inpact on this flag */
544             if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
545                 p_dec->p_owner->p_input->b_out_pace_control )
546             {
547                 msg_Dbg( p_dec, "switching to synch mode" );
548                 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
549             }
550             else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
551                      !p_dec->p_owner->p_input->b_out_pace_control )
552             {
553                 msg_Dbg( p_dec, "switching to asynch mode" );
554                 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
555             }
556         }
557     }
558     else if( p_dec->fmt_in.i_cat == AUDIO_ES )
559     {
560         aout_buffer_t *p_aout_buf;
561
562         if( p_dec->p_owner->p_packetizer )
563         {
564             block_t *p_packetized_block;
565             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
566
567             while( (p_packetized_block =
568                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
569             {
570                 while( (p_aout_buf =
571                         p_dec->pf_decode_audio( p_dec, &p_packetized_block )) )
572                 {
573                     aout_DecPlay( p_dec->p_owner->p_aout,
574                                   p_dec->p_owner->p_aout_input, p_aout_buf );
575                 }
576             }
577         }
578         else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
579         {
580             aout_DecPlay( p_dec->p_owner->p_aout,
581                           p_dec->p_owner->p_aout_input, p_aout_buf );
582         }
583     }
584     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
585     {
586         picture_t *p_pic;
587
588         if( p_dec->p_owner->p_packetizer )
589         {
590             block_t *p_packetized_block;
591             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
592
593             while( (p_packetized_block =
594                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
595             {
596                 while( (p_pic =
597                         p_dec->pf_decode_video( p_dec, &p_packetized_block )) )
598                 {
599                     vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
600                                       p_pic->date );
601                     vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
602                 }
603             }
604         }
605         else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
606         {
607             vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
608             vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
609         }
610     }
611     else if( p_dec->fmt_in.i_cat == SPU_ES )
612     {
613         p_dec->pf_decode_sub( p_dec, &p_block );
614     }
615     else
616     {
617         msg_Err( p_dec, "unknown ES format" );
618         p_dec->b_error = 1;
619     }
620
621     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
622 }
623
624 /**
625  * Destroys a decoder object
626  *
627  * \param p_dec the decoder object
628  * \return nothing
629  */
630 static void DeleteDecoder( decoder_t * p_dec )
631 {
632     vlc_object_detach( p_dec );
633
634     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
635              (char*)&p_dec->fmt_in.i_codec,
636              p_dec->p_owner->p_fifo->i_depth );
637
638     /* Free all packets still in the decoder fifo. */
639     block_FifoEmpty( p_dec->p_owner->p_fifo );
640     block_FifoRelease( p_dec->p_owner->p_fifo );
641
642    /* Cleanup */
643     if( p_dec->p_owner->p_aout_input )
644         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
645
646     if( p_dec->p_owner->p_vout )
647     {
648         int i_pic;
649
650 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
651         /* Hack to make sure all the the pictures are freed by the decoder */
652         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
653              i_pic++ )
654         {
655             if( p_pic->i_status == RESERVED_PICTURE )
656                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
657             if( p_pic->i_refcount > 0 )
658                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
659         }
660 #undef p_pic
661
662         /* We are about to die. Reattach video output to p_vlc. */
663         vout_Request( p_dec, p_dec->p_owner->p_vout, 0, 0, 0, 0 );
664     }
665
666     if( p_dec->p_owner->p_sout_input )
667     {
668         sout_InputDelete( p_dec->p_owner->p_sout_input );
669         es_format_Clean( &p_dec->p_owner->sout );
670     }
671
672     es_format_Clean( &p_dec->fmt_in );
673     es_format_Clean( &p_dec->fmt_out );
674
675     if( p_dec->p_owner->p_packetizer )
676     {
677         module_Unneed( p_dec->p_owner->p_packetizer,
678                        p_dec->p_owner->p_packetizer->p_module );
679         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
680         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
681         vlc_object_detach( p_dec->p_owner->p_packetizer );
682         vlc_object_destroy( p_dec->p_owner->p_packetizer );
683     }
684
685     free( p_dec->p_owner );
686 }
687
688 /*****************************************************************************
689  * Buffers allocation callbacks for the decoders
690  *****************************************************************************/
691 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
692 {
693     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
694     aout_buffer_t *p_buffer;
695
696     if( p_sys->p_aout_input != NULL &&
697         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
698           p_dec->fmt_out.audio.i_original_channels !=
699               p_sys->audio.i_original_channels ||
700           p_dec->fmt_out.audio.i_bytes_per_frame !=
701               p_sys->audio.i_bytes_per_frame ) )
702     {
703         /* Parameters changed, restart the aout */
704         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
705         p_sys->p_aout_input = NULL;
706     }
707
708     if( p_sys->p_aout_input == NULL )
709     {
710         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
711         p_sys->audio = p_dec->fmt_out.audio;
712         p_sys->p_aout_input =
713             aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->audio );
714         if( p_sys->p_aout_input == NULL )
715         {
716             msg_Err( p_dec, "failed to create audio output" );
717             p_dec->b_error = VLC_TRUE;
718             return NULL;
719         }
720         p_dec->fmt_out.audio.i_bytes_per_frame =
721             p_sys->audio.i_bytes_per_frame;
722     }
723
724     p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
725                                   i_samples );
726
727     return p_buffer;
728 }
729
730 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
731 {
732     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
733                           p_dec->p_owner->p_aout_input, p_buffer );
734 }
735
736 static picture_t *vout_new_buffer( decoder_t *p_dec )
737 {
738     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
739     picture_t *p_pic;
740
741     if( p_sys->p_vout == NULL ||
742         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
743         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
744         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
745         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
746     {
747         if( !p_dec->fmt_out.video.i_width ||
748             !p_dec->fmt_out.video.i_height )
749         {
750             /* Can't create a new vout without display size */
751             return NULL;
752         }
753
754         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
755         p_sys->video = p_dec->fmt_out.video;
756
757         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
758                                       p_sys->video.i_width,
759                                       p_sys->video.i_height,
760                                       p_sys->video.i_chroma,
761                                       p_sys->video.i_aspect );
762
763         if( p_sys->p_vout == NULL )
764         {
765             msg_Err( p_dec, "failed to create video output" );
766             p_dec->b_error = VLC_TRUE;
767             return NULL;
768         }
769     }
770
771     /* Get a new picture */
772     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
773     {
774         int i_pic, i_ready_pic = 0;
775
776         if( p_dec->b_die || p_dec->b_error )
777         {
778             return NULL;
779         }
780
781 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
782         /* Check the decoder doesn't leak pictures */
783         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
784              i_pic++ )
785         {
786             if( p_pic->i_status == READY_PICTURE && i_ready_pic++ > 0 ) break;
787
788             if( p_pic->i_status != DISPLAYED_PICTURE &&
789                 p_pic->i_status != RESERVED_PICTURE &&
790                 p_pic->i_status != READY_PICTURE ) break;
791
792             if( !p_pic->i_refcount ) break;
793         }
794         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
795         {
796             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
797
798             /* Just free all the pictures */
799             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
800                  i_pic++ )
801             {
802                 if( p_pic->i_status == RESERVED_PICTURE )
803                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
804                 if( p_pic->i_refcount > 0 )
805                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
806             }
807         }
808 #undef p_pic
809
810         msleep( VOUT_OUTMEM_SLEEP );
811     }
812
813     return p_pic;
814 }
815
816 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
817 {
818     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
819 }
820
821 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
822 {
823     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
824 }
825
826 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
827 {
828     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
829 }