- /* if this is a new IDR picture following an IDR picture, reset the idr flag.
- * Just check first_mb_in_slice to be 0 as this is the simplest solution.
- * This could be checking idr_pic_id instead, but would complexify the parsing. */
- if (!s->new_idr && unit_type == H264_NAL_IDR_SLICE && (buf[1] & 0x80))
- s->new_idr = 1;
-
- /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
- if (s->new_idr && unit_type == H264_NAL_IDR_SLICE && !s->idr_sps_seen && !s->idr_pps_seen) {
- if ((ret=alloc_and_copy(out,
- ctx->par_out->extradata, ctx->par_out->extradata_size,
- buf, nal_size, 1)) < 0)
- goto fail;
- s->new_idr = 0;
- /* if only SPS has been seen, also insert PPS */
- } else if (s->new_idr && unit_type == H264_NAL_IDR_SLICE && s->idr_sps_seen && !s->idr_pps_seen) {
- if (s->pps_offset == -1) {
- av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
- if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size, 0)) < 0)
- goto fail;
- } else if ((ret = alloc_and_copy(out,
- ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset,
- buf, nal_size, 1)) < 0)
- goto fail;
- } else {
- if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size, unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS)) < 0)
+ /* If this is a new IDR picture following an IDR picture, reset the idr flag.
+ * Just check first_mb_in_slice to be 0 as this is the simplest solution.
+ * This could be checking idr_pic_id instead, but would complexify the parsing. */
+ if (!new_idr && unit_type == H264_NAL_IDR_SLICE && (buf[1] & 0x80))
+ new_idr = 1;
+
+ /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
+ if (new_idr && unit_type == H264_NAL_IDR_SLICE && !sps_seen && !pps_seen) {
+ if (ctx->par_out->extradata)
+ count_or_copy(&out, &out_size, ctx->par_out->extradata,
+ ctx->par_out->extradata_size, -1, j);
+ new_idr = 0;
+ /* if only SPS has been seen, also insert PPS */
+ } else if (new_idr && unit_type == H264_NAL_IDR_SLICE && sps_seen && !pps_seen) {
+ if (!s->pps_size) {
+ LOG_ONCE(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
+ } else {
+ count_or_copy(&out, &out_size, s->pps, s->pps_size, -1, j);
+ }
+ }
+
+ count_or_copy(&out, &out_size, buf, nal_size,
+ unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS, j);
+ if (!new_idr && unit_type == H264_NAL_SLICE) {
+ new_idr = 1;
+ sps_seen = 0;
+ pps_seen = 0;
+ }
+
+ buf += nal_size;
+ } while (buf < buf_end);
+
+ if (!j) {
+ if (out_size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
+ ret = AVERROR_INVALIDDATA;