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>
44 #include <vlc_modules.h>
46 #include "audio_output/aout_internal.h"
47 #include "stream_output/stream_output.h"
48 #include "input_internal.h"
54 #include "../video_output/vout_control.h"
56 static decoder_t *CreateDecoder( vlc_object_t *, input_thread_t *,
57 es_format_t *, bool, input_resource_t *,
58 sout_instance_t *p_sout );
59 static void DeleteDecoder( decoder_t * );
61 static void *DecoderThread( void * );
62 static void DecoderProcess( decoder_t *, block_t * );
63 static void DecoderError( decoder_t *p_dec, block_t *p_block );
64 static void DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
65 static void DecoderFlush( decoder_t * );
66 static void DecoderSignalBuffering( decoder_t *, bool );
67 static void DecoderFlushBuffering( decoder_t * );
69 static void DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );
71 /* Buffers allocation callbacks for the decoders */
72 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
73 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
75 static picture_t *vout_new_buffer( decoder_t * );
76 static void vout_del_buffer( decoder_t *, picture_t * );
77 static void vout_link_picture( decoder_t *, picture_t * );
78 static void vout_unlink_picture( decoder_t *, picture_t * );
80 static subpicture_t *spu_new_buffer( decoder_t *, const subpicture_updater_t * );
81 static void spu_del_buffer( decoder_t *, subpicture_t * );
83 struct decoder_owner_sys_t
85 int64_t i_preroll_end;
87 input_thread_t *p_input;
88 input_resource_t*p_resource;
89 input_clock_t *p_clock;
92 vout_thread_t *p_spu_vout;
96 sout_instance_t *p_sout;
97 sout_packetizer_input_t *p_sout_input;
101 /* Some decoders require already packetized data (ie. not truncated) */
102 decoder_t *p_packetizer;
105 /* Current format in use by the output */
106 video_format_t video;
107 audio_format_t audio;
111 bool b_fmt_description;
112 es_format_t fmt_description;
113 vlc_meta_t *p_description;
116 block_fifo_t *p_fifo;
118 /* Lock for communication with decoder thread */
120 vlc_cond_t wait_request;
121 vlc_cond_t wait_acknowledge;
123 /* -- These variables need locking on write(only) -- */
124 aout_instance_t *p_aout;
125 aout_input_t *p_aout_input;
127 vout_thread_t *p_vout;
129 /* -- Theses variables need locking on read *and* write -- */
148 picture_t *p_picture;
149 picture_t **pp_picture_next;
151 subpicture_t *p_subpic;
152 subpicture_t **pp_subpic_next;
154 aout_buffer_t *p_audio;
155 aout_buffer_t **pp_audio_next;
158 block_t **pp_block_next;
169 decoder_t *pp_decoder[4];
176 #define DECODER_MAX_BUFFERING_COUNT (4)
177 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
178 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
180 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
181 * a bogus PTS and won't be displayed */
182 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
185 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
188 /*****************************************************************************
190 *****************************************************************************/
191 picture_t *decoder_NewPicture( decoder_t *p_decoder )
193 picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
195 msg_Warn( p_decoder, "can't get output picture" );
198 void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
200 p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
202 void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
204 p_decoder->pf_picture_link( p_decoder, p_picture );
206 void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
208 p_decoder->pf_picture_unlink( p_decoder, p_picture );
211 aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
213 if( !p_decoder->pf_aout_buffer_new )
215 return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
217 void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
219 p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
222 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder,
223 const subpicture_updater_t *p_dyn )
225 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder, p_dyn );
227 msg_Warn( p_decoder, "can't get output subpicture" );
231 void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
233 p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
236 /* decoder_GetInputAttachments:
238 int decoder_GetInputAttachments( decoder_t *p_dec,
239 input_attachment_t ***ppp_attachment,
242 if( !p_dec->pf_get_attachments )
245 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
247 /* decoder_GetDisplayDate:
249 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
251 if( !p_dec->pf_get_display_date )
252 return VLC_TS_INVALID;
254 return p_dec->pf_get_display_date( p_dec, i_ts );
256 /* decoder_GetDisplayRate:
258 int decoder_GetDisplayRate( decoder_t *p_dec )
260 if( !p_dec->pf_get_display_rate )
261 return INPUT_RATE_DEFAULT;
263 return p_dec->pf_get_display_rate( p_dec );
266 /* TODO: pass p_sout through p_resource? -- Courmisch */
267 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
268 es_format_t *fmt, input_clock_t *p_clock,
269 input_resource_t *p_resource,
270 sout_instance_t *p_sout )
272 decoder_t *p_dec = NULL;
273 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
276 /* Create the decoder configuration structure */
277 p_dec = CreateDecoder( p_parent, p_input, fmt,
278 p_sout != NULL, p_resource, p_sout );
281 msg_Err( p_parent, "could not create %s", psz_type );
282 dialog_Fatal( p_parent, _("Streaming / Transcoding failed"),
283 _("VLC could not open the %s module."),
284 vlc_gettext( psz_type ) );
288 if( !p_dec->p_module )
290 DecoderUnsupportedCodec( p_dec, fmt->i_codec );
292 DeleteDecoder( p_dec );
296 p_dec->p_owner->p_clock = p_clock;
297 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
299 if( p_dec->fmt_out.i_cat == AUDIO_ES )
300 i_priority = VLC_THREAD_PRIORITY_AUDIO;
302 i_priority = VLC_THREAD_PRIORITY_VIDEO;
304 /* Spawn the decoder thread */
305 if( vlc_clone( &p_dec->p_owner->thread, DecoderThread, p_dec, i_priority ) )
307 msg_Err( p_dec, "cannot spawn decoder thread" );
308 module_unneed( p_dec, p_dec->p_module );
309 DeleteDecoder( p_dec );
318 * Spawns a new decoder thread from the input thread
320 * \param p_input the input thread
321 * \param p_es the es descriptor
322 * \return the spawned decoder object
324 decoder_t *input_DecoderNew( input_thread_t *p_input,
325 es_format_t *fmt, input_clock_t *p_clock,
326 sout_instance_t *p_sout )
328 return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
329 p_input->p->p_resource, p_sout );
333 * Spawn a decoder thread outside of the input thread.
335 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, es_format_t *fmt,
336 input_resource_t *p_resource )
338 return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
343 * Kills a decoder thread and waits until it's finished
345 * \param p_input the input thread
346 * \param p_es the es descriptor
349 void input_DecoderDelete( decoder_t *p_dec )
351 decoder_owner_sys_t *p_owner = p_dec->p_owner;
353 vlc_cancel( p_owner->thread );
355 /* Make sure we aren't paused/buffering/waiting/decoding anymore */
356 vlc_mutex_lock( &p_owner->lock );
357 const bool b_was_paused = p_owner->b_paused;
358 p_owner->b_paused = false;
359 p_owner->b_buffering = false;
360 p_owner->b_flushing = true;
361 p_owner->b_exit = true;
362 vlc_cond_signal( &p_owner->wait_request );
363 vlc_mutex_unlock( &p_owner->lock );
365 vlc_join( p_owner->thread, NULL );
366 p_owner->b_paused = b_was_paused;
368 module_unneed( p_dec, p_dec->p_module );
371 if( p_dec->p_owner->cc.b_supported )
374 for( i = 0; i < 4; i++ )
375 input_DecoderSetCcState( p_dec, false, i );
379 DeleteDecoder( p_dec );
383 * Put a block_t in the decoder's fifo.
384 * Thread-safe w.r.t. the decoder. May be a cancellation point.
386 * \param p_dec the decoder object
387 * \param p_block the data block
389 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
391 decoder_owner_sys_t *p_owner = p_dec->p_owner;
395 /* The fifo is not consummed when buffering and so will
397 * There is no need to lock as b_buffering is never modify
398 * inside decoder thread. */
399 if( !p_owner->b_buffering )
400 block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
403 else if( block_FifoSize( p_owner->p_fifo ) > 50*1024*1024 /* 50 MiB */ )
405 else if( block_FifoSize( p_owner->p_fifo ) > 400*1024*1024 /* 400 MiB, ie ~ 50mb/s for 60s */ )
408 /* FIXME: ideally we would check the time amount of data
409 * in the FIFO instead of its size. */
410 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
411 "consumed quickly enough), resetting fifo!" );
412 block_FifoEmpty( p_owner->p_fifo );
415 block_FifoPut( p_owner->p_fifo, p_block );
418 bool input_DecoderIsEmpty( decoder_t * p_dec )
420 decoder_owner_sys_t *p_owner = p_dec->p_owner;
421 assert( !p_owner->b_buffering );
423 bool b_empty = block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
426 vlc_mutex_lock( &p_owner->lock );
427 /* TODO subtitles support */
428 if( p_dec->fmt_out.i_cat == VIDEO_ES && p_owner->p_vout )
429 b_empty = vout_IsEmpty( p_owner->p_vout );
430 else if( p_dec->fmt_out.i_cat == AUDIO_ES && p_owner->p_aout )
431 b_empty = aout_DecIsEmpty( p_owner->p_aout, p_owner->p_aout_input );
432 vlc_mutex_unlock( &p_owner->lock );
437 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
439 decoder_owner_sys_t *p_owner = p_dec->p_owner;
442 vlc_mutex_lock( &p_owner->lock );
443 for( i = 0; i < 4; i++ )
444 pb_present[i] = p_owner->cc.pb_present[i];
445 vlc_mutex_unlock( &p_owner->lock );
447 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
449 decoder_owner_sys_t *p_owner = p_dec->p_owner;
451 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
453 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
458 static const vlc_fourcc_t fcc[4] = {
459 VLC_FOURCC('c', 'c', '1', ' '),
460 VLC_FOURCC('c', 'c', '2', ' '),
461 VLC_FOURCC('c', 'c', '3', ' '),
462 VLC_FOURCC('c', 'c', '4', ' '),
467 es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
468 p_cc = CreateDecoder( VLC_OBJECT(p_dec), p_owner->p_input, &fmt,
469 false, p_owner->p_resource, p_owner->p_sout );
472 msg_Err( p_dec, "could not create decoder" );
473 dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
474 _("VLC could not open the decoder module.") );
477 else if( !p_cc->p_module )
479 DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
480 DeleteDecoder( p_cc );
483 p_cc->p_owner->p_clock = p_owner->p_clock;
485 vlc_mutex_lock( &p_owner->lock );
486 p_owner->cc.pp_decoder[i_channel] = p_cc;
487 vlc_mutex_unlock( &p_owner->lock );
493 vlc_mutex_lock( &p_owner->lock );
494 p_cc = p_owner->cc.pp_decoder[i_channel];
495 p_owner->cc.pp_decoder[i_channel] = NULL;
496 vlc_mutex_unlock( &p_owner->lock );
500 module_unneed( p_cc, p_cc->p_module );
501 DeleteDecoder( p_cc );
506 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
508 decoder_owner_sys_t *p_owner = p_dec->p_owner;
511 if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
514 vlc_mutex_lock( &p_owner->lock );
515 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
516 vlc_mutex_unlock( &p_owner->lock );
520 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
522 decoder_owner_sys_t *p_owner = p_dec->p_owner;
524 vlc_mutex_lock( &p_owner->lock );
526 assert( !p_owner->b_paused || !b_paused );
528 p_owner->b_paused = b_paused;
529 p_owner->pause.i_date = i_date;
530 p_owner->pause.i_ignore = 0;
531 vlc_cond_signal( &p_owner->wait_request );
533 DecoderOutputChangePause( p_dec, b_paused, i_date );
535 vlc_mutex_unlock( &p_owner->lock );
538 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
540 decoder_owner_sys_t *p_owner = p_dec->p_owner;
542 vlc_mutex_lock( &p_owner->lock );
543 p_owner->i_ts_delay = i_delay;
544 vlc_mutex_unlock( &p_owner->lock );
547 void input_DecoderStartBuffering( decoder_t *p_dec )
549 decoder_owner_sys_t *p_owner = p_dec->p_owner;
551 vlc_mutex_lock( &p_owner->lock );
553 DecoderFlush( p_dec );
555 p_owner->buffer.b_first = true;
556 p_owner->buffer.b_full = false;
557 p_owner->buffer.i_count = 0;
559 assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
560 !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
562 p_owner->buffer.p_picture = NULL;
563 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
565 p_owner->buffer.p_subpic = NULL;
566 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
568 p_owner->buffer.p_audio = NULL;
569 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
571 p_owner->buffer.p_block = NULL;
572 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
575 p_owner->b_buffering = true;
577 vlc_cond_signal( &p_owner->wait_request );
579 vlc_mutex_unlock( &p_owner->lock );
582 void input_DecoderStopBuffering( decoder_t *p_dec )
584 decoder_owner_sys_t *p_owner = p_dec->p_owner;
586 vlc_mutex_lock( &p_owner->lock );
588 p_owner->b_buffering = false;
590 vlc_cond_signal( &p_owner->wait_request );
592 vlc_mutex_unlock( &p_owner->lock );
595 void input_DecoderWaitBuffering( decoder_t *p_dec )
597 decoder_owner_sys_t *p_owner = p_dec->p_owner;
599 vlc_mutex_lock( &p_owner->lock );
601 while( p_owner->b_buffering && !p_owner->buffer.b_full )
603 block_FifoWake( p_owner->p_fifo );
604 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
607 vlc_mutex_unlock( &p_owner->lock );
610 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
612 decoder_owner_sys_t *p_owner = p_dec->p_owner;
616 vlc_mutex_lock( &p_owner->lock );
617 if( p_dec->fmt_out.i_cat == VIDEO_ES )
619 if( p_owner->b_paused && p_owner->p_vout )
621 vout_NextPicture( p_owner->p_vout, pi_duration );
622 p_owner->pause.i_ignore++;
623 vlc_cond_signal( &p_owner->wait_request );
628 /* TODO subtitle should not be flushed */
629 DecoderFlush( p_dec );
631 vlc_mutex_unlock( &p_owner->lock );
634 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
636 decoder_owner_sys_t *p_owner = p_dec->p_owner;
639 vlc_mutex_lock( &p_owner->lock );
640 b_changed = p_owner->b_fmt_description;
644 es_format_Copy( p_fmt, &p_owner->fmt_description );
649 if( p_owner->p_description )
651 *pp_meta = vlc_meta_New();
653 vlc_meta_Merge( *pp_meta, p_owner->p_description );
656 p_owner->b_fmt_description = false;
658 vlc_mutex_unlock( &p_owner->lock );
662 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
664 decoder_owner_sys_t *p_owner = p_dec->p_owner;
666 return block_FifoSize( p_owner->p_fifo );
669 void input_DecoderGetObjects( decoder_t *p_dec,
670 vout_thread_t **pp_vout, aout_instance_t **pp_aout )
672 decoder_owner_sys_t *p_owner = p_dec->p_owner;
674 vlc_mutex_lock( &p_owner->lock );
676 *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
678 *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
679 vlc_mutex_unlock( &p_owner->lock );
682 /*****************************************************************************
684 *****************************************************************************/
685 static int DecoderGetInputAttachments( decoder_t *p_dec,
686 input_attachment_t ***ppp_attachment,
689 input_thread_t *p_input = p_dec->p_owner->p_input;
691 if( unlikely(p_input == NULL) )
693 return input_Control( p_input, INPUT_GET_ATTACHMENTS,
694 ppp_attachment, pi_attachment );
696 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
698 decoder_owner_sys_t *p_owner = p_dec->p_owner;
700 vlc_mutex_lock( &p_owner->lock );
701 if( p_owner->b_buffering || p_owner->b_paused )
702 i_ts = VLC_TS_INVALID;
703 vlc_mutex_unlock( &p_owner->lock );
705 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
708 if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
709 return VLC_TS_INVALID;
713 static int DecoderGetDisplayRate( decoder_t *p_dec )
715 decoder_owner_sys_t *p_owner = p_dec->p_owner;
717 if( !p_owner->p_clock )
718 return INPUT_RATE_DEFAULT;
719 return input_clock_GetRate( p_owner->p_clock );
723 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
725 msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'. "
726 "VLC probably does not support this sound or video format.",
728 dialog_Fatal( p_dec, _("No suitable decoder module"),
729 _("VLC does not support the audio or video format \"%4.4s\". "
730 "Unfortunately there is no way for you to fix this."),
736 * Create a decoder object
738 * \param p_input the input thread
739 * \param p_es the es descriptor
740 * \param b_packetizer instead of a decoder
741 * \return the decoder object
743 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
744 input_thread_t *p_input,
745 es_format_t *fmt, bool b_packetizer,
746 input_resource_t *p_resource,
747 sout_instance_t *p_sout )
750 decoder_owner_sys_t *p_owner;
751 es_format_t null_es_format;
753 p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
757 p_dec->pf_decode_audio = NULL;
758 p_dec->pf_decode_video = NULL;
759 p_dec->pf_decode_sub = NULL;
760 p_dec->pf_get_cc = NULL;
761 p_dec->pf_packetize = NULL;
763 /* Initialize the decoder */
764 p_dec->p_module = NULL;
766 memset( &null_es_format, 0, sizeof(es_format_t) );
767 es_format_Copy( &p_dec->fmt_in, fmt );
768 es_format_Copy( &p_dec->fmt_out, &null_es_format );
770 p_dec->p_description = NULL;
772 /* Allocate our private structure for the decoder */
773 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
774 if( unlikely(p_owner == NULL) )
776 vlc_object_release( p_dec );
779 p_owner->i_preroll_end = VLC_TS_INVALID;
780 p_owner->i_last_rate = INPUT_RATE_DEFAULT;
781 p_owner->p_input = p_input;
782 p_owner->p_resource = p_resource;
783 p_owner->p_aout = NULL;
784 p_owner->p_aout_input = NULL;
785 p_owner->p_vout = NULL;
786 p_owner->p_spu_vout = NULL;
787 p_owner->i_spu_channel = 0;
788 p_owner->i_spu_order = 0;
789 p_owner->p_sout = p_sout;
790 p_owner->p_sout_input = NULL;
791 p_owner->p_packetizer = NULL;
792 p_owner->b_packetizer = b_packetizer;
795 p_owner->p_fifo = block_FifoNew();
796 if( unlikely(p_owner->p_fifo == NULL) )
799 vlc_object_release( p_dec );
803 /* Set buffers allocation callbacks for the decoders */
804 p_dec->pf_aout_buffer_new = aout_new_buffer;
805 p_dec->pf_aout_buffer_del = aout_del_buffer;
806 p_dec->pf_vout_buffer_new = vout_new_buffer;
807 p_dec->pf_vout_buffer_del = vout_del_buffer;
808 p_dec->pf_picture_link = vout_link_picture;
809 p_dec->pf_picture_unlink = vout_unlink_picture;
810 p_dec->pf_spu_buffer_new = spu_new_buffer;
811 p_dec->pf_spu_buffer_del = spu_del_buffer;
813 p_dec->pf_get_attachments = DecoderGetInputAttachments;
814 p_dec->pf_get_display_date = DecoderGetDisplayDate;
815 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
817 /* Find a suitable decoder/packetizer module */
819 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
821 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
823 /* Check if decoder requires already packetized data */
825 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
827 p_owner->p_packetizer =
828 vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
829 if( p_owner->p_packetizer )
831 es_format_Copy( &p_owner->p_packetizer->fmt_in,
834 es_format_Copy( &p_owner->p_packetizer->fmt_out,
837 p_owner->p_packetizer->p_module =
838 module_need( p_owner->p_packetizer,
839 "packetizer", "$packetizer", false );
841 if( !p_owner->p_packetizer->p_module )
843 es_format_Clean( &p_owner->p_packetizer->fmt_in );
844 vlc_object_release( p_owner->p_packetizer );
849 /* Copy ourself the input replay gain */
850 if( fmt->i_cat == AUDIO_ES )
852 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
854 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
856 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
857 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
859 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
861 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
862 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
868 vlc_mutex_init( &p_owner->lock );
869 vlc_cond_init( &p_owner->wait_request );
870 vlc_cond_init( &p_owner->wait_acknowledge );
872 p_owner->b_fmt_description = false;
873 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
874 p_owner->p_description = NULL;
876 p_owner->b_exit = false;
878 p_owner->b_paused = false;
879 p_owner->pause.i_date = VLC_TS_INVALID;
880 p_owner->pause.i_ignore = 0;
882 p_owner->b_buffering = false;
883 p_owner->buffer.b_first = true;
884 p_owner->buffer.b_full = false;
885 p_owner->buffer.i_count = 0;
886 p_owner->buffer.p_picture = NULL;
887 p_owner->buffer.p_subpic = NULL;
888 p_owner->buffer.p_audio = NULL;
889 p_owner->buffer.p_block = NULL;
891 p_owner->b_flushing = false;
894 p_owner->cc.b_supported = false;
897 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
898 p_owner->cc.b_supported = true;
899 if( p_dec->pf_get_cc )
900 p_owner->cc.b_supported = true;
903 for( unsigned i = 0; i < 4; i++ )
905 p_owner->cc.pb_present[i] = false;
906 p_owner->cc.pp_decoder[i] = NULL;
908 p_owner->i_ts_delay = 0;
913 * The decoding main loop
915 * \param p_dec the decoder
917 static void *DecoderThread( void *p_data )
919 decoder_t *p_dec = (decoder_t *)p_data;
920 decoder_owner_sys_t *p_owner = p_dec->p_owner;
922 /* The decoder's main loop */
925 block_t *p_block = block_FifoGet( p_owner->p_fifo );
927 /* Make sure there is no cancellation point other than this one^^.
928 * If you need one, be sure to push cleanup of p_block. */
929 DecoderSignalBuffering( p_dec, p_block == NULL );
933 int canc = vlc_savecancel();
936 DecoderError( p_dec, p_block );
938 DecoderProcess( p_dec, p_block );
940 vlc_restorecancel( canc );
946 static block_t *DecoderBlockFlushNew()
948 block_t *p_null = block_Alloc( 128 );
952 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
953 BLOCK_FLAG_CORRUPTED |
954 BLOCK_FLAG_CORE_FLUSH;
955 memset( p_null->p_buffer, 0, p_null->i_buffer );
960 static void DecoderFlush( decoder_t *p_dec )
962 decoder_owner_sys_t *p_owner = p_dec->p_owner;
964 vlc_assert_locked( &p_owner->lock );
967 block_FifoEmpty( p_owner->p_fifo );
969 /* Monitor for flush end */
970 p_owner->b_flushing = true;
971 vlc_cond_signal( &p_owner->wait_request );
973 /* Send a special block */
974 block_t *p_null = DecoderBlockFlushNew();
977 input_DecoderDecode( p_dec, p_null, false );
980 while( p_owner->b_flushing )
981 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
984 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
986 decoder_owner_sys_t *p_owner = p_dec->p_owner;
988 vlc_mutex_lock( &p_owner->lock );
990 if( p_owner->b_buffering )
993 p_owner->buffer.b_full = true;
994 vlc_cond_signal( &p_owner->wait_acknowledge );
997 vlc_mutex_unlock( &p_owner->lock );
1000 static bool DecoderIsFlushing( decoder_t *p_dec )
1002 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1005 vlc_mutex_lock( &p_owner->lock );
1007 b_flushing = p_owner->b_flushing;
1009 vlc_mutex_unlock( &p_owner->lock );
1014 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
1016 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1018 vlc_assert_locked( &p_owner->lock );
1020 while( !p_owner->b_flushing )
1022 if( p_owner->b_paused )
1024 if( p_owner->b_buffering && !p_owner->buffer.b_full )
1026 if( p_owner->pause.i_ignore > 0 )
1028 p_owner->pause.i_ignore--;
1034 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
1037 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
1041 *pb_reject = p_owner->b_flushing;
1044 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
1046 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1048 vlc_assert_locked( &p_owner->lock );
1050 /* XXX only audio and video output have to be paused.
1051 * - for sout it is useless
1052 * - for subs, it is done by the vout
1054 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1056 if( p_owner->p_aout )
1057 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1060 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1062 if( p_owner->p_vout )
1063 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1066 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1068 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1069 *pi_preroll = INT64_MAX;
1070 else if( p->i_dts > VLC_TS_INVALID )
1071 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1072 else if( p->i_pts > VLC_TS_INVALID )
1073 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1076 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1078 mtime_t current_date = mdate();
1080 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1081 if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1083 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1084 * for teletext streams. */
1085 return current_date + 400000;
1090 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1091 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1093 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1094 input_clock_t *p_clock = p_owner->p_clock;
1096 vlc_assert_locked( &p_owner->lock );
1098 const mtime_t i_es_delay = p_owner->i_ts_delay;
1102 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1105 if( *pi_ts0 > VLC_TS_INVALID )
1107 *pi_ts0 += i_es_delay;
1108 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1109 *pi_ts1 += i_es_delay;
1110 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1111 *pi_ts0 = VLC_TS_INVALID;
1115 i_rate = input_clock_GetRate( p_clock );
1118 /* Do not create ephemere data because of rounding errors */
1119 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1123 *pi_duration = ( *pi_duration * i_rate +
1124 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1131 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1132 if( pi_ts1 && *pi_ts1 <= VLC_TS_INVALID )
1138 static bool DecoderIsExitRequested( decoder_t *p_dec )
1140 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1142 vlc_mutex_lock( &p_owner->lock );
1143 bool b_exit = p_owner->b_exit;
1144 vlc_mutex_unlock( &p_owner->lock );
1150 * If *pb_reject, it does nothing, otherwise it waits for the given
1151 * deadline or a flush request (in which case it set *pi_reject to true.
1153 static void DecoderWaitDate( decoder_t *p_dec,
1154 bool *pb_reject, mtime_t i_deadline )
1156 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1158 if( *pb_reject || i_deadline < 0 )
1163 vlc_mutex_lock( &p_owner->lock );
1164 if( p_owner->b_flushing || p_owner->b_exit )
1167 vlc_mutex_unlock( &p_owner->lock );
1170 int i_ret = vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1172 vlc_mutex_unlock( &p_owner->lock );
1178 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1179 int *pi_played_sum, int *pi_lost_sum )
1181 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1182 aout_instance_t *p_aout = p_owner->p_aout;
1183 aout_input_t *p_aout_input = p_owner->p_aout_input;
1186 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1188 msg_Warn( p_dec, "non-dated audio buffer received" );
1190 aout_BufferFree( p_audio );
1195 vlc_mutex_lock( &p_owner->lock );
1197 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1199 p_audio->p_next = NULL;
1201 *p_owner->buffer.pp_audio_next = p_audio;
1202 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1204 p_owner->buffer.i_count++;
1205 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1206 p_audio->i_pts - p_owner->buffer.p_audio->i_pts > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1208 p_owner->buffer.b_full = true;
1209 vlc_cond_signal( &p_owner->wait_acknowledge );
1215 bool b_has_more = false;
1217 DecoderWaitUnblock( p_dec, &b_reject );
1219 if( p_owner->b_buffering )
1221 vlc_mutex_unlock( &p_owner->lock );
1226 if( p_owner->buffer.p_audio )
1228 p_audio = p_owner->buffer.p_audio;
1230 p_owner->buffer.p_audio = p_audio->p_next;
1231 p_owner->buffer.i_count--;
1233 b_has_more = p_owner->buffer.p_audio != NULL;
1235 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1239 const bool b_dated = p_audio->i_pts > VLC_TS_INVALID;
1240 int i_rate = INPUT_RATE_DEFAULT;
1242 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1243 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1245 vlc_mutex_unlock( &p_owner->lock );
1247 if( !p_aout || !p_aout_input ||
1248 p_audio->i_pts <= VLC_TS_INVALID ||
1249 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1250 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1253 DecoderWaitDate( p_dec, &b_reject,
1254 p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1258 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1259 *pi_played_sum += 1;
1260 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1265 msg_Warn( p_dec, "received buffer in the future" );
1267 msg_Warn( p_dec, "non-dated audio buffer received" );
1270 aout_BufferFree( p_audio );
1276 vlc_mutex_lock( &p_owner->lock );
1277 if( !p_owner->buffer.p_audio )
1279 vlc_mutex_unlock( &p_owner->lock );
1285 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1287 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1288 aout_buffer_t *p_aout_buf;
1293 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1295 aout_instance_t *p_aout = p_owner->p_aout;
1296 aout_input_t *p_aout_input = p_owner->p_aout_input;
1298 if( DecoderIsExitRequested( p_dec ) )
1300 /* It prevent freezing VLC in case of broken decoder */
1301 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1303 block_Release( p_block );
1308 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1309 p_aout_buf->i_pts < p_owner->i_preroll_end )
1311 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1315 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1317 msg_Dbg( p_dec, "End of audio preroll" );
1318 if( p_owner->p_aout && p_owner->p_aout_input )
1319 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1321 p_owner->i_preroll_end = VLC_TS_INVALID;
1324 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1327 /* Update ugly stat */
1328 input_thread_t *p_input = p_owner->p_input;
1330 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1332 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1334 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1336 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1338 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1341 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1344 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1346 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1349 bool b_processed = false;
1353 assert( p_dec_cc->pf_get_cc != NULL );
1355 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1356 if( !p_owner->cc.b_supported )
1359 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1363 vlc_mutex_lock( &p_owner->lock );
1364 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1366 p_owner->cc.pb_present[i] |= pb_present[i];
1367 if( p_owner->cc.pp_decoder[i] )
1371 for( i = 0; i < 4; i++ )
1373 if( !p_owner->cc.pp_decoder[i] )
1376 if( i_cc_decoder > 1 )
1377 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1379 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1383 vlc_mutex_unlock( &p_owner->lock );
1386 block_Release( p_cc );
1389 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1390 int *pi_played_sum, int *pi_lost_sum )
1392 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1393 vout_thread_t *p_vout = p_owner->p_vout;
1394 bool b_first_buffered;
1396 if( p_picture->date <= VLC_TS_INVALID )
1398 msg_Warn( p_dec, "non-dated video buffer received" );
1400 vout_ReleasePicture( p_vout, p_picture );
1405 vlc_mutex_lock( &p_owner->lock );
1407 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1409 p_picture->p_next = NULL;
1411 *p_owner->buffer.pp_picture_next = p_picture;
1412 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1414 p_owner->buffer.i_count++;
1415 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1416 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1418 p_owner->buffer.b_full = true;
1419 vlc_cond_signal( &p_owner->wait_acknowledge );
1422 b_first_buffered = p_owner->buffer.p_picture != NULL;
1424 for( ;; b_first_buffered = false )
1426 bool b_has_more = false;
1430 DecoderWaitUnblock( p_dec, &b_reject );
1432 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1434 vlc_mutex_unlock( &p_owner->lock );
1437 bool b_buffering_first = p_owner->b_buffering;
1440 if( p_owner->buffer.p_picture )
1442 p_picture = p_owner->buffer.p_picture;
1444 p_owner->buffer.p_picture = p_picture->p_next;
1445 p_owner->buffer.i_count--;
1447 b_has_more = p_owner->buffer.p_picture != NULL;
1449 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1453 if( b_buffering_first )
1455 assert( p_owner->buffer.b_first );
1456 assert( !p_owner->buffer.i_count );
1457 msg_Dbg( p_dec, "Received first picture" );
1458 p_owner->buffer.b_first = false;
1459 p_picture->b_force = true;
1462 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1463 int i_rate = INPUT_RATE_DEFAULT;
1464 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1465 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1467 vlc_mutex_unlock( &p_owner->lock );
1470 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1475 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1477 /* Be sure to not display old picture after our own */
1478 vout_Flush( p_vout, p_picture->date );
1479 p_owner->i_last_rate = i_rate;
1481 vout_PutPicture( p_vout, p_picture );
1486 msg_Warn( p_dec, "early picture skipped" );
1488 msg_Warn( p_dec, "non-dated video buffer received" );
1491 vout_ReleasePicture( p_vout, p_picture );
1495 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1497 *pi_played_sum += i_tmp_display;
1498 *pi_lost_sum += i_tmp_lost;
1500 if( !b_has_more || b_buffering_first )
1503 vlc_mutex_lock( &p_owner->lock );
1504 if( !p_owner->buffer.p_picture )
1506 vlc_mutex_unlock( &p_owner->lock );
1512 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1514 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1518 int i_displayed = 0;
1520 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1522 vout_thread_t *p_vout = p_owner->p_vout;
1523 if( DecoderIsExitRequested( p_dec ) )
1525 /* It prevent freezing VLC in case of broken decoder */
1526 vout_ReleasePicture( p_vout, p_pic );
1528 block_Release( p_block );
1534 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1536 vout_ReleasePicture( p_vout, p_pic );
1540 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1542 msg_Dbg( p_dec, "End of video preroll" );
1544 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1546 p_owner->i_preroll_end = VLC_TS_INVALID;
1549 if( p_dec->pf_get_cc &&
1550 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1551 DecoderGetCc( p_dec, p_dec );
1553 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1556 /* Update ugly stat */
1557 input_thread_t *p_input = p_owner->p_input;
1559 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
1561 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1563 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1565 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1568 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1571 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1575 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1578 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1579 vout_thread_t *p_vout = p_owner->p_spu_vout;
1582 if( p_subpic->i_start <= VLC_TS_INVALID && !b_telx )
1584 msg_Warn( p_dec, "non-dated spu buffer received" );
1585 subpicture_Delete( p_subpic );
1590 vlc_mutex_lock( &p_owner->lock );
1592 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1594 p_subpic->p_next = NULL;
1596 *p_owner->buffer.pp_subpic_next = p_subpic;
1597 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1599 p_owner->buffer.i_count++;
1600 /* XXX it is important to be full after the first one */
1601 if( p_owner->buffer.i_count > 0 )
1603 p_owner->buffer.b_full = true;
1604 vlc_cond_signal( &p_owner->wait_acknowledge );
1610 bool b_has_more = false;
1612 DecoderWaitUnblock( p_dec, &b_reject );
1614 if( p_owner->b_buffering )
1616 vlc_mutex_unlock( &p_owner->lock );
1621 if( p_owner->buffer.p_subpic )
1623 p_subpic = p_owner->buffer.p_subpic;
1625 p_owner->buffer.p_subpic = p_subpic->p_next;
1626 p_owner->buffer.i_count--;
1628 b_has_more = p_owner->buffer.p_subpic != NULL;
1630 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1634 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1635 NULL, INT64_MAX, b_telx );
1637 vlc_mutex_unlock( &p_owner->lock );
1639 if( p_subpic->i_start <= VLC_TS_INVALID )
1642 DecoderWaitDate( p_dec, &b_reject,
1643 p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1646 vout_PutSubpicture( p_vout, p_subpic );
1648 subpicture_Delete( p_subpic );
1652 vlc_mutex_lock( &p_owner->lock );
1653 if( !p_owner->buffer.p_subpic )
1655 vlc_mutex_unlock( &p_owner->lock );
1662 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1665 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1667 assert( p_owner->p_clock );
1668 assert( !p_sout_block->p_next );
1670 vlc_mutex_lock( &p_owner->lock );
1672 if( p_owner->b_buffering || p_owner->buffer.p_block )
1674 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1676 p_owner->buffer.i_count++;
1677 /* XXX it is important to be full after the first one */
1678 if( p_owner->buffer.i_count > 0 )
1680 p_owner->buffer.b_full = true;
1681 vlc_cond_signal( &p_owner->wait_acknowledge );
1687 bool b_has_more = false;
1689 DecoderWaitUnblock( p_dec, &b_reject );
1691 if( p_owner->b_buffering )
1693 vlc_mutex_unlock( &p_owner->lock );
1698 if( p_owner->buffer.p_block )
1700 p_sout_block = p_owner->buffer.p_block;
1702 p_owner->buffer.p_block = p_sout_block->p_next;
1703 p_owner->buffer.i_count--;
1705 b_has_more = p_owner->buffer.p_block != NULL;
1707 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1709 p_sout_block->p_next = NULL;
1711 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1712 &p_sout_block->i_length,
1713 &p_sout_block->i_rate, INT64_MAX, b_telx );
1715 vlc_mutex_unlock( &p_owner->lock );
1718 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1720 block_Release( p_sout_block );
1724 vlc_mutex_lock( &p_owner->lock );
1725 if( !p_owner->buffer.p_block )
1727 vlc_mutex_unlock( &p_owner->lock );
1735 static void DecoderFlushBuffering( decoder_t *p_dec )
1737 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1739 vlc_assert_locked( &p_owner->lock );
1741 while( p_owner->buffer.p_picture )
1743 picture_t *p_picture = p_owner->buffer.p_picture;
1745 p_owner->buffer.p_picture = p_picture->p_next;
1746 p_owner->buffer.i_count--;
1748 if( p_owner->p_vout )
1750 vout_ReleasePicture( p_owner->p_vout, p_picture );
1753 if( !p_owner->buffer.p_picture )
1754 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1756 while( p_owner->buffer.p_audio )
1758 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1760 p_owner->buffer.p_audio = p_audio->p_next;
1761 p_owner->buffer.i_count--;
1763 aout_BufferFree( p_audio );
1765 if( !p_owner->buffer.p_audio )
1766 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1768 while( p_owner->buffer.p_subpic )
1770 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1772 p_owner->buffer.p_subpic = p_subpic->p_next;
1773 p_owner->buffer.i_count--;
1775 subpicture_Delete( p_subpic );
1777 if( !p_owner->buffer.p_subpic )
1778 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1780 if( p_owner->buffer.p_block )
1782 block_ChainRelease( p_owner->buffer.p_block );
1784 p_owner->buffer.i_count = 0;
1785 p_owner->buffer.p_block = NULL;
1786 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1791 /* This function process a block for sout
1793 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1795 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1796 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1797 block_t *p_sout_block;
1799 while( ( p_sout_block =
1800 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1802 if( !p_owner->p_sout_input )
1804 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1806 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1807 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1808 if( p_dec->fmt_in.psz_language )
1810 free( p_owner->sout.psz_language );
1811 p_owner->sout.psz_language =
1812 strdup( p_dec->fmt_in.psz_language );
1815 p_owner->p_sout_input =
1816 sout_InputNew( p_owner->p_sout,
1819 if( p_owner->p_sout_input == NULL )
1821 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1822 (char *)&p_owner->sout.i_codec );
1823 p_dec->b_error = true;
1825 while( p_sout_block )
1827 block_t *p_next = p_sout_block->p_next;
1828 block_Release( p_sout_block );
1829 p_sout_block = p_next;
1835 while( p_sout_block )
1837 block_t *p_next = p_sout_block->p_next;
1839 p_sout_block->p_next = NULL;
1841 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1843 p_sout_block = p_next;
1849 /* This function process a video block
1851 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1853 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1855 if( p_owner->p_packetizer )
1857 block_t *p_packetized_block;
1858 decoder_t *p_packetizer = p_owner->p_packetizer;
1860 while( (p_packetized_block =
1861 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1863 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1865 es_format_Clean( &p_dec->fmt_in );
1866 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1868 if( p_packetizer->pf_get_cc )
1869 DecoderGetCc( p_dec, p_packetizer );
1871 while( p_packetized_block )
1873 block_t *p_next = p_packetized_block->p_next;
1874 p_packetized_block->p_next = NULL;
1876 DecoderDecodeVideo( p_dec, p_packetized_block );
1878 p_packetized_block = p_next;
1881 /* The packetizer does not output a block that tell the decoder to flush
1885 block_t *p_null = DecoderBlockFlushNew();
1887 DecoderDecodeVideo( p_dec, p_null );
1892 DecoderDecodeVideo( p_dec, p_block );
1895 if( b_flush && p_owner->p_vout )
1896 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1899 /* This function process a audio block
1901 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1903 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1905 if( p_owner->p_packetizer )
1907 block_t *p_packetized_block;
1908 decoder_t *p_packetizer = p_owner->p_packetizer;
1910 while( (p_packetized_block =
1911 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1913 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1915 es_format_Clean( &p_dec->fmt_in );
1916 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1919 while( p_packetized_block )
1921 block_t *p_next = p_packetized_block->p_next;
1922 p_packetized_block->p_next = NULL;
1924 DecoderDecodeAudio( p_dec, p_packetized_block );
1926 p_packetized_block = p_next;
1929 /* The packetizer does not output a block that tell the decoder to flush
1933 block_t *p_null = DecoderBlockFlushNew();
1935 DecoderDecodeAudio( p_dec, p_null );
1940 DecoderDecodeAudio( p_dec, p_block );
1943 if( b_flush && p_owner->p_aout )
1944 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1947 /* This function process a subtitle block
1949 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1951 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1952 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1954 input_thread_t *p_input = p_owner->p_input;
1955 vout_thread_t *p_vout;
1956 subpicture_t *p_spu;
1958 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1960 if( p_input != NULL )
1962 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1963 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1,
1965 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1968 p_vout = input_resource_HoldVout( p_owner->p_resource );
1969 if( p_vout && p_owner->p_spu_vout == p_vout )
1971 /* Preroll does not work very well with subtitle */
1972 if( p_spu->i_start > VLC_TS_INVALID &&
1973 p_spu->i_start < p_owner->i_preroll_end &&
1974 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1976 subpicture_Delete( p_spu );
1980 DecoderPlaySpu( p_dec, p_spu, b_telx );
1985 subpicture_Delete( p_spu );
1988 vlc_object_release( p_vout );
1991 if( b_flush && p_owner->p_spu_vout )
1993 p_vout = input_resource_HoldVout( p_owner->p_resource );
1995 if( p_vout && p_owner->p_spu_vout == p_vout )
1996 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1999 vlc_object_release( p_vout );
2004 static void DecoderProcessOnFlush( decoder_t *p_dec )
2006 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2008 vlc_mutex_lock( &p_owner->lock );
2009 DecoderFlushBuffering( p_dec );
2011 if( p_owner->b_flushing )
2013 p_owner->b_flushing = false;
2014 vlc_cond_signal( &p_owner->wait_acknowledge );
2016 vlc_mutex_unlock( &p_owner->lock );
2022 * \param p_dec the decoder object
2023 * \param p_block the block to decode
2024 * \return VLC_SUCCESS or an error code
2026 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
2028 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
2029 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2031 if( p_block && p_block->i_buffer <= 0 )
2033 assert( !b_flush_request );
2034 block_Release( p_block );
2039 if( p_owner->b_packetizer )
2042 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2044 DecoderProcessSout( p_dec, p_block );
2049 bool b_flush = false;
2053 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
2054 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
2056 b_flush = !b_flushing && b_flush_request;
2058 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2061 if( p_dec->fmt_out.i_cat == AUDIO_ES )
2063 DecoderProcessAudio( p_dec, p_block, b_flush );
2065 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
2067 DecoderProcessVideo( p_dec, p_block, b_flush );
2069 else if( p_dec->fmt_out.i_cat == SPU_ES )
2071 DecoderProcessSpu( p_dec, p_block, b_flush );
2075 msg_Err( p_dec, "unknown ES format" );
2076 p_dec->b_error = true;
2081 if( b_flush_request )
2082 DecoderProcessOnFlush( p_dec );
2085 static void DecoderError( decoder_t *p_dec, block_t *p_block )
2087 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2091 block_Release( p_block );
2093 if( b_flush_request )
2094 DecoderProcessOnFlush( p_dec );
2099 * Destroys a decoder object
2101 * \param p_dec the decoder object
2104 static void DeleteDecoder( decoder_t * p_dec )
2106 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2108 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2109 (char*)&p_dec->fmt_in.i_codec,
2110 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2112 /* Free all packets still in the decoder fifo. */
2113 block_FifoEmpty( p_owner->p_fifo );
2114 block_FifoRelease( p_owner->p_fifo );
2117 vlc_mutex_lock( &p_owner->lock );
2118 DecoderFlushBuffering( p_dec );
2119 vlc_mutex_unlock( &p_owner->lock );
2122 if( p_owner->p_aout )
2124 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2125 input_resource_RequestAout( p_owner->p_resource, p_owner->p_aout );
2126 if( p_owner->p_input != NULL )
2127 input_SendEventAout( p_owner->p_input );
2129 if( p_owner->p_vout )
2131 /* Hack to make sure all the the pictures are freed by the decoder
2132 * and that the vout is not paused anymore */
2133 vout_Reset( p_owner->p_vout );
2136 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
2138 if( p_owner->p_input != NULL )
2139 input_SendEventVout( p_owner->p_input );
2143 if( p_owner->p_sout_input )
2145 sout_InputDelete( p_owner->p_sout_input );
2146 es_format_Clean( &p_owner->sout );
2150 if( p_dec->fmt_out.i_cat == SPU_ES )
2152 vout_thread_t *p_vout;
2154 p_vout = input_resource_HoldVout( p_owner->p_resource );
2157 if( p_owner->p_spu_vout == p_vout )
2158 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
2159 vlc_object_release( p_vout );
2163 es_format_Clean( &p_dec->fmt_in );
2164 es_format_Clean( &p_dec->fmt_out );
2165 if( p_dec->p_description )
2166 vlc_meta_Delete( p_dec->p_description );
2167 es_format_Clean( &p_owner->fmt_description );
2168 if( p_owner->p_description )
2169 vlc_meta_Delete( p_owner->p_description );
2171 if( p_owner->p_packetizer )
2173 module_unneed( p_owner->p_packetizer,
2174 p_owner->p_packetizer->p_module );
2175 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2176 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2177 if( p_owner->p_packetizer->p_description )
2178 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2179 vlc_object_release( p_owner->p_packetizer );
2182 vlc_cond_destroy( &p_owner->wait_acknowledge );
2183 vlc_cond_destroy( &p_owner->wait_request );
2184 vlc_mutex_destroy( &p_owner->lock );
2186 vlc_object_release( p_dec );
2191 /*****************************************************************************
2192 * Buffers allocation callbacks for the decoders
2193 *****************************************************************************/
2194 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2196 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2198 vlc_assert_locked( &p_owner->lock );
2200 p_owner->b_fmt_description = true;
2202 /* Copy es_format */
2203 es_format_Clean( &p_owner->fmt_description );
2204 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2206 /* Move p_description */
2207 if( p_owner->p_description && p_dec->p_description )
2208 vlc_meta_Delete( p_owner->p_description );
2209 p_owner->p_description = p_dec->p_description;
2210 p_dec->p_description = NULL;
2212 static vout_thread_t *aout_request_vout( void *p_private,
2213 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2215 decoder_t *p_dec = p_private;
2216 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2217 input_thread_t *p_input = p_owner->p_input;
2219 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
2221 if( p_input != NULL )
2222 input_SendEventVout( p_input );
2227 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2229 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2230 aout_buffer_t *p_buffer;
2232 if( p_owner->p_aout_input != NULL &&
2233 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2234 p_dec->fmt_out.audio.i_original_channels !=
2235 p_owner->audio.i_original_channels ||
2236 p_dec->fmt_out.audio.i_bytes_per_frame !=
2237 p_owner->audio.i_bytes_per_frame ) )
2239 aout_input_t *p_aout_input = p_owner->p_aout_input;
2240 aout_instance_t *p_aout = p_owner->p_aout;
2242 /* Parameters changed, restart the aout */
2243 vlc_mutex_lock( &p_owner->lock );
2245 DecoderFlushBuffering( p_dec );
2247 aout_DecDelete( p_owner->p_aout, p_aout_input );
2248 p_owner->p_aout = NULL;
2249 p_owner->p_aout_input = NULL;
2251 vlc_mutex_unlock( &p_owner->lock );
2252 input_resource_RequestAout( p_owner->p_resource, p_aout );
2255 if( p_owner->p_aout_input == NULL )
2257 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
2258 audio_sample_format_t format;
2259 aout_input_t *p_aout_input;
2260 aout_instance_t *p_aout;
2261 aout_request_vout_t request_vout;
2263 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2264 p_owner->audio = p_dec->fmt_out.audio;
2266 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2267 if( i_force_dolby &&
2268 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2269 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2271 if( i_force_dolby == 1 )
2273 format.i_original_channels = format.i_original_channels |
2274 AOUT_CHAN_DOLBYSTEREO;
2276 else /* i_force_dolby == 2 */
2278 format.i_original_channels = format.i_original_channels &
2279 ~AOUT_CHAN_DOLBYSTEREO;
2283 request_vout.pf_request_vout = aout_request_vout;
2284 request_vout.p_private = p_dec;
2286 assert( p_owner->p_aout == NULL );
2287 p_aout = input_resource_RequestAout( p_owner->p_resource, NULL );
2290 p_aout_input = aout_DecNew( p_aout, &format,
2291 &p_dec->fmt_out.audio_replay_gain,
2293 if( p_aout_input == NULL )
2295 input_resource_RequestAout( p_owner->p_resource, p_aout );
2300 p_aout_input = NULL;
2302 vlc_mutex_lock( &p_owner->lock );
2304 p_owner->p_aout = p_aout;
2305 p_owner->p_aout_input = p_aout_input;
2306 DecoderUpdateFormatLocked( p_dec );
2308 vlc_mutex_unlock( &p_owner->lock );
2310 if( p_owner->p_input != NULL )
2311 input_SendEventAout( p_owner->p_input );
2313 if( p_owner->p_aout_input == NULL )
2315 msg_Err( p_dec, "failed to create audio output" );
2316 p_dec->b_error = true;
2319 p_dec->fmt_out.audio.i_bytes_per_frame =
2320 p_owner->audio.i_bytes_per_frame;
2323 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2328 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2330 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2332 aout_DecDeleteBuffer( p_owner->p_aout,
2333 p_owner->p_aout_input, p_buffer );
2336 static picture_t *vout_new_buffer( decoder_t *p_dec )
2338 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2340 if( p_owner->p_vout == NULL ||
2341 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2342 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2343 p_dec->fmt_out.video.i_visible_width != p_owner->video.i_visible_width ||
2344 p_dec->fmt_out.video.i_visible_height != p_owner->video.i_visible_height ||
2345 p_dec->fmt_out.video.i_x_offset != p_owner->video.i_x_offset ||
2346 p_dec->fmt_out.video.i_y_offset != p_owner->video.i_y_offset ||
2347 p_dec->fmt_out.i_codec != p_owner->video.i_chroma ||
2348 (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2349 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num )
2351 vout_thread_t *p_vout;
2353 if( !p_dec->fmt_out.video.i_width ||
2354 !p_dec->fmt_out.video.i_height )
2356 /* Can't create a new vout without display size */
2360 video_format_t fmt = p_dec->fmt_out.video;
2361 fmt.i_chroma = p_dec->fmt_out.i_codec;
2362 p_owner->video = fmt;
2364 if( !fmt.i_visible_width || !fmt.i_visible_height )
2366 if( p_dec->fmt_in.video.i_visible_width &&
2367 p_dec->fmt_in.video.i_visible_height )
2369 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
2370 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2374 fmt.i_visible_width = fmt.i_width;
2375 fmt.i_visible_height = fmt.i_height;
2379 if( fmt.i_visible_height == 1088 &&
2380 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2382 fmt.i_visible_height = 1080;
2383 if( !(fmt.i_sar_num % 136))
2385 fmt.i_sar_num *= 135;
2386 fmt.i_sar_den *= 136;
2388 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2391 if( !fmt.i_sar_num || !fmt.i_sar_den )
2397 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2398 fmt.i_sar_num, fmt.i_sar_den, 50000 );
2400 vlc_mutex_lock( &p_owner->lock );
2402 DecoderFlushBuffering( p_dec );
2404 p_vout = p_owner->p_vout;
2405 p_owner->p_vout = NULL;
2406 vlc_mutex_unlock( &p_owner->lock );
2409 switch( p_dec->fmt_in.i_codec )
2411 case VLC_CODEC_H264:
2412 case VLC_CODEC_DIRAC: /* FIXME valid ? */
2417 case VLC_CODEC_VP6F:
2425 p_vout = input_resource_RequestVout( p_owner->p_resource,
2427 dpb_size + 1 + DECODER_MAX_BUFFERING_COUNT,
2429 vlc_mutex_lock( &p_owner->lock );
2430 p_owner->p_vout = p_vout;
2432 DecoderUpdateFormatLocked( p_dec );
2434 vlc_mutex_unlock( &p_owner->lock );
2436 if( p_owner->p_input != NULL )
2437 input_SendEventVout( p_owner->p_input );
2438 if( p_vout == NULL )
2440 msg_Err( p_dec, "failed to create video output" );
2441 p_dec->b_error = true;
2446 /* Get a new picture
2450 if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2453 picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2457 if( DecoderIsFlushing( p_dec ) )
2461 DecoderSignalBuffering( p_dec, true );
2463 /* Check the decoder doesn't leak pictures */
2464 vout_FixLeaks( p_owner->p_vout );
2466 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2467 msleep( VOUT_OUTMEM_SLEEP );
2471 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2473 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2476 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2478 vout_HoldPicture( p_dec->p_owner->p_vout, p_pic );
2481 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2483 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2486 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2487 const subpicture_updater_t *p_updater )
2489 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2490 vout_thread_t *p_vout = NULL;
2491 subpicture_t *p_subpic;
2492 int i_attempts = 30;
2494 while( i_attempts-- )
2496 if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2499 p_vout = input_resource_HoldVout( p_owner->p_resource );
2503 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2508 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2512 if( p_owner->p_spu_vout != p_vout )
2514 vlc_mutex_lock( &p_owner->lock );
2516 DecoderFlushBuffering( p_dec );
2518 vlc_mutex_unlock( &p_owner->lock );
2520 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
2521 p_owner->i_spu_order = 0;
2522 p_owner->p_spu_vout = p_vout;
2525 p_subpic = subpicture_New( p_updater );
2528 p_subpic->i_channel = p_owner->i_spu_channel;
2529 p_subpic->i_order = p_owner->i_spu_order++;
2530 p_subpic->b_subtitle = true;
2533 vlc_object_release( p_vout );
2538 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2540 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2541 vout_thread_t *p_vout = NULL;
2543 p_vout = input_resource_HoldVout( p_owner->p_resource );
2544 if( !p_vout || p_owner->p_spu_vout != p_vout )
2547 vlc_object_release( p_vout );
2548 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2552 subpicture_Delete( p_subpic );
2554 vlc_object_release( p_vout );