-
Notifications
You must be signed in to change notification settings - Fork 11
/
codec.c
987 lines (868 loc) · 26.8 KB
/
codec.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
/// Copyright (C) 2009 - 2015 by Johns. All Rights Reserved.
/// Copyright (C) 2018 by pesintta, rofafor.
///
/// SPDX-License-Identifier: AGPL-3.0-only
///
/// This module contains all decoder and codec functions.
/// It is uses ffmpeg (http://ffmpeg.org) as backend.
///
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef __FreeBSD__
#include <sys/endian.h>
#else
#include <endian.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libavcodec/avcodec.h>
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57,64,100)
#error "libavcodec is too old - please, upgrade!"
#endif
#include <libavutil/mem.h>
#include <libavutil/opt.h>
#include <libavcodec/vaapi.h>
#include <libavutil/hwcontext.h>
#include <libavutil/hwcontext_vaapi.h>
#include <libswresample/swresample.h>
#if LIBSWRESAMPLE_VERSION_INT < AV_VERSION_INT(0,15,100)
#error "libswresample is too old - please, upgrade!"
#endif
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <pthread.h>
#include "iatomic.h"
#include "misc.h"
#include "video.h"
#include "audio.h"
#include "codec.h"
//----------------------------------------------------------------------------
// Global
//----------------------------------------------------------------------------
///
/// ffmpeg lock mutex
///
/// new ffmpeg dislikes simultanous open/close
/// this breaks our code, until this is fixed use lock.
///
static pthread_mutex_t CodecLockMutex;
//----------------------------------------------------------------------------
// Video
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Call-backs
//----------------------------------------------------------------------------
/**
** Callback to negotiate the PixelFormat.
**
** @param video_ctx codec context
** @param fmt is the list of formats which are supported by
** the codec, it is terminated by -1 as 0 is a
** valid format, the formats are ordered by
** quality.
*/
static enum AVPixelFormat Codec_get_format(AVCodecContext * video_ctx, const enum AVPixelFormat *fmt)
{
VideoDecoder *decoder = video_ctx->opaque;
return Video_get_format(decoder->HwDecoder, video_ctx, fmt);
}
/**
** Video buffer management, release buffer for frame.
** Called to release buffers which were allocated with get_buffer.
**
** @param opaque opaque data
** @param data buffer data
*/
static void Codec_free_buffer(void *opaque, uint8_t * data)
{
}
/**
** Video buffer management, get buffer for frame.
**
** Called at the beginning of each frame to get a buffer for it.
**
** @param video_ctx Codec context
** @param frame Get buffer for this frame
*/
static int Codec_get_buffer2(AVCodecContext * video_ctx, AVFrame * frame, int flags)
{
unsigned surface;
VideoDecoder *decoder = video_ctx->opaque;
if (frame->format != AV_PIX_FMT_VAAPI || !video_ctx->hw_frames_ctx
|| !(decoder->VideoCodec->capabilities & AV_CODEC_CAP_DR1))
return avcodec_default_get_buffer2(video_ctx, frame, flags);
pthread_mutex_lock(&CodecLockMutex);
surface = VideoGetSurface(decoder->HwDecoder, video_ctx);
// vaapi needs both fields set
frame->buf[0] = av_buffer_create((uint8_t *) (size_t) surface, 0, Codec_free_buffer, video_ctx, 0);
frame->data[0] = frame->buf[0]->data;
frame->data[3] = frame->data[0];
pthread_mutex_unlock(&CodecLockMutex);
return 0;
}
/**
** Allocate a new video decoder context.
**
** @param hw_decoder video hardware decoder
**
** @returns private decoder pointer for video decoder.
*/
VideoDecoder *CodecVideoNewDecoder(VideoHwDecoder * hw_decoder)
{
VideoDecoder *decoder;
if (!(decoder = calloc(1, sizeof(*decoder)))) {
Fatal("codec: can't allocate vodeo decoder");
}
decoder->HwDecoder = hw_decoder;
return decoder;
}
/**
** Deallocate a video decoder context.
**
** @param decoder private video decoder
*/
void CodecVideoDelDecoder(VideoDecoder * decoder)
{
free(decoder);
}
/**
** Open video decoder.
**
** @param decoder private video decoder
** @param codec_id video codec id
*/
void CodecVideoOpen(VideoDecoder * decoder, int codec_id)
{
AVCodec *video_codec;
Debug4("codec: using video codec ID %#06x (%s)", codec_id, avcodec_get_name(codec_id));
if (decoder->VideoCtx) {
Error("codec: missing close");
}
if (!(video_codec = avcodec_find_decoder(codec_id))) {
Fatal("codec: codec ID %#06x not found", codec_id);
// FIXME: none fatal
}
decoder->VideoCodec = video_codec;
if (!(decoder->VideoCtx = avcodec_alloc_context3(video_codec))) {
Fatal("codec: can't allocate video codec context");
}
if (!HwDeviceContext) {
Fatal("codec: no hw device context to be used");
}
decoder->VideoCtx->hw_device_ctx = av_buffer_ref(HwDeviceContext);
// FIXME: for software decoder use all cpus, otherwise 1
decoder->VideoCtx->thread_count = 1;
pthread_mutex_lock(&CodecLockMutex);
// open codec
if (video_codec->capabilities & (AV_CODEC_CAP_AUTO_THREADS)) {
Debug4("codec: auto threads enabled");
decoder->VideoCtx->thread_count = 0;
}
decoder->VideoCtx->opaque = decoder; // our structure
Debug4("codec: video '%s'", decoder->VideoCodec->long_name);
if (video_codec->capabilities & AV_CODEC_CAP_TRUNCATED) {
Debug4("codec: supports truncated packets");
//decoder->VideoCtx->flags |= CODEC_FLAG_TRUNCATED;
}
// FIXME: own memory management for video frames.
if (video_codec->capabilities & AV_CODEC_CAP_DR1) {
Debug4("codec: can use own buffer management");
}
if (video_codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) {
Debug4("codec: supports frame threads");
decoder->VideoCtx->thread_count = 0;
decoder->VideoCtx->thread_type |= FF_THREAD_FRAME;
}
if (video_codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) {
Debug4("codec: supports slice threads");
decoder->VideoCtx->thread_count = 0;
decoder->VideoCtx->thread_type |= FF_THREAD_SLICE;
}
decoder->VideoCtx->thread_safe_callbacks = 0;
decoder->VideoCtx->get_format = Codec_get_format;
decoder->VideoCtx->get_buffer2 = Codec_get_buffer2;
decoder->VideoCtx->draw_horiz_band = NULL;
av_opt_set_int(decoder->VideoCtx, "refcounted_frames", 1, 0);
if (avcodec_open2(decoder->VideoCtx, video_codec, NULL) < 0) {
pthread_mutex_unlock(&CodecLockMutex);
Fatal("codec: can't open video codec!");
}
pthread_mutex_unlock(&CodecLockMutex);
//
// Prepare frame buffer for decoder
//
if (!(decoder->Frame = av_frame_alloc())) {
Fatal("codec: can't allocate video decoder frame buffer");
}
}
/**
** Close video decoder.
**
** @param video_decoder private video decoder
*/
void CodecVideoClose(VideoDecoder * video_decoder)
{
// FIXME: play buffered data
av_frame_free(&video_decoder->Frame); // callee does checks
if (video_decoder->VideoCtx) {
pthread_mutex_lock(&CodecLockMutex);
avcodec_free_context(&video_decoder->VideoCtx);
pthread_mutex_unlock(&CodecLockMutex);
}
}
/**
** Decode a video packet.
**
** @param decoder video decoder data
** @param avpkt video packet
*/
void CodecVideoDecode(VideoDecoder * decoder, const AVPacket * avpkt)
{
AVCodecContext *video_ctx = decoder->VideoCtx;
if (video_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
int ret;
AVPacket pkt[1];
AVFrame *frame = decoder->Frame;
*pkt = *avpkt; // use copy
ret = avcodec_send_packet(video_ctx, pkt);
if (ret < 0) {
Debug4("codec: sending video packet failed");
return;
}
ret = avcodec_receive_frame(video_ctx, frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
Debug4("codec: receiving video frame failed");
return;
}
if (ret >= 0) {
VideoRenderFrame(decoder->HwDecoder, video_ctx, frame);
}
av_frame_unref(frame);
}
}
/**
** Flush the video decoder.
**
** @param decoder video decoder data
*/
void CodecVideoFlushBuffers(VideoDecoder * decoder)
{
if (decoder->VideoCtx) {
avcodec_flush_buffers(decoder->VideoCtx);
}
}
//----------------------------------------------------------------------------
// Audio
//----------------------------------------------------------------------------
///
/// Audio decoder structure.
///
struct _audio_decoder_
{
AVCodec *AudioCodec; ///< audio codec
AVCodecContext *AudioCtx; ///< audio codec context
char Passthrough; ///< current pass-through flags
int SampleRate; ///< current stream sample rate
int Channels; ///< current stream channels
int HwSampleRate; ///< hw sample rate
int HwChannels; ///< hw channels
AVFrame *Frame; ///< decoded audio frame buffer
SwrContext *Resample; ///< ffmpeg software resample context
uint16_t Spdif[24576 / 2]; ///< SPDIF output buffer
int SpdifIndex; ///< index into SPDIF output buffer
int SpdifCount; ///< SPDIF repeat counter
int64_t LastDelay; ///< last delay
struct timespec LastTime; ///< last time
int64_t LastPTS; ///< last PTS
int Drift; ///< accumulated audio drift
int DriftCorr; ///< audio drift correction value
int DriftFrac; ///< audio drift fraction for ac3
};
///
/// IEC Data type enumeration.
///
enum IEC61937
{
IEC61937_AC3 = 0x01, ///< AC-3 data
// FIXME: more data types
IEC61937_EAC3 = 0x15, ///< E-AC-3 data
};
#define CORRECT_PCM 1 ///< do PCM audio-drift correction
#define CORRECT_AC3 2 ///< do AC-3 audio-drift correction
static char CodecAudioDrift; ///< flag: enable audio-drift correction
///
/// Pass-through flags: CodecPCM, CodecAC3, CodecEAC3, ...
///
static char CodecPassthrough;
static char CodecDownmix; ///< enable AC-3 decoder downmix
/**
** Allocate a new audio decoder context.
**
** @returns private decoder pointer for audio decoder.
*/
AudioDecoder *CodecAudioNewDecoder(void)
{
AudioDecoder *audio_decoder;
if (!(audio_decoder = calloc(1, sizeof(*audio_decoder)))) {
Fatal("codec: can't allocate audio decoder");
}
if (!(audio_decoder->Frame = av_frame_alloc())) {
Fatal("codec: can't allocate audio decoder frame buffer");
}
return audio_decoder;
}
/**
** Deallocate an audio decoder context.
**
** @param decoder private audio decoder
*/
void CodecAudioDelDecoder(AudioDecoder * decoder)
{
av_frame_free(&decoder->Frame); // callee does checks
free(decoder);
}
/**
** Open audio decoder.
**
** @param audio_decoder private audio decoder
** @param codec_id audio codec id
*/
void CodecAudioOpen(AudioDecoder * audio_decoder, int codec_id)
{
AVCodec *audio_codec;
Debug4("codec: using audio codec ID %#06x (%s)", codec_id, avcodec_get_name(codec_id));
if (!(audio_codec = avcodec_find_decoder(codec_id))) {
Fatal("codec: codec ID %#06x not found", codec_id);
// FIXME: errors aren't fatal
}
audio_decoder->AudioCodec = audio_codec;
if (!(audio_decoder->AudioCtx = avcodec_alloc_context3(audio_codec))) {
Fatal("codec: can't allocate audio codec context");
}
if (CodecDownmix) {
audio_decoder->AudioCtx->request_channel_layout = AV_CH_LAYOUT_STEREO_DOWNMIX;
}
pthread_mutex_lock(&CodecLockMutex);
// open codec
{
AVDictionary *av_dict = NULL;
// FIXME: import settings
//av_dict_set(&av_dict, "dmix_mode", "0", 0);
//av_dict_set(&av_dict, "ltrt_cmixlev", "1.414", 0);
//av_dict_set(&av_dict, "loro_cmixlev", "1.414", 0);
if (avcodec_open2(audio_decoder->AudioCtx, audio_codec, &av_dict) < 0) {
pthread_mutex_unlock(&CodecLockMutex);
Fatal("codec: can't open audio codec");
}
av_dict_free(&av_dict);
}
pthread_mutex_unlock(&CodecLockMutex);
Debug4("codec: audio '%s'", audio_decoder->AudioCodec->long_name);
if (audio_codec->capabilities & AV_CODEC_CAP_TRUNCATED) {
Debug4("codec: audio can use truncated packets");
// we send only complete frames
// audio_decoder->AudioCtx->flags |= CODEC_FLAG_TRUNCATED;
}
audio_decoder->SampleRate = 0;
audio_decoder->Channels = 0;
audio_decoder->HwSampleRate = 0;
audio_decoder->HwChannels = 0;
audio_decoder->LastDelay = 0;
}
/**
** Close audio decoder.
**
** @param audio_decoder private audio decoder
*/
void CodecAudioClose(AudioDecoder * audio_decoder)
{
// FIXME: output any buffered data
if (audio_decoder->Resample) {
swr_free(&audio_decoder->Resample);
}
if (audio_decoder->AudioCtx) {
pthread_mutex_lock(&CodecLockMutex);
avcodec_free_context(&audio_decoder->AudioCtx);
pthread_mutex_unlock(&CodecLockMutex);
}
}
/**
** Set audio drift correction.
**
** @param mask enable mask (PCM, AC-3)
*/
void CodecSetAudioDrift(int mask)
{
CodecAudioDrift = mask & (CORRECT_PCM | CORRECT_AC3);
}
/**
** Set audio pass-through.
**
** @param mask enable mask (PCM, AC-3, E-AC-3)
*/
void CodecSetAudioPassthrough(int mask)
{
CodecPassthrough = mask & (CodecPCM | CodecAC3 | CodecEAC3);
}
/**
** Set audio downmix.
**
** @param onoff enable/disable downmix.
*/
void CodecSetAudioDownmix(int onoff)
{
if (onoff == -1) {
CodecDownmix ^= 1;
return;
}
CodecDownmix = onoff;
}
/**
** Reorder audio frame.
**
** ffmpeg L R C Ls Rs -> alsa L R Ls Rs C
** ffmpeg L R C LFE Ls Rs -> alsa L R Ls Rs C LFE
** ffmpeg L R C LFE Ls Rs Rl Rr -> alsa L R Ls Rs C LFE Rl Rr
**
** @param buf[IN,OUT] sample buffer
** @param size size of sample buffer in bytes
** @param channels number of channels interleaved in sample buffer
*/
static void CodecReorderAudioFrame(int16_t * buf, int size, int channels)
{
int i;
int c;
int ls;
int rs;
int lfe;
switch (channels) {
case 5:
size /= 2;
for (i = 0; i < size; i += 5) {
c = buf[i + 2];
ls = buf[i + 3];
rs = buf[i + 4];
buf[i + 2] = ls;
buf[i + 3] = rs;
buf[i + 4] = c;
}
break;
case 6:
size /= 2;
for (i = 0; i < size; i += 6) {
c = buf[i + 2];
lfe = buf[i + 3];
ls = buf[i + 4];
rs = buf[i + 5];
buf[i + 2] = ls;
buf[i + 3] = rs;
buf[i + 4] = c;
buf[i + 5] = lfe;
}
break;
case 8:
size /= 2;
for (i = 0; i < size; i += 8) {
c = buf[i + 2];
lfe = buf[i + 3];
ls = buf[i + 4];
rs = buf[i + 5];
buf[i + 2] = ls;
buf[i + 3] = rs;
buf[i + 4] = c;
buf[i + 5] = lfe;
}
break;
}
}
/**
** Handle audio format changes helper.
**
** @param audio_decoder audio decoder data
** @param[out] passthrough pass-through output
*/
static int CodecAudioUpdateHelper(AudioDecoder * audio_decoder, int *passthrough)
{
const AVCodecContext *audio_ctx;
int err;
audio_ctx = audio_decoder->AudioCtx;
Debug4("codec/audio: format change %s %dHz *%d channels%s%s%s%s%s", av_get_sample_fmt_name(audio_ctx->sample_fmt),
audio_ctx->sample_rate, audio_ctx->channels, (CodecPassthrough & CodecPCM) ? " PCM" : "",
(CodecPassthrough & CodecMPA) ? " MPA" : "", (CodecPassthrough & CodecAC3) ? " AC-3" : "",
(CodecPassthrough & CodecEAC3) ? " E-AC-3" : "", CodecPassthrough ? " pass-through" : "");
*passthrough = 0;
audio_decoder->SampleRate = audio_ctx->sample_rate;
audio_decoder->HwSampleRate = audio_ctx->sample_rate;
audio_decoder->Channels = audio_ctx->channels;
audio_decoder->HwChannels = audio_ctx->channels;
audio_decoder->Passthrough = CodecPassthrough;
// SPDIF/HDMI pass-through
if ((CodecPassthrough & CodecAC3 && audio_ctx->codec_id == AV_CODEC_ID_AC3)
|| (CodecPassthrough & CodecEAC3 && audio_ctx->codec_id == AV_CODEC_ID_EAC3)) {
if (audio_ctx->codec_id == AV_CODEC_ID_EAC3) {
// E-AC-3 over HDMI some receivers need HBR
audio_decoder->HwSampleRate *= 4;
}
audio_decoder->HwChannels = 2;
audio_decoder->SpdifIndex = 0; // reset buffer
audio_decoder->SpdifCount = 0;
*passthrough = 1;
}
// channels/sample-rate not support?
if ((err = AudioSetup(&audio_decoder->HwSampleRate, &audio_decoder->HwChannels, *passthrough))) {
// try E-AC-3 none HBR
audio_decoder->HwSampleRate /= 4;
if (audio_ctx->codec_id != AV_CODEC_ID_EAC3
|| (err = AudioSetup(&audio_decoder->HwSampleRate, &audio_decoder->HwChannels, *passthrough))) {
Debug4("codec/audio: audio setup error");
// FIXME: handle errors
audio_decoder->HwChannels = 0;
audio_decoder->HwSampleRate = 0;
return err;
}
}
Debug4("codec/audio: resample %s %dHz *%d -> %s %dHz *%d", av_get_sample_fmt_name(audio_ctx->sample_fmt),
audio_ctx->sample_rate, audio_ctx->channels, av_get_sample_fmt_name(AV_SAMPLE_FMT_S16),
audio_decoder->HwSampleRate, audio_decoder->HwChannels);
return 0;
}
/**
** Audio pass-through decoder helper.
**
** @param audio_decoder audio decoder data
** @param avpkt undecoded audio packet
*/
static int CodecAudioPassthroughHelper(AudioDecoder * audio_decoder, const AVPacket * avpkt)
{
const AVCodecContext *audio_ctx;
audio_ctx = audio_decoder->AudioCtx;
// SPDIF/HDMI passthrough
if (CodecPassthrough & CodecAC3 && audio_ctx->codec_id == AV_CODEC_ID_AC3) {
uint16_t *spdif;
int spdif_sz;
spdif = audio_decoder->Spdif;
spdif_sz = 6144;
// FIXME: this works with some TVs/AVReceivers
// FIXME: write burst size drift correction, which should work with all
if (CodecAudioDrift & CORRECT_AC3) {
int x;
x = (audio_decoder->DriftFrac +
(audio_decoder->DriftCorr * spdif_sz)) / (10 * audio_decoder->HwSampleRate * 100);
audio_decoder->DriftFrac =
(audio_decoder->DriftFrac +
(audio_decoder->DriftCorr * spdif_sz)) % (10 * audio_decoder->HwSampleRate * 100);
// round to word border
x *= audio_decoder->HwChannels * 4;
if (x < -64) { // limit correction
x = -64;
} else if (x > 64) {
x = 64;
}
spdif_sz += x;
}
// build SPDIF header and append A52 audio to it
// avpkt is the original data
if (spdif_sz < avpkt->size + 8) {
Error("codec/audio: decoded data smaller than encoded");
return -1;
}
spdif[0] = htole16(0xF872); // iec 61937 sync word
spdif[1] = htole16(0x4E1F);
spdif[2] = htole16(IEC61937_AC3 | (avpkt->data[5] & 0x07) << 8);
spdif[3] = htole16(avpkt->size * 8);
// copy original data for output
// FIXME: not 100% sure, if endian is correct on not intel hardware
swab(avpkt->data, spdif + 4, avpkt->size);
// FIXME: don't need to clear always
memset(spdif + 4 + avpkt->size / 2, 0, spdif_sz - 8 - avpkt->size);
// don't play with the ac-3 samples
AudioEnqueue(spdif, spdif_sz);
return 1;
}
if (CodecPassthrough & CodecEAC3 && audio_ctx->codec_id == AV_CODEC_ID_EAC3) {
uint16_t *spdif;
int spdif_sz;
int repeat;
// build SPDIF header and append A52 audio to it
// avpkt is the original data
spdif = audio_decoder->Spdif;
spdif_sz = 24576; // 4 * 6144
if (audio_decoder->HwSampleRate == 48000) {
spdif_sz = 6144;
}
if (spdif_sz < audio_decoder->SpdifIndex + avpkt->size + 8) {
Error("codec/audio: decoded data smaller than encoded");
return -1;
}
// check if we must pack multiple packets
repeat = 1;
if ((avpkt->data[4] & 0xc0) != 0xc0) { // fscod
static const uint8_t eac3_repeat[4] = { 6, 3, 2, 1 };
// fscod2
repeat = eac3_repeat[(avpkt->data[4] & 0x30) >> 4];
}
// copy original data for output
// pack upto repeat EAC-3 pakets into one IEC 61937 burst
// FIXME: not 100% sure, if endian is correct on not intel hardware
swab(avpkt->data, spdif + 4 + audio_decoder->SpdifIndex, avpkt->size);
audio_decoder->SpdifIndex += avpkt->size;
if (++audio_decoder->SpdifCount < repeat) {
return 1;
}
spdif[0] = htole16(0xF872); // iec 61937 sync word
spdif[1] = htole16(0x4E1F);
spdif[2] = htole16(IEC61937_EAC3);
spdif[3] = htole16(audio_decoder->SpdifIndex * 8);
memset(spdif + 4 + audio_decoder->SpdifIndex / 2, 0, spdif_sz - 8 - audio_decoder->SpdifIndex);
// don't play with the eac-3 samples
AudioEnqueue(spdif, spdif_sz);
audio_decoder->SpdifIndex = 0;
audio_decoder->SpdifCount = 0;
return 1;
}
return 0;
}
/**
** Set/update audio pts clock.
**
** @param audio_decoder audio decoder data
** @param pts presentation timestamp
*/
static void CodecAudioSetClock(AudioDecoder * audio_decoder, int64_t pts)
{
struct timespec nowtime;
int64_t delay;
int64_t tim_diff;
int64_t pts_diff;
int drift;
int corr = 0;
static int c;
AudioSetClock(pts);
delay = AudioGetDelay();
if (!delay) {
return;
}
clock_gettime(CLOCK_MONOTONIC, &nowtime);
if (!audio_decoder->LastDelay) {
audio_decoder->LastTime = nowtime;
audio_decoder->LastPTS = pts;
audio_decoder->LastDelay = delay;
audio_decoder->Drift = 0;
audio_decoder->DriftFrac = 0;
Debug4("codec/audio: initial drift delay %" PRId64 "ms", delay / 90);
return;
}
// collect over some time
pts_diff = pts - audio_decoder->LastPTS;
if (pts_diff < 10 * 1000 * 90) {
return;
}
tim_diff = (nowtime.tv_sec - audio_decoder->LastTime.tv_sec)
* 1000 * 1000 * 1000 + (nowtime.tv_nsec - audio_decoder->LastTime.tv_nsec);
drift = (tim_diff * 90) / (1000 * 1000) - pts_diff + delay - audio_decoder->LastDelay;
// adjust rounding error
nowtime.tv_nsec -= nowtime.tv_nsec % (1000 * 1000 / 90);
audio_decoder->LastTime = nowtime;
audio_decoder->LastPTS = pts;
audio_decoder->LastDelay = delay;
// underruns and av_resample have the same time :(((
if (abs(drift) > 10 * 90) {
// drift too big, pts changed?
Debug4("codec/audio: drift(%6d) %3dms reset", audio_decoder->DriftCorr, drift / 90);
audio_decoder->LastDelay = 0;
} else {
drift += audio_decoder->Drift;
audio_decoder->Drift = drift;
corr = (10 * audio_decoder->HwSampleRate * drift) / (90 * 1000);
// SPDIF/HDMI passthrough
if ((CodecAudioDrift & CORRECT_AC3) && (!(CodecPassthrough & CodecAC3)
|| audio_decoder->AudioCtx->codec_id != AV_CODEC_ID_AC3)
&& (!(CodecPassthrough & CodecEAC3)
|| audio_decoder->AudioCtx->codec_id != AV_CODEC_ID_EAC3)) {
audio_decoder->DriftCorr = -corr;
}
if (audio_decoder->DriftCorr < -20000) { // limit correction
audio_decoder->DriftCorr = -20000;
} else if (audio_decoder->DriftCorr > 20000) {
audio_decoder->DriftCorr = 20000;
}
}
if (audio_decoder->Resample && audio_decoder->DriftCorr) {
int distance;
// try workaround for buggy ffmpeg 0.10
if (abs(audio_decoder->DriftCorr) < 2000) {
distance = (pts_diff * audio_decoder->HwSampleRate) / (900 * 1000);
} else {
distance = (pts_diff * audio_decoder->HwSampleRate) / (90 * 1000);
}
if (swr_set_compensation(audio_decoder->Resample, audio_decoder->DriftCorr / 10, distance)) {
Debug4("codec/audio: swr_set_compensation failed");
}
}
if (!(c++ % 10)) {
Debug4("codec/audio: drift(%6d) %8dus %5d", audio_decoder->DriftCorr, drift * 1000 / 90, corr);
}
}
/**
** Handle audio format changes.
**
** @param audio_decoder audio decoder data
*/
static void CodecAudioUpdateFormat(AudioDecoder * audio_decoder)
{
int passthrough;
const AVCodecContext *audio_ctx;
if (CodecAudioUpdateHelper(audio_decoder, &passthrough)) {
// FIXME: handle swresample format conversions.
return;
}
if (passthrough) { // pass-through no conversion allowed
return;
}
audio_ctx = audio_decoder->AudioCtx;
#ifdef DEBUG
if (audio_ctx->sample_fmt == AV_SAMPLE_FMT_S16 && audio_ctx->sample_rate == audio_decoder->HwSampleRate
&& !CodecAudioDrift) {
// FIXME: use Resample only, when it is needed!
Debug4("codec/audio: no resample needed");
}
#endif
audio_decoder->Resample =
swr_alloc_set_opts(audio_decoder->Resample, audio_ctx->channel_layout, AV_SAMPLE_FMT_S16,
audio_decoder->HwSampleRate, audio_ctx->channel_layout, audio_ctx->sample_fmt, audio_ctx->sample_rate, 0,
NULL);
if (audio_decoder->Resample) {
swr_init(audio_decoder->Resample);
} else {
Error("codec/audio: can't setup resample");
}
}
/**
** Decode an audio packet.
**
** PTS must be handled self.
**
** @note the caller has not aligned avpkt and not cleared the end.
**
** @param audio_decoder audio decoder data
** @param avpkt audio packet
*/
void CodecAudioDecode(AudioDecoder * audio_decoder, const AVPacket * avpkt)
{
AVCodecContext *audio_ctx = audio_decoder->AudioCtx;
if (audio_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
int ret;
AVPacket pkt[1];
AVFrame *frame = audio_decoder->Frame;
av_frame_unref(frame);
*pkt = *avpkt; // use copy
ret = avcodec_send_packet(audio_ctx, pkt);
if (ret < 0) {
Debug4("codec: sending audio packet failed");
return;
}
ret = avcodec_receive_frame(audio_ctx, frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
Debug4("codec: receiving audio frame failed");
return;
}
if (ret >= 0) {
// update audio clock
if (avpkt->pts != (int64_t) AV_NOPTS_VALUE) {
CodecAudioSetClock(audio_decoder, avpkt->pts);
}
// format change
if (audio_decoder->Passthrough != CodecPassthrough || audio_decoder->SampleRate != audio_ctx->sample_rate
|| audio_decoder->Channels != audio_ctx->channels) {
CodecAudioUpdateFormat(audio_decoder);
}
if (!audio_decoder->HwSampleRate || !audio_decoder->HwChannels) {
return; // unsupported sample format
}
if (CodecAudioPassthroughHelper(audio_decoder, avpkt)) {
return;
}
if (audio_decoder->Resample) {
uint8_t outbuf[8192 * 2 * 8];
uint8_t *out[1];
out[0] = outbuf;
ret =
swr_convert(audio_decoder->Resample, out, sizeof(outbuf) / (2 * audio_decoder->HwChannels),
(const uint8_t **)frame->extended_data, frame->nb_samples);
if (ret > 0) {
if (!(audio_decoder->Passthrough & CodecPCM)) {
CodecReorderAudioFrame((int16_t *) outbuf, ret * 2 * audio_decoder->HwChannels,
audio_decoder->HwChannels);
}
AudioEnqueue(outbuf, ret * 2 * audio_decoder->HwChannels);
}
return;
}
}
}
}
/**
** Flush the audio decoder.
**
** @param decoder audio decoder data
*/
void CodecAudioFlushBuffers(AudioDecoder * decoder)
{
if (decoder->AudioCtx)
avcodec_flush_buffers(decoder->AudioCtx);
}
/**
** Get the audio decoder info.
**
** @param decoder audio decoder data
** @param codec_id audio codec id
*/
char *CodecAudioGetInfo(AudioDecoder * decoder, int codec_id)
{
if (decoder) {
char buffer[255];
if (snprintf(&buffer[0], sizeof(buffer), " Audio: %s %dHz %d channels", avcodec_get_name(codec_id),
decoder->SampleRate, decoder->Channels)) {
return strdup(buffer);
}
}
return NULL;
}
//----------------------------------------------------------------------------
// Codec
//----------------------------------------------------------------------------
/**
** FFMPEG log callback
*/
static void FFmpegLogCallback(void *ptr, int level, const char *fmt, va_list vargs)
{
if (level >= AV_LOG_VERBOSE)
Debug9(fmt, vargs);
else if (level >= AV_LOG_INFO)
Debug10(fmt, vargs);
else if (level >= AV_LOG_WARNING)
Debug11(fmt, vargs);
else if (level >= AV_LOG_ERROR)
Debug12(fmt, vargs);
}
/**
** Codec init
*/
void CodecInit(void)
{
pthread_mutex_init(&CodecLockMutex, NULL);
av_log_set_level(AV_LOG_VERBOSE);
av_log_set_callback(FFmpegLogCallback);
avcodec_register_all(); // register all formats and codecs
}
/**
** Codec exit.
*/
void CodecExit(void)
{
pthread_mutex_destroy(&CodecLockMutex);
}