vf_pp7.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (C) 2005 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of MPlayer.
  5. *
  6. * MPlayer is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * MPlayer is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with MPlayer; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <inttypes.h>
  24. #include <math.h>
  25. #include "config.h"
  26. #include "mp_msg.h"
  27. #include "cpudetect.h"
  28. #if HAVE_MALLOC_H
  29. #include <malloc.h>
  30. #endif
  31. #include "libavutil/mem.h"
  32. #include "img_format.h"
  33. #include "mp_image.h"
  34. #include "vf.h"
  35. #include "libvo/fastmemcpy.h"
  36. #define XMIN(a,b) ((a) < (b) ? (a) : (b))
  37. #define XMAX(a,b) ((a) > (b) ? (a) : (b))
  38. //===========================================================================//
  39. static const uint8_t __attribute__((aligned(8))) dither[8][8]={
  40. { 0, 48, 12, 60, 3, 51, 15, 63, },
  41. { 32, 16, 44, 28, 35, 19, 47, 31, },
  42. { 8, 56, 4, 52, 11, 59, 7, 55, },
  43. { 40, 24, 36, 20, 43, 27, 39, 23, },
  44. { 2, 50, 14, 62, 1, 49, 13, 61, },
  45. { 34, 18, 46, 30, 33, 17, 45, 29, },
  46. { 10, 58, 6, 54, 9, 57, 5, 53, },
  47. { 42, 26, 38, 22, 41, 25, 37, 21, },
  48. };
  49. struct vf_priv_s {
  50. int qp;
  51. int mode;
  52. int mpeg2;
  53. int temp_stride;
  54. uint8_t *src;
  55. };
  56. #if 0
  57. static inline void dct7_c(int16_t *dst, int s0, int s1, int s2, int s3, int step){
  58. int s, d;
  59. int dst2[64];
  60. //#define S0 (1024/0.37796447300922719759)
  61. #define C0 ((int)(1024*0.37796447300922719759+0.5)) //sqrt(1/7)
  62. #define C1 ((int)(1024*0.53452248382484879308/6+0.5)) //sqrt(2/7)/6
  63. #define C2 ((int)(1024*0.45221175985034745004/2+0.5))
  64. #define C3 ((int)(1024*0.36264567479870879474/2+0.5))
  65. //0.1962505182412941918 0.0149276808419397944-0.2111781990832339584
  66. #define C4 ((int)(1024*0.1962505182412941918+0.5))
  67. #define C5 ((int)(1024*0.0149276808419397944+0.5))
  68. //#define C6 ((int)(1024*0.2111781990832339584+0.5))
  69. #if 0
  70. s= s0 + s1 + s2;
  71. dst[0*step] = ((s + s3)*C0 + 512) >> 10;
  72. s= (s - 6*s3)*C1 + 512;
  73. d= (s0-s2)*C4 + (s1-s2)*C5;
  74. dst[1*step] = (s + 2*d)>>10;
  75. s -= d;
  76. d= (s1-s0)*C2 + (s1-s2)*C3;
  77. dst[2*step] = (s + d)>>10;
  78. dst[3*step] = (s - d)>>10;
  79. #elif 1
  80. s = s3+s3;
  81. s3= s-s0;
  82. s0= s+s0;
  83. s = s2+s1;
  84. s2= s2-s1;
  85. dst[0*step]= s0 + s;
  86. dst[2*step]= s0 - s;
  87. dst[1*step]= 2*s3 + s2;
  88. dst[3*step]= s3 - 2*s2;
  89. #else
  90. int i,j,n=7;
  91. for(i=0; i<7; i+=2){
  92. dst2[i*step/2]= 0;
  93. for(j=0; j<4; j++)
  94. dst2[i*step/2] += src[j*step] * cos(i*M_PI/n*(j+0.5)) * sqrt((i?2.0:1.0)/n);
  95. if(fabs(dst2[i*step/2] - dst[i*step/2]) > 20)
  96. printf("%d %d %d (%d %d %d %d) -> (%d %d %d %d)\n", i,dst2[i*step/2], dst[i*step/2],src[0*step], src[1*step], src[2*step], src[3*step], dst[0*step], dst[1*step],dst[2*step],dst[3*step]);
  97. }
  98. #endif
  99. }
  100. #endif
  101. static inline void dctA_c(int16_t *dst, uint8_t *src, int stride){
  102. int i;
  103. for(i=0; i<4; i++){
  104. int s0= src[0*stride] + src[6*stride];
  105. int s1= src[1*stride] + src[5*stride];
  106. int s2= src[2*stride] + src[4*stride];
  107. int s3= src[3*stride];
  108. int s= s3+s3;
  109. s3= s-s0;
  110. s0= s+s0;
  111. s = s2+s1;
  112. s2= s2-s1;
  113. dst[0]= s0 + s;
  114. dst[2]= s0 - s;
  115. dst[1]= 2*s3 + s2;
  116. dst[3]= s3 - 2*s2;
  117. src++;
  118. dst+=4;
  119. }
  120. }
  121. static void dctB_c(int16_t *dst, int16_t *src){
  122. int i;
  123. for(i=0; i<4; i++){
  124. int s0= src[0*4] + src[6*4];
  125. int s1= src[1*4] + src[5*4];
  126. int s2= src[2*4] + src[4*4];
  127. int s3= src[3*4];
  128. int s= s3+s3;
  129. s3= s-s0;
  130. s0= s+s0;
  131. s = s2+s1;
  132. s2= s2-s1;
  133. dst[0*4]= s0 + s;
  134. dst[2*4]= s0 - s;
  135. dst[1*4]= 2*s3 + s2;
  136. dst[3*4]= s3 - 2*s2;
  137. src++;
  138. dst++;
  139. }
  140. }
  141. #if HAVE_MMX
  142. static void dctB_mmx(int16_t *dst, int16_t *src){
  143. __asm__ volatile (
  144. "movq (%0), %%mm0 \n\t"
  145. "movq 1*4*2(%0), %%mm1 \n\t"
  146. "paddw 6*4*2(%0), %%mm0 \n\t"
  147. "paddw 5*4*2(%0), %%mm1 \n\t"
  148. "movq 2*4*2(%0), %%mm2 \n\t"
  149. "movq 3*4*2(%0), %%mm3 \n\t"
  150. "paddw 4*4*2(%0), %%mm2 \n\t"
  151. "paddw %%mm3, %%mm3 \n\t" //s
  152. "movq %%mm3, %%mm4 \n\t" //s
  153. "psubw %%mm0, %%mm3 \n\t" //s-s0
  154. "paddw %%mm0, %%mm4 \n\t" //s+s0
  155. "movq %%mm2, %%mm0 \n\t" //s2
  156. "psubw %%mm1, %%mm2 \n\t" //s2-s1
  157. "paddw %%mm1, %%mm0 \n\t" //s2+s1
  158. "movq %%mm4, %%mm1 \n\t" //s0'
  159. "psubw %%mm0, %%mm4 \n\t" //s0'-s'
  160. "paddw %%mm0, %%mm1 \n\t" //s0'+s'
  161. "movq %%mm3, %%mm0 \n\t" //s3'
  162. "psubw %%mm2, %%mm3 \n\t"
  163. "psubw %%mm2, %%mm3 \n\t"
  164. "paddw %%mm0, %%mm2 \n\t"
  165. "paddw %%mm0, %%mm2 \n\t"
  166. "movq %%mm1, (%1) \n\t"
  167. "movq %%mm4, 2*4*2(%1) \n\t"
  168. "movq %%mm2, 1*4*2(%1) \n\t"
  169. "movq %%mm3, 3*4*2(%1) \n\t"
  170. :: "r" (src), "r"(dst)
  171. );
  172. }
  173. #endif
  174. static void (*dctB)(int16_t *dst, int16_t *src)= dctB_c;
  175. #define N0 4
  176. #define N1 5
  177. #define N2 10
  178. #define SN0 2
  179. #define SN1 2.2360679775
  180. #define SN2 3.16227766017
  181. #define N (1<<16)
  182. static const int factor[16]={
  183. N/(N0*N0), N/(N0*N1), N/(N0*N0),N/(N0*N2),
  184. N/(N1*N0), N/(N1*N1), N/(N1*N0),N/(N1*N2),
  185. N/(N0*N0), N/(N0*N1), N/(N0*N0),N/(N0*N2),
  186. N/(N2*N0), N/(N2*N1), N/(N2*N0),N/(N2*N2),
  187. };
  188. static const int thres[16]={
  189. N/(SN0*SN0), N/(SN0*SN2), N/(SN0*SN0),N/(SN0*SN2),
  190. N/(SN2*SN0), N/(SN2*SN2), N/(SN2*SN0),N/(SN2*SN2),
  191. N/(SN0*SN0), N/(SN0*SN2), N/(SN0*SN0),N/(SN0*SN2),
  192. N/(SN2*SN0), N/(SN2*SN2), N/(SN2*SN0),N/(SN2*SN2),
  193. };
  194. static int thres2[99][16];
  195. static void init_thres2(void){
  196. int qp, i;
  197. int bias= 0; //FIXME
  198. for(qp=0; qp<99; qp++){
  199. for(i=0; i<16; i++){
  200. thres2[qp][i]= ((i&1)?SN2:SN0) * ((i&4)?SN2:SN0) * XMAX(1,qp) * (1<<2) - 1 - bias;
  201. }
  202. }
  203. }
  204. static int hardthresh_c(int16_t *src, int qp){
  205. int i;
  206. int a;
  207. a= src[0] * factor[0];
  208. for(i=1; i<16; i++){
  209. unsigned int threshold1= thres2[qp][i];
  210. unsigned int threshold2= (threshold1<<1);
  211. int level= src[i];
  212. if(((unsigned)(level+threshold1))>threshold2){
  213. a += level * factor[i];
  214. }
  215. }
  216. return (a + (1<<11))>>12;
  217. }
  218. static int mediumthresh_c(int16_t *src, int qp){
  219. int i;
  220. int a;
  221. a= src[0] * factor[0];
  222. for(i=1; i<16; i++){
  223. unsigned int threshold1= thres2[qp][i];
  224. unsigned int threshold2= (threshold1<<1);
  225. int level= src[i];
  226. if(((unsigned)(level+threshold1))>threshold2){
  227. if(((unsigned)(level+2*threshold1))>2*threshold2){
  228. a += level * factor[i];
  229. }else{
  230. if(level>0) a+= 2*(level - (int)threshold1)*factor[i];
  231. else a+= 2*(level + (int)threshold1)*factor[i];
  232. }
  233. }
  234. }
  235. return (a + (1<<11))>>12;
  236. }
  237. static int softthresh_c(int16_t *src, int qp){
  238. int i;
  239. int a;
  240. a= src[0] * factor[0];
  241. for(i=1; i<16; i++){
  242. unsigned int threshold1= thres2[qp][i];
  243. unsigned int threshold2= (threshold1<<1);
  244. int level= src[i];
  245. if(((unsigned)(level+threshold1))>threshold2){
  246. if(level>0) a+= (level - (int)threshold1)*factor[i];
  247. else a+= (level + (int)threshold1)*factor[i];
  248. }
  249. }
  250. return (a + (1<<11))>>12;
  251. }
  252. static int (*requantize)(int16_t *src, int qp)= hardthresh_c;
  253. static void filter(struct vf_priv_s *p, uint8_t *dst, uint8_t *src, int dst_stride, int src_stride, int width, int height, uint8_t *qp_store, int qp_stride, int is_luma){
  254. int x, y;
  255. const int stride= is_luma ? p->temp_stride : ((width+16+15)&(~15));
  256. uint8_t *p_src= p->src + 8*stride;
  257. int16_t *block= (int16_t *)p->src;
  258. int16_t *temp= (int16_t *)(p->src + 32);
  259. if (!src || !dst) return; // HACK avoid crash for Y8 colourspace
  260. for(y=0; y<height; y++){
  261. int index= 8 + 8*stride + y*stride;
  262. fast_memcpy(p_src + index, src + y*src_stride, width);
  263. for(x=0; x<8; x++){
  264. p_src[index - x - 1]= p_src[index + x ];
  265. p_src[index + width + x ]= p_src[index + width - x - 1];
  266. }
  267. }
  268. for(y=0; y<8; y++){
  269. fast_memcpy(p_src + ( 7-y)*stride, p_src + ( y+8)*stride, stride);
  270. fast_memcpy(p_src + (height+8+y)*stride, p_src + (height-y+7)*stride, stride);
  271. }
  272. //FIXME (try edge emu)
  273. for(y=0; y<height; y++){
  274. for(x=-8; x<0; x+=4){
  275. const int index= x + y*stride + (8-3)*(1+stride) + 8; //FIXME silly offset
  276. uint8_t *src = p_src + index;
  277. int16_t *tp= temp+4*x;
  278. dctA_c(tp+4*8, src, stride);
  279. }
  280. for(x=0; x<width; ){
  281. const int qps= 3 + is_luma;
  282. int qp;
  283. int end= XMIN(x+8, width);
  284. if(p->qp)
  285. qp= p->qp;
  286. else{
  287. qp= qp_store[ (XMIN(x, width-1)>>qps) + (XMIN(y, height-1)>>qps) * qp_stride];
  288. qp=norm_qscale(qp, p->mpeg2);
  289. }
  290. for(; x<end; x++){
  291. const int index= x + y*stride + (8-3)*(1+stride) + 8; //FIXME silly offset
  292. uint8_t *src = p_src + index;
  293. int16_t *tp= temp+4*x;
  294. int v;
  295. if((x&3)==0)
  296. dctA_c(tp+4*8, src, stride);
  297. dctB(block, tp);
  298. v= requantize(block, qp);
  299. v= (v + dither[y&7][x&7])>>6;
  300. if((unsigned)v > 255)
  301. v= (-v)>>31;
  302. dst[x + y*dst_stride]= v;
  303. }
  304. }
  305. }
  306. }
  307. static int config(struct vf_instance *vf,
  308. int width, int height, int d_width, int d_height,
  309. unsigned int flags, unsigned int outfmt){
  310. int h= (height+16+15)&(~15);
  311. vf->priv->temp_stride= (width+16+15)&(~15);
  312. vf->priv->src = av_malloc(vf->priv->temp_stride*(h+8)*sizeof(uint8_t));
  313. return ff_vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
  314. }
  315. static void get_image(struct vf_instance *vf, mp_image_t *mpi){
  316. if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
  317. // ok, we can do pp in-place (or pp disabled):
  318. vf->dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
  319. mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
  320. mpi->planes[0]=vf->dmpi->planes[0];
  321. mpi->stride[0]=vf->dmpi->stride[0];
  322. mpi->width=vf->dmpi->width;
  323. if(mpi->flags&MP_IMGFLAG_PLANAR){
  324. mpi->planes[1]=vf->dmpi->planes[1];
  325. mpi->planes[2]=vf->dmpi->planes[2];
  326. mpi->stride[1]=vf->dmpi->stride[1];
  327. mpi->stride[2]=vf->dmpi->stride[2];
  328. }
  329. mpi->flags|=MP_IMGFLAG_DIRECT;
  330. }
  331. static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
  332. mp_image_t *dmpi;
  333. if(mpi->flags&MP_IMGFLAG_DIRECT){
  334. dmpi=vf->dmpi;
  335. }else{
  336. // no DR, so get a new image! hope we'll get DR buffer:
  337. dmpi=ff_vf_get_image(vf->next,mpi->imgfmt,
  338. MP_IMGTYPE_TEMP,
  339. MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
  340. mpi->width,mpi->height);
  341. ff_vf_clone_mpi_attributes(dmpi, mpi);
  342. }
  343. vf->priv->mpeg2= mpi->qscale_type;
  344. if(mpi->qscale || vf->priv->qp){
  345. filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, mpi->qscale, mpi->qstride, 1);
  346. filter(vf->priv, dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, mpi->qscale, mpi->qstride, 0);
  347. filter(vf->priv, dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, mpi->qscale, mpi->qstride, 0);
  348. }else{
  349. memcpy_pic(dmpi->planes[0], mpi->planes[0], mpi->w, mpi->h, dmpi->stride[0], mpi->stride[0]);
  350. 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]);
  351. 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]);
  352. }
  353. #if HAVE_MMX
  354. if(ff_gCpuCaps.hasMMX) __asm__ volatile ("emms\n\t");
  355. #endif
  356. #if HAVE_MMX2
  357. if(ff_gCpuCaps.hasMMX2) __asm__ volatile ("sfence\n\t");
  358. #endif
  359. return ff_vf_next_put_image(vf,dmpi, pts);
  360. }
  361. static void uninit(struct vf_instance *vf){
  362. if(!vf->priv) return;
  363. av_free(vf->priv->src);
  364. vf->priv->src= NULL;
  365. free(vf->priv);
  366. vf->priv=NULL;
  367. }
  368. //===========================================================================//
  369. static int query_format(struct vf_instance *vf, unsigned int fmt){
  370. switch(fmt){
  371. case IMGFMT_YVU9:
  372. case IMGFMT_IF09:
  373. case IMGFMT_YV12:
  374. case IMGFMT_I420:
  375. case IMGFMT_IYUV:
  376. case IMGFMT_CLPL:
  377. case IMGFMT_Y800:
  378. case IMGFMT_Y8:
  379. case IMGFMT_444P:
  380. case IMGFMT_422P:
  381. case IMGFMT_411P:
  382. return ff_vf_next_query_format(vf,fmt);
  383. }
  384. return 0;
  385. }
  386. static int control(struct vf_instance *vf, int request, void* data){
  387. return ff_vf_next_control(vf,request,data);
  388. }
  389. static int vf_open(vf_instance_t *vf, char *args){
  390. vf->config=config;
  391. vf->put_image=put_image;
  392. vf->get_image=get_image;
  393. vf->query_format=query_format;
  394. vf->uninit=uninit;
  395. vf->control= control;
  396. vf->priv=malloc(sizeof(struct vf_priv_s));
  397. memset(vf->priv, 0, sizeof(struct vf_priv_s));
  398. if (args) sscanf(args, "%d:%d", &vf->priv->qp, &vf->priv->mode);
  399. if(vf->priv->qp < 0)
  400. vf->priv->qp = 0;
  401. init_thres2();
  402. switch(vf->priv->mode){
  403. case 0: requantize= hardthresh_c; break;
  404. case 1: requantize= softthresh_c; break;
  405. default:
  406. case 2: requantize= mediumthresh_c; break;
  407. }
  408. #if HAVE_MMX
  409. if(ff_gCpuCaps.hasMMX){
  410. dctB= dctB_mmx;
  411. }
  412. #endif
  413. #if 0
  414. if(ff_gCpuCaps.hasMMX){
  415. switch(vf->priv->mode){
  416. case 0: requantize= hardthresh_mmx; break;
  417. case 1: requantize= softthresh_mmx; break;
  418. }
  419. }
  420. #endif
  421. return 1;
  422. }
  423. const vf_info_t ff_vf_info_pp7 = {
  424. "postprocess 7",
  425. "pp7",
  426. "Michael Niedermayer",
  427. "",
  428. vf_open,
  429. NULL
  430. };