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