m4a.c 7.06 KB
Newer Older
1 2 3
/*****************************************************************************
 * m4a.c : MPEG-4 audio demuxer
 *****************************************************************************
4
 * Copyright (C) 2002-2004 the VideoLAN team
Antoine Cellerier's avatar
Antoine Cellerier committed
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Gildas Bazin <gbazin@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
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

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

32
#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
33
#include <vlc_demux.h>
34
#include <vlc_codec.h>
35
#include <vlc_input.h>
36 37 38 39 40 41 42 43

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

vlc_module_begin();
Clément Stenac's avatar
Clément Stenac committed
44 45
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
46
    set_description( _("MPEG-4 audio demuxer" ) );
47
    set_capability( "demux", 110 );
48 49 50 51 52 53 54 55 56 57 58
    set_callbacks( Open, Close );
    add_shortcut( "m4a" );
    add_shortcut( "mp4a" );
    add_shortcut( "aac" );
vlc_module_end();

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
struct demux_sys_t
{
59
    bool  b_start;
60 61 62
    es_out_id_t *p_es;

    decoder_t   *p_packetizer;
63 64 65 66 67

    mtime_t     i_pts;
    int64_t     i_bytes;
    mtime_t     i_time_offset;
    int         i_bitrate_avg;
68 69 70 71 72 73
};

static int Demux( demux_t * );
static int Control( demux_t *, int, va_list );

#define M4A_PACKET_SIZE 4096
74
#define M4A_PTS_START 1
75 76 77 78 79 80 81 82

/*****************************************************************************
 * Open: initializes demux structures
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
83
    const uint8_t *p_peek;
84
    int         b_forced = false;
85

86
    if( demux_IsPathExtension( p_demux, ".aac" ) )
87
        b_forced = true;
88 89 90 91 92 93 94 95 96 97

    if( !p_demux->b_force && !b_forced )
        return VLC_EGENERIC;

    /* peek the begining (10 is for adts header) */
    if( stream_Peek( p_demux->s, &p_peek, 10 ) < 10 )
    {
        msg_Err( p_demux, "cannot peek" );
        return VLC_EGENERIC;
    }
98
    if( !strncmp( (char *)p_peek, "ADIF", 4 ) )
99 100 101 102 103 104 105
    {
        msg_Err( p_demux, "ADIF file. Not yet supported. (Please report)" );
        return VLC_EGENERIC;
    }

    p_demux->pf_demux  = Demux;
    p_demux->pf_control= Control;
106
    p_demux->p_sys     = p_sys = calloc( sizeof( demux_sys_t ), 1 );
107
    p_sys->b_start     = true;
108

Clément Stenac's avatar
Clément Stenac committed
109 110
    /* Load the mpeg 4 audio packetizer */
    INIT_APACKETIZER( p_sys->p_packetizer,  'm', 'p', '4', 'a'  );
111
    es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
Clément Stenac's avatar
Clément Stenac committed
112
    LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "mp4 audio" );
113 114 115 116 117 118 119 120 121 122 123 124

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;

125
    DESTROY_PACKETIZER( p_sys->p_packetizer );
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    free( p_sys );
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux( demux_t *p_demux)
{
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t *p_block_in, *p_block_out;

    if( ( p_block_in = stream_Block( p_demux->s, M4A_PACKET_SIZE ) ) == NULL )
    {
        return 0;
    }

145
    p_block_in->i_pts = p_block_in->i_dts = p_sys->b_start ? M4A_PTS_START : 0;
146
    p_sys->b_start = false;
147 148 149 150 151 152 153 154 155 156

    while( (p_block_out = p_sys->p_packetizer->pf_packetize(
                                          p_sys->p_packetizer, &p_block_in )) )
    {
        while( p_block_out )
        {
            block_t *p_next = p_block_out->p_next;

            if( p_sys->p_es == NULL )
            {
157
                p_sys->p_packetizer->fmt_out.b_packetized = true;
158 159 160 161 162 163 164
                p_sys->p_es = es_out_Add( p_demux->out,
                                          &p_sys->p_packetizer->fmt_out);
            }

            es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );

            p_block_out->p_next = NULL;
165 166 167 168 169 170 171

            p_sys->i_pts = p_block_out->i_pts;
            if( p_sys->i_pts > M4A_PTS_START + I64C(500000) )
                p_sys->i_bitrate_avg =
                    8*I64C(1000000)*p_sys->i_bytes/(p_sys->i_pts-M4A_PTS_START);

            p_sys->i_bytes += p_block_out->i_buffer;
172 173 174 175 176 177 178 179 180 181 182 183 184
            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );

            p_block_out = p_next;
        }
    }
    return 1;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
185
    demux_sys_t *p_sys = p_demux->p_sys;
186
    bool *pb_bool;
187 188 189 190 191
    int64_t *pi64;
    int i_ret;

    switch( i_query )
    {
192
    case DEMUX_HAS_UNSUPPORTED_META:
193 194
        pb_bool = (bool *)va_arg( args, bool* );
        *pb_bool = true;
195 196
        return VLC_SUCCESS;

197 198 199 200 201 202 203
    case DEMUX_GET_TIME:
        pi64 = (int64_t*)va_arg( args, int64_t * );
        *pi64 = p_sys->i_pts + p_sys->i_time_offset;
        return VLC_SUCCESS;

    case DEMUX_SET_TIME: /* TODO high precision seek for multi-input */
    default:
204
        i_ret = demux_vaControlHelper( p_demux->s, 0, -1,
205 206 207 208 209 210 211 212 213 214 215 216 217
                                        p_sys->i_bitrate_avg, 1, i_query, args);
        /* Fix time_offset */
        if( (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME ) &&
            i_ret == VLC_SUCCESS && p_sys->i_bitrate_avg > 0 )
        {
            int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) /
                p_sys->i_bitrate_avg;

            if( i_time >= 0 )
                p_sys->i_time_offset = i_time - p_sys->i_pts;
        }
        return i_ret;
    }
218
}
219