LCOV - code coverage report
Current view: top level - libavformat - rtpdec_h263_rfc2190.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 85 0.0 %
Date: 2017-12-16 01:21:47 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /*
       2             :  * RTP H.263 Depacketizer, RFC 2190
       3             :  * Copyright (c) 2012 Martin Storsjo
       4             :  * Based on the GStreamer H.263 Depayloder:
       5             :  * Copyright 2005 Wim Taymans
       6             :  * Copyright 2007 Edward Hervey
       7             :  * Copyright 2007 Nokia Corporation
       8             :  * Copyright 2007 Collabora Ltd, Philippe Kalaf
       9             :  * Copyright 2010 Mark Nauwelaerts
      10             :  *
      11             :  * This file is part of FFmpeg.
      12             :  *
      13             :  * FFmpeg is free software; you can redistribute it and/or
      14             :  * modify it under the terms of the GNU Lesser General Public
      15             :  * License as published by the Free Software Foundation; either
      16             :  * version 2.1 of the License, or (at your option) any later version.
      17             :  *
      18             :  * FFmpeg is distributed in the hope that it will be useful,
      19             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      21             :  * Lesser General Public License for more details.
      22             :  *
      23             :  * You should have received a copy of the GNU Lesser General Public
      24             :  * License along with FFmpeg; if not, write to the Free Software
      25             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      26             :  */
      27             : 
      28             : #include "avformat.h"
      29             : #include "avio_internal.h"
      30             : #include "rtpdec_formats.h"
      31             : #include "libavutil/attributes.h"
      32             : #include "libavutil/intreadwrite.h"
      33             : #include "libavcodec/get_bits.h"
      34             : 
      35             : struct PayloadContext {
      36             :     AVIOContext *buf;
      37             :     uint8_t      endbyte;
      38             :     int          endbyte_bits;
      39             :     uint32_t     timestamp;
      40             :     int          newformat;
      41             : };
      42             : 
      43           0 : static void h263_close_context(PayloadContext *data)
      44             : {
      45           0 :     ffio_free_dyn_buf(&data->buf);
      46           0 : }
      47             : 
      48           0 : static int h263_handle_packet(AVFormatContext *ctx, PayloadContext *data,
      49             :                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
      50             :                               const uint8_t *buf, int len, uint16_t seq,
      51             :                               int flags)
      52             : {
      53             :     /* Corresponding to header fields in the RFC */
      54             :     int f, p, i, sbit, ebit, src, r;
      55             :     int header_size, ret;
      56             : 
      57           0 :     if (data->newformat)
      58           0 :         return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
      59             :                                      seq, flags);
      60             : 
      61           0 :     if (data->buf && data->timestamp != *timestamp) {
      62             :         /* Dropping old buffered, unfinished data */
      63           0 :         ffio_free_dyn_buf(&data->buf);
      64           0 :         data->endbyte_bits = 0;
      65             :     }
      66             : 
      67           0 :     if (len < 4) {
      68           0 :         av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
      69           0 :         return AVERROR_INVALIDDATA;
      70             :     }
      71             : 
      72           0 :     f = buf[0] & 0x80;
      73           0 :     p = buf[0] & 0x40;
      74           0 :     if (!f) {
      75             :         /* Mode A */
      76           0 :         header_size = 4;
      77           0 :         i = buf[1] & 0x10;
      78           0 :         r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
      79           0 :     } else if (!p) {
      80             :         /* Mode B */
      81           0 :         header_size = 8;
      82           0 :         if (len < header_size) {
      83           0 :             av_log(ctx, AV_LOG_ERROR,
      84             :                    "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
      85             :                    len, header_size);
      86           0 :             return AVERROR_INVALIDDATA;
      87             :         }
      88           0 :         r = buf[3] & 0x03;
      89           0 :         i = buf[4] & 0x80;
      90             :     } else {
      91             :         /* Mode C */
      92           0 :         header_size = 12;
      93           0 :         if (len < header_size) {
      94           0 :             av_log(ctx, AV_LOG_ERROR,
      95             :                    "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
      96             :                    len, header_size);
      97           0 :             return AVERROR_INVALIDDATA;
      98             :         }
      99           0 :         r = buf[3] & 0x03;
     100           0 :         i = buf[4] & 0x80;
     101             :     }
     102           0 :     sbit = (buf[0] >> 3) & 0x7;
     103           0 :     ebit =  buf[0]       & 0x7;
     104           0 :     src  = (buf[1] & 0xe0) >> 5;
     105           0 :     if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
     106           0 :         if ((src == 0 || src >= 6) && r) {
     107             :             /* Invalid src for this format, and bits that should be zero
     108             :              * according to RFC 2190 aren't zero. */
     109           0 :             av_log(ctx, AV_LOG_WARNING,
     110             :                    "Interpreting H.263 RTP data as RFC 2429/4629 even though "
     111             :                    "signalled with a static payload type.\n");
     112           0 :             data->newformat = 1;
     113           0 :             return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
     114             :                                          len, seq, flags);
     115             :         }
     116             :     }
     117             : 
     118           0 :     buf += header_size;
     119           0 :     len -= header_size;
     120             : 
     121           0 :     if (!data->buf) {
     122             :         /* Check the picture start code, only start buffering a new frame
     123             :          * if this is correct */
     124           0 :         if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
     125           0 :             ret = avio_open_dyn_buf(&data->buf);
     126           0 :             if (ret < 0)
     127           0 :                 return ret;
     128           0 :             data->timestamp = *timestamp;
     129             :         } else {
     130             :             /* Frame not started yet, skipping */
     131           0 :             return AVERROR(EAGAIN);
     132             :         }
     133             :     }
     134             : 
     135           0 :     if (data->endbyte_bits || sbit) {
     136           0 :         if (data->endbyte_bits == sbit) {
     137           0 :             data->endbyte |= buf[0] & (0xff >> sbit);
     138           0 :             data->endbyte_bits = 0;
     139           0 :             buf++;
     140           0 :             len--;
     141           0 :             avio_w8(data->buf, data->endbyte);
     142             :         } else {
     143             :             /* Start/end skip bits not matching - missed packets? */
     144             :             GetBitContext gb;
     145           0 :             init_get_bits(&gb, buf, len*8 - ebit);
     146           0 :             skip_bits(&gb, sbit);
     147           0 :             if (data->endbyte_bits) {
     148           0 :                 data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
     149           0 :                 avio_w8(data->buf, data->endbyte);
     150             :             }
     151           0 :             while (get_bits_left(&gb) >= 8)
     152           0 :                 avio_w8(data->buf, get_bits(&gb, 8));
     153           0 :             data->endbyte_bits = get_bits_left(&gb);
     154           0 :             if (data->endbyte_bits)
     155           0 :                 data->endbyte = get_bits(&gb, data->endbyte_bits) <<
     156           0 :                                 (8 - data->endbyte_bits);
     157           0 :             ebit = 0;
     158           0 :             len = 0;
     159             :         }
     160             :     }
     161           0 :     if (ebit) {
     162           0 :         if (len > 0)
     163           0 :             avio_write(data->buf, buf, len - 1);
     164           0 :         data->endbyte_bits = 8 - ebit;
     165           0 :         data->endbyte = buf[len - 1] & (0xff << ebit);
     166             :     } else {
     167           0 :         avio_write(data->buf, buf, len);
     168             :     }
     169             : 
     170           0 :     if (!(flags & RTP_FLAG_MARKER))
     171           0 :         return AVERROR(EAGAIN);
     172             : 
     173           0 :     if (data->endbyte_bits)
     174           0 :         avio_w8(data->buf, data->endbyte);
     175           0 :     data->endbyte_bits = 0;
     176             : 
     177           0 :     ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
     178           0 :     if (ret < 0)
     179           0 :         return ret;
     180           0 :     if (!i)
     181           0 :         pkt->flags   |= AV_PKT_FLAG_KEY;
     182             : 
     183           0 :     return 0;
     184             : }
     185             : 
     186             : RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler = {
     187             :     .codec_type        = AVMEDIA_TYPE_VIDEO,
     188             :     .codec_id          = AV_CODEC_ID_H263,
     189             :     .need_parsing      = AVSTREAM_PARSE_FULL,
     190             :     .parse_packet      = h263_handle_packet,
     191             :     .priv_data_size    = sizeof(PayloadContext),
     192             :     .close             = h263_close_context,
     193             :     .static_payload_id = 34,
     194             : };

Generated by: LCOV version 1.13