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