-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_ffmpeg.cpp
More file actions
281 lines (241 loc) · 8.74 KB
/
Copy pathimport_ffmpeg.cpp
File metadata and controls
281 lines (241 loc) · 8.74 KB
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
#include "import_ffmpeg.h"
#include <cstdio>
#include <cstdint>
#include <vector>
#include <cstring>
#include <windows.h>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libavutil/frame.h>
#include <libavutil/imgutils.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
}
static FILE* fopen_utf8(const char* path, const char* mode) {
wchar_t wpath[MAX_PATH], wmode[16];
if (MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH) == 0) return fopen(path, mode);
if (MultiByteToWideChar(CP_UTF8, 0, mode, -1, wmode, 16) == 0) return fopen(path, mode);
return _wfopen(wpath, wmode);
}
static bool write_wav(const char* path, const uint8_t* data, size_t data_size, int sample_rate, int channels, int bits_per_sample)
{
FILE* f = fopen_utf8(path, "wb");
if (!f) return false;
uint32_t fmt_size = 16;
uint16_t audio_format = 1;
uint16_t num_channels = (uint16_t)channels;
uint32_t byte_rate = sample_rate * channels * bits_per_sample / 8;
uint16_t block_align = (uint16_t)(channels * bits_per_sample / 8);
uint16_t bits = (uint16_t)bits_per_sample;
uint32_t file_size = 36 + (uint32_t)data_size;
fwrite("RIFF", 1, 4, f);
fwrite(&file_size, 4, 1, f);
fwrite("WAVE", 1, 4, f);
fwrite("fmt ", 1, 4, f);
fwrite(&fmt_size, 4, 1, f);
fwrite(&audio_format, 2, 1, f);
fwrite(&num_channels, 2, 1, f);
fwrite((const void*)&sample_rate, 4, 1, f);
fwrite(&byte_rate, 4, 1, f);
fwrite(&block_align, 2, 1, f);
fwrite(&bits, 2, 1, f);
fwrite("data", 1, 4, f);
uint32_t ds = (uint32_t)data_size;
fwrite(&ds, 4, 1, f);
fwrite(data, 1, data_size, f);
fclose(f);
return true;
}
static bool write_ppm(const char* path, int w, int h, const uint8_t* rgb)
{
FILE* f = fopen_utf8(path, "wb");
if (!f) return false;
fprintf(f, "P6\n%d %d\n255\n", w, h);
fwrite(rgb, 1, w * h * 3, f);
fclose(f);
return true;
}
bool ffmpeg_convert_audio(const std::string& src, const std::string& dst)
{
AVFormatContext* fmt_ctx = nullptr;
if (avformat_open_input(&fmt_ctx, src.c_str(), nullptr, nullptr) < 0)
return false;
if (avformat_find_stream_info(fmt_ctx, nullptr) < 0) {
avformat_close_input(&fmt_ctx);
return false;
}
int stream_idx = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
if (stream_idx < 0) {
avformat_close_input(&fmt_ctx);
return false;
}
AVStream* stream = fmt_ctx->streams[stream_idx];
const AVCodec* decoder = avcodec_find_decoder(stream->codecpar->codec_id);
if (!decoder) {
avformat_close_input(&fmt_ctx);
return false;
}
AVCodecContext* dec_ctx = avcodec_alloc_context3(decoder);
avcodec_parameters_to_context(dec_ctx, stream->codecpar);
if (avcodec_open2(dec_ctx, decoder, nullptr) < 0) {
avcodec_free_context(&dec_ctx);
avformat_close_input(&fmt_ctx);
return false;
}
AVChannelLayout in_layout = dec_ctx->ch_layout;
AVSampleFormat in_fmt = dec_ctx->sample_fmt;
int in_rate = dec_ctx->sample_rate;
AVChannelLayout out_layout;
av_channel_layout_default(&out_layout, 2);
int out_rate = 44100;
AVSampleFormat out_fmt = AV_SAMPLE_FMT_S16;
SwrContext* swr = swr_alloc();
if (!swr) {
avcodec_free_context(&dec_ctx);
avformat_close_input(&fmt_ctx);
return false;
}
av_opt_set_chlayout(swr, "in_chlayout", &in_layout, 0);
av_opt_set_int(swr, "in_sample_rate", in_rate, 0);
av_opt_set_sample_fmt(swr, "in_sample_fmt", in_fmt, 0);
av_opt_set_chlayout(swr, "out_chlayout", &out_layout, 0);
av_opt_set_int(swr, "out_sample_rate", out_rate, 0);
av_opt_set_sample_fmt(swr, "out_sample_fmt", out_fmt, 0);
if (swr_init(swr) < 0) {
swr_free(&swr);
avcodec_free_context(&dec_ctx);
avformat_close_input(&fmt_ctx);
return false;
}
std::vector<uint8_t> all_pcm;
AVPacket* pkt = av_packet_alloc();
AVFrame* frame = av_frame_alloc();
while (av_read_frame(fmt_ctx, pkt) >= 0) {
if (pkt->stream_index != stream_idx) {
av_packet_unref(pkt);
continue;
}
if (avcodec_send_packet(dec_ctx, pkt) < 0) {
av_packet_unref(pkt);
continue;
}
while (avcodec_receive_frame(dec_ctx, frame) >= 0) {
uint8_t* out_buf[8] = { nullptr };
int out_linesize[8] = { 0 };
int out_samples = swr_get_out_samples(swr, frame->nb_samples);
av_samples_alloc(out_buf, out_linesize, 2, out_samples, out_fmt, 0);
int converted = swr_convert(swr, out_buf, out_samples,
(const uint8_t**)frame->data, frame->nb_samples);
if (converted > 0) {
int bytes = converted * 2 * av_get_bytes_per_sample(out_fmt);
size_t old = all_pcm.size();
all_pcm.resize(old + bytes);
memcpy(all_pcm.data() + old, out_buf[0], bytes);
}
av_freep(&out_buf[0]);
av_frame_unref(frame);
}
av_packet_unref(pkt);
}
avcodec_send_packet(dec_ctx, nullptr);
while (avcodec_receive_frame(dec_ctx, frame) >= 0) {
uint8_t* out_buf[8] = { nullptr };
int out_linesize[8] = { 0 };
int out_samples = swr_get_out_samples(swr, frame->nb_samples);
av_samples_alloc(out_buf, out_linesize, 2, out_samples, out_fmt, 0);
int converted = swr_convert(swr, out_buf, out_samples,
(const uint8_t**)frame->data, frame->nb_samples);
if (converted > 0) {
int bytes = converted * 2 * av_get_bytes_per_sample(out_fmt);
size_t old = all_pcm.size();
all_pcm.resize(old + bytes);
memcpy(all_pcm.data() + old, out_buf[0], bytes);
}
av_freep(&out_buf[0]);
av_frame_unref(frame);
}
bool ok = write_wav(dst.c_str(), all_pcm.data(), all_pcm.size(), out_rate, 2, 16);
av_frame_free(&frame);
av_packet_free(&pkt);
swr_free(&swr);
avcodec_free_context(&dec_ctx);
avformat_close_input(&fmt_ctx);
return ok;
}
bool ffmpeg_extract_thumbnail(const std::string& src, const std::string& dst)
{
AVFormatContext* fmt_ctx = nullptr;
if (avformat_open_input(&fmt_ctx, src.c_str(), nullptr, nullptr) < 0)
return false;
if (avformat_find_stream_info(fmt_ctx, nullptr) < 0) {
avformat_close_input(&fmt_ctx);
return false;
}
int stream_idx = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
if (stream_idx < 0) {
avformat_close_input(&fmt_ctx);
return false;
}
AVStream* stream = fmt_ctx->streams[stream_idx];
const AVCodec* decoder = avcodec_find_decoder(stream->codecpar->codec_id);
if (!decoder) {
avformat_close_input(&fmt_ctx);
return false;
}
AVCodecContext* dec_ctx = avcodec_alloc_context3(decoder);
avcodec_parameters_to_context(dec_ctx, stream->codecpar);
if (avcodec_open2(dec_ctx, decoder, nullptr) < 0) {
avcodec_free_context(&dec_ctx);
avformat_close_input(&fmt_ctx);
return false;
}
AVPacket* pkt = av_packet_alloc();
AVFrame* frame = av_frame_alloc();
bool got_frame = false;
while (av_read_frame(fmt_ctx, pkt) >= 0) {
if (pkt->stream_index != stream_idx) {
av_packet_unref(pkt);
continue;
}
if (avcodec_send_packet(dec_ctx, pkt) < 0) {
av_packet_unref(pkt);
continue;
}
if (avcodec_receive_frame(dec_ctx, frame) >= 0) {
got_frame = true;
av_packet_unref(pkt);
break;
}
av_packet_unref(pkt);
}
if (!got_frame) {
avcodec_send_packet(dec_ctx, nullptr);
if (avcodec_receive_frame(dec_ctx, frame) >= 0)
got_frame = true;
}
bool success = false;
if (got_frame) {
int w = frame->width;
int h = frame->height;
SwsContext* sws = sws_getContext(w, h, dec_ctx->pix_fmt,
w, h, AV_PIX_FMT_RGB24,
SWS_BILINEAR, nullptr, nullptr, nullptr);
if (sws) {
std::vector<uint8_t> rgb(w * h * 3);
uint8_t* dest[4] = { rgb.data(), nullptr, nullptr, nullptr };
int dest_stride[4] = { w * 3, 0, 0, 0 };
sws_scale(sws, frame->data, frame->linesize, 0, h, dest, dest_stride);
success = write_ppm(dst.c_str(), w, h, rgb.data());
sws_freeContext(sws);
}
}
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&dec_ctx);
avformat_close_input(&fmt_ctx);
return success;
}