forked from rellla/vdr-plugin-softhddevice-drm-gles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
softhddev.c
1902 lines (1686 loc) · 47.1 KB
/
softhddev.c
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
///
/// @file softhddev.c @brief A software HD device plugin for VDR.
///
/// Copyright (c) 2011 - 2015 by Johns. All Rights Reserved.
/// Copyright (c) 2018 - 2019 by zille. All Rights Reserved.
///
/// Contributor(s):
///
/// License: AGPLv3
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License.
///
/// 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 Affero General Public License for more details.
///
/// $Id$
//////////////////////////////////////////////////////////////////////////////
#define noDUMP_TRICKSPEED ///< dump raw trickspeed packets
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <assert.h>
#include <unistd.h>
#include <libintl.h>
#define _(str) gettext(str) ///< gettext shortcut
#define _N(str) str ///< gettext_noop shortcut
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/timestamp.h>
#include "iatomic.h" // portable atomic_t
#include "misc.h"
#include "softhddev.h"
#include "audio.h"
#include "video.h"
#include "codec.h"
//////////////////////////////////////////////////////////////////////////////
// Variables
//////////////////////////////////////////////////////////////////////////////
extern int ConfigAudioBufferTime; ///< config size ms of audio buffer
static volatile char StreamFreezed; ///< stream freezed
//////////////////////////////////////////////////////////////////////////////
// Video
//////////////////////////////////////////////////////////////////////////////
#define VIDEO_BUFFER_SIZE (512 * 1024) ///< video PES buffer default size
#define VIDEO_PACKET_MAX 192 ///< max number of video packets
/**
** Video output stream device structure. Parser, decoder, display.
*/
struct __video_stream__
{
VideoRender *Render; ///< video hardware decoder
VideoDecoder *Decoder; ///< video decoder
enum AVCodecID CodecID; ///< current codec id
AVCodecParameters * Par;
struct AVRational timebase;
volatile char NewStream; ///< flag new video stream
volatile char ClosingStream; ///< flag closing video stream
volatile char TrickSpeed; ///< current trick speed
AVPacket PacketRb[VIDEO_PACKET_MAX]; ///< PES packet ring buffer
int PacketWrite; ///< ring buffer write pointer
int PacketRead; ///< ring buffer read pointer
atomic_t PacketsFilled; ///< how many of the ring buffer is used
};
static VideoStream MyVideoStream[1]; ///< normal video stream
static pthread_mutex_t PktsLockMutex; ///< video packets lock mutex
//////////////////////////////////////////////////////////////////////////////
// Audio
//////////////////////////////////////////////////////////////////////////////
static volatile char NewAudioStream; ///< new audio stream
static volatile char SkipAudio; ///< skip audio stream
static AudioDecoder *MyAudioDecoder; ///< audio decoder
static enum AVCodecID AudioCodecID; ///< current codec id
static int AudioChannelID; ///< current audio channel id
//static VideoStream *AudioSyncStream; ///< video stream for audio/video sync
/// Minimum free space in audio buffer 8 packets for 8 channels
#define AUDIO_MIN_BUFFER_FREE (3072 * 8 * 8)
#define AUDIO_BUFFER_SIZE (512 * 1024) ///< audio PES buffer default size
static AVPacket AudioAvPkt[1]; ///< audio a/v packet
//////////////////////////////////////////////////////////////////////////////
// Audio codec parser
//////////////////////////////////////////////////////////////////////////////
///
/// Mpeg bitrate table.
///
/// BitRateTable[Version][Layer][Index]
///
static const uint16_t BitRateTable[2][4][16] = {
// MPEG Version 1
{{},
{0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,
0},
{0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
{0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}},
// MPEG Version 2 & 2.5
{{},
{0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
}
};
///
/// Mpeg samperate table.
///
static const uint16_t SampleRateTable[4] = {
44100, 48000, 32000, 0
};
///
/// Fast check for Mpeg audio.
///
/// 4 bytes 0xFFExxxxx Mpeg audio
///
static inline int FastMpegCheck(const uint8_t * p)
{
if (p[0] != 0xFF) { // 11bit frame sync
return 0;
}
if ((p[1] & 0xE0) != 0xE0) {
return 0;
}
if ((p[1] & 0x18) == 0x08) { // version ID - 01 reserved
return 0;
}
if (!(p[1] & 0x06)) { // layer description - 00 reserved
return 0;
}
if ((p[2] & 0xF0) == 0xF0) { // bitrate index - 1111 reserved
return 0;
}
if ((p[2] & 0x0C) == 0x0C) { // sampling rate index - 11 reserved
return 0;
}
return 1;
}
///
/// Check for Mpeg audio.
///
/// 0xFFEx already checked.
///
/// @param data incomplete PES packet
/// @param size number of bytes
///
/// @retval <0 possible mpeg audio, but need more data
/// @retval 0 no valid mpeg audio
/// @retval >0 valid mpeg audio
///
/// From: http://www.mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm
///
/// AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
///
/// o a 11x Frame sync
/// o b 2x Mpeg audio version (2.5, reserved, 2, 1)
/// o c 2x Layer (reserved, III, II, I)
/// o e 2x BitRate index
/// o f 2x SampleRate index (4100, 48000, 32000, 0)
/// o g 1x Paddding bit
/// o .. Doesn't care
///
/// frame length:
/// Layer I:
/// FrameLengthInBytes = (12 * BitRate / SampleRate + Padding) * 4
/// Layer II & III:
/// FrameLengthInBytes = 144 * BitRate / SampleRate + Padding
///
static int MpegCheck(const uint8_t * data, int size)
{
int mpeg2;
int mpeg25;
int layer;
int bit_rate_index;
int sample_rate_index;
int padding;
int bit_rate;
int sample_rate;
int frame_size;
mpeg2 = !(data[1] & 0x08) && (data[1] & 0x10);
mpeg25 = !(data[1] & 0x08) && !(data[1] & 0x10);
layer = 4 - ((data[1] >> 1) & 0x03);
bit_rate_index = (data[2] >> 4) & 0x0F;
sample_rate_index = (data[2] >> 2) & 0x03;
padding = (data[2] >> 1) & 0x01;
sample_rate = SampleRateTable[sample_rate_index];
if (!sample_rate) { // no valid sample rate try next
// moved into fast check
abort();
return 0;
}
sample_rate >>= mpeg2; // mpeg 2 half rate
sample_rate >>= mpeg25; // mpeg 2.5 quarter rate
bit_rate = BitRateTable[mpeg2 | mpeg25][layer][bit_rate_index];
if (!bit_rate) { // no valid bit-rate try next
// FIXME: move into fast check?
return 0;
}
bit_rate *= 1000;
switch (layer) {
case 1:
frame_size = (12 * bit_rate) / sample_rate;
frame_size = (frame_size + padding) * 4;
break;
case 2:
case 3:
default:
frame_size = (144 * bit_rate) / sample_rate;
frame_size = frame_size + padding;
break;
}
if (0) {
Debug(3,
"pesdemux: mpeg%s layer%d bitrate=%d samplerate=%d %d bytes\n",
mpeg25 ? "2.5" : mpeg2 ? "2" : "1", layer, bit_rate, sample_rate,
frame_size);
}
if (frame_size + 4 > size) {
return -frame_size - 4;
}
// check if after this frame a new mpeg frame starts
if (FastMpegCheck(data + frame_size)) {
return frame_size;
}
return 0;
}
///
/// Fast check for AAC LATM audio.
///
/// 3 bytes 0x56Exxx AAC LATM audio
///
static inline int FastLatmCheck(const uint8_t * p)
{
if (p[0] != 0x56) { // 11bit sync
return 0;
}
if ((p[1] & 0xE0) != 0xE0) {
return 0;
}
return 1;
}
///
/// Check for AAC LATM audio.
///
/// 0x56Exxx already checked.
///
/// @param data incomplete PES packet
/// @param size number of bytes
///
/// @retval <0 possible AAC LATM audio, but need more data
/// @retval 0 no valid AAC LATM audio
/// @retval >0 valid AAC LATM audio
///
static int LatmCheck(const uint8_t * data, int size)
{
int frame_size;
// 13 bit frame size without header
frame_size = ((data[1] & 0x1F) << 8) + data[2];
frame_size += 3;
if (frame_size + 2 > size) {
return -frame_size - 2;
}
// check if after this frame a new AAC LATM frame starts
if (FastLatmCheck(data + frame_size)) {
return frame_size;
}
return 0;
}
///
/// Possible AC-3 frame sizes.
///
/// from ATSC A/52 table 5.18 frame size code table.
///
const uint16_t Ac3FrameSizeTable[38][3] = {
{64, 69, 96}, {64, 70, 96}, {80, 87, 120}, {80, 88, 120},
{96, 104, 144}, {96, 105, 144}, {112, 121, 168}, {112, 122, 168},
{128, 139, 192}, {128, 140, 192}, {160, 174, 240}, {160, 175, 240},
{192, 208, 288}, {192, 209, 288}, {224, 243, 336}, {224, 244, 336},
{256, 278, 384}, {256, 279, 384}, {320, 348, 480}, {320, 349, 480},
{384, 417, 576}, {384, 418, 576}, {448, 487, 672}, {448, 488, 672},
{512, 557, 768}, {512, 558, 768}, {640, 696, 960}, {640, 697, 960},
{768, 835, 1152}, {768, 836, 1152}, {896, 975, 1344}, {896, 976, 1344},
{1024, 1114, 1536}, {1024, 1115, 1536}, {1152, 1253, 1728},
{1152, 1254, 1728}, {1280, 1393, 1920}, {1280, 1394, 1920},
};
///
/// Fast check for (E-)AC-3 audio.
///
/// 5 bytes 0x0B77xxxxxx AC-3 audio
///
static inline int FastAc3Check(const uint8_t * p)
{
if (p[0] != 0x0B) { // 16bit sync
return 0;
}
if (p[1] != 0x77) {
return 0;
}
return 1;
}
///
/// Check for (E-)AC-3 audio.
///
/// 0x0B77xxxxxx already checked.
///
/// @param data incomplete PES packet
/// @param size number of bytes
///
/// @retval <0 possible AC-3 audio, but need more data
/// @retval 0 no valid AC-3 audio
/// @retval >0 valid AC-3 audio
///
/// o AC-3 Header
/// AAAAAAAA AAAAAAAA BBBBBBBB BBBBBBBB CCDDDDDD EEEEEFFF
///
/// o a 16x Frame sync, always 0x0B77
/// o b 16x CRC 16
/// o c 2x Samplerate
/// o d 6x Framesize code
/// o e 5x Bitstream ID
/// o f 3x Bitstream mode
///
/// o E-AC-3 Header
/// AAAAAAAA AAAAAAAA BBCCCDDD DDDDDDDD EEFFGGGH IIIII...
///
/// o a 16x Frame sync, always 0x0B77
/// o b 2x Frame type
/// o c 3x Sub stream ID
/// o d 10x Framesize - 1 in words
/// o e 2x Framesize code
/// o f 2x Framesize code 2
///
static int Ac3Check(const uint8_t * data, int size)
{
int frame_size;
if (size < 5) { // need 5 bytes to see if AC-3/E-AC-3
return -5;
}
if (data[5] > (10 << 3)) { // E-AC-3
if ((data[4] & 0xF0) == 0xF0) { // invalid fscod fscod2
return 0;
}
frame_size = ((data[2] & 0x03) << 8) + data[3] + 1;
frame_size *= 2;
} else { // AC-3
int fscod;
int frmsizcod;
// crc1 crc1 fscod|frmsizcod
fscod = data[4] >> 6;
if (fscod == 0x03) { // invalid sample rate
return 0;
}
frmsizcod = data[4] & 0x3F;
if (frmsizcod > 37) { // invalid frame size
return 0;
}
// invalid is checked above
frame_size = Ac3FrameSizeTable[frmsizcod][fscod] * 2;
}
if (frame_size + 5 > size) {
return -frame_size - 5;
}
// FIXME: relaxed checks if codec is already detected
// check if after this frame a new AC-3 frame starts
if (FastAc3Check(data + frame_size)) {
return frame_size;
}
return 0;
}
///
/// Fast check for ADTS Audio Data Transport Stream.
///
/// 7/9 bytes 0xFFFxxxxxxxxxxx(xxxx) ADTS audio
///
static inline int FastAdtsCheck(const uint8_t * p)
{
if (p[0] != 0xFF) { // 12bit sync
return 0;
}
if ((p[1] & 0xF6) != 0xF0) { // sync + layer must be 0
return 0;
}
if ((p[2] & 0x3C) == 0x3C) { // sampling frequency index != 15
return 0;
}
return 1;
}
///
/// Check for ADTS Audio Data Transport Stream.
///
/// 0xFFF already checked.
///
/// @param data incomplete PES packet
/// @param size number of bytes
///
/// @retval <0 possible ADTS audio, but need more data
/// @retval 0 no valid ADTS audio
/// @retval >0 valid AC-3 audio
///
/// AAAAAAAA AAAABCCD EEFFFFGH HHIJKLMM MMMMMMMM MMMOOOOO OOOOOOPP
/// (QQQQQQQQ QQQQQQQ)
///
/// o A*12 syncword 0xFFF
/// o B*1 MPEG Version: 0 for MPEG-4, 1 for MPEG-2
/// o C*2 layer: always 0
/// o ..
/// o F*4 sampling frequency index (15 is invalid)
/// o ..
/// o M*13 frame length
///
static int AdtsCheck(const uint8_t * data, int size)
{
int frame_size;
if (size < 6) {
return -6;
}
frame_size = (data[3] & 0x03) << 11;
frame_size |= (data[4] & 0xFF) << 3;
frame_size |= (data[5] & 0xE0) >> 5;
if (frame_size + 3 > size) {
return -frame_size - 3;
}
// check if after this frame a new ADTS frame starts
if (FastAdtsCheck(data + frame_size)) {
return frame_size;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// PES Demux
//////////////////////////////////////////////////////////////////////////////
/**
** Play audio packet.
**
** @param data data of exactly one complete PES packet
** @param size size of PES packet
** @param id PES packet type
*/
int PlayAudio(const uint8_t * data, int size, uint8_t id)
{
int n;
const uint8_t *p;
AudioAvPkt->pts = AV_NOPTS_VALUE;
// fprintf(stderr, "[PlayAudio] size %d\n", size);
if (SkipAudio || !MyAudioDecoder) { // skip audio
return size;
}
if (StreamFreezed) { // stream freezed
return 0;
}
if (NewAudioStream) {
// this clears the audio ringbuffer indirect, open and setup does it
#ifdef DEBUG
fprintf(stderr, "PlayAudio: NewAudioStream\n");
#endif
CodecAudioClose(MyAudioDecoder);
// AudioFlushBuffers();
// AudioSetBufferTime(ConfigAudioBufferTime); // ???
AudioCodecID = AV_CODEC_ID_NONE;
AudioChannelID = -1;
NewAudioStream = 0;
}
// hard limit buffer full: don't overrun audio buffers on replay
if (AudioFreeBytes() < AUDIO_MIN_BUFFER_FREE) {
// fprintf(stderr, "PlayAudio: AudioFreeBytes %d < AUDIO_MIN_BUFFER_FREE %d\n",
// AudioFreeBytes(), AUDIO_MIN_BUFFER_FREE);
return 0;
}
// PES header 0x00 0x00 0x01 ID
// ID 0xBD 0xC0-0xCF
// must be a PES start code
if (size < 9 || !data || data[0] || data[1] || data[2] != 0x01) {
Error(_("[softhddev] invalid PES audio packet\n"));
return size;
}
n = data[8]; // header size
if (size < 9 + n + 4) { // wrong size
if (size == 9 + n) {
Warning(_("[softhddev] empty audio packet\n"));
} else {
Error(_("[softhddev] invalid audio packet %d bytes\n"), size);
}
return size;
}
if (data[7] & 0x80 && n >= 5) {
AudioAvPkt->pts =
(int64_t) (data[9] & 0x0E) << 29 | data[10] << 22 | (data[11] &
0xFE) << 14 | data[12] << 7 | (data[13] & 0xFE) >> 1;
//Debug(3, "audio: pts %#012" PRIx64 "\n", AudioAvPkt->pts);
}
p = data + 9 + n;
n = size - 9 - n; // skip pes header
if (n + AudioAvPkt->stream_index > AudioAvPkt->size) {
Fatal(_("[softhddev] audio buffer too small needed %d avail %d\n"),
n + AudioAvPkt->stream_index, AudioAvPkt->size);
AudioAvPkt->stream_index = 0;
}
if (AudioChannelID != id) { // id changed audio track changed
AudioChannelID = id;
AudioCodecID = AV_CODEC_ID_NONE;
Debug(3, "audio/demux: new channel id\n");
}
// Private stream + LPCM ID
if ((id & 0xF0) == 0xA0) {
if (n < 7) {
Error(_("[softhddev] invalid LPCM audio packet %d bytes\n"), size);
return size;
}
/* if (AudioCodecID != AV_CODEC_ID_PCM_DVD) {
static int samplerates[] = { 48000, 96000, 44100, 32000 };
int samplerate;
int channels;
int bits_per_sample;
Debug(3, "[softhddev]%s: LPCM %d sr:%d bits:%d chan:%d\n",
__FUNCTION__, id, p[5] >> 4, (((p[5] >> 6) & 0x3) + 4) * 4,
(p[5] & 0x7) + 1);
CodecAudioClose(MyAudioDecoder);
bits_per_sample = (((p[5] >> 6) & 0x3) + 4) * 4;
if (bits_per_sample != 16) {
Error(_
("[softhddev] LPCM %d bits per sample aren't supported\n"),
bits_per_sample);
// FIXME: handle unsupported formats.
}
samplerate = samplerates[p[5] >> 4];
channels = (p[5] & 0x7) + 1;
// FIXME: ConfigAudioBufferTime + x
// AudioSetBufferTime(400);
// AudioSetup(&samplerate, &channels, 0);
if (samplerate != samplerates[p[5] >> 4]) {
Error(_("[softhddev] LPCM %d sample-rate is unsupported\n"),
samplerates[p[5] >> 4]);
// FIXME: support resample
}
if (channels != (p[5] & 0x7) + 1) {
Error(_("[softhddev] LPCM %d channels are unsupported\n"),
(p[5] & 0x7) + 1);
// FIXME: support resample
}
//CodecAudioOpen(MyAudioDecoder, AV_CODEC_ID_PCM_DVD);
AudioCodecID = AV_CODEC_ID_PCM_DVD;
}
if (AudioAvPkt->pts != (int64_t) AV_NOPTS_VALUE) {
// AudioSetClock(AudioAvPkt->pts);
AudioAvPkt->pts = AV_NOPTS_VALUE;
}
swab(p + 7, AudioAvPkt->data, n - 7);
Audiofilter(AudioAvPkt->data, n - 7, NULL); // Das muss in ein AVFrame gepackt werden!!!
*/
return size;
}
// DVD track header
if ((id & 0xF0) == 0x80 && (p[0] & 0xF0) == 0x80) {
p += 4;
n -= 4; // skip track header
// if (AudioCodecID == AV_CODEC_ID_NONE) {
// // FIXME: ConfigAudioBufferTime + x
// AudioSetBufferTime(400);
// }
}
// append new packet, to partial old data
memcpy(AudioAvPkt->data + AudioAvPkt->stream_index, p, n);
AudioAvPkt->stream_index += n;
n = AudioAvPkt->stream_index;
p = AudioAvPkt->data;
while (n >= 5) {
int r;
unsigned codec_id;
// 4 bytes 0xFFExxxxx Mpeg audio
// 3 bytes 0x56Exxx AAC LATM audio
// 5 bytes 0x0B77xxxxxx AC-3 audio
// 6 bytes 0x0B77xxxxxxxx E-AC-3 audio
// 7/9 bytes 0xFFFxxxxxxxxxxx ADTS audio
// PCM audio can't be found
r = 0;
codec_id = AV_CODEC_ID_NONE; // keep compiler happy
if (id != 0xbd && FastMpegCheck(p)) {
r = MpegCheck(p, n);
codec_id = AV_CODEC_ID_MP2;
}
if (id != 0xbd && !r && FastLatmCheck(p)) {
r = LatmCheck(p, n);
codec_id = AV_CODEC_ID_AAC_LATM;
}
if ((id == 0xbd || (id & 0xF0) == 0x80) && !r && FastAc3Check(p)) {
r = Ac3Check(p, n);
codec_id = AV_CODEC_ID_AC3;
if (r > 0 && p[5] > (10 << 3)) {
codec_id = AV_CODEC_ID_EAC3;
}
/* faster ac3 detection at end of pes packet (no improvemnts)
if (AudioCodecID == codec_id && -r - 2 == n) {
r = n;
}
*/
}
if (id != 0xbd && !r && FastAdtsCheck(p)) {
r = AdtsCheck(p, n);
codec_id = AV_CODEC_ID_AAC;
}
if (r < 0) { // need more bytes
break;
}
if (r > 0) {
AVPacket avpkt[1];
// new codec id, close and open new
if (AudioCodecID != codec_id) {
AVRational timebase;
timebase.den = 90000;
timebase.num = 1;
CodecAudioClose(MyAudioDecoder);
CodecAudioOpen(MyAudioDecoder, codec_id, NULL, &timebase);
AudioCodecID = codec_id;
}
av_init_packet(avpkt);
avpkt->data = (void *)p;
avpkt->size = r;
avpkt->pts = AudioAvPkt->pts;
avpkt->dts = AudioAvPkt->dts;
// FIXME: not aligned for ffmpeg
CodecAudioDecode(MyAudioDecoder, avpkt);
AudioAvPkt->pts = AV_NOPTS_VALUE;
AudioAvPkt->dts = AV_NOPTS_VALUE;
p += r;
n -= r;
continue;
}
++p;
--n;
}
// copy remaining bytes to start of packet
if (n) {
memmove(AudioAvPkt->data, p, n);
}
AudioAvPkt->stream_index = n;
return size;
}
/**
** Clears all audio data from the decoder and ringbufffer.
*/
void ClearAudio(void)
{
if (!SkipAudio) {
#ifdef DEBUG
fprintf(stderr, "ClearAudio()\n");
#endif
CodecAudioFlushBuffers(MyAudioDecoder);
AudioFlushBuffers();
NewAudioStream = 1;
}
}
/**
** Set volume of audio device.
**
** @param volume VDR volume (0 .. 255)
*/
void SetVolumeDevice(int volume)
{
AudioSetVolume((volume * 1000) / 255);
}
/**
*** Resets channel ID (restarts audio).
**/
void ResetChannelId(void)
{
#ifdef DEBUG
fprintf(stderr, "ResetChannelId:\n");
#endif
AudioChannelID = -1;
Debug(3, "audio/demux: reset channel id\n");
}
//////////////////////////////////////////////////////////////////////////////
// Video
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
void PrintStreamData(const uint8_t *data, int size)
{
fprintf(stderr, "Data: %02x %02x %02x %02x %02x %02x %02x %02x %02x "
"%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x "
"%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x size %d\n",
data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[8],
data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16], data[17],
data[18], data[19], data[20], data[21], data[22], data[23], data[24], data[25], data[26],
data[27], data[28], data[29], data[30], data[31], data[32], data[33], data[34], size);
}
// helper functions to parse resolution from stream
const unsigned char * m_pStart;
unsigned short m_nLength;
int m_nCurrentBit;
unsigned int ReadBit()
{
assert(m_nCurrentBit <= m_nLength * 8);
int nIndex = m_nCurrentBit / 8;
int nOffset = m_nCurrentBit % 8 + 1;
m_nCurrentBit++;
return (m_pStart[nIndex] >> (8-nOffset)) & 0x01;
}
unsigned int ReadBits(int n)
{
int r = 0;
for (int i = 0; i < n; i++) {
r |= ( ReadBit() << ( n - i - 1 ) );
}
return r;
}
unsigned int ReadExponentialGolombCode()
{
int r = 0;
int i = 0;
while((ReadBit() == 0) && (i < 32)) {
i++;
}
r = ReadBits(i);
r += (1 << i) - 1;
return r;
}
unsigned int ReadSE()
{
int r = ReadExponentialGolombCode();
if (r & 0x01) {
r = (r+1)/2;
} else {
r = -(r/2);
}
return r;
}
void ParseResolutionH264(int *width, int *height)
{
AVPacket *avpkt;
m_pStart = NULL;
int i;
while (!VideoGetPackets()) {
usleep(10000);
}
avpkt = &MyVideoStream->PacketRb[MyVideoStream->PacketRead];
for (i = 0; i < avpkt->size; i++) {
if (!avpkt->data[i] && !avpkt->data[i + 1] && avpkt->data[i + 2] == 0x01 &&
(avpkt->data[i + 3] == 0x67 || avpkt->data[i + 3] == 0x27)) {
m_pStart = &avpkt->data[i + 4];
m_nLength = avpkt->size - i - 4;
break;
}
}
if (!m_pStart) {
fprintf(stderr, "ParseResolutionH264: No m_pStart %p Pkt %p Packets %d i %d\n",
m_pStart, avpkt, VideoGetPackets(), i);
PrintStreamData(avpkt->data, avpkt->size);
return;
}
m_nCurrentBit = 0;
int frame_crop_left_offset = 0;
int frame_crop_right_offset = 0;
int frame_crop_top_offset = 0;
int frame_crop_bottom_offset = 0;
int chroma_format_idc = 0;
int separate_colour_plane_flag = 0;
int profile_idc = ReadBits(8);
ReadBits(16);
ReadExponentialGolombCode();
if (profile_idc == 100 || profile_idc == 110 ||
profile_idc == 122 || profile_idc == 244 ||
profile_idc == 44 || profile_idc == 83 ||
profile_idc == 86 || profile_idc == 118) {
chroma_format_idc = ReadExponentialGolombCode();
if (chroma_format_idc == 3) {
separate_colour_plane_flag = ReadBit();
}
ReadExponentialGolombCode();
ReadExponentialGolombCode();
ReadBit();
int seq_scaling_matrix_present_flag = ReadBit();
if (seq_scaling_matrix_present_flag) {
for (int i = 0; i < 8; i++) {
int seq_scaling_list_present_flag = ReadBit();
if (seq_scaling_list_present_flag) {
int sizeOfScalingList = (i < 6) ? 16 : 64;
int lastScale = 8;
int nextScale = 8;
for (int j = 0; j < sizeOfScalingList; j++) {
if (nextScale != 0) {
int delta_scale = ReadSE();
nextScale = (lastScale + delta_scale + 256) % 256;
}
lastScale = (nextScale == 0) ? lastScale : nextScale;
}
}
}
}
}
ReadExponentialGolombCode();
int pic_order_cnt_type = ReadExponentialGolombCode();
if (pic_order_cnt_type == 0) {
ReadExponentialGolombCode();
} else if (pic_order_cnt_type == 1) {
ReadBit();
ReadSE();
ReadSE();
int num_ref_frames_in_pic_order_cnt_cycle = ReadExponentialGolombCode();
for (int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++ ) {
ReadSE();
}
}
ReadExponentialGolombCode();
ReadBit();
int pic_width_in_mbs_minus1 = ReadExponentialGolombCode();
int pic_height_in_map_units_minus1 = ReadExponentialGolombCode();
int frame_mbs_only_flag = ReadBit();
if (!frame_mbs_only_flag) {
ReadBit();
}
ReadBit();
int frame_cropping_flag = ReadBit();
if (frame_cropping_flag) {
frame_crop_left_offset = ReadExponentialGolombCode();
frame_crop_right_offset = ReadExponentialGolombCode();
frame_crop_top_offset = ReadExponentialGolombCode();
frame_crop_bottom_offset = ReadExponentialGolombCode();
}
int SubWidthC = 0;
int SubHeightC = 0;
if (chroma_format_idc == 0 && separate_colour_plane_flag == 0) { //monochrome
SubWidthC = SubHeightC = 2;
} else if (chroma_format_idc == 1 && separate_colour_plane_flag == 0) { //4:2:0
SubWidthC = SubHeightC = 2;
} else if (chroma_format_idc == 2 && separate_colour_plane_flag == 0) { //4:2:2
SubWidthC = 2;
SubHeightC = 1;
} else if (chroma_format_idc == 3) { //4:4:4
if (separate_colour_plane_flag == 0) {
SubWidthC = SubHeightC = 1;
} else if (separate_colour_plane_flag == 1) {
SubWidthC = SubHeightC = 0;
}
}
*width = ((pic_width_in_mbs_minus1 + 1) * 16) -
SubWidthC * (frame_crop_right_offset + frame_crop_left_offset);
*height = ((2 - frame_mbs_only_flag)* (pic_height_in_map_units_minus1 +1) * 16) -
SubHeightC * ((frame_crop_bottom_offset * 2) + (frame_crop_top_offset * 2));
}
/**
** Initialize video packet ringbuffer.
**
** @param stream video stream
*/
static void VideoPacketInit(VideoStream * stream)
{
for (int i = 0; i < VIDEO_PACKET_MAX; ++i) {
AVPacket *avpkt;
avpkt = &stream->PacketRb[i];
if (av_new_packet(avpkt, VIDEO_BUFFER_SIZE)) {
Fatal(_("[softhddev] out of memory\n"));
}
avpkt->size = 0;
}
atomic_set(&stream->PacketsFilled, 0);
stream->PacketRead = 0;
stream->PacketWrite = 0;
}
/**
** Cleanup video packet ringbuffer.
**
** @param stream video stream
*/
static void VideoPacketExit(VideoStream * stream)
{
atomic_set(&stream->PacketsFilled, 0);
for (int i = 0; i < VIDEO_PACKET_MAX; ++i) {
av_packet_unref(&stream->PacketRb[i]);
}
}
/**
** Place video data in packet ringbuffer.
**
** @param stream video stream
** @param pts presentation timestamp of pes packet
** @param data data of pes packet
** @param size size of pes packet
*/
static void VideoEnqueue(VideoStream * stream, int64_t pts, const void *data,
int size)
{
AVPacket *avpkt;
// PrintStreamData(data, size);
// fprintf(stderr, "VideoEnqueue: pts %s size %d\n",
// PtsTimestamp2String(pts), size);
avpkt = &stream->PacketRb[stream->PacketWrite];