Merge commit '8d0cc8ca97678f4ca87948ebabcbaab5a4f4c1f6'

* commit '8d0cc8ca97678f4ca87948ebabcbaab5a4f4c1f6':
  h264_parser: switch to h2645_parse for NAL unescaping

Merged-by: Hendrik Leppkes <h.leppkes@gmail.com>
This commit is contained in:
Hendrik Leppkes 2016-05-17 14:21:12 +02:00
commit 15ad023210
4 changed files with 28 additions and 134 deletions

View File

@ -913,7 +913,7 @@ OBJS-$(CONFIG_G729_PARSER) += g729_parser.o
OBJS-$(CONFIG_GSM_PARSER) += gsm_parser.o OBJS-$(CONFIG_GSM_PARSER) += gsm_parser.o
OBJS-$(CONFIG_H261_PARSER) += h261_parser.o OBJS-$(CONFIG_H261_PARSER) += h261_parser.o
OBJS-$(CONFIG_H263_PARSER) += h263_parser.o OBJS-$(CONFIG_H263_PARSER) += h263_parser.o
OBJS-$(CONFIG_H264_PARSER) += h264_parser.o h264_parse.o OBJS-$(CONFIG_H264_PARSER) += h264_parser.o h264_parse.o h2645_parse.o
OBJS-$(CONFIG_HEVC_PARSER) += hevc_parser.o h2645_parse.o hevc_ps.o hevc_data.o OBJS-$(CONFIG_HEVC_PARSER) += hevc_parser.o h2645_parse.o hevc_ps.o hevc_data.o
OBJS-$(CONFIG_MJPEG_PARSER) += mjpeg_parser.o OBJS-$(CONFIG_MJPEG_PARSER) += mjpeg_parser.o
OBJS-$(CONFIG_MLP_PARSER) += mlp_parser.o mlp.o OBJS-$(CONFIG_MLP_PARSER) += mlp_parser.o mlp.o

View File

@ -134,116 +134,6 @@ void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
} }
} }
const uint8_t *ff_h264_decode_nal(H264Context *h, H264SliceContext *sl,
const uint8_t *src,
int *dst_length, int *consumed, int length)
{
int i, si, di;
uint8_t *dst;
// src[0]&0x80; // forbidden bit
h->nal_ref_idc = src[0] >> 5;
h->nal_unit_type = src[0] & 0x1F;
src++;
length--;
#define STARTCODE_TEST \
if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
if (src[i + 2] != 3 && src[i + 2] != 0) { \
/* startcode, so we must be past the end */ \
length = i; \
} \
break; \
}
#if HAVE_FAST_UNALIGNED
#define FIND_FIRST_ZERO \
if (i > 0 && !src[i]) \
i--; \
while (src[i]) \
i++
#if HAVE_FAST_64BIT
for (i = 0; i + 1 < length; i += 9) {
if (!((~AV_RN64A(src + i) &
(AV_RN64A(src + i) - 0x0100010001000101ULL)) &
0x8000800080008080ULL))
continue;
FIND_FIRST_ZERO;
STARTCODE_TEST;
i -= 7;
}
#else
for (i = 0; i + 1 < length; i += 5) {
if (!((~AV_RN32A(src + i) &
(AV_RN32A(src + i) - 0x01000101U)) &
0x80008080U))
continue;
FIND_FIRST_ZERO;
STARTCODE_TEST;
i -= 3;
}
#endif
#else
for (i = 0; i + 1 < length; i += 2) {
if (src[i])
continue;
if (i > 0 && src[i - 1] == 0)
i--;
STARTCODE_TEST;
}
#endif
av_fast_padded_malloc(&sl->rbsp_buffer, &sl->rbsp_buffer_size, length+MAX_MBPAIR_SIZE);
dst = sl->rbsp_buffer;
if (!dst)
return NULL;
if(i>=length-1){ //no escaped 0
*dst_length= length;
*consumed= length+1; //+1 for the header
if(h->avctx->flags2 & AV_CODEC_FLAG2_FAST){
return src;
}else{
memcpy(dst, src, length);
return dst;
}
}
memcpy(dst, src, i);
si = di = i;
while (si + 2 < length) {
// remove escapes (very rare 1:2^22)
if (src[si + 2] > 3) {
dst[di++] = src[si++];
dst[di++] = src[si++];
} else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
if (src[si + 2] == 3) { // escape
dst[di++] = 0;
dst[di++] = 0;
si += 3;
continue;
} else // next start code
goto nsc;
}
dst[di++] = src[si++];
}
while (si < length)
dst[di++] = src[si++];
nsc:
memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
*dst_length = di;
*consumed = si + 1; // +1 for the header
/* FIXME store exact number of bits in the getbitcontext
* (it is needed for decoding) */
return dst;
}
void ff_h264_free_tables(H264Context *h) void ff_h264_free_tables(H264Context *h)
{ {
int i; int i;

View File

@ -860,17 +860,6 @@ int ff_h264_get_profile(SPS *sps);
*/ */
int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length); int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length);
/**
* Decode a network abstraction layer unit.
* @param consumed is the number of bytes used as input
* @param length is the length of the array
* @param dst_length is the number of decoded bytes FIXME here
* or a decode rbsp tailing?
* @return decoded bytes, might be src+1 if no escapes
*/
const uint8_t *ff_h264_decode_nal(H264Context *h, H264SliceContext *sl, const uint8_t *src,
int *dst_length, int *consumed, int length);
/** /**
* Free any data that may have been allocated in the H264 context * Free any data that may have been allocated in the H264 context
* like SPS, PPS etc. * like SPS, PPS etc.

View File

@ -227,13 +227,14 @@ static inline int parse_nal_units(AVCodecParserContext *s,
H264ParseContext *p = s->priv_data; H264ParseContext *p = s->priv_data;
H264Context *h = &p->h; H264Context *h = &p->h;
H264SliceContext *sl = &h->slice_ctx[0]; H264SliceContext *sl = &h->slice_ctx[0];
H2645NAL nal = { NULL };
int buf_index, next_avc; int buf_index, next_avc;
unsigned int pps_id; unsigned int pps_id;
unsigned int slice_type; unsigned int slice_type;
int state = -1, got_reset = 0; int state = -1, got_reset = 0;
const uint8_t *ptr;
int q264 = buf_size >=4 && !memcmp("Q264", buf, 4); int q264 = buf_size >=4 && !memcmp("Q264", buf, 4);
int field_poc[2]; int field_poc[2];
int ret;
/* set some sane default values */ /* set some sane default values */
s->pict_type = AV_PICTURE_TYPE_I; s->pict_type = AV_PICTURE_TYPE_I;
@ -250,7 +251,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
buf_index = 0; buf_index = 0;
next_avc = h->is_avc ? 0 : buf_size; next_avc = h->is_avc ? 0 : buf_size;
for (;;) { for (;;) {
int src_length, dst_length, consumed, nalsize = 0; int src_length, consumed, nalsize = 0;
if (buf_index >= next_avc) { if (buf_index >= next_avc) {
nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index); nalsize = get_avc_nalsize(h, buf, buf_size, &buf_index);
@ -283,14 +284,23 @@ static inline int parse_nal_units(AVCodecParserContext *s,
} }
break; break;
} }
ptr = ff_h264_decode_nal(h, sl, buf + buf_index, &dst_length, consumed = ff_h2645_extract_rbsp(buf + buf_index, src_length, &nal);
&consumed, src_length); if (consumed < 0)
if (!ptr || dst_length < 0)
break; break;
buf_index += consumed; buf_index += consumed;
init_get_bits(&h->gb, ptr, 8 * dst_length); ret = init_get_bits8(&nal.gb, nal.data, nal.size);
if (ret < 0)
goto fail;
get_bits1(&nal.gb);
nal.ref_idc = get_bits(&nal.gb, 2);
nal.type = get_bits(&nal.gb, 5);
h->gb = nal.gb;
h->nal_ref_idc = nal.ref_idc;
h->nal_unit_type = nal.type;
switch (h->nal_unit_type) { switch (h->nal_unit_type) {
case NAL_SPS: case NAL_SPS:
ff_h264_decode_seq_parameter_set(h, 0); ff_h264_decode_seq_parameter_set(h, 0);
@ -310,7 +320,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
h->prev_poc_lsb = 0; h->prev_poc_lsb = 0;
/* fall through */ /* fall through */
case NAL_SLICE: case NAL_SLICE:
init_get_bits(&sl->gb, ptr, 8 * dst_length); sl->gb = nal.gb;
get_ue_golomb_long(&sl->gb); // skip first_mb_in_slice get_ue_golomb_long(&sl->gb); // skip first_mb_in_slice
slice_type = get_ue_golomb_31(&sl->gb); slice_type = get_ue_golomb_31(&sl->gb);
s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5]; s->pict_type = ff_h264_golomb_to_pict_type[slice_type % 5];
@ -322,18 +332,18 @@ static inline int parse_nal_units(AVCodecParserContext *s,
if (pps_id >= MAX_PPS_COUNT) { if (pps_id >= MAX_PPS_COUNT) {
av_log(h->avctx, AV_LOG_ERROR, av_log(h->avctx, AV_LOG_ERROR,
"pps_id %u out of range\n", pps_id); "pps_id %u out of range\n", pps_id);
return -1; goto fail;
} }
if (!h->pps_buffers[pps_id]) { if (!h->pps_buffers[pps_id]) {
av_log(h->avctx, AV_LOG_ERROR, av_log(h->avctx, AV_LOG_ERROR,
"non-existing PPS %u referenced\n", pps_id); "non-existing PPS %u referenced\n", pps_id);
return -1; goto fail;
} }
h->pps = *h->pps_buffers[pps_id]; h->pps = *h->pps_buffers[pps_id];
if (!h->sps_buffers[h->pps.sps_id]) { if (!h->sps_buffers[h->pps.sps_id]) {
av_log(h->avctx, AV_LOG_ERROR, av_log(h->avctx, AV_LOG_ERROR,
"non-existing SPS %u referenced\n", h->pps.sps_id); "non-existing SPS %u referenced\n", h->pps.sps_id);
return -1; goto fail;
} }
h->sps = *h->sps_buffers[h->pps.sps_id]; h->sps = *h->sps_buffers[h->pps.sps_id];
h->frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num); h->frame_num = get_bits(&sl->gb, h->sps.log2_max_frame_num);
@ -414,7 +424,7 @@ static inline int parse_nal_units(AVCodecParserContext *s,
if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) { if (h->nal_ref_idc && h->nal_unit_type != NAL_IDR_SLICE) {
got_reset = scan_mmco_reset(s); got_reset = scan_mmco_reset(s);
if (got_reset < 0) if (got_reset < 0)
return got_reset; goto fail;
} }
/* Set up the prev_ values for decoding POC of the next picture. */ /* Set up the prev_ values for decoding POC of the next picture. */
@ -492,13 +502,18 @@ static inline int parse_nal_units(AVCodecParserContext *s,
s->field_order = AV_FIELD_UNKNOWN; s->field_order = AV_FIELD_UNKNOWN;
} }
av_freep(&nal.rbsp_buffer);
return 0; /* no need to evaluate the rest */ return 0; /* no need to evaluate the rest */
} }
} }
if (q264) if (q264) {
av_freep(&nal.rbsp_buffer);
return 0; return 0;
}
/* didn't find a picture! */ /* didn't find a picture! */
av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size); av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit with size %d\n", buf_size);
fail:
av_freep(&nal.rbsp_buffer);
return -1; return -1;
} }