dec: access vout/aout only if the category matches
[vlc.git] / src / input / decoder.c
blobacd0b4ec0a2a57ea3d1c8e4a655c60dd6e8572f0
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>
33 #include <stdatomic.h>
35 #include <vlc_common.h>
36 #include <vlc_block.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41 #include <vlc_spu.h>
42 #include <vlc_meta.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"
49 #include "../clock/input_clock.h"
50 #include "decoder.h"
51 #include "event.h"
52 #include "resource.h"
54 #include "../video_output/vout_internal.h"
57 * Possibles values set in p_owner->reload atomic
59 enum reload
61 RELOAD_NO_REQUEST,
62 RELOAD_DECODER, /* Reload the decoder module */
63 RELOAD_DECODER_AOUT /* Stop the aout and reload the decoder module */
66 struct decoder_owner
68 decoder_t dec;
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)( struct decoder_owner *, 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 mtime_t i_preroll_end;
115 /* Pause & Rate */
116 mtime_t pause_date;
117 float rate;
118 unsigned frames_countdown;
119 bool paused;
121 bool error;
123 /* Waiting */
124 bool b_waiting;
125 bool b_first;
126 bool b_has_data;
128 /* Flushing */
129 bool flushing;
130 bool b_draining;
131 atomic_bool drained;
132 bool b_idle;
134 /* CC */
135 #define MAX_CC_DECODERS 64 /* The es_out only creates one type of es */
136 struct
138 bool b_supported;
139 decoder_cc_desc_t desc;
140 decoder_t *pp_decoder[MAX_CC_DECODERS];
141 } cc;
143 /* Delay */
144 mtime_t i_ts_delay;
147 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
148 * a bogus PTS and won't be displayed */
149 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
151 /* */
152 #define DECODER_SPU_VOUT_WAIT_DURATION (CLOCK_FREQ/5)
153 #define BLOCK_FLAG_CORE_PRIVATE_RELOADED (1 << BLOCK_FLAG_CORE_PRIVATE_SHIFT)
155 #define VLC_TS_OLDEST (VLC_TS_INVALID + 1)
157 static inline struct decoder_owner *dec_get_owner( decoder_t *p_dec )
159 return container_of( p_dec, struct decoder_owner, dec );
163 * Load a decoder module
165 static int LoadDecoder( decoder_t *p_dec, bool b_packetizer,
166 const es_format_t *restrict p_fmt )
168 p_dec->b_frame_drop_allowed = true;
169 p_dec->i_extra_picture_buffers = 0;
171 p_dec->pf_decode = NULL;
172 p_dec->pf_get_cc = NULL;
173 p_dec->pf_packetize = NULL;
174 p_dec->pf_flush = NULL;
176 es_format_Copy( &p_dec->fmt_in, p_fmt );
177 es_format_Init( &p_dec->fmt_out, p_fmt->i_cat, 0 );
179 /* Find a suitable decoder/packetizer module */
180 if( !b_packetizer )
182 static const char caps[ES_CATEGORY_COUNT][16] = {
183 [VIDEO_ES] = "video decoder",
184 [AUDIO_ES] = "audio decoder",
185 [SPU_ES] = "spu decoder",
187 p_dec->p_module = module_need_var( p_dec, caps[p_dec->fmt_in.i_cat],
188 "codec" );
190 else
191 p_dec->p_module = module_need_var( p_dec, "packetizer", "packetizer" );
193 if( !p_dec->p_module )
195 es_format_Clean( &p_dec->fmt_in );
196 return -1;
198 else
199 return 0;
203 * Unload a decoder module
205 static void UnloadDecoder( decoder_t *p_dec )
207 if( p_dec->p_module )
209 module_unneed( p_dec, p_dec->p_module );
210 p_dec->p_module = NULL;
213 if( p_dec->p_description )
215 vlc_meta_Delete( p_dec->p_description );
216 p_dec->p_description = NULL;
219 es_format_Clean( &p_dec->fmt_in );
220 es_format_Clean( &p_dec->fmt_out );
223 static int ReloadDecoder( decoder_t *p_dec, bool b_packetizer,
224 const es_format_t *restrict p_fmt, enum reload reload )
226 /* Copy p_fmt since it can be destroyed by UnloadDecoder */
227 struct decoder_owner *p_owner = dec_get_owner( p_dec );
228 es_format_t fmt_in;
229 if( es_format_Copy( &fmt_in, p_fmt ) != VLC_SUCCESS )
231 p_owner->error = true;
232 return VLC_EGENERIC;
235 /* Restart the decoder module */
236 UnloadDecoder( p_dec );
237 p_owner->error = false;
239 if( reload == RELOAD_DECODER_AOUT )
241 assert( p_owner->fmt.i_cat == AUDIO_ES );
242 audio_output_t *p_aout = p_owner->p_aout;
244 vlc_mutex_lock( &p_owner->lock );
245 p_owner->p_aout = NULL;
246 vlc_mutex_unlock( &p_owner->lock );
247 if( p_aout )
249 aout_DecDelete( p_aout );
250 input_resource_PutAout( p_owner->p_resource, p_aout );
254 if( LoadDecoder( p_dec, b_packetizer, &fmt_in ) )
256 p_owner->error = true;
257 es_format_Clean( &fmt_in );
258 return VLC_EGENERIC;
260 es_format_Clean( &fmt_in );
261 return VLC_SUCCESS;
264 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
266 struct decoder_owner *p_owner = dec_get_owner( p_dec );
268 vlc_assert_locked( &p_owner->lock );
270 es_format_Clean( &p_owner->fmt );
271 es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
273 /* Move p_description */
274 if( p_dec->p_description != NULL )
276 if( p_owner->p_description != NULL )
277 vlc_meta_Delete( p_owner->p_description );
278 p_owner->p_description = p_dec->p_description;
279 p_dec->p_description = NULL;
282 p_owner->b_fmt_description = true;
285 /*****************************************************************************
286 * Buffers allocation callbacks for the decoders
287 *****************************************************************************/
288 static vout_thread_t *aout_request_vout( void *p_private,
289 vout_thread_t *p_vout,
290 const video_format_t *p_fmt, bool b_recyle )
292 decoder_t *p_dec = p_private;
293 struct decoder_owner *p_owner = dec_get_owner( p_dec );
294 input_thread_t *p_input = p_owner->p_input;
296 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
297 b_recyle );
298 if( p_input != NULL )
299 input_SendEventVout( p_input );
301 return p_vout;
304 static bool aout_replaygain_changed( const audio_replay_gain_t *a,
305 const audio_replay_gain_t *b )
307 for( size_t i=0; i<AUDIO_REPLAY_GAIN_MAX; i++ )
309 if( a->pb_gain[i] != b->pb_gain[i] ||
310 a->pb_peak[i] != b->pb_peak[i] ||
311 a->pb_gain[i] != b->pb_gain[i] ||
312 a->pb_peak[i] != b->pb_peak[i] )
313 return true;
315 return false;
318 static int aout_update_format( decoder_t *p_dec )
320 struct decoder_owner *p_owner = dec_get_owner( p_dec );
322 if( p_owner->p_aout &&
323 ( !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->fmt.audio) ||
324 p_dec->fmt_out.i_codec != p_dec->fmt_out.audio.i_format ||
325 p_dec->fmt_out.i_profile != p_owner->fmt.i_profile ) )
327 audio_output_t *p_aout = p_owner->p_aout;
329 /* Parameters changed, restart the aout */
330 vlc_mutex_lock( &p_owner->lock );
331 p_owner->p_aout = NULL;
332 vlc_mutex_unlock( &p_owner->lock );
333 aout_DecDelete( p_aout );
335 input_resource_PutAout( p_owner->p_resource, p_aout );
338 /* Check if only replay gain has changed */
339 if( aout_replaygain_changed( &p_dec->fmt_in.audio_replay_gain,
340 &p_owner->fmt.audio_replay_gain ) )
342 p_dec->fmt_out.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
343 if( p_owner->p_aout )
345 p_owner->fmt.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
346 var_TriggerCallback( p_owner->p_aout, "audio-replay-gain-mode" );
350 if( p_owner->p_aout == NULL )
352 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
354 audio_sample_format_t format = p_dec->fmt_out.audio;
355 aout_FormatPrepare( &format );
357 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
358 if( i_force_dolby &&
359 format.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
361 if( i_force_dolby == 1 )
362 format.i_chan_mode |= AOUT_CHANMODE_DOLBYSTEREO;
363 else /* i_force_dolby == 2 */
364 format.i_chan_mode &= ~AOUT_CHANMODE_DOLBYSTEREO;
367 aout_request_vout_t request_vout = {
368 .pf_request_vout = aout_request_vout,
369 .p_private = p_dec,
371 audio_output_t *p_aout;
373 p_aout = input_resource_GetAout( p_owner->p_resource );
374 if( p_aout )
376 /* TODO: 3.0 HACK: we need to put i_profile inside audio_format_t
377 * for 4.0 */
378 if( p_dec->fmt_out.i_codec == VLC_CODEC_DTS )
379 var_SetBool( p_aout, "dtshd", p_dec->fmt_out.i_profile > 0 );
381 if( aout_DecNew( p_aout, &format,
382 &p_dec->fmt_out.audio_replay_gain,
383 &request_vout ) )
385 input_resource_PutAout( p_owner->p_resource, p_aout );
386 p_aout = NULL;
390 vlc_mutex_lock( &p_owner->lock );
391 p_owner->p_aout = p_aout;
393 DecoderUpdateFormatLocked( p_dec );
394 aout_FormatPrepare( &p_owner->fmt.audio );
395 vlc_mutex_unlock( &p_owner->lock );
397 if( p_owner->p_input != NULL )
398 input_SendEventAout( p_owner->p_input );
400 if( p_aout == NULL )
402 msg_Err( p_dec, "failed to create audio output" );
403 return -1;
406 p_dec->fmt_out.audio.i_bytes_per_frame =
407 p_owner->fmt.audio.i_bytes_per_frame;
408 p_dec->fmt_out.audio.i_frame_length =
409 p_owner->fmt.audio.i_frame_length;
411 return 0;
414 static int vout_update_format( decoder_t *p_dec )
416 struct decoder_owner *p_owner = dec_get_owner( p_dec );
418 if( p_owner->p_vout == NULL
419 || p_dec->fmt_out.video.i_width != p_owner->fmt.video.i_width
420 || p_dec->fmt_out.video.i_height != p_owner->fmt.video.i_height
421 || p_dec->fmt_out.video.i_visible_width != p_owner->fmt.video.i_visible_width
422 || p_dec->fmt_out.video.i_visible_height != p_owner->fmt.video.i_visible_height
423 || p_dec->fmt_out.video.i_x_offset != p_owner->fmt.video.i_x_offset
424 || p_dec->fmt_out.video.i_y_offset != p_owner->fmt.video.i_y_offset
425 || p_dec->fmt_out.i_codec != p_owner->fmt.video.i_chroma
426 || (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->fmt.video.i_sar_den !=
427 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->fmt.video.i_sar_num ||
428 p_dec->fmt_out.video.orientation != p_owner->fmt.video.orientation ||
429 p_dec->fmt_out.video.multiview_mode != p_owner->fmt.video.multiview_mode )
431 vout_thread_t *p_vout;
433 if( !p_dec->fmt_out.video.i_width ||
434 !p_dec->fmt_out.video.i_height ||
435 p_dec->fmt_out.video.i_width < p_dec->fmt_out.video.i_visible_width ||
436 p_dec->fmt_out.video.i_height < p_dec->fmt_out.video.i_visible_height )
438 /* Can't create a new vout without display size */
439 return -1;
442 video_format_t fmt = p_dec->fmt_out.video;
443 fmt.i_chroma = p_dec->fmt_out.i_codec;
445 if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
447 const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
448 for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
450 while( fmt.i_width % dsc->p[i].w.den )
451 fmt.i_width++;
452 while( fmt.i_height % dsc->p[i].h.den )
453 fmt.i_height++;
457 if( !fmt.i_visible_width || !fmt.i_visible_height )
459 if( p_dec->fmt_in.video.i_visible_width &&
460 p_dec->fmt_in.video.i_visible_height )
462 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
463 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
464 fmt.i_x_offset = p_dec->fmt_in.video.i_x_offset;
465 fmt.i_y_offset = p_dec->fmt_in.video.i_y_offset;
467 else
469 fmt.i_visible_width = fmt.i_width;
470 fmt.i_visible_height = fmt.i_height;
471 fmt.i_x_offset = 0;
472 fmt.i_y_offset = 0;
476 if( fmt.i_visible_height == 1088 &&
477 var_CreateGetBool( p_dec, "hdtv-fix" ) )
479 fmt.i_visible_height = 1080;
480 if( !(fmt.i_sar_num % 136))
482 fmt.i_sar_num *= 135;
483 fmt.i_sar_den *= 136;
485 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
488 if( !fmt.i_sar_num || !fmt.i_sar_den )
490 fmt.i_sar_num = 1;
491 fmt.i_sar_den = 1;
494 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
495 fmt.i_sar_num, fmt.i_sar_den, 50000 );
497 video_format_AdjustColorSpace( &fmt );
499 vlc_mutex_lock( &p_owner->lock );
501 p_vout = p_owner->p_vout;
502 p_owner->p_vout = NULL;
503 vlc_mutex_unlock( &p_owner->lock );
505 unsigned dpb_size;
506 switch( p_dec->fmt_in.i_codec )
508 case VLC_CODEC_HEVC:
509 case VLC_CODEC_H264:
510 case VLC_CODEC_DIRAC: /* FIXME valid ? */
511 dpb_size = 18;
512 break;
513 case VLC_CODEC_VP5:
514 case VLC_CODEC_VP6:
515 case VLC_CODEC_VP6F:
516 case VLC_CODEC_VP8:
517 dpb_size = 3;
518 break;
519 default:
520 dpb_size = 2;
521 break;
523 p_vout = input_resource_RequestVout( p_owner->p_resource,
524 p_vout, &fmt,
525 dpb_size +
526 p_dec->i_extra_picture_buffers + 1,
527 true );
528 vlc_mutex_lock( &p_owner->lock );
529 p_owner->p_vout = p_vout;
531 DecoderUpdateFormatLocked( p_dec );
532 p_owner->fmt.video.i_chroma = p_dec->fmt_out.i_codec;
533 vlc_mutex_unlock( &p_owner->lock );
535 if( p_owner->p_input != NULL )
536 input_SendEventVout( p_owner->p_input );
537 if( p_vout == NULL )
539 msg_Err( p_dec, "failed to create video output" );
540 return -1;
544 if ( memcmp( &p_dec->fmt_out.video.mastering,
545 &p_owner->fmt.video.mastering,
546 sizeof(p_owner->fmt.video.mastering)) ||
547 p_dec->fmt_out.video.lighting.MaxCLL !=
548 p_owner->fmt.video.lighting.MaxCLL ||
549 p_dec->fmt_out.video.lighting.MaxFALL !=
550 p_owner->fmt.video.lighting.MaxFALL)
552 /* the format has changed but we don't need a new vout */
553 vlc_mutex_lock( &p_owner->lock );
554 DecoderUpdateFormatLocked( p_dec );
555 vlc_mutex_unlock( &p_owner->lock );
557 return 0;
560 static picture_t *vout_new_buffer( decoder_t *p_dec )
562 struct decoder_owner *p_owner = dec_get_owner( p_dec );
563 assert( p_owner->p_vout );
565 return vout_GetPicture( p_owner->p_vout );
568 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
569 const subpicture_updater_t *p_updater )
571 struct decoder_owner *p_owner = dec_get_owner( p_dec );
572 vout_thread_t *p_vout = NULL;
573 subpicture_t *p_subpic;
574 int i_attempts = 30;
576 while( i_attempts-- )
578 if( p_owner->error )
579 break;
581 p_vout = input_resource_HoldVout( p_owner->p_resource );
582 if( p_vout )
583 break;
585 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
588 if( !p_vout )
590 msg_Warn( p_dec, "no vout found, dropping subpicture" );
591 return NULL;
594 if( p_owner->p_spu_vout != p_vout )
596 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
597 p_owner->i_spu_order = 0;
598 p_owner->p_spu_vout = p_vout;
601 p_subpic = subpicture_New( p_updater );
602 if( p_subpic )
604 p_subpic->i_channel = p_owner->i_spu_channel;
605 p_subpic->i_order = p_owner->i_spu_order++;
606 p_subpic->b_subtitle = true;
609 vlc_object_release( p_vout );
611 return p_subpic;
614 static int DecoderGetInputAttachments( decoder_t *p_dec,
615 input_attachment_t ***ppp_attachment,
616 int *pi_attachment )
618 struct decoder_owner *p_owner = dec_get_owner( p_dec );
619 input_thread_t *p_input = p_owner->p_input;
621 if( unlikely(p_input == NULL) )
622 return VLC_ENOOBJ;
623 return input_Control( p_input, INPUT_GET_ATTACHMENTS,
624 ppp_attachment, pi_attachment );
627 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
629 struct decoder_owner *p_owner = dec_get_owner( p_dec );
631 vlc_mutex_lock( &p_owner->lock );
632 if( p_owner->b_waiting || p_owner->paused )
633 i_ts = VLC_TS_INVALID;
634 vlc_mutex_unlock( &p_owner->lock );
636 if( !p_owner->p_clock || i_ts == VLC_TS_INVALID )
637 return i_ts;
639 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) ) {
640 msg_Err(p_dec, "Could not get display date for timestamp %"PRId64"", i_ts);
641 return VLC_TS_INVALID;
644 return i_ts;
647 static int DecoderGetDisplayRate( decoder_t *p_dec )
649 struct decoder_owner *p_owner = dec_get_owner( p_dec );
651 if( !p_owner->p_clock )
652 return INPUT_RATE_DEFAULT;
653 return input_clock_GetRate( p_owner->p_clock );
656 /*****************************************************************************
657 * Public functions
658 *****************************************************************************/
659 block_t *decoder_NewAudioBuffer( decoder_t *dec, int samples )
661 assert( dec->fmt_out.audio.i_frame_length > 0
662 && dec->fmt_out.audio.i_bytes_per_frame > 0 );
664 size_t length = samples * dec->fmt_out.audio.i_bytes_per_frame
665 / dec->fmt_out.audio.i_frame_length;
666 block_t *block = block_Alloc( length );
667 if( likely(block != NULL) )
669 block->i_nb_samples = samples;
670 block->i_pts = block->i_length = 0;
672 return block;
675 static void RequestReload( decoder_t * p_dec )
677 struct decoder_owner *p_owner = dec_get_owner( p_dec );
678 /* Don't override reload if it's RELOAD_DECODER_AOUT */
679 int expected = RELOAD_NO_REQUEST;
680 atomic_compare_exchange_strong( &p_owner->reload, &expected, RELOAD_DECODER );
683 void decoder_AbortPictures( decoder_t *p_dec, bool b_abort )
685 struct decoder_owner *p_owner = dec_get_owner( p_dec );
687 vlc_mutex_lock( &p_owner->lock );
688 if( p_owner->p_vout != NULL )
689 vout_Cancel( p_owner->p_vout, b_abort );
690 vlc_mutex_unlock( &p_owner->lock );
693 static void DecoderWaitUnblock( decoder_t *p_dec )
695 struct decoder_owner *p_owner = dec_get_owner( p_dec );
697 vlc_assert_locked( &p_owner->lock );
699 for( ;; )
701 if( !p_owner->b_waiting || !p_owner->b_has_data )
702 break;
703 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
707 /* DecoderTimedWait: Interruptible wait
708 * Returns VLC_SUCCESS if wait was not interrupted, and VLC_EGENERIC otherwise */
709 static int DecoderTimedWait( decoder_t *p_dec, mtime_t deadline )
711 struct decoder_owner *p_owner = dec_get_owner( p_dec );
713 if (deadline - mdate() <= 0)
714 return VLC_SUCCESS;
716 vlc_fifo_Lock( p_owner->p_fifo );
717 while( !p_owner->flushing
718 && vlc_fifo_TimedWaitCond( p_owner->p_fifo, &p_owner->wait_timed,
719 deadline ) == 0 );
720 int ret = p_owner->flushing ? VLC_EGENERIC : VLC_SUCCESS;
721 vlc_fifo_Unlock( p_owner->p_fifo );
722 return ret;
725 static inline void DecoderUpdatePreroll( mtime_t *pi_preroll, const block_t *p )
727 if( p->i_flags & BLOCK_FLAG_PREROLL )
728 *pi_preroll = (mtime_t)INT64_MAX;
729 /* Check if we can use the packet for end of preroll */
730 else if( (p->i_flags & BLOCK_FLAG_DISCONTINUITY) &&
731 (p->i_buffer == 0 || (p->i_flags & BLOCK_FLAG_CORRUPTED)) )
732 *pi_preroll = (mtime_t)INT64_MAX;
733 else if( p->i_dts != VLC_TS_INVALID )
734 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
735 else if( p->i_pts != VLC_TS_INVALID )
736 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
739 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
740 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
742 struct decoder_owner *p_owner = dec_get_owner( p_dec );
743 input_clock_t *p_clock = p_owner->p_clock;
745 vlc_assert_locked( &p_owner->lock );
747 const mtime_t i_es_delay = p_owner->i_ts_delay;
749 if( !p_clock )
750 return;
752 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
753 int i_rate;
755 if( *pi_ts0 != VLC_TS_INVALID )
757 *pi_ts0 += i_es_delay;
758 if( pi_ts1 && *pi_ts1 != VLC_TS_INVALID )
759 *pi_ts1 += i_es_delay;
760 if( i_ts_bound != INT64_MAX )
761 i_ts_bound += i_es_delay;
762 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) ) {
763 const char *psz_name = module_get_name( p_dec->p_module, false );
764 if( pi_ts1 != NULL )
765 msg_Err(p_dec, "Could not convert timestamps %"PRId64
766 ", %"PRId64" for %s", *pi_ts0, *pi_ts1, psz_name );
767 else
768 msg_Err(p_dec, "Could not convert timestamp %"PRId64" for %s", *pi_ts0, psz_name );
769 *pi_ts0 = VLC_TS_INVALID;
772 else
774 i_rate = input_clock_GetRate( p_clock );
777 /* Do not create ephemere data because of rounding errors */
778 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
779 *pi_ts1 += 1;
781 if( pi_duration )
782 *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
783 / INPUT_RATE_DEFAULT;
785 if( pi_rate )
786 *pi_rate = i_rate;
789 #ifdef ENABLE_SOUT
790 static int DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
792 struct decoder_owner *p_owner = dec_get_owner( p_dec );
794 assert( p_owner->p_clock );
795 assert( !p_sout_block->p_next );
797 vlc_mutex_lock( &p_owner->lock );
799 if( p_owner->b_waiting )
801 p_owner->b_has_data = true;
802 vlc_cond_signal( &p_owner->wait_acknowledge );
805 DecoderWaitUnblock( p_dec );
806 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
807 &p_sout_block->i_length, NULL, INT64_MAX );
809 vlc_mutex_unlock( &p_owner->lock );
811 /* FIXME --VLC_TS_INVALID inspect stream_output*/
812 return sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
815 /* This function process a block for sout
817 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
819 struct decoder_owner *p_owner = dec_get_owner( p_dec );
820 block_t *p_sout_block;
821 block_t **pp_block = p_block ? &p_block : NULL;
823 while( ( p_sout_block =
824 p_dec->pf_packetize( p_dec, pp_block ) ) )
826 if( p_owner->p_sout_input == NULL )
828 vlc_mutex_lock( &p_owner->lock );
829 DecoderUpdateFormatLocked( p_dec );
831 p_owner->fmt.i_group = p_dec->fmt_in.i_group;
832 p_owner->fmt.i_id = p_dec->fmt_in.i_id;
833 if( p_dec->fmt_in.psz_language )
835 free( p_owner->fmt.psz_language );
836 p_owner->fmt.psz_language =
837 strdup( p_dec->fmt_in.psz_language );
839 vlc_mutex_unlock( &p_owner->lock );
841 p_owner->p_sout_input =
842 sout_InputNew( p_owner->p_sout, &p_owner->fmt );
844 if( p_owner->p_sout_input == NULL )
846 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
847 (char *)&p_owner->fmt.i_codec );
848 p_owner->error = true;
850 if(p_block)
851 block_Release(p_block);
853 block_ChainRelease(p_sout_block);
854 break;
858 while( p_sout_block )
860 block_t *p_next = p_sout_block->p_next;
862 p_sout_block->p_next = NULL;
864 if( DecoderPlaySout( p_dec, p_sout_block ) == VLC_EGENERIC )
866 msg_Err( p_dec, "cannot continue streaming due to errors with codec %4.4s",
867 (char *)&p_owner->fmt.i_codec );
869 p_owner->error = true;
871 /* Cleanup */
873 if( p_block )
874 block_Release( p_block );
876 block_ChainRelease( p_next );
877 return;
880 p_sout_block = p_next;
884 #endif
886 static void DecoderPlayCc( decoder_t *p_dec, block_t *p_cc,
887 const decoder_cc_desc_t *p_desc )
889 struct decoder_owner *p_owner = dec_get_owner( p_dec );
891 vlc_mutex_lock( &p_owner->lock );
893 p_owner->cc.desc = *p_desc;
895 /* Fanout data to all decoders. We do not know if es_out
896 selected 608 or 708. */
897 uint64_t i_bitmap = p_owner->cc.desc.i_608_channels |
898 p_owner->cc.desc.i_708_channels;
900 for( int i=0; i_bitmap > 0; i_bitmap >>= 1, i++ )
902 decoder_t *p_ccdec = p_owner->cc.pp_decoder[i];
903 struct decoder_owner *p_ccowner = dec_get_owner( p_ccdec );
904 if( !p_ccdec )
905 continue;
907 if( i_bitmap > 1 )
909 block_FifoPut( p_ccowner->p_fifo, block_Duplicate(p_cc) );
911 else
913 block_FifoPut( p_ccowner->p_fifo, p_cc );
914 p_cc = NULL; /* was last dec */
918 vlc_mutex_unlock( &p_owner->lock );
920 if( p_cc ) /* can have bitmap set but no created decs */
921 block_Release( p_cc );
924 static void PacketizerGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
926 struct decoder_owner *p_owner = dec_get_owner( p_dec );
927 block_t *p_cc;
928 decoder_cc_desc_t desc;
930 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
931 if( !p_owner->cc.b_supported )
932 return;
934 assert( p_dec_cc->pf_get_cc != NULL );
936 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, &desc );
937 if( !p_cc )
938 return;
939 DecoderPlayCc( p_dec, p_cc, &desc );
942 static void DecoderQueueCc( decoder_t *p_videodec, block_t *p_cc,
943 const decoder_cc_desc_t *p_desc )
945 struct decoder_owner *p_owner = dec_get_owner( p_videodec );
947 if( unlikely( p_cc != NULL ) )
949 if( p_owner->cc.b_supported &&
950 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
951 DecoderPlayCc( p_videodec, p_cc, p_desc );
952 else
953 block_Release( p_cc );
957 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
958 unsigned *restrict pi_lost_sum )
960 struct decoder_owner *p_owner = dec_get_owner( p_dec );
961 vout_thread_t *p_vout = p_owner->p_vout;
962 bool prerolled;
964 vlc_mutex_lock( &p_owner->lock );
965 if( p_owner->i_preroll_end > p_picture->date )
967 vlc_mutex_unlock( &p_owner->lock );
968 picture_Release( p_picture );
969 return;
972 prerolled = p_owner->i_preroll_end > (mtime_t)INT64_MIN;
973 p_owner->i_preroll_end = (mtime_t)INT64_MIN;
974 vlc_mutex_unlock( &p_owner->lock );
976 if( unlikely(prerolled) )
978 msg_Dbg( p_dec, "end of video preroll" );
980 if( p_vout )
981 vout_Flush( p_vout, VLC_TS_OLDEST );
984 if( p_picture->date == VLC_TS_INVALID )
986 msg_Warn( p_dec, "non-dated video buffer received" );
987 goto discard;
990 /* */
991 vlc_mutex_lock( &p_owner->lock );
993 if( p_owner->b_waiting && !p_owner->b_first )
995 p_owner->b_has_data = true;
996 vlc_cond_signal( &p_owner->wait_acknowledge );
998 bool b_first_after_wait = p_owner->b_waiting && p_owner->b_has_data;
1000 DecoderWaitUnblock( p_dec );
1002 if( p_owner->b_waiting )
1004 assert( p_owner->b_first );
1005 msg_Dbg( p_dec, "Received first picture" );
1006 p_owner->b_first = false;
1007 p_picture->b_force = true;
1010 const bool b_dated = p_picture->date != VLC_TS_INVALID;
1011 int i_rate = INPUT_RATE_DEFAULT;
1012 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1013 &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1015 vlc_mutex_unlock( &p_owner->lock );
1017 /* FIXME: The *input* FIFO should not be locked here. This will not work
1018 * properly if/when pictures are queued asynchronously. */
1019 vlc_fifo_Lock( p_owner->p_fifo );
1020 if( unlikely(p_owner->paused) && likely(p_owner->frames_countdown > 0) )
1021 p_owner->frames_countdown--;
1022 vlc_fifo_Unlock( p_owner->p_fifo );
1024 /* */
1025 if( p_vout == NULL )
1026 goto discard;
1028 if( p_picture->b_force || p_picture->date != VLC_TS_INVALID )
1029 /* FIXME: VLC_TS_INVALID -- verify video_output */
1031 if( i_rate != p_owner->i_last_rate || b_first_after_wait )
1033 /* Be sure to not display old picture after our own */
1034 vout_Flush( p_vout, p_picture->date );
1035 p_owner->i_last_rate = i_rate;
1037 vout_PutPicture( p_vout, p_picture );
1039 else
1041 if( b_dated )
1042 msg_Warn( p_dec, "early picture skipped" );
1043 else
1044 msg_Warn( p_dec, "non-dated video buffer received" );
1045 goto discard;
1048 return;
1049 discard:
1050 *pi_lost_sum += 1;
1051 picture_Release( p_picture );
1054 static void DecoderUpdateStatVideo( struct decoder_owner *p_owner,
1055 unsigned decoded, unsigned lost )
1057 input_thread_t *p_input = p_owner->p_input;
1058 unsigned displayed = 0;
1060 /* Update ugly stat */
1061 if( p_input == NULL )
1062 return;
1064 if( p_owner->p_vout != NULL )
1066 unsigned vout_lost = 0;
1068 vout_GetResetStatistic( p_owner->p_vout, &displayed, &vout_lost );
1069 lost += vout_lost;
1072 struct input_stats *stats = input_priv(p_input)->stats;
1074 if( stats != NULL )
1076 atomic_fetch_add_explicit(&stats->decoded_video, decoded,
1077 memory_order_relaxed);
1078 atomic_fetch_add_explicit(&stats->lost_pictures, lost,
1079 memory_order_relaxed);
1080 atomic_fetch_add_explicit(&stats->displayed_pictures, displayed,
1081 memory_order_relaxed);
1085 static void DecoderQueueVideo( decoder_t *p_dec, picture_t *p_pic )
1087 assert( p_pic );
1088 unsigned i_lost = 0;
1089 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1091 DecoderPlayVideo( p_dec, p_pic, &i_lost );
1093 p_owner->pf_update_stat( p_owner, 1, i_lost );
1096 static void DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
1097 unsigned *restrict pi_lost_sum )
1099 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1100 bool prerolled;
1102 assert( p_audio != NULL );
1104 vlc_mutex_lock( &p_owner->lock );
1105 if( p_owner->i_preroll_end > p_audio->i_pts )
1107 vlc_mutex_unlock( &p_owner->lock );
1108 block_Release( p_audio );
1109 return;
1112 prerolled = p_owner->i_preroll_end > (mtime_t)INT64_MIN;
1113 p_owner->i_preroll_end = (mtime_t)INT64_MIN;
1114 vlc_mutex_unlock( &p_owner->lock );
1116 if( unlikely(prerolled) )
1118 msg_Dbg( p_dec, "end of audio preroll" );
1120 if( p_owner->p_aout )
1121 aout_DecFlush( p_owner->p_aout, false );
1124 /* */
1125 if( p_audio->i_pts == VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1127 msg_Warn( p_dec, "non-dated audio buffer received" );
1128 *pi_lost_sum += 1;
1129 block_Release( p_audio );
1130 return;
1133 /* */
1134 vlc_mutex_lock( &p_owner->lock );
1135 if( p_owner->b_waiting )
1137 p_owner->b_has_data = true;
1138 vlc_cond_signal( &p_owner->wait_acknowledge );
1141 /* */
1142 int i_rate = INPUT_RATE_DEFAULT;
1144 DecoderWaitUnblock( p_dec );
1145 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1146 &i_rate, AOUT_MAX_ADVANCE_TIME );
1147 vlc_mutex_unlock( &p_owner->lock );
1149 audio_output_t *p_aout = p_owner->p_aout;
1151 if( p_aout != NULL && p_audio->i_pts != VLC_TS_INVALID
1152 && i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1153 && i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE
1154 && !DecoderTimedWait( p_dec, p_audio->i_pts - AOUT_MAX_PREPARE_TIME ) )
1156 int status = aout_DecPlay( p_aout, p_audio );
1157 if( status == AOUT_DEC_CHANGED )
1159 /* Only reload the decoder */
1160 RequestReload( p_dec );
1162 else if( status == AOUT_DEC_FAILED )
1164 /* If we reload because the aout failed, we should release it. That
1165 * way, a next call to aout_update_format() won't re-use the
1166 * previous (failing) aout but will try to create a new one. */
1167 atomic_store( &p_owner->reload, RELOAD_DECODER_AOUT );
1170 else
1172 msg_Dbg( p_dec, "discarded audio buffer" );
1173 *pi_lost_sum += 1;
1174 block_Release( p_audio );
1176 return;
1179 static void DecoderUpdateStatAudio( struct decoder_owner *p_owner,
1180 unsigned decoded, unsigned lost )
1182 input_thread_t *p_input = p_owner->p_input;
1183 unsigned played = 0;
1185 /* Update ugly stat */
1186 if( p_input == NULL )
1187 return;
1189 if( p_owner->p_aout != NULL )
1191 unsigned aout_lost;
1193 aout_DecGetResetStats( p_owner->p_aout, &aout_lost, &played );
1194 lost += aout_lost;
1197 struct input_stats *stats = input_priv(p_input)->stats;
1199 if( stats != NULL )
1201 atomic_fetch_add_explicit(&stats->lost_abuffers, lost,
1202 memory_order_relaxed);
1203 atomic_fetch_add_explicit(&stats->played_abuffers, played,
1204 memory_order_relaxed);
1205 atomic_fetch_add_explicit(&stats->decoded_audio, decoded,
1206 memory_order_relaxed);
1210 static void DecoderQueueAudio( decoder_t *p_dec, block_t *p_aout_buf )
1212 unsigned lost = 0;
1213 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1215 DecoderPlayAudio( p_dec, p_aout_buf, &lost );
1217 p_owner->pf_update_stat( p_owner, 1, lost );
1220 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1222 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1223 vout_thread_t *p_vout = p_owner->p_spu_vout;
1225 /* */
1226 if( p_subpic->i_start == VLC_TS_INVALID )
1228 msg_Warn( p_dec, "non-dated spu buffer received" );
1229 subpicture_Delete( p_subpic );
1230 return;
1233 /* */
1234 vlc_mutex_lock( &p_owner->lock );
1236 if( p_owner->b_waiting )
1238 p_owner->b_has_data = true;
1239 vlc_cond_signal( &p_owner->wait_acknowledge );
1242 DecoderWaitUnblock( p_dec );
1243 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1244 NULL, INT64_MAX );
1245 vlc_mutex_unlock( &p_owner->lock );
1247 if( p_subpic->i_start == VLC_TS_INVALID
1248 || DecoderTimedWait( p_dec, p_subpic->i_start - SPU_MAX_PREPARE_TIME ) )
1250 subpicture_Delete( p_subpic );
1251 return;
1254 vout_PutSubpicture( p_vout, p_subpic );
1257 static void DecoderUpdateStatSpu( struct decoder_owner *p_owner,
1258 unsigned decoded, unsigned lost )
1260 (void) p_owner; (void) decoded; (void) lost;
1263 static void DecoderQueueSpu( decoder_t *p_dec, subpicture_t *p_spu )
1265 assert( p_spu );
1266 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1268 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1269 if( p_vout && p_owner->p_spu_vout == p_vout )
1271 /* Preroll does not work very well with subtitle */
1272 vlc_mutex_lock( &p_owner->lock );
1273 if( p_spu->i_start != VLC_TS_INVALID &&
1274 p_spu->i_start < p_owner->i_preroll_end &&
1275 ( p_spu->i_stop == VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1277 vlc_mutex_unlock( &p_owner->lock );
1278 subpicture_Delete( p_spu );
1280 else
1282 vlc_mutex_unlock( &p_owner->lock );
1283 DecoderPlaySpu( p_dec, p_spu );
1286 else
1288 subpicture_Delete( p_spu );
1290 if( p_vout )
1291 vlc_object_release( p_vout );
1294 static void DecoderProcess( decoder_t *p_dec, block_t *p_block );
1295 static void DecoderDecode( decoder_t *p_dec, block_t *p_block )
1297 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1299 int ret = p_dec->pf_decode( p_dec, p_block );
1300 switch( ret )
1302 case VLCDEC_SUCCESS:
1303 p_owner->pf_update_stat( p_owner, 1, 0 );
1304 break;
1305 case VLCDEC_ECRITICAL:
1306 p_owner->error = true;
1307 break;
1308 case VLCDEC_RELOAD:
1309 RequestReload( p_dec );
1310 if( unlikely( p_block == NULL ) )
1311 break;
1312 if( !( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1314 p_block->i_flags |= BLOCK_FLAG_CORE_PRIVATE_RELOADED;
1315 DecoderProcess( p_dec, p_block );
1317 else /* We prefer loosing this block than an infinite recursion */
1318 block_Release( p_block );
1319 break;
1320 default:
1321 vlc_assert_unreachable();
1326 * Decode a block
1328 * \param p_dec the decoder object
1329 * \param p_block the block to decode
1331 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1333 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1335 if( p_owner->error )
1336 goto error;
1338 /* Here, the atomic doesn't prevent to miss a reload request.
1339 * DecoderProcess() can still be called after the decoder module or the
1340 * audio output requested a reload. This will only result in a drop of an
1341 * input block or an output buffer. */
1342 enum reload reload;
1343 if( ( reload = atomic_exchange( &p_owner->reload, RELOAD_NO_REQUEST ) ) )
1345 msg_Warn( p_dec, "Reloading the decoder module%s",
1346 reload == RELOAD_DECODER_AOUT ? " and the audio output" : "" );
1348 if( ReloadDecoder( p_dec, false, &p_dec->fmt_in, reload ) != VLC_SUCCESS )
1349 goto error;
1352 bool packetize = p_owner->p_packetizer != NULL;
1353 if( p_block )
1355 if( p_block->i_buffer <= 0 )
1356 goto error;
1358 vlc_mutex_lock( &p_owner->lock );
1359 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1360 vlc_mutex_unlock( &p_owner->lock );
1361 if( unlikely( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1363 /* This block has already been packetized */
1364 packetize = false;
1368 #ifdef ENABLE_SOUT
1369 if( p_owner->p_sout != NULL )
1371 DecoderProcessSout( p_dec, p_block );
1372 return;
1374 #endif
1375 if( packetize )
1377 block_t *p_packetized_block;
1378 block_t **pp_block = p_block ? &p_block : NULL;
1379 decoder_t *p_packetizer = p_owner->p_packetizer;
1381 while( (p_packetized_block =
1382 p_packetizer->pf_packetize( p_packetizer, pp_block ) ) )
1384 if( !es_format_IsSimilar( &p_dec->fmt_in, &p_packetizer->fmt_out ) )
1386 msg_Dbg( p_dec, "restarting module due to input format change");
1388 /* Drain the decoder module */
1389 DecoderDecode( p_dec, NULL );
1391 if( ReloadDecoder( p_dec, false, &p_packetizer->fmt_out,
1392 RELOAD_DECODER ) != VLC_SUCCESS )
1394 block_ChainRelease( p_packetized_block );
1395 return;
1399 if( p_packetizer->pf_get_cc )
1400 PacketizerGetCc( p_dec, p_packetizer );
1402 while( p_packetized_block )
1404 block_t *p_next = p_packetized_block->p_next;
1405 p_packetized_block->p_next = NULL;
1407 DecoderDecode( p_dec, p_packetized_block );
1408 if( p_owner->error )
1410 block_ChainRelease( p_next );
1411 return;
1414 p_packetized_block = p_next;
1417 /* Drain the decoder after the packetizer is drained */
1418 if( !pp_block )
1419 DecoderDecode( p_dec, NULL );
1421 else
1422 DecoderDecode( p_dec, p_block );
1423 return;
1425 error:
1426 if( p_block )
1427 block_Release( p_block );
1430 static void DecoderProcessFlush( decoder_t *p_dec )
1432 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1433 decoder_t *p_packetizer = p_owner->p_packetizer;
1435 if( p_owner->error )
1436 return;
1438 if( p_packetizer != NULL && p_packetizer->pf_flush != NULL )
1439 p_packetizer->pf_flush( p_packetizer );
1441 if ( p_dec->pf_flush != NULL )
1442 p_dec->pf_flush( p_dec );
1444 /* flush CC sub decoders */
1445 if( p_owner->cc.b_supported )
1447 for( int i=0; i<MAX_CC_DECODERS; i++ )
1449 decoder_t *p_subdec = p_owner->cc.pp_decoder[i];
1450 if( p_subdec && p_subdec->pf_flush )
1451 p_subdec->pf_flush( p_subdec );
1455 #ifdef ENABLE_SOUT
1456 if ( p_owner->p_sout_input != NULL )
1458 sout_InputFlush( p_owner->p_sout_input );
1460 #endif
1461 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1463 if( p_owner->p_aout )
1464 aout_DecFlush( p_owner->p_aout, false );
1466 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1468 if( p_owner->p_vout )
1469 vout_Flush( p_owner->p_vout, VLC_TS_OLDEST );
1471 else if( p_dec->fmt_out.i_cat == SPU_ES )
1473 if( p_owner->p_spu_vout )
1475 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1477 if( p_vout && p_owner->p_spu_vout == p_vout )
1478 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1480 if( p_vout )
1481 vlc_object_release( p_vout );
1485 vlc_mutex_lock( &p_owner->lock );
1486 p_owner->i_preroll_end = (mtime_t)INT64_MIN;
1487 vlc_mutex_unlock( &p_owner->lock );
1491 * The decoding main loop
1493 * \param p_dec the decoder
1495 static void *DecoderThread( void *p_data )
1497 decoder_t *p_dec = (decoder_t *)p_data;
1498 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1499 float rate = 1.f;
1500 bool paused = false;
1502 /* The decoder's main loop */
1503 vlc_fifo_Lock( p_owner->p_fifo );
1504 vlc_fifo_CleanupPush( p_owner->p_fifo );
1506 for( ;; )
1508 if( p_owner->flushing )
1509 { /* Flush before/regardless of pause. We do not want to resume just
1510 * for the sake of flushing (glitches could otherwise happen). */
1511 int canc = vlc_savecancel();
1513 vlc_fifo_Unlock( p_owner->p_fifo );
1515 /* Flush the decoder (and the output) */
1516 DecoderProcessFlush( p_dec );
1518 vlc_fifo_Lock( p_owner->p_fifo );
1519 vlc_restorecancel( canc );
1521 /* Reset flushing after DecoderProcess in case input_DecoderFlush
1522 * is called again. This will avoid a second useless flush (but
1523 * harmless). */
1524 p_owner->flushing = false;
1526 continue;
1529 if( paused != p_owner->paused )
1530 { /* Update playing/paused status of the output */
1531 int canc = vlc_savecancel();
1532 mtime_t date = p_owner->pause_date;
1534 paused = p_owner->paused;
1535 vlc_fifo_Unlock( p_owner->p_fifo );
1537 /* NOTE: Only the audio and video outputs care about pause. */
1538 msg_Dbg( p_dec, "toggling %s", paused ? "resume" : "pause" );
1539 switch( p_dec->fmt_out.i_cat )
1541 case VIDEO_ES:
1542 if( p_owner->p_vout != NULL )
1543 vout_ChangePause( p_owner->p_vout, paused, date );
1544 break;
1545 case AUDIO_ES:
1546 if( p_owner->p_aout != NULL )
1547 aout_DecChangePause( p_owner->p_aout, paused, date );
1548 break;
1549 case SPU_ES:
1550 break;
1551 default:
1552 vlc_assert_unreachable();
1555 vlc_restorecancel( canc );
1556 vlc_fifo_Lock( p_owner->p_fifo );
1557 continue;
1560 if( rate != p_owner->rate )
1562 int canc = vlc_savecancel();
1564 rate = p_owner->rate;
1565 vlc_fifo_Unlock( p_owner->p_fifo );
1567 msg_Dbg( p_dec, "changing rate: %f", rate );
1568 switch( p_dec->fmt_out.i_cat )
1570 case VIDEO_ES:
1571 break;
1572 case AUDIO_ES:
1573 if( p_owner->p_aout != NULL )
1574 aout_DecChangeRate( p_owner->p_aout, rate );
1575 break;
1576 case SPU_ES:
1577 break;
1578 default:
1579 vlc_assert_unreachable();
1582 vlc_restorecancel( canc );
1583 vlc_fifo_Lock( p_owner->p_fifo );
1586 if( p_owner->paused && p_owner->frames_countdown == 0 )
1587 { /* Wait for resumption from pause */
1588 p_owner->b_idle = true;
1589 vlc_cond_signal( &p_owner->wait_acknowledge );
1590 vlc_fifo_Wait( p_owner->p_fifo );
1591 p_owner->b_idle = false;
1592 continue;
1595 vlc_cond_signal( &p_owner->wait_fifo );
1596 vlc_testcancel(); /* forced expedited cancellation in case of stop */
1598 block_t *p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
1599 if( p_block == NULL )
1601 if( likely(!p_owner->b_draining) )
1602 { /* Wait for a block to decode (or a request to drain) */
1603 p_owner->b_idle = true;
1604 vlc_cond_signal( &p_owner->wait_acknowledge );
1605 vlc_fifo_Wait( p_owner->p_fifo );
1606 p_owner->b_idle = false;
1607 continue;
1609 /* We have emptied the FIFO and there is a pending request to
1610 * drain. Pass p_block = NULL to decoder just once. */
1613 vlc_fifo_Unlock( p_owner->p_fifo );
1615 int canc = vlc_savecancel();
1616 DecoderProcess( p_dec, p_block );
1618 if( p_block == NULL && p_dec->fmt_out.i_cat == AUDIO_ES )
1619 { /* Draining: the decoder is drained and all decoded buffers are
1620 * queued to the output at this point. Now drain the output. */
1621 if( p_owner->p_aout != NULL )
1622 aout_DecFlush( p_owner->p_aout, true );
1624 vlc_restorecancel( canc );
1626 /* TODO? Wait for draining instead of polling. */
1627 vlc_mutex_lock( &p_owner->lock );
1628 if( p_owner->b_draining && (p_block == NULL) )
1630 p_owner->b_draining = false;
1631 p_owner->drained = true;
1633 vlc_fifo_Lock( p_owner->p_fifo );
1634 vlc_cond_signal( &p_owner->wait_acknowledge );
1635 vlc_mutex_unlock( &p_owner->lock );
1637 vlc_cleanup_pop();
1638 vlc_assert_unreachable();
1641 static const struct decoder_owner_callbacks dec_video_cbs =
1643 .video = {
1644 vout_update_format,
1645 vout_new_buffer,
1646 DecoderQueueVideo,
1647 DecoderQueueCc,
1648 DecoderGetDisplayDate,
1649 DecoderGetDisplayRate
1651 DecoderGetInputAttachments,
1653 static const struct decoder_owner_callbacks dec_audio_cbs =
1655 .audio = {
1656 aout_update_format,
1657 DecoderQueueAudio,
1659 DecoderGetInputAttachments,
1661 static const struct decoder_owner_callbacks dec_spu_cbs =
1663 .spu = {
1664 spu_new_buffer,
1665 DecoderQueueSpu
1667 DecoderGetInputAttachments,
1671 * Create a decoder object
1673 * \param p_input the input thread
1674 * \param p_es the es descriptor
1675 * \param b_packetizer instead of a decoder
1676 * \return the decoder object
1678 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
1679 input_thread_t *p_input,
1680 const es_format_t *fmt,
1681 input_resource_t *p_resource,
1682 sout_instance_t *p_sout )
1684 decoder_t *p_dec;
1685 struct decoder_owner *p_owner;
1687 p_owner = vlc_custom_create( p_parent, sizeof( *p_owner ), "decoder" );
1688 if( p_owner == NULL )
1689 return NULL;
1690 p_dec = &p_owner->dec;
1692 p_owner->i_preroll_end = (mtime_t)INT64_MIN;
1693 p_owner->i_last_rate = INPUT_RATE_DEFAULT;
1694 p_owner->p_input = p_input;
1695 p_owner->p_resource = p_resource;
1696 p_owner->p_aout = NULL;
1697 p_owner->p_vout = NULL;
1698 p_owner->p_spu_vout = NULL;
1699 p_owner->i_spu_channel = 0;
1700 p_owner->i_spu_order = 0;
1701 p_owner->p_sout = p_sout;
1702 p_owner->p_sout_input = NULL;
1703 p_owner->p_packetizer = NULL;
1705 p_owner->b_fmt_description = false;
1706 p_owner->p_description = NULL;
1708 p_owner->rate = 1.f;
1709 p_owner->paused = false;
1710 p_owner->pause_date = VLC_TS_INVALID;
1711 p_owner->frames_countdown = 0;
1713 p_owner->b_waiting = false;
1714 p_owner->b_first = true;
1715 p_owner->b_has_data = false;
1717 p_owner->error = false;
1719 p_owner->flushing = false;
1720 p_owner->b_draining = false;
1721 p_owner->drained = false;
1722 atomic_init( &p_owner->reload, RELOAD_NO_REQUEST );
1723 p_owner->b_idle = false;
1725 es_format_Init( &p_owner->fmt, fmt->i_cat, 0 );
1727 /* decoder fifo */
1728 p_owner->p_fifo = block_FifoNew();
1729 if( unlikely(p_owner->p_fifo == NULL) )
1731 vlc_object_release( p_dec );
1732 return NULL;
1735 vlc_mutex_init( &p_owner->lock );
1736 vlc_cond_init( &p_owner->wait_request );
1737 vlc_cond_init( &p_owner->wait_acknowledge );
1738 vlc_cond_init( &p_owner->wait_fifo );
1739 vlc_cond_init( &p_owner->wait_timed );
1741 /* Load a packetizer module if the input is not already packetized */
1742 if( p_sout == NULL && !fmt->b_packetized )
1744 p_owner->p_packetizer =
1745 vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
1746 if( p_owner->p_packetizer )
1748 if( LoadDecoder( p_owner->p_packetizer, true, fmt ) )
1750 vlc_object_release( p_owner->p_packetizer );
1751 p_owner->p_packetizer = NULL;
1753 else
1755 p_owner->p_packetizer->fmt_out.b_packetized = true;
1756 fmt = &p_owner->p_packetizer->fmt_out;
1761 switch( fmt->i_cat )
1763 case VIDEO_ES:
1764 p_dec->cbs = &dec_video_cbs;
1765 p_owner->pf_update_stat = DecoderUpdateStatVideo;
1766 break;
1767 case AUDIO_ES:
1768 p_dec->cbs = &dec_audio_cbs;
1769 p_owner->pf_update_stat = DecoderUpdateStatAudio;
1770 break;
1771 case SPU_ES:
1772 p_dec->cbs = &dec_spu_cbs;
1773 p_owner->pf_update_stat = DecoderUpdateStatSpu;
1774 break;
1775 default:
1776 msg_Err( p_dec, "unknown ES format" );
1777 return p_dec;
1780 /* Find a suitable decoder/packetizer module */
1781 if( LoadDecoder( p_dec, p_sout != NULL, fmt ) )
1782 return p_dec;
1784 assert( p_dec->fmt_in.i_cat == p_dec->fmt_out.i_cat && fmt->i_cat == p_dec->fmt_in.i_cat);
1786 /* Copy ourself the input replay gain */
1787 if( fmt->i_cat == AUDIO_ES )
1789 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1791 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
1793 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
1794 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
1796 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
1798 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
1799 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
1804 /* */
1805 p_owner->cc.b_supported = ( p_sout == NULL );
1807 p_owner->cc.desc.i_608_channels = 0;
1808 p_owner->cc.desc.i_708_channels = 0;
1809 for( unsigned i = 0; i < MAX_CC_DECODERS; i++ )
1810 p_owner->cc.pp_decoder[i] = NULL;
1811 p_owner->i_ts_delay = 0;
1812 return p_dec;
1816 * Destroys a decoder object
1818 * \param p_dec the decoder object
1819 * \return nothing
1821 static void DeleteDecoder( decoder_t * p_dec )
1823 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1825 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s'",
1826 (char*)&p_dec->fmt_in.i_codec );
1828 const enum es_format_category_e i_cat =p_dec->fmt_out.i_cat;
1829 UnloadDecoder( p_dec );
1831 /* Free all packets still in the decoder fifo. */
1832 block_FifoRelease( p_owner->p_fifo );
1834 /* Cleanup */
1835 #ifdef ENABLE_SOUT
1836 if( p_owner->p_sout_input )
1838 sout_InputDelete( p_owner->p_sout_input );
1840 #endif
1842 switch( i_cat )
1844 case AUDIO_ES:
1845 if( p_owner->p_aout )
1847 /* TODO: REVISIT gap-less audio */
1848 aout_DecFlush( p_owner->p_aout, false );
1849 aout_DecDelete( p_owner->p_aout );
1850 input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1851 if( p_owner->p_input != NULL )
1852 input_SendEventAout( p_owner->p_input );
1854 break;
1855 case VIDEO_ES:
1856 if( p_owner->p_vout )
1858 /* Reset the cancel state that was set before joining the decoder
1859 * thread */
1860 vout_Cancel( p_owner->p_vout, false );
1862 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1863 0, true );
1864 if( p_owner->p_input != NULL )
1865 input_SendEventVout( p_owner->p_input );
1867 break;
1868 case SPU_ES:
1870 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1871 if( p_vout )
1873 if( p_owner->p_spu_vout == p_vout )
1874 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1875 vlc_object_release( p_vout );
1877 break;
1879 default:
1880 vlc_assert_unreachable();
1883 es_format_Clean( &p_owner->fmt );
1885 if( p_owner->p_description )
1886 vlc_meta_Delete( p_owner->p_description );
1888 if( p_owner->p_packetizer )
1890 UnloadDecoder( p_owner->p_packetizer );
1891 vlc_object_release( p_owner->p_packetizer );
1894 vlc_cond_destroy( &p_owner->wait_timed );
1895 vlc_cond_destroy( &p_owner->wait_fifo );
1896 vlc_cond_destroy( &p_owner->wait_acknowledge );
1897 vlc_cond_destroy( &p_owner->wait_request );
1898 vlc_mutex_destroy( &p_owner->lock );
1900 vlc_object_release( p_dec );
1903 /* */
1904 static void DecoderUnsupportedCodec( decoder_t *p_dec, const es_format_t *fmt, bool b_decoding )
1906 if (fmt->i_codec != VLC_CODEC_UNKNOWN && fmt->i_codec) {
1907 const char *desc = vlc_fourcc_GetDescription(fmt->i_cat, fmt->i_codec);
1908 if (!desc || !*desc)
1909 desc = N_("No description for this codec");
1910 msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&fmt->i_codec, desc );
1911 vlc_dialog_display_error( p_dec, _("Codec not supported"),
1912 _("VLC could not decode the format \"%4.4s\" (%s)"),
1913 (char*)&fmt->i_codec, desc );
1914 } else if( b_decoding ){
1915 msg_Err( p_dec, "could not identify codec" );
1916 vlc_dialog_display_error( p_dec, _("Unidentified codec"),
1917 _("VLC could not identify the audio or video codec" ) );
1921 /* TODO: pass p_sout through p_resource? -- Courmisch */
1922 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
1923 const es_format_t *fmt, input_clock_t *p_clock,
1924 input_resource_t *p_resource,
1925 sout_instance_t *p_sout )
1927 decoder_t *p_dec = NULL;
1928 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
1929 int i_priority;
1931 /* Create the decoder configuration structure */
1932 p_dec = CreateDecoder( p_parent, p_input, fmt, p_resource, p_sout );
1933 if( p_dec == NULL )
1935 msg_Err( p_parent, "could not create %s", psz_type );
1936 vlc_dialog_display_error( p_parent, _("Streaming / Transcoding failed"),
1937 _("VLC could not open the %s module."), vlc_gettext( psz_type ) );
1938 return NULL;
1941 if( !p_dec->p_module )
1943 DecoderUnsupportedCodec( p_dec, fmt, !p_sout );
1945 DeleteDecoder( p_dec );
1946 return NULL;
1949 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1950 p_owner->p_clock = p_clock;
1951 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
1953 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1954 i_priority = VLC_THREAD_PRIORITY_AUDIO;
1955 else
1956 i_priority = VLC_THREAD_PRIORITY_VIDEO;
1958 #ifdef ENABLE_SOUT
1959 /* Do not delay sout creation for SPU or DATA. */
1960 if( p_sout && fmt->b_packetized &&
1961 (fmt->i_cat != VIDEO_ES && fmt->i_cat != AUDIO_ES) )
1963 p_owner->p_sout_input = sout_InputNew( p_owner->p_sout, fmt );
1964 if( p_owner->p_sout_input == NULL )
1966 msg_Err( p_dec, "cannot create sout input (%4.4s)",
1967 (char *)&fmt->i_codec );
1968 p_owner->error = true;
1971 #endif
1973 /* Spawn the decoder thread */
1974 if( vlc_clone( &p_owner->thread, DecoderThread, p_dec, i_priority ) )
1976 msg_Err( p_dec, "cannot spawn decoder thread" );
1977 DeleteDecoder( p_dec );
1978 return NULL;
1981 return p_dec;
1986 * Spawns a new decoder thread from the input thread
1988 * \param p_input the input thread
1989 * \param p_es the es descriptor
1990 * \return the spawned decoder object
1992 decoder_t *input_DecoderNew( input_thread_t *p_input,
1993 es_format_t *fmt, input_clock_t *p_clock,
1994 sout_instance_t *p_sout )
1996 return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
1997 input_priv(p_input)->p_resource, p_sout );
2001 * Spawn a decoder thread outside of the input thread.
2003 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, const es_format_t *fmt,
2004 input_resource_t *p_resource )
2006 return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
2011 * Kills a decoder thread and waits until it's finished
2013 * \param p_input the input thread
2014 * \param p_es the es descriptor
2015 * \return nothing
2017 void input_DecoderDelete( decoder_t *p_dec )
2019 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2021 vlc_cancel( p_owner->thread );
2023 vlc_fifo_Lock( p_owner->p_fifo );
2024 /* Signal DecoderTimedWait */
2025 p_owner->flushing = true;
2026 vlc_cond_signal( &p_owner->wait_timed );
2027 vlc_fifo_Unlock( p_owner->p_fifo );
2029 /* Make sure we aren't waiting/decoding anymore */
2030 vlc_mutex_lock( &p_owner->lock );
2031 p_owner->b_waiting = false;
2032 vlc_cond_signal( &p_owner->wait_request );
2034 /* If the video output is paused or slow, or if the picture pool size was
2035 * under-estimated (e.g. greedy video filter, buggy decoder...), the
2036 * the picture pool may be empty, and the decoder thread or any decoder
2037 * module worker threads may be stuck waiting for free picture buffers.
2039 * This unblocks the thread, allowing the decoder module to join all its
2040 * worker threads (if any) and the decoder thread to terminate. */
2041 if( p_dec->fmt_out.i_cat == VIDEO_ES && p_owner->p_vout != NULL )
2042 vout_Cancel( p_owner->p_vout, true );
2043 vlc_mutex_unlock( &p_owner->lock );
2045 vlc_join( p_owner->thread, NULL );
2047 /* */
2048 if( p_owner->cc.b_supported )
2050 for( int i = 0; i < MAX_CC_DECODERS; i++ )
2051 input_DecoderSetCcState( p_dec, VLC_CODEC_CEA608, i, false );
2054 /* Delete decoder */
2055 DeleteDecoder( p_dec );
2059 * Put a block_t in the decoder's fifo.
2060 * Thread-safe w.r.t. the decoder. May be a cancellation point.
2062 * \param p_dec the decoder object
2063 * \param p_block the data block
2065 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
2067 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2069 vlc_fifo_Lock( p_owner->p_fifo );
2070 if( !b_do_pace )
2072 /* FIXME: ideally we would check the time amount of data
2073 * in the FIFO instead of its size. */
2074 /* 400 MiB, i.e. ~ 50mb/s for 60s */
2075 if( vlc_fifo_GetBytes( p_owner->p_fifo ) > 400*1024*1024 )
2077 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
2078 "consumed quickly enough), resetting fifo!" );
2079 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2080 p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
2083 else
2084 if( !p_owner->b_waiting )
2085 { /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
2086 * Locking is not necessary as b_waiting is only read, not written by
2087 * the decoder thread. */
2088 while( vlc_fifo_GetCount( p_owner->p_fifo ) >= 10 )
2089 vlc_fifo_WaitCond( p_owner->p_fifo, &p_owner->wait_fifo );
2092 vlc_fifo_QueueUnlocked( p_owner->p_fifo, p_block );
2093 vlc_fifo_Unlock( p_owner->p_fifo );
2096 bool input_DecoderIsEmpty( decoder_t * p_dec )
2098 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2100 assert( !p_owner->b_waiting );
2102 vlc_fifo_Lock( p_owner->p_fifo );
2103 if( !vlc_fifo_IsEmpty( p_owner->p_fifo ) || p_owner->b_draining )
2105 vlc_fifo_Unlock( p_owner->p_fifo );
2106 return false;
2108 vlc_fifo_Unlock( p_owner->p_fifo );
2110 bool b_empty;
2112 vlc_mutex_lock( &p_owner->lock );
2113 #ifdef ENABLE_SOUT
2114 if( p_owner->p_sout_input != NULL )
2115 b_empty = sout_InputIsEmpty( p_owner->p_sout_input );
2116 else
2117 #endif
2118 if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->p_vout != NULL )
2119 b_empty = vout_IsEmpty( p_owner->p_vout );
2120 else if( p_owner->fmt.i_cat == AUDIO_ES )
2121 b_empty = !p_owner->b_draining || p_owner->drained;
2122 else
2123 b_empty = true; /* TODO subtitles support */
2124 vlc_mutex_unlock( &p_owner->lock );
2126 return b_empty;
2130 * Signals that there are no further blocks to decode, and requests that the
2131 * decoder drain all pending buffers. This is used to ensure that all
2132 * intermediate buffers empty and no samples get lost at the end of the stream.
2134 * @note The function does not actually wait for draining. It just signals that
2135 * draining should be performed once the decoder has emptied FIFO.
2137 void input_DecoderDrain( decoder_t *p_dec )
2139 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2141 vlc_fifo_Lock( p_owner->p_fifo );
2142 p_owner->b_draining = true;
2143 vlc_fifo_Signal( p_owner->p_fifo );
2144 vlc_fifo_Unlock( p_owner->p_fifo );
2148 * Requests that the decoder immediately discard all pending buffers.
2149 * This is useful when seeking or when deselecting a stream.
2151 void input_DecoderFlush( decoder_t *p_dec )
2153 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2155 vlc_fifo_Lock( p_owner->p_fifo );
2157 /* Empty the fifo */
2158 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2160 /* Don't need to wait for the DecoderThread to flush. Indeed, if called a
2161 * second time, this function will clear the FIFO again before anything was
2162 * dequeued by DecoderThread and there is no need to flush a second time in
2163 * a row. */
2164 p_owner->flushing = true;
2166 /* Flush video/spu decoder when paused: increment frames_countdown in order
2167 * to display one frame/subtitle */
2168 if( p_owner->paused
2169 && ( p_owner->fmt.i_cat == VIDEO_ES || p_owner->fmt.i_cat == SPU_ES )
2170 && p_owner->frames_countdown == 0 )
2171 p_owner->frames_countdown++;
2173 vlc_fifo_Signal( p_owner->p_fifo );
2174 vlc_cond_signal( &p_owner->wait_timed );
2176 vlc_fifo_Unlock( p_owner->p_fifo );
2179 void input_DecoderGetCcDesc( decoder_t *p_dec, decoder_cc_desc_t *p_desc )
2181 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2183 vlc_mutex_lock( &p_owner->lock );
2184 *p_desc = p_owner->cc.desc;
2185 vlc_mutex_unlock( &p_owner->lock );
2188 static bool input_DecoderHasCCChanFlag( decoder_t *p_dec,
2189 vlc_fourcc_t codec, int i_channel )
2191 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2193 int i_max_channels;
2194 uint64_t i_bitmap;
2195 if( codec == VLC_CODEC_CEA608 )
2197 i_max_channels = 4;
2198 i_bitmap = p_owner->cc.desc.i_608_channels;
2200 else if( codec == VLC_CODEC_CEA708 )
2202 i_max_channels = 64;
2203 i_bitmap = p_owner->cc.desc.i_708_channels;
2205 else return false;
2207 return ( i_channel >= 0 && i_channel < i_max_channels &&
2208 ( i_bitmap & ((uint64_t)1 << i_channel) ) );
2211 int input_DecoderSetCcState( decoder_t *p_dec, vlc_fourcc_t codec,
2212 int i_channel, bool b_decode )
2214 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2216 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%x", b_decode, i_channel );
2218 if( !input_DecoderHasCCChanFlag( p_dec, codec, i_channel ) )
2219 return VLC_EGENERIC;
2221 if( b_decode )
2223 decoder_t *p_cc;
2224 es_format_t fmt;
2226 es_format_Init( &fmt, SPU_ES, codec );
2227 fmt.subs.cc.i_channel = i_channel;
2228 fmt.subs.cc.i_reorder_depth = p_owner->cc.desc.i_reorder_depth;
2229 p_cc = input_DecoderNew( p_owner->p_input, &fmt,
2230 p_owner->p_clock, p_owner->p_sout );
2231 if( !p_cc )
2233 msg_Err( p_dec, "could not create decoder" );
2234 vlc_dialog_display_error( p_dec,
2235 _("Streaming / Transcoding failed"), "%s",
2236 _("VLC could not open the decoder module.") );
2237 return VLC_EGENERIC;
2239 else if( !p_cc->p_module )
2241 DecoderUnsupportedCodec( p_dec, &fmt, true );
2242 input_DecoderDelete(p_cc);
2243 return VLC_EGENERIC;
2245 struct decoder_owner *p_ccowner = dec_get_owner( p_cc );
2246 p_ccowner->p_clock = p_owner->p_clock;
2248 vlc_mutex_lock( &p_owner->lock );
2249 p_owner->cc.pp_decoder[i_channel] = p_cc;
2250 vlc_mutex_unlock( &p_owner->lock );
2252 else
2254 decoder_t *p_cc;
2256 vlc_mutex_lock( &p_owner->lock );
2257 p_cc = p_owner->cc.pp_decoder[i_channel];
2258 p_owner->cc.pp_decoder[i_channel] = NULL;
2259 vlc_mutex_unlock( &p_owner->lock );
2261 if( p_cc )
2262 input_DecoderDelete(p_cc);
2264 return VLC_SUCCESS;
2267 int input_DecoderGetCcState( decoder_t *p_dec, vlc_fourcc_t codec,
2268 int i_channel, bool *pb_decode )
2270 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2272 if( !input_DecoderHasCCChanFlag( p_dec, codec, i_channel ) )
2273 return VLC_EGENERIC;
2275 vlc_mutex_lock( &p_owner->lock );
2276 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
2277 vlc_mutex_unlock( &p_owner->lock );
2278 return VLC_SUCCESS;
2281 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
2283 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2285 /* Normally, p_owner->b_paused != b_paused here. But if a track is added
2286 * while the input is paused (e.g. add sub file), then b_paused is
2287 * (incorrectly) false. FIXME: This is a bug in the decoder owner. */
2288 vlc_fifo_Lock( p_owner->p_fifo );
2289 p_owner->paused = b_paused;
2290 p_owner->pause_date = i_date;
2291 p_owner->frames_countdown = 0;
2292 vlc_fifo_Signal( p_owner->p_fifo );
2293 vlc_fifo_Unlock( p_owner->p_fifo );
2296 void input_DecoderChangeRate( decoder_t *dec, float rate )
2298 struct decoder_owner *owner = dec_get_owner( dec );
2300 vlc_fifo_Lock( owner->p_fifo );
2301 owner->rate = rate;
2302 vlc_fifo_Signal( owner->p_fifo );
2303 vlc_fifo_Unlock( owner->p_fifo );
2306 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
2308 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2310 vlc_mutex_lock( &p_owner->lock );
2311 p_owner->i_ts_delay = i_delay;
2312 vlc_mutex_unlock( &p_owner->lock );
2315 void input_DecoderStartWait( decoder_t *p_dec )
2317 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2319 assert( !p_owner->b_waiting );
2321 vlc_mutex_lock( &p_owner->lock );
2322 p_owner->b_first = true;
2323 p_owner->b_has_data = false;
2324 p_owner->b_waiting = true;
2325 vlc_cond_signal( &p_owner->wait_request );
2326 vlc_mutex_unlock( &p_owner->lock );
2329 void input_DecoderStopWait( decoder_t *p_dec )
2331 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2333 assert( p_owner->b_waiting );
2335 vlc_mutex_lock( &p_owner->lock );
2336 p_owner->b_waiting = false;
2337 vlc_cond_signal( &p_owner->wait_request );
2338 vlc_mutex_unlock( &p_owner->lock );
2341 void input_DecoderWait( decoder_t *p_dec )
2343 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2345 assert( p_owner->b_waiting );
2347 vlc_mutex_lock( &p_owner->lock );
2348 while( !p_owner->b_has_data )
2350 /* Don't need to lock p_owner->paused since it's only modified by the
2351 * owner */
2352 if( p_owner->paused )
2353 break;
2354 vlc_fifo_Lock( p_owner->p_fifo );
2355 if( p_owner->b_idle && vlc_fifo_IsEmpty( p_owner->p_fifo ) )
2357 msg_Err( p_dec, "buffer deadlock prevented" );
2358 vlc_fifo_Unlock( p_owner->p_fifo );
2359 break;
2361 vlc_fifo_Unlock( p_owner->p_fifo );
2362 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
2364 vlc_mutex_unlock( &p_owner->lock );
2367 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
2369 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2371 assert( p_owner->paused );
2372 *pi_duration = 0;
2374 vlc_fifo_Lock( p_owner->p_fifo );
2375 p_owner->frames_countdown++;
2376 vlc_fifo_Signal( p_owner->p_fifo );
2377 vlc_fifo_Unlock( p_owner->p_fifo );
2379 vlc_mutex_lock( &p_owner->lock );
2380 if( p_owner->fmt.i_cat == VIDEO_ES )
2382 if( p_owner->p_vout )
2383 vout_NextPicture( p_owner->p_vout, pi_duration );
2385 vlc_mutex_unlock( &p_owner->lock );
2388 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
2390 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2391 bool b_changed;
2393 vlc_mutex_lock( &p_owner->lock );
2394 b_changed = p_owner->b_fmt_description;
2395 if( b_changed )
2397 if( p_fmt != NULL )
2398 es_format_Copy( p_fmt, &p_owner->fmt );
2400 if( pp_meta )
2402 *pp_meta = NULL;
2403 if( p_owner->p_description )
2405 *pp_meta = vlc_meta_New();
2406 if( *pp_meta )
2407 vlc_meta_Merge( *pp_meta, p_owner->p_description );
2410 p_owner->b_fmt_description = false;
2412 vlc_mutex_unlock( &p_owner->lock );
2413 return b_changed;
2416 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
2418 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2420 return block_FifoSize( p_owner->p_fifo );
2423 void input_DecoderGetObjects( decoder_t *p_dec,
2424 vout_thread_t **pp_vout, audio_output_t **pp_aout )
2426 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2428 vlc_mutex_lock( &p_owner->lock );
2429 if( pp_vout )
2430 *pp_vout = p_dec->fmt_out.i_cat == VIDEO_ES && p_owner->p_vout ?
2431 vlc_object_hold( p_owner->p_vout ) : NULL;
2432 if( pp_aout )
2433 *pp_aout = p_dec->fmt_out.i_cat == AUDIO_ES && p_owner->p_aout ?
2434 vlc_object_hold( p_owner->p_aout ) : NULL;
2435 vlc_mutex_unlock( &p_owner->lock );