vlc/modules/demux/xa.c

203 lines
6.9 KiB
C
Raw Normal View History

/*****************************************************************************
2007-02-13 11:44:57 +01:00
* xa.c : xa file demux module for vlc
*****************************************************************************
2006-04-29 15:30:10 +02:00
* Copyright (C) 2005 Rémi Denis-Courmont
*
2019-09-04 21:10:51 +02:00
* Authors: Rémi Denis-Courmont
*
LGPL Re-license almost all the playback modules to LGPLv2.1+ with authorization from their respective contributors (230+) This includes: - access, codec, packetizers, demux - audio filters, audio mixers, audio output - video filters, video chroma, video output - text renderers - XML parser - ARM NEON and SSE2 optimisations (mostly for chromas and filters) Some modules are not concerned: - BDA and DShow access modules because Manol Manolov is AWOL - Real RTSP, because it is derived from Xine - x264 and t140 because they are encoders only - DLL Loader, because it is derived from MPlayer - DTS packetizer, because Jon Lech Johansen is AWOL - Shine and WMAfixed, because they are derived from Rockbox - Real demuxer, as it is derived from MPlayer and Wang Bo is AWOL - MPC demuxer, as Yavor Doganov is AWOL - Tivo demuxer, because it is derived from an MPlayer fork - Playlist demuxer, (WPL and ZPL parts missing), because suheaven is AWOL - iOS audio output and video display, because author refuses the license change - Equalizer and compressor, because Ronald Wright is AWOL - Mono, Headphone and Dolby, because author refuses the license change - hqdn3d and yadif, because they are from MPlayer/libavfilter - remoteosd, because it derives from RealVNC code - MMX optimisations, because Ollie Lho, from SiS, is AWOL - Rotate, because it depends on GPL motion Nota Bene: - Some modules depend on GPL-only libraries, a LGPL module does not mean that the resulting binary module will be LGPL. Libraries affected would include liba52, libdvdcss, libdvdnav, libdvdread, faad2, libdca, libmad, libmpeg2, libpostproc, SRC, sid, zvbi and probably others.
2012-11-06 17:48:33 +01:00
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 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
LGPL Re-license almost all the playback modules to LGPLv2.1+ with authorization from their respective contributors (230+) This includes: - access, codec, packetizers, demux - audio filters, audio mixers, audio output - video filters, video chroma, video output - text renderers - XML parser - ARM NEON and SSE2 optimisations (mostly for chromas and filters) Some modules are not concerned: - BDA and DShow access modules because Manol Manolov is AWOL - Real RTSP, because it is derived from Xine - x264 and t140 because they are encoders only - DLL Loader, because it is derived from MPlayer - DTS packetizer, because Jon Lech Johansen is AWOL - Shine and WMAfixed, because they are derived from Rockbox - Real demuxer, as it is derived from MPlayer and Wang Bo is AWOL - MPC demuxer, as Yavor Doganov is AWOL - Tivo demuxer, because it is derived from an MPlayer fork - Playlist demuxer, (WPL and ZPL parts missing), because suheaven is AWOL - iOS audio output and video display, because author refuses the license change - Equalizer and compressor, because Ronald Wright is AWOL - Mono, Headphone and Dolby, because author refuses the license change - hqdn3d and yadif, because they are from MPlayer/libavfilter - remoteosd, because it derives from RealVNC code - MMX optimisations, because Ollie Lho, from SiS, is AWOL - Rotate, because it depends on GPL motion Nota Bene: - Some modules depend on GPL-only libraries, a LGPL module does not mean that the resulting binary module will be LGPL. Libraries affected would include liba52, libdvdcss, libdvdnav, libdvdread, faad2, libdca, libmad, libmpeg2, libpostproc, SRC, sid, zvbi and probably others.
2012-11-06 17:48:33 +01:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
LGPL Re-license almost all the playback modules to LGPLv2.1+ with authorization from their respective contributors (230+) This includes: - access, codec, packetizers, demux - audio filters, audio mixers, audio output - video filters, video chroma, video output - text renderers - XML parser - ARM NEON and SSE2 optimisations (mostly for chromas and filters) Some modules are not concerned: - BDA and DShow access modules because Manol Manolov is AWOL - Real RTSP, because it is derived from Xine - x264 and t140 because they are encoders only - DLL Loader, because it is derived from MPlayer - DTS packetizer, because Jon Lech Johansen is AWOL - Shine and WMAfixed, because they are derived from Rockbox - Real demuxer, as it is derived from MPlayer and Wang Bo is AWOL - MPC demuxer, as Yavor Doganov is AWOL - Tivo demuxer, because it is derived from an MPlayer fork - Playlist demuxer, (WPL and ZPL parts missing), because suheaven is AWOL - iOS audio output and video display, because author refuses the license change - Equalizer and compressor, because Ronald Wright is AWOL - Mono, Headphone and Dolby, because author refuses the license change - hqdn3d and yadif, because they are from MPlayer/libavfilter - remoteosd, because it derives from RealVNC code - MMX optimisations, because Ollie Lho, from SiS, is AWOL - Rotate, because it depends on GPL motion Nota Bene: - Some modules depend on GPL-only libraries, a LGPL module does not mean that the resulting binary module will be LGPL. Libraries affected would include liba52, libdvdcss, libdvdnav, libdvdread, faad2, libdca, libmad, libmpeg2, libpostproc, SRC, sid, zvbi and probably others.
2012-11-06 17:48:33 +01:00
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <vlc_common.h>
2008-05-08 17:30:33 +02:00
#include <vlc_plugin.h>
#include <vlc_demux.h>
/*****************************************************************************
* Module descriptor
*****************************************************************************/
static int Open ( vlc_object_t * );
vlc_module_begin ()
set_description( N_("XA demuxer") )
set_category( CAT_INPUT )
set_subcategory( SUBCAT_INPUT_DEMUX )
set_capability( "demux", 10 )
2019-07-17 21:59:02 +02:00
set_callback( Open )
vlc_module_end ()
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int Demux ( demux_t * );
static int Control( demux_t *, int i_query, va_list args );
typedef struct
{
es_out_id_t *p_es;
unsigned int i_data_size;
unsigned int i_block_frames;
unsigned int i_frame_size;
unsigned int i_bitrate;
date_t pts;
} demux_sys_t;
2005-03-07 14:28:08 +01:00
typedef struct xa_header_t
{
2005-07-08 20:45:05 +02:00
char xa_id[4];
uint32_t iSize;
uint16_t wFormatTag;
uint16_t nChannels;
uint32_t nSamplesPerSec;
uint32_t nAvgBytesPerSec;
uint16_t nBlockAlign;
uint16_t wBitsPerSample;
} xa_header_t;
2017-07-09 15:22:04 +02:00
#define HEADER_LENGTH 24
static_assert(offsetof(xa_header_t, wBitsPerSample) == 22, "Bad padding");
#define FRAME_LENGTH 28 /* samples per frame */
/*****************************************************************************
* Open: check file and initializes structures
*****************************************************************************/
static int Open( vlc_object_t * p_this )
{
demux_t *p_demux = (demux_t*)p_this;
const uint8_t *peek;
/* XA file heuristic */
if( vlc_stream_Peek( p_demux->s, &peek, 10 ) < 10 )
return VLC_EGENERIC;
2017-09-03 09:33:02 +02:00
if( memcmp( peek, "XAI", 4 ) && memcmp( peek, "XAJ", 4 ) &&
memcmp( peek, "XA\0", 4 ) )
return VLC_EGENERIC;
if( GetWLE( peek + 8 ) != 1 ) /* format tag */
return VLC_EGENERIC;
2018-05-03 10:58:16 +02:00
demux_sys_t *p_sys = vlc_obj_malloc( p_this, sizeof (*p_sys) );
2011-07-11 21:28:28 +02:00
if( unlikely( p_sys == NULL ) )
return VLC_ENOMEM;
/* read XA header*/
xa_header_t xa;
2017-07-09 15:22:04 +02:00
if( vlc_stream_Read( p_demux->s, &xa, HEADER_LENGTH ) < HEADER_LENGTH )
return VLC_EGENERIC;
es_format_t fmt;
2017-07-13 09:47:35 +02:00
es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_ADPCM_XA_EA );
2005-04-07 11:39:25 +02:00
msg_Dbg( p_demux, "assuming EA ADPCM audio codec" );
fmt.audio.i_rate = GetDWLE( &xa.nSamplesPerSec );
fmt.audio.i_bytes_per_frame = 15 * GetWLE( &xa.nChannels );
fmt.audio.i_frame_length = FRAME_LENGTH;
fmt.audio.i_channels = GetWLE ( &xa.nChannels );
fmt.audio.i_blockalign = fmt.audio.i_bytes_per_frame;
fmt.audio.i_bitspersample = GetWLE( &xa.wBitsPerSample );
fmt.i_bitrate = (fmt.audio.i_rate * fmt.audio.i_bytes_per_frame * 8)
/ fmt.audio.i_frame_length;
/* FIXME: better computation */
p_sys->i_data_size = xa.iSize * 15 / 56;
/* How many frames per block (1:1 is too CPU intensive) */
p_sys->i_block_frames = fmt.audio.i_rate / (FRAME_LENGTH * 20) + 1;
p_sys->i_frame_size = fmt.audio.i_bytes_per_frame;
p_sys->i_bitrate = fmt.i_bitrate;
msg_Dbg( p_demux, "fourcc: %4.4s, channels: %d, "
"freq: %d Hz, bitrate: %dKo/s, blockalign: %d",
(char *)&fmt.i_codec, fmt.audio.i_channels, fmt.audio.i_rate,
fmt.i_bitrate / 8192, fmt.audio.i_blockalign );
if( fmt.audio.i_rate == 0 || fmt.audio.i_channels == 0
|| fmt.audio.i_bitspersample != 16 )
2016-10-28 16:11:55 +02:00
return VLC_EGENERIC;
p_sys->p_es = es_out_Add( p_demux->out, &fmt );
2018-05-03 10:58:57 +02:00
if( unlikely(p_sys->p_es == NULL) )
return VLC_ENOMEM;
date_Init( &p_sys->pts, fmt.audio.i_rate, 1 );
2018-07-02 15:52:19 +02:00
date_Set( &p_sys->pts, VLC_TICK_0 );
2017-02-25 13:00:04 +01:00
p_demux->pf_demux = Demux;
p_demux->pf_control = Control;
p_demux->p_sys = p_sys;
return VLC_SUCCESS;
}
/*****************************************************************************
* Demux: read packet and send them to decoders
*****************************************************************************
* 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;
2017-07-09 15:22:04 +02:00
int64_t i_offset = vlc_stream_Tell( p_demux->s );
unsigned i_frames = p_sys->i_block_frames;
if( p_sys->i_data_size > 0 &&
2017-07-09 15:22:04 +02:00
(i_offset - HEADER_LENGTH) >= p_sys->i_data_size )
{
2018-05-03 13:49:59 +02:00
return VLC_DEMUXER_EOF;
}
2016-07-20 22:02:54 +02:00
p_block = vlc_stream_Block( p_demux->s, p_sys->i_frame_size * i_frames );
if( p_block == NULL )
{
msg_Warn( p_demux, "cannot read data" );
2018-05-03 13:49:59 +02:00
return VLC_DEMUXER_EOF;
}
i_frames = p_block->i_buffer / p_sys->i_frame_size;
p_block->i_dts = p_block->i_pts = date_Get( &p_sys->pts );
es_out_SetPCR( p_demux->out, p_block->i_pts );
es_out_Send( p_demux->out, p_sys->p_es, p_block );
date_Increment( &p_sys->pts, i_frames * FRAME_LENGTH );
2018-05-03 13:49:59 +02:00
return VLC_DEMUXER_SUCCESS;
}
/*****************************************************************************
* Control:
*****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
demux_sys_t *p_sys = p_demux->p_sys;
2017-07-09 15:22:04 +02:00
return demux_vaControlHelper( p_demux->s, HEADER_LENGTH,
2017-07-24 18:24:33 +02:00
p_sys->i_data_size ? (int64_t)HEADER_LENGTH + p_sys->i_data_size : -1,
2017-07-09 15:22:04 +02:00
p_sys->i_bitrate, p_sys->i_frame_size,
i_query, args );
}