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.

244 lines
5.5KB

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