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.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, int stream_index_ptr,
  47. uint8_t *buf, int size, int force_pts)
  48. {
  49. put_buffer(&s->pb, buf, 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. static int amr_probe(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 (p->buf_size < 5)
  63. return 0;
  64. if(memcmp(p->buf,AMR_header,5)==0)
  65. return AVPROBE_SCORE_MAX;
  66. else
  67. return 0;
  68. }
  69. /* amr input */
  70. static int amr_read_header(AVFormatContext *s,
  71. AVFormatParameters *ap)
  72. {
  73. ByteIOContext *pb = &s->pb;
  74. AVStream *st;
  75. uint8_t header[9];
  76. get_buffer(pb, header, 6);
  77. if(memcmp(header,AMR_header,6)!=0)
  78. {
  79. get_buffer(pb, header+6, 3);
  80. if(memcmp(header,AMRWB_header,9)!=0)
  81. {
  82. return -1;
  83. }
  84. st = av_new_stream(s, 0);
  85. if (!st)
  86. {
  87. return AVERROR_NOMEM;
  88. }
  89. st->codec.codec_type = CODEC_TYPE_AUDIO;
  90. st->codec.codec_tag = CODEC_ID_AMR_WB;
  91. st->codec.codec_id = CODEC_ID_AMR_WB;
  92. st->codec.channels = 1;
  93. st->codec.sample_rate = 16000;
  94. }
  95. else
  96. {
  97. st = av_new_stream(s, 0);
  98. if (!st)
  99. {
  100. return AVERROR_NOMEM;
  101. }
  102. st->codec.codec_type = CODEC_TYPE_AUDIO;
  103. st->codec.codec_tag = CODEC_ID_AMR_NB;
  104. st->codec.codec_id = CODEC_ID_AMR_NB;
  105. st->codec.channels = 1;
  106. st->codec.sample_rate = 8000;
  107. }
  108. return 0;
  109. }
  110. #define MAX_SIZE 32
  111. static int amr_read_packet(AVFormatContext *s,
  112. AVPacket *pkt)
  113. {
  114. AVCodecContext *enc = &s->streams[0]->codec;
  115. if (enc->codec_id == CODEC_ID_AMR_NB)
  116. {
  117. const static uint8_t packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  118. uint8_t toc, q, ft;
  119. int read;
  120. int size;
  121. if (url_feof(&s->pb))
  122. {
  123. return -EIO;
  124. }
  125. toc=get_byte(&s->pb);
  126. q = (toc >> 2) & 0x01;
  127. ft = (toc >> 3) & 0x0F;
  128. size=packed_size[ft];
  129. if (av_new_packet(pkt, size+1))
  130. {
  131. return -EIO;
  132. }
  133. pkt->stream_index = 0;
  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 -EIO;
  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 -EIO;
  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 -EIO;
  159. }
  160. pkt->stream_index = 0;
  161. pkt->data[0]=toc;
  162. read = get_buffer(&s->pb, pkt->data+1, size-1);
  163. if (read != (size-1))
  164. {
  165. av_free_packet(pkt);
  166. return -EIO;
  167. }
  168. return 0;
  169. }
  170. else
  171. {
  172. return -EIO;
  173. }
  174. }
  175. static int amr_read_close(AVFormatContext *s)
  176. {
  177. return 0;
  178. }
  179. static AVInputFormat amr_iformat = {
  180. "amr",
  181. "3gpp amr file format",
  182. 0, /*priv_data_size*/
  183. amr_probe,
  184. amr_read_header,
  185. amr_read_packet,
  186. amr_read_close,
  187. };
  188. static AVOutputFormat amr_oformat = {
  189. "amr",
  190. "3gpp amr file format",
  191. "audio/amr",
  192. "amr",
  193. 0,
  194. CODEC_ID_AMR_NB,
  195. CODEC_ID_NONE,
  196. amr_write_header,
  197. amr_write_packet,
  198. amr_write_trailer,
  199. };
  200. int amr_init(void)
  201. {
  202. av_register_input_format(&amr_iformat);
  203. av_register_output_format(&amr_oformat);
  204. return 0;
  205. }