static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
{
    AVCodecContext *const avctx = h->avctx;
    unsigned context_count = 0;
    int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
    int i, ret = 0;

    h->max_contexts = h->slice_context_count;
    if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
        h->current_slice = 0;
        if (!h->first_field)
            h->cur_pic_ptr = NULL;
        ff_h264_sei_uninit(&h->sei);
    }

    ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->is_avc,
                                h->nal_length_size, avctx->codec_id);
    if (ret < 0) {
        av_log(avctx, AV_LOG_ERROR,
               "Error splitting the input into NAL units.\n");
        return ret;
    }

    if (avctx->active_thread_type & FF_THREAD_FRAME)
        nals_needed = get_last_needed_nal(h);

    for (i = 0; i < h->pkt.nb_nals; i++) {
        H2645NAL *nal = &h->pkt.nals[i];
        H264SliceContext *sl = &h->slice_ctx[context_count];
        int err;

        if (avctx->skip_frame >= AVDISCARD_NONREF &&
            nal->ref_idc == 0 && nal->type != NAL_SEI)
            continue;

        // FIXME these should stop being context-global variables
        h->nal_ref_idc   = nal->ref_idc;
        h->nal_unit_type = nal->type;

        err = 0;
        switch (nal->type) {
        case NAL_IDR_SLICE:
            if (nal->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:
            sl->gb = nal->gb;

            if ((err = ff_h264_decode_slice_header(h, sl)))
                break;

            if (h->sei.recovery_point.recovery_frame_cnt >= 0 && h->recovery_frame < 0) {
                h->recovery_frame = (h->poc.frame_num + h->sei.recovery_point.recovery_frame_cnt) &
                                    ((1 << h->ps.sps->log2_max_frame_num) - 1);
            }

            h->cur_pic_ptr->f->key_frame |=
                (nal->type == NAL_IDR_SLICE) || (h->sei.recovery_point.recovery_frame_cnt >= 0);

            if (nal->type == NAL_IDR_SLICE || h->recovery_frame == h->poc.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 (nal->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 & AV_CODEC_FLAG2_CHUNKS))
                    decode_postinit(h, i >= nals_needed);

                if (h->avctx->hwaccel &&
                    (ret = h->avctx->hwaccel->start_frame(h->avctx, NULL, 0)) < 0)
                    return ret;
            }

            if (sl->redundant_pic_count == 0 &&
                (avctx->skip_frame < AVDISCARD_NONREF || nal->ref_idc) &&
                (avctx->skip_frame < AVDISCARD_BIDIR  ||
                 sl->slice_type_nos != AV_PICTURE_TYPE_B) &&
                (avctx->skip_frame < AVDISCARD_NONKEY ||
                 h->cur_pic_ptr->f->key_frame) &&
                avctx->skip_frame < AVDISCARD_ALL) {
                if (avctx->hwaccel) {
                    ret = avctx->hwaccel->decode_slice(avctx, nal->raw_data, nal->raw_size);
                    if (ret < 0)
                        return ret;
                } else
                    context_count++;
            }
            break;
        case NAL_DPA:
        case NAL_DPB:
        case NAL_DPC:
            avpriv_request_sample(avctx, "data partitioning");
            ret = AVERROR(ENOSYS);
            goto end;
            break;
        case NAL_SEI:
            ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);
            if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
                goto end;
            break;
        case NAL_SPS:
            ret = ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps);
            if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
                goto end;
            break;
        case NAL_PPS:
            ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,
                                                       nal->size_bits);
            if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
                goto end;
            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",
                   nal->type, nal->size_bits);
        }

        if (context_count == h->max_contexts) {
            ret = ff_h264_execute_decode_slices(h, context_count);
            if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
                goto end;
            context_count = 0;
        }

        if (err < 0) {
            av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
            sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
        }
    }
    if (context_count) {
        ret = ff_h264_execute_decode_slices(h, context_count);
        if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
            goto end;
    }

    ret = 0;
end:
    /* clean up */
    if (h->cur_pic_ptr && !h->droppable) {
        ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
                                  h->picture_structure == PICT_BOTTOM_FIELD);
    }

    return (ret < 0) ? ret : buf_size;
}