OSDN Git Service

Fixed invalid access in wavpack decoder on corrupted bitstream.
[coroid/libav_saccubus.git] / libavcodec / h264dsp_template.c
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3  * Copyright (c) 2003-2010 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 DSP functions.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #include "bit_depth_template.c"
29
30 #define op_scale1(x)  block[x] = av_clip_pixel( (block[x]*weight + offset) >> log2_denom )
31 #define op_scale2(x)  dst[x] = av_clip_pixel( (src[x]*weights + dst[x]*weightd + offset) >> (log2_denom+1))
32 #define H264_WEIGHT(W,H) \
33 static void FUNCC(weight_h264_pixels ## W ## x ## H)(uint8_t *_block, int stride, int log2_denom, int weight, int offset){ \
34     int y; \
35     pixel *block = (pixel*)_block; \
36     stride /= sizeof(pixel); \
37     offset <<= (log2_denom + (BIT_DEPTH-8)); \
38     if(log2_denom) offset += 1<<(log2_denom-1); \
39     for(y=0; y<H; y++, block += stride){ \
40         op_scale1(0); \
41         op_scale1(1); \
42         if(W==2) continue; \
43         op_scale1(2); \
44         op_scale1(3); \
45         if(W==4) continue; \
46         op_scale1(4); \
47         op_scale1(5); \
48         op_scale1(6); \
49         op_scale1(7); \
50         if(W==8) continue; \
51         op_scale1(8); \
52         op_scale1(9); \
53         op_scale1(10); \
54         op_scale1(11); \
55         op_scale1(12); \
56         op_scale1(13); \
57         op_scale1(14); \
58         op_scale1(15); \
59     } \
60 } \
61 static void FUNCC(biweight_h264_pixels ## W ## x ## H)(uint8_t *_dst, uint8_t *_src, int stride, int log2_denom, int weightd, int weights, int offset){ \
62     int y; \
63     pixel *dst = (pixel*)_dst; \
64     pixel *src = (pixel*)_src; \
65     stride /= sizeof(pixel); \
66     offset <<= (BIT_DEPTH-8); \
67     offset = ((offset + 1) | 1) << log2_denom; \
68     for(y=0; y<H; y++, dst += stride, src += stride){ \
69         op_scale2(0); \
70         op_scale2(1); \
71         if(W==2) continue; \
72         op_scale2(2); \
73         op_scale2(3); \
74         if(W==4) continue; \
75         op_scale2(4); \
76         op_scale2(5); \
77         op_scale2(6); \
78         op_scale2(7); \
79         if(W==8) continue; \
80         op_scale2(8); \
81         op_scale2(9); \
82         op_scale2(10); \
83         op_scale2(11); \
84         op_scale2(12); \
85         op_scale2(13); \
86         op_scale2(14); \
87         op_scale2(15); \
88     } \
89 }
90
91 H264_WEIGHT(16,16)
92 H264_WEIGHT(16,8)
93 H264_WEIGHT(8,16)
94 H264_WEIGHT(8,8)
95 H264_WEIGHT(8,4)
96 H264_WEIGHT(4,8)
97 H264_WEIGHT(4,4)
98 H264_WEIGHT(4,2)
99 H264_WEIGHT(2,4)
100 H264_WEIGHT(2,2)
101
102 #undef op_scale1
103 #undef op_scale2
104 #undef H264_WEIGHT
105
106 static av_always_inline av_flatten void FUNCC(h264_loop_filter_luma)(uint8_t *_pix, int xstride, int ystride, int inner_iters, int alpha, int beta, int8_t *tc0)
107 {
108     pixel *pix = (pixel*)_pix;
109     int i, d;
110     xstride /= sizeof(pixel);
111     ystride /= sizeof(pixel);
112     alpha <<= BIT_DEPTH - 8;
113     beta  <<= BIT_DEPTH - 8;
114     for( i = 0; i < 4; i++ ) {
115         const int tc_orig = tc0[i] << (BIT_DEPTH - 8);
116         if( tc_orig < 0 ) {
117             pix += inner_iters*ystride;
118             continue;
119         }
120         for( d = 0; d < inner_iters; d++ ) {
121             const int p0 = pix[-1*xstride];
122             const int p1 = pix[-2*xstride];
123             const int p2 = pix[-3*xstride];
124             const int q0 = pix[0];
125             const int q1 = pix[1*xstride];
126             const int q2 = pix[2*xstride];
127
128             if( FFABS( p0 - q0 ) < alpha &&
129                 FFABS( p1 - p0 ) < beta &&
130                 FFABS( q1 - q0 ) < beta ) {
131
132                 int tc = tc_orig;
133                 int i_delta;
134
135                 if( FFABS( p2 - p0 ) < beta ) {
136                     if(tc_orig)
137                     pix[-2*xstride] = p1 + av_clip( (( p2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - p1, -tc_orig, tc_orig );
138                     tc++;
139                 }
140                 if( FFABS( q2 - q0 ) < beta ) {
141                     if(tc_orig)
142                     pix[   xstride] = q1 + av_clip( (( q2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - q1, -tc_orig, tc_orig );
143                     tc++;
144                 }
145
146                 i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
147                 pix[-xstride] = av_clip_pixel( p0 + i_delta );    /* p0' */
148                 pix[0]        = av_clip_pixel( q0 - i_delta );    /* q0' */
149             }
150             pix += ystride;
151         }
152     }
153 }
154 static void FUNCC(h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
155 {
156     FUNCC(h264_loop_filter_luma)(pix, stride, sizeof(pixel), 4, alpha, beta, tc0);
157 }
158 static void FUNCC(h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
159 {
160     FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 4, alpha, beta, tc0);
161 }
162 static void FUNCC(h264_h_loop_filter_luma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
163 {
164     FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
165 }
166
167 static av_always_inline av_flatten void FUNCC(h264_loop_filter_luma_intra)(uint8_t *_pix, int xstride, int ystride, int inner_iters, int alpha, int beta)
168 {
169     pixel *pix = (pixel*)_pix;
170     int d;
171     xstride /= sizeof(pixel);
172     ystride /= sizeof(pixel);
173     alpha <<= BIT_DEPTH - 8;
174     beta  <<= BIT_DEPTH - 8;
175     for( d = 0; d < 4 * inner_iters; d++ ) {
176         const int p2 = pix[-3*xstride];
177         const int p1 = pix[-2*xstride];
178         const int p0 = pix[-1*xstride];
179
180         const int q0 = pix[ 0*xstride];
181         const int q1 = pix[ 1*xstride];
182         const int q2 = pix[ 2*xstride];
183
184         if( FFABS( p0 - q0 ) < alpha &&
185             FFABS( p1 - p0 ) < beta &&
186             FFABS( q1 - q0 ) < beta ) {
187
188             if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
189                 if( FFABS( p2 - p0 ) < beta)
190                 {
191                     const int p3 = pix[-4*xstride];
192                     /* p0', p1', p2' */
193                     pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
194                     pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
195                     pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
196                 } else {
197                     /* p0' */
198                     pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
199                 }
200                 if( FFABS( q2 - q0 ) < beta)
201                 {
202                     const int q3 = pix[3*xstride];
203                     /* q0', q1', q2' */
204                     pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
205                     pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
206                     pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
207                 } else {
208                     /* q0' */
209                     pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
210                 }
211             }else{
212                 /* p0', q0' */
213                 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
214                 pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
215             }
216         }
217         pix += ystride;
218     }
219 }
220 static void FUNCC(h264_v_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
221 {
222     FUNCC(h264_loop_filter_luma_intra)(pix, stride, sizeof(pixel), 4, alpha, beta);
223 }
224 static void FUNCC(h264_h_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
225 {
226     FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 4, alpha, beta);
227 }
228 static void FUNCC(h264_h_loop_filter_luma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
229 {
230     FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
231 }
232
233 static av_always_inline av_flatten void FUNCC(h264_loop_filter_chroma)(uint8_t *_pix, int xstride, int ystride, int inner_iters, int alpha, int beta, int8_t *tc0)
234 {
235     pixel *pix = (pixel*)_pix;
236     int i, d;
237     xstride /= sizeof(pixel);
238     ystride /= sizeof(pixel);
239     alpha <<= BIT_DEPTH - 8;
240     beta  <<= BIT_DEPTH - 8;
241     for( i = 0; i < 4; i++ ) {
242         const int tc = ((tc0[i] - 1) << (BIT_DEPTH - 8)) + 1;
243         if( tc <= 0 ) {
244             pix += inner_iters*ystride;
245             continue;
246         }
247         for( d = 0; d < inner_iters; d++ ) {
248             const int p0 = pix[-1*xstride];
249             const int p1 = pix[-2*xstride];
250             const int q0 = pix[0];
251             const int q1 = pix[1*xstride];
252
253             if( FFABS( p0 - q0 ) < alpha &&
254                 FFABS( p1 - p0 ) < beta &&
255                 FFABS( q1 - q0 ) < beta ) {
256
257                 int delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
258
259                 pix[-xstride] = av_clip_pixel( p0 + delta );    /* p0' */
260                 pix[0]        = av_clip_pixel( q0 - delta );    /* q0' */
261             }
262             pix += ystride;
263         }
264     }
265 }
266 static void FUNCC(h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
267 {
268     FUNCC(h264_loop_filter_chroma)(pix, stride, sizeof(pixel), 2, alpha, beta, tc0);
269 }
270 static void FUNCC(h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
271 {
272     FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
273 }
274 static void FUNCC(h264_h_loop_filter_chroma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
275 {
276     FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 1, alpha, beta, tc0);
277 }
278
279 static av_always_inline av_flatten void FUNCC(h264_loop_filter_chroma_intra)(uint8_t *_pix, int xstride, int ystride, int inner_iters, int alpha, int beta)
280 {
281     pixel *pix = (pixel*)_pix;
282     int d;
283     xstride /= sizeof(pixel);
284     ystride /= sizeof(pixel);
285     alpha <<= BIT_DEPTH - 8;
286     beta  <<= BIT_DEPTH - 8;
287     for( d = 0; d < 4 * inner_iters; d++ ) {
288         const int p0 = pix[-1*xstride];
289         const int p1 = pix[-2*xstride];
290         const int q0 = pix[0];
291         const int q1 = pix[1*xstride];
292
293         if( FFABS( p0 - q0 ) < alpha &&
294             FFABS( p1 - p0 ) < beta &&
295             FFABS( q1 - q0 ) < beta ) {
296
297             pix[-xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;   /* p0' */
298             pix[0]        = ( 2*q1 + q0 + p1 + 2 ) >> 2;   /* q0' */
299         }
300         pix += ystride;
301     }
302 }
303 static void FUNCC(h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
304 {
305     FUNCC(h264_loop_filter_chroma_intra)(pix, stride, sizeof(pixel), 2, alpha, beta);
306 }
307 static void FUNCC(h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
308 {
309     FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
310 }
311 static void FUNCC(h264_h_loop_filter_chroma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
312 {
313     FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 1, alpha, beta);
314 }