2 * codec2 encoder/decoder using libcodec2
3 * Copyright (c) 2017 Tomas Härdin
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <codec2/codec2.h>
24 #include "libavutil/opt.h"
26 #include "codec2utils.h"
34 static const AVOption options[] = {
35 //not AV_OPT_FLAG_DECODING_PARAM since mode should come from the demuxer
36 //1300 (aka FreeDV 1600) is the most common mode on-the-air, default to it here as well
37 CODEC2_AVOPTIONS("codec2 mode", LibCodec2Context, 0, 4 /*CODEC2_MODE_1300*/, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM),
41 static const AVClass libcodec2_enc_class = {
42 .class_name = "libcodec2 encoder",
43 .item_name = av_default_item_name,
45 .version = LIBAVUTIL_VERSION_INT,
48 static const AVClass libcodec2_dec_class = {
49 .class_name = "libcodec2 decoder",
50 .item_name = av_default_item_name,
51 .version = LIBAVUTIL_VERSION_INT,
54 static av_cold int libcodec2_init_common(AVCodecContext *avctx, int mode)
56 LibCodec2Context *c2 = avctx->priv_data;
57 //Grab mode name from options, unless it's some weird number.
58 const char *modename = mode >= 0 && mode <= CODEC2_MODE_MAX ? options[mode+1].name : "?";
60 c2->codec = codec2_create(mode);
62 //Out of memory or unsupported mode. The latter seems most likely,
63 //but we can't tell for sure with the current API.
64 goto libcodec2_init_common_error;
67 avctx->frame_size = codec2_samples_per_frame(c2->codec);
68 avctx->block_align = (codec2_bits_per_frame(c2->codec) + 7) / 8;
70 if (avctx->frame_size <= 0 || avctx->block_align <= 0) {
71 //codec2_create() may succeed for some modes but still fail at codec2_samples_per_frame()
72 //example is -mode 700C on libcodec2 0.4
73 codec2_destroy(c2->codec);
75 goto libcodec2_init_common_error;
78 codec2_set_natural_or_gray(c2->codec, 1);
82 libcodec2_init_common_error:
83 av_log(avctx, AV_LOG_ERROR,
84 "Mode %i (%s) not supported with the linked version of libcodec2\n",
86 return AVERROR(EINVAL);
89 static av_cold int libcodec2_init_decoder(AVCodecContext *avctx)
91 avctx->sample_rate = 8000;
93 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
94 avctx->channel_layout = AV_CH_LAYOUT_MONO;
96 if (avctx->extradata_size != CODEC2_EXTRADATA_SIZE) {
97 av_log(avctx, AV_LOG_ERROR, "must have exactly %i bytes of extradata (got %i)\n",
98 CODEC2_EXTRADATA_SIZE, avctx->extradata_size);
99 return AVERROR_INVALIDDATA;
102 return libcodec2_init_common(avctx, codec2_mode_from_extradata(avctx->extradata));
105 static av_cold int libcodec2_init_encoder(AVCodecContext *avctx)
107 LibCodec2Context *c2 = avctx->priv_data;
109 //will need to be smarter once we get wideband support
110 if (avctx->sample_rate != 8000 ||
111 avctx->channels != 1 ||
112 avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
113 av_log(avctx, AV_LOG_ERROR, "only 8 kHz 16-bit mono allowed\n");
114 return AVERROR(EINVAL);
117 avctx->extradata = av_mallocz(CODEC2_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
118 if (!avctx->extradata) {
119 return AVERROR(ENOMEM);
122 avctx->extradata_size = CODEC2_EXTRADATA_SIZE;
123 codec2_make_extradata(avctx->extradata, c2->mode);
125 return libcodec2_init_common(avctx, c2->mode);
128 static av_cold int libcodec2_close(AVCodecContext *avctx)
130 LibCodec2Context *c2 = avctx->priv_data;
132 codec2_destroy(c2->codec);
136 static int libcodec2_decode(AVCodecContext *avctx, void *data,
137 int *got_frame_ptr, AVPacket *pkt)
139 LibCodec2Context *c2 = avctx->priv_data;
140 AVFrame *frame = data;
145 nframes = pkt->size / avctx->block_align;
146 frame->nb_samples = avctx->frame_size * nframes;
148 ret = ff_get_buffer(avctx, frame, 0);
154 output = (int16_t *)frame->data[0];
156 for (i = 0; i < nframes; i++) {
157 codec2_decode(c2->codec, output, input);
158 input += avctx->block_align;
159 output += avctx->frame_size;
162 *got_frame_ptr = nframes > 0;
163 return nframes * avctx->block_align;
166 static int libcodec2_encode(AVCodecContext *avctx, AVPacket *avpkt,
167 const AVFrame *frame, int *got_packet_ptr)
169 LibCodec2Context *c2 = avctx->priv_data;
170 int16_t *samples = (int16_t *)frame->data[0];
172 int ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align, 0);
177 codec2_encode(c2->codec, avpkt->data, samples);
183 const AVCodec ff_libcodec2_decoder = {
185 .long_name = NULL_IF_CONFIG_SMALL("codec2 decoder using libcodec2"),
186 .type = AVMEDIA_TYPE_AUDIO,
187 .id = AV_CODEC_ID_CODEC2,
188 .priv_data_size = sizeof(LibCodec2Context),
189 .init = libcodec2_init_decoder,
190 .close = libcodec2_close,
191 .decode = libcodec2_decode,
192 .capabilities = AV_CODEC_CAP_CHANNEL_CONF,
193 .supported_samplerates = (const int[]){ 8000, 0 },
194 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
195 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 },
196 .priv_class = &libcodec2_dec_class,
199 const AVCodec ff_libcodec2_encoder = {
201 .long_name = NULL_IF_CONFIG_SMALL("codec2 encoder using libcodec2"),
202 .type = AVMEDIA_TYPE_AUDIO,
203 .id = AV_CODEC_ID_CODEC2,
204 .priv_data_size = sizeof(LibCodec2Context),
205 .init = libcodec2_init_encoder,
206 .close = libcodec2_close,
207 .encode2 = libcodec2_encode,
209 .supported_samplerates = (const int[]){ 8000, 0 },
210 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
211 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 },
212 .priv_class = &libcodec2_enc_class,