#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include "../config.h"
#include "../mp_msg.h"

#include "img_format.h"
#include "mp_image.h"
#include "vf.h"

#include "../libvo/fastmemcpy.h"
#include "../postproc/swscale.h"

struct vf_priv_s {
    int w,h;
    int v_chr_drop;
    int param;
    unsigned int fmt;
    SwsContext *ctx;
    unsigned char* palette;
};

static SwsFilter *orig_srcFilter, *orig_dstFilter;
static int orig_int_sws_flags=0;
static int orig_outfmt = 0, orig_best = 0;

static SwsContext *S_0 = NULL;
static SwsContext *S_1 = NULL;
static SwsContext *S_2 = NULL;
static SwsContext *S_3 = NULL;
static SwsContext *S_4 = NULL;
static SwsContext *S_5 = NULL;
static SwsContext *S_6 = NULL;
static SwsContext *S_7 = NULL;
static SwsContext *S_9 = NULL;
static SwsContext *S_13 = NULL;


extern int opt_screen_size_x;
extern int opt_screen_size_y;

//===========================================================================//

static unsigned int outfmt_list[]={
// RGB:
    IMGFMT_BGR32,
    IMGFMT_RGB32,
    IMGFMT_BGR24,
    IMGFMT_RGB24,
    IMGFMT_BGR16,
    IMGFMT_RGB16,
    IMGFMT_BGR15,
    IMGFMT_RGB15,
    IMGFMT_BGR8,
    IMGFMT_RGB8,
    IMGFMT_BGR4,
    IMGFMT_RGB4,
    IMGFMT_BG4B,
    IMGFMT_RG4B,
    IMGFMT_BGR1,
    IMGFMT_RGB1,
// YUV:
    IMGFMT_444P,
    IMGFMT_422P,
    IMGFMT_YV12,
    IMGFMT_I420,
    IMGFMT_IYUV,
    IMGFMT_YVU9,
    IMGFMT_IF09,
    IMGFMT_411P,
    IMGFMT_Y800,
    IMGFMT_Y8,
    IMGFMT_YUY2,
    0
};

static unsigned int find_best_out(vf_instance_t *vf){
    unsigned int best=0;
    unsigned int* p=outfmt_list;
    // find the best outfmt:
    while(*p){
	int ret=vf_next_query_format(vf,*p);
	if(ret&VFCAP_CSP_SUPPORTED_BY_HW){ best=*p; break;} // no conversion -> bingo!
	if(ret&VFCAP_CSP_SUPPORTED && !best) best=*p; // best with conversion
	++p;
    }
    return best;
}

static int config(struct vf_instance_s* vf,
        int width, int height, int d_width, int d_height,
	unsigned int flags, unsigned int outfmt){
    unsigned int best=find_best_out(vf);
	int k;
    int vo_flags = 0;
    int int_sws_flags=0;
    SwsFilter *srcFilter, *dstFilter;
    
    if(!best){
	mp_msg(MSGT_VFILTER,MSGL_WARN,"SwScale: no supported outfmt found :(\n");
	return 0;
    }

	k = avamax_var->my_displaymode & 3;
	switch (k) {
		case 0: { // 4:3 ==> 4:3
			vf->priv->w = 640; 
    		vf->priv->h = 480;
		} break;
		case 1: { // 16:9 ==> 4:3
			vf->priv->w = 640; 
    		vf->priv->h = 360;
		} break;
		case 2: { // 4:3 ==> 16:9
			vf->priv->w = 480; 
    		vf->priv->h = 480;
		} break;
		case 3: { // 16:9 ==> 16:9
			vf->priv->w = 720; 
    		vf->priv->h = 480;
		} break;
	}

    
    vo_flags=vf->next->query_format(vf->next,best);
    
    // scaling to dwidth*d_height, if all these TRUE:
    // - option -zoom
    // - no other sw/hw up/down scaling avail.
    // - we're after postproc
    // - user didn't set w:h
    if(!(vo_flags&VFCAP_POSTPROC) && (flags&4) && 
	    vf->priv->w<0 && vf->priv->h<0){	// -zoom
	int x=(vo_flags&VFCAP_SWSCALE) ? 0 : 1;
	if(d_width<width || d_height<height){
	    // downscale!
	    if(vo_flags&VFCAP_HWSCALE_DOWN) x=0;
	} else {
	    // upscale:
	    if(vo_flags&VFCAP_HWSCALE_UP) x=0;
	}
	if(x){
	    // user wants sw scaling! (-zoom)
	    vf->priv->w=d_width;
	    vf->priv->h=d_height;
	}
    }

    // calculate the missing parameters:
    switch(best) {
    case IMGFMT_YUY2:		/* YUY2 needs w rounded to 2 */
	if(vf->priv->w==-3) vf->priv->w=(vf->priv->h*width/height+1)&~1; else
	if(vf->priv->w==-2) vf->priv->w=(vf->priv->h*d_width/d_height+1)&~1;
	if(vf->priv->w<0) vf->priv->w=width; else
	if(vf->priv->w==0) vf->priv->w=d_width;
	if(vf->priv->h==-3) vf->priv->h=vf->priv->w*height/width; else
	if(vf->priv->h==-2) vf->priv->h=vf->priv->w*d_height/d_width;
	break;
    case IMGFMT_YV12:		/* YV12 needs w & h rounded to 2 */
	if(vf->priv->w==-3) vf->priv->w=(vf->priv->h*width/height+1)&~1; else
	if(vf->priv->w==-2) vf->priv->w=(vf->priv->h*d_width/d_height+1)&~1;
	if(vf->priv->w<0) vf->priv->w=width; else
	if(vf->priv->w==0) vf->priv->w=d_width;
	if(vf->priv->h==-3) vf->priv->h=(vf->priv->w*height/width+1)&~1; else
	if(vf->priv->h==-2) vf->priv->h=(vf->priv->w*d_height/d_width+1)&~1;
	break;
    default:
    if(vf->priv->w==-3) vf->priv->w=vf->priv->h*width/height; else
    if(vf->priv->w==-2) vf->priv->w=vf->priv->h*d_width/d_height;
    if(vf->priv->w<0) vf->priv->w=width; else
    if(vf->priv->w==0) vf->priv->w=d_width;
    if(vf->priv->h==-3) vf->priv->h=vf->priv->w*height/width; else
    if(vf->priv->h==-2) vf->priv->h=vf->priv->w*d_height/d_width;
    break;
    }
    
    if(vf->priv->h<0) vf->priv->h=height; else
    if(vf->priv->h==0) vf->priv->h=vf->priv->w*height/width;

    mp_msg(MSGT_VFILTER,MSGL_DBG2,"SwScale: scaling %dx%d %s to %dx%d %s  \n",
	width,height,vo_format_name(outfmt),
	vf->priv->w,vf->priv->h,vo_format_name(best));

    // free old ctx:
    if(vf->priv->ctx) freeSwsContext(vf->priv->ctx);
    
    // new swscaler:
    swsGetFlagsAndFilterFromCmdLine(&int_sws_flags, &srcFilter, &dstFilter);
    int_sws_flags|= vf->priv->v_chr_drop << SWS_SRC_V_CHR_DROP_SHIFT;
    int_sws_flags|= vf->priv->param      << SWS_PARAM_SHIFT;
    vf->priv->ctx=getSwsContext(width,height,
	    (outfmt==IMGFMT_I420 || outfmt==IMGFMT_IYUV)?IMGFMT_YV12:outfmt,
		  vf->priv->w,vf->priv->h,
	    (best==IMGFMT_I420 || best==IMGFMT_IYUV)?IMGFMT_YV12:best,
	    int_sws_flags, srcFilter, dstFilter);
   
    orig_int_sws_flags = int_sws_flags;
    orig_outfmt = (outfmt==IMGFMT_I420 || outfmt==IMGFMT_IYUV)?IMGFMT_YV12:outfmt;
	orig_best = (best==IMGFMT_I420 || best==IMGFMT_IYUV)?IMGFMT_YV12:best;
        
    S_0 = getSwsContext(width,height,
			orig_outfmt, 640,480, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter); 			
 	S_1 = getSwsContext(width,height,
			orig_outfmt, 640,360, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter);
 	S_2 = getSwsContext(width,height,
			orig_outfmt, avamax_var->S2X ,480, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter); 			
	S_3 = getSwsContext(width,height,
			orig_outfmt, avamax_var->S3X ,480, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter);
 	S_4 = getSwsContext(width,height,
			orig_outfmt, 800,600, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter);
 	S_5 = getSwsContext(width,height,
			orig_outfmt, 800,450, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter);
 	S_6 = getSwsContext(width,height,
			orig_outfmt, avamax_var->S6X,600, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter);
 	S_7 = getSwsContext(width,height,
			orig_outfmt, 800,avamax_var->S7Y, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter);
	S_9 = getSwsContext(540,height,
			orig_outfmt, 640,480, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter);
	S_13 = getSwsContext(540,height,
			orig_outfmt, 800,600, orig_best,
 			orig_int_sws_flags, orig_srcFilter, orig_dstFilter);
    
	vf->priv->fmt=best;

    if(vf->priv->palette){
	free(vf->priv->palette);
	vf->priv->palette=NULL;
    }
    switch(best){
    case IMGFMT_BGR8: {
      /* set 332 palette for 8 bpp */
	int i;
	vf->priv->palette=malloc(4*256);
	for(i=0; i<256; i++){
	    vf->priv->palette[4*i+0]=4*(i&3)*21;
	    vf->priv->palette[4*i+1]=4*((i>>2)&7)*9;
	    vf->priv->palette[4*i+2]=4*((i>>5)&7)*9;
	}
	break; }
    case IMGFMT_BGR4: 
    case IMGFMT_BG4B: {
	int i;
	vf->priv->palette=malloc(4*16);
	for(i=0; i<16; i++){
	    vf->priv->palette[4*i+0]=4*(i&1)*63;
	    vf->priv->palette[4*i+1]=4*((i>>1)&3)*21;
	    vf->priv->palette[4*i+2]=4*((i>>3)&1)*63;
	}
	break; }
    }

    if(!opt_screen_size_x && !opt_screen_size_y){
	d_width=d_width*vf->priv->w/width;
	d_height=d_height*vf->priv->h/height;
    }
    return vf_next_config(vf,vf->priv->w,vf->priv->h,d_width,d_height,flags,best);
}

void change_vfvo_buffer(struct vf_instance_s* vf)
{
	while (vf) {
		if (vf->imgctx.temp_images[0]) {
			free_mp_image(vf->imgctx.temp_images[0]);
		}
		vf->imgctx.temp_images[0] = NULL;
		vf= vf->next;
	}
}

#define BUGBUG1	printf("Scale BUG!!! %d\n",old_displaymode); exit(10);
static void MyChangeFilter(struct vf_instance_s* vf)
{
	static int old_displaymode = -1;
	
	if (old_displaymode == avamax_var->my_displaymode) 
		return ;

	old_displaymode = avamax_var->my_displaymode;
	printf("vf_scale , mode=%d\n",old_displaymode);

	switch (old_displaymode) {
		case 0:	{ // 4:3 ==> 4:3 , normal, no aspect
			vf->priv->ctx = S_0;
            vf->priv->w = 640;
            vf->priv->h = 480;
		} break;
		case 1:	{ // 16:9 ==> 4:3 , normal, no aspect, black bar
			vf->priv->ctx = S_1;
            vf->priv->w = 640;
            vf->priv->h = 360;
		} break;
		case 2:	{ // 4:3 ==> 16:9, normal, no aspect
			vf->priv->ctx = S_2;
            vf->priv->w = avamax_var->S2X;
            vf->priv->h = 480;
		} break;
		case 3:{ // 16:9 ==> 16:9, normal, no aspect
			vf->priv->ctx = S_3;
            vf->priv->w = avamax_var->S3X;
            vf->priv->h = 480;
		} break;
		case 4:	{ // 4:3 ==> 4:3 , full, no aspect
			vf->priv->ctx = S_4;
            vf->priv->w = 800;
            vf->priv->h = 600;
		} break;
		case 5:{ // 16:9 ==> 4:3 , full, no aspect, black bar
			vf->priv->ctx = S_5;
            vf->priv->w = 800;
            vf->priv->h = 450;
		} break;
		case 6:	{ // 4:3 ==> 16:9, full, no aspect, Black Bar
			vf->priv->ctx = S_6;
            vf->priv->w = avamax_var->S6X;
            vf->priv->h = 600;
		} break;
		case 7: { // 16:9 ==> 16:9, full, no aspect
			vf->priv->ctx = S_7;
            vf->priv->w = 800;
            vf->priv->h = avamax_var->S7Y;
		} break;
		case 8:	{ // 4:3 ==> 4:3 , full, aspect
			BUGBUG1;
		} break;
		case 9:	{ // 16:9 ==> 4:3 , normal, aspect, Cut
			vf->priv->ctx = S_9;
            vf->priv->w = 640;
            vf->priv->h = 480;
		} break;
		case 10:	{ // 4:3 ==> 16:9, normal, aspect
			BUGBUG1;
		} break;
		case 11:{ // 16:9 ==> 16:9, normal, aspect
			BUGBUG1
		} break;
		case 12:	{ // 4:3 ==> 4:3 , full, aspect
			BUGBUG1;
		} break;
		case 13:{ // 16:9 ==> 4:3 , full, aspect, Cut
			vf->priv->ctx = S_13;
            vf->priv->w = 800;
            vf->priv->h = 600;
		} break;
		case 14:	{ // 4:3 ==> 16:9, normal, aspect
			BUGBUG1;
		} break;
		case 15:{ // 16:9 ==> 16:9, full, aspect
			BUGBUG1
		} break;
	}
    change_vfvo_buffer(vf->next);
}


static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
    mp_image_t *dmpi;

    MyChangeFilter(vf);
    
    // hope we'll get DR buffer:
    dmpi=vf_get_image(vf->next,vf->priv->fmt,
	MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
	vf->priv->w, vf->priv->h);
    vf->priv->ctx->swScale(vf->priv->ctx,mpi->planes,mpi->stride,0,mpi->h,dmpi->planes,dmpi->stride);

    if(vf->priv->w==mpi->w && vf->priv->h==mpi->h){
	// just conversion, no scaling -> keep postprocessing data
	// this way we can apply pp filter to non-yv12 source using scaler
	dmpi->qscale=mpi->qscale;
	dmpi->qstride=mpi->qstride;
    }

    if(vf->priv->palette) dmpi->planes[1]=vf->priv->palette; // export palette!
    
    return vf_next_put_image(vf,dmpi);
}

//===========================================================================//

//  supported Input formats: YV12, I420, IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8, Y800

static int query_format(struct vf_instance_s* vf, unsigned int fmt){
    switch(fmt){
    case IMGFMT_YV12:
    case IMGFMT_I420:
    case IMGFMT_IYUV:
    case IMGFMT_UYVY:
    case IMGFMT_YUY2:
    case IMGFMT_BGR32:
    case IMGFMT_BGR24:
    case IMGFMT_BGR16:
    case IMGFMT_BGR15:
    case IMGFMT_RGB32:
    case IMGFMT_RGB24:
    case IMGFMT_Y800: 
    case IMGFMT_Y8: 
    case IMGFMT_YVU9: 
    case IMGFMT_IF09: 
    case IMGFMT_444P: 
    case IMGFMT_422P: 
    case IMGFMT_411P: 
    {
	unsigned int best=find_best_out(vf);
	int flags;
	if(!best) return 0;	 // no matching out-fmt
	flags=vf_next_query_format(vf,best);
	if(!(flags&3)) return 0; // huh?
	if(fmt!=best) flags&=~VFCAP_CSP_SUPPORTED_BY_HW;
	// do not allow scaling, if we are before the PP fliter!
	if(!(flags&VFCAP_POSTPROC)) flags|=VFCAP_SWSCALE;
	return flags;
      }
    }
    return 0;	// nomatching in-fmt
}

static int open(vf_instance_t *vf, char* args){
    vf->config=config;
    vf->put_image=put_image;
    vf->query_format=query_format;
    vf->priv=malloc(sizeof(struct vf_priv_s));
    // TODO: parse args ->
    vf->priv->ctx=NULL;
    vf->priv->w=
    vf->priv->h=-1;
    vf->priv->v_chr_drop=0;
    vf->priv->param=0;
    vf->priv->palette=NULL;
    if(args) sscanf(args, "%d:%d:%d:%d",
    &vf->priv->w,
    &vf->priv->h,
    &vf->priv->v_chr_drop,
    &vf->priv->param);
    mp_msg(MSGT_VFILTER,MSGL_V,"SwScale params: %d x %d (-1=no scaling)\n",
    vf->priv->w,
    vf->priv->h);
    return 1;
}

vf_info_t vf_info_scale = {
    "software scaling",
    "scale",
    "A'rpi",
    "",
    open
};

//===========================================================================//
