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