pcm-dvd.c 11.1 KB
Newer Older
1 2
/*
 * LPCM codecs for PCM formats found in Video DVD streams
3
 * Copyright (c) 2013 Christian Schmidt
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
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
 * FFmpeg is distributed in the hope that it will be useful,
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 FFmpeg; if not, write to the Free Software
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
 * LPCM codecs for PCM formats found in Video DVD streams
 */

#include "avcodec.h"
#include "bytestream.h"
#include "internal.h"

typedef struct PCMDVDContext {
    uint32_t last_header;    // Cached header to see if parsing is needed
    int block_size;          // Size of a block of samples in bytes
34
    int last_block_size;     // Size of the last block of samples in bytes
35
    int samples_per_block;   // Number of samples per channel per block
36
    int groups_per_block;    // Number of 20/24-bit sample groups per block
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    uint8_t *extra_samples;  // Pointer to leftover samples from a frame
    int extra_sample_count;  // Number of leftover samples in the buffer
} PCMDVDContext;

static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx)
{
    PCMDVDContext *s = avctx->priv_data;

    /* Invalid header to force parsing of the first header */
    s->last_header = -1;
    /* reserve space for 8 channels, 3 bytes/sample, 4 samples/block */
    if (!(s->extra_samples = av_malloc(8 * 3 * 4)))
        return AVERROR(ENOMEM);

    return 0;
}

static av_cold int pcm_dvd_decode_uninit(AVCodecContext *avctx)
{
    PCMDVDContext *s = avctx->priv_data;

Paul B Mahol's avatar
Paul B Mahol committed
58
    av_freep(&s->extra_samples);
59 60 61 62 63 64 65 66 67 68 69 70 71 72

    return 0;
}

static int pcm_dvd_parse_header(AVCodecContext *avctx, const uint8_t *header)
{
    /* no traces of 44100 and 32000Hz in any commercial software or player */
    static const uint32_t frequencies[4] = { 48000, 96000, 44100, 32000 };
    PCMDVDContext *s = avctx->priv_data;
    int header_int = (header[0] & 0xe0) | (header[1] << 8) | (header[2] << 16);

    /* early exit if the header didn't change apart from the frame number */
    if (s->last_header == header_int)
        return 0;
73
    s->last_header = -1;
74 75

    if (avctx->debug & FF_DEBUG_PICT_INFO)
76
        av_log(avctx, AV_LOG_DEBUG, "pcm_dvd_parse_header: header = %02x%02x%02x\n",
77 78 79 80 81 82 83
                header[0], header[1], header[2]);
    /*
     * header[0] emphasis (1), muse(1), reserved(1), frame number(5)
     * header[1] quant (2), freq(2), reserved(1), channels(3)
     * header[2] dynamic range control (0x80 = off)
     */

84 85 86
    /* Discard potentially existing leftover samples from old channel layout */
    s->extra_sample_count = 0;

87 88 89
    /* get the sample depth and derive the sample format from it */
    avctx->bits_per_coded_sample = 16 + (header[1] >> 6 & 3) * 4;
    if (avctx->bits_per_coded_sample == 28) {
90 91 92
        av_log(avctx, AV_LOG_ERROR,
               "PCM DVD unsupported sample depth %i\n",
               avctx->bits_per_coded_sample);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        return AVERROR_INVALIDDATA;
    }
    avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16
                                                           : AV_SAMPLE_FMT_S32;
    avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;

    /* get the sample rate */
    avctx->sample_rate = frequencies[header[1] >> 4 & 3];

    /* get the number of channels */
    avctx->channels = 1 + (header[1] & 7);
    /* calculate the bitrate */
    avctx->bit_rate = avctx->channels *
                      avctx->sample_rate *
                      avctx->bits_per_coded_sample;

109
    /* 4 samples form a group in 20/24-bit PCM on DVD Video.
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
     * A block is formed by the number of groups that are
     * needed to complete a set of samples for each channel. */
    if (avctx->bits_per_coded_sample == 16) {
        s->samples_per_block = 1;
        s->block_size        = avctx->channels * 2;
    } else {
        switch (avctx->channels) {
        case 1:
        case 2:
        case 4:
            /* one group has all the samples needed */
            s->block_size        = 4 * avctx->bits_per_coded_sample / 8;
            s->samples_per_block = 4 / avctx->channels;
            s->groups_per_block  = 1;
            break;
        case 8:
            /* two groups have all the samples needed */
            s->block_size        = 8 * avctx->bits_per_coded_sample / 8;
            s->samples_per_block = 1;
            s->groups_per_block  = 2;
            break;
        default:
            /* need avctx->channels groups */
            s->block_size        = 4 * avctx->channels *
                                   avctx->bits_per_coded_sample / 8;
            s->samples_per_block = 4;
            s->groups_per_block  = avctx->channels;
            break;
        }
    }

    if (avctx->debug & FF_DEBUG_PICT_INFO)
142
        ff_dlog(avctx,
143
                "pcm_dvd_parse_header: %d channels, %d bits per sample, %d Hz, %"PRId64" bit/s\n",
144
                avctx->channels, avctx->bits_per_coded_sample,
145
                avctx->sample_rate, avctx->bit_rate);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

    s->last_header = header_int;

    return 0;
}

static void *pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src,
                                    void *dst, int blocks)
{
    PCMDVDContext *s = avctx->priv_data;
    int16_t *dst16   = dst;
    int32_t *dst32   = dst;
    GetByteContext gb;
    int i;
    uint8_t t;

    bytestream2_init(&gb, src, blocks * s->block_size);
    switch (avctx->bits_per_coded_sample) {
164
    case 16: {
165 166
#if HAVE_BIGENDIAN
        bytestream2_get_buffer(&gb, dst16, blocks * s->block_size);
167
        dst16 += blocks * s->block_size / 2;
168
#else
169
        int samples = blocks * avctx->channels;
170 171 172 173 174
        do {
            *dst16++ = bytestream2_get_be16u(&gb);
        } while (--samples);
#endif
        return dst16;
175
    }
176
    case 20:
177 178 179 180 181 182 183 184 185 186 187
        if (avctx->channels == 1) {
            do {
                for (i = 2; i; i--) {
                    dst32[0] = bytestream2_get_be16u(&gb) << 16;
                    dst32[1] = bytestream2_get_be16u(&gb) << 16;
                    t = bytestream2_get_byteu(&gb);
                    *dst32++ += (t & 0xf0) << 8;
                    *dst32++ += (t & 0x0f) << 12;
                }
            } while (--blocks);
        } else {
188 189 190 191 192 193 194
        do {
            for (i = s->groups_per_block; i; i--) {
                dst32[0] = bytestream2_get_be16u(&gb) << 16;
                dst32[1] = bytestream2_get_be16u(&gb) << 16;
                dst32[2] = bytestream2_get_be16u(&gb) << 16;
                dst32[3] = bytestream2_get_be16u(&gb) << 16;
                t = bytestream2_get_byteu(&gb);
195 196
                *dst32++ += (t & 0xf0) << 8;
                *dst32++ += (t & 0x0f) << 12;
197
                t = bytestream2_get_byteu(&gb);
198 199
                *dst32++ += (t & 0xf0) << 8;
                *dst32++ += (t & 0x0f) << 12;
200 201
            }
        } while (--blocks);
202
        }
203 204
        return dst32;
    case 24:
205 206 207 208 209 210 211 212 213 214
        if (avctx->channels == 1) {
            do {
                for (i = 2; i; i--) {
                    dst32[0] = bytestream2_get_be16u(&gb) << 16;
                    dst32[1] = bytestream2_get_be16u(&gb) << 16;
                    *dst32++ += bytestream2_get_byteu(&gb) << 8;
                    *dst32++ += bytestream2_get_byteu(&gb) << 8;
                }
            } while (--blocks);
        } else {
215 216 217 218 219 220 221 222 223 224 225 226
        do {
            for (i = s->groups_per_block; i; i--) {
                dst32[0] = bytestream2_get_be16u(&gb) << 16;
                dst32[1] = bytestream2_get_be16u(&gb) << 16;
                dst32[2] = bytestream2_get_be16u(&gb) << 16;
                dst32[3] = bytestream2_get_be16u(&gb) << 16;
                *dst32++ += bytestream2_get_byteu(&gb) << 8;
                *dst32++ += bytestream2_get_byteu(&gb) << 8;
                *dst32++ += bytestream2_get_byteu(&gb) << 8;
                *dst32++ += bytestream2_get_byteu(&gb) << 8;
            }
        } while (--blocks);
227
        }
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        return dst32;
    default:
        return NULL;
    }
}

static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data,
                                int *got_frame_ptr, AVPacket *avpkt)
{
    AVFrame *frame     = data;
    const uint8_t *src = avpkt->data;
    int buf_size       = avpkt->size;
    PCMDVDContext *s   = avctx->priv_data;
    int retval;
    int blocks;
    void *dst;

    if (buf_size < 3) {
        av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
        return AVERROR_INVALIDDATA;
    }

    if ((retval = pcm_dvd_parse_header(avctx, src)))
        return retval;
252
    if (s->last_block_size && s->last_block_size != s->block_size) {
253
        av_log(avctx, AV_LOG_WARNING, "block_size has changed %d != %d\n", s->last_block_size, s->block_size);
254 255 256
        s->extra_sample_count = 0;
    }
    s->last_block_size = s->block_size;
257 258 259 260 261 262 263
    src      += 3;
    buf_size -= 3;

    blocks = (buf_size + s->extra_sample_count) / s->block_size;

    /* get output buffer */
    frame->nb_samples = blocks * s->samples_per_block;
264
    if ((retval = ff_get_buffer(avctx, frame, 0)) < 0)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        return retval;
    dst = frame->data[0];

    /* consume leftover samples from last packet */
    if (s->extra_sample_count) {
        int missing_samples = s->block_size - s->extra_sample_count;
        if (buf_size >= missing_samples) {
            memcpy(s->extra_samples + s->extra_sample_count, src,
                   missing_samples);
            dst = pcm_dvd_decode_samples(avctx, s->extra_samples, dst, 1);
            src += missing_samples;
            buf_size -= missing_samples;
            s->extra_sample_count = 0;
            blocks--;
        } else {
            /* new packet still doesn't have enough samples */
            memcpy(s->extra_samples + s->extra_sample_count, src, buf_size);
            s->extra_sample_count += buf_size;
            return avpkt->size;
        }
    }

    /* decode remaining complete samples */
    if (blocks) {
        pcm_dvd_decode_samples(avctx, src, dst, blocks);
        buf_size -= blocks * s->block_size;
    }

    /* store leftover samples */
    if (buf_size) {
        src += blocks * s->block_size;
        memcpy(s->extra_samples, src, buf_size);
        s->extra_sample_count = buf_size;
    }

    *got_frame_ptr = 1;

    return avpkt->size;
}

AVCodec ff_pcm_dvd_decoder = {
    .name           = "pcm_dvd",
    .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"),
    .type           = AVMEDIA_TYPE_AUDIO,
    .id             = AV_CODEC_ID_PCM_DVD,
    .priv_data_size = sizeof(PCMDVDContext),
    .init           = pcm_dvd_decode_init,
    .decode         = pcm_dvd_decode_frame,
    .close          = pcm_dvd_decode_uninit,
314
    .capabilities   = AV_CODEC_CAP_DR1,
315 316 317 318
    .sample_fmts    = (const enum AVSampleFormat[]) {
        AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
    }
};