Skip to content

Commit

Permalink
demux_lavf: utilize side data getter for replaygain
Browse files Browse the repository at this point in the history
This simplifies the code, not requiring a loop.

Moves the getter definition somewhat upwards to allow
for its usage in a function that was defined before it.
  • Loading branch information
jeeb authored and kasper93 committed Dec 5, 2024
1 parent f753049 commit 1f86733
Showing 1 changed file with 43 additions and 49 deletions.
92 changes: 43 additions & 49 deletions demux/demux_lavf.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,51 +600,55 @@ static void select_tracks(struct demuxer *demuxer, int start)
}
}

static inline const uint8_t *mp_av_stream_get_side_data(const AVStream *st,
enum AVPacketSideDataType type)
{
const AVPacketSideData *sd;
sd = av_packet_side_data_get(st->codecpar->coded_side_data,
st->codecpar->nb_coded_side_data,
type);
return sd ? sd->data : NULL;
}

static void export_replaygain(demuxer_t *demuxer, struct sh_stream *sh,
AVStream *st)
{
AVPacketSideData *side_data = st->codecpar->coded_side_data;
int nb_side_data = st->codecpar->nb_coded_side_data;
for (int i = 0; i < nb_side_data; i++) {
AVReplayGain *av_rgain;
struct replaygain_data *rgain;
AVPacketSideData *src_sd = &side_data[i];

if (src_sd->type != AV_PKT_DATA_REPLAYGAIN)
continue;
const AVReplayGain *av_rgain =
(const AVReplayGain *)mp_av_stream_get_side_data(st, AV_PKT_DATA_REPLAYGAIN);
if (!av_rgain)
return;

av_rgain = (AVReplayGain*)src_sd->data;
rgain = talloc_ptrtype(demuxer, rgain);
rgain->track_gain = rgain->album_gain = 0;
rgain->track_peak = rgain->album_peak = 1;

// Set values in *rgain, using track gain as a fallback for album gain
// if the latter is not present. This behavior matches that in
// demux/demux.c's decode_rgain; if you change this, please make
// equivalent changes there too.
if (av_rgain->track_gain != INT32_MIN && av_rgain->track_peak != 0.0) {
// Track gain is defined.
rgain->track_gain = av_rgain->track_gain / 100000.0f;
rgain->track_peak = av_rgain->track_peak / 100000.0f;

if (av_rgain->album_gain != INT32_MIN &&
av_rgain->album_peak != 0.0)
{
// Album gain is also defined.
rgain->album_gain = av_rgain->album_gain / 100000.0f;
rgain->album_peak = av_rgain->album_peak / 100000.0f;
} else {
// Album gain is undefined; fall back to track gain.
rgain->album_gain = rgain->track_gain;
rgain->album_peak = rgain->track_peak;
}
}
struct replaygain_data *rgain = talloc_ptrtype(demuxer, rgain);

rgain->track_gain = rgain->album_gain = 0;
rgain->track_peak = rgain->album_peak = 1;

// This must be run only before the stream was added, otherwise there
// will be race conditions with accesses from the user thread.
assert(!sh->ds);
sh->codec->replaygain_data = rgain;
// Set values in *rgain, using track gain as a fallback for album gain
// if the latter is not present. This behavior matches that in
// demux/demux.c's decode_rgain; if you change this, please make
// equivalent changes there too.
if (av_rgain->track_gain != INT32_MIN && av_rgain->track_peak != 0.0) {
// Track gain is defined.
rgain->track_gain = av_rgain->track_gain / 100000.0f;
rgain->track_peak = av_rgain->track_peak / 100000.0f;

if (av_rgain->album_gain != INT32_MIN &&
av_rgain->album_peak != 0.0)
{
// Album gain is also defined.
rgain->album_gain = av_rgain->album_gain / 100000.0f;
rgain->album_peak = av_rgain->album_peak / 100000.0f;
} else {
// Album gain is undefined; fall back to track gain.
rgain->album_gain = rgain->track_gain;
rgain->album_peak = rgain->track_peak;
}
}

// This must be run only before the stream was added, otherwise there
// will be race conditions with accesses from the user thread.
assert(!sh->ds);
sh->codec->replaygain_data = rgain;
}

// Return a dictionary entry as (decimal) integer.
Expand Down Expand Up @@ -673,16 +677,6 @@ static bool is_image(AVStream *st, bool attached_picture, const AVInputFormat *a
);
}

static inline const uint8_t *mp_av_stream_get_side_data(const AVStream *st,
enum AVPacketSideDataType type)
{
const AVPacketSideData *sd;
sd = av_packet_side_data_get(st->codecpar->coded_side_data,
st->codecpar->nb_coded_side_data,
type);
return sd ? sd->data : NULL;
}

static void handle_new_stream(demuxer_t *demuxer, int i)
{
lavf_priv_t *priv = demuxer->priv;
Expand Down

0 comments on commit 1f86733

Please sign in to comment.