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 const 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 DecoderFlush( decoder_t * );
64 static void DecoderSignalWait( decoder_t * );
66 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
68 /* Buffers allocation callbacks for the decoders */
69 static int vout_update_format( decoder_t * );
70 static picture_t *vout_new_buffer( decoder_t * );
71 static int aout_update_format( decoder_t * );
72 static subpicture_t *spu_new_buffer( decoder_t *, const subpicture_updater_t * );
74 struct decoder_owner_sys_t
76 int64_t i_preroll_end;
78 input_thread_t *p_input;
79 input_resource_t*p_resource;
80 input_clock_t *p_clock;
83 vout_thread_t *p_spu_vout;
87 sout_instance_t *p_sout;
88 sout_packetizer_input_t *p_sout_input;
92 /* Some decoders require already packetized data (ie. not truncated) */
93 decoder_t *p_packetizer;
96 /* Current format in use by the output */
100 bool b_fmt_description;
101 vlc_meta_t *p_description;
104 block_fifo_t *p_fifo;
106 /* Lock for communication with decoder thread */
108 vlc_cond_t wait_request;
109 vlc_cond_t wait_acknowledge;
111 /* -- These variables need locking on write(only) -- */
112 audio_output_t *p_aout;
114 vout_thread_t *p_vout;
116 /* -- Theses variables need locking on read *and* write -- */
141 decoder_t *pp_decoder[4];
148 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
149 * a bogus PTS and won't be displayed */
150 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
153 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
156 /*****************************************************************************
158 *****************************************************************************/
159 picture_t *decoder_NewPicture( decoder_t *p_decoder )
161 if( decoder_UpdateVideoFormat( p_decoder ) )
164 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
166 msg_Warn( p_decoder, "can't get output picture" );
170 block_t *decoder_NewAudioBuffer( decoder_t *dec, int samples )
172 if( decoder_UpdateAudioFormat( dec ) )
175 size_t length = samples * dec->fmt_out.audio.i_bytes_per_frame
176 / dec->fmt_out.audio.i_frame_length;
177 block_t *block = block_Alloc( length );
178 if( likely(block != NULL) )
180 block->i_nb_samples = samples;
181 block->i_pts = block->i_length = 0;
186 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder,
187 const subpicture_updater_t *p_dyn )
189 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder, p_dyn );
191 msg_Warn( p_decoder, "can't get output subpicture" );
195 /* decoder_GetInputAttachments:
197 int decoder_GetInputAttachments( decoder_t *p_dec,
198 input_attachment_t ***ppp_attachment,
201 if( !p_dec->pf_get_attachments )
204 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
206 /* decoder_GetDisplayDate:
208 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
210 if( !p_dec->pf_get_display_date )
211 return VLC_TS_INVALID;
213 return p_dec->pf_get_display_date( p_dec, i_ts );
215 /* decoder_GetDisplayRate:
217 int decoder_GetDisplayRate( decoder_t *p_dec )
219 if( !p_dec->pf_get_display_rate )
220 return INPUT_RATE_DEFAULT;
222 return p_dec->pf_get_display_rate( p_dec );
225 /* TODO: pass p_sout through p_resource? -- Courmisch */
226 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
227 const es_format_t *fmt, input_clock_t *p_clock,
228 input_resource_t *p_resource,
229 sout_instance_t *p_sout )
231 decoder_t *p_dec = NULL;
232 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
235 /* Create the decoder configuration structure */
236 p_dec = CreateDecoder( p_parent, p_input, fmt,
237 p_sout != NULL, p_resource, p_sout );
240 msg_Err( p_parent, "could not create %s", psz_type );
241 dialog_Fatal( p_parent, _("Streaming / Transcoding failed"),
242 _("VLC could not open the %s module."),
243 vlc_gettext( psz_type ) );
247 if( !p_dec->p_module )
249 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
251 DeleteDecoder( p_dec );
255 p_dec->p_owner->p_clock = p_clock;
256 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
258 if( p_dec->fmt_out.i_cat == AUDIO_ES )
259 i_priority = VLC_THREAD_PRIORITY_AUDIO;
261 i_priority = VLC_THREAD_PRIORITY_VIDEO;
263 /* Spawn the decoder thread */
264 if( vlc_clone( &p_dec->p_owner->thread, DecoderThread, p_dec, i_priority ) )
266 msg_Err( p_dec, "cannot spawn decoder thread" );
267 module_unneed( p_dec, p_dec->p_module );
268 DeleteDecoder( p_dec );
277 * Spawns a new decoder thread from the input thread
279 * \param p_input the input thread
280 * \param p_es the es descriptor
281 * \return the spawned decoder object
283 decoder_t *input_DecoderNew( input_thread_t *p_input,
284 es_format_t *fmt, input_clock_t *p_clock,
285 sout_instance_t *p_sout )
287 return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
288 p_input->p->p_resource, p_sout );
292 * Spawn a decoder thread outside of the input thread.
294 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, const es_format_t *fmt,
295 input_resource_t *p_resource )
297 return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
302 * Kills a decoder thread and waits until it's finished
304 * \param p_input the input thread
305 * \param p_es the es descriptor
308 void input_DecoderDelete( decoder_t *p_dec )
310 decoder_owner_sys_t *p_owner = p_dec->p_owner;
312 vlc_cancel( p_owner->thread );
314 /* Make sure we aren't paused/waiting/decoding anymore */
315 vlc_mutex_lock( &p_owner->lock );
316 p_owner->b_paused = false;
317 p_owner->b_waiting = false;
318 p_owner->b_flushing = true;
319 p_owner->b_exit = true;
320 vlc_cond_signal( &p_owner->wait_request );
321 vlc_mutex_unlock( &p_owner->lock );
323 vlc_join( p_owner->thread, NULL );
325 module_unneed( p_dec, p_dec->p_module );
328 if( p_dec->p_owner->cc.b_supported )
331 for( i = 0; i < 4; i++ )
332 input_DecoderSetCcState( p_dec, false, i );
336 DeleteDecoder( p_dec );
340 * Put a block_t in the decoder's fifo.
341 * Thread-safe w.r.t. the decoder. May be a cancellation point.
343 * \param p_dec the decoder object
344 * \param p_block the data block
346 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
348 decoder_owner_sys_t *p_owner = p_dec->p_owner;
350 vlc_fifo_Lock( p_owner->p_fifo );
353 /* FIXME: ideally we would check the time amount of data
354 * in the FIFO instead of its size. */
355 /* 400 MiB, i.e. ~ 50mb/s for 60s */
356 if( vlc_fifo_GetBytes( p_owner->p_fifo ) > 400*1024*1024 )
358 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
359 "consumed quickly enough), resetting fifo!" );
360 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
364 if( !p_owner->b_waiting )
365 { /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
366 * Locking is not necessary as b_waiting is only read, not written by
367 * the decoder thread. */
368 while( vlc_fifo_GetCount( p_owner->p_fifo ) >= 10 )
369 vlc_fifo_WaitCond( p_owner->p_fifo, &p_owner->wait_acknowledge );
372 vlc_fifo_QueueUnlocked( p_owner->p_fifo, p_block );
373 vlc_fifo_Unlock( p_owner->p_fifo );
376 bool input_DecoderIsEmpty( decoder_t * p_dec )
378 decoder_owner_sys_t *p_owner = p_dec->p_owner;
379 assert( !p_owner->b_waiting );
381 bool b_empty = block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
385 vlc_mutex_lock( &p_owner->lock );
386 /* TODO subtitles support */
387 if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->p_vout )
388 b_empty = vout_IsEmpty( p_owner->p_vout );
389 else if( p_owner->fmt.i_cat == AUDIO_ES && p_owner->p_aout )
390 b_empty = aout_DecIsEmpty( p_owner->p_aout );
391 vlc_mutex_unlock( &p_owner->lock );
396 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
398 decoder_owner_sys_t *p_owner = p_dec->p_owner;
401 vlc_mutex_lock( &p_owner->lock );
402 for( i = 0; i < 4; i++ )
403 pb_present[i] = p_owner->cc.pb_present[i];
404 vlc_mutex_unlock( &p_owner->lock );
406 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
408 decoder_owner_sys_t *p_owner = p_dec->p_owner;
410 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
412 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
417 static const vlc_fourcc_t fcc[4] = {
418 VLC_FOURCC('c', 'c', '1', ' '),
419 VLC_FOURCC('c', 'c', '2', ' '),
420 VLC_FOURCC('c', 'c', '3', ' '),
421 VLC_FOURCC('c', 'c', '4', ' '),
426 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
427 p_cc = input_DecoderNew( p_owner->p_input, &fmt,
428 p_dec->p_owner->p_clock, p_owner->p_sout );
431 msg_Err( p_dec, "could not create decoder" );
432 dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
433 _("VLC could not open the decoder module.") );
436 else if( !p_cc->p_module )
438 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
439 input_DecoderDelete(p_cc);
442 p_cc->p_owner->p_clock = p_owner->p_clock;
444 vlc_mutex_lock( &p_owner->lock );
445 p_owner->cc.pp_decoder[i_channel] = p_cc;
446 vlc_mutex_unlock( &p_owner->lock );
452 vlc_mutex_lock( &p_owner->lock );
453 p_cc = p_owner->cc.pp_decoder[i_channel];
454 p_owner->cc.pp_decoder[i_channel] = NULL;
455 vlc_mutex_unlock( &p_owner->lock );
458 input_DecoderDelete(p_cc);
462 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
464 decoder_owner_sys_t *p_owner = p_dec->p_owner;
467 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
470 vlc_mutex_lock( &p_owner->lock );
471 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
472 vlc_mutex_unlock( &p_owner->lock );
476 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
478 decoder_owner_sys_t *p_owner = p_dec->p_owner;
480 vlc_mutex_lock( &p_owner->lock );
481 /* Normally, p_owner->b_paused != b_paused here. But if a track is added
482 * while the input is paused (e.g. add sub file), then b_paused is
483 * (incorrectly) false. */
484 if( likely(p_owner->b_paused != b_paused) ) {
485 p_owner->b_paused = b_paused;
486 p_owner->pause.i_date = i_date;
487 p_owner->pause.i_ignore = 0;
488 vlc_cond_signal( &p_owner->wait_request );
490 /* XXX only audio and video output have to be paused.
491 * - for sout it is useless
492 * - for subs, it is done by the vout
494 if( p_dec->fmt_out.i_cat == AUDIO_ES )
496 if( p_owner->p_aout )
497 aout_DecChangePause( p_owner->p_aout, b_paused, i_date );
499 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
501 if( p_owner->p_vout )
502 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
505 vlc_mutex_unlock( &p_owner->lock );
508 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
510 decoder_owner_sys_t *p_owner = p_dec->p_owner;
512 vlc_mutex_lock( &p_owner->lock );
513 p_owner->i_ts_delay = i_delay;
514 vlc_mutex_unlock( &p_owner->lock );
518 * Requests that the decoder immediately discard all pending buffers.
519 * This is useful at end of stream, when seeking or when deselecting a stream.
521 void input_DecoderFlush( decoder_t *p_dec )
523 decoder_owner_sys_t *p_owner = p_dec->p_owner;
525 vlc_mutex_lock( &p_owner->lock );
526 DecoderFlush( p_dec );
527 vlc_mutex_unlock( &p_owner->lock );
530 void input_DecoderStartWait( decoder_t *p_dec )
532 decoder_owner_sys_t *p_owner = p_dec->p_owner;
534 assert( !p_owner->b_waiting );
536 vlc_mutex_lock( &p_owner->lock );
537 p_owner->b_first = true;
538 p_owner->b_has_data = false;
539 p_owner->b_waiting = true;
540 vlc_cond_signal( &p_owner->wait_request );
541 vlc_mutex_unlock( &p_owner->lock );
544 void input_DecoderStopWait( decoder_t *p_dec )
546 decoder_owner_sys_t *p_owner = p_dec->p_owner;
548 assert( p_owner->b_waiting );
550 vlc_mutex_lock( &p_owner->lock );
551 p_owner->b_waiting = false;
552 vlc_cond_signal( &p_owner->wait_request );
553 vlc_mutex_unlock( &p_owner->lock );
556 void input_DecoderWait( decoder_t *p_dec )
558 decoder_owner_sys_t *p_owner = p_dec->p_owner;
560 assert( p_owner->b_waiting );
562 vlc_mutex_lock( &p_owner->lock );
563 while( !p_owner->b_has_data )
565 vlc_fifo_Lock( p_owner->p_fifo );
566 p_owner->b_woken = true;
567 vlc_fifo_Signal( p_owner->p_fifo );
568 vlc_fifo_Unlock( p_owner->p_fifo );
569 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
571 vlc_mutex_unlock( &p_owner->lock );
574 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
576 decoder_owner_sys_t *p_owner = p_dec->p_owner;
580 vlc_mutex_lock( &p_owner->lock );
581 if( p_dec->fmt_out.i_cat == VIDEO_ES )
583 if( p_owner->b_paused && p_owner->p_vout )
585 vout_NextPicture( p_owner->p_vout, pi_duration );
586 p_owner->pause.i_ignore++;
587 vlc_cond_signal( &p_owner->wait_request );
592 /* TODO subtitle should not be flushed */
593 p_owner->b_waiting = false;
594 DecoderFlush( p_dec );
596 vlc_mutex_unlock( &p_owner->lock );
599 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
601 decoder_owner_sys_t *p_owner = p_dec->p_owner;
604 vlc_mutex_lock( &p_owner->lock );
605 b_changed = p_owner->b_fmt_description;
609 es_format_Copy( p_fmt, &p_owner->fmt );
614 if( p_owner->p_description )
616 *pp_meta = vlc_meta_New();
618 vlc_meta_Merge( *pp_meta, p_owner->p_description );
621 p_owner->b_fmt_description = false;
623 vlc_mutex_unlock( &p_owner->lock );
627 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
629 decoder_owner_sys_t *p_owner = p_dec->p_owner;
631 return block_FifoSize( p_owner->p_fifo );
634 void input_DecoderGetObjects( decoder_t *p_dec,
635 vout_thread_t **pp_vout, audio_output_t **pp_aout )
637 decoder_owner_sys_t *p_owner = p_dec->p_owner;
639 vlc_mutex_lock( &p_owner->lock );
641 *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
643 *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
644 vlc_mutex_unlock( &p_owner->lock );
647 /*****************************************************************************
649 *****************************************************************************/
650 static int DecoderGetInputAttachments( decoder_t *p_dec,
651 input_attachment_t ***ppp_attachment,
654 input_thread_t *p_input = p_dec->p_owner->p_input;
656 if( unlikely(p_input == NULL) )
658 return input_Control( p_input, INPUT_GET_ATTACHMENTS,
659 ppp_attachment, pi_attachment );
661 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
663 decoder_owner_sys_t *p_owner = p_dec->p_owner;
665 vlc_mutex_lock( &p_owner->lock );
666 if( p_owner->b_waiting || p_owner->b_paused )
667 i_ts = VLC_TS_INVALID;
668 vlc_mutex_unlock( &p_owner->lock );
670 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
673 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) ) {
674 msg_Err(p_dec, "Could not get display date for timestamp %"PRId64"", i_ts);
675 return VLC_TS_INVALID;
680 static int DecoderGetDisplayRate( decoder_t *p_dec )
682 decoder_owner_sys_t *p_owner = p_dec->p_owner;
684 if( !p_owner->p_clock )
685 return INPUT_RATE_DEFAULT;
686 return input_clock_GetRate( p_owner->p_clock );
690 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
692 if (codec != VLC_FOURCC('u','n','d','f')) {
693 const char *desc = vlc_fourcc_GetDescription(p_dec->fmt_in.i_cat, codec);
695 desc = N_("No description for this codec");
696 msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&codec, desc );
697 dialog_Fatal( p_dec, _("Codec not supported"),
698 _("VLC could not decode the format \"%4.4s\" (%s)"),
699 (char*)&codec, desc );
701 msg_Err( p_dec, "could not identify codec" );
702 dialog_Fatal( p_dec, _("Unidentified codec"),
703 _("VLC could not identify the audio or video codec" ) );
709 * Create a decoder object
711 * \param p_input the input thread
712 * \param p_es the es descriptor
713 * \param b_packetizer instead of a decoder
714 * \return the decoder object
716 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
717 input_thread_t *p_input,
718 const es_format_t *fmt, bool b_packetizer,
719 input_resource_t *p_resource,
720 sout_instance_t *p_sout )
723 decoder_owner_sys_t *p_owner;
724 es_format_t null_es_format;
726 p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
730 p_dec->pf_decode_audio = NULL;
731 p_dec->pf_decode_video = NULL;
732 p_dec->pf_decode_sub = NULL;
733 p_dec->pf_get_cc = NULL;
734 p_dec->pf_packetize = NULL;
736 /* Initialize the decoder */
737 p_dec->p_module = NULL;
739 memset( &null_es_format, 0, sizeof(es_format_t) );
740 es_format_Copy( &p_dec->fmt_in, fmt );
741 es_format_Copy( &p_dec->fmt_out, &null_es_format );
743 p_dec->p_description = NULL;
745 /* Allocate our private structure for the decoder */
746 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
747 if( unlikely(p_owner == NULL) )
749 vlc_object_release( p_dec );
752 p_owner->i_preroll_end = VLC_TS_INVALID;
753 p_owner->i_last_rate = INPUT_RATE_DEFAULT;
754 p_owner->p_input = p_input;
755 p_owner->p_resource = p_resource;
756 p_owner->p_aout = NULL;
757 p_owner->p_vout = NULL;
758 p_owner->p_spu_vout = NULL;
759 p_owner->i_spu_channel = 0;
760 p_owner->i_spu_order = 0;
761 p_owner->p_sout = p_sout;
762 p_owner->p_sout_input = NULL;
763 p_owner->p_packetizer = NULL;
764 p_owner->b_packetizer = b_packetizer;
765 es_format_Init( &p_owner->fmt, UNKNOWN_ES, 0 );
768 p_owner->b_woken = false;
769 p_owner->p_fifo = block_FifoNew();
770 if( unlikely(p_owner->p_fifo == NULL) )
773 vlc_object_release( p_dec );
777 /* Set buffers allocation callbacks for the decoders */
778 p_dec->pf_aout_format_update = aout_update_format;
779 p_dec->pf_vout_format_update = vout_update_format;
780 p_dec->pf_vout_buffer_new = vout_new_buffer;
781 p_dec->pf_spu_buffer_new = spu_new_buffer;
783 p_dec->pf_get_attachments = DecoderGetInputAttachments;
784 p_dec->pf_get_display_date = DecoderGetDisplayDate;
785 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
787 /* Find a suitable decoder/packetizer module */
789 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
791 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
793 /* Check if decoder requires already packetized data */
795 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
797 p_owner->p_packetizer =
798 vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
799 if( p_owner->p_packetizer )
801 es_format_Copy( &p_owner->p_packetizer->fmt_in,
804 es_format_Copy( &p_owner->p_packetizer->fmt_out,
807 p_owner->p_packetizer->p_module =
808 module_need( p_owner->p_packetizer,
809 "packetizer", "$packetizer", false );
811 if( !p_owner->p_packetizer->p_module )
813 es_format_Clean( &p_owner->p_packetizer->fmt_in );
814 vlc_object_release( p_owner->p_packetizer );
815 p_owner->p_packetizer = NULL;
820 /* Copy ourself the input replay gain */
821 if( fmt->i_cat == AUDIO_ES )
823 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
825 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
827 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
828 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
830 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
832 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
833 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
839 vlc_mutex_init( &p_owner->lock );
840 vlc_cond_init( &p_owner->wait_request );
841 vlc_cond_init( &p_owner->wait_acknowledge );
843 p_owner->b_fmt_description = false;
844 p_owner->p_description = NULL;
846 p_owner->b_exit = false;
848 p_owner->b_paused = false;
849 p_owner->pause.i_date = VLC_TS_INVALID;
850 p_owner->pause.i_ignore = 0;
852 p_owner->b_waiting = false;
853 p_owner->b_first = true;
854 p_owner->b_has_data = false;
856 p_owner->b_flushing = false;
859 p_owner->cc.b_supported = false;
862 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
863 p_owner->cc.b_supported = true;
864 if( p_dec->pf_get_cc )
865 p_owner->cc.b_supported = true;
868 for( unsigned i = 0; i < 4; i++ )
870 p_owner->cc.pb_present[i] = false;
871 p_owner->cc.pp_decoder[i] = NULL;
873 p_owner->i_ts_delay = 0;
878 * The decoding main loop
880 * \param p_dec the decoder
882 static void *DecoderThread( void *p_data )
884 decoder_t *p_dec = (decoder_t *)p_data;
885 decoder_owner_sys_t *p_owner = p_dec->p_owner;
887 /* The decoder's main loop */
892 vlc_fifo_Lock( p_owner->p_fifo );
893 vlc_fifo_CleanupPush( p_owner->p_fifo );
895 while( vlc_fifo_IsEmpty( p_owner->p_fifo ) )
897 if( p_owner->b_woken )
899 vlc_fifo_Wait( p_owner->p_fifo );
900 /* Make sure there is no cancellation point other than this one^^.
901 * If you need one, be sure to push cleanup of p_block. */
904 p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
905 if( p_block != NULL )
906 vlc_cond_signal( &p_owner->wait_acknowledge );
908 p_owner->b_woken = false;
911 if( p_block == NULL || p_block->i_flags & BLOCK_FLAG_CORE_EOS )
912 DecoderSignalWait( p_dec );
916 int canc = vlc_savecancel();
918 if( p_block->i_flags & BLOCK_FLAG_CORE_EOS )
920 /* calling DecoderProcess() with NULL block will make
921 * decoders/packetizers flush their buffers */
922 block_Release( p_block );
926 DecoderProcess( p_dec, p_block );
928 vlc_restorecancel( canc );
934 static block_t *DecoderBlockFlushNew()
936 block_t *p_null = block_Alloc( 128 );
940 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
941 BLOCK_FLAG_CORRUPTED |
942 BLOCK_FLAG_CORE_FLUSH;
943 memset( p_null->p_buffer, 0, p_null->i_buffer );
948 static void DecoderFlush( decoder_t *p_dec )
950 decoder_owner_sys_t *p_owner = p_dec->p_owner;
952 vlc_assert_locked( &p_owner->lock );
955 block_FifoEmpty( p_owner->p_fifo );
957 /* Monitor for flush end */
958 p_owner->b_flushing = true;
959 vlc_cond_signal( &p_owner->wait_request );
961 /* Send a special block */
962 block_t *p_null = DecoderBlockFlushNew();
965 input_DecoderDecode( p_dec, p_null, false );
968 while( p_owner->b_flushing )
969 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
972 static void DecoderSignalWait( decoder_t *p_dec )
974 decoder_owner_sys_t *p_owner = p_dec->p_owner;
976 vlc_mutex_lock( &p_owner->lock );
978 if( p_owner->b_waiting )
980 p_owner->b_has_data = true;
981 vlc_cond_signal( &p_owner->wait_acknowledge );
984 vlc_mutex_unlock( &p_owner->lock );
987 static bool DecoderIsFlushing( decoder_t *p_dec )
989 decoder_owner_sys_t *p_owner = p_dec->p_owner;
992 vlc_mutex_lock( &p_owner->lock );
994 b_flushing = p_owner->b_flushing;
996 vlc_mutex_unlock( &p_owner->lock );
1001 static bool DecoderWaitUnblock( decoder_t *p_dec )
1003 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1005 vlc_assert_locked( &p_owner->lock );
1009 if( p_owner->b_flushing )
1011 if( p_owner->b_paused )
1013 if( p_owner->b_waiting && !p_owner->b_has_data )
1015 if( p_owner->pause.i_ignore > 0 )
1017 p_owner->pause.i_ignore--;
1023 if( !p_owner->b_waiting || !p_owner->b_has_data )
1026 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
1029 return p_owner->b_flushing;
1032 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1034 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1035 *pi_preroll = INT64_MAX;
1036 else if( p->i_dts > VLC_TS_INVALID )
1037 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1038 else if( p->i_pts > VLC_TS_INVALID )
1039 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1042 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1043 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
1045 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1046 input_clock_t *p_clock = p_owner->p_clock;
1048 vlc_assert_locked( &p_owner->lock );
1050 const mtime_t i_es_delay = p_owner->i_ts_delay;
1055 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1058 if( *pi_ts0 > VLC_TS_INVALID )
1060 *pi_ts0 += i_es_delay;
1061 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1062 *pi_ts1 += i_es_delay;
1063 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) ) {
1064 if( pi_ts1 != NULL )
1065 msg_Err(p_dec, "Could not convert timestamps %"PRId64
1066 ", %"PRId64"", *pi_ts0, *pi_ts1);
1068 msg_Err(p_dec, "Could not convert timestamp %"PRId64, *pi_ts0);
1069 *pi_ts0 = VLC_TS_INVALID;
1074 i_rate = input_clock_GetRate( p_clock );
1077 /* Do not create ephemere data because of rounding errors */
1078 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1082 *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
1083 / INPUT_RATE_DEFAULT;
1089 static bool DecoderIsExitRequested( decoder_t *p_dec )
1091 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1093 vlc_mutex_lock( &p_owner->lock );
1094 bool b_exit = p_owner->b_exit;
1095 vlc_mutex_unlock( &p_owner->lock );
1101 * If *pb_reject, it does nothing, otherwise it waits for the given
1102 * deadline or a flush request (in which case it set *pi_reject to true.
1104 static void DecoderWaitDate( decoder_t *p_dec,
1105 bool *pb_reject, mtime_t i_deadline )
1107 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1109 vlc_assert_locked( &p_owner->lock );
1111 if( *pb_reject || i_deadline < 0 )
1116 if( p_owner->b_flushing || p_owner->b_exit )
1122 while( vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1123 i_deadline ) == 0 );
1126 static void DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
1127 int *pi_played_sum, int *pi_lost_sum )
1129 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1130 audio_output_t *p_aout = p_owner->p_aout;
1133 if( p_audio && p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1135 msg_Warn( p_dec, "non-dated audio buffer received" );
1137 block_Release( p_audio );
1142 vlc_mutex_lock( &p_owner->lock );
1144 if( p_audio && p_owner->b_waiting )
1146 p_owner->b_has_data = true;
1147 vlc_cond_signal( &p_owner->wait_acknowledge );
1154 bool b_reject = DecoderWaitUnblock( p_dec );
1156 b_paused = p_owner->b_paused;
1162 int i_rate = INPUT_RATE_DEFAULT;
1164 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1165 &i_rate, AOUT_MAX_ADVANCE_TIME );
1167 if( p_audio->i_pts <= VLC_TS_INVALID
1168 || i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1169 || i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1172 DecoderWaitDate( p_dec, &b_reject,
1173 p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1175 if( unlikely(p_owner->b_paused != b_paused) )
1176 continue; /* race with input thread? retry... */
1177 if( p_aout == NULL )
1182 assert( !p_owner->b_paused );
1183 if( !aout_DecPlay( p_aout, p_audio, i_rate ) )
1184 *pi_played_sum += 1;
1185 *pi_lost_sum += aout_DecGetResetLost( p_aout );
1189 msg_Dbg( p_dec, "discarded audio buffer" );
1191 block_Release( p_audio );
1196 vlc_mutex_unlock( &p_owner->lock );
1199 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1201 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1202 block_t *p_aout_buf;
1208 /* Play a NULL block to output buffered frames */
1209 DecoderPlayAudio( p_dec, NULL, &i_played, &i_lost );
1211 else while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1213 if( DecoderIsExitRequested( p_dec ) )
1215 /* It prevent freezing VLC in case of broken decoder */
1216 block_Release( p_aout_buf );
1218 block_Release( p_block );
1223 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1224 p_aout_buf->i_pts < p_owner->i_preroll_end )
1226 block_Release( p_aout_buf );
1230 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1232 msg_Dbg( p_dec, "End of audio preroll" );
1233 if( p_owner->p_aout )
1234 aout_DecFlush( p_owner->p_aout );
1236 p_owner->i_preroll_end = VLC_TS_INVALID;
1239 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1242 /* Update ugly stat */
1243 input_thread_t *p_input = p_owner->p_input;
1245 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1247 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1248 stats_Update( p_input->p->counters.p_lost_abuffers, i_lost, NULL );
1249 stats_Update( p_input->p->counters.p_played_abuffers, i_played, NULL );
1250 stats_Update( p_input->p->counters.p_decoded_audio, i_decoded, NULL );
1251 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1254 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1256 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1259 bool b_processed = false;
1263 assert( p_dec_cc->pf_get_cc != NULL );
1265 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1266 if( !p_owner->cc.b_supported )
1269 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1273 vlc_mutex_lock( &p_owner->lock );
1274 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1276 p_owner->cc.pb_present[i] |= pb_present[i];
1277 if( p_owner->cc.pp_decoder[i] )
1281 for( i = 0; i < 4; i++ )
1283 if( !p_owner->cc.pp_decoder[i] )
1286 block_FifoPut( p_owner->cc.pp_decoder[i]->p_owner->p_fifo,
1287 (i_cc_decoder > 1) ? block_Duplicate(p_cc) : p_cc);
1292 vlc_mutex_unlock( &p_owner->lock );
1295 block_Release( p_cc );
1298 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1299 int *pi_played_sum, int *pi_lost_sum )
1301 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1302 vout_thread_t *p_vout = p_owner->p_vout;
1304 if( p_picture->date <= VLC_TS_INVALID )
1306 msg_Warn( p_dec, "non-dated video buffer received" );
1308 picture_Release( p_picture );
1313 vlc_mutex_lock( &p_owner->lock );
1315 if( p_owner->b_waiting && !p_owner->b_first )
1317 p_owner->b_has_data = true;
1318 vlc_cond_signal( &p_owner->wait_acknowledge );
1320 bool b_first_after_wait = p_owner->b_waiting && p_owner->b_has_data;
1322 bool b_reject = DecoderWaitUnblock( p_dec );
1324 if( p_owner->b_waiting )
1326 assert( p_owner->b_first );
1327 msg_Dbg( p_dec, "Received first picture" );
1328 p_owner->b_first = false;
1329 p_picture->b_force = true;
1332 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1333 int i_rate = INPUT_RATE_DEFAULT;
1334 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1335 &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1337 vlc_mutex_unlock( &p_owner->lock );
1340 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1345 if( i_rate != p_owner->i_last_rate || b_first_after_wait )
1347 /* Be sure to not display old picture after our own */
1348 vout_Flush( p_vout, p_picture->date );
1349 p_owner->i_last_rate = i_rate;
1351 vout_PutPicture( p_vout, p_picture );
1356 msg_Warn( p_dec, "early picture skipped" );
1358 msg_Warn( p_dec, "non-dated video buffer received" );
1361 picture_Release( p_picture );
1365 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1367 *pi_played_sum += i_tmp_display;
1368 *pi_lost_sum += i_tmp_lost;
1371 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1373 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1377 int i_displayed = 0;
1379 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1381 vout_thread_t *p_vout = p_owner->p_vout;
1382 if( DecoderIsExitRequested( p_dec ) )
1384 /* It prevent freezing VLC in case of broken decoder */
1385 picture_Release( p_pic );
1387 block_Release( p_block );
1393 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1395 picture_Release( p_pic );
1399 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1401 msg_Dbg( p_dec, "End of video preroll" );
1403 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1405 p_owner->i_preroll_end = VLC_TS_INVALID;
1408 if( p_dec->pf_get_cc &&
1409 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1410 DecoderGetCc( p_dec, p_dec );
1412 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1415 /* Update ugly stat */
1416 input_thread_t *p_input = p_owner->p_input;
1418 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
1420 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1421 stats_Update( p_input->p->counters.p_decoded_video, i_decoded, NULL );
1422 stats_Update( p_input->p->counters.p_lost_pictures, i_lost , NULL);
1423 stats_Update( p_input->p->counters.p_displayed_pictures,
1425 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1429 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1431 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1432 vout_thread_t *p_vout = p_owner->p_spu_vout;
1435 if( p_subpic->i_start <= VLC_TS_INVALID )
1437 msg_Warn( p_dec, "non-dated spu buffer received" );
1438 subpicture_Delete( p_subpic );
1443 vlc_mutex_lock( &p_owner->lock );
1445 if( p_owner->b_waiting )
1447 p_owner->b_has_data = true;
1448 vlc_cond_signal( &p_owner->wait_acknowledge );
1451 bool b_reject = DecoderWaitUnblock( p_dec );
1453 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1456 if( p_subpic->i_start <= VLC_TS_INVALID )
1459 DecoderWaitDate( p_dec, &b_reject,
1460 p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1461 vlc_mutex_unlock( &p_owner->lock );
1464 vout_PutSubpicture( p_vout, p_subpic );
1466 subpicture_Delete( p_subpic );
1470 static int DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
1472 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1474 assert( p_owner->p_clock );
1475 assert( !p_sout_block->p_next );
1477 vlc_mutex_lock( &p_owner->lock );
1479 if( p_owner->b_waiting )
1481 p_owner->b_has_data = true;
1482 vlc_cond_signal( &p_owner->wait_acknowledge );
1485 bool b_reject = DecoderWaitUnblock( p_dec );
1487 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1488 &p_sout_block->i_length, NULL, INT64_MAX );
1490 vlc_mutex_unlock( &p_owner->lock );
1494 /* FIXME --VLC_TS_INVALID inspect stream_output*/
1495 return sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1499 block_Release( p_sout_block );
1500 return VLC_EGENERIC;
1506 /* This function process a block for sout
1508 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1510 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1511 block_t *p_sout_block;
1513 while( ( p_sout_block =
1514 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1516 if( p_owner->p_sout_input == NULL )
1518 assert( !p_owner->b_fmt_description ); // no need for owner lock
1519 es_format_Clean( &p_owner->fmt );
1520 es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
1522 p_owner->fmt.i_group = p_dec->fmt_in.i_group;
1523 p_owner->fmt.i_id = p_dec->fmt_in.i_id;
1524 if( p_dec->fmt_in.psz_language )
1526 free( p_owner->fmt.psz_language );
1527 p_owner->fmt.psz_language =
1528 strdup( p_dec->fmt_in.psz_language );
1531 p_owner->p_sout_input =
1532 sout_InputNew( p_owner->p_sout, &p_owner->fmt );
1534 if( p_owner->p_sout_input == NULL )
1536 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1537 (char *)&p_owner->fmt.i_codec );
1538 p_dec->b_error = true;
1540 block_ChainRelease(p_sout_block);
1545 while( p_sout_block )
1547 block_t *p_next = p_sout_block->p_next;
1549 p_sout_block->p_next = NULL;
1551 if( DecoderPlaySout( p_dec, p_sout_block ) == VLC_EGENERIC )
1553 msg_Err( p_dec, "cannot continue streaming due to errors" );
1555 p_dec->b_error = true;
1558 block_ChainRelease( p_next );
1562 p_sout_block = p_next;
1568 /* This function process a video block
1570 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1572 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1574 if( p_owner->p_packetizer )
1576 block_t *p_packetized_block;
1577 decoder_t *p_packetizer = p_owner->p_packetizer;
1579 while( (p_packetized_block =
1580 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1582 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1584 es_format_Clean( &p_dec->fmt_in );
1585 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1588 /* If the packetizer provides aspect ratio information, pass it
1589 * to the decoder as a hint if the decoder itself can't provide
1590 * it. Copy it regardless of the current value of the decoder input
1591 * format aspect ratio, to properly propagate changes in aspect
1593 if( p_packetizer->fmt_out.video.i_sar_num > 0 &&
1594 p_packetizer->fmt_out.video.i_sar_den > 0)
1596 p_dec->fmt_in.video.i_sar_num =
1597 p_packetizer->fmt_out.video.i_sar_num;
1598 p_dec->fmt_in.video.i_sar_den=
1599 p_packetizer->fmt_out.video.i_sar_den;
1602 if( p_packetizer->pf_get_cc )
1603 DecoderGetCc( p_dec, p_packetizer );
1605 while( p_packetized_block )
1607 block_t *p_next = p_packetized_block->p_next;
1608 p_packetized_block->p_next = NULL;
1610 DecoderDecodeVideo( p_dec, p_packetized_block );
1612 p_packetized_block = p_next;
1615 /* The packetizer does not output a block that tell the decoder to flush
1619 block_t *p_null = DecoderBlockFlushNew();
1621 DecoderDecodeVideo( p_dec, p_null );
1626 DecoderDecodeVideo( p_dec, p_block );
1629 if( b_flush && p_owner->p_vout )
1630 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1633 /* This function process a audio block
1635 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1637 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1639 if( p_owner->p_packetizer )
1641 block_t *p_packetized_block;
1642 decoder_t *p_packetizer = p_owner->p_packetizer;
1644 while( (p_packetized_block =
1645 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1647 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1649 es_format_Clean( &p_dec->fmt_in );
1650 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1653 while( p_packetized_block )
1655 block_t *p_next = p_packetized_block->p_next;
1656 p_packetized_block->p_next = NULL;
1658 DecoderDecodeAudio( p_dec, p_packetized_block );
1660 p_packetized_block = p_next;
1663 /* The packetizer does not output a block that tell the decoder to flush
1667 block_t *p_null = DecoderBlockFlushNew();
1669 DecoderDecodeAudio( p_dec, p_null );
1674 DecoderDecodeAudio( p_dec, p_block );
1677 if( b_flush && p_owner->p_aout )
1678 aout_DecFlush( p_owner->p_aout );
1681 /* This function process a subtitle block
1683 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1685 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1687 input_thread_t *p_input = p_owner->p_input;
1688 vout_thread_t *p_vout;
1689 subpicture_t *p_spu;
1691 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1693 if( p_input != NULL )
1695 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1696 stats_Update( p_input->p->counters.p_decoded_sub, 1, NULL );
1697 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1700 p_vout = input_resource_HoldVout( p_owner->p_resource );
1701 if( p_vout && p_owner->p_spu_vout == p_vout )
1703 /* Preroll does not work very well with subtitle */
1704 if( p_spu->i_start > VLC_TS_INVALID &&
1705 p_spu->i_start < p_owner->i_preroll_end &&
1706 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1708 subpicture_Delete( p_spu );
1712 DecoderPlaySpu( p_dec, p_spu );
1717 subpicture_Delete( p_spu );
1720 vlc_object_release( p_vout );
1723 if( b_flush && p_owner->p_spu_vout )
1725 p_vout = input_resource_HoldVout( p_owner->p_resource );
1727 if( p_vout && p_owner->p_spu_vout == p_vout )
1728 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1731 vlc_object_release( p_vout );
1736 static void DecoderProcessOnFlush( decoder_t *p_dec )
1738 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1740 vlc_mutex_lock( &p_owner->lock );
1742 if( p_owner->b_flushing )
1744 p_owner->b_flushing = false;
1745 vlc_cond_signal( &p_owner->wait_acknowledge );
1747 vlc_mutex_unlock( &p_owner->lock );
1753 * \param p_dec the decoder object
1754 * \param p_block the block to decode
1755 * \return VLC_SUCCESS or an error code
1757 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1759 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1760 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1762 if( p_dec->b_error )
1765 block_Release( p_block );
1769 if( p_block && p_block->i_buffer <= 0 )
1771 assert( !b_flush_request );
1772 block_Release( p_block );
1777 if( p_owner->b_packetizer )
1780 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1782 DecoderProcessSout( p_dec, p_block );
1787 bool b_flush = false;
1791 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1792 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1794 b_flush = !b_flushing && b_flush_request;
1796 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1799 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1801 DecoderProcessAudio( p_dec, p_block, b_flush );
1803 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1805 DecoderProcessVideo( p_dec, p_block, b_flush );
1807 else if( p_dec->fmt_out.i_cat == SPU_ES )
1809 DecoderProcessSpu( p_dec, p_block, b_flush );
1813 msg_Err( p_dec, "unknown ES format" );
1814 p_dec->b_error = true;
1820 if( b_flush_request )
1821 DecoderProcessOnFlush( p_dec );
1825 * Destroys a decoder object
1827 * \param p_dec the decoder object
1830 static void DeleteDecoder( decoder_t * p_dec )
1832 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1834 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1835 (char*)&p_dec->fmt_in.i_codec,
1836 (unsigned)block_FifoCount( p_owner->p_fifo ) );
1838 /* Free all packets still in the decoder fifo. */
1839 block_FifoRelease( p_owner->p_fifo );
1842 if( p_owner->p_aout )
1844 /* TODO: REVISIT gap-less audio */
1845 aout_DecFlush( p_owner->p_aout );
1846 aout_DecDelete( p_owner->p_aout );
1847 input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1848 if( p_owner->p_input != NULL )
1849 input_SendEventAout( p_owner->p_input );
1851 if( p_owner->p_vout )
1853 /* Hack to make sure all the the pictures are freed by the decoder
1854 * and that the vout is not paused anymore */
1855 vout_Reset( p_owner->p_vout );
1858 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1860 if( p_owner->p_input != NULL )
1861 input_SendEventVout( p_owner->p_input );
1865 if( p_owner->p_sout_input )
1867 sout_InputDelete( p_owner->p_sout_input );
1870 es_format_Clean( &p_owner->fmt );
1872 if( p_dec->fmt_out.i_cat == SPU_ES )
1874 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1877 if( p_owner->p_spu_vout == p_vout )
1878 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1879 vlc_object_release( p_vout );
1883 es_format_Clean( &p_dec->fmt_in );
1884 es_format_Clean( &p_dec->fmt_out );
1885 if( p_dec->p_description )
1886 vlc_meta_Delete( p_dec->p_description );
1887 if( p_owner->p_description )
1888 vlc_meta_Delete( p_owner->p_description );
1890 if( p_owner->p_packetizer )
1892 module_unneed( p_owner->p_packetizer,
1893 p_owner->p_packetizer->p_module );
1894 es_format_Clean( &p_owner->p_packetizer->fmt_in );
1895 es_format_Clean( &p_owner->p_packetizer->fmt_out );
1896 if( p_owner->p_packetizer->p_description )
1897 vlc_meta_Delete( p_owner->p_packetizer->p_description );
1898 vlc_object_release( p_owner->p_packetizer );
1901 vlc_cond_destroy( &p_owner->wait_acknowledge );
1902 vlc_cond_destroy( &p_owner->wait_request );
1903 vlc_mutex_destroy( &p_owner->lock );
1905 vlc_object_release( p_dec );
1910 /*****************************************************************************
1911 * Buffers allocation callbacks for the decoders
1912 *****************************************************************************/
1913 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
1915 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1917 vlc_assert_locked( &p_owner->lock );
1919 p_owner->b_fmt_description = true;
1921 /* Move p_description */
1922 if( p_owner->p_description && p_dec->p_description )
1923 vlc_meta_Delete( p_owner->p_description );
1924 p_owner->p_description = p_dec->p_description;
1925 p_dec->p_description = NULL;
1927 static vout_thread_t *aout_request_vout( void *p_private,
1928 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
1930 decoder_t *p_dec = p_private;
1931 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1932 input_thread_t *p_input = p_owner->p_input;
1934 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
1936 if( p_input != NULL )
1937 input_SendEventVout( p_input );
1942 static int aout_update_format( decoder_t *p_dec )
1944 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1947 && !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->fmt.audio) )
1949 audio_output_t *p_aout = p_owner->p_aout;
1951 /* Parameters changed, restart the aout */
1952 vlc_mutex_lock( &p_owner->lock );
1954 aout_DecDelete( p_owner->p_aout );
1955 p_owner->p_aout = NULL;
1957 vlc_mutex_unlock( &p_owner->lock );
1958 input_resource_PutAout( p_owner->p_resource, p_aout );
1961 if( p_owner->p_aout == NULL )
1963 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1965 audio_sample_format_t format = p_dec->fmt_out.audio;
1966 aout_FormatPrepare( &format );
1968 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
1969 if( i_force_dolby &&
1970 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
1971 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1973 if( i_force_dolby == 1 )
1975 format.i_original_channels = format.i_original_channels |
1976 AOUT_CHAN_DOLBYSTEREO;
1978 else /* i_force_dolby == 2 */
1980 format.i_original_channels = format.i_original_channels &
1981 ~AOUT_CHAN_DOLBYSTEREO;
1985 aout_request_vout_t request_vout = {
1986 .pf_request_vout = aout_request_vout,
1989 audio_output_t *p_aout;
1991 p_aout = input_resource_GetAout( p_owner->p_resource );
1994 if( aout_DecNew( p_aout, &format,
1995 &p_dec->fmt_out.audio_replay_gain,
1998 input_resource_PutAout( p_owner->p_resource, p_aout );
2003 vlc_mutex_lock( &p_owner->lock );
2005 p_owner->p_aout = p_aout;
2007 es_format_Clean( &p_owner->fmt );
2008 es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
2009 aout_FormatPrepare( &p_owner->fmt.audio );
2011 DecoderUpdateFormatLocked( p_dec );
2013 if( unlikely(p_owner->b_paused) && p_aout != NULL )
2014 /* fake pause if needed */
2015 aout_DecChangePause( p_aout, true, mdate() );
2017 vlc_mutex_unlock( &p_owner->lock );
2019 if( p_owner->p_input != NULL )
2020 input_SendEventAout( p_owner->p_input );
2022 if( p_aout == NULL )
2024 msg_Err( p_dec, "failed to create audio output" );
2025 p_dec->b_error = true;
2029 p_dec->fmt_out.audio.i_bytes_per_frame =
2030 p_owner->fmt.audio.i_bytes_per_frame;
2031 p_dec->fmt_out.audio.i_frame_length =
2032 p_owner->fmt.audio.i_frame_length;
2037 static int vout_update_format( decoder_t *p_dec )
2039 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2041 if( p_owner->p_vout == NULL
2042 || p_dec->fmt_out.video.i_width != p_owner->fmt.video.i_width
2043 || p_dec->fmt_out.video.i_height != p_owner->fmt.video.i_height
2044 || p_dec->fmt_out.video.i_visible_width != p_owner->fmt.video.i_visible_width
2045 || p_dec->fmt_out.video.i_visible_height != p_owner->fmt.video.i_visible_height
2046 || p_dec->fmt_out.video.i_x_offset != p_owner->fmt.video.i_x_offset
2047 || p_dec->fmt_out.video.i_y_offset != p_owner->fmt.video.i_y_offset
2048 || p_dec->fmt_out.i_codec != p_owner->fmt.video.i_chroma
2049 || (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->fmt.video.i_sar_den !=
2050 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->fmt.video.i_sar_num ||
2051 p_dec->fmt_out.video.orientation != p_owner->fmt.video.orientation )
2053 vout_thread_t *p_vout;
2055 if( !p_dec->fmt_out.video.i_width ||
2056 !p_dec->fmt_out.video.i_height )
2058 /* Can't create a new vout without display size */
2062 video_format_t fmt = p_dec->fmt_out.video;
2063 fmt.i_chroma = p_dec->fmt_out.i_codec;
2065 if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
2067 const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
2068 for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
2070 while( fmt.i_width % dsc->p[i].w.den )
2072 while( fmt.i_height % dsc->p[i].h.den )
2077 if( !fmt.i_visible_width || !fmt.i_visible_height )
2079 if( p_dec->fmt_in.video.i_visible_width &&
2080 p_dec->fmt_in.video.i_visible_height )
2082 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
2083 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2084 fmt.i_x_offset = p_dec->fmt_in.video.i_x_offset;
2085 fmt.i_y_offset = p_dec->fmt_in.video.i_y_offset;
2089 fmt.i_visible_width = fmt.i_width;
2090 fmt.i_visible_height = fmt.i_height;
2096 if( fmt.i_visible_height == 1088 &&
2097 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2099 fmt.i_visible_height = 1080;
2100 if( !(fmt.i_sar_num % 136))
2102 fmt.i_sar_num *= 135;
2103 fmt.i_sar_den *= 136;
2105 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2108 if( !fmt.i_sar_num || !fmt.i_sar_den )
2114 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2115 fmt.i_sar_num, fmt.i_sar_den, 50000 );
2117 vlc_mutex_lock( &p_owner->lock );
2119 p_vout = p_owner->p_vout;
2120 p_owner->p_vout = NULL;
2121 vlc_mutex_unlock( &p_owner->lock );
2124 switch( p_dec->fmt_in.i_codec )
2126 case VLC_CODEC_HEVC:
2127 case VLC_CODEC_H264:
2128 case VLC_CODEC_DIRAC: /* FIXME valid ? */
2133 case VLC_CODEC_VP6F:
2141 p_vout = input_resource_RequestVout( p_owner->p_resource,
2144 p_dec->i_extra_picture_buffers + 1,
2146 vlc_mutex_lock( &p_owner->lock );
2147 p_owner->p_vout = p_vout;
2149 es_format_Clean( &p_owner->fmt );
2150 es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
2151 p_owner->fmt.video.i_chroma = p_dec->fmt_out.i_codec;
2153 DecoderUpdateFormatLocked( p_dec );
2155 vlc_mutex_unlock( &p_owner->lock );
2157 if( p_owner->p_input != NULL )
2158 input_SendEventVout( p_owner->p_input );
2159 if( p_vout == NULL )
2161 msg_Err( p_dec, "failed to create video output" );
2162 p_dec->b_error = true;
2169 static picture_t *vout_new_buffer( decoder_t *p_dec )
2171 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2175 if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2178 picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2182 if( DecoderIsFlushing( p_dec ) )
2186 DecoderSignalWait( p_dec );
2188 /* Check the decoder doesn't leak pictures */
2189 vout_FixLeaks( p_owner->p_vout );
2191 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2192 msleep( VOUT_OUTMEM_SLEEP );
2196 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2197 const subpicture_updater_t *p_updater )
2199 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2200 vout_thread_t *p_vout = NULL;
2201 subpicture_t *p_subpic;
2202 int i_attempts = 30;
2204 while( i_attempts-- )
2206 if( DecoderIsExitRequested( p_dec ) || DecoderIsFlushing( p_dec )
2210 p_vout = input_resource_HoldVout( p_owner->p_resource );
2214 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2219 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2223 if( p_owner->p_spu_vout != p_vout )
2225 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
2226 p_owner->i_spu_order = 0;
2227 p_owner->p_spu_vout = p_vout;
2230 p_subpic = subpicture_New( p_updater );
2233 p_subpic->i_channel = p_owner->i_spu_channel;
2234 p_subpic->i_order = p_owner->i_spu_order++;
2235 p_subpic->b_subtitle = true;
2238 vlc_object_release( p_vout );