LCOV - code coverage report
Current view: top level - src/libavformat - dss.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 141 190 74.2 %
Date: 2017-04-23 07:57:23 Functions: 10 11 90.9 %

          Line data    Source code
       1             : /*
       2             :  * Digital Speech Standard (DSS) demuxer
       3             :  * Copyright (c) 2014 Oleksij Rempel <linux@rempel-privat.de>
       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/attributes.h"
      23             : #include "libavutil/bswap.h"
      24             : #include "libavutil/channel_layout.h"
      25             : #include "libavutil/intreadwrite.h"
      26             : 
      27             : #include "avformat.h"
      28             : #include "internal.h"
      29             : 
      30             : #define DSS_HEAD_OFFSET_AUTHOR        0xc
      31             : #define DSS_AUTHOR_SIZE               16
      32             : 
      33             : #define DSS_HEAD_OFFSET_START_TIME    0x26
      34             : #define DSS_HEAD_OFFSET_END_TIME      0x32
      35             : #define DSS_TIME_SIZE                 12
      36             : 
      37             : #define DSS_HEAD_OFFSET_ACODEC        0x2a4
      38             : #define DSS_ACODEC_DSS_SP             0x0    /* SP mode */
      39             : #define DSS_ACODEC_G723_1             0x2    /* LP mode */
      40             : 
      41             : #define DSS_HEAD_OFFSET_COMMENT       0x31e
      42             : #define DSS_COMMENT_SIZE              64
      43             : 
      44             : #define DSS_BLOCK_SIZE                512
      45             : #define DSS_AUDIO_BLOCK_HEADER_SIZE   6
      46             : #define DSS_FRAME_SIZE                42
      47             : 
      48             : static const uint8_t frame_size[4] = { 24, 20, 4, 1 };
      49             : 
      50             : typedef struct DSSDemuxContext {
      51             :     unsigned int audio_codec;
      52             :     int counter;
      53             :     int swap;
      54             :     int dss_sp_swap_byte;
      55             :     int8_t *dss_sp_buf;
      56             : 
      57             :     int packet_size;
      58             :     int dss_header_size;
      59             : } DSSDemuxContext;
      60             : 
      61        5948 : static int dss_probe(AVProbeData *p)
      62             : {
      63        5948 :     if (   AV_RL32(p->buf) != MKTAG(0x2, 'd', 's', 's')
      64        5946 :         && AV_RL32(p->buf) != MKTAG(0x3, 'd', 's', 's'))
      65        5946 :         return 0;
      66             : 
      67           2 :     return AVPROBE_SCORE_MAX;
      68             : }
      69             : 
      70           2 : static int dss_read_metadata_date(AVFormatContext *s, unsigned int offset,
      71             :                                   const char *key)
      72             : {
      73           2 :     AVIOContext *pb = s->pb;
      74           2 :     char datetime[64], string[DSS_TIME_SIZE + 1] = { 0 };
      75             :     int y, month, d, h, minute, sec;
      76             :     int ret;
      77             : 
      78           2 :     avio_seek(pb, offset, SEEK_SET);
      79             : 
      80           2 :     ret = avio_read(s->pb, string, DSS_TIME_SIZE);
      81           2 :     if (ret < DSS_TIME_SIZE)
      82           0 :         return ret < 0 ? ret : AVERROR_EOF;
      83             : 
      84           2 :     if (sscanf(string, "%2d%2d%2d%2d%2d%2d", &y, &month, &d, &h, &minute, &sec) != 6)
      85           0 :         return AVERROR_INVALIDDATA;
      86             :     /* We deal with a two-digit year here, so set the default date to 2000
      87             :      * and hope it will never be used in the next century. */
      88           2 :     snprintf(datetime, sizeof(datetime), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
      89             :              y + 2000, month, d, h, minute, sec);
      90           2 :     return av_dict_set(&s->metadata, key, datetime, 0);
      91             : }
      92             : 
      93           4 : static int dss_read_metadata_string(AVFormatContext *s, unsigned int offset,
      94             :                                     unsigned int size, const char *key)
      95             : {
      96           4 :     AVIOContext *pb = s->pb;
      97             :     char *value;
      98             :     int ret;
      99             : 
     100           4 :     avio_seek(pb, offset, SEEK_SET);
     101             : 
     102           4 :     value = av_mallocz(size + 1);
     103           4 :     if (!value)
     104           0 :         return AVERROR(ENOMEM);
     105             : 
     106           4 :     ret = avio_read(s->pb, value, size);
     107           4 :     if (ret < size) {
     108           0 :         ret = ret < 0 ? ret : AVERROR_EOF;
     109           0 :         goto exit;
     110             :     }
     111             : 
     112           4 :     ret = av_dict_set(&s->metadata, key, value, 0);
     113             : 
     114             : exit:
     115           4 :     av_free(value);
     116           4 :     return ret;
     117             : }
     118             : 
     119           2 : static int dss_read_header(AVFormatContext *s)
     120             : {
     121           2 :     DSSDemuxContext *ctx = s->priv_data;
     122           2 :     AVIOContext *pb = s->pb;
     123             :     AVStream *st;
     124             :     int ret, version;
     125             : 
     126           2 :     st = avformat_new_stream(s, NULL);
     127           2 :     if (!st)
     128           0 :         return AVERROR(ENOMEM);
     129             : 
     130           2 :     version = avio_r8(pb);
     131           2 :     ctx->dss_header_size = version * DSS_BLOCK_SIZE;
     132             : 
     133           2 :     ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_AUTHOR,
     134             :                                    DSS_AUTHOR_SIZE, "author");
     135           2 :     if (ret)
     136           0 :         return ret;
     137             : 
     138           2 :     ret = dss_read_metadata_date(s, DSS_HEAD_OFFSET_END_TIME, "date");
     139           2 :     if (ret)
     140           0 :         return ret;
     141             : 
     142           2 :     ret = dss_read_metadata_string(s, DSS_HEAD_OFFSET_COMMENT,
     143             :                                    DSS_COMMENT_SIZE, "comment");
     144           2 :     if (ret)
     145           0 :         return ret;
     146             : 
     147           2 :     avio_seek(pb, DSS_HEAD_OFFSET_ACODEC, SEEK_SET);
     148           2 :     ctx->audio_codec = avio_r8(pb);
     149             : 
     150           2 :     if (ctx->audio_codec == DSS_ACODEC_DSS_SP) {
     151           1 :         st->codecpar->codec_id    = AV_CODEC_ID_DSS_SP;
     152           1 :         st->codecpar->sample_rate = 11025;
     153           1 :     } else if (ctx->audio_codec == DSS_ACODEC_G723_1) {
     154           1 :         st->codecpar->codec_id    = AV_CODEC_ID_G723_1;
     155           1 :         st->codecpar->sample_rate = 8000;
     156             :     } else {
     157           0 :         avpriv_request_sample(s, "Support for codec %x in DSS",
     158             :                               ctx->audio_codec);
     159           0 :         return AVERROR_PATCHWELCOME;
     160             :     }
     161             : 
     162           2 :     st->codecpar->codec_type     = AVMEDIA_TYPE_AUDIO;
     163           2 :     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     164           2 :     st->codecpar->channels       = 1;
     165             : 
     166           2 :     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
     167           2 :     st->start_time = 0;
     168             : 
     169             :     /* Jump over header */
     170             : 
     171           2 :     if (avio_seek(pb, ctx->dss_header_size, SEEK_SET) != ctx->dss_header_size)
     172           0 :         return AVERROR(EIO);
     173             : 
     174           2 :     ctx->counter = 0;
     175           2 :     ctx->swap    = 0;
     176             : 
     177           2 :     ctx->dss_sp_buf = av_malloc(DSS_FRAME_SIZE + 1);
     178           2 :     if (!ctx->dss_sp_buf)
     179           0 :         return AVERROR(ENOMEM);
     180             : 
     181           2 :     return 0;
     182             : }
     183             : 
     184           8 : static void dss_skip_audio_header(AVFormatContext *s, AVPacket *pkt)
     185             : {
     186           8 :     DSSDemuxContext *ctx = s->priv_data;
     187           8 :     AVIOContext *pb = s->pb;
     188             : 
     189           8 :     avio_skip(pb, DSS_AUDIO_BLOCK_HEADER_SIZE);
     190           8 :     ctx->counter += DSS_BLOCK_SIZE - DSS_AUDIO_BLOCK_HEADER_SIZE;
     191           8 : }
     192             : 
     193          50 : static void dss_sp_byte_swap(DSSDemuxContext *ctx,
     194             :                              uint8_t *dst, const uint8_t *src)
     195             : {
     196             :     int i;
     197             : 
     198          50 :     if (ctx->swap) {
     199         525 :         for (i = 3; i < DSS_FRAME_SIZE; i += 2)
     200         500 :             dst[i] = src[i];
     201             : 
     202         525 :         for (i = 0; i < DSS_FRAME_SIZE - 2; i += 2)
     203         500 :             dst[i] = src[i + 4];
     204             : 
     205          25 :         dst[1] = ctx->dss_sp_swap_byte;
     206             :     } else {
     207          25 :         memcpy(dst, src, DSS_FRAME_SIZE);
     208          25 :         ctx->dss_sp_swap_byte = src[DSS_FRAME_SIZE - 2];
     209             :     }
     210             : 
     211             :     /* make sure byte 40 is always 0 */
     212          50 :     dst[DSS_FRAME_SIZE - 2] = 0;
     213          50 :     ctx->swap             ^= 1;
     214          50 : }
     215             : 
     216          50 : static int dss_sp_read_packet(AVFormatContext *s, AVPacket *pkt)
     217             : {
     218          50 :     DSSDemuxContext *ctx = s->priv_data;
     219          50 :     AVStream *st = s->streams[0];
     220          50 :     int read_size, ret, offset = 0, buff_offset = 0;
     221          50 :     int64_t pos = avio_tell(s->pb);
     222             : 
     223          50 :     if (ctx->counter == 0)
     224           2 :         dss_skip_audio_header(s, pkt);
     225             : 
     226          50 :     if (ctx->swap) {
     227          25 :         read_size   = DSS_FRAME_SIZE - 2;
     228          25 :         buff_offset = 3;
     229             :     } else
     230          25 :         read_size = DSS_FRAME_SIZE;
     231             : 
     232          50 :     ctx->counter -= read_size;
     233          50 :     ctx->packet_size = DSS_FRAME_SIZE - 1;
     234             : 
     235          50 :     ret = av_new_packet(pkt, DSS_FRAME_SIZE);
     236          50 :     if (ret < 0)
     237           0 :         return ret;
     238             : 
     239          50 :     pkt->duration     = 264;
     240          50 :     pkt->pos = pos;
     241          50 :     pkt->stream_index = 0;
     242          50 :     s->bit_rate = 8LL * ctx->packet_size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
     243             : 
     244          50 :     if (ctx->counter < 0) {
     245           3 :         int size2 = ctx->counter + read_size;
     246             : 
     247           3 :         ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
     248             :                         size2 - offset);
     249           3 :         if (ret < size2 - offset)
     250           0 :             goto error_eof;
     251             : 
     252           3 :         dss_skip_audio_header(s, pkt);
     253           3 :         offset = size2;
     254             :     }
     255             : 
     256          50 :     ret = avio_read(s->pb, ctx->dss_sp_buf + offset + buff_offset,
     257             :                     read_size - offset);
     258          50 :     if (ret < read_size - offset)
     259           0 :         goto error_eof;
     260             : 
     261          50 :     dss_sp_byte_swap(ctx, pkt->data, ctx->dss_sp_buf);
     262             : 
     263          50 :     if (ctx->dss_sp_swap_byte < 0) {
     264           0 :         ret = AVERROR(EAGAIN);
     265           0 :         goto error_eof;
     266             :     }
     267             : 
     268          50 :     return pkt->size;
     269             : 
     270             : error_eof:
     271           0 :     av_packet_unref(pkt);
     272           0 :     return ret < 0 ? ret : AVERROR_EOF;
     273             : }
     274             : 
     275          50 : static int dss_723_1_read_packet(AVFormatContext *s, AVPacket *pkt)
     276             : {
     277          50 :     DSSDemuxContext *ctx = s->priv_data;
     278          50 :     AVStream *st = s->streams[0];
     279             :     int size, byte, ret, offset;
     280          50 :     int64_t pos = avio_tell(s->pb);
     281             : 
     282          50 :     if (ctx->counter == 0)
     283           1 :         dss_skip_audio_header(s, pkt);
     284             : 
     285             :     /* We make one byte-step here. Don't forget to add offset. */
     286          50 :     byte = avio_r8(s->pb);
     287          50 :     if (byte == 0xff)
     288           0 :         return AVERROR_INVALIDDATA;
     289             : 
     290          50 :     size = frame_size[byte & 3];
     291             : 
     292          50 :     ctx->packet_size = size;
     293          50 :     ctx->counter -= size;
     294             : 
     295          50 :     ret = av_new_packet(pkt, size);
     296          50 :     if (ret < 0)
     297           0 :         return ret;
     298          50 :     pkt->pos = pos;
     299             : 
     300          50 :     pkt->data[0]  = byte;
     301          50 :     offset        = 1;
     302          50 :     pkt->duration = 240;
     303          50 :     s->bit_rate = 8LL * size * st->codecpar->sample_rate * 512 / (506 * pkt->duration);
     304             : 
     305          50 :     pkt->stream_index = 0;
     306             : 
     307          50 :     if (ctx->counter < 0) {
     308           2 :         int size2 = ctx->counter + size;
     309             : 
     310           2 :         ret = avio_read(s->pb, pkt->data + offset,
     311             :                         size2 - offset);
     312           2 :         if (ret < size2 - offset) {
     313           0 :             av_packet_unref(pkt);
     314           0 :             return ret < 0 ? ret : AVERROR_EOF;
     315             :         }
     316             : 
     317           2 :         dss_skip_audio_header(s, pkt);
     318           2 :         offset = size2;
     319             :     }
     320             : 
     321          50 :     ret = avio_read(s->pb, pkt->data + offset, size - offset);
     322          50 :     if (ret < size - offset) {
     323           0 :         av_packet_unref(pkt);
     324           0 :         return ret < 0 ? ret : AVERROR_EOF;
     325             :     }
     326             : 
     327          50 :     return pkt->size;
     328             : }
     329             : 
     330         100 : static int dss_read_packet(AVFormatContext *s, AVPacket *pkt)
     331             : {
     332         100 :     DSSDemuxContext *ctx = s->priv_data;
     333             : 
     334         100 :     if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
     335          50 :         return dss_sp_read_packet(s, pkt);
     336             :     else
     337          50 :         return dss_723_1_read_packet(s, pkt);
     338             : }
     339             : 
     340           2 : static int dss_read_close(AVFormatContext *s)
     341             : {
     342           2 :     DSSDemuxContext *ctx = s->priv_data;
     343             : 
     344           2 :     av_freep(&ctx->dss_sp_buf);
     345             : 
     346           2 :     return 0;
     347             : }
     348             : 
     349           0 : static int dss_read_seek(AVFormatContext *s, int stream_index,
     350             :                          int64_t timestamp, int flags)
     351             : {
     352           0 :     DSSDemuxContext *ctx = s->priv_data;
     353             :     int64_t ret, seekto;
     354             :     uint8_t header[DSS_AUDIO_BLOCK_HEADER_SIZE];
     355             :     int offset;
     356             : 
     357           0 :     if (ctx->audio_codec == DSS_ACODEC_DSS_SP)
     358           0 :         seekto = timestamp / 264 * 41 / 506 * 512;
     359             :     else
     360           0 :         seekto = timestamp / 240 * ctx->packet_size / 506 * 512;
     361             : 
     362           0 :     if (seekto < 0)
     363           0 :         seekto = 0;
     364             : 
     365           0 :     seekto += ctx->dss_header_size;
     366             : 
     367           0 :     ret = avio_seek(s->pb, seekto, SEEK_SET);
     368           0 :     if (ret < 0)
     369           0 :         return ret;
     370             : 
     371           0 :     avio_read(s->pb, header, DSS_AUDIO_BLOCK_HEADER_SIZE);
     372           0 :     ctx->swap = !!(header[0] & 0x80);
     373           0 :     offset = 2*header[1] + 2*ctx->swap;
     374           0 :     if (offset < DSS_AUDIO_BLOCK_HEADER_SIZE)
     375           0 :         return AVERROR_INVALIDDATA;
     376           0 :     if (offset == DSS_AUDIO_BLOCK_HEADER_SIZE) {
     377           0 :         ctx->counter = 0;
     378           0 :         offset = avio_skip(s->pb, -DSS_AUDIO_BLOCK_HEADER_SIZE);
     379             :     } else {
     380           0 :         ctx->counter = DSS_BLOCK_SIZE - offset;
     381           0 :         offset = avio_skip(s->pb, offset - DSS_AUDIO_BLOCK_HEADER_SIZE);
     382             :     }
     383           0 :     ctx->dss_sp_swap_byte = -1;
     384           0 :     return 0;
     385             : }
     386             : 
     387             : 
     388             : AVInputFormat ff_dss_demuxer = {
     389             :     .name           = "dss",
     390             :     .long_name      = NULL_IF_CONFIG_SMALL("Digital Speech Standard (DSS)"),
     391             :     .priv_data_size = sizeof(DSSDemuxContext),
     392             :     .read_probe     = dss_probe,
     393             :     .read_header    = dss_read_header,
     394             :     .read_packet    = dss_read_packet,
     395             :     .read_close     = dss_read_close,
     396             :     .read_seek      = dss_read_seek,
     397             :     .extensions     = "dss"
     398             : };

Generated by: LCOV version 1.13