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.

305 lines
8.4KB

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