]> git.sesse.net Git - vlc/blob - src/input/decoder.c
decoder: do not wait for buffering when there is no data
[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_waiting;
109     bool b_first;
110     bool b_has_data;
111
112     /* Flushing */
113     bool b_flushing;
114     bool b_draining;
115     bool b_idle;
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
1431         vlc_fifo_Lock( p_owner->p_fifo );
1432         vlc_fifo_CleanupPush( p_owner->p_fifo );
1433
1434         vlc_cond_signal( &p_owner->wait_acknowledge );
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                 break;
1442             }
1443
1444             p_owner->b_idle = true;
1445             vlc_fifo_Wait( p_owner->p_fifo );
1446             /* Make sure there is no cancellation point other than this one^^.
1447              * If you need one, be sure to push cleanup of p_block. */
1448             p_owner->b_idle = false;
1449         }
1450
1451         p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
1452         vlc_cleanup_run();
1453
1454         int canc = vlc_savecancel();
1455         DecoderProcess( p_dec, p_block );
1456         vlc_restorecancel( canc );
1457     }
1458     return NULL;
1459 }
1460
1461 /**
1462  * Create a decoder object
1463  *
1464  * \param p_input the input thread
1465  * \param p_es the es descriptor
1466  * \param b_packetizer instead of a decoder
1467  * \return the decoder object
1468  */
1469 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
1470                                   input_thread_t *p_input,
1471                                   const es_format_t *fmt, bool b_packetizer,
1472                                   input_resource_t *p_resource,
1473                                   sout_instance_t *p_sout )
1474 {
1475     decoder_t *p_dec;
1476     decoder_owner_sys_t *p_owner;
1477     es_format_t null_es_format;
1478
1479     p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
1480     if( p_dec == NULL )
1481         return NULL;
1482
1483     p_dec->pf_decode_audio = NULL;
1484     p_dec->pf_decode_video = NULL;
1485     p_dec->pf_decode_sub = NULL;
1486     p_dec->pf_get_cc = NULL;
1487     p_dec->pf_packetize = NULL;
1488
1489     /* Initialize the decoder */
1490     p_dec->p_module = NULL;
1491
1492     memset( &null_es_format, 0, sizeof(es_format_t) );
1493     es_format_Copy( &p_dec->fmt_in, fmt );
1494     es_format_Copy( &p_dec->fmt_out, &null_es_format );
1495
1496     p_dec->p_description = NULL;
1497
1498     /* Allocate our private structure for the decoder */
1499     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
1500     if( unlikely(p_owner == NULL) )
1501     {
1502         vlc_object_release( p_dec );
1503         return NULL;
1504     }
1505     p_owner->i_preroll_end = VLC_TS_INVALID;
1506     p_owner->i_last_rate = INPUT_RATE_DEFAULT;
1507     p_owner->p_input = p_input;
1508     p_owner->p_resource = p_resource;
1509     p_owner->p_aout = NULL;
1510     p_owner->p_vout = NULL;
1511     p_owner->p_spu_vout = NULL;
1512     p_owner->i_spu_channel = 0;
1513     p_owner->i_spu_order = 0;
1514     p_owner->p_sout = p_sout;
1515     p_owner->p_sout_input = NULL;
1516     p_owner->p_packetizer = NULL;
1517     p_owner->b_packetizer = b_packetizer;
1518     es_format_Init( &p_owner->fmt, UNKNOWN_ES, 0 );
1519
1520     /* decoder fifo */
1521     p_owner->p_fifo = block_FifoNew();
1522     if( unlikely(p_owner->p_fifo == NULL) )
1523     {
1524         free( p_owner );
1525         vlc_object_release( p_dec );
1526         return NULL;
1527     }
1528
1529     /* Set buffers allocation callbacks for the decoders */
1530     p_dec->pf_aout_format_update = aout_update_format;
1531     p_dec->pf_vout_format_update = vout_update_format;
1532     p_dec->pf_vout_buffer_new = vout_new_buffer;
1533     p_dec->pf_spu_buffer_new  = spu_new_buffer;
1534     /* */
1535     p_dec->pf_get_attachments  = DecoderGetInputAttachments;
1536     p_dec->pf_get_display_date = DecoderGetDisplayDate;
1537     p_dec->pf_get_display_rate = DecoderGetDisplayRate;
1538
1539     /* Find a suitable decoder/packetizer module */
1540     if( !b_packetizer )
1541         p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
1542     else
1543         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
1544
1545     /* Check if decoder requires already packetized data */
1546     if( !b_packetizer &&
1547         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
1548     {
1549         p_owner->p_packetizer =
1550             vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
1551         if( p_owner->p_packetizer )
1552         {
1553             es_format_Copy( &p_owner->p_packetizer->fmt_in,
1554                             &p_dec->fmt_in );
1555
1556             es_format_Copy( &p_owner->p_packetizer->fmt_out,
1557                             &null_es_format );
1558
1559             p_owner->p_packetizer->p_module =
1560                 module_need( p_owner->p_packetizer,
1561                              "packetizer", "$packetizer", false );
1562
1563             if( !p_owner->p_packetizer->p_module )
1564             {
1565                 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1566                 vlc_object_release( p_owner->p_packetizer );
1567                 p_owner->p_packetizer = NULL;
1568             }
1569         }
1570     }
1571
1572     /* Copy ourself the input replay gain */
1573     if( fmt->i_cat == AUDIO_ES )
1574     {
1575         for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1576         {
1577             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
1578             {
1579                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
1580                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
1581             }
1582             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
1583             {
1584                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
1585                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
1586             }
1587         }
1588     }
1589
1590     /* */
1591     vlc_mutex_init( &p_owner->lock );
1592     vlc_cond_init( &p_owner->wait_request );
1593     vlc_cond_init( &p_owner->wait_acknowledge );
1594
1595     p_owner->b_fmt_description = false;
1596     p_owner->p_description = NULL;
1597
1598     p_owner->b_paused = false;
1599     p_owner->pause.i_date = VLC_TS_INVALID;
1600     p_owner->pause.i_ignore = 0;
1601
1602     p_owner->b_waiting = false;
1603     p_owner->b_first = true;
1604     p_owner->b_has_data = false;
1605
1606     p_owner->b_flushing = false;
1607     p_owner->b_draining = false;
1608     p_owner->b_idle = false;
1609
1610     /* */
1611     p_owner->cc.b_supported = false;
1612     if( !b_packetizer )
1613     {
1614         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
1615             p_owner->cc.b_supported = true;
1616         if( p_dec->pf_get_cc )
1617             p_owner->cc.b_supported = true;
1618     }
1619
1620     for( unsigned i = 0; i < 4; i++ )
1621     {
1622         p_owner->cc.pb_present[i] = false;
1623         p_owner->cc.pp_decoder[i] = NULL;
1624     }
1625     p_owner->i_ts_delay = 0;
1626     return p_dec;
1627 }
1628
1629 /**
1630  * Destroys a decoder object
1631  *
1632  * \param p_dec the decoder object
1633  * \return nothing
1634  */
1635 static void DeleteDecoder( decoder_t * p_dec )
1636 {
1637     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1638
1639     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1640              (char*)&p_dec->fmt_in.i_codec,
1641              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1642
1643     /* Free all packets still in the decoder fifo. */
1644     block_FifoRelease( p_owner->p_fifo );
1645
1646     /* Cleanup */
1647     if( p_owner->p_aout )
1648     {
1649         /* TODO: REVISIT gap-less audio */
1650         aout_DecFlush( p_owner->p_aout );
1651         aout_DecDelete( p_owner->p_aout );
1652         input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1653         if( p_owner->p_input != NULL )
1654             input_SendEventAout( p_owner->p_input );
1655     }
1656     if( p_owner->p_vout )
1657     {
1658         /* Hack to make sure all the the pictures are freed by the decoder
1659          * and that the vout is not paused anymore */
1660         vout_Reset( p_owner->p_vout );
1661
1662         /* */
1663         input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1664                                     0, true );
1665         if( p_owner->p_input != NULL )
1666             input_SendEventVout( p_owner->p_input );
1667     }
1668
1669 #ifdef ENABLE_SOUT
1670     if( p_owner->p_sout_input )
1671     {
1672         sout_InputDelete( p_owner->p_sout_input );
1673     }
1674 #endif
1675     es_format_Clean( &p_owner->fmt );
1676
1677     if( p_dec->fmt_out.i_cat == SPU_ES )
1678     {
1679         vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1680         if( p_vout )
1681         {
1682             if( p_owner->p_spu_vout == p_vout )
1683                 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1684             vlc_object_release( p_vout );
1685         }
1686     }
1687
1688     es_format_Clean( &p_dec->fmt_in );
1689     es_format_Clean( &p_dec->fmt_out );
1690     if( p_dec->p_description )
1691         vlc_meta_Delete( p_dec->p_description );
1692     if( p_owner->p_description )
1693         vlc_meta_Delete( p_owner->p_description );
1694
1695     if( p_owner->p_packetizer )
1696     {
1697         module_unneed( p_owner->p_packetizer,
1698                        p_owner->p_packetizer->p_module );
1699         es_format_Clean( &p_owner->p_packetizer->fmt_in );
1700         es_format_Clean( &p_owner->p_packetizer->fmt_out );
1701         if( p_owner->p_packetizer->p_description )
1702             vlc_meta_Delete( p_owner->p_packetizer->p_description );
1703         vlc_object_release( p_owner->p_packetizer );
1704     }
1705
1706     vlc_cond_destroy( &p_owner->wait_acknowledge );
1707     vlc_cond_destroy( &p_owner->wait_request );
1708     vlc_mutex_destroy( &p_owner->lock );
1709
1710     vlc_object_release( p_dec );
1711
1712     free( p_owner );
1713 }
1714
1715 /* */
1716 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
1717 {
1718     if (codec != VLC_FOURCC('u','n','d','f')) {
1719         const char *desc = vlc_fourcc_GetDescription(p_dec->fmt_in.i_cat, codec);
1720         if (!desc || !*desc)
1721             desc = N_("No description for this codec");
1722         msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&codec, desc );
1723         dialog_Fatal( p_dec, _("Codec not supported"),
1724                 _("VLC could not decode the format \"%4.4s\" (%s)"),
1725                 (char*)&codec, desc );
1726     } else {
1727         msg_Err( p_dec, "could not identify codec" );
1728         dialog_Fatal( p_dec, _("Unidentified codec"),
1729             _("VLC could not identify the audio or video codec" ) );
1730     }
1731 }
1732
1733 /* TODO: pass p_sout through p_resource? -- Courmisch */
1734 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
1735                                const es_format_t *fmt, input_clock_t *p_clock,
1736                                input_resource_t *p_resource,
1737                                sout_instance_t *p_sout  )
1738 {
1739     decoder_t *p_dec = NULL;
1740     const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
1741     int i_priority;
1742
1743     /* Create the decoder configuration structure */
1744     p_dec = CreateDecoder( p_parent, p_input, fmt,
1745                            p_sout != NULL, p_resource, p_sout );
1746     if( p_dec == NULL )
1747     {
1748         msg_Err( p_parent, "could not create %s", psz_type );
1749         dialog_Fatal( p_parent, _("Streaming / Transcoding failed"),
1750                       _("VLC could not open the %s module."),
1751                       vlc_gettext( psz_type ) );
1752         return NULL;
1753     }
1754
1755     if( !p_dec->p_module )
1756     {
1757         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
1758
1759         DeleteDecoder( p_dec );
1760         return NULL;
1761     }
1762
1763     p_dec->p_owner->p_clock = p_clock;
1764     assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
1765
1766     if( p_dec->fmt_out.i_cat == AUDIO_ES )
1767         i_priority = VLC_THREAD_PRIORITY_AUDIO;
1768     else
1769         i_priority = VLC_THREAD_PRIORITY_VIDEO;
1770
1771     /* Spawn the decoder thread */
1772     if( vlc_clone( &p_dec->p_owner->thread, DecoderThread, p_dec, i_priority ) )
1773     {
1774         msg_Err( p_dec, "cannot spawn decoder thread" );
1775         module_unneed( p_dec, p_dec->p_module );
1776         DeleteDecoder( p_dec );
1777         return NULL;
1778     }
1779
1780     return p_dec;
1781 }
1782
1783
1784 /**
1785  * Spawns a new decoder thread from the input thread
1786  *
1787  * \param p_input the input thread
1788  * \param p_es the es descriptor
1789  * \return the spawned decoder object
1790  */
1791 decoder_t *input_DecoderNew( input_thread_t *p_input,
1792                              es_format_t *fmt, input_clock_t *p_clock,
1793                              sout_instance_t *p_sout  )
1794 {
1795     return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
1796                         p_input->p->p_resource, p_sout );
1797 }
1798
1799 /**
1800  * Spawn a decoder thread outside of the input thread.
1801  */
1802 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, const es_format_t *fmt,
1803                                 input_resource_t *p_resource )
1804 {
1805     return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
1806 }
1807
1808
1809 /**
1810  * Kills a decoder thread and waits until it's finished
1811  *
1812  * \param p_input the input thread
1813  * \param p_es the es descriptor
1814  * \return nothing
1815  */
1816 void input_DecoderDelete( decoder_t *p_dec )
1817 {
1818     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1819
1820     vlc_cancel( p_owner->thread );
1821
1822     /* Make sure we aren't paused/waiting/decoding anymore */
1823     vlc_mutex_lock( &p_owner->lock );
1824     p_owner->b_paused = false;
1825     p_owner->b_waiting = false;
1826     p_owner->b_flushing = true;
1827     vlc_cond_signal( &p_owner->wait_request );
1828     vlc_mutex_unlock( &p_owner->lock );
1829
1830     vlc_join( p_owner->thread, NULL );
1831
1832     module_unneed( p_dec, p_dec->p_module );
1833
1834     /* */
1835     if( p_dec->p_owner->cc.b_supported )
1836     {
1837         int i;
1838         for( i = 0; i < 4; i++ )
1839             input_DecoderSetCcState( p_dec, false, i );
1840     }
1841
1842     /* Delete decoder */
1843     DeleteDecoder( p_dec );
1844 }
1845
1846 /**
1847  * Put a block_t in the decoder's fifo.
1848  * Thread-safe w.r.t. the decoder. May be a cancellation point.
1849  *
1850  * \param p_dec the decoder object
1851  * \param p_block the data block
1852  */
1853 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
1854 {
1855     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1856
1857     vlc_fifo_Lock( p_owner->p_fifo );
1858     if( !b_do_pace )
1859     {
1860         /* FIXME: ideally we would check the time amount of data
1861          * in the FIFO instead of its size. */
1862         /* 400 MiB, i.e. ~ 50mb/s for 60s */
1863         if( vlc_fifo_GetBytes( p_owner->p_fifo ) > 400*1024*1024 )
1864         {
1865             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
1866                       "consumed quickly enough), resetting fifo!" );
1867             block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
1868         }
1869     }
1870     else
1871     if( !p_owner->b_waiting )
1872     {   /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
1873          * Locking is not necessary as b_waiting is only read, not written by
1874          * the decoder thread. */
1875         while( vlc_fifo_GetCount( p_owner->p_fifo ) >= 10 )
1876             vlc_fifo_WaitCond( p_owner->p_fifo, &p_owner->wait_acknowledge );
1877     }
1878
1879     vlc_fifo_QueueUnlocked( p_owner->p_fifo, p_block );
1880     vlc_fifo_Unlock( p_owner->p_fifo );
1881 }
1882
1883 bool input_DecoderIsEmpty( decoder_t * p_dec )
1884 {
1885     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1886     assert( !p_owner->b_waiting );
1887
1888     bool b_empty = block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
1889
1890     if( b_empty )
1891     {
1892         vlc_mutex_lock( &p_owner->lock );
1893         /* TODO subtitles support */
1894         if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->p_vout )
1895             b_empty = vout_IsEmpty( p_owner->p_vout );
1896         else if( p_owner->fmt.i_cat == AUDIO_ES && p_owner->p_aout )
1897             b_empty = aout_DecIsEmpty( p_owner->p_aout );
1898         vlc_mutex_unlock( &p_owner->lock );
1899     }
1900     return b_empty;
1901 }
1902
1903 /**
1904  * Signals that there are no further blocks to decode, and requests that the
1905  * decoder drain all pending buffers. This is used to ensure that all
1906  * intermediate buffers empty and no samples get lost at the end of the stream.
1907  *
1908  * @note The function does not actually wait for draining. It just signals that
1909  * draining should be performed once the decoder has emptied FIFO.
1910  */
1911 void input_DecoderDrain( decoder_t *p_dec )
1912 {
1913     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1914
1915     vlc_fifo_Lock( p_owner->p_fifo );
1916     p_owner->b_draining = true;
1917     vlc_fifo_Signal( p_owner->p_fifo );
1918     vlc_fifo_Unlock( p_owner->p_fifo );
1919 }
1920
1921 static void DecoderFlush( decoder_t *p_dec )
1922 {
1923     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1924
1925     vlc_assert_locked( &p_owner->lock );
1926
1927     vlc_fifo_Lock( p_owner->p_fifo );
1928     /* Empty the fifo */
1929     block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
1930     p_owner->b_draining = false; /* flush supersedes drain */
1931     vlc_fifo_Unlock( p_owner->p_fifo );
1932
1933     /* Monitor for flush end */
1934     p_owner->b_flushing = true;
1935     vlc_cond_signal( &p_owner->wait_request );
1936
1937     /* Send a special block */
1938     block_t *p_null = DecoderBlockFlushNew();
1939     if( !p_null )
1940         return;
1941     input_DecoderDecode( p_dec, p_null, false );
1942
1943     /* */
1944     while( p_owner->b_flushing )
1945         vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
1946 }
1947
1948 /**
1949  * Requests that the decoder immediately discard all pending buffers.
1950  * This is useful when seeking or when deselecting a stream.
1951  */
1952 void input_DecoderFlush( decoder_t *p_dec )
1953 {
1954     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1955
1956     vlc_mutex_lock( &p_owner->lock );
1957     DecoderFlush( p_dec );
1958     vlc_mutex_unlock( &p_owner->lock );
1959 }
1960
1961 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
1962 {
1963     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1964     int i;
1965
1966     vlc_mutex_lock( &p_owner->lock );
1967     for( i = 0; i < 4; i++ )
1968         pb_present[i] =  p_owner->cc.pb_present[i];
1969     vlc_mutex_unlock( &p_owner->lock );
1970 }
1971
1972 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
1973 {
1974     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1975
1976     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
1977
1978     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
1979         return VLC_EGENERIC;
1980
1981     if( b_decode )
1982     {
1983         static const vlc_fourcc_t fcc[4] = {
1984             VLC_FOURCC('c', 'c', '1', ' '),
1985             VLC_FOURCC('c', 'c', '2', ' '),
1986             VLC_FOURCC('c', 'c', '3', ' '),
1987             VLC_FOURCC('c', 'c', '4', ' '),
1988         };
1989         decoder_t *p_cc;
1990         es_format_t fmt;
1991
1992         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
1993         p_cc = input_DecoderNew( p_owner->p_input, &fmt,
1994                               p_dec->p_owner->p_clock, p_owner->p_sout );
1995         if( !p_cc )
1996         {
1997             msg_Err( p_dec, "could not create decoder" );
1998             dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
1999                           _("VLC could not open the decoder module.") );
2000             return VLC_EGENERIC;
2001         }
2002         else if( !p_cc->p_module )
2003         {
2004             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
2005             input_DecoderDelete(p_cc);
2006             return VLC_EGENERIC;
2007         }
2008         p_cc->p_owner->p_clock = p_owner->p_clock;
2009
2010         vlc_mutex_lock( &p_owner->lock );
2011         p_owner->cc.pp_decoder[i_channel] = p_cc;
2012         vlc_mutex_unlock( &p_owner->lock );
2013     }
2014     else
2015     {
2016         decoder_t *p_cc;
2017
2018         vlc_mutex_lock( &p_owner->lock );
2019         p_cc = p_owner->cc.pp_decoder[i_channel];
2020         p_owner->cc.pp_decoder[i_channel] = NULL;
2021         vlc_mutex_unlock( &p_owner->lock );
2022
2023         if( p_cc )
2024             input_DecoderDelete(p_cc);
2025     }
2026     return VLC_SUCCESS;
2027 }
2028
2029 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
2030 {
2031     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2032
2033     *pb_decode = false;
2034     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
2035         return VLC_EGENERIC;
2036
2037     vlc_mutex_lock( &p_owner->lock );
2038     *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
2039     vlc_mutex_unlock( &p_owner->lock );
2040     return VLC_EGENERIC;
2041 }
2042
2043 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
2044 {
2045     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2046
2047     vlc_mutex_lock( &p_owner->lock );
2048     /* Normally, p_owner->b_paused != b_paused here. But if a track is added
2049      * while the input is paused (e.g. add sub file), then b_paused is
2050      * (incorrectly) false. */
2051     if( likely(p_owner->b_paused != b_paused) ) {
2052         p_owner->b_paused = b_paused;
2053         p_owner->pause.i_date = i_date;
2054         p_owner->pause.i_ignore = 0;
2055         vlc_cond_signal( &p_owner->wait_request );
2056
2057         /* XXX only audio and video output have to be paused.
2058          * - for sout it is useless
2059          * - for subs, it is done by the vout
2060          */
2061         if( p_owner->fmt.i_cat == AUDIO_ES )
2062         {
2063             if( p_owner->p_aout )
2064                 aout_DecChangePause( p_owner->p_aout, b_paused, i_date );
2065         }
2066         else if( p_owner->fmt.i_cat == VIDEO_ES )
2067         {
2068             if( p_owner->p_vout )
2069                 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
2070         }
2071     }
2072     vlc_mutex_unlock( &p_owner->lock );
2073 }
2074
2075 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
2076 {
2077     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2078
2079     vlc_mutex_lock( &p_owner->lock );
2080     p_owner->i_ts_delay = i_delay;
2081     vlc_mutex_unlock( &p_owner->lock );
2082 }
2083
2084 void input_DecoderStartWait( decoder_t *p_dec )
2085 {
2086     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2087
2088     assert( !p_owner->b_waiting );
2089
2090     vlc_mutex_lock( &p_owner->lock );
2091     p_owner->b_first = true;
2092     p_owner->b_has_data = false;
2093     p_owner->b_waiting = true;
2094     vlc_cond_signal( &p_owner->wait_request );
2095     vlc_mutex_unlock( &p_owner->lock );
2096 }
2097
2098 void input_DecoderStopWait( decoder_t *p_dec )
2099 {
2100     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2101
2102     assert( p_owner->b_waiting );
2103
2104     vlc_mutex_lock( &p_owner->lock );
2105     p_owner->b_waiting = false;
2106     vlc_cond_signal( &p_owner->wait_request );
2107     vlc_mutex_unlock( &p_owner->lock );
2108 }
2109
2110 void input_DecoderWait( decoder_t *p_dec )
2111 {
2112     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2113
2114     assert( p_owner->b_waiting );
2115
2116     vlc_mutex_lock( &p_owner->lock );
2117     vlc_fifo_Lock( p_owner->p_fifo );
2118     while( !p_owner->b_has_data )
2119     {
2120         if( p_owner->b_idle )
2121         {
2122             assert( vlc_fifo_IsEmpty( p_owner->p_fifo ) );
2123             msg_Warn( p_dec, "can't wait without data to decode" );
2124             break;
2125         }
2126         vlc_fifo_Signal( p_owner->p_fifo );
2127         vlc_fifo_Unlock( p_owner->p_fifo );
2128         vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
2129         vlc_fifo_Lock( p_owner->p_fifo );
2130     }
2131     vlc_fifo_Unlock( p_owner->p_fifo );
2132     vlc_mutex_unlock( &p_owner->lock );
2133 }
2134
2135 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
2136 {
2137     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2138
2139     *pi_duration = 0;
2140
2141     vlc_mutex_lock( &p_owner->lock );
2142     if( p_owner->fmt.i_cat == VIDEO_ES )
2143     {
2144         if( p_owner->b_paused && p_owner->p_vout )
2145         {
2146             vout_NextPicture( p_owner->p_vout, pi_duration );
2147             p_owner->pause.i_ignore++;
2148             vlc_cond_signal( &p_owner->wait_request );
2149         }
2150     }
2151     else
2152     {
2153         /* TODO subtitle should not be flushed */
2154         p_owner->b_waiting = false;
2155         DecoderFlush( p_dec );
2156     }
2157     vlc_mutex_unlock( &p_owner->lock );
2158 }
2159
2160 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
2161 {
2162     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2163     bool b_changed;
2164
2165     vlc_mutex_lock( &p_owner->lock );
2166     b_changed = p_owner->b_fmt_description;
2167     if( b_changed )
2168     {
2169         if( p_fmt != NULL )
2170             es_format_Copy( p_fmt, &p_owner->fmt );
2171
2172         if( pp_meta )
2173         {
2174             *pp_meta = NULL;
2175             if( p_owner->p_description )
2176             {
2177                 *pp_meta = vlc_meta_New();
2178                 if( *pp_meta )
2179                     vlc_meta_Merge( *pp_meta, p_owner->p_description );
2180             }
2181         }
2182         p_owner->b_fmt_description = false;
2183     }
2184     vlc_mutex_unlock( &p_owner->lock );
2185     return b_changed;
2186 }
2187
2188 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
2189 {
2190     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2191
2192     return block_FifoSize( p_owner->p_fifo );
2193 }
2194
2195 void input_DecoderGetObjects( decoder_t *p_dec,
2196                               vout_thread_t **pp_vout, audio_output_t **pp_aout )
2197 {
2198     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2199
2200     vlc_mutex_lock( &p_owner->lock );
2201     if( pp_vout )
2202         *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
2203     if( pp_aout )
2204         *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
2205     vlc_mutex_unlock( &p_owner->lock );
2206 }