qcp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * QCP format (.qcp) demuxer
  3. * Copyright (c) 2009 Kenan Gillet
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * QCP format (.qcp) demuxer
  24. * @author Kenan Gillet
  25. * @sa RFC 3625: "The QCP File Format and Media Types for Speech Data"
  26. * http://tools.ietf.org/html/rfc3625
  27. */
  28. #include "libavutil/intreadwrite.h"
  29. #include "avformat.h"
  30. typedef struct {
  31. uint32_t data_size; ///< size of data chunk
  32. #define QCP_MAX_MODE 4
  33. int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding
  34. ///< to each mode, -1 if no size.
  35. } QCPContext;
  36. /**
  37. * Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file;
  38. * the first byte of the GUID can be either 0x41 or 0x42.
  39. */
  40. static const uint8_t guid_qcelp_13k_part[15] = {
  41. 0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba,
  42. 0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
  43. };
  44. /**
  45. * EVRC GUID as stored in the file
  46. */
  47. static const uint8_t guid_evrc[16] = {
  48. 0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
  49. 0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
  50. };
  51. /**
  52. * SMV GUID as stored in the file
  53. */
  54. static const uint8_t guid_smv[16] = {
  55. 0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
  56. 0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
  57. };
  58. /**
  59. * @param guid contains at least 16 bytes
  60. * @return 1 if the guid is a qcelp_13k guid, 0 otherwise
  61. */
  62. static int is_qcelp_13k_guid(const uint8_t *guid) {
  63. return (guid[0] == 0x41 || guid[0] == 0x42)
  64. && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
  65. }
  66. static int qcp_probe(AVProbeData *pd)
  67. {
  68. if (AV_RL32(pd->buf ) == AV_RL32("RIFF") &&
  69. AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
  70. return AVPROBE_SCORE_MAX;
  71. return 0;
  72. }
  73. static int qcp_read_header(AVFormatContext *s, AVFormatParameters *ap)
  74. {
  75. AVIOContext *pb = s->pb;
  76. QCPContext *c = s->priv_data;
  77. AVStream *st = av_new_stream(s, 0);
  78. uint8_t buf[16];
  79. int i, nb_rates;
  80. if (!st)
  81. return AVERROR(ENOMEM);
  82. avio_rb32(pb); // "RIFF"
  83. s->file_size = avio_rl32(pb) + 8;
  84. avio_skip(pb, 8 + 4 + 1 + 1); // "QLCMfmt " + chunk-size + major-version + minor-version
  85. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  86. st->codec->channels = 1;
  87. avio_read(pb, buf, 16);
  88. if (is_qcelp_13k_guid(buf)) {
  89. st->codec->codec_id = CODEC_ID_QCELP;
  90. } else if (!memcmp(buf, guid_evrc, 16)) {
  91. av_log(s, AV_LOG_ERROR, "EVRC codec is not supported.\n");
  92. return AVERROR_PATCHWELCOME;
  93. } else if (!memcmp(buf, guid_smv, 16)) {
  94. av_log(s, AV_LOG_ERROR, "SMV codec is not supported.\n");
  95. return AVERROR_PATCHWELCOME;
  96. } else {
  97. av_log(s, AV_LOG_ERROR, "Unknown codec GUID.\n");
  98. return AVERROR_INVALIDDATA;
  99. }
  100. avio_skip(pb, 2 + 80); // codec-version + codec-name
  101. st->codec->bit_rate = avio_rl16(pb);
  102. s->packet_size = avio_rl16(pb);
  103. avio_skip(pb, 2); // block-size
  104. st->codec->sample_rate = avio_rl16(pb);
  105. avio_skip(pb, 2); // sample-size
  106. memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode));
  107. nb_rates = avio_rl32(pb);
  108. nb_rates = FFMIN(nb_rates, 8);
  109. for (i=0; i<nb_rates; i++) {
  110. int size = avio_r8(pb);
  111. int mode = avio_r8(pb);
  112. if (mode > QCP_MAX_MODE) {
  113. av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
  114. } else
  115. c->rates_per_mode[mode] = size;
  116. }
  117. avio_skip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved
  118. return 0;
  119. }
  120. static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
  121. {
  122. AVIOContext *pb = s->pb;
  123. QCPContext *c = s->priv_data;
  124. unsigned int chunk_size, tag;
  125. while(!url_feof(pb)) {
  126. if (c->data_size) {
  127. int pkt_size, ret, mode = avio_r8(pb);
  128. if (s->packet_size) {
  129. pkt_size = s->packet_size - 1;
  130. } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) {
  131. c->data_size--;
  132. continue;
  133. }
  134. if (c->data_size <= pkt_size) {
  135. av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
  136. pkt_size = c->data_size - 1;
  137. }
  138. if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) {
  139. if (pkt_size != ret)
  140. av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
  141. c->data_size -= pkt_size + 1;
  142. }
  143. return ret;
  144. }
  145. if (avio_tell(pb) & 1 && avio_r8(pb))
  146. av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");
  147. tag = avio_rl32(pb);
  148. chunk_size = avio_rl32(pb);
  149. switch (tag) {
  150. case MKTAG('v', 'r', 'a', 't'):
  151. if (avio_rl32(pb)) // var-rate-flag
  152. s->packet_size = 0;
  153. avio_skip(pb, 4); // size-in-packets
  154. break;
  155. case MKTAG('d', 'a', 't', 'a'):
  156. c->data_size = chunk_size;
  157. break;
  158. default:
  159. avio_skip(pb, chunk_size);
  160. }
  161. }
  162. return AVERROR_EOF;
  163. }
  164. AVInputFormat ff_qcp_demuxer = {
  165. .name = "qcp",
  166. .long_name = NULL_IF_CONFIG_SMALL("QCP format"),
  167. .priv_data_size = sizeof(QCPContext),
  168. .read_probe = qcp_probe,
  169. .read_header = qcp_read_header,
  170. .read_packet = qcp_read_packet,
  171. };