Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * AST muxer | ||
3 | * Copyright (c) 2012 James Almer | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include "avformat.h" | ||
23 | #include "avio_internal.h" | ||
24 | #include "internal.h" | ||
25 | #include "ast.h" | ||
26 | #include "mux.h" | ||
27 | #include "libavutil/mathematics.h" | ||
28 | #include "libavutil/opt.h" | ||
29 | |||
30 | typedef struct ASTMuxContext { | ||
31 | AVClass *class; | ||
32 | int64_t size; | ||
33 | int64_t samples; | ||
34 | int64_t loopstart; | ||
35 | int64_t loopend; | ||
36 | int fbs; | ||
37 | } ASTMuxContext; | ||
38 | |||
39 | #define CHECK_LOOP(type) \ | ||
40 | if (ast->loop ## type > 0) { \ | ||
41 | ast->loop ## type = av_rescale_rnd(ast->loop ## type, par->sample_rate, 1000, AV_ROUND_DOWN); \ | ||
42 | if (ast->loop ## type < 0 || ast->loop ## type > UINT_MAX) { \ | ||
43 | av_log(s, AV_LOG_ERROR, "Invalid loop" #type " value\n"); \ | ||
44 | return AVERROR(EINVAL); \ | ||
45 | } \ | ||
46 | } | ||
47 | |||
48 | 1 | static int ast_write_header(AVFormatContext *s) | |
49 | { | ||
50 | 1 | ASTMuxContext *ast = s->priv_data; | |
51 | 1 | AVIOContext *pb = s->pb; | |
52 | AVCodecParameters *par; | ||
53 | unsigned int codec_tag; | ||
54 | |||
55 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->nb_streams == 1) { |
56 | 1 | par = s->streams[0]->codecpar; | |
57 | } else { | ||
58 | ✗ | av_log(s, AV_LOG_ERROR, "only one stream is supported\n"); | |
59 | ✗ | return AVERROR(EINVAL); | |
60 | } | ||
61 | |||
62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (par->codec_id == AV_CODEC_ID_ADPCM_AFC) { |
63 | ✗ | av_log(s, AV_LOG_ERROR, "muxing ADPCM AFC is not implemented\n"); | |
64 | ✗ | return AVERROR_PATCHWELCOME; | |
65 | } | ||
66 | |||
67 | 1 | codec_tag = ff_codec_get_tag(ff_codec_ast_tags, par->codec_id); | |
68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!codec_tag) { |
69 | ✗ | av_log(s, AV_LOG_ERROR, "unsupported codec\n"); | |
70 | ✗ | return AVERROR(EINVAL); | |
71 | } | ||
72 | |||
73 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
|
1 | if (ast->loopend > 0 && ast->loopstart >= ast->loopend) { |
74 | ✗ | av_log(s, AV_LOG_ERROR, "loopend can't be less or equal to loopstart\n"); | |
75 | ✗ | return AVERROR(EINVAL); | |
76 | } | ||
77 | |||
78 | /* Convert milliseconds to samples */ | ||
79 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | CHECK_LOOP(start) |
80 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
1 | CHECK_LOOP(end) |
81 | |||
82 | 1 | ffio_wfourcc(pb, "STRM"); | |
83 | |||
84 | 1 | ast->size = avio_tell(pb); | |
85 | 1 | avio_wb32(pb, 0); /* File size minus header */ | |
86 | 1 | avio_wb16(pb, codec_tag); | |
87 | 1 | avio_wb16(pb, 16); /* Bit depth */ | |
88 | 1 | avio_wb16(pb, par->ch_layout.nb_channels); | |
89 | 1 | avio_wb16(pb, 0); /* Loop flag */ | |
90 | 1 | avio_wb32(pb, par->sample_rate); | |
91 | |||
92 | 1 | ast->samples = avio_tell(pb); | |
93 | 1 | avio_wb32(pb, 0); /* Number of samples */ | |
94 | 1 | avio_wb32(pb, 0); /* Loopstart */ | |
95 | 1 | avio_wb32(pb, 0); /* Loopend */ | |
96 | 1 | avio_wb32(pb, 0); /* Size of first block */ | |
97 | |||
98 | /* Unknown */ | ||
99 | 1 | avio_wb32(pb, 0); | |
100 | 1 | avio_wl32(pb, 0x7F); | |
101 | 1 | avio_wb64(pb, 0); | |
102 | 1 | avio_wb64(pb, 0); | |
103 | 1 | avio_wb32(pb, 0); | |
104 | |||
105 | 1 | return 0; | |
106 | } | ||
107 | |||
108 | 44 | static int ast_write_packet(AVFormatContext *s, AVPacket *pkt) | |
109 | { | ||
110 | 44 | AVIOContext *pb = s->pb; | |
111 | 44 | ASTMuxContext *ast = s->priv_data; | |
112 | 44 | AVCodecParameters *par = s->streams[0]->codecpar; | |
113 | 44 | int size = pkt->size / par->ch_layout.nb_channels; | |
114 | |||
115 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 43 times.
|
44 | if (s->streams[0]->nb_frames == 0) |
116 | 1 | ast->fbs = size; | |
117 | |||
118 | 44 | ffio_wfourcc(pb, "BLCK"); | |
119 | 44 | avio_wb32(pb, size); /* Block size */ | |
120 | |||
121 | /* padding */ | ||
122 | 44 | ffio_fill(pb, 0, 24); | |
123 | |||
124 | 44 | avio_write(pb, pkt->data, pkt->size); | |
125 | |||
126 | 44 | return 0; | |
127 | } | ||
128 | |||
129 | 1 | static int ast_write_trailer(AVFormatContext *s) | |
130 | { | ||
131 | 1 | AVIOContext *pb = s->pb; | |
132 | 1 | ASTMuxContext *ast = s->priv_data; | |
133 | 1 | AVCodecParameters *par = s->streams[0]->codecpar; | |
134 | 1 | int64_t file_size = avio_tell(pb); | |
135 | 1 | int64_t samples = (file_size - 64 - (32 * s->streams[0]->nb_frames)) / par->block_align; /* PCM_S16BE_PLANAR */ | |
136 | |||
137 | 1 | av_log(s, AV_LOG_DEBUG, "total samples: %"PRId64"\n", samples); | |
138 | |||
139 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) { |
140 | /* Number of samples */ | ||
141 | 1 | avio_seek(pb, ast->samples, SEEK_SET); | |
142 | 1 | avio_wb32(pb, samples); | |
143 | |||
144 | /* Loopstart if provided */ | ||
145 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ast->loopstart > 0) { |
146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ast->loopstart >= samples) { |
147 | ✗ | av_log(s, AV_LOG_WARNING, "Loopstart value is out of range and will be ignored\n"); | |
148 | ✗ | ast->loopstart = -1; | |
149 | ✗ | avio_skip(pb, 4); | |
150 | } else | ||
151 | 1 | avio_wb32(pb, ast->loopstart); | |
152 | } else | ||
153 | ✗ | avio_skip(pb, 4); | |
154 | |||
155 | /* Loopend if provided. Otherwise number of samples again */ | ||
156 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (ast->loopend && ast->loopstart >= 0) { |
157 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ast->loopend > samples) { |
158 | ✗ | av_log(s, AV_LOG_WARNING, "Loopend value is out of range and will be ignored\n"); | |
159 | ✗ | ast->loopend = samples; | |
160 | } | ||
161 | 1 | avio_wb32(pb, ast->loopend); | |
162 | } else { | ||
163 | ✗ | avio_wb32(pb, samples); | |
164 | } | ||
165 | |||
166 | /* Size of first block */ | ||
167 | 1 | avio_wb32(pb, ast->fbs); | |
168 | |||
169 | /* File size minus header */ | ||
170 | 1 | avio_seek(pb, ast->size, SEEK_SET); | |
171 | 1 | avio_wb32(pb, file_size - 64); | |
172 | |||
173 | /* Loop flag */ | ||
174 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ast->loopstart >= 0) { |
175 | 1 | avio_skip(pb, 6); | |
176 | 1 | avio_wb16(pb, 0xFFFF); | |
177 | } | ||
178 | |||
179 | 1 | avio_seek(pb, file_size, SEEK_SET); | |
180 | } | ||
181 | 1 | return 0; | |
182 | } | ||
183 | |||
184 | #define OFFSET(obj) offsetof(ASTMuxContext, obj) | ||
185 | static const AVOption options[] = { | ||
186 | { "loopstart", "Loopstart position in milliseconds.", OFFSET(loopstart), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, | ||
187 | { "loopend", "Loopend position in milliseconds.", OFFSET(loopend), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, | ||
188 | { NULL }, | ||
189 | }; | ||
190 | |||
191 | static const AVClass ast_muxer_class = { | ||
192 | .class_name = "AST muxer", | ||
193 | .item_name = av_default_item_name, | ||
194 | .option = options, | ||
195 | .version = LIBAVUTIL_VERSION_INT, | ||
196 | }; | ||
197 | |||
198 | const FFOutputFormat ff_ast_muxer = { | ||
199 | .p.name = "ast", | ||
200 | .p.long_name = NULL_IF_CONFIG_SMALL("AST (Audio Stream)"), | ||
201 | .p.extensions = "ast", | ||
202 | .priv_data_size = sizeof(ASTMuxContext), | ||
203 | .p.audio_codec = AV_CODEC_ID_PCM_S16BE_PLANAR, | ||
204 | .p.video_codec = AV_CODEC_ID_NONE, | ||
205 | .write_header = ast_write_header, | ||
206 | .write_packet = ast_write_packet, | ||
207 | .write_trailer = ast_write_trailer, | ||
208 | .p.priv_class = &ast_muxer_class, | ||
209 | .p.codec_tag = ff_ast_codec_tags_list, | ||
210 | }; | ||
211 |