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