]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/aiffdec.c
mpjpeg: Use proper CR/LF in multipart headers
[ffmpeg] / libavformat / aiffdec.c
index 0e815421a753fbf669ade285672c608778f2a032..481a92d351c8cf124c88fd57179e8b649e261ffa 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include "libavutil/intfloat_readwrite.h"
+#include "libavutil/mathematics.h"
 #include "libavutil/dict.h"
 #include "avformat.h"
+#include "internal.h"
 #include "pcm.h"
 #include "aiff.h"
 
 #define AIFF                    0
 #define AIFF_C_VERSION1         0xA2805140
 
-typedef struct {
+typedef struct AIFFInputContext {
     int64_t data_end;
+    int block_duration;
 } AIFFInputContext;
 
-static enum CodecID aiff_codec_get_id(int bps)
+static enum AVCodecID aiff_codec_get_id(int bps)
 {
     if (bps <= 8)
-        return CODEC_ID_PCM_S8;
+        return AV_CODEC_ID_PCM_S8;
     if (bps <= 16)
-        return CODEC_ID_PCM_S16BE;
+        return AV_CODEC_ID_PCM_S16BE;
     if (bps <= 24)
-        return CODEC_ID_PCM_S24BE;
+        return AV_CODEC_ID_PCM_S24BE;
     if (bps <= 32)
-        return CODEC_ID_PCM_S32BE;
+        return AV_CODEC_ID_PCM_S32BE;
 
     /* bigger than 32 isn't allowed  */
-    return CODEC_ID_NONE;
+    return AV_CODEC_ID_NONE;
 }
 
 /* returns the size of the found tag */
@@ -84,72 +86,83 @@ static void get_meta(AVFormatContext *s, const char *key, int size)
 }
 
 /* Returns the number of sound data frames or negative on error */
-static unsigned int get_aiff_header(AVIOContext *pb, AVCodecContext *codec,
-                             int size, unsigned version)
+static unsigned int get_aiff_header(AVFormatContext *s, int size,
+                                    unsigned version)
 {
-    AVExtFloat ext;
+    AVIOContext *pb        = s->pb;
+    AVCodecParameters *par = s->streams[0]->codecpar;
+    AIFFInputContext *aiff = s->priv_data;
+    int exp;
+    uint64_t val;
     double sample_rate;
     unsigned int num_frames;
 
     if (size & 1)
         size++;
-    codec->codec_type = AVMEDIA_TYPE_AUDIO;
-    codec->channels = avio_rb16(pb);
+    par->codec_type = AVMEDIA_TYPE_AUDIO;
+    par->channels = avio_rb16(pb);
     num_frames = avio_rb32(pb);
-    codec->bits_per_coded_sample = avio_rb16(pb);
+    par->bits_per_coded_sample = avio_rb16(pb);
 
-    avio_read(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;
+    exp = avio_rb16(pb);
+    val = avio_rb64(pb);
+    sample_rate = ldexp(val, exp - 16383 - 63);
+    par->sample_rate = sample_rate;
     size -= 18;
 
-    /* Got an AIFF-C? */
+    /* get codec id for AIFF-C */
     if (version == AIFF_C_VERSION1) {
-        codec->codec_tag = avio_rl32(pb);
-        codec->codec_id  = ff_codec_get_id(ff_codec_aiff_tags, codec->codec_tag);
+        par->codec_tag = avio_rl32(pb);
+        par->codec_id  = ff_codec_get_id(ff_codec_aiff_tags, par->codec_tag);
+        size -= 4;
+    }
 
-        switch (codec->codec_id) {
-        case CODEC_ID_PCM_S16BE:
-            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 (version != AIFF_C_VERSION1 || par->codec_id == AV_CODEC_ID_PCM_S16BE) {
+        par->codec_id = aiff_codec_get_id(par->bits_per_coded_sample);
+        par->bits_per_coded_sample = av_get_bits_per_sample(par->codec_id);
+        aiff->block_duration = 1;
+    } else {
+        switch (par->codec_id) {
+        case AV_CODEC_ID_PCM_F32BE:
+        case AV_CODEC_ID_PCM_F64BE:
+        case AV_CODEC_ID_PCM_S16LE:
+        case AV_CODEC_ID_PCM_ALAW:
+        case AV_CODEC_ID_PCM_MULAW:
+            aiff->block_duration = 1;
             break;
-        case CODEC_ID_ADPCM_IMA_QT:
-            codec->block_align = 34*codec->channels;
-            codec->frame_size = 64;
+        case AV_CODEC_ID_ADPCM_IMA_QT:
+            par->block_align = 34 * par->channels;
             break;
-        case CODEC_ID_MACE3:
-            codec->block_align = 2*codec->channels;
-            codec->frame_size = 6;
+        case AV_CODEC_ID_MACE3:
+            par->block_align = 2 * par->channels;
             break;
-        case CODEC_ID_MACE6:
-            codec->block_align = 1*codec->channels;
-            codec->frame_size = 6;
+        case AV_CODEC_ID_ADPCM_G722:
+        case AV_CODEC_ID_MACE6:
+            par->block_align = 1 * par->channels;
             break;
-        case CODEC_ID_GSM:
-            codec->block_align = 33;
-            codec->frame_size = 160;
+        case AV_CODEC_ID_GSM:
+            par->block_align = 33;
             break;
-        case CODEC_ID_QCELP:
-            codec->block_align = 35;
-            codec->frame_size= 160;
+        case AV_CODEC_ID_QCELP:
+            par->block_align = 35;
             break;
         default:
             break;
         }
-        size -= 4;
-    } else {
-        /* Need the codec type */
-        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 (par->block_align > 0)
+            aiff->block_duration = av_get_audio_frame_duration2(par,
+                                                                par->block_align);
     }
 
     /* 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_coded_sample * codec->channels) >> 3;
+    if (!par->block_align)
+        par->block_align = (par->bits_per_coded_sample * par->channels) >> 3;
 
-    codec->bit_rate = (codec->frame_size ? codec->sample_rate/codec->frame_size :
-                       codec->sample_rate) * (codec->block_align << 3);
+    if (aiff->block_duration) {
+        par->bit_rate = par->sample_rate * (par->block_align << 3) /
+                        aiff->block_duration;
+    }
 
     /* Chunk is over */
     if (size)
@@ -171,8 +184,7 @@ static int aiff_probe(AVProbeData *p)
 }
 
 /* aiff input */
-static int aiff_read_header(AVFormatContext *s,
-                            AVFormatParameters *ap)
+static int aiff_read_header(AVFormatContext *s)
 {
     int size, filesize;
     int64_t offset = 0;
@@ -196,7 +208,7 @@ static int aiff_read_header(AVFormatContext *s,
 
     filesize -= 4;
 
-    st = av_new_stream(s, 0);
+    st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
 
@@ -211,7 +223,7 @@ static int aiff_read_header(AVFormatContext *s,
         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);
+            st->nb_frames = get_aiff_header(s, size, version);
             if (st->nb_frames < 0)
                 return st->nb_frames;
             if (offset > 0) // COMM is after SSND
@@ -237,9 +249,9 @@ static int aiff_read_header(AVFormatContext *s,
             offset = avio_rb32(pb);      /* Offset of sound data */
             avio_rb32(pb);               /* BlockSize... don't care */
             offset += avio_tell(pb);    /* Compute absolute data offset */
-            if (st->codec->block_align)    /* Assume COMM already parsed */
+            if (st->codecpar->block_align)    /* Assume COMM already parsed */
                 goto got_sound;
-            if (!pb->seekable) {
+            if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
                 av_log(s, AV_LOG_ERROR, "file is not seekable\n");
                 return -1;
             }
@@ -248,33 +260,33 @@ static int aiff_read_header(AVFormatContext *s,
         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)
+            st->codecpar->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
+            if (!st->codecpar->extradata)
                 return AVERROR(ENOMEM);
-            st->codec->extradata_size = size;
-            avio_read(pb, st->codec->extradata, size);
+            st->codecpar->extradata_size = size;
+            avio_read(pb, st->codecpar->extradata, size);
             break;
         default: /* Jump */
-            if (size & 1)   /* Always even aligned */
-                size++;
             avio_skip(pb, size);
         }
+
+        /* Skip required padding byte for odd-sized chunks. */
+        if (size & 1) {
+            filesize--;
+            avio_skip(pb, 1);
+        }
     }
 
-    if (!st->codec->block_align) {
-        av_log(s, AV_LOG_ERROR, "could not find COMM tag\n");
+got_sound:
+    if (!st->codecpar->block_align) {
+        av_log(s, AV_LOG_ERROR, "could not find COMM tag or invalid block_align value\n");
         return -1;
     }
 
-got_sound:
     /* Now positioned, get the sound data start and end */
-    if (st->nb_frames)
-        s->file_size = st->nb_frames * st->codec->block_align;
-
-    av_set_pts_info(st, 64, 1, st->codec->sample_rate);
+    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
     st->start_time = 0;
-    st->duration = st->codec->frame_size ?
-        st->nb_frames * st->codec->frame_size : st->nb_frames;
+    st->duration = st->nb_frames * aiff->block_duration;
 
     /* Position the stream at the first block */
     avio_seek(pb, offset, SEEK_SET);
@@ -298,10 +310,10 @@ static int aiff_read_packet(AVFormatContext *s,
         return AVERROR_EOF;
 
     /* Now for that packet */
-    if (st->codec->block_align >= 33) // GSM, QCLP, IMA4
-        size = st->codec->block_align;
+    if (st->codecpar->block_align >= 33) // GSM, QCLP, IMA4
+        size = st->codecpar->block_align;
     else
-        size = (MAX_SIZE / st->codec->block_align) * st->codec->block_align;
+        size = (MAX_SIZE / st->codecpar->block_align) * st->codecpar->block_align;
     size = FFMIN(max_size, size);
     res = av_get_packet(s->pb, pkt, size);
     if (res < 0)
@@ -309,17 +321,17 @@ static int aiff_read_packet(AVFormatContext *s,
 
     /* Only one stream in an AIFF file */
     pkt->stream_index = 0;
+    pkt->duration     = (res / st->codecpar->block_align) * aiff->block_duration;
     return 0;
 }
 
 AVInputFormat ff_aiff_demuxer = {
-    "aiff",
-    NULL_IF_CONFIG_SMALL("Audio IFF"),
-    sizeof(AIFFInputContext),
-    aiff_probe,
-    aiff_read_header,
-    aiff_read_packet,
-    NULL,
-    pcm_read_seek,
-    .codec_tag= (const AVCodecTag* const []){ff_codec_aiff_tags, 0},
+    .name           = "aiff",
+    .long_name      = NULL_IF_CONFIG_SMALL("Audio IFF"),
+    .priv_data_size = sizeof(AIFFInputContext),
+    .read_probe     = aiff_probe,
+    .read_header    = aiff_read_header,
+    .read_packet    = aiff_read_packet,
+    .read_seek      = ff_pcm_read_seek,
+    .codec_tag      = (const AVCodecTag* const []){ ff_codec_aiff_tags, 0 },
 };