Skip to content

Commit d36883f

Browse files
felicialimjamrial
authored andcommitted
avformat/iamf_writer: fix setting skip_samples and discard_padding for OPUS
1 parent 3b2a941 commit d36883f

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

libavformat/iamf_writer.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,8 @@ int ff_iamf_write_audio_frame(const IAMFContext *iamf, AVIOContext *pb,
11151115
{
11161116
uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
11171117
PutBitContext pbc;
1118+
const IAMFAudioElement *audio_element;
1119+
IAMFCodecConfig *codec_config;
11181120
AVIOContext *dyn_bc;
11191121
const uint8_t *side_data;
11201122
uint8_t *dyn_buf = NULL;
@@ -1124,22 +1126,21 @@ int ff_iamf_write_audio_frame(const IAMFContext *iamf, AVIOContext *pb,
11241126
audio_substream_id + IAMF_OBU_IA_AUDIO_FRAME_ID0 : IAMF_OBU_IA_AUDIO_FRAME;
11251127
int ret;
11261128

1129+
audio_element = get_audio_element(iamf, audio_substream_id);
1130+
if (!audio_element)
1131+
return AVERROR(EINVAL);
1132+
codec_config = ff_iamf_get_codec_config(iamf, audio_element->codec_config_id);
1133+
if (!codec_config)
1134+
return AVERROR(EINVAL);
1135+
11271136
if (!pkt->size) {
1128-
const IAMFAudioElement *audio_element;
1129-
IAMFCodecConfig *codec_config;
11301137
size_t new_extradata_size;
11311138
const uint8_t *new_extradata = av_packet_get_side_data(pkt,
11321139
AV_PKT_DATA_NEW_EXTRADATA,
11331140
&new_extradata_size);
11341141

11351142
if (!new_extradata)
11361143
return AVERROR_INVALIDDATA;
1137-
audio_element = get_audio_element(iamf, audio_substream_id);
1138-
if (!audio_element)
1139-
return AVERROR(EINVAL);
1140-
codec_config = ff_iamf_get_codec_config(iamf, audio_element->codec_config_id);
1141-
if (!codec_config)
1142-
return AVERROR(EINVAL);
11431144

11441145
av_free(codec_config->extradata);
11451146
codec_config->extradata = av_memdup(new_extradata, new_extradata_size);
@@ -1160,6 +1161,14 @@ int ff_iamf_write_audio_frame(const IAMFContext *iamf, AVIOContext *pb,
11601161
discard_padding = AV_RL32(side_data + 4);
11611162
}
11621163

1164+
if (codec_config->codec_id == AV_CODEC_ID_OPUS) {
1165+
// IAMF's num_samples_to_trim_at_start is the same as Opus's pre-skip.
1166+
skip_samples = pkt->dts < 0
1167+
? av_rescale(-pkt->dts, 48000, pkt->time_base.den)
1168+
: 0;
1169+
discard_padding = av_rescale(discard_padding, 48000, pkt->time_base.den);
1170+
}
1171+
11631172
ret = avio_open_dyn_buf(&dyn_bc);
11641173
if (ret < 0)
11651174
return ret;

0 commit comments

Comments
 (0)