Fixed video format decoder changes detection.
[vlc/solaris.git] / src / input / decoder.c
blob031cfb50b4a14f3d38d8e1256708f4589cc77085
1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
5 * $Id$
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Laurent Aimar <fenrir@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
34 #include <vlc_common.h>
36 #include <vlc_block.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41 #include <vlc_osd.h>
42 #include <vlc_meta.h>
43 #include <vlc_dialog.h>
45 #include "audio_output/aout_internal.h"
46 #include "stream_output/stream_output.h"
47 #include "input_internal.h"
48 #include "clock.h"
49 #include "decoder.h"
50 #include "event.h"
51 #include "resource.h"
53 #include "../video_output/vout_control.h"
55 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, bool,
56 sout_instance_t *p_sout );
57 static void DeleteDecoder( decoder_t * );
59 static void *DecoderThread( vlc_object_t * );
60 static void DecoderProcess( decoder_t *, block_t * );
61 static void DecoderError( decoder_t *p_dec, block_t *p_block );
62 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
63 static void DecoderFlush( decoder_t * );
64 static void DecoderSignalBuffering( decoder_t *, bool );
65 static void DecoderFlushBuffering( decoder_t * );
67 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
69 /* Buffers allocation callbacks for the decoders */
70 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
71 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
73 static picture_t *vout_new_buffer( decoder_t * );
74 static void vout_del_buffer( decoder_t *, picture_t * );
75 static void vout_link_picture( decoder_t *, picture_t * );
76 static void vout_unlink_picture( decoder_t *, picture_t * );
78 static subpicture_t *spu_new_buffer( decoder_t * );
79 static void spu_del_buffer( decoder_t *, subpicture_t * );
81 struct decoder_owner_sys_t
83 int64_t i_preroll_end;
85 input_thread_t *p_input;
86 input_clock_t *p_clock;
87 int i_last_rate;
89 vout_thread_t *p_spu_vout;
90 int i_spu_channel;
91 int64_t i_spu_order;
93 sout_instance_t *p_sout;
94 sout_packetizer_input_t *p_sout_input;
96 /* Some decoders require already packetized data (ie. not truncated) */
97 decoder_t *p_packetizer;
98 bool b_packetizer;
100 /* Current format in use by the output */
101 video_format_t video;
102 audio_format_t audio;
103 es_format_t sout;
105 /* */
106 bool b_fmt_description;
107 es_format_t fmt_description;
108 vlc_meta_t *p_description;
110 /* fifo */
111 block_fifo_t *p_fifo;
113 /* Lock for communication with decoder thread */
114 vlc_mutex_t lock;
115 vlc_cond_t wait_request;
116 vlc_cond_t wait_acknowledge;
118 /* -- These variables need locking on write(only) -- */
119 aout_instance_t *p_aout;
120 aout_input_t *p_aout_input;
122 vout_thread_t *p_vout;
124 /* -- Theses variables need locking on read *and* write -- */
125 /* */
126 /* Pause */
127 bool b_paused;
128 struct
130 mtime_t i_date;
131 int i_ignore;
132 } pause;
134 /* Buffering */
135 bool b_buffering;
136 struct
138 bool b_first;
139 bool b_full;
140 int i_count;
142 picture_t *p_picture;
143 picture_t **pp_picture_next;
145 subpicture_t *p_subpic;
146 subpicture_t **pp_subpic_next;
148 aout_buffer_t *p_audio;
149 aout_buffer_t **pp_audio_next;
151 block_t *p_block;
152 block_t **pp_block_next;
153 } buffer;
155 /* Flushing */
156 bool b_flushing;
158 /* CC */
159 struct
161 bool b_supported;
162 bool pb_present[4];
163 decoder_t *pp_decoder[4];
164 } cc;
166 /* Delay */
167 mtime_t i_ts_delay;
170 #define DECODER_MAX_BUFFERING_COUNT (4)
171 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
172 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
174 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
175 * a bogus PTS and won't be displayed */
176 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
178 /* */
179 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
182 /*****************************************************************************
183 * Public functions
184 *****************************************************************************/
185 picture_t *decoder_NewPicture( decoder_t *p_decoder )
187 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
188 if( !p_picture )
189 msg_Warn( p_decoder, "can't get output picture" );
190 return p_picture;
192 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
194 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
196 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
198 p_decoder->pf_picture_link( p_decoder, p_picture );
200 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
202 p_decoder->pf_picture_unlink( p_decoder, p_picture );
205 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
207 if( !p_decoder->pf_aout_buffer_new )
208 return NULL;
209 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
211 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
213 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
216 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
218 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
219 if( !p_subpicture )
220 msg_Warn( p_decoder, "can't get output subpicture" );
221 return p_subpicture;
223 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
225 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
228 /* decoder_GetInputAttachments:
230 int decoder_GetInputAttachments( decoder_t *p_dec,
231 input_attachment_t ***ppp_attachment,
232 int *pi_attachment )
234 if( !p_dec->pf_get_attachments )
235 return VLC_EGENERIC;
237 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
239 /* decoder_GetDisplayDate:
241 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
243 if( !p_dec->pf_get_display_date )
244 return VLC_TS_INVALID;
246 return p_dec->pf_get_display_date( p_dec, i_ts );
248 /* decoder_GetDisplayRate:
250 int decoder_GetDisplayRate( decoder_t *p_dec )
252 if( !p_dec->pf_get_display_rate )
253 return INPUT_RATE_DEFAULT;
255 return p_dec->pf_get_display_rate( p_dec );
259 * Spawns a new decoder thread
261 * \param p_input the input thread
262 * \param p_es the es descriptor
263 * \return the spawned decoder object
265 decoder_t *input_DecoderNew( input_thread_t *p_input,
266 es_format_t *fmt, input_clock_t *p_clock,
267 sout_instance_t *p_sout )
269 decoder_t *p_dec = NULL;
270 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
271 int i_priority;
273 /* Create the decoder configuration structure */
274 p_dec = CreateDecoder( p_input, fmt, p_sout != NULL, p_sout );
275 if( p_dec == NULL )
277 msg_Err( p_input, "could not create %s", psz_type );
278 dialog_Fatal( p_input, _("Streaming / Transcoding failed"),
279 _("VLC could not open the %s module."),
280 vlc_gettext( psz_type ) );
281 return NULL;
284 if( !p_dec->p_module )
286 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
288 DeleteDecoder( p_dec );
289 return NULL;
292 p_dec->p_owner->p_clock = p_clock;
293 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
295 if( p_dec->fmt_out.i_cat == AUDIO_ES )
296 i_priority = VLC_THREAD_PRIORITY_AUDIO;
297 else
298 i_priority = VLC_THREAD_PRIORITY_VIDEO;
300 /* Spawn the decoder thread */
301 if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
303 msg_Err( p_dec, "cannot spawn decoder thread" );
304 module_unneed( p_dec, p_dec->p_module );
305 DeleteDecoder( p_dec );
306 return NULL;
309 return p_dec;
313 * Kills a decoder thread and waits until it's finished
315 * \param p_input the input thread
316 * \param p_es the es descriptor
317 * \return nothing
319 void input_DecoderDelete( decoder_t *p_dec )
321 decoder_owner_sys_t *p_owner = p_dec->p_owner;
323 vlc_object_kill( p_dec );
325 /* Make sure we aren't paused/buffering/waiting anymore */
326 vlc_mutex_lock( &p_owner->lock );
327 const bool b_was_paused = p_owner->b_paused;
328 p_owner->b_paused = false;
329 p_owner->b_buffering = false;
330 p_owner->b_flushing = true;
331 vlc_cond_signal( &p_owner->wait_request );
332 vlc_mutex_unlock( &p_owner->lock );
334 vlc_thread_join( p_dec );
335 p_owner->b_paused = b_was_paused;
337 module_unneed( p_dec, p_dec->p_module );
339 /* */
340 if( p_dec->p_owner->cc.b_supported )
342 int i;
343 for( i = 0; i < 4; i++ )
344 input_DecoderSetCcState( p_dec, false, i );
347 /* Delete decoder */
348 DeleteDecoder( p_dec );
352 * Put a block_t in the decoder's fifo.
353 * Thread-safe w.r.t. the decoder. May be a cancellation point.
355 * \param p_dec the decoder object
356 * \param p_block the data block
358 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
360 decoder_owner_sys_t *p_owner = p_dec->p_owner;
362 if( b_do_pace )
364 /* The fifo is not consummed when buffering and so will
365 * deadlock vlc.
366 * There is no need to lock as b_buffering is never modify
367 * inside decoder thread. */
368 if( !p_owner->b_buffering )
369 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
371 #ifdef __arm__
372 else if( block_FifoSize( p_owner->p_fifo ) > 50*1024*1024 /* 50 MiB */ )
373 #else
374 else if( block_FifoSize( p_owner->p_fifo ) > 400*1024*1024 /* 400 MiB, ie ~ 50mb/s for 60s */ )
375 #endif
377 /* FIXME: ideally we would check the time amount of data
378 * in the FIFO instead of its size. */
379 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
380 "consumed quickly enough), resetting fifo!" );
381 block_FifoEmpty( p_owner->p_fifo );
384 block_FifoPut( p_owner->p_fifo, p_block );
387 bool input_DecoderIsEmpty( decoder_t * p_dec )
389 assert( !p_dec->p_owner->b_buffering );
391 /* FIXME that's not really true */
392 return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
395 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
397 decoder_owner_sys_t *p_owner = p_dec->p_owner;
398 int i;
400 vlc_mutex_lock( &p_owner->lock );
401 for( i = 0; i < 4; i++ )
402 pb_present[i] = p_owner->cc.pb_present[i];
403 vlc_mutex_unlock( &p_owner->lock );
405 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
407 decoder_owner_sys_t *p_owner = p_dec->p_owner;
409 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
411 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
412 return VLC_EGENERIC;
414 if( b_decode )
416 static const vlc_fourcc_t fcc[4] = {
417 VLC_FOURCC('c', 'c', '1', ' '),
418 VLC_FOURCC('c', 'c', '2', ' '),
419 VLC_FOURCC('c', 'c', '3', ' '),
420 VLC_FOURCC('c', 'c', '4', ' '),
422 decoder_t *p_cc;
423 es_format_t fmt;
425 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
426 p_cc = CreateDecoder( p_owner->p_input, &fmt, false, p_owner->p_sout );
427 if( !p_cc )
429 msg_Err( p_dec, "could not create decoder" );
430 dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
431 _("VLC could not open the decoder module.") );
432 return VLC_EGENERIC;
434 else if( !p_cc->p_module )
436 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
437 DeleteDecoder( p_cc );
438 return VLC_EGENERIC;
440 p_cc->p_owner->p_clock = p_owner->p_clock;
442 vlc_mutex_lock( &p_owner->lock );
443 p_owner->cc.pp_decoder[i_channel] = p_cc;
444 vlc_mutex_unlock( &p_owner->lock );
446 else
448 decoder_t *p_cc;
450 vlc_mutex_lock( &p_owner->lock );
451 p_cc = p_owner->cc.pp_decoder[i_channel];
452 p_owner->cc.pp_decoder[i_channel] = NULL;
453 vlc_mutex_unlock( &p_owner->lock );
455 if( p_cc )
457 vlc_object_kill( p_cc );
458 module_unneed( p_cc, p_cc->p_module );
459 DeleteDecoder( p_cc );
462 return VLC_SUCCESS;
464 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
466 decoder_owner_sys_t *p_owner = p_dec->p_owner;
468 *pb_decode = false;
469 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
470 return VLC_EGENERIC;
472 vlc_mutex_lock( &p_owner->lock );
473 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
474 vlc_mutex_unlock( &p_owner->lock );
475 return VLC_EGENERIC;
478 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
480 decoder_owner_sys_t *p_owner = p_dec->p_owner;
482 vlc_mutex_lock( &p_owner->lock );
484 assert( !p_owner->b_paused || !b_paused );
486 p_owner->b_paused = b_paused;
487 p_owner->pause.i_date = i_date;
488 p_owner->pause.i_ignore = 0;
489 vlc_cond_signal( &p_owner->wait_request );
491 DecoderOutputChangePause( p_dec, b_paused, i_date );
493 vlc_mutex_unlock( &p_owner->lock );
496 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
498 decoder_owner_sys_t *p_owner = p_dec->p_owner;
500 vlc_mutex_lock( &p_owner->lock );
501 p_owner->i_ts_delay = i_delay;
502 vlc_mutex_unlock( &p_owner->lock );
505 void input_DecoderStartBuffering( decoder_t *p_dec )
507 decoder_owner_sys_t *p_owner = p_dec->p_owner;
509 vlc_mutex_lock( &p_owner->lock );
511 DecoderFlush( p_dec );
513 p_owner->buffer.b_first = true;
514 p_owner->buffer.b_full = false;
515 p_owner->buffer.i_count = 0;
517 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
518 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
520 p_owner->buffer.p_picture = NULL;
521 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
523 p_owner->buffer.p_subpic = NULL;
524 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
526 p_owner->buffer.p_audio = NULL;
527 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
529 p_owner->buffer.p_block = NULL;
530 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
533 p_owner->b_buffering = true;
535 vlc_cond_signal( &p_owner->wait_request );
537 vlc_mutex_unlock( &p_owner->lock );
540 void input_DecoderStopBuffering( decoder_t *p_dec )
542 decoder_owner_sys_t *p_owner = p_dec->p_owner;
544 vlc_mutex_lock( &p_owner->lock );
546 p_owner->b_buffering = false;
548 vlc_cond_signal( &p_owner->wait_request );
550 vlc_mutex_unlock( &p_owner->lock );
553 void input_DecoderWaitBuffering( decoder_t *p_dec )
555 decoder_owner_sys_t *p_owner = p_dec->p_owner;
557 vlc_mutex_lock( &p_owner->lock );
559 while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
561 block_FifoWake( p_owner->p_fifo );
562 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
565 vlc_mutex_unlock( &p_owner->lock );
568 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
570 decoder_owner_sys_t *p_owner = p_dec->p_owner;
572 *pi_duration = 0;
574 vlc_mutex_lock( &p_owner->lock );
575 if( p_dec->fmt_out.i_cat == VIDEO_ES )
577 if( p_owner->b_paused && p_owner->p_vout )
579 vout_NextPicture( p_owner->p_vout, pi_duration );
580 p_owner->pause.i_ignore++;
581 vlc_cond_signal( &p_owner->wait_request );
584 else
586 /* TODO subtitle should not be flushed */
587 DecoderFlush( p_dec );
589 vlc_mutex_unlock( &p_owner->lock );
592 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
594 decoder_owner_sys_t *p_owner = p_dec->p_owner;
595 bool b_changed;
597 vlc_mutex_lock( &p_owner->lock );
598 b_changed = p_owner->b_fmt_description;
599 if( b_changed )
601 if( p_fmt )
602 es_format_Copy( p_fmt, &p_owner->fmt_description );
604 if( pp_meta )
606 *pp_meta = NULL;
607 if( p_owner->p_description )
609 *pp_meta = vlc_meta_New();
610 if( *pp_meta )
611 vlc_meta_Merge( *pp_meta, p_owner->p_description );
614 p_owner->b_fmt_description = false;
616 vlc_mutex_unlock( &p_owner->lock );
617 return b_changed;
620 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
622 decoder_owner_sys_t *p_owner = p_dec->p_owner;
624 return block_FifoSize( p_owner->p_fifo );
627 void input_DecoderGetObjects( decoder_t *p_dec,
628 vout_thread_t **pp_vout, aout_instance_t **pp_aout )
630 decoder_owner_sys_t *p_owner = p_dec->p_owner;
632 vlc_mutex_lock( &p_owner->lock );
633 if( pp_vout )
634 *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
635 if( pp_aout )
636 *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
637 vlc_mutex_unlock( &p_owner->lock );
640 /*****************************************************************************
641 * Internal functions
642 *****************************************************************************/
643 static int DecoderGetInputAttachments( decoder_t *p_dec,
644 input_attachment_t ***ppp_attachment,
645 int *pi_attachment )
647 return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
648 ppp_attachment, pi_attachment );
650 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
652 decoder_owner_sys_t *p_owner = p_dec->p_owner;
654 vlc_mutex_lock( &p_owner->lock );
655 if( p_owner->b_buffering || p_owner->b_paused )
656 i_ts = VLC_TS_INVALID;
657 vlc_mutex_unlock( &p_owner->lock );
659 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
660 return i_ts;
662 if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
663 return VLC_TS_INVALID;
665 return i_ts;
667 static int DecoderGetDisplayRate( decoder_t *p_dec )
669 decoder_owner_sys_t *p_owner = p_dec->p_owner;
671 if( !p_owner->p_clock )
672 return INPUT_RATE_DEFAULT;
673 return input_clock_GetRate( p_owner->p_clock );
676 /* */
677 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
679 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'. "
680 "VLC probably does not support this sound or video format.",
681 (char*)&codec );
682 dialog_Fatal( p_dec, _("No suitable decoder module"),
683 _("VLC does not support the audio or video format \"%4.4s\". "
684 "Unfortunately there is no way for you to fix this."),
685 (char*)&codec );
690 * Create a decoder object
692 * \param p_input the input thread
693 * \param p_es the es descriptor
694 * \param b_packetizer instead of a decoder
695 * \return the decoder object
697 static decoder_t * CreateDecoder( input_thread_t *p_input,
698 es_format_t *fmt, bool b_packetizer,
699 sout_instance_t *p_sout )
701 decoder_t *p_dec;
702 decoder_owner_sys_t *p_owner;
703 es_format_t null_es_format;
705 p_dec = vlc_custom_create( p_input, sizeof( *p_dec ), VLC_OBJECT_DECODER,
706 "decoder" );
707 if( p_dec == NULL )
708 return NULL;
710 p_dec->pf_decode_audio = NULL;
711 p_dec->pf_decode_video = NULL;
712 p_dec->pf_decode_sub = NULL;
713 p_dec->pf_get_cc = NULL;
714 p_dec->pf_packetize = NULL;
716 /* Initialize the decoder */
717 p_dec->p_module = NULL;
719 memset( &null_es_format, 0, sizeof(es_format_t) );
720 es_format_Copy( &p_dec->fmt_in, fmt );
721 es_format_Copy( &p_dec->fmt_out, &null_es_format );
723 p_dec->p_description = NULL;
725 /* Allocate our private structure for the decoder */
726 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
727 if( p_dec->p_owner == NULL )
729 vlc_object_release( p_dec );
730 return NULL;
732 p_dec->p_owner->i_preroll_end = VLC_TS_INVALID;
733 p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
734 p_dec->p_owner->p_input = p_input;
735 p_dec->p_owner->p_aout = NULL;
736 p_dec->p_owner->p_aout_input = NULL;
737 p_dec->p_owner->p_vout = NULL;
738 p_dec->p_owner->p_spu_vout = NULL;
739 p_dec->p_owner->i_spu_channel = 0;
740 p_dec->p_owner->i_spu_order = 0;
741 p_dec->p_owner->p_sout = p_sout;
742 p_dec->p_owner->p_sout_input = NULL;
743 p_dec->p_owner->p_packetizer = NULL;
744 p_dec->p_owner->b_packetizer = b_packetizer;
746 /* decoder fifo */
747 if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
749 free( p_dec->p_owner );
750 vlc_object_release( p_dec );
751 return NULL;
754 /* Set buffers allocation callbacks for the decoders */
755 p_dec->pf_aout_buffer_new = aout_new_buffer;
756 p_dec->pf_aout_buffer_del = aout_del_buffer;
757 p_dec->pf_vout_buffer_new = vout_new_buffer;
758 p_dec->pf_vout_buffer_del = vout_del_buffer;
759 p_dec->pf_picture_link = vout_link_picture;
760 p_dec->pf_picture_unlink = vout_unlink_picture;
761 p_dec->pf_spu_buffer_new = spu_new_buffer;
762 p_dec->pf_spu_buffer_del = spu_del_buffer;
763 /* */
764 p_dec->pf_get_attachments = DecoderGetInputAttachments;
765 p_dec->pf_get_display_date = DecoderGetDisplayDate;
766 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
768 vlc_object_attach( p_dec, p_input );
770 /* Find a suitable decoder/packetizer module */
771 if( !b_packetizer )
772 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
773 else
774 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
776 /* Check if decoder requires already packetized data */
777 if( !b_packetizer &&
778 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
780 p_dec->p_owner->p_packetizer =
781 vlc_custom_create( p_input, sizeof( decoder_t ),
782 VLC_OBJECT_DECODER, "packetizer" );
783 if( p_dec->p_owner->p_packetizer )
785 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
786 &p_dec->fmt_in );
788 es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
789 &null_es_format );
791 vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
793 p_dec->p_owner->p_packetizer->p_module =
794 module_need( p_dec->p_owner->p_packetizer,
795 "packetizer", "$packetizer", false );
797 if( !p_dec->p_owner->p_packetizer->p_module )
799 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
800 vlc_object_release( p_dec->p_owner->p_packetizer );
805 /* Copy ourself the input replay gain */
806 if( fmt->i_cat == AUDIO_ES )
808 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
810 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
812 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
813 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
815 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
817 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
818 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
823 /* */
824 vlc_mutex_init( &p_owner->lock );
825 vlc_cond_init( &p_owner->wait_request );
826 vlc_cond_init( &p_owner->wait_acknowledge );
828 p_owner->b_fmt_description = false;
829 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
830 p_owner->p_description = NULL;
832 p_owner->b_paused = false;
833 p_owner->pause.i_date = VLC_TS_INVALID;
834 p_owner->pause.i_ignore = 0;
836 p_owner->b_buffering = false;
837 p_owner->buffer.b_first = true;
838 p_owner->buffer.b_full = false;
839 p_owner->buffer.i_count = 0;
840 p_owner->buffer.p_picture = NULL;
841 p_owner->buffer.p_subpic = NULL;
842 p_owner->buffer.p_audio = NULL;
843 p_owner->buffer.p_block = NULL;
845 p_owner->b_flushing = false;
847 /* */
848 p_owner->cc.b_supported = false;
849 if( !b_packetizer )
851 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
852 p_owner->cc.b_supported = true;
853 if( p_dec->pf_get_cc )
854 p_owner->cc.b_supported = true;
857 for( unsigned i = 0; i < 4; i++ )
859 p_owner->cc.pb_present[i] = false;
860 p_owner->cc.pp_decoder[i] = NULL;
862 p_owner->i_ts_delay = 0;
863 return p_dec;
867 * The decoding main loop
869 * \param p_dec the decoder
871 static void *DecoderThread( vlc_object_t *p_this )
873 decoder_t *p_dec = (decoder_t *)p_this;
874 decoder_owner_sys_t *p_owner = p_dec->p_owner;
876 /* The decoder's main loop */
877 for( ;; )
879 block_t *p_block = block_FifoGet( p_owner->p_fifo );
881 /* Make sure there is no cancellation point other than this one^^.
882 * If you need one, be sure to push cleanup of p_block. */
883 DecoderSignalBuffering( p_dec, p_block == NULL );
885 if( p_block )
887 int canc = vlc_savecancel();
889 if( p_dec->b_error )
890 DecoderError( p_dec, p_block );
891 else
892 DecoderProcess( p_dec, p_block );
894 vlc_restorecancel( canc );
897 return NULL;
900 static block_t *DecoderBlockFlushNew()
902 block_t *p_null = block_Alloc( 128 );
903 if( !p_null )
904 return NULL;
906 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
907 BLOCK_FLAG_CORRUPTED |
908 BLOCK_FLAG_CORE_FLUSH;
909 memset( p_null->p_buffer, 0, p_null->i_buffer );
911 return p_null;
914 static void DecoderFlush( decoder_t *p_dec )
916 decoder_owner_sys_t *p_owner = p_dec->p_owner;
918 vlc_assert_locked( &p_owner->lock );
920 /* Empty the fifo */
921 block_FifoEmpty( p_owner->p_fifo );
923 /* Monitor for flush end */
924 p_owner->b_flushing = true;
925 vlc_cond_signal( &p_owner->wait_request );
927 /* Send a special block */
928 block_t *p_null = DecoderBlockFlushNew();
929 if( !p_null )
930 return;
931 input_DecoderDecode( p_dec, p_null, false );
933 /* */
934 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
935 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
938 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
940 decoder_owner_sys_t *p_owner = p_dec->p_owner;
942 vlc_mutex_lock( &p_owner->lock );
944 if( p_owner->b_buffering )
946 if( b_full )
947 p_owner->buffer.b_full = true;
948 vlc_cond_signal( &p_owner->wait_acknowledge );
951 vlc_mutex_unlock( &p_owner->lock );
954 static bool DecoderIsFlushing( decoder_t *p_dec )
956 decoder_owner_sys_t *p_owner = p_dec->p_owner;
957 bool b_flushing;
959 vlc_mutex_lock( &p_owner->lock );
961 b_flushing = p_owner->b_flushing;
963 vlc_mutex_unlock( &p_owner->lock );
965 return b_flushing;
968 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
970 decoder_owner_sys_t *p_owner = p_dec->p_owner;
972 vlc_assert_locked( &p_owner->lock );
974 while( !p_owner->b_flushing )
976 if( p_owner->b_paused )
978 if( p_owner->b_buffering && !p_owner->buffer.b_full )
979 break;
980 if( p_owner->pause.i_ignore > 0 )
982 p_owner->pause.i_ignore--;
983 break;
986 else
988 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
989 break;
991 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
994 if( pb_reject )
995 *pb_reject = p_owner->b_flushing;
998 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
1000 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1002 vlc_assert_locked( &p_owner->lock );
1004 /* XXX only audio and video output have to be paused.
1005 * - for sout it is useless
1006 * - for subs, it is done by the vout
1008 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1010 if( p_owner->p_aout && p_owner->p_aout_input )
1011 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1012 b_paused, i_date );
1014 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1016 if( p_owner->p_vout )
1017 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1020 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1022 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1023 *pi_preroll = INT64_MAX;
1024 else if( p->i_dts > VLC_TS_INVALID )
1025 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1026 else if( p->i_pts > VLC_TS_INVALID )
1027 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1030 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1032 mtime_t current_date = mdate();
1034 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1035 if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1037 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1038 * for teletext streams. */
1039 return current_date + 400000;
1041 return i_ts;
1044 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1045 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1047 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1048 input_clock_t *p_clock = p_owner->p_clock;
1050 vlc_assert_locked( &p_owner->lock );
1052 const mtime_t i_es_delay = p_owner->i_ts_delay;
1054 if( p_clock )
1056 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1057 int i_rate;
1059 if( *pi_ts0 > VLC_TS_INVALID )
1061 *pi_ts0 += i_es_delay;
1062 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1063 *pi_ts1 += i_es_delay;
1064 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1065 *pi_ts0 = VLC_TS_INVALID;
1067 else
1069 i_rate = input_clock_GetRate( p_clock );
1072 /* Do not create ephemere data because of rounding errors */
1073 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1074 *pi_ts1 += 1;
1076 if( pi_duration )
1077 *pi_duration = ( *pi_duration * i_rate +
1078 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1080 if( pi_rate )
1081 *pi_rate = i_rate;
1083 if( b_telx )
1085 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1086 if( pi_ts1 && *pi_ts1 <= VLC_TS_INVALID )
1087 *pi_ts1 = *pi_ts0;
1093 * If *pb_reject, it does nothing, otherwise it waits for the given
1094 * deadline or a flush request (in which case it set *pi_reject to true.
1096 static void DecoderWaitDate( decoder_t *p_dec,
1097 bool *pb_reject, mtime_t i_deadline )
1099 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1101 if( *pb_reject || i_deadline < 0 )
1102 return;
1104 for( ;; )
1106 vlc_mutex_lock( &p_owner->lock );
1107 if( p_owner->b_flushing || p_dec->b_die )
1109 *pb_reject = true;
1110 vlc_mutex_unlock( &p_owner->lock );
1111 break;
1113 int i_ret = vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1114 i_deadline );
1115 vlc_mutex_unlock( &p_owner->lock );
1116 if( i_ret )
1117 break;
1121 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1122 int *pi_played_sum, int *pi_lost_sum )
1124 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1125 aout_instance_t *p_aout = p_owner->p_aout;
1126 aout_input_t *p_aout_input = p_owner->p_aout_input;
1128 /* */
1129 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1131 msg_Warn( p_dec, "non-dated audio buffer received" );
1132 *pi_lost_sum += 1;
1133 aout_BufferFree( p_audio );
1134 return;
1137 /* */
1138 vlc_mutex_lock( &p_owner->lock );
1140 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1142 p_audio->p_next = NULL;
1144 *p_owner->buffer.pp_audio_next = p_audio;
1145 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1147 p_owner->buffer.i_count++;
1148 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1149 p_audio->i_pts - p_owner->buffer.p_audio->i_pts > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1151 p_owner->buffer.b_full = true;
1152 vlc_cond_signal( &p_owner->wait_acknowledge );
1156 for( ;; )
1158 bool b_has_more = false;
1159 bool b_reject;
1160 DecoderWaitUnblock( p_dec, &b_reject );
1162 if( p_owner->b_buffering )
1164 vlc_mutex_unlock( &p_owner->lock );
1165 return;
1168 /* */
1169 if( p_owner->buffer.p_audio )
1171 p_audio = p_owner->buffer.p_audio;
1173 p_owner->buffer.p_audio = p_audio->p_next;
1174 p_owner->buffer.i_count--;
1176 b_has_more = p_owner->buffer.p_audio != NULL;
1177 if( !b_has_more )
1178 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1181 /* */
1182 const bool b_dated = p_audio->i_pts > VLC_TS_INVALID;
1183 int i_rate = INPUT_RATE_DEFAULT;
1185 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1186 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1188 vlc_mutex_unlock( &p_owner->lock );
1190 if( !p_aout || !p_aout_input ||
1191 p_audio->i_pts <= VLC_TS_INVALID ||
1192 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1193 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1194 b_reject = true;
1196 DecoderWaitDate( p_dec, &b_reject,
1197 p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1199 if( !b_reject )
1201 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1202 *pi_played_sum += 1;
1203 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1205 else
1207 if( b_dated )
1208 msg_Warn( p_aout, "received buffer in the future" );
1209 else
1210 msg_Warn( p_dec, "non-dated audio buffer received" );
1212 *pi_lost_sum += 1;
1213 aout_BufferFree( p_audio );
1216 if( !b_has_more )
1217 break;
1219 vlc_mutex_lock( &p_owner->lock );
1220 if( !p_owner->buffer.p_audio )
1222 vlc_mutex_unlock( &p_owner->lock );
1223 break;
1228 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1230 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1231 input_thread_t *p_input = p_owner->p_input;
1232 aout_buffer_t *p_aout_buf;
1233 int i_decoded = 0;
1234 int i_lost = 0;
1235 int i_played = 0;
1237 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1239 aout_instance_t *p_aout = p_owner->p_aout;
1240 aout_input_t *p_aout_input = p_owner->p_aout_input;
1242 if( p_dec->b_die )
1244 /* It prevent freezing VLC in case of broken decoder */
1245 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1246 if( p_block )
1247 block_Release( p_block );
1248 break;
1250 i_decoded++;
1252 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1253 p_aout_buf->i_pts < p_owner->i_preroll_end )
1255 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1256 continue;
1259 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1261 msg_Dbg( p_dec, "End of audio preroll" );
1262 if( p_owner->p_aout && p_owner->p_aout_input )
1263 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1264 /* */
1265 p_owner->i_preroll_end = VLC_TS_INVALID;
1268 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1271 /* Update ugly stat */
1272 if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1274 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1276 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1277 i_lost, NULL );
1278 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1279 i_played, NULL );
1280 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1281 i_decoded, NULL );
1283 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1286 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1288 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1289 block_t *p_cc;
1290 bool pb_present[4];
1291 bool b_processed = false;
1292 int i;
1293 int i_cc_decoder;
1295 assert( p_dec_cc->pf_get_cc != NULL );
1297 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1298 if( !p_owner->cc.b_supported )
1299 return;
1301 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1302 if( !p_cc )
1303 return;
1305 vlc_mutex_lock( &p_owner->lock );
1306 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1308 p_owner->cc.pb_present[i] |= pb_present[i];
1309 if( p_owner->cc.pp_decoder[i] )
1310 i_cc_decoder++;
1313 for( i = 0; i < 4; i++ )
1315 if( !p_owner->cc.pp_decoder[i] )
1316 continue;
1318 if( i_cc_decoder > 1 )
1319 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1320 else
1321 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1322 i_cc_decoder--;
1323 b_processed = true;
1325 vlc_mutex_unlock( &p_owner->lock );
1327 if( !b_processed )
1328 block_Release( p_cc );
1331 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1332 int *pi_played_sum, int *pi_lost_sum )
1334 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1335 vout_thread_t *p_vout = p_owner->p_vout;
1336 bool b_first_buffered;
1338 if( p_picture->date <= VLC_TS_INVALID )
1340 msg_Warn( p_vout, "non-dated video buffer received" );
1341 *pi_lost_sum += 1;
1342 vout_DropPicture( p_vout, p_picture );
1343 return;
1345 vout_LinkPicture( p_vout, p_picture );
1347 /* */
1348 vlc_mutex_lock( &p_owner->lock );
1350 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1352 p_picture->p_next = NULL;
1354 *p_owner->buffer.pp_picture_next = p_picture;
1355 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1357 p_owner->buffer.i_count++;
1358 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1359 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1361 p_owner->buffer.b_full = true;
1362 vlc_cond_signal( &p_owner->wait_acknowledge );
1365 b_first_buffered = p_owner->buffer.p_picture != NULL;
1367 for( ;; b_first_buffered = false )
1369 bool b_has_more = false;
1371 bool b_reject;
1373 DecoderWaitUnblock( p_dec, &b_reject );
1375 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1377 vlc_mutex_unlock( &p_owner->lock );
1378 return;
1380 bool b_buffering_first = p_owner->b_buffering;
1382 /* */
1383 if( p_owner->buffer.p_picture )
1385 p_picture = p_owner->buffer.p_picture;
1387 p_owner->buffer.p_picture = p_picture->p_next;
1388 p_owner->buffer.i_count--;
1390 b_has_more = p_owner->buffer.p_picture != NULL;
1391 if( !b_has_more )
1392 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1395 /* */
1396 if( b_buffering_first )
1398 assert( p_owner->buffer.b_first );
1399 assert( !p_owner->buffer.i_count );
1400 msg_Dbg( p_dec, "Received first picture" );
1401 p_owner->buffer.b_first = false;
1402 p_picture->b_force = true;
1405 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1406 int i_rate = INPUT_RATE_DEFAULT;
1407 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1408 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1410 vlc_mutex_unlock( &p_owner->lock );
1412 /* */
1413 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1414 b_reject = true;
1416 if( !b_reject )
1418 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1420 /* Be sure to not display old picture after our own */
1421 vout_Flush( p_vout, p_picture->date );
1422 p_owner->i_last_rate = i_rate;
1424 vout_DisplayPicture( p_vout, p_picture );
1425 vout_UnlinkPicture( p_vout, p_picture );
1427 else
1429 if( b_dated )
1430 msg_Warn( p_vout, "early picture skipped" );
1431 else
1432 msg_Warn( p_vout, "non-dated video buffer received" );
1434 *pi_lost_sum += 1;
1435 vout_UnlinkPicture( p_vout, p_picture );
1436 vout_DropPicture( p_vout, p_picture );
1438 int i_tmp_display;
1439 int i_tmp_lost;
1440 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1442 *pi_played_sum += i_tmp_display;
1443 *pi_lost_sum += i_tmp_lost;
1445 if( !b_has_more || b_buffering_first )
1446 break;
1448 vlc_mutex_lock( &p_owner->lock );
1449 if( !p_owner->buffer.p_picture )
1451 vlc_mutex_unlock( &p_owner->lock );
1452 break;
1457 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1459 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1460 input_thread_t *p_input = p_owner->p_input;
1461 picture_t *p_pic;
1462 int i_lost = 0;
1463 int i_decoded = 0;
1464 int i_displayed = 0;
1466 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1468 vout_thread_t *p_vout = p_owner->p_vout;
1469 if( p_dec->b_die )
1471 /* It prevent freezing VLC in case of broken decoder */
1472 vout_DropPicture( p_vout, p_pic );
1473 if( p_block )
1474 block_Release( p_block );
1475 break;
1478 i_decoded++;
1480 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1482 vout_DropPicture( p_vout, p_pic );
1483 continue;
1486 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1488 msg_Dbg( p_dec, "End of video preroll" );
1489 if( p_vout )
1490 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1491 /* */
1492 p_owner->i_preroll_end = VLC_TS_INVALID;
1495 if( p_dec->pf_get_cc &&
1496 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1497 DecoderGetCc( p_dec, p_dec );
1499 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1501 if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1503 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1505 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1506 i_decoded, NULL );
1507 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1508 i_lost , NULL);
1510 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1511 i_displayed, NULL);
1513 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1517 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1518 bool b_telx )
1520 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1521 vout_thread_t *p_vout = p_owner->p_spu_vout;
1523 /* */
1524 if( p_subpic->i_start <= VLC_TS_INVALID && !b_telx )
1526 msg_Warn( p_dec, "non-dated spu buffer received" );
1527 subpicture_Delete( p_subpic );
1528 return;
1531 /* */
1532 vlc_mutex_lock( &p_owner->lock );
1534 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1536 p_subpic->p_next = NULL;
1538 *p_owner->buffer.pp_subpic_next = p_subpic;
1539 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1541 p_owner->buffer.i_count++;
1542 /* XXX it is important to be full after the first one */
1543 if( p_owner->buffer.i_count > 0 )
1545 p_owner->buffer.b_full = true;
1546 vlc_cond_signal( &p_owner->wait_acknowledge );
1550 for( ;; )
1552 bool b_has_more = false;
1553 bool b_reject;
1554 DecoderWaitUnblock( p_dec, &b_reject );
1556 if( p_owner->b_buffering )
1558 vlc_mutex_unlock( &p_owner->lock );
1559 return;
1562 /* */
1563 if( p_owner->buffer.p_subpic )
1565 p_subpic = p_owner->buffer.p_subpic;
1567 p_owner->buffer.p_subpic = p_subpic->p_next;
1568 p_owner->buffer.i_count--;
1570 b_has_more = p_owner->buffer.p_subpic != NULL;
1571 if( !b_has_more )
1572 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1575 /* */
1576 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1577 NULL, INT64_MAX, b_telx );
1579 vlc_mutex_unlock( &p_owner->lock );
1581 if( p_subpic->i_start <= VLC_TS_INVALID )
1582 b_reject = true;
1584 DecoderWaitDate( p_dec, &b_reject,
1585 p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1587 if( !b_reject )
1588 spu_DisplaySubpicture( vout_GetSpu( p_vout ), p_subpic );
1589 else
1590 subpicture_Delete( p_subpic );
1592 if( !b_has_more )
1593 break;
1594 vlc_mutex_lock( &p_owner->lock );
1595 if( !p_owner->buffer.p_subpic )
1597 vlc_mutex_unlock( &p_owner->lock );
1598 break;
1603 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1604 bool b_telx )
1606 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1608 assert( p_owner->p_clock );
1609 assert( !p_sout_block->p_next );
1611 vlc_mutex_lock( &p_owner->lock );
1613 if( p_owner->b_buffering || p_owner->buffer.p_block )
1615 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1617 p_owner->buffer.i_count++;
1618 /* XXX it is important to be full after the first one */
1619 if( p_owner->buffer.i_count > 0 )
1621 p_owner->buffer.b_full = true;
1622 vlc_cond_signal( &p_owner->wait_acknowledge );
1626 for( ;; )
1628 bool b_has_more = false;
1629 bool b_reject;
1630 DecoderWaitUnblock( p_dec, &b_reject );
1632 if( p_owner->b_buffering )
1634 vlc_mutex_unlock( &p_owner->lock );
1635 return;
1638 /* */
1639 if( p_owner->buffer.p_block )
1641 p_sout_block = p_owner->buffer.p_block;
1643 p_owner->buffer.p_block = p_sout_block->p_next;
1644 p_owner->buffer.i_count--;
1646 b_has_more = p_owner->buffer.p_block != NULL;
1647 if( !b_has_more )
1648 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1650 p_sout_block->p_next = NULL;
1652 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1653 &p_sout_block->i_length,
1654 &p_sout_block->i_rate, INT64_MAX, b_telx );
1656 vlc_mutex_unlock( &p_owner->lock );
1658 if( !b_reject )
1659 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1660 else
1661 block_Release( p_sout_block );
1663 if( !b_has_more )
1664 break;
1665 vlc_mutex_lock( &p_owner->lock );
1666 if( !p_owner->buffer.p_block )
1668 vlc_mutex_unlock( &p_owner->lock );
1669 break;
1674 /* */
1675 static void DecoderFlushBuffering( decoder_t *p_dec )
1677 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1679 vlc_assert_locked( &p_owner->lock );
1681 while( p_owner->buffer.p_picture )
1683 picture_t *p_picture = p_owner->buffer.p_picture;
1685 p_owner->buffer.p_picture = p_picture->p_next;
1686 p_owner->buffer.i_count--;
1688 if( p_owner->p_vout )
1690 vout_UnlinkPicture( p_owner->p_vout, p_picture );
1691 vout_DropPicture( p_owner->p_vout, p_picture );
1694 if( !p_owner->buffer.p_picture )
1695 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1697 while( p_owner->buffer.p_audio )
1699 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1701 p_owner->buffer.p_audio = p_audio->p_next;
1702 p_owner->buffer.i_count--;
1704 aout_BufferFree( p_audio );
1706 if( !p_owner->buffer.p_audio )
1707 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1709 while( p_owner->buffer.p_subpic )
1711 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1713 p_owner->buffer.p_subpic = p_subpic->p_next;
1714 p_owner->buffer.i_count--;
1716 subpicture_Delete( p_subpic );
1718 if( !p_owner->buffer.p_subpic )
1719 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1721 if( p_owner->buffer.p_block )
1723 block_ChainRelease( p_owner->buffer.p_block );
1725 p_owner->buffer.i_count = 0;
1726 p_owner->buffer.p_block = NULL;
1727 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1731 /* This function process a block for sout
1733 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1735 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1736 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1737 block_t *p_sout_block;
1739 while( ( p_sout_block =
1740 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1742 if( !p_owner->p_sout_input )
1744 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1746 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1747 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1748 if( p_dec->fmt_in.psz_language )
1750 free( p_owner->sout.psz_language );
1751 p_owner->sout.psz_language =
1752 strdup( p_dec->fmt_in.psz_language );
1755 p_owner->p_sout_input =
1756 sout_InputNew( p_owner->p_sout,
1757 &p_owner->sout );
1759 if( p_owner->p_sout_input == NULL )
1761 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1762 (char *)&p_owner->sout.i_codec );
1763 p_dec->b_error = true;
1765 while( p_sout_block )
1767 block_t *p_next = p_sout_block->p_next;
1768 block_Release( p_sout_block );
1769 p_sout_block = p_next;
1771 break;
1775 while( p_sout_block )
1777 block_t *p_next = p_sout_block->p_next;
1779 p_sout_block->p_next = NULL;
1781 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1783 p_sout_block = p_next;
1788 /* This function process a video block
1790 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1792 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1794 if( p_owner->p_packetizer )
1796 block_t *p_packetized_block;
1797 decoder_t *p_packetizer = p_owner->p_packetizer;
1799 while( (p_packetized_block =
1800 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1802 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1804 es_format_Clean( &p_dec->fmt_in );
1805 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1807 if( p_packetizer->pf_get_cc )
1808 DecoderGetCc( p_dec, p_packetizer );
1810 while( p_packetized_block )
1812 block_t *p_next = p_packetized_block->p_next;
1813 p_packetized_block->p_next = NULL;
1815 DecoderDecodeVideo( p_dec, p_packetized_block );
1817 p_packetized_block = p_next;
1820 /* The packetizer does not output a block that tell the decoder to flush
1821 * do it ourself */
1822 if( b_flush )
1824 block_t *p_null = DecoderBlockFlushNew();
1825 if( p_null )
1826 DecoderDecodeVideo( p_dec, p_null );
1829 else if( p_block )
1831 DecoderDecodeVideo( p_dec, p_block );
1834 if( b_flush && p_owner->p_vout )
1835 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1838 /* This function process a audio block
1840 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1842 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1844 if( p_owner->p_packetizer )
1846 block_t *p_packetized_block;
1847 decoder_t *p_packetizer = p_owner->p_packetizer;
1849 while( (p_packetized_block =
1850 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1852 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1854 es_format_Clean( &p_dec->fmt_in );
1855 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1858 while( p_packetized_block )
1860 block_t *p_next = p_packetized_block->p_next;
1861 p_packetized_block->p_next = NULL;
1863 DecoderDecodeAudio( p_dec, p_packetized_block );
1865 p_packetized_block = p_next;
1868 /* The packetizer does not output a block that tell the decoder to flush
1869 * do it ourself */
1870 if( b_flush )
1872 block_t *p_null = DecoderBlockFlushNew();
1873 if( p_null )
1874 DecoderDecodeAudio( p_dec, p_null );
1877 else if( p_block )
1879 DecoderDecodeAudio( p_dec, p_block );
1882 if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1883 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1886 /* This function process a subtitle block
1888 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1890 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1891 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1893 input_thread_t *p_input = p_owner->p_input;
1894 vout_thread_t *p_vout;
1895 subpicture_t *p_spu;
1897 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1899 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1900 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1901 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1903 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1904 if( p_vout && p_owner->p_spu_vout == p_vout )
1906 /* Preroll does not work very well with subtitle */
1907 if( p_spu->i_start > VLC_TS_INVALID &&
1908 p_spu->i_start < p_owner->i_preroll_end &&
1909 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1911 subpicture_Delete( p_spu );
1913 else
1915 DecoderPlaySpu( p_dec, p_spu, b_telx );
1918 else
1920 subpicture_Delete( p_spu );
1922 if( p_vout )
1923 vlc_object_release( p_vout );
1926 if( b_flush && p_owner->p_spu_vout )
1928 p_vout = input_resource_HoldVout( p_input->p->p_resource );
1930 if( p_vout && p_owner->p_spu_vout == p_vout )
1931 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR,
1932 p_owner->i_spu_channel );
1934 if( p_vout )
1935 vlc_object_release( p_vout );
1939 /* */
1940 static void DecoderProcessOnFlush( decoder_t *p_dec )
1942 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1944 vlc_mutex_lock( &p_owner->lock );
1945 DecoderFlushBuffering( p_dec );
1947 if( p_owner->b_flushing )
1949 p_owner->b_flushing = false;
1950 vlc_cond_signal( &p_owner->wait_acknowledge );
1952 vlc_mutex_unlock( &p_owner->lock );
1956 * Decode a block
1958 * \param p_dec the decoder object
1959 * \param p_block the block to decode
1960 * \return VLC_SUCCESS or an error code
1962 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1964 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1965 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1967 if( p_block && p_block->i_buffer <= 0 )
1969 assert( !b_flush_request );
1970 block_Release( p_block );
1971 return;
1974 #ifdef ENABLE_SOUT
1975 if( p_owner->b_packetizer )
1977 if( p_block )
1978 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1980 DecoderProcessSout( p_dec, p_block );
1982 else
1983 #endif
1985 bool b_flush = false;
1987 if( p_block )
1989 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1990 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1992 b_flush = !b_flushing && b_flush_request;
1994 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1997 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1999 DecoderProcessAudio( p_dec, p_block, b_flush );
2001 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
2003 DecoderProcessVideo( p_dec, p_block, b_flush );
2005 else if( p_dec->fmt_out.i_cat == SPU_ES )
2007 DecoderProcessSpu( p_dec, p_block, b_flush );
2009 else
2011 msg_Err( p_dec, "unknown ES format" );
2012 p_dec->b_error = true;
2016 /* */
2017 if( b_flush_request )
2018 DecoderProcessOnFlush( p_dec );
2021 static void DecoderError( decoder_t *p_dec, block_t *p_block )
2023 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2025 /* */
2026 if( p_block )
2027 block_Release( p_block );
2029 if( b_flush_request )
2030 DecoderProcessOnFlush( p_dec );
2035 * Destroys a decoder object
2037 * \param p_dec the decoder object
2038 * \return nothing
2040 static void DeleteDecoder( decoder_t * p_dec )
2042 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2044 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2045 (char*)&p_dec->fmt_in.i_codec,
2046 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2048 /* Free all packets still in the decoder fifo. */
2049 block_FifoEmpty( p_owner->p_fifo );
2050 block_FifoRelease( p_owner->p_fifo );
2052 /* */
2053 vlc_mutex_lock( &p_owner->lock );
2054 DecoderFlushBuffering( p_dec );
2055 vlc_mutex_unlock( &p_owner->lock );
2057 /* Cleanup */
2058 if( p_owner->p_aout_input )
2059 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2060 if( p_owner->p_aout )
2062 input_resource_RequestAout( p_owner->p_input->p->p_resource,
2063 p_owner->p_aout );
2064 input_SendEventAout( p_owner->p_input );
2065 p_owner->p_aout = NULL;
2067 if( p_owner->p_vout )
2069 /* Hack to make sure all the the pictures are freed by the decoder
2070 * and that the vout is not paused anymore */
2071 vout_FixLeaks( p_owner->p_vout, true );
2072 if( p_owner->b_paused )
2073 vout_ChangePause( p_owner->p_vout, false, mdate() );
2075 /* */
2076 input_resource_RequestVout( p_owner->p_input->p->p_resource, p_owner->p_vout, NULL, true );
2077 input_SendEventVout( p_owner->p_input );
2080 #ifdef ENABLE_SOUT
2081 if( p_owner->p_sout_input )
2083 sout_InputDelete( p_owner->p_sout_input );
2084 es_format_Clean( &p_owner->sout );
2086 #endif
2088 if( p_dec->fmt_out.i_cat == SPU_ES )
2090 vout_thread_t *p_vout;
2092 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2093 if( p_vout )
2095 if( p_owner->p_spu_vout == p_vout )
2096 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR, p_owner->i_spu_channel );
2097 vlc_object_release( p_vout );
2101 es_format_Clean( &p_dec->fmt_in );
2102 es_format_Clean( &p_dec->fmt_out );
2103 if( p_dec->p_description )
2104 vlc_meta_Delete( p_dec->p_description );
2105 es_format_Clean( &p_owner->fmt_description );
2106 if( p_owner->p_description )
2107 vlc_meta_Delete( p_owner->p_description );
2109 if( p_owner->p_packetizer )
2111 module_unneed( p_owner->p_packetizer,
2112 p_owner->p_packetizer->p_module );
2113 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2114 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2115 if( p_owner->p_packetizer->p_description )
2116 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2117 vlc_object_release( p_owner->p_packetizer );
2120 vlc_cond_destroy( &p_owner->wait_acknowledge );
2121 vlc_cond_destroy( &p_owner->wait_request );
2122 vlc_mutex_destroy( &p_owner->lock );
2124 vlc_object_release( p_dec );
2126 free( p_owner );
2129 /*****************************************************************************
2130 * Buffers allocation callbacks for the decoders
2131 *****************************************************************************/
2132 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2134 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2136 vlc_assert_locked( &p_owner->lock );
2138 p_owner->b_fmt_description = true;
2140 /* Copy es_format */
2141 es_format_Clean( &p_owner->fmt_description );
2142 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2144 /* Move p_description */
2145 if( p_owner->p_description && p_dec->p_description )
2146 vlc_meta_Delete( p_owner->p_description );
2147 p_owner->p_description = p_dec->p_description;
2148 p_dec->p_description = NULL;
2150 static vout_thread_t *aout_request_vout( void *p_private,
2151 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2153 decoder_t *p_dec = p_private;
2154 input_thread_t *p_input = p_dec->p_owner->p_input;
2156 p_vout = input_resource_RequestVout( p_input->p->p_resource, p_vout, p_fmt, b_recyle );
2157 input_SendEventVout( p_input );
2159 return p_vout;
2162 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2164 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2165 aout_buffer_t *p_buffer;
2167 if( p_owner->p_aout_input != NULL &&
2168 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2169 p_dec->fmt_out.audio.i_original_channels !=
2170 p_owner->audio.i_original_channels ||
2171 p_dec->fmt_out.audio.i_bytes_per_frame !=
2172 p_owner->audio.i_bytes_per_frame ) )
2174 aout_input_t *p_aout_input = p_owner->p_aout_input;
2176 /* Parameters changed, restart the aout */
2177 vlc_mutex_lock( &p_owner->lock );
2179 DecoderFlushBuffering( p_dec );
2181 p_owner->p_aout_input = NULL;
2182 aout_DecDelete( p_owner->p_aout, p_aout_input );
2184 vlc_mutex_unlock( &p_owner->lock );
2187 if( p_owner->p_aout_input == NULL )
2189 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
2190 audio_sample_format_t format;
2191 aout_input_t *p_aout_input;
2192 aout_instance_t *p_aout;
2193 aout_request_vout_t request_vout;
2195 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2196 p_owner->audio = p_dec->fmt_out.audio;
2198 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2199 if( i_force_dolby &&
2200 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2201 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2203 if( i_force_dolby == 1 )
2205 format.i_original_channels = format.i_original_channels |
2206 AOUT_CHAN_DOLBYSTEREO;
2208 else /* i_force_dolby == 2 */
2210 format.i_original_channels = format.i_original_channels &
2211 ~AOUT_CHAN_DOLBYSTEREO;
2215 request_vout.pf_request_vout = aout_request_vout;
2216 request_vout.p_private = p_dec;
2218 p_aout = p_owner->p_aout;
2219 if( !p_aout )
2220 p_aout = input_resource_RequestAout( p_owner->p_input->p->p_resource, NULL );
2221 p_aout_input = aout_DecNew( p_dec, &p_aout,
2222 &format, &p_dec->fmt_out.audio_replay_gain, &request_vout );
2224 vlc_mutex_lock( &p_owner->lock );
2226 p_owner->p_aout = p_aout;
2227 p_owner->p_aout_input = p_aout_input;
2228 DecoderUpdateFormatLocked( p_dec );
2230 vlc_mutex_unlock( &p_owner->lock );
2232 input_SendEventAout( p_owner->p_input );
2234 if( p_owner->p_aout_input == NULL )
2236 msg_Err( p_dec, "failed to create audio output" );
2237 p_dec->b_error = true;
2238 return NULL;
2240 p_dec->fmt_out.audio.i_bytes_per_frame =
2241 p_owner->audio.i_bytes_per_frame;
2244 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2246 return p_buffer;
2249 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2251 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2253 aout_DecDeleteBuffer( p_owner->p_aout,
2254 p_owner->p_aout_input, p_buffer );
2258 int vout_CountPictureAvailable( vout_thread_t *p_vout );
2260 static picture_t *vout_new_buffer( decoder_t *p_dec )
2262 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2264 if( p_owner->p_vout == NULL ||
2265 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2266 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2267 p_dec->fmt_out.video.i_visible_width != p_owner->video.i_visible_width ||
2268 p_dec->fmt_out.video.i_visible_height != p_owner->video.i_visible_height ||
2269 p_dec->fmt_out.video.i_x_offset != p_owner->video.i_x_offset ||
2270 p_dec->fmt_out.video.i_y_offset != p_owner->video.i_y_offset ||
2271 p_dec->fmt_out.i_codec != p_owner->video.i_chroma ||
2272 (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2273 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num )
2275 vout_thread_t *p_vout;
2277 if( !p_dec->fmt_out.video.i_width ||
2278 !p_dec->fmt_out.video.i_height )
2280 /* Can't create a new vout without display size */
2281 return NULL;
2284 video_format_t fmt = p_dec->fmt_out.video;
2285 fmt.i_chroma = p_dec->fmt_out.i_codec;
2286 p_owner->video = fmt;
2288 if( !fmt.i_visible_width || !fmt.i_visible_height )
2290 if( p_dec->fmt_in.video.i_visible_width &&
2291 p_dec->fmt_in.video.i_visible_height )
2293 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
2294 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2296 else
2298 fmt.i_visible_width = fmt.i_width;
2299 fmt.i_visible_height = fmt.i_height;
2303 if( fmt.i_visible_height == 1088 &&
2304 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2306 fmt.i_visible_height = 1080;
2307 if( !(fmt.i_sar_num % 136))
2309 fmt.i_sar_num *= 135;
2310 fmt.i_sar_den *= 136;
2312 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2315 if( !fmt.i_sar_num || !fmt.i_sar_den )
2317 fmt.i_sar_num = 1;
2318 fmt.i_sar_den = 1;
2321 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2322 fmt.i_sar_num, fmt.i_sar_den, 50000 );
2324 vlc_mutex_lock( &p_owner->lock );
2326 DecoderFlushBuffering( p_dec );
2328 p_vout = p_owner->p_vout;
2329 p_owner->p_vout = NULL;
2330 vlc_mutex_unlock( &p_owner->lock );
2332 p_vout = input_resource_RequestVout( p_owner->p_input->p->p_resource,
2333 p_vout, &fmt, true );
2334 vlc_mutex_lock( &p_owner->lock );
2335 p_owner->p_vout = p_vout;
2337 DecoderUpdateFormatLocked( p_dec );
2339 vlc_mutex_unlock( &p_owner->lock );
2341 input_SendEventVout( p_owner->p_input );
2342 if( p_vout == NULL )
2344 msg_Err( p_dec, "failed to create video output" );
2345 p_dec->b_error = true;
2346 return NULL;
2349 if( p_owner->video.i_rmask )
2350 p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2351 if( p_owner->video.i_gmask )
2352 p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2353 if( p_owner->video.i_bmask )
2354 p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2357 /* Get a new picture
2359 for( ;; )
2361 picture_t *p_picture;
2363 if( p_dec->b_die || p_dec->b_error )
2364 return NULL;
2366 /* The video filter chain required that there is always 1 free buffer
2367 * that it will use as temporary one. It will release the temporary
2368 * buffer once its work is done, so this check is safe even if we don't
2369 * lock around both count() and create().
2371 if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2373 p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2374 if( p_picture )
2375 return p_picture;
2378 if( DecoderIsFlushing( p_dec ) )
2379 return NULL;
2381 /* */
2382 DecoderSignalBuffering( p_dec, true );
2384 /* Check the decoder doesn't leak pictures */
2385 vout_FixLeaks( p_owner->p_vout, false );
2387 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2388 msleep( VOUT_OUTMEM_SLEEP );
2392 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2394 vout_DropPicture( p_dec->p_owner->p_vout, p_pic );
2397 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2399 vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2402 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2404 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2407 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2409 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2410 vout_thread_t *p_vout = NULL;
2411 subpicture_t *p_subpic;
2412 int i_attempts = 30;
2414 while( i_attempts-- )
2416 if( p_dec->b_die || p_dec->b_error )
2417 break;
2419 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2420 if( p_vout )
2421 break;
2423 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2426 if( !p_vout )
2428 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2429 return NULL;
2432 if( p_owner->p_spu_vout != p_vout )
2434 vlc_mutex_lock( &p_owner->lock );
2436 DecoderFlushBuffering( p_dec );
2438 vlc_mutex_unlock( &p_owner->lock );
2440 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_REGISTER,
2441 &p_owner->i_spu_channel );
2442 p_owner->i_spu_order = 0;
2443 p_owner->p_spu_vout = p_vout;
2446 p_subpic = subpicture_New();
2447 if( p_subpic )
2449 p_subpic->i_channel = p_owner->i_spu_channel;
2450 p_subpic->i_order = p_owner->i_spu_order++;
2451 p_subpic->b_subtitle = true;
2454 vlc_object_release( p_vout );
2456 return p_subpic;
2459 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2461 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2462 vout_thread_t *p_vout = NULL;
2464 p_vout = input_resource_HoldVout( p_owner->p_input->p->p_resource );
2465 if( !p_vout || p_owner->p_spu_vout != p_vout )
2467 if( p_vout )
2468 vlc_object_release( p_vout );
2469 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2470 return;
2473 subpicture_Delete( p_subpic );
2475 vlc_object_release( p_vout );