avformat/mpeg: demux ivtv captions
[ffmpeg.git] / libavcodec / liblc3enc.c
blob3c0bcc6840d36c19ac5eadd14d978b7e775f41ed
1 /*
2 * LC3 encoder wrapper
3 * Copyright (C) 2024 Antoine Soulier <asoulier@google.com>
5 * This file is part of FFmpeg.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <lc3.h>
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/mem.h"
26 #include "avcodec.h"
27 #include "codec.h"
28 #include "codec_internal.h"
29 #include "encode.h"
31 #define ENCODER_MAX_CHANNELS 2
33 typedef struct LibLC3EncOpts {
34 float frame_duration;
35 int hr_mode;
36 } LibLC3EncOpts;
38 typedef struct LibLC3EncContext {
39 const AVClass *av_class;
40 LibLC3EncOpts opts;
41 int block_bytes;
42 void *encoder_mem;
43 lc3_encoder_t encoder[ENCODER_MAX_CHANNELS];
44 int delay_samples;
45 int remaining_samples;
46 } LibLC3EncContext;
48 static av_cold int liblc3_encode_init(AVCodecContext *avctx)
50 LibLC3EncContext *liblc3 = avctx->priv_data;
51 bool hr_mode = liblc3->opts.hr_mode;
52 int frame_us = liblc3->opts.frame_duration * 1000;
53 int srate_hz = avctx->sample_rate;
54 int channels = avctx->ch_layout.nb_channels;
55 int effective_bit_rate;
56 unsigned encoder_size;
58 if (frame_us != 2500 && frame_us != 5000 &&
59 frame_us != 7500 && frame_us != 10000 ) {
60 av_log(avctx, AV_LOG_ERROR,
61 "Unsupported frame duration %.1f ms.\n", frame_us / 1000.f);
62 return AVERROR(EINVAL);
64 if (channels < 0 || channels > ENCODER_MAX_CHANNELS) {
65 av_log(avctx, AV_LOG_ERROR,
66 "Invalid number of channels %d. Max %d channels are accepted\n",
67 channels, ENCODER_MAX_CHANNELS);
68 return AVERROR(EINVAL);
71 hr_mode |= srate_hz > 48000;
72 hr_mode &= srate_hz >= 48000;
74 if (frame_us == 7500 && hr_mode) {
75 av_log(avctx, AV_LOG_ERROR,
76 "High-resolution mode is not supported with 7.5 ms frames.\n");
77 return AVERROR(EINVAL);
80 av_log(avctx, AV_LOG_INFO, "Encoding %.1f ms frames.\n", frame_us / 1000.f);
81 if (hr_mode)
82 av_log(avctx, AV_LOG_INFO, "High-resolution mode is enabled.\n");
84 liblc3->block_bytes = lc3_hr_frame_block_bytes(
85 hr_mode, frame_us, srate_hz, channels, avctx->bit_rate);
87 effective_bit_rate = lc3_hr_resolve_bitrate(
88 hr_mode, frame_us, srate_hz, liblc3->block_bytes);
90 if (avctx->bit_rate != effective_bit_rate)
91 av_log(avctx, AV_LOG_WARNING,
92 "Bitrate changed to %d bps.\n", effective_bit_rate);
93 avctx->bit_rate = effective_bit_rate;
95 encoder_size = lc3_hr_encoder_size(hr_mode, frame_us, srate_hz);
96 if (!encoder_size)
97 return AVERROR(EINVAL);
99 liblc3->encoder_mem = av_malloc_array(channels, encoder_size);
100 if (!liblc3->encoder_mem)
101 return AVERROR(ENOMEM);
103 for (int ch = 0; ch < channels; ch++) {
104 liblc3->encoder[ch] = lc3_hr_setup_encoder(
105 hr_mode, frame_us, srate_hz, 0,
106 (char *)liblc3->encoder_mem + ch * encoder_size);
109 avctx->extradata = av_mallocz(6 + AV_INPUT_BUFFER_PADDING_SIZE);
110 if (!avctx->extradata)
111 return AVERROR(ENOMEM);
113 AV_WL16(avctx->extradata + 0, frame_us / 10);
114 AV_WL16(avctx->extradata + 2, 0);
115 AV_WL16(avctx->extradata + 4, hr_mode);
116 avctx->extradata_size = 6;
118 avctx->frame_size = av_rescale(frame_us, srate_hz, 1000*1000);
119 liblc3->delay_samples = lc3_hr_delay_samples(hr_mode, frame_us, srate_hz);
120 liblc3->remaining_samples = 0;
122 return 0;
125 static av_cold int liblc3_encode_close(AVCodecContext *avctx)
127 LibLC3EncContext *liblc3 = avctx->priv_data;
129 av_freep(&liblc3->encoder_mem);
131 return 0;
134 static int liblc3_encode(AVCodecContext *avctx, AVPacket *pkt,
135 const AVFrame *frame, int *got_packet_ptr)
137 LibLC3EncContext *liblc3 = avctx->priv_data;
138 int block_bytes = liblc3->block_bytes;
139 int channels = avctx->ch_layout.nb_channels;
140 void *zero_frame = NULL;
141 uint8_t *data_ptr;
142 int ret;
144 if ((ret = ff_get_encode_buffer(avctx, pkt, block_bytes, 0)) < 0)
145 return ret;
147 if (frame) {
148 int padding = frame->nb_samples - frame->duration;
149 liblc3->remaining_samples = FFMAX(liblc3->delay_samples - padding, 0);
150 } else {
151 if (!liblc3->remaining_samples)
152 return 0;
154 liblc3->remaining_samples = 0;
155 zero_frame = av_mallocz(avctx->frame_size * sizeof(float));
156 if (!zero_frame)
157 return AVERROR(ENOMEM);
160 data_ptr = pkt->data;
161 for (int ch = 0; ch < channels; ch++) {
162 const float *pcm = zero_frame ? zero_frame : frame->data[ch];
163 int nbytes = block_bytes / channels + (ch < block_bytes % channels);
165 lc3_encode(liblc3->encoder[ch],
166 LC3_PCM_FORMAT_FLOAT, pcm, 1, nbytes, data_ptr);
168 data_ptr += nbytes;
171 if (zero_frame)
172 av_free(zero_frame);
174 *got_packet_ptr = 1;
176 return 0;
179 #define OFFSET(x) offsetof(LibLC3EncContext, opts.x)
180 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
181 static const AVOption options[] = {
182 { "frame_duration", "Duration of a frame in milliseconds",
183 OFFSET(frame_duration), AV_OPT_TYPE_FLOAT,
184 { .dbl = 10.0 }, 2.5, 10.0, FLAGS },
185 { "high_resolution", "Enable High-Resolution mode (48 KHz or 96 KHz)",
186 OFFSET(hr_mode), AV_OPT_TYPE_BOOL,
187 { .i64 = 0 }, 0, 1, FLAGS },
188 { NULL }
191 static const AVClass class = {
192 .class_name = "liblc3 encoder",
193 .item_name = av_default_item_name,
194 .option = options,
195 .version = LIBAVUTIL_VERSION_INT,
198 const FFCodec ff_liblc3_encoder = {
199 .p.name = "liblc3",
200 CODEC_LONG_NAME("LC3 (Low Complexity Communication Codec)"),
201 .p.type = AVMEDIA_TYPE_AUDIO,
202 .p.id = AV_CODEC_ID_LC3,
203 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
204 .p.supported_samplerates = (const int [])
205 { 96000, 48000, 32000, 24000, 16000, 8000, 0 },
206 .p.sample_fmts = (const enum AVSampleFormat[])
207 { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
208 .p.priv_class = &class,
209 .p.wrapper_name = "liblc3",
210 .priv_data_size = sizeof(LibLC3EncContext),
211 .init = liblc3_encode_init,
212 .close = liblc3_encode_close,
213 FF_CODEC_ENCODE_CB(liblc3_encode),