copy.c 10.8 KB
Newer Older
1 2 3
/*****************************************************************************
 * copy.c
 *****************************************************************************
4
 * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
 *
 * 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.
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc/vlc.h>
34
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
35 36
#include <vlc_codec.h>
#include <vlc_block.h>
37 38 39 40

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
41 42
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
43 44

vlc_module_begin();
45 46
    set_category( CAT_SOUT );
    set_subcategory( SUBCAT_SOUT_PACKETIZER );
47
    set_description( N_("Copy packetizer") );
48
    set_capability( "packetizer", 1 );
49
    set_callbacks( Open, Close );
50 51
vlc_module_end();

52 53 54
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
55
struct decoder_sys_t
56
{
57 58
    block_t *p_block;
};
59

60 61
static block_t *Packetize   ( decoder_t *, block_t ** );
static block_t *PacketizeSub( decoder_t *, block_t ** );
62

63
/*****************************************************************************
64
 * Open: probe the packetizer and return score
65 66 67 68 69 70
 *****************************************************************************
 * Tries to launch a decoder and return score so that the interface is able
 * to choose.
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
71 72
    decoder_t     *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
73

74 75 76
    if( p_dec->fmt_in.i_cat != AUDIO_ES &&
        p_dec->fmt_in.i_cat != VIDEO_ES &&
        p_dec->fmt_in.i_cat != SPU_ES )
77
    {
78 79
        msg_Err( p_dec, "invalid ES type" );
        return VLC_EGENERIC;
80 81
    }

82 83 84 85
    if( p_dec->fmt_in.i_cat == SPU_ES )
        p_dec->pf_packetize = PacketizeSub;
    else
        p_dec->pf_packetize = Packetize;
86

87
    /* Create the output format */
88
    es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
89

90 91
    /* Fix the value of the fourcc */
    switch( p_dec->fmt_in.i_codec )
92
    {
93
        /* video */
94 95 96 97
        case VLC_FOURCC( 'm', '4', 's', '2'):
        case VLC_FOURCC( 'M', '4', 'S', '2'):
        case VLC_FOURCC( 'm', 'p', '4', 's'):
        case VLC_FOURCC( 'M', 'P', '4', 'S'):
98 99 100 101 102 103
        case VLC_FOURCC( 'D', 'I', 'V', 'X'):
        case VLC_FOURCC( 'd', 'i', 'v', 'x'):
        case VLC_FOURCC( 'X', 'V', 'I', 'D'):
        case VLC_FOURCC( 'X', 'v', 'i', 'D'):
        case VLC_FOURCC( 'x', 'v', 'i', 'd'):
        case VLC_FOURCC( 'D', 'X', '5', '0'):
104 105
        case VLC_FOURCC( 0x04, 0,   0,   0):
        case VLC_FOURCC( '3', 'I', 'V', '2'):
106
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v');
107
            break;
108

109 110
        case VLC_FOURCC( 'm', 'p', 'g', '1' ):
        case VLC_FOURCC( 'm', 'p', 'g', '2' ):
111 112
        case VLC_FOURCC( 'm', 'p', '1', 'v' ):
        case VLC_FOURCC( 'm', 'p', '2', 'v' ):
113
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
114
            break;
115 116 117 118

        case VLC_FOURCC( 'd', 'i', 'v', '1' ):
        case VLC_FOURCC( 'M', 'P', 'G', '4' ):
        case VLC_FOURCC( 'm', 'p', 'g', '4' ):
119
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '1' );
120
            break;
121

122 123 124
        case VLC_FOURCC( 'd', 'i', 'v', '2' ):
        case VLC_FOURCC( 'M', 'P', '4', '2' ):
        case VLC_FOURCC( 'm', 'p', '4', '2' ):
125
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '2' );
126
            break;
127

128 129 130 131 132 133 134 135 136 137 138 139
        case VLC_FOURCC( 'd', 'i', 'v', '3' ):
        case VLC_FOURCC( 'd', 'i', 'v', '4' ):
        case VLC_FOURCC( 'D', 'I', 'V', '4' ):
        case VLC_FOURCC( 'd', 'i', 'v', '5' ):
        case VLC_FOURCC( 'D', 'I', 'V', '5' ):
        case VLC_FOURCC( 'd', 'i', 'v', '6' ):
        case VLC_FOURCC( 'D', 'I', 'V', '6' ):
        case VLC_FOURCC( 'M', 'P', '4', '3' ):
        case VLC_FOURCC( 'm', 'p', '4', '3' ):
        case VLC_FOURCC( 'm', 'p', 'g', '3' ):
        case VLC_FOURCC( 'M', 'P', 'G', '3' ):
        case VLC_FOURCC( 'A', 'P', '4', '1' ):
140
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
141
            break;
142

143 144 145
        case VLC_FOURCC( 'h', '2', '6', '3' ):
        case VLC_FOURCC( 'U', '2', '6', '3' ):
        case VLC_FOURCC( 'u', '2', '6', '3' ):
146
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
147
            break;
148

149
        case VLC_FOURCC( 'i', '2', '6', '3' ):
150
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'I', '2', '6', '3' );
151
            break;
152

153 154 155 156 157
        case VLC_FOURCC( 'm', 'j', 'p', 'g' ):
        case VLC_FOURCC( 'm', 'j', 'p', 'a' ):
        case VLC_FOURCC( 'j', 'p', 'e', 'g' ):
        case VLC_FOURCC( 'J', 'P', 'E', 'G' ):
        case VLC_FOURCC( 'J', 'F', 'I', 'F' ):
158
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
159
            break;
160

161 162 163
        case VLC_FOURCC( 'd', 'v', 's', 'd' ):
        case VLC_FOURCC( 'D', 'V', 'S', 'D' ):
        case VLC_FOURCC( 'd', 'v', 'h', 'd' ):
164
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'd', 'v', 's', 'l' );
Gildas Bazin's avatar
 
Gildas Bazin committed
165
            break;
166

167 168
        /* audio */
        case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
169
            switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
170
            {
171 172 173 174 175 176 177 178 179 180 181 182 183
                case 1:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('u','8',' ',' ');
                    break;
                case 2:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','l');
                    break;
                case 3:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','l');
                    break;
                case 4:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','3','2','l');
                    break;
                default:
184
                    msg_Err( p_dec, "unknown raw audio sample size" );
185
                    return VLC_EGENERIC;
186 187
            }
            break;
188

189
        case VLC_FOURCC( 't', 'w', 'o', 's' ):
190
            switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
191
            {
192 193 194 195 196 197 198 199 200 201 202 203 204
                case 1:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','8',' ',' ');
                    break;
                case 2:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','b');
                    break;
                case 3:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','b');
                    break;
                case 4:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','3','2','b');
                    break;
                default:
205
                    msg_Err( p_dec, "unknown raw audio sample size" );
206
                    return VLC_EGENERIC;
207 208
            }
            break;
209

210
        case VLC_FOURCC( 's', 'o', 'w', 't' ):
211
            switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
212
            {
213 214 215 216 217 218 219 220 221 222 223 224 225
                case 1:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','8',' ',' ');
                    break;
                case 2:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','1','6','l');
                    break;
                case 3:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','2','4','l');
                    break;
                case 4:
                    p_dec->fmt_out.i_codec = VLC_FOURCC('s','3','2','l');
                    break;
                default:
226
                    msg_Err( p_dec, "unknown raw audio sample size" );
227
                    return VLC_EGENERIC;
228 229
            }
            break;
230
    }
231

232 233
    p_dec->p_sys = p_sys = malloc( sizeof( block_t ) );
    p_sys->p_block    = NULL;
234

235 236
    return VLC_SUCCESS;
}
237

238 239 240 241 242 243
/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    decoder_t     *p_dec = (decoder_t*)p_this;
244

245
    if( p_dec->p_sys->p_block )
246
    {
247
        block_ChainRelease( p_dec->p_sys->p_block );
248
    }
249

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
250
    es_format_Clean( &p_dec->fmt_out );
251
    free( p_dec->p_sys );
252 253 254
}

/*****************************************************************************
255
 * Packetize: packetize an unit (here copy a complete block )
256
 *****************************************************************************/
257
static block_t *Packetize ( decoder_t *p_dec, block_t **pp_block )
258
{
259 260
    block_t *p_block;
    block_t *p_ret = p_dec->p_sys->p_block;
261

262
    if( pp_block == NULL || *pp_block == NULL )
263 264
        return NULL;
    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
265
    {
266
        block_Release( *pp_block );
267
        return NULL;
268
    }
269

270 271
    p_block = *pp_block;
    *pp_block = NULL;
272

273
    if( p_block->i_dts <= 0 )
274 275 276 277
    {
        p_block->i_dts = p_block->i_pts;
    }

278
    if( p_block->i_dts <= 0 )
279
    {
280
        msg_Dbg( p_dec, "need dts > 0" );
281 282
        block_Release( p_block );
        return NULL;
283
    }
284

285
    if( p_ret != NULL && p_block->i_pts > p_ret->i_pts )
286
    {
287
        p_ret->i_length = p_block->i_pts - p_ret->i_pts;
288
    }
289
    p_dec->p_sys->p_block = p_block;
290

291
    return p_ret;
292
}
293 294 295 296 297 298 299 300 301

/*****************************************************************************
 * PacketizeSub: packetize an unit (here copy a complete block )
 *****************************************************************************/
static block_t *PacketizeSub( decoder_t *p_dec, block_t **pp_block )
{
    block_t *p_block;

    if( pp_block == NULL || *pp_block == NULL )
302 303
        return NULL;
    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
304
    {
305
        block_Release( *pp_block );
306 307
        return NULL;
    }
308

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    p_block = *pp_block;
    *pp_block = NULL;

    if( p_block->i_dts <= 0 )
    {
        p_block->i_dts = p_block->i_pts;
    }

    if( p_block->i_dts <= 0 )
    {
        msg_Dbg( p_dec, "need dts > 0" );
        block_Release( p_block );
        return NULL;
    }

    return p_block;
}