FFmpeg
libcodec2.c
Go to the documentation of this file.
1 /*
2  * codec2 encoder/decoder using libcodec2
3  * Copyright (c) 2017 Tomas Härdin
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include <codec2/codec2.h>
24 #include "libavutil/mem.h"
25 #include "avcodec.h"
26 #include "libavutil/opt.h"
27 #include "codec_internal.h"
28 #include "decode.h"
29 #include "encode.h"
30 #include "codec2utils.h"
31 
32 typedef struct {
33  const AVClass *class;
34  struct CODEC2 *codec;
35  int mode;
37 
38 static const AVOption options[] = {
39  //not AV_OPT_FLAG_DECODING_PARAM since mode should come from the demuxer
40  //1300 (aka FreeDV 1600) is the most common mode on-the-air, default to it here as well
41  CODEC2_AVOPTIONS("codec2 mode", LibCodec2Context, 0, 4 /*CODEC2_MODE_1300*/, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM),
42  { NULL },
43 };
44 
45 static const AVClass libcodec2_enc_class = {
46  .class_name = "libcodec2 encoder",
47  .item_name = av_default_item_name,
48  .option = options,
49  .version = LIBAVUTIL_VERSION_INT,
50 };
51 
53 {
54  LibCodec2Context *c2 = avctx->priv_data;
55  //Grab mode name from options, unless it's some weird number.
56  const char *modename = mode >= 0 && mode <= CODEC2_MODE_MAX ? options[mode+1].name : "?";
57 
58  c2->codec = codec2_create(mode);
59  if (!c2->codec) {
60  //Out of memory or unsupported mode. The latter seems most likely,
61  //but we can't tell for sure with the current API.
62  goto libcodec2_init_common_error;
63  }
64 
65  avctx->frame_size = codec2_samples_per_frame(c2->codec);
66  avctx->block_align = (codec2_bits_per_frame(c2->codec) + 7) / 8;
67 
68  if (avctx->frame_size <= 0 || avctx->block_align <= 0) {
69  //codec2_create() may succeed for some modes but still fail at codec2_samples_per_frame()
70  //example is -mode 700C on libcodec2 0.4
71  codec2_destroy(c2->codec);
72  c2->codec = NULL;
73  goto libcodec2_init_common_error;
74  }
75 
76  codec2_set_natural_or_gray(c2->codec, 1);
77 
78  return 0;
79 
80 libcodec2_init_common_error:
81  av_log(avctx, AV_LOG_ERROR,
82  "Mode %i (%s) not supported with the linked version of libcodec2\n",
83  mode, modename);
84  return AVERROR(EINVAL);
85 }
86 
88 {
89  avctx->sample_rate = 8000;
93 
94  if (avctx->extradata_size != CODEC2_EXTRADATA_SIZE) {
95  av_log(avctx, AV_LOG_ERROR, "must have exactly %i bytes of extradata (got %i)\n",
97  return AVERROR_INVALIDDATA;
98  }
99 
101 }
102 
104 {
105  LibCodec2Context *c2 = avctx->priv_data;
106 
107  //will need to be smarter once we get wideband support
108  if (avctx->sample_rate != 8000 ||
109  avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
110  av_log(avctx, AV_LOG_ERROR, "only 8 kHz 16-bit mono allowed\n");
111  return AVERROR(EINVAL);
112  }
113 
115  if (!avctx->extradata) {
116  return AVERROR(ENOMEM);
117  }
118 
120  codec2_make_extradata(avctx->extradata, c2->mode);
121 
122  return libcodec2_init_common(avctx, c2->mode);
123 }
124 
126 {
127  LibCodec2Context *c2 = avctx->priv_data;
128 
129  codec2_destroy(c2->codec);
130  return 0;
131 }
132 
134  int *got_frame_ptr, AVPacket *pkt)
135 {
136  LibCodec2Context *c2 = avctx->priv_data;
137  int ret, nframes, i;
138  const uint8_t *input;
139  int16_t *output;
140 
141  nframes = pkt->size / avctx->block_align;
142  frame->nb_samples = avctx->frame_size * nframes;
143 
144  ret = ff_get_buffer(avctx, frame, 0);
145  if (ret < 0) {
146  return ret;
147  }
148 
149  input = pkt->data;
150  output = (int16_t *)frame->data[0];
151 
152  for (i = 0; i < nframes; i++) {
153  codec2_decode(c2->codec, output, input);
154  input += avctx->block_align;
155  output += avctx->frame_size;
156  }
157 
158  *got_frame_ptr = nframes > 0;
159  return nframes * avctx->block_align;
160 }
161 
162 static int libcodec2_encode(AVCodecContext *avctx, AVPacket *avpkt,
163  const AVFrame *frame, int *got_packet_ptr)
164 {
165  LibCodec2Context *c2 = avctx->priv_data;
166  int16_t *samples = (int16_t *)frame->data[0];
167 
168  int ret = ff_get_encode_buffer(avctx, avpkt, avctx->block_align, 0);
169  if (ret < 0) {
170  return ret;
171  }
172 
173  codec2_encode(c2->codec, avpkt->data, samples);
174  *got_packet_ptr = 1;
175 
176  return 0;
177 }
178 
180  .p.name = "libcodec2",
181  CODEC_LONG_NAME("codec2 decoder using libcodec2"),
182  .p.type = AVMEDIA_TYPE_AUDIO,
183  .p.id = AV_CODEC_ID_CODEC2,
184  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF,
185  .p.supported_samplerates = (const int[]){ 8000, 0 },
186  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
187  .p.ch_layouts = (const AVChannelLayout[]) { AV_CHANNEL_LAYOUT_MONO, { 0 } },
188  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
189  .priv_data_size = sizeof(LibCodec2Context),
191  .close = libcodec2_close,
193 };
194 
196  .p.name = "libcodec2",
197  CODEC_LONG_NAME("codec2 encoder using libcodec2"),
198  .p.type = AVMEDIA_TYPE_AUDIO,
199  .p.id = AV_CODEC_ID_CODEC2,
200  .p.capabilities = AV_CODEC_CAP_DR1 |
202  .p.supported_samplerates = (const int[]){ 8000, 0 },
203  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
204  .p.ch_layouts = (const AVChannelLayout[]) { AV_CHANNEL_LAYOUT_MONO, { 0 } },
205  .p.priv_class = &libcodec2_enc_class,
206  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE,
207  .priv_data_size = sizeof(LibCodec2Context),
209  .close = libcodec2_close,
211 };
CODEC2_EXTRADATA_SIZE
#define CODEC2_EXTRADATA_SIZE
Definition: codec2utils.h:48
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1083
libcodec2_encode
static int libcodec2_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libcodec2.c:162
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVOption
AVOption.
Definition: opt.h:429
encode.h
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:35
FFCodec
Definition: codec_internal.h:127
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1071
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:357
libcodec2_init_decoder
static av_cold int libcodec2_init_decoder(AVCodecContext *avctx)
Definition: libcodec2.c:87
options
static const AVOption options[]
Definition: libcodec2.c:38
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
LibCodec2Context::mode
int mode
Definition: libcodec2.c:35
codec2_make_extradata
static void codec2_make_extradata(uint8_t *ptr, int mode)
Definition: codec2utils.h:51
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:530
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
CODEC2_MODE_MAX
#define CODEC2_MODE_MAX
Definition: codec2utils.h:30
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:159
codec2utils.h
decode.h
libcodec2_init_common
static av_cold int libcodec2_init_common(AVCodecContext *avctx, int mode)
Definition: libcodec2.c:52
AV_CODEC_ID_CODEC2
@ AV_CODEC_ID_CODEC2
Definition: codec_id.h:513
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
libcodec2_close
static av_cold int libcodec2_close(AVCodecContext *avctx)
Definition: libcodec2.c:125
libcodec2_enc_class
static const AVClass libcodec2_enc_class
Definition: libcodec2.c:45
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition: opt.h:352
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
ff_libcodec2_encoder
const FFCodec ff_libcodec2_encoder
Definition: libcodec2.c:195
AVPacket::size
int size
Definition: packet.h:540
LibCodec2Context
Definition: libcodec2.c:32
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:317
codec_internal.h
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1063
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
AVOption::name
const char * name
Definition: opt.h:430
ff_libcodec2_decoder
const FFCodec ff_libcodec2_decoder
Definition: libcodec2.c:179
codec2_mode_from_extradata
static uint8_t codec2_mode_from_extradata(uint8_t *ptr)
Definition: codec2utils.h:59
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
LibCodec2Context::codec
struct CODEC2 * codec
Definition: libcodec2.c:34
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:529
CODEC2_AVOPTIONS
#define CODEC2_AVOPTIONS(desc, classname, min_val, default_val, option_flags)
Definition: codec2utils.h:36
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodecContext::block_align
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition: avcodec.h:1089
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
libcodec2_init_encoder
static av_cold int libcodec2_init_encoder(AVCodecContext *avctx)
Definition: libcodec2.c:103
AVCodecContext
main external API structure.
Definition: avcodec.h:451
c2
static const uint64_t c2
Definition: murmur3.c:53
channel_layout.h
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
mode
mode
Definition: ebur128.h:83
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:440
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
mem.h
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:392
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
libcodec2_decode
static int libcodec2_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition: libcodec2.c:133
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61