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