[vlc-devel] [PATCH] rtp: JPEG packetization

Samuel Pitoiset samuel.pitoiset at gmail.com
Fri Oct 5 19:17:33 CEST 2012


---
 modules/stream_out/rtpfmt.c | 126 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 125 insertions(+), 1 deletion(-)

diff --git a/modules/stream_out/rtpfmt.c b/modules/stream_out/rtpfmt.c
index 79ae441..e8f0ffa 100644
--- a/modules/stream_out/rtpfmt.c
+++ b/modules/stream_out/rtpfmt.c
@@ -53,6 +53,7 @@ static int rtp_packetize_g726_24 (sout_stream_id_t *, block_t *);
 static int rtp_packetize_g726_32 (sout_stream_id_t *, block_t *);
 static int rtp_packetize_g726_40 (sout_stream_id_t *, block_t *);
 static int rtp_packetize_xiph (sout_stream_id_t *, block_t *);
+static int rtp_packetize_jpeg (sout_stream_id_t *, block_t *);
 
 #define XIPH_IDENT (0)
 
@@ -511,7 +512,11 @@ int rtp_get_fmt( vlc_object_t *obj, es_format_t *p_fmt, const char *mux,
             rtp_fmt->clock_rate = 1000;
             rtp_fmt->pf_packetize = rtp_packetize_t140;
             break;
-
+        case VLC_CODEC_MJPG:
+            rtp_fmt->payload_type = 26;
+            rtp_fmt->ptname = "JPEG";
+            rtp_fmt->pf_packetize = rtp_packetize_jpeg;
+            break;
         default:
             msg_Err( obj, "cannot add this stream (unsupported "
                      "codec: %4.4s)", (char*)&p_fmt->i_codec );
@@ -660,6 +665,125 @@ static int rtp_packetize_xiph( sout_stream_id_t *id, block_t *in )
     return VLC_SUCCESS;
 }
 
+/* rfc2435 */
+static int rtp_packetize_jpeg( sout_stream_id_t *id, block_t *in )
+{
+    uint8_t       *p_data      = in->p_buffer;
+    int           i_data       = in->i_buffer;
+    uint32_t      i_off        = 0; /* fragment offset of the current frame */
+    int           i_nb_qtables = 0;
+    int           i_packet     = 0;
+    const uint8_t *p_qtables   = NULL;
+    uint8_t       w            = 0;
+    uint8_t       h            = 0;
+    int           i;
+
+    /* preparse the header for getting some infos */
+    for( i = 0; i < i_data; i++ )
+    {
+        if( p_data[i] != 0xff )
+            continue;
+
+        if( p_data[i + 1] == 0xdb ) // DQT
+        {
+            /* a quantization table is 64 bytes long */
+            i_nb_qtables = GetWBE( &p_data[i + 2] ) / 65;
+            if( i + 4 + i_nb_qtables * 65 > i_data )
+            {
+                /* too short JPEG header */
+                return VLC_EGENERIC;
+            }
+
+            p_qtables = p_data + i + 4;
+        }
+        else if( p_data[i + 1] == 0xc0 ) // SOF0
+        {
+            if( p_data[i + 14] != 17 || p_data[i + 17] != 17 )
+            {
+                /* only 1x1 chroma blocks are supported */
+                return VLC_EGENERIC;
+            }
+
+            /* convert video pixel dimensions from pixels to blocks */
+            h = GetWBE( &p_data[i + 5] ) >> 3;
+            w = GetWBE( &p_data[i + 7] ) >> 3;
+        }
+        else if( p_data[i + 1] == 0xda ) // SOS
+        {
+            /* SOS is last marker in the header */
+            i += GetWBE( &p_data[i + 2] ) + 2;
+            break;
+        }
+    }
+
+    if( w == 0 || h == 0 )
+    {
+        /* cannot find video pixel dimensions */
+        return VLC_EGENERIC;
+    }
+
+    /* skip JPEG header */
+    p_data += i;
+    i_data -= i;
+
+    for( i = i_data - 2; i >= 0; i-- )
+    {
+        if( p_data[i] == 0xff && p_data[i + 1] == 0xd9 ) // EOI
+        {
+            /* remove the EOI marker */
+            i_data = i;
+            break;
+        }
+    }
+
+    while( i_data > 0 )
+    {
+        int i_hdr_size = 8;
+
+        if( i_off == 0 && i_nb_qtables )
+            i_hdr_size += 4 + 64 * i_nb_qtables;
+
+        /* payload max in one packet */
+        int i_max     = rtp_mtu (id) - i_hdr_size;
+        int i_count   = ( in->i_buffer + i_max - 1 ) / i_max;
+        int i_payload = __MIN( i_max, i_data );
+        block_t *out  = block_Alloc( 12 + i_hdr_size + i_payload );
+
+        /* rtp common header */
+        rtp_packetize_common( id, out, i_data == i_payload, in->i_dts );
+
+        /* Type-specific:8, Fragment Offset:24, Type:8, Q:8, Width:8, Height:8 */
+        uint64_t header = ((uint64_t)0 << 56) | ((uint64_t)i_off << 32) |
+                          (1 << 24) | (255 << 16) | (w << 8) | h;
+        SetQWBE( out->p_buffer + 12, header );
+
+        if( i_off == 0 && i_nb_qtables )
+        {
+            uint32_t header = (0 << 24) | (0 << 16) | 64 * i_nb_qtables;
+            SetDWBE( out->p_buffer + 20, header );
+
+            for( i = 0; i < i_nb_qtables; i++ )
+                memcpy( &out->p_buffer[24 + 64 * i], &p_qtables[65 * i + 1], 64 );
+        }
+
+        /* copy payload data */
+        memcpy( &out->p_buffer[12 + i_hdr_size], p_data, i_payload );
+
+        out->i_buffer = 12 + i_hdr_size + i_payload;
+        out->i_dts    = in->i_dts + i_packet++ * in->i_length / i_count;
+        out->i_length = in->i_length / i_count;
+
+        /* send the RTP/JPEG packet */
+        rtp_packetize_send( id, out );
+
+        p_data += i_payload;
+        i_data -= i_payload;
+        i_off  += i_payload;
+    }
+
+    return VLC_SUCCESS;
+}
+
 static int rtp_packetize_mpa( sout_stream_id_t *id, block_t *in )
 {
     int     i_max   = rtp_mtu (id) - 4; /* payload max in one packet */
-- 
1.7.12.2




More information about the vlc-devel mailing list