diff mbox series

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

Message ID 20200709192022.9412-2-andreas.rheinhardt@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/7] avformat/avc: Fix undefined shift and assert when reading exp-golomb num
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt July 9, 2020, 7:20 p.m. UTC
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@gmail.com>
---
 libavformat/sdp.c | 34 +++++++++++++++-------------------
 1 file changed, 15 insertions(+), 19 deletions(-)
diff mbox series

Patch

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));