11277
|
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 if(((unsigned)(level+threshold1))>threshold2){
|
|
103 const int j= permutation[i];
|
|
104 if(level>0){
|
|
105 level= (bias + level)>>SHIFT;
|
|
106 dst[j]= level*qmul + qadd;
|
|
107 }else{
|
|
108 level= (bias - level)>>SHIFT;
|
|
109 dst[j]= -level*qmul - qadd;
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113 }
|
|
114
|
|
115 static inline void add_block(int16_t *dst, int stride, DCTELEM block[64]){
|
11280
|
116 int y;
|
11277
|
117
|
|
118 for(y=0; y<8; y++){
|
11280
|
119 *(uint32_t*)&dst[0 + y*stride]+= *(uint32_t*)&block[0 + y*8];
|
|
120 *(uint32_t*)&dst[2 + y*stride]+= *(uint32_t*)&block[2 + y*8];
|
|
121 *(uint32_t*)&dst[4 + y*stride]+= *(uint32_t*)&block[4 + y*8];
|
|
122 *(uint32_t*)&dst[6 + y*stride]+= *(uint32_t*)&block[6 + y*8];
|
11277
|
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;
|
11281
|
130 const int stride= p->temp_stride; //FIXME
|
11277
|
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);
|
11281
|
146 memcpy(p->src + (height+8+y)*stride, p->src + (height-y+7)*stride, stride);
|
11277
|
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 p->dsp.get_pixels(block, p->src + index, stride);
|
|
166 p->dsp.fdct(block);
|
|
167 requantize(block2, block, qp, p->dsp.idct_permutation);
|
|
168 p->dsp.idct(block2);
|
|
169 add_block(p->temp + index, stride, block2);
|
|
170 }
|
|
171 }
|
|
172 }
|
|
173
|
11295
|
174 #define STORE(pos) \
|
|
175 temp= ((p->temp[index + pos]<<log2_scale) + d[pos])>>6;\
|
|
176 if(temp & 0x100) temp= ~(temp>>31);\
|
|
177 dst[x + y*dst_stride + pos]= temp;
|
|
178
|
11277
|
179 for(y=0; y<height; y++){
|
|
180 uint8_t *d= dither[y&7];
|
|
181 for(x=0; x<width; x+=8){
|
|
182 const int index= 8 + 8*stride + x + y*stride;
|
11295
|
183 int temp;
|
|
184 STORE(0);
|
|
185 STORE(1);
|
|
186 STORE(2);
|
|
187 STORE(3);
|
|
188 STORE(4);
|
|
189 STORE(5);
|
|
190 STORE(6);
|
|
191 STORE(7);
|
11277
|
192 }
|
|
193 }
|
11295
|
194 #if 0
|
|
195 for(y=0; y<height; y++){
|
|
196 for(x=0; x<width; x++){
|
|
197 if((((x>>6) ^ (y>>6)) & 1) == 0)
|
|
198 dst[x + y*dst_stride]= p->src[8 + 8*stride + x + y*stride];
|
|
199 if((x&63) == 0 || (y&63)==0)
|
|
200 dst[x + y*dst_stride] += 128;
|
|
201 }
|
|
202 }
|
|
203 #endif
|
11277
|
204 //FIXME reorder for better caching
|
|
205 }
|
|
206
|
|
207 static int config(struct vf_instance_s* vf,
|
|
208 int width, int height, int d_width, int d_height,
|
|
209 unsigned int flags, unsigned int outfmt){
|
|
210
|
|
211 vf->priv->temp_stride= (width+16+15)&(~15);
|
|
212 vf->priv->temp= malloc(vf->priv->temp_stride*(height+16)*sizeof(int16_t));
|
|
213 vf->priv->src = malloc(vf->priv->temp_stride*(height+16)*sizeof(uint8_t));
|
|
214
|
|
215 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
|
|
216 }
|
|
217
|
|
218 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
219 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
|
|
220 if(mpi->imgfmt!=vf->priv->outfmt) return; // colorspace differ
|
|
221 // ok, we can do pp in-place (or pp disabled):
|
|
222 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
223 mpi->type, mpi->flags, mpi->w, mpi->h);
|
|
224 mpi->planes[0]=vf->dmpi->planes[0];
|
|
225 mpi->stride[0]=vf->dmpi->stride[0];
|
|
226 mpi->width=vf->dmpi->width;
|
|
227 if(mpi->flags&MP_IMGFLAG_PLANAR){
|
|
228 mpi->planes[1]=vf->dmpi->planes[1];
|
|
229 mpi->planes[2]=vf->dmpi->planes[2];
|
|
230 mpi->stride[1]=vf->dmpi->stride[1];
|
|
231 mpi->stride[2]=vf->dmpi->stride[2];
|
|
232 }
|
|
233 mpi->flags|=MP_IMGFLAG_DIRECT;
|
|
234 }
|
|
235
|
|
236 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
237 mp_image_t *dmpi;
|
|
238
|
|
239 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
|
|
240 // no DR, so get a new image! hope we'll get DR buffer:
|
|
241 vf->dmpi=vf_get_image(vf->next,vf->priv->outfmt,
|
|
242 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
|
|
243 mpi->w,mpi->h);
|
|
244 }
|
|
245
|
|
246 dmpi= vf->dmpi;
|
|
247
|
|
248 vf->priv->mpeg2= mpi->qscale_type;
|
|
249
|
|
250 filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, mpi->qscale, mpi->qstride, 1);
|
|
251 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);
|
|
252 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);
|
|
253
|
|
254 vf_clone_mpi_attributes(dmpi, mpi);
|
|
255
|
|
256 #ifdef HAVE_MMX
|
|
257 if(gCpuCaps.hasMMX) asm volatile ("emms\n\t");
|
|
258 #endif
|
|
259 #ifdef HAVE_MMX2
|
|
260 if(gCpuCaps.hasMMX2) asm volatile ("sfence\n\t");
|
|
261 #endif
|
|
262
|
|
263 return vf_next_put_image(vf,dmpi);
|
|
264 }
|
|
265
|
|
266 static void uninit(struct vf_instance_s* vf){
|
|
267 if(!vf->priv) return;
|
|
268
|
|
269 if(vf->priv->temp) free(vf->priv->temp);
|
|
270 vf->priv->temp= NULL;
|
|
271 if(vf->priv->src) free(vf->priv->src);
|
|
272 vf->priv->src= NULL;
|
|
273 if(vf->priv->avctx) free(vf->priv->avctx);
|
|
274 vf->priv->avctx= NULL;
|
|
275
|
|
276 free(vf->priv);
|
|
277 vf->priv=NULL;
|
|
278 }
|
|
279
|
|
280 //===========================================================================//
|
|
281
|
|
282 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
|
|
283 switch(fmt)
|
|
284 {
|
|
285 case IMGFMT_YV12:
|
|
286 case IMGFMT_I420:
|
|
287 case IMGFMT_IYUV:
|
|
288 return vf_next_query_format(vf,vf->priv->outfmt);
|
|
289 }
|
|
290 return 0;
|
|
291 }
|
|
292
|
|
293 static unsigned int fmt_list[]={
|
|
294 IMGFMT_YV12,
|
|
295 IMGFMT_I420,
|
|
296 IMGFMT_IYUV,
|
|
297 0
|
|
298 };
|
|
299
|
|
300 static int open(vf_instance_t *vf, char* args){
|
|
301 vf->config=config;
|
|
302 vf->put_image=put_image;
|
|
303 vf->get_image=get_image;
|
|
304 vf->query_format=query_format;
|
|
305 vf->uninit=uninit;
|
|
306 vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
307 memset(vf->priv, 0, sizeof(struct vf_priv_s));
|
11280
|
308
|
|
309 avcodec_init();
|
|
310
|
11277
|
311 vf->priv->avctx= avcodec_alloc_context();
|
|
312 dsputil_init(&vf->priv->dsp, vf->priv->avctx);
|
|
313
|
|
314 vf->priv->log2_count= 6;
|
|
315
|
|
316 if (args) sscanf(args, "%d:%d", &vf->priv->log2_count, &vf->priv->qp);
|
|
317
|
|
318 // check csp:
|
|
319 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
|
|
320 if(!vf->priv->outfmt)
|
|
321 {
|
|
322 uninit(vf);
|
|
323 return 0; // no csp match :(
|
|
324 }
|
|
325
|
|
326 return 1;
|
|
327 }
|
|
328
|
|
329 vf_info_t vf_info_spp = {
|
|
330 "simple postprocess",
|
|
331 "spp",
|
|
332 "Michael Niedermayer",
|
|
333 "",
|
|
334 open,
|
|
335 NULL
|
|
336 };
|
|
337
|
|
338 //===========================================================================//
|