summaryrefslogtreecommitdiff
path: root/libavcodec/hevc.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michael@niedermayer.cc>2015-07-12 23:31:12 +0200
committerMichael Niedermayer <michael@niedermayer.cc>2015-07-12 23:44:24 +0200
commit4690a636324663be660602ea4d1d80ba4fa29763 (patch)
treee2e86aeda6938c596a5ac3362b59910787b6cd79 /libavcodec/hevc.c
parent4c42c66935a7bdaded8667fa76dc0945ae66e867 (diff)
parentd82e1adc2019135a2fc45372e0ed0b5ef107cdd0 (diff)
Merge commit 'd82e1adc2019135a2fc45372e0ed0b5ef107cdd0'
* commit 'd82e1adc2019135a2fc45372e0ed0b5ef107cdd0': hevc: move splitting the packet into NALUs into a separate function Conflicts: libavcodec/hevc.c libavcodec/hevc.h libavcodec/hevc_parse.c Merged-by: Michael Niedermayer <michael@niedermayer.cc>
Diffstat (limited to 'libavcodec/hevc.c')
-rw-r--r--libavcodec/hevc.c153
1 files changed, 18 insertions, 135 deletions
diff --git a/libavcodec/hevc.c b/libavcodec/hevc.c
index a3798e6189..9d99cba4c8 100644
--- a/libavcodec/hevc.c
+++ b/libavcodec/hevc.c
@@ -2505,32 +2505,6 @@ static int hls_slice_data_wpp(HEVCContext *s, const uint8_t *nal, int length)
return res;
}
-/**
- * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
- * 0 if the unit should be skipped, 1 otherwise
- */
-static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
-{
- GetBitContext *gb = &nal->gb;
- int nuh_layer_id;
-
- if (get_bits1(gb) != 0)
- return AVERROR_INVALIDDATA;
-
- nal->type = get_bits(gb, 6);
-
- nuh_layer_id = get_bits(gb, 6);
- nal->temporal_id = get_bits(gb, 3) - 1;
- if (nal->temporal_id < 0)
- return AVERROR_INVALIDDATA;
-
- av_log(avctx, AV_LOG_DEBUG,
- "nal_unit_type: %d, nuh_layer_id: %d, temporal_id: %d\n",
- nal->type, nuh_layer_id, nal->temporal_id);
-
- return nuh_layer_id == 0;
-}
-
static int set_side_data(HEVCContext *s)
{
AVFrame *out = s->ref->frame;
@@ -2781,7 +2755,7 @@ fail:
static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
{
- int i, consumed, ret = 0;
+ int i, ret = 0;
s->ref = NULL;
s->last_eos = s->eos;
@@ -2789,117 +2763,26 @@ static int decode_nal_units(HEVCContext *s, const uint8_t *buf, int length)
/* split the input packet into NAL units, so we know the upper bound on the
* number of slices in the frame */
- s->nb_nals = 0;
- while (length >= 4) {
- HEVCNAL *nal;
- int extract_length = 0;
-
- if (s->is_nalff) {
- int i;
- for (i = 0; i < s->nal_length_size; i++)
- extract_length = (extract_length << 8) | buf[i];
- buf += s->nal_length_size;
- length -= s->nal_length_size;
-
- if (extract_length > length) {
- av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
- ret = AVERROR_INVALIDDATA;
- goto fail;
- }
- } else {
- /* search start code */
- while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
- ++buf;
- --length;
- if (length < 4) {
- av_log(s->avctx, AV_LOG_ERROR, "No start code is found.\n");
- ret = AVERROR_INVALIDDATA;
- goto fail;
- }
- }
-
- buf += 3;
- length -= 3;
- }
-
- if (!s->is_nalff)
- extract_length = length;
-
- if (s->nals_allocated < s->nb_nals + 1) {
- int new_size = s->nals_allocated + 1;
- void *tmp = av_realloc_array(s->nals, new_size, sizeof(*s->nals));
- ret = AVERROR(ENOMEM);
- if (!tmp) {
- goto fail;
- }
- s->nals = tmp;
- memset(s->nals + s->nals_allocated, 0,
- (new_size - s->nals_allocated) * sizeof(*s->nals));
-
- tmp = av_realloc_array(s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));
- if (!tmp)
- goto fail;
- s->skipped_bytes_nal = tmp;
-
- tmp = av_realloc_array(s->skipped_bytes_pos_size_nal, new_size, sizeof(*s->skipped_bytes_pos_size_nal));
- if (!tmp)
- goto fail;
- s->skipped_bytes_pos_size_nal = tmp;
-
- tmp = av_realloc_array(s->skipped_bytes_pos_nal, new_size, sizeof(*s->skipped_bytes_pos_nal));
- if (!tmp)
- goto fail;
- s->skipped_bytes_pos_nal = tmp;
-
- s->skipped_bytes_pos_size_nal[s->nals_allocated] = 1024; // initial buffer size
- s->skipped_bytes_pos_nal[s->nals_allocated] = av_malloc_array(s->skipped_bytes_pos_size_nal[s->nals_allocated], sizeof(*s->skipped_bytes_pos));
- if (!s->skipped_bytes_pos_nal[s->nals_allocated])
- goto fail;
- s->nals_allocated = new_size;
- }
- s->skipped_bytes_pos_size = s->skipped_bytes_pos_size_nal[s->nb_nals];
- s->skipped_bytes_pos = s->skipped_bytes_pos_nal[s->nb_nals];
- nal = &s->nals[s->nb_nals];
-
- consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
- if (consumed < 0) {
- ret = consumed;
- goto fail;
- }
-
- s->skipped_bytes_nal[s->nb_nals] = s->skipped_bytes;
- s->skipped_bytes_pos_size_nal[s->nb_nals] = s->skipped_bytes_pos_size;
- s->skipped_bytes_pos_nal[s->nb_nals++] = s->skipped_bytes_pos;
-
- ret = init_get_bits8(&nal->gb, nal->data, nal->size);
- if (ret < 0)
- goto fail;
-
- ret = hls_nal_unit(nal, s->avctx);
- if (ret <= 0) {
- if (ret < 0) {
- av_log(s->avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
- nal->type);
- }
- s->nb_nals--;
- goto skip_nal;
- }
+ ret = ff_hevc_split_packet(s, &s->pkt, buf, length, s->avctx, s->is_nalff,
+ s->nal_length_size);
+ if (ret < 0) {
+ av_log(s->avctx, AV_LOG_ERROR,
+ "Error splitting the input into NAL units.\n");
+ return ret;
+ }
- if (nal->type == NAL_EOB_NUT ||
- nal->type == NAL_EOS_NUT)
+ for (i = 0; i < s->pkt.nb_nals; i++) {
+ if (s->pkt.nals[i].type == NAL_EOB_NUT ||
+ s->pkt.nals[i].type == NAL_EOS_NUT)
s->eos = 1;
-
-skip_nal:
- buf += consumed;
- length -= consumed;
}
/* parse the NAL units */
- for (i = 0; i < s->nb_nals; i++) {
+ for (i = 0; i < s->pkt.nb_nals; i++) {
s->skipped_bytes = s->skipped_bytes_nal[i];
s->skipped_bytes_pos = s->skipped_bytes_pos_nal[i];
- ret = decode_nal_unit(s, &s->nals[i]);
+ ret = decode_nal_unit(s, &s->pkt.nals[i]);
if (ret < 0) {
av_log(s->avctx, AV_LOG_WARNING,
"Error parsing NAL unit #%d.\n", i);
@@ -3087,7 +2970,7 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx)
av_freep(&s->md5_ctx);
- for(i=0; i < s->nals_allocated; i++) {
+ for(i=0; i < s->pkt.nals_allocated; i++) {
av_freep(&s->skipped_bytes_pos_nal[i]);
}
av_freep(&s->skipped_bytes_pos_size_nal);
@@ -3132,10 +3015,10 @@ static av_cold int hevc_decode_free(AVCodecContext *avctx)
s->HEVClc = NULL;
av_freep(&s->HEVClcList[0]);
- for (i = 0; i < s->nals_allocated; i++)
- av_freep(&s->nals[i].rbsp_buffer);
- av_freep(&s->nals);
- s->nals_allocated = 0;
+ for (i = 0; i < s->pkt.nals_allocated; i++)
+ av_freep(&s->pkt.nals[i].rbsp_buffer);
+ av_freep(&s->pkt.nals);
+ s->pkt.nals_allocated = 0;
return 0;
}