forked from langhuihui/jessibuca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJessibuca.cpp
348 lines (341 loc) · 10.6 KB
/
Jessibuca.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
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
#ifdef EDITTIME
#undef __cplusplus
#define __cplusplus 201703L
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string>
// #include <functional>
// #include <map>
// #include <queue>
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/val.h>
// #include <time.h>
// #include <regex>
using namespace std;
using namespace emscripten;
// #include "slice.h"
typedef unsigned char u8;
// typedef signed char i8;
// typedef unsigned short u16;
// typedef signed short i16;
typedef unsigned int u32;
// typedef signed int i32;
#define PROP(name, type) \
type name; \
val get##name() const \
{ \
return val(name); \
} \
void set##name(val value) \
{ \
name = value.as<type>(); \
emscripten_log(0, #name " = %d", name); \
}
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
}
//const int SamplingFrequencies[] = {96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0};
//const int AudioObjectTypes[] = {};
class FFmpeg
{
public:
AVCodec *codec = nullptr;
AVCodecParserContext *parser = nullptr;
AVCodecContext *dec_ctx = nullptr;
AVFrame *frame;
AVPacket *pkt;
val jsObject;
bool initialized = false;
FFmpeg(val &&v) : jsObject(forward<val>(v))
{
}
void initCodec(enum AVCodecID id)
{
if (dec_ctx != nullptr)
{
clear();
}
pkt = av_packet_alloc();
codec = avcodec_find_decoder(id);
parser = av_parser_init(codec->id);
dec_ctx = avcodec_alloc_context3(codec);
frame = av_frame_alloc();
}
void initCodec(enum AVCodecID id, string input)
{
initCodec(id);
dec_ctx->extradata_size = input.length();
dec_ctx->extradata = (u8 *)input.data();
// // dec_ctx->extradata = (u8 *)malloc(dec_ctx->extradata_size);
// // memcpy(dec_ctx->extradata, input.c_str(), dec_ctx->extradata_size);
avcodec_open2(dec_ctx, codec, NULL);
initialized = true;
}
virtual ~FFmpeg()
{
clear();
}
virtual int decode(string input)
{
int ret = 0;
pkt->data = (u8 *)(input.data());
pkt->size = input.length();
ret = avcodec_send_packet(dec_ctx, pkt);
while (ret >= 0)
{
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return 0;
_decode();
}
return 0;
}
virtual void _decode(){};
virtual void clear()
{
if (parser)
{
av_parser_close(parser);
parser = nullptr;
}
if (dec_ctx)
{
avcodec_free_context(&dec_ctx);
}
if (frame)
{
av_frame_free(&frame);
}
if (pkt)
{
av_packet_free(&pkt);
}
codec = nullptr;
initialized = false;
}
};
class FFmpegAudioDecoder : public FFmpeg
{
SwrContext *au_convert_ctx = nullptr;
u8 *out_buffer[2];
int output_nb_samples;
int n_channel;
public:
PROP(sample_rate, int)
// struct SwrContext *au_convert_ctx = nullptr;
FFmpegAudioDecoder(val &&v) : FFmpeg(move(v))
{
// emscripten_log(0, "FFMpegAudioDecoder init");
}
~FFmpegAudioDecoder()
{
if (au_convert_ctx)
swr_free(&au_convert_ctx);
if (out_buffer[0])
free(out_buffer[0]);
// emscripten_log(0, "FFMpegAudioDecoder destory");
}
void clear() override
{
FFmpeg::clear();
}
int decode(string input) override
{
u8 flag = (u8)input[0];
u8 audioType = flag >> 4;
if (initialized)
{
return FFmpeg::decode(input.substr(audioType == 10 ? 2 : 1));
}
else
{
switch (audioType)
{
case 10:
if (!input[1])
{
initCodec(AV_CODEC_ID_AAC, input.substr(2));
n_channel = 2;
}
break;
case 7:
initCodec(AV_CODEC_ID_PCM_ALAW);
dec_ctx->channel_layout = AV_CH_LAYOUT_MONO;
dec_ctx->sample_rate = 8000;
dec_ctx->channels = 1;
avcodec_open2(dec_ctx, codec, NULL);
n_channel = 1;
initialized = true;
break;
case 8:
initCodec(AV_CODEC_ID_PCM_MULAW);
dec_ctx->channel_layout = AV_CH_LAYOUT_MONO;
dec_ctx->sample_rate = 8000;
dec_ctx->channels = 1;
avcodec_open2(dec_ctx, codec, NULL);
n_channel = 1;
initialized = true;
break;
default:
emscripten_log(0, "audio type not support:%d", audioType);
break;
}
if (initialized)
{
jsObject.call<void>("initAudioPlanar", n_channel, sample_rate);
}
}
return 0;
}
void _decode() override
{
auto nb_samples = frame->nb_samples;
auto bytes_per_sample = av_get_bytes_per_sample(AV_SAMPLE_FMT_FLTP);
if (dec_ctx->sample_fmt == AV_SAMPLE_FMT_FLTP && sample_rate == dec_ctx->sample_rate && dec_ctx->channel_layout == n_channel)
{
jsObject.call<void>("playAudioPlanar", int(frame->data), nb_samples *bytes_per_sample *n_channel);
return;
}
if (!au_convert_ctx)
{
// out_buffer = (uint8_t *)av_malloc(av_get_bytes_per_sample(dec_ctx->sample_fmt)*dec_ctx->channels*dec_ctx->frame_size);
emscripten_log(0, "au_convert_ctx channel_layout:%d,sample_fmt:%d", dec_ctx->channel_layout, dec_ctx->sample_fmt);
au_convert_ctx = swr_alloc_set_opts(NULL, n_channel == 2 ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO, AV_SAMPLE_FMT_FLTP, sample_rate,
dec_ctx->channel_layout, dec_ctx->sample_fmt, dec_ctx->sample_rate,
0, NULL);
auto ret = swr_init(au_convert_ctx);
auto out_buffer_size = av_samples_get_buffer_size(NULL, n_channel, nb_samples, AV_SAMPLE_FMT_FLTP, 0);
auto buffer = (uint8_t *)av_malloc(out_buffer_size);
out_buffer[0] = buffer;
out_buffer[1] = buffer + (out_buffer_size / 2);
emscripten_log(0, "au_convert_ctx init sample_rate:%d->%d,ret:%d", dec_ctx->sample_rate, sample_rate, ret);
}
// // 转换
auto ret = swr_convert(au_convert_ctx, out_buffer, nb_samples, (const uint8_t **)frame->data, nb_samples);
while (ret > 0)
{
jsObject.call<void>("playAudioPlanar", int(&out_buffer), ret);
ret = swr_convert(au_convert_ctx, out_buffer, nb_samples, (const uint8_t **)frame->data, 0);
}
}
};
class FFmpegVideoDecoder : public FFmpeg
{
public:
u32 videoWidth = 0;
u32 videoHeight = 0;
u32 y = 0;
u32 u = 0;
u32 v = 0;
int NAL_unit_length = 0;
u32 compositionTime = 0;
FFmpegVideoDecoder(val &&v) : FFmpeg(move(v))
{
// emscripten_log(0, "FFMpegVideoDecoder init");
}
~FFmpegVideoDecoder()
{
// emscripten_log(0, "FFMpegVideoDecoder destory");
}
void clear() override
{
videoWidth = 0;
videoHeight = 0;
FFmpeg::clear();
if (y)
{
free((void *)y);
y = 0;
}
}
int decode(string data) override
{
if (!initialized)
{
int codec_id = ((int)data[0]) & 0x0F;
if (((int)(data[0]) >> 4) == 1 && data[1] == 0)
{
// emscripten_log(0, "codec = %d", codec_id);
switch (codec_id)
{
case 7:
initCodec(AV_CODEC_ID_H264, data.substr(5));
break;
case 12:
initCodec(AV_CODEC_ID_H265, data.substr(5));
break;
default:
emscripten_log(0, "codec not support: %d", codec_id);
return -1;
}
}
}
else
{
compositionTime = (((u32)data[2]) << 16) + (((u32)data[3]) << 8) + (u32)data[4];
return FFmpeg::decode(data.substr(5));
}
return 0;
}
void _decode() override
{
if (videoWidth != frame->width || videoHeight != frame->height)
{
videoWidth = frame->width;
videoHeight = frame->height;
jsObject.call<void>("setVideoSize", videoWidth, videoHeight);
int size = videoWidth * videoHeight;
if (y)
free((void *)y);
y = (u32)malloc(size * 3 >> 1);
u = y + size;
v = u + (size >> 2);
}
u32 dst = y;
for (int i = 0; i < videoHeight; i++)
{
memcpy((u8 *)dst, (const u8 *)(frame->data[0] + i * frame->linesize[0]), videoWidth);
dst += videoWidth;
}
dst = u;
int halfh = videoHeight >> 1;
int halfw = videoWidth >> 1;
for (int i = 0; i < halfh; i++)
{
memcpy((u8 *)dst, (const u8 *)(frame->data[1] + i * frame->linesize[1]), halfw);
dst += halfw;
}
for (int i = 0; i < halfh; i++)
{
memcpy((u8 *)dst, (const u8 *)(frame->data[2] + i * frame->linesize[2]), halfw);
dst += halfw;
}
jsObject.call<void>("draw", compositionTime, y, u, v);
}
};
#define FUNC(name) function(#name, &FFmpegAudioDecoder::name)
#undef PROP
#define PROP(name) property(#name, &FFmpegAudioDecoder::get##name, &FFmpegAudioDecoder::set##name)
EMSCRIPTEN_BINDINGS(FFmpegAudioDecoder)
{
class_<FFmpegAudioDecoder>("AudioDecoder")
.constructor<val>()
.PROP(sample_rate)
.FUNC(clear)
.FUNC(decode);
}
#undef FUNC
#define FUNC(name) function(#name, &FFmpegVideoDecoder::name)
#undef PROP
#define PROP(name) property(#name, &FFmpegVideoDecoder::get##name, &FFmpegVideoDecoder::set##name)
EMSCRIPTEN_BINDINGS(FFmpegVideoDecoder)
{
class_<FFmpegVideoDecoder>("VideoDecoder")
.constructor<val>()
.FUNC(clear)
.FUNC(decode);
}