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