libavformat/amr.c
115329f1
 /*
067cbf31
  * amr file format
  * Copyright (c) 2001 ffmpeg project
  *
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
 
 */
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
067cbf31
 
191e8ca7
 static const char AMR_header [] = "#!AMR\n";
 static const char AMRWB_header [] = "#!AMR-WB\n";
067cbf31
 
b250f9c6
 #if CONFIG_AMR_MUXER
067cbf31
 static int amr_write_header(AVFormatContext *s)
 {
471fe57e
     AVIOContext *pb = s->pb;
01f4895c
     AVCodecContext *enc = s->streams[0]->codec;
067cbf31
 
     s->priv_data = NULL;
 
d663a1fd
     if (enc->codec_id == CODEC_ID_AMR_NB)
     {
a1b79792
         avio_write(pb, AMR_header,   sizeof(AMR_header)   - 1); /* magic number */
d663a1fd
     }
     else if(enc->codec_id == CODEC_ID_AMR_WB)
     {
a1b79792
         avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */
d663a1fd
     }
     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);
b7f2fdde
     avio_flush(s->pb);
067cbf31
     return 0;
 }
8212568a
 #endif /* CONFIG_AMR_MUXER */
067cbf31
 
 static int amr_probe(AVProbeData *p)
 {
115329f1
     //Only check for "#!AMR" which could be amr-wb, amr-nb.
     //This will also trigger multichannel files: "#!AMR_MC1.0\n" and
d663a1fd
     //"#!AMR-WB_MC1.0\n" (not supported)
 
     if(memcmp(p->buf,AMR_header,5)==0)
067cbf31
         return AVPROBE_SCORE_MAX;
     else
         return 0;
 }
 
 /* amr input */
 static int amr_read_header(AVFormatContext *s,
                            AVFormatParameters *ap)
 {
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);
b4963892
     }
067cbf31
     if(memcmp(header,AMR_header,6)!=0)
     {
e63a3628
         avio_read(pb, header+6, 3);
d663a1fd
         if(memcmp(header,AMRWB_header,9)!=0)
         {
             return -1;
         }
115329f1
 
01f4895c
         st->codec->codec_tag = MKTAG('s', 'a', 'w', 'b');
         st->codec->codec_id = CODEC_ID_AMR_WB;
         st->codec->sample_rate = 16000;
8bb90c53
         st->codec->frame_size = 320;
d663a1fd
     }
     else
     {
01f4895c
         st->codec->codec_tag = MKTAG('s', 'a', 'm', 'r');
         st->codec->codec_id = CODEC_ID_AMR_NB;
         st->codec->sample_rate = 8000;
8bb90c53
         st->codec->frame_size = 160;
067cbf31
     }
d84fd5d1
     st->codec->channels = 1;
72415b2a
     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
c3f9ebf7
     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
067cbf31
 
     return 0;
 }
 
 static int amr_read_packet(AVFormatContext *s,
                           AVPacket *pkt)
 {
01f4895c
     AVCodecContext *enc = s->streams[0]->codec;
f015e411
     int read, size = 0, toc, mode;
026fa81d
     int64_t pos = avio_tell(s->pb);
067cbf31
 
899681cd
     if (url_feof(s->pb))
d663a1fd
     {
6f3e0b21
         return AVERROR(EIO);
b1bf48aa
     }
115329f1
 
b4963892
 //FIXME this is wrong, this should rather be in a AVParset
e63a3628
     toc=avio_r8(s->pb);
b1bf48aa
     mode = (toc >> 3) & 0x0F;
115329f1
 
b1bf48aa
     if (enc->codec_id == CODEC_ID_AMR_NB)
     {
         static const uint8_t packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
115329f1
 
b1bf48aa
         size=packed_size[mode]+1;
d663a1fd
     }
     else if(enc->codec_id == CODEC_ID_AMR_WB)
     {
         static uint8_t packed_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
115329f1
 
b1bf48aa
         size=packed_size[mode];
d663a1fd
     }
     else
067cbf31
     {
effdc8ef
         assert(0);
067cbf31
     }
b1bf48aa
 
     if ( (size==0) || av_new_packet(pkt, size))
     {
6f3e0b21
         return AVERROR(EIO);
b1bf48aa
     }
 
2890cba8
     /* Both AMR formats have 50 frames per second */
     s->streams[0]->codec->bit_rate = size*8*50;
 
b1bf48aa
     pkt->stream_index = 0;
026fa81d
     pkt->pos = pos;
b1bf48aa
     pkt->data[0]=toc;
ce2f3c8b
     pkt->duration= enc->codec_id == CODEC_ID_AMR_NB ? 160 : 320;
e63a3628
     read = avio_read(s->pb, pkt->data+1, size-1);
b1bf48aa
 
     if (read != size-1)
     {
         av_free_packet(pkt);
6f3e0b21
         return AVERROR(EIO);
b1bf48aa
     }
 
effdc8ef
     return 0;
067cbf31
 }
 
b250f9c6
 #if CONFIG_AMR_DEMUXER
66355be3
 AVInputFormat ff_amr_demuxer = {
dfc2c4d9
     .name           = "amr",
     .long_name      = NULL_IF_CONFIG_SMALL("3GPP AMR file format"),
     .read_probe     = amr_probe,
     .read_header    = amr_read_header,
     .read_packet    = amr_read_packet,
af79dd36
     .flags = AVFMT_GENERIC_INDEX,
067cbf31
 };
ff70e601
 #endif
067cbf31
 
b250f9c6
 #if CONFIG_AMR_MUXER
66355be3
 AVOutputFormat ff_amr_muxer = {
dfc2c4d9
     .name              = "amr",
     .long_name         = NULL_IF_CONFIG_SMALL("3GPP AMR file format"),
     .mime_type         = "audio/amr",
     .extensions        = "amr",
     .audio_codec       = CODEC_ID_AMR_NB,
     .video_codec       = CODEC_ID_NONE,
     .write_header      = amr_write_header,
     .write_packet      = amr_write_packet,
067cbf31
 };
0e6c947d
 #endif