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>
23 #include "avcodec.h"
24 #include "libavutil/opt.h"
25 #include "internal.h"
26 #include "codec2utils.h"
27 
28 typedef struct {
29  const AVClass *class;
30  struct CODEC2 *codec;
31  int mode;
33 
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
38  { NULL },
39 };
40 
41 static const AVClass libcodec2_enc_class = {
42  .class_name = "libcodec2 encoder",
43  .item_name = av_default_item_name,
44  .option = options,
45  .version = LIBAVUTIL_VERSION_INT,
46 };
47 
48 static const AVClass libcodec2_dec_class = {
49  .class_name = "libcodec2 decoder",
50  .item_name = av_default_item_name,
51  .version = LIBAVUTIL_VERSION_INT,
52 };
53 
55 {
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 <= AVPRIV_CODEC2_MODE_MAX ? options[mode+1].name : "?";
59 
60  c2->codec = codec2_create(mode);
61  if (!c2->codec) {
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;
65  }
66 
67  avctx->frame_size = codec2_samples_per_frame(c2->codec);
68  avctx->block_align = (codec2_bits_per_frame(c2->codec) + 7) / 8;
69 
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);
74  c2->codec = NULL;
75  goto libcodec2_init_common_error;
76  }
77 
78  codec2_set_natural_or_gray(c2->codec, 1);
79 
80  return 0;
81 
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",
85  mode, modename);
86  return AVERROR(EINVAL);
87 }
88 
90 {
91  avctx->sample_rate = 8000;
92  avctx->channels = 1;
95 
97  av_log(avctx, AV_LOG_ERROR, "must have exactly %i bytes of extradata (got %i)\n",
99  return AVERROR_INVALIDDATA;
100  }
101 
103 }
104 
106 {
107  LibCodec2Context *c2 = avctx->priv_data;
108 
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);
115  }
116 
118  if (!avctx->extradata) {
119  return AVERROR(ENOMEM);
120  }
121 
124 
125  return libcodec2_init_common(avctx, c2->mode);
126 }
127 
129 {
130  LibCodec2Context *c2 = avctx->priv_data;
131 
132  codec2_destroy(c2->codec);
133  return 0;
134 }
135 
136 static int libcodec2_decode(AVCodecContext *avctx, void *data,
137  int *got_frame_ptr, AVPacket *pkt)
138 {
139  LibCodec2Context *c2 = avctx->priv_data;
140  AVFrame *frame = data;
141  int ret, nframes, i;
142  uint8_t *input;
143  int16_t *output;
144 
145  nframes = pkt->size / avctx->block_align;
146  frame->nb_samples = avctx->frame_size * nframes;
147 
148  ret = ff_get_buffer(avctx, frame, 0);
149  if (ret < 0) {
150  return ret;
151  }
152 
153  input = pkt->data;
154  output = (int16_t *)frame->data[0];
155 
156  for (i = 0; i < nframes; i++) {
157  codec2_decode(c2->codec, output, input);
158  input += avctx->block_align;
159  output += avctx->frame_size;
160  }
161 
162  *got_frame_ptr = nframes > 0;
163  return nframes * avctx->block_align;
164 }
165 
166 static int libcodec2_encode(AVCodecContext *avctx, AVPacket *avpkt,
167  const AVFrame *frame, int *got_packet_ptr)
168 {
169  LibCodec2Context *c2 = avctx->priv_data;
170  int16_t *samples = (int16_t *)frame->data[0];
171 
172  int ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align, 0);
173  if (ret < 0) {
174  return ret;
175  }
176 
177  codec2_encode(c2->codec, avpkt->data, samples);
178  *got_packet_ptr = 1;
179 
180  return 0;
181 }
182 
184  .name = "libcodec2",
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),
190  .close = libcodec2_close,
192  .capabilities = 0,
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,
197 };
198 
200  .name = "libcodec2",
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),
206  .close = libcodec2_close,
207  .encode2 = libcodec2_encode,
208  .capabilities = 0,
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,
213 };
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static av_cold int libcodec2_init_common(AVCodecContext *avctx, int mode)
Definition: libcodec2.c:54
static int libcodec2_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libcodec2.c:166
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:278
int size
Definition: avcodec.h:1483
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static uint8_t avpriv_codec2_mode_from_extradata(uint8_t *ptr)
Definition: codec2utils.h:78
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
static const AVClass libcodec2_dec_class
Definition: libcodec2.c:48
static int libcodec2_decode(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *pkt)
Definition: libcodec2.c:136
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3494
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:2267
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AVCodec ff_libcodec2_decoder
Definition: libcodec2.c:183
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:72
const char * name
Definition: opt.h:247
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2238
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
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
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1671
uint8_t * data
Definition: avcodec.h:1482
static av_cold int libcodec2_init_decoder(AVCodecContext *avctx)
Definition: libcodec2.c:89
#define av_log(a,...)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:276
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVCodec ff_libcodec2_encoder
Definition: libcodec2.c:199
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * name
Name of the codec implementation.
Definition: avcodec.h:3501
#define AVPRIV_CODEC2_AVOPTIONS(desc, classname, min_val, default_val, option_flags)
Definition: codec2utils.h:36
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2281
#define AVPRIV_CODEC2_EXTRADATA_SIZE
Definition: codec2utils.h:62
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
static const AVOption options[]
Definition: libcodec2.c:34
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2250
Libavcodec external API header.
struct CODEC2 * codec
Definition: libcodec2.c:30
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int sample_rate
samples per second
Definition: avcodec.h:2230
main external API structure.
Definition: avcodec.h:1570
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1968
int extradata_size
Definition: avcodec.h:1672
Describe the class of an AVClass context structure.
Definition: log.h:67
static av_cold int libcodec2_close(AVCodecContext *avctx)
Definition: libcodec2.c:128
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
static av_cold int libcodec2_init_encoder(AVCodecContext *avctx)
Definition: libcodec2.c:105
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
common internal api header.
signed 16 bits
Definition: samplefmt.h:61
static const uint64_t c2
Definition: murmur3.c:50
static const AVClass libcodec2_enc_class
Definition: libcodec2.c:41
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:795
void * priv_data
Definition: avcodec.h:1597
static void avpriv_codec2_make_extradata(uint8_t *ptr, int mode)
Definition: codec2utils.h:65
int channels
number of audio channels
Definition: avcodec.h:2231
#define AVPRIV_CODEC2_MODE_MAX
Definition: codec2utils.h:30
Filter the word “frame” indicates either a video frame or a group of audio samples
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
#define AV_CH_LAYOUT_MONO
This structure stores compressed data.
Definition: avcodec.h:1459
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:361
for(j=16;j >0;--j)