forked from ibsh/is_KeyFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoderlibav.cpp
executable file
·183 lines (158 loc) · 5.87 KB
/
decoderlibav.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
/*************************************************************************
Copyright 2011 Ibrahim Sha'ath
This file is part of KeyFinder.
KeyFinder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KeyFinder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with KeyFinder. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
#include "decoderlibav.h"
QMutex codecMutex; // I don't think this should be necessary if I get the lock manager right.
KeyFinder::AudioData* LibAvDecoder::decodeFile(const QString& filePath){
QMutexLocker codecMutexLocker(&codecMutex); // mutex the preparatory section of this method
AVCodec *codec = NULL;
AVFormatContext *fCtx = NULL;
AVCodecContext *cCtx = NULL;
AVDictionary* dict = NULL;
// convert filepath
#ifdef Q_OS_WIN
const wchar_t* filePathWc = reinterpret_cast<const wchar_t*>(filePath.constData());
const char* filePathCh = utf16_to_utf8(filePathWc);
#else
QByteArray encodedPath = QFile::encodeName(filePath);
const char* filePathCh = encodedPath;
#endif
// open file
int openInputResult = avformat_open_input(&fCtx, filePathCh, NULL, NULL);
if(openInputResult != 0){
std::ostringstream ss;
ss << "Could not open audio file (" << openInputResult << ")";
throw KeyFinder::Exception(ss.str());
}
if(av_find_stream_info(fCtx) < 0){
throw KeyFinder::Exception("Could not find stream information");
}
int audioStream = -1;
for(int i=0; i<(signed)fCtx->nb_streams; i++){
if(fCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO){
audioStream = i;
break;
}
}
if(audioStream == -1){
throw KeyFinder::Exception("Could not find an audio stream");
}
// Determine stream codec
cCtx = fCtx->streams[audioStream]->codec;
codec = avcodec_find_decoder(cCtx->codec_id);
if(codec == NULL){
throw KeyFinder::Exception("Audio stream has unsupported codec");
}
// Open codec
int codecOpenResult = avcodec_open2(cCtx, codec, &dict);
if(codecOpenResult < 0){
std::ostringstream ss;
ss << "Could not open audio codec: " << codec->long_name << " (" << codecOpenResult << ")";
throw KeyFinder::Exception(ss.str());
}
ReSampleContext* rsCtx = av_audio_resample_init(
cCtx->channels, cCtx->channels,
cCtx->sample_rate, cCtx->sample_rate,
AV_SAMPLE_FMT_S16, cCtx->sample_fmt,
0, 0, 0, 0);
if(rsCtx == NULL){
throw KeyFinder::Exception("Could not create ReSampleContext");
}
qDebug("Decoding %s (%s, %d)", filePathCh, av_get_sample_fmt_name(cCtx->sample_fmt), cCtx->sample_rate);
codecMutexLocker.unlock();
// Prep buffer
KeyFinder::AudioData *audio = new KeyFinder::AudioData();
audio->setFrameRate(cCtx->sample_rate);
audio->setChannels(cCtx->channels);
// Decode stream
AVPacket avpkt;
int badPacketCount = 0;
while(true){
av_init_packet(&avpkt);
if(av_read_frame(fCtx, &avpkt) < 0)
break;
if(avpkt.stream_index == audioStream){
try{
int result = decodePacket(cCtx, rsCtx, &avpkt, audio);
if(result != 0){
if(badPacketCount < 100){
badPacketCount++;
}else{
throw KeyFinder::Exception("100 bad packets");
}
}
}catch(KeyFinder::Exception& e){
throw e;
}
}
av_free_packet(&avpkt);
}
codecMutexLocker.relock();
audio_resample_close(rsCtx);
int codecCloseResult = avcodec_close(cCtx);
if(codecCloseResult < 0){
qCritical("Error closing audio codec: %s (%d)", codec->long_name, codecCloseResult);
}
codecMutexLocker.unlock();
av_close_input_file(fCtx);
return audio;
}
LibAvDecoder::LibAvDecoder(){
frameBufferSize = ((AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2) * sizeof(uint8_t);
frameBuffer = (uint8_t*)av_malloc(frameBufferSize);
frameBufferConverted = (uint8_t*)av_malloc(frameBufferSize);
}
LibAvDecoder::~LibAvDecoder(){
av_free(frameBuffer);
av_free(frameBufferConverted);
}
int LibAvDecoder::decodePacket(AVCodecContext* cCtx, ReSampleContext* rsCtx, AVPacket* originalPacket, KeyFinder::AudioData* audio){
// copy packet so we can shift data pointer about without endangering garbage collection
AVPacket tempPacket;
tempPacket.size = originalPacket->size;
tempPacket.data = originalPacket->data;
// loop in case audio packet contains multiple frames
while(tempPacket.size > 0){
int dataSize = frameBufferSize;
int16_t* dataBuffer = (int16_t*)frameBuffer;
int bytesConsumed = avcodec_decode_audio3(cCtx, dataBuffer, &dataSize, &tempPacket);
if(bytesConsumed < 0){ // error
tempPacket.size = 0;
return 1;
}
tempPacket.data += bytesConsumed;
tempPacket.size -= bytesConsumed;
if(dataSize <= 0)
continue; // nothing decoded
int newSamplesDecoded = dataSize / av_get_bytes_per_sample(cCtx->sample_fmt);
// Resample if necessary
if(cCtx->sample_fmt != AV_SAMPLE_FMT_S16){
int resampleResult = audio_resample(rsCtx, (short*)frameBufferConverted, (short*)frameBuffer, newSamplesDecoded);
if(resampleResult < 0){
throw KeyFinder::Exception("Could not resample");
}
dataBuffer = (int16_t*)frameBufferConverted;
}
int oldSampleCount = audio->getSampleCount();
try{
audio->addToSampleCount(newSamplesDecoded);
}catch(KeyFinder::Exception& e){
throw e;
}
for(int i = 0; i < newSamplesDecoded; i++){
audio->setSample(oldSampleCount+i, (float)dataBuffer[i]);
}
}
return 0;
}