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.

179 lines
3.8KB

  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 amr narrowband (not amr-wb) is supported for now.
  22. */
  23. #include "avformat.h"
  24. #include "avi.h"
  25. static const unsigned char AMR_header [] = "#!AMR\n";
  26. /* AMR_FILE header */
  27. static int put_amr_header(ByteIOContext *pb, AVCodecContext *enc)
  28. {
  29. put_tag(pb, AMR_header); /* magic number */
  30. return 0;
  31. }
  32. static int amr_write_header(AVFormatContext *s)
  33. {
  34. ByteIOContext *pb = &s->pb;
  35. s->priv_data = NULL;
  36. /* format header */
  37. if (put_amr_header(pb, &s->streams[0]->codec) < 0) {
  38. return -1;
  39. }
  40. put_flush_packet(pb);
  41. return 0;
  42. }
  43. static int amr_write_packet(AVFormatContext *s, int stream_index_ptr,
  44. uint8_t *buf, int size, int force_pts)
  45. {
  46. put_buffer(&s->pb, buf, size);
  47. put_flush_packet(&s->pb);
  48. return 0;
  49. }
  50. static int amr_write_trailer(AVFormatContext *s)
  51. {
  52. return 0;
  53. }
  54. static int amr_probe(AVProbeData *p)
  55. {
  56. /* check file header */
  57. if (p->buf_size < 6)
  58. return 0;
  59. if(memcmp(p->buf,AMR_header,6)==0)
  60. return AVPROBE_SCORE_MAX;
  61. else
  62. return 0;
  63. }
  64. /* amr input */
  65. static int amr_read_header(AVFormatContext *s,
  66. AVFormatParameters *ap)
  67. {
  68. ByteIOContext *pb = &s->pb;
  69. AVStream *st;
  70. uint8_t header[6];
  71. get_buffer(pb, header, 6);
  72. if(memcmp(header,AMR_header,6)!=0)
  73. {
  74. return -1;
  75. }
  76. st = av_new_stream(s, 0);
  77. if (!st)
  78. return AVERROR_NOMEM;
  79. st->codec.codec_type = CODEC_TYPE_AUDIO;
  80. st->codec.codec_tag = CODEC_ID_AMR_NB;
  81. st->codec.codec_id = CODEC_ID_AMR_NB;
  82. st->codec.channels = 1;
  83. st->codec.sample_rate = 8000;
  84. return 0;
  85. }
  86. #define MAX_SIZE 32
  87. static int amr_read_packet(AVFormatContext *s,
  88. AVPacket *pkt)
  89. {
  90. static uint16_t packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  91. uint8_t toc, q, ft;
  92. int read;
  93. int size;
  94. if (url_feof(&s->pb))
  95. return -EIO;
  96. toc=0;
  97. toc=get_byte(&s->pb);
  98. q = (toc >> 2) & 0x01;
  99. ft = (toc >> 3) & 0x0F;
  100. size=packed_size[ft];
  101. //printf("amr_read_packet size=%d\n",size);
  102. if (av_new_packet(pkt, size+1))
  103. return -EIO;
  104. pkt->stream_index = 0;
  105. pkt->data[0]=toc;
  106. read = get_buffer(&s->pb, pkt->data+1, size);
  107. if (read != size)
  108. {
  109. av_free_packet(pkt);
  110. return -EIO;
  111. }
  112. return 0;
  113. }
  114. static int amr_read_close(AVFormatContext *s)
  115. {
  116. return 0;
  117. }
  118. static AVInputFormat amr_iformat = {
  119. "amr",
  120. "3gpp amr file format",
  121. 0,
  122. amr_probe,
  123. amr_read_header,
  124. amr_read_packet,
  125. amr_read_close,
  126. };
  127. static AVOutputFormat amr_oformat = {
  128. "amr",
  129. "3gpp amr file format",
  130. "audio/amr",
  131. "amr",
  132. 0,
  133. CODEC_ID_AMR_NB,
  134. CODEC_ID_NONE,
  135. amr_write_header,
  136. amr_write_packet,
  137. amr_write_trailer,
  138. };
  139. int amr_init(void)
  140. {
  141. av_register_input_format(&amr_iformat);
  142. av_register_output_format(&amr_oformat);
  143. return 0;
  144. }