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.

199 lines
6.1KB

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