| 1 |
/* |
| 2 |
* audio stream functions |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "ReadStreamA.h" |
| 6 |
#include "avifmt.h" |
| 7 |
#include "avm_creators.h" |
| 8 |
#include "audiodecoder.h" |
| 9 |
#include "avm_cpuinfo.h" |
| 10 |
#include "utils.h" |
| 11 |
#include "avm_output.h" |
| 12 |
#include <string.h> // memcpy |
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdio.h> |
| 15 |
|
| 16 |
#include <sys/types.h> |
| 17 |
#include <sys/stat.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#include <unistd.h> |
| 20 |
|
| 21 |
AVM_BEGIN_NAMESPACE; |
| 22 |
|
| 23 |
static const short mpeg_bitrates[6][16] = |
| 24 |
{ |
| 25 |
/* -== MPEG-1 ==- */ |
| 26 |
/* Layer I */ |
| 27 |
{ 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448 }, |
| 28 |
/* Layer II */ |
| 29 |
{ 0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }, |
| 30 |
/* Layer III */ |
| 31 |
{ 0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 }, |
| 32 |
|
| 33 |
/* -== MPEG-2 LSF ==- */ |
| 34 |
/* Layer I */ |
| 35 |
{ 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256 }, |
| 36 |
/* Layers II & III */ |
| 37 |
{ 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 }, |
| 38 |
{ 0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 }, |
| 39 |
}; |
| 40 |
|
| 41 |
static const int mpeg_sample_rates[4][3] = |
| 42 |
{ |
| 43 |
{ 44100, 48000, 32000 }, // MPEG1 |
| 44 |
{ 22050, 24000, 16000 }, // MPEG2 |
| 45 |
{ 0, 0, 0 }, // ERR |
| 46 |
{ 11025, 12000, 8000 } // MPEG2.5 |
| 47 |
}; |
| 48 |
|
| 49 |
/* |
| 50 |
bits name comments |
| 51 |
-------------------------------------------------- |
| 52 |
12 sync 0xFFF (12 bit is 0 for mpeg2.5 !) |
| 53 |
1 version 1=mpeg1.0, 0=mpeg2.0 |
| 54 |
2 lay 4-lay = layerI, II or III |
| 55 |
1 error protection 0=yes, 1=no |
| 56 |
4 bitrate_index see table below |
| 57 |
2 sampling_freq see table below |
| 58 |
1 padding |
| 59 |
1 extension see table below |
| 60 |
2 mode see table below |
| 61 |
2 mode_ext used with "joint stereo" mode |
| 62 |
1 copyright 0=no 1=yes |
| 63 |
1 original 0=no 1=yes |
| 64 |
2 emphasis see table below |
| 65 |
-------------------------------------------------- |
| 66 |
*/ |
| 67 |
|
| 68 |
#define MPEG_ID_MASK 0x00180000 |
| 69 |
#define MPEG_MPEG1 0x00180000 |
| 70 |
#define MPEG_MPEG2 0x00100000 |
| 71 |
#define MPEG_MPEG2_5 0x00000000 |
| 72 |
|
| 73 |
#define MPEG_LAYER_MASK 0x00060000 |
| 74 |
#define MPEG_LAYER_III 0x00020000 |
| 75 |
#define MPEG_LAYER_II 0x00040000 |
| 76 |
#define MPEG_LAYER_I 0x00060000 |
| 77 |
#define MPEG_PROTECTION 0x00010000 |
| 78 |
#define MPEG_BITRATE_MASK 0x0000F000 |
| 79 |
#define MPEG_FREQUENCY_MASK 0x00000C00 |
| 80 |
#define MPEG_PAD_MASK 0x00000200 |
| 81 |
#define MPEG_PRIVATE_MASK 0x00000100 |
| 82 |
#define MPEG_MODE_MASK 0x000000C0 |
| 83 |
#define MPEG_MODE_EXT_MASK 0x00000030 |
| 84 |
#define MPEG_COPYRIGHT_MASK 0x00000008 |
| 85 |
#define MPEG_HOME_MASK 0x00000004 |
| 86 |
#define MPEG_EMPHASIS_MASK 0x00000003 |
| 87 |
|
| 88 |
#define LAYER_I_SAMPLES 384 |
| 89 |
#define LAYER_II_III_SAMPLES 1152 |
| 90 |
|
| 91 |
|
| 92 |
Mp3AudioInfo::Mp3AudioInfo() : frame_size(0) {} |
| 93 |
|
| 94 |
// MP3 header needs 40 bytes |
| 95 |
int Mp3AudioInfo::Init(const void* buf, int fast) |
| 96 |
{ |
| 97 |
header = avm_get_be32(buf); |
| 98 |
//AVM_WRITE("audio reader", "HDR %x %.4s\n", header, (char*)&header); |
| 99 |
// CHECK if it's mpeg & Get the sampling frequency |
| 100 |
|
| 101 |
int frequency_index; |
| 102 |
if ((header & 0xffe00000) != 0xffe00000 |
| 103 |
|| ((layer = 4 - ((header & MPEG_LAYER_MASK) >> 17)) > 3) |
| 104 |
|| ((mode = (MPEG_MODE) (3 - ((header >> 19) & 3))) == Mp3AudioInfo::ERR) // MPEG_ID_MASK |
| 105 |
|| ((frequency_index = (header & MPEG_FREQUENCY_MASK)) == MPEG_FREQUENCY_MASK)) |
| 106 |
return 0; // not mp3 header |
| 107 |
|
| 108 |
sample_rate = mpeg_sample_rates[mode][frequency_index >> 10]; |
| 109 |
//AVM_WRITE("audio reader", "HDR %x m:%d fi:%d sr:%d\n", header, mode, frequency_index, sample_rate); |
| 110 |
// Get stereo mode index |
| 111 |
stereo_mode = (STEREO_MODE)((header & MPEG_MODE_MASK) >> 6); |
| 112 |
num_channels = (stereo_mode == MODE_MONO) ? 1 : 2; |
| 113 |
// Get layer |
| 114 |
samples_per_frame = LAYER_I_SAMPLES; |
| 115 |
if (layer > 1) |
| 116 |
samples_per_frame = LAYER_II_III_SAMPLES; |
| 117 |
|
| 118 |
start_offset = 40; |
| 119 |
if (fast) |
| 120 |
return start_offset; |
| 121 |
|
| 122 |
if (mode == MPEG2) |
| 123 |
samples_per_frame /= 2; |
| 124 |
else if (mode == MPEG2_5) |
| 125 |
samples_per_frame /= 4; |
| 126 |
|
| 127 |
// Check for VBR stream (Xing header) |
| 128 |
int b = 4; |
| 129 |
if (header & MPEG_ID_MASK) |
| 130 |
b += (stereo_mode != MODE_MONO) ? 32 : 17; // mpeg1 mpeg2 |
| 131 |
else |
| 132 |
b += (stereo_mode != MODE_MONO) ? 17 : 9; // mpeg2_5 |
| 133 |
xing = (avm_get_le32((uint8_t*)buf + b) == mmioFOURCC('X', 'i', 'n', 'g')); |
| 134 |
bitrate = (xing) ? 0 : GetBitrate(); |
| 135 |
|
| 136 |
//printf ("%d %d l:%d %d ch:%d\n", sample_rate, bitrate, layer, samples_per_frame, num_channels); |
| 137 |
frame_size = GetFrameSize(); |
| 138 |
if (xing) |
| 139 |
start_offset += frame_size + 4; |
| 140 |
|
| 141 |
return start_offset; |
| 142 |
} |
| 143 |
int Mp3AudioInfo::GetBitrate() const |
| 144 |
{ |
| 145 |
int bitrate_idx = (header & MPEG_BITRATE_MASK) >> 12; |
| 146 |
int layer_idx = layer + ((layer > 0 && mode == MPEG1) ? -1 : 2); |
| 147 |
return mpeg_bitrates[layer_idx][bitrate_idx] * 1000; |
| 148 |
} |
| 149 |
int Mp3AudioInfo::GetFrameSize() const |
| 150 |
{ |
| 151 |
int tmp_bitrate = GetBitrate(); |
| 152 |
|
| 153 |
if (!tmp_bitrate) |
| 154 |
return 0; |
| 155 |
|
| 156 |
int ret = (layer == 1) ? 48 : 144; |
| 157 |
|
| 158 |
if (mode == MPEG2 || mode == MPEG2_5) |
| 159 |
ret /= 2; |
| 160 |
//ret /= 4; |
| 161 |
//AVM_WRITE("audio reader", "%d %d %d \n", ret, tmp_bitrate, sample_rate); |
| 162 |
ret = ret * tmp_bitrate / sample_rate; |
| 163 |
if (header & MPEG_PAD_MASK) |
| 164 |
ret++; |
| 165 |
|
| 166 |
return ret; |
| 167 |
} |
| 168 |
void Mp3AudioInfo::PrintHeader() const |
| 169 |
{ |
| 170 |
static const char sf[][8] = { "MPEG1", "MPEG2", "ERR", "MPEG2.5" }; |
| 171 |
static const char sm[][13] = { "Stereo", "JointStereo", "DualChannel", "Mono" }; |
| 172 |
if (frame_size) |
| 173 |
AVM_WRITE("audio reader", "%s Layer-%d %dHz %dkbps %s %s" |
| 174 |
"(%d,%d,%d)\n", |
| 175 |
sf[mode], layer, sample_rate, bitrate / 1000, |
| 176 |
sm[stereo_mode], xing ? "Xing " : "", |
| 177 |
samples_per_frame, start_offset, frame_size); |
| 178 |
} |
| 179 |
|
| 180 |
ReadStreamA::ReadStreamA(IMediaReadStream* stream) |
| 181 |
: ReadStream(stream), m_pAudiodecoder(0), m_uiMinSize(0), m_uiSrcSize(0), m_uiRead(0), m_bIsMp3(false) |
| 182 |
{ |
| 183 |
// FIXME - asf reading needs to know also about VideoStreams |
| 184 |
// currently this isn't possible to use it this way |
| 185 |
// Flush(); |
| 186 |
|
| 187 |
// fix incorrect header |
| 188 |
if (m_WF->wFormatTag == 0x55 |
| 189 |
&& (m_WF->cbSize != 12 |
| 190 |
|| m_uiFormatSize < sizeof(MPEGLAYER3WAVEFORMAT))) |
| 191 |
{ |
| 192 |
if (m_uiFormatSize < sizeof(MPEGLAYER3WAVEFORMAT)) |
| 193 |
AVM_WRITE("audio reader", "WARNING: WAVEFORMAT header missing MP3 header extension for MP3 audio track (sz:%lu != %" PRIsz ")\n", |
| 194 |
(unsigned long)m_uiFormatSize, sizeof(MPEGLAYER3WAVEFORMAT)); |
| 195 |
else |
| 196 |
AVM_WRITE("audio reader", "WARNING: WAVEFORMAT header missing MP3 header extension for MP3 audio track (cb:%lu != 12)\n", |
| 197 |
(unsigned long)m_WF->cbSize); |
| 198 |
m_uiFormatSize = sizeof(MPEGLAYER3WAVEFORMAT); |
| 199 |
char* n = new char[m_uiFormatSize]; |
| 200 |
if (n) |
| 201 |
{ |
| 202 |
memcpy(n, m_pFormat, sizeof(WAVEFORMATEX)); |
| 203 |
delete[] m_pFormat; |
| 204 |
m_pFormat = n; |
| 205 |
} |
| 206 |
m_WF->cbSize = sizeof(MPEGLAYER3WAVEFORMAT) - sizeof(WAVEFORMATEX);//12 |
| 207 |
// everything past WAVEFORMATEX must be kept in stream native format |
| 208 |
avm_set_le16(&m_MP3WF->wID, MPEGLAYER3_ID_MPEG); // These values based on an old |
| 209 |
avm_set_le32(&m_MP3WF->fdwFlags, MPEGLAYER3_FLAG_PADDING_OFF); // Usenet post!! |
| 210 |
avm_set_le16(&m_MP3WF->nBlockSize, 0); |
| 211 |
avm_set_le16(&m_MP3WF->nFramesPerBlock, 1); |
| 212 |
avm_set_le16(&m_MP3WF->nCodecDelay, 1393); // 0x571 for F-IIS MP3 Codec |
| 213 |
} |
| 214 |
m_uiBps = m_WF->nAvgBytesPerSec; |
| 215 |
m_uiSampleSize = m_WF->nBlockAlign; |
| 216 |
|
| 217 |
switch (m_WF->wFormatTag) |
| 218 |
{ |
| 219 |
case 0x55: |
| 220 |
case 0x50: |
| 221 |
{ |
| 222 |
Mp3AudioInfo ai; |
| 223 |
Mp3AudioInfo last; |
| 224 |
char b[8]; |
| 225 |
size_t bf = 0; |
| 226 |
size_t rb = 0; |
| 227 |
int maxloop = 3000; |
| 228 |
memset(&last, 0, sizeof(last)); |
| 229 |
while (ai.frame_size == 0) |
| 230 |
{ |
| 231 |
if (bf) |
| 232 |
{ |
| 233 |
memmove(b, b + 1, bf); |
| 234 |
bf--; // next byte |
| 235 |
} |
| 236 |
size_t samp, bytes = 4 - bf; |
| 237 |
//printf("BYTES %" PRIsz " %" PRIsz "\n", bytes, bf); |
| 238 |
// SampleSize is 1 |
| 239 |
if (ReadDirect(b + bf, bytes, bytes, samp, bytes) < 0) |
| 240 |
//|| !bytes) |
| 241 |
break; |
| 242 |
//printf("Mp3Header READ %" PRIsz " %" PRIsz "\n", bytes, samp); |
| 243 |
bf += bytes; |
| 244 |
rb += bytes; |
| 245 |
//printf("RB %d %d 0x%x\n", rb, bytes, *(int*)b); |
| 246 |
if (!ai.Init(b, true)) |
| 247 |
continue; |
| 248 |
|
| 249 |
//ai.PrintHeader(); |
| 250 |
if (rem_size > 36 && rem_local >= 4) |
| 251 |
ai.Init(&rem_buffer[rem_local - 4]); // full init |
| 252 |
else |
| 253 |
{ |
| 254 |
ai.xing = 0; |
| 255 |
ai.bitrate = ai.GetBitrate(); |
| 256 |
ai.frame_size = ai.GetFrameSize(); |
| 257 |
} |
| 258 |
// now we have found 4 bytes which might be mp3 header |
| 259 |
// let's do some more checking |
| 260 |
//printf("BFSCAN %d %d %d\n", bf, rem_size, rem_local); |
| 261 |
if ((ai.frame_size - 4) == rem_size && rem_local >= 4) |
| 262 |
{ |
| 263 |
// heuristic 1: |
| 264 |
// looks like we have found reasonable good mp3 header |
| 265 |
// it's matching block boundary of the input frame |
| 266 |
//printf("YES 1st. match\n"); |
| 267 |
break; |
| 268 |
} |
| 269 |
//printf("AAA %d %d\n", ai.frame_size, rem_size); |
| 270 |
if ((ai.frame_size - 4) < rem_size) |
| 271 |
{ |
| 272 |
// we could check if the next packet has the same |
| 273 |
// characteristic |
| 274 |
Mp3AudioInfo bi; |
| 275 |
if (bi.Init(&rem_buffer[rem_local + ai.frame_size - 4], true) |
| 276 |
&& bi.sample_rate == ai.sample_rate |
| 277 |
&& bi.layer == ai.layer) |
| 278 |
{ |
| 279 |
//printf("YES 2nd. match\n"); |
| 280 |
break; |
| 281 |
} |
| 282 |
ai.frame_size = 0; |
| 283 |
continue; // failed - try to get next mp3 header |
| 284 |
} |
| 285 |
// this is unsolved if we get here with to short packet |
| 286 |
//ai.PrintHeader(); |
| 287 |
if (maxloop--) |
| 288 |
{ |
| 289 |
//last.PrintHeader(); |
| 290 |
if (last.sample_rate == ai.sample_rate |
| 291 |
&& last.layer == ai.layer) |
| 292 |
break; |
| 293 |
last = ai; |
| 294 |
ai.frame_size = 0; |
| 295 |
} |
| 296 |
else |
| 297 |
AVM_WRITE("audio reader", "failed to easily identify mp3 header! (%lu, %lu, %lu)\n", |
| 298 |
(unsigned long)rem_size, (unsigned long)rem_local, (unsigned long)ai.frame_size); |
| 299 |
|
| 300 |
//int ss = 0; for (uint_t x = 0; x < bytes; x++) ss += b[x]; |
| 301 |
//AVM_WRITE("audio reader", "HR %d lb %d samp: %d %d sum %d\n", hr, bytes, samp, bf, ss); |
| 302 |
} |
| 303 |
if (ai.frame_size) |
| 304 |
{ |
| 305 |
if (rem_local >= 4) |
| 306 |
{ |
| 307 |
// restore last mp3 header |
| 308 |
rem_local -= 4; // so unread |
| 309 |
rem_size += 4; |
| 310 |
rb -= 4; |
| 311 |
} |
| 312 |
ai.PrintHeader(); |
| 313 |
m_WF->nChannels = (uint16_t)ai.num_channels; |
| 314 |
m_WF->nSamplesPerSec = ai.sample_rate; |
| 315 |
m_WF->nBlockAlign = 1; // Windows likes that this way... |
| 316 |
//printf("BLOCKALIGN %d %d\n", m_WF->nBlockAlign, ai.frame_size); |
| 317 |
m_uiSampleSize = ai.frame_size; |
| 318 |
|
| 319 |
if (ai.layer == 3) |
| 320 |
{ |
| 321 |
if (m_WF->wFormatTag != 0x55) |
| 322 |
{ |
| 323 |
AVM_WRITE("audio reader", "WARNING: fixing bad MP3 Layer3 tag 0x%x -> 0x55\n", m_WF->wFormatTag); |
| 324 |
m_WF->wFormatTag = 0x55; // for layer3 |
| 325 |
}// FIXME ai.xing |
| 326 |
if (!ai.xing && stream->GetSampleSize()) |
| 327 |
{ |
| 328 |
if (ai.bitrate) |
| 329 |
{ |
| 330 |
// hard to calculate for VBR - so check only CBR |
| 331 |
int bt = m_uiBps - ai.bitrate / 8; |
| 332 |
if (bt < 0) bt = -bt; //labs |
| 333 |
// do bitrate fix - but only if the difference |
| 334 |
// is huge |
| 335 |
// I've seen stream where packets where |
| 336 |
// 16kbps 24kbps 16kbps --> ~20kbps |
| 337 |
if (bt > 1000) |
| 338 |
{ |
| 339 |
if (stream->FixAvgBytes(ai.bitrate / 8) == 0) |
| 340 |
{ |
| 341 |
AVM_WRITE("audio reader", "WARNING: fixing wrong avg bitrate %dB/s -> %dB/s\n", m_uiBps, ai.bitrate / 8); |
| 342 |
m_uiBps = m_WF->nAvgBytesPerSec = ai.bitrate / 8; |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
//printf("SAMELESIZE %d\n", stream->GetSampleSize()); |
| 347 |
int bs = avm_get_le16(&m_MP3WF->nBlockSize) - ai.frame_size; |
| 348 |
if (bs < 0) bs = -bs; |
| 349 |
if (bs > 256) |
| 350 |
{ |
| 351 |
AVM_WRITE("audio reader", "WARNING: fixing bad MP3 block size %d -> %d\n", |
| 352 |
avm_get_le16(&m_MP3WF->nBlockSize), ai.frame_size); |
| 353 |
avm_set_le16(&m_MP3WF->nBlockSize, (uint16_t)ai.frame_size); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
else |
| 358 |
{ |
| 359 |
if (m_WF->wFormatTag != 0x50) |
| 360 |
{ |
| 361 |
AVM_WRITE("audio reader", "WARNING: fixing bad MP3 Layer2 tag 0x%x - should be 0x50\n", m_WF->wFormatTag); |
| 362 |
m_WF->wFormatTag = 0x50; // for layer2 |
| 363 |
} |
| 364 |
} |
| 365 |
if (rb > 0) |
| 366 |
AVM_WRITE("audio reader", "junk size at the begining: time:%.2fs pos:%u (%" PRIsz "b)\n", GetTime(), GetPos(), rb); |
| 367 |
} |
| 368 |
} |
| 369 |
if (m_WF->wFormatTag == 0x55) |
| 370 |
m_bIsMp3 = true; |
| 371 |
break; |
| 372 |
case 0x2000: |
| 373 |
if (m_WF->nSamplesPerSec > 48000) |
| 374 |
m_WF->nSamplesPerSec = 48000; |
| 375 |
break; |
| 376 |
} |
| 377 |
|
| 378 |
//printf("AFTERCONST pos: %d l:%d s:%d\n", m_uiLastPos, rem_local, rem_size); |
| 379 |
} |
| 380 |
|
| 381 |
ReadStreamA::~ReadStreamA() |
| 382 |
{ |
| 383 |
StopStreaming(); |
| 384 |
} |
| 385 |
|
| 386 |
void ReadStreamA::Flush() |
| 387 |
{ |
| 388 |
ReadStream::Flush(); |
| 389 |
if (m_pAudiodecoder) |
| 390 |
m_pAudiodecoder->Flush(); |
| 391 |
} |
| 392 |
|
| 393 |
// FIXME - when m_pAudiodecoder is running we may supply real data |
| 394 |
size_t ReadStreamA::GetAudioFormat(void* wf, size_t size) const |
| 395 |
{ |
| 396 |
//printf("DATE %p %d\n", wf, size); |
| 397 |
//for (int i = 0; i < m_format_size; i++) |
| 398 |
// printf("### %d %x\n", i, ((unsigned char*)m_format)[i]); |
| 399 |
if (wf) |
| 400 |
memcpy(wf, m_pFormat, (size < m_uiFormatSize) ? size : m_uiFormatSize); |
| 401 |
return m_uiFormatSize; |
| 402 |
} |
| 403 |
|
| 404 |
size_t ReadStreamA::GetFrameSize() const |
| 405 |
{ |
| 406 |
return m_uiMinSize; |
| 407 |
} |
| 408 |
|
| 409 |
size_t ReadStreamA::GetOutputFormat(void* format, size_t size) const |
| 410 |
{ |
| 411 |
if (!m_pAudiodecoder) |
| 412 |
return 0; |
| 413 |
if (!format || size < sizeof(WAVEFORMATEX)) |
| 414 |
return sizeof(WAVEFORMATEX); |
| 415 |
|
| 416 |
return m_pAudiodecoder->GetOutputFormat((WAVEFORMATEX*)format); |
| 417 |
} |
| 418 |
|
| 419 |
framepos_t ReadStreamA::GetPos() const |
| 420 |
{ |
| 421 |
//printf("READA %d\n", m_uiSampleSize); |
| 422 |
framepos_t fp = m_uiLastPos; |
| 423 |
if (m_uiSampleSize) |
| 424 |
{ |
| 425 |
//printf("READAAAAA %d %d %d\n", (int)m_uiLastPos, (int)rem_size, (int)m_uiRead); |
| 426 |
fp += (framepos_t)(((int)m_uiRead - (int)rem_size) / (int)m_uiSampleSize); |
| 427 |
} |
| 428 |
|
| 429 |
return fp; |
| 430 |
} |
| 431 |
|
| 432 |
double ReadStreamA::GetTime(framepos_t pos) const |
| 433 |
{ |
| 434 |
//AVM_WRITE("audio reader", "AGetTime: %d last:%f rem_size:%d bps:%d\n", pos, m_dLastTime, rem_size, m_uiBps); |
| 435 |
if (pos == ERR) |
| 436 |
{ |
| 437 |
// Compensate time which is in buffer |
| 438 |
double smtime = m_dLastTime; |
| 439 |
if (m_uiBps > 0) |
| 440 |
{ |
| 441 |
//AVM_WRITE("audio reader", "AudioGetTime remsize %f time: %f pos: %d\n", rem_size/(double)m_uiBps, smtime, pos); |
| 442 |
// rem_size must be kept small otherwise there will be sync problems with VBR audio |
| 443 |
smtime += (double)((int)m_uiRead - (int)rem_size) / (double)m_uiBps; |
| 444 |
if (smtime < 0.) |
| 445 |
smtime = 0.; |
| 446 |
} |
| 447 |
//printf("ret %f -> %f %" PRIsz " %" PRIsz " (%f)\n", m_dLastTime, smtime, rem_size, |
| 448 |
// m_uiRead, (double)rem_size/(double)m_uiBps); |
| 449 |
return smtime; |
| 450 |
} |
| 451 |
return m_pStream->GetTime(pos); |
| 452 |
} |
| 453 |
|
| 454 |
bool ReadStreamA::IsStreaming() const |
| 455 |
{ |
| 456 |
return (m_pAudiodecoder != NULL); |
| 457 |
} |
| 458 |
|
| 459 |
int ReadStreamA::ReadFrames(void* buffer, size_t bufsize, size_t samples, |
| 460 |
size_t& samples_read, size_t& bytes_read) |
| 461 |
{ |
| 462 |
if (!m_pAudiodecoder || !samples || bufsize < m_uiMinSize) |
| 463 |
return -1; |
| 464 |
|
| 465 |
bool bHadPacket = false; |
| 466 |
//printf("***ReadFrames*** MIN %d SRC %d reml:%d rems:%d bs:%d\n", m_uiMinSize, srcsize, rem_local, rem_size, bufsize); |
| 467 |
if (0 && m_bIsMp3 && rem_size >= 4) |
| 468 |
{ |
| 469 |
// for vbr audio keep buffer minimal for the good timing |
| 470 |
Mp3AudioInfo ai; |
| 471 |
if (ai.Init(&rem_buffer[rem_local], false)) |
| 472 |
m_uiSrcSize = ai.frame_size * 2; |
| 473 |
//printf("RESULT %d %d\n", (int)m_uiSrcSize, (int)ai.frame_size); |
| 474 |
} |
| 475 |
|
| 476 |
//printf("REMSIZE s:%" PRIsz " lp:%" PRIsz " lim:%" PRIsz "\n", rem_size, rem_local, rem_limit); |
| 477 |
while (rem_size < m_uiSrcSize) |
| 478 |
{ |
| 479 |
//printf("SRCSIZE %d %d %d sam:%d loc:%d lim:%d\n", (int)bufsize, (int)m_uiSrcSize, (int)rem_size, (int)samples, (int)rem_local, (int)rem_limit); |
| 480 |
//AVM_WRITE("audio reader", "***********convert %d -> %d (rem: %d) %f\n", m_uiSrcSize, bufsize, rem_size); |
| 481 |
//AVM_WRITE("audio reader", "-----------bufsize: %d rem_size: %d srcsize: %d samples: %d\n", bufsize, rem_size, m_uiSrcSize, samples); |
| 482 |
if (m_pPacket) |
| 483 |
{ |
| 484 |
// wrap the buffer around |
| 485 |
if ((rem_local + m_uiSrcSize) >= rem_limit) |
| 486 |
{ |
| 487 |
//printf("MEMMOVE s:%" PRIsz " lp:%" PRIsz "\n", rem_size, rem_local); |
| 488 |
memmove(&rem_buffer[0], &rem_buffer[rem_local], rem_size); |
| 489 |
rem_local = 0; |
| 490 |
} |
| 491 |
bHadPacket = true; |
| 492 |
//printf("PACKETSIZE %" PRIsz "\n", m_pPacket->size); |
| 493 |
// add samples to internal buffer |
| 494 |
rem_size += m_pPacket->Read(&rem_buffer[rem_local + rem_size], m_uiSrcSize - rem_size); |
| 495 |
m_uiRead = m_pPacket->GetRead(); |
| 496 |
if (m_pPacket->GetSize() > 0) |
| 497 |
break; |
| 498 |
// packet empty -> read next packet |
| 499 |
} |
| 500 |
if (!ReadPacket()) |
| 501 |
{ |
| 502 |
m_uiRead = 0; |
| 503 |
if (!rem_size) |
| 504 |
m_iEof++; |
| 505 |
break; |
| 506 |
} |
| 507 |
//printf("-----SAMPLES %d %d\n", rem_size, p->size); |
| 508 |
} |
| 509 |
|
| 510 |
if (1 && m_bIsMp3) |
| 511 |
{ |
| 512 |
while (rem_size > 4) |
| 513 |
{ |
| 514 |
Mp3AudioInfo ai; |
| 515 |
// using AudioInfo is slower but also more safe... |
| 516 |
// this will skip bad mp3 chunks for Layer-3 audio stream |
| 517 |
if (ai.Init(&rem_buffer[rem_local], true) > 0 |
| 518 |
&& ai.sample_rate == ((WAVEFORMATEX*)m_pFormat)->nSamplesPerSec |
| 519 |
&& ai.layer == 3) |
| 520 |
break; |
| 521 |
//uint32_t t = avm_get_be32(rem_buffer + rem_local); printf("SKIP %d %d %x\n", rem_size, srcsize, t); |
| 522 |
// check next byte |
| 523 |
rem_local++; |
| 524 |
rem_size--; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
//for (int i = 0; i < 32; i++) printf(" %02x", (unsigned char) rem_buffer[rem_local + i]); printf("\n"); |
| 529 |
//Mp3AudioInfo ai; ai.Init(rem_buffer + rem_local, false); printf("AI SIZE %d\n", ai.frame_size); |
| 530 |
|
| 531 |
size_t ocnt = 0; |
| 532 |
size_t lr = 0; |
| 533 |
int hr = m_pAudiodecoder->Convert(&rem_buffer[rem_local], |
| 534 |
(m_uiSrcSize < rem_size) ? m_uiSrcSize : rem_size, |
| 535 |
(char*)buffer, bufsize, &lr, &ocnt); |
| 536 |
//int ds = open("/tmp/xyz", O_WRONLY | O_CREAT | O_APPEND, 0666); write(ds, &rem_buffer[rem_local], lr); close(ds); |
| 537 |
//int ws = open("/tmp/wav", O_WRONLY | O_CREAT | O_APPEND, 0666); write(ws, buffer, ocnt); close(ws); |
| 538 |
//printf("CONVSZE bytes:%d samples:%d r:%d rem_size:%d/%d bs:%d\n", (int)lr, (int)ocnt, hr, (int)rem_size, (int)m_uiSrcSize, (int)bufsize); |
| 539 |
if (hr >= 0 && (ocnt > 0 || lr > 0)) |
| 540 |
{ |
| 541 |
if (lr > rem_size) |
| 542 |
lr = rem_size; |
| 543 |
rem_local += lr; |
| 544 |
rem_size -= lr; |
| 545 |
} |
| 546 |
else |
| 547 |
{ |
| 548 |
//size_t ba = (m_uiSrcSize < rem_size) ? m_uiSrcSize : rem_size; |
| 549 |
size_t ba = ((WAVEFORMATEX*)m_pFormat)->nBlockAlign ? : 1; |
| 550 |
//printf("ERRORCONVSZE hr:%d lr:%d oc:%d rs:%d ba:%d\n", hr, (int)lr, (int)ocnt, (int)rem_size, (int)ba); |
| 551 |
if (rem_size > ba) |
| 552 |
{ |
| 553 |
rem_size -= ba; |
| 554 |
rem_local += ba; |
| 555 |
} |
| 556 |
else if (!bHadPacket) |
| 557 |
rem_size = 0; |
| 558 |
ocnt = 0; |
| 559 |
} |
| 560 |
|
| 561 |
//AVM_WRITE("audio reader", "locread:%6d outputsz:%7d convsz:%6d lr:%d bs:%d\n", rem_local, ocnt, convsz, lr, rem_size); |
| 562 |
#if 0 |
| 563 |
unsigned char* p = (unsigned char*)buffer + convsz; |
| 564 |
for (int i = -32; i < 64; i++) |
| 565 |
{ |
| 566 |
if (!(i % 8)) |
| 567 |
AVM_WRITE("audio reader", "\n%4d ", i); |
| 568 |
//AVM_WRITE("audio reader", " 0x%4.4x", abs(p[i] - p[i-2]) & 0xffff); |
| 569 |
AVM_WRITE("audio reader", " 0x%02X", p[i]); |
| 570 |
} |
| 571 |
#endif |
| 572 |
|
| 573 |
//printf("PACKET %d %d\n", bHadPacket, rem_size); |
| 574 |
//AVM_WRITE("audio reader", 1, "ReadStreamA::ReadFrames conv %d bytes to %d (%d)\n", |
| 575 |
// (int)rem_local, (int)ocnt, (int)bufsize); |
| 576 |
|
| 577 |
bytes_read = ocnt; |
| 578 |
samples_read = bytes_read; |
| 579 |
if (m_uiSampleSize > 1) |
| 580 |
samples_read /= m_uiSampleSize; |
| 581 |
|
| 582 |
//AVM_WRITE("audio reader", 3, "ReadStreamA: new sample is %d\n", m_pStream->GetPos()); |
| 583 |
return 0; |
| 584 |
} |
| 585 |
|
| 586 |
int ReadStreamA::SkipTo(double pos) |
| 587 |
{ |
| 588 |
//printf("SKIPPOS %f %f %d bps:%d\n", pos, GetTime(), m_uiSampleSize, m_uiBps); |
| 589 |
if (!m_uiSampleSize) |
| 590 |
return 0; // FIXME: can't skip in VBR audio stream for now |
| 591 |
|
| 592 |
uint8_t buf[MINSIZE]; |
| 593 |
const size_t max = (sizeof(buf) - PADDING) / m_uiSampleSize; |
| 594 |
while ((pos - GetTime()) > 0.001) |
| 595 |
{ |
| 596 |
size_t samples_read, bytes_read; |
| 597 |
size_t r = 2; |
| 598 |
if (m_uiBps > 0) |
| 599 |
{ |
| 600 |
if ((r = size_t((pos - GetTime()) * m_uiBps) / m_uiSampleSize) == 0) |
| 601 |
break; // can't get any closer |
| 602 |
|
| 603 |
if (r > max) |
| 604 |
r = max; |
| 605 |
} |
| 606 |
AVM_WRITE("audio reader", 3, "Skip blocks %d %d pos:%f t:%f\n", (int)r, (int)m_uiSampleSize, pos, GetTime()); |
| 607 |
if (ReadDirect(buf, r * m_uiSampleSize, r, samples_read, bytes_read) < 0 |
| 608 |
|| !bytes_read) |
| 609 |
break; |
| 610 |
} |
| 611 |
|
| 612 |
return 0; |
| 613 |
} |
| 614 |
|
| 615 |
int ReadStreamA::SeekTime(double timepos) |
| 616 |
{ |
| 617 |
double t = GetTime(); |
| 618 |
if ((timepos - t) < 0.002 && (timepos - t) > -0.002 && timepos > 0.002) |
| 619 |
{ |
| 620 |
//printf("---------SEEK DISCARD %f %f\n", t, timepos); |
| 621 |
return 0; |
| 622 |
} |
| 623 |
return ReadStream::SeekTime(timepos); |
| 624 |
} |
| 625 |
|
| 626 |
int ReadStreamA::StartStreaming(const char* privname) |
| 627 |
{ |
| 628 |
if (m_pAudiodecoder) |
| 629 |
return 0; // already streaming |
| 630 |
|
| 631 |
m_pAudiodecoder = CreateDecoderAudio((WAVEFORMATEX*)m_pFormat, privname); |
| 632 |
|
| 633 |
if (!m_pAudiodecoder) |
| 634 |
{ |
| 635 |
AVM_WRITE("audio reader", "Failed to initialize audio decoder for format 0x%x\n", |
| 636 |
((WAVEFORMATEX*)m_pFormat)->wFormatTag); |
| 637 |
return -1; |
| 638 |
} |
| 639 |
|
| 640 |
m_uiMinSize = m_pAudiodecoder->GetMinSize(); |
| 641 |
if (m_uiMinSize < MINSIZE) |
| 642 |
m_uiMinSize = MINSIZE; |
| 643 |
|
| 644 |
m_uiSrcSize = m_pAudiodecoder->GetSrcSize(SRCSIZE); |
| 645 |
if (m_uiSrcSize < SRCSIZE) |
| 646 |
m_uiSrcSize = SRCSIZE; |
| 647 |
|
| 648 |
if (rem_limit < (MINSIZE + m_uiSrcSize)) { |
| 649 |
rem_limit = MINSIZE + m_uiSrcSize; |
| 650 |
rem_buffer.reserve(rem_limit + PADDING); |
| 651 |
memset(rem_buffer.begin(), 0, rem_limit + PADDING); |
| 652 |
} |
| 653 |
AVM_WRITE("audio reader", 1, "Initialize audio decoder - minsize: %" PRIsz " srsize: %" PRIsz "\n", m_uiMinSize, m_uiSrcSize); |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
int ReadStreamA::StopStreaming() |
| 658 |
{ |
| 659 |
if (m_pAudiodecoder) |
| 660 |
{ |
| 661 |
FreeDecoderAudio(m_pAudiodecoder); |
| 662 |
m_pAudiodecoder = 0; |
| 663 |
m_uiMinSize = 0; |
| 664 |
} |
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
AVM_END_NAMESPACE; |