|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2026 Intel Corporation. |
| 4 | +// |
| 5 | +// libavcodec encode backend for the ffmpeg_dec module (encode mode): PCM in, |
| 6 | +// compressed elementary stream out (MP3 via libshine). It is the mirror of the |
| 7 | +// decoder path - avcodec_send_frame / avcodec_receive_packet instead of |
| 8 | +// send_packet / receive_frame. |
| 9 | +// |
| 10 | +// SOF PCM is interleaved S32; the encoder is opened in whatever sample format it |
| 11 | +// supports (libshine: S16), so S32 is converted to the encoder's format per |
| 12 | +// frame. MP3 uses fixed 1152-sample frames; this feeds one encoder frame worth |
| 13 | +// of input per process call when enough is available. |
| 14 | + |
| 15 | +#include <sof/audio/module_adapter/module/generic.h> |
| 16 | +#include <sof/audio/source_api.h> |
| 17 | +#include <rtos/string.h> |
| 18 | +#include <errno.h> |
| 19 | +#include <stdio.h> |
| 20 | +#include "ffmpeg_dec.h" |
| 21 | + |
| 22 | +#include <libavcodec/avcodec.h> |
| 23 | +#include <libavutil/channel_layout.h> |
| 24 | +#include <libavutil/samplefmt.h> |
| 25 | + |
| 26 | +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); |
| 27 | + |
| 28 | +#define FFMPEG_ENC_DEFAULT_BITRATE 128000 |
| 29 | + |
| 30 | +struct ffmpeg_enc_data { |
| 31 | + const AVCodec *codec; |
| 32 | + AVCodecContext *avctx; |
| 33 | + AVPacket *pkt; |
| 34 | + AVFrame *frame; |
| 35 | + int frame_bytes; /* SOF S32 input frame size (all channels) */ |
| 36 | +}; |
| 37 | + |
| 38 | +int ffmpeg_enc_mod_init(struct processing_module *mod) |
| 39 | +{ |
| 40 | + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); |
| 41 | + struct comp_dev *dev = mod->dev; |
| 42 | + struct ffmpeg_enc_data *e; |
| 43 | + |
| 44 | + ffmpeg_dec_libc_bind(mod); |
| 45 | + |
| 46 | + e = mod_zalloc(mod, sizeof(*e)); |
| 47 | + if (!e) |
| 48 | + return -ENOMEM; |
| 49 | + |
| 50 | + /* libshine is the fixed-point MP3 encoder built into the archive. */ |
| 51 | + e->codec = avcodec_find_encoder_by_name("libshine"); |
| 52 | + if (!e->codec) { |
| 53 | + comp_err(dev, "libshine MP3 encoder not found"); |
| 54 | + mod_free(mod, e); |
| 55 | + return -ENODEV; |
| 56 | + } |
| 57 | + |
| 58 | + e->avctx = avcodec_alloc_context3(e->codec); |
| 59 | + e->pkt = av_packet_alloc(); |
| 60 | + e->frame = av_frame_alloc(); |
| 61 | + if (!e->avctx || !e->pkt || !e->frame) { |
| 62 | + if (e->frame) |
| 63 | + av_frame_free(&e->frame); |
| 64 | + if (e->pkt) |
| 65 | + av_packet_free(&e->pkt); |
| 66 | + if (e->avctx) |
| 67 | + avcodec_free_context(&e->avctx); |
| 68 | + mod_free(mod, e); |
| 69 | + return -ENOMEM; |
| 70 | + } |
| 71 | + |
| 72 | + cd->backend_data = e; |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +int ffmpeg_enc_mod_prepare(struct processing_module *mod, |
| 77 | + struct sof_source **sources, int num_of_sources, |
| 78 | + struct sof_sink **sinks, int num_of_sinks) |
| 79 | +{ |
| 80 | + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); |
| 81 | + struct ffmpeg_enc_data *e = cd->backend_data; |
| 82 | + struct comp_dev *dev = mod->dev; |
| 83 | + int ret; |
| 84 | + |
| 85 | + if (num_of_sources != 1) |
| 86 | + return -EINVAL; |
| 87 | + |
| 88 | + ffmpeg_dec_libc_bind(mod); |
| 89 | + |
| 90 | + cd->out_channels = source_get_channels(sources[0]); |
| 91 | + cd->out_rate = source_get_rate(sources[0]); |
| 92 | + e->frame_bytes = source_get_frame_bytes(sources[0]); |
| 93 | + |
| 94 | + e->avctx->sample_rate = cd->out_rate; |
| 95 | + av_channel_layout_default(&e->avctx->ch_layout, cd->out_channels); |
| 96 | + /* Use the encoder's native sample format (libshine: S16). */ |
| 97 | + e->avctx->sample_fmt = e->codec->sample_fmts ? |
| 98 | + e->codec->sample_fmts[0] : AV_SAMPLE_FMT_S16; |
| 99 | + e->avctx->bit_rate = FFMPEG_ENC_DEFAULT_BITRATE; |
| 100 | + e->avctx->thread_count = 1; |
| 101 | + |
| 102 | + ret = avcodec_open2(e->avctx, e->codec, NULL); |
| 103 | + if (ret < 0) { |
| 104 | + comp_err(dev, "avcodec_open2 (encoder) failed %d", ret); |
| 105 | + return -EIO; |
| 106 | + } |
| 107 | + |
| 108 | + comp_info(dev, "ffmpeg_enc: libshine MP3, rate %u ch %u frame_size %d", |
| 109 | + cd->out_rate, cd->out_channels, e->avctx->frame_size); |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +/* Convert one interleaved S32 frame block to the encoder AVFrame, then encode. */ |
| 114 | +int ffmpeg_enc_mod_process(struct processing_module *mod, |
| 115 | + struct input_stream_buffer *input_buffers, int num_input_buffers, |
| 116 | + struct output_stream_buffer *output_buffers, int num_output_buffers) |
| 117 | +{ |
| 118 | + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); |
| 119 | + struct ffmpeg_enc_data *e = cd->backend_data; |
| 120 | + struct comp_dev *dev = mod->dev; |
| 121 | + int ch = cd->out_channels; |
| 122 | + int nb = e->avctx->frame_size ? e->avctx->frame_size : 1152; |
| 123 | + const int32_t *in; |
| 124 | + uint8_t *out = output_buffers[0].data; |
| 125 | + size_t out_off = 0; |
| 126 | + int planar, i, c, ret; |
| 127 | + |
| 128 | + if (num_input_buffers != 1 || num_output_buffers != 1) |
| 129 | + return -EINVAL; |
| 130 | + |
| 131 | + ffmpeg_dec_libc_bind(mod); |
| 132 | + |
| 133 | + /* Need a full encoder frame of input. */ |
| 134 | + if (input_buffers[0].size < (uint32_t)(nb * e->frame_bytes)) |
| 135 | + return -ENODATA; |
| 136 | + |
| 137 | + e->frame->nb_samples = nb; |
| 138 | + e->frame->format = e->avctx->sample_fmt; |
| 139 | + e->frame->sample_rate = cd->out_rate; |
| 140 | + av_channel_layout_copy(&e->frame->ch_layout, &e->avctx->ch_layout); |
| 141 | + if (av_frame_get_buffer(e->frame, 0) < 0) |
| 142 | + return -ENOMEM; |
| 143 | + |
| 144 | + /* S32 interleaved -> S16 (planar or packed per the encoder format). */ |
| 145 | + in = input_buffers[0].data; |
| 146 | + planar = av_sample_fmt_is_planar(e->frame->format); |
| 147 | + for (i = 0; i < nb; i++) |
| 148 | + for (c = 0; c < ch; c++) { |
| 149 | + int16_t s = (int16_t)(in[i * ch + c] >> 16); |
| 150 | + |
| 151 | + if (planar) |
| 152 | + ((int16_t *)e->frame->data[c])[i] = s; |
| 153 | + else |
| 154 | + ((int16_t *)e->frame->data[0])[i * ch + c] = s; |
| 155 | + } |
| 156 | + input_buffers[0].consumed = nb * e->frame_bytes; |
| 157 | + |
| 158 | + ret = avcodec_send_frame(e->avctx, e->frame); |
| 159 | + av_frame_unref(e->frame); |
| 160 | + if (ret < 0) { |
| 161 | + comp_err(dev, "avcodec_send_frame failed %d", ret); |
| 162 | + return -EIO; |
| 163 | + } |
| 164 | + |
| 165 | + /* Drain compressed packets into the output buffer. */ |
| 166 | + while (ret >= 0) { |
| 167 | + ret = avcodec_receive_packet(e->avctx, e->pkt); |
| 168 | + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) |
| 169 | + break; |
| 170 | + if (ret < 0) { |
| 171 | + comp_err(dev, "avcodec_receive_packet failed %d", ret); |
| 172 | + return -EIO; |
| 173 | + } |
| 174 | + if (out_off + e->pkt->size <= output_buffers[0].size) { |
| 175 | + memcpy_s(out + out_off, output_buffers[0].size - out_off, |
| 176 | + e->pkt->data, e->pkt->size); |
| 177 | + out_off += e->pkt->size; |
| 178 | + } else { |
| 179 | + comp_warn(dev, "output full, MP3 packet dropped"); |
| 180 | + } |
| 181 | + av_packet_unref(e->pkt); |
| 182 | + ret = 0; |
| 183 | + } |
| 184 | + |
| 185 | + output_buffers[0].size = out_off; |
| 186 | + return 0; |
| 187 | +} |
| 188 | + |
| 189 | +int ffmpeg_enc_mod_free(struct processing_module *mod) |
| 190 | +{ |
| 191 | + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); |
| 192 | + struct ffmpeg_enc_data *e = cd->backend_data; |
| 193 | + |
| 194 | + ffmpeg_dec_libc_bind(mod); |
| 195 | + if (!e) |
| 196 | + return 0; |
| 197 | + |
| 198 | + if (e->frame) |
| 199 | + av_frame_free(&e->frame); |
| 200 | + if (e->pkt) |
| 201 | + av_packet_free(&e->pkt); |
| 202 | + if (e->avctx) |
| 203 | + avcodec_free_context(&e->avctx); |
| 204 | + mod_free(mod, e); |
| 205 | + cd->backend_data = NULL; |
| 206 | + return 0; |
| 207 | +} |
0 commit comments