---
 libavcodec/h264.c | 422 +++++++++++++++++++++++++++---------------------------
 1 file changed, 209 insertions(+), 213 deletions(-)

diff --git a/libavcodec/h264.c b/libavcodec/h264.c
index 4c432ae..1a04cc1 100644
--- a/libavcodec/h264.c
+++ b/libavcodec/h264.c
@@ -1395,10 +1395,12 @@ static int decode_nal_units(H264Context *h, const 
uint8_t *buf, int buf_size,
 {
     AVCodecContext *const avctx = h->avctx;
     H264Context *hx; ///< thread context
-    int buf_index;
-    unsigned context_count;
-    int next_avc, nal_index;
-    int nals_needed = 0; ///< number of NALs that need decoding before the 
next frame thread starts
+    int next_avc      = h->is_avc ? 0 : buf_size;
+    int nal_index     = 0;
+    int buf_index     = 0;
+    int nals_needed   = 0; ///< number of NALs that need decoding before the 
next frame thread starts
+    int context_count = 0;
+
     int ret = 0;
 
     h->max_contexts = h->slice_context_count;
@@ -1412,238 +1414,232 @@ static int decode_nal_units(H264Context *h, const 
uint8_t *buf, int buf_size,
     if (avctx->active_thread_type & FF_THREAD_FRAME)
         nals_needed = get_last_needed_nal(h, buf, buf_size);
 
-    {
-        buf_index     = 0;
-        context_count = 0;
-        next_avc      = h->is_avc ? 0 : buf_size;
-        nal_index     = 0;
-        for (;;) {
-            int consumed;
-            int dst_length;
-            int bit_length;
-            const uint8_t *ptr;
-            int nalsize = 0;
-            int err;
-
-            if (buf_index < next_avc)
-                buf_index = get_nal_start(buf, buf_size, buf_index, next_avc);
-
-            if (buf_index >= next_avc) {
-                nalsize = get_nalsize(h, buf, buf_size, &buf_index);
-                if (nalsize < 0)
-                    break;
-                next_avc = buf_index + nalsize;
-            }
+    for (;;) {
+        int consumed;
+        int dst_length;
+        int bit_length;
+        const uint8_t *ptr;
+        int nalsize = 0;
+        int err;
 
-            hx = h->thread_context[context_count];
+        if (buf_index < next_avc)
+            buf_index = get_nal_start(buf, buf_size, buf_index, next_avc);
 
-            ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
-                                     &consumed, next_avc - buf_index);
-            if (ptr == NULL || dst_length < 0) {
-                ret = -1;
-                goto end;
-            }
+        if (buf_index >= next_avc) {
+            nalsize = get_nalsize(h, buf, buf_size, &buf_index);
+            if (nalsize < 0)
+                break;
+            next_avc = buf_index + nalsize;
+        }
 
-            bit_length = get_bit_length(h, buf, ptr, dst_length,
-                                        buf_index + consumed, next_avc);
+        hx = h->thread_context[context_count];
 
-            if (h->is_avc && (nalsize != consumed) && nalsize)
-                av_log(h->avctx, AV_LOG_DEBUG,
-                       "AVC: Consumed only %d bytes instead of %d\n",
-                       consumed, nalsize);
+        ptr = ff_h264_decode_nal(hx, buf + buf_index, &dst_length,
+                                 &consumed, next_avc - buf_index);
+        if (ptr == NULL || dst_length < 0) {
+            ret = -1;
+            goto end;
+        }
 
-            if (h->avctx->debug & FF_DEBUG_STARTCODE)
-                av_log(h->avctx, AV_LOG_DEBUG,
-                       "NAL %d at %d/%d length %d\n",
-                       hx->nal_unit_type, buf_index, buf_size, dst_length);
+        bit_length = get_bit_length(h, buf, ptr, dst_length,
+                                    buf_index + consumed, next_avc);
 
-            buf_index += consumed;
-            nal_index++;
+        if (h->is_avc && (nalsize != consumed) && nalsize)
+            av_log(h->avctx, AV_LOG_DEBUG,
+                   "AVC: Consumed only %d bytes instead of %d\n",
+                   consumed, nalsize);
 
-            if (avctx->skip_frame >= AVDISCARD_NONREF &&
-                h->nal_ref_idc == 0 &&
-                h->nal_unit_type != NAL_SEI)
-                continue;
+        if (h->avctx->debug & FF_DEBUG_STARTCODE)
+            av_log(h->avctx, AV_LOG_DEBUG,
+                   "NAL %d at %d/%d length %d\n",
+                   hx->nal_unit_type, buf_index, buf_size, dst_length);
 
-again:
-            /* Ignore every NAL unit type except PPS and SPS during extradata
-             * parsing. Decoding slices is not possible in codec init
-             * with frame-mt */
-            if (parse_extradata && HAVE_THREADS &&
-                (h->avctx->active_thread_type & FF_THREAD_FRAME) &&
-                (hx->nal_unit_type != NAL_PPS &&
-                 hx->nal_unit_type != NAL_SPS)) {
-                if (hx->nal_unit_type < NAL_AUD ||
-                    hx->nal_unit_type > NAL_AUXILIARY_SLICE)
-                    av_log(avctx, AV_LOG_INFO,
-                           "Ignoring NAL unit %d during extradata parsing\n",
-                           hx->nal_unit_type);
-                hx->nal_unit_type = NAL_FF_IGNORE;
-            }
-            err = 0;
-            switch (hx->nal_unit_type) {
-            case NAL_IDR_SLICE:
-                if (h->nal_unit_type != NAL_IDR_SLICE) {
-                    av_log(h->avctx, AV_LOG_ERROR,
-                           "Invalid mix of idr and non-idr slices\n");
-                    ret = -1;
-                    goto end;
-                }
-                idr(h); // FIXME ensure we don't lose some frames if there is 
reordering
-            case NAL_SLICE:
-                init_get_bits(&hx->gb, ptr, bit_length);
-                hx->intra_gb_ptr      =
-                hx->inter_gb_ptr      = &hx->gb;
-                hx->data_partitioning = 0;
-
-                if ((err = ff_h264_decode_slice_header(hx, h)))
-                    break;
-
-                if (h->sei_recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
-                    h->recovery_frame = (h->frame_num + 
h->sei_recovery_frame_cnt) &
-                                        ((1 << h->sps.log2_max_frame_num) - 1);
-                }
+        buf_index += consumed;
+        nal_index++;
 
-                h->cur_pic_ptr->f.key_frame |=
-                    (hx->nal_unit_type == NAL_IDR_SLICE) ||
-                    (h->sei_recovery_frame_cnt >= 0);
+        if (avctx->skip_frame >= AVDISCARD_NONREF &&
+            h->nal_ref_idc == 0 &&
+            h->nal_unit_type != NAL_SEI)
+            continue;
 
-                if (hx->nal_unit_type == NAL_IDR_SLICE ||
-                    h->recovery_frame == h->frame_num) {
-                    h->recovery_frame         = -1;
-                    h->cur_pic_ptr->recovered = 1;
-                }
-                // If we have an IDR, all frames after it in decoded order are
-                // "recovered".
-                if (hx->nal_unit_type == NAL_IDR_SLICE)
-                    h->frame_recovered |= FRAME_RECOVERED_IDR;
-                h->cur_pic_ptr->recovered |= !!(h->frame_recovered & 
FRAME_RECOVERED_IDR);
-
-                if (h->current_slice == 1) {
-                    if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
-                        decode_postinit(h, nal_index >= nals_needed);
-
-                    if (h->avctx->hwaccel &&
-                        (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 
0)) < 0)
-                        return ret;
-                }
+again:
+        /* Ignore every NAL unit type except PPS and SPS during extradata
+         * parsing. Decoding slices is not possible in codec init
+         * with frame-mt */
+        if (parse_extradata && HAVE_THREADS &&
+            (h->avctx->active_thread_type & FF_THREAD_FRAME) &&
+            (hx->nal_unit_type != NAL_PPS &&
+             hx->nal_unit_type != NAL_SPS)) {
+            if (hx->nal_unit_type < NAL_AUD ||
+                hx->nal_unit_type > NAL_AUXILIARY_SLICE)
+                av_log(avctx, AV_LOG_INFO,
+                       "Ignoring NAL unit %d during extradata parsing\n",
+                       hx->nal_unit_type);
+            hx->nal_unit_type = NAL_FF_IGNORE;
+        }
+        err = 0;
+        switch (hx->nal_unit_type) {
+        case NAL_IDR_SLICE:
+            if (h->nal_unit_type != NAL_IDR_SLICE) {
+                av_log(h->avctx, AV_LOG_ERROR,
+                       "Invalid mix of idr and non-idr slices\n");
+                ret = -1;
+                goto end;
+            }
+            idr(h); // FIXME ensure we don't lose some frames if there is 
reordering
+        case NAL_SLICE:
+            init_get_bits(&hx->gb, ptr, bit_length);
+            hx->intra_gb_ptr      =
+            hx->inter_gb_ptr      = &hx->gb;
+            hx->data_partitioning = 0;
 
-                if (hx->redundant_pic_count == 0 &&
-                    (avctx->skip_frame < AVDISCARD_NONREF ||
-                     hx->nal_ref_idc) &&
-                    (avctx->skip_frame < AVDISCARD_BIDIR  ||
-                     hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
-                    (avctx->skip_frame < AVDISCARD_NONKEY ||
-                     hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
-                    avctx->skip_frame < AVDISCARD_ALL) {
-                    if (avctx->hwaccel) {
-                        ret = avctx->hwaccel->decode_slice(avctx,
-                                                           &buf[buf_index - 
consumed],
-                                                           consumed);
-                        if (ret < 0)
-                            return ret;
-                    } else
-                        context_count++;
-                }
+            if ((err = ff_h264_decode_slice_header(hx, h)))
                 break;
-            case NAL_DPA:
-                if (h->avctx->flags & CODEC_FLAG2_CHUNKS) {
-                    av_log(h->avctx, AV_LOG_ERROR,
-                           "Decoding in chunks is not supported for "
-                           "partitioned slices.\n");
-                    return AVERROR(ENOSYS);
-                }
 
-                init_get_bits(&hx->gb, ptr, bit_length);
-                hx->intra_gb_ptr =
-                hx->inter_gb_ptr = NULL;
+            if (h->sei_recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
+                h->recovery_frame = (h->frame_num + h->sei_recovery_frame_cnt) 
&
+                                    ((1 << h->sps.log2_max_frame_num) - 1);
+            }
 
-                if ((err = ff_h264_decode_slice_header(hx, h)) < 0) {
-                    /* make sure data_partitioning is cleared if it was set
-                     * before, so we don't try decoding a slice without a valid
-                     * slice header later */
-                    h->data_partitioning = 0;
-                    break;
-                }
+            h->cur_pic_ptr->f.key_frame |=
+                (hx->nal_unit_type == NAL_IDR_SLICE) ||
+                (h->sei_recovery_frame_cnt >= 0);
 
-                hx->data_partitioning = 1;
-                break;
-            case NAL_DPB:
-                init_get_bits(&hx->intra_gb, ptr, bit_length);
-                hx->intra_gb_ptr = &hx->intra_gb;
-                break;
-            case NAL_DPC:
-                init_get_bits(&hx->inter_gb, ptr, bit_length);
-                hx->inter_gb_ptr = &hx->inter_gb;
-
-                if (hx->redundant_pic_count == 0 &&
-                    hx->intra_gb_ptr &&
-                    hx->data_partitioning &&
-                    h->cur_pic_ptr && h->context_initialized &&
-                    (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) 
&&
-                    (avctx->skip_frame < AVDISCARD_BIDIR  ||
-                     hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
-                    (avctx->skip_frame < AVDISCARD_NONKEY ||
-                     hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
-                    avctx->skip_frame < AVDISCARD_ALL)
+            if (hx->nal_unit_type == NAL_IDR_SLICE ||
+                h->recovery_frame == h->frame_num) {
+                h->recovery_frame         = -1;
+                h->cur_pic_ptr->recovered = 1;
+            }
+            // If we have an IDR, all frames after it in decoded order are
+            // "recovered".
+            if (hx->nal_unit_type == NAL_IDR_SLICE)
+                h->frame_recovered |= FRAME_RECOVERED_IDR;
+            h->cur_pic_ptr->recovered |= !!(h->frame_recovered & 
FRAME_RECOVERED_IDR);
+
+            if (h->current_slice == 1) {
+                if (!(avctx->flags2 & CODEC_FLAG2_CHUNKS))
+                    decode_postinit(h, nal_index >= nals_needed);
+
+                if (h->avctx->hwaccel &&
+                    (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) 
< 0)
+                    return ret;
+            }
+
+            if (hx->redundant_pic_count == 0 &&
+                (avctx->skip_frame < AVDISCARD_NONREF ||
+                 hx->nal_ref_idc) &&
+                (avctx->skip_frame < AVDISCARD_BIDIR  ||
+                 hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
+                (avctx->skip_frame < AVDISCARD_NONKEY ||
+                 hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
+                avctx->skip_frame < AVDISCARD_ALL) {
+                if (avctx->hwaccel) {
+                    ret = avctx->hwaccel->decode_slice(avctx,
+                                                       &buf[buf_index - 
consumed],
+                                                       consumed);
+                    if (ret < 0)
+                        return ret;
+                } else
                     context_count++;
-                break;
-            case NAL_SEI:
-                init_get_bits(&h->gb, ptr, bit_length);
-                ff_h264_decode_sei(h);
-                break;
-            case NAL_SPS:
-                init_get_bits(&h->gb, ptr, bit_length);
-                ret = ff_h264_decode_seq_parameter_set(h);
-                if (ret < 0 && h->is_avc && (nalsize != consumed) && nalsize) {
-                    av_log(h->avctx, AV_LOG_DEBUG,
-                           "SPS decoding failure, trying again with the 
complete NAL\n");
-                    init_get_bits(&h->gb, buf + buf_index + 1 - consumed,
-                                  8 * (nalsize - 1));
-                    ff_h264_decode_seq_parameter_set(h);
-                }
+            }
+            break;
+        case NAL_DPA:
+            if (h->avctx->flags & CODEC_FLAG2_CHUNKS) {
+                av_log(h->avctx, AV_LOG_ERROR,
+                       "Decoding in chunks is not supported for "
+                       "partitioned slices.\n");
+                return AVERROR(ENOSYS);
+            }
 
-                ret = ff_h264_set_parameter_from_sps(h);
-                if (ret < 0)
-                    goto end;
+            init_get_bits(&hx->gb, ptr, bit_length);
+            hx->intra_gb_ptr =
+            hx->inter_gb_ptr = NULL;
 
+            if ((err = ff_h264_decode_slice_header(hx, h)) < 0) {
+                /* make sure data_partitioning is cleared if it was set
+                 * before, so we don't try decoding a slice without a valid
+                 * slice header later */
+                h->data_partitioning = 0;
                 break;
-            case NAL_PPS:
-                init_get_bits(&h->gb, ptr, bit_length);
-                ff_h264_decode_picture_parameter_set(h, bit_length);
-                break;
-            case NAL_AUD:
-            case NAL_END_SEQUENCE:
-            case NAL_END_STREAM:
-            case NAL_FILLER_DATA:
-            case NAL_SPS_EXT:
-            case NAL_AUXILIARY_SLICE:
-                break;
-            case NAL_FF_IGNORE:
-                break;
-            default:
-                av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
-                       hx->nal_unit_type, bit_length);
             }
 
-            if (context_count == h->max_contexts) {
-                ff_h264_execute_decode_slices(h, context_count);
-                context_count = 0;
+            hx->data_partitioning = 1;
+            break;
+        case NAL_DPB:
+            init_get_bits(&hx->intra_gb, ptr, bit_length);
+            hx->intra_gb_ptr = &hx->intra_gb;
+            break;
+        case NAL_DPC:
+            init_get_bits(&hx->inter_gb, ptr, bit_length);
+            hx->inter_gb_ptr = &hx->inter_gb;
+
+            if (hx->redundant_pic_count == 0 &&
+                hx->intra_gb_ptr &&
+                hx->data_partitioning &&
+                h->cur_pic_ptr && h->context_initialized &&
+                (avctx->skip_frame < AVDISCARD_NONREF || hx->nal_ref_idc) &&
+                (avctx->skip_frame < AVDISCARD_BIDIR  ||
+                 hx->slice_type_nos != AV_PICTURE_TYPE_B) &&
+                (avctx->skip_frame < AVDISCARD_NONKEY ||
+                 hx->slice_type_nos == AV_PICTURE_TYPE_I) &&
+                avctx->skip_frame < AVDISCARD_ALL)
+                context_count++;
+            break;
+        case NAL_SEI:
+            init_get_bits(&h->gb, ptr, bit_length);
+            ff_h264_decode_sei(h);
+            break;
+        case NAL_SPS:
+            init_get_bits(&h->gb, ptr, bit_length);
+            ret = ff_h264_decode_seq_parameter_set(h);
+            if (ret < 0 && h->is_avc && (nalsize != consumed) && nalsize) {
+                av_log(h->avctx, AV_LOG_DEBUG,
+                       "SPS decoding failure, trying again with the complete 
NAL\n");
+                init_get_bits(&h->gb, buf + buf_index + 1 - consumed,
+                              8 * (nalsize - 1));
+                ff_h264_decode_seq_parameter_set(h);
             }
 
-            if (err < 0) {
-                av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
-                h->ref_count[0] = h->ref_count[1] = h->list_count = 0;
-            } else if (err == 1) {
-                /* Slice could not be decoded in parallel mode, copy down
-                 * NAL unit stuff to context 0 and restart. Note that
-                 * rbsp_buffer is not transferred, but since we no longer
-                 * run in parallel mode this should not be an issue. */
-                h->nal_unit_type = hx->nal_unit_type;
-                h->nal_ref_idc   = hx->nal_ref_idc;
-                hx               = h;
-                goto again;
-            }
+            ret = ff_h264_set_parameter_from_sps(h);
+            if (ret < 0)
+                goto end;
+
+            break;
+        case NAL_PPS:
+            init_get_bits(&h->gb, ptr, bit_length);
+            ff_h264_decode_picture_parameter_set(h, bit_length);
+            break;
+        case NAL_AUD:
+        case NAL_END_SEQUENCE:
+        case NAL_END_STREAM:
+        case NAL_FILLER_DATA:
+        case NAL_SPS_EXT:
+        case NAL_AUXILIARY_SLICE:
+            break;
+        case NAL_FF_IGNORE:
+            break;
+        default:
+            av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
+                   hx->nal_unit_type, bit_length);
+        }
+
+        if (context_count == h->max_contexts) {
+            ff_h264_execute_decode_slices(h, context_count);
+            context_count = 0;
+        }
+
+        if (err < 0) {
+            av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
+            h->ref_count[0] = h->ref_count[1] = h->list_count = 0;
+        } else if (err == 1) {
+            /* Slice could not be decoded in parallel mode, copy down
+             * NAL unit stuff to context 0 and restart. Note that
+             * rbsp_buffer is not transferred, but since we no longer
+             * run in parallel mode this should not be an issue. */
+            h->nal_unit_type = hx->nal_unit_type;
+            h->nal_ref_idc   = hx->nal_ref_idc;
+            hx               = h;
+            goto again;
         }
     }
     if (context_count)
-- 
1.8.5.2 (Apple Git-48)

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to