[FFmpeg-devel] [PATCH 10/17] avformat/sdp: Simplify creating H.264 media attributes

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Thu Jul 9 22:20:15 EEST 2020


by using ff_avc_parse_nalu() which means that one no longer has to
take care of finding both the start as well as the end of a NAL unit
separately.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavformat/sdp.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/libavformat/sdp.c b/libavformat/sdp.c
index 023c88a583..bc0c39c8b5 100644
--- a/libavformat/sdp.c
+++ b/libavformat/sdp.c
@@ -156,11 +156,10 @@ static int sdp_get_address(char *dest_addr, int size, int *ttl, const char *url)
 static char *extradata2psets(AVFormatContext *s, AVCodecParameters *par)
 {
     char *psets, *p;
-    const uint8_t *r;
     static const char pset_string[] = "; sprop-parameter-sets=";
     static const char profile_string[] = "; profile-level-id=";
-    uint8_t *extradata = par->extradata;
-    int extradata_size = par->extradata_size;
+    const uint8_t *r = par->extradata, *end = r + par->extradata_size;
+    const uint8_t *nal, *nal_end = NULL;
     uint8_t *tmpbuf = NULL;
     const uint8_t *sps = NULL;
 
@@ -170,10 +169,12 @@ static char *extradata2psets(AVFormatContext *s, AVCodecParameters *par)
         return NULL;
     }
     if (par->extradata[0] == 1) {
-        if (ff_avc_write_annexb_extradata(par->extradata, &extradata,
+        int extradata_size = par->extradata_size;
+        if (ff_avc_write_annexb_extradata(par->extradata, &tmpbuf,
                                           &extradata_size))
             return NULL;
-        tmpbuf = extradata;
+        r   = tmpbuf;
+        end = r + extradata_size;
     }
 
     psets = av_mallocz(MAX_PSET_SIZE);
@@ -184,34 +185,29 @@ static char *extradata2psets(AVFormatContext *s, AVCodecParameters *par)
     }
     memcpy(psets, pset_string, strlen(pset_string));
     p = psets + strlen(pset_string);
-    r = ff_avc_find_startcode(extradata, extradata + extradata_size);
-    while (r < extradata + extradata_size) {
-        const uint8_t *r1;
-        uint8_t nal_type;
-
-        while (!*(r++));
-        nal_type = *r & 0x1f;
-        r1 = ff_avc_find_startcode(r, extradata + extradata_size);
+    while (nal = ff_avc_parse_nalu(&r, &nal_end, end)) {
+        uint8_t nal_type = *nal & 0x1f;
+        ptrdiff_t size   = nal_end - nal;
+
         if (nal_type != 7 && nal_type != 8) { /* Only output SPS and PPS */
-            r = r1;
             continue;
         }
         if (p != (psets + strlen(pset_string))) {
             *p = ',';
             p++;
         }
-        if (nal_type == H264_NAL_SPS && !sps && r1 - r >= 4)
-            sps = r;
+        if (nal_type == H264_NAL_SPS && !sps && size >= 4)
+            sps = nal;
 
-        if (!av_base64_encode(p, MAX_PSET_SIZE - (p - psets), r, r1 - r)) {
-            av_log(s, AV_LOG_ERROR, "Cannot Base64-encode %"PTRDIFF_SPECIFIER" %"PTRDIFF_SPECIFIER"!\n", MAX_PSET_SIZE - (p - psets), r1 - r);
+        if (!av_base64_encode(p, MAX_PSET_SIZE - (p - psets), nal, size)) {
+            av_log(s, AV_LOG_ERROR, "Cannot Base64-encode %"PTRDIFF_SPECIFIER" "
+                   "%"PTRDIFF_SPECIFIER"!\n", MAX_PSET_SIZE - (p - psets), size);
             av_free(psets);
             av_free(tmpbuf);
 
             return NULL;
         }
         p += strlen(p);
-        r = r1;
     }
     if (sps && p - psets <= MAX_PSET_SIZE - strlen(profile_string) - 7) {
         memcpy(p, profile_string, strlen(profile_string));
-- 
2.20.1



More information about the ffmpeg-devel mailing list