2020-05-28 16:04:01 +02:00
|
|
|
|
#include "bink.h"
|
|
|
|
|
|
2018-02-18 18:48:51 -03:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <AL/al.h>
|
2020-05-22 01:34:17 +02:00
|
|
|
|
#include <SDL.h>
|
2020-05-28 16:04:01 +02:00
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
#include <libavutil/avutil.h>
|
|
|
|
|
#include <libavutil/imgutils.h>
|
|
|
|
|
#include <libswscale/swscale.h>
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
|
|
|
|
extern int SoundSys_IsOn();
|
|
|
|
|
extern void DrawAvpMenuBink(char* buf, int width, int height, int pitch);
|
|
|
|
|
extern float PlatVolumeToGain(int volume);
|
|
|
|
|
|
|
|
|
|
//#define AL_CHECK() { int err = alGetError(); if(err!=AL_NO_ERROR) printf("%s:%d ALError %04x\n", __FILE__, __LINE__, err); }
|
|
|
|
|
#define AL_CHECK() {}
|
|
|
|
|
|
|
|
|
|
#define FRAMEQUEUESIZE 4
|
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
struct binkMovie {
|
2018-02-18 18:48:51 -03:00
|
|
|
|
AVFormatContext* avContext;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
AVPacket packet;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
|
|
|
|
int videoStreamIndex;
|
|
|
|
|
AVCodecContext* videoCodecContext;
|
|
|
|
|
AVFrame* videoFrame;
|
|
|
|
|
struct SwsContext* videoScaleContext;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
uint8_t* videoScalePicture[4];
|
|
|
|
|
int videoScaleLineSize[4];
|
2018-02-18 18:48:51 -03:00
|
|
|
|
uint videoScaleWidth;
|
|
|
|
|
uint videoScaleHeight;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
enum AVPixelFormat videoScaleFormat;
|
|
|
|
|
float videoFrameDuration;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
|
|
|
|
int audioStreamIndex;
|
|
|
|
|
AVCodecContext* audioCodecContext;
|
|
|
|
|
AVFrame* audioFrame;
|
|
|
|
|
char* audioTempBuffer;
|
|
|
|
|
|
|
|
|
|
BOOL alInited;
|
|
|
|
|
ALuint alSource;
|
|
|
|
|
ALuint alBuffers[FRAMEQUEUESIZE];
|
|
|
|
|
ALuint alFreeBuffers[FRAMEQUEUESIZE];
|
|
|
|
|
ALuint alNumFreeBuffers;
|
|
|
|
|
ALuint alNumChannels;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
ALenum alFormat;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
ALuint alSampleRate;
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
uint timeStart;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
BOOL looping;
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
static void BinkRenderMovie(struct binkMovie* aMovie)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie && aMovie->videoFrame && aMovie->videoScalePicture[0]) {
|
2018-02-18 18:48:51 -03:00
|
|
|
|
DrawAvpMenuBink(
|
2020-05-22 01:34:17 +02:00
|
|
|
|
aMovie->videoScalePicture[0],
|
2018-02-18 18:48:51 -03:00
|
|
|
|
aMovie->videoFrame->width,
|
|
|
|
|
aMovie->videoFrame->height,
|
2020-05-22 01:34:17 +02:00
|
|
|
|
aMovie->videoScaleLineSize[0]);
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
static void BinkInitMovieStruct(struct binkMovie* aMovie)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
{
|
2020-05-22 01:34:17 +02:00
|
|
|
|
*aMovie = (struct binkMovie){
|
|
|
|
|
.audioStreamIndex = -1,
|
|
|
|
|
.videoStreamIndex = -1,
|
|
|
|
|
.videoScaleFormat = AV_PIX_FMT_NONE,
|
|
|
|
|
};
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
static void BinkReleaseMovie(struct binkMovie* aMovie)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->alInited) {
|
2018-02-18 18:48:51 -03:00
|
|
|
|
alSourceStop(aMovie->alSource);
|
|
|
|
|
alDeleteSources(1, &aMovie->alSource);
|
|
|
|
|
alDeleteBuffers(FRAMEQUEUESIZE, aMovie->alBuffers);
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->audioTempBuffer)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
free(aMovie->audioTempBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->avContext)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
avformat_close_input(&aMovie->avContext);
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->audioCodecContext)
|
2020-05-22 01:34:17 +02:00
|
|
|
|
avcodec_free_context(&aMovie->audioCodecContext);
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->audioFrame)
|
2020-05-22 01:34:17 +02:00
|
|
|
|
av_frame_free(&aMovie->audioFrame);
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->videoCodecContext)
|
2020-05-22 01:34:17 +02:00
|
|
|
|
avcodec_free_context(&aMovie->videoCodecContext);
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->videoFrame)
|
2020-05-22 01:34:17 +02:00
|
|
|
|
av_frame_free(&aMovie->videoFrame);
|
2020-05-28 16:34:09 +02:00
|
|
|
|
|
|
|
|
|
if (aMovie->videoScaleContext) {
|
|
|
|
|
sws_freeContext(aMovie->videoScaleContext);
|
2020-05-28 16:04:01 +02:00
|
|
|
|
av_freep(&aMovie->videoScalePicture[0]);
|
2020-05-28 16:34:09 +02:00
|
|
|
|
}
|
2020-05-22 01:34:17 +02:00
|
|
|
|
|
2018-02-18 18:48:51 -03:00
|
|
|
|
BinkInitMovieStruct(aMovie);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
static int DecodeVideoFrame(struct binkMovie* aMovie)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (avcodec_receive_frame(aMovie->videoCodecContext, aMovie->videoFrame) != 0)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return 0;
|
|
|
|
|
|
2020-05-31 13:51:44 +02:00
|
|
|
|
// Initialize scale context.
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->videoScaleContext == NULL) {
|
|
|
|
|
if (aMovie->videoScaleWidth == 0)
|
|
|
|
|
aMovie->videoScaleWidth = aMovie->videoFrame->width;
|
|
|
|
|
if (aMovie->videoScaleHeight == 0)
|
|
|
|
|
aMovie->videoScaleHeight = aMovie->videoFrame->height;
|
|
|
|
|
if (aMovie->videoScaleFormat == AV_PIX_FMT_NONE)
|
|
|
|
|
aMovie->videoScaleFormat = AV_PIX_FMT_RGB565;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
|
|
|
|
|
aMovie->videoScaleContext = sws_getContext(
|
|
|
|
|
aMovie->videoFrame->width, aMovie->videoFrame->height,
|
|
|
|
|
aMovie->videoFrame->format,
|
|
|
|
|
aMovie->videoScaleWidth, aMovie->videoScaleHeight,
|
|
|
|
|
aMovie->videoScaleFormat,
|
|
|
|
|
SWS_FAST_BILINEAR, NULL, NULL, NULL);
|
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->videoScaleContext == NULL)
|
2020-05-22 01:34:17 +02:00
|
|
|
|
return 0;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
av_image_alloc(aMovie->videoScalePicture, aMovie->videoScaleLineSize,
|
|
|
|
|
aMovie->videoScaleWidth, aMovie->videoScaleHeight,
|
|
|
|
|
aMovie->videoScaleFormat, 1);
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
sws_scale(aMovie->videoScaleContext,
|
|
|
|
|
(const uint8_t* const*)aMovie->videoFrame->data,
|
|
|
|
|
aMovie->videoFrame->linesize, 0, aMovie->videoFrame->height,
|
|
|
|
|
aMovie->videoScalePicture, aMovie->videoScaleLineSize);
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
static int DecodeAudioFrame(struct binkMovie* aMovie)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (avcodec_receive_frame(aMovie->audioCodecContext, aMovie->audioFrame) != 0)
|
2020-05-22 01:34:17 +02:00
|
|
|
|
return 0;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (!SoundSys_IsOn())
|
2020-05-22 01:34:17 +02:00
|
|
|
|
return 0;
|
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (!aMovie->alInited) {
|
|
|
|
|
switch (aMovie->audioFrame->channel_layout) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
case AV_CH_LAYOUT_MONO:
|
2020-05-28 16:04:01 +02:00
|
|
|
|
aMovie->alFormat = (aMovie->audioFrame->format == AV_SAMPLE_FMT_U8) ?
|
|
|
|
|
AL_FORMAT_MONO8 : AL_FORMAT_MONO16;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
aMovie->alNumChannels = 1;
|
|
|
|
|
break;
|
|
|
|
|
case AV_CH_LAYOUT_STEREO:
|
2020-05-28 16:04:01 +02:00
|
|
|
|
aMovie->alFormat = (aMovie->audioFrame->format == AV_SAMPLE_FMT_U8) ?
|
|
|
|
|
AL_FORMAT_STEREO8 : AL_FORMAT_STEREO16;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
aMovie->alNumChannels = 2;
|
|
|
|
|
break;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
aMovie->alSampleRate = aMovie->audioFrame->sample_rate;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
aMovie->audioTempBuffer =
|
|
|
|
|
malloc(aMovie->alNumChannels * aMovie->audioFrame->nb_samples * 2 * 2);
|
|
|
|
|
aMovie->alInited = TRUE;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->alNumChannels == 0)
|
2020-05-22 01:34:17 +02:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// reclaim completed frames
|
2020-05-31 12:37:55 +02:00
|
|
|
|
int processed = 0;
|
|
|
|
|
alGetSourcei(aMovie->alSource, AL_BUFFERS_PROCESSED, &processed);
|
|
|
|
|
if (processed > 0) {
|
|
|
|
|
ALuint buffers[FRAMEQUEUESIZE];
|
|
|
|
|
alSourceUnqueueBuffers(aMovie->alSource, processed, buffers);
|
|
|
|
|
AL_CHECK();
|
|
|
|
|
for (int i = 0; i < processed; i++)
|
|
|
|
|
aMovie->alFreeBuffers[aMovie->alNumFreeBuffers++] = buffers[i];
|
2020-05-22 01:34:17 +02:00
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
// queue this frame
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->alNumFreeBuffers > 0) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
ALuint alBuffer = aMovie->alFreeBuffers[aMovie->alNumFreeBuffers-1];
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
int sampleCount = aMovie->audioFrame->nb_samples * aMovie->alNumChannels;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
uint dataSize = sampleCount * 2; // 16bit is deafult
|
|
|
|
|
void* data = (void*)aMovie->audioFrame->extended_data[0];
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
switch (aMovie->audioFrame->format) {
|
|
|
|
|
case AV_SAMPLE_FMT_U8: {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
dataSize = sampleCount;
|
|
|
|
|
} break;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
default:
|
2020-05-28 16:04:01 +02:00
|
|
|
|
case AV_SAMPLE_FMT_S16: {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
/*
|
|
|
|
|
unsigned short* p = (unsigned short*) data;
|
|
|
|
|
for(int i=0; i<sampleCount; i++)
|
|
|
|
|
p[i] -= p[i]>>2;
|
|
|
|
|
*/
|
|
|
|
|
} break;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
case AV_SAMPLE_FMT_FLT: {
|
|
|
|
|
data = (void*)aMovie->audioTempBuffer;
|
|
|
|
|
short* tempBuf = (short*)aMovie->audioTempBuffer;
|
|
|
|
|
float* srcBuf = (float*)aMovie->audioFrame->extended_data[0];
|
|
|
|
|
for (int i = 0; i < sampleCount; i++) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
float val = srcBuf[i] * 32768;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (val > 32767)
|
|
|
|
|
val = 32767;
|
|
|
|
|
if (val < -32768)
|
2020-05-31 10:54:52 +02:00
|
|
|
|
val = -32768;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
tempBuf[i] = (short)val;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
}
|
|
|
|
|
} break;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
case AV_SAMPLE_FMT_S32: {
|
|
|
|
|
data = (void*)aMovie->audioTempBuffer;
|
|
|
|
|
short* tempBuf = (short*)aMovie->audioTempBuffer;
|
|
|
|
|
unsigned int* srcBuf = (unsigned int*)aMovie->audioFrame->extended_data[0];
|
|
|
|
|
for(int i = 0; i < sampleCount; i++)
|
|
|
|
|
tempBuf[i] = (short)(((*srcBuf - *srcBuf>>2) >> 16) & 0x0000FFFF);
|
2020-05-22 01:34:17 +02:00
|
|
|
|
} break;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
case AV_SAMPLE_FMT_FLTP: {
|
|
|
|
|
data = (void*)aMovie->audioTempBuffer;
|
|
|
|
|
short* tempBuf = (short*)aMovie->audioTempBuffer;
|
|
|
|
|
for (int i = 0; i < aMovie->audioFrame->nb_samples; i++) {
|
|
|
|
|
for (int j = 0; j < aMovie->alNumChannels; j++) {
|
|
|
|
|
float* srcBuf = (float*)aMovie->audioFrame->extended_data[j];
|
2020-05-22 01:34:17 +02:00
|
|
|
|
float val = srcBuf[i] * 32768;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (val > 32767)
|
|
|
|
|
val = 32767;
|
|
|
|
|
if (val < -32768)
|
2020-05-31 10:54:52 +02:00
|
|
|
|
val = -32768;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
tempBuf[(i*aMovie->alNumChannels)+j] = (short)val;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
2020-05-22 01:34:17 +02:00
|
|
|
|
} break;
|
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
alSourceStop(aMovie->alSource);
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
alBufferData(alBuffer, aMovie->alFormat, data, dataSize - 16, aMovie->alSampleRate);
|
2020-05-22 01:34:17 +02:00
|
|
|
|
AL_CHECK();
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
alSourceQueueBuffers(aMovie->alSource, 1, &alBuffer);
|
|
|
|
|
AL_CHECK();
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
float vx, vy, vz;
|
|
|
|
|
alGetListener3f(AL_VELOCITY, &vx, &vy, &vz);
|
|
|
|
|
alSource3f(aMovie->alSource, AL_VELOCITY, vx, vy, vz);
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
alSourcePlay(aMovie->alSource);
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
aMovie->alNumFreeBuffers--;
|
|
|
|
|
aMovie->alFreeBuffers[aMovie->alNumFreeBuffers] = 0;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 13:02:27 +02:00
|
|
|
|
static int ReadPacket(struct binkMovie* aMovie)
|
2020-05-22 01:34:17 +02:00
|
|
|
|
{
|
|
|
|
|
// Read from file if no packet is buffered.
|
|
|
|
|
if (!aMovie->packet.buf && av_read_frame(aMovie->avContext, &aMovie->packet) < 0) {
|
2020-05-28 16:04:01 +02:00
|
|
|
|
// No more packets in file.
|
2020-05-22 01:34:17 +02:00
|
|
|
|
if (aMovie->looping) {
|
|
|
|
|
av_seek_frame(aMovie->avContext, -1, 0, 0);
|
2020-05-31 13:02:27 +02:00
|
|
|
|
return ReadPacket(aMovie);
|
2020-05-22 01:34:17 +02:00
|
|
|
|
} else {
|
|
|
|
|
// Drain buffered frames.
|
|
|
|
|
if (aMovie->videoStreamIndex >= 0)
|
|
|
|
|
avcodec_send_packet(aMovie->videoCodecContext, NULL);
|
|
|
|
|
if (aMovie->audioStreamIndex >= 0)
|
|
|
|
|
avcodec_send_packet(aMovie->audioCodecContext, NULL);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send the (possibly buffered) packet to decoder.
|
|
|
|
|
int ret = 0;
|
|
|
|
|
if (aMovie->packet.stream_index == aMovie->videoStreamIndex)
|
|
|
|
|
ret = avcodec_send_packet(aMovie->videoCodecContext, &aMovie->packet);
|
|
|
|
|
else if (aMovie->packet.stream_index == aMovie->audioStreamIndex)
|
|
|
|
|
ret = avcodec_send_packet(aMovie->audioCodecContext, &aMovie->packet);
|
|
|
|
|
|
|
|
|
|
// Keep the packet around for next time if decoder’s buffer is full.
|
|
|
|
|
if (ret == AVERROR(EAGAIN)) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
av_packet_unref(&aMovie->packet);
|
|
|
|
|
return ret < 0 ? 0 : 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
static int BinkStartMovie(struct binkMovie* aMovie, const char* aFilename,
|
|
|
|
|
BOOL aLoopFlag, BOOL aFmvFlag, BOOL aMusicFlag)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
{
|
2020-05-22 01:34:17 +02:00
|
|
|
|
BinkInitMovieStruct(aMovie);
|
|
|
|
|
aMovie->looping = aLoopFlag;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aFmvFlag) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
aMovie->videoScaleWidth = 128;
|
|
|
|
|
aMovie->videoScaleHeight = 96;
|
|
|
|
|
aMovie->videoScaleFormat = AV_PIX_FMT_RGB24;
|
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (avformat_open_input(&aMovie->avContext, aFilename, NULL, NULL) != 0)
|
2020-05-22 01:34:17 +02:00
|
|
|
|
return 0;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (avformat_find_stream_info(aMovie->avContext, NULL) < 0) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
BinkReleaseMovie(aMovie);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int numStreams = 0;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
for (int i = 0; i < aMovie->avContext->nb_streams; i++) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
const AVStream* stream = aMovie->avContext->streams[i];
|
|
|
|
|
const AVCodec* codec = avcodec_find_decoder(stream->codecpar->codec_id);
|
|
|
|
|
if (!codec)
|
|
|
|
|
continue;
|
|
|
|
|
AVCodecContext *context = avcodec_alloc_context3(codec);
|
|
|
|
|
if (!context)
|
|
|
|
|
continue;
|
|
|
|
|
if (avcodec_parameters_to_context(context, stream->codecpar) < 0 ||
|
2020-05-28 16:04:01 +02:00
|
|
|
|
avcodec_open2(context, codec, NULL) != 0) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
avcodec_free_context(&context);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->videoStreamIndex < 0 && context->codec_type == AVMEDIA_TYPE_VIDEO) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
aMovie->videoCodecContext = context;
|
|
|
|
|
aMovie->videoStreamIndex = i;
|
|
|
|
|
aMovie->videoFrame = av_frame_alloc();
|
|
|
|
|
aMovie->videoFrameDuration =
|
|
|
|
|
1000.0f * (float)stream->time_base.num / (float)stream->time_base.den;
|
|
|
|
|
numStreams++;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
} else if (aMovie->audioStreamIndex < 0 && context->codec_type == AVMEDIA_TYPE_AUDIO) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
aMovie->audioCodecContext = context;
|
|
|
|
|
aMovie->audioStreamIndex = i;
|
|
|
|
|
aMovie->audioFrame = av_frame_alloc();
|
2020-05-31 12:34:18 +02:00
|
|
|
|
|
|
|
|
|
alGenSources(1, &aMovie->alSource);
|
|
|
|
|
alSource3f(aMovie->alSource, AL_POSITION, 0.0, 0.0, 0.0);
|
|
|
|
|
alSource3f(aMovie->alSource, AL_VELOCITY, 0.0, 0.0, 0.0);
|
|
|
|
|
alSource3f(aMovie->alSource, AL_DIRECTION, 0.0, 0.0, 0.0);
|
|
|
|
|
alSourcef(aMovie->alSource, AL_ROLLOFF_FACTOR, 0.0);
|
|
|
|
|
alSourcei(aMovie->alSource, AL_SOURCE_RELATIVE, AL_TRUE);
|
|
|
|
|
alSourcef(aMovie->alSource, AL_PITCH, 1.0);
|
|
|
|
|
alSourcef(aMovie->alSource, AL_GAIN, 1.0);
|
|
|
|
|
|
2020-05-31 13:51:44 +02:00
|
|
|
|
alGenBuffers(FRAMEQUEUESIZE, aMovie->alBuffers);
|
2020-05-31 12:34:18 +02:00
|
|
|
|
aMovie->alNumFreeBuffers = FRAMEQUEUESIZE;
|
|
|
|
|
for (int i = 0; i < aMovie->alNumFreeBuffers; i++)
|
|
|
|
|
aMovie->alFreeBuffers[i] = aMovie->alBuffers[i];
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
numStreams++;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
} else {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
avcodec_free_context(&context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aMovie->videoStreamIndex < 0 && aMovie->audioStreamIndex < 0) {
|
2020-05-22 01:34:17 +02:00
|
|
|
|
BinkReleaseMovie(aMovie);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (!aFmvFlag) {
|
|
|
|
|
for (int i = 0; i < (FRAMEQUEUESIZE-1) * numStreams; i++)
|
2020-05-31 13:02:27 +02:00
|
|
|
|
ReadPacket(aMovie);
|
2020-05-22 01:34:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int BinkUpdateMovie(struct binkMovie* aMovie)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
{
|
|
|
|
|
if(!aMovie->avContext)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
const int t = SDL_GetTicks();
|
2020-05-31 13:02:27 +02:00
|
|
|
|
const int eof = !ReadPacket(aMovie);
|
2020-05-22 01:34:17 +02:00
|
|
|
|
|
|
|
|
|
int playing = 0;
|
|
|
|
|
if (aMovie->videoStreamIndex >= 0) {
|
|
|
|
|
if (aMovie->videoFrame->pts == 0)
|
|
|
|
|
aMovie->timeStart = t - aMovie->videoFrameDuration;
|
|
|
|
|
if (t - aMovie->timeStart >= aMovie->videoFrame->pts * aMovie->videoFrameDuration)
|
|
|
|
|
playing += DecodeVideoFrame(aMovie);
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
2020-05-22 01:34:17 +02:00
|
|
|
|
|
|
|
|
|
if (aMovie->audioStreamIndex >= 0) {
|
2018-02-18 18:48:51 -03:00
|
|
|
|
int processedBuffers = 0;
|
|
|
|
|
alGetSourcei(aMovie->alSource, AL_BUFFERS_PROCESSED, &processedBuffers);
|
2020-05-22 01:34:17 +02:00
|
|
|
|
while (!aMovie->alInited || aMovie->alNumFreeBuffers>0 || processedBuffers>0) {
|
|
|
|
|
if (DecodeAudioFrame(aMovie))
|
|
|
|
|
playing += 1;
|
|
|
|
|
else
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
2020-05-28 16:04:01 +02:00
|
|
|
|
|
2020-05-22 01:34:17 +02:00
|
|
|
|
return !eof || playing;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
void PlayBinkedFMV(char* filenamePtr, int volume)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
{
|
|
|
|
|
struct binkMovie movie;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
if (BinkStartMovie(&movie, filenamePtr, FALSE, FALSE, FALSE)) {
|
2018-02-18 18:48:51 -03:00
|
|
|
|
alSourcef(movie.alSource, AL_GAIN, PlatVolumeToGain(volume));
|
2020-05-22 01:34:17 +02:00
|
|
|
|
while (BinkUpdateMovie(&movie)) {
|
2018-02-18 18:48:51 -03:00
|
|
|
|
BinkRenderMovie(&movie);
|
|
|
|
|
FlipBuffers();
|
2020-05-22 01:34:17 +02:00
|
|
|
|
SDL_Delay(4); // don’t just burn it
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
|
|
|
|
BinkReleaseMovie(&movie);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
struct binkMovie menuBackgroundMovie;
|
|
|
|
|
|
|
|
|
|
void StartMenuBackgroundBink()
|
|
|
|
|
{
|
|
|
|
|
BinkStartMovie(&menuBackgroundMovie, "FMVs/Menubackground.bik", TRUE, FALSE, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PlayMenuBackgroundBink()
|
|
|
|
|
{
|
|
|
|
|
ClearScreenToBlack();
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (BinkUpdateMovie(&menuBackgroundMovie)) {
|
2018-02-18 18:48:51 -03:00
|
|
|
|
BinkRenderMovie(&menuBackgroundMovie);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EndMenuBackgroundBink()
|
|
|
|
|
{
|
|
|
|
|
BinkReleaseMovie(&menuBackgroundMovie);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
struct binkMovie musicMovie;
|
|
|
|
|
|
|
|
|
|
int StartMusicBink(char* filenamePtr, BOOL looping)
|
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (!SoundSys_IsOn())
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return 0;
|
2020-05-28 16:04:01 +02:00
|
|
|
|
return BinkStartMovie(&musicMovie, filenamePtr, looping, FALSE, TRUE);
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int PlayMusicBink(int volume)
|
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (!SoundSys_IsOn())
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return 1;
|
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (!musicMovie.avContext)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return 1;
|
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (musicMovie.audioStreamIndex < 0 || !musicMovie.alInited)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
alSourcef(musicMovie.alSource, AL_GAIN, PlatVolumeToGain(volume));
|
2020-05-28 16:04:01 +02:00
|
|
|
|
for (int i = 0; i < musicMovie.avContext->nb_streams * FRAMEQUEUESIZE; i++) {
|
2018-02-18 18:48:51 -03:00
|
|
|
|
int processedBuffers = 0;
|
|
|
|
|
alGetSourcei(musicMovie.alSource, AL_BUFFERS_PROCESSED, &processedBuffers);
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (processedBuffers + musicMovie.alNumFreeBuffers > 0)
|
2020-05-31 13:02:27 +02:00
|
|
|
|
if (!ReadPacket(&musicMovie))
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EndMusicBink()
|
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (SoundSys_IsOn())
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return;
|
|
|
|
|
BinkReleaseMovie(&musicMovie);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
FMVHandle CreateBinkFMV(char* filenamePtr)
|
|
|
|
|
{
|
|
|
|
|
struct binkMovie* movie = malloc(sizeof(struct binkMovie));
|
|
|
|
|
BinkInitMovieStruct(movie);
|
|
|
|
|
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (!BinkStartMovie(movie, filenamePtr, FALSE, TRUE, FALSE)) {
|
2018-02-18 18:48:51 -03:00
|
|
|
|
free(movie);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return (FMVHandle)movie;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int UpdateBinkFMV(FMVHandle aFmvHandle, int volume)
|
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aFmvHandle == 0)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
struct binkMovie* movie = (struct binkMovie*)aFmvHandle;
|
|
|
|
|
alSourcef(movie->alSource, AL_GAIN, PlatVolumeToGain(volume));
|
|
|
|
|
BinkUpdateMovie(movie);
|
|
|
|
|
BinkUpdateMovie(movie);
|
|
|
|
|
BinkUpdateMovie(movie);
|
|
|
|
|
BinkUpdateMovie(movie);
|
|
|
|
|
return BinkUpdateMovie(movie);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CloseBinkFMV(FMVHandle aFmvHandle)
|
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aFmvHandle == 0)
|
2020-04-18 23:31:03 +02:00
|
|
|
|
return;
|
2018-02-18 18:48:51 -03:00
|
|
|
|
|
|
|
|
|
struct binkMovie* movie = (struct binkMovie*)aFmvHandle;
|
|
|
|
|
BinkReleaseMovie(movie);
|
|
|
|
|
free(movie);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* GetBinkFMVImage(FMVHandle aFmvHandle)
|
|
|
|
|
{
|
2020-05-28 16:04:01 +02:00
|
|
|
|
if (aFmvHandle == 0)
|
2018-02-18 18:48:51 -03:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
struct binkMovie* movie = (struct binkMovie*)aFmvHandle;
|
|
|
|
|
if(!movie->videoScaleContext)
|
|
|
|
|
return 0;
|
2020-05-22 01:34:17 +02:00
|
|
|
|
return movie->videoScalePicture[0];
|
2018-02-18 18:48:51 -03:00
|
|
|
|
}
|