2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libswscale/swscale.h>
|
2012-02-13 23:34:36 +01:00
|
|
|
#include "sound.h"
|
2010-11-10 06:51:05 +01:00
|
|
|
#include "fplay.h"
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2012-02-13 23:34:36 +01:00
|
|
|
AVPicture picture;
|
2010-11-10 06:51:05 +01:00
|
|
|
double pts;
|
|
|
|
volatile int ready;
|
|
|
|
}vframe_t;
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
vframe_t frames[4];
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
struct SwsContext *cvt_ctx = NULL;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
int vfx = 0;
|
|
|
|
int dfx = 0;
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
render_t *render;
|
|
|
|
|
2010-11-10 06:51:05 +01:00
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
|
|
|
|
AVRational video_time_base;
|
|
|
|
AVFrame *Frame;
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
volatile uint32_t driver_lock;
|
|
|
|
|
|
|
|
void get_client_rect(rect_t *rc);
|
|
|
|
|
|
|
|
|
2010-11-10 06:51:05 +01:00
|
|
|
int init_video(AVCodecContext *ctx)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
width = ctx->width;
|
|
|
|
height = ctx->height;
|
|
|
|
|
|
|
|
printf("w = %d h = %d\n\r", width, height);
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
// __asm__ __volatile__("int3");
|
|
|
|
|
|
|
|
render = create_render(ctx->width, ctx->height,
|
|
|
|
ctx->pix_fmt, HW_BIT_BLIT|HW_TEX_BLIT);
|
|
|
|
if( render == NULL)
|
2010-11-10 06:51:05 +01:00
|
|
|
{
|
2012-02-13 23:34:36 +01:00
|
|
|
printf("Cannot create render\n\r");
|
2010-11-10 06:51:05 +01:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
Frame = avcodec_alloc_frame();
|
|
|
|
if ( Frame == NULL )
|
2010-11-10 06:51:05 +01:00
|
|
|
{
|
2012-02-13 23:34:36 +01:00
|
|
|
printf("Cannot alloc video frame\n\r");
|
|
|
|
return 0;
|
|
|
|
};
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
for( i=0; i < 4; i++)
|
2010-11-10 06:51:05 +01:00
|
|
|
{
|
2012-02-13 23:34:36 +01:00
|
|
|
int ret;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
// printf("alloc picture %d %d %x\n",
|
|
|
|
// ctx->width, ctx->height, ctx->pix_fmt );
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
ret = avpicture_alloc(&frames[i].picture, ctx->pix_fmt,
|
2010-11-10 06:51:05 +01:00
|
|
|
ctx->width, ctx->height);
|
2012-02-13 23:34:36 +01:00
|
|
|
if ( ret != 0 )
|
|
|
|
{
|
|
|
|
printf("Cannot alloc video buffer\n\r");
|
|
|
|
return 0;
|
2010-11-10 06:51:05 +01:00
|
|
|
};
|
2012-02-13 23:34:36 +01:00
|
|
|
|
|
|
|
frames[i].pts = 0;
|
|
|
|
frames[i].ready = 0;
|
2010-11-10 06:51:05 +01:00
|
|
|
};
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
create_thread(video_thread, ctx, 1024*1024);
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
delay(50);
|
2010-11-10 06:51:05 +01:00
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
int frameFinished=0;
|
2012-02-13 23:34:36 +01:00
|
|
|
static int frame_count;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
int decode_video(AVCodecContext *ctx, queue_t *qv)
|
2010-11-10 06:51:05 +01:00
|
|
|
{
|
2012-02-13 23:34:36 +01:00
|
|
|
AVPacket pkt;
|
2010-11-10 06:51:05 +01:00
|
|
|
double pts;
|
|
|
|
double av_time;
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
if(frames[dfx].ready != 0 )
|
|
|
|
return 1;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
if( get_packet(qv, &pkt) == 0 )
|
|
|
|
return 0;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
frameFinished = 0;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
ctx->reordered_opaque = pkt.pts;
|
|
|
|
|
|
|
|
if(avcodec_decode_video2(ctx, Frame, &frameFinished, &pkt) <= 0)
|
|
|
|
printf("video decoder error\n");
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
if(frameFinished)
|
|
|
|
{
|
2012-02-13 23:34:36 +01:00
|
|
|
AVPicture *dst_pic;
|
|
|
|
|
|
|
|
|
|
|
|
if( pkt.dts == AV_NOPTS_VALUE &&
|
|
|
|
Frame->reordered_opaque != AV_NOPTS_VALUE)
|
|
|
|
pts = Frame->reordered_opaque;
|
|
|
|
else if(pkt.dts != AV_NOPTS_VALUE)
|
|
|
|
pts= pkt.dts;
|
|
|
|
else
|
|
|
|
pts= 0;
|
|
|
|
|
|
|
|
// pts = *(int64_t*)av_opt_ptr(avcodec_get_frame_class(),
|
|
|
|
// Frame, "best_effort_timestamp");
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
// if (pts == AV_NOPTS_VALUE)
|
|
|
|
// pts = 0;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
pts *= av_q2d(video_time_base);
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
dst_pic = &frames[dfx].picture;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
av_image_copy(dst_pic->data, dst_pic->linesize, Frame->data,
|
|
|
|
Frame->linesize, ctx->pix_fmt, ctx->width, ctx->height);
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
frames[dfx].pts = pts*1000.0;
|
|
|
|
frames[dfx].ready = 1;
|
|
|
|
|
|
|
|
dfx++;
|
2012-02-13 23:34:36 +01:00
|
|
|
dfx&= 3;
|
2010-11-10 06:51:05 +01:00
|
|
|
};
|
2012-02-13 23:34:36 +01:00
|
|
|
av_free_packet(&pkt);
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
return 1;
|
2010-11-10 06:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern volatile uint32_t status;
|
2012-02-13 23:34:36 +01:00
|
|
|
rect_t win_rect;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
int check_events()
|
2010-11-10 06:51:05 +01:00
|
|
|
{
|
|
|
|
int ev;
|
|
|
|
|
|
|
|
ev = check_os_event();
|
|
|
|
|
|
|
|
switch(ev)
|
|
|
|
{
|
2012-02-13 23:34:36 +01:00
|
|
|
case 1:
|
|
|
|
render_adjust_size(render);
|
|
|
|
BeginDraw();
|
|
|
|
DrawWindow(0,0,0,0, NULL, 0x000000,0x73);
|
|
|
|
EndDraw();
|
2010-11-10 06:51:05 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
if(get_os_button()==1)
|
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
extern char *movie_file;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
int video_thread(void *param)
|
2010-11-10 06:51:05 +01:00
|
|
|
{
|
2012-02-13 23:34:36 +01:00
|
|
|
rect_t rc;
|
|
|
|
AVCodecContext *ctx = param;
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
BeginDraw();
|
|
|
|
DrawWindow(10, 10, width+9, height+26, movie_file, 0x000000,0x73);
|
|
|
|
EndDraw();
|
2010-11-10 06:51:05 +01:00
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
render_adjust_size(render);
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
while( status != 0)
|
|
|
|
{
|
|
|
|
double ctime;
|
|
|
|
double fdelay;
|
|
|
|
|
|
|
|
check_events();
|
|
|
|
|
|
|
|
if(frames[vfx].ready == 1 )
|
|
|
|
{
|
|
|
|
ctime = get_master_clock();
|
|
|
|
fdelay = (frames[vfx].pts - ctime);
|
2012-02-13 23:34:36 +01:00
|
|
|
|
|
|
|
// printf("pts %f time %f delay %f\n",
|
|
|
|
// frames[vfx].pts, ctime, fdelay);
|
2010-11-10 06:51:05 +01:00
|
|
|
|
|
|
|
if(fdelay < 0.0 )
|
|
|
|
{
|
|
|
|
int next_vfx;
|
|
|
|
fdelay = 0;
|
2012-02-13 23:34:36 +01:00
|
|
|
next_vfx = (vfx+1) & 3;
|
2010-11-10 06:51:05 +01:00
|
|
|
if( frames[next_vfx].ready == 1 )
|
|
|
|
{
|
|
|
|
if(frames[next_vfx].pts <= ctime)
|
|
|
|
{
|
|
|
|
frames[vfx].ready = 0; // skip this frame
|
|
|
|
vfx++;
|
2012-02-13 23:34:36 +01:00
|
|
|
vfx&= 3;
|
2010-11-10 06:51:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( (frames[next_vfx].pts - ctime) <
|
|
|
|
( ctime - frames[vfx].pts) )
|
|
|
|
{
|
|
|
|
frames[vfx].ready = 0; // skip this frame
|
|
|
|
vfx++;
|
2012-02-13 23:34:36 +01:00
|
|
|
vfx&= 3;
|
2010-11-10 06:51:05 +01:00
|
|
|
fdelay = (frames[next_vfx].pts - ctime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
if(fdelay > 10.0)
|
|
|
|
{
|
|
|
|
delay( (uint32_t)(fdelay/10.0));
|
|
|
|
};
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
// blit_bitmap(&frames[vfx].bitmap, 5, 22, width, height);
|
|
|
|
// frames[vfx].frame->linesize[0]);
|
|
|
|
render->draw(render, &frames[vfx].picture);
|
2010-11-10 06:51:05 +01:00
|
|
|
frames[vfx].ready = 0;
|
|
|
|
vfx++;
|
2012-02-13 23:34:36 +01:00
|
|
|
vfx&= 3;
|
2010-11-10 06:51:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
yield();
|
|
|
|
};
|
|
|
|
};
|
2012-02-13 23:34:36 +01:00
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void draw_hw_picture(render_t *render, AVPicture *picture);
|
|
|
|
void draw_sw_picture(render_t *render, AVPicture *picture);
|
|
|
|
|
|
|
|
render_t *create_render(uint32_t width, uint32_t height,
|
|
|
|
uint32_t ctx_format, uint32_t flags)
|
|
|
|
{
|
|
|
|
render_t *ren;
|
|
|
|
|
|
|
|
render = (render_t*)malloc(sizeof(*ren));
|
|
|
|
memset(ren, 0, sizeof(*ren));
|
|
|
|
|
|
|
|
render->ctx_width = width;
|
|
|
|
render->ctx_height = height;
|
|
|
|
render->ctx_format = ctx_format;
|
|
|
|
|
|
|
|
mutex_lock(&driver_lock);
|
|
|
|
render->caps = InitPixlib(flags);
|
|
|
|
mutex_unlock(&driver_lock);
|
|
|
|
|
|
|
|
if(render->caps==0)
|
|
|
|
{
|
|
|
|
printf("FPlay render engine: Hardware acceleration disabled\n");
|
|
|
|
render->draw = draw_sw_picture;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
render->target = 0;
|
|
|
|
render->draw = draw_hw_picture;
|
|
|
|
};
|
|
|
|
|
|
|
|
render->state = EMPTY;
|
|
|
|
return render;
|
|
|
|
};
|
|
|
|
|
|
|
|
int render_set_size(render_t *render, int width, int height)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
render->win_width = width;
|
|
|
|
render->win_height = height;
|
|
|
|
render->win_state = NORMAL;
|
|
|
|
|
|
|
|
// printf("%s %dx%d\n",__FUNCTION__, width, height);
|
|
|
|
|
|
|
|
if(render->state == EMPTY)
|
|
|
|
{
|
|
|
|
if(render->caps & HW_TEX_BLIT)
|
|
|
|
{
|
|
|
|
for( i=0; i < 4; i++)
|
|
|
|
{
|
|
|
|
render->bitmap[i].width = render->ctx_width;
|
|
|
|
render->bitmap[i].height = render->ctx_height;
|
|
|
|
|
|
|
|
if( create_bitmap(&render->bitmap[i]) != 0 )
|
|
|
|
{
|
|
|
|
status = 0;
|
|
|
|
/*
|
|
|
|
* Epic fail. Need exit_thread() here
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
render->bitmap[0].width = width;
|
|
|
|
render->bitmap[0].height = height;
|
|
|
|
|
|
|
|
if( create_bitmap(&render->bitmap[0]) != 0 )
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
render->state = INIT;
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
if(render->caps & HW_TEX_BLIT) /* hw scaler */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
render->bitmap[0].width = width;
|
|
|
|
render->bitmap[0].height = height;
|
|
|
|
resize_bitmap(&render->bitmap[0]);
|
|
|
|
|
|
|
|
return 0;
|
2010-11-10 06:51:05 +01:00
|
|
|
};
|
|
|
|
|
2012-02-13 23:34:36 +01:00
|
|
|
void render_adjust_size(render_t *render)
|
|
|
|
{
|
|
|
|
char proc_info[1024];
|
|
|
|
|
|
|
|
uint32_t right, bottom, new_w, new_h;
|
|
|
|
uint32_t s, sw, sh;
|
|
|
|
uint8_t state;
|
|
|
|
|
|
|
|
get_proc_info(proc_info);
|
|
|
|
|
|
|
|
right = *(uint32_t*)(proc_info+62)+1;
|
|
|
|
bottom = *(uint32_t*)(proc_info+66)+1;
|
|
|
|
state = *(uint8_t*)(proc_info+70);
|
|
|
|
|
|
|
|
if(state & 2)
|
|
|
|
{ render->win_state = MINIMIZED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(state & 4)
|
|
|
|
{
|
|
|
|
render->win_state = ROLLED;
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
render->win_state = NORMAL;
|
|
|
|
|
|
|
|
if( right == render->win_width &&
|
|
|
|
bottom == render->win_height)
|
|
|
|
return;
|
|
|
|
|
|
|
|
new_w = bottom*render->ctx_width/render->ctx_height;
|
|
|
|
new_h = right*render->ctx_height/render->ctx_width;
|
|
|
|
|
|
|
|
// printf("right %d bottom %d\n", right, bottom);
|
|
|
|
// printf("new_w %d new_h %d\n", new_w, new_h);
|
|
|
|
|
|
|
|
s = right * bottom;
|
|
|
|
sw = right * new_h;
|
|
|
|
sh = bottom * new_w;
|
|
|
|
|
|
|
|
if( abs(s-sw) > abs(s-sh))
|
|
|
|
new_h = bottom;
|
|
|
|
else
|
|
|
|
new_w = right;
|
|
|
|
|
|
|
|
if(new_w < 64)
|
|
|
|
{
|
|
|
|
new_w = 64;
|
|
|
|
new_h = 64*render->ctx_height/render->ctx_width;
|
|
|
|
};
|
|
|
|
__asm__ __volatile__(
|
|
|
|
"int $0x40"
|
|
|
|
::"a"(67), "b"(-1), "c"(-1),
|
|
|
|
"d"(new_w+9),"S"(new_h+26)
|
|
|
|
:"memory" );
|
|
|
|
render_set_size(render, new_w, new_h);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
void draw_hw_picture(render_t *render, AVPicture *picture)
|
|
|
|
{
|
|
|
|
int dst_width, dst_height;
|
|
|
|
uint8_t *data[4];
|
|
|
|
int linesize[4];
|
|
|
|
|
|
|
|
if(render->win_state != NORMAL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(render->caps & HW_TEX_BLIT)
|
|
|
|
{
|
|
|
|
dst_width = render->ctx_width;
|
|
|
|
dst_height = render->ctx_height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dst_width = render->win_width;
|
|
|
|
dst_height = render->win_height;
|
|
|
|
};
|
|
|
|
|
|
|
|
cvt_ctx = sws_getCachedContext(cvt_ctx,
|
|
|
|
render->ctx_width, render->ctx_height, render->ctx_format,
|
|
|
|
dst_width, dst_height, PIX_FMT_BGRA,
|
|
|
|
SWS_FAST_BILINEAR, NULL, NULL, NULL);
|
|
|
|
if(cvt_ctx == NULL)
|
|
|
|
{
|
|
|
|
printf("Cannot initialize the conversion context!\n");
|
|
|
|
return ;
|
|
|
|
};
|
|
|
|
// printf("sws_getCachedContext\n");
|
|
|
|
data[0] = render->bitmap[render->target].data;
|
|
|
|
data[1] = render->bitmap[render->target].data+1;
|
|
|
|
data[2] = render->bitmap[render->target].data+2;
|
|
|
|
data[3] = render->bitmap[render->target].data+3;
|
|
|
|
|
|
|
|
linesize[0] = render->bitmap[render->target].pitch;
|
|
|
|
linesize[1] = render->bitmap[render->target].pitch;
|
|
|
|
linesize[2] = render->bitmap[render->target].pitch;
|
|
|
|
linesize[3] = render->bitmap[render->target].pitch;
|
|
|
|
|
|
|
|
sws_scale(cvt_ctx, (const uint8_t* const *)picture->data,
|
|
|
|
picture->linesize, 0, render->ctx_height, data, linesize);
|
|
|
|
// printf("sws_scale\n");
|
|
|
|
|
|
|
|
blit_bitmap(&render->bitmap[render->target], 5, 22,
|
|
|
|
render->win_width, render->win_height);
|
|
|
|
// printf("blit_bitmap\n");
|
|
|
|
|
|
|
|
delay(2);
|
|
|
|
render->target++;
|
|
|
|
render->target&= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw_sw_picture(render_t *render, AVPicture *picture)
|
|
|
|
{
|
|
|
|
uint8_t *data[4];
|
|
|
|
int linesize[4];
|
|
|
|
|
|
|
|
if(render->win_state != NORMAL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cvt_ctx = sws_getCachedContext(cvt_ctx,
|
|
|
|
render->ctx_width, render->ctx_height,
|
|
|
|
render->ctx_format,
|
|
|
|
render->win_width, render->win_height,
|
|
|
|
PIX_FMT_BGRA, SWS_FAST_BILINEAR, NULL, NULL, NULL);
|
|
|
|
if(cvt_ctx == NULL)
|
|
|
|
{
|
|
|
|
printf("Cannot initialize the conversion context!\n");
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
data[0] = render->bitmap[0].data;
|
|
|
|
data[1] = render->bitmap[0].data+1;
|
|
|
|
data[2] = render->bitmap[0].data+2;
|
|
|
|
data[3] = render->bitmap[0].data+3;
|
|
|
|
|
|
|
|
|
|
|
|
linesize[0] = render->bitmap[0].pitch;
|
|
|
|
linesize[1] = render->bitmap[0].pitch;
|
|
|
|
linesize[2] = render->bitmap[0].pitch;
|
|
|
|
linesize[3] = render->bitmap[0].pitch;
|
|
|
|
|
|
|
|
sws_scale(cvt_ctx, (const uint8_t* const *)picture->data,
|
|
|
|
picture->linesize, 0, render->ctx_height, data, linesize);
|
|
|
|
|
|
|
|
blit_bitmap(&render->bitmap[0], 5, 22,
|
|
|
|
render->win_width, render->win_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|