mirror of
https://github.com/nu774/fdkaac.git
synced 2025-06-05 23:29:14 +02:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
a7c2411335 | |||
b54538f79e | |||
a7e00a4219 | |||
b667da2b01 |
20
ChangeLog
20
ChangeLog
@ -1,14 +1,28 @@
|
||||
2014-02-17 nu774 <honeycomb77@gmail.com>
|
||||
2014-07-30 nu774 <honeycomb77@gmail.com>
|
||||
|
||||
* update ChangeLog [HEAD]
|
||||
|
||||
* win32compat: fix aacenc_printf() not to write junk characters [v0.5.4]
|
||||
|
||||
2014-05-12 nu774 <honeycomb77@gmail.com>
|
||||
|
||||
* write INSTALL [origin/master]
|
||||
|
||||
2014-03-14 nu774 <honeycomb77@gmail.com>
|
||||
|
||||
* update COPYING (patch from darealshinji)
|
||||
|
||||
2014-02-17 nu774 <honeycomb77@gmail.com>
|
||||
|
||||
* update ChangeLog
|
||||
|
||||
* bump version [v0.5.3]
|
||||
|
||||
* fix: CAF chan chunk using channel bitmap was not correctly handled
|
||||
|
||||
2014-01-18 nu774 <honeycomb77@gmail.com>
|
||||
|
||||
* update ChangeLog [origin/master]
|
||||
* update ChangeLog
|
||||
|
||||
* bump version [v0.5.2]
|
||||
|
||||
@ -112,7 +126,7 @@
|
||||
|
||||
2013-10-26 nu774 <honeycomb77@gmail.com>
|
||||
|
||||
* re-fix #ifdef cond for lrint() [old]
|
||||
* re-fix #ifdef cond for lrint()
|
||||
|
||||
* tag mapping: add recorded date and tempo, remove performer->artist
|
||||
|
||||
|
@ -99,12 +99,14 @@ copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ </Command>
|
||||
<ClCompile Include="..\src\caf_reader.c" />
|
||||
<ClCompile Include="..\src\compat_win32.c" />
|
||||
<ClCompile Include="..\src\extrapolater.c" />
|
||||
<ClCompile Include="..\src\limiter.c" />
|
||||
<ClCompile Include="..\src\lpc.c" />
|
||||
<ClCompile Include="..\src\lpcm.c" />
|
||||
<ClCompile Include="..\src\m4af.c" />
|
||||
<ClCompile Include="..\src\main.c" />
|
||||
<ClCompile Include="..\src\metadata.c" />
|
||||
<ClCompile Include="..\src\parson.c" />
|
||||
<ClCompile Include="..\src\pcm_float_converter.c" />
|
||||
<ClCompile Include="..\src\pcm_native_converter.c" />
|
||||
<ClCompile Include="..\src\pcm_readhelper.c" />
|
||||
<ClCompile Include="..\src\pcm_sint16_converter.c" />
|
||||
<ClCompile Include="..\src\progress.c" />
|
||||
@ -113,7 +115,6 @@ copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ </Command>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\missings\getopt.h" />
|
||||
<ClInclude Include="..\src\aacenc.h" />
|
||||
<ClInclude Include="..\src\caf_reader.h" />
|
||||
<ClInclude Include="..\src\catypes.h" />
|
||||
<ClInclude Include="..\src\compat.h" />
|
||||
<ClInclude Include="..\src\lpc.h" />
|
||||
@ -124,7 +125,6 @@ copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ </Command>
|
||||
<ClInclude Include="..\src\parson.h" />
|
||||
<ClInclude Include="..\src\pcm_reader.h" />
|
||||
<ClInclude Include="..\src\progress.h" />
|
||||
<ClInclude Include="..\src\wav_reader.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="fdk-aac.vcxproj">
|
||||
|
@ -27,10 +27,10 @@
|
||||
<ClCompile Include="..\src\extrapolater.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\lpc.c">
|
||||
<ClCompile Include="..\src\limiter.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\lpcm.c">
|
||||
<ClCompile Include="..\src\lpc.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\m4af.c">
|
||||
@ -39,6 +39,12 @@
|
||||
<ClCompile Include="..\src\main.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\pcm_float_converter.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\pcm_native_converter.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\src\pcm_readhelper.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
@ -59,9 +65,6 @@
|
||||
<ClInclude Include="..\src\aacenc.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\src\caf_reader.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\src\catypes.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
@ -86,9 +89,6 @@
|
||||
<ClInclude Include="..\src\progress.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\src\wav_reader.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\missings\getopt.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
|
@ -7,12 +7,14 @@ fdkaac_SOURCES = \
|
||||
src/aacenc.c \
|
||||
src/caf_reader.c \
|
||||
src/extrapolater.c \
|
||||
src/limiter.c \
|
||||
src/lpc.c \
|
||||
src/lpcm.c \
|
||||
src/m4af.c \
|
||||
src/main.c \
|
||||
src/metadata.c \
|
||||
src/parson.c \
|
||||
src/pcm_float_converter.c \
|
||||
src/pcm_native_converter.c \
|
||||
src/pcm_readhelper.c \
|
||||
src/pcm_sint16_converter.c \
|
||||
src/progress.c \
|
||||
|
@ -14,7 +14,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include "caf_reader.h"
|
||||
#include "pcm_reader.h"
|
||||
#include "m4af.h"
|
||||
|
||||
typedef struct caf_reader_t {
|
||||
|
@ -1,15 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 nu774
|
||||
* For conditions of distribution and use, see copyright notice in COPYING
|
||||
*/
|
||||
#ifndef CAF_READER_H
|
||||
#define CAF_READER_H
|
||||
|
||||
#include "lpcm.h"
|
||||
#include "pcm_reader.h"
|
||||
#include "metadata.h"
|
||||
|
||||
pcm_reader_t *caf_open(pcm_io_context_t *io,
|
||||
aacenc_tag_callback_t tag_callback, void *tag_ctx);
|
||||
|
||||
#endif
|
@ -86,11 +86,11 @@ static int fetch(extrapolater_t *self, unsigned nframes)
|
||||
|
||||
if (realloc_buffer(bp, nframes * sfmt->bytes_per_frame) == 0) {
|
||||
rc = pcm_read_frames(self->src, bp->data, nframes);
|
||||
bp->count = rc > 0 ? rc : 0;
|
||||
if (rc > 0) bp->count = rc;
|
||||
}
|
||||
if (rc > 0)
|
||||
self->nbuffer ^= 1;
|
||||
return bp->count;
|
||||
return rc <= 0 ? 0 : bp->count;
|
||||
}
|
||||
|
||||
static int extrapolate(extrapolater_t *self, const buffer_t *bp,
|
||||
@ -126,7 +126,13 @@ static int process0(extrapolater_t *self, void *buffer, unsigned nframes)
|
||||
reverse_buffer(buffer, nframes, nchannels);
|
||||
reverse_buffer(bp->data, bp->count, nchannels);
|
||||
}
|
||||
self->process = bp->count ? process1 : process2;
|
||||
if (bp->count)
|
||||
self->process = process1;
|
||||
else {
|
||||
memset(bp->data, 0, nframes * sfmt->bytes_per_frame);
|
||||
bp->count = nframes;
|
||||
self->process = process2;
|
||||
}
|
||||
return nframes;
|
||||
}
|
||||
|
||||
@ -137,8 +143,27 @@ static int process1(extrapolater_t *self, void *buffer, unsigned nframes)
|
||||
|
||||
assert(bp->count <= nframes);
|
||||
memcpy(buffer, bp->data, bp->count * sfmt->bytes_per_frame);
|
||||
if (!fetch(self, nframes))
|
||||
if (!fetch(self, nframes)) {
|
||||
buffer_t *bbp = &self->buffer[self->nbuffer];
|
||||
if (bp->count < 2 * LPC_ORDER) {
|
||||
size_t total = bp->count + bbp->count;
|
||||
if (bbp->count &&
|
||||
realloc_buffer(bbp, total * sfmt->bytes_per_frame) == 0 &&
|
||||
realloc_buffer(bp, total * sfmt->bytes_per_frame) == 0)
|
||||
{
|
||||
memcpy(bbp->data + bbp->count * sfmt->channels_per_frame,
|
||||
bp->data, bp->count * sfmt->bytes_per_frame);
|
||||
memcpy(bp->data, bbp->data, total * sfmt->bytes_per_frame);
|
||||
bp->count = total;
|
||||
}
|
||||
}
|
||||
if (bp->count >= 2 * LPC_ORDER)
|
||||
extrapolate(self, bp, bbp->data, nframes);
|
||||
else
|
||||
memset(bbp->data, 0, nframes * sfmt->bytes_per_frame);
|
||||
bbp->count = nframes;
|
||||
self->process = process2;
|
||||
}
|
||||
return bp->count;
|
||||
}
|
||||
|
||||
@ -146,27 +171,15 @@ static int process2(extrapolater_t *self, void *buffer, unsigned nframes)
|
||||
{
|
||||
const pcm_sample_description_t *sfmt = pcm_get_format(self->src);
|
||||
buffer_t *bp = &self->buffer[self->nbuffer];
|
||||
buffer_t *bbp = &self->buffer[self->nbuffer ^ 1];
|
||||
|
||||
if (bp->count < 2 * LPC_ORDER) {
|
||||
size_t total = bp->count + bbp->count;
|
||||
if (bbp->count &&
|
||||
realloc_buffer(bbp, total * sfmt->bytes_per_frame) == 0)
|
||||
{
|
||||
memcpy(bbp->data + bbp->count * sfmt->channels_per_frame,
|
||||
bp->data, bp->count * sfmt->bytes_per_frame);
|
||||
bbp->count = total;
|
||||
bp->count = 0;
|
||||
bp = bbp;
|
||||
self->nbuffer ^= 1;
|
||||
}
|
||||
}
|
||||
self->process = process3;
|
||||
|
||||
if (bp->count >= 2 * LPC_ORDER)
|
||||
extrapolate(self, bp, buffer, nframes);
|
||||
else
|
||||
memset(buffer, 0, nframes * sfmt->bytes_per_frame);
|
||||
if (bp->count < nframes)
|
||||
nframes = bp->count;
|
||||
memcpy(buffer, bp->data, nframes * sfmt->bytes_per_frame);
|
||||
if (bp->count > nframes)
|
||||
memmove(bp->data, bp->data + nframes * sfmt->channels_per_frame,
|
||||
(bp->count - nframes) * sfmt->bytes_per_frame);
|
||||
bp->count -= nframes;
|
||||
if (bp->count == 0)
|
||||
self->process = process3;
|
||||
return nframes;
|
||||
}
|
||||
|
||||
|
186
src/limiter.c
Normal file
186
src/limiter.c
Normal file
@ -0,0 +1,186 @@
|
||||
/*
|
||||
* Copyright (C) 2013 nu774
|
||||
* For conditions of distribution and use, see copyright notice in COPYING
|
||||
*/
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#if HAVE_STDINT_H
|
||||
# include <stdint.h>
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
#include "pcm_reader.h"
|
||||
#include "lpc.h"
|
||||
|
||||
typedef struct buffer_t {
|
||||
void *data;
|
||||
unsigned count;
|
||||
unsigned capacity;
|
||||
unsigned head;
|
||||
} buffer_t;
|
||||
|
||||
typedef struct limiter_t {
|
||||
pcm_reader_vtbl_t *vtbl;
|
||||
pcm_reader_t *src;
|
||||
pcm_sample_description_t format;
|
||||
int64_t position;
|
||||
buffer_t buffers[1];
|
||||
} limiter_t;
|
||||
|
||||
static inline pcm_reader_t *get_source(pcm_reader_t *reader)
|
||||
{
|
||||
return ((limiter_t *)reader)->src;
|
||||
}
|
||||
|
||||
static const
|
||||
pcm_sample_description_t *get_format(pcm_reader_t *reader)
|
||||
{
|
||||
return pcm_get_format(get_source(reader));
|
||||
}
|
||||
|
||||
static int64_t get_length(pcm_reader_t *reader)
|
||||
{
|
||||
return pcm_get_length(get_source(reader));
|
||||
}
|
||||
|
||||
static int64_t get_position(pcm_reader_t *reader)
|
||||
{
|
||||
return ((limiter_t *)reader)->position;
|
||||
}
|
||||
|
||||
static int reserve_buffer(buffer_t *bp, size_t required, unsigned unit)
|
||||
{
|
||||
if (bp->capacity < required) {
|
||||
unsigned newsize = 1;
|
||||
void *p;
|
||||
while (newsize < required)
|
||||
newsize <<= 1;
|
||||
p = realloc(bp->data, newsize * unit);
|
||||
if (!p) return -1;
|
||||
bp->data = p;
|
||||
bp->capacity = newsize;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int read_frames(pcm_reader_t *reader, void *buffer, unsigned nframes)
|
||||
{
|
||||
limiter_t *self = (limiter_t *)reader;
|
||||
unsigned i, n, res, nch = self->format.channels_per_frame;
|
||||
size_t bytes = nframes * pcm_get_format(self->src)->bytes_per_frame;
|
||||
buffer_t *ibp = &self->buffers[nch];
|
||||
float *obp = buffer;
|
||||
|
||||
do {
|
||||
if (reserve_buffer(ibp, bytes, 1) < 0)
|
||||
return -1;
|
||||
res = pcm_read_frames(self->src, ibp->data, nframes);
|
||||
for (n = 0; n < nch; ++n) {
|
||||
float *ip = (float *)ibp->data, *x;
|
||||
buffer_t *bp = &self->buffers[n];
|
||||
unsigned end, limit;
|
||||
if (reserve_buffer(bp, bp->count + res, sizeof(float)) < 0)
|
||||
return -1;
|
||||
x = bp->data;
|
||||
for (i = 0; i < res; ++i)
|
||||
x[bp->count++] = pcm_clip(ip[i * nch + n], -3.0, 3.0);
|
||||
limit = bp->count;
|
||||
if (limit > 0 && res > 0) {
|
||||
float last = x[limit - 1];
|
||||
for (; limit > 0 && x[limit-1] * last > 0; --limit)
|
||||
;
|
||||
}
|
||||
end = bp->head;
|
||||
while (end < limit) {
|
||||
unsigned start, peak_pos;
|
||||
float peak;
|
||||
for (peak_pos = end; peak_pos < limit; ++peak_pos)
|
||||
if (x[peak_pos] > 1.0f || x[peak_pos] < -1.0f)
|
||||
break;
|
||||
if (peak_pos == limit)
|
||||
break;
|
||||
start = peak_pos;
|
||||
peak = fabs(x[peak_pos]);
|
||||
while (start > bp->head && x[peak_pos] * x[start] >= 0.0f)
|
||||
--start;
|
||||
++start;
|
||||
for (end = peak_pos + 1; end < limit; ++end) {
|
||||
float y;
|
||||
if (x[peak_pos] * x[end] < 0.0f)
|
||||
break;
|
||||
y = fabs(x[end]);
|
||||
if (y > peak) {
|
||||
peak = y;
|
||||
peak_pos = end;
|
||||
}
|
||||
}
|
||||
if (peak < 2.0f) {
|
||||
float a = (peak - 1.0f) / (peak * peak);
|
||||
if (x[peak_pos] > 0.0f) a = -a;
|
||||
for (i = start; i < end; ++i)
|
||||
x[i] = x[i] + a * x[i] * x[i];
|
||||
} else {
|
||||
float u = peak, v = 1.0f;
|
||||
float a = (u - 2.0f * v) / (u * u * u);
|
||||
float b = (3.0f * v - 2.0f * u) / (u * u);
|
||||
if (x[peak_pos] < 0.0f) b = -b;
|
||||
for (i = start; i < end; ++i)
|
||||
x[i] = x[i] + b * x[i] * x[i] + a * x[i] * x[i] * x[i];
|
||||
}
|
||||
}
|
||||
bp->head = limit;
|
||||
}
|
||||
res = nframes;
|
||||
for (n = 0; n < nch; ++n)
|
||||
if (self->buffers[n].head < res)
|
||||
res = self->buffers[n].head;
|
||||
for (i = 0; i < res; ++i)
|
||||
for (n = 0; n < nch; ++n)
|
||||
*obp++ = ((float *)self->buffers[n].data)[i];
|
||||
if (res) {
|
||||
for (n = 0; n < nch; ++n) {
|
||||
buffer_t *bp = &self->buffers[n];
|
||||
float *p = bp->data;
|
||||
memmove(p, p + res, (bp->count - res) * sizeof(float));
|
||||
bp->count -= res;
|
||||
bp->head -= res;
|
||||
}
|
||||
}
|
||||
} while (res == 0 && self->buffers[0].count);
|
||||
self->position += res;
|
||||
return res;
|
||||
}
|
||||
|
||||
static void teardown(pcm_reader_t **reader)
|
||||
{
|
||||
int i;
|
||||
limiter_t *self = (limiter_t *)*reader;
|
||||
pcm_teardown(&self->src);
|
||||
for (i = 0; i < self->format.channels_per_frame + 1; ++i)
|
||||
free(self->buffers[i].data);
|
||||
free(self);
|
||||
*reader = 0;
|
||||
}
|
||||
|
||||
static pcm_reader_vtbl_t my_vtable = {
|
||||
get_format, get_length, get_position, read_frames, teardown
|
||||
};
|
||||
|
||||
pcm_reader_t *limiter_open(pcm_reader_t *reader)
|
||||
{
|
||||
limiter_t *self;
|
||||
int n = pcm_get_format(reader)->channels_per_frame;
|
||||
size_t size = sizeof(limiter_t) + offsetof(limiter_t, buffers[n + 1]);
|
||||
|
||||
if ((self = calloc(1, size)) == 0)
|
||||
return 0;
|
||||
self->src = reader;
|
||||
self->vtbl = &my_vtable;
|
||||
self->format = *pcm_get_format(reader);
|
||||
self->format.bits_per_channel = 32;
|
||||
return (pcm_reader_t *)self;
|
||||
}
|
226
src/lpcm.c
226
src/lpcm.c
@ -1,226 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 nu774
|
||||
* For conditions of distribution and use, see copyright notice in COPYING
|
||||
*/
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#if HAVE_STDINT_H
|
||||
# include <stdint.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "lpcm.h"
|
||||
#include "m4af_endian.h"
|
||||
|
||||
static
|
||||
inline float pcm_i2f(int32_t n)
|
||||
{
|
||||
union {
|
||||
int32_t ivalue;
|
||||
float fvalue;
|
||||
} u;
|
||||
u.ivalue = n;
|
||||
return u.fvalue;
|
||||
}
|
||||
static
|
||||
inline double pcm_i2d(int64_t n)
|
||||
{
|
||||
union {
|
||||
int64_t ivalue;
|
||||
double fvalue;
|
||||
} u;
|
||||
u.ivalue = n;
|
||||
return u.fvalue;
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_quantize_s32(int32_t n)
|
||||
{
|
||||
n = ((n >> 15) + 1) >> 1;
|
||||
return (n == 0x8000) ? 0x7fff : n;
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_quantize_f64(double v)
|
||||
{
|
||||
return (int16_t)lrint(pcm_clip(v * 32768.0, -32768.0, 32767.0));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_s8_to_s16(int8_t n)
|
||||
{
|
||||
return n << 8;
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_u8_to_s16(uint8_t n)
|
||||
{
|
||||
return (n << 8) ^ 0x8000;
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_s16le_to_s16(int16_t n)
|
||||
{
|
||||
return m4af_ltoh16(n);
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_s16be_to_s16(int16_t n)
|
||||
{
|
||||
return m4af_btoh16(n);
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_u16le_to_s16(uint16_t n)
|
||||
{
|
||||
return m4af_ltoh16(n) ^ 0x8000;
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_u16be_to_s16(uint16_t n)
|
||||
{
|
||||
return m4af_btoh16(n) ^ 0x8000;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_s24le_to_s32(uint8_t *p)
|
||||
{
|
||||
return p[0]<<8 | p[1]<<16 | p[2]<<24;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_s24be_to_s32(uint8_t *p)
|
||||
{
|
||||
return p[0]<<24 | p[1]<<16 | p[2]<<8;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_u24le_to_s32(uint8_t *p)
|
||||
{
|
||||
return pcm_s24le_to_s32(p) ^ 0x80000000;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_u24be_to_s32(uint8_t *p)
|
||||
{
|
||||
return pcm_s24be_to_s32(p) ^ 0x80000000;
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_s24le_to_s16(uint8_t *p)
|
||||
{
|
||||
return pcm_quantize_s32(pcm_s24le_to_s32(p));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_s24be_to_s16(uint8_t *p)
|
||||
{
|
||||
return pcm_quantize_s32(pcm_s24be_to_s32(p));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_u24le_to_s16(uint8_t *p)
|
||||
{
|
||||
return pcm_quantize_s32(pcm_u24le_to_s32(p));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_u24be_to_s16(uint8_t *p)
|
||||
{
|
||||
return pcm_quantize_s32(pcm_u24be_to_s32(p));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_s32le_to_s16(int32_t n)
|
||||
{
|
||||
return pcm_quantize_s32(m4af_ltoh32(n));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_s32be_to_s16(int32_t n)
|
||||
{
|
||||
return pcm_quantize_s32(m4af_btoh32(n));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_u32le_to_s16(int32_t n)
|
||||
{
|
||||
return pcm_quantize_s32(m4af_ltoh32(n) ^ 0x80000000);
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_u32be_to_s16(int32_t n)
|
||||
{
|
||||
return pcm_quantize_s32(m4af_btoh32(n) ^ 0x80000000);
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_f32le_to_s16(int32_t n)
|
||||
{
|
||||
return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n)));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_f32be_to_s16(int32_t n)
|
||||
{
|
||||
return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n)));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_f64le_to_s16(int64_t n)
|
||||
{
|
||||
return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n)));
|
||||
}
|
||||
static
|
||||
inline int16_t pcm_f64be_to_s16(int64_t n)
|
||||
{
|
||||
return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n)));
|
||||
}
|
||||
|
||||
int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
|
||||
const void *input, uint32_t nframes,
|
||||
int16_t *result)
|
||||
{
|
||||
#define CONVERT(type, conv) \
|
||||
do { \
|
||||
unsigned i; \
|
||||
type *ip = (type *)input; \
|
||||
for (i = 0; i < count; ++i) { \
|
||||
result[i] = conv(ip[i]); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define CONVERT_BYTES(conv) \
|
||||
do { \
|
||||
unsigned i, bytes_per_channel; \
|
||||
uint8_t *ip = (uint8_t *)input; \
|
||||
bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \
|
||||
for (i = 0; i < count; ++i) { \
|
||||
result[i] = conv(ip); \
|
||||
ip += bytes_per_channel; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
uint32_t count = nframes * format->channels_per_frame;
|
||||
if (!count)
|
||||
return 0;
|
||||
switch (PCM_BYTES_PER_CHANNEL(format) | format->sample_type<<4) {
|
||||
case 1 | PCM_TYPE_SINT<<4:
|
||||
CONVERT(int8_t, pcm_s8_to_s16); break;
|
||||
case 1 | PCM_TYPE_UINT<<4:
|
||||
CONVERT(uint8_t, pcm_u8_to_s16); break;
|
||||
case 2 | PCM_TYPE_SINT<<4:
|
||||
CONVERT(int16_t, pcm_s16le_to_s16); break;
|
||||
case 2 | PCM_TYPE_UINT<<4:
|
||||
CONVERT(uint16_t, pcm_u16le_to_s16); break;
|
||||
case 2 | PCM_TYPE_SINT_BE<<4:
|
||||
CONVERT(int16_t, pcm_s16be_to_s16); break;
|
||||
case 2 | PCM_TYPE_UINT_BE<<4:
|
||||
CONVERT(int16_t, pcm_u16be_to_s16); break;
|
||||
case 3 | PCM_TYPE_SINT<<4:
|
||||
CONVERT_BYTES(pcm_s24le_to_s16); break;
|
||||
case 3 | PCM_TYPE_UINT<<4:
|
||||
CONVERT_BYTES(pcm_u24le_to_s16); break;
|
||||
case 3 | PCM_TYPE_SINT_BE<<4:
|
||||
CONVERT_BYTES(pcm_s24be_to_s16); break;
|
||||
case 3 | PCM_TYPE_UINT_BE<<4:
|
||||
CONVERT_BYTES(pcm_u24be_to_s16); break;
|
||||
case 4 | PCM_TYPE_SINT<<4:
|
||||
CONVERT(int32_t, pcm_s32le_to_s16); break;
|
||||
case 4 | PCM_TYPE_UINT<<4:
|
||||
CONVERT(uint32_t, pcm_u32le_to_s16); break;
|
||||
case 4 | PCM_TYPE_FLOAT<<4:
|
||||
CONVERT(int32_t, pcm_f32le_to_s16); break;
|
||||
case 4 | PCM_TYPE_SINT_BE<<4:
|
||||
CONVERT(int32_t, pcm_s32be_to_s16); break;
|
||||
case 4 | PCM_TYPE_UINT_BE<<4:
|
||||
CONVERT(uint32_t, pcm_u32be_to_s16); break;
|
||||
case 4 | PCM_TYPE_FLOAT_BE<<4:
|
||||
CONVERT(int32_t, pcm_f32be_to_s16); break;
|
||||
case 8 | PCM_TYPE_FLOAT<<4:
|
||||
CONVERT(int64_t, pcm_f64le_to_s16); break;
|
||||
case 8 | PCM_TYPE_FLOAT_BE<<4:
|
||||
CONVERT(int64_t, pcm_f64be_to_s16); break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -61,7 +61,4 @@ inline double pcm_clip(double n, double min_value, double max_value)
|
||||
return n;
|
||||
}
|
||||
|
||||
int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
|
||||
const void *input, uint32_t nframes,
|
||||
int16_t *result);
|
||||
#endif
|
||||
|
25
src/main.c
25
src/main.c
@ -33,8 +33,7 @@
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#include "compat.h"
|
||||
#include "wav_reader.h"
|
||||
#include "caf_reader.h"
|
||||
#include "pcm_reader.h"
|
||||
#include "aacenc.h"
|
||||
#include "m4af.h"
|
||||
#include "progress.h"
|
||||
@ -699,7 +698,7 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
|
||||
if ((params->input_fp = aacenc_fopen(params->input_filename, "rb")) == 0) {
|
||||
aacenc_fprintf(stderr, "ERROR: %s: %s\n", params->input_filename,
|
||||
strerror(errno));
|
||||
goto END;
|
||||
goto FAIL;
|
||||
}
|
||||
io.cookie = params->input_fp;
|
||||
if (fstat(fileno(params->input_fp), &stb) == 0
|
||||
@ -713,7 +712,7 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
|
||||
pcm_sample_description_t desc = { 0 };
|
||||
if (parse_raw_spec(params->raw_format, &desc) < 0) {
|
||||
fprintf(stderr, "ERROR: invalid raw-format spec\n");
|
||||
goto END;
|
||||
goto FAIL;
|
||||
}
|
||||
desc.sample_rate = params->raw_rate;
|
||||
desc.channels_per_frame = params->raw_channels;
|
||||
@ -721,7 +720,7 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
|
||||
desc.bytes_per_frame = params->raw_channels * bytes_per_channel;
|
||||
if ((reader = raw_open(&io, &desc)) == 0) {
|
||||
fprintf(stderr, "ERROR: failed to open raw input\n");
|
||||
goto END;
|
||||
goto FAIL;
|
||||
}
|
||||
} else {
|
||||
int c;
|
||||
@ -731,7 +730,7 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
|
||||
case 'R':
|
||||
if ((reader = wav_open(&io, params->ignore_length)) == 0) {
|
||||
fprintf(stderr, "ERROR: broken / unsupported input file\n");
|
||||
goto END;
|
||||
goto FAIL;
|
||||
}
|
||||
break;
|
||||
case 'c':
|
||||
@ -741,18 +740,21 @@ pcm_reader_t *open_input(aacenc_param_ex_t *params)
|
||||
aacenc_translate_generic_text_tag,
|
||||
¶ms->source_tag_ctx)) == 0) {
|
||||
fprintf(stderr, "ERROR: broken / unsupported input file\n");
|
||||
goto END;
|
||||
goto FAIL;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "ERROR: unsupported input file\n");
|
||||
goto END;
|
||||
goto FAIL;
|
||||
}
|
||||
}
|
||||
if ((reader = pcm_open_sint16_converter(reader)) != 0)
|
||||
reader = pcm_open_native_converter(reader);
|
||||
if (reader && PCM_IS_FLOAT(pcm_get_format(reader)))
|
||||
reader = limiter_open(reader);
|
||||
if (reader && (reader = pcm_open_sint16_converter(reader)) != 0)
|
||||
reader = extrapolater_open(reader);
|
||||
return reader;
|
||||
END:
|
||||
FAIL:
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -824,8 +826,7 @@ int main(int argc, char **argv)
|
||||
goto END;
|
||||
m4af_set_decoder_specific_info(m4af, 0,
|
||||
aacinfo.confBuf, aacinfo.confSize);
|
||||
m4af_set_fixed_frame_duration(m4af, 0,
|
||||
framelen >> scale_shift);
|
||||
m4af_set_fixed_frame_duration(m4af, 0, framelen >> scale_shift);
|
||||
m4af_set_vbr_mode(m4af, 0, params.bitrate_mode);
|
||||
m4af_set_priming_mode(m4af, params.gapless_mode + 1);
|
||||
m4af_begin_write(m4af);
|
||||
|
85
src/pcm_float_converter.c
Normal file
85
src/pcm_float_converter.c
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (C) 2013 nu774
|
||||
* For conditions of distribution and use, see copyright notice in COPYING
|
||||
*/
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if HAVE_STDINT_H
|
||||
# include <stdint.h>
|
||||
#endif
|
||||
#include "pcm_reader.h"
|
||||
|
||||
typedef struct pcm_float_converter_t {
|
||||
pcm_reader_vtbl_t *vtbl;
|
||||
pcm_reader_t *src;
|
||||
pcm_sample_description_t format;
|
||||
} pcm_float_converter_t;
|
||||
|
||||
static inline pcm_reader_t *get_source(pcm_reader_t *reader)
|
||||
{
|
||||
return ((pcm_float_converter_t *)reader)->src;
|
||||
}
|
||||
|
||||
static const
|
||||
pcm_sample_description_t *get_format(pcm_reader_t *reader)
|
||||
{
|
||||
return &((pcm_float_converter_t *)reader)->format;
|
||||
}
|
||||
|
||||
static int64_t get_length(pcm_reader_t *reader)
|
||||
{
|
||||
return pcm_get_length(get_source(reader));
|
||||
}
|
||||
|
||||
static int64_t get_position(pcm_reader_t *reader)
|
||||
{
|
||||
return pcm_get_position(get_source(reader));
|
||||
}
|
||||
|
||||
static int read_frames(pcm_reader_t *reader, void *buffer, unsigned nframes)
|
||||
{
|
||||
unsigned i;
|
||||
pcm_float_converter_t *self = (pcm_float_converter_t *)reader;
|
||||
const pcm_sample_description_t *sfmt = pcm_get_format(self->src);
|
||||
nframes = pcm_read_frames(self->src, buffer, nframes);
|
||||
if (!(sfmt->sample_type & PCM_TYPE_FLOAT)) {
|
||||
int32_t *ip = buffer;
|
||||
float *op = buffer;
|
||||
unsigned i, count = nframes * sfmt->channels_per_frame;
|
||||
for (i = 0; i < count; ++i)
|
||||
op[i] = ip[i] / 2147483648.0f;
|
||||
}
|
||||
return nframes;
|
||||
}
|
||||
|
||||
static void teardown(pcm_reader_t **reader)
|
||||
{
|
||||
pcm_float_converter_t *self = (pcm_float_converter_t *)*reader;
|
||||
pcm_teardown(&self->src);
|
||||
free(self);
|
||||
*reader = 0;
|
||||
}
|
||||
|
||||
static pcm_reader_vtbl_t my_vtable = {
|
||||
get_format, get_length, get_position, read_frames, teardown
|
||||
};
|
||||
|
||||
pcm_reader_t *pcm_open_float_converter(pcm_reader_t *reader)
|
||||
{
|
||||
pcm_float_converter_t *self = 0;
|
||||
pcm_sample_description_t *fmt;
|
||||
|
||||
if ((self = calloc(1, sizeof(pcm_float_converter_t))) == 0)
|
||||
return 0;
|
||||
self->src = reader;
|
||||
self->vtbl = &my_vtable;
|
||||
memcpy(&self->format, pcm_get_format(reader), sizeof(self->format));
|
||||
fmt = &self->format;
|
||||
fmt->bits_per_channel = 32;
|
||||
fmt->sample_type = PCM_TYPE_FLOAT;
|
||||
fmt->bytes_per_frame = 4 * fmt->channels_per_frame;
|
||||
return (pcm_reader_t *)self;
|
||||
}
|
272
src/pcm_native_converter.c
Normal file
272
src/pcm_native_converter.c
Normal file
@ -0,0 +1,272 @@
|
||||
/*
|
||||
* Copyright (C) 2013 nu774
|
||||
* For conditions of distribution and use, see copyright notice in COPYING
|
||||
*/
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if HAVE_STDINT_H
|
||||
# include <stdint.h>
|
||||
#endif
|
||||
#include "m4af_endian.h"
|
||||
#include "pcm_reader.h"
|
||||
|
||||
static
|
||||
inline float pcm_i2f(int32_t n)
|
||||
{
|
||||
union {
|
||||
int32_t ivalue;
|
||||
float fvalue;
|
||||
} u;
|
||||
u.ivalue = n;
|
||||
return u.fvalue;
|
||||
}
|
||||
static
|
||||
inline double pcm_i2d(int64_t n)
|
||||
{
|
||||
union {
|
||||
int64_t ivalue;
|
||||
double fvalue;
|
||||
} u;
|
||||
u.ivalue = n;
|
||||
return u.fvalue;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_s8_to_s32(int8_t n)
|
||||
{
|
||||
return n << 24;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_u8_to_s32(uint8_t n)
|
||||
{
|
||||
return (n << 24) ^ 0x80000000;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_s16le_to_s32(int16_t n)
|
||||
{
|
||||
return m4af_ltoh16(n) << 16;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_s16be_to_s32(int16_t n)
|
||||
{
|
||||
return m4af_btoh16(n) << 16;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_u16le_to_s32(uint16_t n)
|
||||
{
|
||||
return (m4af_ltoh16(n) << 16) ^ 0x80000000;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_u16be_to_s32(uint16_t n)
|
||||
{
|
||||
return (m4af_btoh16(n) << 16) ^ 0x80000000;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_s24le_to_s32(uint8_t *p)
|
||||
{
|
||||
return p[0]<<8 | p[1]<<16 | p[2]<<24;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_s24be_to_s32(uint8_t *p)
|
||||
{
|
||||
return p[0]<<24 | p[1]<<16 | p[2]<<8;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_u24le_to_s32(uint8_t *p)
|
||||
{
|
||||
return pcm_s24le_to_s32(p) ^ 0x80000000;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_u24be_to_s32(uint8_t *p)
|
||||
{
|
||||
return pcm_s24be_to_s32(p) ^ 0x80000000;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_s32le_to_s32(int32_t n)
|
||||
{
|
||||
return m4af_ltoh32(n);
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_s32be_to_s32(int32_t n)
|
||||
{
|
||||
return m4af_btoh32(n);
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_u32le_to_s32(int32_t n)
|
||||
{
|
||||
return m4af_ltoh32(n) ^ 0x80000000;
|
||||
}
|
||||
static
|
||||
inline int32_t pcm_u32be_to_s32(int32_t n)
|
||||
{
|
||||
return m4af_btoh32(n) ^ 0x80000000;
|
||||
}
|
||||
static
|
||||
inline float pcm_f32le_to_f32(int32_t n)
|
||||
{
|
||||
return pcm_i2f(m4af_ltoh32(n));
|
||||
}
|
||||
static
|
||||
inline float pcm_f32be_to_f32(int32_t n)
|
||||
{
|
||||
return pcm_i2f(m4af_btoh32(n));
|
||||
}
|
||||
static
|
||||
inline float pcm_f64le_to_f32(int64_t n)
|
||||
{
|
||||
return pcm_i2d(m4af_ltoh64(n));
|
||||
}
|
||||
static
|
||||
inline float pcm_f64be_to_f32(int64_t n)
|
||||
{
|
||||
return pcm_i2d(m4af_btoh64(n));
|
||||
}
|
||||
|
||||
static
|
||||
int pcm_convert_to_native(const pcm_sample_description_t *format,
|
||||
const void *input, uint32_t nframes,
|
||||
void *result)
|
||||
{
|
||||
#define CONVERT(type, rtype, conv) \
|
||||
do { \
|
||||
unsigned i; \
|
||||
type *ip = (type *)input; \
|
||||
for (i = 0; i < count; ++i) { \
|
||||
((rtype *)result)[i] = conv(ip[i]); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define CONVERT_BYTES(rtype, conv) \
|
||||
do { \
|
||||
unsigned i, bytes_per_channel; \
|
||||
uint8_t *ip = (uint8_t *)input; \
|
||||
bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \
|
||||
for (i = 0; i < count; ++i) { \
|
||||
((rtype *)result)[i] = conv(ip); \
|
||||
ip += bytes_per_channel; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
uint32_t count = nframes * format->channels_per_frame;
|
||||
if (!count)
|
||||
return 0;
|
||||
switch (PCM_BYTES_PER_CHANNEL(format) | format->sample_type<<4) {
|
||||
case 1 | PCM_TYPE_SINT<<4:
|
||||
CONVERT(int8_t, int32_t, pcm_s8_to_s32); break;
|
||||
case 1 | PCM_TYPE_UINT<<4:
|
||||
CONVERT(uint8_t, int32_t, pcm_u8_to_s32); break;
|
||||
case 2 | PCM_TYPE_SINT<<4:
|
||||
CONVERT(int16_t, int32_t, pcm_s16le_to_s32); break;
|
||||
case 2 | PCM_TYPE_UINT<<4:
|
||||
CONVERT(uint16_t, int32_t, pcm_u16le_to_s32); break;
|
||||
case 2 | PCM_TYPE_SINT_BE<<4:
|
||||
CONVERT(int16_t, int32_t, pcm_s16be_to_s32); break;
|
||||
case 2 | PCM_TYPE_UINT_BE<<4:
|
||||
CONVERT(int16_t, int32_t, pcm_u16be_to_s32); break;
|
||||
case 3 | PCM_TYPE_SINT<<4:
|
||||
CONVERT_BYTES(int32_t, pcm_s24le_to_s32); break;
|
||||
case 3 | PCM_TYPE_UINT<<4:
|
||||
CONVERT_BYTES(int32_t, pcm_u24le_to_s32); break;
|
||||
case 3 | PCM_TYPE_SINT_BE<<4:
|
||||
CONVERT_BYTES(int32_t, pcm_s24be_to_s32); break;
|
||||
case 3 | PCM_TYPE_UINT_BE<<4:
|
||||
CONVERT_BYTES(int32_t, pcm_u24be_to_s32); break;
|
||||
case 4 | PCM_TYPE_SINT<<4:
|
||||
CONVERT(int32_t, int32_t, pcm_s32le_to_s32); break;
|
||||
case 4 | PCM_TYPE_UINT<<4:
|
||||
CONVERT(uint32_t, int32_t, pcm_u32le_to_s32); break;
|
||||
case 4 | PCM_TYPE_FLOAT<<4:
|
||||
CONVERT(int32_t, float, pcm_f32le_to_f32); break;
|
||||
case 4 | PCM_TYPE_SINT_BE<<4:
|
||||
CONVERT(int32_t, int32_t, pcm_s32be_to_s32); break;
|
||||
case 4 | PCM_TYPE_UINT_BE<<4:
|
||||
CONVERT(uint32_t, int32_t, pcm_u32be_to_s32); break;
|
||||
case 4 | PCM_TYPE_FLOAT_BE<<4:
|
||||
CONVERT(int32_t, float, pcm_f32be_to_f32); break;
|
||||
case 8 | PCM_TYPE_FLOAT<<4:
|
||||
CONVERT(int64_t, float, pcm_f64le_to_f32); break;
|
||||
case 8 | PCM_TYPE_FLOAT_BE<<4:
|
||||
CONVERT(int64_t, float, pcm_f64be_to_f32); break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct pcm_native_converter_t {
|
||||
pcm_reader_vtbl_t *vtbl;
|
||||
pcm_reader_t *src;
|
||||
pcm_sample_description_t format;
|
||||
void *pivot;
|
||||
unsigned capacity;
|
||||
} pcm_native_converter_t;
|
||||
|
||||
static inline pcm_reader_t *get_source(pcm_reader_t *reader)
|
||||
{
|
||||
return ((pcm_native_converter_t *)reader)->src;
|
||||
}
|
||||
|
||||
static const
|
||||
pcm_sample_description_t *get_format(pcm_reader_t *reader)
|
||||
{
|
||||
return &((pcm_native_converter_t *)reader)->format;
|
||||
}
|
||||
|
||||
static int64_t get_length(pcm_reader_t *reader)
|
||||
{
|
||||
return pcm_get_length(get_source(reader));
|
||||
}
|
||||
|
||||
static int64_t get_position(pcm_reader_t *reader)
|
||||
{
|
||||
return pcm_get_position(get_source(reader));
|
||||
}
|
||||
|
||||
static int read_frames(pcm_reader_t *reader, void *buffer, unsigned nframes)
|
||||
{
|
||||
pcm_native_converter_t *self = (pcm_native_converter_t *)reader;
|
||||
const pcm_sample_description_t *sfmt = pcm_get_format(self->src);
|
||||
unsigned bytes = nframes * sfmt->bytes_per_frame;
|
||||
|
||||
if (self->capacity < bytes) {
|
||||
void *p = realloc(self->pivot, bytes);
|
||||
if (!p) return -1;
|
||||
self->pivot = p;
|
||||
self->capacity = bytes;
|
||||
}
|
||||
nframes = pcm_read_frames(self->src, self->pivot, nframes);
|
||||
if (pcm_convert_to_native(sfmt, self->pivot, nframes, buffer) < 0)
|
||||
return -1;
|
||||
return nframes;
|
||||
}
|
||||
|
||||
static void teardown(pcm_reader_t **reader)
|
||||
{
|
||||
pcm_native_converter_t *self = (pcm_native_converter_t *)*reader;
|
||||
pcm_teardown(&self->src);
|
||||
free(self->pivot);
|
||||
free(self);
|
||||
*reader = 0;
|
||||
}
|
||||
|
||||
static pcm_reader_vtbl_t my_vtable = {
|
||||
get_format, get_length, get_position, read_frames, teardown
|
||||
};
|
||||
|
||||
pcm_reader_t *pcm_open_native_converter(pcm_reader_t *reader)
|
||||
{
|
||||
pcm_native_converter_t *self = 0;
|
||||
pcm_sample_description_t *fmt;
|
||||
|
||||
if ((self = calloc(1, sizeof(pcm_native_converter_t))) == 0)
|
||||
return 0;
|
||||
self->src = reader;
|
||||
self->vtbl = &my_vtable;
|
||||
memcpy(&self->format, pcm_get_format(reader), sizeof(self->format));
|
||||
fmt = &self->format;
|
||||
fmt->sample_type = PCM_IS_FLOAT(fmt) ? PCM_TYPE_FLOAT : PCM_TYPE_SINT;
|
||||
fmt->bytes_per_frame = 4 * fmt->channels_per_frame;
|
||||
return (pcm_reader_t *)self;
|
||||
}
|
@ -6,6 +6,7 @@
|
||||
#define PCM_READER_H
|
||||
|
||||
#include "lpcm.h"
|
||||
#include "metadata.h"
|
||||
|
||||
typedef struct pcm_reader_t pcm_reader_t;
|
||||
|
||||
@ -54,11 +55,7 @@ int64_t pcm_get_position(pcm_reader_t *r)
|
||||
return r->vtbl->get_position(r);
|
||||
}
|
||||
|
||||
static inline
|
||||
int64_t pcm_read_frames(pcm_reader_t *r, void *data, unsigned nframes)
|
||||
{
|
||||
return r->vtbl->read_frames(r, data, nframes);
|
||||
}
|
||||
int pcm_read_frames(pcm_reader_t *r, void *data, unsigned nframes);
|
||||
|
||||
static inline
|
||||
void pcm_teardown(pcm_reader_t **r)
|
||||
@ -111,8 +108,17 @@ int pcm_scanb(pcm_io_context_t *io, const char *fmt, ...);
|
||||
int apple_chan_chunk(pcm_io_context_t *io, uint32_t chunk_size,
|
||||
pcm_sample_description_t *fmt, uint8_t *mapping);
|
||||
|
||||
pcm_reader_t *wav_open(pcm_io_context_t *io, int ignore_length);
|
||||
pcm_reader_t *raw_open(pcm_io_context_t *io,
|
||||
const pcm_sample_description_t *desc);
|
||||
pcm_reader_t *caf_open(pcm_io_context_t *io,
|
||||
aacenc_tag_callback_t tag_callback, void *tag_ctx);
|
||||
|
||||
pcm_reader_t *pcm_open_native_converter(pcm_reader_t *reader);
|
||||
pcm_reader_t *pcm_open_float_converter(pcm_reader_t *reader);
|
||||
pcm_reader_t *pcm_open_sint16_converter(pcm_reader_t *reader);
|
||||
|
||||
pcm_reader_t *extrapolater_open(pcm_reader_t *reader);
|
||||
pcm_reader_t *limiter_open(pcm_reader_t *reader);
|
||||
|
||||
#endif
|
||||
|
@ -19,15 +19,35 @@
|
||||
#include "m4af_endian.h"
|
||||
#include "catypes.h"
|
||||
|
||||
int pcm_read_frames(pcm_reader_t *r, void *data, unsigned nframes)
|
||||
{
|
||||
int n;
|
||||
unsigned count = 0;
|
||||
uint8_t *bp = data;
|
||||
unsigned bpf = pcm_get_format(r)->bytes_per_frame;
|
||||
|
||||
do {
|
||||
n = r->vtbl->read_frames(r, bp, nframes - count);
|
||||
if (n > 0) {
|
||||
count += n;
|
||||
bp += n * bpf;
|
||||
}
|
||||
} while (n > 0 && count < nframes);
|
||||
return count;
|
||||
}
|
||||
|
||||
int pcm_read(pcm_io_context_t *io, void *buffer, uint32_t size)
|
||||
{
|
||||
int rc;
|
||||
uint32_t count = 0;
|
||||
uint8_t *bp = buffer;
|
||||
|
||||
do {
|
||||
rc = io->vtbl->read(io->cookie, buffer, size - count);
|
||||
if (rc > 0)
|
||||
rc = io->vtbl->read(io->cookie, bp, size - count);
|
||||
if (rc > 0) {
|
||||
count += rc;
|
||||
bp += rc;
|
||||
}
|
||||
} while (rc > 0 && count < size);
|
||||
return count > 0 ? count : rc;
|
||||
}
|
||||
|
@ -43,10 +43,10 @@ static int64_t get_position(pcm_reader_t *reader)
|
||||
|
||||
static int read_frames(pcm_reader_t *reader, void *buffer, unsigned nframes)
|
||||
{
|
||||
unsigned i, count;
|
||||
pcm_sint16_converter_t *self = (pcm_sint16_converter_t *)reader;
|
||||
const pcm_sample_description_t *sfmt = pcm_get_format(self->src);
|
||||
unsigned bytes = nframes * sfmt->bytes_per_frame;
|
||||
|
||||
if (self->capacity < bytes) {
|
||||
void *p = realloc(self->pivot, bytes);
|
||||
if (!p) return -1;
|
||||
@ -54,8 +54,25 @@ static int read_frames(pcm_reader_t *reader, void *buffer, unsigned nframes)
|
||||
self->capacity = bytes;
|
||||
}
|
||||
nframes = pcm_read_frames(self->src, self->pivot, nframes);
|
||||
if (pcm_convert_to_native_sint16(sfmt, self->pivot, nframes, buffer) < 0)
|
||||
return -1;
|
||||
count = nframes * sfmt->channels_per_frame;
|
||||
if (PCM_IS_FLOAT(sfmt)) {
|
||||
float *ip = self->pivot;
|
||||
int16_t *op = buffer;
|
||||
for (i = 0; i < count; ++i)
|
||||
op[i] = pcm_clip(ip[i] * 32768.0, -32768.0, 32767.0);
|
||||
} else {
|
||||
int32_t *ip = self->pivot;
|
||||
int16_t *op = buffer;
|
||||
if (sfmt->bits_per_channel <= 16) {
|
||||
for (i = 0; i < count; ++i)
|
||||
op[i] = ip[i] >> 16;
|
||||
} else {
|
||||
for (i = 0; i < count; ++i) {
|
||||
int n = ((ip[i] >> 15) + 1) >> 1;
|
||||
op[i] = (n == 0x8000) ? 0x7fff : n;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nframes;
|
||||
}
|
||||
|
||||
@ -83,12 +100,8 @@ pcm_reader_t *pcm_open_sint16_converter(pcm_reader_t *reader)
|
||||
self->vtbl = &my_vtable;
|
||||
memcpy(&self->format, pcm_get_format(reader), sizeof(self->format));
|
||||
fmt = &self->format;
|
||||
#if WORDS_BIGENDIAN
|
||||
fmt->sample_type = PCM_TYPE_SINT_BE;
|
||||
#else
|
||||
fmt->sample_type = PCM_TYPE_SINT;
|
||||
#endif
|
||||
fmt->bits_per_channel = 16;
|
||||
fmt->sample_type = PCM_TYPE_SINT;
|
||||
fmt->bytes_per_frame = 2 * fmt->channels_per_frame;
|
||||
return (pcm_reader_t *)self;
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include "wav_reader.h"
|
||||
#include "pcm_reader.h"
|
||||
|
||||
#define RIFF_FOURCC(a,b,c,d) ((a)|((b)<<8)|((c)<<16)|((d)<<24))
|
||||
|
||||
|
@ -1,15 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2013 nu774
|
||||
* For conditions of distribution and use, see copyright notice in COPYING
|
||||
*/
|
||||
#ifndef WAV_READER_H
|
||||
#define WAV_READER_H
|
||||
|
||||
#include "lpcm.h"
|
||||
#include "pcm_reader.h"
|
||||
|
||||
pcm_reader_t *wav_open(pcm_io_context_t *io, int ignore_length);
|
||||
pcm_reader_t *raw_open(pcm_io_context_t *io,
|
||||
const pcm_sample_description_t *desc);
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user