diff --git a/src/h264/nal.c b/src/h264/nal.c index 68623897e..180cedd33 100644 --- a/src/h264/nal.c +++ b/src/h264/nal.c @@ -155,7 +155,8 @@ int h264_fu_hdr_decode(struct h264_fu *fu, struct mbuf *mb) * * @note: copied from ffmpeg source */ -const uint8_t *h264_find_startcode(const uint8_t *p, const uint8_t *end) +static const uint8_t *h264_find_startcode_int(const uint8_t *p, + const uint8_t *end) { const uint8_t *a = p + 4 - ((size_t)p & 3); @@ -191,6 +192,18 @@ const uint8_t *h264_find_startcode(const uint8_t *p, const uint8_t *end) } +const uint8_t *h264_find_startcode(const uint8_t *p, const uint8_t *end) +{ + const uint8_t *out = h264_find_startcode_int(p, end); + + /* check for 4-byte startcode */ + if (plong_startcode ? + sizeof(nal_seq4) : sizeof(nal_seq3); int err; err = h264_nal_header_decode(&h264_hdr, src); @@ -533,7 +537,9 @@ static int depack_handle_h264(struct state *st, bool marker, --src->pos; /* prepend H.264 NAL start sequence */ - err = mbuf_write_mem(st->mb, nal_seq, sizeof(nal_seq)); + err = mbuf_write_mem(st->mb, + st->long_startcode ? nal_seq4 : nal_seq3, + nal_seq_len); err |= mbuf_write_mem(st->mb, mbuf_buf(src), mbuf_get_left(src)); @@ -561,7 +567,9 @@ static int depack_handle_h264(struct state *st, bool marker, st->frag = true; /* prepend H.264 NAL start sequence */ - mbuf_write_mem(st->mb, nal_seq, sizeof(nal_seq)); + mbuf_write_mem(st->mb, + st->long_startcode ? nal_seq4 : nal_seq3, + nal_seq_len); /* encode NAL header back to buffer */ err = h264_nal_header_encode(st->mb, &h264_hdr); @@ -601,7 +609,8 @@ static int depack_handle_h264(struct state *st, bool marker, --src->pos; err = mbuf_write_mem(st->mb, - nal_seq, sizeof(nal_seq)); + st->long_startcode ? nal_seq4 : nal_seq3, + nal_seq_len); err |= mbuf_write_mem(st->mb, mbuf_buf(src), len); if (err) goto out; @@ -661,11 +670,7 @@ static int packet_handler(bool marker, uint64_t rtp_ts, } -/* bitstream in Annex-B format (with startcode 00 00 01) */ -static const char *bitstream = "000001650010e2238712983719283719823798"; - - -int test_h264_packet(void) +static int test_h264_packet_base(const char *bs, bool long_startcode) { struct state state; const size_t MAX_PKTSIZE = 8; @@ -673,9 +678,10 @@ int test_h264_packet(void) memset(&state, 0, sizeof(state)); - state.len = strlen(bitstream)/2; + state.long_startcode = long_startcode; + state.len = strlen(bs)/2; - err = str_hex(state.buf, state.len, bitstream); + err = str_hex(state.buf, state.len, bs); if (err) return err; @@ -696,3 +702,32 @@ int test_h264_packet(void) return err; } + + +/* bitstream in Annex-B format (with startcode 00 00 01) */ +static const char *bitstream = + "0000016701020304" + "0000016801020304" + "000001650010e2238712983719283719823798"; + + +/* bitstream in Annex-B format (with startcode 00 00 00 01) */ +static const char *bitstream_long = + "000000016701020304" + "000000016801020304" + "00000001650010e2238712983719283719823798"; + + +int test_h264_packet(void) +{ + int err; + + err = test_h264_packet_base(bitstream, false); + TEST_ERR(err); + + err = test_h264_packet_base(bitstream_long, true); + TEST_ERR(err); + + out: + return err; +}