]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtpdec_h264.c
blowfish: add av_blowfish_alloc()
[ffmpeg] / libavformat / rtpdec_h264.c
index 3ad461f4ad6b70c6835bd647610b073fd06ec607..e8543ee06df539b10e3e5975a132a433c2cc35c2 100644 (file)
 
 #include "libavutil/attributes.h"
 #include "libavutil/base64.h"
+#include "libavutil/intreadwrite.h"
 #include "libavutil/avstring.h"
-#include "libavcodec/get_bits.h"
 #include "avformat.h"
 
-#include "network.h"
-#include <assert.h>
-
 #include "rtpdec.h"
 #include "rtpdec_formats.h"
 
@@ -58,15 +55,18 @@ struct PayloadContext {
 
 #ifdef DEBUG
 #define COUNT_NAL_TYPE(data, nal) data->packet_types_received[(nal) & 0x1f]++
+#define NAL_COUNTERS data->packet_types_received
 #else
 #define COUNT_NAL_TYPE(data, nal) do { } while (0)
+#define NAL_COUNTERS NULL
 #endif
+#define NAL_MASK 0x1f
 
 static const uint8_t start_sequence[] = { 0, 0, 0, 1 };
 
 static void parse_profile_level_id(AVFormatContext *s,
                                    PayloadContext *h264_data,
-                                   char *value)
+                                   const char *value)
 {
     char buffer[3];
     // 6 characters=3 bytes, in hex.
@@ -93,9 +93,9 @@ static void parse_profile_level_id(AVFormatContext *s,
     h264_data->level_idc   = level_idc;
 }
 
-static int parse_sprop_parameter_sets(AVFormatContext *s,
-                                      AVCodecContext *codec,
-                                      char *value)
+int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s,
+                                       uint8_t **data_ptr, int *size_ptr,
+                                       const char *value)
 {
     char base64packet[1024];
     uint8_t decoded_packet[1024];
@@ -116,45 +116,37 @@ static int parse_sprop_parameter_sets(AVFormatContext *s,
         packet_size = av_base64_decode(decoded_packet, base64packet,
                                        sizeof(decoded_packet));
         if (packet_size > 0) {
-            uint8_t *dest = av_malloc(packet_size + sizeof(start_sequence) +
-                                      codec->extradata_size +
-                                      FF_INPUT_BUFFER_PADDING_SIZE);
+            uint8_t *dest = av_realloc(*data_ptr,
+                                       packet_size + sizeof(start_sequence) +
+                                       *size_ptr +
+                                       AV_INPUT_BUFFER_PADDING_SIZE);
             if (!dest) {
                 av_log(s, AV_LOG_ERROR,
                        "Unable to allocate memory for extradata!\n");
                 return AVERROR(ENOMEM);
             }
-            if (codec->extradata_size) {
-                memcpy(dest, codec->extradata, codec->extradata_size);
-                av_free(codec->extradata);
-            }
+            *data_ptr = dest;
 
-            memcpy(dest + codec->extradata_size, start_sequence,
+            memcpy(dest + *size_ptr, start_sequence,
                    sizeof(start_sequence));
-            memcpy(dest + codec->extradata_size + sizeof(start_sequence),
+            memcpy(dest + *size_ptr + sizeof(start_sequence),
                    decoded_packet, packet_size);
-            memset(dest + codec->extradata_size + sizeof(start_sequence) +
-                   packet_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
+            memset(dest + *size_ptr + sizeof(start_sequence) +
+                   packet_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
 
-            codec->extradata       = dest;
-            codec->extradata_size += sizeof(start_sequence) + packet_size;
+            *size_ptr += sizeof(start_sequence) + packet_size;
         }
     }
 
-    av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n",
-           codec->extradata, codec->extradata_size);
-
     return 0;
 }
 
 static int sdp_parse_fmtp_config_h264(AVFormatContext *s,
                                       AVStream *stream,
                                       PayloadContext *h264_data,
-                                      char *attr, char *value)
+                                      const char *attr, const char *value)
 {
     AVCodecContext *codec = stream->codec;
-    assert(codec->codec_id == AV_CODEC_ID_H264);
-    assert(h264_data != NULL);
 
     if (!strcmp(attr, "packetization-mode")) {
         av_log(s, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value));
@@ -173,21 +165,51 @@ static int sdp_parse_fmtp_config_h264(AVFormatContext *s,
         if (strlen(value) == 6)
             parse_profile_level_id(s, h264_data, value);
     } else if (!strcmp(attr, "sprop-parameter-sets")) {
+        int ret;
         codec->extradata_size = 0;
         av_freep(&codec->extradata);
-        return parse_sprop_parameter_sets(s, codec, value);
+        ret = ff_h264_parse_sprop_parameter_sets(s, &codec->extradata,
+                                                 &codec->extradata_size, value);
+        av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n",
+               codec->extradata, codec->extradata_size);
+        return ret;
     }
     return 0;
 }
 
-static int h264_handle_packet_stap_a(AVFormatContext *ctx, AVPacket *pkt,
-                                     const uint8_t *buf, int len)
+void ff_h264_parse_framesize(AVCodecContext *codec, const char *p)
+{
+    char buf1[50];
+    char *dst = buf1;
+
+    // remove the protocol identifier
+    while (*p && *p == ' ')
+        p++;                     // strip spaces.
+    while (*p && *p != ' ')
+        p++;                     // eat protocol identifier
+    while (*p && *p == ' ')
+        p++;                     // strip trailing spaces.
+    while (*p && *p != '-' && (dst - buf1) < sizeof(buf1) - 1)
+        *dst++ = *p++;
+    *dst = '\0';
+
+    // a='framesize:96 320-240'
+    // set our parameters
+    codec->width   = atoi(buf1);
+    codec->height  = atoi(p + 1); // skip the -
+}
+
+int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, AVPacket *pkt,
+                                     const uint8_t *buf, int len,
+                                     int skip_between, int *nal_counters,
+                                     int nal_mask)
 {
     int pass         = 0;
     int total_length = 0;
     uint8_t *dst     = NULL;
     int ret;
 
+    // first we are going to figure out the total size
     for (pass = 0; pass < 2; pass++) {
         const uint8_t *src = buf;
         int src_len        = len;
@@ -205,25 +227,22 @@ static int h264_handle_packet_stap_a(AVFormatContext *ctx, AVPacket *pkt,
                     total_length += sizeof(start_sequence) + nal_size;
                 } else {
                     // copying
-                    assert(dst);
                     memcpy(dst, start_sequence, sizeof(start_sequence));
                     dst += sizeof(start_sequence);
                     memcpy(dst, src, nal_size);
-                    COUNT_NAL_TYPE(data, *src);
+                    if (nal_counters)
+                        nal_counters[(*src) & nal_mask]++;
                     dst += nal_size;
                 }
             } else {
                 av_log(ctx, AV_LOG_ERROR,
                        "nal size exceeds length: %d %d\n", nal_size, src_len);
+                return AVERROR_INVALIDDATA;
             }
 
             // eat what we handled
-            src     += nal_size;
-            src_len -= nal_size;
-
-            if (src_len < 0)
-                av_log(ctx, AV_LOG_ERROR,
-                       "Consumed more bytes than we got! (%d)\n", src_len);
+            src     += nal_size + skip_between;
+            src_len -= nal_size + skip_between;
         }
 
         if (pass == 0) {
@@ -232,14 +251,59 @@ static int h264_handle_packet_stap_a(AVFormatContext *ctx, AVPacket *pkt,
             if ((ret = av_new_packet(pkt, total_length)) < 0)
                 return ret;
             dst = pkt->data;
-        } else {
-            assert(dst - pkt->data == total_length);
         }
     }
 
     return 0;
 }
 
+int ff_h264_handle_frag_packet(AVPacket *pkt, const uint8_t *buf, int len,
+                               int start_bit, const uint8_t *nal_header,
+                               int nal_header_len)
+{
+    int ret;
+    int tot_len = len;
+    int pos = 0;
+    if (start_bit)
+        tot_len += sizeof(start_sequence) + nal_header_len;
+    if ((ret = av_new_packet(pkt, tot_len)) < 0)
+        return ret;
+    if (start_bit) {
+        memcpy(pkt->data + pos, start_sequence, sizeof(start_sequence));
+        pos += sizeof(start_sequence);
+        memcpy(pkt->data + pos, nal_header, nal_header_len);
+        pos += nal_header_len;
+    }
+    memcpy(pkt->data + pos, buf, len);
+    return 0;
+}
+
+static int h264_handle_packet_fu_a(AVFormatContext *ctx, AVPacket *pkt,
+                                   const uint8_t *buf, int len,
+                                   int *nal_counters, int nal_mask)
+{
+    uint8_t fu_indicator, fu_header, start_bit, nal_type, nal;
+
+    if (len < 3) {
+        av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H264 RTP packet\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    fu_indicator = buf[0];
+    fu_header    = buf[1];
+    start_bit    = fu_header >> 7;
+    nal_type     = fu_header & 0x1f;
+    nal          = fu_indicator & 0xe0 | nal_type;
+
+    // skip the fu_indicator and fu_header
+    buf += 2;
+    len -= 2;
+
+    if (start_bit && nal_counters)
+        nal_counters[nal_type & nal_mask]++;
+    return ff_h264_handle_frag_packet(pkt, buf, len, start_bit, &nal, 1);
+}
+
 // return 0 on packet, no more left, 1 on packet, 1 on partial packet
 static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
@@ -257,9 +321,6 @@ static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
     nal  = buf[0];
     type = nal & 0x1f;
 
-    assert(data);
-    assert(buf);
-
     /* Simplify the case (these are all the nal types used internally by
      * the h264 codec). */
     if (type >= 1 && type <= 23)
@@ -278,8 +339,8 @@ static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
         // consume the STAP-A NAL
         buf++;
         len--;
-        // first we are going to figure out the total size
-        result = h264_handle_packet_stap_a(ctx, pkt, buf, len);
+        result = ff_h264_handle_aggregated_packet(ctx, pkt, buf, len, 0,
+                                                  NAL_COUNTERS, NAL_MASK);
         break;
 
     case 25:                   // STAP-B
@@ -287,51 +348,14 @@ static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
     case 27:                   // MTAP-24
     case 29:                   // FU-B
         av_log(ctx, AV_LOG_ERROR,
-               "Unhandled type (%d) (See RFC for implementation details\n",
+               "Unhandled type (%d) (See RFC for implementation details)\n",
                type);
         result = AVERROR(ENOSYS);
         break;
 
     case 28:                   // FU-A (fragmented nal)
-        buf++;
-        len--;                 // skip the fu_indicator
-        if (len > 1) {
-            // these are the same as above, we just redo them here for clarity
-            uint8_t fu_indicator      = nal;
-            uint8_t fu_header         = *buf;
-            uint8_t start_bit         = fu_header >> 7;
-            uint8_t av_unused end_bit = (fu_header & 0x40) >> 6;
-            uint8_t nal_type          = fu_header & 0x1f;
-            uint8_t reconstructed_nal;
-
-            // Reconstruct this packet's true nal; only the data follows.
-            /* The original nal forbidden bit and NRI are stored in this
-             * packet's nal. */
-            reconstructed_nal  = fu_indicator & 0xe0;
-            reconstructed_nal |= nal_type;
-
-            // skip the fu_header
-            buf++;
-            len--;
-
-            if (start_bit)
-                COUNT_NAL_TYPE(data, nal_type);
-            if (start_bit) {
-                /* copy in the start sequence, and the reconstructed nal */
-                if ((result = av_new_packet(pkt, sizeof(start_sequence) + sizeof(nal) + len)) < 0)
-                    return result;
-                memcpy(pkt->data, start_sequence, sizeof(start_sequence));
-                pkt->data[sizeof(start_sequence)] = reconstructed_nal;
-                memcpy(pkt->data + sizeof(start_sequence) + sizeof(nal), buf, len);
-            } else {
-                if ((result = av_new_packet(pkt, len)) < 0)
-                    return result;
-                memcpy(pkt->data, buf, len);
-            }
-        } else {
-            av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H264 RTP packet\n");
-            result = AVERROR_INVALIDDATA;
-        }
+        result = h264_handle_packet_fu_a(ctx, pkt, buf, len,
+                                         NAL_COUNTERS, NAL_MASK);
         break;
 
     case 30:                   // undefined
@@ -347,12 +371,7 @@ static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
     return result;
 }
 
-static PayloadContext *h264_new_context(void)
-{
-    return av_mallocz(sizeof(PayloadContext) + FF_INPUT_BUFFER_PADDING_SIZE);
-}
-
-static void h264_free_context(PayloadContext *data)
+static void h264_close_context(PayloadContext *data)
 {
 #ifdef DEBUG
     int ii;
@@ -363,51 +382,21 @@ static void h264_free_context(PayloadContext *data)
                    data->packet_types_received[ii], ii);
     }
 #endif
-
-    av_free(data);
-}
-
-static av_cold int h264_init(AVFormatContext *s, int st_index,
-                             PayloadContext *data)
-{
-    if (st_index < 0)
-        return 0;
-    s->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
-    return 0;
 }
 
 static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
                                PayloadContext *h264_data, const char *line)
 {
     AVStream *stream;
-    AVCodecContext *codec;
     const char *p = line;
 
     if (st_index < 0)
         return 0;
 
     stream = s->streams[st_index];
-    codec  = stream->codec;
 
     if (av_strstart(p, "framesize:", &p)) {
-        char buf1[50];
-        char *dst = buf1;
-
-        // remove the protocol identifier
-        while (*p && *p == ' ')
-            p++;                     // strip spaces.
-        while (*p && *p != ' ')
-            p++;                     // eat protocol identifier
-        while (*p && *p == ' ')
-            p++;                     // strip trailing spaces.
-        while (*p && *p != '-' && (dst - buf1) < sizeof(buf1) - 1)
-            *dst++ = *p++;
-        *dst = '\0';
-
-        // a='framesize:96 320-240'
-        // set our parameters
-        codec->width   = atoi(buf1);
-        codec->height  = atoi(p + 1); // skip the -
+        ff_h264_parse_framesize(stream->codec, p);
     } else if (av_strstart(p, "fmtp:", &p)) {
         return ff_parse_fmtp(s, stream, h264_data, p, sdp_parse_fmtp_config_h264);
     } else if (av_strstart(p, "cliprect:", &p)) {
@@ -421,9 +410,9 @@ RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
     .enc_name         = "H264",
     .codec_type       = AVMEDIA_TYPE_VIDEO,
     .codec_id         = AV_CODEC_ID_H264,
-    .init             = h264_init,
+    .need_parsing     = AVSTREAM_PARSE_FULL,
+    .priv_data_size   = sizeof(PayloadContext),
     .parse_sdp_a_line = parse_h264_sdp_line,
-    .alloc            = h264_new_context,
-    .free             = h264_free_context,
-    .parse_packet     = h264_handle_packet
+    .close            = h264_close_context,
+    .parse_packet     = h264_handle_packet,
 };