Skip to content

Commit

Permalink
avformat/avformat: use the side data from AVStream.codecpar
Browse files Browse the repository at this point in the history
Deprecate AVStream.side_data and its helpers in favor of the AVStream's
codecpar.coded_side_data.

This will considerably simplify the propagation of global side data to decoders
and from encoders. Instead of having to do it inside packets, it will be
available during init().
Global and frame specific side data will therefore be distinct.

Signed-off-by: James Almer <jamrial@gmail.com>
  • Loading branch information
jamrial committed Oct 6, 2023
1 parent 21d7cc6 commit 5432d2a
Show file tree
Hide file tree
Showing 27 changed files with 340 additions and 254 deletions.
5 changes: 5 additions & 0 deletions doc/APIchanges
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ The last version increases of all libraries were on 2023-02-09

API changes, most recent first:

2023-10-06 - xxxxxxxxxx - lavc 60.15.100 - avformat.h
Deprecate AVFormatContext.{nb_,}side_data, av_stream_add_side_data(),
av_stream_new_side_data(), and av_stream_get_side_data(). Side data fields
from AVFormatContext.codecpar should be used from now on.

2023-10-06 - xxxxxxxxxx - lavc 60.30.100 - codec_par.h
Added {nb_,}coded_side_data to AVCodecParameters.
The AVCodecParameters helpers will copy it to and from its AVCodecContext
Expand Down
10 changes: 6 additions & 4 deletions libavdevice/android_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ static int wait_for_image_format(AVFormatContext *avctx)
static int add_display_matrix(AVFormatContext *avctx, AVStream *st)
{
AndroidCameraCtx *ctx = avctx->priv_data;
uint8_t *side_data;
AVPacketSideData *side_data;
int32_t display_matrix[9];

av_display_rotation_set(display_matrix, ctx->sensor_orientation);
Expand All @@ -648,14 +648,16 @@ static int add_display_matrix(AVFormatContext *avctx, AVStream *st)
av_display_matrix_flip(display_matrix, 1, 0);
}

side_data = av_stream_new_side_data(st,
AV_PKT_DATA_DISPLAYMATRIX, sizeof(display_matrix));
side_data = av_packet_side_data_new(&st->codecpar->side_data,
&st->codecpar->nb_side_data,
AV_PKT_DATA_DISPLAYMATRIX,
sizeof(display_matrix), 0);

if (!side_data) {
return AVERROR(ENOMEM);
}

memcpy(side_data, display_matrix, sizeof(display_matrix));
memcpy(side_data->data, display_matrix, sizeof(display_matrix));

return 0;
}
Expand Down
42 changes: 8 additions & 34 deletions libavformat/avformat.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ void ff_free_stream(AVStream **pst)
if (!st)
return;

#if FF_API_AVSTREAM_SIDE_DATA
FF_DISABLE_DEPRECATION_WARNINGS
for (int i = 0; i < st->nb_side_data; i++)
av_freep(&st->side_data[i].data);
av_freep(&st->side_data);
FF_ENABLE_DEPRECATION_WARNINGS
#endif

if (st->attached_pic.data)
av_packet_unref(&st->attached_pic);
Expand Down Expand Up @@ -140,6 +144,8 @@ void avformat_free_context(AVFormatContext *s)
av_free(s);
}

#if FF_API_AVSTREAM_SIDE_DATA
FF_DISABLE_DEPRECATION_WARNINGS
uint8_t *av_stream_get_side_data(const AVStream *st,
enum AVPacketSideDataType type, size_t *size)
{
Expand Down Expand Up @@ -207,36 +213,8 @@ uint8_t *av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type,

return data;
}

int ff_stream_side_data_copy(AVStream *dst, const AVStream *src)
{
/* Free existing side data*/
for (int i = 0; i < dst->nb_side_data; i++)
av_free(dst->side_data[i].data);
av_freep(&dst->side_data);
dst->nb_side_data = 0;

/* Copy side data if present */
if (src->nb_side_data) {
dst->side_data = av_calloc(src->nb_side_data,
sizeof(*dst->side_data));
if (!dst->side_data)
return AVERROR(ENOMEM);
dst->nb_side_data = src->nb_side_data;

for (int i = 0; i < src->nb_side_data; i++) {
uint8_t *data = av_memdup(src->side_data[i].data,
src->side_data[i].size);
if (!data)
return AVERROR(ENOMEM);
dst->side_data[i].type = src->side_data[i].type;
dst->side_data[i].size = src->side_data[i].size;
dst->side_data[i].data = data;
}
}

return 0;
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif

/**
* Copy all stream parameters from source to destination stream, with the
Expand Down Expand Up @@ -272,10 +250,6 @@ static int stream_params_copy(AVStream *dst, const AVStream *src)
if (ret < 0)
return ret;

ret = ff_stream_side_data_copy(dst, src);
if (ret < 0)
return ret;

av_packet_unref(&dst->attached_pic);
if (src->attached_pic.data) {
ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);
Expand Down
27 changes: 26 additions & 1 deletion libavformat/avformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ typedef struct AVStream {
*/
AVPacket attached_pic;

#if FF_API_AVSTREAM_SIDE_DATA
/**
* An array of side data that applies to the whole stream (i.e. the
* container does not allow it to change between packets).
Expand All @@ -956,13 +957,20 @@ typedef struct AVStream {
*
* Freed by libavformat in avformat_free_context().
*
* @see av_format_inject_global_side_data()
* @deprecated use AVStream's @ref AVCodecParameters.coded_side_data
* "codecpar side data".
*/
attribute_deprecated
AVPacketSideData *side_data;
/**
* The number of elements in the AVStream.side_data array.
*
* @deprecated use AVStream's @ref AVCodecParameters.nb_coded_side_data
* "codecpar side data".
*/
attribute_deprecated
int nb_side_data;
#endif

/**
* Flags indicating events happening on the stream, a combination of
Expand Down Expand Up @@ -1723,6 +1731,12 @@ typedef struct AVFormatContext {
/**
* This function will cause global side data to be injected in the next packet
* of each stream as well as after any subsequent seek.
*
* @note global side data is always available in every AVStream's
* @ref AVCodecParameters.coded_side_data "codecpar side data" array, and
* in a @ref AVCodecContext.coded_side_data "decoder's side data" array if
* initialized with said stream's codecpar.
* @see av_packet_side_data_get()
*/
void av_format_inject_global_side_data(AVFormatContext *s);

Expand Down Expand Up @@ -1849,6 +1863,7 @@ const AVClass *av_stream_get_class(void);
*/
AVStream *avformat_new_stream(AVFormatContext *s, const struct AVCodec *c);

#if FF_API_AVSTREAM_SIDE_DATA
/**
* Wrap an existing array as stream side data.
*
Expand All @@ -1861,7 +1876,10 @@ AVStream *avformat_new_stream(AVFormatContext *s, const struct AVCodec *c);
*
* @return zero on success, a negative AVERROR code on failure. On failure,
* the stream is unchanged and the data remains owned by the caller.
* @deprecated use av_packet_side_data_add() with the stream's
* @ref AVCodecParameters.coded_side_data "codecpar side data"
*/
attribute_deprecated
int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type,
uint8_t *data, size_t size);

Expand All @@ -1873,7 +1891,10 @@ int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type,
* @param size side information size
*
* @return pointer to fresh allocated data or NULL otherwise
* @deprecated use av_packet_side_data_new() with the stream's
* @ref AVCodecParameters.coded_side_data "codecpar side data"
*/
attribute_deprecated
uint8_t *av_stream_new_side_data(AVStream *stream,
enum AVPacketSideDataType type, size_t size);
/**
Expand All @@ -1885,9 +1906,13 @@ uint8_t *av_stream_new_side_data(AVStream *stream,
* or to zero if the desired side data is not present.
*
* @return pointer to data if present or NULL otherwise
* @deprecated use av_packet_side_data_get() with the stream's
* @ref AVCodecParameters.coded_side_data "codecpar side data"
*/
attribute_deprecated
uint8_t *av_stream_get_side_data(const AVStream *stream,
enum AVPacketSideDataType type, size_t *size);
#endif

AVProgram *av_new_program(AVFormatContext *s, int id);

Expand Down
1 change: 0 additions & 1 deletion libavformat/concatdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ static int copy_stream_props(AVStream *st, AVStream *source_st)
avpriv_set_pts_info(st, 64, source_st->time_base.num, source_st->time_base.den);

av_dict_copy(&st->metadata, source_st->metadata, 0);
ff_stream_side_data_copy(st, source_st);
return 0;
}

Expand Down
11 changes: 0 additions & 11 deletions libavformat/dashdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1954,17 +1954,6 @@ static int open_demux_for_component(AVFormatContext *s, struct representation *p

// copy disposition
st->disposition = ist->disposition;

// copy side data
for (int i = 0; i < ist->nb_side_data; i++) {
const AVPacketSideData *sd_src = &ist->side_data[i];
uint8_t *dst_data;

dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
if (!dst_data)
return AVERROR(ENOMEM);
memcpy(dst_data, sd_src->data, sd_src->size);
}
}

return 0;
Expand Down
53 changes: 33 additions & 20 deletions libavformat/demux.c
Original file line number Diff line number Diff line change
Expand Up @@ -1409,9 +1409,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
sti->skip_samples = 0;
}

#if FF_API_AVSTREAM_SIDE_DATA
if (sti->inject_global_side_data) {
for (int i = 0; i < st->nb_side_data; i++) {
const AVPacketSideData *const src_sd = &st->side_data[i];
for (int i = 0; i < st->codecpar->nb_coded_side_data; i++) {
const AVPacketSideData *const src_sd = &st->codecpar->coded_side_data[i];
uint8_t *dst_data;

if (av_packet_get_side_data(pkt, src_sd->type, NULL))
Expand All @@ -1427,6 +1428,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
}
sti->inject_global_side_data = 0;
}
#endif
}

if (!si->metafree) {
Expand Down Expand Up @@ -2431,19 +2433,6 @@ static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *
return 0;
}

static int add_coded_side_data(AVStream *st, AVCodecContext *avctx)
{
for (int i = 0; i < avctx->nb_coded_side_data; i++) {
const AVPacketSideData *const sd_src = &avctx->coded_side_data[i];
uint8_t *dst_data;
dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
if (!dst_data)
return AVERROR(ENOMEM);
memcpy(dst_data, sd_src->data, sd_src->size);
}
return 0;
}

int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
{
FFFormatContext *const si = ffformatcontext(ic);
Expand Down Expand Up @@ -2969,9 +2958,6 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)

if (sti->avctx_inited) {
ret = avcodec_parameters_from_context(st->codecpar, sti->avctx);
if (ret < 0)
goto find_stream_info_err;
ret = add_coded_side_data(st, sti->avctx);
if (ret < 0)
goto find_stream_info_err;

Expand All @@ -2986,14 +2972,41 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
props->min_bitrate = sti->avctx->rc_min_rate;
if (sti->avctx->rc_max_rate > 0)
props->max_bitrate = sti->avctx->rc_max_rate;
if (av_stream_add_side_data(st, AV_PKT_DATA_CPB_PROPERTIES,
(uint8_t *)props, cpb_size))
if (!av_packet_side_data_add(&st->codecpar->coded_side_data,
&st->codecpar->nb_coded_side_data,
AV_PKT_DATA_CPB_PROPERTIES,
(uint8_t *)props, cpb_size, 0))
av_free(props);
}
}
}

sti->avctx_inited = 0;
#if FF_API_AVSTREAM_SIDE_DATA
FF_DISABLE_DEPRECATION_WARNINGS
if (st->codecpar->nb_coded_side_data > 0) {
av_assert0(!st->side_data && !st->nb_side_data);
st->side_data = av_calloc(st->codecpar->nb_coded_side_data, sizeof(*st->side_data));
if (!st->side_data) {
ret = AVERROR(ENOMEM);
goto find_stream_info_err;
}

for (int j = 0; j < st->codecpar->nb_coded_side_data; j++) {
uint8_t *data = av_memdup(st->codecpar->coded_side_data[j].data,
st->codecpar->coded_side_data[j].size);
if (!data) {
ret = AVERROR(ENOMEM);
goto find_stream_info_err;
}
st->side_data[j].type = st->codecpar->coded_side_data[j].type;
st->side_data[j].size = st->codecpar->coded_side_data[j].size;
st->side_data[j].data = data;
st->nb_side_data++;
}
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif
}

find_stream_info_err:
Expand Down
8 changes: 3 additions & 5 deletions libavformat/dovi_isom.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ int ff_isom_parse_dvcc_dvvc(void *logctx, AVStream *st,
uint32_t buf;
AVDOVIDecoderConfigurationRecord *dovi;
size_t dovi_size;
int ret;

if (size > (1 << 30) || size < 4)
return AVERROR_INVALIDDATA;
Expand Down Expand Up @@ -64,11 +63,10 @@ int ff_isom_parse_dvcc_dvvc(void *logctx, AVStream *st,
dovi->dv_bl_signal_compatibility_id = 0;
}

ret = av_stream_add_side_data(st, AV_PKT_DATA_DOVI_CONF,
(uint8_t *)dovi, dovi_size);
if (ret < 0) {
if (!av_packet_side_data_add(&st->codecpar->coded_side_data, &st->codecpar->nb_coded_side_data,
AV_PKT_DATA_DOVI_CONF, (uint8_t *)dovi, dovi_size, 0)) {
av_free(dovi);
return ret;
return AVERROR(ENOMEM);
}

av_log(logctx, AV_LOG_TRACE, "DOVI in dvcC/dvvC/dvwC box, version: %d.%d, profile: %d, level: %d, "
Expand Down
6 changes: 3 additions & 3 deletions libavformat/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,11 @@ static void dump_sidedata(void *ctx, const AVStream *st, const char *indent)
{
int i;

if (st->nb_side_data)
if (st->codecpar->nb_coded_side_data)
av_log(ctx, AV_LOG_INFO, "%sSide data:\n", indent);

for (i = 0; i < st->nb_side_data; i++) {
const AVPacketSideData *sd = &st->side_data[i];
for (i = 0; i < st->codecpar->nb_coded_side_data; i++) {
const AVPacketSideData *sd = &st->codecpar->coded_side_data[i];
av_log(ctx, AV_LOG_INFO, "%s ", indent);

switch (sd->type) {
Expand Down
11 changes: 0 additions & 11 deletions libavformat/hls.c
Original file line number Diff line number Diff line change
Expand Up @@ -1852,17 +1852,6 @@ static int set_stream_info_from_input_stream(AVStream *st, struct playlist *pls,

av_dict_copy(&st->metadata, ist->metadata, 0);

// copy side data
for (int i = 0; i < ist->nb_side_data; i++) {
const AVPacketSideData *sd_src = &ist->side_data[i];
uint8_t *dst_data;

dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
if (!dst_data)
return AVERROR(ENOMEM);
memcpy(dst_data, sd_src->data, sd_src->size);
}

ffstream(st)->need_context_update = 1;

return 0;
Expand Down
Loading

0 comments on commit 5432d2a

Please sign in to comment.