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