LCOV - code coverage report
Current view: top level - libavformat - rtpdec_h264.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 177 0.0 %
Date: 2017-12-17 11:58:42 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /*
       2             :  * RTP H.264 Protocol (RFC3984)
       3             :  * Copyright (c) 2006 Ryan Martell
       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 H.264 / RTP Code (RFC3984)
      25             :  * @author Ryan Martell <rdm4@martellventures.com>
      26             :  *
      27             :  * @note Notes:
      28             :  * Notes:
      29             :  * This currently supports packetization mode:
      30             :  * Single Nal Unit Mode (0), or
      31             :  * Non-Interleaved Mode (1).  It currently does not support
      32             :  * Interleaved Mode (2). (This requires implementing STAP-B, MTAP16, MTAP24,
      33             :  *                        FU-B packet types)
      34             :  */
      35             : 
      36             : #include "libavutil/attributes.h"
      37             : #include "libavutil/base64.h"
      38             : #include "libavutil/intreadwrite.h"
      39             : #include "libavutil/avstring.h"
      40             : #include "avformat.h"
      41             : 
      42             : #include "rtpdec.h"
      43             : #include "rtpdec_formats.h"
      44             : 
      45             : struct PayloadContext {
      46             :     // sdp setup parameters
      47             :     uint8_t profile_idc;
      48             :     uint8_t profile_iop;
      49             :     uint8_t level_idc;
      50             :     int packetization_mode;
      51             : #ifdef DEBUG
      52             :     int packet_types_received[32];
      53             : #endif
      54             : };
      55             : 
      56             : #ifdef DEBUG
      57             : #define COUNT_NAL_TYPE(data, nal) data->packet_types_received[(nal) & 0x1f]++
      58             : #define NAL_COUNTERS data->packet_types_received
      59             : #else
      60             : #define COUNT_NAL_TYPE(data, nal) do { } while (0)
      61             : #define NAL_COUNTERS NULL
      62             : #endif
      63             : #define NAL_MASK 0x1f
      64             : 
      65             : static const uint8_t start_sequence[] = { 0, 0, 0, 1 };
      66             : 
      67           0 : static void parse_profile_level_id(AVFormatContext *s,
      68             :                                    PayloadContext *h264_data,
      69             :                                    const char *value)
      70             : {
      71             :     char buffer[3];
      72             :     // 6 characters=3 bytes, in hex.
      73             :     uint8_t profile_idc;
      74             :     uint8_t profile_iop;
      75             :     uint8_t level_idc;
      76             : 
      77           0 :     buffer[0]   = value[0];
      78           0 :     buffer[1]   = value[1];
      79           0 :     buffer[2]   = '\0';
      80           0 :     profile_idc = strtol(buffer, NULL, 16);
      81           0 :     buffer[0]   = value[2];
      82           0 :     buffer[1]   = value[3];
      83           0 :     profile_iop = strtol(buffer, NULL, 16);
      84           0 :     buffer[0]   = value[4];
      85           0 :     buffer[1]   = value[5];
      86           0 :     level_idc   = strtol(buffer, NULL, 16);
      87             : 
      88           0 :     av_log(s, AV_LOG_DEBUG,
      89             :            "RTP Profile IDC: %x Profile IOP: %x Level: %x\n",
      90             :            profile_idc, profile_iop, level_idc);
      91           0 :     h264_data->profile_idc = profile_idc;
      92           0 :     h264_data->profile_iop = profile_iop;
      93           0 :     h264_data->level_idc   = level_idc;
      94           0 : }
      95             : 
      96           0 : int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s,
      97             :                                        uint8_t **data_ptr, int *size_ptr,
      98             :                                        const char *value)
      99             : {
     100             :     char base64packet[1024];
     101             :     uint8_t decoded_packet[1024];
     102             :     int packet_size;
     103             : 
     104           0 :     while (*value) {
     105           0 :         char *dst = base64packet;
     106             : 
     107           0 :         while (*value && *value != ','
     108           0 :                && (dst - base64packet) < sizeof(base64packet) - 1) {
     109           0 :             *dst++ = *value++;
     110             :         }
     111           0 :         *dst++ = '\0';
     112             : 
     113           0 :         if (*value == ',')
     114           0 :             value++;
     115             : 
     116           0 :         packet_size = av_base64_decode(decoded_packet, base64packet,
     117             :                                        sizeof(decoded_packet));
     118           0 :         if (packet_size > 0) {
     119           0 :             uint8_t *dest = av_realloc(*data_ptr,
     120           0 :                                        packet_size + sizeof(start_sequence) +
     121           0 :                                        *size_ptr +
     122             :                                        AV_INPUT_BUFFER_PADDING_SIZE);
     123           0 :             if (!dest) {
     124           0 :                 av_log(s, AV_LOG_ERROR,
     125             :                        "Unable to allocate memory for extradata!\n");
     126           0 :                 return AVERROR(ENOMEM);
     127             :             }
     128           0 :             *data_ptr = dest;
     129             : 
     130           0 :             memcpy(dest + *size_ptr, start_sequence,
     131             :                    sizeof(start_sequence));
     132           0 :             memcpy(dest + *size_ptr + sizeof(start_sequence),
     133             :                    decoded_packet, packet_size);
     134           0 :             memset(dest + *size_ptr + sizeof(start_sequence) +
     135             :                    packet_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
     136             : 
     137           0 :             *size_ptr += sizeof(start_sequence) + packet_size;
     138             :         }
     139             :     }
     140             : 
     141           0 :     return 0;
     142             : }
     143             : 
     144           0 : static int sdp_parse_fmtp_config_h264(AVFormatContext *s,
     145             :                                       AVStream *stream,
     146             :                                       PayloadContext *h264_data,
     147             :                                       const char *attr, const char *value)
     148             : {
     149           0 :     AVCodecParameters *par = stream->codecpar;
     150             : 
     151           0 :     if (!strcmp(attr, "packetization-mode")) {
     152           0 :         av_log(s, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value));
     153           0 :         h264_data->packetization_mode = atoi(value);
     154             :         /*
     155             :          * Packetization Mode:
     156             :          * 0 or not present: Single NAL mode (Only nals from 1-23 are allowed)
     157             :          * 1: Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed.
     158             :          * 2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A),
     159             :          *                      and 29 (FU-B) are allowed.
     160             :          */
     161           0 :         if (h264_data->packetization_mode > 1)
     162           0 :             av_log(s, AV_LOG_ERROR,
     163             :                    "Interleaved RTP mode is not supported yet.\n");
     164           0 :     } else if (!strcmp(attr, "profile-level-id")) {
     165           0 :         if (strlen(value) == 6)
     166           0 :             parse_profile_level_id(s, h264_data, value);
     167           0 :     } else if (!strcmp(attr, "sprop-parameter-sets")) {
     168             :         int ret;
     169           0 :         if (*value == 0 || value[strlen(value) - 1] == ',') {
     170           0 :             av_log(s, AV_LOG_WARNING, "Missing PPS in sprop-parameter-sets, ignoring\n");
     171           0 :             return 0;
     172             :         }
     173           0 :         par->extradata_size = 0;
     174           0 :         av_freep(&par->extradata);
     175           0 :         ret = ff_h264_parse_sprop_parameter_sets(s, &par->extradata,
     176             :                                                  &par->extradata_size, value);
     177           0 :         av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n",
     178             :                par->extradata, par->extradata_size);
     179           0 :         return ret;
     180             :     }
     181           0 :     return 0;
     182             : }
     183             : 
     184           0 : void ff_h264_parse_framesize(AVCodecParameters *par, const char *p)
     185             : {
     186             :     char buf1[50];
     187           0 :     char *dst = buf1;
     188             : 
     189             :     // remove the protocol identifier
     190           0 :     while (*p && *p == ' ')
     191           0 :         p++;                     // strip spaces.
     192           0 :     while (*p && *p != ' ')
     193           0 :         p++;                     // eat protocol identifier
     194           0 :     while (*p && *p == ' ')
     195           0 :         p++;                     // strip trailing spaces.
     196           0 :     while (*p && *p != '-' && (dst - buf1) < sizeof(buf1) - 1)
     197           0 :         *dst++ = *p++;
     198           0 :     *dst = '\0';
     199             : 
     200             :     // a='framesize:96 320-240'
     201             :     // set our parameters
     202           0 :     par->width   = atoi(buf1);
     203           0 :     par->height  = atoi(p + 1); // skip the -
     204           0 : }
     205             : 
     206           0 : int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, PayloadContext *data, AVPacket *pkt,
     207             :                                      const uint8_t *buf, int len,
     208             :                                      int skip_between, int *nal_counters,
     209             :                                      int nal_mask)
     210             : {
     211           0 :     int pass         = 0;
     212           0 :     int total_length = 0;
     213           0 :     uint8_t *dst     = NULL;
     214             :     int ret;
     215             : 
     216             :     // first we are going to figure out the total size
     217           0 :     for (pass = 0; pass < 2; pass++) {
     218           0 :         const uint8_t *src = buf;
     219           0 :         int src_len        = len;
     220             : 
     221           0 :         while (src_len > 2) {
     222           0 :             uint16_t nal_size = AV_RB16(src);
     223             : 
     224             :             // consume the length of the aggregate
     225           0 :             src     += 2;
     226           0 :             src_len -= 2;
     227             : 
     228           0 :             if (nal_size <= src_len) {
     229           0 :                 if (pass == 0) {
     230             :                     // counting
     231           0 :                     total_length += sizeof(start_sequence) + nal_size;
     232             :                 } else {
     233             :                     // copying
     234           0 :                     memcpy(dst, start_sequence, sizeof(start_sequence));
     235           0 :                     dst += sizeof(start_sequence);
     236           0 :                     memcpy(dst, src, nal_size);
     237           0 :                     if (nal_counters)
     238           0 :                         nal_counters[(*src) & nal_mask]++;
     239           0 :                     dst += nal_size;
     240             :                 }
     241             :             } else {
     242           0 :                 av_log(ctx, AV_LOG_ERROR,
     243             :                        "nal size exceeds length: %d %d\n", nal_size, src_len);
     244           0 :                 return AVERROR_INVALIDDATA;
     245             :             }
     246             : 
     247             :             // eat what we handled
     248           0 :             src     += nal_size + skip_between;
     249           0 :             src_len -= nal_size + skip_between;
     250             :         }
     251             : 
     252           0 :         if (pass == 0) {
     253             :             /* now we know the total size of the packet (with the
     254             :              * start sequences added) */
     255           0 :             if ((ret = av_new_packet(pkt, total_length)) < 0)
     256           0 :                 return ret;
     257           0 :             dst = pkt->data;
     258             :         }
     259             :     }
     260             : 
     261           0 :     return 0;
     262             : }
     263             : 
     264           0 : int ff_h264_handle_frag_packet(AVPacket *pkt, const uint8_t *buf, int len,
     265             :                                int start_bit, const uint8_t *nal_header,
     266             :                                int nal_header_len)
     267             : {
     268             :     int ret;
     269           0 :     int tot_len = len;
     270           0 :     int pos = 0;
     271           0 :     if (start_bit)
     272           0 :         tot_len += sizeof(start_sequence) + nal_header_len;
     273           0 :     if ((ret = av_new_packet(pkt, tot_len)) < 0)
     274           0 :         return ret;
     275           0 :     if (start_bit) {
     276           0 :         memcpy(pkt->data + pos, start_sequence, sizeof(start_sequence));
     277           0 :         pos += sizeof(start_sequence);
     278           0 :         memcpy(pkt->data + pos, nal_header, nal_header_len);
     279           0 :         pos += nal_header_len;
     280             :     }
     281           0 :     memcpy(pkt->data + pos, buf, len);
     282           0 :     return 0;
     283             : }
     284             : 
     285           0 : static int h264_handle_packet_fu_a(AVFormatContext *ctx, PayloadContext *data, AVPacket *pkt,
     286             :                                    const uint8_t *buf, int len,
     287             :                                    int *nal_counters, int nal_mask)
     288             : {
     289             :     uint8_t fu_indicator, fu_header, start_bit, nal_type, nal;
     290             : 
     291           0 :     if (len < 3) {
     292           0 :         av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H.264 RTP packet\n");
     293           0 :         return AVERROR_INVALIDDATA;
     294             :     }
     295             : 
     296           0 :     fu_indicator = buf[0];
     297           0 :     fu_header    = buf[1];
     298           0 :     start_bit    = fu_header >> 7;
     299           0 :     nal_type     = fu_header & 0x1f;
     300           0 :     nal          = fu_indicator & 0xe0 | nal_type;
     301             : 
     302             :     // skip the fu_indicator and fu_header
     303           0 :     buf += 2;
     304           0 :     len -= 2;
     305             : 
     306           0 :     if (start_bit && nal_counters)
     307           0 :         nal_counters[nal_type & nal_mask]++;
     308           0 :     return ff_h264_handle_frag_packet(pkt, buf, len, start_bit, &nal, 1);
     309             : }
     310             : 
     311             : // return 0 on packet, no more left, 1 on packet, 1 on partial packet
     312           0 : static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
     313             :                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
     314             :                               const uint8_t *buf, int len, uint16_t seq,
     315             :                               int flags)
     316             : {
     317             :     uint8_t nal;
     318             :     uint8_t type;
     319           0 :     int result = 0;
     320             : 
     321           0 :     if (!len) {
     322           0 :         av_log(ctx, AV_LOG_ERROR, "Empty H.264 RTP packet\n");
     323           0 :         return AVERROR_INVALIDDATA;
     324             :     }
     325           0 :     nal  = buf[0];
     326           0 :     type = nal & 0x1f;
     327             : 
     328             :     /* Simplify the case (these are all the NAL types used internally by
     329             :      * the H.264 codec). */
     330           0 :     if (type >= 1 && type <= 23)
     331           0 :         type = 1;
     332           0 :     switch (type) {
     333           0 :     case 0:                    // undefined, but pass them through
     334             :     case 1:
     335           0 :         if ((result = av_new_packet(pkt, len + sizeof(start_sequence))) < 0)
     336           0 :             return result;
     337           0 :         memcpy(pkt->data, start_sequence, sizeof(start_sequence));
     338           0 :         memcpy(pkt->data + sizeof(start_sequence), buf, len);
     339             :         COUNT_NAL_TYPE(data, nal);
     340           0 :         break;
     341             : 
     342           0 :     case 24:                   // STAP-A (one packet, multiple nals)
     343             :         // consume the STAP-A NAL
     344           0 :         buf++;
     345           0 :         len--;
     346           0 :         result = ff_h264_handle_aggregated_packet(ctx, data, pkt, buf, len, 0,
     347             :                                                   NAL_COUNTERS, NAL_MASK);
     348           0 :         break;
     349             : 
     350           0 :     case 25:                   // STAP-B
     351             :     case 26:                   // MTAP-16
     352             :     case 27:                   // MTAP-24
     353             :     case 29:                   // FU-B
     354           0 :         avpriv_report_missing_feature(ctx, "RTP H.264 NAL unit type %d", type);
     355           0 :         result = AVERROR_PATCHWELCOME;
     356           0 :         break;
     357             : 
     358           0 :     case 28:                   // FU-A (fragmented nal)
     359           0 :         result = h264_handle_packet_fu_a(ctx, data, pkt, buf, len,
     360             :                                          NAL_COUNTERS, NAL_MASK);
     361           0 :         break;
     362             : 
     363           0 :     case 30:                   // undefined
     364             :     case 31:                   // undefined
     365             :     default:
     366           0 :         av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)\n", type);
     367           0 :         result = AVERROR_INVALIDDATA;
     368           0 :         break;
     369             :     }
     370             : 
     371           0 :     pkt->stream_index = st->index;
     372             : 
     373           0 :     return result;
     374             : }
     375             : 
     376           0 : static void h264_close_context(PayloadContext *data)
     377             : {
     378             : #ifdef DEBUG
     379             :     int ii;
     380             : 
     381             :     for (ii = 0; ii < 32; ii++) {
     382             :         if (data->packet_types_received[ii])
     383             :             av_log(NULL, AV_LOG_DEBUG, "Received %d packets of type %d\n",
     384             :                    data->packet_types_received[ii], ii);
     385             :     }
     386             : #endif
     387           0 : }
     388             : 
     389           0 : static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
     390             :                                PayloadContext *h264_data, const char *line)
     391             : {
     392             :     AVStream *stream;
     393           0 :     const char *p = line;
     394             : 
     395           0 :     if (st_index < 0)
     396           0 :         return 0;
     397             : 
     398           0 :     stream = s->streams[st_index];
     399             : 
     400           0 :     if (av_strstart(p, "framesize:", &p)) {
     401           0 :         ff_h264_parse_framesize(stream->codecpar, p);
     402           0 :     } else if (av_strstart(p, "fmtp:", &p)) {
     403           0 :         return ff_parse_fmtp(s, stream, h264_data, p, sdp_parse_fmtp_config_h264);
     404           0 :     } else if (av_strstart(p, "cliprect:", &p)) {
     405             :         // could use this if we wanted.
     406             :     }
     407             : 
     408           0 :     return 0;
     409             : }
     410             : 
     411             : RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
     412             :     .enc_name         = "H264",
     413             :     .codec_type       = AVMEDIA_TYPE_VIDEO,
     414             :     .codec_id         = AV_CODEC_ID_H264,
     415             :     .need_parsing     = AVSTREAM_PARSE_FULL,
     416             :     .priv_data_size   = sizeof(PayloadContext),
     417             :     .parse_sdp_a_line = parse_h264_sdp_line,
     418             :     .close            = h264_close_context,
     419             :     .parse_packet     = h264_handle_packet,
     420             : };

Generated by: LCOV version 1.13