]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtpdec_xiph.c
mp3: Properly use AVCodecContext API
[ffmpeg] / libavformat / rtpdec_xiph.c
index 4c9cad287774e1013c4ed2218d6527d9d4c41880..66e619149b2e4592ca1dc0d099de7c97725d6ec1 100644 (file)
@@ -3,20 +3,20 @@
  * Copyright (c) 2009 Colin McQuillian
  * Copyright (c) 2010 Josh Allmann
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav 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 FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
  * @author Josh Allmann <joshua.allmann@gmail.com>
  */
 
+#include "libavutil/attributes.h"
 #include "libavutil/avstring.h"
 #include "libavutil/base64.h"
 #include "libavcodec/bytestream.h"
 
 #include <assert.h>
 
+#include "avio_internal.h"
 #include "rtpdec.h"
-#include "rtpdec_xiph.h"
+#include "rtpdec_formats.h"
 
 /**
  * RTP/Xiph specific private data.
 struct PayloadContext {
     unsigned ident;             ///< 24-bit stream configuration identifier
     uint32_t timestamp;
-    ByteIOContext* fragment;    ///< buffer for split payloads
+    AVIOContext* fragment;    ///< buffer for split payloads
+    uint8_t *split_buf;
+    int split_pos, split_buf_len, split_buf_size;
+    int split_pkts;
 };
 
-static PayloadContext *xiph_new_context(void)
+static void xiph_close_context(PayloadContext * data)
 {
-    return av_mallocz(sizeof(PayloadContext));
+    ffio_free_dyn_buf(&data->fragment);
+    av_free(data->split_buf);
 }
 
-static inline void free_fragment_if_needed(PayloadContext * data)
-{
-    if (data->fragment) {
-        uint8_t* p;
-        url_close_dyn_buf(data->fragment, &p);
-        av_free(p);
-        data->fragment = NULL;
-    }
-}
 
-static void xiph_free_context(PayloadContext * data)
-{
-    free_fragment_if_needed(data);
-    av_free(data);
-}
-
-static int xiph_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, uint16_t seq,
+                              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;
@@ -85,7 +97,7 @@ static int xiph_handle_packet(AVFormatContext * ctx,
     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) {
@@ -112,41 +124,33 @@ static int xiph_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(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;
 
@@ -155,12 +159,12 @@ static int xiph_handle_packet(AVFormatContext * ctx,
         int res;
 
         // end packet has been lost somewhere, so drop buffered data
-        free_fragment_if_needed(data);
+        ffio_free_dyn_buf(&data->fragment);
 
-        if((res = url_open_dyn_buf(&data->fragment)) < 0)
+        if((res = avio_open_dyn_buf(&data->fragment)) < 0)
             return res;
 
-        put_buffer(data->fragment, buf, pkt_len);
+        avio_write(data->fragment, buf, pkt_len);
         data->timestamp = *timestamp;
 
     } else {
@@ -168,30 +172,28 @@ static int xiph_handle_packet(AVFormatContext * ctx,
         if (data->timestamp != *timestamp) {
             // skip if fragmented timestamp is incorrect;
             // a start packet has been lost somewhere
-            free_fragment_if_needed(data);
+            ffio_free_dyn_buf(&data->fragment);
             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);
+        avio_write(data->fragment, buf, pkt_len);
 
         if (fragmented == 3) {
             // end of xiph data packet
-            av_init_packet(pkt);
-            pkt->size = url_close_dyn_buf(data->fragment, &pkt->data);
-
-            if (pkt->size < 0) {
+            int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
+            if (ret < 0) {
                 av_log(ctx, AV_LOG_ERROR,
                        "Error occurred when getting fragment buffer.");
-                return pkt->size;
+                return ret;
             }
 
-            pkt->stream_index = st->index;
-            pkt->destruct = av_destruct_packet;
-
-            data->fragment = NULL;
-
             return 0;
         }
     }
@@ -219,7 +221,7 @@ static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
 /**
  * Based off parse_packed_headers in Vorbis RTP
  */
-static unsigned int
+static int
 parse_packed_headers(const uint8_t * packed_headers,
                      const uint8_t * packed_headers_end,
                      AVCodecContext * codec, PayloadContext * xiph_data)
@@ -280,15 +282,26 @@ parse_packed_headers(const uint8_t * packed_headers,
     return 0;
 }
 
-static int xiph_parse_fmtp_pair(AVStream* stream,
+static int xiph_parse_fmtp_pair(AVFormatContext *s,
+                                AVStream* stream,
                                 PayloadContext *xiph_data,
-                                char *attr, char *value)
+                                const char *attr, const 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 = AV_PIX_FMT_YUV420P;
+        } else if (!strcmp(value, "YCbCr-4:4:2")) {
+            codec->pix_fmt = AV_PIX_FMT_YUV422P;
+        } else if (!strcmp(value, "YCbCr-4:4:4")) {
+            codec->pix_fmt = AV_PIX_FMT_YUV444P;
+        } else {
+            av_log(s, 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. */
@@ -324,12 +337,12 @@ static int xiph_parse_fmtp_pair(AVStream* stream,
                     (decoded_packet, decoded_packet + packet_size, codec,
                     xiph_data);
             } else {
-                av_log(codec, AV_LOG_ERROR,
+                av_log(s, AV_LOG_ERROR,
                        "Out of memory while decoding SDP configuration.\n");
                 result = AVERROR(ENOMEM);
             }
         } else {
-            av_log(codec, AV_LOG_ERROR, "Packet too large\n");
+            av_log(s, AV_LOG_ERROR, "Packet too large\n");
             result = AVERROR_INVALIDDATA;
         }
         av_free(decoded_packet);
@@ -338,12 +351,15 @@ static int xiph_parse_fmtp_pair(AVStream* stream,
 }
 
 static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
-                                 PayloadContext *data, const char *line)
+                               PayloadContext *data, const char *line)
 {
     const char *p;
 
+    if (st_index < 0)
+        return 0;
+
     if (av_strstart(line, "fmtp:", &p)) {
-        return ff_parse_fmtp(s->streams[st_index], data, p,
+        return ff_parse_fmtp(s, s->streams[st_index], data, p,
                              xiph_parse_fmtp_pair);
     }
 
@@ -353,19 +369,20 @@ static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
 RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
     .enc_name         = "theora",
     .codec_type       = AVMEDIA_TYPE_VIDEO,
-    .codec_id         = CODEC_ID_THEORA,
+    .codec_id         = AV_CODEC_ID_THEORA,
+    .priv_data_size   = sizeof(PayloadContext),
     .parse_sdp_a_line = xiph_parse_sdp_line,
-    .open             = xiph_new_context,
-    .close            = xiph_free_context,
-    .parse_packet     = xiph_handle_packet
+    .close            = xiph_close_context,
+    .parse_packet     = xiph_handle_packet,
 };
 
 RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
     .enc_name         = "vorbis",
     .codec_type       = AVMEDIA_TYPE_AUDIO,
-    .codec_id         = CODEC_ID_VORBIS,
+    .codec_id         = AV_CODEC_ID_VORBIS,
+    .need_parsing     = AVSTREAM_PARSE_HEADERS,
+    .priv_data_size   = sizeof(PayloadContext),
     .parse_sdp_a_line = xiph_parse_sdp_line,
-    .open             = xiph_new_context,
-    .close            = xiph_free_context,
-    .parse_packet     = xiph_handle_packet
+    .close            = xiph_close_context,
+    .parse_packet     = xiph_handle_packet,
 };