00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <inttypes.h>
00025 #include <math.h>
00026 #include <assert.h>
00027
00028 #include "config.h"
00029
00030 #include "mp_msg.h"
00031 #include "cpudetect.h"
00032
00033 #include "libavcodec/avcodec.h"
00034
00035 #include "img_format.h"
00036 #include "mp_image.h"
00037 #include "vf.h"
00038 #include "vd_ffmpeg.h"
00039 #include "libvo/fastmemcpy.h"
00040
00041 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
00042
00043 #define BLOCK 16
00044
00045
00046 static const uint8_t __attribute__((aligned(8))) dither[8][8]={
00047 { 0*4, 48*4, 12*4, 60*4, 3*4, 51*4, 15*4, 63*4, },
00048 { 32*4, 16*4, 44*4, 28*4, 35*4, 19*4, 47*4, 31*4, },
00049 { 8*4, 56*4, 4*4, 52*4, 11*4, 59*4, 7*4, 55*4, },
00050 { 40*4, 24*4, 36*4, 20*4, 43*4, 27*4, 39*4, 23*4, },
00051 { 2*4, 50*4, 14*4, 62*4, 1*4, 49*4, 13*4, 61*4, },
00052 { 34*4, 18*4, 46*4, 30*4, 33*4, 17*4, 45*4, 29*4, },
00053 { 10*4, 58*4, 6*4, 54*4, 9*4, 57*4, 5*4, 53*4, },
00054 { 42*4, 26*4, 38*4, 22*4, 41*4, 25*4, 37*4, 21*4, },
00055 };
00056
00057 static const uint8_t offset[511][2]= {
00058 { 0, 0},
00059 { 0, 0}, { 8, 8},
00060 { 0, 0}, { 4, 4}, {12, 8}, { 8,12},
00061 { 0, 0}, {10, 2}, { 4, 4}, {14, 6}, { 8, 8}, { 2,10}, {12,12}, { 6,14},
00062
00063 { 0, 0}, {10, 2}, { 4, 4}, {14, 6}, { 8, 8}, { 2,10}, {12,12}, { 6,14},
00064 { 5, 1}, {15, 3}, { 9, 5}, { 3, 7}, {13, 9}, { 7,11}, { 1,13}, {11,15},
00065
00066 { 0, 0}, { 8, 0}, { 0, 8}, { 8, 8}, { 5, 1}, {13, 1}, { 5, 9}, {13, 9},
00067 { 2, 2}, {10, 2}, { 2,10}, {10,10}, { 7, 3}, {15, 3}, { 7,11}, {15,11},
00068 { 4, 4}, {12, 4}, { 4,12}, {12,12}, { 1, 5}, { 9, 5}, { 1,13}, { 9,13},
00069 { 6, 6}, {14, 6}, { 6,14}, {14,14}, { 3, 7}, {11, 7}, { 3,15}, {11,15},
00070
00071 { 0, 0}, { 8, 0}, { 0, 8}, { 8, 8}, { 4, 0}, {12, 0}, { 4, 8}, {12, 8},
00072 { 1, 1}, { 9, 1}, { 1, 9}, { 9, 9}, { 5, 1}, {13, 1}, { 5, 9}, {13, 9},
00073 { 3, 2}, {11, 2}, { 3,10}, {11,10}, { 7, 2}, {15, 2}, { 7,10}, {15,10},
00074 { 2, 3}, {10, 3}, { 2,11}, {10,11}, { 6, 3}, {14, 3}, { 6,11}, {14,11},
00075 { 0, 4}, { 8, 4}, { 0,12}, { 8,12}, { 4, 4}, {12, 4}, { 4,12}, {12,12},
00076 { 1, 5}, { 9, 5}, { 1,13}, { 9,13}, { 5, 5}, {13, 5}, { 5,13}, {13,13},
00077 { 3, 6}, {11, 6}, { 3,14}, {11,14}, { 7, 6}, {15, 6}, { 7,14}, {15,14},
00078 { 2, 7}, {10, 7}, { 2,15}, {10,15}, { 6, 7}, {14, 7}, { 6,15}, {14,15},
00079
00080 { 0, 0}, { 8, 0}, { 0, 8}, { 8, 8}, { 0, 2}, { 8, 2}, { 0,10}, { 8,10},
00081 { 0, 4}, { 8, 4}, { 0,12}, { 8,12}, { 0, 6}, { 8, 6}, { 0,14}, { 8,14},
00082 { 1, 1}, { 9, 1}, { 1, 9}, { 9, 9}, { 1, 3}, { 9, 3}, { 1,11}, { 9,11},
00083 { 1, 5}, { 9, 5}, { 1,13}, { 9,13}, { 1, 7}, { 9, 7}, { 1,15}, { 9,15},
00084 { 2, 0}, {10, 0}, { 2, 8}, {10, 8}, { 2, 2}, {10, 2}, { 2,10}, {10,10},
00085 { 2, 4}, {10, 4}, { 2,12}, {10,12}, { 2, 6}, {10, 6}, { 2,14}, {10,14},
00086 { 3, 1}, {11, 1}, { 3, 9}, {11, 9}, { 3, 3}, {11, 3}, { 3,11}, {11,11},
00087 { 3, 5}, {11, 5}, { 3,13}, {11,13}, { 3, 7}, {11, 7}, { 3,15}, {11,15},
00088 { 4, 0}, {12, 0}, { 4, 8}, {12, 8}, { 4, 2}, {12, 2}, { 4,10}, {12,10},
00089 { 4, 4}, {12, 4}, { 4,12}, {12,12}, { 4, 6}, {12, 6}, { 4,14}, {12,14},
00090 { 5, 1}, {13, 1}, { 5, 9}, {13, 9}, { 5, 3}, {13, 3}, { 5,11}, {13,11},
00091 { 5, 5}, {13, 5}, { 5,13}, {13,13}, { 5, 7}, {13, 7}, { 5,15}, {13,15},
00092 { 6, 0}, {14, 0}, { 6, 8}, {14, 8}, { 6, 2}, {14, 2}, { 6,10}, {14,10},
00093 { 6, 4}, {14, 4}, { 6,12}, {14,12}, { 6, 6}, {14, 6}, { 6,14}, {14,14},
00094 { 7, 1}, {15, 1}, { 7, 9}, {15, 9}, { 7, 3}, {15, 3}, { 7,11}, {15,11},
00095 { 7, 5}, {15, 5}, { 7,13}, {15,13}, { 7, 7}, {15, 7}, { 7,15}, {15,15},
00096
00097 { 0, 0}, { 8, 0}, { 0, 8}, { 8, 8}, { 4, 4}, {12, 4}, { 4,12}, {12,12}, { 0, 4}, { 8, 4}, { 0,12}, { 8,12}, { 4, 0}, {12, 0}, { 4, 8}, {12, 8}, { 2, 2}, {10, 2}, { 2,10}, {10,10}, { 6, 6}, {14, 6}, { 6,14}, {14,14}, { 2, 6}, {10, 6}, { 2,14}, {10,14}, { 6, 2}, {14, 2}, { 6,10}, {14,10}, { 0, 2}, { 8, 2}, { 0,10}, { 8,10}, { 4, 6}, {12, 6}, { 4,14}, {12,14}, { 0, 6}, { 8, 6}, { 0,14}, { 8,14}, { 4, 2}, {12, 2}, { 4,10}, {12,10}, { 2, 0}, {10, 0}, { 2, 8}, {10, 8}, { 6, 4}, {14, 4}, { 6,12}, {14,12}, { 2, 4}, {10, 4}, { 2,12}, {10,12}, { 6, 0}, {14, 0}, { 6, 8}, {14, 8}, { 1, 1}, { 9, 1}, { 1, 9}, { 9, 9}, { 5, 5}, {13, 5}, { 5,13}, {13,13}, { 1, 5}, { 9, 5}, { 1,13}, { 9,13}, { 5, 1}, {13, 1}, { 5, 9}, {13, 9}, { 3, 3}, {11, 3}, { 3,11}, {11,11}, { 7, 7}, {15, 7}, { 7,15}, {15,15}, { 3, 7}, {11, 7}, { 3,15}, {11,15}, { 7, 3}, {15, 3}, { 7,11}, {15,11}, { 1, 3}, { 9, 3}, { 1,11}, { 9,11}, { 5, 7}, {13, 7}, { 5,15}, {13,15}, { 1, 7}, { 9, 7}, { 1,15}, { 9,15}, { 5, 3}, {13, 3}, { 5,11}, {13,11}, { 3, 1}, {11, 1}, { 3, 9}, {11, 9}, { 7, 5}, {15, 5}, { 7,13}, {15,13}, { 3, 5}, {11, 5}, { 3,13}, {11,13}, { 7, 1}, {15, 1}, { 7, 9}, {15, 9}, { 0, 1}, { 8, 1}, { 0, 9}, { 8, 9}, { 4, 5}, {12, 5}, { 4,13}, {12,13}, { 0, 5}, { 8, 5}, { 0,13}, { 8,13}, { 4, 1}, {12, 1}, { 4, 9}, {12, 9}, { 2, 3}, {10, 3}, { 2,11}, {10,11}, { 6, 7}, {14, 7}, { 6,15}, {14,15}, { 2, 7}, {10, 7}, { 2,15}, {10,15}, { 6, 3}, {14, 3}, { 6,11}, {14,11}, { 0, 3}, { 8, 3}, { 0,11}, { 8,11}, { 4, 7}, {12, 7}, { 4,15}, {12,15}, { 0, 7}, { 8, 7}, { 0,15}, { 8,15}, { 4, 3}, {12, 3}, { 4,11}, {12,11}, { 2, 1}, {10, 1}, { 2, 9}, {10, 9}, { 6, 5}, {14, 5}, { 6,13}, {14,13}, { 2, 5}, {10, 5}, { 2,13}, {10,13}, { 6, 1}, {14, 1}, { 6, 9}, {14, 9}, { 1, 0}, { 9, 0}, { 1, 8}, { 9, 8}, { 5, 4}, {13, 4}, { 5,12}, {13,12}, { 1, 4}, { 9, 4}, { 1,12}, { 9,12}, { 5, 0}, {13, 0}, { 5, 8}, {13, 8}, { 3, 2}, {11, 2}, { 3,10}, {11,10}, { 7, 6}, {15, 6}, { 7,14}, {15,14}, { 3, 6}, {11, 6}, { 3,14}, {11,14}, { 7, 2}, {15, 2}, { 7,10}, {15,10}, { 1, 2}, { 9, 2}, { 1,10}, { 9,10}, { 5, 6}, {13, 6}, { 5,14}, {13,14}, { 1, 6}, { 9, 6}, { 1,14}, { 9,14}, { 5, 2}, {13, 2}, { 5,10}, {13,10}, { 3, 0}, {11, 0}, { 3, 8}, {11, 8}, { 7, 4}, {15, 4}, { 7,12}, {15,12}, { 3, 4}, {11, 4}, { 3,12}, {11,12}, { 7, 0}, {15, 0}, { 7, 8}, {15, 8},
00098 };
00099
00100 struct vf_priv_s {
00101 int log2_count;
00102 int qp;
00103 int mode;
00104 int mpeg2;
00105 int temp_stride[3];
00106 uint8_t *src[3];
00107 int16_t *temp[3];
00108 int outbuf_size;
00109 uint8_t *outbuf;
00110 AVCodecContext *avctx_enc[BLOCK*BLOCK];
00111 AVFrame *frame;
00112 AVFrame *frame_dec;
00113 };
00114
00115 static void store_slice_c(uint8_t *dst, int16_t *src, int dst_stride, int src_stride, int width, int height, int log2_scale){
00116 int y, x;
00117
00118 #define STORE(pos) \
00119 temp= ((src[x + y*src_stride + pos]<<log2_scale) + d[pos])>>8;\
00120 if(temp & 0x100) temp= ~(temp>>31);\
00121 dst[x + y*dst_stride + pos]= temp;
00122
00123 for(y=0; y<height; y++){
00124 const uint8_t *d= dither[y&7];
00125 for(x=0; x<width; x+=8){
00126 int temp;
00127 STORE(0);
00128 STORE(1);
00129 STORE(2);
00130 STORE(3);
00131 STORE(4);
00132 STORE(5);
00133 STORE(6);
00134 STORE(7);
00135 }
00136 }
00137 }
00138
00139 static void filter(struct vf_priv_s *p, uint8_t *dst[3], uint8_t *src[3], int dst_stride[3], int src_stride[3], int width, int height, uint8_t *qp_store, int qp_stride){
00140 int x, y, i, j;
00141 const int count= 1<<p->log2_count;
00142
00143 for(i=0; i<3; i++){
00144 int is_chroma= !!i;
00145 int w= width >>is_chroma;
00146 int h= height>>is_chroma;
00147 int stride= p->temp_stride[i];
00148 int block= BLOCK>>is_chroma;
00149
00150 if (!src[i] || !dst[i])
00151 continue;
00152 for(y=0; y<h; y++){
00153 int index= block + block*stride + y*stride;
00154 fast_memcpy(p->src[i] + index, src[i] + y*src_stride[i], w);
00155 for(x=0; x<block; x++){
00156 p->src[i][index - x - 1]= p->src[i][index + x ];
00157 p->src[i][index + w + x ]= p->src[i][index + w - x - 1];
00158 }
00159 }
00160 for(y=0; y<block; y++){
00161 fast_memcpy(p->src[i] + ( block-1-y)*stride, p->src[i] + ( y+block )*stride, stride);
00162 fast_memcpy(p->src[i] + (h+block +y)*stride, p->src[i] + (h-y+block-1)*stride, stride);
00163 }
00164
00165 p->frame->linesize[i]= stride;
00166 memset(p->temp[i], 0, (h+2*block)*stride*sizeof(int16_t));
00167 }
00168
00169 if(p->qp)
00170 p->frame->quality= p->qp * FF_QP2LAMBDA;
00171 else
00172 p->frame->quality= norm_qscale(qp_store[0], p->mpeg2) * FF_QP2LAMBDA;
00173
00174
00175 for(i=0; i<count; i++){
00176 const int x1= offset[i+count-1][0];
00177 const int y1= offset[i+count-1][1];
00178 int offset;
00179 p->frame->data[0]= p->src[0] + x1 + y1 * p->frame->linesize[0];
00180 p->frame->data[1]= p->src[1] + x1/2 + y1/2 * p->frame->linesize[1];
00181 p->frame->data[2]= p->src[2] + x1/2 + y1/2 * p->frame->linesize[2];
00182
00183 avcodec_encode_video(p->avctx_enc[i], p->outbuf, p->outbuf_size, p->frame);
00184 p->frame_dec = p->avctx_enc[i]->coded_frame;
00185
00186 offset= (BLOCK-x1) + (BLOCK-y1)*p->frame_dec->linesize[0];
00187
00188 for(y=0; y<height; y++){
00189 for(x=0; x<width; x++){
00190 p->temp[0][ x + y*p->temp_stride[0] ] += p->frame_dec->data[0][ x + y*p->frame_dec->linesize[0] + offset ];
00191 }
00192 }
00193 offset= (BLOCK/2-x1/2) + (BLOCK/2-y1/2)*p->frame_dec->linesize[1];
00194 for(y=0; y<height/2; y++){
00195 for(x=0; x<width/2; x++){
00196 p->temp[1][ x + y*p->temp_stride[1] ] += p->frame_dec->data[1][ x + y*p->frame_dec->linesize[1] + offset ];
00197 p->temp[2][ x + y*p->temp_stride[2] ] += p->frame_dec->data[2][ x + y*p->frame_dec->linesize[2] + offset ];
00198 }
00199 }
00200 }
00201
00202 for(j=0; j<3; j++){
00203 int is_chroma= !!j;
00204 store_slice_c(dst[j], p->temp[j], dst_stride[j], p->temp_stride[j], width>>is_chroma, height>>is_chroma, 8-p->log2_count);
00205 }
00206 }
00207
00208 static int config(struct vf_instance *vf,
00209 int width, int height, int d_width, int d_height,
00210 unsigned int flags, unsigned int outfmt){
00211 int i;
00212 AVCodec *enc= avcodec_find_encoder(CODEC_ID_SNOW);
00213
00214 for(i=0; i<3; i++){
00215 int is_chroma= !!i;
00216 int w= ((width + 4*BLOCK-1) & (~(2*BLOCK-1)))>>is_chroma;
00217 int h= ((height + 4*BLOCK-1) & (~(2*BLOCK-1)))>>is_chroma;
00218
00219 vf->priv->temp_stride[i]= w;
00220 vf->priv->temp[i]= malloc(vf->priv->temp_stride[i]*h*sizeof(int16_t));
00221 vf->priv->src [i]= malloc(vf->priv->temp_stride[i]*h*sizeof(uint8_t));
00222 }
00223 for(i=0; i< (1<<vf->priv->log2_count); i++){
00224 AVCodecContext *avctx_enc;
00225
00226 avctx_enc=
00227 vf->priv->avctx_enc[i]= avcodec_alloc_context();
00228 avctx_enc->width = width + BLOCK;
00229 avctx_enc->height = height + BLOCK;
00230 avctx_enc->time_base= (AVRational){1,25};
00231 avctx_enc->gop_size = 300;
00232 avctx_enc->max_b_frames= 0;
00233 avctx_enc->pix_fmt = PIX_FMT_YUV420P;
00234 avctx_enc->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_LOW_DELAY;
00235 avctx_enc->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
00236 avctx_enc->global_quality= 123;
00237 avcodec_open(avctx_enc, enc);
00238 assert(avctx_enc->codec);
00239 }
00240 vf->priv->frame= avcodec_alloc_frame();
00241 vf->priv->frame_dec= avcodec_alloc_frame();
00242
00243 vf->priv->outbuf_size= (width + BLOCK)*(height + BLOCK)*10;
00244 vf->priv->outbuf= malloc(vf->priv->outbuf_size);
00245
00246 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
00247 }
00248
00249 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
00250 if(mpi->flags&MP_IMGFLAG_PRESERVE) return;
00251
00252 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
00253 mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
00254 mpi->planes[0]=vf->dmpi->planes[0];
00255 mpi->stride[0]=vf->dmpi->stride[0];
00256 mpi->width=vf->dmpi->width;
00257 if(mpi->flags&MP_IMGFLAG_PLANAR){
00258 mpi->planes[1]=vf->dmpi->planes[1];
00259 mpi->planes[2]=vf->dmpi->planes[2];
00260 mpi->stride[1]=vf->dmpi->stride[1];
00261 mpi->stride[2]=vf->dmpi->stride[2];
00262 }
00263 mpi->flags|=MP_IMGFLAG_DIRECT;
00264 }
00265
00266 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
00267 mp_image_t *dmpi;
00268
00269 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
00270
00271 dmpi=vf_get_image(vf->next,mpi->imgfmt,
00272 MP_IMGTYPE_TEMP,
00273 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
00274 mpi->width,mpi->height);
00275 vf_clone_mpi_attributes(dmpi, mpi);
00276 }else{
00277 dmpi=vf->dmpi;
00278 }
00279
00280 vf->priv->mpeg2= mpi->qscale_type;
00281 if(vf->priv->log2_count || !(mpi->flags&MP_IMGFLAG_DIRECT)){
00282 if(mpi->qscale || vf->priv->qp){
00283 filter(vf->priv, dmpi->planes, mpi->planes, dmpi->stride, mpi->stride, mpi->w, mpi->h, mpi->qscale, mpi->qstride);
00284 }else{
00285 memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
00286 memcpy_pic(dmpi->planes[1], mpi->planes[1], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, dmpi->stride[1], mpi->stride[1]);
00287 memcpy_pic(dmpi->planes[2], mpi->planes[2], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, dmpi->stride[2], mpi->stride[2]);
00288 }
00289 }
00290
00291 #if HAVE_MMX
00292 if(gCpuCaps.hasMMX) __asm__ volatile ("emms\n\t");
00293 #endif
00294 #if HAVE_MMX2
00295 if(gCpuCaps.hasMMX2) __asm__ volatile ("sfence\n\t");
00296 #endif
00297
00298 return vf_next_put_image(vf,dmpi, pts);
00299 }
00300
00301 static void uninit(struct vf_instance *vf){
00302 int i;
00303 if(!vf->priv) return;
00304
00305 for(i=0; i<3; i++){
00306 free(vf->priv->temp[i]);
00307 vf->priv->temp[i]= NULL;
00308 free(vf->priv->src[i]);
00309 vf->priv->src[i]= NULL;
00310 }
00311 for(i=0; i<BLOCK*BLOCK; i++){
00312 av_freep(&vf->priv->avctx_enc[i]);
00313 }
00314
00315 free(vf->priv);
00316 vf->priv=NULL;
00317 }
00318
00319
00320 static int query_format(struct vf_instance *vf, unsigned int fmt){
00321 switch(fmt){
00322 case IMGFMT_YV12:
00323 case IMGFMT_I420:
00324 case IMGFMT_IYUV:
00325 case IMGFMT_Y800:
00326 case IMGFMT_Y8:
00327 return vf_next_query_format(vf,fmt);
00328 }
00329 return 0;
00330 }
00331
00332 static int control(struct vf_instance *vf, int request, void* data){
00333 switch(request){
00334 case VFCTRL_QUERY_MAX_PP_LEVEL:
00335 return 8;
00336 case VFCTRL_SET_PP_LEVEL:
00337 vf->priv->log2_count= *((unsigned int*)data);
00338
00339 return CONTROL_TRUE;
00340 }
00341 return vf_next_control(vf,request,data);
00342 }
00343
00344 static int vf_open(vf_instance_t *vf, char *args){
00345
00346 int log2c=-1;
00347
00348 vf->config=config;
00349 vf->put_image=put_image;
00350 vf->get_image=get_image;
00351 vf->query_format=query_format;
00352 vf->uninit=uninit;
00353 vf->control= control;
00354 vf->priv=malloc(sizeof(struct vf_priv_s));
00355 memset(vf->priv, 0, sizeof(struct vf_priv_s));
00356
00357 init_avcodec();
00358
00359 vf->priv->log2_count= 4;
00360
00361 if (args) sscanf(args, "%d:%d:%d", &log2c, &vf->priv->qp, &vf->priv->mode);
00362
00363 if( log2c >=0 && log2c <=8 )
00364 vf->priv->log2_count = log2c;
00365
00366 if(vf->priv->qp < 0)
00367 vf->priv->qp = 0;
00368
00369
00370
00371
00372
00373
00374
00375 return 1;
00376 }
00377
00378 const vf_info_t vf_info_uspp = {
00379 "ultra simple/slow postprocess",
00380 "uspp",
00381 "Michael Niedermayer",
00382 "",
00383 vf_open,
00384 NULL
00385 };