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 /*****************************************************************************
27 *****************************************************************************/
32 #include <stdatomic.h>
34 #include <vlc_common.h>
35 #include <vlc_block.h>
39 #include <vlc_codec.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"
54 #include "../video_output/vout_internal.h"
57 * Possibles values set in p_owner->reload atomic
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
69 input_resource_t
*p_resource
;
72 const struct vlc_input_decoder_callbacks
*cbs
;
75 ssize_t i_spu_channel
;
78 sout_instance_t
*p_sout
;
79 sout_packetizer_input_t
*p_sout_input
;
83 /* Some decoders require already packetized data (ie. not truncated) */
84 decoder_t
*p_packetizer
;
87 /* Current format in use by the output */
89 vlc_video_context
*vctx
;
92 atomic_bool b_fmt_description
;
93 vlc_meta_t
*p_description
;
99 /* Lock for communication with decoder thread */
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
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
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
;
132 enum vlc_vout_order vout_order
;
134 /* -- Theses variables need locking on read *and* write -- */
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
142 bool reset_out_state
;
143 vlc_tick_t pause_date
;
145 float request_rate
, output_rate
;
146 unsigned frames_countdown
;
164 #define MAX_CC_DECODERS 64 /* The es_out only creates one type of es */
168 decoder_cc_desc_t desc
;
169 vlc_input_decoder_t
*pp_decoder
[MAX_CC_DECODERS
];
171 sout_packetizer_input_t
*p_sout_input
;
175 vlc_mutex_t mouse_lock
;
176 vlc_mouse_event mouse_event
;
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))
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 */
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
],
220 p_dec
->p_module
= module_need_var( p_dec
, "packetizer", "packetizer" );
222 if( !p_dec
->p_module
)
224 decoder_Clean( p_dec
);
230 static int DecoderThread_Reload( vlc_input_decoder_t
*p_owner
,
231 const es_format_t
*restrict p_fmt
,
234 /* Copy p_fmt since it can be destroyed by decoder_Clean */
235 decoder_t
*p_dec
= &p_owner
->dec
;
237 if( es_format_Copy( &fmt_in
, p_fmt
) != VLC_SUCCESS
)
239 p_owner
->error
= true;
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
;
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
);
266 es_format_Clean( &fmt_in
);
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 assert( p_owner
->fmt
.i_cat
== p_dec
->fmt_in
.i_cat
);
281 /* Move p_description */
282 if( p_dec
->p_description
!= NULL
)
284 if( p_owner
->p_description
!= NULL
)
285 vlc_meta_Delete( p_owner
->p_description
);
286 p_owner
->p_description
= p_dec
->p_description
;
287 p_dec
->p_description
= NULL
;
290 atomic_store_explicit( &p_owner
->b_fmt_description
, true,
291 memory_order_release
);
294 static void MouseEvent( const vlc_mouse_t
*newmouse
, void *user_data
)
296 decoder_t
*dec
= user_data
;
297 vlc_input_decoder_t
*owner
= dec_get_owner( dec
);
299 vlc_mutex_lock( &owner
->mouse_lock
);
300 if( owner
->mouse_event
)
301 owner
->mouse_event( newmouse
, owner
->mouse_opaque
);
302 vlc_mutex_unlock( &owner
->mouse_lock
);
305 /*****************************************************************************
306 * Buffers allocation callbacks for the decoders
307 *****************************************************************************/
308 static bool aout_replaygain_changed( const audio_replay_gain_t
*a
,
309 const audio_replay_gain_t
*b
)
311 for( size_t i
=0; i
<AUDIO_REPLAY_GAIN_MAX
; i
++ )
313 if( a
->pb_gain
[i
] != b
->pb_gain
[i
] ||
314 a
->pb_peak
[i
] != b
->pb_peak
[i
] ||
315 a
->pb_gain
[i
] != b
->pb_gain
[i
] ||
316 a
->pb_peak
[i
] != b
->pb_peak
[i
] )
322 static int ModuleThread_UpdateAudioFormat( decoder_t
*p_dec
)
324 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
326 if( p_owner
->p_aout
&&
327 ( !AOUT_FMTS_IDENTICAL(&p_dec
->fmt_out
.audio
, &p_owner
->fmt
.audio
) ||
328 p_dec
->fmt_out
.i_codec
!= p_dec
->fmt_out
.audio
.i_format
||
329 p_dec
->fmt_out
.i_profile
!= p_owner
->fmt
.i_profile
) )
331 audio_output_t
*p_aout
= p_owner
->p_aout
;
333 /* Parameters changed, restart the aout */
334 vlc_mutex_lock( &p_owner
->lock
);
335 p_owner
->p_aout
= NULL
; // the DecoderThread should not use the old aout anymore
336 vlc_mutex_unlock( &p_owner
->lock
);
337 aout_DecDelete( p_aout
);
339 input_resource_PutAout( p_owner
->p_resource
, p_aout
);
342 /* Check if only replay gain has changed */
343 if( aout_replaygain_changed( &p_dec
->fmt_in
.audio_replay_gain
,
344 &p_owner
->fmt
.audio_replay_gain
) )
346 p_dec
->fmt_out
.audio_replay_gain
= p_dec
->fmt_in
.audio_replay_gain
;
347 if( p_owner
->p_aout
)
349 p_owner
->fmt
.audio_replay_gain
= p_dec
->fmt_in
.audio_replay_gain
;
350 var_TriggerCallback( p_owner
->p_aout
, "audio-replay-gain-mode" );
354 if( p_owner
->p_aout
== NULL
)
356 p_dec
->fmt_out
.audio
.i_format
= p_dec
->fmt_out
.i_codec
;
358 audio_sample_format_t format
= p_dec
->fmt_out
.audio
;
359 aout_FormatPrepare( &format
);
361 const int i_force_dolby
= var_InheritInteger( p_dec
, "force-dolby-surround" );
363 format
.i_physical_channels
== (AOUT_CHAN_LEFT
|AOUT_CHAN_RIGHT
) )
365 if( i_force_dolby
== 1 )
366 format
.i_chan_mode
|= AOUT_CHANMODE_DOLBYSTEREO
;
367 else /* i_force_dolby == 2 */
368 format
.i_chan_mode
&= ~AOUT_CHANMODE_DOLBYSTEREO
;
371 audio_output_t
*p_aout
;
373 p_aout
= input_resource_GetAout( p_owner
->p_resource
);
376 if( aout_DecNew( p_aout
, &format
, p_dec
->fmt_out
.i_profile
,
378 &p_dec
->fmt_out
.audio_replay_gain
) )
380 input_resource_PutAout( p_owner
->p_resource
, p_aout
);
385 vlc_mutex_lock( &p_owner
->lock
);
386 p_owner
->p_aout
= p_aout
;
388 DecoderUpdateFormatLocked( p_owner
);
389 aout_FormatPrepare( &p_owner
->fmt
.audio
);
390 vlc_mutex_unlock( &p_owner
->lock
);
395 p_dec
->fmt_out
.audio
.i_bytes_per_frame
=
396 p_owner
->fmt
.audio
.i_bytes_per_frame
;
397 p_dec
->fmt_out
.audio
.i_frame_length
=
398 p_owner
->fmt
.audio
.i_frame_length
;
400 vlc_fifo_Lock( p_owner
->p_fifo
);
401 p_owner
->reset_out_state
= true;
402 vlc_fifo_Unlock( p_owner
->p_fifo
);
407 static int CreateVoutIfNeeded(vlc_input_decoder_t
*);
410 static int ModuleThread_UpdateVideoFormat( decoder_t
*p_dec
, vlc_video_context
*vctx
)
412 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
414 int created_vout
= CreateVoutIfNeeded(p_owner
);
415 if (created_vout
== -1)
417 if (created_vout
== 0)
419 // video context didn't change
420 if (vctx
!= NULL
&& p_owner
->vctx
== vctx
)
423 assert(p_owner
->p_vout
);
426 vlc_video_context_Release(p_owner
->vctx
);
427 p_owner
->vctx
= vctx
? vlc_video_context_Hold(vctx
) : NULL
;
429 // configure the new vout
431 if ( p_owner
->out_pool
== NULL
)
434 switch( p_dec
->fmt_in
.i_codec
)
438 case VLC_CODEC_DIRAC
: /* FIXME valid ? */
455 picture_pool_t
*pool
= picture_pool_NewFromFormat( &p_dec
->fmt_out
.video
,
456 dpb_size
+ p_dec
->i_extra_picture_buffers
+ 1 );
460 msg_Err(p_dec
, "Failed to create a pool of %d %4.4s pictures",
461 dpb_size
+ p_dec
->i_extra_picture_buffers
+ 1,
462 (char*)&p_dec
->fmt_out
.video
.i_chroma
);
466 vlc_mutex_lock( &p_owner
->lock
);
467 p_owner
->out_pool
= pool
;
468 vlc_mutex_unlock( &p_owner
->lock
);
472 vout_configuration_t cfg
= {
473 .vout
= p_owner
->p_vout
, .clock
= p_owner
->p_clock
, .fmt
= &p_dec
->fmt_out
.video
,
474 .mouse_event
= MouseEvent
, .mouse_opaque
= p_dec
,
477 vout_thread_t
*p_vout
=
478 input_resource_RequestVout(p_owner
->p_resource
, vctx
, &cfg
, NULL
,
482 vlc_mutex_lock( &p_owner
->lock
);
483 p_owner
->vout_started
= true;
484 vlc_mutex_unlock( &p_owner
->lock
);
486 vlc_fifo_Lock( p_owner
->p_fifo
);
487 p_owner
->reset_out_state
= true;
488 vlc_fifo_Unlock( p_owner
->p_fifo
);
491 decoder_Notify(p_owner
, on_vout_started
, p_vout
, p_owner
->vout_order
);
496 /* Clean fmt and vctx to trigger a new vout creation on the next update
498 vlc_mutex_lock( &p_owner
->lock
);
499 es_format_Clean( &p_owner
->fmt
);
500 vlc_mutex_unlock( &p_owner
->lock
);
502 if (p_owner
->vctx
!= NULL
)
504 vlc_video_context_Release(p_owner
->vctx
);
505 p_owner
->vctx
= NULL
;
510 static int CreateVoutIfNeeded(vlc_input_decoder_t
*p_owner
)
512 decoder_t
*p_dec
= &p_owner
->dec
;
513 bool need_vout
= false;
515 if( p_owner
->p_vout
== NULL
)
517 msg_Dbg(p_dec
, "vout: none found");
520 if( p_dec
->fmt_out
.video
.i_width
!= p_owner
->fmt
.video
.i_width
521 || p_dec
->fmt_out
.video
.i_height
!= p_owner
->fmt
.video
.i_height
)
523 msg_Dbg(p_dec
, "vout change: decoder size");
526 if( p_dec
->fmt_out
.video
.i_visible_width
!= p_owner
->fmt
.video
.i_visible_width
527 || p_dec
->fmt_out
.video
.i_visible_height
!= p_owner
->fmt
.video
.i_visible_height
528 || p_dec
->fmt_out
.video
.i_x_offset
!= p_owner
->fmt
.video
.i_x_offset
529 || p_dec
->fmt_out
.video
.i_y_offset
!= p_owner
->fmt
.video
.i_y_offset
)
531 msg_Dbg(p_dec
, "vout change: visible size");
534 if( p_dec
->fmt_out
.i_codec
!= p_owner
->fmt
.video
.i_chroma
)
536 msg_Dbg(p_dec
, "vout change: chroma");
539 if( (int64_t)p_dec
->fmt_out
.video
.i_sar_num
* p_owner
->fmt
.video
.i_sar_den
!=
540 (int64_t)p_dec
->fmt_out
.video
.i_sar_den
* p_owner
->fmt
.video
.i_sar_num
)
542 msg_Dbg(p_dec
, "vout change: SAR");
545 if( p_dec
->fmt_out
.video
.orientation
!= p_owner
->fmt
.video
.orientation
)
547 msg_Dbg(p_dec
, "vout change: orientation");
550 if( p_dec
->fmt_out
.video
.multiview_mode
!= p_owner
->fmt
.video
.multiview_mode
)
552 msg_Dbg(p_dec
, "vout change: multiview");
557 return 0; // vout unchanged
559 vlc_mutex_lock( &p_owner
->lock
);
561 vout_thread_t
*p_vout
= p_owner
->p_vout
;
562 p_owner
->p_vout
= NULL
; // the DecoderThread should not use the old vout anymore
563 p_owner
->vout_started
= false;
564 vlc_mutex_unlock( &p_owner
->lock
);
566 enum vlc_vout_order order
;
567 const vout_configuration_t cfg
= { .vout
= p_vout
, .fmt
= NULL
};
568 p_vout
= input_resource_RequestVout( p_owner
->p_resource
, NULL
, &cfg
, &order
, NULL
);
570 vlc_mutex_lock( &p_owner
->lock
);
571 p_owner
->p_vout
= p_vout
;
572 p_owner
->vout_order
= order
;
574 DecoderUpdateFormatLocked( p_owner
);
575 p_owner
->fmt
.video
.i_chroma
= p_dec
->fmt_out
.i_codec
;
576 picture_pool_t
*pool
= p_owner
->out_pool
;
577 p_owner
->out_pool
= NULL
;
578 vlc_mutex_unlock( &p_owner
->lock
);
581 picture_pool_Release( pool
);
585 msg_Err( p_dec
, "failed to create video output" );
589 return 1; // new vout was created
592 static vlc_decoder_device
* ModuleThread_GetDecoderDevice( decoder_t
*p_dec
)
594 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
596 /* Requesting a decoder device will automatically enable hw decoding */
597 if( !var_InheritBool( p_dec
, "hw-dec" ) )
600 int created_vout
= CreateVoutIfNeeded(p_owner
);
601 if (created_vout
== -1)
602 return NULL
; // error
604 assert(p_owner
->p_vout
);
605 vlc_decoder_device
*dec_device
= vout_GetDevice(p_owner
->p_vout
);
606 if (created_vout
== 1)
607 return dec_device
; // new vout was created with a decoder device
609 bool need_format_update
= false;
610 if ( memcmp( &p_dec
->fmt_out
.video
.mastering
,
611 &p_owner
->fmt
.video
.mastering
,
612 sizeof(p_owner
->fmt
.video
.mastering
)) )
614 msg_Dbg(p_dec
, "vout update: mastering data");
615 need_format_update
= true;
617 if ( p_dec
->fmt_out
.video
.lighting
.MaxCLL
!=
618 p_owner
->fmt
.video
.lighting
.MaxCLL
||
619 p_dec
->fmt_out
.video
.lighting
.MaxFALL
!=
620 p_owner
->fmt
.video
.lighting
.MaxFALL
)
622 msg_Dbg(p_dec
, "vout update: lighting data");
623 need_format_update
= true;
626 if ( need_format_update
)
628 /* the format has changed but we don't need a new vout */
629 vlc_mutex_lock( &p_owner
->lock
);
630 DecoderUpdateFormatLocked( p_owner
);
631 vlc_mutex_unlock( &p_owner
->lock
);
636 static picture_t
*ModuleThread_NewVideoBuffer( decoder_t
*p_dec
)
638 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
639 assert( p_owner
->p_vout
);
640 assert( p_owner
->out_pool
);
642 picture_t
*pic
= picture_pool_Wait( p_owner
->out_pool
);
644 picture_Reset( pic
);
648 static subpicture_t
*ModuleThread_NewSpuBuffer( decoder_t
*p_dec
,
649 const subpicture_updater_t
*p_updater
)
651 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
652 vout_thread_t
*p_vout
= NULL
;
653 subpicture_t
*p_subpic
;
656 while( i_attempts
-- )
661 p_vout
= input_resource_HoldVout( p_owner
->p_resource
);
665 vlc_tick_sleep( DECODER_SPU_VOUT_WAIT_DURATION
);
670 msg_Warn( p_dec
, "no vout found, dropping subpicture" );
671 if( p_owner
->p_vout
)
673 assert(p_owner
->i_spu_channel
!= VOUT_SPU_CHANNEL_INVALID
);
674 decoder_Notify(p_owner
, on_vout_stopped
, p_owner
->p_vout
);
676 vlc_mutex_lock( &p_owner
->lock
);
677 vout_UnregisterSubpictureChannel(p_owner
->p_vout
,
678 p_owner
->i_spu_channel
);
679 p_owner
->i_spu_channel
= VOUT_SPU_CHANNEL_INVALID
;
681 vout_Release(p_owner
->p_vout
);
682 p_owner
->p_vout
= NULL
; // the DecoderThread should not use the old vout anymore
683 vlc_mutex_unlock( &p_owner
->lock
);
688 if( p_owner
->p_vout
!= p_vout
)
690 if (p_owner
->p_vout
) /* notify the previous vout deletion unlocked */
691 decoder_Notify(p_owner
, on_vout_stopped
, p_owner
->p_vout
);
693 vlc_mutex_lock(&p_owner
->lock
);
697 /* Unregister the SPU channel of the previous vout */
698 assert(p_owner
->i_spu_channel
!= VOUT_SPU_CHANNEL_INVALID
);
699 vout_UnregisterSubpictureChannel(p_owner
->p_vout
,
700 p_owner
->i_spu_channel
);
701 vout_Release(p_owner
->p_vout
);
702 p_owner
->p_vout
= NULL
; // the DecoderThread should not use the old vout anymore
705 enum vlc_vout_order channel_order
;
706 p_owner
->i_spu_channel
=
707 vout_RegisterSubpictureChannelInternal(p_vout
, p_owner
->p_clock
,
709 p_owner
->i_spu_order
= 0;
711 if (p_owner
->i_spu_channel
== VOUT_SPU_CHANNEL_INVALID
)
713 /* The new vout doesn't support SPU, aborting... */
714 vlc_mutex_unlock(&p_owner
->lock
);
715 vout_Release(p_vout
);
719 p_owner
->p_vout
= p_vout
;
720 p_owner
->vout_order
= channel_order
;
721 vlc_mutex_unlock(&p_owner
->lock
);
723 assert(channel_order
!= VLC_VOUT_ORDER_NONE
);
724 decoder_Notify(p_owner
, on_vout_started
, p_vout
, channel_order
);
727 vout_Release(p_vout
);
729 p_subpic
= subpicture_New( p_updater
);
732 p_subpic
->i_channel
= p_owner
->i_spu_channel
;
733 p_subpic
->i_order
= p_owner
->i_spu_order
++;
734 p_subpic
->b_subtitle
= true;
740 static int InputThread_GetInputAttachments( decoder_t
*p_dec
,
741 input_attachment_t
***ppp_attachment
,
744 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
745 if (!p_owner
->cbs
|| !p_owner
->cbs
->get_attachments
)
748 int ret
= p_owner
->cbs
->get_attachments(p_owner
, ppp_attachment
,
749 p_owner
->cbs_userdata
);
752 *pi_attachment
= ret
;
756 static vlc_tick_t
ModuleThread_GetDisplayDate( decoder_t
*p_dec
,
757 vlc_tick_t system_now
, vlc_tick_t i_ts
)
759 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
761 vlc_mutex_lock( &p_owner
->lock
);
762 if( p_owner
->b_waiting
|| p_owner
->paused
)
763 i_ts
= VLC_TICK_INVALID
;
764 float rate
= p_owner
->output_rate
;
765 vlc_mutex_unlock( &p_owner
->lock
);
767 if( !p_owner
->p_clock
|| i_ts
== VLC_TICK_INVALID
)
770 return vlc_clock_ConvertToSystem( p_owner
->p_clock
, system_now
, i_ts
, rate
);
773 static float ModuleThread_GetDisplayRate( decoder_t
*p_dec
)
775 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
777 if( !p_owner
->p_clock
)
779 vlc_mutex_lock( &p_owner
->lock
);
780 float rate
= p_owner
->output_rate
;
781 vlc_mutex_unlock( &p_owner
->lock
);
785 /*****************************************************************************
787 *****************************************************************************/
788 block_t
*decoder_NewAudioBuffer( decoder_t
*dec
, int samples
)
790 assert( dec
->fmt_out
.audio
.i_frame_length
> 0
791 && dec
->fmt_out
.audio
.i_bytes_per_frame
> 0 );
793 size_t length
= samples
* dec
->fmt_out
.audio
.i_bytes_per_frame
794 / dec
->fmt_out
.audio
.i_frame_length
;
795 block_t
*block
= block_Alloc( length
);
796 if( likely(block
!= NULL
) )
798 block
->i_nb_samples
= samples
;
799 block
->i_pts
= block
->i_length
= 0;
804 static void RequestReload( vlc_input_decoder_t
*p_owner
)
806 /* Don't override reload if it's RELOAD_DECODER_AOUT */
807 int expected
= RELOAD_NO_REQUEST
;
808 atomic_compare_exchange_strong( &p_owner
->reload
, &expected
, RELOAD_DECODER
);
811 static void DecoderWaitUnblock( vlc_input_decoder_t
*p_owner
)
813 vlc_mutex_assert( &p_owner
->lock
);
817 if( !p_owner
->b_waiting
|| !p_owner
->b_has_data
)
819 vlc_cond_wait( &p_owner
->wait_request
, &p_owner
->lock
);
823 static inline void DecoderUpdatePreroll( vlc_tick_t
*pi_preroll
, const block_t
*p
)
825 if( p
->i_flags
& BLOCK_FLAG_PREROLL
)
826 *pi_preroll
= PREROLL_FORCED
;
827 /* Check if we can use the packet for end of preroll */
828 else if( (p
->i_flags
& BLOCK_FLAG_DISCONTINUITY
) &&
829 (p
->i_buffer
== 0 || (p
->i_flags
& BLOCK_FLAG_CORRUPTED
)) )
830 *pi_preroll
= PREROLL_FORCED
;
831 else if( p
->i_dts
!= VLC_TICK_INVALID
)
832 *pi_preroll
= __MIN( *pi_preroll
, p
->i_dts
);
833 else if( p
->i_pts
!= VLC_TICK_INVALID
)
834 *pi_preroll
= __MIN( *pi_preroll
, p
->i_pts
);
838 static int DecoderThread_PlaySout( vlc_input_decoder_t
*p_owner
, block_t
*p_sout_block
)
840 assert( !p_sout_block
->p_next
);
842 vlc_mutex_lock( &p_owner
->lock
);
844 if( p_owner
->b_waiting
)
846 p_owner
->b_has_data
= true;
847 vlc_cond_signal( &p_owner
->wait_acknowledge
);
850 DecoderWaitUnblock( p_owner
);
852 vlc_mutex_unlock( &p_owner
->lock
);
854 /* FIXME --VLC_TICK_INVALID inspect stream_output*/
855 return sout_InputSendBuffer( p_owner
->p_sout
, p_owner
->p_sout_input
,
859 /* This function process a block for sout
861 static void DecoderThread_ProcessSout( vlc_input_decoder_t
*p_owner
, block_t
*p_block
)
863 decoder_t
*p_dec
= &p_owner
->dec
;
864 block_t
*p_sout_block
;
865 block_t
**pp_block
= p_block
? &p_block
: NULL
;
867 while( ( p_sout_block
=
868 p_dec
->pf_packetize( p_dec
, pp_block
) ) )
870 if( p_owner
->p_sout_input
== NULL
)
872 vlc_mutex_lock( &p_owner
->lock
);
873 DecoderUpdateFormatLocked( p_owner
);
875 p_owner
->fmt
.i_group
= p_dec
->fmt_in
.i_group
;
876 p_owner
->fmt
.i_id
= p_dec
->fmt_in
.i_id
;
877 if( p_dec
->fmt_in
.psz_language
)
879 free( p_owner
->fmt
.psz_language
);
880 p_owner
->fmt
.psz_language
=
881 strdup( p_dec
->fmt_in
.psz_language
);
883 vlc_mutex_unlock( &p_owner
->lock
);
885 p_owner
->p_sout_input
=
886 sout_InputNew( p_owner
->p_sout
, &p_owner
->fmt
);
888 if( p_owner
->p_sout_input
== NULL
)
890 msg_Err( p_dec
, "cannot create packetized sout output (%4.4s)",
891 (char *)&p_owner
->fmt
.i_codec
);
892 p_owner
->error
= true;
895 block_Release(p_block
);
897 block_ChainRelease(p_sout_block
);
902 while( p_sout_block
)
904 block_t
*p_next
= p_sout_block
->p_next
;
906 p_sout_block
->p_next
= NULL
;
908 if( p_owner
->p_sout
->b_wants_substreams
&& p_dec
->pf_get_cc
)
910 if( p_owner
->cc
.p_sout_input
||
911 !p_owner
->cc
.b_sout_created
)
913 decoder_cc_desc_t desc
;
914 block_t
*p_cc
= p_dec
->pf_get_cc( p_dec
, &desc
);
917 if(!p_owner
->cc
.b_sout_created
)
920 es_format_Init(&ccfmt
, SPU_ES
, VLC_CODEC_CEA608
);
921 ccfmt
.i_group
= p_owner
->fmt
.i_group
;
922 ccfmt
.subs
.cc
.i_reorder_depth
= desc
.i_reorder_depth
;
923 p_owner
->cc
.p_sout_input
= sout_InputNew( p_owner
->p_sout
, &ccfmt
);
924 es_format_Clean(&ccfmt
);
925 p_owner
->cc
.b_sout_created
= true;
928 if( !p_owner
->cc
.p_sout_input
||
929 sout_InputSendBuffer( p_owner
->p_sout
, p_owner
->cc
.p_sout_input
, p_cc
) )
931 block_Release( p_cc
);
937 if( DecoderThread_PlaySout( p_owner
, p_sout_block
) == VLC_EGENERIC
)
939 msg_Err( p_dec
, "cannot continue streaming due to errors with codec %4.4s",
940 (char *)&p_owner
->fmt
.i_codec
);
942 p_owner
->error
= true;
947 block_Release( p_block
);
949 block_ChainRelease( p_next
);
953 p_sout_block
= p_next
;
959 static void DecoderPlayCc( vlc_input_decoder_t
*p_owner
, block_t
*p_cc
,
960 const decoder_cc_desc_t
*p_desc
)
962 vlc_mutex_lock( &p_owner
->lock
);
964 p_owner
->cc
.desc
= *p_desc
;
966 /* Fanout data to all decoders. We do not know if es_out
967 selected 608 or 708. */
968 uint64_t i_bitmap
= p_owner
->cc
.desc
.i_608_channels
|
969 p_owner
->cc
.desc
.i_708_channels
;
971 for( int i
=0; i_bitmap
> 0; i_bitmap
>>= 1, i
++ )
973 vlc_input_decoder_t
*p_ccowner
= p_owner
->cc
.pp_decoder
[i
];
979 block_FifoPut( p_ccowner
->p_fifo
, block_Duplicate(p_cc
) );
983 block_FifoPut( p_ccowner
->p_fifo
, p_cc
);
984 p_cc
= NULL
; /* was last dec */
988 vlc_mutex_unlock( &p_owner
->lock
);
990 if( p_cc
) /* can have bitmap set but no created decs */
991 block_Release( p_cc
);
994 static void PacketizerGetCc( vlc_input_decoder_t
*p_owner
, decoder_t
*p_dec_cc
)
997 decoder_cc_desc_t desc
;
999 /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1000 if( !p_owner
->cc
.b_supported
)
1003 assert( p_dec_cc
->pf_get_cc
!= NULL
);
1005 p_cc
= p_dec_cc
->pf_get_cc( p_dec_cc
, &desc
);
1008 DecoderPlayCc( p_owner
, p_cc
, &desc
);
1011 static void ModuleThread_QueueCc( decoder_t
*p_videodec
, block_t
*p_cc
,
1012 const decoder_cc_desc_t
*p_desc
)
1014 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_videodec
);
1016 if( unlikely( p_cc
!= NULL
) )
1018 if( p_owner
->cc
.b_supported
&&
1019 ( !p_owner
->p_packetizer
|| !p_owner
->p_packetizer
->pf_get_cc
) )
1020 DecoderPlayCc( p_owner
, p_cc
, p_desc
);
1022 block_Release( p_cc
);
1026 static int ModuleThread_PlayVideo( vlc_input_decoder_t
*p_owner
, picture_t
*p_picture
)
1028 decoder_t
*p_dec
= &p_owner
->dec
;
1029 vout_thread_t
*p_vout
= p_owner
->p_vout
;
1031 if( p_picture
->date
== VLC_TICK_INVALID
)
1032 /* FIXME: VLC_TICK_INVALID -- verify video_output */
1034 msg_Warn( p_dec
, "non-dated video buffer received" );
1035 picture_Release( p_picture
);
1036 return VLC_EGENERIC
;
1039 vlc_mutex_lock( &p_owner
->lock
);
1040 assert( p_owner
->vout_started
);
1042 bool prerolled
= p_owner
->i_preroll_end
!= PREROLL_NONE
;
1043 if( prerolled
&& p_owner
->i_preroll_end
> p_picture
->date
)
1045 vlc_mutex_unlock( &p_owner
->lock
);
1046 picture_Release( p_picture
);
1050 p_owner
->i_preroll_end
= PREROLL_NONE
;
1052 if( unlikely(prerolled
) )
1054 msg_Dbg( p_dec
, "end of video preroll" );
1057 vout_FlushAll( p_vout
);
1060 if( p_owner
->b_waiting
&& !p_owner
->b_first
)
1062 p_owner
->b_has_data
= true;
1063 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1066 DecoderWaitUnblock( p_owner
);
1068 if( p_owner
->b_waiting
)
1070 assert( p_owner
->b_first
);
1071 msg_Dbg( p_dec
, "Received first picture" );
1072 p_owner
->b_first
= false;
1073 p_picture
->b_force
= true;
1076 vlc_mutex_unlock( &p_owner
->lock
);
1078 /* FIXME: The *input* FIFO should not be locked here. This will not work
1079 * properly if/when pictures are queued asynchronously. */
1080 vlc_fifo_Lock( p_owner
->p_fifo
);
1081 if( unlikely(p_owner
->paused
) && likely(p_owner
->frames_countdown
> 0) )
1082 p_owner
->frames_countdown
--;
1083 vlc_fifo_Unlock( p_owner
->p_fifo
);
1086 if( p_vout
== NULL
)
1088 picture_Release( p_picture
);
1089 return VLC_EGENERIC
;
1092 if( p_picture
->b_still
)
1094 /* Ensure no earlier higher pts breaks still state */
1095 vout_Flush( p_vout
, p_picture
->date
);
1097 vout_PutPicture( p_vout
, p_picture
);
1102 static void ModuleThread_UpdateStatVideo( vlc_input_decoder_t
*p_owner
,
1105 unsigned displayed
= 0;
1106 unsigned vout_lost
= 0;
1107 unsigned vout_late
= 0;
1108 if( p_owner
->p_vout
!= NULL
)
1110 vout_GetResetStatistic( p_owner
->p_vout
, &displayed
, &vout_lost
, &vout_late
);
1112 if (lost
) vout_lost
++;
1114 decoder_Notify(p_owner
, on_new_video_stats
, 1, vout_lost
, displayed
, vout_late
);
1117 static void ModuleThread_QueueVideo( decoder_t
*p_dec
, picture_t
*p_pic
)
1120 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
1122 int success
= ModuleThread_PlayVideo( p_owner
, p_pic
);
1124 ModuleThread_UpdateStatVideo( p_owner
, success
!= VLC_SUCCESS
);
1127 static vlc_decoder_device
* thumbnailer_get_device( decoder_t
*p_dec
)
1130 // no hardware decoder on purpose
1131 // we don't want to load many DLLs and allocate many pictures
1132 // just to decode one picture
1136 static picture_t
*thumbnailer_buffer_new( decoder_t
*p_dec
)
1138 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
1139 /* Avoid decoding more than one frame when a thumbnail was
1140 * already generated */
1141 vlc_mutex_lock( &p_owner
->lock
);
1142 if( !p_owner
->b_first
)
1144 vlc_mutex_unlock( &p_owner
->lock
);
1147 vlc_mutex_unlock( &p_owner
->lock
);
1148 return picture_NewFromFormat( &p_dec
->fmt_out
.video
);
1151 static void ModuleThread_QueueThumbnail( decoder_t
*p_dec
, picture_t
*p_pic
)
1153 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
1156 vlc_mutex_lock( &p_owner
->lock
);
1157 b_first
= p_owner
->b_first
;
1158 p_owner
->b_first
= false;
1159 vlc_mutex_unlock( &p_owner
->lock
);
1162 decoder_Notify(p_owner
, on_thumbnail_ready
, p_pic
);
1163 picture_Release( p_pic
);
1167 static int ModuleThread_PlayAudio( vlc_input_decoder_t
*p_owner
, block_t
*p_audio
)
1169 decoder_t
*p_dec
= &p_owner
->dec
;
1171 assert( p_audio
!= NULL
);
1173 if( p_audio
->i_pts
== VLC_TICK_INVALID
) // FIXME --VLC_TICK_INVALID verify audio_output/*
1175 msg_Warn( p_dec
, "non-dated audio buffer received" );
1176 block_Release( p_audio
);
1177 return VLC_EGENERIC
;
1180 vlc_mutex_lock( &p_owner
->lock
);
1181 bool prerolled
= p_owner
->i_preroll_end
!= PREROLL_NONE
;
1182 if( prerolled
&& p_owner
->i_preroll_end
> p_audio
->i_pts
)
1184 vlc_mutex_unlock( &p_owner
->lock
);
1185 block_Release( p_audio
);
1189 p_owner
->i_preroll_end
= PREROLL_NONE
;
1190 vlc_mutex_unlock( &p_owner
->lock
);
1192 if( unlikely(prerolled
) )
1194 msg_Dbg( p_dec
, "end of audio preroll" );
1196 if( p_owner
->p_aout
)
1197 aout_DecFlush( p_owner
->p_aout
);
1202 vlc_mutex_lock( &p_owner
->lock
);
1203 if( p_owner
->b_waiting
)
1205 p_owner
->b_has_data
= true;
1206 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1210 DecoderWaitUnblock( p_owner
);
1211 vlc_mutex_unlock( &p_owner
->lock
);
1213 audio_output_t
*p_aout
= p_owner
->p_aout
;
1215 if( p_aout
== NULL
)
1217 msg_Dbg( p_dec
, "discarded audio buffer" );
1218 block_Release( p_audio
);
1219 return VLC_EGENERIC
;
1222 int status
= aout_DecPlay( p_aout
, p_audio
);
1223 if( status
== AOUT_DEC_CHANGED
)
1225 /* Only reload the decoder */
1226 RequestReload( p_owner
);
1228 else if( status
== AOUT_DEC_FAILED
)
1230 /* If we reload because the aout failed, we should release it. That
1231 * way, a next call to ModuleThread_UpdateAudioFormat() won't re-use the
1232 * previous (failing) aout but will try to create a new one. */
1233 atomic_store( &p_owner
->reload
, RELOAD_DECODER_AOUT
);
1238 static void ModuleThread_UpdateStatAudio( vlc_input_decoder_t
*p_owner
,
1241 unsigned played
= 0;
1242 unsigned aout_lost
= 0;
1243 if( p_owner
->p_aout
!= NULL
)
1245 aout_DecGetResetStats( p_owner
->p_aout
, &aout_lost
, &played
);
1247 if (lost
) aout_lost
++;
1249 decoder_Notify(p_owner
, on_new_audio_stats
, 1, aout_lost
, played
);
1252 static void ModuleThread_QueueAudio( decoder_t
*p_dec
, block_t
*p_aout_buf
)
1254 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
1256 int success
= ModuleThread_PlayAudio( p_owner
, p_aout_buf
);
1258 ModuleThread_UpdateStatAudio( p_owner
, success
!= VLC_SUCCESS
);
1261 static void ModuleThread_PlaySpu( vlc_input_decoder_t
*p_owner
, subpicture_t
*p_subpic
)
1263 decoder_t
*p_dec
= &p_owner
->dec
;
1264 vout_thread_t
*p_vout
= p_owner
->p_vout
;
1267 if( p_subpic
->i_start
== VLC_TICK_INVALID
)
1269 msg_Warn( p_dec
, "non-dated spu buffer received" );
1270 subpicture_Delete( p_subpic
);
1275 vlc_mutex_lock( &p_owner
->lock
);
1277 if( p_owner
->b_waiting
)
1279 p_owner
->b_has_data
= true;
1280 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1283 DecoderWaitUnblock( p_owner
);
1284 vlc_mutex_unlock( &p_owner
->lock
);
1286 if( p_subpic
->i_start
== VLC_TICK_INVALID
)
1288 subpicture_Delete( p_subpic
);
1292 vout_PutSubpicture( p_vout
, p_subpic
);
1295 static void ModuleThread_QueueSpu( decoder_t
*p_dec
, subpicture_t
*p_spu
)
1298 vlc_input_decoder_t
*p_owner
= dec_get_owner( p_dec
);
1300 /* The vout must be created from a previous decoder_NewSubpicture call. */
1301 assert( p_owner
->p_vout
);
1303 /* Preroll does not work very well with subtitle */
1304 vlc_mutex_lock( &p_owner
->lock
);
1305 if( p_spu
->i_start
!= VLC_TICK_INVALID
&&
1306 p_spu
->i_start
< p_owner
->i_preroll_end
&&
1307 ( p_spu
->i_stop
== VLC_TICK_INVALID
|| p_spu
->i_stop
< p_owner
->i_preroll_end
) )
1309 vlc_mutex_unlock( &p_owner
->lock
);
1310 subpicture_Delete( p_spu
);
1314 vlc_mutex_unlock( &p_owner
->lock
);
1315 ModuleThread_PlaySpu( p_owner
, p_spu
);
1319 static void DecoderThread_ProcessInput( vlc_input_decoder_t
*p_owner
, block_t
*p_block
);
1320 static void DecoderThread_DecodeBlock( vlc_input_decoder_t
*p_owner
, block_t
*p_block
)
1322 decoder_t
*p_dec
= &p_owner
->dec
;
1324 int ret
= p_dec
->pf_decode( p_dec
, p_block
);
1327 case VLCDEC_SUCCESS
:
1329 case VLCDEC_ECRITICAL
:
1330 p_owner
->error
= true;
1333 RequestReload( p_owner
);
1334 if( unlikely( p_block
== NULL
) )
1336 if( !( p_block
->i_flags
& BLOCK_FLAG_CORE_PRIVATE_RELOADED
) )
1338 p_block
->i_flags
|= BLOCK_FLAG_CORE_PRIVATE_RELOADED
;
1339 DecoderThread_ProcessInput( p_owner
, p_block
);
1341 else /* We prefer loosing this block than an infinite recursion */
1342 block_Release( p_block
);
1345 vlc_assert_unreachable();
1352 * \param p_dec the decoder object
1353 * \param p_block the block to decode
1355 static void DecoderThread_ProcessInput( vlc_input_decoder_t
*p_owner
, block_t
*p_block
)
1357 decoder_t
*p_dec
= &p_owner
->dec
;
1359 if( p_owner
->error
)
1362 /* Here, the atomic doesn't prevent to miss a reload request.
1363 * DecoderThread_ProcessInput() can still be called after the decoder module or the
1364 * audio output requested a reload. This will only result in a drop of an
1365 * input block or an output buffer. */
1367 if( ( reload
= atomic_exchange( &p_owner
->reload
, RELOAD_NO_REQUEST
) ) )
1369 msg_Warn( p_dec
, "Reloading the decoder module%s",
1370 reload
== RELOAD_DECODER_AOUT
? " and the audio output" : "" );
1372 if( DecoderThread_Reload( p_owner
, &p_dec
->fmt_in
, reload
) != VLC_SUCCESS
)
1376 bool packetize
= p_owner
->p_packetizer
!= NULL
;
1379 if( p_block
->i_buffer
<= 0 )
1382 vlc_mutex_lock( &p_owner
->lock
);
1383 DecoderUpdatePreroll( &p_owner
->i_preroll_end
, p_block
);
1384 vlc_mutex_unlock( &p_owner
->lock
);
1385 if( unlikely( p_block
->i_flags
& BLOCK_FLAG_CORE_PRIVATE_RELOADED
) )
1387 /* This block has already been packetized */
1393 if( p_owner
->p_sout
!= NULL
)
1395 DecoderThread_ProcessSout( p_owner
, p_block
);
1401 block_t
*p_packetized_block
;
1402 block_t
**pp_block
= p_block
? &p_block
: NULL
;
1403 decoder_t
*p_packetizer
= p_owner
->p_packetizer
;
1405 while( (p_packetized_block
=
1406 p_packetizer
->pf_packetize( p_packetizer
, pp_block
) ) )
1408 if( !es_format_IsSimilar( &p_dec
->fmt_in
, &p_packetizer
->fmt_out
) )
1410 msg_Dbg( p_dec
, "restarting module due to input format change");
1412 /* Drain the decoder module */
1413 DecoderThread_DecodeBlock( p_owner
, NULL
);
1415 if( DecoderThread_Reload( p_owner
, &p_packetizer
->fmt_out
,
1416 RELOAD_DECODER
) != VLC_SUCCESS
)
1418 block_ChainRelease( p_packetized_block
);
1423 if( p_packetizer
->pf_get_cc
)
1424 PacketizerGetCc( p_owner
, p_packetizer
);
1426 while( p_packetized_block
)
1428 block_t
*p_next
= p_packetized_block
->p_next
;
1429 p_packetized_block
->p_next
= NULL
;
1431 DecoderThread_DecodeBlock( p_owner
, p_packetized_block
);
1432 if( p_owner
->error
)
1434 block_ChainRelease( p_next
);
1438 p_packetized_block
= p_next
;
1441 /* Drain the decoder after the packetizer is drained */
1443 DecoderThread_DecodeBlock( p_owner
, NULL
);
1446 DecoderThread_DecodeBlock( p_owner
, p_block
);
1451 block_Release( p_block
);
1454 static void DecoderThread_Flush( vlc_input_decoder_t
*p_owner
)
1456 decoder_t
*p_dec
= &p_owner
->dec
;
1457 decoder_t
*p_packetizer
= p_owner
->p_packetizer
;
1459 if( p_owner
->error
)
1462 if( p_packetizer
!= NULL
&& p_packetizer
->pf_flush
!= NULL
)
1463 p_packetizer
->pf_flush( p_packetizer
);
1465 if ( p_dec
->pf_flush
!= NULL
)
1466 p_dec
->pf_flush( p_dec
);
1468 /* flush CC sub decoders */
1469 if( p_owner
->cc
.b_supported
)
1471 for( int i
=0; i
<MAX_CC_DECODERS
; i
++ )
1473 vlc_input_decoder_t
*p_ccowner
= p_owner
->cc
.pp_decoder
[i
];
1474 if( p_ccowner
&& p_ccowner
->dec
.pf_flush
)
1475 p_ccowner
->dec
.pf_flush( &p_ccowner
->dec
);
1479 vlc_mutex_lock( &p_owner
->lock
);
1481 if ( p_owner
->p_sout_input
!= NULL
)
1483 sout_InputFlush( p_owner
->p_sout
, p_owner
->p_sout_input
);
1486 if( p_dec
->fmt_out
.i_cat
== AUDIO_ES
)
1488 if( p_owner
->p_aout
)
1489 aout_DecFlush( p_owner
->p_aout
);
1491 else if( p_dec
->fmt_out
.i_cat
== VIDEO_ES
)
1493 if( p_owner
->p_vout
&& p_owner
->vout_started
)
1494 vout_FlushAll( p_owner
->p_vout
);
1496 /* Reset the pool cancel state, previously set by
1497 * vlc_input_decoder_Flush() */
1498 if( p_owner
->out_pool
!= NULL
)
1499 picture_pool_Cancel( p_owner
->out_pool
, false );
1501 else if( p_dec
->fmt_out
.i_cat
== SPU_ES
)
1503 if( p_owner
->p_vout
)
1505 assert( p_owner
->i_spu_channel
!= VOUT_SPU_CHANNEL_INVALID
);
1506 vout_FlushSubpictureChannel( p_owner
->p_vout
, p_owner
->i_spu_channel
);
1510 p_owner
->i_preroll_end
= PREROLL_NONE
;
1511 vlc_mutex_unlock( &p_owner
->lock
);
1514 static void DecoderThread_ChangePause( vlc_input_decoder_t
*p_owner
, bool paused
, vlc_tick_t date
)
1516 decoder_t
*p_dec
= &p_owner
->dec
;
1518 msg_Dbg( p_dec
, "toggling %s", paused
? "resume" : "pause" );
1519 switch( p_dec
->fmt_out
.i_cat
)
1522 vlc_mutex_lock( &p_owner
->lock
);
1523 if( p_owner
->p_vout
!= NULL
&& p_owner
->vout_started
)
1524 vout_ChangePause( p_owner
->p_vout
, paused
, date
);
1525 vlc_mutex_unlock( &p_owner
->lock
);
1528 vlc_mutex_lock( &p_owner
->lock
);
1529 if( p_owner
->p_aout
!= NULL
)
1530 aout_DecChangePause( p_owner
->p_aout
, paused
, date
);
1531 vlc_mutex_unlock( &p_owner
->lock
);
1536 vlc_assert_unreachable();
1540 static void DecoderThread_ChangeRate( vlc_input_decoder_t
*p_owner
, float rate
)
1542 decoder_t
*p_dec
= &p_owner
->dec
;
1544 msg_Dbg( p_dec
, "changing rate: %f", rate
);
1545 vlc_mutex_lock( &p_owner
->lock
);
1546 switch( p_dec
->fmt_out
.i_cat
)
1549 if( p_owner
->p_vout
!= NULL
&& p_owner
->vout_started
)
1550 vout_ChangeRate( p_owner
->p_vout
, rate
);
1553 if( p_owner
->p_aout
!= NULL
)
1554 aout_DecChangeRate( p_owner
->p_aout
, rate
);
1557 if( p_owner
->p_vout
!= NULL
)
1559 assert(p_owner
->i_spu_channel
!= VOUT_SPU_CHANNEL_INVALID
);
1560 vout_ChangeSpuRate(p_owner
->p_vout
, p_owner
->i_spu_channel
,
1565 vlc_assert_unreachable();
1567 p_owner
->output_rate
= rate
;
1568 vlc_mutex_unlock( &p_owner
->lock
);
1571 static void DecoderThread_ChangeDelay( vlc_input_decoder_t
*p_owner
, vlc_tick_t delay
)
1573 decoder_t
*p_dec
= &p_owner
->dec
;
1575 msg_Dbg( p_dec
, "changing delay: %"PRId64
, delay
);
1577 switch( p_dec
->fmt_out
.i_cat
)
1580 vlc_mutex_lock( &p_owner
->lock
);
1581 if( p_owner
->p_vout
!= NULL
&& p_owner
->vout_started
)
1582 vout_ChangeDelay( p_owner
->p_vout
, delay
);
1583 vlc_mutex_unlock( &p_owner
->lock
);
1586 vlc_mutex_lock( &p_owner
->lock
);
1587 if( p_owner
->p_aout
!= NULL
)
1588 aout_DecChangeDelay( p_owner
->p_aout
, delay
);
1589 vlc_mutex_unlock( &p_owner
->lock
);
1592 vlc_mutex_lock( &p_owner
->lock
);
1593 if( p_owner
->p_vout
!= NULL
)
1595 assert(p_owner
->i_spu_channel
!= VOUT_SPU_CHANNEL_INVALID
);
1596 vout_ChangeSpuDelay(p_owner
->p_vout
, p_owner
->i_spu_channel
,
1599 vlc_mutex_unlock( &p_owner
->lock
);
1602 vlc_assert_unreachable();
1607 * The decoding main loop
1609 * \param p_dec the decoder
1611 static void *DecoderThread( void *p_data
)
1613 vlc_input_decoder_t
*p_owner
= (vlc_input_decoder_t
*)p_data
;
1615 vlc_tick_t delay
= 0;
1616 bool paused
= false;
1618 /* The decoder's main loop */
1619 vlc_fifo_Lock( p_owner
->p_fifo
);
1621 while( !p_owner
->aborting
)
1623 if( p_owner
->flushing
)
1624 { /* Flush before/regardless of pause. We do not want to resume just
1625 * for the sake of flushing (glitches could otherwise happen). */
1626 vlc_fifo_Unlock( p_owner
->p_fifo
);
1628 /* Flush the decoder (and the output) */
1629 DecoderThread_Flush( p_owner
);
1631 vlc_fifo_Lock( p_owner
->p_fifo
);
1633 /* Reset flushing after DecoderThread_ProcessInput in case vlc_input_decoder_Flush
1634 * is called again. This will avoid a second useless flush (but
1636 p_owner
->flushing
= false;
1641 /* Reset the original pause/rate state when a new aout/vout is created:
1642 * this will trigger the DecoderThread_ChangePause/DecoderThread_ChangeRate code path
1644 if( p_owner
->reset_out_state
)
1649 p_owner
->reset_out_state
= false;
1652 if( paused
!= p_owner
->paused
)
1653 { /* Update playing/paused status of the output */
1654 vlc_tick_t date
= p_owner
->pause_date
;
1656 paused
= p_owner
->paused
;
1657 vlc_fifo_Unlock( p_owner
->p_fifo
);
1659 DecoderThread_ChangePause( p_owner
, paused
, date
);
1661 vlc_fifo_Lock( p_owner
->p_fifo
);
1665 if( rate
!= p_owner
->request_rate
)
1667 rate
= p_owner
->request_rate
;
1668 vlc_fifo_Unlock( p_owner
->p_fifo
);
1670 DecoderThread_ChangeRate( p_owner
, rate
);
1672 vlc_fifo_Lock( p_owner
->p_fifo
);
1676 if( delay
!= p_owner
->delay
)
1678 delay
= p_owner
->delay
;
1679 vlc_fifo_Unlock( p_owner
->p_fifo
);
1681 DecoderThread_ChangeDelay( p_owner
, delay
);
1683 vlc_fifo_Lock( p_owner
->p_fifo
);
1687 if( p_owner
->paused
&& p_owner
->frames_countdown
== 0 )
1688 { /* Wait for resumption from pause */
1689 p_owner
->b_idle
= true;
1690 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1691 vlc_fifo_Wait( p_owner
->p_fifo
);
1692 p_owner
->b_idle
= false;
1696 vlc_cond_signal( &p_owner
->wait_fifo
);
1698 block_t
*p_block
= vlc_fifo_DequeueUnlocked( p_owner
->p_fifo
);
1699 if( p_block
== NULL
)
1701 if( likely(!p_owner
->b_draining
) )
1702 { /* Wait for a block to decode (or a request to drain) */
1703 p_owner
->b_idle
= true;
1704 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1705 vlc_fifo_Wait( p_owner
->p_fifo
);
1706 p_owner
->b_idle
= false;
1709 /* We have emptied the FIFO and there is a pending request to
1710 * drain. Pass p_block = NULL to decoder just once. */
1713 vlc_fifo_Unlock( p_owner
->p_fifo
);
1715 DecoderThread_ProcessInput( p_owner
, p_block
);
1717 if( p_block
== NULL
&& p_owner
->dec
.fmt_out
.i_cat
== AUDIO_ES
)
1718 { /* Draining: the decoder is drained and all decoded buffers are
1719 * queued to the output at this point. Now drain the output. */
1720 if( p_owner
->p_aout
!= NULL
)
1721 aout_DecDrain( p_owner
->p_aout
);
1724 /* TODO? Wait for draining instead of polling. */
1725 vlc_mutex_lock( &p_owner
->lock
);
1726 vlc_fifo_Lock( p_owner
->p_fifo
);
1727 if( p_owner
->b_draining
&& (p_block
== NULL
) )
1729 p_owner
->b_draining
= false;
1730 p_owner
->drained
= true;
1732 vlc_cond_signal( &p_owner
->wait_acknowledge
);
1733 vlc_mutex_unlock( &p_owner
->lock
);
1736 vlc_fifo_Unlock( p_owner
->p_fifo
);
1740 static const struct decoder_owner_callbacks dec_video_cbs
=
1743 .get_device
= ModuleThread_GetDecoderDevice
,
1744 .format_update
= ModuleThread_UpdateVideoFormat
,
1745 .buffer_new
= ModuleThread_NewVideoBuffer
,
1746 .queue
= ModuleThread_QueueVideo
,
1747 .queue_cc
= ModuleThread_QueueCc
,
1748 .get_display_date
= ModuleThread_GetDisplayDate
,
1749 .get_display_rate
= ModuleThread_GetDisplayRate
,
1751 .get_attachments
= InputThread_GetInputAttachments
,
1753 static const struct decoder_owner_callbacks dec_thumbnailer_cbs
=
1756 .get_device
= thumbnailer_get_device
,
1757 .buffer_new
= thumbnailer_buffer_new
,
1758 .queue
= ModuleThread_QueueThumbnail
,
1760 .get_attachments
= InputThread_GetInputAttachments
,
1762 static const struct decoder_owner_callbacks dec_audio_cbs
=
1765 .format_update
= ModuleThread_UpdateAudioFormat
,
1766 .queue
= ModuleThread_QueueAudio
,
1768 .get_attachments
= InputThread_GetInputAttachments
,
1770 static const struct decoder_owner_callbacks dec_spu_cbs
=
1773 .buffer_new
= ModuleThread_NewSpuBuffer
,
1774 .queue
= ModuleThread_QueueSpu
,
1776 .get_attachments
= InputThread_GetInputAttachments
,
1780 * Create a decoder object
1782 * \param p_input the input thread
1783 * \param p_es the es descriptor
1784 * \param b_packetizer instead of a decoder
1785 * \return the decoder object
1787 static vlc_input_decoder_t
*
1788 CreateDecoder( vlc_object_t
*p_parent
,
1789 const es_format_t
*fmt
, vlc_clock_t
*p_clock
,
1790 input_resource_t
*p_resource
, sout_instance_t
*p_sout
,
1791 bool b_thumbnailing
, const struct vlc_input_decoder_callbacks
*cbs
,
1792 void *cbs_userdata
)
1795 vlc_input_decoder_t
*p_owner
;
1796 static_assert(offsetof(vlc_input_decoder_t
, dec
) == 0,
1797 "the decoder must be first in the owner structure");
1799 p_owner
= vlc_custom_create( p_parent
, sizeof( *p_owner
), "decoder" );
1800 if( p_owner
== NULL
)
1802 p_dec
= &p_owner
->dec
;
1804 p_owner
->p_clock
= p_clock
;
1805 p_owner
->i_preroll_end
= PREROLL_NONE
;
1806 p_owner
->p_resource
= p_resource
;
1808 p_owner
->cbs_userdata
= cbs_userdata
;
1809 p_owner
->p_aout
= NULL
;
1810 p_owner
->p_vout
= NULL
;
1811 p_owner
->vout_started
= false;
1812 p_owner
->i_spu_channel
= VOUT_SPU_CHANNEL_INVALID
;
1813 p_owner
->i_spu_order
= 0;
1814 p_owner
->p_sout
= p_sout
;
1815 p_owner
->p_sout_input
= NULL
;
1816 p_owner
->p_packetizer
= NULL
;
1818 atomic_init( &p_owner
->b_fmt_description
, false );
1819 p_owner
->p_description
= NULL
;
1821 p_owner
->reset_out_state
= false;
1823 p_owner
->output_rate
= p_owner
->request_rate
= 1.f
;
1824 p_owner
->paused
= false;
1825 p_owner
->pause_date
= VLC_TICK_INVALID
;
1826 p_owner
->frames_countdown
= 0;
1828 p_owner
->b_waiting
= false;
1829 p_owner
->b_first
= true;
1830 p_owner
->b_has_data
= false;
1832 p_owner
->error
= false;
1834 p_owner
->flushing
= false;
1835 p_owner
->b_draining
= false;
1836 p_owner
->drained
= false;
1837 atomic_init( &p_owner
->reload
, RELOAD_NO_REQUEST
);
1838 p_owner
->b_idle
= false;
1840 p_owner
->mouse_event
= NULL
;
1841 p_owner
->mouse_opaque
= NULL
;
1843 es_format_Init( &p_owner
->fmt
, fmt
->i_cat
, 0 );
1846 p_owner
->p_fifo
= block_FifoNew();
1847 if( unlikely(p_owner
->p_fifo
== NULL
) )
1849 vlc_object_delete(p_dec
);
1853 vlc_mutex_init( &p_owner
->lock
);
1854 vlc_mutex_init( &p_owner
->mouse_lock
);
1855 vlc_cond_init( &p_owner
->wait_request
);
1856 vlc_cond_init( &p_owner
->wait_acknowledge
);
1857 vlc_cond_init( &p_owner
->wait_fifo
);
1859 /* Load a packetizer module if the input is not already packetized */
1860 if( p_sout
== NULL
&& !fmt
->b_packetized
)
1862 p_owner
->p_packetizer
=
1863 vlc_custom_create( p_parent
, sizeof( decoder_t
), "packetizer" );
1864 if( p_owner
->p_packetizer
)
1866 if( LoadDecoder( p_owner
->p_packetizer
, true, fmt
) )
1868 vlc_object_delete(p_owner
->p_packetizer
);
1869 p_owner
->p_packetizer
= NULL
;
1873 p_owner
->p_packetizer
->fmt_out
.b_packetized
= true;
1874 fmt
= &p_owner
->p_packetizer
->fmt_out
;
1879 switch( fmt
->i_cat
)
1882 if( !b_thumbnailing
)
1883 p_dec
->cbs
= &dec_video_cbs
;
1885 p_dec
->cbs
= &dec_thumbnailer_cbs
;
1888 p_dec
->cbs
= &dec_audio_cbs
;
1891 p_dec
->cbs
= &dec_spu_cbs
;
1894 msg_Err( p_dec
, "unknown ES format" );
1898 /* Find a suitable decoder/packetizer module */
1899 if( LoadDecoder( p_dec
, p_sout
!= NULL
, fmt
) )
1902 assert( p_dec
->fmt_in
.i_cat
== p_dec
->fmt_out
.i_cat
&& fmt
->i_cat
== p_dec
->fmt_in
.i_cat
);
1904 /* Copy ourself the input replay gain */
1905 if( fmt
->i_cat
== AUDIO_ES
)
1907 for( unsigned i
= 0; i
< AUDIO_REPLAY_GAIN_MAX
; i
++ )
1909 if( !p_dec
->fmt_out
.audio_replay_gain
.pb_peak
[i
] )
1911 p_dec
->fmt_out
.audio_replay_gain
.pb_peak
[i
] = fmt
->audio_replay_gain
.pb_peak
[i
];
1912 p_dec
->fmt_out
.audio_replay_gain
.pf_peak
[i
] = fmt
->audio_replay_gain
.pf_peak
[i
];
1914 if( !p_dec
->fmt_out
.audio_replay_gain
.pb_gain
[i
] )
1916 p_dec
->fmt_out
.audio_replay_gain
.pb_gain
[i
] = fmt
->audio_replay_gain
.pb_gain
[i
];
1917 p_dec
->fmt_out
.audio_replay_gain
.pf_gain
[i
] = fmt
->audio_replay_gain
.pf_gain
[i
];
1923 p_owner
->cc
.b_supported
= ( p_sout
== NULL
);
1925 p_owner
->cc
.desc
.i_608_channels
= 0;
1926 p_owner
->cc
.desc
.i_708_channels
= 0;
1927 for( unsigned i
= 0; i
< MAX_CC_DECODERS
; i
++ )
1928 p_owner
->cc
.pp_decoder
[i
] = NULL
;
1929 p_owner
->cc
.p_sout_input
= NULL
;
1930 p_owner
->cc
.b_sout_created
= false;
1935 * Destroys a decoder object
1937 * \param p_dec the decoder object
1940 static void DeleteDecoder( vlc_input_decoder_t
*p_owner
)
1942 decoder_t
*p_dec
= &p_owner
->dec
;
1943 msg_Dbg( p_dec
, "killing decoder fourcc `%4.4s'",
1944 (char*)&p_dec
->fmt_in
.i_codec
);
1946 const enum es_format_category_e i_cat
=p_dec
->fmt_in
.i_cat
;
1947 decoder_Clean( p_dec
);
1948 if ( p_owner
->out_pool
)
1950 picture_pool_Release( p_owner
->out_pool
);
1951 p_owner
->out_pool
= NULL
;
1955 vlc_video_context_Release( p_owner
->vctx
);
1957 /* Free all packets still in the decoder fifo. */
1958 block_FifoRelease( p_owner
->p_fifo
);
1962 if( p_owner
->p_sout_input
)
1964 sout_InputDelete( p_owner
->p_sout
, p_owner
->p_sout_input
);
1965 if( p_owner
->cc
.p_sout_input
)
1966 sout_InputDelete( p_owner
->p_sout
, p_owner
->cc
.p_sout_input
);
1973 if( p_owner
->p_aout
)
1975 /* TODO: REVISIT gap-less audio */
1976 aout_DecDelete( p_owner
->p_aout
);
1977 input_resource_PutAout( p_owner
->p_resource
, p_owner
->p_aout
);
1981 vout_thread_t
*vout
= p_owner
->p_vout
;
1985 /* Hold the vout since PutVout will likely release it and a
1986 * last reference is needed for notify callbacks */
1990 input_resource_PutVout(p_owner
->p_resource
, vout
, &has_stopped
);
1992 decoder_Notify(p_owner
, on_vout_stopped
, vout
);
2000 if( p_owner
->p_vout
)
2002 assert( p_owner
->i_spu_channel
!= VOUT_SPU_CHANNEL_INVALID
);
2003 decoder_Notify(p_owner
, on_vout_stopped
, p_owner
->p_vout
);
2005 vout_UnregisterSubpictureChannel( p_owner
->p_vout
,
2006 p_owner
->i_spu_channel
);
2007 vout_Release(p_owner
->p_vout
);
2015 vlc_assert_unreachable();
2018 es_format_Clean( &p_owner
->fmt
);
2020 if( p_owner
->p_description
)
2021 vlc_meta_Delete( p_owner
->p_description
);
2023 decoder_Destroy( p_owner
->p_packetizer
);
2024 decoder_Destroy( &p_owner
->dec
);
2028 static void DecoderUnsupportedCodec( decoder_t
*p_dec
, const es_format_t
*fmt
, bool b_decoding
)
2030 if (fmt
->i_codec
!= VLC_CODEC_UNKNOWN
&& fmt
->i_codec
) {
2031 const char *desc
= vlc_fourcc_GetDescription(fmt
->i_cat
, fmt
->i_codec
);
2032 if (!desc
|| !*desc
)
2033 desc
= N_("No description for this codec");
2034 msg_Err( p_dec
, "Codec `%4.4s' (%s) is not supported.", (char*)&fmt
->i_codec
, desc
);
2035 vlc_dialog_display_error( p_dec
, _("Codec not supported"),
2036 _("VLC could not decode the format \"%4.4s\" (%s)"),
2037 (char*)&fmt
->i_codec
, desc
);
2038 } else if( b_decoding
){
2039 msg_Err( p_dec
, "could not identify codec" );
2040 vlc_dialog_display_error( p_dec
, _("Unidentified codec"),
2041 _("VLC could not identify the audio or video codec" ) );
2045 /* TODO: pass p_sout through p_resource? -- Courmisch */
2046 static vlc_input_decoder_t
*
2047 decoder_New( vlc_object_t
*p_parent
, const es_format_t
*fmt
,
2048 vlc_clock_t
*p_clock
, input_resource_t
*p_resource
,
2049 sout_instance_t
*p_sout
, bool thumbnailing
,
2050 const struct vlc_input_decoder_callbacks
*cbs
, void *userdata
)
2052 const char *psz_type
= p_sout
? N_("packetizer") : N_("decoder");
2055 /* Create the decoder configuration structure */
2056 vlc_input_decoder_t
*p_owner
=
2057 CreateDecoder( p_parent
, fmt
, p_clock
, p_resource
, p_sout
,
2058 thumbnailing
, cbs
, userdata
);
2059 if( p_owner
== NULL
)
2061 msg_Err( p_parent
, "could not create %s", psz_type
);
2062 vlc_dialog_display_error( p_parent
, _("Streaming / Transcoding failed"),
2063 _("VLC could not open the %s module."), vlc_gettext( psz_type
) );
2067 decoder_t
*p_dec
= &p_owner
->dec
;
2068 if( !p_dec
->p_module
)
2070 DecoderUnsupportedCodec( p_dec
, fmt
, !p_sout
);
2072 DeleteDecoder( p_owner
);
2076 assert( p_dec
->fmt_in
.i_cat
!= UNKNOWN_ES
);
2078 #if VLC_THREAD_PRIORITY_AUDIO != VLC_THREAD_PRIORITY_VIDEO
2079 if( p_dec
->fmt_in
.i_cat
== AUDIO_ES
)
2080 i_priority
= VLC_THREAD_PRIORITY_AUDIO
;
2083 i_priority
= VLC_THREAD_PRIORITY_VIDEO
;
2086 /* Do not delay sout creation for SPU or DATA. */
2087 if( p_sout
&& fmt
->b_packetized
&&
2088 (fmt
->i_cat
!= VIDEO_ES
&& fmt
->i_cat
!= AUDIO_ES
) )
2090 p_owner
->p_sout_input
= sout_InputNew( p_owner
->p_sout
, fmt
);
2091 if( p_owner
->p_sout_input
== NULL
)
2093 msg_Err( p_dec
, "cannot create sout input (%4.4s)",
2094 (char *)&fmt
->i_codec
);
2095 p_owner
->error
= true;
2100 /* Spawn the decoder thread */
2101 if( vlc_clone( &p_owner
->thread
, DecoderThread
, p_owner
, i_priority
) )
2103 msg_Err( p_dec
, "cannot spawn decoder thread" );
2104 DeleteDecoder( p_owner
);
2113 * Spawns a new decoder thread from the input thread
2115 * \param p_input the input thread
2116 * \param p_es the es descriptor
2117 * \return the spawned decoder object
2119 vlc_input_decoder_t
*
2120 vlc_input_decoder_New( vlc_object_t
*parent
, es_format_t
*fmt
,
2121 vlc_clock_t
*p_clock
, input_resource_t
*resource
,
2122 sout_instance_t
*p_sout
, bool thumbnailing
,
2123 const struct vlc_input_decoder_callbacks
*cbs
,
2126 return decoder_New( parent
, fmt
, p_clock
, resource
, p_sout
, thumbnailing
,
2127 cbs
, cbs_userdata
);
2131 * Spawn a decoder thread outside of the input thread.
2133 vlc_input_decoder_t
*
2134 vlc_input_decoder_Create( vlc_object_t
*p_parent
, const es_format_t
*fmt
,
2135 input_resource_t
*p_resource
)
2137 return decoder_New( p_parent
, fmt
, NULL
, p_resource
, NULL
, false, NULL
,
2143 * Kills a decoder thread and waits until it's finished
2145 * \param p_input the input thread
2146 * \param p_es the es descriptor
2149 void vlc_input_decoder_Delete( vlc_input_decoder_t
*p_owner
)
2151 decoder_t
*p_dec
= &p_owner
->dec
;
2153 vlc_fifo_Lock( p_owner
->p_fifo
);
2154 p_owner
->aborting
= true;
2155 p_owner
->flushing
= true;
2156 vlc_fifo_Signal( p_owner
->p_fifo
);
2157 vlc_fifo_Unlock( p_owner
->p_fifo
);
2159 /* Make sure we aren't waiting/decoding anymore */
2160 vlc_mutex_lock( &p_owner
->lock
);
2161 p_owner
->b_waiting
= false;
2162 vlc_cond_signal( &p_owner
->wait_request
);
2164 /* If the video output is paused or slow, or if the picture pool size was
2165 * under-estimated (e.g. greedy video filter, buggy decoder...), the
2166 * the picture pool may be empty, and the decoder thread or any decoder
2167 * module worker threads may be stuck waiting for free picture buffers.
2169 * This unblocks the thread, allowing the decoder module to join all its
2170 * worker threads (if any) and the decoder thread to terminate. */
2171 if( p_dec
->fmt_in
.i_cat
== VIDEO_ES
&& p_owner
->p_vout
!= NULL
2172 && p_owner
->vout_started
)
2174 if (p_owner
->out_pool
)
2175 picture_pool_Cancel( p_owner
->out_pool
, true );
2177 if( p_owner
->paused
)
2179 /* The DecoderThread could be stuck in pf_decode(). This is likely the
2180 * case with paused asynchronous decoder modules that have a limited
2181 * input and output pool size. Indeed, with such decoders, you have to
2182 * release an output buffer to get an input buffer. So, when paused and
2183 * flushed, the DecoderThread could be waiting for an output buffer to
2184 * be released (or rendered). In that case, the DecoderThread will
2185 * never be flushed since it be never leave pf_decode(). To fix this
2186 * issue, pre-flush the vout from here. The vout will have to be
2187 * flushed again since the module could be outputting more buffers just
2188 * after being unstuck. */
2190 vout_FlushAll( p_owner
->p_vout
);
2193 vlc_mutex_unlock( &p_owner
->lock
);
2195 vlc_join( p_owner
->thread
, NULL
);
2198 if( p_owner
->cc
.b_supported
)
2200 for( int i
= 0; i
< MAX_CC_DECODERS
; i
++ )
2201 vlc_input_decoder_SetCcState( p_owner
, VLC_CODEC_CEA608
, i
, false );
2204 /* Delete decoder */
2205 DeleteDecoder( p_owner
);
2209 * Put a block_t in the decoder's fifo.
2210 * Thread-safe w.r.t. the decoder. May be a cancellation point.
2212 * \param p_dec the decoder object
2213 * \param p_block the data block
2215 void vlc_input_decoder_Decode( vlc_input_decoder_t
*p_owner
, block_t
*p_block
,
2218 vlc_fifo_Lock( p_owner
->p_fifo
);
2221 /* FIXME: ideally we would check the time amount of data
2222 * in the FIFO instead of its size. */
2223 /* 400 MiB, i.e. ~ 50mb/s for 60s */
2224 if( vlc_fifo_GetBytes( p_owner
->p_fifo
) > 400*1024*1024 )
2226 msg_Warn( &p_owner
->dec
, "decoder/packetizer fifo full (data not "
2227 "consumed quickly enough), resetting fifo!" );
2228 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner
->p_fifo
) );
2229 p_block
->i_flags
|= BLOCK_FLAG_DISCONTINUITY
;
2233 if( !p_owner
->b_waiting
)
2234 { /* The FIFO is not consumed when waiting, so pacing would deadlock VLC.
2235 * Locking is not necessary as b_waiting is only read, not written by
2236 * the decoder thread. */
2237 while( vlc_fifo_GetCount( p_owner
->p_fifo
) >= 10 )
2238 vlc_fifo_WaitCond( p_owner
->p_fifo
, &p_owner
->wait_fifo
);
2241 vlc_fifo_QueueUnlocked( p_owner
->p_fifo
, p_block
);
2242 vlc_fifo_Unlock( p_owner
->p_fifo
);
2245 bool vlc_input_decoder_IsEmpty( vlc_input_decoder_t
* p_owner
)
2247 assert( !p_owner
->b_waiting
);
2249 vlc_fifo_Lock( p_owner
->p_fifo
);
2250 if( !vlc_fifo_IsEmpty( p_owner
->p_fifo
) || p_owner
->b_draining
)
2252 vlc_fifo_Unlock( p_owner
->p_fifo
);
2255 vlc_fifo_Unlock( p_owner
->p_fifo
);
2259 vlc_mutex_lock( &p_owner
->lock
);
2261 if( p_owner
->p_sout_input
!= NULL
)
2265 if( p_owner
->fmt
.i_cat
== VIDEO_ES
&& p_owner
->p_vout
!= NULL
)
2266 b_empty
= vout_IsEmpty( p_owner
->p_vout
);
2267 else if( p_owner
->fmt
.i_cat
== AUDIO_ES
)
2268 b_empty
= !p_owner
->b_draining
|| p_owner
->drained
;
2270 b_empty
= true; /* TODO subtitles support */
2271 vlc_mutex_unlock( &p_owner
->lock
);
2277 * Signals that there are no further blocks to decode, and requests that the
2278 * decoder drain all pending buffers. This is used to ensure that all
2279 * intermediate buffers empty and no samples get lost at the end of the stream.
2281 * @note The function does not actually wait for draining. It just signals that
2282 * draining should be performed once the decoder has emptied FIFO.
2284 void vlc_input_decoder_Drain( vlc_input_decoder_t
*p_owner
)
2286 vlc_fifo_Lock( p_owner
->p_fifo
);
2287 p_owner
->b_draining
= true;
2288 vlc_fifo_Signal( p_owner
->p_fifo
);
2289 vlc_fifo_Unlock( p_owner
->p_fifo
);
2293 * Requests that the decoder immediately discard all pending buffers.
2294 * This is useful when seeking or when deselecting a stream.
2296 void vlc_input_decoder_Flush( vlc_input_decoder_t
*p_owner
)
2298 enum es_format_category_e cat
= p_owner
->dec
.fmt_in
.i_cat
;
2300 vlc_fifo_Lock( p_owner
->p_fifo
);
2302 /* Empty the fifo */
2303 block_ChainRelease( vlc_fifo_DequeueAllUnlocked( p_owner
->p_fifo
) );
2305 /* Don't need to wait for the DecoderThread to flush. Indeed, if called a
2306 * second time, this function will clear the FIFO again before anything was
2307 * dequeued by DecoderThread and there is no need to flush a second time in
2309 p_owner
->flushing
= true;
2311 /* Flush video/spu decoder when paused: increment frames_countdown in order
2312 * to display one frame/subtitle */
2313 if( p_owner
->paused
&& ( cat
== VIDEO_ES
|| cat
== SPU_ES
)
2314 && p_owner
->frames_countdown
== 0 )
2315 p_owner
->frames_countdown
++;
2317 vlc_fifo_Signal( p_owner
->p_fifo
);
2319 vlc_fifo_Unlock( p_owner
->p_fifo
);
2321 if ( cat
== VIDEO_ES
)
2323 /* Set the pool cancel state. This will unblock the module if it is
2324 * waiting for new pictures (likely). This state will be reset back
2325 * from the DecoderThread once the flush request is processed. */
2326 vlc_mutex_lock( &p_owner
->lock
);
2327 if( p_owner
->out_pool
!= NULL
)
2328 picture_pool_Cancel( p_owner
->out_pool
, true );
2329 vlc_mutex_unlock( &p_owner
->lock
);
2332 if( p_owner
->paused
)
2334 /* The DecoderThread could be stuck in pf_decode(). This is likely the
2335 * case with paused asynchronous decoder modules that have a limited
2336 * input and output pool size. Indeed, with such decoders, you have to
2337 * release an output buffer to get an input buffer. So, when paused and
2338 * flushed, the DecoderThread could be waiting for an output buffer to
2339 * be released (or rendered). In that case, the DecoderThread will
2340 * never be flushed since it be never leave pf_decode(). To fix this
2341 * issue, pre-flush the vout from here. The vout will have to be
2342 * flushed again since the module could be outputting more buffers just
2343 * after being unstuck. */
2345 vlc_mutex_lock( &p_owner
->lock
);
2346 if( cat
== VIDEO_ES
&& p_owner
->p_vout
&& p_owner
->vout_started
)
2347 vout_FlushAll( p_owner
->p_vout
);
2348 vlc_mutex_unlock( &p_owner
->lock
);
2352 void vlc_input_decoder_GetCcDesc( vlc_input_decoder_t
*p_owner
,
2353 decoder_cc_desc_t
*p_desc
)
2355 vlc_mutex_lock( &p_owner
->lock
);
2356 *p_desc
= p_owner
->cc
.desc
;
2357 vlc_mutex_unlock( &p_owner
->lock
);
2360 static bool vlc_input_decoder_HasCCChanFlag( vlc_input_decoder_t
*p_owner
,
2361 vlc_fourcc_t codec
, int i_channel
)
2365 if( codec
== VLC_CODEC_CEA608
)
2368 i_bitmap
= p_owner
->cc
.desc
.i_608_channels
;
2370 else if( codec
== VLC_CODEC_CEA708
)
2372 i_max_channels
= 64;
2373 i_bitmap
= p_owner
->cc
.desc
.i_708_channels
;
2377 return ( i_channel
>= 0 && i_channel
< i_max_channels
&&
2378 ( i_bitmap
& ((uint64_t)1 << i_channel
) ) );
2381 int vlc_input_decoder_SetCcState( vlc_input_decoder_t
*p_owner
, vlc_fourcc_t codec
,
2382 int i_channel
, bool b_decode
)
2384 decoder_t
*p_dec
= &p_owner
->dec
;
2385 //msg_Warn( p_dec, "vlc_input_decoder_SetCcState: %d @%x", b_decode, i_channel );
2387 if( !vlc_input_decoder_HasCCChanFlag( p_owner
, codec
, i_channel
) )
2388 return VLC_EGENERIC
;
2392 vlc_input_decoder_t
*p_ccowner
;
2395 es_format_Init( &fmt
, SPU_ES
, codec
);
2396 fmt
.subs
.cc
.i_channel
= i_channel
;
2397 fmt
.subs
.cc
.i_reorder_depth
= p_owner
->cc
.desc
.i_reorder_depth
;
2398 p_ccowner
= vlc_input_decoder_New( VLC_OBJECT(p_dec
), &fmt
, p_owner
->p_clock
,
2399 p_owner
->p_resource
, p_owner
->p_sout
, false,
2403 msg_Err( p_dec
, "could not create decoder" );
2404 vlc_dialog_display_error( p_dec
,
2405 _("Streaming / Transcoding failed"), "%s",
2406 _("VLC could not open the decoder module.") );
2407 return VLC_EGENERIC
;
2409 else if( !p_ccowner
->dec
.p_module
)
2411 DecoderUnsupportedCodec( p_dec
, &fmt
, true );
2412 vlc_input_decoder_Delete(p_ccowner
);
2413 return VLC_EGENERIC
;
2415 p_ccowner
->p_clock
= p_owner
->p_clock
;
2417 vlc_mutex_lock( &p_owner
->lock
);
2418 p_owner
->cc
.pp_decoder
[i_channel
] = p_ccowner
;
2419 vlc_mutex_unlock( &p_owner
->lock
);
2423 vlc_input_decoder_t
*p_cc
;
2425 vlc_mutex_lock( &p_owner
->lock
);
2426 p_cc
= p_owner
->cc
.pp_decoder
[i_channel
];
2427 p_owner
->cc
.pp_decoder
[i_channel
] = NULL
;
2428 vlc_mutex_unlock( &p_owner
->lock
);
2431 vlc_input_decoder_Delete(p_cc
);
2436 int vlc_input_decoder_GetCcState( vlc_input_decoder_t
*p_owner
, vlc_fourcc_t codec
,
2437 int i_channel
, bool *pb_decode
)
2439 if( !vlc_input_decoder_HasCCChanFlag( p_owner
, codec
, i_channel
) )
2440 return VLC_EGENERIC
;
2442 vlc_mutex_lock( &p_owner
->lock
);
2443 *pb_decode
= p_owner
->cc
.pp_decoder
[i_channel
] != NULL
;
2444 vlc_mutex_unlock( &p_owner
->lock
);
2448 void vlc_input_decoder_ChangePause( vlc_input_decoder_t
*p_owner
,
2449 bool b_paused
, vlc_tick_t i_date
)
2451 /* Normally, p_owner->b_paused != b_paused here. But if a track is added
2452 * while the input is paused (e.g. add sub file), then b_paused is
2453 * (incorrectly) false. FIXME: This is a bug in the decoder owner. */
2454 vlc_fifo_Lock( p_owner
->p_fifo
);
2455 p_owner
->paused
= b_paused
;
2456 p_owner
->pause_date
= i_date
;
2457 p_owner
->frames_countdown
= 0;
2458 vlc_fifo_Signal( p_owner
->p_fifo
);
2459 vlc_fifo_Unlock( p_owner
->p_fifo
);
2462 void vlc_input_decoder_ChangeRate( vlc_input_decoder_t
*owner
, float rate
)
2464 vlc_fifo_Lock( owner
->p_fifo
);
2465 owner
->request_rate
= rate
;
2466 vlc_fifo_Unlock( owner
->p_fifo
);
2469 void vlc_input_decoder_ChangeDelay( vlc_input_decoder_t
*owner
, vlc_tick_t delay
)
2471 vlc_fifo_Lock( owner
->p_fifo
);
2472 owner
->delay
= delay
;
2473 vlc_fifo_Unlock( owner
->p_fifo
);
2476 void vlc_input_decoder_StartWait( vlc_input_decoder_t
*p_owner
)
2478 assert( !p_owner
->b_waiting
);
2480 vlc_mutex_lock( &p_owner
->lock
);
2481 p_owner
->b_first
= true;
2482 p_owner
->b_has_data
= false;
2483 p_owner
->b_waiting
= true;
2484 vlc_cond_signal( &p_owner
->wait_request
);
2485 vlc_mutex_unlock( &p_owner
->lock
);
2488 void vlc_input_decoder_StopWait( vlc_input_decoder_t
*p_owner
)
2490 assert( p_owner
->b_waiting
);
2492 vlc_mutex_lock( &p_owner
->lock
);
2493 p_owner
->b_waiting
= false;
2494 vlc_cond_signal( &p_owner
->wait_request
);
2495 vlc_mutex_unlock( &p_owner
->lock
);
2498 void vlc_input_decoder_Wait( vlc_input_decoder_t
*p_owner
)
2500 assert( p_owner
->b_waiting
);
2502 vlc_mutex_lock( &p_owner
->lock
);
2503 while( !p_owner
->b_has_data
)
2505 /* Don't need to lock p_owner->paused since it's only modified by the
2507 if( p_owner
->paused
)
2509 vlc_fifo_Lock( p_owner
->p_fifo
);
2510 if( p_owner
->b_idle
&& vlc_fifo_IsEmpty( p_owner
->p_fifo
) )
2512 msg_Err( &p_owner
->dec
, "buffer deadlock prevented" );
2513 vlc_fifo_Unlock( p_owner
->p_fifo
);
2516 vlc_fifo_Unlock( p_owner
->p_fifo
);
2517 vlc_cond_wait( &p_owner
->wait_acknowledge
, &p_owner
->lock
);
2519 vlc_mutex_unlock( &p_owner
->lock
);
2522 void vlc_input_decoder_FrameNext( vlc_input_decoder_t
*p_owner
,
2523 vlc_tick_t
*pi_duration
)
2525 assert( p_owner
->paused
);
2528 vlc_fifo_Lock( p_owner
->p_fifo
);
2529 p_owner
->frames_countdown
++;
2530 vlc_fifo_Signal( p_owner
->p_fifo
);
2531 vlc_fifo_Unlock( p_owner
->p_fifo
);
2533 vlc_mutex_lock( &p_owner
->lock
);
2534 if( p_owner
->dec
.fmt_in
.i_cat
== VIDEO_ES
)
2536 if( p_owner
->p_vout
)
2537 vout_NextPicture( p_owner
->p_vout
, pi_duration
);
2539 vlc_mutex_unlock( &p_owner
->lock
);
2542 bool vlc_input_decoder_HasFormatChanged( vlc_input_decoder_t
*p_owner
,
2543 es_format_t
*p_fmt
, vlc_meta_t
**pp_meta
)
2545 if( !atomic_exchange_explicit( &p_owner
->b_fmt_description
, false,
2546 memory_order_acquire
) )
2549 vlc_mutex_lock( &p_owner
->lock
);
2551 if( p_owner
->dec
.fmt_in
.i_cat
== UNKNOWN_ES
)
2553 /* The format changed but the output creation failed */
2554 vlc_mutex_unlock( &p_owner
->lock
);
2559 es_format_Copy( p_fmt
, &p_owner
->fmt
);
2564 if( p_owner
->p_description
)
2566 *pp_meta
= vlc_meta_New();
2568 vlc_meta_Merge( *pp_meta
, p_owner
->p_description
);
2571 vlc_mutex_unlock( &p_owner
->lock
);
2575 size_t vlc_input_decoder_GetFifoSize( vlc_input_decoder_t
*p_owner
)
2577 return block_FifoSize( p_owner
->p_fifo
);
2580 static bool DecoderHasVbi( decoder_t
*dec
)
2582 return dec
->fmt_in
.i_cat
== SPU_ES
&& dec
->fmt_in
.i_codec
== VLC_CODEC_TELETEXT
2583 && var_Type( dec
, "vbi-page" ) == VLC_VAR_INTEGER
;
2586 int vlc_input_decoder_GetVbiPage( vlc_input_decoder_t
*owner
, bool *opaque
)
2588 decoder_t
*dec
= &owner
->dec
;
2589 if( !DecoderHasVbi( dec
) )
2591 *opaque
= var_GetBool( dec
, "vbi-opaque" );
2592 return var_GetInteger( dec
, "vbi-page" );
2595 int vlc_input_decoder_SetVbiPage( vlc_input_decoder_t
*owner
, unsigned page
)
2597 decoder_t
*dec
= &owner
->dec
;
2598 if( !DecoderHasVbi( dec
) )
2599 return VLC_EGENERIC
;
2600 return var_SetInteger( dec
, "vbi-page", page
);
2603 int vlc_input_decoder_SetVbiOpaque( vlc_input_decoder_t
*owner
, bool opaque
)
2605 decoder_t
*dec
= &owner
->dec
;
2606 if( !DecoderHasVbi( dec
) )
2607 return VLC_EGENERIC
;
2608 return var_SetBool( dec
, "vbi-opaque", opaque
);
2611 void vlc_input_decoder_SetVoutMouseEvent( vlc_input_decoder_t
*owner
,
2612 vlc_mouse_event mouse_event
,
2615 assert( owner
->dec
.fmt_in
.i_cat
== VIDEO_ES
);
2617 vlc_mutex_lock( &owner
->mouse_lock
);
2619 owner
->mouse_event
= mouse_event
;
2620 owner
->mouse_opaque
= user_data
;
2622 vlc_mutex_unlock( &owner
->mouse_lock
);
2625 int vlc_input_decoder_AddVoutOverlay( vlc_input_decoder_t
*owner
, subpicture_t
*sub
,
2628 assert( owner
->dec
.fmt_in
.i_cat
== VIDEO_ES
);
2629 assert( sub
&& channel
);
2631 vlc_mutex_lock( &owner
->lock
);
2633 if( !owner
->p_vout
)
2635 vlc_mutex_unlock( &owner
->lock
);
2636 return VLC_EGENERIC
;
2638 ssize_t channel_id
=
2639 vout_RegisterSubpictureChannel( owner
->p_vout
);
2640 if (channel_id
== -1)
2642 vlc_mutex_unlock( &owner
->lock
);
2643 return VLC_EGENERIC
;
2645 sub
->i_start
= sub
->i_stop
= vlc_tick_now();
2646 sub
->i_channel
= *channel
= channel_id
;
2648 sub
->b_ephemer
= true;
2649 vout_PutSubpicture( owner
->p_vout
, sub
);
2651 vlc_mutex_unlock( &owner
->lock
);
2655 int vlc_input_decoder_DelVoutOverlay( vlc_input_decoder_t
*owner
, size_t channel
)
2657 assert( owner
->dec
.fmt_in
.i_cat
== VIDEO_ES
);
2659 vlc_mutex_lock( &owner
->lock
);
2661 if( !owner
->p_vout
)
2663 vlc_mutex_unlock( &owner
->lock
);
2664 return VLC_EGENERIC
;
2666 vout_UnregisterSubpictureChannel( owner
->p_vout
, channel
);
2668 vlc_mutex_unlock( &owner
->lock
);
2672 int vlc_input_decoder_SetSpuHighlight( vlc_input_decoder_t
*p_owner
,
2673 const vlc_spu_highlight_t
*spu_hl
)
2675 assert( p_owner
->dec
.fmt_in
.i_cat
== SPU_ES
);
2678 if( p_owner
->p_sout_input
)
2679 sout_InputControl( p_owner
->p_sout
, p_owner
->p_sout_input
,
2680 SOUT_INPUT_SET_SPU_HIGHLIGHT
, spu_hl
);
2683 vlc_mutex_lock( &p_owner
->lock
);
2684 if( !p_owner
->p_vout
)
2686 vlc_mutex_unlock( &p_owner
->lock
);
2687 return VLC_EGENERIC
;
2690 vout_SetSpuHighlight( p_owner
->p_vout
, spu_hl
);
2692 vlc_mutex_unlock( &p_owner
->lock
);