[FFmpeg-devel] [PATCH] avformat/aea: Add aea muxer

asivery asivery at protonmail.com
Fri Mar 8 15:50:59 EET 2024


Thank you for your incredibly thorough and fast response. I've applied all the corrections you requested. Please let me know if there's anything else wrong with my patch, and thank you for your time.

- asivery

On Friday, March 8th, 2024 at 1:06 PM, Andreas Rheinhardt <andreas.rheinhardt at outlook.com> wrote:

> asivery via ffmpeg-devel:
> 
> > +typedef struct {
> > + int block_count;
> > +} AeaMuxerContext;
> > +
> > +static int aea_init(AVFormatContext *s)
> > +{
> > + AeaMuxerContext *c = s->priv_data;
> > + c->block_count = 0;
> 
> 
> Unnecessary: A muxer's priv_data is always pre-zeroed.
> 
> > +
> > + return 0;
> > +}
> > +
> > +static int aea_write_header(AVFormatContext *s)
> > +{
> > + AVDictionaryEntry *title_entry;
> 
> 
> const
> 
> > + int title_length = 0, i;
> > + char* title_contents;
> > + AVStream *st;
> > +
> > +
> > + if (s->nb_streams > 1) {
> > + av_log(s, AV_LOG_WARNING, "Got more than one stream to encode, they will be ignored.\n");
> 
> 
> We don't ignore extraneous streams, we normally error out if something
> like this happens.
> 
> > + }
> > +
> > + st = s->streams[0];
> > + if (st->codecpar->ch_layout.nb_channels != 1 && st->codecpar->ch_layout.nb_channels != 2) {
> > + av_log(s, AV_LOG_ERROR, "Invalid amount of channels to mux (%d).\n", st->codecpar->ch_layout.nb_channels);
> > + return AVERROR(EINVAL);
> > + }
> > +
> > + if (st->codecpar->sample_rate != 44100) {
> > + av_log(s, AV_LOG_ERROR, "Invalid sample rate (%d) AEA only supports 44.1kHz.\n", st->codecpar->sample_rate);
> > + return AVERROR(EINVAL);
> > + }
> > +
> > + /* Write magic /
> > + avio_wl32(s->pb, 2048);
> > +
> > + / Write AEA title */
> > + title_entry = av_dict_get(st->metadata, "title", NULL, 0);
> > + if (title_entry) {
> > + title_contents = title_entry->value;
> > + title_length = FFMIN(256, strlen(title_contents));
> 
> 
> Don't use strlen() in FFMIN (like many macros, it can evaluate its
> argument multiple times).
> 
> > + }
> > +
> > + if (title_length) {
> > + avio_write(s->pb, title_contents, title_length);
> > + }
> 
> 
> No need for this branch, just call this inside the "if (title_entry)"
> block above.
> 
> > +
> > + for (i = 0; i<(256 - title_length); i++) {
> > + avio_w8(s->pb, 0);
> > + }
> 
> 
> ffio_fill(). Same below.
> 
> > +
> > + /* Write number of frames (zero at header-writing time, will seek later), number of channels /
> > + avio_wl32(s->pb, 0);
> > + avio_w8(s->pb, st->codecpar->ch_layout.nb_channels);
> > + avio_w8(s->pb, 0);
> > +
> > + / Write flags (meaning unknown) /
> > + for(i = 0; i<11; i++) {
> > + avio_wl32(s->pb, 0);
> > + }
> > +
> > + / Pad the header to 2048 bytes */
> > + for(i = 0; i<1738; i++) avio_w8(s->pb, 0);
> > +
> > + return 0;
> > +}
> > +
> > +static int aea_write_packet(struct AVFormatContext *s, AVPacket *pkt)
> > +{
> > + AeaMuxerContext *c = s->priv_data;
> > + c->block_count++;
> > +
> > + avio_write(s->pb, pkt->data, pkt->size);
> > + return 0;
> 
> 
> You can avoid this and the whole muxer context by using
> ff_raw_write_packet() and relying on AVStream.nb_frames.
> 
> > +}
> > +
> > +static int aea_write_trailer(struct AVFormatContext *s)
> > +{
> > + AVIOContext *pb = s->pb;
> > + AeaMuxerContext c = s->priv_data;
> > + if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
> > + / Seek to rewrite the block count. */
> > + avio_seek(pb, 260, SEEK_SET);
> > + avio_wl32(pb, c->block_count * s->streams[0]->codecpar->ch_layout.nb_channels);
> > + } else {
> > + av_log(s, AV_LOG_WARNING, "unable to rewrite AEA header.\n");
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +const FFOutputFormat ff_aea_muxer = {
> > + .p.name = "aea",
> > + .p.long_name = NULL_IF_CONFIG_SMALL("MD STUDIO audio"),
> > + .priv_data_size = sizeof(AeaMuxerContext),
> > + .p.extensions = "aea",
> > + .p.audio_codec = AV_CODEC_ID_ATRAC1,
> > + .init = aea_init,
> > +
> > + .write_header = aea_write_header,
> > + .write_packet = aea_write_packet,
> > + .write_trailer = aea_write_trailer,
> > + .p.flags = AVFMT_GENERIC_INDEX,
> 
> 
> Makes no sense for a muxer.
> 
> > +};
> 
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-avformat-aea-Add-aea-muxer.patch
Type: text/x-patch
Size: 5622 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20240308/35878626/attachment.bin>


More information about the ffmpeg-devel mailing list