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.

251 lines
5.6KB

  1. /*
  2. * amr file format
  3. * Copyright (c) 2001 ffmpeg project
  4. *
  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.
  9. *
  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.
  14. *
  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
  18. */
  19. /*
  20. Write and read amr data according to RFC3267, http://www.ietf.org/rfc/rfc3267.txt?number=3267
  21. Only mono files are supported.
  22. */
  23. #include "avformat.h"
  24. static const unsigned char AMR_header [] = "#!AMR\n";
  25. static const unsigned char AMRWB_header [] = "#!AMR-WB\n";
  26. #ifdef CONFIG_MUXERS
  27. static int amr_write_header(AVFormatContext *s)
  28. {
  29. ByteIOContext *pb = &s->pb;
  30. AVCodecContext *enc = s->streams[0]->codec;
  31. s->priv_data = NULL;
  32. if (enc->codec_id == CODEC_ID_AMR_NB)
  33. {
  34. put_tag(pb, AMR_header); /* magic number */
  35. }
  36. else if(enc->codec_id == CODEC_ID_AMR_WB)
  37. {
  38. put_tag(pb, AMRWB_header); /* magic number */
  39. }
  40. else
  41. {
  42. //This is an error!
  43. }
  44. put_flush_packet(pb);
  45. return 0;
  46. }
  47. static int amr_write_packet(AVFormatContext *s, AVPacket *pkt)
  48. {
  49. put_buffer(&s->pb, pkt->data, pkt->size);
  50. put_flush_packet(&s->pb);
  51. return 0;
  52. }
  53. static int amr_write_trailer(AVFormatContext *s)
  54. {
  55. return 0;
  56. }
  57. #endif /* CONFIG_MUXERS */
  58. static int amr_probe(AVProbeData *p)
  59. {
  60. //Only check for "#!AMR" which could be amr-wb, amr-nb.
  61. //This will also trigger multichannel files: "#!AMR_MC1.0\n" and
  62. //"#!AMR-WB_MC1.0\n" (not supported)
  63. if (p->buf_size < 5)
  64. return 0;
  65. if(memcmp(p->buf,AMR_header,5)==0)
  66. return AVPROBE_SCORE_MAX;
  67. else
  68. return 0;
  69. }
  70. /* amr input */
  71. static int amr_read_header(AVFormatContext *s,
  72. AVFormatParameters *ap)
  73. {
  74. ByteIOContext *pb = &s->pb;
  75. AVStream *st;
  76. uint8_t header[9];
  77. get_buffer(pb, header, 6);
  78. if(memcmp(header,AMR_header,6)!=0)
  79. {
  80. get_buffer(pb, header+6, 3);
  81. if(memcmp(header,AMRWB_header,9)!=0)
  82. {
  83. return -1;
  84. }
  85. st = av_new_stream(s, 0);
  86. if (!st)
  87. {
  88. return AVERROR_NOMEM;
  89. }
  90. st->codec->codec_type = CODEC_TYPE_AUDIO;
  91. st->codec->codec_tag = MKTAG('s', 'a', 'w', 'b');
  92. st->codec->codec_id = CODEC_ID_AMR_WB;
  93. st->codec->channels = 1;
  94. st->codec->sample_rate = 16000;
  95. }
  96. else
  97. {
  98. st = av_new_stream(s, 0);
  99. if (!st)
  100. {
  101. return AVERROR_NOMEM;
  102. }
  103. st->codec->codec_type = CODEC_TYPE_AUDIO;
  104. st->codec->codec_tag = MKTAG('s', 'a', 'm', 'r');
  105. st->codec->codec_id = CODEC_ID_AMR_NB;
  106. st->codec->channels = 1;
  107. st->codec->sample_rate = 8000;
  108. }
  109. return 0;
  110. }
  111. #define MAX_SIZE 32
  112. static int amr_read_packet(AVFormatContext *s,
  113. AVPacket *pkt)
  114. {
  115. AVCodecContext *enc = s->streams[0]->codec;
  116. if (enc->codec_id == CODEC_ID_AMR_NB)
  117. {
  118. const static uint8_t packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  119. uint8_t toc, q, ft;
  120. int read;
  121. int size;
  122. if (url_feof(&s->pb))
  123. {
  124. return AVERROR_IO;
  125. }
  126. toc=get_byte(&s->pb);
  127. q = (toc >> 2) & 0x01;
  128. ft = (toc >> 3) & 0x0F;
  129. size=packed_size[ft];
  130. if (av_new_packet(pkt, size+1))
  131. {
  132. return AVERROR_IO;
  133. }
  134. pkt->stream_index = 0;
  135. pkt->pos= url_ftell(&s->pb);
  136. pkt->data[0]=toc;
  137. read = get_buffer(&s->pb, pkt->data+1, size);
  138. if (read != size)
  139. {
  140. av_free_packet(pkt);
  141. return AVERROR_IO;
  142. }
  143. return 0;
  144. }
  145. else if(enc->codec_id == CODEC_ID_AMR_WB)
  146. {
  147. static uint8_t packed_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
  148. uint8_t toc, mode;
  149. int read;
  150. int size;
  151. if (url_feof(&s->pb))
  152. {
  153. return AVERROR_IO;
  154. }
  155. toc=get_byte(&s->pb);
  156. mode = (uint8_t)((toc >> 3) & 0x0F);
  157. size = packed_size[mode];
  158. if ( (size==0) || av_new_packet(pkt, size))
  159. {
  160. return AVERROR_IO;
  161. }
  162. pkt->stream_index = 0;
  163. pkt->pos= url_ftell(&s->pb);
  164. pkt->data[0]=toc;
  165. read = get_buffer(&s->pb, pkt->data+1, size-1);
  166. if (read != (size-1))
  167. {
  168. av_free_packet(pkt);
  169. return AVERROR_IO;
  170. }
  171. return 0;
  172. }
  173. else
  174. {
  175. return AVERROR_IO;
  176. }
  177. }
  178. static int amr_read_close(AVFormatContext *s)
  179. {
  180. return 0;
  181. }
  182. static AVInputFormat amr_iformat = {
  183. "amr",
  184. "3gpp amr file format",
  185. 0, /*priv_data_size*/
  186. amr_probe,
  187. amr_read_header,
  188. amr_read_packet,
  189. amr_read_close,
  190. };
  191. #ifdef CONFIG_MUXERS
  192. static AVOutputFormat amr_oformat = {
  193. "amr",
  194. "3gpp amr file format",
  195. "audio/amr",
  196. "amr",
  197. 0,
  198. CODEC_ID_AMR_NB,
  199. CODEC_ID_NONE,
  200. amr_write_header,
  201. amr_write_packet,
  202. amr_write_trailer,
  203. };
  204. #endif
  205. int amr_init(void)
  206. {
  207. av_register_input_format(&amr_iformat);
  208. #ifdef CONFIG_MUXERS
  209. av_register_output_format(&amr_oformat);
  210. #endif
  211. return 0;
  212. }