LCOV - code coverage report
Current view: top level - libavformat - amr.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 82 130 63.1 %
Date: 2017-12-16 01:21:47 Functions: 5 9 55.6 %

          Line data    Source code
       1             : /*
       2             :  * amr file format
       3             :  * Copyright (c) 2001 FFmpeg project
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /*
      23             : Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
      24             : 
      25             : Only mono files are supported.
      26             : 
      27             : */
      28             : 
      29             : #include "libavutil/channel_layout.h"
      30             : #include "avformat.h"
      31             : #include "internal.h"
      32             : 
      33             : typedef struct {
      34             :     uint64_t cumulated_size;
      35             :     uint64_t block_count;
      36             : } AMRContext;
      37             : 
      38             : static const char AMR_header[]   = "#!AMR\n";
      39             : static const char AMRWB_header[] = "#!AMR-WB\n";
      40             : 
      41             : static const uint8_t amrnb_packed_size[16] = {
      42             :     13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
      43             : };
      44             : static const uint8_t amrwb_packed_size[16] = {
      45             :     18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
      46             : };
      47             : 
      48             : #if CONFIG_AMR_MUXER
      49           0 : static int amr_write_header(AVFormatContext *s)
      50             : {
      51           0 :     AVIOContext    *pb  = s->pb;
      52           0 :     AVCodecParameters *par = s->streams[0]->codecpar;
      53             : 
      54           0 :     s->priv_data = NULL;
      55             : 
      56           0 :     if (par->codec_id == AV_CODEC_ID_AMR_NB) {
      57           0 :         avio_write(pb, AMR_header,   sizeof(AMR_header)   - 1); /* magic number */
      58           0 :     } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
      59           0 :         avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */
      60             :     } else {
      61           0 :         return -1;
      62             :     }
      63           0 :     avio_flush(pb);
      64           0 :     return 0;
      65             : }
      66             : 
      67           0 : static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
      68             : {
      69           0 :     avio_write(s->pb, pkt->data, pkt->size);
      70           0 :     return 0;
      71             : }
      72             : #endif /* CONFIG_AMR_MUXER */
      73             : 
      74        6130 : static int amr_probe(AVProbeData *p)
      75             : {
      76             :     // Only check for "#!AMR" which could be amr-wb, amr-nb.
      77             :     // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
      78             :     // "#!AMR-WB_MC1.0\n" (not supported)
      79             : 
      80        6130 :     if (!memcmp(p->buf, AMR_header, 5))
      81           8 :         return AVPROBE_SCORE_MAX;
      82             :     else
      83        6122 :         return 0;
      84             : }
      85             : 
      86             : /* amr input */
      87           8 : static int amr_read_header(AVFormatContext *s)
      88             : {
      89           8 :     AVIOContext *pb = s->pb;
      90             :     AVStream *st;
      91             :     uint8_t header[9];
      92             : 
      93           8 :     avio_read(pb, header, 6);
      94             : 
      95           8 :     st = avformat_new_stream(s, NULL);
      96           8 :     if (!st)
      97           0 :         return AVERROR(ENOMEM);
      98           8 :     if (memcmp(header, AMR_header, 6)) {
      99           0 :         avio_read(pb, header + 6, 3);
     100           0 :         if (memcmp(header, AMRWB_header, 9)) {
     101           0 :             return -1;
     102             :         }
     103             : 
     104           0 :         st->codecpar->codec_tag   = MKTAG('s', 'a', 'w', 'b');
     105           0 :         st->codecpar->codec_id    = AV_CODEC_ID_AMR_WB;
     106           0 :         st->codecpar->sample_rate = 16000;
     107             :     } else {
     108           8 :         st->codecpar->codec_tag   = MKTAG('s', 'a', 'm', 'r');
     109           8 :         st->codecpar->codec_id    = AV_CODEC_ID_AMR_NB;
     110           8 :         st->codecpar->sample_rate = 8000;
     111             :     }
     112           8 :     st->codecpar->channels   = 1;
     113           8 :     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     114           8 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     115           8 :     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
     116             : 
     117           8 :     return 0;
     118             : }
     119             : 
     120        2280 : static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
     121             : {
     122        2280 :     AVCodecParameters *par = s->streams[0]->codecpar;
     123        2280 :     int read, size = 0, toc, mode;
     124        2280 :     int64_t pos = avio_tell(s->pb);
     125        2280 :     AMRContext *amr = s->priv_data;
     126             : 
     127        2280 :     if (avio_feof(s->pb)) {
     128           0 :         return AVERROR_EOF;
     129             :     }
     130             : 
     131             :     // FIXME this is wrong, this should rather be in an AVParser
     132        2280 :     toc  = avio_r8(s->pb);
     133        2280 :     mode = (toc >> 3) & 0x0F;
     134             : 
     135        2280 :     if (par->codec_id == AV_CODEC_ID_AMR_NB) {
     136        2280 :         size = amrnb_packed_size[mode];
     137           0 :     } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
     138           0 :         size = amrwb_packed_size[mode];
     139             :     }
     140             : 
     141        2280 :     if (!size || av_new_packet(pkt, size))
     142           0 :         return AVERROR(EIO);
     143             : 
     144        2280 :     if (amr->cumulated_size < UINT64_MAX - size) {
     145        2280 :         amr->cumulated_size += size;
     146             :         /* Both AMR formats have 50 frames per second */
     147        2280 :         s->streams[0]->codecpar->bit_rate = amr->cumulated_size / ++amr->block_count * 8 * 50;
     148             :     }
     149             : 
     150        2280 :     pkt->stream_index = 0;
     151        2280 :     pkt->pos          = pos;
     152        2280 :     pkt->data[0]      = toc;
     153        2280 :     pkt->duration     = par->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
     154        2280 :     read              = avio_read(s->pb, pkt->data + 1, size - 1);
     155             : 
     156        2280 :     if (read != size - 1) {
     157           8 :         av_packet_unref(pkt);
     158           8 :         if (read < 0)
     159           8 :             return read;
     160           0 :         return AVERROR(EIO);
     161             :     }
     162             : 
     163        2272 :     return 0;
     164             : }
     165             : 
     166             : #if CONFIG_AMR_DEMUXER
     167             : AVInputFormat ff_amr_demuxer = {
     168             :     .name           = "amr",
     169             :     .long_name      = NULL_IF_CONFIG_SMALL("3GPP AMR"),
     170             :     .priv_data_size = sizeof(AMRContext),
     171             :     .read_probe     = amr_probe,
     172             :     .read_header    = amr_read_header,
     173             :     .read_packet    = amr_read_packet,
     174             :     .flags          = AVFMT_GENERIC_INDEX,
     175             : };
     176             : #endif
     177             : 
     178             : #if CONFIG_AMRNB_DEMUXER
     179        6130 : static int amrnb_probe(AVProbeData *p)
     180             : {
     181        6130 :     int mode, i = 0, valid = 0;
     182        6130 :     const uint8_t *b = p->buf;
     183             : 
     184    31826410 :     while (i < p->buf_size) {
     185    31814150 :         mode = b[i] >> 3 & 0x0F;
     186    37587509 :         if (mode < 9 && (b[i] & 0x4) == 0x4) {
     187     5773359 :             int last = mode;
     188     5773359 :             int size = amrnb_packed_size[mode];
     189    11668913 :             while (size--) {
     190     5895234 :                 if (b[++i] != last)
     191     5773039 :                     break;
     192      122195 :                 last = b[i];
     193             :             }
     194     5773359 :             if (size > 0) {
     195     5773032 :                 valid++;
     196     5773032 :                 i += size;
     197             :             }
     198             :         } else {
     199    26040791 :             valid = 0;
     200    26040791 :             i++;
     201             :         }
     202             :     }
     203        6130 :     if (valid > 100)
     204          14 :         return AVPROBE_SCORE_EXTENSION / 2 + 1;
     205        6116 :     return 0;
     206             : }
     207             : 
     208           0 : static int amrnb_read_header(AVFormatContext *s)
     209             : {
     210           0 :     AVStream *st = avformat_new_stream(s, NULL);
     211           0 :     if (!st)
     212           0 :         return AVERROR(ENOMEM);
     213           0 :     st->codecpar->codec_id       = AV_CODEC_ID_AMR_NB;
     214           0 :     st->codecpar->sample_rate    = 8000;
     215           0 :     st->codecpar->channels       = 1;
     216           0 :     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     217           0 :     st->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
     218           0 :     avpriv_set_pts_info(st, 64, 1, 8000);
     219             : 
     220           0 :     return 0;
     221             : }
     222             : 
     223             : AVInputFormat ff_amrnb_demuxer = {
     224             :     .name           = "amrnb",
     225             :     .long_name      = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
     226             :     .priv_data_size = sizeof(AMRContext),
     227             :     .read_probe     = amrnb_probe,
     228             :     .read_header    = amrnb_read_header,
     229             :     .read_packet    = amr_read_packet,
     230             :     .flags          = AVFMT_GENERIC_INDEX,
     231             : };
     232             : #endif
     233             : 
     234             : #if CONFIG_AMRWB_DEMUXER
     235        6130 : static int amrwb_probe(AVProbeData *p)
     236             : {
     237        6130 :     int mode, i = 0, valid = 0;
     238        6130 :     const uint8_t *b = p->buf;
     239             : 
     240    19324109 :     while (i < p->buf_size) {
     241    19311849 :         mode = b[i] >> 3 & 0x0F;
     242    22501535 :         if (mode < 10 && (b[i] & 0x4) == 0x4) {
     243     3189686 :             int last = mode;
     244     3189686 :             int size = amrwb_packed_size[mode];
     245     6443526 :             while (size--) {
     246     3253731 :                 if (b[++i] != last)
     247     3189577 :                     break;
     248       64154 :                 last = b[i];
     249             :             }
     250     3189686 :             if (size > 0) {
     251     3189550 :                 valid++;
     252     3189550 :                 i += size;
     253             :             }
     254             :         } else {
     255    16122163 :             valid = 0;
     256    16122163 :             i++;
     257             :         }
     258             :     }
     259        6130 :     if (valid > 100)
     260           3 :         return AVPROBE_SCORE_EXTENSION / 2 - 1;
     261        6127 :     return 0;
     262             : }
     263             : 
     264           0 : static int amrwb_read_header(AVFormatContext *s)
     265             : {
     266           0 :     AVStream *st = avformat_new_stream(s, NULL);
     267           0 :     if (!st)
     268           0 :         return AVERROR(ENOMEM);
     269           0 :     st->codecpar->codec_id       = AV_CODEC_ID_AMR_WB;
     270           0 :     st->codecpar->sample_rate    = 16000;
     271           0 :     st->codecpar->channels       = 1;
     272           0 :     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     273           0 :     st->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
     274           0 :     avpriv_set_pts_info(st, 64, 1, 16000);
     275             : 
     276           0 :     return 0;
     277             : }
     278             : 
     279             : AVInputFormat ff_amrwb_demuxer = {
     280             :     .name           = "amrwb",
     281             :     .long_name      = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
     282             :     .priv_data_size = sizeof(AMRContext),
     283             :     .read_probe     = amrwb_probe,
     284             :     .read_header    = amrwb_read_header,
     285             :     .read_packet    = amr_read_packet,
     286             :     .flags          = AVFMT_GENERIC_INDEX,
     287             : };
     288             : #endif
     289             : 
     290             : #if CONFIG_AMR_MUXER
     291             : AVOutputFormat ff_amr_muxer = {
     292             :     .name              = "amr",
     293             :     .long_name         = NULL_IF_CONFIG_SMALL("3GPP AMR"),
     294             :     .mime_type         = "audio/amr",
     295             :     .extensions        = "amr",
     296             :     .audio_codec       = AV_CODEC_ID_AMR_NB,
     297             :     .video_codec       = AV_CODEC_ID_NONE,
     298             :     .write_header      = amr_write_header,
     299             :     .write_packet      = amr_write_packet,
     300             :     .flags             = AVFMT_NOTIMESTAMPS,
     301             : };
     302             : #endif

Generated by: LCOV version 1.13