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