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

Samuel Pitoiset samuel.pitoiset at gmail.com
Sat Sep 29 23:41:50 CEST 2012


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

diff --git a/modules/stream_out/rtpfmt.c b/modules/stream_out/rtpfmt.c
index 79ae441..1de82c0 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,116 @@ 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 )
+            {
+                fprintf( stderr, "Too short JPEG header. Aborted!\n" );
+                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 )
+            {
+                fprintf( stderr,
+                         "Only 1x1 chroma blocks are supported. Aborted!\n" );
+                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 )
+    {
+        fprintf( stderr, "Cannot find video pixel dimensions. Aborted!\n" );
+        return VLC_EGENERIC;
+    }
+
+    /* skip JPEG header */
+    p_data += i;
+    i_data -= i;
+
+    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.1




More information about the vlc-devel mailing list