📄 libmadplugin.cpp
字号:
/************************************************************************ Copyright (C) 2000-2005 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.** ** 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.** ** A copy of the GNU GPL license version 2 is included in this package as ** LICENSE.GPL.**** 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.**** In addition, as a special exception Trolltech gives permission to link** the code of this program with Qtopia applications copyrighted, developed** and distributed by Trolltech under the terms of the Qtopia Personal Use** License Agreement. You must comply with the GNU General Public License** in all respects for all of the code used other than the applications** licensed under the Qtopia Personal Use License Agreement. If you modify** this file, you may extend this exception to your version of the file,** but you are not obligated to do so. If you do not wish to do so, delete** this exception statement from your version.** ** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include <stdio.h>#include <stdarg.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#include <ctype.h>#include <errno.h>#include <time.h>#include <locale.h>#include <math.h>#include <assert.h>#include <qapplication.h>//#define HAVE_MMAP#if defined(HAVE_MMAP)# include <sys/mman.h>#endif#include "libmadplugin.h"extern "C" {#include "mad.h"}#define MPEG_BUFFER_SIZE 65536//#define debugMsg(a) qDebug(a)#define debugMsg(a)/*! \class LibMadPlugin libmadplugin.h \brief The LibMadPlugin class is a wrapper for the libmad library. \legalese LibMAD is an integer-based MPEG audio decoder that supports all three MPEG Layers, and is used by the MediaPlayer. The libmad library is distributed under the terms of the GNU General Public License, Version 2. The primary copyright holder is <a href="mailto:rob@mars.org">Robert Leslie</a>. More information about the Mad library can be found at <a href="http://www.mars.org/home/rob/proj/mpeg/">http://www.mars.org/home/rob/proj/mpeg/</a>*/// QDOC_SKIP_BEGINclass Input {public: char const *path; int fd;#if defined(HAVE_MMAP) void *fdm;#endif unsigned long fileLength; unsigned char *data; unsigned long length; int eof;};class Output {public: mad_fixed_t attenuate; struct filter *filters; unsigned int channels_in; unsigned int channels_out; unsigned int speed_in; unsigned int speed_out; const char *path;};# if defined(HAVE_MMAP)static void *map_file(int fd, unsigned long *length){ void *fdm; *length += MAD_BUFFER_GUARD; fdm = mmap(0, *length, PROT_READ, MAP_SHARED, fd, 0); if (fdm == MAP_FAILED) return 0;# if defined(HAVE_MADVISE) madvise(fdm, *length, MADV_SEQUENTIAL);# endif return fdm;}static int unmap_file(void *fdm, unsigned long length){ if (munmap(fdm, length) == -1) return -1; return 0;}# endifclass LibMadPluginData {public: Input input; Output output; int bad_last_frame; struct mad_stream stream; struct mad_frame frame; struct mad_synth synth; int buffered; mad_fixed_t buffer[2][65536 * 2]; bool resync;};LibMadPlugin::LibMadPlugin() { d = new LibMadPluginData; d->input.fd = 0;#if defined(HAVE_MMAP) d->input.fdm = 0;#endif d->input.data = 0; d->buffered = 0; d->resync = FALSE; info = qApp->translate( "MediaPlayer", "No Song Open", "libmad strings for mp3 file info" );}/*! \fn LibMadPlugin::pluginComment(); \internal*//*! \fn LibMadPlugin::pluginVersion(); \internal*//*! \fn LibMadPlugin::fileInfo() \internal*//*! \fn LibMadPlugin::videoStreams() \internal*//*! \fn LibMadPlugin::videoWidth(int) \internal*//*! \fn LibMadPlugin::videoHeight(int) \internal*//*! \fn LibMadPlugin::videFrameRate(int) \internal*//*! \fn LibMadPlugin::videoFrames(int) \internal*//*! \fn LibMadPlugin::videoSetFrame(long,int) \internal*//*! \fn LibMadPlugin::videoGetFrame(int) \internal*//*! \fn LibMadPlugin::videoReadFrame(unsigned char**, int, int, int, int, ColorFormat, int ) \internal*//*! \fn LibMadPlugin::videoReadScaledFrame( unsigned char**, int, int, int, int, int, int, ColorFormat, int ) \internal*//*! \fn LibMadPlugin::videoReadYUVFrame(char *, char *, char *, int, int, int, int, int ) \internal*//*! \fn LibMadPlugin::setSMP(int) \internal*//*! \fn LibMadPlugin::setMMX(bool) \internal*//*! \fn LibMadPlugin::supportsAudio() \internal*//*! \fn LibMadPlugin::supportsVideo() \internal*//*! \fn LibMadPlugin::supportsYUV() \internal*//*! \fn LibMadPlugin::supportsMMX() \internal*//*! \fn LibMadPlugin::supportsSMP() \internal*//*! \fn LibMadPlugin::supportsStereo() \internal*//*! \fn LibMadPlugin::supportsScaling() \internal*//*! \fn LibMadPlugin::getPlayTime() \internal*//*! \fn LibMadPlugin::videoFrameRate( int ) \internal*//*! \internal */LibMadPlugin::~LibMadPlugin() { close(); delete d;}/*! \a path \internal */bool LibMadPlugin::isFileSupported( const QString& path ) { debugMsg( "LibMadPlugin::isFileSupported" ); // Mpeg file extensions // "mp2","mp3","m1v","m2v","m2s","mpg","vob","mpeg","ac3" // Other media extensions // "wav","mid","mod","s3m","ogg","avi","mov","sid" char *ext = strrchr( path.latin1(), '.' ); // Test file extension if ( ext ) { if ( strncasecmp(ext, ".mp2", 4) == 0 ) return TRUE; if ( strncasecmp(ext, ".mp3", 4) == 0 ) return TRUE; } return FALSE;}/*! \a path \internal */bool LibMadPlugin::open( const QString& path ) { debugMsg( "LibMadPlugin::open" ); d->bad_last_frame = 0; d->buffered = 0; info = QString( "" ); //qDebug( "Opening %s", path.latin1() ); d->input.path = path.latin1(); d->input.fd = ::open( d->input.path, O_RDONLY ); if (d->input.fd == -1) { qDebug("error opening %s", d->input.path ); return FALSE; } struct stat stat; if (fstat(d->input.fd, &stat) == -1) { qDebug("error calling fstat"); return FALSE; } if (S_ISREG(stat.st_mode) && stat.st_size > 0) d->input.fileLength = stat.st_size; else d->input.fileLength = 0;#if defined(HAVE_MMAP) if (S_ISREG(stat.st_mode) && stat.st_size > 0) { d->input.length = stat.st_size; d->input.fdm = map_file(d->input.fd, &d->input.length); if (d->input.fdm == 0) { qDebug("error mmapping file"); return FALSE; } d->input.data = (unsigned char *)d->input.fdm; }#endif if (d->input.data == 0) { d->input.data = (unsigned char *)malloc(MPEG_BUFFER_SIZE); if (d->input.data == 0) { qDebug("error allocating input buffer"); return FALSE; } d->input.length = 0; } d->input.eof = 0; d->resync = TRUE; d->buffered = 0; mad_stream_init(&d->stream); mad_frame_init(&d->frame); mad_synth_init(&d->synth); return TRUE;}/*! \internal */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -