The branch master has been updated
       via  108d7a732e83470a4d6358dd5fcedeaf0d63081b (commit)
      from  958b843d29e54a42c88fe75f58a4f33e12fc1d7e (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit 108d7a732e83470a4d6358dd5fcedeaf0d63081b
Author: Alessandro Molina <[email protected]>
Date:   Fri Feb 20 17:52:38 2009 +0100

    Support for mpeg audio depacketization



Summary of changes:
 rtp/unpack/Makefile.am                      |    2 +-
 rtp/unpack/depacketizer.hpp                 |   11 +-
 rtp/unpack/factory.cpp                      |    5 +
 rtp/unpack/mpegaudio.cpp                    |  302 +++++++++++++++++++++++++++
 rtp/unpack/{mpegvideo.hpp => mpegaudio.hpp} |   18 +-
 rtp/unpack/mpegvideo.cpp                    |   23 ++
 rtp/unpack/mpegvideo.hpp                    |   10 +-
 7 files changed, 350 insertions(+), 21 deletions(-)
 create mode 100644 rtp/unpack/mpegaudio.cpp
 copy rtp/unpack/{mpegvideo.hpp => mpegaudio.hpp} (84%)

diff --git a/rtp/unpack/Makefile.am b/rtp/unpack/Makefile.am
index 0a5cdd9..e502c49 100644
--- a/rtp/unpack/Makefile.am
+++ b/rtp/unpack/Makefile.am
@@ -1,3 +1,3 @@
 noinst_LIBRARIES = librtpunpack.a
 
-librtpunpack_a_SOURCES = factory.cpp  mpegvideo.cpp  depacketizer.cpp 
+librtpunpack_a_SOURCES = factory.cpp  depacketizer.cpp mpegvideo.cpp 
mpegaudio.cpp 
diff --git a/rtp/unpack/depacketizer.hpp b/rtp/unpack/depacketizer.hpp
index c7b5eeb..f9e7aa0 100644
--- a/rtp/unpack/depacketizer.hpp
+++ b/rtp/unpack/depacketizer.hpp
@@ -1,9 +1,9 @@
-/* * 
+/* *
  * This file is part of Flux
  *
  * Copyright (C) 2009 by LScube team <[email protected]>
  * See AUTHORS for more details
- * 
+ *
  * flux 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
@@ -16,7 +16,7 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with flux; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
USA 
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  *
  * */
 
@@ -37,14 +37,15 @@ namespace flux
       public:
         enum ParseResult { Ok = 0,
                            NeedNextFragment = 1,
-                           NotImplemented = -1, PacketEmpty = -2, 
MalformedPacket = -3, LostFirstFragment = -4, OutOfMemory = -5 };
+                           NotImplemented = -1, PacketEmpty = -2, 
MalformedPacket = -3,
+                           LostFirstFragment = -4, OutOfMemory = -5 };
 
         struct ConfigBuffer
         {
             unsigned int len;
             unsigned char *data;
         };
-        
+
         struct FrameBuffer
         {
             long len;
diff --git a/rtp/unpack/factory.cpp b/rtp/unpack/factory.cpp
index b1a8571..59b10c3 100644
--- a/rtp/unpack/factory.cpp
+++ b/rtp/unpack/factory.cpp
@@ -28,6 +28,7 @@
 
 #include "depacketizer.hpp"
 #include "mpegvideo.hpp"
+#include "mpegaudio.hpp"
 
 namespace flux
 {
@@ -40,6 +41,8 @@ namespace flux
         if (!name.empty()) {
             if (name == "MPV")
                 payload_type = 32;
+            else if (name == "MPA")
+                payload_type = 14;
             else {
                 std::cerr << "libRTP Depacketizer Factory, unsupported payload 
name " << name << std::endl;
                 return 0;
@@ -49,6 +52,8 @@ namespace flux
         switch (payload_type) {
             case 32:
                 return new MpegVideoDepacketizer();
+            case 14:
+                return new MpegAudioDepacketizer();
             default:
                 std::cerr << "libRTP Depacketizer Factory, unsupported payload 
type " << payload_type << std::endl;
                 return 0;
diff --git a/rtp/unpack/mpegaudio.cpp b/rtp/unpack/mpegaudio.cpp
new file mode 100644
index 0000000..e197e8b
--- /dev/null
+++ b/rtp/unpack/mpegaudio.cpp
@@ -0,0 +1,302 @@
+/* *
+ * This file is part of Flux
+ *
+ * Copyright (C) 2009 by LScube team <[email protected]>
+ * See AUTHORS for more details
+ *
+ * flux 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.
+ *
+ * flux 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 flux; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * */
+
+#include "config.h"
+
+#include "../packet.hpp"
+#include "mpegaudio.hpp"
+
+#if ENABLE_DEBUG
+#include <cstdio>
+#endif
+
+#include <cstdlib>
+#include <cstring>
+#include <algorithm>
+
+#include <arpa/inet.h>
+
+#define DEFAULT_MPA_DATA_FRAME 1024
+
+#define MPA_IS_SYNC(buff_data) (((buff_data)[0]==0xff) && (((buff_data)[1] & 
0xe0)==0xe0))
+
+#define BITRATEMATRIX { \
+    {0,     0,     0,     0,     0     }, \
+    {32000, 32000, 32000, 32000, 8000  }, \
+    {64000, 48000, 40000, 48000, 16000 }, \
+    {96000, 56000, 48000, 56000, 24000 }, \
+    {128000,64000, 56000, 64000, 32000 }, \
+    {160000,80000, 64000, 80000, 40000 }, \
+    {192000,96000, 80000, 96000, 48000 }, \
+    {224000,112000,96000, 112000,56000 }, \
+    {256000,128000,112000,128000,64000 }, \
+    {288000,160000,128000,144000,80000 }, \
+    {320000,192000,160000,160000,96000 }, \
+    {352000,224000,192000,176000,112000}, \
+    {384000,256000,224000,192000,128000}, \
+    {416000,320000,256000,224000,144000}, \
+    {448000,384000,320000,256000,160000}, \
+    {0,     0,     0,     0,     0     } \
+}
+
+#define SRATEMATRIX { \
+    {44100,    22050,    11025    }, \
+    {48000,    24000,    12000    }, \
+    {32000,    16000,    8000    }, \
+    {-1,    -1,    -1    } \
+}
+
+enum MpegType { MPA_MPEG_2_5 = 0, MPA_MPEG_RES, MPA_MPEG_2, MPA_MPEG_1 };
+enum MpegLayer { MPA_LAYER_RES = 0, MPA_LAYER_III, MPA_LAYER_II, MPA_LAYER_I };
+
+struct mpa_frm
+{
+    int id;
+    int layer;
+    uint32_t bit_rate;
+    float sample_rate;
+    uint32_t frame_size;
+    uint32_t frm_len;
+};
+
+struct rtp_mpa
+{
+    uint8_t *data;
+    long data_size;
+};
+
+struct rtp_mpa_pkt
+{
+    uint32_t mbz:16;
+    uint32_t frag_offset:16;
+    uint8_t data[1];
+};
+
+#define RTP_MPA_PKT(pkt)        ((rtp_mpa_pkt *)(pkt.payload()))
+#define RTP_MPA_DATA_LEN(pkt)    (pkt.getPayloadSize()-4)
+#define RTP_MPA_FRAG_OFFSET(pkt)    (ntohs(RTP_MPA_PKT(pkt)->frag_offset))
+
+static int mpa_sync(uint8_t ** data, size_t * data_len /*, mpa_frm *mpa */ )
+{
+    for (; !MPA_IS_SYNC(*data) && (*data_len >= 4); (*data)++, (*data_len)--) {
+#if ENABLE_DEBUG
+        fprintf(stderr, "[MPA] sync: %X%X%X%X\n", data[0], data[1], data[2], 
data[3]);
+#endif
+    }
+
+    return (*data_len >= 4) ? 0 /*sync found */ : 1 /*sync not found */ ;
+}
+
+#if ENABLE_DEBUG
+// debug function to diplay MPA header information
+static void mpa_info(mpa_frm * mpa)
+{
+    switch (mpa->id) {
+    case MPA_MPEG_1:
+        fprintf(stderr, "[MPA] MPEG1\n");
+        break;
+    case MPA_MPEG_2:
+        fprintf(stderr, "[MPA] MPEG2\n");
+        break;
+    case MPA_MPEG_2_5:
+        fprintf(stderr, "[MPA] MPEG2.5\n");
+        break;
+    default:
+        fprintf(stderr, "[MPA] MPEG reserved (bad)\n");
+        return;
+        break;
+    }
+    switch (mpa->layer) {
+    case MPA_LAYER_I:
+        fprintf(stderr, "[MPA] Layer I\n");
+        break;
+    case MPA_LAYER_II:
+        fprintf(stderr, "[MPA] Layer II\n");
+        break;
+    case MPA_LAYER_III:
+        fprintf(stderr, "[MPA] Layer III\n");
+        break;
+    default:
+        fprintf(stderr, "[MPA] Layer reserved (bad)\n");
+        return;
+        break;
+    }
+    fprintf(stderr, "[MPA] bitrate: %d; sample rate: %3.0f; pkt_len: %d\n",
+           mpa->bit_rate, mpa->sample_rate, mpa->frm_len);
+}
+#endif
+
+static int mpa_decode_header(uint8_t * buff_data, mpa_frm * mpa)
+{
+    int RowIndex, ColIndex;
+    int padding;
+    int BitrateMatrix[16][5] = BITRATEMATRIX;
+    float SRateMatrix[4][3] = SRATEMATRIX;
+
+    if (!MPA_IS_SYNC(buff_data))
+        return flux::rtp::Depacketizer::MalformedPacket;  /*syncword not found 
*/
+
+    mpa->id = (buff_data[1] & 0x18) >> 3;
+    mpa->layer = (buff_data[1] & 0x06) >> 1;
+
+    switch (mpa->id << 2 | mpa->layer) {
+    case MPA_MPEG_1 << 2 | MPA_LAYER_I:    // MPEG 1 layer I
+        ColIndex = 0;
+        break;
+    case MPA_MPEG_1 << 2 | MPA_LAYER_II:    // MPEG 1 layer II
+        ColIndex = 1;
+        break;
+    case MPA_MPEG_1 << 2 | MPA_LAYER_III:    // MPEG 1 layer III
+        ColIndex = 2;
+        break;
+    case MPA_MPEG_2 << 2 | MPA_LAYER_I:    // MPEG 2 layer I
+    case MPA_MPEG_2_5 << 2 | MPA_LAYER_I:    // MPEG 2.5 layer I
+        ColIndex = 3;
+        break;
+    case MPA_MPEG_2 << 2 | MPA_LAYER_II:    // MPEG 2 layer II
+    case MPA_MPEG_2 << 2 | MPA_LAYER_III:    // MPEG 2 layer III
+    case MPA_MPEG_2_5 << 2 | MPA_LAYER_II:    // MPEG 2.5 layer II
+    case MPA_MPEG_2_5 << 2 | MPA_LAYER_III:    // MPEG 2.5 layer III
+        ColIndex = 4;
+        break;
+    default:
+        return flux::rtp::Depacketizer::MalformedPacket;
+        break;
+    }
+
+    RowIndex = (buff_data[2] & 0xf0) >> 4;
+    if (RowIndex == 0xF)    // bad bitrate
+        return flux::rtp::Depacketizer::MalformedPacket;
+    if (RowIndex == 0)    // free bitrate: not supported
+        return flux::rtp::Depacketizer::MalformedPacket;
+
+    mpa->bit_rate = BitrateMatrix[RowIndex][ColIndex];
+
+    switch (mpa->id) {
+    case MPA_MPEG_1:
+        ColIndex = 0;
+        break;
+    case MPA_MPEG_2:
+        ColIndex = 1;
+        break;
+    case MPA_MPEG_2_5:
+        ColIndex = 2;
+        break;
+    default:
+        return flux::rtp::Depacketizer::MalformedPacket;
+        break;
+    }
+
+    RowIndex = (buff_data[2] & 0x0c) >> 2;
+    if (RowIndex == 3)    // reserved
+        return flux::rtp::Depacketizer::MalformedPacket;
+
+    mpa->sample_rate = SRateMatrix[RowIndex][ColIndex];
+
+    // padding
+    padding = (buff_data[2] & 0x02) >> 1;
+
+    // packet len
+    if (mpa->layer == MPA_LAYER_I) {    // layer 1
+        mpa->frame_size = 384;
+        mpa->frm_len = ((12 * mpa->bit_rate) / mpa->sample_rate + padding) * 4;
+    } else {        // layer 2 or 3
+        mpa->frame_size = 1152;
+        mpa->frm_len = 144 * mpa->bit_rate / mpa->sample_rate + padding;
+    }
+
+#if ENABLE_DEBUG
+    mpa_info(mpa);
+#endif
+
+    return 0;
+}
+
+
+namespace flux
+{
+    namespace rtp
+    {
+        MpegAudioDepacketizer::MpegAudioDepacketizer() :
+            Depacketizer(14, "MPA"), data(0), len(0), data_size(0)
+        {
+        }
+
+        MpegAudioDepacketizer::~MpegAudioDepacketizer()
+        {
+            if (data)
+                free(data);
+        }
+
+        Depacketizer::ParseResult MpegAudioDepacketizer::parse(Packet const 
&pkt,
+                                                               
Depacketizer::FrameBuffer *fr,
+                                                               
Depacketizer::ConfigBuffer *config)
+        {
+            mpa_frm mpa;
+            uint8_t *mpa_data;
+            size_t mpa_data_len;
+
+            if (!pkt.getPayloadSize())
+                return Depacketizer::PacketEmpty;
+
+            mpa_data = RTP_MPA_PKT(pkt)->data;
+            mpa_data_len = RTP_MPA_DATA_LEN(pkt); // now pkt_len is only the 
payload len (excluded mpa subheader)
+
+#if ENABLE_DEBUG
+            fprintf(stderr, "--- fr->len: %d of %d\n", pkt_len, 
pkt.getPayloadSize());
+#endif
+
+            if (mpa_sync(&mpa_data, &mpa_data_len))
+                return Depacketizer::MalformedPacket;
+
+            if (mpa_decode_header(mpa_data, &mpa))
+                return Depacketizer::MalformedPacket;
+
+            // init private struct if this is the first time we're called
+            if ((!data) || (mpa.frm_len > data_size)) {
+                data_size = mpa.frm_len;
+                data = static_cast<unsigned char*>(realloc(data, data_size));
+
+                if (!data) {
+                    data_size = 0;
+                    return Depacketizer::OutOfMemory;
+                }
+            }
+
+            if (RTP_MPA_FRAG_OFFSET(pkt) == 0)
+                len = 0;
+
+            len += mpa_data_len;
+            memcpy(data + RTP_MPA_FRAG_OFFSET(pkt), mpa_data, mpa_data_len);
+
+            if (len < mpa.frm_len) {
+                return Depacketizer::NeedNextFragment;
+            }
+            else {
+                fr->timestamp = pkt.getTimestamp();
+                fr->data = data;
+                fr->len = len;
+                return Depacketizer::Ok;
+            }
+        }
+    }
+}
diff --git a/rtp/unpack/mpegvideo.hpp b/rtp/unpack/mpegaudio.hpp
similarity index 84%
copy from rtp/unpack/mpegvideo.hpp
copy to rtp/unpack/mpegaudio.hpp
index a046cec..b2fe0f9 100644
--- a/rtp/unpack/mpegvideo.hpp
+++ b/rtp/unpack/mpegaudio.hpp
@@ -1,9 +1,9 @@
-/* * 
+/* *
  * This file is part of Flux
  *
  * Copyright (C) 2009 by LScube team <[email protected]>
  * See AUTHORS for more details
- * 
+ *
  * flux 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
@@ -16,13 +16,12 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with flux; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
USA 
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  *
  * */
 
-
-#ifndef _LIBRTP_MPEG_VIDEO_
-#define _LIBRTP_MPEG_VIDEO_
+#ifndef _LIBRTP_MPEG_AUDIO_UNPACK_
+#define _LIBRTP_MPEG_VIDEO_UNPACK_
 
 #include "depacketizer.hpp"
 
@@ -30,17 +29,16 @@ namespace flux
 {
   namespace rtp
   {
-    class MpegVideoDepacketizer : public Depacketizer
+    class MpegAudioDepacketizer : public Depacketizer
     {
       protected:
         unsigned char *data;
         unsigned long len;
         unsigned long data_size;
-        unsigned long timestamp;
 
       public:
-        MpegVideoDepacketizer();
-        ~MpegVideoDepacketizer();
+        MpegAudioDepacketizer();
+        ~MpegAudioDepacketizer();
         Depacketizer::ParseResult parse(Packet const &packet, 
Depacketizer::FrameBuffer *destination_frame_buffer,
                                         Depacketizer::ConfigBuffer 
*destination_config_buffer);
     };
diff --git a/rtp/unpack/mpegvideo.cpp b/rtp/unpack/mpegvideo.cpp
index d6c74a1..4e375a7 100644
--- a/rtp/unpack/mpegvideo.cpp
+++ b/rtp/unpack/mpegvideo.cpp
@@ -1,3 +1,25 @@
+/* *
+ * This file is part of Flux
+ *
+ * Copyright (C) 2009 by LScube team <[email protected]>
+ * See AUTHORS for more details
+ *
+ * flux 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.
+ *
+ * flux 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 flux; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * */
+
 #include "config.h"
 
 #if ENABLE_DEBUG
@@ -120,6 +142,7 @@ namespace flux
         } else {
             fr->data = this->data;
             fr->len  = this->len;
+            fr->timestamp = this->timestamp;
             this->len = 0;
         }
 
diff --git a/rtp/unpack/mpegvideo.hpp b/rtp/unpack/mpegvideo.hpp
index a046cec..fc7f285 100644
--- a/rtp/unpack/mpegvideo.hpp
+++ b/rtp/unpack/mpegvideo.hpp
@@ -1,9 +1,9 @@
-/* * 
+/* *
  * This file is part of Flux
  *
  * Copyright (C) 2009 by LScube team <[email protected]>
  * See AUTHORS for more details
- * 
+ *
  * flux 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
@@ -16,13 +16,13 @@
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with flux; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
USA 
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  *
  * */
 
 
-#ifndef _LIBRTP_MPEG_VIDEO_
-#define _LIBRTP_MPEG_VIDEO_
+#ifndef _LIBRTP_MPEG_VIDEO_UNPACK_
+#define _LIBRTP_MPEG_VIDEO_UNPACK_
 
 #include "depacketizer.hpp"
 


hooks/post-receive
--
RTP multiplexer
_______________________________________________
Lscube-commits mailing list
[email protected]
http://lscube.org/mailman/listinfo/lscube-commits

Reply via email to