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