[FFmpeg-cvslog] Merge commit '8d0cc8ca97678f4ca87948ebabcbaab5a4f4c1f6'

Hendrik Leppkes git at videolan.org
Tue May 17 14:24:47 CEST 2016


ffmpeg | branch: master | Hendrik Leppkes <h.leppkes at gmail.com> | Tue May 17 14:21:12 2016 +0200| [15ad0232107496c3b12d8aaedfc3398b8cda9ee7] | committer: Hendrik Leppkes

Merge commit '8d0cc8ca97678f4ca87948ebabcbaab5a4f4c1f6'

* commit '8d0cc8ca97678f4ca87948ebabcbaab5a4f4c1f6':
  h264_parser: switch to h2645_parse for NAL unescaping

Merged-by: Hendrik Leppkes <h.leppkes at gmail.com>

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

 libavcodec/Makefile      |    2 +-
 libavcodec/h264.c        |  110 ----------------------------------------------
 libavcodec/h264.h        |   11 -----
 libavcodec/h264_parser.c |   39 +++++++++++-----
 4 files changed, 28 insertions(+), 134 deletions(-)

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3f0ffd1..472e8f0 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -913,7 +913,7 @@ OBJS-$(CONFIG_G729_PARSER)             += g729_parser.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 95248af..2a522fd 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -134,116 +134,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 && src[i + 2] != 0) {                       \
-            /* 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
-
-    av_fast_padded_malloc(&sl->rbsp_buffer, &sl->rbsp_buffer_size, length+MAX_MBPAIR_SIZE);
-    dst = sl->rbsp_buffer;
-
-    if (!dst)
-        return NULL;
-
-    if(i>=length-1){ //no escaped 0
-        *dst_length= length;
-        *consumed= length+1; //+1 for the header
-        if(h->avctx->flags2 & AV_CODEC_FLAG2_FAST){
-            return src;
-        }else{
-            memcpy(dst, src, length);
-            return dst;
-        }
-    }
-
-    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 && src[si + 2] != 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 33a73ba..2659e55 100644
--- a/libavcodec/h264.h
+++ b/libavcodec/h264.h
@@ -861,17 +861,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 c86f032..6cf4a23 100644
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@ -227,13 +227,14 @@ static inline int parse_nal_units(AVCodecParserContext *s,
     H264ParseContext *p = s->priv_data;
     H264Context      *h = &p->h;
     H264SliceContext *sl = &h->slice_ctx[0];
+    H2645NAL nal = { NULL };
     int buf_index, next_avc;
     unsigned int pps_id;
     unsigned int slice_type;
     int state = -1, got_reset = 0;
-    const uint8_t *ptr;
     int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
     int field_poc[2];
+    int ret;
 
     /* set some sane default values */
     s->pict_type         = AV_PICTURE_TYPE_I;
@@ -250,7 +251,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
     buf_index     = 0;
     next_avc      = h->is_avc ? 0 : buf_size;
     for (;;) {
-        int src_length, dst_length, consumed, nalsize = 0;
+        int src_length, consumed, nalsize = 0;
 
         if (buf_index >= next_avc) {
             nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
@@ -283,14 +284,23 @@ static inline int parse_nal_units(AVCodecParserContext *s,
             }
             break;
         }
-        ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length,
-                                 &consumed, src_length);
-        if (!ptr || dst_length < 0)
+        consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &nal);
+        if (consumed < 0)
             break;
 
         buf_index += consumed;
 
-        init_get_bits(&h->gb, ptr, 8 * dst_length);
+        ret = init_get_bits8(&nal.gb, nal.data, nal.size);
+        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, 0);
@@ -310,7 +320,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_long(&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];
@@ -322,18 +332,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);
@@ -414,7 +424,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. */
@@ -492,13 +502,18 @@ 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 */
         }
     }
-    if (q264)
+    if (q264) {
+        av_freep(&nal.rbsp_buffer);
         return 0;
+    }
     /* didn't find a picture! */
     av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
+fail:
+    av_freep(&nal.rbsp_buffer);
     return -1;
 }
 


======================================================================

diff --cc libavcodec/h264_parser.c
index c86f032,bcbaf1a..6cf4a23
--- a/libavcodec/h264_parser.c
+++ b/libavcodec/h264_parser.c
@@@ -227,13 -194,15 +227,14 @@@ static inline int parse_nal_units(AVCod
      H264ParseContext *p = s->priv_data;
      H264Context      *h = &p->h;
      H264SliceContext *sl = &h->slice_ctx[0];
 -    const uint8_t *buf_end = buf + buf_size;
 -
+     H2645NAL nal = { NULL };
 -
 +    int buf_index, next_avc;
      unsigned int pps_id;
      unsigned int slice_type;
      int state = -1, got_reset = 0;
-     const uint8_t *ptr;
 +    int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
      int field_poc[2];
+     int ret;
  
      /* set some sane default values */
      s->pict_type         = AV_PICTURE_TYPE_I;
@@@ -247,26 -215,13 +248,26 @@@
      if (!buf_size)
          return 0;
  
 +    buf_index     = 0;
 +    next_avc      = h->is_avc ? 0 : buf_size;
      for (;;) {
-         int src_length, dst_length, consumed, nalsize = 0;
 -        int src_length, consumed;
 -        buf = avpriv_find_start_code(buf, buf_end, &state);
 -        if (buf >= buf_end)
 -            break;
 -        --buf;
 -        src_length = buf_end - buf;
++        int src_length, consumed, nalsize = 0;
 +
 +        if (buf_index >= next_avc) {
 +            nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
 +            if (nalsize < 0)
 +                break;
 +            next_avc = buf_index + nalsize;
 +        } else {
 +            buf_index = find_start_code(buf, buf_size, buf_index, next_avc);
 +            if (buf_index >= buf_size)
 +                break;
 +            if (buf_index >= next_avc)
 +                continue;
 +        }
 +        src_length = next_avc - buf_index;
 +
 +        state = buf[buf_index];
          switch (state & 0x1f) {
          case NAL_SLICE:
          case NAL_IDR_SLICE:
@@@ -283,17 -238,25 +284,26 @@@
              }
              break;
          }
-         ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length,
-                                  &consumed, src_length);
-         if (!ptr || dst_length < 0)
 -
 -        consumed = ff_h2645_extract_rbsp(buf, src_length, &nal);
++        consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &nal);
+         if (consumed < 0)
              break;
  
 -        ret = init_get_bits(&nal.gb, nal.data, nal.size * 8);
 +        buf_index += consumed;
 +
-         init_get_bits(&h->gb, ptr, 8 * dst_length);
++        ret = init_get_bits8(&nal.gb, nal.data, nal.size);
+         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);
 +            ff_h264_decode_seq_parameter_set(h, 0);
              break;
          case NAL_PPS:
              ff_h264_decode_picture_parameter_set(h, h->gb.size_in_bits);
@@@ -310,8 -273,8 +320,8 @@@
              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
 +            get_ue_golomb_long(&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];
              if (h->sei_recovery_frame_cnt >= 0) {
@@@ -492,13 -452,15 +502,18 @@@
                  s->field_order = AV_FIELD_UNKNOWN;
              }
  
+             av_freep(&nal.rbsp_buffer);
              return 0; /* no need to evaluate the rest */
          }
 -        buf += consumed;
 +    }
-     if (q264)
++    if (q264) {
++        av_freep(&nal.rbsp_buffer);
 +        return 0;
+     }
      /* didn't find a picture! */
 -    av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
 +    av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
+ fail:
+     av_freep(&nal.rbsp_buffer);
      return -1;
  }
  



More information about the ffmpeg-cvslog mailing list