Commit 0cc8e34a authored by Mark Thompson's avatar Mark Thompson

Merge commit 'ce5870a3'

* commit 'ce5870a3':
  cbs: Refcount all the things!

Some changes for bitstream API.
Merged-by: 's avatarMark Thompson <sw@jkqxz.net>
parents b656fa71 ce5870a3
......@@ -21,6 +21,7 @@
#include "config.h"
#include "libavutil/avassert.h"
#include "libavutil/buffer.h"
#include "libavutil/common.h"
#include "cbs.h"
......@@ -95,10 +96,11 @@ void ff_cbs_close(CodedBitstreamContext **ctx_ptr)
static void cbs_unit_uninit(CodedBitstreamContext *ctx,
CodedBitstreamUnit *unit)
{
if (ctx->codec->free_unit && unit->content && !unit->content_external)
ctx->codec->free_unit(unit);
av_buffer_unref(&unit->content_ref);
unit->content = NULL;
av_freep(&unit->data);
av_buffer_unref(&unit->data_ref);
unit->data = NULL;
unit->data_size = 0;
unit->data_bit_padding = 0;
}
......@@ -113,7 +115,8 @@ void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
av_freep(&frag->units);
frag->nb_units = 0;
av_freep(&frag->data);
av_buffer_unref(&frag->data_ref);
frag->data = NULL;
frag->data_size = 0;
frag->data_bit_padding = 0;
}
......@@ -133,6 +136,9 @@ static int cbs_read_fragment_content(CodedBitstreamContext *ctx,
continue;
}
av_buffer_unref(&frag->units[i].content_ref);
frag->units[i].content = NULL;
err = ctx->codec->read_unit(ctx, &frag->units[i]);
if (err == AVERROR(ENOSYS)) {
av_log(ctx->log_ctx, AV_LOG_VERBOSE,
......@@ -169,6 +175,27 @@ int ff_cbs_read_extradata(CodedBitstreamContext *ctx,
return cbs_read_fragment_content(ctx, frag);
}
static int cbs_fill_fragment_data(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
const uint8_t *data, size_t size)
{
av_assert0(!frag->data && !frag->data_ref);
frag->data_ref =
av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
if (!frag->data_ref)
return AVERROR(ENOMEM);
frag->data = frag->data_ref->data;
frag->data_size = size;
memcpy(frag->data, data, size);
memset(frag->data + size, 0,
AV_INPUT_BUFFER_PADDING_SIZE);
return 0;
}
int ff_cbs_read_packet(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
const AVPacket *pkt)
......@@ -177,15 +204,23 @@ int ff_cbs_read_packet(CodedBitstreamContext *ctx,
memset(frag, 0, sizeof(*frag));
if (pkt->buf) {
frag->data_ref = av_buffer_ref(pkt->buf);
if (!frag->data_ref)
return AVERROR(ENOMEM);
frag->data = pkt->data;
frag->data_size = pkt->size;
err = ctx->codec->split_fragment(ctx, frag, 0);
} else {
err = cbs_fill_fragment_data(ctx, frag, pkt->data, pkt->size);
if (err < 0)
return err;
}
frag->data = NULL;
frag->data_size = 0;
err = ctx->codec->split_fragment(ctx, frag, 0);
if (err < 0)
return err;
return cbs_read_fragment_content(ctx, frag);
}
......@@ -198,17 +233,14 @@ int ff_cbs_read(CodedBitstreamContext *ctx,
memset(frag, 0, sizeof(*frag));
// (We won't write to this during split.)
frag->data = (uint8_t*)data;
frag->data_size = size;
err = cbs_fill_fragment_data(ctx, frag, data, size);
if (err < 0)
return err;
err = ctx->codec->split_fragment(ctx, frag, 0);
if (err < 0)
return err;
frag->data = NULL;
frag->data_size = 0;
return cbs_read_fragment_content(ctx, frag);
}
......@@ -219,17 +251,25 @@ int ff_cbs_write_fragment_data(CodedBitstreamContext *ctx,
int err, i;
for (i = 0; i < frag->nb_units; i++) {
if (!frag->units[i].content)
CodedBitstreamUnit *unit = &frag->units[i];
if (!unit->content)
continue;
err = ctx->codec->write_unit(ctx, &frag->units[i]);
av_buffer_unref(&unit->data_ref);
unit->data = NULL;
err = ctx->codec->write_unit(ctx, unit);
if (err < 0) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to write unit %d "
"(type %"PRIu32").\n", i, frag->units[i].type);
"(type %"PRIu32").\n", i, unit->type);
return err;
}
}
av_buffer_unref(&frag->data_ref);
frag->data = NULL;
err = ctx->codec->assemble_fragment(ctx, frag);
if (err < 0) {
av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to assemble fragment.\n");
......@@ -394,6 +434,45 @@ int ff_cbs_write_unsigned(CodedBitstreamContext *ctx, PutBitContext *pbc,
}
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
CodedBitstreamUnit *unit,
size_t size,
void (*free)(void *opaque, uint8_t *data))
{
av_assert0(!unit->content && !unit->content_ref);
unit->content = av_mallocz(size);
if (!unit->content)
return AVERROR(ENOMEM);
unit->content_ref = av_buffer_create(unit->content, size,
free, ctx, 0);
if (!unit->content_ref) {
av_freep(&unit->content);
return AVERROR(ENOMEM);
}
return 0;
}
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
CodedBitstreamUnit *unit,
size_t size)
{
av_assert0(!unit->data && !unit->data_ref);
unit->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
if (!unit->data_ref)
return AVERROR(ENOMEM);
unit->data = unit->data_ref->data;
unit->data_size = size;
memset(unit->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
return 0;
}
static int cbs_insert_unit(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
int position)
......@@ -423,21 +502,35 @@ int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
int position,
CodedBitstreamUnitType type,
void *content)
void *content,
AVBufferRef *content_buf)
{
CodedBitstreamUnit *unit;
AVBufferRef *content_ref;
int err;
if (position == -1)
position = frag->nb_units;
av_assert0(position >= 0 && position <= frag->nb_units);
if (content_buf) {
content_ref = av_buffer_ref(content_buf);
if (!content_ref)
return AVERROR(ENOMEM);
} else {
content_ref = NULL;
}
err = cbs_insert_unit(ctx, frag, position);
if (err < 0)
if (err < 0) {
av_buffer_unref(&content_ref);
return err;
}
frag->units[position].type = type;
frag->units[position].content = content;
frag->units[position].content_external = 1;
unit = &frag->units[position];
unit->type = type;
unit->content = content;
unit->content_ref = content_ref;
return 0;
}
......@@ -446,21 +539,35 @@ int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
int position,
CodedBitstreamUnitType type,
uint8_t *data, size_t data_size)
uint8_t *data, size_t data_size,
AVBufferRef *data_buf)
{
CodedBitstreamUnit *unit;
AVBufferRef *data_ref;
int err;
if (position == -1)
position = frag->nb_units;
av_assert0(position >= 0 && position <= frag->nb_units);
if (data_buf)
data_ref = av_buffer_ref(data_buf);
else
data_ref = av_buffer_create(data, data_size, NULL, NULL, 0);
if (!data_ref)
return AVERROR(ENOMEM);
err = cbs_insert_unit(ctx, frag, position);
if (err < 0)
if (err < 0) {
av_buffer_unref(&data_ref);
return err;
}
frag->units[position].type = type;
frag->units[position].data = data;
frag->units[position].data_size = data_size;
unit = &frag->units[position];
unit->type = type;
unit->data = data;
unit->data_size = data_size;
unit->data_ref = data_ref;
return 0;
}
......
......@@ -22,6 +22,8 @@
#include <stddef.h>
#include <stdint.h>
#include "libavutil/buffer.h"
#include "avcodec.h"
......@@ -81,6 +83,11 @@ typedef struct CodedBitstreamUnit {
* This supports non-byte-aligned bitstreams.
*/
size_t data_bit_padding;
/**
* If data is reference counted, a reference to the buffer containing
* data. Null if data is not reference counted.
*/
AVBufferRef *data_ref;
/**
* Pointer to the decomposed form of this unit.
......@@ -91,11 +98,10 @@ typedef struct CodedBitstreamUnit {
*/
void *content;
/**
* Whether the content was supplied externally.
*
* If so, it should not be freed when freeing the unit.
* If content is reference counted, a reference to the buffer containing
* content. Null if content is not reference counted.
*/
int content_external;
AVBufferRef *content_ref;
} CodedBitstreamUnit;
/**
......@@ -123,6 +129,11 @@ typedef struct CodedBitstreamFragment {
* The number of bits which should be ignored in the final byte.
*/
size_t data_bit_padding;
/**
* If data is reference counted, a reference to the buffer containing
* data. Null if data is not reference counted.
*/
AVBufferRef *data_ref;
/**
* Number of units in this fragment.
......@@ -278,28 +289,50 @@ void ff_cbs_fragment_uninit(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag);
/**
* Allocate a new internal content buffer of the given size in the unit.
*
* The content will be zeroed.
*/
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx,
CodedBitstreamUnit *unit,
size_t size,
void (*free)(void *unit, uint8_t *content));
/**
* Allocate a new internal data buffer of the given size in the unit.
*
* The data buffer will have input padding.
*/
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx,
CodedBitstreamUnit *unit,
size_t size);
/**
* Insert a new unit into a fragment with the given content.
*
* The content structure continues to be owned by the caller, and
* will not be freed when the unit is.
* The content structure continues to be owned by the caller if
* content_buf is not supplied.
*/
int ff_cbs_insert_unit_content(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
int position,
CodedBitstreamUnitType type,
void *content);
void *content,
AVBufferRef *content_buf);
/**
* Insert a new unit into a fragment with the given data bitstream.
*
* The data buffer will be owned by the unit after this operation.
* If data_buf is not supplied then data must have been allocated with
* av_malloc() and will become owned by the unit after this call.
*/
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
int position,
CodedBitstreamUnitType type,
uint8_t *data, size_t data_size);
uint8_t *data, size_t data_size,
AVBufferRef *data_buf);
/**
* Delete a unit from a fragment and free all memory it uses.
......
......@@ -266,12 +266,14 @@ typedef struct H264RawSEIUserDataRegistered {
uint8_t itu_t_t35_country_code_extension_byte;
uint8_t *data;
size_t data_length;
AVBufferRef *data_ref;
} H264RawSEIUserDataRegistered;
typedef struct H264RawSEIUserDataUnregistered {
uint8_t uuid_iso_iec_11578[16];
uint8_t *data;
size_t data_length;
AVBufferRef *data_ref;
} H264RawSEIUserDataUnregistered;
typedef struct H264RawSEIRecoveryPoint {
......@@ -304,6 +306,7 @@ typedef struct H264RawSEIPayload {
struct {
uint8_t *data;
size_t data_length;
AVBufferRef *data_ref;
} other;
} payload;
} H264RawSEIPayload;
......@@ -399,6 +402,7 @@ typedef struct H264RawSlice {
uint8_t *data;
size_t data_size;
int data_bit_start;
AVBufferRef *data_ref;
} H264RawSlice;
......
This diff is collapsed.
......@@ -154,6 +154,7 @@ typedef struct H265RawVUI {
typedef struct H265RawPSExtensionData {
uint8_t *data;
size_t bit_length;
AVBufferRef *data_ref;
} H265RawPSExtensionData;
typedef struct H265RawVPS {
......@@ -512,6 +513,7 @@ typedef struct H265RawSlice {
uint8_t *data;
size_t data_size;
int data_bit_start;
AVBufferRef *data_ref;
} H265RawSlice;
......
......@@ -53,9 +53,6 @@ typedef struct CodedBitstreamType {
int (*assemble_fragment)(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag);
// Free the content and data of a single unit.
void (*free_unit)(CodedBitstreamUnit *unit);
// Free the codec internal state.
void (*close)(CodedBitstreamContext *ctx);
} CodedBitstreamType;
......
......@@ -102,6 +102,21 @@
#undef nextbits
static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
{
MPEG2RawUserData *user = (MPEG2RawUserData*)content;
av_buffer_unref(&user->user_data_ref);
av_freep(&content);
}
static void cbs_mpeg2_free_slice(void *unit, uint8_t *content)
{
MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
av_buffer_unref(&slice->header.extra_information_ref);
av_buffer_unref(&slice->data_ref);
av_freep(&content);
}
static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
CodedBitstreamFragment *frag,
int header)
......@@ -138,7 +153,7 @@ static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
memset(unit_data + unit_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
unit_data, unit_size);
unit_data, unit_size, NULL);
if (err < 0) {
av_freep(&unit_data);
return err;
......@@ -168,25 +183,25 @@ static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
MPEG2RawSlice *slice;
int pos, len;
slice = av_mallocz(sizeof(*slice));
if (!slice)
return AVERROR(ENOMEM);
err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
&cbs_mpeg2_free_slice);
if (err < 0)
return err;
slice = unit->content;
err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
if (err < 0) {
av_free(slice);
if (err < 0)
return err;
}
pos = get_bits_count(&gbc);
len = unit->data_size;
slice->data_size = len - pos / 8;
slice->data = av_malloc(slice->data_size +
slice->data_ref = av_buffer_alloc(slice->data_size +
AV_INPUT_BUFFER_PADDING_SIZE);
if (!slice->data) {
av_free(slice);
if (!slice->data_ref)
return AVERROR(ENOMEM);
}
slice->data = slice->data_ref->data;
memcpy(slice->data,
unit->data + pos / 8, slice->data_size);
......@@ -194,30 +209,29 @@ static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
AV_INPUT_BUFFER_PADDING_SIZE);
slice->data_bit_start = pos % 8;
unit->content = slice;
} else {
switch (unit->type) {
#define START(start_code, type, func) \
#define START(start_code, type, read_func, free_func) \
case start_code: \
{ \
type *header; \
header = av_mallocz(sizeof(*header)); \
if (!header) \
return AVERROR(ENOMEM); \
err = cbs_mpeg2_read_ ## func(ctx, &gbc, header); \
if (err < 0) { \
av_free(header); \
err = ff_cbs_alloc_unit_content(ctx, unit, \
sizeof(*header), free_func); \
if (err < 0) \
return err; \
header = unit->content; \
err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
if (err < 0) \
return err; \
} \
unit->content = header; \
} \
break;
START(0x00, MPEG2RawPictureHeader, picture_header);
START(0xb2, MPEG2RawUserData, user_data);
START(0xb3, MPEG2RawSequenceHeader, sequence_header);
START(0xb5, MPEG2RawExtensionData, extension_data);
START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
START(0x00, MPEG2RawPictureHeader, picture_header, NULL);
START(0xb2, MPEG2RawUserData, user_data,
&cbs_mpeg2_free_user_data);
START(0xb3, MPEG2RawSequenceHeader, sequence_header, NULL);
START(0xb5, MPEG2RawExtensionData, extension_data, NULL);
START(0xb8, MPEG2RawGroupOfPicturesHeader,
group_of_pictures_header, NULL);
#undef START
default:
av_log(ctx->log_ctx, AV_LOG_ERROR, "Unknown start code %02"PRIx32".\n",
......@@ -335,7 +349,7 @@ static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
unit->data_size = (put_bits_count(&pbc) + 7) / 8;
flush_put_bits(&pbc);
err = av_reallocp(&unit->data, unit->data_size);
err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
if (err < 0)
return err;
......@@ -355,9 +369,10 @@ static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
for (i = 0; i < frag->nb_units; i++)
size += 3 + frag->units[i].data_size;
data = av_malloc(size);
if (!data)
frag->data_ref = av_buffer_alloc(size);
if (!frag->data_ref)
return AVERROR(ENOMEM);
data = frag->data_ref->data;
dp = 0;
for (i = 0; i < frag->nb_units; i++) {
......@@ -379,19 +394,6 @@ static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
return 0;
}
static void cbs_mpeg2_free_unit(CodedBitstreamUnit *unit)
{
if (MPEG2_START_IS_SLICE(unit->type)) {
MPEG2RawSlice *slice = unit->content;
av_freep(&slice->data);
av_freep(&slice->header.extra_information);
} else if (unit->type == MPEG2_START_USER_DATA) {
MPEG2RawUserData *user = unit->content;
av_freep(&user->user_data);
}
av_freep(&unit->content);
}
static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
{
CodedBitstreamMPEG2Context *priv = ctx->priv_data;
......@@ -409,6 +411,5 @@ const CodedBitstreamType ff_cbs_type_mpeg2 = {
.write_unit = &cbs_mpeg2_write_unit,
.assemble_fragment = &cbs_mpeg2_assemble_fragment,
.free_unit = &cbs_mpeg2_free_unit,
.close = &cbs_mpeg2_close,
};
......@@ -22,6 +22,8 @@
#include <stddef.h>
#include <stdint.h>
#include "libavutil/buffer.h"
enum {
MPEG2_START_PICTURE = 0x00,
......@@ -76,6 +78,7 @@ typedef struct MPEG2RawUserData {
uint8_t *user_data;
size_t user_data_length;
AVBufferRef *user_data_ref;
} MPEG2RawUserData;
typedef struct MPEG2RawSequenceExtension {
......@@ -195,6 +198,7 @@ typedef struct MPEG2RawSliceHeader {
size_t extra_information_length;
uint8_t *extra_information;
AVBufferRef *extra_information_ref;
} MPEG2RawSliceHeader;
typedef struct MPEG2RawSlice {
......@@ -203,6 +207,7 @@ typedef struct MPEG2RawSlice {
uint8_t *data;
size_t data_size;
int data_bit_start;
AVBufferRef *data_ref;
} MPEG2RawSlice;
......
......@@ -71,9 +71,10 @@ static int FUNC(user_data)(CodedBitstreamContext *ctx, RWContext *rw,
av_assert0(k % 8 == 0);
current->user_data_length = k /= 8;
if (k > 0) {
current->user_data = av_malloc(k);
if (!current->user_data)
current->user_data_ref = av_buffer_alloc(k);
if (!current->user_data_ref)
return AVERROR(ENOMEM);
current->user_data = current->user_data_ref->data;
}
#endif
......
......@@ -271,7 +271,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
aud->primary_pic_type = j;
err = ff_cbs_insert_unit_content(ctx->cbc, au,
0, H264_NAL_AUD, aud);
0, H264_NAL_AUD, aud, NULL);
if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
goto fail;
......@@ -318,8 +318,8 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *out)
sei->nal_unit_header.nal_unit_type = H264_NAL_SEI;
err = ff_cbs_insert_unit_content(ctx->cbc, au,
sei_pos, H264_NAL_SEI, sei);
err = ff_cbs_insert_unit_content(ctx->cbc, au, sei_pos,
H264_NAL_SEI, sei, NULL);
if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to insert SEI.\n");
goto fail;
......
......@@ -289,7 +289,7 @@ static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *out)
aud->pic_type = pic_type;
err = ff_cbs_insert_unit_content(ctx->cbc, au,
0, HEVC_NAL_AUD, aud);
0, HEVC_NAL_AUD, aud, NULL);
if (err) {
av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
goto fail;
......
......@@ -167,7 +167,8 @@ static int mpeg2_metadata_update_fragment(AVBSFContext *bsf,
err = ff_cbs_insert_unit_content(ctx->cbc, frag, se_pos + 1,
MPEG2_START_EXTENSION,
&ctx->sequence_display_extension);
&ctx->sequence_display_extension,
NULL);
if (err < 0) {
av_log(bsf, AV_LOG_ERROR, "Failed to insert new sequence "
"display extension.\n");
......
......@@ -135,7 +135,7 @@ static int vaapi_encode_h264_add_nal(AVCodecContext *avctx,
int err;
err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
header->nal_unit_type, nal_unit);
header->nal_unit_type, nal_unit, NULL);
if (err < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
"type = %d.\n", header->nal_unit_type);
......
......@@ -105,7 +105,7 @@ static int vaapi_encode_h265_add_nal(AVCodecContext *avctx,
int err;
err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
header->nal_unit_type, nal_unit);
header->nal_unit_type, nal_unit, NULL);
if (err < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
"type = %d.\n", header->nal_unit_type);
......
......@@ -92,7 +92,7 @@ static int vaapi_encode_mpeg2_add_header(AVCodecContext *avctx,
VAAPIEncodeMPEG2Context *priv = ctx->priv_data;
int err;
err = ff_cbs_insert_unit_content(priv->cbc, frag, -1, type, header);
err = ff_cbs_insert_unit_content(priv->cbc, frag, -1, type, header, NULL);
if (err < 0) {
av_log(avctx, AV_LOG_ERROR, "Failed to add header: "
"type = %d.\n", type);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment