[FFmpeg-devel] [PATCH] hevc_parser: use ff_h2645_packet_split() to parse NAL units

James Almer jamrial at gmail.com
Sun Apr 30 20:40:45 EEST 2017


This simplifies the code considerably.
---
 libavcodec/hevc_parser.c | 50 ++++++++++--------------------------------------
 1 file changed, 10 insertions(+), 40 deletions(-)

diff --git a/libavcodec/hevc_parser.c b/libavcodec/hevc_parser.c
index 310428d0ff..5b9cd87d88 100644
--- a/libavcodec/hevc_parser.c
+++ b/libavcodec/hevc_parser.c
@@ -192,11 +192,8 @@ static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
     GetBitContext      *gb;
     SliceHeader        *sh = &h->sh;
     HEVCParamSets *ps = &h->ps;
-    H2645Packet   *pkt = &ctx->pkt;
-    const uint8_t *buf_end = buf + buf_size;
-    int state = -1, i;
-    H2645NAL *nal;
     int is_global = buf == avctx->extradata;
+    int i, ret;
 
     if (!h->HEVClc)
         h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
@@ -214,44 +211,18 @@ static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
 
     ff_hevc_reset_sei(h);
 
-    if (!buf_size)
-        return 0;
-
-    if (pkt->nals_allocated < 1) {
-        H2645NAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
-        if (!tmp)
-            return AVERROR(ENOMEM);
-        pkt->nals = tmp;
-        memset(pkt->nals, 0, sizeof(*tmp));
-        pkt->nals_allocated = 1;
-    }
-
-    nal = &pkt->nals[0];
+    ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
+                                AV_CODEC_ID_HEVC, 1);
+    if (ret < 0)
+        return ret;
 
-    for (;;) {
-        int src_length, consumed;
-        int ret;
+    for (i = 0; i < ctx->pkt.nb_nals; i++) {
+        H2645NAL *nal = &ctx->pkt.nals[i];
         int num = 0, den = 0;
-        buf = avpriv_find_start_code(buf, buf_end, &state);
-        if (--buf + 2 >= buf_end)
-            break;
-        src_length = buf_end - buf;
-
-        h->nal_unit_type = (*buf >> 1) & 0x3f;
-        h->temporal_id   = (*(buf + 1) & 0x07) - 1;
-        if (h->nal_unit_type <= HEVC_NAL_CRA_NUT) {
-            // Do not walk the whole buffer just to decode slice segment header
-            if (src_length > 20)
-                src_length = 20;
-        }
-
-        consumed = ff_h2645_extract_rbsp(buf, src_length, nal, 1);
-        if (consumed < 0)
-            return consumed;
 
-        ret = init_get_bits8(gb, nal->data + 2, nal->size);
-        if (ret < 0)
-            return ret;
+        h->nal_unit_type = nal->type;
+        h->temporal_id   = nal->temporal_id;
+        *gb = nal->gb;
 
         switch (h->nal_unit_type) {
         case HEVC_NAL_VPS:
@@ -394,7 +365,6 @@ static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
 
             return 0; /* no need to evaluate the rest */
         }
-        buf += consumed;
     }
     /* didn't find a picture! */
     if (!is_global)
-- 
2.12.1



More information about the ffmpeg-devel mailing list