summaryrefslogtreecommitdiff
path: root/libavcodec/cbs_h2645.c
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2019-11-17 08:34:35 +0100
committerMark Thompson <sw@jkqxz.net>2019-11-17 23:31:44 +0000
commit7c92eaace2b338e0b3acc18e1543b365610578fd (patch)
tree51f9343c4cf5bfbb156c3a303f0839df57bcb31e /libavcodec/cbs_h2645.c
parent5d8d9e032cafa88e99046478090dd5a629e3d51c (diff)
avcodec/cbs: Factor out common code for writing units
All cbs-functions to write units share a common pattern: 1. They check whether they have a write buffer (that is used to store the unit's data until the needed size becomes known after writing the unit when a dedicated buffer will be allocated). 2. They use this buffer for a PutBitContext. 3. The (codec-specific) writing takes place through the PutBitContext. 4. The return value is checked. AVERROR(ENOSPC) here always indicates that the buffer was too small and leads to a reallocation of said buffer. 5. The final buffer will be allocated and the data copied. This commit factors this common code out in a single function in cbs.c. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
Diffstat (limited to 'libavcodec/cbs_h2645.c')
-rw-r--r--libavcodec/cbs_h2645.c67
1 files changed, 2 insertions, 65 deletions
diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c
index 5dd371153a..923f77dcb4 100644
--- a/libavcodec/cbs_h2645.c
+++ b/libavcodec/cbs_h2645.c
@@ -1380,65 +1380,6 @@ static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
return 0;
}
-static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
- CodedBitstreamUnit *unit)
-{
- CodedBitstreamH2645Context *priv = ctx->priv_data;
- enum AVCodecID codec_id = ctx->codec->codec_id;
- PutBitContext pbc;
- int err;
-
- if (!priv->write_buffer) {
- // Initial write buffer size is 1MB.
- priv->write_buffer_size = 1024 * 1024;
-
- reallocate_and_try_again:
- err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
- if (err < 0) {
- av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
- "sufficiently large write buffer (last attempt "
- "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
- return err;
- }
- }
-
- init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
-
- if (codec_id == AV_CODEC_ID_H264)
- err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
- else
- err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
-
- if (err == AVERROR(ENOSPC)) {
- // Overflow.
- priv->write_buffer_size *= 2;
- goto reallocate_and_try_again;
- }
- // Overflow but we didn't notice.
- av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
-
- if (err < 0) {
- // Write failed for some other reason.
- return err;
- }
-
- if (put_bits_count(&pbc) % 8)
- unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
- else
- unit->data_bit_padding = 0;
-
- unit->data_size = (put_bits_count(&pbc) + 7) / 8;
- flush_put_bits(&pbc);
-
- err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
- if (err < 0)
- return err;
-
- memcpy(unit->data, priv->write_buffer, unit->data_size);
-
- return 0;
-}
-
static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag)
{
@@ -1533,8 +1474,6 @@ static void cbs_h264_close(CodedBitstreamContext *ctx)
ff_h2645_packet_uninit(&h264->common.read_packet);
- av_freep(&h264->common.write_buffer);
-
for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
av_buffer_unref(&h264->sps_ref[i]);
for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
@@ -1548,8 +1487,6 @@ static void cbs_h265_close(CodedBitstreamContext *ctx)
ff_h2645_packet_uninit(&h265->common.read_packet);
- av_freep(&h265->common.write_buffer);
-
for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
av_buffer_unref(&h265->vps_ref[i]);
for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
@@ -1565,7 +1502,7 @@ const CodedBitstreamType ff_cbs_type_h264 = {
.split_fragment = &cbs_h2645_split_fragment,
.read_unit = &cbs_h264_read_nal_unit,
- .write_unit = &cbs_h2645_write_nal_unit,
+ .write_unit = &cbs_h264_write_nal_unit,
.assemble_fragment = &cbs_h2645_assemble_fragment,
.close = &cbs_h264_close,
@@ -1578,7 +1515,7 @@ const CodedBitstreamType ff_cbs_type_h265 = {
.split_fragment = &cbs_h2645_split_fragment,
.read_unit = &cbs_h265_read_nal_unit,
- .write_unit = &cbs_h2645_write_nal_unit,
+ .write_unit = &cbs_h265_write_nal_unit,
.assemble_fragment = &cbs_h2645_assemble_fragment,
.close = &cbs_h265_close,