LCOV - code coverage report
Current view: top level - libavformat - sdsdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 70 4.3 %
Date: 2017-12-15 18:13:28 Functions: 1 6 16.7 %

          Line data    Source code
       1             : /*
       2             :  * MIDI Sample Dump Standard format demuxer
       3             :  * Copyright (c) 2017 Paul B Mahol
       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 "libavutil/intreadwrite.h"
      23             : #include "avformat.h"
      24             : #include "internal.h"
      25             : 
      26             : typedef struct SDSContext {
      27             :     uint8_t data[120];
      28             :     int bit_depth;
      29             :     int size;
      30             :     void (*read_block)(const uint8_t *src, uint32_t *dst);
      31             : } SDSContext;
      32             : 
      33        6130 : static int sds_probe(AVProbeData *p)
      34             : {
      35        6130 :     if (AV_RB32(p->buf) == 0xF07E0001 && p->buf[20] == 0xF7 &&
      36           0 :         p->buf[6] >= 8 && p->buf[6] <= 28)
      37           0 :         return AVPROBE_SCORE_EXTENSION;
      38        6130 :     return 0;
      39             : }
      40             : 
      41           0 : static void byte2_read(const uint8_t *src, uint32_t *dst)
      42             : {
      43             :     int i;
      44             : 
      45           0 :     for (i = 0; i < 120; i += 2) {
      46           0 :         unsigned sample = (src[i + 0] << 25) + (src[i + 1] << 18);
      47             : 
      48           0 :         dst[i / 2] = sample;
      49             :     }
      50           0 : }
      51             : 
      52           0 : static void byte3_read(const uint8_t *src, uint32_t *dst)
      53             : {
      54             :     int i;
      55             : 
      56           0 :     for (i = 0; i < 120; i += 3) {
      57             :         unsigned sample;
      58             : 
      59           0 :         sample = (src[i + 0] << 25) | (src[i + 1] << 18) | (src[i + 2] << 11);
      60           0 :         dst[i / 3] = sample;
      61             :     }
      62           0 : }
      63             : 
      64           0 : static void byte4_read(const uint8_t *src, uint32_t *dst)
      65             : {
      66             :     int i;
      67             : 
      68           0 :     for (i = 0; i < 120; i += 4) {
      69             :         unsigned sample;
      70             : 
      71           0 :         sample = (src[i + 0] << 25) | (src[i + 1] << 18) | (src[i + 2] << 11) | (src[i + 3] << 4);
      72           0 :         dst[i / 4] = sample;
      73             :     }
      74           0 : }
      75             : 
      76             : #define SDS_3BYTE_TO_INT_DECODE(x) (((x) & 0x7F) | (((x) & 0x7F00) >> 1) | (((x) & 0x7F0000) >> 2))
      77             : 
      78           0 : static int sds_read_header(AVFormatContext *ctx)
      79             : {
      80           0 :     SDSContext *s = ctx->priv_data;
      81             :     unsigned sample_period;
      82           0 :     AVIOContext *pb = ctx->pb;
      83             :     AVStream *st;
      84             : 
      85           0 :     st = avformat_new_stream(ctx, NULL);
      86           0 :     if (!st)
      87           0 :         return AVERROR(ENOMEM);
      88             : 
      89           0 :     avio_skip(pb, 4);
      90           0 :     avio_skip(pb, 2);
      91             : 
      92           0 :     s->bit_depth = avio_r8(pb);
      93           0 :     if (s->bit_depth < 8 || s->bit_depth > 28)
      94           0 :         return AVERROR_INVALIDDATA;
      95             : 
      96           0 :     if (s->bit_depth < 14) {
      97           0 :         s->read_block = byte2_read;
      98           0 :         s->size = 60 * 4;
      99           0 :     } else if (s->bit_depth < 21) {
     100           0 :         s->read_block = byte3_read;
     101           0 :         s->size = 40 * 4;
     102             :     } else {
     103           0 :         s->read_block = byte4_read;
     104           0 :         s->size = 30 * 4;
     105             :     }
     106           0 :     st->codecpar->codec_id = AV_CODEC_ID_PCM_U32LE;
     107             : 
     108           0 :     sample_period = avio_rl24(pb);
     109           0 :     sample_period = SDS_3BYTE_TO_INT_DECODE(sample_period);
     110           0 :     avio_skip(pb, 11);
     111             : 
     112           0 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     113           0 :     st->codecpar->channels = 1;
     114           0 :     st->codecpar->sample_rate = sample_period ? 1000000000 / sample_period : 16000;
     115           0 :     st->duration = (avio_size(pb) - 21) / (127) * s->size / 4;
     116             : 
     117           0 :     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
     118             : 
     119           0 :     return 0;
     120             : }
     121             : 
     122           0 : static int sds_read_packet(AVFormatContext *ctx, AVPacket *pkt)
     123             : {
     124           0 :     SDSContext *s = ctx->priv_data;
     125           0 :     AVIOContext *pb = ctx->pb;
     126             :     int64_t pos;
     127             :     int ret;
     128             : 
     129           0 :     if (avio_feof(pb))
     130           0 :         return AVERROR_EOF;
     131             : 
     132           0 :     pos = avio_tell(pb);
     133           0 :     if (avio_rb16(pb) != 0xF07E)
     134           0 :         return AVERROR_INVALIDDATA;
     135           0 :     avio_skip(pb, 3);
     136             : 
     137           0 :     ret = av_new_packet(pkt, s->size);
     138           0 :     if (ret < 0)
     139           0 :         return ret;
     140             : 
     141           0 :     ret = avio_read(pb, s->data, 120);
     142             : 
     143           0 :     s->read_block(s->data, (uint32_t *)pkt->data);
     144             : 
     145           0 :     avio_skip(pb, 1); // checksum
     146           0 :     if (avio_r8(pb) != 0xF7)
     147           0 :         return AVERROR_INVALIDDATA;
     148             : 
     149           0 :     pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
     150           0 :     pkt->stream_index = 0;
     151           0 :     pkt->pos = pos;
     152             : 
     153           0 :     return ret;
     154             : }
     155             : 
     156             : AVInputFormat ff_sds_demuxer = {
     157             :     .name           = "sds",
     158             :     .long_name      = NULL_IF_CONFIG_SMALL("MIDI Sample Dump Standard"),
     159             :     .priv_data_size = sizeof(SDSContext),
     160             :     .read_probe     = sds_probe,
     161             :     .read_header    = sds_read_header,
     162             :     .read_packet    = sds_read_packet,
     163             :     .extensions     = "sds",
     164             :     .flags          = AVFMT_GENERIC_INDEX,
     165             : };

Generated by: LCOV version 1.13