forked from langhuihui/jessibuca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffmpeg.h
183 lines (181 loc) · 6.46 KB
/
ffmpeg.h
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
#pragma once
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;
AVCodecParserContext *parser = nullptr;
AVCodecContext *dec_ctx = nullptr;
AVFrame *frame;
AVPacket *pkt;
FFmpeg() : pkt(av_packet_alloc()), frame(av_frame_alloc())
{
}
void initCodec(enum AVCodecID id)
{
if (dec_ctx != nullptr)
clear();
codec = avcodec_find_decoder(id);
parser = av_parser_init(codec->id);
dec_ctx = avcodec_alloc_context3(codec);
}
virtual ~FFmpeg()
{
av_frame_free(&frame);
av_packet_free(&pkt);
clear();
}
virtual void clear()
{
av_parser_close(parser);
avcodec_free_context(&dec_ctx);
}
};
class FFmpegAAC : public FFmpeg
{
public:
struct SwrContext *au_convert_ctx = nullptr;
FFmpegAAC()
{
emscripten_log(0, "FFMpegAAC init");
}
~FFmpegAAC()
{
swr_free(&au_convert_ctx);
emscripten_log(0, "FFMpegAAC destory");
}
int decode(IOBuffer &input)
{
if (input.readB<1, u8>())
{
int ret = 0;
pkt->data = (u8 *)(input);
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;
// emscripten_log(0, "aac channel_layout:%d", dec_ctx->channel_layout);
// if(au_convert_ctx==nullptr){
// // out_buffer = (uint8_t *)av_malloc(av_get_bytes_per_sample(dec_ctx->sample_fmt)*dec_ctx->channels*dec_ctx->frame_size);
// au_convert_ctx = swr_alloc();
// au_convert_ctx = swr_alloc_set_opts(au_convert_ctx,
// AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, dec_ctx->sample_rate,
// dec_ctx->channel_layout, dec_ctx->sample_fmt, dec_ctx->sample_rate,
// 0, NULL);
// swr_init(au_convert_ctx);
// }
// // // 转换
// swr_convert(au_convert_ctx, &output, frame->nb_samples, (const uint8_t **)frame->data , frame->nb_samples);
int bytes_per_sample = av_get_bytes_per_sample(dec_ctx->sample_fmt);
// int samplesBytes = frame->nb_samples<<1;
// int outputInterval = bytes_per_sample*dec_ctx->channels;
// for(int i =0;i<frame->nb_samples;i++){
// u8* start = &output[i*4];
// start[0] = out_buffer[0][i*2];
// start[1] = out_buffer[0][i*2+1];
// start[2] = out_buffer[1][i*2];
// start[3] = out_buffer[1][i*2+1];
// }
//memcpy(output,frame->data[0],samplesBytes);
//memcpy(output+samplesBytes,frame->data[1],samplesBytes);
// memcpy(output,frame->data[0],frame->nb_samples<<1);
return frame->nb_samples * bytes_per_sample * dec_ctx->channels;
}
}
else
{
// u8 config1 = input[0];
// u8 config2 = input[1];
initCodec(AV_CODEC_ID_AAC);
// dec_ctx->codec_type = AVMEDIA_TYPE_AUDIO;
// dec_ctx->channels = (config2 >> 3) & 0x0F;
// dec_ctx->sample_rate = SamplingFrequencies[((config1 & 0x7) << 1) | (config2 >> 7)];
// dec_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
// emscripten_log(0, "aac samplerate:%d channels:%d", dec_ctx->sample_rate, dec_ctx->channels);
dec_ctx->extradata_size = input.length;
dec_ctx->extradata = (u8 *)malloc(dec_ctx->extradata_size);
memcpy(dec_ctx->extradata, (const u8 *)input, dec_ctx->extradata_size);
auto ret = avcodec_open2(dec_ctx, codec, NULL);
}
return 0;
}
};
class FFmpegAVC : public FFmpeg, public VideoDecoder
{
public:
FFmpegAVC()
{
emscripten_log(0, "FFMpegAVC init");
}
~FFmpegAVC()
{
emscripten_log(0, "FFMpegAVC destory");
}
void clear() override
{
videoWidth = 0;
videoHeight = 0;
VideoDecoder::clear();
FFmpeg::clear();
}
void decodeH264Header(IOBuffer &data) override
{
initCodec(AV_CODEC_ID_H264);
dec_ctx->extradata_size = data.length;
dec_ctx->extradata = (u8 *)malloc(dec_ctx->extradata_size);
memcpy(dec_ctx->extradata, (const u8 *)data, dec_ctx->extradata_size);
auto ret = avcodec_open2(dec_ctx, codec, NULL);
//emscripten_log(0, "avcodec_open2:%d", ret);
}
void decodeBody(IOBuffer &data) override
{
// int NALUnitLength = 0;
// while (data.length > 4)
// {
// data.read4B(NALUnitLength);
// data<<=4;
// _decode(data(0, NALUnitLength+4));
// data >>= NALUnitLength+4;
// }
_decode(data);
}
void _decode(IOBuffer data) override
{
// emscripten_log(0, "len:%d", data.length);
// int ret = av_parser_parse2(parser, dec_ctx, &pkt->data, &pkt->size,
// (const u8 *)(data), data.length, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
// emscripten_log(0, "ffmpeg pkt->size:%d", pkt->size);
// if (ret >= 0 && pkt->size)
// {
int ret = 0;
pkt->data = (u8 *)(data);
pkt->size = data.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;
p_yuv[0] = (u32)frame->data[0];
p_yuv[1] = (u32)frame->data[1];
p_yuv[2] = (u32)frame->data[2];
if (videoWidth != frame->width || videoHeight != frame->height)
decodeVideoSize(frame->width, frame->height);
decodeYUV420();
}
// }
// else
// {
// emscripten_log(0, "ffmpeg decode ret:%d", ret);
// }
}
};