summaryrefslogtreecommitdiff
path: root/libavcodec/hevc_parse.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_parse.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_parse.c')
-rw-r--r--libavcodec/hevc_parse.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/libavcodec/hevc_parse.c b/libavcodec/hevc_parse.c
index 260f1e1aa6..ee9a9d9e96 100644
--- a/libavcodec/hevc_parse.c
+++ b/libavcodec/hevc_parse.c
@@ -143,3 +143,132 @@ nsc:
return si;
}
+/**
+ * @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;
+}
+
+
+int ff_hevc_split_packet(HEVCContext *s, HEVCPacket *pkt, const uint8_t *buf, int length,
+ AVCodecContext *avctx, int is_nalff, int nal_length_size)
+{
+ int consumed, ret = 0;
+
+ pkt->nb_nals = 0;
+ while (length >= 4) {
+ HEVCNAL *nal;
+ int extract_length = 0;
+
+ if (is_nalff) {
+ int i;
+ for (i = 0; i < nal_length_size; i++)
+ extract_length = (extract_length << 8) | buf[i];
+ buf += nal_length_size;
+ length -= nal_length_size;
+
+ if (extract_length > length) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ } else {
+ /* search start code */
+ while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
+ ++buf;
+ --length;
+ if (length < 4) {
+ av_log(avctx, AV_LOG_ERROR, "No start code is found.\n");
+ return AVERROR_INVALIDDATA;
+ }
+ }
+
+ buf += 3;
+ length -= 3;
+ extract_length = length;
+ }
+
+ if (pkt->nals_allocated < pkt->nb_nals + 1) {
+ int new_size = pkt->nals_allocated + 1;
+ void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
+
+ if (!tmp)
+ return AVERROR(ENOMEM);
+
+ pkt->nals = tmp;
+ memset(pkt->nals + pkt->nals_allocated, 0,
+ (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
+
+ tmp = av_realloc_array(s->skipped_bytes_nal, new_size, sizeof(*s->skipped_bytes_nal));
+ if (!tmp)
+ return AVERROR(ENOMEM);
+ 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)
+ return AVERROR(ENOMEM);
+ 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)
+ return AVERROR(ENOMEM);
+ s->skipped_bytes_pos_nal = tmp;
+
+ s->skipped_bytes_pos_size_nal[pkt->nals_allocated] = 1024; // initial buffer size
+ s->skipped_bytes_pos_nal[pkt->nals_allocated] = av_malloc_array(s->skipped_bytes_pos_size_nal[pkt->nals_allocated], sizeof(*s->skipped_bytes_pos));
+ if (!s->skipped_bytes_pos_nal[pkt->nals_allocated])
+ return AVERROR(ENOMEM);
+
+ pkt->nals_allocated = new_size;
+ }
+ s->skipped_bytes_pos_size = s->skipped_bytes_pos_size_nal[pkt->nb_nals];
+ s->skipped_bytes_pos = s->skipped_bytes_pos_nal[pkt->nb_nals];
+ nal = &pkt->nals[pkt->nb_nals];
+
+ consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
+ if (consumed < 0)
+ return consumed;
+
+ s->skipped_bytes_nal[pkt->nb_nals] = s->skipped_bytes;
+ s->skipped_bytes_pos_size_nal[pkt->nb_nals] = s->skipped_bytes_pos_size;
+ s->skipped_bytes_pos_nal[pkt->nb_nals++] = s->skipped_bytes_pos;
+
+ ret = init_get_bits8(&nal->gb, nal->data, nal->size);
+ if (ret < 0)
+ return ret;
+
+ ret = hls_nal_unit(nal, avctx);
+ if (ret <= 0) {
+ if (ret < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
+ nal->type);
+ }
+ pkt->nb_nals--;
+ }
+
+ buf += consumed;
+ length -= consumed;
+ }
+
+ return 0;
+}
+