]> git.sesse.net Git - vlc/blob - src/input/decoder.c
Remove unused variables
[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         if( DecoderIsExitRequested( p_dec ) )
1272         {
1273             /* It prevent freezing VLC in case of broken decoder */
1274             block_Release( p_aout_buf );
1275             if( p_block )
1276                 block_Release( p_block );
1277             break;
1278         }
1279         i_decoded++;
1280
1281         if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1282             p_aout_buf->i_pts < p_owner->i_preroll_end )
1283         {
1284             block_Release( p_aout_buf );
1285             continue;
1286         }
1287
1288         if( p_owner->i_preroll_end > VLC_TS_INVALID )
1289         {
1290             msg_Dbg( p_dec, "End of audio preroll" );
1291             if( p_owner->p_aout )
1292                 aout_DecFlush( p_owner->p_aout );
1293             /* */
1294             p_owner->i_preroll_end = VLC_TS_INVALID;
1295         }
1296
1297         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1298     }
1299
1300     /* Update ugly stat */
1301     input_thread_t  *p_input = p_owner->p_input;
1302
1303     if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1304     {
1305         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1306         stats_Update( p_input->p->counters.p_lost_abuffers, i_lost, NULL );
1307         stats_Update( p_input->p->counters.p_played_abuffers, i_played, NULL );
1308         stats_Update( p_input->p->counters.p_decoded_audio, i_decoded, NULL );
1309         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1310     }
1311 }
1312 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1313 {
1314     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1315     block_t *p_cc;
1316     bool pb_present[4];
1317     bool b_processed = false;
1318     int i;
1319     int i_cc_decoder;
1320
1321     assert( p_dec_cc->pf_get_cc != NULL );
1322
1323     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1324     if( !p_owner->cc.b_supported )
1325         return;
1326
1327     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1328     if( !p_cc )
1329         return;
1330
1331     vlc_mutex_lock( &p_owner->lock );
1332     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1333     {
1334         p_owner->cc.pb_present[i] |= pb_present[i];
1335         if( p_owner->cc.pp_decoder[i] )
1336             i_cc_decoder++;
1337     }
1338
1339     for( i = 0; i < 4; i++ )
1340     {
1341         if( !p_owner->cc.pp_decoder[i] )
1342             continue;
1343
1344         if( i_cc_decoder > 1 )
1345             DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1346         else
1347             DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1348         i_cc_decoder--;
1349         b_processed = true;
1350     }
1351     vlc_mutex_unlock( &p_owner->lock );
1352
1353     if( !b_processed )
1354         block_Release( p_cc );
1355 }
1356
1357 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1358                               int *pi_played_sum, int *pi_lost_sum )
1359 {
1360     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1361     vout_thread_t  *p_vout = p_owner->p_vout;
1362     bool b_first_buffered;
1363
1364     if( p_picture->date <= VLC_TS_INVALID )
1365     {
1366         msg_Warn( p_dec, "non-dated video buffer received" );
1367         *pi_lost_sum += 1;
1368         vout_ReleasePicture( p_vout, p_picture );
1369         return;
1370     }
1371
1372     /* */
1373     vlc_mutex_lock( &p_owner->lock );
1374
1375     if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1376     {
1377         p_picture->p_next = NULL;
1378
1379         *p_owner->buffer.pp_picture_next = p_picture;
1380         p_owner->buffer.pp_picture_next = &p_picture->p_next;
1381
1382         p_owner->buffer.i_count++;
1383         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1384             p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1385         {
1386             p_owner->buffer.b_full = true;
1387             vlc_cond_signal( &p_owner->wait_acknowledge );
1388         }
1389     }
1390     b_first_buffered = p_owner->buffer.p_picture != NULL;
1391
1392     for( ;; b_first_buffered = false )
1393     {
1394         bool b_has_more = false;
1395
1396         bool b_reject;
1397
1398         DecoderWaitUnblock( p_dec, &b_reject );
1399
1400         if( p_owner->b_buffering && !p_owner->buffer.b_first )
1401         {
1402             vlc_mutex_unlock( &p_owner->lock );
1403             return;
1404         }
1405         bool b_buffering_first = p_owner->b_buffering;
1406
1407         /* */
1408         if( p_owner->buffer.p_picture )
1409         {
1410             p_picture = p_owner->buffer.p_picture;
1411
1412             p_owner->buffer.p_picture = p_picture->p_next;
1413             p_owner->buffer.i_count--;
1414
1415             b_has_more = p_owner->buffer.p_picture != NULL;
1416             if( !b_has_more )
1417                 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1418         }
1419
1420         /* */
1421         if( b_buffering_first )
1422         {
1423             assert( p_owner->buffer.b_first );
1424             assert( !p_owner->buffer.i_count );
1425             msg_Dbg( p_dec, "Received first picture" );
1426             p_owner->buffer.b_first = false;
1427             p_picture->b_force = true;
1428         }
1429
1430         const bool b_dated = p_picture->date > VLC_TS_INVALID;
1431         int i_rate = INPUT_RATE_DEFAULT;
1432         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1433                       &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1434
1435         vlc_mutex_unlock( &p_owner->lock );
1436
1437         /* */
1438         if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1439             b_reject = true;
1440
1441         if( !b_reject )
1442         {
1443             if( i_rate != p_owner->i_last_rate || b_first_buffered )
1444             {
1445                 /* Be sure to not display old picture after our own */
1446                 vout_Flush( p_vout, p_picture->date );
1447                 p_owner->i_last_rate = i_rate;
1448             }
1449             vout_PutPicture( p_vout, p_picture );
1450         }
1451         else
1452         {
1453             if( b_dated )
1454                 msg_Warn( p_dec, "early picture skipped" );
1455             else
1456                 msg_Warn( p_dec, "non-dated video buffer received" );
1457
1458             *pi_lost_sum += 1;
1459             vout_ReleasePicture( p_vout, p_picture );
1460         }
1461         int i_tmp_display;
1462         int i_tmp_lost;
1463         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1464
1465         *pi_played_sum += i_tmp_display;
1466         *pi_lost_sum += i_tmp_lost;
1467
1468         if( !b_has_more || b_buffering_first )
1469             break;
1470
1471         vlc_mutex_lock( &p_owner->lock );
1472         if( !p_owner->buffer.p_picture )
1473         {
1474             vlc_mutex_unlock( &p_owner->lock );
1475             break;
1476         }
1477     }
1478 }
1479
1480 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1481 {
1482     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1483     picture_t      *p_pic;
1484     int i_lost = 0;
1485     int i_decoded = 0;
1486     int i_displayed = 0;
1487
1488     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1489     {
1490         vout_thread_t  *p_vout = p_owner->p_vout;
1491         if( DecoderIsExitRequested( p_dec ) )
1492         {
1493             /* It prevent freezing VLC in case of broken decoder */
1494             vout_ReleasePicture( p_vout, p_pic );
1495             if( p_block )
1496                 block_Release( p_block );
1497             break;
1498         }
1499
1500         i_decoded++;
1501
1502         if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1503         {
1504             vout_ReleasePicture( p_vout, p_pic );
1505             continue;
1506         }
1507
1508         if( p_owner->i_preroll_end > VLC_TS_INVALID )
1509         {
1510             msg_Dbg( p_dec, "End of video preroll" );
1511             if( p_vout )
1512                 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1513             /* */
1514             p_owner->i_preroll_end = VLC_TS_INVALID;
1515         }
1516
1517         if( p_dec->pf_get_cc &&
1518             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1519             DecoderGetCc( p_dec, p_dec );
1520
1521         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1522     }
1523
1524     /* Update ugly stat */
1525     input_thread_t *p_input = p_owner->p_input;
1526
1527     if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
1528     {
1529         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1530         stats_Update( p_input->p->counters.p_decoded_video, i_decoded, NULL );
1531         stats_Update( p_input->p->counters.p_lost_pictures, i_lost , NULL);
1532         stats_Update( p_input->p->counters.p_displayed_pictures,
1533                       i_displayed, NULL);
1534         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1535     }
1536 }
1537
1538 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1539 {
1540     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1541     vout_thread_t *p_vout = p_owner->p_spu_vout;
1542
1543     /* */
1544     if( p_subpic->i_start <= VLC_TS_INVALID )
1545     {
1546         msg_Warn( p_dec, "non-dated spu buffer received" );
1547         subpicture_Delete( p_subpic );
1548         return;
1549     }
1550
1551     /* */
1552     vlc_mutex_lock( &p_owner->lock );
1553
1554     if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1555     {
1556         p_subpic->p_next = NULL;
1557
1558         *p_owner->buffer.pp_subpic_next = p_subpic;
1559         p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1560
1561         p_owner->buffer.i_count++;
1562         /* XXX it is important to be full after the first one */
1563         if( p_owner->buffer.i_count > 0 )
1564         {
1565             p_owner->buffer.b_full = true;
1566             vlc_cond_signal( &p_owner->wait_acknowledge );
1567         }
1568     }
1569
1570     for( ;; )
1571     {
1572         bool b_has_more = false;
1573         bool b_reject;
1574         DecoderWaitUnblock( p_dec, &b_reject );
1575
1576         if( p_owner->b_buffering )
1577         {
1578             vlc_mutex_unlock( &p_owner->lock );
1579             return;
1580         }
1581
1582         /* */
1583         if( p_owner->buffer.p_subpic )
1584         {
1585             p_subpic = p_owner->buffer.p_subpic;
1586
1587             p_owner->buffer.p_subpic = p_subpic->p_next;
1588             p_owner->buffer.i_count--;
1589
1590             b_has_more = p_owner->buffer.p_subpic != NULL;
1591             if( !b_has_more )
1592                 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1593         }
1594
1595         /* */
1596         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1597                       NULL, INT64_MAX );
1598
1599         if( p_subpic->i_start <= VLC_TS_INVALID )
1600             b_reject = true;
1601
1602         DecoderWaitDate( p_dec, &b_reject,
1603                          p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1604         vlc_mutex_unlock( &p_owner->lock );
1605
1606         if( !b_reject )
1607             vout_PutSubpicture( p_vout, p_subpic );
1608         else
1609             subpicture_Delete( p_subpic );
1610
1611         if( !b_has_more )
1612             break;
1613         vlc_mutex_lock( &p_owner->lock );
1614         if( !p_owner->buffer.p_subpic )
1615         {
1616             vlc_mutex_unlock( &p_owner->lock );
1617             break;
1618         }
1619     }
1620 }
1621
1622 #ifdef ENABLE_SOUT
1623 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
1624 {
1625     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1626
1627     assert( p_owner->p_clock );
1628     assert( !p_sout_block->p_next );
1629
1630     vlc_mutex_lock( &p_owner->lock );
1631
1632     if( p_owner->b_buffering || p_owner->buffer.p_block )
1633     {
1634         block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1635
1636         p_owner->buffer.i_count++;
1637         /* XXX it is important to be full after the first one */
1638         if( p_owner->buffer.i_count > 0 )
1639         {
1640             p_owner->buffer.b_full = true;
1641             vlc_cond_signal( &p_owner->wait_acknowledge );
1642         }
1643     }
1644
1645     for( ;; )
1646     {
1647         bool b_has_more = false;
1648         bool b_reject;
1649         DecoderWaitUnblock( p_dec, &b_reject );
1650
1651         if( p_owner->b_buffering )
1652         {
1653             vlc_mutex_unlock( &p_owner->lock );
1654             return;
1655         }
1656
1657         /* */
1658         if( p_owner->buffer.p_block )
1659         {
1660             p_sout_block = p_owner->buffer.p_block;
1661
1662             p_owner->buffer.p_block = p_sout_block->p_next;
1663             p_owner->buffer.i_count--;
1664
1665             b_has_more = p_owner->buffer.p_block != NULL;
1666             if( !b_has_more )
1667                 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1668         }
1669         p_sout_block->p_next = NULL;
1670
1671         DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1672                       &p_sout_block->i_length, NULL, INT64_MAX );
1673
1674         vlc_mutex_unlock( &p_owner->lock );
1675
1676         if( !b_reject )
1677             sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1678         else
1679             block_Release( p_sout_block );
1680
1681         if( !b_has_more )
1682             break;
1683         vlc_mutex_lock( &p_owner->lock );
1684         if( !p_owner->buffer.p_block )
1685         {
1686             vlc_mutex_unlock( &p_owner->lock );
1687             break;
1688         }
1689     }
1690 }
1691 #endif
1692
1693 /* */
1694 static void DecoderFlushBuffering( decoder_t *p_dec )
1695 {
1696     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1697
1698     vlc_assert_locked( &p_owner->lock );
1699
1700     while( p_owner->buffer.p_picture )
1701     {
1702         picture_t *p_picture = p_owner->buffer.p_picture;
1703
1704         p_owner->buffer.p_picture = p_picture->p_next;
1705         p_owner->buffer.i_count--;
1706
1707         if( p_owner->p_vout )
1708         {
1709             vout_ReleasePicture( p_owner->p_vout, p_picture );
1710         }
1711
1712         if( !p_owner->buffer.p_picture )
1713             p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1714     }
1715     while( p_owner->buffer.p_audio )
1716     {
1717         block_t *p_audio = p_owner->buffer.p_audio;
1718
1719         p_owner->buffer.p_audio = p_audio->p_next;
1720         p_owner->buffer.i_count--;
1721
1722         block_Release( p_audio );
1723
1724         if( !p_owner->buffer.p_audio )
1725             p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1726     }
1727     while( p_owner->buffer.p_subpic )
1728     {
1729         subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1730
1731         p_owner->buffer.p_subpic = p_subpic->p_next;
1732         p_owner->buffer.i_count--;
1733
1734         subpicture_Delete( p_subpic );
1735
1736         if( !p_owner->buffer.p_subpic )
1737             p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1738     }
1739     if( p_owner->buffer.p_block )
1740     {
1741         block_ChainRelease( p_owner->buffer.p_block );
1742
1743         p_owner->buffer.i_count = 0;
1744         p_owner->buffer.p_block = NULL;
1745         p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1746     }
1747 }
1748
1749 #ifdef ENABLE_SOUT
1750 /* This function process a block for sout
1751  */
1752 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1753 {
1754     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1755     block_t *p_sout_block;
1756
1757     while( ( p_sout_block =
1758                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1759     {
1760         if( !p_owner->p_sout_input )
1761         {
1762             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1763
1764             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1765             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1766             if( p_dec->fmt_in.psz_language )
1767             {
1768                 free( p_owner->sout.psz_language );
1769                 p_owner->sout.psz_language =
1770                     strdup( p_dec->fmt_in.psz_language );
1771             }
1772
1773             p_owner->p_sout_input =
1774                 sout_InputNew( p_owner->p_sout,
1775                                &p_owner->sout );
1776
1777             if( p_owner->p_sout_input == NULL )
1778             {
1779                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1780                          (char *)&p_owner->sout.i_codec );
1781                 p_dec->b_error = true;
1782
1783                 while( p_sout_block )
1784                 {
1785                     block_t *p_next = p_sout_block->p_next;
1786                     block_Release( p_sout_block );
1787                     p_sout_block = p_next;
1788                 }
1789                 break;
1790             }
1791         }
1792
1793         while( p_sout_block )
1794         {
1795             block_t *p_next = p_sout_block->p_next;
1796
1797             p_sout_block->p_next = NULL;
1798
1799             DecoderPlaySout( p_dec, p_sout_block );
1800
1801             p_sout_block = p_next;
1802         }
1803     }
1804 }
1805 #endif
1806
1807 /* This function process a video block
1808  */
1809 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1810 {
1811     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1812
1813     if( p_owner->p_packetizer )
1814     {
1815         block_t *p_packetized_block;
1816         decoder_t *p_packetizer = p_owner->p_packetizer;
1817
1818         while( (p_packetized_block =
1819                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1820         {
1821             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1822             {
1823                 es_format_Clean( &p_dec->fmt_in );
1824                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1825             }
1826             if( p_packetizer->pf_get_cc )
1827                 DecoderGetCc( p_dec, p_packetizer );
1828
1829             while( p_packetized_block )
1830             {
1831                 block_t *p_next = p_packetized_block->p_next;
1832                 p_packetized_block->p_next = NULL;
1833
1834                 DecoderDecodeVideo( p_dec, p_packetized_block );
1835
1836                 p_packetized_block = p_next;
1837             }
1838         }
1839         /* The packetizer does not output a block that tell the decoder to flush
1840          * do it ourself */
1841         if( b_flush )
1842         {
1843             block_t *p_null = DecoderBlockFlushNew();
1844             if( p_null )
1845                 DecoderDecodeVideo( p_dec, p_null );
1846         }
1847     }
1848     else if( p_block )
1849     {
1850         DecoderDecodeVideo( p_dec, p_block );
1851     }
1852
1853     if( b_flush && p_owner->p_vout )
1854         vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1855 }
1856
1857 /* This function process a audio block
1858  */
1859 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1860 {
1861     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1862
1863     if( p_owner->p_packetizer )
1864     {
1865         block_t *p_packetized_block;
1866         decoder_t *p_packetizer = p_owner->p_packetizer;
1867
1868         while( (p_packetized_block =
1869                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1870         {
1871             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1872             {
1873                 es_format_Clean( &p_dec->fmt_in );
1874                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1875             }
1876
1877             while( p_packetized_block )
1878             {
1879                 block_t *p_next = p_packetized_block->p_next;
1880                 p_packetized_block->p_next = NULL;
1881
1882                 DecoderDecodeAudio( p_dec, p_packetized_block );
1883
1884                 p_packetized_block = p_next;
1885             }
1886         }
1887         /* The packetizer does not output a block that tell the decoder to flush
1888          * do it ourself */
1889         if( b_flush )
1890         {
1891             block_t *p_null = DecoderBlockFlushNew();
1892             if( p_null )
1893                 DecoderDecodeAudio( p_dec, p_null );
1894         }
1895     }
1896     else if( p_block )
1897     {
1898         DecoderDecodeAudio( p_dec, p_block );
1899     }
1900
1901     if( b_flush && p_owner->p_aout )
1902         aout_DecFlush( p_owner->p_aout );
1903 }
1904
1905 /* This function process a subtitle block
1906  */
1907 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1908 {
1909     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1910
1911     input_thread_t *p_input = p_owner->p_input;
1912     vout_thread_t *p_vout;
1913     subpicture_t *p_spu;
1914
1915     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1916     {
1917         if( p_input != NULL )
1918         {
1919             vlc_mutex_lock( &p_input->p->counters.counters_lock );
1920             stats_Update( p_input->p->counters.p_decoded_sub, 1, NULL );
1921             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1922         }
1923
1924         p_vout = input_resource_HoldVout( p_owner->p_resource );
1925         if( p_vout && p_owner->p_spu_vout == p_vout )
1926         {
1927             /* Preroll does not work very well with subtitle */
1928             if( p_spu->i_start > VLC_TS_INVALID &&
1929                 p_spu->i_start < p_owner->i_preroll_end &&
1930                 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1931             {
1932                 subpicture_Delete( p_spu );
1933             }
1934             else
1935             {
1936                 DecoderPlaySpu( p_dec, p_spu );
1937             }
1938         }
1939         else
1940         {
1941             subpicture_Delete( p_spu );
1942         }
1943         if( p_vout )
1944             vlc_object_release( p_vout );
1945     }
1946
1947     if( b_flush && p_owner->p_spu_vout )
1948     {
1949         p_vout = input_resource_HoldVout( p_owner->p_resource );
1950
1951         if( p_vout && p_owner->p_spu_vout == p_vout )
1952             vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1953
1954         if( p_vout )
1955             vlc_object_release( p_vout );
1956     }
1957 }
1958
1959 /* */
1960 static void DecoderProcessOnFlush( decoder_t *p_dec )
1961 {
1962     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1963
1964     vlc_mutex_lock( &p_owner->lock );
1965     DecoderFlushBuffering( p_dec );
1966
1967     if( p_owner->b_flushing )
1968     {
1969         p_owner->b_flushing = false;
1970         vlc_cond_signal( &p_owner->wait_acknowledge );
1971     }
1972     vlc_mutex_unlock( &p_owner->lock );
1973 }
1974
1975 /**
1976  * Decode a block
1977  *
1978  * \param p_dec the decoder object
1979  * \param p_block the block to decode
1980  * \return VLC_SUCCESS or an error code
1981  */
1982 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1983 {
1984     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1985     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1986
1987     if( p_block && p_block->i_buffer <= 0 )
1988     {
1989         assert( !b_flush_request );
1990         block_Release( p_block );
1991         return;
1992     }
1993
1994 #ifdef ENABLE_SOUT
1995     if( p_owner->b_packetizer )
1996     {
1997         if( p_block )
1998             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1999
2000         DecoderProcessSout( p_dec, p_block );
2001     }
2002     else
2003 #endif
2004     {
2005         bool b_flush = false;
2006
2007         if( p_block )
2008         {
2009             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
2010             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
2011
2012             b_flush = !b_flushing && b_flush_request;
2013
2014             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2015         }
2016
2017         if( p_dec->fmt_out.i_cat == AUDIO_ES )
2018         {
2019             DecoderProcessAudio( p_dec, p_block, b_flush );
2020         }
2021         else if( p_dec->fmt_out.i_cat == VIDEO_ES )
2022         {
2023             DecoderProcessVideo( p_dec, p_block, b_flush );
2024         }
2025         else if( p_dec->fmt_out.i_cat == SPU_ES )
2026         {
2027             DecoderProcessSpu( p_dec, p_block, b_flush );
2028         }
2029         else
2030         {
2031             msg_Err( p_dec, "unknown ES format" );
2032             p_dec->b_error = true;
2033         }
2034     }
2035
2036     /* */
2037     if( b_flush_request )
2038         DecoderProcessOnFlush( p_dec );
2039 }
2040
2041 static void DecoderError( decoder_t *p_dec, block_t *p_block )
2042 {
2043     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2044
2045     /* */
2046     if( p_block )
2047         block_Release( p_block );
2048
2049     if( b_flush_request )
2050         DecoderProcessOnFlush( p_dec );
2051 }
2052
2053
2054 /**
2055  * Destroys a decoder object
2056  *
2057  * \param p_dec the decoder object
2058  * \return nothing
2059  */
2060 static void DeleteDecoder( decoder_t * p_dec )
2061 {
2062     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2063
2064     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2065              (char*)&p_dec->fmt_in.i_codec,
2066              (unsigned)block_FifoCount( p_owner->p_fifo ) );
2067
2068     /* Free all packets still in the decoder fifo. */
2069     block_FifoEmpty( p_owner->p_fifo );
2070     block_FifoRelease( p_owner->p_fifo );
2071
2072     /* */
2073     vlc_mutex_lock( &p_owner->lock );
2074     DecoderFlushBuffering( p_dec );
2075     vlc_mutex_unlock( &p_owner->lock );
2076
2077     /* Cleanup */
2078     if( p_owner->p_aout )
2079     {
2080         /* TODO: REVISIT gap-less audio */
2081         aout_DecFlush( p_owner->p_aout );
2082         aout_DecDelete( p_owner->p_aout );
2083         input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
2084         if( p_owner->p_input != NULL )
2085             input_SendEventAout( p_owner->p_input );
2086     }
2087     if( p_owner->p_vout )
2088     {
2089         /* Hack to make sure all the the pictures are freed by the decoder
2090          * and that the vout is not paused anymore */
2091         vout_Reset( p_owner->p_vout );
2092
2093         /* */
2094         input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
2095                                     0, true );
2096         if( p_owner->p_input != NULL )
2097             input_SendEventVout( p_owner->p_input );
2098     }
2099
2100 #ifdef ENABLE_SOUT
2101     if( p_owner->p_sout_input )
2102     {
2103         sout_InputDelete( p_owner->p_sout_input );
2104         es_format_Clean( &p_owner->sout );
2105     }
2106 #endif
2107
2108     if( p_dec->fmt_out.i_cat == SPU_ES )
2109     {
2110         vout_thread_t *p_vout;
2111
2112         p_vout = input_resource_HoldVout( p_owner->p_resource );
2113         if( p_vout )
2114         {
2115             if( p_owner->p_spu_vout == p_vout )
2116                 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
2117             vlc_object_release( p_vout );
2118         }
2119     }
2120
2121     es_format_Clean( &p_dec->fmt_in );
2122     es_format_Clean( &p_dec->fmt_out );
2123     if( p_dec->p_description )
2124         vlc_meta_Delete( p_dec->p_description );
2125     es_format_Clean( &p_owner->fmt_description );
2126     if( p_owner->p_description )
2127         vlc_meta_Delete( p_owner->p_description );
2128
2129     if( p_owner->p_packetizer )
2130     {
2131         module_unneed( p_owner->p_packetizer,
2132                        p_owner->p_packetizer->p_module );
2133         es_format_Clean( &p_owner->p_packetizer->fmt_in );
2134         es_format_Clean( &p_owner->p_packetizer->fmt_out );
2135         if( p_owner->p_packetizer->p_description )
2136             vlc_meta_Delete( p_owner->p_packetizer->p_description );
2137         vlc_object_release( p_owner->p_packetizer );
2138     }
2139
2140     vlc_cond_destroy( &p_owner->wait_acknowledge );
2141     vlc_cond_destroy( &p_owner->wait_request );
2142     vlc_mutex_destroy( &p_owner->lock );
2143
2144     vlc_object_release( p_dec );
2145
2146     free( p_owner );
2147 }
2148
2149 /*****************************************************************************
2150  * Buffers allocation callbacks for the decoders
2151  *****************************************************************************/
2152 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2153 {
2154     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2155
2156     vlc_assert_locked( &p_owner->lock );
2157
2158     p_owner->b_fmt_description = true;
2159
2160     /* Copy es_format */
2161     es_format_Clean( &p_owner->fmt_description );
2162     es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2163
2164     /* Move p_description */
2165     if( p_owner->p_description && p_dec->p_description )
2166         vlc_meta_Delete( p_owner->p_description );
2167     p_owner->p_description = p_dec->p_description;
2168     p_dec->p_description = NULL;
2169 }
2170 static vout_thread_t *aout_request_vout( void *p_private,
2171                                          vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2172 {
2173     decoder_t *p_dec = p_private;
2174     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2175     input_thread_t *p_input = p_owner->p_input;
2176
2177     p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
2178                                          b_recyle );
2179     if( p_input != NULL )
2180         input_SendEventVout( p_input );
2181
2182     return p_vout;
2183 }
2184
2185 static int aout_update_format( decoder_t *p_dec )
2186 {
2187     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2188
2189     if( p_owner->p_aout
2190      && !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->audio) )
2191     {
2192         audio_output_t *p_aout = p_owner->p_aout;
2193
2194         /* Parameters changed, restart the aout */
2195         vlc_mutex_lock( &p_owner->lock );
2196         DecoderFlushBuffering( p_dec );
2197
2198         aout_DecDelete( p_owner->p_aout );
2199         p_owner->p_aout = NULL;
2200
2201         vlc_mutex_unlock( &p_owner->lock );
2202         input_resource_PutAout( p_owner->p_resource, p_aout );
2203     }
2204
2205     if( p_owner->p_aout == NULL )
2206     {
2207         const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
2208         audio_sample_format_t format;
2209         audio_output_t *p_aout;
2210         aout_request_vout_t request_vout;
2211
2212         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2213         p_owner->audio = p_dec->fmt_out.audio;
2214         aout_FormatPrepare( &p_owner->audio );
2215
2216         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2217         if( i_force_dolby &&
2218             (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2219                 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2220         {
2221             if( i_force_dolby == 1 )
2222             {
2223                 format.i_original_channels = format.i_original_channels |
2224                                              AOUT_CHAN_DOLBYSTEREO;
2225             }
2226             else /* i_force_dolby == 2 */
2227             {
2228                 format.i_original_channels = format.i_original_channels &
2229                                              ~AOUT_CHAN_DOLBYSTEREO;
2230             }
2231         }
2232
2233         request_vout.pf_request_vout = aout_request_vout;
2234         request_vout.p_private = p_dec;
2235
2236         assert( p_owner->p_aout == NULL );
2237         p_aout = input_resource_GetAout( p_owner->p_resource );
2238         if( p_aout )
2239         {
2240             if( aout_DecNew( p_aout, &format,
2241                              &p_dec->fmt_out.audio_replay_gain,
2242                              &request_vout ) )
2243             {
2244                 input_resource_PutAout( p_owner->p_resource, p_aout );
2245                 p_aout = NULL;
2246             }
2247         }
2248
2249         vlc_mutex_lock( &p_owner->lock );
2250
2251         p_owner->p_aout = p_aout;
2252         DecoderUpdateFormatLocked( p_dec );
2253         if( unlikely(p_owner->b_paused) ) /* fake pause if needed */
2254             aout_DecChangePause( p_aout, true, mdate() );
2255
2256         vlc_mutex_unlock( &p_owner->lock );
2257
2258         if( p_owner->p_input != NULL )
2259             input_SendEventAout( p_owner->p_input );
2260
2261         if( p_aout == NULL )
2262         {
2263             msg_Err( p_dec, "failed to create audio output" );
2264             p_dec->b_error = true;
2265             return -1;
2266         }
2267
2268         p_dec->fmt_out.audio.i_bytes_per_frame =
2269             p_owner->audio.i_bytes_per_frame;
2270         p_dec->fmt_out.audio.i_frame_length = p_owner->audio.i_frame_length;
2271     }
2272     return 0;
2273 }
2274
2275 static picture_t *vout_new_buffer( decoder_t *p_dec )
2276 {
2277     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2278
2279     if( p_owner->p_vout == NULL ||
2280         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2281         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2282         p_dec->fmt_out.video.i_visible_width != p_owner->video.i_visible_width ||
2283         p_dec->fmt_out.video.i_visible_height != p_owner->video.i_visible_height ||
2284         p_dec->fmt_out.video.i_x_offset != p_owner->video.i_x_offset  ||
2285         p_dec->fmt_out.video.i_y_offset != p_owner->video.i_y_offset  ||
2286         p_dec->fmt_out.i_codec != p_owner->video.i_chroma ||
2287         (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2288         (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num )
2289     {
2290         vout_thread_t *p_vout;
2291
2292         if( !p_dec->fmt_out.video.i_width ||
2293             !p_dec->fmt_out.video.i_height )
2294         {
2295             /* Can't create a new vout without display size */
2296             return NULL;
2297         }
2298
2299         video_format_t fmt = p_dec->fmt_out.video;
2300         fmt.i_chroma = p_dec->fmt_out.i_codec;
2301         p_owner->video = fmt;
2302
2303         if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
2304         {
2305             const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
2306             for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
2307             {
2308                 while( fmt.i_width % dsc->p[i].w.den )
2309                     fmt.i_width++;
2310                 while( fmt.i_height % dsc->p[i].h.den )
2311                     fmt.i_height++;
2312             }
2313         }
2314
2315         if( !fmt.i_visible_width || !fmt.i_visible_height )
2316         {
2317             if( p_dec->fmt_in.video.i_visible_width &&
2318                 p_dec->fmt_in.video.i_visible_height )
2319             {
2320                 fmt.i_visible_width  = p_dec->fmt_in.video.i_visible_width;
2321                 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2322                 fmt.i_x_offset       = p_dec->fmt_in.video.i_x_offset;
2323                 fmt.i_y_offset       = p_dec->fmt_in.video.i_y_offset;
2324             }
2325             else
2326             {
2327                 fmt.i_visible_width  = fmt.i_width;
2328                 fmt.i_visible_height = fmt.i_height;
2329                 fmt.i_x_offset       = 0;
2330                 fmt.i_y_offset       = 0;
2331             }
2332         }
2333
2334         if( fmt.i_visible_height == 1088 &&
2335             var_CreateGetBool( p_dec, "hdtv-fix" ) )
2336         {
2337             fmt.i_visible_height = 1080;
2338             if( !(fmt.i_sar_num % 136))
2339             {
2340                 fmt.i_sar_num *= 135;
2341                 fmt.i_sar_den *= 136;
2342             }
2343             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2344         }
2345
2346         if( !fmt.i_sar_num || !fmt.i_sar_den )
2347         {
2348             fmt.i_sar_num = 1;
2349             fmt.i_sar_den = 1;
2350         }
2351
2352         vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2353                      fmt.i_sar_num, fmt.i_sar_den, 50000 );
2354
2355         vlc_mutex_lock( &p_owner->lock );
2356
2357         DecoderFlushBuffering( p_dec );
2358
2359         p_vout = p_owner->p_vout;
2360         p_owner->p_vout = NULL;
2361         vlc_mutex_unlock( &p_owner->lock );
2362
2363         unsigned dpb_size;
2364         switch( p_dec->fmt_in.i_codec )
2365         {
2366         case VLC_CODEC_H264:
2367         case VLC_CODEC_DIRAC: /* FIXME valid ? */
2368             dpb_size = 18;
2369             break;
2370         case VLC_CODEC_VP5:
2371         case VLC_CODEC_VP6:
2372         case VLC_CODEC_VP6F:
2373         case VLC_CODEC_VP8:
2374             dpb_size = 3;
2375             break;
2376         default:
2377             dpb_size = 2;
2378             break;
2379         }
2380         p_vout = input_resource_RequestVout( p_owner->p_resource,
2381                                              p_vout, &fmt,
2382                                              dpb_size +
2383                                              p_dec->i_extra_picture_buffers +
2384                                              1 + DECODER_MAX_BUFFERING_COUNT,
2385                                              true );
2386         vlc_mutex_lock( &p_owner->lock );
2387         p_owner->p_vout = p_vout;
2388
2389         DecoderUpdateFormatLocked( p_dec );
2390
2391         vlc_mutex_unlock( &p_owner->lock );
2392
2393         if( p_owner->p_input != NULL )
2394             input_SendEventVout( p_owner->p_input );
2395         if( p_vout == NULL )
2396         {
2397             msg_Err( p_dec, "failed to create video output" );
2398             p_dec->b_error = true;
2399             return NULL;
2400         }
2401     }
2402
2403     /* Get a new picture
2404      */
2405     for( ;; )
2406     {
2407         if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2408             return NULL;
2409
2410         picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2411         if( p_picture )
2412             return p_picture;
2413
2414         if( DecoderIsFlushing( p_dec ) )
2415             return NULL;
2416
2417         /* */
2418         DecoderSignalBuffering( p_dec, true );
2419
2420         /* Check the decoder doesn't leak pictures */
2421         vout_FixLeaks( p_owner->p_vout );
2422
2423         /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2424         msleep( VOUT_OUTMEM_SLEEP );
2425     }
2426 }
2427
2428 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2429 {
2430     vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2431 }
2432
2433 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2434 {
2435     vout_HoldPicture( p_dec->p_owner->p_vout, p_pic );
2436 }
2437
2438 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2439 {
2440     vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2441 }
2442
2443 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2444                                      const subpicture_updater_t *p_updater )
2445 {
2446     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2447     vout_thread_t *p_vout = NULL;
2448     subpicture_t *p_subpic;
2449     int i_attempts = 30;
2450
2451     while( i_attempts-- )
2452     {
2453         if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2454             break;
2455
2456         p_vout = input_resource_HoldVout( p_owner->p_resource );
2457         if( p_vout )
2458             break;
2459
2460         msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2461     }
2462
2463     if( !p_vout )
2464     {
2465         msg_Warn( p_dec, "no vout found, dropping subpicture" );
2466         return NULL;
2467     }
2468
2469     if( p_owner->p_spu_vout != p_vout )
2470     {
2471         vlc_mutex_lock( &p_owner->lock );
2472
2473         DecoderFlushBuffering( p_dec );
2474
2475         vlc_mutex_unlock( &p_owner->lock );
2476
2477         p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
2478         p_owner->i_spu_order = 0;
2479         p_owner->p_spu_vout = p_vout;
2480     }
2481
2482     p_subpic = subpicture_New( p_updater );
2483     if( p_subpic )
2484     {
2485         p_subpic->i_channel = p_owner->i_spu_channel;
2486         p_subpic->i_order = p_owner->i_spu_order++;
2487         p_subpic->b_subtitle = true;
2488     }
2489
2490     vlc_object_release( p_vout );
2491
2492     return p_subpic;
2493 }
2494
2495 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2496 {
2497     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2498     vout_thread_t *p_vout = NULL;
2499
2500     p_vout = input_resource_HoldVout( p_owner->p_resource );
2501     if( !p_vout || p_owner->p_spu_vout != p_vout )
2502     {
2503         if( p_vout )
2504             vlc_object_release( p_vout );
2505         msg_Warn( p_dec, "no vout found, leaking subpicture" );
2506         return;
2507     }
2508
2509     subpicture_Delete( p_subpic );
2510
2511     vlc_object_release( p_vout );
2512 }
2513