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