spdifdec.c 7.1 KB
Newer Older
Anssi Hannula's avatar
Anssi Hannula committed
1 2 3 4
/*
 * IEC 61937 demuxer
 * Copyright (c) 2010 Anssi Hannula <anssi.hannula at iki.fi>
 *
5
 * This file is part of Libav.
Anssi Hannula's avatar
Anssi Hannula committed
6
 *
7
 * Libav is free software; you can redistribute it and/or
Anssi Hannula's avatar
Anssi Hannula committed
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
Anssi Hannula's avatar
Anssi Hannula committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
Anssi Hannula's avatar
Anssi Hannula committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * IEC 61937 demuxer, used for compressed data in S/PDIF
 * @author Anssi Hannula
 */

#include "avformat.h"
#include "spdif.h"
#include "libavcodec/ac3.h"
#include "libavcodec/aacadtsdec.h"

static int spdif_get_offset_and_codec(AVFormatContext *s,
34
                                      enum IEC61937DataType data_type,
Anssi Hannula's avatar
Anssi Hannula committed
35 36 37 38 39 40 41
                                      const char *buf, int *offset,
                                      enum CodecID *codec)
{
    AACADTSHeaderInfo aac_hdr;
    GetBitContext gbc;

    switch (data_type & 0xff) {
42
    case IEC61937_AC3:
Anssi Hannula's avatar
Anssi Hannula committed
43 44 45
        *offset = AC3_FRAME_SIZE << 2;
        *codec = CODEC_ID_AC3;
        break;
46
    case IEC61937_MPEG1_LAYER1:
Anssi Hannula's avatar
Anssi Hannula committed
47 48 49
        *offset = spdif_mpeg_pkt_offset[1][0];
        *codec = CODEC_ID_MP1;
        break;
50
    case IEC61937_MPEG1_LAYER23:
Anssi Hannula's avatar
Anssi Hannula committed
51 52 53
        *offset = spdif_mpeg_pkt_offset[1][0];
        *codec = CODEC_ID_MP3;
        break;
54
    case IEC61937_MPEG2_EXT:
Anssi Hannula's avatar
Anssi Hannula committed
55 56 57
        *offset = 4608;
        *codec = CODEC_ID_MP3;
        break;
58
    case IEC61937_MPEG2_AAC:
Anssi Hannula's avatar
Anssi Hannula committed
59 60 61 62 63 64 65 66 67
        init_get_bits(&gbc, buf, AAC_ADTS_HEADER_SIZE * 8);
        if (ff_aac_parse_header(&gbc, &aac_hdr)) {
            if (s) /* be silent during a probe */
                av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
            return AVERROR_INVALIDDATA;
        }
        *offset = aac_hdr.samples << 2;
        *codec = CODEC_ID_AAC;
        break;
68
    case IEC61937_MPEG2_LAYER1_LSF:
Anssi Hannula's avatar
Anssi Hannula committed
69 70 71
        *offset = spdif_mpeg_pkt_offset[0][0];
        *codec = CODEC_ID_MP1;
        break;
72
    case IEC61937_MPEG2_LAYER2_LSF:
Anssi Hannula's avatar
Anssi Hannula committed
73 74 75
        *offset = spdif_mpeg_pkt_offset[0][1];
        *codec = CODEC_ID_MP2;
        break;
76
    case IEC61937_MPEG2_LAYER3_LSF:
Anssi Hannula's avatar
Anssi Hannula committed
77 78 79
        *offset = spdif_mpeg_pkt_offset[0][2];
        *codec = CODEC_ID_MP3;
        break;
80
    case IEC61937_DTS1:
Anssi Hannula's avatar
Anssi Hannula committed
81 82 83
        *offset = 2048;
        *codec = CODEC_ID_DTS;
        break;
84
    case IEC61937_DTS2:
Anssi Hannula's avatar
Anssi Hannula committed
85 86 87
        *offset = 4096;
        *codec = CODEC_ID_DTS;
        break;
88
    case IEC61937_DTS3:
Anssi Hannula's avatar
Anssi Hannula committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        *offset = 8192;
        *codec = CODEC_ID_DTS;
        break;
    default:
        if (s) { /* be silent during a probe */
            av_log(s, AV_LOG_WARNING, "Data type 0x%04x", data_type);
            av_log_missing_feature(s, " in IEC 61937 is", 1);
        }
        return AVERROR_PATCHWELCOME;
    }
    return 0;
}

/* Largest offset between bursts we currently handle, i.e. AAC with
   aac_hdr.samples = 4096 */
#define SPDIF_MAX_OFFSET 16384

static int spdif_probe(AVProbeData *p)
{
    const uint8_t *buf = p->buf;
    const uint8_t *probe_end = p->buf + FFMIN(2 * SPDIF_MAX_OFFSET, p->buf_size - 1);
    const uint8_t *expected_code = buf + 7;
    uint32_t state = 0;
    int sync_codes = 0;
    int consecutive_codes = 0;
    int offset;
    enum CodecID codec;

    for (; buf < probe_end; buf++) {
        state = (state << 8) | *buf;

        if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
                && buf[1] < 0x37) {
            sync_codes++;

            if (buf == expected_code) {
                if (++consecutive_codes >= 2)
                    return AVPROBE_SCORE_MAX;
            } else
                consecutive_codes = 0;

            if (buf + 4 + AAC_ADTS_HEADER_SIZE > p->buf + p->buf_size)
                break;

            /* continue probing to find more sync codes */
            probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p->buf + p->buf_size - 1);

            /* skip directly to the next sync code */
            if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
                                            &buf[5], &offset, &codec)) {
                if (buf + offset >= p->buf + p->buf_size)
                    break;
                expected_code = buf + offset;
                buf = expected_code - 7;
            }
        }
    }

    if (!sync_codes)
        return 0;

    if (sync_codes >= 6)
        /* good amount of sync codes but with unexpected offsets */
        return AVPROBE_SCORE_MAX / 2;

    /* some sync codes were found */
    return AVPROBE_SCORE_MAX / 8;
}

static int spdif_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
    s->ctx_flags |= AVFMTCTX_NOHEADER;
    return 0;
}

static int spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
{
166
    AVIOContext *pb = s->pb;
167
    enum IEC61937DataType data_type;
Anssi Hannula's avatar
Anssi Hannula committed
168 169 170 171 172
    enum CodecID codec_id;
    uint32_t state = 0;
    int pkt_size_bits, offset, ret;

    while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
173
        state = (state << 8) | avio_r8(pb);
Anton Khirnov's avatar
Anton Khirnov committed
174
        if (pb->eof_reached)
Anssi Hannula's avatar
Anssi Hannula committed
175 176 177
            return AVERROR_EOF;
    }

178 179
    data_type = avio_rl16(pb);
    pkt_size_bits = avio_rl16(pb);
Anssi Hannula's avatar
Anssi Hannula committed
180 181 182 183 184 185 186 187

    if (pkt_size_bits % 16)
        av_log_ask_for_sample(s, "Packet does not end to a 16-bit boundary.");

    ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
    if (ret)
        return ret;

188
    pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
Anssi Hannula's avatar
Anssi Hannula committed
189

190
    if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
Anssi Hannula's avatar
Anssi Hannula committed
191 192 193 194 195 196 197 198 199 200 201 202 203
        av_free_packet(pkt);
        return AVERROR_EOF;
    }
    ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);

    ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
                                     &offset, &codec_id);
    if (ret) {
        av_free_packet(pkt);
        return ret;
    }

    /* skip over the padding to the beginning of the next frame */
204
    avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
Anssi Hannula's avatar
Anssi Hannula committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

    if (!s->nb_streams) {
        /* first packet, create a stream */
        AVStream *st = av_new_stream(s, 0);
        if (!st) {
            av_free_packet(pkt);
            return AVERROR(ENOMEM);
        }
        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
        st->codec->codec_id = codec_id;
    } else if (codec_id != s->streams[0]->codec->codec_id) {
        av_log_missing_feature(s, "codec change in IEC 61937", 0);
        return AVERROR_PATCHWELCOME;
    }

    if (!s->bit_rate && s->streams[0]->codec->sample_rate)
        /* stream bitrate matches 16-bit stereo PCM bitrate for currently
           supported codecs */
        s->bit_rate = 2 * 16 * s->streams[0]->codec->sample_rate;

    return 0;
}

228
AVInputFormat ff_spdif_demuxer = {
Anssi Hannula's avatar
Anssi Hannula committed
229 230 231 232 233 234 235 236
    "spdif",
    NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
    0,
    spdif_probe,
    spdif_read_header,
    spdif_read_packet,
    .flags = AVFMT_GENERIC_INDEX,
};