| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * AU muxer and demuxer | ||
| 3 | * Copyright (c) 2001 Fabrice Bellard | ||
| 4 | * | ||
| 5 | * first version by Francois Revol <revol@free.fr> | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 9 | * FFmpeg is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with FFmpeg; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | /* | ||
| 25 | * Reference documents: | ||
| 26 | * http://www.opengroup.org/public/pubs/external/auformat.html | ||
| 27 | * http://www.goice.co.jp/member/mo/formats/au.html | ||
| 28 | */ | ||
| 29 | |||
| 30 | #include "config_components.h" | ||
| 31 | |||
| 32 | #include "libavutil/bprint.h" | ||
| 33 | #include "libavutil/intreadwrite.h" | ||
| 34 | #include "libavutil/mem.h" | ||
| 35 | #include "avformat.h" | ||
| 36 | #include "demux.h" | ||
| 37 | #include "internal.h" | ||
| 38 | #include "avio_internal.h" | ||
| 39 | #include "mux.h" | ||
| 40 | #include "pcm.h" | ||
| 41 | #include "libavutil/avassert.h" | ||
| 42 | |||
| 43 | /* if we don't know the size in advance */ | ||
| 44 | #define AU_UNKNOWN_SIZE ((uint32_t)(~0)) | ||
| 45 | |||
| 46 | static const AVCodecTag codec_au_tags[] = { | ||
| 47 | { AV_CODEC_ID_PCM_MULAW, 1 }, | ||
| 48 | { AV_CODEC_ID_PCM_S8, 2 }, | ||
| 49 | { AV_CODEC_ID_PCM_S16BE, 3 }, | ||
| 50 | { AV_CODEC_ID_PCM_S24BE, 4 }, | ||
| 51 | { AV_CODEC_ID_PCM_S32BE, 5 }, | ||
| 52 | { AV_CODEC_ID_PCM_F32BE, 6 }, | ||
| 53 | { AV_CODEC_ID_PCM_F64BE, 7 }, | ||
| 54 | { AV_CODEC_ID_ADPCM_G726LE, 23 }, | ||
| 55 | { AV_CODEC_ID_ADPCM_G722,24 }, | ||
| 56 | { AV_CODEC_ID_ADPCM_G726LE, 25 }, | ||
| 57 | { AV_CODEC_ID_ADPCM_G726LE, 26 }, | ||
| 58 | { AV_CODEC_ID_PCM_ALAW, 27 }, | ||
| 59 | { AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') }, | ||
| 60 | { AV_CODEC_ID_NONE, 0 }, | ||
| 61 | }; | ||
| 62 | |||
| 63 | static const AVCodecTag *const au_codec_tags[] = { codec_au_tags, NULL }; | ||
| 64 | |||
| 65 | #if CONFIG_AU_DEMUXER | ||
| 66 | |||
| 67 | 7279 | static int au_probe(const AVProbeData *p) | |
| 68 | { | ||
| 69 |
1/2✓ Branch 0 taken 7279 times.
✗ Branch 1 not taken.
|
7279 | if (p->buf_size < 24 || |
| 70 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7273 times.
|
7279 | AV_RL32(p->buf) != MKTAG('.', 's', 'n', 'd') || |
| 71 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | AV_RN32(p->buf+4) == 0 || |
| 72 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | AV_RN32(p->buf+8) == 0 || |
| 73 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | AV_RN32(p->buf+12) == 0 || |
| 74 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | AV_RN32(p->buf+16) == 0 || |
| 75 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | AV_RN32(p->buf+20) == 0) |
| 76 | 7273 | return 0; | |
| 77 | 6 | return AVPROBE_SCORE_MAX; | |
| 78 | } | ||
| 79 | |||
| 80 | 6 | static int au_read_annotation(AVFormatContext *s, int size) | |
| 81 | { | ||
| 82 | static const char keys[][7] = { | ||
| 83 | "title", | ||
| 84 | "artist", | ||
| 85 | "album", | ||
| 86 | "track", | ||
| 87 | "genre", | ||
| 88 | }; | ||
| 89 | 6 | AVIOContext *pb = s->pb; | |
| 90 | 6 | enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY; | |
| 91 | char c; | ||
| 92 | AVBPrint bprint; | ||
| 93 | 6 | char * key = NULL; | |
| 94 | 6 | char * value = NULL; | |
| 95 | int ret, i; | ||
| 96 | |||
| 97 | 6 | av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); | |
| 98 | |||
| 99 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 6 times.
|
70 | while (size-- > 0) { |
| 100 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
|
64 | if (avio_feof(pb)) { |
| 101 | ✗ | av_bprint_finalize(&bprint, NULL); | |
| 102 | ✗ | av_freep(&key); | |
| 103 | ✗ | return AVERROR_EOF; | |
| 104 | } | ||
| 105 | 64 | c = avio_r8(pb); | |
| 106 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
|
64 | switch(state) { |
| 107 | 16 | case PARSE_KEY: | |
| 108 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
|
16 | if (c == '\0') { |
| 109 | 4 | state = PARSE_FINISHED; | |
| 110 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
|
12 | } else if (c == '=') { |
| 111 | 2 | ret = av_bprint_finalize(&bprint, &key); | |
| 112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
| 113 | ✗ | return ret; | |
| 114 | 2 | av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); | |
| 115 | 2 | state = PARSE_VALUE; | |
| 116 | } else { | ||
| 117 | 10 | av_bprint_chars(&bprint, c, 1); | |
| 118 | } | ||
| 119 | 16 | break; | |
| 120 | 18 | case PARSE_VALUE: | |
| 121 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
18 | if (c == '\0' || c == '\n') { |
| 122 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (av_bprint_finalize(&bprint, &value) != 0) { |
| 123 | ✗ | av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n"); | |
| 124 | } else { | ||
| 125 | 2 | av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED); | |
| 126 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | for (i = 0; i < FF_ARRAY_ELEMS(keys); i++) { |
| 127 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if (av_strcasecmp(keys[i], key) == 0) { |
| 128 | 2 | av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL); | |
| 129 | 2 | value = NULL; | |
| 130 | 2 | break; | |
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | 2 | av_freep(&key); | |
| 135 | 2 | av_freep(&value); | |
| 136 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY; |
| 137 | } else { | ||
| 138 | 16 | av_bprint_chars(&bprint, c, 1); | |
| 139 | } | ||
| 140 | 18 | break; | |
| 141 | 30 | case PARSE_FINISHED: | |
| 142 | 30 | break; | |
| 143 | ✗ | default: | |
| 144 | /* should never happen */ | ||
| 145 | ✗ | av_assert0(0); | |
| 146 | } | ||
| 147 | } | ||
| 148 | 6 | av_bprint_finalize(&bprint, NULL); | |
| 149 | 6 | av_freep(&key); | |
| 150 | 6 | return 0; | |
| 151 | } | ||
| 152 | |||
| 153 | #define BLOCK_SIZE 1024 | ||
| 154 | |||
| 155 | 6 | static int au_read_header(AVFormatContext *s) | |
| 156 | { | ||
| 157 | 6 | int size, data_size = 0; | |
| 158 | unsigned int tag; | ||
| 159 | 6 | AVIOContext *pb = s->pb; | |
| 160 | unsigned int id, channels, rate; | ||
| 161 | 6 | int bps, ba = 0; | |
| 162 | enum AVCodecID codec; | ||
| 163 | AVStream *st; | ||
| 164 | int ret; | ||
| 165 | |||
| 166 | 6 | tag = avio_rl32(pb); | |
| 167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (tag != MKTAG('.', 's', 'n', 'd')) |
| 168 | ✗ | return AVERROR_INVALIDDATA; | |
| 169 | 6 | size = avio_rb32(pb); /* header size */ | |
| 170 | 6 | data_size = avio_rb32(pb); /* data size in bytes */ | |
| 171 | |||
| 172 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
6 | if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) { |
| 173 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size); | |
| 174 | ✗ | return AVERROR_INVALIDDATA; | |
| 175 | } | ||
| 176 | |||
| 177 | 6 | id = avio_rb32(pb); | |
| 178 | 6 | rate = avio_rb32(pb); | |
| 179 | 6 | channels = avio_rb32(pb); | |
| 180 | |||
| 181 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (size > 24) { |
| 182 | /* parse annotation field to get metadata */ | ||
| 183 | 6 | ret = au_read_annotation(s, size - 24); | |
| 184 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
| 185 | ✗ | return ret; | |
| 186 | } | ||
| 187 | |||
| 188 | 6 | codec = ff_codec_get_id(codec_au_tags, id); | |
| 189 | |||
| 190 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (codec == AV_CODEC_ID_NONE) { |
| 191 | ✗ | avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id); | |
| 192 | ✗ | return AVERROR_PATCHWELCOME; | |
| 193 | } | ||
| 194 | |||
| 195 | 6 | bps = av_get_bits_per_sample(codec); | |
| 196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (codec == AV_CODEC_ID_ADPCM_G726LE) { |
| 197 | ✗ | if (id == MKBETAG('7','2','6','2')) { | |
| 198 | ✗ | bps = 2; | |
| 199 | } else { | ||
| 200 | ✗ | const uint8_t bpcss[] = {4, 0, 3, 5}; | |
| 201 | ✗ | av_assert0(id >= 23 && id < 23 + 4); | |
| 202 | ✗ | ba = bpcss[id - 23]; | |
| 203 | ✗ | bps = bpcss[id - 23]; | |
| 204 | } | ||
| 205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | } else if (!bps) { |
| 206 | ✗ | avpriv_request_sample(s, "Unknown bits per sample"); | |
| 207 | ✗ | return AVERROR_PATCHWELCOME; | |
| 208 | } | ||
| 209 | |||
| 210 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) { |
| 211 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels); | |
| 212 | ✗ | return AVERROR_INVALIDDATA; | |
| 213 | } | ||
| 214 | |||
| 215 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | if (rate == 0 || rate > INT_MAX) { |
| 216 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate); | |
| 217 | ✗ | return AVERROR_INVALIDDATA; | |
| 218 | } | ||
| 219 | |||
| 220 | 6 | st = avformat_new_stream(s, NULL); | |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (!st) |
| 222 | ✗ | return AVERROR(ENOMEM); | |
| 223 | 6 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 224 | 6 | st->codecpar->codec_tag = id; | |
| 225 | 6 | st->codecpar->codec_id = codec; | |
| 226 | 6 | st->codecpar->ch_layout.nb_channels = channels; | |
| 227 | 6 | st->codecpar->sample_rate = rate; | |
| 228 | 6 | st->codecpar->bits_per_coded_sample = bps; | |
| 229 | 6 | st->codecpar->bit_rate = channels * rate * bps; | |
| 230 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | st->codecpar->block_align = ba ? ba : FFMAX(bps * channels / 8, 1); |
| 231 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (data_size != AU_UNKNOWN_SIZE) |
| 232 | 6 | st->duration = (((int64_t)data_size)<<3) / (channels * (int64_t)bps); | |
| 233 | |||
| 234 | 6 | st->start_time = 0; | |
| 235 | 6 | avpriv_set_pts_info(st, 64, 1, rate); | |
| 236 | |||
| 237 | 6 | return 0; | |
| 238 | } | ||
| 239 | |||
| 240 | const FFInputFormat ff_au_demuxer = { | ||
| 241 | .p.name = "au", | ||
| 242 | .p.long_name = NULL_IF_CONFIG_SMALL("Sun AU"), | ||
| 243 | .p.codec_tag = au_codec_tags, | ||
| 244 | .read_probe = au_probe, | ||
| 245 | .read_header = au_read_header, | ||
| 246 | .read_packet = ff_pcm_read_packet, | ||
| 247 | .read_seek = ff_pcm_read_seek, | ||
| 248 | }; | ||
| 249 | |||
| 250 | #endif /* CONFIG_AU_DEMUXER */ | ||
| 251 | |||
| 252 | #if CONFIG_AU_MUXER | ||
| 253 | |||
| 254 | typedef struct AUContext { | ||
| 255 | uint32_t header_size; | ||
| 256 | } AUContext; | ||
| 257 | |||
| 258 | #include "rawenc.h" | ||
| 259 | |||
| 260 | 3 | static int au_get_annotations(AVFormatContext *s, AVBPrint *annotations) | |
| 261 | { | ||
| 262 | static const char keys[][7] = { | ||
| 263 | "Title", | ||
| 264 | "Artist", | ||
| 265 | "Album", | ||
| 266 | "Track", | ||
| 267 | "Genre", | ||
| 268 | }; | ||
| 269 | 3 | int cnt = 0; | |
| 270 | 3 | AVDictionary *m = s->metadata; | |
| 271 | 3 | AVDictionaryEntry *t = NULL; | |
| 272 | |||
| 273 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
|
18 | for (int i = 0; i < FF_ARRAY_ELEMS(keys); i++) { |
| 274 | 15 | t = av_dict_get(m, keys[i], NULL, 0); | |
| 275 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
|
15 | if (t != NULL) { |
| 276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (cnt++) |
| 277 | ✗ | av_bprint_chars(annotations, '\n', 1); | |
| 278 | 1 | av_bprintf(annotations, "%s=%s", keys[i], t->value); | |
| 279 | } | ||
| 280 | } | ||
| 281 | /* The specification requires the annotation field to be zero-terminated | ||
| 282 | * and its length to be a multiple of eight, so pad with 0's */ | ||
| 283 | 3 | av_bprint_chars(annotations, '\0', 8); | |
| 284 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | return av_bprint_is_complete(annotations) ? 0 : AVERROR(ENOMEM); |
| 285 | } | ||
| 286 | |||
| 287 | 3 | static int au_write_header(AVFormatContext *s) | |
| 288 | { | ||
| 289 | int ret; | ||
| 290 | 3 | AUContext *au = s->priv_data; | |
| 291 | 3 | AVIOContext *pb = s->pb; | |
| 292 | 3 | AVCodecParameters *par = s->streams[0]->codecpar; | |
| 293 | AVBPrint annotations; | ||
| 294 | |||
| 295 | 3 | par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id); | |
| 296 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!par->codec_tag) { |
| 297 | ✗ | av_log(s, AV_LOG_ERROR, "unsupported codec\n"); | |
| 298 | ✗ | return AVERROR(EINVAL); | |
| 299 | } | ||
| 300 | |||
| 301 | 3 | av_bprint_init(&annotations, 0, INT_MAX - 24); | |
| 302 | 3 | ret = au_get_annotations(s, &annotations); | |
| 303 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (ret < 0) |
| 304 | ✗ | goto fail; | |
| 305 | 3 | au->header_size = 24 + annotations.len & ~7; | |
| 306 | |||
| 307 | 3 | ffio_wfourcc(pb, ".snd"); /* magic number */ | |
| 308 | 3 | avio_wb32(pb, au->header_size); /* header size */ | |
| 309 | 3 | avio_wb32(pb, AU_UNKNOWN_SIZE); /* data size */ | |
| 310 | 3 | avio_wb32(pb, par->codec_tag); /* codec ID */ | |
| 311 | 3 | avio_wb32(pb, par->sample_rate); | |
| 312 | 3 | avio_wb32(pb, par->ch_layout.nb_channels); | |
| 313 | 3 | avio_write(pb, annotations.str, annotations.len & ~7); | |
| 314 | |||
| 315 | 3 | fail: | |
| 316 | 3 | av_bprint_finalize(&annotations, NULL); | |
| 317 | |||
| 318 | 3 | return ret; | |
| 319 | } | ||
| 320 | |||
| 321 | 3 | static int au_write_trailer(AVFormatContext *s) | |
| 322 | { | ||
| 323 | 3 | AVIOContext *pb = s->pb; | |
| 324 | 3 | AUContext *au = s->priv_data; | |
| 325 | 3 | int64_t file_size = avio_tell(pb); | |
| 326 | |||
| 327 |
2/4✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) { |
| 328 | /* update file size */ | ||
| 329 | 3 | avio_seek(pb, 8, SEEK_SET); | |
| 330 | 3 | avio_wb32(pb, (uint32_t)(file_size - au->header_size)); | |
| 331 | 3 | avio_seek(pb, file_size, SEEK_SET); | |
| 332 | } | ||
| 333 | |||
| 334 | 3 | return 0; | |
| 335 | } | ||
| 336 | |||
| 337 | const FFOutputFormat ff_au_muxer = { | ||
| 338 | .p.name = "au", | ||
| 339 | .p.long_name = NULL_IF_CONFIG_SMALL("Sun AU"), | ||
| 340 | .p.mime_type = "audio/basic", | ||
| 341 | .p.extensions = "au", | ||
| 342 | .p.codec_tag = au_codec_tags, | ||
| 343 | .p.audio_codec = AV_CODEC_ID_PCM_S16BE, | ||
| 344 | .p.video_codec = AV_CODEC_ID_NONE, | ||
| 345 | .p.subtitle_codec = AV_CODEC_ID_NONE, | ||
| 346 | .p.flags = AVFMT_NOTIMESTAMPS, | ||
| 347 | .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH, | ||
| 348 | .priv_data_size = sizeof(AUContext), | ||
| 349 | .write_header = au_write_header, | ||
| 350 | .write_packet = ff_raw_write_packet, | ||
| 351 | .write_trailer = au_write_trailer, | ||
| 352 | }; | ||
| 353 | |||
| 354 | #endif /* CONFIG_AU_MUXER */ | ||
| 355 |