forked from mirror/openmw-tes3mp
first FFmpeg / avcodoc commit (does NOT compile)
git-svn-id: https://openmw.svn.sourceforge.net/svnroot/openmw/trunk@24 ea6a568a-9f4f-0410-981a-c910a81bb256actorid
parent
1974b68dcd
commit
a99139cd2f
@ -0,0 +1,40 @@
|
||||
extern (C):
|
||||
|
||||
// A unique handle that represents an AV file
|
||||
typedef void* AVFile;
|
||||
|
||||
// A unique handle representing an audio stream
|
||||
typedef void* AVAudio;
|
||||
|
||||
// In case we ever decide to implement more codec backends, here's what
|
||||
// these functions need to do...
|
||||
|
||||
// Open the named file, and return a unique handle representing it.
|
||||
// Returns NULL on error
|
||||
AVFile cpp_openAVFile(char *fname);
|
||||
|
||||
// Close the file handle, invalidating all streams taken from it
|
||||
void cpp_closeAVFile(AVFile file);
|
||||
|
||||
// Get a unique handle to an audio stream in the file. The given number
|
||||
// is for files that can contain multiple audio streams (generally you
|
||||
// would pass 0, for the first audio stream)
|
||||
void *cpp_getAVAudioStream(AVFile file, int streamnum);
|
||||
|
||||
// Get audio info representing the current stream. Returns 0 for success
|
||||
// (not likely to fail)
|
||||
int cpp_getAVAudioInfo(AVAudio stream, int *rate, int *channels, int *bits);
|
||||
|
||||
// Decode the next bit of data for the given audio stream. The function
|
||||
// must provide no less than the requested number of bytes, except for
|
||||
// end-of-stream conditions, and is responsible for buffering data. For
|
||||
// files with multiple streams, it must take care to preserve data for
|
||||
// any stream that has had a stream handle returned.
|
||||
// eg. if a file has one video stream and 2 audio streams and the app
|
||||
// gets a handle to the video stream and one audio stream, it must
|
||||
// not destroy video data for subsequent calls to cpp_getAVVideoData if
|
||||
// it has to read over it while decoding the audio stream. The other
|
||||
// audio stream's data, however, may be discarded.
|
||||
// Returns the number of bytes written to the buffer, which will be no
|
||||
// more than the provided length.
|
||||
int cpp_getAVAudioData(AVAudio stream, void *data, int length);
|
@ -0,0 +1,209 @@
|
||||
/*
|
||||
OpenMW - The completely unofficial reimplementation of Morrowind
|
||||
Copyright (C) 2008 Nicolay Korslund
|
||||
Email: < korslund@gmail.com >
|
||||
WWW: http://openmw.snaptoad.com/
|
||||
|
||||
This file (cpp_avcodec.cpp) is part of the OpenMW package.
|
||||
|
||||
OpenMW is distributed as free software: you can redistribute it
|
||||
and/or modify it under the terms of the GNU General Public License
|
||||
version 3, as published by the Free Software Foundation.
|
||||
|
||||
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
|
||||
version 3 along with this program. If not, see
|
||||
http://www.gnu.org/licenses/ .
|
||||
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
extern "C" { // the headers don't do this..
|
||||
#include <ffmpeg/avcodec.h>
|
||||
#include <ffmpeg/avformat.h>
|
||||
}
|
||||
|
||||
#include <vector>
|
||||
|
||||
using std::vector;
|
||||
|
||||
struct MyFile {
|
||||
AVFormatContext *FmtCtx;
|
||||
struct MyStream {
|
||||
MyFile *parent;
|
||||
|
||||
AVCodecContext *CodecCtx;
|
||||
int StreamNum;
|
||||
|
||||
vector<uint8_t> Data;
|
||||
vector<char> DecodedData;
|
||||
};
|
||||
vector<MyStream> Streams;
|
||||
};
|
||||
|
||||
// TODO:
|
||||
// extern "C" MyFile::MyStream *cpp_getAVVideoStream(MyFile *file, int streamnum);
|
||||
// extern "C" int cpp_getAVVideoInfo(MyFile::MyStream *stream, float *fps, int *width, int * height);
|
||||
// extern "C" int cpp_getAVVideoData(MyFile::MyStream *stream, char *data, int length);
|
||||
|
||||
extern "C" MyFile *cpp_openAVFile(char *fname)
|
||||
{
|
||||
static bool done = false;
|
||||
if(!done) { av_register_all();
|
||||
av_log_set_level(AV_LOG_ERROR);}
|
||||
done = true;
|
||||
|
||||
MyFile *file = new MyFile;
|
||||
if(av_open_input_file(&file->FmtCtx, fname, NULL, 0, NULL) == 0)
|
||||
{
|
||||
if(av_find_stream_info(file->FmtCtx) >= 0)
|
||||
return file;
|
||||
av_close_input_file(file->FmtCtx);
|
||||
}
|
||||
delete file;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
extern "C" void cpp_closeAVFile(MyFile *file)
|
||||
{
|
||||
if(!file) return;
|
||||
|
||||
for(size_t i = 0;i < file->Streams.size();i++)
|
||||
{
|
||||
avcodec_close(file->Streams[i].CodecCtx);
|
||||
file->Streams[i].Data.clear();
|
||||
file->Streams[i].DecodedData.clear();
|
||||
}
|
||||
file->Streams.clear();
|
||||
|
||||
av_close_input_file(file->FmtCtx);
|
||||
delete file;
|
||||
}
|
||||
|
||||
extern "C" MyFile::MyStream *cpp_getAVAudioStream(MyFile *file, int streamnum)
|
||||
{
|
||||
if(!file) return NULL;
|
||||
for(unsigned int i = 0;i < file->FmtCtx->nb_streams;i++)
|
||||
{
|
||||
if(file->FmtCtx->streams[i]->codec->codec_type != CODEC_TYPE_AUDIO)
|
||||
continue;
|
||||
|
||||
if(streamnum == 0)
|
||||
{
|
||||
MyFile::MyStream stream;
|
||||
stream.parent = file;
|
||||
stream.CodecCtx = file->FmtCtx->streams[i]->codec;
|
||||
stream.StreamNum = i;
|
||||
|
||||
AVCodec *codec = avcodec_find_decoder(stream.CodecCtx->codec_id);
|
||||
if(!codec) return NULL;
|
||||
if(avcodec_open(stream.CodecCtx, codec) < 0)
|
||||
return NULL;
|
||||
|
||||
file->Streams.push_back(stream);
|
||||
return &file->Streams[file->Streams.size()-1];
|
||||
}
|
||||
streamnum--;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
extern "C" int cpp_getAVAudioInfo(MyFile::MyStream *stream,
|
||||
int *rate, int *channels, int *bits)
|
||||
{
|
||||
if(!stream) return 1;
|
||||
|
||||
if(rate) *rate = stream->CodecCtx->sample_rate;
|
||||
if(channels) *channels = stream->CodecCtx->channels;
|
||||
if(bits) *bits = 16;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getNextPacket(MyFile *file)
|
||||
{
|
||||
AVPacket packet;
|
||||
while(av_read_frame(file->FmtCtx, &packet) >= 0)
|
||||
{
|
||||
for(vector<MyFile::MyStream>::iterator i = file->Streams.begin();
|
||||
i != file->Streams.end();i++)
|
||||
{
|
||||
if(i->StreamNum == packet.stream_index)
|
||||
{
|
||||
size_t idx = i->Data.size();
|
||||
i->Data.resize(idx + packet.size);
|
||||
memcpy(&i->Data[idx], packet.data, packet.size);
|
||||
av_free_packet(&packet);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
av_free_packet(&packet);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
extern "C" int cpp_getAVAudioData(MyFile::MyStream *stream, char *data, int length)
|
||||
{
|
||||
if(!stream) return 0;
|
||||
|
||||
int dec = 0;
|
||||
while(dec < length)
|
||||
{
|
||||
if(stream->DecodedData.size() == 0)
|
||||
{
|
||||
while(stream->Data.size() == 0)
|
||||
{
|
||||
if(getNextPacket(stream->parent) != 0)
|
||||
break;
|
||||
}
|
||||
|
||||
int insize = stream->Data.size();
|
||||
if(insize == 0)
|
||||
break;
|
||||
|
||||
// Temporarilly add padding to the input data since some
|
||||
// codecs read in larger chunks and may accidently read
|
||||
// past the end of the allocated buffer
|
||||
stream->Data.resize(insize + FF_INPUT_BUFFER_PADDING_SIZE);
|
||||
stream->DecodedData.resize(AVCODEC_MAX_AUDIO_FRAME_SIZE);
|
||||
|
||||
int16_t *ptr = (int16_t*)&stream->DecodedData[0];
|
||||
int size = stream->DecodedData.size();
|
||||
int len = avcodec_decode_audio2(stream->CodecCtx, ptr, &size,
|
||||
&stream->Data[0], insize);
|
||||
if(len < 0)
|
||||
{
|
||||
stream->Data.resize(insize);
|
||||
break;
|
||||
}
|
||||
|
||||
int datarem = insize-len;
|
||||
if(datarem)
|
||||
memmove(&stream->Data[0], &stream->Data[len], datarem);
|
||||
stream->Data.resize(datarem);
|
||||
|
||||
stream->DecodedData.resize(size);
|
||||
if(stream->DecodedData.size() == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
size_t rem = length-dec;
|
||||
if(rem > stream->DecodedData.size())
|
||||
rem = stream->DecodedData.size();
|
||||
|
||||
memcpy(data, &stream->DecodedData[0], rem);
|
||||
data += rem;
|
||||
dec += rem;
|
||||
if(rem < stream->DecodedData.size())
|
||||
memmove(&stream->DecodedData[0], &stream->DecodedData[rem],
|
||||
stream->DecodedData.size() - rem);
|
||||
stream->DecodedData.resize(stream->DecodedData.size()-rem);
|
||||
}
|
||||
return dec;
|
||||
}
|
Loading…
Reference in New Issue