Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
28 | 1 | static int aea_write_header(AVFormatContext *s) | |
29 | { | ||
30 | const AVDictionaryEntry *title_entry; | ||
31 | 1 | size_t title_length = 0; | |
32 | 1 | AVStream *st = s->streams[0]; | |
33 | |||
34 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | 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 | 1 | avio_wl32(s->pb, 2048); | |
47 | |||
48 | /* Write AEA title */ | ||
49 | 1 | title_entry = av_dict_get(st->metadata, "title", NULL, 0); | |
50 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (title_entry) { |
51 | 1 | const char *title_contents = title_entry->value; | |
52 | 1 | title_length = strlen(title_contents); | |
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | 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 | 1 | avio_write(s->pb, title_contents, title_length); | |
58 | } | ||
59 | |||
60 | 1 | 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 | 1 | avio_wl32(s->pb, 0); | |
64 | 1 | avio_w8(s->pb, st->codecpar->ch_layout.nb_channels); | |
65 | 1 | avio_w8(s->pb, 0); | |
66 | |||
67 | /* Pad the header to 2048 bytes */ | ||
68 | 1 | ffio_fill(s->pb, 0, 1782); | |
69 | |||
70 | 1 | return 0; | |
71 | } | ||
72 | |||
73 | 1 | static int aea_write_trailer(struct AVFormatContext *s) | |
74 | { | ||
75 | int64_t total_blocks; | ||
76 | 1 | AVIOContext *pb = s->pb; | |
77 | 1 | AVStream *st = s->streams[0]; | |
78 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (pb->seekable & AVIO_SEEKABLE_NORMAL) { |
79 | /* Seek to rewrite the block count. */ | ||
80 | 1 | avio_seek(pb, 260, SEEK_SET); | |
81 | 1 | total_blocks = st->nb_frames * st->codecpar->ch_layout.nb_channels; | |
82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | 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 | 1 | avio_wl32(pb, total_blocks); | |
88 | } else { | ||
89 | ✗ | av_log(s, AV_LOG_WARNING, "Unable to rewrite AEA header.\n"); | |
90 | } | ||
91 | |||
92 | 1 | return 0; | |
93 | } | ||
94 | |||
95 | const FFOutputFormat ff_aea_muxer = { | ||
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 | | ||
104 | FF_OFMT_FLAG_ONLY_DEFAULT_CODECS, | ||
105 | .write_header = aea_write_header, | ||
106 | .write_packet = ff_raw_write_packet, | ||
107 | .write_trailer = aea_write_trailer, | ||
108 | }; | ||
109 |