]> git.sesse.net Git - vlc/blob - src/input/decoder.c
Audio decoder: output buffered decoded frames on EOF
[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 (p_owner->buffer.i_count) /* buffered frames */
426         b_empty = false;
427
428     if( b_empty )
429     {
430         vlc_mutex_lock( &p_owner->lock );
431         /* TODO subtitles support */
432         if( p_dec->fmt_out.i_cat == VIDEO_ES && p_owner->p_vout )
433             b_empty = vout_IsEmpty( p_owner->p_vout );
434         else if( p_dec->fmt_out.i_cat == AUDIO_ES && p_owner->p_aout )
435             b_empty = aout_DecIsEmpty( p_owner->p_aout );
436         vlc_mutex_unlock( &p_owner->lock );
437     }
438     return b_empty;
439 }
440
441 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
442 {
443     decoder_owner_sys_t *p_owner = p_dec->p_owner;
444     int i;
445
446     vlc_mutex_lock( &p_owner->lock );
447     for( i = 0; i < 4; i++ )
448         pb_present[i] =  p_owner->cc.pb_present[i];
449     vlc_mutex_unlock( &p_owner->lock );
450 }
451 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
452 {
453     decoder_owner_sys_t *p_owner = p_dec->p_owner;
454
455     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
456
457     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
458         return VLC_EGENERIC;
459
460     if( b_decode )
461     {
462         static const vlc_fourcc_t fcc[4] = {
463             VLC_FOURCC('c', 'c', '1', ' '),
464             VLC_FOURCC('c', 'c', '2', ' '),
465             VLC_FOURCC('c', 'c', '3', ' '),
466             VLC_FOURCC('c', 'c', '4', ' '),
467         };
468         decoder_t *p_cc;
469         es_format_t fmt;
470
471         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
472         p_cc = input_DecoderNew( p_owner->p_input, &fmt,
473                               p_dec->p_owner->p_clock, p_owner->p_sout );
474         if( !p_cc )
475         {
476             msg_Err( p_dec, "could not create decoder" );
477             dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
478                           _("VLC could not open the decoder module.") );
479             return VLC_EGENERIC;
480         }
481         else if( !p_cc->p_module )
482         {
483             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
484             input_DecoderDelete(p_cc);
485             return VLC_EGENERIC;
486         }
487         p_cc->p_owner->p_clock = p_owner->p_clock;
488
489         vlc_mutex_lock( &p_owner->lock );
490         p_owner->cc.pp_decoder[i_channel] = p_cc;
491         vlc_mutex_unlock( &p_owner->lock );
492     }
493     else
494     {
495         decoder_t *p_cc;
496
497         vlc_mutex_lock( &p_owner->lock );
498         p_cc = p_owner->cc.pp_decoder[i_channel];
499         p_owner->cc.pp_decoder[i_channel] = NULL;
500         vlc_mutex_unlock( &p_owner->lock );
501
502         if( p_cc )
503             input_DecoderDelete(p_cc);
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         bool end_buffering = !p_block || p_block->i_flags & BLOCK_FLAG_CORE_EOS;
930         DecoderSignalBuffering( p_dec, end_buffering );
931         if (end_buffering)
932             input_DecoderStopBuffering( p_dec );
933
934         if( p_block )
935         {
936             int canc = vlc_savecancel();
937
938             if( p_block->i_flags & BLOCK_FLAG_CORE_EOS )
939             {
940                 /* calling DecoderProcess() with NULL block will make
941                  * decoders/packetizers flush their buffers */
942                 block_Release( p_block );
943                 p_block = NULL;
944             }
945
946             if( p_dec->b_error )
947                 DecoderError( p_dec, p_block );
948             else
949                 DecoderProcess( p_dec, p_block );
950
951             vlc_restorecancel( canc );
952         }
953     }
954     return NULL;
955 }
956
957 static block_t *DecoderBlockFlushNew()
958 {
959     block_t *p_null = block_Alloc( 128 );
960     if( !p_null )
961         return NULL;
962
963     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
964                        BLOCK_FLAG_CORRUPTED |
965                        BLOCK_FLAG_CORE_FLUSH;
966     memset( p_null->p_buffer, 0, p_null->i_buffer );
967
968     return p_null;
969 }
970
971 static void DecoderFlush( decoder_t *p_dec )
972 {
973     decoder_owner_sys_t *p_owner = p_dec->p_owner;
974
975     vlc_assert_locked( &p_owner->lock );
976
977     /* Empty the fifo */
978     block_FifoEmpty( p_owner->p_fifo );
979
980     /* Monitor for flush end */
981     p_owner->b_flushing = true;
982     vlc_cond_signal( &p_owner->wait_request );
983
984     /* Send a special block */
985     block_t *p_null = DecoderBlockFlushNew();
986     if( !p_null )
987         return;
988     input_DecoderDecode( p_dec, p_null, false );
989
990     /* */
991     while( p_owner->b_flushing )
992         vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
993 }
994
995 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
996 {
997     decoder_owner_sys_t *p_owner = p_dec->p_owner;
998
999     vlc_mutex_lock( &p_owner->lock );
1000
1001     if( p_owner->b_buffering )
1002     {
1003         if( b_full )
1004             p_owner->buffer.b_full = true;
1005         vlc_cond_signal( &p_owner->wait_acknowledge );
1006     }
1007
1008     vlc_mutex_unlock( &p_owner->lock );
1009 }
1010
1011 static bool DecoderIsFlushing( decoder_t *p_dec )
1012 {
1013     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1014     bool b_flushing;
1015
1016     vlc_mutex_lock( &p_owner->lock );
1017
1018     b_flushing = p_owner->b_flushing;
1019
1020     vlc_mutex_unlock( &p_owner->lock );
1021
1022     return b_flushing;
1023 }
1024
1025 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
1026 {
1027     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1028
1029     vlc_assert_locked( &p_owner->lock );
1030
1031     for( ;; )
1032     {
1033         if( p_owner->b_flushing )
1034             break;
1035         if( p_owner->b_paused )
1036         {
1037             if( p_owner->b_buffering && !p_owner->buffer.b_full )
1038                 break;
1039             if( p_owner->pause.i_ignore > 0 )
1040             {
1041                 p_owner->pause.i_ignore--;
1042                 break;
1043             }
1044         }
1045         else
1046         {
1047             if( !p_owner->b_buffering || !p_owner->buffer.b_full )
1048                 break;
1049         }
1050         vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
1051     }
1052
1053     if( pb_reject )
1054         *pb_reject = p_owner->b_flushing;
1055 }
1056
1057 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
1058 {
1059     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1060
1061     vlc_assert_locked( &p_owner->lock );
1062
1063     /* XXX only audio and video output have to be paused.
1064      * - for sout it is useless
1065      * - for subs, it is done by the vout
1066      */
1067     if( p_dec->fmt_out.i_cat == AUDIO_ES )
1068     {
1069         if( p_owner->p_aout )
1070             aout_DecChangePause( p_owner->p_aout, b_paused, i_date );
1071     }
1072     else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1073     {
1074         if( p_owner->p_vout )
1075             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1076     }
1077 }
1078 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1079 {
1080     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1081         *pi_preroll = INT64_MAX;
1082     else if( p->i_dts > VLC_TS_INVALID )
1083         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1084     else if( p->i_pts > VLC_TS_INVALID )
1085         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1086 }
1087
1088 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1089                           mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
1090 {
1091     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1092     input_clock_t   *p_clock = p_owner->p_clock;
1093
1094     vlc_assert_locked( &p_owner->lock );
1095
1096     const mtime_t i_es_delay = p_owner->i_ts_delay;
1097
1098     if( !p_clock )
1099         return;
1100
1101     const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1102     int i_rate;
1103
1104     if( *pi_ts0 > VLC_TS_INVALID )
1105     {
1106         *pi_ts0 += i_es_delay;
1107         if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1108             *pi_ts1 += i_es_delay;
1109         if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1110             *pi_ts0 = VLC_TS_INVALID;
1111     }
1112     else
1113     {
1114         i_rate = input_clock_GetRate( p_clock );
1115     }
1116
1117     /* Do not create ephemere data because of rounding errors */
1118     if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1119         *pi_ts1 += 1;
1120
1121     if( pi_duration )
1122         *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
1123             / INPUT_RATE_DEFAULT;
1124
1125     if( pi_rate )
1126         *pi_rate = i_rate;
1127 }
1128
1129 static bool DecoderIsExitRequested( decoder_t *p_dec )
1130 {
1131     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1132
1133     vlc_mutex_lock( &p_owner->lock );
1134     bool b_exit = p_owner->b_exit;
1135     vlc_mutex_unlock( &p_owner->lock );
1136
1137     return b_exit;
1138 }
1139
1140 /**
1141  * If *pb_reject, it does nothing, otherwise it waits for the given
1142  * deadline or a flush request (in which case it set *pi_reject to true.
1143  */
1144 static void DecoderWaitDate( decoder_t *p_dec,
1145                              bool *pb_reject, mtime_t i_deadline )
1146 {
1147     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1148
1149     vlc_assert_locked( &p_owner->lock );
1150
1151     if( *pb_reject || i_deadline < 0 )
1152         return;
1153
1154     do
1155     {
1156         if( p_owner->b_flushing || p_owner->b_exit )
1157         {
1158             *pb_reject = true;
1159             break;
1160         }
1161     }
1162     while( vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1163                                i_deadline ) == 0 );
1164 }
1165
1166 static void DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
1167                               int *pi_played_sum, int *pi_lost_sum )
1168 {
1169     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1170     audio_output_t *p_aout = p_owner->p_aout;
1171
1172     /* */
1173     if( p_audio && p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1174     {
1175         msg_Warn( p_dec, "non-dated audio buffer received" );
1176         *pi_lost_sum += 1;
1177         block_Release( p_audio );
1178         return;
1179     }
1180
1181     /* */
1182     vlc_mutex_lock( &p_owner->lock );
1183
1184     if( p_audio && (p_owner->b_buffering || p_owner->buffer.p_audio) )
1185     {
1186         p_audio->p_next = NULL;
1187
1188         *p_owner->buffer.pp_audio_next = p_audio;
1189         p_owner->buffer.pp_audio_next = &p_audio->p_next;
1190
1191         p_owner->buffer.i_count++;
1192         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1193             p_audio->i_pts - p_owner->buffer.p_audio->i_pts > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1194         {
1195             p_owner->buffer.b_full = true;
1196             vlc_cond_signal( &p_owner->wait_acknowledge );
1197         }
1198     }
1199
1200     for( ;; )
1201     {
1202         bool b_has_more = false, b_paused, b_reject;
1203
1204         DecoderWaitUnblock( p_dec, &b_reject );
1205         if( p_owner->b_buffering )
1206             break;
1207
1208         b_paused = p_owner->b_paused;
1209
1210         /* */
1211         if( p_owner->buffer.p_audio )
1212         {
1213             p_audio = p_owner->buffer.p_audio;
1214
1215             p_owner->buffer.p_audio = p_audio->p_next;
1216             p_owner->buffer.i_count--;
1217
1218             b_has_more = p_owner->buffer.p_audio != NULL;
1219             if( !b_has_more )
1220                 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1221         }
1222
1223         if (!p_audio)
1224             break;
1225
1226         /* */
1227         int i_rate = INPUT_RATE_DEFAULT;
1228
1229         DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1230                       &i_rate, AOUT_MAX_ADVANCE_TIME );
1231
1232         if( p_audio->i_pts <= VLC_TS_INVALID
1233          || i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1234          || i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1235             b_reject = true;
1236
1237         DecoderWaitDate( p_dec, &b_reject,
1238                          p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1239
1240         if( unlikely(p_owner->b_paused != b_paused) )
1241             continue; /* race with input thread? retry... */
1242         if( p_aout == NULL )
1243             b_reject = true;
1244
1245         if( !b_reject )
1246         {
1247             assert( !p_owner->b_paused );
1248             if( !aout_DecPlay( p_aout, p_audio, i_rate ) )
1249                 *pi_played_sum += 1;
1250             *pi_lost_sum += aout_DecGetResetLost( p_aout );
1251         }
1252         else
1253         {
1254             msg_Dbg( p_dec, "discarded audio buffer" );
1255             *pi_lost_sum += 1;
1256             block_Release( p_audio );
1257         }
1258
1259         if( !b_has_more )
1260             break;
1261         if( !p_owner->buffer.p_audio )
1262             break;
1263     }
1264     vlc_mutex_unlock( &p_owner->lock );
1265 }
1266
1267 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1268 {
1269     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1270     block_t *p_aout_buf;
1271     int i_decoded = 0;
1272     int i_lost = 0;
1273     int i_played = 0;
1274
1275     if (!p_block) {
1276         /* Play a NULL block to output buffered frames */
1277         DecoderPlayAudio( p_dec, NULL, &i_played, &i_lost );
1278     }
1279     else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1280     {
1281         if( DecoderIsExitRequested( p_dec ) )
1282         {
1283             /* It prevent freezing VLC in case of broken decoder */
1284             block_Release( p_aout_buf );
1285             if( p_block )
1286                 block_Release( p_block );
1287             break;
1288         }
1289         i_decoded++;
1290
1291         if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1292             p_aout_buf->i_pts < p_owner->i_preroll_end )
1293         {
1294             block_Release( p_aout_buf );
1295             continue;
1296         }
1297
1298         if( p_owner->i_preroll_end > VLC_TS_INVALID )
1299         {
1300             msg_Dbg( p_dec, "End of audio preroll" );
1301             if( p_owner->p_aout )
1302                 aout_DecFlush( p_owner->p_aout );
1303             /* */
1304             p_owner->i_preroll_end = VLC_TS_INVALID;
1305         }
1306
1307         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1308     }
1309
1310     /* Update ugly stat */
1311     input_thread_t  *p_input = p_owner->p_input;
1312
1313     if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1314     {
1315         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1316         stats_Update( p_input->p->counters.p_lost_abuffers, i_lost, NULL );
1317         stats_Update( p_input->p->counters.p_played_abuffers, i_played, NULL );
1318         stats_Update( p_input->p->counters.p_decoded_audio, i_decoded, NULL );
1319         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1320     }
1321 }
1322 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1323 {
1324     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1325     block_t *p_cc;
1326     bool pb_present[4];
1327     bool b_processed = false;
1328     int i;
1329     int i_cc_decoder;
1330
1331     assert( p_dec_cc->pf_get_cc != NULL );
1332
1333     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1334     if( !p_owner->cc.b_supported )
1335         return;
1336
1337     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1338     if( !p_cc )
1339         return;
1340
1341     vlc_mutex_lock( &p_owner->lock );
1342     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1343     {
1344         p_owner->cc.pb_present[i] |= pb_present[i];
1345         if( p_owner->cc.pp_decoder[i] )
1346             i_cc_decoder++;
1347     }
1348
1349     for( i = 0; i < 4; i++ )
1350     {
1351         if( !p_owner->cc.pp_decoder[i] )
1352             continue;
1353
1354         block_FifoPut( p_owner->cc.pp_decoder[i]->p_owner->p_fifo,
1355             (i_cc_decoder > 1) ? block_Duplicate(p_cc) : p_cc);
1356
1357         i_cc_decoder--;
1358         b_processed = true;
1359     }
1360     vlc_mutex_unlock( &p_owner->lock );
1361
1362     if( !b_processed )
1363         block_Release( p_cc );
1364 }
1365
1366 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1367                               int *pi_played_sum, int *pi_lost_sum )
1368 {
1369     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1370     vout_thread_t  *p_vout = p_owner->p_vout;
1371     bool b_first_buffered;
1372
1373     if( p_picture->date <= VLC_TS_INVALID )
1374     {
1375         msg_Warn( p_dec, "non-dated video buffer received" );
1376         *pi_lost_sum += 1;
1377         vout_ReleasePicture( p_vout, p_picture );
1378         return;
1379     }
1380
1381     /* */
1382     vlc_mutex_lock( &p_owner->lock );
1383
1384     if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1385     {
1386         p_picture->p_next = NULL;
1387
1388         *p_owner->buffer.pp_picture_next = p_picture;
1389         p_owner->buffer.pp_picture_next = &p_picture->p_next;
1390
1391         p_owner->buffer.i_count++;
1392         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1393             p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1394         {
1395             p_owner->buffer.b_full = true;
1396             vlc_cond_signal( &p_owner->wait_acknowledge );
1397         }
1398     }
1399     b_first_buffered = p_owner->buffer.p_picture != NULL;
1400
1401     for( ;; b_first_buffered = false )
1402     {
1403         bool b_has_more = false;
1404
1405         bool b_reject;
1406
1407         DecoderWaitUnblock( p_dec, &b_reject );
1408
1409         if( p_owner->b_buffering && !p_owner->buffer.b_first )
1410         {
1411             vlc_mutex_unlock( &p_owner->lock );
1412             return;
1413         }
1414         bool b_buffering_first = p_owner->b_buffering;
1415
1416         /* */
1417         if( p_owner->buffer.p_picture )
1418         {
1419             p_picture = p_owner->buffer.p_picture;
1420
1421             p_owner->buffer.p_picture = p_picture->p_next;
1422             p_owner->buffer.i_count--;
1423
1424             b_has_more = p_owner->buffer.p_picture != NULL;
1425             if( !b_has_more )
1426                 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1427         }
1428
1429         /* */
1430         if( b_buffering_first )
1431         {
1432             assert( p_owner->buffer.b_first );
1433             assert( !p_owner->buffer.i_count );
1434             msg_Dbg( p_dec, "Received first picture" );
1435             p_owner->buffer.b_first = false;
1436             p_picture->b_force = true;
1437         }
1438
1439         const bool b_dated = p_picture->date > VLC_TS_INVALID;
1440         int i_rate = INPUT_RATE_DEFAULT;
1441         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1442                       &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1443
1444         vlc_mutex_unlock( &p_owner->lock );
1445
1446         /* */
1447         if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1448             b_reject = true;
1449
1450         if( !b_reject )
1451         {
1452             if( i_rate != p_owner->i_last_rate || b_first_buffered )
1453             {
1454                 /* Be sure to not display old picture after our own */
1455                 vout_Flush( p_vout, p_picture->date );
1456                 p_owner->i_last_rate = i_rate;
1457             }
1458             vout_PutPicture( p_vout, p_picture );
1459         }
1460         else
1461         {
1462             if( b_dated )
1463                 msg_Warn( p_dec, "early picture skipped" );
1464             else
1465                 msg_Warn( p_dec, "non-dated video buffer received" );
1466
1467             *pi_lost_sum += 1;
1468             vout_ReleasePicture( p_vout, p_picture );
1469         }
1470         int i_tmp_display;
1471         int i_tmp_lost;
1472         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1473
1474         *pi_played_sum += i_tmp_display;
1475         *pi_lost_sum += i_tmp_lost;
1476
1477         if( !b_has_more || b_buffering_first )
1478             break;
1479
1480         vlc_mutex_lock( &p_owner->lock );
1481         if( !p_owner->buffer.p_picture )
1482         {
1483             vlc_mutex_unlock( &p_owner->lock );
1484             break;
1485         }
1486     }
1487 }
1488
1489 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1490 {
1491     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1492     picture_t      *p_pic;
1493     int i_lost = 0;
1494     int i_decoded = 0;
1495     int i_displayed = 0;
1496
1497     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1498     {
1499         vout_thread_t  *p_vout = p_owner->p_vout;
1500         if( DecoderIsExitRequested( p_dec ) )
1501         {
1502             /* It prevent freezing VLC in case of broken decoder */
1503             vout_ReleasePicture( p_vout, p_pic );
1504             if( p_block )
1505                 block_Release( p_block );
1506             break;
1507         }
1508
1509         i_decoded++;
1510
1511         if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1512         {
1513             vout_ReleasePicture( p_vout, p_pic );
1514             continue;
1515         }
1516
1517         if( p_owner->i_preroll_end > VLC_TS_INVALID )
1518         {
1519             msg_Dbg( p_dec, "End of video preroll" );
1520             if( p_vout )
1521                 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1522             /* */
1523             p_owner->i_preroll_end = VLC_TS_INVALID;
1524         }
1525
1526         if( p_dec->pf_get_cc &&
1527             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1528             DecoderGetCc( p_dec, p_dec );
1529
1530         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1531     }
1532
1533     /* Update ugly stat */
1534     input_thread_t *p_input = p_owner->p_input;
1535
1536     if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
1537     {
1538         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1539         stats_Update( p_input->p->counters.p_decoded_video, i_decoded, NULL );
1540         stats_Update( p_input->p->counters.p_lost_pictures, i_lost , NULL);
1541         stats_Update( p_input->p->counters.p_displayed_pictures,
1542                       i_displayed, NULL);
1543         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1544     }
1545 }
1546
1547 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1548 {
1549     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1550     vout_thread_t *p_vout = p_owner->p_spu_vout;
1551
1552     /* */
1553     if( p_subpic->i_start <= VLC_TS_INVALID )
1554     {
1555         msg_Warn( p_dec, "non-dated spu buffer received" );
1556         subpicture_Delete( p_subpic );
1557         return;
1558     }
1559
1560     /* */
1561     vlc_mutex_lock( &p_owner->lock );
1562
1563     if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1564     {
1565         p_subpic->p_next = NULL;
1566
1567         *p_owner->buffer.pp_subpic_next = p_subpic;
1568         p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1569
1570         p_owner->buffer.i_count++;
1571         /* XXX it is important to be full after the first one */
1572         if( p_owner->buffer.i_count > 0 )
1573         {
1574             p_owner->buffer.b_full = true;
1575             vlc_cond_signal( &p_owner->wait_acknowledge );
1576         }
1577     }
1578
1579     for( ;; )
1580     {
1581         bool b_has_more = false;
1582         bool b_reject;
1583         DecoderWaitUnblock( p_dec, &b_reject );
1584
1585         if( p_owner->b_buffering )
1586         {
1587             vlc_mutex_unlock( &p_owner->lock );
1588             return;
1589         }
1590
1591         /* */
1592         if( p_owner->buffer.p_subpic )
1593         {
1594             p_subpic = p_owner->buffer.p_subpic;
1595
1596             p_owner->buffer.p_subpic = p_subpic->p_next;
1597             p_owner->buffer.i_count--;
1598
1599             b_has_more = p_owner->buffer.p_subpic != NULL;
1600             if( !b_has_more )
1601                 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1602         }
1603
1604         /* */
1605         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1606                       NULL, INT64_MAX );
1607
1608         if( p_subpic->i_start <= VLC_TS_INVALID )
1609             b_reject = true;
1610
1611         DecoderWaitDate( p_dec, &b_reject,
1612                          p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1613         vlc_mutex_unlock( &p_owner->lock );
1614
1615         if( !b_reject )
1616             vout_PutSubpicture( p_vout, p_subpic );
1617         else
1618             subpicture_Delete( p_subpic );
1619
1620         if( !b_has_more )
1621             break;
1622         vlc_mutex_lock( &p_owner->lock );
1623         if( !p_owner->buffer.p_subpic )
1624         {
1625             vlc_mutex_unlock( &p_owner->lock );
1626             break;
1627         }
1628     }
1629 }
1630
1631 #ifdef ENABLE_SOUT
1632 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
1633 {
1634     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1635
1636     assert( p_owner->p_clock );
1637     assert( !p_sout_block->p_next );
1638
1639     vlc_mutex_lock( &p_owner->lock );
1640
1641     if( p_owner->b_buffering || p_owner->buffer.p_block )
1642     {
1643         block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1644
1645         p_owner->buffer.i_count++;
1646         /* XXX it is important to be full after the first one */
1647         if( p_owner->buffer.i_count > 0 )
1648         {
1649             p_owner->buffer.b_full = true;
1650             vlc_cond_signal( &p_owner->wait_acknowledge );
1651         }
1652     }
1653
1654     for( ;; )
1655     {
1656         bool b_has_more = false;
1657         bool b_reject;
1658         DecoderWaitUnblock( p_dec, &b_reject );
1659
1660         if( p_owner->b_buffering )
1661         {
1662             vlc_mutex_unlock( &p_owner->lock );
1663             return;
1664         }
1665
1666         /* */
1667         if( p_owner->buffer.p_block )
1668         {
1669             p_sout_block = p_owner->buffer.p_block;
1670
1671             p_owner->buffer.p_block = p_sout_block->p_next;
1672             p_owner->buffer.i_count--;
1673
1674             b_has_more = p_owner->buffer.p_block != NULL;
1675             if( !b_has_more )
1676                 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1677         }
1678         p_sout_block->p_next = NULL;
1679
1680         DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1681                       &p_sout_block->i_length, NULL, INT64_MAX );
1682
1683         vlc_mutex_unlock( &p_owner->lock );
1684
1685         if( !b_reject )
1686             sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1687         else
1688             block_Release( p_sout_block );
1689
1690         if( !b_has_more )
1691             break;
1692         vlc_mutex_lock( &p_owner->lock );
1693         if( !p_owner->buffer.p_block )
1694         {
1695             vlc_mutex_unlock( &p_owner->lock );
1696             break;
1697         }
1698     }
1699 }
1700 #endif
1701
1702 /* */
1703 static void DecoderFlushBuffering( decoder_t *p_dec )
1704 {
1705     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1706
1707     vlc_assert_locked( &p_owner->lock );
1708
1709     while( p_owner->buffer.p_picture )
1710     {
1711         picture_t *p_picture = p_owner->buffer.p_picture;
1712
1713         p_owner->buffer.p_picture = p_picture->p_next;
1714         p_owner->buffer.i_count--;
1715
1716         if( p_owner->p_vout )
1717         {
1718             vout_ReleasePicture( p_owner->p_vout, p_picture );
1719         }
1720
1721         if( !p_owner->buffer.p_picture )
1722             p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1723     }
1724     while( p_owner->buffer.p_audio )
1725     {
1726         block_t *p_audio = p_owner->buffer.p_audio;
1727
1728         p_owner->buffer.p_audio = p_audio->p_next;
1729         p_owner->buffer.i_count--;
1730
1731         block_Release( p_audio );
1732
1733         if( !p_owner->buffer.p_audio )
1734             p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1735     }
1736     while( p_owner->buffer.p_subpic )
1737     {
1738         subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1739
1740         p_owner->buffer.p_subpic = p_subpic->p_next;
1741         p_owner->buffer.i_count--;
1742
1743         subpicture_Delete( p_subpic );
1744
1745         if( !p_owner->buffer.p_subpic )
1746             p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1747     }
1748     if( p_owner->buffer.p_block )
1749     {
1750         block_ChainRelease( p_owner->buffer.p_block );
1751
1752         p_owner->buffer.i_count = 0;
1753         p_owner->buffer.p_block = NULL;
1754         p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1755     }
1756 }
1757
1758 #ifdef ENABLE_SOUT
1759 /* This function process a block for sout
1760  */
1761 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1762 {
1763     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1764     block_t *p_sout_block;
1765
1766     while( ( p_sout_block =
1767                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1768     {
1769         if( !p_owner->p_sout_input )
1770         {
1771             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1772
1773             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1774             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1775             if( p_dec->fmt_in.psz_language )
1776             {
1777                 free( p_owner->sout.psz_language );
1778                 p_owner->sout.psz_language =
1779                     strdup( p_dec->fmt_in.psz_language );
1780             }
1781
1782             p_owner->p_sout_input =
1783                 sout_InputNew( p_owner->p_sout,
1784                                &p_owner->sout );
1785
1786             if( p_owner->p_sout_input == NULL )
1787             {
1788                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1789                          (char *)&p_owner->sout.i_codec );
1790                 p_dec->b_error = true;
1791
1792                 while( p_sout_block )
1793                 {
1794                     block_t *p_next = p_sout_block->p_next;
1795                     block_Release( p_sout_block );
1796                     p_sout_block = p_next;
1797                 }
1798                 break;
1799             }
1800         }
1801
1802         while( p_sout_block )
1803         {
1804             block_t *p_next = p_sout_block->p_next;
1805
1806             p_sout_block->p_next = NULL;
1807
1808             DecoderPlaySout( p_dec, p_sout_block );
1809
1810             p_sout_block = p_next;
1811         }
1812     }
1813 }
1814 #endif
1815
1816 /* This function process a video block
1817  */
1818 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1819 {
1820     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1821
1822     if( p_owner->p_packetizer )
1823     {
1824         block_t *p_packetized_block;
1825         decoder_t *p_packetizer = p_owner->p_packetizer;
1826
1827         while( (p_packetized_block =
1828                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1829         {
1830             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1831             {
1832                 es_format_Clean( &p_dec->fmt_in );
1833                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1834             }
1835             if( p_packetizer->pf_get_cc )
1836                 DecoderGetCc( p_dec, p_packetizer );
1837
1838             while( p_packetized_block )
1839             {
1840                 block_t *p_next = p_packetized_block->p_next;
1841                 p_packetized_block->p_next = NULL;
1842
1843                 DecoderDecodeVideo( p_dec, p_packetized_block );
1844
1845                 p_packetized_block = p_next;
1846             }
1847         }
1848         /* The packetizer does not output a block that tell the decoder to flush
1849          * do it ourself */
1850         if( b_flush )
1851         {
1852             block_t *p_null = DecoderBlockFlushNew();
1853             if( p_null )
1854                 DecoderDecodeVideo( p_dec, p_null );
1855         }
1856     }
1857     else if( p_block )
1858     {
1859         DecoderDecodeVideo( p_dec, p_block );
1860     }
1861
1862     if( b_flush && p_owner->p_vout )
1863         vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1864 }
1865
1866 /* This function process a audio block
1867  */
1868 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1869 {
1870     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1871
1872     if( p_owner->p_packetizer )
1873     {
1874         block_t *p_packetized_block;
1875         decoder_t *p_packetizer = p_owner->p_packetizer;
1876
1877         while( (p_packetized_block =
1878                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1879         {
1880             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1881             {
1882                 es_format_Clean( &p_dec->fmt_in );
1883                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1884             }
1885
1886             while( p_packetized_block )
1887             {
1888                 block_t *p_next = p_packetized_block->p_next;
1889                 p_packetized_block->p_next = NULL;
1890
1891                 DecoderDecodeAudio( p_dec, p_packetized_block );
1892
1893                 p_packetized_block = p_next;
1894             }
1895         }
1896         /* The packetizer does not output a block that tell the decoder to flush
1897          * do it ourself */
1898         if( b_flush )
1899         {
1900             block_t *p_null = DecoderBlockFlushNew();
1901             if( p_null )
1902                 DecoderDecodeAudio( p_dec, p_null );
1903         }
1904     }
1905     else
1906     {
1907         DecoderDecodeAudio( p_dec, p_block );
1908     }
1909
1910     if( b_flush && p_owner->p_aout )
1911         aout_DecFlush( p_owner->p_aout );
1912 }
1913
1914 /* This function process a subtitle block
1915  */
1916 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1917 {
1918     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1919
1920     input_thread_t *p_input = p_owner->p_input;
1921     vout_thread_t *p_vout;
1922     subpicture_t *p_spu;
1923
1924     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1925     {
1926         if( p_input != NULL )
1927         {
1928             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1929             stats_Update( p_input->p->counters.p_decoded_sub, 1, NULL );
1930             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1931         }
1932
1933         p_vout = input_resource_HoldVout( p_owner->p_resource );
1934         if( p_vout && p_owner->p_spu_vout == p_vout )
1935         {
1936             /* Preroll does not work very well with subtitle */
1937             if( p_spu->i_start > VLC_TS_INVALID &&
1938                 p_spu->i_start < p_owner->i_preroll_end &&
1939                 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1940             {
1941                 subpicture_Delete( p_spu );
1942             }
1943             else
1944             {
1945                 DecoderPlaySpu( p_dec, p_spu );
1946             }
1947         }
1948         else
1949         {
1950             subpicture_Delete( p_spu );
1951         }
1952         if( p_vout )
1953             vlc_object_release( p_vout );
1954     }
1955
1956     if( b_flush && p_owner->p_spu_vout )
1957     {
1958         p_vout = input_resource_HoldVout( p_owner->p_resource );
1959
1960         if( p_vout && p_owner->p_spu_vout == p_vout )
1961             vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1962
1963         if( p_vout )
1964             vlc_object_release( p_vout );
1965     }
1966 }
1967
1968 /* */
1969 static void DecoderProcessOnFlush( decoder_t *p_dec )
1970 {
1971     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1972
1973     vlc_mutex_lock( &p_owner->lock );
1974     DecoderFlushBuffering( p_dec );
1975
1976     if( p_owner->b_flushing )
1977     {
1978         p_owner->b_flushing = false;
1979         vlc_cond_signal( &p_owner->wait_acknowledge );
1980     }
1981     vlc_mutex_unlock( &p_owner->lock );
1982 }
1983
1984 /**
1985  * Decode a block
1986  *
1987  * \param p_dec the decoder object
1988  * \param p_block the block to decode
1989  * \return VLC_SUCCESS or an error code
1990  */
1991 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1992 {
1993     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1994     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1995
1996     if( p_block && p_block->i_buffer <= 0 )
1997     {
1998         assert( !b_flush_request );
1999         block_Release( p_block );
2000         return;
2001     }
2002
2003 #ifdef ENABLE_SOUT
2004     if( p_owner->b_packetizer )
2005     {
2006         if( p_block )
2007             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2008
2009         DecoderProcessSout( p_dec, p_block );
2010     }
2011     else
2012 #endif
2013     {
2014         bool b_flush = false;
2015
2016         if( p_block )
2017         {
2018             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
2019             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
2020
2021             b_flush = !b_flushing && b_flush_request;
2022
2023             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2024         }
2025
2026         if( p_dec->fmt_out.i_cat == AUDIO_ES )
2027         {
2028             DecoderProcessAudio( p_dec, p_block, b_flush );
2029         }
2030         else if( p_dec->fmt_out.i_cat == VIDEO_ES )
2031         {
2032             DecoderProcessVideo( p_dec, p_block, b_flush );
2033         }
2034         else if( p_dec->fmt_out.i_cat == SPU_ES )
2035         {
2036             DecoderProcessSpu( p_dec, p_block, b_flush );
2037         }
2038         else
2039         {
2040             msg_Err( p_dec, "unknown ES format" );
2041             p_dec->b_error = true;
2042         }
2043     }
2044
2045     /* */
2046     if( b_flush_request )
2047         DecoderProcessOnFlush( p_dec );
2048 }
2049
2050 static void DecoderError( decoder_t *p_dec, block_t *p_block )
2051 {
2052     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2053
2054     /* */
2055     if( p_block )
2056         block_Release( p_block );
2057
2058     if( b_flush_request )
2059         DecoderProcessOnFlush( p_dec );
2060 }
2061
2062
2063 /**
2064  * Destroys a decoder object
2065  *
2066  * \param p_dec the decoder object
2067  * \return nothing
2068  */
2069 static void DeleteDecoder( decoder_t * p_dec )
2070 {
2071     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2072
2073     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2074              (char*)&p_dec->fmt_in.i_codec,
2075              (unsigned)block_FifoCount( p_owner->p_fifo ) );
2076
2077     /* Free all packets still in the decoder fifo. */
2078     block_FifoEmpty( p_owner->p_fifo );
2079     block_FifoRelease( p_owner->p_fifo );
2080
2081     /* */
2082     vlc_mutex_lock( &p_owner->lock );
2083     DecoderFlushBuffering( p_dec );
2084     vlc_mutex_unlock( &p_owner->lock );
2085
2086     /* Cleanup */
2087     if( p_owner->p_aout )
2088     {
2089         /* TODO: REVISIT gap-less audio */
2090         aout_DecFlush( p_owner->p_aout );
2091         aout_DecDelete( p_owner->p_aout );
2092         input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
2093         if( p_owner->p_input != NULL )
2094             input_SendEventAout( p_owner->p_input );
2095     }
2096     if( p_owner->p_vout )
2097     {
2098         /* Hack to make sure all the the pictures are freed by the decoder
2099          * and that the vout is not paused anymore */
2100         vout_Reset( p_owner->p_vout );
2101
2102         /* */
2103         input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
2104                                     0, true );
2105         if( p_owner->p_input != NULL )
2106             input_SendEventVout( p_owner->p_input );
2107     }
2108
2109 #ifdef ENABLE_SOUT
2110     if( p_owner->p_sout_input )
2111     {
2112         sout_InputDelete( p_owner->p_sout_input );
2113         es_format_Clean( &p_owner->sout );
2114     }
2115 #endif
2116
2117     if( p_dec->fmt_out.i_cat == SPU_ES )
2118     {
2119         vout_thread_t *p_vout;
2120
2121         p_vout = input_resource_HoldVout( p_owner->p_resource );
2122         if( p_vout )
2123         {
2124             if( p_owner->p_spu_vout == p_vout )
2125                 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
2126             vlc_object_release( p_vout );
2127         }
2128     }
2129
2130     es_format_Clean( &p_dec->fmt_in );
2131     es_format_Clean( &p_dec->fmt_out );
2132     if( p_dec->p_description )
2133         vlc_meta_Delete( p_dec->p_description );
2134     es_format_Clean( &p_owner->fmt_description );
2135     if( p_owner->p_description )
2136         vlc_meta_Delete( p_owner->p_description );
2137
2138     if( p_owner->p_packetizer )
2139     {
2140         module_unneed( p_owner->p_packetizer,
2141                        p_owner->p_packetizer->p_module );
2142         es_format_Clean( &p_owner->p_packetizer->fmt_in );
2143         es_format_Clean( &p_owner->p_packetizer->fmt_out );
2144         if( p_owner->p_packetizer->p_description )
2145             vlc_meta_Delete( p_owner->p_packetizer->p_description );
2146         vlc_object_release( p_owner->p_packetizer );
2147     }
2148
2149     vlc_cond_destroy( &p_owner->wait_acknowledge );
2150     vlc_cond_destroy( &p_owner->wait_request );
2151     vlc_mutex_destroy( &p_owner->lock );
2152
2153     vlc_object_release( p_dec );
2154
2155     free( p_owner );
2156 }
2157
2158 /*****************************************************************************
2159  * Buffers allocation callbacks for the decoders
2160  *****************************************************************************/
2161 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2162 {
2163     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2164
2165     vlc_assert_locked( &p_owner->lock );
2166
2167     p_owner->b_fmt_description = true;
2168
2169     /* Copy es_format */
2170     es_format_Clean( &p_owner->fmt_description );
2171     es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2172
2173     /* Move p_description */
2174     if( p_owner->p_description && p_dec->p_description )
2175         vlc_meta_Delete( p_owner->p_description );
2176     p_owner->p_description = p_dec->p_description;
2177     p_dec->p_description = NULL;
2178 }
2179 static vout_thread_t *aout_request_vout( void *p_private,
2180                                          vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2181 {
2182     decoder_t *p_dec = p_private;
2183     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2184     input_thread_t *p_input = p_owner->p_input;
2185
2186     p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
2187                                          b_recyle );
2188     if( p_input != NULL )
2189         input_SendEventVout( p_input );
2190
2191     return p_vout;
2192 }
2193
2194 static int aout_update_format( decoder_t *p_dec )
2195 {
2196     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2197
2198     if( p_owner->p_aout
2199      && !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->audio) )
2200     {
2201         audio_output_t *p_aout = p_owner->p_aout;
2202
2203         /* Parameters changed, restart the aout */
2204         vlc_mutex_lock( &p_owner->lock );
2205         DecoderFlushBuffering( p_dec );
2206
2207         aout_DecDelete( p_owner->p_aout );
2208         p_owner->p_aout = NULL;
2209
2210         vlc_mutex_unlock( &p_owner->lock );
2211         input_resource_PutAout( p_owner->p_resource, p_aout );
2212     }
2213
2214     if( p_owner->p_aout == NULL )
2215     {
2216         const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
2217         audio_sample_format_t format;
2218         audio_output_t *p_aout;
2219         aout_request_vout_t request_vout;
2220
2221         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2222         p_owner->audio = p_dec->fmt_out.audio;
2223         aout_FormatPrepare( &p_owner->audio );
2224
2225         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2226         if( i_force_dolby &&
2227             (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2228                 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2229         {
2230             if( i_force_dolby == 1 )
2231             {
2232                 format.i_original_channels = format.i_original_channels |
2233                                              AOUT_CHAN_DOLBYSTEREO;
2234             }
2235             else /* i_force_dolby == 2 */
2236             {
2237                 format.i_original_channels = format.i_original_channels &
2238                                              ~AOUT_CHAN_DOLBYSTEREO;
2239             }
2240         }
2241
2242         request_vout.pf_request_vout = aout_request_vout;
2243         request_vout.p_private = p_dec;
2244
2245         assert( p_owner->p_aout == NULL );
2246         p_aout = input_resource_GetAout( p_owner->p_resource );
2247         if( p_aout )
2248         {
2249             if( aout_DecNew( p_aout, &format,
2250                              &p_dec->fmt_out.audio_replay_gain,
2251                              &request_vout ) )
2252             {
2253                 input_resource_PutAout( p_owner->p_resource, p_aout );
2254                 p_aout = NULL;
2255             }
2256         }
2257
2258         vlc_mutex_lock( &p_owner->lock );
2259
2260         p_owner->p_aout = p_aout;
2261         DecoderUpdateFormatLocked( p_dec );
2262         if( unlikely(p_owner->b_paused) ) /* fake pause if needed */
2263             aout_DecChangePause( p_aout, true, mdate() );
2264
2265         vlc_mutex_unlock( &p_owner->lock );
2266
2267         if( p_owner->p_input != NULL )
2268             input_SendEventAout( p_owner->p_input );
2269
2270         if( p_aout == NULL )
2271         {
2272             msg_Err( p_dec, "failed to create audio output" );
2273             p_dec->b_error = true;
2274             return -1;
2275         }
2276
2277         p_dec->fmt_out.audio.i_bytes_per_frame =
2278             p_owner->audio.i_bytes_per_frame;
2279         p_dec->fmt_out.audio.i_frame_length = p_owner->audio.i_frame_length;
2280     }
2281     return 0;
2282 }
2283
2284 static picture_t *vout_new_buffer( decoder_t *p_dec )
2285 {
2286     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2287
2288     if( p_owner->p_vout == NULL ||
2289         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2290         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2291         p_dec->fmt_out.video.i_visible_width != p_owner->video.i_visible_width ||
2292         p_dec->fmt_out.video.i_visible_height != p_owner->video.i_visible_height ||
2293         p_dec->fmt_out.video.i_x_offset != p_owner->video.i_x_offset  ||
2294         p_dec->fmt_out.video.i_y_offset != p_owner->video.i_y_offset  ||
2295         p_dec->fmt_out.i_codec != p_owner->video.i_chroma ||
2296         (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2297         (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num )
2298     {
2299         vout_thread_t *p_vout;
2300
2301         if( !p_dec->fmt_out.video.i_width ||
2302             !p_dec->fmt_out.video.i_height )
2303         {
2304             /* Can't create a new vout without display size */
2305             return NULL;
2306         }
2307
2308         video_format_t fmt = p_dec->fmt_out.video;
2309         fmt.i_chroma = p_dec->fmt_out.i_codec;
2310         p_owner->video = fmt;
2311
2312         if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
2313         {
2314             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
2315             for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
2316             {
2317                 while( fmt.i_width % dsc->p[i].w.den )
2318                     fmt.i_width++;
2319                 while( fmt.i_height % dsc->p[i].h.den )
2320                     fmt.i_height++;
2321             }
2322         }
2323
2324         if( !fmt.i_visible_width || !fmt.i_visible_height )
2325         {
2326             if( p_dec->fmt_in.video.i_visible_width &&
2327                 p_dec->fmt_in.video.i_visible_height )
2328             {
2329                 fmt.i_visible_width  = p_dec->fmt_in.video.i_visible_width;
2330                 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2331                 fmt.i_x_offset       = p_dec->fmt_in.video.i_x_offset;
2332                 fmt.i_y_offset       = p_dec->fmt_in.video.i_y_offset;
2333             }
2334             else
2335             {
2336                 fmt.i_visible_width  = fmt.i_width;
2337                 fmt.i_visible_height = fmt.i_height;
2338                 fmt.i_x_offset       = 0;
2339                 fmt.i_y_offset       = 0;
2340             }
2341         }
2342
2343         if( fmt.i_visible_height == 1088 &&
2344             var_CreateGetBool( p_dec, "hdtv-fix" ) )
2345         {
2346             fmt.i_visible_height = 1080;
2347             if( !(fmt.i_sar_num % 136))
2348             {
2349                 fmt.i_sar_num *= 135;
2350                 fmt.i_sar_den *= 136;
2351             }
2352             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2353         }
2354
2355         if( !fmt.i_sar_num || !fmt.i_sar_den )
2356         {
2357             fmt.i_sar_num = 1;
2358             fmt.i_sar_den = 1;
2359         }
2360
2361         vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2362                      fmt.i_sar_num, fmt.i_sar_den, 50000 );
2363
2364         vlc_mutex_lock( &p_owner->lock );
2365
2366         DecoderFlushBuffering( p_dec );
2367
2368         p_vout = p_owner->p_vout;
2369         p_owner->p_vout = NULL;
2370         vlc_mutex_unlock( &p_owner->lock );
2371
2372         unsigned dpb_size;
2373         switch( p_dec->fmt_in.i_codec )
2374         {
2375         case VLC_CODEC_H264:
2376         case VLC_CODEC_DIRAC: /* FIXME valid ? */
2377             dpb_size = 18;
2378             break;
2379         case VLC_CODEC_VP5:
2380         case VLC_CODEC_VP6:
2381         case VLC_CODEC_VP6F:
2382         case VLC_CODEC_VP8:
2383             dpb_size = 3;
2384             break;
2385         default:
2386             dpb_size = 2;
2387             break;
2388         }
2389         p_vout = input_resource_RequestVout( p_owner->p_resource,
2390                                              p_vout, &fmt,
2391                                              dpb_size +
2392                                              p_dec->i_extra_picture_buffers +
2393                                              1 + DECODER_MAX_BUFFERING_COUNT,
2394                                              true );
2395         vlc_mutex_lock( &p_owner->lock );
2396         p_owner->p_vout = p_vout;
2397
2398         DecoderUpdateFormatLocked( p_dec );
2399
2400         vlc_mutex_unlock( &p_owner->lock );
2401
2402         if( p_owner->p_input != NULL )
2403             input_SendEventVout( p_owner->p_input );
2404         if( p_vout == NULL )
2405         {
2406             msg_Err( p_dec, "failed to create video output" );
2407             p_dec->b_error = true;
2408             return NULL;
2409         }
2410     }
2411
2412     /* Get a new picture
2413      */
2414     for( ;; )
2415     {
2416         if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2417             return NULL;
2418
2419         picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2420         if( p_picture )
2421             return p_picture;
2422
2423         if( DecoderIsFlushing( p_dec ) )
2424             return NULL;
2425
2426         /* */
2427         DecoderSignalBuffering( p_dec, true );
2428
2429         /* Check the decoder doesn't leak pictures */
2430         vout_FixLeaks( p_owner->p_vout );
2431
2432         /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2433         msleep( VOUT_OUTMEM_SLEEP );
2434     }
2435 }
2436
2437 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2438 {
2439     vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2440 }
2441
2442 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2443 {
2444     vout_HoldPicture( p_dec->p_owner->p_vout, p_pic );
2445 }
2446
2447 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2448 {
2449     vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2450 }
2451
2452 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2453                                      const subpicture_updater_t *p_updater )
2454 {
2455     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2456     vout_thread_t *p_vout = NULL;
2457     subpicture_t *p_subpic;
2458     int i_attempts = 30;
2459
2460     while( i_attempts-- )
2461     {
2462         if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2463             break;
2464
2465         p_vout = input_resource_HoldVout( p_owner->p_resource );
2466         if( p_vout )
2467             break;
2468
2469         msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2470     }
2471
2472     if( !p_vout )
2473     {
2474         msg_Warn( p_dec, "no vout found, dropping subpicture" );
2475         return NULL;
2476     }
2477
2478     if( p_owner->p_spu_vout != p_vout )
2479     {
2480         vlc_mutex_lock( &p_owner->lock );
2481
2482         DecoderFlushBuffering( p_dec );
2483
2484         vlc_mutex_unlock( &p_owner->lock );
2485
2486         p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
2487         p_owner->i_spu_order = 0;
2488         p_owner->p_spu_vout = p_vout;
2489     }
2490
2491     p_subpic = subpicture_New( p_updater );
2492     if( p_subpic )
2493     {
2494         p_subpic->i_channel = p_owner->i_spu_channel;
2495         p_subpic->i_order = p_owner->i_spu_order++;
2496         p_subpic->b_subtitle = true;
2497     }
2498
2499     vlc_object_release( p_vout );
2500
2501     return p_subpic;
2502 }
2503
2504 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2505 {
2506     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2507     vout_thread_t *p_vout = NULL;
2508
2509     p_vout = input_resource_HoldVout( p_owner->p_resource );
2510     if( !p_vout || p_owner->p_spu_vout != p_vout )
2511     {
2512         if( p_vout )
2513             vlc_object_release( p_vout );
2514         msg_Warn( p_dec, "no vout found, leaking subpicture" );
2515         return;
2516     }
2517
2518     subpicture_Delete( p_subpic );
2519
2520     vlc_object_release( p_vout );
2521 }
2522