If there is a single group of SPS/PPS before an IDR frame, but no SPS/PPS after that, we will miss the chance to reset idr_sps_seen/idr_pps_seen. No SPS/PPS are inserted afterwards. This patch saves in-band SPS/PPS and insert them before IDR frames when necessary.
Unified · Split
| @@ -36,6 +36,8 @@ typedef struct H264BSFContext { · 35 unmodified lines | ||
| 36 | 36 | uint8_t *pps; |
| 37 | 37 | int sps_size; |
| 38 | 38 | int pps_size; |
| 39 | + unsigned sps_buf_size; | |
| 40 | + unsigned pps_buf_size; | |
| 39 | 41 | uint8_t length_size; |
| 40 | 42 | uint8_t new_idr; |
| 41 | 43 | uint8_t idr_sps_seen; |
| @@ -130,16 +132,33 @@ pps: · 88 unmodified lines | ||
| 130 | 132 | memset(out + total_size, 0, padding); |
| 131 | 133 | |
| 132 | 134 | if (pps_offset) { |
| 133 | − s->sps = out; | |
| 135 | + uint8_t *sps; | |
| 136 | + | |
| 134 | 137 | s->sps_size = pps_offset; |
| 138 | + sps = av_fast_realloc(s->sps, &s->sps_buf_size, s->sps_size); | |
| 139 | + if (!sps) { | |
| 140 | + av_free(out); | |
| 141 | + return AVERROR(ENOMEM); | |
| 142 | + } | |
| 143 | + s->sps = sps; | |
| 144 | + memcpy(s->sps, out, s->sps_size); | |
| 135 | 145 | } else { |
| 136 | 146 | av_log(ctx, AV_LOG_WARNING, |
| 137 | 147 | "Warning: SPS NALU missing or invalid. " |
| 138 | 148 | "The resulting stream may not play.\n"); |
| 139 | 149 | } |
| 140 | 150 | if (pps_offset < total_size) { |
| 141 | − s->pps = out + pps_offset; | |
| 151 | + uint8_t *pps; | |
| 152 | + | |
| 142 | 153 | s->pps_size = total_size - pps_offset; |
| 154 | + pps = av_fast_realloc(s->pps, &s->pps_buf_size, s->pps_size); | |
| 155 | + if (!pps) { | |
| 156 | + av_freep(&s->sps); | |
| 157 | + av_free(out); | |
| 158 | + return AVERROR(ENOMEM); | |
| 159 | + } | |
| 160 | + s->pps = pps; | |
| 161 | + memcpy(s->pps, out + pps_offset, s->pps_size); | |
| 143 | 162 | } else { |
| 144 | 163 | av_log(ctx, AV_LOG_WARNING, |
| 145 | 164 | "Warning: PPS NALU missing or invalid. " |
| @@ -153,6 +172,35 @@ pps: · 7 unmodified lines | ||
| 153 | 172 | return length_size; |
| 154 | 173 | } |
| 155 | 174 | |
| 175 | +static int h264_mp4toannexb_save_ps(uint8_t **dst, int *dst_size, | |
| 176 | + unsigned *dst_buf_size, | |
| 177 | + const uint8_t *nal, uint32_t nal_size, | |
| 178 | + int first) | |
| 179 | +{ | |
| 180 | + static const uint8_t nalu_header[4] = { 0, 0, 0, 1 }; | |
| 181 | + const int start_code_size = sizeof(nalu_header); | |
| 182 | + uint8_t *ptr; | |
| 183 | + uint32_t size; | |
| 184 | + | |
| 185 | + if (first) | |
| 186 | + size = 0; | |
| 187 | + else | |
| 188 | + size = *dst_size; | |
| 189 | + | |
| 190 | + ptr = av_fast_realloc(*dst, dst_buf_size, size + nal_size + start_code_size); | |
| 191 | + if (!ptr) | |
| 192 | + return AVERROR(ENOMEM); | |
| 193 | + | |
| 194 | + memcpy(ptr + size, nalu_header, start_code_size); | |
| 195 | + size += start_code_size; | |
| 196 | + memcpy(ptr + size, nal, nal_size); | |
| 197 | + size += nal_size; | |
| 198 | + | |
| 199 | + *dst = ptr; | |
| 200 | + *dst_size = size; | |
| 201 | + return 0; | |
| 202 | +} | |
| 203 | + | |
| 156 | 204 | static int h264_mp4toannexb_init(AVBSFContext *ctx) |
| 157 | 205 | { |
| 158 | 206 | H264BSFContext *s = ctx->priv_data; |
| @@ -211,6 +259,9 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 52 unmodified lines | ||
| 211 | 259 | if (j) \ |
| 212 | 260 | av_log(__VA_ARGS__) |
| 213 | 261 | for (int j = 0; j < 2; j++) { |
| 262 | + int sps_count = 0; | |
| 263 | + int pps_count = 0; | |
| 264 | + | |
| 214 | 265 | buf = in->data; |
| 215 | 266 | new_idr = s->new_idr; |
| 216 | 267 | sps_seen = s->idr_sps_seen; |
| @@ -241,8 +292,18 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 24 unmodified lines | ||
| 241 | 292 | |
| 242 | 293 | if (unit_type == H264_NAL_SPS) { |
| 243 | 294 | sps_seen = new_idr = 1; |
| 295 | + if (!j) { | |
| 296 | + h264_mp4toannexb_save_ps(&s->sps, &s->sps_size, &s->sps_buf_size, | |
| 297 | + buf, nal_size, !sps_count); | |
| 298 | + sps_count++; | |
| 299 | + } | |
| 244 | 300 | } else if (unit_type == H264_NAL_PPS) { |
| 245 | 301 | pps_seen = new_idr = 1; |
| 302 | + if (!j) { | |
| 303 | + h264_mp4toannexb_save_ps(&s->pps, &s->pps_size, &s->pps_buf_size, | |
| 304 | + buf, nal_size, !pps_count); | |
| 305 | + pps_count++; | |
| 306 | + } | |
| 246 | 307 | /* if SPS has not been seen yet, prepend the AVCC one to PPS */ |
| 247 | 308 | if (!sps_seen) { |
| 248 | 309 | if (!s->sps_size) { |
| @@ -262,9 +323,10 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 13 unmodified lines | ||
| 262 | 323 | |
| 263 | 324 | /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */ |
| 264 | 325 | if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) { |
| 265 | − if (ctx->par_out->extradata) | |
| 266 | − count_or_copy(&out, &out_size, ctx->par_out->extradata, | |
| 267 | − ctx->par_out->extradata_size, PS_OUT_OF_BAND, j); | |
| 326 | + if (s->sps_size) | |
| 327 | + count_or_copy(&out, &out_size, s->sps, s->sps_size, PS_OUT_OF_BAND, j); | |
| 328 | + if (s->pps_size) | |
| 329 | + count_or_copy(&out, &out_size, s->pps, s->pps_size, PS_OUT_OF_BAND, j); | |
| 268 | 330 | new_idr = 0; |
| 269 | 331 | /* if only SPS has been seen, also insert PPS */ |
| 270 | 332 | } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) { |
| @@ -280,7 +342,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 9 unmodified lines | ||
| 280 | 342 | else |
| 281 | 343 | ps = PS_NONE; |
| 282 | 344 | count_or_copy(&out, &out_size, buf, nal_size, ps, j); |
| 283 | − if (!new_idr && unit_type == H264_NAL_SLICE) { | |
| 345 | + if (unit_type == H264_NAL_SLICE) { | |
| 284 | 346 | new_idr = 1; |
| 285 | 347 | sps_seen = 0; |
| 286 | 348 | pps_seen = 0; |
| @@ -320,6 +382,14 @@ fail: · 33 unmodified lines | ||
| 320 | 382 | return ret; |
| 321 | 383 | } |
| 322 | 384 | |
| 385 | +static void h264_mp4toannexb_close(AVBSFContext *ctx) | |
| 386 | +{ | |
| 387 | + H264BSFContext *s = ctx->priv_data; | |
| 388 | + | |
| 389 | + av_freep(&s->sps); | |
| 390 | + av_freep(&s->pps); | |
| 391 | +} | |
| 392 | + | |
| 323 | 393 | static void h264_mp4toannexb_flush(AVBSFContext *ctx) |
| 324 | 394 | { |
| 325 | 395 | H264BSFContext *s = ctx->priv_data; |
| @@ -339,5 +409,6 @@ const FFBitStreamFilter ff_h264_mp4toannexb_bsf = { · 13 unmodified lines | ||
| 339 | 409 | .priv_data_size = sizeof(H264BSFContext), |
| 340 | 410 | .init = h264_mp4toannexb_init, |
| 341 | 411 | .filter = h264_mp4toannexb_filter, |
| 412 | + .close = h264_mp4toannexb_close, | |
| 342 | 413 | .flush = h264_mp4toannexb_flush, |
| 343 | 414 | }; |
| @@ -224,7 +224,9 @@ FATE_H264-$(call FRAMECRC, MOV, H264) += fate-h264-unescaped-extradata · 223 unmodified lines | ||
| 224 | 224 | # this sample contains field-coded frames, with both fields in a single packet |
| 225 | 225 | FATE_H264-$(call FRAMECRC, MOV, H264) += fate-h264-twofields-packet |
| 226 | 226 | |
| 227 | −FATE_H264-$(call DEMMUX, MOV, H264, H264_MP4TOANNEXB_BSF) += fate-h264-bsf-mp4toannexb | |
| 227 | +FATE_H264-$(call DEMMUX, MOV, H264, H264_MP4TOANNEXB_BSF) += fate-h264-bsf-mp4toannexb \ | |
| 228 | + fate-h264-bsf-mp4toannexb-2 | |
| 229 | + | |
| 228 | 230 | FATE_H264-$(call FRAMECRC, MATROSKA, H264) += fate-h264-direct-bff |
| 229 | 231 | FATE_H264-$(call FRAMECRC, FLV, H264, SCALE_FILTER) += fate-h264-brokensps-2580 |
| 230 | 232 | FATE_H264-$(call FRAMECRC, MXF, H264, PCM_S24LE_DECODER SCALE_FILTER ARESAMPLE_FILTER) += fate-h264-xavc-4389 |
| @@ -426,6 +428,10 @@ fate-h264-conformance-sva_nl1_b: CMD = framecrc -i $(TARGET_SAM · 195 unmodified lines | ||
| 426 | 428 | fate-h264-conformance-sva_nl2_e: CMD = framecrc -i $(TARGET_SAMPLES)/h264-conformance/SVA_NL2_E.264 |
| 427 | 429 | |
| 428 | 430 | fate-h264-bsf-mp4toannexb: CMD = md5 -i $(TARGET_SAMPLES)/h264/interlaced_crop.mp4 -c:v copy -f h264 |
| 431 | +# First IDR is prefixed by SPS/PPS | |
| 432 | +fate-h264-bsf-mp4toannexb-2: CMD = md5 -i $(TARGET_SAMPLES)/h264/ps_prefix_first_idr.mp4 -c:v copy -f h264 | |
| 433 | +fate-h264-bsf-mp4toannexb-2: CMP = oneline | |
| 434 | +fate-h264-bsf-mp4toannexb-2: REF = cffcfa6a2d0b58c9de1f5785f099f41d | |
| 429 | 435 | |
| 430 | 436 | fate-h264-crop-to-container: CMD = framemd5 -i $(TARGET_SAMPLES)/h264/crop-to-container-dims-canon.mov |
| 431 | 437 | fate-h264-direct-bff: CMD = framecrc -i $(TARGET_SAMPLES)/h264/direct-bff.mkv |