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