Skip to content

Commit

Permalink
ffmpeg7 support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanlf committed Apr 8, 2024
1 parent a4a0c5d commit b4f3e5b
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 39 deletions.
37 changes: 31 additions & 6 deletions src/filters/ff_avf.c
Expand Up @@ -582,8 +582,14 @@ static GF_Err ffavf_process(GF_Filter *filter)
ctx->frame->sample_aspect_ratio.num = ipid->sar.num;
ctx->frame->sample_aspect_ratio.den = ipid->sar.den;
} else {
#ifdef FFMPEG_OLD_CHLAYOUT
ctx->frame->channel_layout = ipid->ch_layout;
ctx->frame->channels = ipid->nb_ch;
#else
ctx->frame->ch_layout.order = AV_CHANNEL_ORDER_NATIVE;
ctx->frame->ch_layout.nb_channels = ipid->nb_ch;
ctx->frame->ch_layout.u.mask = ipid->ch_layout;
#endif
ctx->frame->sample_rate = ipid->sr;
ctx->frame->format = ipid->pfmt;
ctx->frame->nb_samples = data_size / ipid->nb_ch / ipid->bps;
Expand Down Expand Up @@ -728,24 +734,36 @@ static GF_Err ffavf_process(GF_Filter *filter)
GF_FilterPacket *pck;
Bool update_props=GF_TRUE;
if (frame->sample_rate!=opid->sr) {}
#ifdef FFMPEG_OLD_CHLAYOUT
else if (frame->channel_layout!=opid->ch_layout) {}
else if (frame->channels != opid->nb_ch) {}
#else
else if (frame->ch_layout.u.mask!=opid->ch_layout) {}
else if (frame->ch_layout.nb_channels != opid->nb_ch) {}
#endif
else if (frame->format != opid->pfmt) {}
else {
update_props = GF_FALSE;
}
if (update_props) {
u64 gpac_ch_layout = ffmpeg_channel_layout_to_gpac(frame->channel_layout);
#ifdef FFMPEG_OLD_CHLAYOUT
u32 nb_ch = frame->channels;
u64 ff_ch_layout = frame->channel_layout;
#else
u32 nb_ch = frame->ch_layout.nb_channels;
u64 ff_ch_layout = (frame->ch_layout.order>=AV_CHANNEL_ORDER_CUSTOM) ? 0 : frame->ch_layout.u.mask;
#endif
u64 gpac_ch_layout = ffmpeg_channel_layout_to_gpac(ff_ch_layout);
gf_filter_pid_set_property(opid->io_pid, GF_PROP_PID_SAMPLE_RATE, &PROP_UINT(frame->sample_rate));
gf_filter_pid_set_property(opid->io_pid, GF_PROP_PID_CHANNEL_LAYOUT, &PROP_LONGUINT(gpac_ch_layout));
gf_filter_pid_set_property(opid->io_pid, GF_PROP_PID_NUM_CHANNELS, &PROP_UINT(frame->channels));
gf_filter_pid_set_property(opid->io_pid, GF_PROP_PID_NUM_CHANNELS, &PROP_UINT(nb_ch));
opid->gf_pfmt = ffmpeg_audio_fmt_to_gpac(frame->format);
gf_filter_pid_set_property(opid->io_pid, GF_PROP_PID_AUDIO_FORMAT, &PROP_UINT(opid->gf_pfmt));
gf_filter_pid_set_property(opid->io_pid, GF_PROP_PID_TIMESCALE, &PROP_UINT(opid->io_filter_ctx->inputs[0]->time_base.den) );

opid->sr = frame->sample_rate;
opid->ch_layout = frame->channel_layout;
opid->nb_ch = frame->channels;
opid->ch_layout = ff_ch_layout;
opid->nb_ch = nb_ch;
opid->pfmt = frame->format;
opid->tb_num = opid->io_filter_ctx->inputs[0]->time_base.num;
opid->bps = gf_audio_fmt_bit_depth(opid->gf_pfmt) / 8;
Expand Down Expand Up @@ -890,8 +908,15 @@ static GF_Err ffavf_configure_pid(GF_Filter *filter, GF_FilterPid *pid, Bool is_
p = gf_filter_pid_get_property(pid, GF_PROP_PID_NUM_CHANNELS);
if (!p) return GF_OK; //not ready yet
nb_ch = p->value.uint;
if (!ch_layout) ch_layout = av_get_default_channel_layout(p->value.uint);

if (!ch_layout) {
#ifdef FFMPEG_OLD_CHLAYOUT
ch_layout = av_get_default_channel_layout(p->value.uint);
#else
AVChannelLayout ff_ch_layout;
av_channel_layout_default(&ff_ch_layout, p->value.uint);
ch_layout = ff_ch_layout.u.mask;
#endif
}
p = gf_filter_pid_get_property(pid, GF_PROP_PID_SAMPLE_RATE);
if (!p) return GF_OK; //not ready yet
sr = p->value.uint;
Expand Down
39 changes: 31 additions & 8 deletions src/filters/ff_common.c
Expand Up @@ -809,7 +809,9 @@ GF_FilterArgs ffmpeg_arg_translate(const struct AVOption *opt)
switch (opt->type) {
case AV_OPT_TYPE_INT64:
case AV_OPT_TYPE_INT:
#ifdef FFMPEG_OLD_CHLAYOUT
case AV_OPT_TYPE_CHANNEL_LAYOUT:
#endif
if (opt->type==AV_OPT_TYPE_INT64) arg.arg_type = GF_PROP_LSINT;
else if (opt->type==AV_OPT_TYPE_INT) arg.arg_type = GF_PROP_SINT;
else arg.arg_type = GF_PROP_UINT; //channel layout, map to int
Expand Down Expand Up @@ -2091,12 +2093,21 @@ GF_Err ffmpeg_codec_par_from_gpac(GF_FilterPid *pid, AVCodecParameters *codecpar
}
else if (streamtype==GF_STREAM_AUDIO) {
u64 ch_layout;
u32 samplerate=0;
u32 nb_ch=0, samplerate=0;

p = gf_filter_pid_get_property(pid, GF_PROP_PID_SAMPLE_RATE);
if (p) codecpar->sample_rate = samplerate = p->value.uint;
p = gf_filter_pid_get_property(pid, GF_PROP_PID_NUM_CHANNELS);
if (p) codecpar->channels = p->value.uint;
if (p) {
nb_ch = p->value.uint;
#ifdef FFMPEG_OLD_CHLAYOUT
codecpar->channels = p->value.uint;
#else
codecpar->ch_layout.order = AV_CHANNEL_ORDER_NATIVE;
codecpar->ch_layout.nb_channels = p->value.uint;
#endif
}

p = gf_filter_pid_get_property(pid, GF_PROP_PID_SAMPLES_PER_FRAME);
if (p) codecpar->frame_size = p->value.uint;
p = gf_filter_pid_get_property(pid, GF_PROP_PID_AUDIO_BPS);
Expand All @@ -2110,10 +2121,13 @@ GF_Err ffmpeg_codec_par_from_gpac(GF_FilterPid *pid, AVCodecParameters *codecpar
p = gf_filter_pid_get_property(pid, GF_PROP_PID_CHANNEL_LAYOUT);
if (p)
ch_layout = p->value.longuint;
else if (codecpar->channels==2)
else if (nb_ch==2)
ch_layout = GF_AUDIO_CH_FRONT_LEFT|GF_AUDIO_CH_FRONT_RIGHT;
#ifdef FFMPEG_OLD_CHLAYOUT
codecpar->channel_layout = ffmpeg_channel_layout_from_gpac(ch_layout);

#else
codecpar->ch_layout.u.mask = ffmpeg_channel_layout_from_gpac(ch_layout);
#endif
p = gf_filter_pid_get_property(pid, GF_PROP_PID_DELAY);
if (p && (p->value.sint<0) && samplerate) {
s64 pad = -p->value.longsint;
Expand Down Expand Up @@ -2173,10 +2187,19 @@ GF_Err ffmpeg_codec_par_to_gpac(AVCodecParameters *codecpar, GF_FilterPid *opid,
if (codecpar->sample_rate)
gf_filter_pid_set_property(opid, GF_PROP_PID_SAMPLE_RATE, &PROP_UINT(codecpar->sample_rate));

if (codecpar->channels) {
gf_filter_pid_set_property(opid, GF_PROP_PID_NUM_CHANNELS, &PROP_UINT(codecpar->channels));
if (codecpar->channel_layout) {
gf_filter_pid_set_property(opid, GF_PROP_PID_CHANNEL_LAYOUT, &PROP_LONGUINT( ffmpeg_channel_layout_to_gpac(codecpar->channel_layout) ));
u32 nb_ch=0;
u64 ch_layout=0;
#ifdef FFMPEG_OLD_CHLAYOUT
nb_ch = codecpar->channels;
ch_layout = codecpar->channel_layout;
#else
nb_ch = codecpar->ch_layout.nb_channels;
ch_layout = (codecpar->ch_layout.order>=AV_CHANNEL_ORDER_CUSTOM) ? 0 : codecpar->ch_layout.u.mask;
#endif
if (nb_ch) {
gf_filter_pid_set_property(opid, GF_PROP_PID_NUM_CHANNELS, &PROP_UINT(nb_ch));
if (ch_layout) {
gf_filter_pid_set_property(opid, GF_PROP_PID_CHANNEL_LAYOUT, &PROP_LONGUINT( ffmpeg_channel_layout_to_gpac(ch_layout) ));
}

if (codecpar->frame_size)
Expand Down
4 changes: 4 additions & 0 deletions src/filters/ff_common.h
Expand Up @@ -64,6 +64,10 @@
#include <libavcodec/bsf.h>
#endif

#if AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, LIBAFORMAT_VERSION_MINOR, 0) < AV_VERSION_INT(59,19, 0)
#define FFMPEG_OLD_CHLAYOUT
#endif


GF_FilterArgs ffmpeg_arg_translate(const struct AVOption *opt);
void ffmpeg_setup_logs(u32 log_class);
Expand Down
29 changes: 24 additions & 5 deletions src/filters/ff_dec.c
Expand Up @@ -713,8 +713,13 @@ static GF_Err ffdec_process_audio(GF_Filter *filter, struct _gf_ffdec_ctx *ctx)
FF_RELEASE_PCK(pkt);


#ifdef FFMPEG_OLD_CHLAYOUT
FF_CHECK_PROP(channels, channels, GF_PROP_PID_NUM_CHANNELS)
FF_CHECK_PROPL(channel_layout, channel_layout, GF_PROP_PID_CHANNEL_LAYOUT)
#else
FF_CHECK_PROP(channels, ch_layout.nb_channels, GF_PROP_PID_NUM_CHANNELS)
FF_CHECK_PROPL(channel_layout, ch_layout.u.mask, GF_PROP_PID_CHANNEL_LAYOUT)
#endif
FF_CHECK_PROP(sample_rate, sample_rate, GF_PROP_PID_SAMPLE_RATE)

if (prev_afmt != ctx->decoder->sample_fmt) {
Expand Down Expand Up @@ -1202,7 +1207,11 @@ static GF_Err ffdec_configure_pid(GF_Filter *filter, GF_FilterPid *pid, Bool is_
}
if (ctx->sample_rate && ctx->channels) {
ctx->decoder->sample_rate = ctx->sample_rate;
#ifdef FFMPEG_OLD_CHLAYOUT
ctx->decoder->channels = ctx->channels;
#else
ctx->decoder->ch_layout.nb_channels = ctx->channels;
#endif
}
}

Expand Down Expand Up @@ -1352,13 +1361,23 @@ static GF_Err ffdec_configure_pid(GF_Filter *filter, GF_FilterPid *pid, Bool is_
ctx->bytes_per_sample = gf_audio_fmt_bit_depth(ctx->sample_fmt) / 8;
}

u32 nb_ch=0;
u64 ff_ch_layout=0;
#ifdef FFMPEG_OLD_CHLAYOUT
nb_ch = ctx->decoder->channels;
ff_ch_layout = ctx->decoder->channel_layout;
#else
nb_ch = ctx->decoder->ch_layout.nb_channels;
ff_ch_layout = (ctx->decoder->ch_layout.order>=AV_CHANNEL_ORDER_CUSTOM) ? 0 : ctx->decoder->ch_layout.u.mask;
#endif

//override PID props with what decoder gives us
if (ctx->decoder->channels) {
ctx->channels = 0;
FF_CHECK_PROP(channels, channels, GF_PROP_PID_NUM_CHANNELS)
if (nb_ch) {
gf_filter_pid_set_property(ctx->out_pid, GF_PROP_PID_NUM_CHANNELS, &PROP_UINT(nb_ch ) );
ctx->channels = nb_ch;
}
if (ctx->decoder->channel_layout) {
u64 ch_lay = ffmpeg_channel_layout_to_gpac(ctx->decoder->channel_layout);
if (ff_ch_layout) {
u64 ch_lay = ffmpeg_channel_layout_to_gpac(ff_ch_layout);
if (ctx->channel_layout != ch_lay) {
gf_filter_pid_set_property(ctx->out_pid, GF_PROP_PID_CHANNEL_LAYOUT, &PROP_LONGUINT(ch_lay ) );
ctx->channel_layout = ch_lay;
Expand Down
16 changes: 15 additions & 1 deletion src/filters/ff_dmx.c
Expand Up @@ -599,6 +599,7 @@ static GF_Err ffdmx_process(GF_Filter *filter)
GF_BitStream *bs = gf_bs_new(sd->data, sd->size, GF_BITSTREAM_READ);

u32 flags = gf_bs_read_u32_le(bs);
#ifdef FFMPEG_OLD_CHLAYOUT
if (flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT) {
u32 new_ch = gf_bs_read_u32_le(bs);
gf_filter_pid_set_property(pctx->pid, GF_PROP_PID_NUM_CHANNELS, &PROP_UINT(new_ch) );
Expand All @@ -608,6 +609,10 @@ static GF_Err ffdmx_process(GF_Filter *filter)
new_lay = ffmpeg_channel_layout_to_gpac(new_lay);
gf_filter_pid_set_property(pctx->pid, GF_PROP_PID_CHANNEL_LAYOUT, &PROP_LONGUINT(new_lay) );
}
#else
//no message for ch layout/count change in latest API
#endif

if (flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE) {
u32 new_sr = gf_bs_read_u32_le(bs);
gf_filter_pid_set_property(pctx->pid, GF_PROP_PID_SAMPLE_RATE, &PROP_UINT(new_sr) );
Expand Down Expand Up @@ -978,7 +983,11 @@ GF_Err ffdmx_init_common(GF_Filter *filter, GF_FFDemuxCtx *ctx, u32 grab_type)
u32 exdata_size = stream->codecpar->extradata_size;
u32 codec_sample_rate = stream->codecpar->sample_rate;
u32 codec_frame_size = stream->codecpar->frame_size;
#ifdef FFMPEG_OLD_CHLAYOUT
u32 codec_channels = stream->codecpar->channels;
#else
u32 codec_channels = stream->codecpar->ch_layout.nb_channels;
#endif
u32 codec_width = stream->codecpar->width;
u32 codec_height = stream->codecpar->height;
u32 codec_field_order = stream->codecpar->field_order;
Expand Down Expand Up @@ -1068,8 +1077,13 @@ GF_Err ffdmx_init_common(GF_Filter *filter, GF_FFDemuxCtx *ctx, u32 grab_type)
if (grab_type)
gf_filter_pid_set_property(pid, GF_PROP_PID_RAWGRAB, &PROP_UINT(grab_type) );
else if (ctx->demuxer->iformat) {
if ((ctx->demuxer->iformat->flags & AVFMT_SEEK_TO_PTS) || ctx->demuxer->iformat->read_seek)
if ((ctx->demuxer->iformat->flags & AVFMT_SEEK_TO_PTS)
#if (LIBAVFORMAT_VERSION_MAJOR < 59)
|| ctx->demuxer->iformat->read_seek
#endif
) {
gf_filter_pid_set_property(pid, GF_PROP_PID_PLAYBACK_MODE, &PROP_UINT(GF_PLAYBACK_MODE_FASTFORWARD ) );
}
}


Expand Down

0 comments on commit b4f3e5b

Please sign in to comment.