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