-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathAudio.cpp
180 lines (137 loc) · 5.13 KB
/
Audio.cpp
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
#include "stdafx.h"
Audio* Audio::instance = 0;
Audio* Audio::get_instance()
{
if (instance == 0)
instance = new Audio();
return instance;
}
void Audio::open()
{
SDLWrapper::open_audio(&wantedSpec, &audioSpec);
wanted_frame.format = AV_SAMPLE_FMT_S16;
wanted_frame.sample_rate = audioSpec.freq;
wanted_frame.channel_layout = av_get_default_channel_layout(audioSpec.channels);
wanted_frame.channels = audioSpec.channels;
init_audio_packet(&audioq);
SDL_PauseAudio(0);
}
void Audio::init_audio_packet(AudioPacket* q)
{
q->last = NULL;
q->first = NULL;
q->mutex = SDL_CreateMutex();
q->cond = SDL_CreateCond();
}
void Audio::malloc(AVCodecContext* pCodecAudioCtx)
{
AudioCallback::set_audio_instance(this);
swrCtx = swr_alloc();
if (swrCtx == NULL)
Utils::display_exception("Failed to load audio");
av_opt_set_channel_layout(swrCtx, "in_channel_layout", pCodecAudioCtx->channel_layout, 0);
av_opt_set_channel_layout(swrCtx, "out_channel_layout", pCodecAudioCtx->channel_layout, 0);
av_opt_set_int(swrCtx, "in_sample_rate", pCodecAudioCtx->sample_rate, 0);
av_opt_set_int(swrCtx, "out_sample_rate", pCodecAudioCtx->sample_rate, 0);
av_opt_set_sample_fmt(swrCtx, "in_sample_fmt", pCodecAudioCtx->sample_fmt, 0);
av_opt_set_sample_fmt(swrCtx, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
int res = swr_init(swrCtx);
if (res != 0)
Utils::display_exception("Failed to initialize audio");
memset(&wantedSpec, 0, sizeof(wantedSpec));
wantedSpec.channels = pCodecAudioCtx->channels;
wantedSpec.freq = pCodecAudioCtx->sample_rate;
wantedSpec.format = AUDIO_S16SYS;
wantedSpec.silence = 0;
wantedSpec.samples = SDL_AUDIO_BUFFER_SIZE;
wantedSpec.userdata = pCodecAudioCtx;
wantedSpec.callback = AudioCallback::audio_callback;
}
int Audio::audio_decode_frame(AVCodecContext* aCodecCtx, uint8_t* audio_buf, int buf_size) {
static AVPacket pkt;
static uint8_t* audio_pkt_data = NULL;
static int audio_pkt_size = 0;
static AVFrame frame;
int len1;
int data_size = 0;
SwrContext* swr_ctx = NULL;
while (1)
{
while (audio_pkt_size > 0)
{
int got_frame = 0;
avcodec_send_packet(aCodecCtx, &pkt);
avcodec_receive_frame(aCodecCtx, &frame);
len1 = frame.pkt_size;
if (len1 < 0)
{
audio_pkt_size = 0;
break;
}
audio_pkt_data += len1;
audio_pkt_size -= len1;
data_size = 0;
if (got_frame)
{
int linesize = 1;
data_size = av_samples_get_buffer_size(&linesize, aCodecCtx->channels, frame.nb_samples, aCodecCtx->sample_fmt, 1);
assert(data_size <= buf_size);
memcpy(audio_buf, frame.data[0], data_size);
}
if (frame.channels > 0 && frame.channel_layout == 0)
frame.channel_layout = av_get_default_channel_layout(frame.channels);
else if (frame.channels == 0 && frame.channel_layout > 0)
frame.channels = av_get_channel_layout_nb_channels(frame.channel_layout);
if (swr_ctx)
{
swr_free(&swr_ctx);
swr_ctx = NULL;
}
swr_ctx = swr_alloc_set_opts(NULL, wanted_frame.channel_layout, (AVSampleFormat)wanted_frame.format, wanted_frame.sample_rate,
frame.channel_layout, (AVSampleFormat)frame.format, frame.sample_rate, 0, NULL);
if (!swr_ctx || swr_init(swr_ctx) < 0)
Utils::display_exception("swr_init failed");
int dst_nb_samples = (int)av_rescale_rnd(swr_get_delay(swr_ctx, frame.sample_rate) + frame.nb_samples,
wanted_frame.sample_rate, wanted_frame.format, AV_ROUND_INF);
int len2 = swr_convert(swr_ctx, &audio_buf, dst_nb_samples,
(const uint8_t**)frame.data, frame.nb_samples);
if (len2 < 0)
Utils::display_exception("swr_convert failed");
av_packet_unref(&pkt);
if (swr_ctx)
{
swr_free(&swr_ctx);
swr_ctx = NULL;
}
return wanted_frame.channels * len2 * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);
}
if (Player::get_instance()->getAudioPacket(&audioq, &pkt, 1) < 0)
return -1;
audio_pkt_data = pkt.data;
audio_pkt_size = pkt.size;
}
}
int Audio::put_audio_packet(AVPacket* packet)
{
AVPacketList* pktl;
AVPacket* newPkt;
newPkt = (AVPacket*)av_mallocz_array(1, sizeof(AVPacket));
if (av_packet_ref(newPkt, packet) < 0)
return -1;
pktl = (AVPacketList*)av_malloc(sizeof(AVPacketList));
if (!pktl)
return -1;
pktl->pkt = *newPkt;
pktl->next = NULL;
SDL_LockMutex(audioq.mutex);
if (!audioq.last)
audioq.first = pktl;
else
audioq.last->next = pktl;
audioq.last = pktl;
audioq.nb_packets++;
audioq.size += newPkt->size;
SDL_CondSignal(audioq.cond);
SDL_UnlockMutex(audioq.mutex);
return 0;
}