1
0
Fork 0

avformat/iamfenc: further split into shareable modules

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2024-02-17 10:26:51 -03:00
parent c7266ad60f
commit c95c8a0158
5 changed files with 238 additions and 210 deletions

2
configure vendored
View File

@ -2517,6 +2517,7 @@ CONFIG_EXTRA="
huffyuvdsp
huffyuvencdsp
iamfdec
iamfenc
idctdsp
iirfilter
inflate_wrapper
@ -3536,6 +3537,7 @@ hds_muxer_select="flv_muxer"
hls_demuxer_select="adts_header ac3_parser mov_demuxer mpegts_demuxer"
hls_muxer_select="mov_muxer mpegts_muxer"
iamf_demuxer_select="iamfdec"
iamf_muxer_select="iamfenc"
image2_alias_pix_demuxer_select="image2_demuxer"
image2_brender_pix_demuxer_select="image2_demuxer"
imf_demuxer_deps="libxml2"

View File

@ -36,6 +36,7 @@ OBJS-$(HAVE_LIBC_MSVCRT) += file_open.o
# subsystems
OBJS-$(CONFIG_ISO_MEDIA) += isom.o
OBJS-$(CONFIG_IAMFDEC) += iamf_reader.o iamf_parse.o iamf.o
OBJS-$(CONFIG_IAMFENC) += iamf_writer.o iamf.o
OBJS-$(CONFIG_NETWORK) += network.o
OBJS-$(CONFIG_RIFFDEC) += riffdec.o
OBJS-$(CONFIG_RIFFENC) += riffenc.o
@ -260,7 +261,7 @@ OBJS-$(CONFIG_HLS_DEMUXER) += hls.o hls_sample_encryption.o
OBJS-$(CONFIG_HLS_MUXER) += hlsenc.o hlsplaylist.o avc.o
OBJS-$(CONFIG_HNM_DEMUXER) += hnm.o
OBJS-$(CONFIG_IAMF_DEMUXER) += iamfdec.o
OBJS-$(CONFIG_IAMF_MUXER) += iamfenc.o iamf_writer.o iamf.o
OBJS-$(CONFIG_IAMF_MUXER) += iamfenc.o
OBJS-$(CONFIG_ICO_DEMUXER) += icodec.o
OBJS-$(CONFIG_ICO_MUXER) += icoenc.o
OBJS-$(CONFIG_IDCIN_DEMUXER) += idcin.o

View File

@ -852,3 +852,225 @@ int ff_iamf_write_descriptors(const IAMFContext *iamf, AVIOContext *pb, void *lo
return 0;
}
static int write_parameter_block(const IAMFContext *iamf, AVIOContext *pb,
const AVIAMFParamDefinition *param, void *log_ctx)
{
uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
IAMFParamDefinition *param_definition = ff_iamf_get_param_definition(iamf, param->parameter_id);
PutBitContext pbc;
AVIOContext *dyn_bc;
uint8_t *dyn_buf = NULL;
int dyn_size, ret;
if (param->type > AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN) {
av_log(log_ctx, AV_LOG_DEBUG, "Ignoring side data with unknown type %u\n",
param->type);
return 0;
}
if (!param_definition) {
av_log(log_ctx, AV_LOG_ERROR, "Non-existent Parameter Definition with ID %u referenced by a packet\n",
param->parameter_id);
return AVERROR(EINVAL);
}
if (param->type != param_definition->param->type) {
av_log(log_ctx, AV_LOG_ERROR, "Inconsistent values for Parameter Definition "
"with ID %u in a packet\n",
param->parameter_id);
return AVERROR(EINVAL);
}
ret = avio_open_dyn_buf(&dyn_bc);
if (ret < 0)
return ret;
// Sequence Header
init_put_bits(&pbc, header, sizeof(header));
put_bits(&pbc, 5, IAMF_OBU_IA_PARAMETER_BLOCK);
put_bits(&pbc, 3, 0);
flush_put_bits(&pbc);
avio_write(pb, header, put_bytes_count(&pbc, 1));
ffio_write_leb(dyn_bc, param->parameter_id);
if (!param_definition->mode) {
ffio_write_leb(dyn_bc, param->duration);
ffio_write_leb(dyn_bc, param->constant_subblock_duration);
if (param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, param->nb_subblocks);
}
for (int i = 0; i < param->nb_subblocks; i++) {
const void *subblock = av_iamf_param_definition_get_subblock(param, i);
switch (param->type) {
case AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN: {
const AVIAMFMixGain *mix = subblock;
if (!param_definition->mode && param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, mix->subblock_duration);
ffio_write_leb(dyn_bc, mix->animation_type);
avio_wb16(dyn_bc, rescale_rational(mix->start_point_value, 1 << 8));
if (mix->animation_type >= AV_IAMF_ANIMATION_TYPE_LINEAR)
avio_wb16(dyn_bc, rescale_rational(mix->end_point_value, 1 << 8));
if (mix->animation_type == AV_IAMF_ANIMATION_TYPE_BEZIER) {
avio_wb16(dyn_bc, rescale_rational(mix->control_point_value, 1 << 8));
avio_w8(dyn_bc, av_clip_uint8(av_rescale(mix->control_point_relative_time.num, 1 << 8,
mix->control_point_relative_time.den)));
}
break;
}
case AV_IAMF_PARAMETER_DEFINITION_DEMIXING: {
const AVIAMFDemixingInfo *demix = subblock;
if (!param_definition->mode && param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, demix->subblock_duration);
avio_w8(dyn_bc, demix->dmixp_mode << 5);
break;
}
case AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN: {
const AVIAMFReconGain *recon = subblock;
const AVIAMFAudioElement *audio_element = param_definition->audio_element->celement;
if (!param_definition->mode && param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, recon->subblock_duration);
if (!audio_element) {
av_log(log_ctx, AV_LOG_ERROR, "Invalid Parameter Definition with ID %u referenced by a packet\n", param->parameter_id);
return AVERROR(EINVAL);
}
for (int j = 0; j < audio_element->nb_layers; j++) {
const AVIAMFLayer *layer = audio_element->layers[j];
if (layer->flags & AV_IAMF_LAYER_FLAG_RECON_GAIN) {
unsigned int recon_gain_flags = 0;
int k = 0;
for (; k < 7; k++)
recon_gain_flags |= (1 << k) * !!recon->recon_gain[j][k];
for (; k < 12; k++)
recon_gain_flags |= (2 << k) * !!recon->recon_gain[j][k];
if (recon_gain_flags >> 8)
recon_gain_flags |= (1 << k);
ffio_write_leb(dyn_bc, recon_gain_flags);
for (k = 0; k < 12; k++) {
if (recon->recon_gain[j][k])
avio_w8(dyn_bc, recon->recon_gain[j][k]);
}
}
}
break;
}
default:
av_assert0(0);
}
}
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
ffio_write_leb(pb, dyn_size);
avio_write(pb, dyn_buf, dyn_size);
ffio_free_dyn_buf(&dyn_bc);
return 0;
}
int ff_iamf_write_parameter_blocks(const IAMFContext *iamf, AVIOContext *pb,
AVPacket *pkt, void *log_ctx)
{
AVIAMFParamDefinition *mix =
(AVIAMFParamDefinition *)av_packet_get_side_data(pkt,
AV_PKT_DATA_IAMF_MIX_GAIN_PARAM,
NULL);
AVIAMFParamDefinition *demix =
(AVIAMFParamDefinition *)av_packet_get_side_data(pkt,
AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM,
NULL);
AVIAMFParamDefinition *recon =
(AVIAMFParamDefinition *)av_packet_get_side_data(pkt,
AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM,
NULL);
if (mix) {
int ret = write_parameter_block(iamf, pb, mix, log_ctx);
if (ret < 0)
return ret;
}
if (demix) {
int ret = write_parameter_block(iamf, pb, demix, log_ctx);
if (ret < 0)
return ret;
}
if (recon) {
int ret = write_parameter_block(iamf, pb, recon, log_ctx);
if (ret < 0)
return ret;
}
return 0;
}
int ff_iamf_write_audio_frame(const IAMFContext *iamf, AVIOContext *pb,
unsigned audio_substream_id, AVPacket *pkt)
{
uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
PutBitContext pbc;
AVIOContext *dyn_bc;
uint8_t *side_data, *dyn_buf = NULL;
unsigned int skip_samples = 0, discard_padding = 0;
size_t side_data_size;
int dyn_size, type = audio_substream_id <= 17 ?
audio_substream_id + IAMF_OBU_IA_AUDIO_FRAME_ID0 : IAMF_OBU_IA_AUDIO_FRAME;
int ret;
if (!pkt->size) {
uint8_t *new_extradata = av_packet_get_side_data(pkt,
AV_PKT_DATA_NEW_EXTRADATA,
NULL);
if (!new_extradata)
return AVERROR_INVALIDDATA;
// TODO: update FLAC Streaminfo on seekable output
return 0;
}
side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES,
&side_data_size);
if (side_data && side_data_size >= 10) {
skip_samples = AV_RL32(side_data);
discard_padding = AV_RL32(side_data + 4);
}
ret = avio_open_dyn_buf(&dyn_bc);
if (ret < 0)
return ret;
init_put_bits(&pbc, header, sizeof(header));
put_bits(&pbc, 5, type);
put_bits(&pbc, 1, 0); // obu_redundant_copy
put_bits(&pbc, 1, skip_samples || discard_padding);
put_bits(&pbc, 1, 0); // obu_extension_flag
flush_put_bits(&pbc);
avio_write(pb, header, put_bytes_count(&pbc, 1));
if (skip_samples || discard_padding) {
ffio_write_leb(dyn_bc, discard_padding);
ffio_write_leb(dyn_bc, skip_samples);
}
if (audio_substream_id > 17)
ffio_write_leb(dyn_bc, audio_substream_id);
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
ffio_write_leb(pb, dyn_size + pkt->size);
avio_write(pb, dyn_buf, dyn_size);
ffio_free_dyn_buf(&dyn_bc);
avio_write(pb, pkt->data, pkt->size);
return 0;
}

View File

@ -24,6 +24,7 @@
#include <stddef.h>
#include "libavcodec/packet.h"
#include "avformat.h"
#include "avio.h"
#include "iamf.h"
@ -47,4 +48,9 @@ int ff_iamf_add_mix_presentation(IAMFContext *iamf, const AVStreamGroup *stg, vo
int ff_iamf_write_descriptors(const IAMFContext *iamf, AVIOContext *pb, void *log_ctx);
int ff_iamf_write_parameter_blocks(const IAMFContext *iamf, AVIOContext *pb,
AVPacket *pkt, void *log_ctx);
int ff_iamf_write_audio_frame(const IAMFContext *iamf, AVIOContext *pb,
unsigned audio_substream_id, AVPacket *pkt);
#endif /* AVFORMAT_IAMF_WRITER_H */

View File

@ -133,219 +133,18 @@ static int iamf_write_header(AVFormatContext *s)
return 0;
}
static inline int rescale_rational(AVRational q, int b)
{
return av_clip_int16(av_rescale(q.num, b, q.den));
}
static int write_parameter_block(AVFormatContext *s, const AVIAMFParamDefinition *param)
{
const IAMFMuxContext *const c = s->priv_data;
const IAMFContext *const iamf = &c->iamf;
uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
IAMFParamDefinition *param_definition = ff_iamf_get_param_definition(iamf, param->parameter_id);
PutBitContext pb;
AVIOContext *dyn_bc;
uint8_t *dyn_buf = NULL;
int dyn_size, ret;
if (param->type > AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN) {
av_log(s, AV_LOG_DEBUG, "Ignoring side data with unknown type %u\n",
param->type);
return 0;
}
if (!param_definition) {
av_log(s, AV_LOG_ERROR, "Non-existent Parameter Definition with ID %u referenced by a packet\n",
param->parameter_id);
return AVERROR(EINVAL);
}
if (param->type != param_definition->param->type) {
av_log(s, AV_LOG_ERROR, "Inconsistent values for Parameter Definition "
"with ID %u in a packet\n",
param->parameter_id);
return AVERROR(EINVAL);
}
ret = avio_open_dyn_buf(&dyn_bc);
if (ret < 0)
return ret;
// Sequence Header
init_put_bits(&pb, header, sizeof(header));
put_bits(&pb, 5, IAMF_OBU_IA_PARAMETER_BLOCK);
put_bits(&pb, 3, 0);
flush_put_bits(&pb);
avio_write(s->pb, header, put_bytes_count(&pb, 1));
ffio_write_leb(dyn_bc, param->parameter_id);
if (!param_definition->mode) {
ffio_write_leb(dyn_bc, param->duration);
ffio_write_leb(dyn_bc, param->constant_subblock_duration);
if (param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, param->nb_subblocks);
}
for (int i = 0; i < param->nb_subblocks; i++) {
const void *subblock = av_iamf_param_definition_get_subblock(param, i);
switch (param->type) {
case AV_IAMF_PARAMETER_DEFINITION_MIX_GAIN: {
const AVIAMFMixGain *mix = subblock;
if (!param_definition->mode && param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, mix->subblock_duration);
ffio_write_leb(dyn_bc, mix->animation_type);
avio_wb16(dyn_bc, rescale_rational(mix->start_point_value, 1 << 8));
if (mix->animation_type >= AV_IAMF_ANIMATION_TYPE_LINEAR)
avio_wb16(dyn_bc, rescale_rational(mix->end_point_value, 1 << 8));
if (mix->animation_type == AV_IAMF_ANIMATION_TYPE_BEZIER) {
avio_wb16(dyn_bc, rescale_rational(mix->control_point_value, 1 << 8));
avio_w8(dyn_bc, av_clip_uint8(av_rescale(mix->control_point_relative_time.num, 1 << 8,
mix->control_point_relative_time.den)));
}
break;
}
case AV_IAMF_PARAMETER_DEFINITION_DEMIXING: {
const AVIAMFDemixingInfo *demix = subblock;
if (!param_definition->mode && param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, demix->subblock_duration);
avio_w8(dyn_bc, demix->dmixp_mode << 5);
break;
}
case AV_IAMF_PARAMETER_DEFINITION_RECON_GAIN: {
const AVIAMFReconGain *recon = subblock;
const AVIAMFAudioElement *audio_element = param_definition->audio_element->celement;
if (!param_definition->mode && param->constant_subblock_duration == 0)
ffio_write_leb(dyn_bc, recon->subblock_duration);
if (!audio_element) {
av_log(s, AV_LOG_ERROR, "Invalid Parameter Definition with ID %u referenced by a packet\n", param->parameter_id);
return AVERROR(EINVAL);
}
for (int j = 0; j < audio_element->nb_layers; j++) {
const AVIAMFLayer *layer = audio_element->layers[j];
if (layer->flags & AV_IAMF_LAYER_FLAG_RECON_GAIN) {
unsigned int recon_gain_flags = 0;
int k = 0;
for (; k < 7; k++)
recon_gain_flags |= (1 << k) * !!recon->recon_gain[j][k];
for (; k < 12; k++)
recon_gain_flags |= (2 << k) * !!recon->recon_gain[j][k];
if (recon_gain_flags >> 8)
recon_gain_flags |= (1 << k);
ffio_write_leb(dyn_bc, recon_gain_flags);
for (k = 0; k < 12; k++) {
if (recon->recon_gain[j][k])
avio_w8(dyn_bc, recon->recon_gain[j][k]);
}
}
}
break;
}
default:
av_assert0(0);
}
}
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
ffio_write_leb(s->pb, dyn_size);
avio_write(s->pb, dyn_buf, dyn_size);
ffio_free_dyn_buf(&dyn_bc);
return 0;
}
static int iamf_write_packet(AVFormatContext *s, AVPacket *pkt)
{
const IAMFMuxContext *const c = s->priv_data;
AVStream *st = s->streams[pkt->stream_index];
uint8_t header[MAX_IAMF_OBU_HEADER_SIZE];
PutBitContext pb;
AVIOContext *dyn_bc;
uint8_t *side_data, *dyn_buf = NULL;
unsigned int skip_samples = 0, discard_padding = 0;
size_t side_data_size;
int dyn_size, type = st->id <= 17 ? st->id + IAMF_OBU_IA_AUDIO_FRAME_ID0 : IAMF_OBU_IA_AUDIO_FRAME;
int ret;
int ret = 0;
if (!pkt->size) {
uint8_t *new_extradata = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, NULL);
if (st->id == c->first_stream_id)
ret = ff_iamf_write_parameter_blocks(&c->iamf, s->pb, pkt, s);
if (!ret)
ret = ff_iamf_write_audio_frame(&c->iamf, s->pb, st->id, pkt);
if (!new_extradata)
return AVERROR_INVALIDDATA;
// TODO: update FLAC Streaminfo on seekable output
return 0;
}
if (s->nb_stream_groups && st->id == c->first_stream_id) {
AVIAMFParamDefinition *mix =
(AVIAMFParamDefinition *)av_packet_get_side_data(pkt, AV_PKT_DATA_IAMF_MIX_GAIN_PARAM, NULL);
AVIAMFParamDefinition *demix =
(AVIAMFParamDefinition *)av_packet_get_side_data(pkt, AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM, NULL);
AVIAMFParamDefinition *recon =
(AVIAMFParamDefinition *)av_packet_get_side_data(pkt, AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM, NULL);
if (mix) {
ret = write_parameter_block(s, mix);
if (ret < 0)
return ret;
}
if (demix) {
ret = write_parameter_block(s, demix);
if (ret < 0)
return ret;
}
if (recon) {
ret = write_parameter_block(s, recon);
if (ret < 0)
return ret;
}
}
side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES,
&side_data_size);
if (side_data && side_data_size >= 10) {
skip_samples = AV_RL32(side_data);
discard_padding = AV_RL32(side_data + 4);
}
ret = avio_open_dyn_buf(&dyn_bc);
if (ret < 0)
return ret;
init_put_bits(&pb, header, sizeof(header));
put_bits(&pb, 5, type);
put_bits(&pb, 1, 0); // obu_redundant_copy
put_bits(&pb, 1, skip_samples || discard_padding);
put_bits(&pb, 1, 0); // obu_extension_flag
flush_put_bits(&pb);
avio_write(s->pb, header, put_bytes_count(&pb, 1));
if (skip_samples || discard_padding) {
ffio_write_leb(dyn_bc, discard_padding);
ffio_write_leb(dyn_bc, skip_samples);
}
if (st->id > 17)
ffio_write_leb(dyn_bc, st->id);
dyn_size = avio_get_dyn_buf(dyn_bc, &dyn_buf);
ffio_write_leb(s->pb, dyn_size + pkt->size);
avio_write(s->pb, dyn_buf, dyn_size);
ffio_free_dyn_buf(&dyn_bc);
avio_write(s->pb, pkt->data, pkt->size);
return 0;
return ret;
}
static void iamf_deinit(AVFormatContext *s)
@ -354,8 +153,6 @@ static void iamf_deinit(AVFormatContext *s)
IAMFContext *const iamf = &c->iamf;
ff_iamf_uninit_context(iamf);
return;
}
static const AVCodecTag iamf_codec_tags[] = {