]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtpdec_xiph.c
Make RTPFirstDynamicPayloadHandler static to rtpdec.c
[ffmpeg] / libavformat / rtpdec_xiph.c
index 11d3c92abfdb5ed95079ec14edc190bf190e3717..fe17a42ba4e17cd67440d917f55ea1b7ebeaddd6 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * RTP Theora Protocol
+ * Xiph RTP Protocols
+ * Copyright (c) 2009 Colin McQuillian
  * Copyright (c) 2010 Josh Allmann
  *
  * This file is part of FFmpeg.
@@ -20,8 +21,9 @@
  */
 
 /**
- * @file libavformat/rtpdec_theora.c
- * @brief Theora / RTP Code
+ * @file
+ * @brief Xiph / RTP Code
+ * @author Colin McQuillan <m.niloc@gmail.com>
  * @author Josh Allmann <joshua.allmann@gmail.com>
  */
 
 #include <assert.h>
 
 #include "rtpdec.h"
-#include "rtpdec_xiph.h"
+#include "rtpdec_formats.h"
 
 /**
- * RTP/Theora specific private data.
+ * RTP/Xiph specific private data.
  */
 struct PayloadContext {
     unsigned ident;             ///< 24-bit stream configuration identifier
     uint32_t timestamp;
     ByteIOContext* fragment;    ///< buffer for split payloads
+    uint8_t *split_buf;
+    int split_pos, split_buf_len, split_buf_size;
+    int split_pkts;
 };
 
-static PayloadContext *theora_new_context(void)
+static PayloadContext *xiph_new_context(void)
 {
     return av_mallocz(sizeof(PayloadContext));
 }
@@ -58,32 +63,56 @@ static inline void free_fragment_if_needed(PayloadContext * data)
     }
 }
 
-static void theora_free_context(PayloadContext * data)
+static void xiph_free_context(PayloadContext * data)
 {
     free_fragment_if_needed(data);
+    av_free(data->split_buf);
     av_free(data);
 }
 
-static int theora_handle_packet(AVFormatContext * ctx,
-                                PayloadContext * data,
-                                AVStream * st,
-                                AVPacket * pkt,
-                                uint32_t * timestamp,
-                                const uint8_t * buf, int len, int flags)
+static int xiph_handle_packet(AVFormatContext * ctx,
+                              PayloadContext * data,
+                              AVStream * st,
+                              AVPacket * pkt,
+                              uint32_t * timestamp,
+                              const uint8_t * buf, int len, int flags)
 {
 
     int ident, fragmented, tdt, num_pkts, pkt_len;
 
+    if (!buf) {
+        if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
+            data->split_pkts <= 0) {
+            av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
+            return AVERROR_INVALIDDATA;
+        }
+        pkt_len = AV_RB16(data->split_buf + data->split_pos);
+        data->split_pos += 2;
+        if (data->split_pos + pkt_len > data->split_buf_len) {
+            av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
+            return AVERROR_INVALIDDATA;
+        }
+        if (av_new_packet(pkt, pkt_len)) {
+            av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
+            return AVERROR(ENOMEM);
+        }
+        pkt->stream_index = st->index;
+        memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
+        data->split_pos += pkt_len;
+        data->split_pkts--;
+        return data->split_pkts > 0;
+    }
+
     if (len < 6) {
         av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
         return AVERROR_INVALIDDATA;
     }
 
-    // read theora rtp headers
+    // read xiph rtp headers
     ident       = AV_RB24(buf);
     fragmented  = buf[3] >> 6;
     tdt         = (buf[3] >> 4) & 3;
-    num_pkts    = buf[3] & 7;
+    num_pkts    = buf[3] & 0xf;
     pkt_len     = AV_RB16(buf + 4);
 
     if (pkt_len > len - 6) {
@@ -95,13 +124,13 @@ static int theora_handle_packet(AVFormatContext * ctx,
 
     if (ident != data->ident) {
         av_log(ctx, AV_LOG_ERROR,
-               "Unimplemented Theora SDP configuration change detected\n");
+               "Unimplemented Xiph SDP configuration change detected\n");
         return AVERROR_PATCHWELCOME;
     }
 
     if (tdt) {
         av_log(ctx, AV_LOG_ERROR,
-               "Unimplemented RTP Theora packet settings (%d,%d,%d)\n",
+               "Unimplemented RTP Xiph packet settings (%d,%d,%d)\n",
                fragmented, tdt, num_pkts);
         return AVERROR_PATCHWELCOME;
     }
@@ -110,46 +139,38 @@ static int theora_handle_packet(AVFormatContext * ctx,
     len -= 6;
 
     if (fragmented == 0) {
-        // whole frame(s)
-        int i, data_len, write_len;
-        buf -= 2;
-        len += 2;
-
-        // fast first pass to calculate total length
-        for (i = 0, data_len = 0;  (i < num_pkts) && (len >= 2);  i++) {
-            int off   = data_len + (i << 1);
-            pkt_len   = AV_RB16(buf + off);
-            data_len += pkt_len;
-            len      -= pkt_len + 2;
-        }
-
-        if (len < 0 || i < num_pkts) {
-            av_log(ctx, AV_LOG_ERROR,
-                   "Bad packet: %d bytes left at frame %d of %d\n",
-                   len, i, num_pkts);
-            return AVERROR_INVALIDDATA;
-        }
-
-        if (av_new_packet(pkt, data_len)) {
+        if (av_new_packet(pkt, pkt_len)) {
             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
-            return AVERROR_NOMEM;
+            return AVERROR(ENOMEM);
         }
         pkt->stream_index = st->index;
-
-        // concatenate frames
-        for (i = 0, write_len = 0; write_len < data_len; i++) {
-            pkt_len = AV_RB16(buf);
-            buf += 2;
-            memcpy(pkt->data + write_len, buf, pkt_len);
-            write_len += pkt_len;
-            buf += pkt_len;
+        memcpy(pkt->data, buf, pkt_len);
+        buf += pkt_len;
+        len -= pkt_len;
+        num_pkts--;
+
+        if (num_pkts > 0) {
+            if (len > data->split_buf_size || !data->split_buf) {
+                av_freep(&data->split_buf);
+                data->split_buf_size = 2 * len;
+                data->split_buf = av_malloc(data->split_buf_size);
+                if (!data->split_buf) {
+                    av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
+                    av_free_packet(pkt);
+                    return AVERROR(ENOMEM);
+                }
+            }
+            memcpy(data->split_buf, buf, len);
+            data->split_buf_len = len;
+            data->split_pos = 0;
+            data->split_pkts = num_pkts;
+            return 1;
         }
-        assert(write_len == data_len);
 
         return 0;
 
     } else if (fragmented == 1) {
-        // start of theora data fragment
+        // start of xiph data fragment
         int res;
 
         // end packet has been lost somewhere, so drop buffered data
@@ -170,30 +191,29 @@ static int theora_handle_packet(AVFormatContext * ctx,
             av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
             return AVERROR_INVALIDDATA;
         }
+        if (!data->fragment) {
+            av_log(ctx, AV_LOG_WARNING,
+                   "Received packet without a start fragment; dropping.\n");
+            return AVERROR(EAGAIN);
+        }
 
         // copy data to fragment buffer
         put_buffer(data->fragment, buf, pkt_len);
 
         if (fragmented == 3) {
-            // end of theora data packet
-            uint8_t* theora_data;
-            int frame_size = url_close_dyn_buf(data->fragment, &theora_data);
+            // end of xiph data packet
+            av_init_packet(pkt);
+            pkt->size = url_close_dyn_buf(data->fragment, &pkt->data);
 
-            if (frame_size < 0) {
+            if (pkt->size < 0) {
                 av_log(ctx, AV_LOG_ERROR,
                        "Error occurred when getting fragment buffer.");
-                return frame_size;
-            }
-
-            if (av_new_packet(pkt, frame_size)) {
-                av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
-                return AVERROR_NOMEM;
+                return pkt->size;
             }
 
-            memcpy(pkt->data, theora_data, frame_size);
             pkt->stream_index = st->index;
+            pkt->destruct = av_destruct_packet;
 
-            av_free(theora_data);
             data->fragment = NULL;
 
             return 0;
@@ -226,7 +246,7 @@ static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
 static unsigned int
 parse_packed_headers(const uint8_t * packed_headers,
                      const uint8_t * packed_headers_end,
-                     AVCodecContext * codec, PayloadContext * theora_data)
+                     AVCodecContext * codec, PayloadContext * xiph_data)
 {
 
     unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
@@ -234,13 +254,13 @@ parse_packed_headers(const uint8_t * packed_headers,
 
     if (packed_headers_end - packed_headers < 9) {
         av_log(codec, AV_LOG_ERROR,
-               "Invalid %d byte packed header.",
+               "Invalid %td byte packed header.",
                packed_headers_end - packed_headers);
         return AVERROR_INVALIDDATA;
     }
 
     num_packed         = bytestream_get_be32(&packed_headers);
-    theora_data->ident = bytestream_get_be24(&packed_headers);
+    xiph_data->ident   = bytestream_get_be24(&packed_headers);
     length             = bytestream_get_be16(&packed_headers);
     num_headers        = get_base128(&packed_headers, packed_headers_end);
     length1            = get_base128(&packed_headers, packed_headers_end);
@@ -256,7 +276,7 @@ parse_packed_headers(const uint8_t * packed_headers,
     if (packed_headers_end - packed_headers != length ||
         length1 > length || length2 > length - length1) {
         av_log(codec, AV_LOG_ERROR,
-               "Bad packed header lengths (%d,%d,%d,%d)\n", length1,
+               "Bad packed header lengths (%d,%d,%td,%d)\n", length1,
                length2, packed_headers_end - packed_headers, length);
         return AVERROR_INVALIDDATA;
     }
@@ -270,7 +290,7 @@ parse_packed_headers(const uint8_t * packed_headers,
     ptr = codec->extradata = av_malloc(extradata_alloc);
     if (!ptr) {
         av_log(codec, AV_LOG_ERROR, "Out of memory\n");
-        return AVERROR_NOMEM;
+        return AVERROR(ENOMEM);
     }
     *ptr++ = 2;
     ptr += av_xiphlacing(ptr, length1);
@@ -284,14 +304,25 @@ parse_packed_headers(const uint8_t * packed_headers,
     return 0;
 }
 
-static int theora_parse_fmtp_pair(AVCodecContext * codec,
-                                  PayloadContext *theora_data,
-                                  char *attr, char *value)
+static int xiph_parse_fmtp_pair(AVStream* stream,
+                                PayloadContext *xiph_data,
+                                char *attr, char *value)
 {
+    AVCodecContext *codec = stream->codec;
     int result = 0;
 
     if (!strcmp(attr, "sampling")) {
-        return AVERROR_PATCHWELCOME;
+        if (!strcmp(value, "YCbCr-4:2:0")) {
+            codec->pix_fmt = PIX_FMT_YUV420P;
+        } else if (!strcmp(value, "YCbCr-4:4:2")) {
+            codec->pix_fmt = PIX_FMT_YUV422P;
+        } else if (!strcmp(value, "YCbCr-4:4:4")) {
+            codec->pix_fmt = PIX_FMT_YUV444P;
+        } else {
+            av_log(codec, AV_LOG_ERROR,
+                   "Unsupported pixel format %s\n", attr);
+            return AVERROR_INVALIDDATA;
+        }
     } else if (!strcmp(attr, "width")) {
         /* This is an integer between 1 and 1048561
          * and MUST be in multiples of 16. */
@@ -325,11 +356,11 @@ static int theora_parse_fmtp_pair(AVCodecContext * codec,
 
                 result = parse_packed_headers
                     (decoded_packet, decoded_packet + packet_size, codec,
-                    theora_data);
+                    xiph_data);
             } else {
                 av_log(codec, AV_LOG_ERROR,
                        "Out of memory while decoding SDP configuration.\n");
-                result = AVERROR_NOMEM;
+                result = AVERROR(ENOMEM);
             }
         } else {
             av_log(codec, AV_LOG_ERROR, "Packet too large\n");
@@ -340,39 +371,16 @@ static int theora_parse_fmtp_pair(AVCodecContext * codec,
     return result;
 }
 
-static int theora_parse_sdp_line(AVFormatContext *s, int st_index,
+static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
                                  PayloadContext *data, const char *line)
 {
     const char *p;
-    char *value;
-    char attr[25];
-    int value_size = strlen(line), attr_size = sizeof(attr), res = 0;
-    AVCodecContext* codec = s->streams[st_index]->codec;
-
-    assert(codec->id == CODEC_ID_THEORA);
-    assert(data);
-
-    if (!(value = av_malloc(value_size))) {
-        av_log(codec, AV_LOG_ERROR, "Out of memory\n");
-        return AVERROR_NOMEM;
-    }
 
     if (av_strstart(line, "fmtp:", &p)) {
-        // remove protocol identifier
-        while (*p && *p == ' ') p++; // strip spaces
-        while (*p && *p != ' ') p++; // eat protocol identifier
-        while (*p && *p == ' ') p++; // strip trailing spaces
-
-        while (ff_rtsp_next_attr_and_value(&p,
-                                           attr, attr_size,
-                                           value, value_size)) {
-            res = theora_parse_fmtp_pair(codec, data, attr, value);
-            if (res < 0 && res != AVERROR_PATCHWELCOME)
-                return res;
-        }
+        return ff_parse_fmtp(s->streams[st_index], data, p,
+                             xiph_parse_fmtp_pair);
     }
 
-    av_free(value);
     return 0;
 }
 
@@ -380,8 +388,18 @@ RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
     .enc_name         = "theora",
     .codec_type       = AVMEDIA_TYPE_VIDEO,
     .codec_id         = CODEC_ID_THEORA,
-    .parse_sdp_a_line = theora_parse_sdp_line,
-    .open             = theora_new_context,
-    .close            = theora_free_context,
-    .parse_packet     = theora_handle_packet
+    .parse_sdp_a_line = xiph_parse_sdp_line,
+    .open             = xiph_new_context,
+    .close            = xiph_free_context,
+    .parse_packet     = xiph_handle_packet
+};
+
+RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
+    .enc_name         = "vorbis",
+    .codec_type       = AVMEDIA_TYPE_AUDIO,
+    .codec_id         = CODEC_ID_VORBIS,
+    .parse_sdp_a_line = xiph_parse_sdp_line,
+    .open             = xiph_new_context,
+    .close            = xiph_free_context,
+    .parse_packet     = xiph_handle_packet
 };