]> git.sesse.net Git - vlc/blob - src/input/decoder.c
ce072a3389e4e8021fea2402295b71d7e5bf6773
[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->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 race:
1087     if( p_owner->b_waiting )
1088     {
1089         p_owner->b_has_data = true;
1090         vlc_cond_signal( &p_owner->wait_acknowledge );
1091     }
1092
1093     bool b_reject = DecoderWaitUnblock( p_dec );
1094     bool b_paused = p_owner->b_paused;
1095
1096     /* */
1097     int i_rate = INPUT_RATE_DEFAULT;
1098
1099     DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1100                   &i_rate, AOUT_MAX_ADVANCE_TIME );
1101
1102     if( p_audio->i_pts <= VLC_TS_INVALID
1103      || i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1104      || i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1105         b_reject = true;
1106
1107     DecoderWaitDate( p_dec, &b_reject,
1108                      p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1109
1110     if( unlikely(p_owner->b_paused != b_paused) )
1111         goto race; /* race with input thread? retry... */
1112     if( p_aout == NULL )
1113         b_reject = true;
1114
1115     if( !b_reject )
1116     {
1117         assert( !p_owner->b_paused );
1118         if( !aout_DecPlay( p_aout, p_audio, i_rate ) )
1119             *pi_played_sum += 1;
1120         *pi_lost_sum += aout_DecGetResetLost( p_aout );
1121     }
1122     else
1123     {
1124         msg_Dbg( p_dec, "discarded audio buffer" );
1125         *pi_lost_sum += 1;
1126         block_Release( p_audio );
1127     }
1128     vlc_mutex_unlock( &p_owner->lock );
1129 }
1130
1131 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1132 {
1133     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1134     block_t *p_aout_buf;
1135     int i_decoded = 0;
1136     int i_lost = 0;
1137     int i_played = 0;
1138
1139     if( p_block == NULL )
1140         return; /* TODO: remove this check, drain audio decoders properly */
1141
1142     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1143     {
1144         if( DecoderIsFlushing( p_dec ) )
1145         {
1146             /* It prevent freezing VLC in case of broken decoder */
1147             block_Release( p_aout_buf );
1148             if( p_block )
1149                 block_Release( p_block );
1150             break;
1151         }
1152         i_decoded++;
1153
1154         if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1155             p_aout_buf->i_pts < p_owner->i_preroll_end )
1156         {
1157             block_Release( p_aout_buf );
1158             continue;
1159         }
1160
1161         if( p_owner->i_preroll_end > VLC_TS_INVALID )
1162         {
1163             msg_Dbg( p_dec, "End of audio preroll" );
1164             if( p_owner->p_aout )
1165                 aout_DecFlush( p_owner->p_aout );
1166             /* */
1167             p_owner->i_preroll_end = VLC_TS_INVALID;
1168         }
1169
1170         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1171     }
1172
1173     /* Update ugly stat */
1174     input_thread_t  *p_input = p_owner->p_input;
1175
1176     if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1177     {
1178         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1179         stats_Update( p_input->p->counters.p_lost_abuffers, i_lost, NULL );
1180         stats_Update( p_input->p->counters.p_played_abuffers, i_played, NULL );
1181         stats_Update( p_input->p->counters.p_decoded_audio, i_decoded, NULL );
1182         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1183     }
1184 }
1185
1186 /* This function process a audio block
1187  */
1188 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1189 {
1190     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1191
1192     if( p_owner->p_packetizer )
1193     {
1194         block_t *p_packetized_block;
1195         decoder_t *p_packetizer = p_owner->p_packetizer;
1196
1197         while( (p_packetized_block =
1198                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1199         {
1200             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1201             {
1202                 es_format_Clean( &p_dec->fmt_in );
1203                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1204             }
1205
1206             while( p_packetized_block )
1207             {
1208                 block_t *p_next = p_packetized_block->p_next;
1209                 p_packetized_block->p_next = NULL;
1210
1211                 DecoderDecodeAudio( p_dec, p_packetized_block );
1212
1213                 p_packetized_block = p_next;
1214             }
1215         }
1216         /* The packetizer does not output a block that tell the decoder to flush
1217          * do it ourself */
1218         if( b_flush )
1219         {
1220             block_t *p_null = DecoderBlockFlushNew();
1221             if( p_null )
1222                 DecoderDecodeAudio( p_dec, p_null );
1223         }
1224     }
1225     else
1226     {
1227         DecoderDecodeAudio( p_dec, p_block );
1228     }
1229
1230     if( b_flush && p_owner->p_aout )
1231         aout_DecFlush( p_owner->p_aout );
1232 }
1233
1234 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1235 {
1236     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1237     vout_thread_t *p_vout = p_owner->p_spu_vout;
1238
1239     /* */
1240     if( p_subpic->i_start <= VLC_TS_INVALID )
1241     {
1242         msg_Warn( p_dec, "non-dated spu buffer received" );
1243         subpicture_Delete( p_subpic );
1244         return;
1245     }
1246
1247     /* */
1248     vlc_mutex_lock( &p_owner->lock );
1249
1250     if( p_owner->b_waiting )
1251     {
1252         p_owner->b_has_data = true;
1253         vlc_cond_signal( &p_owner->wait_acknowledge );
1254     }
1255
1256     bool b_reject = DecoderWaitUnblock( p_dec );
1257
1258     DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1259                   NULL, INT64_MAX );
1260
1261     if( p_subpic->i_start <= VLC_TS_INVALID )
1262         b_reject = true;
1263
1264     DecoderWaitDate( p_dec, &b_reject,
1265                      p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1266     vlc_mutex_unlock( &p_owner->lock );
1267
1268     if( !b_reject )
1269         vout_PutSubpicture( p_vout, p_subpic );
1270     else
1271         subpicture_Delete( p_subpic );
1272 }
1273
1274 /* This function process a subtitle block
1275  */
1276 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1277 {
1278     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1279
1280     input_thread_t *p_input = p_owner->p_input;
1281     vout_thread_t *p_vout;
1282     subpicture_t *p_spu;
1283
1284     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1285     {
1286         if( p_input != NULL )
1287         {
1288             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1289             stats_Update( p_input->p->counters.p_decoded_sub, 1, NULL );
1290             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1291         }
1292
1293         p_vout = input_resource_HoldVout( p_owner->p_resource );
1294         if( p_vout && p_owner->p_spu_vout == p_vout )
1295         {
1296             /* Preroll does not work very well with subtitle */
1297             if( p_spu->i_start > VLC_TS_INVALID &&
1298                 p_spu->i_start < p_owner->i_preroll_end &&
1299                 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1300             {
1301                 subpicture_Delete( p_spu );
1302             }
1303             else
1304             {
1305                 DecoderPlaySpu( p_dec, p_spu );
1306             }
1307         }
1308         else
1309         {
1310             subpicture_Delete( p_spu );
1311         }
1312         if( p_vout )
1313             vlc_object_release( p_vout );
1314     }
1315
1316     if( b_flush && p_owner->p_spu_vout )
1317     {
1318         p_vout = input_resource_HoldVout( p_owner->p_resource );
1319
1320         if( p_vout && p_owner->p_spu_vout == p_vout )
1321             vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1322
1323         if( p_vout )
1324             vlc_object_release( p_vout );
1325     }
1326 }
1327
1328 /* */
1329 static void DecoderProcessOnFlush( decoder_t *p_dec )
1330 {
1331     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1332
1333     vlc_mutex_lock( &p_owner->lock );
1334
1335     if( p_owner->b_flushing )
1336     {
1337         p_owner->b_flushing = false;
1338         vlc_cond_signal( &p_owner->wait_acknowledge );
1339     }
1340     vlc_mutex_unlock( &p_owner->lock );
1341 }
1342
1343 /**
1344  * Decode a block
1345  *
1346  * \param p_dec the decoder object
1347  * \param p_block the block to decode
1348  * \return VLC_SUCCESS or an error code
1349  */
1350 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1351 {
1352     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1353     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1354
1355     if( p_dec->b_error )
1356     {
1357         if( p_block )
1358             block_Release( p_block );
1359         goto flush;
1360     }
1361
1362     if( p_block && p_block->i_buffer <= 0 )
1363     {
1364         assert( !b_flush_request );
1365         block_Release( p_block );
1366         return;
1367     }
1368
1369 #ifdef ENABLE_SOUT
1370     if( p_owner->b_packetizer )
1371     {
1372         if( p_block )
1373             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1374
1375         DecoderProcessSout( p_dec, p_block );
1376     }
1377     else
1378 #endif
1379     {
1380         bool b_flush = false;
1381
1382         if( p_block )
1383         {
1384             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1385             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1386
1387             b_flush = !b_flushing && b_flush_request;
1388
1389             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1390         }
1391
1392         if( p_dec->fmt_out.i_cat == AUDIO_ES )
1393         {
1394             DecoderProcessAudio( p_dec, p_block, b_flush );
1395         }
1396         else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1397         {
1398             DecoderProcessVideo( p_dec, p_block, b_flush );
1399         }
1400         else if( p_dec->fmt_out.i_cat == SPU_ES )
1401         {
1402             DecoderProcessSpu( p_dec, p_block, b_flush );
1403         }
1404         else
1405         {
1406             msg_Err( p_dec, "unknown ES format" );
1407             p_dec->b_error = true;
1408         }
1409     }
1410
1411     /* */
1412 flush:
1413     if( b_flush_request )
1414         DecoderProcessOnFlush( p_dec );
1415 }
1416
1417
1418 /**
1419  * The decoding main loop
1420  *
1421  * \param p_dec the decoder
1422  */
1423 static void *DecoderThread( void *p_data )
1424 {
1425     decoder_t *p_dec = (decoder_t *)p_data;
1426     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1427
1428     /* The decoder's main loop */
1429     for( ;; )
1430     {
1431         block_t *p_block;
1432
1433         vlc_fifo_Lock( p_owner->p_fifo );
1434         vlc_fifo_CleanupPush( p_owner->p_fifo );
1435
1436         while( vlc_fifo_IsEmpty( p_owner->p_fifo ) )
1437         {
1438             if( p_owner->b_woken )
1439                 break;
1440             vlc_fifo_Wait( p_owner->p_fifo );
1441             /* Make sure there is no cancellation point other than this one^^.
1442              * If you need one, be sure to push cleanup of p_block. */
1443         }
1444
1445         p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
1446         if( p_block != NULL )
1447             vlc_cond_signal( &p_owner->wait_acknowledge );
1448
1449         p_owner->b_woken = false;
1450         vlc_cleanup_run();
1451
1452         if( p_block == NULL || p_block->i_flags & BLOCK_FLAG_CORE_EOS )
1453             DecoderSignalWait( p_dec );
1454
1455         if( p_block )
1456         {
1457             int canc = vlc_savecancel();
1458
1459             if( p_block->i_flags & BLOCK_FLAG_CORE_EOS )
1460             {
1461                 /* calling DecoderProcess() with NULL block will make
1462                  * decoders/packetizers flush their buffers */
1463                 block_Release( p_block );
1464                 p_block = NULL;
1465             }
1466
1467             DecoderProcess( p_dec, p_block );
1468
1469             vlc_restorecancel( canc );
1470         }
1471     }
1472     return NULL;
1473 }
1474
1475 /**
1476  * Create a decoder object
1477  *
1478  * \param p_input the input thread
1479  * \param p_es the es descriptor
1480  * \param b_packetizer instead of a decoder
1481  * \return the decoder object
1482  */
1483 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
1484                                   input_thread_t *p_input,
1485                                   const es_format_t *fmt, bool b_packetizer,
1486                                   input_resource_t *p_resource,
1487                                   sout_instance_t *p_sout )
1488 {
1489     decoder_t *p_dec;
1490     decoder_owner_sys_t *p_owner;
1491     es_format_t null_es_format;
1492
1493     p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
1494     if( p_dec == NULL )
1495         return NULL;
1496
1497     p_dec->pf_decode_audio = NULL;
1498     p_dec->pf_decode_video = NULL;
1499     p_dec->pf_decode_sub = NULL;
1500     p_dec->pf_get_cc = NULL;
1501     p_dec->pf_packetize = NULL;
1502
1503     /* Initialize the decoder */
1504     p_dec->p_module = NULL;
1505
1506     memset( &null_es_format, 0, sizeof(es_format_t) );
1507     es_format_Copy( &p_dec->fmt_in, fmt );
1508     es_format_Copy( &p_dec->fmt_out, &null_es_format );
1509
1510     p_dec->p_description = NULL;
1511
1512     /* Allocate our private structure for the decoder */
1513     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
1514     if( unlikely(p_owner == NULL) )
1515     {
1516         vlc_object_release( p_dec );
1517         return NULL;
1518     }
1519     p_owner->i_preroll_end = VLC_TS_INVALID;
1520     p_owner->i_last_rate = INPUT_RATE_DEFAULT;
1521     p_owner->p_input = p_input;
1522     p_owner->p_resource = p_resource;
1523     p_owner->p_aout = NULL;
1524     p_owner->p_vout = NULL;
1525     p_owner->p_spu_vout = NULL;
1526     p_owner->i_spu_channel = 0;
1527     p_owner->i_spu_order = 0;
1528     p_owner->p_sout = p_sout;
1529     p_owner->p_sout_input = NULL;
1530     p_owner->p_packetizer = NULL;
1531     p_owner->b_packetizer = b_packetizer;
1532     es_format_Init( &p_owner->fmt, UNKNOWN_ES, 0 );
1533
1534     /* decoder fifo */
1535     p_owner->b_woken = false;
1536     p_owner->p_fifo = block_FifoNew();
1537     if( unlikely(p_owner->p_fifo == NULL) )
1538     {
1539         free( p_owner );
1540         vlc_object_release( p_dec );
1541         return NULL;
1542     }
1543
1544     /* Set buffers allocation callbacks for the decoders */
1545     p_dec->pf_aout_format_update = aout_update_format;
1546     p_dec->pf_vout_format_update = vout_update_format;
1547     p_dec->pf_vout_buffer_new = vout_new_buffer;
1548     p_dec->pf_spu_buffer_new  = spu_new_buffer;
1549     /* */
1550     p_dec->pf_get_attachments  = DecoderGetInputAttachments;
1551     p_dec->pf_get_display_date = DecoderGetDisplayDate;
1552     p_dec->pf_get_display_rate = DecoderGetDisplayRate;
1553
1554     /* Find a suitable decoder/packetizer module */
1555     if( !b_packetizer )
1556         p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
1557     else
1558         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
1559
1560     /* Check if decoder requires already packetized data */
1561     if( !b_packetizer &&
1562         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
1563     {
1564         p_owner->p_packetizer =
1565             vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
1566         if( p_owner->p_packetizer )
1567         {
1568             es_format_Copy( &p_owner->p_packetizer->fmt_in,
1569                             &p_dec->fmt_in );
1570
1571             es_format_Copy( &p_owner->p_packetizer->fmt_out,
1572                             &null_es_format );
1573
1574             p_owner->p_packetizer->p_module =
1575                 module_need( p_owner->p_packetizer,
1576                              "packetizer", "$packetizer", false );
1577
1578             if( !p_owner->p_packetizer->p_module )
1579             {
1580                 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1581                 vlc_object_release( p_owner->p_packetizer );
1582                 p_owner->p_packetizer = NULL;
1583             }
1584         }
1585     }
1586
1587     /* Copy ourself the input replay gain */
1588     if( fmt->i_cat == AUDIO_ES )
1589     {
1590         for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1591         {
1592             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
1593             {
1594                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
1595                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
1596             }
1597             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
1598             {
1599                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
1600                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
1601             }
1602         }
1603     }
1604
1605     /* */
1606     vlc_mutex_init( &p_owner->lock );
1607     vlc_cond_init( &p_owner->wait_request );
1608     vlc_cond_init( &p_owner->wait_acknowledge );
1609
1610     p_owner->b_fmt_description = false;
1611     p_owner->p_description = NULL;
1612
1613     p_owner->b_paused = false;
1614     p_owner->pause.i_date = VLC_TS_INVALID;
1615     p_owner->pause.i_ignore = 0;
1616
1617     p_owner->b_waiting = false;
1618     p_owner->b_first = true;
1619     p_owner->b_has_data = false;
1620
1621     p_owner->b_flushing = false;
1622
1623     /* */
1624     p_owner->cc.b_supported = false;
1625     if( !b_packetizer )
1626     {
1627         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
1628             p_owner->cc.b_supported = true;
1629         if( p_dec->pf_get_cc )
1630             p_owner->cc.b_supported = true;
1631     }
1632
1633     for( unsigned i = 0; i < 4; i++ )
1634     {
1635         p_owner->cc.pb_present[i] = false;
1636         p_owner->cc.pp_decoder[i] = NULL;
1637     }
1638     p_owner->i_ts_delay = 0;
1639     return p_dec;
1640 }
1641
1642 /**
1643  * Destroys a decoder object
1644  *
1645  * \param p_dec the decoder object
1646  * \return nothing
1647  */
1648 static void DeleteDecoder( decoder_t * p_dec )
1649 {
1650     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1651
1652     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1653              (char*)&p_dec->fmt_in.i_codec,
1654              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1655
1656     /* Free all packets still in the decoder fifo. */
1657     block_FifoRelease( p_owner->p_fifo );
1658
1659     /* Cleanup */
1660     if( p_owner->p_aout )
1661     {
1662         /* TODO: REVISIT gap-less audio */
1663         aout_DecFlush( p_owner->p_aout );
1664         aout_DecDelete( p_owner->p_aout );
1665         input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1666         if( p_owner->p_input != NULL )
1667             input_SendEventAout( p_owner->p_input );
1668     }
1669     if( p_owner->p_vout )
1670     {
1671         /* Hack to make sure all the the pictures are freed by the decoder
1672          * and that the vout is not paused anymore */
1673         vout_Reset( p_owner->p_vout );
1674
1675         /* */
1676         input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1677                                     0, true );
1678         if( p_owner->p_input != NULL )
1679             input_SendEventVout( p_owner->p_input );
1680     }
1681
1682 #ifdef ENABLE_SOUT
1683     if( p_owner->p_sout_input )
1684     {
1685         sout_InputDelete( p_owner->p_sout_input );
1686     }
1687 #endif
1688     es_format_Clean( &p_owner->fmt );
1689
1690     if( p_dec->fmt_out.i_cat == SPU_ES )
1691     {
1692         vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1693         if( p_vout )
1694         {
1695             if( p_owner->p_spu_vout == p_vout )
1696                 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1697             vlc_object_release( p_vout );
1698         }
1699     }
1700
1701     es_format_Clean( &p_dec->fmt_in );
1702     es_format_Clean( &p_dec->fmt_out );
1703     if( p_dec->p_description )
1704         vlc_meta_Delete( p_dec->p_description );
1705     if( p_owner->p_description )
1706         vlc_meta_Delete( p_owner->p_description );
1707
1708     if( p_owner->p_packetizer )
1709     {
1710         module_unneed( p_owner->p_packetizer,
1711                        p_owner->p_packetizer->p_module );
1712         es_format_Clean( &p_owner->p_packetizer->fmt_in );
1713         es_format_Clean( &p_owner->p_packetizer->fmt_out );
1714         if( p_owner->p_packetizer->p_description )
1715             vlc_meta_Delete( p_owner->p_packetizer->p_description );
1716         vlc_object_release( p_owner->p_packetizer );
1717     }
1718
1719     vlc_cond_destroy( &p_owner->wait_acknowledge );
1720     vlc_cond_destroy( &p_owner->wait_request );
1721     vlc_mutex_destroy( &p_owner->lock );
1722
1723     vlc_object_release( p_dec );
1724
1725     free( p_owner );
1726 }
1727
1728 /* */
1729 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
1730 {
1731     if (codec != VLC_FOURCC('u','n','d','f')) {
1732         const char *desc = vlc_fourcc_GetDescription(p_dec->fmt_in.i_cat, codec);
1733         if (!desc || !*desc)
1734             desc = N_("No description for this codec");
1735         msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&codec, desc );
1736         dialog_Fatal( p_dec, _("Codec not supported"),
1737                 _("VLC could not decode the format \"%4.4s\" (%s)"),
1738                 (char*)&codec, desc );
1739     } else {
1740         msg_Err( p_dec, "could not identify codec" );
1741         dialog_Fatal( p_dec, _("Unidentified codec"),
1742             _("VLC could not identify the audio or video codec" ) );
1743     }
1744 }
1745
1746 /* TODO: pass p_sout through p_resource? -- Courmisch */
1747 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
1748                                const es_format_t *fmt, input_clock_t *p_clock,
1749                                input_resource_t *p_resource,
1750                                sout_instance_t *p_sout  )
1751 {
1752     decoder_t *p_dec = NULL;
1753     const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
1754     int i_priority;
1755
1756     /* Create the decoder configuration structure */
1757     p_dec = CreateDecoder( p_parent, p_input, fmt,
1758                            p_sout != NULL, p_resource, p_sout );
1759     if( p_dec == NULL )
1760     {
1761         msg_Err( p_parent, "could not create %s", psz_type );
1762         dialog_Fatal( p_parent, _("Streaming / Transcoding failed"),
1763                       _("VLC could not open the %s module."),
1764                       vlc_gettext( psz_type ) );
1765         return NULL;
1766     }
1767
1768     if( !p_dec->p_module )
1769     {
1770         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
1771
1772         DeleteDecoder( p_dec );
1773         return NULL;
1774     }
1775
1776     p_dec->p_owner->p_clock = p_clock;
1777     assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
1778
1779     if( p_dec->fmt_out.i_cat == AUDIO_ES )
1780         i_priority = VLC_THREAD_PRIORITY_AUDIO;
1781     else
1782         i_priority = VLC_THREAD_PRIORITY_VIDEO;
1783
1784     /* Spawn the decoder thread */
1785     if( vlc_clone( &p_dec->p_owner->thread, DecoderThread, p_dec, i_priority ) )
1786     {
1787         msg_Err( p_dec, "cannot spawn decoder thread" );
1788         module_unneed( p_dec, p_dec->p_module );
1789         DeleteDecoder( p_dec );
1790         return NULL;
1791     }
1792
1793     return p_dec;
1794 }
1795
1796
1797 /**
1798  * Spawns a new decoder thread from the input thread
1799  *
1800  * \param p_input the input thread
1801  * \param p_es the es descriptor
1802  * \return the spawned decoder object
1803  */
1804 decoder_t *input_DecoderNew( input_thread_t *p_input,
1805                              es_format_t *fmt, input_clock_t *p_clock,
1806                              sout_instance_t *p_sout  )
1807 {
1808     return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
1809                         p_input->p->p_resource, p_sout );
1810 }
1811
1812 /**
1813  * Spawn a decoder thread outside of the input thread.
1814  */
1815 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, const es_format_t *fmt,
1816                                 input_resource_t *p_resource )
1817 {
1818     return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
1819 }
1820
1821
1822 /**
1823  * Kills a decoder thread and waits until it's finished
1824  *
1825  * \param p_input the input thread
1826  * \param p_es the es descriptor
1827  * \return nothing
1828  */
1829 void input_DecoderDelete( decoder_t *p_dec )
1830 {
1831     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1832
1833     vlc_cancel( p_owner->thread );
1834
1835     /* Make sure we aren't paused/waiting/decoding anymore */
1836     vlc_mutex_lock( &p_owner->lock );
1837     p_owner->b_paused = false;
1838     p_owner->b_waiting = false;
1839     p_owner->b_flushing = true;
1840     vlc_cond_signal( &p_owner->wait_request );
1841     vlc_mutex_unlock( &p_owner->lock );
1842
1843     vlc_join( p_owner->thread, NULL );
1844
1845     module_unneed( p_dec, p_dec->p_module );
1846
1847     /* */
1848     if( p_dec->p_owner->cc.b_supported )
1849     {
1850         int i;
1851         for( i = 0; i < 4; i++ )
1852             input_DecoderSetCcState( p_dec, false, i );
1853     }
1854
1855     /* Delete decoder */
1856     DeleteDecoder( p_dec );
1857 }
1858
1859 /**
1860  * Put a block_t in the decoder's fifo.
1861  * Thread-safe w.r.t. the decoder. May be a cancellation point.
1862  *
1863  * \param p_dec the decoder object
1864  * \param p_block the data block
1865  */
1866 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
1867 {
1868     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1869
1870     vlc_fifo_Lock( p_owner->p_fifo );
1871     if( !b_do_pace )
1872     {
1873         /* FIXME: ideally we would check the time amount of data
1874          * in the FIFO instead of its size. */
1875         /* 400 MiB, i.e. ~ 50mb/s for 60s */
1876         if( vlc_fifo_GetBytes( p_owner->p_fifo ) > 400*1024*1024 )
1877         {
1878             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
1879                       "consumed quickly enough), resetting fifo!" );
1880             block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
1881         }
1882     }
1883     else
1884     if( !p_owner->b_waiting )
1885     {   /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
1886          * Locking is not necessary as b_waiting is only read, not written by
1887          * the decoder thread. */
1888         while( vlc_fifo_GetCount( p_owner->p_fifo ) >= 10 )
1889             vlc_fifo_WaitCond( p_owner->p_fifo, &p_owner->wait_acknowledge );
1890     }
1891
1892     vlc_fifo_QueueUnlocked( p_owner->p_fifo, p_block );
1893     vlc_fifo_Unlock( p_owner->p_fifo );
1894 }
1895
1896 bool input_DecoderIsEmpty( decoder_t * p_dec )
1897 {
1898     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1899     assert( !p_owner->b_waiting );
1900
1901     bool b_empty = block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
1902
1903     if( b_empty )
1904     {
1905         vlc_mutex_lock( &p_owner->lock );
1906         /* TODO subtitles support */
1907         if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->p_vout )
1908             b_empty = vout_IsEmpty( p_owner->p_vout );
1909         else if( p_owner->fmt.i_cat == AUDIO_ES && p_owner->p_aout )
1910             b_empty = aout_DecIsEmpty( p_owner->p_aout );
1911         vlc_mutex_unlock( &p_owner->lock );
1912     }
1913     return b_empty;
1914 }
1915
1916 /**
1917  * Signals that there are no further blocks to decode, and requests that the
1918  * decoder drain all pending buffers. This is used to ensure that all
1919  * intermediate buffers empty and no samples get lost at the end of the stream.
1920  *
1921  * @note The function does not actually wait for draining. It just signals that
1922  * draining should be performed once the decoder has emptied FIFO.
1923  */
1924 void input_DecoderDrain( decoder_t *p_dec )
1925 {
1926     block_t *p_block = block_Alloc(0);
1927     if( unlikely(p_block == NULL) )
1928         return;
1929
1930     p_block->i_flags |= BLOCK_FLAG_CORE_EOS;
1931     input_DecoderDecode( p_dec, p_block, false );
1932 }
1933
1934 static void DecoderFlush( decoder_t *p_dec )
1935 {
1936     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1937
1938     vlc_assert_locked( &p_owner->lock );
1939
1940     /* Empty the fifo */
1941     block_FifoEmpty( p_owner->p_fifo );
1942
1943     /* Monitor for flush end */
1944     p_owner->b_flushing = true;
1945     vlc_cond_signal( &p_owner->wait_request );
1946
1947     /* Send a special block */
1948     block_t *p_null = DecoderBlockFlushNew();
1949     if( !p_null )
1950         return;
1951     input_DecoderDecode( p_dec, p_null, false );
1952
1953     /* */
1954     while( p_owner->b_flushing )
1955         vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
1956 }
1957
1958 /**
1959  * Requests that the decoder immediately discard all pending buffers.
1960  * This is useful when seeking or when deselecting a stream.
1961  */
1962 void input_DecoderFlush( decoder_t *p_dec )
1963 {
1964     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1965
1966     vlc_mutex_lock( &p_owner->lock );
1967     DecoderFlush( p_dec );
1968     vlc_mutex_unlock( &p_owner->lock );
1969 }
1970
1971 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
1972 {
1973     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1974     int i;
1975
1976     vlc_mutex_lock( &p_owner->lock );
1977     for( i = 0; i < 4; i++ )
1978         pb_present[i] =  p_owner->cc.pb_present[i];
1979     vlc_mutex_unlock( &p_owner->lock );
1980 }
1981
1982 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
1983 {
1984     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1985
1986     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
1987
1988     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
1989         return VLC_EGENERIC;
1990
1991     if( b_decode )
1992     {
1993         static const vlc_fourcc_t fcc[4] = {
1994             VLC_FOURCC('c', 'c', '1', ' '),
1995             VLC_FOURCC('c', 'c', '2', ' '),
1996             VLC_FOURCC('c', 'c', '3', ' '),
1997             VLC_FOURCC('c', 'c', '4', ' '),
1998         };
1999         decoder_t *p_cc;
2000         es_format_t fmt;
2001
2002         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
2003         p_cc = input_DecoderNew( p_owner->p_input, &fmt,
2004                               p_dec->p_owner->p_clock, p_owner->p_sout );
2005         if( !p_cc )
2006         {
2007             msg_Err( p_dec, "could not create decoder" );
2008             dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
2009                           _("VLC could not open the decoder module.") );
2010             return VLC_EGENERIC;
2011         }
2012         else if( !p_cc->p_module )
2013         {
2014             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
2015             input_DecoderDelete(p_cc);
2016             return VLC_EGENERIC;
2017         }
2018         p_cc->p_owner->p_clock = p_owner->p_clock;
2019
2020         vlc_mutex_lock( &p_owner->lock );
2021         p_owner->cc.pp_decoder[i_channel] = p_cc;
2022         vlc_mutex_unlock( &p_owner->lock );
2023     }
2024     else
2025     {
2026         decoder_t *p_cc;
2027
2028         vlc_mutex_lock( &p_owner->lock );
2029         p_cc = p_owner->cc.pp_decoder[i_channel];
2030         p_owner->cc.pp_decoder[i_channel] = NULL;
2031         vlc_mutex_unlock( &p_owner->lock );
2032
2033         if( p_cc )
2034             input_DecoderDelete(p_cc);
2035     }
2036     return VLC_SUCCESS;
2037 }
2038
2039 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
2040 {
2041     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2042
2043     *pb_decode = false;
2044     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
2045         return VLC_EGENERIC;
2046
2047     vlc_mutex_lock( &p_owner->lock );
2048     *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
2049     vlc_mutex_unlock( &p_owner->lock );
2050     return VLC_EGENERIC;
2051 }
2052
2053 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
2054 {
2055     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2056
2057     vlc_mutex_lock( &p_owner->lock );
2058     /* Normally, p_owner->b_paused != b_paused here. But if a track is added
2059      * while the input is paused (e.g. add sub file), then b_paused is
2060      * (incorrectly) false. */
2061     if( likely(p_owner->b_paused != b_paused) ) {
2062         p_owner->b_paused = b_paused;
2063         p_owner->pause.i_date = i_date;
2064         p_owner->pause.i_ignore = 0;
2065         vlc_cond_signal( &p_owner->wait_request );
2066
2067         /* XXX only audio and video output have to be paused.
2068          * - for sout it is useless
2069          * - for subs, it is done by the vout
2070          */
2071         if( p_owner->fmt.i_cat == AUDIO_ES )
2072         {
2073             if( p_owner->p_aout )
2074                 aout_DecChangePause( p_owner->p_aout, b_paused, i_date );
2075         }
2076         else if( p_owner->fmt.i_cat == VIDEO_ES )
2077         {
2078             if( p_owner->p_vout )
2079                 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
2080         }
2081     }
2082     vlc_mutex_unlock( &p_owner->lock );
2083 }
2084
2085 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
2086 {
2087     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2088
2089     vlc_mutex_lock( &p_owner->lock );
2090     p_owner->i_ts_delay = i_delay;
2091     vlc_mutex_unlock( &p_owner->lock );
2092 }
2093
2094 void input_DecoderStartWait( decoder_t *p_dec )
2095 {
2096     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2097
2098     assert( !p_owner->b_waiting );
2099
2100     vlc_mutex_lock( &p_owner->lock );
2101     p_owner->b_first = true;
2102     p_owner->b_has_data = false;
2103     p_owner->b_waiting = true;
2104     vlc_cond_signal( &p_owner->wait_request );
2105     vlc_mutex_unlock( &p_owner->lock );
2106 }
2107
2108 void input_DecoderStopWait( decoder_t *p_dec )
2109 {
2110     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2111
2112     assert( p_owner->b_waiting );
2113
2114     vlc_mutex_lock( &p_owner->lock );
2115     p_owner->b_waiting = false;
2116     vlc_cond_signal( &p_owner->wait_request );
2117     vlc_mutex_unlock( &p_owner->lock );
2118 }
2119
2120 void input_DecoderWait( 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     while( !p_owner->b_has_data )
2128     {
2129         vlc_fifo_Lock( p_owner->p_fifo );
2130         p_owner->b_woken = true;
2131         vlc_fifo_Signal( p_owner->p_fifo );
2132         vlc_fifo_Unlock( p_owner->p_fifo );
2133         vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
2134     }
2135     vlc_mutex_unlock( &p_owner->lock );
2136 }
2137
2138 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
2139 {
2140     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2141
2142     *pi_duration = 0;
2143
2144     vlc_mutex_lock( &p_owner->lock );
2145     if( p_owner->fmt.i_cat == VIDEO_ES )
2146     {
2147         if( p_owner->b_paused && p_owner->p_vout )
2148         {
2149             vout_NextPicture( p_owner->p_vout, pi_duration );
2150             p_owner->pause.i_ignore++;
2151             vlc_cond_signal( &p_owner->wait_request );
2152         }
2153     }
2154     else
2155     {
2156         /* TODO subtitle should not be flushed */
2157         p_owner->b_waiting = false;
2158         DecoderFlush( p_dec );
2159     }
2160     vlc_mutex_unlock( &p_owner->lock );
2161 }
2162
2163 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
2164 {
2165     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2166     bool b_changed;
2167
2168     vlc_mutex_lock( &p_owner->lock );
2169     b_changed = p_owner->b_fmt_description;
2170     if( b_changed )
2171     {
2172         if( p_fmt != NULL )
2173             es_format_Copy( p_fmt, &p_owner->fmt );
2174
2175         if( pp_meta )
2176         {
2177             *pp_meta = NULL;
2178             if( p_owner->p_description )
2179             {
2180                 *pp_meta = vlc_meta_New();
2181                 if( *pp_meta )
2182                     vlc_meta_Merge( *pp_meta, p_owner->p_description );
2183             }
2184         }
2185         p_owner->b_fmt_description = false;
2186     }
2187     vlc_mutex_unlock( &p_owner->lock );
2188     return b_changed;
2189 }
2190
2191 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
2192 {
2193     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2194
2195     return block_FifoSize( p_owner->p_fifo );
2196 }
2197
2198 void input_DecoderGetObjects( decoder_t *p_dec,
2199                               vout_thread_t **pp_vout, audio_output_t **pp_aout )
2200 {
2201     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2202
2203     vlc_mutex_lock( &p_owner->lock );
2204     if( pp_vout )
2205         *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
2206     if( pp_aout )
2207         *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
2208     vlc_mutex_unlock( &p_owner->lock );
2209 }