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