3 * Copyright (c) 2001 ffmpeg project
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
23 Only mono files are supported.
28 static const unsigned char AMR_header
[] = "#!AMR\n";
29 static const unsigned char AMRWB_header
[] = "#!AMR-WB\n";
32 static int amr_write_header(AVFormatContext
*s
)
34 ByteIOContext
*pb
= &s
->pb
;
35 AVCodecContext
*enc
= s
->streams
[0]->codec
;
39 if (enc
->codec_id
== CODEC_ID_AMR_NB
)
41 put_tag(pb
, AMR_header
); /* magic number */
43 else if(enc
->codec_id
== CODEC_ID_AMR_WB
)
45 put_tag(pb
, AMRWB_header
); /* magic number */
55 static int amr_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
57 put_buffer(&s
->pb
, pkt
->data
, pkt
->size
);
58 put_flush_packet(&s
->pb
);
62 static int amr_write_trailer(AVFormatContext
*s
)
66 #endif /* CONFIG_MUXERS */
68 static int amr_probe(AVProbeData
*p
)
70 //Only check for "#!AMR" which could be amr-wb, amr-nb.
71 //This will also trigger multichannel files: "#!AMR_MC1.0\n" and
72 //"#!AMR-WB_MC1.0\n" (not supported)
76 if(memcmp(p
->buf
,AMR_header
,5)==0)
77 return AVPROBE_SCORE_MAX
;
83 static int amr_read_header(AVFormatContext
*s
,
84 AVFormatParameters
*ap
)
86 ByteIOContext
*pb
= &s
->pb
;
90 get_buffer(pb
, header
, 6);
92 if(memcmp(header
,AMR_header
,6)!=0)
94 get_buffer(pb
, header
+6, 3);
95 if(memcmp(header
,AMRWB_header
,9)!=0)
99 st
= av_new_stream(s
, 0);
102 return AVERROR_NOMEM
;
105 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
106 st
->codec
->codec_tag
= MKTAG('s', 'a', 'w', 'b');
107 st
->codec
->codec_id
= CODEC_ID_AMR_WB
;
108 st
->codec
->channels
= 1;
109 st
->codec
->sample_rate
= 16000;
113 st
= av_new_stream(s
, 0);
116 return AVERROR_NOMEM
;
119 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
120 st
->codec
->codec_tag
= MKTAG('s', 'a', 'm', 'r');
121 st
->codec
->codec_id
= CODEC_ID_AMR_NB
;
122 st
->codec
->channels
= 1;
123 st
->codec
->sample_rate
= 8000;
131 static int amr_read_packet(AVFormatContext
*s
,
134 AVCodecContext
*enc
= s
->streams
[0]->codec
;
136 if (enc
->codec_id
== CODEC_ID_AMR_NB
)
138 static const uint8_t packed_size
[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
143 if (url_feof(&s
->pb
))
148 toc
=get_byte(&s
->pb
);
149 q
= (toc
>> 2) & 0x01;
150 ft
= (toc
>> 3) & 0x0F;
152 size
=packed_size
[ft
];
154 if (av_new_packet(pkt
, size
+1))
158 pkt
->stream_index
= 0;
159 pkt
->pos
= url_ftell(&s
->pb
);
162 read
= get_buffer(&s
->pb
, pkt
->data
+1, size
);
172 else if(enc
->codec_id
== CODEC_ID_AMR_WB
)
174 static uint8_t packed_size
[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
179 if (url_feof(&s
->pb
))
184 toc
=get_byte(&s
->pb
);
185 mode
= (uint8_t)((toc
>> 3) & 0x0F);
186 size
= packed_size
[mode
];
188 if ( (size
==0) || av_new_packet(pkt
, size
))
193 pkt
->stream_index
= 0;
194 pkt
->pos
= url_ftell(&s
->pb
);
197 read
= get_buffer(&s
->pb
, pkt
->data
+1, size
-1);
199 if (read
!= (size
-1))
213 static int amr_read_close(AVFormatContext
*s
)
218 #ifdef CONFIG_AMR_DEMUXER
219 AVInputFormat amr_demuxer
= {
221 "3gpp amr file format",
222 0, /*priv_data_size*/
230 #ifdef CONFIG_AMR_MUXER
231 AVOutputFormat amr_muxer
= {
233 "3gpp amr file format",