diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-alac.c b/src/audio/ffmpeg_dec/ffmpeg-enc-alac.c index f2f00b261a05..8fe7829d4096 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-enc-alac.c +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-alac.c @@ -164,8 +164,12 @@ static int ffmpeg_enc_alac_encode_frame(struct processing_module *mod, struct ff if (av_frame_get_buffer(alac->frame, 0) < 0) return -ENOMEM; - ffmpeg_dec_pcm_to_frame(e->pcm_in, alac->frame, - e->src_fmt, ch, e->nb); + ret = ffmpeg_dec_pcm_to_frame(e->pcm_in, alac->frame, + e->src_fmt, ch, e->nb); + if (ret < 0) { + av_frame_unref(alac->frame); + return ret; + } ret = avcodec_send_frame(alac->avctx, alac->frame); av_frame_unref(alac->frame); diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-aptx.c b/src/audio/ffmpeg_dec/ffmpeg-enc-aptx.c index dd85488165f8..f7625d9aeec9 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-enc-aptx.c +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-aptx.c @@ -125,8 +125,12 @@ static int ffmpeg_enc_aptx_encode_frame(struct processing_module *mod, struct ff if (av_frame_get_buffer(aptx->frame, 0) < 0) return -ENOMEM; - ffmpeg_dec_pcm_to_frame(e->pcm_in, aptx->frame, - e->src_fmt, 2, e->nb); + ret = ffmpeg_dec_pcm_to_frame(e->pcm_in, aptx->frame, + e->src_fmt, 2, e->nb); + if (ret < 0) { + av_frame_unref(aptx->frame); + return ret; + } ret = avcodec_send_frame(aptx->avctx, aptx->frame); av_frame_unref(aptx->frame); diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-flac.c b/src/audio/ffmpeg_dec/ffmpeg-enc-flac.c index 03c4e602bfe2..b0d49a19025d 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-enc-flac.c +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-flac.c @@ -164,8 +164,12 @@ static int ffmpeg_enc_flac_encode_frame(struct processing_module *mod, struct ff if (av_frame_get_buffer(flac->frame, 0) < 0) return -ENOMEM; - ffmpeg_dec_pcm_to_frame(e->pcm_in, flac->frame, - e->src_fmt, ch, e->nb); + ret = ffmpeg_dec_pcm_to_frame(e->pcm_in, flac->frame, + e->src_fmt, ch, e->nb); + if (ret < 0) { + av_frame_unref(flac->frame); + return ret; + } ret = avcodec_send_frame(flac->avctx, flac->frame); av_frame_unref(flac->frame); diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-g722.c b/src/audio/ffmpeg_dec/ffmpeg-enc-g722.c index 2f48e1209c9f..a3b59186f1a6 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-enc-g722.c +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-g722.c @@ -160,9 +160,13 @@ static int ffmpeg_enc_g722_encode_frame(struct processing_module *mod, struct ff G722_ENC_FRAME_SAMPLES, e->decim_hist); } else { - ffmpeg_dec_pcm_to_frame(e->pcm_in, g722->frame, - e->src_fmt, cd->out_channels, - G722_ENC_FRAME_SAMPLES); + ret = ffmpeg_dec_pcm_to_frame(e->pcm_in, g722->frame, + e->src_fmt, cd->out_channels, + G722_ENC_FRAME_SAMPLES); + if (ret < 0) { + av_frame_unref(g722->frame); + return ret; + } } ret = avcodec_send_frame(g722->avctx, g722->frame); diff --git a/src/audio/ffmpeg_dec/ffmpeg-enc-sbc.c b/src/audio/ffmpeg_dec/ffmpeg-enc-sbc.c index 451f4731860c..1e67b77765b8 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-enc-sbc.c +++ b/src/audio/ffmpeg_dec/ffmpeg-enc-sbc.c @@ -172,9 +172,13 @@ static int ffmpeg_enc_sbc_encode_frame(struct processing_module *mod, struct ffm MSBC_FRAME_SAMPLES, e->decim_hist); } else { - ffmpeg_dec_pcm_to_frame(e->pcm_in, sbc->frame, - e->src_fmt, cd->out_channels, - enc_samples); + ret = ffmpeg_dec_pcm_to_frame(e->pcm_in, sbc->frame, + e->src_fmt, cd->out_channels, + enc_samples); + if (ret < 0) { + av_frame_unref(sbc->frame); + return ret; + } } ret = avcodec_send_frame(sbc->avctx, sbc->frame); diff --git a/src/audio/ffmpeg_dec/ffmpeg-encode.c b/src/audio/ffmpeg_dec/ffmpeg-encode.c index 570acba697c6..3e2fec68ccf9 100644 --- a/src/audio/ffmpeg_dec/ffmpeg-encode.c +++ b/src/audio/ffmpeg_dec/ffmpeg-encode.c @@ -80,133 +80,6 @@ static inline size_t ffmpeg_enc_min(size_t a, size_t b) return a < b ? a : b; } -static inline int16_t ffmpeg_enc_s24_to_s16(int32_t s) -{ - int32_t v = s >> 8; - - if (v > 32767) - return 32767; - if (v < -32768) - return -32768; - return (int16_t)v; -} - -void ffmpeg_enc_pcm_to_s16_interleaved(const uint8_t *pcm_in, int16_t *dst, - size_t total_samples, int src_fmt) -{ - size_t i; - - if (src_fmt == SOF_IPC_FRAME_S16_LE) { - memcpy_s(dst, total_samples * sizeof(int16_t), - pcm_in, total_samples * sizeof(int16_t)); - return; - } - - if (src_fmt == SOF_IPC_FRAME_S24_4LE) { - const int32_t *in = (const int32_t *)pcm_in; - - for (i = 0; i < total_samples; i++) - dst[i] = ffmpeg_enc_s24_to_s16(in[i]); - return; - } - - { - const int32_t *in = (const int32_t *)pcm_in; - - for (i = 0; i + 4 <= total_samples; i += 4) { - dst[i + 0] = (int16_t)(in[i + 0] >> 16); - dst[i + 1] = (int16_t)(in[i + 1] >> 16); - dst[i + 2] = (int16_t)(in[i + 2] >> 16); - dst[i + 3] = (int16_t)(in[i + 3] >> 16); - } - for (; i < total_samples; i++) - dst[i] = (int16_t)(in[i] >> 16); - } -} - -void ffmpeg_enc_pcm_to_s16_planar(const uint8_t *pcm_in, uint8_t * const *planes, - int nb_samples, int ch, int src_fmt) -{ - int i, c; - - if (src_fmt == SOF_IPC_FRAME_S16_LE) { - const int16_t *in = (const int16_t *)pcm_in; - - if (ch == 2) { - int16_t *out0 = (int16_t *)planes[0]; - int16_t *out1 = (int16_t *)planes[1]; - - for (i = 0; i < nb_samples; i++) { - out0[i] = in[2 * i + 0]; - out1[i] = in[2 * i + 1]; - } - return; - } - for (c = 0; c < ch; c++) { - int16_t *out = (int16_t *)planes[c]; - - for (i = 0; i < nb_samples; i++) - out[i] = in[i * ch + c]; - } - return; - } - - if (src_fmt == SOF_IPC_FRAME_S24_4LE) { - const int32_t *in = (const int32_t *)pcm_in; - - if (ch == 2) { - int16_t *out0 = (int16_t *)planes[0]; - int16_t *out1 = (int16_t *)planes[1]; - - for (i = 0; i < nb_samples; i++) { - out0[i] = ffmpeg_enc_s24_to_s16(in[2 * i + 0]); - out1[i] = ffmpeg_enc_s24_to_s16(in[2 * i + 1]); - } - return; - } - for (c = 0; c < ch; c++) { - int16_t *out = (int16_t *)planes[c]; - - for (i = 0; i < nb_samples; i++) - out[i] = ffmpeg_enc_s24_to_s16(in[i * ch + c]); - } - return; - } - - { - const int32_t *in = (const int32_t *)pcm_in; - - if (ch == 2) { - int16_t *out0 = (int16_t *)planes[0]; - int16_t *out1 = (int16_t *)planes[1]; - - for (i = 0; i + 4 <= nb_samples; i += 4) { - out0[i + 0] = (int16_t)(in[0] >> 16); - out1[i + 0] = (int16_t)(in[1] >> 16); - out0[i + 1] = (int16_t)(in[2] >> 16); - out1[i + 1] = (int16_t)(in[3] >> 16); - out0[i + 2] = (int16_t)(in[4] >> 16); - out1[i + 2] = (int16_t)(in[5] >> 16); - out0[i + 3] = (int16_t)(in[6] >> 16); - out1[i + 3] = (int16_t)(in[7] >> 16); - in += 8; - } - for (; i < nb_samples; i++) { - out0[i] = (int16_t)(in[0] >> 16); - out1[i] = (int16_t)(in[1] >> 16); - in += 2; - } - return; - } - for (c = 0; c < ch; c++) { - int16_t *out = (int16_t *)planes[c]; - - for (i = 0; i < nb_samples; i++) - out[i] = (int16_t)(in[i * ch + c] >> 16); - } - } -} - void ffmpeg_enc_stage_packet(struct processing_module *mod, struct ffmpeg_enc_data *e, const uint8_t *data, size_t len) { diff --git a/src/audio/ffmpeg_dec/ffmpeg.cmake b/src/audio/ffmpeg_dec/ffmpeg.cmake index 47455628bca1..b558d95b5ebd 100644 --- a/src/audio/ffmpeg_dec/ffmpeg.cmake +++ b/src/audio/ffmpeg_dec/ffmpeg.cmake @@ -129,7 +129,7 @@ set(_ff_flacenc_prep "") if(CONFIG_FFMPEG_ENC_FLAC) set(_ff_extra_cflags "${_ff_extra_cflags} -I${SOF_FFMPEG_SRC_DIR}/libavcodec") set(_ff_flacenc_prep - COMMAND sh -c "sed -e 's/FLAC_MAX_BLOCKSIZE/1152/g' -e 's/rc_sums\\[32\\]/rc_sums[1]/g' -e 's/subframes\\[FLAC_MAX_CHANNELS\\]/subframes[2]/g' -e 's/channels > FLAC_MAX_CHANNELS/channels > 2/g' /libavcodec/flacenc.c > /libavcodec/flacenc.c") + COMMAND sh -c "sed -e 's/\\[FLAC_MAX_BLOCKSIZE\\]/[1152]/g' -e 's/\\[FLAC_MAX_BLOCKSIZE+11\\]/[1152+11]/g' -e 's/> FLAC_MAX_BLOCKSIZE/> 1152/g' -e 's/rc_sums\\[32\\]/rc_sums[1]/g' -e 's/subframes\\[FLAC_MAX_CHANNELS\\]/subframes[2]/g' -e 's/channels > FLAC_MAX_CHANNELS/channels > 2/g' /libavcodec/flacenc.c > /libavcodec/flacenc.c") endif() if(CMAKE_C_COMPILER_ID STREQUAL "Clang") diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-alac.c b/src/audio/ffmpeg_dec/ffmpeg_dec-alac.c index a75993a9d39e..c10548f84a74 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-alac.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-alac.c @@ -10,13 +10,50 @@ #include #include #include +#include +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +#include +#endif #include "ffmpeg_dec.h" LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); #define ALAC_EXTRADATA_SIZE 36 #define ALAC_SPECIFIC_CONFIG_SIZE 24 +#define ALAC_ATOM_HDR_SIZE 12 +#define ALAC_ATOM_TAG_OFFSET 4 +#define ALAC_COOKIE_SAMPLES_OFFSET 12 +#define ALAC_COOKIE_COMPAT_OFFSET 16 +#define ALAC_COOKIE_BITS_OFFSET 17 +#define ALAC_COOKIE_PB_OFFSET 18 +#define ALAC_COOKIE_MB_OFFSET 19 +#define ALAC_COOKIE_KB_OFFSET 20 +#define ALAC_COOKIE_CH_OFFSET 21 +#define ALAC_COOKIE_MAX_RUN_OFFSET 22 +#define ALAC_COOKIE_MAX_FRAME_OFFSET 24 +#define ALAC_COOKIE_AVG_BITRATE_OFFSET 28 +#define ALAC_COOKIE_SAMPLE_RATE_OFFSET 32 +#define ALAC_COOKIE_MIN_SIZE 22 + +#define ALAC_HEADER_TAG_SIZE 4 +#define ALAC_HEADER_PEEK_BYTES 8 +#define ALAC_CAF_HDR_SIZE 8 +#define ALAC_CAF_CHUNK_HDR_SIZE 12 +#define ALAC_CAF_DATA_EDIT_COUNT_SIZE 4 +#define ALAC_CAF_DATA_HDR_SIZE (ALAC_CAF_CHUNK_HDR_SIZE + ALAC_CAF_DATA_EDIT_COUNT_SIZE) +#define ALAC_CAF_MAX_CHUNK_SIZE 0x10000000LL + #define ALAC_DEFAULT_FRAME_SAMPLES 4096 +#define ALAC_MAX_FRAME_SAMPLES 16384 +#define ALAC_MAX_BITS_PER_SAMPLE 32 +#define ALAC_DEFAULT_COMPAT_VERSION 0 +#define ALAC_DEFAULT_SAMPLE_SIZE 16 +#define ALAC_DEFAULT_PB 40 +#define ALAC_DEFAULT_MB 10 +#define ALAC_DEFAULT_KB 14 +#define ALAC_DEFAULT_CHANNELS 2 +#define ALAC_DEFAULT_MAX_RUN 255 +#define ALAC_DEFAULT_SAMPLE_RATE 48000 /* * Prefix layout of libavcodec's private ALACContext (libavcodec/alac.c): @@ -28,7 +65,16 @@ LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); * padded to the next byte boundary by flush_put_bits(). Because libavcodec * does not ship an AVCodecParser for ALAC, reading gb.index after * avcodec_receive_frame() yields the exact bit count of the decoded frame. + * The _Static_assert below pins the expected libavcodec version so any + * submodule upgrade forces re-verification of ALACContext's prefix layout. */ +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +_Static_assert(LIBAVCODEC_VERSION_MAJOR == 61 && + LIBAVCODEC_VERSION_MINOR == 19 && + LIBAVCODEC_VERSION_MICRO == 101, + "struct alac_priv_prefix must be verified against ALACContext in libavcodec/alac.c"); +#endif + struct alac_priv_prefix { const void *av_class; void *avctx; @@ -67,11 +113,12 @@ static inline void alac_write_be32(uint8_t *p, uint32_t v) static uint32_t ffmpeg_dec_alac_max_frame_samples(const struct ffmpeg_dec_comp_data *cd) { - if (cd->extradata_size >= 16 && - memcmp(&cd->extradata[4], "alac", 4) == 0) { - uint32_t max_samples = alac_read_be32(&cd->extradata[12]); + if (cd->extradata_size >= ALAC_COOKIE_SAMPLES_OFFSET + 4 && + memcmp(&cd->extradata[ALAC_ATOM_TAG_OFFSET], "alac", 4) == 0) { + uint32_t max_samples = + alac_read_be32(&cd->extradata[ALAC_COOKIE_SAMPLES_OFFSET]); - if (max_samples > 0 && max_samples <= 16384) + if (max_samples > 0 && max_samples <= ALAC_MAX_FRAME_SAMPLES) return max_samples; } return ALAC_DEFAULT_FRAME_SAMPLES; @@ -80,11 +127,17 @@ static uint32_t ffmpeg_dec_alac_max_frame_samples(const struct ffmpeg_dec_comp_d static size_t ffmpeg_dec_alac_frame_bytes_consumed(const void *priv_data) { const struct alac_priv_prefix *priv = priv_data; + size_t bytes; - if (!priv || priv->gb_index <= 0) + if (!priv || !priv->gb_buffer || !priv->gb_buffer_end || + priv->gb_buffer_end < priv->gb_buffer || priv->gb_index <= 0) return 0; - return (size_t)((priv->gb_index + 7) >> 3); + bytes = (size_t)((priv->gb_index + 7) >> 3); + if (bytes > (size_t)(priv->gb_buffer_end - priv->gb_buffer)) + return 0; + + return bytes; } /* @@ -117,34 +170,35 @@ static int ffmpeg_dec_alac_skip_container(struct processing_module *mod, /* Step 2: Parse CAF chunks until the "data" chunk payload is reached */ while (cd->alac_parsing_caf) { - uint8_t chunk_hdr[12]; + uint8_t chunk_hdr[ALAC_CAF_CHUNK_HDR_SIZE]; int64_t chunk_size; avail = source_get_data_available(src); - if (avail < 12) + if (avail < ALAC_CAF_CHUNK_HDR_SIZE) return -ENODATA; - ret = source_get_data(src, 12, &sp, &sstart, &sbytes); + ret = source_get_data(src, ALAC_CAF_CHUNK_HDR_SIZE, &sp, &sstart, &sbytes); if (ret) return ret; - ffmpeg_dec_copy_from_circular(chunk_hdr, sp, sstart, sbytes, 12); + ffmpeg_dec_copy_from_circular(chunk_hdr, sp, sstart, sbytes, + ALAC_CAF_CHUNK_HDR_SIZE); source_release_data(src, 0); chunk_size = (int64_t)alac_read_be64(&chunk_hdr[4]); /* "data" chunk: 12-byte header + 4-byte mEditCount, followed by ALAC frames */ if (memcmp(chunk_hdr, "data", 4) == 0) { - if (avail < 16) + if (avail < ALAC_CAF_DATA_HDR_SIZE) return -ENODATA; - source_drop_data(src, 16); + source_drop_data(src, ALAC_CAF_DATA_HDR_SIZE); cd->alac_parsing_caf = false; comp_info(dev, "ALAC CAF data chunk reached (size %lld)", (long long)chunk_size); return 0; } - if (chunk_size < 0 || chunk_size > 0x10000000LL) { + if (chunk_size < 0 || chunk_size > ALAC_CAF_MAX_CHUNK_SIZE) { comp_err(dev, "invalid CAF chunk %.4s size %lld", (const char *)chunk_hdr, (long long)chunk_size); return -EINVAL; @@ -153,8 +207,8 @@ static int ffmpeg_dec_alac_skip_container(struct processing_module *mod, /* "kuki" (magic cookie) chunk: extract 36-byte QuickTime 'alac' atom */ if (memcmp(chunk_hdr, "kuki", 4) == 0 && chunk_size >= ALAC_EXTRADATA_SIZE && - 12 + (size_t)chunk_size <= cd->in_buf_size) { - req = 12 + (size_t)chunk_size; + ALAC_CAF_CHUNK_HDR_SIZE + (size_t)chunk_size <= cd->in_buf_size) { + req = ALAC_CAF_CHUNK_HDR_SIZE + (size_t)chunk_size; if (avail < req) return -ENODATA; @@ -165,7 +219,7 @@ static int ffmpeg_dec_alac_skip_container(struct processing_module *mod, source_release_data(src, 0); if (!cd->extradata_size) { - const uint8_t *cookie = &cd->in_buf[12]; + const uint8_t *cookie = &cd->in_buf[ALAC_CAF_CHUNK_HDR_SIZE]; size_t cookie_len = (size_t)chunk_size; const uint8_t *atom = NULL; @@ -173,7 +227,8 @@ static int ffmpeg_dec_alac_skip_container(struct processing_module *mod, for (size_t off = 0; off + ALAC_EXTRADATA_SIZE <= cookie_len; off++) { if (alac_read_be32(&cookie[off]) == ALAC_EXTRADATA_SIZE && - memcmp(&cookie[off + 4], "alac", 4) == 0) { + memcmp(&cookie[off + ALAC_ATOM_TAG_OFFSET], + "alac", 4) == 0) { atom = &cookie[off]; break; } @@ -189,7 +244,7 @@ static int ffmpeg_dec_alac_skip_container(struct processing_module *mod, } /* Skip other CAF metadata chunks ("desc", "chan", "info", "free", etc.) */ - size_t chunk_total = 12 + (size_t)chunk_size; + size_t chunk_total = ALAC_CAF_CHUNK_HDR_SIZE + (size_t)chunk_size; if (avail >= chunk_total) { source_drop_data(src, chunk_total); @@ -209,38 +264,56 @@ static int ffmpeg_dec_alac_skip_container(struct processing_module *mod, } while (ret > 0); avail = source_get_data_available(src); + if (avail < ALAC_HEADER_TAG_SIZE) + return -ENODATA; + + ret = source_get_data(src, ALAC_HEADER_TAG_SIZE, &sp, &sstart, &sbytes); + if (ret) + return ret; + ffmpeg_dec_copy_from_circular(cd->in_buf, sp, sstart, sbytes, + ALAC_HEADER_TAG_SIZE); + source_release_data(src, 0); /* Step 4: Check for the 8-byte Core Audio Format ("caff\x00\x01\x00\x00") header */ if (memcmp(cd->in_buf, "caff", 4) == 0) { - if (avail < 8) + if (avail < ALAC_CAF_HDR_SIZE) return -ENODATA; - comp_info(dev, "CAF header detected, parsing ALAC container chunks"); - source_drop_data(src, 8); + source_drop_data(src, ALAC_CAF_HDR_SIZE); cd->alac_parsing_caf = true; return ffmpeg_dec_alac_skip_container(mod, src); } /* Step 5: Check for an inline 36-byte QuickTime 'alac' atom prefix */ - if (avail >= 8 && - alac_read_be32(cd->in_buf) == ALAC_EXTRADATA_SIZE && - memcmp(&cd->in_buf[4], "alac", 4) == 0) { - if (avail < ALAC_EXTRADATA_SIZE) + if (alac_read_be32(cd->in_buf) == ALAC_EXTRADATA_SIZE) { + if (avail < ALAC_HEADER_PEEK_BYTES) return -ENODATA; - ret = source_get_data(src, ALAC_EXTRADATA_SIZE, &sp, &sstart, &sbytes); + ret = source_get_data(src, ALAC_HEADER_PEEK_BYTES, &sp, &sstart, &sbytes); if (ret) return ret; ffmpeg_dec_copy_from_circular(cd->in_buf, sp, sstart, sbytes, - ALAC_EXTRADATA_SIZE); + ALAC_HEADER_PEEK_BYTES); source_release_data(src, 0); - if (!cd->extradata_size) { - ffmpeg_dec_store_extradata(mod, cd->in_buf, ALAC_EXTRADATA_SIZE); - comp_info(dev, "extracted inline 36-byte ALAC atom header"); + if (memcmp(&cd->in_buf[ALAC_ATOM_TAG_OFFSET], "alac", 4) == 0) { + if (avail < ALAC_EXTRADATA_SIZE) + return -ENODATA; + + ret = source_get_data(src, ALAC_EXTRADATA_SIZE, &sp, &sstart, &sbytes); + if (ret) + return ret; + ffmpeg_dec_copy_from_circular(cd->in_buf, sp, sstart, sbytes, + ALAC_EXTRADATA_SIZE); + source_release_data(src, 0); + + if (!cd->extradata_size) { + ffmpeg_dec_store_extradata(mod, cd->in_buf, ALAC_EXTRADATA_SIZE); + comp_info(dev, "extracted inline 36-byte ALAC atom header"); + } + source_drop_data(src, ALAC_EXTRADATA_SIZE); + return 0; } - source_drop_data(src, ALAC_EXTRADATA_SIZE); - return 0; } return 0; @@ -250,7 +323,9 @@ static void ffmpeg_dec_alac_resize_pcm_buf(struct processing_module *mod) { struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); uint32_t max_samples = ffmpeg_dec_alac_max_frame_samples(cd); - size_t frame_bytes = cd->out_frame_bytes ? cd->out_frame_bytes : 8; + size_t frame_bytes = cd->out_frame_bytes ? + cd->out_frame_bytes : + ALAC_DEFAULT_CHANNELS * sizeof(int32_t); size_t need = (size_t)max_samples * frame_bytes; if (need > cd->pcm_buf_size) { @@ -269,30 +344,30 @@ static void ffmpeg_dec_alac_prepare_extradata(struct processing_module *mod) struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); uint8_t atom[ALAC_EXTRADATA_SIZE]; uint32_t frame_length = ALAC_DEFAULT_FRAME_SAMPLES; - uint8_t compatible_version = 0; - uint8_t sample_size = 16; - uint8_t pb = 40; - uint8_t mb = 10; - uint8_t kb = 14; - uint8_t channels = cd->out_channels ? (uint8_t)cd->out_channels : 2; - uint16_t max_run = 255; + uint8_t compatible_version = ALAC_DEFAULT_COMPAT_VERSION; + uint8_t sample_size = ALAC_DEFAULT_SAMPLE_SIZE; + uint8_t pb = ALAC_DEFAULT_PB; + uint8_t mb = ALAC_DEFAULT_MB; + uint8_t kb = ALAC_DEFAULT_KB; + uint8_t channels = cd->out_channels ? (uint8_t)cd->out_channels : ALAC_DEFAULT_CHANNELS; + uint16_t max_run = ALAC_DEFAULT_MAX_RUN; uint32_t max_frame_bytes = 0; uint32_t avg_bitrate = 0; - uint32_t sample_rate = cd->out_rate ? cd->out_rate : 48000; + uint32_t sample_rate = cd->out_rate ? cd->out_rate : ALAC_DEFAULT_SAMPLE_RATE; /* Valid 36-byte QuickTime 'alac' atom already present */ if (cd->extradata_size == ALAC_EXTRADATA_SIZE && - memcmp(&cd->extradata[4], "alac", 4) == 0) { + memcmp(&cd->extradata[ALAC_ATOM_TAG_OFFSET], "alac", 4) == 0) { ffmpeg_dec_alac_resize_pcm_buf(mod); return; } /* Wrap a raw 24-byte ALACSpecificConfig into the 36-byte QuickTime 'alac' atom */ if (cd->extradata_size == ALAC_SPECIFIC_CONFIG_SIZE) { - memset(atom, 0, 12); + memset(atom, 0, ALAC_ATOM_HDR_SIZE); alac_write_be32(&atom[0], ALAC_EXTRADATA_SIZE); - memcpy_s(&atom[4], 4, "alac", 4); - memcpy_s(&atom[12], ALAC_SPECIFIC_CONFIG_SIZE, + memcpy_s(&atom[ALAC_ATOM_TAG_OFFSET], 4, "alac", 4); + memcpy_s(&atom[ALAC_ATOM_HDR_SIZE], ALAC_SPECIFIC_CONFIG_SIZE, cd->extradata, ALAC_SPECIFIC_CONFIG_SIZE); ffmpeg_dec_store_extradata(mod, atom, ALAC_EXTRADATA_SIZE); ffmpeg_dec_alac_resize_pcm_buf(mod); @@ -306,7 +381,8 @@ static void ffmpeg_dec_alac_prepare_extradata(struct processing_module *mod) const struct snd_codec *codec_params = mod->priv.cfg.ext_data->module_data; const struct snd_dec_alac *alac_opt = &codec_params->options.alac_d; - if (alac_opt->frame_length > 0 && alac_opt->frame_length <= 16384) + if (alac_opt->frame_length > 0 && + alac_opt->frame_length <= ALAC_MAX_FRAME_SAMPLES) frame_length = alac_opt->frame_length; if (alac_opt->pb > 0) pb = alac_opt->pb; @@ -314,7 +390,7 @@ static void ffmpeg_dec_alac_prepare_extradata(struct processing_module *mod) mb = alac_opt->mb; if (alac_opt->kb > 0) kb = alac_opt->kb; - if (alac_opt->max_run > 0 && alac_opt->max_run <= 0xFFFF) + if (alac_opt->max_run > 0 && alac_opt->max_run <= UINT16_MAX) max_run = (uint16_t)alac_opt->max_run; if (alac_opt->max_frame_bytes > 0) max_frame_bytes = alac_opt->max_frame_bytes; @@ -323,18 +399,18 @@ static void ffmpeg_dec_alac_prepare_extradata(struct processing_module *mod) memset(atom, 0, sizeof(atom)); alac_write_be32(&atom[0], ALAC_EXTRADATA_SIZE); - memcpy_s(&atom[4], 4, "alac", 4); - alac_write_be32(&atom[12], frame_length); - atom[16] = compatible_version; - atom[17] = sample_size; - atom[18] = pb; - atom[19] = mb; - atom[20] = kb; - atom[21] = channels; - alac_write_be16(&atom[22], max_run); - alac_write_be32(&atom[24], max_frame_bytes); - alac_write_be32(&atom[28], avg_bitrate); - alac_write_be32(&atom[32], sample_rate); + memcpy_s(&atom[ALAC_ATOM_TAG_OFFSET], 4, "alac", 4); + alac_write_be32(&atom[ALAC_COOKIE_SAMPLES_OFFSET], frame_length); + atom[ALAC_COOKIE_COMPAT_OFFSET] = compatible_version; + atom[ALAC_COOKIE_BITS_OFFSET] = sample_size; + atom[ALAC_COOKIE_PB_OFFSET] = pb; + atom[ALAC_COOKIE_MB_OFFSET] = mb; + atom[ALAC_COOKIE_KB_OFFSET] = kb; + atom[ALAC_COOKIE_CH_OFFSET] = channels; + alac_write_be16(&atom[ALAC_COOKIE_MAX_RUN_OFFSET], max_run); + alac_write_be32(&atom[ALAC_COOKIE_MAX_FRAME_OFFSET], max_frame_bytes); + alac_write_be32(&atom[ALAC_COOKIE_AVG_BITRATE_OFFSET], avg_bitrate); + alac_write_be32(&atom[ALAC_COOKIE_SAMPLE_RATE_OFFSET], sample_rate); ffmpeg_dec_store_extradata(mod, atom, ALAC_EXTRADATA_SIZE); ffmpeg_dec_alac_resize_pcm_buf(mod); @@ -342,6 +418,40 @@ static void ffmpeg_dec_alac_prepare_extradata(struct processing_module *mod) frame_length, sample_rate, channels); } +static int ffmpeg_dec_alac_configure_avctx(struct processing_module *mod, void *avctx) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + uint32_t max_ch = cd->out_channels ? cd->out_channels : ALAC_DEFAULT_CHANNELS; + uint32_t cookie_samples = 0; + uint8_t cookie_bits = 0; + uint8_t cookie_ch = 0; + + (void)avctx; + + if (cd->extradata_size >= ALAC_COOKIE_MIN_SIZE && + memcmp(&cd->extradata[ALAC_ATOM_TAG_OFFSET], "alac", 4) == 0) { + cookie_samples = alac_read_be32(&cd->extradata[ALAC_COOKIE_SAMPLES_OFFSET]); + cookie_bits = cd->extradata[ALAC_COOKIE_BITS_OFFSET]; + cookie_ch = cd->extradata[ALAC_COOKIE_CH_OFFSET]; + } + if (cookie_samples == 0 || cookie_samples > ALAC_MAX_FRAME_SAMPLES || + cookie_bits == 0 || cookie_bits > ALAC_MAX_BITS_PER_SAMPLE || + cookie_ch == 0 || cookie_ch > max_ch) { + comp_err(mod->dev, "unsupported ALAC cookie (samples=%u bits=%u ch=%u)", + cookie_samples, cookie_bits, cookie_ch); + return -EINVAL; + } + + return 0; +} + +static bool ffmpeg_dec_alac_is_trailing_eof(const uint8_t *pkt_data, size_t pkt_len) +{ + return pkt_len >= 4 && + (memcmp(pkt_data, "pakt", 4) == 0 || + memcmp(pkt_data, "free", 4) == 0); +} + const struct ffmpeg_dec_codec_ops ffmpeg_dec_alac_ops = { .id = FFMPEG_DEC_CODEC_ALAC, .snd_codec_id = SND_AUDIOCODEC_ALAC, @@ -349,5 +459,7 @@ const struct ffmpeg_dec_codec_ops ffmpeg_dec_alac_ops = { .max_frame_samples = ffmpeg_dec_alac_max_frame_samples, .skip_container = ffmpeg_dec_alac_skip_container, .prepare_extradata = ffmpeg_dec_alac_prepare_extradata, + .configure_avctx = ffmpeg_dec_alac_configure_avctx, + .is_trailing_eof = ffmpeg_dec_alac_is_trailing_eof, .frame_bytes_consumed = ffmpeg_dec_alac_frame_bytes_consumed, }; diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-aptx.c b/src/audio/ffmpeg_dec/ffmpeg_dec-aptx.c index 60f793f5fa49..2afd06f56e38 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-aptx.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-aptx.c @@ -6,10 +6,16 @@ #include #include +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +#include +#include +#endif #include "ffmpeg_dec.h" #define APTX_DEC_MAX_SAMPLES 1024 #define APTX_BLOCK_ALIGN 4 +#define APTX_DEFAULT_CHANNELS 2 +#define APTX_DEFAULT_RATE 48000 static uint32_t ffmpeg_dec_aptx_max_frame_samples(const struct ffmpeg_dec_comp_data *cd) { @@ -31,11 +37,29 @@ static size_t ffmpeg_dec_aptx_limit_packet_size(const struct ffmpeg_dec_comp_dat return clamped & ~(size_t)(APTX_BLOCK_ALIGN - 1); } +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +static int ffmpeg_dec_aptx_configure_avctx(struct processing_module *mod, void *ctx) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + AVCodecContext *avctx = ctx; + + if (avctx->ch_layout.nb_channels <= 0) + av_channel_layout_default(&avctx->ch_layout, APTX_DEFAULT_CHANNELS); + if (avctx->sample_rate <= 0) + avctx->sample_rate = cd->out_rate ? cd->out_rate : APTX_DEFAULT_RATE; + + return 0; +} +#endif + const struct ffmpeg_dec_codec_ops ffmpeg_dec_aptx_ops = { .id = FFMPEG_DEC_CODEC_APTX, .snd_codec_id = SND_AUDIOCODEC_APTX, .name = "aptx", .decoder_name = "aptx", .max_frame_samples = ffmpeg_dec_aptx_max_frame_samples, +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) + .configure_avctx = ffmpeg_dec_aptx_configure_avctx, +#endif .limit_packet_size = ffmpeg_dec_aptx_limit_packet_size, }; diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-convert.c b/src/audio/ffmpeg_dec/ffmpeg_dec-convert.c index f48290fe23e8..ba697f3fb496 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-convert.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-convert.c @@ -12,6 +12,7 @@ #include #include #include "ffmpeg_dec-convert.h" +#include "ffmpeg_dec.h" /* * Branch-free soft-float-free float -> Q1.31 for f in [-1.0, 1.0). @@ -387,37 +388,62 @@ static void conv_s32_to_fltp(const int32_t *src, float *const *dst, } } -static inline int16_t pcm_frame_to_s16_mono(const uint8_t *in, int idx, - int src_fmt, int src_ch) +static inline bool sof_pcm_fmt_supported(int src_fmt) { - int32_t acc = 0; - int c; + return src_fmt == SOF_IPC_FRAME_S16_LE || + src_fmt == SOF_IPC_FRAME_S24_4LE || + src_fmt == SOF_IPC_FRAME_S32_LE; +} - if (src_fmt == SOF_IPC_FRAME_S16_LE) { - const int16_t *s16 = (const int16_t *)in + (size_t)idx * src_ch; +static inline int32_t sof_pcm_sample_to_q31(const uint8_t *in, size_t idx, int src_fmt) +{ + switch (src_fmt) { + case SOF_IPC_FRAME_S16_LE: + return Q_SHIFT_LEFT((int32_t)((const int16_t *)in)[idx], 15, 31); + case SOF_IPC_FRAME_S24_4LE: + return Q_SHIFT_LEFT(sign_extend_s24(((const int32_t *)in)[idx]), 23, 31); + case SOF_IPC_FRAME_S32_LE: + default: + return ((const int32_t *)in)[idx]; + } +} - if (src_ch == 1) - return s16[0]; - for (c = 0; c < src_ch; c++) - acc += s16[c]; - return (int16_t)(acc / src_ch); +void ffmpeg_enc_pcm_to_s16_interleaved(const uint8_t *pcm_in, int16_t *dst, + size_t total_samples, int src_fmt) +{ + if (src_fmt == SOF_IPC_FRAME_S16_LE) { + memcpy_s(dst, total_samples * sizeof(int16_t), + pcm_in, total_samples * sizeof(int16_t)); + return; } - if (src_fmt == SOF_IPC_FRAME_S24_4LE) { - const int32_t *s24 = (const int32_t *)in + (size_t)idx * src_ch; + for (size_t i = 0; i < total_samples; i++) + dst[i] = (int16_t)Q_SHIFT(sof_pcm_sample_to_q31(pcm_in, i, src_fmt), 31, 15); +} + +void ffmpeg_enc_pcm_to_s16_planar(const uint8_t *pcm_in, uint8_t * const *planes, + int nb_samples, int ch, int src_fmt) +{ + for (int c = 0; c < ch; c++) { + int16_t *out = (int16_t *)planes[c]; + + for (int i = 0; i < nb_samples; i++) { + int32_t q = sof_pcm_sample_to_q31(pcm_in, (size_t)i * ch + c, src_fmt); - for (c = 0; c < src_ch; c++) - acc += Q_SHIFT(sign_extend_s24(s24[c]), 23, 15); - return (int16_t)(acc / src_ch); + out[i] = (int16_t)Q_SHIFT(q, 31, 15); + } } +} - { - const int32_t *s32 = (const int32_t *)in + (size_t)idx * src_ch; +static inline int16_t pcm_frame_to_s16_mono(const uint8_t *in, int idx, + int src_fmt, int src_ch) +{ + int64_t acc = 0; - for (c = 0; c < src_ch; c++) - acc += Q_SHIFT(s32[c], 31, 15); - return (int16_t)(acc / src_ch); - } + for (int c = 0; c < src_ch; c++) + acc += sof_pcm_sample_to_q31(in, (size_t)idx * src_ch + c, src_fmt); + + return (int16_t)Q_SHIFT((int32_t)(acc / src_ch), 31, 15); } void ffmpeg_dec_pcm_decimate_3to1_s16_mono(const uint8_t *in, int16_t *dst, @@ -472,187 +498,62 @@ int ffmpeg_dec_pcm_to_frame(const uint8_t *in, AVFrame *frame, { int dst_ch; - if (!in || !frame || channels <= 0 || nb_samples <= 0) + if (!in || !frame || channels <= 0 || nb_samples <= 0 || + !sof_pcm_fmt_supported(src_fmt)) return -EINVAL; dst_ch = frame->ch_layout.nb_channels; if (dst_ch != channels && !(dst_ch == 1 && frame->format == AV_SAMPLE_FMT_S16)) return -EINVAL; - if (frame->format == AV_SAMPLE_FMT_S16) { + switch (frame->format) { + case AV_SAMPLE_FMT_S16: { int16_t *dst = (int16_t *)frame->data[0]; if (dst_ch == 1 && channels > 1) { for (int i = 0; i < nb_samples; i++) dst[i] = pcm_frame_to_s16_mono(in, i, src_fmt, channels); - return 0; - } - - size_t total = (size_t)nb_samples * dst_ch; - - if (src_fmt == SOF_IPC_FRAME_S16_LE) { - memcpy_s(dst, total * sizeof(int16_t), - in, total * sizeof(int16_t)); - return 0; - } - if (src_fmt == SOF_IPC_FRAME_S24_4LE) { - const int32_t *src24 = (const int32_t *)in; - - for (size_t i = 0; i < total; i++) - dst[i] = (int16_t)Q_SHIFT(sign_extend_s24(src24[i]), 23, 15); - return 0; - } - if (src_fmt == SOF_IPC_FRAME_S32_LE) { - const int32_t *src32 = (const int32_t *)in; - - for (size_t i = 0; i < total; i++) - dst[i] = (int16_t)Q_SHIFT(src32[i], 31, 15); - return 0; - } - return -EINVAL; - } - - if (frame->format == AV_SAMPLE_FMT_S16P) { - if (src_fmt == SOF_IPC_FRAME_S16_LE) { - const int16_t *src16 = (const int16_t *)in; - - for (int c = 0; c < dst_ch; c++) { - int16_t *out = (int16_t *)frame->data[c]; - - for (int i = 0; i < nb_samples; i++) - out[i] = src16[i * dst_ch + c]; - } - return 0; - } - if (src_fmt == SOF_IPC_FRAME_S24_4LE) { - const int32_t *src24 = (const int32_t *)in; - - for (int c = 0; c < dst_ch; c++) { - int16_t *out = (int16_t *)frame->data[c]; - - for (int i = 0; i < nb_samples; i++) { - int32_t v = sign_extend_s24(src24[i * dst_ch + c]); - - out[i] = (int16_t)Q_SHIFT(v, 23, 15); - } - } - return 0; - } - if (src_fmt == SOF_IPC_FRAME_S32_LE) { - const int32_t *src32 = (const int32_t *)in; - - for (int c = 0; c < dst_ch; c++) { - int16_t *out = (int16_t *)frame->data[c]; - - for (int i = 0; i < nb_samples; i++) - out[i] = (int16_t)Q_SHIFT(src32[i * dst_ch + c], 31, 15); - } - return 0; + } else { + ffmpeg_enc_pcm_to_s16_interleaved(in, dst, + (size_t)nb_samples * dst_ch, + src_fmt); } - return -EINVAL; + return 0; } - - if (frame->format == AV_SAMPLE_FMT_S32) { + case AV_SAMPLE_FMT_S16P: + ffmpeg_enc_pcm_to_s16_planar(in, frame->data, nb_samples, dst_ch, src_fmt); + return 0; + case AV_SAMPLE_FMT_S32: { int32_t *dst = (int32_t *)frame->data[0]; size_t total = (size_t)nb_samples * dst_ch; - if (src_fmt == SOF_IPC_FRAME_S16_LE) { - const int16_t *src16 = (const int16_t *)in; - - for (size_t i = 0; i < total; i++) - dst[i] = Q_SHIFT_LEFT((int32_t)src16[i], 15, 31); - return 0; - } - if (src_fmt == SOF_IPC_FRAME_S24_4LE) { - const int32_t *src24 = (const int32_t *)in; - - for (size_t i = 0; i < total; i++) - dst[i] = Q_SHIFT_LEFT(sign_extend_s24(src24[i]), 23, 31); - return 0; - } if (src_fmt == SOF_IPC_FRAME_S32_LE) { memcpy_s(dst, total * sizeof(int32_t), in, total * sizeof(int32_t)); - return 0; + } else { + for (size_t i = 0; i < total; i++) + dst[i] = sof_pcm_sample_to_q31(in, i, src_fmt); } - return -EINVAL; + return 0; } + case AV_SAMPLE_FMT_S32P: + for (int c = 0; c < dst_ch; c++) { + int32_t *out = (int32_t *)frame->data[c]; - if (frame->format == AV_SAMPLE_FMT_S32P) { - if (src_fmt == SOF_IPC_FRAME_S16_LE) { - const int16_t *src16 = (const int16_t *)in; - - for (int c = 0; c < dst_ch; c++) { - int32_t *out = (int32_t *)frame->data[c]; - - for (int i = 0; i < nb_samples; i++) { - int32_t v = src16[i * dst_ch + c]; - - out[i] = Q_SHIFT_LEFT(v, 15, 31); - } - } - return 0; - } - if (src_fmt == SOF_IPC_FRAME_S24_4LE) { - const int32_t *src24 = (const int32_t *)in; - - for (int c = 0; c < dst_ch; c++) { - int32_t *out = (int32_t *)frame->data[c]; - - for (int i = 0; i < nb_samples; i++) { - int32_t v = sign_extend_s24(src24[i * dst_ch + c]); - - out[i] = Q_SHIFT_LEFT(v, 23, 31); - } - } - return 0; - } - if (src_fmt == SOF_IPC_FRAME_S32_LE) { - const int32_t *src32 = (const int32_t *)in; - - for (int c = 0; c < dst_ch; c++) { - int32_t *out = (int32_t *)frame->data[c]; - - for (int i = 0; i < nb_samples; i++) - out[i] = src32[i * dst_ch + c]; - } - return 0; + for (int i = 0; i < nb_samples; i++) + out[i] = sof_pcm_sample_to_q31(in, (size_t)i * dst_ch + c, + src_fmt); } - return -EINVAL; - } - - if (frame->format == AV_SAMPLE_FMT_DBL) { + return 0; + case AV_SAMPLE_FMT_DBL: { double *dst = (double *)frame->data[0]; - int total = nb_samples * channels; - - switch (src_fmt) { - case SOF_IPC_FRAME_S16_LE: { - const int16_t *src16 = (const int16_t *)in; - - for (int i = 0; i < total; i++) - dst[i] = Q_CONVERT_QTOD(src16[i], 15); - return 0; - } - case SOF_IPC_FRAME_S24_4LE: { - const int32_t *src24 = (const int32_t *)in; + size_t total = (size_t)nb_samples * channels; - for (int i = 0; i < total; i++) - dst[i] = Q_CONVERT_QTOD(sign_extend_s24(src24[i]), 23); - return 0; - } - case SOF_IPC_FRAME_S32_LE: { - const int32_t *src32 = (const int32_t *)in; - - for (int i = 0; i < total; i++) - dst[i] = Q_CONVERT_QTOD(src32[i], 31); - return 0; - } - default: - return -EINVAL; - } + for (size_t i = 0; i < total; i++) + dst[i] = Q_CONVERT_QTOD(sof_pcm_sample_to_q31(in, i, src_fmt), 31); + return 0; } - - if (frame->format == AV_SAMPLE_FMT_FLTP) { + case AV_SAMPLE_FMT_FLTP: { float *const *dst = (float *const *)frame->data; switch (src_fmt) { @@ -669,8 +570,9 @@ int ffmpeg_dec_pcm_to_frame(const uint8_t *in, AVFrame *frame, return -EINVAL; } } - - return -EINVAL; + default: + return -EINVAL; + } } /* ------------------------------------------------------------------------- diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c b/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c index a87cf881aa3b..15a3f0c2b63c 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c @@ -27,7 +27,6 @@ #include #include #include -#include #include LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); @@ -205,28 +204,10 @@ static int ffmpeg_dec_ff_configure(struct processing_module *mod) return -ENOMEM; } - if (cd->codec == FFMPEG_DEC_CODEC_ALAC) { - uint32_t max_ch = cd->out_channels ? cd->out_channels : 2; - uint32_t cookie_samples = 0; - uint8_t cookie_bits = 0; - uint8_t cookie_ch = 0; - - if (cd->extradata_size >= 22 && - memcmp(&cd->extradata[4], "alac", 4) == 0) { - cookie_samples = ((uint32_t)cd->extradata[12] << 24) | - ((uint32_t)cd->extradata[13] << 16) | - ((uint32_t)cd->extradata[14] << 8) | - (uint32_t)cd->extradata[15]; - cookie_bits = cd->extradata[17]; - cookie_ch = cd->extradata[21]; - } - if (cookie_samples == 0 || cookie_samples > 16384 || - cookie_bits == 0 || cookie_bits > 32 || - cookie_ch == 0 || cookie_ch > max_ch) { - comp_err(dev, "unsupported ALAC cookie (samples=%u bits=%u ch=%u)", - cookie_samples, cookie_bits, cookie_ch); - return -EINVAL; - } + if (cd->codec_ops && cd->codec_ops->configure_avctx) { + ret = cd->codec_ops->configure_avctx(mod, ff->avctx); + if (ret < 0) + return ret; } if (cd->extradata_size) { @@ -253,28 +234,6 @@ static int ffmpeg_dec_ff_configure(struct processing_module *mod) if (ff->avctx->max_samples <= 0) ff->avctx->max_samples = INT_MAX; - if (cd->codec == FFMPEG_DEC_CODEC_G722) { - int bpc = 8; - - if (cd->req_bit_rate == 48000) { - bpc = 6; - } else if (cd->req_bit_rate == 56000) { - bpc = 7; - } else if (cd->req_bit_rate != 0 && cd->req_bit_rate != 64000) { - comp_err(dev, "unsupported G.722 bitrate %u", cd->req_bit_rate); - return -EINVAL; - } - ff->avctx->sample_rate = 16000; - av_opt_set_int(ff->avctx->priv_data, "bits_per_codeword", bpc, 0); - } - - if (cd->codec == FFMPEG_DEC_CODEC_APTX) { - if (ff->avctx->ch_layout.nb_channels <= 0) - av_channel_layout_default(&ff->avctx->ch_layout, 2); - if (ff->avctx->sample_rate <= 0) - ff->avctx->sample_rate = cd->out_rate ? cd->out_rate : 48000; - } - ret = avcodec_open2(ff->avctx, ff->codec, NULL); if (ret < 0) { comp_err(dev, "avcodec_open2 failed %d", ret); @@ -344,10 +303,11 @@ static int ffmpeg_dec_ff_decode(struct processing_module *mod, size_t prev_len = ff->pktbuf_len; size_t cap = ff->pktbuf_sz > AV_INPUT_BUFFER_PADDING_SIZE ? ff->pktbuf_sz - AV_INPUT_BUFFER_PADDING_SIZE : 0; - const uint8_t *pkt_data; size_t pkt_len; + size_t copy = 0; + int prev_log_level; - if (cd->alac_skip_to_eof) { + if (cd->unparsed_skip_to_eof) { *consumed = in_size; return 0; } @@ -355,29 +315,21 @@ static int ffmpeg_dec_ff_decode(struct processing_module *mod, if (!in || !in_size) { if (!prev_len) return 0; - pkt_data = ff->pktbuf; pkt_len = prev_len; - } else if (prev_len == 0) { - pkt_data = in; - pkt_len = in_size; } else { size_t space = prev_len < cap ? cap - prev_len : 0; - size_t copy = in_size < space ? in_size : space; - if (copy) { + copy = in_size < space ? in_size : space; + if (copy) memcpy_s(ff->pktbuf + prev_len, space, in, copy); - memset(ff->pktbuf + prev_len + copy, 0, - AV_INPUT_BUFFER_PADDING_SIZE); - } - pkt_data = ff->pktbuf; pkt_len = prev_len + copy; + memset(ff->pktbuf + pkt_len, 0, AV_INPUT_BUFFER_PADDING_SIZE); } - /* Stop cleanly if a trailing CAF chunk ("pakt" or "free") is reached */ - if (cd->codec == FFMPEG_DEC_CODEC_ALAC && pkt_len >= 4 && - (memcmp(pkt_data, "pakt", 4) == 0 || - memcmp(pkt_data, "free", 4) == 0)) { - cd->alac_skip_to_eof = true; + /* Stop cleanly if trailing container metadata is reached */ + if (cd->codec_ops && cd->codec_ops->is_trailing_eof && + cd->codec_ops->is_trailing_eof(ff->pktbuf, pkt_len)) { + cd->unparsed_skip_to_eof = true; ff->pktbuf_len = 0; *consumed = in_size; return 0; @@ -393,45 +345,25 @@ static int ffmpeg_dec_ff_decode(struct processing_module *mod, ff->pktbuf_len = 0; return 0; } - if (prev_len == 0) { - size_t copy = in_size < cap ? in_size : cap; - - memcpy_s(ff->pktbuf, cap, in, copy); - memset(ff->pktbuf + copy, 0, - AV_INPUT_BUFFER_PADDING_SIZE); - ff->pktbuf_len = copy; - *consumed = copy; - } else { - size_t space = prev_len < cap ? cap - prev_len : 0; - size_t copy = in_size < space ? in_size : space; - - ff->pktbuf_len = prev_len + copy; - *consumed = copy; - } + ff->pktbuf_len = pkt_len; + *consumed = copy; return 0; } if (lim < pkt_len) { - if (pkt_data == in && lim <= cap) { - memcpy_s(ff->pktbuf, cap, in, lim); - memset(ff->pktbuf + lim, 0, - AV_INPUT_BUFFER_PADDING_SIZE); - pkt_data = ff->pktbuf; - } else if (pkt_data == ff->pktbuf) { - memset(ff->pktbuf + lim, 0, - AV_INPUT_BUFFER_PADDING_SIZE); - } + memset(ff->pktbuf + lim, 0, AV_INPUT_BUFFER_PADDING_SIZE); pkt_len = lim; } } - ff->pkt->data = (uint8_t *)pkt_data; + ff->pkt->data = ff->pktbuf; ff->pkt->size = (int)pkt_len; + prev_log_level = av_log_get_level(); av_log_set_level(AV_LOG_FATAL); ret = avcodec_send_packet(ff->avctx, ff->pkt); if (ret == 0) ret = avcodec_receive_frame(ff->avctx, ff->frame); - av_log_set_level(AV_LOG_ERROR); + av_log_set_level(prev_log_level); if (ret < 0) { avcodec_flush_buffers(ff->avctx); if (!in || !in_size) { @@ -444,21 +376,8 @@ static int ffmpeg_dec_ff_decode(struct processing_module *mod, ff->pktbuf_len = 0; return -EIO; } - if (prev_len == 0) { - size_t copy = in_size < cap ? in_size : cap; - - memcpy_s(ff->pktbuf, cap, in, copy); - memset(ff->pktbuf + copy, 0, - AV_INPUT_BUFFER_PADDING_SIZE); - ff->pktbuf_len = copy; - *consumed = copy; - } else { - size_t space = prev_len < cap ? cap - prev_len : 0; - size_t copy = in_size < space ? in_size : space; - - ff->pktbuf_len = prev_len + copy; - *consumed = copy; - } + ff->pktbuf_len = pkt_len; + *consumed = copy; return 0; } diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-g722.c b/src/audio/ffmpeg_dec/ffmpeg_dec-g722.c index c51b30e8da1e..db236d205db0 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec-g722.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-g722.c @@ -6,10 +6,21 @@ #include #include +#include +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +#include +#include +#endif #include "ffmpeg_dec.h" +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + #define G722_DEC_MAX_SAMPLES 1024 #define G722_DEC_MAX_PKT_BYTES (G722_DEC_MAX_SAMPLES / 2) +#define G722_SAMPLE_RATE 16000 +#define G722_BPC_48K 6 +#define G722_BPC_56K 7 +#define G722_BPC_64K 8 static uint32_t ffmpeg_dec_g722_max_frame_samples(const struct ffmpeg_dec_comp_data *cd) { @@ -29,11 +40,36 @@ static size_t ffmpeg_dec_g722_limit_packet_size(const struct ffmpeg_dec_comp_dat return pkt_len > G722_DEC_MAX_PKT_BYTES ? G722_DEC_MAX_PKT_BYTES : pkt_len; } +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) +static int ffmpeg_dec_g722_configure_avctx(struct processing_module *mod, void *ctx) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + AVCodecContext *avctx = ctx; + int bpc = G722_BPC_64K; + + if (cd->req_bit_rate == 48000) { + bpc = G722_BPC_48K; + } else if (cd->req_bit_rate == 56000) { + bpc = G722_BPC_56K; + } else if (cd->req_bit_rate != 0 && cd->req_bit_rate != 64000) { + comp_err(mod->dev, "unsupported G.722 bitrate %u", cd->req_bit_rate); + return -EINVAL; + } + + avctx->sample_rate = G722_SAMPLE_RATE; + av_opt_set_int(avctx->priv_data, "bits_per_codeword", bpc, 0); + return 0; +} +#endif + const struct ffmpeg_dec_codec_ops ffmpeg_dec_g722_ops = { .id = FFMPEG_DEC_CODEC_G722, .snd_codec_id = SND_AUDIOCODEC_G722, .name = "g722", .decoder_name = "g722", .max_frame_samples = ffmpeg_dec_g722_max_frame_samples, +#if !defined(CONFIG_COMP_FFMPEG_DEC_STUB) + .configure_avctx = ffmpeg_dec_g722_configure_avctx, +#endif .limit_packet_size = ffmpeg_dec_g722_limit_packet_size, }; diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.c b/src/audio/ffmpeg_dec/ffmpeg_dec.c index 8724a5e6cb56..81eaab628441 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.c @@ -533,7 +533,7 @@ static int ffmpeg_dec_prepare(struct processing_module *mod, cd->flac_parsing_meta = false; cd->flac_last_block = false; cd->alac_parsing_caf = false; - cd->alac_skip_to_eof = false; + cd->unparsed_skip_to_eof = false; cd->draining = false; cd->drained = false; @@ -904,7 +904,7 @@ static int ffmpeg_dec_reset(struct processing_module *mod) cd->flac_parsing_meta = false; cd->flac_last_block = false; cd->alac_parsing_caf = false; - cd->alac_skip_to_eof = false; + cd->unparsed_skip_to_eof = false; cd->draining = false; cd->drained = false; #if CONFIG_IPC_MAJOR_4 diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.h b/src/audio/ffmpeg_dec/ffmpeg_dec.h index d7a0a94dba4d..e7b3a022bb65 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec.h +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.h @@ -213,6 +213,10 @@ void ffmpeg_enc_stage_packet(struct processing_module *mod, struct ffmpeg_enc_da * decoder open; returns 0 when ready to decode frames, -ENODATA * when more input bytes are needed, or negative errno on error. * @prepare_extradata: Optional hook to validate/filter cd->extradata before avcodec_open2(). + * @configure_avctx: Optional hook to validate extradata / apply codec-specific settings on + * AVCodecContext right before avcodec_open2(); returns 0 or -EINVAL/-errno. + * @is_trailing_eof: Optional hook for unparsed streams returning true when a trailing + * non-audio container chunk is reached so remaining input is skipped to EOF. * @frame_bytes_consumed: Optional hook for unparsed self-delimiting codecs (e.g. ALAC) * returning the exact byte count consumed by the last decoded frame. * @limit_packet_size: Optional hook for unparsed block/stream codecs (e.g. G.722, aptX) @@ -226,6 +230,8 @@ struct ffmpeg_dec_codec_ops { uint32_t (*max_frame_samples)(const struct ffmpeg_dec_comp_data *cd); int (*skip_container)(struct processing_module *mod, struct sof_source *src); void (*prepare_extradata)(struct processing_module *mod); + int (*configure_avctx)(struct processing_module *mod, void *avctx); + bool (*is_trailing_eof)(const uint8_t *pkt_data, size_t pkt_len); size_t (*frame_bytes_consumed)(const void *priv_data); size_t (*limit_packet_size)(const struct ffmpeg_dec_comp_data *cd, size_t pkt_len); }; @@ -319,7 +325,7 @@ struct ffmpeg_dec_comp_data { bool flac_parsing_meta; bool flac_last_block; bool alac_parsing_caf; - bool alac_skip_to_eof; + bool unparsed_skip_to_eof; bool draining; bool drained; #if CONFIG_IPC_MAJOR_4 diff --git a/src/kernel/audio/base_fw.c b/src/kernel/audio/base_fw.c index a233acfcb64e..880306a4a03b 100644 --- a/src/kernel/audio/base_fw.c +++ b/src/kernel/audio/base_fw.c @@ -151,12 +151,12 @@ static void get_codec_info(struct sof_tlv **tuple) SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_CAPTURE); #endif #if defined(CONFIG_FFMPEG_ENC_FLAC) - /* FLAC encode (compress capture) via ffmpeg_dec (native libavcodec). */ + /* FLAC encode (compress capture) via ffmpeg_enc (native libavcodec). */ codec_info.items[codec_info.count++] = SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_FLAC, SOF_IPC_STREAM_CAPTURE); #endif #if defined(CONFIG_FFMPEG_ENC_ALAC) - /* ALAC encode (compress capture) via ffmpeg_dec (native libavcodec). */ + /* ALAC encode (compress capture) via ffmpeg_enc (native libavcodec). */ codec_info.items[codec_info.count++] = SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_ALAC, SOF_IPC_STREAM_CAPTURE); #endif @@ -166,7 +166,7 @@ static void get_codec_info(struct sof_tlv **tuple) SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_SBC, SOF_IPC_STREAM_PLAYBACK); #endif #if defined(CONFIG_FFMPEG_ENC_SBC) - /* Bluetooth SBC / mSBC encode via ffmpeg_dec (native libavcodec). */ + /* Bluetooth SBC / mSBC encode via ffmpeg_enc (native libavcodec). */ codec_info.items[codec_info.count++] = SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_SBC, SOF_IPC_STREAM_CAPTURE); #endif @@ -176,7 +176,7 @@ static void get_codec_info(struct sof_tlv **tuple) SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_G722, SOF_IPC_STREAM_PLAYBACK); #endif #if defined(CONFIG_FFMPEG_ENC_G722) - /* ITU-T G.722 wideband speech encode via ffmpeg_dec (native libavcodec). */ + /* ITU-T G.722 wideband speech encode via ffmpeg_enc (native libavcodec). */ codec_info.items[codec_info.count++] = SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_G722, SOF_IPC_STREAM_CAPTURE); #endif @@ -186,7 +186,7 @@ static void get_codec_info(struct sof_tlv **tuple) SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX, SOF_IPC_STREAM_PLAYBACK); #endif #if defined(CONFIG_FFMPEG_ENC_APTX) - /* Bluetooth aptX encode via ffmpeg_dec (native libavcodec). */ + /* Bluetooth aptX encode via ffmpeg_enc (native libavcodec). */ codec_info.items[codec_info.count++] = SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX, SOF_IPC_STREAM_CAPTURE); #endif