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