decoder: fix abort on vout failure
[vlc.git] / src / input / decoder.c
blobd475f3d207ba6a9892abd2f24f05624bd7d8e767
1 /*****************************************************************************
2 * decoder.c: Functions for the management of decoders
3 *****************************************************************************
4 * Copyright (C) 1999-2019 VLC authors, VideoLAN and Videolabs SAS
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
7 * Gildas Bazin <gbazin@videolan.org>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 #include <assert.h>
32 #include <stdatomic.h>
34 #include <vlc_common.h>
35 #include <vlc_block.h>
36 #include <vlc_vout.h>
37 #include <vlc_aout.h>
38 #include <vlc_sout.h>
39 #include <vlc_codec.h>
40 #include <vlc_spu.h>
41 #include <vlc_meta.h>
42 #include <vlc_dialog.h>
43 #include <vlc_modules.h>
44 #include <vlc_decoder.h>
45 #include <vlc_picture_pool.h>
47 #include "audio_output/aout_internal.h"
48 #include "stream_output/stream_output.h"
49 #include "../clock/clock.h"
50 #include "decoder.h"
51 #include "resource.h"
52 #include "libvlc.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 vlc_input_decoder_t
68 decoder_t dec;
69 input_resource_t*p_resource;
70 vlc_clock_t *p_clock;
72 const struct vlc_input_decoder_callbacks *cbs;
73 void *cbs_userdata;
75 ssize_t i_spu_channel;
76 int64_t i_spu_order;
78 sout_instance_t *p_sout;
79 sout_packetizer_input_t *p_sout_input;
81 vlc_thread_t thread;
83 /* Some decoders require already packetized data (ie. not truncated) */
84 decoder_t *p_packetizer;
85 bool b_packetizer;
87 /* Current format in use by the output */
88 es_format_t fmt;
89 vlc_video_context *vctx;
91 /* */
92 atomic_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 */
105 /* pool to use when the decoder doesn't use its own */
106 struct picture_pool_t *out_pool;
109 * 3 threads can read/write these output variables, the DecoderThread, the
110 * input thread, and the ModuleThread. The ModuleThread is either the
111 * DecoderThread for synchronous modules or any thread for asynchronous
112 * modules.
114 * Asynchronous modules are responsible for serializing/locking every
115 * output calls in any thread as long as the decoder_UpdateVideoFormat() or
116 * decoder_NewPicture() calls are not concurrent, cf.
117 * decoder_UpdateVideoFormat() and decoder_NewPicture() notes.
119 * The ModuleThread is the owner of these variables, it should hold
120 * the lock when writing them but doesn't have to hold it when using them.
122 * The DecoderThread should always hold the lock when reading/using
123 * aout/vouts.
125 * The input thread can read these variables in order to stop outputs, when
126 * both ModuleThread and DecoderThread are stopped (from DecoderDelete()).
128 audio_output_t *p_aout;
130 vout_thread_t *p_vout;
131 bool vout_started;
132 enum vlc_vout_order vout_order;
134 /* -- Theses variables need locking on read *and* write -- */
135 /* Preroll */
136 vlc_tick_t i_preroll_end;
138 #define PREROLL_NONE INT64_MIN // vlc_tick_t
139 #define PREROLL_FORCED INT64_MAX // vlc_tick_t
141 /* Pause & Rate */
142 bool reset_out_state;
143 vlc_tick_t pause_date;
144 vlc_tick_t delay;
145 float request_rate, output_rate;
146 unsigned frames_countdown;
147 bool paused;
149 bool error;
151 /* Waiting */
152 bool b_waiting;
153 bool b_first;
154 bool b_has_data;
156 /* Flushing */
157 bool flushing;
158 bool b_draining;
159 atomic_bool drained;
160 bool b_idle;
161 bool aborting;
163 /* CC */
164 #define MAX_CC_DECODERS 64 /* The es_out only creates one type of es */
165 struct
167 bool b_supported;
168 decoder_cc_desc_t desc;
169 vlc_input_decoder_t *pp_decoder[MAX_CC_DECODERS];
170 bool b_sout_created;
171 sout_packetizer_input_t *p_sout_input;
172 } cc;
174 /* Mouse event */
175 vlc_mutex_t mouse_lock;
176 vlc_mouse_event mouse_event;
177 void *mouse_opaque;
180 /* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
181 * a bogus PTS and won't be displayed */
182 #define DECODER_BOGUS_VIDEO_DELAY ((vlc_tick_t)(DEFAULT_PTS_DELAY * 30))
184 /* */
185 #define DECODER_SPU_VOUT_WAIT_DURATION VLC_TICK_FROM_MS(200)
186 #define BLOCK_FLAG_CORE_PRIVATE_RELOADED (1 << BLOCK_FLAG_CORE_PRIVATE_SHIFT)
188 #define decoder_Notify(decoder_priv, event, ...) \
189 if (decoder_priv->cbs && decoder_priv->cbs->event) \
190 decoder_priv->cbs->event(decoder_priv, __VA_ARGS__, \
191 decoder_priv->cbs_userdata);
193 static inline vlc_input_decoder_t *dec_get_owner( decoder_t *p_dec )
195 return container_of( p_dec, vlc_input_decoder_t, dec );
199 * Load a decoder module
201 static int LoadDecoder( decoder_t *p_dec, bool b_packetizer,
202 const es_format_t *restrict p_fmt )
204 decoder_Init( p_dec, p_fmt );
206 p_dec->b_frame_drop_allowed = true;
208 /* Find a suitable decoder/packetizer module */
209 if( !b_packetizer )
211 static const char caps[ES_CATEGORY_COUNT][16] = {
212 [VIDEO_ES] = "video decoder",
213 [AUDIO_ES] = "audio decoder",
214 [SPU_ES] = "spu decoder",
216 p_dec->p_module = module_need_var( p_dec, caps[p_dec->fmt_in.i_cat],
217 "codec" );
219 else
220 p_dec->p_module = module_need_var( p_dec, "packetizer", "packetizer" );
222 if( !p_dec->p_module )
224 decoder_Clean( p_dec );
225 return -1;
227 return 0;
230 static int DecoderThread_Reload( vlc_input_decoder_t *p_owner,
231 const es_format_t *restrict p_fmt,
232 enum reload reload )
234 /* Copy p_fmt since it can be destroyed by decoder_Clean */
235 decoder_t *p_dec = &p_owner->dec;
236 es_format_t fmt_in;
237 if( es_format_Copy( &fmt_in, p_fmt ) != VLC_SUCCESS )
239 p_owner->error = true;
240 return VLC_EGENERIC;
243 /* Restart the decoder module */
244 decoder_Clean( p_dec );
245 p_owner->error = false;
247 if( reload == RELOAD_DECODER_AOUT )
249 assert( p_owner->fmt.i_cat == AUDIO_ES );
250 audio_output_t *p_aout = p_owner->p_aout;
251 // no need to lock, the decoder and ModuleThread are dead
252 p_owner->p_aout = NULL;
253 if( p_aout )
255 aout_DecDelete( p_aout );
256 input_resource_PutAout( p_owner->p_resource, p_aout );
260 if( LoadDecoder( p_dec, false, &fmt_in ) )
262 p_owner->error = true;
263 es_format_Clean( &fmt_in );
264 return VLC_EGENERIC;
266 es_format_Clean( &fmt_in );
267 return VLC_SUCCESS;
270 static void DecoderUpdateFormatLocked( vlc_input_decoder_t *p_owner )
272 decoder_t *p_dec = &p_owner->dec;
274 vlc_mutex_assert( &p_owner->lock );
276 es_format_Clean( &p_owner->fmt );
277 es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
279 /* Move p_description */
280 if( p_dec->p_description != NULL )
282 if( p_owner->p_description != NULL )
283 vlc_meta_Delete( p_owner->p_description );
284 p_owner->p_description = p_dec->p_description;
285 p_dec->p_description = NULL;
288 atomic_store_explicit( &p_owner->b_fmt_description, true,
289 memory_order_release );
292 static void MouseEvent( const vlc_mouse_t *newmouse, void *user_data )
294 decoder_t *dec = user_data;
295 vlc_input_decoder_t *owner = dec_get_owner( dec );
297 vlc_mutex_lock( &owner->mouse_lock );
298 if( owner->mouse_event )
299 owner->mouse_event( newmouse, owner->mouse_opaque);
300 vlc_mutex_unlock( &owner->mouse_lock );
303 /*****************************************************************************
304 * Buffers allocation callbacks for the decoders
305 *****************************************************************************/
306 static bool aout_replaygain_changed( const audio_replay_gain_t *a,
307 const audio_replay_gain_t *b )
309 for( size_t i=0; i<AUDIO_REPLAY_GAIN_MAX; i++ )
311 if( a->pb_gain[i] != b->pb_gain[i] ||
312 a->pb_peak[i] != b->pb_peak[i] ||
313 a->pb_gain[i] != b->pb_gain[i] ||
314 a->pb_peak[i] != b->pb_peak[i] )
315 return true;
317 return false;
320 static int ModuleThread_UpdateAudioFormat( decoder_t *p_dec )
322 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
324 if( p_owner->p_aout &&
325 ( !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->fmt.audio) ||
326 p_dec->fmt_out.i_codec != p_dec->fmt_out.audio.i_format ||
327 p_dec->fmt_out.i_profile != p_owner->fmt.i_profile ) )
329 audio_output_t *p_aout = p_owner->p_aout;
331 /* Parameters changed, restart the aout */
332 vlc_mutex_lock( &p_owner->lock );
333 p_owner->p_aout = NULL; // the DecoderThread should not use the old aout anymore
334 vlc_mutex_unlock( &p_owner->lock );
335 aout_DecDelete( p_aout );
337 input_resource_PutAout( p_owner->p_resource, p_aout );
340 /* Check if only replay gain has changed */
341 if( aout_replaygain_changed( &p_dec->fmt_in.audio_replay_gain,
342 &p_owner->fmt.audio_replay_gain ) )
344 p_dec->fmt_out.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
345 if( p_owner->p_aout )
347 p_owner->fmt.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
348 var_TriggerCallback( p_owner->p_aout, "audio-replay-gain-mode" );
352 if( p_owner->p_aout == NULL )
354 p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
356 audio_sample_format_t format = p_dec->fmt_out.audio;
357 aout_FormatPrepare( &format );
359 const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
360 if( i_force_dolby &&
361 format.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
363 if( i_force_dolby == 1 )
364 format.i_chan_mode |= AOUT_CHANMODE_DOLBYSTEREO;
365 else /* i_force_dolby == 2 */
366 format.i_chan_mode &= ~AOUT_CHANMODE_DOLBYSTEREO;
369 audio_output_t *p_aout;
371 p_aout = input_resource_GetAout( p_owner->p_resource );
372 if( p_aout )
374 if( aout_DecNew( p_aout, &format, p_dec->fmt_out.i_profile,
375 p_owner->p_clock,
376 &p_dec->fmt_out.audio_replay_gain ) )
378 input_resource_PutAout( p_owner->p_resource, p_aout );
379 p_aout = NULL;
383 vlc_mutex_lock( &p_owner->lock );
384 p_owner->p_aout = p_aout;
386 DecoderUpdateFormatLocked( p_owner );
387 aout_FormatPrepare( &p_owner->fmt.audio );
388 vlc_mutex_unlock( &p_owner->lock );
390 if( p_aout == NULL )
391 return -1;
393 p_dec->fmt_out.audio.i_bytes_per_frame =
394 p_owner->fmt.audio.i_bytes_per_frame;
395 p_dec->fmt_out.audio.i_frame_length =
396 p_owner->fmt.audio.i_frame_length;
398 vlc_fifo_Lock( p_owner->p_fifo );
399 p_owner->reset_out_state = true;
400 vlc_fifo_Unlock( p_owner->p_fifo );
402 return 0;
405 static int CreateVoutIfNeeded(vlc_input_decoder_t *);
408 static int ModuleThread_UpdateVideoFormat( decoder_t *p_dec, vlc_video_context *vctx )
410 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
412 int created_vout = CreateVoutIfNeeded(p_owner);
413 if (created_vout == -1)
414 return -1; // error
415 if (created_vout == 0)
417 // video context didn't change
418 if (vctx != NULL && p_owner->vctx == vctx)
419 return 0;
421 assert(p_owner->p_vout);
423 if (p_owner->vctx)
424 vlc_video_context_Release(p_owner->vctx);
425 p_owner->vctx = vctx ? vlc_video_context_Hold(vctx) : NULL;
427 // configure the new vout
429 if ( p_owner->out_pool == NULL )
431 unsigned dpb_size;
432 switch( p_dec->fmt_in.i_codec )
434 case VLC_CODEC_HEVC:
435 case VLC_CODEC_H264:
436 case VLC_CODEC_DIRAC: /* FIXME valid ? */
437 dpb_size = 18;
438 break;
439 case VLC_CODEC_AV1:
440 dpb_size = 10;
441 break;
442 case VLC_CODEC_MP4V:
443 case VLC_CODEC_VP5:
444 case VLC_CODEC_VP6:
445 case VLC_CODEC_VP6F:
446 case VLC_CODEC_VP8:
447 dpb_size = 3;
448 break;
449 default:
450 dpb_size = 2;
451 break;
453 picture_pool_t *pool = picture_pool_NewFromFormat( &p_dec->fmt_out.video,
454 dpb_size + p_dec->i_extra_picture_buffers + 1 );
456 if( pool == NULL)
458 msg_Err(p_dec, "Failed to create a pool of %d %4.4s pictures",
459 dpb_size + p_dec->i_extra_picture_buffers + 1,
460 (char*)&p_dec->fmt_out.video.i_chroma);
461 goto error;
464 vlc_mutex_lock( &p_owner->lock );
465 p_owner->out_pool = pool;
466 vlc_mutex_unlock( &p_owner->lock );
470 vout_configuration_t cfg = {
471 .vout = p_owner->p_vout, .clock = p_owner->p_clock, .fmt = &p_dec->fmt_out.video,
472 .mouse_event = MouseEvent, .mouse_opaque = p_dec,
474 bool has_started;
475 vout_thread_t *p_vout =
476 input_resource_RequestVout(p_owner->p_resource, vctx, &cfg, NULL,
477 &has_started);
478 if (p_vout != NULL)
480 vlc_mutex_lock( &p_owner->lock );
481 p_owner->vout_started = true;
482 vlc_mutex_unlock( &p_owner->lock );
484 vlc_fifo_Lock( p_owner->p_fifo );
485 p_owner->reset_out_state = true;
486 vlc_fifo_Unlock( p_owner->p_fifo );
488 if (has_started)
489 decoder_Notify(p_owner, on_vout_started, p_vout, p_owner->vout_order);
490 return 0;
493 error:
494 /* Clean fmt and vctx to trigger a new vout creation on the next update
495 * call */
496 vlc_mutex_lock( &p_owner->lock );
497 es_format_Clean( &p_owner->fmt );
498 vlc_mutex_unlock( &p_owner->lock );
500 if (p_owner->vctx != NULL)
502 vlc_video_context_Release(p_owner->vctx);
503 p_owner->vctx = NULL;
505 return -1;
508 static int CreateVoutIfNeeded(vlc_input_decoder_t *p_owner)
510 decoder_t *p_dec = &p_owner->dec;
511 bool need_vout = false;
513 if( p_owner->p_vout == NULL )
515 msg_Dbg(p_dec, "vout: none found");
516 need_vout = true;
518 if( p_dec->fmt_out.video.i_width != p_owner->fmt.video.i_width
519 || p_dec->fmt_out.video.i_height != p_owner->fmt.video.i_height )
521 msg_Dbg(p_dec, "vout change: decoder size");
522 need_vout = true;
524 if( p_dec->fmt_out.video.i_visible_width != p_owner->fmt.video.i_visible_width
525 || p_dec->fmt_out.video.i_visible_height != p_owner->fmt.video.i_visible_height
526 || p_dec->fmt_out.video.i_x_offset != p_owner->fmt.video.i_x_offset
527 || p_dec->fmt_out.video.i_y_offset != p_owner->fmt.video.i_y_offset )
529 msg_Dbg(p_dec, "vout change: visible size");
530 need_vout = true;
532 if( p_dec->fmt_out.i_codec != p_owner->fmt.video.i_chroma )
534 msg_Dbg(p_dec, "vout change: chroma");
535 need_vout = true;
537 if( (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->fmt.video.i_sar_den !=
538 (int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->fmt.video.i_sar_num )
540 msg_Dbg(p_dec, "vout change: SAR");
541 need_vout = true;
543 if( p_dec->fmt_out.video.orientation != p_owner->fmt.video.orientation )
545 msg_Dbg(p_dec, "vout change: orientation");
546 need_vout = true;
548 if( p_dec->fmt_out.video.multiview_mode != p_owner->fmt.video.multiview_mode )
550 msg_Dbg(p_dec, "vout change: multiview");
551 need_vout = true;
554 if( !need_vout )
555 return 0; // vout unchanged
557 vlc_mutex_lock( &p_owner->lock );
559 vout_thread_t *p_vout = p_owner->p_vout;
560 p_owner->p_vout = NULL; // the DecoderThread should not use the old vout anymore
561 p_owner->vout_started = false;
562 vlc_mutex_unlock( &p_owner->lock );
564 enum vlc_vout_order order;
565 const vout_configuration_t cfg = { .vout = p_vout, .fmt = NULL };
566 p_vout = input_resource_RequestVout( p_owner->p_resource, NULL, &cfg, &order, NULL );
568 vlc_mutex_lock( &p_owner->lock );
569 p_owner->p_vout = p_vout;
570 p_owner->vout_order = order;
572 DecoderUpdateFormatLocked( p_owner );
573 p_owner->fmt.video.i_chroma = p_dec->fmt_out.i_codec;
574 picture_pool_t *pool = p_owner->out_pool;
575 p_owner->out_pool = NULL;
576 vlc_mutex_unlock( &p_owner->lock );
578 if ( pool != NULL )
579 picture_pool_Release( pool );
581 if( p_vout == NULL )
583 msg_Err( p_dec, "failed to create video output" );
584 return -1;
587 return 1; // new vout was created
590 static vlc_decoder_device * ModuleThread_GetDecoderDevice( decoder_t *p_dec )
592 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
594 /* Requesting a decoder device will automatically enable hw decoding */
595 if( !var_InheritBool( p_dec, "hw-dec" ) )
596 return NULL;
598 int created_vout = CreateVoutIfNeeded(p_owner);
599 if (created_vout == -1)
600 return NULL; // error
602 assert(p_owner->p_vout);
603 vlc_decoder_device *dec_device = vout_GetDevice(p_owner->p_vout);
604 if (created_vout == 1)
605 return dec_device; // new vout was created with a decoder device
607 bool need_format_update = false;
608 if ( memcmp( &p_dec->fmt_out.video.mastering,
609 &p_owner->fmt.video.mastering,
610 sizeof(p_owner->fmt.video.mastering)) )
612 msg_Dbg(p_dec, "vout update: mastering data");
613 need_format_update = true;
615 if ( p_dec->fmt_out.video.lighting.MaxCLL !=
616 p_owner->fmt.video.lighting.MaxCLL ||
617 p_dec->fmt_out.video.lighting.MaxFALL !=
618 p_owner->fmt.video.lighting.MaxFALL )
620 msg_Dbg(p_dec, "vout update: lighting data");
621 need_format_update = true;
624 if ( need_format_update )
626 /* the format has changed but we don't need a new vout */
627 vlc_mutex_lock( &p_owner->lock );
628 DecoderUpdateFormatLocked( p_owner );
629 vlc_mutex_unlock( &p_owner->lock );
631 return dec_device;
634 static picture_t *ModuleThread_NewVideoBuffer( decoder_t *p_dec )
636 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
637 assert( p_owner->p_vout );
638 assert( p_owner->out_pool );
640 picture_t *pic = picture_pool_Wait( p_owner->out_pool );
641 if (pic)
642 picture_Reset( pic );
643 return pic;
646 static subpicture_t *ModuleThread_NewSpuBuffer( decoder_t *p_dec,
647 const subpicture_updater_t *p_updater )
649 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
650 vout_thread_t *p_vout = NULL;
651 subpicture_t *p_subpic;
652 int i_attempts = 30;
654 while( i_attempts-- )
656 if( p_owner->error )
657 break;
659 p_vout = input_resource_HoldVout( p_owner->p_resource );
660 if( p_vout )
661 break;
663 vlc_tick_sleep( DECODER_SPU_VOUT_WAIT_DURATION );
666 if( !p_vout )
668 msg_Warn( p_dec, "no vout found, dropping subpicture" );
669 if( p_owner->p_vout )
671 assert(p_owner->i_spu_channel != VOUT_SPU_CHANNEL_INVALID);
672 decoder_Notify(p_owner, on_vout_stopped, p_owner->p_vout);
674 vlc_mutex_lock( &p_owner->lock );
675 vout_UnregisterSubpictureChannel(p_owner->p_vout,
676 p_owner->i_spu_channel);
677 p_owner->i_spu_channel = VOUT_SPU_CHANNEL_INVALID;
679 vout_Release(p_owner->p_vout);
680 p_owner->p_vout = NULL; // the DecoderThread should not use the old vout anymore
681 vlc_mutex_unlock( &p_owner->lock );
683 return NULL;
686 if( p_owner->p_vout != p_vout )
688 if (p_owner->p_vout) /* notify the previous vout deletion unlocked */
689 decoder_Notify(p_owner, on_vout_stopped, p_owner->p_vout);
691 vlc_mutex_lock(&p_owner->lock);
693 if (p_owner->p_vout)
695 /* Unregister the SPU channel of the previous vout */
696 assert(p_owner->i_spu_channel != VOUT_SPU_CHANNEL_INVALID);
697 vout_UnregisterSubpictureChannel(p_owner->p_vout,
698 p_owner->i_spu_channel);
699 vout_Release(p_owner->p_vout);
700 p_owner->p_vout = NULL; // the DecoderThread should not use the old vout anymore
703 enum vlc_vout_order channel_order;
704 p_owner->i_spu_channel =
705 vout_RegisterSubpictureChannelInternal(p_vout, p_owner->p_clock,
706 &channel_order);
707 p_owner->i_spu_order = 0;
709 if (p_owner->i_spu_channel == VOUT_SPU_CHANNEL_INVALID)
711 /* The new vout doesn't support SPU, aborting... */
712 vlc_mutex_unlock(&p_owner->lock);
713 vout_Release(p_vout);
714 return NULL;
717 p_owner->p_vout = p_vout;
718 p_owner->vout_order = channel_order;
719 vlc_mutex_unlock(&p_owner->lock);
721 assert(channel_order != VLC_VOUT_ORDER_NONE);
722 decoder_Notify(p_owner, on_vout_started, p_vout, channel_order);
724 else
725 vout_Release(p_vout);
727 p_subpic = subpicture_New( p_updater );
728 if( p_subpic )
730 p_subpic->i_channel = p_owner->i_spu_channel;
731 p_subpic->i_order = p_owner->i_spu_order++;
732 p_subpic->b_subtitle = true;
735 return p_subpic;
738 static int InputThread_GetInputAttachments( decoder_t *p_dec,
739 input_attachment_t ***ppp_attachment,
740 int *pi_attachment )
742 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
743 if (!p_owner->cbs || !p_owner->cbs->get_attachments)
744 return VLC_ENOOBJ;
746 int ret = p_owner->cbs->get_attachments(p_owner, ppp_attachment,
747 p_owner->cbs_userdata);
748 if (ret < 0)
749 return VLC_EGENERIC;
750 *pi_attachment = ret;
751 return VLC_SUCCESS;
754 static vlc_tick_t ModuleThread_GetDisplayDate( decoder_t *p_dec,
755 vlc_tick_t system_now, vlc_tick_t i_ts )
757 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
759 vlc_mutex_lock( &p_owner->lock );
760 if( p_owner->b_waiting || p_owner->paused )
761 i_ts = VLC_TICK_INVALID;
762 float rate = p_owner->output_rate;
763 vlc_mutex_unlock( &p_owner->lock );
765 if( !p_owner->p_clock || i_ts == VLC_TICK_INVALID )
766 return i_ts;
768 return vlc_clock_ConvertToSystem( p_owner->p_clock, system_now, i_ts, rate );
771 static float ModuleThread_GetDisplayRate( decoder_t *p_dec )
773 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
775 if( !p_owner->p_clock )
776 return 1.f;
777 vlc_mutex_lock( &p_owner->lock );
778 float rate = p_owner->output_rate;
779 vlc_mutex_unlock( &p_owner->lock );
780 return rate;
783 /*****************************************************************************
784 * Public functions
785 *****************************************************************************/
786 block_t *decoder_NewAudioBuffer( decoder_t *dec, int samples )
788 assert( dec->fmt_out.audio.i_frame_length > 0
789 && dec->fmt_out.audio.i_bytes_per_frame > 0 );
791 size_t length = samples * dec->fmt_out.audio.i_bytes_per_frame
792 / dec->fmt_out.audio.i_frame_length;
793 block_t *block = block_Alloc( length );
794 if( likely(block != NULL) )
796 block->i_nb_samples = samples;
797 block->i_pts = block->i_length = 0;
799 return block;
802 static void RequestReload( vlc_input_decoder_t *p_owner )
804 /* Don't override reload if it's RELOAD_DECODER_AOUT */
805 int expected = RELOAD_NO_REQUEST;
806 atomic_compare_exchange_strong( &p_owner->reload, &expected, RELOAD_DECODER );
809 static void DecoderWaitUnblock( vlc_input_decoder_t *p_owner )
811 vlc_mutex_assert( &p_owner->lock );
813 for( ;; )
815 if( !p_owner->b_waiting || !p_owner->b_has_data )
816 break;
817 vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
821 static inline void DecoderUpdatePreroll( vlc_tick_t *pi_preroll, const block_t *p )
823 if( p->i_flags & BLOCK_FLAG_PREROLL )
824 *pi_preroll = PREROLL_FORCED;
825 /* Check if we can use the packet for end of preroll */
826 else if( (p->i_flags & BLOCK_FLAG_DISCONTINUITY) &&
827 (p->i_buffer == 0 || (p->i_flags & BLOCK_FLAG_CORRUPTED)) )
828 *pi_preroll = PREROLL_FORCED;
829 else if( p->i_dts != VLC_TICK_INVALID )
830 *pi_preroll = __MIN( *pi_preroll, p->i_dts );
831 else if( p->i_pts != VLC_TICK_INVALID )
832 *pi_preroll = __MIN( *pi_preroll, p->i_pts );
835 #ifdef ENABLE_SOUT
836 static int DecoderThread_PlaySout( vlc_input_decoder_t *p_owner, block_t *p_sout_block )
838 assert( !p_sout_block->p_next );
840 vlc_mutex_lock( &p_owner->lock );
842 if( p_owner->b_waiting )
844 p_owner->b_has_data = true;
845 vlc_cond_signal( &p_owner->wait_acknowledge );
848 DecoderWaitUnblock( p_owner );
850 vlc_mutex_unlock( &p_owner->lock );
852 /* FIXME --VLC_TICK_INVALID inspect stream_output*/
853 return sout_InputSendBuffer( p_owner->p_sout, p_owner->p_sout_input,
854 p_sout_block );
857 /* This function process a block for sout
859 static void DecoderThread_ProcessSout( vlc_input_decoder_t *p_owner, block_t *p_block )
861 decoder_t *p_dec = &p_owner->dec;
862 block_t *p_sout_block;
863 block_t **pp_block = p_block ? &p_block : NULL;
865 while( ( p_sout_block =
866 p_dec->pf_packetize( p_dec, pp_block ) ) )
868 if( p_owner->p_sout_input == NULL )
870 vlc_mutex_lock( &p_owner->lock );
871 DecoderUpdateFormatLocked( p_owner );
873 p_owner->fmt.i_group = p_dec->fmt_in.i_group;
874 p_owner->fmt.i_id = p_dec->fmt_in.i_id;
875 if( p_dec->fmt_in.psz_language )
877 free( p_owner->fmt.psz_language );
878 p_owner->fmt.psz_language =
879 strdup( p_dec->fmt_in.psz_language );
881 vlc_mutex_unlock( &p_owner->lock );
883 p_owner->p_sout_input =
884 sout_InputNew( p_owner->p_sout, &p_owner->fmt );
886 if( p_owner->p_sout_input == NULL )
888 msg_Err( p_dec, "cannot create packetized sout output (%4.4s)",
889 (char *)&p_owner->fmt.i_codec );
890 p_owner->error = true;
892 if(p_block)
893 block_Release(p_block);
895 block_ChainRelease(p_sout_block);
896 break;
900 while( p_sout_block )
902 block_t *p_next = p_sout_block->p_next;
904 p_sout_block->p_next = NULL;
906 if( p_owner->p_sout->b_wants_substreams && p_dec->pf_get_cc )
908 if( p_owner->cc.p_sout_input ||
909 !p_owner->cc.b_sout_created )
911 decoder_cc_desc_t desc;
912 block_t *p_cc = p_dec->pf_get_cc( p_dec, &desc );
913 if( p_cc )
915 if(!p_owner->cc.b_sout_created)
917 es_format_t ccfmt;
918 es_format_Init(&ccfmt, SPU_ES, VLC_CODEC_CEA608);
919 ccfmt.i_group = p_owner->fmt.i_group;
920 ccfmt.subs.cc.i_reorder_depth = desc.i_reorder_depth;
921 p_owner->cc.p_sout_input = sout_InputNew( p_owner->p_sout, &ccfmt );
922 es_format_Clean(&ccfmt);
923 p_owner->cc.b_sout_created = true;
926 if( !p_owner->cc.p_sout_input ||
927 sout_InputSendBuffer( p_owner->p_sout, p_owner->cc.p_sout_input, p_cc ) )
929 block_Release( p_cc );
935 if( DecoderThread_PlaySout( p_owner, p_sout_block ) == VLC_EGENERIC )
937 msg_Err( p_dec, "cannot continue streaming due to errors with codec %4.4s",
938 (char *)&p_owner->fmt.i_codec );
940 p_owner->error = true;
942 /* Cleanup */
944 if( p_block )
945 block_Release( p_block );
947 block_ChainRelease( p_next );
948 return;
951 p_sout_block = p_next;
955 #endif
957 static void DecoderPlayCc( vlc_input_decoder_t *p_owner, block_t *p_cc,
958 const decoder_cc_desc_t *p_desc )
960 vlc_mutex_lock( &p_owner->lock );
962 p_owner->cc.desc = *p_desc;
964 /* Fanout data to all decoders. We do not know if es_out
965 selected 608 or 708. */
966 uint64_t i_bitmap = p_owner->cc.desc.i_608_channels |
967 p_owner->cc.desc.i_708_channels;
969 for( int i=0; i_bitmap > 0; i_bitmap >>= 1, i++ )
971 vlc_input_decoder_t *p_ccowner = p_owner->cc.pp_decoder[i];
972 if( !p_ccowner )
973 continue;
975 if( i_bitmap > 1 )
977 block_FifoPut( p_ccowner->p_fifo, block_Duplicate(p_cc) );
979 else
981 block_FifoPut( p_ccowner->p_fifo, p_cc );
982 p_cc = NULL; /* was last dec */
986 vlc_mutex_unlock( &p_owner->lock );
988 if( p_cc ) /* can have bitmap set but no created decs */
989 block_Release( p_cc );
992 static void PacketizerGetCc( vlc_input_decoder_t *p_owner, decoder_t *p_dec_cc )
994 block_t *p_cc;
995 decoder_cc_desc_t desc;
997 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
998 if( !p_owner->cc.b_supported )
999 return;
1001 assert( p_dec_cc->pf_get_cc != NULL );
1003 p_cc = p_dec_cc->pf_get_cc( p_dec_cc, &desc );
1004 if( !p_cc )
1005 return;
1006 DecoderPlayCc( p_owner, p_cc, &desc );
1009 static void ModuleThread_QueueCc( decoder_t *p_videodec, block_t *p_cc,
1010 const decoder_cc_desc_t *p_desc )
1012 vlc_input_decoder_t *p_owner = dec_get_owner( p_videodec );
1014 if( unlikely( p_cc != NULL ) )
1016 if( p_owner->cc.b_supported &&
1017 ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1018 DecoderPlayCc( p_owner, p_cc, p_desc );
1019 else
1020 block_Release( p_cc );
1024 static int ModuleThread_PlayVideo( vlc_input_decoder_t *p_owner, picture_t *p_picture )
1026 decoder_t *p_dec = &p_owner->dec;
1027 vout_thread_t *p_vout = p_owner->p_vout;
1029 if( p_picture->date == VLC_TICK_INVALID )
1030 /* FIXME: VLC_TICK_INVALID -- verify video_output */
1032 msg_Warn( p_dec, "non-dated video buffer received" );
1033 picture_Release( p_picture );
1034 return VLC_EGENERIC;
1037 vlc_mutex_lock( &p_owner->lock );
1038 assert( p_owner->vout_started );
1040 bool prerolled = p_owner->i_preroll_end != PREROLL_NONE;
1041 if( prerolled && p_owner->i_preroll_end > p_picture->date )
1043 vlc_mutex_unlock( &p_owner->lock );
1044 picture_Release( p_picture );
1045 return VLC_SUCCESS;
1048 p_owner->i_preroll_end = PREROLL_NONE;
1050 if( unlikely(prerolled) )
1052 msg_Dbg( p_dec, "end of video preroll" );
1054 if( p_vout )
1055 vout_FlushAll( p_vout );
1058 if( p_owner->b_waiting && !p_owner->b_first )
1060 p_owner->b_has_data = true;
1061 vlc_cond_signal( &p_owner->wait_acknowledge );
1064 DecoderWaitUnblock( p_owner );
1066 if( p_owner->b_waiting )
1068 assert( p_owner->b_first );
1069 msg_Dbg( p_dec, "Received first picture" );
1070 p_owner->b_first = false;
1071 p_picture->b_force = true;
1074 vlc_mutex_unlock( &p_owner->lock );
1076 /* FIXME: The *input* FIFO should not be locked here. This will not work
1077 * properly if/when pictures are queued asynchronously. */
1078 vlc_fifo_Lock( p_owner->p_fifo );
1079 if( unlikely(p_owner->paused) && likely(p_owner->frames_countdown > 0) )
1080 p_owner->frames_countdown--;
1081 vlc_fifo_Unlock( p_owner->p_fifo );
1083 /* */
1084 if( p_vout == NULL )
1086 picture_Release( p_picture );
1087 return VLC_EGENERIC;
1090 if( p_picture->b_still )
1092 /* Ensure no earlier higher pts breaks still state */
1093 vout_Flush( p_vout, p_picture->date );
1095 vout_PutPicture( p_vout, p_picture );
1097 return VLC_SUCCESS;
1100 static void ModuleThread_UpdateStatVideo( vlc_input_decoder_t *p_owner,
1101 bool lost )
1103 unsigned displayed = 0;
1104 unsigned vout_lost = 0;
1105 unsigned vout_late = 0;
1106 if( p_owner->p_vout != NULL )
1108 vout_GetResetStatistic( p_owner->p_vout, &displayed, &vout_lost, &vout_late );
1110 if (lost) vout_lost++;
1112 decoder_Notify(p_owner, on_new_video_stats, 1, vout_lost, displayed, vout_late);
1115 static void ModuleThread_QueueVideo( decoder_t *p_dec, picture_t *p_pic )
1117 assert( p_pic );
1118 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
1120 int success = ModuleThread_PlayVideo( p_owner, p_pic );
1122 ModuleThread_UpdateStatVideo( p_owner, success != VLC_SUCCESS );
1125 static vlc_decoder_device * thumbnailer_get_device( decoder_t *p_dec )
1127 VLC_UNUSED(p_dec);
1128 // no hardware decoder on purpose
1129 // we don't want to load many DLLs and allocate many pictures
1130 // just to decode one picture
1131 return NULL;
1134 static picture_t *thumbnailer_buffer_new( decoder_t *p_dec )
1136 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
1137 /* Avoid decoding more than one frame when a thumbnail was
1138 * already generated */
1139 vlc_mutex_lock( &p_owner->lock );
1140 if( !p_owner->b_first )
1142 vlc_mutex_unlock( &p_owner->lock );
1143 return NULL;
1145 vlc_mutex_unlock( &p_owner->lock );
1146 return picture_NewFromFormat( &p_dec->fmt_out.video );
1149 static void ModuleThread_QueueThumbnail( decoder_t *p_dec, picture_t *p_pic )
1151 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
1152 bool b_first;
1154 vlc_mutex_lock( &p_owner->lock );
1155 b_first = p_owner->b_first;
1156 p_owner->b_first = false;
1157 vlc_mutex_unlock( &p_owner->lock );
1159 if( b_first )
1160 decoder_Notify(p_owner, on_thumbnail_ready, p_pic);
1161 picture_Release( p_pic );
1165 static int ModuleThread_PlayAudio( vlc_input_decoder_t *p_owner, block_t *p_audio )
1167 decoder_t *p_dec = &p_owner->dec;
1169 assert( p_audio != NULL );
1171 if( p_audio->i_pts == VLC_TICK_INVALID ) // FIXME --VLC_TICK_INVALID verify audio_output/*
1173 msg_Warn( p_dec, "non-dated audio buffer received" );
1174 block_Release( p_audio );
1175 return VLC_EGENERIC;
1178 vlc_mutex_lock( &p_owner->lock );
1179 bool prerolled = p_owner->i_preroll_end != PREROLL_NONE;
1180 if( prerolled && p_owner->i_preroll_end > p_audio->i_pts )
1182 vlc_mutex_unlock( &p_owner->lock );
1183 block_Release( p_audio );
1184 return VLC_SUCCESS;
1187 p_owner->i_preroll_end = PREROLL_NONE;
1188 vlc_mutex_unlock( &p_owner->lock );
1190 if( unlikely(prerolled) )
1192 msg_Dbg( p_dec, "end of audio preroll" );
1194 if( p_owner->p_aout )
1195 aout_DecFlush( p_owner->p_aout );
1198 /* */
1199 /* */
1200 vlc_mutex_lock( &p_owner->lock );
1201 if( p_owner->b_waiting )
1203 p_owner->b_has_data = true;
1204 vlc_cond_signal( &p_owner->wait_acknowledge );
1207 /* */
1208 DecoderWaitUnblock( p_owner );
1209 vlc_mutex_unlock( &p_owner->lock );
1211 audio_output_t *p_aout = p_owner->p_aout;
1213 if( p_aout == NULL )
1215 msg_Dbg( p_dec, "discarded audio buffer" );
1216 block_Release( p_audio );
1217 return VLC_EGENERIC;
1220 int status = aout_DecPlay( p_aout, p_audio );
1221 if( status == AOUT_DEC_CHANGED )
1223 /* Only reload the decoder */
1224 RequestReload( p_owner );
1226 else if( status == AOUT_DEC_FAILED )
1228 /* If we reload because the aout failed, we should release it. That
1229 * way, a next call to ModuleThread_UpdateAudioFormat() won't re-use the
1230 * previous (failing) aout but will try to create a new one. */
1231 atomic_store( &p_owner->reload, RELOAD_DECODER_AOUT );
1233 return VLC_SUCCESS;
1236 static void ModuleThread_UpdateStatAudio( vlc_input_decoder_t *p_owner,
1237 bool lost )
1239 unsigned played = 0;
1240 unsigned aout_lost = 0;
1241 if( p_owner->p_aout != NULL )
1243 aout_DecGetResetStats( p_owner->p_aout, &aout_lost, &played );
1245 if (lost) aout_lost++;
1247 decoder_Notify(p_owner, on_new_audio_stats, 1, aout_lost, played);
1250 static void ModuleThread_QueueAudio( decoder_t *p_dec, block_t *p_aout_buf )
1252 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
1254 int success = ModuleThread_PlayAudio( p_owner, p_aout_buf );
1256 ModuleThread_UpdateStatAudio( p_owner, success != VLC_SUCCESS );
1259 static void ModuleThread_PlaySpu( vlc_input_decoder_t *p_owner, subpicture_t *p_subpic )
1261 decoder_t *p_dec = &p_owner->dec;
1262 vout_thread_t *p_vout = p_owner->p_vout;
1264 /* */
1265 if( p_subpic->i_start == VLC_TICK_INVALID )
1267 msg_Warn( p_dec, "non-dated spu buffer received" );
1268 subpicture_Delete( p_subpic );
1269 return;
1272 /* */
1273 vlc_mutex_lock( &p_owner->lock );
1275 if( p_owner->b_waiting )
1277 p_owner->b_has_data = true;
1278 vlc_cond_signal( &p_owner->wait_acknowledge );
1281 DecoderWaitUnblock( p_owner );
1282 vlc_mutex_unlock( &p_owner->lock );
1284 if( p_subpic->i_start == VLC_TICK_INVALID )
1286 subpicture_Delete( p_subpic );
1287 return;
1290 vout_PutSubpicture( p_vout, p_subpic );
1293 static void ModuleThread_QueueSpu( decoder_t *p_dec, subpicture_t *p_spu )
1295 assert( p_spu );
1296 vlc_input_decoder_t *p_owner = dec_get_owner( p_dec );
1298 /* The vout must be created from a previous decoder_NewSubpicture call. */
1299 assert( p_owner->p_vout );
1301 /* Preroll does not work very well with subtitle */
1302 vlc_mutex_lock( &p_owner->lock );
1303 if( p_spu->i_start != VLC_TICK_INVALID &&
1304 p_spu->i_start < p_owner->i_preroll_end &&
1305 ( p_spu->i_stop == VLC_TICK_INVALID || p_spu->i_stop < p_owner->i_preroll_end ) )
1307 vlc_mutex_unlock( &p_owner->lock );
1308 subpicture_Delete( p_spu );
1310 else
1312 vlc_mutex_unlock( &p_owner->lock );
1313 ModuleThread_PlaySpu( p_owner, p_spu );
1317 static void DecoderThread_ProcessInput( vlc_input_decoder_t *p_owner, block_t *p_block );
1318 static void DecoderThread_DecodeBlock( vlc_input_decoder_t *p_owner, block_t *p_block )
1320 decoder_t *p_dec = &p_owner->dec;
1322 int ret = p_dec->pf_decode( p_dec, p_block );
1323 switch( ret )
1325 case VLCDEC_SUCCESS:
1326 break;
1327 case VLCDEC_ECRITICAL:
1328 p_owner->error = true;
1329 break;
1330 case VLCDEC_RELOAD:
1331 RequestReload( p_owner );
1332 if( unlikely( p_block == NULL ) )
1333 break;
1334 if( !( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1336 p_block->i_flags |= BLOCK_FLAG_CORE_PRIVATE_RELOADED;
1337 DecoderThread_ProcessInput( p_owner, p_block );
1339 else /* We prefer loosing this block than an infinite recursion */
1340 block_Release( p_block );
1341 break;
1342 default:
1343 vlc_assert_unreachable();
1348 * Decode a block
1350 * \param p_dec the decoder object
1351 * \param p_block the block to decode
1353 static void DecoderThread_ProcessInput( vlc_input_decoder_t *p_owner, block_t *p_block )
1355 decoder_t *p_dec = &p_owner->dec;
1357 if( p_owner->error )
1358 goto error;
1360 /* Here, the atomic doesn't prevent to miss a reload request.
1361 * DecoderThread_ProcessInput() can still be called after the decoder module or the
1362 * audio output requested a reload. This will only result in a drop of an
1363 * input block or an output buffer. */
1364 enum reload reload;
1365 if( ( reload = atomic_exchange( &p_owner->reload, RELOAD_NO_REQUEST ) ) )
1367 msg_Warn( p_dec, "Reloading the decoder module%s",
1368 reload == RELOAD_DECODER_AOUT ? " and the audio output" : "" );
1370 if( DecoderThread_Reload( p_owner, &p_dec->fmt_in, reload ) != VLC_SUCCESS )
1371 goto error;
1374 bool packetize = p_owner->p_packetizer != NULL;
1375 if( p_block )
1377 if( p_block->i_buffer <= 0 )
1378 goto error;
1380 vlc_mutex_lock( &p_owner->lock );
1381 DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1382 vlc_mutex_unlock( &p_owner->lock );
1383 if( unlikely( p_block->i_flags & BLOCK_FLAG_CORE_PRIVATE_RELOADED ) )
1385 /* This block has already been packetized */
1386 packetize = false;
1390 #ifdef ENABLE_SOUT
1391 if( p_owner->p_sout != NULL )
1393 DecoderThread_ProcessSout( p_owner, p_block );
1394 return;
1396 #endif
1397 if( packetize )
1399 block_t *p_packetized_block;
1400 block_t **pp_block = p_block ? &p_block : NULL;
1401 decoder_t *p_packetizer = p_owner->p_packetizer;
1403 while( (p_packetized_block =
1404 p_packetizer->pf_packetize( p_packetizer, pp_block ) ) )
1406 if( !es_format_IsSimilar( &p_dec->fmt_in, &p_packetizer->fmt_out ) )
1408 msg_Dbg( p_dec, "restarting module due to input format change");
1410 /* Drain the decoder module */
1411 DecoderThread_DecodeBlock( p_owner, NULL );
1413 if( DecoderThread_Reload( p_owner, &p_packetizer->fmt_out,
1414 RELOAD_DECODER ) != VLC_SUCCESS )
1416 block_ChainRelease( p_packetized_block );
1417 return;
1421 if( p_packetizer->pf_get_cc )
1422 PacketizerGetCc( p_owner, p_packetizer );
1424 while( p_packetized_block )
1426 block_t *p_next = p_packetized_block->p_next;
1427 p_packetized_block->p_next = NULL;
1429 DecoderThread_DecodeBlock( p_owner, p_packetized_block );
1430 if( p_owner->error )
1432 block_ChainRelease( p_next );
1433 return;
1436 p_packetized_block = p_next;
1439 /* Drain the decoder after the packetizer is drained */
1440 if( !pp_block )
1441 DecoderThread_DecodeBlock( p_owner, NULL );
1443 else
1444 DecoderThread_DecodeBlock( p_owner, p_block );
1445 return;
1447 error:
1448 if( p_block )
1449 block_Release( p_block );
1452 static void DecoderThread_Flush( vlc_input_decoder_t *p_owner )
1454 decoder_t *p_dec = &p_owner->dec;
1455 decoder_t *p_packetizer = p_owner->p_packetizer;
1457 if( p_owner->error )
1458 return;
1460 if( p_packetizer != NULL && p_packetizer->pf_flush != NULL )
1461 p_packetizer->pf_flush( p_packetizer );
1463 if ( p_dec->pf_flush != NULL )
1464 p_dec->pf_flush( p_dec );
1466 /* flush CC sub decoders */
1467 if( p_owner->cc.b_supported )
1469 for( int i=0; i<MAX_CC_DECODERS; i++ )
1471 vlc_input_decoder_t *p_ccowner = p_owner->cc.pp_decoder[i];
1472 if( p_ccowner && p_ccowner->dec.pf_flush )
1473 p_ccowner->dec.pf_flush( &p_ccowner->dec );
1477 vlc_mutex_lock( &p_owner->lock );
1478 #ifdef ENABLE_SOUT
1479 if ( p_owner->p_sout_input != NULL )
1481 sout_InputFlush( p_owner->p_sout, p_owner->p_sout_input );
1483 #endif
1484 if( p_dec->fmt_out.i_cat == AUDIO_ES )
1486 if( p_owner->p_aout )
1487 aout_DecFlush( p_owner->p_aout );
1489 else if( p_dec->fmt_out.i_cat == VIDEO_ES )
1491 if( p_owner->p_vout && p_owner->vout_started )
1492 vout_FlushAll( p_owner->p_vout );
1494 /* Reset the pool cancel state, previously set by
1495 * vlc_input_decoder_Flush() */
1496 if( p_owner->out_pool != NULL )
1497 picture_pool_Cancel( p_owner->out_pool, false );
1499 else if( p_dec->fmt_out.i_cat == SPU_ES )
1501 if( p_owner->p_vout )
1503 assert( p_owner->i_spu_channel != VOUT_SPU_CHANNEL_INVALID );
1504 vout_FlushSubpictureChannel( p_owner->p_vout, p_owner->i_spu_channel );
1508 p_owner->i_preroll_end = PREROLL_NONE;
1509 vlc_mutex_unlock( &p_owner->lock );
1512 static void DecoderThread_ChangePause( vlc_input_decoder_t *p_owner, bool paused, vlc_tick_t date )
1514 decoder_t *p_dec = &p_owner->dec;
1516 msg_Dbg( p_dec, "toggling %s", paused ? "resume" : "pause" );
1517 switch( p_dec->fmt_out.i_cat )
1519 case VIDEO_ES:
1520 vlc_mutex_lock( &p_owner->lock );
1521 if( p_owner->p_vout != NULL && p_owner->vout_started )
1522 vout_ChangePause( p_owner->p_vout, paused, date );
1523 vlc_mutex_unlock( &p_owner->lock );
1524 break;
1525 case AUDIO_ES:
1526 vlc_mutex_lock( &p_owner->lock );
1527 if( p_owner->p_aout != NULL )
1528 aout_DecChangePause( p_owner->p_aout, paused, date );
1529 vlc_mutex_unlock( &p_owner->lock );
1530 break;
1531 case SPU_ES:
1532 break;
1533 default:
1534 vlc_assert_unreachable();
1538 static void DecoderThread_ChangeRate( vlc_input_decoder_t *p_owner, float rate )
1540 decoder_t *p_dec = &p_owner->dec;
1542 msg_Dbg( p_dec, "changing rate: %f", rate );
1543 vlc_mutex_lock( &p_owner->lock );
1544 switch( p_dec->fmt_out.i_cat )
1546 case VIDEO_ES:
1547 if( p_owner->p_vout != NULL && p_owner->vout_started )
1548 vout_ChangeRate( p_owner->p_vout, rate );
1549 break;
1550 case AUDIO_ES:
1551 if( p_owner->p_aout != NULL )
1552 aout_DecChangeRate( p_owner->p_aout, rate );
1553 break;
1554 case SPU_ES:
1555 if( p_owner->p_vout != NULL )
1557 assert(p_owner->i_spu_channel != VOUT_SPU_CHANNEL_INVALID);
1558 vout_ChangeSpuRate(p_owner->p_vout, p_owner->i_spu_channel,
1559 rate );
1561 break;
1562 default:
1563 vlc_assert_unreachable();
1565 p_owner->output_rate = rate;
1566 vlc_mutex_unlock( &p_owner->lock );
1569 static void DecoderThread_ChangeDelay( vlc_input_decoder_t *p_owner, vlc_tick_t delay )
1571 decoder_t *p_dec = &p_owner->dec;
1573 msg_Dbg( p_dec, "changing delay: %"PRId64, delay );
1575 switch( p_dec->fmt_out.i_cat )
1577 case VIDEO_ES:
1578 vlc_mutex_lock( &p_owner->lock );
1579 if( p_owner->p_vout != NULL && p_owner->vout_started )
1580 vout_ChangeDelay( p_owner->p_vout, delay );
1581 vlc_mutex_unlock( &p_owner->lock );
1582 break;
1583 case AUDIO_ES:
1584 vlc_mutex_lock( &p_owner->lock );
1585 if( p_owner->p_aout != NULL )
1586 aout_DecChangeDelay( p_owner->p_aout, delay );
1587 vlc_mutex_unlock( &p_owner->lock );
1588 break;
1589 case SPU_ES:
1590 vlc_mutex_lock( &p_owner->lock );
1591 if( p_owner->p_vout != NULL )
1593 assert(p_owner->i_spu_channel != VOUT_SPU_CHANNEL_INVALID);
1594 vout_ChangeSpuDelay(p_owner->p_vout, p_owner->i_spu_channel,
1595 delay);
1597 vlc_mutex_unlock( &p_owner->lock );
1598 break;
1599 default:
1600 vlc_assert_unreachable();
1605 * The decoding main loop
1607 * \param p_dec the decoder
1609 static void *DecoderThread( void *p_data )
1611 vlc_input_decoder_t *p_owner = (vlc_input_decoder_t *)p_data;
1612 float rate = 1.f;
1613 vlc_tick_t delay = 0;
1614 bool paused = false;
1616 /* The decoder's main loop */
1617 vlc_fifo_Lock( p_owner->p_fifo );
1619 while( !p_owner->aborting )
1621 if( p_owner->flushing )
1622 { /* Flush before/regardless of pause. We do not want to resume just
1623 * for the sake of flushing (glitches could otherwise happen). */
1624 vlc_fifo_Unlock( p_owner->p_fifo );
1626 /* Flush the decoder (and the output) */
1627 DecoderThread_Flush( p_owner );
1629 vlc_fifo_Lock( p_owner->p_fifo );
1631 /* Reset flushing after DecoderThread_ProcessInput in case vlc_input_decoder_Flush
1632 * is called again. This will avoid a second useless flush (but
1633 * harmless). */
1634 p_owner->flushing = false;
1636 continue;
1639 /* Reset the original pause/rate state when a new aout/vout is created:
1640 * this will trigger the DecoderThread_ChangePause/DecoderThread_ChangeRate code path
1641 * if needed. */
1642 if( p_owner->reset_out_state )
1644 rate = 1.f;
1645 paused = false;
1646 delay = 0;
1647 p_owner->reset_out_state = false;
1650 if( paused != p_owner->paused )
1651 { /* Update playing/paused status of the output */
1652 vlc_tick_t date = p_owner->pause_date;
1654 paused = p_owner->paused;
1655 vlc_fifo_Unlock( p_owner->p_fifo );
1657 DecoderThread_ChangePause( p_owner, paused, date );
1659 vlc_fifo_Lock( p_owner->p_fifo );
1660 continue;
1663 if( rate != p_owner->request_rate )
1665 rate = p_owner->request_rate;
1666 vlc_fifo_Unlock( p_owner->p_fifo );
1668 DecoderThread_ChangeRate( p_owner, rate );
1670 vlc_fifo_Lock( p_owner->p_fifo );
1671 continue;
1674 if( delay != p_owner->delay )
1676 delay = p_owner->delay;
1677 vlc_fifo_Unlock( p_owner->p_fifo );
1679 DecoderThread_ChangeDelay( p_owner, delay );
1681 vlc_fifo_Lock( p_owner->p_fifo );
1682 continue;
1685 if( p_owner->paused && p_owner->frames_countdown == 0 )
1686 { /* Wait for resumption from pause */
1687 p_owner->b_idle = true;
1688 vlc_cond_signal( &p_owner->wait_acknowledge );
1689 vlc_fifo_Wait( p_owner->p_fifo );
1690 p_owner->b_idle = false;
1691 continue;
1694 vlc_cond_signal( &p_owner->wait_fifo );
1696 block_t *p_block = vlc_fifo_DequeueUnlocked( p_owner->p_fifo );
1697 if( p_block == NULL )
1699 if( likely(!p_owner->b_draining) )
1700 { /* Wait for a block to decode (or a request to drain) */
1701 p_owner->b_idle = true;
1702 vlc_cond_signal( &p_owner->wait_acknowledge );
1703 vlc_fifo_Wait( p_owner->p_fifo );
1704 p_owner->b_idle = false;
1705 continue;
1707 /* We have emptied the FIFO and there is a pending request to
1708 * drain. Pass p_block = NULL to decoder just once. */
1711 vlc_fifo_Unlock( p_owner->p_fifo );
1713 DecoderThread_ProcessInput( p_owner, p_block );
1715 if( p_block == NULL && p_owner->dec.fmt_out.i_cat == AUDIO_ES )
1716 { /* Draining: the decoder is drained and all decoded buffers are
1717 * queued to the output at this point. Now drain the output. */
1718 if( p_owner->p_aout != NULL )
1719 aout_DecDrain( p_owner->p_aout );
1722 /* TODO? Wait for draining instead of polling. */
1723 vlc_mutex_lock( &p_owner->lock );
1724 vlc_fifo_Lock( p_owner->p_fifo );
1725 if( p_owner->b_draining && (p_block == NULL) )
1727 p_owner->b_draining = false;
1728 p_owner->drained = true;
1730 vlc_cond_signal( &p_owner->wait_acknowledge );
1731 vlc_mutex_unlock( &p_owner->lock );
1734 vlc_fifo_Unlock( p_owner->p_fifo );
1735 return NULL;
1738 static const struct decoder_owner_callbacks dec_video_cbs =
1740 .video = {
1741 .get_device = ModuleThread_GetDecoderDevice,
1742 .format_update = ModuleThread_UpdateVideoFormat,
1743 .buffer_new = ModuleThread_NewVideoBuffer,
1744 .queue = ModuleThread_QueueVideo,
1745 .queue_cc = ModuleThread_QueueCc,
1746 .get_display_date = ModuleThread_GetDisplayDate,
1747 .get_display_rate = ModuleThread_GetDisplayRate,
1749 .get_attachments = InputThread_GetInputAttachments,
1751 static const struct decoder_owner_callbacks dec_thumbnailer_cbs =
1753 .video = {
1754 .get_device = thumbnailer_get_device,
1755 .buffer_new = thumbnailer_buffer_new,
1756 .queue = ModuleThread_QueueThumbnail,
1758 .get_attachments = InputThread_GetInputAttachments,
1760 static const struct decoder_owner_callbacks dec_audio_cbs =
1762 .audio = {
1763 .format_update = ModuleThread_UpdateAudioFormat,
1764 .queue = ModuleThread_QueueAudio,
1766 .get_attachments = InputThread_GetInputAttachments,
1768 static const struct decoder_owner_callbacks dec_spu_cbs =
1770 .spu = {
1771 .buffer_new = ModuleThread_NewSpuBuffer,
1772 .queue = ModuleThread_QueueSpu,
1774 .get_attachments = InputThread_GetInputAttachments,
1778 * Create a decoder object
1780 * \param p_input the input thread
1781 * \param p_es the es descriptor
1782 * \param b_packetizer instead of a decoder
1783 * \return the decoder object
1785 static vlc_input_decoder_t *
1786 CreateDecoder( vlc_object_t *p_parent,
1787 const es_format_t *fmt, vlc_clock_t *p_clock,
1788 input_resource_t *p_resource, sout_instance_t *p_sout,
1789 bool b_thumbnailing, const struct vlc_input_decoder_callbacks *cbs,
1790 void *cbs_userdata )
1792 decoder_t *p_dec;
1793 vlc_input_decoder_t *p_owner;
1794 static_assert(offsetof(vlc_input_decoder_t, dec) == 0,
1795 "the decoder must be first in the owner structure");
1797 p_owner = vlc_custom_create( p_parent, sizeof( *p_owner ), "decoder" );
1798 if( p_owner == NULL )
1799 return NULL;
1800 p_dec = &p_owner->dec;
1802 p_owner->p_clock = p_clock;
1803 p_owner->i_preroll_end = PREROLL_NONE;
1804 p_owner->p_resource = p_resource;
1805 p_owner->cbs = cbs;
1806 p_owner->cbs_userdata = cbs_userdata;
1807 p_owner->p_aout = NULL;
1808 p_owner->p_vout = NULL;
1809 p_owner->vout_started = false;
1810 p_owner->i_spu_channel = VOUT_SPU_CHANNEL_INVALID;
1811 p_owner->i_spu_order = 0;
1812 p_owner->p_sout = p_sout;
1813 p_owner->p_sout_input = NULL;
1814 p_owner->p_packetizer = NULL;
1816 atomic_init( &p_owner->b_fmt_description, false );
1817 p_owner->p_description = NULL;
1819 p_owner->reset_out_state = false;
1820 p_owner->delay = 0;
1821 p_owner->output_rate = p_owner->request_rate = 1.f;
1822 p_owner->paused = false;
1823 p_owner->pause_date = VLC_TICK_INVALID;
1824 p_owner->frames_countdown = 0;
1826 p_owner->b_waiting = false;
1827 p_owner->b_first = true;
1828 p_owner->b_has_data = false;
1830 p_owner->error = false;
1832 p_owner->flushing = false;
1833 p_owner->b_draining = false;
1834 p_owner->drained = false;
1835 atomic_init( &p_owner->reload, RELOAD_NO_REQUEST );
1836 p_owner->b_idle = false;
1838 p_owner->mouse_event = NULL;
1839 p_owner->mouse_opaque = NULL;
1841 es_format_Init( &p_owner->fmt, fmt->i_cat, 0 );
1843 /* decoder fifo */
1844 p_owner->p_fifo = block_FifoNew();
1845 if( unlikely(p_owner->p_fifo == NULL) )
1847 vlc_object_delete(p_dec);
1848 return NULL;
1851 vlc_mutex_init( &p_owner->lock );
1852 vlc_mutex_init( &p_owner->mouse_lock );
1853 vlc_cond_init( &p_owner->wait_request );
1854 vlc_cond_init( &p_owner->wait_acknowledge );
1855 vlc_cond_init( &p_owner->wait_fifo );
1857 /* Load a packetizer module if the input is not already packetized */
1858 if( p_sout == NULL && !fmt->b_packetized )
1860 p_owner->p_packetizer =
1861 vlc_custom_create( p_parent, sizeof( decoder_t ), "packetizer" );
1862 if( p_owner->p_packetizer )
1864 if( LoadDecoder( p_owner->p_packetizer, true, fmt ) )
1866 vlc_object_delete(p_owner->p_packetizer);
1867 p_owner->p_packetizer = NULL;
1869 else
1871 p_owner->p_packetizer->fmt_out.b_packetized = true;
1872 fmt = &p_owner->p_packetizer->fmt_out;
1877 switch( fmt->i_cat )
1879 case VIDEO_ES:
1880 if( !b_thumbnailing )
1881 p_dec->cbs = &dec_video_cbs;
1882 else
1883 p_dec->cbs = &dec_thumbnailer_cbs;
1884 break;
1885 case AUDIO_ES:
1886 p_dec->cbs = &dec_audio_cbs;
1887 break;
1888 case SPU_ES:
1889 p_dec->cbs = &dec_spu_cbs;
1890 break;
1891 default:
1892 msg_Err( p_dec, "unknown ES format" );
1893 return p_owner;
1896 /* Find a suitable decoder/packetizer module */
1897 if( LoadDecoder( p_dec, p_sout != NULL, fmt ) )
1898 return p_owner;
1900 assert( p_dec->fmt_in.i_cat == p_dec->fmt_out.i_cat && fmt->i_cat == p_dec->fmt_in.i_cat);
1902 /* Copy ourself the input replay gain */
1903 if( fmt->i_cat == AUDIO_ES )
1905 for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
1907 if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
1909 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
1910 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
1912 if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
1914 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
1915 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
1920 /* */
1921 p_owner->cc.b_supported = ( p_sout == NULL );
1923 p_owner->cc.desc.i_608_channels = 0;
1924 p_owner->cc.desc.i_708_channels = 0;
1925 for( unsigned i = 0; i < MAX_CC_DECODERS; i++ )
1926 p_owner->cc.pp_decoder[i] = NULL;
1927 p_owner->cc.p_sout_input = NULL;
1928 p_owner->cc.b_sout_created = false;
1929 return p_owner;
1933 * Destroys a decoder object
1935 * \param p_dec the decoder object
1936 * \return nothing
1938 static void DeleteDecoder( vlc_input_decoder_t *p_owner )
1940 decoder_t *p_dec = &p_owner->dec;
1941 msg_Dbg( p_dec, "killing decoder fourcc `%4.4s'",
1942 (char*)&p_dec->fmt_in.i_codec );
1944 const enum es_format_category_e i_cat =p_dec->fmt_in.i_cat;
1945 decoder_Clean( p_dec );
1946 if ( p_owner->out_pool )
1948 picture_pool_Release( p_owner->out_pool );
1949 p_owner->out_pool = NULL;
1952 if (p_owner->vctx)
1953 vlc_video_context_Release( p_owner->vctx );
1955 /* Free all packets still in the decoder fifo. */
1956 block_FifoRelease( p_owner->p_fifo );
1958 /* Cleanup */
1959 #ifdef ENABLE_SOUT
1960 if( p_owner->p_sout_input )
1962 sout_InputDelete( p_owner->p_sout, p_owner->p_sout_input );
1963 if( p_owner->cc.p_sout_input )
1964 sout_InputDelete( p_owner->p_sout, p_owner->cc.p_sout_input );
1966 #endif
1968 switch( i_cat )
1970 case AUDIO_ES:
1971 if( p_owner->p_aout )
1973 /* TODO: REVISIT gap-less audio */
1974 aout_DecDelete( p_owner->p_aout );
1975 input_resource_PutAout( p_owner->p_resource, p_owner->p_aout );
1977 break;
1978 case VIDEO_ES: {
1979 vout_thread_t *vout = p_owner->p_vout;
1981 if (vout != NULL)
1983 /* Hold the vout since PutVout will likely release it and a
1984 * last reference is needed for notify callbacks */
1985 vout_Hold(vout);
1987 bool has_stopped;
1988 input_resource_PutVout(p_owner->p_resource, vout, &has_stopped);
1989 if (has_stopped)
1990 decoder_Notify(p_owner, on_vout_stopped, vout);
1992 vout_Release(vout);
1994 break;
1996 case SPU_ES:
1998 if( p_owner->p_vout )
2000 assert( p_owner->i_spu_channel != VOUT_SPU_CHANNEL_INVALID );
2001 decoder_Notify(p_owner, on_vout_stopped, p_owner->p_vout);
2003 vout_UnregisterSubpictureChannel( p_owner->p_vout,
2004 p_owner->i_spu_channel );
2005 vout_Release(p_owner->p_vout);
2007 break;
2009 case DATA_ES:
2010 case UNKNOWN_ES:
2011 break;
2012 default:
2013 vlc_assert_unreachable();
2016 es_format_Clean( &p_owner->fmt );
2018 if( p_owner->p_description )
2019 vlc_meta_Delete( p_owner->p_description );
2021 decoder_Destroy( p_owner->p_packetizer );
2022 decoder_Destroy( &p_owner->dec );
2025 /* */
2026 static void DecoderUnsupportedCodec( decoder_t *p_dec, const es_format_t *fmt, bool b_decoding )
2028 if (fmt->i_codec != VLC_CODEC_UNKNOWN && fmt->i_codec) {
2029 const char *desc = vlc_fourcc_GetDescription(fmt->i_cat, fmt->i_codec);
2030 if (!desc || !*desc)
2031 desc = N_("No description for this codec");
2032 msg_Err( p_dec, "Codec `%4.4s' (%s) is not supported.", (char*)&fmt->i_codec, desc );
2033 vlc_dialog_display_error( p_dec, _("Codec not supported"),
2034 _("VLC could not decode the format \"%4.4s\" (%s)"),
2035 (char*)&fmt->i_codec, desc );
2036 } else if( b_decoding ){
2037 msg_Err( p_dec, "could not identify codec" );
2038 vlc_dialog_display_error( p_dec, _("Unidentified codec"),
2039 _("VLC could not identify the audio or video codec" ) );
2043 /* TODO: pass p_sout through p_resource? -- Courmisch */
2044 static vlc_input_decoder_t *
2045 decoder_New( vlc_object_t *p_parent, const es_format_t *fmt,
2046 vlc_clock_t *p_clock, input_resource_t *p_resource,
2047 sout_instance_t *p_sout, bool thumbnailing,
2048 const struct vlc_input_decoder_callbacks *cbs, void *userdata)
2050 const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
2051 int i_priority;
2053 /* Create the decoder configuration structure */
2054 vlc_input_decoder_t *p_owner =
2055 CreateDecoder( p_parent, fmt, p_clock, p_resource, p_sout,
2056 thumbnailing, cbs, userdata );
2057 if( p_owner == NULL )
2059 msg_Err( p_parent, "could not create %s", psz_type );
2060 vlc_dialog_display_error( p_parent, _("Streaming / Transcoding failed"),
2061 _("VLC could not open the %s module."), vlc_gettext( psz_type ) );
2062 return NULL;
2065 decoder_t *p_dec = &p_owner->dec;
2066 if( !p_dec->p_module )
2068 DecoderUnsupportedCodec( p_dec, fmt, !p_sout );
2070 DeleteDecoder( p_owner );
2071 return NULL;
2074 assert( p_dec->fmt_in.i_cat != UNKNOWN_ES );
2076 #if VLC_THREAD_PRIORITY_AUDIO != VLC_THREAD_PRIORITY_VIDEO
2077 if( p_dec->fmt_in.i_cat == AUDIO_ES )
2078 i_priority = VLC_THREAD_PRIORITY_AUDIO;
2079 else
2080 #endif
2081 i_priority = VLC_THREAD_PRIORITY_VIDEO;
2083 #ifdef ENABLE_SOUT
2084 /* Do not delay sout creation for SPU or DATA. */
2085 if( p_sout && fmt->b_packetized &&
2086 (fmt->i_cat != VIDEO_ES && fmt->i_cat != AUDIO_ES) )
2088 p_owner->p_sout_input = sout_InputNew( p_owner->p_sout, fmt );
2089 if( p_owner->p_sout_input == NULL )
2091 msg_Err( p_dec, "cannot create sout input (%4.4s)",
2092 (char *)&fmt->i_codec );
2093 p_owner->error = true;
2096 #endif
2098 /* Spawn the decoder thread */
2099 if( vlc_clone( &p_owner->thread, DecoderThread, p_owner, i_priority ) )
2101 msg_Err( p_dec, "cannot spawn decoder thread" );
2102 DeleteDecoder( p_owner );
2103 return NULL;
2106 return p_owner;
2111 * Spawns a new decoder thread from the input thread
2113 * \param p_input the input thread
2114 * \param p_es the es descriptor
2115 * \return the spawned decoder object
2117 vlc_input_decoder_t *
2118 vlc_input_decoder_New( vlc_object_t *parent, es_format_t *fmt,
2119 vlc_clock_t *p_clock, input_resource_t *resource,
2120 sout_instance_t *p_sout, bool thumbnailing,
2121 const struct vlc_input_decoder_callbacks *cbs,
2122 void *cbs_userdata)
2124 return decoder_New( parent, fmt, p_clock, resource, p_sout, thumbnailing,
2125 cbs, cbs_userdata );
2129 * Spawn a decoder thread outside of the input thread.
2131 vlc_input_decoder_t *
2132 vlc_input_decoder_Create( vlc_object_t *p_parent, const es_format_t *fmt,
2133 input_resource_t *p_resource )
2135 return decoder_New( p_parent, fmt, NULL, p_resource, NULL, false, NULL,
2136 NULL );
2141 * Kills a decoder thread and waits until it's finished
2143 * \param p_input the input thread
2144 * \param p_es the es descriptor
2145 * \return nothing
2147 void vlc_input_decoder_Delete( vlc_input_decoder_t *p_owner )
2149 decoder_t *p_dec = &p_owner->dec;
2151 vlc_fifo_Lock( p_owner->p_fifo );
2152 p_owner->aborting = true;
2153 p_owner->flushing = true;
2154 vlc_fifo_Signal( p_owner->p_fifo );
2155 vlc_fifo_Unlock( p_owner->p_fifo );
2157 /* Make sure we aren't waiting/decoding anymore */
2158 vlc_mutex_lock( &p_owner->lock );
2159 p_owner->b_waiting = false;
2160 vlc_cond_signal( &p_owner->wait_request );
2162 /* If the video output is paused or slow, or if the picture pool size was
2163 * under-estimated (e.g. greedy video filter, buggy decoder...), the
2164 * the picture pool may be empty, and the decoder thread or any decoder
2165 * module worker threads may be stuck waiting for free picture buffers.
2167 * This unblocks the thread, allowing the decoder module to join all its
2168 * worker threads (if any) and the decoder thread to terminate. */
2169 if( p_dec->fmt_in.i_cat == VIDEO_ES && p_owner->p_vout != NULL
2170 && p_owner->vout_started )
2172 if (p_owner->out_pool)
2173 picture_pool_Cancel( p_owner->out_pool, true );
2175 if( p_owner->paused )
2177 /* The DecoderThread could be stuck in pf_decode(). This is likely the
2178 * case with paused asynchronous decoder modules that have a limited
2179 * input and output pool size. Indeed, with such decoders, you have to
2180 * release an output buffer to get an input buffer. So, when paused and
2181 * flushed, the DecoderThread could be waiting for an output buffer to
2182 * be released (or rendered). In that case, the DecoderThread will
2183 * never be flushed since it be never leave pf_decode(). To fix this
2184 * issue, pre-flush the vout from here. The vout will have to be
2185 * flushed again since the module could be outputting more buffers just
2186 * after being unstuck. */
2188 vout_FlushAll( p_owner->p_vout );
2191 vlc_mutex_unlock( &p_owner->lock );
2193 vlc_join( p_owner->thread, NULL );
2195 /* */
2196 if( p_owner->cc.b_supported )
2198 for( int i = 0; i < MAX_CC_DECODERS; i++ )
2199 vlc_input_decoder_SetCcState( p_owner, VLC_CODEC_CEA608, i, false );
2202 /* Delete decoder */
2203 DeleteDecoder( p_owner );
2207 * Put a block_t in the decoder's fifo.
2208 * Thread-safe w.r.t. the decoder. May be a cancellation point.
2210 * \param p_dec the decoder object
2211 * \param p_block the data block
2213 void vlc_input_decoder_Decode( vlc_input_decoder_t *p_owner, block_t *p_block,
2214 bool b_do_pace )
2216 vlc_fifo_Lock( p_owner->p_fifo );
2217 if( !b_do_pace )
2219 /* FIXME: ideally we would check the time amount of data
2220 * in the FIFO instead of its size. */
2221 /* 400 MiB, i.e. ~ 50mb/s for 60s */
2222 if( vlc_fifo_GetBytes( p_owner->p_fifo ) > 400*1024*1024 )
2224 msg_Warn( &p_owner->dec, "decoder/packetizer fifo full (data not "
2225 "consumed quickly enough), resetting fifo!" );
2226 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2227 p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;
2230 else
2231 if( !p_owner->b_waiting )
2232 { /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
2233 * Locking is not necessary as b_waiting is only read, not written by
2234 * the decoder thread. */
2235 while( vlc_fifo_GetCount( p_owner->p_fifo ) >= 10 )
2236 vlc_fifo_WaitCond( p_owner->p_fifo, &p_owner->wait_fifo );
2239 vlc_fifo_QueueUnlocked( p_owner->p_fifo, p_block );
2240 vlc_fifo_Unlock( p_owner->p_fifo );
2243 bool vlc_input_decoder_IsEmpty( vlc_input_decoder_t * p_owner )
2245 assert( !p_owner->b_waiting );
2247 vlc_fifo_Lock( p_owner->p_fifo );
2248 if( !vlc_fifo_IsEmpty( p_owner->p_fifo ) || p_owner->b_draining )
2250 vlc_fifo_Unlock( p_owner->p_fifo );
2251 return false;
2253 vlc_fifo_Unlock( p_owner->p_fifo );
2255 bool b_empty;
2257 vlc_mutex_lock( &p_owner->lock );
2258 #ifdef ENABLE_SOUT
2259 if( p_owner->p_sout_input != NULL )
2260 b_empty = true;
2261 else
2262 #endif
2263 if( p_owner->fmt.i_cat == VIDEO_ES && p_owner->p_vout != NULL )
2264 b_empty = vout_IsEmpty( p_owner->p_vout );
2265 else if( p_owner->fmt.i_cat == AUDIO_ES )
2266 b_empty = !p_owner->b_draining || p_owner->drained;
2267 else
2268 b_empty = true; /* TODO subtitles support */
2269 vlc_mutex_unlock( &p_owner->lock );
2271 return b_empty;
2275 * Signals that there are no further blocks to decode, and requests that the
2276 * decoder drain all pending buffers. This is used to ensure that all
2277 * intermediate buffers empty and no samples get lost at the end of the stream.
2279 * @note The function does not actually wait for draining. It just signals that
2280 * draining should be performed once the decoder has emptied FIFO.
2282 void vlc_input_decoder_Drain( vlc_input_decoder_t *p_owner )
2284 vlc_fifo_Lock( p_owner->p_fifo );
2285 p_owner->b_draining = true;
2286 vlc_fifo_Signal( p_owner->p_fifo );
2287 vlc_fifo_Unlock( p_owner->p_fifo );
2291 * Requests that the decoder immediately discard all pending buffers.
2292 * This is useful when seeking or when deselecting a stream.
2294 void vlc_input_decoder_Flush( vlc_input_decoder_t *p_owner )
2296 vlc_fifo_Lock( p_owner->p_fifo );
2298 /* Empty the fifo */
2299 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner->p_fifo ) );
2301 /* Don't need to wait for the DecoderThread to flush. Indeed, if called a
2302 * second time, this function will clear the FIFO again before anything was
2303 * dequeued by DecoderThread and there is no need to flush a second time in
2304 * a row. */
2305 p_owner->flushing = true;
2307 /* Flush video/spu decoder when paused: increment frames_countdown in order
2308 * to display one frame/subtitle */
2309 if( p_owner->paused
2310 && ( p_owner->fmt.i_cat == VIDEO_ES || p_owner->fmt.i_cat == SPU_ES )
2311 && p_owner->frames_countdown == 0 )
2312 p_owner->frames_countdown++;
2314 vlc_fifo_Signal( p_owner->p_fifo );
2316 vlc_fifo_Unlock( p_owner->p_fifo );
2318 if ( p_owner->fmt.i_cat == VIDEO_ES )
2320 /* Set the pool cancel state. This will unblock the module if it is
2321 * waiting for new pictures (likely). This state will be reset back
2322 * from the DecoderThread once the flush request is processed. */
2323 vlc_mutex_lock( &p_owner->lock );
2324 if( p_owner->out_pool != NULL )
2325 picture_pool_Cancel( p_owner->out_pool, true );
2326 vlc_mutex_unlock( &p_owner->lock );
2329 if( p_owner->paused )
2331 /* The DecoderThread could be stuck in pf_decode(). This is likely the
2332 * case with paused asynchronous decoder modules that have a limited
2333 * input and output pool size. Indeed, with such decoders, you have to
2334 * release an output buffer to get an input buffer. So, when paused and
2335 * flushed, the DecoderThread could be waiting for an output buffer to
2336 * be released (or rendered). In that case, the DecoderThread will
2337 * never be flushed since it be never leave pf_decode(). To fix this
2338 * issue, pre-flush the vout from here. The vout will have to be
2339 * flushed again since the module could be outputting more buffers just
2340 * after being unstuck. */
2342 vlc_mutex_lock( &p_owner->lock );
2343 if( p_owner->dec.fmt_out.i_cat == VIDEO_ES && p_owner->p_vout
2344 && p_owner->vout_started )
2345 vout_FlushAll( p_owner->p_vout );
2346 vlc_mutex_unlock( &p_owner->lock );
2350 void vlc_input_decoder_GetCcDesc( vlc_input_decoder_t *p_owner,
2351 decoder_cc_desc_t *p_desc )
2353 vlc_mutex_lock( &p_owner->lock );
2354 *p_desc = p_owner->cc.desc;
2355 vlc_mutex_unlock( &p_owner->lock );
2358 static bool vlc_input_decoder_HasCCChanFlag( vlc_input_decoder_t *p_owner,
2359 vlc_fourcc_t codec, int i_channel )
2361 int i_max_channels;
2362 uint64_t i_bitmap;
2363 if( codec == VLC_CODEC_CEA608 )
2365 i_max_channels = 4;
2366 i_bitmap = p_owner->cc.desc.i_608_channels;
2368 else if( codec == VLC_CODEC_CEA708 )
2370 i_max_channels = 64;
2371 i_bitmap = p_owner->cc.desc.i_708_channels;
2373 else return false;
2375 return ( i_channel >= 0 && i_channel < i_max_channels &&
2376 ( i_bitmap & ((uint64_t)1 << i_channel) ) );
2379 int vlc_input_decoder_SetCcState( vlc_input_decoder_t *p_owner, vlc_fourcc_t codec,
2380 int i_channel, bool b_decode )
2382 decoder_t *p_dec = &p_owner->dec;
2383 //msg_Warn( p_dec, "vlc_input_decoder_SetCcState: %d @%x", b_decode, i_channel );
2385 if( !vlc_input_decoder_HasCCChanFlag( p_owner, codec, i_channel ) )
2386 return VLC_EGENERIC;
2388 if( b_decode )
2390 vlc_input_decoder_t *p_ccowner;
2391 es_format_t fmt;
2393 es_format_Init( &fmt, SPU_ES, codec );
2394 fmt.subs.cc.i_channel = i_channel;
2395 fmt.subs.cc.i_reorder_depth = p_owner->cc.desc.i_reorder_depth;
2396 p_ccowner = vlc_input_decoder_New( VLC_OBJECT(p_dec), &fmt, p_owner->p_clock,
2397 p_owner->p_resource, p_owner->p_sout, false,
2398 NULL, NULL );
2399 if( !p_ccowner )
2401 msg_Err( p_dec, "could not create decoder" );
2402 vlc_dialog_display_error( p_dec,
2403 _("Streaming / Transcoding failed"), "%s",
2404 _("VLC could not open the decoder module.") );
2405 return VLC_EGENERIC;
2407 else if( !p_ccowner->dec.p_module )
2409 DecoderUnsupportedCodec( p_dec, &fmt, true );
2410 vlc_input_decoder_Delete(p_ccowner);
2411 return VLC_EGENERIC;
2413 p_ccowner->p_clock = p_owner->p_clock;
2415 vlc_mutex_lock( &p_owner->lock );
2416 p_owner->cc.pp_decoder[i_channel] = p_ccowner;
2417 vlc_mutex_unlock( &p_owner->lock );
2419 else
2421 vlc_input_decoder_t *p_cc;
2423 vlc_mutex_lock( &p_owner->lock );
2424 p_cc = p_owner->cc.pp_decoder[i_channel];
2425 p_owner->cc.pp_decoder[i_channel] = NULL;
2426 vlc_mutex_unlock( &p_owner->lock );
2428 if( p_cc )
2429 vlc_input_decoder_Delete(p_cc);
2431 return VLC_SUCCESS;
2434 int vlc_input_decoder_GetCcState( vlc_input_decoder_t *p_owner, vlc_fourcc_t codec,
2435 int i_channel, bool *pb_decode )
2437 if( !vlc_input_decoder_HasCCChanFlag( p_owner, codec, i_channel ) )
2438 return VLC_EGENERIC;
2440 vlc_mutex_lock( &p_owner->lock );
2441 *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
2442 vlc_mutex_unlock( &p_owner->lock );
2443 return VLC_SUCCESS;
2446 void vlc_input_decoder_ChangePause( vlc_input_decoder_t *p_owner,
2447 bool b_paused, vlc_tick_t i_date )
2449 /* Normally, p_owner->b_paused != b_paused here. But if a track is added
2450 * while the input is paused (e.g. add sub file), then b_paused is
2451 * (incorrectly) false. FIXME: This is a bug in the decoder owner. */
2452 vlc_fifo_Lock( p_owner->p_fifo );
2453 p_owner->paused = b_paused;
2454 p_owner->pause_date = i_date;
2455 p_owner->frames_countdown = 0;
2456 vlc_fifo_Signal( p_owner->p_fifo );
2457 vlc_fifo_Unlock( p_owner->p_fifo );
2460 void vlc_input_decoder_ChangeRate( vlc_input_decoder_t *owner, float rate )
2462 vlc_fifo_Lock( owner->p_fifo );
2463 owner->request_rate = rate;
2464 vlc_fifo_Unlock( owner->p_fifo );
2467 void vlc_input_decoder_ChangeDelay( vlc_input_decoder_t *owner, vlc_tick_t delay )
2469 vlc_fifo_Lock( owner->p_fifo );
2470 owner->delay = delay;
2471 vlc_fifo_Unlock( owner->p_fifo );
2474 void vlc_input_decoder_StartWait( vlc_input_decoder_t *p_owner )
2476 assert( !p_owner->b_waiting );
2478 vlc_mutex_lock( &p_owner->lock );
2479 p_owner->b_first = true;
2480 p_owner->b_has_data = false;
2481 p_owner->b_waiting = true;
2482 vlc_cond_signal( &p_owner->wait_request );
2483 vlc_mutex_unlock( &p_owner->lock );
2486 void vlc_input_decoder_StopWait( vlc_input_decoder_t *p_owner )
2488 assert( p_owner->b_waiting );
2490 vlc_mutex_lock( &p_owner->lock );
2491 p_owner->b_waiting = false;
2492 vlc_cond_signal( &p_owner->wait_request );
2493 vlc_mutex_unlock( &p_owner->lock );
2496 void vlc_input_decoder_Wait( vlc_input_decoder_t *p_owner )
2498 assert( p_owner->b_waiting );
2500 vlc_mutex_lock( &p_owner->lock );
2501 while( !p_owner->b_has_data )
2503 /* Don't need to lock p_owner->paused since it's only modified by the
2504 * owner */
2505 if( p_owner->paused )
2506 break;
2507 vlc_fifo_Lock( p_owner->p_fifo );
2508 if( p_owner->b_idle && vlc_fifo_IsEmpty( p_owner->p_fifo ) )
2510 msg_Err( &p_owner->dec, "buffer deadlock prevented" );
2511 vlc_fifo_Unlock( p_owner->p_fifo );
2512 break;
2514 vlc_fifo_Unlock( p_owner->p_fifo );
2515 vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
2517 vlc_mutex_unlock( &p_owner->lock );
2520 void vlc_input_decoder_FrameNext( vlc_input_decoder_t *p_owner,
2521 vlc_tick_t *pi_duration )
2523 assert( p_owner->paused );
2524 *pi_duration = 0;
2526 vlc_fifo_Lock( p_owner->p_fifo );
2527 p_owner->frames_countdown++;
2528 vlc_fifo_Signal( p_owner->p_fifo );
2529 vlc_fifo_Unlock( p_owner->p_fifo );
2531 vlc_mutex_lock( &p_owner->lock );
2532 if( p_owner->fmt.i_cat == VIDEO_ES )
2534 if( p_owner->p_vout )
2535 vout_NextPicture( p_owner->p_vout, pi_duration );
2537 vlc_mutex_unlock( &p_owner->lock );
2540 bool vlc_input_decoder_HasFormatChanged( vlc_input_decoder_t *p_owner,
2541 es_format_t *p_fmt, vlc_meta_t **pp_meta )
2543 if( !atomic_exchange_explicit( &p_owner->b_fmt_description, false,
2544 memory_order_acquire ) )
2545 return false;
2547 vlc_mutex_lock( &p_owner->lock );
2549 if( p_owner->fmt.i_cat == UNKNOWN_ES )
2551 /* The format changed but the output creation failed */
2552 vlc_mutex_unlock( &p_owner->lock );
2553 return false;
2556 if( p_fmt != NULL )
2557 es_format_Copy( p_fmt, &p_owner->fmt );
2559 if( pp_meta )
2561 *pp_meta = NULL;
2562 if( p_owner->p_description )
2564 *pp_meta = vlc_meta_New();
2565 if( *pp_meta )
2566 vlc_meta_Merge( *pp_meta, p_owner->p_description );
2569 vlc_mutex_unlock( &p_owner->lock );
2570 return true;
2573 size_t vlc_input_decoder_GetFifoSize( vlc_input_decoder_t *p_owner )
2575 return block_FifoSize( p_owner->p_fifo );
2578 static bool DecoderHasVbi( decoder_t *dec )
2580 return dec->fmt_in.i_cat == SPU_ES && dec->fmt_in.i_codec == VLC_CODEC_TELETEXT
2581 && var_Type( dec, "vbi-page" ) == VLC_VAR_INTEGER;
2584 int vlc_input_decoder_GetVbiPage( vlc_input_decoder_t *owner, bool *opaque )
2586 decoder_t *dec = &owner->dec;
2587 if( !DecoderHasVbi( dec ) )
2588 return -1;
2589 *opaque = var_GetBool( dec, "vbi-opaque" );
2590 return var_GetInteger( dec, "vbi-page" );
2593 int vlc_input_decoder_SetVbiPage( vlc_input_decoder_t *owner, unsigned page )
2595 decoder_t *dec = &owner->dec;
2596 if( !DecoderHasVbi( dec ) )
2597 return VLC_EGENERIC;
2598 return var_SetInteger( dec, "vbi-page", page );
2601 int vlc_input_decoder_SetVbiOpaque( vlc_input_decoder_t *owner, bool opaque )
2603 decoder_t *dec = &owner->dec;
2604 if( !DecoderHasVbi( dec ) )
2605 return VLC_EGENERIC;
2606 return var_SetBool( dec, "vbi-opaque", opaque );
2609 void vlc_input_decoder_SetVoutMouseEvent( vlc_input_decoder_t *owner,
2610 vlc_mouse_event mouse_event,
2611 void *user_data )
2613 assert( owner->dec.fmt_in.i_cat == VIDEO_ES );
2615 vlc_mutex_lock( &owner->mouse_lock );
2617 owner->mouse_event = mouse_event;
2618 owner->mouse_opaque = user_data;
2620 vlc_mutex_unlock( &owner->mouse_lock );
2623 int vlc_input_decoder_AddVoutOverlay( vlc_input_decoder_t *owner, subpicture_t *sub,
2624 size_t *channel )
2626 assert( owner->dec.fmt_in.i_cat == VIDEO_ES );
2627 assert( sub && channel );
2629 vlc_mutex_lock( &owner->lock );
2631 if( !owner->p_vout )
2633 vlc_mutex_unlock( &owner->lock );
2634 return VLC_EGENERIC;
2636 ssize_t channel_id =
2637 vout_RegisterSubpictureChannel( owner->p_vout );
2638 if (channel_id == -1)
2640 vlc_mutex_unlock( &owner->lock );
2641 return VLC_EGENERIC;
2643 sub->i_start = sub->i_stop = vlc_tick_now();
2644 sub->i_channel = *channel = channel_id;
2645 sub->i_order = 0;
2646 sub->b_ephemer = true;
2647 vout_PutSubpicture( owner->p_vout, sub );
2649 vlc_mutex_unlock( &owner->lock );
2650 return VLC_SUCCESS;
2653 int vlc_input_decoder_DelVoutOverlay( vlc_input_decoder_t *owner, size_t channel )
2655 assert( owner->dec.fmt_in.i_cat == VIDEO_ES );
2657 vlc_mutex_lock( &owner->lock );
2659 if( !owner->p_vout )
2661 vlc_mutex_unlock( &owner->lock );
2662 return VLC_EGENERIC;
2664 vout_UnregisterSubpictureChannel( owner->p_vout, channel );
2666 vlc_mutex_unlock( &owner->lock );
2667 return VLC_SUCCESS;
2670 int vlc_input_decoder_SetSpuHighlight( vlc_input_decoder_t *p_owner,
2671 const vlc_spu_highlight_t *spu_hl )
2673 assert( p_owner->dec.fmt_in.i_cat == SPU_ES );
2675 #ifdef ENABLE_SOUT
2676 if( p_owner->p_sout_input )
2677 sout_InputControl( p_owner->p_sout, p_owner->p_sout_input,
2678 SOUT_INPUT_SET_SPU_HIGHLIGHT, spu_hl );
2679 #endif
2681 vlc_mutex_lock( &p_owner->lock );
2682 if( !p_owner->p_vout )
2684 vlc_mutex_unlock( &p_owner->lock );
2685 return VLC_EGENERIC;
2688 vout_SetSpuHighlight( p_owner->p_vout, spu_hl );
2690 vlc_mutex_unlock( &p_owner->lock );
2691 return VLC_SUCCESS;