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