On 5/5/2025 8:37 PM, Andreas Rheinhardt wrote: > diff --git a/libavcodec/cbs_apv.c b/libavcodec/cbs_apv.c > index ebf57d3bbb..ddc363fbf3 100644 > --- a/libavcodec/cbs_apv.c > +++ b/libavcodec/cbs_apv.c > @@ -276,12 +276,14 @@ static int cbs_apv_read_unit(CodedBitstreamContext *ctx, > if (err < 0) > return err; > > - // Each tile inside the frame has pointers into the unit > - // data buffer; make a single reference here for all of > - // them together. > - frame->tile_data_ref = av_buffer_ref(unit->data_ref); > - if (!frame->tile_data_ref) > - return AVERROR(ENOMEM); > + if (unit->data_ref) { > + // Each tile inside the frame has pointers into the unit > + // data buffer; make a single reference here for all of > + // them together. > + frame->tile_data_ref = av_buffer_ref(unit->data_ref); > + if (!frame->tile_data_ref) > + return AVERROR(ENOMEM); > + } > } > break; > case APV_PBU_ACCESS_UNIT_INFORMATION: > diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c > index 4edb6ecd50..40c3cc8167 100644 > --- a/libavcodec/cbs_av1.c > +++ b/libavcodec/cbs_av1.c > @@ -824,9 +824,11 @@ static int cbs_av1_ref_tile_data(CodedBitstreamContext *ctx, > // Must be byte-aligned at this point. > av_assert0(pos % 8 == 0); > > - *data_ref = av_buffer_ref(unit->data_ref); > - if (!*data_ref) > - return AVERROR(ENOMEM); > + if (unit->data_ref) { > + *data_ref = av_buffer_ref(unit->data_ref); > + if (!*data_ref) > + return AVERROR(ENOMEM); > + } > > *data = unit->data + pos / 8; > *data_size = unit->data_size - pos / 8; > diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c > index 369e3ac876..45b0c2ce87 100644 > --- a/libavcodec/cbs_h2645.c > +++ b/libavcodec/cbs_h2645.c > @@ -902,9 +902,11 @@ static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx, > len = unit->data_size; > > slice->data_size = len - pos / 8; > - slice->data_ref = av_buffer_ref(unit->data_ref); > - if (!slice->data_ref) > - return AVERROR(ENOMEM); > + if (unit->data_ref) { > + slice->data_ref = av_buffer_ref(unit->data_ref); > + if (!slice->data_ref) > + return AVERROR(ENOMEM); > + } > slice->data = unit->data + pos / 8; > slice->data_bit_start = pos % 8; > } > @@ -1039,9 +1041,11 @@ static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx, > len = unit->data_size; > > slice->data_size = len - pos / 8; > - slice->data_ref = av_buffer_ref(unit->data_ref); > - if (!slice->data_ref) > - return AVERROR(ENOMEM); > + if (unit->data_ref) { > + slice->data_ref = av_buffer_ref(unit->data_ref); > + if (!slice->data_ref) > + return AVERROR(ENOMEM); > + } > slice->data = unit->data + pos / 8; > slice->data_bit_start = pos % 8; > } > @@ -1205,9 +1209,11 @@ static int cbs_h266_read_nal_unit(CodedBitstreamContext *ctx, > > slice->header_size = pos / 8; > slice->data_size = len - pos / 8; > - slice->data_ref = av_buffer_ref(unit->data_ref); > - if (!slice->data_ref) > - return AVERROR(ENOMEM); > + if (unit->data_ref) { > + slice->data_ref = av_buffer_ref(unit->data_ref); > + if (!slice->data_ref) > + return AVERROR(ENOMEM); > + } > slice->data = unit->data + pos / 8; > slice->data_bit_start = pos % 8; > } > diff --git a/libavcodec/cbs_jpeg.c b/libavcodec/cbs_jpeg.c > index 281606e7af..cfb9b6d46c 100644 > --- a/libavcodec/cbs_jpeg.c > +++ b/libavcodec/cbs_jpeg.c > @@ -255,9 +255,11 @@ static int cbs_jpeg_read_unit(CodedBitstreamContext *ctx, > av_assert0(pos % 8 == 0); > if (pos > 0) { > scan->data_size = unit->data_size - pos / 8; > - scan->data_ref = av_buffer_ref(unit->data_ref); > - if (!scan->data_ref) > - return AVERROR(ENOMEM); > + if (unit->data_ref) { > + scan->data_ref = av_buffer_ref(unit->data_ref); > + if (!scan->data_ref) > + return AVERROR(ENOMEM); > + } > scan->data = unit->data + pos / 8; > } > > diff --git a/libavcodec/cbs_mpeg2.c b/libavcodec/cbs_mpeg2.c > index 37fc28a4e6..4b51647b13 100644 > --- a/libavcodec/cbs_mpeg2.c > +++ b/libavcodec/cbs_mpeg2.c > @@ -234,9 +234,11 @@ static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx, > len = unit->data_size; > > slice->data_size = len - pos / 8; > - slice->data_ref = av_buffer_ref(unit->data_ref); > - if (!slice->data_ref) > - return AVERROR(ENOMEM); > + if (unit->data_ref) { > + slice->data_ref = av_buffer_ref(unit->data_ref); > + if (!slice->data_ref) > + return AVERROR(ENOMEM); > + } > slice->data = unit->data + pos / 8; > > slice->data_bit_start = pos % 8; > diff --git a/libavcodec/cbs_vp8.c b/libavcodec/cbs_vp8.c > index 1f80f34faf..9bfba71d9e 100644 > --- a/libavcodec/cbs_vp8.c > +++ b/libavcodec/cbs_vp8.c > @@ -344,9 +344,11 @@ static int cbs_vp8_read_unit(CodedBitstreamContext *ctx, > pos = (pos + 7) / 8; > av_assert0(pos <= unit->data_size); > > - frame->data_ref = av_buffer_ref(unit->data_ref); > - if (!frame->data_ref) > - return AVERROR(ENOMEM); > + if (unit->data_ref) { > + frame->data_ref = av_buffer_ref(unit->data_ref); > + if (!frame->data_ref) > + return AVERROR(ENOMEM); > + } > > frame->data = unit->data + pos; > frame->data_size = unit->data_size - pos; > diff --git a/libavcodec/cbs_vp9.c b/libavcodec/cbs_vp9.c > index ff99fe32fb..3ca52770f3 100644 > --- a/libavcodec/cbs_vp9.c > +++ b/libavcodec/cbs_vp9.c > @@ -460,9 +460,11 @@ static int cbs_vp9_read_unit(CodedBitstreamContext *ctx, > if (pos == unit->data_size) { > // No data (e.g. a show-existing-frame frame). > } else { > - frame->data_ref = av_buffer_ref(unit->data_ref); > - if (!frame->data_ref) > - return AVERROR(ENOMEM); > + if (unit->data_ref) { > + frame->data_ref = av_buffer_ref(unit->data_ref); > + if (!frame->data_ref) > + return AVERROR(ENOMEM); > + } You can use av_buffer_replace() for all of these to simplify this change.