On 6 April 2010 02:18, Ronald S. Bultje <rsbul...@gmail.com> wrote:

> Hi Mohamed,
>
> On Mon, Apr 5, 2010 at 8:55 AM, Mohamed Naufal <naufa...@gmail.com> wrote:
> > +    temp = get_bits(&gb, 7);
> > +    if (temp <= 123)       // test if forbidden code
> > +        frame->subframe[0].pitch_lag = temp + PITCH_MIN;
> > +    else
> > +        return -1;         // transmission error
> > +
>
> temp = get_bits(&gb, 7);
> if ((temp & 0x7c) == 0x7c) OR if (temp > 123) (whichever you prefer)
>    return -1;
> frame->subframe[0].pitch_lag = ..
>
> Saves one line of code.
>
> > +    temp = get_bits(&gb, 7);
> > +    if (temp <= 123)
> > +        frame->subframe[1].pitch_lag = temp + PITCH_MIN;
> > +    else
> > +        return -1;
>
> Same.
>
> Rest seems OK to me, I'll leave it to Benjamin since I think he will
> be mentor (is that right?). Also I didn't check code for correctness,
> I can do that but only will if Benjamin isn't doing so already.
>
>

Ok, fixed. Also added CODEC_CAP_SUBFRAMES.
Please take a look.

Regards,
Naufal
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 68b42b4..291762b 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -131,6 +131,7 @@ OBJS-$(CONFIG_FRAPS_DECODER)           += fraps.o huffman.o
 OBJS-$(CONFIG_FRWU_DECODER)            += frwu.o
 OBJS-$(CONFIG_GIF_DECODER)             += gifdec.o lzw.o
 OBJS-$(CONFIG_GIF_ENCODER)             += gif.o lzwenc.o
+OBJS-$(CONFIG_G723_1_DECODER)          += g723_1.o
 OBJS-$(CONFIG_H261_DECODER)            += h261dec.o h261.o \
                                           mpegvideo.o error_resilience.o
 OBJS-$(CONFIG_H261_ENCODER)            += h261enc.o h261.o             \
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 6db65cf..c901ed3 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -227,6 +227,7 @@ void avcodec_register_all(void)
     REGISTER_DECODER (DSICINAUDIO, dsicinaudio);
     REGISTER_DECODER (EAC3, eac3);
     REGISTER_ENCDEC  (FLAC, flac);
+    REGISTER_DECODER (G723_1, g723_1);
     REGISTER_DECODER (IMC, imc);
     REGISTER_DECODER (MACE3, mace3);
     REGISTER_DECODER (MACE6, mace6);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 23ea4bb..b70c713 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -335,6 +335,7 @@ enum CodecID {
     CODEC_ID_ATRAC1,
     CODEC_ID_BINKAUDIO_RDFT,
     CODEC_ID_BINKAUDIO_DCT,
+    CODEC_ID_G723_1,
 
     /* subtitle codecs */
     CODEC_ID_DVD_SUBTITLE= 0x17000,
diff --git a/libavcodec/g723_1.c b/libavcodec/g723_1.c
new file mode 100644
index 0000000..9017126
--- /dev/null
+++ b/libavcodec/g723_1.c
@@ -0,0 +1,170 @@
+#include "avcodec.h"
+#define ALT_BITSTREAM_READER_LE
+#include "get_bits.h"
+
+#define PITCH_MIN 18
+
+/*
+ * G723.1 frame types
+ */
+typedef enum {
+    Frame6k3 = 0,       ///< 6.3 kbps frame rate
+    Frame5k3,           ///< 5.3 kbps frame rate
+    FrameSID,           ///< silence insertion descriptor frame
+    FrameUntransmitted
+} FrameType;
+
+static const uint8_t frame_size[4] = {24, 20, 4, 1};
+
+typedef enum {
+    Rate6k3,
+    Rate5k3
+} Rate;
+
+/*
+ * G723.1 unpacked data subframe
+ */
+typedef struct {
+    uint16_t ad_cb_lag;     ///< adaptive codebook lag
+    uint16_t pitch_lag;
+    uint16_t combined_gain;
+    uint16_t pulse_pos;
+    uint16_t pulse_sign;
+    uint16_t grid_index;
+    uint16_t amp_index;     ///< for SID frames
+} G723_1_Subframe;
+
+/*
+ * G723.1 unpacked data frame
+ */
+typedef struct {
+    uint32_t lsp_index;
+    G723_1_Subframe subframe[4];
+} G723_1_Frame;
+
+typedef struct g723_1_context {
+    G723_1_Frame frame;
+    FrameType cur_frame_type;
+    Rate cur_rate;
+} G723_1_Context;
+
+/*
+ * Unpack the frame into parameters.
+ *
+ * @param p           the context
+ * @param buf         pointer to the input buffer
+ * @param buf_size    size of the input buffer
+ */
+static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf,
+                            int buf_size)
+{
+    GetBitContext gb;
+    G723_1_Frame *frame = &p->frame;
+    uint32_t temp;
+    int i;
+
+    init_get_bits(&gb, buf, buf_size * 8);
+
+    // Extract frame type and rate info
+    p->cur_frame_type = get_bits(&gb, 2);
+
+    if (p->cur_frame_type == FrameUntransmitted)
+        return 0;
+
+    frame->lsp_index = get_bits(&gb, 24);
+
+    if (p->cur_frame_type == FrameSID) {
+        frame->subframe[0].amp_index = get_bits(&gb, 6);
+        return 0;
+    }
+
+    // Extract the info common to both rates
+    p->cur_rate = (!p->cur_frame_type) ? Rate6k3 : Rate5k3;
+
+    temp = get_bits(&gb, 7);
+    if (temp > 123)       // test if forbidden code
+        return -1;
+    frame->subframe[0].pitch_lag = temp + PITCH_MIN;
+    frame->subframe[1].ad_cb_lag = get_bits(&gb, 2);
+
+    temp = get_bits(&gb, 7);
+    if (temp > 123)
+        return -1;
+    frame->subframe[1].pitch_lag  = temp + PITCH_MIN;
+    frame->subframe[3].ad_cb_lag  = get_bits(&gb, 2);
+    frame->subframe[0].ad_cb_lag  = 1;
+    frame->subframe[2].ad_cb_lag  = 1;
+
+    for (i = 0; i < 4; i++)
+        frame->subframe[i].combined_gain = get_bits(&gb, 12);
+
+    for (i = 0; i < 4; i++)
+        frame->subframe[i].grid_index = get_bits(&gb, 1);
+
+    if (p->cur_frame_type == Frame6k3) {
+        skip_bits(&gb, 1);  // skip reserved bit
+
+        // Compute pulse_pos index using the 13bit combined position index
+        temp = get_bits(&gb, 13);
+        frame->subframe[0].pulse_pos = temp / 810;
+
+        temp -= frame->subframe[0].pulse_pos * 810;
+        frame->subframe[1].pulse_pos = FASTDIV(temp, 90);
+
+        temp -= frame->subframe[1].pulse_pos * 90;
+        frame->subframe[2].pulse_pos = FASTDIV(temp, 9);
+        frame->subframe[3].pulse_pos = temp - frame->subframe[2].pulse_pos * 9;
+
+        frame->subframe[0].pulse_pos = (frame->subframe[0].pulse_pos << 16) +
+                                       get_bits(&gb, 16);
+        frame->subframe[1].pulse_pos = (frame->subframe[1].pulse_pos << 14) +
+                                       get_bits(&gb, 14);
+        frame->subframe[2].pulse_pos = (frame->subframe[2].pulse_pos << 16) +
+                                       get_bits(&gb, 16);
+        frame->subframe[3].pulse_pos = (frame->subframe[3].pulse_pos << 14) +
+                                       get_bits(&gb, 14);
+
+        frame->subframe[0].pulse_sign = get_bits(&gb, 6);
+        frame->subframe[1].pulse_sign = get_bits(&gb, 5);
+        frame->subframe[2].pulse_sign = get_bits(&gb, 6);
+        frame->subframe[3].pulse_sign = get_bits(&gb, 5);
+    } else { // Frame5k3
+        for (i = 0; i < 4; i++)
+            frame->subframe[i].pulse_pos  = get_bits(&gb, 12);
+
+        for (i = 0; i < 4; i++)
+            frame->subframe[i].pulse_sign = get_bits(&gb, 4);
+    }
+
+    return 0;
+}
+
+static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
+                              int *data_size, AVPacket *avpkt)
+{
+    G723_1_Context *p  = avctx->priv_data;
+    const uint8_t *buf = avpkt->data;
+    int buf_size       = avpkt->size;
+
+    if (!buf_size || buf_size < frame_size[buf[0] & 3]) {
+        *data_size = 0;
+        return buf_size;
+    }
+
+    if (unpack_bitstream(p, buf, buf_size) < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Bad frame\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    return frame_size[p->cur_frame_type];
+}
+
+AVCodec g723_1_decoder = {
+    .name           = "g723_1",
+    .type           = AVMEDIA_TYPE_AUDIO,
+    .id             = CODEC_ID_G723_1,
+    .priv_data_size = sizeof(G723_1_Context),
+    .decode         = g723_1_decode_frame,
+    .long_name      = NULL_IF_CONFIG_SMALL("G.723.1"),
+    .capabilities   = CODEC_CAP_SUBFRAMES,
+};
diff --git a/libavformat/rtp.c b/libavformat/rtp.c
index a8dcfd7..b7ab999 100644
--- a/libavformat/rtp.c
+++ b/libavformat/rtp.c
@@ -43,7 +43,7 @@ static const struct
 {
   {0, "PCMU",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_PCM_MULAW, 8000, 1},
   {3, "GSM",         AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
-  {4, "G723",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
+  {4, "G723",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_G723_1, 8000, 1},
   {5, "DVI4",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
   {6, "DVI4",        AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 16000, 1},
   {7, "LPC",         AVMEDIA_TYPE_AUDIO,   CODEC_ID_NONE, 8000, 1},
_______________________________________________
FFmpeg-soc mailing list
FFmpeg-soc@mplayerhq.hu
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc

Reply via email to