[FFmpeg-cvslog] h264_parser: switch to h2645_parse for NAL unescaping

Anton Khirnov git at videolan.org
Tue May 17 14:24:47 CEST 2016


ffmpeg | branch: master | Anton Khirnov <anton at khirnov.net> | Mon Mar 21 15:38:31 2016 +0100| [8d0cc8ca97678f4ca87948ebabcbaab5a4f4c1f6] | committer: Anton Khirnov

h264_parser: switch to h2645_parse for NAL unescaping

Remove now unused ff_h264_decode_nal().

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=8d0cc8ca97678f4ca87948ebabcbaab5a4f4c1f6
---

 libavcodec/Makefile      |    2 +-
 libavcodec/h264.c        |  106 ----------------------------------------------
 libavcodec/h264.h        |   11 -----
 libavcodec/h264_parser.c |   37 +++++++++++-----
 4 files changed, 28 insertions(+), 128 deletions(-)

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index d9f614d..7181db7 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -728,7 +728,7 @@ OBJS-$(CONFIG_FLAC_PARSER)             += flac_parser.o flacdata.o flac.o
 OBJS-$(CONFIG_GSM_PARSER)              += gsm_parser.o
 OBJS-$(CONFIG_H261_PARSER)             += h261_parser.o
 OBJS-$(CONFIG_H263_PARSER)             += h263_parser.o
-OBJS-$(CONFIG_H264_PARSER)             += h264_parser.o h264_parse.o
+OBJS-$(CONFIG_H264_PARSER)             += h264_parser.o h264_parse.o h2645_parse.o
 OBJS-$(CONFIG_HEVC_PARSER)             += hevc_parser.o h2645_parse.o hevc_ps.o hevc_data.o
 OBJS-$(CONFIG_MJPEG_PARSER)            += mjpeg_parser.o
 OBJS-$(CONFIG_MLP_PARSER)              += mlp_parser.o mlp.o
diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index acda35b..da7f88f 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -116,112 +116,6 @@ void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
     }
 }
 
-const uint8_t *ff_h264_decode_nal(H264Context *h, H264SliceContext *sl,
-                                  const uint8_t *src,
-                                  int *dst_length, int *consumed, int length)
-{
-    int i, si, di;
-    uint8_t *dst;
-
-    // src[0]&0x80; // forbidden bit
-    h->nal_ref_idc   = src[0] >> 5;
-    h->nal_unit_type = src[0] & 0x1F;
-
-    src++;
-    length--;
-
-#define STARTCODE_TEST                                                  \
-    if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {         \
-        if (src[i + 2] != 3) {                                          \
-            /* startcode, so we must be past the end */                 \
-            length = i;                                                 \
-        }                                                               \
-        break;                                                          \
-    }
-
-#if HAVE_FAST_UNALIGNED
-#define FIND_FIRST_ZERO                                                 \
-    if (i > 0 && !src[i])                                               \
-        i--;                                                            \
-    while (src[i])                                                      \
-        i++
-
-#if HAVE_FAST_64BIT
-    for (i = 0; i + 1 < length; i += 9) {
-        if (!((~AV_RN64A(src + i) &
-               (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
-              0x8000800080008080ULL))
-            continue;
-        FIND_FIRST_ZERO;
-        STARTCODE_TEST;
-        i -= 7;
-    }
-#else
-    for (i = 0; i + 1 < length; i += 5) {
-        if (!((~AV_RN32A(src + i) &
-               (AV_RN32A(src + i) - 0x01000101U)) &
-              0x80008080U))
-            continue;
-        FIND_FIRST_ZERO;
-        STARTCODE_TEST;
-        i -= 3;
-    }
-#endif
-#else
-    for (i = 0; i + 1 < length; i += 2) {
-        if (src[i])
-            continue;
-        if (i > 0 && src[i - 1] == 0)
-            i--;
-        STARTCODE_TEST;
-    }
-#endif
-
-    if (i >= length - 1) { // no escaped 0
-        *dst_length = length;
-        *consumed   = length + 1; // +1 for the header
-        return src;
-    }
-
-    av_fast_malloc(&sl->rbsp_buffer, &sl->rbsp_buffer_size,
-                   length + AV_INPUT_BUFFER_PADDING_SIZE);
-    dst = sl->rbsp_buffer;
-
-    if (!dst)
-        return NULL;
-
-    memcpy(dst, src, i);
-    si = di = i;
-    while (si + 2 < length) {
-        // remove escapes (very rare 1:2^22)
-        if (src[si + 2] > 3) {
-            dst[di++] = src[si++];
-            dst[di++] = src[si++];
-        } else if (src[si] == 0 && src[si + 1] == 0) {
-            if (src[si + 2] == 3) { // escape
-                dst[di++]  = 0;
-                dst[di++]  = 0;
-                si        += 3;
-                continue;
-            } else // next start code
-                goto nsc;
-        }
-
-        dst[di++] = src[si++];
-    }
-    while (si < length)
-        dst[di++] = src[si++];
-
-nsc:
-    memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
-
-    *dst_length = di;
-    *consumed   = si + 1; // +1 for the header
-    /* FIXME store exact number of bits in the getbitcontext
-     * (it is needed for decoding) */
-    return dst;
-}
-
 void ff_h264_free_tables(H264Context *h)
 {
     int i;
diff --git a/libavcodec/h264.h b/libavcodec/h264.h
index 796d5d5..cce973f 100644
--- a/libavcodec/h264.h
+++ b/libavcodec/h264.h
@@ -769,17 +769,6 @@ int ff_h264_get_profile(SPS *sps);
 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
 
 /**
- * Decode a network abstraction layer unit.
- * @param consumed is the number of bytes used as input
- * @param length is the length of the array
- * @param dst_length is the number of decoded bytes FIXME here
- *                   or a decode rbsp tailing?
- * @return decoded bytes, might be src+1 if no escapes
- */
-const uint8_t *ff_h264_decode_nal(H264Context *h, H264SliceContext *sl, const uint8_t *src,
-                                  int *dst_length, int *consumed, int length);
-
-/**
  * Free any data that may have been allocated in the H264 context
  * like SPS, PPS etc.
  */
diff --git a/libavcodec/h264_parser.c b/libavcodec/h264_parser.c
index b5ccce3..bcbaf1a 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -195,11 +195,14 @@ static inline int parse_nal_units(AVCodecParserContext *s,
     H264Context      *h = &p->h;
     H264SliceContext *sl = &h->slice_ctx[0];
     const uint8_t *buf_end = buf + buf_size;
+
+    H2645NAL nal = { NULL };
+
     unsigned int pps_id;
     unsigned int slice_type;
     int state = -1, got_reset = 0;
-    const uint8_t *ptr;
     int field_poc[2];
+    int ret;
 
     /* set some sane default values */
     s->pict_type         = AV_PICTURE_TYPE_I;
@@ -213,7 +216,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
         return 0;
 
     for (;;) {
-        int src_length, dst_length, consumed;
+        int src_length, consumed;
         buf = avpriv_find_start_code(buf, buf_end, &state);
         if (buf >= buf_end)
             break;
@@ -235,11 +238,22 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             }
             break;
         }
-        ptr = ff_h264_decode_nal(h, sl, buf, &dst_length, &consumed, src_length);
-        if (!ptr || dst_length < 0)
+
+        consumed = ff_h2645_extract_rbsp(buf, src_length, &nal);
+        if (consumed < 0)
             break;
 
-        init_get_bits(&h->gb, ptr, 8 * dst_length);
+        ret = init_get_bits(&nal.gb, nal.data, nal.size * 8);
+        if (ret < 0)
+            goto fail;
+        get_bits1(&nal.gb);
+        nal.ref_idc = get_bits(&nal.gb, 2);
+        nal.type    = get_bits(&nal.gb, 5);
+
+        h->gb            = nal.gb;
+        h->nal_ref_idc   = nal.ref_idc;
+        h->nal_unit_type = nal.type;
+
         switch (h->nal_unit_type) {
         case NAL_SPS:
             ff_h264_decode_seq_parameter_set(h);
@@ -259,7 +273,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             h->prev_poc_lsb          = 0;
         /* fall through */
         case NAL_SLICE:
-            init_get_bits(&sl->gb, ptr, 8 * dst_length);
+            sl->gb = nal.gb;
             get_ue_golomb(&sl->gb);  // skip first_mb_in_slice
             slice_type   = get_ue_golomb_31(&sl->gb);
             s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
@@ -271,18 +285,18 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             if (pps_id >= MAX_PPS_COUNT) {
                 av_log(h->avctx, AV_LOG_ERROR,
                        "pps_id %u out of range\n", pps_id);
-                return -1;
+                goto fail;
             }
             if (!h->pps_buffers[pps_id]) {
                 av_log(h->avctx, AV_LOG_ERROR,
                        "non-existing PPS %u referenced\n", pps_id);
-                return -1;
+                goto fail;
             }
             h->pps = *h->pps_buffers[pps_id];
             if (!h->sps_buffers[h->pps.sps_id]) {
                 av_log(h->avctx, AV_LOG_ERROR,
                        "non-existing SPS %u referenced\n", h->pps.sps_id);
-                return -1;
+                goto fail;
             }
             h->sps       = *h->sps_buffers[h->pps.sps_id];
             h->frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num);
@@ -360,7 +374,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
                 got_reset = scan_mmco_reset(s);
                 if (got_reset < 0)
-                    return got_reset;
+                    goto fail;
             }
 
             /* Set up the prev_ values for decoding POC of the next picture. */
@@ -438,12 +452,15 @@ static inline int parse_nal_units(AVCodecParserContext *s,
                 s->field_order = AV_FIELD_UNKNOWN;
             }
 
+            av_freep(&nal.rbsp_buffer);
             return 0; /* no need to evaluate the rest */
         }
         buf += consumed;
     }
     /* didn't find a picture! */
     av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
+fail:
+    av_freep(&nal.rbsp_buffer);
     return -1;
 }
 



More information about the ffmpeg-cvslog mailing list