FFmpeg
aeaenc.c
Go to the documentation of this file.
1 /*
2  * MD STUDIO audio muxer
3  *
4  * Copyright (c) 2024 asivery
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "avformat.h"
24 #include "avio_internal.h"
25 #include "rawenc.h"
26 #include "mux.h"
27 
29 {
30  const AVDictionaryEntry *title_entry;
31  size_t title_length = 0;
32  AVStream *st = s->streams[0];
33 
34  if (st->codecpar->ch_layout.nb_channels != 1 && st->codecpar->ch_layout.nb_channels != 2) {
35  av_log(s, AV_LOG_ERROR, "Only maximum 2 channels are supported in the audio"
36  " stream, %d channels were found.\n", st->codecpar->ch_layout.nb_channels);
37  return AVERROR(EINVAL);
38  }
39 
40  if (st->codecpar->sample_rate != 44100) {
41  av_log(s, AV_LOG_ERROR, "Invalid sample rate (%d) AEA only supports 44.1kHz.\n", st->codecpar->sample_rate);
42  return AVERROR(EINVAL);
43  }
44 
45  /* Write magic */
46  avio_wl32(s->pb, 2048);
47 
48  /* Write AEA title */
49  title_entry = av_dict_get(st->metadata, "title", NULL, 0);
50  if (title_entry) {
51  const char *title_contents = title_entry->value;
52  title_length = strlen(title_contents);
53  if (title_length > 256) {
54  av_log(s, AV_LOG_WARNING, "Title too long, truncated to 256 bytes.\n");
55  title_length = 256;
56  }
57  avio_write(s->pb, title_contents, title_length);
58  }
59 
60  ffio_fill(s->pb, 0, 256 - title_length);
61 
62  /* Write number of frames (zero at header-writing time, will seek later), number of channels */
63  avio_wl32(s->pb, 0);
65  avio_w8(s->pb, 0);
66 
67  /* Pad the header to 2048 bytes */
68  ffio_fill(s->pb, 0, 1782);
69 
70  return 0;
71 }
72 
73 static int aea_write_trailer(struct AVFormatContext *s)
74 {
75  int64_t total_blocks;
76  AVIOContext *pb = s->pb;
77  AVStream *st = s->streams[0];
78  if (pb->seekable & AVIO_SEEKABLE_NORMAL) {
79  /* Seek to rewrite the block count. */
80  avio_seek(pb, 260, SEEK_SET);
81  total_blocks = st->nb_frames * st->codecpar->ch_layout.nb_channels;
82  if (total_blocks > UINT32_MAX) {
83  av_log(s, AV_LOG_WARNING, "Too many frames in the file to properly encode the header (%"PRId64")."
84  " Block count in the header will be truncated.\n", total_blocks);
85  total_blocks = UINT32_MAX;
86  }
87  avio_wl32(pb, total_blocks);
88  } else {
89  av_log(s, AV_LOG_WARNING, "Unable to rewrite AEA header.\n");
90  }
91 
92  return 0;
93 }
94 
96  .p.name = "aea",
97  .p.long_name = NULL_IF_CONFIG_SMALL("MD STUDIO audio"),
98  .p.extensions = "aea",
99  .p.audio_codec = AV_CODEC_ID_ATRAC1,
100  .p.video_codec = AV_CODEC_ID_NONE,
101  .p.subtitle_codec = AV_CODEC_ID_NONE,
102 
103  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
105  .write_header = aea_write_header,
106  .write_packet = ff_raw_write_packet,
107  .write_trailer = aea_write_trailer,
108 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
int64_t
long long int64_t
Definition: coverity.c:34
aea_write_trailer
static int aea_write_trailer(struct AVFormatContext *s)
Definition: aeaenc.c:73
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition: mux.h:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:62
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
NULL
#define NULL
Definition: coverity.c:32
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:823
FFOutputFormat
Definition: mux.h:61
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:178
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:186
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:804
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
aea_write_header
static int aea_write_header(AVFormatContext *s)
Definition: aeaenc.c:28
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:200
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:356
rawenc.h
AV_CODEC_ID_ATRAC1
@ AV_CODEC_ID_ATRAC1
Definition: codec_id.h:486
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
avio_internal.h
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition: mux.h:50
ff_aea_muxer
const FFOutputFormat ff_aea_muxer
Definition: aeaenc.c:95
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
avformat.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
AVDictionaryEntry
Definition: dict.h:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVDictionaryEntry::value
char * value
Definition: dict.h:91
mux.h