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