]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blobdiff - libavformat/aiff.c
O_DIRECT works!!!
[frescor/ffmpeg.git] / libavformat / aiff.c
index 4a574644a76c32e802bc9dc0210edb8e8a4bd0e2..8fb7925be64e6e071b199a581c553706afecd927 100644 (file)
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+
+#include "libavutil/intfloat_readwrite.h"
 #include "avformat.h"
 #include "raw.h"
 #include "riff.h"
-#include "intfloat_readwrite.h"
 
 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_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_F32BE,    MKTAG('f','l','3','2') },
+    { CODEC_ID_PCM_F64BE,    MKTAG('f','l','6','4') },
+    { CODEC_ID_PCM_ALAW,     MKTAG('a','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 },
 };
 
 #define AIFF                    0
 #define AIFF_C_VERSION1         0xA2805140
 
-static int aiff_codec_get_id (int bps)
+static enum CodecID aiff_codec_get_id(int bps)
 {
     if (bps <= 8)
         return CODEC_ID_PCM_S8;
@@ -56,7 +58,7 @@ static int aiff_codec_get_id (int bps)
         return CODEC_ID_PCM_S32BE;
 
     /* bigger than 32 isn't allowed  */
-    return 0;
+    return CODEC_ID_NONE;
 }
 
 /* returns the size of the found tag */
@@ -77,15 +79,10 @@ static int get_tag(ByteIOContext *pb, uint32_t * tag)
 }
 
 /* Metadata string read */
-static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
+static void get_meta(AVFormatContext *s, const char *key, 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);
-
+    uint8_t str[1024];
+    int res = get_buffer(s->pb, str, FFMIN(sizeof(str)-1, size));
     if (res < 0)
         return;
 
@@ -94,7 +91,9 @@ static void get_meta(ByteIOContext *pb, char * str, int strsize, int size)
         size++;
     size -= res;
     if (size)
-        url_fskip(pb, size);
+        url_fskip(s->pb, size);
+
+    av_metadata_set(&s->metadata, key, str);
 }
 
 /* Returns the number of sound data frames or negative on error */
@@ -105,14 +104,12 @@ 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);
-    codec->bits_per_sample = get_be16(pb);
+    codec->bits_per_coded_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 */
@@ -122,36 +119,46 @@ 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);
 
         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);
+            codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
+            codec->bits_per_coded_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:
+            codec->block_align = 2*codec->channels;
+            codec->frame_size = 6;
+            break;
+        case CODEC_ID_MACE6:
+            codec->block_align = 1*codec->channels;
+            codec->frame_size = 6;
+            break;
+        case CODEC_ID_GSM:
+            codec->block_align = 33;
+            codec->frame_size = 160;
+            break;
         default:
             break;
         }
         size -= 4;
     } else {
         /* Need the codec type */
-        codec->codec_id = aiff_codec_get_id (codec->bits_per_sample);
-        codec->bits_per_sample = av_get_bits_per_sample(codec->codec_id);
+        codec->codec_id = aiff_codec_get_id(codec->bits_per_coded_sample);
+        codec->bits_per_coded_sample = av_get_bits_per_sample(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 */
     if (!codec->block_align)
-        codec->block_align = (codec->bits_per_sample * codec->channels) >> 3;
+        codec->block_align = (codec->bits_per_coded_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,11 +167,11 @@ static unsigned int get_aiff_header(ByteIOContext *pb, AVCodecContext *codec,
     return num_frames;
 }
 
-#ifdef CONFIG_MUXERS
+#if CONFIG_AIFF_MUXER
 typedef struct {
-    offset_t form;
-    offset_t frames;
-    offset_t ssnd;
+    int64_t form;
+    int64_t frames;
+    int64_t ssnd;
 } AIFFOutputContext;
 
 static int aiff_write_header(AVFormatContext *s)
@@ -176,10 +183,8 @@ static int aiff_write_header(AVFormatContext *s)
     int aifc = 0;
 
     /* First verify if format is ok */
-    if (!enc->codec_tag) {
+    if (!enc->codec_tag)
         return -1;
-    }
-
     if (enc->codec_tag != MKTAG('N','O','N','E'))
         aifc = 1;
 
@@ -190,7 +195,7 @@ static int aiff_write_header(AVFormatContext *s)
     put_tag(pb, aifc ? "AIFC" : "AIFF");
 
     if (aifc) { // compressed audio
-        enc->bits_per_sample = 16;
+        enc->bits_per_coded_sample = 16;
         if (!enc->block_align) {
             av_log(s, AV_LOG_ERROR, "block align not set\n");
             return -1;
@@ -204,21 +209,21 @@ static int aiff_write_header(AVFormatContext *s)
     /* Common chunk */
     put_tag(pb, "COMM");
     put_be32(pb, aifc ? 24 : 18); /* size */
-    put_be16(pb, enc->channels);        /* Number of channels */
+    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);
-    if (!enc->bits_per_sample) {
+    if (!enc->bits_per_coded_sample)
+        enc->bits_per_coded_sample = av_get_bits_per_sample(enc->codec_id);
+    if (!enc->bits_per_coded_sample) {
         av_log(s, AV_LOG_ERROR, "could not compute bits per sample\n");
         return -1;
     }
     if (!enc->block_align)
-        enc->block_align = (enc->bits_per_sample * enc->channels) >> 3;
+        enc->block_align = (enc->bits_per_coded_sample * enc->channels) >> 3;
 
-    put_be16(pb, enc->bits_per_sample); /* Sample size */
+    put_be16(pb, enc->bits_per_coded_sample); /* Sample size */
 
     sample_rate = av_dbl2ext((double)enc->sample_rate);
     put_buffer(pb, (uint8_t*)&sample_rate, sizeof(sample_rate));
@@ -257,7 +262,7 @@ static int aiff_write_trailer(AVFormatContext *s)
     AVCodecContext *enc = s->streams[0]->codec;
 
     /* Chunks sizes must be even */
-    offset_t file_size, end_size;
+    int64_t file_size, end_size;
     end_size = file_size = url_ftell(pb);
     if (file_size & 1) {
         put_byte(pb, 0);
@@ -267,15 +272,15 @@ static int aiff_write_trailer(AVFormatContext *s)
     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);
@@ -285,7 +290,7 @@ static int aiff_write_trailer(AVFormatContext *s)
 
     return 0;
 }
-#endif //CONFIG_MUXERS
+#endif /* CONFIG_AIFF_MUXER */
 
 static int aiff_probe(AVProbeData *p)
 {
@@ -304,11 +309,11 @@ static int aiff_read_header(AVFormatContext *s,
                             AVFormatParameters *ap)
 {
     int size, filesize;
-    offset_t offset = 0;
+    int64_t offset = 0;
     uint32_t tag;
     unsigned version = AIFF_C_VERSION1;
     ByteIOContext *pb = s->pb;
-    AVStream * st = s->streams[0];
+    AVStream * st;
 
     /* check FORM header */
     filesize = get_tag(pb, &tag);
@@ -337,57 +342,61 @@ 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;
-                if (offset > 0) // COMM is after SSND
-                    goto got_sound;
-                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;
-
-            default: /* Jump */
-                if (size & 1)   /* Always even aligned */
-                    size++;
-                url_fskip (pb, size);
+        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;
+            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(s, "title"    , size);
+            break;
+        case MKTAG('A', 'U', 'T', 'H'):     /* Author chunk */
+            get_meta(s, "author"   , size);
+            break;
+        case MKTAG('(', 'c', ')', ' '):     /* Copyright chunk */
+            get_meta(s, "copyright", size);
+            break;
+        case MKTAG('A', 'N', 'N', 'O'):     /* Annotation chunk */
+            get_meta(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->block_align)    /* 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);
         }
     }
 
-    /* End of loop and didn't get sound */
-    return AVERROR_INVALIDDATA;
+    if (!st->codec->block_align) {
+        av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
+        return -1;
+    }
 
 got_sound:
     /* Now positioned, get the sound data start and end */
@@ -427,35 +436,24 @@ 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)
-{
-    return pcm_read_seek(s, stream_index, timestamp, flags);
-}
-
-#ifdef CONFIG_AIFF_DEMUXER
+#if CONFIG_AIFF_DEMUXER
 AVInputFormat aiff_demuxer = {
     "aiff",
-    "Audio IFF",
+    NULL_IF_CONFIG_SMALL("Audio IFF"),
     0,
     aiff_probe,
     aiff_read_header,
     aiff_read_packet,
-    aiff_read_close,
-    aiff_read_seek,
-    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
+    NULL,
+    pcm_read_seek,
+    .codec_tag= (const AVCodecTag* const []){codec_aiff_tags, 0},
 };
 #endif
 
-#ifdef CONFIG_AIFF_MUXER
+#if CONFIG_AIFF_MUXER
 AVOutputFormat aiff_muxer = {
     "aiff",
-    "Audio IFF",
+    NULL_IF_CONFIG_SMALL("Audio IFF"),
     "audio/aiff",
     "aif,aiff,afc,aifc",
     sizeof(AIFFOutputContext),
@@ -464,6 +462,6 @@ AVOutputFormat aiff_muxer = {
     aiff_write_header,
     aiff_write_packet,
     aiff_write_trailer,
-    .codec_tag= (const AVCodecTag*[]){codec_aiff_tags, 0},
+    .codec_tag= (const AVCodecTag* const []){codec_aiff_tags, 0},
 };
 #endif