comparison libmpcodecs/vf_spp.c @ 11277:fffd200d81a7

simple alternative postprocessing filter
author michael
date Sun, 26 Oct 2003 23:07:21 +0000
parents
children b677102fa650
comparison
equal deleted inserted replaced
11276:ec45f2106ec6 11277:fffd200d81a7
1 /*
2 Copyright (C) 2003 Michael Niedermayer <michaelni@gmx.at>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 /*
20 * This implementation is based on an algorithm described in
21 * "Aria Nosratinia Embedded Post-Processing for
22 * Enhancement of Compressed Images (1999)"
23 * (http://citeseer.nj.nec.com/nosratinia99embedded.html)
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <inttypes.h>
30 #include <math.h>
31
32 #include "../config.h"
33 #include "../mp_msg.h"
34 #include "../cpudetect.h"
35 #include "../libavcodec/avcodec.h"
36 #include "../libavcodec/dsputil.h"
37
38 #ifdef HAVE_MALLOC_H
39 #include <malloc.h>
40 #endif
41
42 #include "img_format.h"
43 #include "mp_image.h"
44 #include "vf.h"
45 #include "../libvo/fastmemcpy.h"
46
47 #define XMIN(a,b) ((a) < (b) ? (a) : (b))
48
49 //===========================================================================//
50 const uint8_t __attribute__((aligned(8))) dither[8][8]={
51 { 0, 48, 12, 60, 3, 51, 15, 63, },
52 { 32, 16, 44, 28, 35, 19, 47, 31, },
53 { 8, 56, 4, 52, 11, 59, 7, 55, },
54 { 40, 24, 36, 20, 43, 27, 39, 23, },
55 { 2, 50, 14, 62, 1, 49, 13, 61, },
56 { 34, 18, 46, 30, 33, 17, 45, 29, },
57 { 10, 58, 6, 54, 9, 57, 5, 53, },
58 { 42, 26, 38, 22, 41, 25, 37, 21, },
59 };
60
61 const uint8_t offset[64][2]= {
62 {0,0}, {4,4}, {0,4}, {4,0}, {2,2}, {6,6}, {2,6}, {6,2},
63 {0,2}, {4,6}, {0,6}, {4,2}, {2,0}, {6,4}, {2,4}, {6,0},
64 {1,1}, {5,5}, {1,5}, {5,1}, {3,3}, {7,7}, {3,7}, {7,3},
65 {1,3}, {5,7}, {1,7}, {5,3}, {3,1}, {7,5}, {3,5}, {7,1},
66 {0,1}, {4,5}, {0,5}, {4,1}, {2,3}, {6,7}, {2,7}, {6,3},
67 {0,3}, {4,7}, {0,7}, {4,3}, {2,1}, {6,5}, {2,5}, {6,1},
68 {1,0}, {5,4}, {1,4}, {5,0}, {3,2}, {7,6}, {3,6}, {7,2},
69 {1,2}, {5,6}, {1,6}, {5,2}, {3,0}, {7,4}, {3,4}, {7,0},
70 };
71
72 struct vf_priv_s {
73 int log2_count;
74 int qp;
75 int mpeg2;
76 unsigned int outfmt;
77 int temp_stride;
78 uint8_t *src;
79 int16_t *temp;
80 AVCodecContext *avctx;
81 DSPContext dsp;
82 };
83
84 #define SHIFT 22
85
86 static inline void requantize(DCTELEM dst[64], DCTELEM src[64], int qp, uint8_t *permutation){
87 int i;
88 const int qmul= qp<<1;
89 const int qadd= (qp-1)|1;
90 const int qinv= ((1<<(SHIFT-3)) + qmul/2)/ qmul;
91 int bias= 0; //FIXME
92 unsigned int threshold1, threshold2;
93
94 threshold1= (1<<SHIFT) - bias - 1;
95 threshold2= (threshold1<<1);
96
97 memset(dst, 0, 64*sizeof(DCTELEM));
98 dst[0]= (src[0] + 4)>>3;;
99
100 for(i=1; i<64; i++){
101 int level= qinv*src[i];
102
103 if(((unsigned)(level+threshold1))>threshold2){
104 const int j= permutation[i];
105 if(level>0){
106 level= (bias + level)>>SHIFT;
107 dst[j]= level*qmul + qadd;
108 }else{
109 level= (bias - level)>>SHIFT;
110 dst[j]= -level*qmul - qadd;
111 }
112 }
113 }
114 }
115
116 static inline void add_block(int16_t *dst, int stride, DCTELEM block[64]){
117 int x,y;
118
119 for(y=0; y<8; y++){
120 for(x=0; x<8; x++){
121 dst[x + y*stride]+= block[x + y*8];
122 }
123 }
124 }
125
126 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){
127 int x, y, i;
128 const int count= 1<<p->log2_count;
129 const int log2_scale= 6-p->log2_count;
130 const int stride= p->temp_stride;
131 uint64_t block_align[32];
132 DCTELEM *block = (DCTELEM *)block_align;
133 DCTELEM *block2= (DCTELEM *)(block_align+16);
134
135 for(y=0; y<height; y++){
136 memcpy(p->src + 8 + 8*stride + y*stride, src + y*src_stride, width);
137 memset(p->temp + 8*stride + y*stride, 0, stride*sizeof(int16_t));
138 for(x=0; x<8; x++){
139 int index= 8 + 8*stride + y*stride;
140 p->src[index - x - 1]= p->src[index + x ];
141 p->src[index + width + x ]= p->src[index + width - x - 1];
142 }
143 }
144 for(y=0; y<8; y++){
145 memcpy(p->src + ( 7-y)*stride, p->src + ( y+8)*stride, stride);
146 memcpy(p->src + (width+8+y)*stride, p->src + (width-y+7)*stride, stride);
147 }
148 //FIXME (try edge emu)
149
150 for(y=0; y<height+8; y+=8){
151 for(x=0; x<width+8; x+=8){
152 const int qps= 3 + is_luma;
153 int qp;
154
155 if(p->qp)
156 qp= p->qp;
157 else{
158 qp= qp_store[ (XMIN(x, width-1)>>qps) + (XMIN(y, height-1)>>qps) * qp_stride];
159 if(p->mpeg2) qp>>=1;
160 }
161 for(i=0; i<count; i++){
162 const int x1= x + offset[i][0];
163 const int y1= y + offset[i][1];
164 const int index= x1 + y1*stride;
165
166 p->dsp.get_pixels(block, p->src + index, stride);
167 p->dsp.fdct(block);
168 requantize(block2, block, qp, p->dsp.idct_permutation);
169 p->dsp.idct(block2);
170 add_block(p->temp + index, stride, block2);
171 }
172 }
173 }
174
175 for(y=0; y<height; y++){
176 uint8_t *d= dither[y&7];
177 for(x=0; x<width; x+=8){
178 const int index= 8 + 8*stride + x + y*stride;
179 dst[x + y*src_stride + 0]= ((p->temp[index + 0]<<log2_scale) + d[0])>>6;
180 dst[x + y*src_stride + 1]= ((p->temp[index + 1]<<log2_scale) + d[1])>>6;
181 dst[x + y*src_stride + 2]= ((p->temp[index + 2]<<log2_scale) + d[2])>>6;
182 dst[x + y*src_stride + 3]= ((p->temp[index + 3]<<log2_scale) + d[3])>>6;
183 dst[x + y*src_stride + 4]= ((p->temp[index + 4]<<log2_scale) + d[4])>>6;
184 dst[x + y*src_stride + 5]= ((p->temp[index + 5]<<log2_scale) + d[5])>>6;
185 dst[x + y*src_stride + 6]= ((p->temp[index + 6]<<log2_scale) + d[6])>>6;
186 dst[x + y*src_stride + 7]= ((p->temp[index + 7]<<log2_scale) + d[7])>>6;
187 }
188 }
189 //FIXME reorder for better caching
190 }
191
192 static int config(struct vf_instance_s* vf,
193 int width, int height, int d_width, int d_height,
194 unsigned int flags, unsigned int outfmt){
195
196 vf->priv->temp_stride= (width+16+15)&(~15);
197 vf->priv->temp= malloc(vf->priv->temp_stride*(height+16)*sizeof(int16_t));
198 vf->priv->src = malloc(vf->priv->temp_stride*(height+16)*sizeof(uint8_t));
199
200 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
201 }
202
203 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
204 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
205 if(mpi->imgfmt!=vf->priv->outfmt) return; // colorspace differ
206 // ok, we can do pp in-place (or pp disabled):
207 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
208 mpi->type, mpi->flags, mpi->w, mpi->h);
209 mpi->planes[0]=vf->dmpi->planes[0];
210 mpi->stride[0]=vf->dmpi->stride[0];
211 mpi->width=vf->dmpi->width;
212 if(mpi->flags&MP_IMGFLAG_PLANAR){
213 mpi->planes[1]=vf->dmpi->planes[1];
214 mpi->planes[2]=vf->dmpi->planes[2];
215 mpi->stride[1]=vf->dmpi->stride[1];
216 mpi->stride[2]=vf->dmpi->stride[2];
217 }
218 mpi->flags|=MP_IMGFLAG_DIRECT;
219 }
220
221 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
222 mp_image_t *dmpi;
223
224 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
225 // no DR, so get a new image! hope we'll get DR buffer:
226 vf->dmpi=vf_get_image(vf->next,vf->priv->outfmt,
227 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
228 mpi->w,mpi->h);
229 }
230
231 dmpi= vf->dmpi;
232
233 vf->priv->mpeg2= mpi->qscale_type;
234
235 filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, mpi->qscale, mpi->qstride, 1);
236 filter(vf->priv, dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w>>1, mpi->h>>1, mpi->qscale, mpi->qstride, 0);
237 filter(vf->priv, dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w>>1, mpi->h>>1, mpi->qscale, mpi->qstride, 0);
238
239 vf_clone_mpi_attributes(dmpi, mpi);
240
241 #ifdef HAVE_MMX
242 if(gCpuCaps.hasMMX) asm volatile ("emms\n\t");
243 #endif
244 #ifdef HAVE_MMX2
245 if(gCpuCaps.hasMMX2) asm volatile ("sfence\n\t");
246 #endif
247
248 return vf_next_put_image(vf,dmpi);
249 }
250
251 static void uninit(struct vf_instance_s* vf){
252 if(!vf->priv) return;
253
254 if(vf->priv->temp) free(vf->priv->temp);
255 vf->priv->temp= NULL;
256 if(vf->priv->src) free(vf->priv->src);
257 vf->priv->src= NULL;
258 if(vf->priv->avctx) free(vf->priv->avctx);
259 vf->priv->avctx= NULL;
260
261 free(vf->priv);
262 vf->priv=NULL;
263 }
264
265 //===========================================================================//
266
267 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
268 switch(fmt)
269 {
270 case IMGFMT_YV12:
271 case IMGFMT_I420:
272 case IMGFMT_IYUV:
273 return vf_next_query_format(vf,vf->priv->outfmt);
274 }
275 return 0;
276 }
277
278 static unsigned int fmt_list[]={
279 IMGFMT_YV12,
280 IMGFMT_I420,
281 IMGFMT_IYUV,
282 0
283 };
284
285 static int open(vf_instance_t *vf, char* args){
286 vf->config=config;
287 vf->put_image=put_image;
288 vf->get_image=get_image;
289 vf->query_format=query_format;
290 vf->uninit=uninit;
291 vf->priv=malloc(sizeof(struct vf_priv_s));
292 memset(vf->priv, 0, sizeof(struct vf_priv_s));
293 vf->priv->avctx= avcodec_alloc_context();
294 dsputil_init(&vf->priv->dsp, vf->priv->avctx);
295
296 vf->priv->log2_count= 6;
297
298 if (args) sscanf(args, "%d:%d", &vf->priv->log2_count, &vf->priv->qp);
299
300 // check csp:
301 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
302 if(!vf->priv->outfmt)
303 {
304 uninit(vf);
305 return 0; // no csp match :(
306 }
307
308 return 1;
309 }
310
311 vf_info_t vf_info_spp = {
312 "simple postprocess",
313 "spp",
314 "Michael Niedermayer",
315 "",
316 open,
317 NULL
318 };
319
320 //===========================================================================//