2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavdevice/avdevice.h>
|
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <kos32sys.h>
|
2014-08-07 00:17:00 +02:00
|
|
|
#include "winlib/winlib.h"
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
#include "sound.h"
|
|
|
|
#include "fplay.h"
|
|
|
|
|
2016-02-01 17:55:10 +01:00
|
|
|
#ifdef HAVE_VAAPI
|
|
|
|
int va_check_codec_support(enum AVCodecID id);
|
|
|
|
#endif
|
|
|
|
|
2014-01-10 14:47:21 +01:00
|
|
|
volatile enum player_state player_state = STOP;
|
|
|
|
volatile enum player_state decoder_state = PREPARE;
|
|
|
|
volatile enum player_state sound_state = STOP;
|
|
|
|
|
|
|
|
uint32_t win_width, win_height;
|
|
|
|
|
|
|
|
|
2016-02-01 20:27:17 +01:00
|
|
|
AVFrame *pFrame;
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 20:27:17 +01:00
|
|
|
int have_sound = 0;
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 20:27:17 +01:00
|
|
|
uint8_t *decoder_buffer;
|
2014-01-10 14:47:21 +01:00
|
|
|
extern int resampler_size;
|
|
|
|
|
|
|
|
extern int sample_rate;
|
|
|
|
char *movie_file;
|
|
|
|
|
2016-02-01 17:55:10 +01:00
|
|
|
void flush_video(vst_t* vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
|
2014-01-10 14:47:21 +01:00
|
|
|
int64_t rewind_pos;
|
|
|
|
|
|
|
|
int64_t stream_duration;
|
|
|
|
|
2015-07-23 12:13:34 +02:00
|
|
|
int threads_running = DECODER_THREAD;
|
|
|
|
|
2014-01-10 14:47:21 +01:00
|
|
|
extern double audio_base;
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
|
|
|
|
double get_audio_base(vst_t* vst)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
return (double)av_q2d(vst->fCtx->streams[vst->aStream]->time_base)*1000;
|
2014-01-10 14:47:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char *argv[])
|
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
static vst_t vst;
|
|
|
|
int i, ret;
|
2014-01-10 14:47:21 +01:00
|
|
|
char *file_name, *dot;
|
|
|
|
|
|
|
|
if(argc < 2)
|
|
|
|
{
|
|
|
|
movie_file = get_moviefile();
|
|
|
|
if(movie_file == NULL)
|
|
|
|
{
|
|
|
|
printf("Please provide a movie file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else movie_file = argv[1];
|
|
|
|
|
|
|
|
/* register all codecs, demux and protocols */
|
|
|
|
|
|
|
|
av_log_set_level(AV_LOG_FATAL);
|
|
|
|
|
|
|
|
avcodec_register_all();
|
|
|
|
avdevice_register_all();
|
|
|
|
av_register_all();
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
if( avformat_open_input(&vst.fCtx, movie_file, NULL, NULL) < 0)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
|
|
|
printf("Cannot open file %s\n\r", movie_file);
|
|
|
|
return -1; // Couldn't open file
|
|
|
|
};
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.fCtx->flags |= AVFMT_FLAG_GENPTS;
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
// Retrieve stream information
|
2016-02-01 16:10:02 +01:00
|
|
|
if(avformat_find_stream_info(vst.fCtx, NULL) < 0)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
|
|
|
printf("Cannot find streams\n\r");
|
|
|
|
return -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
file_name = strrchr(movie_file,'/')+1;
|
|
|
|
dot = strrchr(file_name,'.');
|
|
|
|
if(dot)
|
|
|
|
{
|
|
|
|
movie_file = malloc(dot-file_name+1);
|
|
|
|
memcpy(movie_file, file_name, dot-file_name);
|
|
|
|
movie_file[dot-file_name] = 0;
|
|
|
|
}
|
|
|
|
else movie_file = file_name;
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
stream_duration = vst.fCtx->duration;
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
// Find the first video stream
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.vStream = -1;
|
|
|
|
vst.aStream = -1;
|
|
|
|
|
|
|
|
for(i=0; i < vst.fCtx->nb_streams; i++)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
if(vst.fCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO
|
|
|
|
&& vst.vStream < 0)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.vStream = i;
|
|
|
|
video_time_base = vst.fCtx->streams[i]->time_base;
|
2014-01-10 14:47:21 +01:00
|
|
|
if(stream_duration == 0)
|
2016-02-01 16:10:02 +01:00
|
|
|
stream_duration = vst.fCtx->streams[i]->duration;
|
2014-01-10 14:47:21 +01:00
|
|
|
}
|
2016-02-01 16:10:02 +01:00
|
|
|
|
|
|
|
if(vst.fCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
|
|
|
|
vst.aStream < 0)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.aStream = i;
|
2014-01-10 14:47:21 +01:00
|
|
|
if(stream_duration == 0)
|
2016-02-01 16:10:02 +01:00
|
|
|
stream_duration = vst.fCtx->streams[i]->duration;
|
2014-01-10 14:47:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
if(vst.vStream==-1)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
|
|
|
printf("Video stream not detected\n\r");
|
|
|
|
return -1; // Didn't find a video stream
|
|
|
|
};
|
|
|
|
|
|
|
|
// __asm__ __volatile__("int3");
|
|
|
|
|
|
|
|
// Get a pointer to the codec context for the video stream
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.vCtx = vst.fCtx->streams[vst.vStream]->codec;
|
|
|
|
vst.aCtx = vst.fCtx->streams[vst.aStream]->codec;
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.vCodec = avcodec_find_decoder(vst.vCtx->codec_id);
|
|
|
|
printf("codec id %x name %s\n",vst.vCtx->codec_id, vst.vCodec->name);
|
|
|
|
printf("ctx->pix_fmt %d\n", vst.vCtx->pix_fmt);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
if(vst.vCodec == NULL)
|
|
|
|
{
|
2014-01-10 14:47:21 +01:00
|
|
|
printf("Unsupported codec with id %d for input stream %d\n",
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.vCtx->codec_id, vst.vStream);
|
2014-01-10 14:47:21 +01:00
|
|
|
return -1; // Codec not found
|
|
|
|
}
|
|
|
|
|
2016-02-01 20:27:17 +01:00
|
|
|
if(fplay_init_context(&vst))
|
|
|
|
return -1;
|
2016-02-01 16:10:02 +01:00
|
|
|
|
|
|
|
if(avcodec_open2(vst.vCtx, vst.vCodec, NULL) < 0)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
|
|
|
printf("Error while opening codec for input stream %d\n",
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.vStream);
|
2014-01-10 14:47:21 +01:00
|
|
|
return -1; // Could not open codec
|
|
|
|
};
|
|
|
|
|
2016-02-01 20:27:17 +01:00
|
|
|
printf("ctx->pix_fmt %d\n", vst.vCtx->pix_fmt);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
mutex_init(&vst.q_video.lock);
|
|
|
|
mutex_init(&vst.q_audio.lock);
|
|
|
|
mutex_init(&vst.gpu_lock);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
if (vst.aCtx->channels > 0)
|
|
|
|
vst.aCtx->request_channels = FFMIN(2, vst.aCtx->channels);
|
2014-01-10 14:47:21 +01:00
|
|
|
else
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.aCtx->request_channels = 2;
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.aCodec = avcodec_find_decoder(vst.aCtx->codec_id);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
if(vst.aCodec)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
if(avcodec_open2(vst.aCtx, vst.aCodec, NULL) >= 0 )
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
|
|
|
WAVEHEADER whdr;
|
|
|
|
int fmt;
|
|
|
|
int channels;
|
|
|
|
|
|
|
|
printf("audio stream rate %d channels %d format %d\n",
|
2016-02-01 16:10:02 +01:00
|
|
|
vst.aCtx->sample_rate, vst.aCtx->channels, vst.aCtx->sample_fmt );
|
2014-01-10 14:47:21 +01:00
|
|
|
whdr.riff_id = 0x46464952;
|
|
|
|
whdr.riff_format = 0x45564157;
|
|
|
|
whdr.wFormatTag = 0x01;
|
2016-02-01 16:10:02 +01:00
|
|
|
whdr.nSamplesPerSec = vst.aCtx->sample_rate;
|
2014-01-10 14:47:21 +01:00
|
|
|
whdr.nChannels = 2;
|
|
|
|
whdr.wBitsPerSample = 16;
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
sample_rate = vst.aCtx->sample_rate;
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
fmt = test_wav(&whdr);
|
|
|
|
|
|
|
|
if( init_audio(fmt) )
|
|
|
|
{
|
|
|
|
decoder_buffer = (uint8_t*)av_mallocz(192000*2+64);
|
|
|
|
if( decoder_buffer != NULL )
|
|
|
|
{
|
2015-07-23 12:13:34 +02:00
|
|
|
mutex_init(&astream.lock);
|
2014-01-10 14:47:21 +01:00
|
|
|
astream.count = 0;
|
|
|
|
astream.buffer = (char *)av_mallocz(192000*3);
|
|
|
|
if( astream.buffer != NULL )
|
|
|
|
have_sound = 1;
|
|
|
|
else
|
|
|
|
av_free(decoder_buffer);
|
|
|
|
}
|
|
|
|
if( have_sound == 0)
|
|
|
|
{
|
|
|
|
printf("Not enough memory for audio buffers\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else printf("Cannot open audio codec\n\r");
|
|
|
|
}
|
|
|
|
else printf("Unsupported audio codec!\n");
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
if(!init_video(&vst))
|
2014-01-10 14:47:21 +01:00
|
|
|
return 0;
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
decoder(&vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
// Free the YUV frame
|
|
|
|
av_free(pFrame);
|
|
|
|
|
|
|
|
|
|
|
|
//__asm__ __volatile__("int3");
|
|
|
|
|
2015-07-23 12:13:34 +02:00
|
|
|
while( threads_running &
|
|
|
|
(AUDIO_THREAD | VIDEO_THREAD))
|
|
|
|
delay(1);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2015-07-23 12:13:34 +02:00
|
|
|
if(astream.lock.handle)
|
|
|
|
mutex_destroy(&astream.lock);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
mutex_destroy(&vst.q_video.lock);
|
|
|
|
mutex_destroy(&vst.q_audio.lock);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
static int load_frame(vst_t *vst)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
|
|
|
AVPacket packet;
|
|
|
|
int err;
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
err = av_read_frame(vst->fCtx, &packet);
|
2014-01-10 14:47:21 +01:00
|
|
|
if( err == 0)
|
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
if(packet.stream_index == vst->vStream)
|
|
|
|
put_packet(&vst->q_video, &packet);
|
|
|
|
else if( (packet.stream_index == vst->aStream) &&
|
2014-01-10 14:47:21 +01:00
|
|
|
(have_sound != 0) )
|
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
put_packet(&vst->q_audio, &packet);
|
2014-01-10 14:47:21 +01:00
|
|
|
if(audio_base == -1.0)
|
|
|
|
{
|
|
|
|
if (packet.pts != AV_NOPTS_VALUE)
|
2016-02-01 16:10:02 +01:00
|
|
|
audio_base = get_audio_base(vst) * packet.pts;
|
2014-01-10 14:47:21 +01:00
|
|
|
// printf("audio base %f\n", audio_base);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else av_free_packet(&packet);
|
|
|
|
}
|
|
|
|
else if (err != AVERROR_EOF)
|
|
|
|
printf("av_read_frame: error %x\n", err);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
static int fill_queue(vst_t* vst)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
AVPacket packet;
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
while( (vst->q_video.size < 4*1024*1024) && !err )
|
|
|
|
err = load_frame(vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
return err;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
static void flush_all(vst_t* vst)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
|
|
|
AVPacket packet;
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
avcodec_flush_buffers(vst->vCtx);
|
|
|
|
avcodec_flush_buffers(vst->aCtx);
|
|
|
|
while( get_packet(&vst->q_video, &packet) != 0)
|
2014-01-10 14:47:21 +01:00
|
|
|
av_free_packet(&packet);
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
while( get_packet(&vst->q_audio, &packet)!= 0)
|
2014-01-10 14:47:21 +01:00
|
|
|
av_free_packet(&packet);
|
|
|
|
|
2016-02-01 17:55:10 +01:00
|
|
|
flush_video(vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
astream.count = 0;
|
|
|
|
};
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
void decoder(vst_t* vst)
|
2014-01-10 14:47:21 +01:00
|
|
|
{
|
|
|
|
int eof;
|
|
|
|
AVPacket packet;
|
|
|
|
int ret, vret, aret;
|
|
|
|
|
|
|
|
int64_t min_pos, max_pos;
|
|
|
|
|
2015-07-21 16:28:32 +02:00
|
|
|
// av_log_set_level(AV_LOG_DEBUG);
|
|
|
|
|
2014-01-10 14:47:21 +01:00
|
|
|
while( player_state != CLOSED )
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
switch(decoder_state)
|
|
|
|
{
|
|
|
|
case PREPARE:
|
2016-02-01 16:10:02 +01:00
|
|
|
eof = fill_queue(vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
if( (vst->q_video.size < 4*1024*1024) &&
|
2014-01-10 14:47:21 +01:00
|
|
|
(eof == 0) )
|
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
eof = load_frame(vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
}
|
2016-02-01 16:10:02 +01:00
|
|
|
decode_video(vst);
|
|
|
|
ret = decode_audio(vst->aCtx, &vst->q_audio);
|
2014-01-10 14:47:21 +01:00
|
|
|
}while(astream.count < resampler_size*2 &&
|
|
|
|
ret == 1);
|
|
|
|
|
|
|
|
sound_state = PREPARE;
|
|
|
|
decoder_state = PLAY;
|
|
|
|
player_state = PLAY;
|
|
|
|
|
|
|
|
case PLAY:
|
2016-02-01 16:10:02 +01:00
|
|
|
if( (vst->q_video.size < 4*1024*1024) &&
|
2014-01-10 14:47:21 +01:00
|
|
|
(eof == 0) )
|
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
eof = load_frame(vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
}
|
2016-02-01 16:10:02 +01:00
|
|
|
vret = decode_video(vst);
|
|
|
|
aret = decode_audio(vst->aCtx, &vst->q_audio);
|
2014-01-10 14:47:21 +01:00
|
|
|
ret = vret | aret;
|
|
|
|
|
|
|
|
if( eof && !ret)
|
|
|
|
{
|
|
|
|
decoder_state = STOP;
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
if( (vret & aret) == -1)
|
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
if( (vst->q_video.size < 4*1024*1024) &&
|
2014-01-10 14:47:21 +01:00
|
|
|
(eof == 0) )
|
|
|
|
{
|
2016-02-01 16:10:02 +01:00
|
|
|
eof = load_frame(vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
yield();
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
delay(1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case STOP:
|
|
|
|
delay(1);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
case PLAY_2_STOP:
|
|
|
|
while(sound_state != STOP)
|
|
|
|
delay(1);
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
flush_all(vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
if (vst->fCtx->start_time != AV_NOPTS_VALUE)
|
|
|
|
rewind_pos = vst->fCtx->start_time;
|
2014-01-10 14:47:21 +01:00
|
|
|
else
|
|
|
|
rewind_pos = 0;
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
ret = avformat_seek_file(vst->fCtx, -1, INT64_MIN,
|
2014-01-10 14:47:21 +01:00
|
|
|
rewind_pos, INT64_MAX, 0);
|
|
|
|
|
|
|
|
decoder_state = STOP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case REWIND:
|
|
|
|
while(sound_state != STOP)
|
|
|
|
yield();
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
flush_all(vst);
|
2014-01-10 14:47:21 +01:00
|
|
|
int opts = 0;
|
|
|
|
if(rewind_pos < 0)
|
|
|
|
{
|
|
|
|
rewind_pos = -rewind_pos;
|
|
|
|
opts = AVSEEK_FLAG_BACKWARD;
|
|
|
|
};
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
if (vst->fCtx->start_time != AV_NOPTS_VALUE)
|
|
|
|
rewind_pos += vst->fCtx->start_time;
|
2014-01-10 14:47:21 +01:00
|
|
|
|
|
|
|
// printf("rewind %8"PRId64"\n", rewind_pos);
|
|
|
|
min_pos = rewind_pos - 1000000;
|
|
|
|
max_pos = rewind_pos + 1000000;
|
|
|
|
|
2016-02-01 16:10:02 +01:00
|
|
|
ret = avformat_seek_file(vst->fCtx, -1, INT64_MIN,
|
2014-01-10 14:47:21 +01:00
|
|
|
rewind_pos, INT64_MAX, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("could not seek to position %f\n",
|
|
|
|
(double)rewind_pos / AV_TIME_BASE);
|
|
|
|
}
|
|
|
|
decoder_state = PREPARE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2015-07-21 16:28:32 +02:00
|
|
|
|