mirror of
https://github.com/nu774/fdkaac.git
synced 2025-06-05 23:29:14 +02:00
Compare commits
54 Commits
Author | SHA1 | Date | |
---|---|---|---|
bd8c9a5d15 | |||
91ef87b610 | |||
29e0948097 | |||
e8e9f79eec | |||
2d744bd56c | |||
2f6fc566cc | |||
0c502d30e5 | |||
b87f2251a4 | |||
afe73f4916 | |||
fe2d3aa3e7 | |||
2f960ef8fd | |||
fb2b36350a | |||
f5363b23c3 | |||
9517c27537 | |||
d317e29d46 | |||
ffc230a83e | |||
70e912edba | |||
bd02d0e753 | |||
d6a8b9652a | |||
229c3ead72 | |||
4d060c0da0 | |||
1184a1f52b | |||
93fb917b75 | |||
cbb23cdbd8 | |||
721d977f59 | |||
e4bbeeb016 | |||
2642af896e | |||
7ce09815f7 | |||
adbd1aac0e | |||
fe4d497877 | |||
bd3b4b343a | |||
406f0e0f44 | |||
1170c9a610 | |||
a56831da9f | |||
aa2ca1e348 | |||
7b1f213618 | |||
5af13f7d79 | |||
5c534696a9 | |||
33e12bc4f5 | |||
bfb6aa3cb8 | |||
360cf7dc8b | |||
43ab433178 | |||
47c164c85a | |||
d52b8bbf13 | |||
f67cfe227e | |||
e476728e77 | |||
dac71de305 | |||
61b6a9e383 | |||
5888fddccf | |||
c5c459082a | |||
00799c5e44 | |||
6887939854 | |||
6854317606 | |||
8960a17764 |
206
ChangeLog
206
ChangeLog
@ -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]
|
||||
|
||||
|
@ -52,13 +52,20 @@
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<PreprocessorDefinitions>HAVE_STDINT_H;inline=__inline;_CRT_SECURE_NO_WARNINGS;WIN32;_CONSOLE;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../fdk-aac/libSYS/include;../fdk-aac/libAACenc/include;../missings;.;..</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>./include;../missings;.;..</AdditionalIncludeDirectories>
|
||||
<StringPooling>true</StringPooling>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
<PreBuildEvent>
|
||||
<Command>copy ..\fdk-aac\libAACdec\include\aacdecoder_lib.h include\fdk-aac\
|
||||
copy ..\fdk-aac\libAACenc\include\aacenc_lib.h include\fdk-aac\
|
||||
copy ..\fdk-aac\libSYS\include\FDK_Audio.h include\fdk-aac\
|
||||
copy ..\fdk-aac\libSYS\include\genericStds.h include\fdk-aac\
|
||||
copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ </Command>
|
||||
</PreBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
@ -69,7 +76,7 @@
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<PreprocessorDefinitions>HAVE_STDINT_H;inline=__inline;_CRT_SECURE_NO_WARNINGS;WIN32;_CONSOLE;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalIncludeDirectories>../fdk-aac/libSYS/include;../fdk-aac/libAACenc/include;../missings;.;..</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>./include;../missings;.;..</AdditionalIncludeDirectories>
|
||||
<StringPooling>true</StringPooling>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
@ -78,6 +85,13 @@
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
</Link>
|
||||
<PreBuildEvent>
|
||||
<Command>copy ..\fdk-aac\libAACdec\include\aacdecoder_lib.h include\fdk-aac\
|
||||
copy ..\fdk-aac\libAACenc\include\aacenc_lib.h include\fdk-aac\
|
||||
copy ..\fdk-aac\libSYS\include\FDK_Audio.h include\fdk-aac\
|
||||
copy ..\fdk-aac\libSYS\include\genericStds.h include\fdk-aac\
|
||||
copy ..\fdk-aac\libSYS\include\machine_type.h include\fdk-aac\ </Command>
|
||||
</PreBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\missings\getopt.c" />
|
||||
@ -86,6 +100,9 @@
|
||||
<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_sint16_converter.c" />
|
||||
<ClCompile Include="..\src\progress.c" />
|
||||
<ClCompile Include="..\src\wav_reader.c" />
|
||||
</ItemGroup>
|
||||
@ -96,6 +113,8 @@
|
||||
<ClInclude Include="..\src\lpcm.h" />
|
||||
<ClInclude Include="..\src\m4af.h" />
|
||||
<ClInclude Include="..\src\m4af_endian.h" />
|
||||
<ClInclude Include="..\src\metadata.h" />
|
||||
<ClInclude Include="..\src\parson.h" />
|
||||
<ClInclude Include="..\src\progress.h" />
|
||||
<ClInclude Include="..\src\wav_reader.h" />
|
||||
</ItemGroup>
|
||||
|
@ -56,6 +56,9 @@
|
||||
<ClInclude Include="..\src\m4af_endian.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\src\pcm_reader.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\src\progress.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
|
0
MSVC/include/fdk-aac/.gitkeep
Normal file
0
MSVC/include/fdk-aac/.gitkeep
Normal file
13
Makefile.am
13
Makefile.am
@ -4,11 +4,14 @@ AUTOMAKE_OPTIONS = subdir-objects
|
||||
bin_PROGRAMS = fdkaac
|
||||
|
||||
fdkaac_SOURCES = \
|
||||
src/aacenc.c \
|
||||
src/lpcm.c \
|
||||
src/m4af.c \
|
||||
src/main.c \
|
||||
src/progress.c \
|
||||
src/aacenc.c \
|
||||
src/lpcm.c \
|
||||
src/m4af.c \
|
||||
src/main.c \
|
||||
src/metadata.c \
|
||||
src/parson.c \
|
||||
src/pcm_sint16_converter.c \
|
||||
src/progress.c \
|
||||
src/wav_reader.c
|
||||
|
||||
fdkaac_LDADD = \
|
||||
|
82
README
82
README
@ -31,3 +31,85 @@ look like the following:
|
||||
+- src
|
||||
|
||||
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
|
||||
---------------
|
||||
Generic tagging options like --tag, --tag-from-file, --long-tag allows you
|
||||
to set arbitrary tags.
|
||||
Available tags and their fcc (four char code) for --tag and --tag-from-file
|
||||
can be found at http://code.google.com/p/mp4v2/wiki/iTunesMetadata
|
||||
|
||||
For tags such as Artist where first char of fcc is copyright sign,
|
||||
you can skip first char and just say like --tag="ART:Foo Bar" or
|
||||
--tag-from-file=lyr:/path/to/your/lyrics.txt
|
||||
|
||||
Currently, --tag-from-file just stores file contents into m4a without any
|
||||
character encoding / line terminater conversion.
|
||||
Therefore, only use UTF-8 (without BOM) when setting text tags by this option.
|
||||
|
||||
On the other hand, --tag / --long-tag (and other command line arguments) are
|
||||
converted from locale character encoding to UTF-8 on Posix environment.
|
||||
On Windows, command line arguments are always treated as Unicode.
|
||||
|
||||
Tagging using JSON
|
||||
------------------
|
||||
With --tag-from-json, fdkaac can read JSON file and set tags from it.
|
||||
By default, tags are assumed to be in the root object(dictionary) like this:
|
||||
|
||||
{
|
||||
"title": "No Expectations",
|
||||
"artist": "The Rolling Stones",
|
||||
"album": "Beggars Banquet",
|
||||
"track": 2
|
||||
}
|
||||
|
||||
In this case, you can simply specify the filename like:
|
||||
--tag-from-json=/path/to/json
|
||||
|
||||
If the object containing tags is placed somewhere else, you can optionally
|
||||
specify the path of the object with dotted notation.
|
||||
|
||||
{
|
||||
"format" : {
|
||||
"filename" : "Middle Curse.flac",
|
||||
"nb_streams" : 1,
|
||||
"format_name" : "flac",
|
||||
"format_long_name" : "raw FLAC",
|
||||
"start_time" : "N/A",
|
||||
"duration" : "216.146667",
|
||||
"size" : "11851007.000000",
|
||||
"bit_rate" : "438628.000000",
|
||||
"tags" : {
|
||||
"ALBUM" : "Scary World Theory",
|
||||
"ARTIST" : "Lali Puna",
|
||||
"DATE" : "2001",
|
||||
"DISCID" : "9208CC0A",
|
||||
"TITLE" : "Middle Curse",
|
||||
"TRACKTOTAL" : "10",
|
||||
"track" : "2"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
In this example, tags are placed under the object "format.tags".
|
||||
("format" is a child of the root, and "tags" is a child of the "format").
|
||||
In this case, you can say:
|
||||
--tag-from-json=/path/to/json?format.tags
|
||||
|
||||
For your information, ffprobe of ffmpeg project (or avprobe of libav) can
|
||||
output media information/metadata in json format like this.
|
||||
|
||||
Note that not all tags can be read/written this way.
|
||||
|
@ -31,8 +31,9 @@ AC_TYPE_UINT8_T
|
||||
AC_CHECK_TYPES([ptrdiff_t])
|
||||
|
||||
AC_SYS_LARGEFILE
|
||||
AC_CHECK_TYPES([struct __timeb64],[],[],[[#include <sys/timeb.h>]])
|
||||
AC_FUNC_FSEEKO
|
||||
AC_CHECK_FUNCS([gettimeofday nl_langinfo strdup])
|
||||
AC_CHECK_FUNCS([sigaction gettimeofday nl_langinfo _vscprintf fseeko64])
|
||||
AC_CHECK_FUNC(getopt_long)
|
||||
AM_CONDITIONAL([FDK_NO_GETOPT_LONG],[test "$ac_cv_func_getopt_long" != "yes"])
|
||||
AC_SEARCH_LIBS([aacEncOpen],[fdk-aac],[],[],[])
|
||||
|
44
git2changelog.py
Executable file
44
git2changelog.py
Executable 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))
|
@ -97,7 +97,7 @@ int aacenc_init(HANDLE_AACENCODER *encoder, const aacenc_param_t *params,
|
||||
}
|
||||
if (aacEncoder_SetParam(*encoder, AACENC_SIGNALING_MODE,
|
||||
params->sbr_signaling) != AACENC_OK) {
|
||||
fprintf(stderr, "ERROR: unsupported transport format\n");
|
||||
fprintf(stderr, "ERROR: failed to set SBR signaling mode\n");
|
||||
goto FAIL;
|
||||
}
|
||||
if (params->adts_crc_check)
|
||||
@ -142,7 +142,7 @@ int aac_encode_frame(HANDLE_AACENCODER encoder,
|
||||
unsigned channel_mode, obytes;
|
||||
|
||||
channel_mode = aacEncoder_GetParam(encoder, AACENC_CHANNELMODE);
|
||||
obytes = 6144 / 8 * channel_mode;
|
||||
obytes = 6144 / 8 * channel_mode + 7;
|
||||
if (!*output || *osize < obytes) {
|
||||
*osize = obytes;
|
||||
*output = realloc(*output, obytes);
|
||||
|
@ -6,7 +6,10 @@
|
||||
#define COMPAT_H
|
||||
|
||||
#ifndef HAVE_FSEEKO
|
||||
# if _MSC_VER >= 1400
|
||||
# if HAVE_FSEEKO64
|
||||
# define fseeko fseeko64
|
||||
# define ftello ftello64
|
||||
# elif _MSC_VER >= 1400
|
||||
# define fseeko _fseeki64
|
||||
# define ftello _ftelli64
|
||||
# else
|
||||
@ -20,5 +23,6 @@ FILE *aacenc_fopen(const char *name, const char *mode);
|
||||
void aacenc_getmainargs(int *argc, char ***argv);
|
||||
char *aacenc_to_utf8(const char *s);
|
||||
int aacenc_fprintf(FILE *fp, const char *fmt, ...);
|
||||
const char *aacenc_basename(const char *path);
|
||||
|
||||
#endif
|
||||
|
@ -48,6 +48,16 @@ int aacenc_fprintf(FILE *fp, const char *fmt, ...)
|
||||
return cnt;
|
||||
}
|
||||
|
||||
/*
|
||||
* Different from POSIX basename() when path ends with /.
|
||||
* Since we use this only for a regular file, the difference doesn't matter.
|
||||
*/
|
||||
const char *aacenc_basename(const char *path)
|
||||
{
|
||||
const char *p = strrchr(path, '/');
|
||||
return p ? p + 1: path;
|
||||
}
|
||||
|
||||
#ifndef HAVE_ICONV
|
||||
char *aacenc_to_utf8(const char *s)
|
||||
{
|
||||
|
@ -12,8 +12,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <assert.h>
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#include <share.h>
|
||||
#include <sys/timeb.h>
|
||||
#include "compat.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
@ -29,8 +31,13 @@ int __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, _startupinfo *);
|
||||
|
||||
int64_t aacenc_timer(void)
|
||||
{
|
||||
#if HAVE_STRUCT___TIMEB64
|
||||
struct __timeb64 tv;
|
||||
_ftime64(&tv);
|
||||
#else
|
||||
struct timeb tv;
|
||||
ftime(&tv);
|
||||
#endif
|
||||
return (int64_t)tv.time * 1000 + tv.millitm;
|
||||
}
|
||||
|
||||
@ -65,15 +72,29 @@ FILE *aacenc_fopen(const char *name, const char *mode)
|
||||
fp = (mode[0] == 'r') ? stdin : stdout;
|
||||
_setmode(_fileno(fp), _O_BINARY);
|
||||
} else {
|
||||
int share = _SH_DENYRW;
|
||||
if (strchr(mode, 'r') && !strchr(mode, '+'))
|
||||
share = _SH_DENYWR;
|
||||
codepage_decode_wchar(CP_UTF8, name, &wname);
|
||||
codepage_decode_wchar(CP_UTF8, mode, &wmode);
|
||||
fp = _wfopen(wname, wmode);
|
||||
fp = _wfsopen(wname, wmode, share);
|
||||
free(wname);
|
||||
free(wmode);
|
||||
}
|
||||
return fp;
|
||||
}
|
||||
|
||||
static char **__aacenc_argv__;
|
||||
|
||||
static
|
||||
void aacenc_free_mainargs(void)
|
||||
{
|
||||
char **p = __aacenc_argv__;
|
||||
for (; *p; ++p)
|
||||
free(*p);
|
||||
free(__aacenc_argv__);
|
||||
}
|
||||
|
||||
void aacenc_getmainargs(int *argc, char ***argv)
|
||||
{
|
||||
int i;
|
||||
@ -85,6 +106,8 @@ void aacenc_getmainargs(int *argc, char ***argv)
|
||||
for (i = 0; i < *argc; ++i)
|
||||
codepage_encode_wchar(CP_UTF8, wargv[i], &(*argv)[i]);
|
||||
(*argv)[*argc] = 0;
|
||||
__aacenc_argv__ = *argv;
|
||||
atexit(aacenc_free_mainargs);
|
||||
}
|
||||
|
||||
char *aacenc_to_utf8(const char *s)
|
||||
@ -92,6 +115,20 @@ char *aacenc_to_utf8(const char *s)
|
||||
return _strdup(s);
|
||||
}
|
||||
|
||||
#if defined(__MINGW32__) && !defined(HAVE__VSCPRINTF)
|
||||
int _vscprintf(const char *fmt, va_list ap)
|
||||
{
|
||||
static int (*fp_vscprintf)(const char *, va_list) = 0;
|
||||
if (!fp_vscprintf) {
|
||||
HANDLE h = GetModuleHandleA("msvcrt.dll");
|
||||
FARPROC fp = GetProcAddress(h, "_vscprintf");
|
||||
InterlockedCompareExchangePointer(&fp_vscprintf, fp, 0);
|
||||
}
|
||||
assert(fp_vscprintf);
|
||||
return fp_vscprintf(fmt, ap);
|
||||
}
|
||||
#endif
|
||||
|
||||
int aacenc_fprintf(FILE *fp, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
@ -126,3 +163,15 @@ int aacenc_fprintf(FILE *fp, const char *fmt, ...)
|
||||
return cnt;
|
||||
}
|
||||
|
||||
const char *aacenc_basename(const char *path)
|
||||
{
|
||||
/*
|
||||
* Since path is encoded with UTF-8, naive usage of strrchr() shoule be safe.
|
||||
*/
|
||||
const char *p = strrchr(path, '/');
|
||||
const char *q = strrchr(path, '\\');
|
||||
const char *r = strrchr(path, ':');
|
||||
if (q > p) p = q;
|
||||
if (r > p) p = r;
|
||||
return p ? p + 1 : path;
|
||||
}
|
||||
|
11
src/lpcm.c
11
src/lpcm.c
@ -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,
|
||||
const void *input, uint32_t nframes,
|
||||
int16_t **result, uint32_t *osize)
|
||||
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]); \
|
||||
result[i] = conv(ip[i]); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
@ -204,7 +204,7 @@ int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
|
||||
uint8_t *ip = (uint8_t *)input; \
|
||||
bytes_per_channel = PCM_BYTES_PER_CHANNEL(format); \
|
||||
for (i = 0; i < count; ++i) { \
|
||||
(*result)[i] = conv(ip); \
|
||||
result[i] = conv(ip); \
|
||||
ip += bytes_per_channel; \
|
||||
} \
|
||||
} 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;
|
||||
if (!count)
|
||||
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) {
|
||||
case 1 | PCM_TYPE_SINT<<4:
|
||||
CONVERT(int8_t, pcm_s8_to_s16); break;
|
||||
|
@ -33,5 +33,5 @@ typedef struct pcm_sample_description_t {
|
||||
|
||||
int pcm_convert_to_native_sint16(const pcm_sample_description_t *format,
|
||||
const void *input, uint32_t nframes,
|
||||
int16_t **result, uint32_t *osize);
|
||||
int16_t *result);
|
||||
#endif
|
||||
|
691
src/m4af.c
691
src/m4af.c
File diff suppressed because it is too large
Load Diff
77
src/m4af.h
77
src/m4af.h
@ -8,8 +8,10 @@
|
||||
#define M4AF_FOURCC(a,b,c,d) (((a)<<24)|((b)<<16)|((c)<<8)|(d))
|
||||
|
||||
enum m4af_error_code {
|
||||
M4AF_IO_ERROR = 1,
|
||||
M4AF_NO_MEMORY,
|
||||
M4AF_IO_ERROR = -1,
|
||||
M4AF_NO_MEMORY = -2,
|
||||
M4AF_FORMAT_ERROR = -3,
|
||||
M4AF_NOT_SUPPORTED = -4,
|
||||
};
|
||||
|
||||
enum m4af_itmf_tag {
|
||||
@ -50,65 +52,80 @@ enum m4af_codec_type {
|
||||
M4AF_CODEC_TEXT = M4AF_FOURCC('t','e','x','t'),
|
||||
};
|
||||
|
||||
enum m4af_priming_mode {
|
||||
M4AF_PRIMING_MODE_ITUNSMPB = 1,
|
||||
M4AF_PRIMING_MODE_EDTS = 2,
|
||||
M4AF_PRIMING_MODE_BOTH = 3
|
||||
};
|
||||
|
||||
typedef int (*m4af_read_callback)(void *cookie, void *buffer, uint32_t size);
|
||||
typedef int (*m4af_write_callback)(void *cookie, const void *data,
|
||||
uint32_t size);
|
||||
typedef int (*m4af_seek_callback)(void *cookie, int64_t off, int whence);
|
||||
typedef int64_t (*m4af_tell_callback)(void *cookie);
|
||||
|
||||
typedef struct m4af_io_callbacks_t {
|
||||
m4af_read_callback read;
|
||||
m4af_write_callback write;
|
||||
m4af_seek_callback seek;
|
||||
m4af_tell_callback tell;
|
||||
} m4af_io_callbacks_t;
|
||||
|
||||
typedef struct m4af_writer_t m4af_writer_t;
|
||||
typedef struct m4af_itmf_entry_t {
|
||||
uint32_t fcc;
|
||||
char *name;
|
||||
uint32_t type_code;
|
||||
char *data;
|
||||
uint32_t data_size;
|
||||
} m4af_itmf_entry_t;
|
||||
|
||||
m4af_writer_t *m4af_create(uint32_t codec, uint32_t timescale,
|
||||
m4af_io_callbacks_t *io, void *io_cookie);
|
||||
typedef struct m4af_ctx_t m4af_ctx_t;
|
||||
|
||||
void m4af_teardown(m4af_writer_t **ctx);
|
||||
|
||||
int m4af_begin_write(m4af_writer_t *ctx);
|
||||
m4af_ctx_t *m4af_create(uint32_t codec, uint32_t timescale,
|
||||
m4af_io_callbacks_t *io, void *io_cookie);
|
||||
|
||||
int m4af_finalize(m4af_writer_t *ctx);
|
||||
int m4af_begin_write(m4af_ctx_t *ctx);
|
||||
|
||||
/* can be called before m4af_write_sample() */
|
||||
void m4af_set_fixed_frame_duration(m4af_writer_t *ctx, int track_idx,
|
||||
uint32_t length);
|
||||
int m4af_finalize(m4af_ctx_t *ctx, int optimize);
|
||||
|
||||
/* can be called between mfa4_begin_write() and m4af_finalize() */
|
||||
int m4af_write_sample(m4af_writer_t *ctx, int track_idx, const void *data,
|
||||
void m4af_teardown(m4af_ctx_t **ctx);
|
||||
|
||||
int m4af_write_sample(m4af_ctx_t *ctx, uint32_t track_idx, const void *data,
|
||||
uint32_t size, uint32_t duration);
|
||||
|
||||
int m4af_set_decoder_specific_info(m4af_ctx_t *ctx, uint32_t track_idx,
|
||||
uint8_t *data, uint32_t size);
|
||||
|
||||
/* the following can be called at anytime before m4af_finalize() */
|
||||
void m4af_set_vbr_mode(m4af_ctx_t *ctx, uint32_t track_idx, int is_vbr);
|
||||
|
||||
int m4af_decoder_specific_info(m4af_writer_t *ctx, int track_idx,
|
||||
uint8_t *data, uint32_t size);
|
||||
|
||||
void m4af_set_priming(m4af_writer_t *ctx, int track_idx,
|
||||
void m4af_set_priming(m4af_ctx_t *ctx, uint32_t track_idx,
|
||||
uint32_t encoder_delay, uint32_t padding);
|
||||
|
||||
int m4af_add_itmf_long_tag(m4af_writer_t *ctx, const char *name,
|
||||
void m4af_set_priming_mode(m4af_ctx_t *ctx, int mode);
|
||||
|
||||
void m4af_set_fixed_frame_duration(m4af_ctx_t *ctx, uint32_t track_idx,
|
||||
uint32_t length);
|
||||
|
||||
int m4af_add_itmf_long_tag(m4af_ctx_t *ctx, const char *name,
|
||||
const char *data);
|
||||
|
||||
int m4af_add_itmf_short_tag(m4af_writer_t *ctx, uint32_t type,
|
||||
uint32_t type_code, const void *data,
|
||||
uint32_t data_size);
|
||||
int m4af_add_itmf_short_tag(m4af_ctx_t *ctx, uint32_t fcc, uint32_t type_code,
|
||||
const void *data, uint32_t data_size);
|
||||
|
||||
int m4af_add_itmf_string_tag(m4af_writer_t *ctx, uint32_t type,
|
||||
const char *data);
|
||||
int m4af_add_itmf_string_tag(m4af_ctx_t *ctx, uint32_t fcc, const char *data);
|
||||
|
||||
int m4af_add_itmf_int8_tag(m4af_writer_t *ctx, uint32_t type, int value);
|
||||
int m4af_add_itmf_int8_tag(m4af_ctx_t *ctx, uint32_t fcc, int value);
|
||||
|
||||
int m4af_add_itmf_int16_tag(m4af_writer_t *ctx, uint32_t type, int value);
|
||||
int m4af_add_itmf_int16_tag(m4af_ctx_t *ctx, uint32_t fcc, int value);
|
||||
|
||||
int m4af_add_itmf_int32_tag(m4af_writer_t *ctx, uint32_t type, int value);
|
||||
int m4af_add_itmf_int32_tag(m4af_ctx_t *ctx, uint32_t fcc, uint32_t value);
|
||||
|
||||
int m4af_add_itmf_track_tag(m4af_writer_t *ctx, int track, int total);
|
||||
int m4af_add_itmf_int64_tag(m4af_ctx_t *ctx, uint32_t fcc, uint64_t value);
|
||||
|
||||
int m4af_add_itmf_disk_tag(m4af_writer_t *ctx, int disk, int total);
|
||||
int m4af_add_itmf_track_tag(m4af_ctx_t *ctx, int track, int total);
|
||||
|
||||
int m4af_add_itmf_genre_tag(m4af_writer_t *ctx, int genre);
|
||||
int m4af_add_itmf_disk_tag(m4af_ctx_t *ctx, int disk, int total);
|
||||
|
||||
int m4af_add_itmf_genre_tag(m4af_ctx_t *ctx, int genre);
|
||||
#endif
|
||||
|
538
src/main.c
538
src/main.c
@ -8,9 +8,15 @@
|
||||
#if HAVE_STDINT_H
|
||||
# include <stdint.h>
|
||||
#endif
|
||||
#if HAVE_INTTYPES_H
|
||||
# include <inttypes.h>
|
||||
#elif defined(_MSC_VER)
|
||||
# define SCNd64 "I64d"
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <locale.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
@ -18,8 +24,13 @@
|
||||
#if HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#if HAVE_SIGACTION
|
||||
#include <signal.h>
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#endif
|
||||
#include "compat.h"
|
||||
#include "wav_reader.h"
|
||||
@ -27,9 +38,44 @@
|
||||
#include "m4af.h"
|
||||
#include "progress.h"
|
||||
#include "version.h"
|
||||
#include "metadata.h"
|
||||
|
||||
#define PROGNAME "fdkaac"
|
||||
|
||||
static volatile int g_interrupted = 0;
|
||||
|
||||
#if HAVE_SIGACTION
|
||||
static void signal_handler(int signum)
|
||||
{
|
||||
g_interrupted = 1;
|
||||
}
|
||||
static void handle_signals(void)
|
||||
{
|
||||
int i, sigs[] = { SIGINT, SIGHUP, SIGTERM };
|
||||
for (i = 0; i < sizeof(sigs)/sizeof(sigs[0]); ++i) {
|
||||
struct sigaction sa = { 0 };
|
||||
sa.sa_handler = signal_handler;
|
||||
sa.sa_flags |= SA_RESTART;
|
||||
sigaction(sigs[i], &sa, 0);
|
||||
}
|
||||
}
|
||||
#elif defined(_WIN32)
|
||||
static BOOL WINAPI signal_handler(DWORD type)
|
||||
{
|
||||
g_interrupted = 1;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void handle_signals(void)
|
||||
{
|
||||
SetConsoleCtrlHandler(signal_handler, TRUE);
|
||||
}
|
||||
#else
|
||||
static void handle_signals(void)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
static
|
||||
int read_callback(void *cookie, void *data, uint32_t size)
|
||||
{
|
||||
@ -97,12 +143,32 @@ PROGNAME " %s\n"
|
||||
" 6: LATM MCP=1\n"
|
||||
" 7: LATM MCP=0\n"
|
||||
" 10: LOAS/LATM (LATM within LOAS)\n"
|
||||
" -c, --adts-crc-check Add CRC protection on ADTS header\n"
|
||||
" -C, --adts-crc-check Add CRC protection on ADTS header\n"
|
||||
" -h, --header-period <n> StreamMuxConfig/PCE repetition period in\n"
|
||||
" transport layer\n"
|
||||
"\n"
|
||||
" -o <filename> Output filename\n"
|
||||
" --ignore-length Ignore length of WAV header\n"
|
||||
" -G, --gapless-mode <n> Encoder delay signaling for gapless playback\n"
|
||||
" 0: iTunSMPB (default)\n"
|
||||
" 1: ISO standard (edts + sgpd)\n"
|
||||
" 2: Both\n"
|
||||
" --ignorelength Ignore length of WAV header\n"
|
||||
" -S, --silent Don't print progress messages\n"
|
||||
" --moov-before-mdat Place moov box before mdat box on m4a output\n"
|
||||
"\n"
|
||||
"Options for raw (headerless) input:\n"
|
||||
" -R, --raw Treat input as raw (by default WAV is\n"
|
||||
" assumed)\n"
|
||||
" --raw-channels <n> Number of channels (default: 2)\n"
|
||||
" --raw-rate <n> Sample rate (default: 44100)\n"
|
||||
" --raw-format <spec> Sample format, default is \"S16L\".\n"
|
||||
" Spec is as follows:\n"
|
||||
" 1st char: S(igned)|U(nsigned)|F(loat)\n"
|
||||
" 2nd part: bits per channel\n"
|
||||
" Last char: L(ittle)|B(ig)\n"
|
||||
" Last char can be omitted, in which case L is\n"
|
||||
" assumed. Spec is case insensitive, therefore\n"
|
||||
" \"u16b\" is same as \"U16B\".\n"
|
||||
"\n"
|
||||
"Tagging options:\n"
|
||||
" --title <string>\n"
|
||||
@ -117,24 +183,42 @@ PROGNAME " %s\n"
|
||||
" --track <number[/total]>\n"
|
||||
" --disk <number[/total]>\n"
|
||||
" --tempo <n>\n"
|
||||
" --tag <fcc>:<value> Set iTunes predefined tag with four char code.\n"
|
||||
" --tag-from-file <fcc>:<filename>\n"
|
||||
" Same as above, but value is read from file.\n"
|
||||
" --long-tag <name>:<value> Set arbitrary tag as iTunes custom metadata.\n"
|
||||
" --tag-from-json <filename[?dot_notation]>\n"
|
||||
" Read tags from JSON. By default, tags are\n"
|
||||
" assumed to be direct children of the root\n"
|
||||
" object(dictionary).\n"
|
||||
" Optionally, position of the dictionary\n"
|
||||
" that contains tags can be specified with\n"
|
||||
" dotted notation.\n"
|
||||
" Example:\n"
|
||||
" --tag-from-json /path/to/json?format.tags\n"
|
||||
, fdkaac_version);
|
||||
}
|
||||
|
||||
typedef struct aacenc_tag_entry_t {
|
||||
uint32_t tag;
|
||||
const char *data;
|
||||
} aacenc_tag_entry_t;
|
||||
|
||||
typedef struct aacenc_param_ex_t {
|
||||
AACENC_PARAMS
|
||||
|
||||
char *input_filename;
|
||||
FILE *input_fp;
|
||||
char *output_filename;
|
||||
FILE *output_fp;
|
||||
unsigned gapless_mode;
|
||||
unsigned ignore_length;
|
||||
int silent;
|
||||
int moov_before_mdat;
|
||||
|
||||
aacenc_tag_entry_t *tag_table;
|
||||
unsigned tag_count;
|
||||
unsigned tag_table_capacity;
|
||||
int is_raw;
|
||||
unsigned raw_channels;
|
||||
unsigned raw_rate;
|
||||
const char *raw_format;
|
||||
|
||||
aacenc_tag_param_t tags;
|
||||
|
||||
char *json_filename;
|
||||
} aacenc_param_ex_t;
|
||||
|
||||
static
|
||||
@ -142,7 +226,15 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
|
||||
{
|
||||
int ch;
|
||||
unsigned n;
|
||||
aacenc_tag_entry_t *tag;
|
||||
|
||||
#define OPT_MOOV_BEFORE_MDAT M4AF_FOURCC('m','o','o','v')
|
||||
#define OPT_RAW_CHANNELS M4AF_FOURCC('r','c','h','n')
|
||||
#define OPT_RAW_RATE M4AF_FOURCC('r','r','a','t')
|
||||
#define OPT_RAW_FORMAT M4AF_FOURCC('r','f','m','t')
|
||||
#define OPT_SHORT_TAG M4AF_FOURCC('s','t','a','g')
|
||||
#define OPT_SHORT_TAG_FILE M4AF_FOURCC('s','t','g','f')
|
||||
#define OPT_LONG_TAG M4AF_FOURCC('l','t','a','g')
|
||||
#define OPT_TAG_FROM_JSON M4AF_FOURCC('t','f','j','s')
|
||||
|
||||
static struct option long_options[] = {
|
||||
{ "help", no_argument, 0, 'h' },
|
||||
@ -154,28 +246,41 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
|
||||
{ "lowdelay-sbr", no_argument, 0, 'L' },
|
||||
{ "sbr-signaling", required_argument, 0, 's' },
|
||||
{ "transport-format", required_argument, 0, 'f' },
|
||||
{ "adts-crc-check", no_argument, 0, 'c' },
|
||||
{ "adts-crc-check", no_argument, 0, 'C' },
|
||||
{ "header-period", required_argument, 0, 'P' },
|
||||
|
||||
{ "ignore-length", no_argument, 0, 'I' },
|
||||
{ "gapless-mode", required_argument, 0, 'G' },
|
||||
{ "ignorelength", no_argument, 0, 'I' },
|
||||
{ "silent", no_argument, 0, 'S' },
|
||||
{ "moov-before-mdat", no_argument, 0, OPT_MOOV_BEFORE_MDAT },
|
||||
|
||||
{ "title", required_argument, 0, M4AF_TAG_TITLE },
|
||||
{ "artist", required_argument, 0, M4AF_TAG_ARTIST },
|
||||
{ "album", required_argument, 0, M4AF_TAG_ALBUM },
|
||||
{ "genre", required_argument, 0, M4AF_TAG_GENRE },
|
||||
{ "date", required_argument, 0, M4AF_TAG_DATE },
|
||||
{ "composer", required_argument, 0, M4AF_TAG_COMPOSER },
|
||||
{ "grouping", required_argument, 0, M4AF_TAG_GROUPING },
|
||||
{ "comment", required_argument, 0, M4AF_TAG_COMMENT },
|
||||
{ "album-artist", required_argument, 0, M4AF_TAG_ALBUM_ARTIST },
|
||||
{ "track", required_argument, 0, M4AF_TAG_TRACK },
|
||||
{ "disk", required_argument, 0, M4AF_TAG_DISK },
|
||||
{ "tempo", required_argument, 0, M4AF_TAG_TEMPO },
|
||||
{ "raw", no_argument, 0, 'R' },
|
||||
{ "raw-channels", required_argument, 0, OPT_RAW_CHANNELS },
|
||||
{ "raw-rate", required_argument, 0, OPT_RAW_RATE },
|
||||
{ "raw-format", required_argument, 0, OPT_RAW_FORMAT },
|
||||
|
||||
{ "title", required_argument, 0, M4AF_TAG_TITLE },
|
||||
{ "artist", required_argument, 0, M4AF_TAG_ARTIST },
|
||||
{ "album", required_argument, 0, M4AF_TAG_ALBUM },
|
||||
{ "genre", required_argument, 0, M4AF_TAG_GENRE },
|
||||
{ "date", required_argument, 0, M4AF_TAG_DATE },
|
||||
{ "composer", required_argument, 0, M4AF_TAG_COMPOSER },
|
||||
{ "grouping", required_argument, 0, M4AF_TAG_GROUPING },
|
||||
{ "comment", required_argument, 0, M4AF_TAG_COMMENT },
|
||||
{ "album-artist", required_argument, 0, M4AF_TAG_ALBUM_ARTIST },
|
||||
{ "track", required_argument, 0, M4AF_TAG_TRACK },
|
||||
{ "disk", required_argument, 0, M4AF_TAG_DISK },
|
||||
{ "tempo", required_argument, 0, M4AF_TAG_TEMPO },
|
||||
{ "tag", required_argument, 0, OPT_SHORT_TAG },
|
||||
{ "tag-from-file", required_argument, 0, OPT_SHORT_TAG_FILE },
|
||||
{ "long-tag", required_argument, 0, OPT_LONG_TAG },
|
||||
{ "tag-from-json", required_argument, 0, OPT_TAG_FROM_JSON },
|
||||
{ 0, 0, 0, 0 },
|
||||
};
|
||||
params->afterburner = 1;
|
||||
|
||||
aacenc_getmainargs(&argc, &argv);
|
||||
while ((ch = getopt_long(argc, argv, "hp:b:m:w:a:Ls:f:cP:Io:",
|
||||
while ((ch = getopt_long(argc, argv, "hp:b:m:w:a:Ls:f:CP:G:Io:SR",
|
||||
long_options, 0)) != EOF) {
|
||||
switch (ch) {
|
||||
case 'h':
|
||||
@ -232,7 +337,7 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
|
||||
}
|
||||
params->transport_format = n;
|
||||
break;
|
||||
case 'c':
|
||||
case 'C':
|
||||
params->adts_crc_check = 1;
|
||||
break;
|
||||
case 'P':
|
||||
@ -245,9 +350,42 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
|
||||
case 'o':
|
||||
params->output_filename = optarg;
|
||||
break;
|
||||
case 'G':
|
||||
if (sscanf(optarg, "%u", &n) != 1 || n > 2) {
|
||||
fprintf(stderr, "invalid arg for gapless-mode\n");
|
||||
return -1;
|
||||
}
|
||||
params->gapless_mode = n;
|
||||
break;
|
||||
case 'I':
|
||||
params->ignore_length = 1;
|
||||
break;
|
||||
case 'S':
|
||||
params->silent = 1;
|
||||
break;
|
||||
case OPT_MOOV_BEFORE_MDAT:
|
||||
params->moov_before_mdat = 1;
|
||||
break;
|
||||
case 'R':
|
||||
params->is_raw = 1;
|
||||
break;
|
||||
case OPT_RAW_CHANNELS:
|
||||
if (sscanf(optarg, "%u", &n) != 1) {
|
||||
fprintf(stderr, "invalid arg for raw-channels\n");
|
||||
return -1;
|
||||
}
|
||||
params->raw_channels = n;
|
||||
break;
|
||||
case OPT_RAW_RATE:
|
||||
if (sscanf(optarg, "%u", &n) != 1) {
|
||||
fprintf(stderr, "invalid arg for raw-rate\n");
|
||||
return -1;
|
||||
}
|
||||
params->raw_rate = n;
|
||||
break;
|
||||
case OPT_RAW_FORMAT:
|
||||
params->raw_format = optarg;
|
||||
break;
|
||||
case M4AF_TAG_TITLE:
|
||||
case M4AF_TAG_ARTIST:
|
||||
case M4AF_TAG_ALBUM:
|
||||
@ -260,18 +398,47 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
|
||||
case M4AF_TAG_TRACK:
|
||||
case M4AF_TAG_DISK:
|
||||
case M4AF_TAG_TEMPO:
|
||||
if (params->tag_count == params->tag_table_capacity) {
|
||||
unsigned newsize = params->tag_table_capacity;
|
||||
newsize = newsize ? newsize * 2 : 1;
|
||||
params->tag_table =
|
||||
realloc(params->tag_table,
|
||||
newsize * sizeof(aacenc_tag_entry_t));
|
||||
params->tag_table_capacity = newsize;
|
||||
aacenc_param_add_itmf_entry(¶ms->tags, ch, 0, optarg,
|
||||
strlen(optarg), 0);
|
||||
break;
|
||||
case OPT_SHORT_TAG:
|
||||
case OPT_SHORT_TAG_FILE:
|
||||
case OPT_LONG_TAG:
|
||||
{
|
||||
char *val;
|
||||
size_t klen;
|
||||
unsigned fcc = M4AF_FOURCC('-','-','-','-');
|
||||
|
||||
if ((val = strchr(optarg, ':')) == 0) {
|
||||
fprintf(stderr, "invalid arg for tag\n");
|
||||
return -1;
|
||||
}
|
||||
*val++ = '\0';
|
||||
if (ch == OPT_SHORT_TAG || ch == OPT_SHORT_TAG_FILE) {
|
||||
/*
|
||||
* take care of U+00A9(COPYRIGHT SIGN).
|
||||
* 1) if length of fcc is 3, we prepend '\xa9'.
|
||||
* 2) U+00A9 becomes "\xc2\xa9" in UTF-8. Therefore
|
||||
* we remove first '\xc2'.
|
||||
*/
|
||||
if (optarg[0] == '\xc2')
|
||||
++optarg;
|
||||
if ((klen = strlen(optarg))== 3)
|
||||
fcc = 0xa9;
|
||||
else if (klen != 4) {
|
||||
fprintf(stderr, "invalid arg for tag\n");
|
||||
return -1;
|
||||
}
|
||||
for (; *optarg; ++optarg)
|
||||
fcc = ((fcc << 8) | (*optarg & 0xff));
|
||||
}
|
||||
aacenc_param_add_itmf_entry(¶ms->tags, fcc, optarg,
|
||||
val, strlen(val),
|
||||
ch == OPT_SHORT_TAG_FILE);
|
||||
}
|
||||
tag = params->tag_table + params->tag_count;
|
||||
tag->tag = ch;
|
||||
tag->data = optarg;
|
||||
params->tag_count++;
|
||||
break;
|
||||
case OPT_TAG_FROM_JSON:
|
||||
params->json_filename = optarg;
|
||||
break;
|
||||
default:
|
||||
return usage(), -1;
|
||||
@ -279,6 +446,7 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
|
||||
}
|
||||
if (argc == optind)
|
||||
return usage(), -1;
|
||||
|
||||
if (!params->bitrate && !params->bitrate_mode) {
|
||||
fprintf(stderr, "bitrate or bitrate-mode is mandatory\n");
|
||||
return -1;
|
||||
@ -290,12 +458,21 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
|
||||
}
|
||||
if (params->bitrate && params->bitrate < 10000)
|
||||
params->bitrate *= 1000;
|
||||
|
||||
if (params->is_raw) {
|
||||
if (!params->raw_channels)
|
||||
params->raw_channels = 2;
|
||||
if (!params->raw_rate)
|
||||
params->raw_rate = 44100;
|
||||
if (!params->raw_format)
|
||||
params->raw_format = "S16L";
|
||||
}
|
||||
params->input_filename = argv[optind];
|
||||
return 0;
|
||||
};
|
||||
|
||||
static
|
||||
int write_sample(FILE *ofp, m4af_writer_t *m4af,
|
||||
int write_sample(FILE *ofp, m4af_ctx_t *m4af,
|
||||
const void *data, uint32_t size, uint32_t duration)
|
||||
{
|
||||
if (!m4af) {
|
||||
@ -312,12 +489,11 @@ int write_sample(FILE *ofp, m4af_writer_t *m4af,
|
||||
}
|
||||
|
||||
static
|
||||
int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder,
|
||||
uint32_t frame_length, FILE *ofp, m4af_writer_t *m4af)
|
||||
int encode(pcm_reader_t *reader, HANDLE_AACENCODER encoder,
|
||||
uint32_t frame_length, FILE *ofp, m4af_ctx_t *m4af,
|
||||
int show_progress)
|
||||
{
|
||||
uint8_t *ibuf = 0;
|
||||
int16_t *pcmbuf = 0;
|
||||
uint32_t pcmsize = 0;
|
||||
int16_t *ibuf = 0;
|
||||
uint8_t *obuf = 0;
|
||||
uint32_t olen;
|
||||
uint32_t osize = 0;
|
||||
@ -326,26 +502,23 @@ int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder,
|
||||
int rc = -1;
|
||||
int frames_written = 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);
|
||||
aacenc_progress_init(&progress, wav_get_length(wavf), format->sample_rate);
|
||||
ibuf = malloc(frame_length * fmt->bytes_per_frame);
|
||||
aacenc_progress_init(&progress, pcm_get_length(reader), fmt->sample_rate);
|
||||
do {
|
||||
if (nread) {
|
||||
if ((nread = wav_read_frames(wavf, ibuf, frame_length)) < 0) {
|
||||
if (g_interrupted)
|
||||
nread = 0;
|
||||
else if (nread) {
|
||||
if ((nread = pcm_read_frames(reader, ibuf, frame_length)) < 0) {
|
||||
fprintf(stderr, "ERROR: read failed\n");
|
||||
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;
|
||||
}
|
||||
}
|
||||
aacenc_progress_update(&progress, wav_get_position(wavf),
|
||||
format->sample_rate * 2);
|
||||
if (show_progress)
|
||||
aacenc_progress_update(&progress, pcm_get_position(reader),
|
||||
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)
|
||||
goto END;
|
||||
if (olen > 0) {
|
||||
@ -354,94 +527,66 @@ int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder,
|
||||
++frames_written;
|
||||
}
|
||||
} while (nread > 0 || olen > 0);
|
||||
aacenc_progress_finish(&progress, wav_get_position(wavf));
|
||||
|
||||
if (show_progress)
|
||||
aacenc_progress_finish(&progress, pcm_get_position(reader));
|
||||
rc = frames_written;
|
||||
END:
|
||||
if (ibuf) free(ibuf);
|
||||
if (pcmbuf) free(pcmbuf);
|
||||
if (obuf) free(obuf);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static
|
||||
int finalize_m4a(m4af_writer_t *m4af, const aacenc_param_ex_t *params,
|
||||
void put_tool_tag(m4af_ctx_t *m4af, const aacenc_param_ex_t *params,
|
||||
HANDLE_AACENCODER encoder)
|
||||
{
|
||||
char tool_info[256];
|
||||
char *p = tool_info;
|
||||
LIB_INFO *lib_info = 0;
|
||||
|
||||
p += sprintf(p, PROGNAME " %s, ", fdkaac_version);
|
||||
|
||||
lib_info = calloc(FDK_MODULE_LAST, sizeof(LIB_INFO));
|
||||
if (aacEncGetLibInfo(lib_info) == AACENC_OK) {
|
||||
int i;
|
||||
for (i = 0; i < FDK_MODULE_LAST; ++i)
|
||||
if (lib_info[i].module_id == FDK_AACENC)
|
||||
break;
|
||||
p += sprintf(p, "libfdk-aac %s, ", lib_info[i].versionStr);
|
||||
}
|
||||
free(lib_info);
|
||||
if (params->bitrate_mode)
|
||||
sprintf(p, "VBR mode %d", params->bitrate_mode);
|
||||
else
|
||||
sprintf(p, "CBR %dkbps",
|
||||
aacEncoder_GetParam(encoder, AACENC_BITRATE) / 1000);
|
||||
|
||||
m4af_add_itmf_string_tag(m4af, M4AF_TAG_TOOL, tool_info);
|
||||
}
|
||||
|
||||
static
|
||||
int finalize_m4a(m4af_ctx_t *m4af, const aacenc_param_ex_t *params,
|
||||
HANDLE_AACENCODER encoder)
|
||||
{
|
||||
unsigned i;
|
||||
aacenc_tag_entry_t *tag = params->tag_table;
|
||||
aacenc_tag_entry_t *tag = params->tags.tag_table;
|
||||
|
||||
for (i = 0; i < params->tag_count; ++i, ++tag) {
|
||||
switch (tag->tag) {
|
||||
case M4AF_TAG_TRACK:
|
||||
{
|
||||
unsigned m, n = 0;
|
||||
if (sscanf(tag->data, "%u/%u", &m, &n) >= 1)
|
||||
m4af_add_itmf_track_tag(m4af, m, n);
|
||||
break;
|
||||
}
|
||||
case M4AF_TAG_DISK:
|
||||
{
|
||||
unsigned m, n = 0;
|
||||
if (sscanf(tag->data, "%u/%u", &m, &n) >= 1)
|
||||
m4af_add_itmf_disk_tag(m4af, m, n);
|
||||
break;
|
||||
}
|
||||
case M4AF_TAG_TEMPO:
|
||||
{
|
||||
unsigned n;
|
||||
if (sscanf(tag->data, "%u", &n) == 1)
|
||||
m4af_add_itmf_int16_tag(m4af, tag->tag, n);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
char *u8 = aacenc_to_utf8(tag->data);
|
||||
m4af_add_itmf_string_tag(m4af, tag->tag, u8);
|
||||
free(u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
char tool_info[256];
|
||||
char *p = tool_info;
|
||||
LIB_INFO *lib_info = 0;
|
||||
if (params->json_filename)
|
||||
aacenc_put_tags_from_json(m4af, params->json_filename);
|
||||
|
||||
p += sprintf(p, PROGNAME " %s, ", fdkaac_version);
|
||||
for (i = 0; i < params->tags.tag_count; ++i, ++tag)
|
||||
aacenc_put_tag_entry(m4af, tag);
|
||||
|
||||
lib_info = calloc(FDK_MODULE_LAST, sizeof(LIB_INFO));
|
||||
if (aacEncGetLibInfo(lib_info) == AACENC_OK) {
|
||||
for (i = 0; i < FDK_MODULE_LAST; ++i)
|
||||
if (lib_info[i].module_id == FDK_AACENC)
|
||||
break;
|
||||
p += sprintf(p, "libfdk-aac %s, ", lib_info[i].versionStr);
|
||||
}
|
||||
free(lib_info);
|
||||
if (params->bitrate_mode)
|
||||
sprintf(p, "VBR mode %d", params->bitrate_mode);
|
||||
else
|
||||
sprintf(p, "CBR %dkbps",
|
||||
aacEncoder_GetParam(encoder, AACENC_BITRATE) / 1000);
|
||||
put_tool_tag(m4af, params, encoder);
|
||||
|
||||
m4af_add_itmf_string_tag(m4af, M4AF_TAG_TOOL, tool_info);
|
||||
}
|
||||
if (m4af_finalize(m4af) < 0) {
|
||||
if (m4af_finalize(m4af, params->moov_before_mdat) < 0) {
|
||||
fprintf(stderr, "ERROR: failed to finalize m4a\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static
|
||||
const char *basename(const char *filename)
|
||||
{
|
||||
char *p = strrchr(filename, '/');
|
||||
#ifdef _WIN32
|
||||
char *q = strrchr(filename, '\\');
|
||||
if (p < q) p = q;
|
||||
#endif
|
||||
return p ? p + 1 : filename;
|
||||
}
|
||||
|
||||
static
|
||||
char *generate_output_filename(const char *filename, const char *ext)
|
||||
{
|
||||
@ -452,7 +597,7 @@ char *generate_output_filename(const char *filename, const char *ext)
|
||||
p = malloc(ext_len + 6);
|
||||
sprintf(p, "stdin%s", ext);
|
||||
} else {
|
||||
const char *base = basename(filename);
|
||||
const char *base = aacenc_basename(filename);
|
||||
size_t ilen = strlen(base);
|
||||
const char *ext_org = strrchr(base, '.');
|
||||
if (ext_org) ilen = ext_org - base;
|
||||
@ -462,25 +607,104 @@ char *generate_output_filename(const char *filename, const char *ext)
|
||||
return p;
|
||||
}
|
||||
|
||||
static
|
||||
int parse_raw_spec(const char *spec, pcm_sample_description_t *desc)
|
||||
{
|
||||
unsigned bits;
|
||||
unsigned char c_type, c_endian = 'L';
|
||||
int type;
|
||||
|
||||
if (sscanf(spec, "%c%u%c", &c_type, &bits, &c_endian) < 2)
|
||||
return -1;
|
||||
c_type = toupper(c_type);
|
||||
c_endian = toupper(c_endian);
|
||||
|
||||
if (c_type == 'S')
|
||||
type = 1;
|
||||
else if (c_type == 'U')
|
||||
type = 2;
|
||||
else if (c_type == 'F')
|
||||
type = 4;
|
||||
else
|
||||
return -1;
|
||||
|
||||
if (c_endian == 'B')
|
||||
type |= 8;
|
||||
else if (c_endian != 'L')
|
||||
return -1;
|
||||
|
||||
if (c_type == 'F' && bits != 32 && bits != 64)
|
||||
return -1;
|
||||
if (c_type != 'F' && (bits < 8 || bits > 32))
|
||||
return -1;
|
||||
|
||||
desc->sample_type = type;
|
||||
desc->bits_per_channel = bits;
|
||||
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)
|
||||
{
|
||||
wav_io_context_t wav_io = { read_callback, seek_callback };
|
||||
m4af_io_callbacks_t m4af_io = {
|
||||
write_callback, seek_callback, tell_callback };
|
||||
static m4af_io_callbacks_t m4af_io = {
|
||||
read_callback, write_callback, seek_callback, tell_callback
|
||||
};
|
||||
aacenc_param_ex_t params = { 0 };
|
||||
|
||||
int result = 2;
|
||||
FILE *ifp = 0;
|
||||
FILE *ofp = 0;
|
||||
char *output_filename = 0;
|
||||
wav_reader_t *wavf = 0;
|
||||
pcm_reader_t *reader = 0;
|
||||
HANDLE_AACENCODER encoder = 0;
|
||||
AACENC_InfoStruct aacinfo = { 0 };
|
||||
m4af_writer_t *m4af = 0;
|
||||
m4af_ctx_t *m4af = 0;
|
||||
const pcm_sample_description_t *sample_format;
|
||||
int downsampled_timescale = 0;
|
||||
int frame_count = 0;
|
||||
struct stat stb = { 0 };
|
||||
|
||||
setlocale(LC_CTYPE, "");
|
||||
setbuf(stderr, 0);
|
||||
@ -488,20 +712,10 @@ int main(int argc, char **argv)
|
||||
if (parse_options(argc, argv, ¶ms) < 0)
|
||||
return 1;
|
||||
|
||||
if ((ifp = aacenc_fopen(params.input_filename, "rb")) == 0) {
|
||||
aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.input_filename,
|
||||
strerror(errno));
|
||||
if ((reader = open_input(¶ms)) == 0)
|
||||
goto END;
|
||||
}
|
||||
|
||||
if (fstat(fileno(ifp), &stb) == 0 && (stb.st_mode & S_IFMT) != S_IFREG)
|
||||
wav_io.seek = 0;
|
||||
|
||||
if ((wavf = wav_open(&wav_io, ifp, params.ignore_length)) == 0) {
|
||||
fprintf(stderr, "ERROR: broken / unsupported input file\n");
|
||||
goto END;
|
||||
}
|
||||
sample_format = wav_get_format(wavf);
|
||||
sample_format = pcm_get_format(reader);
|
||||
|
||||
if (aacenc_init(&encoder, (aacenc_param_t*)¶ms, sample_format,
|
||||
&aacinfo) < 0)
|
||||
@ -513,32 +727,38 @@ int main(int argc, char **argv)
|
||||
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,
|
||||
strerror(errno));
|
||||
goto END;
|
||||
}
|
||||
handle_signals();
|
||||
if (!params.transport_format) {
|
||||
uint32_t scale;
|
||||
unsigned framelen = aacinfo.frameLength;
|
||||
int sbr_mode = aacenc_is_sbr_active((aacenc_param_t*)¶ms);
|
||||
int sig_mode = aacEncoder_GetParam(encoder, AACENC_SIGNALING_MODE);
|
||||
if (sbr_mode && !sig_mode)
|
||||
downsampled_timescale = 1;
|
||||
int sbr_mode = aacenc_is_sbr_active((aacenc_param_t*)¶ms);
|
||||
int sig_mode = aacEncoder_GetParam(encoder, AACENC_SIGNALING_MODE);
|
||||
if (sbr_mode && !sig_mode)
|
||||
downsampled_timescale = 1;
|
||||
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;
|
||||
m4af_decoder_specific_info(m4af, 0, aacinfo.confBuf, aacinfo.confSize);
|
||||
m4af_set_decoder_specific_info(m4af, 0, aacinfo.confBuf,
|
||||
aacinfo.confSize);
|
||||
m4af_set_fixed_frame_duration(m4af, 0,
|
||||
framelen >> downsampled_timescale);
|
||||
m4af_set_vbr_mode(m4af, 0, params.bitrate_mode);
|
||||
m4af_set_priming_mode(m4af, params.gapless_mode + 1);
|
||||
m4af_begin_write(m4af);
|
||||
}
|
||||
frame_count = encode(wavf, encoder, aacinfo.frameLength, ofp, m4af);
|
||||
frame_count = encode(reader, encoder, aacinfo.frameLength,
|
||||
params.output_fp, m4af, !params.silent);
|
||||
if (frame_count < 0)
|
||||
goto END;
|
||||
if (m4af) {
|
||||
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
|
||||
- frames_read - aacinfo.encoderDelay;
|
||||
m4af_set_priming(m4af, 0, delay >> downsampled_timescale,
|
||||
@ -548,13 +768,13 @@ int main(int argc, char **argv)
|
||||
}
|
||||
result = 0;
|
||||
END:
|
||||
if (wavf) wav_teardown(&wavf);
|
||||
if (ifp) fclose(ifp);
|
||||
if (reader) pcm_teardown(&reader);
|
||||
if (params.input_fp) fclose(params.input_fp);
|
||||
if (m4af) m4af_teardown(&m4af);
|
||||
if (ofp) fclose(ofp);
|
||||
if (params.output_fp) fclose(params.output_fp);
|
||||
if (encoder) aacEncClose(&encoder);
|
||||
if (output_filename) free(output_filename);
|
||||
if (params.tag_table) free(params.tag_table);
|
||||
if (params.tags.tag_table) free(params.tags.tag_table);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
392
src/metadata.c
Normal file
392
src/metadata.c
Normal file
@ -0,0 +1,392 @@
|
||||
#if HAVE_CONFIG_H
|
||||
# include "config.h"
|
||||
#endif
|
||||
#if HAVE_STDINT_H
|
||||
# include <stdint.h>
|
||||
#endif
|
||||
#if HAVE_INTTYPES_H
|
||||
# include <inttypes.h>
|
||||
#elif defined(_MSC_VER)
|
||||
# define SCNd64 "I64d"
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include "m4af.h"
|
||||
#include "metadata.h"
|
||||
#include "compat.h"
|
||||
#include "parson.h"
|
||||
|
||||
typedef struct tag_key_mapping_t {
|
||||
const char *name;
|
||||
uint32_t fcc;
|
||||
} tag_key_mapping_t;
|
||||
|
||||
enum {
|
||||
TAG_TOTAL_DISCS = 1,
|
||||
TAG_TOTAL_TRACKS = 2
|
||||
};
|
||||
|
||||
static tag_key_mapping_t tag_mapping_table[] = {
|
||||
{ "album", M4AF_TAG_ALBUM },
|
||||
{ "albumartist", M4AF_TAG_ALBUM_ARTIST },
|
||||
{ "albumartistsort", M4AF_FOURCC('s','o','a','a') },
|
||||
{ "albumartistsortorder", M4AF_FOURCC('s','o','a','a') },
|
||||
{ "albumsort", M4AF_FOURCC('s','o','a','l') },
|
||||
{ "albumsortorder", M4AF_FOURCC('s','o','a','l') },
|
||||
{ "artist", M4AF_TAG_ARTIST },
|
||||
{ "artistsort", M4AF_FOURCC('s','o','a','r') },
|
||||
{ "artistsortorder", M4AF_FOURCC('s','o','a','r') },
|
||||
{ "band", M4AF_TAG_ALBUM_ARTIST },
|
||||
{ "bpm", M4AF_TAG_TEMPO },
|
||||
{ "comment", M4AF_TAG_COMMENT },
|
||||
{ "compilation", M4AF_TAG_COMPILATION },
|
||||
{ "composer", M4AF_TAG_COMPOSER },
|
||||
{ "composersort", M4AF_FOURCC('s','o','c','o') },
|
||||
{ "composersortorder", M4AF_FOURCC('s','o','c','o') },
|
||||
{ "contentgroup", M4AF_TAG_GROUPING },
|
||||
{ "copyright", M4AF_TAG_COPYRIGHT },
|
||||
{ "date", M4AF_TAG_DATE },
|
||||
{ "disc", M4AF_TAG_DISK },
|
||||
{ "disctotal", TAG_TOTAL_DISCS },
|
||||
{ "discnumber", M4AF_TAG_DISK },
|
||||
{ "genre", M4AF_TAG_GENRE },
|
||||
{ "grouping", M4AF_TAG_GROUPING },
|
||||
{ "itunescompilation", M4AF_TAG_COMPILATION },
|
||||
{ "lyrics", M4AF_TAG_LYRICS },
|
||||
{ "title", M4AF_TAG_TITLE },
|
||||
{ "titlesort", M4AF_FOURCC('s','o','n','m') },
|
||||
{ "titlesortorder", M4AF_FOURCC('s','o','n','m') },
|
||||
{ "totaldiscs", TAG_TOTAL_DISCS },
|
||||
{ "totaltracks", TAG_TOTAL_TRACKS },
|
||||
{ "track", M4AF_TAG_TRACK },
|
||||
{ "tracknumber", M4AF_TAG_TRACK },
|
||||
{ "tracktotal", TAG_TOTAL_TRACKS },
|
||||
{ "unsyncedlyrics", M4AF_TAG_LYRICS },
|
||||
{ "year", M4AF_TAG_DATE },
|
||||
};
|
||||
|
||||
static
|
||||
int tag_key_comparator(const void *k, const void *v)
|
||||
{
|
||||
return strcmp((const char *)k, ((tag_key_mapping_t*)v)->name);
|
||||
}
|
||||
|
||||
static
|
||||
uint32_t get_tag_fcc_from_name(const char *name)
|
||||
{
|
||||
char *name_p = 0, *p;
|
||||
const tag_key_mapping_t *ent;
|
||||
|
||||
name_p = malloc(strlen(name) + 1);
|
||||
for (p = name_p; *name; ++name) {
|
||||
unsigned char c = *name;
|
||||
if (c != ' ' && c != '-' && c != '_')
|
||||
*p++ = tolower(c);
|
||||
}
|
||||
*p = 0;
|
||||
ent = bsearch(name_p, tag_mapping_table,
|
||||
sizeof(tag_mapping_table) / sizeof(tag_mapping_table[0]),
|
||||
sizeof(tag_mapping_table[0]),
|
||||
tag_key_comparator);
|
||||
free(name_p);
|
||||
return ent ? ent->fcc : 0;
|
||||
}
|
||||
|
||||
char *aacenc_load_tag_from_file(const char *path, uint32_t *data_size)
|
||||
{
|
||||
FILE *fp = 0;
|
||||
char *data = 0;
|
||||
int64_t size;
|
||||
|
||||
if ((fp = aacenc_fopen(path, "rb")) == NULL) {
|
||||
aacenc_fprintf(stderr, "WARNING: %s: %s\n", path, strerror(errno));
|
||||
goto END;
|
||||
}
|
||||
fseeko(fp, 0, SEEK_END);
|
||||
size = ftello(fp);
|
||||
if (size > 5*1024*1024) {
|
||||
aacenc_fprintf(stderr, "WARNING: %s: size too large\n", path);
|
||||
goto END;
|
||||
}
|
||||
fseeko(fp, 0, SEEK_SET);
|
||||
data = malloc(size + 1);
|
||||
if (data) fread(data, 1, size, fp);
|
||||
data[size] = 0;
|
||||
*data_size = (uint32_t)size;
|
||||
END:
|
||||
if (fp) fclose(fp);
|
||||
return data;
|
||||
}
|
||||
|
||||
void aacenc_param_add_itmf_entry(aacenc_tag_param_t *params, uint32_t tag,
|
||||
const char *key, const char *value,
|
||||
uint32_t size, int is_file_name)
|
||||
{
|
||||
aacenc_tag_entry_t *entry;
|
||||
|
||||
if (!is_file_name && !size)
|
||||
return;
|
||||
if (params->tag_count == params->tag_table_capacity) {
|
||||
unsigned newsize = params->tag_table_capacity;
|
||||
newsize = newsize ? newsize * 2 : 1;
|
||||
params->tag_table =
|
||||
realloc(params->tag_table, newsize * sizeof(aacenc_tag_entry_t));
|
||||
params->tag_table_capacity = newsize;
|
||||
}
|
||||
entry = params->tag_table + params->tag_count;
|
||||
entry->tag = tag;
|
||||
if (tag == M4AF_FOURCC('-','-','-','-'))
|
||||
entry->name = key;
|
||||
entry->data = value;
|
||||
entry->data_size = size;
|
||||
entry->is_file_name = is_file_name;
|
||||
params->tag_count++;
|
||||
}
|
||||
|
||||
static
|
||||
void tag_put_number_pair(m4af_ctx_t *m4af, uint32_t fcc,
|
||||
const char *snumber, const char *stotal)
|
||||
{
|
||||
unsigned number = 0, total = 0;
|
||||
char buf[128];
|
||||
aacenc_tag_entry_t entry = { 0 };
|
||||
|
||||
if (snumber) sscanf(snumber, "%u", &number);
|
||||
if (stotal) sscanf(stotal, "%u", &total);
|
||||
if (number) {
|
||||
if (total) sprintf(buf, "%u/%u", number, total);
|
||||
else sprintf(buf, "%u", number);
|
||||
entry.tag = fcc;
|
||||
entry.data = buf;
|
||||
entry.data_size = strlen(buf);
|
||||
aacenc_put_tag_entry(m4af, &entry);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
const char *aacenc_json_object_get_string(JSON_Object *obj, const char *key,
|
||||
char *buf)
|
||||
{
|
||||
JSON_Value_Type type;
|
||||
const char *val = 0;
|
||||
|
||||
type = json_value_get_type(json_object_get_value(obj, key));
|
||||
if (type == JSONString)
|
||||
val = json_object_get_string(obj, key);
|
||||
else if (type == JSONNumber) {
|
||||
double num = json_object_get_number(obj, key);
|
||||
sprintf(buf, "%.15g", num);
|
||||
val = buf;
|
||||
} else if (type == JSONBoolean) {
|
||||
int n = json_object_get_boolean(obj, key);
|
||||
sprintf(buf, "%d", n);
|
||||
val = buf;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
void aacenc_put_tags_from_json(m4af_ctx_t *m4af, const char *json_filename)
|
||||
{
|
||||
char *data = 0;
|
||||
JSON_Value *json = 0;
|
||||
JSON_Object *root;
|
||||
size_t i, nelts;
|
||||
uint32_t data_size;
|
||||
char *json_dot_path;
|
||||
char *filename = 0;
|
||||
char *disc = 0;
|
||||
char *track = 0;
|
||||
char *total_discs = 0;
|
||||
char *total_tracks = 0;
|
||||
aacenc_tag_entry_t entry = { 0 };
|
||||
|
||||
filename = strdup(json_filename);
|
||||
if ((json_dot_path = strchr(filename, '?')) != 0)
|
||||
*json_dot_path++ = '\0';
|
||||
|
||||
if (!(data = aacenc_load_tag_from_file(filename, &data_size)))
|
||||
goto DONE;
|
||||
if (!(json = json_parse_string(data))) {
|
||||
aacenc_fprintf(stderr, "WARNING: failed to parse JSON\n");
|
||||
goto DONE;
|
||||
}
|
||||
root = json_value_get_object(json);
|
||||
if (json_dot_path) {
|
||||
if (!(root = json_object_dotget_object(root, json_dot_path))) {
|
||||
aacenc_fprintf(stderr, "WARNING: %s not found in JSON\n",
|
||||
json_dot_path);
|
||||
goto DONE;
|
||||
}
|
||||
}
|
||||
nelts = json_object_get_count(root);
|
||||
for (i = 0; i < nelts; ++i) {
|
||||
char buf[256];
|
||||
const char *key = json_object_get_name(root, i);
|
||||
const char *val = aacenc_json_object_get_string(root, key, buf);
|
||||
uint32_t fcc = get_tag_fcc_from_name(key);
|
||||
if (!val || !fcc)
|
||||
continue;
|
||||
|
||||
switch (fcc) {
|
||||
case TAG_TOTAL_DISCS:
|
||||
total_discs = realloc(total_discs, strlen(val) + 1);
|
||||
strcpy(total_discs, val);
|
||||
break;
|
||||
case TAG_TOTAL_TRACKS:
|
||||
total_tracks = realloc(total_tracks, strlen(val) + 1);
|
||||
strcpy(total_tracks, val);
|
||||
break;
|
||||
case M4AF_TAG_DISK:
|
||||
disc = realloc(disc, strlen(val) + 1);
|
||||
strcpy(disc, val);
|
||||
break;
|
||||
case M4AF_TAG_TRACK:
|
||||
track = realloc(track, strlen(val) + 1);
|
||||
strcpy(track, val);
|
||||
break;
|
||||
default:
|
||||
{
|
||||
entry.tag = fcc;
|
||||
entry.data = val;
|
||||
entry.data_size = strlen(val);
|
||||
aacenc_put_tag_entry(m4af, &entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
tag_put_number_pair(m4af, M4AF_TAG_TRACK, track, total_tracks);
|
||||
tag_put_number_pair(m4af, M4AF_TAG_DISK, disc, total_discs);
|
||||
DONE:
|
||||
if (track) free(track);
|
||||
if (disc) free(disc);
|
||||
if (total_tracks) free(total_tracks);
|
||||
if (total_discs) free(total_discs);
|
||||
if (data) free(data);
|
||||
if (filename) free(filename);
|
||||
if (json) json_value_free(json);
|
||||
}
|
||||
|
||||
void aacenc_put_tag_entry(m4af_ctx_t *m4af, const aacenc_tag_entry_t *tag)
|
||||
{
|
||||
unsigned m, n = 0;
|
||||
const char *data = tag->data;
|
||||
uint32_t data_size = tag->data_size;
|
||||
char *file_contents = 0;
|
||||
|
||||
if (tag->is_file_name) {
|
||||
data = file_contents = aacenc_load_tag_from_file(tag->data, &data_size);
|
||||
if (!data) return;
|
||||
}
|
||||
switch (tag->tag) {
|
||||
case M4AF_TAG_TRACK:
|
||||
if (sscanf(data, "%u/%u", &m, &n) >= 1)
|
||||
m4af_add_itmf_track_tag(m4af, m, n);
|
||||
break;
|
||||
case M4AF_TAG_DISK:
|
||||
if (sscanf(data, "%u/%u", &m, &n) >= 1)
|
||||
m4af_add_itmf_disk_tag(m4af, m, n);
|
||||
break;
|
||||
case M4AF_TAG_GENRE_ID3:
|
||||
if (sscanf(data, "%u", &n) == 1)
|
||||
m4af_add_itmf_genre_tag(m4af, n);
|
||||
break;
|
||||
case M4AF_TAG_TEMPO:
|
||||
if (sscanf(data, "%u", &n) == 1)
|
||||
m4af_add_itmf_int16_tag(m4af, tag->tag, n);
|
||||
break;
|
||||
case M4AF_TAG_COMPILATION:
|
||||
case M4AF_FOURCC('a','k','I','D'):
|
||||
case M4AF_FOURCC('h','d','v','d'):
|
||||
case M4AF_FOURCC('p','c','s','t'):
|
||||
case M4AF_FOURCC('p','g','a','p'):
|
||||
case M4AF_FOURCC('r','t','n','g'):
|
||||
case M4AF_FOURCC('s','t','i','k'):
|
||||
if (sscanf(data, "%u", &n) == 1)
|
||||
m4af_add_itmf_int8_tag(m4af, tag->tag, n);
|
||||
break;
|
||||
case M4AF_FOURCC('a','t','I','D'):
|
||||
case M4AF_FOURCC('c','m','I','D'):
|
||||
case M4AF_FOURCC('c','n','I','D'):
|
||||
case M4AF_FOURCC('g','e','I','D'):
|
||||
case M4AF_FOURCC('s','f','I','D'):
|
||||
case M4AF_FOURCC('t','v','s','n'):
|
||||
case M4AF_FOURCC('t','v','s','s'):
|
||||
if (sscanf(data, "%u", &n) == 1)
|
||||
m4af_add_itmf_int32_tag(m4af, tag->tag, n);
|
||||
break;
|
||||
case M4AF_FOURCC('p','l','I','D'):
|
||||
{
|
||||
int64_t qn;
|
||||
if (sscanf(data, "%" SCNd64, &qn) == 1)
|
||||
m4af_add_itmf_int64_tag(m4af, tag->tag, qn);
|
||||
break;
|
||||
}
|
||||
case M4AF_TAG_ARTWORK:
|
||||
{
|
||||
int data_type = 0;
|
||||
if (!memcmp(data, "GIF", 3))
|
||||
data_type = M4AF_GIF;
|
||||
else if (!memcmp(data, "\xff\xd8\xff", 3))
|
||||
data_type = M4AF_JPEG;
|
||||
else if (!memcmp(data, "\x89PNG", 4))
|
||||
data_type = M4AF_PNG;
|
||||
if (data_type)
|
||||
m4af_add_itmf_short_tag(m4af, tag->tag, data_type,
|
||||
data, data_size);
|
||||
break;
|
||||
}
|
||||
case M4AF_FOURCC('-','-','-','-'):
|
||||
{
|
||||
char *u8 = aacenc_to_utf8(data);
|
||||
m4af_add_itmf_long_tag(m4af, tag->name, u8);
|
||||
free(u8);
|
||||
break;
|
||||
}
|
||||
case M4AF_TAG_TITLE:
|
||||
case M4AF_TAG_ARTIST:
|
||||
case M4AF_TAG_ALBUM:
|
||||
case M4AF_TAG_GENRE:
|
||||
case M4AF_TAG_DATE:
|
||||
case M4AF_TAG_COMPOSER:
|
||||
case M4AF_TAG_GROUPING:
|
||||
case M4AF_TAG_COMMENT:
|
||||
case M4AF_TAG_LYRICS:
|
||||
case M4AF_TAG_TOOL:
|
||||
case M4AF_TAG_ALBUM_ARTIST:
|
||||
case M4AF_TAG_DESCRIPTION:
|
||||
case M4AF_TAG_LONG_DESCRIPTION:
|
||||
case M4AF_TAG_COPYRIGHT:
|
||||
case M4AF_FOURCC('a','p','I','D'):
|
||||
case M4AF_FOURCC('c','a','t','g'):
|
||||
case M4AF_FOURCC('k','e','y','w'):
|
||||
case M4AF_FOURCC('p','u','r','d'):
|
||||
case M4AF_FOURCC('p','u','r','l'):
|
||||
case M4AF_FOURCC('s','o','a','a'):
|
||||
case M4AF_FOURCC('s','o','a','l'):
|
||||
case M4AF_FOURCC('s','o','a','r'):
|
||||
case M4AF_FOURCC('s','o','c','o'):
|
||||
case M4AF_FOURCC('s','o','n','m'):
|
||||
case M4AF_FOURCC('s','o','s','n'):
|
||||
case M4AF_FOURCC('t','v','e','n'):
|
||||
case M4AF_FOURCC('t','v','n','n'):
|
||||
case M4AF_FOURCC('t','v','s','h'):
|
||||
case M4AF_FOURCC('x','i','d',' '):
|
||||
case M4AF_FOURCC('\xa9','e','n','c'):
|
||||
case M4AF_FOURCC('\xa9','s','t','3'):
|
||||
{
|
||||
char *u8 = aacenc_to_utf8(data);
|
||||
m4af_add_itmf_string_tag(m4af, tag->tag, u8);
|
||||
free(u8);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
fprintf(stderr, "WARNING: unknown/unsupported tag: %c%c%c%c\n",
|
||||
tag->tag >> 24, (tag->tag >> 16) & 0xff,
|
||||
(tag->tag >> 8) & 0xff, tag->tag & 0xff);
|
||||
}
|
||||
if (file_contents) free(file_contents);
|
||||
}
|
||||
|
||||
|
28
src/metadata.h
Normal file
28
src/metadata.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef METADATA_H
|
||||
#define METADATA_H
|
||||
|
||||
typedef struct aacenc_tag_entry_t {
|
||||
uint32_t tag;
|
||||
const char *name;
|
||||
const char *data;
|
||||
uint32_t data_size;
|
||||
int is_file_name;
|
||||
} aacenc_tag_entry_t;
|
||||
|
||||
typedef struct aacenc_tag_param_t {
|
||||
aacenc_tag_entry_t *tag_table;
|
||||
unsigned tag_count;
|
||||
unsigned tag_table_capacity;
|
||||
} aacenc_tag_param_t;
|
||||
|
||||
char *aacenc_load_tag_from_file(const char *path, uint32_t *data_size);
|
||||
|
||||
void aacenc_param_add_itmf_entry(aacenc_tag_param_t *params, uint32_t tag,
|
||||
const char *key, const char *value,
|
||||
uint32_t size, int is_file_name);
|
||||
|
||||
void aacenc_put_tags_from_json(m4af_ctx_t *m4af, const char *json_filename);
|
||||
|
||||
void aacenc_put_tag_entry(m4af_ctx_t *m4af, const aacenc_tag_entry_t *tag);
|
||||
|
||||
#endif
|
647
src/parson.c
Normal file
647
src/parson.c
Normal file
@ -0,0 +1,647 @@
|
||||
/*
|
||||
Parson ( http://kgabis.github.com/parson/ )
|
||||
Copyright (c) 2012 Krzysztof Gabis
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "parson.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define ERROR 0
|
||||
#define SUCCESS 1
|
||||
#define STARTING_CAPACITY 15
|
||||
#define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */
|
||||
#define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */
|
||||
#define MAX_NESTING 19
|
||||
#define sizeof_token(a) (sizeof(a) - 1)
|
||||
#define skip_char(str) ((*str)++)
|
||||
#define skip_whitespaces(str) while (isspace(**string)) { skip_char(string); }
|
||||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
#define parson_malloc(a) malloc(a)
|
||||
#define parson_free(a) free((void*)a)
|
||||
#define parson_realloc(a, b) realloc(a, b)
|
||||
|
||||
/* Type definitions */
|
||||
typedef union json_value_value {
|
||||
const char *string;
|
||||
double number;
|
||||
JSON_Object *object;
|
||||
JSON_Array *array;
|
||||
int boolean;
|
||||
int null;
|
||||
} JSON_Value_Value;
|
||||
|
||||
struct json_value_t {
|
||||
JSON_Value_Type type;
|
||||
JSON_Value_Value value;
|
||||
};
|
||||
|
||||
struct json_object_t {
|
||||
const char **names;
|
||||
JSON_Value **values;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
struct json_array_t {
|
||||
JSON_Value **items;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
/* Various */
|
||||
static int try_realloc(void **ptr, size_t new_size);
|
||||
static char * parson_strndup(const char *string, size_t n);
|
||||
static int is_utf(const unsigned char *string);
|
||||
static int is_decimal(const char *string, size_t length);
|
||||
|
||||
/* JSON Object */
|
||||
static JSON_Object * json_object_init(void);
|
||||
static int json_object_add(JSON_Object *object, const char *name, JSON_Value *value);
|
||||
static int json_object_resize(JSON_Object *object, size_t capacity);
|
||||
static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n);
|
||||
static void json_object_free(JSON_Object *object);
|
||||
|
||||
/* JSON Array */
|
||||
static JSON_Array * json_array_init(void);
|
||||
static int json_array_add(JSON_Array *array, JSON_Value *value);
|
||||
static int json_array_resize(JSON_Array *array, size_t capacity);
|
||||
static void json_array_free(JSON_Array *array);
|
||||
|
||||
/* JSON Value */
|
||||
static JSON_Value * json_value_init_object(void);
|
||||
static JSON_Value * json_value_init_array(void);
|
||||
static JSON_Value * json_value_init_string(const char *string);
|
||||
static JSON_Value * json_value_init_number(double number);
|
||||
static JSON_Value * json_value_init_boolean(int boolean);
|
||||
static JSON_Value * json_value_init_null(void);
|
||||
|
||||
/* Parser */
|
||||
static void skip_quotes(const char **string);
|
||||
static const char * get_processed_string(const char **string);
|
||||
static JSON_Value * parse_object_value(const char **string, size_t nesting);
|
||||
static JSON_Value * parse_array_value(const char **string, size_t nesting);
|
||||
static JSON_Value * parse_string_value(const char **string);
|
||||
static JSON_Value * parse_boolean_value(const char **string);
|
||||
static JSON_Value * parse_number_value(const char **string);
|
||||
static JSON_Value * parse_null_value(const char **string);
|
||||
static JSON_Value * parse_value(const char **string, size_t nesting);
|
||||
|
||||
/* Various */
|
||||
static int try_realloc(void **ptr, size_t new_size) {
|
||||
void *reallocated_ptr = parson_realloc(*ptr, new_size);
|
||||
if (!reallocated_ptr) { return ERROR; }
|
||||
*ptr = reallocated_ptr;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
static char * parson_strndup(const char *string, size_t n) {
|
||||
char *output_string = (char*)parson_malloc(n + 1);
|
||||
if (!output_string) { return NULL; }
|
||||
output_string[n] = '\0';
|
||||
strncpy(output_string, string, n);
|
||||
return output_string;
|
||||
}
|
||||
|
||||
static int is_utf(const unsigned char *s) {
|
||||
return isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]);
|
||||
}
|
||||
|
||||
static int is_decimal(const char *string, size_t length) {
|
||||
if (length > 1 && string[0] == '0' && string[1] != '.') { return 0; }
|
||||
if (length > 2 && !strncmp(string, "-0", 2) && string[2] != '.') { return 0; }
|
||||
while (length--) { if (strchr("xX", string[length])) { return 0; } }
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* JSON Object */
|
||||
static JSON_Object * json_object_init(void) {
|
||||
JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object));
|
||||
if (!new_obj) { return NULL; }
|
||||
new_obj->names = (const char**)NULL;
|
||||
new_obj->values = (JSON_Value**)NULL;
|
||||
new_obj->capacity = 0;
|
||||
new_obj->count = 0;
|
||||
return new_obj;
|
||||
}
|
||||
|
||||
static int json_object_add(JSON_Object *object, const char *name, JSON_Value *value) {
|
||||
size_t index;
|
||||
if (object->count >= object->capacity) {
|
||||
size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY);
|
||||
if (new_capacity > OBJECT_MAX_CAPACITY) { return ERROR; }
|
||||
if (json_object_resize(object, new_capacity) == ERROR) { return ERROR; }
|
||||
}
|
||||
if (json_object_get_value(object, name) != NULL) { return ERROR; }
|
||||
index = object->count;
|
||||
object->names[index] = parson_strndup(name, strlen(name));
|
||||
if (!object->names[index]) { return ERROR; }
|
||||
object->values[index] = value;
|
||||
object->count++;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
static int json_object_resize(JSON_Object *object, size_t capacity) {
|
||||
if (try_realloc((void**)&object->names, capacity * sizeof(char*)) == ERROR) { return ERROR; }
|
||||
if (try_realloc((void**)&object->values, capacity * sizeof(JSON_Value*)) == ERROR) { return ERROR; }
|
||||
object->capacity = capacity;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n) {
|
||||
size_t i, name_length;
|
||||
for (i = 0; i < json_object_get_count(object); i++) {
|
||||
name_length = strlen(object->names[i]);
|
||||
if (name_length != n) { continue; }
|
||||
if (strncmp(object->names[i], name, n) == 0) { return object->values[i]; }
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void json_object_free(JSON_Object *object) {
|
||||
while(object->count--) {
|
||||
parson_free(object->names[object->count]);
|
||||
json_value_free(object->values[object->count]);
|
||||
}
|
||||
parson_free(object->names);
|
||||
parson_free(object->values);
|
||||
parson_free(object);
|
||||
}
|
||||
|
||||
/* JSON Array */
|
||||
static JSON_Array * json_array_init(void) {
|
||||
JSON_Array *new_array = (JSON_Array*)parson_malloc(sizeof(JSON_Array));
|
||||
if (!new_array) { return NULL; }
|
||||
new_array->items = (JSON_Value**)NULL;
|
||||
new_array->capacity = 0;
|
||||
new_array->count = 0;
|
||||
return new_array;
|
||||
}
|
||||
|
||||
static int json_array_add(JSON_Array *array, JSON_Value *value) {
|
||||
if (array->count >= array->capacity) {
|
||||
size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY);
|
||||
if (new_capacity > ARRAY_MAX_CAPACITY) { return ERROR; }
|
||||
if (!json_array_resize(array, new_capacity)) { return ERROR; }
|
||||
}
|
||||
array->items[array->count] = value;
|
||||
array->count++;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
static int json_array_resize(JSON_Array *array, size_t capacity) {
|
||||
if (try_realloc((void**)&array->items, capacity * sizeof(JSON_Value*)) == ERROR) { return ERROR; }
|
||||
array->capacity = capacity;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
static void json_array_free(JSON_Array *array) {
|
||||
while (array->count--) { json_value_free(array->items[array->count]); }
|
||||
parson_free(array->items);
|
||||
parson_free(array);
|
||||
}
|
||||
|
||||
/* JSON Value */
|
||||
static JSON_Value * json_value_init_object(void) {
|
||||
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
||||
if (!new_value) { return NULL; }
|
||||
new_value->type = JSONObject;
|
||||
new_value->value.object = json_object_init();
|
||||
if (!new_value->value.object) { parson_free(new_value); return NULL; }
|
||||
return new_value;
|
||||
}
|
||||
|
||||
static JSON_Value * json_value_init_array(void) {
|
||||
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
||||
if (!new_value) { return NULL; }
|
||||
new_value->type = JSONArray;
|
||||
new_value->value.array = json_array_init();
|
||||
if (!new_value->value.array) { parson_free(new_value); return NULL; }
|
||||
return new_value;
|
||||
}
|
||||
|
||||
static JSON_Value * json_value_init_string(const char *string) {
|
||||
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
||||
if (!new_value) { return NULL; }
|
||||
new_value->type = JSONString;
|
||||
new_value->value.string = string;
|
||||
return new_value;
|
||||
}
|
||||
|
||||
static JSON_Value * json_value_init_number(double number) {
|
||||
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
||||
if (!new_value) { return NULL; }
|
||||
new_value->type = JSONNumber;
|
||||
new_value->value.number = number;
|
||||
return new_value;
|
||||
}
|
||||
|
||||
static JSON_Value * json_value_init_boolean(int boolean) {
|
||||
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
||||
if (!new_value) { return NULL; }
|
||||
new_value->type = JSONBoolean;
|
||||
new_value->value.boolean = boolean;
|
||||
return new_value;
|
||||
}
|
||||
|
||||
static JSON_Value * json_value_init_null(void) {
|
||||
JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value));
|
||||
if (!new_value) { return NULL; }
|
||||
new_value->type = JSONNull;
|
||||
return new_value;
|
||||
}
|
||||
|
||||
/* Parser */
|
||||
static void skip_quotes(const char **string) {
|
||||
skip_char(string);
|
||||
while (**string != '\"') {
|
||||
if (**string == '\0') { return; }
|
||||
if (**string == '\\') { skip_char(string); if (**string == '\0') { return; }}
|
||||
skip_char(string);
|
||||
}
|
||||
skip_char(string);
|
||||
}
|
||||
|
||||
/* Returns contents of a string inside double quotes and parses escaped
|
||||
characters inside.
|
||||
Example: "\u006Corem ipsum" -> lorem ipsum */
|
||||
static const char * get_processed_string(const char **string) {
|
||||
const char *string_start = *string;
|
||||
char *output, *processed_ptr, *unprocessed_ptr, current_char;
|
||||
unsigned int utf_val;
|
||||
skip_quotes(string);
|
||||
if (**string == '\0') { return NULL; }
|
||||
output = parson_strndup(string_start + 1, *string - string_start - 2);
|
||||
if (!output) { return NULL; }
|
||||
processed_ptr = unprocessed_ptr = output;
|
||||
while (*unprocessed_ptr) {
|
||||
current_char = *unprocessed_ptr;
|
||||
if (current_char == '\\') {
|
||||
unprocessed_ptr++;
|
||||
current_char = *unprocessed_ptr;
|
||||
switch (current_char) {
|
||||
case '\"': case '\\': case '/': break;
|
||||
case 'b': current_char = '\b'; break;
|
||||
case 'f': current_char = '\f'; break;
|
||||
case 'n': current_char = '\n'; break;
|
||||
case 'r': current_char = '\r'; break;
|
||||
case 't': current_char = '\t'; break;
|
||||
case 'u':
|
||||
unprocessed_ptr++;
|
||||
if (!is_utf((const unsigned char*)unprocessed_ptr) ||
|
||||
sscanf(unprocessed_ptr, "%4x", &utf_val) == EOF) {
|
||||
parson_free(output); return NULL;
|
||||
}
|
||||
if (utf_val < 0x80) {
|
||||
current_char = utf_val;
|
||||
} else if (utf_val < 0x800) {
|
||||
*processed_ptr++ = (utf_val >> 6) | 0xC0;
|
||||
current_char = ((utf_val | 0x80) & 0xBF);
|
||||
} else {
|
||||
*processed_ptr++ = (utf_val >> 12) | 0xE0;
|
||||
*processed_ptr++ = (((utf_val >> 6) | 0x80) & 0xBF);
|
||||
current_char = ((utf_val | 0x80) & 0xBF);
|
||||
}
|
||||
unprocessed_ptr += 3;
|
||||
break;
|
||||
default:
|
||||
parson_free(output);
|
||||
return NULL;
|
||||
break;
|
||||
}
|
||||
} else if ((unsigned char)current_char < 0x20) { /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */
|
||||
parson_free(output);
|
||||
return NULL;
|
||||
}
|
||||
*processed_ptr = current_char;
|
||||
processed_ptr++;
|
||||
unprocessed_ptr++;
|
||||
}
|
||||
*processed_ptr = '\0';
|
||||
if (try_realloc((void**)&output, strlen(output) + 1) == ERROR) { return NULL; }
|
||||
return output;
|
||||
}
|
||||
|
||||
static JSON_Value * parse_value(const char **string, size_t nesting) {
|
||||
if (nesting > MAX_NESTING) { return NULL; }
|
||||
skip_whitespaces(string);
|
||||
switch (**string) {
|
||||
case '{':
|
||||
return parse_object_value(string, nesting + 1);
|
||||
case '[':
|
||||
return parse_array_value(string, nesting + 1);
|
||||
case '\"':
|
||||
return parse_string_value(string);
|
||||
case 'f': case 't':
|
||||
return parse_boolean_value(string);
|
||||
case '-':
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
return parse_number_value(string);
|
||||
case 'n':
|
||||
return parse_null_value(string);
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static JSON_Value * parse_object_value(const char **string, size_t nesting) {
|
||||
JSON_Value *output_value = json_value_init_object(), *new_value = NULL;
|
||||
JSON_Object *output_object = json_value_get_object(output_value);
|
||||
const char *new_key = NULL;
|
||||
if (!output_value) { return NULL; }
|
||||
skip_char(string);
|
||||
skip_whitespaces(string);
|
||||
if (**string == '}') { skip_char(string); return output_value; } /* empty object */
|
||||
while (**string != '\0') {
|
||||
new_key = get_processed_string(string);
|
||||
skip_whitespaces(string);
|
||||
if (!new_key || **string != ':') {
|
||||
json_value_free(output_value);
|
||||
return NULL;
|
||||
}
|
||||
skip_char(string);
|
||||
new_value = parse_value(string, nesting);
|
||||
if (!new_value) {
|
||||
parson_free(new_key);
|
||||
json_value_free(output_value);
|
||||
return NULL;
|
||||
}
|
||||
if(!json_object_add(output_object, new_key, new_value)) {
|
||||
parson_free(new_key);
|
||||
parson_free(new_value);
|
||||
json_value_free(output_value);
|
||||
return NULL;
|
||||
}
|
||||
parson_free(new_key);
|
||||
skip_whitespaces(string);
|
||||
if (**string != ',') { break; }
|
||||
skip_char(string);
|
||||
skip_whitespaces(string);
|
||||
}
|
||||
skip_whitespaces(string);
|
||||
if (**string != '}' || /* Trim object after parsing is over */
|
||||
json_object_resize(output_object, json_object_get_count(output_object)) == ERROR) {
|
||||
json_value_free(output_value);
|
||||
return NULL;
|
||||
}
|
||||
skip_char(string);
|
||||
return output_value;
|
||||
}
|
||||
|
||||
static JSON_Value * parse_array_value(const char **string, size_t nesting) {
|
||||
JSON_Value *output_value = json_value_init_array(), *new_array_value = NULL;
|
||||
JSON_Array *output_array = json_value_get_array(output_value);
|
||||
if (!output_value) { return NULL; }
|
||||
skip_char(string);
|
||||
skip_whitespaces(string);
|
||||
if (**string == ']') { /* empty array */
|
||||
skip_char(string);
|
||||
return output_value;
|
||||
}
|
||||
while (**string != '\0') {
|
||||
new_array_value = parse_value(string, nesting);
|
||||
if (!new_array_value) {
|
||||
json_value_free(output_value);
|
||||
return NULL;
|
||||
}
|
||||
if(json_array_add(output_array, new_array_value) == ERROR) {
|
||||
parson_free(new_array_value);
|
||||
json_value_free(output_value);
|
||||
return NULL;
|
||||
}
|
||||
skip_whitespaces(string);
|
||||
if (**string != ',') { break; }
|
||||
skip_char(string);
|
||||
skip_whitespaces(string);
|
||||
}
|
||||
skip_whitespaces(string);
|
||||
if (**string != ']' || /* Trim array after parsing is over */
|
||||
json_array_resize(output_array, json_array_get_count(output_array)) == ERROR) {
|
||||
json_value_free(output_value);
|
||||
return NULL;
|
||||
}
|
||||
skip_char(string);
|
||||
return output_value;
|
||||
}
|
||||
|
||||
static JSON_Value * parse_string_value(const char **string) {
|
||||
const char *new_string = get_processed_string(string);
|
||||
if (!new_string) { return NULL; }
|
||||
return json_value_init_string(new_string);
|
||||
}
|
||||
|
||||
static JSON_Value * parse_boolean_value(const char **string) {
|
||||
size_t true_token_size = sizeof_token("true");
|
||||
size_t false_token_size = sizeof_token("false");
|
||||
if (strncmp("true", *string, true_token_size) == 0) {
|
||||
*string += true_token_size;
|
||||
return json_value_init_boolean(1);
|
||||
} else if (strncmp("false", *string, false_token_size) == 0) {
|
||||
*string += false_token_size;
|
||||
return json_value_init_boolean(0);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static JSON_Value * parse_number_value(const char **string) {
|
||||
char *end;
|
||||
double number = strtod(*string, &end);
|
||||
JSON_Value *output_value;
|
||||
if (is_decimal(*string, end - *string)) {
|
||||
*string = end;
|
||||
output_value = json_value_init_number(number);
|
||||
} else {
|
||||
output_value = NULL;
|
||||
}
|
||||
return output_value;
|
||||
}
|
||||
|
||||
static JSON_Value * parse_null_value(const char **string) {
|
||||
size_t token_size = sizeof_token("null");
|
||||
if (strncmp("null", *string, token_size) == 0) {
|
||||
*string += token_size;
|
||||
return json_value_init_null();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Parser API */
|
||||
JSON_Value * json_parse_file(const char *filename) {
|
||||
FILE *fp = fopen(filename, "r");
|
||||
size_t file_size;
|
||||
char *file_contents;
|
||||
JSON_Value *output_value;
|
||||
if (!fp) { return NULL; }
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
file_size = ftell(fp);
|
||||
rewind(fp);
|
||||
file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 1));
|
||||
if (!file_contents) { fclose(fp); return NULL; }
|
||||
fread(file_contents, file_size, 1, fp);
|
||||
fclose(fp);
|
||||
file_contents[file_size] = '\0';
|
||||
output_value = json_parse_string(file_contents);
|
||||
parson_free(file_contents);
|
||||
return output_value;
|
||||
}
|
||||
|
||||
JSON_Value * json_parse_string(const char *string) {
|
||||
if (!string || (*string != '{' && *string != '[')) { return NULL; }
|
||||
return parse_value((const char**)&string, 0);
|
||||
}
|
||||
|
||||
/* JSON Object API */
|
||||
JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) {
|
||||
return json_object_nget_value(object, name, strlen(name));
|
||||
}
|
||||
|
||||
const char * json_object_get_string(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_string(json_object_get_value(object, name));
|
||||
}
|
||||
|
||||
double json_object_get_number(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_number(json_object_get_value(object, name));
|
||||
}
|
||||
|
||||
JSON_Object * json_object_get_object(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_object(json_object_get_value(object, name));
|
||||
}
|
||||
|
||||
JSON_Array * json_object_get_array(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_array(json_object_get_value(object, name));
|
||||
}
|
||||
|
||||
int json_object_get_boolean(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_boolean(json_object_get_value(object, name));
|
||||
}
|
||||
|
||||
JSON_Value * json_object_dotget_value(const JSON_Object *object, const char *name) {
|
||||
const char *dot_position = strchr(name, '.');
|
||||
if (!dot_position) { return json_object_get_value(object, name); }
|
||||
object = json_value_get_object(json_object_nget_value(object, name, dot_position - name));
|
||||
return json_object_dotget_value(object, dot_position + 1);
|
||||
}
|
||||
|
||||
const char * json_object_dotget_string(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_string(json_object_dotget_value(object, name));
|
||||
}
|
||||
|
||||
double json_object_dotget_number(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_number(json_object_dotget_value(object, name));
|
||||
}
|
||||
|
||||
JSON_Object * json_object_dotget_object(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_object(json_object_dotget_value(object, name));
|
||||
}
|
||||
|
||||
JSON_Array * json_object_dotget_array(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_array(json_object_dotget_value(object, name));
|
||||
}
|
||||
|
||||
int json_object_dotget_boolean(const JSON_Object *object, const char *name) {
|
||||
return json_value_get_boolean(json_object_dotget_value(object, name));
|
||||
}
|
||||
|
||||
size_t json_object_get_count(const JSON_Object *object) {
|
||||
return object ? object->count : 0;
|
||||
}
|
||||
|
||||
const char * json_object_get_name(const JSON_Object *object, size_t index) {
|
||||
if (index >= json_object_get_count(object)) { return NULL; }
|
||||
return object->names[index];
|
||||
}
|
||||
|
||||
/* JSON Array API */
|
||||
JSON_Value * json_array_get_value(const JSON_Array *array, size_t index) {
|
||||
if (index >= json_array_get_count(array)) { return NULL; }
|
||||
return array->items[index];
|
||||
}
|
||||
|
||||
const char * json_array_get_string(const JSON_Array *array, size_t index) {
|
||||
return json_value_get_string(json_array_get_value(array, index));
|
||||
}
|
||||
|
||||
double json_array_get_number(const JSON_Array *array, size_t index) {
|
||||
return json_value_get_number(json_array_get_value(array, index));
|
||||
}
|
||||
|
||||
JSON_Object * json_array_get_object(const JSON_Array *array, size_t index) {
|
||||
return json_value_get_object(json_array_get_value(array, index));
|
||||
}
|
||||
|
||||
JSON_Array * json_array_get_array(const JSON_Array *array, size_t index) {
|
||||
return json_value_get_array(json_array_get_value(array, index));
|
||||
}
|
||||
|
||||
int json_array_get_boolean(const JSON_Array *array, size_t index) {
|
||||
return json_value_get_boolean(json_array_get_value(array, index));
|
||||
}
|
||||
|
||||
size_t json_array_get_count(const JSON_Array *array) {
|
||||
return array ? array->count : 0;
|
||||
}
|
||||
|
||||
/* JSON Value API */
|
||||
JSON_Value_Type json_value_get_type(const JSON_Value *value) {
|
||||
return value ? value->type : JSONError;
|
||||
}
|
||||
|
||||
JSON_Object * json_value_get_object(const JSON_Value *value) {
|
||||
return json_value_get_type(value) == JSONObject ? value->value.object : NULL;
|
||||
}
|
||||
|
||||
JSON_Array * json_value_get_array(const JSON_Value *value) {
|
||||
return json_value_get_type(value) == JSONArray ? value->value.array : NULL;
|
||||
}
|
||||
|
||||
const char * json_value_get_string(const JSON_Value *value) {
|
||||
return json_value_get_type(value) == JSONString ? value->value.string : NULL;
|
||||
}
|
||||
|
||||
double json_value_get_number(const JSON_Value *value) {
|
||||
return json_value_get_type(value) == JSONNumber ? value->value.number : 0;
|
||||
}
|
||||
|
||||
int json_value_get_boolean(const JSON_Value *value) {
|
||||
return json_value_get_type(value) == JSONBoolean ? value->value.boolean : -1;
|
||||
}
|
||||
|
||||
void json_value_free(JSON_Value *value) {
|
||||
switch (json_value_get_type(value)) {
|
||||
case JSONObject:
|
||||
json_object_free(value->value.object);
|
||||
break;
|
||||
case JSONString:
|
||||
if (value->value.string) { parson_free(value->value.string); }
|
||||
break;
|
||||
case JSONArray:
|
||||
json_array_free(value->value.array);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
parson_free(value);
|
||||
}
|
100
src/parson.h
Normal file
100
src/parson.h
Normal file
@ -0,0 +1,100 @@
|
||||
/*
|
||||
Parson ( http://kgabis.github.com/parson/ )
|
||||
Copyright (c) 2012 Krzysztof Gabis
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef parson_parson_h
|
||||
#define parson_parson_h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <stddef.h> /* size_t */
|
||||
|
||||
/* Types and enums */
|
||||
typedef struct json_object_t JSON_Object;
|
||||
typedef struct json_array_t JSON_Array;
|
||||
typedef struct json_value_t JSON_Value;
|
||||
|
||||
typedef enum json_value_type {
|
||||
JSONError = 0,
|
||||
JSONNull = 1,
|
||||
JSONString = 2,
|
||||
JSONNumber = 3,
|
||||
JSONObject = 4,
|
||||
JSONArray = 5,
|
||||
JSONBoolean = 6
|
||||
} JSON_Value_Type;
|
||||
|
||||
/* Parses first JSON value in a file, returns NULL in case of error */
|
||||
JSON_Value * json_parse_file(const char *filename);
|
||||
|
||||
/* Parses first JSON value in a string, returns NULL in case of error */
|
||||
JSON_Value * json_parse_string(const char *string);
|
||||
|
||||
/* JSON Object */
|
||||
JSON_Value * json_object_get_value (const JSON_Object *object, const char *name);
|
||||
const char * json_object_get_string (const JSON_Object *object, const char *name);
|
||||
JSON_Object * json_object_get_object (const JSON_Object *object, const char *name);
|
||||
JSON_Array * json_object_get_array (const JSON_Object *object, const char *name);
|
||||
double json_object_get_number (const JSON_Object *object, const char *name);
|
||||
int json_object_get_boolean(const JSON_Object *object, const char *name);
|
||||
|
||||
/* dotget functions enable addressing values with dot notation in nested objects,
|
||||
just like in structs or c++/java/c# objects (e.g. objectA.objectB.value).
|
||||
Because valid names in JSON can contain dots, some values may be inaccessible
|
||||
this way. */
|
||||
JSON_Value * json_object_dotget_value (const JSON_Object *object, const char *name);
|
||||
const char * json_object_dotget_string (const JSON_Object *object, const char *name);
|
||||
JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name);
|
||||
JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name);
|
||||
double json_object_dotget_number (const JSON_Object *object, const char *name);
|
||||
int json_object_dotget_boolean(const JSON_Object *object, const char *name);
|
||||
|
||||
/* Functions to get available names */
|
||||
size_t json_object_get_count(const JSON_Object *object);
|
||||
const char * json_object_get_name (const JSON_Object *object, size_t index);
|
||||
|
||||
/* JSON Array */
|
||||
JSON_Value * json_array_get_value (const JSON_Array *array, size_t index);
|
||||
const char * json_array_get_string (const JSON_Array *array, size_t index);
|
||||
JSON_Object * json_array_get_object (const JSON_Array *array, size_t index);
|
||||
JSON_Array * json_array_get_array (const JSON_Array *array, size_t index);
|
||||
double json_array_get_number (const JSON_Array *array, size_t index);
|
||||
int json_array_get_boolean(const JSON_Array *array, size_t index);
|
||||
size_t json_array_get_count (const JSON_Array *array);
|
||||
|
||||
/* JSON Value */
|
||||
JSON_Value_Type json_value_get_type (const JSON_Value *value);
|
||||
JSON_Object * json_value_get_object (const JSON_Value *value);
|
||||
JSON_Array * json_value_get_array (const JSON_Value *value);
|
||||
const char * json_value_get_string (const JSON_Value *value);
|
||||
double json_value_get_number (const JSON_Value *value);
|
||||
int json_value_get_boolean(const JSON_Value *value);
|
||||
void json_value_free (JSON_Value *value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
52
src/pcm_reader.h
Normal file
52
src/pcm_reader.h
Normal 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
|
90
src/pcm_sint16_converter.c
Normal file
90
src/pcm_sint16_converter.c
Normal 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;
|
||||
}
|
@ -6,6 +6,8 @@
|
||||
# include "config.h"
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <float.h>
|
||||
#include <time.h>
|
||||
#if HAVE_STDINT_H
|
||||
# include <stdint.h>
|
||||
@ -51,11 +53,14 @@ void aacenc_progress_init(aacenc_progress_t *progress, int64_t total,
|
||||
void aacenc_progress_update(aacenc_progress_t *progress, int64_t current,
|
||||
int period)
|
||||
{
|
||||
int percent = 100.0 * current / progress->total + .5;
|
||||
double seconds = current / progress->timescale;
|
||||
double ellapsed = (aacenc_timer() - progress->start) / 1000.0;
|
||||
double eta = ellapsed * (progress->total / (double)current - 1.0);
|
||||
double speed = ellapsed ? seconds / ellapsed : 0.0;
|
||||
double speed = ellapsed ? seconds / ellapsed : 1.0;
|
||||
int percent = progress->total ? 100.0 * current / progress->total + .5
|
||||
: 100;
|
||||
double eta = current ? ellapsed * (progress->total / (double)current - 1.0)
|
||||
: progress->total ? DBL_MAX : 0;
|
||||
|
||||
if (current < progress->processed + period)
|
||||
return;
|
||||
|
||||
@ -80,7 +85,7 @@ void aacenc_progress_finish(aacenc_progress_t *progress, int64_t current)
|
||||
double ellapsed = (aacenc_timer() - progress->start) / 1000.0;
|
||||
aacenc_progress_update(progress, current, 0);
|
||||
if (progress->total == INT64_MAX)
|
||||
fprintf(stderr, "\n%" PRId64 "samples processed in ", current);
|
||||
fprintf(stderr, "\n%" PRId64 " samples processed in ", current);
|
||||
else
|
||||
fprintf(stderr, "\n%" PRId64 "/%" PRId64 " samples processed in ",
|
||||
current, progress->total);
|
||||
|
121
src/wav_reader.c
121
src/wav_reader.c
@ -35,9 +35,11 @@
|
||||
} while (0)
|
||||
|
||||
struct wav_reader_t {
|
||||
pcm_reader_vtbl_t *vtbl;
|
||||
pcm_sample_description_t sample_format;
|
||||
int64_t length;
|
||||
int64_t position;
|
||||
int32_t data_offset;
|
||||
int ignore_length;
|
||||
int last_error;
|
||||
wav_io_context_t io;
|
||||
@ -51,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
|
||||
};
|
||||
|
||||
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);
|
||||
*reader = 0;
|
||||
@ -91,7 +93,7 @@ int riff_read(wav_reader_t *reader, void *buffer, uint32_t size)
|
||||
}
|
||||
|
||||
static
|
||||
int riff_skip(wav_reader_t *reader, uint32_t count)
|
||||
int riff_skip(wav_reader_t *reader, int64_t count)
|
||||
{
|
||||
char buff[8192];
|
||||
int rc;
|
||||
@ -114,6 +116,37 @@ int riff_skip(wav_reader_t *reader, uint32_t count)
|
||||
return reader->last_error ? -1 : 0;
|
||||
}
|
||||
|
||||
static
|
||||
int riff_seek(wav_reader_t *reader, int64_t off, int whence)
|
||||
{
|
||||
int rc;
|
||||
if (reader->last_error)
|
||||
return -1;
|
||||
if (!reader->io.seek)
|
||||
goto FAIL;
|
||||
if ((rc = reader->io.seek(reader->io_cookie, off, whence)) < 0)
|
||||
goto FAIL;
|
||||
return 0;
|
||||
FAIL:
|
||||
reader->last_error = WAV_IO_ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static
|
||||
int64_t riff_tell(wav_reader_t *reader)
|
||||
{
|
||||
int64_t off;
|
||||
|
||||
if (reader->last_error || !reader->io.tell)
|
||||
return -1;
|
||||
off = reader->io.tell(reader->io_cookie);
|
||||
if (off < 0) {
|
||||
reader->last_error = WAV_IO_ERROR;
|
||||
return -1;
|
||||
}
|
||||
return off;
|
||||
}
|
||||
|
||||
static
|
||||
int riff_read16(wav_reader_t *reader, uint16_t *value)
|
||||
{
|
||||
@ -181,10 +214,12 @@ uint32_t riff_next_chunk(wav_reader_t *reader, uint32_t *chunk_size)
|
||||
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;
|
||||
unsigned nbytes;
|
||||
wav_reader_t *reader = (wav_reader_t *)preader;
|
||||
|
||||
if (!reader->ignore_length && nframes > reader->length - reader->position)
|
||||
nframes = reader->length - reader->position;
|
||||
@ -209,9 +244,8 @@ int riff_ds64(wav_reader_t *reader, int64_t *length)
|
||||
fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28);
|
||||
TRY_IO(riff_scan(reader, "QQQL",
|
||||
&riff_size, length, &sample_count, &table_size) != 4);
|
||||
if (table_size == 0)
|
||||
return 0;
|
||||
reader->last_error = WAV_UNSUPPORTED_FORMAT;
|
||||
TRY_IO(riff_skip(reader, (chunk_size - 27) & ~1));
|
||||
reader->data_offset += (chunk_size + 9) & ~1;
|
||||
FAIL:
|
||||
return -1;
|
||||
}
|
||||
@ -290,6 +324,8 @@ int wav_parse(wav_reader_t *reader, int64_t *data_length)
|
||||
TRY_IO(riff_read32(reader, &fcc));
|
||||
if (fcc != RIFF_FOURCC('W','A','V','E'))
|
||||
goto FAIL;
|
||||
reader->data_offset = 12;
|
||||
|
||||
if (container == RIFF_FOURCC('R','F','6','4'))
|
||||
riff_ds64(reader, data_length);
|
||||
while ((fcc = riff_next_chunk(reader, &chunk_size)) != 0) {
|
||||
@ -299,9 +335,12 @@ int wav_parse(wav_reader_t *reader, int64_t *data_length)
|
||||
} else if (fcc == RIFF_FOURCC('d','a','t','a')) {
|
||||
if (container == RIFF_FOURCC('R','I','F','F'))
|
||||
*data_length = chunk_size;
|
||||
reader->data_offset += 8;
|
||||
break;
|
||||
} else
|
||||
} else {
|
||||
TRY_IO(riff_skip(reader, (chunk_size + 1) & ~1));
|
||||
}
|
||||
reader->data_offset += (chunk_size + 9) & ~1;
|
||||
}
|
||||
if (fcc == RIFF_FOURCC('d','a','t','a'))
|
||||
return 0;
|
||||
@ -309,11 +348,20 @@ FAIL:
|
||||
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)
|
||||
{
|
||||
wav_reader_t *reader;
|
||||
wav_reader_t *reader = 0;
|
||||
int64_t data_length;
|
||||
unsigned bpf;
|
||||
|
||||
if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
|
||||
return 0;
|
||||
@ -324,10 +372,45 @@ wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
|
||||
free(reader);
|
||||
return 0;
|
||||
}
|
||||
if (ignore_length || !data_length ||
|
||||
data_length % reader->sample_format.bytes_per_frame != 0)
|
||||
bpf = reader->sample_format.bytes_per_frame;
|
||||
if (ignore_length || !data_length || data_length % bpf)
|
||||
reader->length = INT64_MAX;
|
||||
else
|
||||
reader->length = data_length / reader->sample_format.bytes_per_frame;
|
||||
return reader;
|
||||
reader->length = data_length / bpf;
|
||||
|
||||
if (reader->length == INT64_MAX && reader->io.seek && reader->io.tell) {
|
||||
if (reader->io.seek(reader->io_cookie, 0, SEEK_END) >= 0) {
|
||||
int64_t size = reader->io.tell(reader->io_cookie);
|
||||
if (size > 0)
|
||||
reader->length = (size - reader->data_offset) / bpf;
|
||||
reader->io.seek(reader->io_cookie, reader->data_offset, SEEK_SET);
|
||||
}
|
||||
}
|
||||
reader->vtbl = &wav_vtable;
|
||||
return (pcm_reader_t *)reader;
|
||||
}
|
||||
|
||||
pcm_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie,
|
||||
const pcm_sample_description_t *desc)
|
||||
{
|
||||
wav_reader_t *reader = 0;
|
||||
|
||||
if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
|
||||
return 0;
|
||||
memcpy(&reader->io, io_ctx, sizeof(wav_io_context_t));
|
||||
memcpy(&reader->sample_format, desc, sizeof(pcm_sample_description_t));
|
||||
reader->io_cookie = io_cookie;
|
||||
if (io_ctx->seek && io_ctx->tell) {
|
||||
if (reader->io.seek(reader->io_cookie, 0, SEEK_END) >= 0) {
|
||||
int64_t size = reader->io.tell(reader->io_cookie);
|
||||
if (size > 0)
|
||||
reader->length = size / desc->bytes_per_frame;
|
||||
reader->io.seek(reader->io_cookie, reader->data_offset, SEEK_SET);
|
||||
}
|
||||
} else
|
||||
reader->length = INT64_MAX;
|
||||
reader->vtbl = &wav_vtable;
|
||||
return (pcm_reader_t *)reader;
|
||||
}
|
||||
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
#define WAV_READER_H
|
||||
|
||||
#include "lpcm.h"
|
||||
#include "pcm_reader.h"
|
||||
|
||||
enum wav_error_code {
|
||||
WAV_IO_ERROR = 1,
|
||||
@ -16,20 +17,19 @@ enum wav_error_code {
|
||||
|
||||
typedef int (*wav_read_callback)(void *cookie, void *data, uint32_t size);
|
||||
typedef int (*wav_seek_callback)(void *cookie, int64_t off, int whence);
|
||||
typedef int64_t (*wav_tell_callback)(void *cookie);
|
||||
|
||||
typedef struct wav_io_context_t {
|
||||
wav_read_callback read;
|
||||
wav_seek_callback seek;
|
||||
wav_tell_callback tell;
|
||||
} wav_io_context_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);
|
||||
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);
|
||||
pcm_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie,
|
||||
const pcm_sample_description_t *desc);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user