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