1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 the VideoLAN team
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 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_block.h>
40 #include <vlc_codec.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"
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
;
89 vout_thread_t
*p_spu_vout
;
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
;
100 /* Current format in use by the output */
101 video_format_t video
;
102 audio_format_t audio
;
106 bool b_fmt_description
;
107 es_format_t fmt_description
;
108 vlc_meta_t
*p_description
;
111 block_fifo_t
*p_fifo
;
113 /* Lock for communication with decoder thread */
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 -- */
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
;
152 block_t
**pp_block_next
;
163 decoder_t
*pp_decoder
[4];
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))
179 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
182 /*****************************************************************************
184 *****************************************************************************/
185 picture_t
*decoder_NewPicture( decoder_t
*p_decoder
)
187 picture_t
*p_picture
= p_decoder
->pf_vout_buffer_new( p_decoder
);
189 msg_Warn( p_decoder
, "can't get output 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
)
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
);
220 msg_Warn( p_decoder
, "can't get output 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
,
234 if( !p_dec
->pf_get_attachments
)
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");
273 /* Create the decoder configuration structure */
274 p_dec
= CreateDecoder( p_input
, fmt
, p_sout
!= NULL
, p_sout
);
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
) );
284 if( !p_dec
->p_module
)
286 DecoderUnsupportedCodec( p_dec
, fmt
->i_codec
);
288 DeleteDecoder( p_dec
);
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
;
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
);
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
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
);
340 if( p_dec
->p_owner
->cc
.b_supported
)
343 for( i
= 0; i
< 4; i
++ )
344 input_DecoderSetCcState( p_dec
, false, i
);
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
;
364 /* The fifo is not consummed when buffering and so will
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
);
372 else if( block_FifoSize( p_owner
->p_fifo
) > 50*1024*1024 /* 50 MiB */ )
374 else if( block_FifoSize( p_owner
->p_fifo
) > 400*1024*1024 /* 400 MiB, ie ~ 50mb/s for 60s */ )
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
;
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
] )
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', ' '),
425 es_format_Init( &fmt
, SPU_ES
, fcc
[i_channel
] );
426 p_cc
= CreateDecoder( p_owner
->p_input
, &fmt
, false, p_owner
->p_sout
);
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.") );
434 else if( !p_cc
->p_module
)
436 DecoderUnsupportedCodec( p_dec
, fcc
[i_channel
] );
437 DeleteDecoder( p_cc
);
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
);
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
);
457 vlc_object_kill( p_cc
);
458 module_unneed( p_cc
, p_cc
->p_module
);
459 DeleteDecoder( p_cc
);
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
;
469 if( i_channel
< 0 || i_channel
>= 4 || !p_owner
->cc
.pb_present
[i_channel
] )
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
);
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
;
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
);
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
;
597 vlc_mutex_lock( &p_owner
->lock
);
598 b_changed
= p_owner
->b_fmt_description
;
602 es_format_Copy( p_fmt
, &p_owner
->fmt_description
);
607 if( p_owner
->p_description
)
609 *pp_meta
= vlc_meta_New();
611 vlc_meta_Merge( *pp_meta
, p_owner
->p_description
);
614 p_owner
->b_fmt_description
= false;
616 vlc_mutex_unlock( &p_owner
->lock
);
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
);
634 *pp_vout
= p_owner
->p_vout
? vlc_object_hold( p_owner
->p_vout
) : NULL
;
636 *pp_aout
= p_owner
->p_aout
? vlc_object_hold( p_owner
->p_aout
) : NULL
;
637 vlc_mutex_unlock( &p_owner
->lock
);
640 /*****************************************************************************
642 *****************************************************************************/
643 static int DecoderGetInputAttachments( decoder_t
*p_dec
,
644 input_attachment_t
***ppp_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
)
662 if( input_clock_ConvertTS( p_owner
->p_clock
, NULL
, &i_ts
, NULL
, INT64_MAX
) )
663 return VLC_TS_INVALID
;
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
);
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.",
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."),
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
)
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
,
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
);
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
;
747 if( ( p_dec
->p_owner
->p_fifo
= block_FifoNew() ) == NULL
)
749 free( p_dec
->p_owner
);
750 vlc_object_release( p_dec
);
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
;
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 */
772 p_dec
->p_module
= module_need( p_dec
, "decoder", "$codec", false );
774 p_dec
->p_module
= module_need( p_dec
, "packetizer", "$packetizer", false );
776 /* Check if decoder requires already packetized data */
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
,
788 es_format_Copy( &p_dec
->p_owner
->p_packetizer
->fmt_out
,
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
];
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;
848 p_owner
->cc
.b_supported
= false;
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;
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 */
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
);
887 int canc
= vlc_savecancel();
890 DecoderError( p_dec
, p_block
);
892 DecoderProcess( p_dec
, p_block
);
894 vlc_restorecancel( canc
);
900 static block_t
*DecoderBlockFlushNew()
902 block_t
*p_null
= block_Alloc( 128 );
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
);
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
);
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();
931 input_DecoderDecode( p_dec
, p_null
, false );
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
)
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
;
959 vlc_mutex_lock( &p_owner
->lock
);
961 b_flushing
= p_owner
->b_flushing
;
963 vlc_mutex_unlock( &p_owner
->lock
);
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
)
980 if( p_owner
->pause
.i_ignore
> 0 )
982 p_owner
->pause
.i_ignore
--;
988 if( !p_owner
->b_buffering
|| !p_owner
->buffer
.b_full
)
991 vlc_cond_wait( &p_owner
->wait_request
, &p_owner
->lock
);
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
,
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;
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
;
1056 const bool b_ephemere
= pi_ts1
&& *pi_ts0
== *pi_ts1
;
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
;
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
)
1077 *pi_duration
= ( *pi_duration
* i_rate
+
1078 INPUT_RATE_DEFAULT
-1 ) / INPUT_RATE_DEFAULT
;
1085 *pi_ts0
= DecoderTeletextFixTs( *pi_ts0
);
1086 if( pi_ts1
&& *pi_ts1
<= VLC_TS_INVALID
)
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 )
1106 vlc_mutex_lock( &p_owner
->lock
);
1107 if( p_owner
->b_flushing
|| p_dec
->b_die
)
1110 vlc_mutex_unlock( &p_owner
->lock
);
1113 int i_ret
= vlc_cond_timedwait( &p_owner
->wait_request
, &p_owner
->lock
,
1115 vlc_mutex_unlock( &p_owner
->lock
);
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
;
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" );
1133 aout_BufferFree( p_audio
);
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
);
1158 bool b_has_more
= false;
1160 DecoderWaitUnblock( p_dec
, &b_reject
);
1162 if( p_owner
->b_buffering
)
1164 vlc_mutex_unlock( &p_owner
->lock
);
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
;
1178 p_owner
->buffer
.pp_audio_next
= &p_owner
->buffer
.p_audio
;
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
)
1196 DecoderWaitDate( p_dec
, &b_reject
,
1197 p_audio
->i_pts
- AOUT_MAX_PREPARE_TIME
);
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
);
1208 msg_Warn( p_aout
, "received buffer in the future" );
1210 msg_Warn( p_dec
, "non-dated audio buffer received" );
1213 aout_BufferFree( p_audio
);
1219 vlc_mutex_lock( &p_owner
->lock
);
1220 if( !p_owner
->buffer
.p_audio
)
1222 vlc_mutex_unlock( &p_owner
->lock
);
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
;
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
;
1244 /* It prevent freezing VLC in case of broken decoder */
1245 aout_DecDeleteBuffer( p_aout
, p_aout_input
, p_aout_buf
);
1247 block_Release( p_block
);
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
);
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
);
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
,
1278 stats_UpdateInteger( p_dec
, p_input
->p
->counters
.p_played_abuffers
,
1280 stats_UpdateInteger( p_dec
, p_input
->p
->counters
.p_decoded_audio
,
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
;
1291 bool b_processed
= false;
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
)
1301 p_cc
= p_dec_cc
->pf_get_cc( p_dec_cc
, pb_present
);
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
] )
1313 for( i
= 0; i
< 4; i
++ )
1315 if( !p_owner
->cc
.pp_decoder
[i
] )
1318 if( i_cc_decoder
> 1 )
1319 DecoderProcess( p_owner
->cc
.pp_decoder
[i
], block_Duplicate( p_cc
) );
1321 DecoderProcess( p_owner
->cc
.pp_decoder
[i
], p_cc
);
1325 vlc_mutex_unlock( &p_owner
->lock
);
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" );
1342 vout_DropPicture( p_vout
, p_picture
);
1345 vout_LinkPicture( p_vout
, p_picture
);
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;
1373 DecoderWaitUnblock( p_dec
, &b_reject
);
1375 if( p_owner
->b_buffering
&& !p_owner
->buffer
.b_first
)
1377 vlc_mutex_unlock( &p_owner
->lock
);
1380 bool b_buffering_first
= p_owner
->b_buffering
;
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
;
1392 p_owner
->buffer
.pp_picture_next
= &p_owner
->buffer
.p_picture
;
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
);
1413 if( !p_picture
->b_force
&& p_picture
->date
<= VLC_TS_INVALID
) // FIXME --VLC_TS_INVALID verify video_output/*
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
);
1430 msg_Warn( p_vout
, "early picture skipped" );
1432 msg_Warn( p_vout
, "non-dated video buffer received" );
1435 vout_UnlinkPicture( p_vout
, p_picture
);
1436 vout_DropPicture( p_vout
, p_picture
);
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
)
1448 vlc_mutex_lock( &p_owner
->lock
);
1449 if( !p_owner
->buffer
.p_picture
)
1451 vlc_mutex_unlock( &p_owner
->lock
);
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
;
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
;
1471 /* It prevent freezing VLC in case of broken decoder */
1472 vout_DropPicture( p_vout
, p_pic
);
1474 block_Release( p_block
);
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
);
1486 if( p_owner
->i_preroll_end
> VLC_TS_INVALID
)
1488 msg_Dbg( p_dec
, "End of video preroll" );
1490 vout_Flush( p_vout
, VLC_TS_INVALID
+1 );
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
,
1507 stats_UpdateInteger( p_dec
, p_input
->p
->counters
.p_lost_pictures
,
1510 stats_UpdateInteger( p_dec
, p_input
->p
->counters
.p_displayed_pictures
,
1513 vlc_mutex_unlock( &p_input
->p
->counters
.counters_lock
);
1517 static void DecoderPlaySpu( decoder_t
*p_dec
, subpicture_t
*p_subpic
,
1520 decoder_owner_sys_t
*p_owner
= p_dec
->p_owner
;
1521 vout_thread_t
*p_vout
= p_owner
->p_spu_vout
;
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
);
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
);
1552 bool b_has_more
= false;
1554 DecoderWaitUnblock( p_dec
, &b_reject
);
1556 if( p_owner
->b_buffering
)
1558 vlc_mutex_unlock( &p_owner
->lock
);
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
;
1572 p_owner
->buffer
.pp_subpic_next
= &p_owner
->buffer
.p_subpic
;
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
)
1584 DecoderWaitDate( p_dec
, &b_reject
,
1585 p_subpic
->i_start
- SPU_MAX_PREPARE_TIME
);
1588 spu_DisplaySubpicture( vout_GetSpu( p_vout
), p_subpic
);
1590 subpicture_Delete( p_subpic
);
1594 vlc_mutex_lock( &p_owner
->lock
);
1595 if( !p_owner
->buffer
.p_subpic
)
1597 vlc_mutex_unlock( &p_owner
->lock
);
1603 static void DecoderPlaySout( decoder_t
*p_dec
, block_t
*p_sout_block
,
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
);
1628 bool b_has_more
= false;
1630 DecoderWaitUnblock( p_dec
, &b_reject
);
1632 if( p_owner
->b_buffering
)
1634 vlc_mutex_unlock( &p_owner
->lock
);
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
;
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
);
1659 sout_InputSendBuffer( p_owner
->p_sout_input
, p_sout_block
); // FIXME --VLC_TS_INVALID inspect stream_output/*
1661 block_Release( p_sout_block
);
1665 vlc_mutex_lock( &p_owner
->lock
);
1666 if( !p_owner
->buffer
.p_block
)
1668 vlc_mutex_unlock( &p_owner
->lock
);
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
,
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
;
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
1824 block_t
*p_null
= DecoderBlockFlushNew();
1826 DecoderDecodeVideo( p_dec
, p_null
);
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
1872 block_t
*p_null
= DecoderBlockFlushNew();
1874 DecoderDecodeAudio( p_dec
, p_null
);
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
);
1915 DecoderPlaySpu( p_dec
, p_spu
, b_telx
);
1920 subpicture_Delete( p_spu
);
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
);
1935 vlc_object_release( p_vout
);
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
);
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
);
1975 if( p_owner
->b_packetizer
)
1978 p_block
->i_flags
&= ~BLOCK_FLAG_CORE_PRIVATE_MASK
;
1980 DecoderProcessSout( p_dec
, p_block
);
1985 bool b_flush
= false;
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
);
2011 msg_Err( p_dec
, "unknown ES format" );
2012 p_dec
->b_error
= true;
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
);
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
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
);
2053 vlc_mutex_lock( &p_owner
->lock
);
2054 DecoderFlushBuffering( p_dec
);
2055 vlc_mutex_unlock( &p_owner
->lock
);
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
,
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() );
2076 input_resource_RequestVout( p_owner
->p_input
->p
->p_resource
, p_owner
->p_vout
, NULL
, true );
2077 input_SendEventVout( p_owner
->p_input
);
2081 if( p_owner
->p_sout_input
)
2083 sout_InputDelete( p_owner
->p_sout_input
);
2084 es_format_Clean( &p_owner
->sout
);
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
);
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
);
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
);
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
;
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;
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
);
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 */
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
;
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
)
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;
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
2361 picture_t
*p_picture
;
2363 if( p_dec
->b_die
|| p_dec
->b_error
)
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 );
2378 if( DecoderIsFlushing( p_dec
) )
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
)
2419 p_vout
= input_resource_HoldVout( p_owner
->p_input
->p
->p_resource
);
2423 msleep( DECODER_SPU_VOUT_WAIT_DURATION
);
2428 msg_Warn( p_dec
, "no vout found, dropping subpicture" );
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();
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
);
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
)
2468 vlc_object_release( p_vout
);
2469 msg_Warn( p_dec
, "no vout found, leaking subpicture" );
2473 subpicture_Delete( p_subpic
);
2475 vlc_object_release( p_vout
);