Repository navigation
Expand file tree
/
Copy pathdecoder.c
More file actions
2606 lines (2207 loc) · 78.1 KB
/
Copy pathdecoder.c
File metadata and controls
2606 lines (2207 loc) · 78.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* decoder.c: Functions for the management of decoders
*****************************************************************************
* Copyright (C) 1999-2019 VLC authors, VideoLAN and Videolabs SAS
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Gildas Bazin <gbazin@videolan.org>
* Laurent Aimar <fenrir@via.ecp.fr>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <stdatomic.h>
#include <vlc_common.h>
#include <vlc_block.h>
#include <vlc_vout.h>
#include <vlc_aout.h>
#include <vlc_sout.h>
#include <vlc_codec.h>
#include <vlc_spu.h>
#include <vlc_meta.h>
#include <vlc_dialog.h>
#include <vlc_modules.h>
#include <vlc_decoder.h>
#include "audio_output/aout_internal.h"
#include "stream_output/stream_output.h"
#include "../clock/clock.h"
#include "decoder.h"
#include "resource.h"
#include "../video_output/vout_internal.h"
/*
* Possibles values set in p_owner->reload atomic
*/
enum reload
{
RELOAD_NO_REQUEST,
RELOAD_DECODER, /* Reload the decoder module */
RELOAD_DECODER_AOUT /* Stop the aout and reload the decoder module */
};
struct decoder_owner
{
decoder_t dec;
input_resource_t*p_resource;
vlc_clock_t *p_clock;
const struct input_decoder_callbacks *cbs;
void *cbs_userdata;
ssize_t i_spu_channel;
int64_t i_spu_order;
sout_instance_t *p_sout;
sout_packetizer_input_t *p_sout_input;
vlc_thread_t thread;
void (*pf_update_stat)( struct decoder_owner *, unsigned decoded, unsigned lost );
/* Some decoders require already packetized data (ie. not truncated) */
decoder_t *p_packetizer;
bool b_packetizer;
/* Current format in use by the output */
es_format_t fmt;
/* */
atomic_bool b_fmt_description;
vlc_meta_t *p_description;
atomic_int reload;
/* fifo */
block_fifo_t *p_fifo;
/* Lock for communication with decoder thread */
vlc_mutex_t lock;
vlc_cond_t wait_request;
vlc_cond_t wait_acknowledge;
vlc_cond_t wait_fifo; /* TODO: merge with wait_acknowledge */
/* -- These variables need locking on write(only) -- */
audio_output_t *p_aout;
vout_thread_t *p_vout;
/* -- Theses variables need locking on read *and* write -- */
/* Preroll */
vlc_tick_t i_preroll_end;
/* Pause & Rate */
bool reset_out_state;
vlc_tick_t pause_date;
vlc_tick_t delay;
float request_rate, output_rate;
unsigned frames_countdown;
bool paused;
bool error;
/* Waiting */
bool b_waiting;
bool b_first;
bool b_has_data;
/* Flushing */
bool flushing;
bool b_draining;
atomic_bool drained;
bool b_idle;
/* CC */
#define MAX_CC_DECODERS 64 /* The es_out only creates one type of es */
struct
{
bool b_supported;
decoder_cc_desc_t desc;
decoder_t *pp_decoder[MAX_CC_DECODERS];
bool b_sout_created;
sout_packetizer_input_t *p_sout_input;
} cc;
/* Mouse event */
vlc_mutex_t mouse_lock;
vlc_mouse_event mouse_event;
void *mouse_opaque;
};
/* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
* a bogus PTS and won't be displayed */
#define DECODER_BOGUS_VIDEO_DELAY ((vlc_tick_t)(DEFAULT_PTS_DELAY * 30))
/* */
#define DECODER_SPU_VOUT_WAIT_DURATION VLC_TICK_FROM_MS(200)
#define BLOCK_FLAG_CORE_PRIVATE_RELOADED (1 << BLOCK_FLAG_CORE_PRIVATE_SHIFT)
#define decoder_Notify(decoder_priv, event, ...) \
if (decoder_priv->cbs && decoder_priv->cbs->event) \
decoder_priv->cbs->event(&decoder_priv->dec, __VA_ARGS__, \
decoder_priv->cbs_userdata);
static inline struct decoder_owner *dec_get_owner( decoder_t *p_dec )
{
return container_of( p_dec, struct decoder_owner, dec );
}
/**
* Load a decoder module
*/
static int LoadDecoder( decoder_t *p_dec, bool b_packetizer,
const es_format_t *restrict p_fmt )
{
decoder_Init( p_dec, p_fmt );
p_dec->b_frame_drop_allowed = true;
/* Find a suitable decoder/packetizer module */
if( !b_packetizer )
{
static const char caps[ES_CATEGORY_COUNT][16] = {
[VIDEO_ES] = "video decoder",
[AUDIO_ES] = "audio decoder",
[SPU_ES] = "spu decoder",
};
p_dec->p_module = module_need_var( p_dec, caps[p_dec->fmt_in.i_cat],
"codec" );
}
else
p_dec->p_module = module_need_var( p_dec, "packetizer", "packetizer" );
if( !p_dec->p_module )
{
decoder_Clean( p_dec );
return -1;
}
return 0;
}
static int ReloadDecoder( decoder_t *p_dec, bool b_packetizer,
const es_format_t *restrict p_fmt, enum reload reload )
{
/* Copy p_fmt since it can be destroyed by decoder_Clean */
struct decoder_owner *p_owner = dec_get_owner( p_dec );
es_format_t fmt_in;
if( es_format_Copy( &fmt_in, p_fmt ) != VLC_SUCCESS )
{
p_owner->error = true;
return VLC_EGENERIC;
}
/* Restart the decoder module */
decoder_Clean( p_dec );
p_owner->error = false;
if( reload == RELOAD_DECODER_AOUT )
{
assert( p_owner->fmt.i_cat == AUDIO_ES );
audio_output_t *p_aout = p_owner->p_aout;
vlc_mutex_lock( &p_owner->lock );
p_owner->p_aout = NULL;
vlc_mutex_unlock( &p_owner->lock );
if( p_aout )
{
aout_DecDelete( p_aout );
input_resource_PutAout( p_owner->p_resource, p_aout );
}
}
if( LoadDecoder( p_dec, b_packetizer, &fmt_in ) )
{
p_owner->error = true;
es_format_Clean( &fmt_in );
return VLC_EGENERIC;
}
es_format_Clean( &fmt_in );
return VLC_SUCCESS;
}
static void DecoderUpdateFormatLocked( decoder_t *p_dec )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
vlc_mutex_assert( &p_owner->lock );
es_format_Clean( &p_owner->fmt );
es_format_Copy( &p_owner->fmt, &p_dec->fmt_out );
/* Move p_description */
if( p_dec->p_description != NULL )
{
if( p_owner->p_description != NULL )
vlc_meta_Delete( p_owner->p_description );
p_owner->p_description = p_dec->p_description;
p_dec->p_description = NULL;
}
atomic_store_explicit( &p_owner->b_fmt_description, true,
memory_order_release );
}
static void MouseEvent( const vlc_mouse_t *newmouse, void *user_data )
{
decoder_t *dec = user_data;
struct decoder_owner *owner = dec_get_owner( dec );
vlc_mutex_lock( &owner->mouse_lock );
if( owner->mouse_event )
owner->mouse_event( newmouse, owner->mouse_opaque);
vlc_mutex_unlock( &owner->mouse_lock );
}
/*****************************************************************************
* Buffers allocation callbacks for the decoders
*****************************************************************************/
static bool aout_replaygain_changed( const audio_replay_gain_t *a,
const audio_replay_gain_t *b )
{
for( size_t i=0; i<AUDIO_REPLAY_GAIN_MAX; i++ )
{
if( a->pb_gain[i] != b->pb_gain[i] ||
a->pb_peak[i] != b->pb_peak[i] ||
a->pb_gain[i] != b->pb_gain[i] ||
a->pb_peak[i] != b->pb_peak[i] )
return true;
}
return false;
}
static int aout_update_format( decoder_t *p_dec )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
if( p_owner->p_aout &&
( !AOUT_FMTS_IDENTICAL(&p_dec->fmt_out.audio, &p_owner->fmt.audio) ||
p_dec->fmt_out.i_codec != p_dec->fmt_out.audio.i_format ||
p_dec->fmt_out.i_profile != p_owner->fmt.i_profile ) )
{
audio_output_t *p_aout = p_owner->p_aout;
/* Parameters changed, restart the aout */
vlc_mutex_lock( &p_owner->lock );
p_owner->p_aout = NULL;
vlc_mutex_unlock( &p_owner->lock );
aout_DecDelete( p_aout );
input_resource_PutAout( p_owner->p_resource, p_aout );
}
/* Check if only replay gain has changed */
if( aout_replaygain_changed( &p_dec->fmt_in.audio_replay_gain,
&p_owner->fmt.audio_replay_gain ) )
{
p_dec->fmt_out.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
if( p_owner->p_aout )
{
p_owner->fmt.audio_replay_gain = p_dec->fmt_in.audio_replay_gain;
var_TriggerCallback( p_owner->p_aout, "audio-replay-gain-mode" );
}
}
if( p_owner->p_aout == NULL )
{
p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
audio_sample_format_t format = p_dec->fmt_out.audio;
aout_FormatPrepare( &format );
const int i_force_dolby = var_InheritInteger( p_dec, "force-dolby-surround" );
if( i_force_dolby &&
format.i_physical_channels == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
{
if( i_force_dolby == 1 )
format.i_chan_mode |= AOUT_CHANMODE_DOLBYSTEREO;
else /* i_force_dolby == 2 */
format.i_chan_mode &= ~AOUT_CHANMODE_DOLBYSTEREO;
}
audio_output_t *p_aout;
p_aout = input_resource_GetAout( p_owner->p_resource );
if( p_aout )
{
/* TODO: 3.0 HACK: we need to put i_profile inside audio_format_t
* for 4.0 */
if( p_dec->fmt_out.i_codec == VLC_CODEC_DTS )
var_SetBool( p_aout, "dtshd", p_dec->fmt_out.i_profile > 0 );
if( aout_DecNew( p_aout, &format, p_owner->p_clock,
&p_dec->fmt_out.audio_replay_gain ) )
{
input_resource_PutAout( p_owner->p_resource, p_aout );
p_aout = NULL;
}
}
vlc_mutex_lock( &p_owner->lock );
p_owner->p_aout = p_aout;
DecoderUpdateFormatLocked( p_dec );
aout_FormatPrepare( &p_owner->fmt.audio );
vlc_mutex_unlock( &p_owner->lock );
if( p_aout == NULL )
{
msg_Err( p_dec, "failed to create audio output" );
return -1;
}
p_dec->fmt_out.audio.i_bytes_per_frame =
p_owner->fmt.audio.i_bytes_per_frame;
p_dec->fmt_out.audio.i_frame_length =
p_owner->fmt.audio.i_frame_length;
vlc_fifo_Lock( p_owner->p_fifo );
p_owner->reset_out_state = true;
vlc_fifo_Unlock( p_owner->p_fifo );
}
return 0;
}
static int vout_update_format( decoder_t *p_dec )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
bool need_vout = false;
bool need_format_update = false;
if( p_owner->p_vout == NULL )
{
msg_Dbg(p_dec, "vout: none found");
need_vout = true;
}
if( p_dec->fmt_out.video.i_width != p_owner->fmt.video.i_width
|| p_dec->fmt_out.video.i_height != p_owner->fmt.video.i_height )
{
msg_Dbg(p_dec, "vout change: decoder size");
need_vout = true;
}
if( p_dec->fmt_out.video.i_visible_width != p_owner->fmt.video.i_visible_width
|| p_dec->fmt_out.video.i_visible_height != p_owner->fmt.video.i_visible_height
|| p_dec->fmt_out.video.i_x_offset != p_owner->fmt.video.i_x_offset
|| p_dec->fmt_out.video.i_y_offset != p_owner->fmt.video.i_y_offset )
{
msg_Dbg(p_dec, "vout change: visible size");
need_vout = true;
}
if( p_dec->fmt_out.i_codec != p_owner->fmt.video.i_chroma )
{
msg_Dbg(p_dec, "vout change: chroma");
need_vout = true;
}
if( (int64_t)p_dec->fmt_out.video.i_sar_num * p_owner->fmt.video.i_sar_den !=
(int64_t)p_dec->fmt_out.video.i_sar_den * p_owner->fmt.video.i_sar_num )
{
msg_Dbg(p_dec, "vout change: SAR");
need_vout = true;
}
if( p_dec->fmt_out.video.orientation != p_owner->fmt.video.orientation )
{
msg_Dbg(p_dec, "vout change: orientation");
need_vout = true;
}
if( p_dec->fmt_out.video.multiview_mode != p_owner->fmt.video.multiview_mode )
{
msg_Dbg(p_dec, "vout change: multiview");
need_vout = true;
}
if ( memcmp( &p_dec->fmt_out.video.mastering,
&p_owner->fmt.video.mastering,
sizeof(p_owner->fmt.video.mastering)) )
{
msg_Dbg(p_dec, "vout update: mastering data");
need_format_update = true;
}
if ( p_dec->fmt_out.video.lighting.MaxCLL !=
p_owner->fmt.video.lighting.MaxCLL ||
p_dec->fmt_out.video.lighting.MaxFALL !=
p_owner->fmt.video.lighting.MaxFALL )
{
msg_Dbg(p_dec, "vout update: lighting data");
need_format_update = true;
}
if( need_vout )
{
vout_thread_t *p_vout;
if( !p_dec->fmt_out.video.i_width ||
!p_dec->fmt_out.video.i_height ||
p_dec->fmt_out.video.i_width < p_dec->fmt_out.video.i_visible_width ||
p_dec->fmt_out.video.i_height < p_dec->fmt_out.video.i_visible_height )
{
/* Can't create a new vout without display size */
return -1;
}
video_format_t fmt = p_dec->fmt_out.video;
fmt.i_chroma = p_dec->fmt_out.i_codec;
if( vlc_fourcc_IsYUV( fmt.i_chroma ) )
{
const vlc_chroma_description_t *dsc = vlc_fourcc_GetChromaDescription( fmt.i_chroma );
for( unsigned int i = 0; dsc && i < dsc->plane_count; i++ )
{
while( fmt.i_width % dsc->p[i].w.den )
fmt.i_width++;
while( fmt.i_height % dsc->p[i].h.den )
fmt.i_height++;
}
}
if( !fmt.i_visible_width || !fmt.i_visible_height )
{
if( p_dec->fmt_in.video.i_visible_width &&
p_dec->fmt_in.video.i_visible_height )
{
fmt.i_visible_width = p_dec->fmt_in.video.i_visible_width;
fmt.i_visible_height = p_dec->fmt_in.video.i_visible_height;
fmt.i_x_offset = p_dec->fmt_in.video.i_x_offset;
fmt.i_y_offset = p_dec->fmt_in.video.i_y_offset;
}
else
{
fmt.i_visible_width = fmt.i_width;
fmt.i_visible_height = fmt.i_height;
fmt.i_x_offset = 0;
fmt.i_y_offset = 0;
}
}
if( fmt.i_visible_height == 1088 &&
var_CreateGetBool( p_dec, "hdtv-fix" ) )
{
fmt.i_visible_height = 1080;
if( !(fmt.i_sar_num % 136))
{
fmt.i_sar_num *= 135;
fmt.i_sar_den *= 136;
}
msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
}
if( !fmt.i_sar_num || !fmt.i_sar_den )
{
fmt.i_sar_num = 1;
fmt.i_sar_den = 1;
}
vlc_ureduce( &fmt.i_sar_num, &fmt.i_sar_den,
fmt.i_sar_num, fmt.i_sar_den, 50000 );
video_format_AdjustColorSpace( &fmt );
vlc_mutex_lock( &p_owner->lock );
p_vout = p_owner->p_vout;
p_owner->p_vout = NULL;
vlc_mutex_unlock( &p_owner->lock );
unsigned dpb_size;
switch( p_dec->fmt_in.i_codec )
{
case VLC_CODEC_HEVC:
case VLC_CODEC_H264:
case VLC_CODEC_DIRAC: /* FIXME valid ? */
dpb_size = 18;
break;
case VLC_CODEC_AV1:
dpb_size = 10;
break;
case VLC_CODEC_VP5:
case VLC_CODEC_VP6:
case VLC_CODEC_VP6F:
case VLC_CODEC_VP8:
dpb_size = 3;
break;
default:
dpb_size = 2;
break;
}
enum vlc_vout_order order;
p_vout = input_resource_GetVout( p_owner->p_resource,
&(vout_configuration_t) {
.vout = p_vout, .clock = p_owner->p_clock, .fmt = &fmt,
.dpb_size = dpb_size + p_dec->i_extra_picture_buffers + 1,
.mouse_event = MouseEvent, .mouse_opaque = p_dec
}, &order );
if (p_vout)
decoder_Notify(p_owner, on_vout_added, p_vout, order);
vlc_mutex_lock( &p_owner->lock );
p_owner->p_vout = p_vout;
DecoderUpdateFormatLocked( p_dec );
p_owner->fmt.video.i_chroma = p_dec->fmt_out.i_codec;
vlc_mutex_unlock( &p_owner->lock );
if( p_vout == NULL )
{
msg_Err( p_dec, "failed to create video output" );
return -1;
}
vlc_fifo_Lock( p_owner->p_fifo );
p_owner->reset_out_state = true;
vlc_fifo_Unlock( p_owner->p_fifo );
}
else
if ( need_format_update )
{
/* the format has changed but we don't need a new vout */
vlc_mutex_lock( &p_owner->lock );
DecoderUpdateFormatLocked( p_dec );
vlc_mutex_unlock( &p_owner->lock );
}
return 0;
}
static picture_t *vout_new_buffer( decoder_t *p_dec )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
assert( p_owner->p_vout );
return vout_GetPicture( p_owner->p_vout );
}
static subpicture_t *spu_new_buffer( decoder_t *p_dec,
const subpicture_updater_t *p_updater )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
vout_thread_t *p_vout = NULL;
subpicture_t *p_subpic;
int i_attempts = 30;
while( i_attempts-- )
{
if( p_owner->error )
break;
p_vout = input_resource_HoldVout( p_owner->p_resource );
if( p_vout )
break;
vlc_tick_sleep( DECODER_SPU_VOUT_WAIT_DURATION );
}
if( !p_vout )
{
msg_Warn( p_dec, "no vout found, dropping subpicture" );
if( p_owner->p_vout )
{
assert(p_owner->i_spu_channel != VOUT_SPU_CHANNEL_INVALID);
decoder_Notify(p_owner, on_vout_deleted, p_owner->p_vout);
vlc_mutex_lock( &p_owner->lock );
vout_UnregisterSubpictureChannel(p_owner->p_vout,
p_owner->i_spu_channel);
p_owner->i_spu_channel = VOUT_SPU_CHANNEL_INVALID;
vout_Release(p_owner->p_vout);
p_owner->p_vout = NULL;
vlc_mutex_unlock( &p_owner->lock );
}
return NULL;
}
if( p_owner->p_vout != p_vout )
{
if (p_owner->p_vout) /* notify the previous vout deletion unlocked */
decoder_Notify(p_owner, on_vout_deleted, p_owner->p_vout);
vlc_mutex_lock(&p_owner->lock);
if (p_owner->p_vout)
{
/* Unregister the SPU channel of the previous vout */
assert(p_owner->i_spu_channel != VOUT_SPU_CHANNEL_INVALID);
vout_UnregisterSubpictureChannel(p_owner->p_vout,
p_owner->i_spu_channel);
vout_Release(p_owner->p_vout);
}
enum vlc_vout_order channel_order;
p_owner->i_spu_channel =
vout_RegisterSubpictureChannelInternal(p_vout, p_owner->p_clock,
&channel_order);
p_owner->i_spu_order = 0;
if (p_owner->i_spu_channel == VOUT_SPU_CHANNEL_INVALID)
{
/* The new vout doesn't support SPU, aborting... */
p_owner->p_vout = NULL;
vlc_mutex_unlock(&p_owner->lock);
vout_Release(p_vout);
return NULL;
}
p_owner->p_vout = p_vout;
vlc_mutex_unlock(&p_owner->lock);
assert(channel_order != VLC_VOUT_ORDER_NONE);
decoder_Notify(p_owner, on_vout_added, p_vout, channel_order);
}
else
vout_Release(p_vout);
p_subpic = subpicture_New( p_updater );
if( p_subpic )
{
p_subpic->i_channel = p_owner->i_spu_channel;
p_subpic->i_order = p_owner->i_spu_order++;
p_subpic->b_subtitle = true;
}
return p_subpic;
}
static int DecoderGetInputAttachments( decoder_t *p_dec,
input_attachment_t ***ppp_attachment,
int *pi_attachment )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
if (!p_owner->cbs || !p_owner->cbs->get_attachments)
return VLC_ENOOBJ;
int ret = p_owner->cbs->get_attachments(p_dec, ppp_attachment,
p_owner->cbs_userdata);
if (ret < 0)
return VLC_EGENERIC;
*pi_attachment = ret;
return VLC_SUCCESS;
}
static vlc_tick_t DecoderGetDisplayDate( decoder_t *p_dec, vlc_tick_t system_now,
vlc_tick_t i_ts )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
vlc_mutex_lock( &p_owner->lock );
if( p_owner->b_waiting || p_owner->paused )
i_ts = VLC_TICK_INVALID;
float rate = p_owner->output_rate;
vlc_mutex_unlock( &p_owner->lock );
if( !p_owner->p_clock || i_ts == VLC_TICK_INVALID )
return i_ts;
return vlc_clock_ConvertToSystem( p_owner->p_clock, system_now, i_ts, rate );
}
static float DecoderGetDisplayRate( decoder_t *p_dec )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
if( !p_owner->p_clock )
return 1.f;
vlc_mutex_lock( &p_owner->lock );
float rate = p_owner->output_rate;
vlc_mutex_unlock( &p_owner->lock );
return rate;
}
/*****************************************************************************
* Public functions
*****************************************************************************/
block_t *decoder_NewAudioBuffer( decoder_t *dec, int samples )
{
assert( dec->fmt_out.audio.i_frame_length > 0
&& dec->fmt_out.audio.i_bytes_per_frame > 0 );
size_t length = samples * dec->fmt_out.audio.i_bytes_per_frame
/ dec->fmt_out.audio.i_frame_length;
block_t *block = block_Alloc( length );
if( likely(block != NULL) )
{
block->i_nb_samples = samples;
block->i_pts = block->i_length = 0;
}
return block;
}
static void RequestReload( decoder_t * p_dec )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
/* Don't override reload if it's RELOAD_DECODER_AOUT */
int expected = RELOAD_NO_REQUEST;
atomic_compare_exchange_strong( &p_owner->reload, &expected, RELOAD_DECODER );
}
void decoder_AbortPictures( decoder_t *p_dec, bool b_abort )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
vlc_mutex_lock( &p_owner->lock );
if( p_owner->p_vout != NULL )
vout_Cancel( p_owner->p_vout, b_abort );
vlc_mutex_unlock( &p_owner->lock );
}
static void DecoderWaitUnblock( decoder_t *p_dec )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
vlc_mutex_assert( &p_owner->lock );
for( ;; )
{
if( !p_owner->b_waiting || !p_owner->b_has_data )
break;
vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
}
}
static inline void DecoderUpdatePreroll( vlc_tick_t *pi_preroll, const block_t *p )
{
if( p->i_flags & BLOCK_FLAG_PREROLL )
*pi_preroll = (vlc_tick_t)INT64_MAX;
/* Check if we can use the packet for end of preroll */
else if( (p->i_flags & BLOCK_FLAG_DISCONTINUITY) &&
(p->i_buffer == 0 || (p->i_flags & BLOCK_FLAG_CORRUPTED)) )
*pi_preroll = (vlc_tick_t)INT64_MAX;
else if( p->i_dts != VLC_TICK_INVALID )
*pi_preroll = __MIN( *pi_preroll, p->i_dts );
else if( p->i_pts != VLC_TICK_INVALID )
*pi_preroll = __MIN( *pi_preroll, p->i_pts );
}
#ifdef ENABLE_SOUT
static int DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
assert( !p_sout_block->p_next );
vlc_mutex_lock( &p_owner->lock );
if( p_owner->b_waiting )
{
p_owner->b_has_data = true;
vlc_cond_signal( &p_owner->wait_acknowledge );
}
DecoderWaitUnblock( p_dec );
vlc_mutex_unlock( &p_owner->lock );
/* FIXME --VLC_TICK_INVALID inspect stream_output*/
return sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
}
/* This function process a block for sout
*/
static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
block_t *p_sout_block;
block_t **pp_block = p_block ? &p_block : NULL;
while( ( p_sout_block =
p_dec->pf_packetize( p_dec, pp_block ) ) )
{
if( p_owner->p_sout_input == NULL )
{
vlc_mutex_lock( &p_owner->lock );
DecoderUpdateFormatLocked( p_dec );
p_owner->fmt.i_group = p_dec->fmt_in.i_group;
p_owner->fmt.i_id = p_dec->fmt_in.i_id;
if( p_dec->fmt_in.psz_language )
{
free( p_owner->fmt.psz_language );
p_owner->fmt.psz_language =
strdup( p_dec->fmt_in.psz_language );
}
vlc_mutex_unlock( &p_owner->lock );
p_owner->p_sout_input =
sout_InputNew( p_owner->p_sout, &p_owner->fmt );
if( p_owner->p_sout_input == NULL )
{
msg_Err( p_dec, "cannot create packetized sout output (%4.4s)",
(char *)&p_owner->fmt.i_codec );
p_owner->error = true;
if(p_block)
block_Release(p_block);
block_ChainRelease(p_sout_block);
break;
}
}
while( p_sout_block )
{
block_t *p_next = p_sout_block->p_next;
p_sout_block->p_next = NULL;
if( p_owner->p_sout->b_wants_substreams && p_dec->pf_get_cc )
{
if( p_owner->cc.p_sout_input ||
!p_owner->cc.b_sout_created )
{
decoder_cc_desc_t desc;
block_t *p_cc = p_dec->pf_get_cc( p_dec, &desc );
if( p_cc )
{
if(!p_owner->cc.b_sout_created)
{
es_format_t ccfmt;
es_format_Init(&ccfmt, SPU_ES, VLC_CODEC_CEA608);
ccfmt.i_group = p_owner->fmt.i_group;
ccfmt.subs.cc.i_reorder_depth = desc.i_reorder_depth;
p_owner->cc.p_sout_input = sout_InputNew( p_owner->p_sout, &ccfmt );
es_format_Clean(&ccfmt);
p_owner->cc.b_sout_created = true;
}
if( !p_owner->cc.p_sout_input ||
sout_InputSendBuffer( p_owner->cc.p_sout_input, p_cc ) )
{
block_Release( p_cc );
}
}
}
}
if( DecoderPlaySout( p_dec, p_sout_block ) == VLC_EGENERIC )
{
msg_Err( p_dec, "cannot continue streaming due to errors with codec %4.4s",
(char *)&p_owner->fmt.i_codec );
p_owner->error = true;
/* Cleanup */
if( p_block )
block_Release( p_block );
block_ChainRelease( p_next );
return;
}
p_sout_block = p_next;
}
}
}
#endif
static void DecoderPlayCc( decoder_t *p_dec, block_t *p_cc,
const decoder_cc_desc_t *p_desc )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
vlc_mutex_lock( &p_owner->lock );
p_owner->cc.desc = *p_desc;
/* Fanout data to all decoders. We do not know if es_out
selected 608 or 708. */
uint64_t i_bitmap = p_owner->cc.desc.i_608_channels |
p_owner->cc.desc.i_708_channels;
for( int i=0; i_bitmap > 0; i_bitmap >>= 1, i++ )
{
decoder_t *p_ccdec = p_owner->cc.pp_decoder[i];
struct decoder_owner *p_ccowner = dec_get_owner( p_ccdec );
if( !p_ccdec )
continue;
if( i_bitmap > 1 )
{
block_FifoPut( p_ccowner->p_fifo, block_Duplicate(p_cc) );
}
else
{
block_FifoPut( p_ccowner->p_fifo, p_cc );
p_cc = NULL; /* was last dec */
}
}
vlc_mutex_unlock( &p_owner->lock );
if( p_cc ) /* can have bitmap set but no created decs */
block_Release( p_cc );
}
static void PacketizerGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
block_t *p_cc;
decoder_cc_desc_t desc;
/* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
if( !p_owner->cc.b_supported )
return;
assert( p_dec_cc->pf_get_cc != NULL );
p_cc = p_dec_cc->pf_get_cc( p_dec_cc, &desc );
if( !p_cc )
return;
DecoderPlayCc( p_dec, p_cc, &desc );
}
static void DecoderQueueCc( decoder_t *p_videodec, block_t *p_cc,
const decoder_cc_desc_t *p_desc )
{
struct decoder_owner *p_owner = dec_get_owner( p_videodec );
if( unlikely( p_cc != NULL ) )
{
if( p_owner->cc.b_supported &&
( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
DecoderPlayCc( p_videodec, p_cc, p_desc );
else
block_Release( p_cc );
}
}
static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
unsigned *restrict pi_lost_sum )
{
struct decoder_owner *p_owner = dec_get_owner( p_dec );
vout_thread_t *p_vout = p_owner->p_vout;
bool prerolled;
vlc_mutex_lock( &p_owner->lock );
if( p_owner->i_preroll_end > p_picture->date )
{
vlc_mutex_unlock( &p_owner->lock );
picture_Release( p_picture );
return;
}
prerolled = p_owner->i_preroll_end > (vlc_tick_t)INT64_MIN;
p_owner->i_preroll_end = (vlc_tick_t)INT64_MIN;