LCOV - code coverage report
Current view: top level - libavformat - rtpdec_hevc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 136 0.0 %
Date: 2017-12-13 18:07:29 Functions: 0 3 0.0 %

          Line data    Source code
       1             : /*
       2             :  * RTP parser for HEVC/H.265 payload format (draft version 6)
       3             :  * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
       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             : #include "libavutil/avassert.h"
      23             : #include "libavutil/avstring.h"
      24             : #include "libavutil/base64.h"
      25             : #include "libavcodec/get_bits.h"
      26             : 
      27             : #include "avformat.h"
      28             : #include "rtpdec.h"
      29             : #include "rtpdec_formats.h"
      30             : 
      31             : #define RTP_HEVC_PAYLOAD_HEADER_SIZE       2
      32             : #define RTP_HEVC_FU_HEADER_SIZE            1
      33             : #define RTP_HEVC_DONL_FIELD_SIZE           2
      34             : #define RTP_HEVC_DOND_FIELD_SIZE           1
      35             : #define RTP_HEVC_AP_NALU_LENGTH_FIELD_SIZE 2
      36             : #define HEVC_SPECIFIED_NAL_UNIT_TYPES      48
      37             : 
      38             : /* SDP out-of-band signaling data */
      39             : struct PayloadContext {
      40             :     int using_donl_field;
      41             :     int profile_id;
      42             :     uint8_t *sps, *pps, *vps, *sei;
      43             :     int sps_size, pps_size, vps_size, sei_size;
      44             : };
      45             : 
      46             : static const uint8_t start_sequence[] = { 0x00, 0x00, 0x00, 0x01 };
      47             : 
      48           0 : static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
      49             :                                               AVStream *stream,
      50             :                                               PayloadContext *hevc_data,
      51             :                                               const char *attr, const char *value)
      52             : {
      53             :     /* profile-space: 0-3 */
      54             :     /* profile-id: 0-31 */
      55           0 :     if (!strcmp(attr, "profile-id")) {
      56           0 :         hevc_data->profile_id = atoi(value);
      57           0 :         av_log(s, AV_LOG_TRACE, "SDP: found profile-id: %d\n", hevc_data->profile_id);
      58             :     }
      59             : 
      60             :     /* tier-flag: 0-1 */
      61             :     /* level-id: 0-255 */
      62             :     /* interop-constraints: [base16] */
      63             :     /* profile-compatibility-indicator: [base16] */
      64             :     /* sprop-sub-layer-id: 0-6, defines highest possible value for TID, default: 6 */
      65             :     /* recv-sub-layer-id: 0-6 */
      66             :     /* max-recv-level-id: 0-255 */
      67             :     /* tx-mode: MSM,SSM */
      68             :     /* sprop-vps: [base64] */
      69             :     /* sprop-sps: [base64] */
      70             :     /* sprop-pps: [base64] */
      71             :     /* sprop-sei: [base64] */
      72           0 :     if (!strcmp(attr, "sprop-vps") || !strcmp(attr, "sprop-sps") ||
      73           0 :         !strcmp(attr, "sprop-pps") || !strcmp(attr, "sprop-sei")) {
      74           0 :         uint8_t **data_ptr = NULL;
      75           0 :         int *size_ptr = NULL;
      76           0 :         if (!strcmp(attr, "sprop-vps")) {
      77           0 :             data_ptr = &hevc_data->vps;
      78           0 :             size_ptr = &hevc_data->vps_size;
      79           0 :         } else if (!strcmp(attr, "sprop-sps")) {
      80           0 :             data_ptr = &hevc_data->sps;
      81           0 :             size_ptr = &hevc_data->sps_size;
      82           0 :         } else if (!strcmp(attr, "sprop-pps")) {
      83           0 :             data_ptr = &hevc_data->pps;
      84           0 :             size_ptr = &hevc_data->pps_size;
      85           0 :         } else if (!strcmp(attr, "sprop-sei")) {
      86           0 :             data_ptr = &hevc_data->sei;
      87           0 :             size_ptr = &hevc_data->sei_size;
      88             :         } else
      89           0 :             av_assert0(0);
      90             : 
      91           0 :         ff_h264_parse_sprop_parameter_sets(s, data_ptr,
      92             :                                            size_ptr, value);
      93             :     }
      94             : 
      95             :     /* max-lsr, max-lps, max-cpb, max-dpb, max-br, max-tr, max-tc */
      96             :     /* max-fps */
      97             : 
      98             :     /* sprop-max-don-diff: 0-32767
      99             : 
     100             :          When the RTP stream depends on one or more other RTP
     101             :          streams (in this case tx-mode MUST be equal to "MSM" and
     102             :          MSM is in use), this parameter MUST be present and the
     103             :          value MUST be greater than 0.
     104             :     */
     105           0 :     if (!strcmp(attr, "sprop-max-don-diff")) {
     106           0 :         if (atoi(value) > 0)
     107           0 :             hevc_data->using_donl_field = 1;
     108           0 :         av_log(s, AV_LOG_TRACE, "Found sprop-max-don-diff in SDP, DON field usage is: %d\n",
     109             :                 hevc_data->using_donl_field);
     110             :     }
     111             : 
     112             :     /* sprop-depack-buf-nalus: 0-32767 */
     113           0 :     if (!strcmp(attr, "sprop-depack-buf-nalus")) {
     114           0 :         if (atoi(value) > 0)
     115           0 :             hevc_data->using_donl_field = 1;
     116           0 :         av_log(s, AV_LOG_TRACE, "Found sprop-depack-buf-nalus in SDP, DON field usage is: %d\n",
     117             :                 hevc_data->using_donl_field);
     118             :     }
     119             : 
     120             :     /* sprop-depack-buf-bytes: 0-4294967295 */
     121             :     /* depack-buf-cap */
     122             :     /* sprop-segmentation-id: 0-3 */
     123             :     /* sprop-spatial-segmentation-idc: [base16] */
     124             :     /* dec-parallel-ca: */
     125             :     /* include-dph */
     126             : 
     127           0 :     return 0;
     128             : }
     129             : 
     130           0 : static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index,
     131             :                                        PayloadContext *hevc_data, const char *line)
     132             : {
     133             :     AVStream *current_stream;
     134             :     AVCodecParameters *par;
     135           0 :     const char *sdp_line_ptr = line;
     136             : 
     137           0 :     if (st_index < 0)
     138           0 :         return 0;
     139             : 
     140           0 :     current_stream = ctx->streams[st_index];
     141           0 :     par  = current_stream->codecpar;
     142             : 
     143           0 :     if (av_strstart(sdp_line_ptr, "framesize:", &sdp_line_ptr)) {
     144           0 :         ff_h264_parse_framesize(par, sdp_line_ptr);
     145           0 :     } else if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
     146           0 :         int ret = ff_parse_fmtp(ctx, current_stream, hevc_data, sdp_line_ptr,
     147             :                                 hevc_sdp_parse_fmtp_config);
     148           0 :         if (hevc_data->vps_size || hevc_data->sps_size ||
     149           0 :             hevc_data->pps_size || hevc_data->sei_size) {
     150           0 :             av_freep(&par->extradata);
     151           0 :             par->extradata_size = hevc_data->vps_size + hevc_data->sps_size +
     152           0 :                                   hevc_data->pps_size + hevc_data->sei_size;
     153           0 :             par->extradata = av_malloc(par->extradata_size +
     154             :                                        AV_INPUT_BUFFER_PADDING_SIZE);
     155           0 :             if (!par->extradata) {
     156           0 :                 ret = AVERROR(ENOMEM);
     157           0 :                 par->extradata_size = 0;
     158             :             } else {
     159           0 :                 int pos = 0;
     160           0 :                 memcpy(par->extradata + pos, hevc_data->vps, hevc_data->vps_size);
     161           0 :                 pos += hevc_data->vps_size;
     162           0 :                 memcpy(par->extradata + pos, hevc_data->sps, hevc_data->sps_size);
     163           0 :                 pos += hevc_data->sps_size;
     164           0 :                 memcpy(par->extradata + pos, hevc_data->pps, hevc_data->pps_size);
     165           0 :                 pos += hevc_data->pps_size;
     166           0 :                 memcpy(par->extradata + pos, hevc_data->sei, hevc_data->sei_size);
     167           0 :                 pos += hevc_data->sei_size;
     168           0 :                 memset(par->extradata + pos, 0, AV_INPUT_BUFFER_PADDING_SIZE);
     169             :             }
     170             : 
     171           0 :             av_freep(&hevc_data->vps);
     172           0 :             av_freep(&hevc_data->sps);
     173           0 :             av_freep(&hevc_data->pps);
     174           0 :             av_freep(&hevc_data->sei);
     175           0 :             hevc_data->vps_size = 0;
     176           0 :             hevc_data->sps_size = 0;
     177           0 :             hevc_data->pps_size = 0;
     178           0 :             hevc_data->sei_size = 0;
     179             :         }
     180           0 :         return ret;
     181             :     }
     182             : 
     183           0 :     return 0;
     184             : }
     185             : 
     186           0 : static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx,
     187             :                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
     188             :                               const uint8_t *buf, int len, uint16_t seq,
     189             :                               int flags)
     190             : {
     191           0 :     const uint8_t *rtp_pl = buf;
     192             :     int tid, lid, nal_type;
     193             :     int first_fragment, last_fragment, fu_type;
     194             :     uint8_t new_nal_header[2];
     195           0 :     int res = 0;
     196             : 
     197             :     /* sanity check for size of input packet: 1 byte payload at least */
     198           0 :     if (len < RTP_HEVC_PAYLOAD_HEADER_SIZE + 1) {
     199           0 :         av_log(ctx, AV_LOG_ERROR, "Too short RTP/HEVC packet, got %d bytes\n", len);
     200           0 :         return AVERROR_INVALIDDATA;
     201             :     }
     202             : 
     203             :     /*
     204             :      * decode the HEVC payload header according to section 4 of draft version 6:
     205             :      *
     206             :      *    0                   1
     207             :      *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
     208             :      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     209             :      *   |F|   Type    |  LayerId  | TID |
     210             :      *   +-------------+-----------------+
     211             :      *
     212             :      *      Forbidden zero (F): 1 bit
     213             :      *      NAL unit type (Type): 6 bits
     214             :      *      NUH layer ID (LayerId): 6 bits
     215             :      *      NUH temporal ID plus 1 (TID): 3 bits
     216             :      */
     217           0 :     nal_type =  (buf[0] >> 1) & 0x3f;
     218           0 :     lid  = ((buf[0] << 5) & 0x20) | ((buf[1] >> 3) & 0x1f);
     219           0 :     tid  =   buf[1] & 0x07;
     220             : 
     221             :     /* sanity check for correct layer ID */
     222           0 :     if (lid) {
     223             :         /* future scalable or 3D video coding extensions */
     224           0 :         avpriv_report_missing_feature(ctx, "Multi-layer HEVC coding");
     225           0 :         return AVERROR_PATCHWELCOME;
     226             :     }
     227             : 
     228             :     /* sanity check for correct temporal ID */
     229           0 :     if (!tid) {
     230           0 :         av_log(ctx, AV_LOG_ERROR, "Illegal temporal ID in RTP/HEVC packet\n");
     231           0 :         return AVERROR_INVALIDDATA;
     232             :     }
     233             : 
     234             :     /* sanity check for correct NAL unit type */
     235           0 :     if (nal_type > 50) {
     236           0 :         av_log(ctx, AV_LOG_ERROR, "Unsupported (HEVC) NAL type (%d)\n", nal_type);
     237           0 :         return AVERROR_INVALIDDATA;
     238             :     }
     239             : 
     240           0 :     switch (nal_type) {
     241             :     /* video parameter set (VPS) */
     242           0 :     case 32:
     243             :     /* sequence parameter set (SPS) */
     244             :     case 33:
     245             :     /* picture parameter set (PPS) */
     246             :     case 34:
     247             :     /*  supplemental enhancement information (SEI) */
     248             :     case 39:
     249             :     /* single NAL unit packet */
     250             :     default:
     251             :         /* create A/V packet */
     252           0 :         if ((res = av_new_packet(pkt, sizeof(start_sequence) + len)) < 0)
     253           0 :             return res;
     254             :         /* A/V packet: copy start sequence */
     255           0 :         memcpy(pkt->data, start_sequence, sizeof(start_sequence));
     256             :         /* A/V packet: copy NAL unit data */
     257           0 :         memcpy(pkt->data + sizeof(start_sequence), buf, len);
     258             : 
     259           0 :         break;
     260             :     /* aggregated packet (AP) - with two or more NAL units */
     261           0 :     case 48:
     262             :         /* pass the HEVC payload header */
     263           0 :         buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
     264           0 :         len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
     265             : 
     266             :         /* pass the HEVC DONL field */
     267           0 :         if (rtp_hevc_ctx->using_donl_field) {
     268           0 :             buf += RTP_HEVC_DONL_FIELD_SIZE;
     269           0 :             len -= RTP_HEVC_DONL_FIELD_SIZE;
     270             :         }
     271             : 
     272           0 :         res = ff_h264_handle_aggregated_packet(ctx, rtp_hevc_ctx, pkt, buf, len,
     273           0 :                                                rtp_hevc_ctx->using_donl_field ?
     274             :                                                RTP_HEVC_DOND_FIELD_SIZE : 0,
     275             :                                                NULL, 0);
     276           0 :         if (res < 0)
     277           0 :             return res;
     278           0 :         break;
     279             :     /* fragmentation unit (FU) */
     280           0 :     case 49:
     281             :         /* pass the HEVC payload header */
     282           0 :         buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
     283           0 :         len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
     284             : 
     285             :         /*
     286             :          *    decode the FU header
     287             :          *
     288             :          *     0 1 2 3 4 5 6 7
     289             :          *    +-+-+-+-+-+-+-+-+
     290             :          *    |S|E|  FuType   |
     291             :          *    +---------------+
     292             :          *
     293             :          *       Start fragment (S): 1 bit
     294             :          *       End fragment (E): 1 bit
     295             :          *       FuType: 6 bits
     296             :          */
     297           0 :         first_fragment = buf[0] & 0x80;
     298           0 :         last_fragment  = buf[0] & 0x40;
     299           0 :         fu_type        = buf[0] & 0x3f;
     300             : 
     301             :         /* pass the HEVC FU header */
     302           0 :         buf += RTP_HEVC_FU_HEADER_SIZE;
     303           0 :         len -= RTP_HEVC_FU_HEADER_SIZE;
     304             : 
     305             :         /* pass the HEVC DONL field */
     306           0 :         if (rtp_hevc_ctx->using_donl_field) {
     307           0 :             buf += RTP_HEVC_DONL_FIELD_SIZE;
     308           0 :             len -= RTP_HEVC_DONL_FIELD_SIZE;
     309             :         }
     310             : 
     311           0 :         av_log(ctx, AV_LOG_TRACE, " FU type %d with %d bytes\n", fu_type, len);
     312             : 
     313             :         /* sanity check for size of input packet: 1 byte payload at least */
     314           0 :         if (len <= 0) {
     315           0 :             if (len < 0) {
     316           0 :                 av_log(ctx, AV_LOG_ERROR,
     317             :                        "Too short RTP/HEVC packet, got %d bytes of NAL unit type %d\n",
     318             :                        len, nal_type);
     319           0 :                 return AVERROR_INVALIDDATA;
     320             :             } else {
     321           0 :                 return AVERROR(EAGAIN);
     322             :             }
     323             :         }
     324             : 
     325           0 :         if (first_fragment && last_fragment) {
     326           0 :             av_log(ctx, AV_LOG_ERROR, "Illegal combination of S and E bit in RTP/HEVC packet\n");
     327           0 :             return AVERROR_INVALIDDATA;
     328             :         }
     329             : 
     330           0 :         new_nal_header[0] = (rtp_pl[0] & 0x81) | (fu_type << 1);
     331           0 :         new_nal_header[1] = rtp_pl[1];
     332             : 
     333           0 :         res = ff_h264_handle_frag_packet(pkt, buf, len, first_fragment,
     334             :                                          new_nal_header, sizeof(new_nal_header));
     335             : 
     336           0 :         break;
     337             :     /* PACI packet */
     338           0 :     case 50:
     339             :         /* Temporal scalability control information (TSCI) */
     340           0 :         avpriv_report_missing_feature(ctx, "PACI packets for RTP/HEVC");
     341           0 :         res = AVERROR_PATCHWELCOME;
     342           0 :         break;
     343             :     }
     344             : 
     345           0 :     pkt->stream_index = st->index;
     346             : 
     347           0 :     return res;
     348             : }
     349             : 
     350             : RTPDynamicProtocolHandler ff_hevc_dynamic_handler = {
     351             :     .enc_name         = "H265",
     352             :     .codec_type       = AVMEDIA_TYPE_VIDEO,
     353             :     .codec_id         = AV_CODEC_ID_HEVC,
     354             :     .need_parsing     = AVSTREAM_PARSE_FULL,
     355             :     .priv_data_size   = sizeof(PayloadContext),
     356             :     .parse_sdp_a_line = hevc_parse_sdp_line,
     357             :     .parse_packet     = hevc_handle_packet,
     358             : };

Generated by: LCOV version 1.13