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 | uint8_t *pps; | 36 | uint8_t *pps; |
| 37 | int sps_size; | 37 | int sps_size; |
| 38 | int pps_size; | 38 | int pps_size; |
| 39 | unsigned sps_buf_size; | ||
| 40 | unsigned pps_buf_size; | ||
| 39 | uint8_t length_size; | 41 | uint8_t length_size; |
| 40 | uint8_t new_idr; | 42 | uint8_t new_idr; |
| 41 | uint8_t idr_sps_seen; | 43 | uint8_t idr_sps_seen; |
| @@ -130,16 +132,33 @@ pps: · 88 unmodified lines | |||
| 130 | memset(out + total_size, 0, padding); | 132 | memset(out + total_size, 0, padding); |
| 131 | 133 | ||
| 132 | if (pps_offset) { | 134 | if (pps_offset) { |
| 133 | s->sps = out; | 135 | uint8_t *sps; |
| 136 | |||
| 134 | s->sps_size = pps_offset; | 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 | } else { | 145 | } else { |
| 136 | av_log(ctx, AV_LOG_WARNING, | 146 | av_log(ctx, AV_LOG_WARNING, |
| 137 | "Warning: SPS NALU missing or invalid. " | 147 | "Warning: SPS NALU missing or invalid. " |
| 138 | "The resulting stream may not play.\n"); | 148 | "The resulting stream may not play.\n"); |
| 139 | } | 149 | } |
| 140 | if (pps_offset < total_size) { | 150 | if (pps_offset < total_size) { |
| 141 | s->pps = out + pps_offset; | 151 | uint8_t *pps; |
| 152 | |||
| 142 | s->pps_size = total_size - pps_offset; | 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 | } else { | 162 | } else { |
| 144 | av_log(ctx, AV_LOG_WARNING, | 163 | av_log(ctx, AV_LOG_WARNING, |
| 145 | "Warning: PPS NALU missing or invalid. " | 164 | "Warning: PPS NALU missing or invalid. " |
| @@ -153,6 +172,35 @@ pps: · 7 unmodified lines | |||
| 153 | return length_size; | 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 | static int h264_mp4toannexb_init(AVBSFContext *ctx) | 204 | static int h264_mp4toannexb_init(AVBSFContext *ctx) |
| 157 | { | 205 | { |
| 158 | H264BSFContext *s = ctx->priv_data; | 206 | H264BSFContext *s = ctx->priv_data; |
| @@ -211,6 +259,9 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 52 unmodified lines | |||
| 211 | if (j) \ | 259 | if (j) \ |
| 212 | av_log(__VA_ARGS__) | 260 | av_log(__VA_ARGS__) |
| 213 | for (int j = 0; j < 2; j++) { | 261 | for (int j = 0; j < 2; j++) { |
| 262 | int sps_count = 0; | ||
| 263 | int pps_count = 0; | ||
| 264 | |||
| 214 | buf = in->data; | 265 | buf = in->data; |
| 215 | new_idr = s->new_idr; | 266 | new_idr = s->new_idr; |
| 216 | sps_seen = s->idr_sps_seen; | 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 | if (unit_type == H264_NAL_SPS) { | 293 | if (unit_type == H264_NAL_SPS) { |
| 243 | sps_seen = new_idr = 1; | 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 | } else if (unit_type == H264_NAL_PPS) { | 300 | } else if (unit_type == H264_NAL_PPS) { |
| 245 | pps_seen = new_idr = 1; | 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 | /* if SPS has not been seen yet, prepend the AVCC one to PPS */ | 307 | /* if SPS has not been seen yet, prepend the AVCC one to PPS */ |
| 247 | if (!sps_seen) { | 308 | if (!sps_seen) { |
| 248 | if (!s->sps_size) { | 309 | if (!s->sps_size) { |
| @@ -262,9 +323,10 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *opkt) · 13 unmodified lines | |||
| 262 | 323 | ||
| 263 | /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */ | 324 | /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */ |
| 264 | if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) { | 325 | if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) { |
| 265 | if (ctx->par_out->extradata) | 326 | if (s->sps_size) |
| 266 | count_or_copy(&out, &out_size, ctx->par_out->extradata, | 327 | count_or_copy(&out, &out_size, s->sps, s->sps_size, PS_OUT_OF_BAND, j); |
| 267 | ctx->par_out->extradata_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 | new_idr = 0; | 330 | new_idr = 0; |
| 269 | /* if only SPS has been seen, also insert PPS */ | 331 | /* if only SPS has been seen, also insert PPS */ |
| 270 | } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) { | 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 | else | 342 | else |
| 281 | ps = PS_NONE; | 343 | ps = PS_NONE; |
| 282 | count_or_copy(&out, &out_size, buf, nal_size, ps, j); | 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 | new_idr = 1; | 346 | new_idr = 1; |
| 285 | sps_seen = 0; | 347 | sps_seen = 0; |
| 286 | pps_seen = 0; | 348 | pps_seen = 0; |
| @@ -320,6 +382,14 @@ fail: · 33 unmodified lines | |||
| 320 | return ret; | 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 | static void h264_mp4toannexb_flush(AVBSFContext *ctx) | 393 | static void h264_mp4toannexb_flush(AVBSFContext *ctx) |
| 324 | { | 394 | { |
| 325 | H264BSFContext *s = ctx->priv_data; | 395 | H264BSFContext *s = ctx->priv_data; |
| @@ -339,5 +409,6 @@ const FFBitStreamFilter ff_h264_mp4toannexb_bsf = { · 13 unmodified lines | |||
| 339 | .priv_data_size = sizeof(H264BSFContext), | 409 | .priv_data_size = sizeof(H264BSFContext), |
| 340 | .init = h264_mp4toannexb_init, | 410 | .init = h264_mp4toannexb_init, |
| 341 | .filter = h264_mp4toannexb_filter, | 411 | .filter = h264_mp4toannexb_filter, |
| 412 | .close = h264_mp4toannexb_close, | ||
| 342 | .flush = h264_mp4toannexb_flush, | 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 | # this sample contains field-coded frames, with both fields in a single packet | 224 | # this sample contains field-coded frames, with both fields in a single packet |
| 225 | FATE_H264-$(call FRAMECRC, MOV, H264) += fate-h264-twofields-packet | 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 | FATE_H264-$(call FRAMECRC, MATROSKA, H264) += fate-h264-direct-bff | 230 | FATE_H264-$(call FRAMECRC, MATROSKA, H264) += fate-h264-direct-bff |
| 229 | FATE_H264-$(call FRAMECRC, FLV, H264, SCALE_FILTER) += fate-h264-brokensps-2580 | 231 | FATE_H264-$(call FRAMECRC, FLV, H264, SCALE_FILTER) += fate-h264-brokensps-2580 |
| 230 | FATE_H264-$(call FRAMECRC, MXF, H264, PCM_S24LE_DECODER SCALE_FILTER ARESAMPLE_FILTER) += fate-h264-xavc-4389 | 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 | fate-h264-conformance-sva_nl2_e: CMD = framecrc -i $(TARGET_SAMPLES)/h264-conformance/SVA_NL2_E.264 | 428 | fate-h264-conformance-sva_nl2_e: CMD = framecrc -i $(TARGET_SAMPLES)/h264-conformance/SVA_NL2_E.264 |
| 427 | 429 | ||
| 428 | fate-h264-bsf-mp4toannexb: CMD = md5 -i $(TARGET_SAMPLES)/h264/interlaced_crop.mp4 -c:v copy -f h264 | 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 | fate-h264-crop-to-container: CMD = framemd5 -i $(TARGET_SAMPLES)/h264/crop-to-container-dims-canon.mov | 436 | fate-h264-crop-to-container: CMD = framemd5 -i $(TARGET_SAMPLES)/h264/crop-to-container-dims-canon.mov |
| 431 | fate-h264-direct-bff: CMD = framecrc -i $(TARGET_SAMPLES)/h264/direct-bff.mkv | 437 | fate-h264-direct-bff: CMD = framecrc -i $(TARGET_SAMPLES)/h264/direct-bff.mkv |