dec: move output handling into new functions
[vlc.git] / src / input / decoder.c
blobb996f215b65bd9861d18f0fb086f39f3094f8f69
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 int i_spu_channel;
75 int64_t i_spu_order;
77 sout_instance_t *p_sout;
78 sout_packetizer_input_t *p_sout_input;
80 vlc_thread_t thread;
82 void (*pf_update_stat)( struct decoder_owner *, unsigned decoded, unsigned lost );
84 /* Some decoders require already packetized data (ie. not truncated) */
85 decoder_t *p_packetizer;
86 bool b_packetizer;
88 /* Current format in use by the output */
89 es_format_t fmt;
91 /* */
92 bool b_fmt_description;
93 vlc_meta_t *p_description;
94 atomic_int reload;
96 /* fifo */
97 block_fifo_t *p_fifo;
99 /* Lock for communication with decoder thread */
100 vlc_mutex_t lock;
101 vlc_cond_t wait_request;
102 vlc_cond_t wait_acknowledge;
103 vlc_cond_t wait_fifo; /* TODO: merge with wait_acknowledge */
104 vlc_cond_t wait_timed;
106 /* -- These variables need locking on write(only) -- */
107 audio_output_t *p_aout;
109 vout_thread_t *p_vout;
111 /* -- Theses variables need locking on read *and* write -- */
112 /* Preroll */
113 mtime_t i_preroll_end;
114 /* Pause & Rate */
115 mtime_t pause_date;
116 float rate;
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 (CLOCK_FREQ/5)
152 #define BLOCK_FLAG_CORE_PRIVATE_RELOADED (1 << BLOCK_FLAG_CORE_PRIVATE_SHIFT)
154 #define VLC_TS_OLDEST (VLC_TS_INVALID + 1)
156 static inline struct decoder_owner *dec_get_owner( decoder_t *p_dec )
158 return container_of( p_dec, struct decoder_owner, dec );
162 * Load a decoder module
164 static int LoadDecoder( decoder_t *p_dec, bool b_packetizer,
165 const es_format_t *restrict p_fmt )
167 p_dec->b_frame_drop_allowed = true;
168 p_dec->i_extra_picture_buffers = 0;
170 p_dec->pf_decode = NULL;
171 p_dec->pf_get_cc = NULL;
172 p_dec->pf_packetize = NULL;
173 p_dec->pf_flush = NULL;
175 es_format_Copy( &p_dec->fmt_in, p_fmt );
176 es_format_Init( &p_dec->fmt_out, p_fmt->i_cat, 0 );
178 /* Find a suitable decoder/packetizer module */
179 if( !b_packetizer )
181 static const char caps[ES_CATEGORY_COUNT][16] = {
182 [VIDEO_ES] = "video decoder",
183 [AUDIO_ES] = "audio decoder",
184 [SPU_ES] = "spu decoder",
186 p_dec->p_module = module_need_var( p_dec, caps[p_dec->fmt_in.i_cat],
187 "codec" );
189 else
190 p_dec->p_module = module_need_var( p_dec, "packetizer", "packetizer" );
192 if( !p_dec->p_module )
194 es_format_Clean( &p_dec->fmt_in );
195 return -1;
197 else
198 return 0;
202 * Unload a decoder module
204 static void UnloadDecoder( decoder_t *p_dec )
206 if( p_dec->p_module )
208 module_unneed( p_dec, p_dec->p_module );
209 p_dec->p_module = NULL;
212 if( p_dec->p_description )
214 vlc_meta_Delete( p_dec->p_description );
215 p_dec->p_description = NULL;
218 es_format_Clean( &p_dec->fmt_in );
219 es_format_Clean( &p_dec->fmt_out );
222 static int ReloadDecoder( decoder_t *p_dec, bool b_packetizer,
223 const es_format_t *restrict p_fmt, enum reload reload )
225 /* Copy p_fmt since it can be destroyed by UnloadDecoder */
226 struct decoder_owner *p_owner = dec_get_owner( p_dec );
227 es_format_t fmt_in;
228 if( es_format_Copy( &fmt_in, p_fmt ) != VLC_SUCCESS )
230 p_owner->error = true;
231 return VLC_EGENERIC;
234 /* Restart the decoder module */
235 UnloadDecoder( p_dec );
236 p_owner->error = false;
238 if( reload == RELOAD_DECODER_AOUT )
240 assert( p_owner->fmt.i_cat == AUDIO_ES );
241 audio_output_t *p_aout = p_owner->p_aout;
243 vlc_mutex_lock( &p_owner->lock );
244 p_owner->p_aout = NULL;
245 vlc_mutex_unlock( &p_owner->lock );
246 if( p_aout )
248 aout_DecDelete( p_aout );
249 input_resource_PutAout( p_owner->p_resource, p_aout );
253 if( LoadDecoder( p_dec, b_packetizer, &fmt_in ) )
255 p_owner->error = true;
256 es_format_Clean( &fmt_in );
257 return VLC_EGENERIC;
259 es_format_Clean( &fmt_in );
260 return VLC_SUCCESS;
263 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
265 struct decoder_owner *p_owner = dec_get_owner( p_dec );
267 vlc_assert_locked( &p_owner->lock );
269 es_format_Clean( &p_owner->fmt );
270 es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
272 /* Move p_description */
273 if( p_dec->p_description != NULL )
275 if( p_owner->p_description != NULL )
276 vlc_meta_Delete( p_owner->p_description );
277 p_owner->p_description = p_dec->p_description;
278 p_dec->p_description = NULL;
281 p_owner->b_fmt_description = true;
284 /*****************************************************************************
285 * Buffers allocation callbacks for the decoders
286 *****************************************************************************/
287 static vout_thread_t *aout_request_vout( void *p_private,
288 vout_thread_t *p_vout,
289 const video_format_t *p_fmt, bool b_recyle )
291 decoder_t *p_dec = p_private;
292 struct decoder_owner *p_owner = dec_get_owner( p_dec );
293 input_thread_t *p_input = p_owner->p_input;
295 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
296 b_recyle );
297 if( p_input != NULL )
298 input_SendEventVout( p_input );
300 return p_vout;
303 static bool aout_replaygain_changed( const audio_replay_gain_t *a,
304 const audio_replay_gain_t *b )
306 for( size_t i=0; i<AUDIO_REPLAY_GAIN_MAX; i++ )
308 if( a->pb_gain[i] != b->pb_gain[i] ||
309 a->pb_peak[i] != b->pb_peak[i] ||
310 a->pb_gain[i] != b->pb_gain[i] ||
311 a->pb_peak[i] != b->pb_peak[i] )
312 return true;
314 return false;
317 static int aout_update_format( decoder_t *p_dec )
319 struct decoder_owner *p_owner = dec_get_owner( p_dec );
321 if( p_owner->p_aout &&
322 ( !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->fmt.audio) ||
323 p_dec->fmt_out.i_codec != p_dec->fmt_out.audio.i_format ||
324 p_dec->fmt_out.i_profile != p_owner->fmt.i_profile ) )
326 audio_output_t *p_aout = p_owner->p_aout;
328 /* Parameters changed, restart the aout */
329 vlc_mutex_lock( &p_owner->lock );
330 p_owner->p_aout = NULL;
331 vlc_mutex_unlock( &p_owner->lock );
332 aout_DecDelete( p_aout );
334 input_resource_PutAout( p_owner->p_resource, p_aout );
337 /* Check if only replay gain has changed */
338 if( aout_replaygain_changed( &p_dec->fmt_in.audio_replay_gain,
339 &p_owner->fmt.audio_replay_gain ) )
341 p_dec->fmt_out.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
342 if( p_owner->p_aout )
344 p_owner->fmt.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
345 var_TriggerCallback( p_owner->p_aout, "audio-replay-gain-mode" );
349 if( p_owner->p_aout == NULL )
351 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
353 audio_sample_format_t format = p_dec->fmt_out.audio;
354 aout_FormatPrepare( &format );
356 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
357 if( i_force_dolby &&
358 format.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
360 if( i_force_dolby == 1 )
361 format.i_chan_mode |= AOUT_CHANMODE_DOLBYSTEREO;
362 else /* i_force_dolby == 2 */
363 format.i_chan_mode &= ~AOUT_CHANMODE_DOLBYSTEREO;
366 aout_request_vout_t request_vout = {
367 .pf_request_vout = aout_request_vout,
368 .p_private = p_dec,
370 audio_output_t *p_aout;
372 p_aout = input_resource_GetAout( p_owner->p_resource );
373 if( p_aout )
375 /* TODO: 3.0 HACK: we need to put i_profile inside audio_format_t
376 * for 4.0 */
377 if( p_dec->fmt_out.i_codec == VLC_CODEC_DTS )
378 var_SetBool( p_aout, "dtshd", p_dec->fmt_out.i_profile > 0 );
380 if( aout_DecNew( p_aout, &format,
381 &p_dec->fmt_out.audio_replay_gain,
382 &request_vout ) )
384 input_resource_PutAout( p_owner->p_resource, p_aout );
385 p_aout = NULL;
389 vlc_mutex_lock( &p_owner->lock );
390 p_owner->p_aout = p_aout;
392 DecoderUpdateFormatLocked( p_dec );
393 aout_FormatPrepare( &p_owner->fmt.audio );
394 vlc_mutex_unlock( &p_owner->lock );
396 if( p_owner->p_input != NULL )
397 input_SendEventAout( p_owner->p_input );
399 if( p_aout == NULL )
401 msg_Err( p_dec, "failed to create audio output" );
402 return -1;
405 p_dec->fmt_out.audio.i_bytes_per_frame =
406 p_owner->fmt.audio.i_bytes_per_frame;
407 p_dec->fmt_out.audio.i_frame_length =
408 p_owner->fmt.audio.i_frame_length;
410 return 0;
413 static int vout_update_format( decoder_t *p_dec )
415 struct decoder_owner *p_owner = dec_get_owner( p_dec );
417 if( p_owner->p_vout == NULL
418 || p_dec->fmt_out.video.i_width != p_owner->fmt.video.i_width
419 || p_dec->fmt_out.video.i_height != p_owner->fmt.video.i_height
420 || p_dec->fmt_out.video.i_visible_width != p_owner->fmt.video.i_visible_width
421 || p_dec->fmt_out.video.i_visible_height != p_owner->fmt.video.i_visible_height
422 || p_dec->fmt_out.video.i_x_offset != p_owner->fmt.video.i_x_offset
423 || p_dec->fmt_out.video.i_y_offset != p_owner->fmt.video.i_y_offset
424 || p_dec->fmt_out.i_codec != p_owner->fmt.video.i_chroma
425 || (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->fmt.video.i_sar_den !=
426 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->fmt.video.i_sar_num ||
427 p_dec->fmt_out.video.orientation != p_owner->fmt.video.orientation ||
428 p_dec->fmt_out.video.multiview_mode != p_owner->fmt.video.multiview_mode )
430 vout_thread_t *p_vout;
432 if( !p_dec->fmt_out.video.i_width ||
433 !p_dec->fmt_out.video.i_height ||
434 p_dec->fmt_out.video.i_width < p_dec->fmt_out.video.i_visible_width ||
435 p_dec->fmt_out.video.i_height < p_dec->fmt_out.video.i_visible_height )
437 /* Can't create a new vout without display size */
438 return -1;
441 video_format_t fmt = p_dec->fmt_out.video;
442 fmt.i_chroma = p_dec->fmt_out.i_codec;
444 if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
446 const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
447 for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
449 while( fmt.i_width % dsc->p[i].w.den )
450 fmt.i_width++;
451 while( fmt.i_height % dsc->p[i].h.den )
452 fmt.i_height++;
456 if( !fmt.i_visible_width || !fmt.i_visible_height )
458 if( p_dec->fmt_in.video.i_visible_width &&
459 p_dec->fmt_in.video.i_visible_height )
461 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
462 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
463 fmt.i_x_offset = p_dec->fmt_in.video.i_x_offset;
464 fmt.i_y_offset = p_dec->fmt_in.video.i_y_offset;
466 else
468 fmt.i_visible_width = fmt.i_width;
469 fmt.i_visible_height = fmt.i_height;
470 fmt.i_x_offset = 0;
471 fmt.i_y_offset = 0;
475 if( fmt.i_visible_height == 1088 &&
476 var_CreateGetBool( p_dec, "hdtv-fix" ) )
478 fmt.i_visible_height = 1080;
479 if( !(fmt.i_sar_num % 136))
481 fmt.i_sar_num *= 135;
482 fmt.i_sar_den *= 136;
484 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
487 if( !fmt.i_sar_num || !fmt.i_sar_den )
489 fmt.i_sar_num = 1;
490 fmt.i_sar_den = 1;
493 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
494 fmt.i_sar_num, fmt.i_sar_den, 50000 );
496 video_format_AdjustColorSpace( &fmt );
498 vlc_mutex_lock( &p_owner->lock );
500 p_vout = p_owner->p_vout;
501 p_owner->p_vout = NULL;
502 vlc_mutex_unlock( &p_owner->lock );
504 unsigned dpb_size;
505 switch( p_dec->fmt_in.i_codec )
507 case VLC_CODEC_HEVC:
508 case VLC_CODEC_H264:
509 case VLC_CODEC_DIRAC: /* FIXME valid ? */
510 dpb_size = 18;
511 break;
512 case VLC_CODEC_VP5:
513 case VLC_CODEC_VP6:
514 case VLC_CODEC_VP6F:
515 case VLC_CODEC_VP8:
516 dpb_size = 3;
517 break;
518 default:
519 dpb_size = 2;
520 break;
522 p_vout = input_resource_RequestVout( p_owner->p_resource,
523 p_vout, &fmt,
524 dpb_size +
525 p_dec->i_extra_picture_buffers + 1,
526 true );
527 vlc_mutex_lock( &p_owner->lock );
528 p_owner->p_vout = p_vout;
530 DecoderUpdateFormatLocked( p_dec );
531 p_owner->fmt.video.i_chroma = p_dec->fmt_out.i_codec;
532 vlc_mutex_unlock( &p_owner->lock );
534 if( p_owner->p_input != NULL )
535 input_SendEventVout( p_owner->p_input );
536 if( p_vout == NULL )
538 msg_Err( p_dec, "failed to create video output" );
539 return -1;
543 if ( memcmp( &p_dec->fmt_out.video.mastering,
544 &p_owner->fmt.video.mastering,
545 sizeof(p_owner->fmt.video.mastering)) ||
546 p_dec->fmt_out.video.lighting.MaxCLL !=
547 p_owner->fmt.video.lighting.MaxCLL ||
548 p_dec->fmt_out.video.lighting.MaxFALL !=
549 p_owner->fmt.video.lighting.MaxFALL)
551 /* the format has changed but we don't need a new vout */
552 vlc_mutex_lock( &p_owner->lock );
553 DecoderUpdateFormatLocked( p_dec );
554 vlc_mutex_unlock( &p_owner->lock );
556 return 0;
559 static picture_t *vout_new_buffer( decoder_t *p_dec )
561 struct decoder_owner *p_owner = dec_get_owner( p_dec );
562 assert( p_owner->p_vout );
564 return vout_GetPicture( p_owner->p_vout );
567 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
568 const subpicture_updater_t *p_updater )
570 struct decoder_owner *p_owner = dec_get_owner( p_dec );
571 vout_thread_t *p_vout = NULL;
572 subpicture_t *p_subpic;
573 int i_attempts = 30;
575 while( i_attempts-- )
577 if( p_owner->error )
578 break;
580 p_vout = input_resource_HoldVout( p_owner->p_resource );
581 if( p_vout )
582 break;
584 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
587 if( !p_vout )
589 msg_Warn( p_dec, "no vout found, dropping subpicture" );
590 return NULL;
593 if( p_owner->p_vout != p_vout )
595 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
596 p_owner->i_spu_order = 0;
597 p_owner->p_vout = p_vout;
600 p_subpic = subpicture_New( p_updater );
601 if( p_subpic )
603 p_subpic->i_channel = p_owner->i_spu_channel;
604 p_subpic->i_order = p_owner->i_spu_order++;
605 p_subpic->b_subtitle = true;
608 vlc_object_release( p_vout );
610 return p_subpic;
613 static int DecoderGetInputAttachments( decoder_t *p_dec,
614 input_attachment_t ***ppp_attachment,
615 int *pi_attachment )
617 struct decoder_owner *p_owner = dec_get_owner( p_dec );
618 input_thread_t *p_input = p_owner->p_input;
620 if( unlikely(p_input == NULL) )
621 return VLC_ENOOBJ;
622 return input_Control( p_input, INPUT_GET_ATTACHMENTS,
623 ppp_attachment, pi_attachment );
626 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
628 struct decoder_owner *p_owner = dec_get_owner( p_dec );
630 vlc_mutex_lock( &p_owner->lock );
631 if( p_owner->b_waiting || p_owner->paused )
632 i_ts = VLC_TS_INVALID;
633 vlc_mutex_unlock( &p_owner->lock );
635 if( !p_owner->p_clock || i_ts == VLC_TS_INVALID )
636 return i_ts;
638 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) ) {
639 msg_Err(p_dec, "Could not get display date for timestamp %"PRId64"", i_ts);
640 return VLC_TS_INVALID;
643 return i_ts;
646 static int DecoderGetDisplayRate( decoder_t *p_dec )
648 struct decoder_owner *p_owner = dec_get_owner( p_dec );
650 if( !p_owner->p_clock )
651 return INPUT_RATE_DEFAULT;
652 return input_clock_GetRate( p_owner->p_clock );
655 /*****************************************************************************
656 * Public functions
657 *****************************************************************************/
658 block_t *decoder_NewAudioBuffer( decoder_t *dec, int samples )
660 assert( dec->fmt_out.audio.i_frame_length > 0
661 && dec->fmt_out.audio.i_bytes_per_frame > 0 );
663 size_t length = samples * dec->fmt_out.audio.i_bytes_per_frame
664 / dec->fmt_out.audio.i_frame_length;
665 block_t *block = block_Alloc( length );
666 if( likely(block != NULL) )
668 block->i_nb_samples = samples;
669 block->i_pts = block->i_length = 0;
671 return block;
674 static void RequestReload( decoder_t * p_dec )
676 struct decoder_owner *p_owner = dec_get_owner( p_dec );
677 /* Don't override reload if it's RELOAD_DECODER_AOUT */
678 int expected = RELOAD_NO_REQUEST;
679 atomic_compare_exchange_strong( &p_owner->reload, &expected, RELOAD_DECODER );
682 void decoder_AbortPictures( decoder_t *p_dec, bool b_abort )
684 struct decoder_owner *p_owner = dec_get_owner( p_dec );
686 vlc_mutex_lock( &p_owner->lock );
687 if( p_owner->p_vout != NULL )
688 vout_Cancel( p_owner->p_vout, b_abort );
689 vlc_mutex_unlock( &p_owner->lock );
692 static void DecoderWaitUnblock( decoder_t *p_dec )
694 struct decoder_owner *p_owner = dec_get_owner( p_dec );
696 vlc_assert_locked( &p_owner->lock );
698 for( ;; )
700 if( !p_owner->b_waiting || !p_owner->b_has_data )
701 break;
702 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
706 /* DecoderTimedWait: Interruptible wait
707 * Returns VLC_SUCCESS if wait was not interrupted, and VLC_EGENERIC otherwise */
708 static int DecoderTimedWait( decoder_t *p_dec, mtime_t deadline )
710 struct decoder_owner *p_owner = dec_get_owner( p_dec );
712 if (deadline - mdate() <= 0)
713 return VLC_SUCCESS;
715 vlc_fifo_Lock( p_owner->p_fifo );
716 while( !p_owner->flushing
717 && vlc_fifo_TimedWaitCond( p_owner->p_fifo, &p_owner->wait_timed,
718 deadline ) == 0 );
719 int ret = p_owner->flushing ? VLC_EGENERIC : VLC_SUCCESS;
720 vlc_fifo_Unlock( p_owner->p_fifo );
721 return ret;
724 static inline void DecoderUpdatePreroll( mtime_t *pi_preroll, const block_t *p )
726 if( p->i_flags & BLOCK_FLAG_PREROLL )
727 *pi_preroll = (mtime_t)INT64_MAX;
728 /* Check if we can use the packet for end of preroll */
729 else if( (p->i_flags & BLOCK_FLAG_DISCONTINUITY) &&
730 (p->i_buffer == 0 || (p->i_flags & BLOCK_FLAG_CORRUPTED)) )
731 *pi_preroll = (mtime_t)INT64_MAX;
732 else if( p->i_dts != VLC_TS_INVALID )
733 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
734 else if( p->i_pts != VLC_TS_INVALID )
735 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
738 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
739 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
741 struct decoder_owner *p_owner = dec_get_owner( p_dec );
742 input_clock_t *p_clock = p_owner->p_clock;
744 vlc_assert_locked( &p_owner->lock );
746 const mtime_t i_es_delay = p_owner->i_ts_delay;
748 if( !p_clock )
749 return;
751 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
752 int i_rate;
754 if( *pi_ts0 != VLC_TS_INVALID )
756 *pi_ts0 += i_es_delay;
757 if( pi_ts1 && *pi_ts1 != VLC_TS_INVALID )
758 *pi_ts1 += i_es_delay;
759 if( i_ts_bound != INT64_MAX )
760 i_ts_bound += i_es_delay;
761 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) ) {
762 const char *psz_name = module_get_name( p_dec->p_module, false );
763 if( pi_ts1 != NULL )
764 msg_Err(p_dec, "Could not convert timestamps %"PRId64
765 ", %"PRId64" for %s", *pi_ts0, *pi_ts1, psz_name );
766 else
767 msg_Err(p_dec, "Could not convert timestamp %"PRId64" for %s", *pi_ts0, psz_name );
768 *pi_ts0 = VLC_TS_INVALID;
771 else
773 i_rate = input_clock_GetRate( p_clock );
776 /* Do not create ephemere data because of rounding errors */
777 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
778 *pi_ts1 += 1;
780 if( pi_duration )
781 *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
782 / INPUT_RATE_DEFAULT;
784 if( pi_rate )
785 *pi_rate = i_rate;
788 #ifdef ENABLE_SOUT
789 static int DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
791 struct decoder_owner *p_owner = dec_get_owner( p_dec );
793 assert( p_owner->p_clock );
794 assert( !p_sout_block->p_next );
796 vlc_mutex_lock( &p_owner->lock );
798 if( p_owner->b_waiting )
800 p_owner->b_has_data = true;
801 vlc_cond_signal( &p_owner->wait_acknowledge );
804 DecoderWaitUnblock( p_dec );
805 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
806 &p_sout_block->i_length, NULL, INT64_MAX );
808 vlc_mutex_unlock( &p_owner->lock );
810 /* FIXME --VLC_TS_INVALID inspect stream_output*/
811 return sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
814 /* This function process a block for sout
816 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
818 struct decoder_owner *p_owner = dec_get_owner( p_dec );
819 block_t *p_sout_block;
820 block_t **pp_block = p_block ? &p_block : NULL;
822 while( ( p_sout_block =
823 p_dec->pf_packetize( p_dec, pp_block ) ) )
825 if( p_owner->p_sout_input == NULL )
827 vlc_mutex_lock( &p_owner->lock );
828 DecoderUpdateFormatLocked( p_dec );
830 p_owner->fmt.i_group = p_dec->fmt_in.i_group;
831 p_owner->fmt.i_id = p_dec->fmt_in.i_id;
832 if( p_dec->fmt_in.psz_language )
834 free( p_owner->fmt.psz_language );
835 p_owner->fmt.psz_language =
836 strdup( p_dec->fmt_in.psz_language );
838 vlc_mutex_unlock( &p_owner->lock );
840 p_owner->p_sout_input =
841 sout_InputNew( p_owner->p_sout, &p_owner->fmt );
843 if( p_owner->p_sout_input == NULL )
845 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
846 (char *)&p_owner->fmt.i_codec );
847 p_owner->error = true;
849 if(p_block)
850 block_Release(p_block);
852 block_ChainRelease(p_sout_block);
853 break;
857 while( p_sout_block )
859 block_t *p_next = p_sout_block->p_next;
861 p_sout_block->p_next = NULL;
863 if( DecoderPlaySout( p_dec, p_sout_block ) == VLC_EGENERIC )
865 msg_Err( p_dec, "cannot continue streaming due to errors with codec %4.4s",
866 (char *)&p_owner->fmt.i_codec );
868 p_owner->error = true;
870 /* Cleanup */
872 if( p_block )
873 block_Release( p_block );
875 block_ChainRelease( p_next );
876 return;
879 p_sout_block = p_next;
883 #endif
885 static void DecoderPlayCc( decoder_t *p_dec, block_t *p_cc,
886 const decoder_cc_desc_t *p_desc )
888 struct decoder_owner *p_owner = dec_get_owner( p_dec );
890 vlc_mutex_lock( &p_owner->lock );
892 p_owner->cc.desc = *p_desc;
894 /* Fanout data to all decoders. We do not know if es_out
895 selected 608 or 708. */
896 uint64_t i_bitmap = p_owner->cc.desc.i_608_channels |
897 p_owner->cc.desc.i_708_channels;
899 for( int i=0; i_bitmap > 0; i_bitmap >>= 1, i++ )
901 decoder_t *p_ccdec = p_owner->cc.pp_decoder[i];
902 struct decoder_owner *p_ccowner = dec_get_owner( p_ccdec );
903 if( !p_ccdec )
904 continue;
906 if( i_bitmap > 1 )
908 block_FifoPut( p_ccowner->p_fifo, block_Duplicate(p_cc) );
910 else
912 block_FifoPut( p_ccowner->p_fifo, p_cc );
913 p_cc = NULL; /* was last dec */
917 vlc_mutex_unlock( &p_owner->lock );
919 if( p_cc ) /* can have bitmap set but no created decs */
920 block_Release( p_cc );
923 static void PacketizerGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
925 struct decoder_owner *p_owner = dec_get_owner( p_dec );
926 block_t *p_cc;
927 decoder_cc_desc_t desc;
929 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
930 if( !p_owner->cc.b_supported )
931 return;
933 assert( p_dec_cc->pf_get_cc != NULL );
935 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, &desc );
936 if( !p_cc )
937 return;
938 DecoderPlayCc( p_dec, p_cc, &desc );
941 static void DecoderQueueCc( decoder_t *p_videodec, block_t *p_cc,
942 const decoder_cc_desc_t *p_desc )
944 struct decoder_owner *p_owner = dec_get_owner( p_videodec );
946 if( unlikely( p_cc != NULL ) )
948 if( p_owner->cc.b_supported &&
949 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
950 DecoderPlayCc( p_videodec, p_cc, p_desc );
951 else
952 block_Release( p_cc );
956 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
957 unsigned *restrict pi_lost_sum )
959 struct decoder_owner *p_owner = dec_get_owner( p_dec );
960 vout_thread_t *p_vout = p_owner->p_vout;
961 bool prerolled;
963 vlc_mutex_lock( &p_owner->lock );
964 if( p_owner->i_preroll_end > p_picture->date )
966 vlc_mutex_unlock( &p_owner->lock );
967 picture_Release( p_picture );
968 return;
971 prerolled = p_owner->i_preroll_end > (mtime_t)INT64_MIN;
972 p_owner->i_preroll_end = (mtime_t)INT64_MIN;
973 vlc_mutex_unlock( &p_owner->lock );
975 if( unlikely(prerolled) )
977 msg_Dbg( p_dec, "end of video preroll" );
979 if( p_vout )
980 vout_Flush( p_vout, VLC_TS_OLDEST );
983 if( p_picture->date == VLC_TS_INVALID )
985 msg_Warn( p_dec, "non-dated video buffer received" );
986 goto discard;
989 /* */
990 vlc_mutex_lock( &p_owner->lock );
992 if( p_owner->b_waiting && !p_owner->b_first )
994 p_owner->b_has_data = true;
995 vlc_cond_signal( &p_owner->wait_acknowledge );
997 bool b_first_after_wait = p_owner->b_waiting && p_owner->b_has_data;
999 DecoderWaitUnblock( p_dec );
1001 if( p_owner->b_waiting )
1003 assert( p_owner->b_first );
1004 msg_Dbg( p_dec, "Received first picture" );
1005 p_owner->b_first = false;
1006 p_picture->b_force = true;
1009 const bool b_dated = p_picture->date != VLC_TS_INVALID;
1010 int i_rate = INPUT_RATE_DEFAULT;
1011 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1012 &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1014 vlc_mutex_unlock( &p_owner->lock );
1016 /* FIXME: The *input* FIFO should not be locked here. This will not work
1017 * properly if/when pictures are queued asynchronously. */
1018 vlc_fifo_Lock( p_owner->p_fifo );
1019 if( unlikely(p_owner->paused) && likely(p_owner->frames_countdown > 0) )
1020 p_owner->frames_countdown--;
1021 vlc_fifo_Unlock( p_owner->p_fifo );
1023 /* */
1024 if( p_vout == NULL )
1025 goto discard;
1027 if( p_picture->b_force || p_picture->date != VLC_TS_INVALID )
1028 /* FIXME: VLC_TS_INVALID -- verify video_output */
1030 if( i_rate != p_owner->i_last_rate || b_first_after_wait )
1032 /* Be sure to not display old picture after our own */
1033 vout_Flush( p_vout, p_picture->date );
1034 p_owner->i_last_rate = i_rate;
1036 vout_PutPicture( p_vout, p_picture );
1038 else
1040 if( b_dated )
1041 msg_Warn( p_dec, "early picture skipped" );
1042 else
1043 msg_Warn( p_dec, "non-dated video buffer received" );
1044 goto discard;
1047 return;
1048 discard:
1049 *pi_lost_sum += 1;
1050 picture_Release( p_picture );
1053 static void DecoderUpdateStatVideo( struct decoder_owner *p_owner,
1054 unsigned decoded, unsigned lost )
1056 input_thread_t *p_input = p_owner->p_input;
1057 unsigned displayed = 0;
1059 /* Update ugly stat */
1060 if( p_input == NULL )
1061 return;
1063 if( p_owner->p_vout != NULL )
1065 unsigned vout_lost = 0;
1067 vout_GetResetStatistic( p_owner->p_vout, &displayed, &vout_lost );
1068 lost += vout_lost;
1071 struct input_stats *stats = input_priv(p_input)->stats;
1073 if( stats != NULL )
1075 atomic_fetch_add_explicit(&stats->decoded_video, decoded,
1076 memory_order_relaxed);
1077 atomic_fetch_add_explicit(&stats->lost_pictures, lost,
1078 memory_order_relaxed);
1079 atomic_fetch_add_explicit(&stats->displayed_pictures, displayed,
1080 memory_order_relaxed);
1084 static void DecoderQueueVideo( decoder_t *p_dec, picture_t *p_pic )
1086 assert( p_pic );
1087 unsigned i_lost = 0;
1088 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1090 DecoderPlayVideo( p_dec, p_pic, &i_lost );
1092 p_owner->pf_update_stat( p_owner, 1, i_lost );
1095 static void DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
1096 unsigned *restrict pi_lost_sum )
1098 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1099 bool prerolled;
1101 assert( p_audio != NULL );
1103 vlc_mutex_lock( &p_owner->lock );
1104 if( p_owner->i_preroll_end > p_audio->i_pts )
1106 vlc_mutex_unlock( &p_owner->lock );
1107 block_Release( p_audio );
1108 return;
1111 prerolled = p_owner->i_preroll_end > (mtime_t)INT64_MIN;
1112 p_owner->i_preroll_end = (mtime_t)INT64_MIN;
1113 vlc_mutex_unlock( &p_owner->lock );
1115 if( unlikely(prerolled) )
1117 msg_Dbg( p_dec, "end of audio preroll" );
1119 if( p_owner->p_aout )
1120 aout_DecFlush( p_owner->p_aout, false );
1123 /* */
1124 if( p_audio->i_pts == VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1126 msg_Warn( p_dec, "non-dated audio buffer received" );
1127 *pi_lost_sum += 1;
1128 block_Release( p_audio );
1129 return;
1132 /* */
1133 vlc_mutex_lock( &p_owner->lock );
1134 if( p_owner->b_waiting )
1136 p_owner->b_has_data = true;
1137 vlc_cond_signal( &p_owner->wait_acknowledge );
1140 /* */
1141 int i_rate = INPUT_RATE_DEFAULT;
1143 DecoderWaitUnblock( p_dec );
1144 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1145 &i_rate, AOUT_MAX_ADVANCE_TIME );
1146 vlc_mutex_unlock( &p_owner->lock );
1148 audio_output_t *p_aout = p_owner->p_aout;
1150 if( p_aout != NULL && p_audio->i_pts != VLC_TS_INVALID
1151 && i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1152 && i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE
1153 && !DecoderTimedWait( p_dec, p_audio->i_pts - AOUT_MAX_PREPARE_TIME ) )
1155 int status = aout_DecPlay( p_aout, p_audio );
1156 if( status == AOUT_DEC_CHANGED )
1158 /* Only reload the decoder */
1159 RequestReload( p_dec );
1161 else if( status == AOUT_DEC_FAILED )
1163 /* If we reload because the aout failed, we should release it. That
1164 * way, a next call to aout_update_format() won't re-use the
1165 * previous (failing) aout but will try to create a new one. */
1166 atomic_store( &p_owner->reload, RELOAD_DECODER_AOUT );
1169 else
1171 msg_Dbg( p_dec, "discarded audio buffer" );
1172 *pi_lost_sum += 1;
1173 block_Release( p_audio );
1175 return;
1178 static void DecoderUpdateStatAudio( struct decoder_owner *p_owner,
1179 unsigned decoded, unsigned lost )
1181 input_thread_t *p_input = p_owner->p_input;
1182 unsigned played = 0;
1184 /* Update ugly stat */
1185 if( p_input == NULL )
1186 return;
1188 if( p_owner->p_aout != NULL )
1190 unsigned aout_lost;
1192 aout_DecGetResetStats( p_owner->p_aout, &aout_lost, &played );
1193 lost += aout_lost;
1196 struct input_stats *stats = input_priv(p_input)->stats;
1198 if( stats != NULL )
1200 atomic_fetch_add_explicit(&stats->lost_abuffers, lost,
1201 memory_order_relaxed);
1202 atomic_fetch_add_explicit(&stats->played_abuffers, played,
1203 memory_order_relaxed);
1204 atomic_fetch_add_explicit(&stats->decoded_audio, decoded,
1205 memory_order_relaxed);
1209 static void DecoderQueueAudio( decoder_t *p_dec, block_t *p_aout_buf )
1211 unsigned lost = 0;
1212 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1214 DecoderPlayAudio( p_dec, p_aout_buf, &lost );
1216 p_owner->pf_update_stat( p_owner, 1, lost );
1219 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1221 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1222 vout_thread_t *p_vout = p_owner->p_vout;
1224 /* */
1225 if( p_subpic->i_start == VLC_TS_INVALID )
1227 msg_Warn( p_dec, "non-dated spu buffer received" );
1228 subpicture_Delete( p_subpic );
1229 return;
1232 /* */
1233 vlc_mutex_lock( &p_owner->lock );
1235 if( p_owner->b_waiting )
1237 p_owner->b_has_data = true;
1238 vlc_cond_signal( &p_owner->wait_acknowledge );
1241 DecoderWaitUnblock( p_dec );
1242 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1243 NULL, INT64_MAX );
1244 vlc_mutex_unlock( &p_owner->lock );
1246 if( p_subpic->i_start == VLC_TS_INVALID
1247 || DecoderTimedWait( p_dec, p_subpic->i_start - SPU_MAX_PREPARE_TIME ) )
1249 subpicture_Delete( p_subpic );
1250 return;
1253 vout_PutSubpicture( p_vout, p_subpic );
1256 static void DecoderUpdateStatSpu( struct decoder_owner *p_owner,
1257 unsigned decoded, unsigned lost )
1259 (void) p_owner; (void) decoded; (void) lost;
1262 static void DecoderQueueSpu( decoder_t *p_dec, subpicture_t *p_spu )
1264 assert( p_spu );
1265 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1267 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1268 if( p_vout && p_owner->p_vout == p_vout )
1270 /* Preroll does not work very well with subtitle */
1271 vlc_mutex_lock( &p_owner->lock );
1272 if( p_spu->i_start != VLC_TS_INVALID &&
1273 p_spu->i_start < p_owner->i_preroll_end &&
1274 ( p_spu->i_stop == VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1276 vlc_mutex_unlock( &p_owner->lock );
1277 subpicture_Delete( p_spu );
1279 else
1281 vlc_mutex_unlock( &p_owner->lock );
1282 DecoderPlaySpu( p_dec, p_spu );
1285 else
1287 subpicture_Delete( p_spu );
1289 if( p_vout )
1290 vlc_object_release( p_vout );
1293 static void DecoderProcess( decoder_t *p_dec, block_t *p_block );
1294 static void DecoderDecode( decoder_t *p_dec, block_t *p_block )
1296 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1298 int ret = p_dec->pf_decode( p_dec, p_block );
1299 switch( ret )
1301 case VLCDEC_SUCCESS:
1302 p_owner->pf_update_stat( p_owner, 1, 0 );
1303 break;
1304 case VLCDEC_ECRITICAL:
1305 p_owner->error = true;
1306 break;
1307 case VLCDEC_RELOAD:
1308 RequestReload( p_dec );
1309 if( unlikely( p_block == NULL ) )
1310 break;
1311 if( !( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1313 p_block->i_flags |= BLOCK_FLAG_CORE_PRIVATE_RELOADED;
1314 DecoderProcess( p_dec, p_block );
1316 else /* We prefer loosing this block than an infinite recursion */
1317 block_Release( p_block );
1318 break;
1319 default:
1320 vlc_assert_unreachable();
1325 * Decode a block
1327 * \param p_dec the decoder object
1328 * \param p_block the block to decode
1330 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1332 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1334 if( p_owner->error )
1335 goto error;
1337 /* Here, the atomic doesn't prevent to miss a reload request.
1338 * DecoderProcess() can still be called after the decoder module or the
1339 * audio output requested a reload. This will only result in a drop of an
1340 * input block or an output buffer. */
1341 enum reload reload;
1342 if( ( reload = atomic_exchange( &p_owner->reload, RELOAD_NO_REQUEST ) ) )
1344 msg_Warn( p_dec, "Reloading the decoder module%s",
1345 reload == RELOAD_DECODER_AOUT ? " and the audio output" : "" );
1347 if( ReloadDecoder( p_dec, false, &p_dec->fmt_in, reload ) != VLC_SUCCESS )
1348 goto error;
1351 bool packetize = p_owner->p_packetizer != NULL;
1352 if( p_block )
1354 if( p_block->i_buffer <= 0 )
1355 goto error;
1357 vlc_mutex_lock( &p_owner->lock );
1358 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1359 vlc_mutex_unlock( &p_owner->lock );
1360 if( unlikely( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1362 /* This block has already been packetized */
1363 packetize = false;
1367 #ifdef ENABLE_SOUT
1368 if( p_owner->p_sout != NULL )
1370 DecoderProcessSout( p_dec, p_block );
1371 return;
1373 #endif
1374 if( packetize )
1376 block_t *p_packetized_block;
1377 block_t **pp_block = p_block ? &p_block : NULL;
1378 decoder_t *p_packetizer = p_owner->p_packetizer;
1380 while( (p_packetized_block =
1381 p_packetizer->pf_packetize( p_packetizer, pp_block ) ) )
1383 if( !es_format_IsSimilar( &p_dec->fmt_in, &p_packetizer->fmt_out ) )
1385 msg_Dbg( p_dec, "restarting module due to input format change");
1387 /* Drain the decoder module */
1388 DecoderDecode( p_dec, NULL );
1390 if( ReloadDecoder( p_dec, false, &p_packetizer->fmt_out,
1391 RELOAD_DECODER ) != VLC_SUCCESS )
1393 block_ChainRelease( p_packetized_block );
1394 return;
1398 if( p_packetizer->pf_get_cc )
1399 PacketizerGetCc( p_dec, p_packetizer );
1401 while( p_packetized_block )
1403 block_t *p_next = p_packetized_block->p_next;
1404 p_packetized_block->p_next = NULL;
1406 DecoderDecode( p_dec, p_packetized_block );
1407 if( p_owner->error )
1409 block_ChainRelease( p_next );
1410 return;
1413 p_packetized_block = p_next;
1416 /* Drain the decoder after the packetizer is drained */
1417 if( !pp_block )
1418 DecoderDecode( p_dec, NULL );
1420 else
1421 DecoderDecode( p_dec, p_block );
1422 return;
1424 error:
1425 if( p_block )
1426 block_Release( p_block );
1429 static void DecoderProcessFlush( decoder_t *p_dec )
1431 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1432 decoder_t *p_packetizer = p_owner->p_packetizer;
1434 if( p_owner->error )
1435 return;
1437 if( p_packetizer != NULL && p_packetizer->pf_flush != NULL )
1438 p_packetizer->pf_flush( p_packetizer );
1440 if ( p_dec->pf_flush != NULL )
1441 p_dec->pf_flush( p_dec );
1443 /* flush CC sub decoders */
1444 if( p_owner->cc.b_supported )
1446 for( int i=0; i<MAX_CC_DECODERS; i++ )
1448 decoder_t *p_subdec = p_owner->cc.pp_decoder[i];
1449 if( p_subdec && p_subdec->pf_flush )
1450 p_subdec->pf_flush( p_subdec );
1454 #ifdef ENABLE_SOUT
1455 if ( p_owner->p_sout_input != NULL )
1457 sout_InputFlush( p_owner->p_sout_input );
1459 #endif
1460 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1462 if( p_owner->p_aout )
1463 aout_DecFlush( p_owner->p_aout, false );
1465 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1467 if( p_owner->p_vout )
1468 vout_Flush( p_owner->p_vout, VLC_TS_OLDEST );
1470 else if( p_dec->fmt_out.i_cat == SPU_ES )
1472 if( p_owner->p_vout )
1474 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1476 if( p_vout && p_owner->p_vout == p_vout )
1477 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1479 if( p_vout )
1480 vlc_object_release( p_vout );
1484 vlc_mutex_lock( &p_owner->lock );
1485 p_owner->i_preroll_end = (mtime_t)INT64_MIN;
1486 vlc_mutex_unlock( &p_owner->lock );
1489 static void OutputChangePause( decoder_t *p_dec, bool paused, mtime_t date )
1491 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1493 msg_Dbg( p_dec, "toggling %s", paused ? "resume" : "pause" );
1494 switch( p_dec->fmt_out.i_cat )
1496 case VIDEO_ES:
1497 if( p_owner->p_vout != NULL )
1498 vout_ChangePause( p_owner->p_vout, paused, date );
1499 break;
1500 case AUDIO_ES:
1501 if( p_owner->p_aout != NULL )
1502 aout_DecChangePause( p_owner->p_aout, paused, date );
1503 break;
1504 case SPU_ES:
1505 break;
1506 default:
1507 vlc_assert_unreachable();
1511 static void OutputChangeRate( decoder_t *p_dec, float rate )
1513 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1515 msg_Dbg( p_dec, "changing rate: %f", rate );
1516 switch( p_dec->fmt_out.i_cat )
1518 case VIDEO_ES:
1519 break;
1520 case AUDIO_ES:
1521 if( p_owner->p_aout != NULL )
1522 aout_DecChangeRate( p_owner->p_aout, rate );
1523 break;
1524 case SPU_ES:
1525 break;
1526 default:
1527 vlc_assert_unreachable();
1532 * The decoding main loop
1534 * \param p_dec the decoder
1536 static void *DecoderThread( void *p_data )
1538 decoder_t *p_dec = (decoder_t *)p_data;
1539 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1540 float rate = 1.f;
1541 bool paused = false;
1543 /* The decoder's main loop */
1544 vlc_fifo_Lock( p_owner->p_fifo );
1545 vlc_fifo_CleanupPush( p_owner->p_fifo );
1547 for( ;; )
1549 if( p_owner->flushing )
1550 { /* Flush before/regardless of pause. We do not want to resume just
1551 * for the sake of flushing (glitches could otherwise happen). */
1552 int canc = vlc_savecancel();
1554 vlc_fifo_Unlock( p_owner->p_fifo );
1556 /* Flush the decoder (and the output) */
1557 DecoderProcessFlush( p_dec );
1559 vlc_fifo_Lock( p_owner->p_fifo );
1560 vlc_restorecancel( canc );
1562 /* Reset flushing after DecoderProcess in case input_DecoderFlush
1563 * is called again. This will avoid a second useless flush (but
1564 * harmless). */
1565 p_owner->flushing = false;
1567 continue;
1570 if( paused != p_owner->paused )
1571 { /* Update playing/paused status of the output */
1572 int canc = vlc_savecancel();
1573 mtime_t date = p_owner->pause_date;
1575 paused = p_owner->paused;
1576 vlc_fifo_Unlock( p_owner->p_fifo );
1578 OutputChangePause( p_dec, paused, date );
1580 vlc_restorecancel( canc );
1581 vlc_fifo_Lock( p_owner->p_fifo );
1582 continue;
1585 if( rate != p_owner->rate )
1587 int canc = vlc_savecancel();
1589 rate = p_owner->rate;
1590 vlc_fifo_Unlock( p_owner->p_fifo );
1592 OutputChangeRate( p_dec, rate );
1594 vlc_restorecancel( canc );
1595 vlc_fifo_Lock( p_owner->p_fifo );
1598 if( p_owner->paused && p_owner->frames_countdown == 0 )
1599 { /* Wait for resumption from pause */
1600 p_owner->b_idle = true;
1601 vlc_cond_signal( &p_owner->wait_acknowledge );
1602 vlc_fifo_Wait( p_owner->p_fifo );
1603 p_owner->b_idle = false;
1604 continue;
1607 vlc_cond_signal( &p_owner->wait_fifo );
1608 vlc_testcancel(); /* forced expedited cancellation in case of stop */
1610 block_t *p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
1611 if( p_block == NULL )
1613 if( likely(!p_owner->b_draining) )
1614 { /* Wait for a block to decode (or a request to drain) */
1615 p_owner->b_idle = true;
1616 vlc_cond_signal( &p_owner->wait_acknowledge );
1617 vlc_fifo_Wait( p_owner->p_fifo );
1618 p_owner->b_idle = false;
1619 continue;
1621 /* We have emptied the FIFO and there is a pending request to
1622 * drain. Pass p_block = NULL to decoder just once. */
1625 vlc_fifo_Unlock( p_owner->p_fifo );
1627 int canc = vlc_savecancel();
1628 DecoderProcess( p_dec, p_block );
1630 if( p_block == NULL && p_dec->fmt_out.i_cat == AUDIO_ES )
1631 { /* Draining: the decoder is drained and all decoded buffers are
1632 * queued to the output at this point. Now drain the output. */
1633 if( p_owner->p_aout != NULL )
1634 aout_DecFlush( p_owner->p_aout, true );
1636 vlc_restorecancel( canc );
1638 /* TODO? Wait for draining instead of polling. */
1639 vlc_mutex_lock( &p_owner->lock );
1640 if( p_owner->b_draining && (p_block == NULL) )
1642 p_owner->b_draining = false;
1643 p_owner->drained = true;
1645 vlc_fifo_Lock( p_owner->p_fifo );
1646 vlc_cond_signal( &p_owner->wait_acknowledge );
1647 vlc_mutex_unlock( &p_owner->lock );
1649 vlc_cleanup_pop();
1650 vlc_assert_unreachable();
1653 static const struct decoder_owner_callbacks dec_video_cbs =
1655 .video = {
1656 vout_update_format,
1657 vout_new_buffer,
1658 DecoderQueueVideo,
1659 DecoderQueueCc,
1660 DecoderGetDisplayDate,
1661 DecoderGetDisplayRate
1663 DecoderGetInputAttachments,
1665 static const struct decoder_owner_callbacks dec_audio_cbs =
1667 .audio = {
1668 aout_update_format,
1669 DecoderQueueAudio,
1671 DecoderGetInputAttachments,
1673 static const struct decoder_owner_callbacks dec_spu_cbs =
1675 .spu = {
1676 spu_new_buffer,
1677 DecoderQueueSpu
1679 DecoderGetInputAttachments,
1683 * Create a decoder object
1685 * \param p_input the input thread
1686 * \param p_es the es descriptor
1687 * \param b_packetizer instead of a decoder
1688 * \return the decoder object
1690 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
1691 input_thread_t *p_input,
1692 const es_format_t *fmt,
1693 input_resource_t *p_resource,
1694 sout_instance_t *p_sout )
1696 decoder_t *p_dec;
1697 struct decoder_owner *p_owner;
1699 p_owner = vlc_custom_create( p_parent, sizeof( *p_owner ), "decoder" );
1700 if( p_owner == NULL )
1701 return NULL;
1702 p_dec = &p_owner->dec;
1704 p_owner->i_preroll_end = (mtime_t)INT64_MIN;
1705 p_owner->i_last_rate = INPUT_RATE_DEFAULT;
1706 p_owner->p_input = p_input;
1707 p_owner->p_resource = p_resource;
1708 p_owner->p_aout = NULL;
1709 p_owner->p_vout = NULL;
1710 p_owner->i_spu_channel = 0;
1711 p_owner->i_spu_order = 0;
1712 p_owner->p_sout = p_sout;
1713 p_owner->p_sout_input = NULL;
1714 p_owner->p_packetizer = NULL;
1716 p_owner->b_fmt_description = false;
1717 p_owner->p_description = NULL;
1719 p_owner->rate = 1.f;
1720 p_owner->paused = false;
1721 p_owner->pause_date = VLC_TS_INVALID;
1722 p_owner->frames_countdown = 0;
1724 p_owner->b_waiting = false;
1725 p_owner->b_first = true;
1726 p_owner->b_has_data = false;
1728 p_owner->error = false;
1730 p_owner->flushing = false;
1731 p_owner->b_draining = false;
1732 p_owner->drained = false;
1733 atomic_init( &p_owner->reload, RELOAD_NO_REQUEST );
1734 p_owner->b_idle = false;
1736 es_format_Init( &p_owner->fmt, fmt->i_cat, 0 );
1738 /* decoder fifo */
1739 p_owner->p_fifo = block_FifoNew();
1740 if( unlikely(p_owner->p_fifo == NULL) )
1742 vlc_object_release( p_dec );
1743 return NULL;
1746 vlc_mutex_init( &p_owner->lock );
1747 vlc_cond_init( &p_owner->wait_request );
1748 vlc_cond_init( &p_owner->wait_acknowledge );
1749 vlc_cond_init( &p_owner->wait_fifo );
1750 vlc_cond_init( &p_owner->wait_timed );
1752 /* Load a packetizer module if the input is not already packetized */
1753 if( p_sout == NULL && !fmt->b_packetized )
1755 p_owner->p_packetizer =
1756 vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
1757 if( p_owner->p_packetizer )
1759 if( LoadDecoder( p_owner->p_packetizer, true, fmt ) )
1761 vlc_object_release( p_owner->p_packetizer );
1762 p_owner->p_packetizer = NULL;
1764 else
1766 p_owner->p_packetizer->fmt_out.b_packetized = true;
1767 fmt = &p_owner->p_packetizer->fmt_out;
1772 switch( fmt->i_cat )
1774 case VIDEO_ES:
1775 p_dec->cbs = &dec_video_cbs;
1776 p_owner->pf_update_stat = DecoderUpdateStatVideo;
1777 break;
1778 case AUDIO_ES:
1779 p_dec->cbs = &dec_audio_cbs;
1780 p_owner->pf_update_stat = DecoderUpdateStatAudio;
1781 break;
1782 case SPU_ES:
1783 p_dec->cbs = &dec_spu_cbs;
1784 p_owner->pf_update_stat = DecoderUpdateStatSpu;
1785 break;
1786 default:
1787 msg_Err( p_dec, "unknown ES format" );
1788 return p_dec;
1791 /* Find a suitable decoder/packetizer module */
1792 if( LoadDecoder( p_dec, p_sout != NULL, fmt ) )
1793 return p_dec;
1795 assert( p_dec->fmt_in.i_cat == p_dec->fmt_out.i_cat && fmt->i_cat == p_dec->fmt_in.i_cat);
1797 /* Copy ourself the input replay gain */
1798 if( fmt->i_cat == AUDIO_ES )
1800 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1802 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
1804 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
1805 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
1807 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
1809 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
1810 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
1815 /* */
1816 p_owner->cc.b_supported = ( p_sout == NULL );
1818 p_owner->cc.desc.i_608_channels = 0;
1819 p_owner->cc.desc.i_708_channels = 0;
1820 for( unsigned i = 0; i < MAX_CC_DECODERS; i++ )
1821 p_owner->cc.pp_decoder[i] = NULL;
1822 p_owner->i_ts_delay = 0;
1823 return p_dec;
1827 * Destroys a decoder object
1829 * \param p_dec the decoder object
1830 * \return nothing
1832 static void DeleteDecoder( decoder_t * p_dec )
1834 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1836 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s'",
1837 (char*)&p_dec->fmt_in.i_codec );
1839 const enum es_format_category_e i_cat =p_dec->fmt_out.i_cat;
1840 UnloadDecoder( p_dec );
1842 /* Free all packets still in the decoder fifo. */
1843 block_FifoRelease( p_owner->p_fifo );
1845 /* Cleanup */
1846 #ifdef ENABLE_SOUT
1847 if( p_owner->p_sout_input )
1849 sout_InputDelete( p_owner->p_sout_input );
1851 #endif
1853 switch( i_cat )
1855 case AUDIO_ES:
1856 if( p_owner->p_aout )
1858 /* TODO: REVISIT gap-less audio */
1859 aout_DecFlush( p_owner->p_aout, false );
1860 aout_DecDelete( p_owner->p_aout );
1861 input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1862 if( p_owner->p_input != NULL )
1863 input_SendEventAout( p_owner->p_input );
1865 break;
1866 case VIDEO_ES:
1867 if( p_owner->p_vout )
1869 /* Reset the cancel state that was set before joining the decoder
1870 * thread */
1871 vout_Cancel( p_owner->p_vout, false );
1873 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1874 0, true );
1875 if( p_owner->p_input != NULL )
1876 input_SendEventVout( p_owner->p_input );
1878 break;
1879 case SPU_ES:
1881 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1882 if( p_vout )
1884 if( p_owner->p_vout == p_vout )
1885 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1886 vlc_object_release( p_vout );
1888 break;
1890 default:
1891 vlc_assert_unreachable();
1894 es_format_Clean( &p_owner->fmt );
1896 if( p_owner->p_description )
1897 vlc_meta_Delete( p_owner->p_description );
1899 if( p_owner->p_packetizer )
1901 UnloadDecoder( p_owner->p_packetizer );
1902 vlc_object_release( p_owner->p_packetizer );
1905 vlc_cond_destroy( &p_owner->wait_timed );
1906 vlc_cond_destroy( &p_owner->wait_fifo );
1907 vlc_cond_destroy( &p_owner->wait_acknowledge );
1908 vlc_cond_destroy( &p_owner->wait_request );
1909 vlc_mutex_destroy( &p_owner->lock );
1911 vlc_object_release( p_dec );
1914 /* */
1915 static void DecoderUnsupportedCodec( decoder_t *p_dec, const es_format_t *fmt, bool b_decoding )
1917 if (fmt->i_codec != VLC_CODEC_UNKNOWN && fmt->i_codec) {
1918 const char *desc = vlc_fourcc_GetDescription(fmt->i_cat, fmt->i_codec);
1919 if (!desc || !*desc)
1920 desc = N_("No description for this codec");
1921 msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&fmt->i_codec, desc );
1922 vlc_dialog_display_error( p_dec, _("Codec not supported"),
1923 _("VLC could not decode the format \"%4.4s\" (%s)"),
1924 (char*)&fmt->i_codec, desc );
1925 } else if( b_decoding ){
1926 msg_Err( p_dec, "could not identify codec" );
1927 vlc_dialog_display_error( p_dec, _("Unidentified codec"),
1928 _("VLC could not identify the audio or video codec" ) );
1932 /* TODO: pass p_sout through p_resource? -- Courmisch */
1933 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
1934 const es_format_t *fmt, input_clock_t *p_clock,
1935 input_resource_t *p_resource,
1936 sout_instance_t *p_sout )
1938 decoder_t *p_dec = NULL;
1939 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
1940 int i_priority;
1942 /* Create the decoder configuration structure */
1943 p_dec = CreateDecoder( p_parent, p_input, fmt, p_resource, p_sout );
1944 if( p_dec == NULL )
1946 msg_Err( p_parent, "could not create %s", psz_type );
1947 vlc_dialog_display_error( p_parent, _("Streaming / Transcoding failed"),
1948 _("VLC could not open the %s module."), vlc_gettext( psz_type ) );
1949 return NULL;
1952 if( !p_dec->p_module )
1954 DecoderUnsupportedCodec( p_dec, fmt, !p_sout );
1956 DeleteDecoder( p_dec );
1957 return NULL;
1960 struct decoder_owner *p_owner = dec_get_owner( p_dec );
1961 p_owner->p_clock = p_clock;
1962 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
1964 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1965 i_priority = VLC_THREAD_PRIORITY_AUDIO;
1966 else
1967 i_priority = VLC_THREAD_PRIORITY_VIDEO;
1969 #ifdef ENABLE_SOUT
1970 /* Do not delay sout creation for SPU or DATA. */
1971 if( p_sout && fmt->b_packetized &&
1972 (fmt->i_cat != VIDEO_ES && fmt->i_cat != AUDIO_ES) )
1974 p_owner->p_sout_input = sout_InputNew( p_owner->p_sout, fmt );
1975 if( p_owner->p_sout_input == NULL )
1977 msg_Err( p_dec, "cannot create sout input (%4.4s)",
1978 (char *)&fmt->i_codec );
1979 p_owner->error = true;
1982 #endif
1984 /* Spawn the decoder thread */
1985 if( vlc_clone( &p_owner->thread, DecoderThread, p_dec, i_priority ) )
1987 msg_Err( p_dec, "cannot spawn decoder thread" );
1988 DeleteDecoder( p_dec );
1989 return NULL;
1992 return p_dec;
1997 * Spawns a new decoder thread from the input thread
1999 * \param p_input the input thread
2000 * \param p_es the es descriptor
2001 * \return the spawned decoder object
2003 decoder_t *input_DecoderNew( input_thread_t *p_input,
2004 es_format_t *fmt, input_clock_t *p_clock,
2005 sout_instance_t *p_sout )
2007 return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
2008 input_priv(p_input)->p_resource, p_sout );
2012 * Spawn a decoder thread outside of the input thread.
2014 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, const es_format_t *fmt,
2015 input_resource_t *p_resource )
2017 return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
2022 * Kills a decoder thread and waits until it's finished
2024 * \param p_input the input thread
2025 * \param p_es the es descriptor
2026 * \return nothing
2028 void input_DecoderDelete( decoder_t *p_dec )
2030 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2032 vlc_cancel( p_owner->thread );
2034 vlc_fifo_Lock( p_owner->p_fifo );
2035 /* Signal DecoderTimedWait */
2036 p_owner->flushing = true;
2037 vlc_cond_signal( &p_owner->wait_timed );
2038 vlc_fifo_Unlock( p_owner->p_fifo );
2040 /* Make sure we aren't waiting/decoding anymore */
2041 vlc_mutex_lock( &p_owner->lock );
2042 p_owner->b_waiting = false;
2043 vlc_cond_signal( &p_owner->wait_request );
2045 /* If the video output is paused or slow, or if the picture pool size was
2046 * under-estimated (e.g. greedy video filter, buggy decoder...), the
2047 * the picture pool may be empty, and the decoder thread or any decoder
2048 * module worker threads may be stuck waiting for free picture buffers.
2050 * This unblocks the thread, allowing the decoder module to join all its
2051 * worker threads (if any) and the decoder thread to terminate. */
2052 if( p_dec->fmt_out.i_cat == VIDEO_ES && p_owner->p_vout != NULL )
2053 vout_Cancel( p_owner->p_vout, true );
2054 vlc_mutex_unlock( &p_owner->lock );
2056 vlc_join( p_owner->thread, NULL );
2058 /* */
2059 if( p_owner->cc.b_supported )
2061 for( int i = 0; i < MAX_CC_DECODERS; i++ )
2062 input_DecoderSetCcState( p_dec, VLC_CODEC_CEA608, i, false );
2065 /* Delete decoder */
2066 DeleteDecoder( p_dec );
2070 * Put a block_t in the decoder's fifo.
2071 * Thread-safe w.r.t. the decoder. May be a cancellation point.
2073 * \param p_dec the decoder object
2074 * \param p_block the data block
2076 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
2078 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2080 vlc_fifo_Lock( p_owner->p_fifo );
2081 if( !b_do_pace )
2083 /* FIXME: ideally we would check the time amount of data
2084 * in the FIFO instead of its size. */
2085 /* 400 MiB, i.e. ~ 50mb/s for 60s */
2086 if( vlc_fifo_GetBytes( p_owner->p_fifo ) > 400*1024*1024 )
2088 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
2089 "consumed quickly enough), resetting fifo!" );
2090 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2091 p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
2094 else
2095 if( !p_owner->b_waiting )
2096 { /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
2097 * Locking is not necessary as b_waiting is only read, not written by
2098 * the decoder thread. */
2099 while( vlc_fifo_GetCount( p_owner->p_fifo ) >= 10 )
2100 vlc_fifo_WaitCond( p_owner->p_fifo, &p_owner->wait_fifo );
2103 vlc_fifo_QueueUnlocked( p_owner->p_fifo, p_block );
2104 vlc_fifo_Unlock( p_owner->p_fifo );
2107 bool input_DecoderIsEmpty( decoder_t * p_dec )
2109 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2111 assert( !p_owner->b_waiting );
2113 vlc_fifo_Lock( p_owner->p_fifo );
2114 if( !vlc_fifo_IsEmpty( p_owner->p_fifo ) || p_owner->b_draining )
2116 vlc_fifo_Unlock( p_owner->p_fifo );
2117 return false;
2119 vlc_fifo_Unlock( p_owner->p_fifo );
2121 bool b_empty;
2123 vlc_mutex_lock( &p_owner->lock );
2124 #ifdef ENABLE_SOUT
2125 if( p_owner->p_sout_input != NULL )
2126 b_empty = sout_InputIsEmpty( p_owner->p_sout_input );
2127 else
2128 #endif
2129 if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->p_vout != NULL )
2130 b_empty = vout_IsEmpty( p_owner->p_vout );
2131 else if( p_owner->fmt.i_cat == AUDIO_ES )
2132 b_empty = !p_owner->b_draining || p_owner->drained;
2133 else
2134 b_empty = true; /* TODO subtitles support */
2135 vlc_mutex_unlock( &p_owner->lock );
2137 return b_empty;
2141 * Signals that there are no further blocks to decode, and requests that the
2142 * decoder drain all pending buffers. This is used to ensure that all
2143 * intermediate buffers empty and no samples get lost at the end of the stream.
2145 * @note The function does not actually wait for draining. It just signals that
2146 * draining should be performed once the decoder has emptied FIFO.
2148 void input_DecoderDrain( decoder_t *p_dec )
2150 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2152 vlc_fifo_Lock( p_owner->p_fifo );
2153 p_owner->b_draining = true;
2154 vlc_fifo_Signal( p_owner->p_fifo );
2155 vlc_fifo_Unlock( p_owner->p_fifo );
2159 * Requests that the decoder immediately discard all pending buffers.
2160 * This is useful when seeking or when deselecting a stream.
2162 void input_DecoderFlush( decoder_t *p_dec )
2164 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2166 vlc_fifo_Lock( p_owner->p_fifo );
2168 /* Empty the fifo */
2169 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2171 /* Don't need to wait for the DecoderThread to flush. Indeed, if called a
2172 * second time, this function will clear the FIFO again before anything was
2173 * dequeued by DecoderThread and there is no need to flush a second time in
2174 * a row. */
2175 p_owner->flushing = true;
2177 /* Flush video/spu decoder when paused: increment frames_countdown in order
2178 * to display one frame/subtitle */
2179 if( p_owner->paused
2180 && ( p_owner->fmt.i_cat == VIDEO_ES || p_owner->fmt.i_cat == SPU_ES )
2181 && p_owner->frames_countdown == 0 )
2182 p_owner->frames_countdown++;
2184 vlc_fifo_Signal( p_owner->p_fifo );
2185 vlc_cond_signal( &p_owner->wait_timed );
2187 vlc_fifo_Unlock( p_owner->p_fifo );
2190 void input_DecoderGetCcDesc( decoder_t *p_dec, decoder_cc_desc_t *p_desc )
2192 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2194 vlc_mutex_lock( &p_owner->lock );
2195 *p_desc = p_owner->cc.desc;
2196 vlc_mutex_unlock( &p_owner->lock );
2199 static bool input_DecoderHasCCChanFlag( decoder_t *p_dec,
2200 vlc_fourcc_t codec, int i_channel )
2202 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2204 int i_max_channels;
2205 uint64_t i_bitmap;
2206 if( codec == VLC_CODEC_CEA608 )
2208 i_max_channels = 4;
2209 i_bitmap = p_owner->cc.desc.i_608_channels;
2211 else if( codec == VLC_CODEC_CEA708 )
2213 i_max_channels = 64;
2214 i_bitmap = p_owner->cc.desc.i_708_channels;
2216 else return false;
2218 return ( i_channel >= 0 && i_channel < i_max_channels &&
2219 ( i_bitmap & ((uint64_t)1 << i_channel) ) );
2222 int input_DecoderSetCcState( decoder_t *p_dec, vlc_fourcc_t codec,
2223 int i_channel, bool b_decode )
2225 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2227 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%x", b_decode, i_channel );
2229 if( !input_DecoderHasCCChanFlag( p_dec, codec, i_channel ) )
2230 return VLC_EGENERIC;
2232 if( b_decode )
2234 decoder_t *p_cc;
2235 es_format_t fmt;
2237 es_format_Init( &fmt, SPU_ES, codec );
2238 fmt.subs.cc.i_channel = i_channel;
2239 fmt.subs.cc.i_reorder_depth = p_owner->cc.desc.i_reorder_depth;
2240 p_cc = input_DecoderNew( p_owner->p_input, &fmt,
2241 p_owner->p_clock, p_owner->p_sout );
2242 if( !p_cc )
2244 msg_Err( p_dec, "could not create decoder" );
2245 vlc_dialog_display_error( p_dec,
2246 _("Streaming / Transcoding failed"), "%s",
2247 _("VLC could not open the decoder module.") );
2248 return VLC_EGENERIC;
2250 else if( !p_cc->p_module )
2252 DecoderUnsupportedCodec( p_dec, &fmt, true );
2253 input_DecoderDelete(p_cc);
2254 return VLC_EGENERIC;
2256 struct decoder_owner *p_ccowner = dec_get_owner( p_cc );
2257 p_ccowner->p_clock = p_owner->p_clock;
2259 vlc_mutex_lock( &p_owner->lock );
2260 p_owner->cc.pp_decoder[i_channel] = p_cc;
2261 vlc_mutex_unlock( &p_owner->lock );
2263 else
2265 decoder_t *p_cc;
2267 vlc_mutex_lock( &p_owner->lock );
2268 p_cc = p_owner->cc.pp_decoder[i_channel];
2269 p_owner->cc.pp_decoder[i_channel] = NULL;
2270 vlc_mutex_unlock( &p_owner->lock );
2272 if( p_cc )
2273 input_DecoderDelete(p_cc);
2275 return VLC_SUCCESS;
2278 int input_DecoderGetCcState( decoder_t *p_dec, vlc_fourcc_t codec,
2279 int i_channel, bool *pb_decode )
2281 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2283 if( !input_DecoderHasCCChanFlag( p_dec, codec, i_channel ) )
2284 return VLC_EGENERIC;
2286 vlc_mutex_lock( &p_owner->lock );
2287 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
2288 vlc_mutex_unlock( &p_owner->lock );
2289 return VLC_SUCCESS;
2292 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
2294 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2296 /* Normally, p_owner->b_paused != b_paused here. But if a track is added
2297 * while the input is paused (e.g. add sub file), then b_paused is
2298 * (incorrectly) false. FIXME: This is a bug in the decoder owner. */
2299 vlc_fifo_Lock( p_owner->p_fifo );
2300 p_owner->paused = b_paused;
2301 p_owner->pause_date = i_date;
2302 p_owner->frames_countdown = 0;
2303 vlc_fifo_Signal( p_owner->p_fifo );
2304 vlc_fifo_Unlock( p_owner->p_fifo );
2307 void input_DecoderChangeRate( decoder_t *dec, float rate )
2309 struct decoder_owner *owner = dec_get_owner( dec );
2311 vlc_fifo_Lock( owner->p_fifo );
2312 owner->rate = rate;
2313 vlc_fifo_Signal( owner->p_fifo );
2314 vlc_fifo_Unlock( owner->p_fifo );
2317 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
2319 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2321 vlc_mutex_lock( &p_owner->lock );
2322 p_owner->i_ts_delay = i_delay;
2323 vlc_mutex_unlock( &p_owner->lock );
2326 void input_DecoderStartWait( decoder_t *p_dec )
2328 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2330 assert( !p_owner->b_waiting );
2332 vlc_mutex_lock( &p_owner->lock );
2333 p_owner->b_first = true;
2334 p_owner->b_has_data = false;
2335 p_owner->b_waiting = true;
2336 vlc_cond_signal( &p_owner->wait_request );
2337 vlc_mutex_unlock( &p_owner->lock );
2340 void input_DecoderStopWait( decoder_t *p_dec )
2342 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2344 assert( p_owner->b_waiting );
2346 vlc_mutex_lock( &p_owner->lock );
2347 p_owner->b_waiting = false;
2348 vlc_cond_signal( &p_owner->wait_request );
2349 vlc_mutex_unlock( &p_owner->lock );
2352 void input_DecoderWait( decoder_t *p_dec )
2354 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2356 assert( p_owner->b_waiting );
2358 vlc_mutex_lock( &p_owner->lock );
2359 while( !p_owner->b_has_data )
2361 /* Don't need to lock p_owner->paused since it's only modified by the
2362 * owner */
2363 if( p_owner->paused )
2364 break;
2365 vlc_fifo_Lock( p_owner->p_fifo );
2366 if( p_owner->b_idle && vlc_fifo_IsEmpty( p_owner->p_fifo ) )
2368 msg_Err( p_dec, "buffer deadlock prevented" );
2369 vlc_fifo_Unlock( p_owner->p_fifo );
2370 break;
2372 vlc_fifo_Unlock( p_owner->p_fifo );
2373 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
2375 vlc_mutex_unlock( &p_owner->lock );
2378 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
2380 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2382 assert( p_owner->paused );
2383 *pi_duration = 0;
2385 vlc_fifo_Lock( p_owner->p_fifo );
2386 p_owner->frames_countdown++;
2387 vlc_fifo_Signal( p_owner->p_fifo );
2388 vlc_fifo_Unlock( p_owner->p_fifo );
2390 vlc_mutex_lock( &p_owner->lock );
2391 if( p_owner->fmt.i_cat == VIDEO_ES )
2393 if( p_owner->p_vout )
2394 vout_NextPicture( p_owner->p_vout, pi_duration );
2396 vlc_mutex_unlock( &p_owner->lock );
2399 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
2401 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2402 bool b_changed;
2404 vlc_mutex_lock( &p_owner->lock );
2405 b_changed = p_owner->b_fmt_description;
2406 if( b_changed )
2408 if( p_fmt != NULL )
2409 es_format_Copy( p_fmt, &p_owner->fmt );
2411 if( pp_meta )
2413 *pp_meta = NULL;
2414 if( p_owner->p_description )
2416 *pp_meta = vlc_meta_New();
2417 if( *pp_meta )
2418 vlc_meta_Merge( *pp_meta, p_owner->p_description );
2421 p_owner->b_fmt_description = false;
2423 vlc_mutex_unlock( &p_owner->lock );
2424 return b_changed;
2427 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
2429 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2431 return block_FifoSize( p_owner->p_fifo );
2434 void input_DecoderGetObjects( decoder_t *p_dec,
2435 vout_thread_t **pp_vout, audio_output_t **pp_aout )
2437 struct decoder_owner *p_owner = dec_get_owner( p_dec );
2439 vlc_mutex_lock( &p_owner->lock );
2440 if( pp_vout )
2441 *pp_vout = p_dec->fmt_out.i_cat == VIDEO_ES && p_owner->p_vout ?
2442 vlc_object_hold( p_owner->p_vout ) : NULL;
2443 if( pp_aout )
2444 *pp_aout = p_dec->fmt_out.i_cat == AUDIO_ES && p_owner->p_aout ?
2445 vlc_object_hold( p_owner->p_aout ) : NULL;
2446 vlc_mutex_unlock( &p_owner->lock );