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.

245 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., 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 = MKTAG('s', 'a', 'w', 'b');
  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 = MKTAG('s', 'a', 'm', 'r');
  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->pos= url_ftell(&s->pb);
  134. pkt->data[0]=toc;
  135. read = get_buffer(&s->pb, pkt->data+1, size);
  136. if (read != size)
  137. {
  138. av_free_packet(pkt);
  139. return AVERROR_IO;
  140. }
  141. return 0;
  142. }
  143. else if(enc->codec_id == CODEC_ID_AMR_WB)
  144. {
  145. static uint8_t packed_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
  146. uint8_t toc, mode;
  147. int read;
  148. int size;
  149. if (url_feof(&s->pb))
  150. {
  151. return AVERROR_IO;
  152. }
  153. toc=get_byte(&s->pb);
  154. mode = (uint8_t)((toc >> 3) & 0x0F);
  155. size = packed_size[mode];
  156. if ( (size==0) || av_new_packet(pkt, size))
  157. {
  158. return AVERROR_IO;
  159. }
  160. pkt->stream_index = 0;
  161. pkt->pos= url_ftell(&s->pb);
  162. pkt->data[0]=toc;
  163. read = get_buffer(&s->pb, pkt->data+1, size-1);
  164. if (read != (size-1))
  165. {
  166. av_free_packet(pkt);
  167. return AVERROR_IO;
  168. }
  169. return 0;
  170. }
  171. else
  172. {
  173. return AVERROR_IO;
  174. }
  175. }
  176. static int amr_read_close(AVFormatContext *s)
  177. {
  178. return 0;
  179. }
  180. static AVInputFormat amr_iformat = {
  181. "amr",
  182. "3gpp amr file format",
  183. 0, /*priv_data_size*/
  184. amr_probe,
  185. amr_read_header,
  186. amr_read_packet,
  187. amr_read_close,
  188. };
  189. static AVOutputFormat amr_oformat = {
  190. "amr",
  191. "3gpp amr file format",
  192. "audio/amr",
  193. "amr",
  194. 0,
  195. CODEC_ID_AMR_NB,
  196. CODEC_ID_NONE,
  197. amr_write_header,
  198. amr_write_packet,
  199. amr_write_trailer,
  200. };
  201. int amr_init(void)
  202. {
  203. av_register_input_format(&amr_iformat);
  204. av_register_output_format(&amr_oformat);
  205. return 0;
  206. }