Skip to content

Instantly share code, notes, and snippets.

@rcombs

rcombs/stdin Secret

Created June 24, 2015 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcombs/f0411529f620d81508b4 to your computer and use it in GitHub Desktop.
Save rcombs/f0411529f620d81508b4 to your computer and use it in GitHub Desktop.
diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c
index ae96ee9..dfbd643 100644
--- a/libavcodec/h264_mp4toannexb_bsf.c
+++ b/libavcodec/h264_mp4toannexb_bsf.c
@@ -63,31 +63,56 @@ static int alloc_and_copy(uint8_t **poutbuf, int *poutbuf_size,
return 0;
}
+#define NAL_SPS 33
+#define NAL_PPS 34
+
static int h264_extradata_to_annexb(H264BSFContext *ctx, AVCodecContext *avctx, const int padding)
{
- uint16_t unit_size;
+ int hevc = 1;
+ uint16_t unit_size, unit_nb;
uint64_t total_size = 0;
- uint8_t *out = NULL, unit_nb, sps_done = 0,
- sps_seen = 0, pps_seen = 0;
- const uint8_t *extradata = avctx->extradata + 4;
+ uint8_t *out = NULL, sps_done = 0,
+ sps_seen = 0, pps_seen = 0, set_nb;
+ const uint8_t *extradata = avctx->extradata + (hevc ? 21 : 4);
static const uint8_t nalu_header[4] = { 0, 0, 0, 1 };
int length_size = (*extradata++ & 0x3) + 1; // retrieve length coded size
ctx->sps_offset = ctx->pps_offset = -1;
+ set_nb = hevc ? *extradata++ : 1;
+
+ while (set_nb--) {
+ int type = hevc ? *extradata++ & 0x3f : 0;
/* retrieve sps and pps unit(s) */
- unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
+ if (hevc) {
+ unit_nb = AV_RB16(extradata);
+ extradata += 2;
+ if (type == NAL_SPS) {
+ ctx->sps_offset = total_size;
+ sps_seen = 1;
+ } else if (type == NAL_PPS) {
+ ctx->pps_offset = total_size;
+ pps_seen = 1;
+ }
+ } else {
+ unit_nb = *extradata++ & 0x1f; /* number of units */
if (!unit_nb) {
+ type = NAL_PPS;
goto pps;
} else {
+ type = NAL_SPS;
ctx->sps_offset = 0;
sps_seen = 1;
}
+ }
while (unit_nb--) {
int err;
unit_size = AV_RB16(extradata);
+ if (type == NAL_SPS || type == NAL_PPS || !hevc) {
+ if (!hevc)
+ ctx->sps_offset = total_size;
total_size += unit_size + 4;
if (total_size > INT_MAX - padding) {
av_log(avctx, AV_LOG_ERROR,
@@ -105,9 +130,10 @@ static int h264_extradata_to_annexb(H264BSFContext *ctx, AVCodecContext *avctx,
return err;
memcpy(out + total_size - unit_size - 4, nalu_header, 4);
memcpy(out + total_size - unit_size, extradata + 2, unit_size);
+ }
extradata += 2 + unit_size;
pps:
- if (!unit_nb && !sps_done++) {
+ if (!hevc && !unit_nb && !sps_done++) {
unit_nb = *extradata++; /* number of pps unit(s) */
if (unit_nb) {
ctx->pps_offset = (extradata - 1) - (avctx->extradata + 4);
@@ -115,6 +141,7 @@ pps:
}
}
}
+ }
if (out)
memset(out + total_size, 0, padding);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment