-
Notifications
You must be signed in to change notification settings - Fork 0
/
playback.c
230 lines (177 loc) · 6.29 KB
/
playback.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
#include "playback.h"
void playback_init (void)
{
//av_register_all();
//avcodec_register_all();
ao_initialize();
av_log_set_level(AV_LOG_QUIET);
logcmd(LOG_DMSG, "playback initialized");
}
audio *audio_create (void)
{
audio *a = malloc(sizeof(audio));
if (!a)
logcmd(LOG_ERROR_MALLOC, "playback: audio_create: unable to allocate memory for audio");
a->pb = NULL;
a->playstate = PLAYSTATE_STOP;
a->threadstate = THREADSTATE_FINISHED;
a->cycle = CYCLE_ALL_ARTIST;
return a;
}
void audio_free (audio **a)
{
if ((*a)->pb)
{
playback_free_file((*a)->pb);
free((*a)->pb);
}
free(*a);
*a = NULL;
}
playback *playback_open_file (const char *path)
{
playback *pb = malloc(sizeof(playback));
pb->first_try = 2;
pb->ctx = NULL;
if (avformat_open_input(&pb->ctx, path, NULL, NULL) < 0)
logcmd(LOG_ERROR, "playback: playback_open_file: coult not open file");
if (avformat_find_stream_info(pb->ctx, NULL) < 0)
logcmd(LOG_ERROR, "playback: playback_open_file: could not find stream info");
AVCodec *codec = avcodec_find_decoder(pb->ctx->streams[0]->codecpar->codec_id);
if (! codec)
logcmd(LOG_ERROR, "playback: playback_open_file: could not find codec");
pb->avctx = avcodec_alloc_context3(codec); if (! pb->avctx)
logcmd(LOG_ERROR, "playback: playback_open_file: could not allocate decoding context: %s", path);
if (avcodec_parameters_to_context(pb->avctx, pb->ctx->streams[0]->codecpar) < 0)
logcmd(LOG_ERROR, "playback: playback_open_file: ERROR: %s", path);
if (avcodec_open2(pb->avctx, NULL, NULL) < 0)
logcmd(LOG_ERROR, "playback: playback_open_file: could not open codec: %s", path);
switch (pb->avctx->sample_fmt)
{
case AV_SAMPLE_FMT_U8: case AV_SAMPLE_FMT_U8P:
pb->sformat.bits = 8;
break;
case AV_SAMPLE_FMT_S16: case AV_SAMPLE_FMT_S16P:
pb->sformat.bits = 16;
break;
case AV_SAMPLE_FMT_FLT: case AV_SAMPLE_FMT_FLTP:
pb->sformat.bits = 16;
break;
case AV_SAMPLE_FMT_DBL: case AV_SAMPLE_FMT_DBLP:
pb->sformat.bits = 16;
default:
logcmd(LOG_ERROR, "playback: playback_open_file: could not detect format: %s", path);
break;
}
pb->sformat.channels = pb->avctx->channels;
pb->sformat.rate = pb->avctx->sample_rate;
pb->sformat.byte_format = AO_FMT_NATIVE;
if (pb->sformat.channels > 1)
pb->sformat.matrix = "L,R";
else
pb->sformat.matrix = "C";
int driver = ao_default_driver_id();
pb->adevice = ao_open_live(driver, &pb->sformat, NULL);
pb->pkt = av_packet_alloc();
av_init_packet(pb->pkt);
pb->frame = av_frame_alloc();
return pb;
}
int playback_playback (playback *pb)
{
int plane_size;
if (av_read_frame(pb->ctx, pb->pkt) >= 0)
{
if (avcodec_send_packet(pb->avctx, pb->pkt) < 0 && pb->first_try)
{
pb->first_try--;
logcmd(LOG_DMSG, "error sending packet, trying again");
av_packet_unref(pb->pkt);
return 1;
}
else if (! pb->first_try)
{
logcmd(LOG_DMSG, "error sending packet");
return -1;
}
if (avcodec_receive_frame(pb->avctx, pb->frame) < 0)
return 1;
int data_size = av_samples_get_buffer_size(&plane_size, pb->avctx->channels, pb->frame->nb_samples, pb->avctx->sample_fmt, 1);
uint16_t *out = (uint16_t*) pb->samples;
int write = 0;
switch (pb->avctx->sample_fmt)
{
case AV_SAMPLE_FMT_S16:
ao_play(pb->adevice, (char*) pb->frame->extended_data[0], pb->frame->linesize[0]);
break;
case AV_SAMPLE_FMT_S16P:
for (int nb = 0; nb < plane_size / sizeof(uint16_t); nb++)
{
for (int ch = 0; ch < pb->avctx->channels; ch++)
{
out[write] = ((uint16_t*) pb->frame->extended_data[ch])[nb];
write++;
}
}
ao_play(pb->adevice, (char*) pb->samples, plane_size * pb->avctx->channels);
break;
case AV_SAMPLE_FMT_FLT:
ao_play(pb->adevice, (char*) pb->frame->extended_data[0], pb->frame->linesize[0]);
break;
case AV_SAMPLE_FMT_FLTP:
for (int nb = 0; nb < pb->frame->nb_samples; nb++)
{
for (int ch = 0; ch < pb->avctx->channels; ch++)
{
float *extended_data = (float*) pb->frame->extended_data[ch];
float sample = extended_data[nb];
if (sample < -1.0f)
sample = -1.0f;
else if (sample > 1.0f)
sample = 1.0f;
out[nb * pb->avctx->channels + ch] = (uint16_t) round(sample * SHRT_MAX);
}
}
ao_play(pb->adevice, (char*) pb->samples, (plane_size / sizeof(float)) * sizeof(uint16_t) * pb->avctx->channels);
break;
}
av_packet_unref(pb->pkt);
}
else
return 0;
return 1;
}
void playback_seek_timestamp (playback *pb, const int time)
{
while (av_read_frame(pb->ctx, pb->pkt) >= 0)
{
if (avcodec_send_packet(pb->avctx, pb->pkt) < 0 && pb->first_try)
{
pb->first_try--;
continue;
}
else if (!pb->first_try)
return;
if (avcodec_receive_frame(pb->avctx, pb->frame) < 0)
return;
int t = pb->frame->best_effort_timestamp / pb->ctx->streams[0]->time_base.den;
//int t = av_frame_get_best_effort_timestamp(pb->frame) / pb->ctx->streams[0]->time_base.den;
if (t >= time)
return;
}
}
void playback_free_file (playback *pb)
{
logcmd(LOG_DMSG, "freeing playback");
if (! pb)
return;
av_packet_free(&pb->pkt);
ao_close(pb->adevice);
av_frame_free(&pb->frame);
avcodec_free_context(&pb->avctx);
avformat_close_input(&pb->ctx);
}
void playback_shutdown (void)
{
ao_shutdown();
}