vf_pp7.c 14 KB

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