079b546acd5360ea56f505486d12828471aa764a
[vlc.git] / src / input / decoder.c
1 /*****************************************************************************
2  * decoder.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>
30 #include <vlc/vlc.h>
31
32 #include <vlc_block.h>
33 #include <vlc_vout.h>
34 #include <vlc_aout.h>
35 #include <vlc_sout.h>
36 #include <vlc_codec.h>
37 #include <vlc_osd.h>
38
39 #include <vlc_interface.h>
40 #include "audio_output/aout_internal.h"
41 #include "stream_output/stream_output.h"
42 #include "input_internal.h"
43
44 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
45 static void        DeleteDecoder( decoder_t * );
46
47 static int         DecoderThread( decoder_t * );
48 static int         DecoderDecode( decoder_t * p_dec, block_t *p_block );
49
50 /* Buffers allocation callbacks for the decoders */
51 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
52 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
53
54 static picture_t *vout_new_buffer( decoder_t * );
55 static void vout_del_buffer( decoder_t *, picture_t * );
56 static void vout_link_picture( decoder_t *, picture_t * );
57 static void vout_unlink_picture( decoder_t *, picture_t * );
58
59 static subpicture_t *spu_new_buffer( decoder_t * );
60 static void spu_del_buffer( decoder_t *, subpicture_t * );
61
62 static es_format_t null_es_format;
63
64 struct decoder_owner_sys_t
65 {
66     vlc_bool_t      b_own_thread;
67
68     int64_t         i_preroll_end;
69
70     input_thread_t  *p_input;
71
72     aout_instance_t *p_aout;
73     aout_input_t    *p_aout_input;
74
75     vout_thread_t   *p_vout;
76
77     vout_thread_t   *p_spu_vout;
78     int              i_spu_channel;
79
80     sout_instance_t         *p_sout;
81     sout_packetizer_input_t *p_sout_input;
82
83     /* Some decoders require already packetized data (ie. not truncated) */
84     decoder_t *p_packetizer;
85
86     /* Current format in use by the output */
87     video_format_t video;
88     audio_format_t audio;
89     es_format_t    sout;
90
91     /* fifo */
92     block_fifo_t *p_fifo;
93 };
94
95 /* decoder_GetInputAttachment:
96  */
97 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
98                                                 const char *psz_name )
99 {
100     input_attachment_t *p_attachment;
101     if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
102         return NULL;
103     return p_attachment;
104 }
105 /* decoder_GetInputAttachments:
106  */
107 int decoder_GetInputAttachments( decoder_t *p_dec,
108                                  input_attachment_t ***ppp_attachment,
109                                  int *pi_attachment )
110 {
111     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
112                           ppp_attachment, pi_attachment );
113 }
114
115 /**
116  * Spawns a new decoder thread
117  *
118  * \param p_input the input thread
119  * \param p_es the es descriptor
120  * \return the spawned decoder object
121  */
122 decoder_t *input_DecoderNew( input_thread_t *p_input,
123                              es_format_t *fmt, vlc_bool_t b_force_decoder )
124 {
125     decoder_t   *p_dec = NULL;
126     vlc_value_t val;
127
128     /* If we are in sout mode, search for packetizer module */
129     if( p_input->p->p_sout && !b_force_decoder )
130     {
131         /* Create the decoder configuration structure */
132         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
133         if( p_dec == NULL )
134         {
135             msg_Err( p_input, "could not create packetizer" );
136             intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"), 
137                             _("VLC could not open the packetizer module.") );
138             return NULL;
139         }
140     }
141     else
142     {
143         /* Create the decoder configuration structure */
144         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
145         if( p_dec == NULL )
146         {
147             msg_Err( p_input, "could not create decoder" );
148             intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"), 
149                             _("VLC could not open the decoder module.") );
150             return NULL;
151         }
152     }
153
154     if( !p_dec->p_module )
155     {
156         msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
157                  "VLC probably does not support this sound or video format.",
158                  (char*)&p_dec->fmt_in.i_codec );
159         intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module "
160             "for format"), _("VLC probably does not support the \"%4.4s\" "
161             "audio or video format. Unfortunately there is no way for you "
162             "to fix this."), (char*)&p_dec->fmt_in.i_codec );
163
164         DeleteDecoder( p_dec );
165         vlc_object_destroy( p_dec );
166         return NULL;
167     }
168
169     if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
170         !b_force_decoder )
171     {
172         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
173         p_dec->p_owner->b_own_thread = VLC_FALSE;
174     }
175     else
176     {
177         var_Get( p_input, "minimize-threads", &val );
178         p_dec->p_owner->b_own_thread = !val.b_bool;
179     }
180
181     if( p_dec->p_owner->b_own_thread )
182     {
183         int i_priority;
184         if( fmt->i_cat == AUDIO_ES )
185             i_priority = VLC_THREAD_PRIORITY_AUDIO;
186         else
187             i_priority = VLC_THREAD_PRIORITY_VIDEO;
188
189         /* Spawn the decoder thread */
190         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
191                                i_priority, VLC_FALSE ) )
192         {
193             msg_Err( p_dec, "cannot spawn decoder thread" );
194             module_Unneed( p_dec, p_dec->p_module );
195             DeleteDecoder( p_dec );
196             vlc_object_destroy( p_dec );
197             return NULL;
198         }
199     }
200
201     return p_dec;
202 }
203
204 /**
205  * Kills a decoder thread and waits until it's finished
206  *
207  * \param p_input the input thread
208  * \param p_es the es descriptor
209  * \return nothing
210  */
211 void input_DecoderDelete( decoder_t *p_dec )
212 {
213     vlc_object_kill( p_dec );
214
215     if( p_dec->p_owner->b_own_thread )
216     {
217         /* Make sure the thread leaves the function by
218          * sending it an empty block. */
219         block_t *p_block = block_New( p_dec, 0 );
220         input_DecoderDecode( p_dec, p_block );
221
222         vlc_thread_join( p_dec );
223
224         /* Don't module_Unneed() here because of the dll loader that wants
225          * close() in the same thread than open()/decode() */
226     }
227     else
228     {
229         /* Flush */
230         input_DecoderDecode( p_dec, NULL );
231
232         module_Unneed( p_dec, p_dec->p_module );
233     }
234
235     /* Delete decoder configuration */
236     DeleteDecoder( p_dec );
237
238     /* Delete the decoder */
239     vlc_object_destroy( p_dec );
240 }
241
242 /**
243  * Put a block_t in the decoder's fifo.
244  *
245  * \param p_dec the decoder object
246  * \param p_block the data block
247  */
248 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
249 {
250     if( p_dec->p_owner->b_own_thread )
251     {
252         if( p_dec->p_owner->p_input->p->b_out_pace_control )
253         {
254             /* FIXME !!!!! */
255             while( !p_dec->b_die && !p_dec->b_error &&
256                    p_dec->p_owner->p_fifo->i_depth > 10 )
257             {
258                 msleep( 1000 );
259             }
260         }
261         else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
262         {
263             /* FIXME: ideally we would check the time amount of data
264              * in the fifo instead of its size. */
265             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
266                       "consumed quickly enough), resetting fifo!" );
267             block_FifoEmpty( p_dec->p_owner->p_fifo );
268         }
269
270         block_FifoPut( p_dec->p_owner->p_fifo, p_block );
271     }
272     else
273     {
274         if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
275         {
276             if( p_block ) block_Release( p_block );
277         }
278         else
279         {
280             DecoderDecode( p_dec, p_block );
281         }
282     }
283 }
284
285 void input_DecoderDiscontinuity( decoder_t * p_dec, vlc_bool_t b_flush )
286 {
287     block_t *p_null;
288
289     /* Empty the fifo */
290     if( p_dec->p_owner->b_own_thread && b_flush )
291         block_FifoEmpty( p_dec->p_owner->p_fifo );
292
293     /* Send a special block */
294     p_null = block_New( p_dec, 128 );
295     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
296     /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
297     if( p_dec->p_owner->p_packetizer && b_flush )
298         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
299     memset( p_null->p_buffer, 0, p_null->i_buffer );
300
301     input_DecoderDecode( p_dec, p_null );
302 }
303
304 vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
305 {
306     if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
307     {
308         return VLC_FALSE;
309     }
310     return VLC_TRUE;
311 }
312
313 /**
314  * Create a decoder object
315  *
316  * \param p_input the input thread
317  * \param p_es the es descriptor
318  * \param i_object_type Object type as define in include/vlc_objects.h
319  * \return the decoder object
320  */
321 static decoder_t * CreateDecoder( input_thread_t *p_input,
322                                   es_format_t *fmt, int i_object_type )
323 {
324     decoder_t *p_dec;
325
326     p_dec = vlc_object_create( p_input, i_object_type );
327     if( p_dec == NULL )
328     {
329         msg_Err( p_input, "out of memory" );
330         return NULL;
331     }
332
333     p_dec->pf_decode_audio = 0;
334     p_dec->pf_decode_video = 0;
335     p_dec->pf_decode_sub = 0;
336     p_dec->pf_packetize = 0;
337
338    /* Initialize the decoder fifo */
339     p_dec->p_module = NULL;
340
341     memset( &null_es_format, 0, sizeof(es_format_t) );
342     es_format_Copy( &p_dec->fmt_in, fmt );
343     es_format_Copy( &p_dec->fmt_out, &null_es_format );
344
345     /* Allocate our private structure for the decoder */
346     p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
347     if( p_dec->p_owner == NULL )
348     {
349         msg_Err( p_dec, "out of memory" );
350         return NULL;
351     }
352     p_dec->p_owner->b_own_thread = VLC_TRUE;
353     p_dec->p_owner->i_preroll_end = -1;
354     p_dec->p_owner->p_input = p_input;
355     p_dec->p_owner->p_aout = NULL;
356     p_dec->p_owner->p_aout_input = NULL;
357     p_dec->p_owner->p_vout = NULL;
358     p_dec->p_owner->p_spu_vout = NULL;
359     p_dec->p_owner->i_spu_channel = 0;
360     p_dec->p_owner->p_sout = p_input->p->p_sout;
361     p_dec->p_owner->p_sout_input = NULL;
362     p_dec->p_owner->p_packetizer = NULL;
363
364     /* decoder fifo */
365     if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
366     {
367         msg_Err( p_dec, "out of memory" );
368         return NULL;
369     }
370
371     /* Set buffers allocation callbacks for the decoders */
372     p_dec->pf_aout_buffer_new = aout_new_buffer;
373     p_dec->pf_aout_buffer_del = aout_del_buffer;
374     p_dec->pf_vout_buffer_new = vout_new_buffer;
375     p_dec->pf_vout_buffer_del = vout_del_buffer;
376     p_dec->pf_picture_link    = vout_link_picture;
377     p_dec->pf_picture_unlink  = vout_unlink_picture;
378     p_dec->pf_spu_buffer_new  = spu_new_buffer;
379     p_dec->pf_spu_buffer_del  = spu_del_buffer;
380
381     vlc_object_attach( p_dec, p_input );
382
383     /* Find a suitable decoder/packetizer module */
384     if( i_object_type == VLC_OBJECT_DECODER )
385         p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
386     else
387         p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
388
389     /* Check if decoder requires already packetized data */
390     if( i_object_type == VLC_OBJECT_DECODER &&
391         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
392     {
393         p_dec->p_owner->p_packetizer =
394             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
395         if( p_dec->p_owner->p_packetizer )
396         {
397             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
398                             &p_dec->fmt_in );
399
400             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
401                             &null_es_format );
402
403             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
404
405             p_dec->p_owner->p_packetizer->p_module =
406                 module_Need( p_dec->p_owner->p_packetizer,
407                              "packetizer", "$packetizer", 0 );
408
409             if( !p_dec->p_owner->p_packetizer->p_module )
410             {
411                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
412                 vlc_object_detach( p_dec->p_owner->p_packetizer );
413                 vlc_object_destroy( p_dec->p_owner->p_packetizer );
414             }
415         }
416     }
417
418     return p_dec;
419 }
420
421 /**
422  * The decoding main loop
423  *
424  * \param p_dec the decoder
425  * \return 0
426  */
427 static int DecoderThread( decoder_t * p_dec )
428 {
429     block_t *p_block;
430
431     /* The decoder's main loop */
432     while( !p_dec->b_die && !p_dec->b_error )
433     {
434         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
435         {
436             p_dec->b_error = 1;
437             break;
438         }
439         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
440         {
441             break;
442         }
443     }
444
445     while( !p_dec->b_die )
446     {
447         /* Trash all received PES packets */
448         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
449         if( p_block ) block_Release( p_block );
450     }
451
452     /* We do it here because of the dll loader that wants close() in the
453      * same thread than open()/decode() */
454     module_Unneed( p_dec, p_dec->p_module );
455
456     return 0;
457 }
458
459 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
460 {
461     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
462         *pi_preroll = INT64_MAX;
463     else if( p->i_pts > 0 )
464         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
465     else if( p->i_dts > 0 )
466         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
467 }
468 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
469 {
470     input_thread_t *p_input = p_dec->p_owner->p_input;
471     aout_buffer_t *p_aout_buf;
472
473     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
474     {
475         vlc_mutex_lock( &p_input->p->counters.counters_lock );
476         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
477         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
478
479         if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
480         {
481             aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
482                                   p_dec->p_owner->p_aout_input, p_aout_buf );
483             continue;
484         }
485
486         if( p_dec->p_owner->i_preroll_end > 0 )
487         {
488             /* FIXME TODO flush audio output (don't know how to do that) */
489             msg_Dbg( p_dec, "End of audio preroll" );
490             p_dec->p_owner->i_preroll_end = -1;
491         }
492         aout_DecPlay( p_dec->p_owner->p_aout,
493                       p_dec->p_owner->p_aout_input,
494                       p_aout_buf );
495     }
496 }
497 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
498 {
499     vlc_mutex_lock( &p_vout->picture_lock );
500
501     if( p_pic->i_status == READY_PICTURE )
502     {
503         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
504         p_pic->date = 1;
505     }
506     else if( p_pic->i_refcount > 0 )
507     {
508         p_pic->i_status = DISPLAYED_PICTURE;
509     }
510     else
511     {
512         p_pic->i_status = DESTROYED_PICTURE;
513         p_vout->i_heap_size--;
514     }
515
516     vlc_mutex_unlock( &p_vout->picture_lock );
517 }
518 static void VoutFlushPicture( vout_thread_t *p_vout )
519 {
520     int i;
521     vlc_mutex_lock( &p_vout->picture_lock );
522     for( i = 0; i < p_vout->render.i_pictures; i++ )
523     {
524         picture_t *p_pic = p_vout->render.pp_picture[i];
525
526         if( p_pic->i_status != READY_PICTURE )
527         {
528             /* We cannot change picture status if it is in READY_PICTURE state,
529              * Just make sure they won't be displayed */
530             p_pic->date = 1;
531         }
532     }
533     vlc_mutex_unlock( &p_vout->picture_lock );
534 }
535 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
536 {
537     input_thread_t *p_input = p_dec->p_owner->p_input;
538     picture_t *p_pic;
539
540     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
541     {
542         vlc_mutex_lock( &p_input->p->counters.counters_lock );
543         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
544         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
545
546         if( p_pic->date < p_dec->p_owner->i_preroll_end )
547         {
548             VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
549             continue;
550         }
551
552         if( p_dec->p_owner->i_preroll_end > 0 )
553         {
554             msg_Dbg( p_dec, "End of video preroll" );
555             if( p_dec->p_owner->p_vout )
556                 VoutFlushPicture( p_dec->p_owner->p_vout );
557             /* */
558             p_dec->p_owner->i_preroll_end = -1;
559         }
560
561         vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
562                           p_pic->date );
563         vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
564     }
565 }
566
567 /**
568  * Decode a block
569  *
570  * \param p_dec the decoder object
571  * \param p_block the block to decode
572  * \return VLC_SUCCESS or an error code
573  */
574 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
575 {
576     const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
577
578     if( p_block && p_block->i_buffer <= 0 )
579     {
580         block_Release( p_block );
581         return VLC_SUCCESS;
582     }
583
584     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
585     {
586         block_t *p_sout_block;
587
588         while( ( p_sout_block =
589                      p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
590         {
591             if( !p_dec->p_owner->p_sout_input )
592             {
593                 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
594
595                 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
596                 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
597                 if( p_dec->fmt_in.psz_language )
598                 {
599                     if( p_dec->p_owner->sout.psz_language )
600                         free( p_dec->p_owner->sout.psz_language );
601                     p_dec->p_owner->sout.psz_language =
602                         strdup( p_dec->fmt_in.psz_language );
603                 }
604
605                 p_dec->p_owner->p_sout_input =
606                     sout_InputNew( p_dec->p_owner->p_sout,
607                                    &p_dec->p_owner->sout );
608
609                 if( p_dec->p_owner->p_sout_input == NULL )
610                 {
611                     msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
612                              (char *)&p_dec->p_owner->sout.i_codec );
613                     p_dec->b_error = VLC_TRUE;
614
615                     while( p_sout_block )
616                     {
617                         block_t *p_next = p_sout_block->p_next;
618                         block_Release( p_sout_block );
619                         p_sout_block = p_next;
620                     }
621                     break;
622                 }
623             }
624
625             while( p_sout_block )
626             {
627                 block_t *p_next = p_sout_block->p_next;
628
629                 p_sout_block->p_next = NULL;
630                 p_sout_block->i_rate = i_rate;
631
632                 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
633                                       p_sout_block );
634
635                 p_sout_block = p_next;
636             }
637
638             /* For now it's enough, as only sout inpact on this flag */
639             if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
640                 p_dec->p_owner->p_input->p->b_out_pace_control )
641             {
642                 msg_Dbg( p_dec, "switching to sync mode" );
643                 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
644             }
645             else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
646                      !p_dec->p_owner->p_input->p->b_out_pace_control )
647             {
648                 msg_Dbg( p_dec, "switching to async mode" );
649                 p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
650             }
651         }
652     }
653     else if( p_dec->fmt_in.i_cat == AUDIO_ES )
654     {
655         DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
656
657         if( p_dec->p_owner->p_packetizer )
658         {
659             block_t *p_packetized_block;
660             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
661
662             while( (p_packetized_block =
663                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
664             {
665                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
666                 {
667                     es_format_Clean( &p_dec->fmt_in );
668                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
669                 }
670
671                 while( p_packetized_block )
672                 {
673                     block_t *p_next = p_packetized_block->p_next;
674                     p_packetized_block->p_next = NULL;
675                     p_packetized_block->i_rate = i_rate;
676
677                     DecoderDecodeAudio( p_dec, p_packetized_block );
678
679                     p_packetized_block = p_next;
680                 }
681             }
682         }
683         else
684         {
685             DecoderDecodeAudio( p_dec, p_block );
686         }
687     }
688     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
689     {
690         DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
691
692         if( p_dec->p_owner->p_packetizer )
693         {
694             block_t *p_packetized_block;
695             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
696
697             while( (p_packetized_block =
698                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
699             {
700                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
701                 {
702                     es_format_Clean( &p_dec->fmt_in );
703                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
704                 }
705
706                 while( p_packetized_block )
707                 {
708                     block_t *p_next = p_packetized_block->p_next;
709                     p_packetized_block->p_next = NULL;
710                     p_packetized_block->i_rate = i_rate;
711
712                     DecoderDecodeVideo( p_dec, p_packetized_block );
713
714                     p_packetized_block = p_next;
715                 }
716             }
717         }
718         else
719         {
720             DecoderDecodeVideo( p_dec, p_block );
721         }
722     }
723     else if( p_dec->fmt_in.i_cat == SPU_ES )
724     {
725         input_thread_t *p_input = p_dec->p_owner->p_input;
726         vout_thread_t *p_vout;
727         subpicture_t *p_spu;
728
729         DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
730
731         while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
732         {
733             vlc_mutex_lock( &p_input->p->counters.counters_lock );
734             stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
735             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
736
737             if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
738                 ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
739             {
740                 spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
741                 continue;
742             }
743
744             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
745             if( p_vout )
746             {
747                 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
748                 vlc_object_release( p_vout );
749             }
750         }
751     }
752     else
753     {
754         msg_Err( p_dec, "unknown ES format" );
755         p_dec->b_error = 1;
756     }
757
758     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
759 }
760
761 /**
762  * Destroys a decoder object
763  *
764  * \param p_dec the decoder object
765  * \return nothing
766  */
767 static void DeleteDecoder( decoder_t * p_dec )
768 {
769     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
770              (char*)&p_dec->fmt_in.i_codec,
771              p_dec->p_owner->p_fifo->i_depth );
772
773     /* Free all packets still in the decoder fifo. */
774     block_FifoEmpty( p_dec->p_owner->p_fifo );
775     block_FifoRelease( p_dec->p_owner->p_fifo );
776
777     /* Cleanup */
778     if( p_dec->p_owner->p_aout_input )
779         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
780
781     if( p_dec->p_owner->p_vout )
782     {
783         int i_pic;
784
785 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
786         /* Hack to make sure all the the pictures are freed by the decoder */
787         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
788              i_pic++ )
789         {
790             if( p_pic->i_status == RESERVED_PICTURE )
791                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
792             if( p_pic->i_refcount > 0 )
793                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
794         }
795 #undef p_pic
796
797         /* We are about to die. Reattach video output to p_vlc. */
798         vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
799     }
800
801     if( p_dec->p_owner->p_sout_input )
802     {
803         sout_InputDelete( p_dec->p_owner->p_sout_input );
804         es_format_Clean( &p_dec->p_owner->sout );
805     }
806
807     if( p_dec->fmt_in.i_cat == SPU_ES )
808     {
809         vout_thread_t *p_vout;
810
811         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
812         if( p_vout )
813         {
814             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
815                          p_dec->p_owner->i_spu_channel );
816             vlc_object_release( p_vout );
817         }
818     }
819
820     es_format_Clean( &p_dec->fmt_in );
821     es_format_Clean( &p_dec->fmt_out );
822
823     if( p_dec->p_owner->p_packetizer )
824     {
825         module_Unneed( p_dec->p_owner->p_packetizer,
826                        p_dec->p_owner->p_packetizer->p_module );
827         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
828         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
829         vlc_object_detach( p_dec->p_owner->p_packetizer );
830         vlc_object_destroy( p_dec->p_owner->p_packetizer );
831     }
832
833     vlc_object_detach( p_dec );
834
835     free( p_dec->p_owner );
836 }
837
838 /*****************************************************************************
839  * Buffers allocation callbacks for the decoders
840  *****************************************************************************/
841 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
842 {
843     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
844     aout_buffer_t *p_buffer;
845
846     if( p_sys->p_aout_input != NULL &&
847         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
848           p_dec->fmt_out.audio.i_original_channels !=
849               p_sys->audio.i_original_channels ||
850           p_dec->fmt_out.audio.i_bytes_per_frame !=
851               p_sys->audio.i_bytes_per_frame ) )
852     {
853         /* Parameters changed, restart the aout */
854         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
855         p_sys->p_aout_input = NULL;
856     }
857
858     if( p_sys->p_aout_input == NULL )
859     {
860         audio_sample_format_t format;
861         int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
862
863         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
864         p_sys->audio = p_dec->fmt_out.audio;
865
866         memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
867         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
868                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
869         {
870             if ( i_force_dolby == 1 )
871             {
872                 format.i_original_channels = format.i_original_channels |
873                                              AOUT_CHAN_DOLBYSTEREO;
874             }
875             else /* i_force_dolby == 2 */
876             {
877                 format.i_original_channels = format.i_original_channels &
878                                              ~AOUT_CHAN_DOLBYSTEREO;
879             }
880         }
881
882         p_sys->p_aout_input =
883             aout_DecNew( p_dec, &p_sys->p_aout, &format );
884         if( p_sys->p_aout_input == NULL )
885         {
886             msg_Err( p_dec, "failed to create audio output" );
887             p_dec->b_error = VLC_TRUE;
888             return NULL;
889         }
890         p_dec->fmt_out.audio.i_bytes_per_frame =
891             p_sys->audio.i_bytes_per_frame;
892     }
893
894     p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
895                                   i_samples );
896
897     return p_buffer;
898 }
899
900 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
901 {
902     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
903                           p_dec->p_owner->p_aout_input, p_buffer );
904 }
905
906 static picture_t *vout_new_buffer( decoder_t *p_dec )
907 {
908     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
909     picture_t *p_pic;
910
911     if( p_sys->p_vout == NULL ||
912         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
913         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
914         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
915         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
916     {
917         if( !p_dec->fmt_out.video.i_width ||
918             !p_dec->fmt_out.video.i_height )
919         {
920             /* Can't create a new vout without display size */
921             return NULL;
922         }
923
924         if( !p_dec->fmt_out.video.i_visible_width ||
925             !p_dec->fmt_out.video.i_visible_height )
926         {
927             if( p_dec->fmt_in.video.i_visible_width &&
928                 p_dec->fmt_in.video.i_visible_height )
929             {
930                 p_dec->fmt_out.video.i_visible_width =
931                     p_dec->fmt_in.video.i_visible_width;
932                 p_dec->fmt_out.video.i_visible_height =
933                     p_dec->fmt_in.video.i_visible_height;
934             }
935             else
936             {
937                 p_dec->fmt_out.video.i_visible_width =
938                     p_dec->fmt_out.video.i_width;
939                 p_dec->fmt_out.video.i_visible_height =
940                     p_dec->fmt_out.video.i_height;
941             }
942         }
943
944         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
945             var_CreateGetBool( p_dec, "hdtv-fix" ) )
946         {
947             p_dec->fmt_out.video.i_visible_height = 1080;
948             p_dec->fmt_out.video.i_sar_num *= 135;
949             p_dec->fmt_out.video.i_sar_den *= 136;
950             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
951         }
952
953         if( !p_dec->fmt_out.video.i_sar_num ||
954             !p_dec->fmt_out.video.i_sar_den )
955         {
956             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
957               p_dec->fmt_out.video.i_visible_height;
958
959             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
960               p_dec->fmt_out.video.i_visible_width;
961         }
962
963         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
964                      &p_dec->fmt_out.video.i_sar_den,
965                      p_dec->fmt_out.video.i_sar_num,
966                      p_dec->fmt_out.video.i_sar_den, 50000 );
967
968         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
969         p_sys->video = p_dec->fmt_out.video;
970
971         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
972                                       &p_dec->fmt_out.video );
973         if( p_sys->p_vout == NULL )
974         {
975             msg_Err( p_dec, "failed to create video output" );
976             p_dec->b_error = VLC_TRUE;
977             return NULL;
978         }
979
980         if( p_sys->video.i_rmask )
981             p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
982         if( p_sys->video.i_gmask )
983             p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
984         if( p_sys->video.i_bmask )
985             p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
986     }
987
988     /* Get a new picture */
989     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
990     {
991         int i_pic, i_ready_pic = 0;
992
993         if( p_dec->b_die || p_dec->b_error )
994         {
995             return NULL;
996         }
997
998 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
999         /* Check the decoder doesn't leak pictures */
1000         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1001              i_pic++ )
1002         {
1003             if( p_pic->i_status == READY_PICTURE )
1004             {
1005                 if( i_ready_pic++ > 0 ) break;
1006                 else continue;
1007             }
1008
1009             if( p_pic->i_status != DISPLAYED_PICTURE &&
1010                 p_pic->i_status != RESERVED_PICTURE &&
1011                 p_pic->i_status != READY_PICTURE ) break;
1012
1013             if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1014                 break;
1015         }
1016         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1017         {
1018             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1019
1020             /* Just free all the pictures */
1021             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1022                  i_pic++ )
1023             {
1024                 if( p_pic->i_status == RESERVED_PICTURE )
1025                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1026                 if( p_pic->i_refcount > 0 )
1027                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1028             }
1029         }
1030 #undef p_pic
1031
1032         msleep( VOUT_OUTMEM_SLEEP );
1033     }
1034
1035     return p_pic;
1036 }
1037
1038 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1039 {
1040     VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1041 }
1042
1043 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1044 {
1045     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1046 }
1047
1048 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1049 {
1050     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1051 }
1052
1053 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1054 {
1055     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1056     vout_thread_t *p_vout = NULL;
1057     subpicture_t *p_subpic;
1058     int i_attempts = 30;
1059
1060     while( i_attempts-- )
1061     {
1062         if( p_dec->b_die || p_dec->b_error ) break;
1063
1064         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1065         if( p_vout ) break;
1066
1067         msleep( VOUT_DISPLAY_DELAY );
1068     }
1069
1070     if( !p_vout )
1071     {
1072         msg_Warn( p_dec, "no vout found, dropping subpicture" );
1073         return NULL;
1074     }
1075
1076     if( p_sys->p_spu_vout != p_vout )
1077     {
1078         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1079                      &p_sys->i_spu_channel );
1080         p_sys->p_spu_vout = p_vout;
1081     }
1082
1083     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1084     if( p_subpic )
1085     {
1086         p_subpic->i_channel = p_sys->i_spu_channel;
1087     }
1088
1089     vlc_object_release( p_vout );
1090
1091     return p_subpic;
1092 }
1093
1094 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1095 {
1096     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1097     vout_thread_t *p_vout = NULL;
1098
1099     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1100     if( !p_vout || p_sys->p_spu_vout != p_vout )
1101     {
1102         if( p_vout )
1103             vlc_object_release( p_vout );
1104         msg_Warn( p_dec, "no vout found, leaking subpicture" );
1105         return;
1106     }
1107
1108     spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1109
1110     vlc_object_release( p_vout );
1111 }
1112