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
#include <stdlib.h>                                      /* malloc(), free() */

30
#include <vlc/vlc.h>
31 32
#include <vlc_codec.h>
#include <vlc_block.h>
33 34 35 36

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
37 38
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
39 40

vlc_module_begin();
41 42
    set_category( CAT_SOUT );
    set_subcategory( SUBCAT_SOUT_PACKETIZER );
43
    set_description( _("Copy packetizer") );
44
    set_capability( "packetizer", 1 );
45
    set_callbacks( Open, Close );
46 47
vlc_module_end();

48 49 50
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
51
struct decoder_sys_t
52
{
53 54
    block_t *p_block;
};
55

56 57
static block_t *Packetize   ( decoder_t *, block_t ** );
static block_t *PacketizeSub( decoder_t *, block_t ** );
58

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

70 71 72
    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 )
73
    {
74 75
        msg_Err( p_dec, "invalid ES type" );
        return VLC_EGENERIC;
76 77
    }

78 79 80 81
    if( p_dec->fmt_in.i_cat == SPU_ES )
        p_dec->pf_packetize = PacketizeSub;
    else
        p_dec->pf_packetize = Packetize;
82

83
    /* Create the output format */
84
    es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
85

86 87
    /* Fix the value of the fourcc */
    switch( p_dec->fmt_in.i_codec )
88
    {
89
        /* video */
90 91 92 93
        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'):
94 95 96 97 98 99
        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'):
100 101
        case VLC_FOURCC( 0x04, 0,   0,   0):
        case VLC_FOURCC( '3', 'I', 'V', '2'):
102
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v');
103
            break;
104

105 106
        case VLC_FOURCC( 'm', 'p', 'g', '1' ):
        case VLC_FOURCC( 'm', 'p', 'g', '2' ):
107 108
        case VLC_FOURCC( 'm', 'p', '1', 'v' ):
        case VLC_FOURCC( 'm', 'p', '2', 'v' ):
109
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
110
            break;
111 112 113 114

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

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

124 125 126 127 128 129 130 131 132 133 134 135
        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' ):
136
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
137
            break;
138

139 140 141
        case VLC_FOURCC( 'h', '2', '6', '3' ):
        case VLC_FOURCC( 'U', '2', '6', '3' ):
        case VLC_FOURCC( 'u', '2', '6', '3' ):
142
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
143
            break;
144

145
        case VLC_FOURCC( 'i', '2', '6', '3' ):
146
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'I', '2', '6', '3' );
147
            break;
148

149 150 151 152 153
        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' ):
154
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
155
            break;
156

157 158 159
        case VLC_FOURCC( 'd', 'v', 's', 'd' ):
        case VLC_FOURCC( 'D', 'V', 'S', 'D' ):
        case VLC_FOURCC( 'd', 'v', 'h', 'd' ):
160
            p_dec->fmt_out.i_codec = VLC_FOURCC( 'd', 'v', 's', 'l' );
161
            break;
162

163 164
        /* audio */
        case VLC_FOURCC( 'a', 'r', 'a', 'w' ):
165
            switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
166
            {
167 168 169 170 171 172 173 174 175 176 177 178 179
                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:
180
                    msg_Err( p_dec, "unknown raw audio sample size" );
181
                    return VLC_EGENERIC;
182 183
            }
            break;
184

185
        case VLC_FOURCC( 't', 'w', 'o', 's' ):
186
            switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
187
            {
188 189 190 191 192 193 194 195 196 197 198 199 200
                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:
201
                    msg_Err( p_dec, "unknown raw audio sample size" );
202
                    return VLC_EGENERIC;
203 204
            }
            break;
205

206
        case VLC_FOURCC( 's', 'o', 'w', 't' ):
207
            switch( ( p_dec->fmt_in.audio.i_bitspersample + 7 ) / 8 )
208
            {
209 210 211 212 213 214 215 216 217 218 219 220 221
                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:
222
                    msg_Err( p_dec, "unknown raw audio sample size" );
223
                    return VLC_EGENERIC;
224 225
            }
            break;
226
    }
227

228 229
    p_dec->p_sys = p_sys = malloc( sizeof( block_t ) );
    p_sys->p_block    = NULL;
230

231 232
    return VLC_SUCCESS;
}
233

234 235 236 237 238 239
/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    decoder_t     *p_dec = (decoder_t*)p_this;
240

241
    if( p_dec->p_sys->p_block )
242
    {
243
        block_ChainRelease( p_dec->p_sys->p_block );
244
    }
245

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
246
    es_format_Clean( &p_dec->fmt_out );
247
    free( p_dec->p_sys );
248 249 250
}

/*****************************************************************************
251
 * Packetize: packetize an unit (here copy a complete block )
252
 *****************************************************************************/
253
static block_t *Packetize ( decoder_t *p_dec, block_t **pp_block )
254
{
255 256
    block_t *p_block;
    block_t *p_ret = p_dec->p_sys->p_block;
257

258
    if( pp_block == NULL || *pp_block == NULL )
259 260
        return NULL;
    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
261
    {
262
        block_Release( *pp_block );
263
        return NULL;
264
    }
265

266 267
    p_block = *pp_block;
    *pp_block = NULL;
268

269
    if( p_block->i_dts <= 0 )
270 271 272 273
    {
        p_block->i_dts = p_block->i_pts;
    }

274
    if( p_block->i_dts <= 0 )
275
    {
276
        msg_Dbg( p_dec, "need dts > 0" );
277 278
        block_Release( p_block );
        return NULL;
279
    }
280

281
    if( p_ret != NULL && p_block->i_pts > p_ret->i_pts )
282
    {
283
        p_ret->i_length = p_block->i_pts - p_ret->i_pts;
284
    }
285
    p_dec->p_sys->p_block = p_block;
286

287
    return p_ret;
288
}
289 290 291 292 293 294 295 296 297

/*****************************************************************************
 * 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 )
298 299
        return NULL;
    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
300
    {
301
        block_Release( *pp_block );
302 303
        return NULL;
    }
304

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    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;
}