replacing MULH by asm for x86
[FFMpeg-mirror/ordered_chapters.git] / libavformat / amr.c
blobbac9fbe44fe36454fa2b9522be5049573ab61acc
1 /*
2 * amr file format
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.
26 #include "avformat.h"
28 static const unsigned char AMR_header [] = "#!AMR\n";
29 static const unsigned char AMRWB_header [] = "#!AMR-WB\n";
31 #ifdef CONFIG_MUXERS
32 static int amr_write_header(AVFormatContext *s)
34 ByteIOContext *pb = &s->pb;
35 AVCodecContext *enc = s->streams[0]->codec;
37 s->priv_data = NULL;
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 */
47 else
49 //This is an error!
51 put_flush_packet(pb);
52 return 0;
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);
59 return 0;
62 static int amr_write_trailer(AVFormatContext *s)
64 return 0;
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)
74 if (p->buf_size < 5)
75 return 0;
76 if(memcmp(p->buf,AMR_header,5)==0)
77 return AVPROBE_SCORE_MAX;
78 else
79 return 0;
82 /* amr input */
83 static int amr_read_header(AVFormatContext *s,
84 AVFormatParameters *ap)
86 ByteIOContext *pb = &s->pb;
87 AVStream *st;
88 uint8_t header[9];
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)
97 return -1;
99 st = av_new_stream(s, 0);
100 if (!st)
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;
111 else
113 st = av_new_stream(s, 0);
114 if (!st)
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;
126 return 0;
129 #define MAX_SIZE 32
131 static int amr_read_packet(AVFormatContext *s,
132 AVPacket *pkt)
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};
139 uint8_t toc, q, ft;
140 int read;
141 int size;
143 if (url_feof(&s->pb))
145 return AVERROR_IO;
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))
156 return AVERROR_IO;
158 pkt->stream_index = 0;
159 pkt->pos= url_ftell(&s->pb);
160 pkt->data[0]=toc;
162 read = get_buffer(&s->pb, pkt->data+1, size);
164 if (read != size)
166 av_free_packet(pkt);
167 return AVERROR_IO;
170 return 0;
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};
175 uint8_t toc, mode;
176 int read;
177 int size;
179 if (url_feof(&s->pb))
181 return AVERROR_IO;
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))
190 return AVERROR_IO;
193 pkt->stream_index = 0;
194 pkt->pos= url_ftell(&s->pb);
195 pkt->data[0]=toc;
197 read = get_buffer(&s->pb, pkt->data+1, size-1);
199 if (read != (size-1))
201 av_free_packet(pkt);
202 return AVERROR_IO;
205 return 0;
207 else
209 return AVERROR_IO;
213 static int amr_read_close(AVFormatContext *s)
215 return 0;
218 #ifdef CONFIG_AMR_DEMUXER
219 AVInputFormat amr_demuxer = {
220 "amr",
221 "3gpp amr file format",
222 0, /*priv_data_size*/
223 amr_probe,
224 amr_read_header,
225 amr_read_packet,
226 amr_read_close,
228 #endif
230 #ifdef CONFIG_AMR_MUXER
231 AVOutputFormat amr_muxer = {
232 "amr",
233 "3gpp amr file format",
234 "audio/amr",
235 "amr",
237 CODEC_ID_AMR_NB,
238 CODEC_ID_NONE,
239 amr_write_header,
240 amr_write_packet,
241 amr_write_trailer,
243 #endif