]> git.sesse.net Git - ffmpeg/blob - libavcodec/msmpeg4.c
avcodec/msmpeg4: Make ff_msmpeg4_common_init() thread-safe
[ffmpeg] / libavcodec / msmpeg4.c
1 /*
2  * MSMPEG4 backend for encoder and decoder
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 /**
26  * @file
27  * MSMPEG4 backend for encoder and decoder
28  */
29
30 #include "libavutil/thread.h"
31
32 #include "avcodec.h"
33 #include "idctdsp.h"
34 #include "mpegvideo.h"
35 #include "msmpeg4.h"
36 #include "libavutil/x86/asm.h"
37 #include "h263.h"
38 #include "mpeg4video.h"
39 #include "msmpeg4data.h"
40 #include "mpegvideodata.h"
41 #include "vc1data.h"
42 #include "libavutil/imgutils.h"
43
44 /*
45  * You can also call this codec: MPEG-4 with a twist!
46  *
47  * TODO:
48  *        - (encoding) select best mv table (two choices)
49  *        - (encoding) select best vlc/dc table
50  */
51
52 /* This table is practically identical to the one from H.263
53  * except that it is inverted. */
54 static av_cold void init_h263_dc_for_msmpeg4(void)
55 {
56         int level, uni_code, uni_len;
57
58         for(level=-256; level<256; level++){
59             int size, v, l;
60             /* find number of bits */
61             size = 0;
62             v = abs(level);
63             while (v) {
64                 v >>= 1;
65                     size++;
66             }
67
68             if (level < 0)
69                 l= (-level) ^ ((1 << size) - 1);
70             else
71                 l= level;
72
73             /* luminance H.263 */
74             uni_code= ff_mpeg4_DCtab_lum[size][0];
75             uni_len = ff_mpeg4_DCtab_lum[size][1];
76             uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
77
78             if (size > 0) {
79                 uni_code<<=size; uni_code|=l;
80                 uni_len+=size;
81                 if (size > 8){
82                     uni_code<<=1; uni_code|=1;
83                     uni_len++;
84                 }
85             }
86             ff_v2_dc_lum_table[level + 256][0] = uni_code;
87             ff_v2_dc_lum_table[level + 256][1] = uni_len;
88
89             /* chrominance H.263 */
90             uni_code= ff_mpeg4_DCtab_chrom[size][0];
91             uni_len = ff_mpeg4_DCtab_chrom[size][1];
92             uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
93
94             if (size > 0) {
95                 uni_code<<=size; uni_code|=l;
96                 uni_len+=size;
97                 if (size > 8){
98                     uni_code<<=1; uni_code|=1;
99                     uni_len++;
100                 }
101             }
102             ff_v2_dc_chroma_table[level + 256][0] = uni_code;
103             ff_v2_dc_chroma_table[level + 256][1] = uni_len;
104
105         }
106 }
107
108 av_cold void ff_msmpeg4_common_init(MpegEncContext *s)
109 {
110     static AVOnce init_static_once = AV_ONCE_INIT;
111
112     switch(s->msmpeg4_version){
113     case 1:
114     case 2:
115         s->y_dc_scale_table=
116         s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
117         break;
118     case 3:
119         if(s->workaround_bugs){
120             s->y_dc_scale_table= ff_old_ff_y_dc_scale_table;
121             s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
122         } else{
123             s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
124             s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
125         }
126         break;
127     case 4:
128     case 5:
129         s->y_dc_scale_table= ff_wmv1_y_dc_scale_table;
130         s->c_dc_scale_table= ff_wmv1_c_dc_scale_table;
131         break;
132 #if CONFIG_VC1_DECODER
133     case 6:
134         s->y_dc_scale_table= ff_wmv3_dc_scale_table;
135         s->c_dc_scale_table= ff_wmv3_dc_scale_table;
136         break;
137 #endif
138
139     }
140
141
142     if(s->msmpeg4_version>=4){
143         ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable,   ff_wmv1_scantable[1]);
144         ff_init_scantable(s->idsp.idct_permutation, &s->intra_h_scantable, ff_wmv1_scantable[2]);
145         ff_init_scantable(s->idsp.idct_permutation, &s->intra_v_scantable, ff_wmv1_scantable[3]);
146         ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable,   ff_wmv1_scantable[0]);
147     }
148     //Note the default tables are set in common_init in mpegvideo.c
149
150     ff_thread_once(&init_static_once, init_h263_dc_for_msmpeg4);
151 }
152
153 /* predict coded block */
154 int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
155 {
156     int xy, wrap, pred, a, b, c;
157
158     xy = s->block_index[n];
159     wrap = s->b8_stride;
160
161     /* B C
162      * A X
163      */
164     a = s->coded_block[xy - 1       ];
165     b = s->coded_block[xy - 1 - wrap];
166     c = s->coded_block[xy     - wrap];
167
168     if (b == c) {
169         pred = a;
170     } else {
171         pred = c;
172     }
173
174     /* store value */
175     *coded_block_ptr = &s->coded_block[xy];
176
177     return pred;
178 }
179
180 static int get_dc(uint8_t *src, int stride, int scale, int block_size)
181 {
182     int y;
183     int sum=0;
184     for(y=0; y<block_size; y++){
185         int x;
186         for(x=0; x<block_size; x++){
187             sum+=src[x + y*stride];
188         }
189     }
190     return FASTDIV((sum + (scale>>1)), scale);
191 }
192
193 /* dir = 0: left, dir = 1: top prediction */
194 int ff_msmpeg4_pred_dc(MpegEncContext *s, int n,
195                        int16_t **dc_val_ptr, int *dir_ptr)
196 {
197     int a, b, c, wrap, pred, scale;
198     int16_t *dc_val;
199
200     /* find prediction */
201     if (n < 4) {
202         scale = s->y_dc_scale;
203     } else {
204         scale = s->c_dc_scale;
205     }
206
207     wrap = s->block_wrap[n];
208     dc_val= s->dc_val[0] + s->block_index[n];
209
210     /* B C
211      * A X
212      */
213     a = dc_val[ - 1];
214     b = dc_val[ - 1 - wrap];
215     c = dc_val[ - wrap];
216
217     if(s->first_slice_line && (n&2)==0 && s->msmpeg4_version<4){
218         b=c=1024;
219     }
220
221     /* XXX: the following solution consumes divisions, but it does not
222        necessitate to modify mpegvideo.c. The problem comes from the
223        fact they decided to store the quantized DC (which would lead
224        to problems if Q could vary !) */
225 #if ARCH_X86 && HAVE_7REGS && HAVE_EBX_AVAILABLE
226     __asm__ volatile(
227         "movl %3, %%eax         \n\t"
228         "shrl $1, %%eax         \n\t"
229         "addl %%eax, %2         \n\t"
230         "addl %%eax, %1         \n\t"
231         "addl %0, %%eax         \n\t"
232         "imull %4               \n\t"
233         "movl %%edx, %0         \n\t"
234         "movl %1, %%eax         \n\t"
235         "imull %4               \n\t"
236         "movl %%edx, %1         \n\t"
237         "movl %2, %%eax         \n\t"
238         "imull %4               \n\t"
239         "movl %%edx, %2         \n\t"
240         : "+b" (a), "+c" (b), "+D" (c)
241         : "g" (scale), "S" (ff_inverse[scale])
242         : "%eax", "%edx"
243     );
244 #else
245     /* Divisions are costly everywhere; optimize the most common case. */
246     if (scale == 8) {
247         a = (a + (8 >> 1)) / 8;
248         b = (b + (8 >> 1)) / 8;
249         c = (c + (8 >> 1)) / 8;
250     } else {
251         a = FASTDIV((a + (scale >> 1)), scale);
252         b = FASTDIV((b + (scale >> 1)), scale);
253         c = FASTDIV((c + (scale >> 1)), scale);
254     }
255 #endif
256     /* XXX: WARNING: they did not choose the same test as MPEG-4. This
257        is very important ! */
258     if(s->msmpeg4_version>3){
259         if(s->inter_intra_pred){
260             uint8_t *dest;
261             int wrap;
262
263             if(n==1){
264                 pred=a;
265                 *dir_ptr = 0;
266             }else if(n==2){
267                 pred=c;
268                 *dir_ptr = 1;
269             }else if(n==3){
270                 if (abs(a - b) < abs(b - c)) {
271                     pred = c;
272                     *dir_ptr = 1;
273                 } else {
274                     pred = a;
275                     *dir_ptr = 0;
276                 }
277             }else{
278                 int bs = 8 >> s->avctx->lowres;
279                 if(n<4){
280                     wrap= s->linesize;
281                     dest= s->current_picture.f->data[0] + (((n >> 1) + 2*s->mb_y) * bs*  wrap ) + ((n & 1) + 2*s->mb_x) * bs;
282                 }else{
283                     wrap= s->uvlinesize;
284                     dest= s->current_picture.f->data[n - 3] + (s->mb_y * bs * wrap) + s->mb_x * bs;
285                 }
286                 if(s->mb_x==0) a= (1024 + (scale>>1))/scale;
287                 else           a= get_dc(dest-bs, wrap, scale*8>>(2*s->avctx->lowres), bs);
288                 if(s->mb_y==0) c= (1024 + (scale>>1))/scale;
289                 else           c= get_dc(dest-bs*wrap, wrap, scale*8>>(2*s->avctx->lowres), bs);
290
291                 if (s->h263_aic_dir==0) {
292                     pred= a;
293                     *dir_ptr = 0;
294                 }else if (s->h263_aic_dir==1) {
295                     if(n==0){
296                         pred= c;
297                         *dir_ptr = 1;
298                     }else{
299                         pred= a;
300                         *dir_ptr = 0;
301                     }
302                 }else if (s->h263_aic_dir==2) {
303                     if(n==0){
304                         pred= a;
305                         *dir_ptr = 0;
306                     }else{
307                         pred= c;
308                         *dir_ptr = 1;
309                     }
310                 } else {
311                     pred= c;
312                     *dir_ptr = 1;
313                 }
314             }
315         }else{
316             if (abs(a - b) < abs(b - c)) {
317                 pred = c;
318                 *dir_ptr = 1;
319             } else {
320                 pred = a;
321                 *dir_ptr = 0;
322             }
323         }
324     }else{
325         if (abs(a - b) <= abs(b - c)) {
326             pred = c;
327             *dir_ptr = 1;
328         } else {
329             pred = a;
330             *dir_ptr = 0;
331         }
332     }
333
334     /* update predictor */
335     *dc_val_ptr = &dc_val[0];
336     return pred;
337 }
338