2013-01-04 17:10:05 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 nu774
|
|
|
|
* For conditions of distribution and use, see copyright notice in COPYING
|
|
|
|
*/
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#if HAVE_STDINT_H
|
|
|
|
# include <stdint.h>
|
|
|
|
#endif
|
2013-01-09 08:41:08 +01:00
|
|
|
#if HAVE_INTTYPES_H
|
|
|
|
# include <inttypes.h>
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
# define SCNd64 "I64d"
|
|
|
|
#endif
|
2013-01-04 17:10:05 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-01-08 16:12:55 +01:00
|
|
|
#include <ctype.h>
|
2013-01-04 17:10:05 +01:00
|
|
|
#include <locale.h>
|
|
|
|
#include <errno.h>
|
2013-01-06 05:41:31 +01:00
|
|
|
#include <sys/stat.h>
|
2013-01-04 17:10:05 +01:00
|
|
|
#include <getopt.h>
|
2013-01-06 05:41:31 +01:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2013-01-04 17:10:05 +01:00
|
|
|
#include "compat.h"
|
|
|
|
#include "wav_reader.h"
|
|
|
|
#include "aacenc.h"
|
|
|
|
#include "m4af.h"
|
|
|
|
#include "progress.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
|
|
|
#define PROGNAME "fdkaac"
|
|
|
|
|
|
|
|
static
|
|
|
|
int read_callback(void *cookie, void *data, uint32_t size)
|
|
|
|
{
|
2013-01-07 05:14:46 +01:00
|
|
|
size_t rc = fread(data, 1, size, (FILE*)cookie);
|
|
|
|
return ferror((FILE*)cookie) ? -1 : (int)rc;
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int write_callback(void *cookie, const void *data, uint32_t size)
|
|
|
|
{
|
2013-01-07 05:14:46 +01:00
|
|
|
size_t rc = fwrite(data, 1, size, (FILE*)cookie);
|
|
|
|
return ferror((FILE*)cookie) ? -1 : (int)rc;
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int seek_callback(void *cookie, int64_t off, int whence)
|
|
|
|
{
|
|
|
|
return fseeko((FILE*)cookie, off, whence);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int64_t tell_callback(void *cookie)
|
|
|
|
{
|
|
|
|
return ftello((FILE*)cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void usage(void)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
PROGNAME " %s\n"
|
|
|
|
"Usage: " PROGNAME " [options] input_file\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -h, --help Print this help message\n"
|
|
|
|
" -p, --profile <n> Profile (audio object type)\n"
|
|
|
|
" 2: MPEG-4 AAC LC (default)\n"
|
|
|
|
" 5: MPEG-4 HE-AAC (SBR)\n"
|
|
|
|
" 29: MPEG-4 HE-AAC v2 (SBR+PS)\n"
|
|
|
|
" 23: MPEG-4 AAC LD\n"
|
|
|
|
" 39: MPEG-4 AAC ELD\n"
|
|
|
|
" 129: MPEG-2 AAC LC\n"
|
|
|
|
" 132: MPEG-2 HE-AAC (SBR)\n"
|
|
|
|
" 156: MPEG-2 HE-AAC v2 (SBR+PS)\n"
|
|
|
|
" -b, --bitrate <n> Bitrate in bits per seconds (for CBR)\n"
|
|
|
|
" -m, --bitrate-mode <n> Bitrate configuration\n"
|
|
|
|
" 0: CBR (default)\n"
|
|
|
|
" 1-5: VBR\n"
|
|
|
|
" (VBR mode is not officially supported, and\n"
|
|
|
|
" works only on a certain combination of\n"
|
|
|
|
" parameter settings, sample rate, and\n"
|
|
|
|
" channel configuration)\n"
|
|
|
|
" -w, --bandwidth <n> Frequency bandwidth in Hz (AAC LC only)\n"
|
2013-01-07 02:58:09 +01:00
|
|
|
" -a, --afterburner <n> Afterburner\n"
|
2013-01-04 17:10:05 +01:00
|
|
|
" 0: Off\n"
|
|
|
|
" 1: On(default)\n"
|
|
|
|
" -L, --lowdelay-sbr Enable ELD-SBR (AAC ELD only)\n"
|
|
|
|
" -s, --sbr-signaling <n> SBR signaling mode\n"
|
|
|
|
" 0: Implicit, backward compatible(default)\n"
|
|
|
|
" 1: Explicit SBR and implicit PS\n"
|
|
|
|
" 2: Explicit hierarchical signaling\n"
|
|
|
|
" -f, --transport-format <n> Transport format\n"
|
|
|
|
" 0: RAW (default, muxed into M4A)\n"
|
|
|
|
" 1: ADIF\n"
|
|
|
|
" 2: ADTS\n"
|
|
|
|
" 6: LATM MCP=1\n"
|
|
|
|
" 7: LATM MCP=0\n"
|
|
|
|
" 10: LOAS/LATM (LATM within LOAS)\n"
|
2013-01-08 16:12:55 +01:00
|
|
|
" -C, --adts-crc-check Add CRC protection on ADTS header\n"
|
2013-01-04 17:10:05 +01:00
|
|
|
" -h, --header-period <n> StreamMuxConfig/PCE repetition period in\n"
|
|
|
|
" transport layer\n"
|
|
|
|
"\n"
|
|
|
|
" -o <filename> Output filename\n"
|
2013-01-19 10:12:49 +01:00
|
|
|
" --ignorelength Ignore length of WAV header\n"
|
2013-01-04 17:10:05 +01:00
|
|
|
"\n"
|
2013-01-08 16:12:55 +01:00
|
|
|
"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"
|
2013-01-04 17:10:05 +01:00
|
|
|
"Tagging options:\n"
|
|
|
|
" --title <string>\n"
|
|
|
|
" --artist <string>\n"
|
|
|
|
" --album <string>\n"
|
|
|
|
" --genre <string>\n"
|
|
|
|
" --date <string>\n"
|
|
|
|
" --composer <string>\n"
|
|
|
|
" --grouping <string>\n"
|
|
|
|
" --comment <string>\n"
|
|
|
|
" --album-artist <string>\n"
|
|
|
|
" --track <number[/total]>\n"
|
|
|
|
" --disk <number[/total]>\n"
|
|
|
|
" --tempo <n>\n"
|
2013-01-09 08:41:08 +01:00
|
|
|
" --tag <fcc>:<value> Set iTunes predefined tag with four char code.\n"
|
|
|
|
" --long-tag <name>:<value> Set arbitrary tag as iTunes custom metadata.\n"
|
2013-01-04 17:10:05 +01:00
|
|
|
, fdkaac_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct aacenc_tag_entry_t {
|
|
|
|
uint32_t tag;
|
2013-01-09 08:41:08 +01:00
|
|
|
const char *name;
|
2013-01-04 17:10:05 +01:00
|
|
|
const char *data;
|
2013-01-09 08:41:08 +01:00
|
|
|
uint32_t data_size;
|
2013-01-04 17:10:05 +01:00
|
|
|
} aacenc_tag_entry_t;
|
|
|
|
|
|
|
|
typedef struct aacenc_param_ex_t {
|
|
|
|
AACENC_PARAMS
|
|
|
|
|
|
|
|
char *input_filename;
|
|
|
|
char *output_filename;
|
|
|
|
unsigned ignore_length;
|
|
|
|
|
2013-01-08 16:12:55 +01:00
|
|
|
int is_raw;
|
|
|
|
unsigned raw_channels;
|
|
|
|
unsigned raw_rate;
|
|
|
|
const char *raw_format;
|
|
|
|
|
2013-01-04 17:10:05 +01:00
|
|
|
aacenc_tag_entry_t *tag_table;
|
|
|
|
unsigned tag_count;
|
|
|
|
unsigned tag_table_capacity;
|
|
|
|
} aacenc_param_ex_t;
|
|
|
|
|
2013-01-09 08:41:08 +01:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:10:05 +01:00
|
|
|
static
|
|
|
|
int parse_options(int argc, char **argv, aacenc_param_ex_t *params)
|
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
unsigned n;
|
|
|
|
|
2013-01-08 16:12:55 +01:00
|
|
|
#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')
|
2013-01-09 08:41:08 +01:00
|
|
|
#define OPT_SHORT_TAG M4AF_FOURCC('s','t','a','g')
|
|
|
|
#define OPT_LONG_TAG M4AF_FOURCC('l','t','a','g')
|
2013-01-08 16:12:55 +01:00
|
|
|
|
2013-01-04 17:10:05 +01:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{ "help", no_argument, 0, 'h' },
|
|
|
|
{ "profile", required_argument, 0, 'p' },
|
|
|
|
{ "bitrate", required_argument, 0, 'b' },
|
2013-01-07 12:11:52 +01:00
|
|
|
{ "bitrate-mode", required_argument, 0, 'm' },
|
2013-01-04 17:10:05 +01:00
|
|
|
{ "bandwidth", required_argument, 0, 'w' },
|
|
|
|
{ "afterburner", required_argument, 0, 'a' },
|
|
|
|
{ "lowdelay-sbr", no_argument, 0, 'L' },
|
|
|
|
{ "sbr-signaling", required_argument, 0, 's' },
|
|
|
|
{ "transport-format", required_argument, 0, 'f' },
|
2013-01-08 16:12:55 +01:00
|
|
|
{ "adts-crc-check", no_argument, 0, 'C' },
|
2013-01-04 17:10:05 +01:00
|
|
|
{ "header-period", required_argument, 0, 'P' },
|
|
|
|
|
2013-01-19 10:12:49 +01:00
|
|
|
{ "ignorelength", no_argument, 0, 'I' },
|
2013-01-04 17:10:05 +01:00
|
|
|
|
2013-01-08 16:12:55 +01:00
|
|
|
{ "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 },
|
2013-01-09 08:41:08 +01:00
|
|
|
{ "tag", required_argument, 0, OPT_SHORT_TAG },
|
|
|
|
{ "long-tag", required_argument, 0, OPT_LONG_TAG },
|
2013-01-19 10:12:49 +01:00
|
|
|
{ 0, 0, 0, 0 },
|
2013-01-04 17:10:05 +01:00
|
|
|
};
|
|
|
|
params->afterburner = 1;
|
|
|
|
|
|
|
|
aacenc_getmainargs(&argc, &argv);
|
2013-01-08 16:12:55 +01:00
|
|
|
while ((ch = getopt_long(argc, argv, "hp:b:m:w:a:Ls:f:CP:Io:R",
|
2013-01-04 17:10:05 +01:00
|
|
|
long_options, 0)) != EOF) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
|
|
|
return usage(), -1;
|
|
|
|
case 'p':
|
|
|
|
if (sscanf(optarg, "%u", &n) != 1) {
|
|
|
|
fprintf(stderr, "invalid arg for profile\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
params->profile = n;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
if (sscanf(optarg, "%u", &n) != 1) {
|
|
|
|
fprintf(stderr, "invalid arg for bitrate\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
params->bitrate = n;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (sscanf(optarg, "%u", &n) != 1 || n > 5) {
|
|
|
|
fprintf(stderr, "invalid arg for bitrate-mode\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
params->bitrate_mode = n;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
if (sscanf(optarg, "%u", &n) != 1) {
|
|
|
|
fprintf(stderr, "invalid arg for bandwidth\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
params->bandwidth = n;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
if (sscanf(optarg, "%u", &n) != 1 || n > 1) {
|
|
|
|
fprintf(stderr, "invalid arg for afterburner\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
params->afterburner = n;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
params->lowdelay_sbr = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (sscanf(optarg, "%u", &n) != 1 || n > 2) {
|
|
|
|
fprintf(stderr, "invalid arg for sbr-signaling\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
params->sbr_signaling = n;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (sscanf(optarg, "%u", &n) != 1) {
|
|
|
|
fprintf(stderr, "invalid arg for transport-format\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
params->transport_format = n;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
params->adts_crc_check = 1;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
if (sscanf(optarg, "%u", &n) != 1) {
|
|
|
|
fprintf(stderr, "invalid arg for header-period\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
params->header_period = n;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
params->output_filename = optarg;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
params->ignore_length = 1;
|
|
|
|
break;
|
2013-01-08 16:12:55 +01:00
|
|
|
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;
|
2013-01-04 17:10:05 +01:00
|
|
|
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_ALBUM_ARTIST:
|
|
|
|
case M4AF_TAG_TRACK:
|
|
|
|
case M4AF_TAG_DISK:
|
|
|
|
case M4AF_TAG_TEMPO:
|
2013-01-09 08:41:08 +01:00
|
|
|
param_add_itmf_entry(params, ch, 0, optarg, strlen(optarg));
|
|
|
|
break;
|
|
|
|
case OPT_SHORT_TAG:
|
|
|
|
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) {
|
2013-01-17 09:10:21 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
2013-01-09 08:41:08 +01:00
|
|
|
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));
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return usage(), -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc == optind)
|
|
|
|
return usage(), -1;
|
2013-01-08 16:12:55 +01:00
|
|
|
|
2013-01-04 17:10:05 +01:00
|
|
|
if (!params->bitrate && !params->bitrate_mode) {
|
|
|
|
fprintf(stderr, "bitrate or bitrate-mode is mandatory\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (params->output_filename && !strcmp(params->output_filename, "-") &&
|
|
|
|
!params->transport_format) {
|
|
|
|
fprintf(stderr, "stdout streaming is not available on M4A output\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (params->bitrate && params->bitrate < 10000)
|
|
|
|
params->bitrate *= 1000;
|
2013-01-08 16:12:55 +01:00
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
2013-01-04 17:10:05 +01:00
|
|
|
params->input_filename = argv[optind];
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
int write_sample(FILE *ofp, m4af_writer_t *m4af,
|
|
|
|
const void *data, uint32_t size, uint32_t duration)
|
|
|
|
{
|
|
|
|
if (!m4af) {
|
2013-01-07 05:14:46 +01:00
|
|
|
fwrite(data, 1, size, ofp);
|
|
|
|
if (ferror(ofp)) {
|
2013-01-04 17:10:05 +01:00
|
|
|
fprintf(stderr, "ERROR: fwrite(): %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (m4af_write_sample(m4af, 0, data, size, duration) < 0) {
|
|
|
|
fprintf(stderr, "ERROR: failed to write m4a sample\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int encode(wav_reader_t *wavf, HANDLE_AACENCODER encoder,
|
|
|
|
uint32_t frame_length, FILE *ofp, m4af_writer_t *m4af)
|
|
|
|
{
|
|
|
|
uint8_t *ibuf = 0;
|
|
|
|
int16_t *pcmbuf = 0;
|
|
|
|
uint32_t pcmsize = 0;
|
|
|
|
uint8_t *obuf = 0;
|
|
|
|
uint32_t olen;
|
|
|
|
uint32_t osize = 0;
|
|
|
|
int nread = 1;
|
|
|
|
int consumed;
|
|
|
|
int rc = -1;
|
|
|
|
int frames_written = 0;
|
|
|
|
aacenc_progress_t progress = { 0 };
|
|
|
|
const pcm_sample_description_t *format = wav_get_format(wavf);
|
|
|
|
|
|
|
|
ibuf = malloc(frame_length * format->bytes_per_frame);
|
|
|
|
aacenc_progress_init(&progress, wav_get_length(wavf), format->sample_rate);
|
|
|
|
do {
|
|
|
|
if (nread) {
|
|
|
|
if ((nread = wav_read_frames(wavf, 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 ((consumed = aac_encode_frame(encoder, format, pcmbuf, nread,
|
|
|
|
&obuf, &olen, &osize)) < 0)
|
|
|
|
goto END;
|
|
|
|
if (olen > 0) {
|
|
|
|
if (write_sample(ofp, m4af, obuf, olen, frame_length) < 0)
|
|
|
|
goto END;
|
|
|
|
++frames_written;
|
|
|
|
}
|
|
|
|
} while (nread > 0 || olen > 0);
|
|
|
|
aacenc_progress_finish(&progress, wav_get_position(wavf));
|
|
|
|
rc = frames_written;
|
|
|
|
END:
|
|
|
|
if (ibuf) free(ibuf);
|
|
|
|
if (pcmbuf) free(pcmbuf);
|
|
|
|
if (obuf) free(obuf);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-01-09 08:41:08 +01:00
|
|
|
static
|
2013-01-15 13:19:32 +01:00
|
|
|
void put_tag_entry(m4af_writer_t *m4af, const aacenc_tag_entry_t *tag)
|
2013-01-09 08:41:08 +01:00
|
|
|
{
|
|
|
|
unsigned m, n = 0;
|
|
|
|
|
|
|
|
switch (tag->tag) {
|
|
|
|
case M4AF_TAG_TRACK:
|
|
|
|
if (sscanf(tag->data, "%u/%u", &m, &n) >= 1)
|
|
|
|
m4af_add_itmf_track_tag(m4af, m, n);
|
|
|
|
break;
|
|
|
|
case M4AF_TAG_DISK:
|
|
|
|
if (sscanf(tag->data, "%u/%u", &m, &n) >= 1)
|
|
|
|
m4af_add_itmf_disk_tag(m4af, m, n);
|
|
|
|
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:
|
|
|
|
if (sscanf(tag->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(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;
|
|
|
|
}
|
|
|
|
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'):
|
2013-01-11 14:17:28 +01:00
|
|
|
case M4AF_FOURCC('x','i','d',' '):
|
2013-01-09 08:41:08 +01:00
|
|
|
case M4AF_FOURCC('\xa9','e','n','c'):
|
|
|
|
case M4AF_FOURCC('\xa9','s','t','3'):
|
|
|
|
{
|
|
|
|
char *u8 = aacenc_to_utf8(tag->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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void put_tool_tag(m4af_writer_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);
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:10:05 +01:00
|
|
|
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;
|
2013-01-09 08:41:08 +01:00
|
|
|
for (i = 0; i < params->tag_count; ++i, ++tag)
|
|
|
|
put_tag_entry(m4af, tag);
|
|
|
|
|
|
|
|
put_tool_tag(m4af, params, encoder);
|
2013-01-04 17:10:05 +01:00
|
|
|
|
|
|
|
if (m4af_finalize(m4af) < 0) {
|
|
|
|
fprintf(stderr, "ERROR: failed to finalize m4a\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-06 05:19:07 +01:00
|
|
|
static
|
|
|
|
char *generate_output_filename(const char *filename, const char *ext)
|
|
|
|
{
|
|
|
|
char *p = 0;
|
|
|
|
size_t ext_len = strlen(ext);
|
|
|
|
|
|
|
|
if (strcmp(filename, "-") == 0) {
|
|
|
|
p = malloc(ext_len + 6);
|
|
|
|
sprintf(p, "stdin%s", ext);
|
|
|
|
} else {
|
2013-01-10 03:14:16 +01:00
|
|
|
const char *base = aacenc_basename(filename);
|
2013-01-06 05:19:07 +01:00
|
|
|
size_t ilen = strlen(base);
|
|
|
|
const char *ext_org = strrchr(base, '.');
|
|
|
|
if (ext_org) ilen = ext_org - base;
|
|
|
|
p = malloc(ilen + ext_len + 1);
|
|
|
|
sprintf(p, "%.*s%s", ilen, base, ext);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2013-01-08 16:12:55 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-04 17:10:05 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-01-08 16:12:55 +01:00
|
|
|
wav_io_context_t wav_io = { read_callback, seek_callback, tell_callback };
|
2013-01-04 17:10:05 +01:00
|
|
|
m4af_io_callbacks_t m4af_io = {
|
|
|
|
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;
|
|
|
|
HANDLE_AACENCODER encoder = 0;
|
|
|
|
AACENC_InfoStruct aacinfo = { 0 };
|
|
|
|
m4af_writer_t *m4af = 0;
|
|
|
|
const pcm_sample_description_t *sample_format;
|
|
|
|
int downsampled_timescale = 0;
|
|
|
|
int frame_count = 0;
|
2013-01-06 05:41:31 +01:00
|
|
|
struct stat stb = { 0 };
|
2013-01-04 17:10:05 +01:00
|
|
|
|
|
|
|
setlocale(LC_CTYPE, "");
|
|
|
|
setbuf(stderr, 0);
|
|
|
|
|
|
|
|
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));
|
|
|
|
goto END;
|
|
|
|
}
|
2013-01-08 16:12:55 +01:00
|
|
|
if (fstat(fileno(ifp), &stb) == 0 && (stb.st_mode & S_IFMT) != S_IFREG) {
|
2013-01-04 17:10:05 +01:00
|
|
|
wav_io.seek = 0;
|
2013-01-08 16:12:55 +01:00
|
|
|
wav_io.tell = 0;
|
|
|
|
}
|
|
|
|
if (!params.is_raw) {
|
|
|
|
if ((wavf = wav_open(&wav_io, ifp, params.ignore_length)) == 0) {
|
|
|
|
fprintf(stderr, "ERROR: broken / unsupported input file\n");
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int bytes_per_channel;
|
|
|
|
pcm_sample_description_t desc = { 0 };
|
|
|
|
if (parse_raw_spec(params.raw_format, &desc) < 0) {
|
|
|
|
fprintf(stderr, "ERROR: invalid raw-format spec\n");
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
desc.sample_rate = params.raw_rate;
|
|
|
|
desc.channels_per_frame = params.raw_channels;
|
|
|
|
bytes_per_channel = (desc.bits_per_channel + 7) / 8;
|
|
|
|
desc.bytes_per_frame = params.raw_channels * bytes_per_channel;
|
|
|
|
if ((wavf = raw_open(&wav_io, ifp, &desc)) == 0) {
|
|
|
|
fprintf(stderr, "ERROR: failed to open raw input\n");
|
|
|
|
goto END;
|
|
|
|
}
|
2013-01-04 17:10:05 +01:00
|
|
|
}
|
|
|
|
sample_format = wav_get_format(wavf);
|
|
|
|
|
|
|
|
if (aacenc_init(&encoder, (aacenc_param_t*)¶ms, sample_format,
|
|
|
|
&aacinfo) < 0)
|
|
|
|
goto END;
|
|
|
|
|
|
|
|
if (!params.output_filename) {
|
2013-01-06 05:19:07 +01:00
|
|
|
const char *ext = params.transport_format ? ".aac" : ".m4a";
|
|
|
|
output_filename = generate_output_filename(params.input_filename, ext);
|
2013-01-04 17:10:05 +01:00
|
|
|
params.output_filename = output_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ofp = aacenc_fopen(params.output_filename, "wb")) == 0) {
|
|
|
|
aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.output_filename,
|
|
|
|
strerror(errno));
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
scale = sample_format->sample_rate >> downsampled_timescale;
|
|
|
|
if ((m4af = m4af_create(M4AF_CODEC_MP4A, scale, &m4af_io, ofp)) < 0)
|
|
|
|
goto END;
|
|
|
|
m4af_decoder_specific_info(m4af, 0, aacinfo.confBuf, aacinfo.confSize);
|
|
|
|
m4af_set_fixed_frame_duration(m4af, 0,
|
|
|
|
framelen >> downsampled_timescale);
|
|
|
|
m4af_begin_write(m4af);
|
|
|
|
}
|
|
|
|
frame_count = encode(wavf, encoder, aacinfo.frameLength, ofp, m4af);
|
|
|
|
if (frame_count < 0)
|
|
|
|
goto END;
|
|
|
|
if (m4af) {
|
|
|
|
uint32_t delay = aacinfo.encoderDelay;
|
|
|
|
int64_t frames_read = wav_get_position(wavf);
|
|
|
|
uint32_t padding = frame_count * aacinfo.frameLength
|
|
|
|
- frames_read - aacinfo.encoderDelay;
|
|
|
|
m4af_set_priming(m4af, 0, delay >> downsampled_timescale,
|
|
|
|
padding >> downsampled_timescale);
|
|
|
|
if (finalize_m4a(m4af, ¶ms, encoder) < 0)
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
result = 0;
|
|
|
|
END:
|
|
|
|
if (wavf) wav_teardown(&wavf);
|
|
|
|
if (ifp) fclose(ifp);
|
|
|
|
if (m4af) m4af_teardown(&m4af);
|
|
|
|
if (ofp) fclose(ofp);
|
|
|
|
if (encoder) aacEncClose(&encoder);
|
|
|
|
if (output_filename) free(output_filename);
|
|
|
|
if (params.tag_table) free(params.tag_table);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|