copy.c 5.89 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_common.h>
34
#include <vlc_plugin.h>
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 45 46 47 48 49 50
vlc_module_begin ()
    set_category( CAT_SOUT )
    set_subcategory( SUBCAT_SOUT_PACKETIZER )
    set_description( N_("Copy packetizer") )
    set_capability( "packetizer", 1 )
    set_callbacks( Open, Close )
vlc_module_end ()
51

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 for audio */
    if( p_dec->fmt_in.i_cat == AUDIO_ES )
92
    {
93 94 95 96 97 98 99
        p_dec->fmt_out.i_codec = vlc_fourcc_GetCodecAudio( p_dec->fmt_in.i_codec,
                                                           p_dec->fmt_in.audio.i_bitspersample );
        if( !p_dec->fmt_out.i_codec )
        {
            msg_Err( p_dec, "unknown raw audio sample size" );
            return VLC_EGENERIC;
        }
100
    }
101

102
    p_dec->p_sys = p_sys = malloc( sizeof(*p_sys) );
103
    p_sys->p_block    = NULL;
104

105 106
    return VLC_SUCCESS;
}
107

108 109 110 111 112 113
/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    decoder_t     *p_dec = (decoder_t*)p_this;
114

115
    if( p_dec->p_sys->p_block )
116
    {
117
        block_ChainRelease( p_dec->p_sys->p_block );
118
    }
119

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
120
    es_format_Clean( &p_dec->fmt_out );
121
    free( p_dec->p_sys );
122 123 124
}

/*****************************************************************************
125
 * Packetize: packetize an unit (here copy a complete block )
126
 *****************************************************************************/
127
static block_t *Packetize ( decoder_t *p_dec, block_t **pp_block )
128
{
129 130
    block_t *p_block;
    block_t *p_ret = p_dec->p_sys->p_block;
131

132
    if( pp_block == NULL || *pp_block == NULL )
133 134
        return NULL;
    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
135
    {
136
        block_Release( *pp_block );
137
        return NULL;
138
    }
139

140 141
    p_block = *pp_block;
    *pp_block = NULL;
142

143
    if( p_block->i_dts <= 0 )
144 145 146 147
    {
        p_block->i_dts = p_block->i_pts;
    }

148
    if( p_block->i_dts <= 0 )
149
    {
150
        msg_Dbg( p_dec, "need dts > 0" );
151 152
        block_Release( p_block );
        return NULL;
153
    }
154

155
    if( p_ret != NULL && p_block->i_pts > p_ret->i_pts )
156
    {
157
        p_ret->i_length = p_block->i_pts - p_ret->i_pts;
158
    }
159
    p_dec->p_sys->p_block = p_block;
160

161
    return p_ret;
162
}
163 164 165 166 167 168 169 170 171

/*****************************************************************************
 * 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 )
172 173
        return NULL;
    if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
174
    {
175
        block_Release( *pp_block );
176 177
        return NULL;
    }
178

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    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;
}