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