]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blobdiff - libavformat/rdt.c
cosmetics, alignment
[frescor/ffmpeg.git] / libavformat / rdt.c
index 9561859678dc29c5812e9f10bf504c15e1e385c5..7c66242220f09eb24c4a5a6a286f2c5a3c894990 100644 (file)
  */
 
 /**
- * @file rdt.c
+ * @file libavformat/rdt.c
  * @brief Realmedia RTSP protocol (RDT) support
  * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
  */
 
 #include "avformat.h"
 #include "libavutil/avstring.h"
-#include "rtp_internal.h"
+#include "rtpdec.h"
 #include "rdt.h"
 #include "libavutil/base64.h"
 #include "libavutil/md5.h"
 #include "rm.h"
 #include "internal.h"
+#include "libavcodec/get_bits.h"
 
 struct RDTDemuxContext {
-    AVFormatContext *ic;
-    AVStream *st;
+    AVFormatContext *ic; /**< the containing (RTSP) demux context */
+    /** Each RDT stream-set (represented by one RTSPStream) can contain
+     * multiple streams (of the same content, but with possibly different
+     * codecs/bitrates). Each such stream is represented by one AVStream
+     * in the AVFormatContext, and this variable points to the offset in
+     * that array such that the first is the first stream of this set. */
+    AVStream **streams;
+    int n_streams; /**< streams with identifical content in this set */
     void *dynamic_protocol_context;
     DynamicPayloadPacketHandlerProc parse_packet;
-    uint32_t prev_set_id, prev_timestamp;
+    uint32_t prev_timestamp;
+    int prev_set_id, prev_stream_id;
 };
 
 RDTDemuxContext *
-ff_rdt_parse_open(AVFormatContext *ic, AVStream *st,
+ff_rdt_parse_open(AVFormatContext *ic, int first_stream_of_set_idx,
                   void *priv_data, RTPDynamicProtocolHandler *handler)
 {
     RDTDemuxContext *s = av_mallocz(sizeof(RDTDemuxContext));
@@ -51,8 +59,13 @@ ff_rdt_parse_open(AVFormatContext *ic, AVStream *st,
         return NULL;
 
     s->ic = ic;
-    s->st = st;
+    s->streams = &ic->streams[first_stream_of_set_idx];
+    do {
+        s->n_streams++;
+    } while (first_stream_of_set_idx + s->n_streams < ic->nb_streams &&
+             s->streams[s->n_streams]->priv_data == s->streams[0]->priv_data);
     s->prev_set_id    = -1;
+    s->prev_stream_id = -1;
     s->prev_timestamp = -1;
     s->parse_packet = handler->parse_packet;
     s->dynamic_protocol_context = priv_data;
@@ -63,14 +76,21 @@ ff_rdt_parse_open(AVFormatContext *ic, AVStream *st,
 void
 ff_rdt_parse_close(RDTDemuxContext *s)
 {
+    int i;
+
+    for (i = 1; i < s->n_streams; i++)
+        s->streams[i]->priv_data = NULL;
+
     av_free(s);
 }
 
 struct PayloadContext {
     AVFormatContext *rmctx;
+    RMStream *rmst[MAX_STREAMS];
     uint8_t *mlti_data;
     unsigned int mlti_data_size;
     char buffer[RTP_MAX_PACKET_LENGTH + FF_INPUT_BUFFER_PADDING_SIZE];
+    int audio_pkt_cnt; /**< remaining audio packets in rmdec */
 };
 
 void
@@ -115,7 +135,7 @@ ff_rdt_calc_response_and_checksum(char response[41], char chksum[9],
 static int
 rdt_load_mdpr (PayloadContext *rdt, AVStream *st, int rule_nr)
 {
-    ByteIOContext *pb;
+    ByteIOContext pb;
     int size;
     uint32_t tag;
 
@@ -135,34 +155,34 @@ rdt_load_mdpr (PayloadContext *rdt, AVStream *st, int rule_nr)
      */
     if (!rdt->mlti_data)
         return -1;
-    url_open_buf(&pb, rdt->mlti_data, rdt->mlti_data_size, URL_RDONLY);
-    tag = get_le32(pb);
+    init_put_byte(&pb, rdt->mlti_data, rdt->mlti_data_size, 0,
+                  NULL, NULL, NULL, NULL);
+    tag = get_le32(&pb);
     if (tag == MKTAG('M', 'L', 'T', 'I')) {
         int num, chunk_nr;
 
         /* read index of MDPR chunk numbers */
-        num = get_be16(pb);
+        num = get_be16(&pb);
         if (rule_nr < 0 || rule_nr >= num)
             return -1;
-        url_fskip(pb, rule_nr * 2);
-        chunk_nr = get_be16(pb);
-        url_fskip(pb, (num - 1 - rule_nr) * 2);
+        url_fskip(&pb, rule_nr * 2);
+        chunk_nr = get_be16(&pb);
+        url_fskip(&pb, (num - 1 - rule_nr) * 2);
 
         /* read MDPR chunks */
-        num = get_be16(pb);
+        num = get_be16(&pb);
         if (chunk_nr >= num)
             return -1;
         while (chunk_nr--)
-            url_fskip(pb, get_be32(pb));
-        size = get_be32(pb);
+            url_fskip(&pb, get_be32(&pb));
+        size = get_be32(&pb);
     } else {
         size = rdt->mlti_data_size;
-        url_fseek(pb, 0, SEEK_SET);
+        url_fseek(&pb, 0, SEEK_SET);
     }
-    if (ff_rm_read_mdpr_codecdata(rdt->rmctx, pb, st, size) < 0)
+    if (ff_rm_read_mdpr_codecdata(rdt->rmctx, &pb, st, rdt->rmst[st->index], size) < 0)
         return -1;
 
-    url_close_buf(pb);
     return 0;
 }
 
@@ -172,10 +192,13 @@ rdt_load_mdpr (PayloadContext *rdt, AVStream *st, int rule_nr)
 
 int
 ff_rdt_parse_header(const uint8_t *buf, int len,
-                    int *set_id, int *seq_no, int *stream_id,
-                    int *is_keyframe, uint32_t *timestamp)
+                    int *pset_id, int *pseq_no, int *pstream_id,
+                    int *pis_keyframe, uint32_t *ptimestamp)
 {
-    int consumed = 10;
+    GetBitContext gb;
+    int consumed = 0, set_id, seq_no, stream_id, is_keyframe,
+        len_included, need_reliable;
+    uint32_t timestamp;
 
     /* skip status packets */
     while (len >= 5 && buf[1] == 0xFF /* status packet */) {
@@ -189,7 +212,7 @@ ff_rdt_parse_header(const uint8_t *buf, int len,
         len -= pkt_len;
         consumed += pkt_len;
     }
-    if (len < 10)
+    if (len < 16)
         return -1;
     /**
      * Layout of the header (in bits):
@@ -242,51 +265,74 @@ ff_rdt_parse_header(const uint8_t *buf, int len,
      * [2] http://www.wireshark.org/docs/dfref/r/rdt.html and
      *     http://anonsvn.wireshark.org/viewvc/trunk/epan/dissectors/packet-rdt.c
      */
-    if (set_id)      *set_id      = (buf[0]>>1) & 0x1f;
-    if (seq_no)      *seq_no      = AV_RB16(buf+1);
-    if (timestamp)   *timestamp   = AV_RB32(buf+4);
-    if (stream_id)   *stream_id   = (buf[3]>>1) & 0x1f;
-    if (is_keyframe) *is_keyframe = !(buf[3] & 0x1);
-
-    return consumed;
+    init_get_bits(&gb, buf, len << 3);
+    len_included  = get_bits1(&gb);
+    need_reliable = get_bits1(&gb);
+    set_id        = get_bits(&gb, 5);
+    skip_bits(&gb, 1);
+    seq_no        = get_bits(&gb, 16);
+    if (len_included)
+        skip_bits(&gb, 16);
+    skip_bits(&gb, 2);
+    stream_id     = get_bits(&gb, 5);
+    is_keyframe   = !get_bits1(&gb);
+    timestamp     = get_bits_long(&gb, 32);
+    if (set_id == 0x1f)
+        set_id    = get_bits(&gb, 16);
+    if (need_reliable)
+        skip_bits(&gb, 16);
+    if (stream_id == 0x1f)
+        stream_id = get_bits(&gb, 16);
+
+    if (pset_id)      *pset_id      = set_id;
+    if (pseq_no)      *pseq_no      = seq_no;
+    if (pstream_id)   *pstream_id   = stream_id;
+    if (pis_keyframe) *pis_keyframe = is_keyframe;
+    if (ptimestamp)   *ptimestamp   = timestamp;
+
+    return consumed + (get_bits_count(&gb) >> 3);
 }
 
 /**< return 0 on packet, no more left, 1 on packet, 1 on partial packet... */
 static int
-rdt_parse_packet (PayloadContext *rdt, AVStream *st,
+rdt_parse_packet (AVFormatContext *ctx, PayloadContext *rdt, AVStream *st,
                   AVPacket *pkt, uint32_t *timestamp,
                   const uint8_t *buf, int len, int flags)
 {
     int seq = 1, res;
-    ByteIOContext *pb;
-    RMContext *rm = rdt->rmctx->priv_data;
+    ByteIOContext pb;
 
-    if (rm->audio_pkt_cnt == 0) {
+    if (rdt->audio_pkt_cnt == 0) {
         int pos;
 
-        url_open_buf (&pb, buf, len, URL_RDONLY);
-        flags = (flags & PKT_FLAG_KEY) ? 2 : 0;
-        res = ff_rm_parse_packet (rdt->rmctx, pb, st, len, pkt,
+        init_put_byte(&pb, buf, len, 0, NULL, NULL, NULL, NULL);
+        flags = (flags & RTP_FLAG_KEY) ? 2 : 0;
+        res = ff_rm_parse_packet (rdt->rmctx, &pb, st, rdt->rmst[st->index], len, pkt,
                                   &seq, &flags, timestamp);
-        pos = url_ftell(pb);
-        url_close_buf (pb);
+        pos = url_ftell(&pb);
         if (res < 0)
             return res;
-        if (rm->audio_pkt_cnt > 0 &&
-            st->codec->codec_id == CODEC_ID_AAC) {
-            memcpy (rdt->buffer, buf + pos, len - pos);
-            url_open_buf (&rdt->rmctx->pb, rdt->buffer, len - pos, URL_RDONLY);
+        if (res > 0) {
+            if (st->codec->codec_id == CODEC_ID_AAC) {
+                memcpy (rdt->buffer, buf + pos, len - pos);
+                rdt->rmctx->pb = av_alloc_put_byte (rdt->buffer, len - pos, 0,
+                                                    NULL, NULL, NULL, NULL);
+            }
+            goto get_cache;
         }
     } else {
-        ff_rm_retrieve_cache (rdt->rmctx, rdt->rmctx->pb, st, pkt);
-        if (rm->audio_pkt_cnt == 0 &&
+get_cache:
+        rdt->audio_pkt_cnt =
+            ff_rm_retrieve_cache (rdt->rmctx, rdt->rmctx->pb,
+                                  st, rdt->rmst[st->index], pkt);
+        if (rdt->audio_pkt_cnt == 0 &&
             st->codec->codec_id == CODEC_ID_AAC)
-            url_close_buf (rdt->rmctx->pb);
+            av_freep(&rdt->rmctx->pb);
     }
     pkt->stream_index = st->index;
     pkt->pts = *timestamp;
 
-    return rm->audio_pkt_cnt > 0;
+    return rdt->audio_pkt_cnt > 0;
 }
 
 int
@@ -300,11 +346,12 @@ ff_rdt_parse_packet(RDTDemuxContext *s, AVPacket *pkt,
     if (!s->parse_packet)
         return -1;
 
-    if (!buf) {
+    if (!buf && s->prev_stream_id != -1) {
         /* return the next packets, if any */
         timestamp= 0; ///< Should not be used if buf is NULL, but should be set to the timestamp of the packet returned....
-        rv= s->parse_packet(s->dynamic_protocol_context,
-                            s->st, pkt, &timestamp, NULL, 0, flags);
+        rv= s->parse_packet(s->ic, s->dynamic_protocol_context,
+                            s->streams[s->prev_stream_id],
+                            pkt, &timestamp, NULL, 0, flags);
         return rv;
     }
 
@@ -313,16 +360,25 @@ ff_rdt_parse_packet(RDTDemuxContext *s, AVPacket *pkt,
     rv = ff_rdt_parse_header(buf, len, &set_id, &seq_no, &stream_id, &is_keyframe, &timestamp);
     if (rv < 0)
         return rv;
-    if (is_keyframe && (set_id != s->prev_set_id || timestamp != s->prev_timestamp)) {
-        flags |= PKT_FLAG_KEY;
+    if (is_keyframe &&
+        (set_id != s->prev_set_id || timestamp != s->prev_timestamp ||
+         stream_id != s->prev_stream_id)) {
+        flags |= RTP_FLAG_KEY;
         s->prev_set_id    = set_id;
         s->prev_timestamp = timestamp;
     }
+    s->prev_stream_id = stream_id;
     buf += rv;
     len -= rv;
 
-    rv = s->parse_packet(s->dynamic_protocol_context,
-                         s->st, pkt, &timestamp, buf, len, flags);
+     if (s->prev_stream_id >= s->n_streams) {
+         s->prev_stream_id = -1;
+         return -1;
+     }
+
+    rv = s->parse_packet(s->ic, s->dynamic_protocol_context,
+                         s->streams[s->prev_stream_id],
+                         pkt, &timestamp, buf, len, flags);
 
     return rv;
 }
@@ -335,15 +391,6 @@ ff_rdt_subscribe_rule (char *cmd, int size,
                 stream_nr, rule_nr * 2, stream_nr, rule_nr * 2 + 1);
 }
 
-void
-ff_rdt_subscribe_rule2 (RDTDemuxContext *s, char *cmd, int size,
-                        int stream_nr, int rule_nr)
-{
-    PayloadContext *rdt = s->dynamic_protocol_context;
-
-    rdt_load_mdpr(rdt, s->st, rule_nr * 2);
-}
-
 static unsigned char *
 rdt_parse_b64buf (unsigned int *target_len, const char *p)
 {
@@ -360,18 +407,109 @@ rdt_parse_b64buf (unsigned int *target_len, const char *p)
 }
 
 static int
-rdt_parse_sdp_line (AVStream *stream, PayloadContext *rdt, const char *line)
+rdt_parse_sdp_line (AVFormatContext *s, int st_index,
+                    PayloadContext *rdt, const char *line)
 {
+    AVStream *stream = s->streams[st_index];
     const char *p = line;
 
     if (av_strstart(p, "OpaqueData:buffer;", &p)) {
         rdt->mlti_data = rdt_parse_b64buf(&rdt->mlti_data_size, p);
     } else if (av_strstart(p, "StartTime:integer;", &p))
         stream->first_dts = atoi(p);
+    else if (av_strstart(p, "ASMRuleBook:string;", &p)) {
+        int n = st_index, first = -1;
+
+        for (n = 0; n < s->nb_streams; n++)
+            if (s->streams[n]->priv_data == stream->priv_data) {
+                if (first == -1) first = n;
+                rdt->rmst[s->streams[n]->index] = ff_rm_alloc_rmstream();
+                rdt_load_mdpr(rdt, s->streams[n], (n - first) * 2);
+
+                if (s->streams[n]->codec->codec_id == CODEC_ID_AAC)
+                    s->streams[n]->codec->frame_size = 1; // FIXME
+           }
+    }
 
     return 0;
 }
 
+static void
+real_parse_asm_rule(AVStream *st, const char *p, const char *end)
+{
+    do {
+        /* can be either averagebandwidth= or AverageBandwidth= */
+        if (sscanf(p, " %*1[Aa]verage%*1[Bb]andwidth=%d", &st->codec->bit_rate) == 1)
+            break;
+        if (!(p = strchr(p, ',')) || p > end)
+            p = end;
+        p++;
+    } while (p < end);
+}
+
+static AVStream *
+add_dstream(AVFormatContext *s, AVStream *orig_st)
+{
+    AVStream *st;
+
+    if (!(st = av_new_stream(s, 0)))
+        return NULL;
+    st->codec->codec_type = orig_st->codec->codec_type;
+    st->priv_data         = orig_st->priv_data;
+    st->first_dts         = orig_st->first_dts;
+
+    return st;
+}
+
+static void
+real_parse_asm_rulebook(AVFormatContext *s, AVStream *orig_st,
+                        const char *p)
+{
+    const char *end;
+    int n_rules, odd = 0;
+    AVStream *st;
+
+    /**
+     * The ASMRuleBook contains a list of comma-separated strings per rule,
+     * and each rule is separated by a ;. The last one also has a ; at the
+     * end so we can use it as delimiter.
+     * Every rule occurs twice, once for when the RTSP packet header marker
+     * is set and once for if it isn't. We only read the first because we
+     * don't care much (that's what the "odd" variable is for).
+     * Each rule contains a set of one or more statements, optionally
+     * preceeded by a single condition. If there's a condition, the rule
+     * starts with a '#'. Multiple conditions are merged between brackets,
+     * so there are never multiple conditions spread out over separate
+     * statements. Generally, these conditions are bitrate limits (min/max)
+     * for multi-bitrate streams.
+     */
+    if (*p == '\"') p++;
+    for (n_rules = 0; s->nb_streams < MAX_STREAMS;) {
+        if (!(end = strchr(p, ';')))
+            break;
+        if (!odd && end != p) {
+            if (n_rules > 0)
+                st = add_dstream(s, orig_st);
+            else
+                st = orig_st;
+            real_parse_asm_rule(st, p, end);
+            n_rules++;
+        }
+        p = end + 1;
+        odd ^= 1;
+    }
+}
+
+void
+ff_real_parse_sdp_a_line (AVFormatContext *s, int stream_index,
+                          const char *line)
+{
+    const char *p = line;
+
+    if (av_strstart(p, "ASMRuleBook:string;", &p))
+        real_parse_asm_rulebook(s, s->streams[stream_index], p);
+}
+
 static PayloadContext *
 rdt_new_extradata (void)
 {
@@ -385,6 +523,13 @@ rdt_new_extradata (void)
 static void
 rdt_free_extradata (PayloadContext *rdt)
 {
+    int i;
+
+    for (i = 0; i < MAX_STREAMS; i++)
+        if (rdt->rmst[i]) {
+            ff_rm_free_rmstream(rdt->rmst[i]);
+            av_freep(&rdt->rmst[i]);
+        }
     if (rdt->rmctx)
         av_close_input_stream(rdt->rmctx);
     av_freep(&rdt->mlti_data);