[vlc-commits] h264_nal: fix h264_get_spspps

Thomas Guillem git at videolan.org
Wed Aug 19 17:40:15 CEST 2015


vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Wed Aug 19 16:03:47 2015 +0200| [f44100deacd1514a2f9e875a04a9f225fcd618f2] | committer: Thomas Guillem

h264_nal: fix h264_get_spspps

Don't end the NAL unit when we reach 0x000000. Refactor since we don't need to
parse separately the start and the end of the NAL unit anymore.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=f44100deacd1514a2f9e875a04a9f225fcd618f2
---

 modules/packetizer/h264_nal.c |   81 +++++++++++++++++++----------------------
 1 file changed, 37 insertions(+), 44 deletions(-)

diff --git a/modules/packetizer/h264_nal.c b/modules/packetizer/h264_nal.c
index 15dda26..227433d 100644
--- a/modules/packetizer/h264_nal.c
+++ b/modules/packetizer/h264_nal.c
@@ -322,59 +322,52 @@ int h264_get_spspps( uint8_t *p_buf, size_t i_buf,
     {
         unsigned int i_move = 1;
 
-        if( i_nal_type == NAL_UNKNOWN )
+        /* cf B.1.1: a NAL unit starts and ends with 0x000001 or 0x00000001 */
+        if( i_buf > 3 && !memcmp( p_buf, annexb_startcode, 3 ) )
         {
-            if( i_buf > 4 && !memcmp( p_buf, annexb_startcode, 3 ) )
-            {
-                i_nal_type = p_buf[3] & 0x1F;
-                i_move = 4;
-
-                /* The start prefix is always 0x00000001 (annexb_startcode + a
-                 * leading zero byte) for SPS, PPS or the first NAL */
-                if( !b_has_zero_byte && ( b_first_nal || i_nal_type == NAL_SPS
-                 || i_nal_type == NAL_PPS ) )
-                    return -1;
-                b_first_nal = false;
-
-                /* Pointer to the beginning of the SPS/PPS starting with the
-                 * leading zero byte */
-                if( i_nal_type == NAL_SPS && !p_sps )
-                    p_sps = p_buf - 1;
-                if( i_nal_type == NAL_PPS && !p_pps )
-                    p_pps = p_buf - 1;
-
-                /* cf. 7.4.1.2.3 */
-                if( i_nal_type > 18 || ( i_nal_type >= 10 && i_nal_type <= 12 ) )
-                    return -1;
-
-                /* SPS/PPS are before the slices */
-                if ( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
-                    break;
-
-            } else if( b_first_nal && p_buf[0] != 0 )
-            {
-                /* leading_zero_8bits only before the first NAL */
-                return -1;
-            }
-        }
-        else
-        {
-            /* cf B.3-3: a NAL unit ends with 0x000000, 0x000001 or with the
-             * end of the bytestream */
-            if( i_buf > 3 && p_buf[0] == 0 && p_buf[1] == 0
-             && ( p_buf[2] == 0 || p_buf[2] == 1 ) )
+            if( i_nal_type != NAL_UNKNOWN )
             {
                 /* update SPS/PPS size */
                 if( i_nal_type == NAL_SPS )
-                    i_sps_size = p_buf - p_sps;
+                    i_sps_size = p_buf - p_sps - (b_has_zero_byte ? 1 : 0);
                 if( i_nal_type == NAL_PPS )
-                    i_pps_size = p_buf - p_pps;
+                    i_pps_size = p_buf - p_pps - (b_has_zero_byte ? 1 : 0);
 
                 if( i_sps_size && i_pps_size )
                     break;
-                i_nal_type = NAL_UNKNOWN;
-                i_move = 0;
             }
+
+            if (i_buf < 4)
+                return -1;
+            i_nal_type = p_buf[3] & 0x1F;
+
+            /* The start prefix is always 0x00000001 (annexb_startcode + a
+             * leading zero byte) for SPS, PPS or the first NAL */
+            if( !b_has_zero_byte && ( b_first_nal || i_nal_type == NAL_SPS
+             || i_nal_type == NAL_PPS ) )
+                return -1;
+            b_first_nal = false;
+
+            /* Pointer to the beginning of the SPS/PPS starting with the
+             * leading zero byte */
+            if( i_nal_type == NAL_SPS && !p_sps )
+                p_sps = p_buf - 1;
+            if( i_nal_type == NAL_PPS && !p_pps )
+                p_pps = p_buf - 1;
+
+            /* cf. 7.4.1.2.3 */
+            if( i_nal_type > 18 || ( i_nal_type >= 10 && i_nal_type <= 12 ) )
+                return -1;
+
+            /* SPS/PPS are before the slices */
+            if ( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
+                break;
+            i_move = 4;
+        }
+        else if( b_first_nal && p_buf[0] != 0 )
+        {
+            /* leading_zero_8bits only before the first NAL */
+            return -1;
         }
         b_has_zero_byte = *p_buf == 0;
         i_buf -= i_move;



More information about the vlc-commits mailing list