-
Notifications
You must be signed in to change notification settings - Fork 500
/
OMXAudioCodecOMX.cpp
433 lines (379 loc) · 14.4 KB
/
OMXAudioCodecOMX.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (C) 2005-2008 Team XBMC
* http://www.xbmc.org
*
* This Program 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 2, or (at your option)
* any later version.
*
* This Program 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 XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "OMXAudioCodecOMX.h"
#ifdef _LINUX
#include "XMemUtils.h"
#endif
#include "utils/log.h"
#define MAX_AUDIO_FRAME_SIZE (AVCODEC_MAX_AUDIO_FRAME_SIZE*2)
#define AV_SAMPLE_FMT_DESIRED AV_SAMPLE_FMT_FLTP
COMXAudioCodecOMX::COMXAudioCodecOMX()
{
m_iBufferSize2 = 0;
m_pBuffer2 = (BYTE*)_aligned_malloc(MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE, 16);
memset(m_pBuffer2, 0, MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
m_iBufferUpmixSize = 0;
m_pBufferUpmix = (BYTE*)_aligned_malloc(MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE, 16);
memset(m_pBufferUpmix, 0, MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
m_iBuffered = 0;
m_pCodecContext = NULL;
m_pConvert = NULL;
m_bOpenedCodec = false;
m_channelMap[0] = PCM_INVALID;
m_channels = 0;
m_layout = 0;
m_pFrame1 = NULL;
m_iSampleFormat = AV_SAMPLE_FMT_NONE;
m_iBufferSize1 = 0;
}
COMXAudioCodecOMX::~COMXAudioCodecOMX()
{
_aligned_free(m_pBuffer2);
_aligned_free(m_pBufferUpmix);
Dispose();
}
bool COMXAudioCodecOMX::Open(COMXStreamInfo &hints)
{
AVCodec* pCodec;
m_bOpenedCodec = false;
if (!m_dllAvUtil.Load() || !m_dllAvCodec.Load() || !m_dllSwResample.Load())
return false;
m_dllAvCodec.avcodec_register_all();
pCodec = m_dllAvCodec.avcodec_find_decoder(hints.codec);
if (!pCodec)
{
CLog::Log(LOGDEBUG,"COMXAudioCodecOMX::Open() Unable to find codec %d", hints.codec);
return false;
}
m_bFirstFrame = true;
m_pCodecContext = m_dllAvCodec.avcodec_alloc_context3(pCodec);
m_pCodecContext->debug_mv = 0;
m_pCodecContext->debug = 0;
m_pCodecContext->workaround_bugs = 1;
m_pCodecContext->request_sample_fmt = AV_SAMPLE_FMT_DESIRED;
if (pCodec->capabilities & CODEC_CAP_TRUNCATED)
m_pCodecContext->flags |= CODEC_FLAG_TRUNCATED;
m_channels = 0;
m_pCodecContext->channels = hints.channels;
m_pCodecContext->sample_rate = hints.samplerate;
m_pCodecContext->block_align = hints.blockalign;
m_pCodecContext->bit_rate = hints.bitrate;
m_pCodecContext->bits_per_coded_sample = hints.bitspersample;
if(m_pCodecContext->bits_per_coded_sample == 0)
m_pCodecContext->bits_per_coded_sample = 16;
if( hints.extradata && hints.extrasize > 0 )
{
m_pCodecContext->extradata_size = hints.extrasize;
m_pCodecContext->extradata = (uint8_t*)m_dllAvUtil.av_mallocz(hints.extrasize + FF_INPUT_BUFFER_PADDING_SIZE);
memcpy(m_pCodecContext->extradata, hints.extradata, hints.extrasize);
}
if (m_dllAvCodec.avcodec_open2(m_pCodecContext, pCodec, NULL) < 0)
{
CLog::Log(LOGDEBUG,"COMXAudioCodecOMX::Open() Unable to open codec");
Dispose();
return false;
}
m_pFrame1 = m_dllAvCodec.avcodec_alloc_frame();
m_bOpenedCodec = true;
m_iSampleFormat = AV_SAMPLE_FMT_NONE;
return true;
}
void COMXAudioCodecOMX::Dispose()
{
if (m_pFrame1) m_dllAvUtil.av_free(m_pFrame1);
m_pFrame1 = NULL;
if (m_pConvert)
m_dllSwResample.swr_free(&m_pConvert);
if (m_pCodecContext)
{
if (m_pCodecContext->extradata) m_dllAvUtil.av_free(m_pCodecContext->extradata);
m_pCodecContext->extradata = NULL;
if (m_bOpenedCodec) m_dllAvCodec.avcodec_close(m_pCodecContext);
m_bOpenedCodec = false;
m_dllAvUtil.av_free(m_pCodecContext);
m_pCodecContext = NULL;
}
m_dllAvCodec.Unload();
m_dllAvUtil.Unload();
m_dllSwResample.Unload();
m_iBufferSize1 = 0;
m_iBufferSize2 = 0;
m_iBuffered = 0;
}
int COMXAudioCodecOMX::Decode(BYTE* pData, int iSize)
{
int iBytesUsed, got_frame;
if (!m_pCodecContext) return -1;
m_iBufferSize2 = 0;
AVPacket avpkt;
m_dllAvCodec.av_init_packet(&avpkt);
avpkt.data = pData;
avpkt.size = iSize;
iBytesUsed = m_dllAvCodec.avcodec_decode_audio4( m_pCodecContext
, m_pFrame1
, &got_frame
, &avpkt);
if (iBytesUsed < 0 || !got_frame)
{
m_iBufferSize1 = 0;
m_iBufferSize2 = 0;
return iBytesUsed;
}
int linesize1, linesize2;
m_iBufferSize1 = m_dllAvUtil.av_samples_get_buffer_size(&linesize1, m_pCodecContext->channels, m_pFrame1->nb_samples, m_pCodecContext->sample_fmt, 1);
m_iBufferSize2 = m_dllAvUtil.av_samples_get_buffer_size(&linesize2, m_pCodecContext->channels, m_pFrame1->nb_samples, AV_SAMPLE_FMT_DESIRED, 1);
/* some codecs will attempt to consume more data than what we gave */
if (iBytesUsed > iSize)
{
CLog::Log(LOGWARNING, "COMXAudioCodecOMX::Decode - decoder attempted to consume more data than given");
iBytesUsed = iSize;
}
if(m_iBufferSize1 == 0 && iBytesUsed >= 0)
m_iBuffered += iBytesUsed;
else
m_iBuffered = 0;
if (m_bFirstFrame)
{
CLog::Log(LOGDEBUG, "COMXAudioCodecOMX::Decode(%p,%d) format=%d(%d) chan=%d samples=%d size=%d/%d,%d/%d,%d data=%p,%p,%p,%p,%p,%p,%p,%p",
pData, iSize, m_pCodecContext->sample_fmt, AV_SAMPLE_FMT_DESIRED, m_pCodecContext->channels, m_pFrame1->nb_samples,
m_iBufferSize1, m_iBufferSize2, linesize1, linesize2, m_pFrame1->linesize[0],
m_pFrame1->data[0], m_pFrame1->data[1], m_pFrame1->data[2], m_pFrame1->data[3], m_pFrame1->data[4], m_pFrame1->data[5], m_pFrame1->data[6], m_pFrame1->data[7]
);
}
if(m_pCodecContext->sample_fmt != AV_SAMPLE_FMT_DESIRED && m_iBufferSize1 > 0)
{
if(m_pConvert && m_pCodecContext->sample_fmt != m_iSampleFormat)
m_dllSwResample.swr_free(&m_pConvert);
if(!m_pConvert)
{
m_iSampleFormat = m_pCodecContext->sample_fmt;
m_pConvert = m_dllSwResample.swr_alloc_set_opts(NULL,
m_dllAvUtil.av_get_default_channel_layout(m_pCodecContext->channels),
AV_SAMPLE_FMT_DESIRED, m_pCodecContext->sample_rate,
m_dllAvUtil.av_get_default_channel_layout(m_pCodecContext->channels),
m_pCodecContext->sample_fmt, m_pCodecContext->sample_rate,
0, NULL);
}
if(!m_pConvert || m_dllSwResample.swr_init(m_pConvert) < 0)
{
CLog::Log(LOGERROR, "COMXAudioCodecOMX::Decode - Unable to convert %d to %d", m_pCodecContext->sample_fmt, AV_SAMPLE_FMT_DESIRED);
m_iBufferSize1 = 0;
m_iBufferSize2 = 0;
return iBytesUsed;
}
m_iBufferSize1 = 0;
BYTE *out_planes[] = {
m_pBuffer2 + 0 * linesize2, m_pBuffer2 + 1 * linesize2, m_pBuffer2 + 2 * linesize2, m_pBuffer2 + 3 * linesize2,
m_pBuffer2 + 4 * linesize2, m_pBuffer2 + 5 * linesize2, m_pBuffer2 + 6 * linesize2, m_pBuffer2 + 7 * linesize2,
};
if(m_dllSwResample.swr_convert(m_pConvert, out_planes, m_pFrame1->nb_samples, (const uint8_t **)m_pFrame1->data, m_pFrame1->nb_samples) < 0)
{
CLog::Log(LOGERROR, "COMXAudioCodecOMX::Decode - Unable to convert %d to %d", (int)m_pCodecContext->sample_fmt, AV_SAMPLE_FMT_DESIRED);
m_iBufferSize1 = 0;
m_iBufferSize2 = 0;
return iBytesUsed;
}
}
else
{
m_iBufferSize2 = 0;
}
return iBytesUsed;
}
int COMXAudioCodecOMX::GetData(BYTE** dst)
{
int size = 0;
bool contiguous = true;
if(m_iBufferSize1)
{
int i;
BYTE *next = m_pFrame1->data[0];
for (i=0; i<m_pCodecContext->channels; i++)
{
if (next != m_pFrame1->data[i])
contiguous = false;
next += m_pFrame1->linesize[0];
size += m_pFrame1->linesize[0];
}
if (size != m_iBufferSize1)
contiguous = false;
if (contiguous)
{
*dst = m_pFrame1->data[0];
size = m_iBufferSize1;
}
else
{
int linesize;
m_iBufferUpmixSize = m_dllAvUtil.av_samples_get_buffer_size(&linesize, m_pCodecContext->channels, m_pFrame1->nb_samples, m_pCodecContext->sample_fmt, 1);
m_iBufferUpmixSize = 0;
for (i=0; i<m_pCodecContext->channels; i++)
{
if (m_iBufferUpmixSize + linesize <= MAX_AUDIO_FRAME_SIZE && m_pFrame1->data[i])
{
memcpy(m_pBufferUpmix + m_iBufferUpmixSize, m_pFrame1->data[i], linesize);
m_iBufferUpmixSize += linesize;
} else assert(0);
}
*dst = m_pBufferUpmix;
size = m_iBufferUpmixSize;
}
}
if(m_iBufferSize2)
{
*dst = m_pBuffer2;
size = m_iBufferSize2;
}
if (m_bFirstFrame)
{
CLog::Log(LOGDEBUG, "COMXAudioCodecOMX::GetData size=%d/%d/%d cont=%d buf=%p", m_iBufferSize1, m_iBufferSize2, size, contiguous, *dst);
m_bFirstFrame = false;
}
return size;
}
void COMXAudioCodecOMX::Reset()
{
if (m_pCodecContext) m_dllAvCodec.avcodec_flush_buffers(m_pCodecContext);
m_iBufferSize1 = 0;
m_iBufferSize2 = 0;
m_iBuffered = 0;
}
int COMXAudioCodecOMX::GetChannels()
{
return m_pCodecContext->channels;
}
int COMXAudioCodecOMX::GetSampleRate()
{
if (m_pCodecContext) return m_pCodecContext->sample_rate;
return 0;
}
int COMXAudioCodecOMX::GetBitsPerSample()
{
return AV_SAMPLE_FMT_DESIRED == AV_SAMPLE_FMT_S16 ? 16 : 32;
}
int COMXAudioCodecOMX::GetBitRate()
{
if (m_pCodecContext) return m_pCodecContext->bit_rate;
return 0;
}
static unsigned count_bits(int64_t value)
{
unsigned bits = 0;
for(;value;++bits)
value &= value - 1;
return bits;
}
void COMXAudioCodecOMX::BuildChannelMap()
{
if (m_channels == m_pCodecContext->channels && m_layout == m_pCodecContext->channel_layout)
return; //nothing to do here
m_channels = m_pCodecContext->channels;
m_layout = m_pCodecContext->channel_layout;
int index = 0;
if(m_pCodecContext->codec_id == CODEC_ID_AAC && m_pCodecContext->channels == 3)
{
m_channelMap[index++] = PCM_FRONT_CENTER;
m_channelMap[index++] = PCM_FRONT_LEFT;
m_channelMap[index++] = PCM_FRONT_RIGHT;
}
else if(m_pCodecContext->codec_id == CODEC_ID_AAC && m_pCodecContext->channels == 4)
{
m_channelMap[index++] = PCM_FRONT_CENTER;
m_channelMap[index++] = PCM_FRONT_LEFT;
m_channelMap[index++] = PCM_FRONT_RIGHT;
m_channelMap[index++] = PCM_BACK_CENTER;
}
else if(m_pCodecContext->codec_id == CODEC_ID_AAC && m_pCodecContext->channels == 5)
{
m_channelMap[index++] = PCM_FRONT_CENTER;
m_channelMap[index++] = PCM_FRONT_LEFT;
m_channelMap[index++] = PCM_FRONT_RIGHT;
m_channelMap[index++] = PCM_BACK_LEFT;
m_channelMap[index++] = PCM_BACK_RIGHT;
}
else if(m_pCodecContext->codec_id == CODEC_ID_AAC && m_pCodecContext->channels == 6)
{
m_channelMap[index++] = PCM_FRONT_CENTER;
m_channelMap[index++] = PCM_FRONT_LEFT;
m_channelMap[index++] = PCM_FRONT_RIGHT;
m_channelMap[index++] = PCM_BACK_LEFT;
m_channelMap[index++] = PCM_BACK_RIGHT;
m_channelMap[index++] = PCM_LOW_FREQUENCY;
}
else if(m_pCodecContext->codec_id == CODEC_ID_AAC && m_pCodecContext->channels == 7)
{
m_channelMap[index++] = PCM_FRONT_CENTER;
m_channelMap[index++] = PCM_FRONT_LEFT;
m_channelMap[index++] = PCM_FRONT_RIGHT;
m_channelMap[index++] = PCM_BACK_LEFT;
m_channelMap[index++] = PCM_BACK_RIGHT;
m_channelMap[index++] = PCM_BACK_CENTER;
m_channelMap[index++] = PCM_LOW_FREQUENCY;
}
else if(m_pCodecContext->codec_id == CODEC_ID_AAC && m_pCodecContext->channels == 8)
{
m_channelMap[index++] = PCM_FRONT_CENTER;
m_channelMap[index++] = PCM_SIDE_LEFT;
m_channelMap[index++] = PCM_SIDE_RIGHT;
m_channelMap[index++] = PCM_FRONT_LEFT;
m_channelMap[index++] = PCM_FRONT_RIGHT;
m_channelMap[index++] = PCM_BACK_LEFT;
m_channelMap[index++] = PCM_BACK_RIGHT;
m_channelMap[index++] = PCM_LOW_FREQUENCY;
}
else
{
int64_t layout;
int bits = count_bits(m_pCodecContext->channel_layout);
if (bits == m_pCodecContext->channels)
layout = m_pCodecContext->channel_layout;
else
{
CLog::Log(LOGINFO, "COMXAudioCodecOMX::GetChannelMap - FFmpeg reported %d channels, but the layout contains %d ignoring", m_pCodecContext->channels, bits);
layout = m_dllAvUtil.av_get_default_channel_layout(m_pCodecContext->channels);
}
if (layout & AV_CH_FRONT_LEFT ) m_channelMap[index++] = PCM_FRONT_LEFT ;
if (layout & AV_CH_FRONT_RIGHT ) m_channelMap[index++] = PCM_FRONT_RIGHT ;
if (layout & AV_CH_FRONT_CENTER ) m_channelMap[index++] = PCM_FRONT_CENTER ;
if (layout & AV_CH_LOW_FREQUENCY ) m_channelMap[index++] = PCM_LOW_FREQUENCY ;
if (layout & AV_CH_BACK_LEFT ) m_channelMap[index++] = PCM_BACK_LEFT ;
if (layout & AV_CH_BACK_RIGHT ) m_channelMap[index++] = PCM_BACK_RIGHT ;
if (layout & AV_CH_FRONT_LEFT_OF_CENTER ) m_channelMap[index++] = PCM_FRONT_LEFT_OF_CENTER ;
if (layout & AV_CH_FRONT_RIGHT_OF_CENTER) m_channelMap[index++] = PCM_FRONT_RIGHT_OF_CENTER;
if (layout & AV_CH_BACK_CENTER ) m_channelMap[index++] = PCM_BACK_CENTER ;
if (layout & AV_CH_SIDE_LEFT ) m_channelMap[index++] = PCM_SIDE_LEFT ;
if (layout & AV_CH_SIDE_RIGHT ) m_channelMap[index++] = PCM_SIDE_RIGHT ;
if (layout & AV_CH_TOP_CENTER ) m_channelMap[index++] = PCM_TOP_CENTER ;
if (layout & AV_CH_TOP_FRONT_LEFT ) m_channelMap[index++] = PCM_TOP_FRONT_LEFT ;
if (layout & AV_CH_TOP_FRONT_CENTER ) m_channelMap[index++] = PCM_TOP_FRONT_CENTER ;
if (layout & AV_CH_TOP_FRONT_RIGHT ) m_channelMap[index++] = PCM_TOP_FRONT_RIGHT ;
if (layout & AV_CH_TOP_BACK_LEFT ) m_channelMap[index++] = PCM_TOP_BACK_LEFT ;
if (layout & AV_CH_TOP_BACK_CENTER ) m_channelMap[index++] = PCM_TOP_BACK_CENTER ;
if (layout & AV_CH_TOP_BACK_RIGHT ) m_channelMap[index++] = PCM_TOP_BACK_RIGHT ;
}
}
enum PCMChannels* COMXAudioCodecOMX::GetChannelMap()
{
BuildChannelMap();
if (m_channelMap[0] == PCM_INVALID)
return NULL;
return m_channelMap;
}