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