libavformat/rtpdec_asf.c
1a30d541
 /*
  * Microsoft RTP/ASF support.
  * Copyright (c) 2008 Ronald S. Bultje
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg 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,
  * 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
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
  * @file
1a30d541
  * @brief Microsoft RTP/ASF support
  * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
  */
 
feb9057b
 #include "libavutil/avassert.h"
a16ebd40
 #include "libavutil/base64.h"
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
e9fce261
 #include "rtp.h"
965a3ddb
 #include "rtpdec_formats.h"
1a30d541
 #include "rtsp.h"
 #include "asf.h"
e731b8d8
 #include "avio_internal.h"
c3f9ebf7
 #include "internal.h"
1a30d541
 
c2f3eec4
 /**
  * From MSDN 2.2.1.4, we learn that ASF data packets over RTP should not
  * contain any padding. Unfortunately, the header min/max_pktsize are not
  * updated (thus making min_pktsize invalid). Here, we "fix" these faulty
  * min_pktsize values in the ASF file header.
  * @return 0 on success, <0 on failure (currently -1).
  */
acfe8faf
 static int rtp_asf_fix_header(uint8_t *buf, int len)
c2f3eec4
 {
     uint8_t *p = buf, *end = buf + len;
 
     if (len < sizeof(ff_asf_guid) * 2 + 22 ||
         memcmp(p, ff_asf_header, sizeof(ff_asf_guid))) {
         return -1;
     }
     p += sizeof(ff_asf_guid) + 14;
     do {
         uint64_t chunksize = AV_RL64(p + sizeof(ff_asf_guid));
445a02b1
         int skip = 6 * 8 + 3 * 4 + sizeof(ff_asf_guid) * 2;
c2f3eec4
         if (memcmp(p, ff_asf_file_header, sizeof(ff_asf_guid))) {
             if (chunksize > end - p)
                 return -1;
             p += chunksize;
             continue;
         }
 
445a02b1
         if (end - p < 8 + skip)
             break;
c2f3eec4
         /* skip most of the file header, to min_pktsize */
445a02b1
         p += skip;
         if (AV_RL32(p) == AV_RL32(p + 4)) {
c2f3eec4
             /* and set that to zero */
             AV_WL32(p, 0);
             return 0;
         }
         break;
     } while (end - p >= sizeof(ff_asf_guid) + 8);
 
     return -1;
 }
 
 /**
ae628ec1
  * The following code is basically a buffered AVIOContext,
c2f3eec4
  * with the added benefit of returning -EAGAIN (instead of 0)
  * on packet boundaries, such that the ASF demuxer can return
  * safely and resume business at the next packet.
  */
acfe8faf
 static int packetizer_read(void *opaque, uint8_t *buf, int buf_size)
c2f3eec4
 {
     return AVERROR(EAGAIN);
 }
 
ae628ec1
 static void init_packetizer(AVIOContext *pb, uint8_t *buf, int len)
c2f3eec4
 {
e731b8d8
     ffio_init_context(pb, buf, len, 0, NULL, packetizer_read, NULL, NULL);
c2f3eec4
 
     /* this "fills" the buffer with its current content */
     pb->pos     = len;
     pb->buf_end = buf + len;
 }
 
0fca8d24
 int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p)
1a30d541
 {
0fca8d24
     int ret = 0;
1a30d541
     if (av_strstart(p, "pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,", &p)) {
e947b75b
         AVIOContext pb = { 0 };
1a30d541
         RTSPState *rt = s->priv_data;
d293e346
         AVDictionary *opts = NULL;
1a30d541
         int len = strlen(p) * 6 / 8;
         char *buf = av_mallocz(len);
2ce10542
         AVInputFormat *iformat;
 
4733a12d
         if (!buf)
             return AVERROR(ENOMEM);
1a30d541
         av_base64_decode(buf, p, len);
 
c2f3eec4
         if (rtp_asf_fix_header(buf, len) < 0)
             av_log(s, AV_LOG_ERROR,
                    "Failed to fix invalid RTSP-MS/ASF min_pktsize\n");
         init_packetizer(&pb, buf, len);
1a30d541
         if (rt->asf_ctx) {
cd3716b9
             avformat_close_input(&rt->asf_ctx);
1a30d541
         }
d5a64562
 
2ce10542
         if (!(iformat = av_find_input_format("asf")))
             return AVERROR_DEMUXER_NOT_FOUND;
d5a64562
 
4733a12d
         rt->asf_ctx = avformat_alloc_context();
         if (!rt->asf_ctx) {
             av_free(buf);
603b8bc2
             return AVERROR(ENOMEM);
4733a12d
         }
603b8bc2
         rt->asf_ctx->pb      = &pb;
d293e346
         av_dict_set(&opts, "no_resync_search", "1", 0);
feb9057b
 
93629735
         if ((ret = ff_copy_whiteblacklists(rt->asf_ctx, s)) < 0) {
4641ae35
             av_dict_free(&opts);
             return ret;
         }
feb9057b
 
2ce10542
         ret = avformat_open_input(&rt->asf_ctx, "", iformat, &opts);
d293e346
         av_dict_free(&opts);
4733a12d
         if (ret < 0) {
f7c01ff2
             av_free(pb.buffer);
0fca8d24
             return ret;
4733a12d
         }
d2d67e42
         av_dict_copy(&s->metadata, rt->asf_ctx->metadata, 0);
a2704c97
         rt->asf_pb_pos = avio_tell(&pb);
f7c01ff2
         av_free(pb.buffer);
1a30d541
         rt->asf_ctx->pb = NULL;
     }
0fca8d24
     return ret;
1a30d541
 }
e9fce261
 
acfe8faf
 static int asfrtp_parse_sdp_line(AVFormatContext *s, int stream_index,
                                  PayloadContext *asf, const char *line)
e9fce261
 {
45600148
     if (stream_index < 0)
         return 0;
e9fce261
     if (av_strstart(line, "stream:", &line)) {
         RTSPState *rt = s->priv_data;
 
         s->streams[stream_index]->id = strtol(line, NULL, 10);
 
         if (rt->asf_ctx) {
             int i;
 
             for (i = 0; i < rt->asf_ctx->nb_streams; i++) {
                 if (s->streams[stream_index]->id == rt->asf_ctx->streams[i]->id) {
9200514a
                     avcodec_parameters_copy(s->streams[stream_index]->codecpar,
                                             rt->asf_ctx->streams[i]->codecpar);
2aec9e22
                     s->streams[stream_index]->need_parsing =
                         rt->asf_ctx->streams[i]->need_parsing;
c3f9ebf7
                     avpriv_set_pts_info(s->streams[stream_index], 32, 1, 1000);
e9fce261
                 }
            }
         }
     }
 
     return 0;
 }
 
c2f3eec4
 struct PayloadContext {
ae628ec1
     AVIOContext *pktbuf, pb;
9b1947c7
     uint8_t *buf;
c2f3eec4
 };
 
 /**
  * @return 0 when a packet was written into /p pkt, and no more data is left;
  *         1 when a packet was written into /p pkt, and more packets might be left;
  *        <0 when not enough data was provided to return a full packet, or on error.
  */
acfe8faf
 static int asfrtp_parse_packet(AVFormatContext *s, PayloadContext *asf,
                                AVStream *st, AVPacket *pkt,
                                uint32_t *timestamp,
90c784cc
                                const uint8_t *buf, int len, uint16_t seq,
                                int flags)
c2f3eec4
 {
ae628ec1
     AVIOContext *pb = &asf->pb;
c2f3eec4
     int res, mflags, len_off;
     RTSPState *rt = s->priv_data;
 
     if (!rt->asf_ctx)
         return -1;
 
     if (len > 0) {
bcc4cb46
         int off, out_len = 0;
c2f3eec4
 
         if (len < 4)
             return -1;
 
bcc4cb46
         av_freep(&asf->buf);
 
e6516944
         ffio_init_context(pb, (uint8_t *)buf, len, 0, NULL, NULL, NULL, NULL);
bcc4cb46
 
a2704c97
         while (avio_tell(pb) + 4 < len) {
             int start_off = avio_tell(pb);
4f5340a0
 
b7effd4e
             mflags = avio_r8(pb);
             len_off = avio_rb24(pb);
4f5340a0
             if (mflags & 0x20)   /**< relative timestamp */
45a8a02a
                 avio_skip(pb, 4);
4f5340a0
             if (mflags & 0x10)   /**< has duration */
45a8a02a
                 avio_skip(pb, 4);
4f5340a0
             if (mflags & 0x8)    /**< has location ID */
45a8a02a
                 avio_skip(pb, 4);
a2704c97
             off = avio_tell(pb);
4f5340a0
 
             if (!(mflags & 0x40)) {
                 /**
                  * If 0x40 is not set, the len_off field specifies an offset
                  * of this packet's payload data in the complete (reassembled)
                  * ASF packet. This is used to spread one ASF packet over
                  * multiple RTP packets.
                  */
a2704c97
                 if (asf->pktbuf && len_off != avio_tell(asf->pktbuf)) {
199fb402
                     ffio_free_dyn_buf(&asf->pktbuf);
4f5340a0
                 }
                 if (!len_off && !asf->pktbuf &&
b92c5452
                     (res = avio_open_dyn_buf(&asf->pktbuf)) < 0)
4f5340a0
                     return res;
                 if (!asf->pktbuf)
                     return AVERROR(EIO);
 
77eb5504
                 avio_write(asf->pktbuf, buf + off, len - off);
45a8a02a
                 avio_skip(pb, len - off);
4f5340a0
                 if (!(flags & RTP_FLAG_MARKER))
                     return -1;
6dc7d80d
                 out_len     = avio_close_dyn_buf(asf->pktbuf, &asf->buf);
c2f3eec4
                 asf->pktbuf = NULL;
4f5340a0
             } else {
                 /**
                  * If 0x40 is set, the len_off field specifies the length of
                  * the next ASF packet that can be read from this payload
                  * data alone. This is commonly the same as the payload size,
                  * but could be less in case of packet splitting (i.e.
                  * multiple ASF packets in one RTP packet).
                  */
 
                 int cur_len = start_off + len_off - off;
                 int prev_len = out_len;
                 out_len += cur_len;
5ea091fb
                 if (FFMIN(cur_len, len - off) < 0)
ba9a7e0d
                     return -1;
5626f994
                 if ((res = av_reallocp(&asf->buf, out_len)) < 0)
                     return res;
afbc4d2d
                 memcpy(asf->buf + prev_len, buf + off,
                        FFMIN(cur_len, len - off));
45a8a02a
                 avio_skip(pb, cur_len);
c2f3eec4
             }
         }
 
         init_packetizer(pb, asf->buf, out_len);
         pb->pos += rt->asf_pb_pos;
         pb->eof_reached = 0;
         rt->asf_ctx->pb = pb;
     }
 
     for (;;) {
         int i;
 
3c90cc2e
         res = ff_read_packet(rt->asf_ctx, pkt);
a2704c97
         rt->asf_pb_pos = avio_tell(pb);
c2f3eec4
         if (res != 0)
             break;
         for (i = 0; i < s->nb_streams; i++) {
             if (s->streams[i]->id == rt->asf_ctx->streams[pkt->stream_index]->id) {
                 pkt->stream_index = i;
                 return 1; // FIXME: return 0 if last packet
             }
         }
ce70f28a
         av_packet_unref(pkt);
c2f3eec4
     }
 
     return res == 1 ? -1 : res;
 }
 
d594dbec
 static void asfrtp_close_context(PayloadContext *asf)
c2f3eec4
 {
199fb402
     ffio_free_dyn_buf(&asf->pktbuf);
c2f3eec4
     av_freep(&asf->buf);
 }
 
e9fce261
 #define RTP_ASF_HANDLER(n, s, t) \
 RTPDynamicProtocolHandler ff_ms_rtp_ ## n ## _handler = { \
202a6697
     .enc_name         = s, \
     .codec_type       = t, \
36ef5369
     .codec_id         = AV_CODEC_ID_NONE, \
5d8cae45
     .priv_data_size   = sizeof(PayloadContext), \
202a6697
     .parse_sdp_a_line = asfrtp_parse_sdp_line, \
d594dbec
     .close            = asfrtp_close_context, \
202a6697
     .parse_packet     = asfrtp_parse_packet,   \
44adbebe
 }
e9fce261
 
72415b2a
 RTP_ASF_HANDLER(asf_pfv, "x-asf-pf",  AVMEDIA_TYPE_VIDEO);
 RTP_ASF_HANDLER(asf_pfa, "x-asf-pf",  AVMEDIA_TYPE_AUDIO);