LCOV - code coverage report
Current view: top level - src/libavformat - aadec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 4 153 2.6 %
Date: 2017-01-24 04:42:20 Functions: 1 5 20.0 %

          Line data    Source code
       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             : 
      26             : #include "avformat.h"
      27             : #include "internal.h"
      28             : #include "libavutil/dict.h"
      29             : #include "libavutil/intreadwrite.h"
      30             : #include "libavutil/tea.h"
      31             : #include "libavutil/opt.h"
      32             : 
      33             : #define AA_MAGIC 1469084982 /* this identifies an audible .aa file */
      34             : #define MAX_CODEC_SECOND_SIZE 3982
      35             : #define MAX_TOC_ENTRIES 16
      36             : #define MAX_DICTIONARY_ENTRIES 128
      37             : #define TEA_BLOCK_SIZE 8
      38             : 
      39             : typedef struct AADemuxContext {
      40             :     AVClass *class;
      41             :     uint8_t *aa_fixed_key;
      42             :     int aa_fixed_key_len;
      43             :     int codec_second_size;
      44             :     int current_codec_second_size;
      45             :     int chapter_idx;
      46             :     struct AVTEA *tea_ctx;
      47             :     uint8_t file_key[16];
      48             :     int64_t current_chapter_size;
      49             : } AADemuxContext;
      50             : 
      51           0 : static int get_second_size(char *codec_name)
      52             : {
      53           0 :     int result = -1;
      54             : 
      55           0 :     if (!strcmp(codec_name, "mp332")) {
      56           0 :         result = 3982;
      57           0 :     } else if (!strcmp(codec_name, "acelp16")) {
      58           0 :         result = 2000;
      59           0 :     } else if (!strcmp(codec_name, "acelp85")) {
      60           0 :         result = 1045;
      61             :     }
      62             : 
      63           0 :     return result;
      64             : }
      65             : 
      66           0 : static int aa_read_header(AVFormatContext *s)
      67             : {
      68           0 :     int i, j, idx, largest_idx = -1;
      69           0 :     uint32_t nkey, nval, toc_size, npairs, header_seed = 0, start;
      70           0 :     char key[128], val[128], codec_name[64] = {0};
      71             :     uint8_t output[24], dst[8], src[8];
      72           0 :     int64_t largest_size = -1, current_size = -1;
      73             :     struct toc_entry {
      74             :         uint32_t offset;
      75             :         uint32_t size;
      76             :     } TOC[MAX_TOC_ENTRIES];
      77             :     uint32_t header_key_part[4];
      78           0 :     uint8_t header_key[16] = {0};
      79           0 :     AADemuxContext *c = s->priv_data;
      80           0 :     AVIOContext *pb = s->pb;
      81             :     AVStream *st;
      82             : 
      83             :     /* parse .aa header */
      84           0 :     avio_skip(pb, 4); // file size
      85           0 :     avio_skip(pb, 4); // magic string
      86           0 :     toc_size = avio_rb32(pb); // TOC size
      87           0 :     avio_skip(pb, 4); // unidentified integer
      88           0 :     if (toc_size > MAX_TOC_ENTRIES)
      89           0 :         return AVERROR_INVALIDDATA;
      90           0 :     for (i = 0; i < toc_size; i++) { // read TOC
      91           0 :         avio_skip(pb, 4); // TOC entry index
      92           0 :         TOC[i].offset = avio_rb32(pb); // block offset
      93           0 :         TOC[i].size = avio_rb32(pb); // block size
      94             :     }
      95           0 :     avio_skip(pb, 24); // header termination block (ignored)
      96           0 :     npairs = avio_rb32(pb); // read dictionary entries
      97           0 :     if (npairs > MAX_DICTIONARY_ENTRIES)
      98           0 :         return AVERROR_INVALIDDATA;
      99           0 :     for (i = 0; i < npairs; i++) {
     100           0 :         memset(val, 0, sizeof(val));
     101           0 :         memset(key, 0, sizeof(key));
     102           0 :         avio_skip(pb, 1); // unidentified integer
     103           0 :         nkey = avio_rb32(pb); // key string length
     104           0 :         nval = avio_rb32(pb); // value string length
     105           0 :         avio_get_str(pb, nkey, key, sizeof(key));
     106           0 :         avio_get_str(pb, nval, val, sizeof(val));
     107           0 :         if (!strcmp(key, "codec")) {
     108           0 :             av_log(s, AV_LOG_DEBUG, "Codec is <%s>\n", val);
     109           0 :             strncpy(codec_name, val, sizeof(codec_name) - 1);
     110           0 :         } else if (!strcmp(key, "HeaderSeed")) {
     111           0 :             av_log(s, AV_LOG_DEBUG, "HeaderSeed is <%s>\n", val);
     112           0 :             header_seed = atoi(val);
     113           0 :         } else if (!strcmp(key, "HeaderKey")) { // this looks like "1234567890 1234567890 1234567890 1234567890"
     114           0 :             av_log(s, AV_LOG_DEBUG, "HeaderKey is <%s>\n", val);
     115           0 :             sscanf(val, "%u%u%u%u", &header_key_part[0], &header_key_part[1], &header_key_part[2], &header_key_part[3]);
     116           0 :             for (idx = 0; idx < 4; idx++) {
     117           0 :                 AV_WB32(&header_key[idx * 4], header_key_part[idx]); // convert each part to BE!
     118             :             }
     119           0 :             av_log(s, AV_LOG_DEBUG, "Processed HeaderKey is ");
     120           0 :             for (i = 0; i < 16; i++)
     121           0 :                 av_log(s, AV_LOG_DEBUG, "%02x", header_key[i]);
     122           0 :             av_log(s, AV_LOG_DEBUG, "\n");
     123             :         } else {
     124           0 :             av_dict_set(&s->metadata, key, val, 0);
     125             :         }
     126             :     }
     127             : 
     128             :     /* verify fixed key */
     129           0 :     if (c->aa_fixed_key_len != 16) {
     130           0 :         av_log(s, AV_LOG_ERROR, "aa_fixed_key value needs to be 16 bytes!\n");
     131           0 :         return AVERROR(EINVAL);
     132             :     }
     133             : 
     134             :     /* verify codec */
     135           0 :     if ((c->codec_second_size = get_second_size(codec_name)) == -1) {
     136           0 :         av_log(s, AV_LOG_ERROR, "unknown codec <%s>!\n", codec_name);
     137           0 :         return AVERROR(EINVAL);
     138             :     }
     139             : 
     140             :     /* decryption key derivation */
     141           0 :     c->tea_ctx = av_tea_alloc();
     142           0 :     if (!c->tea_ctx)
     143           0 :         return AVERROR(ENOMEM);
     144           0 :     av_tea_init(c->tea_ctx, c->aa_fixed_key, 16);
     145           0 :     output[0] = output[1] = 0; // purely for padding purposes
     146           0 :     memcpy(output + 2, header_key, 16);
     147           0 :     idx = 0;
     148           0 :     for (i = 0; i < 3; i++) { // TEA CBC with weird mixed endianness
     149           0 :         AV_WB32(src, header_seed);
     150           0 :         AV_WB32(src + 4, header_seed + 1);
     151           0 :         header_seed += 2;
     152           0 :         av_tea_crypt(c->tea_ctx, dst, src, 1, NULL, 0); // TEA ECB encrypt
     153           0 :         for (j = 0; j < TEA_BLOCK_SIZE && idx < 18; j+=1, idx+=1) {
     154           0 :             output[idx] = output[idx] ^ dst[j];
     155             :         }
     156             :     }
     157           0 :     memcpy(c->file_key, output + 2, 16); // skip first 2 bytes of output
     158           0 :     av_log(s, AV_LOG_DEBUG, "File key is ");
     159           0 :     for (i = 0; i < 16; i++)
     160           0 :         av_log(s, AV_LOG_DEBUG, "%02x", c->file_key[i]);
     161           0 :     av_log(s, AV_LOG_DEBUG, "\n");
     162             : 
     163             :     /* decoder setup */
     164           0 :     st = avformat_new_stream(s, NULL);
     165           0 :     if (!st) {
     166           0 :         av_freep(&c->tea_ctx);
     167           0 :         return AVERROR(ENOMEM);
     168             :     }
     169           0 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     170           0 :     if (!strcmp(codec_name, "mp332")) {
     171           0 :         st->codecpar->codec_id = AV_CODEC_ID_MP3;
     172           0 :         st->codecpar->sample_rate = 22050;
     173           0 :         st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     174           0 :         st->start_time = 0;
     175           0 :     } else if (!strcmp(codec_name, "acelp85")) {
     176           0 :         st->codecpar->codec_id = AV_CODEC_ID_SIPR;
     177           0 :         st->codecpar->block_align = 19;
     178           0 :         st->codecpar->channels = 1;
     179           0 :         st->codecpar->sample_rate = 8500;
     180           0 :         st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     181           0 :     } else if (!strcmp(codec_name, "acelp16")) {
     182           0 :         st->codecpar->codec_id = AV_CODEC_ID_SIPR;
     183           0 :         st->codecpar->block_align = 20;
     184           0 :         st->codecpar->channels = 1;
     185           0 :         st->codecpar->sample_rate = 16000;
     186           0 :         st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     187             :     }
     188             : 
     189             :     /* determine, and jump to audio start offset */
     190           0 :     for (i = 1; i < toc_size; i++) { // skip the first entry!
     191           0 :         current_size = TOC[i].size;
     192           0 :         if (current_size > largest_size) {
     193           0 :             largest_idx = i;
     194           0 :             largest_size = current_size;
     195             :         }
     196             :     }
     197           0 :     start = TOC[largest_idx].offset;
     198           0 :     avio_seek(pb, start, SEEK_SET);
     199           0 :     c->current_chapter_size = 0;
     200             : 
     201           0 :     return 0;
     202             : }
     203             : 
     204           0 : static int aa_read_packet(AVFormatContext *s, AVPacket *pkt)
     205             : {
     206             :     uint8_t dst[TEA_BLOCK_SIZE];
     207             :     uint8_t src[TEA_BLOCK_SIZE];
     208             :     int i;
     209             :     int trailing_bytes;
     210             :     int blocks;
     211             :     uint8_t buf[MAX_CODEC_SECOND_SIZE * 2];
     212           0 :     int written = 0;
     213             :     int ret;
     214           0 :     AADemuxContext *c = s->priv_data;
     215             : 
     216             :     // are we at the start of a chapter?
     217           0 :     if (c->current_chapter_size == 0) {
     218           0 :         c->current_chapter_size = avio_rb32(s->pb);
     219           0 :         if (c->current_chapter_size == 0) {
     220           0 :             return AVERROR_EOF;
     221             :         }
     222           0 :         av_log(s, AV_LOG_DEBUG, "Chapter %d (%" PRId64 " bytes)\n", c->chapter_idx, c->current_chapter_size);
     223           0 :         c->chapter_idx = c->chapter_idx + 1;
     224           0 :         avio_skip(s->pb, 4); // data start offset
     225           0 :         c->current_codec_second_size = c->codec_second_size;
     226             :     }
     227             : 
     228             :     // is this the last block in this chapter?
     229           0 :     if (c->current_chapter_size / c->current_codec_second_size == 0) {
     230           0 :         c->current_codec_second_size = c->current_chapter_size % c->current_codec_second_size;
     231             :     }
     232             : 
     233             :     // decrypt c->current_codec_second_size bytes
     234           0 :     blocks = c->current_codec_second_size / TEA_BLOCK_SIZE;
     235           0 :     for (i = 0; i < blocks; i++) {
     236           0 :         avio_read(s->pb, src, TEA_BLOCK_SIZE);
     237           0 :         av_tea_init(c->tea_ctx, c->file_key, 16);
     238           0 :         av_tea_crypt(c->tea_ctx, dst, src, 1, NULL, 1);
     239           0 :         memcpy(buf + written, dst, TEA_BLOCK_SIZE);
     240           0 :         written = written + TEA_BLOCK_SIZE;
     241             :     }
     242           0 :     trailing_bytes = c->current_codec_second_size % TEA_BLOCK_SIZE;
     243           0 :     if (trailing_bytes != 0) { // trailing bytes are left unencrypted!
     244           0 :         avio_read(s->pb, src, trailing_bytes);
     245           0 :         memcpy(buf + written, src, trailing_bytes);
     246           0 :         written = written + trailing_bytes;
     247             :     }
     248             : 
     249             :     // update state
     250           0 :     c->current_chapter_size = c->current_chapter_size - c->current_codec_second_size;
     251           0 :     if (c->current_chapter_size <= 0)
     252           0 :         c->current_chapter_size = 0;
     253             : 
     254           0 :     ret = av_new_packet(pkt, written);
     255           0 :     if (ret < 0)
     256           0 :         return ret;
     257           0 :     memcpy(pkt->data, buf, written);
     258             : 
     259           0 :     return 0;
     260             : }
     261             : 
     262        5563 : static int aa_probe(AVProbeData *p)
     263             : {
     264        5563 :     uint8_t *buf = p->buf;
     265             : 
     266             :     // first 4 bytes are file size, next 4 bytes are the magic
     267        5563 :     if (AV_RB32(buf+4) != AA_MAGIC)
     268        5563 :         return 0;
     269             : 
     270           0 :     return AVPROBE_SCORE_MAX / 2;
     271             : }
     272             : 
     273           0 : static int aa_read_close(AVFormatContext *s)
     274             : {
     275           0 :     AADemuxContext *c = s->priv_data;
     276             : 
     277           0 :     av_freep(&c->tea_ctx);
     278             : 
     279           0 :     return 0;
     280             : }
     281             : 
     282             : #define OFFSET(x) offsetof(AADemuxContext, x)
     283             : static const AVOption aa_options[] = {
     284             :     { "aa_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
     285             :         "Fixed key used for handling Audible AA files", OFFSET(aa_fixed_key),
     286             :         AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd2a51d673"},
     287             :         .flags = AV_OPT_FLAG_DECODING_PARAM },
     288             :     { NULL },
     289             : };
     290             : 
     291             : static const AVClass aa_class = {
     292             :     .class_name = "aa",
     293             :     .item_name  = av_default_item_name,
     294             :     .option     = aa_options,
     295             :     .version    = LIBAVUTIL_VERSION_INT,
     296             : };
     297             : 
     298             : AVInputFormat ff_aa_demuxer = {
     299             :     .name           = "aa",
     300             :     .long_name      = NULL_IF_CONFIG_SMALL("Audible AA format files"),
     301             :     .priv_class     = &aa_class,
     302             :     .priv_data_size = sizeof(AADemuxContext),
     303             :     .extensions     = "aa",
     304             :     .read_probe     = aa_probe,
     305             :     .read_header    = aa_read_header,
     306             :     .read_packet    = aa_read_packet,
     307             :     .read_close     = aa_read_close,
     308             :     .flags          = AVFMT_GENERIC_INDEX,
     309             : };

Generated by: LCOV version 1.12