From ac3a91bb750ab719286fd2d3cf3607558d034310 Mon Sep 17 00:00:00 2001
From: Carl Eugen Hoyos <ceffmpeg@gmail.com>
Date: Mon, 21 Aug 2017 16:17:17 +0200
Subject: [PATCH] lavf/g726: Demuxer for raw G.726 streams, both left- and
 right-justified.

Compatible with the binary encoder attached to ticket #6596 (right-aligned)
and a sample from a SEG Mp3-Player (left-aligned).
---
 Changelog                |    1 +
 doc/general.texi         |    1 +
 libavformat/Makefile     |    2 +
 libavformat/allformats.c |    2 +
 libavformat/g726.c       |  105 ++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h    |    2 +-
 6 files changed, 112 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/g726.c

diff --git a/Changelog b/Changelog
index 7a6987a..f5dc1da 100644
--- a/Changelog
+++ b/Changelog
@@ -33,6 +33,7 @@ version <next>:
 - tlut2 video filter
 - floodfill video filter
 - pseudocolor video filter
+- raw G.726 demuxer, left- and right-justified
 
 version 3.3:
 - CrystalHD decoder moved to new decode API
diff --git a/doc/general.texi b/doc/general.texi
index 036c8c2..49eb969 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -341,6 +341,7 @@ library:
 @item FunCom ISS                @tab   @tab X
     @tab Audio format used in various games from FunCom like The Longest Journey.
 @item G.723.1                   @tab X @tab X
+@item G.726                     @tab   @tab X
 @item G.729 BIT                 @tab X @tab X
 @item G.729 raw                 @tab   @tab X
 @item GENH                      @tab   @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index b0ef82c..8e2afb7 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -188,6 +188,8 @@ OBJS-$(CONFIG_GSM_MUXER)                 += rawenc.o
 OBJS-$(CONFIG_GXF_DEMUXER)               += gxf.o
 OBJS-$(CONFIG_GXF_MUXER)                 += gxfenc.o audiointerleave.o
 OBJS-$(CONFIG_G722_DEMUXER)              += g722.o rawdec.o
+OBJS-$(CONFIG_G726_DEMUXER)              += g726.o
+OBJS-$(CONFIG_G726LE_DEMUXER)            += g726.o
 OBJS-$(CONFIG_G722_MUXER)                += rawenc.o
 OBJS-$(CONFIG_G723_1_DEMUXER)            += g723_1.o
 OBJS-$(CONFIG_G723_1_MUXER)              += rawenc.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 1ebc142..78ff2eb 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -132,6 +132,8 @@ static void register_all(void)
     REGISTER_DEMUXER (FRM,              frm);
     REGISTER_DEMUXER (FSB,              fsb);
     REGISTER_MUXDEMUX(G722,             g722);
+    REGISTER_DEMUXER (G726,             g726);
+    REGISTER_DEMUXER (G726LE,           g726le);
     REGISTER_MUXDEMUX(G723_1,           g723_1);
     REGISTER_DEMUXER (G729,             g729);
     REGISTER_DEMUXER (GDV,              gdv);
diff --git a/libavformat/g726.c b/libavformat/g726.c
new file mode 100644
index 0000000..eca9404
--- /dev/null
+++ b/libavformat/g726.c
@@ -0,0 +1,105 @@
+/*
+ * G.726 raw demuxer
+ * Copyright 2017 Carl Eugen Hoyos
+ *
+ * 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 "avformat.h"
+#include "internal.h"
+#include "libavutil/opt.h"
+
+typedef struct G726Context {
+    AVClass *class;
+    int code_size;
+    int sample_rate;
+} G726Context;
+
+static int g726_read_header(AVFormatContext *s)
+{
+    G726Context *c = s->priv_data;
+    AVStream *st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+    st->codecpar->codec_id   = s->iformat->raw_codec_id;
+
+    st->codecpar->sample_rate           = c->sample_rate;
+    st->codecpar->bits_per_coded_sample = c->code_size;
+    st->codecpar->bit_rate              = ((int[]){ 16000, 24000, 32000, 40000 })[c->code_size - 2];
+    st->codecpar->channels              = 1;
+
+    return 0;
+}
+
+static int g726_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    int res;
+    res = av_get_packet(s->pb, pkt, 1020); // a size similar to RAW_PACKET_SIZE divisible by all code_size values
+    if (res < 0)
+        return res;
+    return 0;
+}
+
+#define OFFSET(x) offsetof(G726Context, x)
+static const AVOption options[] = {
+    { "code_size", "Bits per G.726 code",
+        OFFSET(code_size),   AV_OPT_TYPE_INT, {.i64 =    4}, 2,       5, AV_OPT_FLAG_DECODING_PARAM },
+    { "sample_rate", "",
+        OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 8000}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
+    { NULL },
+};
+
+#if CONFIG_G726_DEMUXER
+static const AVClass g726le_demuxer_class = {
+    .class_name     = "G.726 big-endian demuxer",
+    .item_name      = av_default_item_name,
+    .option         = options,
+    .version        = LIBAVUTIL_VERSION_INT,
+};
+
+AVInputFormat ff_g726_demuxer = {
+    .name           = "g726",
+    .long_name      = NULL_IF_CONFIG_SMALL("raw big-endian G.726 (\"left aligned\")"),
+    .read_header    = g726_read_header,
+    .read_packet    = g726_read_packet,
+    .priv_data_size = sizeof(G726Context),
+    .priv_class     = &g726le_demuxer_class,
+    .raw_codec_id   = AV_CODEC_ID_ADPCM_G726,
+};
+#endif
+
+#if CONFIG_G726LE_DEMUXER
+static const AVClass g726_demuxer_class = {
+    .class_name     = "G.726 little-endian demuxer",
+    .item_name      = av_default_item_name,
+    .option         = options,
+    .version        = LIBAVUTIL_VERSION_INT,
+};
+
+AVInputFormat ff_g726le_demuxer = {
+    .name           = "g726le",
+    .long_name      = NULL_IF_CONFIG_SMALL("raw little-endian G.726 (\"right aligned\")"),
+    .read_header    = g726_read_header,
+    .read_packet    = g726_read_packet,
+    .priv_data_size = sizeof(G726Context),
+    .priv_class     = &g726_demuxer_class,
+    .raw_codec_id   = AV_CODEC_ID_ADPCM_G726LE,
+};
+#endif
+
diff --git a/libavformat/version.h b/libavformat/version.h
index 48b81f2..a8cf4c1 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  57
-#define LIBAVFORMAT_VERSION_MINOR  76
+#define LIBAVFORMAT_VERSION_MINOR  77
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
1.7.10.4

