LCOV - code coverage report
Current view: top level - libavformat - au.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 128 155 82.6 %
Date: 2017-12-12 11:08:38 Functions: 6 6 100.0 %

          Line data    Source code
       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 "avformat.h"
      31             : #include "internal.h"
      32             : #include "avio_internal.h"
      33             : #include "pcm.h"
      34             : #include "libavutil/avassert.h"
      35             : 
      36             : /* if we don't know the size in advance */
      37             : #define AU_UNKNOWN_SIZE ((uint32_t)(~0))
      38             : /* the specification requires an annotation field of at least eight bytes */
      39             : #define AU_DEFAULT_HEADER_SIZE (24+8)
      40             : 
      41             : static const AVCodecTag codec_au_tags[] = {
      42             :     { AV_CODEC_ID_PCM_MULAW,  1 },
      43             :     { AV_CODEC_ID_PCM_S8,     2 },
      44             :     { AV_CODEC_ID_PCM_S16BE,  3 },
      45             :     { AV_CODEC_ID_PCM_S24BE,  4 },
      46             :     { AV_CODEC_ID_PCM_S32BE,  5 },
      47             :     { AV_CODEC_ID_PCM_F32BE,  6 },
      48             :     { AV_CODEC_ID_PCM_F64BE,  7 },
      49             :     { AV_CODEC_ID_ADPCM_G726LE, 23 },
      50             :     { AV_CODEC_ID_ADPCM_G722,24 },
      51             :     { AV_CODEC_ID_ADPCM_G726LE, 25 },
      52             :     { AV_CODEC_ID_ADPCM_G726LE, 26 },
      53             :     { AV_CODEC_ID_PCM_ALAW,  27 },
      54             :     { AV_CODEC_ID_ADPCM_G726LE, MKBETAG('7','2','6','2') },
      55             :     { AV_CODEC_ID_NONE,       0 },
      56             : };
      57             : 
      58             : #if CONFIG_AU_DEMUXER
      59             : 
      60        6130 : static int au_probe(AVProbeData *p)
      61             : {
      62        6136 :     if (p->buf[0] == '.' && p->buf[1] == 's' &&
      63          12 :         p->buf[2] == 'n' && p->buf[3] == 'd')
      64           6 :         return AVPROBE_SCORE_MAX;
      65             :     else
      66        6124 :         return 0;
      67             : }
      68             : 
      69           6 : static int au_read_annotation(AVFormatContext *s, int size)
      70             : {
      71             :     static const char * keys[] = {
      72             :         "title",
      73             :         "artist",
      74             :         "album",
      75             :         "track",
      76             :         "genre",
      77             :         NULL };
      78           6 :     AVIOContext *pb = s->pb;
      79           6 :     enum { PARSE_KEY, PARSE_VALUE, PARSE_FINISHED } state = PARSE_KEY;
      80             :     char c;
      81             :     AVBPrint bprint;
      82           6 :     char * key = NULL;
      83           6 :     char * value = NULL;
      84             :     int i;
      85             : 
      86           6 :     av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
      87             : 
      88           6 :     while (size-- > 0) {
      89          64 :         c = avio_r8(pb);
      90          64 :         switch(state) {
      91          16 :         case PARSE_KEY:
      92          16 :             if (c == '\0') {
      93           4 :                 state = PARSE_FINISHED;
      94          12 :             } else if (c == '=') {
      95           2 :                 av_bprint_finalize(&bprint, &key);
      96           2 :                 av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
      97           2 :                 state = PARSE_VALUE;
      98             :             } else {
      99          10 :                 av_bprint_chars(&bprint, c, 1);
     100             :             }
     101          16 :             break;
     102          18 :         case PARSE_VALUE:
     103          18 :             if (c == '\0' || c == '\n') {
     104           2 :                 if (av_bprint_finalize(&bprint, &value) != 0) {
     105           0 :                     av_log(s, AV_LOG_ERROR, "Memory error while parsing AU metadata.\n");
     106             :                 } else {
     107           2 :                     av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
     108           4 :                     for (i = 0; keys[i] != NULL && key != NULL; i++) {
     109           2 :                         if (av_strcasecmp(keys[i], key) == 0) {
     110           2 :                             av_dict_set(&(s->metadata), keys[i], value, AV_DICT_DONT_STRDUP_VAL);
     111           2 :                             av_freep(&key);
     112           2 :                             value = NULL;
     113             :                         }
     114             :                     }
     115             :                 }
     116           2 :                 av_freep(&key);
     117           2 :                 av_freep(&value);
     118           2 :                 state = (c == '\0') ? PARSE_FINISHED : PARSE_KEY;
     119             :             } else {
     120          16 :                 av_bprint_chars(&bprint, c, 1);
     121             :             }
     122          18 :             break;
     123          30 :         case PARSE_FINISHED:
     124          30 :             break;
     125           0 :         default:
     126             :             /* should never happen */
     127           0 :             av_assert0(0);
     128             :         }
     129             :     }
     130           6 :     av_bprint_finalize(&bprint, NULL);
     131           6 :     av_freep(&key);
     132           6 :     return 0;
     133             : }
     134             : 
     135             : #define BLOCK_SIZE 1024
     136             : 
     137           6 : static int au_read_header(AVFormatContext *s)
     138             : {
     139           6 :     int size, data_size = 0;
     140             :     unsigned int tag;
     141           6 :     AVIOContext *pb = s->pb;
     142             :     unsigned int id, channels, rate;
     143             :     int bps;
     144             :     enum AVCodecID codec;
     145             :     AVStream *st;
     146             : 
     147           6 :     tag = avio_rl32(pb);
     148           6 :     if (tag != MKTAG('.', 's', 'n', 'd'))
     149           0 :         return AVERROR_INVALIDDATA;
     150           6 :     size = avio_rb32(pb); /* header size */
     151           6 :     data_size = avio_rb32(pb); /* data size in bytes */
     152             : 
     153           6 :     if (data_size < 0 && data_size != AU_UNKNOWN_SIZE) {
     154           0 :         av_log(s, AV_LOG_ERROR, "Invalid negative data size '%d' found\n", data_size);
     155           0 :         return AVERROR_INVALIDDATA;
     156             :     }
     157             : 
     158           6 :     id       = avio_rb32(pb);
     159           6 :     rate     = avio_rb32(pb);
     160           6 :     channels = avio_rb32(pb);
     161             : 
     162           6 :     if (size > 24) {
     163             :         /* parse annotation field to get metadata */
     164           6 :         au_read_annotation(s, size - 24);
     165             :     }
     166             : 
     167           6 :     codec = ff_codec_get_id(codec_au_tags, id);
     168             : 
     169           6 :     if (codec == AV_CODEC_ID_NONE) {
     170           0 :         avpriv_request_sample(s, "unknown or unsupported codec tag: %u", id);
     171           0 :         return AVERROR_PATCHWELCOME;
     172             :     }
     173             : 
     174           6 :     bps = av_get_bits_per_sample(codec);
     175           6 :     if (codec == AV_CODEC_ID_ADPCM_G726LE) {
     176           0 :         if (id == MKBETAG('7','2','6','2')) {
     177           0 :             bps = 2;
     178             :         } else {
     179           0 :             const uint8_t bpcss[] = {4, 0, 3, 5};
     180           0 :             av_assert0(id >= 23 && id < 23 + 4);
     181           0 :             bps = bpcss[id - 23];
     182             :         }
     183           6 :     } else if (!bps) {
     184           0 :         avpriv_request_sample(s, "Unknown bits per sample");
     185           0 :         return AVERROR_PATCHWELCOME;
     186             :     }
     187             : 
     188           6 :     if (channels == 0 || channels >= INT_MAX / (BLOCK_SIZE * bps >> 3)) {
     189           0 :         av_log(s, AV_LOG_ERROR, "Invalid number of channels %u\n", channels);
     190           0 :         return AVERROR_INVALIDDATA;
     191             :     }
     192             : 
     193           6 :     if (rate == 0 || rate > INT_MAX) {
     194           0 :         av_log(s, AV_LOG_ERROR, "Invalid sample rate: %u\n", rate);
     195           0 :         return AVERROR_INVALIDDATA;
     196             :     }
     197             : 
     198           6 :     st = avformat_new_stream(s, NULL);
     199           6 :     if (!st)
     200           0 :         return AVERROR(ENOMEM);
     201           6 :     st->codecpar->codec_type  = AVMEDIA_TYPE_AUDIO;
     202           6 :     st->codecpar->codec_tag   = id;
     203           6 :     st->codecpar->codec_id    = codec;
     204           6 :     st->codecpar->channels    = channels;
     205           6 :     st->codecpar->sample_rate = rate;
     206           6 :     st->codecpar->bits_per_coded_sample = bps;
     207           6 :     st->codecpar->bit_rate    = channels * rate * bps;
     208           6 :     st->codecpar->block_align = FFMAX(bps * st->codecpar->channels / 8, 1);
     209           6 :     if (data_size != AU_UNKNOWN_SIZE)
     210           6 :         st->duration = (((int64_t)data_size)<<3) / (st->codecpar->channels * (int64_t)bps);
     211             : 
     212           6 :     st->start_time = 0;
     213           6 :     avpriv_set_pts_info(st, 64, 1, rate);
     214             : 
     215           6 :     return 0;
     216             : }
     217             : 
     218             : AVInputFormat ff_au_demuxer = {
     219             :     .name        = "au",
     220             :     .long_name   = NULL_IF_CONFIG_SMALL("Sun AU"),
     221             :     .read_probe  = au_probe,
     222             :     .read_header = au_read_header,
     223             :     .read_packet = ff_pcm_read_packet,
     224             :     .read_seek   = ff_pcm_read_seek,
     225             :     .codec_tag   = (const AVCodecTag* const []) { codec_au_tags, 0 },
     226             : };
     227             : 
     228             : #endif /* CONFIG_AU_DEMUXER */
     229             : 
     230             : #if CONFIG_AU_MUXER
     231             : 
     232             : typedef struct AUContext {
     233             :     uint32_t header_size;
     234             : } AUContext;
     235             : 
     236             : #include "rawenc.h"
     237             : 
     238           1 : static int au_get_annotations(AVFormatContext *s, char **buffer)
     239             : {
     240             :     static const char * keys[] = {
     241             :         "Title",
     242             :         "Artist",
     243             :         "Album",
     244             :         "Track",
     245             :         "Genre",
     246             :         NULL };
     247             :     int i;
     248           1 :     int cnt = 0;
     249           1 :     AVDictionary *m = s->metadata;
     250           1 :     AVDictionaryEntry *t = NULL;
     251             :     AVBPrint bprint;
     252             : 
     253           1 :     av_bprint_init(&bprint, 64, AV_BPRINT_SIZE_UNLIMITED);
     254             : 
     255           6 :     for (i = 0; keys[i] != NULL; i++) {
     256           5 :         t = av_dict_get(m, keys[i], NULL, 0);
     257           5 :         if (t != NULL) {
     258           1 :             if (cnt++)
     259           0 :                 av_bprint_chars(&bprint, '\n', 1);
     260           1 :             av_bprint_append_data(&bprint, keys[i], strlen(keys[i]));
     261           1 :             av_bprint_chars(&bprint, '=', 1);
     262           1 :             av_bprint_append_data(&bprint, t->value, strlen(t->value));
     263             :         }
     264             :     }
     265             :     /* pad with 0's */
     266           1 :     av_bprint_append_data(&bprint, "\0\0\0\0\0\0\0\0", 8);
     267           1 :     return av_bprint_finalize(&bprint, buffer);
     268             : }
     269             : 
     270           3 : static int au_write_header(AVFormatContext *s)
     271             : {
     272             :     int ret;
     273           3 :     AUContext *au = s->priv_data;
     274           3 :     AVIOContext *pb = s->pb;
     275           3 :     AVCodecParameters *par = s->streams[0]->codecpar;
     276           3 :     char *annotations = NULL;
     277             : 
     278           3 :     au->header_size = AU_DEFAULT_HEADER_SIZE;
     279             : 
     280           3 :     if (s->nb_streams != 1) {
     281           0 :         av_log(s, AV_LOG_ERROR, "only one stream is supported\n");
     282           0 :         return AVERROR(EINVAL);
     283             :     }
     284             : 
     285           3 :     par->codec_tag = ff_codec_get_tag(codec_au_tags, par->codec_id);
     286           3 :     if (!par->codec_tag) {
     287           0 :         av_log(s, AV_LOG_ERROR, "unsupported codec\n");
     288           0 :         return AVERROR(EINVAL);
     289             :     }
     290             : 
     291           3 :     if (av_dict_count(s->metadata) > 0) {
     292           1 :         ret = au_get_annotations(s, &annotations);
     293           1 :         if (ret < 0)
     294           0 :             return ret;
     295           1 :         if (annotations != NULL) {
     296           1 :             au->header_size = (24 + strlen(annotations) + 8) & ~7;
     297           1 :             if (au->header_size < AU_DEFAULT_HEADER_SIZE)
     298           0 :                 au->header_size = AU_DEFAULT_HEADER_SIZE;
     299             :         }
     300             :     }
     301           3 :     ffio_wfourcc(pb, ".snd");                   /* magic number */
     302           3 :     avio_wb32(pb, au->header_size);             /* header size */
     303           3 :     avio_wb32(pb, AU_UNKNOWN_SIZE);             /* data size */
     304           3 :     avio_wb32(pb, par->codec_tag);              /* codec ID */
     305           3 :     avio_wb32(pb, par->sample_rate);
     306           3 :     avio_wb32(pb, par->channels);
     307           3 :     if (annotations != NULL) {
     308           1 :         avio_write(pb, annotations, au->header_size - 24);
     309           1 :         av_freep(&annotations);
     310             :     } else {
     311           2 :         avio_wb64(pb, 0); /* annotation field */
     312             :     }
     313           3 :     avio_flush(pb);
     314             : 
     315           3 :     return 0;
     316             : }
     317             : 
     318           3 : static int au_write_trailer(AVFormatContext *s)
     319             : {
     320           3 :     AVIOContext *pb = s->pb;
     321           3 :     AUContext *au = s->priv_data;
     322           3 :     int64_t file_size = avio_tell(pb);
     323             : 
     324           3 :     if ((s->pb->seekable & AVIO_SEEKABLE_NORMAL) && file_size < INT32_MAX) {
     325             :         /* update file size */
     326           3 :         avio_seek(pb, 8, SEEK_SET);
     327           3 :         avio_wb32(pb, (uint32_t)(file_size - au->header_size));
     328           3 :         avio_seek(pb, file_size, SEEK_SET);
     329           3 :         avio_flush(pb);
     330             :     }
     331             : 
     332           3 :     return 0;
     333             : }
     334             : 
     335             : AVOutputFormat ff_au_muxer = {
     336             :     .name          = "au",
     337             :     .long_name     = NULL_IF_CONFIG_SMALL("Sun AU"),
     338             :     .mime_type     = "audio/basic",
     339             :     .extensions    = "au",
     340             :     .priv_data_size = sizeof(AUContext),
     341             :     .audio_codec   = AV_CODEC_ID_PCM_S16BE,
     342             :     .video_codec   = AV_CODEC_ID_NONE,
     343             :     .write_header  = au_write_header,
     344             :     .write_packet  = ff_raw_write_packet,
     345             :     .write_trailer = au_write_trailer,
     346             :     .codec_tag     = (const AVCodecTag* const []) { codec_au_tags, 0 },
     347             :     .flags         = AVFMT_NOTIMESTAMPS,
     348             : };
     349             : 
     350             : #endif /* CONFIG_AU_MUXER */

Generated by: LCOV version 1.13