-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathffmpegDecode.cpp
More file actions
369 lines (300 loc) · 7.75 KB
/
Copy pathffmpegDecode.cpp
File metadata and controls
369 lines (300 loc) · 7.75 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
It is FFmpeg decoder class. Sample for article from unick-soft.ru
*/
#include "ffmpegDecode.h"
extern "C"
{
#include "avcodec.h"
}
#define min(a,b) (a > b ? b : a)
#define MAX_AUDIO_PACKET (2 * 1024 * 1024)
bool FFmpegDecoder::OpenFile (std::string& inputFile)
{
CloseFile();
// Register all components
av_register_all();
// Open media file.
if (avformat_open_input(&pFormatCtx, inputFile.c_str(), NULL, NULL) != 0)
{
CloseFile();
return false;
}
// Get format info.
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
{
CloseFile();
return false;
}
// open video and audio stream.
bool hasVideo = OpenVideo();
bool hasAudio = OpenAudio();
if (!hasVideo)
{
CloseFile();
return false;
}
isOpen = true;
// Get file information.
if (videoStreamIndex != -1)
{
videoFramePerSecond = av_q2d(pFormatCtx->streams[videoStreamIndex]->r_frame_rate);
// Need for convert time to ffmpeg time.
videoBaseTime = av_q2d(pFormatCtx->streams[videoStreamIndex]->time_base);
}
if (audioStreamIndex != -1)
{
audioBaseTime = av_q2d(pFormatCtx->streams[audioStreamIndex]->time_base);
}
return true;
}
bool FFmpegDecoder::CloseFile()
{
isOpen = false;
// Close video and audio.
CloseVideo();
CloseAudio();
if (pFormatCtx)
{
av_close_input_file(pFormatCtx);
pFormatCtx = NULL;
}
return true;
}
AVFrame * FFmpegDecoder::GetNextFrame()
{
AVFrame * res = NULL;
if (videoStreamIndex != -1)
{
AVFrame *pVideoYuv = avcodec_alloc_frame();
AVPacket packet;
if (isOpen)
{
// Read packet.
while (av_read_frame(pFormatCtx, &packet) >= 0)
{
int64_t pts = 0;
pts = (packet.dts != AV_NOPTS_VALUE) ? packet.dts : 0;
if(packet.stream_index == videoStreamIndex)
{
// Convert ffmpeg frame timestamp to real frame number.
int64_t numberFrame = (double)((int64_t)pts -
pFormatCtx->streams[videoStreamIndex]->start_time) *
videoBaseTime * videoFramePerSecond;
// Decode frame
bool isDecodeComplite = DecodeVideo(&packet, pVideoYuv);
if (isDecodeComplite)
{
res = GetRGBAFrame(pVideoYuv);
}
break;
}
else if (packet.stream_index == audioStreamIndex)
{
if (packet.dts != AV_NOPTS_VALUE)
{
int audioFrameSize = MAX_AUDIO_PACKET;
uint8_t * pFrameAudio = new uint8_t[audioFrameSize];
if (pFrameAudio)
{
double fCurrentTime = (double)(pts - pFormatCtx->streams[videoStreamIndex]->start_time)
* audioBaseTime;
double fCurrentDuration = (double)packet.duration * audioBaseTime;
// Decode audio
int nDecodedSize = DecodeAudio(audioStreamIndex, &packet,
pFrameAudio, audioFrameSize);
if (nDecodedSize > 0 && pAudioCodecCtx->sample_fmt == AV_SAMPLE_FMT_FLTP)
{
// Process audio here.
/* Uncommend sample if you want write raw data to file.
{
int size = nDecodedSize / sizeof(float);
signed short * ar = new signed short[nDecodedSize / sizeof(float)];
float* pointer = (float*)pFrameAudio;
// Convert float to S16.
for (int i = 0; i < size / 2; i ++)
{
ar[i] = pointer[i] * 32767.0f;
}
FILE* file = fopen("c:\\temp\\AudioRaw.raw", "ab");
fwrite(ar, 1, size * sizeof (signed short) / 2, file);
fclose(file);
}
*/
}
// Delete buffer.
delete[] pFrameAudio;
pFrameAudio = NULL;
}
}
}
av_free_packet(&packet);
packet = AVPacket();
}
av_free(pVideoYuv);
}
}
return res;
}
AVFrame * FFmpegDecoder::GetRGBAFrame(AVFrame *pFrameYuv)
{
AVFrame * frame = NULL;
int width = pVideoCodecCtx->width;
int height = pVideoCodecCtx->height;
int bufferImgSize = avpicture_get_size(PIX_FMT_BGR24, width, height);
frame = avcodec_alloc_frame();
uint8_t * buffer = (uint8_t*)av_mallocz(bufferImgSize);
if (frame)
{
avpicture_fill((AVPicture*)frame, buffer, PIX_FMT_BGR24, width, height);
frame->width = width;
frame->height = height;
//frame->data[0] = buffer;
sws_scale(pImgConvertCtx, pFrameYuv->data, pFrameYuv->linesize,
0, height, frame->data, frame->linesize);
}
return (AVFrame *)frame;
}
bool FFmpegDecoder::OpenVideo()
{
bool res = false;
if (pFormatCtx)
{
videoStreamIndex = -1;
for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoStreamIndex = i;
pVideoCodecCtx = pFormatCtx->streams[i]->codec;
pVideoCodec = avcodec_find_decoder(pVideoCodecCtx->codec_id);
if (pVideoCodec)
{
res = !(avcodec_open2(pVideoCodecCtx, pVideoCodec, NULL) < 0);
width = pVideoCodecCtx->coded_width;
height = pVideoCodecCtx->coded_height;
}
break;
}
}
if (!res)
{
CloseVideo();
}
else
{
pImgConvertCtx = sws_getContext(pVideoCodecCtx->width, pVideoCodecCtx->height,
pVideoCodecCtx->pix_fmt,
pVideoCodecCtx->width, pVideoCodecCtx->height,
PIX_FMT_BGR24,
SWS_BICUBIC, NULL, NULL, NULL);
}
}
return res;
}
bool FFmpegDecoder::DecodeVideo(const AVPacket *avpkt, AVFrame * pOutFrame)
{
bool res = false;
if (pVideoCodecCtx)
{
if (avpkt && pOutFrame)
{
int got_picture_ptr = 0;
int videoFrameBytes = avcodec_decode_video2(pVideoCodecCtx, pOutFrame, &got_picture_ptr, avpkt);
// avcodec_decode_video(pVideoCodecCtx, pOutFrame, &videoFrameBytes, pInBuffer, nInbufferSize);
res = (videoFrameBytes > 0);
}
}
return res;
}
bool FFmpegDecoder::OpenAudio()
{
bool res = false;
if (pFormatCtx)
{
audioStreamIndex = -1;
for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++)
{
if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
{
audioStreamIndex = i;
pAudioCodecCtx = pFormatCtx->streams[i]->codec;
pAudioCodec = avcodec_find_decoder(pAudioCodecCtx->codec_id);
if (pAudioCodec)
{
res = !(avcodec_open2(pAudioCodecCtx, pAudioCodec, NULL) < 0);
}
break;
}
}
if (! res)
{
CloseAudio();
}
}
return res;
}
void FFmpegDecoder::CloseVideo()
{
if (pVideoCodecCtx)
{
avcodec_close(pVideoCodecCtx);
pVideoCodecCtx = NULL;
pVideoCodec = NULL;
videoStreamIndex = 0;
}
}
void FFmpegDecoder::CloseAudio()
{
if (pAudioCodecCtx)
{
avcodec_close(pAudioCodecCtx);
pAudioCodecCtx = NULL;
pAudioCodec = NULL;
audioStreamIndex = 0;
}
}
int FFmpegDecoder::DecodeAudio(int nStreamIndex, const AVPacket *avpkt, uint8_t* pOutBuffer, size_t nOutBufferSize)
{
int decodedSize = 0;
int packetSize = avpkt->size;
uint8_t* pPacketData = (uint8_t*) avpkt->data;
while (packetSize > 0)
{
int sizeToDecode = nOutBufferSize;
uint8_t* pDest = pOutBuffer + decodedSize;
int got_picture_ptr = 0;
AVFrame* audioFrame = avcodec_alloc_frame();
int packetDecodedSize = avcodec_decode_audio4(pAudioCodecCtx, audioFrame, &got_picture_ptr, avpkt);
if (packetDecodedSize > 0)
{
sizeToDecode = av_samples_get_buffer_size(NULL, audioFrame->channels,
audioFrame->nb_samples,
(AVSampleFormat)audioFrame->format, 1);
// Currenlty we process only AV_SAMPLE_FMT_FLTP.
if ((AVSampleFormat)audioFrame->format == AV_SAMPLE_FMT_FLTP)
{
// Copy each channel plane.
for (int i = 0; i < audioFrame->channels; i++)
{
memcpy(pDest + i * sizeToDecode / audioFrame->channels,
audioFrame->extended_data[i], sizeToDecode / audioFrame->channels);
}
}
}
if (packetDecodedSize < 0)
{
decodedSize = 0;
break;
}
packetSize -= packetDecodedSize;
pPacketData += packetDecodedSize;
if (sizeToDecode <= 0)
{
continue;
}
decodedSize += sizeToDecode;
avcodec_free_frame(&audioFrame);
audioFrame = NULL;
}
return decodedSize;
}