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.

315 lines
11KB

  1. /*
  2. * Audible AA demuxer
  3. * Copyright (c) 2015 Vesselin Bontchev
  4. *
  5. * Header parsing is borrowed from https://github.com/jteeuwen/audible project.
  6. * Copyright (c) 2001-2014, Jim Teeuwen
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice, this
  12. * list of conditions and the following disclaimer.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/tea.h"
  29. #include "libavutil/opt.h"
  30. #define AA_MAGIC 1469084982 /* this identifies an audible .aa file */
  31. #define MAX_CODEC_SECOND_SIZE 3982
  32. #define MAX_TOC_ENTRIES 16
  33. #define MAX_DICTIONARY_ENTRIES 128
  34. #define TEA_BLOCK_SIZE 8
  35. typedef struct AADemuxContext {
  36. AVClass *class;
  37. uint8_t *aa_fixed_key;
  38. int aa_fixed_key_len;
  39. int codec_second_size;
  40. int current_codec_second_size;
  41. int chapter_idx;
  42. struct AVTEA *tea_ctx;
  43. uint8_t file_key[16];
  44. int64_t current_chapter_size;
  45. } AADemuxContext;
  46. static int get_second_size(char *codec_name)
  47. {
  48. int result = -1;
  49. if (!strcmp(codec_name, "mp332")) {
  50. result = 3982;
  51. } else if (!strcmp(codec_name, "acelp16")) {
  52. result = 2000;
  53. } else if (!strcmp(codec_name, "acelp85")) {
  54. result = 1045;
  55. }
  56. return result;
  57. }
  58. static int aa_read_header(AVFormatContext *s)
  59. {
  60. int i, j, idx, largest_idx = -1;
  61. uint32_t nkey, nval, toc_size, npairs, header_seed = 0, start;
  62. char key[128], val[128], codec_name[64] = {0};
  63. uint8_t output[24], dst[8], src[8];
  64. int64_t largest_size = -1, current_size = -1;
  65. struct toc_entry {
  66. uint32_t offset;
  67. uint32_t size;
  68. } TOC[MAX_TOC_ENTRIES];
  69. uint32_t header_key_part[4];
  70. uint8_t header_key[16] = {0};
  71. AADemuxContext *c = s->priv_data;
  72. AVIOContext *pb = s->pb;
  73. AVStream *st;
  74. /* parse .aa header */
  75. avio_skip(pb, 4); // file size
  76. avio_skip(pb, 4); // magic string
  77. toc_size = avio_rb32(pb); // TOC size
  78. avio_skip(pb, 4); // unidentified integer
  79. if (toc_size > MAX_TOC_ENTRIES)
  80. return AVERROR_INVALIDDATA;
  81. for (i = 0; i < toc_size; i++) { // read TOC
  82. avio_skip(pb, 4); // TOC entry index
  83. TOC[i].offset = avio_rb32(pb); // block offset
  84. TOC[i].size = avio_rb32(pb); // block size
  85. }
  86. avio_skip(pb, 24); // header termination block (ignored)
  87. npairs = avio_rb32(pb); // read dictionary entries
  88. if (npairs > MAX_DICTIONARY_ENTRIES)
  89. return AVERROR_INVALIDDATA;
  90. for (i = 0; i < npairs; i++) {
  91. memset(val, 0, sizeof(val));
  92. memset(key, 0, sizeof(key));
  93. avio_skip(pb, 1); // unidentified integer
  94. nkey = avio_rb32(pb); // key string length
  95. nval = avio_rb32(pb); // value string length
  96. if (nkey > sizeof(key)) {
  97. avio_skip(pb, nkey);
  98. } else {
  99. avio_read(pb, key, nkey); // key string
  100. }
  101. if (nval > sizeof(val)) {
  102. avio_skip(pb, nval);
  103. } else {
  104. avio_read(pb, val, nval); // value string
  105. }
  106. if (!strcmp(key, "codec")) {
  107. av_log(s, AV_LOG_DEBUG, "Codec is <%s>\n", val);
  108. strncpy(codec_name, val, sizeof(codec_name) - 1);
  109. }
  110. if (!strcmp(key, "HeaderSeed")) {
  111. av_log(s, AV_LOG_DEBUG, "HeaderSeed is <%s>\n", val);
  112. header_seed = atoi(val);
  113. }
  114. if (!strcmp(key, "HeaderKey")) { // this looks like "1234567890 1234567890 1234567890 1234567890"
  115. av_log(s, AV_LOG_DEBUG, "HeaderKey is <%s>\n", val);
  116. sscanf(val, "%u%u%u%u", &header_key_part[0], &header_key_part[1], &header_key_part[2], &header_key_part[3]);
  117. for (idx = 0; idx < 4; idx++) {
  118. AV_WB32(&header_key[idx * 4], header_key_part[idx]); // convert each part to BE!
  119. }
  120. av_log(s, AV_LOG_DEBUG, "Processed HeaderKey is ");
  121. for (i = 0; i < 16; i++)
  122. av_log(s, AV_LOG_DEBUG, "%02x", header_key[i]);
  123. av_log(s, AV_LOG_DEBUG, "\n");
  124. }
  125. }
  126. /* verify fixed key */
  127. if (c->aa_fixed_key_len != 16) {
  128. av_log(s, AV_LOG_ERROR, "aa_fixed_key value needs to be 16 bytes!\n");
  129. return AVERROR(EINVAL);
  130. }
  131. /* verify codec */
  132. if ((c->codec_second_size = get_second_size(codec_name)) == -1) {
  133. av_log(s, AV_LOG_ERROR, "unknown codec <%s>!\n", codec_name);
  134. return AVERROR(EINVAL);
  135. }
  136. /* decryption key derivation */
  137. c->tea_ctx = av_tea_alloc();
  138. if (!c->tea_ctx)
  139. return AVERROR(ENOMEM);
  140. av_tea_init(c->tea_ctx, c->aa_fixed_key, 16);
  141. output[0] = output[1] = 0; // purely for padding purposes
  142. memcpy(output + 2, header_key, 16);
  143. idx = 0;
  144. for (i = 0; i < 3; i++) { // TEA CBC with weird mixed endianness
  145. AV_WB32(src, header_seed);
  146. AV_WB32(src + 4, header_seed + 1);
  147. header_seed += 2;
  148. av_tea_crypt(c->tea_ctx, dst, src, 1, NULL, 0); // TEA ECB encrypt
  149. for (j = 0; j < TEA_BLOCK_SIZE && idx < 18; j+=1, idx+=1) {
  150. output[idx] = output[idx] ^ dst[j];
  151. }
  152. }
  153. memcpy(c->file_key, output + 2, 16); // skip first 2 bytes of output
  154. av_log(s, AV_LOG_DEBUG, "File key is ");
  155. for (i = 0; i < 16; i++)
  156. av_log(s, AV_LOG_DEBUG, "%02x", c->file_key[i]);
  157. av_log(s, AV_LOG_DEBUG, "\n");
  158. /* decoder setup */
  159. st = avformat_new_stream(s, NULL);
  160. if (!st) {
  161. av_freep(&c->tea_ctx);
  162. return AVERROR(ENOMEM);
  163. }
  164. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  165. if (!strcmp(codec_name, "mp332")) {
  166. st->codecpar->codec_id = AV_CODEC_ID_MP3;
  167. st->codecpar->sample_rate = 22050;
  168. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  169. st->start_time = 0;
  170. } else if (!strcmp(codec_name, "acelp85")) {
  171. st->codecpar->codec_id = AV_CODEC_ID_SIPR;
  172. st->codecpar->block_align = 19;
  173. st->codecpar->channels = 1;
  174. st->codecpar->sample_rate = 8500;
  175. } else if (!strcmp(codec_name, "acelp16")) {
  176. st->codecpar->codec_id = AV_CODEC_ID_SIPR;
  177. st->codecpar->block_align = 20;
  178. st->codecpar->channels = 1;
  179. st->codecpar->sample_rate = 16000;
  180. }
  181. /* determine, and jump to audio start offset */
  182. for (i = 1; i < toc_size; i++) { // skip the first entry!
  183. current_size = TOC[i].size;
  184. if (current_size > largest_size) {
  185. largest_idx = i;
  186. largest_size = current_size;
  187. }
  188. }
  189. start = TOC[largest_idx].offset;
  190. avio_seek(pb, start, SEEK_SET);
  191. c->current_chapter_size = 0;
  192. return 0;
  193. }
  194. static int aa_read_packet(AVFormatContext *s, AVPacket *pkt)
  195. {
  196. uint8_t dst[TEA_BLOCK_SIZE];
  197. uint8_t src[TEA_BLOCK_SIZE];
  198. int i;
  199. int trailing_bytes;
  200. int blocks;
  201. uint8_t buf[MAX_CODEC_SECOND_SIZE * 2];
  202. int written = 0;
  203. int ret;
  204. AADemuxContext *c = s->priv_data;
  205. // are we at the start of a chapter?
  206. if (c->current_chapter_size == 0) {
  207. c->current_chapter_size = avio_rb32(s->pb);
  208. if (c->current_chapter_size == 0) {
  209. return AVERROR_EOF;
  210. }
  211. av_log(s, AV_LOG_DEBUG, "Chapter %d (%" PRId64 " bytes)\n", c->chapter_idx, c->current_chapter_size);
  212. c->chapter_idx = c->chapter_idx + 1;
  213. avio_skip(s->pb, 4); // data start offset
  214. c->current_codec_second_size = c->codec_second_size;
  215. }
  216. // is this the last block in this chapter?
  217. if (c->current_chapter_size / c->current_codec_second_size == 0) {
  218. c->current_codec_second_size = c->current_chapter_size % c->current_codec_second_size;
  219. }
  220. // decrypt c->current_codec_second_size bytes
  221. blocks = c->current_codec_second_size / TEA_BLOCK_SIZE;
  222. for (i = 0; i < blocks; i++) {
  223. avio_read(s->pb, src, TEA_BLOCK_SIZE);
  224. av_tea_init(c->tea_ctx, c->file_key, 16);
  225. av_tea_crypt(c->tea_ctx, dst, src, 1, NULL, 1);
  226. memcpy(buf + written, dst, TEA_BLOCK_SIZE);
  227. written = written + TEA_BLOCK_SIZE;
  228. }
  229. trailing_bytes = c->current_codec_second_size % TEA_BLOCK_SIZE;
  230. if (trailing_bytes != 0) { // trailing bytes are left unencrypted!
  231. avio_read(s->pb, src, trailing_bytes);
  232. memcpy(buf + written, src, trailing_bytes);
  233. written = written + trailing_bytes;
  234. }
  235. // update state
  236. c->current_chapter_size = c->current_chapter_size - c->current_codec_second_size;
  237. if (c->current_chapter_size <= 0)
  238. c->current_chapter_size = 0;
  239. ret = av_new_packet(pkt, written);
  240. if (ret < 0)
  241. return ret;
  242. memcpy(pkt->data, buf, written);
  243. return 0;
  244. }
  245. static int aa_probe(AVProbeData *p)
  246. {
  247. uint8_t *buf = p->buf;
  248. // first 4 bytes are file size, next 4 bytes are the magic
  249. if (AV_RB32(buf+4) != AA_MAGIC)
  250. return 0;
  251. return AVPROBE_SCORE_MAX / 2;
  252. }
  253. static int aa_read_close(AVFormatContext *s)
  254. {
  255. AADemuxContext *c = s->priv_data;
  256. av_freep(&c->tea_ctx);
  257. return 0;
  258. }
  259. #define OFFSET(x) offsetof(AADemuxContext, x)
  260. static const AVOption aa_options[] = {
  261. { "aa_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
  262. "Fixed key used for handling Audible AA files", OFFSET(aa_fixed_key),
  263. AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd2a51d673"},
  264. .flags = AV_OPT_FLAG_DECODING_PARAM },
  265. { NULL },
  266. };
  267. static const AVClass aa_class = {
  268. .class_name = "aa",
  269. .item_name = av_default_item_name,
  270. .option = aa_options,
  271. .version = LIBAVUTIL_VERSION_INT,
  272. };
  273. AVInputFormat ff_aa_demuxer = {
  274. .name = "aa",
  275. .long_name = NULL_IF_CONFIG_SMALL("Audible AA format files"),
  276. .priv_class = &aa_class,
  277. .priv_data_size = sizeof(AADemuxContext),
  278. .extensions = "aa",
  279. .read_probe = aa_probe,
  280. .read_header = aa_read_header,
  281. .read_packet = aa_read_packet,
  282. .read_close = aa_read_close,
  283. .flags = AVFMT_GENERIC_INDEX,
  284. };