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

24 Commits

Author SHA1 Message Date
5af13f7d79 bump version 2013-01-19 18:14:00 +09:00
5c534696a9 fix crash on wrong long option, rename --ignore-length to --ignorelength 2013-01-19 18:12:49 +09:00
33e12bc4f5 bump version 2013-01-17 17:12:06 +09:00
bfb6aa3cb8 compat_win32: free argv with atexit() 2013-01-17 17:11:19 +09:00
360cf7dc8b take care of COPYRIGHT-SIGN in UTF-8 2013-01-17 17:10:21 +09:00
43ab433178 bump version 2013-01-15 21:20:18 +09:00
47c164c85a fix return type of put_type_entry() to void 2013-01-15 21:19:32 +09:00
d52b8bbf13 add ADTS header size(7) to output byte length 2013-01-15 21:18:31 +09:00
f67cfe227e fix mp4 duration & version calcuration 2013-01-13 00:58:38 +09:00
e476728e77 add support for xid 2013-01-11 22:17:28 +09:00
dac71de305 support for i686-pc-mingw32 (missing _vscprintf) 2013-01-11 17:33:54 +09:00
61b6a9e383 bump version 2013-01-10 12:18:56 +09:00
5888fddccf rename basename() -> aacenc_basename() and move to compat layer 2013-01-10 11:14:16 +09:00
c5c459082a add --tag and --long-tag 2013-01-10 10:58:19 +09:00
00799c5e44 fix corner case of progress display 2013-01-09 16:39:39 +09:00
6887939854 calculate length from file size 2013-01-09 11:37:25 +09:00
6854317606 raw input support 2013-01-09 00:12:55 +09:00
8960a17764 insert a white space in progress message 2013-01-08 22:11:10 +09:00
34b319e08b fix typo of bitrate-mode option 2013-01-07 20:11:52 +09:00
158dc13cc8 more static inlining (missed on the previous commit) 2013-01-07 13:24:09 +09:00
5e1168a4dd check error of fread() and fwrite() 2013-01-07 13:14:46 +09:00
01993d6774 change inline->static inline to follow C99 semantics (for Clang) 2013-01-07 12:54:50 +09:00
9ad6264b3c explicitly add -lfdk-aac to LDADD in Makefile.am 2013-01-07 11:36:41 +09:00
7db9e2768d add some files to EXTRA_DIST in Makefile.am 2013-01-07 11:25:22 +09:00
14 changed files with 561 additions and 141 deletions

View File

@ -12,7 +12,7 @@ fdkaac_SOURCES = \
src/wav_reader.c src/wav_reader.c
fdkaac_LDADD = \ fdkaac_LDADD = \
@LIBICONV@ -lm @LIBICONV@ -lfdk-aac -lm
if FDK_PLATFORM_POSIX if FDK_PLATFORM_POSIX
fdkaac_SOURCES += \ fdkaac_SOURCES += \
@ -31,6 +31,10 @@ if FDK_NO_GETOPT_LONG
endif endif
EXTRA_DIST = \ EXTRA_DIST = \
m4/.gitkeep \
src/*.h \ src/*.h \
missings/*.c \ missings/*.c \
missings/*.h missings/*.h \
MSVC/*.vcxproj \
MSVC/*.vcxproj.filters \
MSVC/*.sln

View File

@ -32,10 +32,10 @@ AC_CHECK_TYPES([ptrdiff_t])
AC_SYS_LARGEFILE AC_SYS_LARGEFILE
AC_FUNC_FSEEKO AC_FUNC_FSEEKO
AC_CHECK_FUNCS([gettimeofday nl_langinfo strdup]) AC_CHECK_FUNCS([gettimeofday nl_langinfo strdup _vscprintf])
AC_CHECK_FUNC(getopt_long) AC_CHECK_FUNC(getopt_long)
AM_CONDITIONAL([FDK_NO_GETOPT_LONG],[test "$ac_cv_func_getopt_long" != "yes"]) AM_CONDITIONAL([FDK_NO_GETOPT_LONG],[test "$ac_cv_func_getopt_long" != "yes"])
AC_SEARCH_LIBS([aacEncOpen],[fdk-aac]) AC_SEARCH_LIBS([aacEncOpen],[fdk-aac],[],[],[])
AC_CANONICAL_HOST AC_CANONICAL_HOST

View File

@ -142,7 +142,7 @@ int aac_encode_frame(HANDLE_AACENCODER encoder,
unsigned channel_mode, obytes; unsigned channel_mode, obytes;
channel_mode = aacEncoder_GetParam(encoder, AACENC_CHANNELMODE); channel_mode = aacEncoder_GetParam(encoder, AACENC_CHANNELMODE);
obytes = 6144 / 8 * channel_mode; obytes = 6144 / 8 * channel_mode + 7;
if (!*output || *osize < obytes) { if (!*output || *osize < obytes) {
*osize = obytes; *osize = obytes;
*output = realloc(*output, obytes); *output = realloc(*output, obytes);

View File

@ -20,5 +20,6 @@ FILE *aacenc_fopen(const char *name, const char *mode);
void aacenc_getmainargs(int *argc, char ***argv); void aacenc_getmainargs(int *argc, char ***argv);
char *aacenc_to_utf8(const char *s); char *aacenc_to_utf8(const char *s);
int aacenc_fprintf(FILE *fp, const char *fmt, ...); int aacenc_fprintf(FILE *fp, const char *fmt, ...);
const char *aacenc_basename(const char *path);
#endif #endif

View File

@ -48,6 +48,16 @@ int aacenc_fprintf(FILE *fp, const char *fmt, ...)
return cnt; 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 #ifndef HAVE_ICONV
char *aacenc_to_utf8(const char *s) char *aacenc_to_utf8(const char *s)
{ {

View File

@ -12,6 +12,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include <assert.h>
#include <io.h> #include <io.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/timeb.h> #include <sys/timeb.h>
@ -74,6 +75,17 @@ FILE *aacenc_fopen(const char *name, const char *mode)
return fp; 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) void aacenc_getmainargs(int *argc, char ***argv)
{ {
int i; int i;
@ -85,6 +97,8 @@ void aacenc_getmainargs(int *argc, char ***argv)
for (i = 0; i < *argc; ++i) for (i = 0; i < *argc; ++i)
codepage_encode_wchar(CP_UTF8, wargv[i], &(*argv)[i]); codepage_encode_wchar(CP_UTF8, wargv[i], &(*argv)[i]);
(*argv)[*argc] = 0; (*argv)[*argc] = 0;
__aacenc_argv__ = *argv;
atexit(aacenc_free_mainargs);
} }
char *aacenc_to_utf8(const char *s) char *aacenc_to_utf8(const char *s)
@ -92,6 +106,20 @@ char *aacenc_to_utf8(const char *s)
return _strdup(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, ...) int aacenc_fprintf(FILE *fp, const char *fmt, ...)
{ {
va_list ap; va_list ap;
@ -126,3 +154,15 @@ int aacenc_fprintf(FILE *fp, const char *fmt, ...)
return cnt; 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;
}

View File

@ -34,6 +34,7 @@ inline int lrint(double x)
# endif # endif
#endif #endif
static
inline double pcm_clip(double n, double min_value, double max_value) inline double pcm_clip(double n, double min_value, double max_value)
{ {
if (n < min_value) if (n < min_value)
@ -42,6 +43,7 @@ inline double pcm_clip(double n, double min_value, double max_value)
return max_value; return max_value;
return n; return n;
} }
static
inline float pcm_i2f(int32_t n) inline float pcm_i2f(int32_t n)
{ {
union { union {
@ -51,6 +53,7 @@ inline float pcm_i2f(int32_t n)
u.ivalue = n; u.ivalue = n;
return u.fvalue; return u.fvalue;
} }
static
inline double pcm_i2d(int64_t n) inline double pcm_i2d(int64_t n)
{ {
union { union {
@ -60,99 +63,123 @@ inline double pcm_i2d(int64_t n)
u.ivalue = n; u.ivalue = n;
return u.fvalue; return u.fvalue;
} }
static
inline int16_t pcm_quantize_s32(int32_t n) inline int16_t pcm_quantize_s32(int32_t n)
{ {
n = ((n >> 15) + 1) >> 1; n = ((n >> 15) + 1) >> 1;
return (n == 0x8000) ? 0x7fff : n; return (n == 0x8000) ? 0x7fff : n;
} }
static
inline int16_t pcm_quantize_f64(double v) inline int16_t pcm_quantize_f64(double v)
{ {
return (int16_t)lrint(pcm_clip(v * 32768.0, -32768.0, 32767.0)); return (int16_t)lrint(pcm_clip(v * 32768.0, -32768.0, 32767.0));
} }
static
inline int16_t pcm_s8_to_s16(int8_t n) inline int16_t pcm_s8_to_s16(int8_t n)
{ {
return n << 8; return n << 8;
} }
static
inline int16_t pcm_u8_to_s16(uint8_t n) inline int16_t pcm_u8_to_s16(uint8_t n)
{ {
return (n << 8) ^ 0x8000; return (n << 8) ^ 0x8000;
} }
static
inline int16_t pcm_s16le_to_s16(int16_t n) inline int16_t pcm_s16le_to_s16(int16_t n)
{ {
return m4af_ltoh16(n); return m4af_ltoh16(n);
} }
static
inline int16_t pcm_s16be_to_s16(int16_t n) inline int16_t pcm_s16be_to_s16(int16_t n)
{ {
return m4af_btoh16(n); return m4af_btoh16(n);
} }
static
inline int16_t pcm_u16le_to_s16(uint16_t n) inline int16_t pcm_u16le_to_s16(uint16_t n)
{ {
return m4af_ltoh16(n) ^ 0x8000; return m4af_ltoh16(n) ^ 0x8000;
} }
static
inline int16_t pcm_u16be_to_s16(uint16_t n) inline int16_t pcm_u16be_to_s16(uint16_t n)
{ {
return m4af_btoh16(n) ^ 0x8000; return m4af_btoh16(n) ^ 0x8000;
} }
static
inline int32_t pcm_s24le_to_s32(uint8_t *p) inline int32_t pcm_s24le_to_s32(uint8_t *p)
{ {
return p[0]<<8 | p[1]<<16 | p[2]<<24; return p[0]<<8 | p[1]<<16 | p[2]<<24;
} }
static
inline int32_t pcm_s24be_to_s32(uint8_t *p) inline int32_t pcm_s24be_to_s32(uint8_t *p)
{ {
return p[0]<<24 | p[1]<<16 | p[2]<<8; return p[0]<<24 | p[1]<<16 | p[2]<<8;
} }
static
inline int32_t pcm_u24le_to_s32(uint8_t *p) inline int32_t pcm_u24le_to_s32(uint8_t *p)
{ {
return pcm_s24le_to_s32(p) ^ 0x80000000; return pcm_s24le_to_s32(p) ^ 0x80000000;
} }
static
inline int32_t pcm_u24be_to_s32(uint8_t *p) inline int32_t pcm_u24be_to_s32(uint8_t *p)
{ {
return pcm_s24be_to_s32(p) ^ 0x80000000; return pcm_s24be_to_s32(p) ^ 0x80000000;
} }
static
inline int16_t pcm_s24le_to_s16(uint8_t *p) inline int16_t pcm_s24le_to_s16(uint8_t *p)
{ {
return pcm_quantize_s32(pcm_s24le_to_s32(p)); return pcm_quantize_s32(pcm_s24le_to_s32(p));
} }
static
inline int16_t pcm_s24be_to_s16(uint8_t *p) inline int16_t pcm_s24be_to_s16(uint8_t *p)
{ {
return pcm_quantize_s32(pcm_s24be_to_s32(p)); return pcm_quantize_s32(pcm_s24be_to_s32(p));
} }
static
inline int16_t pcm_u24le_to_s16(uint8_t *p) inline int16_t pcm_u24le_to_s16(uint8_t *p)
{ {
return pcm_quantize_s32(pcm_u24le_to_s32(p)); return pcm_quantize_s32(pcm_u24le_to_s32(p));
} }
static
inline int16_t pcm_u24be_to_s16(uint8_t *p) inline int16_t pcm_u24be_to_s16(uint8_t *p)
{ {
return pcm_quantize_s32(pcm_u24be_to_s32(p)); return pcm_quantize_s32(pcm_u24be_to_s32(p));
} }
static
inline int16_t pcm_s32le_to_s16(int32_t n) inline int16_t pcm_s32le_to_s16(int32_t n)
{ {
return pcm_quantize_s32(m4af_ltoh32(n)); return pcm_quantize_s32(m4af_ltoh32(n));
} }
static
inline int16_t pcm_s32be_to_s16(int32_t n) inline int16_t pcm_s32be_to_s16(int32_t n)
{ {
return pcm_quantize_s32(m4af_btoh32(n)); return pcm_quantize_s32(m4af_btoh32(n));
} }
static
inline int16_t pcm_u32le_to_s16(int32_t n) inline int16_t pcm_u32le_to_s16(int32_t n)
{ {
return pcm_quantize_s32(m4af_ltoh32(n) ^ 0x80000000); return pcm_quantize_s32(m4af_ltoh32(n) ^ 0x80000000);
} }
static
inline int16_t pcm_u32be_to_s16(int32_t n) inline int16_t pcm_u32be_to_s16(int32_t n)
{ {
return pcm_quantize_s32(m4af_btoh32(n) ^ 0x80000000); return pcm_quantize_s32(m4af_btoh32(n) ^ 0x80000000);
} }
static
inline int16_t pcm_f32le_to_s16(int32_t n) inline int16_t pcm_f32le_to_s16(int32_t n)
{ {
return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n))); return pcm_quantize_f64(pcm_i2f(m4af_ltoh32(n)));
} }
static
inline int16_t pcm_f32be_to_s16(int32_t n) inline int16_t pcm_f32be_to_s16(int32_t n)
{ {
return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n))); return pcm_quantize_f64(pcm_i2f(m4af_btoh32(n)));
} }
static
inline int16_t pcm_f64le_to_s16(int64_t n) inline int16_t pcm_f64le_to_s16(int64_t n)
{ {
return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n))); return pcm_quantize_f64(pcm_i2d(m4af_ltoh64(n)));
} }
static
inline int16_t pcm_f64be_to_s16(int64_t n) inline int16_t pcm_f64be_to_s16(int64_t n)
{ {
return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n))); return pcm_quantize_f64(pcm_i2d(m4af_btoh64(n)));

View File

@ -456,12 +456,18 @@ int m4af_add_itmf_int16_tag(m4af_writer_t *ctx, uint32_t type, int value)
return m4af_add_itmf_short_tag(ctx, type, M4AF_INTEGER, &data, 2); return m4af_add_itmf_short_tag(ctx, type, M4AF_INTEGER, &data, 2);
} }
int m4af_add_itmf_int32_tag(m4af_writer_t *ctx, uint32_t type, int value) int m4af_add_itmf_int32_tag(m4af_writer_t *ctx, uint32_t type, uint32_t value)
{ {
uint32_t data = m4af_htob32(value); uint32_t data = m4af_htob32(value);
return m4af_add_itmf_short_tag(ctx, type, M4AF_INTEGER, &data, 4); return m4af_add_itmf_short_tag(ctx, type, M4AF_INTEGER, &data, 4);
} }
int m4af_add_itmf_int64_tag(m4af_writer_t *ctx, uint32_t type, uint64_t value)
{
uint64_t data = m4af_htob64(value);
return m4af_add_itmf_short_tag(ctx, type, M4AF_INTEGER, &data, 8);
}
int m4af_add_itmf_track_tag(m4af_writer_t *ctx, int track, int total) int m4af_add_itmf_track_tag(m4af_writer_t *ctx, int track, int total)
{ {
uint16_t data[4] = { 0 }; uint16_t data[4] = { 0 };
@ -509,15 +515,6 @@ void m4af_update_size(m4af_writer_t *ctx, int64_t pos)
m4af_set_pos(ctx, current_pos); m4af_set_pos(ctx, current_pos);
} }
static
int m4af_head_version(m4af_writer_t *ctx, int track_idx)
{
m4af_track_t *track = &ctx->track[track_idx];
return track->duration > UINT32_MAX
|| track->creation_time > UINT32_MAX
|| track->modification_time > UINT32_MAX;
}
static static
void m4af_descriptor(m4af_writer_t *ctx, uint32_t tag, uint32_t size) void m4af_descriptor(m4af_writer_t *ctx, uint32_t tag, uint32_t size)
{ {
@ -829,7 +826,9 @@ void m4af_mdhd_box(m4af_writer_t *ctx, int track_idx)
{ {
m4af_track_t *track = &ctx->track[track_idx]; m4af_track_t *track = &ctx->track[track_idx];
int64_t pos = m4af_tell(ctx); int64_t pos = m4af_tell(ctx);
uint8_t version = m4af_head_version(ctx, track_idx); uint8_t version = (track->creation_time > UINT32_MAX ||
track->modification_time > UINT32_MAX ||
track->duration > UINT32_MAX);
m4af_write(ctx, "\0\0\0\0mdhd", 8); m4af_write(ctx, "\0\0\0\0mdhd", 8);
m4af_write(ctx, &version, 1); m4af_write(ctx, &version, 1);
@ -892,7 +891,11 @@ void m4af_tkhd_box(m4af_writer_t *ctx, int track_idx)
{ {
m4af_track_t *track = &ctx->track[track_idx]; m4af_track_t *track = &ctx->track[track_idx];
int64_t pos = m4af_tell(ctx); int64_t pos = m4af_tell(ctx);
uint8_t version = m4af_head_version(ctx, track_idx); int64_t duration =
(double)track->duration / track->timescale * ctx->timescale + .5;
uint8_t version = (track->creation_time > UINT32_MAX ||
track->modification_time > UINT32_MAX ||
duration > UINT32_MAX);
m4af_write(ctx, "\0\0\0\0tkhd", 8); m4af_write(ctx, "\0\0\0\0tkhd", 8);
m4af_write(ctx, &version, 1); m4af_write(ctx, &version, 1);
m4af_write(ctx, "\0\0\007", 3); /* flags */ m4af_write(ctx, "\0\0\007", 3); /* flags */
@ -902,14 +905,14 @@ void m4af_tkhd_box(m4af_writer_t *ctx, int track_idx)
m4af_write32(ctx, track_idx + 1); m4af_write32(ctx, track_idx + 1);
m4af_write(ctx, "\0\0\0\0" /* reserved */ m4af_write(ctx, "\0\0\0\0" /* reserved */
, 4); , 4);
m4af_write64(ctx, track->duration); m4af_write64(ctx, duration);
} else { } else {
m4af_write32(ctx, track->creation_time); m4af_write32(ctx, track->creation_time);
m4af_write32(ctx, track->modification_time); m4af_write32(ctx, track->modification_time);
m4af_write32(ctx, track_idx + 1); m4af_write32(ctx, track_idx + 1);
m4af_write(ctx, "\0\0\0\0" /* reserved */ m4af_write(ctx, "\0\0\0\0" /* reserved */
, 4); , 4);
m4af_write32(ctx, track->duration); m4af_write32(ctx, duration);
} }
m4af_write(ctx, m4af_write(ctx,
"\0\0\0\0" /* reserved[0] */ "\0\0\0\0" /* reserved[0] */
@ -950,7 +953,7 @@ int64_t m4af_movie_duration(m4af_writer_t *ctx)
unsigned i; unsigned i;
for (i = 0; i < ctx->num_tracks; ++i) { for (i = 0; i < ctx->num_tracks; ++i) {
double x = ctx->track[i].duration; double x = ctx->track[i].duration;
int64_t duration = x * ctx->track[i].timescale / ctx->timescale + .5; int64_t duration = x / ctx->track[i].timescale * ctx->timescale + .5;
if (duration > movie_duration) if (duration > movie_duration)
movie_duration = duration; movie_duration = duration;
} }
@ -961,8 +964,10 @@ static
void m4af_mvhd_box(m4af_writer_t *ctx) void m4af_mvhd_box(m4af_writer_t *ctx)
{ {
int64_t pos = m4af_tell(ctx); int64_t pos = m4af_tell(ctx);
uint8_t version = m4af_head_version(ctx, 0);
int64_t movie_duration = m4af_movie_duration(ctx); int64_t movie_duration = m4af_movie_duration(ctx);
uint8_t version = (ctx->creation_time > UINT32_MAX ||
ctx->modification_time > UINT32_MAX ||
movie_duration > UINT32_MAX);
m4af_write(ctx, "\0\0\0\0mvhd", 8); m4af_write(ctx, "\0\0\0\0mvhd", 8);
m4af_write(ctx, &version, 1); m4af_write(ctx, &version, 1);

View File

@ -103,7 +103,9 @@ int m4af_add_itmf_int8_tag(m4af_writer_t *ctx, uint32_t type, int value);
int m4af_add_itmf_int16_tag(m4af_writer_t *ctx, uint32_t type, int value); int m4af_add_itmf_int16_tag(m4af_writer_t *ctx, uint32_t type, int value);
int m4af_add_itmf_int32_tag(m4af_writer_t *ctx, uint32_t type, int value); int m4af_add_itmf_int32_tag(m4af_writer_t *ctx, uint32_t type, uint32_t value);
int m4af_add_itmf_int64_tag(m4af_writer_t *ctx, uint32_t type, uint64_t value);
int m4af_add_itmf_track_tag(m4af_writer_t *ctx, int track, int total); int m4af_add_itmf_track_tag(m4af_writer_t *ctx, int track, int total);

View File

@ -8,9 +8,15 @@
#if HAVE_STDINT_H #if HAVE_STDINT_H
# include <stdint.h> # include <stdint.h>
#endif #endif
#if HAVE_INTTYPES_H
# include <inttypes.h>
#elif defined(_MSC_VER)
# define SCNd64 "I64d"
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <locale.h> #include <locale.h>
#include <errno.h> #include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -33,13 +39,15 @@
static static
int read_callback(void *cookie, void *data, uint32_t size) int read_callback(void *cookie, void *data, uint32_t size)
{ {
return fread(data, 1, size, (FILE*)cookie); size_t rc = fread(data, 1, size, (FILE*)cookie);
return ferror((FILE*)cookie) ? -1 : (int)rc;
} }
static static
int write_callback(void *cookie, const void *data, uint32_t size) int write_callback(void *cookie, const void *data, uint32_t size)
{ {
return fwrite(data, 1, size, (FILE*)cookie); size_t rc = fwrite(data, 1, size, (FILE*)cookie);
return ferror((FILE*)cookie) ? -1 : (int)rc;
} }
static static
@ -95,12 +103,26 @@ PROGNAME " %s\n"
" 6: LATM MCP=1\n" " 6: LATM MCP=1\n"
" 7: LATM MCP=0\n" " 7: LATM MCP=0\n"
" 10: LOAS/LATM (LATM within LOAS)\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" " -h, --header-period <n> StreamMuxConfig/PCE repetition period in\n"
" transport layer\n" " transport layer\n"
"\n" "\n"
" -o <filename> Output filename\n" " -o <filename> Output filename\n"
" --ignore-length Ignore length of WAV header\n" " --ignorelength Ignore length of WAV header\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" "\n"
"Tagging options:\n" "Tagging options:\n"
" --title <string>\n" " --title <string>\n"
@ -115,12 +137,16 @@ PROGNAME " %s\n"
" --track <number[/total]>\n" " --track <number[/total]>\n"
" --disk <number[/total]>\n" " --disk <number[/total]>\n"
" --tempo <n>\n" " --tempo <n>\n"
" --tag <fcc>:<value> Set iTunes predefined tag with four char code.\n"
" --long-tag <name>:<value> Set arbitrary tag as iTunes custom metadata.\n"
, fdkaac_version); , fdkaac_version);
} }
typedef struct aacenc_tag_entry_t { typedef struct aacenc_tag_entry_t {
uint32_t tag; uint32_t tag;
const char *name;
const char *data; const char *data;
uint32_t data_size;
} aacenc_tag_entry_t; } aacenc_tag_entry_t;
typedef struct aacenc_param_ex_t { typedef struct aacenc_param_ex_t {
@ -130,32 +156,68 @@ typedef struct aacenc_param_ex_t {
char *output_filename; char *output_filename;
unsigned ignore_length; unsigned ignore_length;
int is_raw;
unsigned raw_channels;
unsigned raw_rate;
const char *raw_format;
aacenc_tag_entry_t *tag_table; aacenc_tag_entry_t *tag_table;
unsigned tag_count; unsigned tag_count;
unsigned tag_table_capacity; unsigned tag_table_capacity;
} aacenc_param_ex_t; } aacenc_param_ex_t;
static
void param_add_itmf_entry(aacenc_param_ex_t *params, uint32_t tag,
const char *key, const char *value, uint32_t size)
{
aacenc_tag_entry_t *entry;
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;
params->tag_count++;
}
static static
int parse_options(int argc, char **argv, aacenc_param_ex_t *params) int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
{ {
int ch; int ch;
unsigned n; unsigned n;
aacenc_tag_entry_t *tag;
#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_LONG_TAG M4AF_FOURCC('l','t','a','g')
static struct option long_options[] = { static struct option long_options[] = {
{ "help", no_argument, 0, 'h' }, { "help", no_argument, 0, 'h' },
{ "profile", required_argument, 0, 'p' }, { "profile", required_argument, 0, 'p' },
{ "bitrate", required_argument, 0, 'b' }, { "bitrate", required_argument, 0, 'b' },
{ "biterate-mode", required_argument, 0, 'm' }, { "bitrate-mode", required_argument, 0, 'm' },
{ "bandwidth", required_argument, 0, 'w' }, { "bandwidth", required_argument, 0, 'w' },
{ "afterburner", required_argument, 0, 'a' }, { "afterburner", required_argument, 0, 'a' },
{ "lowdelay-sbr", no_argument, 0, 'L' }, { "lowdelay-sbr", no_argument, 0, 'L' },
{ "sbr-signaling", required_argument, 0, 's' }, { "sbr-signaling", required_argument, 0, 's' },
{ "transport-format", required_argument, 0, 'f' }, { "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' }, { "header-period", required_argument, 0, 'P' },
{ "ignore-length", no_argument, 0, 'I' }, { "ignorelength", no_argument, 0, 'I' },
{ "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 }, { "title", required_argument, 0, M4AF_TAG_TITLE },
{ "artist", required_argument, 0, M4AF_TAG_ARTIST }, { "artist", required_argument, 0, M4AF_TAG_ARTIST },
@ -169,11 +231,14 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
{ "track", required_argument, 0, M4AF_TAG_TRACK }, { "track", required_argument, 0, M4AF_TAG_TRACK },
{ "disk", required_argument, 0, M4AF_TAG_DISK }, { "disk", required_argument, 0, M4AF_TAG_DISK },
{ "tempo", required_argument, 0, M4AF_TAG_TEMPO }, { "tempo", required_argument, 0, M4AF_TAG_TEMPO },
{ "tag", required_argument, 0, OPT_SHORT_TAG },
{ "long-tag", required_argument, 0, OPT_LONG_TAG },
{ 0, 0, 0, 0 },
}; };
params->afterburner = 1; params->afterburner = 1;
aacenc_getmainargs(&argc, &argv); 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:Io:R",
long_options, 0)) != EOF) { long_options, 0)) != EOF) {
switch (ch) { switch (ch) {
case 'h': case 'h':
@ -246,6 +311,26 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
case 'I': case 'I':
params->ignore_length = 1; params->ignore_length = 1;
break; 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_TITLE:
case M4AF_TAG_ARTIST: case M4AF_TAG_ARTIST:
case M4AF_TAG_ALBUM: case M4AF_TAG_ALBUM:
@ -258,18 +343,40 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
case M4AF_TAG_TRACK: case M4AF_TAG_TRACK:
case M4AF_TAG_DISK: case M4AF_TAG_DISK:
case M4AF_TAG_TEMPO: case M4AF_TAG_TEMPO:
if (params->tag_count == params->tag_table_capacity) { param_add_itmf_entry(params, ch, 0, optarg, strlen(optarg));
unsigned newsize = params->tag_table_capacity; break;
newsize = newsize ? newsize * 2 : 1; case OPT_SHORT_TAG:
params->tag_table = case OPT_LONG_TAG:
realloc(params->tag_table, {
newsize * sizeof(aacenc_tag_entry_t)); char *val;
params->tag_table_capacity = newsize; 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) {
/*
* 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));
}
param_add_itmf_entry(params, fcc, optarg, val, strlen(val));
} }
tag = params->tag_table + params->tag_count;
tag->tag = ch;
tag->data = optarg;
params->tag_count++;
break; break;
default: default:
return usage(), -1; return usage(), -1;
@ -277,6 +384,7 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
} }
if (argc == optind) if (argc == optind)
return usage(), -1; return usage(), -1;
if (!params->bitrate && !params->bitrate_mode) { if (!params->bitrate && !params->bitrate_mode) {
fprintf(stderr, "bitrate or bitrate-mode is mandatory\n"); fprintf(stderr, "bitrate or bitrate-mode is mandatory\n");
return -1; return -1;
@ -288,6 +396,15 @@ int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
} }
if (params->bitrate && params->bitrate < 10000) if (params->bitrate && params->bitrate < 10000)
params->bitrate *= 1000; 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]; params->input_filename = argv[optind];
return 0; return 0;
}; };
@ -297,7 +414,8 @@ int write_sample(FILE *ofp, m4af_writer_t *m4af,
const void *data, uint32_t size, uint32_t duration) const void *data, uint32_t size, uint32_t duration)
{ {
if (!m4af) { if (!m4af) {
if (fwrite(data, 1, size, ofp) < 0) { fwrite(data, 1, size, ofp);
if (ferror(ofp)) {
fprintf(stderr, "ERROR: fwrite(): %s\n", strerror(errno)); fprintf(stderr, "ERROR: fwrite(): %s\n", strerror(errno));
return -1; return -1;
} }
@ -361,43 +479,122 @@ END:
} }
static static
int finalize_m4a(m4af_writer_t *m4af, const aacenc_param_ex_t *params, void put_tag_entry(m4af_writer_t *m4af, const aacenc_tag_entry_t *tag)
HANDLE_AACENCODER encoder)
{
unsigned i;
aacenc_tag_entry_t *tag = params->tag_table;
for (i = 0; i < params->tag_count; ++i, ++tag) {
switch (tag->tag) {
case M4AF_TAG_TRACK:
{ {
unsigned m, n = 0; unsigned m, n = 0;
switch (tag->tag) {
case M4AF_TAG_TRACK:
if (sscanf(tag->data, "%u/%u", &m, &n) >= 1) if (sscanf(tag->data, "%u/%u", &m, &n) >= 1)
m4af_add_itmf_track_tag(m4af, m, n); m4af_add_itmf_track_tag(m4af, m, n);
break; break;
}
case M4AF_TAG_DISK: case M4AF_TAG_DISK:
{
unsigned m, n = 0;
if (sscanf(tag->data, "%u/%u", &m, &n) >= 1) if (sscanf(tag->data, "%u/%u", &m, &n) >= 1)
m4af_add_itmf_disk_tag(m4af, m, n); m4af_add_itmf_disk_tag(m4af, m, n);
break; break;
} case M4AF_TAG_GENRE_ID3:
if (sscanf(tag->data, "%u", &n) == 1)
m4af_add_itmf_genre_tag(m4af, n);
break;
case M4AF_TAG_TEMPO: case M4AF_TAG_TEMPO:
{
unsigned n;
if (sscanf(tag->data, "%u", &n) == 1) if (sscanf(tag->data, "%u", &n) == 1)
m4af_add_itmf_int16_tag(m4af, tag->tag, n); m4af_add_itmf_int16_tag(m4af, tag->tag, n);
break; 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(tag->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(tag->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(tag->data, "%" SCNd64, &qn) == 1)
m4af_add_itmf_int64_tag(m4af, tag->tag, qn);
break;
} }
default: case M4AF_TAG_ARTWORK:
{
int data_type = 0;
if (!memcmp(tag->data, "GIF", 3))
data_type = M4AF_GIF;
else if (!memcmp(tag->data, "\xff\xd8\xff", 3))
data_type = M4AF_JPEG;
else if (!memcmp(tag->data, "\x89PNG", 4))
data_type = M4AF_PNG;
if (data_type)
m4af_add_itmf_short_tag(m4af, tag->tag, data_type,
tag->data, tag->data_size);
break;
}
case M4AF_FOURCC('-','-','-','-'):
{
char *u8 = aacenc_to_utf8(tag->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(tag->data); char *u8 = aacenc_to_utf8(tag->data);
m4af_add_itmf_string_tag(m4af, tag->tag, u8); m4af_add_itmf_string_tag(m4af, tag->tag, u8);
free(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);
} }
} }
}
static
void put_tool_tag(m4af_writer_t *m4af, const aacenc_param_ex_t *params,
HANDLE_AACENCODER encoder)
{ {
char tool_info[256]; char tool_info[256];
char *p = tool_info; char *p = tool_info;
@ -407,6 +604,7 @@ int finalize_m4a(m4af_writer_t *m4af, const aacenc_param_ex_t *params,
lib_info = calloc(FDK_MODULE_LAST, sizeof(LIB_INFO)); lib_info = calloc(FDK_MODULE_LAST, sizeof(LIB_INFO));
if (aacEncGetLibInfo(lib_info) == AACENC_OK) { if (aacEncGetLibInfo(lib_info) == AACENC_OK) {
int i;
for (i = 0; i < FDK_MODULE_LAST; ++i) for (i = 0; i < FDK_MODULE_LAST; ++i)
if (lib_info[i].module_id == FDK_AACENC) if (lib_info[i].module_id == FDK_AACENC)
break; break;
@ -421,6 +619,18 @@ int finalize_m4a(m4af_writer_t *m4af, const aacenc_param_ex_t *params,
m4af_add_itmf_string_tag(m4af, M4AF_TAG_TOOL, tool_info); m4af_add_itmf_string_tag(m4af, M4AF_TAG_TOOL, tool_info);
} }
static
int finalize_m4a(m4af_writer_t *m4af, const aacenc_param_ex_t *params,
HANDLE_AACENCODER encoder)
{
unsigned i;
aacenc_tag_entry_t *tag = params->tag_table;
for (i = 0; i < params->tag_count; ++i, ++tag)
put_tag_entry(m4af, tag);
put_tool_tag(m4af, params, encoder);
if (m4af_finalize(m4af) < 0) { if (m4af_finalize(m4af) < 0) {
fprintf(stderr, "ERROR: failed to finalize m4a\n"); fprintf(stderr, "ERROR: failed to finalize m4a\n");
return -1; return -1;
@ -428,17 +638,6 @@ int finalize_m4a(m4af_writer_t *m4af, const aacenc_param_ex_t *params,
return 0; 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 static
char *generate_output_filename(const char *filename, const char *ext) char *generate_output_filename(const char *filename, const char *ext)
{ {
@ -449,7 +648,7 @@ char *generate_output_filename(const char *filename, const char *ext)
p = malloc(ext_len + 6); p = malloc(ext_len + 6);
sprintf(p, "stdin%s", ext); sprintf(p, "stdin%s", ext);
} else { } else {
const char *base = basename(filename); const char *base = aacenc_basename(filename);
size_t ilen = strlen(base); size_t ilen = strlen(base);
const char *ext_org = strrchr(base, '.'); const char *ext_org = strrchr(base, '.');
if (ext_org) ilen = ext_org - base; if (ext_org) ilen = ext_org - base;
@ -459,9 +658,45 @@ char *generate_output_filename(const char *filename, const char *ext)
return p; 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;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
wav_io_context_t wav_io = { read_callback, seek_callback }; wav_io_context_t wav_io = { read_callback, seek_callback, tell_callback };
m4af_io_callbacks_t m4af_io = { m4af_io_callbacks_t m4af_io = {
write_callback, seek_callback, tell_callback }; write_callback, seek_callback, tell_callback };
aacenc_param_ex_t params = { 0 }; aacenc_param_ex_t params = { 0 };
@ -490,14 +725,31 @@ int main(int argc, char **argv)
strerror(errno)); strerror(errno));
goto END; goto END;
} }
if (fstat(fileno(ifp), &stb) == 0 && (stb.st_mode & S_IFMT) != S_IFREG) {
if (fstat(fileno(ifp), &stb) == 0 && (stb.st_mode & S_IFMT) != S_IFREG)
wav_io.seek = 0; wav_io.seek = 0;
wav_io.tell = 0;
}
if (!params.is_raw) {
if ((wavf = wav_open(&wav_io, ifp, params.ignore_length)) == 0) { if ((wavf = wav_open(&wav_io, ifp, params.ignore_length)) == 0) {
fprintf(stderr, "ERROR: broken / unsupported input file\n"); fprintf(stderr, "ERROR: broken / unsupported input file\n");
goto END; goto END;
} }
} else {
int bytes_per_channel;
pcm_sample_description_t desc = { 0 };
if (parse_raw_spec(params.raw_format, &desc) < 0) {
fprintf(stderr, "ERROR: invalid raw-format spec\n");
goto END;
}
desc.sample_rate = params.raw_rate;
desc.channels_per_frame = params.raw_channels;
bytes_per_channel = (desc.bits_per_channel + 7) / 8;
desc.bytes_per_frame = params.raw_channels * bytes_per_channel;
if ((wavf = raw_open(&wav_io, ifp, &desc)) == 0) {
fprintf(stderr, "ERROR: failed to open raw input\n");
goto END;
}
}
sample_format = wav_get_format(wavf); sample_format = wav_get_format(wavf);
if (aacenc_init(&encoder, (aacenc_param_t*)&params, sample_format, if (aacenc_init(&encoder, (aacenc_param_t*)&params, sample_format,

View File

@ -6,6 +6,8 @@
# include "config.h" # include "config.h"
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <limits.h>
#include <float.h>
#include <time.h> #include <time.h>
#if HAVE_STDINT_H #if HAVE_STDINT_H
# include <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, void aacenc_progress_update(aacenc_progress_t *progress, int64_t current,
int period) int period)
{ {
int percent = 100.0 * current / progress->total + .5;
double seconds = current / progress->timescale; double seconds = current / progress->timescale;
double ellapsed = (aacenc_timer() - progress->start) / 1000.0; double ellapsed = (aacenc_timer() - progress->start) / 1000.0;
double eta = ellapsed * (progress->total / (double)current - 1.0); double speed = ellapsed ? seconds / ellapsed : 1.0;
double speed = ellapsed ? seconds / ellapsed : 0.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) if (current < progress->processed + period)
return; return;

View File

@ -38,6 +38,7 @@ struct wav_reader_t {
pcm_sample_description_t sample_format; pcm_sample_description_t sample_format;
int64_t length; int64_t length;
int64_t position; int64_t position;
int32_t data_offset;
int ignore_length; int ignore_length;
int last_error; int last_error;
wav_io_context_t io; wav_io_context_t io;
@ -91,7 +92,7 @@ int riff_read(wav_reader_t *reader, void *buffer, uint32_t size)
} }
static static
int riff_skip(wav_reader_t *reader, uint32_t count) int riff_skip(wav_reader_t *reader, int64_t count)
{ {
char buff[8192]; char buff[8192];
int rc; int rc;
@ -114,6 +115,37 @@ int riff_skip(wav_reader_t *reader, uint32_t count)
return reader->last_error ? -1 : 0; 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 static
int riff_read16(wav_reader_t *reader, uint16_t *value) int riff_read16(wav_reader_t *reader, uint16_t *value)
{ {
@ -209,9 +241,8 @@ int riff_ds64(wav_reader_t *reader, int64_t *length)
fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28); fcc == RIFF_FOURCC('d','s','6','4') && chunk_size >= 28);
TRY_IO(riff_scan(reader, "QQQL", TRY_IO(riff_scan(reader, "QQQL",
&riff_size, length, &sample_count, &table_size) != 4); &riff_size, length, &sample_count, &table_size) != 4);
if (table_size == 0) TRY_IO(riff_skip(reader, (chunk_size - 27) & ~1));
return 0; reader->data_offset += (chunk_size + 9) & ~1;
reader->last_error = WAV_UNSUPPORTED_FORMAT;
FAIL: FAIL:
return -1; return -1;
} }
@ -290,6 +321,8 @@ int wav_parse(wav_reader_t *reader, int64_t *data_length)
TRY_IO(riff_read32(reader, &fcc)); TRY_IO(riff_read32(reader, &fcc));
if (fcc != RIFF_FOURCC('W','A','V','E')) if (fcc != RIFF_FOURCC('W','A','V','E'))
goto FAIL; goto FAIL;
reader->data_offset = 12;
if (container == RIFF_FOURCC('R','F','6','4')) if (container == RIFF_FOURCC('R','F','6','4'))
riff_ds64(reader, data_length); riff_ds64(reader, data_length);
while ((fcc = riff_next_chunk(reader, &chunk_size)) != 0) { while ((fcc = riff_next_chunk(reader, &chunk_size)) != 0) {
@ -299,10 +332,13 @@ int wav_parse(wav_reader_t *reader, int64_t *data_length)
} else if (fcc == RIFF_FOURCC('d','a','t','a')) { } else if (fcc == RIFF_FOURCC('d','a','t','a')) {
if (container == RIFF_FOURCC('R','I','F','F')) if (container == RIFF_FOURCC('R','I','F','F'))
*data_length = chunk_size; *data_length = chunk_size;
reader->data_offset += 8;
break; break;
} else } else {
TRY_IO(riff_skip(reader, (chunk_size + 1) & ~1)); TRY_IO(riff_skip(reader, (chunk_size + 1) & ~1));
} }
reader->data_offset += (chunk_size + 9) & ~1;
}
if (fcc == RIFF_FOURCC('d','a','t','a')) if (fcc == RIFF_FOURCC('d','a','t','a'))
return 0; return 0;
FAIL: FAIL:
@ -312,8 +348,9 @@ FAIL:
wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie, wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
int ignore_length) int ignore_length)
{ {
wav_reader_t *reader; wav_reader_t *reader = 0;
int64_t data_length; int64_t data_length;
unsigned bpf;
if ((reader = calloc(1, sizeof(wav_reader_t))) == 0) if ((reader = calloc(1, sizeof(wav_reader_t))) == 0)
return 0; return 0;
@ -324,10 +361,43 @@ wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
free(reader); free(reader);
return 0; return 0;
} }
if (ignore_length || !data_length || bpf = reader->sample_format.bytes_per_frame;
data_length % reader->sample_format.bytes_per_frame != 0) if (ignore_length || !data_length || data_length % bpf)
reader->length = INT64_MAX; reader->length = INT64_MAX;
else else
reader->length = data_length / reader->sample_format.bytes_per_frame; 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);
}
}
return reader; return reader;
} }
wav_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;
return reader;
}

View File

@ -16,16 +16,20 @@ enum wav_error_code {
typedef int (*wav_read_callback)(void *cookie, void *data, uint32_t size); 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 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 { typedef struct wav_io_context_t {
wav_read_callback read; wav_read_callback read;
wav_seek_callback seek; wav_seek_callback seek;
wav_tell_callback tell;
} wav_io_context_t; } wav_io_context_t;
typedef struct wav_reader_t wav_reader_t; typedef struct wav_reader_t wav_reader_t;
wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie, wav_reader_t *wav_open(wav_io_context_t *io_ctx, void *io_cookie,
int ignore_length); int ignore_length);
wav_reader_t *raw_open(wav_io_context_t *io_ctx, void *io_cookie,
const pcm_sample_description_t *desc);
const pcm_sample_description_t *wav_get_format(wav_reader_t *reader); const pcm_sample_description_t *wav_get_format(wav_reader_t *reader);
int wav_read_frames(wav_reader_t *reader, void *buffer, unsigned nframes); 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_length(wav_reader_t *reader);

View File

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