-#include "avformat.h"
-#include "allformats.h"
-#include "avi.h"
-#include "intfloat_readwrite.h"
-
-const CodecTag codec_aiff_tags[] = {
- { CODEC_ID_PCM_S16BE, MKTAG('N','O','N','E') },
- { CODEC_ID_PCM_S8, MKTAG('N','O','N','E') },
- { CODEC_ID_PCM_S24BE, MKTAG('N','O','N','E') },
- { CODEC_ID_PCM_S32BE, MKTAG('N','O','N','E') },
- { CODEC_ID_PCM_ALAW, MKTAG('a','l','a','w') },
- { CODEC_ID_PCM_ALAW, MKTAG('A','L','A','W') },
- { CODEC_ID_PCM_MULAW, MKTAG('u','l','a','w') },
- { CODEC_ID_PCM_MULAW, MKTAG('U','L','A','W') },
- { CODEC_ID_MACE3, MKTAG('M','A','C','3') },
- { CODEC_ID_MACE6, MKTAG('M','A','C','6') },
- { CODEC_ID_GSM, MKTAG('G','S','M',' ') },
- { CODEC_ID_ADPCM_G726, MKTAG('G','7','2','6') },
- { 0, 0 },
-};
-
-#define AIFF 0
-#define AIFF_C_VERSION1 0xA2805140
-
-static int aiff_codec_get_id (int bps)
-{
- if (bps <= 8)
- return CODEC_ID_PCM_S8;
- if (bps <= 16)
- return CODEC_ID_PCM_S16BE;
- if (bps <= 24)
- return CODEC_ID_PCM_S24BE;
- if (bps <= 32)
- return CODEC_ID_PCM_S32BE;
-
- /* bigger than 32 isn't allowed */
- return 0;
-}
-
-/* returns the size of the found tag */
-static int get_tag(ByteIOContext *pb, uint32_t * tag)
-{
- int size;
-
- if (url_feof(pb))
- return AVERROR_IO;
-
- *tag = get_le32(pb);
- size = get_be32(pb);
-
- if (size < 0)
- size = 0x7fffffff;
-
- return size;
-}
-
-/* Metadata string read */
-static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
-{
- int res;
-
- if (size > strsize-1)
- res = get_buffer(pb, (uint8_t*)str, strsize-1);
- else
- res = get_buffer(pb, (uint8_t*)str, size);
-
- if (res < 0)
- return;
-
- str[res] = 0;
- if (size & 1)
- size++;
- size -= res;
- if (size);
- url_fskip(pb, size);
-}
-
-/* Returns the number of bits per second */
-static int fix_bps(int codec_id)
-{
- switch (codec_id) {
- case CODEC_ID_PCM_S8:
- return 8;
- case CODEC_ID_PCM_S16BE:
- return 16;
- case CODEC_ID_PCM_S24BE:
- return 24;
- case CODEC_ID_PCM_S32BE:
- return 32;
- }
-
- return -1;
-}
-
-/* Returns the number of sound data frames or negative on error */
-static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
- int size, unsigned version)
-{
- AVExtFloat ext;
- double sample_rate;
- unsigned int num_frames;
-
-
- if (size & 1)
- size++;
-
- codec->codec_type = CODEC_TYPE_AUDIO;
- codec->channels = get_be16(pb);
- num_frames = get_be32(pb);
- codec->bits_per_sample = get_be16(pb);
-
- get_buffer(pb, (uint8_t*)&ext, sizeof(ext));/* Sample rate is in */
- sample_rate = av_ext2dbl(ext); /* 80 bits BE IEEE extended float */
- codec->sample_rate = sample_rate;
- size -= 18;
-
- /* Got an AIFF-C? */
- if (version == AIFF_C_VERSION1) {
- codec->codec_tag = get_le32(pb);
- codec->codec_id = codec_get_id (codec_aiff_tags, codec->codec_tag);
-
- if (codec->codec_id == CODEC_ID_PCM_S16BE) {
- codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
- codec->bits_per_sample = fix_bps(codec->codec_id);
- }
-
- size -= 4;
- } else {
- /* Need the codec type */
- codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
- codec->bits_per_sample = fix_bps(codec->codec_id);
- }
-
- if (!codec->codec_id)
- return AVERROR_INVALIDDATA;
-
- /* Block align needs to be computed in all cases, as the definition
- * is specific to applications -> here we use the WAVE format definition */
- codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
-
- codec->bit_rate = codec->sample_rate * codec->block_align;
-
- /* Chunk is over */
- if (size)
- url_fseek(pb, size, SEEK_CUR);
-
- return num_frames;
-}
-
-#ifdef CONFIG_MUXERS
-typedef struct {
- offset_t form;
- offset_t frames;
- offset_t ssnd;
-} AIFFOutputContext;
-
-static int aiff_write_header(AVFormatContext *s)
-{
- AIFFOutputContext *aiff = s->priv_data;
- ByteIOContext *pb = &s->pb;
- AVCodecContext *enc = s->streams[0]->codec;
- AVExtFloat sample_rate;
- int coder_len;
-
- /* First verify if format is ok */
- enc->codec_tag = codec_get_tag(codec_aiff_tags, enc->codec_id);
- if (!enc->codec_tag) {
- av_free(aiff);
- return -1;
- }
-
- coder_len = strlen(enc->codec->name);
-
- /* FORM AIFF header */
- put_tag(pb, "FORM");
- aiff->form = url_ftell(pb);
- put_be32(pb, 0); /* file length */
- put_tag(pb, "AIFC");
-
- /* Version chunk */
- put_tag(pb, "FVER");
- put_be32(pb, 4);
- put_be32(pb, 0xA2805140);
-
- /* Common chunk */
- put_tag(pb, "COMM");
- if (coder_len & 1) /* Common chunk is of var size */
- put_be32(pb, 23+coder_len);
- else
- put_be32(pb, 24+coder_len);
- put_be16(pb, enc->channels); /* Number of channels */
-
- aiff->frames = url_ftell(pb);
- put_be32(pb, 0); /* Number of frames */
-
- if (!enc->bits_per_sample)
- enc->bits_per_sample = (enc->block_align<<3) / enc->channels;
- put_be16(pb, enc->bits_per_sample); /* Sample size */
-
- sample_rate = av_dbl2ext((double)enc->sample_rate);
- put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
-
- put_le32(pb, enc->codec_tag);
- if (coder_len & 1) {
- put_byte(pb, coder_len);
- put_buffer(pb, (const uint8_t*)enc->codec->name, coder_len);
- } else {
- put_byte(pb, coder_len+1);
- put_buffer(pb, (const uint8_t*)enc->codec->name, coder_len);
- put_byte(pb, 0);
- }
-
- /* Sound data chunk */
- put_tag(pb, "SSND");
- aiff->ssnd = url_ftell(pb); /* Sound chunk size */
- put_be32(pb, 0); /* Sound samples data size */
- put_be32(pb, 0); /* Data offset */
- put_be32(pb, 0); /* Block-size (block align) */
-
- av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);