]> git.sesse.net Git - vlc/blob - src/input/decoder.c
Added a new decoder function (decoder_GetDisplayDate) to be used to convert a
[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         DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
683
684         if( p_dec->p_owner->p_packetizer )
685         {
686             block_t *p_packetized_block;
687             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
688
689             while( (p_packetized_block =
690                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
691             {
692                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
693                 {
694                     es_format_Clean( &p_dec->fmt_in );
695                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
696                 }
697
698                 while( p_packetized_block )
699                 {
700                     block_t *p_next = p_packetized_block->p_next;
701                     p_packetized_block->p_next = NULL;
702                     p_packetized_block->i_rate = i_rate;
703
704                     DecoderDecodeAudio( p_dec, p_packetized_block );
705
706                     p_packetized_block = p_next;
707                 }
708             }
709         }
710         else
711         {
712             DecoderDecodeAudio( p_dec, p_block );
713         }
714     }
715     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
716     {
717         DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
718
719         if( p_dec->p_owner->p_packetizer )
720         {
721             block_t *p_packetized_block;
722             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
723
724             while( (p_packetized_block =
725                     p_packetizer->pf_packetize( p_packetizer, &p_block )) )
726             {
727                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
728                 {
729                     es_format_Clean( &p_dec->fmt_in );
730                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
731                 }
732
733                 while( p_packetized_block )
734                 {
735                     block_t *p_next = p_packetized_block->p_next;
736                     p_packetized_block->p_next = NULL;
737                     p_packetized_block->i_rate = i_rate;
738
739                     DecoderDecodeVideo( p_dec, p_packetized_block );
740
741                     p_packetized_block = p_next;
742                 }
743             }
744         }
745         else
746         {
747             DecoderDecodeVideo( p_dec, p_block );
748         }
749     }
750     else if( p_dec->fmt_in.i_cat == SPU_ES )
751     {
752         input_thread_t *p_input = p_dec->p_owner->p_input;
753         vout_thread_t *p_vout;
754         subpicture_t *p_spu;
755
756         DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
757
758         while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
759         {
760             vlc_mutex_lock( &p_input->p->counters.counters_lock );
761             stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
762             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
763
764             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
765             if( p_vout && p_sys->p_spu_vout == p_vout )
766             {
767                 /* Prerool does not work very well with subtitle */
768                 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
769                     ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
770                     spu_DestroySubpicture( p_vout->p_spu, p_spu );
771                 else
772                     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
773             }
774             else
775             {
776                 msg_Warn( p_dec, "no vout found, leaking subpicture" );
777             }
778             if( p_vout )
779                 vlc_object_release( p_vout );
780         }
781     }
782     else
783     {
784         msg_Err( p_dec, "unknown ES format" );
785         p_dec->b_error = 1;
786     }
787
788     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
789 }
790
791 /**
792  * Destroys a decoder object
793  *
794  * \param p_dec the decoder object
795  * \return nothing
796  */
797 static void DeleteDecoder( decoder_t * p_dec )
798 {
799     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
800              (char*)&p_dec->fmt_in.i_codec,
801              (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
802
803     /* Free all packets still in the decoder fifo. */
804     block_FifoEmpty( p_dec->p_owner->p_fifo );
805     block_FifoRelease( p_dec->p_owner->p_fifo );
806
807     /* Cleanup */
808     if( p_dec->p_owner->p_aout_input )
809         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
810
811     if( p_dec->p_owner->p_vout )
812     {
813         int i_pic;
814
815 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
816         /* Hack to make sure all the the pictures are freed by the decoder */
817         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
818              i_pic++ )
819         {
820             if( p_pic->i_status == RESERVED_PICTURE )
821                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
822             if( p_pic->i_refcount > 0 )
823                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
824         }
825 #undef p_pic
826
827         /* We are about to die. Reattach video output to p_vlc. */
828         vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
829     }
830
831     if( p_dec->p_owner->p_sout_input )
832     {
833         sout_InputDelete( p_dec->p_owner->p_sout_input );
834         es_format_Clean( &p_dec->p_owner->sout );
835     }
836
837     if( p_dec->fmt_in.i_cat == SPU_ES )
838     {
839         vout_thread_t *p_vout;
840
841         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
842         if( p_vout )
843         {
844             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
845                          p_dec->p_owner->i_spu_channel );
846             vlc_object_release( p_vout );
847         }
848     }
849
850     es_format_Clean( &p_dec->fmt_in );
851     es_format_Clean( &p_dec->fmt_out );
852
853     if( p_dec->p_owner->p_packetizer )
854     {
855         module_Unneed( p_dec->p_owner->p_packetizer,
856                        p_dec->p_owner->p_packetizer->p_module );
857         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
858         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
859         vlc_object_detach( p_dec->p_owner->p_packetizer );
860         vlc_object_destroy( p_dec->p_owner->p_packetizer );
861     }
862
863     vlc_object_detach( p_dec );
864
865     free( p_dec->p_owner );
866 }
867
868 /*****************************************************************************
869  * Buffers allocation callbacks for the decoders
870  *****************************************************************************/
871 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
872 {
873     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
874     aout_buffer_t *p_buffer;
875
876     if( p_sys->p_aout_input != NULL &&
877         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
878           p_dec->fmt_out.audio.i_original_channels !=
879               p_sys->audio.i_original_channels ||
880           p_dec->fmt_out.audio.i_bytes_per_frame !=
881               p_sys->audio.i_bytes_per_frame ) )
882     {
883         /* Parameters changed, restart the aout */
884         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
885         p_sys->p_aout_input = NULL;
886     }
887
888     if( p_sys->p_aout_input == NULL )
889     {
890         audio_sample_format_t format;
891         int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
892
893         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
894         p_sys->audio = p_dec->fmt_out.audio;
895
896         memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
897         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
898                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
899         {
900             if ( i_force_dolby == 1 )
901             {
902                 format.i_original_channels = format.i_original_channels |
903                                              AOUT_CHAN_DOLBYSTEREO;
904             }
905             else /* i_force_dolby == 2 */
906             {
907                 format.i_original_channels = format.i_original_channels &
908                                              ~AOUT_CHAN_DOLBYSTEREO;
909             }
910         }
911
912         p_sys->p_aout_input =
913             aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
914         if( p_sys->p_aout_input == NULL )
915         {
916             msg_Err( p_dec, "failed to create audio output" );
917             p_dec->b_error = VLC_TRUE;
918             return NULL;
919         }
920         p_dec->fmt_out.audio.i_bytes_per_frame =
921             p_sys->audio.i_bytes_per_frame;
922     }
923
924     p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
925                                   i_samples );
926
927     return p_buffer;
928 }
929
930 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
931 {
932     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
933                           p_dec->p_owner->p_aout_input, p_buffer );
934 }
935
936 static picture_t *vout_new_buffer( decoder_t *p_dec )
937 {
938     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
939     picture_t *p_pic;
940
941     if( p_sys->p_vout == NULL ||
942         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
943         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
944         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
945         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
946     {
947         if( !p_dec->fmt_out.video.i_width ||
948             !p_dec->fmt_out.video.i_height )
949         {
950             /* Can't create a new vout without display size */
951             return NULL;
952         }
953
954         if( !p_dec->fmt_out.video.i_visible_width ||
955             !p_dec->fmt_out.video.i_visible_height )
956         {
957             if( p_dec->fmt_in.video.i_visible_width &&
958                 p_dec->fmt_in.video.i_visible_height )
959             {
960                 p_dec->fmt_out.video.i_visible_width =
961                     p_dec->fmt_in.video.i_visible_width;
962                 p_dec->fmt_out.video.i_visible_height =
963                     p_dec->fmt_in.video.i_visible_height;
964             }
965             else
966             {
967                 p_dec->fmt_out.video.i_visible_width =
968                     p_dec->fmt_out.video.i_width;
969                 p_dec->fmt_out.video.i_visible_height =
970                     p_dec->fmt_out.video.i_height;
971             }
972         }
973
974         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
975             var_CreateGetBool( p_dec, "hdtv-fix" ) )
976         {
977             p_dec->fmt_out.video.i_visible_height = 1080;
978             p_dec->fmt_out.video.i_sar_num *= 135;
979             p_dec->fmt_out.video.i_sar_den *= 136;
980             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
981         }
982
983         if( !p_dec->fmt_out.video.i_sar_num ||
984             !p_dec->fmt_out.video.i_sar_den )
985         {
986             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
987               p_dec->fmt_out.video.i_visible_height;
988
989             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
990               p_dec->fmt_out.video.i_visible_width;
991         }
992
993         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
994                      &p_dec->fmt_out.video.i_sar_den,
995                      p_dec->fmt_out.video.i_sar_num,
996                      p_dec->fmt_out.video.i_sar_den, 50000 );
997
998         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
999         p_sys->video = p_dec->fmt_out.video;
1000
1001         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1002                                       &p_dec->fmt_out.video );
1003         if( p_sys->p_vout == NULL )
1004         {
1005             msg_Err( p_dec, "failed to create video output" );
1006             p_dec->b_error = VLC_TRUE;
1007             return NULL;
1008         }
1009
1010         if( p_sys->video.i_rmask )
1011             p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1012         if( p_sys->video.i_gmask )
1013             p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1014         if( p_sys->video.i_bmask )
1015             p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1016     }
1017
1018     /* Get a new picture */
1019     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1020     {
1021         int i_pic, i_ready_pic = 0;
1022
1023         if( p_dec->b_die || p_dec->b_error )
1024         {
1025             return NULL;
1026         }
1027
1028 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1029         /* Check the decoder doesn't leak pictures */
1030         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1031              i_pic++ )
1032         {
1033             if( p_pic->i_status == READY_PICTURE )
1034             {
1035                 if( i_ready_pic++ > 0 ) break;
1036                 else continue;
1037             }
1038
1039             if( p_pic->i_status != DISPLAYED_PICTURE &&
1040                 p_pic->i_status != RESERVED_PICTURE &&
1041                 p_pic->i_status != READY_PICTURE ) break;
1042
1043             if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1044                 break;
1045         }
1046         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1047         {
1048             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1049
1050             /* Just free all the pictures */
1051             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1052                  i_pic++ )
1053             {
1054                 if( p_pic->i_status == RESERVED_PICTURE )
1055                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1056                 if( p_pic->i_refcount > 0 )
1057                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1058             }
1059         }
1060 #undef p_pic
1061
1062         msleep( VOUT_OUTMEM_SLEEP );
1063     }
1064
1065     return p_pic;
1066 }
1067
1068 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1069 {
1070     VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1071 }
1072
1073 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1074 {
1075     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1076 }
1077
1078 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1079 {
1080     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1081 }
1082
1083 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1084 {
1085     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1086     vout_thread_t *p_vout = NULL;
1087     subpicture_t *p_subpic;
1088     int i_attempts = 30;
1089
1090     while( i_attempts-- )
1091     {
1092         if( p_dec->b_die || p_dec->b_error ) break;
1093
1094         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1095         if( p_vout ) break;
1096
1097         msleep( VOUT_DISPLAY_DELAY );
1098     }
1099
1100     if( !p_vout )
1101     {
1102         msg_Warn( p_dec, "no vout found, dropping subpicture" );
1103         return NULL;
1104     }
1105
1106     if( p_sys->p_spu_vout != p_vout )
1107     {
1108         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1109                      &p_sys->i_spu_channel );
1110         p_sys->p_spu_vout = p_vout;
1111     }
1112
1113     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1114     if( p_subpic )
1115     {
1116         p_subpic->i_channel = p_sys->i_spu_channel;
1117     }
1118
1119     vlc_object_release( p_vout );
1120
1121     return p_subpic;
1122 }
1123
1124 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1125 {
1126     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1127     vout_thread_t *p_vout = NULL;
1128
1129     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1130     if( !p_vout || p_sys->p_spu_vout != p_vout )
1131     {
1132         if( p_vout )
1133             vlc_object_release( p_vout );
1134         msg_Warn( p_dec, "no vout found, leaking subpicture" );
1135         return;
1136     }
1137
1138     spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1139
1140     vlc_object_release( p_vout );
1141 }
1142