uleaddvaudio.c 5.65 KB
Newer Older
1 2 3 4 5 6 7 8
/*****************************************************************************
 * uleaddvaudio.c
 *****************************************************************************
 * Copyright (C) 2012 Laurent Aimar
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or 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
12 13 14 15
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>

#include "../demux/rawdv.h"

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open(vlc_object_t *);
static void Close(vlc_object_t *);

vlc_module_begin()
    set_description(N_("Ulead DV audio decoder"))
42
    set_capability("audio decoder", 50)
43 44 45 46 47
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_ACODEC)
    set_callbacks(Open, Close)
vlc_module_end()

48
typedef struct
49 50 51 52 53 54
{
    date_t end_date;

    bool     is_pal;
    bool     is_12bit;
    uint16_t shuffle[2000];
55
} decoder_sys_t;
56

57 58 59 60
static void Flush(decoder_t *dec)
{
    decoder_sys_t *sys = dec->p_sys;

61
    date_Set(&sys->end_date, VLC_TS_INVALID);
62 63
}

64
static block_t *DecodeBlock(decoder_t *dec, block_t **block_ptr)
65 66 67
{
    decoder_sys_t *sys  = dec->p_sys;

68
    if (!*block_ptr)
69 70 71
        return NULL;

    block_t *block = *block_ptr;
72
    if (block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED)) {
73
        Flush(dec);
74 75 76 77 78
        if (block->i_flags & BLOCK_FLAG_CORRUPTED) {
            block_Release(block);
            *block_ptr = NULL;
            return NULL;
        }
79
    }
80

81
    if (block->i_pts != VLC_TS_INVALID &&
82 83 84
        block->i_pts != date_Get(&sys->end_date))
        date_Set(&sys->end_date, block->i_pts);
    block->i_pts = VLC_TS_INVALID;
85
    if (date_Get(&sys->end_date) == VLC_TS_INVALID) {
86 87 88 89 90
        /* We've just started the stream, wait for the first PTS. */
        block_Release(block);
        return NULL;
    }

91
    const unsigned int block_size = sys->is_pal ? 8640 : 7200;
92 93 94 95 96 97 98 99
    if (block->i_buffer >= block_size) {
        uint8_t *src = block->p_buffer;

        block->i_buffer -= block_size;
        block->p_buffer += block_size;

        int sample_count = dv_get_audio_sample_count(&src[244], sys->is_pal);

100 101
        if( decoder_UpdateAudioFormat(dec))
            return NULL;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        block_t *output = decoder_NewAudioBuffer(dec, sample_count);
        if (!output)
            return NULL;
        output->i_pts    = date_Get(&sys->end_date);
        output->i_length = date_Increment(&sys->end_date, sample_count) - output->i_pts;

        int16_t *dst = (int16_t*)output->p_buffer;
        for (int i = 0; i < sample_count; i++) {
          const uint8_t *v = &src[sys->shuffle[i]];
          if (sys->is_12bit) {
              *dst++ = dv_audio_12to16((v[0] << 4) | ((v[2] >> 4) & 0x0f));
              *dst++ = dv_audio_12to16((v[1] << 4) | ((v[2] >> 0) & 0x0f));
          } else {
              *dst++ = GetWBE(&v[0]);
              *dst++ = GetWBE(&v[sys->is_pal ? 4320 : 3600]);
          }
        }
        return output;
    }
    block_Release(block);
    return NULL;
}

125 126 127 128 129 130 131 132 133 134 135
static int DecodeAudio(decoder_t *dec, block_t *block)
{
    if (block == NULL) /* No Drain */
        return VLCDEC_SUCCESS;

    block_t **block_ptr = &block, *out;
    while ((out = DecodeBlock(dec, block_ptr)) != NULL)
        decoder_QueueAudio(dec,out);
    return VLCDEC_SUCCESS;
}

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
static int Open(vlc_object_t *object)
{
    decoder_t *dec = (decoder_t*)object;

    if (dec->fmt_in.i_codec != VLC_CODEC_ULEAD_DV_AUDIO_NTSC &&
        dec->fmt_in.i_codec != VLC_CODEC_ULEAD_DV_AUDIO_PAL)
        return VLC_EGENERIC;
    if (dec->fmt_in.audio.i_bitspersample != 12 && dec->fmt_in.audio.i_bitspersample != 16)
        return VLC_EGENERIC;
    if (dec->fmt_in.audio.i_channels != 2)
        return VLC_EGENERIC;
    if (dec->fmt_in.audio.i_rate <= 0)
        return VLC_EGENERIC;

    decoder_sys_t *sys = dec->p_sys = malloc(sizeof(*sys));
    if (!sys)
        return VLC_ENOMEM;

    sys->is_pal = dec->fmt_in.i_codec == VLC_CODEC_ULEAD_DV_AUDIO_PAL;
    sys->is_12bit = dec->fmt_in.audio.i_bitspersample == 12;

    date_Init(&sys->end_date, dec->fmt_in.audio.i_rate, 1);

    for (unsigned i = 0; i < sizeof(sys->shuffle) / sizeof(*sys->shuffle); i++) {
        const unsigned a = sys->is_pal ? 18 : 15;
        const unsigned b = 3 * a;
        sys->shuffle[i] = 80 * ((21 * (i % 3) + 9 * (i / 3) + ((i / a) % 3)) % b) +
                          (2 + sys->is_12bit) * (i / b) + 8;
    }

166
    dec->fmt_out.i_codec = VLC_CODEC_S16N;
167 168
    dec->fmt_out.audio.i_rate = dec->fmt_in.audio.i_rate;
    dec->fmt_out.audio.i_channels = 2;
169
    dec->fmt_out.audio.i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
170

171 172
    dec->pf_decode = DecodeAudio;
    dec->pf_flush  = Flush;
173 174 175 176 177 178 179 180 181 182 183

    return VLC_SUCCESS;
}

static void Close(vlc_object_t *object)
{
    decoder_t *dec = (decoder_t *)object;

    free(dec->p_sys);
}