1
0
mirror of https://github.com/nu774/fdkaac.git synced 2025-06-05 23:29:14 +02:00

11 Commits

Author SHA1 Message Date
bd8c9a5d15 bump version 2013-10-22 19:50:35 +09:00
91ef87b610 fixed bogus sgpd written on --gapless-mode=1 and 2 2013-10-22 19:49:01 +09:00
29e0948097 bump version 2013-10-21 00:35:51 +09:00
e8e9f79eec reimplement int16 conversion as pcm_reader 2013-10-21 00:17:14 +09:00
2d744bd56c add abstraction layer for pcm reading 2013-10-20 22:50:10 +09:00
2f6fc566cc improve handling of avgBitrate 2013-10-20 21:33:06 +09:00
0c502d30e5 update ChangeLog and git2changelog.py 2013-10-19 12:54:50 +09:00
b87f2251a4 bump version 2013-10-18 22:36:10 +09:00
afe73f4916 update README 2013-10-18 22:35:34 +09:00
fe2d3aa3e7 set avgBitrate field to zero for 14496-1 compliance 2013-10-18 22:11:09 +09:00
2f960ef8fd updated ChangeLog with new git2changelog.py 2013-09-07 15:58:59 +09:00
16 changed files with 538 additions and 105 deletions

206
ChangeLog
View File

@ -0,0 +1,206 @@
2013-10-22 nu774 <honeycomb77@gmail.com>
* bump version [HEAD]
* fixed bogus sgpd written on --gapless-mode=1 and 2
2013-10-21 nu774 <honeycomb77@gmail.com>
* bump version [v0.3.2]
2013-10-20 nu774 <honeycomb77@gmail.com>
* reimplement int16 conversion as pcm_reader
* add abstraction layer for pcm reading
* improve handling of avgBitrate
2013-10-19 nu774 <honeycomb77@gmail.com>
* update ChangeLog and git2changelog.py
2013-10-18 nu774 <honeycomb77@gmail.com>
* bump version [v0.3.1]
* update README
* set avgBitrate field to zero for 14496-1 compliance
2013-09-07 nu774 <honeycomb77@gmail.com>
* updated ChangeLog with new git2changelog.py
2013-06-14 nu774 <honeycomb77@gmail.com>
* add --moov-before-mdat [v0.3.0]
2013-03-04 nu774 <honeycomb77@gmail.com>
* fix an error message
2013-03-03 nu774 <honeycomb77@gmail.com>
* bump version [v0.2.0]
* add --gapless-mode
2013-02-20 nu774 <honeycomb77@gmail.com>
* simplify __timeb64 condition
* use fseeko64() on i686-pc-mingw32
2013-02-18 nu774 <honeycomb77@gmail.com>
* fix build issue on i686-pc-mingw (struct __timeb64 is missing)
2013-02-17 nu774 <honeycomb77@gmail.com>
* bump version [v0.1.9]
* fix to accept option -C
2013-02-16 nu774 <honeycomb77@gmail.com>
* bump version [v0.1.8]
* refine json metadata importing
* m4af: duplication check on adding tags
2013-02-15 nu774 <honeycomb77@gmail.com>
* add --tag-from-json [v0.1.7]
* fix implicit int variable decl.
* update m4af
2013-02-03 nu774 <honeycomb77@gmail.com>
* bump version [v0.1.6]
* win32: change _wfopen() -> wfsopen()
2013-01-30 nu774 <honeycomb77@gmail.com>
* update README (add note for character encoding)
2013-01-28 nu774 <honeycomb77@gmail.com>
* bump version [v0.1.5]
* gracefully shutdown on signals
2013-01-27 nu774 <honeycomb77@gmail.com>
* fix MSVC project build issue
2013-01-25 nu774 <honeycomb77@gmail.com>
* bump version [v0.1.4]
* add --tag-from-file
2013-01-24 nu774 <honeycomb77@gmail.com>
* add --silent
2013-01-19 nu774 <honeycomb77@gmail.com>
* retab
* bump version [v0.1.3]
* fix crash on wrong long option, rename --ignore-length to --ignorelength
2013-01-17 nu774 <honeycomb77@gmail.com>
* bump version [v0.1.2]
* compat_win32: free argv with atexit()
* take care of COPYRIGHT-SIGN in UTF-8
2013-01-15 nu774 <honeycomb77@gmail.com>
* bump version [v0.1.1]
* fix return type of put_type_entry() to void
* add ADTS header size(7) to output byte length
2013-01-13 nu774 <honeycomb77@gmail.com>
* fix mp4 duration & version calcuration [v0.1.0]
2013-01-11 nu774 <honeycomb77@gmail.com>
* add support for xid
* support for i686-pc-mingw32 (missing _vscprintf)
2013-01-10 nu774 <honeycomb77@gmail.com>
* bump version [v0.0.9]
* rename basename() -> aacenc_basename() and move to compat layer
2013-01-09 nu774 <honeycomb77@gmail.com>
* add --tag and --long-tag
* fix corner case of progress display
* calculate length from file size
* raw input support
2013-01-08 nu774 <honeycomb77@gmail.com>
* insert a white space in progress message
2013-01-07 nu774 <honeycomb77@gmail.com>
* fix typo of bitrate-mode option [v0.0.8]
* more static inlining (missed on the previous commit) [v0.0.7]
* check error of fread() and fwrite() [v0.0.6]
* change inline->static inline to follow C99 semantics (for Clang)
* explicitly add -lfdk-aac to LDADD in Makefile.am
* add some files to EXTRA_DIST in Makefile.am
* fixed a typo in usage message [v0.0.5]
2013-01-06 nu774 <honeycomb77@gmail.com>
* add MSVC projects
* add .gitattributes
* more tweak on configure.ac and Makefile.am (take care of getopt_long) [v0.0.4]
* use fstat() to test seekability of input file
* retrieve bitrate for tool tag with aacEncoder_GetParam()
* output to current working directory by default
2013-01-05 nu774 <honeycomb77@gmail.com>
* zero clear LIB_INFO before calling aacEncGetLibInfo() [v0.0.3]
* tweak configure.ac and Makefile.am
* update version.h [v0.0.2]
* fixed to clip before converting float to int
* initial commit [v0.0.1]

View File

@ -102,6 +102,7 @@ copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ </Command>
<ClCompile Include="..\src\main.c" /> <ClCompile Include="..\src\main.c" />
<ClCompile Include="..\src\metadata.c" /> <ClCompile Include="..\src\metadata.c" />
<ClCompile Include="..\src\parson.c" /> <ClCompile Include="..\src\parson.c" />
<ClCompile Include="..\src\pcm_sint16_converter.c" />
<ClCompile Include="..\src\progress.c" /> <ClCompile Include="..\src\progress.c" />
<ClCompile Include="..\src\wav_reader.c" /> <ClCompile Include="..\src\wav_reader.c" />
</ItemGroup> </ItemGroup>

View File

@ -56,6 +56,9 @@
<ClInclude Include="..\src\m4af_endian.h"> <ClInclude Include="..\src\m4af_endian.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\pcm_reader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\src\progress.h"> <ClInclude Include="..\src\progress.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>

View File

@ -4,13 +4,14 @@ AUTOMAKE_OPTIONS = subdir-objects
bin_PROGRAMS = fdkaac bin_PROGRAMS = fdkaac
fdkaac_SOURCES = \ fdkaac_SOURCES = \
src/aacenc.c \ src/aacenc.c \
src/lpcm.c \ src/lpcm.c \
src/m4af.c \ src/m4af.c \
src/main.c \ src/main.c \
src/metadata.c \ src/metadata.c \
src/parson.c \ src/parson.c \
src/progress.c \ src/pcm_sint16_converter.c \
src/progress.c \
src/wav_reader.c src/wav_reader.c
fdkaac_LDADD = \ fdkaac_LDADD = \

13
README
View File

@ -32,6 +32,19 @@ look like the following:
MSVC solution for Visual Studio 2010 is under MSVC directory. MSVC solution for Visual Studio 2010 is under MSVC directory.
Available input format
----------------------
WAV (or RF64), upto 32bit int / 64bit float format is supported.
However, since FDK AAC encoder is implemented based on fixed point integer,
encoder itself treats 16bit input only.
Therefore, when feeding non-integer input, be careful so that input doesn't
exceed 0dBFS to avoid hard clips.
You might also want to apply dither/noise shape beforehand when your input
has higher resolution.
Note that fdkaac doesn't automatically resample for you
when input samplerate is not supported by AAC spec.
Tagging Options Tagging Options
--------------- ---------------
Generic tagging options like --tag, --tag-from-file, --long-tag allows you Generic tagging options like --tag, --tag-from-file, --long-tag allows you

44
git2changelog.py Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env python
# Copyright (C) 2013 nu774
# For conditions of distribution and use, see copyright notice in COPYING
import sys
import re
from subprocess import Popen, PIPE
from itertools import groupby
from collections import namedtuple
GITLOG_FMT = 'commit %H%nauthor %cn <%ae>%ndate %ad%nsubject %s%nref %d%n%n'
GITLOG_CMD = ['git','log','--date=short','--format={0}'.format(GITLOG_FMT)]
Commit = namedtuple('Commit', 'commit author date subject ref')
def parse_gitlog(stream):
re_decode_ref = re.compile(r'(?<=\()([^,)]+)')
re_strip_tag = re.compile(r'^tag: ')
commit = dict()
for line in stream:
fields = line.decode('utf-8').rstrip('\r\n').split(' ', 1)
if len(fields) == 2:
key, value = fields
if key == 'ref':
m = re_decode_ref.search(value)
if m:
value = ' [{0}]'.format(re_strip_tag.sub('', m.group()))
else:
value = ''
commit[key] = value
elif commit:
yield Commit(**commit)
commit = dict()
output=sys.stdout.write
with Popen(GITLOG_CMD, shell=False, stdout=PIPE).stdout as pipe:
commits = parse_gitlog(pipe)
commits_by_date_author = groupby(commits, key=lambda x: (x.date, x.author))
for (date, author), commits in commits_by_date_author:
output('{0} {1}\n\n'.format(date, author))
for c in commits:
output(' * {0}{1}\n\n'.format(c.subject, c.ref))

View File

@ -187,14 +187,14 @@ inline int16_t pcm_f64be_to_s16(int64_t n)
int pcm_convert_to_native_sint16(const pcm_sample_description_t *format, int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
const void *input, uint32_t nframes, const void *input, uint32_t nframes,
int16_t **result, uint32_t *osize) int16_t *result)
{ {
#define CONVERT(type, conv) \ #define CONVERT(type, conv) \
do { \ do { \
unsigned i; \ unsigned i; \
type *ip = (type *)input; \ type *ip = (type *)input; \
for (i = 0; i < count; ++i) { \ for (i = 0; i < count; ++i) { \
(*result)[i] = conv(ip[i]); \ result[i] = conv(ip[i]); \
} \ } \
} while(0) } while(0)
@ -204,7 +204,7 @@ int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
uint8_t *ip = (uint8_t *)input; \ uint8_t *ip = (uint8_t *)input; \
bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \ bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \
for (i = 0; i < count; ++i) { \ for (i = 0; i < count; ++i) { \
(*result)[i] = conv(ip); \ result[i] = conv(ip); \
ip += bytes_per_channel; \ ip += bytes_per_channel; \
} \ } \
} while(0) } while(0)
@ -212,11 +212,6 @@ int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
uint32_t count = nframes * format->channels_per_frame; uint32_t count = nframes * format->channels_per_frame;
if (!count) if (!count)
return 0; return 0;
if (!*result || *osize < count) {
*osize = count;
*result = realloc(*result, count * sizeof(int16_t));
}
switch (PCM_BYTES_PER_CHANNEL(format) | format->sample_type<<4) { switch (PCM_BYTES_PER_CHANNEL(format) | format->sample_type<<4) {
case 1 | PCM_TYPE_SINT<<4: case 1 | PCM_TYPE_SINT<<4:
CONVERT(int8_t, pcm_s8_to_s16); break; CONVERT(int8_t, pcm_s8_to_s16); break;

View File

@ -33,5 +33,5 @@ typedef struct pcm_sample_description_t {
int pcm_convert_to_native_sint16(const pcm_sample_description_t *format, int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
const void *input, uint32_t nframes, const void *input, uint32_t nframes,
int16_t **result, uint32_t *osize); int16_t *result);
#endif #endif

View File

@ -53,6 +53,7 @@ typedef struct m4af_track_t {
uint32_t bufferSizeDB; uint32_t bufferSizeDB;
uint32_t maxBitrate; uint32_t maxBitrate;
uint32_t avgBitrate; uint32_t avgBitrate;
int is_vbr;
m4af_sample_entry_t *sample_table; m4af_sample_entry_t *sample_table;
uint32_t num_samples; uint32_t num_samples;
@ -278,6 +279,12 @@ DONE:
return ctx->last_error; return ctx->last_error;
} }
void m4af_set_vbr_mode(m4af_ctx_t *ctx, uint32_t track_idx, int is_vbr)
{
m4af_track_t *track = &ctx->track[track_idx];
track->is_vbr = is_vbr;
}
void m4af_set_priming(m4af_ctx_t *ctx, uint32_t track_idx, void m4af_set_priming(m4af_ctx_t *ctx, uint32_t track_idx,
uint32_t encoder_delay, uint32_t padding) uint32_t encoder_delay, uint32_t padding)
{ {
@ -771,7 +778,7 @@ void m4af_write_esds_box(m4af_ctx_t *ctx, uint32_t track_idx)
, 2); , 2);
m4af_write24(ctx, track->bufferSizeDB); m4af_write24(ctx, track->bufferSizeDB);
m4af_write32(ctx, track->maxBitrate); m4af_write32(ctx, track->maxBitrate);
m4af_write32(ctx, track->avgBitrate); m4af_write32(ctx, track->is_vbr ? 0: track->avgBitrate);
/* DecoderSpecificInfo */ /* DecoderSpecificInfo */
m4af_write_descriptor(ctx, 5, track->decSpecificInfoSize); m4af_write_descriptor(ctx, 5, track->decSpecificInfoSize);
m4af_write(ctx, track->decSpecificInfo, track->decSpecificInfoSize); m4af_write(ctx, track->decSpecificInfo, track->decSpecificInfoSize);
@ -859,15 +866,14 @@ void m4af_write_sgpd_box(m4af_ctx_t *ctx, uint32_t track_idx)
{ {
m4af_track_t *track = &ctx->track[track_idx]; m4af_track_t *track = &ctx->track[track_idx];
m4af_write(ctx, m4af_write(ctx,
"\0\0\0\032" /* size */ "\0\0\0\026" /* size: 22 */
"sgpd" /* type */ "sgpd" /* type */
"\001" /* version */ "\0" /* version */
"\0\0\0" /* flags */ "\0\0\0" /* flags */
"roll" /* grouping_type */ "roll" /* grouping_type */
"\0\0\0\002" /* default_length: 2 */
"\0\0\0\001" /* entry_count: 1 */ "\0\0\0\001" /* entry_count: 1 */
"\377\377" /* payload_data: -1 */ "\377\377" /* payload_data: -1 */
, 26); , 22);
} }
static static

View File

@ -97,6 +97,8 @@ int m4af_write_sample(m4af_ctx_t *ctx, uint32_t track_idx, const void *data,
int m4af_set_decoder_specific_info(m4af_ctx_t *ctx, uint32_t track_idx, int m4af_set_decoder_specific_info(m4af_ctx_t *ctx, uint32_t track_idx,
uint8_t *data, uint32_t size); uint8_t *data, uint32_t size);
void m4af_set_vbr_mode(m4af_ctx_t *ctx, uint32_t track_idx, int is_vbr);
void m4af_set_priming(m4af_ctx_t *ctx, uint32_t track_idx, void m4af_set_priming(m4af_ctx_t *ctx, uint32_t track_idx,
uint32_t encoder_delay, uint32_t padding); uint32_t encoder_delay, uint32_t padding);

View File

@ -203,7 +203,9 @@ typedef struct aacenc_param_ex_t {
AACENC_PARAMS AACENC_PARAMS
char *input_filename; char *input_filename;
FILE *input_fp;
char *output_filename; char *output_filename;
FILE *output_fp;
unsigned gapless_mode; unsigned gapless_mode;
unsigned ignore_length; unsigned ignore_length;
int silent; int silent;
@ -487,13 +489,11 @@ int write_sample(FILE *ofp, m4af_ctx_t *m4af,
} }
static static
int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder, int encode(pcm_reader_t *reader, HANDLE_AACENCODER encoder,
uint32_t frame_length, FILE *ofp, m4af_ctx_t *m4af, uint32_t frame_length, FILE *ofp, m4af_ctx_t *m4af,
int show_progress) int show_progress)
{ {
uint8_t *ibuf = 0; int16_t *ibuf = 0;
int16_t *pcmbuf = 0;
uint32_t pcmsize = 0;
uint8_t *obuf = 0; uint8_t *obuf = 0;
uint32_t olen; uint32_t olen;
uint32_t osize = 0; uint32_t osize = 0;
@ -502,29 +502,23 @@ int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder,
int rc = -1; int rc = -1;
int frames_written = 0; int frames_written = 0;
aacenc_progress_t progress = { 0 }; aacenc_progress_t progress = { 0 };
const pcm_sample_description_t *format = wav_get_format(wavf); const pcm_sample_description_t *fmt = pcm_get_format(reader);
ibuf = malloc(frame_length * format->bytes_per_frame); ibuf = malloc(frame_length * fmt->bytes_per_frame);
aacenc_progress_init(&progress, wav_get_length(wavf), format->sample_rate); aacenc_progress_init(&progress, pcm_get_length(reader), fmt->sample_rate);
do { do {
if (g_interrupted) if (g_interrupted)
nread = 0; nread = 0;
else if (nread) { else if (nread) {
if ((nread = wav_read_frames(wavf, ibuf, frame_length)) < 0) { if ((nread = pcm_read_frames(reader, ibuf, frame_length)) < 0) {
fprintf(stderr, "ERROR: read failed\n"); fprintf(stderr, "ERROR: read failed\n");
goto END; goto END;
} else if (nread > 0) {
if (pcm_convert_to_native_sint16(format, ibuf, nread,
&pcmbuf, &pcmsize) < 0) {
fprintf(stderr, "ERROR: unsupported sample format\n");
goto END;
}
} }
if (show_progress) if (show_progress)
aacenc_progress_update(&progress, wav_get_position(wavf), aacenc_progress_update(&progress, pcm_get_position(reader),
format->sample_rate * 2); fmt->sample_rate * 2);
} }
if ((consumed = aac_encode_frame(encoder, format, pcmbuf, nread, if ((consumed = aac_encode_frame(encoder, fmt, ibuf, nread,
&obuf, &olen, &osize)) < 0) &obuf, &olen, &osize)) < 0)
goto END; goto END;
if (olen > 0) { if (olen > 0) {
@ -535,11 +529,10 @@ int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder,
} while (nread > 0 || olen > 0); } while (nread > 0 || olen > 0);
if (show_progress) if (show_progress)
aacenc_progress_finish(&progress, wav_get_position(wavf)); aacenc_progress_finish(&progress, pcm_get_position(reader));
rc = frames_written; rc = frames_written;
END: END:
if (ibuf) free(ibuf); if (ibuf) free(ibuf);
if (pcmbuf) free(pcmbuf);
if (obuf) free(obuf); if (obuf) free(obuf);
return rc; return rc;
} }
@ -650,25 +643,68 @@ int parse_raw_spec(const char *spec, pcm_sample_description_t *desc)
return 0; return 0;
} }
static
pcm_reader_t *open_input(aacenc_param_ex_t *params)
{
wav_io_context_t wav_io = {
read_callback, seek_callback, tell_callback
};
pcm_reader_t *reader = 0;
struct stat stb = { 0 };
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;
}
if (fstat(fileno(params->input_fp), &stb) == 0
&& (stb.st_mode & S_IFMT) != S_IFREG) {
wav_io.seek = 0;
wav_io.tell = 0;
}
if (params->is_raw) {
int bytes_per_channel;
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;
}
desc.sample_rate = params->raw_rate;
desc.channels_per_frame = params->raw_channels;
bytes_per_channel = (desc.bits_per_channel + 7) / 8;
desc.bytes_per_frame = params->raw_channels * bytes_per_channel;
if ((reader = raw_open(&wav_io, params->input_fp, &desc)) == 0) {
fprintf(stderr, "ERROR: failed to open raw input\n");
goto END;
}
} else {
if ((reader = wav_open(&wav_io, params->input_fp,
params->ignore_length)) == 0) {
fprintf(stderr, "ERROR: broken / unsupported input file\n");
goto END;
}
}
return pcm_open_sint16_converter(reader);
END:
return 0;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
wav_io_context_t wav_io = { read_callback, seek_callback, tell_callback }; static m4af_io_callbacks_t m4af_io = {
m4af_io_callbacks_t read_callback, write_callback, seek_callback, tell_callback
m4af_io = { read_callback, write_callback, seek_callback, tell_callback }; };
aacenc_param_ex_t params = { 0 }; aacenc_param_ex_t params = { 0 };
int result = 2; int result = 2;
FILE *ifp = 0;
FILE *ofp = 0;
char *output_filename = 0; char *output_filename = 0;
wav_reader_t *wavf = 0; pcm_reader_t *reader = 0;
HANDLE_AACENCODER encoder = 0; HANDLE_AACENCODER encoder = 0;
AACENC_InfoStruct aacinfo = { 0 }; AACENC_InfoStruct aacinfo = { 0 };
m4af_ctx_t *m4af = 0; m4af_ctx_t *m4af = 0;
const pcm_sample_description_t *sample_format; const pcm_sample_description_t *sample_format;
int downsampled_timescale = 0; int downsampled_timescale = 0;
int frame_count = 0; int frame_count = 0;
struct stat stb = { 0 };
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
setbuf(stderr, 0); setbuf(stderr, 0);
@ -676,37 +712,10 @@ int main(int argc, char **argv)
if (parse_options(argc, argv, &params) < 0) if (parse_options(argc, argv, &params) < 0)
return 1; return 1;
if ((ifp = aacenc_fopen(params.input_filename, "rb")) == 0) { if ((reader = open_input(&params)) == 0)
aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.input_filename,
strerror(errno));
goto END; goto END;
}
if (fstat(fileno(ifp), &stb) == 0 && (stb.st_mode & S_IFMT) != S_IFREG) { sample_format = pcm_get_format(reader);
wav_io.seek = 0;
wav_io.tell = 0;
}
if (!params.is_raw) {
if ((wavf = wav_open(&wav_io, ifp, params.ignore_length)) == 0) {
fprintf(stderr, "ERROR: broken / unsupported input file\n");
goto END;
}
} else {
int bytes_per_channel;
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;
}
desc.sample_rate = params.raw_rate;
desc.channels_per_frame = params.raw_channels;
bytes_per_channel = (desc.bits_per_channel + 7) / 8;
desc.bytes_per_frame = params.raw_channels * bytes_per_channel;
if ((wavf = raw_open(&wav_io, ifp, &desc)) == 0) {
fprintf(stderr, "ERROR: failed to open raw input\n");
goto END;
}
}
sample_format = wav_get_format(wavf);
if (aacenc_init(&encoder, (aacenc_param_t*)&params, sample_format, if (aacenc_init(&encoder, (aacenc_param_t*)&params, sample_format,
&aacinfo) < 0) &aacinfo) < 0)
@ -718,7 +727,7 @@ int main(int argc, char **argv)
params.output_filename = output_filename; params.output_filename = output_filename;
} }
if ((ofp = aacenc_fopen(params.output_filename, "wb+")) == 0) { if ((params.output_fp = aacenc_fopen(params.output_filename, "wb+")) == 0) {
aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.output_filename, aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.output_filename,
strerror(errno)); strerror(errno));
goto END; goto END;
@ -732,22 +741,24 @@ int main(int argc, char **argv)
if (sbr_mode && !sig_mode) if (sbr_mode && !sig_mode)
downsampled_timescale = 1; downsampled_timescale = 1;
scale = sample_format->sample_rate >> downsampled_timescale; scale = sample_format->sample_rate >> downsampled_timescale;
if ((m4af = m4af_create(M4AF_CODEC_MP4A, scale, &m4af_io, ofp)) < 0) if ((m4af = m4af_create(M4AF_CODEC_MP4A, scale, &m4af_io,
params.output_fp)) < 0)
goto END; goto END;
m4af_set_decoder_specific_info(m4af, 0, aacinfo.confBuf, m4af_set_decoder_specific_info(m4af, 0, aacinfo.confBuf,
aacinfo.confSize); aacinfo.confSize);
m4af_set_fixed_frame_duration(m4af, 0, m4af_set_fixed_frame_duration(m4af, 0,
framelen >> downsampled_timescale); framelen >> downsampled_timescale);
m4af_set_vbr_mode(m4af, 0, params.bitrate_mode);
m4af_set_priming_mode(m4af, params.gapless_mode + 1); m4af_set_priming_mode(m4af, params.gapless_mode + 1);
m4af_begin_write(m4af); m4af_begin_write(m4af);
} }
frame_count = encode(wavf, encoder, aacinfo.frameLength, ofp, m4af, frame_count = encode(reader, encoder, aacinfo.frameLength,
!params.silent); params.output_fp, m4af, !params.silent);
if (frame_count < 0) if (frame_count < 0)
goto END; goto END;
if (m4af) { if (m4af) {
uint32_t delay = aacinfo.encoderDelay; uint32_t delay = aacinfo.encoderDelay;
int64_t frames_read = wav_get_position(wavf); int64_t frames_read = pcm_get_position(reader);
uint32_t padding = frame_count * aacinfo.frameLength uint32_t padding = frame_count * aacinfo.frameLength
- frames_read - aacinfo.encoderDelay; - frames_read - aacinfo.encoderDelay;
m4af_set_priming(m4af, 0, delay >> downsampled_timescale, m4af_set_priming(m4af, 0, delay >> downsampled_timescale,
@ -757,10 +768,10 @@ int main(int argc, char **argv)
} }
result = 0; result = 0;
END: END:
if (wavf) wav_teardown(&wavf); if (reader) pcm_teardown(&reader);
if (ifp) fclose(ifp); if (params.input_fp) fclose(params.input_fp);
if (m4af) m4af_teardown(&m4af); if (m4af) m4af_teardown(&m4af);
if (ofp) fclose(ofp); if (params.output_fp) fclose(params.output_fp);
if (encoder) aacEncClose(&encoder); if (encoder) aacEncClose(&encoder);
if (output_filename) free(output_filename); if (output_filename) free(output_filename);
if (params.tags.tag_table) free(params.tags.tag_table); if (params.tags.tag_table) free(params.tags.tag_table);

52
src/pcm_reader.h Normal file
View File

@ -0,0 +1,52 @@
#ifndef PCM_READER_H
#define PCM_READER_H
#include "lpcm.h"
typedef struct pcm_reader_t pcm_reader_t;
typedef struct pcm_reader_vtbl_t {
const pcm_sample_description_t *(*get_format)(pcm_reader_t *);
int64_t (*get_length)(pcm_reader_t *);
int64_t (*get_position)(pcm_reader_t *);
int (*read_frames)(pcm_reader_t *, void *, unsigned);
void (*teardown)(pcm_reader_t **);
} pcm_reader_vtbl_t;
struct pcm_reader_t {
pcm_reader_vtbl_t *vtbl;
};
static inline
const pcm_sample_description_t *pcm_get_format(pcm_reader_t *r)
{
return r->vtbl->get_format(r);
}
static inline
int64_t pcm_get_length(pcm_reader_t *r)
{
return r->vtbl->get_length(r);
}
static inline
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);
}
static inline
void pcm_teardown(pcm_reader_t **r)
{
(*r)->vtbl->teardown(r);
}
pcm_reader_t *pcm_open_sint16_converter(pcm_reader_t *reader);
#endif

View File

@ -0,0 +1,90 @@
#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_sint16_converter_t {
pcm_reader_vtbl_t *vtbl;
pcm_reader_t *src;
pcm_sample_description_t format;
void *pivot;
unsigned capacity;
} pcm_sint16_converter_t;
static inline pcm_reader_t *get_source(pcm_reader_t *reader)
{
return ((pcm_sint16_converter_t *)reader)->src;
}
static const
pcm_sample_description_t *get_format(pcm_reader_t *reader)
{
return &((pcm_sint16_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_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;
self->pivot = p;
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;
return nframes;
}
static void teardown(pcm_reader_t **reader)
{
pcm_sint16_converter_t *self = (pcm_sint16_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_sint16_converter(pcm_reader_t *reader)
{
pcm_sint16_converter_t *self = 0;
pcm_sample_description_t *fmt;
if ((self = calloc(1, sizeof(pcm_sint16_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;
#if WORDS_BIGENDIAN
fmt->sample_type = PCM_TYPE_SINT_BE;
#else
fmt->sample_type = PCM_TYPE_SINT;
#endif
fmt->bits_per_channel = 16;
fmt->bytes_per_frame = 2 * fmt->channels_per_frame;
return (pcm_reader_t *)self;
}

View File

@ -35,6 +35,7 @@
} while (0) } while (0)
struct wav_reader_t { struct wav_reader_t {
pcm_reader_vtbl_t *vtbl;
pcm_sample_description_t sample_format; pcm_sample_description_t sample_format;
int64_t length; int64_t length;
int64_t position; int64_t position;
@ -52,22 +53,22 @@ static const uint8_t WAV_GUID_FLOAT[] = {
3, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71 3, 0, 0, 0, 0, 0, 0x10, 0, 0x80, 0, 0, 0xaa, 0, 0x38, 0x9b, 0x71
}; };
const pcm_sample_description_t *wav_get_format(wav_reader_t *reader) static const pcm_sample_description_t *wav_get_format(pcm_reader_t *reader)
{ {
return &reader->sample_format; return &((wav_reader_t *)reader)->sample_format;
} }
int64_t wav_get_length(wav_reader_t *reader) static int64_t wav_get_length(pcm_reader_t *reader)
{ {
return reader->length; return ((wav_reader_t *)reader)->length;
} }
int64_t wav_get_position(wav_reader_t *reader) static int64_t wav_get_position(pcm_reader_t *reader)
{ {
return reader->position; return ((wav_reader_t *)reader)->position;
} }
void wav_teardown(wav_reader_t **reader) static void wav_teardown(pcm_reader_t **reader)
{ {
free(*reader); free(*reader);
*reader = 0; *reader = 0;
@ -213,10 +214,12 @@ uint32_t riff_next_chunk(wav_reader_t *reader, uint32_t *chunk_size)
return 0; return 0;
} }
int wav_read_frames(wav_reader_t *reader, void *buffer, unsigned nframes) static
int wav_read_frames(pcm_reader_t *preader, void *buffer, unsigned nframes)
{ {
int rc; int rc;
unsigned nbytes; unsigned nbytes;
wav_reader_t *reader = (wav_reader_t *)preader;
if (!reader->ignore_length && nframes > reader->length - reader->position) if (!reader->ignore_length && nframes > reader->length - reader->position)
nframes = reader->length - reader->position; nframes = reader->length - reader->position;
@ -345,7 +348,15 @@ FAIL:
return -1; return -1;
} }
wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie, static pcm_reader_vtbl_t wav_vtable = {
wav_get_format,
wav_get_length,
wav_get_position,
wav_read_frames,
wav_teardown
};
pcm_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
int ignore_length) int ignore_length)
{ {
wav_reader_t *reader = 0; wav_reader_t *reader = 0;
@ -375,10 +386,11 @@ wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
reader->io.seek(reader->io_cookie, reader->data_offset, SEEK_SET); reader->io.seek(reader->io_cookie, reader->data_offset, SEEK_SET);
} }
} }
return reader; reader->vtbl = &wav_vtable;
return (pcm_reader_t *)reader;
} }
wav_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie, pcm_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie,
const pcm_sample_description_t *desc) const pcm_sample_description_t *desc)
{ {
wav_reader_t *reader = 0; wav_reader_t *reader = 0;
@ -397,7 +409,8 @@ wav_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie,
} }
} else } else
reader->length = INT64_MAX; reader->length = INT64_MAX;
return reader; reader->vtbl = &wav_vtable;
return (pcm_reader_t *)reader;
} }

View File

@ -6,6 +6,7 @@
#define WAV_READER_H #define WAV_READER_H
#include "lpcm.h" #include "lpcm.h"
#include "pcm_reader.h"
enum wav_error_code { enum wav_error_code {
WAV_IO_ERROR = 1, WAV_IO_ERROR = 1,
@ -26,14 +27,9 @@ typedef struct wav_io_context_t {
typedef struct wav_reader_t wav_reader_t; typedef struct wav_reader_t wav_reader_t;
wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie, pcm_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
int ignore_length); int ignore_length);
wav_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie, pcm_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie,
const pcm_sample_description_t *desc); const pcm_sample_description_t *desc);
const pcm_sample_description_t *wav_get_format(wav_reader_t *reader);
int wav_read_frames(wav_reader_t *reader, void *buffer, unsigned nframes);
int64_t wav_get_length(wav_reader_t *reader);
int64_t wav_get_position(wav_reader_t *reader);
void wav_teardown(wav_reader_t **reader);
#endif #endif

View File

@ -1,4 +1,4 @@
#ifndef VERSION_H #ifndef VERSION_H
#define VERSION_H #define VERSION_H
const char *fdkaac_version = "0.3.0"; const char *fdkaac_version = "0.3.3";
#endif #endif