aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/cbs_av1.c
diff options
context:
space:
mode:
authorAndreas Rheinhardt <andreas.rheinhardt@gmail.com>2019-11-17 08:34:35 +0100
committerJames Almer <jamrial@gmail.com>2019-12-31 16:57:37 -0300
commit1cf238d3bfefdfd3345ca262f57e08a798bb0d90 (patch)
tree4586fc542d44b5baae1e999453781e0834579a97 /libavcodec/cbs_av1.c
parentcb3a59ca82d070628a4ea12ffa91155328e6a05d (diff)
downloadffmpeg-1cf238d3bfefdfd3345ca262f57e08a798bb0d90.tar.gz
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> (cherry picked from commit 7c92eaace2b338e0b3acc18e1543b365610578fd)
Diffstat (limited to 'libavcodec/cbs_av1.c')
-rw-r--r--libavcodec/cbs_av1.c59
1 files changed, 5 insertions, 54 deletions
diff --git a/libavcodec/cbs_av1.c b/libavcodec/cbs_av1.c
index cf3561a4ea..ffb68adcd3 100644
--- a/libavcodec/cbs_av1.c
+++ b/libavcodec/cbs_av1.c
@@ -1205,66 +1205,19 @@ static int cbs_av1_write_obu(CodedBitstreamContext *ctx,
return AVERROR(ENOSPC);
if (obu->obu_size > 0) {
- memmove(priv->write_buffer + data_pos,
- priv->write_buffer + start_pos, header_size);
+ memmove(pbc->buf + data_pos,
+ pbc->buf + start_pos, header_size);
skip_put_bytes(pbc, header_size);
if (td) {
- memcpy(priv->write_buffer + data_pos + header_size,
+ memcpy(pbc->buf + data_pos + header_size,
td->data, td->data_size);
skip_put_bytes(pbc, td->data_size);
}
}
- return 0;
-}
-
-static int cbs_av1_write_unit(CodedBitstreamContext *ctx,
- CodedBitstreamUnit *unit)
-{
- CodedBitstreamAV1Context *priv = ctx->priv_data;
- 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);
-
- err = cbs_av1_write_obu(ctx, unit, &pbc);
- if (err == AVERROR(ENOSPC)) {
- // Overflow.
- priv->write_buffer_size *= 2;
- goto reallocate_and_try_again;
- }
- if (err < 0)
- return err;
-
- // Overflow but we didn't notice.
- av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
-
// OBU data must be byte-aligned.
- av_assert0(put_bits_count(&pbc) % 8 == 0);
-
- unit->data_size = put_bits_count(&pbc) / 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);
+ av_assert0(put_bits_count(pbc) % 8 == 0);
return 0;
}
@@ -1303,8 +1256,6 @@ static void cbs_av1_close(CodedBitstreamContext *ctx)
av_buffer_unref(&priv->sequence_header_ref);
av_buffer_unref(&priv->frame_header_ref);
-
- av_freep(&priv->write_buffer);
}
const CodedBitstreamType ff_cbs_type_av1 = {
@@ -1314,7 +1265,7 @@ const CodedBitstreamType ff_cbs_type_av1 = {
.split_fragment = &cbs_av1_split_fragment,
.read_unit = &cbs_av1_read_unit,
- .write_unit = &cbs_av1_write_unit,
+ .write_unit = &cbs_av1_write_obu,
.assemble_fragment = &cbs_av1_assemble_fragment,
.close = &cbs_av1_close,