ARM MP3 Encoder: Integration Guide
ARM MP3 Encoder: Integration Guide
Integration Guide
Change history
Proprietary Notice
Words and logos marked with ® or ™ are registered trademarks or trademarks owned by ARM Limited, except
as otherwise stated below in this proprietary notice. Other brands and names mentioned herein may be the
trademarks of their respective owners.
Neither the whole nor any part of the information contained in, or the product described in, this document
may be adapted or reproduced in any material form except with the prior written permission of the copyright
holder.
The product described in this document is subject to continuous developments and improvements. All
particulars of the product and its use contained in this document are given by ARM in good faith. However,
all warranties implied or expressed, including but not limited to implied warranties of merchantability, or
fitness for purpose, are excluded.
This document is intended only to assist the reader in the use of the product. ARM Limited shall not be liable
for any loss or damage arising from the use of any information in this document, or any error or omission in
such information, or any incorrect use of the product.
Confidentiality Status
Product Status
Web Address
[Link]
ii Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Contents
ARM MP3 Encoder Integration Guide
Preface
About this book ...............................................................................................vi
Feedback ....................................................................................................... ix
Chapter 1 Introduction
1.1 About MPEG audio coding .......................................................................... 1-2
1.2 About the ARM MP3 Encoder ..................................................................... 1-4
Glossary
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. iii
Contents
iv Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Preface
This preface introduces the ARM MP3 Encoder. It contains the following sections:
• About this book on page vi
• Feedback on page ix.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. v
Preface
Intended audience
This book is intended for all developers who are designing or implementing an
ARM-based embedded system that uses the ARM MP3 Encoder.
Chapter 1 Introduction
Read this chapter for an introduction to the functionality of the ARM
MP3 Encoder.
vi Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Preface
Typographical conventions
typewriter Denotes text that can be entered at the keyboard, such as commands, file
and program names, and source code.
typewriter italic
Denotes arguments to commands and functions where the argument is to
be replaced by a specific value.
typewriter bold
Denotes language keywords when used outside example code and ARM
processor signal names.
bold Highlights interface elements, such as menu names and buttons. Also
used for terms in descriptive lists, where appropriate.
Further reading
This section lists publications from both ARM Limited and third parties that provide
additional information on developing code for the ARM family of processors.
ARM publications
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. vii
Preface
Other publications
For more information on Metrowerks, and the CodeWarrior IDE generally, visit the
Metrowerks web site at [Link]
viii Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Preface
Feedback
ARM Limited welcomes feedback on both the ARM MP3 Encoder and its
documentation.
If you have any problems with the ARM MP3 Encoder, please contact your supplier. To
help us provide a rapid and useful response, please give:
• details of the release you are using
• details of the platform you are running on, such as the hardware platform,
operating system type and version
• a small standalone sample of code that reproduces the problem
• a clear explanation of what you expected to happen, and what actually happened
• the commands you used, including any command-line options
• sample output illustrating the problem
• the version string of the tool, including the version number and date.
If you have any comments on this book, please send email to errata@[Link] giving:
• the document title
• the document number
• the page number(s) to which your comments apply
• a concise explanation of your comments.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. ix
Preface
x Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Chapter 1-
Introduction
This chapter introduces MPEG in general and the MP3 Encoder. It contains the
following sections:
• About MPEG audio coding on page 1-2
• About the ARM MP3 Encoder on page 1-4.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 1-1
Introduction
Like many compression methods, MP3 attempts to reduce the amount of information in
the signal by:
• removing redundant information that can be reconstructed from what remains
• removing irrelevant information whose absence will not be noticed
• encoding the remaining information in an efficient manner.
1. For a stereo signal, exploit similarities between the channels where possible. This
is known as joint stereo coding.
The original MPEG Audio specification, MPEG-1, has had two extensions:
1-2 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Introduction
Table 1-1 summarizes the sample rates and encoded bit rates supported by the three
variations.
When encoding at high sample rates, a bit rate of 128kbps gives subjective quality
similar to that of a compact disc recording, while at 64kbps the quality is similar to FM
radio.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 1-3
Introduction
The Encoder is distributed with an example application, that reads audio data from a file
and writes the bitstream to another file. This is described in detail in Chapter 3 Example
Application.
The Encoder supports all features specified in MPEG-1, including joint stereo coding.
It supports the lower sampling rates specified in MPEG-2 and MPEG-2.5, but does not
support the multi-channel extensions of MPEG-2. The output bit rate can be either fixed
or variable.
1-4 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Chapter 2-
The ARM MP3 Encoder API
This chapter describes the Application Programmer’s Interface (API) provided by the
ARM MP3 Encoder library. It contains the following sections:
• Configuration options on page 2-2
• Data types on page 2-5
• Constants on page 2-7
• Functions on page 2-9.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 2-1
The ARM MP3 Encoder API
You must specify the sample rate of the signal to encode it correctly. It is expressed in
samples per second, and must take one of the values given in Table 1-1 on page 1-3.
The average bit rate of the output bitstream can be either fixed or variable:
2-2 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
The ARM MP3 Encoder API
You can encode either one or two channels. You can specify a different number of input
and output channels, and the Encoder either down-mixes (combines one or more
signals) or duplicates the signals, as appropriate.
Note
Duplicating a mono signal to produce a stereo signal is not recommended, because this
is coded less efficiently than the mono signal.
2.1.4 Bandwidth
By default, the Encoder limits the bandwidth of the signal according to the bit rate. This
avoids unpleasant high-frequency distortion at low bit rates. Bandwidth limitation is
generally regarded as a less offensive distortion. You can override the bandwidth
selected by the Encoder.
Note
The bandwidth is always limited to either half the sampling frequency, or 16kHz,
whichever is lower.
The Encoder can use a stereo coding technique known as intensity coding. You can
exploit similarities between the channels of a stereo signal to increase coding efficiency,
and so reduce quantization noise in the output. However, this can introduce artifacts in
the stereo imaging, so its use is optional.
Note
Intensity coding is automatically disabled for bit rates above 96kbps.
This option specifies the complexity of the final variable-length coding stage. This
affects the number of bits required to encode the signal, and this affects the amount of
quantization noise. The standard search is fast and generally gives good results. The full
search requires more CPU performance, and gives the best possible results.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 2-3
The ARM MP3 Encoder API
The length of each frame of the bitstream is constrained to be a whole number of bytes.
However, to achieve the desired bit rate exactly, the average frame length might have to
be a fractional number of bytes. It is therefore necessary either to vary the frame sizes
slightly, or to have a bit rate that does not exactly match the desired rate. This can be
achieved using the following modes:
ISO This mode inserts an extra byte in some frames to achieve exactly
the desired average bit rate.
NEVER This mode never inserts an extra byte, so that the average bit rate
is slightly too low.
ALWAYS This mode always inserts an extra byte, so that the average bit rate
is slightly too high.
You can optionally add a Cyclic Redundancy Check (CRC) to each frame to allow error
detection. This slightly reduces the bit rate available for the signal, and so can slightly
degrade the quality at low bit rates.
Each MP3 frame contains three flags containing information about the material. The
flags are:
2-4 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
The ARM MP3 Encoder API
This is an opaque type containing all the internal read/write data used by the Encoder.
This is separated into two blocks:
Scratch data This might be overwritten by the application between calls to the
Encoder.
struct MP3_ENCODER
{
struct MP3_ENCODER_STATE *State;
struct MP3_ENCODER_SCRATCH *Scratch;
};
This structure must be provided by the application to configure the Encoder. See
Configuration options on page 2-2 for a complete description of the options. The
structure is declared as follows:
struct MP3ENC_CONFIG
{
int sampleRate; /* encoder sample rate */
int nChannelsIn; /* number of channels on input (1,2) */
int nChannelsOut; /* number of channels on output (1,2) */
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 2-5
The ARM MP3 Encoder API
This structure provides information about the Encoder that is only known after
initialization.
struct MP3ENC_INFO
{
int bandWidth; /* audio bandwidth in Hz */
int bitRate; /* actual bit rate */
int delay; /* encoder delay in units of sample frames */
int bufferSizeMin; /* minimum size of output buffer (bytes) */
};
2-6 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
The ARM MP3 Encoder API
2.3 Constants
The ARM MP3 Encoder defines the following constants, as shown in Table 2-1:
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 2-7
The ARM MP3 Encoder API
Dynamic allocation
To use dynamic memory allocation, you allocate the required number of bytes:
struct MP3_ENCODER MP3_Encoder;
Static allocation
To use static memory allocation, you first determine the size of the library by writing a
small program to print the value of mp3encEncoderStateSize and
mp3encEncoderScratchSize. A suitable program is as follows:
#include <stdio.h>
#include "mp3enc.h"
int main(void)
{
printf("Encoder state size is %d\n", mp3encEncoderStateSize);
printf("Encoder scratch space is %d\n", mp3encEncoderScratchSize);
return 0;
}
You can then define compile-time constants with these values. It is advisable to include
a run-time check to ensure that the values are still valid:
/* statically allocate the space using the compile-time constants */
static char MP3_EncoderState [ MP3ENC_ENCODER_STATE_SIZE ];
static char MP3_EncoderScratch [ MP3ENC_ENCODER_SCRATCH_SIZE ];
2-8 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
The ARM MP3 Encoder API
2.4 Functions
The following functions are provided by the ARM MP3 Encoder:
• mp3encInit on page 2-9
• mp3encGetInfo on page 2-10
• mp3encEncode on page 2-11.
2.4.1 mp3encInit
The mp3encInit() function initializes a new Encoder instance. The application must
allocate the internal data structure and then call this function before attempting to
encode any data.
Syntax
int mp3encInit(struct MP3_ENCODER* Mp3Enc, const struct MP3ENC_CONFIG* Config);
where:
MP3Enc Is a pointer to the internal data structure of the ARM MP3 Encoder.
Return values
MP3ENC_ERROR_INIT
The configuration structure contained illegal options.
MP3ENC_ERROR_INTERNAL
An internal error occurred.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 2-9
The ARM MP3 Encoder API
2.4.2 mp3encGetInfo
The mp3encGetInfo() function returns information about the Encoder. It can be called
any time after mp3encInit().
Syntax
int mp3encGetInfo(const struct MP3_ENCODER* Mp3Enc, struct MP3ENC_INFO* Info);
where:
Return values
MP3ENC_ERROR_INTERNAL
An internal error occurred.
Returned information
2-10 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
The ARM MP3 Encoder API
2.4.3 mp3encEncode
The mp3encEncode function places a new block of input samples in the Encoder, and
collects any new output data.
Syntax
int mp3encEncode(struct MP3_ENCODER *Mp3Enc, const short *InputBuffer,
int nSamples, unsigned char *OutputBuffer, int nOutputSize,
int *nOut);
where:
OutputBuffer Is the buffer where complete frames of bitstream data are written.
Return values
MP3ENC_ERROR_NSAMPLES
There were too many samples to encode.
MP3ENC_ERROR_BUFSIZE
The output buffer might be too small.
MP3ENC_ERROR_INTERNAL
An internal error occurred.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 2-11
The ARM MP3 Encoder API
Notes
This function must normally be called with nSamples set to MP3ENC_BLOCKSIZE for a mono
signal, or twice that for a stereo signal. It is an error to give a higher value.
At the end of a piece of music, if there are fewer than MP3ENC_BLOCKSIZE samples per
channel, call the function with nSamples set to the number of remaining samples. The
Encoder automatically pads the input with silence.
When the last samples have been encoded, the end of the bitstream might still be
buffered inside the Encoder. To extract the final frame(s), call this function with
nSamples set to zero as many times as necessary until it returns no data (that is, it returns
with nOut set to zero). In other words, the application must stop calling this function
when the input data is exhausted and the function returns with nOut set to zero.
While the input stream is being read, it is normal for this function to occasionally return
zero data, because there is often not enough data to make a whole frame. The partial
frame is buffered within the Encoder, and is output as soon as it is complete. When the
input stream is exhausted, this function returns data every time it is called until encoding
is complete.
However, this function occasionally returns two blocks, and so the output buffer must
be large enough to contain both. The function returns with an error if the buffer is too
small.
2-12 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Chapter 3-
Example Application
This chapter describes the example application supplied with the ARM MP3 Encoder
library. It contains the following sections:
• About the example application on page 3-2
• Command-line arguments on page 3-3
• Benchmarking on page 3-4.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 3-1
Example Application
The distribution contains makefiles and Metrowerks CodeWarrior project files, that can
be used with the ARM Developer Suite (ADS) to build the application. See the
documentation for the ARM Developer Suite for details of building and running ARM
applications.
The application runs on any ARM-based system (real or emulated) that supports
semihosted file operations.
The example application uses another library, AudioIO.a, to read the data from a variety
of common audio file formats. This library is not optimized, and must be linked with
the ARM C++ library.
Note
The AudioIO.a library is intended only for testing purposes, and its use in an embedded
application is strongly discouraged.
3-2 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Example Application
Note
All the numeric arguments have default values if they are omitted. Only the file names
are mandatory.
-b N Sets the output bit rate in N bits per second (use zero for a variable rate).
-q N Sets the quality if you are using a variable bit rate, where N is in the range
0-100.
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. 3-3
Example Application
3.3 Benchmarking
The example application can generate basic information about memory and CPU usage.
Note
The statistics are of limited accuracy, and are intended only as a guide. You must
measure the performance characteristics with more accurate methods when designing a
system using the library.
1. Set up the ARMulator with an emulated clock speed of 100kHz, and a core and
memory map to match your system.
2. If the system does not have zero wait-state memory, scale the memory access
times to match the emulated clock speed. For example, for a 100MHz system with
20ns memory, scale the memory access time to 20µs.
Note
Beware that this might cause further inaccuracy in the timing information.
3. Run the ARM MP3 Encoder with the -t argument for a range of configurations
and inputs. This displays:
• The amount of memory allocated for the buffers and working data during
initialization.
• The peak and average CPU usages at regular intervals. The overall peak
CPU usage is given when encoding is complete.
3-4 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
-Glossary
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. Glossary-1
Glossary
Glossary-2 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A
Index
The items in this index are listed in alphabetical order, with symbols and numerics appearing at the end. The
references given are to page numbers.
A C D
AIFF format 3-3 Channels 2-3 Data types
ALWAYS frame padding mode 2-4 Coding efficiency 2-3 Encoder configuration structure 2-5
API 2-2 Command-line arguments 3-3 Encoder information structure 2-6
Application Programmer’s Interface. Compression methods 1-2 internal data structure 2-5
See API Configuration options Distortion, high frequency 2-3
ARM C++ library 3-2 bandwidth 2-3 Down-mixing 2-3, 3-3
ARM MP3 Encoder bit rate 2-2 Dynamic memory allocation 2-8
features 1-4 bitstream flags 2-4
overview 1-4 frame padding 2-4
ARM processors for MP3 1-4 Huffman search mode 2-3 E
AudIO.a file 3-2 intensity coding 2-3
number of channels 2-3 Encoder
protection mode 2-4 returned information on 2-10
B sample rate 2-2 Encoder configuration structure 2-5
Constants 2-7 Encoder information structure 2-6
Bandwidth 2-3, 3-3 controlling writable memory 2-7 Encoding a signal 2-3
Benchmarking 3-3 mp3encEncoderScratchSize 2-7 Error detection 2-4
generating data 3-4 mp3encEncoderStateSize 2-7
Bit rate 2-2 Copyright bitstream flag 2-4
adjusting 2-4 Cyclic Redundancy checks 2-4
Bitstream flags 2-4
ARM DII 0021A Copyright © 2001 ARM Limited. All rights reserved. Index-1
Index
Q
L
Quality of sound 1-3, 2-2, 2-4, 3-3
Layer III compression 1-2
R
M
Raw PCM format 3-3
Memory allocation 3-3 RIFF/WAV format 3-3
dynamic 2-8
static 2-8
Mono to stereo signals 2-3 S
MPEG overview 1-2
MPEG standards Sample rate 2-2
MPEG-1 1-2 Scratch data 2-5
Index-2 Copyright © 2001 ARM Limited. All rights reserved. ARM DII 0021A