-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathffmpeg_stream_info.cpp
More file actions
215 lines (191 loc) · 6.49 KB
/
Copy pathffmpeg_stream_info.cpp
File metadata and controls
215 lines (191 loc) · 6.49 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
#ifndef GDEXTENSION
#include "core/error/error_macros.h"
#include "core/object/class_db.h"
extern "C" {
#include "libavcodec/codec_id.h"
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libavutil/avutil.h"
};
#ifdef ENABLE_STREAM_INFO
#include "core/io/file_access.h"
#include "ffmpeg_stream_info.h"
#include "video_decoder.h"
int64_t FFmpegStreamInfo::_stream_seek_callback(void *p_opaque, int64_t p_offset, int p_whence) {
FFmpegStreamInfo *decoder = (FFmpegStreamInfo *)p_opaque;
switch (p_whence) {
case SEEK_CUR: {
decoder->current_file->seek(decoder->current_file->get_position() + p_offset);
} break;
case SEEK_SET: {
decoder->current_file->seek(p_offset);
} break;
case SEEK_END: {
decoder->current_file->seek_end(p_offset);
} break;
case AVSEEK_SIZE: {
return decoder->current_file->get_length();
} break;
default: {
return -1;
} break;
}
return decoder->current_file->get_position();
}
int FFmpegStreamInfo::_read_packet_callback(void *p_opaque, uint8_t *p_buf, int p_buf_size) {
FFmpegStreamInfo *decoder = (FFmpegStreamInfo *)p_opaque;
uint64_t read_bytes = decoder->current_file->get_buffer(p_buf, p_buf_size);
return read_bytes != 0 ? read_bytes : AVERROR_EOF;
}
void FFmpegStreamInfo::_bind_methods() {
ClassDB::bind_method(D_METHOD("load", "path"), &FFmpegStreamInfo::load);
ClassDB::bind_method(D_METHOD("get_video_stream_count"), &FFmpegStreamInfo::get_video_stream_count);
ClassDB::bind_method(D_METHOD("get_audio_stream_count"), &FFmpegStreamInfo::get_audio_stream_count);
ClassDB::bind_method(D_METHOD("get_video_stream_codec"), &FFmpegStreamInfo::get_video_stream_codec);
ClassDB::bind_method(D_METHOD("get_audio_stream_codec"), &FFmpegStreamInfo::get_audio_stream_codec);
ClassDB::bind_static_method("FFmpegStreamInfo", D_METHOD("video_codec_to_string"), &FFmpegStreamInfo::video_codec_to_string);
ClassDB::bind_static_method("FFmpegStreamInfo", D_METHOD("audio_codec_to_string"), &FFmpegStreamInfo::audio_codec_to_string);
BIND_ENUM_CONSTANT(VIDEO_CODEC_H264);
BIND_ENUM_CONSTANT(VIDEO_CODEC_H265);
BIND_ENUM_CONSTANT(VIDEO_CODEC_VP9);
BIND_ENUM_CONSTANT(VIDEO_CODEC_UNKNOWN);
BIND_ENUM_CONSTANT(AUDIO_CODEC_VORBIS);
BIND_ENUM_CONSTANT(AUDIO_CODEC_AAC);
BIND_ENUM_CONSTANT(AUDIO_CODEC_UNKNOWN);
}
int FFmpegStreamInfo::get_video_stream_count() const {
return video_streams.size();
}
FFmpegStreamInfo::VideoCodec FFmpegStreamInfo::get_video_stream_codec(int p_idx) const {
ERR_FAIL_UNSIGNED_INDEX_V(p_idx, video_streams.size(), VIDEO_CODEC_UNKNOWN);
return video_streams[p_idx].video_codec;
}
FFmpegStreamInfo::AudioCodec FFmpegStreamInfo::get_audio_stream_codec(int p_idx) const {
ERR_FAIL_UNSIGNED_INDEX_V(p_idx, audio_streams.size(), AUDIO_CODEC_UNKNOWN);
return audio_streams[p_idx].audio_codec;
}
int FFmpegStreamInfo::get_audio_stream_count() const {
return audio_streams.size();
}
FFmpegStreamInfo::VideoCodec _ffmpeg_to_video_codec(AVCodecID p_codec) {
FFmpegStreamInfo::VideoCodec video_codec = FFmpegStreamInfo::VIDEO_CODEC_UNKNOWN;
switch (p_codec) {
case AV_CODEC_ID_H264: {
video_codec = FFmpegStreamInfo::VIDEO_CODEC_H264;
} break;
case AV_CODEC_ID_H265: {
video_codec = FFmpegStreamInfo::VIDEO_CODEC_H265;
} break;
case AV_CODEC_ID_VP9: {
video_codec = FFmpegStreamInfo::VIDEO_CODEC_H265;
} break;
default: {
} break;
}
return video_codec;
}
FFmpegStreamInfo::AudioCodec _ffmpeg_to_audio_codec(AVCodecID p_codec) {
FFmpegStreamInfo::AudioCodec audio_codec = FFmpegStreamInfo::AUDIO_CODEC_UNKNOWN;
switch (p_codec) {
case AV_CODEC_ID_AAC: {
audio_codec = FFmpegStreamInfo::AUDIO_CODEC_AAC;
} break;
case AV_CODEC_ID_VORBIS: {
audio_codec = FFmpegStreamInfo::AUDIO_CODEC_VORBIS;
} break;
default: {
} break;
}
return audio_codec;
}
int FFmpegStreamInfo::load(String p_path) {
Error err;
current_file = FileAccess::open(p_path, FileAccess::READ, &err);
if (err != OK) {
return err;
}
avio_seek(io_context, 0, SEEK_SET);
if (!io_context) {
const int context_buffer_size = 4096;
unsigned char *context_buffer = (unsigned char *)av_malloc(context_buffer_size);
io_context = avio_alloc_context(context_buffer, context_buffer_size, 0, this, &FFmpegStreamInfo::_read_packet_callback, nullptr, &FFmpegStreamInfo::_stream_seek_callback);
}
format_context = avformat_alloc_context();
format_context->pb = io_context;
format_context->flags |= AVFMT_FLAG_GENPTS;
int open_input_res = avformat_open_input(&format_context, "dummy", nullptr, nullptr);
bool input_opened = open_input_res >= 0;
if (!input_opened) {
ERR_PRINT(vformat("Error opening file or stream: %s", ffmpeg_get_error_message(open_input_res)));
avformat_close_input(&format_context);
av_free(io_context->buffer);
avio_context_free(&io_context);
avformat_free_context(format_context);
return FAILED;
}
int find_stream_info_result = avformat_find_stream_info(format_context, nullptr);
if (find_stream_info_result < 0) {
ERR_PRINT(vformat("Error finding stream info: %s", ffmpeg_get_error_message(find_stream_info_result)));
avformat_close_input(&format_context);
av_free(io_context->buffer);
avio_context_free(&io_context);
avformat_free_context(format_context);
return FAILED;
}
for (int i = 0; i < format_context->nb_streams; i++) {
AVStream *stream = format_context->streams[i];
if (stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
video_streams.push_back({
.video_codec = _ffmpeg_to_video_codec(stream->codecpar->codec_id),
});
} else if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_streams.push_back({
.audio_codec = _ffmpeg_to_audio_codec(stream->codecpar->codec_id),
});
}
}
avformat_close_input(&format_context);
av_free(io_context->buffer);
avio_context_free(&io_context);
avformat_free_context(format_context);
return OK;
}
String FFmpegStreamInfo::video_codec_to_string(VideoCodec p_codec) {
String codec = "Invalid";
switch (p_codec) {
case VIDEO_CODEC_H264: {
codec = "H264";
} break;
case VIDEO_CODEC_H265: {
codec = "H265";
} break;
case VIDEO_CODEC_VP9: {
codec = "VP9";
} break;
case VIDEO_CODEC_UNKNOWN: {
codec = "Unknown";
} break;
default:
break;
}
return codec;
}
String FFmpegStreamInfo::audio_codec_to_string(AudioCodec p_codec) {
String codec = "Invalid";
switch (p_codec) {
case AUDIO_CODEC_VORBIS: {
codec = "Vorbis";
} break;
case AUDIO_CODEC_AAC: {
codec = "AAC";
} break;
case AUDIO_CODEC_UNKNOWN: {
codec = "Unknown";
} break;
default:
break;
}
return codec;
}
#endif // ENABLE_STREAM_INFO
#endif