summaryrefslogtreecommitdiff
path: root/libavcodec/hevc_parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/hevc_parse.c')
-rw-r--r--libavcodec/hevc_parse.c99
1 files changed, 81 insertions, 18 deletions
diff --git a/libavcodec/hevc_parse.c b/libavcodec/hevc_parse.c
index de00245809..63ed84a8de 100644
--- a/libavcodec/hevc_parse.c
+++ b/libavcodec/hevc_parse.c
@@ -1,20 +1,20 @@
/*
* HEVC common code
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@@ -29,12 +29,14 @@
/* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
* between these functions would be nice. */
-int ff_hevc_extract_rbsp(const uint8_t *src, int length,
+int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
HEVCNAL *nal)
{
int i, si, di;
uint8_t *dst;
+ if (s)
+ nal->skipped_bytes = 0;
#define STARTCODE_TEST \
if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
if (src[i + 2] != 3) { \
@@ -108,6 +110,22 @@ int ff_hevc_extract_rbsp(const uint8_t *src, int length,
dst[di++] = 0;
si += 3;
+ if (s && nal->skipped_bytes_pos) {
+ nal->skipped_bytes++;
+ if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
+ nal->skipped_bytes_pos_size *= 2;
+ av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
+ av_reallocp_array(&nal->skipped_bytes_pos,
+ nal->skipped_bytes_pos_size,
+ sizeof(*nal->skipped_bytes_pos));
+ if (!nal->skipped_bytes_pos) {
+ nal->skipped_bytes_pos_size = 0;
+ return AVERROR(ENOMEM);
+ }
+ }
+ if (nal->skipped_bytes_pos)
+ nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
+ }
continue;
} else // next start code
goto nsc;
@@ -128,6 +146,38 @@ nsc:
return si;
}
+static const char *nal_unit_name(int nal_type)
+{
+ switch(nal_type) {
+ case NAL_TRAIL_N : return "TRAIL_N";
+ case NAL_TRAIL_R : return "TRAIL_R";
+ case NAL_TSA_N : return "TSA_N";
+ case NAL_TSA_R : return "TSA_R";
+ case NAL_STSA_N : return "STSA_N";
+ case NAL_STSA_R : return "STSA_R";
+ case NAL_RADL_N : return "RADL_N";
+ case NAL_RADL_R : return "RADL_R";
+ case NAL_RASL_N : return "RASL_N";
+ case NAL_RASL_R : return "RASL_R";
+ case NAL_BLA_W_LP : return "BLA_W_LP";
+ case NAL_BLA_W_RADL : return "BLA_W_RADL";
+ case NAL_BLA_N_LP : return "BLA_N_LP";
+ case NAL_IDR_W_RADL : return "IDR_W_RADL";
+ case NAL_IDR_N_LP : return "IDR_N_LP";
+ case NAL_CRA_NUT : return "CRA_NUT";
+ case NAL_VPS : return "VPS";
+ case NAL_SPS : return "SPS";
+ case NAL_PPS : return "PPS";
+ case NAL_AUD : return "AUD";
+ case NAL_EOS_NUT : return "EOS_NUT";
+ case NAL_EOB_NUT : return "EOB_NUT";
+ case NAL_FD_NUT : return "FD_NUT";
+ case NAL_SEI_PREFIX : return "SEI_PREFIX";
+ case NAL_SEI_SUFFIX : return "SEI_SUFFIX";
+ default : return "?";
+ }
+}
+
/**
* @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
* 0 if the unit should be skipped, 1 otherwise
@@ -148,14 +198,14 @@ static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
return AVERROR_INVALIDDATA;
av_log(avctx, AV_LOG_DEBUG,
- "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
- nal->type, nuh_layer_id, nal->temporal_id);
+ "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
+ nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
return nuh_layer_id == 0;
}
-int ff_hevc_split_packet(HEVCPacket *pkt, const uint8_t *buf, int length,
+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;
@@ -177,13 +227,15 @@ int ff_hevc_split_packet(HEVCPacket *pkt, const uint8_t *buf, int length,
return AVERROR_INVALIDDATA;
}
} else {
- if (buf[2] == 0) {
- length--;
- buf++;
- continue;
+ /* 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;
+ }
}
- if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1)
- return AVERROR_INVALIDDATA;
buf += 3;
length -= 3;
@@ -192,21 +244,31 @@ int ff_hevc_split_packet(HEVCPacket *pkt, const uint8_t *buf, int length,
if (pkt->nals_allocated < pkt->nb_nals + 1) {
int new_size = pkt->nals_allocated + 1;
- HEVCNAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));
+ 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(*tmp));
+ (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
+
+ nal = &pkt->nals[pkt->nb_nals];
+ nal->skipped_bytes_pos_size = 1024; // initial buffer size
+ nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
+ if (!nal->skipped_bytes_pos)
+ return AVERROR(ENOMEM);
+
pkt->nals_allocated = new_size;
}
- nal = &pkt->nals[pkt->nb_nals++];
+ nal = &pkt->nals[pkt->nb_nals];
- consumed = ff_hevc_extract_rbsp(buf, extract_length, nal);
+ consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
if (consumed < 0)
return consumed;
+ pkt->nb_nals++;
+
ret = init_get_bits8(&nal->gb, nal->data, nal->size);
if (ret < 0)
return ret;
@@ -226,3 +288,4 @@ int ff_hevc_split_packet(HEVCPacket *pkt, const uint8_t *buf, int length,
return 0;
}
+