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