main.c
来自「faac-1.25.rar音频编解码器demo」· C语言 代码 · 共 1,324 行 · 第 1/3 页
C
1,324 行
/*
* FAAC - Freeware Advanced Audio Coder
* Copyright (C) 2001 Menno Bakker
* Copyright (C) 2002-2004 Krzysztof Nikiel
* Copyright (C) 2004 Dan Villiom P. Christiansen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: main.c,v 1.78 2004/12/08 11:07:17 menno Exp $
*/
#ifdef _MSC_VER
# define HAVE_LIBMP4V2 1
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_LIBMP4V2
# include "mp4.h"
#endif
#define DEFAULT_TNS 0
#ifdef _WIN32
#include <windows.h>
#include <fcntl.h>
#else
#include <signal.h>
#endif
/* the BSD derivatives don't define __unix__ */
#if defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__bsdi__)
#define __unix__
#endif
#ifdef __unix__
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#else
# include "getopt.h"
# include "getopt.c"
#endif
#if !defined(HAVE_STRCASECMP) && !defined(_WIN32)
# define strcasecmp strcmp
#endif
#ifdef _WIN32
# undef stderr
# define stderr stdout
#endif
#include "input.h"
#include "faac.h"
const char *usage =
"Usage: %s [options] [-o outfile] infiles ...\n"
"\n"
"\t<infiles> and/or <outfile> can be \"-\", which means stdin/stdout.\n"
"\n"
"See also:\n"
"\t\"%s --help\" for short help on using FAAC\n"
"\t\"%s --long-help\" for a description of all options for FAAC.\n"
"\t\"%s --license\" for the license terms for FAAC.\n\n";
const char *short_help =
"Usage: %s [options] infiles ...\n"
"Options:\n"
" -q <quality>\tSet quantizer quality.\n"
" -b <bitrate>\tSet average bitrate to x kbps. (ABR, lower quality mode)\n"
" -c <freq>\tSet the bandwidth in Hz. (default=automatic)\n"
" -o X\t\tSet output file to X (only for one input file)\n"
" -r\t\tUse RAW AAC output file.\n"
" -P\t\tRaw PCM input mode (default 44100Hz 16bit stereo).\n"
" -R\t\tRaw PCM input rate.\n"
" -B\t\tRaw PCM input sample size (8, 16 (default), 24 or 32bits).\n"
" -C\t\tRaw PCM input channels.\n"
" -X\t\tRaw PCM swap input bytes\n"
" -I <C,LF>\tInput channel config, default is 3,4 (Center third, LF fourth)\n"
"\n"
"MP4 specific options:\n"
#ifdef HAVE_LIBMP4V2
" -w\t\tWrap AAC data in MP4 container. (default for *.mp4 and *.m4a)\n"
" --artist X\tSet artist to X\n"
" --writer X\tSet writer to X\n"
" --title X\tSet title to X\n"
" --genre X\tSet genre to X\n"
" --album X\tSet album to X\n"
" --compilation\tSet compilation\n"
" --track X\tSet track to X (number/total)\n"
" --disc X\tSet disc to X (number/total)\n"
" --year X\tSet year to X\n"
" --cover-art X\tRead cover art from file X\n"
" --comment X\tSet comment to X\n"
#else
" MP4 support unavailable.\n"
#endif
"\n"
"Documentation:\n"
" --license\tShow the FAAC license.\n"
" --help\tShow this abbreviated help.\n"
" --long-help\tShow complete help.\n"
"\n"
" More tips can be found in the audiocoding.com Knowledge Base at\n"
" <http://www.audiocoding.com/wiki/>\n"
"\n";
const char *long_help =
"Usage: %s [options] infiles ...\n"
"\n"
"Quality-related options:\n"
" -q <quality>\tSet default variable bitrate (VBR) quantizer quality in percent.\n"
"\t\t(default: 100, averages at approx. 120 kbps VBR for a normal\n"
"\t\tstereo input file with 16 bit and 44.1 kHz sample rate; max.\n"
"\t\tvalue 500, min. 10).\n"
" -b <bitrate>\tSet average bitrate (ABR) to approximately <bitrate> kbps.\n"
"\t\t(max. value 152 kbps/stereo with a 16 kHz cutoff, can be raised\n"
"\t\twith a higher -c setting).\n"
" -c <freq>\tSet the bandwidth in Hz (default: automatic, i.e. adapts\n"
"\t\tmaximum value to input sample rate).\n"
"\n"
"Input/output options:\n"
" -\t\t<stdin/stdout>: If you simply use a hyphen/minus sign instead\n"
"\t\tof an input file name, FAAC can encode directly from stdin,\n"
"\t\tthus enabling piping from other applications and utilities. The\n"
"\t\tsame works for stdout as well, so FAAC can pipe its output to\n"
"\t\tother apps such as a server.\n"
" -o X\t\tSet output file to X (only for one input file)\n"
"\t\tonly for one input file; you can use *.aac, *.mp4, *.m4a or\n"
"\t\t*.m4b as file extension, and the file format will be set\n"
"\t\tautomatically to ADTS or MP4).\n"
" -P\t\tRaw PCM input mode (default: off, i.e. expecting a WAV header;\n"
"\t\tnecessary for input files or bitstreams without a header; using\n"
"\t\tonly -P assumes the default values for -R, -B and -C in the\n"
"\t\tinput file).\n"
" -R\t\tRaw PCM input sample rate in Hz (default: 44100 Hz, max. 96 kHz)\n"
" -B\t\tRaw PCM input sample size (default: 16, also possible 8, 24, 32\n"
"\t\tbit fixed or float input).\n"
" -C\t\tRaw PCM input channels (default: 2, max. 33 + 1 LFE).\n"
" -X\t\tRaw PCM swap input bytes (default: bigendian).\n"
" -I <C[,LFE]>\tInput multichannel configuration (default: 3,4 which means\n"
"\t\tCenter is third and LFE is fourth like in 5.1 WAV, so you only\n"
"\t\thave to specify a different position of these two mono channels\n"
"\t\tin your multichannel input files if they haven't been reordered\n"
"\t\talready).\n"
"\n"
"MP4 specific options:\n"
#ifdef HAVE_LIBMP4V2
" -w\t\tWrap AAC data in MP4 container. (default for *.mp4, *.m4a and\n"
"\t\t*.m4b)\n"
" --artist X\tSet artist to X\n"
" --writer X\tSet writer/composer to X\n"
" --title X\tSet title/track name to X\n"
" --genre X\tSet genre to X\n"
" --album X\tSet album/performer to X\n"
" --compilation\tMark as compilation\n"
" --track X\tSet track to X (number/total)\n"
" --disc X\tSet disc to X (number/total)\n"
" --year X\tSet year to X\n"
" --cover-art X\tRead cover art from file X\n"
"\t\tSupported image formats are GIF, JPEG, and PNG.\n"
" --comment X\tSet comment to X\n"
#else
" MP4 support unavailable.\n"
#endif
"\n"
"Expert options, only for testing purposes:\n"
#if !DEFAULT_TNS
" --tns \tEnable coding of TNS, temporal noise shaping.\n"
#else
" --no-tns\tDisable coding of TNS, temporal noise shaping.\n"
#endif
" --no-midside\tDon\'t use mid/side coding.\n"
" --mpeg-vers X\tForce AAC MPEG version, X can be 2 or 4\n"
" --obj-type X\tAAC object type. (LC (Low Complexity, default), Main or LTP\n"
"\t\t(Long Term Prediction)\n"
" --shortctl X\tEnforce block type (0 = both (default); 1 = no short; 2 = no\n"
"\t\tlong).\n"
" -r\t\tGenerate raw AAC bitstream (i.e. without any headers).\n"
"\t\tNot advised!!!, RAW AAC files are practically useless!!!\n"
"\n"
"Documentation:\n"
" --license\tShow the FAAC license.\n"
" --help\tShow this abbreviated help.\n"
" --long-help\tShow complete help.\n"
"\n"
" More tips can be found in the audiocoding.com Knowledge Base at\n"
" <http://www.audiocoding.com/wiki/>\n"
"\n";
char *license =
"\nPlease note that the use of this software may require the payment of patent\n"
"royalties. You need to consider this issue before you start building derivative\n"
"works. We are not warranting or indemnifying you in any way for patent\n"
"royalities! YOU ARE SOLELY RESPONSIBLE FOR YOUR OWN ACTIONS!\n"
"\n"
"FAAC is based on the ISO MPEG-4 reference code. For this code base the\n"
"following license applies:\n"
"\n"
"This software module was originally developed by\n"
"\n"
"FirstName LastName (CompanyName)\n"
"\n"
"and edited by\n"
"\n"
"FirstName LastName (CompanyName)\n"
"FirstName LastName (CompanyName)\n"
"\n"
"in the course of development of the MPEG-2 NBC/MPEG-4 Audio standard\n"
"ISO/IEC 13818-7, 14496-1,2 and 3. This software module is an\n"
"implementation of a part of one or more MPEG-2 NBC/MPEG-4 Audio tools\n"
"as specified by the MPEG-2 NBC/MPEG-4 Audio standard. ISO/IEC gives\n"
"users of the MPEG-2 NBC/MPEG-4 Audio standards free license to this\n"
"software module or modifications thereof for use in hardware or\n"
"software products claiming conformance to the MPEG-2 NBC/ MPEG-4 Audio\n"
"standards. Those intending to use this software module in hardware or\n"
"software products are advised that this use may infringe existing\n"
"patents. The original developer of this software module and his/her\n"
"company, the subsequent editors and their companies, and ISO/IEC have\n"
"no liability for use of this software module or modifications thereof\n"
"in an implementation. Copyright is not released for non MPEG-2\n"
"NBC/MPEG-4 Audio conforming products. The original developer retains\n"
"full right to use the code for his/her own purpose, assign or donate\n"
"the code to a third party and to inhibit third party from using the\n"
"code for non MPEG-2 NBC/MPEG-4 Audio conforming products. This\n"
"copyright notice must be included in all copies or derivative works.\n"
"\n"
"Copyright (c) 1997.\n"
"\n"
"For the changes made for the FAAC project the GNU Lesser General Public\n"
"License (LGPL), version 2 1991 applies:\n"
"\n"
"FAAC - Freeware Advanced Audio Coder\n"
"Copyright (C) 2001-2004 The individual contributors\n"
"\n"
"This library is free software; you can redistribute it and/or\n"
"modify it under the terms of the GNU Lesser General Public\n"
"License as published by the Free Software Foundation; either\n"
"version 2.1 of the License, or (at your option) any later version.\n"
"\n"
"This library is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
"Lesser General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU Lesser General Public\n"
"License along with this library; if not, write to the Free Software\n"
"Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
"\n";
#ifndef min
#define min(a,b) ( (a) < (b) ? (a) : (b) )
#endif
/* globals */
char* progName;
#ifndef _WIN32
volatile int running = 1;
#endif
enum stream_format {
RAW_STREAM = 0,
ADTS_STREAM = 1,
};
enum container_format {
NO_CONTAINER,
#ifdef HAVE_LIBMP4V2
MP4_CONTAINER,
#endif
};
enum flags {
SHORTCTL_FLAG = 300,
TNS_FLAG = 301,
NO_TNS_FLAG = 302,
MPEGVERS_FLAG = 303,
OBJTYPE_FLAG = 304,
NO_MIDSIDE_FLAG = 306,
#ifdef HAVE_LIBMP4V2
ARTIST_FLAG = 320,
TITLE_FLAG = 321,
GENRE_FLAG = 322,
ALBUM_FLAG = 323,
COMPILATION_FLAG = 324,
TRACK_FLAG = 325,
DISC_FLAG = 326,
YEAR_FLAG = 327,
COVER_ART_FLAG = 328,
COMMENT_FLAG = 329,
WRITER_FLAG = 330,
#endif
};
#ifndef _WIN32
void signal_handler(int signal) {
running = 0;
}
#endif
#ifdef HAVE_LIBMP4V2
static int check_image_header(const char *buf) {
if (!strncmp(buf, "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8))
return 1; /* PNG */
else if (!strncmp(buf, "\xFF\xD8\xFF\xE0", 4) &&
!strncmp(buf + 6, "JFIF\0", 5))
return 1; /* JPEG */
else if (!strncmp(buf, "GIF87a", 6) || !strncmp(buf, "GIF89a", 6))
return 1; /* GIF */
else
return 0;
}
#endif
static int *mkChanMap(int channels, int center, int lf)
{
int *map;
int inpos;
int outpos;
if (!center && !lf)
return NULL;
if (channels < 3)
return NULL;
if (lf > 0)
lf--;
else
lf = channels - 1; // default AAC position
if (center > 0)
center--;
else
center = 0; // default AAC position
map = malloc(channels * sizeof(map[0]));
memset(map, 0, channels * sizeof(map[0]));
outpos = 0;
if ((center >= 0) && (center < channels))
map[outpos++] = center;
inpos = 0;
for (; outpos < (channels - 1); inpos++)
{
if (inpos == center)
continue;
if (inpos == lf)
continue;
map[outpos++] = inpos;
}
if (outpos < channels)
{
if ((lf >= 0) && (lf < channels))
map[outpos] = lf;
else
map[outpos] = inpos;
}
return map;
}
int main(int argc, char *argv[])
{
int frames, currentFrame;
faacEncHandle hEncoder;
pcmfile_t *infile = NULL;
unsigned long samplesInput, maxBytesOutput, totalBytesWritten=0;
faacEncConfigurationPtr myFormat;
unsigned int mpegVersion = MPEG2;
unsigned int objectType = LOW;
unsigned int useMidSide = 1;
static unsigned int useTns = DEFAULT_TNS;
enum container_format container = NO_CONTAINER;
enum stream_format stream = ADTS_STREAM;
int cutOff = -1;
int bitRate = 0;
unsigned long quantqual = 0;
int chanC = 3;
int chanLF = 4;
char *audioFileName = NULL;
char *aacFileName = NULL;
char *aacFileExt = NULL;
int aacFileNameGiven = 0; //标记:输出文件名是否给定
float *pcmbuf;
int *chanmap = NULL;
unsigned char *bitbuf;
int samplesRead = 0;
const char *dieMessage = NULL;
int rawChans = 0; // disabled by default
int rawBits = 16;
int rawRate = 44100;
int rawEndian = 1;
int shortctl = SHORTCTL_NORMAL;
FILE *outfile = NULL;
#ifdef HAVE_LIBMP4V2
MP4FileHandle MP4hFile = MP4_INVALID_FILE_HANDLE;
MP4TrackId MP4track = 0;
unsigned int ntracks = 0, trackno = 0;
unsigned int ndiscs = 0, discno = 0;
u_int8_t compilation = 0;
const char *artist = NULL, *title = NULL, *album = NULL, *year = NULL,
*genre = NULL, *comment = NULL, *writer = NULL;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?