[vlc-devel] [PATCH 3/5] add convert_hevc_nal_units in hevc_nal.c

Thomas Guillem thomas.guillem at gmail.com
Fri Oct 3 16:53:41 CEST 2014


utility function to convert hvcC extradata to annex b.
---
 modules/codec/Makefile.am |   3 +-
 modules/codec/hevc_nal.c  | 107 ++++++++++++++++++++++++++++++++++++++++++++++
 modules/codec/hevc_nal.h  |  38 ++++++++++++++++
 3 files changed, 147 insertions(+), 1 deletion(-)
 create mode 100644 modules/codec/hevc_nal.c
 create mode 100644 modules/codec/hevc_nal.h

diff --git a/modules/codec/Makefile.am b/modules/codec/Makefile.am
index b65fa37..48d8a7d 100644
--- a/modules/codec/Makefile.am
+++ b/modules/codec/Makefile.am
@@ -353,6 +353,7 @@ noinst_HEADERS += \
 libomxil_plugin_la_SOURCES = \
 	codec/omxil/utils.c codec/omxil/omxil_utils.h \
 	codec/h264_nal.c codec/h264_nal.h \
+	codec/hevc_nal.c codec/hevc_nal.h \
 	codec/omxil/qcom.c codec/omxil/qcom.h \
 	codec/omxil/omxil.c codec/omxil/omxil.h codec/omxil/omxil_core.c codec/omxil/omxil_core.h \
 	video_chroma/copy.c
@@ -372,7 +373,7 @@ libiomx_plugin_la_LIBADD = $(libomxil_plugin_la_LIBADD)
 libmediacodec_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/codec/omxil
 libmediacodec_plugin_la_SOURCES = codec/omxil/android_mediacodec.c codec/omxil/utils.c \
 	video_chroma/copy.c codec/omxil/android_opaque.c codec/omxil/android_opaque.h \
-	codec/h264_nal.c codec/h264_nal.h
+	codec/h264_nal.c codec/h264_nal.h codec/hevc_nal.c codec/hevc_nal.h
 
 codec_LTLIBRARIES += $(LTLIBomxil) $(LTLIBomxil_vout)
 EXTRA_LTLIBRARIES += libomxil_plugin.la libomxil_vout_plugin.la
diff --git a/modules/codec/hevc_nal.c b/modules/codec/hevc_nal.c
new file mode 100644
index 0000000..45c5218
--- /dev/null
+++ b/modules/codec/hevc_nal.c
@@ -0,0 +1,107 @@
+/*****************************************************************************
+ * Copyright © 2010-2014 VideoLAN
+ *
+ * Authors: Thomas Guillem <thomas.guillem at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#include "hevc_nal.h"
+
+#include <limits.h>
+
+/* Inspired by libavcodec/hevc.c */
+int convert_hevc_nal_units(decoder_t *p_dec, const uint8_t *p_buf,
+                           uint32_t i_buf_size, uint8_t *p_out_buf,
+                           uint32_t i_out_buf_size, uint32_t *p_sps_pps_size,
+                           uint32_t *p_nal_size)
+{
+    int i, num_arrays;
+    const uint8_t *p_end = p_buf + i_buf_size;
+    uint32_t i_sps_pps_size = 0;
+
+    if( i_buf_size <= 3 || ( !p_buf[0] && !p_buf[1] && p_buf[2] <= 1 ) )
+        return VLC_EGENERIC;
+
+    if( p_end - p_buf < 23 )
+    {
+        msg_Err( p_dec, "Input Metadata too small" );
+        return VLC_ENOMEM;
+    }
+
+    p_buf += 21;
+
+    if( p_nal_size )
+        *p_nal_size = (*p_buf & 0x03) + 1;
+    p_buf++;
+
+    num_arrays = *p_buf++;
+
+    for( i = 0; i < num_arrays; i++ )
+    {
+        int type, cnt, j;
+
+        if( p_end - p_buf < 3 )
+        {
+            msg_Err( p_dec, "Input Metadata too small" );
+            return VLC_ENOMEM;
+        }
+        type = *(p_buf++) & 0x3f;
+        VLC_UNUSED(type);
+
+        cnt = p_buf[0] << 8 | p_buf[1];
+        p_buf += 2;
+
+        for( j = 0; j < cnt; j++ )
+        {
+            int i_nal_size;
+
+            if( p_end - p_buf < 2 )
+            {
+                msg_Err( p_dec, "Input Metadata too small" );
+                return VLC_ENOMEM;
+            }
+            
+            i_nal_size = p_buf[0] << 8 | p_buf[1];
+            p_buf += 2;
+
+            if( i_nal_size < 0 || p_end - p_buf < i_nal_size )
+            {
+                msg_Err( p_dec, "NAL unit size does not match Input Metadata size" );
+                return VLC_ENOMEM;
+            }
+
+            if( i_sps_pps_size + 4 + i_nal_size > i_out_buf_size )
+            {
+                msg_Err( p_dec, "Output buffer too small" );
+                return VLC_ENOMEM;
+            }
+
+            p_out_buf[i_sps_pps_size++] = 0;
+            p_out_buf[i_sps_pps_size++] = 0;
+            p_out_buf[i_sps_pps_size++] = 0;
+            p_out_buf[i_sps_pps_size++] = 1;
+
+            memcpy(p_out_buf + i_sps_pps_size, p_buf, i_nal_size);
+            p_buf += i_nal_size;
+
+            i_sps_pps_size += i_nal_size;
+        }
+    }
+
+    *p_sps_pps_size = i_sps_pps_size;
+
+    return VLC_SUCCESS;
+}
diff --git a/modules/codec/hevc_nal.h b/modules/codec/hevc_nal.h
new file mode 100644
index 0000000..17625e9
--- /dev/null
+++ b/modules/codec/hevc_nal.h
@@ -0,0 +1,38 @@
+/*****************************************************************************
+ * Copyright © 2010-2014 VideoLAN
+ *
+ * Authors: Thomas Guillem <thomas.guillem at gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef HEVC_NAL_H
+# define HEVC_NAL_H
+
+# ifdef HAVE_CONFIG_H
+#  include "config.h"
+# endif
+
+# include <vlc_common.h>
+# include <vlc_codec.h>
+
+/* Parse the hvcC Metadata and convert it to annex b format */
+int convert_hevc_nal_units( decoder_t *p_dec, const uint8_t *p_buf,
+                            uint32_t i_buf_size, uint8_t *p_out_buf,
+                            uint32_t i_out_buf_size, uint32_t *p_sps_pps_size,
+                            uint32_t *p_nal_size);
+
+
+#endif /* HEVC_NAL_H */
-- 
2.1.0




More information about the vlc-devel mailing list