LCOV - code coverage report
Current view: top level - libavformat - rtpdec_xiph.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 170 0.0 %
Date: 2017-12-17 23:02:56 Functions: 0 6 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Xiph RTP Protocols
       3             :  * Copyright (c) 2009 Colin McQuillian
       4             :  * Copyright (c) 2010 Josh Allmann
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : /**
      24             :  * @file
      25             :  * @brief Xiph / RTP Code
      26             :  * @author Colin McQuillan <m.niloc@gmail.com>
      27             :  * @author Josh Allmann <joshua.allmann@gmail.com>
      28             :  */
      29             : 
      30             : #include "libavutil/attributes.h"
      31             : #include "libavutil/avassert.h"
      32             : #include "libavutil/avstring.h"
      33             : #include "libavutil/base64.h"
      34             : #include "libavcodec/bytestream.h"
      35             : 
      36             : #include "avio_internal.h"
      37             : #include "internal.h"
      38             : #include "rtpdec.h"
      39             : #include "rtpdec_formats.h"
      40             : 
      41             : /**
      42             :  * RTP/Xiph specific private data.
      43             :  */
      44             : struct PayloadContext {
      45             :     unsigned ident;             ///< 24-bit stream configuration identifier
      46             :     uint32_t timestamp;
      47             :     AVIOContext* fragment;    ///< buffer for split payloads
      48             :     uint8_t *split_buf;
      49             :     int split_pos, split_buf_len, split_buf_size;
      50             :     int split_pkts;
      51             : };
      52             : 
      53           0 : static void xiph_close_context(PayloadContext * data)
      54             : {
      55           0 :     ffio_free_dyn_buf(&data->fragment);
      56           0 :     av_freep(&data->split_buf);
      57           0 : }
      58             : 
      59             : 
      60           0 : static int xiph_handle_packet(AVFormatContext *ctx, PayloadContext *data,
      61             :                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
      62             :                               const uint8_t *buf, int len, uint16_t seq,
      63             :                               int flags)
      64             : {
      65             : 
      66             :     int ident, fragmented, tdt, num_pkts, pkt_len;
      67             : 
      68           0 :     if (!buf) {
      69           0 :         if (!data->split_buf || data->split_pos + 2 > data->split_buf_len ||
      70           0 :             data->split_pkts <= 0) {
      71           0 :             av_log(ctx, AV_LOG_ERROR, "No more data to return\n");
      72           0 :             return AVERROR_INVALIDDATA;
      73             :         }
      74           0 :         pkt_len = AV_RB16(data->split_buf + data->split_pos);
      75           0 :         data->split_pos += 2;
      76           0 :         if (pkt_len > data->split_buf_len - data->split_pos) {
      77           0 :             av_log(ctx, AV_LOG_ERROR, "Not enough data to return\n");
      78           0 :             return AVERROR_INVALIDDATA;
      79             :         }
      80           0 :         if (av_new_packet(pkt, pkt_len)) {
      81           0 :             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
      82           0 :             return AVERROR(ENOMEM);
      83             :         }
      84           0 :         pkt->stream_index = st->index;
      85           0 :         memcpy(pkt->data, data->split_buf + data->split_pos, pkt_len);
      86           0 :         data->split_pos += pkt_len;
      87           0 :         data->split_pkts--;
      88           0 :         return data->split_pkts > 0;
      89             :     }
      90             : 
      91           0 :     if (len < 6 || len > INT_MAX/2) {
      92           0 :         av_log(ctx, AV_LOG_ERROR, "Invalid %d byte packet\n", len);
      93           0 :         return AVERROR_INVALIDDATA;
      94             :     }
      95             : 
      96             :     // read xiph rtp headers
      97           0 :     ident       = AV_RB24(buf);
      98           0 :     fragmented  = buf[3] >> 6;
      99           0 :     tdt         = (buf[3] >> 4) & 3;
     100           0 :     num_pkts    = buf[3] & 0xf;
     101           0 :     pkt_len     = AV_RB16(buf + 4);
     102             : 
     103           0 :     if (pkt_len > len - 6) {
     104           0 :         av_log(ctx, AV_LOG_ERROR,
     105             :                "Invalid packet length %d in %d byte packet\n", pkt_len,
     106             :                len);
     107           0 :         return AVERROR_INVALIDDATA;
     108             :     }
     109             : 
     110           0 :     if (ident != data->ident) {
     111           0 :         avpriv_report_missing_feature(ctx, "Xiph SDP configuration change");
     112           0 :         return AVERROR_PATCHWELCOME;
     113             :     }
     114             : 
     115           0 :     if (tdt) {
     116           0 :         avpriv_report_missing_feature(ctx,
     117             :                                       "RTP Xiph packet settings (%d,%d,%d)",
     118             :                                       fragmented, tdt, num_pkts);
     119           0 :         return AVERROR_PATCHWELCOME;
     120             :     }
     121             : 
     122           0 :     buf += 6; // move past header bits
     123           0 :     len -= 6;
     124             : 
     125           0 :     if (fragmented == 0) {
     126           0 :         if (av_new_packet(pkt, pkt_len)) {
     127           0 :             av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
     128           0 :             return AVERROR(ENOMEM);
     129             :         }
     130           0 :         pkt->stream_index = st->index;
     131           0 :         memcpy(pkt->data, buf, pkt_len);
     132           0 :         buf += pkt_len;
     133           0 :         len -= pkt_len;
     134           0 :         num_pkts--;
     135             : 
     136           0 :         if (num_pkts > 0) {
     137           0 :             if (len > data->split_buf_size || !data->split_buf) {
     138           0 :                 av_freep(&data->split_buf);
     139           0 :                 data->split_buf_size = 2 * len;
     140           0 :                 data->split_buf = av_malloc(data->split_buf_size);
     141           0 :                 if (!data->split_buf) {
     142           0 :                     av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
     143           0 :                     av_packet_unref(pkt);
     144           0 :                     return AVERROR(ENOMEM);
     145             :                 }
     146             :             }
     147           0 :             memcpy(data->split_buf, buf, len);
     148           0 :             data->split_buf_len = len;
     149           0 :             data->split_pos = 0;
     150           0 :             data->split_pkts = num_pkts;
     151           0 :             return 1;
     152             :         }
     153             : 
     154           0 :         return 0;
     155             : 
     156           0 :     } else if (fragmented == 1) {
     157             :         // start of xiph data fragment
     158             :         int res;
     159             : 
     160             :         // end packet has been lost somewhere, so drop buffered data
     161           0 :         ffio_free_dyn_buf(&data->fragment);
     162             : 
     163           0 :         if((res = avio_open_dyn_buf(&data->fragment)) < 0)
     164           0 :             return res;
     165             : 
     166           0 :         avio_write(data->fragment, buf, pkt_len);
     167           0 :         data->timestamp = *timestamp;
     168             : 
     169             :     } else {
     170             :         av_assert1(fragmented < 4);
     171           0 :         if (data->timestamp != *timestamp) {
     172             :             // skip if fragmented timestamp is incorrect;
     173             :             // a start packet has been lost somewhere
     174           0 :             ffio_free_dyn_buf(&data->fragment);
     175           0 :             av_log(ctx, AV_LOG_ERROR, "RTP timestamps don't match!\n");
     176           0 :             return AVERROR_INVALIDDATA;
     177             :         }
     178           0 :         if (!data->fragment) {
     179           0 :             av_log(ctx, AV_LOG_WARNING,
     180             :                    "Received packet without a start fragment; dropping.\n");
     181           0 :             return AVERROR(EAGAIN);
     182             :         }
     183             : 
     184             :         // copy data to fragment buffer
     185           0 :         avio_write(data->fragment, buf, pkt_len);
     186             : 
     187           0 :         if (fragmented == 3) {
     188             :             // end of xiph data packet
     189           0 :             int ret = ff_rtp_finalize_packet(pkt, &data->fragment, st->index);
     190           0 :             if (ret < 0) {
     191           0 :                 av_log(ctx, AV_LOG_ERROR,
     192             :                        "Error occurred when getting fragment buffer.");
     193           0 :                 return ret;
     194             :             }
     195             : 
     196           0 :             return 0;
     197             :         }
     198             :     }
     199             : 
     200           0 :    return AVERROR(EAGAIN);
     201             : }
     202             : 
     203             : /**
     204             :  * Length encoding described in RFC5215 section 3.1.1.
     205             :  */
     206           0 : static int get_base128(const uint8_t ** buf, const uint8_t * buf_end)
     207             : {
     208           0 :     int n = 0;
     209           0 :     for (; *buf < buf_end; ++*buf) {
     210           0 :         n <<= 7;
     211           0 :         n += **buf & 0x7f;
     212           0 :         if (!(**buf & 0x80)) {
     213           0 :             ++*buf;
     214           0 :             return n;
     215             :         }
     216             :     }
     217           0 :     return 0;
     218             : }
     219             : 
     220             : /**
     221             :  * Based off parse_packed_headers in Vorbis RTP
     222             :  */
     223             : static int
     224           0 : parse_packed_headers(AVFormatContext *s,
     225             :                      const uint8_t * packed_headers,
     226             :                      const uint8_t * packed_headers_end,
     227             :                      AVCodecParameters *par, PayloadContext * xiph_data)
     228             : {
     229             : 
     230             :     unsigned num_packed, num_headers, length, length1, length2, extradata_alloc;
     231             :     uint8_t *ptr;
     232             : 
     233           0 :     if (packed_headers_end - packed_headers < 9) {
     234           0 :         av_log(s, AV_LOG_ERROR,
     235             :                "Invalid %"PTRDIFF_SPECIFIER" byte packed header.",
     236             :                packed_headers_end - packed_headers);
     237           0 :         return AVERROR_INVALIDDATA;
     238             :     }
     239             : 
     240           0 :     num_packed         = bytestream_get_be32(&packed_headers);
     241           0 :     xiph_data->ident   = bytestream_get_be24(&packed_headers);
     242           0 :     length             = bytestream_get_be16(&packed_headers);
     243           0 :     num_headers        = get_base128(&packed_headers, packed_headers_end);
     244           0 :     length1            = get_base128(&packed_headers, packed_headers_end);
     245           0 :     length2            = get_base128(&packed_headers, packed_headers_end);
     246             : 
     247           0 :     if (num_packed != 1 || num_headers > 3) {
     248           0 :         avpriv_report_missing_feature(s, "%u packed headers, %u headers",
     249             :                                       num_packed, num_headers);
     250           0 :         return AVERROR_PATCHWELCOME;
     251             :     }
     252             : 
     253           0 :     if (packed_headers_end - packed_headers != length ||
     254           0 :         length1 > length || length2 > length - length1) {
     255           0 :         av_log(s, AV_LOG_ERROR,
     256             :                "Bad packed header lengths (%d,%d,%"PTRDIFF_SPECIFIER",%u)\n", length1,
     257             :                length2, packed_headers_end - packed_headers, length);
     258           0 :         return AVERROR_INVALIDDATA;
     259             :     }
     260             : 
     261             :     /* allocate extra space:
     262             :      * -- length/255 +2 for xiphlacing
     263             :      * -- one for the '2' marker
     264             :      * -- AV_INPUT_BUFFER_PADDING_SIZE required */
     265           0 :     extradata_alloc = length + length/255 + 3 + AV_INPUT_BUFFER_PADDING_SIZE;
     266             : 
     267           0 :     if (ff_alloc_extradata(par, extradata_alloc)) {
     268           0 :         av_log(s, AV_LOG_ERROR, "Out of memory\n");
     269           0 :         return AVERROR(ENOMEM);
     270             :     }
     271           0 :     ptr = par->extradata;
     272           0 :     *ptr++ = 2;
     273           0 :     ptr += av_xiphlacing(ptr, length1);
     274           0 :     ptr += av_xiphlacing(ptr, length2);
     275           0 :     memcpy(ptr, packed_headers, length);
     276           0 :     ptr += length;
     277           0 :     par->extradata_size = ptr - par->extradata;
     278             :     // clear out remaining parts of the buffer
     279           0 :     memset(ptr, 0, extradata_alloc - par->extradata_size);
     280             : 
     281           0 :     return 0;
     282             : }
     283             : 
     284           0 : static int xiph_parse_fmtp_pair(AVFormatContext *s,
     285             :                                 AVStream* stream,
     286             :                                 PayloadContext *xiph_data,
     287             :                                 const char *attr, const char *value)
     288             : {
     289           0 :     AVCodecParameters *par = stream->codecpar;
     290           0 :     int result = 0;
     291             : 
     292           0 :     if (!strcmp(attr, "sampling")) {
     293           0 :         if (!strcmp(value, "YCbCr-4:2:0")) {
     294           0 :             par->format = AV_PIX_FMT_YUV420P;
     295           0 :         } else if (!strcmp(value, "YCbCr-4:4:2")) {
     296           0 :             par->format = AV_PIX_FMT_YUV422P;
     297           0 :         } else if (!strcmp(value, "YCbCr-4:4:4")) {
     298           0 :             par->format = AV_PIX_FMT_YUV444P;
     299             :         } else {
     300           0 :             av_log(s, AV_LOG_ERROR,
     301             :                    "Unsupported pixel format %s\n", attr);
     302           0 :             return AVERROR_INVALIDDATA;
     303             :         }
     304           0 :     } else if (!strcmp(attr, "width")) {
     305             :         /* This is an integer between 1 and 1048561
     306             :          * and MUST be in multiples of 16. */
     307           0 :         par->width = atoi(value);
     308           0 :         return 0;
     309           0 :     } else if (!strcmp(attr, "height")) {
     310             :         /* This is an integer between 1 and 1048561
     311             :          * and MUST be in multiples of 16. */
     312           0 :         par->height = atoi(value);
     313           0 :         return 0;
     314           0 :     } else if (!strcmp(attr, "delivery-method")) {
     315             :         /* Possible values are: inline, in_band, out_band/specific_name. */
     316           0 :         return AVERROR_PATCHWELCOME;
     317           0 :     } else if (!strcmp(attr, "configuration-uri")) {
     318             :         /* NOTE: configuration-uri is supported only under 2 conditions:
     319             :          *--after the delivery-method tag
     320             :          * --with a delivery-method value of out_band */
     321           0 :         return AVERROR_PATCHWELCOME;
     322           0 :     } else if (!strcmp(attr, "configuration")) {
     323             :         /* NOTE: configuration is supported only AFTER the delivery-method tag
     324             :          * The configuration value is a base64 encoded packed header */
     325           0 :         uint8_t *decoded_packet = NULL;
     326             :         int packet_size;
     327           0 :         size_t decoded_alloc = strlen(value) / 4 * 3 + 4;
     328             : 
     329           0 :         if (decoded_alloc <= INT_MAX) {
     330           0 :             decoded_packet = av_malloc(decoded_alloc);
     331           0 :             if (decoded_packet) {
     332           0 :                 packet_size =
     333           0 :                     av_base64_decode(decoded_packet, value, decoded_alloc);
     334             : 
     335           0 :                 result = parse_packed_headers
     336           0 :                     (s, decoded_packet, decoded_packet + packet_size, par,
     337             :                     xiph_data);
     338             :             } else {
     339           0 :                 av_log(s, AV_LOG_ERROR,
     340             :                        "Out of memory while decoding SDP configuration.\n");
     341           0 :                 result = AVERROR(ENOMEM);
     342             :             }
     343             :         } else {
     344           0 :             av_log(s, AV_LOG_ERROR, "Packet too large\n");
     345           0 :             result = AVERROR_INVALIDDATA;
     346             :         }
     347           0 :         av_free(decoded_packet);
     348             :     }
     349           0 :     return result;
     350             : }
     351             : 
     352           0 : static int xiph_parse_sdp_line(AVFormatContext *s, int st_index,
     353             :                                PayloadContext *data, const char *line)
     354             : {
     355             :     const char *p;
     356             : 
     357           0 :     if (st_index < 0)
     358           0 :         return 0;
     359             : 
     360           0 :     if (av_strstart(line, "fmtp:", &p)) {
     361           0 :         return ff_parse_fmtp(s, s->streams[st_index], data, p,
     362             :                              xiph_parse_fmtp_pair);
     363             :     }
     364             : 
     365           0 :     return 0;
     366             : }
     367             : 
     368             : RTPDynamicProtocolHandler ff_theora_dynamic_handler = {
     369             :     .enc_name         = "theora",
     370             :     .codec_type       = AVMEDIA_TYPE_VIDEO,
     371             :     .codec_id         = AV_CODEC_ID_THEORA,
     372             :     .priv_data_size   = sizeof(PayloadContext),
     373             :     .parse_sdp_a_line = xiph_parse_sdp_line,
     374             :     .close            = xiph_close_context,
     375             :     .parse_packet     = xiph_handle_packet,
     376             : };
     377             : 
     378             : RTPDynamicProtocolHandler ff_vorbis_dynamic_handler = {
     379             :     .enc_name         = "vorbis",
     380             :     .codec_type       = AVMEDIA_TYPE_AUDIO,
     381             :     .codec_id         = AV_CODEC_ID_VORBIS,
     382             :     .need_parsing     = AVSTREAM_PARSE_HEADERS,
     383             :     .priv_data_size   = sizeof(PayloadContext),
     384             :     .parse_sdp_a_line = xiph_parse_sdp_line,
     385             :     .close            = xiph_close_context,
     386             :     .parse_packet     = xiph_handle_packet,
     387             : };

Generated by: LCOV version 1.13