1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
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 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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_block.h>
40 #include <vlc_codec.h>
43 #include <vlc_dialog.h>
44 #include <vlc_modules.h>
46 #include "audio_output/aout_internal.h"
47 #include "stream_output/stream_output.h"
48 #include "input_internal.h"
54 #include "../video_output/vout_control.h"
56 static decoder_t
*CreateDecoder( vlc_object_t
*, input_thread_t
*,
57 es_format_t
*, bool, input_resource_t
*,
58 sout_instance_t
*p_sout
);
59 static void DeleteDecoder( decoder_t
* );
61 static void *DecoderThread( void * );
62 static void DecoderProcess( decoder_t
*, block_t
* );
63 static void DecoderError( decoder_t
*p_dec
, block_t
*p_block
);
64 static void DecoderOutputChangePause( decoder_t
*, bool b_paused
, mtime_t i_date
);
65 static void DecoderFlush( decoder_t
* );
66 static void DecoderSignalBuffering( decoder_t
*, bool );
67 static void DecoderFlushBuffering( decoder_t
* );
69 static void DecoderUnsupportedCodec( decoder_t
*, vlc_fourcc_t
);
71 /* Buffers allocation callbacks for the decoders */
72 static block_t
*aout_new_buffer( decoder_t
*, int );
74 static picture_t
*vout_new_buffer( decoder_t
* );
75 static void vout_del_buffer( decoder_t
*, picture_t
* );
76 static void vout_link_picture( decoder_t
*, picture_t
* );
77 static void vout_unlink_picture( decoder_t
*, picture_t
* );
79 static subpicture_t
*spu_new_buffer( decoder_t
*, const subpicture_updater_t
* );
80 static void spu_del_buffer( decoder_t
*, subpicture_t
* );
82 struct decoder_owner_sys_t
84 int64_t i_preroll_end
;
86 input_thread_t
*p_input
;
87 input_resource_t
*p_resource
;
88 input_clock_t
*p_clock
;
91 vout_thread_t
*p_spu_vout
;
95 sout_instance_t
*p_sout
;
96 sout_packetizer_input_t
*p_sout_input
;
100 /* Some decoders require already packetized data (ie. not truncated) */
101 decoder_t
*p_packetizer
;
104 /* Current format in use by the output */
105 video_format_t video
;
106 audio_format_t audio
;
110 bool b_fmt_description
;
111 es_format_t fmt_description
;
112 vlc_meta_t
*p_description
;
115 block_fifo_t
*p_fifo
;
117 /* Lock for communication with decoder thread */
119 vlc_cond_t wait_request
;
120 vlc_cond_t wait_acknowledge
;
122 /* -- These variables need locking on write(only) -- */
123 audio_output_t
*p_aout
;
125 vout_thread_t
*p_vout
;
127 /* -- Theses variables need locking on read *and* write -- */
146 picture_t
*p_picture
;
147 picture_t
**pp_picture_next
;
149 subpicture_t
*p_subpic
;
150 subpicture_t
**pp_subpic_next
;
153 block_t
**pp_audio_next
;
156 block_t
**pp_block_next
;
167 decoder_t
*pp_decoder
[4];
174 #define DECODER_MAX_BUFFERING_COUNT (4)
175 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
176 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
178 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
179 * a bogus PTS and won't be displayed */
180 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
183 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
186 /*****************************************************************************
188 *****************************************************************************/
189 picture_t
*decoder_NewPicture( decoder_t
*p_decoder
)
191 picture_t
*p_picture
= p_decoder
->pf_vout_buffer_new( p_decoder
);
193 msg_Warn( p_decoder
, "can't get output picture" );
196 void decoder_DeletePicture( decoder_t
*p_decoder
, picture_t
*p_picture
)
198 p_decoder
->pf_vout_buffer_del( p_decoder
, p_picture
);
200 void decoder_LinkPicture( decoder_t
*p_decoder
, picture_t
*p_picture
)
202 p_decoder
->pf_picture_link( p_decoder
, p_picture
);
204 void decoder_UnlinkPicture( decoder_t
*p_decoder
, picture_t
*p_picture
)
206 p_decoder
->pf_picture_unlink( p_decoder
, p_picture
);
209 block_t
*decoder_NewAudioBuffer( decoder_t
*p_decoder
, int i_size
)
211 if( !p_decoder
->pf_aout_buffer_new
)
213 return p_decoder
->pf_aout_buffer_new( p_decoder
, i_size
);
216 subpicture_t
*decoder_NewSubpicture( decoder_t
*p_decoder
,
217 const subpicture_updater_t
*p_dyn
)
219 subpicture_t
*p_subpicture
= p_decoder
->pf_spu_buffer_new( p_decoder
, p_dyn
);
221 msg_Warn( p_decoder
, "can't get output subpicture" );
225 void decoder_DeleteSubpicture( decoder_t
*p_decoder
, subpicture_t
*p_subpicture
)
227 p_decoder
->pf_spu_buffer_del( p_decoder
, p_subpicture
);
230 /* decoder_GetInputAttachments:
232 int decoder_GetInputAttachments( decoder_t
*p_dec
,
233 input_attachment_t
***ppp_attachment
,
236 if( !p_dec
->pf_get_attachments
)
239 return p_dec
->pf_get_attachments( p_dec
, ppp_attachment
, pi_attachment
);
241 /* decoder_GetDisplayDate:
243 mtime_t
decoder_GetDisplayDate( decoder_t
*p_dec
, mtime_t i_ts
)
245 if( !p_dec
->pf_get_display_date
)
246 return VLC_TS_INVALID
;
248 return p_dec
->pf_get_display_date( p_dec
, i_ts
);
250 /* decoder_GetDisplayRate:
252 int decoder_GetDisplayRate( decoder_t
*p_dec
)
254 if( !p_dec
->pf_get_display_rate
)
255 return INPUT_RATE_DEFAULT
;
257 return p_dec
->pf_get_display_rate( p_dec
);
260 /* TODO: pass p_sout through p_resource? -- Courmisch */
261 static decoder_t
*decoder_New( vlc_object_t
*p_parent
, input_thread_t
*p_input
,
262 es_format_t
*fmt
, input_clock_t
*p_clock
,
263 input_resource_t
*p_resource
,
264 sout_instance_t
*p_sout
)
266 decoder_t
*p_dec
= NULL
;
267 const char *psz_type
= p_sout
? N_("packetizer") : N_("decoder");
270 /* Create the decoder configuration structure */
271 p_dec
= CreateDecoder( p_parent
, p_input
, fmt
,
272 p_sout
!= NULL
, p_resource
, p_sout
);
275 msg_Err( p_parent
, "could not create %s", psz_type
);
276 dialog_Fatal( p_parent
, _("Streaming / Transcoding failed"),
277 _("VLC could not open the %s module."),
278 vlc_gettext( psz_type
) );
282 if( !p_dec
->p_module
)
284 DecoderUnsupportedCodec( p_dec
, fmt
->i_codec
);
286 DeleteDecoder( p_dec
);
290 p_dec
->p_owner
->p_clock
= p_clock
;
291 assert( p_dec
->fmt_out
.i_cat
!= UNKNOWN_ES
);
293 if( p_dec
->fmt_out
.i_cat
== AUDIO_ES
)
294 i_priority
= VLC_THREAD_PRIORITY_AUDIO
;
296 i_priority
= VLC_THREAD_PRIORITY_VIDEO
;
298 /* Spawn the decoder thread */
299 if( vlc_clone( &p_dec
->p_owner
->thread
, DecoderThread
, p_dec
, i_priority
) )
301 msg_Err( p_dec
, "cannot spawn decoder thread" );
302 module_unneed( p_dec
, p_dec
->p_module
);
303 DeleteDecoder( p_dec
);
312 * Spawns a new decoder thread from the input thread
314 * \param p_input the input thread
315 * \param p_es the es descriptor
316 * \return the spawned decoder object
318 decoder_t
*input_DecoderNew( input_thread_t
*p_input
,
319 es_format_t
*fmt
, input_clock_t
*p_clock
,
320 sout_instance_t
*p_sout
)
322 return decoder_New( VLC_OBJECT(p_input
), p_input
, fmt
, p_clock
,
323 p_input
->p
->p_resource
, p_sout
);
327 * Spawn a decoder thread outside of the input thread.
329 decoder_t
*input_DecoderCreate( vlc_object_t
*p_parent
, es_format_t
*fmt
,
330 input_resource_t
*p_resource
)
332 return decoder_New( p_parent
, NULL
, fmt
, NULL
, p_resource
, NULL
);
337 * Kills a decoder thread and waits until it's finished
339 * \param p_input the input thread
340 * \param p_es the es descriptor
343 void input_DecoderDelete( decoder_t
*p_dec
)
345 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
347 vlc_cancel( p_owner
->thread
);
349 /* Make sure we aren't paused/buffering/waiting/decoding anymore */
350 vlc_mutex_lock( &p_owner
->lock
);
351 const bool b_was_paused
= p_owner
->b_paused
;
352 p_owner
->b_paused
= false;
353 p_owner
->b_buffering
= false;
354 p_owner
->b_flushing
= true;
355 p_owner
->b_exit
= true;
356 vlc_cond_signal( &p_owner
->wait_request
);
357 vlc_mutex_unlock( &p_owner
->lock
);
359 vlc_join( p_owner
->thread
, NULL
);
360 p_owner
->b_paused
= b_was_paused
;
362 module_unneed( p_dec
, p_dec
->p_module
);
365 if( p_dec
->p_owner
->cc
.b_supported
)
368 for( i
= 0; i
< 4; i
++ )
369 input_DecoderSetCcState( p_dec
, false, i
);
373 DeleteDecoder( p_dec
);
377 * Put a block_t in the decoder's fifo.
378 * Thread-safe w.r.t. the decoder. May be a cancellation point.
380 * \param p_dec the decoder object
381 * \param p_block the data block
383 void input_DecoderDecode( decoder_t
*p_dec
, block_t
*p_block
, bool b_do_pace
)
385 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
389 /* The fifo is not consummed when buffering and so will
391 * There is no need to lock as b_buffering is never modify
392 * inside decoder thread. */
393 if( !p_owner
->b_buffering
)
394 block_FifoPace( p_owner
->p_fifo
, 10, SIZE_MAX
);
397 else if( block_FifoSize( p_owner
->p_fifo
) > 50*1024*1024 /* 50 MiB */ )
399 else if( block_FifoSize( p_owner
->p_fifo
) > 400*1024*1024 /* 400 MiB, ie ~ 50mb/s for 60s */ )
402 /* FIXME: ideally we would check the time amount of data
403 * in the FIFO instead of its size. */
404 msg_Warn( p_dec
, "decoder/packetizer fifo full (data not "
405 "consumed quickly enough), resetting fifo!" );
406 block_FifoEmpty( p_owner
->p_fifo
);
409 block_FifoPut( p_owner
->p_fifo
, p_block
);
412 bool input_DecoderIsEmpty( decoder_t
* p_dec
)
414 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
415 assert( !p_owner
->b_buffering
);
417 bool b_empty
= block_FifoCount( p_dec
->p_owner
->p_fifo
) <= 0;
420 vlc_mutex_lock( &p_owner
->lock
);
421 /* TODO subtitles support */
422 if( p_dec
->fmt_out
.i_cat
== VIDEO_ES
&& p_owner
->p_vout
)
423 b_empty
= vout_IsEmpty( p_owner
->p_vout
);
424 else if( p_dec
->fmt_out
.i_cat
== AUDIO_ES
&& p_owner
->p_aout
)
425 b_empty
= aout_DecIsEmpty( p_owner
->p_aout
);
426 vlc_mutex_unlock( &p_owner
->lock
);
431 void input_DecoderIsCcPresent( decoder_t
*p_dec
, bool pb_present
[4] )
433 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
436 vlc_mutex_lock( &p_owner
->lock
);
437 for( i
= 0; i
< 4; i
++ )
438 pb_present
[i
] = p_owner
->cc
.pb_present
[i
];
439 vlc_mutex_unlock( &p_owner
->lock
);
441 int input_DecoderSetCcState( decoder_t
*p_dec
, bool b_decode
, int i_channel
)
443 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
445 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
447 if( i_channel
< 0 || i_channel
>= 4 || !p_owner
->cc
.pb_present
[i_channel
] )
452 static const vlc_fourcc_t fcc
[4] = {
453 VLC_FOURCC('c', 'c', '1', ' '),
454 VLC_FOURCC('c', 'c', '2', ' '),
455 VLC_FOURCC('c', 'c', '3', ' '),
456 VLC_FOURCC('c', 'c', '4', ' '),
461 es_format_Init( &fmt
, SPU_ES
, fcc
[i_channel
] );
462 p_cc
= CreateDecoder( VLC_OBJECT(p_dec
), p_owner
->p_input
, &fmt
,
463 false, p_owner
->p_resource
, p_owner
->p_sout
);
466 msg_Err( p_dec
, "could not create decoder" );
467 dialog_Fatal( p_dec
, _("Streaming / Transcoding failed"), "%s",
468 _("VLC could not open the decoder module.") );
471 else if( !p_cc
->p_module
)
473 DecoderUnsupportedCodec( p_dec
, fcc
[i_channel
] );
474 DeleteDecoder( p_cc
);
477 p_cc
->p_owner
->p_clock
= p_owner
->p_clock
;
479 vlc_mutex_lock( &p_owner
->lock
);
480 p_owner
->cc
.pp_decoder
[i_channel
] = p_cc
;
481 vlc_mutex_unlock( &p_owner
->lock
);
487 vlc_mutex_lock( &p_owner
->lock
);
488 p_cc
= p_owner
->cc
.pp_decoder
[i_channel
];
489 p_owner
->cc
.pp_decoder
[i_channel
] = NULL
;
490 vlc_mutex_unlock( &p_owner
->lock
);
494 module_unneed( p_cc
, p_cc
->p_module
);
495 DeleteDecoder( p_cc
);
500 int input_DecoderGetCcState( decoder_t
*p_dec
, bool *pb_decode
, int i_channel
)
502 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
505 if( i_channel
< 0 || i_channel
>= 4 || !p_owner
->cc
.pb_present
[i_channel
] )
508 vlc_mutex_lock( &p_owner
->lock
);
509 *pb_decode
= p_owner
->cc
.pp_decoder
[i_channel
] != NULL
;
510 vlc_mutex_unlock( &p_owner
->lock
);
514 void input_DecoderChangePause( decoder_t
*p_dec
, bool b_paused
, mtime_t i_date
)
516 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
518 vlc_mutex_lock( &p_owner
->lock
);
519 /* Normally, p_owner->b_paused != b_paused here. But if a track is added
520 * while the input is paused (e.g. add sub file), then b_paused is
521 * (incorrectly) false. */
522 if( likely(p_owner
->b_paused
!= b_paused
) ) {
523 p_owner
->b_paused
= b_paused
;
524 p_owner
->pause
.i_date
= i_date
;
525 p_owner
->pause
.i_ignore
= 0;
526 vlc_cond_signal( &p_owner
->wait_request
);
528 DecoderOutputChangePause( p_dec
, b_paused
, i_date
);
530 vlc_mutex_unlock( &p_owner
->lock
);
533 void input_DecoderChangeDelay( decoder_t
*p_dec
, mtime_t i_delay
)
535 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
537 vlc_mutex_lock( &p_owner
->lock
);
538 p_owner
->i_ts_delay
= i_delay
;
539 vlc_mutex_unlock( &p_owner
->lock
);
542 void input_DecoderStartBuffering( decoder_t
*p_dec
)
544 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
546 vlc_mutex_lock( &p_owner
->lock
);
548 DecoderFlush( p_dec
);
550 p_owner
->buffer
.b_first
= true;
551 p_owner
->buffer
.b_full
= false;
552 p_owner
->buffer
.i_count
= 0;
554 assert( !p_owner
->buffer
.p_picture
&& !p_owner
->buffer
.p_subpic
&&
555 !p_owner
->buffer
.p_audio
&& !p_owner
->buffer
.p_block
);
557 p_owner
->buffer
.p_picture
= NULL
;
558 p_owner
->buffer
.pp_picture_next
= &p_owner
->buffer
.p_picture
;
560 p_owner
->buffer
.p_subpic
= NULL
;
561 p_owner
->buffer
.pp_subpic_next
= &p_owner
->buffer
.p_subpic
;
563 p_owner
->buffer
.p_audio
= NULL
;
564 p_owner
->buffer
.pp_audio_next
= &p_owner
->buffer
.p_audio
;
566 p_owner
->buffer
.p_block
= NULL
;
567 p_owner
->buffer
.pp_block_next
= &p_owner
->buffer
.p_block
;
570 p_owner
->b_buffering
= true;
572 vlc_cond_signal( &p_owner
->wait_request
);
574 vlc_mutex_unlock( &p_owner
->lock
);
577 void input_DecoderStopBuffering( decoder_t
*p_dec
)
579 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
581 vlc_mutex_lock( &p_owner
->lock
);
583 p_owner
->b_buffering
= false;
585 vlc_cond_signal( &p_owner
->wait_request
);
587 vlc_mutex_unlock( &p_owner
->lock
);
590 void input_DecoderWaitBuffering( decoder_t
*p_dec
)
592 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
594 vlc_mutex_lock( &p_owner
->lock
);
596 while( p_owner
->b_buffering
&& !p_owner
->buffer
.b_full
)
598 block_FifoWake( p_owner
->p_fifo
);
599 vlc_cond_wait( &p_owner
->wait_acknowledge
, &p_owner
->lock
);
602 vlc_mutex_unlock( &p_owner
->lock
);
605 void input_DecoderFrameNext( decoder_t
*p_dec
, mtime_t
*pi_duration
)
607 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
611 vlc_mutex_lock( &p_owner
->lock
);
612 if( p_dec
->fmt_out
.i_cat
== VIDEO_ES
)
614 if( p_owner
->b_paused
&& p_owner
->p_vout
)
616 vout_NextPicture( p_owner
->p_vout
, pi_duration
);
617 p_owner
->pause
.i_ignore
++;
618 vlc_cond_signal( &p_owner
->wait_request
);
623 /* TODO subtitle should not be flushed */
624 DecoderFlush( p_dec
);
626 vlc_mutex_unlock( &p_owner
->lock
);
629 bool input_DecoderHasFormatChanged( decoder_t
*p_dec
, es_format_t
*p_fmt
, vlc_meta_t
**pp_meta
)
631 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
634 vlc_mutex_lock( &p_owner
->lock
);
635 b_changed
= p_owner
->b_fmt_description
;
639 es_format_Copy( p_fmt
, &p_owner
->fmt_description
);
644 if( p_owner
->p_description
)
646 *pp_meta
= vlc_meta_New();
648 vlc_meta_Merge( *pp_meta
, p_owner
->p_description
);
651 p_owner
->b_fmt_description
= false;
653 vlc_mutex_unlock( &p_owner
->lock
);
657 size_t input_DecoderGetFifoSize( decoder_t
*p_dec
)
659 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
661 return block_FifoSize( p_owner
->p_fifo
);
664 void input_DecoderGetObjects( decoder_t
*p_dec
,
665 vout_thread_t
**pp_vout
, audio_output_t
**pp_aout
)
667 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
669 vlc_mutex_lock( &p_owner
->lock
);
671 *pp_vout
= p_owner
->p_vout
? vlc_object_hold( p_owner
->p_vout
) : NULL
;
673 *pp_aout
= p_owner
->p_aout
? vlc_object_hold( p_owner
->p_aout
) : NULL
;
674 vlc_mutex_unlock( &p_owner
->lock
);
677 /*****************************************************************************
679 *****************************************************************************/
680 static int DecoderGetInputAttachments( decoder_t
*p_dec
,
681 input_attachment_t
***ppp_attachment
,
684 input_thread_t
*p_input
= p_dec
->p_owner
->p_input
;
686 if( unlikely(p_input
== NULL
) )
688 return input_Control( p_input
, INPUT_GET_ATTACHMENTS
,
689 ppp_attachment
, pi_attachment
);
691 static mtime_t
DecoderGetDisplayDate( decoder_t
*p_dec
, mtime_t i_ts
)
693 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
695 vlc_mutex_lock( &p_owner
->lock
);
696 if( p_owner
->b_buffering
|| p_owner
->b_paused
)
697 i_ts
= VLC_TS_INVALID
;
698 vlc_mutex_unlock( &p_owner
->lock
);
700 if( !p_owner
->p_clock
|| i_ts
<= VLC_TS_INVALID
)
703 if( input_clock_ConvertTS( p_owner
->p_clock
, NULL
, &i_ts
, NULL
, INT64_MAX
) )
704 return VLC_TS_INVALID
;
708 static int DecoderGetDisplayRate( decoder_t
*p_dec
)
710 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
712 if( !p_owner
->p_clock
)
713 return INPUT_RATE_DEFAULT
;
714 return input_clock_GetRate( p_owner
->p_clock
);
718 static void DecoderUnsupportedCodec( decoder_t
*p_dec
, vlc_fourcc_t codec
)
720 msg_Err( p_dec
, "no suitable decoder module for fourcc `%4.4s'. "
721 "VLC probably does not support this sound or video format.",
723 dialog_Fatal( p_dec
, _("No suitable decoder module"),
724 _("VLC does not support the audio or video format \"%4.4s\". "
725 "Unfortunately there is no way for you to fix this."),
731 * Create a decoder object
733 * \param p_input the input thread
734 * \param p_es the es descriptor
735 * \param b_packetizer instead of a decoder
736 * \return the decoder object
738 static decoder_t
* CreateDecoder( vlc_object_t
*p_parent
,
739 input_thread_t
*p_input
,
740 es_format_t
*fmt
, bool b_packetizer
,
741 input_resource_t
*p_resource
,
742 sout_instance_t
*p_sout
)
745 decoder_owner_sys_t
*p_owner
;
746 es_format_t null_es_format
;
748 p_dec
= vlc_custom_create( p_parent
, sizeof( *p_dec
), "decoder" );
752 p_dec
->pf_decode_audio
= NULL
;
753 p_dec
->pf_decode_video
= NULL
;
754 p_dec
->pf_decode_sub
= NULL
;
755 p_dec
->pf_get_cc
= NULL
;
756 p_dec
->pf_packetize
= NULL
;
758 /* Initialize the decoder */
759 p_dec
->p_module
= NULL
;
761 memset( &null_es_format
, 0, sizeof(es_format_t
) );
762 es_format_Copy( &p_dec
->fmt_in
, fmt
);
763 es_format_Copy( &p_dec
->fmt_out
, &null_es_format
);
765 p_dec
->p_description
= NULL
;
767 /* Allocate our private structure for the decoder */
768 p_dec
->p_owner
= p_owner
= malloc( sizeof( decoder_owner_sys_t
) );
769 if( unlikely(p_owner
== NULL
) )
771 vlc_object_release( p_dec
);
774 p_owner
->i_preroll_end
= VLC_TS_INVALID
;
775 p_owner
->i_last_rate
= INPUT_RATE_DEFAULT
;
776 p_owner
->p_input
= p_input
;
777 p_owner
->p_resource
= p_resource
;
778 p_owner
->p_aout
= NULL
;
779 p_owner
->p_vout
= NULL
;
780 p_owner
->p_spu_vout
= NULL
;
781 p_owner
->i_spu_channel
= 0;
782 p_owner
->i_spu_order
= 0;
783 p_owner
->p_sout
= p_sout
;
784 p_owner
->p_sout_input
= NULL
;
785 p_owner
->p_packetizer
= NULL
;
786 p_owner
->b_packetizer
= b_packetizer
;
789 p_owner
->p_fifo
= block_FifoNew();
790 if( unlikely(p_owner
->p_fifo
== NULL
) )
793 vlc_object_release( p_dec
);
797 /* Set buffers allocation callbacks for the decoders */
798 p_dec
->pf_aout_buffer_new
= aout_new_buffer
;
799 p_dec
->pf_vout_buffer_new
= vout_new_buffer
;
800 p_dec
->pf_vout_buffer_del
= vout_del_buffer
;
801 p_dec
->pf_picture_link
= vout_link_picture
;
802 p_dec
->pf_picture_unlink
= vout_unlink_picture
;
803 p_dec
->pf_spu_buffer_new
= spu_new_buffer
;
804 p_dec
->pf_spu_buffer_del
= spu_del_buffer
;
806 p_dec
->pf_get_attachments
= DecoderGetInputAttachments
;
807 p_dec
->pf_get_display_date
= DecoderGetDisplayDate
;
808 p_dec
->pf_get_display_rate
= DecoderGetDisplayRate
;
810 /* Find a suitable decoder/packetizer module */
812 p_dec
->p_module
= module_need( p_dec
, "decoder", "$codec", false );
814 p_dec
->p_module
= module_need( p_dec
, "packetizer", "$packetizer", false );
816 /* Check if decoder requires already packetized data */
818 p_dec
->b_need_packetized
&& !p_dec
->fmt_in
.b_packetized
)
820 p_owner
->p_packetizer
=
821 vlc_custom_create( p_parent
, sizeof( decoder_t
), "packetizer" );
822 if( p_owner
->p_packetizer
)
824 es_format_Copy( &p_owner
->p_packetizer
->fmt_in
,
827 es_format_Copy( &p_owner
->p_packetizer
->fmt_out
,
830 p_owner
->p_packetizer
->p_module
=
831 module_need( p_owner
->p_packetizer
,
832 "packetizer", "$packetizer", false );
834 if( !p_owner
->p_packetizer
->p_module
)
836 es_format_Clean( &p_owner
->p_packetizer
->fmt_in
);
837 vlc_object_release( p_owner
->p_packetizer
);
842 /* Copy ourself the input replay gain */
843 if( fmt
->i_cat
== AUDIO_ES
)
845 for( unsigned i
= 0; i
< AUDIO_REPLAY_GAIN_MAX
; i
++ )
847 if( !p_dec
->fmt_out
.audio_replay_gain
.pb_peak
[i
] )
849 p_dec
->fmt_out
.audio_replay_gain
.pb_peak
[i
] = fmt
->audio_replay_gain
.pb_peak
[i
];
850 p_dec
->fmt_out
.audio_replay_gain
.pf_peak
[i
] = fmt
->audio_replay_gain
.pf_peak
[i
];
852 if( !p_dec
->fmt_out
.audio_replay_gain
.pb_gain
[i
] )
854 p_dec
->fmt_out
.audio_replay_gain
.pb_gain
[i
] = fmt
->audio_replay_gain
.pb_gain
[i
];
855 p_dec
->fmt_out
.audio_replay_gain
.pf_gain
[i
] = fmt
->audio_replay_gain
.pf_gain
[i
];
861 vlc_mutex_init( &p_owner
->lock
);
862 vlc_cond_init( &p_owner
->wait_request
);
863 vlc_cond_init( &p_owner
->wait_acknowledge
);
865 p_owner
->b_fmt_description
= false;
866 es_format_Init( &p_owner
->fmt_description
, UNKNOWN_ES
, 0 );
867 p_owner
->p_description
= NULL
;
869 p_owner
->b_exit
= false;
871 p_owner
->b_paused
= false;
872 p_owner
->pause
.i_date
= VLC_TS_INVALID
;
873 p_owner
->pause
.i_ignore
= 0;
875 p_owner
->b_buffering
= false;
876 p_owner
->buffer
.b_first
= true;
877 p_owner
->buffer
.b_full
= false;
878 p_owner
->buffer
.i_count
= 0;
879 p_owner
->buffer
.p_picture
= NULL
;
880 p_owner
->buffer
.p_subpic
= NULL
;
881 p_owner
->buffer
.p_audio
= NULL
;
882 p_owner
->buffer
.p_block
= NULL
;
884 p_owner
->b_flushing
= false;
887 p_owner
->cc
.b_supported
= false;
890 if( p_owner
->p_packetizer
&& p_owner
->p_packetizer
->pf_get_cc
)
891 p_owner
->cc
.b_supported
= true;
892 if( p_dec
->pf_get_cc
)
893 p_owner
->cc
.b_supported
= true;
896 for( unsigned i
= 0; i
< 4; i
++ )
898 p_owner
->cc
.pb_present
[i
] = false;
899 p_owner
->cc
.pp_decoder
[i
] = NULL
;
901 p_owner
->i_ts_delay
= 0;
906 * The decoding main loop
908 * \param p_dec the decoder
910 static void *DecoderThread( void *p_data
)
912 decoder_t
*p_dec
= (decoder_t
*)p_data
;
913 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
915 /* The decoder's main loop */
918 block_t
*p_block
= block_FifoGet( p_owner
->p_fifo
);
920 /* Make sure there is no cancellation point other than this one^^.
921 * If you need one, be sure to push cleanup of p_block. */
922 DecoderSignalBuffering( p_dec
, p_block
== NULL
);
926 int canc
= vlc_savecancel();
928 if( p_block
->i_flags
& BLOCK_FLAG_CORE_EOS
)
930 /* calling DecoderProcess() with NULL block will make
931 * decoders/packetizers flush their buffers */
932 block_Release( p_block
);
937 DecoderError( p_dec
, p_block
);
939 DecoderProcess( p_dec
, p_block
);
941 vlc_restorecancel( canc
);
947 static block_t
*DecoderBlockFlushNew()
949 block_t
*p_null
= block_Alloc( 128 );
953 p_null
->i_flags
|= BLOCK_FLAG_DISCONTINUITY
|
954 BLOCK_FLAG_CORRUPTED
|
955 BLOCK_FLAG_CORE_FLUSH
;
956 memset( p_null
->p_buffer
, 0, p_null
->i_buffer
);
961 static void DecoderFlush( decoder_t
*p_dec
)
963 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
965 vlc_assert_locked( &p_owner
->lock
);
968 block_FifoEmpty( p_owner
->p_fifo
);
970 /* Monitor for flush end */
971 p_owner
->b_flushing
= true;
972 vlc_cond_signal( &p_owner
->wait_request
);
974 /* Send a special block */
975 block_t
*p_null
= DecoderBlockFlushNew();
978 input_DecoderDecode( p_dec
, p_null
, false );
981 while( p_owner
->b_flushing
)
982 vlc_cond_wait( &p_owner
->wait_acknowledge
, &p_owner
->lock
);
985 static void DecoderSignalBuffering( decoder_t
*p_dec
, bool b_full
)
987 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
989 vlc_mutex_lock( &p_owner
->lock
);
991 if( p_owner
->b_buffering
)
994 p_owner
->buffer
.b_full
= true;
995 vlc_cond_signal( &p_owner
->wait_acknowledge
);
998 vlc_mutex_unlock( &p_owner
->lock
);
1001 static bool DecoderIsFlushing( decoder_t
*p_dec
)
1003 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1006 vlc_mutex_lock( &p_owner
->lock
);
1008 b_flushing
= p_owner
->b_flushing
;
1010 vlc_mutex_unlock( &p_owner
->lock
);
1015 static void DecoderWaitUnblock( decoder_t
*p_dec
, bool *pb_reject
)
1017 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1019 vlc_assert_locked( &p_owner
->lock
);
1023 if( p_owner
->b_flushing
)
1025 if( p_owner
->b_paused
)
1027 if( p_owner
->b_buffering
&& !p_owner
->buffer
.b_full
)
1029 if( p_owner
->pause
.i_ignore
> 0 )
1031 p_owner
->pause
.i_ignore
--;
1037 if( !p_owner
->b_buffering
|| !p_owner
->buffer
.b_full
)
1040 vlc_cond_wait( &p_owner
->wait_request
, &p_owner
->lock
);
1044 *pb_reject
= p_owner
->b_flushing
;
1047 static void DecoderOutputChangePause( decoder_t
*p_dec
, bool b_paused
, mtime_t i_date
)
1049 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1051 vlc_assert_locked( &p_owner
->lock
);
1053 /* XXX only audio and video output have to be paused.
1054 * - for sout it is useless
1055 * - for subs, it is done by the vout
1057 if( p_dec
->fmt_out
.i_cat
== AUDIO_ES
)
1059 if( p_owner
->p_aout
)
1060 aout_DecChangePause( p_owner
->p_aout
, b_paused
, i_date
);
1062 else if( p_dec
->fmt_out
.i_cat
== VIDEO_ES
)
1064 if( p_owner
->p_vout
)
1065 vout_ChangePause( p_owner
->p_vout
, b_paused
, i_date
);
1068 static inline void DecoderUpdatePreroll( int64_t *pi_preroll
, const block_t
*p
)
1070 if( p
->i_flags
& (BLOCK_FLAG_PREROLL
|BLOCK_FLAG_DISCONTINUITY
) )
1071 *pi_preroll
= INT64_MAX
;
1072 else if( p
->i_dts
> VLC_TS_INVALID
)
1073 *pi_preroll
= __MIN( *pi_preroll
, p
->i_dts
);
1074 else if( p
->i_pts
> VLC_TS_INVALID
)
1075 *pi_preroll
= __MIN( *pi_preroll
, p
->i_pts
);
1078 static void DecoderFixTs( decoder_t
*p_dec
, mtime_t
*pi_ts0
, mtime_t
*pi_ts1
,
1079 mtime_t
*pi_duration
, int *pi_rate
, mtime_t i_ts_bound
)
1081 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1082 input_clock_t
*p_clock
= p_owner
->p_clock
;
1084 vlc_assert_locked( &p_owner
->lock
);
1086 const mtime_t i_es_delay
= p_owner
->i_ts_delay
;
1091 const bool b_ephemere
= pi_ts1
&& *pi_ts0
== *pi_ts1
;
1094 if( *pi_ts0
> VLC_TS_INVALID
)
1096 *pi_ts0
+= i_es_delay
;
1097 if( pi_ts1
&& *pi_ts1
> VLC_TS_INVALID
)
1098 *pi_ts1
+= i_es_delay
;
1099 if( input_clock_ConvertTS( p_clock
, &i_rate
, pi_ts0
, pi_ts1
, i_ts_bound
) )
1100 *pi_ts0
= VLC_TS_INVALID
;
1104 i_rate
= input_clock_GetRate( p_clock
);
1107 /* Do not create ephemere data because of rounding errors */
1108 if( !b_ephemere
&& pi_ts1
&& *pi_ts0
== *pi_ts1
)
1112 *pi_duration
= ( *pi_duration
* i_rate
+ INPUT_RATE_DEFAULT
-1 )
1113 / INPUT_RATE_DEFAULT
;
1119 static bool DecoderIsExitRequested( decoder_t
*p_dec
)
1121 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1123 vlc_mutex_lock( &p_owner
->lock
);
1124 bool b_exit
= p_owner
->b_exit
;
1125 vlc_mutex_unlock( &p_owner
->lock
);
1131 * If *pb_reject, it does nothing, otherwise it waits for the given
1132 * deadline or a flush request (in which case it set *pi_reject to true.
1134 static void DecoderWaitDate( decoder_t
*p_dec
,
1135 bool *pb_reject
, mtime_t i_deadline
)
1137 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1139 vlc_assert_locked( &p_owner
->lock
);
1141 if( *pb_reject
|| i_deadline
< 0 )
1146 if( p_owner
->b_flushing
|| p_owner
->b_exit
)
1152 while( vlc_cond_timedwait( &p_owner
->wait_request
, &p_owner
->lock
,
1153 i_deadline
) == 0 );
1156 static void DecoderPlayAudio( decoder_t
*p_dec
, block_t
*p_audio
,
1157 int *pi_played_sum
, int *pi_lost_sum
)
1159 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1160 audio_output_t
*p_aout
= p_owner
->p_aout
;
1163 if( p_audio
->i_pts
<= VLC_TS_INVALID
) // FIXME --VLC_TS_INVALID verify audio_output/*
1165 msg_Warn( p_dec
, "non-dated audio buffer received" );
1167 block_Release( p_audio
);
1172 vlc_mutex_lock( &p_owner
->lock
);
1174 if( p_owner
->b_buffering
|| p_owner
->buffer
.p_audio
)
1176 p_audio
->p_next
= NULL
;
1178 *p_owner
->buffer
.pp_audio_next
= p_audio
;
1179 p_owner
->buffer
.pp_audio_next
= &p_audio
->p_next
;
1181 p_owner
->buffer
.i_count
++;
1182 if( p_owner
->buffer
.i_count
> DECODER_MAX_BUFFERING_COUNT
||
1183 p_audio
->i_pts
- p_owner
->buffer
.p_audio
->i_pts
> DECODER_MAX_BUFFERING_AUDIO_DURATION
)
1185 p_owner
->buffer
.b_full
= true;
1186 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1192 bool b_has_more
= false, b_paused
, b_reject
;
1194 DecoderWaitUnblock( p_dec
, &b_reject
);
1195 if( p_owner
->b_buffering
)
1198 b_paused
= p_owner
->b_paused
;
1201 if( p_owner
->buffer
.p_audio
)
1203 p_audio
= p_owner
->buffer
.p_audio
;
1205 p_owner
->buffer
.p_audio
= p_audio
->p_next
;
1206 p_owner
->buffer
.i_count
--;
1208 b_has_more
= p_owner
->buffer
.p_audio
!= NULL
;
1210 p_owner
->buffer
.pp_audio_next
= &p_owner
->buffer
.p_audio
;
1214 int i_rate
= INPUT_RATE_DEFAULT
;
1216 DecoderFixTs( p_dec
, &p_audio
->i_pts
, NULL
, &p_audio
->i_length
,
1217 &i_rate
, AOUT_MAX_ADVANCE_TIME
);
1219 if( p_audio
->i_pts
<= VLC_TS_INVALID
1220 || i_rate
< INPUT_RATE_DEFAULT
/AOUT_MAX_INPUT_RATE
1221 || i_rate
> INPUT_RATE_DEFAULT
*AOUT_MAX_INPUT_RATE
)
1224 DecoderWaitDate( p_dec
, &b_reject
,
1225 p_audio
->i_pts
- AOUT_MAX_PREPARE_TIME
);
1227 if( unlikely(p_owner
->b_paused
!= b_paused
) )
1228 continue; /* race with input thread? retry... */
1229 if( p_aout
== NULL
)
1234 assert( !p_owner
->b_paused
);
1235 if( !aout_DecPlay( p_aout
, p_audio
, i_rate
) )
1236 *pi_played_sum
+= 1;
1237 *pi_lost_sum
+= aout_DecGetResetLost( p_aout
);
1241 msg_Dbg( p_dec
, "discarded audio buffer" );
1243 block_Release( p_audio
);
1248 if( !p_owner
->buffer
.p_audio
)
1251 vlc_mutex_unlock( &p_owner
->lock
);
1254 static void DecoderDecodeAudio( decoder_t
*p_dec
, block_t
*p_block
)
1256 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1257 block_t
*p_aout_buf
;
1262 while( (p_aout_buf
= p_dec
->pf_decode_audio( p_dec
, &p_block
)) )
1264 audio_output_t
*p_aout
= p_owner
->p_aout
;
1266 if( DecoderIsExitRequested( p_dec
) )
1268 /* It prevent freezing VLC in case of broken decoder */
1269 aout_DecDeleteBuffer( p_aout
, p_aout_buf
);
1271 block_Release( p_block
);
1276 if( p_owner
->i_preroll_end
> VLC_TS_INVALID
&&
1277 p_aout_buf
->i_pts
< p_owner
->i_preroll_end
)
1279 aout_DecDeleteBuffer( p_aout
, p_aout_buf
);
1283 if( p_owner
->i_preroll_end
> VLC_TS_INVALID
)
1285 msg_Dbg( p_dec
, "End of audio preroll" );
1286 if( p_owner
->p_aout
)
1287 aout_DecFlush( p_owner
->p_aout
);
1289 p_owner
->i_preroll_end
= VLC_TS_INVALID
;
1292 DecoderPlayAudio( p_dec
, p_aout_buf
, &i_played
, &i_lost
);
1295 /* Update ugly stat */
1296 input_thread_t
*p_input
= p_owner
->p_input
;
1298 if( p_input
!= NULL
&& (i_decoded
> 0 || i_lost
> 0 || i_played
> 0) )
1300 vlc_mutex_lock( &p_input
->p
->counters
.counters_lock
);
1301 stats_Update( p_input
->p
->counters
.p_lost_abuffers
, i_lost
, NULL
);
1302 stats_Update( p_input
->p
->counters
.p_played_abuffers
, i_played
, NULL
);
1303 stats_Update( p_input
->p
->counters
.p_decoded_audio
, i_decoded
, NULL
);
1304 vlc_mutex_unlock( &p_input
->p
->counters
.counters_lock
);
1307 static void DecoderGetCc( decoder_t
*p_dec
, decoder_t
*p_dec_cc
)
1309 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1312 bool b_processed
= false;
1316 assert( p_dec_cc
->pf_get_cc
!= NULL
);
1318 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1319 if( !p_owner
->cc
.b_supported
)
1322 p_cc
= p_dec_cc
->pf_get_cc( p_dec_cc
, pb_present
);
1326 vlc_mutex_lock( &p_owner
->lock
);
1327 for( i
= 0, i_cc_decoder
= 0; i
< 4; i
++ )
1329 p_owner
->cc
.pb_present
[i
] |= pb_present
[i
];
1330 if( p_owner
->cc
.pp_decoder
[i
] )
1334 for( i
= 0; i
< 4; i
++ )
1336 if( !p_owner
->cc
.pp_decoder
[i
] )
1339 if( i_cc_decoder
> 1 )
1340 DecoderProcess( p_owner
->cc
.pp_decoder
[i
], block_Duplicate( p_cc
) );
1342 DecoderProcess( p_owner
->cc
.pp_decoder
[i
], p_cc
);
1346 vlc_mutex_unlock( &p_owner
->lock
);
1349 block_Release( p_cc
);
1352 static void DecoderPlayVideo( decoder_t
*p_dec
, picture_t
*p_picture
,
1353 int *pi_played_sum
, int *pi_lost_sum
)
1355 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1356 vout_thread_t
*p_vout
= p_owner
->p_vout
;
1357 bool b_first_buffered
;
1359 if( p_picture
->date
<= VLC_TS_INVALID
)
1361 msg_Warn( p_dec
, "non-dated video buffer received" );
1363 vout_ReleasePicture( p_vout
, p_picture
);
1368 vlc_mutex_lock( &p_owner
->lock
);
1370 if( ( p_owner
->b_buffering
&& !p_owner
->buffer
.b_first
) || p_owner
->buffer
.p_picture
)
1372 p_picture
->p_next
= NULL
;
1374 *p_owner
->buffer
.pp_picture_next
= p_picture
;
1375 p_owner
->buffer
.pp_picture_next
= &p_picture
->p_next
;
1377 p_owner
->buffer
.i_count
++;
1378 if( p_owner
->buffer
.i_count
> DECODER_MAX_BUFFERING_COUNT
||
1379 p_picture
->date
- p_owner
->buffer
.p_picture
->date
> DECODER_MAX_BUFFERING_VIDEO_DURATION
)
1381 p_owner
->buffer
.b_full
= true;
1382 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1385 b_first_buffered
= p_owner
->buffer
.p_picture
!= NULL
;
1387 for( ;; b_first_buffered
= false )
1389 bool b_has_more
= false;
1393 DecoderWaitUnblock( p_dec
, &b_reject
);
1395 if( p_owner
->b_buffering
&& !p_owner
->buffer
.b_first
)
1397 vlc_mutex_unlock( &p_owner
->lock
);
1400 bool b_buffering_first
= p_owner
->b_buffering
;
1403 if( p_owner
->buffer
.p_picture
)
1405 p_picture
= p_owner
->buffer
.p_picture
;
1407 p_owner
->buffer
.p_picture
= p_picture
->p_next
;
1408 p_owner
->buffer
.i_count
--;
1410 b_has_more
= p_owner
->buffer
.p_picture
!= NULL
;
1412 p_owner
->buffer
.pp_picture_next
= &p_owner
->buffer
.p_picture
;
1416 if( b_buffering_first
)
1418 assert( p_owner
->buffer
.b_first
);
1419 assert( !p_owner
->buffer
.i_count
);
1420 msg_Dbg( p_dec
, "Received first picture" );
1421 p_owner
->buffer
.b_first
= false;
1422 p_picture
->b_force
= true;
1425 const bool b_dated
= p_picture
->date
> VLC_TS_INVALID
;
1426 int i_rate
= INPUT_RATE_DEFAULT
;
1427 DecoderFixTs( p_dec
, &p_picture
->date
, NULL
, NULL
,
1428 &i_rate
, DECODER_BOGUS_VIDEO_DELAY
);
1430 vlc_mutex_unlock( &p_owner
->lock
);
1433 if( !p_picture
->b_force
&& p_picture
->date
<= VLC_TS_INVALID
) // FIXME --VLC_TS_INVALID verify video_output/*
1438 if( i_rate
!= p_owner
->i_last_rate
|| b_first_buffered
)
1440 /* Be sure to not display old picture after our own */
1441 vout_Flush( p_vout
, p_picture
->date
);
1442 p_owner
->i_last_rate
= i_rate
;
1444 vout_PutPicture( p_vout
, p_picture
);
1449 msg_Warn( p_dec
, "early picture skipped" );
1451 msg_Warn( p_dec
, "non-dated video buffer received" );
1454 vout_ReleasePicture( p_vout
, p_picture
);
1458 vout_GetResetStatistic( p_vout
, &i_tmp_display
, &i_tmp_lost
);
1460 *pi_played_sum
+= i_tmp_display
;
1461 *pi_lost_sum
+= i_tmp_lost
;
1463 if( !b_has_more
|| b_buffering_first
)
1466 vlc_mutex_lock( &p_owner
->lock
);
1467 if( !p_owner
->buffer
.p_picture
)
1469 vlc_mutex_unlock( &p_owner
->lock
);
1475 static void DecoderDecodeVideo( decoder_t
*p_dec
, block_t
*p_block
)
1477 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1481 int i_displayed
= 0;
1483 while( (p_pic
= p_dec
->pf_decode_video( p_dec
, &p_block
)) )
1485 vout_thread_t
*p_vout
= p_owner
->p_vout
;
1486 if( DecoderIsExitRequested( p_dec
) )
1488 /* It prevent freezing VLC in case of broken decoder */
1489 vout_ReleasePicture( p_vout
, p_pic
);
1491 block_Release( p_block
);
1497 if( p_owner
->i_preroll_end
> VLC_TS_INVALID
&& p_pic
->date
< p_owner
->i_preroll_end
)
1499 vout_ReleasePicture( p_vout
, p_pic
);
1503 if( p_owner
->i_preroll_end
> VLC_TS_INVALID
)
1505 msg_Dbg( p_dec
, "End of video preroll" );
1507 vout_Flush( p_vout
, VLC_TS_INVALID
+1 );
1509 p_owner
->i_preroll_end
= VLC_TS_INVALID
;
1512 if( p_dec
->pf_get_cc
&&
1513 ( !p_owner
->p_packetizer
|| !p_owner
->p_packetizer
->pf_get_cc
) )
1514 DecoderGetCc( p_dec
, p_dec
);
1516 DecoderPlayVideo( p_dec
, p_pic
, &i_displayed
, &i_lost
);
1519 /* Update ugly stat */
1520 input_thread_t
*p_input
= p_owner
->p_input
;
1522 if( p_input
!= NULL
&& (i_decoded
> 0 || i_lost
> 0 || i_displayed
> 0) )
1524 vlc_mutex_lock( &p_input
->p
->counters
.counters_lock
);
1525 stats_Update( p_input
->p
->counters
.p_decoded_video
, i_decoded
, NULL
);
1526 stats_Update( p_input
->p
->counters
.p_lost_pictures
, i_lost
, NULL
);
1527 stats_Update( p_input
->p
->counters
.p_displayed_pictures
,
1529 vlc_mutex_unlock( &p_input
->p
->counters
.counters_lock
);
1533 static void DecoderPlaySpu( decoder_t
*p_dec
, subpicture_t
*p_subpic
)
1535 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1536 vout_thread_t
*p_vout
= p_owner
->p_spu_vout
;
1539 if( p_subpic
->i_start
<= VLC_TS_INVALID
)
1541 msg_Warn( p_dec
, "non-dated spu buffer received" );
1542 subpicture_Delete( p_subpic
);
1547 vlc_mutex_lock( &p_owner
->lock
);
1549 if( p_owner
->b_buffering
|| p_owner
->buffer
.p_subpic
)
1551 p_subpic
->p_next
= NULL
;
1553 *p_owner
->buffer
.pp_subpic_next
= p_subpic
;
1554 p_owner
->buffer
.pp_subpic_next
= &p_subpic
->p_next
;
1556 p_owner
->buffer
.i_count
++;
1557 /* XXX it is important to be full after the first one */
1558 if( p_owner
->buffer
.i_count
> 0 )
1560 p_owner
->buffer
.b_full
= true;
1561 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1567 bool b_has_more
= false;
1569 DecoderWaitUnblock( p_dec
, &b_reject
);
1571 if( p_owner
->b_buffering
)
1573 vlc_mutex_unlock( &p_owner
->lock
);
1578 if( p_owner
->buffer
.p_subpic
)
1580 p_subpic
= p_owner
->buffer
.p_subpic
;
1582 p_owner
->buffer
.p_subpic
= p_subpic
->p_next
;
1583 p_owner
->buffer
.i_count
--;
1585 b_has_more
= p_owner
->buffer
.p_subpic
!= NULL
;
1587 p_owner
->buffer
.pp_subpic_next
= &p_owner
->buffer
.p_subpic
;
1591 DecoderFixTs( p_dec
, &p_subpic
->i_start
, &p_subpic
->i_stop
, NULL
,
1594 if( p_subpic
->i_start
<= VLC_TS_INVALID
)
1597 DecoderWaitDate( p_dec
, &b_reject
,
1598 p_subpic
->i_start
- SPU_MAX_PREPARE_TIME
);
1599 vlc_mutex_unlock( &p_owner
->lock
);
1602 vout_PutSubpicture( p_vout
, p_subpic
);
1604 subpicture_Delete( p_subpic
);
1608 vlc_mutex_lock( &p_owner
->lock
);
1609 if( !p_owner
->buffer
.p_subpic
)
1611 vlc_mutex_unlock( &p_owner
->lock
);
1618 static void DecoderPlaySout( decoder_t
*p_dec
, block_t
*p_sout_block
)
1620 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1622 assert( p_owner
->p_clock
);
1623 assert( !p_sout_block
->p_next
);
1625 vlc_mutex_lock( &p_owner
->lock
);
1627 if( p_owner
->b_buffering
|| p_owner
->buffer
.p_block
)
1629 block_ChainLastAppend( &p_owner
->buffer
.pp_block_next
, p_sout_block
);
1631 p_owner
->buffer
.i_count
++;
1632 /* XXX it is important to be full after the first one */
1633 if( p_owner
->buffer
.i_count
> 0 )
1635 p_owner
->buffer
.b_full
= true;
1636 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1642 bool b_has_more
= false;
1644 DecoderWaitUnblock( p_dec
, &b_reject
);
1646 if( p_owner
->b_buffering
)
1648 vlc_mutex_unlock( &p_owner
->lock
);
1653 if( p_owner
->buffer
.p_block
)
1655 p_sout_block
= p_owner
->buffer
.p_block
;
1657 p_owner
->buffer
.p_block
= p_sout_block
->p_next
;
1658 p_owner
->buffer
.i_count
--;
1660 b_has_more
= p_owner
->buffer
.p_block
!= NULL
;
1662 p_owner
->buffer
.pp_block_next
= &p_owner
->buffer
.p_block
;
1664 p_sout_block
->p_next
= NULL
;
1666 DecoderFixTs( p_dec
, &p_sout_block
->i_dts
, &p_sout_block
->i_pts
,
1667 &p_sout_block
->i_length
, NULL
, INT64_MAX
);
1669 vlc_mutex_unlock( &p_owner
->lock
);
1672 sout_InputSendBuffer( p_owner
->p_sout_input
, p_sout_block
); // FIXME --VLC_TS_INVALID inspect stream_output/*
1674 block_Release( p_sout_block
);
1678 vlc_mutex_lock( &p_owner
->lock
);
1679 if( !p_owner
->buffer
.p_block
)
1681 vlc_mutex_unlock( &p_owner
->lock
);
1689 static void DecoderFlushBuffering( decoder_t
*p_dec
)
1691 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1693 vlc_assert_locked( &p_owner
->lock
);
1695 while( p_owner
->buffer
.p_picture
)
1697 picture_t
*p_picture
= p_owner
->buffer
.p_picture
;
1699 p_owner
->buffer
.p_picture
= p_picture
->p_next
;
1700 p_owner
->buffer
.i_count
--;
1702 if( p_owner
->p_vout
)
1704 vout_ReleasePicture( p_owner
->p_vout
, p_picture
);
1707 if( !p_owner
->buffer
.p_picture
)
1708 p_owner
->buffer
.pp_picture_next
= &p_owner
->buffer
.p_picture
;
1710 while( p_owner
->buffer
.p_audio
)
1712 block_t
*p_audio
= p_owner
->buffer
.p_audio
;
1714 p_owner
->buffer
.p_audio
= p_audio
->p_next
;
1715 p_owner
->buffer
.i_count
--;
1717 block_Release( p_audio
);
1719 if( !p_owner
->buffer
.p_audio
)
1720 p_owner
->buffer
.pp_audio_next
= &p_owner
->buffer
.p_audio
;
1722 while( p_owner
->buffer
.p_subpic
)
1724 subpicture_t
*p_subpic
= p_owner
->buffer
.p_subpic
;
1726 p_owner
->buffer
.p_subpic
= p_subpic
->p_next
;
1727 p_owner
->buffer
.i_count
--;
1729 subpicture_Delete( p_subpic
);
1731 if( !p_owner
->buffer
.p_subpic
)
1732 p_owner
->buffer
.pp_subpic_next
= &p_owner
->buffer
.p_subpic
;
1734 if( p_owner
->buffer
.p_block
)
1736 block_ChainRelease( p_owner
->buffer
.p_block
);
1738 p_owner
->buffer
.i_count
= 0;
1739 p_owner
->buffer
.p_block
= NULL
;
1740 p_owner
->buffer
.pp_block_next
= &p_owner
->buffer
.p_block
;
1745 /* This function process a block for sout
1747 static void DecoderProcessSout( decoder_t
*p_dec
, block_t
*p_block
)
1749 decoder_owner_sys_t
*p_owner
= (decoder_owner_sys_t
*)p_dec
->p_owner
;
1750 block_t
*p_sout_block
;
1752 while( ( p_sout_block
=
1753 p_dec
->pf_packetize( p_dec
, p_block
? &p_block
: NULL
) ) )
1755 if( !p_owner
->p_sout_input
)
1757 es_format_Copy( &p_owner
->sout
, &p_dec
->fmt_out
);
1759 p_owner
->sout
.i_group
= p_dec
->fmt_in
.i_group
;
1760 p_owner
->sout
.i_id
= p_dec
->fmt_in
.i_id
;
1761 if( p_dec
->fmt_in
.psz_language
)
1763 free( p_owner
->sout
.psz_language
);
1764 p_owner
->sout
.psz_language
=
1765 strdup( p_dec
->fmt_in
.psz_language
);
1768 p_owner
->p_sout_input
=
1769 sout_InputNew( p_owner
->p_sout
,
1772 if( p_owner
->p_sout_input
== NULL
)
1774 msg_Err( p_dec
, "cannot create packetizer output (%4.4s)",
1775 (char *)&p_owner
->sout
.i_codec
);
1776 p_dec
->b_error
= true;
1778 while( p_sout_block
)
1780 block_t
*p_next
= p_sout_block
->p_next
;
1781 block_Release( p_sout_block
);
1782 p_sout_block
= p_next
;
1788 while( p_sout_block
)
1790 block_t
*p_next
= p_sout_block
->p_next
;
1792 p_sout_block
->p_next
= NULL
;
1794 DecoderPlaySout( p_dec
, p_sout_block
);
1796 p_sout_block
= p_next
;
1802 /* This function process a video block
1804 static void DecoderProcessVideo( decoder_t
*p_dec
, block_t
*p_block
, bool b_flush
)
1806 decoder_owner_sys_t
*p_owner
= (decoder_owner_sys_t
*)p_dec
->p_owner
;
1808 if( p_owner
->p_packetizer
)
1810 block_t
*p_packetized_block
;
1811 decoder_t
*p_packetizer
= p_owner
->p_packetizer
;
1813 while( (p_packetized_block
=
1814 p_packetizer
->pf_packetize( p_packetizer
, p_block
? &p_block
: NULL
)) )
1816 if( p_packetizer
->fmt_out
.i_extra
&& !p_dec
->fmt_in
.i_extra
)
1818 es_format_Clean( &p_dec
->fmt_in
);
1819 es_format_Copy( &p_dec
->fmt_in
, &p_packetizer
->fmt_out
);
1821 if( p_packetizer
->pf_get_cc
)
1822 DecoderGetCc( p_dec
, p_packetizer
);
1824 while( p_packetized_block
)
1826 block_t
*p_next
= p_packetized_block
->p_next
;
1827 p_packetized_block
->p_next
= NULL
;
1829 DecoderDecodeVideo( p_dec
, p_packetized_block
);
1831 p_packetized_block
= p_next
;
1834 /* The packetizer does not output a block that tell the decoder to flush
1838 block_t
*p_null
= DecoderBlockFlushNew();
1840 DecoderDecodeVideo( p_dec
, p_null
);
1845 DecoderDecodeVideo( p_dec
, p_block
);
1848 if( b_flush
&& p_owner
->p_vout
)
1849 vout_Flush( p_owner
->p_vout
, VLC_TS_INVALID
+1 );
1852 /* This function process a audio block
1854 static void DecoderProcessAudio( decoder_t
*p_dec
, block_t
*p_block
, bool b_flush
)
1856 decoder_owner_sys_t
*p_owner
= (decoder_owner_sys_t
*)p_dec
->p_owner
;
1858 if( p_owner
->p_packetizer
)
1860 block_t
*p_packetized_block
;
1861 decoder_t
*p_packetizer
= p_owner
->p_packetizer
;
1863 while( (p_packetized_block
=
1864 p_packetizer
->pf_packetize( p_packetizer
, p_block
? &p_block
: NULL
)) )
1866 if( p_packetizer
->fmt_out
.i_extra
&& !p_dec
->fmt_in
.i_extra
)
1868 es_format_Clean( &p_dec
->fmt_in
);
1869 es_format_Copy( &p_dec
->fmt_in
, &p_packetizer
->fmt_out
);
1872 while( p_packetized_block
)
1874 block_t
*p_next
= p_packetized_block
->p_next
;
1875 p_packetized_block
->p_next
= NULL
;
1877 DecoderDecodeAudio( p_dec
, p_packetized_block
);
1879 p_packetized_block
= p_next
;
1882 /* The packetizer does not output a block that tell the decoder to flush
1886 block_t
*p_null
= DecoderBlockFlushNew();
1888 DecoderDecodeAudio( p_dec
, p_null
);
1893 DecoderDecodeAudio( p_dec
, p_block
);
1896 if( b_flush
&& p_owner
->p_aout
)
1897 aout_DecFlush( p_owner
->p_aout
);
1900 /* This function process a subtitle block
1902 static void DecoderProcessSpu( decoder_t
*p_dec
, block_t
*p_block
, bool b_flush
)
1904 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1906 input_thread_t
*p_input
= p_owner
->p_input
;
1907 vout_thread_t
*p_vout
;
1908 subpicture_t
*p_spu
;
1910 while( (p_spu
= p_dec
->pf_decode_sub( p_dec
, p_block
? &p_block
: NULL
) ) )
1912 if( p_input
!= NULL
)
1914 vlc_mutex_lock( &p_input
->p
->counters
.counters_lock
);
1915 stats_Update( p_input
->p
->counters
.p_decoded_sub
, 1, NULL
);
1916 vlc_mutex_unlock( &p_input
->p
->counters
.counters_lock
);
1919 p_vout
= input_resource_HoldVout( p_owner
->p_resource
);
1920 if( p_vout
&& p_owner
->p_spu_vout
== p_vout
)
1922 /* Preroll does not work very well with subtitle */
1923 if( p_spu
->i_start
> VLC_TS_INVALID
&&
1924 p_spu
->i_start
< p_owner
->i_preroll_end
&&
1925 ( p_spu
->i_stop
<= VLC_TS_INVALID
|| p_spu
->i_stop
< p_owner
->i_preroll_end
) )
1927 subpicture_Delete( p_spu
);
1931 DecoderPlaySpu( p_dec
, p_spu
);
1936 subpicture_Delete( p_spu
);
1939 vlc_object_release( p_vout
);
1942 if( b_flush
&& p_owner
->p_spu_vout
)
1944 p_vout
= input_resource_HoldVout( p_owner
->p_resource
);
1946 if( p_vout
&& p_owner
->p_spu_vout
== p_vout
)
1947 vout_FlushSubpictureChannel( p_vout
, p_owner
->i_spu_channel
);
1950 vlc_object_release( p_vout
);
1955 static void DecoderProcessOnFlush( decoder_t
*p_dec
)
1957 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1959 vlc_mutex_lock( &p_owner
->lock
);
1960 DecoderFlushBuffering( p_dec
);
1962 if( p_owner
->b_flushing
)
1964 p_owner
->b_flushing
= false;
1965 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1967 vlc_mutex_unlock( &p_owner
->lock
);
1973 * \param p_dec the decoder object
1974 * \param p_block the block to decode
1975 * \return VLC_SUCCESS or an error code
1977 static void DecoderProcess( decoder_t
*p_dec
, block_t
*p_block
)
1979 decoder_owner_sys_t
*p_owner
= (decoder_owner_sys_t
*)p_dec
->p_owner
;
1980 const bool b_flush_request
= p_block
&& (p_block
->i_flags
& BLOCK_FLAG_CORE_FLUSH
);
1982 if( p_block
&& p_block
->i_buffer
<= 0 )
1984 assert( !b_flush_request
);
1985 block_Release( p_block
);
1990 if( p_owner
->b_packetizer
)
1993 p_block
->i_flags
&= ~BLOCK_FLAG_CORE_PRIVATE_MASK
;
1995 DecoderProcessSout( p_dec
, p_block
);
2000 bool b_flush
= false;
2004 const bool b_flushing
= p_owner
->i_preroll_end
== INT64_MAX
;
2005 DecoderUpdatePreroll( &p_owner
->i_preroll_end
, p_block
);
2007 b_flush
= !b_flushing
&& b_flush_request
;
2009 p_block
->i_flags
&= ~BLOCK_FLAG_CORE_PRIVATE_MASK
;
2012 if( p_dec
->fmt_out
.i_cat
== AUDIO_ES
)
2014 DecoderProcessAudio( p_dec
, p_block
, b_flush
);
2016 else if( p_dec
->fmt_out
.i_cat
== VIDEO_ES
)
2018 DecoderProcessVideo( p_dec
, p_block
, b_flush
);
2020 else if( p_dec
->fmt_out
.i_cat
== SPU_ES
)
2022 DecoderProcessSpu( p_dec
, p_block
, b_flush
);
2026 msg_Err( p_dec
, "unknown ES format" );
2027 p_dec
->b_error
= true;
2032 if( b_flush_request
)
2033 DecoderProcessOnFlush( p_dec
);
2036 static void DecoderError( decoder_t
*p_dec
, block_t
*p_block
)
2038 const bool b_flush_request
= p_block
&& (p_block
->i_flags
& BLOCK_FLAG_CORE_FLUSH
);
2042 block_Release( p_block
);
2044 if( b_flush_request
)
2045 DecoderProcessOnFlush( p_dec
);
2050 * Destroys a decoder object
2052 * \param p_dec the decoder object
2055 static void DeleteDecoder( decoder_t
* p_dec
)
2057 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
2059 msg_Dbg( p_dec
, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2060 (char*)&p_dec
->fmt_in
.i_codec
,
2061 (unsigned)block_FifoCount( p_owner
->p_fifo
) );
2063 /* Free all packets still in the decoder fifo. */
2064 block_FifoEmpty( p_owner
->p_fifo
);
2065 block_FifoRelease( p_owner
->p_fifo
);
2068 vlc_mutex_lock( &p_owner
->lock
);
2069 DecoderFlushBuffering( p_dec
);
2070 vlc_mutex_unlock( &p_owner
->lock
);
2073 if( p_owner
->p_aout
)
2075 /* TODO: REVISIT gap-less audio */
2076 aout_DecFlush( p_owner
->p_aout
);
2077 aout_DecDelete( p_owner
->p_aout
);
2078 input_resource_PutAout( p_owner
->p_resource
, p_owner
->p_aout
);
2079 if( p_owner
->p_input
!= NULL
)
2080 input_SendEventAout( p_owner
->p_input
);
2082 if( p_owner
->p_vout
)
2084 /* Hack to make sure all the the pictures are freed by the decoder
2085 * and that the vout is not paused anymore */
2086 vout_Reset( p_owner
->p_vout
);
2089 input_resource_RequestVout( p_owner
->p_resource
, p_owner
->p_vout
, NULL
,
2091 if( p_owner
->p_input
!= NULL
)
2092 input_SendEventVout( p_owner
->p_input
);
2096 if( p_owner
->p_sout_input
)
2098 sout_InputDelete( p_owner
->p_sout_input
);
2099 es_format_Clean( &p_owner
->sout
);
2103 if( p_dec
->fmt_out
.i_cat
== SPU_ES
)
2105 vout_thread_t
*p_vout
;
2107 p_vout
= input_resource_HoldVout( p_owner
->p_resource
);
2110 if( p_owner
->p_spu_vout
== p_vout
)
2111 vout_FlushSubpictureChannel( p_vout
, p_owner
->i_spu_channel
);
2112 vlc_object_release( p_vout
);
2116 es_format_Clean( &p_dec
->fmt_in
);
2117 es_format_Clean( &p_dec
->fmt_out
);
2118 if( p_dec
->p_description
)
2119 vlc_meta_Delete( p_dec
->p_description
);
2120 es_format_Clean( &p_owner
->fmt_description
);
2121 if( p_owner
->p_description
)
2122 vlc_meta_Delete( p_owner
->p_description
);
2124 if( p_owner
->p_packetizer
)
2126 module_unneed( p_owner
->p_packetizer
,
2127 p_owner
->p_packetizer
->p_module
);
2128 es_format_Clean( &p_owner
->p_packetizer
->fmt_in
);
2129 es_format_Clean( &p_owner
->p_packetizer
->fmt_out
);
2130 if( p_owner
->p_packetizer
->p_description
)
2131 vlc_meta_Delete( p_owner
->p_packetizer
->p_description
);
2132 vlc_object_release( p_owner
->p_packetizer
);
2135 vlc_cond_destroy( &p_owner
->wait_acknowledge
);
2136 vlc_cond_destroy( &p_owner
->wait_request
);
2137 vlc_mutex_destroy( &p_owner
->lock
);
2139 vlc_object_release( p_dec
);
2144 /*****************************************************************************
2145 * Buffers allocation callbacks for the decoders
2146 *****************************************************************************/
2147 static void DecoderUpdateFormatLocked( decoder_t
*p_dec
)
2149 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
2151 vlc_assert_locked( &p_owner
->lock
);
2153 p_owner
->b_fmt_description
= true;
2155 /* Copy es_format */
2156 es_format_Clean( &p_owner
->fmt_description
);
2157 es_format_Copy( &p_owner
->fmt_description
, &p_dec
->fmt_out
);
2159 /* Move p_description */
2160 if( p_owner
->p_description
&& p_dec
->p_description
)
2161 vlc_meta_Delete( p_owner
->p_description
);
2162 p_owner
->p_description
= p_dec
->p_description
;
2163 p_dec
->p_description
= NULL
;
2165 static vout_thread_t
*aout_request_vout( void *p_private
,
2166 vout_thread_t
*p_vout
, video_format_t
*p_fmt
, bool b_recyle
)
2168 decoder_t
*p_dec
= p_private
;
2169 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
2170 input_thread_t
*p_input
= p_owner
->p_input
;
2172 p_vout
= input_resource_RequestVout( p_owner
->p_resource
, p_vout
, p_fmt
, 1,
2174 if( p_input
!= NULL
)
2175 input_SendEventVout( p_input
);
2180 static block_t
*aout_new_buffer( decoder_t
*p_dec
, int i_samples
)
2182 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
2186 && !AOUT_FMTS_IDENTICAL(&p_dec
->fmt_out
.audio
, &p_owner
->audio
) )
2188 audio_output_t
*p_aout
= p_owner
->p_aout
;
2190 /* Parameters changed, restart the aout */
2191 vlc_mutex_lock( &p_owner
->lock
);
2193 DecoderFlushBuffering( p_dec
);
2195 aout_DecDelete( p_owner
->p_aout
);
2196 p_owner
->p_aout
= NULL
;
2198 vlc_mutex_unlock( &p_owner
->lock
);
2199 input_resource_PutAout( p_owner
->p_resource
, p_aout
);
2202 if( p_owner
->p_aout
== NULL
)
2204 const int i_force_dolby
= var_InheritInteger( p_dec
, "force-dolby-surround" );
2205 audio_sample_format_t format
;
2206 audio_output_t
*p_aout
;
2207 aout_request_vout_t request_vout
;
2209 p_dec
->fmt_out
.audio
.i_format
= p_dec
->fmt_out
.i_codec
;
2210 p_owner
->audio
= p_dec
->fmt_out
.audio
;
2212 memcpy( &format
, &p_owner
->audio
, sizeof( audio_sample_format_t
) );
2213 if( i_force_dolby
&&
2214 (format
.i_original_channels
&AOUT_CHAN_PHYSMASK
) ==
2215 (AOUT_CHAN_LEFT
|AOUT_CHAN_RIGHT
) )
2217 if( i_force_dolby
== 1 )
2219 format
.i_original_channels
= format
.i_original_channels
|
2220 AOUT_CHAN_DOLBYSTEREO
;
2222 else /* i_force_dolby == 2 */
2224 format
.i_original_channels
= format
.i_original_channels
&
2225 ~AOUT_CHAN_DOLBYSTEREO
;
2229 request_vout
.pf_request_vout
= aout_request_vout
;
2230 request_vout
.p_private
= p_dec
;
2232 assert( p_owner
->p_aout
== NULL
);
2233 p_aout
= input_resource_GetAout( p_owner
->p_resource
);
2236 aout_FormatPrepare( &format
);
2237 if( aout_DecNew( p_aout
, &format
,
2238 &p_dec
->fmt_out
.audio_replay_gain
,
2241 input_resource_PutAout( p_owner
->p_resource
, p_aout
);
2246 vlc_mutex_lock( &p_owner
->lock
);
2248 p_owner
->p_aout
= p_aout
;
2249 DecoderUpdateFormatLocked( p_dec
);
2250 if( unlikely(p_owner
->b_paused
) ) /* fake pause if needed */
2251 aout_DecChangePause( p_aout
, true, mdate() );
2253 vlc_mutex_unlock( &p_owner
->lock
);
2255 if( p_owner
->p_input
!= NULL
)
2256 input_SendEventAout( p_owner
->p_input
);
2258 if( p_aout
== NULL
)
2260 msg_Err( p_dec
, "failed to create audio output" );
2261 p_dec
->b_error
= true;
2264 p_dec
->fmt_out
.audio
.i_bytes_per_frame
=
2265 p_owner
->audio
.i_bytes_per_frame
;
2268 p_buffer
= aout_DecNewBuffer( p_owner
->p_aout
, i_samples
);
2273 static picture_t
*vout_new_buffer( decoder_t
*p_dec
)
2275 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
2277 if( p_owner
->p_vout
== NULL
||
2278 p_dec
->fmt_out
.video
.i_width
!= p_owner
->video
.i_width
||
2279 p_dec
->fmt_out
.video
.i_height
!= p_owner
->video
.i_height
||
2280 p_dec
->fmt_out
.video
.i_visible_width
!= p_owner
->video
.i_visible_width
||
2281 p_dec
->fmt_out
.video
.i_visible_height
!= p_owner
->video
.i_visible_height
||
2282 p_dec
->fmt_out
.video
.i_x_offset
!= p_owner
->video
.i_x_offset
||
2283 p_dec
->fmt_out
.video
.i_y_offset
!= p_owner
->video
.i_y_offset
||
2284 p_dec
->fmt_out
.i_codec
!= p_owner
->video
.i_chroma
||
2285 (int64_t)p_dec
->fmt_out
.video
.i_sar_num
* p_owner
->video
.i_sar_den
!=
2286 (int64_t)p_dec
->fmt_out
.video
.i_sar_den
* p_owner
->video
.i_sar_num
)
2288 vout_thread_t
*p_vout
;
2290 if( !p_dec
->fmt_out
.video
.i_width
||
2291 !p_dec
->fmt_out
.video
.i_height
)
2293 /* Can't create a new vout without display size */
2297 video_format_t fmt
= p_dec
->fmt_out
.video
;
2298 fmt
.i_chroma
= p_dec
->fmt_out
.i_codec
;
2299 p_owner
->video
= fmt
;
2301 if( vlc_fourcc_IsYUV( fmt
.i_chroma
) )
2303 const vlc_chroma_description_t
*dsc
= vlc_fourcc_GetChromaDescription( fmt
.i_chroma
);
2304 for( unsigned int i
= 0; dsc
&& i
< dsc
->plane_count
; i
++ )
2306 while( fmt
.i_width
% dsc
->p
[i
].w
.den
)
2308 while( fmt
.i_height
% dsc
->p
[i
].h
.den
)
2313 if( !fmt
.i_visible_width
|| !fmt
.i_visible_height
)
2315 if( p_dec
->fmt_in
.video
.i_visible_width
&&
2316 p_dec
->fmt_in
.video
.i_visible_height
)
2318 fmt
.i_visible_width
= p_dec
->fmt_in
.video
.i_visible_width
;
2319 fmt
.i_visible_height
= p_dec
->fmt_in
.video
.i_visible_height
;
2320 fmt
.i_x_offset
= p_dec
->fmt_in
.video
.i_x_offset
;
2321 fmt
.i_y_offset
= p_dec
->fmt_in
.video
.i_y_offset
;
2325 fmt
.i_visible_width
= fmt
.i_width
;
2326 fmt
.i_visible_height
= fmt
.i_height
;
2332 if( fmt
.i_visible_height
== 1088 &&
2333 var_CreateGetBool( p_dec
, "hdtv-fix" ) )
2335 fmt
.i_visible_height
= 1080;
2336 if( !(fmt
.i_sar_num
% 136))
2338 fmt
.i_sar_num
*= 135;
2339 fmt
.i_sar_den
*= 136;
2341 msg_Warn( p_dec
, "Fixing broken HDTV stream (display_height=1088)");
2344 if( !fmt
.i_sar_num
|| !fmt
.i_sar_den
)
2350 vlc_ureduce( &fmt
.i_sar_num
, &fmt
.i_sar_den
,
2351 fmt
.i_sar_num
, fmt
.i_sar_den
, 50000 );
2353 vlc_mutex_lock( &p_owner
->lock
);
2355 DecoderFlushBuffering( p_dec
);
2357 p_vout
= p_owner
->p_vout
;
2358 p_owner
->p_vout
= NULL
;
2359 vlc_mutex_unlock( &p_owner
->lock
);
2362 switch( p_dec
->fmt_in
.i_codec
)
2364 case VLC_CODEC_H264
:
2365 case VLC_CODEC_DIRAC
: /* FIXME valid ? */
2370 case VLC_CODEC_VP6F
:
2378 p_vout
= input_resource_RequestVout( p_owner
->p_resource
,
2381 p_dec
->i_extra_picture_buffers
+
2382 1 + DECODER_MAX_BUFFERING_COUNT
,
2384 vlc_mutex_lock( &p_owner
->lock
);
2385 p_owner
->p_vout
= p_vout
;
2387 DecoderUpdateFormatLocked( p_dec
);
2389 vlc_mutex_unlock( &p_owner
->lock
);
2391 if( p_owner
->p_input
!= NULL
)
2392 input_SendEventVout( p_owner
->p_input
);
2393 if( p_vout
== NULL
)
2395 msg_Err( p_dec
, "failed to create video output" );
2396 p_dec
->b_error
= true;
2401 /* Get a new picture
2405 if( DecoderIsExitRequested( p_dec
) || p_dec
->b_error
)
2408 picture_t
*p_picture
= vout_GetPicture( p_owner
->p_vout
);
2412 if( DecoderIsFlushing( p_dec
) )
2416 DecoderSignalBuffering( p_dec
, true );
2418 /* Check the decoder doesn't leak pictures */
2419 vout_FixLeaks( p_owner
->p_vout
);
2421 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2422 msleep( VOUT_OUTMEM_SLEEP
);
2426 static void vout_del_buffer( decoder_t
*p_dec
, picture_t
*p_pic
)
2428 vout_ReleasePicture( p_dec
->p_owner
->p_vout
, p_pic
);
2431 static void vout_link_picture( decoder_t
*p_dec
, picture_t
*p_pic
)
2433 vout_HoldPicture( p_dec
->p_owner
->p_vout
, p_pic
);
2436 static void vout_unlink_picture( decoder_t
*p_dec
, picture_t
*p_pic
)
2438 vout_ReleasePicture( p_dec
->p_owner
->p_vout
, p_pic
);
2441 static subpicture_t
*spu_new_buffer( decoder_t
*p_dec
,
2442 const subpicture_updater_t
*p_updater
)
2444 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
2445 vout_thread_t
*p_vout
= NULL
;
2446 subpicture_t
*p_subpic
;
2447 int i_attempts
= 30;
2449 while( i_attempts
-- )
2451 if( DecoderIsExitRequested( p_dec
) || p_dec
->b_error
)
2454 p_vout
= input_resource_HoldVout( p_owner
->p_resource
);
2458 msleep( DECODER_SPU_VOUT_WAIT_DURATION
);
2463 msg_Warn( p_dec
, "no vout found, dropping subpicture" );
2467 if( p_owner
->p_spu_vout
!= p_vout
)
2469 vlc_mutex_lock( &p_owner
->lock
);
2471 DecoderFlushBuffering( p_dec
);
2473 vlc_mutex_unlock( &p_owner
->lock
);
2475 p_owner
->i_spu_channel
= vout_RegisterSubpictureChannel( p_vout
);
2476 p_owner
->i_spu_order
= 0;
2477 p_owner
->p_spu_vout
= p_vout
;
2480 p_subpic
= subpicture_New( p_updater
);
2483 p_subpic
->i_channel
= p_owner
->i_spu_channel
;
2484 p_subpic
->i_order
= p_owner
->i_spu_order
++;
2485 p_subpic
->b_subtitle
= true;
2488 vlc_object_release( p_vout
);
2493 static void spu_del_buffer( decoder_t
*p_dec
, subpicture_t
*p_subpic
)
2495 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
2496 vout_thread_t
*p_vout
= NULL
;
2498 p_vout
= input_resource_HoldVout( p_owner
->p_resource
);
2499 if( !p_vout
|| p_owner
->p_spu_vout
!= p_vout
)
2502 vlc_object_release( p_vout
);
2503 msg_Warn( p_dec
, "no vout found, leaking subpicture" );
2507 subpicture_Delete( p_subpic
);
2509 vlc_object_release( p_vout
);