You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
8.3KB

  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. Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
  23. Only mono files are supported.
  24. */
  25. #include "libavutil/channel_layout.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "rawenc.h"
  29. typedef struct {
  30. uint64_t cumulated_size;
  31. uint64_t block_count;
  32. } AMRContext;
  33. static const char AMR_header[] = "#!AMR\n";
  34. static const char AMRWB_header[] = "#!AMR-WB\n";
  35. static const uint8_t amrnb_packed_size[16] = {
  36. 13, 14, 16, 18, 20, 21, 27, 32, 6, 1, 1, 1, 1, 1, 1, 1
  37. };
  38. static const uint8_t amrwb_packed_size[16] = {
  39. 18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 1, 1, 1, 1, 1, 1
  40. };
  41. #if CONFIG_AMR_MUXER
  42. static int amr_write_header(AVFormatContext *s)
  43. {
  44. AVIOContext *pb = s->pb;
  45. AVCodecParameters *par = s->streams[0]->codecpar;
  46. s->priv_data = NULL;
  47. if (par->codec_id == AV_CODEC_ID_AMR_NB) {
  48. avio_write(pb, AMR_header, sizeof(AMR_header) - 1); /* magic number */
  49. } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
  50. avio_write(pb, AMRWB_header, sizeof(AMRWB_header) - 1); /* magic number */
  51. } else {
  52. return -1;
  53. }
  54. return 0;
  55. }
  56. #endif /* CONFIG_AMR_MUXER */
  57. static int amr_probe(const AVProbeData *p)
  58. {
  59. // Only check for "#!AMR" which could be amr-wb, amr-nb.
  60. // This will also trigger multichannel files: "#!AMR_MC1.0\n" and
  61. // "#!AMR-WB_MC1.0\n" (not supported)
  62. if (!memcmp(p->buf, AMR_header, 5))
  63. return AVPROBE_SCORE_MAX;
  64. else
  65. return 0;
  66. }
  67. /* amr input */
  68. static int amr_read_header(AVFormatContext *s)
  69. {
  70. AVIOContext *pb = s->pb;
  71. AVStream *st;
  72. uint8_t header[9];
  73. if (avio_read(pb, header, 6) != 6)
  74. return AVERROR_INVALIDDATA;
  75. st = avformat_new_stream(s, NULL);
  76. if (!st)
  77. return AVERROR(ENOMEM);
  78. if (memcmp(header, AMR_header, 6)) {
  79. if (avio_read(pb, header + 6, 3) != 3)
  80. return AVERROR_INVALIDDATA;
  81. if (memcmp(header, AMRWB_header, 9)) {
  82. return -1;
  83. }
  84. st->codecpar->codec_tag = MKTAG('s', 'a', 'w', 'b');
  85. st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
  86. st->codecpar->sample_rate = 16000;
  87. } else {
  88. st->codecpar->codec_tag = MKTAG('s', 'a', 'm', 'r');
  89. st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
  90. st->codecpar->sample_rate = 8000;
  91. }
  92. st->codecpar->channels = 1;
  93. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  94. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  95. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  96. return 0;
  97. }
  98. static int amr_read_packet(AVFormatContext *s, AVPacket *pkt)
  99. {
  100. AVCodecParameters *par = s->streams[0]->codecpar;
  101. int read, size = 0, toc, mode;
  102. int64_t pos = avio_tell(s->pb);
  103. AMRContext *amr = s->priv_data;
  104. if (avio_feof(s->pb)) {
  105. return AVERROR_EOF;
  106. }
  107. // FIXME this is wrong, this should rather be in an AVParser
  108. toc = avio_r8(s->pb);
  109. mode = (toc >> 3) & 0x0F;
  110. if (par->codec_id == AV_CODEC_ID_AMR_NB) {
  111. size = amrnb_packed_size[mode];
  112. } else if (par->codec_id == AV_CODEC_ID_AMR_WB) {
  113. size = amrwb_packed_size[mode];
  114. }
  115. if (!size || av_new_packet(pkt, size))
  116. return AVERROR(EIO);
  117. if (amr->cumulated_size < UINT64_MAX - size) {
  118. amr->cumulated_size += size;
  119. /* Both AMR formats have 50 frames per second */
  120. s->streams[0]->codecpar->bit_rate = amr->cumulated_size / ++amr->block_count * 8 * 50;
  121. }
  122. pkt->stream_index = 0;
  123. pkt->pos = pos;
  124. pkt->data[0] = toc;
  125. pkt->duration = par->codec_id == AV_CODEC_ID_AMR_NB ? 160 : 320;
  126. read = avio_read(s->pb, pkt->data + 1, size - 1);
  127. if (read != size - 1) {
  128. if (read < 0)
  129. return read;
  130. return AVERROR(EIO);
  131. }
  132. return 0;
  133. }
  134. #if CONFIG_AMR_DEMUXER
  135. AVInputFormat ff_amr_demuxer = {
  136. .name = "amr",
  137. .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
  138. .priv_data_size = sizeof(AMRContext),
  139. .read_probe = amr_probe,
  140. .read_header = amr_read_header,
  141. .read_packet = amr_read_packet,
  142. .flags = AVFMT_GENERIC_INDEX,
  143. };
  144. #endif
  145. #if CONFIG_AMRNB_DEMUXER
  146. static int amrnb_probe(const AVProbeData *p)
  147. {
  148. int mode, i = 0, valid = 0, invalid = 0;
  149. const uint8_t *b = p->buf;
  150. while (i < p->buf_size) {
  151. mode = b[i] >> 3 & 0x0F;
  152. if (mode < 9 && (b[i] & 0x4) == 0x4) {
  153. int last = b[i];
  154. int size = amrnb_packed_size[mode];
  155. while (size--) {
  156. if (b[++i] != last)
  157. break;
  158. }
  159. if (size > 0) {
  160. valid++;
  161. i += size;
  162. }
  163. } else {
  164. valid = 0;
  165. invalid++;
  166. i++;
  167. }
  168. }
  169. if (valid > 100 && valid >> 4 > invalid)
  170. return AVPROBE_SCORE_EXTENSION / 2 + 1;
  171. return 0;
  172. }
  173. static int amrnb_read_header(AVFormatContext *s)
  174. {
  175. AVStream *st = avformat_new_stream(s, NULL);
  176. if (!st)
  177. return AVERROR(ENOMEM);
  178. st->codecpar->codec_id = AV_CODEC_ID_AMR_NB;
  179. st->codecpar->sample_rate = 8000;
  180. st->codecpar->channels = 1;
  181. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  182. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  183. avpriv_set_pts_info(st, 64, 1, 8000);
  184. return 0;
  185. }
  186. AVInputFormat ff_amrnb_demuxer = {
  187. .name = "amrnb",
  188. .long_name = NULL_IF_CONFIG_SMALL("raw AMR-NB"),
  189. .priv_data_size = sizeof(AMRContext),
  190. .read_probe = amrnb_probe,
  191. .read_header = amrnb_read_header,
  192. .read_packet = amr_read_packet,
  193. .flags = AVFMT_GENERIC_INDEX,
  194. };
  195. #endif
  196. #if CONFIG_AMRWB_DEMUXER
  197. static int amrwb_probe(const AVProbeData *p)
  198. {
  199. int mode, i = 0, valid = 0, invalid = 0;
  200. const uint8_t *b = p->buf;
  201. while (i < p->buf_size) {
  202. mode = b[i] >> 3 & 0x0F;
  203. if (mode < 10 && (b[i] & 0x4) == 0x4) {
  204. int last = b[i];
  205. int size = amrwb_packed_size[mode];
  206. while (size--) {
  207. if (b[++i] != last)
  208. break;
  209. }
  210. if (size > 0) {
  211. valid++;
  212. i += size;
  213. }
  214. } else {
  215. valid = 0;
  216. invalid++;
  217. i++;
  218. }
  219. }
  220. if (valid > 100 && valid >> 4 > invalid)
  221. return AVPROBE_SCORE_EXTENSION / 2 + 1;
  222. return 0;
  223. }
  224. static int amrwb_read_header(AVFormatContext *s)
  225. {
  226. AVStream *st = avformat_new_stream(s, NULL);
  227. if (!st)
  228. return AVERROR(ENOMEM);
  229. st->codecpar->codec_id = AV_CODEC_ID_AMR_WB;
  230. st->codecpar->sample_rate = 16000;
  231. st->codecpar->channels = 1;
  232. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  233. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  234. avpriv_set_pts_info(st, 64, 1, 16000);
  235. return 0;
  236. }
  237. AVInputFormat ff_amrwb_demuxer = {
  238. .name = "amrwb",
  239. .long_name = NULL_IF_CONFIG_SMALL("raw AMR-WB"),
  240. .priv_data_size = sizeof(AMRContext),
  241. .read_probe = amrwb_probe,
  242. .read_header = amrwb_read_header,
  243. .read_packet = amr_read_packet,
  244. .flags = AVFMT_GENERIC_INDEX,
  245. };
  246. #endif
  247. #if CONFIG_AMR_MUXER
  248. AVOutputFormat ff_amr_muxer = {
  249. .name = "amr",
  250. .long_name = NULL_IF_CONFIG_SMALL("3GPP AMR"),
  251. .mime_type = "audio/amr",
  252. .extensions = "amr",
  253. .audio_codec = AV_CODEC_ID_AMR_NB,
  254. .video_codec = AV_CODEC_ID_NONE,
  255. .write_header = amr_write_header,
  256. .write_packet = ff_raw_write_packet,
  257. .flags = AVFMT_NOTIMESTAMPS,
  258. };
  259. #endif