]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtpdec_hevc.c
rtpdec_vp9: Update header parsing to spec draft 02
[ffmpeg] / libavformat / rtpdec_hevc.c
index 3ed7cac8f601a77d09b9efc2d2c00a4f3467feaf..97f87aeec353ef126e0c7375fd48b422a8f5ba96 100644 (file)
@@ -17,7 +17,6 @@
  * You should have received a copy of the GNU Lesser General Public
  * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
  */
 
 #include "libavutil/avstring.h"
 
 #include "avformat.h"
 #include "rtpdec.h"
+#include "rtpdec_formats.h"
 
 #define RTP_HEVC_PAYLOAD_HEADER_SIZE  2
 #define RTP_HEVC_FU_HEADER_SIZE       1
 #define RTP_HEVC_DONL_FIELD_SIZE      2
+#define RTP_HEVC_DOND_FIELD_SIZE      1
 #define HEVC_SPECIFIED_NAL_UNIT_TYPES 48
 
 /* SDP out-of-band signaling data */
@@ -41,39 +42,16 @@ struct PayloadContext {
 
 static const uint8_t start_sequence[] = { 0x00, 0x00, 0x00, 0x01 };
 
-static av_cold PayloadContext *hevc_new_context(void)
-{
-    return av_mallocz(sizeof(PayloadContext));
-}
-
-static av_cold void hevc_free_context(PayloadContext *data)
-{
-    av_free(data);
-}
-
-static av_cold int hevc_init(AVFormatContext *ctx, int st_index,
-                             PayloadContext *data)
-{
-    av_dlog(ctx, "hevc_init() for stream %d\n", st_index);
-
-    if (st_index < 0)
-        return 0;
-
-    ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
-
-    return 0;
-}
-
 static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
                                               AVStream *stream,
                                               PayloadContext *hevc_data,
-                                              char *attr, char *value)
+                                              const char *attr, const char *value)
 {
     /* profile-space: 0-3 */
     /* profile-id: 0-31 */
     if (!strcmp(attr, "profile-id")) {
         hevc_data->profile_id = atoi(value);
-        av_dlog(s, "SDP: found profile-id: %d\n", hevc_data->profile_id);
+        av_log(s, AV_LOG_TRACE, "SDP: found profile-id: %d\n", hevc_data->profile_id);
     }
 
     /* tier-flag: 0-1 */
@@ -90,8 +68,8 @@ static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
     /* sprop-sei: [base64] */
     if (!strcmp(attr, "sprop-vps") || !strcmp(attr, "sprop-sps") ||
         !strcmp(attr, "sprop-pps") || !strcmp(attr, "sprop-sei")) {
-        uint8_t **data_ptr;
-        int *size_ptr;
+        uint8_t **data_ptr = NULL;
+        int *size_ptr = NULL;
         if (!strcmp(attr, "sprop-vps")) {
             data_ptr = &hevc_data->vps;
             size_ptr = &hevc_data->vps_size;
@@ -106,41 +84,8 @@ static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
             size_ptr = &hevc_data->sei_size;
         }
 
-        while (*value) {
-            char base64packet[1024];
-            uint8_t decoded_packet[1024];
-            int decoded_packet_size;
-            char *dst = base64packet;
-
-            while (*value && *value != ',' &&
-                   (dst - base64packet) < sizeof(base64packet) - 1) {
-                *dst++ = *value++;
-            }
-            *dst++ = '\0';
-
-            if (*value == ',')
-                value++;
-
-            decoded_packet_size = av_base64_decode(decoded_packet, base64packet,
-                                                   sizeof(decoded_packet));
-            if (decoded_packet_size > 0) {
-                uint8_t *tmp = av_realloc(*data_ptr, decoded_packet_size +
-                                          sizeof(start_sequence) + *size_ptr);
-                if (!tmp) {
-                    av_log(s, AV_LOG_ERROR,
-                           "Unable to allocate memory for extradata!\n");
-                    return AVERROR(ENOMEM);
-                }
-                *data_ptr = tmp;
-
-                memcpy(*data_ptr + *size_ptr, start_sequence,
-                       sizeof(start_sequence));
-                memcpy(*data_ptr + *size_ptr + sizeof(start_sequence),
-                       decoded_packet, decoded_packet_size);
-
-                *size_ptr += sizeof(start_sequence) + decoded_packet_size;
-            }
-        }
+        ff_h264_parse_sprop_parameter_sets(s, data_ptr,
+                                           size_ptr, value);
     }
 
     /* max-lsr, max-lps, max-cpb, max-dpb, max-br, max-tr, max-tc */
@@ -156,7 +101,7 @@ static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
     if (!strcmp(attr, "sprop-max-don-diff")) {
         if (atoi(value) > 0)
             hevc_data->using_donl_field = 1;
-        av_dlog(s, "Found sprop-max-don-diff in SDP, DON field usage is: %d\n",
+        av_log(s, AV_LOG_TRACE, "Found sprop-max-don-diff in SDP, DON field usage is: %d\n",
                 hevc_data->using_donl_field);
     }
 
@@ -164,7 +109,7 @@ static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
     if (!strcmp(attr, "sprop-depack-buf-nalus")) {
         if (atoi(value) > 0)
             hevc_data->using_donl_field = 1;
-        av_dlog(s, "Found sprop-depack-buf-nalus in SDP, DON field usage is: %d\n",
+        av_log(s, AV_LOG_TRACE, "Found sprop-depack-buf-nalus in SDP, DON field usage is: %d\n",
                 hevc_data->using_donl_field);
     }
 
@@ -182,67 +127,41 @@ static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index,
                                        PayloadContext *hevc_data, const char *line)
 {
     AVStream *current_stream;
-    AVCodecContext *codec;
+    AVCodecParameters *par;
     const char *sdp_line_ptr = line;
 
     if (st_index < 0)
         return 0;
 
     current_stream = ctx->streams[st_index];
-    codec  = current_stream->codec;
+    par  = current_stream->codecpar;
 
     if (av_strstart(sdp_line_ptr, "framesize:", &sdp_line_ptr)) {
-        char str_video_width[50];
-        char *str_video_width_ptr = str_video_width;
-
-        /*
-         * parse "a=framesize:96 320-240"
-         */
-
-        /* ignore spaces */
-        while (*sdp_line_ptr && *sdp_line_ptr == ' ')
-            sdp_line_ptr++;
-        /* ignore RTP payload ID */
-        while (*sdp_line_ptr && *sdp_line_ptr != ' ')
-            sdp_line_ptr++;
-        /* ignore spaces */
-        while (*sdp_line_ptr && *sdp_line_ptr == ' ')
-            sdp_line_ptr++;
-        /* extract the actual video resolution description */
-        while (*sdp_line_ptr && *sdp_line_ptr != '-' &&
-               (str_video_width_ptr - str_video_width) < sizeof(str_video_width) - 1)
-            *str_video_width_ptr++ = *sdp_line_ptr++;
-        /* add trailing zero byte */
-        *str_video_width_ptr = '\0';
-
-        /* determine the width value */
-        codec->width   = atoi(str_video_width);
-        /* jump beyond the "-" and determine the height value */
-        codec->height  = atoi(sdp_line_ptr + 1);
+        ff_h264_parse_framesize(par, sdp_line_ptr);
     } else if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
         int ret = ff_parse_fmtp(ctx, current_stream, hevc_data, sdp_line_ptr,
                                 hevc_sdp_parse_fmtp_config);
         if (hevc_data->vps_size || hevc_data->sps_size ||
             hevc_data->pps_size || hevc_data->sei_size) {
-            av_freep(&codec->extradata);
-            codec->extradata_size = hevc_data->vps_size + hevc_data->sps_size +
-                                    hevc_data->pps_size + hevc_data->sei_size;
-            codec->extradata = av_malloc(codec->extradata_size +
-                                         FF_INPUT_BUFFER_PADDING_SIZE);
-            if (!codec->extradata) {
+            av_freep(&par->extradata);
+            par->extradata_size = hevc_data->vps_size + hevc_data->sps_size +
+                                  hevc_data->pps_size + hevc_data->sei_size;
+            par->extradata = av_malloc(par->extradata_size +
+                                       AV_INPUT_BUFFER_PADDING_SIZE);
+            if (!par->extradata) {
                 ret = AVERROR(ENOMEM);
-                codec->extradata_size = 0;
+                par->extradata_size = 0;
             } else {
                 int pos = 0;
-                memcpy(codec->extradata + pos, hevc_data->vps, hevc_data->vps_size);
+                memcpy(par->extradata + pos, hevc_data->vps, hevc_data->vps_size);
                 pos += hevc_data->vps_size;
-                memcpy(codec->extradata + pos, hevc_data->sps, hevc_data->sps_size);
+                memcpy(par->extradata + pos, hevc_data->sps, hevc_data->sps_size);
                 pos += hevc_data->sps_size;
-                memcpy(codec->extradata + pos, hevc_data->pps, hevc_data->pps_size);
+                memcpy(par->extradata + pos, hevc_data->pps, hevc_data->pps_size);
                 pos += hevc_data->pps_size;
-                memcpy(codec->extradata + pos, hevc_data->sei, hevc_data->sei_size);
+                memcpy(par->extradata + pos, hevc_data->sei, hevc_data->sei_size);
                 pos += hevc_data->sei_size;
-                memset(codec->extradata + pos, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+                memset(par->extradata + pos, 0, AV_INPUT_BUFFER_PADDING_SIZE);
             }
 
             av_freep(&hevc_data->vps);
@@ -278,19 +197,19 @@ static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx
     }
 
     /*
-      decode the HEVC payload header according to section 4 of draft version 6:
-
-         0                   1
-         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
-        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-        |F|   Type    |  LayerId  | TID |
-        +-------------+-----------------+
-
-           Forbidden zero (F): 1 bit
-           NAL unit type (Type): 6 bits
-           NUH layer ID (LayerId): 6 bits
-           NUH temporal ID plus 1 (TID): 3 bits
-    */
+     * decode the HEVC payload header according to section 4 of draft version 6:
+     *
+     *    0                   1
+     *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+     *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+     *   |F|   Type    |  LayerId  | TID |
+     *   +-------------+-----------------+
+     *
+     *      Forbidden zero (F): 1 bit
+     *      NAL unit type (Type): 6 bits
+     *      NUH layer ID (LayerId): 6 bits
+     *      NUH temporal ID plus 1 (TID): 3 bits
+     */
     nal_type =  (buf[0] >> 1) & 0x3f;
     lid  = ((buf[0] << 5) & 0x20) | ((buf[1] >> 3) & 0x1f);
     tid  =   buf[1] & 0x07;
@@ -298,7 +217,7 @@ static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx
     /* sanity check for correct layer ID */
     if (lid) {
         /* future scalable or 3D video coding extensions */
-        avpriv_report_missing_feature(ctx, "Multi-layer HEVC coding\n");
+        avpriv_report_missing_feature(ctx, "Multi-layer HEVC coding");
         return AVERROR_PATCHWELCOME;
     }
 
@@ -315,19 +234,6 @@ static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx
     }
 
     switch (nal_type) {
-    /* aggregated packets (AP) */
-    case 48:
-        /* pass the HEVC payload header */
-        buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
-        len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
-
-        /* pass the HEVC DONL field */
-        if (rtp_hevc_ctx->using_donl_field) {
-            buf += RTP_HEVC_DONL_FIELD_SIZE;
-            len -= RTP_HEVC_DONL_FIELD_SIZE;
-        }
-
-        /* fall-through */
     /* video parameter set (VPS) */
     case 32:
     /* sequence parameter set (SPS) */
@@ -338,14 +244,6 @@ static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx
     case 39:
     /* single NAL unit packet */
     default:
-        /* sanity check for size of input packet: 1 byte payload at least */
-        if (len < 1) {
-            av_log(ctx, AV_LOG_ERROR,
-                   "Too short RTP/HEVC packet, got %d bytes of NAL unit type %d\n",
-                   len, nal_type);
-            return AVERROR_INVALIDDATA;
-        }
-
         /* create A/V packet */
         if ((res = av_new_packet(pkt, sizeof(start_sequence) + len)) < 0)
             return res;
@@ -355,6 +253,25 @@ static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx
         memcpy(pkt->data + sizeof(start_sequence), buf, len);
 
         break;
+    /* aggregated packet (AP) - with two or more NAL units */
+    case 48:
+        /* pass the HEVC payload header */
+        buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
+        len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
+
+        /* pass the HEVC DONL field */
+        if (rtp_hevc_ctx->using_donl_field) {
+            buf += RTP_HEVC_DONL_FIELD_SIZE;
+            len -= RTP_HEVC_DONL_FIELD_SIZE;
+        }
+
+        res = ff_h264_handle_aggregated_packet(ctx, pkt, buf, len,
+                                               rtp_hevc_ctx->using_donl_field ?
+                                               RTP_HEVC_DOND_FIELD_SIZE : 0,
+                                               NULL, 0);
+        if (res < 0)
+            return res;
+        break;
     /* fragmentation unit (FU) */
     case 49:
         /* pass the HEVC payload header */
@@ -362,17 +279,17 @@ static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx
         len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
 
         /*
-             decode the FU header
-
-              0 1 2 3 4 5 6 7
-             +-+-+-+-+-+-+-+-+
-             |S|E|  FuType   |
-             +---------------+
-
-                Start fragment (S): 1 bit
-                End fragment (E): 1 bit
-                FuType: 6 bits
-        */
+         *    decode the FU header
+         *
+         *     0 1 2 3 4 5 6 7
+         *    +-+-+-+-+-+-+-+-+
+         *    |S|E|  FuType   |
+         *    +---------------+
+         *
+         *       Start fragment (S): 1 bit
+         *       End fragment (E): 1 bit
+         *       FuType: 6 bits
+         */
         first_fragment = buf[0] & 0x80;
         last_fragment  = buf[0] & 0x40;
         fu_type        = buf[0] & 0x3f;
@@ -387,48 +304,32 @@ static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx
             len -= RTP_HEVC_DONL_FIELD_SIZE;
         }
 
-        av_dlog(ctx, " FU type %d with %d bytes\n", fu_type, len);
-
-        if (len > 0) {
-            new_nal_header[0] = (rtp_pl[0] & 0x81) | (fu_type << 1);
-            new_nal_header[1] = rtp_pl[1];
-
-            /* start fragment vs. subsequent fragments */
-            if (first_fragment) {
-                if (!last_fragment) {
-                    /* create A/V packet which is big enough */
-                    if ((res = av_new_packet(pkt, sizeof(start_sequence) + sizeof(new_nal_header) + len)) < 0)
-                        return res;
-                    /* A/V packet: copy start sequence */
-                    memcpy(pkt->data, start_sequence, sizeof(start_sequence));
-                    /* A/V packet: copy new NAL header */
-                    memcpy(pkt->data + sizeof(start_sequence), new_nal_header, sizeof(new_nal_header));
-                    /* A/V packet: copy NAL unit data */
-                    memcpy(pkt->data + sizeof(start_sequence) + sizeof(new_nal_header), buf, len);
-                } else {
-                    av_log(ctx, AV_LOG_ERROR, "Illegal combination of S and E bit in RTP/HEVC packet\n");
-                    res = AVERROR_INVALIDDATA;
-                }
-            } else {
-                /* create A/V packet */
-                if ((res = av_new_packet(pkt, len)) < 0)
-                    return res;
-                /* A/V packet: copy NAL unit data */
-                memcpy(pkt->data, buf, len);
-            }
-        } else {
+        av_log(ctx, AV_LOG_TRACE, " FU type %d with %d bytes\n", fu_type, len);
+
+        if (len <= 0) {
             /* sanity check for size of input packet: 1 byte payload at least */
             av_log(ctx, AV_LOG_ERROR,
                    "Too short RTP/HEVC packet, got %d bytes of NAL unit type %d\n",
                    len, nal_type);
-            res = AVERROR_INVALIDDATA;
+            return AVERROR_INVALIDDATA;
+        }
+
+        if (first_fragment && last_fragment) {
+            av_log(ctx, AV_LOG_ERROR, "Illegal combination of S and E bit in RTP/HEVC packet\n");
+            return AVERROR_INVALIDDATA;
         }
 
+        new_nal_header[0] = (rtp_pl[0] & 0x81) | (fu_type << 1);
+        new_nal_header[1] = rtp_pl[1];
+
+        res = ff_h264_handle_frag_packet(pkt, buf, len, first_fragment,
+                                         new_nal_header, sizeof(new_nal_header));
+
         break;
     /* PACI packet */
     case 50:
         /* Temporal scalability control information (TSCI) */
-        avpriv_report_missing_feature(ctx, "PACI packets for RTP/HEVC\n");
+        avpriv_report_missing_feature(ctx, "PACI packets for RTP/HEVC");
         res = AVERROR_PATCHWELCOME;
         break;
     }
@@ -442,9 +343,8 @@ RTPDynamicProtocolHandler ff_hevc_dynamic_handler = {
     .enc_name         = "H265",
     .codec_type       = AVMEDIA_TYPE_VIDEO,
     .codec_id         = AV_CODEC_ID_HEVC,
-    .init             = hevc_init,
+    .need_parsing     = AVSTREAM_PARSE_FULL,
+    .priv_data_size   = sizeof(PayloadContext),
     .parse_sdp_a_line = hevc_parse_sdp_line,
-    .alloc            = hevc_new_context,
-    .free             = hevc_free_context,
-    .parse_packet     = hevc_handle_packet
+    .parse_packet     = hevc_handle_packet,
 };