]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264dsp_template.c
Add missing #includes to mp3_header_(de)compress bsf
[ffmpeg] / 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 "high_bit_depth.h"
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 = ((offset + 1) | 1) << log2_denom; \
67     for(y=0; y<H; y++, dst += stride, src += stride){ \
68         op_scale2(0); \
69         op_scale2(1); \
70         if(W==2) continue; \
71         op_scale2(2); \
72         op_scale2(3); \
73         if(W==4) continue; \
74         op_scale2(4); \
75         op_scale2(5); \
76         op_scale2(6); \
77         op_scale2(7); \
78         if(W==8) continue; \
79         op_scale2(8); \
80         op_scale2(9); \
81         op_scale2(10); \
82         op_scale2(11); \
83         op_scale2(12); \
84         op_scale2(13); \
85         op_scale2(14); \
86         op_scale2(15); \
87     } \
88 }
89
90 H264_WEIGHT(16,16)
91 H264_WEIGHT(16,8)
92 H264_WEIGHT(8,16)
93 H264_WEIGHT(8,8)
94 H264_WEIGHT(8,4)
95 H264_WEIGHT(4,8)
96 H264_WEIGHT(4,4)
97 H264_WEIGHT(4,2)
98 H264_WEIGHT(2,4)
99 H264_WEIGHT(2,2)
100
101 #undef op_scale1
102 #undef op_scale2
103 #undef H264_WEIGHT
104
105 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)
106 {
107     pixel *pix = (pixel*)_pix;
108     int i, d;
109     xstride /= sizeof(pixel);
110     ystride /= sizeof(pixel);
111     alpha <<= BIT_DEPTH - 8;
112     beta  <<= BIT_DEPTH - 8;
113     for( i = 0; i < 4; i++ ) {
114         const int tc_orig = tc0[i] << (BIT_DEPTH - 8);
115         if( tc_orig < 0 ) {
116             pix += inner_iters*ystride;
117             continue;
118         }
119         for( d = 0; d < inner_iters; d++ ) {
120             const int p0 = pix[-1*xstride];
121             const int p1 = pix[-2*xstride];
122             const int p2 = pix[-3*xstride];
123             const int q0 = pix[0];
124             const int q1 = pix[1*xstride];
125             const int q2 = pix[2*xstride];
126
127             if( FFABS( p0 - q0 ) < alpha &&
128                 FFABS( p1 - p0 ) < beta &&
129                 FFABS( q1 - q0 ) < beta ) {
130
131                 int tc = tc_orig;
132                 int i_delta;
133
134                 if( FFABS( p2 - p0 ) < beta ) {
135                     if(tc_orig)
136                     pix[-2*xstride] = p1 + av_clip( (( p2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - p1, -tc_orig, tc_orig );
137                     tc++;
138                 }
139                 if( FFABS( q2 - q0 ) < beta ) {
140                     if(tc_orig)
141                     pix[   xstride] = q1 + av_clip( (( q2 + ( ( p0 + q0 + 1 ) >> 1 ) ) >> 1) - q1, -tc_orig, tc_orig );
142                     tc++;
143                 }
144
145                 i_delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
146                 pix[-xstride] = av_clip_pixel( p0 + i_delta );    /* p0' */
147                 pix[0]        = av_clip_pixel( q0 - i_delta );    /* q0' */
148             }
149             pix += ystride;
150         }
151     }
152 }
153 static void FUNCC(h264_v_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
154 {
155     FUNCC(h264_loop_filter_luma)(pix, stride, sizeof(pixel), 4, alpha, beta, tc0);
156 }
157 static void FUNCC(h264_h_loop_filter_luma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
158 {
159     FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 4, alpha, beta, tc0);
160 }
161 static void FUNCC(h264_h_loop_filter_luma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
162 {
163     FUNCC(h264_loop_filter_luma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
164 }
165
166 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)
167 {
168     pixel *pix = (pixel*)_pix;
169     int d;
170     xstride /= sizeof(pixel);
171     ystride /= sizeof(pixel);
172     alpha <<= BIT_DEPTH - 8;
173     beta  <<= BIT_DEPTH - 8;
174     for( d = 0; d < 4 * inner_iters; d++ ) {
175         const int p2 = pix[-3*xstride];
176         const int p1 = pix[-2*xstride];
177         const int p0 = pix[-1*xstride];
178
179         const int q0 = pix[ 0*xstride];
180         const int q1 = pix[ 1*xstride];
181         const int q2 = pix[ 2*xstride];
182
183         if( FFABS( p0 - q0 ) < alpha &&
184             FFABS( p1 - p0 ) < beta &&
185             FFABS( q1 - q0 ) < beta ) {
186
187             if(FFABS( p0 - q0 ) < (( alpha >> 2 ) + 2 )){
188                 if( FFABS( p2 - p0 ) < beta)
189                 {
190                     const int p3 = pix[-4*xstride];
191                     /* p0', p1', p2' */
192                     pix[-1*xstride] = ( p2 + 2*p1 + 2*p0 + 2*q0 + q1 + 4 ) >> 3;
193                     pix[-2*xstride] = ( p2 + p1 + p0 + q0 + 2 ) >> 2;
194                     pix[-3*xstride] = ( 2*p3 + 3*p2 + p1 + p0 + q0 + 4 ) >> 3;
195                 } else {
196                     /* p0' */
197                     pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
198                 }
199                 if( FFABS( q2 - q0 ) < beta)
200                 {
201                     const int q3 = pix[3*xstride];
202                     /* q0', q1', q2' */
203                     pix[0*xstride] = ( p1 + 2*p0 + 2*q0 + 2*q1 + q2 + 4 ) >> 3;
204                     pix[1*xstride] = ( p0 + q0 + q1 + q2 + 2 ) >> 2;
205                     pix[2*xstride] = ( 2*q3 + 3*q2 + q1 + q0 + p0 + 4 ) >> 3;
206                 } else {
207                     /* q0' */
208                     pix[0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
209                 }
210             }else{
211                 /* p0', q0' */
212                 pix[-1*xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;
213                 pix[ 0*xstride] = ( 2*q1 + q0 + p1 + 2 ) >> 2;
214             }
215         }
216         pix += ystride;
217     }
218 }
219 static void FUNCC(h264_v_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
220 {
221     FUNCC(h264_loop_filter_luma_intra)(pix, stride, sizeof(pixel), 4, alpha, beta);
222 }
223 static void FUNCC(h264_h_loop_filter_luma_intra)(uint8_t *pix, int stride, int alpha, int beta)
224 {
225     FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 4, alpha, beta);
226 }
227 static void FUNCC(h264_h_loop_filter_luma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
228 {
229     FUNCC(h264_loop_filter_luma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
230 }
231
232 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)
233 {
234     pixel *pix = (pixel*)_pix;
235     int i, d;
236     xstride /= sizeof(pixel);
237     ystride /= sizeof(pixel);
238     alpha <<= BIT_DEPTH - 8;
239     beta  <<= BIT_DEPTH - 8;
240     for( i = 0; i < 4; i++ ) {
241         const int tc = ((tc0[i] - 1) << (BIT_DEPTH - 8)) + 1;
242         if( tc <= 0 ) {
243             pix += inner_iters*ystride;
244             continue;
245         }
246         for( d = 0; d < inner_iters; d++ ) {
247             const int p0 = pix[-1*xstride];
248             const int p1 = pix[-2*xstride];
249             const int q0 = pix[0];
250             const int q1 = pix[1*xstride];
251
252             if( FFABS( p0 - q0 ) < alpha &&
253                 FFABS( p1 - p0 ) < beta &&
254                 FFABS( q1 - q0 ) < beta ) {
255
256                 int delta = av_clip( (((q0 - p0 ) << 2) + (p1 - q1) + 4) >> 3, -tc, tc );
257
258                 pix[-xstride] = av_clip_pixel( p0 + delta );    /* p0' */
259                 pix[0]        = av_clip_pixel( q0 - delta );    /* q0' */
260             }
261             pix += ystride;
262         }
263     }
264 }
265 static void FUNCC(h264_v_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
266 {
267     FUNCC(h264_loop_filter_chroma)(pix, stride, sizeof(pixel), 2, alpha, beta, tc0);
268 }
269 static void FUNCC(h264_h_loop_filter_chroma)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
270 {
271     FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 2, alpha, beta, tc0);
272 }
273 static void FUNCC(h264_h_loop_filter_chroma_mbaff)(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0)
274 {
275     FUNCC(h264_loop_filter_chroma)(pix, sizeof(pixel), stride, 1, alpha, beta, tc0);
276 }
277
278 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)
279 {
280     pixel *pix = (pixel*)_pix;
281     int d;
282     xstride /= sizeof(pixel);
283     ystride /= sizeof(pixel);
284     alpha <<= BIT_DEPTH - 8;
285     beta  <<= BIT_DEPTH - 8;
286     for( d = 0; d < 4 * inner_iters; d++ ) {
287         const int p0 = pix[-1*xstride];
288         const int p1 = pix[-2*xstride];
289         const int q0 = pix[0];
290         const int q1 = pix[1*xstride];
291
292         if( FFABS( p0 - q0 ) < alpha &&
293             FFABS( p1 - p0 ) < beta &&
294             FFABS( q1 - q0 ) < beta ) {
295
296             pix[-xstride] = ( 2*p1 + p0 + q1 + 2 ) >> 2;   /* p0' */
297             pix[0]        = ( 2*q1 + q0 + p1 + 2 ) >> 2;   /* q0' */
298         }
299         pix += ystride;
300     }
301 }
302 static void FUNCC(h264_v_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
303 {
304     FUNCC(h264_loop_filter_chroma_intra)(pix, stride, sizeof(pixel), 2, alpha, beta);
305 }
306 static void FUNCC(h264_h_loop_filter_chroma_intra)(uint8_t *pix, int stride, int alpha, int beta)
307 {
308     FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 2, alpha, beta);
309 }
310 static void FUNCC(h264_h_loop_filter_chroma_mbaff_intra)(uint8_t *pix, int stride, int alpha, int beta)
311 {
312     FUNCC(h264_loop_filter_chroma_intra)(pix, sizeof(pixel), stride, 1, alpha, beta);
313 }