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_InputIsEmpty( 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( vlc_object_alive( p_dec ) && 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 ), VLC_OBJECT_DECODER,
758 p_dec->pf_decode_audio = NULL;
759 p_dec->pf_decode_video = NULL;
760 p_dec->pf_decode_sub = NULL;
761 p_dec->pf_get_cc = NULL;
762 p_dec->pf_packetize = NULL;
764 /* Initialize the decoder */
765 p_dec->p_module = NULL;
767 memset( &null_es_format, 0, sizeof(es_format_t) );
768 es_format_Copy( &p_dec->fmt_in, fmt );
769 es_format_Copy( &p_dec->fmt_out, &null_es_format );
771 p_dec->p_description = NULL;
773 /* Allocate our private structure for the decoder */
774 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
775 if( unlikely(p_owner == NULL) )
777 vlc_object_release( p_dec );
780 p_owner->i_preroll_end = VLC_TS_INVALID;
781 p_owner->i_last_rate = INPUT_RATE_DEFAULT;
782 p_owner->p_input = p_input;
783 p_owner->p_resource = p_resource;
784 p_owner->p_aout = NULL;
785 p_owner->p_aout_input = NULL;
786 p_owner->p_vout = NULL;
787 p_owner->p_spu_vout = NULL;
788 p_owner->i_spu_channel = 0;
789 p_owner->i_spu_order = 0;
790 p_owner->p_sout = p_sout;
791 p_owner->p_sout_input = NULL;
792 p_owner->p_packetizer = NULL;
793 p_owner->b_packetizer = b_packetizer;
796 p_owner->p_fifo = block_FifoNew();
797 if( unlikely(p_owner->p_fifo == NULL) )
800 vlc_object_release( p_dec );
804 /* Set buffers allocation callbacks for the decoders */
805 p_dec->pf_aout_buffer_new = aout_new_buffer;
806 p_dec->pf_aout_buffer_del = aout_del_buffer;
807 p_dec->pf_vout_buffer_new = vout_new_buffer;
808 p_dec->pf_vout_buffer_del = vout_del_buffer;
809 p_dec->pf_picture_link = vout_link_picture;
810 p_dec->pf_picture_unlink = vout_unlink_picture;
811 p_dec->pf_spu_buffer_new = spu_new_buffer;
812 p_dec->pf_spu_buffer_del = spu_del_buffer;
814 p_dec->pf_get_attachments = DecoderGetInputAttachments;
815 p_dec->pf_get_display_date = DecoderGetDisplayDate;
816 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
818 vlc_object_attach( p_dec, p_parent );
820 /* Find a suitable decoder/packetizer module */
822 p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
824 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
826 /* Check if decoder requires already packetized data */
828 p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
830 p_owner->p_packetizer =
831 vlc_custom_create( p_dec, sizeof( decoder_t ),
832 VLC_OBJECT_DECODER, "packetizer" );
833 if( p_owner->p_packetizer )
835 es_format_Copy( &p_owner->p_packetizer->fmt_in,
838 es_format_Copy( &p_owner->p_packetizer->fmt_out,
841 vlc_object_attach( p_owner->p_packetizer, p_parent );
843 p_owner->p_packetizer->p_module =
844 module_need( p_owner->p_packetizer,
845 "packetizer", "$packetizer", false );
847 if( !p_owner->p_packetizer->p_module )
849 es_format_Clean( &p_owner->p_packetizer->fmt_in );
850 vlc_object_release( p_owner->p_packetizer );
855 /* Copy ourself the input replay gain */
856 if( fmt->i_cat == AUDIO_ES )
858 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
860 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
862 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
863 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
865 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
867 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
868 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
874 vlc_mutex_init( &p_owner->lock );
875 vlc_cond_init( &p_owner->wait_request );
876 vlc_cond_init( &p_owner->wait_acknowledge );
878 p_owner->b_fmt_description = false;
879 es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
880 p_owner->p_description = NULL;
882 p_owner->b_exit = false;
884 p_owner->b_paused = false;
885 p_owner->pause.i_date = VLC_TS_INVALID;
886 p_owner->pause.i_ignore = 0;
888 p_owner->b_buffering = false;
889 p_owner->buffer.b_first = true;
890 p_owner->buffer.b_full = false;
891 p_owner->buffer.i_count = 0;
892 p_owner->buffer.p_picture = NULL;
893 p_owner->buffer.p_subpic = NULL;
894 p_owner->buffer.p_audio = NULL;
895 p_owner->buffer.p_block = NULL;
897 p_owner->b_flushing = false;
900 p_owner->cc.b_supported = false;
903 if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
904 p_owner->cc.b_supported = true;
905 if( p_dec->pf_get_cc )
906 p_owner->cc.b_supported = true;
909 for( unsigned i = 0; i < 4; i++ )
911 p_owner->cc.pb_present[i] = false;
912 p_owner->cc.pp_decoder[i] = NULL;
914 p_owner->i_ts_delay = 0;
919 * The decoding main loop
921 * \param p_dec the decoder
923 static void *DecoderThread( void *p_data )
925 decoder_t *p_dec = (decoder_t *)p_data;
926 decoder_owner_sys_t *p_owner = p_dec->p_owner;
928 /* The decoder's main loop */
931 block_t *p_block = block_FifoGet( p_owner->p_fifo );
933 /* Make sure there is no cancellation point other than this one^^.
934 * If you need one, be sure to push cleanup of p_block. */
935 DecoderSignalBuffering( p_dec, p_block == NULL );
939 int canc = vlc_savecancel();
942 DecoderError( p_dec, p_block );
944 DecoderProcess( p_dec, p_block );
946 vlc_restorecancel( canc );
952 static block_t *DecoderBlockFlushNew()
954 block_t *p_null = block_Alloc( 128 );
958 p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
959 BLOCK_FLAG_CORRUPTED |
960 BLOCK_FLAG_CORE_FLUSH;
961 memset( p_null->p_buffer, 0, p_null->i_buffer );
966 static void DecoderFlush( decoder_t *p_dec )
968 decoder_owner_sys_t *p_owner = p_dec->p_owner;
970 vlc_assert_locked( &p_owner->lock );
973 block_FifoEmpty( p_owner->p_fifo );
975 /* Monitor for flush end */
976 p_owner->b_flushing = true;
977 vlc_cond_signal( &p_owner->wait_request );
979 /* Send a special block */
980 block_t *p_null = DecoderBlockFlushNew();
983 input_DecoderDecode( p_dec, p_null, false );
986 while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
987 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
990 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
992 decoder_owner_sys_t *p_owner = p_dec->p_owner;
994 vlc_mutex_lock( &p_owner->lock );
996 if( p_owner->b_buffering )
999 p_owner->buffer.b_full = true;
1000 vlc_cond_signal( &p_owner->wait_acknowledge );
1003 vlc_mutex_unlock( &p_owner->lock );
1006 static bool DecoderIsFlushing( decoder_t *p_dec )
1008 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1011 vlc_mutex_lock( &p_owner->lock );
1013 b_flushing = p_owner->b_flushing;
1015 vlc_mutex_unlock( &p_owner->lock );
1020 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
1022 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1024 vlc_assert_locked( &p_owner->lock );
1026 while( !p_owner->b_flushing )
1028 if( p_owner->b_paused )
1030 if( p_owner->b_buffering && !p_owner->buffer.b_full )
1032 if( p_owner->pause.i_ignore > 0 )
1034 p_owner->pause.i_ignore--;
1040 if( !p_owner->b_buffering || !p_owner->buffer.b_full )
1043 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
1047 *pb_reject = p_owner->b_flushing;
1050 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
1052 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1054 vlc_assert_locked( &p_owner->lock );
1056 /* XXX only audio and video output have to be paused.
1057 * - for sout it is useless
1058 * - for subs, it is done by the vout
1060 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1062 if( p_owner->p_aout )
1063 aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
1066 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1068 if( p_owner->p_vout )
1069 vout_ChangePause( p_owner->p_vout, b_paused, i_date );
1072 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
1074 if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
1075 *pi_preroll = INT64_MAX;
1076 else if( p->i_dts > VLC_TS_INVALID )
1077 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1078 else if( p->i_pts > VLC_TS_INVALID )
1079 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
1082 static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1084 mtime_t current_date = mdate();
1086 /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1087 if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1089 /* ETSI EN 300 472 Annex A : do not take into account the PTS
1090 * for teletext streams. */
1091 return current_date + 400000;
1096 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1097 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1099 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1100 input_clock_t *p_clock = p_owner->p_clock;
1102 vlc_assert_locked( &p_owner->lock );
1104 const mtime_t i_es_delay = p_owner->i_ts_delay;
1108 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1111 if( *pi_ts0 > VLC_TS_INVALID )
1113 *pi_ts0 += i_es_delay;
1114 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
1115 *pi_ts1 += i_es_delay;
1116 if( input_clock_ConvertTS( p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) )
1117 *pi_ts0 = VLC_TS_INVALID;
1121 i_rate = input_clock_GetRate( p_clock );
1124 /* Do not create ephemere data because of rounding errors */
1125 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
1129 *pi_duration = ( *pi_duration * i_rate +
1130 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
1137 *pi_ts0 = DecoderTeletextFixTs( *pi_ts0 );
1138 if( pi_ts1 && *pi_ts1 <= VLC_TS_INVALID )
1144 static bool DecoderIsExitRequested( decoder_t *p_dec )
1146 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1148 vlc_mutex_lock( &p_owner->lock );
1149 bool b_exit = p_owner->b_exit;
1150 vlc_mutex_unlock( &p_owner->lock );
1156 * If *pb_reject, it does nothing, otherwise it waits for the given
1157 * deadline or a flush request (in which case it set *pi_reject to true.
1159 static void DecoderWaitDate( decoder_t *p_dec,
1160 bool *pb_reject, mtime_t i_deadline )
1162 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1164 if( *pb_reject || i_deadline < 0 )
1169 vlc_mutex_lock( &p_owner->lock );
1170 if( p_owner->b_flushing || p_owner->b_exit )
1173 vlc_mutex_unlock( &p_owner->lock );
1176 int i_ret = vlc_cond_timedwait( &p_owner->wait_request, &p_owner->lock,
1178 vlc_mutex_unlock( &p_owner->lock );
1184 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
1185 int *pi_played_sum, int *pi_lost_sum )
1187 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1188 aout_instance_t *p_aout = p_owner->p_aout;
1189 aout_input_t *p_aout_input = p_owner->p_aout_input;
1192 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1194 msg_Warn( p_dec, "non-dated audio buffer received" );
1196 aout_BufferFree( p_audio );
1201 vlc_mutex_lock( &p_owner->lock );
1203 if( p_owner->b_buffering || p_owner->buffer.p_audio )
1205 p_audio->p_next = NULL;
1207 *p_owner->buffer.pp_audio_next = p_audio;
1208 p_owner->buffer.pp_audio_next = &p_audio->p_next;
1210 p_owner->buffer.i_count++;
1211 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1212 p_audio->i_pts - p_owner->buffer.p_audio->i_pts > DECODER_MAX_BUFFERING_AUDIO_DURATION )
1214 p_owner->buffer.b_full = true;
1215 vlc_cond_signal( &p_owner->wait_acknowledge );
1221 bool b_has_more = false;
1223 DecoderWaitUnblock( p_dec, &b_reject );
1225 if( p_owner->b_buffering )
1227 vlc_mutex_unlock( &p_owner->lock );
1232 if( p_owner->buffer.p_audio )
1234 p_audio = p_owner->buffer.p_audio;
1236 p_owner->buffer.p_audio = p_audio->p_next;
1237 p_owner->buffer.i_count--;
1239 b_has_more = p_owner->buffer.p_audio != NULL;
1241 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1245 const bool b_dated = p_audio->i_pts > VLC_TS_INVALID;
1246 int i_rate = INPUT_RATE_DEFAULT;
1248 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1249 &i_rate, AOUT_MAX_ADVANCE_TIME, false );
1251 vlc_mutex_unlock( &p_owner->lock );
1253 if( !p_aout || !p_aout_input ||
1254 p_audio->i_pts <= VLC_TS_INVALID ||
1255 i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1256 i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1259 DecoderWaitDate( p_dec, &b_reject,
1260 p_audio->i_pts - AOUT_MAX_PREPARE_TIME );
1264 if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1265 *pi_played_sum += 1;
1266 *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1271 msg_Warn( p_dec, "received buffer in the future" );
1273 msg_Warn( p_dec, "non-dated audio buffer received" );
1276 aout_BufferFree( p_audio );
1282 vlc_mutex_lock( &p_owner->lock );
1283 if( !p_owner->buffer.p_audio )
1285 vlc_mutex_unlock( &p_owner->lock );
1291 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1293 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1294 aout_buffer_t *p_aout_buf;
1299 while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1301 aout_instance_t *p_aout = p_owner->p_aout;
1302 aout_input_t *p_aout_input = p_owner->p_aout_input;
1304 if( DecoderIsExitRequested( p_dec ) )
1306 /* It prevent freezing VLC in case of broken decoder */
1307 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1309 block_Release( p_block );
1314 if( p_owner->i_preroll_end > VLC_TS_INVALID &&
1315 p_aout_buf->i_pts < p_owner->i_preroll_end )
1317 aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1321 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1323 msg_Dbg( p_dec, "End of audio preroll" );
1324 if( p_owner->p_aout && p_owner->p_aout_input )
1325 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1327 p_owner->i_preroll_end = VLC_TS_INVALID;
1330 DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1333 /* Update ugly stat */
1334 input_thread_t *p_input = p_owner->p_input;
1336 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_played > 0) )
1338 vlc_mutex_lock( &p_input->p->counters.counters_lock);
1340 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1342 stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1344 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1347 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1350 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1352 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1355 bool b_processed = false;
1359 assert( p_dec_cc->pf_get_cc != NULL );
1361 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1362 if( !p_owner->cc.b_supported )
1365 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1369 vlc_mutex_lock( &p_owner->lock );
1370 for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1372 p_owner->cc.pb_present[i] |= pb_present[i];
1373 if( p_owner->cc.pp_decoder[i] )
1377 for( i = 0; i < 4; i++ )
1379 if( !p_owner->cc.pp_decoder[i] )
1382 if( i_cc_decoder > 1 )
1383 DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1385 DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1389 vlc_mutex_unlock( &p_owner->lock );
1392 block_Release( p_cc );
1395 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1396 int *pi_played_sum, int *pi_lost_sum )
1398 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1399 vout_thread_t *p_vout = p_owner->p_vout;
1400 bool b_first_buffered;
1402 if( p_picture->date <= VLC_TS_INVALID )
1404 msg_Warn( p_dec, "non-dated video buffer received" );
1406 vout_ReleasePicture( p_vout, p_picture );
1411 vlc_mutex_lock( &p_owner->lock );
1413 if( ( p_owner->b_buffering && !p_owner->buffer.b_first ) || p_owner->buffer.p_picture )
1415 p_picture->p_next = NULL;
1417 *p_owner->buffer.pp_picture_next = p_picture;
1418 p_owner->buffer.pp_picture_next = &p_picture->p_next;
1420 p_owner->buffer.i_count++;
1421 if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1422 p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1424 p_owner->buffer.b_full = true;
1425 vlc_cond_signal( &p_owner->wait_acknowledge );
1428 b_first_buffered = p_owner->buffer.p_picture != NULL;
1430 for( ;; b_first_buffered = false )
1432 bool b_has_more = false;
1436 DecoderWaitUnblock( p_dec, &b_reject );
1438 if( p_owner->b_buffering && !p_owner->buffer.b_first )
1440 vlc_mutex_unlock( &p_owner->lock );
1443 bool b_buffering_first = p_owner->b_buffering;
1446 if( p_owner->buffer.p_picture )
1448 p_picture = p_owner->buffer.p_picture;
1450 p_owner->buffer.p_picture = p_picture->p_next;
1451 p_owner->buffer.i_count--;
1453 b_has_more = p_owner->buffer.p_picture != NULL;
1455 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1459 if( b_buffering_first )
1461 assert( p_owner->buffer.b_first );
1462 assert( !p_owner->buffer.i_count );
1463 msg_Dbg( p_dec, "Received first picture" );
1464 p_owner->buffer.b_first = false;
1465 p_picture->b_force = true;
1468 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1469 int i_rate = INPUT_RATE_DEFAULT;
1470 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1471 &i_rate, DECODER_BOGUS_VIDEO_DELAY, false );
1473 vlc_mutex_unlock( &p_owner->lock );
1476 if( !p_picture->b_force && p_picture->date <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify video_output/*
1481 if( i_rate != p_owner->i_last_rate || b_first_buffered )
1483 /* Be sure to not display old picture after our own */
1484 vout_Flush( p_vout, p_picture->date );
1485 p_owner->i_last_rate = i_rate;
1487 vout_PutPicture( p_vout, p_picture );
1492 msg_Warn( p_dec, "early picture skipped" );
1494 msg_Warn( p_dec, "non-dated video buffer received" );
1497 vout_ReleasePicture( p_vout, p_picture );
1501 vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1503 *pi_played_sum += i_tmp_display;
1504 *pi_lost_sum += i_tmp_lost;
1506 if( !b_has_more || b_buffering_first )
1509 vlc_mutex_lock( &p_owner->lock );
1510 if( !p_owner->buffer.p_picture )
1512 vlc_mutex_unlock( &p_owner->lock );
1518 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1520 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1524 int i_displayed = 0;
1526 while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1528 vout_thread_t *p_vout = p_owner->p_vout;
1529 if( DecoderIsExitRequested( p_dec ) )
1531 /* It prevent freezing VLC in case of broken decoder */
1532 vout_ReleasePicture( p_vout, p_pic );
1534 block_Release( p_block );
1540 if( p_owner->i_preroll_end > VLC_TS_INVALID && p_pic->date < p_owner->i_preroll_end )
1542 vout_ReleasePicture( p_vout, p_pic );
1546 if( p_owner->i_preroll_end > VLC_TS_INVALID )
1548 msg_Dbg( p_dec, "End of video preroll" );
1550 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1552 p_owner->i_preroll_end = VLC_TS_INVALID;
1555 if( p_dec->pf_get_cc &&
1556 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1557 DecoderGetCc( p_dec, p_dec );
1559 DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1562 /* Update ugly stat */
1563 input_thread_t *p_input = p_owner->p_input;
1565 if( p_input != NULL && (i_decoded > 0 || i_lost > 0 || i_displayed > 0) )
1567 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1569 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1571 stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1574 stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1577 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1581 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1584 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1585 vout_thread_t *p_vout = p_owner->p_spu_vout;
1588 if( p_subpic->i_start <= VLC_TS_INVALID && !b_telx )
1590 msg_Warn( p_dec, "non-dated spu buffer received" );
1591 subpicture_Delete( p_subpic );
1596 vlc_mutex_lock( &p_owner->lock );
1598 if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1600 p_subpic->p_next = NULL;
1602 *p_owner->buffer.pp_subpic_next = p_subpic;
1603 p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1605 p_owner->buffer.i_count++;
1606 /* XXX it is important to be full after the first one */
1607 if( p_owner->buffer.i_count > 0 )
1609 p_owner->buffer.b_full = true;
1610 vlc_cond_signal( &p_owner->wait_acknowledge );
1616 bool b_has_more = false;
1618 DecoderWaitUnblock( p_dec, &b_reject );
1620 if( p_owner->b_buffering )
1622 vlc_mutex_unlock( &p_owner->lock );
1627 if( p_owner->buffer.p_subpic )
1629 p_subpic = p_owner->buffer.p_subpic;
1631 p_owner->buffer.p_subpic = p_subpic->p_next;
1632 p_owner->buffer.i_count--;
1634 b_has_more = p_owner->buffer.p_subpic != NULL;
1636 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1640 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1641 NULL, INT64_MAX, b_telx );
1643 vlc_mutex_unlock( &p_owner->lock );
1645 if( p_subpic->i_start <= VLC_TS_INVALID )
1648 DecoderWaitDate( p_dec, &b_reject,
1649 p_subpic->i_start - SPU_MAX_PREPARE_TIME );
1652 vout_PutSubpicture( p_vout, p_subpic );
1654 subpicture_Delete( p_subpic );
1658 vlc_mutex_lock( &p_owner->lock );
1659 if( !p_owner->buffer.p_subpic )
1661 vlc_mutex_unlock( &p_owner->lock );
1668 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1671 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1673 assert( p_owner->p_clock );
1674 assert( !p_sout_block->p_next );
1676 vlc_mutex_lock( &p_owner->lock );
1678 if( p_owner->b_buffering || p_owner->buffer.p_block )
1680 block_ChainLastAppend( &p_owner->buffer.pp_block_next, p_sout_block );
1682 p_owner->buffer.i_count++;
1683 /* XXX it is important to be full after the first one */
1684 if( p_owner->buffer.i_count > 0 )
1686 p_owner->buffer.b_full = true;
1687 vlc_cond_signal( &p_owner->wait_acknowledge );
1693 bool b_has_more = false;
1695 DecoderWaitUnblock( p_dec, &b_reject );
1697 if( p_owner->b_buffering )
1699 vlc_mutex_unlock( &p_owner->lock );
1704 if( p_owner->buffer.p_block )
1706 p_sout_block = p_owner->buffer.p_block;
1708 p_owner->buffer.p_block = p_sout_block->p_next;
1709 p_owner->buffer.i_count--;
1711 b_has_more = p_owner->buffer.p_block != NULL;
1713 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1715 p_sout_block->p_next = NULL;
1717 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
1718 &p_sout_block->i_length,
1719 &p_sout_block->i_rate, INT64_MAX, b_telx );
1721 vlc_mutex_unlock( &p_owner->lock );
1724 sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block ); // FIXME --VLC_TS_INVALID inspect stream_output/*
1726 block_Release( p_sout_block );
1730 vlc_mutex_lock( &p_owner->lock );
1731 if( !p_owner->buffer.p_block )
1733 vlc_mutex_unlock( &p_owner->lock );
1741 static void DecoderFlushBuffering( decoder_t *p_dec )
1743 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1745 vlc_assert_locked( &p_owner->lock );
1747 while( p_owner->buffer.p_picture )
1749 picture_t *p_picture = p_owner->buffer.p_picture;
1751 p_owner->buffer.p_picture = p_picture->p_next;
1752 p_owner->buffer.i_count--;
1754 if( p_owner->p_vout )
1756 vout_ReleasePicture( p_owner->p_vout, p_picture );
1759 if( !p_owner->buffer.p_picture )
1760 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1762 while( p_owner->buffer.p_audio )
1764 aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1766 p_owner->buffer.p_audio = p_audio->p_next;
1767 p_owner->buffer.i_count--;
1769 aout_BufferFree( p_audio );
1771 if( !p_owner->buffer.p_audio )
1772 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1774 while( p_owner->buffer.p_subpic )
1776 subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1778 p_owner->buffer.p_subpic = p_subpic->p_next;
1779 p_owner->buffer.i_count--;
1781 subpicture_Delete( p_subpic );
1783 if( !p_owner->buffer.p_subpic )
1784 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1786 if( p_owner->buffer.p_block )
1788 block_ChainRelease( p_owner->buffer.p_block );
1790 p_owner->buffer.i_count = 0;
1791 p_owner->buffer.p_block = NULL;
1792 p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;
1797 /* This function process a block for sout
1799 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1801 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1802 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1803 block_t *p_sout_block;
1805 while( ( p_sout_block =
1806 p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1808 if( !p_owner->p_sout_input )
1810 es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1812 p_owner->sout.i_group = p_dec->fmt_in.i_group;
1813 p_owner->sout.i_id = p_dec->fmt_in.i_id;
1814 if( p_dec->fmt_in.psz_language )
1816 free( p_owner->sout.psz_language );
1817 p_owner->sout.psz_language =
1818 strdup( p_dec->fmt_in.psz_language );
1821 p_owner->p_sout_input =
1822 sout_InputNew( p_owner->p_sout,
1825 if( p_owner->p_sout_input == NULL )
1827 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1828 (char *)&p_owner->sout.i_codec );
1829 p_dec->b_error = true;
1831 while( p_sout_block )
1833 block_t *p_next = p_sout_block->p_next;
1834 block_Release( p_sout_block );
1835 p_sout_block = p_next;
1841 while( p_sout_block )
1843 block_t *p_next = p_sout_block->p_next;
1845 p_sout_block->p_next = NULL;
1847 DecoderPlaySout( p_dec, p_sout_block, b_telx );
1849 p_sout_block = p_next;
1855 /* This function process a video block
1857 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1859 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1861 if( p_owner->p_packetizer )
1863 block_t *p_packetized_block;
1864 decoder_t *p_packetizer = p_owner->p_packetizer;
1866 while( (p_packetized_block =
1867 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1869 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1871 es_format_Clean( &p_dec->fmt_in );
1872 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1874 if( p_packetizer->pf_get_cc )
1875 DecoderGetCc( p_dec, p_packetizer );
1877 while( p_packetized_block )
1879 block_t *p_next = p_packetized_block->p_next;
1880 p_packetized_block->p_next = NULL;
1882 DecoderDecodeVideo( p_dec, p_packetized_block );
1884 p_packetized_block = p_next;
1887 /* The packetizer does not output a block that tell the decoder to flush
1891 block_t *p_null = DecoderBlockFlushNew();
1893 DecoderDecodeVideo( p_dec, p_null );
1898 DecoderDecodeVideo( p_dec, p_block );
1901 if( b_flush && p_owner->p_vout )
1902 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1905 /* This function process a audio block
1907 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1909 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1911 if( p_owner->p_packetizer )
1913 block_t *p_packetized_block;
1914 decoder_t *p_packetizer = p_owner->p_packetizer;
1916 while( (p_packetized_block =
1917 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1919 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1921 es_format_Clean( &p_dec->fmt_in );
1922 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1925 while( p_packetized_block )
1927 block_t *p_next = p_packetized_block->p_next;
1928 p_packetized_block->p_next = NULL;
1930 DecoderDecodeAudio( p_dec, p_packetized_block );
1932 p_packetized_block = p_next;
1935 /* The packetizer does not output a block that tell the decoder to flush
1939 block_t *p_null = DecoderBlockFlushNew();
1941 DecoderDecodeAudio( p_dec, p_null );
1946 DecoderDecodeAudio( p_dec, p_block );
1949 if( b_flush && p_owner->p_aout )
1950 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1953 /* This function process a subtitle block
1955 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1957 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1958 const bool b_telx = p_dec->fmt_in.i_codec == VLC_CODEC_TELETEXT;
1960 input_thread_t *p_input = p_owner->p_input;
1961 vout_thread_t *p_vout;
1962 subpicture_t *p_spu;
1964 while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1966 if( p_input != NULL )
1968 vlc_mutex_lock( &p_input->p->counters.counters_lock );
1969 stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1,
1971 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1974 p_vout = input_resource_HoldVout( p_owner->p_resource );
1975 if( p_vout && p_owner->p_spu_vout == p_vout )
1977 /* Preroll does not work very well with subtitle */
1978 if( p_spu->i_start > VLC_TS_INVALID &&
1979 p_spu->i_start < p_owner->i_preroll_end &&
1980 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1982 subpicture_Delete( p_spu );
1986 DecoderPlaySpu( p_dec, p_spu, b_telx );
1991 subpicture_Delete( p_spu );
1994 vlc_object_release( p_vout );
1997 if( b_flush && p_owner->p_spu_vout )
1999 p_vout = input_resource_HoldVout( p_owner->p_resource );
2001 if( p_vout && p_owner->p_spu_vout == p_vout )
2002 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
2005 vlc_object_release( p_vout );
2010 static void DecoderProcessOnFlush( decoder_t *p_dec )
2012 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2014 vlc_mutex_lock( &p_owner->lock );
2015 DecoderFlushBuffering( p_dec );
2017 if( p_owner->b_flushing )
2019 p_owner->b_flushing = false;
2020 vlc_cond_signal( &p_owner->wait_acknowledge );
2022 vlc_mutex_unlock( &p_owner->lock );
2028 * \param p_dec the decoder object
2029 * \param p_block the block to decode
2030 * \return VLC_SUCCESS or an error code
2032 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
2034 decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
2035 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2037 if( p_block && p_block->i_buffer <= 0 )
2039 assert( !b_flush_request );
2040 block_Release( p_block );
2045 if( p_owner->b_packetizer )
2048 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2050 DecoderProcessSout( p_dec, p_block );
2055 bool b_flush = false;
2059 const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
2060 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
2062 b_flush = !b_flushing && b_flush_request;
2064 p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
2067 if( p_dec->fmt_out.i_cat == AUDIO_ES )
2069 DecoderProcessAudio( p_dec, p_block, b_flush );
2071 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
2073 DecoderProcessVideo( p_dec, p_block, b_flush );
2075 else if( p_dec->fmt_out.i_cat == SPU_ES )
2077 DecoderProcessSpu( p_dec, p_block, b_flush );
2081 msg_Err( p_dec, "unknown ES format" );
2082 p_dec->b_error = true;
2087 if( b_flush_request )
2088 DecoderProcessOnFlush( p_dec );
2091 static void DecoderError( decoder_t *p_dec, block_t *p_block )
2093 const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
2097 block_Release( p_block );
2099 if( b_flush_request )
2100 DecoderProcessOnFlush( p_dec );
2105 * Destroys a decoder object
2107 * \param p_dec the decoder object
2110 static void DeleteDecoder( decoder_t * p_dec )
2112 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2114 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
2115 (char*)&p_dec->fmt_in.i_codec,
2116 (unsigned)block_FifoCount( p_owner->p_fifo ) );
2118 /* Free all packets still in the decoder fifo. */
2119 block_FifoEmpty( p_owner->p_fifo );
2120 block_FifoRelease( p_owner->p_fifo );
2123 vlc_mutex_lock( &p_owner->lock );
2124 DecoderFlushBuffering( p_dec );
2125 vlc_mutex_unlock( &p_owner->lock );
2128 if( p_owner->p_aout )
2130 aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
2131 input_resource_RequestAout( p_owner->p_resource, p_owner->p_aout );
2132 if( p_owner->p_input != NULL )
2133 input_SendEventAout( p_owner->p_input );
2135 if( p_owner->p_vout )
2137 /* Hack to make sure all the the pictures are freed by the decoder
2138 * and that the vout is not paused anymore */
2139 vout_Reset( p_owner->p_vout );
2142 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
2144 if( p_owner->p_input != NULL )
2145 input_SendEventVout( p_owner->p_input );
2149 if( p_owner->p_sout_input )
2151 sout_InputDelete( p_owner->p_sout_input );
2152 es_format_Clean( &p_owner->sout );
2156 if( p_dec->fmt_out.i_cat == SPU_ES )
2158 vout_thread_t *p_vout;
2160 p_vout = input_resource_HoldVout( p_owner->p_resource );
2163 if( p_owner->p_spu_vout == p_vout )
2164 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
2165 vlc_object_release( p_vout );
2169 es_format_Clean( &p_dec->fmt_in );
2170 es_format_Clean( &p_dec->fmt_out );
2171 if( p_dec->p_description )
2172 vlc_meta_Delete( p_dec->p_description );
2173 es_format_Clean( &p_owner->fmt_description );
2174 if( p_owner->p_description )
2175 vlc_meta_Delete( p_owner->p_description );
2177 if( p_owner->p_packetizer )
2179 module_unneed( p_owner->p_packetizer,
2180 p_owner->p_packetizer->p_module );
2181 es_format_Clean( &p_owner->p_packetizer->fmt_in );
2182 es_format_Clean( &p_owner->p_packetizer->fmt_out );
2183 if( p_owner->p_packetizer->p_description )
2184 vlc_meta_Delete( p_owner->p_packetizer->p_description );
2185 vlc_object_release( p_owner->p_packetizer );
2188 vlc_cond_destroy( &p_owner->wait_acknowledge );
2189 vlc_cond_destroy( &p_owner->wait_request );
2190 vlc_mutex_destroy( &p_owner->lock );
2192 vlc_object_release( p_dec );
2197 /*****************************************************************************
2198 * Buffers allocation callbacks for the decoders
2199 *****************************************************************************/
2200 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
2202 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2204 vlc_assert_locked( &p_owner->lock );
2206 p_owner->b_fmt_description = true;
2208 /* Copy es_format */
2209 es_format_Clean( &p_owner->fmt_description );
2210 es_format_Copy( &p_owner->fmt_description, &p_dec->fmt_out );
2212 /* Move p_description */
2213 if( p_owner->p_description && p_dec->p_description )
2214 vlc_meta_Delete( p_owner->p_description );
2215 p_owner->p_description = p_dec->p_description;
2216 p_dec->p_description = NULL;
2218 static vout_thread_t *aout_request_vout( void *p_private,
2219 vout_thread_t *p_vout, video_format_t *p_fmt, bool b_recyle )
2221 decoder_t *p_dec = p_private;
2222 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2223 input_thread_t *p_input = p_owner->p_input;
2225 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
2227 if( p_input != NULL )
2228 input_SendEventVout( p_input );
2233 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
2235 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2236 aout_buffer_t *p_buffer;
2238 if( p_owner->p_aout_input != NULL &&
2239 ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
2240 p_dec->fmt_out.audio.i_original_channels !=
2241 p_owner->audio.i_original_channels ||
2242 p_dec->fmt_out.audio.i_bytes_per_frame !=
2243 p_owner->audio.i_bytes_per_frame ) )
2245 aout_input_t *p_aout_input = p_owner->p_aout_input;
2246 aout_instance_t *p_aout = p_owner->p_aout;
2248 /* Parameters changed, restart the aout */
2249 vlc_mutex_lock( &p_owner->lock );
2251 DecoderFlushBuffering( p_dec );
2253 aout_DecDelete( p_owner->p_aout, p_aout_input );
2254 p_owner->p_aout = NULL;
2255 p_owner->p_aout_input = NULL;
2257 vlc_mutex_unlock( &p_owner->lock );
2258 input_resource_RequestAout( p_owner->p_resource, p_aout );
2261 if( p_owner->p_aout_input == NULL )
2263 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
2264 audio_sample_format_t format;
2265 aout_input_t *p_aout_input;
2266 aout_instance_t *p_aout;
2267 aout_request_vout_t request_vout;
2269 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
2270 p_owner->audio = p_dec->fmt_out.audio;
2272 memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
2273 if( i_force_dolby &&
2274 (format.i_original_channels&AOUT_CHAN_PHYSMASK) ==
2275 (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
2277 if( i_force_dolby == 1 )
2279 format.i_original_channels = format.i_original_channels |
2280 AOUT_CHAN_DOLBYSTEREO;
2282 else /* i_force_dolby == 2 */
2284 format.i_original_channels = format.i_original_channels &
2285 ~AOUT_CHAN_DOLBYSTEREO;
2289 request_vout.pf_request_vout = aout_request_vout;
2290 request_vout.p_private = p_dec;
2292 assert( p_owner->p_aout == NULL );
2293 p_aout = input_resource_RequestAout( p_owner->p_resource, NULL );
2296 p_aout_input = aout_DecNew( p_aout, &format,
2297 &p_dec->fmt_out.audio_replay_gain,
2299 if( p_aout_input == NULL )
2301 input_resource_RequestAout( p_owner->p_resource, p_aout );
2306 p_aout_input = NULL;
2308 vlc_mutex_lock( &p_owner->lock );
2310 p_owner->p_aout = p_aout;
2311 p_owner->p_aout_input = p_aout_input;
2312 DecoderUpdateFormatLocked( p_dec );
2314 vlc_mutex_unlock( &p_owner->lock );
2316 if( p_owner->p_input != NULL )
2317 input_SendEventAout( p_owner->p_input );
2319 if( p_owner->p_aout_input == NULL )
2321 msg_Err( p_dec, "failed to create audio output" );
2322 p_dec->b_error = true;
2325 p_dec->fmt_out.audio.i_bytes_per_frame =
2326 p_owner->audio.i_bytes_per_frame;
2329 p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
2334 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
2336 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2338 aout_DecDeleteBuffer( p_owner->p_aout,
2339 p_owner->p_aout_input, p_buffer );
2342 static picture_t *vout_new_buffer( decoder_t *p_dec )
2344 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2346 if( p_owner->p_vout == NULL ||
2347 p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
2348 p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
2349 p_dec->fmt_out.video.i_visible_width != p_owner->video.i_visible_width ||
2350 p_dec->fmt_out.video.i_visible_height != p_owner->video.i_visible_height ||
2351 p_dec->fmt_out.video.i_x_offset != p_owner->video.i_x_offset ||
2352 p_dec->fmt_out.video.i_y_offset != p_owner->video.i_y_offset ||
2353 p_dec->fmt_out.i_codec != p_owner->video.i_chroma ||
2354 (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->video.i_sar_den !=
2355 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->video.i_sar_num )
2357 vout_thread_t *p_vout;
2359 if( !p_dec->fmt_out.video.i_width ||
2360 !p_dec->fmt_out.video.i_height )
2362 /* Can't create a new vout without display size */
2366 video_format_t fmt = p_dec->fmt_out.video;
2367 fmt.i_chroma = p_dec->fmt_out.i_codec;
2368 p_owner->video = fmt;
2370 if( !fmt.i_visible_width || !fmt.i_visible_height )
2372 if( p_dec->fmt_in.video.i_visible_width &&
2373 p_dec->fmt_in.video.i_visible_height )
2375 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
2376 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
2380 fmt.i_visible_width = fmt.i_width;
2381 fmt.i_visible_height = fmt.i_height;
2385 if( fmt.i_visible_height == 1088 &&
2386 var_CreateGetBool( p_dec, "hdtv-fix" ) )
2388 fmt.i_visible_height = 1080;
2389 if( !(fmt.i_sar_num % 136))
2391 fmt.i_sar_num *= 135;
2392 fmt.i_sar_den *= 136;
2394 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2397 if( !fmt.i_sar_num || !fmt.i_sar_den )
2403 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
2404 fmt.i_sar_num, fmt.i_sar_den, 50000 );
2406 vlc_mutex_lock( &p_owner->lock );
2408 DecoderFlushBuffering( p_dec );
2410 p_vout = p_owner->p_vout;
2411 p_owner->p_vout = NULL;
2412 vlc_mutex_unlock( &p_owner->lock );
2415 switch( p_dec->fmt_in.i_codec )
2417 case VLC_CODEC_H264:
2418 case VLC_CODEC_DIRAC: /* FIXME valid ? */
2423 case VLC_CODEC_VP6F:
2431 p_vout = input_resource_RequestVout( p_owner->p_resource,
2433 dpb_size + 1 + DECODER_MAX_BUFFERING_COUNT,
2435 vlc_mutex_lock( &p_owner->lock );
2436 p_owner->p_vout = p_vout;
2438 DecoderUpdateFormatLocked( p_dec );
2440 vlc_mutex_unlock( &p_owner->lock );
2442 if( p_owner->p_input != NULL )
2443 input_SendEventVout( p_owner->p_input );
2444 if( p_vout == NULL )
2446 msg_Err( p_dec, "failed to create video output" );
2447 p_dec->b_error = true;
2452 /* Get a new picture
2456 if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2459 picture_t *p_picture = vout_GetPicture( p_owner->p_vout );
2463 if( DecoderIsFlushing( p_dec ) )
2467 DecoderSignalBuffering( p_dec, true );
2469 /* Check the decoder doesn't leak pictures */
2470 vout_FixLeaks( p_owner->p_vout );
2472 /* FIXME add a vout_WaitPictureAvailable (timedwait) */
2473 msleep( VOUT_OUTMEM_SLEEP );
2477 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2479 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2482 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2484 vout_HoldPicture( p_dec->p_owner->p_vout, p_pic );
2487 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2489 vout_ReleasePicture( p_dec->p_owner->p_vout, p_pic );
2492 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
2493 const subpicture_updater_t *p_updater )
2495 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2496 vout_thread_t *p_vout = NULL;
2497 subpicture_t *p_subpic;
2498 int i_attempts = 30;
2500 while( i_attempts-- )
2502 if( DecoderIsExitRequested( p_dec ) || p_dec->b_error )
2505 p_vout = input_resource_HoldVout( p_owner->p_resource );
2509 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
2514 msg_Warn( p_dec, "no vout found, dropping subpicture" );
2518 if( p_owner->p_spu_vout != p_vout )
2520 vlc_mutex_lock( &p_owner->lock );
2522 DecoderFlushBuffering( p_dec );
2524 vlc_mutex_unlock( &p_owner->lock );
2526 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
2527 p_owner->i_spu_order = 0;
2528 p_owner->p_spu_vout = p_vout;
2531 p_subpic = subpicture_New( p_updater );
2534 p_subpic->i_channel = p_owner->i_spu_channel;
2535 p_subpic->i_order = p_owner->i_spu_order++;
2536 p_subpic->b_subtitle = true;
2539 vlc_object_release( p_vout );
2544 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2546 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2547 vout_thread_t *p_vout = NULL;
2549 p_vout = input_resource_HoldVout( p_owner->p_resource );
2550 if( !p_vout || p_owner->p_spu_vout != p_vout )
2553 vlc_object_release( p_vout );
2554 msg_Warn( p_dec, "no vout found, leaking subpicture" );
2558 subpicture_Delete( p_subpic );
2560 vlc_object_release( p_vout );