[FFmpeg-devel,WIP] libcodec2 wrapper + de/muxer in FFmpeg

Submitted by Tomas Härdin on Aug. 8, 2017, 9:49 p.m.

Details

Message ID 1502228985.20758.5.camel@acc.umu.se
State New
Headers show

Commit Message

Tomas Härdin Aug. 8, 2017, 9:49 p.m.
On Fri, 2017-08-04 at 19:20 +0200, Tomas Härdin wrote:
> TODO:
> 
> * have -mode be an integer and use the CONST system Nicolas mentioned
> * option for demuxing multiple frames at a time
> * sort the extradata aliasing thing
> * address API issues in libcodec2, possibly modify the format (there
> should still be time)

Had some time today and went over and addressed all TODOs but the API
issue in libcodec2. For that I need to figure out why reCAPTCHA is
broken on sourceforge.net, to be able to fix my ML registration >:|

I split the patchset into one for lavc and a second for lavf, plus a
third for the small ffmpeg CLI fix. Should make review simpler.

I also removed the 0xC0DEC2 magic from extradata, mostly as part of
reworking codec2utils

Finally I made codec2_probe only award AVPROBE_SCORE_MAX if the file
extension is .c2

Feel free to comment

/Tomas

Comments

Michael Niedermayer Aug. 9, 2017, 7:42 p.m.
On Tue, Aug 08, 2017 at 11:49:45PM +0200, Tomas Härdin wrote:
 [...]

>  ffmpeg.c |    3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 5bd20883fdc12aefa609fc803fe5709069b3e9a0  0003-Don-t-complain-about-codec2-s-700-bit-s-modes-in-ffm.patch
> From b693b6175289e6ad0c643462d8f69f6830086099 Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tjoppen@acc.umu.se>
> Date: Thu, 3 Aug 2017 17:33:04 +0200
> Subject: [PATCH 3/3] Don't complain about codec2's 700 bit/s modes in ffmpeg.c
> 
> ---
>  ffmpeg.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/ffmpeg.c b/ffmpeg.c
> index 888d19a647..09a5b541c0 100644
> --- a/ffmpeg.c
> +++ b/ffmpeg.c
> @@ -3480,7 +3480,8 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len)
>              av_buffersink_set_frame_size(ost->filter->filter,
>                                              ost->enc_ctx->frame_size);
>          assert_avoptions(ost->encoder_opts);
> -        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)
> +        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
> +            ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
>              av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
>                                           " It takes bits/s as argument, not kbits/s\n");
>  

LGTM

alternatively you could add a minimum bitrate parameter to AVCodec
or AVCodecDescriptor

thx

[...]
Paul B Mahol Aug. 10, 2017, 8:23 a.m.
On 8/9/17, Michael Niedermayer <michael@niedermayer.cc> wrote:
> On Tue, Aug 08, 2017 at 11:49:45PM +0200, Tomas Härdin wrote:
>  [...]
>
>>  ffmpeg.c |    3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>> 5bd20883fdc12aefa609fc803fe5709069b3e9a0
>> 0003-Don-t-complain-about-codec2-s-700-bit-s-modes-in-ffm.patch
>> From b693b6175289e6ad0c643462d8f69f6830086099 Mon Sep 17 00:00:00 2001
>> From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tjoppen@acc.umu.se>
>> Date: Thu, 3 Aug 2017 17:33:04 +0200
>> Subject: [PATCH 3/3] Don't complain about codec2's 700 bit/s modes in
>> ffmpeg.c
>>
>> ---
>>  ffmpeg.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/ffmpeg.c b/ffmpeg.c
>> index 888d19a647..09a5b541c0 100644
>> --- a/ffmpeg.c
>> +++ b/ffmpeg.c
>> @@ -3480,7 +3480,8 @@ static int init_output_stream(OutputStream *ost,
>> char *error, int error_len)
>>              av_buffersink_set_frame_size(ost->filter->filter,
>>                                              ost->enc_ctx->frame_size);
>>          assert_avoptions(ost->encoder_opts);
>> -        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)
>> +        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&
>> +            ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't
>> complain about 700 bit/s modes */)
>>              av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set
>> too low."
>>                                           " It takes bits/s as argument,
>> not kbits/s\n");
>>
>
> LGTM
>
> alternatively you could add a minimum bitrate parameter to AVCodec
> or AVCodecDescriptor

Please no.
Tomas Härdin Aug. 11, 2017, 6:39 a.m.
---- Michael Niedermayer skrev ----

> On Tue, Aug 08, 2017 at 11:49:45PM +0200, Tomas Härdin wrote:

>  [...]

> 

> >  ffmpeg.c |    3 ++-

> >  1 file changed, 2 insertions(+), 1 deletion(-)

> > 5bd20883fdc12aefa609fc803fe5709069b3e9a0  0003-Don-t-complain-about-codec2-s-700-bit-s-modes-in-ffm.patch

> > From b693b6175289e6ad0c643462d8f69f6830086099 Mon Sep 17 00:00:00 2001

> > From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tjoppen@acc.umu.se>

> > Date: Thu, 3 Aug 2017 17:33:04 +0200

> > Subject: [PATCH 3/3] Don't complain about codec2's 700 bit/s modes in ffmpeg.c

> > 

> > ---

> >  ffmpeg.c | 3 ++-

> >  1 file changed, 2 insertions(+), 1 deletion(-)

> > 

> > diff --git a/ffmpeg.c b/ffmpeg.c

> > index 888d19a647..09a5b541c0 100644

> > --- a/ffmpeg.c

> > +++ b/ffmpeg.c

> > @@ -3480,7 +3480,8 @@ static int init_output_stream(OutputStream *ost, char *error, int error_len)

> >              av_buffersink_set_frame_size(ost->filter->filter,

> >                                              ost->enc_ctx->frame_size);

> >          assert_avoptions(ost->encoder_opts);

> > -        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000)

> > +        if (ost->enc_ctx->bit_rate && ost->enc_ctx->bit_rate < 1000 &&

> > +            ost->enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)

> >              av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."

> >                                           " It takes bits/s as argument, not kbits/s\n");

> >  

> 

> LGTM

> 

> alternatively you could add a minimum bitrate parameter to AVCodec

> or AVCodecDescriptor


That seems a bit excessive for just one case. Bitrate merely gets set as a side effect of setting mode.

/Tomas
Moritz Barsnick Aug. 15, 2017, 7:15 p.m.
On Tue, Aug 08, 2017 at 23:49:45 +0200, Tomas Härdin wrote:
> Feel free to comment

Don't forget to mention #1959 in the commit message.

Hilsen,
Moritz
Tomas Härdin Aug. 16, 2017, 7:56 a.m.
On 2017-08-15 21:15, Moritz Barsnick wrote:
> On Tue, Aug 08, 2017 at 23:49:45 +0200, Tomas Härdin wrote:
>> Feel free to comment
> Don't forget to mention #1959 in the commit message.

Huh, I didn't know there was a ticket for it. That audiobook guy popped 
up on [Freetel-codec2] too, and thus the container discussion was born. 
It's also part of the reason why a raw demuxer is needed. Guess I'll 
have to send him some e-mail too, maybe I can nip some format headaches 
in the bud

/Tomas

Patch hide | download patch | download mbox

From c9ba525447a461a9e6961e942f83d9bd8bdf342b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <tjoppen@acc.umu.se>
Date: Tue, 8 Aug 2017 15:27:27 +0200
Subject: [PATCH 1/3] Add libcodec2 en/decoder

---
 Changelog                |   1 +
 configure                |   5 ++
 doc/general.texi         |  11 +++
 libavcodec/Makefile      |   2 +
 libavcodec/allcodecs.c   |   1 +
 libavcodec/avcodec.h     |   1 +
 libavcodec/codec2utils.c |  80 +++++++++++++++++++
 libavcodec/codec2utils.h |  92 ++++++++++++++++++++++
 libavcodec/codec_desc.c  |   7 ++
 libavcodec/libcodec2.c   | 196 +++++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/version.h     |   2 +-
 11 files changed, 397 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/codec2utils.c
 create mode 100644 libavcodec/codec2utils.h
 create mode 100644 libavcodec/libcodec2.c

diff --git a/Changelog b/Changelog
index c797d68a36..a3a16f0073 100644
--- a/Changelog
+++ b/Changelog
@@ -32,6 +32,7 @@  version <next>:
 - unpremultiply video filter
 - tlut2 video filter
 - floodfill video filter
+- codec2 en/decoding via libcodec2
 
 version 3.3:
 - CrystalHD decoder moved to new decode API
diff --git a/configure b/configure
index 66c7b948e4..50f455af71 100755
--- a/configure
+++ b/configure
@@ -220,6 +220,7 @@  External library support:
   --enable-libcaca         enable textual display using libcaca [no]
   --enable-libcelt         enable CELT decoding via libcelt [no]
   --enable-libcdio         enable audio CD grabbing with libcdio [no]
+  --enable-libcodec2       enable codec2 en/decoding using libcodec2 [no]
   --enable-libdc1394       enable IIDC-1394 grabbing using libdc1394
                            and libraw1394 [no]
   --enable-libfdk-aac      enable AAC de/encoding via libfdk-aac [no]
@@ -1540,6 +1541,7 @@  EXTERNAL_LIBRARY_LIST="
     libbs2b
     libcaca
     libcelt
+    libcodec2
     libdc1394
     libflite
     libfontconfig
@@ -2863,6 +2865,8 @@  pcm_mulaw_at_encoder_select="audio_frame_queue"
 chromaprint_muxer_deps="chromaprint"
 h264_videotoolbox_encoder_deps="videotoolbox_encoder pthreads"
 libcelt_decoder_deps="libcelt"
+libcodec2_decoder_deps="libcodec2"
+libcodec2_encoder_deps="libcodec2"
 libfdk_aac_decoder_deps="libfdk_aac"
 libfdk_aac_encoder_deps="libfdk_aac"
 libfdk_aac_encoder_select="audio_frame_queue"
@@ -5837,6 +5841,7 @@  enabled libcelt           && require libcelt celt/celt.h celt_decode -lcelt0 &&
                              { check_lib libcelt celt/celt.h celt_decoder_create_custom -lcelt0 ||
                                die "ERROR: libcelt must be installed and version must be >= 0.11.0."; }
 enabled libcaca           && require_pkg_config caca caca.h caca_create_canvas
+enabled libcodec2         && require libcodec2 codec2/codec2.h codec2_create -lcodec2
 enabled libdc1394         && require_pkg_config libdc1394-2 dc1394/dc1394.h dc1394_new
 enabled libfdk_aac        && { use_pkg_config fdk-aac "fdk-aac/aacenc_lib.h" aacEncOpen ||
                                { require libfdk_aac fdk-aac/aacenc_lib.h aacEncOpen -lfdk-aac &&
diff --git a/doc/general.texi b/doc/general.texi
index 036c8c25d4..fd8d657e4e 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -85,6 +85,15 @@  Go to @url{http://www.twolame.org/} and follow the
 instructions for installing the library.
 Then pass @code{--enable-libtwolame} to configure to enable it.
 
+@section libcodec2
+
+FFmpeg can make use of libcodec2 to codec2 encoding and decoding.
+There is currently no native decoder, so libcodec2 must be used for decoding.
+
+Go to @url{http://freedv.org/}, download "Codec 2 source archive".
+Build and install using CMake. Debian users can install the libcodec2-dev package instead.
+Once libcodec2 is installed you can pass @code{--enable-libcodec2} to configure to enable it.
+
 @section libvpx
 
 FFmpeg can make use of the libvpx library for VP8/VP9 encoding.
@@ -994,6 +1003,8 @@  following image formats are supported:
     @tab Used in Bink and Smacker files in many games.
 @item CELT                   @tab     @tab  E
     @tab decoding supported through external library libcelt
+@item codec2                 @tab  E  @tab  E
+    @tab en/decoding supported through external library libcodec2
 @item Delphine Software International CIN audio  @tab     @tab  X
     @tab Codec used in Delphine Software International games.
 @item Digital Speech Standard - Standard Play mode (DSS SP) @tab     @tab  X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index b0c39ac040..4ba1f8e138 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -885,6 +885,8 @@  OBJS-$(CONFIG_ILBC_AT_ENCODER)            += audiotoolboxenc.o
 OBJS-$(CONFIG_PCM_ALAW_AT_ENCODER)        += audiotoolboxenc.o
 OBJS-$(CONFIG_PCM_MULAW_AT_ENCODER)       += audiotoolboxenc.o
 OBJS-$(CONFIG_LIBCELT_DECODER)            += libcelt_dec.o
+OBJS-$(CONFIG_LIBCODEC2_DECODER)          += libcodec2.o codec2utils.o
+OBJS-$(CONFIG_LIBCODEC2_ENCODER)          += libcodec2.o codec2utils.o
 OBJS-$(CONFIG_LIBFDK_AAC_DECODER)         += libfdk-aacdec.o
 OBJS-$(CONFIG_LIBFDK_AAC_ENCODER)         += libfdk-aacenc.o
 OBJS-$(CONFIG_LIBGSM_DECODER)             += libgsmdec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 4712592a5f..5296fac507 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -618,6 +618,7 @@  static void register_all(void)
     REGISTER_DECODER(QDMC_AT,           qdmc_at);
     REGISTER_DECODER(QDM2_AT,           qdm2_at);
     REGISTER_DECODER(LIBCELT,           libcelt);
+    REGISTER_ENCDEC (LIBCODEC2,         libcodec2);
     REGISTER_ENCDEC (LIBFDK_AAC,        libfdk_aac);
     REGISTER_ENCDEC (LIBGSM,            libgsm);
     REGISTER_ENCDEC (LIBGSM_MS,         libgsm_ms);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index c594993766..488eb8b1f5 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -622,6 +622,7 @@  enum AVCodecID {
     AV_CODEC_ID_PAF_AUDIO,
     AV_CODEC_ID_ON2AVC,
     AV_CODEC_ID_DSS_SP,
+    AV_CODEC_ID_CODEC2,
 
     AV_CODEC_ID_FFWAVESYNTH = 0x15800,
     AV_CODEC_ID_SONIC,
diff --git a/libavcodec/codec2utils.c b/libavcodec/codec2utils.c
new file mode 100644
index 0000000000..fb8b4b48b3
--- /dev/null
+++ b/libavcodec/codec2utils.c
@@ -0,0 +1,80 @@ 
+/*
+ * codec2 utility functions
+ * Copyright (c) 2017 Tomas Härdin
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <string.h>
+#include "internal.h"
+#include "libavcodec/codec2utils.h"
+
+int avpriv_codec2_mode_bit_rate(void *logctx, int mode)
+{
+    int frame_size  = avpriv_codec2_mode_frame_size(logctx, mode);
+    int block_align = avpriv_codec2_mode_block_align(logctx, mode);
+
+    if (frame_size <= 0 || block_align <= 0) {
+        return 0;
+    }
+
+    return 8 * 8000 * block_align / frame_size;
+}
+
+int avpriv_codec2_mode_frame_size(void *logctx, int mode)
+{
+    int frame_size_table[AVPRIV_CODEC2_MAX_KNOWN_MODE+1] = {
+        160,    // 3200
+        160,    // 2400
+        320,    // 1600
+        320,    // 1400
+        320,    // 1300
+        320,    // 1200
+        320,    // 700
+        320,    // 700B
+        320,    // 700C
+    };
+
+    if (mode < 0 || mode > AVPRIV_CODEC2_MAX_KNOWN_MODE) {
+        av_log(logctx, AV_LOG_ERROR, "unknown codec2 mode %i, can't find frame_size\n", mode);
+        return 0;
+    } else {
+        return frame_size_table[mode];
+    }
+}
+
+int avpriv_codec2_mode_block_align(void *logctx, int mode)
+{
+    int block_align_table[AVPRIV_CODEC2_MAX_KNOWN_MODE+1] = {
+        8,      // 3200
+        6,      // 2400
+        8,      // 1600
+        7,      // 1400
+        7,      // 1300
+        6,      // 1200
+        4,      // 700
+        4,      // 700B
+        4,      // 700C
+    };
+
+    if (mode < 0 || mode > AVPRIV_CODEC2_MAX_KNOWN_MODE) {
+        av_log(logctx, AV_LOG_ERROR, "unknown codec2 mode %i, can't find block_align\n", mode);
+        return 0;
+    } else {
+        return block_align_table[mode];
+    }
+}
diff --git a/libavcodec/codec2utils.h b/libavcodec/codec2utils.h
new file mode 100644
index 0000000000..383e40e932
--- /dev/null
+++ b/libavcodec/codec2utils.h
@@ -0,0 +1,92 @@ 
+/*
+ * codec2 utility functions
+ * Copyright (c) 2017 Tomas Härdin
+ *
+ * Constants taken from codec2.h in libcodec2, which is also under the LGPL 2.1
+ * Copyright (C) 2010 David Rowe
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_CODEC2UTILS_H
+#define AVCODEC_CODEC2UTILS_H
+
+#include <stdint.h>
+
+#ifndef __CODEC2__
+//from codec2.h, repeated here to avoid a dependency
+#define CODEC2_MODE_3200 0
+#define CODEC2_MODE_2400 1
+#define CODEC2_MODE_1600 2
+#define CODEC2_MODE_1400 3
+#define CODEC2_MODE_1300 4
+#define CODEC2_MODE_1200 5
+#define CODEC2_MODE_700  6
+#define CODEC2_MODE_700B 7
+#define CODEC2_MODE_700C 8
+#endif
+
+#define AVPRIV_CODEC2_MAX_KNOWN_MODE CODEC2_MODE_700C
+
+//used by both codec2raw demuxer and libcodec2 encoder
+#define AVPRIV_CODEC2_AVOPTIONS(desc, classname, min_val, default_val, option_flags) \
+    { "mode", desc, offsetof(classname, mode), AV_OPT_TYPE_INT, {.i64 = default_val}, min_val, AVPRIV_CODEC2_MAX_KNOWN_MODE, .flags=option_flags, .unit="codec2_mode"},\
+    { "3200", "3200", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC2_MODE_3200}, .flags=option_flags, .unit="codec2_mode"},\
+    { "2400", "2400", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC2_MODE_2400}, .flags=option_flags, .unit="codec2_mode"},\
+    { "1600", "1600", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC2_MODE_1600}, .flags=option_flags, .unit="codec2_mode"},\
+    { "1400", "1400", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC2_MODE_1400}, .flags=option_flags, .unit="codec2_mode"},\
+    { "1300", "1300", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC2_MODE_1300}, .flags=option_flags, .unit="codec2_mode"},\
+    { "1200", "1200", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC2_MODE_1200}, .flags=option_flags, .unit="codec2_mode"},\
+    { "700",  "700",  0, AV_OPT_TYPE_CONST, {.i64 = CODEC2_MODE_700},  .flags=option_flags, .unit="codec2_mode"},\
+    { "700B", "700B", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC2_MODE_700B}, .flags=option_flags, .unit="codec2_mode"},\
+    { "700C", "700C", 0, AV_OPT_TYPE_CONST, {.i64 = CODEC2_MODE_700C}, .flags=option_flags, .unit="codec2_mode"}
+
+//The three following functions are here to avoid needing libavformat/codec2.c to depend on libcodec2
+
+//Computes bitrate from mode, with frames rounded up to the nearest octet.
+//So 700 bit/s (28 bits/frame) becomes 800 bits/s (32 bits/frame).
+//logctx is used for av_log()
+//Returns <0 if mode is invalid
+int avpriv_codec2_mode_bit_rate(void *logctx, int mode);
+
+//duplicates codec2_samples_per_frame()
+int avpriv_codec2_mode_frame_size(void *logctx, int mode);
+
+//duplicates (codec2_bits_per_frame()+7)/8
+int avpriv_codec2_mode_block_align(void *logctx, int mode);
+
+#define AVPRIV_CODEC2_EXTRADATA_SIZE 4
+
+//Used in codec2raw demuxer and libcodec2 encoder
+static inline void avpriv_codec2_make_extradata(uint8_t *ptr, int mode) {
+    //version 0.8 as of 2017-08-02 (r3345)
+    ptr[0] = 0;     //major
+    ptr[1] = 8;     //minor
+    ptr[2] = mode;  //mode
+    ptr[3] = 0;     //flags
+}
+
+//Returns version as a 16-bit value. 0.8 -> 0x0008
+static inline uint16_t avpriv_codec2_version_from_extradata(uint8_t *ptr) {
+    return (ptr[0] << 8) + ptr[1];
+}
+
+static inline uint8_t avpriv_codec2_mode_from_extradata(uint8_t *ptr) {
+    return ptr[2];
+}
+
+#endif /* AVCODEC_CODEC2UTILS_H */
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 6f43b68b83..b5f5c2d9d8 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2657,6 +2657,13 @@  static const AVCodecDescriptor codec_descriptors[] = {
         .props     = AV_CODEC_PROP_LOSSY,
     },
     {
+        .id        = AV_CODEC_ID_CODEC2,
+        .type      = AVMEDIA_TYPE_AUDIO,
+        .name      = "codec2",
+        .long_name = NULL_IF_CONFIG_SMALL("codec2 (very low bitrate speech codec)"),
+        .props     = AV_CODEC_PROP_LOSSY,
+    },
+    {
         .id        = AV_CODEC_ID_G723_1,
         .type      = AVMEDIA_TYPE_AUDIO,
         .name      = "g723_1",
diff --git a/libavcodec/libcodec2.c b/libavcodec/libcodec2.c
new file mode 100644
index 0000000000..0373a425ed
--- /dev/null
+++ b/libavcodec/libcodec2.c
@@ -0,0 +1,196 @@ 
+/*
+ * codec2 encoder/decoder using libcodec2
+ * Copyright (c) 2017 Tomas Härdin
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <codec2/codec2.h>
+#include "avcodec.h"
+#include "libavutil/opt.h"
+#include "internal.h"
+#include "codec2utils.h"
+
+typedef struct {
+    const AVClass *class;
+    struct CODEC2 *codec;
+    int mode;
+} LibCodec2Context;
+
+static const AVOption options[] = {
+    //not AV_OPT_FLAG_DECODING_PARAM since mode should come from the demuxer
+    //1300 (aka FreeDV 1600) is the most common mode on-the-air, default to it here as well
+    AVPRIV_CODEC2_AVOPTIONS("codec2 mode", LibCodec2Context, 0, CODEC2_MODE_1300, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM),
+    { NULL },
+};
+
+static const AVClass libcodec2_enc_class = {
+    .class_name = "libcodec2 encoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static const AVClass libcodec2_dec_class = {
+    .class_name = "libcodec2 decoder",
+    .item_name  = av_default_item_name,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+static av_cold int libcodec2_init_common(AVCodecContext *avctx, int mode)
+{
+    LibCodec2Context *c2 = avctx->priv_data;
+
+    c2->codec = codec2_create(mode);
+    if (!c2->codec) {
+        //it's also possible that the mode is wrong,
+        //but we can't tell for sure with the current API
+        return AVERROR(ENOMEM);
+    }
+
+    avctx->frame_size = codec2_samples_per_frame(c2->codec);
+    avctx->block_align = (codec2_bits_per_frame(c2->codec) + 7) / 8;
+    codec2_set_natural_or_gray(c2->codec, 1);
+
+    return 0;
+}
+
+static av_cold int libcodec2_init_decoder(AVCodecContext *avctx)
+{
+    avctx->sample_rate      = 8000;
+    avctx->channels         = 1;
+    avctx->sample_fmt       = AV_SAMPLE_FMT_S16;
+    avctx->channel_layout   = AV_CH_LAYOUT_MONO;
+
+    if (avctx->extradata_size != AVPRIV_CODEC2_EXTRADATA_SIZE) {
+        av_log(avctx, AV_LOG_ERROR, "must have exactly %i bytes of extradata (got %i)\n",
+               AVPRIV_CODEC2_EXTRADATA_SIZE, avctx->extradata_size);
+        return AVERROR_INVALIDDATA;
+    }
+
+    return libcodec2_init_common(avctx, avpriv_codec2_mode_from_extradata(avctx->extradata));
+}
+
+static av_cold int libcodec2_init_encoder(AVCodecContext *avctx)
+{
+    LibCodec2Context *c2 = avctx->priv_data;
+
+    //will need to be smarter once we get wideband support
+    if (avctx->sample_rate != 8000 ||
+        avctx->channels != 1 ||
+        avctx->sample_fmt != AV_SAMPLE_FMT_S16) {
+        av_log(avctx, AV_LOG_ERROR, "only 8 kHz 16-bit mono allowed\n");
+        return AVERROR(EINVAL);
+    }
+
+    avctx->extradata = av_mallocz(AVPRIV_CODEC2_EXTRADATA_SIZE + AV_INPUT_BUFFER_PADDING_SIZE);
+    if (!avctx->extradata) {
+        return AVERROR(ENOMEM);
+    }
+
+    avctx->extradata_size = AVPRIV_CODEC2_EXTRADATA_SIZE;
+    avpriv_codec2_make_extradata(avctx->extradata, c2->mode);
+
+    return libcodec2_init_common(avctx, c2->mode);
+}
+
+static av_cold int libcodec2_close(AVCodecContext *avctx)
+{
+    LibCodec2Context *c2 = avctx->priv_data;
+
+    codec2_destroy(c2->codec);
+    return 0;
+}
+
+static int libcodec2_decode(AVCodecContext *avctx, void *data,
+                            int *got_frame_ptr, AVPacket *pkt)
+{
+    LibCodec2Context *c2 = avctx->priv_data;
+    AVFrame *frame = data;
+    int ret, nframes, i;
+    uint8_t *input;
+    int16_t *output;
+
+    nframes           = pkt->size / avctx->block_align;
+    frame->nb_samples = avctx->frame_size * nframes;
+
+    ret = ff_get_buffer(avctx, frame, 0);
+    if (ret < 0) {
+        return ret;
+    }
+
+    input  = pkt->data;
+    output = (int16_t *)frame->data[0];
+
+    for (i = 0; i < nframes; i++) {
+        codec2_decode(c2->codec, output, input);
+        input  += avctx->block_align;
+        output += avctx->frame_size;
+    }
+
+    *got_frame_ptr = nframes > 0;
+    return nframes * avctx->block_align;
+}
+
+static int libcodec2_encode(AVCodecContext *avctx, AVPacket *avpkt,
+                            const AVFrame *frame, int *got_packet_ptr)
+{
+    LibCodec2Context *c2 = avctx->priv_data;
+    int16_t *samples = (int16_t *)frame->data[0];
+
+    int ret = ff_alloc_packet2(avctx, avpkt, avctx->block_align, 0);
+    if (ret < 0) {
+        return ret;
+    }
+
+    codec2_encode(c2->codec, avpkt->data, samples);
+    *got_packet_ptr = 1;
+
+    return 0;
+}
+
+AVCodec ff_libcodec2_decoder = {
+    .name                   = "libcodec2",
+    .long_name              = NULL_IF_CONFIG_SMALL("codec2 decoder using libcodec2"),
+    .type                   = AVMEDIA_TYPE_AUDIO,
+    .id                     = AV_CODEC_ID_CODEC2,
+    .priv_data_size         = sizeof(LibCodec2Context),
+    .init                   = libcodec2_init_decoder,
+    .close                  = libcodec2_close,
+    .decode                 = libcodec2_decode,
+    .capabilities           = 0,
+    .supported_samplerates  = (const int[]){ 8000, 0 },
+    .sample_fmts            = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
+    .channel_layouts        = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 },
+    .priv_class             = &libcodec2_dec_class,
+};
+
+AVCodec ff_libcodec2_encoder = {
+    .name                   = "libcodec2",
+    .long_name              = NULL_IF_CONFIG_SMALL("codec2 encoder using libcodec2"),
+    .type                   = AVMEDIA_TYPE_AUDIO,
+    .id                     = AV_CODEC_ID_CODEC2,
+    .priv_data_size         = sizeof(LibCodec2Context),
+    .init                   = libcodec2_init_encoder,
+    .close                  = libcodec2_close,
+    .encode2                = libcodec2_encode,
+    .capabilities           = 0,
+    .supported_samplerates  = (const int[]){ 8000, 0 },
+    .sample_fmts            = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE },
+    .channel_layouts        = (const uint64_t[]) { AV_CH_LAYOUT_MONO, 0 },
+    .priv_class             = &libcodec2_enc_class,
+};
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 02c4f41800..7473000579 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -28,7 +28,7 @@ 
 #include "libavutil/version.h"
 
 #define LIBAVCODEC_VERSION_MAJOR  57
-#define LIBAVCODEC_VERSION_MINOR 102
+#define LIBAVCODEC_VERSION_MINOR 103
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
-- 
2.13.3