]> git.sesse.net Git - vlc/blob - src/input/decoder.c
* ALL: separation of the SPU engine from the VOUT.
[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@videolan.org>
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 subpicture_t *spu_new_buffer( decoder_t * );
55 static void spu_del_buffer( decoder_t *, subpicture_t * );
56
57 static es_format_t null_es_format = {0};
58
59 struct decoder_owner_sys_t
60 {
61     vlc_bool_t      b_own_thread;
62
63     input_thread_t  *p_input;
64
65     aout_instance_t *p_aout;
66     aout_input_t    *p_aout_input;
67
68     vout_thread_t   *p_vout;
69
70     vout_thread_t   *p_spu_vout;
71     int              i_spu_channel;
72
73     sout_instance_t         *p_sout;
74     sout_packetizer_input_t *p_sout_input;
75
76     /* Some decoders require already packetized data (ie. not truncated) */
77     decoder_t *p_packetizer;
78
79     /* Current format in use by the output */
80     video_format_t video;
81     audio_format_t audio;
82     es_format_t    sout;
83
84     /* fifo */
85     block_fifo_t *p_fifo;
86 };
87
88
89 /**
90  * Spawns a new decoder thread
91  *
92  * \param p_input the input thread
93  * \param p_es the es descriptor
94  * \return the spawned decoder object
95  */
96 decoder_t *input_DecoderNew( input_thread_t *p_input,
97                              es_format_t *fmt, vlc_bool_t b_force_decoder )
98 {
99     decoder_t   *p_dec = NULL;
100     vlc_value_t val;
101
102     /* If we are in sout mode, search for packetizer module */
103     if( p_input->p_sout && !b_force_decoder )
104     {
105         /* Create the decoder configuration structure */
106         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
107         if( p_dec == NULL )
108         {
109             msg_Err( p_input, "could not create packetizer" );
110             return NULL;
111         }
112     }
113     else
114     {
115         /* Create the decoder configuration structure */
116         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
117         if( p_dec == NULL )
118         {
119             msg_Err( p_input, "could not create decoder" );
120             return NULL;
121         }
122     }
123
124     if( !p_dec->p_module )
125     {
126         msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
127                  "VLC probably does not support this sound or video format.",
128                  (char*)&p_dec->fmt_in.i_codec );
129
130         DeleteDecoder( p_dec );
131         vlc_object_destroy( p_dec );
132         return NULL;
133     }
134
135     if( p_input->p_sout && p_input->input.b_can_pace_control &&
136         !b_force_decoder )
137     {
138         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
139         p_dec->p_owner->b_own_thread = VLC_FALSE;
140     }
141     else
142     {
143         var_Get( p_input, "minimize-threads", &val );
144         p_dec->p_owner->b_own_thread = !val.b_bool;
145     }
146
147     if( p_dec->p_owner->b_own_thread )
148     {
149         int i_priority;
150         if( fmt->i_cat == AUDIO_ES )
151             i_priority = VLC_THREAD_PRIORITY_AUDIO;
152         else
153             i_priority = VLC_THREAD_PRIORITY_VIDEO;
154
155         /* Spawn the decoder thread */
156         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
157                                i_priority, VLC_FALSE ) )
158         {
159             msg_Err( p_dec, "cannot spawn decoder thread \"%s\"",
160                              p_dec->p_module->psz_object_name );
161             module_Unneed( p_dec, p_dec->p_module );
162             DeleteDecoder( p_dec );
163             vlc_object_destroy( p_dec );
164             return NULL;
165         }
166     }
167
168     return p_dec;
169 }
170
171 /**
172  * Kills a decoder thread and waits until it's finished
173  *
174  * \param p_input the input thread
175  * \param p_es the es descriptor
176  * \return nothing
177  */
178 void input_DecoderDelete( decoder_t *p_dec )
179 {
180     p_dec->b_die = VLC_TRUE;
181
182     if( p_dec->p_owner->b_own_thread )
183     {
184         /* Make sure the thread leaves the function by
185          * sending it an empty block. */
186         block_t *p_block = block_New( p_dec, 0 );
187         input_DecoderDecode( p_dec, p_block );
188
189         vlc_thread_join( p_dec );
190
191         /* Don't module_Unneed() here because of the dll loader that wants
192          * close() in the same thread than open()/decode() */
193     }
194     else
195     {
196         /* Flush */
197         input_DecoderDecode( p_dec, NULL );
198
199         module_Unneed( p_dec, p_dec->p_module );
200     }
201
202     /* Delete decoder configuration */
203     DeleteDecoder( p_dec );
204
205     /* Delete the decoder */
206     vlc_object_destroy( p_dec );
207 }
208
209 /**
210  * Put a block_t in the decoder's fifo.
211  *
212  * \param p_dec the decoder object
213  * \param p_block the data block
214  */
215 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
216 {
217     if( p_dec->p_owner->b_own_thread )
218     {
219         if( p_dec->p_owner->p_input->b_out_pace_control )
220         {
221             /* FIXME !!!!! */
222             while( !p_dec->b_die && !p_dec->b_error &&
223                    p_dec->p_owner->p_fifo->i_depth > 10 )
224             {
225                 msleep( 1000 );
226             }
227         }
228         else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
229         {
230             /* FIXME: ideally we would check the time amount of data
231              * in the fifo instead of its size. */
232             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
233                       "consummed quickly enough), resetting fifo!" );
234             block_FifoEmpty( p_dec->p_owner->p_fifo );
235         }
236
237         block_FifoPut( p_dec->p_owner->p_fifo, p_block );
238     }
239     else
240     {
241         if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
242         {
243             if( p_block ) block_Release( p_block );
244         }
245         else
246         {
247             DecoderDecode( p_dec, p_block );
248         }
249     }
250 }
251
252 void input_DecoderDiscontinuity( decoder_t * p_dec )
253 {
254     block_t *p_null;
255
256     /* Empty the fifo */
257     if( p_dec->p_owner->b_own_thread )
258     {
259         block_FifoEmpty( p_dec->p_owner->p_fifo );
260     }
261
262     /* Send a special block */
263     p_null = block_New( p_dec, 128 );
264     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
265     memset( p_null->p_buffer, 0, p_null->i_buffer );
266
267     input_DecoderDecode( p_dec, p_null );
268 }
269
270 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
271 {
272     if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
273     {
274         return VLC_FALSE;
275     }
276     return VLC_TRUE;
277 }
278
279 #if 0
280 /**
281  * Create a NULL packet for padding in case of a data loss
282  *
283  * \param p_input the input thread
284  * \param p_es es descriptor
285  * \return nothing
286  */
287 static void input_NullPacket( input_thread_t * p_input,
288                               es_descriptor_t * p_es )
289 {
290 #if 0
291     block_t *p_block = block_New( p_input, PADDING_PACKET_SIZE );
292     if( p_block )
293     {
294         memset( p_block->p_buffer, 0, PADDING_PACKET_SIZE );
295         p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
296
297         block_FifoPut( p_es->p_dec->p_owner->p_fifo, p_block );
298     }
299 #endif
300 }
301
302 /**
303  * Send a NULL packet to the decoders
304  *
305  * \param p_input the input thread
306  * \return nothing
307  */
308 void input_EscapeDiscontinuity( input_thread_t * p_input )
309 {
310 #if 0
311     unsigned int i_es, i;
312
313     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
314     {
315         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
316
317         if( p_es->p_dec != NULL )
318         {
319             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
320             {
321                 input_NullPacket( p_input, p_es );
322             }
323         }
324     }
325 #endif
326 }
327
328 /**
329  * Send a NULL packet to the audio decoders
330  *
331  * \param p_input the input thread
332  * \return nothing
333  */
334 void input_EscapeAudioDiscontinuity( input_thread_t * p_input )
335 {
336 #if 0
337     unsigned int i_es, i;
338
339     for( i_es = 0; i_es < p_input->stream.i_selected_es_number; i_es++ )
340     {
341         es_descriptor_t * p_es = p_input->stream.pp_selected_es[i_es];
342
343         if( p_es->p_dec != NULL && p_es->i_cat == AUDIO_ES )
344         {
345             for( i = 0; i < PADDING_PACKET_NUMBER; i++ )
346             {
347                 input_NullPacket( p_input, p_es );
348             }
349         }
350     }
351 #endif
352 }
353 #endif
354
355 /**
356  * Create a decoder object
357  *
358  * \param p_input the input thread
359  * \param p_es the es descriptor
360  * \param i_object_type Object type as define in include/vlc_objects.h
361  * \return the decoder object
362  */
363 static decoder_t * CreateDecoder( input_thread_t *p_input,
364                                   es_format_t *fmt, int i_object_type )
365 {
366     decoder_t *p_dec;
367
368     p_dec = vlc_object_create( p_input, i_object_type );
369     if( p_dec == NULL )
370     {
371         msg_Err( p_input, "out of memory" );
372         return NULL;
373     }
374
375     p_dec->pf_decode_audio = 0;
376     p_dec->pf_decode_video = 0;
377     p_dec->pf_decode_sub = 0;
378     p_dec->pf_packetize = 0;
379
380     /* Initialize the decoder fifo */
381     p_dec->p_module = NULL;
382
383
384     es_format_Copy( &p_dec->fmt_in, fmt );
385     es_format_Copy( &p_dec->fmt_out, &null_es_format );
386
387     /* Allocate our private structure for the decoder */
388     p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
389     if( p_dec->p_owner == NULL )
390     {
391         msg_Err( p_dec, "out of memory" );
392         return NULL;
393     }
394     p_dec->p_owner->b_own_thread = VLC_TRUE;
395     p_dec->p_owner->p_input = p_input;
396     p_dec->p_owner->p_aout = NULL;
397     p_dec->p_owner->p_aout_input = NULL;
398     p_dec->p_owner->p_vout = NULL;
399     p_dec->p_owner->p_spu_vout = NULL;
400     p_dec->p_owner->i_spu_channel = 0;
401     p_dec->p_owner->p_sout = p_input->p_sout;
402     p_dec->p_owner->p_sout_input = NULL;
403     p_dec->p_owner->p_packetizer = NULL;
404
405     /* decoder fifo */
406     if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
407     {
408         msg_Err( p_dec, "out of memory" );
409         return NULL;
410     }
411
412     /* Set buffers allocation callbacks for the decoders */
413     p_dec->pf_aout_buffer_new = aout_new_buffer;
414     p_dec->pf_aout_buffer_del = aout_del_buffer;
415     p_dec->pf_vout_buffer_new = vout_new_buffer;
416     p_dec->pf_vout_buffer_del = vout_del_buffer;
417     p_dec->pf_picture_link    = vout_link_picture;
418     p_dec->pf_picture_unlink  = vout_unlink_picture;
419     p_dec->pf_spu_buffer_new  = spu_new_buffer;
420     p_dec->pf_spu_buffer_del  = spu_del_buffer;
421
422     vlc_object_attach( p_dec, p_input );
423
424     /* Find a suitable decoder/packetizer module */
425     if( i_object_type == VLC_OBJECT_DECODER )
426         p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
427     else
428         p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
429
430     /* Check if decoder requires already packetized data */
431     if( i_object_type == VLC_OBJECT_DECODER &&
432         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
433     {
434         p_dec->p_owner->p_packetizer =
435             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
436         if( p_dec->p_owner->p_packetizer )
437         {
438             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
439                             &p_dec->fmt_in );
440
441             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
442                             &null_es_format );
443
444             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
445
446             p_dec->p_owner->p_packetizer->p_module =
447                 module_Need( p_dec->p_owner->p_packetizer,
448                              "packetizer", "$packetizer", 0 );
449
450             if( !p_dec->p_owner->p_packetizer->p_module )
451             {
452                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
453                 vlc_object_detach( p_dec->p_owner->p_packetizer );
454                 vlc_object_destroy( p_dec->p_owner->p_packetizer );
455             }
456         }
457     }
458
459     return p_dec;
460 }
461
462 /**
463  * The decoding main loop
464  *
465  * \param p_dec the decoder
466  * \return 0
467  */
468 static int DecoderThread( decoder_t * p_dec )
469 {
470     block_t *p_block;
471
472     /* The decoder's main loop */
473     while( !p_dec->b_die && !p_dec->b_error )
474     {
475         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
476         {
477             p_dec->b_error = 1;
478             break;
479         }
480         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
481         {
482             break;
483         }
484     }
485
486     while( !p_dec->b_die )
487     {
488         /* Trash all received PES packets */
489         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
490         if( p_block ) block_Release( p_block );
491     }
492
493     /* We do it here because of the dll loader that wants close() in the
494      * same thread than open()/decode() */
495     module_Unneed( p_dec, p_dec->p_module );
496
497     return 0;
498 }
499
500 /**
501  * Decode a block
502  *
503  * \param p_dec the decoder object
504  * \param p_block the block to decode
505  * \return VLC_SUCCESS or an error code
506  */
507 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
508 {
509     int i_rate = p_block ? p_block->i_rate : 1000;
510
511     if( p_block && p_block->i_buffer <= 0 )
512     {
513         block_Release( p_block );
514         return VLC_SUCCESS;
515     }
516
517     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
518     {
519         block_t *p_sout_block;
520
521         while( ( p_sout_block =
522                      p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
523         {
524             if( !p_dec->p_owner->p_sout_input )
525             {
526                 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
527
528                 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
529                 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
530                 if( p_dec->fmt_in.psz_language )
531                 {
532                     p_dec->p_owner->sout.psz_language =
533                         strdup( p_dec->fmt_in.psz_language );
534                 }
535
536                 p_dec->p_owner->p_sout_input =
537                     sout_InputNew( p_dec->p_owner->p_sout,
538                                    &p_dec->p_owner->sout );
539
540                 if( p_dec->p_owner->p_sout_input == NULL )
541                 {
542                     msg_Err( p_dec, "cannot create packetizer output" );
543                     p_dec->b_error = VLC_TRUE;
544
545                     while( p_sout_block )
546                     {
547                         block_t *p_next = p_sout_block->p_next;
548                         block_Release( p_sout_block );
549                         p_sout_block = p_next;
550                     }
551                     break;
552                 }
553             }
554
555             while( p_sout_block )
556             {
557                 block_t *p_next = p_sout_block->p_next;
558
559                 p_sout_block->p_next = NULL;
560                 p_sout_block->i_rate = i_rate;
561
562                 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
563                                       p_sout_block );
564
565                 p_sout_block = p_next;
566             }
567
568             /* For now it's enough, as only sout inpact on this flag */
569             if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
570                 p_dec->p_owner->p_input->b_out_pace_control )
571             {
572                 msg_Dbg( p_dec, "switching to synch mode" );
573                 p_dec->p_owner->p_input->b_out_pace_control = VLC_FALSE;
574             }
575             else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
576                      !p_dec->p_owner->p_input->b_out_pace_control )
577             {
578                 msg_Dbg( p_dec, "switching to asynch mode" );
579                 p_dec->p_owner->p_input->b_out_pace_control = VLC_TRUE;
580             }
581         }
582     }
583     else if( p_dec->fmt_in.i_cat == AUDIO_ES )
584     {
585         aout_buffer_t *p_aout_buf;
586
587         if( p_dec->p_owner->p_packetizer )
588         {
589             block_t *p_packetized_block;
590             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
591
592             while( (p_packetized_block =
593                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
594             {
595                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
596                 {
597                     p_dec->fmt_in.i_extra = p_packetizer->fmt_out.i_extra;
598                     p_dec->fmt_in.p_extra = malloc( p_dec->fmt_in.i_extra );
599                     memcpy( p_dec->fmt_in.p_extra,
600                             p_packetizer->fmt_out.p_extra,
601                             p_dec->fmt_in.i_extra );
602                 }
603
604                 while( p_packetized_block )
605                 {
606                     block_t *p_next = p_packetized_block->p_next;
607                     p_packetized_block->p_next = NULL;
608                     p_packetized_block->i_rate = i_rate;
609
610                     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec,
611                                                        &p_packetized_block )) )
612                     {
613                         aout_DecPlay( p_dec->p_owner->p_aout,
614                                       p_dec->p_owner->p_aout_input,
615                                       p_aout_buf );
616                     }
617
618                     p_packetized_block = p_next;
619                 }
620             }
621         }
622         else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
623         {
624             aout_DecPlay( p_dec->p_owner->p_aout,
625                           p_dec->p_owner->p_aout_input, p_aout_buf );
626         }
627     }
628     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
629     {
630         picture_t *p_pic;
631
632         if( p_dec->p_owner->p_packetizer )
633         {
634             block_t *p_packetized_block;
635             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
636
637             while( (p_packetized_block =
638                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
639             {
640                 while( p_packetized_block )
641                 {
642                     block_t *p_next = p_packetized_block->p_next;
643                     p_packetized_block->p_next = NULL;
644                     p_packetized_block->i_rate = i_rate;
645
646                     while( (p_pic = p_dec->pf_decode_video( p_dec,
647                                                        &p_packetized_block )) )
648                     {
649                         vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
650                                           p_pic->date );
651                         vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
652                     }
653
654                     p_packetized_block = p_next;
655                 }
656             }
657         }
658         else while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
659         {
660             vout_DatePicture( p_dec->p_owner->p_vout, p_pic, p_pic->date );
661             vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
662         }
663     }
664     else if( p_dec->fmt_in.i_cat == SPU_ES )
665     {
666         vout_thread_t *p_vout;
667         subpicture_t *p_spu;
668         while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
669         {
670             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
671             if( p_vout )
672             {
673                 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
674                 vlc_object_release( p_vout );
675             }
676         }
677     }
678     else
679     {
680         msg_Err( p_dec, "unknown ES format" );
681         p_dec->b_error = 1;
682     }
683
684     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
685 }
686
687 /**
688  * Destroys a decoder object
689  *
690  * \param p_dec the decoder object
691  * \return nothing
692  */
693 static void DeleteDecoder( decoder_t * p_dec )
694 {
695     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
696              (char*)&p_dec->fmt_in.i_codec,
697              p_dec->p_owner->p_fifo->i_depth );
698
699     /* Free all packets still in the decoder fifo. */
700     block_FifoEmpty( p_dec->p_owner->p_fifo );
701     block_FifoRelease( p_dec->p_owner->p_fifo );
702
703     /* Cleanup */
704     if( p_dec->p_owner->p_aout_input )
705         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
706
707     if( p_dec->p_owner->p_vout )
708     {
709         int i_pic;
710
711 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
712         /* Hack to make sure all the the pictures are freed by the decoder */
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 == RESERVED_PICTURE )
717                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
718             if( p_pic->i_refcount > 0 )
719                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
720         }
721 #undef p_pic
722
723         /* We are about to die. Reattach video output to p_vlc. */
724         vout_Request( p_dec, p_dec->p_owner->p_vout, 0, 0, 0, 0 );
725     }
726
727     if( p_dec->p_owner->p_sout_input )
728     {
729         sout_InputDelete( p_dec->p_owner->p_sout_input );
730         es_format_Clean( &p_dec->p_owner->sout );
731     }
732
733     if( p_dec->fmt_in.i_cat == SPU_ES )
734     {
735         vout_thread_t *p_vout;
736
737         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
738         if( p_vout )
739         {
740             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
741                          p_dec->p_owner->i_spu_channel );
742             vlc_object_release( p_vout );
743         }
744     }
745
746     es_format_Clean( &p_dec->fmt_in );
747     es_format_Clean( &p_dec->fmt_out );
748
749     if( p_dec->p_owner->p_packetizer )
750     {
751         module_Unneed( p_dec->p_owner->p_packetizer,
752                        p_dec->p_owner->p_packetizer->p_module );
753         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
754         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
755         vlc_object_detach( p_dec->p_owner->p_packetizer );
756         vlc_object_destroy( p_dec->p_owner->p_packetizer );
757     }
758
759     vlc_object_detach( p_dec );
760
761     free( p_dec->p_owner );
762 }
763
764 /*****************************************************************************
765  * Buffers allocation callbacks for the decoders
766  *****************************************************************************/
767 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
768 {
769     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
770     aout_buffer_t *p_buffer;
771
772     if( p_sys->p_aout_input != NULL &&
773         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
774           p_dec->fmt_out.audio.i_original_channels !=
775               p_sys->audio.i_original_channels ||
776           p_dec->fmt_out.audio.i_bytes_per_frame !=
777               p_sys->audio.i_bytes_per_frame ) )
778     {
779         /* Parameters changed, restart the aout */
780         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
781         p_sys->p_aout_input = NULL;
782     }
783
784     if( p_sys->p_aout_input == NULL )
785     {
786         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
787         p_sys->audio = p_dec->fmt_out.audio;
788         p_sys->p_aout_input =
789             aout_DecNew( p_dec, &p_sys->p_aout, &p_sys->audio );
790         if( p_sys->p_aout_input == NULL )
791         {
792             msg_Err( p_dec, "failed to create audio output" );
793             p_dec->b_error = VLC_TRUE;
794             return NULL;
795         }
796         p_dec->fmt_out.audio.i_bytes_per_frame =
797             p_sys->audio.i_bytes_per_frame;
798     }
799
800     p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
801                                   i_samples );
802
803     return p_buffer;
804 }
805
806 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
807 {
808     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
809                           p_dec->p_owner->p_aout_input, p_buffer );
810 }
811
812 static picture_t *vout_new_buffer( decoder_t *p_dec )
813 {
814     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
815     picture_t *p_pic;
816
817     if( p_sys->p_vout == NULL ||
818         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
819         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
820         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
821         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
822     {
823         if( !p_dec->fmt_out.video.i_width ||
824             !p_dec->fmt_out.video.i_height )
825         {
826             /* Can't create a new vout without display size */
827             return NULL;
828         }
829
830         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
831         p_sys->video = p_dec->fmt_out.video;
832
833         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
834                                       p_sys->video.i_width,
835                                       p_sys->video.i_height,
836                                       p_sys->video.i_chroma,
837                                       p_sys->video.i_aspect );
838
839         if( p_sys->p_vout == NULL )
840         {
841             msg_Err( p_dec, "failed to create video output" );
842             p_dec->b_error = VLC_TRUE;
843             return NULL;
844         }
845
846         if( p_sys->video.i_rmask )
847             p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
848         if( p_sys->video.i_gmask )
849             p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
850         if( p_sys->video.i_bmask )
851             p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
852     }
853
854     /* Get a new picture */
855     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
856     {
857         int i_pic, i_ready_pic = 0;
858
859         if( p_dec->b_die || p_dec->b_error )
860         {
861             return NULL;
862         }
863
864 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
865         /* Check the decoder doesn't leak pictures */
866         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
867              i_pic++ )
868         {
869             if( p_pic->i_status == READY_PICTURE )
870             {
871                 if( i_ready_pic++ > 0 ) break;
872                 else continue;
873             }
874
875             if( p_pic->i_status != DISPLAYED_PICTURE &&
876                 p_pic->i_status != RESERVED_PICTURE &&
877                 p_pic->i_status != READY_PICTURE ) break;
878
879             if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
880                 break;
881         }
882         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
883         {
884             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
885
886             /* Just free all the pictures */
887             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
888                  i_pic++ )
889             {
890                 if( p_pic->i_status == RESERVED_PICTURE )
891                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
892                 if( p_pic->i_refcount > 0 )
893                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
894             }
895         }
896 #undef p_pic
897
898         msleep( VOUT_OUTMEM_SLEEP );
899     }
900
901     return p_pic;
902 }
903
904 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
905 {
906     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
907 }
908
909 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
910 {
911     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
912 }
913
914 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
915 {
916     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
917 }
918
919 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
920 {
921     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
922     vout_thread_t *p_vout = NULL;
923     subpicture_t *p_subpic;
924     int i_attempts = 30;
925
926     while( i_attempts-- )
927     {
928         if( p_dec->b_die || p_dec->b_error ) break;
929
930         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
931         if( p_vout ) break;
932
933         msleep( VOUT_DISPLAY_DELAY );
934     }
935
936     if( !p_vout )
937     {
938         msg_Warn( p_dec, "no vout found, dropping subpicture" );
939         return NULL;
940     }
941
942     if( p_sys->p_spu_vout != p_vout )
943     {
944         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
945                      &p_sys->i_spu_channel );
946         p_sys->p_spu_vout = p_vout;
947     }
948
949     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
950     p_subpic->i_channel = p_sys->i_spu_channel;
951
952     vlc_object_release( p_vout );
953
954     return p_subpic;
955 }
956
957 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
958 {
959     spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_subpic );
960 }