LCOV - code coverage report
Current view: top level - libavformat - redspark.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 62 71 87.3 %
Date: 2017-12-15 02:19:58 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * RedSpark demuxer
       3             :  * Copyright (c) 2013 James Almer
       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             : #include "libavcodec/bytestream.h"
      23             : #include "libavutil/intreadwrite.h"
      24             : #include "avformat.h"
      25             : #include "avio.h"
      26             : #include "internal.h"
      27             : 
      28             : #define HEADER_SIZE 4096
      29             : #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
      30             : 
      31             : typedef struct RedSparkContext {
      32             :     int         samples_count;
      33             : } RedSparkContext;
      34             : 
      35        6130 : static int redspark_probe(AVProbeData *p)
      36             : {
      37             :     uint32_t key, data;
      38             :     uint8_t header[8];
      39             : 
      40             :     /* Decrypt first 8 bytes of the header */
      41        6130 :     data = AV_RB32(p->buf);
      42        6130 :     key  = data ^ 0x52656453;
      43        6130 :     data ^= key;
      44        6130 :     AV_WB32(header, data);
      45        6130 :     key = rol(key, 11);
      46             : 
      47        6130 :     key += rol(key, 3);
      48        6130 :     data = AV_RB32(p->buf + 4) ^ key;
      49        6130 :     AV_WB32(header + 4, data);
      50             : 
      51        6130 :     if (AV_RB64(header) == AV_RB64("RedSpark"))
      52           1 :         return AVPROBE_SCORE_MAX;
      53             : 
      54        6129 :     return 0;
      55             : }
      56             : 
      57           1 : static int redspark_read_header(AVFormatContext *s)
      58             : {
      59           1 :     AVIOContext *pb = s->pb;
      60           1 :     RedSparkContext *redspark = s->priv_data;
      61             :     AVCodecParameters *par;
      62             :     GetByteContext gbc;
      63           1 :     int i, coef_off, ret = 0;
      64             :     uint32_t key, data;
      65             :     uint8_t header[HEADER_SIZE];
      66             :     AVStream *st;
      67             : 
      68           1 :     st = avformat_new_stream(s, NULL);
      69           1 :     if (!st)
      70           0 :         return AVERROR(ENOMEM);
      71           1 :     par = st->codecpar;
      72             : 
      73             :     /* Decrypt header */
      74           1 :     data = avio_rb32(pb);
      75           1 :     key  = data ^ 0x52656453;
      76           1 :     data ^= key;
      77           1 :     AV_WB32(header, data);
      78           1 :     key = rol(key, 11);
      79             : 
      80        1024 :     for (i = 4; i < HEADER_SIZE; i += 4) {
      81        1023 :         key += rol(key, 3);
      82        1023 :         data = avio_rb32(pb) ^ key;
      83        1023 :         AV_WB32(header + i, data);
      84             :     }
      85             : 
      86           1 :     par->codec_id    = AV_CODEC_ID_ADPCM_THP;
      87           1 :     par->codec_type  = AVMEDIA_TYPE_AUDIO;
      88             : 
      89           1 :     bytestream2_init(&gbc, header, HEADER_SIZE);
      90           1 :     bytestream2_seek(&gbc, 0x3c, SEEK_SET);
      91           1 :     par->sample_rate = bytestream2_get_be32u(&gbc);
      92           1 :     if (par->sample_rate <= 0 || par->sample_rate > 96000) {
      93           0 :         av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", par->sample_rate);
      94           0 :         return AVERROR_INVALIDDATA;
      95             :     }
      96             : 
      97           1 :     st->duration = bytestream2_get_be32u(&gbc) * 14;
      98           1 :     redspark->samples_count = 0;
      99           1 :     bytestream2_skipu(&gbc, 10);
     100           1 :     par->channels = bytestream2_get_byteu(&gbc);
     101           1 :     if (!par->channels) {
     102           0 :         return AVERROR_INVALIDDATA;
     103             :     }
     104             : 
     105           1 :     coef_off = 0x54 + par->channels * 8;
     106           1 :     if (bytestream2_get_byteu(&gbc)) // Loop flag
     107           0 :         coef_off += 16;
     108             : 
     109           1 :     if (coef_off + par->channels * (32 + 14) > HEADER_SIZE) {
     110           0 :         return AVERROR_INVALIDDATA;
     111             :     }
     112             : 
     113           1 :     if (ff_alloc_extradata(par, 32 * par->channels)) {
     114           0 :         return AVERROR_INVALIDDATA;
     115             :     }
     116             : 
     117             :     /* Get the ADPCM table */
     118           1 :     bytestream2_seek(&gbc, coef_off, SEEK_SET);
     119           3 :     for (i = 0; i < par->channels; i++) {
     120           2 :         if (bytestream2_get_bufferu(&gbc, par->extradata + i * 32, 32) != 32) {
     121           0 :             return AVERROR_INVALIDDATA;
     122             :         }
     123           2 :         bytestream2_skipu(&gbc, 14);
     124             :     }
     125             : 
     126           1 :     avpriv_set_pts_info(st, 64, 1, par->sample_rate);
     127             : 
     128           1 :     return ret;
     129             : }
     130             : 
     131        1761 : static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt)
     132             : {
     133        1761 :     AVCodecParameters *par = s->streams[0]->codecpar;
     134        1761 :     RedSparkContext *redspark = s->priv_data;
     135        1761 :     uint32_t size = 8 * par->channels;
     136             :     int ret;
     137             : 
     138        1761 :     if (avio_feof(s->pb) || redspark->samples_count == s->streams[0]->duration)
     139           0 :         return AVERROR_EOF;
     140             : 
     141        1761 :     ret = av_get_packet(s->pb, pkt, size);
     142        1761 :     if (ret != size) {
     143           1 :         av_packet_unref(pkt);
     144           1 :         return AVERROR(EIO);
     145             :     }
     146             : 
     147        1760 :     pkt->duration = 14;
     148        1760 :     redspark->samples_count += pkt->duration;
     149        1760 :     pkt->stream_index = 0;
     150             : 
     151        1760 :     return ret;
     152             : }
     153             : 
     154             : AVInputFormat ff_redspark_demuxer = {
     155             :     .name           =   "redspark",
     156             :     .long_name      =   NULL_IF_CONFIG_SMALL("RedSpark"),
     157             :     .priv_data_size =   sizeof(RedSparkContext),
     158             :     .read_probe     =   redspark_probe,
     159             :     .read_header    =   redspark_read_header,
     160             :     .read_packet    =   redspark_read_packet,
     161             :     .extensions     =   "rsd",
     162             : };

Generated by: LCOV version 1.13