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