avformat/iamf_writer, iamfenc: Avoid allocations when using dyn buffers

Use avio_get_dyn_buf()+ffio_free_dyn_buf() instead of
avio_close_dyn_buf()+av_free(). This saves an allocation
(and memcpy) in case all the data fits in the AVIOContext's
write buffer.

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2024-01-31 09:21:55 +01:00
parent b9596daafb
commit 8d4b22edc7
2 changed files with 10 additions and 10 deletions

View File

@ -453,11 +453,11 @@ static int iamf_write_codec_config(const IAMFContext *iamf,
put_bits(&pbc, 3, 0);
flush_put_bits(&pbc);
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
avio_write(pb, header, put_bytes_count(&pbc, 1));
ffio_write_leb(pb, dyn_size);
avio_write(pb, dyn_buf, dyn_size);
av_free(dyn_buf);
ffio_free_dyn_buf(&dyn_bc);
return 0;
}
@ -660,11 +660,11 @@ static int iamf_write_audio_element(const IAMFContext *iamf,
put_bits(&pbc, 3, 0);
flush_put_bits(&pbc);
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
avio_write(pb, header, put_bytes_count(&pbc, 1));
ffio_write_leb(pb, dyn_size);
avio_write(pb, dyn_buf, dyn_size);
av_free(dyn_buf);
ffio_free_dyn_buf(&dyn_bc);
return 0;
}
@ -796,11 +796,11 @@ static int iamf_write_mixing_presentation(const IAMFContext *iamf,
put_bits(&pbc, 3, 0);
flush_put_bits(&pbc);
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
avio_write(pb, header, put_bytes_count(&pbc, 1));
ffio_write_leb(pb, dyn_size);
avio_write(pb, dyn_buf, dyn_size);
av_free(dyn_buf);
ffio_free_dyn_buf(&dyn_bc);
return 0;
}

View File

@ -257,10 +257,10 @@ static int write_parameter_block(AVFormatContext *s, const AVIAMFParamDefinition
}
}
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
ffio_write_leb(s->pb, dyn_size);
avio_write(s->pb, dyn_buf, dyn_size);
av_free(dyn_buf);
ffio_free_dyn_buf(&dyn_bc);
return 0;
}
@ -340,10 +340,10 @@ static int iamf_write_packet(AVFormatContext *s, AVPacket *pkt)
if (st->id > 17)
ffio_write_leb(dyn_bc, st->id);
dyn_size = avio_close_dyn_buf(dyn_bc, &dyn_buf);
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
ffio_write_leb(s->pb, dyn_size + pkt->size);
avio_write(s->pb, dyn_buf, dyn_size);
av_free(dyn_buf);
ffio_free_dyn_buf(&dyn_bc);
avio_write(s->pb, pkt->data, pkt->size);
return 0;