LCOV - code coverage report
Current view: top level - libavformat - rtpdec_qt.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 139 0.0 %
Date: 2017-12-18 06:23:41 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /*
       2             :  * RTP/Quicktime support.
       3             :  * Copyright (c) 2009 Ronald S. Bultje
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * @brief Quicktime-style RTP support
      25             :  * @author Ronald S. Bultje <rbultje@ronald.bitfreak.net>
      26             :  */
      27             : 
      28             : #include "avformat.h"
      29             : #include "internal.h"
      30             : #include "avio_internal.h"
      31             : #include "rtp.h"
      32             : #include "rtpdec.h"
      33             : #include "isom.h"
      34             : #include "libavcodec/get_bits.h"
      35             : 
      36             : struct PayloadContext {
      37             :     AVPacket pkt;
      38             :     int bytes_per_frame, remaining;
      39             :     uint32_t timestamp;
      40             : };
      41             : 
      42           0 : static int qt_rtp_parse_packet(AVFormatContext *s, PayloadContext *qt,
      43             :                                AVStream *st, AVPacket *pkt,
      44             :                                uint32_t *timestamp, const uint8_t *buf,
      45             :                                int len, uint16_t seq, int flags)
      46             : {
      47             :     AVIOContext pb;
      48             :     GetBitContext gb;
      49             :     int packing_scheme, has_payload_desc, has_packet_info, alen,
      50           0 :         has_marker_bit = flags & RTP_FLAG_MARKER,
      51             :         keyframe;
      52             : 
      53           0 :     if (qt->remaining) {
      54           0 :         int num = qt->pkt.size / qt->bytes_per_frame;
      55             : 
      56           0 :         if (av_new_packet(pkt, qt->bytes_per_frame))
      57           0 :             return AVERROR(ENOMEM);
      58           0 :         pkt->stream_index = st->index;
      59           0 :         pkt->flags        = qt->pkt.flags;
      60           0 :         memcpy(pkt->data,
      61           0 :                &qt->pkt.data[(num - qt->remaining) * qt->bytes_per_frame],
      62           0 :                qt->bytes_per_frame);
      63           0 :         if (--qt->remaining == 0) {
      64           0 :             av_freep(&qt->pkt.data);
      65           0 :             qt->pkt.size = 0;
      66             :         }
      67           0 :         return qt->remaining > 0;
      68             :     }
      69             : 
      70             :     /**
      71             :      * The RTP payload is described in:
      72             :      * http://developer.apple.com/quicktime/icefloe/dispatch026.html
      73             :      */
      74           0 :     init_get_bits(&gb, buf, len << 3);
      75           0 :     ffio_init_context(&pb, (uint8_t*)buf, len, 0, NULL, NULL, NULL, NULL);
      76             : 
      77           0 :     if (len < 4)
      78           0 :         return AVERROR_INVALIDDATA;
      79             : 
      80           0 :     skip_bits(&gb, 4); // version
      81           0 :     if ((packing_scheme = get_bits(&gb, 2)) == 0)
      82           0 :         return AVERROR_INVALIDDATA;
      83           0 :     keyframe            = get_bits1(&gb);
      84           0 :     has_payload_desc    = get_bits1(&gb);
      85           0 :     has_packet_info     = get_bits1(&gb);
      86           0 :     skip_bits(&gb, 23); // reserved:7, cache payload info:1, payload ID:15
      87             : 
      88           0 :     if (has_payload_desc) {
      89             :         int data_len, pos, is_start, is_finish;
      90             :         uint32_t tag;
      91             : 
      92           0 :         pos = get_bits_count(&gb) >> 3;
      93           0 :         if (pos + 12 > len)
      94           0 :             return AVERROR_INVALIDDATA;
      95             : 
      96           0 :         skip_bits(&gb, 2); // has non-I-frames:1, is sparse:1
      97           0 :         is_start  = get_bits1(&gb);
      98           0 :         is_finish = get_bits1(&gb);
      99           0 :         if (!is_start || !is_finish) {
     100           0 :             avpriv_request_sample(s, "RTP-X-QT with payload description "
     101             :                                   "split over several packets");
     102           0 :             return AVERROR_PATCHWELCOME;
     103             :         }
     104           0 :         skip_bits(&gb, 12); // reserved
     105           0 :         data_len = get_bits(&gb, 16);
     106             : 
     107           0 :         avio_seek(&pb, pos + 4, SEEK_SET);
     108           0 :         tag = avio_rl32(&pb);
     109           0 :         if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
     110           0 :                  tag != MKTAG('v','i','d','e')) ||
     111           0 :             (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
     112             :                  tag != MKTAG('s','o','u','n')))
     113           0 :             return AVERROR_INVALIDDATA;
     114           0 :         avpriv_set_pts_info(st, 32, 1, avio_rb32(&pb));
     115             : 
     116           0 :         if (pos + data_len > len)
     117           0 :             return AVERROR_INVALIDDATA;
     118             :         /* TLVs */
     119           0 :         while (avio_tell(&pb) + 4 < pos + data_len) {
     120           0 :             int tlv_len = avio_rb16(&pb);
     121           0 :             tag = avio_rl16(&pb);
     122           0 :             if (avio_tell(&pb) + tlv_len > pos + data_len)
     123           0 :                 return AVERROR_INVALIDDATA;
     124             : 
     125             : #define MKTAG16(a,b) MKTAG(a,b,0,0)
     126           0 :             switch (tag) {
     127           0 :             case MKTAG16('s','d'): {
     128             :                 MOVStreamContext *msc;
     129           0 :                 void *priv_data = st->priv_data;
     130           0 :                 int nb_streams = s->nb_streams;
     131           0 :                 MOVContext *mc = av_mallocz(sizeof(*mc));
     132           0 :                 if (!mc)
     133           0 :                     return AVERROR(ENOMEM);
     134           0 :                 mc->fc = s;
     135           0 :                 st->priv_data = msc = av_mallocz(sizeof(MOVStreamContext));
     136           0 :                 if (!msc) {
     137           0 :                     av_free(mc);
     138           0 :                     st->priv_data = priv_data;
     139           0 :                     return AVERROR(ENOMEM);
     140             :                 }
     141             :                 /* ff_mov_read_stsd_entries updates stream s->nb_streams-1,
     142             :                  * so set it temporarily to indicate which stream to update. */
     143           0 :                 s->nb_streams = st->index + 1;
     144           0 :                 ff_mov_read_stsd_entries(mc, &pb, 1);
     145           0 :                 qt->bytes_per_frame = msc->bytes_per_frame;
     146           0 :                 av_free(msc);
     147           0 :                 av_free(mc);
     148           0 :                 st->priv_data = priv_data;
     149           0 :                 s->nb_streams = nb_streams;
     150           0 :                 break;
     151             :             }
     152           0 :             default:
     153           0 :                 avio_skip(&pb, tlv_len);
     154           0 :                 break;
     155             :             }
     156             :         }
     157             : 
     158             :         /* 32-bit alignment */
     159           0 :         avio_skip(&pb, ((avio_tell(&pb) + 3) & ~3) - avio_tell(&pb));
     160             :     } else
     161           0 :         avio_seek(&pb, 4, SEEK_SET);
     162             : 
     163           0 :     if (has_packet_info) {
     164           0 :         avpriv_request_sample(s, "RTP-X-QT with packet-specific info");
     165           0 :         return AVERROR_PATCHWELCOME;
     166             :     }
     167             : 
     168           0 :     alen = len - avio_tell(&pb);
     169           0 :     if (alen <= 0)
     170           0 :         return AVERROR_INVALIDDATA;
     171             : 
     172           0 :     switch (packing_scheme) {
     173           0 :     case 3: /* one data packet spread over 1 or multiple RTP packets */
     174           0 :         if (qt->pkt.size > 0 && qt->timestamp == *timestamp) {
     175             :             int err;
     176           0 :             if ((err = av_reallocp(&qt->pkt.data, qt->pkt.size + alen +
     177             :                                    AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
     178           0 :                 qt->pkt.size = 0;
     179           0 :                 return err;
     180             :             }
     181             :         } else {
     182           0 :             av_freep(&qt->pkt.data);
     183           0 :             av_init_packet(&qt->pkt);
     184           0 :             qt->pkt.data = av_realloc(NULL, alen + AV_INPUT_BUFFER_PADDING_SIZE);
     185           0 :             if (!qt->pkt.data)
     186           0 :                 return AVERROR(ENOMEM);
     187           0 :             qt->pkt.size = 0;
     188           0 :             qt->timestamp = *timestamp;
     189             :         }
     190           0 :         memcpy(qt->pkt.data + qt->pkt.size, buf + avio_tell(&pb), alen);
     191           0 :         qt->pkt.size += alen;
     192           0 :         if (has_marker_bit) {
     193           0 :             int ret = av_packet_from_data(pkt, qt->pkt.data, qt->pkt.size);
     194           0 :             if (ret < 0)
     195           0 :                 return ret;
     196             : 
     197           0 :             qt->pkt.size = 0;
     198           0 :             qt->pkt.data = NULL;
     199           0 :             pkt->flags        = keyframe ? AV_PKT_FLAG_KEY : 0;
     200           0 :             pkt->stream_index = st->index;
     201           0 :             memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
     202           0 :             return 0;
     203             :         }
     204           0 :         return AVERROR(EAGAIN);
     205             : 
     206           0 :     case 1: /* constant packet size, multiple packets per RTP packet */
     207           0 :         if (qt->bytes_per_frame == 0 ||
     208           0 :             alen % qt->bytes_per_frame != 0)
     209           0 :             return AVERROR_INVALIDDATA; /* wrongly padded */
     210           0 :         qt->remaining = (alen / qt->bytes_per_frame) - 1;
     211           0 :         if (av_new_packet(pkt, qt->bytes_per_frame))
     212           0 :             return AVERROR(ENOMEM);
     213           0 :         memcpy(pkt->data, buf + avio_tell(&pb), qt->bytes_per_frame);
     214           0 :         pkt->flags = keyframe ? AV_PKT_FLAG_KEY : 0;
     215           0 :         pkt->stream_index = st->index;
     216           0 :         if (qt->remaining > 0) {
     217           0 :             av_freep(&qt->pkt.data);
     218           0 :             qt->pkt.data = av_realloc(NULL, qt->remaining * qt->bytes_per_frame);
     219           0 :             if (!qt->pkt.data) {
     220           0 :                 av_packet_unref(pkt);
     221           0 :                 return AVERROR(ENOMEM);
     222             :             }
     223           0 :             qt->pkt.size = qt->remaining * qt->bytes_per_frame;
     224           0 :             memcpy(qt->pkt.data,
     225           0 :                    buf + avio_tell(&pb) + qt->bytes_per_frame,
     226           0 :                    qt->remaining * qt->bytes_per_frame);
     227           0 :             qt->pkt.flags = pkt->flags;
     228           0 :             return 1;
     229             :         }
     230           0 :         return 0;
     231             : 
     232           0 :     default:  /* unimplemented */
     233           0 :         avpriv_request_sample(NULL, "RTP-X-QT with packing scheme 2");
     234           0 :         return AVERROR_PATCHWELCOME;
     235             :     }
     236             : }
     237             : 
     238           0 : static void qt_rtp_close(PayloadContext *qt)
     239             : {
     240           0 :     av_freep(&qt->pkt.data);
     241           0 : }
     242             : 
     243             : #define RTP_QT_HANDLER(m, n, s, t) \
     244             : RTPDynamicProtocolHandler ff_ ## m ## _rtp_ ## n ## _handler = { \
     245             :     .enc_name         = s, \
     246             :     .codec_type       = t, \
     247             :     .codec_id         = AV_CODEC_ID_NONE, \
     248             :     .priv_data_size   = sizeof(PayloadContext), \
     249             :     .close            = qt_rtp_close,   \
     250             :     .parse_packet     = qt_rtp_parse_packet, \
     251             : }
     252             : 
     253             : RTP_QT_HANDLER(qt,        vid, "X-QT",        AVMEDIA_TYPE_VIDEO);
     254             : RTP_QT_HANDLER(qt,        aud, "X-QT",        AVMEDIA_TYPE_AUDIO);
     255             : RTP_QT_HANDLER(quicktime, vid, "X-QUICKTIME", AVMEDIA_TYPE_VIDEO);
     256             : RTP_QT_HANDLER(quicktime, aud, "X-QUICKTIME", AVMEDIA_TYPE_AUDIO);

Generated by: LCOV version 1.13