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