]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/aiff.c
correctly compute bit rate for compressed audio, should fix seeking in aiff
[ffmpeg] / libavformat / aiff.c
index e4cf66c3b40edae9bdc9ebc8393973bc31240dbe..4343e79c48b90842facd8d822ea3f293c43f4197 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
-#include "allformats.h"
+#include "raw.h"
 #include "riff.h"
 #include "intfloat_readwrite.h"
 
-static const CodecTag codec_aiff_tags[] = {
+static const AVCodecTag 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') },
+    { CODEC_ID_PCM_S16LE, MKTAG('s','o','w','t') },
+    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i','m','a','4') },
+    { CODEC_ID_QDM2, MKTAG('Q','D','M','2') },
     { 0, 0 },
 };
 
@@ -63,7 +64,7 @@ static int get_tag(ByteIOContext *pb, uint32_t * tag)
     int size;
 
     if (url_feof(pb))
-        return AVERROR_IO;
+        return AVERROR(EIO);
 
     *tag = get_le32(pb);
     size = get_be32(pb);
@@ -91,7 +92,7 @@ static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
     if (size & 1)
         size++;
     size -= res;
-    if (size);
+    if (size)
         url_fskip(pb, size);
 }
 
@@ -103,10 +104,8 @@ static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
     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);
@@ -120,17 +119,28 @@ static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
     /* 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);
+        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);
+        switch (codec->codec_id) {
+        case CODEC_ID_PCM_S16BE:
+            codec->codec_id = aiff_codec_get_id(codec->bits_per_sample);
             codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
+            break;
+        case CODEC_ID_ADPCM_IMA_QT:
+            codec->block_align = 34*codec->channels;
+            codec->frame_size = 64;
+            break;
+        case CODEC_ID_MACE3:
+        case CODEC_ID_MACE6:
+            codec->frame_size = 6;
+            break;
+        default:
+            break;
         }
-
         size -= 4;
     } else {
         /* Need the codec type */
-        codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
+        codec->codec_id = aiff_codec_get_id(codec->bits_per_sample);
         codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
     }
 
@@ -139,9 +149,11 @@ static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
 
     /* 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;
+    if (!codec->block_align)
+        codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
 
-    codec->bit_rate = codec->sample_rate * (codec->block_align << 3);
+    codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
+                       codec->sample_rate) * (codec->block_align << 3);
 
     /* Chunk is over */
     if (size)
@@ -160,35 +172,42 @@ typedef struct {
 static int aiff_write_header(AVFormatContext *s)
 {
     AIFFOutputContext *aiff = s->priv_data;
-    ByteIOContext *pb = &s->pb;
+    ByteIOContext *pb = s->pb;
     AVCodecContext *enc = s->streams[0]->codec;
     AVExtFloat sample_rate;
+    int aifc = 0;
 
     /* 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);
+    if (!enc->codec_tag)
         return -1;
-    }
+    if (enc->codec_tag != MKTAG('N','O','N','E'))
+        aifc = 1;
 
     /* FORM AIFF header */
     put_tag(pb, "FORM");
     aiff->form = url_ftell(pb);
     put_be32(pb, 0);                    /* file length */
-    put_tag(pb, "AIFC");
+    put_tag(pb, aifc ? "AIFC" : "AIFF");
 
-    /* Version chunk */
-    put_tag(pb, "FVER");
-    put_be32(pb, 4);
-    put_be32(pb, 0xA2805140);
+    if (aifc) { // compressed audio
+        enc->bits_per_sample = 16;
+        if (!enc->block_align) {
+            av_log(s, AV_LOG_ERROR, "block align not set\n");
+            return -1;
+        }
+        /* Version chunk */
+        put_tag(pb, "FVER");
+        put_be32(pb, 4);
+        put_be32(pb, 0xA2805140);
+    }
 
     /* Common chunk */
     put_tag(pb, "COMM");
-    put_be32(pb, 24); /* size */
-    put_be16(pb, enc->channels);        /* Number of channels */
+    put_be32(pb, aifc ? 24 : 18); /* size */
+    put_be16(pb, enc->channels);  /* Number of channels */
 
     aiff->frames = url_ftell(pb);
-    put_be32(pb, 0);                    /* Number of frames */
+    put_be32(pb, 0);              /* Number of frames */
 
     if (!enc->bits_per_sample)
         enc->bits_per_sample = av_get_bits_per_sample(enc->codec_id);
@@ -204,8 +223,10 @@ static int aiff_write_header(AVFormatContext *s)
     sample_rate = av_dbl2ext((double)enc->sample_rate);
     put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
 
-    put_le32(pb, enc->codec_tag);
-    put_be16(pb, 0);
+    if (aifc) {
+        put_le32(pb, enc->codec_tag);
+        put_be16(pb, 0);
+    }
 
     /* Sound data chunk */
     put_tag(pb, "SSND");
@@ -224,14 +245,14 @@ static int aiff_write_header(AVFormatContext *s)
 
 static int aiff_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
-    ByteIOContext *pb = &s->pb;
+    ByteIOContext *pb = s->pb;
     put_buffer(pb, pkt->data, pkt->size);
     return 0;
 }
 
 static int aiff_write_trailer(AVFormatContext *s)
 {
-    ByteIOContext *pb = &s->pb;
+    ByteIOContext *pb = s->pb;
     AIFFOutputContext *aiff = s->priv_data;
     AVCodecContext *enc = s->streams[0]->codec;
 
@@ -243,18 +264,18 @@ static int aiff_write_trailer(AVFormatContext *s)
         end_size++;
     }
 
-    if (!url_is_streamed(&s->pb)) {
+    if (!url_is_streamed(s->pb)) {
         /* File length */
         url_fseek(pb, aiff->form, SEEK_SET);
-        put_be32(pb, (uint32_t)(file_size - aiff->form - 4));
+        put_be32(pb, file_size - aiff->form - 4);
 
         /* Number of sample frames */
         url_fseek(pb, aiff->frames, SEEK_SET);
-        put_be32(pb, ((uint32_t)(file_size-aiff->ssnd-12))/enc->block_align);
+        put_be32(pb, (file_size-aiff->ssnd-12)/enc->block_align);
 
         /* Sound Data chunk size */
         url_fseek(pb, aiff->ssnd, SEEK_SET);
-        put_be32(pb, (uint32_t)(file_size - aiff->ssnd - 4));
+        put_be32(pb, file_size - aiff->ssnd - 4);
 
         /* return to the end */
         url_fseek(pb, end_size, SEEK_SET);
@@ -269,8 +290,6 @@ static int aiff_write_trailer(AVFormatContext *s)
 static int aiff_probe(AVProbeData *p)
 {
     /* check file header */
-    if (p->buf_size < 16)
-        return 0;
     if (p->buf[0] == 'F' && p->buf[1] == 'O' &&
         p->buf[2] == 'R' && p->buf[3] == 'M' &&
         p->buf[8] == 'A' && p->buf[9] == 'I' &&
@@ -284,10 +303,11 @@ static int aiff_probe(AVProbeData *p)
 static int aiff_read_header(AVFormatContext *s,
                             AVFormatParameters *ap)
 {
-    int size, filesize, offset;
+    int size, filesize;
+    offset_t offset = 0;
     uint32_t tag;
     unsigned version = AIFF_C_VERSION1;
-    ByteIOContext *pb = &s->pb;
+    ByteIOContext *pb = s->pb;
     AVStream * st = s->streams[0];
 
     /* check FORM header */
@@ -306,7 +326,7 @@ static int aiff_read_header(AVFormatContext *s,
 
     st = av_new_stream(s, 0);
     if (!st)
-        return AVERROR_NOMEM;
+        return AVERROR(ENOMEM);
 
     while (filesize > 0) {
         /* parse different chunks */
@@ -317,42 +337,54 @@ static int aiff_read_header(AVFormatContext *s,
         filesize -= size + 8;
 
         switch (tag) {
-            case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
-                /* Then for the complete header info */
-                st->nb_frames = get_aiff_header (pb, st->codec, size, version);
-                if (st->nb_frames < 0)
-                        return st->nb_frames;
-                break;
-
-            case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
-                version = get_be32(pb);
-                break;
-
-            case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
-                get_meta (pb, s->title, sizeof(s->title), size);
-                break;
-
-            case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
-                get_meta (pb, s->author, sizeof(s->author), size);
-                break;
-
-            case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
-                get_meta (pb, s->copyright, sizeof(s->copyright), size);
-                break;
-
-            case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
-                get_meta (pb, s->comment, sizeof(s->comment), size);
-                break;
-
-            case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
-                get_be32(pb);               /* Block align... don't care */
-                offset = get_be32(pb);      /* Offset of sound data */
+        case MKTAG('C', 'O', 'M', 'M'):     /* Common chunk */
+            /* Then for the complete header info */
+            st->nb_frames = get_aiff_header (pb, st->codec, size, version);
+            if (st->nb_frames < 0)
+                return st->nb_frames;
+            if (offset > 0) // COMM is after SSND
                 goto got_sound;
-
-            default: /* Jump */
-                if (size & 1)   /* Always even aligned */
-                    size++;
-                url_fskip (pb, size);
+            break;
+        case MKTAG('F', 'V', 'E', 'R'):     /* Version chunk */
+            version = get_be32(pb);
+            break;
+        case MKTAG('N', 'A', 'M', 'E'):     /* Sample name chunk */
+            get_meta (pb, s->title, sizeof(s->title), size);
+            break;
+        case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
+            get_meta (pb, s->author, sizeof(s->author), size);
+            break;
+        case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
+            get_meta (pb, s->copyright, sizeof(s->copyright), size);
+            break;
+        case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
+            get_meta (pb, s->comment, sizeof(s->comment), size);
+            break;
+        case MKTAG('S', 'S', 'N', 'D'):     /* Sampled sound chunk */
+            offset = get_be32(pb);      /* Offset of sound data */
+            get_be32(pb);               /* BlockSize... don't care */
+            offset += url_ftell(pb);    /* Compute absolute data offset */
+            if (st->codec->codec_id)    /* Assume COMM already parsed */
+                goto got_sound;
+            if (url_is_streamed(pb)) {
+                av_log(s, AV_LOG_ERROR, "file is not seekable\n");
+                return -1;
+            }
+            url_fskip(pb, size - 8);
+            break;
+        case MKTAG('w', 'a', 'v', 'e'):
+            if ((uint64_t)size > (1<<30))
+                return -1;
+            st->codec->extradata = av_mallocz(size + FF_INPUT_BUFFER_PADDING_SIZE);
+            if (!st->codec->extradata)
+                return AVERROR(ENOMEM);
+            st->codec->extradata_size = size;
+            get_buffer(pb, st->codec->extradata, size);
+            break;
+        default: /* Jump */
+            if (size & 1)   /* Always even aligned */
+                size++;
+            url_fskip (pb, size);
         }
     }
 
@@ -366,10 +398,11 @@ got_sound:
 
     av_set_pts_info(st, 64, 1, st->codec->sample_rate);
     st->start_time = 0;
-    st->duration = st->nb_frames;
+    st->duration = st->codec->frame_size ?
+        st->nb_frames * st->codec->frame_size : st->nb_frames;
 
     /* Position the stream at the first block */
-    url_fskip(pb, offset);
+    url_fseek(pb, offset, SEEK_SET);
 
     return 0;
 }
@@ -383,11 +416,11 @@ static int aiff_read_packet(AVFormatContext *s,
     int res;
 
     /* End of stream may be reached */
-    if (url_feof(&s->pb))
-        return AVERROR_IO;
+    if (url_feof(s->pb))
+        return AVERROR(EIO);
 
     /* Now for that packet */
-    res = av_get_packet(&s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
+    res = av_get_packet(s->pb, pkt, (MAX_SIZE / st->codec->block_align) * st->codec->block_align);
     if (res < 0)
         return res;
 
@@ -396,11 +429,6 @@ static int aiff_read_packet(AVFormatContext *s,
     return 0;
 }
 
-static int aiff_read_close(AVFormatContext *s)
-{
-    return 0;
-}
-
 static int aiff_read_seek(AVFormatContext *s,
                           int stream_index, int64_t timestamp, int flags)
 {
@@ -415,8 +443,9 @@ AVInputFormat aiff_demuxer = {
     aiff_probe,
     aiff_read_header,
     aiff_read_packet,
-    aiff_read_close,
+    NULL,
     aiff_read_seek,
+    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
 };
 #endif
 
@@ -432,5 +461,6 @@ AVOutputFormat aiff_muxer = {
     aiff_write_header,
     aiff_write_packet,
     aiff_write_trailer,
+    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
 };
 #endif