Mercurial > libavcodec.hg
annotate mpegvideo.c @ 977:3c2b94407f02 libavcodec
suppressed redundant math.h - suppressed M_PI redefinition
author | bellard |
---|---|
date | Tue, 07 Jan 2003 22:43:16 +0000 |
parents | 45bd748e305f |
children | 6129c88a6393 |
rev | line source |
---|---|
0 | 1 /* |
2 * The simplest mpeg encoder (well, it was the simplest!) | |
429 | 3 * Copyright (c) 2000,2001 Fabrice Bellard. |
0 | 4 * |
429 | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
0 | 9 * |
429 | 10 * This library is distributed in the hope that it will be useful, |
0 | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
429 | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 * Lesser General Public License for more details. | |
0 | 14 * |
429 | 15 * You should have received a copy of the GNU Lesser General Public |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
295 | 18 * |
325 | 19 * 4MV & hq & b-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at> |
0 | 20 */ |
701 | 21 |
22 #include <ctype.h> | |
0 | 23 #include "avcodec.h" |
24 #include "dsputil.h" | |
25 #include "mpegvideo.h" | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
26 #include "simple_idct.h" |
0 | 27 |
17
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
28 #ifdef USE_FASTMEMCPY |
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
29 #include "fastmemcpy.h" |
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
30 #endif |
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
31 |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
32 //#undef NDEBUG |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
33 //#include <assert.h> |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
34 |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
35 static void encode_picture(MpegEncContext *s, int picture_number); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
36 static void dct_unquantize_mpeg1_c(MpegEncContext *s, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
37 DCTELEM *block, int n, int qscale); |
325 | 38 static void dct_unquantize_mpeg2_c(MpegEncContext *s, |
39 DCTELEM *block, int n, int qscale); | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
40 static void dct_unquantize_h263_c(MpegEncContext *s, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
41 DCTELEM *block, int n, int qscale); |
206 | 42 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w); |
344 | 43 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); |
945 | 44 static int dct_quantize_trellis_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); |
206 | 45 |
46 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c; | |
47 | |
0 | 48 |
49 /* enable all paranoid tests for rounding, overflows, etc... */ | |
50 //#define PARANOID | |
51 | |
52 //#define DEBUG | |
53 | |
321 | 54 |
0 | 55 /* for jpeg fast DCT */ |
56 #define CONST_BITS 14 | |
57 | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
58 static const uint16_t aanscales[64] = { |
0 | 59 /* precomputed values scaled up by 14 bits */ |
60 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
61 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, | |
62 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, | |
63 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, | |
64 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
65 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, | |
936 | 66 8867 , 12299, 11585, 10426, 8867, 6967, 4799, 2446, |
67 4520 , 6270, 5906, 5315, 4520, 3552, 2446, 1247 | |
0 | 68 }; |
69 | |
768
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
70 /* Input permutation for the simple_idct_mmx */ |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
71 static const uint8_t simple_mmx_permutation[64]={ |
768
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
72 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D, |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
73 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D, |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
74 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D, |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
75 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F, |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
76 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F, |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
77 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D, |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
78 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F, |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
79 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F, |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
80 }; |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
81 |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
82 static const uint8_t h263_chroma_roundtab[16] = { |
0 | 83 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, |
84 }; | |
85 | |
815
78accc54493b
put a few large tables under #ifdef CONFIG_ENCODERS or dynamically allocate them
michaelni
parents:
814
diff
changeset
|
86 static UINT16 (*default_mv_penalty)[MAX_MV*2+1]=NULL; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
87 static UINT8 default_fcode_tab[MAX_MV*2+1]; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
88 |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
89 static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64], |
709
afeff6ccb7f5
convert only needed matrixes in convert_matrix() (mjpeg calls it for every frame)
michaelni
parents:
706
diff
changeset
|
90 const UINT16 *quant_matrix, int bias, int qmin, int qmax) |
0 | 91 { |
344 | 92 int qscale; |
0 | 93 |
709
afeff6ccb7f5
convert only needed matrixes in convert_matrix() (mjpeg calls it for every frame)
michaelni
parents:
706
diff
changeset
|
94 for(qscale=qmin; qscale<=qmax; qscale++){ |
344 | 95 int i; |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
96 if (s->fdct == ff_jpeg_fdct_islow) { |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
97 for(i=0;i<64;i++) { |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
98 const int j= s->idct_permutation[i]; |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
99 /* 16 <= qscale * quant_matrix[i] <= 7905 */ |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
100 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
101 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
102 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
103 |
764 | 104 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
105 (qscale * quant_matrix[j])); |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
106 } |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
107 } else if (s->fdct == fdct_ifast) { |
344 | 108 for(i=0;i<64;i++) { |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
109 const int j= s->idct_permutation[i]; |
344 | 110 /* 16 <= qscale * quant_matrix[i] <= 7905 */ |
111 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ | |
112 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ | |
113 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ | |
114 | |
764 | 115 qmat[qscale][i] = (int)((UINT64_C(1) << (QMAT_SHIFT + 14)) / |
344 | 116 (aanscales[i] * qscale * quant_matrix[j])); |
117 } | |
118 } else { | |
119 for(i=0;i<64;i++) { | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
120 const int j= s->idct_permutation[i]; |
344 | 121 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 |
122 So 16 <= qscale * quant_matrix[i] <= 7905 | |
123 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905 | |
124 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 | |
125 */ | |
945 | 126 qmat[qscale][i] = (int)((UINT64_C(1) << QMAT_SHIFT) / (qscale * quant_matrix[j])); |
127 // qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
128 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[j]); |
344 | 129 |
130 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1; | |
131 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]); | |
132 } | |
0 | 133 } |
134 } | |
135 } | |
456 | 136 // move into common.c perhaps |
137 #define CHECKED_ALLOCZ(p, size)\ | |
138 {\ | |
139 p= av_mallocz(size);\ | |
140 if(p==NULL){\ | |
141 perror("malloc");\ | |
142 goto fail;\ | |
143 }\ | |
144 } | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
145 |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
146 void ff_init_scantable(MpegEncContext *s, ScanTable *st, const UINT8 *src_scantable){ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
147 int i; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
148 int end; |
764 | 149 |
150 st->scantable= src_scantable; | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
151 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
152 for(i=0; i<64; i++){ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
153 int j; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
154 j = src_scantable[i]; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
155 st->permutated[i] = s->idct_permutation[j]; |
828
ace3ccd18dd2
Altivec Patch (Mark III) by (Dieter Shirley <dieters at schemasoft dot com>)
michaelni
parents:
815
diff
changeset
|
156 #ifdef ARCH_POWERPC |
ace3ccd18dd2
Altivec Patch (Mark III) by (Dieter Shirley <dieters at schemasoft dot com>)
michaelni
parents:
815
diff
changeset
|
157 st->inverse[j] = i; |
ace3ccd18dd2
Altivec Patch (Mark III) by (Dieter Shirley <dieters at schemasoft dot com>)
michaelni
parents:
815
diff
changeset
|
158 #endif |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
159 } |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
160 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
161 end=-1; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
162 for(i=0; i<64; i++){ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
163 int j; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
164 j = st->permutated[i]; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
165 if(j>end) end=j; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
166 st->raster_end[i]= end; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
167 } |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
168 } |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
169 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
170 /* XXX: those functions should be suppressed ASAP when all IDCTs are |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
171 converted */ |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
172 // *FIXME* this is ugly hack using local static |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
173 static void (*ff_put_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size); |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
174 static void (*ff_add_pixels_clamped)(const DCTELEM *block, UINT8 *pixels, int line_size); |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
175 static void ff_jref_idct_put(UINT8 *dest, int line_size, DCTELEM *block) |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
176 { |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
177 j_rev_dct (block); |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
178 ff_put_pixels_clamped(block, dest, line_size); |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
179 } |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
180 static void ff_jref_idct_add(UINT8 *dest, int line_size, DCTELEM *block) |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
181 { |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
182 j_rev_dct (block); |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
183 ff_add_pixels_clamped(block, dest, line_size); |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
184 } |
0 | 185 |
726
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
186 /* init common dct for both encoder and decoder */ |
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
187 int DCT_common_init(MpegEncContext *s) |
0 | 188 { |
726
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
189 int i; |
0 | 190 |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
191 ff_put_pixels_clamped = s->dsp.put_pixels_clamped; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
192 ff_add_pixels_clamped = s->dsp.add_pixels_clamped; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
193 |
312 | 194 s->dct_unquantize_h263 = dct_unquantize_h263_c; |
325 | 195 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c; |
196 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c; | |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
197 s->dct_quantize= dct_quantize_c; |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
198 |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
199 if(s->avctx->dct_algo==FF_DCT_FASTINT) |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
200 s->fdct = fdct_ifast; |
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
201 else |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
202 s->fdct = ff_jpeg_fdct_islow; //slow/accurate/default |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
203 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
204 if(s->avctx->idct_algo==FF_IDCT_INT){ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
205 s->idct_put= ff_jref_idct_put; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
206 s->idct_add= ff_jref_idct_add; |
768
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
207 s->idct_permutation_type= FF_LIBMPEG2_IDCT_PERM; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
208 }else{ //accurate/default |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
209 s->idct_put= simple_idct_put; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
210 s->idct_add= simple_idct_add; |
768
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
211 s->idct_permutation_type= FF_NO_IDCT_PERM; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
212 } |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
213 |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
214 #ifdef HAVE_MMX |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
215 MPV_common_init_mmx(s); |
8 | 216 #endif |
514
c9f724e3a797
Update and activate dct_unquantize_h263_mvi. Thanks to M«©ns Rullg«©rd
mellum
parents:
506
diff
changeset
|
217 #ifdef ARCH_ALPHA |
c9f724e3a797
Update and activate dct_unquantize_h263_mvi. Thanks to M«©ns Rullg«©rd
mellum
parents:
506
diff
changeset
|
218 MPV_common_init_axp(s); |
c9f724e3a797
Update and activate dct_unquantize_h263_mvi. Thanks to M«©ns Rullg«©rd
mellum
parents:
506
diff
changeset
|
219 #endif |
628
f596db4aa871
sun solaris compilation bugfix, patch by (Martin Olschewski <olschewski at zpr dot uni-koeln dot de>)
michaelni
parents:
625
diff
changeset
|
220 #ifdef HAVE_MLIB |
f596db4aa871
sun solaris compilation bugfix, patch by (Martin Olschewski <olschewski at zpr dot uni-koeln dot de>)
michaelni
parents:
625
diff
changeset
|
221 MPV_common_init_mlib(s); |
f596db4aa871
sun solaris compilation bugfix, patch by (Martin Olschewski <olschewski at zpr dot uni-koeln dot de>)
michaelni
parents:
625
diff
changeset
|
222 #endif |
721
71f669e9f633
ps2 optimizations update patch by (Leon van Stuivenberg <leonvs at iae dot nl>)
michaelni
parents:
717
diff
changeset
|
223 #ifdef HAVE_MMI |
71f669e9f633
ps2 optimizations update patch by (Leon van Stuivenberg <leonvs at iae dot nl>)
michaelni
parents:
717
diff
changeset
|
224 MPV_common_init_mmi(s); |
71f669e9f633
ps2 optimizations update patch by (Leon van Stuivenberg <leonvs at iae dot nl>)
michaelni
parents:
717
diff
changeset
|
225 #endif |
730 | 226 #ifdef ARCH_ARMV4L |
874 | 227 MPV_common_init_armv4l(s); |
730 | 228 #endif |
828
ace3ccd18dd2
Altivec Patch (Mark III) by (Dieter Shirley <dieters at schemasoft dot com>)
michaelni
parents:
815
diff
changeset
|
229 #ifdef ARCH_POWERPC |
ace3ccd18dd2
Altivec Patch (Mark III) by (Dieter Shirley <dieters at schemasoft dot com>)
michaelni
parents:
815
diff
changeset
|
230 MPV_common_init_ppc(s); |
ace3ccd18dd2
Altivec Patch (Mark III) by (Dieter Shirley <dieters at schemasoft dot com>)
michaelni
parents:
815
diff
changeset
|
231 #endif |
730 | 232 |
945 | 233 if(s->flags&CODEC_FLAG_TRELLIS_QUANT){ |
234 s->dct_quantize= dct_quantize_trellis_c; //move before MPV_common_init_* | |
235 } | |
236 | |
768
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
237 switch(s->idct_permutation_type){ |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
238 case FF_NO_IDCT_PERM: |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
239 for(i=0; i<64; i++) |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
240 s->idct_permutation[i]= i; |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
241 break; |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
242 case FF_LIBMPEG2_IDCT_PERM: |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
243 for(i=0; i<64; i++) |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
244 s->idct_permutation[i]= (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2); |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
245 break; |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
246 case FF_SIMPLE_IDCT_PERM: |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
247 for(i=0; i<64; i++) |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
248 s->idct_permutation[i]= simple_mmx_permutation[i]; |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
249 break; |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
250 case FF_TRANSPOSE_IDCT_PERM: |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
251 for(i=0; i<64; i++) |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
252 s->idct_permutation[i]= ((i&7)<<3) | (i>>3); |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
253 break; |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
254 default: |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
255 fprintf(stderr, "Internal error, IDCT permutation not set\n"); |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
256 return -1; |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
257 } |
c3fc09466f92
idct_permutation_type variable, so the permutation type can quickly be identified
michaelni
parents:
767
diff
changeset
|
258 |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
259 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
260 /* load & permutate scantables |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
261 note: only wmv uses differnt ones |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
262 */ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
263 ff_init_scantable(s, &s->inter_scantable , ff_zigzag_direct); |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
264 ff_init_scantable(s, &s->intra_scantable , ff_zigzag_direct); |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
265 ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan); |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
266 ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan); |
591 | 267 |
726
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
268 return 0; |
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
269 } |
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
270 |
903 | 271 /** |
924 | 272 * allocates a Picture |
273 * The pixels are allocated/set by calling get_buffer() if shared=0 | |
903 | 274 */ |
924 | 275 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared){ |
276 | |
277 if(shared){ | |
278 assert(pic->data[0]); | |
279 assert(pic->type == 0 || pic->type == FF_BUFFER_TYPE_SHARED); | |
280 pic->type= FF_BUFFER_TYPE_SHARED; | |
281 }else{ | |
282 int r; | |
283 | |
284 assert(!pic->data[0]); | |
285 | |
925 | 286 r= s->avctx->get_buffer(s->avctx, (AVFrame*)pic); |
924 | 287 |
288 if(r<0 || !pic->age || !pic->type || !pic->data[0]){ | |
289 fprintf(stderr, "get_buffer() failed (%d %d %d %X)\n", r, pic->age, pic->type, (int)pic->data[0]); | |
290 return -1; | |
291 } | |
292 | |
293 if(s->linesize && (s->linesize != pic->linesize[0] || s->uvlinesize != pic->linesize[1])){ | |
294 fprintf(stderr, "get_buffer() failed (stride changed)\n"); | |
295 return -1; | |
296 } | |
297 | |
298 if(pic->linesize[1] != pic->linesize[2]){ | |
299 fprintf(stderr, "get_buffer() failed (uv stride missmatch)\n"); | |
300 return -1; | |
301 } | |
302 | |
303 s->linesize = pic->linesize[0]; | |
304 s->uvlinesize= pic->linesize[1]; | |
903 | 305 } |
924 | 306 |
307 if(pic->qscale_table==NULL){ | |
308 if (s->encoding) { | |
309 CHECKED_ALLOCZ(pic->mb_var , s->mb_num * sizeof(INT16)) | |
310 CHECKED_ALLOCZ(pic->mc_mb_var, s->mb_num * sizeof(INT16)) | |
311 CHECKED_ALLOCZ(pic->mb_mean , s->mb_num * sizeof(INT8)) | |
312 } | |
313 | |
314 CHECKED_ALLOCZ(pic->mbskip_table , s->mb_num * sizeof(UINT8)+1) //the +1 is for the slice end check | |
315 CHECKED_ALLOCZ(pic->qscale_table , s->mb_num * sizeof(UINT8)) | |
316 pic->qstride= s->mb_width; | |
317 } | |
903 | 318 |
319 return 0; | |
320 fail: //for the CHECKED_ALLOCZ macro | |
321 return -1; | |
322 } | |
323 | |
924 | 324 /** |
325 * deallocates a picture | |
326 */ | |
903 | 327 static void free_picture(MpegEncContext *s, Picture *pic){ |
328 int i; | |
924 | 329 |
330 if(pic->data[0] && pic->type!=FF_BUFFER_TYPE_SHARED){ | |
925 | 331 s->avctx->release_buffer(s->avctx, (AVFrame*)pic); |
924 | 332 } |
333 | |
903 | 334 av_freep(&pic->mb_var); |
335 av_freep(&pic->mc_mb_var); | |
336 av_freep(&pic->mb_mean); | |
337 av_freep(&pic->mbskip_table); | |
338 av_freep(&pic->qscale_table); | |
339 | |
924 | 340 if(pic->type == FF_BUFFER_TYPE_INTERNAL){ |
903 | 341 for(i=0; i<4; i++){ |
342 av_freep(&pic->base[i]); | |
343 pic->data[i]= NULL; | |
344 } | |
345 av_freep(&pic->opaque); | |
924 | 346 pic->type= 0; |
347 }else if(pic->type == FF_BUFFER_TYPE_SHARED){ | |
348 for(i=0; i<4; i++){ | |
349 pic->base[i]= | |
350 pic->data[i]= NULL; | |
351 } | |
352 pic->type= 0; | |
903 | 353 } |
354 } | |
355 | |
726
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
356 /* init common structure for both encoder and decoder */ |
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
357 int MPV_common_init(MpegEncContext *s) |
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
358 { |
756 | 359 int y_size, c_size, yc_size, i; |
726
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
360 |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
361 dsputil_init(&s->dsp, s->avctx->dsp_mask); |
726
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
362 DCT_common_init(s); |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
363 |
754 | 364 s->flags= s->avctx->flags; |
726
a91203b34e71
moved dct init out from mpv_common_init to dct_common_init (for less-uglier way for dv)
al3x
parents:
721
diff
changeset
|
365 |
903 | 366 s->mb_width = (s->width + 15) / 16; |
0 | 367 s->mb_height = (s->height + 15) / 16; |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
368 |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
369 /* set default edge pos, will be overriden in decode_header if needed */ |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
370 s->h_edge_pos= s->mb_width*16; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
371 s->v_edge_pos= s->mb_height*16; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
372 |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
373 s->mb_num = s->mb_width * s->mb_height; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
374 |
756 | 375 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); |
376 c_size = (s->mb_width + 2) * (s->mb_height + 2); | |
377 yc_size = y_size + 2 * c_size; | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
378 |
701 | 379 /* convert fourcc to upper case */ |
380 s->avctx->fourcc= toupper( s->avctx->fourcc &0xFF) | |
381 + (toupper((s->avctx->fourcc>>8 )&0xFF)<<8 ) | |
382 + (toupper((s->avctx->fourcc>>16)&0xFF)<<16) | |
383 + (toupper((s->avctx->fourcc>>24)&0xFF)<<24); | |
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
384 |
956
45bd748e305f
negative linesize support (so mplayer -flip works)
michaelni
parents:
955
diff
changeset
|
385 CHECKED_ALLOCZ(s->allocated_edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance |
45bd748e305f
negative linesize support (so mplayer -flip works)
michaelni
parents:
955
diff
changeset
|
386 s->edge_emu_buffer= s->allocated_edge_emu_buffer + (s->width+64)*2*17; |
903 | 387 |
925 | 388 s->avctx->coded_frame= (AVFrame*)&s->current_picture; |
903 | 389 |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
390 if (s->encoding) { |
324 | 391 int mv_table_size= (s->mb_width+2)*(s->mb_height+2); |
392 | |
393 /* Allocate MV tables */ | |
456 | 394 CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(INT16)) |
395 CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
396 CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
397 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
398 CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
399 CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
324 | 400 |
903 | 401 //FIXME should be linesize instead of s->width*2 but that isnt known before get_buffer() |
936 | 402 CHECKED_ALLOCZ(s->me.scratchpad, s->width*2*16*3*sizeof(uint8_t)) |
456 | 403 |
936 | 404 CHECKED_ALLOCZ(s->me.map , ME_MAP_SIZE*sizeof(uint32_t)) |
405 CHECKED_ALLOCZ(s->me.score_map, ME_MAP_SIZE*sizeof(uint32_t)) | |
327 | 406 |
456 | 407 if(s->codec_id==CODEC_ID_MPEG4){ |
408 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE); | |
409 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE); | |
410 } | |
612 | 411 |
650
ef4a33aad86e
reducing sizeof MpegEncContext to avoid stack overflow on crap M$ windo$
michaelni
parents:
635
diff
changeset
|
412 if(s->msmpeg4_version){ |
ef4a33aad86e
reducing sizeof MpegEncContext to avoid stack overflow on crap M$ windo$
michaelni
parents:
635
diff
changeset
|
413 CHECKED_ALLOCZ(s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int)); |
ef4a33aad86e
reducing sizeof MpegEncContext to avoid stack overflow on crap M$ windo$
michaelni
parents:
635
diff
changeset
|
414 } |
612 | 415 CHECKED_ALLOCZ(s->avctx->stats_out, 256); |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
416 } |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
417 |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
418 CHECKED_ALLOCZ(s->error_status_table, s->mb_num*sizeof(UINT8)) |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
419 |
288 | 420 if (s->out_format == FMT_H263 || s->encoding) { |
0 | 421 int size; |
456 | 422 /* Allocate MB type table */ |
423 CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8)) | |
424 | |
0 | 425 /* MV prediction */ |
426 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
456 | 427 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16)); |
664 | 428 } |
429 | |
430 if(s->codec_id==CODEC_ID_MPEG4){ | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
431 /* interlaced direct mode decoding tables */ |
664 | 432 CHECKED_ALLOCZ(s->field_mv_table, s->mb_num*2*2 * sizeof(INT16)) |
433 CHECKED_ALLOCZ(s->field_select_table, s->mb_num*2* sizeof(INT8)) | |
0 | 434 } |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
435 /* 4mv b frame decoding table */ |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
436 //note this is needed for h263 without b frames too (segfault on damaged streams otherwise) |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
437 CHECKED_ALLOCZ(s->co_located_type_table, s->mb_num * sizeof(UINT8)) |
767 | 438 if (s->out_format == FMT_H263) { |
0 | 439 /* ac values */ |
756 | 440 CHECKED_ALLOCZ(s->ac_val[0], yc_size * sizeof(INT16) * 16); |
0 | 441 s->ac_val[1] = s->ac_val[0] + y_size; |
442 s->ac_val[2] = s->ac_val[1] + c_size; | |
443 | |
444 /* cbp values */ | |
456 | 445 CHECKED_ALLOCZ(s->coded_block, y_size); |
333 | 446 |
447 /* divx501 bitstream reorder buffer */ | |
456 | 448 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE); |
843 | 449 |
456 | 450 /* cbp, ac_pred, pred_dir */ |
451 CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(UINT8)) | |
452 CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8)) | |
197
21abf1b20016
different fix, s->mbintra_table used only if h263_pred set. - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
196
diff
changeset
|
453 } |
756 | 454 |
455 if (s->h263_pred || s->h263_plus || !s->encoding) { | |
456 /* dc values */ | |
457 //MN: we need these for error resilience of intra-frames | |
458 CHECKED_ALLOCZ(s->dc_val[0], yc_size * sizeof(INT16)); | |
459 s->dc_val[1] = s->dc_val[0] + y_size; | |
460 s->dc_val[2] = s->dc_val[1] + c_size; | |
461 for(i=0;i<yc_size;i++) | |
462 s->dc_val[0][i] = 1024; | |
463 } | |
464 | |
611
3214d3f4519e
error concealment needs the mbintra_table so it should allways be allocated
michaelni
parents:
608
diff
changeset
|
465 /* which mb is a intra block */ |
3214d3f4519e
error concealment needs the mbintra_table so it should allways be allocated
michaelni
parents:
608
diff
changeset
|
466 CHECKED_ALLOCZ(s->mbintra_table, s->mb_num); |
3214d3f4519e
error concealment needs the mbintra_table so it should allways be allocated
michaelni
parents:
608
diff
changeset
|
467 memset(s->mbintra_table, 1, s->mb_num); |
3214d3f4519e
error concealment needs the mbintra_table so it should allways be allocated
michaelni
parents:
608
diff
changeset
|
468 |
0 | 469 /* default structure is frame */ |
470 s->picture_structure = PICT_FRAME; | |
553 | 471 |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
472 /* init macroblock skip table */ |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
473 CHECKED_ALLOCZ(s->mbskip_table, s->mb_num+1); |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
474 //Note the +1 is for a quicker mpeg4 slice_end detection |
294 | 475 |
327 | 476 s->block= s->blocks[0]; |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
477 |
842
e460775adb38
cleanup (breaks compatibility, requested by fabrice)
michaelni
parents:
828
diff
changeset
|
478 s->parse_context.state= -1; |
e460775adb38
cleanup (breaks compatibility, requested by fabrice)
michaelni
parents:
828
diff
changeset
|
479 |
0 | 480 s->context_initialized = 1; |
481 return 0; | |
482 fail: | |
244 | 483 MPV_common_end(s); |
484 return -1; | |
485 } | |
486 | |
456 | 487 |
488 //extern int sads; | |
489 | |
244 | 490 /* init common structure for both encoder and decoder */ |
491 void MPV_common_end(MpegEncContext *s) | |
492 { | |
493 int i; | |
494 | |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
495 av_freep(&s->mb_type); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
496 av_freep(&s->p_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
497 av_freep(&s->b_forw_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
498 av_freep(&s->b_back_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
499 av_freep(&s->b_bidir_forw_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
500 av_freep(&s->b_bidir_back_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
501 av_freep(&s->b_direct_mv_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
502 av_freep(&s->motion_val); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
503 av_freep(&s->dc_val[0]); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
504 av_freep(&s->ac_val[0]); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
505 av_freep(&s->coded_block); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
506 av_freep(&s->mbintra_table); |
456 | 507 av_freep(&s->cbp_table); |
508 av_freep(&s->pred_dir_table); | |
936 | 509 av_freep(&s->me.scratchpad); |
510 av_freep(&s->me.map); | |
511 av_freep(&s->me.score_map); | |
456 | 512 |
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
513 av_freep(&s->mbskip_table); |
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
514 av_freep(&s->bitstream_buffer); |
456 | 515 av_freep(&s->tex_pb_buffer); |
516 av_freep(&s->pb2_buffer); | |
956
45bd748e305f
negative linesize support (so mplayer -flip works)
michaelni
parents:
955
diff
changeset
|
517 av_freep(&s->allocated_edge_emu_buffer); s->edge_emu_buffer= NULL; |
664 | 518 av_freep(&s->co_located_type_table); |
519 av_freep(&s->field_mv_table); | |
520 av_freep(&s->field_select_table); | |
612 | 521 av_freep(&s->avctx->stats_out); |
650
ef4a33aad86e
reducing sizeof MpegEncContext to avoid stack overflow on crap M$ windo$
michaelni
parents:
635
diff
changeset
|
522 av_freep(&s->ac_stats); |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
523 av_freep(&s->error_status_table); |
903 | 524 |
525 for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
526 free_picture(s, &s->picture[i]); | |
0 | 527 } |
528 s->context_initialized = 0; | |
529 } | |
530 | |
531 /* init video encoder */ | |
532 int MPV_encode_init(AVCodecContext *avctx) | |
533 { | |
534 MpegEncContext *s = avctx->priv_data; | |
60 | 535 int i; |
0 | 536 |
315 | 537 avctx->pix_fmt = PIX_FMT_YUV420P; |
538 | |
0 | 539 s->bit_rate = avctx->bit_rate; |
268 | 540 s->bit_rate_tolerance = avctx->bit_rate_tolerance; |
0 | 541 s->frame_rate = avctx->frame_rate; |
542 s->width = avctx->width; | |
543 s->height = avctx->height; | |
456 | 544 if(avctx->gop_size > 600){ |
462
3fdd0627f6bd
typo (found by Bohdan Horst <nexus at hoth.amu.edu.pl>)
michaelni
parents:
461
diff
changeset
|
545 fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n"); |
456 | 546 avctx->gop_size=600; |
547 } | |
0 | 548 s->gop_size = avctx->gop_size; |
162 | 549 s->rtp_mode = avctx->rtp_mode; |
550 s->rtp_payload_size = avctx->rtp_payload_size; | |
231 | 551 if (avctx->rtp_callback) |
552 s->rtp_callback = avctx->rtp_callback; | |
268 | 553 s->qmin= avctx->qmin; |
554 s->qmax= avctx->qmax; | |
555 s->max_qdiff= avctx->max_qdiff; | |
556 s->qcompress= avctx->qcompress; | |
557 s->qblur= avctx->qblur; | |
194
27d1773552c9
mpeg4 encoder fix by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
191
diff
changeset
|
558 s->avctx = avctx; |
294 | 559 s->flags= avctx->flags; |
324 | 560 s->max_b_frames= avctx->max_b_frames; |
329 | 561 s->b_frame_strategy= avctx->b_frame_strategy; |
344 | 562 s->codec_id= avctx->codec->id; |
456 | 563 s->luma_elim_threshold = avctx->luma_elim_threshold; |
564 s->chroma_elim_threshold= avctx->chroma_elim_threshold; | |
565 s->strict_std_compliance= avctx->strict_std_compliance; | |
566 s->data_partitioning= avctx->flags & CODEC_FLAG_PART; | |
936 | 567 s->quarter_sample= (avctx->flags & CODEC_FLAG_QPEL)!=0; |
599 | 568 s->mpeg_quant= avctx->mpeg_quant; |
344 | 569 |
0 | 570 if (s->gop_size <= 1) { |
571 s->intra_only = 1; | |
572 s->gop_size = 12; | |
573 } else { | |
574 s->intra_only = 0; | |
575 } | |
693
b6a7ff92df57
darkness masking (lumi masking does only bright stuff now)
michaelni
parents:
690
diff
changeset
|
576 |
936 | 577 s->me_method = avctx->me_method; |
693
b6a7ff92df57
darkness masking (lumi masking does only bright stuff now)
michaelni
parents:
690
diff
changeset
|
578 |
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
579 /* Fixed QSCALE */ |
0 | 580 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE); |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
581 |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
582 s->adaptive_quant= ( s->avctx->lumi_masking |
693
b6a7ff92df57
darkness masking (lumi masking does only bright stuff now)
michaelni
parents:
690
diff
changeset
|
583 || s->avctx->dark_masking |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
584 || s->avctx->temporal_cplx_masking |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
585 || s->avctx->spatial_cplx_masking |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
586 || s->avctx->p_masking) |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
587 && !s->fixed_qscale; |
697 | 588 |
589 s->progressive_sequence= !(avctx->flags & CODEC_FLAG_INTERLACED_DCT); | |
693
b6a7ff92df57
darkness masking (lumi masking does only bright stuff now)
michaelni
parents:
690
diff
changeset
|
590 |
0 | 591 switch(avctx->codec->id) { |
592 case CODEC_ID_MPEG1VIDEO: | |
593 s->out_format = FMT_MPEG1; | |
336 | 594 avctx->delay=0; //FIXME not sure, should check the spec |
0 | 595 break; |
596 case CODEC_ID_MJPEG: | |
597 s->out_format = FMT_MJPEG; | |
598 s->intra_only = 1; /* force intra only for jpeg */ | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
599 s->mjpeg_write_tables = 1; /* write all tables */ |
370
0eca28d16cbd
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
al3x
parents:
365
diff
changeset
|
600 s->mjpeg_data_only_frames = 0; /* write all the needed headers */ |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
601 s->mjpeg_vsample[0] = 2; /* set up default sampling factors */ |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
602 s->mjpeg_vsample[1] = 1; /* the only currently supported values */ |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
603 s->mjpeg_vsample[2] = 1; |
370
0eca28d16cbd
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
al3x
parents:
365
diff
changeset
|
604 s->mjpeg_hsample[0] = 2; |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
605 s->mjpeg_hsample[1] = 1; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
606 s->mjpeg_hsample[2] = 1; |
0 | 607 if (mjpeg_init(s) < 0) |
608 return -1; | |
336 | 609 avctx->delay=0; |
924 | 610 s->low_delay=1; |
0 | 611 break; |
612 case CODEC_ID_H263: | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
613 if (h263_get_picture_format(s->width, s->height) == 7) { |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
614 printf("Input picture size isn't suitable for h263 codec! try h263+\n"); |
0 | 615 return -1; |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
616 } |
0 | 617 s->out_format = FMT_H263; |
336 | 618 avctx->delay=0; |
924 | 619 s->low_delay=1; |
0 | 620 break; |
621 case CODEC_ID_H263P: | |
622 s->out_format = FMT_H263; | |
623 s->h263_plus = 1; | |
78 | 624 s->unrestricted_mv = 1; |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
625 s->h263_aic = 1; |
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
626 |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
627 /* These are just to be sure */ |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
628 s->umvplus = 0; |
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
629 s->umvplus_dec = 0; |
336 | 630 avctx->delay=0; |
924 | 631 s->low_delay=1; |
0 | 632 break; |
633 case CODEC_ID_RV10: | |
634 s->out_format = FMT_H263; | |
635 s->h263_rv10 = 1; | |
336 | 636 avctx->delay=0; |
924 | 637 s->low_delay=1; |
0 | 638 break; |
71 | 639 case CODEC_ID_MPEG4: |
0 | 640 s->out_format = FMT_H263; |
641 s->h263_pred = 1; | |
642 s->unrestricted_mv = 1; | |
924 | 643 s->low_delay= s->max_b_frames ? 0 : 1; |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
644 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); |
0 | 645 break; |
307 | 646 case CODEC_ID_MSMPEG4V1: |
0 | 647 s->out_format = FMT_H263; |
648 s->h263_msmpeg4 = 1; | |
649 s->h263_pred = 1; | |
650 s->unrestricted_mv = 1; | |
307 | 651 s->msmpeg4_version= 1; |
336 | 652 avctx->delay=0; |
924 | 653 s->low_delay=1; |
307 | 654 break; |
655 case CODEC_ID_MSMPEG4V2: | |
656 s->out_format = FMT_H263; | |
657 s->h263_msmpeg4 = 1; | |
658 s->h263_pred = 1; | |
659 s->unrestricted_mv = 1; | |
660 s->msmpeg4_version= 2; | |
336 | 661 avctx->delay=0; |
924 | 662 s->low_delay=1; |
307 | 663 break; |
664 case CODEC_ID_MSMPEG4V3: | |
665 s->out_format = FMT_H263; | |
666 s->h263_msmpeg4 = 1; | |
667 s->h263_pred = 1; | |
668 s->unrestricted_mv = 1; | |
669 s->msmpeg4_version= 3; | |
336 | 670 avctx->delay=0; |
924 | 671 s->low_delay=1; |
0 | 672 break; |
499 | 673 case CODEC_ID_WMV1: |
674 s->out_format = FMT_H263; | |
675 s->h263_msmpeg4 = 1; | |
676 s->h263_pred = 1; | |
677 s->unrestricted_mv = 1; | |
678 s->msmpeg4_version= 4; | |
679 avctx->delay=0; | |
924 | 680 s->low_delay=1; |
499 | 681 break; |
682 case CODEC_ID_WMV2: | |
683 s->out_format = FMT_H263; | |
684 s->h263_msmpeg4 = 1; | |
685 s->h263_pred = 1; | |
686 s->unrestricted_mv = 1; | |
687 s->msmpeg4_version= 5; | |
688 avctx->delay=0; | |
924 | 689 s->low_delay=1; |
499 | 690 break; |
0 | 691 default: |
692 return -1; | |
693 } | |
295 | 694 |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
695 { /* set up some save defaults, some codecs might override them later */ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
696 static int done=0; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
697 if(!done){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
698 int i; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
699 done=1; |
815
78accc54493b
put a few large tables under #ifdef CONFIG_ENCODERS or dynamically allocate them
michaelni
parents:
814
diff
changeset
|
700 |
78accc54493b
put a few large tables under #ifdef CONFIG_ENCODERS or dynamically allocate them
michaelni
parents:
814
diff
changeset
|
701 default_mv_penalty= av_mallocz( sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1) ); |
292
73a9ce3d9715
fcode_tables where too small, found by Klaas-Pieter Vlieg <vlieg@eurescom.de>
michaelni
parents:
288
diff
changeset
|
702 memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1)); |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
703 memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1)); |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
704 |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
705 for(i=-16; i<16; i++){ |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
706 default_fcode_tab[i + MAX_MV]= 1; |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
707 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
708 } |
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
709 } |
936 | 710 s->me.mv_penalty= default_mv_penalty; |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
711 s->fcode_tab= default_fcode_tab; |
506
b9ed0ae72d51
init dc_scale tables to defaults (fixes mjpeg sig11)
michaelni
parents:
499
diff
changeset
|
712 s->y_dc_scale_table= |
b9ed0ae72d51
init dc_scale tables to defaults (fixes mjpeg sig11)
michaelni
parents:
499
diff
changeset
|
713 s->c_dc_scale_table= ff_mpeg1_dc_scale_table; |
b9ed0ae72d51
init dc_scale tables to defaults (fixes mjpeg sig11)
michaelni
parents:
499
diff
changeset
|
714 |
287 | 715 /* dont use mv_penalty table for crap MV as it would be confused */ |
936 | 716 //FIXME remove after fixing / removing old ME |
717 if (s->me_method < ME_EPZS) s->me.mv_penalty = default_mv_penalty; | |
287 | 718 |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
719 s->encoding = 1; |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
720 |
0 | 721 /* init */ |
722 if (MPV_common_init(s) < 0) | |
723 return -1; | |
724 | |
936 | 725 ff_init_me(s); |
726 | |
815
78accc54493b
put a few large tables under #ifdef CONFIG_ENCODERS or dynamically allocate them
michaelni
parents:
814
diff
changeset
|
727 #ifdef CONFIG_ENCODERS |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
728 if (s->out_format == FMT_H263) |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
729 h263_encode_init(s); |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
730 else if (s->out_format == FMT_MPEG1) |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
731 ff_mpeg1_encode_init(s); |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
732 if(s->msmpeg4_version) |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
733 ff_msmpeg4_encode_init(s); |
815
78accc54493b
put a few large tables under #ifdef CONFIG_ENCODERS or dynamically allocate them
michaelni
parents:
814
diff
changeset
|
734 #endif |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
735 |
60 | 736 /* init default q matrix */ |
737 for(i=0;i<64;i++) { | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
738 int j= s->idct_permutation[i]; |
599 | 739 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){ |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
740 s->intra_matrix[j] = ff_mpeg4_default_intra_matrix[i]; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
741 s->inter_matrix[j] = ff_mpeg4_default_non_intra_matrix[i]; |
599 | 742 }else if(s->out_format == FMT_H263){ |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
743 s->intra_matrix[j] = |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
744 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i]; |
599 | 745 }else{ /* mpeg1 */ |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
746 s->intra_matrix[j] = ff_mpeg1_default_intra_matrix[i]; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
747 s->inter_matrix[j] = ff_mpeg1_default_non_intra_matrix[i]; |
599 | 748 } |
344 | 749 } |
750 | |
751 /* precompute matrix */ | |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
752 /* for mjpeg, we do include qscale in the matrix */ |
344 | 753 if (s->out_format != FMT_MJPEG) { |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
754 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, |
709
afeff6ccb7f5
convert only needed matrixes in convert_matrix() (mjpeg calls it for every frame)
michaelni
parents:
706
diff
changeset
|
755 s->intra_matrix, s->intra_quant_bias, 1, 31); |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
756 convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, |
709
afeff6ccb7f5
convert only needed matrixes in convert_matrix() (mjpeg calls it for every frame)
michaelni
parents:
706
diff
changeset
|
757 s->inter_matrix, s->inter_quant_bias, 1, 31); |
60 | 758 } |
759 | |
329 | 760 if(ff_rate_control_init(s) < 0) |
761 return -1; | |
0 | 762 |
763 s->picture_number = 0; | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
764 s->picture_in_gop_number = 0; |
0 | 765 s->fake_picture_number = 0; |
766 /* motion detector init */ | |
767 s->f_code = 1; | |
324 | 768 s->b_code = 1; |
0 | 769 |
770 return 0; | |
771 } | |
772 | |
773 int MPV_encode_end(AVCodecContext *avctx) | |
774 { | |
775 MpegEncContext *s = avctx->priv_data; | |
776 | |
777 #ifdef STATS | |
778 print_stats(); | |
779 #endif | |
329 | 780 |
781 ff_rate_control_uninit(s); | |
782 | |
0 | 783 MPV_common_end(s); |
784 if (s->out_format == FMT_MJPEG) | |
785 mjpeg_close(s); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
786 |
0 | 787 return 0; |
788 } | |
789 | |
790 /* draw the edges of width 'w' of an image of size width, height */ | |
619
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
791 //FIXME check that this is ok for mpeg4 interlaced |
206 | 792 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w) |
0 | 793 { |
794 UINT8 *ptr, *last_line; | |
795 int i; | |
796 | |
797 last_line = buf + (height - 1) * wrap; | |
798 for(i=0;i<w;i++) { | |
799 /* top and bottom */ | |
800 memcpy(buf - (i + 1) * wrap, buf, width); | |
801 memcpy(last_line + (i + 1) * wrap, last_line, width); | |
802 } | |
803 /* left and right */ | |
804 ptr = buf; | |
805 for(i=0;i<height;i++) { | |
806 memset(ptr - w, ptr[0], w); | |
807 memset(ptr + width, ptr[width-1], w); | |
808 ptr += wrap; | |
809 } | |
810 /* corners */ | |
811 for(i=0;i<w;i++) { | |
812 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */ | |
813 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */ | |
814 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */ | |
815 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */ | |
816 } | |
817 } | |
818 | |
924 | 819 static int find_unused_picture(MpegEncContext *s, int shared){ |
820 int i; | |
821 | |
822 if(shared){ | |
823 for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
824 if(s->picture[i].data[0]==NULL && s->picture[i].type==0) break; | |
825 } | |
826 }else{ | |
827 for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
828 if(s->picture[i].data[0]==NULL && s->picture[i].type!=0) break; | |
829 } | |
830 for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
831 if(s->picture[i].data[0]==NULL) break; | |
832 } | |
833 } | |
834 | |
835 assert(i<MAX_PICTURE_COUNT); | |
836 return i; | |
837 } | |
838 | |
0 | 839 /* generic function for encode/decode called before a frame is coded/decoded */ |
771
d4cc92144266
handle direct rendering buffer allocation failure
michaelni
parents:
768
diff
changeset
|
840 int MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) |
0 | 841 { |
924 | 842 int i; |
925 | 843 AVFrame *pic; |
0 | 844 |
46
931417475f5b
fixed mpeg1 first block bug (pb with black picture optimisation for B frames)
glantau
parents:
40
diff
changeset
|
845 s->mb_skiped = 0; |
903 | 846 |
847 /* mark&release old frames */ | |
848 if (s->pict_type != B_TYPE && s->last_picture.data[0]) { | |
849 for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
924 | 850 //printf("%8X %d %d %X %X\n", s->picture[i].data[0], s->picture[i].type, i, s->next_picture.data[0], s->last_picture.data[0]); |
903 | 851 if(s->picture[i].data[0] == s->last_picture.data[0]){ |
852 // s->picture[i].reference=0; | |
925 | 853 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]); |
903 | 854 break; |
855 } | |
856 } | |
857 assert(i<MAX_PICTURE_COUNT); | |
858 | |
859 /* release forgotten pictures */ | |
860 /* if(mpeg124/h263) */ | |
861 if(!s->encoding){ | |
862 for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
863 if(s->picture[i].data[0] && s->picture[i].data[0] != s->next_picture.data[0] && s->picture[i].reference){ | |
864 fprintf(stderr, "releasing zombie picture\n"); | |
925 | 865 avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]); |
903 | 866 } |
867 } | |
868 } | |
869 } | |
910
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
870 alloc: |
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
871 if(!s->encoding){ |
924 | 872 i= find_unused_picture(s, 0); |
903 | 873 |
925 | 874 pic= (AVFrame*)&s->picture[i]; |
903 | 875 pic->reference= s->pict_type != B_TYPE; |
876 pic->coded_picture_number= s->current_picture.coded_picture_number+1; | |
877 | |
924 | 878 alloc_picture(s, (Picture*)pic, 0); |
903 | 879 |
880 s->current_picture= s->picture[i]; | |
881 } | |
456 | 882 |
903 | 883 if (s->pict_type != B_TYPE) { |
884 s->last_picture= s->next_picture; | |
885 s->next_picture= s->current_picture; | |
553 | 886 } |
910
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
887 |
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
888 if(s->pict_type != I_TYPE && s->last_picture.data[0]==NULL){ |
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
889 fprintf(stderr, "warning: first frame is no keyframe\n"); |
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
890 assert(s->pict_type != B_TYPE); //these should have been dropped if we dont have a reference |
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
891 goto alloc; |
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
892 } |
903 | 893 |
910
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
894 s->hurry_up= s->avctx->hurry_up; |
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
895 s->error_resilience= avctx->error_resilience; |
eb448df811be
fixing files where the first frame isn a keyframe
michaelni
parents:
909
diff
changeset
|
896 |
591 | 897 /* set dequantizer, we cant do it during init as it might change for mpeg4 |
898 and we cant do it in the header decode as init isnt called for mpeg4 there yet */ | |
899 if(s->out_format == FMT_H263){ | |
900 if(s->mpeg_quant) | |
901 s->dct_unquantize = s->dct_unquantize_mpeg2; | |
902 else | |
903 s->dct_unquantize = s->dct_unquantize_h263; | |
904 }else | |
905 s->dct_unquantize = s->dct_unquantize_mpeg1; | |
771
d4cc92144266
handle direct rendering buffer allocation failure
michaelni
parents:
768
diff
changeset
|
906 |
d4cc92144266
handle direct rendering buffer allocation failure
michaelni
parents:
768
diff
changeset
|
907 return 0; |
0 | 908 } |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
909 |
0 | 910 /* generic function for encode/decode called after a frame has been coded/decoded */ |
911 void MPV_frame_end(MpegEncContext *s) | |
912 { | |
903 | 913 int i; |
0 | 914 /* draw edge for correct motion prediction if outside */ |
903 | 915 if(s->codec_id!=CODEC_ID_SVQ1){ |
916 if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) { | |
917 draw_edges(s->current_picture.data[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH ); | |
918 draw_edges(s->current_picture.data[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); | |
919 draw_edges(s->current_picture.data[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); | |
920 } | |
0 | 921 } |
207 | 922 emms_c(); |
329 | 923 |
612 | 924 s->last_pict_type = s->pict_type; |
329 | 925 if(s->pict_type!=B_TYPE){ |
926 s->last_non_b_pict_type= s->pict_type; | |
927 } | |
903 | 928 |
929 s->current_picture.quality= s->qscale; //FIXME get average of qscale_table | |
930 s->current_picture.pict_type= s->pict_type; | |
931 s->current_picture.key_frame= s->pict_type == I_TYPE; | |
932 | |
933 /* copy back current_picture variables */ | |
934 for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
935 if(s->picture[i].data[0] == s->current_picture.data[0]){ | |
936 s->picture[i]= s->current_picture; | |
937 break; | |
938 } | |
939 } | |
940 assert(i<MAX_PICTURE_COUNT); | |
941 | |
942 /* release non refernce frames */ | |
943 for(i=0; i<MAX_PICTURE_COUNT; i++){ | |
924 | 944 if(s->picture[i].data[0] && !s->picture[i].reference /*&& s->picture[i].type!=FF_BUFFER_TYPE_SHARED*/) |
925 | 945 s->avctx->release_buffer(s->avctx, (AVFrame*)&s->picture[i]); |
324 | 946 } |
936 | 947 if(s->avctx->debug&FF_DEBUG_SKIP){ |
948 int x,y; | |
949 for(y=0; y<s->mb_height; y++){ | |
950 for(x=0; x<s->mb_width; x++){ | |
951 int count= s->mbskip_table[x + y*s->mb_width]; | |
952 if(count>9) count=9; | |
953 printf(" %1d", count); | |
954 } | |
955 printf("\n"); | |
956 } | |
957 printf("pict type: %d\n", s->pict_type); | |
958 } | |
903 | 959 } |
960 | |
915 | 961 static int get_sae(uint8_t *src, int ref, int stride){ |
962 int x,y; | |
963 int acc=0; | |
964 | |
965 for(y=0; y<16; y++){ | |
966 for(x=0; x<16; x++){ | |
967 acc+= ABS(src[x+y*stride] - ref); | |
968 } | |
969 } | |
970 | |
971 return acc; | |
972 } | |
973 | |
974 static int get_intra_count(MpegEncContext *s, uint8_t *src, uint8_t *ref, int stride){ | |
975 int x, y, w, h; | |
976 int acc=0; | |
977 | |
978 w= s->width &~15; | |
979 h= s->height&~15; | |
980 | |
981 for(y=0; y<h; y+=16){ | |
982 for(x=0; x<w; x+=16){ | |
983 int offset= x + y*stride; | |
984 int sad = s->dsp.pix_abs16x16(src + offset, ref + offset, stride); | |
985 int mean= (s->dsp.pix_sum(src + offset, stride) + 128)>>8; | |
986 int sae = get_sae(src + offset, mean, stride); | |
987 | |
988 acc+= sae + 500 < sad; | |
989 } | |
990 } | |
991 return acc; | |
992 } | |
993 | |
924 | 994 |
925 | 995 static int load_input_picture(MpegEncContext *s, AVFrame *pic_arg){ |
996 AVFrame *pic; | |
924 | 997 int i; |
903 | 998 const int encoding_delay= s->max_b_frames; |
924 | 999 int direct=1; |
1000 | |
1001 if(encoding_delay && !(s->flags&CODEC_FLAG_INPUT_PRESERVED)) direct=0; | |
1002 if(pic_arg->linesize[0] != s->linesize) direct=0; | |
1003 if(pic_arg->linesize[1] != s->uvlinesize) direct=0; | |
1004 if(pic_arg->linesize[2] != s->uvlinesize) direct=0; | |
1005 | |
1006 // printf("%d %d %d %d\n",pic_arg->linesize[0], pic_arg->linesize[1], s->linesize, s->uvlinesize); | |
1007 | |
1008 if(direct){ | |
1009 i= find_unused_picture(s, 1); | |
1010 | |
925 | 1011 pic= (AVFrame*)&s->picture[i]; |
924 | 1012 pic->reference= 1; |
903 | 1013 |
924 | 1014 for(i=0; i<4; i++){ |
1015 pic->data[i]= pic_arg->data[i]; | |
1016 pic->linesize[i]= pic_arg->linesize[i]; | |
1017 } | |
1018 alloc_picture(s, (Picture*)pic, 1); | |
1019 }else{ | |
1020 i= find_unused_picture(s, 0); | |
1021 | |
925 | 1022 pic= (AVFrame*)&s->picture[i]; |
924 | 1023 pic->reference= 1; |
1024 | |
1025 alloc_picture(s, (Picture*)pic, 0); | |
1026 | |
1027 if( pic->data[0] == pic_arg->data[0] | |
1028 && pic->data[1] == pic_arg->data[1] | |
1029 && pic->data[2] == pic_arg->data[2]){ | |
1030 // empty | |
1031 }else{ | |
1032 int h_chroma_shift, v_chroma_shift; | |
1033 | |
1034 avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift); | |
1035 | |
1036 for(i=0; i<3; i++){ | |
1037 int src_stride= pic_arg->linesize[i]; | |
1038 int dst_stride= i ? s->uvlinesize : s->linesize; | |
1039 int h_shift= i ? h_chroma_shift : 0; | |
1040 int v_shift= i ? v_chroma_shift : 0; | |
1041 int w= s->width >>h_shift; | |
1042 int h= s->height>>v_shift; | |
1043 uint8_t *src= pic_arg->data[i]; | |
1044 uint8_t *dst= pic->data[i]; | |
1045 | |
1046 if(src_stride==dst_stride) | |
1047 memcpy(dst, src, src_stride*h); | |
1048 else{ | |
1049 while(h--){ | |
1050 memcpy(dst, src, w); | |
1051 dst += dst_stride; | |
1052 src += src_stride; | |
1053 } | |
1054 } | |
1055 } | |
1056 } | |
324 | 1057 } |
924 | 1058 pic->quality= pic_arg->quality; |
1059 pic->pict_type= pic_arg->pict_type; | |
955
8f5d4c666806
pts encoding fix patch by (Thomas Jarosch <tomj at simonv dot com>)
michaelni
parents:
954
diff
changeset
|
1060 pic->pts = pic_arg->pts; |
903 | 1061 |
1062 if(s->input_picture[encoding_delay]) | |
1063 pic->display_picture_number= s->input_picture[encoding_delay]->display_picture_number + 1; | |
1064 | |
1065 /* shift buffer entries */ | |
1066 for(i=1; i<MAX_PICTURE_COUNT /*s->encoding_delay+1*/; i++) | |
1067 s->input_picture[i-1]= s->input_picture[i]; | |
1068 | |
1069 s->input_picture[encoding_delay]= (Picture*)pic; | |
1070 | |
1071 return 0; | |
1072 } | |
1073 | |
1074 static void select_input_picture(MpegEncContext *s){ | |
1075 int i; | |
1076 const int encoding_delay= s->max_b_frames; | |
1077 int coded_pic_num=0; | |
1078 | |
1079 if(s->reordered_input_picture[0]) | |
1080 coded_pic_num= s->reordered_input_picture[0]->coded_picture_number + 1; | |
924 | 1081 |
903 | 1082 for(i=1; i<MAX_PICTURE_COUNT; i++) |
1083 s->reordered_input_picture[i-1]= s->reordered_input_picture[i]; | |
1084 s->reordered_input_picture[MAX_PICTURE_COUNT-1]= NULL; | |
1085 | |
1086 /* set next picture types & ordering */ | |
1087 if(s->reordered_input_picture[0]==NULL && s->input_picture[0]){ | |
915 | 1088 if(/*s->picture_in_gop_number >= s->gop_size ||*/ s->next_picture.data[0]==NULL || s->intra_only){ |
1089 s->reordered_input_picture[0]= s->input_picture[0]; | |
1090 s->reordered_input_picture[0]->pict_type= I_TYPE; | |
1091 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; | |
1092 }else{ | |
1093 int b_frames; | |
1094 | |
1095 if(s->flags&CODEC_FLAG_PASS2){ | |
1096 for(i=0; i<s->max_b_frames+1; i++){ | |
1097 int pict_num= s->input_picture[0]->display_picture_number + i; | |
1098 int pict_type= s->rc_context.entry[pict_num].new_pict_type; | |
1099 s->input_picture[i]->pict_type= pict_type; | |
1100 | |
1101 if(i + 1 >= s->rc_context.num_entries) break; | |
1102 } | |
1103 } | |
924 | 1104 |
915 | 1105 if(s->input_picture[0]->pict_type){ |
1106 /* user selected pict_type */ | |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1107 for(b_frames=0; b_frames<s->max_b_frames+1; b_frames++){ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1108 if(s->input_picture[b_frames]->pict_type!=B_TYPE) break; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1109 } |
915 | 1110 |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1111 if(b_frames > s->max_b_frames){ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1112 fprintf(stderr, "warning, too many bframes in a row\n"); |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1113 b_frames = s->max_b_frames; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1114 } |
915 | 1115 }else if(s->b_frame_strategy==0){ |
1116 b_frames= s->max_b_frames; | |
1117 }else if(s->b_frame_strategy==1){ | |
1118 for(i=1; i<s->max_b_frames+1; i++){ | |
1119 if(s->input_picture[i]->b_frame_score==0){ | |
1120 s->input_picture[i]->b_frame_score= | |
924 | 1121 get_intra_count(s, s->input_picture[i ]->data[0], |
1122 s->input_picture[i-1]->data[0], s->linesize) + 1; | |
915 | 1123 } |
1124 } | |
1125 for(i=0; i<s->max_b_frames; i++){ | |
1126 if(s->input_picture[i]->b_frame_score - 1 > s->mb_num/40) break; | |
1127 } | |
1128 | |
1129 b_frames= FFMAX(0, i-1); | |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1130 |
915 | 1131 /* reset scores */ |
1132 for(i=0; i<b_frames+1; i++){ | |
1133 s->input_picture[i]->b_frame_score=0; | |
1134 } | |
1135 }else{ | |
1136 fprintf(stderr, "illegal b frame strategy\n"); | |
1137 b_frames=0; | |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1138 } |
915 | 1139 |
1140 emms_c(); | |
1141 //static int b_count=0; | |
1142 //b_count+= b_frames; | |
1143 //printf("b_frames: %d\n", b_count); | |
1144 | |
1145 s->reordered_input_picture[0]= s->input_picture[b_frames]; | |
1146 if( s->picture_in_gop_number + b_frames >= s->gop_size | |
1147 || s->reordered_input_picture[0]->pict_type== I_TYPE) | |
903 | 1148 s->reordered_input_picture[0]->pict_type= I_TYPE; |
915 | 1149 else |
1150 s->reordered_input_picture[0]->pict_type= P_TYPE; | |
1151 s->reordered_input_picture[0]->coded_picture_number= coded_pic_num; | |
1152 for(i=0; i<b_frames; i++){ | |
1153 coded_pic_num++; | |
1154 s->reordered_input_picture[i+1]= s->input_picture[i]; | |
1155 s->reordered_input_picture[i+1]->pict_type= B_TYPE; | |
1156 s->reordered_input_picture[i+1]->coded_picture_number= coded_pic_num; | |
903 | 1157 } |
324 | 1158 } |
1159 } | |
903 | 1160 |
1161 if(s->reordered_input_picture[0]){ | |
924 | 1162 s->reordered_input_picture[0]->reference= s->reordered_input_picture[0]->pict_type!=B_TYPE; |
1163 | |
1164 if(s->reordered_input_picture[0]->type == FF_BUFFER_TYPE_SHARED){ | |
1165 int i= find_unused_picture(s, 0); | |
1166 Picture *pic= &s->picture[i]; | |
1167 | |
1168 s->new_picture= *s->reordered_input_picture[0]; | |
1169 | |
1170 /* mark us unused / free shared pic */ | |
1171 for(i=0; i<4; i++) | |
1172 s->reordered_input_picture[0]->data[i]= NULL; | |
1173 s->reordered_input_picture[0]->type= 0; | |
1174 | |
1175 pic->pict_type = s->reordered_input_picture[0]->pict_type; | |
1176 pic->quality = s->reordered_input_picture[0]->quality; | |
1177 pic->coded_picture_number = s->reordered_input_picture[0]->coded_picture_number; | |
1178 pic->reference = s->reordered_input_picture[0]->reference; | |
1179 | |
1180 alloc_picture(s, pic, 0); | |
1181 | |
1182 s->current_picture= *pic; | |
1183 }else{ | |
1184 assert( s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_USER | |
1185 || s->reordered_input_picture[0]->type==FF_BUFFER_TYPE_INTERNAL); | |
1186 | |
1187 s->new_picture= *s->reordered_input_picture[0]; | |
1188 | |
1189 for(i=0; i<4; i++){ | |
1190 s->reordered_input_picture[0]->data[i]-=16; //FIXME dirty | |
1191 } | |
1192 s->current_picture= *s->reordered_input_picture[0]; | |
903 | 1193 } |
1194 | |
1195 s->picture_number= s->new_picture.display_picture_number; | |
1196 //printf("dpn:%d\n", s->picture_number); | |
1197 }else{ | |
1198 memset(&s->new_picture, 0, sizeof(Picture)); | |
324 | 1199 } |
1200 } | |
1201 | |
0 | 1202 int MPV_encode_picture(AVCodecContext *avctx, |
1203 unsigned char *buf, int buf_size, void *data) | |
1204 { | |
1205 MpegEncContext *s = avctx->priv_data; | |
925 | 1206 AVFrame *pic_arg = data; |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1207 int i; |
0 | 1208 |
1209 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | |
1210 | |
903 | 1211 s->picture_in_gop_number++; |
1212 | |
1213 load_input_picture(s, pic_arg); | |
329 | 1214 |
903 | 1215 select_input_picture(s); |
324 | 1216 |
1217 /* output? */ | |
903 | 1218 if(s->new_picture.data[0]){ |
1219 | |
1220 s->pict_type= s->new_picture.pict_type; | |
1221 if (s->fixed_qscale){ /* the ratecontrol needs the last qscale so we dont touch it for CBR */ | |
1222 s->qscale= (int)(s->new_picture.quality+0.5); | |
1223 assert(s->qscale); | |
1224 } | |
1225 //emms_c(); | |
1226 //printf("qs:%f %f %d\n", s->new_picture.quality, s->current_picture.quality, s->qscale); | |
553 | 1227 MPV_frame_start(s, avctx); |
286 | 1228 |
324 | 1229 encode_picture(s, s->picture_number); |
652 | 1230 |
376 | 1231 avctx->real_pict_num = s->picture_number; |
324 | 1232 avctx->header_bits = s->header_bits; |
1233 avctx->mv_bits = s->mv_bits; | |
1234 avctx->misc_bits = s->misc_bits; | |
1235 avctx->i_tex_bits = s->i_tex_bits; | |
1236 avctx->p_tex_bits = s->p_tex_bits; | |
1237 avctx->i_count = s->i_count; | |
656
e47fa3e3f2d5
statistics for forw & back p-MBs instead of just one counter for both
michaelni
parents:
652
diff
changeset
|
1238 avctx->p_count = s->mb_num - s->i_count - s->skip_count; //FIXME f/b_count in avctx |
324 | 1239 avctx->skip_count = s->skip_count; |
0 | 1240 |
324 | 1241 MPV_frame_end(s); |
1242 | |
1243 if (s->out_format == FMT_MJPEG) | |
1244 mjpeg_picture_trailer(s); | |
329 | 1245 |
1246 if(s->flags&CODEC_FLAG_PASS1) | |
1247 ff_write_pass1_stats(s); | |
324 | 1248 } |
1249 | |
1250 s->input_picture_number++; | |
0 | 1251 |
1252 flush_put_bits(&s->pb); | |
268 | 1253 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; |
612 | 1254 |
268 | 1255 s->total_bits += s->frame_bits; |
286 | 1256 avctx->frame_bits = s->frame_bits; |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1257 |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1258 for(i=0; i<4; i++){ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1259 avctx->error[i] += s->current_picture.error[i]; |
252
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
1260 } |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1261 |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
233
diff
changeset
|
1262 return pbBufPtr(&s->pb) - s->pb.buf; |
0 | 1263 } |
1264 | |
255 | 1265 static inline void gmc1_motion(MpegEncContext *s, |
1266 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
1267 int dest_offset, | |
753 | 1268 UINT8 **ref_picture, int src_offset) |
255 | 1269 { |
1270 UINT8 *ptr; | |
556 | 1271 int offset, src_x, src_y, linesize, uvlinesize; |
255 | 1272 int motion_x, motion_y; |
566 | 1273 int emu=0; |
255 | 1274 |
1275 motion_x= s->sprite_offset[0][0]; | |
1276 motion_y= s->sprite_offset[0][1]; | |
1277 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
1278 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
1279 motion_x<<=(3-s->sprite_warping_accuracy); | |
1280 motion_y<<=(3-s->sprite_warping_accuracy); | |
1281 src_x = clip(src_x, -16, s->width); | |
1282 if (src_x == s->width) | |
1283 motion_x =0; | |
1284 src_y = clip(src_y, -16, s->height); | |
1285 if (src_y == s->height) | |
1286 motion_y =0; | |
753 | 1287 |
255 | 1288 linesize = s->linesize; |
556 | 1289 uvlinesize = s->uvlinesize; |
753 | 1290 |
255 | 1291 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; |
1292 | |
1293 dest_y+=dest_offset; | |
566 | 1294 if(s->flags&CODEC_FLAG_EMU_EDGE){ |
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1295 if(src_x<0 || src_y<0 || src_x + (motion_x&15) + 16 > s->h_edge_pos |
753 | 1296 || src_y + (motion_y&15) + 16 > s->v_edge_pos){ |
936 | 1297 ff_emulated_edge_mc(s, ptr, linesize, 17, 17, src_x, src_y, s->h_edge_pos, s->v_edge_pos); |
566 | 1298 ptr= s->edge_emu_buffer; |
1299 emu=1; | |
1300 } | |
1301 } | |
753 | 1302 |
1303 if((motion_x|motion_y)&7){ | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1304 s->dsp.gmc1(dest_y , ptr , linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding); |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1305 s->dsp.gmc1(dest_y+8, ptr+8, linesize, 16, motion_x&15, motion_y&15, 128 - s->no_rounding); |
753 | 1306 }else{ |
1307 int dxy; | |
1308 | |
1309 dxy= ((motion_x>>3)&1) | ((motion_y>>2)&2); | |
1310 if (s->no_rounding){ | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1311 s->dsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16); |
753 | 1312 }else{ |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1313 s->dsp.put_pixels_tab [0][dxy](dest_y, ptr, linesize, 16); |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1314 } |
753 | 1315 } |
1316 | |
1317 if(s->flags&CODEC_FLAG_GRAY) return; | |
255 | 1318 |
1319 motion_x= s->sprite_offset[1][0]; | |
1320 motion_y= s->sprite_offset[1][1]; | |
1321 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
1322 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
1323 motion_x<<=(3-s->sprite_warping_accuracy); | |
1324 motion_y<<=(3-s->sprite_warping_accuracy); | |
1325 src_x = clip(src_x, -8, s->width>>1); | |
1326 if (src_x == s->width>>1) | |
1327 motion_x =0; | |
1328 src_y = clip(src_y, -8, s->height>>1); | |
1329 if (src_y == s->height>>1) | |
1330 motion_y =0; | |
1331 | |
556 | 1332 offset = (src_y * uvlinesize) + src_x + (src_offset>>1); |
255 | 1333 ptr = ref_picture[1] + offset; |
566 | 1334 if(emu){ |
936 | 1335 ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); |
566 | 1336 ptr= s->edge_emu_buffer; |
1337 } | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1338 s->dsp.gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding); |
566 | 1339 |
255 | 1340 ptr = ref_picture[2] + offset; |
566 | 1341 if(emu){ |
936 | 1342 ff_emulated_edge_mc(s, ptr, uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); |
566 | 1343 ptr= s->edge_emu_buffer; |
1344 } | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1345 s->dsp.gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, 8, motion_x&15, motion_y&15, 128 - s->no_rounding); |
255 | 1346 |
1347 return; | |
1348 } | |
1349 | |
753 | 1350 static inline void gmc_motion(MpegEncContext *s, |
1351 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
1352 int dest_offset, | |
1353 UINT8 **ref_picture, int src_offset) | |
1354 { | |
1355 UINT8 *ptr; | |
1356 int linesize, uvlinesize; | |
1357 const int a= s->sprite_warping_accuracy; | |
1358 int ox, oy; | |
1359 | |
1360 linesize = s->linesize; | |
1361 uvlinesize = s->uvlinesize; | |
1362 | |
1363 ptr = ref_picture[0] + src_offset; | |
1364 | |
1365 dest_y+=dest_offset; | |
1366 | |
1367 ox= s->sprite_offset[0][0] + s->sprite_delta[0][0]*s->mb_x*16 + s->sprite_delta[0][1]*s->mb_y*16; | |
1368 oy= s->sprite_offset[0][1] + s->sprite_delta[1][0]*s->mb_x*16 + s->sprite_delta[1][1]*s->mb_y*16; | |
1369 | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1370 s->dsp.gmc(dest_y, ptr, linesize, 16, |
753 | 1371 ox, |
1372 oy, | |
1373 s->sprite_delta[0][0], s->sprite_delta[0][1], | |
1374 s->sprite_delta[1][0], s->sprite_delta[1][1], | |
1375 a+1, (1<<(2*a+1)) - s->no_rounding, | |
1376 s->h_edge_pos, s->v_edge_pos); | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1377 s->dsp.gmc(dest_y+8, ptr, linesize, 16, |
753 | 1378 ox + s->sprite_delta[0][0]*8, |
1379 oy + s->sprite_delta[1][0]*8, | |
1380 s->sprite_delta[0][0], s->sprite_delta[0][1], | |
1381 s->sprite_delta[1][0], s->sprite_delta[1][1], | |
1382 a+1, (1<<(2*a+1)) - s->no_rounding, | |
1383 s->h_edge_pos, s->v_edge_pos); | |
1384 | |
1385 if(s->flags&CODEC_FLAG_GRAY) return; | |
1386 | |
1387 | |
1388 dest_cb+=dest_offset>>1; | |
1389 dest_cr+=dest_offset>>1; | |
1390 | |
1391 ox= s->sprite_offset[1][0] + s->sprite_delta[0][0]*s->mb_x*8 + s->sprite_delta[0][1]*s->mb_y*8; | |
1392 oy= s->sprite_offset[1][1] + s->sprite_delta[1][0]*s->mb_x*8 + s->sprite_delta[1][1]*s->mb_y*8; | |
1393 | |
1394 ptr = ref_picture[1] + (src_offset>>1); | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1395 s->dsp.gmc(dest_cb, ptr, uvlinesize, 8, |
753 | 1396 ox, |
1397 oy, | |
1398 s->sprite_delta[0][0], s->sprite_delta[0][1], | |
1399 s->sprite_delta[1][0], s->sprite_delta[1][1], | |
1400 a+1, (1<<(2*a+1)) - s->no_rounding, | |
1401 s->h_edge_pos>>1, s->v_edge_pos>>1); | |
1402 | |
1403 ptr = ref_picture[2] + (src_offset>>1); | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1404 s->dsp.gmc(dest_cr, ptr, uvlinesize, 8, |
753 | 1405 ox, |
1406 oy, | |
1407 s->sprite_delta[0][0], s->sprite_delta[0][1], | |
1408 s->sprite_delta[1][0], s->sprite_delta[1][1], | |
1409 a+1, (1<<(2*a+1)) - s->no_rounding, | |
1410 s->h_edge_pos>>1, s->v_edge_pos>>1); | |
1411 } | |
1412 | |
1413 | |
936 | 1414 void ff_emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, |
553 | 1415 int src_x, int src_y, int w, int h){ |
1416 int x, y; | |
1417 int start_y, start_x, end_y, end_x; | |
566 | 1418 UINT8 *buf= s->edge_emu_buffer; |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
1419 |
553 | 1420 if(src_y>= h){ |
1421 src+= (h-1-src_y)*linesize; | |
1422 src_y=h-1; | |
554 | 1423 }else if(src_y<=-block_h){ |
1424 src+= (1-block_h-src_y)*linesize; | |
1425 src_y=1-block_h; | |
553 | 1426 } |
1427 if(src_x>= w){ | |
1428 src+= (w-1-src_x); | |
1429 src_x=w-1; | |
554 | 1430 }else if(src_x<=-block_w){ |
1431 src+= (1-block_w-src_x); | |
1432 src_x=1-block_w; | |
553 | 1433 } |
1434 | |
847 | 1435 start_y= FFMAX(0, -src_y); |
1436 start_x= FFMAX(0, -src_x); | |
1437 end_y= FFMIN(block_h, h-src_y); | |
1438 end_x= FFMIN(block_w, w-src_x); | |
566 | 1439 |
553 | 1440 // copy existing part |
1441 for(y=start_y; y<end_y; y++){ | |
1442 for(x=start_x; x<end_x; x++){ | |
1443 buf[x + y*linesize]= src[x + y*linesize]; | |
1444 } | |
1445 } | |
1446 | |
1447 //top | |
1448 for(y=0; y<start_y; y++){ | |
1449 for(x=start_x; x<end_x; x++){ | |
1450 buf[x + y*linesize]= buf[x + start_y*linesize]; | |
1451 } | |
1452 } | |
1453 | |
1454 //bottom | |
1455 for(y=end_y; y<block_h; y++){ | |
1456 for(x=start_x; x<end_x; x++){ | |
1457 buf[x + y*linesize]= buf[x + (end_y-1)*linesize]; | |
1458 } | |
1459 } | |
1460 | |
1461 for(y=0; y<block_h; y++){ | |
1462 //left | |
1463 for(x=0; x<start_x; x++){ | |
1464 buf[x + y*linesize]= buf[start_x + y*linesize]; | |
1465 } | |
1466 | |
1467 //right | |
1468 for(x=end_x; x<block_w; x++){ | |
1469 buf[x + y*linesize]= buf[end_x - 1 + y*linesize]; | |
1470 } | |
1471 } | |
1472 } | |
1473 | |
1474 | |
0 | 1475 /* apply one mpeg motion vector to the three components */ |
1476 static inline void mpeg_motion(MpegEncContext *s, | |
1477 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
1478 int dest_offset, | |
1479 UINT8 **ref_picture, int src_offset, | |
651 | 1480 int field_based, op_pixels_func (*pix_op)[4], |
0 | 1481 int motion_x, int motion_y, int h) |
1482 { | |
1483 UINT8 *ptr; | |
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1484 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize; |
553 | 1485 int emu=0; |
651 | 1486 #if 0 |
255 | 1487 if(s->quarter_sample) |
1488 { | |
1489 motion_x>>=1; | |
1490 motion_y>>=1; | |
1491 } | |
651 | 1492 #endif |
0 | 1493 dxy = ((motion_y & 1) << 1) | (motion_x & 1); |
1494 src_x = s->mb_x * 16 + (motion_x >> 1); | |
1495 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); | |
1496 | |
1497 /* WARNING: do no forget half pels */ | |
1498 height = s->height >> field_based; | |
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1499 v_edge_pos = s->v_edge_pos >> field_based; |
0 | 1500 src_x = clip(src_x, -16, s->width); |
1501 if (src_x == s->width) | |
1502 dxy &= ~1; | |
1503 src_y = clip(src_y, -16, height); | |
1504 if (src_y == height) | |
1505 dxy &= ~2; | |
565
44d744901ded
interlaced mpeg2 fix ... replacing linesize>>1 by uvlinesize brainlessly wasnt a good idea
michaelni
parents:
562
diff
changeset
|
1506 linesize = s->linesize << field_based; |
44d744901ded
interlaced mpeg2 fix ... replacing linesize>>1 by uvlinesize brainlessly wasnt a good idea
michaelni
parents:
562
diff
changeset
|
1507 uvlinesize = s->uvlinesize << field_based; |
0 | 1508 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset; |
1509 dest_y += dest_offset; | |
553 | 1510 |
1511 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1512 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 16 > s->h_edge_pos |
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1513 || src_y + (motion_y&1) + h > v_edge_pos){ |
936 | 1514 ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, |
763 | 1515 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos); |
1516 ptr= s->edge_emu_buffer + src_offset; | |
553 | 1517 emu=1; |
1518 } | |
1519 } | |
651 | 1520 pix_op[0][dxy](dest_y, ptr, linesize, h); |
0 | 1521 |
485 | 1522 if(s->flags&CODEC_FLAG_GRAY) return; |
1523 | |
0 | 1524 if (s->out_format == FMT_H263) { |
1525 dxy = 0; | |
1526 if ((motion_x & 3) != 0) | |
1527 dxy |= 1; | |
1528 if ((motion_y & 3) != 0) | |
1529 dxy |= 2; | |
1530 mx = motion_x >> 2; | |
1531 my = motion_y >> 2; | |
1532 } else { | |
1533 mx = motion_x / 2; | |
1534 my = motion_y / 2; | |
1535 dxy = ((my & 1) << 1) | (mx & 1); | |
1536 mx >>= 1; | |
1537 my >>= 1; | |
1538 } | |
1539 | |
1540 src_x = s->mb_x * 8 + mx; | |
1541 src_y = s->mb_y * (8 >> field_based) + my; | |
1542 src_x = clip(src_x, -8, s->width >> 1); | |
1543 if (src_x == (s->width >> 1)) | |
1544 dxy &= ~1; | |
1545 src_y = clip(src_y, -8, height >> 1); | |
1546 if (src_y == (height >> 1)) | |
1547 dxy &= ~2; | |
565
44d744901ded
interlaced mpeg2 fix ... replacing linesize>>1 by uvlinesize brainlessly wasnt a good idea
michaelni
parents:
562
diff
changeset
|
1548 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1); |
0 | 1549 ptr = ref_picture[1] + offset; |
553 | 1550 if(emu){ |
936 | 1551 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, |
763 | 1552 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); |
1553 ptr= s->edge_emu_buffer + (src_offset >> 1); | |
553 | 1554 } |
651 | 1555 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1); |
553 | 1556 |
0 | 1557 ptr = ref_picture[2] + offset; |
553 | 1558 if(emu){ |
936 | 1559 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9+field_based, |
763 | 1560 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); |
1561 ptr= s->edge_emu_buffer + (src_offset >> 1); | |
553 | 1562 } |
651 | 1563 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1); |
0 | 1564 } |
1565 | |
255 | 1566 static inline void qpel_motion(MpegEncContext *s, |
1567 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
1568 int dest_offset, | |
1569 UINT8 **ref_picture, int src_offset, | |
651 | 1570 int field_based, op_pixels_func (*pix_op)[4], |
1571 qpel_mc_func (*qpix_op)[16], | |
255 | 1572 int motion_x, int motion_y, int h) |
1573 { | |
1574 UINT8 *ptr; | |
671 | 1575 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize; |
554 | 1576 int emu=0; |
255 | 1577 |
1578 dxy = ((motion_y & 3) << 2) | (motion_x & 3); | |
1579 src_x = s->mb_x * 16 + (motion_x >> 2); | |
1580 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2); | |
1581 | |
1582 height = s->height >> field_based; | |
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1583 v_edge_pos = s->v_edge_pos >> field_based; |
255 | 1584 src_x = clip(src_x, -16, s->width); |
1585 if (src_x == s->width) | |
1586 dxy &= ~3; | |
1587 src_y = clip(src_y, -16, height); | |
1588 if (src_y == height) | |
1589 dxy &= ~12; | |
1590 linesize = s->linesize << field_based; | |
671 | 1591 uvlinesize = s->uvlinesize << field_based; |
255 | 1592 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; |
1593 dest_y += dest_offset; | |
1594 //printf("%d %d %d\n", src_x, src_y, dxy); | |
554 | 1595 |
1596 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1597 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 16 > s->h_edge_pos |
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1598 || src_y + (motion_y&3) + h > v_edge_pos){ |
936 | 1599 ff_emulated_edge_mc(s, ptr - src_offset, s->linesize, 17, 17+field_based, |
763 | 1600 src_x, src_y<<field_based, s->h_edge_pos, s->v_edge_pos); |
1601 ptr= s->edge_emu_buffer + src_offset; | |
554 | 1602 emu=1; |
1603 } | |
1604 } | |
671 | 1605 if(!field_based) |
1606 qpix_op[0][dxy](dest_y, ptr, linesize); | |
1607 else{ | |
1608 //damn interlaced mode | |
1609 //FIXME boundary mirroring is not exactly correct here | |
1610 qpix_op[1][dxy](dest_y , ptr , linesize); | |
1611 qpix_op[1][dxy](dest_y+8, ptr+8, linesize); | |
1612 } | |
651 | 1613 |
485 | 1614 if(s->flags&CODEC_FLAG_GRAY) return; |
1615 | |
671 | 1616 if(field_based){ |
1617 mx= motion_x/2; | |
1618 my= motion_y>>1; | |
760 | 1619 }else if(s->workaround_bugs&FF_BUG_QPEL_CHROMA){ |
671 | 1620 mx= (motion_x>>1)|(motion_x&1); |
1621 my= (motion_y>>1)|(motion_y&1); | |
1622 }else{ | |
1623 mx= motion_x/2; | |
1624 my= motion_y/2; | |
1625 } | |
1626 mx= (mx>>1)|(mx&1); | |
1627 my= (my>>1)|(my&1); | |
1628 dxy= (mx&1) | ((my&1)<<1); | |
1629 mx>>=1; | |
1630 my>>=1; | |
255 | 1631 |
1632 src_x = s->mb_x * 8 + mx; | |
1633 src_y = s->mb_y * (8 >> field_based) + my; | |
1634 src_x = clip(src_x, -8, s->width >> 1); | |
1635 if (src_x == (s->width >> 1)) | |
1636 dxy &= ~1; | |
1637 src_y = clip(src_y, -8, height >> 1); | |
1638 if (src_y == (height >> 1)) | |
1639 dxy &= ~2; | |
1640 | |
671 | 1641 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1); |
255 | 1642 ptr = ref_picture[1] + offset; |
554 | 1643 if(emu){ |
936 | 1644 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, |
763 | 1645 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); |
1646 ptr= s->edge_emu_buffer + (src_offset >> 1); | |
554 | 1647 } |
671 | 1648 pix_op[1][dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1); |
554 | 1649 |
255 | 1650 ptr = ref_picture[2] + offset; |
554 | 1651 if(emu){ |
936 | 1652 ff_emulated_edge_mc(s, ptr - (src_offset >> 1), s->uvlinesize, 9, 9 + field_based, |
763 | 1653 src_x, src_y<<field_based, s->h_edge_pos>>1, s->v_edge_pos>>1); |
1654 ptr= s->edge_emu_buffer + (src_offset >> 1); | |
554 | 1655 } |
671 | 1656 pix_op[1][dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1); |
255 | 1657 } |
1658 | |
1659 | |
0 | 1660 static inline void MPV_motion(MpegEncContext *s, |
1661 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
1662 int dir, UINT8 **ref_picture, | |
651 | 1663 op_pixels_func (*pix_op)[4], qpel_mc_func (*qpix_op)[16]) |
0 | 1664 { |
1665 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y; | |
1666 int mb_x, mb_y, i; | |
1667 UINT8 *ptr, *dest; | |
554 | 1668 int emu=0; |
0 | 1669 |
1670 mb_x = s->mb_x; | |
1671 mb_y = s->mb_y; | |
1672 | |
1673 switch(s->mv_type) { | |
1674 case MV_TYPE_16X16: | |
255 | 1675 if(s->mcsel){ |
753 | 1676 if(s->real_sprite_warping_points==1){ |
1677 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0, | |
1678 ref_picture, 0); | |
1679 }else{ | |
1680 gmc_motion(s, dest_y, dest_cb, dest_cr, 0, | |
1681 ref_picture, 0); | |
1682 } | |
651 | 1683 }else if(s->quarter_sample){ |
255 | 1684 qpel_motion(s, dest_y, dest_cb, dest_cr, 0, |
1685 ref_picture, 0, | |
1686 0, pix_op, qpix_op, | |
1687 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
936 | 1688 }else if(s->mspel){ |
1689 ff_mspel_motion(s, dest_y, dest_cb, dest_cr, | |
1690 ref_picture, pix_op, | |
1691 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
255 | 1692 }else{ |
1693 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
1694 ref_picture, 0, | |
1695 0, pix_op, | |
1696 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
1697 } | |
0 | 1698 break; |
1699 case MV_TYPE_8X8: | |
673 | 1700 mx = 0; |
1701 my = 0; | |
1702 if(s->quarter_sample){ | |
1703 for(i=0;i<4;i++) { | |
1704 motion_x = s->mv[dir][i][0]; | |
1705 motion_y = s->mv[dir][i][1]; | |
0 | 1706 |
673 | 1707 dxy = ((motion_y & 3) << 2) | (motion_x & 3); |
1708 src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8; | |
1709 src_y = mb_y * 16 + (motion_y >> 2) + (i >>1) * 8; | |
1710 | |
1711 /* WARNING: do no forget half pels */ | |
1712 src_x = clip(src_x, -16, s->width); | |
1713 if (src_x == s->width) | |
1714 dxy &= ~3; | |
1715 src_y = clip(src_y, -16, s->height); | |
1716 if (src_y == s->height) | |
1717 dxy &= ~12; | |
0 | 1718 |
673 | 1719 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); |
1720 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
1721 if(src_x<0 || src_y<0 || src_x + (motion_x&3) + 8 > s->h_edge_pos | |
1722 || src_y + (motion_y&3) + 8 > s->v_edge_pos){ | |
936 | 1723 ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos); |
673 | 1724 ptr= s->edge_emu_buffer; |
1725 } | |
1726 } | |
1727 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; | |
1728 qpix_op[1][dxy](dest, ptr, s->linesize); | |
1729 | |
1730 mx += s->mv[dir][i][0]/2; | |
1731 my += s->mv[dir][i][1]/2; | |
1732 } | |
1733 }else{ | |
1734 for(i=0;i<4;i++) { | |
1735 motion_x = s->mv[dir][i][0]; | |
1736 motion_y = s->mv[dir][i][1]; | |
1737 | |
1738 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
1739 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; | |
1740 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8; | |
0 | 1741 |
673 | 1742 /* WARNING: do no forget half pels */ |
1743 src_x = clip(src_x, -16, s->width); | |
1744 if (src_x == s->width) | |
1745 dxy &= ~1; | |
1746 src_y = clip(src_y, -16, s->height); | |
1747 if (src_y == s->height) | |
1748 dxy &= ~2; | |
1749 | |
1750 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); | |
1751 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
1752 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos | |
1753 || src_y + (motion_y&1) + 8 > s->v_edge_pos){ | |
936 | 1754 ff_emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos); |
673 | 1755 ptr= s->edge_emu_buffer; |
1756 } | |
554 | 1757 } |
673 | 1758 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; |
1759 pix_op[1][dxy](dest, ptr, s->linesize, 8); | |
1760 | |
1761 mx += s->mv[dir][i][0]; | |
1762 my += s->mv[dir][i][1]; | |
554 | 1763 } |
0 | 1764 } |
673 | 1765 |
485 | 1766 if(s->flags&CODEC_FLAG_GRAY) break; |
0 | 1767 /* In case of 8X8, we construct a single chroma motion vector |
1768 with a special rounding */ | |
1769 for(i=0;i<4;i++) { | |
1770 } | |
1771 if (mx >= 0) | |
1772 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
1773 else { | |
1774 mx = -mx; | |
1775 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
1776 } | |
1777 if (my >= 0) | |
1778 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
1779 else { | |
1780 my = -my; | |
1781 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
1782 } | |
1783 dxy = ((my & 1) << 1) | (mx & 1); | |
1784 mx >>= 1; | |
1785 my >>= 1; | |
1786 | |
1787 src_x = mb_x * 8 + mx; | |
1788 src_y = mb_y * 8 + my; | |
1789 src_x = clip(src_x, -8, s->width/2); | |
1790 if (src_x == s->width/2) | |
1791 dxy &= ~1; | |
1792 src_y = clip(src_y, -8, s->height/2); | |
1793 if (src_y == s->height/2) | |
1794 dxy &= ~2; | |
1795 | |
556 | 1796 offset = (src_y * (s->uvlinesize)) + src_x; |
0 | 1797 ptr = ref_picture[1] + offset; |
554 | 1798 if(s->flags&CODEC_FLAG_EMU_EDGE){ |
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1799 if(src_x<0 || src_y<0 || src_x + (dxy &1) + 8 > s->h_edge_pos>>1 |
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1800 || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){ |
936 | 1801 ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); |
554 | 1802 ptr= s->edge_emu_buffer; |
1803 emu=1; | |
1804 } | |
1805 } | |
651 | 1806 pix_op[1][dxy](dest_cb, ptr, s->uvlinesize, 8); |
554 | 1807 |
0 | 1808 ptr = ref_picture[2] + offset; |
554 | 1809 if(emu){ |
936 | 1810 ff_emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); |
554 | 1811 ptr= s->edge_emu_buffer; |
1812 } | |
651 | 1813 pix_op[1][dxy](dest_cr, ptr, s->uvlinesize, 8); |
0 | 1814 break; |
1815 case MV_TYPE_FIELD: | |
1816 if (s->picture_structure == PICT_FRAME) { | |
671 | 1817 if(s->quarter_sample){ |
1818 /* top field */ | |
1819 qpel_motion(s, dest_y, dest_cb, dest_cr, 0, | |
1820 ref_picture, s->field_select[dir][0] ? s->linesize : 0, | |
1821 1, pix_op, qpix_op, | |
1822 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
1823 /* bottom field */ | |
1824 qpel_motion(s, dest_y, dest_cb, dest_cr, s->linesize, | |
1825 ref_picture, s->field_select[dir][1] ? s->linesize : 0, | |
1826 1, pix_op, qpix_op, | |
1827 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
1828 }else{ | |
1829 /* top field */ | |
1830 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
1831 ref_picture, s->field_select[dir][0] ? s->linesize : 0, | |
1832 1, pix_op, | |
1833 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
1834 /* bottom field */ | |
1835 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize, | |
1836 ref_picture, s->field_select[dir][1] ? s->linesize : 0, | |
1837 1, pix_op, | |
1838 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
1839 } | |
0 | 1840 } else { |
1841 | |
1842 | |
1843 } | |
1844 break; | |
1845 } | |
1846 } | |
1847 | |
1848 | |
1849 /* put block[] to dest[] */ | |
1850 static inline void put_dct(MpegEncContext *s, | |
1851 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
1852 { | |
711 | 1853 s->dct_unquantize(s, block, i, s->qscale); |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
1854 s->idct_put (dest, line_size, block); |
0 | 1855 } |
1856 | |
1857 /* add block[] to dest[] */ | |
1858 static inline void add_dct(MpegEncContext *s, | |
1859 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
1860 { | |
1861 if (s->block_last_index[i] >= 0) { | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
1862 s->idct_add (dest, line_size, block); |
481 | 1863 } |
1864 } | |
1865 | |
1866 static inline void add_dequant_dct(MpegEncContext *s, | |
1867 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
1868 { | |
1869 if (s->block_last_index[i] >= 0) { | |
1870 s->dct_unquantize(s, block, i, s->qscale); | |
324 | 1871 |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
1872 s->idct_add (dest, line_size, block); |
0 | 1873 } |
1874 } | |
1875 | |
456 | 1876 /** |
1877 * cleans dc, ac, coded_block for the current non intra MB | |
1878 */ | |
1879 void ff_clean_intra_table_entries(MpegEncContext *s) | |
1880 { | |
1881 int wrap = s->block_wrap[0]; | |
1882 int xy = s->block_index[0]; | |
1883 | |
1884 s->dc_val[0][xy ] = | |
1885 s->dc_val[0][xy + 1 ] = | |
1886 s->dc_val[0][xy + wrap] = | |
1887 s->dc_val[0][xy + 1 + wrap] = 1024; | |
1888 /* ac pred */ | |
1889 memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16)); | |
1890 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16)); | |
1891 if (s->msmpeg4_version>=3) { | |
1892 s->coded_block[xy ] = | |
1893 s->coded_block[xy + 1 ] = | |
1894 s->coded_block[xy + wrap] = | |
1895 s->coded_block[xy + 1 + wrap] = 0; | |
1896 } | |
1897 /* chroma */ | |
1898 wrap = s->block_wrap[4]; | |
1899 xy = s->mb_x + 1 + (s->mb_y + 1) * wrap; | |
1900 s->dc_val[1][xy] = | |
1901 s->dc_val[2][xy] = 1024; | |
1902 /* ac pred */ | |
1903 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16)); | |
1904 memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16)); | |
1905 | |
1906 s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0; | |
1907 } | |
1908 | |
0 | 1909 /* generic function called after a macroblock has been parsed by the |
1910 decoder or after it has been encoded by the encoder. | |
1911 | |
1912 Important variables used: | |
1913 s->mb_intra : true if intra macroblock | |
1914 s->mv_dir : motion vector direction | |
1915 s->mv_type : motion vector type | |
1916 s->mv : motion vector | |
1917 s->interlaced_dct : true if interlaced dct used (mpeg2) | |
1918 */ | |
1919 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
1920 { | |
244 | 1921 int mb_x, mb_y; |
481 | 1922 const int mb_xy = s->mb_y * s->mb_width + s->mb_x; |
0 | 1923 |
1924 mb_x = s->mb_x; | |
1925 mb_y = s->mb_y; | |
1926 | |
903 | 1927 s->current_picture.qscale_table[mb_xy]= s->qscale; |
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1928 |
0 | 1929 /* update DC predictors for P macroblocks */ |
1930 if (!s->mb_intra) { | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1931 if (s->h263_pred || s->h263_aic) { |
481 | 1932 if(s->mbintra_table[mb_xy]) |
456 | 1933 ff_clean_intra_table_entries(s); |
0 | 1934 } else { |
456 | 1935 s->last_dc[0] = |
1936 s->last_dc[1] = | |
0 | 1937 s->last_dc[2] = 128 << s->intra_dc_precision; |
1938 } | |
1939 } | |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1940 else if (s->h263_pred || s->h263_aic) |
481 | 1941 s->mbintra_table[mb_xy]=1; |
191
883f184537e6
AC table reset (memset) optimization - patch by Michael Niedermayer <michaelni@gmx.at>
uid46427
parents:
189
diff
changeset
|
1942 |
280 | 1943 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */ |
481 | 1944 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here |
936 | 1945 //FIXME a lot of thet is only needed for !low_delay |
481 | 1946 const int wrap = s->block_wrap[0]; |
1947 const int xy = s->block_index[0]; | |
664 | 1948 const int mb_index= s->mb_x + s->mb_y*s->mb_width; |
619
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1949 if(s->mv_type == MV_TYPE_8X8){ |
664 | 1950 s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_4MV; |
619
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1951 } else { |
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1952 int motion_x, motion_y; |
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1953 if (s->mb_intra) { |
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1954 motion_x = 0; |
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1955 motion_y = 0; |
664 | 1956 if(s->co_located_type_table) |
1957 s->co_located_type_table[mb_index]= 0; | |
619
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1958 } else if (s->mv_type == MV_TYPE_16X16) { |
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1959 motion_x = s->mv[0][0][0]; |
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1960 motion_y = s->mv[0][0][1]; |
664 | 1961 if(s->co_located_type_table) |
1962 s->co_located_type_table[mb_index]= 0; | |
619
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1963 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ { |
664 | 1964 int i; |
619
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1965 motion_x = s->mv[0][0][0] + s->mv[0][1][0]; |
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1966 motion_y = s->mv[0][0][1] + s->mv[0][1][1]; |
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1967 motion_x = (motion_x>>1) | (motion_x&1); |
664 | 1968 for(i=0; i<2; i++){ |
1969 s->field_mv_table[mb_index][i][0]= s->mv[0][i][0]; | |
1970 s->field_mv_table[mb_index][i][1]= s->mv[0][i][1]; | |
1971 s->field_select_table[mb_index][i]= s->field_select[0][i]; | |
1972 } | |
1973 s->co_located_type_table[mb_index]= CO_LOCATED_TYPE_FIELDMV; | |
619
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1974 } |
0 | 1975 /* no update if 8X8 because it has been done during parsing */ |
244 | 1976 s->motion_val[xy][0] = motion_x; |
1977 s->motion_val[xy][1] = motion_y; | |
1978 s->motion_val[xy + 1][0] = motion_x; | |
1979 s->motion_val[xy + 1][1] = motion_y; | |
1980 s->motion_val[xy + wrap][0] = motion_x; | |
1981 s->motion_val[xy + wrap][1] = motion_y; | |
1982 s->motion_val[xy + 1 + wrap][0] = motion_x; | |
1983 s->motion_val[xy + 1 + wrap][1] = motion_y; | |
0 | 1984 } |
1985 } | |
1986 | |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
1987 if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { //FIXME precalc |
0 | 1988 UINT8 *dest_y, *dest_cb, *dest_cr; |
481 | 1989 int dct_linesize, dct_offset; |
651 | 1990 op_pixels_func (*op_pix)[4]; |
1991 qpel_mc_func (*op_qpix)[16]; | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1992 |
903 | 1993 /* avoid copy if macroblock skipped in last frame too */ |
324 | 1994 if (s->pict_type != B_TYPE) { |
903 | 1995 s->current_picture.mbskip_table[mb_xy]= s->mb_skiped; |
1996 } | |
1997 | |
1998 /* skip only during decoding as we might trash the buffers during encoding a bit */ | |
1999 if(!s->encoding){ | |
481 | 2000 UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy]; |
903 | 2001 const int age= s->current_picture.age; |
2002 | |
2003 assert(age); | |
2004 | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
2005 if (s->mb_skiped) { |
903 | 2006 s->mb_skiped= 0; |
2007 assert(s->pict_type!=I_TYPE); | |
2008 | |
556 | 2009 (*mbskip_ptr) ++; /* indicate that this time we skiped it */ |
2010 if(*mbskip_ptr >99) *mbskip_ptr= 99; | |
2011 | |
903 | 2012 /* if previous was skipped too, then nothing to do ! */ |
2013 if (*mbskip_ptr >= age){ | |
2014 //if(s->pict_type!=B_TYPE && s->mb_x==0) printf("\n"); | |
2015 //if(s->pict_type!=B_TYPE) printf("%d%d ", *mbskip_ptr, age); | |
2016 if(s->pict_type!=B_TYPE) return; | |
2017 if(s->avctx->draw_horiz_band==NULL && *mbskip_ptr > age) return; | |
2018 /* we dont draw complete frames here so we cant skip */ | |
2019 } | |
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
2020 } else { |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
2021 *mbskip_ptr = 0; /* not skipped */ |
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
2022 } |
903 | 2023 }else |
2024 s->mb_skiped= 0; | |
0 | 2025 |
717 | 2026 if(s->pict_type==B_TYPE && s->avctx->draw_horiz_band){ |
903 | 2027 dest_y = s->current_picture.data[0] + mb_x * 16; |
2028 dest_cb = s->current_picture.data[1] + mb_x * 8; | |
2029 dest_cr = s->current_picture.data[2] + mb_x * 8; | |
717 | 2030 }else{ |
903 | 2031 dest_y = s->current_picture.data[0] + (mb_y * 16* s->linesize ) + mb_x * 16; |
2032 dest_cb = s->current_picture.data[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; | |
2033 dest_cr = s->current_picture.data[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; | |
717 | 2034 } |
0 | 2035 |
2036 if (s->interlaced_dct) { | |
2037 dct_linesize = s->linesize * 2; | |
2038 dct_offset = s->linesize; | |
2039 } else { | |
2040 dct_linesize = s->linesize; | |
2041 dct_offset = s->linesize * 8; | |
2042 } | |
2043 | |
2044 if (!s->mb_intra) { | |
2045 /* motion handling */ | |
456 | 2046 /* decoding or more than one mb_type (MC was allready done otherwise) */ |
481 | 2047 if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){ |
327 | 2048 if ((!s->no_rounding) || s->pict_type==B_TYPE){ |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2049 op_pix = s->dsp.put_pixels_tab; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2050 op_qpix= s->dsp.put_qpel_pixels_tab; |
324 | 2051 }else{ |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2052 op_pix = s->dsp.put_no_rnd_pixels_tab; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2053 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab; |
324 | 2054 } |
0 | 2055 |
324 | 2056 if (s->mv_dir & MV_DIR_FORWARD) { |
903 | 2057 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix); |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2058 op_pix = s->dsp.avg_pixels_tab; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2059 op_qpix= s->dsp.avg_qpel_pixels_tab; |
324 | 2060 } |
2061 if (s->mv_dir & MV_DIR_BACKWARD) { | |
903 | 2062 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix); |
324 | 2063 } |
0 | 2064 } |
2065 | |
481 | 2066 /* skip dequant / idct if we are really late ;) */ |
814 | 2067 if(s->hurry_up>1) return; |
481 | 2068 |
0 | 2069 /* add dct residue */ |
711 | 2070 if(s->encoding || !( s->mpeg2 || s->h263_msmpeg4 || s->codec_id==CODEC_ID_MPEG1VIDEO |
2071 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){ | |
481 | 2072 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize); |
2073 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
2074 add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
2075 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
0 | 2076 |
485 | 2077 if(!(s->flags&CODEC_FLAG_GRAY)){ |
556 | 2078 add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize); |
2079 add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize); | |
485 | 2080 } |
936 | 2081 } else if(s->codec_id != CODEC_ID_WMV2){ |
481 | 2082 add_dct(s, block[0], 0, dest_y, dct_linesize); |
2083 add_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
2084 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
2085 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
2086 | |
485 | 2087 if(!(s->flags&CODEC_FLAG_GRAY)){ |
556 | 2088 add_dct(s, block[4], 4, dest_cb, s->uvlinesize); |
2089 add_dct(s, block[5], 5, dest_cr, s->uvlinesize); | |
485 | 2090 } |
936 | 2091 } else{ |
2092 ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr); | |
481 | 2093 } |
0 | 2094 } else { |
2095 /* dct only in intra block */ | |
711 | 2096 if(s->encoding || !(s->mpeg2 || s->codec_id==CODEC_ID_MPEG1VIDEO)){ |
2097 put_dct(s, block[0], 0, dest_y, dct_linesize); | |
2098 put_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
2099 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
2100 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
0 | 2101 |
711 | 2102 if(!(s->flags&CODEC_FLAG_GRAY)){ |
2103 put_dct(s, block[4], 4, dest_cb, s->uvlinesize); | |
2104 put_dct(s, block[5], 5, dest_cr, s->uvlinesize); | |
2105 } | |
2106 }else{ | |
2107 s->idct_put(dest_y , dct_linesize, block[0]); | |
2108 s->idct_put(dest_y + 8, dct_linesize, block[1]); | |
2109 s->idct_put(dest_y + dct_offset , dct_linesize, block[2]); | |
2110 s->idct_put(dest_y + dct_offset + 8, dct_linesize, block[3]); | |
2111 | |
2112 if(!(s->flags&CODEC_FLAG_GRAY)){ | |
2113 s->idct_put(dest_cb, s->uvlinesize, block[4]); | |
2114 s->idct_put(dest_cr, s->uvlinesize, block[5]); | |
2115 } | |
485 | 2116 } |
0 | 2117 } |
2118 } | |
294 | 2119 } |
2120 | |
605
40874804a5af
same dc skip behavior for chroma & luma elimination, its confusing otherwise imho
michaelni
parents:
604
diff
changeset
|
2121 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold) |
456 | 2122 { |
2123 static const char tab[64]= | |
2124 {3,2,2,1,1,1,1,1, | |
2125 1,1,1,1,1,1,1,1, | |
2126 1,1,1,1,1,1,1,1, | |
2127 0,0,0,0,0,0,0,0, | |
2128 0,0,0,0,0,0,0,0, | |
2129 0,0,0,0,0,0,0,0, | |
2130 0,0,0,0,0,0,0,0, | |
2131 0,0,0,0,0,0,0,0}; | |
2132 int score=0; | |
2133 int run=0; | |
2134 int i; | |
2135 DCTELEM *block= s->block[n]; | |
2136 const int last_index= s->block_last_index[n]; | |
605
40874804a5af
same dc skip behavior for chroma & luma elimination, its confusing otherwise imho
michaelni
parents:
604
diff
changeset
|
2137 int skip_dc; |
456 | 2138 |
604
4344cc8033bd
skip blocks with small chroma dc too (if the user wants it) needed to avoid IDCT(input coeffs !=0) == 0 problems which cause catastrophic error accumulation at qp=1
michaelni
parents:
599
diff
changeset
|
2139 if(threshold<0){ |
4344cc8033bd
skip blocks with small chroma dc too (if the user wants it) needed to avoid IDCT(input coeffs !=0) == 0 problems which cause catastrophic error accumulation at qp=1
michaelni
parents:
599
diff
changeset
|
2140 skip_dc=0; |
4344cc8033bd
skip blocks with small chroma dc too (if the user wants it) needed to avoid IDCT(input coeffs !=0) == 0 problems which cause catastrophic error accumulation at qp=1
michaelni
parents:
599
diff
changeset
|
2141 threshold= -threshold; |
605
40874804a5af
same dc skip behavior for chroma & luma elimination, its confusing otherwise imho
michaelni
parents:
604
diff
changeset
|
2142 }else |
40874804a5af
same dc skip behavior for chroma & luma elimination, its confusing otherwise imho
michaelni
parents:
604
diff
changeset
|
2143 skip_dc=1; |
604
4344cc8033bd
skip blocks with small chroma dc too (if the user wants it) needed to avoid IDCT(input coeffs !=0) == 0 problems which cause catastrophic error accumulation at qp=1
michaelni
parents:
599
diff
changeset
|
2144 |
456 | 2145 /* are all which we could set to zero are allready zero? */ |
2146 if(last_index<=skip_dc - 1) return; | |
2147 | |
2148 for(i=0; i<=last_index; i++){ | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2149 const int j = s->intra_scantable.permutated[i]; |
456 | 2150 const int level = ABS(block[j]); |
2151 if(level==1){ | |
2152 if(skip_dc && i==0) continue; | |
2153 score+= tab[run]; | |
2154 run=0; | |
2155 }else if(level>1){ | |
2156 return; | |
2157 }else{ | |
2158 run++; | |
2159 } | |
2160 } | |
2161 if(score >= threshold) return; | |
2162 for(i=skip_dc; i<=last_index; i++){ | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2163 const int j = s->intra_scantable.permutated[i]; |
456 | 2164 block[j]=0; |
2165 } | |
2166 if(block[0]) s->block_last_index[n]= 0; | |
2167 else s->block_last_index[n]= -1; | |
2168 } | |
2169 | |
344 | 2170 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index) |
2171 { | |
2172 int i; | |
2173 const int maxlevel= s->max_qcoeff; | |
2174 const int minlevel= s->min_qcoeff; | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2175 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2176 if(s->mb_intra){ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2177 i=1; //skip clipping of intra dc |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2178 }else |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2179 i=0; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2180 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2181 for(;i<=last_index; i++){ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2182 const int j= s->intra_scantable.permutated[i]; |
344 | 2183 int level = block[j]; |
2184 | |
2185 if (level>maxlevel) level=maxlevel; | |
2186 else if(level<minlevel) level=minlevel; | |
2187 block[j]= level; | |
2188 } | |
2189 } | |
324 | 2190 |
697 | 2191 static inline void requantize_coeffs(MpegEncContext *s, DCTELEM block[64], int oldq, int newq, int n) |
2192 { | |
2193 int i; | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2194 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2195 if(s->mb_intra){ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2196 i=1; //skip clipping of intra dc |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2197 //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...) |
697 | 2198 }else |
2199 i=0; | |
2200 | |
2201 for(;i<=s->block_last_index[n]; i++){ | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2202 const int j = s->intra_scantable.permutated[i]; |
697 | 2203 int level = block[j]; |
2204 | |
2205 block[j]= ROUNDED_DIV(level*oldq, newq); | |
2206 } | |
2207 | |
2208 for(i=s->block_last_index[n]; i>=0; i--){ | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2209 const int j = s->intra_scantable.permutated[i]; |
697 | 2210 if(block[j]) break; |
2211 } | |
2212 s->block_last_index[n]= i; | |
2213 } | |
2214 | |
2215 static inline void auto_requantize_coeffs(MpegEncContext *s, DCTELEM block[6][64]) | |
2216 { | |
2217 int i,n, newq; | |
2218 const int maxlevel= s->max_qcoeff; | |
2219 const int minlevel= s->min_qcoeff; | |
2220 int largest=0, smallest=0; | |
2221 | |
2222 assert(s->adaptive_quant); | |
2223 | |
2224 for(n=0; n<6; n++){ | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2225 if(s->mb_intra){ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2226 i=1; //skip clipping of intra dc |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2227 //FIXME requantize, note (mpeg1/h263/h263p-aic dont need it,...) |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2228 }else |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2229 i=0; |
697 | 2230 |
2231 for(;i<=s->block_last_index[n]; i++){ | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2232 const int j = s->intra_scantable.permutated[i]; |
697 | 2233 int level = block[n][j]; |
2234 if(largest < level) largest = level; | |
2235 if(smallest > level) smallest= level; | |
2236 } | |
2237 } | |
2238 | |
2239 for(newq=s->qscale+1; newq<32; newq++){ | |
2240 if( ROUNDED_DIV(smallest*s->qscale, newq) >= minlevel | |
2241 && ROUNDED_DIV(largest *s->qscale, newq) <= maxlevel) | |
2242 break; | |
2243 } | |
2244 | |
2245 if(s->out_format==FMT_H263){ | |
2246 /* h263 like formats cannot change qscale by more than 2 easiely */ | |
2247 if(s->avctx->qmin + 2 < newq) | |
2248 newq= s->avctx->qmin + 2; | |
2249 } | |
2250 | |
2251 for(n=0; n<6; n++){ | |
2252 requantize_coeffs(s, block[n], s->qscale, newq, n); | |
2253 clip_coeffs(s, block[n], s->block_last_index[n]); | |
2254 } | |
2255 | |
2256 s->dquant+= newq - s->qscale; | |
2257 s->qscale= newq; | |
2258 } | |
2259 #if 0 | |
2260 static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize | |
2261 int score=0; | |
2262 int x,y; | |
2263 | |
2264 for(y=0; y<7; y++){ | |
2265 for(x=0; x<16; x+=4){ | |
2266 score+= ABS(s[x ] - s[x +stride]) + ABS(s[x+1] - s[x+1+stride]) | |
2267 +ABS(s[x+2] - s[x+2+stride]) + ABS(s[x+3] - s[x+3+stride]); | |
2268 } | |
2269 s+= stride; | |
2270 } | |
2271 | |
2272 return score; | |
2273 } | |
2274 | |
2275 static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize | |
2276 int score=0; | |
2277 int x,y; | |
2278 | |
2279 for(y=0; y<7; y++){ | |
2280 for(x=0; x<16; x++){ | |
2281 score+= ABS(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]); | |
2282 } | |
2283 s1+= stride; | |
2284 s2+= stride; | |
2285 } | |
2286 | |
2287 return score; | |
2288 } | |
2289 #else | |
2290 #define SQ(a) ((a)*(a)) | |
2291 | |
2292 static int pix_vcmp16x8(UINT8 *s, int stride){ //FIXME move to dsputil & optimize | |
2293 int score=0; | |
2294 int x,y; | |
2295 | |
2296 for(y=0; y<7; y++){ | |
2297 for(x=0; x<16; x+=4){ | |
2298 score+= SQ(s[x ] - s[x +stride]) + SQ(s[x+1] - s[x+1+stride]) | |
2299 +SQ(s[x+2] - s[x+2+stride]) + SQ(s[x+3] - s[x+3+stride]); | |
2300 } | |
2301 s+= stride; | |
2302 } | |
2303 | |
2304 return score; | |
2305 } | |
2306 | |
2307 static int pix_diff_vcmp16x8(UINT8 *s1, UINT8*s2, int stride){ //FIXME move to dsputil & optimize | |
2308 int score=0; | |
2309 int x,y; | |
2310 | |
2311 for(y=0; y<7; y++){ | |
2312 for(x=0; x<16; x++){ | |
2313 score+= SQ(s1[x ] - s2[x ] - s1[x +stride] + s2[x +stride]); | |
2314 } | |
2315 s1+= stride; | |
2316 s2+= stride; | |
2317 } | |
2318 | |
2319 return score; | |
2320 } | |
2321 | |
2322 #endif | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2323 |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2324 void ff_draw_horiz_band(MpegEncContext *s){ |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2325 if ( s->avctx->draw_horiz_band |
924 | 2326 && (s->last_picture.data[0] || s->low_delay) ) { |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2327 UINT8 *src_ptr[3]; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2328 int y, h, offset; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2329 y = s->mb_y * 16; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2330 h = s->height - y; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2331 if (h > 16) |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2332 h = 16; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2333 |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2334 if(s->pict_type==B_TYPE) |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2335 offset = 0; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2336 else |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2337 offset = y * s->linesize; |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2338 |
924 | 2339 if(s->pict_type==B_TYPE || s->low_delay){ |
903 | 2340 src_ptr[0] = s->current_picture.data[0] + offset; |
2341 src_ptr[1] = s->current_picture.data[1] + (offset >> 2); | |
2342 src_ptr[2] = s->current_picture.data[2] + (offset >> 2); | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2343 } else { |
903 | 2344 src_ptr[0] = s->last_picture.data[0] + offset; |
2345 src_ptr[1] = s->last_picture.data[1] + (offset >> 2); | |
2346 src_ptr[2] = s->last_picture.data[2] + (offset >> 2); | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2347 } |
813 | 2348 emms_c(); |
2349 | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2350 s->avctx->draw_horiz_band(s->avctx, src_ptr, s->linesize, |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2351 y, s->width, h); |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2352 } |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2353 } |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2354 |
324 | 2355 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) |
294 | 2356 { |
2357 const int mb_x= s->mb_x; | |
2358 const int mb_y= s->mb_y; | |
2359 int i; | |
456 | 2360 int skip_dct[6]; |
697 | 2361 int dct_offset = s->linesize*8; //default for progressive frames |
2362 | |
456 | 2363 for(i=0; i<6; i++) skip_dct[i]=0; |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2364 |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2365 if(s->adaptive_quant){ |
903 | 2366 s->dquant= s->current_picture.qscale_table[mb_x + mb_y*s->mb_width] - s->qscale; |
697 | 2367 |
2368 if(s->out_format==FMT_H263){ | |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2369 if (s->dquant> 2) s->dquant= 2; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2370 else if(s->dquant<-2) s->dquant=-2; |
697 | 2371 } |
2372 | |
2373 if(s->codec_id==CODEC_ID_MPEG4){ | |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2374 if(!s->mb_intra){ |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2375 assert(s->dquant==0 || s->mv_type!=MV_TYPE_8X8); |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2376 |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2377 if(s->mv_dir&MV_DIRECT) |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2378 s->dquant=0; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2379 } |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2380 } |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2381 s->qscale+= s->dquant; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2382 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ]; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2383 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ]; |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2384 } |
294 | 2385 |
324 | 2386 if (s->mb_intra) { |
2387 UINT8 *ptr; | |
697 | 2388 int wrap_y; |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2389 int emu=0; |
294 | 2390 |
697 | 2391 wrap_y = s->linesize; |
903 | 2392 ptr = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16; |
697 | 2393 |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2394 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){ |
936 | 2395 ff_emulated_edge_mc(s, ptr, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height); |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2396 ptr= s->edge_emu_buffer; |
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2397 emu=1; |
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2398 } |
697 | 2399 |
2400 if(s->flags&CODEC_FLAG_INTERLACED_DCT){ | |
2401 int progressive_score, interlaced_score; | |
2402 | |
2403 progressive_score= pix_vcmp16x8(ptr, wrap_y ) + pix_vcmp16x8(ptr + wrap_y*8, wrap_y ); | |
2404 interlaced_score = pix_vcmp16x8(ptr, wrap_y*2) + pix_vcmp16x8(ptr + wrap_y , wrap_y*2); | |
2405 | |
2406 if(progressive_score > interlaced_score + 100){ | |
2407 s->interlaced_dct=1; | |
2408 | |
2409 dct_offset= wrap_y; | |
2410 wrap_y<<=1; | |
2411 }else | |
2412 s->interlaced_dct=0; | |
2413 } | |
2414 | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2415 s->dsp.get_pixels(s->block[0], ptr , wrap_y); |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2416 s->dsp.get_pixels(s->block[1], ptr + 8, wrap_y); |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2417 s->dsp.get_pixels(s->block[2], ptr + dct_offset , wrap_y); |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2418 s->dsp.get_pixels(s->block[3], ptr + dct_offset + 8, wrap_y); |
294 | 2419 |
487 | 2420 if(s->flags&CODEC_FLAG_GRAY){ |
2421 skip_dct[4]= 1; | |
2422 skip_dct[5]= 1; | |
2423 }else{ | |
697 | 2424 int wrap_c = s->uvlinesize; |
903 | 2425 ptr = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8; |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2426 if(emu){ |
936 | 2427 ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1); |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2428 ptr= s->edge_emu_buffer; |
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2429 } |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2430 s->dsp.get_pixels(s->block[4], ptr, wrap_c); |
294 | 2431 |
903 | 2432 ptr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8; |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2433 if(emu){ |
936 | 2434 ff_emulated_edge_mc(s, ptr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1); |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2435 ptr= s->edge_emu_buffer; |
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2436 } |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2437 s->dsp.get_pixels(s->block[5], ptr, wrap_c); |
487 | 2438 } |
324 | 2439 }else{ |
651 | 2440 op_pixels_func (*op_pix)[4]; |
2441 qpel_mc_func (*op_qpix)[16]; | |
324 | 2442 UINT8 *dest_y, *dest_cb, *dest_cr; |
456 | 2443 UINT8 *ptr_y, *ptr_cb, *ptr_cr; |
2444 int wrap_y, wrap_c; | |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2445 int emu=0; |
294 | 2446 |
903 | 2447 dest_y = s->current_picture.data[0] + (mb_y * 16 * s->linesize ) + mb_x * 16; |
2448 dest_cb = s->current_picture.data[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; | |
2449 dest_cr = s->current_picture.data[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; | |
456 | 2450 wrap_y = s->linesize; |
697 | 2451 wrap_c = s->uvlinesize; |
903 | 2452 ptr_y = s->new_picture.data[0] + (mb_y * 16 * wrap_y) + mb_x * 16; |
2453 ptr_cb = s->new_picture.data[1] + (mb_y * 8 * wrap_c) + mb_x * 8; | |
2454 ptr_cr = s->new_picture.data[2] + (mb_y * 8 * wrap_c) + mb_x * 8; | |
324 | 2455 |
327 | 2456 if ((!s->no_rounding) || s->pict_type==B_TYPE){ |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2457 op_pix = s->dsp.put_pixels_tab; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2458 op_qpix= s->dsp.put_qpel_pixels_tab; |
295 | 2459 }else{ |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2460 op_pix = s->dsp.put_no_rnd_pixels_tab; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2461 op_qpix= s->dsp.put_no_rnd_qpel_pixels_tab; |
324 | 2462 } |
295 | 2463 |
324 | 2464 if (s->mv_dir & MV_DIR_FORWARD) { |
903 | 2465 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.data, op_pix, op_qpix); |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2466 op_pix = s->dsp.avg_pixels_tab; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2467 op_qpix= s->dsp.avg_qpel_pixels_tab; |
324 | 2468 } |
2469 if (s->mv_dir & MV_DIR_BACKWARD) { | |
903 | 2470 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.data, op_pix, op_qpix); |
324 | 2471 } |
295 | 2472 |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2473 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){ |
936 | 2474 ff_emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height); |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2475 ptr_y= s->edge_emu_buffer; |
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2476 emu=1; |
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2477 } |
697 | 2478 |
2479 if(s->flags&CODEC_FLAG_INTERLACED_DCT){ | |
2480 int progressive_score, interlaced_score; | |
2481 | |
2482 progressive_score= pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y ) | |
2483 + pix_diff_vcmp16x8(ptr_y + wrap_y*8, dest_y + wrap_y*8, wrap_y ); | |
2484 interlaced_score = pix_diff_vcmp16x8(ptr_y , dest_y , wrap_y*2) | |
2485 + pix_diff_vcmp16x8(ptr_y + wrap_y , dest_y + wrap_y , wrap_y*2); | |
2486 | |
2487 if(progressive_score > interlaced_score + 600){ | |
2488 s->interlaced_dct=1; | |
2489 | |
2490 dct_offset= wrap_y; | |
2491 wrap_y<<=1; | |
2492 }else | |
2493 s->interlaced_dct=0; | |
2494 } | |
2495 | |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2496 s->dsp.diff_pixels(s->block[0], ptr_y , dest_y , wrap_y); |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2497 s->dsp.diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y); |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2498 s->dsp.diff_pixels(s->block[2], ptr_y + dct_offset , dest_y + dct_offset , wrap_y); |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2499 s->dsp.diff_pixels(s->block[3], ptr_y + dct_offset + 8, dest_y + dct_offset + 8, wrap_y); |
487 | 2500 |
2501 if(s->flags&CODEC_FLAG_GRAY){ | |
2502 skip_dct[4]= 1; | |
2503 skip_dct[5]= 1; | |
2504 }else{ | |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2505 if(emu){ |
936 | 2506 ff_emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1); |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2507 ptr_cb= s->edge_emu_buffer; |
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2508 } |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2509 s->dsp.diff_pixels(s->block[4], ptr_cb, dest_cb, wrap_c); |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2510 if(emu){ |
936 | 2511 ff_emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1); |
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2512 ptr_cr= s->edge_emu_buffer; |
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
2513 } |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2514 s->dsp.diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c); |
487 | 2515 } |
456 | 2516 /* pre quantization */ |
903 | 2517 if(s->current_picture.mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){ |
697 | 2518 //FIXME optimize |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2519 if(s->dsp.pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2520 if(s->dsp.pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2521 if(s->dsp.pix_abs8x8(ptr_y +dct_offset , dest_y +dct_offset , wrap_y) < 20*s->qscale) skip_dct[2]= 1; |
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2522 if(s->dsp.pix_abs8x8(ptr_y +dct_offset+ 8, dest_y +dct_offset+ 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1; |
899 | 2523 if(s->dsp.pix_abs8x8(ptr_cb , dest_cb , wrap_c) < 20*s->qscale) skip_dct[4]= 1; |
2524 if(s->dsp.pix_abs8x8(ptr_cr , dest_cr , wrap_c) < 20*s->qscale) skip_dct[5]= 1; | |
456 | 2525 #if 0 |
2526 { | |
2527 static int stat[7]; | |
2528 int num=0; | |
2529 for(i=0; i<6; i++) | |
2530 if(skip_dct[i]) num++; | |
2531 stat[num]++; | |
2532 | |
2533 if(s->mb_x==0 && s->mb_y==0){ | |
2534 for(i=0; i<7; i++){ | |
2535 printf("%6d %1d\n", stat[i], i); | |
2536 } | |
2537 } | |
2538 } | |
2539 #endif | |
2540 } | |
324 | 2541 |
294 | 2542 } |
2543 | |
2544 #if 0 | |
2545 { | |
2546 float adap_parm; | |
2547 | |
2548 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) / | |
2549 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0); | |
2550 | |
2551 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", | |
2552 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', | |
2553 s->qscale, adap_parm, s->qscale*adap_parm, | |
2554 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var); | |
2555 } | |
2556 #endif | |
2557 /* DCT & quantize */ | |
344 | 2558 if(s->out_format==FMT_MJPEG){ |
2559 for(i=0;i<6;i++) { | |
2560 int overflow; | |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
2561 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, 8, &overflow); |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2562 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); |
344 | 2563 } |
2564 }else{ | |
2565 for(i=0;i<6;i++) { | |
456 | 2566 if(!skip_dct[i]){ |
2567 int overflow; | |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
2568 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow); |
344 | 2569 // FIXME we could decide to change to quantizer instead of clipping |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2570 // JS: I don't think that would be a good idea it could lower quality instead |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2571 // of improve it. Just INTRADC clipping deserves changes in quantizer |
456 | 2572 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); |
2573 }else | |
2574 s->block_last_index[i]= -1; | |
344 | 2575 } |
456 | 2576 if(s->luma_elim_threshold && !s->mb_intra) |
2577 for(i=0; i<4; i++) | |
605
40874804a5af
same dc skip behavior for chroma & luma elimination, its confusing otherwise imho
michaelni
parents:
604
diff
changeset
|
2578 dct_single_coeff_elimination(s, i, s->luma_elim_threshold); |
456 | 2579 if(s->chroma_elim_threshold && !s->mb_intra) |
2580 for(i=4; i<6; i++) | |
605
40874804a5af
same dc skip behavior for chroma & luma elimination, its confusing otherwise imho
michaelni
parents:
604
diff
changeset
|
2581 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold); |
294 | 2582 } |
2583 | |
487 | 2584 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){ |
2585 s->block_last_index[4]= | |
2586 s->block_last_index[5]= 0; | |
2587 s->block[4][0]= | |
2588 s->block[5][0]= 128; | |
2589 } | |
2590 | |
815
78accc54493b
put a few large tables under #ifdef CONFIG_ENCODERS or dynamically allocate them
michaelni
parents:
814
diff
changeset
|
2591 #ifdef CONFIG_ENCODERS |
294 | 2592 /* huffman encode */ |
936 | 2593 switch(s->codec_id){ //FIXME funct ptr could be slightly faster |
2594 case CODEC_ID_MPEG1VIDEO: | |
2595 mpeg1_encode_mb(s, s->block, motion_x, motion_y); break; | |
2596 case CODEC_ID_MPEG4: | |
2597 mpeg4_encode_mb(s, s->block, motion_x, motion_y); break; | |
2598 case CODEC_ID_MSMPEG4V2: | |
2599 case CODEC_ID_MSMPEG4V3: | |
2600 case CODEC_ID_WMV1: | |
2601 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); break; | |
2602 case CODEC_ID_WMV2: | |
2603 ff_wmv2_encode_mb(s, s->block, motion_x, motion_y); break; | |
2604 case CODEC_ID_MJPEG: | |
2605 mjpeg_encode_mb(s, s->block); break; | |
2606 case CODEC_ID_H263: | |
2607 case CODEC_ID_H263P: | |
2608 case CODEC_ID_RV10: | |
2609 h263_encode_mb(s, s->block, motion_x, motion_y); break; | |
2610 default: | |
2611 assert(0); | |
294 | 2612 } |
815
78accc54493b
put a few large tables under #ifdef CONFIG_ENCODERS or dynamically allocate them
michaelni
parents:
814
diff
changeset
|
2613 #endif |
294 | 2614 } |
2615 | |
456 | 2616 void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length) |
294 | 2617 { |
326 | 2618 int bytes= length>>4; |
2619 int bits= length&15; | |
2620 int i; | |
2621 | |
456 | 2622 if(length==0) return; |
2623 | |
326 | 2624 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i])); |
2625 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits)); | |
0 | 2626 } |
2627 | |
456 | 2628 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){ |
326 | 2629 int i; |
2630 | |
2631 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | |
2632 | |
2633 /* mpeg1 */ | |
2634 d->mb_incr= s->mb_incr; | |
2635 for(i=0; i<3; i++) | |
2636 d->last_dc[i]= s->last_dc[i]; | |
2637 | |
2638 /* statistics */ | |
2639 d->mv_bits= s->mv_bits; | |
2640 d->i_tex_bits= s->i_tex_bits; | |
2641 d->p_tex_bits= s->p_tex_bits; | |
2642 d->i_count= s->i_count; | |
656
e47fa3e3f2d5
statistics for forw & back p-MBs instead of just one counter for both
michaelni
parents:
652
diff
changeset
|
2643 d->f_count= s->f_count; |
e47fa3e3f2d5
statistics for forw & back p-MBs instead of just one counter for both
michaelni
parents:
652
diff
changeset
|
2644 d->b_count= s->b_count; |
326 | 2645 d->skip_count= s->skip_count; |
2646 d->misc_bits= s->misc_bits; | |
329 | 2647 d->last_bits= 0; |
327 | 2648 |
2649 d->mb_skiped= s->mb_skiped; | |
912 | 2650 d->qscale= s->qscale; |
326 | 2651 } |
2652 | |
456 | 2653 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ |
326 | 2654 int i; |
2655 | |
2656 memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); | |
2657 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | |
2658 | |
2659 /* mpeg1 */ | |
2660 d->mb_incr= s->mb_incr; | |
2661 for(i=0; i<3; i++) | |
2662 d->last_dc[i]= s->last_dc[i]; | |
2663 | |
2664 /* statistics */ | |
2665 d->mv_bits= s->mv_bits; | |
2666 d->i_tex_bits= s->i_tex_bits; | |
2667 d->p_tex_bits= s->p_tex_bits; | |
2668 d->i_count= s->i_count; | |
656
e47fa3e3f2d5
statistics for forw & back p-MBs instead of just one counter for both
michaelni
parents:
652
diff
changeset
|
2669 d->f_count= s->f_count; |
e47fa3e3f2d5
statistics for forw & back p-MBs instead of just one counter for both
michaelni
parents:
652
diff
changeset
|
2670 d->b_count= s->b_count; |
326 | 2671 d->skip_count= s->skip_count; |
2672 d->misc_bits= s->misc_bits; | |
2673 | |
2674 d->mb_intra= s->mb_intra; | |
327 | 2675 d->mb_skiped= s->mb_skiped; |
326 | 2676 d->mv_type= s->mv_type; |
2677 d->mv_dir= s->mv_dir; | |
2678 d->pb= s->pb; | |
456 | 2679 if(s->data_partitioning){ |
2680 d->pb2= s->pb2; | |
2681 d->tex_pb= s->tex_pb; | |
2682 } | |
326 | 2683 d->block= s->block; |
2684 for(i=0; i<6; i++) | |
2685 d->block_last_index[i]= s->block_last_index[i]; | |
755 | 2686 d->interlaced_dct= s->interlaced_dct; |
912 | 2687 d->qscale= s->qscale; |
326 | 2688 } |
2689 | |
456 | 2690 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, |
2691 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2], | |
2692 int *dmin, int *next_block, int motion_x, int motion_y) | |
2693 { | |
2694 int bits_count; | |
2695 | |
2696 copy_context_before_encode(s, backup, type); | |
2697 | |
2698 s->block= s->blocks[*next_block]; | |
2699 s->pb= pb[*next_block]; | |
2700 if(s->data_partitioning){ | |
2701 s->pb2 = pb2 [*next_block]; | |
2702 s->tex_pb= tex_pb[*next_block]; | |
2703 } | |
2704 | |
2705 encode_mb(s, motion_x, motion_y); | |
2706 | |
2707 bits_count= get_bit_count(&s->pb); | |
2708 if(s->data_partitioning){ | |
2709 bits_count+= get_bit_count(&s->pb2); | |
2710 bits_count+= get_bit_count(&s->tex_pb); | |
2711 } | |
2712 | |
2713 if(bits_count<*dmin){ | |
2714 *dmin= bits_count; | |
2715 *next_block^=1; | |
2716 | |
2717 copy_context_after_encode(best, s, type); | |
2718 } | |
2719 } | |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2720 |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2721 static inline int sse(MpegEncContext *s, uint8_t *src1, uint8_t *src2, int w, int h, int stride){ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2722 uint32_t *sq = squareTbl + 256; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2723 int acc=0; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2724 int x,y; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2725 |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2726 if(w==16 && h==16) |
936 | 2727 return s->dsp.sse[0](NULL, src1, src2, stride); |
2728 else if(w==8 && h==8) | |
2729 return s->dsp.sse[1](NULL, src1, src2, stride); | |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2730 |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2731 for(y=0; y<h; y++){ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2732 for(x=0; x<w; x++){ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2733 acc+= sq[src1[x + y*stride] - src2[x + y*stride]]; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2734 } |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2735 } |
936 | 2736 |
2737 assert(acc>=0); | |
2738 | |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2739 return acc; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2740 } |
326 | 2741 |
0 | 2742 static void encode_picture(MpegEncContext *s, int picture_number) |
2743 { | |
766 | 2744 int mb_x, mb_y, pdif = 0; |
294 | 2745 int i; |
286 | 2746 int bits; |
326 | 2747 MpegEncContext best_s, backup_s; |
456 | 2748 UINT8 bit_buf[2][3000]; |
2749 UINT8 bit_buf2[2][3000]; | |
2750 UINT8 bit_buf_tex[2][3000]; | |
2751 PutBitContext pb[2], pb2[2], tex_pb[2]; | |
2752 | |
2753 for(i=0; i<2; i++){ | |
2754 init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL); | |
2755 init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL); | |
2756 init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL); | |
2757 } | |
0 | 2758 |
2759 s->picture_number = picture_number; | |
268 | 2760 |
294 | 2761 s->block_wrap[0]= |
2762 s->block_wrap[1]= | |
2763 s->block_wrap[2]= | |
2764 s->block_wrap[3]= s->mb_width*2 + 2; | |
2765 s->block_wrap[4]= | |
2766 s->block_wrap[5]= s->mb_width + 2; | |
2767 | |
268 | 2768 /* Reset the average MB variance */ |
903 | 2769 s->current_picture.mb_var_sum = 0; |
2770 s->current_picture.mc_mb_var_sum = 0; | |
327 | 2771 |
2772 /* we need to initialize some time vars before we can encode b-frames */ | |
2773 if (s->h263_pred && !s->h263_msmpeg4) | |
2774 ff_set_mpeg4_time(s, s->picture_number); | |
2775 | |
608 | 2776 s->scene_change_score=0; |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2777 |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2778 s->qscale= (int)(s->frame_qscale + 0.5); //FIXME qscale / ... stuff for ME ratedistoration |
936 | 2779 |
2780 if(s->msmpeg4_version){ | |
2781 if(s->pict_type==I_TYPE) | |
2782 s->no_rounding=1; | |
2783 else if(s->flipflop_rounding) | |
2784 s->no_rounding ^= 1; | |
947 | 2785 }else if(s->out_format == FMT_H263){ |
936 | 2786 if(s->pict_type==I_TYPE) |
2787 s->no_rounding=0; | |
2788 else if(s->pict_type!=B_TYPE) | |
2789 s->no_rounding ^= 1; | |
2790 } | |
268 | 2791 /* Estimate motion for every MB */ |
324 | 2792 if(s->pict_type != I_TYPE){ |
951 | 2793 if(s->pict_type != B_TYPE){ |
2794 if((s->avctx->pre_me && s->last_non_b_pict_type==I_TYPE) || s->avctx->pre_me==2){ | |
952 | 2795 s->me.pre_pass=1; |
954 | 2796 s->me.dia_size= s->avctx->pre_dia_size; |
952 | 2797 |
951 | 2798 for(mb_y=s->mb_height-1; mb_y >=0 ; mb_y--) { |
2799 for(mb_x=s->mb_width-1; mb_x >=0 ; mb_x--) { | |
2800 s->mb_x = mb_x; | |
2801 s->mb_y = mb_y; | |
2802 ff_pre_estimate_p_frame_motion(s, mb_x, mb_y); | |
2803 } | |
2804 } | |
952 | 2805 s->me.pre_pass=0; |
951 | 2806 } |
2807 } | |
2808 | |
954 | 2809 s->me.dia_size= s->avctx->dia_size; |
294 | 2810 for(mb_y=0; mb_y < s->mb_height; mb_y++) { |
2811 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; | |
2812 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
2813 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
2814 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
2815 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
2816 s->mb_x = mb_x; | |
2817 s->mb_y = mb_y; | |
2818 s->block_index[0]+=2; | |
2819 s->block_index[1]+=2; | |
2820 s->block_index[2]+=2; | |
2821 s->block_index[3]+=2; | |
954 | 2822 |
294 | 2823 /* compute motion vector & mb_type and store in context */ |
324 | 2824 if(s->pict_type==B_TYPE) |
2825 ff_estimate_b_frame_motion(s, mb_x, mb_y); | |
2826 else | |
2827 ff_estimate_p_frame_motion(s, mb_x, mb_y); | |
268 | 2828 } |
2829 } | |
456 | 2830 }else /* if(s->pict_type == I_TYPE) */{ |
294 | 2831 /* I-Frame */ |
2832 //FIXME do we need to zero them? | |
2833 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); | |
324 | 2834 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2); |
294 | 2835 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); |
612 | 2836 |
2837 if(!s->fixed_qscale){ | |
2838 /* finding spatial complexity for I-frame rate control */ | |
2839 for(mb_y=0; mb_y < s->mb_height; mb_y++) { | |
2840 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
2841 int xx = mb_x * 16; | |
2842 int yy = mb_y * 16; | |
903 | 2843 uint8_t *pix = s->new_picture.data[0] + (yy * s->linesize) + xx; |
612 | 2844 int varc; |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2845 int sum = s->dsp.pix_sum(pix, s->linesize); |
612 | 2846 |
853
eacc2dd8fd9d
* using DSPContext - so each codec could use its local (sub)set of CPU extension
kabi
parents:
847
diff
changeset
|
2847 varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8; |
612 | 2848 |
903 | 2849 s->current_picture.mb_var [s->mb_width * mb_y + mb_x] = varc; |
2850 s->current_picture.mb_mean[s->mb_width * mb_y + mb_x] = (sum+128)>>8; | |
2851 s->current_picture.mb_var_sum += varc; | |
612 | 2852 } |
2853 } | |
2854 } | |
268 | 2855 } |
814 | 2856 emms_c(); |
2857 | |
608 | 2858 if(s->scene_change_score > 0 && s->pict_type == P_TYPE){ |
271 | 2859 s->pict_type= I_TYPE; |
294 | 2860 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); |
903 | 2861 //printf("Scene change detected, encoding as I Frame %d %d\n", s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum); |
271 | 2862 } |
903 | 2863 |
324 | 2864 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) |
2865 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER); | |
2866 ff_fix_long_p_mvs(s); | |
2867 if(s->pict_type==B_TYPE){ | |
2868 s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD); | |
2869 s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD); | |
327 | 2870 |
2871 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD); | |
2872 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD); | |
2873 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR); | |
2874 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR); | |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
2875 } |
324 | 2876 |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2877 if (s->fixed_qscale) |
903 | 2878 s->frame_qscale = s->current_picture.quality; |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2879 else |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2880 s->frame_qscale = ff_rate_estimate_qscale(s); |
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
2881 |
695 | 2882 if(s->adaptive_quant){ |
2883 switch(s->codec_id){ | |
2884 case CODEC_ID_MPEG4: | |
2885 ff_clean_mpeg4_qscales(s); | |
2886 break; | |
2887 case CODEC_ID_H263: | |
2888 case CODEC_ID_H263P: | |
2889 ff_clean_h263_qscales(s); | |
2890 break; | |
2891 } | |
2892 | |
903 | 2893 s->qscale= s->current_picture.qscale_table[0]; |
695 | 2894 }else |
690
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2895 s->qscale= (int)(s->frame_qscale + 0.5); |
a1c69cb685b3
adaptive quantization (lumi/temporal & spatial complexity masking)
michaelni
parents:
673
diff
changeset
|
2896 |
0 | 2897 if (s->out_format == FMT_MJPEG) { |
2898 /* for mjpeg, we do include qscale in the matrix */ | |
533
3c07cf9595de
adding ff prefix to avoid global name conficts with xvid (patch by Marko Kreen <marko at l-t.ee>)
michaelni
parents:
514
diff
changeset
|
2899 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0]; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2900 for(i=1;i<64;i++){ |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2901 int j= s->idct_permutation[i]; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2902 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2903 s->intra_matrix[j] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3); |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
2904 } |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
2905 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, |
709
afeff6ccb7f5
convert only needed matrixes in convert_matrix() (mjpeg calls it for every frame)
michaelni
parents:
706
diff
changeset
|
2906 s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias, 8, 8); |
0 | 2907 } |
903 | 2908 |
2909 //FIXME var duplication | |
2910 s->current_picture.key_frame= s->pict_type == I_TYPE; | |
2911 s->current_picture.pict_type= s->pict_type; | |
2912 | |
2913 if(s->current_picture.key_frame) | |
2914 s->picture_in_gop_number=0; | |
0 | 2915 |
286 | 2916 s->last_bits= get_bit_count(&s->pb); |
0 | 2917 switch(s->out_format) { |
2918 case FMT_MJPEG: | |
2919 mjpeg_picture_header(s); | |
2920 break; | |
2921 case FMT_H263: | |
936 | 2922 if (s->codec_id == CODEC_ID_WMV2) |
2923 ff_wmv2_encode_picture_header(s, picture_number); | |
2924 else if (s->h263_msmpeg4) | |
0 | 2925 msmpeg4_encode_picture_header(s, picture_number); |
2926 else if (s->h263_pred) | |
2927 mpeg4_encode_picture_header(s, picture_number); | |
2928 else if (s->h263_rv10) | |
2929 rv10_encode_picture_header(s, picture_number); | |
2930 else | |
2931 h263_encode_picture_header(s, picture_number); | |
2932 break; | |
2933 case FMT_MPEG1: | |
2934 mpeg1_encode_picture_header(s, picture_number); | |
2935 break; | |
2936 } | |
286 | 2937 bits= get_bit_count(&s->pb); |
2938 s->header_bits= bits - s->last_bits; | |
2939 s->last_bits= bits; | |
2940 s->mv_bits=0; | |
2941 s->misc_bits=0; | |
2942 s->i_tex_bits=0; | |
2943 s->p_tex_bits=0; | |
2944 s->i_count=0; | |
656
e47fa3e3f2d5
statistics for forw & back p-MBs instead of just one counter for both
michaelni
parents:
652
diff
changeset
|
2945 s->f_count=0; |
e47fa3e3f2d5
statistics for forw & back p-MBs instead of just one counter for both
michaelni
parents:
652
diff
changeset
|
2946 s->b_count=0; |
286 | 2947 s->skip_count=0; |
2948 | |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2949 for(i=0; i<3; i++){ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2950 /* init last dc values */ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2951 /* note: quant matrix value (8) is implied here */ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2952 s->last_dc[i] = 128; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2953 |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2954 s->current_picture.error[i] = 0; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
2955 } |
0 | 2956 s->mb_incr = 1; |
2957 s->last_mv[0][0][0] = 0; | |
2958 s->last_mv[0][0][1] = 0; | |
2959 | |
766 | 2960 if (s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P) |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2961 s->gob_index = ff_h263_get_gob_height(s); |
456 | 2962 |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2963 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame) |
456 | 2964 ff_mpeg4_init_partitions(s); |
2965 | |
2966 s->resync_mb_x=0; | |
2967 s->resync_mb_y=0; | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
2968 s->first_slice_line = 1; |
766 | 2969 s->ptr_lastgob = s->pb.buf; |
2970 s->ptr_last_mb_line = s->pb.buf; | |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2971 for(mb_y=0; mb_y < s->mb_height; mb_y++) { |
499 | 2972 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ]; |
2973 s->c_dc_scale= s->c_dc_scale_table[ s->qscale ]; | |
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2974 |
266 | 2975 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; |
2976 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
2977 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
2978 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
2979 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
2980 s->block_index[5]= s->block_wrap[4]*(mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
2981 for(mb_x=0; mb_x < s->mb_width; mb_x++) { |
327 | 2982 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x]; |
2983 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; | |
456 | 2984 // int d; |
294 | 2985 int dmin=10000000; |
0 | 2986 |
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
2987 s->mb_x = mb_x; |
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
2988 s->mb_y = mb_y; |
266 | 2989 s->block_index[0]+=2; |
2990 s->block_index[1]+=2; | |
2991 s->block_index[2]+=2; | |
2992 s->block_index[3]+=2; | |
2993 s->block_index[4]++; | |
2994 s->block_index[5]++; | |
456 | 2995 |
766 | 2996 /* write gob / video packet header */ |
2997 if(s->rtp_mode){ | |
2998 int current_packet_size, is_gob_start; | |
2999 | |
3000 current_packet_size= pbBufPtr(&s->pb) - s->ptr_lastgob; | |
3001 is_gob_start=0; | |
3002 | |
3003 if(s->codec_id==CODEC_ID_MPEG4){ | |
3004 if(current_packet_size + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size | |
3005 && s->mb_y + s->mb_x>0){ | |
3006 | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
3007 if(s->partitioned_frame){ |
456 | 3008 ff_mpeg4_merge_partitions(s); |
3009 ff_mpeg4_init_partitions(s); | |
3010 } | |
3011 ff_mpeg4_encode_video_packet_header(s); | |
3012 | |
3013 if(s->flags&CODEC_FLAG_PASS1){ | |
3014 int bits= get_bit_count(&s->pb); | |
3015 s->misc_bits+= bits - s->last_bits; | |
3016 s->last_bits= bits; | |
3017 } | |
3018 ff_mpeg4_clean_buffers(s); | |
766 | 3019 is_gob_start=1; |
456 | 3020 } |
766 | 3021 }else{ |
3022 if(current_packet_size + s->mb_line_avgsize*s->gob_index >= s->rtp_payload_size | |
3023 && s->mb_x==0 && s->mb_y>0 && s->mb_y%s->gob_index==0){ | |
3024 | |
3025 h263_encode_gob_header(s, mb_y); | |
3026 is_gob_start=1; | |
3027 } | |
3028 } | |
3029 | |
3030 if(is_gob_start){ | |
456 | 3031 s->ptr_lastgob = pbBufPtr(&s->pb); |
3032 s->first_slice_line=1; | |
3033 s->resync_mb_x=mb_x; | |
3034 s->resync_mb_y=mb_y; | |
3035 } | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
3036 } |
456 | 3037 |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
3038 if( (s->resync_mb_x == s->mb_x) |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
3039 && s->resync_mb_y+1 == s->mb_y){ |
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
3040 s->first_slice_line=0; |
456 | 3041 } |
3042 | |
294 | 3043 if(mb_type & (mb_type-1)){ // more than 1 MB type possible |
327 | 3044 int next_block=0; |
456 | 3045 int pb_bits_count, pb2_bits_count, tex_pb_bits_count; |
326 | 3046 |
3047 copy_context_before_encode(&backup_s, s, -1); | |
456 | 3048 backup_s.pb= s->pb; |
3049 best_s.data_partitioning= s->data_partitioning; | |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
3050 best_s.partitioned_frame= s->partitioned_frame; |
456 | 3051 if(s->data_partitioning){ |
3052 backup_s.pb2= s->pb2; | |
3053 backup_s.tex_pb= s->tex_pb; | |
3054 } | |
326 | 3055 |
294 | 3056 if(mb_type&MB_TYPE_INTER){ |
327 | 3057 s->mv_dir = MV_DIR_FORWARD; |
295 | 3058 s->mv_type = MV_TYPE_16X16; |
294 | 3059 s->mb_intra= 0; |
324 | 3060 s->mv[0][0][0] = s->p_mv_table[xy][0]; |
3061 s->mv[0][0][1] = s->p_mv_table[xy][1]; | |
456 | 3062 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, |
3063 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); | |
0 | 3064 } |
326 | 3065 if(mb_type&MB_TYPE_INTER4V){ |
327 | 3066 s->mv_dir = MV_DIR_FORWARD; |
295 | 3067 s->mv_type = MV_TYPE_8X8; |
3068 s->mb_intra= 0; | |
3069 for(i=0; i<4; i++){ | |
3070 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; | |
3071 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; | |
3072 } | |
456 | 3073 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, |
3074 &dmin, &next_block, 0, 0); | |
327 | 3075 } |
3076 if(mb_type&MB_TYPE_FORWARD){ | |
3077 s->mv_dir = MV_DIR_FORWARD; | |
3078 s->mv_type = MV_TYPE_16X16; | |
3079 s->mb_intra= 0; | |
3080 s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; | |
3081 s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; | |
456 | 3082 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, |
3083 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); | |
327 | 3084 } |
3085 if(mb_type&MB_TYPE_BACKWARD){ | |
3086 s->mv_dir = MV_DIR_BACKWARD; | |
3087 s->mv_type = MV_TYPE_16X16; | |
3088 s->mb_intra= 0; | |
3089 s->mv[1][0][0] = s->b_back_mv_table[xy][0]; | |
3090 s->mv[1][0][1] = s->b_back_mv_table[xy][1]; | |
456 | 3091 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, |
3092 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]); | |
327 | 3093 } |
3094 if(mb_type&MB_TYPE_BIDIR){ | |
3095 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | |
3096 s->mv_type = MV_TYPE_16X16; | |
3097 s->mb_intra= 0; | |
3098 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; | |
3099 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; | |
3100 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; | |
3101 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; | |
456 | 3102 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, |
3103 &dmin, &next_block, 0, 0); | |
327 | 3104 } |
3105 if(mb_type&MB_TYPE_DIRECT){ | |
936 | 3106 int mx= s->b_direct_mv_table[xy][0]; |
3107 int my= s->b_direct_mv_table[xy][1]; | |
3108 | |
327 | 3109 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; |
3110 s->mb_intra= 0; | |
936 | 3111 ff_mpeg4_set_direct_mv(s, mx, my); |
456 | 3112 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, |
936 | 3113 &dmin, &next_block, mx, my); |
295 | 3114 } |
294 | 3115 if(mb_type&MB_TYPE_INTRA){ |
327 | 3116 s->mv_dir = MV_DIR_FORWARD; |
295 | 3117 s->mv_type = MV_TYPE_16X16; |
294 | 3118 s->mb_intra= 1; |
3119 s->mv[0][0][0] = 0; | |
3120 s->mv[0][0][1] = 0; | |
456 | 3121 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, |
3122 &dmin, &next_block, 0, 0); | |
326 | 3123 /* force cleaning of ac/dc pred stuff if needed ... */ |
3124 if(s->h263_pred || s->h263_aic) | |
3125 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; | |
295 | 3126 } |
326 | 3127 copy_context_after_encode(s, &best_s, -1); |
456 | 3128 |
3129 pb_bits_count= get_bit_count(&s->pb); | |
3130 flush_put_bits(&s->pb); | |
3131 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count); | |
3132 s->pb= backup_s.pb; | |
3133 | |
3134 if(s->data_partitioning){ | |
3135 pb2_bits_count= get_bit_count(&s->pb2); | |
3136 flush_put_bits(&s->pb2); | |
3137 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count); | |
3138 s->pb2= backup_s.pb2; | |
3139 | |
3140 tex_pb_bits_count= get_bit_count(&s->tex_pb); | |
3141 flush_put_bits(&s->tex_pb); | |
3142 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count); | |
3143 s->tex_pb= backup_s.tex_pb; | |
3144 } | |
329 | 3145 s->last_bits= get_bit_count(&s->pb); |
294 | 3146 } else { |
324 | 3147 int motion_x, motion_y; |
3148 s->mv_type=MV_TYPE_16X16; | |
294 | 3149 // only one MB-Type possible |
327 | 3150 switch(mb_type){ |
3151 case MB_TYPE_INTRA: | |
324 | 3152 s->mv_dir = MV_DIR_FORWARD; |
294 | 3153 s->mb_intra= 1; |
324 | 3154 motion_x= s->mv[0][0][0] = 0; |
3155 motion_y= s->mv[0][0][1] = 0; | |
327 | 3156 break; |
3157 case MB_TYPE_INTER: | |
324 | 3158 s->mv_dir = MV_DIR_FORWARD; |
3159 s->mb_intra= 0; | |
3160 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0]; | |
3161 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1]; | |
327 | 3162 break; |
456 | 3163 case MB_TYPE_INTER4V: |
3164 s->mv_dir = MV_DIR_FORWARD; | |
3165 s->mv_type = MV_TYPE_8X8; | |
3166 s->mb_intra= 0; | |
3167 for(i=0; i<4; i++){ | |
3168 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; | |
3169 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; | |
3170 } | |
3171 motion_x= motion_y= 0; | |
3172 break; | |
327 | 3173 case MB_TYPE_DIRECT: |
324 | 3174 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; |
3175 s->mb_intra= 0; | |
327 | 3176 motion_x=s->b_direct_mv_table[xy][0]; |
3177 motion_y=s->b_direct_mv_table[xy][1]; | |
936 | 3178 ff_mpeg4_set_direct_mv(s, motion_x, motion_y); |
327 | 3179 break; |
3180 case MB_TYPE_BIDIR: | |
324 | 3181 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; |
294 | 3182 s->mb_intra= 0; |
324 | 3183 motion_x=0; |
3184 motion_y=0; | |
3185 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; | |
3186 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; | |
3187 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; | |
3188 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; | |
327 | 3189 break; |
3190 case MB_TYPE_BACKWARD: | |
324 | 3191 s->mv_dir = MV_DIR_BACKWARD; |
3192 s->mb_intra= 0; | |
3193 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0]; | |
3194 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1]; | |
327 | 3195 break; |
3196 case MB_TYPE_FORWARD: | |
324 | 3197 s->mv_dir = MV_DIR_FORWARD; |
3198 s->mb_intra= 0; | |
3199 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; | |
3200 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; | |
3201 // printf(" %d %d ", motion_x, motion_y); | |
327 | 3202 break; |
3203 default: | |
324 | 3204 motion_x=motion_y=0; //gcc warning fix |
3205 printf("illegal MB type\n"); | |
294 | 3206 } |
324 | 3207 encode_mb(s, motion_x, motion_y); |
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
3208 } |
327 | 3209 /* clean the MV table in IPS frames for direct mode in B frames */ |
3210 if(s->mb_intra /* && I,P,S_TYPE */){ | |
3211 s->p_mv_table[xy][0]=0; | |
3212 s->p_mv_table[xy][1]=0; | |
3213 } | |
0 | 3214 |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3215 MPV_decode_mb(s, s->block); |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3216 |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3217 if(s->flags&CODEC_FLAG_PSNR){ |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3218 int w= 16; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3219 int h= 16; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3220 |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3221 if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16; |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3222 if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16; |
936 | 3223 |
909
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3224 s->current_picture.error[0] += sse( |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3225 s, |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3226 s->new_picture .data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3227 s->current_picture.data[0] + s->mb_x*16 + s->mb_y*s->linesize*16, |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3228 w, h, s->linesize); |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3229 s->current_picture.error[1] += sse( |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3230 s, |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3231 s->new_picture .data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3232 s->current_picture.data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3233 w>>1, h>>1, s->uvlinesize); |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3234 s->current_picture.error[2] += sse( |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3235 s, |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3236 s->new_picture .data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3237 s->current_picture.data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*8, |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3238 w>>1, h>>1, s->uvlinesize); |
8ae1e4c24e91
new PSNR code (now works with chroma, b frames, ...)
michaelni
parents:
903
diff
changeset
|
3239 } |
456 | 3240 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb)); |
0 | 3241 } |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
233
diff
changeset
|
3242 |
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
233
diff
changeset
|
3243 |
766 | 3244 /* Obtain average mb_row size for RTP */ |
231 | 3245 if (s->rtp_mode) { |
766 | 3246 if (mb_y==0) |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
233
diff
changeset
|
3247 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line; |
766 | 3248 else { |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
233
diff
changeset
|
3249 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1; |
231 | 3250 } |
766 | 3251 s->ptr_last_mb_line = pbBufPtr(&s->pb); |
231 | 3252 } |
0 | 3253 } |
294 | 3254 emms_c(); |
286 | 3255 |
745
25d7fb7c89be
better/cleaner error resilience (done in a 2nd pass after decoding)
michaelni
parents:
731
diff
changeset
|
3256 if(s->codec_id==CODEC_ID_MPEG4 && s->partitioned_frame) |
456 | 3257 ff_mpeg4_merge_partitions(s); |
3258 | |
3259 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE) | |
208 | 3260 msmpeg4_encode_ext_header(s); |
3261 | |
456 | 3262 if(s->codec_id==CODEC_ID_MPEG4) |
3263 ff_mpeg4_stuffing(&s->pb); | |
3264 | |
162 | 3265 //if (s->gob_number) |
3266 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number); | |
231 | 3267 |
3268 /* Send the last GOB if RTP */ | |
3269 if (s->rtp_mode) { | |
3270 flush_put_bits(&s->pb); | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
233
diff
changeset
|
3271 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; |
231 | 3272 /* Call the RTP callback to send the last GOB */ |
3273 if (s->rtp_callback) | |
3274 s->rtp_callback(s->ptr_lastgob, pdif, s->gob_number); | |
234
5fc0c3af3fe4
alternative bitstream writer (disabled by default, uncomment #define ALT_BISTREAM_WRITER in common.h if u want to try it)
michaelni
parents:
233
diff
changeset
|
3275 s->ptr_lastgob = pbBufPtr(&s->pb); |
231 | 3276 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif); |
3277 } | |
0 | 3278 } |
3279 | |
945 | 3280 static int dct_quantize_trellis_c(MpegEncContext *s, |
3281 DCTELEM *block, int n, | |
3282 int qscale, int *overflow){ | |
3283 const int *qmat; | |
3284 const UINT8 *scantable= s->intra_scantable.scantable; | |
3285 int max=0; | |
3286 unsigned int threshold1, threshold2; | |
3287 int bias=0; | |
3288 int run_tab[65]; | |
3289 int level_tab[65]; | |
3290 int score_tab[65]; | |
946 | 3291 int last_run=0; |
3292 int last_level=0; | |
3293 int last_score= 0; | |
3294 int last_i= 0; | |
3295 int coeff[3][64]; | |
945 | 3296 int coeff_count[64]; |
946 | 3297 int lambda, qmul, qadd, start_i, last_non_zero, i; |
945 | 3298 const int esc_length= s->ac_esc_length; |
3299 uint8_t * length; | |
3300 uint8_t * last_length; | |
946 | 3301 int score_limit=0; |
3302 int left_limit= 0; | |
945 | 3303 |
3304 s->fdct (block); | |
3305 | |
3306 qmul= qscale*16; | |
3307 qadd= ((qscale-1)|1)*8; | |
946 | 3308 |
945 | 3309 if (s->mb_intra) { |
3310 int q; | |
3311 if (!s->h263_aic) { | |
3312 if (n < 4) | |
3313 q = s->y_dc_scale; | |
3314 else | |
3315 q = s->c_dc_scale; | |
3316 q = q << 3; | |
3317 } else{ | |
3318 /* For AIC we skip quant/dequant of INTRADC */ | |
3319 q = 1 << 3; | |
3320 qadd=0; | |
3321 } | |
3322 | |
3323 /* note: block[0] is assumed to be positive */ | |
3324 block[0] = (block[0] + (q >> 1)) / q; | |
3325 start_i = 1; | |
3326 last_non_zero = 0; | |
3327 qmat = s->q_intra_matrix[qscale]; | |
947 | 3328 if(s->mpeg_quant || s->codec_id== CODEC_ID_MPEG1VIDEO) |
945 | 3329 bias= 1<<(QMAT_SHIFT-1); |
3330 length = s->intra_ac_vlc_length; | |
3331 last_length= s->intra_ac_vlc_last_length; | |
3332 } else { | |
3333 start_i = 0; | |
3334 last_non_zero = -1; | |
3335 qmat = s->q_inter_matrix[qscale]; | |
3336 length = s->inter_ac_vlc_length; | |
3337 last_length= s->inter_ac_vlc_last_length; | |
3338 } | |
3339 | |
3340 threshold1= (1<<QMAT_SHIFT) - bias - 1; | |
3341 threshold2= (threshold1<<1); | |
946 | 3342 |
945 | 3343 for(i=start_i; i<64; i++) { |
3344 const int j = scantable[i]; | |
3345 const int k= i-start_i; | |
3346 int level = block[j]; | |
3347 level = level * qmat[j]; | |
3348 | |
3349 // if( bias+level >= (1<<(QMAT_SHIFT - 3)) | |
3350 // || bias-level >= (1<<(QMAT_SHIFT - 3))){ | |
3351 if(((unsigned)(level+threshold1))>threshold2){ | |
3352 if(level>0){ | |
3353 level= (bias + level)>>QMAT_SHIFT; | |
3354 coeff[0][k]= level; | |
3355 coeff[1][k]= level-1; | |
946 | 3356 // coeff[2][k]= level-2; |
945 | 3357 }else{ |
3358 level= (bias - level)>>QMAT_SHIFT; | |
3359 coeff[0][k]= -level; | |
3360 coeff[1][k]= -level+1; | |
946 | 3361 // coeff[2][k]= -level+2; |
945 | 3362 } |
946 | 3363 coeff_count[k]= FFMIN(level, 2); |
945 | 3364 max |=level; |
3365 last_non_zero = i; | |
3366 }else{ | |
946 | 3367 coeff[0][k]= (level>>31)|1; |
945 | 3368 coeff_count[k]= 1; |
3369 } | |
3370 } | |
3371 | |
3372 *overflow= s->max_qcoeff < max; //overflow might have happend | |
3373 | |
3374 if(last_non_zero < start_i){ | |
3375 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM)); | |
3376 return last_non_zero; | |
3377 } | |
3378 | |
3379 lambda= (qscale*qscale*64*82 + 50)/100; //FIXME finetune | |
3380 | |
946 | 3381 score_tab[0]= 0; |
945 | 3382 for(i=0; i<=last_non_zero - start_i; i++){ |
3383 int level_index, run, j; | |
3384 const int dct_coeff= block[ scantable[i + start_i] ]; | |
3385 const int zero_distoration= dct_coeff*dct_coeff; | |
946 | 3386 int best_score=256*256*256*120; |
3387 | |
3388 last_score += zero_distoration; | |
945 | 3389 for(level_index=0; level_index < coeff_count[i]; level_index++){ |
3390 int distoration; | |
3391 int level= coeff[level_index][i]; | |
3392 int unquant_coeff; | |
3393 | |
3394 assert(level); | |
3395 | |
3396 if(s->out_format == FMT_H263){ | |
3397 if(level>0){ | |
3398 unquant_coeff= level*qmul + qadd; | |
3399 }else{ | |
3400 unquant_coeff= level*qmul - qadd; | |
3401 } | |
947 | 3402 }else{ //MPEG1 |
3403 j= s->idct_permutation[ scantable[i + start_i] ]; //FIXME optimize | |
3404 if(s->mb_intra){ | |
3405 if (level < 0) { | |
3406 unquant_coeff = (int)((-level) * qscale * s->intra_matrix[j]) >> 3; | |
3407 unquant_coeff = -((unquant_coeff - 1) | 1); | |
3408 } else { | |
3409 unquant_coeff = (int)( level * qscale * s->intra_matrix[j]) >> 3; | |
3410 unquant_coeff = (unquant_coeff - 1) | 1; | |
3411 } | |
3412 }else{ | |
3413 if (level < 0) { | |
3414 unquant_coeff = ((((-level) << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4; | |
3415 unquant_coeff = -((unquant_coeff - 1) | 1); | |
3416 } else { | |
3417 unquant_coeff = ((( level << 1) + 1) * qscale * ((int) s->inter_matrix[j])) >> 4; | |
3418 unquant_coeff = (unquant_coeff - 1) | 1; | |
3419 } | |
3420 } | |
3421 unquant_coeff<<= 3; | |
3422 } | |
946 | 3423 |
945 | 3424 distoration= (unquant_coeff - dct_coeff) * (unquant_coeff - dct_coeff); |
3425 level+=64; | |
3426 if((level&(~127)) == 0){ | |
946 | 3427 for(run=0; run<=i - left_limit; run++){ |
947 | 3428 int score= distoration + length[UNI_AC_ENC_INDEX(run, level)]*lambda; |
945 | 3429 score += score_tab[i-run]; |
3430 | |
3431 if(score < best_score){ | |
3432 best_score= | |
3433 score_tab[i+1]= score; | |
3434 run_tab[i+1]= run; | |
3435 level_tab[i+1]= level-64; | |
3436 } | |
3437 } | |
3438 | |
3439 if(s->out_format == FMT_H263){ | |
946 | 3440 for(run=0; run<=i - left_limit; run++){ |
947 | 3441 int score= distoration + last_length[UNI_AC_ENC_INDEX(run, level)]*lambda; |
945 | 3442 score += score_tab[i-run]; |
946 | 3443 if(score < last_score){ |
3444 last_score= score; | |
3445 last_run= run; | |
3446 last_level= level-64; | |
3447 last_i= i+1; | |
945 | 3448 } |
3449 } | |
3450 } | |
3451 }else{ | |
3452 distoration += esc_length*lambda; | |
946 | 3453 for(run=0; run<=i - left_limit; run++){ |
945 | 3454 int score= distoration + score_tab[i-run]; |
3455 | |
3456 if(score < best_score){ | |
3457 best_score= | |
3458 score_tab[i+1]= score; | |
3459 run_tab[i+1]= run; | |
3460 level_tab[i+1]= level-64; | |
3461 } | |
3462 } | |
3463 | |
3464 if(s->out_format == FMT_H263){ | |
946 | 3465 for(run=0; run<=i - left_limit; run++){ |
945 | 3466 int score= distoration + score_tab[i-run]; |
946 | 3467 if(score < last_score){ |
3468 last_score= score; | |
3469 last_run= run; | |
3470 last_level= level-64; | |
3471 last_i= i+1; | |
945 | 3472 } |
3473 } | |
3474 } | |
3475 } | |
3476 } | |
3477 | |
946 | 3478 for(j=left_limit; j<=i; j++){ |
945 | 3479 score_tab[j] += zero_distoration; |
3480 } | |
946 | 3481 score_limit+= zero_distoration; |
3482 if(score_tab[i+1] < score_limit) | |
3483 score_limit= score_tab[i+1]; | |
3484 | |
3485 //Note: there is a vlc code in mpeg4 which is 1 bit shorter then another one with a shorter run and the same level | |
3486 while(score_tab[ left_limit ] > score_limit + lambda) left_limit++; | |
945 | 3487 } |
946 | 3488 |
3489 //FIXME add some cbp penalty | |
3490 | |
945 | 3491 if(s->out_format != FMT_H263){ |
946 | 3492 last_score= 256*256*256*120; |
947 | 3493 for(i= left_limit; i<=last_non_zero - start_i + 1; i++){ |
946 | 3494 int score= score_tab[i]; |
947 | 3495 if(i) score += lambda*2; //FIXME exacter? |
3496 | |
946 | 3497 if(score < last_score){ |
3498 last_score= score; | |
3499 last_i= i; | |
3500 last_level= level_tab[i]; | |
3501 last_run= run_tab[i]; | |
3502 } | |
945 | 3503 } |
3504 } | |
3505 | |
946 | 3506 last_non_zero= last_i - 1 + start_i; |
945 | 3507 memset(block + start_i, 0, (64-start_i)*sizeof(DCTELEM)); |
3508 | |
3509 if(last_non_zero < start_i) | |
3510 return last_non_zero; | |
3511 | |
946 | 3512 i= last_i; |
3513 assert(last_level); | |
945 | 3514 //FIXME use permutated scantable |
946 | 3515 block[ s->idct_permutation[ scantable[last_non_zero] ] ]= last_level; |
3516 i -= last_run + 1; | |
945 | 3517 |
3518 for(;i>0 ; i -= run_tab[i] + 1){ | |
3519 const int j= s->idct_permutation[ scantable[i - 1 + start_i] ]; | |
3520 | |
3521 block[j]= level_tab[i]; | |
3522 assert(block[j]); | |
3523 } | |
3524 | |
3525 return last_non_zero; | |
3526 } | |
3527 | |
220 | 3528 static int dct_quantize_c(MpegEncContext *s, |
0 | 3529 DCTELEM *block, int n, |
344 | 3530 int qscale, int *overflow) |
0 | 3531 { |
3532 int i, j, level, last_non_zero, q; | |
3533 const int *qmat; | |
764 | 3534 const UINT8 *scantable= s->intra_scantable.scantable; |
344 | 3535 int bias; |
3536 int max=0; | |
3537 unsigned int threshold1, threshold2; | |
828
ace3ccd18dd2
Altivec Patch (Mark III) by (Dieter Shirley <dieters at schemasoft dot com>)
michaelni
parents:
815
diff
changeset
|
3538 |
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
3539 s->fdct (block); |
0 | 3540 |
3541 if (s->mb_intra) { | |
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3542 if (!s->h263_aic) { |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3543 if (n < 4) |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3544 q = s->y_dc_scale; |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3545 else |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3546 q = s->c_dc_scale; |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3547 q = q << 3; |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3548 } else |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3549 /* For AIC we skip quant/dequant of INTRADC */ |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3550 q = 1 << 3; |
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
3551 |
0 | 3552 /* note: block[0] is assumed to be positive */ |
3553 block[0] = (block[0] + (q >> 1)) / q; | |
3554 i = 1; | |
3555 last_non_zero = 0; | |
344 | 3556 qmat = s->q_intra_matrix[qscale]; |
635 | 3557 bias= s->intra_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT); |
0 | 3558 } else { |
3559 i = 0; | |
3560 last_non_zero = -1; | |
344 | 3561 qmat = s->q_inter_matrix[qscale]; |
635 | 3562 bias= s->inter_quant_bias<<(QMAT_SHIFT - QUANT_BIAS_SHIFT); |
0 | 3563 } |
635 | 3564 threshold1= (1<<QMAT_SHIFT) - bias - 1; |
3565 threshold2= (threshold1<<1); | |
0 | 3566 |
3567 for(;i<64;i++) { | |
764 | 3568 j = scantable[i]; |
0 | 3569 level = block[j]; |
3570 level = level * qmat[j]; | |
344 | 3571 |
3572 // if( bias+level >= (1<<(QMAT_SHIFT - 3)) | |
3573 // || bias-level >= (1<<(QMAT_SHIFT - 3))){ | |
3574 if(((unsigned)(level+threshold1))>threshold2){ | |
3575 if(level>0){ | |
635 | 3576 level= (bias + level)>>QMAT_SHIFT; |
344 | 3577 block[j]= level; |
3578 }else{ | |
635 | 3579 level= (bias - level)>>QMAT_SHIFT; |
344 | 3580 block[j]= -level; |
0 | 3581 } |
344 | 3582 max |=level; |
0 | 3583 last_non_zero = i; |
344 | 3584 }else{ |
3585 block[j]=0; | |
0 | 3586 } |
3587 } | |
344 | 3588 *overflow= s->max_qcoeff < max; //overflow might have happend |
3589 | |
764 | 3590 /* we need this permutation so that we correct the IDCT, we only permute the !=0 elements */ |
882 | 3591 if (s->idct_permutation_type != FF_NO_IDCT_PERM) |
3592 ff_block_permute(block, s->idct_permutation, scantable, last_non_zero); | |
764 | 3593 |
0 | 3594 return last_non_zero; |
3595 } | |
3596 | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3597 static void dct_unquantize_mpeg1_c(MpegEncContext *s, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3598 DCTELEM *block, int n, int qscale) |
0 | 3599 { |
200 | 3600 int i, level, nCoeffs; |
0 | 3601 const UINT16 *quant_matrix; |
3602 | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3603 nCoeffs= s->block_last_index[n]; |
200 | 3604 |
0 | 3605 if (s->mb_intra) { |
3606 if (n < 4) | |
3607 block[0] = block[0] * s->y_dc_scale; | |
3608 else | |
3609 block[0] = block[0] * s->c_dc_scale; | |
3610 /* XXX: only mpeg1 */ | |
3611 quant_matrix = s->intra_matrix; | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3612 for(i=1;i<=nCoeffs;i++) { |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3613 int j= s->intra_scantable.permutated[i]; |
200 | 3614 level = block[j]; |
0 | 3615 if (level) { |
3616 if (level < 0) { | |
3617 level = -level; | |
200 | 3618 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
0 | 3619 level = (level - 1) | 1; |
3620 level = -level; | |
3621 } else { | |
200 | 3622 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
0 | 3623 level = (level - 1) | 1; |
3624 } | |
3625 #ifdef PARANOID | |
3626 if (level < -2048 || level > 2047) | |
3627 fprintf(stderr, "unquant error %d %d\n", i, level); | |
3628 #endif | |
200 | 3629 block[j] = level; |
0 | 3630 } |
3631 } | |
3632 } else { | |
3633 i = 0; | |
344 | 3634 quant_matrix = s->inter_matrix; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3635 for(;i<=nCoeffs;i++) { |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3636 int j= s->intra_scantable.permutated[i]; |
200 | 3637 level = block[j]; |
0 | 3638 if (level) { |
3639 if (level < 0) { | |
3640 level = -level; | |
3641 level = (((level << 1) + 1) * qscale * | |
200 | 3642 ((int) (quant_matrix[j]))) >> 4; |
0 | 3643 level = (level - 1) | 1; |
3644 level = -level; | |
3645 } else { | |
3646 level = (((level << 1) + 1) * qscale * | |
200 | 3647 ((int) (quant_matrix[j]))) >> 4; |
0 | 3648 level = (level - 1) | 1; |
3649 } | |
3650 #ifdef PARANOID | |
3651 if (level < -2048 || level > 2047) | |
3652 fprintf(stderr, "unquant error %d %d\n", i, level); | |
3653 #endif | |
200 | 3654 block[j] = level; |
0 | 3655 } |
3656 } | |
3657 } | |
3658 } | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3659 |
325 | 3660 static void dct_unquantize_mpeg2_c(MpegEncContext *s, |
3661 DCTELEM *block, int n, int qscale) | |
3662 { | |
3663 int i, level, nCoeffs; | |
3664 const UINT16 *quant_matrix; | |
3665 | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3666 if(s->alternate_scan) nCoeffs= 63; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3667 else nCoeffs= s->block_last_index[n]; |
325 | 3668 |
3669 if (s->mb_intra) { | |
3670 if (n < 4) | |
3671 block[0] = block[0] * s->y_dc_scale; | |
3672 else | |
3673 block[0] = block[0] * s->c_dc_scale; | |
3674 quant_matrix = s->intra_matrix; | |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3675 for(i=1;i<=nCoeffs;i++) { |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3676 int j= s->intra_scantable.permutated[i]; |
325 | 3677 level = block[j]; |
3678 if (level) { | |
3679 if (level < 0) { | |
3680 level = -level; | |
3681 level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
3682 level = -level; | |
3683 } else { | |
3684 level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
3685 } | |
3686 #ifdef PARANOID | |
3687 if (level < -2048 || level > 2047) | |
3688 fprintf(stderr, "unquant error %d %d\n", i, level); | |
3689 #endif | |
3690 block[j] = level; | |
3691 } | |
3692 } | |
3693 } else { | |
3694 int sum=-1; | |
3695 i = 0; | |
344 | 3696 quant_matrix = s->inter_matrix; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3697 for(;i<=nCoeffs;i++) { |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3698 int j= s->intra_scantable.permutated[i]; |
325 | 3699 level = block[j]; |
3700 if (level) { | |
3701 if (level < 0) { | |
3702 level = -level; | |
3703 level = (((level << 1) + 1) * qscale * | |
3704 ((int) (quant_matrix[j]))) >> 4; | |
3705 level = -level; | |
3706 } else { | |
3707 level = (((level << 1) + 1) * qscale * | |
3708 ((int) (quant_matrix[j]))) >> 4; | |
3709 } | |
3710 #ifdef PARANOID | |
3711 if (level < -2048 || level > 2047) | |
3712 fprintf(stderr, "unquant error %d %d\n", i, level); | |
3713 #endif | |
3714 block[j] = level; | |
3715 sum+=level; | |
3716 } | |
3717 } | |
3718 block[63]^=sum&1; | |
3719 } | |
3720 } | |
3721 | |
3722 | |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3723 static void dct_unquantize_h263_c(MpegEncContext *s, |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3724 DCTELEM *block, int n, int qscale) |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3725 { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3726 int i, level, qmul, qadd; |
200 | 3727 int nCoeffs; |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
3728 |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3729 assert(s->block_last_index[n]>=0); |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3730 |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3731 qadd = (qscale - 1) | 1; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3732 qmul = qscale << 1; |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3733 |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3734 if (s->mb_intra) { |
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
3735 if (!s->h263_aic) { |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
3736 if (n < 4) |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
3737 block[0] = block[0] * s->y_dc_scale; |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
3738 else |
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
3739 block[0] = block[0] * s->c_dc_scale; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3740 }else |
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3741 qadd = 0; |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3742 i = 1; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3743 nCoeffs= 63; //does not allways use zigzag table |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3744 } else { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3745 i = 0; |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3746 nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ]; |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3747 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3748 |
706
e65798d228ea
idct permutation cleanup, idct can be selected per context now
michaelni
parents:
704
diff
changeset
|
3749 for(;i<=nCoeffs;i++) { |
13
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3750 level = block[i]; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3751 if (level) { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3752 if (level < 0) { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3753 level = level * qmul - qadd; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3754 } else { |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3755 level = level * qmul + qadd; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3756 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3757 #ifdef PARANOID |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3758 if (level < -2048 || level > 2047) |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3759 fprintf(stderr, "unquant error %d %d\n", i, level); |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3760 #endif |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3761 block[i] = level; |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3762 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3763 } |
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
3764 } |
0 | 3765 |
930 | 3766 char ff_get_pict_type_char(int pict_type){ |
3767 switch(pict_type){ | |
3768 case I_TYPE: return 'I'; | |
3769 case P_TYPE: return 'P'; | |
3770 case B_TYPE: return 'B'; | |
3771 case S_TYPE: return 'S'; | |
936 | 3772 default: return '?'; |
930 | 3773 } |
3774 } | |
3775 | |
0 | 3776 AVCodec mpeg1video_encoder = { |
3777 "mpeg1video", | |
3778 CODEC_TYPE_VIDEO, | |
3779 CODEC_ID_MPEG1VIDEO, | |
3780 sizeof(MpegEncContext), | |
3781 MPV_encode_init, | |
3782 MPV_encode_picture, | |
3783 MPV_encode_end, | |
3784 }; | |
3785 | |
3786 AVCodec h263_encoder = { | |
3787 "h263", | |
3788 CODEC_TYPE_VIDEO, | |
3789 CODEC_ID_H263, | |
3790 sizeof(MpegEncContext), | |
3791 MPV_encode_init, | |
3792 MPV_encode_picture, | |
3793 MPV_encode_end, | |
3794 }; | |
3795 | |
3796 AVCodec h263p_encoder = { | |
3797 "h263p", | |
3798 CODEC_TYPE_VIDEO, | |
3799 CODEC_ID_H263P, | |
3800 sizeof(MpegEncContext), | |
3801 MPV_encode_init, | |
3802 MPV_encode_picture, | |
3803 MPV_encode_end, | |
3804 }; | |
3805 | |
3806 AVCodec rv10_encoder = { | |
3807 "rv10", | |
3808 CODEC_TYPE_VIDEO, | |
3809 CODEC_ID_RV10, | |
3810 sizeof(MpegEncContext), | |
3811 MPV_encode_init, | |
3812 MPV_encode_picture, | |
3813 MPV_encode_end, | |
3814 }; | |
3815 | |
3816 AVCodec mjpeg_encoder = { | |
3817 "mjpeg", | |
3818 CODEC_TYPE_VIDEO, | |
3819 CODEC_ID_MJPEG, | |
3820 sizeof(MpegEncContext), | |
3821 MPV_encode_init, | |
3822 MPV_encode_picture, | |
3823 MPV_encode_end, | |
3824 }; | |
3825 | |
71 | 3826 AVCodec mpeg4_encoder = { |
3827 "mpeg4", | |
0 | 3828 CODEC_TYPE_VIDEO, |
71 | 3829 CODEC_ID_MPEG4, |
0 | 3830 sizeof(MpegEncContext), |
3831 MPV_encode_init, | |
3832 MPV_encode_picture, | |
3833 MPV_encode_end, | |
3834 }; | |
3835 | |
307 | 3836 AVCodec msmpeg4v1_encoder = { |
3837 "msmpeg4v1", | |
0 | 3838 CODEC_TYPE_VIDEO, |
307 | 3839 CODEC_ID_MSMPEG4V1, |
0 | 3840 sizeof(MpegEncContext), |
3841 MPV_encode_init, | |
3842 MPV_encode_picture, | |
3843 MPV_encode_end, | |
3844 }; | |
307 | 3845 |
3846 AVCodec msmpeg4v2_encoder = { | |
3847 "msmpeg4v2", | |
3848 CODEC_TYPE_VIDEO, | |
3849 CODEC_ID_MSMPEG4V2, | |
3850 sizeof(MpegEncContext), | |
3851 MPV_encode_init, | |
3852 MPV_encode_picture, | |
3853 MPV_encode_end, | |
3854 }; | |
3855 | |
3856 AVCodec msmpeg4v3_encoder = { | |
3857 "msmpeg4", | |
3858 CODEC_TYPE_VIDEO, | |
3859 CODEC_ID_MSMPEG4V3, | |
3860 sizeof(MpegEncContext), | |
3861 MPV_encode_init, | |
3862 MPV_encode_picture, | |
3863 MPV_encode_end, | |
3864 }; | |
499 | 3865 |
3866 AVCodec wmv1_encoder = { | |
3867 "wmv1", | |
3868 CODEC_TYPE_VIDEO, | |
3869 CODEC_ID_WMV1, | |
3870 sizeof(MpegEncContext), | |
3871 MPV_encode_init, | |
3872 MPV_encode_picture, | |
3873 MPV_encode_end, | |
3874 }; | |
3875 |