libavformat/amr.c
115329f1
 /*
067cbf31
  * amr file format
41ed7ab4
  * Copyright (c) 2001 FFmpeg project
067cbf31
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
067cbf31
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
067cbf31
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
067cbf31
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
067cbf31
  */
 
 /*
 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
 
d663a1fd
 Only mono files are supported.
067cbf31
 
 */
5595368b
 
 #include "libavutil/channel_layout.h"
067cbf31
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
067cbf31
 
c43222f4
 typedef struct {
     uint64_t cumulated_size;
     uint64_t block_count;
 } AMRContext;
 
5f26d4d4
 static const char AMR_header[]   = "#!AMR\n";
 static const char AMRWB_header[] = "#!AMR-WB\n";
067cbf31
 
e06bdc3c
 static const uint8_t amrnb_packed_size[16] = {
     13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
 };
 static const uint8_t amrwb_packed_size[16] = {
     18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
 };
 
b250f9c6
 #if CONFIG_AMR_MUXER
067cbf31
 static int amr_write_header(AVFormatContext *s)
 {
5f26d4d4
     AVIOContext    *pb  = s->pb;
9200514a
     AVCodecParameters *par = s->streams[0]->codecpar;
067cbf31
 
     s->priv_data = NULL;
 
9200514a
     if (par->codec_id == AV_CODEC_ID_AMR_NB) {
a1b79792
         avio_write(pb, AMR_header,   sizeof(AMR_header)   - 1); /* magic number */
9200514a
     } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
a1b79792
         avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */
5f26d4d4
     } else {
effdc8ef
         return -1;
067cbf31
     }
b7f2fdde
     avio_flush(pb);
067cbf31
     return 0;
 }
 
e928649b
 static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
067cbf31
 {
e9eb8d0b
     avio_write(s->pb, pkt->data, pkt->size);
067cbf31
     return 0;
 }
8212568a
 #endif /* CONFIG_AMR_MUXER */
067cbf31
 
 static int amr_probe(AVProbeData *p)
 {
5f26d4d4
     // Only check for "#!AMR" which could be amr-wb, amr-nb.
     // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
     // "#!AMR-WB_MC1.0\n" (not supported)
d663a1fd
 
5f26d4d4
     if (!memcmp(p->buf, AMR_header, 5))
067cbf31
         return AVPROBE_SCORE_MAX;
     else
         return 0;
 }
 
 /* amr input */
6e9651d1
 static int amr_read_header(AVFormatContext *s)
067cbf31
 {
471fe57e
     AVIOContext *pb = s->pb;
067cbf31
     AVStream *st;
d663a1fd
     uint8_t header[9];
067cbf31
 
e63a3628
     avio_read(pb, header, 6);
067cbf31
 
3b3bbdd3
     st = avformat_new_stream(s, NULL);
b4963892
     if (!st)
769e10f0
         return AVERROR(ENOMEM);
5f26d4d4
     if (memcmp(header, AMR_header, 6)) {
         avio_read(pb, header + 6, 3);
         if (memcmp(header, AMRWB_header, 9)) {
d663a1fd
             return -1;
         }
115329f1
 
9200514a
         st->codecpar->codec_tag   = MKTAG('s', 'a', 'w', 'b');
         st->codecpar->codec_id    = AV_CODEC_ID_AMR_WB;
         st->codecpar->sample_rate = 16000;
5f26d4d4
     } else {
9200514a
         st->codecpar->codec_tag   = MKTAG('s', 'a', 'm', 'r');
         st->codecpar->codec_id    = AV_CODEC_ID_AMR_NB;
         st->codecpar->sample_rate = 8000;
067cbf31
     }
9200514a
     st->codecpar->channels   = 1;
     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
067cbf31
 
     return 0;
 }
 
5f26d4d4
 static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
067cbf31
 {
9200514a
     AVCodecParameters *par = s->streams[0]->codecpar;
f015e411
     int read, size = 0, toc, mode;
026fa81d
     int64_t pos = avio_tell(s->pb);
c43222f4
     AMRContext *amr = s->priv_data;
067cbf31
 
d34ec64a
     if (avio_feof(s->pb)) {
76dd87c9
         return AVERROR_EOF;
b1bf48aa
     }
115329f1
 
41ed7ab4
     // FIXME this is wrong, this should rather be in an AVParser
5f26d4d4
     toc  = avio_r8(s->pb);
b1bf48aa
     mode = (toc >> 3) & 0x0F;
115329f1
 
9200514a
     if (par->codec_id == AV_CODEC_ID_AMR_NB) {
e06bdc3c
         size = amrnb_packed_size[mode];
9200514a
     } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
e06bdc3c
         size = amrwb_packed_size[mode];
067cbf31
     }
b1bf48aa
 
5f26d4d4
     if (!size || av_new_packet(pkt, size))
6f3e0b21
         return AVERROR(EIO);
b1bf48aa
 
c43222f4
     if (amr->cumulated_size < UINT64_MAX - size) {
         amr->cumulated_size += size;
         /* Both AMR formats have 50 frames per second */
6f69f7a8
         s->streams[0]->codecpar->bit_rate = amr->cumulated_size / ++amr->block_count * 8 * 50;
c43222f4
     }
2890cba8
 
b1bf48aa
     pkt->stream_index = 0;
5f26d4d4
     pkt->pos          = pos;
     pkt->data[0]      = toc;
9200514a
     pkt->duration     = par->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
5f26d4d4
     read              = avio_read(s->pb, pkt->data + 1, size - 1);
b1bf48aa
 
5f26d4d4
     if (read != size - 1) {
ce70f28a
         av_packet_unref(pkt);
76dd87c9
         if (read < 0)
             return read;
6f3e0b21
         return AVERROR(EIO);
b1bf48aa
     }
 
effdc8ef
     return 0;
067cbf31
 }
 
b250f9c6
 #if CONFIG_AMR_DEMUXER
66355be3
 AVInputFormat ff_amr_demuxer = {
dfc2c4d9
     .name           = "amr",
6774247a
     .long_name      = NULL_IF_CONFIG_SMALL("3GPP AMR"),
c43222f4
     .priv_data_size = sizeof(AMRContext),
dfc2c4d9
     .read_probe     = amr_probe,
     .read_header    = amr_read_header,
     .read_packet    = amr_read_packet,
20234a4b
     .flags          = AVFMT_GENERIC_INDEX,
067cbf31
 };
ff70e601
 #endif
067cbf31
 
e06bdc3c
 #if CONFIG_AMRNB_DEMUXER
 static int amrnb_probe(AVProbeData *p)
 {
233f52fd
     int mode, i = 0, valid = 0, invalid = 0;
e06bdc3c
     const uint8_t *b = p->buf;
 
     while (i < p->buf_size) {
         mode = b[i] >> 3 & 0x0F;
         if (mode < 9 && (b[i] & 0x4) == 0x4) {
7becc703
             int last = b[i];
e06bdc3c
             int size = amrnb_packed_size[mode];
             while (size--) {
                 if (b[++i] != last)
                     break;
             }
             if (size > 0) {
                 valid++;
                 i += size;
             }
         } else {
             valid = 0;
233f52fd
             invalid++;
e06bdc3c
             i++;
         }
     }
40b7e607
     if (valid > 100 && valid >> 4 > invalid)
e06bdc3c
         return AVPROBE_SCORE_EXTENSION / 2 + 1;
     return 0;
 }
 
 static int amrnb_read_header(AVFormatContext *s)
 {
     AVStream *st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
     st->codecpar->codec_id       = AV_CODEC_ID_AMR_NB;
     st->codecpar->sample_rate    = 8000;
     st->codecpar->channels       = 1;
     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     st->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
     avpriv_set_pts_info(st, 64, 1, 8000);
 
     return 0;
 }
 
 AVInputFormat ff_amrnb_demuxer = {
     .name           = "amrnb",
     .long_name      = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
     .priv_data_size = sizeof(AMRContext),
     .read_probe     = amrnb_probe,
     .read_header    = amrnb_read_header,
     .read_packet    = amr_read_packet,
     .flags          = AVFMT_GENERIC_INDEX,
 };
 #endif
 
 #if CONFIG_AMRWB_DEMUXER
 static int amrwb_probe(AVProbeData *p)
 {
233f52fd
     int mode, i = 0, valid = 0, invalid = 0;
e06bdc3c
     const uint8_t *b = p->buf;
 
     while (i < p->buf_size) {
         mode = b[i] >> 3 & 0x0F;
         if (mode < 10 && (b[i] & 0x4) == 0x4) {
7becc703
             int last = b[i];
e06bdc3c
             int size = amrwb_packed_size[mode];
             while (size--) {
                 if (b[++i] != last)
                     break;
             }
             if (size > 0) {
                 valid++;
                 i += size;
             }
         } else {
             valid = 0;
233f52fd
             invalid++;
e06bdc3c
             i++;
         }
     }
40b7e607
     if (valid > 100 && valid >> 4 > invalid)
         return AVPROBE_SCORE_EXTENSION / 2 + 1;
e06bdc3c
     return 0;
 }
 
 static int amrwb_read_header(AVFormatContext *s)
 {
     AVStream *st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
     st->codecpar->codec_id       = AV_CODEC_ID_AMR_WB;
     st->codecpar->sample_rate    = 16000;
     st->codecpar->channels       = 1;
     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     st->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
     avpriv_set_pts_info(st, 64, 1, 16000);
 
     return 0;
 }
 
 AVInputFormat ff_amrwb_demuxer = {
     .name           = "amrwb",
     .long_name      = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
     .priv_data_size = sizeof(AMRContext),
     .read_probe     = amrwb_probe,
     .read_header    = amrwb_read_header,
     .read_packet    = amr_read_packet,
     .flags          = AVFMT_GENERIC_INDEX,
 };
 #endif
 
b250f9c6
 #if CONFIG_AMR_MUXER
66355be3
 AVOutputFormat ff_amr_muxer = {
dfc2c4d9
     .name              = "amr",
6774247a
     .long_name         = NULL_IF_CONFIG_SMALL("3GPP AMR"),
dfc2c4d9
     .mime_type         = "audio/amr",
     .extensions        = "amr",
36ef5369
     .audio_codec       = AV_CODEC_ID_AMR_NB,
     .video_codec       = AV_CODEC_ID_NONE,
dfc2c4d9
     .write_header      = amr_write_header,
     .write_packet      = amr_write_packet,
f792d3cb
     .flags             = AVFMT_NOTIMESTAMPS,
067cbf31
 };
0e6c947d
 #endif