dtstospdif.c 7.82 KB
Newer Older
Jon Lech Johansen's avatar
Jon Lech Johansen committed
1
2
3
/*****************************************************************************
 * dtstospdif.c : encapsulates DTS frames into S/PDIF packets
 *****************************************************************************
4
 * Copyright (C) 2003-2009 the VideoLAN team
zorglub's avatar
zorglub committed
5
 * $Id$
Jon Lech Johansen's avatar
Jon Lech Johansen committed
6
7
 *
 * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
8
9
10
11
12
 *          Gildas Bazin
 *          Derk-Jan Hartman
 *          Pierre d'Herbemont
 *          Rémi Denis-Courmont
 *          Rafaël Carré
Jon Lech Johansen's avatar
Jon Lech Johansen committed
13
14
15
16
17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
18
 *
Jon Lech Johansen's avatar
Jon Lech Johansen committed
19
20
21
22
23
24
25
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Jon Lech Johansen's avatar
Jon Lech Johansen committed
27
28
29
30
31
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
32
33
34
35
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

36
#include <vlc_common.h>
37
#include <vlc_plugin.h>
Jon Lech Johansen's avatar
Jon Lech Johansen committed
38

zorglub's avatar
zorglub committed
39
#include <vlc_aout.h>
40
#include <vlc_filter.h>
Jon Lech Johansen's avatar
Jon Lech Johansen committed
41

gbazin's avatar
   
gbazin committed
42
43
44
/*****************************************************************************
 * Local structures
 *****************************************************************************/
45
struct filter_sys_t
gbazin's avatar
   
gbazin committed
46
{
47
48
    mtime_t start_date;

49
    /* 3 DTS frames (max 2048) have to be packed into an S/PDIF frame (6144).
gbazin's avatar
   
gbazin committed
50
51
     * We accumulate DTS frames from the decoder until we have enough to
     * send. */
52
    size_t i_frame_size;
gbazin's avatar
   
gbazin committed
53
    uint8_t *p_buf;
54
    unsigned i_frames;
gbazin's avatar
   
gbazin committed
55
56
};

Jon Lech Johansen's avatar
Jon Lech Johansen committed
57
58
59
60
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create    ( vlc_object_t * );
gbazin's avatar
   
gbazin committed
61
static void Close     ( vlc_object_t * );
62
static block_t *DoWork( filter_t *, block_t * );
Jon Lech Johansen's avatar
Jon Lech Johansen committed
63
64
65
66

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
67
68
69
70
vlc_module_begin ()
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_MISC )
    set_description( N_("Audio filter for DTS->S/PDIF encapsulation") )
71
    set_capability( "audio converter", 10 )
72
73
    set_callbacks( Create, Close )
vlc_module_end ()
Jon Lech Johansen's avatar
Jon Lech Johansen committed
74
75
76
77
78
79

/*****************************************************************************
 * Create:
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
80
81
    filter_t * p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys;
Jon Lech Johansen's avatar
Jon Lech Johansen committed
82

83
84
85
    if( p_filter->fmt_in.audio.i_format != VLC_CODEC_DTS ||
        ( p_filter->fmt_out.audio.i_format != VLC_CODEC_SPDIFL &&
          p_filter->fmt_out.audio.i_format != VLC_CODEC_SPDIFB ) )
Jon Lech Johansen's avatar
Jon Lech Johansen committed
86
    {
87
        return VLC_EGENERIC;
Jon Lech Johansen's avatar
Jon Lech Johansen committed
88
89
    }

gbazin's avatar
   
gbazin committed
90
    /* Allocate the memory needed to store the module's structure */
91
92
    p_sys = p_filter->p_sys = malloc( sizeof(*p_sys) );
    if( !p_sys )
gbazin's avatar
   
gbazin committed
93
        return VLC_ENOMEM;
94

95
96
97
    p_sys->p_buf = NULL;
    p_sys->i_frame_size = 0;
    p_sys->i_frames = 0;
gbazin's avatar
   
gbazin committed
98

99
    p_filter->pf_audio_filter = DoWork;
Jon Lech Johansen's avatar
Jon Lech Johansen committed
100

101
    return VLC_SUCCESS;
Jon Lech Johansen's avatar
Jon Lech Johansen committed
102
103
}

gbazin's avatar
   
gbazin committed
104
105
106
107
108
/*****************************************************************************
 * Close: free our resources
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
109
110
111
    filter_t * p_filter = (filter_t *)p_this;

    free( p_filter->p_sys->p_buf );
gbazin's avatar
   
gbazin committed
112
113
114
    free( p_filter->p_sys );
}

Jon Lech Johansen's avatar
Jon Lech Johansen committed
115
116
117
/*****************************************************************************
 * DoWork: convert a buffer
 *****************************************************************************/
118
static block_t *DoWork( filter_t * p_filter, block_t * p_in_buf )
Jon Lech Johansen's avatar
Jon Lech Johansen committed
119
{
120
    uint32_t i_ac5_spdif_type = 0;
gbazin's avatar
   
gbazin committed
121
    uint16_t i_fz = p_in_buf->i_nb_samples * 4;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
122
    uint16_t i_frame, i_length = p_in_buf->i_buffer;
123
124
    static const uint8_t p_sync_le[6] = { 0x72, 0xF8, 0x1F, 0x4E, 0x00, 0x00 };
    static const uint8_t p_sync_be[6] = { 0xF8, 0x72, 0x4E, 0x1F, 0x00, 0x00 };
Jon Lech Johansen's avatar
Jon Lech Johansen committed
125

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
126
    if( p_in_buf->i_buffer != p_filter->p_sys->i_frame_size )
gbazin's avatar
   
gbazin committed
127
128
    {
        /* Frame size changed, reset everything */
129
        msg_Warn( p_filter, "Frame size changed from %zu to %zu, "
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
130
                          "resetting everything.",
131
                  p_filter->p_sys->i_frame_size, p_in_buf->i_buffer );
132

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
133
        p_filter->p_sys->i_frame_size = p_in_buf->i_buffer;
134
        p_filter->p_sys->p_buf = xrealloc( p_filter->p_sys->p_buf,
135
                                                  p_in_buf->i_buffer * 3 );
gbazin's avatar
   
gbazin committed
136
137
138
139
        p_filter->p_sys->i_frames = 0;
    }

    /* Backup frame */
140
    /* TODO: keeping the blocks in a list would save one memcpy */
Rafaël Carré's avatar
Rafaël Carré committed
141
    memcpy( p_filter->p_sys->p_buf + p_in_buf->i_buffer *
142
                  p_filter->p_sys->i_frames,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
143
                p_in_buf->p_buffer, p_in_buf->i_buffer );
gbazin's avatar
   
gbazin committed
144
145
146
147
148

    p_filter->p_sys->i_frames++;

    if( p_filter->p_sys->i_frames < 3 )
    {
149
        if( p_filter->p_sys->i_frames == 1 )
gbazin's avatar
   
gbazin committed
150
            /* We'll need the starting date */
151
            p_filter->p_sys->start_date = p_in_buf->i_pts;
gbazin's avatar
   
gbazin committed
152

gbazin's avatar
   
gbazin committed
153
        /* Not enough data */
154
155
        block_Release( p_in_buf );
        return NULL;
gbazin's avatar
   
gbazin committed
156
157
158
    }

    p_filter->p_sys->i_frames = 0;
159
160
161
162
    block_t *p_out_buf = filter_NewAudioBuffer( p_filter,
                                                12 * p_in_buf->i_nb_samples );
    if( !p_out_buf )
        goto out;
gbazin's avatar
   
gbazin committed
163

Jon Lech Johansen's avatar
Jon Lech Johansen committed
164
165
    for( i_frame = 0; i_frame < 3; i_frame++ )
    {
166
        uint16_t i_length_padded = i_length;
167
168
        uint8_t * p_out = p_out_buf->p_buffer + (i_frame * i_fz);
        uint8_t * p_in = p_filter->p_sys->p_buf + (i_frame * i_length);
Jon Lech Johansen's avatar
Jon Lech Johansen committed
169

gbazin's avatar
   
gbazin committed
170
        switch( p_in_buf->i_nb_samples )
Jon Lech Johansen's avatar
Jon Lech Johansen committed
171
        {
172
173
174
            case  512: i_ac5_spdif_type = 0x0B; break;
            case 1024: i_ac5_spdif_type = 0x0C; break;
            case 2048: i_ac5_spdif_type = 0x0D; break;
Jon Lech Johansen's avatar
Jon Lech Johansen committed
175
176
        }

177
        /* Copy the S/PDIF headers. */
178
        if( p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB )
179
        {
Rafaël Carré's avatar
Rafaël Carré committed
180
            memcpy( p_out, p_sync_be, 6 );
181
            p_out[5] = i_ac5_spdif_type;
182
            SetWBE( p_out + 6, i_length << 3 );
183
184
185
        }
        else
        {
Rafaël Carré's avatar
Rafaël Carré committed
186
            memcpy( p_out, p_sync_le, 6 );
187
            p_out[4] = i_ac5_spdif_type;
188
            SetWLE( p_out + 6, i_length << 3 );
189
        }
Jon Lech Johansen's avatar
Jon Lech Johansen committed
190

191
192
        if( ( (p_in[0] == 0x1F || p_in[0] == 0x7F) && p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFL ) ||
            ( (p_in[0] == 0xFF || p_in[0] == 0xFE) && p_filter->fmt_out.audio.i_format == VLC_CODEC_SPDIFB ) )
gbazin's avatar
   
gbazin committed
193
        {
194
            /* We are dealing with a big endian bitstream and a little endian output
195
196
             * or a little endian bitstream and a big endian output.
             * Byteswap the stream */
gbazin's avatar
   
gbazin committed
197
            swab( p_in, p_out + 8, i_length );
Laurent Aimar's avatar
Laurent Aimar committed
198

199
200
201
202
203
204
205
            /* If i_length is odd, we have to adjust swapping a bit.. */
            if( i_length & 1 )
            {
                p_out[8+i_length-1] = 0;
                p_out[8+i_length] = p_in[i_length-1];
                i_length_padded++;
            }
gbazin's avatar
   
gbazin committed
206
207
        }
        else
Jon Lech Johansen's avatar
Jon Lech Johansen committed
208
        {
Rafaël Carré's avatar
Rafaël Carré committed
209
            memcpy( p_out + 8, p_in, i_length );
Jon Lech Johansen's avatar
Jon Lech Johansen committed
210
211
        }

212
213
        if( i_fz > i_length + 8 )
        {
Rafaël Carré's avatar
Rafaël Carré committed
214
            memset( p_out + 8 + i_length_padded, 0,
215
                        i_fz - i_length_padded - 8 );
216
        }
Jon Lech Johansen's avatar
Jon Lech Johansen committed
217
218
    }

219
    p_out_buf->i_pts = p_filter->p_sys->start_date;
gbazin's avatar
   
gbazin committed
220
    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples * 3;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
221
    p_out_buf->i_buffer = p_out_buf->i_nb_samples * 4;
222
223
224
out:
    block_Release( p_in_buf );
    return p_out_buf;
Jon Lech Johansen's avatar
Jon Lech Johansen committed
225
}