codec/esout: add support for CEA708
[vlc.git] / src / input / decoder.c
bloba79891da555d30b372f2ac66c0e7a20a9d049904
1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Laurent Aimar <fenrir@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
34 #include <vlc_common.h>
36 #include <vlc_atomic.h>
37 #include <vlc_block.h>
38 #include <vlc_vout.h>
39 #include <vlc_aout.h>
40 #include <vlc_sout.h>
41 #include <vlc_codec.h>
42 #include <vlc_spu.h>
43 #include <vlc_meta.h>
44 #include <vlc_dialog.h>
45 #include <vlc_modules.h>
47 #include "audio_output/aout_internal.h"
48 #include "stream_output/stream_output.h"
49 #include "input_internal.h"
50 #include "clock.h"
51 #include "decoder.h"
52 #include "event.h"
53 #include "resource.h"
55 #include "../video_output/vout_control.h"
58 * Possibles values set in p_owner->reload atomic
60 enum reload
62 RELOAD_NO_REQUEST,
63 RELOAD_DECODER, /* Reload the decoder module */
64 RELOAD_DECODER_AOUT /* Stop the aout and reload the decoder module */
67 struct decoder_owner_sys_t
69 input_thread_t *p_input;
70 input_resource_t*p_resource;
71 input_clock_t *p_clock;
72 int i_last_rate;
74 vout_thread_t *p_spu_vout;
75 int i_spu_channel;
76 int64_t i_spu_order;
78 sout_instance_t *p_sout;
79 sout_packetizer_input_t *p_sout_input;
81 vlc_thread_t thread;
83 void (*pf_update_stat)( decoder_owner_sys_t *, unsigned decoded, unsigned lost );
85 /* Some decoders require already packetized data (ie. not truncated) */
86 decoder_t *p_packetizer;
87 bool b_packetizer;
89 /* Current format in use by the output */
90 es_format_t fmt;
92 /* */
93 bool b_fmt_description;
94 vlc_meta_t *p_description;
95 atomic_int reload;
97 /* fifo */
98 block_fifo_t *p_fifo;
100 /* Lock for communication with decoder thread */
101 vlc_mutex_t lock;
102 vlc_cond_t wait_request;
103 vlc_cond_t wait_acknowledge;
104 vlc_cond_t wait_fifo; /* TODO: merge with wait_acknowledge */
105 vlc_cond_t wait_timed;
107 /* -- These variables need locking on write(only) -- */
108 audio_output_t *p_aout;
110 vout_thread_t *p_vout;
112 /* -- Theses variables need locking on read *and* write -- */
113 /* Preroll */
114 int64_t i_preroll_end;
115 /* Pause */
116 mtime_t pause_date;
117 unsigned frames_countdown;
118 bool paused;
120 bool error;
122 /* Waiting */
123 bool b_waiting;
124 bool b_first;
125 bool b_has_data;
127 /* Flushing */
128 bool flushing;
129 bool b_draining;
130 atomic_bool drained;
131 bool b_idle;
133 /* CC */
134 #define MAX_CC_DECODERS 64 /* The es_out only creates one type of es */
135 struct
137 bool b_supported;
138 decoder_cc_desc_t desc;
139 decoder_t *pp_decoder[MAX_CC_DECODERS];
140 } cc;
142 /* Delay */
143 mtime_t i_ts_delay;
146 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
147 * a bogus PTS and won't be displayed */
148 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
150 /* */
151 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
152 #define BLOCK_FLAG_CORE_PRIVATE_RELOADED (1 << BLOCK_FLAG_CORE_PRIVATE_SHIFT)
155 * Load a decoder module
157 static int LoadDecoder( decoder_t *p_dec, bool b_packetizer,
158 const es_format_t *restrict p_fmt )
160 p_dec->b_frame_drop_allowed = true;
161 p_dec->i_extra_picture_buffers = 0;
163 p_dec->pf_decode = NULL;
164 p_dec->pf_get_cc = NULL;
165 p_dec->pf_packetize = NULL;
166 p_dec->pf_flush = NULL;
168 es_format_Copy( &p_dec->fmt_in, p_fmt );
169 es_format_Init( &p_dec->fmt_out, p_fmt->i_cat, 0 );
171 /* Find a suitable decoder/packetizer module */
172 if( !b_packetizer )
174 static const char caps[ES_CATEGORY_COUNT][16] = {
175 [VIDEO_ES] = "video decoder",
176 [AUDIO_ES] = "audio decoder",
177 [SPU_ES] = "spu decoder",
179 p_dec->p_module = module_need( p_dec, caps[p_dec->fmt_in.i_cat],
180 "$codec", false );
182 else
183 p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
185 if( !p_dec->p_module )
187 es_format_Clean( &p_dec->fmt_in );
188 return -1;
190 else
191 return 0;
195 * Unload a decoder module
197 static void UnloadDecoder( decoder_t *p_dec )
199 if( p_dec->p_module )
201 module_unneed( p_dec, p_dec->p_module );
202 p_dec->p_module = NULL;
205 if( p_dec->p_description )
207 vlc_meta_Delete( p_dec->p_description );
208 p_dec->p_description = NULL;
211 es_format_Clean( &p_dec->fmt_in );
212 es_format_Clean( &p_dec->fmt_out );
215 static int ReloadDecoder( decoder_t *p_dec, bool b_packetizer,
216 const es_format_t *restrict p_fmt, enum reload reload )
218 /* Copy p_fmt since it can be destroyed by UnloadDecoder */
219 es_format_t fmt_in;
220 if( es_format_Copy( &fmt_in, p_fmt ) != VLC_SUCCESS )
222 p_dec->p_owner->error = true;
223 return VLC_EGENERIC;
226 /* Restart the decoder module */
227 UnloadDecoder( p_dec );
228 p_dec->p_owner->error = false;
230 if( reload == RELOAD_DECODER_AOUT )
232 decoder_owner_sys_t *p_owner = p_dec->p_owner;
233 assert( p_owner->fmt.i_cat == AUDIO_ES );
234 audio_output_t *p_aout = p_owner->p_aout;
236 vlc_mutex_lock( &p_owner->lock );
237 p_owner->p_aout = NULL;
238 vlc_mutex_unlock( &p_owner->lock );
239 if( p_aout )
241 aout_DecDelete( p_aout );
242 input_resource_PutAout( p_owner->p_resource, p_aout );
246 if( LoadDecoder( p_dec, b_packetizer, &fmt_in ) )
248 p_dec->p_owner->error = true;
249 es_format_Clean( &fmt_in );
250 return VLC_EGENERIC;
252 es_format_Clean( &fmt_in );
253 return VLC_SUCCESS;
256 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
258 decoder_owner_sys_t *p_owner = p_dec->p_owner;
260 vlc_assert_locked( &p_owner->lock );
262 es_format_Clean( &p_owner->fmt );
263 es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
265 /* Move p_description */
266 if( p_dec->p_description != NULL )
268 if( p_owner->p_description != NULL )
269 vlc_meta_Delete( p_owner->p_description );
270 p_owner->p_description = p_dec->p_description;
271 p_dec->p_description = NULL;
274 p_owner->b_fmt_description = true;
277 /*****************************************************************************
278 * Buffers allocation callbacks for the decoders
279 *****************************************************************************/
280 static vout_thread_t *aout_request_vout( void *p_private,
281 vout_thread_t *p_vout,
282 const video_format_t *p_fmt, bool b_recyle )
284 decoder_t *p_dec = p_private;
285 decoder_owner_sys_t *p_owner = p_dec->p_owner;
286 input_thread_t *p_input = p_owner->p_input;
288 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
289 b_recyle );
290 if( p_input != NULL )
291 input_SendEventVout( p_input );
293 return p_vout;
296 static bool aout_replaygain_changed( const audio_replay_gain_t *a,
297 const audio_replay_gain_t *b )
299 for( size_t i=0; i<AUDIO_REPLAY_GAIN_MAX; i++ )
301 if( a->pb_gain[i] != b->pb_gain[i] ||
302 a->pb_peak[i] != b->pb_peak[i] ||
303 a->pb_gain[i] != b->pb_gain[i] ||
304 a->pb_peak[i] != b->pb_peak[i] )
305 return true;
307 return false;
310 static int aout_update_format( decoder_t *p_dec )
312 decoder_owner_sys_t *p_owner = p_dec->p_owner;
314 if( p_owner->p_aout &&
315 ( !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->fmt.audio) ||
316 p_dec->fmt_out.i_codec != p_dec->fmt_out.audio.i_format ) )
318 audio_output_t *p_aout = p_owner->p_aout;
320 /* Parameters changed, restart the aout */
321 vlc_mutex_lock( &p_owner->lock );
322 p_owner->p_aout = NULL;
323 vlc_mutex_unlock( &p_owner->lock );
324 aout_DecDelete( p_aout );
326 input_resource_PutAout( p_owner->p_resource, p_aout );
329 /* Check if only replay gain has changed */
330 if( aout_replaygain_changed( &p_dec->fmt_in.audio_replay_gain,
331 &p_owner->fmt.audio_replay_gain ) )
333 p_dec->fmt_out.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
334 if( p_owner->p_aout )
336 p_owner->fmt.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
337 var_TriggerCallback( p_owner->p_aout, "audio-replay-gain-mode" );
341 if( p_owner->p_aout == NULL )
343 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
345 audio_sample_format_t format = p_dec->fmt_out.audio;
346 aout_FormatPrepare( &format );
348 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
349 if( i_force_dolby &&
350 format.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
352 if( i_force_dolby == 1 )
353 format.i_chan_mode |= AOUT_CHANMODE_DOLBYSTEREO;
354 else /* i_force_dolby == 2 */
355 format.i_chan_mode &= ~AOUT_CHANMODE_DOLBYSTEREO;
358 aout_request_vout_t request_vout = {
359 .pf_request_vout = aout_request_vout,
360 .p_private = p_dec,
362 audio_output_t *p_aout;
364 p_aout = input_resource_GetAout( p_owner->p_resource );
365 if( p_aout )
367 if( aout_DecNew( p_aout, &format,
368 &p_dec->fmt_out.audio_replay_gain,
369 &request_vout ) )
371 input_resource_PutAout( p_owner->p_resource, p_aout );
372 p_aout = NULL;
376 vlc_mutex_lock( &p_owner->lock );
377 p_owner->p_aout = p_aout;
379 DecoderUpdateFormatLocked( p_dec );
380 aout_FormatPrepare( &p_owner->fmt.audio );
381 vlc_mutex_unlock( &p_owner->lock );
383 if( p_owner->p_input != NULL )
384 input_SendEventAout( p_owner->p_input );
386 if( p_aout == NULL )
388 msg_Err( p_dec, "failed to create audio output" );
389 return -1;
392 p_dec->fmt_out.audio.i_bytes_per_frame =
393 p_owner->fmt.audio.i_bytes_per_frame;
394 p_dec->fmt_out.audio.i_frame_length =
395 p_owner->fmt.audio.i_frame_length;
397 return 0;
400 static int vout_update_format( decoder_t *p_dec )
402 decoder_owner_sys_t *p_owner = p_dec->p_owner;
404 if( p_owner->p_vout == NULL
405 || p_dec->fmt_out.video.i_width != p_owner->fmt.video.i_width
406 || p_dec->fmt_out.video.i_height != p_owner->fmt.video.i_height
407 || p_dec->fmt_out.video.i_visible_width != p_owner->fmt.video.i_visible_width
408 || p_dec->fmt_out.video.i_visible_height != p_owner->fmt.video.i_visible_height
409 || p_dec->fmt_out.video.i_x_offset != p_owner->fmt.video.i_x_offset
410 || p_dec->fmt_out.video.i_y_offset != p_owner->fmt.video.i_y_offset
411 || p_dec->fmt_out.i_codec != p_owner->fmt.video.i_chroma
412 || (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->fmt.video.i_sar_den !=
413 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->fmt.video.i_sar_num ||
414 p_dec->fmt_out.video.orientation != p_owner->fmt.video.orientation ||
415 p_dec->fmt_out.video.multiview_mode != p_owner->fmt.video.multiview_mode )
417 vout_thread_t *p_vout;
419 if( !p_dec->fmt_out.video.i_width ||
420 !p_dec->fmt_out.video.i_height ||
421 p_dec->fmt_out.video.i_width < p_dec->fmt_out.video.i_visible_width ||
422 p_dec->fmt_out.video.i_height < p_dec->fmt_out.video.i_visible_height )
424 /* Can't create a new vout without display size */
425 return -1;
428 video_format_t fmt = p_dec->fmt_out.video;
429 fmt.i_chroma = p_dec->fmt_out.i_codec;
431 if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
433 const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
434 for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
436 while( fmt.i_width % dsc->p[i].w.den )
437 fmt.i_width++;
438 while( fmt.i_height % dsc->p[i].h.den )
439 fmt.i_height++;
443 if( !fmt.i_visible_width || !fmt.i_visible_height )
445 if( p_dec->fmt_in.video.i_visible_width &&
446 p_dec->fmt_in.video.i_visible_height )
448 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
449 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
450 fmt.i_x_offset = p_dec->fmt_in.video.i_x_offset;
451 fmt.i_y_offset = p_dec->fmt_in.video.i_y_offset;
453 else
455 fmt.i_visible_width = fmt.i_width;
456 fmt.i_visible_height = fmt.i_height;
457 fmt.i_x_offset = 0;
458 fmt.i_y_offset = 0;
462 if( fmt.i_visible_height == 1088 &&
463 var_CreateGetBool( p_dec, "hdtv-fix" ) )
465 fmt.i_visible_height = 1080;
466 if( !(fmt.i_sar_num % 136))
468 fmt.i_sar_num *= 135;
469 fmt.i_sar_den *= 136;
471 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
474 if( !fmt.i_sar_num || !fmt.i_sar_den )
476 fmt.i_sar_num = 1;
477 fmt.i_sar_den = 1;
480 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
481 fmt.i_sar_num, fmt.i_sar_den, 50000 );
483 video_format_AdjustColorSpace( &fmt );
485 vlc_mutex_lock( &p_owner->lock );
487 p_vout = p_owner->p_vout;
488 p_owner->p_vout = NULL;
489 vlc_mutex_unlock( &p_owner->lock );
491 unsigned dpb_size;
492 switch( p_dec->fmt_in.i_codec )
494 case VLC_CODEC_HEVC:
495 case VLC_CODEC_H264:
496 case VLC_CODEC_DIRAC: /* FIXME valid ? */
497 dpb_size = 18;
498 break;
499 case VLC_CODEC_VP5:
500 case VLC_CODEC_VP6:
501 case VLC_CODEC_VP6F:
502 case VLC_CODEC_VP8:
503 dpb_size = 3;
504 break;
505 default:
506 dpb_size = 2;
507 break;
509 p_vout = input_resource_RequestVout( p_owner->p_resource,
510 p_vout, &fmt,
511 dpb_size +
512 p_dec->i_extra_picture_buffers + 1,
513 true );
514 vlc_mutex_lock( &p_owner->lock );
515 p_owner->p_vout = p_vout;
517 DecoderUpdateFormatLocked( p_dec );
518 p_owner->fmt.video.i_chroma = p_dec->fmt_out.i_codec;
519 vlc_mutex_unlock( &p_owner->lock );
521 if( p_owner->p_input != NULL )
522 input_SendEventVout( p_owner->p_input );
523 if( p_vout == NULL )
525 msg_Err( p_dec, "failed to create video output" );
526 return -1;
530 if ( memcmp( &p_dec->fmt_out.video.mastering,
531 &p_owner->fmt.video.mastering,
532 sizeof(p_owner->fmt.video.mastering)) ||
533 p_dec->fmt_out.video.lighting.MaxCLL !=
534 p_owner->fmt.video.lighting.MaxCLL ||
535 p_dec->fmt_out.video.lighting.MaxFALL !=
536 p_owner->fmt.video.lighting.MaxFALL)
538 /* the format has changed but we don't need a new vout */
539 vlc_mutex_lock( &p_owner->lock );
540 DecoderUpdateFormatLocked( p_dec );
541 vlc_mutex_unlock( &p_owner->lock );
543 return 0;
546 static picture_t *vout_new_buffer( decoder_t *p_dec )
548 decoder_owner_sys_t *p_owner = p_dec->p_owner;
549 assert( p_owner->p_vout );
551 return vout_GetPicture( p_owner->p_vout );
554 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
555 const subpicture_updater_t *p_updater )
557 decoder_owner_sys_t *p_owner = p_dec->p_owner;
558 vout_thread_t *p_vout = NULL;
559 subpicture_t *p_subpic;
560 int i_attempts = 30;
562 while( i_attempts-- )
564 if( p_owner->error )
565 break;
567 p_vout = input_resource_HoldVout( p_owner->p_resource );
568 if( p_vout )
569 break;
571 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
574 if( !p_vout )
576 msg_Warn( p_dec, "no vout found, dropping subpicture" );
577 return NULL;
580 if( p_owner->p_spu_vout != p_vout )
582 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
583 p_owner->i_spu_order = 0;
584 p_owner->p_spu_vout = p_vout;
587 p_subpic = subpicture_New( p_updater );
588 if( p_subpic )
590 p_subpic->i_channel = p_owner->i_spu_channel;
591 p_subpic->i_order = p_owner->i_spu_order++;
592 p_subpic->b_subtitle = true;
595 vlc_object_release( p_vout );
597 return p_subpic;
600 static int DecoderGetInputAttachments( decoder_t *p_dec,
601 input_attachment_t ***ppp_attachment,
602 int *pi_attachment )
604 input_thread_t *p_input = p_dec->p_owner->p_input;
606 if( unlikely(p_input == NULL) )
607 return VLC_ENOOBJ;
608 return input_Control( p_input, INPUT_GET_ATTACHMENTS,
609 ppp_attachment, pi_attachment );
612 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
614 decoder_owner_sys_t *p_owner = p_dec->p_owner;
616 vlc_mutex_lock( &p_owner->lock );
617 if( p_owner->b_waiting )
618 i_ts = VLC_TS_INVALID;
619 vlc_mutex_unlock( &p_owner->lock );
621 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
622 return i_ts;
624 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) ) {
625 msg_Err(p_dec, "Could not get display date for timestamp %"PRId64"", i_ts);
626 return VLC_TS_INVALID;
629 return i_ts;
632 static int DecoderGetDisplayRate( decoder_t *p_dec )
634 decoder_owner_sys_t *p_owner = p_dec->p_owner;
636 if( !p_owner->p_clock )
637 return INPUT_RATE_DEFAULT;
638 return input_clock_GetRate( p_owner->p_clock );
641 /*****************************************************************************
642 * Public functions
643 *****************************************************************************/
644 block_t *decoder_NewAudioBuffer( decoder_t *dec, int samples )
646 assert( dec->fmt_out.audio.i_frame_length > 0
647 && dec->fmt_out.audio.i_bytes_per_frame > 0 );
649 size_t length = samples * dec->fmt_out.audio.i_bytes_per_frame
650 / dec->fmt_out.audio.i_frame_length;
651 block_t *block = block_Alloc( length );
652 if( likely(block != NULL) )
654 block->i_nb_samples = samples;
655 block->i_pts = block->i_length = 0;
657 return block;
660 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder,
661 const subpicture_updater_t *p_dyn )
663 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder, p_dyn );
664 if( !p_subpicture )
665 msg_Warn( p_decoder, "can't get output subpicture" );
666 return p_subpicture;
669 static void RequestReload( decoder_t * p_dec )
671 decoder_owner_sys_t *p_owner = p_dec->p_owner;
672 /* Don't override reload if it's RELOAD_DECODER_AOUT */
673 int expected = RELOAD_NO_REQUEST;
674 atomic_compare_exchange_strong( &p_owner->reload, &expected, RELOAD_DECODER );
677 /* decoder_GetInputAttachments:
679 int decoder_GetInputAttachments( decoder_t *p_dec,
680 input_attachment_t ***ppp_attachment,
681 int *pi_attachment )
683 if( !p_dec->pf_get_attachments )
684 return VLC_EGENERIC;
686 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
688 /* decoder_GetDisplayDate:
690 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
692 if( !p_dec->pf_get_display_date )
693 return VLC_TS_INVALID;
695 return p_dec->pf_get_display_date( p_dec, i_ts );
697 /* decoder_GetDisplayRate:
699 int decoder_GetDisplayRate( decoder_t *p_dec )
701 if( !p_dec->pf_get_display_rate )
702 return INPUT_RATE_DEFAULT;
704 return p_dec->pf_get_display_rate( p_dec );
707 void decoder_AbortPictures( decoder_t *p_dec, bool b_abort )
709 decoder_owner_sys_t *p_owner = p_dec->p_owner;
711 vlc_mutex_lock( &p_owner->lock );
712 if( p_owner->p_vout != NULL )
713 vout_Cancel( p_owner->p_vout, b_abort );
714 vlc_mutex_unlock( &p_owner->lock );
717 static void DecoderWaitUnblock( decoder_t *p_dec )
719 decoder_owner_sys_t *p_owner = p_dec->p_owner;
721 vlc_assert_locked( &p_owner->lock );
723 for( ;; )
725 if( !p_owner->b_waiting || !p_owner->b_has_data )
726 break;
727 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
731 /* DecoderTimedWait: Interruptible wait
732 * Returns VLC_SUCCESS if wait was not interrupted, and VLC_EGENERIC otherwise */
733 static int DecoderTimedWait( decoder_t *p_dec, mtime_t deadline )
735 decoder_owner_sys_t *p_owner = p_dec->p_owner;
737 if (deadline - mdate() <= 0)
738 return VLC_SUCCESS;
740 vlc_fifo_Lock( p_owner->p_fifo );
741 while( !p_owner->flushing
742 && vlc_fifo_TimedWaitCond( p_owner->p_fifo, &p_owner->wait_timed,
743 deadline ) == 0 );
744 int ret = p_owner->flushing ? VLC_EGENERIC : VLC_SUCCESS;
745 vlc_fifo_Unlock( p_owner->p_fifo );
746 return ret;
749 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
751 if( p->i_flags & BLOCK_FLAG_PREROLL )
752 *pi_preroll = INT64_MAX;
753 /* Check if we can use the packet for end of preroll */
754 else if( (p->i_flags & BLOCK_FLAG_DISCONTINUITY) &&
755 (p->i_buffer == 0 || (p->i_flags & BLOCK_FLAG_CORRUPTED)) )
756 *pi_preroll = INT64_MAX;
757 else if( p->i_dts > VLC_TS_INVALID )
758 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
759 else if( p->i_pts > VLC_TS_INVALID )
760 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
763 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
764 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
766 decoder_owner_sys_t *p_owner = p_dec->p_owner;
767 input_clock_t *p_clock = p_owner->p_clock;
769 vlc_assert_locked( &p_owner->lock );
771 const mtime_t i_es_delay = p_owner->i_ts_delay;
773 if( !p_clock )
774 return;
776 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
777 int i_rate;
779 if( *pi_ts0 > VLC_TS_INVALID )
781 *pi_ts0 += i_es_delay;
782 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
783 *pi_ts1 += i_es_delay;
784 if( i_ts_bound != INT64_MAX )
785 i_ts_bound += i_es_delay;
786 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) ) {
787 const char *psz_name = module_get_name( p_dec->p_module, false );
788 if( pi_ts1 != NULL )
789 msg_Err(p_dec, "Could not convert timestamps %"PRId64
790 ", %"PRId64" for %s", *pi_ts0, *pi_ts1, psz_name );
791 else
792 msg_Err(p_dec, "Could not convert timestamp %"PRId64" for %s", *pi_ts0, psz_name );
793 *pi_ts0 = VLC_TS_INVALID;
796 else
798 i_rate = input_clock_GetRate( p_clock );
801 /* Do not create ephemere data because of rounding errors */
802 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
803 *pi_ts1 += 1;
805 if( pi_duration )
806 *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
807 / INPUT_RATE_DEFAULT;
809 if( pi_rate )
810 *pi_rate = i_rate;
813 #ifdef ENABLE_SOUT
814 static int DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
816 decoder_owner_sys_t *p_owner = p_dec->p_owner;
818 assert( p_owner->p_clock );
819 assert( !p_sout_block->p_next );
821 vlc_mutex_lock( &p_owner->lock );
823 if( p_owner->b_waiting )
825 p_owner->b_has_data = true;
826 vlc_cond_signal( &p_owner->wait_acknowledge );
829 DecoderWaitUnblock( p_dec );
830 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
831 &p_sout_block->i_length, NULL, INT64_MAX );
833 vlc_mutex_unlock( &p_owner->lock );
835 /* FIXME --VLC_TS_INVALID inspect stream_output*/
836 return sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
839 /* This function process a block for sout
841 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
843 decoder_owner_sys_t *p_owner = p_dec->p_owner;
844 block_t *p_sout_block;
845 block_t **pp_block = p_block ? &p_block : NULL;
847 while( ( p_sout_block =
848 p_dec->pf_packetize( p_dec, pp_block ) ) )
850 if( p_owner->p_sout_input == NULL )
852 vlc_mutex_lock( &p_owner->lock );
853 DecoderUpdateFormatLocked( p_dec );
854 vlc_mutex_unlock( &p_owner->lock );
856 p_owner->fmt.i_group = p_dec->fmt_in.i_group;
857 p_owner->fmt.i_id = p_dec->fmt_in.i_id;
858 if( p_dec->fmt_in.psz_language )
860 free( p_owner->fmt.psz_language );
861 p_owner->fmt.psz_language =
862 strdup( p_dec->fmt_in.psz_language );
865 p_owner->p_sout_input =
866 sout_InputNew( p_owner->p_sout, &p_owner->fmt );
868 if( p_owner->p_sout_input == NULL )
870 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
871 (char *)&p_owner->fmt.i_codec );
872 p_owner->error = true;
874 if(p_block)
875 block_Release(p_block);
877 block_ChainRelease(p_sout_block);
878 break;
882 while( p_sout_block )
884 block_t *p_next = p_sout_block->p_next;
886 p_sout_block->p_next = NULL;
888 if( DecoderPlaySout( p_dec, p_sout_block ) == VLC_EGENERIC )
890 msg_Err( p_dec, "cannot continue streaming due to errors" );
892 p_owner->error = true;
894 /* Cleanup */
896 if( p_block )
897 block_Release( p_block );
899 block_ChainRelease( p_next );
900 return;
903 p_sout_block = p_next;
907 #endif
909 static void DecoderPlayCc( decoder_t *p_dec, block_t *p_cc,
910 const decoder_cc_desc_t *p_desc )
912 decoder_owner_sys_t *p_owner = p_dec->p_owner;
914 vlc_mutex_lock( &p_owner->lock );
916 p_owner->cc.desc = *p_desc;
918 /* Fanout data to all decoders. We do not know if es_out
919 selected 608 or 708. */
920 uint64_t i_bitmap = p_owner->cc.desc.i_608_channels |
921 p_owner->cc.desc.i_708_channels;
923 for( int i=0; i_bitmap > 0; i_bitmap >>= 1, i++ )
925 decoder_t *p_ccdec = p_owner->cc.pp_decoder[i];
926 if( !p_ccdec )
927 continue;
929 if( i_bitmap > 1 )
931 block_FifoPut( p_ccdec->p_owner->p_fifo, block_Duplicate(p_cc) );
933 else
935 block_FifoPut( p_ccdec->p_owner->p_fifo, p_cc );
936 p_cc = NULL; /* was last dec */
940 vlc_mutex_unlock( &p_owner->lock );
942 if( p_cc ) /* can have bitmap set but no created decs */
943 block_Release( p_cc );
946 static void PacketizerGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
948 decoder_owner_sys_t *p_owner = p_dec->p_owner;
949 block_t *p_cc;
950 decoder_cc_desc_t desc;
952 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
953 if( !p_owner->cc.b_supported )
954 return;
956 assert( p_dec_cc->pf_get_cc != NULL );
958 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, &desc );
959 if( !p_cc )
960 return;
961 DecoderPlayCc( p_dec, p_cc, &desc );
964 static int DecoderQueueCc( decoder_t *p_videodec, block_t *p_cc,
965 const decoder_cc_desc_t *p_desc )
967 decoder_owner_sys_t *p_owner = p_videodec->p_owner;
969 if( unlikely( p_cc != NULL ) )
971 if( p_owner->cc.b_supported &&
972 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
973 DecoderPlayCc( p_videodec, p_cc, p_desc );
974 else
975 block_Release( p_cc );
977 return 0;
980 static int DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
981 unsigned *restrict pi_lost_sum )
983 decoder_owner_sys_t *p_owner = p_dec->p_owner;
984 vout_thread_t *p_vout = p_owner->p_vout;
985 bool prerolled;
987 vlc_mutex_lock( &p_owner->lock );
988 if( p_owner->i_preroll_end > p_picture->date )
990 vlc_mutex_unlock( &p_owner->lock );
991 picture_Release( p_picture );
992 return -1;
995 prerolled = p_owner->i_preroll_end > INT64_MIN;
996 p_owner->i_preroll_end = INT64_MIN;
997 vlc_mutex_unlock( &p_owner->lock );
999 if( unlikely(prerolled) )
1001 msg_Dbg( p_dec, "end of video preroll" );
1003 if( p_vout )
1004 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1007 if( p_picture->date <= VLC_TS_INVALID )
1009 msg_Warn( p_dec, "non-dated video buffer received" );
1010 goto discard;
1013 /* */
1014 vlc_mutex_lock( &p_owner->lock );
1016 if( p_owner->b_waiting && !p_owner->b_first )
1018 p_owner->b_has_data = true;
1019 vlc_cond_signal( &p_owner->wait_acknowledge );
1021 bool b_first_after_wait = p_owner->b_waiting && p_owner->b_has_data;
1023 DecoderWaitUnblock( p_dec );
1025 if( p_owner->b_waiting )
1027 assert( p_owner->b_first );
1028 msg_Dbg( p_dec, "Received first picture" );
1029 p_owner->b_first = false;
1030 p_picture->b_force = true;
1033 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1034 int i_rate = INPUT_RATE_DEFAULT;
1035 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1036 &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1038 vlc_mutex_unlock( &p_owner->lock );
1040 /* FIXME: The *input* FIFO should not be locked here. This will not work
1041 * properly if/when pictures are queued asynchronously. */
1042 vlc_fifo_Lock( p_owner->p_fifo );
1043 if( unlikely(p_owner->paused) && likely(p_owner->frames_countdown > 0) )
1044 p_owner->frames_countdown--;
1045 vlc_fifo_Unlock( p_owner->p_fifo );
1047 /* */
1048 if( p_vout == NULL )
1049 goto discard;
1051 if( p_picture->b_force || p_picture->date > VLC_TS_INVALID )
1052 /* FIXME: VLC_TS_INVALID -- verify video_output */
1054 if( i_rate != p_owner->i_last_rate || b_first_after_wait )
1056 /* Be sure to not display old picture after our own */
1057 vout_Flush( p_vout, p_picture->date );
1058 p_owner->i_last_rate = i_rate;
1060 vout_PutPicture( p_vout, p_picture );
1062 else
1064 if( b_dated )
1065 msg_Warn( p_dec, "early picture skipped" );
1066 else
1067 msg_Warn( p_dec, "non-dated video buffer received" );
1068 goto discard;
1071 return 0;
1072 discard:
1073 *pi_lost_sum += 1;
1074 picture_Release( p_picture );
1075 return 0;
1078 static void DecoderUpdateStatVideo( decoder_owner_sys_t *p_owner,
1079 unsigned decoded, unsigned lost )
1081 input_thread_t *p_input = p_owner->p_input;
1082 unsigned displayed = 0;
1084 /* Update ugly stat */
1085 if( p_input == NULL )
1086 return;
1088 if( p_owner->p_vout != NULL )
1090 unsigned vout_lost = 0;
1092 vout_GetResetStatistic( p_owner->p_vout, &displayed, &vout_lost );
1093 lost += vout_lost;
1096 vlc_mutex_lock( &input_priv(p_input)->counters.counters_lock );
1097 stats_Update( input_priv(p_input)->counters.p_decoded_video, decoded, NULL );
1098 stats_Update( input_priv(p_input)->counters.p_lost_pictures, lost , NULL);
1099 stats_Update( input_priv(p_input)->counters.p_displayed_pictures, displayed, NULL);
1100 vlc_mutex_unlock( &input_priv(p_input)->counters.counters_lock );
1103 static int DecoderQueueVideo( decoder_t *p_dec, picture_t *p_pic )
1105 assert( p_pic );
1106 unsigned i_lost = 0;
1107 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1109 int ret = DecoderPlayVideo( p_dec, p_pic, &i_lost );
1111 p_owner->pf_update_stat( p_owner, 1, i_lost );
1112 return ret;
1115 static int DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
1116 unsigned *restrict pi_lost_sum )
1118 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1119 bool prerolled;
1121 assert( p_audio != NULL );
1123 vlc_mutex_lock( &p_owner->lock );
1124 if( p_owner->i_preroll_end > p_audio->i_pts )
1126 vlc_mutex_unlock( &p_owner->lock );
1127 block_Release( p_audio );
1128 return -1;
1131 prerolled = p_owner->i_preroll_end > INT64_MIN;
1132 p_owner->i_preroll_end = INT64_MIN;
1133 vlc_mutex_unlock( &p_owner->lock );
1135 if( unlikely(prerolled) )
1137 msg_Dbg( p_dec, "end of audio preroll" );
1139 if( p_owner->p_aout )
1140 aout_DecFlush( p_owner->p_aout, false );
1143 /* */
1144 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1146 msg_Warn( p_dec, "non-dated audio buffer received" );
1147 *pi_lost_sum += 1;
1148 block_Release( p_audio );
1149 return 0;
1152 /* */
1153 vlc_mutex_lock( &p_owner->lock );
1154 if( p_owner->b_waiting )
1156 p_owner->b_has_data = true;
1157 vlc_cond_signal( &p_owner->wait_acknowledge );
1160 /* */
1161 int i_rate = INPUT_RATE_DEFAULT;
1163 DecoderWaitUnblock( p_dec );
1164 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1165 &i_rate, AOUT_MAX_ADVANCE_TIME );
1166 vlc_mutex_unlock( &p_owner->lock );
1168 audio_output_t *p_aout = p_owner->p_aout;
1170 if( p_aout != NULL && p_audio->i_pts > VLC_TS_INVALID
1171 && i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1172 && i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE
1173 && !DecoderTimedWait( p_dec, p_audio->i_pts - AOUT_MAX_PREPARE_TIME ) )
1175 int status = aout_DecPlay( p_aout, p_audio, i_rate );
1176 if( status == AOUT_DEC_CHANGED )
1178 /* Only reload the decoder */
1179 RequestReload( p_dec );
1181 else if( status == AOUT_DEC_FAILED )
1183 /* If we reload because the aout failed, we should release it. That
1184 * way, a next call to aout_update_format() won't re-use the
1185 * previous (failing) aout but will try to create a new one. */
1186 atomic_store( &p_owner->reload, RELOAD_DECODER_AOUT );
1189 else
1191 msg_Dbg( p_dec, "discarded audio buffer" );
1192 *pi_lost_sum += 1;
1193 block_Release( p_audio );
1195 return 0;
1198 static void DecoderUpdateStatAudio( decoder_owner_sys_t *p_owner,
1199 unsigned decoded, unsigned lost )
1201 input_thread_t *p_input = p_owner->p_input;
1202 unsigned played = 0;
1204 /* Update ugly stat */
1205 if( p_input == NULL )
1206 return;
1208 if( p_owner->p_aout != NULL )
1210 unsigned aout_lost;
1212 aout_DecGetResetStats( p_owner->p_aout, &aout_lost, &played );
1213 lost += aout_lost;
1216 vlc_mutex_lock( &input_priv(p_input)->counters.counters_lock);
1217 stats_Update( input_priv(p_input)->counters.p_lost_abuffers, lost, NULL );
1218 stats_Update( input_priv(p_input)->counters.p_played_abuffers, played, NULL );
1219 stats_Update( input_priv(p_input)->counters.p_decoded_audio, decoded, NULL );
1220 vlc_mutex_unlock( &input_priv(p_input)->counters.counters_lock);
1223 static int DecoderQueueAudio( decoder_t *p_dec, block_t *p_aout_buf )
1225 unsigned lost = 0;
1226 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1228 int ret = DecoderPlayAudio( p_dec, p_aout_buf, &lost );
1230 p_owner->pf_update_stat( p_owner, 1, lost );
1232 return ret;
1235 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1237 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1238 vout_thread_t *p_vout = p_owner->p_spu_vout;
1240 /* */
1241 if( p_subpic->i_start <= VLC_TS_INVALID )
1243 msg_Warn( p_dec, "non-dated spu buffer received" );
1244 subpicture_Delete( p_subpic );
1245 return;
1248 /* */
1249 vlc_mutex_lock( &p_owner->lock );
1251 if( p_owner->b_waiting )
1253 p_owner->b_has_data = true;
1254 vlc_cond_signal( &p_owner->wait_acknowledge );
1257 DecoderWaitUnblock( p_dec );
1258 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1259 NULL, INT64_MAX );
1260 vlc_mutex_unlock( &p_owner->lock );
1262 if( p_subpic->i_start <= VLC_TS_INVALID
1263 || DecoderTimedWait( p_dec, p_subpic->i_start - SPU_MAX_PREPARE_TIME ) )
1265 subpicture_Delete( p_subpic );
1266 return;
1269 vout_PutSubpicture( p_vout, p_subpic );
1272 static void DecoderUpdateStatSpu( decoder_owner_sys_t *p_owner,
1273 unsigned decoded, unsigned lost )
1275 (void) p_owner; (void) decoded; (void) lost;
1278 static int DecoderQueueSpu( decoder_t *p_dec, subpicture_t *p_spu )
1280 assert( p_spu );
1281 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1282 input_thread_t *p_input = p_owner->p_input;
1284 if( p_input != NULL )
1286 vlc_mutex_lock( &input_priv(p_input)->counters.counters_lock );
1287 stats_Update( input_priv(p_input)->counters.p_decoded_sub, 1, NULL );
1288 vlc_mutex_unlock( &input_priv(p_input)->counters.counters_lock );
1291 int i_ret = -1;
1292 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1293 if( p_vout && p_owner->p_spu_vout == p_vout )
1295 /* Preroll does not work very well with subtitle */
1296 vlc_mutex_lock( &p_owner->lock );
1297 if( p_spu->i_start > VLC_TS_INVALID &&
1298 p_spu->i_start < p_owner->i_preroll_end &&
1299 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1301 vlc_mutex_unlock( &p_owner->lock );
1302 subpicture_Delete( p_spu );
1304 else
1306 vlc_mutex_unlock( &p_owner->lock );
1307 DecoderPlaySpu( p_dec, p_spu );
1308 i_ret = 0;
1311 else
1313 subpicture_Delete( p_spu );
1315 if( p_vout )
1316 vlc_object_release( p_vout );
1317 return i_ret;
1320 static void DecoderProcess( decoder_t *p_dec, block_t *p_block );
1321 static void DecoderDecode( decoder_t *p_dec, block_t *p_block )
1323 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1325 int ret = p_dec->pf_decode( p_dec, p_block );
1326 switch( ret )
1328 case VLCDEC_SUCCESS:
1329 p_owner->pf_update_stat( p_owner, 1, 0 );
1330 break;
1331 case VLCDEC_ECRITICAL:
1332 p_owner->error = true;
1333 break;
1334 case VLCDEC_RELOAD:
1335 RequestReload( p_dec );
1336 if( unlikely( p_block == NULL ) )
1337 break;
1338 if( !( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1340 p_block->i_flags |= BLOCK_FLAG_CORE_PRIVATE_RELOADED;
1341 DecoderProcess( p_dec, p_block );
1343 else /* We prefer loosing this block than an infinite recursion */
1344 block_Release( p_block );
1345 break;
1346 default:
1347 vlc_assert_unreachable();
1352 * Decode a block
1354 * \param p_dec the decoder object
1355 * \param p_block the block to decode
1357 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1359 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1361 if( p_owner->error )
1362 goto error;
1364 /* Here, the atomic doesn't prevent to miss a reload request.
1365 * DecoderProcess() can still be called after the decoder module or the
1366 * audio output requested a reload. This will only result in a drop of an
1367 * input block or an output buffer. */
1368 enum reload reload;
1369 if( ( reload = atomic_exchange( &p_owner->reload, RELOAD_NO_REQUEST ) ) )
1371 msg_Warn( p_dec, "Reloading the decoder module%s",
1372 reload == RELOAD_DECODER_AOUT ? " and the audio output" : "" );
1374 if( ReloadDecoder( p_dec, false, &p_dec->fmt_in, reload ) != VLC_SUCCESS )
1375 goto error;
1378 bool packetize = p_owner->p_packetizer != NULL;
1379 if( p_block )
1381 if( p_block->i_buffer <= 0 )
1382 goto error;
1384 vlc_mutex_lock( &p_owner->lock );
1385 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1386 vlc_mutex_unlock( &p_owner->lock );
1387 if( unlikely( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1389 /* This block has already been packetized */
1390 packetize = false;
1394 #ifdef ENABLE_SOUT
1395 if( p_owner->p_sout != NULL )
1397 DecoderProcessSout( p_dec, p_block );
1398 return;
1400 #endif
1401 if( packetize )
1403 block_t *p_packetized_block;
1404 block_t **pp_block = p_block ? &p_block : NULL;
1405 decoder_t *p_packetizer = p_owner->p_packetizer;
1407 while( (p_packetized_block =
1408 p_packetizer->pf_packetize( p_packetizer, pp_block ) ) )
1410 if( !es_format_IsSimilar( &p_dec->fmt_in, &p_packetizer->fmt_out ) )
1412 msg_Dbg( p_dec, "restarting module due to input format change");
1414 /* Drain the decoder module */
1415 DecoderDecode( p_dec, NULL );
1417 if( ReloadDecoder( p_dec, false, &p_packetizer->fmt_out,
1418 RELOAD_DECODER ) != VLC_SUCCESS )
1420 block_ChainRelease( p_packetized_block );
1421 return;
1425 if( p_packetizer->pf_get_cc )
1426 PacketizerGetCc( p_dec, p_packetizer );
1428 while( p_packetized_block )
1430 block_t *p_next = p_packetized_block->p_next;
1431 p_packetized_block->p_next = NULL;
1433 DecoderDecode( p_dec, p_packetized_block );
1434 if( p_owner->error )
1436 block_ChainRelease( p_next );
1437 return;
1440 p_packetized_block = p_next;
1443 /* Drain the decoder after the packetizer is drained */
1444 if( !pp_block )
1445 DecoderDecode( p_dec, NULL );
1447 else
1448 DecoderDecode( p_dec, p_block );
1449 return;
1451 error:
1452 if( p_block )
1453 block_Release( p_block );
1456 static void DecoderProcessFlush( decoder_t *p_dec )
1458 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1459 decoder_t *p_packetizer = p_owner->p_packetizer;
1461 if( p_owner->error )
1462 return;
1464 if( p_packetizer != NULL && p_packetizer->pf_flush != NULL )
1465 p_packetizer->pf_flush( p_packetizer );
1467 if ( p_dec->pf_flush != NULL )
1468 p_dec->pf_flush( p_dec );
1470 /* flush CC sub decoders */
1471 if( p_owner->cc.b_supported )
1473 for( int i=0; i<MAX_CC_DECODERS; i++ )
1475 decoder_t *p_subdec = p_owner->cc.pp_decoder[i];
1476 if( p_subdec && p_subdec->pf_flush )
1477 p_subdec->pf_flush( p_subdec );
1481 #ifdef ENABLE_SOUT
1482 if ( p_owner->p_sout_input != NULL )
1484 sout_InputFlush( p_owner->p_sout_input );
1486 #endif
1487 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1489 if( p_owner->p_aout )
1490 aout_DecFlush( p_owner->p_aout, false );
1492 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1494 if( p_owner->p_vout )
1495 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1497 else if( p_dec->fmt_out.i_cat == SPU_ES )
1499 if( p_owner->p_spu_vout )
1501 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1503 if( p_vout && p_owner->p_spu_vout == p_vout )
1504 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1506 if( p_vout )
1507 vlc_object_release( p_vout );
1511 vlc_mutex_lock( &p_owner->lock );
1512 p_owner->i_preroll_end = INT64_MIN;
1513 vlc_mutex_unlock( &p_owner->lock );
1517 * The decoding main loop
1519 * \param p_dec the decoder
1521 static void *DecoderThread( void *p_data )
1523 decoder_t *p_dec = (decoder_t *)p_data;
1524 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1525 bool paused = false;
1527 /* The decoder's main loop */
1528 vlc_fifo_Lock( p_owner->p_fifo );
1529 vlc_fifo_CleanupPush( p_owner->p_fifo );
1531 for( ;; )
1533 if( p_owner->flushing )
1534 { /* Flush before/regardless of pause. We do not want to resume just
1535 * for the sake of flushing (glitches could otherwise happen). */
1536 int canc = vlc_savecancel();
1538 vlc_fifo_Unlock( p_owner->p_fifo );
1540 /* Flush the decoder (and the output) */
1541 DecoderProcessFlush( p_dec );
1543 vlc_fifo_Lock( p_owner->p_fifo );
1544 vlc_restorecancel( canc );
1546 /* Reset flushing after DecoderProcess in case input_DecoderFlush
1547 * is called again. This will avoid a second useless flush (but
1548 * harmless). */
1549 p_owner->flushing = false;
1551 continue;
1554 if( paused != p_owner->paused )
1555 { /* Update playing/paused status of the output */
1556 int canc = vlc_savecancel();
1557 mtime_t date = p_owner->pause_date;
1559 paused = p_owner->paused;
1560 vlc_fifo_Unlock( p_owner->p_fifo );
1562 /* NOTE: Only the audio and video outputs care about pause. */
1563 msg_Dbg( p_dec, "toggling %s", paused ? "resume" : "pause" );
1564 if( p_owner->p_vout != NULL )
1565 vout_ChangePause( p_owner->p_vout, paused, date );
1566 if( p_owner->p_aout != NULL )
1567 aout_DecChangePause( p_owner->p_aout, paused, date );
1569 vlc_restorecancel( canc );
1570 vlc_fifo_Lock( p_owner->p_fifo );
1571 continue;
1574 if( p_owner->paused && p_owner->frames_countdown == 0 )
1575 { /* Wait for resumption from pause */
1576 p_owner->b_idle = true;
1577 vlc_cond_signal( &p_owner->wait_acknowledge );
1578 vlc_fifo_Wait( p_owner->p_fifo );
1579 p_owner->b_idle = false;
1580 continue;
1583 vlc_cond_signal( &p_owner->wait_fifo );
1584 vlc_testcancel(); /* forced expedited cancellation in case of stop */
1586 block_t *p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
1587 if( p_block == NULL )
1589 if( likely(!p_owner->b_draining) )
1590 { /* Wait for a block to decode (or a request to drain) */
1591 p_owner->b_idle = true;
1592 vlc_cond_signal( &p_owner->wait_acknowledge );
1593 vlc_fifo_Wait( p_owner->p_fifo );
1594 p_owner->b_idle = false;
1595 continue;
1597 /* We have emptied the FIFO and there is a pending request to
1598 * drain. Pass p_block = NULL to decoder just once. */
1601 vlc_fifo_Unlock( p_owner->p_fifo );
1603 int canc = vlc_savecancel();
1604 DecoderProcess( p_dec, p_block );
1606 if( p_block == NULL )
1607 { /* Draining: the decoder is drained and all decoded buffers are
1608 * queued to the output at this point. Now drain the output. */
1609 if( p_owner->p_aout != NULL )
1610 aout_DecFlush( p_owner->p_aout, true );
1612 vlc_restorecancel( canc );
1614 /* TODO? Wait for draining instead of polling. */
1615 vlc_mutex_lock( &p_owner->lock );
1616 if( p_owner->b_draining && (p_block == NULL) )
1618 p_owner->b_draining = false;
1619 p_owner->drained = true;
1621 vlc_fifo_Lock( p_owner->p_fifo );
1622 vlc_cond_signal( &p_owner->wait_acknowledge );
1623 vlc_mutex_unlock( &p_owner->lock );
1625 vlc_cleanup_pop();
1626 vlc_assert_unreachable();
1630 * Create a decoder object
1632 * \param p_input the input thread
1633 * \param p_es the es descriptor
1634 * \param b_packetizer instead of a decoder
1635 * \return the decoder object
1637 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
1638 input_thread_t *p_input,
1639 const es_format_t *fmt,
1640 input_resource_t *p_resource,
1641 sout_instance_t *p_sout )
1643 decoder_t *p_dec;
1644 decoder_owner_sys_t *p_owner;
1646 p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
1647 if( p_dec == NULL )
1648 return NULL;
1650 /* Allocate our private structure for the decoder */
1651 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
1652 if( unlikely(p_owner == NULL) )
1654 vlc_object_release( p_dec );
1655 return NULL;
1657 p_owner->i_preroll_end = INT64_MIN;
1658 p_owner->i_last_rate = INPUT_RATE_DEFAULT;
1659 p_owner->p_input = p_input;
1660 p_owner->p_resource = p_resource;
1661 p_owner->p_aout = NULL;
1662 p_owner->p_vout = NULL;
1663 p_owner->p_spu_vout = NULL;
1664 p_owner->i_spu_channel = 0;
1665 p_owner->i_spu_order = 0;
1666 p_owner->p_sout = p_sout;
1667 p_owner->p_sout_input = NULL;
1668 p_owner->p_packetizer = NULL;
1670 p_owner->b_fmt_description = false;
1671 p_owner->p_description = NULL;
1673 p_owner->paused = false;
1674 p_owner->pause_date = VLC_TS_INVALID;
1675 p_owner->frames_countdown = 0;
1677 p_owner->b_waiting = false;
1678 p_owner->b_first = true;
1679 p_owner->b_has_data = false;
1681 p_owner->error = false;
1683 p_owner->flushing = false;
1684 p_owner->b_draining = false;
1685 p_owner->drained = false;
1686 atomic_init( &p_owner->reload, RELOAD_NO_REQUEST );
1687 p_owner->b_idle = false;
1689 es_format_Init( &p_owner->fmt, fmt->i_cat, 0 );
1691 /* decoder fifo */
1692 p_owner->p_fifo = block_FifoNew();
1693 if( unlikely(p_owner->p_fifo == NULL) )
1695 free( p_owner );
1696 vlc_object_release( p_dec );
1697 return NULL;
1700 vlc_mutex_init( &p_owner->lock );
1701 vlc_cond_init( &p_owner->wait_request );
1702 vlc_cond_init( &p_owner->wait_acknowledge );
1703 vlc_cond_init( &p_owner->wait_fifo );
1704 vlc_cond_init( &p_owner->wait_timed );
1706 /* Set buffers allocation callbacks for the decoders */
1707 p_dec->pf_aout_format_update = aout_update_format;
1708 p_dec->pf_vout_format_update = vout_update_format;
1709 p_dec->pf_vout_buffer_new = vout_new_buffer;
1710 p_dec->pf_spu_buffer_new = spu_new_buffer;
1711 /* */
1712 p_dec->pf_get_attachments = DecoderGetInputAttachments;
1713 p_dec->pf_get_display_date = DecoderGetDisplayDate;
1714 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
1716 /* Load a packetizer module if the input is not already packetized */
1717 if( p_sout == NULL && !fmt->b_packetized )
1719 p_owner->p_packetizer =
1720 vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
1721 if( p_owner->p_packetizer )
1723 if( LoadDecoder( p_owner->p_packetizer, true, fmt ) )
1725 vlc_object_release( p_owner->p_packetizer );
1726 p_owner->p_packetizer = NULL;
1728 else
1730 p_owner->p_packetizer->fmt_out.b_packetized = true;
1731 fmt = &p_owner->p_packetizer->fmt_out;
1736 /* Find a suitable decoder/packetizer module */
1737 if( LoadDecoder( p_dec, p_sout != NULL, fmt ) )
1738 return p_dec;
1740 switch( p_dec->fmt_out.i_cat )
1742 case VIDEO_ES:
1743 p_dec->pf_queue_video = DecoderQueueVideo;
1744 p_dec->pf_queue_cc = DecoderQueueCc;
1745 p_owner->pf_update_stat = DecoderUpdateStatVideo;
1746 break;
1747 case AUDIO_ES:
1748 p_dec->pf_queue_audio = DecoderQueueAudio;
1749 p_owner->pf_update_stat = DecoderUpdateStatAudio;
1750 break;
1751 case SPU_ES:
1752 p_dec->pf_queue_sub = DecoderQueueSpu;
1753 p_owner->pf_update_stat = DecoderUpdateStatSpu;
1754 break;
1755 default:
1756 msg_Err( p_dec, "unknown ES format" );
1757 UnloadDecoder( p_dec );
1758 return p_dec;
1760 /* Copy ourself the input replay gain */
1761 if( fmt->i_cat == AUDIO_ES )
1763 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1765 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
1767 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
1768 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
1770 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
1772 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
1773 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
1778 /* */
1779 p_owner->cc.b_supported = ( p_sout == NULL );
1781 p_owner->cc.desc.i_608_channels = 0;
1782 p_owner->cc.desc.i_708_channels = 0;
1783 for( unsigned i = 0; i < MAX_CC_DECODERS; i++ )
1784 p_owner->cc.pp_decoder[i] = NULL;
1785 p_owner->i_ts_delay = 0;
1786 return p_dec;
1790 * Destroys a decoder object
1792 * \param p_dec the decoder object
1793 * \return nothing
1795 static void DeleteDecoder( decoder_t * p_dec )
1797 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1799 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s'",
1800 (char*)&p_dec->fmt_in.i_codec );
1802 const bool b_flush_spu = p_dec->fmt_out.i_cat == SPU_ES;
1803 UnloadDecoder( p_dec );
1805 /* Free all packets still in the decoder fifo. */
1806 block_FifoRelease( p_owner->p_fifo );
1808 /* Cleanup */
1809 if( p_owner->p_aout )
1811 /* TODO: REVISIT gap-less audio */
1812 aout_DecFlush( p_owner->p_aout, false );
1813 aout_DecDelete( p_owner->p_aout );
1814 input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1815 if( p_owner->p_input != NULL )
1816 input_SendEventAout( p_owner->p_input );
1818 if( p_owner->p_vout )
1820 /* Reset the cancel state that was set before joining the decoder
1821 * thread */
1822 vout_Cancel( p_owner->p_vout, false );
1824 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1825 0, true );
1826 if( p_owner->p_input != NULL )
1827 input_SendEventVout( p_owner->p_input );
1830 #ifdef ENABLE_SOUT
1831 if( p_owner->p_sout_input )
1833 sout_InputDelete( p_owner->p_sout_input );
1835 #endif
1836 es_format_Clean( &p_owner->fmt );
1838 if( b_flush_spu )
1840 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1841 if( p_vout )
1843 if( p_owner->p_spu_vout == p_vout )
1844 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1845 vlc_object_release( p_vout );
1849 if( p_owner->p_description )
1850 vlc_meta_Delete( p_owner->p_description );
1852 if( p_owner->p_packetizer )
1854 UnloadDecoder( p_owner->p_packetizer );
1855 vlc_object_release( p_owner->p_packetizer );
1858 vlc_cond_destroy( &p_owner->wait_timed );
1859 vlc_cond_destroy( &p_owner->wait_fifo );
1860 vlc_cond_destroy( &p_owner->wait_acknowledge );
1861 vlc_cond_destroy( &p_owner->wait_request );
1862 vlc_mutex_destroy( &p_owner->lock );
1864 vlc_object_release( p_dec );
1866 free( p_owner );
1869 /* */
1870 static void DecoderUnsupportedCodec( decoder_t *p_dec, const es_format_t *fmt, bool b_decoding )
1872 if (fmt->i_codec != VLC_CODEC_UNKNOWN && fmt->i_codec) {
1873 const char *desc = vlc_fourcc_GetDescription(fmt->i_cat, fmt->i_codec);
1874 if (!desc || !*desc)
1875 desc = N_("No description for this codec");
1876 msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&fmt->i_codec, desc );
1877 vlc_dialog_display_error( p_dec, _("Codec not supported"),
1878 _("VLC could not decode the format \"%4.4s\" (%s)"),
1879 (char*)&fmt->i_codec, desc );
1880 } else if( b_decoding ){
1881 msg_Err( p_dec, "could not identify codec" );
1882 vlc_dialog_display_error( p_dec, _("Unidentified codec"),
1883 _("VLC could not identify the audio or video codec" ) );
1887 /* TODO: pass p_sout through p_resource? -- Courmisch */
1888 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
1889 const es_format_t *fmt, input_clock_t *p_clock,
1890 input_resource_t *p_resource,
1891 sout_instance_t *p_sout )
1893 decoder_t *p_dec = NULL;
1894 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
1895 int i_priority;
1897 /* Create the decoder configuration structure */
1898 p_dec = CreateDecoder( p_parent, p_input, fmt, p_resource, p_sout );
1899 if( p_dec == NULL )
1901 msg_Err( p_parent, "could not create %s", psz_type );
1902 vlc_dialog_display_error( p_parent, _("Streaming / Transcoding failed"),
1903 _("VLC could not open the %s module."), vlc_gettext( psz_type ) );
1904 return NULL;
1907 if( !p_dec->p_module )
1909 DecoderUnsupportedCodec( p_dec, fmt, !p_sout );
1911 DeleteDecoder( p_dec );
1912 return NULL;
1915 p_dec->p_owner->p_clock = p_clock;
1916 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
1918 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1919 i_priority = VLC_THREAD_PRIORITY_AUDIO;
1920 else
1921 i_priority = VLC_THREAD_PRIORITY_VIDEO;
1923 /* Spawn the decoder thread */
1924 if( vlc_clone( &p_dec->p_owner->thread, DecoderThread, p_dec, i_priority ) )
1926 msg_Err( p_dec, "cannot spawn decoder thread" );
1927 DeleteDecoder( p_dec );
1928 return NULL;
1931 return p_dec;
1936 * Spawns a new decoder thread from the input thread
1938 * \param p_input the input thread
1939 * \param p_es the es descriptor
1940 * \return the spawned decoder object
1942 decoder_t *input_DecoderNew( input_thread_t *p_input,
1943 es_format_t *fmt, input_clock_t *p_clock,
1944 sout_instance_t *p_sout )
1946 return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
1947 input_priv(p_input)->p_resource, p_sout );
1951 * Spawn a decoder thread outside of the input thread.
1953 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, const es_format_t *fmt,
1954 input_resource_t *p_resource )
1956 return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
1961 * Kills a decoder thread and waits until it's finished
1963 * \param p_input the input thread
1964 * \param p_es the es descriptor
1965 * \return nothing
1967 void input_DecoderDelete( decoder_t *p_dec )
1969 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1971 vlc_cancel( p_owner->thread );
1973 vlc_fifo_Lock( p_owner->p_fifo );
1974 /* Signal DecoderTimedWait */
1975 p_owner->flushing = true;
1976 vlc_cond_signal( &p_owner->wait_timed );
1977 vlc_fifo_Unlock( p_owner->p_fifo );
1979 /* Make sure we aren't waiting/decoding anymore */
1980 vlc_mutex_lock( &p_owner->lock );
1981 p_owner->b_waiting = false;
1982 vlc_cond_signal( &p_owner->wait_request );
1984 /* If the video output is paused or slow, or if the picture pool size was
1985 * under-estimated (e.g. greedy video filter, buggy decoder...), the
1986 * the picture pool may be empty, and the decoder thread or any decoder
1987 * module worker threads may be stuck waiting for free picture buffers.
1989 * This unblocks the thread, allowing the decoder module to join all its
1990 * worker threads (if any) and the decoder thread to terminate. */
1991 if( p_owner->p_vout != NULL )
1992 vout_Cancel( p_owner->p_vout, true );
1993 vlc_mutex_unlock( &p_owner->lock );
1995 vlc_join( p_owner->thread, NULL );
1997 /* */
1998 if( p_dec->p_owner->cc.b_supported )
2000 for( int i = 0; i < MAX_CC_DECODERS; i++ )
2001 input_DecoderSetCcState( p_dec, VLC_CODEC_CEA608, i, false );
2004 /* Delete decoder */
2005 DeleteDecoder( p_dec );
2009 * Put a block_t in the decoder's fifo.
2010 * Thread-safe w.r.t. the decoder. May be a cancellation point.
2012 * \param p_dec the decoder object
2013 * \param p_block the data block
2015 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
2017 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2019 vlc_fifo_Lock( p_owner->p_fifo );
2020 if( !b_do_pace )
2022 /* FIXME: ideally we would check the time amount of data
2023 * in the FIFO instead of its size. */
2024 /* 400 MiB, i.e. ~ 50mb/s for 60s */
2025 if( vlc_fifo_GetBytes( p_owner->p_fifo ) > 400*1024*1024 )
2027 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
2028 "consumed quickly enough), resetting fifo!" );
2029 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2032 else
2033 if( !p_owner->b_waiting )
2034 { /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
2035 * Locking is not necessary as b_waiting is only read, not written by
2036 * the decoder thread. */
2037 while( vlc_fifo_GetCount( p_owner->p_fifo ) >= 10 )
2038 vlc_fifo_WaitCond( p_owner->p_fifo, &p_owner->wait_fifo );
2041 vlc_fifo_QueueUnlocked( p_owner->p_fifo, p_block );
2042 vlc_fifo_Unlock( p_owner->p_fifo );
2045 bool input_DecoderIsEmpty( decoder_t * p_dec )
2047 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2049 assert( !p_owner->b_waiting );
2051 vlc_fifo_Lock( p_owner->p_fifo );
2052 if( !vlc_fifo_IsEmpty( p_dec->p_owner->p_fifo ) || p_owner->b_draining )
2054 vlc_fifo_Unlock( p_owner->p_fifo );
2055 return false;
2057 vlc_fifo_Unlock( p_owner->p_fifo );
2059 bool b_empty;
2061 vlc_mutex_lock( &p_owner->lock );
2062 #ifdef ENABLE_SOUT
2063 if( p_owner->p_sout_input != NULL )
2064 b_empty = sout_InputIsEmpty( p_owner->p_sout_input );
2065 else
2066 #endif
2067 if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->p_vout != NULL )
2068 b_empty = vout_IsEmpty( p_owner->p_vout );
2069 else if( p_owner->fmt.i_cat == AUDIO_ES )
2070 b_empty = !p_owner->b_draining || p_owner->drained;
2071 else
2072 b_empty = true; /* TODO subtitles support */
2073 vlc_mutex_unlock( &p_owner->lock );
2075 return b_empty;
2079 * Signals that there are no further blocks to decode, and requests that the
2080 * decoder drain all pending buffers. This is used to ensure that all
2081 * intermediate buffers empty and no samples get lost at the end of the stream.
2083 * @note The function does not actually wait for draining. It just signals that
2084 * draining should be performed once the decoder has emptied FIFO.
2086 void input_DecoderDrain( decoder_t *p_dec )
2088 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2090 vlc_fifo_Lock( p_owner->p_fifo );
2091 p_owner->b_draining = true;
2092 vlc_fifo_Signal( p_owner->p_fifo );
2093 vlc_fifo_Unlock( p_owner->p_fifo );
2097 * Requests that the decoder immediately discard all pending buffers.
2098 * This is useful when seeking or when deselecting a stream.
2100 void input_DecoderFlush( decoder_t *p_dec )
2102 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2104 vlc_fifo_Lock( p_owner->p_fifo );
2106 /* Empty the fifo */
2107 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2109 /* Don't need to wait for the DecoderThread to flush. Indeed, if called a
2110 * second time, this function will clear the FIFO again before anything was
2111 * dequeued by DecoderThread and there is no need to flush a second time in
2112 * a row. */
2113 p_owner->flushing = true;
2115 /* Flushing video decoder when paused: increment frames_countdown in order
2116 * to display one frame */
2117 if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->paused
2118 && p_owner->frames_countdown == 0 )
2119 p_owner->frames_countdown++;
2121 vlc_fifo_Signal( p_owner->p_fifo );
2122 vlc_cond_signal( &p_owner->wait_timed );
2124 vlc_fifo_Unlock( p_owner->p_fifo );
2127 void input_DecoderGetCcDesc( decoder_t *p_dec, decoder_cc_desc_t *p_desc )
2129 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2131 vlc_mutex_lock( &p_owner->lock );
2132 *p_desc = p_owner->cc.desc;
2133 vlc_mutex_unlock( &p_owner->lock );
2136 static bool input_DecoderHasCCChanFlag( decoder_t *p_dec,
2137 vlc_fourcc_t codec, int i_channel )
2139 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2141 int i_max_channels;
2142 uint64_t i_bitmap;
2143 if( codec == VLC_CODEC_CEA608 )
2145 i_max_channels = 4;
2146 i_bitmap = p_owner->cc.desc.i_608_channels;
2148 else if( codec == VLC_CODEC_CEA708 )
2150 i_max_channels = 64;
2151 i_bitmap = p_owner->cc.desc.i_708_channels;
2153 else return false;
2155 return ( i_channel >= 0 && i_channel < i_max_channels &&
2156 ( i_bitmap & ((uint64_t)1 << i_channel) ) );
2159 int input_DecoderSetCcState( decoder_t *p_dec, vlc_fourcc_t codec,
2160 int i_channel, bool b_decode )
2162 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2164 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%x", b_decode, i_channel );
2166 if( !input_DecoderHasCCChanFlag( p_dec, codec, i_channel ) )
2167 return VLC_EGENERIC;
2169 if( b_decode )
2171 decoder_t *p_cc;
2172 es_format_t fmt;
2174 es_format_Init( &fmt, SPU_ES, codec );
2175 fmt.subs.cc.i_channel = i_channel;
2176 fmt.subs.cc.i_reorder_depth = p_owner->cc.desc.i_reorder_depth;
2177 p_cc = input_DecoderNew( p_owner->p_input, &fmt,
2178 p_dec->p_owner->p_clock, p_owner->p_sout );
2179 if( !p_cc )
2181 msg_Err( p_dec, "could not create decoder" );
2182 vlc_dialog_display_error( p_dec,
2183 _("Streaming / Transcoding failed"), "%s",
2184 _("VLC could not open the decoder module.") );
2185 return VLC_EGENERIC;
2187 else if( !p_cc->p_module )
2189 DecoderUnsupportedCodec( p_dec, &fmt, true );
2190 input_DecoderDelete(p_cc);
2191 return VLC_EGENERIC;
2193 p_cc->p_owner->p_clock = p_owner->p_clock;
2195 vlc_mutex_lock( &p_owner->lock );
2196 p_owner->cc.pp_decoder[i_channel] = p_cc;
2197 vlc_mutex_unlock( &p_owner->lock );
2199 else
2201 decoder_t *p_cc;
2203 vlc_mutex_lock( &p_owner->lock );
2204 p_cc = p_owner->cc.pp_decoder[i_channel];
2205 p_owner->cc.pp_decoder[i_channel] = NULL;
2206 vlc_mutex_unlock( &p_owner->lock );
2208 if( p_cc )
2209 input_DecoderDelete(p_cc);
2211 return VLC_SUCCESS;
2214 int input_DecoderGetCcState( decoder_t *p_dec, vlc_fourcc_t codec,
2215 int i_channel, bool *pb_decode )
2217 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2219 if( !input_DecoderHasCCChanFlag( p_dec, codec, i_channel ) )
2220 return VLC_EGENERIC;
2222 vlc_mutex_lock( &p_owner->lock );
2223 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
2224 vlc_mutex_unlock( &p_owner->lock );
2225 return VLC_SUCCESS;
2228 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
2230 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2232 /* Normally, p_owner->b_paused != b_paused here. But if a track is added
2233 * while the input is paused (e.g. add sub file), then b_paused is
2234 * (incorrectly) false. FIXME: This is a bug in the decoder owner. */
2235 vlc_fifo_Lock( p_owner->p_fifo );
2236 p_owner->paused = b_paused;
2237 p_owner->pause_date = i_date;
2238 p_owner->frames_countdown = 0;
2239 vlc_fifo_Signal( p_owner->p_fifo );
2240 vlc_fifo_Unlock( p_owner->p_fifo );
2243 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
2245 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2247 vlc_mutex_lock( &p_owner->lock );
2248 p_owner->i_ts_delay = i_delay;
2249 vlc_mutex_unlock( &p_owner->lock );
2252 void input_DecoderStartWait( decoder_t *p_dec )
2254 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2256 assert( !p_owner->b_waiting );
2258 vlc_mutex_lock( &p_owner->lock );
2259 p_owner->b_first = true;
2260 p_owner->b_has_data = false;
2261 p_owner->b_waiting = true;
2262 vlc_cond_signal( &p_owner->wait_request );
2263 vlc_mutex_unlock( &p_owner->lock );
2266 void input_DecoderStopWait( decoder_t *p_dec )
2268 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2270 assert( p_owner->b_waiting );
2272 vlc_mutex_lock( &p_owner->lock );
2273 p_owner->b_waiting = false;
2274 vlc_cond_signal( &p_owner->wait_request );
2275 vlc_mutex_unlock( &p_owner->lock );
2278 void input_DecoderWait( decoder_t *p_dec )
2280 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2282 assert( p_owner->b_waiting );
2284 vlc_mutex_lock( &p_owner->lock );
2285 while( !p_owner->b_has_data )
2287 /* Don't need to lock p_owner->paused since it's only modified by the
2288 * owner */
2289 if( p_owner->paused )
2290 break;
2291 vlc_fifo_Lock( p_owner->p_fifo );
2292 if( p_owner->b_idle && vlc_fifo_IsEmpty( p_owner->p_fifo ) )
2294 msg_Err( p_dec, "buffer deadlock prevented" );
2295 vlc_fifo_Unlock( p_owner->p_fifo );
2296 break;
2298 vlc_fifo_Unlock( p_owner->p_fifo );
2299 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
2301 vlc_mutex_unlock( &p_owner->lock );
2304 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
2306 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2308 assert( p_owner->paused );
2309 *pi_duration = 0;
2311 vlc_fifo_Lock( p_owner->p_fifo );
2312 p_owner->frames_countdown++;
2313 vlc_fifo_Signal( p_owner->p_fifo );
2314 vlc_fifo_Unlock( p_owner->p_fifo );
2316 vlc_mutex_lock( &p_owner->lock );
2317 if( p_owner->fmt.i_cat == VIDEO_ES )
2319 if( p_owner->p_vout )
2320 vout_NextPicture( p_owner->p_vout, pi_duration );
2322 vlc_mutex_unlock( &p_owner->lock );
2325 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
2327 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2328 bool b_changed;
2330 vlc_mutex_lock( &p_owner->lock );
2331 b_changed = p_owner->b_fmt_description;
2332 if( b_changed )
2334 if( p_fmt != NULL )
2335 es_format_Copy( p_fmt, &p_owner->fmt );
2337 if( pp_meta )
2339 *pp_meta = NULL;
2340 if( p_owner->p_description )
2342 *pp_meta = vlc_meta_New();
2343 if( *pp_meta )
2344 vlc_meta_Merge( *pp_meta, p_owner->p_description );
2347 p_owner->b_fmt_description = false;
2349 vlc_mutex_unlock( &p_owner->lock );
2350 return b_changed;
2353 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
2355 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2357 return block_FifoSize( p_owner->p_fifo );
2360 void input_DecoderGetObjects( decoder_t *p_dec,
2361 vout_thread_t **pp_vout, audio_output_t **pp_aout )
2363 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2365 vlc_mutex_lock( &p_owner->lock );
2366 if( pp_vout )
2367 *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
2368 if( pp_aout )
2369 *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
2370 vlc_mutex_unlock( &p_owner->lock );