chroma: cvpx: always use cached copy
[vlc.git] / src / input / decoder.c
blob98817e6f397adef49d55748260d580f53e294b38
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.h"
50 #include "decoder.h"
51 #include "event.h"
52 #include "resource.h"
54 #include "../video_output/vout_control.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_sys_t
68 input_thread_t *p_input;
69 input_resource_t*p_resource;
70 input_clock_t *p_clock;
71 int i_last_rate;
73 vout_thread_t *p_spu_vout;
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)( decoder_owner_sys_t *, 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 int64_t i_preroll_end;
114 /* Pause */
115 mtime_t pause_date;
116 unsigned frames_countdown;
117 bool paused;
119 bool error;
121 /* Waiting */
122 bool b_waiting;
123 bool b_first;
124 bool b_has_data;
126 /* Flushing */
127 bool flushing;
128 bool b_draining;
129 atomic_bool drained;
130 bool b_idle;
132 /* CC */
133 #define MAX_CC_DECODERS 64 /* The es_out only creates one type of es */
134 struct
136 bool b_supported;
137 decoder_cc_desc_t desc;
138 decoder_t *pp_decoder[MAX_CC_DECODERS];
139 } cc;
141 /* Delay */
142 mtime_t i_ts_delay;
145 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
146 * a bogus PTS and won't be displayed */
147 #define DECODER_BOGUS_VIDEO_DELAY ((mtime_t)(DEFAULT_PTS_DELAY * 30))
149 /* */
150 #define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))
151 #define BLOCK_FLAG_CORE_PRIVATE_RELOADED (1 << BLOCK_FLAG_CORE_PRIVATE_SHIFT)
154 * Load a decoder module
156 static int LoadDecoder( decoder_t *p_dec, bool b_packetizer,
157 const es_format_t *restrict p_fmt )
159 p_dec->b_frame_drop_allowed = true;
160 p_dec->i_extra_picture_buffers = 0;
162 p_dec->pf_decode = NULL;
163 p_dec->pf_get_cc = NULL;
164 p_dec->pf_packetize = NULL;
165 p_dec->pf_flush = NULL;
167 es_format_Copy( &p_dec->fmt_in, p_fmt );
168 es_format_Init( &p_dec->fmt_out, p_fmt->i_cat, 0 );
170 /* Find a suitable decoder/packetizer module */
171 if( !b_packetizer )
173 static const char caps[ES_CATEGORY_COUNT][16] = {
174 [VIDEO_ES] = "video decoder",
175 [AUDIO_ES] = "audio decoder",
176 [SPU_ES] = "spu decoder",
178 p_dec->p_module = module_need_var( p_dec, caps[p_dec->fmt_in.i_cat],
179 "codec" );
181 else
182 p_dec->p_module = module_need_var( p_dec, "packetizer", "packetizer" );
184 if( !p_dec->p_module )
186 es_format_Clean( &p_dec->fmt_in );
187 return -1;
189 else
190 return 0;
194 * Unload a decoder module
196 static void UnloadDecoder( decoder_t *p_dec )
198 if( p_dec->p_module )
200 module_unneed( p_dec, p_dec->p_module );
201 p_dec->p_module = NULL;
204 if( p_dec->p_description )
206 vlc_meta_Delete( p_dec->p_description );
207 p_dec->p_description = NULL;
210 es_format_Clean( &p_dec->fmt_in );
211 es_format_Clean( &p_dec->fmt_out );
214 static int ReloadDecoder( decoder_t *p_dec, bool b_packetizer,
215 const es_format_t *restrict p_fmt, enum reload reload )
217 /* Copy p_fmt since it can be destroyed by UnloadDecoder */
218 es_format_t fmt_in;
219 if( es_format_Copy( &fmt_in, p_fmt ) != VLC_SUCCESS )
221 p_dec->p_owner->error = true;
222 return VLC_EGENERIC;
225 /* Restart the decoder module */
226 UnloadDecoder( p_dec );
227 p_dec->p_owner->error = false;
229 if( reload == RELOAD_DECODER_AOUT )
231 decoder_owner_sys_t *p_owner = p_dec->p_owner;
232 assert( p_owner->fmt.i_cat == AUDIO_ES );
233 audio_output_t *p_aout = p_owner->p_aout;
235 vlc_mutex_lock( &p_owner->lock );
236 p_owner->p_aout = NULL;
237 vlc_mutex_unlock( &p_owner->lock );
238 if( p_aout )
240 aout_DecDelete( p_aout );
241 input_resource_PutAout( p_owner->p_resource, p_aout );
245 if( LoadDecoder( p_dec, b_packetizer, &fmt_in ) )
247 p_dec->p_owner->error = true;
248 es_format_Clean( &fmt_in );
249 return VLC_EGENERIC;
251 es_format_Clean( &fmt_in );
252 return VLC_SUCCESS;
255 static void DecoderUpdateFormatLocked( decoder_t *p_dec )
257 decoder_owner_sys_t *p_owner = p_dec->p_owner;
259 vlc_assert_locked( &p_owner->lock );
261 es_format_Clean( &p_owner->fmt );
262 es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
264 /* Move p_description */
265 if( p_dec->p_description != NULL )
267 if( p_owner->p_description != NULL )
268 vlc_meta_Delete( p_owner->p_description );
269 p_owner->p_description = p_dec->p_description;
270 p_dec->p_description = NULL;
273 p_owner->b_fmt_description = true;
276 /*****************************************************************************
277 * Buffers allocation callbacks for the decoders
278 *****************************************************************************/
279 static vout_thread_t *aout_request_vout( void *p_private,
280 vout_thread_t *p_vout,
281 const video_format_t *p_fmt, bool b_recyle )
283 decoder_t *p_dec = p_private;
284 decoder_owner_sys_t *p_owner = p_dec->p_owner;
285 input_thread_t *p_input = p_owner->p_input;
287 p_vout = input_resource_RequestVout( p_owner->p_resource, p_vout, p_fmt, 1,
288 b_recyle );
289 if( p_input != NULL )
290 input_SendEventVout( p_input );
292 return p_vout;
295 static bool aout_replaygain_changed( const audio_replay_gain_t *a,
296 const audio_replay_gain_t *b )
298 for( size_t i=0; i<AUDIO_REPLAY_GAIN_MAX; i++ )
300 if( a->pb_gain[i] != b->pb_gain[i] ||
301 a->pb_peak[i] != b->pb_peak[i] ||
302 a->pb_gain[i] != b->pb_gain[i] ||
303 a->pb_peak[i] != b->pb_peak[i] )
304 return true;
306 return false;
309 static int aout_update_format( decoder_t *p_dec )
311 decoder_owner_sys_t *p_owner = p_dec->p_owner;
313 if( p_owner->p_aout &&
314 ( !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->fmt.audio) ||
315 p_dec->fmt_out.i_codec != p_dec->fmt_out.audio.i_format ||
316 p_dec->fmt_out.i_profile != p_owner->fmt.i_profile ) )
318 audio_output_t *p_aout = p_owner->p_aout;
320 /* Parameters changed, restart the aout */
321 vlc_mutex_lock( &p_owner->lock );
322 p_owner->p_aout = NULL;
323 vlc_mutex_unlock( &p_owner->lock );
324 aout_DecDelete( p_aout );
326 input_resource_PutAout( p_owner->p_resource, p_aout );
329 /* Check if only replay gain has changed */
330 if( aout_replaygain_changed( &p_dec->fmt_in.audio_replay_gain,
331 &p_owner->fmt.audio_replay_gain ) )
333 p_dec->fmt_out.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
334 if( p_owner->p_aout )
336 p_owner->fmt.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
337 var_TriggerCallback( p_owner->p_aout, "audio-replay-gain-mode" );
341 if( p_owner->p_aout == NULL )
343 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
345 audio_sample_format_t format = p_dec->fmt_out.audio;
346 aout_FormatPrepare( &format );
348 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
349 if( i_force_dolby &&
350 format.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
352 if( i_force_dolby == 1 )
353 format.i_chan_mode |= AOUT_CHANMODE_DOLBYSTEREO;
354 else /* i_force_dolby == 2 */
355 format.i_chan_mode &= ~AOUT_CHANMODE_DOLBYSTEREO;
358 aout_request_vout_t request_vout = {
359 .pf_request_vout = aout_request_vout,
360 .p_private = p_dec,
362 audio_output_t *p_aout;
364 p_aout = input_resource_GetAout( p_owner->p_resource );
365 if( p_aout )
367 /* TODO: 3.0 HACK: we need to put i_profile inside audio_format_t
368 * for 4.0 */
369 if( p_dec->fmt_out.i_codec == VLC_CODEC_DTS )
370 var_SetBool( p_aout, "dtshd", p_dec->fmt_out.i_profile > 0 );
372 if( aout_DecNew( p_aout, &format,
373 &p_dec->fmt_out.audio_replay_gain,
374 &request_vout ) )
376 input_resource_PutAout( p_owner->p_resource, p_aout );
377 p_aout = NULL;
381 vlc_mutex_lock( &p_owner->lock );
382 p_owner->p_aout = p_aout;
384 DecoderUpdateFormatLocked( p_dec );
385 aout_FormatPrepare( &p_owner->fmt.audio );
386 vlc_mutex_unlock( &p_owner->lock );
388 if( p_owner->p_input != NULL )
389 input_SendEventAout( p_owner->p_input );
391 if( p_aout == NULL )
393 msg_Err( p_dec, "failed to create audio output" );
394 return -1;
397 p_dec->fmt_out.audio.i_bytes_per_frame =
398 p_owner->fmt.audio.i_bytes_per_frame;
399 p_dec->fmt_out.audio.i_frame_length =
400 p_owner->fmt.audio.i_frame_length;
402 return 0;
405 static int vout_update_format( decoder_t *p_dec )
407 decoder_owner_sys_t *p_owner = p_dec->p_owner;
409 if( p_owner->p_vout == NULL
410 || p_dec->fmt_out.video.i_width != p_owner->fmt.video.i_width
411 || p_dec->fmt_out.video.i_height != p_owner->fmt.video.i_height
412 || p_dec->fmt_out.video.i_visible_width != p_owner->fmt.video.i_visible_width
413 || p_dec->fmt_out.video.i_visible_height != p_owner->fmt.video.i_visible_height
414 || p_dec->fmt_out.video.i_x_offset != p_owner->fmt.video.i_x_offset
415 || p_dec->fmt_out.video.i_y_offset != p_owner->fmt.video.i_y_offset
416 || p_dec->fmt_out.i_codec != p_owner->fmt.video.i_chroma
417 || (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->fmt.video.i_sar_den !=
418 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->fmt.video.i_sar_num ||
419 p_dec->fmt_out.video.orientation != p_owner->fmt.video.orientation ||
420 p_dec->fmt_out.video.multiview_mode != p_owner->fmt.video.multiview_mode )
422 vout_thread_t *p_vout;
424 if( !p_dec->fmt_out.video.i_width ||
425 !p_dec->fmt_out.video.i_height ||
426 p_dec->fmt_out.video.i_width < p_dec->fmt_out.video.i_visible_width ||
427 p_dec->fmt_out.video.i_height < p_dec->fmt_out.video.i_visible_height )
429 /* Can't create a new vout without display size */
430 return -1;
433 video_format_t fmt = p_dec->fmt_out.video;
434 fmt.i_chroma = p_dec->fmt_out.i_codec;
436 if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
438 const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
439 for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
441 while( fmt.i_width % dsc->p[i].w.den )
442 fmt.i_width++;
443 while( fmt.i_height % dsc->p[i].h.den )
444 fmt.i_height++;
448 if( !fmt.i_visible_width || !fmt.i_visible_height )
450 if( p_dec->fmt_in.video.i_visible_width &&
451 p_dec->fmt_in.video.i_visible_height )
453 fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
454 fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
455 fmt.i_x_offset = p_dec->fmt_in.video.i_x_offset;
456 fmt.i_y_offset = p_dec->fmt_in.video.i_y_offset;
458 else
460 fmt.i_visible_width = fmt.i_width;
461 fmt.i_visible_height = fmt.i_height;
462 fmt.i_x_offset = 0;
463 fmt.i_y_offset = 0;
467 if( fmt.i_visible_height == 1088 &&
468 var_CreateGetBool( p_dec, "hdtv-fix" ) )
470 fmt.i_visible_height = 1080;
471 if( !(fmt.i_sar_num % 136))
473 fmt.i_sar_num *= 135;
474 fmt.i_sar_den *= 136;
476 msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
479 if( !fmt.i_sar_num || !fmt.i_sar_den )
481 fmt.i_sar_num = 1;
482 fmt.i_sar_den = 1;
485 vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
486 fmt.i_sar_num, fmt.i_sar_den, 50000 );
488 video_format_AdjustColorSpace( &fmt );
490 vlc_mutex_lock( &p_owner->lock );
492 p_vout = p_owner->p_vout;
493 p_owner->p_vout = NULL;
494 vlc_mutex_unlock( &p_owner->lock );
496 unsigned dpb_size;
497 switch( p_dec->fmt_in.i_codec )
499 case VLC_CODEC_HEVC:
500 case VLC_CODEC_H264:
501 case VLC_CODEC_DIRAC: /* FIXME valid ? */
502 dpb_size = 18;
503 break;
504 case VLC_CODEC_VP5:
505 case VLC_CODEC_VP6:
506 case VLC_CODEC_VP6F:
507 case VLC_CODEC_VP8:
508 dpb_size = 3;
509 break;
510 default:
511 dpb_size = 2;
512 break;
514 p_vout = input_resource_RequestVout( p_owner->p_resource,
515 p_vout, &fmt,
516 dpb_size +
517 p_dec->i_extra_picture_buffers + 1,
518 true );
519 vlc_mutex_lock( &p_owner->lock );
520 p_owner->p_vout = p_vout;
522 DecoderUpdateFormatLocked( p_dec );
523 p_owner->fmt.video.i_chroma = p_dec->fmt_out.i_codec;
524 vlc_mutex_unlock( &p_owner->lock );
526 if( p_owner->p_input != NULL )
527 input_SendEventVout( p_owner->p_input );
528 if( p_vout == NULL )
530 msg_Err( p_dec, "failed to create video output" );
531 return -1;
535 if ( memcmp( &p_dec->fmt_out.video.mastering,
536 &p_owner->fmt.video.mastering,
537 sizeof(p_owner->fmt.video.mastering)) ||
538 p_dec->fmt_out.video.lighting.MaxCLL !=
539 p_owner->fmt.video.lighting.MaxCLL ||
540 p_dec->fmt_out.video.lighting.MaxFALL !=
541 p_owner->fmt.video.lighting.MaxFALL)
543 /* the format has changed but we don't need a new vout */
544 vlc_mutex_lock( &p_owner->lock );
545 DecoderUpdateFormatLocked( p_dec );
546 vlc_mutex_unlock( &p_owner->lock );
548 return 0;
551 static picture_t *vout_new_buffer( decoder_t *p_dec )
553 decoder_owner_sys_t *p_owner = p_dec->p_owner;
554 assert( p_owner->p_vout );
556 return vout_GetPicture( p_owner->p_vout );
559 static subpicture_t *spu_new_buffer( decoder_t *p_dec,
560 const subpicture_updater_t *p_updater )
562 decoder_owner_sys_t *p_owner = p_dec->p_owner;
563 vout_thread_t *p_vout = NULL;
564 subpicture_t *p_subpic;
565 int i_attempts = 30;
567 while( i_attempts-- )
569 if( p_owner->error )
570 break;
572 p_vout = input_resource_HoldVout( p_owner->p_resource );
573 if( p_vout )
574 break;
576 msleep( DECODER_SPU_VOUT_WAIT_DURATION );
579 if( !p_vout )
581 msg_Warn( p_dec, "no vout found, dropping subpicture" );
582 return NULL;
585 if( p_owner->p_spu_vout != p_vout )
587 p_owner->i_spu_channel = vout_RegisterSubpictureChannel( p_vout );
588 p_owner->i_spu_order = 0;
589 p_owner->p_spu_vout = p_vout;
592 p_subpic = subpicture_New( p_updater );
593 if( p_subpic )
595 p_subpic->i_channel = p_owner->i_spu_channel;
596 p_subpic->i_order = p_owner->i_spu_order++;
597 p_subpic->b_subtitle = true;
600 vlc_object_release( p_vout );
602 return p_subpic;
605 static int DecoderGetInputAttachments( decoder_t *p_dec,
606 input_attachment_t ***ppp_attachment,
607 int *pi_attachment )
609 input_thread_t *p_input = p_dec->p_owner->p_input;
611 if( unlikely(p_input == NULL) )
612 return VLC_ENOOBJ;
613 return input_Control( p_input, INPUT_GET_ATTACHMENTS,
614 ppp_attachment, pi_attachment );
617 static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
619 decoder_owner_sys_t *p_owner = p_dec->p_owner;
621 vlc_mutex_lock( &p_owner->lock );
622 if( p_owner->b_waiting )
623 i_ts = VLC_TS_INVALID;
624 vlc_mutex_unlock( &p_owner->lock );
626 if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
627 return i_ts;
629 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) ) {
630 msg_Err(p_dec, "Could not get display date for timestamp %"PRId64"", i_ts);
631 return VLC_TS_INVALID;
634 return i_ts;
637 static int DecoderGetDisplayRate( decoder_t *p_dec )
639 decoder_owner_sys_t *p_owner = p_dec->p_owner;
641 if( !p_owner->p_clock )
642 return INPUT_RATE_DEFAULT;
643 return input_clock_GetRate( p_owner->p_clock );
646 /*****************************************************************************
647 * Public functions
648 *****************************************************************************/
649 block_t *decoder_NewAudioBuffer( decoder_t *dec, int samples )
651 assert( dec->fmt_out.audio.i_frame_length > 0
652 && dec->fmt_out.audio.i_bytes_per_frame > 0 );
654 size_t length = samples * dec->fmt_out.audio.i_bytes_per_frame
655 / dec->fmt_out.audio.i_frame_length;
656 block_t *block = block_Alloc( length );
657 if( likely(block != NULL) )
659 block->i_nb_samples = samples;
660 block->i_pts = block->i_length = 0;
662 return block;
665 subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder,
666 const subpicture_updater_t *p_dyn )
668 subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder, p_dyn );
669 if( !p_subpicture )
670 msg_Warn( p_decoder, "can't get output subpicture" );
671 return p_subpicture;
674 static void RequestReload( decoder_t * p_dec )
676 decoder_owner_sys_t *p_owner = p_dec->p_owner;
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 /* decoder_GetInputAttachments:
684 int decoder_GetInputAttachments( decoder_t *p_dec,
685 input_attachment_t ***ppp_attachment,
686 int *pi_attachment )
688 if( !p_dec->pf_get_attachments )
689 return VLC_EGENERIC;
691 return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
693 /* decoder_GetDisplayDate:
695 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
697 if( !p_dec->pf_get_display_date )
698 return VLC_TS_INVALID;
700 return p_dec->pf_get_display_date( p_dec, i_ts );
702 /* decoder_GetDisplayRate:
704 int decoder_GetDisplayRate( decoder_t *p_dec )
706 if( !p_dec->pf_get_display_rate )
707 return INPUT_RATE_DEFAULT;
709 return p_dec->pf_get_display_rate( p_dec );
712 void decoder_AbortPictures( decoder_t *p_dec, bool b_abort )
714 decoder_owner_sys_t *p_owner = p_dec->p_owner;
716 vlc_mutex_lock( &p_owner->lock );
717 if( p_owner->p_vout != NULL )
718 vout_Cancel( p_owner->p_vout, b_abort );
719 vlc_mutex_unlock( &p_owner->lock );
722 static void DecoderWaitUnblock( decoder_t *p_dec )
724 decoder_owner_sys_t *p_owner = p_dec->p_owner;
726 vlc_assert_locked( &p_owner->lock );
728 for( ;; )
730 if( !p_owner->b_waiting || !p_owner->b_has_data )
731 break;
732 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
736 /* DecoderTimedWait: Interruptible wait
737 * Returns VLC_SUCCESS if wait was not interrupted, and VLC_EGENERIC otherwise */
738 static int DecoderTimedWait( decoder_t *p_dec, mtime_t deadline )
740 decoder_owner_sys_t *p_owner = p_dec->p_owner;
742 if (deadline - mdate() <= 0)
743 return VLC_SUCCESS;
745 vlc_fifo_Lock( p_owner->p_fifo );
746 while( !p_owner->flushing
747 && vlc_fifo_TimedWaitCond( p_owner->p_fifo, &p_owner->wait_timed,
748 deadline ) == 0 );
749 int ret = p_owner->flushing ? VLC_EGENERIC : VLC_SUCCESS;
750 vlc_fifo_Unlock( p_owner->p_fifo );
751 return ret;
754 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
756 if( p->i_flags & BLOCK_FLAG_PREROLL )
757 *pi_preroll = INT64_MAX;
758 /* Check if we can use the packet for end of preroll */
759 else if( (p->i_flags & BLOCK_FLAG_DISCONTINUITY) &&
760 (p->i_buffer == 0 || (p->i_flags & BLOCK_FLAG_CORRUPTED)) )
761 *pi_preroll = INT64_MAX;
762 else if( p->i_dts > VLC_TS_INVALID )
763 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
764 else if( p->i_pts > VLC_TS_INVALID )
765 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
768 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
769 mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound )
771 decoder_owner_sys_t *p_owner = p_dec->p_owner;
772 input_clock_t *p_clock = p_owner->p_clock;
774 vlc_assert_locked( &p_owner->lock );
776 const mtime_t i_es_delay = p_owner->i_ts_delay;
778 if( !p_clock )
779 return;
781 const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
782 int i_rate;
784 if( *pi_ts0 > VLC_TS_INVALID )
786 *pi_ts0 += i_es_delay;
787 if( pi_ts1 && *pi_ts1 > VLC_TS_INVALID )
788 *pi_ts1 += i_es_delay;
789 if( i_ts_bound != INT64_MAX )
790 i_ts_bound += i_es_delay;
791 if( input_clock_ConvertTS( VLC_OBJECT(p_dec), p_clock, &i_rate, pi_ts0, pi_ts1, i_ts_bound ) ) {
792 const char *psz_name = module_get_name( p_dec->p_module, false );
793 if( pi_ts1 != NULL )
794 msg_Err(p_dec, "Could not convert timestamps %"PRId64
795 ", %"PRId64" for %s", *pi_ts0, *pi_ts1, psz_name );
796 else
797 msg_Err(p_dec, "Could not convert timestamp %"PRId64" for %s", *pi_ts0, psz_name );
798 *pi_ts0 = VLC_TS_INVALID;
801 else
803 i_rate = input_clock_GetRate( p_clock );
806 /* Do not create ephemere data because of rounding errors */
807 if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
808 *pi_ts1 += 1;
810 if( pi_duration )
811 *pi_duration = ( *pi_duration * i_rate + INPUT_RATE_DEFAULT-1 )
812 / INPUT_RATE_DEFAULT;
814 if( pi_rate )
815 *pi_rate = i_rate;
818 #ifdef ENABLE_SOUT
819 static int DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
821 decoder_owner_sys_t *p_owner = p_dec->p_owner;
823 assert( p_owner->p_clock );
824 assert( !p_sout_block->p_next );
826 vlc_mutex_lock( &p_owner->lock );
828 if( p_owner->b_waiting )
830 p_owner->b_has_data = true;
831 vlc_cond_signal( &p_owner->wait_acknowledge );
834 DecoderWaitUnblock( p_dec );
835 DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts,
836 &p_sout_block->i_length, NULL, INT64_MAX );
838 vlc_mutex_unlock( &p_owner->lock );
840 /* FIXME --VLC_TS_INVALID inspect stream_output*/
841 return sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
844 /* This function process a block for sout
846 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
848 decoder_owner_sys_t *p_owner = p_dec->p_owner;
849 block_t *p_sout_block;
850 block_t **pp_block = p_block ? &p_block : NULL;
852 while( ( p_sout_block =
853 p_dec->pf_packetize( p_dec, pp_block ) ) )
855 if( p_owner->p_sout_input == NULL )
857 vlc_mutex_lock( &p_owner->lock );
858 DecoderUpdateFormatLocked( p_dec );
860 p_owner->fmt.i_group = p_dec->fmt_in.i_group;
861 p_owner->fmt.i_id = p_dec->fmt_in.i_id;
862 if( p_dec->fmt_in.psz_language )
864 free( p_owner->fmt.psz_language );
865 p_owner->fmt.psz_language =
866 strdup( p_dec->fmt_in.psz_language );
868 vlc_mutex_unlock( &p_owner->lock );
870 p_owner->p_sout_input =
871 sout_InputNew( p_owner->p_sout, &p_owner->fmt );
873 if( p_owner->p_sout_input == NULL )
875 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
876 (char *)&p_owner->fmt.i_codec );
877 p_owner->error = true;
879 if(p_block)
880 block_Release(p_block);
882 block_ChainRelease(p_sout_block);
883 break;
887 while( p_sout_block )
889 block_t *p_next = p_sout_block->p_next;
891 p_sout_block->p_next = NULL;
893 if( DecoderPlaySout( p_dec, p_sout_block ) == VLC_EGENERIC )
895 msg_Err( p_dec, "cannot continue streaming due to errors with codec %4.4s",
896 (char *)&p_owner->fmt.i_codec );
898 p_owner->error = true;
900 /* Cleanup */
902 if( p_block )
903 block_Release( p_block );
905 block_ChainRelease( p_next );
906 return;
909 p_sout_block = p_next;
913 #endif
915 static void DecoderPlayCc( decoder_t *p_dec, block_t *p_cc,
916 const decoder_cc_desc_t *p_desc )
918 decoder_owner_sys_t *p_owner = p_dec->p_owner;
920 vlc_mutex_lock( &p_owner->lock );
922 p_owner->cc.desc = *p_desc;
924 /* Fanout data to all decoders. We do not know if es_out
925 selected 608 or 708. */
926 uint64_t i_bitmap = p_owner->cc.desc.i_608_channels |
927 p_owner->cc.desc.i_708_channels;
929 for( int i=0; i_bitmap > 0; i_bitmap >>= 1, i++ )
931 decoder_t *p_ccdec = p_owner->cc.pp_decoder[i];
932 if( !p_ccdec )
933 continue;
935 if( i_bitmap > 1 )
937 block_FifoPut( p_ccdec->p_owner->p_fifo, block_Duplicate(p_cc) );
939 else
941 block_FifoPut( p_ccdec->p_owner->p_fifo, p_cc );
942 p_cc = NULL; /* was last dec */
946 vlc_mutex_unlock( &p_owner->lock );
948 if( p_cc ) /* can have bitmap set but no created decs */
949 block_Release( p_cc );
952 static void PacketizerGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
954 decoder_owner_sys_t *p_owner = p_dec->p_owner;
955 block_t *p_cc;
956 decoder_cc_desc_t desc;
958 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
959 if( !p_owner->cc.b_supported )
960 return;
962 assert( p_dec_cc->pf_get_cc != NULL );
964 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, &desc );
965 if( !p_cc )
966 return;
967 DecoderPlayCc( p_dec, p_cc, &desc );
970 static int DecoderQueueCc( decoder_t *p_videodec, block_t *p_cc,
971 const decoder_cc_desc_t *p_desc )
973 decoder_owner_sys_t *p_owner = p_videodec->p_owner;
975 if( unlikely( p_cc != NULL ) )
977 if( p_owner->cc.b_supported &&
978 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
979 DecoderPlayCc( p_videodec, p_cc, p_desc );
980 else
981 block_Release( p_cc );
983 return 0;
986 static int DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
987 unsigned *restrict pi_lost_sum )
989 decoder_owner_sys_t *p_owner = p_dec->p_owner;
990 vout_thread_t *p_vout = p_owner->p_vout;
991 bool prerolled;
993 vlc_mutex_lock( &p_owner->lock );
994 if( p_owner->i_preroll_end > p_picture->date )
996 vlc_mutex_unlock( &p_owner->lock );
997 picture_Release( p_picture );
998 return -1;
1001 prerolled = p_owner->i_preroll_end > INT64_MIN;
1002 p_owner->i_preroll_end = INT64_MIN;
1003 vlc_mutex_unlock( &p_owner->lock );
1005 if( unlikely(prerolled) )
1007 msg_Dbg( p_dec, "end of video preroll" );
1009 if( p_vout )
1010 vout_Flush( p_vout, VLC_TS_INVALID+1 );
1013 if( p_picture->date <= VLC_TS_INVALID )
1015 msg_Warn( p_dec, "non-dated video buffer received" );
1016 goto discard;
1019 /* */
1020 vlc_mutex_lock( &p_owner->lock );
1022 if( p_owner->b_waiting && !p_owner->b_first )
1024 p_owner->b_has_data = true;
1025 vlc_cond_signal( &p_owner->wait_acknowledge );
1027 bool b_first_after_wait = p_owner->b_waiting && p_owner->b_has_data;
1029 DecoderWaitUnblock( p_dec );
1031 if( p_owner->b_waiting )
1033 assert( p_owner->b_first );
1034 msg_Dbg( p_dec, "Received first picture" );
1035 p_owner->b_first = false;
1036 p_picture->b_force = true;
1039 const bool b_dated = p_picture->date > VLC_TS_INVALID;
1040 int i_rate = INPUT_RATE_DEFAULT;
1041 DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1042 &i_rate, DECODER_BOGUS_VIDEO_DELAY );
1044 vlc_mutex_unlock( &p_owner->lock );
1046 /* FIXME: The *input* FIFO should not be locked here. This will not work
1047 * properly if/when pictures are queued asynchronously. */
1048 vlc_fifo_Lock( p_owner->p_fifo );
1049 if( unlikely(p_owner->paused) && likely(p_owner->frames_countdown > 0) )
1050 p_owner->frames_countdown--;
1051 vlc_fifo_Unlock( p_owner->p_fifo );
1053 /* */
1054 if( p_vout == NULL )
1055 goto discard;
1057 if( p_picture->b_force || p_picture->date > VLC_TS_INVALID )
1058 /* FIXME: VLC_TS_INVALID -- verify video_output */
1060 if( i_rate != p_owner->i_last_rate || b_first_after_wait )
1062 /* Be sure to not display old picture after our own */
1063 vout_Flush( p_vout, p_picture->date );
1064 p_owner->i_last_rate = i_rate;
1066 vout_PutPicture( p_vout, p_picture );
1068 else
1070 if( b_dated )
1071 msg_Warn( p_dec, "early picture skipped" );
1072 else
1073 msg_Warn( p_dec, "non-dated video buffer received" );
1074 goto discard;
1077 return 0;
1078 discard:
1079 *pi_lost_sum += 1;
1080 picture_Release( p_picture );
1081 return 0;
1084 static void DecoderUpdateStatVideo( decoder_owner_sys_t *p_owner,
1085 unsigned decoded, unsigned lost )
1087 input_thread_t *p_input = p_owner->p_input;
1088 unsigned displayed = 0;
1090 /* Update ugly stat */
1091 if( p_input == NULL )
1092 return;
1094 if( p_owner->p_vout != NULL )
1096 unsigned vout_lost = 0;
1098 vout_GetResetStatistic( p_owner->p_vout, &displayed, &vout_lost );
1099 lost += vout_lost;
1102 struct input_stats *stats = input_priv(p_input)->stats;
1104 if( stats != NULL )
1106 atomic_fetch_add_explicit(&stats->decoded_video, decoded,
1107 memory_order_relaxed);
1108 atomic_fetch_add_explicit(&stats->lost_pictures, lost,
1109 memory_order_relaxed);
1110 atomic_fetch_add_explicit(&stats->displayed_pictures, displayed,
1111 memory_order_relaxed);
1115 static int DecoderQueueVideo( decoder_t *p_dec, picture_t *p_pic )
1117 assert( p_pic );
1118 unsigned i_lost = 0;
1119 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1121 int ret = DecoderPlayVideo( p_dec, p_pic, &i_lost );
1123 p_owner->pf_update_stat( p_owner, 1, i_lost );
1124 return ret;
1127 static int DecoderPlayAudio( decoder_t *p_dec, block_t *p_audio,
1128 unsigned *restrict pi_lost_sum )
1130 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1131 bool prerolled;
1133 assert( p_audio != NULL );
1135 vlc_mutex_lock( &p_owner->lock );
1136 if( p_owner->i_preroll_end > p_audio->i_pts )
1138 vlc_mutex_unlock( &p_owner->lock );
1139 block_Release( p_audio );
1140 return -1;
1143 prerolled = p_owner->i_preroll_end > INT64_MIN;
1144 p_owner->i_preroll_end = INT64_MIN;
1145 vlc_mutex_unlock( &p_owner->lock );
1147 if( unlikely(prerolled) )
1149 msg_Dbg( p_dec, "end of audio preroll" );
1151 if( p_owner->p_aout )
1152 aout_DecFlush( p_owner->p_aout, false );
1155 /* */
1156 if( p_audio->i_pts <= VLC_TS_INVALID ) // FIXME --VLC_TS_INVALID verify audio_output/*
1158 msg_Warn( p_dec, "non-dated audio buffer received" );
1159 *pi_lost_sum += 1;
1160 block_Release( p_audio );
1161 return 0;
1164 /* */
1165 vlc_mutex_lock( &p_owner->lock );
1166 if( p_owner->b_waiting )
1168 p_owner->b_has_data = true;
1169 vlc_cond_signal( &p_owner->wait_acknowledge );
1172 /* */
1173 int i_rate = INPUT_RATE_DEFAULT;
1175 DecoderWaitUnblock( p_dec );
1176 DecoderFixTs( p_dec, &p_audio->i_pts, NULL, &p_audio->i_length,
1177 &i_rate, AOUT_MAX_ADVANCE_TIME );
1178 vlc_mutex_unlock( &p_owner->lock );
1180 audio_output_t *p_aout = p_owner->p_aout;
1182 if( p_aout != NULL && p_audio->i_pts > VLC_TS_INVALID
1183 && i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE
1184 && i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE
1185 && !DecoderTimedWait( p_dec, p_audio->i_pts - AOUT_MAX_PREPARE_TIME ) )
1187 int status = aout_DecPlay( p_aout, p_audio, i_rate );
1188 if( status == AOUT_DEC_CHANGED )
1190 /* Only reload the decoder */
1191 RequestReload( p_dec );
1193 else if( status == AOUT_DEC_FAILED )
1195 /* If we reload because the aout failed, we should release it. That
1196 * way, a next call to aout_update_format() won't re-use the
1197 * previous (failing) aout but will try to create a new one. */
1198 atomic_store( &p_owner->reload, RELOAD_DECODER_AOUT );
1201 else
1203 msg_Dbg( p_dec, "discarded audio buffer" );
1204 *pi_lost_sum += 1;
1205 block_Release( p_audio );
1207 return 0;
1210 static void DecoderUpdateStatAudio( decoder_owner_sys_t *p_owner,
1211 unsigned decoded, unsigned lost )
1213 input_thread_t *p_input = p_owner->p_input;
1214 unsigned played = 0;
1216 /* Update ugly stat */
1217 if( p_input == NULL )
1218 return;
1220 if( p_owner->p_aout != NULL )
1222 unsigned aout_lost;
1224 aout_DecGetResetStats( p_owner->p_aout, &aout_lost, &played );
1225 lost += aout_lost;
1228 struct input_stats *stats = input_priv(p_input)->stats;
1230 if( stats != NULL )
1232 atomic_fetch_add_explicit(&stats->lost_abuffers, lost,
1233 memory_order_relaxed);
1234 atomic_fetch_add_explicit(&stats->played_abuffers, played,
1235 memory_order_relaxed);
1236 atomic_fetch_add_explicit(&stats->decoded_audio, decoded,
1237 memory_order_relaxed);
1241 static int DecoderQueueAudio( decoder_t *p_dec, block_t *p_aout_buf )
1243 unsigned lost = 0;
1244 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1246 int ret = DecoderPlayAudio( p_dec, p_aout_buf, &lost );
1248 p_owner->pf_update_stat( p_owner, 1, lost );
1250 return ret;
1253 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic )
1255 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1256 vout_thread_t *p_vout = p_owner->p_spu_vout;
1258 /* */
1259 if( p_subpic->i_start <= VLC_TS_INVALID )
1261 msg_Warn( p_dec, "non-dated spu buffer received" );
1262 subpicture_Delete( p_subpic );
1263 return;
1266 /* */
1267 vlc_mutex_lock( &p_owner->lock );
1269 if( p_owner->b_waiting )
1271 p_owner->b_has_data = true;
1272 vlc_cond_signal( &p_owner->wait_acknowledge );
1275 DecoderWaitUnblock( p_dec );
1276 DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1277 NULL, INT64_MAX );
1278 vlc_mutex_unlock( &p_owner->lock );
1280 if( p_subpic->i_start <= VLC_TS_INVALID
1281 || DecoderTimedWait( p_dec, p_subpic->i_start - SPU_MAX_PREPARE_TIME ) )
1283 subpicture_Delete( p_subpic );
1284 return;
1287 vout_PutSubpicture( p_vout, p_subpic );
1290 static void DecoderUpdateStatSpu( decoder_owner_sys_t *p_owner,
1291 unsigned decoded, unsigned lost )
1293 (void) p_owner; (void) decoded; (void) lost;
1296 static int DecoderQueueSpu( decoder_t *p_dec, subpicture_t *p_spu )
1298 assert( p_spu );
1299 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1301 int i_ret = -1;
1302 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1303 if( p_vout && p_owner->p_spu_vout == p_vout )
1305 /* Preroll does not work very well with subtitle */
1306 vlc_mutex_lock( &p_owner->lock );
1307 if( p_spu->i_start > VLC_TS_INVALID &&
1308 p_spu->i_start < p_owner->i_preroll_end &&
1309 ( p_spu->i_stop <= VLC_TS_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1311 vlc_mutex_unlock( &p_owner->lock );
1312 subpicture_Delete( p_spu );
1314 else
1316 vlc_mutex_unlock( &p_owner->lock );
1317 DecoderPlaySpu( p_dec, p_spu );
1318 i_ret = 0;
1321 else
1323 subpicture_Delete( p_spu );
1325 if( p_vout )
1326 vlc_object_release( p_vout );
1327 return i_ret;
1330 static void DecoderProcess( decoder_t *p_dec, block_t *p_block );
1331 static void DecoderDecode( decoder_t *p_dec, block_t *p_block )
1333 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1335 int ret = p_dec->pf_decode( p_dec, p_block );
1336 switch( ret )
1338 case VLCDEC_SUCCESS:
1339 p_owner->pf_update_stat( p_owner, 1, 0 );
1340 break;
1341 case VLCDEC_ECRITICAL:
1342 p_owner->error = true;
1343 break;
1344 case VLCDEC_RELOAD:
1345 RequestReload( p_dec );
1346 if( unlikely( p_block == NULL ) )
1347 break;
1348 if( !( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1350 p_block->i_flags |= BLOCK_FLAG_CORE_PRIVATE_RELOADED;
1351 DecoderProcess( p_dec, p_block );
1353 else /* We prefer loosing this block than an infinite recursion */
1354 block_Release( p_block );
1355 break;
1356 default:
1357 vlc_assert_unreachable();
1362 * Decode a block
1364 * \param p_dec the decoder object
1365 * \param p_block the block to decode
1367 static void DecoderProcess( decoder_t *p_dec, block_t *p_block )
1369 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1371 if( p_owner->error )
1372 goto error;
1374 /* Here, the atomic doesn't prevent to miss a reload request.
1375 * DecoderProcess() can still be called after the decoder module or the
1376 * audio output requested a reload. This will only result in a drop of an
1377 * input block or an output buffer. */
1378 enum reload reload;
1379 if( ( reload = atomic_exchange( &p_owner->reload, RELOAD_NO_REQUEST ) ) )
1381 msg_Warn( p_dec, "Reloading the decoder module%s",
1382 reload == RELOAD_DECODER_AOUT ? " and the audio output" : "" );
1384 if( ReloadDecoder( p_dec, false, &p_dec->fmt_in, reload ) != VLC_SUCCESS )
1385 goto error;
1388 bool packetize = p_owner->p_packetizer != NULL;
1389 if( p_block )
1391 if( p_block->i_buffer <= 0 )
1392 goto error;
1394 vlc_mutex_lock( &p_owner->lock );
1395 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1396 vlc_mutex_unlock( &p_owner->lock );
1397 if( unlikely( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1399 /* This block has already been packetized */
1400 packetize = false;
1404 #ifdef ENABLE_SOUT
1405 if( p_owner->p_sout != NULL )
1407 DecoderProcessSout( p_dec, p_block );
1408 return;
1410 #endif
1411 if( packetize )
1413 block_t *p_packetized_block;
1414 block_t **pp_block = p_block ? &p_block : NULL;
1415 decoder_t *p_packetizer = p_owner->p_packetizer;
1417 while( (p_packetized_block =
1418 p_packetizer->pf_packetize( p_packetizer, pp_block ) ) )
1420 if( !es_format_IsSimilar( &p_dec->fmt_in, &p_packetizer->fmt_out ) )
1422 msg_Dbg( p_dec, "restarting module due to input format change");
1424 /* Drain the decoder module */
1425 DecoderDecode( p_dec, NULL );
1427 if( ReloadDecoder( p_dec, false, &p_packetizer->fmt_out,
1428 RELOAD_DECODER ) != VLC_SUCCESS )
1430 block_ChainRelease( p_packetized_block );
1431 return;
1435 if( p_packetizer->pf_get_cc )
1436 PacketizerGetCc( p_dec, p_packetizer );
1438 while( p_packetized_block )
1440 block_t *p_next = p_packetized_block->p_next;
1441 p_packetized_block->p_next = NULL;
1443 DecoderDecode( p_dec, p_packetized_block );
1444 if( p_owner->error )
1446 block_ChainRelease( p_next );
1447 return;
1450 p_packetized_block = p_next;
1453 /* Drain the decoder after the packetizer is drained */
1454 if( !pp_block )
1455 DecoderDecode( p_dec, NULL );
1457 else
1458 DecoderDecode( p_dec, p_block );
1459 return;
1461 error:
1462 if( p_block )
1463 block_Release( p_block );
1466 static void DecoderProcessFlush( decoder_t *p_dec )
1468 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1469 decoder_t *p_packetizer = p_owner->p_packetizer;
1471 if( p_owner->error )
1472 return;
1474 if( p_packetizer != NULL && p_packetizer->pf_flush != NULL )
1475 p_packetizer->pf_flush( p_packetizer );
1477 if ( p_dec->pf_flush != NULL )
1478 p_dec->pf_flush( p_dec );
1480 /* flush CC sub decoders */
1481 if( p_owner->cc.b_supported )
1483 for( int i=0; i<MAX_CC_DECODERS; i++ )
1485 decoder_t *p_subdec = p_owner->cc.pp_decoder[i];
1486 if( p_subdec && p_subdec->pf_flush )
1487 p_subdec->pf_flush( p_subdec );
1491 #ifdef ENABLE_SOUT
1492 if ( p_owner->p_sout_input != NULL )
1494 sout_InputFlush( p_owner->p_sout_input );
1496 #endif
1497 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1499 if( p_owner->p_aout )
1500 aout_DecFlush( p_owner->p_aout, false );
1502 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1504 if( p_owner->p_vout )
1505 vout_Flush( p_owner->p_vout, VLC_TS_INVALID+1 );
1507 else if( p_dec->fmt_out.i_cat == SPU_ES )
1509 if( p_owner->p_spu_vout )
1511 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1513 if( p_vout && p_owner->p_spu_vout == p_vout )
1514 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1516 if( p_vout )
1517 vlc_object_release( p_vout );
1521 vlc_mutex_lock( &p_owner->lock );
1522 p_owner->i_preroll_end = INT64_MIN;
1523 vlc_mutex_unlock( &p_owner->lock );
1527 * The decoding main loop
1529 * \param p_dec the decoder
1531 static void *DecoderThread( void *p_data )
1533 decoder_t *p_dec = (decoder_t *)p_data;
1534 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1535 bool paused = false;
1537 /* The decoder's main loop */
1538 vlc_fifo_Lock( p_owner->p_fifo );
1539 vlc_fifo_CleanupPush( p_owner->p_fifo );
1541 for( ;; )
1543 if( p_owner->flushing )
1544 { /* Flush before/regardless of pause. We do not want to resume just
1545 * for the sake of flushing (glitches could otherwise happen). */
1546 int canc = vlc_savecancel();
1548 vlc_fifo_Unlock( p_owner->p_fifo );
1550 /* Flush the decoder (and the output) */
1551 DecoderProcessFlush( p_dec );
1553 vlc_fifo_Lock( p_owner->p_fifo );
1554 vlc_restorecancel( canc );
1556 /* Reset flushing after DecoderProcess in case input_DecoderFlush
1557 * is called again. This will avoid a second useless flush (but
1558 * harmless). */
1559 p_owner->flushing = false;
1561 continue;
1564 if( paused != p_owner->paused )
1565 { /* Update playing/paused status of the output */
1566 int canc = vlc_savecancel();
1567 mtime_t date = p_owner->pause_date;
1569 paused = p_owner->paused;
1570 vlc_fifo_Unlock( p_owner->p_fifo );
1572 /* NOTE: Only the audio and video outputs care about pause. */
1573 msg_Dbg( p_dec, "toggling %s", paused ? "resume" : "pause" );
1574 if( p_owner->p_vout != NULL )
1575 vout_ChangePause( p_owner->p_vout, paused, date );
1576 if( p_owner->p_aout != NULL )
1577 aout_DecChangePause( p_owner->p_aout, paused, date );
1579 vlc_restorecancel( canc );
1580 vlc_fifo_Lock( p_owner->p_fifo );
1581 continue;
1584 if( p_owner->paused && p_owner->frames_countdown == 0 )
1585 { /* Wait for resumption from pause */
1586 p_owner->b_idle = true;
1587 vlc_cond_signal( &p_owner->wait_acknowledge );
1588 vlc_fifo_Wait( p_owner->p_fifo );
1589 p_owner->b_idle = false;
1590 continue;
1593 vlc_cond_signal( &p_owner->wait_fifo );
1594 vlc_testcancel(); /* forced expedited cancellation in case of stop */
1596 block_t *p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
1597 if( p_block == NULL )
1599 if( likely(!p_owner->b_draining) )
1600 { /* Wait for a block to decode (or a request to drain) */
1601 p_owner->b_idle = true;
1602 vlc_cond_signal( &p_owner->wait_acknowledge );
1603 vlc_fifo_Wait( p_owner->p_fifo );
1604 p_owner->b_idle = false;
1605 continue;
1607 /* We have emptied the FIFO and there is a pending request to
1608 * drain. Pass p_block = NULL to decoder just once. */
1611 vlc_fifo_Unlock( p_owner->p_fifo );
1613 int canc = vlc_savecancel();
1614 DecoderProcess( p_dec, p_block );
1616 if( p_block == NULL )
1617 { /* Draining: the decoder is drained and all decoded buffers are
1618 * queued to the output at this point. Now drain the output. */
1619 if( p_owner->p_aout != NULL )
1620 aout_DecFlush( p_owner->p_aout, true );
1622 vlc_restorecancel( canc );
1624 /* TODO? Wait for draining instead of polling. */
1625 vlc_mutex_lock( &p_owner->lock );
1626 if( p_owner->b_draining && (p_block == NULL) )
1628 p_owner->b_draining = false;
1629 p_owner->drained = true;
1631 vlc_fifo_Lock( p_owner->p_fifo );
1632 vlc_cond_signal( &p_owner->wait_acknowledge );
1633 vlc_mutex_unlock( &p_owner->lock );
1635 vlc_cleanup_pop();
1636 vlc_assert_unreachable();
1640 * Create a decoder object
1642 * \param p_input the input thread
1643 * \param p_es the es descriptor
1644 * \param b_packetizer instead of a decoder
1645 * \return the decoder object
1647 static decoder_t * CreateDecoder( vlc_object_t *p_parent,
1648 input_thread_t *p_input,
1649 const es_format_t *fmt,
1650 input_resource_t *p_resource,
1651 sout_instance_t *p_sout )
1653 decoder_t *p_dec;
1654 decoder_owner_sys_t *p_owner;
1656 p_dec = vlc_custom_create( p_parent, sizeof( *p_dec ), "decoder" );
1657 if( p_dec == NULL )
1658 return NULL;
1660 /* Allocate our private structure for the decoder */
1661 p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
1662 if( unlikely(p_owner == NULL) )
1664 vlc_object_release( p_dec );
1665 return NULL;
1667 p_owner->i_preroll_end = INT64_MIN;
1668 p_owner->i_last_rate = INPUT_RATE_DEFAULT;
1669 p_owner->p_input = p_input;
1670 p_owner->p_resource = p_resource;
1671 p_owner->p_aout = NULL;
1672 p_owner->p_vout = NULL;
1673 p_owner->p_spu_vout = NULL;
1674 p_owner->i_spu_channel = 0;
1675 p_owner->i_spu_order = 0;
1676 p_owner->p_sout = p_sout;
1677 p_owner->p_sout_input = NULL;
1678 p_owner->p_packetizer = NULL;
1680 p_owner->b_fmt_description = false;
1681 p_owner->p_description = NULL;
1683 p_owner->paused = false;
1684 p_owner->pause_date = VLC_TS_INVALID;
1685 p_owner->frames_countdown = 0;
1687 p_owner->b_waiting = false;
1688 p_owner->b_first = true;
1689 p_owner->b_has_data = false;
1691 p_owner->error = false;
1693 p_owner->flushing = false;
1694 p_owner->b_draining = false;
1695 p_owner->drained = false;
1696 atomic_init( &p_owner->reload, RELOAD_NO_REQUEST );
1697 p_owner->b_idle = false;
1699 es_format_Init( &p_owner->fmt, fmt->i_cat, 0 );
1701 /* decoder fifo */
1702 p_owner->p_fifo = block_FifoNew();
1703 if( unlikely(p_owner->p_fifo == NULL) )
1705 free( p_owner );
1706 vlc_object_release( p_dec );
1707 return NULL;
1710 vlc_mutex_init( &p_owner->lock );
1711 vlc_cond_init( &p_owner->wait_request );
1712 vlc_cond_init( &p_owner->wait_acknowledge );
1713 vlc_cond_init( &p_owner->wait_fifo );
1714 vlc_cond_init( &p_owner->wait_timed );
1716 /* Set buffers allocation callbacks for the decoders */
1717 p_dec->pf_aout_format_update = aout_update_format;
1718 p_dec->pf_vout_format_update = vout_update_format;
1719 p_dec->pf_vout_buffer_new = vout_new_buffer;
1720 p_dec->pf_spu_buffer_new = spu_new_buffer;
1721 /* */
1722 p_dec->pf_get_attachments = DecoderGetInputAttachments;
1723 p_dec->pf_get_display_date = DecoderGetDisplayDate;
1724 p_dec->pf_get_display_rate = DecoderGetDisplayRate;
1726 /* Load a packetizer module if the input is not already packetized */
1727 if( p_sout == NULL && !fmt->b_packetized )
1729 p_owner->p_packetizer =
1730 vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
1731 if( p_owner->p_packetizer )
1733 if( LoadDecoder( p_owner->p_packetizer, true, fmt ) )
1735 vlc_object_release( p_owner->p_packetizer );
1736 p_owner->p_packetizer = NULL;
1738 else
1740 p_owner->p_packetizer->fmt_out.b_packetized = true;
1741 fmt = &p_owner->p_packetizer->fmt_out;
1746 /* Find a suitable decoder/packetizer module */
1747 if( LoadDecoder( p_dec, p_sout != NULL, fmt ) )
1748 return p_dec;
1750 switch( p_dec->fmt_out.i_cat )
1752 case VIDEO_ES:
1753 p_dec->pf_queue_video = DecoderQueueVideo;
1754 p_dec->pf_queue_cc = DecoderQueueCc;
1755 p_owner->pf_update_stat = DecoderUpdateStatVideo;
1756 break;
1757 case AUDIO_ES:
1758 p_dec->pf_queue_audio = DecoderQueueAudio;
1759 p_owner->pf_update_stat = DecoderUpdateStatAudio;
1760 break;
1761 case SPU_ES:
1762 p_dec->pf_queue_sub = DecoderQueueSpu;
1763 p_owner->pf_update_stat = DecoderUpdateStatSpu;
1764 break;
1765 default:
1766 msg_Err( p_dec, "unknown ES format" );
1767 UnloadDecoder( p_dec );
1768 return p_dec;
1770 /* Copy ourself the input replay gain */
1771 if( fmt->i_cat == AUDIO_ES )
1773 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1775 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
1777 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
1778 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
1780 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
1782 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
1783 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
1788 /* */
1789 p_owner->cc.b_supported = ( p_sout == NULL );
1791 p_owner->cc.desc.i_608_channels = 0;
1792 p_owner->cc.desc.i_708_channels = 0;
1793 for( unsigned i = 0; i < MAX_CC_DECODERS; i++ )
1794 p_owner->cc.pp_decoder[i] = NULL;
1795 p_owner->i_ts_delay = 0;
1796 return p_dec;
1800 * Destroys a decoder object
1802 * \param p_dec the decoder object
1803 * \return nothing
1805 static void DeleteDecoder( decoder_t * p_dec )
1807 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1809 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s'",
1810 (char*)&p_dec->fmt_in.i_codec );
1812 const bool b_flush_spu = p_dec->fmt_out.i_cat == SPU_ES;
1813 UnloadDecoder( p_dec );
1815 /* Free all packets still in the decoder fifo. */
1816 block_FifoRelease( p_owner->p_fifo );
1818 /* Cleanup */
1819 if( p_owner->p_aout )
1821 /* TODO: REVISIT gap-less audio */
1822 aout_DecFlush( p_owner->p_aout, false );
1823 aout_DecDelete( p_owner->p_aout );
1824 input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1825 if( p_owner->p_input != NULL )
1826 input_SendEventAout( p_owner->p_input );
1828 if( p_owner->p_vout )
1830 /* Reset the cancel state that was set before joining the decoder
1831 * thread */
1832 vout_Cancel( p_owner->p_vout, false );
1834 input_resource_RequestVout( p_owner->p_resource, p_owner->p_vout, NULL,
1835 0, true );
1836 if( p_owner->p_input != NULL )
1837 input_SendEventVout( p_owner->p_input );
1840 #ifdef ENABLE_SOUT
1841 if( p_owner->p_sout_input )
1843 sout_InputDelete( p_owner->p_sout_input );
1845 #endif
1846 es_format_Clean( &p_owner->fmt );
1848 if( b_flush_spu )
1850 vout_thread_t *p_vout = input_resource_HoldVout( p_owner->p_resource );
1851 if( p_vout )
1853 if( p_owner->p_spu_vout == p_vout )
1854 vout_FlushSubpictureChannel( p_vout, p_owner->i_spu_channel );
1855 vlc_object_release( p_vout );
1859 if( p_owner->p_description )
1860 vlc_meta_Delete( p_owner->p_description );
1862 if( p_owner->p_packetizer )
1864 UnloadDecoder( p_owner->p_packetizer );
1865 vlc_object_release( p_owner->p_packetizer );
1868 vlc_cond_destroy( &p_owner->wait_timed );
1869 vlc_cond_destroy( &p_owner->wait_fifo );
1870 vlc_cond_destroy( &p_owner->wait_acknowledge );
1871 vlc_cond_destroy( &p_owner->wait_request );
1872 vlc_mutex_destroy( &p_owner->lock );
1874 vlc_object_release( p_dec );
1876 free( p_owner );
1879 /* */
1880 static void DecoderUnsupportedCodec( decoder_t *p_dec, const es_format_t *fmt, bool b_decoding )
1882 if (fmt->i_codec != VLC_CODEC_UNKNOWN && fmt->i_codec) {
1883 const char *desc = vlc_fourcc_GetDescription(fmt->i_cat, fmt->i_codec);
1884 if (!desc || !*desc)
1885 desc = N_("No description for this codec");
1886 msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&fmt->i_codec, desc );
1887 vlc_dialog_display_error( p_dec, _("Codec not supported"),
1888 _("VLC could not decode the format \"%4.4s\" (%s)"),
1889 (char*)&fmt->i_codec, desc );
1890 } else if( b_decoding ){
1891 msg_Err( p_dec, "could not identify codec" );
1892 vlc_dialog_display_error( p_dec, _("Unidentified codec"),
1893 _("VLC could not identify the audio or video codec" ) );
1897 /* TODO: pass p_sout through p_resource? -- Courmisch */
1898 static decoder_t *decoder_New( vlc_object_t *p_parent, input_thread_t *p_input,
1899 const es_format_t *fmt, input_clock_t *p_clock,
1900 input_resource_t *p_resource,
1901 sout_instance_t *p_sout )
1903 decoder_t *p_dec = NULL;
1904 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
1905 int i_priority;
1907 /* Create the decoder configuration structure */
1908 p_dec = CreateDecoder( p_parent, p_input, fmt, p_resource, p_sout );
1909 if( p_dec == NULL )
1911 msg_Err( p_parent, "could not create %s", psz_type );
1912 vlc_dialog_display_error( p_parent, _("Streaming / Transcoding failed"),
1913 _("VLC could not open the %s module."), vlc_gettext( psz_type ) );
1914 return NULL;
1917 if( !p_dec->p_module )
1919 DecoderUnsupportedCodec( p_dec, fmt, !p_sout );
1921 DeleteDecoder( p_dec );
1922 return NULL;
1925 p_dec->p_owner->p_clock = p_clock;
1926 assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
1928 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1929 i_priority = VLC_THREAD_PRIORITY_AUDIO;
1930 else
1931 i_priority = VLC_THREAD_PRIORITY_VIDEO;
1933 #ifdef ENABLE_SOUT
1934 /* Do not delay sout creation for SPU or DATA. */
1935 if( p_sout && fmt->b_packetized &&
1936 (fmt->i_cat != VIDEO_ES && fmt->i_cat != AUDIO_ES) )
1938 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1939 p_owner->p_sout_input = sout_InputNew( p_owner->p_sout, fmt );
1940 if( p_owner->p_sout_input == NULL )
1942 msg_Err( p_dec, "cannot create sout input (%4.4s)",
1943 (char *)&fmt->i_codec );
1944 p_owner->error = true;
1947 #endif
1949 /* Spawn the decoder thread */
1950 if( vlc_clone( &p_dec->p_owner->thread, DecoderThread, p_dec, i_priority ) )
1952 msg_Err( p_dec, "cannot spawn decoder thread" );
1953 DeleteDecoder( p_dec );
1954 return NULL;
1957 return p_dec;
1962 * Spawns a new decoder thread from the input thread
1964 * \param p_input the input thread
1965 * \param p_es the es descriptor
1966 * \return the spawned decoder object
1968 decoder_t *input_DecoderNew( input_thread_t *p_input,
1969 es_format_t *fmt, input_clock_t *p_clock,
1970 sout_instance_t *p_sout )
1972 return decoder_New( VLC_OBJECT(p_input), p_input, fmt, p_clock,
1973 input_priv(p_input)->p_resource, p_sout );
1977 * Spawn a decoder thread outside of the input thread.
1979 decoder_t *input_DecoderCreate( vlc_object_t *p_parent, const es_format_t *fmt,
1980 input_resource_t *p_resource )
1982 return decoder_New( p_parent, NULL, fmt, NULL, p_resource, NULL );
1987 * Kills a decoder thread and waits until it's finished
1989 * \param p_input the input thread
1990 * \param p_es the es descriptor
1991 * \return nothing
1993 void input_DecoderDelete( decoder_t *p_dec )
1995 decoder_owner_sys_t *p_owner = p_dec->p_owner;
1997 vlc_cancel( p_owner->thread );
1999 vlc_fifo_Lock( p_owner->p_fifo );
2000 /* Signal DecoderTimedWait */
2001 p_owner->flushing = true;
2002 vlc_cond_signal( &p_owner->wait_timed );
2003 vlc_fifo_Unlock( p_owner->p_fifo );
2005 /* Make sure we aren't waiting/decoding anymore */
2006 vlc_mutex_lock( &p_owner->lock );
2007 p_owner->b_waiting = false;
2008 vlc_cond_signal( &p_owner->wait_request );
2010 /* If the video output is paused or slow, or if the picture pool size was
2011 * under-estimated (e.g. greedy video filter, buggy decoder...), the
2012 * the picture pool may be empty, and the decoder thread or any decoder
2013 * module worker threads may be stuck waiting for free picture buffers.
2015 * This unblocks the thread, allowing the decoder module to join all its
2016 * worker threads (if any) and the decoder thread to terminate. */
2017 if( p_owner->p_vout != NULL )
2018 vout_Cancel( p_owner->p_vout, true );
2019 vlc_mutex_unlock( &p_owner->lock );
2021 vlc_join( p_owner->thread, NULL );
2023 /* */
2024 if( p_dec->p_owner->cc.b_supported )
2026 for( int i = 0; i < MAX_CC_DECODERS; i++ )
2027 input_DecoderSetCcState( p_dec, VLC_CODEC_CEA608, i, false );
2030 /* Delete decoder */
2031 DeleteDecoder( p_dec );
2035 * Put a block_t in the decoder's fifo.
2036 * Thread-safe w.r.t. the decoder. May be a cancellation point.
2038 * \param p_dec the decoder object
2039 * \param p_block the data block
2041 void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
2043 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2045 vlc_fifo_Lock( p_owner->p_fifo );
2046 if( !b_do_pace )
2048 /* FIXME: ideally we would check the time amount of data
2049 * in the FIFO instead of its size. */
2050 /* 400 MiB, i.e. ~ 50mb/s for 60s */
2051 if( vlc_fifo_GetBytes( p_owner->p_fifo ) > 400*1024*1024 )
2053 msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
2054 "consumed quickly enough), resetting fifo!" );
2055 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2056 p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
2059 else
2060 if( !p_owner->b_waiting )
2061 { /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
2062 * Locking is not necessary as b_waiting is only read, not written by
2063 * the decoder thread. */
2064 while( vlc_fifo_GetCount( p_owner->p_fifo ) >= 10 )
2065 vlc_fifo_WaitCond( p_owner->p_fifo, &p_owner->wait_fifo );
2068 vlc_fifo_QueueUnlocked( p_owner->p_fifo, p_block );
2069 vlc_fifo_Unlock( p_owner->p_fifo );
2072 bool input_DecoderIsEmpty( decoder_t * p_dec )
2074 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2076 assert( !p_owner->b_waiting );
2078 vlc_fifo_Lock( p_owner->p_fifo );
2079 if( !vlc_fifo_IsEmpty( p_dec->p_owner->p_fifo ) || p_owner->b_draining )
2081 vlc_fifo_Unlock( p_owner->p_fifo );
2082 return false;
2084 vlc_fifo_Unlock( p_owner->p_fifo );
2086 bool b_empty;
2088 vlc_mutex_lock( &p_owner->lock );
2089 #ifdef ENABLE_SOUT
2090 if( p_owner->p_sout_input != NULL )
2091 b_empty = sout_InputIsEmpty( p_owner->p_sout_input );
2092 else
2093 #endif
2094 if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->p_vout != NULL )
2095 b_empty = vout_IsEmpty( p_owner->p_vout );
2096 else if( p_owner->fmt.i_cat == AUDIO_ES )
2097 b_empty = !p_owner->b_draining || p_owner->drained;
2098 else
2099 b_empty = true; /* TODO subtitles support */
2100 vlc_mutex_unlock( &p_owner->lock );
2102 return b_empty;
2106 * Signals that there are no further blocks to decode, and requests that the
2107 * decoder drain all pending buffers. This is used to ensure that all
2108 * intermediate buffers empty and no samples get lost at the end of the stream.
2110 * @note The function does not actually wait for draining. It just signals that
2111 * draining should be performed once the decoder has emptied FIFO.
2113 void input_DecoderDrain( decoder_t *p_dec )
2115 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2117 vlc_fifo_Lock( p_owner->p_fifo );
2118 p_owner->b_draining = true;
2119 vlc_fifo_Signal( p_owner->p_fifo );
2120 vlc_fifo_Unlock( p_owner->p_fifo );
2124 * Requests that the decoder immediately discard all pending buffers.
2125 * This is useful when seeking or when deselecting a stream.
2127 void input_DecoderFlush( decoder_t *p_dec )
2129 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2131 vlc_fifo_Lock( p_owner->p_fifo );
2133 /* Empty the fifo */
2134 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2136 /* Don't need to wait for the DecoderThread to flush. Indeed, if called a
2137 * second time, this function will clear the FIFO again before anything was
2138 * dequeued by DecoderThread and there is no need to flush a second time in
2139 * a row. */
2140 p_owner->flushing = true;
2142 /* Flushing video decoder when paused: increment frames_countdown in order
2143 * to display one frame */
2144 if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->paused
2145 && p_owner->frames_countdown == 0 )
2146 p_owner->frames_countdown++;
2148 vlc_fifo_Signal( p_owner->p_fifo );
2149 vlc_cond_signal( &p_owner->wait_timed );
2151 vlc_fifo_Unlock( p_owner->p_fifo );
2154 void input_DecoderGetCcDesc( decoder_t *p_dec, decoder_cc_desc_t *p_desc )
2156 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2158 vlc_mutex_lock( &p_owner->lock );
2159 *p_desc = p_owner->cc.desc;
2160 vlc_mutex_unlock( &p_owner->lock );
2163 static bool input_DecoderHasCCChanFlag( decoder_t *p_dec,
2164 vlc_fourcc_t codec, int i_channel )
2166 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2168 int i_max_channels;
2169 uint64_t i_bitmap;
2170 if( codec == VLC_CODEC_CEA608 )
2172 i_max_channels = 4;
2173 i_bitmap = p_owner->cc.desc.i_608_channels;
2175 else if( codec == VLC_CODEC_CEA708 )
2177 i_max_channels = 64;
2178 i_bitmap = p_owner->cc.desc.i_708_channels;
2180 else return false;
2182 return ( i_channel >= 0 && i_channel < i_max_channels &&
2183 ( i_bitmap & ((uint64_t)1 << i_channel) ) );
2186 int input_DecoderSetCcState( decoder_t *p_dec, vlc_fourcc_t codec,
2187 int i_channel, bool b_decode )
2189 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2191 //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%x", b_decode, i_channel );
2193 if( !input_DecoderHasCCChanFlag( p_dec, codec, i_channel ) )
2194 return VLC_EGENERIC;
2196 if( b_decode )
2198 decoder_t *p_cc;
2199 es_format_t fmt;
2201 es_format_Init( &fmt, SPU_ES, codec );
2202 fmt.subs.cc.i_channel = i_channel;
2203 fmt.subs.cc.i_reorder_depth = p_owner->cc.desc.i_reorder_depth;
2204 p_cc = input_DecoderNew( p_owner->p_input, &fmt,
2205 p_dec->p_owner->p_clock, p_owner->p_sout );
2206 if( !p_cc )
2208 msg_Err( p_dec, "could not create decoder" );
2209 vlc_dialog_display_error( p_dec,
2210 _("Streaming / Transcoding failed"), "%s",
2211 _("VLC could not open the decoder module.") );
2212 return VLC_EGENERIC;
2214 else if( !p_cc->p_module )
2216 DecoderUnsupportedCodec( p_dec, &fmt, true );
2217 input_DecoderDelete(p_cc);
2218 return VLC_EGENERIC;
2220 p_cc->p_owner->p_clock = p_owner->p_clock;
2222 vlc_mutex_lock( &p_owner->lock );
2223 p_owner->cc.pp_decoder[i_channel] = p_cc;
2224 vlc_mutex_unlock( &p_owner->lock );
2226 else
2228 decoder_t *p_cc;
2230 vlc_mutex_lock( &p_owner->lock );
2231 p_cc = p_owner->cc.pp_decoder[i_channel];
2232 p_owner->cc.pp_decoder[i_channel] = NULL;
2233 vlc_mutex_unlock( &p_owner->lock );
2235 if( p_cc )
2236 input_DecoderDelete(p_cc);
2238 return VLC_SUCCESS;
2241 int input_DecoderGetCcState( decoder_t *p_dec, vlc_fourcc_t codec,
2242 int i_channel, bool *pb_decode )
2244 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2246 if( !input_DecoderHasCCChanFlag( p_dec, codec, i_channel ) )
2247 return VLC_EGENERIC;
2249 vlc_mutex_lock( &p_owner->lock );
2250 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
2251 vlc_mutex_unlock( &p_owner->lock );
2252 return VLC_SUCCESS;
2255 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
2257 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2259 /* Normally, p_owner->b_paused != b_paused here. But if a track is added
2260 * while the input is paused (e.g. add sub file), then b_paused is
2261 * (incorrectly) false. FIXME: This is a bug in the decoder owner. */
2262 vlc_fifo_Lock( p_owner->p_fifo );
2263 p_owner->paused = b_paused;
2264 p_owner->pause_date = i_date;
2265 p_owner->frames_countdown = 0;
2266 vlc_fifo_Signal( p_owner->p_fifo );
2267 vlc_fifo_Unlock( p_owner->p_fifo );
2270 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
2272 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2274 vlc_mutex_lock( &p_owner->lock );
2275 p_owner->i_ts_delay = i_delay;
2276 vlc_mutex_unlock( &p_owner->lock );
2279 void input_DecoderStartWait( decoder_t *p_dec )
2281 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2283 assert( !p_owner->b_waiting );
2285 vlc_mutex_lock( &p_owner->lock );
2286 p_owner->b_first = true;
2287 p_owner->b_has_data = false;
2288 p_owner->b_waiting = true;
2289 vlc_cond_signal( &p_owner->wait_request );
2290 vlc_mutex_unlock( &p_owner->lock );
2293 void input_DecoderStopWait( decoder_t *p_dec )
2295 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2297 assert( p_owner->b_waiting );
2299 vlc_mutex_lock( &p_owner->lock );
2300 p_owner->b_waiting = false;
2301 vlc_cond_signal( &p_owner->wait_request );
2302 vlc_mutex_unlock( &p_owner->lock );
2305 void input_DecoderWait( decoder_t *p_dec )
2307 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2309 assert( p_owner->b_waiting );
2311 vlc_mutex_lock( &p_owner->lock );
2312 while( !p_owner->b_has_data )
2314 /* Don't need to lock p_owner->paused since it's only modified by the
2315 * owner */
2316 if( p_owner->paused )
2317 break;
2318 vlc_fifo_Lock( p_owner->p_fifo );
2319 if( p_owner->b_idle && vlc_fifo_IsEmpty( p_owner->p_fifo ) )
2321 msg_Err( p_dec, "buffer deadlock prevented" );
2322 vlc_fifo_Unlock( p_owner->p_fifo );
2323 break;
2325 vlc_fifo_Unlock( p_owner->p_fifo );
2326 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
2328 vlc_mutex_unlock( &p_owner->lock );
2331 void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
2333 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2335 assert( p_owner->paused );
2336 *pi_duration = 0;
2338 vlc_fifo_Lock( p_owner->p_fifo );
2339 p_owner->frames_countdown++;
2340 vlc_fifo_Signal( p_owner->p_fifo );
2341 vlc_fifo_Unlock( p_owner->p_fifo );
2343 vlc_mutex_lock( &p_owner->lock );
2344 if( p_owner->fmt.i_cat == VIDEO_ES )
2346 if( p_owner->p_vout )
2347 vout_NextPicture( p_owner->p_vout, pi_duration );
2349 vlc_mutex_unlock( &p_owner->lock );
2352 bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
2354 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2355 bool b_changed;
2357 vlc_mutex_lock( &p_owner->lock );
2358 b_changed = p_owner->b_fmt_description;
2359 if( b_changed )
2361 if( p_fmt != NULL )
2362 es_format_Copy( p_fmt, &p_owner->fmt );
2364 if( pp_meta )
2366 *pp_meta = NULL;
2367 if( p_owner->p_description )
2369 *pp_meta = vlc_meta_New();
2370 if( *pp_meta )
2371 vlc_meta_Merge( *pp_meta, p_owner->p_description );
2374 p_owner->b_fmt_description = false;
2376 vlc_mutex_unlock( &p_owner->lock );
2377 return b_changed;
2380 size_t input_DecoderGetFifoSize( decoder_t *p_dec )
2382 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2384 return block_FifoSize( p_owner->p_fifo );
2387 void input_DecoderGetObjects( decoder_t *p_dec,
2388 vout_thread_t **pp_vout, audio_output_t **pp_aout )
2390 decoder_owner_sys_t *p_owner = p_dec->p_owner;
2392 vlc_mutex_lock( &p_owner->lock );
2393 if( pp_vout )
2394 *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
2395 if( pp_aout )
2396 *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
2397 vlc_mutex_unlock( &p_owner->lock );