Skip to content

Commit 5432d2a

Browse files
committed
avformat/avformat: use the side data from AVStream.codecpar
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 <[email protected]>
1 parent 21d7cc6 commit 5432d2a

27 files changed

+340
-254
lines changed

doc/APIchanges

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ The last version increases of all libraries were on 2023-02-09
22

33
API changes, most recent first:
44

5+
2023-10-06 - xxxxxxxxxx - lavc 60.15.100 - avformat.h
6+
Deprecate AVFormatContext.{nb_,}side_data, av_stream_add_side_data(),
7+
av_stream_new_side_data(), and av_stream_get_side_data(). Side data fields
8+
from AVFormatContext.codecpar should be used from now on.
9+
510
2023-10-06 - xxxxxxxxxx - lavc 60.30.100 - codec_par.h
611
Added {nb_,}coded_side_data to AVCodecParameters.
712
The AVCodecParameters helpers will copy it to and from its AVCodecContext

libavdevice/android_camera.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ static int wait_for_image_format(AVFormatContext *avctx)
639639
static int add_display_matrix(AVFormatContext *avctx, AVStream *st)
640640
{
641641
AndroidCameraCtx *ctx = avctx->priv_data;
642-
uint8_t *side_data;
642+
AVPacketSideData *side_data;
643643
int32_t display_matrix[9];
644644

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

651-
side_data = av_stream_new_side_data(st,
652-
AV_PKT_DATA_DISPLAYMATRIX, sizeof(display_matrix));
651+
side_data = av_packet_side_data_new(&st->codecpar->side_data,
652+
&st->codecpar->nb_side_data,
653+
AV_PKT_DATA_DISPLAYMATRIX,
654+
sizeof(display_matrix), 0);
653655

654656
if (!side_data) {
655657
return AVERROR(ENOMEM);
656658
}
657659

658-
memcpy(side_data, display_matrix, sizeof(display_matrix));
660+
memcpy(side_data->data, display_matrix, sizeof(display_matrix));
659661

660662
return 0;
661663
}

libavformat/avformat.c

+8-34
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ void ff_free_stream(AVStream **pst)
4848
if (!st)
4949
return;
5050

51+
#if FF_API_AVSTREAM_SIDE_DATA
52+
FF_DISABLE_DEPRECATION_WARNINGS
5153
for (int i = 0; i < st->nb_side_data; i++)
5254
av_freep(&st->side_data[i].data);
5355
av_freep(&st->side_data);
56+
FF_ENABLE_DEPRECATION_WARNINGS
57+
#endif
5458

5559
if (st->attached_pic.data)
5660
av_packet_unref(&st->attached_pic);
@@ -140,6 +144,8 @@ void avformat_free_context(AVFormatContext *s)
140144
av_free(s);
141145
}
142146

147+
#if FF_API_AVSTREAM_SIDE_DATA
148+
FF_DISABLE_DEPRECATION_WARNINGS
143149
uint8_t *av_stream_get_side_data(const AVStream *st,
144150
enum AVPacketSideDataType type, size_t *size)
145151
{
@@ -207,36 +213,8 @@ uint8_t *av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type,
207213

208214
return data;
209215
}
210-
211-
int ff_stream_side_data_copy(AVStream *dst, const AVStream *src)
212-
{
213-
/* Free existing side data*/
214-
for (int i = 0; i < dst->nb_side_data; i++)
215-
av_free(dst->side_data[i].data);
216-
av_freep(&dst->side_data);
217-
dst->nb_side_data = 0;
218-
219-
/* Copy side data if present */
220-
if (src->nb_side_data) {
221-
dst->side_data = av_calloc(src->nb_side_data,
222-
sizeof(*dst->side_data));
223-
if (!dst->side_data)
224-
return AVERROR(ENOMEM);
225-
dst->nb_side_data = src->nb_side_data;
226-
227-
for (int i = 0; i < src->nb_side_data; i++) {
228-
uint8_t *data = av_memdup(src->side_data[i].data,
229-
src->side_data[i].size);
230-
if (!data)
231-
return AVERROR(ENOMEM);
232-
dst->side_data[i].type = src->side_data[i].type;
233-
dst->side_data[i].size = src->side_data[i].size;
234-
dst->side_data[i].data = data;
235-
}
236-
}
237-
238-
return 0;
239-
}
216+
FF_ENABLE_DEPRECATION_WARNINGS
217+
#endif
240218

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

275-
ret = ff_stream_side_data_copy(dst, src);
276-
if (ret < 0)
277-
return ret;
278-
279253
av_packet_unref(&dst->attached_pic);
280254
if (src->attached_pic.data) {
281255
ret = av_packet_ref(&dst->attached_pic, &src->attached_pic);

libavformat/avformat.h

+26-1
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ typedef struct AVStream {
940940
*/
941941
AVPacket attached_pic;
942942

943+
#if FF_API_AVSTREAM_SIDE_DATA
943944
/**
944945
* An array of side data that applies to the whole stream (i.e. the
945946
* container does not allow it to change between packets).
@@ -956,13 +957,20 @@ typedef struct AVStream {
956957
*
957958
* Freed by libavformat in avformat_free_context().
958959
*
959-
* @see av_format_inject_global_side_data()
960+
* @deprecated use AVStream's @ref AVCodecParameters.coded_side_data
961+
* "codecpar side data".
960962
*/
963+
attribute_deprecated
961964
AVPacketSideData *side_data;
962965
/**
963966
* The number of elements in the AVStream.side_data array.
967+
*
968+
* @deprecated use AVStream's @ref AVCodecParameters.nb_coded_side_data
969+
* "codecpar side data".
964970
*/
971+
attribute_deprecated
965972
int nb_side_data;
973+
#endif
966974

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

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

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

@@ -1873,7 +1891,10 @@ int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type,
18731891
* @param size side information size
18741892
*
18751893
* @return pointer to fresh allocated data or NULL otherwise
1894+
* @deprecated use av_packet_side_data_new() with the stream's
1895+
* @ref AVCodecParameters.coded_side_data "codecpar side data"
18761896
*/
1897+
attribute_deprecated
18771898
uint8_t *av_stream_new_side_data(AVStream *stream,
18781899
enum AVPacketSideDataType type, size_t size);
18791900
/**
@@ -1885,9 +1906,13 @@ uint8_t *av_stream_new_side_data(AVStream *stream,
18851906
* or to zero if the desired side data is not present.
18861907
*
18871908
* @return pointer to data if present or NULL otherwise
1909+
* @deprecated use av_packet_side_data_get() with the stream's
1910+
* @ref AVCodecParameters.coded_side_data "codecpar side data"
18881911
*/
1912+
attribute_deprecated
18891913
uint8_t *av_stream_get_side_data(const AVStream *stream,
18901914
enum AVPacketSideDataType type, size_t *size);
1915+
#endif
18911916

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

libavformat/concatdec.c

-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ static int copy_stream_props(AVStream *st, AVStream *source_st)
194194
avpriv_set_pts_info(st, 64, source_st->time_base.num, source_st->time_base.den);
195195

196196
av_dict_copy(&st->metadata, source_st->metadata, 0);
197-
ff_stream_side_data_copy(st, source_st);
198197
return 0;
199198
}
200199

libavformat/dashdec.c

-11
Original file line numberDiff line numberDiff line change
@@ -1954,17 +1954,6 @@ static int open_demux_for_component(AVFormatContext *s, struct representation *p
19541954

19551955
// copy disposition
19561956
st->disposition = ist->disposition;
1957-
1958-
// copy side data
1959-
for (int i = 0; i < ist->nb_side_data; i++) {
1960-
const AVPacketSideData *sd_src = &ist->side_data[i];
1961-
uint8_t *dst_data;
1962-
1963-
dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
1964-
if (!dst_data)
1965-
return AVERROR(ENOMEM);
1966-
memcpy(dst_data, sd_src->data, sd_src->size);
1967-
}
19681957
}
19691958

19701959
return 0;

libavformat/demux.c

+33-20
Original file line numberDiff line numberDiff line change
@@ -1409,9 +1409,10 @@ FF_ENABLE_DEPRECATION_WARNINGS
14091409
sti->skip_samples = 0;
14101410
}
14111411

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

14171418
if (av_packet_get_side_data(pkt, src_sd->type, NULL))
@@ -1427,6 +1428,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
14271428
}
14281429
sti->inject_global_side_data = 0;
14291430
}
1431+
#endif
14301432
}
14311433

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

2434-
static int add_coded_side_data(AVStream *st, AVCodecContext *avctx)
2435-
{
2436-
for (int i = 0; i < avctx->nb_coded_side_data; i++) {
2437-
const AVPacketSideData *const sd_src = &avctx->coded_side_data[i];
2438-
uint8_t *dst_data;
2439-
dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
2440-
if (!dst_data)
2441-
return AVERROR(ENOMEM);
2442-
memcpy(dst_data, sd_src->data, sd_src->size);
2443-
}
2444-
return 0;
2445-
}
2446-
24472436
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
24482437
{
24492438
FFFormatContext *const si = ffformatcontext(ic);
@@ -2969,9 +2958,6 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
29692958

29702959
if (sti->avctx_inited) {
29712960
ret = avcodec_parameters_from_context(st->codecpar, sti->avctx);
2972-
if (ret < 0)
2973-
goto find_stream_info_err;
2974-
ret = add_coded_side_data(st, sti->avctx);
29752961
if (ret < 0)
29762962
goto find_stream_info_err;
29772963

@@ -2986,14 +2972,41 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
29862972
props->min_bitrate = sti->avctx->rc_min_rate;
29872973
if (sti->avctx->rc_max_rate > 0)
29882974
props->max_bitrate = sti->avctx->rc_max_rate;
2989-
if (av_stream_add_side_data(st, AV_PKT_DATA_CPB_PROPERTIES,
2990-
(uint8_t *)props, cpb_size))
2975+
if (!av_packet_side_data_add(&st->codecpar->coded_side_data,
2976+
&st->codecpar->nb_coded_side_data,
2977+
AV_PKT_DATA_CPB_PROPERTIES,
2978+
(uint8_t *)props, cpb_size, 0))
29912979
av_free(props);
29922980
}
29932981
}
29942982
}
29952983

29962984
sti->avctx_inited = 0;
2985+
#if FF_API_AVSTREAM_SIDE_DATA
2986+
FF_DISABLE_DEPRECATION_WARNINGS
2987+
if (st->codecpar->nb_coded_side_data > 0) {
2988+
av_assert0(!st->side_data && !st->nb_side_data);
2989+
st->side_data = av_calloc(st->codecpar->nb_coded_side_data, sizeof(*st->side_data));
2990+
if (!st->side_data) {
2991+
ret = AVERROR(ENOMEM);
2992+
goto find_stream_info_err;
2993+
}
2994+
2995+
for (int j = 0; j < st->codecpar->nb_coded_side_data; j++) {
2996+
uint8_t *data = av_memdup(st->codecpar->coded_side_data[j].data,
2997+
st->codecpar->coded_side_data[j].size);
2998+
if (!data) {
2999+
ret = AVERROR(ENOMEM);
3000+
goto find_stream_info_err;
3001+
}
3002+
st->side_data[j].type = st->codecpar->coded_side_data[j].type;
3003+
st->side_data[j].size = st->codecpar->coded_side_data[j].size;
3004+
st->side_data[j].data = data;
3005+
st->nb_side_data++;
3006+
}
3007+
}
3008+
FF_ENABLE_DEPRECATION_WARNINGS
3009+
#endif
29973010
}
29983011

29993012
find_stream_info_err:

libavformat/dovi_isom.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ int ff_isom_parse_dvcc_dvvc(void *logctx, AVStream *st,
3434
uint32_t buf;
3535
AVDOVIDecoderConfigurationRecord *dovi;
3636
size_t dovi_size;
37-
int ret;
3837

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

67-
ret = av_stream_add_side_data(st, AV_PKT_DATA_DOVI_CONF,
68-
(uint8_t *)dovi, dovi_size);
69-
if (ret < 0) {
66+
if (!av_packet_side_data_add(&st->codecpar->coded_side_data, &st->codecpar->nb_coded_side_data,
67+
AV_PKT_DATA_DOVI_CONF, (uint8_t *)dovi, dovi_size, 0)) {
7068
av_free(dovi);
71-
return ret;
69+
return AVERROR(ENOMEM);
7270
}
7371

7472
av_log(logctx, AV_LOG_TRACE, "DOVI in dvcC/dvvC/dvwC box, version: %d.%d, profile: %d, level: %d, "

libavformat/dump.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,11 @@ static void dump_sidedata(void *ctx, const AVStream *st, const char *indent)
431431
{
432432
int i;
433433

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

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

441441
switch (sd->type) {

libavformat/hls.c

-11
Original file line numberDiff line numberDiff line change
@@ -1852,17 +1852,6 @@ static int set_stream_info_from_input_stream(AVStream *st, struct playlist *pls,
18521852

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

1855-
// copy side data
1856-
for (int i = 0; i < ist->nb_side_data; i++) {
1857-
const AVPacketSideData *sd_src = &ist->side_data[i];
1858-
uint8_t *dst_data;
1859-
1860-
dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
1861-
if (!dst_data)
1862-
return AVERROR(ENOMEM);
1863-
memcpy(dst_data, sd_src->data, sd_src->size);
1864-
}
1865-
18661855
ffstream(st)->need_context_update = 1;
18671856

18681857
return 0;

0 commit comments

Comments
 (0)