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-06-24 07:01:58 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, "%"SCNu32"%"SCNu32"%"SCNu32"%"SCNu32,
     116             :                    &header_key_part[0], &header_key_part[1], &header_key_part[2], &header_key_part[3]);
     117           0 :             for (idx = 0; idx < 4; idx++) {
     118           0 :                 AV_WB32(&header_key[idx * 4], header_key_part[idx]); // convert each part to BE!
     119             :             }
     120           0 :             av_log(s, AV_LOG_DEBUG, "Processed HeaderKey is ");
     121           0 :             for (i = 0; i < 16; i++)
     122           0 :                 av_log(s, AV_LOG_DEBUG, "%02x", header_key[i]);
     123           0 :             av_log(s, AV_LOG_DEBUG, "\n");
     124             :         } else {
     125           0 :             av_dict_set(&s->metadata, key, val, 0);
     126             :         }
     127             :     }
     128             : 
     129             :     /* verify fixed key */
     130           0 :     if (c->aa_fixed_key_len != 16) {
     131           0 :         av_log(s, AV_LOG_ERROR, "aa_fixed_key value needs to be 16 bytes!\n");
     132           0 :         return AVERROR(EINVAL);
     133             :     }
     134             : 
     135             :     /* verify codec */
     136           0 :     if ((c->codec_second_size = get_second_size(codec_name)) == -1) {
     137           0 :         av_log(s, AV_LOG_ERROR, "unknown codec <%s>!\n", codec_name);
     138           0 :         return AVERROR(EINVAL);
     139             :     }
     140             : 
     141             :     /* decryption key derivation */
     142           0 :     c->tea_ctx = av_tea_alloc();
     143           0 :     if (!c->tea_ctx)
     144           0 :         return AVERROR(ENOMEM);
     145           0 :     av_tea_init(c->tea_ctx, c->aa_fixed_key, 16);
     146           0 :     output[0] = output[1] = 0; // purely for padding purposes
     147           0 :     memcpy(output + 2, header_key, 16);
     148           0 :     idx = 0;
     149           0 :     for (i = 0; i < 3; i++) { // TEA CBC with weird mixed endianness
     150           0 :         AV_WB32(src, header_seed);
     151           0 :         AV_WB32(src + 4, header_seed + 1);
     152           0 :         header_seed += 2;
     153           0 :         av_tea_crypt(c->tea_ctx, dst, src, 1, NULL, 0); // TEA ECB encrypt
     154           0 :         for (j = 0; j < TEA_BLOCK_SIZE && idx < 18; j+=1, idx+=1) {
     155           0 :             output[idx] = output[idx] ^ dst[j];
     156             :         }
     157             :     }
     158           0 :     memcpy(c->file_key, output + 2, 16); // skip first 2 bytes of output
     159           0 :     av_log(s, AV_LOG_DEBUG, "File key is ");
     160           0 :     for (i = 0; i < 16; i++)
     161           0 :         av_log(s, AV_LOG_DEBUG, "%02x", c->file_key[i]);
     162           0 :     av_log(s, AV_LOG_DEBUG, "\n");
     163             : 
     164             :     /* decoder setup */
     165           0 :     st = avformat_new_stream(s, NULL);
     166           0 :     if (!st) {
     167           0 :         av_freep(&c->tea_ctx);
     168           0 :         return AVERROR(ENOMEM);
     169             :     }
     170           0 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     171           0 :     if (!strcmp(codec_name, "mp332")) {
     172           0 :         st->codecpar->codec_id = AV_CODEC_ID_MP3;
     173           0 :         st->codecpar->sample_rate = 22050;
     174           0 :         st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     175           0 :         st->start_time = 0;
     176           0 :     } else if (!strcmp(codec_name, "acelp85")) {
     177           0 :         st->codecpar->codec_id = AV_CODEC_ID_SIPR;
     178           0 :         st->codecpar->block_align = 19;
     179           0 :         st->codecpar->channels = 1;
     180           0 :         st->codecpar->sample_rate = 8500;
     181           0 :         st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     182           0 :     } else if (!strcmp(codec_name, "acelp16")) {
     183           0 :         st->codecpar->codec_id = AV_CODEC_ID_SIPR;
     184           0 :         st->codecpar->block_align = 20;
     185           0 :         st->codecpar->channels = 1;
     186           0 :         st->codecpar->sample_rate = 16000;
     187           0 :         st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
     188             :     }
     189             : 
     190             :     /* determine, and jump to audio start offset */
     191           0 :     for (i = 1; i < toc_size; i++) { // skip the first entry!
     192           0 :         current_size = TOC[i].size;
     193           0 :         if (current_size > largest_size) {
     194           0 :             largest_idx = i;
     195           0 :             largest_size = current_size;
     196             :         }
     197             :     }
     198           0 :     start = TOC[largest_idx].offset;
     199           0 :     avio_seek(pb, start, SEEK_SET);
     200           0 :     c->current_chapter_size = 0;
     201             : 
     202           0 :     return 0;
     203             : }
     204             : 
     205           0 : static int aa_read_packet(AVFormatContext *s, AVPacket *pkt)
     206             : {
     207             :     uint8_t dst[TEA_BLOCK_SIZE];
     208             :     uint8_t src[TEA_BLOCK_SIZE];
     209             :     int i;
     210             :     int trailing_bytes;
     211             :     int blocks;
     212             :     uint8_t buf[MAX_CODEC_SECOND_SIZE * 2];
     213           0 :     int written = 0;
     214             :     int ret;
     215           0 :     AADemuxContext *c = s->priv_data;
     216             : 
     217             :     // are we at the start of a chapter?
     218           0 :     if (c->current_chapter_size == 0) {
     219           0 :         c->current_chapter_size = avio_rb32(s->pb);
     220           0 :         if (c->current_chapter_size == 0) {
     221           0 :             return AVERROR_EOF;
     222             :         }
     223           0 :         av_log(s, AV_LOG_DEBUG, "Chapter %d (%" PRId64 " bytes)\n", c->chapter_idx, c->current_chapter_size);
     224           0 :         c->chapter_idx = c->chapter_idx + 1;
     225           0 :         avio_skip(s->pb, 4); // data start offset
     226           0 :         c->current_codec_second_size = c->codec_second_size;
     227             :     }
     228             : 
     229             :     // is this the last block in this chapter?
     230           0 :     if (c->current_chapter_size / c->current_codec_second_size == 0) {
     231           0 :         c->current_codec_second_size = c->current_chapter_size % c->current_codec_second_size;
     232             :     }
     233             : 
     234             :     // decrypt c->current_codec_second_size bytes
     235           0 :     blocks = c->current_codec_second_size / TEA_BLOCK_SIZE;
     236           0 :     for (i = 0; i < blocks; i++) {
     237           0 :         avio_read(s->pb, src, TEA_BLOCK_SIZE);
     238           0 :         av_tea_init(c->tea_ctx, c->file_key, 16);
     239           0 :         av_tea_crypt(c->tea_ctx, dst, src, 1, NULL, 1);
     240           0 :         memcpy(buf + written, dst, TEA_BLOCK_SIZE);
     241           0 :         written = written + TEA_BLOCK_SIZE;
     242             :     }
     243           0 :     trailing_bytes = c->current_codec_second_size % TEA_BLOCK_SIZE;
     244           0 :     if (trailing_bytes != 0) { // trailing bytes are left unencrypted!
     245           0 :         avio_read(s->pb, src, trailing_bytes);
     246           0 :         memcpy(buf + written, src, trailing_bytes);
     247           0 :         written = written + trailing_bytes;
     248             :     }
     249             : 
     250             :     // update state
     251           0 :     c->current_chapter_size = c->current_chapter_size - c->current_codec_second_size;
     252           0 :     if (c->current_chapter_size <= 0)
     253           0 :         c->current_chapter_size = 0;
     254             : 
     255           0 :     ret = av_new_packet(pkt, written);
     256           0 :     if (ret < 0)
     257           0 :         return ret;
     258           0 :     memcpy(pkt->data, buf, written);
     259             : 
     260           0 :     return 0;
     261             : }
     262             : 
     263        5965 : static int aa_probe(AVProbeData *p)
     264             : {
     265        5965 :     uint8_t *buf = p->buf;
     266             : 
     267             :     // first 4 bytes are file size, next 4 bytes are the magic
     268        5965 :     if (AV_RB32(buf+4) != AA_MAGIC)
     269        5965 :         return 0;
     270             : 
     271           0 :     return AVPROBE_SCORE_MAX / 2;
     272             : }
     273             : 
     274           0 : static int aa_read_close(AVFormatContext *s)
     275             : {
     276           0 :     AADemuxContext *c = s->priv_data;
     277             : 
     278           0 :     av_freep(&c->tea_ctx);
     279             : 
     280           0 :     return 0;
     281             : }
     282             : 
     283             : #define OFFSET(x) offsetof(AADemuxContext, x)
     284             : static const AVOption aa_options[] = {
     285             :     { "aa_fixed_key", // extracted from libAAX_SDK.so and AAXSDKWin.dll files!
     286             :         "Fixed key used for handling Audible AA files", OFFSET(aa_fixed_key),
     287             :         AV_OPT_TYPE_BINARY, {.str="77214d4b196a87cd520045fd2a51d673"},
     288             :         .flags = AV_OPT_FLAG_DECODING_PARAM },
     289             :     { NULL },
     290             : };
     291             : 
     292             : static const AVClass aa_class = {
     293             :     .class_name = "aa",
     294             :     .item_name  = av_default_item_name,
     295             :     .option     = aa_options,
     296             :     .version    = LIBAVUTIL_VERSION_INT,
     297             : };
     298             : 
     299             : AVInputFormat ff_aa_demuxer = {
     300             :     .name           = "aa",
     301             :     .long_name      = NULL_IF_CONFIG_SMALL("Audible AA format files"),
     302             :     .priv_class     = &aa_class,
     303             :     .priv_data_size = sizeof(AADemuxContext),
     304             :     .extensions     = "aa",
     305             :     .read_probe     = aa_probe,
     306             :     .read_header    = aa_read_header,
     307             :     .read_packet    = aa_read_packet,
     308             :     .read_close     = aa_read_close,
     309             :     .flags          = AVFMT_GENERIC_INDEX,
     310             : };

Generated by: LCOV version 1.13