]> git.sesse.net Git - vlc/blob - src/input/decoder.c
Privatized i_pts_delay (input_thread_t).
[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 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35
36 #include <vlc_block.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41 #include <vlc_osd.h>
42 #include <vlc_meta.h>
43
44 #include <vlc_interface.h>
45 #include "audio_output/aout_internal.h"
46 #include "stream_output/stream_output.h"
47 #include "input_internal.h"
48 #include "clock.h"
49 #include "decoder.h"
50 #include "event.h"
51 #include "ressource.h"
52
53 #include "../video_output/vout_control.h"
54
55 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
56 static void       DeleteDecoder( decoder_t * );
57
58 static void      *DecoderThread( vlc_object_t * );
59 static void       DecoderProcess( decoder_t *, block_t * );
60 static void       DecoderError( decoder_t *p_dec, block_t *p_block );
61 static void       DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
62 static void       DecoderFlush( decoder_t * );
63 static void       DecoderSignalBuffering( decoder_t *, bool );
64 static void       DecoderFlushBuffering( decoder_t * );
65
66 static void       DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
67
68 /* Buffers allocation callbacks for the decoders */
69 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
70 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
71
72 static picture_t *vout_new_buffer( decoder_t * );
73 static void vout_del_buffer( decoder_t *, picture_t * );
74 static void vout_link_picture( decoder_t *, picture_t * );
75 static void vout_unlink_picture( decoder_t *, picture_t * );
76
77 static subpicture_t *spu_new_buffer( decoder_t * );
78 static void spu_del_buffer( decoder_t *, subpicture_t * );
79
80 static es_format_t null_es_format;
81
82 struct decoder_owner_sys_t
83 {
84     int64_t         i_preroll_end;
85
86     input_thread_t  *p_input;
87     input_clock_t   *p_clock;
88     int             i_last_rate;
89
90     vout_thread_t   *p_spu_vout;
91     int              i_spu_channel;
92     int64_t          i_spu_order;
93
94     sout_instance_t         *p_sout;
95     sout_packetizer_input_t *p_sout_input;
96
97     /* Some decoders require already packetized data (ie. not truncated) */
98     decoder_t *p_packetizer;
99
100     /* Current format in use by the output */
101     video_format_t video;
102     audio_format_t audio;
103     es_format_t    sout;
104
105     /* */
106     bool           b_fmt_description;
107     es_format_t    fmt_description;
108     vlc_meta_t     *p_description;
109
110     /* fifo */
111     block_fifo_t *p_fifo;
112
113     /* Lock for communication with decoder thread */
114     vlc_mutex_t lock;
115     vlc_cond_t  wait;
116
117     /* -- These variables need locking on write(only) -- */
118     aout_instance_t *p_aout;
119     aout_input_t    *p_aout_input;
120
121     vout_thread_t   *p_vout;
122
123     /* -- Theses variables need locking on read *and* write -- */
124     /* */
125     /* Pause */
126     bool b_paused;
127     struct
128     {
129         mtime_t i_date;
130         int     i_ignore;
131     } pause;
132
133     /* Buffering */
134     bool b_buffering;
135     struct
136     {
137         bool b_first;
138         bool b_full;
139         int  i_count;
140
141         picture_t     *p_picture;
142         picture_t     **pp_picture_next;
143
144         subpicture_t  *p_subpic;
145         subpicture_t  **pp_subpic_next;
146
147         aout_buffer_t *p_audio;
148         aout_buffer_t **pp_audio_next;
149
150         block_t       *p_block;
151         block_t       **pp_block_next;
152     } buffer;
153
154     /* Flushing */
155     bool b_flushing;
156
157     /* CC */
158     struct
159     {
160         bool b_supported;
161         bool pb_present[4];
162         decoder_t *pp_decoder[4];
163     } cc;
164
165     /* Delay */
166     mtime_t i_ts_delay;
167 };
168
169 #define DECODER_MAX_BUFFERING_COUNT (4)
170 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
171 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
172
173 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
174  * a bogus PTS and won't be displayed */
175 #define DECODER_BOGUS_VIDEO_DELAY                ((mtime_t)(DEFAULT_PTS_DELAY * 30))
176
177 /* */
178 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
179
180
181 /*****************************************************************************
182  * Public functions
183  *****************************************************************************/
184 picture_t *decoder_NewPicture( decoder_t *p_decoder )
185 {
186     picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
187     if( !p_picture )
188         msg_Warn( p_decoder, "can't get output picture" );
189     return p_picture;
190 }
191 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
192 {
193     p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
194 }
195 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
196 {
197     p_decoder->pf_picture_link( p_decoder, p_picture );
198 }
199 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
200 {
201     p_decoder->pf_picture_unlink( p_decoder, p_picture );
202 }
203
204 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
205 {
206     if( !p_decoder->pf_aout_buffer_new )
207         return NULL;
208     return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
209 }
210 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
211 {
212     p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
213 }
214
215 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
216 {
217     subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
218     if( !p_subpicture )
219         msg_Warn( p_decoder, "can't get output subpicture" );
220     return p_subpicture;
221 }
222 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
223 {
224     p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
225 }
226
227 /* decoder_GetInputAttachments:
228  */
229 int decoder_GetInputAttachments( decoder_t *p_dec,
230                                  input_attachment_t ***ppp_attachment,
231                                  int *pi_attachment )
232 {
233     if( !p_dec->pf_get_attachments )
234         return VLC_EGENERIC;
235
236     return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
237 }
238 /* decoder_GetDisplayDate:
239  */
240 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
241 {
242     if( !p_dec->pf_get_display_date )
243         return 0;
244
245     return p_dec->pf_get_display_date( p_dec, i_ts );
246 }
247 /* decoder_GetDisplayRate:
248  */
249 int decoder_GetDisplayRate( decoder_t *p_dec )
250 {
251     if( !p_dec->pf_get_display_rate )
252         return INPUT_RATE_DEFAULT;
253
254     return p_dec->pf_get_display_rate( p_dec );
255 }
256
257 /**
258  * Spawns a new decoder thread
259  *
260  * \param p_input the input thread
261  * \param p_es the es descriptor
262  * \return the spawned decoder object
263  */
264 decoder_t *input_DecoderNew( input_thread_t *p_input,
265                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
266 {
267     decoder_t *p_dec = NULL;
268     int i_priority;
269
270 #ifdef ENABLE_SOUT
271     /* If we are in sout mode, search for packetizer module */
272     if( p_sout )
273     {
274         /* Create the decoder configuration structure */
275         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
276         if( p_dec == NULL )
277         {
278             msg_Err( p_input, "could not create packetizer" );
279             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
280                             _("VLC could not open the packetizer module.") );
281             return NULL;
282         }
283     }
284     else
285 #endif
286     {
287         /* Create the decoder configuration structure */
288         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
289         if( p_dec == NULL )
290         {
291             msg_Err( p_input, "could not create decoder" );
292             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
293                             _("VLC could not open the decoder module.") );
294             return NULL;
295         }
296     }
297
298     if( !p_dec->p_module )
299     {
300         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
301
302         DeleteDecoder( p_dec );
303         vlc_object_release( p_dec );
304         return NULL;
305     }
306
307     p_dec->p_owner->p_clock = p_clock;
308
309     if( fmt->i_cat == AUDIO_ES )
310         i_priority = VLC_THREAD_PRIORITY_AUDIO;
311     else
312         i_priority = VLC_THREAD_PRIORITY_VIDEO;
313
314     /* Spawn the decoder thread */
315     if( vlc_thread_create( p_dec, "decoder", DecoderThread,
316                            i_priority, false ) )
317     {
318         msg_Err( p_dec, "cannot spawn decoder thread" );
319         module_unneed( p_dec, p_dec->p_module );
320         DeleteDecoder( p_dec );
321         vlc_object_release( p_dec );
322         return NULL;
323     }
324
325     return p_dec;
326 }
327
328 /**
329  * Kills a decoder thread and waits until it's finished
330  *
331  * \param p_input the input thread
332  * \param p_es the es descriptor
333  * \return nothing
334  */
335 void input_DecoderDelete( decoder_t *p_dec )
336 {
337     decoder_owner_sys_t *p_owner = p_dec->p_owner;
338
339     vlc_object_kill( p_dec );
340
341     /* Make sure we aren't paused/buffering/waiting anymore */
342     vlc_mutex_lock( &p_owner->lock );
343     p_owner->b_paused = false;
344     p_owner->b_buffering = false;
345     p_owner->b_flushing = true;
346     vlc_cond_signal( &p_owner->wait );
347     vlc_mutex_unlock( &p_owner->lock );
348
349     vlc_thread_join( p_dec );
350     module_unneed( p_dec, p_dec->p_module );
351
352     /* */
353     if( p_dec->p_owner->cc.b_supported )
354     {
355         int i;
356         for( i = 0; i < 4; i++ )
357             input_DecoderSetCcState( p_dec, false, i );
358     }
359
360     /* Delete decoder configuration */
361     DeleteDecoder( p_dec );
362
363     /* Delete the decoder */
364     vlc_object_release( p_dec );
365 }
366
367 /**
368  * Put a block_t in the decoder's fifo.
369  * Thread-safe w.r.t. the decoder. May be a cancellation point.
370  *
371  * \param p_dec the decoder object
372  * \param p_block the data block
373  */
374 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block )
375 {
376     decoder_owner_sys_t *p_owner = p_dec->p_owner;
377
378     if( p_owner->p_input->p->b_out_pace_control )
379     {
380         /* The fifo is not consummed when buffering and so will
381          * deadlock vlc.
382          * There is no need to lock as b_buffering is never modify
383          * inside decoder thread. */
384         if( !p_owner->b_buffering )
385             block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
386     }
387     else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
388     {
389         /* FIXME: ideally we would check the time amount of data
390          * in the FIFO instead of its size. */
391         msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
392                   "consumed quickly enough), resetting fifo!" );
393         block_FifoEmpty( p_owner->p_fifo );
394     }
395
396     block_FifoPut( p_owner->p_fifo, p_block );
397 }
398
399 bool input_DecoderIsEmpty( decoder_t * p_dec )
400 {
401     assert( !p_dec->p_owner->b_buffering );
402
403     /* FIXME that's not really true */
404     return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
405 }
406
407 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
408 {
409     decoder_owner_sys_t *p_owner = p_dec->p_owner;
410     int i;
411
412     vlc_mutex_lock( &p_owner->lock );
413     for( i = 0; i < 4; i++ )
414         pb_present[i] =  p_owner->cc.pb_present[i];
415     vlc_mutex_unlock( &p_owner->lock );
416 }
417 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
418 {
419     decoder_owner_sys_t *p_owner = p_dec->p_owner;
420
421     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
422
423     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
424         return VLC_EGENERIC;
425
426     if( b_decode )
427     {
428         static const vlc_fourcc_t fcc[4] = {
429             VLC_FOURCC('c', 'c', '1', ' '),
430             VLC_FOURCC('c', 'c', '2', ' '),
431             VLC_FOURCC('c', 'c', '3', ' '),
432             VLC_FOURCC('c', 'c', '4', ' '),
433         };
434         decoder_t *p_cc;
435         es_format_t fmt;
436
437         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
438         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
439         if( !p_cc )
440         {
441             msg_Err( p_dec, "could not create decoder" );
442             intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
443                             _("VLC could not open the decoder module.") );
444             return VLC_EGENERIC;
445         }
446         else if( !p_cc->p_module )
447         {
448             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
449             DeleteDecoder( p_cc );
450             vlc_object_release( p_cc );
451             return VLC_EGENERIC;
452         }
453         p_cc->p_owner->p_clock = p_owner->p_clock;
454
455         vlc_mutex_lock( &p_owner->lock );
456         p_owner->cc.pp_decoder[i_channel] = p_cc;
457         vlc_mutex_unlock( &p_owner->lock );
458     }
459     else
460     {
461         decoder_t *p_cc;
462
463         vlc_mutex_lock( &p_owner->lock );
464         p_cc = p_owner->cc.pp_decoder[i_channel];
465         p_owner->cc.pp_decoder[i_channel] = NULL;
466         vlc_mutex_unlock( &p_owner->lock );
467
468         if( p_cc )
469         {
470             vlc_object_kill( p_cc );
471             module_unneed( p_cc, p_cc->p_module );
472             DeleteDecoder( p_cc );
473             vlc_object_release( p_cc );
474         }
475     }
476     return VLC_SUCCESS;
477 }
478 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
479 {
480     decoder_owner_sys_t *p_owner = p_dec->p_owner;
481
482     *pb_decode = false;
483     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
484         return VLC_EGENERIC;
485
486     vlc_mutex_lock( &p_owner->lock );
487     *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
488     vlc_mutex_unlock( &p_owner->lock );
489     return VLC_EGENERIC;
490 }
491
492 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
493 {
494     decoder_owner_sys_t *p_owner = p_dec->p_owner;
495
496     vlc_mutex_lock( &p_owner->lock );
497
498     assert( !p_owner->b_paused || !b_paused );
499
500     p_owner->b_paused = b_paused;
501     p_owner->pause.i_date = i_date;
502     p_owner->pause.i_ignore = 0;
503     vlc_cond_signal( &p_owner->wait );
504
505     DecoderOutputChangePause( p_dec, b_paused, i_date );
506
507     vlc_mutex_unlock( &p_owner->lock );
508 }
509
510 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
511 {
512     decoder_owner_sys_t *p_owner = p_dec->p_owner;
513
514     vlc_mutex_lock( &p_owner->lock );
515     p_owner->i_ts_delay = i_delay;
516     vlc_mutex_unlock( &p_owner->lock );
517 }
518
519 void input_DecoderStartBuffering( decoder_t *p_dec )
520 {
521     decoder_owner_sys_t *p_owner = p_dec->p_owner;
522
523     vlc_mutex_lock( &p_owner->lock );
524
525     DecoderFlush( p_dec );
526
527     p_owner->buffer.b_first = true;
528     p_owner->buffer.b_full = false;
529     p_owner->buffer.i_count = 0;
530
531     assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
532             !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
533
534     p_owner->buffer.p_picture = NULL;
535     p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
536
537     p_owner->buffer.p_subpic = NULL;
538     p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
539
540     p_owner->buffer.p_audio = NULL;
541     p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
542
543     p_owner->buffer.p_block = NULL;
544     p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
545
546
547     p_owner->b_buffering = true;
548
549     vlc_cond_signal( &p_owner->wait );
550
551     vlc_mutex_unlock( &p_owner->lock );
552 }
553
554 void input_DecoderStopBuffering( decoder_t *p_dec )
555 {
556     decoder_owner_sys_t *p_owner = p_dec->p_owner;
557
558     vlc_mutex_lock( &p_owner->lock );
559
560     p_owner->b_buffering = false;
561
562     vlc_cond_signal( &p_owner->wait );
563
564     vlc_mutex_unlock( &p_owner->lock );
565 }
566
567 void input_DecoderWaitBuffering( decoder_t *p_dec )
568 {
569     decoder_owner_sys_t *p_owner = p_dec->p_owner;
570
571     vlc_mutex_lock( &p_owner->lock );
572
573     while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
574     {
575         block_FifoWake( p_owner->p_fifo );
576         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
577     }
578
579     vlc_mutex_unlock( &p_owner->lock );
580 }
581
582 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
583 {
584     decoder_owner_sys_t *p_owner = p_dec->p_owner;
585
586     *pi_duration = 0;
587
588     vlc_mutex_lock( &p_owner->lock );
589     if( p_dec->fmt_in.i_cat == VIDEO_ES )
590     {
591         if( p_owner->b_paused && p_owner->p_vout )
592         {
593             vout_NextPicture( p_owner->p_vout, pi_duration );
594             p_owner->pause.i_ignore++;
595             vlc_cond_signal( &p_owner->wait );
596         }
597     }
598     else
599     {
600         /* TODO subtitle should not be flushed */
601         DecoderFlush( p_dec );
602     }
603     vlc_mutex_unlock( &p_owner->lock );
604 }
605
606 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
607 {
608     decoder_owner_sys_t *p_owner = p_dec->p_owner;
609     bool b_changed;
610
611     vlc_mutex_lock( &p_owner->lock );
612     b_changed = p_owner->b_fmt_description;
613     if( b_changed )
614     {
615         if( p_fmt )
616             es_format_Copy( p_fmt, &p_owner->fmt_description );
617
618         if( pp_meta )
619         {
620             *pp_meta = NULL;
621             if( p_owner->p_description )
622             {
623                 *pp_meta = vlc_meta_New();
624                 if( *pp_meta )
625                     vlc_meta_Merge( *pp_meta, p_owner->p_description );
626             }
627         }
628         p_owner->b_fmt_description = false;
629     }
630     vlc_mutex_unlock( &p_owner->lock );
631     return b_changed;
632 }
633
634 /*****************************************************************************
635  * Internal functions
636  *****************************************************************************/
637 static int DecoderGetInputAttachments( decoder_t *p_dec,
638                                        input_attachment_t ***ppp_attachment,
639                                        int *pi_attachment )
640 {
641     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
642                           ppp_attachment, pi_attachment );
643 }
644 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
645 {
646     decoder_owner_sys_t *p_owner = p_dec->p_owner;
647
648     vlc_mutex_lock( &p_owner->lock );
649     if( p_owner->b_buffering || p_owner->b_paused )
650         i_ts = 0;
651     vlc_mutex_unlock( &p_owner->lock );
652
653     if( !p_owner->p_clock || !i_ts )
654         return i_ts;
655
656     return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->p->i_pts_delay, i_ts );
657 }
658 static int DecoderGetDisplayRate( decoder_t *p_dec )
659 {
660     decoder_owner_sys_t *p_owner = p_dec->p_owner;
661
662     if( !p_owner->p_clock )
663         return INPUT_RATE_DEFAULT;
664     return input_clock_GetRate( p_owner->p_clock );
665 }
666
667 /* */
668 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
669 {
670     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
671              "VLC probably does not support this sound or video format.",
672              (char*)&codec );
673     intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
674                     _("VLC does not support the audio or video format \"%4.4s\". "
675                       "Unfortunately there is no way for you to fix this."), (char*)&codec );
676 }
677
678
679 /**
680  * Create a decoder object
681  *
682  * \param p_input the input thread
683  * \param p_es the es descriptor
684  * \param i_object_type Object type as define in include/vlc_objects.h
685  * \return the decoder object
686  */
687 static decoder_t * CreateDecoder( input_thread_t *p_input,
688                                   es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
689 {
690     decoder_t *p_dec;
691     decoder_owner_sys_t *p_owner;
692     int i;
693
694     p_dec = vlc_object_create( p_input, i_object_type );
695     if( p_dec == NULL )
696         return NULL;
697
698     p_dec->pf_decode_audio = NULL;
699     p_dec->pf_decode_video = NULL;
700     p_dec->pf_decode_sub = NULL;
701     p_dec->pf_get_cc = NULL;
702     p_dec->pf_packetize = NULL;
703
704     /* Initialize the decoder */
705     p_dec->p_module = NULL;
706
707     memset( &null_es_format, 0, sizeof(es_format_t) );
708     es_format_Copy( &p_dec->fmt_in, fmt );
709     es_format_Copy( &p_dec->fmt_out, &null_es_format );
710
711     p_dec->p_description = NULL;
712
713     /* Allocate our private structure for the decoder */
714     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
715     if( p_dec->p_owner == NULL )
716     {
717         vlc_object_release( p_dec );
718         return NULL;
719     }
720     p_dec->p_owner->i_preroll_end = -1;
721     p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
722     p_dec->p_owner->p_input = p_input;
723     p_dec->p_owner->p_aout = NULL;
724     p_dec->p_owner->p_aout_input = NULL;
725     p_dec->p_owner->p_vout = NULL;
726     p_dec->p_owner->p_spu_vout = NULL;
727     p_dec->p_owner->i_spu_channel = 0;
728     p_dec->p_owner->i_spu_order = 0;
729     p_dec->p_owner->p_sout = p_sout;
730     p_dec->p_owner->p_sout_input = NULL;
731     p_dec->p_owner->p_packetizer = NULL;
732
733     /* decoder fifo */
734     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
735     {
736         free( p_dec->p_owner );
737         vlc_object_release( p_dec );
738         return NULL;
739     }
740
741     /* Set buffers allocation callbacks for the decoders */
742     p_dec->pf_aout_buffer_new = aout_new_buffer;
743     p_dec->pf_aout_buffer_del = aout_del_buffer;
744     p_dec->pf_vout_buffer_new = vout_new_buffer;
745     p_dec->pf_vout_buffer_del = vout_del_buffer;
746     p_dec->pf_picture_link    = vout_link_picture;
747     p_dec->pf_picture_unlink  = vout_unlink_picture;
748     p_dec->pf_spu_buffer_new  = spu_new_buffer;
749     p_dec->pf_spu_buffer_del  = spu_del_buffer;
750     /* */
751     p_dec->pf_get_attachments  = DecoderGetInputAttachments;
752     p_dec->pf_get_display_date = DecoderGetDisplayDate;
753     p_dec->pf_get_display_rate = DecoderGetDisplayRate;
754
755     vlc_object_attach( p_dec, p_input );
756
757     /* Find a suitable decoder/packetizer module */
758     if( i_object_type == VLC_OBJECT_DECODER )
759         p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
760     else
761         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
762
763     /* Check if decoder requires already packetized data */
764     if( i_object_type == VLC_OBJECT_DECODER &&
765         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
766     {
767         p_dec->p_owner->p_packetizer =
768             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
769         if( p_dec->p_owner->p_packetizer )
770         {
771             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
772                             &p_dec->fmt_in );
773
774             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
775                             &null_es_format );
776
777             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
778
779             p_dec->p_owner->p_packetizer->p_module =
780                 module_need( p_dec->p_owner->p_packetizer,
781                              "packetizer", "$packetizer", false );
782
783             if( !p_dec->p_owner->p_packetizer->p_module )
784             {
785                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
786                 vlc_object_detach( p_dec->p_owner->p_packetizer );
787                 vlc_object_release( p_dec->p_owner->p_packetizer );
788             }
789         }
790     }
791
792     /* Copy ourself the input replay gain */
793     if( fmt->i_cat == AUDIO_ES )
794     {
795         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
796         {
797             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
798             {
799                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
800                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
801             }
802             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
803             {
804                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
805                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
806             }
807         }
808     }
809
810     /* */
811     vlc_mutex_init( &p_owner->lock );
812     vlc_cond_init( &p_owner->wait );
813
814     p_owner->b_fmt_description = false;
815     es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
816     p_owner->p_description = NULL;
817
818     p_owner->b_paused = false;
819     p_owner->pause.i_date = 0;
820     p_owner->pause.i_ignore = 0;
821
822     p_owner->b_buffering = false;
823     p_owner->buffer.b_first = true;
824     p_owner->buffer.b_full = false;
825     p_owner->buffer.i_count = 0;
826     p_owner->buffer.p_picture = NULL;
827     p_owner->buffer.p_subpic = NULL;
828     p_owner->buffer.p_audio = NULL;
829     p_owner->buffer.p_block = NULL;
830
831     p_owner->b_flushing = false;
832
833     /* */
834     p_owner->cc.b_supported = false;
835     if( i_object_type == VLC_OBJECT_DECODER )
836     {
837         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
838             p_owner->cc.b_supported = true;
839         if( p_dec->pf_get_cc )
840             p_owner->cc.b_supported = true;
841     }
842
843     for( i = 0; i < 4; i++ )
844     {
845         p_owner->cc.pb_present[i] = false;
846         p_owner->cc.pp_decoder[i] = NULL;
847     }
848     p_owner->i_ts_delay = 0;
849     return p_dec;
850 }
851
852 /**
853  * The decoding main loop
854  *
855  * \param p_dec the decoder
856  */
857 static void *DecoderThread( vlc_object_t *p_this )
858 {
859     decoder_t *p_dec = (decoder_t *)p_this;
860     decoder_owner_sys_t *p_owner = p_dec->p_owner;
861
862     /* The decoder's main loop */
863     for( ;; )
864     {
865         block_t *p_block = block_FifoGet( p_owner->p_fifo );
866
867         /* Make sure there is no cancellation point other than this one^^.
868          * If you need one, be sure to push cleanup of p_block. */
869         DecoderSignalBuffering( p_dec, p_block == NULL );
870
871         if( p_block )
872         {
873             int canc = vlc_savecancel();
874
875             if( p_dec->b_error )
876                 DecoderError( p_dec, p_block );
877             else
878                 DecoderProcess( p_dec, p_block );
879
880             vlc_restorecancel( canc );
881         }
882     }
883     return NULL;
884 }
885
886 static void DecoderFlush( decoder_t *p_dec )
887 {
888     decoder_owner_sys_t *p_owner = p_dec->p_owner;
889     block_t *p_null;
890
891     vlc_assert_locked( &p_owner->lock );
892
893     /* Empty the fifo */
894     block_FifoEmpty( p_owner->p_fifo );
895
896     /* Monitor for flush end */
897     p_owner->b_flushing = true;
898     vlc_cond_signal( &p_owner->wait );
899
900     /* Send a special block */
901     p_null = block_New( p_dec, 128 );
902     if( !p_null )
903         return;
904     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
905     p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
906     if( !p_dec->fmt_in.b_packetized )
907         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
908     memset( p_null->p_buffer, 0, p_null->i_buffer );
909
910     input_DecoderDecode( p_dec, p_null );
911
912     /* */
913     while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
914         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
915 }
916
917 static void DecoderSignalFlushed( decoder_t *p_dec )
918 {
919     decoder_owner_sys_t *p_owner = p_dec->p_owner;
920
921     vlc_mutex_lock( &p_owner->lock );
922
923     if( p_owner->b_flushing )
924     {
925         p_owner->b_flushing = false;
926         vlc_cond_signal( &p_owner->wait );
927     }
928
929     vlc_mutex_unlock( &p_owner->lock );
930 }
931
932 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
933 {
934     decoder_owner_sys_t *p_owner = p_dec->p_owner;
935
936     vlc_mutex_lock( &p_owner->lock );
937
938     if( p_owner->b_buffering )
939     {
940         if( b_full )
941             p_owner->buffer.b_full = true;
942         vlc_cond_signal( &p_owner->wait );
943     }
944
945     vlc_mutex_unlock( &p_owner->lock );
946 }
947
948 static bool DecoderIsFlushing( decoder_t *p_dec )
949 {
950     decoder_owner_sys_t *p_owner = p_dec->p_owner;
951     bool b_flushing;
952
953     vlc_mutex_lock( &p_owner->lock );
954
955     b_flushing = p_owner->b_flushing;
956
957     vlc_mutex_unlock( &p_owner->lock );
958
959     return b_flushing;
960 }
961
962 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
963 {
964     decoder_owner_sys_t *p_owner = p_dec->p_owner;
965
966     vlc_assert_locked( &p_owner->lock );
967
968     while( !p_owner->b_flushing )
969     {
970         if( p_owner->b_paused )
971         {
972             if( p_owner->b_buffering && !p_owner->buffer.b_full )
973                 break;
974             if( p_owner->pause.i_ignore > 0 )
975             {
976                 p_owner->pause.i_ignore--;
977                 break;
978             }
979         }
980         else
981         {
982             if( !p_owner->b_buffering || !p_owner->buffer.b_full )
983                 break;
984         }
985         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
986     }
987
988     if( pb_reject )
989         *pb_reject = p_owner->b_flushing;
990 }
991
992 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
993 {
994     decoder_owner_sys_t *p_owner = p_dec->p_owner;
995
996     vlc_assert_locked( &p_owner->lock );
997
998     /* XXX only audio and video output have to be paused.
999      * - for sout it is useless
1000      * - for subs, it is done by the vout
1001      */
1002     if( p_dec->fmt_in.i_cat == AUDIO_ES )
1003     {
1004         if( p_owner->p_aout && p_owner->p_aout_input )
1005             aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1006                                  b_paused, i_date );
1007     }
1008     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1009     {
1010         if( p_owner->p_vout )
1011             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1012     }
1013 }
1014 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1015 {
1016     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1017         *pi_preroll = INT64_MAX;
1018     else if( p->i_pts > 0 )
1019         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1020     else if( p->i_dts > 0 )
1021         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1022 }
1023
1024 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
1025 {
1026     mtime_t current_date = mdate();
1027
1028     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1029     if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
1030     {
1031         /* ETSI EN 300 472 Annex A : do not take into account the PTS
1032          * for teletext streams. */
1033         return current_date + 400000 + i_ts_delay;
1034     }
1035     return i_ts;
1036 }
1037
1038 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1039                           mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
1040 {
1041     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1042     input_clock_t   *p_clock = p_owner->p_clock;
1043     int i_rate = 0;
1044
1045     vlc_assert_locked( &p_owner->lock );
1046
1047     const mtime_t i_ts_delay = p_owner->p_input->p->i_pts_delay;
1048     const mtime_t i_es_delay = p_owner->i_ts_delay;
1049
1050     if( p_clock )
1051     {
1052         const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1053
1054         if( *pi_ts0 > 0 )
1055             *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1056                                          *pi_ts0 + i_es_delay );
1057         if( pi_ts1 && *pi_ts1 > 0 )
1058             *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
1059                                          *pi_ts1 + i_es_delay );
1060
1061         /* Do not create ephemere data because of rounding errors */
1062         if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1063             *pi_ts1 += 1;
1064
1065         if( i_rate <= 0 )
1066             i_rate = input_clock_GetRate( p_clock ); 
1067
1068         if( pi_duration )
1069             *pi_duration = ( *pi_duration * i_rate +
1070                                     INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1071
1072         if( pi_rate )
1073             *pi_rate = i_rate;
1074
1075         if( b_telx )
1076         {
1077             *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
1078             if( pi_ts1 && *pi_ts1 <= 0 )
1079                 *pi_ts1 = *pi_ts0;
1080         }
1081     }
1082     if( pi_delay )
1083     {
1084         const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
1085         *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
1086     }
1087 }
1088
1089 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1090                               int *pi_played_sum, int *pi_lost_sum )
1091 {
1092     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1093     aout_instance_t *p_aout = p_owner->p_aout;
1094     aout_input_t    *p_aout_input = p_owner->p_aout_input;
1095
1096     /* */
1097     if( p_audio->start_date <= 0 )
1098     {
1099         msg_Warn( p_dec, "non-dated audio buffer received" );
1100         *pi_lost_sum += 1;
1101         aout_BufferFree( p_audio );
1102         return;
1103     }
1104
1105     /* */
1106     vlc_mutex_lock( &p_owner->lock );
1107
1108     if( p_owner->b_buffering || p_owner->buffer.p_audio )
1109     {
1110         p_audio->p_next = NULL;
1111
1112         *p_owner->buffer.pp_audio_next = p_audio;
1113         p_owner->buffer.pp_audio_next = &p_audio->p_next;
1114
1115         p_owner->buffer.i_count++;
1116         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1117             p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1118         {
1119             p_owner->buffer.b_full = true;
1120             vlc_cond_signal( &p_owner->wait );
1121         }
1122     }
1123
1124     for( ;; )
1125     {
1126         bool b_has_more = false;
1127         bool b_reject;
1128         DecoderWaitUnblock( p_dec, &b_reject );
1129
1130         if( p_owner->b_buffering )
1131         {
1132             vlc_mutex_unlock( &p_owner->lock );
1133             return;
1134         }
1135
1136         /* */
1137         if( p_owner->buffer.p_audio )
1138         {
1139             p_audio = p_owner->buffer.p_audio;
1140
1141             p_owner->buffer.p_audio = p_audio->p_next;
1142             p_owner->buffer.i_count--;
1143
1144             b_has_more = p_owner->buffer.p_audio != NULL;
1145             if( !b_has_more )
1146                 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1147         }
1148
1149         /* */
1150         int i_rate = INPUT_RATE_DEFAULT;
1151         mtime_t i_delay;
1152
1153         DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1154                       &i_rate, &i_delay, false );
1155
1156         vlc_mutex_unlock( &p_owner->lock );
1157
1158         /* */
1159         const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1160
1161         if( !p_aout || !p_aout_input ||
1162             p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1163             i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1164             i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1165             b_reject = true;
1166
1167         /* Do not wait against unprotected date */
1168         const mtime_t i_deadline = p_audio->start_date - AOUT_MAX_PREPARE_TIME;
1169         while( !b_reject && i_deadline > mdate() )
1170         {
1171             vlc_mutex_lock( &p_owner->lock );
1172             if( p_owner->b_flushing || p_dec->b_die )
1173             {
1174                 b_reject = true;
1175                 vlc_mutex_unlock( &p_owner->lock );
1176                 break;
1177             }
1178             vlc_cond_timedwait( &p_owner->wait, &p_owner->lock, i_deadline );
1179             vlc_mutex_unlock( &p_owner->lock );
1180         }
1181
1182         if( !b_reject )
1183         {
1184             if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1185                 *pi_played_sum += 1;
1186             *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1187         }
1188         else
1189         {
1190             if( p_audio->start_date <= 0 )
1191             {
1192                 msg_Warn( p_dec, "non-dated audio buffer received" );
1193             }
1194             else if( p_audio->start_date > i_max_date )
1195             {
1196                 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1197                           p_audio->start_date - mdate() );
1198             }
1199             *pi_lost_sum += 1;
1200             aout_BufferFree( p_audio );
1201         }
1202
1203         if( !b_has_more )
1204             break;
1205
1206         vlc_mutex_lock( &p_owner->lock );
1207         if( !p_owner->buffer.p_audio )
1208         {
1209             vlc_mutex_unlock( &p_owner->lock );
1210             break;
1211         }
1212     }
1213 }
1214
1215 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1216 {
1217     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1218     input_thread_t  *p_input = p_owner->p_input;
1219     aout_buffer_t   *p_aout_buf;
1220     int i_decoded = 0;
1221     int i_lost = 0;
1222     int i_played = 0;
1223
1224     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1225     {
1226         aout_instance_t *p_aout = p_owner->p_aout;
1227         aout_input_t    *p_aout_input = p_owner->p_aout_input;
1228
1229         if( p_dec->b_die )
1230         {
1231             /* It prevent freezing VLC in case of broken decoder */
1232             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1233             if( p_block )
1234                 block_Release( p_block );
1235             break;
1236         }
1237         i_decoded++;
1238
1239         if( p_aout_buf->start_date < p_owner->i_preroll_end )
1240         {
1241             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1242             continue;
1243         }
1244
1245         if( p_owner->i_preroll_end > 0 )
1246         {
1247             msg_Dbg( p_dec, "End of audio preroll" );
1248             if( p_owner->p_aout && p_owner->p_aout_input )
1249                 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1250             /* */
1251             p_owner->i_preroll_end = -1;
1252         }
1253
1254         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1255     }
1256
1257     /* Update ugly stat */
1258     if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1259     {
1260         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1261
1262         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1263                              i_lost, NULL );
1264         stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1265                              i_played, NULL );
1266         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1267                              i_decoded, NULL );
1268
1269         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1270     }
1271 }
1272 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1273 {
1274     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1275     block_t *p_cc;
1276     bool pb_present[4];
1277     int i;
1278     int i_cc_decoder;
1279
1280     assert( p_dec_cc->pf_get_cc != NULL );
1281
1282     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1283     if( !p_owner->cc.b_supported )
1284         return;
1285
1286     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1287     if( !p_cc )
1288         return;
1289
1290     vlc_mutex_lock( &p_owner->lock );
1291     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1292     {
1293         p_owner->cc.pb_present[i] |= pb_present[i];
1294         if( p_owner->cc.pp_decoder[i] )
1295             i_cc_decoder++;
1296     }
1297
1298     for( i = 0; i < 4; i++ )
1299     {
1300         if( !p_owner->cc.pp_decoder[i] )
1301             continue;
1302
1303         if( i_cc_decoder > 1 )
1304             DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1305         else
1306             DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1307         i_cc_decoder--;
1308     }
1309     vlc_mutex_unlock( &p_owner->lock );
1310 }
1311
1312 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1313                               int *pi_played_sum, int *pi_lost_sum )
1314 {
1315     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1316     vout_thread_t  *p_vout = p_owner->p_vout;
1317     bool b_first_buffered;
1318
1319     if( p_picture->date <= 0 )
1320     {
1321         msg_Warn( p_vout, "non-dated video buffer received" );
1322         *pi_lost_sum += 1;
1323         vout_DropPicture( p_vout, p_picture );
1324         return;
1325     }
1326
1327     /* */
1328     vlc_mutex_lock( &p_owner->lock );
1329
1330     if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1331     {
1332         p_picture->p_next = NULL;
1333
1334         *p_owner->buffer.pp_picture_next = p_picture;
1335         p_owner->buffer.pp_picture_next = &p_picture->p_next;
1336
1337         p_owner->buffer.i_count++;
1338         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1339             p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1340         {
1341             p_owner->buffer.b_full = true;
1342             vlc_cond_signal( &p_owner->wait );
1343         }
1344     }
1345     b_first_buffered = p_owner->buffer.p_picture != NULL;
1346
1347     for( ;; b_first_buffered = false )
1348     {
1349         bool b_has_more = false;
1350
1351         bool b_reject;
1352
1353         DecoderWaitUnblock( p_dec, &b_reject );
1354
1355         if( p_owner->b_buffering && !p_owner->buffer.b_first )
1356         {
1357             vlc_mutex_unlock( &p_owner->lock );
1358             return;
1359         }
1360         bool b_buffering_first = p_owner->b_buffering;
1361
1362         /* */
1363         if( p_owner->buffer.p_picture )
1364         {
1365             p_picture = p_owner->buffer.p_picture;
1366
1367             p_owner->buffer.p_picture = p_picture->p_next;
1368             p_owner->buffer.i_count--;
1369
1370             b_has_more = p_owner->buffer.p_picture != NULL;
1371             if( !b_has_more )
1372                 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1373         }
1374
1375         /* */
1376         if( b_buffering_first )
1377         {
1378             assert( p_owner->buffer.b_first );
1379             assert( !p_owner->buffer.i_count );
1380             msg_Dbg( p_dec, "Received first picture" );
1381             p_owner->buffer.b_first = false;
1382             p_picture->b_force = true;
1383         }
1384
1385         int i_rate = INPUT_RATE_DEFAULT;
1386         mtime_t i_delay;
1387         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1388                       &i_rate, &i_delay, false );
1389
1390         vlc_mutex_unlock( &p_owner->lock );
1391
1392         /* */
1393         const mtime_t i_max_date = mdate() + i_delay + DECODER_BOGUS_VIDEO_DELAY;
1394
1395         if( !p_picture->b_force && ( p_picture->date <= 0 || p_picture->date >= i_max_date ) )
1396             b_reject = true;
1397
1398         if( !b_reject )
1399         {
1400             if( i_rate != p_owner->i_last_rate || b_first_buffered )
1401             {
1402                 /* Be sure to not display old picture after our own */
1403                 vout_Flush( p_vout, p_picture->date );
1404                 p_owner->i_last_rate = i_rate;
1405             }
1406             vout_DisplayPicture( p_vout, p_picture );
1407         }
1408         else
1409         {
1410             if( p_picture->date <= 0 )
1411             {
1412                 msg_Warn( p_vout, "non-dated video buffer received" );
1413             }
1414             else
1415             {
1416                 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1417                           p_picture->date - mdate() );
1418             }
1419             *pi_lost_sum += 1;
1420             vout_DropPicture( p_vout, p_picture );
1421         }
1422         int i_tmp_display;
1423         int i_tmp_lost;
1424         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1425
1426         *pi_played_sum += i_tmp_display;
1427         *pi_lost_sum += i_tmp_lost;
1428
1429         if( !b_has_more || b_buffering_first )
1430             break;
1431
1432         vlc_mutex_lock( &p_owner->lock );
1433         if( !p_owner->buffer.p_picture )
1434         {
1435             vlc_mutex_unlock( &p_owner->lock );
1436             break;
1437         }
1438     }
1439 }
1440
1441 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1442 {
1443     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1444     input_thread_t *p_input = p_owner->p_input;
1445     picture_t      *p_pic;
1446     int i_lost = 0;
1447     int i_decoded = 0;
1448     int i_displayed = 0;
1449
1450     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1451     {
1452         vout_thread_t  *p_vout = p_owner->p_vout;
1453         if( p_dec->b_die )
1454         {
1455             /* It prevent freezing VLC in case of broken decoder */
1456             vout_DropPicture( p_vout, p_pic );
1457             if( p_block )
1458                 block_Release( p_block );
1459             break;
1460         }
1461
1462         i_decoded++;
1463
1464         if( p_pic->date < p_owner->i_preroll_end )
1465         {
1466             vout_DropPicture( p_vout, p_pic );
1467             continue;
1468         }
1469
1470         if( p_owner->i_preroll_end > 0 )
1471         {
1472             msg_Dbg( p_dec, "End of video preroll" );
1473             if( p_vout )
1474                 vout_Flush( p_vout, 1 );
1475             /* */
1476             p_owner->i_preroll_end = -1;
1477         }
1478
1479         if( p_dec->pf_get_cc &&
1480             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1481             DecoderGetCc( p_dec, p_dec );
1482
1483         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1484     }
1485     if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1486     {
1487         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1488
1489         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1490                              i_decoded, NULL );
1491         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1492                              i_lost , NULL);
1493
1494         stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1495                              i_displayed, NULL);
1496
1497         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1498     }
1499 }
1500
1501 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1502                             bool b_telx )
1503 {
1504     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1505     vout_thread_t *p_vout = p_owner->p_spu_vout;
1506
1507     /* */
1508     if( p_subpic->i_start <= 0 )
1509     {
1510         msg_Warn( p_dec, "non-dated spu buffer received" );
1511         subpicture_Delete( p_subpic );
1512         return;
1513     }
1514
1515     /* */
1516     vlc_mutex_lock( &p_owner->lock );
1517
1518     if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1519     {
1520         p_subpic->p_next = NULL;
1521
1522         *p_owner->buffer.pp_subpic_next = p_subpic;
1523         p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1524
1525         p_owner->buffer.i_count++;
1526         /* XXX it is important to be full after the first one */
1527         if( p_owner->buffer.i_count > 0 )
1528         {
1529             p_owner->buffer.b_full = true;
1530             vlc_cond_signal( &p_owner->wait );
1531         }
1532     }
1533
1534     for( ;; )
1535     {
1536         bool b_has_more = false;
1537         bool b_reject;
1538         DecoderWaitUnblock( p_dec, &b_reject );
1539
1540         if( p_owner->b_buffering )
1541         {
1542             vlc_mutex_unlock( &p_owner->lock );
1543             return;
1544         }
1545
1546         /* */
1547         if( p_owner->buffer.p_subpic )
1548         {
1549             p_subpic = p_owner->buffer.p_subpic;
1550
1551             p_owner->buffer.p_subpic = p_subpic->p_next;
1552             p_owner->buffer.i_count--;
1553
1554             b_has_more = p_owner->buffer.p_subpic != NULL;
1555             if( !b_has_more )
1556                 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1557         }
1558
1559         /* */
1560         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1561                       NULL, NULL, b_telx );
1562
1563         vlc_mutex_unlock( &p_owner->lock );
1564
1565         if( !b_reject )
1566             spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1567         else
1568             subpicture_Delete( p_subpic );
1569
1570         if( !b_has_more )
1571             break;
1572         vlc_mutex_lock( &p_owner->lock );
1573         if( !p_owner->buffer.p_subpic )
1574         {
1575             vlc_mutex_unlock( &p_owner->lock );
1576             break;
1577         }
1578     }
1579 }
1580
1581 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1582                              bool b_telx )
1583 {
1584     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1585
1586     assert( p_owner->p_clock );
1587     assert( !p_sout_block->p_next );
1588
1589     vlc_mutex_lock( &p_owner->lock );
1590
1591     if( p_owner->b_buffering || p_owner->buffer.p_block )
1592     {
1593         block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1594
1595         p_owner->buffer.i_count++;
1596         /* XXX it is important to be full after the first one */
1597         if( p_owner->buffer.i_count > 0 )
1598         {
1599             p_owner->buffer.b_full = true;
1600             vlc_cond_signal( &p_owner->wait );
1601         }
1602     }
1603
1604     for( ;; )
1605     {
1606         bool b_has_more = false;
1607         bool b_reject;
1608         DecoderWaitUnblock( p_dec, &b_reject );
1609
1610         if( p_owner->b_buffering )
1611         {
1612             vlc_mutex_unlock( &p_owner->lock );
1613             return;
1614         }
1615
1616         /* */
1617         if( p_owner->buffer.p_block )
1618         {
1619             p_sout_block = p_owner->buffer.p_block;
1620
1621             p_owner->buffer.p_block = p_sout_block->p_next;
1622             p_owner->buffer.i_count--;
1623
1624             b_has_more = p_owner->buffer.p_block != NULL;
1625             if( !b_has_more )
1626                 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1627         }
1628         p_sout_block->p_next = NULL;
1629
1630         DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1631                       &p_sout_block->i_length,
1632                       &p_sout_block->i_rate, NULL, b_telx );
1633
1634         vlc_mutex_unlock( &p_owner->lock );
1635
1636         if( !b_reject )
1637             sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1638         else
1639             block_Release( p_sout_block );
1640
1641         if( !b_has_more )
1642             break;
1643         vlc_mutex_lock( &p_owner->lock );
1644         if( !p_owner->buffer.p_block )
1645         {
1646             vlc_mutex_unlock( &p_owner->lock );
1647             break;
1648         }
1649     }
1650 }
1651
1652 /* */
1653 static void DecoderFlushBuffering( decoder_t *p_dec )
1654 {
1655     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1656
1657     vlc_assert_locked( &p_owner->lock );
1658
1659     while( p_owner->buffer.p_picture )
1660     {
1661         picture_t *p_picture = p_owner->buffer.p_picture;
1662
1663         p_owner->buffer.p_picture = p_picture->p_next;
1664         p_owner->buffer.i_count--;
1665
1666         if( p_owner->p_vout )
1667             vout_DropPicture( p_owner->p_vout, p_picture );
1668
1669         if( !p_owner->buffer.p_picture )
1670             p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1671     }
1672     while( p_owner->buffer.p_audio )
1673     {
1674         aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1675
1676         p_owner->buffer.p_audio = p_audio->p_next;
1677         p_owner->buffer.i_count--;
1678
1679         aout_BufferFree( p_audio );
1680
1681         if( !p_owner->buffer.p_audio )
1682             p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1683     }
1684     while( p_owner->buffer.p_subpic )
1685     {
1686         subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1687
1688         p_owner->buffer.p_subpic = p_subpic->p_next;
1689         p_owner->buffer.i_count--;
1690
1691         subpicture_Delete( p_subpic );
1692
1693         if( !p_owner->buffer.p_subpic )
1694             p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1695     }
1696     if( p_owner->buffer.p_block )
1697     {
1698         block_ChainRelease( p_owner->buffer.p_block );
1699
1700         p_owner->buffer.i_count = 0;
1701         p_owner->buffer.p_block = NULL;
1702         p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1703     }
1704 }
1705
1706 /* This function process a block for sout
1707  */
1708 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1709 {
1710     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1711     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1712     block_t *p_sout_block;
1713
1714     while( ( p_sout_block =
1715                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1716     {
1717         if( !p_owner->p_sout_input )
1718         {
1719             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1720
1721             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1722             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1723             if( p_dec->fmt_in.psz_language )
1724             {
1725                 if( p_owner->sout.psz_language )
1726                     free( p_owner->sout.psz_language );
1727                 p_owner->sout.psz_language =
1728                     strdup( p_dec->fmt_in.psz_language );
1729             }
1730
1731             p_owner->p_sout_input =
1732                 sout_InputNew( p_owner->p_sout,
1733                                &p_owner->sout );
1734
1735             if( p_owner->p_sout_input == NULL )
1736             {
1737                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1738                          (char *)&p_owner->sout.i_codec );
1739                 p_dec->b_error = true;
1740
1741                 while( p_sout_block )
1742                 {
1743                     block_t *p_next = p_sout_block->p_next;
1744                     block_Release( p_sout_block );
1745                     p_sout_block = p_next;
1746                 }
1747                 break;
1748             }
1749         }
1750
1751         while( p_sout_block )
1752         {
1753             block_t *p_next = p_sout_block->p_next;
1754
1755             p_sout_block->p_next = NULL;
1756
1757             DecoderPlaySout( p_dec, p_sout_block, b_telx );
1758
1759             p_sout_block = p_next;
1760         }
1761
1762         /* For now it's enough, as only sout impact on this flag */
1763         if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1764             p_owner->p_input->p->b_out_pace_control )
1765         {
1766             msg_Dbg( p_dec, "switching to sync mode" );
1767             p_owner->p_input->p->b_out_pace_control = false;
1768         }
1769         else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1770                  !p_owner->p_input->p->b_out_pace_control )
1771         {
1772             msg_Dbg( p_dec, "switching to async mode" );
1773             p_owner->p_input->p->b_out_pace_control = true;
1774         }
1775     }
1776 }
1777
1778 /* This function process a video block
1779  */
1780 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1781 {
1782     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1783
1784     if( p_owner->p_packetizer )
1785     {
1786         block_t *p_packetized_block;
1787         decoder_t *p_packetizer = p_owner->p_packetizer;
1788
1789         while( (p_packetized_block =
1790                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1791         {
1792             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1793             {
1794                 es_format_Clean( &p_dec->fmt_in );
1795                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1796             }
1797             if( p_packetizer->pf_get_cc )
1798                 DecoderGetCc( p_dec, p_packetizer );
1799
1800             while( p_packetized_block )
1801             {
1802                 block_t *p_next = p_packetized_block->p_next;
1803                 p_packetized_block->p_next = NULL;
1804
1805                 DecoderDecodeVideo( p_dec, p_packetized_block );
1806
1807                 p_packetized_block = p_next;
1808             }
1809         }
1810     }
1811     else if( p_block )
1812     {
1813         DecoderDecodeVideo( p_dec, p_block );
1814     }
1815
1816     if( b_flush && p_owner->p_vout )
1817         vout_Flush( p_owner->p_vout, 1 );
1818 }
1819
1820 /* This function process a audio block
1821  */
1822 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1823 {
1824     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1825
1826     if( p_owner->p_packetizer )
1827     {
1828         block_t *p_packetized_block;
1829         decoder_t *p_packetizer = p_owner->p_packetizer;
1830
1831         while( (p_packetized_block =
1832                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1833         {
1834             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1835             {
1836                 es_format_Clean( &p_dec->fmt_in );
1837                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1838             }
1839
1840             while( p_packetized_block )
1841             {
1842                 block_t *p_next = p_packetized_block->p_next;
1843                 p_packetized_block->p_next = NULL;
1844
1845                 DecoderDecodeAudio( p_dec, p_packetized_block );
1846
1847                 p_packetized_block = p_next;
1848             }
1849         }
1850     }
1851     else if( p_block )
1852     {
1853         DecoderDecodeAudio( p_dec, p_block );
1854     }
1855
1856     if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1857         aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1858 }
1859
1860 /* This function process a subtitle block
1861  */
1862 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1863 {
1864     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1865     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1866
1867     input_thread_t *p_input = p_owner->p_input;
1868     vout_thread_t *p_vout;
1869     subpicture_t *p_spu;
1870
1871     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1872     {
1873         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1874         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1875         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1876
1877         p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1878         if( p_vout && p_owner->p_spu_vout == p_vout )
1879         {
1880             /* Preroll does not work very well with subtitle */
1881             if( p_spu->i_start > 0 &&
1882                 p_spu->i_start < p_owner->i_preroll_end &&
1883                 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1884             {
1885                 subpicture_Delete( p_spu );
1886             }
1887             else
1888             {
1889                 DecoderPlaySpu( p_dec, p_spu, b_telx );
1890             }
1891         }
1892         else
1893         {
1894             subpicture_Delete( p_spu );
1895         }
1896         if( p_vout )
1897             vlc_object_release( p_vout );
1898     }
1899
1900     if( b_flush && p_owner->p_spu_vout )
1901     {
1902         p_vout = input_ressource_HoldVout( p_input->p->p_ressource );
1903
1904         if( p_vout && p_owner->p_spu_vout == p_vout )
1905             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1906                          p_owner->i_spu_channel );
1907
1908         if( p_vout )
1909             vlc_object_release( p_vout );
1910     }
1911 }
1912
1913 /* */
1914 static void DecoderProcessOnFlush( decoder_t *p_dec )
1915 {
1916     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1917
1918     vlc_mutex_lock( &p_owner->lock );
1919     DecoderFlushBuffering( p_dec );
1920     vlc_mutex_unlock( &p_owner->lock );
1921
1922     DecoderSignalFlushed( p_dec );
1923 }
1924 /**
1925  * Decode a block
1926  *
1927  * \param p_dec the decoder object
1928  * \param p_block the block to decode
1929  * \return VLC_SUCCESS or an error code
1930  */
1931 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1932 {
1933     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1934     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1935
1936     if( p_block && p_block->i_buffer <= 0 )
1937     {
1938         assert( !b_flush_request );
1939         block_Release( p_block );
1940         return;
1941     }
1942
1943 #ifdef ENABLE_SOUT
1944     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1945     {
1946         if( p_block )
1947             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1948
1949         DecoderProcessSout( p_dec, p_block );
1950     }
1951     else
1952 #endif
1953     {
1954         bool b_flush = false;
1955
1956         if( p_block )
1957         {
1958             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1959             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1960
1961             b_flush = !b_flushing && b_flush_request;
1962
1963             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1964         }
1965
1966         if( p_dec->fmt_in.i_cat == AUDIO_ES )
1967         {
1968             DecoderProcessAudio( p_dec, p_block, b_flush );
1969         }
1970         else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1971         {
1972             DecoderProcessVideo( p_dec, p_block, b_flush );
1973         }
1974         else if( p_dec->fmt_in.i_cat == SPU_ES )
1975         {
1976             DecoderProcessSpu( p_dec, p_block, b_flush );
1977         }
1978         else
1979         {
1980             msg_Err( p_dec, "unknown ES format" );
1981             p_dec->b_error = true;
1982         }
1983     }
1984
1985     /* */
1986     if( b_flush_request )
1987         DecoderProcessOnFlush( p_dec );
1988 }
1989
1990 static void DecoderError( decoder_t *p_dec, block_t *p_block )
1991 {
1992     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1993
1994     /* */
1995     if( p_block )
1996         block_Release( p_block );
1997
1998     if( b_flush_request )
1999         DecoderProcessOnFlush( p_dec );
2000 }
2001
2002
2003 /**
2004  * Destroys a decoder object
2005  *
2006  * \param p_dec the decoder object
2007  * \return nothing
2008  */
2009 static void DeleteDecoder( decoder_t * p_dec )
2010 {
2011     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2012
2013     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2014              (char*)&p_dec->fmt_in.i_codec,
2015              (unsigned)block_FifoCount( p_owner->p_fifo ) );
2016
2017     /* Free all packets still in the decoder fifo. */
2018     block_FifoEmpty( p_owner->p_fifo );
2019     block_FifoRelease( p_owner->p_fifo );
2020
2021     /* */
2022     vlc_mutex_lock( &p_owner->lock );
2023     DecoderFlushBuffering( p_dec );
2024     vlc_mutex_unlock( &p_owner->lock );
2025
2026     /* Cleanup */
2027     if( p_owner->p_aout_input )
2028         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2029     if( p_owner->p_aout )
2030     {
2031         input_ressource_RequestAout( p_owner->p_input->p->p_ressource,
2032                                      p_owner->p_aout );
2033         input_SendEventAout( p_owner->p_input );
2034         p_owner->p_aout = NULL;
2035     }
2036     if( p_owner->p_vout )
2037     {
2038         /* Hack to make sure all the the pictures are freed by the decoder */
2039         vout_FixLeaks( p_owner->p_vout, true );
2040
2041         /* */
2042         input_ressource_RequestVout( p_owner->p_input->p->p_ressource, p_owner->p_vout, NULL );
2043         input_SendEventVout( p_owner->p_input );
2044     }
2045
2046 #ifdef ENABLE_SOUT
2047     if( p_owner->p_sout_input )
2048     {
2049         sout_InputDelete( p_owner->p_sout_input );
2050         es_format_Clean( &p_owner->sout );
2051     }
2052 #endif
2053
2054     if( p_dec->fmt_in.i_cat == SPU_ES )
2055     {
2056         vout_thread_t *p_vout;
2057
2058         p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2059         if( p_vout )
2060         {
2061             if( p_owner->p_spu_vout == p_vout )
2062                 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2063             vlc_object_release( p_vout );
2064         }
2065     }
2066
2067     es_format_Clean( &p_dec->fmt_in );
2068     es_format_Clean( &p_dec->fmt_out );
2069     if( p_dec->p_description )
2070         vlc_meta_Delete( p_dec->p_description );
2071     es_format_Clean( &p_owner->fmt_description );
2072     if( p_owner->p_description )
2073         vlc_meta_Delete( p_owner->p_description );
2074
2075     if( p_owner->p_packetizer )
2076     {
2077         module_unneed( p_owner->p_packetizer,
2078                        p_owner->p_packetizer->p_module );
2079         es_format_Clean( &p_owner->p_packetizer->fmt_in );
2080         es_format_Clean( &p_owner->p_packetizer->fmt_out );
2081         if( p_owner->p_packetizer->p_description )
2082             vlc_meta_Delete( p_owner->p_packetizer->p_description );
2083         vlc_object_detach( p_owner->p_packetizer );
2084         vlc_object_release( p_owner->p_packetizer );
2085     }
2086
2087     vlc_cond_destroy( &p_owner->wait );
2088     vlc_mutex_destroy( &p_owner->lock );
2089
2090     vlc_object_detach( p_dec );
2091
2092     free( p_owner );
2093 }
2094
2095 /*****************************************************************************
2096  * Buffers allocation callbacks for the decoders
2097  *****************************************************************************/
2098 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2099 {
2100     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2101
2102     vlc_assert_locked( &p_owner->lock );
2103
2104     p_owner->b_fmt_description = true;
2105
2106     /* Copy es_format */
2107     es_format_Clean( &p_owner->fmt_description );
2108     es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2109
2110     /* Move p_description */
2111     if( p_owner->p_description && p_dec->p_description )
2112         vlc_meta_Delete( p_owner->p_description );
2113     p_owner->p_description = p_dec->p_description;
2114     p_dec->p_description = NULL;
2115 }
2116 static vout_thread_t *aout_request_vout( void *p_private,
2117                                          vout_thread_t *p_vout, video_format_t *p_fmt )
2118 {
2119     decoder_t *p_dec = p_private;
2120
2121     p_vout = input_ressource_RequestVout( p_dec->p_owner->p_input->p->p_ressource, p_vout, p_fmt );
2122     input_SendEventVout( p_dec->p_owner->p_input );
2123
2124     return p_vout;
2125 }
2126
2127 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2128 {
2129     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2130     aout_buffer_t *p_buffer;
2131
2132     if( p_owner->p_aout_input != NULL &&
2133         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2134           p_dec->fmt_out.audio.i_original_channels !=
2135               p_owner->audio.i_original_channels ||
2136           p_dec->fmt_out.audio.i_bytes_per_frame !=
2137               p_owner->audio.i_bytes_per_frame ) )
2138     {
2139         aout_input_t *p_aout_input = p_owner->p_aout_input;
2140
2141         /* Parameters changed, restart the aout */
2142         vlc_mutex_lock( &p_owner->lock );
2143
2144         DecoderFlushBuffering( p_dec );
2145
2146         p_owner->p_aout_input = NULL;
2147         aout_DecDelete( p_owner->p_aout, p_aout_input );
2148
2149         vlc_mutex_unlock( &p_owner->lock );
2150     }
2151
2152     if( p_owner->p_aout_input == NULL )
2153     {
2154         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
2155         audio_sample_format_t format;
2156         aout_input_t *p_aout_input;
2157         aout_instance_t *p_aout;
2158         aout_request_vout_t request_vout;
2159
2160         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2161         p_owner->audio = p_dec->fmt_out.audio;
2162
2163         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2164         if( i_force_dolby &&
2165             (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2166                 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2167         {
2168             if( i_force_dolby == 1 )
2169             {
2170                 format.i_original_channels = format.i_original_channels |
2171                                              AOUT_CHAN_DOLBYSTEREO;
2172             }
2173             else /* i_force_dolby == 2 */
2174             {
2175                 format.i_original_channels = format.i_original_channels &
2176                                              ~AOUT_CHAN_DOLBYSTEREO;
2177             }
2178         }
2179
2180         request_vout.pf_request_vout = aout_request_vout;
2181         request_vout.p_private = p_dec;
2182
2183         p_aout = p_owner->p_aout;
2184         if( !p_aout )
2185             p_aout = input_ressource_RequestAout( p_owner->p_input->p->p_ressource, NULL );
2186         p_aout_input = aout_DecNew( p_dec, &p_aout,
2187                                     &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2188
2189         vlc_mutex_lock( &p_owner->lock );
2190
2191         p_owner->p_aout = p_aout;
2192         p_owner->p_aout_input = p_aout_input;
2193         DecoderUpdateFormatLocked( p_dec );
2194
2195         vlc_mutex_unlock( &p_owner->lock );
2196
2197         input_SendEventAout( p_owner->p_input );
2198
2199         if( p_owner->p_aout_input == NULL )
2200         {
2201             msg_Err( p_dec, "failed to create audio output" );
2202             p_dec->b_error = true;
2203             return NULL;
2204         }
2205         p_dec->fmt_out.audio.i_bytes_per_frame =
2206             p_owner->audio.i_bytes_per_frame;
2207     }
2208
2209     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2210
2211     return p_buffer;
2212 }
2213
2214 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2215 {
2216     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2217
2218     aout_DecDeleteBuffer( p_owner->p_aout,
2219                           p_owner->p_aout_input, p_buffer );
2220 }
2221
2222
2223 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2224
2225 static picture_t *vout_new_buffer( decoder_t *p_dec )
2226 {
2227     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2228
2229     if( p_owner->p_vout == NULL ||
2230         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2231         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2232         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
2233         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
2234     {
2235         vout_thread_t *p_vout;
2236
2237         if( !p_dec->fmt_out.video.i_width ||
2238             !p_dec->fmt_out.video.i_height )
2239         {
2240             /* Can't create a new vout without display size */
2241             return NULL;
2242         }
2243
2244         if( !p_dec->fmt_out.video.i_visible_width ||
2245             !p_dec->fmt_out.video.i_visible_height )
2246         {
2247             if( p_dec->fmt_in.video.i_visible_width &&
2248                 p_dec->fmt_in.video.i_visible_height )
2249             {
2250                 p_dec->fmt_out.video.i_visible_width =
2251                     p_dec->fmt_in.video.i_visible_width;
2252                 p_dec->fmt_out.video.i_visible_height =
2253                     p_dec->fmt_in.video.i_visible_height;
2254             }
2255             else
2256             {
2257                 p_dec->fmt_out.video.i_visible_width =
2258                     p_dec->fmt_out.video.i_width;
2259                 p_dec->fmt_out.video.i_visible_height =
2260                     p_dec->fmt_out.video.i_height;
2261             }
2262         }
2263
2264         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2265             var_CreateGetBool( p_dec, "hdtv-fix" ) )
2266         {
2267             p_dec->fmt_out.video.i_visible_height = 1080;
2268             p_dec->fmt_out.video.i_sar_num *= 135;
2269             p_dec->fmt_out.video.i_sar_den *= 136;
2270             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2271         }
2272
2273         if( !p_dec->fmt_out.video.i_sar_num ||
2274             !p_dec->fmt_out.video.i_sar_den )
2275         {
2276             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2277               p_dec->fmt_out.video.i_visible_height;
2278
2279             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2280               p_dec->fmt_out.video.i_visible_width;
2281         }
2282
2283         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2284                      &p_dec->fmt_out.video.i_sar_den,
2285                      p_dec->fmt_out.video.i_sar_num,
2286                      p_dec->fmt_out.video.i_sar_den, 50000 );
2287
2288         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2289         p_owner->video = p_dec->fmt_out.video;
2290
2291         vlc_mutex_lock( &p_owner->lock );
2292
2293         DecoderFlushBuffering( p_dec );
2294
2295         p_vout = p_owner->p_vout;
2296         p_owner->p_vout = NULL;
2297         vlc_mutex_unlock( &p_owner->lock );
2298
2299         p_vout = input_ressource_RequestVout( p_owner->p_input->p->p_ressource,
2300                                               p_vout, &p_dec->fmt_out.video );
2301
2302         vlc_mutex_lock( &p_owner->lock );
2303         p_owner->p_vout = p_vout;
2304
2305         DecoderUpdateFormatLocked( p_dec );
2306
2307         vlc_mutex_unlock( &p_owner->lock );
2308
2309         input_SendEventVout( p_owner->p_input );
2310         if( p_vout == NULL )
2311         {
2312             msg_Err( p_dec, "failed to create video output" );
2313             p_dec->b_error = true;
2314             return NULL;
2315         }
2316
2317         if( p_owner->video.i_rmask )
2318             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2319         if( p_owner->video.i_gmask )
2320             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2321         if( p_owner->video.i_bmask )
2322             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2323     }
2324
2325     /* Get a new picture
2326      */
2327     for( ;; )
2328     {
2329         picture_t *p_picture;
2330
2331         if( p_dec->b_die || p_dec->b_error )
2332             return NULL;
2333
2334         /* The video filter chain required that there is always 1 free buffer
2335          * that it will use as temporary one. It will release the temporary
2336          * buffer once its work is done, so this check is safe even if we don't
2337          * lock around both count() and create().
2338          */
2339         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2340         {
2341             p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2342             if( p_picture )
2343                 return p_picture;
2344         }
2345
2346         if( DecoderIsFlushing( p_dec ) )
2347             return NULL;
2348
2349         /* */
2350         DecoderSignalBuffering( p_dec, true );
2351
2352         /* Check the decoder doesn't leak pictures */
2353         vout_FixLeaks( p_owner->p_vout, false );
2354
2355         /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2356         msleep( VOUT_OUTMEM_SLEEP );
2357     }
2358 }
2359
2360 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2361 {
2362     vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2363 }
2364
2365 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2366 {
2367     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2368 }
2369
2370 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2371 {
2372     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2373 }
2374
2375 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2376 {
2377     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2378     vout_thread_t *p_vout = NULL;
2379     subpicture_t *p_subpic;
2380     int i_attempts = 30;
2381
2382     while( i_attempts-- )
2383     {
2384         if( p_dec->b_die || p_dec->b_error )
2385             break;
2386
2387         p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2388         if( p_vout )
2389             break;
2390
2391         msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2392     }
2393
2394     if( !p_vout )
2395     {
2396         msg_Warn( p_dec, "no vout found, dropping subpicture" );
2397         return NULL;
2398     }
2399
2400     if( p_owner->p_spu_vout != p_vout )
2401     {
2402         vlc_mutex_lock( &p_owner->lock );
2403
2404         DecoderFlushBuffering( p_dec );
2405
2406         vlc_mutex_unlock( &p_owner->lock );
2407
2408         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2409                      &p_owner->i_spu_channel );
2410         p_owner->i_spu_order = 0;
2411         p_owner->p_spu_vout = p_vout;
2412     }
2413
2414     p_subpic = subpicture_New();
2415     if( p_subpic )
2416     {
2417         p_subpic->i_channel = p_owner->i_spu_channel;
2418         p_subpic->i_order = p_owner->i_spu_order++;
2419         p_subpic->b_subtitle = true;
2420     }
2421
2422     vlc_object_release( p_vout );
2423
2424     return p_subpic;
2425 }
2426
2427 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2428 {
2429     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2430     vout_thread_t *p_vout = NULL;
2431
2432     p_vout = input_ressource_HoldVout( p_owner->p_input->p->p_ressource );
2433     if( !p_vout || p_owner->p_spu_vout != p_vout )
2434     {
2435         if( p_vout )
2436             vlc_object_release( p_vout );
2437         msg_Warn( p_dec, "no vout found, leaking subpicture" );
2438         return;
2439     }
2440
2441     subpicture_Delete( p_subpic );
2442
2443     vlc_object_release( p_vout );
2444 }
2445