EmbeddedRelated.com

Codec

Category: Electrical

A codec (coder-decoder) is a hardware block, software library, or combination of both that encodes raw data into a compressed or formatted bitstream and decodes that bitstream back to the original (or approximated) form. In embedded contexts, codecs most commonly handle audio, video, or image data, though the term also applies to line coding and data-compression schemes used in communication links.

In practice

Codec work in embedded systems spans a wide performance range. On resource-constrained MCUs (8/16-bit or low-end Cortex-M0/M0+), software codecs are typically limited to low-bitrate audio formats such as ADPCM, G.711 mu-law/A-law, or small fixed-coefficient codecs like Speex at reduced complexity. Mid-range Cortex-M4/M7 parts, particularly those with DSP instructions and sufficient clock speed, can often run MP3 decoding or Opus at real-time rates in software, though the presence of an FPU is not always the deciding factor and some parts ship without one. Higher-end application processors (Cortex-A family, i.MX, Allwinner, Rockchip) often include dedicated hardware codec accelerators for H.264/H.265 encode and decode, though support varies by product tier and some parts offer decode-only blocks or omit hardware acceleration entirely.

A common architecture splits the codec into a front-end (framing, header parsing, entropy decoding) and a back-end (transform, synthesis filter bank, or DCT). On MCUs without hardware acceleration, the back-end is almost always the CPU bottleneck. Profiling the inner loops and mapping them to SIMD or DSP instructions (ARM CMSIS-DSP intrinsics, for example) is a frequent optimization step. Memory is equally important: many audio codecs require scratch buffers of 10-50 KB, which can exhaust SRAM on smaller devices, making external PSRAM or careful static-allocation strategies necessary.

Codec selection involves trade-offs among compression ratio, latency, computational load, licensing, and patent status. Opus is royalty-free and offers competitive quality at low bitrates; MP3 and AAC historically carried licensing requirements (MP3 patents have largely expired, though AAC licensing remains more complex and varies by implementation and region). G.711 and G.722 are widely used in telephony codecs on voice-band hardware because they are computationally trivial. For video, MJPEG is simpler to implement on constrained hardware than interframe codecs (H.264, VP8) because it requires no motion-vector buffers or reference-frame storage.

Integration pitfalls include endianness mismatches when reading multi-byte codec headers, buffer alignment requirements for SIMD operations, and real-time deadline violations caused by variable decode time (a property of many entropy-coded formats). The blog post "Multimedia Compression: Another Revolution" discusses how compression algorithms have evolved and the practical impact on embedded multimedia design. "Endianness and Serial Communication" is relevant when parsing codec bitstreams that cross byte boundaries in non-obvious ways.

Frequently asked

Can I run an audio codec on a bare-metal MCU without an RTOS?
Yes, many audio codecs run fine on bare metal. The usual approach is to fill a DMA-backed ping-pong buffer: one half transfers to the DAC/I2S peripheral while the CPU decodes into the other half. The constraint is that the decode of one half must complete before the DMA pointer wraps. Whether that deadline is met depends on MCU clock speed, codec complexity, and buffer size. ADPCM and G.711 are easy; MP3 on a 48 MHz Cortex-M0 is not.
What is the difference between a lossless and a lossy codec?
A lossless codec (FLAC, PNG, LZ4, zlib) reconstructs the original data bit-for-bit. A lossy codec (MP3, AAC, Opus, H.264, JPEG) discards information that is judged less significant -- in media codecs this is typically perceptually insignificant detail -- to achieve higher compression ratios. For sensor data, telemetry logs, or firmware images, only lossless schemes are acceptable. Lossy codecs are appropriate for human-consumed audio and video where exact reconstruction is not required.
How do hardware codec accelerators differ from software codecs?
A hardware codec accelerator is a dedicated peripheral or integrated IP block within the SoC that performs encode or decode with minimal CPU involvement, typically using DMA to move data in and out. This frees the CPU for application logic and drastically reduces power per decoded frame compared to software running on the CPU. The trade-off is inflexibility: hardware blocks support only the specific standards they were designed for, and updating them requires a silicon respin. Software codecs are flexible and updatable but consume CPU cycles proportional to the codec's complexity.
What causes audio glitches (clicks, pops) in embedded codec implementations?
The most common cause is a buffer underrun: the CPU did not finish decoding the next audio block before the DMA peripheral exhausted the previous one, causing the output to repeat stale data or output silence. Other causes include interrupt latency spikes that delay the decode loop, heap fragmentation stalling a malloc call inside the codec library, and clock-rate mismatches between the codec's expected sample rate and the I2S peripheral's actual output rate.
What should I watch out for when porting a PC-derived codec library to an embedded target?
Key issues include: (1) dynamic memory use -- many desktop codec libraries call malloc freely; on MCUs with limited heap this can fail or fragment badly, so prefer codecs with static-allocation APIs; (2) floating-point dependence -- if the target lacks an FPU, float operations fall back to software emulation and can be 10-100x slower; (3) word-size assumptions -- code written for 32- or 64-bit hosts may use 'int' for loop counters in ways that overflow on 16-bit targets; (4) endianness -- bitstream parsers often assume little-endian host byte order; verify with the target's actual endianness.

Differentiators vs similar concepts

A codec is sometimes conflated with a DAC/ADC chip that carries the word 'codec' in its datasheet name (such as the WM8731 audio codec). Those chips are analog front-end devices that perform PCM conversion; they do not themselves implement compression algorithms. A compression codec operates on digital data to reduce its size or adapt its format, and may exist entirely in software. The two meanings coexist in embedded audio work: a system might use a hardware audio codec chip for ADC/DAC conversion while a software codec (MP3, Opus) handles compressed-stream encode or decode on the MCU.