Mercurial > libavcodec.hg
annotate mpegvideo.c @ 625:bb6a69f9d409 libavcodec
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
per context DCT selection
| author | michaelni |
|---|---|
| date | Thu, 29 Aug 2002 23:55:32 +0000 |
| parents | 2be2cc8fd0a1 |
| children | f596db4aa871 |
| 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 */ |
| 21 #include "avcodec.h" | |
| 22 #include "dsputil.h" | |
| 23 #include "mpegvideo.h" | |
| 24 | |
|
17
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
25 #ifdef USE_FASTMEMCPY |
|
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
26 #include "fastmemcpy.h" |
|
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
27 #endif |
|
b69fe46fd708
Adding fastmemcpy stuff to speedup mplayer project
nickols_k
parents:
13
diff
changeset
|
28 |
|
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
|
29 static void encode_picture(MpegEncContext *s, int picture_number); |
|
174ef88f619a
use block[] in structure to have it aligned on 8 bytes for mmx optimizations - dct_unquantize is always a function pointer - added specialized dct_unquantize_h263
glantau
parents:
8
diff
changeset
|
30 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
|
31 DCTELEM *block, int n, int qscale); |
| 325 | 32 static void dct_unquantize_mpeg2_c(MpegEncContext *s, |
| 33 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
|
34 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
|
35 DCTELEM *block, int n, int qscale); |
| 206 | 36 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w); |
| 344 | 37 static int dct_quantize_c(MpegEncContext *s, DCTELEM *block, int n, int qscale, int *overflow); |
| 206 | 38 |
| 39 void (*draw_edges)(UINT8 *buf, int wrap, int width, int height, int w)= draw_edges_c; | |
| 566 | 40 static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, |
| 41 int src_x, int src_y, int w, int h); | |
| 206 | 42 |
| 0 | 43 #define EDGE_WIDTH 16 |
| 44 | |
| 45 /* enable all paranoid tests for rounding, overflows, etc... */ | |
| 46 //#define PARANOID | |
| 47 | |
| 48 //#define DEBUG | |
| 49 | |
| 321 | 50 |
| 0 | 51 /* for jpeg fast DCT */ |
| 52 #define CONST_BITS 14 | |
| 53 | |
| 54 static const unsigned short aanscales[64] = { | |
| 55 /* precomputed values scaled up by 14 bits */ | |
| 56 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
| 57 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, | |
| 58 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, | |
| 59 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, | |
| 60 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
| 61 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, | |
| 62 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, | |
| 63 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 | |
| 64 }; | |
| 65 | |
| 66 static UINT8 h263_chroma_roundtab[16] = { | |
| 67 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, | |
| 68 }; | |
| 69 | |
|
292
73a9ce3d9715
fcode_tables where too small, found by Klaas-Pieter Vlieg <vlieg@eurescom.de>
michaelni
parents:
288
diff
changeset
|
70 static UINT16 default_mv_penalty[MAX_FCODE+1][MAX_MV*2+1]; |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
71 static UINT8 default_fcode_tab[MAX_MV*2+1]; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
72 |
| 321 | 73 extern UINT8 zigzag_end[64]; |
| 74 | |
| 0 | 75 /* default motion estimation */ |
| 321 | 76 int motion_estimation_method = ME_EPZS; |
| 200 | 77 |
|
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
|
78 static void convert_matrix(MpegEncContext *s, int (*qmat)[64], uint16_t (*qmat16)[64], uint16_t (*qmat16_bias)[64], |
| 344 | 79 const UINT16 *quant_matrix, int bias) |
| 0 | 80 { |
| 344 | 81 int qscale; |
| 0 | 82 |
| 344 | 83 for(qscale=1; qscale<32; qscale++){ |
| 84 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
|
85 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
|
86 for(i=0;i<64;i++) { |
|
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
|
87 const int j= block_permute_op(i); |
|
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
|
88 /* 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
|
89 /* 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
|
90 /* (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
|
91 /* 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
|
92 |
|
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
|
93 qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT-3)) / |
|
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
|
94 (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
|
95 } |
|
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
96 } else if (s->fdct == fdct_ifast) { |
| 344 | 97 for(i=0;i<64;i++) { |
| 98 const int j= block_permute_op(i); | |
| 99 /* 16 <= qscale * quant_matrix[i] <= 7905 */ | |
| 100 /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */ | |
| 101 /* (1<<36)/19952 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= (1<<36)/249205026 */ | |
| 102 /* 3444240 >= (1<<36)/(aanscales[i] * qscale * quant_matrix[i]) >= 275 */ | |
| 103 | |
| 104 qmat[qscale][j] = (int)((UINT64_C(1) << (QMAT_SHIFT + 11)) / | |
| 105 (aanscales[i] * qscale * quant_matrix[j])); | |
| 106 } | |
| 107 } else { | |
| 108 for(i=0;i<64;i++) { | |
| 109 /* We can safely suppose that 16 <= quant_matrix[i] <= 255 | |
| 110 So 16 <= qscale * quant_matrix[i] <= 7905 | |
| 111 so (1<<19) / 16 >= (1<<19) / (qscale * quant_matrix[i]) >= (1<<19) / 7905 | |
| 112 so 32768 >= (1<<19) / (qscale * quant_matrix[i]) >= 67 | |
| 113 */ | |
| 114 qmat [qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]); | |
| 115 qmat16[qscale][i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[block_permute_op(i)]); | |
| 116 | |
| 117 if(qmat16[qscale][i]==0 || qmat16[qscale][i]==128*256) qmat16[qscale][i]=128*256-1; | |
| 118 | |
| 119 qmat16_bias[qscale][i]= ROUNDED_DIV(bias<<(16-QUANT_BIAS_SHIFT), qmat16[qscale][i]); | |
| 120 } | |
| 0 | 121 } |
| 122 } | |
| 123 } | |
| 456 | 124 // move into common.c perhaps |
| 125 #define CHECKED_ALLOCZ(p, size)\ | |
| 126 {\ | |
| 127 p= av_mallocz(size);\ | |
| 128 if(p==NULL){\ | |
| 129 perror("malloc");\ | |
| 130 goto fail;\ | |
| 131 }\ | |
| 132 } | |
| 0 | 133 |
| 134 /* init common structure for both encoder and decoder */ | |
| 135 int MPV_common_init(MpegEncContext *s) | |
| 136 { | |
| 137 int c_size, i; | |
| 138 UINT8 *pict; | |
| 139 | |
| 312 | 140 s->dct_unquantize_h263 = dct_unquantize_h263_c; |
| 325 | 141 s->dct_unquantize_mpeg1 = dct_unquantize_mpeg1_c; |
| 142 s->dct_unquantize_mpeg2 = dct_unquantize_mpeg2_c; | |
|
625
bb6a69f9d409
slow but accurate integer dct from IJG (should be ok with the LGPL as the old DCT is the fast integer DCT from IJG)
michaelni
parents:
619
diff
changeset
|
143 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
|
144 |
|
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
|
145 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
|
146 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
|
147 else |
|
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
|
148 s->fdct = ff_jpeg_fdct_islow; |
|
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
|
149 |
|
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
|
150 #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
|
151 MPV_common_init_mmx(s); |
| 8 | 152 #endif |
|
514
c9f724e3a797
Update and activate dct_unquantize_h263_mvi. Thanks to M?ns Rullg?rd
mellum
parents:
506
diff
changeset
|
153 #ifdef ARCH_ALPHA |
|
c9f724e3a797
Update and activate dct_unquantize_h263_mvi. Thanks to M?ns Rullg?rd
mellum
parents:
506
diff
changeset
|
154 MPV_common_init_axp(s); |
|
c9f724e3a797
Update and activate dct_unquantize_h263_mvi. Thanks to M?ns Rullg?rd
mellum
parents:
506
diff
changeset
|
155 #endif |
| 591 | 156 |
| 0 | 157 s->mb_width = (s->width + 15) / 16; |
| 158 s->mb_height = (s->height + 15) / 16; | |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
159 |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
160 /* set default edge pos, will be overriden in decode_header if needed */ |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
161 s->h_edge_pos= s->mb_width*16; |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
162 s->v_edge_pos= s->mb_height*16; |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
163 |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
164 s->mb_num = s->mb_width * s->mb_height; |
| 553 | 165 if(!(s->flags&CODEC_FLAG_DR1)){ |
| 556 | 166 s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH; |
| 167 s->uvlinesize = s->mb_width * 8 + EDGE_WIDTH; | |
| 0 | 168 |
| 553 | 169 for(i=0;i<3;i++) { |
| 0 | 170 int w, h, shift, pict_start; |
| 171 | |
| 172 w = s->linesize; | |
| 173 h = s->mb_height * 16 + 2 * EDGE_WIDTH; | |
| 174 shift = (i == 0) ? 0 : 1; | |
| 556 | 175 c_size = (s->linesize>>shift) * (h >> shift); |
| 176 pict_start = (s->linesize>>shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift); | |
| 0 | 177 |
| 456 | 178 CHECKED_ALLOCZ(pict, c_size) |
| 0 | 179 s->last_picture_base[i] = pict; |
| 180 s->last_picture[i] = pict + pict_start; | |
| 485 | 181 if(i>0) memset(s->last_picture_base[i], 128, c_size); |
| 0 | 182 |
| 456 | 183 CHECKED_ALLOCZ(pict, c_size) |
| 0 | 184 s->next_picture_base[i] = pict; |
| 185 s->next_picture[i] = pict + pict_start; | |
| 485 | 186 if(i>0) memset(s->next_picture_base[i], 128, c_size); |
| 324 | 187 |
|
365
fdeec2834c79
there are divx5? encoded files without a userdata section but with b-frames :(
michaelni
parents:
350
diff
changeset
|
188 if (s->has_b_frames || s->codec_id==CODEC_ID_MPEG4) { |
|
fdeec2834c79
there are divx5? encoded files without a userdata section but with b-frames :(
michaelni
parents:
350
diff
changeset
|
189 /* Note the MPEG4 stuff is here cuz of buggy encoders which dont set the low_delay flag but |
|
fdeec2834c79
there are divx5? encoded files without a userdata section but with b-frames :(
michaelni
parents:
350
diff
changeset
|
190 do low-delay encoding, so we cant allways distinguish b-frame containing streams from low_delay streams */ |
| 456 | 191 CHECKED_ALLOCZ(pict, c_size) |
| 0 | 192 s->aux_picture_base[i] = pict; |
| 193 s->aux_picture[i] = pict + pict_start; | |
| 485 | 194 if(i>0) memset(s->aux_picture_base[i], 128, c_size); |
| 0 | 195 } |
| 553 | 196 } |
| 556 | 197 s->ip_buffer_count= 2; |
| 0 | 198 } |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
199 |
|
567
5a6eaaa9fba2
larger edge emu buffer (might have been too small)
michaelni
parents:
566
diff
changeset
|
200 CHECKED_ALLOCZ(s->edge_emu_buffer, (s->width+64)*2*17*2); //(width + edge + align)*interlaced*MBsize*tolerance |
| 553 | 201 |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
202 if (s->encoding) { |
| 324 | 203 int j; |
| 204 int mv_table_size= (s->mb_width+2)*(s->mb_height+2); | |
| 239 | 205 |
| 456 | 206 CHECKED_ALLOCZ(s->mb_var , s->mb_num * sizeof(INT16)) |
| 207 CHECKED_ALLOCZ(s->mc_mb_var, s->mb_num * sizeof(INT16)) | |
| 324 | 208 |
| 209 /* Allocate MV tables */ | |
| 456 | 210 CHECKED_ALLOCZ(s->p_mv_table , mv_table_size * 2 * sizeof(INT16)) |
| 211 CHECKED_ALLOCZ(s->b_forw_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
| 212 CHECKED_ALLOCZ(s->b_back_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
| 213 CHECKED_ALLOCZ(s->b_bidir_forw_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
| 214 CHECKED_ALLOCZ(s->b_bidir_back_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
| 215 CHECKED_ALLOCZ(s->b_direct_forw_mv_table, mv_table_size * 2 * sizeof(INT16)) | |
| 216 CHECKED_ALLOCZ(s->b_direct_back_mv_table, mv_table_size * 2 * sizeof(INT16)) | |
| 217 CHECKED_ALLOCZ(s->b_direct_mv_table , mv_table_size * 2 * sizeof(INT16)) | |
| 324 | 218 |
| 456 | 219 CHECKED_ALLOCZ(s->me_scratchpad, s->linesize*16*3*sizeof(uint8_t)) |
| 220 | |
| 221 CHECKED_ALLOCZ(s->me_map , ME_MAP_SIZE*sizeof(uint32_t)) | |
| 222 CHECKED_ALLOCZ(s->me_score_map, ME_MAP_SIZE*sizeof(uint16_t)) | |
| 327 | 223 |
| 324 | 224 if(s->max_b_frames){ |
| 225 for(j=0; j<REORDER_BUFFER_SIZE; j++){ | |
| 226 int i; | |
| 227 for(i=0;i<3;i++) { | |
| 228 int w, h, shift; | |
| 229 | |
| 230 w = s->linesize; | |
| 231 h = s->mb_height * 16; | |
| 232 shift = (i == 0) ? 0 : 1; | |
| 233 c_size = (w >> shift) * (h >> shift); | |
| 234 | |
| 456 | 235 CHECKED_ALLOCZ(pict, c_size); |
| 324 | 236 s->picture_buffer[j][i] = pict; |
| 237 } | |
| 238 } | |
| 239 } | |
| 456 | 240 |
| 241 if(s->codec_id==CODEC_ID_MPEG4){ | |
| 242 CHECKED_ALLOCZ(s->tex_pb_buffer, PB_BUFFER_SIZE); | |
| 243 CHECKED_ALLOCZ( s->pb2_buffer, PB_BUFFER_SIZE); | |
| 244 } | |
| 612 | 245 |
| 246 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
|
247 } |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
248 |
| 288 | 249 if (s->out_format == FMT_H263 || s->encoding) { |
| 0 | 250 int size; |
| 456 | 251 /* Allocate MB type table */ |
| 252 CHECKED_ALLOCZ(s->mb_type , s->mb_num * sizeof(UINT8)) | |
| 253 | |
| 0 | 254 /* MV prediction */ |
| 255 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
| 456 | 256 CHECKED_ALLOCZ(s->motion_val, size * 2 * sizeof(INT16)); |
| 598 | 257 |
| 258 /* 4mv direct mode decoding table */ | |
| 259 CHECKED_ALLOCZ(s->non_b_mv4_table, size * sizeof(UINT8)) | |
| 0 | 260 } |
| 261 | |
|
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
262 if (s->h263_pred || s->h263_plus) { |
| 0 | 263 int y_size, c_size, i, size; |
| 264 | |
| 265 /* dc values */ | |
| 266 | |
| 267 y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); | |
| 268 c_size = (s->mb_width + 2) * (s->mb_height + 2); | |
| 269 size = y_size + 2 * c_size; | |
| 456 | 270 CHECKED_ALLOCZ(s->dc_val[0], size * sizeof(INT16)); |
| 0 | 271 s->dc_val[1] = s->dc_val[0] + y_size; |
| 272 s->dc_val[2] = s->dc_val[1] + c_size; | |
| 273 for(i=0;i<size;i++) | |
| 274 s->dc_val[0][i] = 1024; | |
| 275 | |
| 276 /* ac values */ | |
| 456 | 277 CHECKED_ALLOCZ(s->ac_val[0], size * sizeof(INT16) * 16); |
| 0 | 278 s->ac_val[1] = s->ac_val[0] + y_size; |
| 279 s->ac_val[2] = s->ac_val[1] + c_size; | |
| 280 | |
| 281 /* cbp values */ | |
| 456 | 282 CHECKED_ALLOCZ(s->coded_block, y_size); |
| 333 | 283 |
| 284 /* divx501 bitstream reorder buffer */ | |
| 456 | 285 CHECKED_ALLOCZ(s->bitstream_buffer, BITSTREAM_BUFFER_SIZE); |
| 286 | |
| 287 /* cbp, ac_pred, pred_dir */ | |
| 288 CHECKED_ALLOCZ(s->cbp_table , s->mb_num * sizeof(UINT8)) | |
| 289 CHECKED_ALLOCZ(s->pred_dir_table, s->mb_num * sizeof(UINT8)) | |
|
197
21abf1b20016
different fix, s->mbintra_table used only if h263_pred set. - patch by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
196
diff
changeset
|
290 } |
| 575 | 291 CHECKED_ALLOCZ(s->qscale_table , s->mb_num * sizeof(UINT8)) |
| 292 | |
|
611
3214d3f4519e
error concealment needs the mbintra_table so it should allways be allocated
michaelni
parents:
608
diff
changeset
|
293 /* which mb is a intra block */ |
|
3214d3f4519e
error concealment needs the mbintra_table so it should allways be allocated
michaelni
parents:
608
diff
changeset
|
294 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
|
295 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
|
296 |
| 0 | 297 /* default structure is frame */ |
| 298 s->picture_structure = PICT_FRAME; | |
| 553 | 299 |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
300 /* init macroblock skip table */ |
| 456 | 301 CHECKED_ALLOCZ(s->mbskip_table, s->mb_num); |
| 294 | 302 |
| 327 | 303 s->block= s->blocks[0]; |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
304 |
| 0 | 305 s->context_initialized = 1; |
| 306 return 0; | |
| 307 fail: | |
| 244 | 308 MPV_common_end(s); |
| 309 return -1; | |
| 310 } | |
| 311 | |
| 456 | 312 |
| 313 //extern int sads; | |
| 314 | |
| 244 | 315 /* init common structure for both encoder and decoder */ |
| 316 void MPV_common_end(MpegEncContext *s) | |
| 317 { | |
| 318 int i; | |
| 319 | |
|
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
320 av_freep(&s->mb_type); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
321 av_freep(&s->mb_var); |
| 456 | 322 av_freep(&s->mc_mb_var); |
|
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
323 av_freep(&s->p_mv_table); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
324 av_freep(&s->b_forw_mv_table); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
325 av_freep(&s->b_back_mv_table); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
326 av_freep(&s->b_bidir_forw_mv_table); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
327 av_freep(&s->b_bidir_back_mv_table); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
328 av_freep(&s->b_direct_forw_mv_table); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
329 av_freep(&s->b_direct_back_mv_table); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
330 av_freep(&s->b_direct_mv_table); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
331 av_freep(&s->motion_val); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
332 av_freep(&s->dc_val[0]); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
333 av_freep(&s->ac_val[0]); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
334 av_freep(&s->coded_block); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
335 av_freep(&s->mbintra_table); |
| 456 | 336 av_freep(&s->cbp_table); |
| 337 av_freep(&s->pred_dir_table); | |
| 338 av_freep(&s->qscale_table); | |
|
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
339 av_freep(&s->me_scratchpad); |
| 456 | 340 av_freep(&s->me_map); |
| 341 av_freep(&s->me_score_map); | |
| 342 | |
|
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
343 av_freep(&s->mbskip_table); |
|
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
344 av_freep(&s->bitstream_buffer); |
| 456 | 345 av_freep(&s->tex_pb_buffer); |
| 346 av_freep(&s->pb2_buffer); | |
| 553 | 347 av_freep(&s->edge_emu_buffer); |
| 598 | 348 av_freep(&s->non_b_mv4_table); |
| 612 | 349 av_freep(&s->avctx->stats_out); |
| 350 | |
| 0 | 351 for(i=0;i<3;i++) { |
| 324 | 352 int j; |
| 553 | 353 if(!(s->flags&CODEC_FLAG_DR1)){ |
| 354 av_freep(&s->last_picture_base[i]); | |
| 355 av_freep(&s->next_picture_base[i]); | |
| 356 av_freep(&s->aux_picture_base[i]); | |
| 357 } | |
| 358 s->last_picture_base[i]= | |
| 359 s->next_picture_base[i]= | |
| 360 s->aux_picture_base [i] = NULL; | |
| 361 s->last_picture[i]= | |
| 362 s->next_picture[i]= | |
| 363 s->aux_picture [i] = NULL; | |
| 364 | |
| 324 | 365 for(j=0; j<REORDER_BUFFER_SIZE; j++){ |
|
396
fce0a2520551
removed useless header includes - use av memory functions
glantau
parents:
376
diff
changeset
|
366 av_freep(&s->picture_buffer[j][i]); |
| 324 | 367 } |
| 0 | 368 } |
| 369 s->context_initialized = 0; | |
| 370 } | |
| 371 | |
| 372 /* init video encoder */ | |
| 373 int MPV_encode_init(AVCodecContext *avctx) | |
| 374 { | |
| 375 MpegEncContext *s = avctx->priv_data; | |
| 60 | 376 int i; |
| 0 | 377 |
| 315 | 378 avctx->pix_fmt = PIX_FMT_YUV420P; |
| 379 | |
| 0 | 380 s->bit_rate = avctx->bit_rate; |
| 268 | 381 s->bit_rate_tolerance = avctx->bit_rate_tolerance; |
| 0 | 382 s->frame_rate = avctx->frame_rate; |
| 383 s->width = avctx->width; | |
| 384 s->height = avctx->height; | |
| 456 | 385 if(avctx->gop_size > 600){ |
|
462
3fdd0627f6bd
typo (found by Bohdan Horst <nexus at hoth.amu.edu.pl>)
michaelni
parents:
461
diff
changeset
|
386 fprintf(stderr, "Warning keyframe interval too large! reducing it ...\n"); |
| 456 | 387 avctx->gop_size=600; |
| 388 } | |
| 0 | 389 s->gop_size = avctx->gop_size; |
| 162 | 390 s->rtp_mode = avctx->rtp_mode; |
| 391 s->rtp_payload_size = avctx->rtp_payload_size; | |
| 231 | 392 if (avctx->rtp_callback) |
| 393 s->rtp_callback = avctx->rtp_callback; | |
| 268 | 394 s->qmin= avctx->qmin; |
| 395 s->qmax= avctx->qmax; | |
| 396 s->max_qdiff= avctx->max_qdiff; | |
| 397 s->qcompress= avctx->qcompress; | |
| 398 s->qblur= avctx->qblur; | |
|
194
27d1773552c9
mpeg4 encoder fix by Michael Niedermayer <michaelni@gmx.at>
arpi_esp
parents:
191
diff
changeset
|
399 s->avctx = avctx; |
|
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
400 s->aspect_ratio_info= avctx->aspect_ratio_info; |
| 618 | 401 if (avctx->aspect_ratio_info == FF_ASPECT_EXTENDED) |
| 402 { | |
| 403 s->aspected_width = avctx->aspected_width; | |
| 404 s->aspected_height = avctx->aspected_height; | |
| 405 } | |
| 294 | 406 s->flags= avctx->flags; |
| 324 | 407 s->max_b_frames= avctx->max_b_frames; |
| 329 | 408 s->b_frame_strategy= avctx->b_frame_strategy; |
| 344 | 409 s->codec_id= avctx->codec->id; |
| 456 | 410 s->luma_elim_threshold = avctx->luma_elim_threshold; |
| 411 s->chroma_elim_threshold= avctx->chroma_elim_threshold; | |
| 412 s->strict_std_compliance= avctx->strict_std_compliance; | |
| 413 s->data_partitioning= avctx->flags & CODEC_FLAG_PART; | |
| 599 | 414 s->mpeg_quant= avctx->mpeg_quant; |
| 344 | 415 |
| 0 | 416 if (s->gop_size <= 1) { |
| 417 s->intra_only = 1; | |
| 418 s->gop_size = 12; | |
| 419 } else { | |
| 420 s->intra_only = 0; | |
| 421 } | |
|
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
422 |
|
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
423 /* ME algorithm */ |
| 321 | 424 if (avctx->me_method == 0) |
| 425 /* For compatibility */ | |
| 426 s->me_method = motion_estimation_method; | |
| 427 else | |
| 428 s->me_method = avctx->me_method; | |
| 429 | |
|
320
cda7d0857baf
- ME setting moved to AVCodecContext/MpegEncContext, no longer a global.
pulento
parents:
315
diff
changeset
|
430 /* Fixed QSCALE */ |
| 0 | 431 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
|
432 |
| 0 | 433 switch(avctx->codec->id) { |
| 434 case CODEC_ID_MPEG1VIDEO: | |
| 435 s->out_format = FMT_MPEG1; | |
| 336 | 436 avctx->delay=0; //FIXME not sure, should check the spec |
| 0 | 437 break; |
| 438 case CODEC_ID_MJPEG: | |
| 439 s->out_format = FMT_MJPEG; | |
| 440 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
|
441 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
|
442 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
|
443 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
|
444 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
|
445 s->mjpeg_vsample[2] = 1; |
|
370
0eca28d16cbd
clamp intra matrix to 8bit for mjpeg (workaround for qscale>=25)
al3x
parents:
365
diff
changeset
|
446 s->mjpeg_hsample[0] = 2; |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
447 s->mjpeg_hsample[1] = 1; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
448 s->mjpeg_hsample[2] = 1; |
| 0 | 449 if (mjpeg_init(s) < 0) |
| 450 return -1; | |
| 336 | 451 avctx->delay=0; |
| 0 | 452 break; |
| 453 case CODEC_ID_H263: | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
454 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
|
455 printf("Input picture size isn't suitable for h263 codec! try h263+\n"); |
| 0 | 456 return -1; |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
457 } |
| 0 | 458 s->out_format = FMT_H263; |
| 336 | 459 avctx->delay=0; |
| 0 | 460 break; |
| 461 case CODEC_ID_H263P: | |
| 462 s->out_format = FMT_H263; | |
| 162 | 463 s->rtp_mode = 1; |
| 464 s->rtp_payload_size = 1200; | |
| 0 | 465 s->h263_plus = 1; |
| 78 | 466 s->unrestricted_mv = 1; |
|
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
467 s->h263_aic = 1; |
|
79
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
468 |
|
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
469 /* These are just to be sure */ |
|
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
470 s->umvplus = 0; |
|
82e579c37bc3
Moved some H.263+ variables to MpegEncContext to be thread-safe.
pulento
parents:
78
diff
changeset
|
471 s->umvplus_dec = 0; |
| 336 | 472 avctx->delay=0; |
| 0 | 473 break; |
| 474 case CODEC_ID_RV10: | |
| 475 s->out_format = FMT_H263; | |
| 476 s->h263_rv10 = 1; | |
| 336 | 477 avctx->delay=0; |
| 0 | 478 break; |
| 71 | 479 case CODEC_ID_MPEG4: |
| 0 | 480 s->out_format = FMT_H263; |
| 481 s->h263_pred = 1; | |
| 482 s->unrestricted_mv = 1; | |
| 324 | 483 s->has_b_frames= s->max_b_frames ? 1 : 0; |
| 336 | 484 s->low_delay=0; |
| 485 avctx->delay= s->low_delay ? 0 : (s->max_b_frames + 1); | |
| 0 | 486 break; |
| 307 | 487 case CODEC_ID_MSMPEG4V1: |
| 0 | 488 s->out_format = FMT_H263; |
| 489 s->h263_msmpeg4 = 1; | |
| 490 s->h263_pred = 1; | |
| 491 s->unrestricted_mv = 1; | |
| 307 | 492 s->msmpeg4_version= 1; |
| 336 | 493 avctx->delay=0; |
| 307 | 494 break; |
| 495 case CODEC_ID_MSMPEG4V2: | |
| 496 s->out_format = FMT_H263; | |
| 497 s->h263_msmpeg4 = 1; | |
| 498 s->h263_pred = 1; | |
| 499 s->unrestricted_mv = 1; | |
| 500 s->msmpeg4_version= 2; | |
| 336 | 501 avctx->delay=0; |
| 307 | 502 break; |
| 503 case CODEC_ID_MSMPEG4V3: | |
| 504 s->out_format = FMT_H263; | |
| 505 s->h263_msmpeg4 = 1; | |
| 506 s->h263_pred = 1; | |
| 507 s->unrestricted_mv = 1; | |
| 508 s->msmpeg4_version= 3; | |
| 336 | 509 avctx->delay=0; |
| 0 | 510 break; |
| 499 | 511 case CODEC_ID_WMV1: |
| 512 s->out_format = FMT_H263; | |
| 513 s->h263_msmpeg4 = 1; | |
| 514 s->h263_pred = 1; | |
| 515 s->unrestricted_mv = 1; | |
| 516 s->msmpeg4_version= 4; | |
| 517 avctx->delay=0; | |
| 518 break; | |
| 519 case CODEC_ID_WMV2: | |
| 520 s->out_format = FMT_H263; | |
| 521 s->h263_msmpeg4 = 1; | |
| 522 s->h263_pred = 1; | |
| 523 s->unrestricted_mv = 1; | |
| 524 s->msmpeg4_version= 5; | |
| 525 avctx->delay=0; | |
| 526 break; | |
| 0 | 527 default: |
| 528 return -1; | |
| 529 } | |
| 295 | 530 |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
531 { /* 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
|
532 static int done=0; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
533 if(!done){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
534 int i; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
535 done=1; |
|
292
73a9ce3d9715
fcode_tables where too small, found by Klaas-Pieter Vlieg <vlieg@eurescom.de>
michaelni
parents:
288
diff
changeset
|
536 memset(default_mv_penalty, 0, sizeof(UINT16)*(MAX_FCODE+1)*(2*MAX_MV+1)); |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
537 memset(default_fcode_tab , 0, sizeof(UINT8)*(2*MAX_MV+1)); |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
538 |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
539 for(i=-16; i<16; i++){ |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
540 default_fcode_tab[i + MAX_MV]= 1; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
541 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
542 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
543 } |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
544 s->mv_penalty= default_mv_penalty; |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
545 s->fcode_tab= default_fcode_tab; |
|
506
b9ed0ae72d51
init dc_scale tables to defaults (fixes mjpeg sig11)
michaelni
parents:
499
diff
changeset
|
546 s->y_dc_scale_table= |
|
b9ed0ae72d51
init dc_scale tables to defaults (fixes mjpeg sig11)
michaelni
parents:
499
diff
changeset
|
547 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
|
548 |
| 0 | 549 if (s->out_format == FMT_H263) |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
550 h263_encode_init(s); |
|
281
1fc96b02142e
mpeg4 aspect_ratio_info in AVCodecContext (requested by alex)
michaelni
parents:
280
diff
changeset
|
551 else if (s->out_format == FMT_MPEG1) |
| 499 | 552 ff_mpeg1_encode_init(s); |
| 553 if(s->msmpeg4_version) | |
| 554 ff_msmpeg4_encode_init(s); | |
| 0 | 555 |
| 287 | 556 /* dont use mv_penalty table for crap MV as it would be confused */ |
| 324 | 557 if (s->me_method < ME_EPZS) s->mv_penalty = default_mv_penalty; |
| 287 | 558 |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
559 s->encoding = 1; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
560 |
| 0 | 561 /* init */ |
| 562 if (MPV_common_init(s) < 0) | |
| 563 return -1; | |
| 564 | |
| 60 | 565 /* init default q matrix */ |
| 566 for(i=0;i<64;i++) { | |
| 599 | 567 if(s->codec_id==CODEC_ID_MPEG4 && s->mpeg_quant){ |
| 568 s->intra_matrix[i] = ff_mpeg4_default_intra_matrix[i]; | |
| 569 s->inter_matrix[i] = ff_mpeg4_default_non_intra_matrix[i]; | |
| 570 }else if(s->out_format == FMT_H263){ | |
| 571 s->intra_matrix[i] = | |
| 572 s->inter_matrix[i] = ff_mpeg1_default_non_intra_matrix[i]; | |
| 573 }else{ /* mpeg1 */ | |
|
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
|
574 s->intra_matrix[i] = ff_mpeg1_default_intra_matrix[i]; |
| 599 | 575 s->inter_matrix[i] = ff_mpeg1_default_non_intra_matrix[i]; |
| 576 } | |
| 344 | 577 } |
| 578 | |
| 579 /* precompute matrix */ | |
|
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
580 /* for mjpeg, we do include qscale in the matrix */ |
| 344 | 581 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
|
582 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, s->q_intra_matrix16_bias, |
| 344 | 583 s->intra_matrix, s->intra_quant_bias); |
|
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
|
584 convert_matrix(s, s->q_inter_matrix, s->q_inter_matrix16, s->q_inter_matrix16_bias, |
| 344 | 585 s->inter_matrix, s->inter_quant_bias); |
| 60 | 586 } |
| 587 | |
| 329 | 588 if(ff_rate_control_init(s) < 0) |
| 589 return -1; | |
| 0 | 590 |
| 591 s->picture_number = 0; | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
592 s->picture_in_gop_number = 0; |
| 0 | 593 s->fake_picture_number = 0; |
| 594 /* motion detector init */ | |
| 595 s->f_code = 1; | |
| 324 | 596 s->b_code = 1; |
| 0 | 597 |
| 598 return 0; | |
| 599 } | |
| 600 | |
| 601 int MPV_encode_end(AVCodecContext *avctx) | |
| 602 { | |
| 603 MpegEncContext *s = avctx->priv_data; | |
| 604 | |
| 605 #ifdef STATS | |
| 606 print_stats(); | |
| 607 #endif | |
| 329 | 608 |
| 609 ff_rate_control_uninit(s); | |
| 610 | |
| 0 | 611 MPV_common_end(s); |
| 612 if (s->out_format == FMT_MJPEG) | |
| 613 mjpeg_close(s); | |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
614 |
| 0 | 615 return 0; |
| 616 } | |
| 617 | |
| 618 /* 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
|
619 //FIXME check that this is ok for mpeg4 interlaced |
| 206 | 620 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w) |
| 0 | 621 { |
| 622 UINT8 *ptr, *last_line; | |
| 623 int i; | |
| 624 | |
| 625 last_line = buf + (height - 1) * wrap; | |
| 626 for(i=0;i<w;i++) { | |
| 627 /* top and bottom */ | |
| 628 memcpy(buf - (i + 1) * wrap, buf, width); | |
| 629 memcpy(last_line + (i + 1) * wrap, last_line, width); | |
| 630 } | |
| 631 /* left and right */ | |
| 632 ptr = buf; | |
| 633 for(i=0;i<height;i++) { | |
| 634 memset(ptr - w, ptr[0], w); | |
| 635 memset(ptr + width, ptr[width-1], w); | |
| 636 ptr += wrap; | |
| 637 } | |
| 638 /* corners */ | |
| 639 for(i=0;i<w;i++) { | |
| 640 memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */ | |
| 641 memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */ | |
| 642 memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */ | |
| 643 memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */ | |
| 644 } | |
| 645 } | |
| 646 | |
| 647 /* generic function for encode/decode called before a frame is coded/decoded */ | |
| 553 | 648 void MPV_frame_start(MpegEncContext *s, AVCodecContext *avctx) |
| 0 | 649 { |
| 650 int i; | |
| 651 UINT8 *tmp; | |
| 652 | |
|
46
931417475f5b
fixed mpeg1 first block bug (pb with black picture optimisation for B frames)
glantau
parents:
40
diff
changeset
|
653 s->mb_skiped = 0; |
| 456 | 654 s->decoding_error=0; |
|
562
23e58889a108
exporting mbskip_table after it has been allocated
michaelni
parents:
557
diff
changeset
|
655 avctx->mbskip_table= s->mbskip_table; |
| 456 | 656 |
| 553 | 657 if(avctx->flags&CODEC_FLAG_DR1){ |
| 658 avctx->get_buffer_callback(avctx, s->width, s->height, s->pict_type); | |
| 659 | |
| 556 | 660 s->linesize = avctx->dr_stride; |
| 661 s->uvlinesize= avctx->dr_uvstride; | |
| 662 s->ip_buffer_count= avctx->dr_ip_buffer_count; | |
| 553 | 663 } |
|
562
23e58889a108
exporting mbskip_table after it has been allocated
michaelni
parents:
557
diff
changeset
|
664 avctx->dr_ip_buffer_count= s->ip_buffer_count; |
| 553 | 665 |
| 0 | 666 if (s->pict_type == B_TYPE) { |
| 667 for(i=0;i<3;i++) { | |
| 553 | 668 if(avctx->flags&CODEC_FLAG_DR1) |
| 669 s->aux_picture[i]= avctx->dr_buffer[i]; | |
| 569 | 670 |
| 671 //FIXME the following should never be needed, the decoder should drop b frames if no reference is available | |
| 672 if(s->next_picture[i]==NULL) | |
| 673 s->next_picture[i]= s->aux_picture[i]; | |
| 674 if(s->last_picture[i]==NULL) | |
| 675 s->last_picture[i]= s->next_picture[i]; | |
| 553 | 676 |
| 0 | 677 s->current_picture[i] = s->aux_picture[i]; |
| 678 } | |
| 679 } else { | |
| 680 for(i=0;i<3;i++) { | |
| 681 /* swap next and last */ | |
| 553 | 682 if(avctx->flags&CODEC_FLAG_DR1) |
| 683 tmp= avctx->dr_buffer[i]; | |
| 684 else | |
| 685 tmp = s->last_picture[i]; | |
| 686 | |
| 0 | 687 s->last_picture[i] = s->next_picture[i]; |
| 688 s->next_picture[i] = tmp; | |
| 689 s->current_picture[i] = tmp; | |
| 553 | 690 |
|
592
909e50c67d0e
last_picture should be never == NULL (it was with dr1) this might fix a segfault with error concealment
michaelni
parents:
591
diff
changeset
|
691 if(s->last_picture[i]==NULL) |
|
909e50c67d0e
last_picture should be never == NULL (it was with dr1) this might fix a segfault with error concealment
michaelni
parents:
591
diff
changeset
|
692 s->last_picture[i]= s->next_picture[i]; |
|
909e50c67d0e
last_picture should be never == NULL (it was with dr1) this might fix a segfault with error concealment
michaelni
parents:
591
diff
changeset
|
693 |
| 553 | 694 s->last_dr_opaque= s->next_dr_opaque; |
| 695 s->next_dr_opaque= avctx->dr_opaque_frame; | |
| 696 | |
| 557 | 697 if(s->has_b_frames && s->last_dr_opaque && s->codec_id!=CODEC_ID_SVQ1) |
| 553 | 698 avctx->dr_opaque_frame= s->last_dr_opaque; |
| 699 else | |
| 700 avctx->dr_opaque_frame= s->next_dr_opaque; | |
| 0 | 701 } |
| 702 } | |
| 591 | 703 /* set dequantizer, we cant do it during init as it might change for mpeg4 |
| 704 and we cant do it in the header decode as init isnt called for mpeg4 there yet */ | |
| 705 if(s->out_format == FMT_H263){ | |
| 706 if(s->mpeg_quant) | |
| 707 s->dct_unquantize = s->dct_unquantize_mpeg2; | |
| 708 else | |
| 709 s->dct_unquantize = s->dct_unquantize_h263; | |
| 710 }else | |
| 711 s->dct_unquantize = s->dct_unquantize_mpeg1; | |
| 0 | 712 } |
|
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
|
713 |
| 0 | 714 /* generic function for encode/decode called after a frame has been coded/decoded */ |
| 715 void MPV_frame_end(MpegEncContext *s) | |
| 716 { | |
| 456 | 717 // if((s->picture_number%100)==0 && s->encoding) printf("sads:%d //\n", sads); |
| 718 | |
| 0 | 719 /* draw edge for correct motion prediction if outside */ |
| 553 | 720 if (s->pict_type != B_TYPE && !s->intra_only && !(s->flags&CODEC_FLAG_EMU_EDGE)) { |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
721 draw_edges(s->current_picture[0], s->linesize , s->h_edge_pos , s->v_edge_pos , EDGE_WIDTH ); |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
722 draw_edges(s->current_picture[1], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
723 draw_edges(s->current_picture[2], s->uvlinesize, s->h_edge_pos>>1, s->v_edge_pos>>1, EDGE_WIDTH/2); |
| 0 | 724 } |
| 207 | 725 emms_c(); |
| 329 | 726 |
| 612 | 727 s->last_pict_type = s->pict_type; |
| 329 | 728 if(s->pict_type!=B_TYPE){ |
| 729 s->last_non_b_pict_type= s->pict_type; | |
| 341 | 730 s->num_available_buffers++; |
| 731 if(s->num_available_buffers>2) s->num_available_buffers= 2; | |
| 329 | 732 } |
| 0 | 733 } |
| 734 | |
| 324 | 735 /* reorder input for encoding */ |
| 736 void reorder_input(MpegEncContext *s, AVPicture *pict) | |
| 737 { | |
| 738 int i, j, index; | |
| 739 | |
| 740 if(s->max_b_frames > FF_MAX_B_FRAMES) s->max_b_frames= FF_MAX_B_FRAMES; | |
| 741 | |
| 742 // delay= s->max_b_frames+1; (or 0 if no b frames cuz decoder diff) | |
| 743 | |
| 744 for(j=0; j<REORDER_BUFFER_SIZE-1; j++){ | |
| 745 s->coded_order[j]= s->coded_order[j+1]; | |
| 746 } | |
| 747 s->coded_order[j].picture[0]= s->coded_order[j].picture[1]= s->coded_order[j].picture[2]= NULL; //catch uninitalized buffers | |
| 339 | 748 s->coded_order[j].pict_type=0; |
| 324 | 749 |
| 750 switch(s->input_pict_type){ | |
| 751 default: | |
| 752 case I_TYPE: | |
| 753 case S_TYPE: | |
| 754 case P_TYPE: | |
| 755 index= s->max_b_frames - s->b_frames_since_non_b; | |
| 756 s->b_frames_since_non_b=0; | |
| 757 break; | |
| 758 case B_TYPE: | |
| 759 index= s->max_b_frames + 1; | |
| 760 s->b_frames_since_non_b++; | |
| 761 break; | |
| 762 } | |
| 763 //printf("index:%d type:%d strides: %d %d\n", index, s->input_pict_type, pict->linesize[0], s->linesize); | |
| 764 if( (index==0 || (s->flags&CODEC_FLAG_INPUT_PRESERVED)) | |
| 765 && pict->linesize[0] == s->linesize | |
| 556 | 766 && pict->linesize[1] == s->uvlinesize |
| 767 && pict->linesize[2] == s->uvlinesize){ | |
| 324 | 768 //printf("ptr\n"); |
| 769 for(i=0; i<3; i++){ | |
| 770 s->coded_order[index].picture[i]= pict->data[i]; | |
| 771 } | |
| 772 }else{ | |
| 773 //printf("copy\n"); | |
| 774 for(i=0; i<3; i++){ | |
| 775 uint8_t *src = pict->data[i]; | |
| 776 uint8_t *dest; | |
| 777 int src_wrap = pict->linesize[i]; | |
| 778 int dest_wrap = s->linesize; | |
| 779 int w = s->width; | |
| 780 int h = s->height; | |
| 781 | |
| 782 if(index==0) dest= s->last_picture[i]+16; //is current_picture indeed but the switch hapens after reordering | |
| 783 else dest= s->picture_buffer[s->picture_buffer_index][i]; | |
| 784 | |
| 785 if (i >= 1) { | |
| 786 dest_wrap >>= 1; | |
| 787 w >>= 1; | |
| 788 h >>= 1; | |
| 789 } | |
| 790 | |
| 791 s->coded_order[index].picture[i]= dest; | |
| 792 for(j=0;j<h;j++) { | |
| 793 memcpy(dest, src, w); | |
| 794 dest += dest_wrap; | |
| 795 src += src_wrap; | |
| 796 } | |
| 797 } | |
| 798 if(index!=0){ | |
| 799 s->picture_buffer_index++; | |
| 588 | 800 if(s->picture_buffer_index >= REORDER_BUFFER_SIZE) s->picture_buffer_index=0; |
| 324 | 801 } |
| 802 } | |
| 803 s->coded_order[index].pict_type = s->input_pict_type; | |
| 804 s->coded_order[index].qscale = s->input_qscale; | |
| 805 s->coded_order[index].force_type= s->force_input_type; | |
| 806 s->coded_order[index].picture_in_gop_number= s->input_picture_in_gop_number; | |
| 807 s->coded_order[index].picture_number= s->input_picture_number; | |
| 808 | |
| 809 for(i=0; i<3; i++){ | |
| 810 s->new_picture[i]= s->coded_order[0].picture[i]; | |
| 811 } | |
| 812 } | |
| 813 | |
| 0 | 814 int MPV_encode_picture(AVCodecContext *avctx, |
| 815 unsigned char *buf, int buf_size, void *data) | |
| 816 { | |
| 817 MpegEncContext *s = avctx->priv_data; | |
| 818 AVPicture *pict = data; | |
| 819 | |
| 324 | 820 s->input_qscale = avctx->quality; |
| 0 | 821 |
| 822 init_put_bits(&s->pb, buf, buf_size, NULL, NULL); | |
| 823 | |
| 329 | 824 if(avctx->flags&CODEC_FLAG_TYPE){ |
| 825 s->input_pict_type= | |
| 826 s->force_input_type= avctx->key_frame ? I_TYPE : P_TYPE; | |
| 827 }else if(s->flags&CODEC_FLAG_PASS2){ | |
| 828 s->input_pict_type= | |
| 829 s->force_input_type= s->rc_context.entry[s->input_picture_number].new_pict_type; | |
| 830 }else{ | |
| 831 s->force_input_type=0; | |
| 832 if (!s->intra_only) { | |
| 833 /* first picture of GOP is intra */ | |
| 834 if (s->input_picture_in_gop_number % s->gop_size==0){ | |
| 835 s->input_pict_type = I_TYPE; | |
| 836 }else if(s->max_b_frames==0){ | |
| 837 s->input_pict_type = P_TYPE; | |
| 838 }else{ | |
| 839 if(s->b_frames_since_non_b < s->max_b_frames) //FIXME more IQ | |
| 840 s->input_pict_type = B_TYPE; | |
| 841 else | |
| 842 s->input_pict_type = P_TYPE; | |
| 843 } | |
| 844 } else { | |
| 324 | 845 s->input_pict_type = I_TYPE; |
| 846 } | |
| 0 | 847 } |
| 848 | |
| 329 | 849 if(s->input_pict_type==I_TYPE) |
| 850 s->input_picture_in_gop_number=0; | |
| 851 | |
| 324 | 852 reorder_input(s, pict); |
| 853 | |
| 854 /* output? */ | |
| 855 if(s->coded_order[0].picture[0]){ | |
| 856 | |
| 857 s->pict_type= s->coded_order[0].pict_type; | |
| 858 if (s->fixed_qscale) /* the ratecontrol needs the last qscale so we dont touch it for CBR */ | |
| 859 s->qscale= s->coded_order[0].qscale; | |
| 860 s->force_type= s->coded_order[0].force_type; | |
| 861 s->picture_in_gop_number= s->coded_order[0].picture_in_gop_number; | |
| 862 s->picture_number= s->coded_order[0].picture_number; | |
| 863 | |
| 553 | 864 MPV_frame_start(s, avctx); |
| 286 | 865 |
| 324 | 866 encode_picture(s, s->picture_number); |
| 376 | 867 avctx->key_frame = (s->pict_type == I_TYPE); |
| 868 avctx->pict_type = s->pict_type; | |
| 869 avctx->real_pict_num = s->picture_number; | |
| 324 | 870 avctx->header_bits = s->header_bits; |
| 871 avctx->mv_bits = s->mv_bits; | |
| 872 avctx->misc_bits = s->misc_bits; | |
| 873 avctx->i_tex_bits = s->i_tex_bits; | |
| 874 avctx->p_tex_bits = s->p_tex_bits; | |
| 875 avctx->i_count = s->i_count; | |
| 876 avctx->p_count = s->p_count; | |
| 877 avctx->skip_count = s->skip_count; | |
| 0 | 878 |
| 324 | 879 MPV_frame_end(s); |
| 880 | |
| 881 if (s->out_format == FMT_MJPEG) | |
| 882 mjpeg_picture_trailer(s); | |
| 883 | |
| 884 avctx->quality = s->qscale; | |
| 329 | 885 |
| 886 if(s->flags&CODEC_FLAG_PASS1) | |
| 887 ff_write_pass1_stats(s); | |
| 456 | 888 |
| 324 | 889 } |
| 890 | |
| 891 s->input_picture_number++; | |
| 892 s->input_picture_in_gop_number++; | |
| 0 | 893 |
| 894 flush_put_bits(&s->pb); | |
| 268 | 895 s->frame_bits = (pbBufPtr(&s->pb) - s->pb.buf) * 8; |
| 612 | 896 |
| 268 | 897 s->total_bits += s->frame_bits; |
| 286 | 898 avctx->frame_bits = s->frame_bits; |
| 899 //printf("fcode: %d, type: %d, head: %d, mv: %d, misc: %d, frame: %d, itex: %d, ptex: %d\n", | |
| 900 //s->f_code, avctx->key_frame, s->header_bits, s->mv_bits, s->misc_bits, s->frame_bits, s->i_tex_bits, s->p_tex_bits); | |
| 612 | 901 #if 0 //dump some stats to stats.txt for testing/debuging |
| 902 if(s->max_b_frames==0) | |
| 903 { | |
| 904 static FILE *f=NULL; | |
| 905 if(!f) f= fopen("stats.txt", "wb"); | |
| 906 get_psnr(pict->data, s->current_picture, | |
| 907 pict->linesize, s->linesize, avctx); | |
| 908 fprintf(f, "%7d, %7d, %2.4f\n", pbBufPtr(&s->pb) - s->pb.buf, s->qscale, avctx->psnr_y); | |
| 909 } | |
| 910 #endif | |
|
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
|
911 |
|
252
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
912 if (avctx->get_psnr) { |
|
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
913 /* At this point pict->data should have the original frame */ |
|
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
914 /* an s->current_picture should have the coded/decoded frame */ |
|
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
915 get_psnr(pict->data, s->current_picture, |
|
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
916 pict->linesize, s->linesize, avctx); |
| 324 | 917 // printf("%f\n", avctx->psnr_y); |
|
252
ddb1a0e94cf4
- Added PSNR feature to libavcodec and ffmpeg. By now just Y PSNR until I'm
pulento
parents:
251
diff
changeset
|
918 } |
|
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
|
919 return pbBufPtr(&s->pb) - s->pb.buf; |
| 0 | 920 } |
| 921 | |
| 255 | 922 static inline void gmc1_motion(MpegEncContext *s, |
| 923 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 924 int dest_offset, | |
| 925 UINT8 **ref_picture, int src_offset, | |
| 926 int h) | |
| 927 { | |
| 928 UINT8 *ptr; | |
| 556 | 929 int offset, src_x, src_y, linesize, uvlinesize; |
| 255 | 930 int motion_x, motion_y; |
| 566 | 931 int emu=0; |
| 255 | 932 |
|
265
4e9e728021d8
use ac prediction in mpeg4 encoding (5% smaller intra-blocks/keyframes)
michaelni
parents:
262
diff
changeset
|
933 if(s->real_sprite_warping_points>1) printf("more than 1 warp point isnt supported\n"); |
| 255 | 934 motion_x= s->sprite_offset[0][0]; |
| 935 motion_y= s->sprite_offset[0][1]; | |
| 936 src_x = s->mb_x * 16 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
| 937 src_y = s->mb_y * 16 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
| 938 motion_x<<=(3-s->sprite_warping_accuracy); | |
| 939 motion_y<<=(3-s->sprite_warping_accuracy); | |
| 940 src_x = clip(src_x, -16, s->width); | |
| 941 if (src_x == s->width) | |
| 942 motion_x =0; | |
| 943 src_y = clip(src_y, -16, s->height); | |
| 944 if (src_y == s->height) | |
| 945 motion_y =0; | |
| 946 | |
| 947 linesize = s->linesize; | |
| 556 | 948 uvlinesize = s->uvlinesize; |
| 255 | 949 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; |
| 950 | |
| 951 dest_y+=dest_offset; | |
| 566 | 952 if(s->flags&CODEC_FLAG_EMU_EDGE){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
953 if(src_x<0 || src_y<0 || src_x + (motion_x&15) + 16 > s->h_edge_pos |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
954 || src_y + (motion_y&15) + h > s->v_edge_pos){ |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
955 emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->h_edge_pos, s->v_edge_pos); |
| 566 | 956 ptr= s->edge_emu_buffer; |
| 957 emu=1; | |
| 958 } | |
| 959 } | |
| 255 | 960 gmc1(dest_y , ptr , linesize, h, motion_x&15, motion_y&15, s->no_rounding); |
| 961 gmc1(dest_y+8, ptr+8, linesize, h, motion_x&15, motion_y&15, s->no_rounding); | |
| 962 | |
| 963 motion_x= s->sprite_offset[1][0]; | |
| 964 motion_y= s->sprite_offset[1][1]; | |
| 965 src_x = s->mb_x * 8 + (motion_x >> (s->sprite_warping_accuracy+1)); | |
| 966 src_y = s->mb_y * 8 + (motion_y >> (s->sprite_warping_accuracy+1)); | |
| 967 motion_x<<=(3-s->sprite_warping_accuracy); | |
| 968 motion_y<<=(3-s->sprite_warping_accuracy); | |
| 969 src_x = clip(src_x, -8, s->width>>1); | |
| 970 if (src_x == s->width>>1) | |
| 971 motion_x =0; | |
| 972 src_y = clip(src_y, -8, s->height>>1); | |
| 973 if (src_y == s->height>>1) | |
| 974 motion_y =0; | |
| 975 | |
| 556 | 976 offset = (src_y * uvlinesize) + src_x + (src_offset>>1); |
| 255 | 977 ptr = ref_picture[1] + offset; |
| 566 | 978 if(emu){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
979 emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); |
| 566 | 980 ptr= s->edge_emu_buffer; |
| 981 } | |
| 556 | 982 gmc1(dest_cb + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding); |
| 566 | 983 |
| 255 | 984 ptr = ref_picture[2] + offset; |
| 566 | 985 if(emu){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
986 emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); |
| 566 | 987 ptr= s->edge_emu_buffer; |
| 988 } | |
| 556 | 989 gmc1(dest_cr + (dest_offset>>1), ptr, uvlinesize, h>>1, motion_x&15, motion_y&15, s->no_rounding); |
| 255 | 990 |
| 991 return; | |
| 992 } | |
| 993 | |
| 566 | 994 static void emulated_edge_mc(MpegEncContext *s, UINT8 *src, int linesize, int block_w, int block_h, |
| 553 | 995 int src_x, int src_y, int w, int h){ |
| 996 int x, y; | |
| 997 int start_y, start_x, end_y, end_x; | |
| 566 | 998 UINT8 *buf= s->edge_emu_buffer; |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
999 |
| 553 | 1000 if(src_y>= h){ |
| 1001 src+= (h-1-src_y)*linesize; | |
| 1002 src_y=h-1; | |
| 554 | 1003 }else if(src_y<=-block_h){ |
| 1004 src+= (1-block_h-src_y)*linesize; | |
| 1005 src_y=1-block_h; | |
| 553 | 1006 } |
| 1007 if(src_x>= w){ | |
| 1008 src+= (w-1-src_x); | |
| 1009 src_x=w-1; | |
| 554 | 1010 }else if(src_x<=-block_w){ |
| 1011 src+= (1-block_w-src_x); | |
| 1012 src_x=1-block_w; | |
| 553 | 1013 } |
| 1014 | |
| 1015 start_y= MAX(0, -src_y); | |
| 1016 start_x= MAX(0, -src_x); | |
| 1017 end_y= MIN(block_h, h-src_y); | |
| 1018 end_x= MIN(block_w, w-src_x); | |
| 566 | 1019 |
| 553 | 1020 // copy existing part |
| 1021 for(y=start_y; y<end_y; y++){ | |
| 1022 for(x=start_x; x<end_x; x++){ | |
| 1023 buf[x + y*linesize]= src[x + y*linesize]; | |
| 1024 } | |
| 1025 } | |
| 1026 | |
| 1027 //top | |
| 1028 for(y=0; y<start_y; y++){ | |
| 1029 for(x=start_x; x<end_x; x++){ | |
| 1030 buf[x + y*linesize]= buf[x + start_y*linesize]; | |
| 1031 } | |
| 1032 } | |
| 1033 | |
| 1034 //bottom | |
| 1035 for(y=end_y; y<block_h; y++){ | |
| 1036 for(x=start_x; x<end_x; x++){ | |
| 1037 buf[x + y*linesize]= buf[x + (end_y-1)*linesize]; | |
| 1038 } | |
| 1039 } | |
| 1040 | |
| 1041 for(y=0; y<block_h; y++){ | |
| 1042 //left | |
| 1043 for(x=0; x<start_x; x++){ | |
| 1044 buf[x + y*linesize]= buf[start_x + y*linesize]; | |
| 1045 } | |
| 1046 | |
| 1047 //right | |
| 1048 for(x=end_x; x<block_w; x++){ | |
| 1049 buf[x + y*linesize]= buf[end_x - 1 + y*linesize]; | |
| 1050 } | |
| 1051 } | |
| 1052 } | |
| 1053 | |
| 1054 | |
| 0 | 1055 /* apply one mpeg motion vector to the three components */ |
| 1056 static inline void mpeg_motion(MpegEncContext *s, | |
| 1057 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 1058 int dest_offset, | |
| 1059 UINT8 **ref_picture, int src_offset, | |
| 1060 int field_based, op_pixels_func *pix_op, | |
| 1061 int motion_x, int motion_y, int h) | |
| 1062 { | |
| 1063 UINT8 *ptr; | |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1064 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize, uvlinesize; |
| 553 | 1065 int emu=0; |
| 1066 | |
| 255 | 1067 if(s->quarter_sample) |
| 1068 { | |
| 1069 motion_x>>=1; | |
| 1070 motion_y>>=1; | |
| 1071 } | |
| 0 | 1072 dxy = ((motion_y & 1) << 1) | (motion_x & 1); |
| 1073 src_x = s->mb_x * 16 + (motion_x >> 1); | |
| 1074 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1); | |
| 1075 | |
| 1076 /* WARNING: do no forget half pels */ | |
| 1077 height = s->height >> field_based; | |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1078 v_edge_pos = s->v_edge_pos >> field_based; |
| 0 | 1079 src_x = clip(src_x, -16, s->width); |
| 1080 if (src_x == s->width) | |
| 1081 dxy &= ~1; | |
| 1082 src_y = clip(src_y, -16, height); | |
| 1083 if (src_y == height) | |
| 1084 dxy &= ~2; | |
|
565
44d744901ded
interlaced mpeg2 fix ... replacing linesize>>1 by uvlinesize brainlessly wasnt a good idea
michaelni
parents:
562
diff
changeset
|
1085 linesize = s->linesize << field_based; |
|
44d744901ded
interlaced mpeg2 fix ... replacing linesize>>1 by uvlinesize brainlessly wasnt a good idea
michaelni
parents:
562
diff
changeset
|
1086 uvlinesize = s->uvlinesize << field_based; |
| 0 | 1087 ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset; |
| 1088 dest_y += dest_offset; | |
| 553 | 1089 |
| 1090 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1091 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
|
1092 || src_y + (motion_y&1) + h > v_edge_pos){ |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1093 emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->h_edge_pos, v_edge_pos); |
| 553 | 1094 ptr= s->edge_emu_buffer; |
| 1095 emu=1; | |
| 1096 } | |
| 1097 } | |
| 0 | 1098 pix_op[dxy](dest_y, ptr, linesize, h); |
| 1099 pix_op[dxy](dest_y + 8, ptr + 8, linesize, h); | |
| 1100 | |
| 485 | 1101 if(s->flags&CODEC_FLAG_GRAY) return; |
| 1102 | |
| 0 | 1103 if (s->out_format == FMT_H263) { |
| 1104 dxy = 0; | |
| 1105 if ((motion_x & 3) != 0) | |
| 1106 dxy |= 1; | |
| 1107 if ((motion_y & 3) != 0) | |
| 1108 dxy |= 2; | |
| 1109 mx = motion_x >> 2; | |
| 1110 my = motion_y >> 2; | |
| 1111 } else { | |
| 1112 mx = motion_x / 2; | |
| 1113 my = motion_y / 2; | |
| 1114 dxy = ((my & 1) << 1) | (mx & 1); | |
| 1115 mx >>= 1; | |
| 1116 my >>= 1; | |
| 1117 } | |
| 1118 | |
| 1119 src_x = s->mb_x * 8 + mx; | |
| 1120 src_y = s->mb_y * (8 >> field_based) + my; | |
| 1121 src_x = clip(src_x, -8, s->width >> 1); | |
| 1122 if (src_x == (s->width >> 1)) | |
| 1123 dxy &= ~1; | |
| 1124 src_y = clip(src_y, -8, height >> 1); | |
| 1125 if (src_y == (height >> 1)) | |
| 1126 dxy &= ~2; | |
|
565
44d744901ded
interlaced mpeg2 fix ... replacing linesize>>1 by uvlinesize brainlessly wasnt a good idea
michaelni
parents:
562
diff
changeset
|
1127 offset = (src_y * uvlinesize) + src_x + (src_offset >> 1); |
| 0 | 1128 ptr = ref_picture[1] + offset; |
| 553 | 1129 if(emu){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1130 emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1); |
| 553 | 1131 ptr= s->edge_emu_buffer; |
| 1132 } | |
|
565
44d744901ded
interlaced mpeg2 fix ... replacing linesize>>1 by uvlinesize brainlessly wasnt a good idea
michaelni
parents:
562
diff
changeset
|
1133 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, uvlinesize, h >> 1); |
| 553 | 1134 |
| 0 | 1135 ptr = ref_picture[2] + offset; |
| 553 | 1136 if(emu){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1137 emulated_edge_mc(s, ptr, uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1); |
| 553 | 1138 ptr= s->edge_emu_buffer; |
| 1139 } | |
|
565
44d744901ded
interlaced mpeg2 fix ... replacing linesize>>1 by uvlinesize brainlessly wasnt a good idea
michaelni
parents:
562
diff
changeset
|
1140 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, uvlinesize, h >> 1); |
| 0 | 1141 } |
| 1142 | |
| 255 | 1143 static inline void qpel_motion(MpegEncContext *s, |
| 1144 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 1145 int dest_offset, | |
| 1146 UINT8 **ref_picture, int src_offset, | |
| 1147 int field_based, op_pixels_func *pix_op, | |
| 1148 qpel_mc_func *qpix_op, | |
| 1149 int motion_x, int motion_y, int h) | |
| 1150 { | |
| 1151 UINT8 *ptr; | |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1152 int dxy, offset, mx, my, src_x, src_y, height, v_edge_pos, linesize; |
| 554 | 1153 int emu=0; |
| 255 | 1154 |
| 1155 dxy = ((motion_y & 3) << 2) | (motion_x & 3); | |
| 1156 src_x = s->mb_x * 16 + (motion_x >> 2); | |
| 1157 src_y = s->mb_y * (16 >> field_based) + (motion_y >> 2); | |
| 1158 | |
| 1159 height = s->height >> field_based; | |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1160 v_edge_pos = s->v_edge_pos >> field_based; |
| 255 | 1161 src_x = clip(src_x, -16, s->width); |
| 1162 if (src_x == s->width) | |
| 1163 dxy &= ~3; | |
| 1164 src_y = clip(src_y, -16, height); | |
| 1165 if (src_y == height) | |
| 1166 dxy &= ~12; | |
| 1167 linesize = s->linesize << field_based; | |
| 1168 ptr = ref_picture[0] + (src_y * linesize) + src_x + src_offset; | |
| 1169 dest_y += dest_offset; | |
| 1170 //printf("%d %d %d\n", src_x, src_y, dxy); | |
| 554 | 1171 |
| 1172 if(s->flags&CODEC_FLAG_EMU_EDGE){ | |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1173 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
|
1174 || src_y + (motion_y&3) + h > v_edge_pos){ |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1175 emulated_edge_mc(s, ptr, linesize, 17, h+1, src_x, src_y, s->h_edge_pos, v_edge_pos); |
| 554 | 1176 ptr= s->edge_emu_buffer; |
| 1177 emu=1; | |
| 1178 } | |
| 1179 } | |
| 255 | 1180 qpix_op[dxy](dest_y , ptr , linesize, linesize, motion_x&3, motion_y&3); |
| 1181 qpix_op[dxy](dest_y + 8, ptr + 8, linesize, linesize, motion_x&3, motion_y&3); | |
| 1182 qpix_op[dxy](dest_y + linesize*8 , ptr + linesize*8 , linesize, linesize, motion_x&3, motion_y&3); | |
| 1183 qpix_op[dxy](dest_y + linesize*8 + 8, ptr + linesize*8 + 8, linesize, linesize, motion_x&3, motion_y&3); | |
| 1184 | |
| 485 | 1185 if(s->flags&CODEC_FLAG_GRAY) return; |
| 1186 | |
| 255 | 1187 mx= (motion_x>>1) | (motion_x&1); |
| 1188 my= (motion_y>>1) | (motion_y&1); | |
| 1189 | |
| 1190 dxy = 0; | |
| 1191 if ((mx & 3) != 0) | |
| 1192 dxy |= 1; | |
| 1193 if ((my & 3) != 0) | |
| 1194 dxy |= 2; | |
| 1195 mx = mx >> 2; | |
| 1196 my = my >> 2; | |
| 1197 | |
| 1198 src_x = s->mb_x * 8 + mx; | |
| 1199 src_y = s->mb_y * (8 >> field_based) + my; | |
| 1200 src_x = clip(src_x, -8, s->width >> 1); | |
| 1201 if (src_x == (s->width >> 1)) | |
| 1202 dxy &= ~1; | |
| 1203 src_y = clip(src_y, -8, height >> 1); | |
| 1204 if (src_y == (height >> 1)) | |
| 1205 dxy &= ~2; | |
| 1206 | |
| 556 | 1207 offset = (src_y * s->uvlinesize) + src_x + (src_offset >> 1); |
| 255 | 1208 ptr = ref_picture[1] + offset; |
| 554 | 1209 if(emu){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1210 emulated_edge_mc(s, ptr, s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1); |
| 554 | 1211 ptr= s->edge_emu_buffer; |
| 1212 } | |
| 556 | 1213 pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, s->uvlinesize, h >> 1); |
| 554 | 1214 |
| 255 | 1215 ptr = ref_picture[2] + offset; |
| 554 | 1216 if(emu){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1217 emulated_edge_mc(s, ptr, s->uvlinesize, 9, (h>>1)+1, src_x, src_y, s->h_edge_pos>>1, v_edge_pos>>1); |
| 554 | 1218 ptr= s->edge_emu_buffer; |
| 1219 } | |
| 556 | 1220 pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, s->uvlinesize, h >> 1); |
| 255 | 1221 } |
| 1222 | |
| 1223 | |
| 0 | 1224 static inline void MPV_motion(MpegEncContext *s, |
| 1225 UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr, | |
| 1226 int dir, UINT8 **ref_picture, | |
| 255 | 1227 op_pixels_func *pix_op, qpel_mc_func *qpix_op) |
| 0 | 1228 { |
| 1229 int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y; | |
| 1230 int mb_x, mb_y, i; | |
| 1231 UINT8 *ptr, *dest; | |
| 554 | 1232 int emu=0; |
| 0 | 1233 |
| 1234 mb_x = s->mb_x; | |
| 1235 mb_y = s->mb_y; | |
| 1236 | |
| 1237 switch(s->mv_type) { | |
| 1238 case MV_TYPE_16X16: | |
| 255 | 1239 if(s->mcsel){ |
| 1240 #if 0 | |
| 1241 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 1242 ref_picture, 0, | |
| 1243 0, pix_op, | |
| 1244 s->sprite_offset[0][0]>>3, | |
| 1245 s->sprite_offset[0][1]>>3, | |
| 1246 16); | |
| 1247 #else | |
| 1248 gmc1_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 1249 ref_picture, 0, | |
| 1250 16); | |
| 1251 #endif | |
| 262 | 1252 }else if(s->quarter_sample && dir==0){ //FIXME |
| 255 | 1253 qpel_motion(s, dest_y, dest_cb, dest_cr, 0, |
| 1254 ref_picture, 0, | |
| 1255 0, pix_op, qpix_op, | |
| 1256 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
| 1257 }else{ | |
| 1258 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 1259 ref_picture, 0, | |
| 1260 0, pix_op, | |
| 1261 s->mv[dir][0][0], s->mv[dir][0][1], 16); | |
| 1262 } | |
| 0 | 1263 break; |
| 1264 case MV_TYPE_8X8: | |
| 1265 for(i=0;i<4;i++) { | |
| 1266 motion_x = s->mv[dir][i][0]; | |
| 1267 motion_y = s->mv[dir][i][1]; | |
| 1268 | |
| 1269 dxy = ((motion_y & 1) << 1) | (motion_x & 1); | |
| 1270 src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8; | |
| 295 | 1271 src_y = mb_y * 16 + (motion_y >> 1) + (i >>1) * 8; |
| 0 | 1272 |
| 1273 /* WARNING: do no forget half pels */ | |
| 1274 src_x = clip(src_x, -16, s->width); | |
| 1275 if (src_x == s->width) | |
| 1276 dxy &= ~1; | |
| 1277 src_y = clip(src_y, -16, s->height); | |
| 1278 if (src_y == s->height) | |
| 1279 dxy &= ~2; | |
| 1280 | |
| 1281 ptr = ref_picture[0] + (src_y * s->linesize) + (src_x); | |
| 554 | 1282 if(s->flags&CODEC_FLAG_EMU_EDGE){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1283 if(src_x<0 || src_y<0 || src_x + (motion_x&1) + 8 > s->h_edge_pos |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1284 || src_y + (motion_y&1) + 8 > s->v_edge_pos){ |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1285 emulated_edge_mc(s, ptr, s->linesize, 9, 9, src_x, src_y, s->h_edge_pos, s->v_edge_pos); |
| 554 | 1286 ptr= s->edge_emu_buffer; |
| 1287 } | |
| 1288 } | |
| 0 | 1289 dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize; |
| 1290 pix_op[dxy](dest, ptr, s->linesize, 8); | |
| 1291 } | |
| 485 | 1292 |
| 1293 if(s->flags&CODEC_FLAG_GRAY) break; | |
| 0 | 1294 /* In case of 8X8, we construct a single chroma motion vector |
| 1295 with a special rounding */ | |
| 1296 mx = 0; | |
| 1297 my = 0; | |
| 1298 for(i=0;i<4;i++) { | |
| 1299 mx += s->mv[dir][i][0]; | |
| 1300 my += s->mv[dir][i][1]; | |
| 1301 } | |
| 1302 if (mx >= 0) | |
| 1303 mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
| 1304 else { | |
| 1305 mx = -mx; | |
| 1306 mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1)); | |
| 1307 } | |
| 1308 if (my >= 0) | |
| 1309 my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
| 1310 else { | |
| 1311 my = -my; | |
| 1312 my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1)); | |
| 1313 } | |
| 1314 dxy = ((my & 1) << 1) | (mx & 1); | |
| 1315 mx >>= 1; | |
| 1316 my >>= 1; | |
| 1317 | |
| 1318 src_x = mb_x * 8 + mx; | |
| 1319 src_y = mb_y * 8 + my; | |
| 1320 src_x = clip(src_x, -8, s->width/2); | |
| 1321 if (src_x == s->width/2) | |
| 1322 dxy &= ~1; | |
| 1323 src_y = clip(src_y, -8, s->height/2); | |
| 1324 if (src_y == s->height/2) | |
| 1325 dxy &= ~2; | |
| 1326 | |
| 556 | 1327 offset = (src_y * (s->uvlinesize)) + src_x; |
| 0 | 1328 ptr = ref_picture[1] + offset; |
| 554 | 1329 if(s->flags&CODEC_FLAG_EMU_EDGE){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1330 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
|
1331 || src_y + (dxy>>1) + 8 > s->v_edge_pos>>1){ |
|
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1332 emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); |
| 554 | 1333 ptr= s->edge_emu_buffer; |
| 1334 emu=1; | |
| 1335 } | |
| 1336 } | |
| 556 | 1337 pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8); |
| 554 | 1338 |
| 0 | 1339 ptr = ref_picture[2] + offset; |
| 554 | 1340 if(emu){ |
|
582
5132a4ee50cd
different edge positions fixed with edge emu / dr1
michaelni
parents:
575
diff
changeset
|
1341 emulated_edge_mc(s, ptr, s->uvlinesize, 9, 9, src_x, src_y, s->h_edge_pos>>1, s->v_edge_pos>>1); |
| 554 | 1342 ptr= s->edge_emu_buffer; |
| 1343 } | |
| 556 | 1344 pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8); |
| 0 | 1345 break; |
| 1346 case MV_TYPE_FIELD: | |
| 1347 if (s->picture_structure == PICT_FRAME) { | |
| 1348 /* top field */ | |
| 1349 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 1350 ref_picture, s->field_select[dir][0] ? s->linesize : 0, | |
| 1351 1, pix_op, | |
| 1352 s->mv[dir][0][0], s->mv[dir][0][1], 8); | |
| 1353 /* bottom field */ | |
| 1354 mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize, | |
| 1355 ref_picture, s->field_select[dir][1] ? s->linesize : 0, | |
| 1356 1, pix_op, | |
| 1357 s->mv[dir][1][0], s->mv[dir][1][1], 8); | |
| 1358 } else { | |
| 1359 | |
| 1360 | |
| 1361 } | |
| 1362 break; | |
| 1363 } | |
| 1364 } | |
| 1365 | |
| 1366 | |
| 1367 /* put block[] to dest[] */ | |
| 1368 static inline void put_dct(MpegEncContext *s, | |
| 1369 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
| 1370 { | |
| 1371 if (!s->mpeg2) | |
|
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
|
1372 s->dct_unquantize(s, block, i, s->qscale); |
| 478 | 1373 ff_idct_put (dest, line_size, block); |
| 0 | 1374 } |
| 1375 | |
| 1376 /* add block[] to dest[] */ | |
| 1377 static inline void add_dct(MpegEncContext *s, | |
| 1378 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
| 1379 { | |
| 1380 if (s->block_last_index[i] >= 0) { | |
| 481 | 1381 ff_idct_add (dest, line_size, block); |
| 1382 } | |
| 1383 } | |
| 1384 | |
| 1385 static inline void add_dequant_dct(MpegEncContext *s, | |
| 1386 DCTELEM *block, int i, UINT8 *dest, int line_size) | |
| 1387 { | |
| 1388 if (s->block_last_index[i] >= 0) { | |
| 1389 s->dct_unquantize(s, block, i, s->qscale); | |
| 324 | 1390 |
| 478 | 1391 ff_idct_add (dest, line_size, block); |
| 0 | 1392 } |
| 1393 } | |
| 1394 | |
| 456 | 1395 /** |
| 1396 * cleans dc, ac, coded_block for the current non intra MB | |
| 1397 */ | |
| 1398 void ff_clean_intra_table_entries(MpegEncContext *s) | |
| 1399 { | |
| 1400 int wrap = s->block_wrap[0]; | |
| 1401 int xy = s->block_index[0]; | |
| 1402 | |
| 1403 s->dc_val[0][xy ] = | |
| 1404 s->dc_val[0][xy + 1 ] = | |
| 1405 s->dc_val[0][xy + wrap] = | |
| 1406 s->dc_val[0][xy + 1 + wrap] = 1024; | |
| 1407 /* ac pred */ | |
| 1408 memset(s->ac_val[0][xy ], 0, 32 * sizeof(INT16)); | |
| 1409 memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(INT16)); | |
| 1410 if (s->msmpeg4_version>=3) { | |
| 1411 s->coded_block[xy ] = | |
| 1412 s->coded_block[xy + 1 ] = | |
| 1413 s->coded_block[xy + wrap] = | |
| 1414 s->coded_block[xy + 1 + wrap] = 0; | |
| 1415 } | |
| 1416 /* chroma */ | |
| 1417 wrap = s->block_wrap[4]; | |
| 1418 xy = s->mb_x + 1 + (s->mb_y + 1) * wrap; | |
| 1419 s->dc_val[1][xy] = | |
| 1420 s->dc_val[2][xy] = 1024; | |
| 1421 /* ac pred */ | |
| 1422 memset(s->ac_val[1][xy], 0, 16 * sizeof(INT16)); | |
| 1423 memset(s->ac_val[2][xy], 0, 16 * sizeof(INT16)); | |
| 1424 | |
| 1425 s->mbintra_table[s->mb_x + s->mb_y*s->mb_width]= 0; | |
| 1426 } | |
| 1427 | |
| 0 | 1428 /* generic function called after a macroblock has been parsed by the |
| 1429 decoder or after it has been encoded by the encoder. | |
| 1430 | |
| 1431 Important variables used: | |
| 1432 s->mb_intra : true if intra macroblock | |
| 1433 s->mv_dir : motion vector direction | |
| 1434 s->mv_type : motion vector type | |
| 1435 s->mv : motion vector | |
| 1436 s->interlaced_dct : true if interlaced dct used (mpeg2) | |
| 1437 */ | |
| 1438 void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64]) | |
| 1439 { | |
| 244 | 1440 int mb_x, mb_y; |
| 481 | 1441 const int mb_xy = s->mb_y * s->mb_width + s->mb_x; |
| 0 | 1442 |
| 1443 mb_x = s->mb_x; | |
| 1444 mb_y = s->mb_y; | |
| 1445 | |
|
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1446 #ifdef FF_POSTPROCESS |
|
466
805714c0c10f
new field for communicating with external postprocessing
nickols_k
parents:
462
diff
changeset
|
1447 /* Obsolete. Exists for compatibility with mplayer only. */ |
|
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1448 quant_store[mb_y][mb_x]=s->qscale; |
|
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1449 //printf("[%02d][%02d] %d\n",mb_x,mb_y,s->qscale); |
|
466
805714c0c10f
new field for communicating with external postprocessing
nickols_k
parents:
462
diff
changeset
|
1450 #else |
| 575 | 1451 /* even more obsolete, exists for mplayer xp only */ |
| 467 | 1452 if(s->avctx->quant_store) s->avctx->quant_store[mb_y*s->avctx->qstride+mb_x] = s->qscale; |
|
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1453 #endif |
| 575 | 1454 s->qscale_table[mb_xy]= s->qscale; |
|
108
1e4a4af694d1
exporting qscale data for postprocessing (for MPlayer)
arpi_esp
parents:
79
diff
changeset
|
1455 |
| 0 | 1456 /* update DC predictors for P macroblocks */ |
| 1457 if (!s->mb_intra) { | |
|
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1458 if (s->h263_pred || s->h263_aic) { |
| 481 | 1459 if(s->mbintra_table[mb_xy]) |
| 456 | 1460 ff_clean_intra_table_entries(s); |
| 0 | 1461 } else { |
| 456 | 1462 s->last_dc[0] = |
| 1463 s->last_dc[1] = | |
| 0 | 1464 s->last_dc[2] = 128 << s->intra_dc_precision; |
| 1465 } | |
| 1466 } | |
|
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
1467 else if (s->h263_pred || s->h263_aic) |
| 481 | 1468 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
|
1469 |
| 280 | 1470 /* update motion predictor, not for B-frames as they need the motion_val from the last P/S-Frame */ |
| 481 | 1471 if (s->out_format == FMT_H263 && s->pict_type!=B_TYPE) { //FIXME move into h263.c if possible, format specific stuff shouldnt be here |
| 0 | 1472 |
| 481 | 1473 const int wrap = s->block_wrap[0]; |
| 1474 const int xy = s->block_index[0]; | |
|
619
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1475 if(s->mv_type == MV_TYPE_8X8){ |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1476 s->non_b_mv4_table[xy]=1; |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1477 } else { |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1478 int motion_x, motion_y; |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1479 if (s->mb_intra) { |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1480 motion_x = 0; |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1481 motion_y = 0; |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1482 } 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
|
1483 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
|
1484 motion_y = s->mv[0][0][1]; |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1485 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ { |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1486 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
|
1487 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
|
1488 motion_x = (motion_x>>1) | (motion_x&1); |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1489 motion_y = (motion_y>>1) | (motion_y&1); |
|
2be2cc8fd0a1
mpeg4 interlaced decoding support (not completly implemented/tested due to lack of samples)
michaelni
parents:
618
diff
changeset
|
1490 } |
| 0 | 1491 /* no update if 8X8 because it has been done during parsing */ |
| 244 | 1492 s->motion_val[xy][0] = motion_x; |
| 1493 s->motion_val[xy][1] = motion_y; | |
| 1494 s->motion_val[xy + 1][0] = motion_x; | |
| 1495 s->motion_val[xy + 1][1] = motion_y; | |
| 1496 s->motion_val[xy + wrap][0] = motion_x; | |
| 1497 s->motion_val[xy + wrap][1] = motion_y; | |
| 1498 s->motion_val[xy + 1 + wrap][0] = motion_x; | |
| 1499 s->motion_val[xy + 1 + wrap][1] = motion_y; | |
| 598 | 1500 s->non_b_mv4_table[xy]=0; |
| 0 | 1501 } |
| 1502 } | |
| 1503 | |
| 324 | 1504 if (!(s->encoding && (s->intra_only || s->pict_type==B_TYPE))) { |
| 0 | 1505 UINT8 *dest_y, *dest_cb, *dest_cr; |
| 481 | 1506 int dct_linesize, dct_offset; |
| 1507 op_pixels_func *op_pix; | |
| 1508 qpel_mc_func *op_qpix; | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1509 |
| 324 | 1510 /* avoid copy if macroblock skipped in last frame too |
| 1511 dont touch it for B-frames as they need the skip info from the next p-frame */ | |
| 1512 if (s->pict_type != B_TYPE) { | |
| 481 | 1513 UINT8 *mbskip_ptr = &s->mbskip_table[mb_xy]; |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1514 if (s->mb_skiped) { |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1515 s->mb_skiped = 0; |
| 556 | 1516 |
| 1517 (*mbskip_ptr) ++; /* indicate that this time we skiped it */ | |
| 1518 if(*mbskip_ptr >99) *mbskip_ptr= 99; | |
| 1519 | |
| 324 | 1520 /* if previous was skipped too, then nothing to do ! |
| 1521 skip only during decoding as we might trash the buffers during encoding a bit */ | |
| 556 | 1522 if (*mbskip_ptr >= s->ip_buffer_count && !s->encoding) |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1523 goto the_end; |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1524 } else { |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1525 *mbskip_ptr = 0; /* not skipped */ |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1526 } |
|
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1527 } |
| 0 | 1528 |
|
562
23e58889a108
exporting mbskip_table after it has been allocated
michaelni
parents:
557
diff
changeset
|
1529 dest_y = s->current_picture [0] + (mb_y * 16* s->linesize ) + mb_x * 16; |
|
23e58889a108
exporting mbskip_table after it has been allocated
michaelni
parents:
557
diff
changeset
|
1530 dest_cb = s->current_picture[1] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; |
|
23e58889a108
exporting mbskip_table after it has been allocated
michaelni
parents:
557
diff
changeset
|
1531 dest_cr = s->current_picture[2] + (mb_y * 8 * s->uvlinesize) + mb_x * 8; |
| 0 | 1532 |
| 1533 if (s->interlaced_dct) { | |
| 1534 dct_linesize = s->linesize * 2; | |
| 1535 dct_offset = s->linesize; | |
| 1536 } else { | |
| 1537 dct_linesize = s->linesize; | |
| 1538 dct_offset = s->linesize * 8; | |
| 1539 } | |
| 1540 | |
| 1541 if (!s->mb_intra) { | |
| 1542 /* motion handling */ | |
| 456 | 1543 /* decoding or more than one mb_type (MC was allready done otherwise) */ |
| 481 | 1544 if((!s->encoding) || (s->mb_type[mb_xy]&(s->mb_type[mb_xy]-1))){ |
| 327 | 1545 if ((!s->no_rounding) || s->pict_type==B_TYPE){ |
| 324 | 1546 op_pix = put_pixels_tab; |
| 1547 op_qpix= qpel_mc_rnd_tab; | |
| 1548 }else{ | |
| 1549 op_pix = put_no_rnd_pixels_tab; | |
| 1550 op_qpix= qpel_mc_no_rnd_tab; | |
| 1551 } | |
| 0 | 1552 |
| 324 | 1553 if (s->mv_dir & MV_DIR_FORWARD) { |
| 1554 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); | |
| 327 | 1555 if ((!s->no_rounding) || s->pict_type==B_TYPE) |
| 324 | 1556 op_pix = avg_pixels_tab; |
| 1557 else | |
| 1558 op_pix = avg_no_rnd_pixels_tab; | |
| 1559 } | |
| 1560 if (s->mv_dir & MV_DIR_BACKWARD) { | |
| 1561 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); | |
| 1562 } | |
| 0 | 1563 } |
| 1564 | |
| 481 | 1565 /* skip dequant / idct if we are really late ;) */ |
| 1566 if(s->hurry_up>1) goto the_end; | |
| 1567 | |
| 0 | 1568 /* add dct residue */ |
| 591 | 1569 if(s->encoding || !(s->mpeg2 || s->h263_msmpeg4 || (s->codec_id==CODEC_ID_MPEG4 && !s->mpeg_quant))){ |
| 481 | 1570 add_dequant_dct(s, block[0], 0, dest_y, dct_linesize); |
| 1571 add_dequant_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
| 1572 add_dequant_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
| 1573 add_dequant_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
| 0 | 1574 |
| 485 | 1575 if(!(s->flags&CODEC_FLAG_GRAY)){ |
| 556 | 1576 add_dequant_dct(s, block[4], 4, dest_cb, s->uvlinesize); |
| 1577 add_dequant_dct(s, block[5], 5, dest_cr, s->uvlinesize); | |
| 485 | 1578 } |
| 481 | 1579 } else { |
| 1580 add_dct(s, block[0], 0, dest_y, dct_linesize); | |
| 1581 add_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
| 1582 add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
| 1583 add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
| 1584 | |
| 485 | 1585 if(!(s->flags&CODEC_FLAG_GRAY)){ |
| 556 | 1586 add_dct(s, block[4], 4, dest_cb, s->uvlinesize); |
| 1587 add_dct(s, block[5], 5, dest_cr, s->uvlinesize); | |
| 485 | 1588 } |
| 481 | 1589 } |
| 0 | 1590 } else { |
| 1591 /* dct only in intra block */ | |
| 1592 put_dct(s, block[0], 0, dest_y, dct_linesize); | |
| 1593 put_dct(s, block[1], 1, dest_y + 8, dct_linesize); | |
| 1594 put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize); | |
| 1595 put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize); | |
| 1596 | |
| 485 | 1597 if(!(s->flags&CODEC_FLAG_GRAY)){ |
| 556 | 1598 put_dct(s, block[4], 4, dest_cb, s->uvlinesize); |
| 1599 put_dct(s, block[5], 5, dest_cr, s->uvlinesize); | |
| 485 | 1600 } |
| 0 | 1601 } |
| 1602 } | |
|
7
1d3ac9654178
added skip macroblock optimization (big perf win on black regions for example)
glantau
parents:
0
diff
changeset
|
1603 the_end: |
| 294 | 1604 emms_c(); //FIXME remove |
| 1605 } | |
| 1606 | |
|
605
40874804a5af
same dc skip behavior for chroma & luma elimination, its confusing otherwise imho
michaelni
parents:
604
diff
changeset
|
1607 static inline void dct_single_coeff_elimination(MpegEncContext *s, int n, int threshold) |
| 456 | 1608 { |
| 1609 static const char tab[64]= | |
| 1610 {3,2,2,1,1,1,1,1, | |
| 1611 1,1,1,1,1,1,1,1, | |
| 1612 1,1,1,1,1,1,1,1, | |
| 1613 0,0,0,0,0,0,0,0, | |
| 1614 0,0,0,0,0,0,0,0, | |
| 1615 0,0,0,0,0,0,0,0, | |
| 1616 0,0,0,0,0,0,0,0, | |
| 1617 0,0,0,0,0,0,0,0}; | |
| 1618 int score=0; | |
| 1619 int run=0; | |
| 1620 int i; | |
| 1621 DCTELEM *block= s->block[n]; | |
| 1622 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
|
1623 int skip_dc; |
| 456 | 1624 |
|
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
|
1625 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
|
1626 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
|
1627 threshold= -threshold; |
|
605
40874804a5af
same dc skip behavior for chroma & luma elimination, its confusing otherwise imho
michaelni
parents:
604
diff
changeset
|
1628 }else |
|
40874804a5af
same dc skip behavior for chroma & luma elimination, its confusing otherwise imho
michaelni
parents:
604
diff
changeset
|
1629 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
|
1630 |
| 456 | 1631 /* are all which we could set to zero are allready zero? */ |
| 1632 if(last_index<=skip_dc - 1) return; | |
| 1633 | |
| 1634 for(i=0; i<=last_index; i++){ | |
| 1635 const int j = zigzag_direct[i]; | |
| 1636 const int level = ABS(block[j]); | |
| 1637 if(level==1){ | |
| 1638 if(skip_dc && i==0) continue; | |
| 1639 score+= tab[run]; | |
| 1640 run=0; | |
| 1641 }else if(level>1){ | |
| 1642 return; | |
| 1643 }else{ | |
| 1644 run++; | |
| 1645 } | |
| 1646 } | |
| 1647 if(score >= threshold) return; | |
| 1648 for(i=skip_dc; i<=last_index; i++){ | |
| 1649 const int j = zigzag_direct[i]; | |
| 1650 block[j]=0; | |
| 1651 } | |
| 1652 if(block[0]) s->block_last_index[n]= 0; | |
| 1653 else s->block_last_index[n]= -1; | |
| 1654 } | |
| 1655 | |
| 344 | 1656 static inline void clip_coeffs(MpegEncContext *s, DCTELEM *block, int last_index) |
| 1657 { | |
| 1658 int i; | |
| 1659 const int maxlevel= s->max_qcoeff; | |
| 1660 const int minlevel= s->min_qcoeff; | |
|
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1661 |
|
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
1662 for(i=0;i<=last_index; i++){ |
| 344 | 1663 const int j = zigzag_direct[i]; |
| 1664 int level = block[j]; | |
| 1665 | |
| 1666 if (level>maxlevel) level=maxlevel; | |
| 1667 else if(level<minlevel) level=minlevel; | |
| 1668 block[j]= level; | |
| 1669 } | |
| 1670 } | |
| 324 | 1671 |
| 1672 static void encode_mb(MpegEncContext *s, int motion_x, int motion_y) | |
| 294 | 1673 { |
| 1674 const int mb_x= s->mb_x; | |
| 1675 const int mb_y= s->mb_y; | |
| 1676 int i; | |
| 456 | 1677 int skip_dct[6]; |
| 324 | 1678 #if 0 |
| 1679 if (s->interlaced_dct) { | |
| 1680 dct_linesize = s->linesize * 2; | |
| 1681 dct_offset = s->linesize; | |
| 1682 } else { | |
| 1683 dct_linesize = s->linesize; | |
| 1684 dct_offset = s->linesize * 8; | |
| 1685 } | |
| 1686 #endif | |
| 456 | 1687 for(i=0; i<6; i++) skip_dct[i]=0; |
| 294 | 1688 |
| 324 | 1689 if (s->mb_intra) { |
| 1690 UINT8 *ptr; | |
| 1691 int wrap; | |
|
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1692 int emu=0; |
| 294 | 1693 |
| 324 | 1694 wrap = s->linesize; |
| 1695 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16; | |
|
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1696 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){ |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1697 emulated_edge_mc(s, ptr, wrap, 16, 16, mb_x*16, mb_y*16, s->width, s->height); |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1698 ptr= s->edge_emu_buffer; |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1699 emu=1; |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1700 } |
| 324 | 1701 get_pixels(s->block[0], ptr , wrap); |
| 1702 get_pixels(s->block[1], ptr + 8, wrap); | |
| 1703 get_pixels(s->block[2], ptr + 8 * wrap , wrap); | |
| 1704 get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap); | |
| 294 | 1705 |
| 487 | 1706 if(s->flags&CODEC_FLAG_GRAY){ |
| 1707 skip_dct[4]= 1; | |
| 1708 skip_dct[5]= 1; | |
| 1709 }else{ | |
| 1710 wrap >>=1; | |
| 1711 ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8; | |
|
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1712 if(emu){ |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1713 emulated_edge_mc(s, ptr, wrap, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1); |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1714 ptr= s->edge_emu_buffer; |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1715 } |
| 487 | 1716 get_pixels(s->block[4], ptr, wrap); |
| 294 | 1717 |
| 487 | 1718 ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8; |
|
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1719 if(emu){ |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1720 emulated_edge_mc(s, ptr, wrap, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1); |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1721 ptr= s->edge_emu_buffer; |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1722 } |
| 487 | 1723 get_pixels(s->block[5], ptr, wrap); |
| 1724 } | |
| 324 | 1725 }else{ |
| 1726 op_pixels_func *op_pix; | |
| 1727 qpel_mc_func *op_qpix; | |
| 1728 UINT8 *dest_y, *dest_cb, *dest_cr; | |
| 456 | 1729 UINT8 *ptr_y, *ptr_cb, *ptr_cr; |
| 1730 int wrap_y, wrap_c; | |
|
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1731 int emu=0; |
| 294 | 1732 |
| 324 | 1733 dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize ) + mb_x * 16; |
| 556 | 1734 dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; |
| 1735 dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; | |
| 456 | 1736 wrap_y = s->linesize; |
| 1737 wrap_c = wrap_y>>1; | |
| 1738 ptr_y = s->new_picture[0] + (mb_y * 16 * wrap_y) + mb_x * 16; | |
| 1739 ptr_cb = s->new_picture[1] + (mb_y * 8 * wrap_c) + mb_x * 8; | |
| 1740 ptr_cr = s->new_picture[2] + (mb_y * 8 * wrap_c) + mb_x * 8; | |
| 324 | 1741 |
| 327 | 1742 if ((!s->no_rounding) || s->pict_type==B_TYPE){ |
| 324 | 1743 op_pix = put_pixels_tab; |
| 1744 op_qpix= qpel_mc_rnd_tab; | |
| 295 | 1745 }else{ |
| 324 | 1746 op_pix = put_no_rnd_pixels_tab; |
| 1747 op_qpix= qpel_mc_no_rnd_tab; | |
| 1748 } | |
| 295 | 1749 |
| 324 | 1750 if (s->mv_dir & MV_DIR_FORWARD) { |
| 1751 MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix, op_qpix); | |
| 327 | 1752 if ((!s->no_rounding) || s->pict_type==B_TYPE) |
| 324 | 1753 op_pix = avg_pixels_tab; |
| 1754 else | |
| 1755 op_pix = avg_no_rnd_pixels_tab; | |
| 1756 } | |
| 1757 if (s->mv_dir & MV_DIR_BACKWARD) { | |
| 1758 MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix, op_qpix); | |
| 1759 } | |
| 295 | 1760 |
|
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1761 if(mb_x*16+16 > s->width || mb_y*16+16 > s->height){ |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1762 emulated_edge_mc(s, ptr_y, wrap_y, 16, 16, mb_x*16, mb_y*16, s->width, s->height); |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1763 ptr_y= s->edge_emu_buffer; |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1764 emu=1; |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1765 } |
| 456 | 1766 diff_pixels(s->block[0], ptr_y , dest_y , wrap_y); |
| 1767 diff_pixels(s->block[1], ptr_y + 8, dest_y + 8, wrap_y); | |
| 1768 diff_pixels(s->block[2], ptr_y + 8 * wrap_y , dest_y + 8 * wrap_y , wrap_y); | |
| 1769 diff_pixels(s->block[3], ptr_y + 8 * wrap_y + 8, dest_y + 8 * wrap_y + 8, wrap_y); | |
| 487 | 1770 |
| 1771 if(s->flags&CODEC_FLAG_GRAY){ | |
| 1772 skip_dct[4]= 1; | |
| 1773 skip_dct[5]= 1; | |
| 1774 }else{ | |
|
570
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1775 if(emu){ |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1776 emulated_edge_mc(s, ptr_cb, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1); |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1777 ptr_cb= s->edge_emu_buffer; |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1778 } |
| 487 | 1779 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
|
1780 if(emu){ |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1781 emulated_edge_mc(s, ptr_cr, wrap_c, 8, 8, mb_x*8, mb_y*8, s->width>>1, s->height>>1); |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1782 ptr_cr= s->edge_emu_buffer; |
|
274d9c5a75ee
use edge emu for encoding of width or height %16!=0 files
michaelni
parents:
569
diff
changeset
|
1783 } |
| 487 | 1784 diff_pixels(s->block[5], ptr_cr, dest_cr, wrap_c); |
| 1785 } | |
| 1786 | |
| 456 | 1787 /* pre quantization */ |
| 1788 if(s->mc_mb_var[s->mb_width*mb_y+ mb_x]<2*s->qscale*s->qscale){ | |
| 1789 if(pix_abs8x8(ptr_y , dest_y , wrap_y) < 20*s->qscale) skip_dct[0]= 1; | |
| 1790 if(pix_abs8x8(ptr_y + 8, dest_y + 8, wrap_y) < 20*s->qscale) skip_dct[1]= 1; | |
| 1791 if(pix_abs8x8(ptr_y + 8*wrap_y , dest_y + 8*wrap_y , wrap_y) < 20*s->qscale) skip_dct[2]= 1; | |
| 1792 if(pix_abs8x8(ptr_y + 8*wrap_y + 8, dest_y + 8*wrap_y + 8, wrap_y) < 20*s->qscale) skip_dct[3]= 1; | |
| 1793 if(pix_abs8x8(ptr_cb , dest_cb , wrap_y) < 20*s->qscale) skip_dct[4]= 1; | |
| 1794 if(pix_abs8x8(ptr_cr , dest_cr , wrap_y) < 20*s->qscale) skip_dct[5]= 1; | |
| 1795 #if 0 | |
| 1796 { | |
| 1797 static int stat[7]; | |
| 1798 int num=0; | |
| 1799 for(i=0; i<6; i++) | |
| 1800 if(skip_dct[i]) num++; | |
| 1801 stat[num]++; | |
| 1802 | |
| 1803 if(s->mb_x==0 && s->mb_y==0){ | |
| 1804 for(i=0; i<7; i++){ | |
| 1805 printf("%6d %1d\n", stat[i], i); | |
| 1806 } | |
| 1807 } | |
| 1808 } | |
| 1809 #endif | |
| 1810 } | |
| 324 | 1811 |
| 294 | 1812 } |
| 1813 | |
| 1814 #if 0 | |
| 1815 { | |
| 1816 float adap_parm; | |
| 1817 | |
| 1818 adap_parm = ((s->avg_mb_var << 1) + s->mb_var[s->mb_width*mb_y+mb_x] + 1.0) / | |
| 1819 ((s->mb_var[s->mb_width*mb_y+mb_x] << 1) + s->avg_mb_var + 1.0); | |
| 1820 | |
| 1821 printf("\ntype=%c qscale=%2d adap=%0.2f dquant=%4.2f var=%4d avgvar=%4d", | |
| 1822 (s->mb_type[s->mb_width*mb_y+mb_x] > 0) ? 'I' : 'P', | |
| 1823 s->qscale, adap_parm, s->qscale*adap_parm, | |
| 1824 s->mb_var[s->mb_width*mb_y+mb_x], s->avg_mb_var); | |
| 1825 } | |
| 1826 #endif | |
| 1827 /* DCT & quantize */ | |
| 344 | 1828 if(s->out_format==FMT_MJPEG){ |
| 1829 for(i=0;i<6;i++) { | |
| 1830 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
|
1831 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
|
1832 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); |
| 344 | 1833 } |
| 1834 }else{ | |
| 1835 for(i=0;i<6;i++) { | |
| 456 | 1836 if(!skip_dct[i]){ |
| 1837 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
|
1838 s->block_last_index[i] = s->dct_quantize(s, s->block[i], i, s->qscale, &overflow); |
| 344 | 1839 // 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
|
1840 // 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
|
1841 // of improve it. Just INTRADC clipping deserves changes in quantizer |
| 456 | 1842 if (overflow) clip_coeffs(s, s->block[i], s->block_last_index[i]); |
| 1843 }else | |
| 1844 s->block_last_index[i]= -1; | |
| 344 | 1845 } |
| 456 | 1846 if(s->luma_elim_threshold && !s->mb_intra) |
| 1847 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
|
1848 dct_single_coeff_elimination(s, i, s->luma_elim_threshold); |
| 456 | 1849 if(s->chroma_elim_threshold && !s->mb_intra) |
| 1850 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
|
1851 dct_single_coeff_elimination(s, i, s->chroma_elim_threshold); |
| 294 | 1852 } |
| 1853 | |
| 487 | 1854 if((s->flags&CODEC_FLAG_GRAY) && s->mb_intra){ |
| 1855 s->block_last_index[4]= | |
| 1856 s->block_last_index[5]= 0; | |
| 1857 s->block[4][0]= | |
| 1858 s->block[5][0]= 128; | |
| 1859 } | |
| 1860 | |
| 294 | 1861 /* huffman encode */ |
| 1862 switch(s->out_format) { | |
| 1863 case FMT_MPEG1: | |
| 1864 mpeg1_encode_mb(s, s->block, motion_x, motion_y); | |
| 1865 break; | |
| 1866 case FMT_H263: | |
| 1867 if (s->h263_msmpeg4) | |
| 1868 msmpeg4_encode_mb(s, s->block, motion_x, motion_y); | |
| 1869 else if(s->h263_pred) | |
| 1870 mpeg4_encode_mb(s, s->block, motion_x, motion_y); | |
| 1871 else | |
| 1872 h263_encode_mb(s, s->block, motion_x, motion_y); | |
| 1873 break; | |
| 1874 case FMT_MJPEG: | |
| 1875 mjpeg_encode_mb(s, s->block); | |
| 1876 break; | |
| 1877 } | |
| 1878 } | |
| 1879 | |
| 456 | 1880 void ff_copy_bits(PutBitContext *pb, UINT8 *src, int length) |
| 294 | 1881 { |
| 326 | 1882 int bytes= length>>4; |
| 1883 int bits= length&15; | |
| 1884 int i; | |
| 1885 | |
| 456 | 1886 if(length==0) return; |
| 1887 | |
| 326 | 1888 for(i=0; i<bytes; i++) put_bits(pb, 16, be2me_16(((uint16_t*)src)[i])); |
| 1889 put_bits(pb, bits, be2me_16(((uint16_t*)src)[i])>>(16-bits)); | |
| 0 | 1890 } |
| 1891 | |
| 456 | 1892 static inline void copy_context_before_encode(MpegEncContext *d, MpegEncContext *s, int type){ |
| 326 | 1893 int i; |
| 1894 | |
| 1895 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | |
| 1896 | |
| 1897 /* mpeg1 */ | |
| 1898 d->mb_incr= s->mb_incr; | |
| 1899 for(i=0; i<3; i++) | |
| 1900 d->last_dc[i]= s->last_dc[i]; | |
| 1901 | |
| 1902 /* statistics */ | |
| 1903 d->mv_bits= s->mv_bits; | |
| 1904 d->i_tex_bits= s->i_tex_bits; | |
| 1905 d->p_tex_bits= s->p_tex_bits; | |
| 1906 d->i_count= s->i_count; | |
| 1907 d->p_count= s->p_count; | |
| 1908 d->skip_count= s->skip_count; | |
| 1909 d->misc_bits= s->misc_bits; | |
| 329 | 1910 d->last_bits= 0; |
| 327 | 1911 |
| 1912 d->mb_skiped= s->mb_skiped; | |
| 326 | 1913 } |
| 1914 | |
| 456 | 1915 static inline void copy_context_after_encode(MpegEncContext *d, MpegEncContext *s, int type){ |
| 326 | 1916 int i; |
| 1917 | |
| 1918 memcpy(d->mv, s->mv, 2*4*2*sizeof(int)); | |
| 1919 memcpy(d->last_mv, s->last_mv, 2*2*2*sizeof(int)); //FIXME is memcpy faster then a loop? | |
| 1920 | |
| 1921 /* mpeg1 */ | |
| 1922 d->mb_incr= s->mb_incr; | |
| 1923 for(i=0; i<3; i++) | |
| 1924 d->last_dc[i]= s->last_dc[i]; | |
| 1925 | |
| 1926 /* statistics */ | |
| 1927 d->mv_bits= s->mv_bits; | |
| 1928 d->i_tex_bits= s->i_tex_bits; | |
| 1929 d->p_tex_bits= s->p_tex_bits; | |
| 1930 d->i_count= s->i_count; | |
| 1931 d->p_count= s->p_count; | |
| 1932 d->skip_count= s->skip_count; | |
| 1933 d->misc_bits= s->misc_bits; | |
| 1934 | |
| 1935 d->mb_intra= s->mb_intra; | |
| 327 | 1936 d->mb_skiped= s->mb_skiped; |
| 326 | 1937 d->mv_type= s->mv_type; |
| 1938 d->mv_dir= s->mv_dir; | |
| 1939 d->pb= s->pb; | |
| 456 | 1940 if(s->data_partitioning){ |
| 1941 d->pb2= s->pb2; | |
| 1942 d->tex_pb= s->tex_pb; | |
| 1943 } | |
| 326 | 1944 d->block= s->block; |
| 1945 for(i=0; i<6; i++) | |
| 1946 d->block_last_index[i]= s->block_last_index[i]; | |
| 1947 } | |
| 1948 | |
| 456 | 1949 static inline void encode_mb_hq(MpegEncContext *s, MpegEncContext *backup, MpegEncContext *best, int type, |
| 1950 PutBitContext pb[2], PutBitContext pb2[2], PutBitContext tex_pb[2], | |
| 1951 int *dmin, int *next_block, int motion_x, int motion_y) | |
| 1952 { | |
| 1953 int bits_count; | |
| 1954 | |
| 1955 copy_context_before_encode(s, backup, type); | |
| 1956 | |
| 1957 s->block= s->blocks[*next_block]; | |
| 1958 s->pb= pb[*next_block]; | |
| 1959 if(s->data_partitioning){ | |
| 1960 s->pb2 = pb2 [*next_block]; | |
| 1961 s->tex_pb= tex_pb[*next_block]; | |
| 1962 } | |
| 1963 | |
| 1964 encode_mb(s, motion_x, motion_y); | |
| 1965 | |
| 1966 bits_count= get_bit_count(&s->pb); | |
| 1967 if(s->data_partitioning){ | |
| 1968 bits_count+= get_bit_count(&s->pb2); | |
| 1969 bits_count+= get_bit_count(&s->tex_pb); | |
| 1970 } | |
| 1971 | |
| 1972 if(bits_count<*dmin){ | |
| 1973 *dmin= bits_count; | |
| 1974 *next_block^=1; | |
| 1975 | |
| 1976 copy_context_after_encode(best, s, type); | |
| 1977 } | |
| 1978 } | |
| 326 | 1979 |
| 0 | 1980 static void encode_picture(MpegEncContext *s, int picture_number) |
| 1981 { | |
| 294 | 1982 int mb_x, mb_y, last_gob, pdif = 0; |
| 1983 int i; | |
| 286 | 1984 int bits; |
| 326 | 1985 MpegEncContext best_s, backup_s; |
| 456 | 1986 UINT8 bit_buf[2][3000]; |
| 1987 UINT8 bit_buf2[2][3000]; | |
| 1988 UINT8 bit_buf_tex[2][3000]; | |
| 1989 PutBitContext pb[2], pb2[2], tex_pb[2]; | |
| 1990 | |
| 1991 for(i=0; i<2; i++){ | |
| 1992 init_put_bits(&pb [i], bit_buf [i], 3000, NULL, NULL); | |
| 1993 init_put_bits(&pb2 [i], bit_buf2 [i], 3000, NULL, NULL); | |
| 1994 init_put_bits(&tex_pb[i], bit_buf_tex[i], 3000, NULL, NULL); | |
| 1995 } | |
| 0 | 1996 |
| 1997 s->picture_number = picture_number; | |
| 268 | 1998 |
| 294 | 1999 s->block_wrap[0]= |
| 2000 s->block_wrap[1]= | |
| 2001 s->block_wrap[2]= | |
| 2002 s->block_wrap[3]= s->mb_width*2 + 2; | |
| 2003 s->block_wrap[4]= | |
| 2004 s->block_wrap[5]= s->mb_width + 2; | |
| 2005 | |
| 268 | 2006 /* Reset the average MB variance */ |
| 456 | 2007 s->mb_var_sum = 0; |
| 2008 s->mc_mb_var_sum = 0; | |
| 327 | 2009 |
| 2010 /* we need to initialize some time vars before we can encode b-frames */ | |
| 2011 if (s->h263_pred && !s->h263_msmpeg4) | |
| 2012 ff_set_mpeg4_time(s, s->picture_number); | |
| 2013 | |
| 608 | 2014 s->scene_change_score=0; |
| 2015 | |
| 268 | 2016 /* Estimate motion for every MB */ |
| 324 | 2017 if(s->pict_type != I_TYPE){ |
| 294 | 2018 for(mb_y=0; mb_y < s->mb_height; mb_y++) { |
| 2019 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; | |
| 2020 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
| 2021 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
| 2022 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
| 2023 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
| 2024 s->mb_x = mb_x; | |
| 2025 s->mb_y = mb_y; | |
| 2026 s->block_index[0]+=2; | |
| 2027 s->block_index[1]+=2; | |
| 2028 s->block_index[2]+=2; | |
| 2029 s->block_index[3]+=2; | |
| 268 | 2030 |
| 294 | 2031 /* compute motion vector & mb_type and store in context */ |
| 324 | 2032 if(s->pict_type==B_TYPE) |
| 2033 ff_estimate_b_frame_motion(s, mb_x, mb_y); | |
| 2034 else | |
| 2035 ff_estimate_p_frame_motion(s, mb_x, mb_y); | |
| 294 | 2036 // s->mb_type[mb_y*s->mb_width + mb_x]=MB_TYPE_INTER; |
| 268 | 2037 } |
| 2038 } | |
| 294 | 2039 emms_c(); |
| 456 | 2040 }else /* if(s->pict_type == I_TYPE) */{ |
| 294 | 2041 /* I-Frame */ |
| 2042 //FIXME do we need to zero them? | |
| 2043 memset(s->motion_val[0], 0, sizeof(INT16)*(s->mb_width*2 + 2)*(s->mb_height*2 + 2)*2); | |
| 324 | 2044 memset(s->p_mv_table , 0, sizeof(INT16)*(s->mb_width+2)*(s->mb_height+2)*2); |
| 294 | 2045 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); |
| 612 | 2046 |
| 2047 if(!s->fixed_qscale){ | |
| 2048 /* finding spatial complexity for I-frame rate control */ | |
| 2049 for(mb_y=0; mb_y < s->mb_height; mb_y++) { | |
| 2050 for(mb_x=0; mb_x < s->mb_width; mb_x++) { | |
| 2051 int xx = mb_x * 16; | |
| 2052 int yy = mb_y * 16; | |
| 2053 uint8_t *pix = s->new_picture[0] + (yy * s->linesize) + xx; | |
| 2054 int varc; | |
| 2055 int sum = pix_sum(pix, s->linesize); | |
| 2056 | |
| 2057 sum= (sum+8)>>4; | |
| 2058 varc = (pix_norm1(pix, s->linesize) - sum*sum + 500 + 128)>>8; | |
| 2059 | |
| 2060 s->mb_var[s->mb_width * mb_y + mb_x] = varc; | |
| 2061 s->mb_var_sum += varc; | |
| 2062 } | |
| 2063 } | |
| 2064 } | |
| 268 | 2065 } |
| 608 | 2066 if(s->scene_change_score > 0 && s->pict_type == P_TYPE){ |
| 271 | 2067 s->pict_type= I_TYPE; |
| 294 | 2068 memset(s->mb_type , MB_TYPE_INTRA, sizeof(UINT8)*s->mb_width*s->mb_height); |
| 329 | 2069 if(s->max_b_frames==0){ |
| 2070 s->input_pict_type= I_TYPE; | |
| 2071 s->input_picture_in_gop_number=0; | |
| 2072 } | |
| 612 | 2073 //printf("Scene change detected, encoding as I Frame %d %d\n", s->mb_var_sum, s->mc_mb_var_sum); |
| 271 | 2074 } |
| 324 | 2075 |
| 2076 if(s->pict_type==P_TYPE || s->pict_type==S_TYPE) | |
| 2077 s->f_code= ff_get_best_fcode(s, s->p_mv_table, MB_TYPE_INTER); | |
| 2078 ff_fix_long_p_mvs(s); | |
| 2079 if(s->pict_type==B_TYPE){ | |
| 2080 s->f_code= ff_get_best_fcode(s, s->b_forw_mv_table, MB_TYPE_FORWARD); | |
| 2081 s->b_code= ff_get_best_fcode(s, s->b_back_mv_table, MB_TYPE_BACKWARD); | |
| 327 | 2082 |
| 2083 ff_fix_long_b_mvs(s, s->b_forw_mv_table, s->f_code, MB_TYPE_FORWARD); | |
| 2084 ff_fix_long_b_mvs(s, s->b_back_mv_table, s->b_code, MB_TYPE_BACKWARD); | |
| 2085 ff_fix_long_b_mvs(s, s->b_bidir_forw_mv_table, s->f_code, MB_TYPE_BIDIR); | |
| 2086 ff_fix_long_b_mvs(s, s->b_bidir_back_mv_table, s->b_code, MB_TYPE_BIDIR); | |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
2087 } |
| 324 | 2088 |
|
277
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
2089 //printf("f_code %d ///\n", s->f_code); |
|
5cb2978e701f
new motion estimation (epzs) not complete yet but allready pretty good :)
michaelni
parents:
271
diff
changeset
|
2090 |
| 271 | 2091 // printf("%d %d\n", s->avg_mb_var, s->mc_mb_var); |
| 612 | 2092 if (!s->fixed_qscale) |
| 329 | 2093 s->qscale = ff_rate_estimate_qscale(s); |
| 2094 | |
| 0 | 2095 if (s->out_format == FMT_MJPEG) { |
| 2096 /* 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
|
2097 s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0]; |
| 0 | 2098 for(i=1;i<64;i++) |
|
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
|
2099 s->intra_matrix[i] = CLAMP_TO_8BIT((ff_mpeg1_default_intra_matrix[i] * s->qscale) >> 3); |
|
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
|
2100 convert_matrix(s, s->q_intra_matrix, s->q_intra_matrix16, |
| 344 | 2101 s->q_intra_matrix16_bias, s->intra_matrix, s->intra_quant_bias); |
| 0 | 2102 } |
| 2103 | |
| 286 | 2104 s->last_bits= get_bit_count(&s->pb); |
| 0 | 2105 switch(s->out_format) { |
| 2106 case FMT_MJPEG: | |
| 2107 mjpeg_picture_header(s); | |
| 2108 break; | |
| 2109 case FMT_H263: | |
| 2110 if (s->h263_msmpeg4) | |
| 2111 msmpeg4_encode_picture_header(s, picture_number); | |
| 2112 else if (s->h263_pred) | |
| 2113 mpeg4_encode_picture_header(s, picture_number); | |
| 2114 else if (s->h263_rv10) | |
| 2115 rv10_encode_picture_header(s, picture_number); | |
| 2116 else | |
| 2117 h263_encode_picture_header(s, picture_number); | |
| 2118 break; | |
| 2119 case FMT_MPEG1: | |
| 2120 mpeg1_encode_picture_header(s, picture_number); | |
| 2121 break; | |
| 2122 } | |
| 286 | 2123 bits= get_bit_count(&s->pb); |
| 2124 s->header_bits= bits - s->last_bits; | |
| 2125 s->last_bits= bits; | |
| 2126 s->mv_bits=0; | |
| 2127 s->misc_bits=0; | |
| 2128 s->i_tex_bits=0; | |
| 2129 s->p_tex_bits=0; | |
| 2130 s->i_count=0; | |
| 2131 s->p_count=0; | |
| 2132 s->skip_count=0; | |
| 2133 | |
| 0 | 2134 /* init last dc values */ |
| 2135 /* note: quant matrix value (8) is implied here */ | |
| 2136 s->last_dc[0] = 128; | |
| 2137 s->last_dc[1] = 128; | |
| 2138 s->last_dc[2] = 128; | |
| 2139 s->mb_incr = 1; | |
| 2140 s->last_mv[0][0][0] = 0; | |
| 2141 s->last_mv[0][0][1] = 0; | |
| 2142 | |
| 162 | 2143 /* Get the GOB height based on picture height */ |
| 231 | 2144 if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4) { |
| 162 | 2145 if (s->height <= 400) |
| 2146 s->gob_index = 1; | |
| 2147 else if (s->height <= 800) | |
| 2148 s->gob_index = 2; | |
| 2149 else | |
| 2150 s->gob_index = 4; | |
| 456 | 2151 }else if(s->codec_id==CODEC_ID_MPEG4){ |
| 2152 s->gob_index = 1; | |
| 162 | 2153 } |
| 456 | 2154 |
| 2155 if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE) | |
| 2156 ff_mpeg4_init_partitions(s); | |
| 2157 | |
| 2158 s->resync_mb_x=0; | |
| 2159 s->resync_mb_y=0; | |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2160 for(mb_y=0; mb_y < s->mb_height; mb_y++) { |
| 456 | 2161 /* Put GOB header based on RTP MTU for formats which support it per line (H263*)*/ |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2162 /* TODO: Put all this stuff in a separate generic function */ |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2163 if (s->rtp_mode) { |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2164 if (!mb_y) { |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2165 s->ptr_lastgob = s->pb.buf; |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2166 s->ptr_last_mb_line = s->pb.buf; |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2167 } else if (s->out_format == FMT_H263 && !s->h263_pred && !s->h263_msmpeg4 && !(mb_y % s->gob_index)) { |
| 456 | 2168 // MN: we could move the space check from h263 -> here, as its not h263 specific |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2169 last_gob = h263_encode_gob_header(s, mb_y); |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2170 if (last_gob) { |
| 456 | 2171 s->first_slice_line = 1; |
| 2172 }else{ | |
| 2173 /*MN: we reset it here instead at the end of each line cuz mpeg4 can have | |
| 2174 slice lines starting & ending in the middle*/ | |
| 2175 s->first_slice_line = 0; | |
|
233
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2176 } |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2177 } |
|
3f5b72726118
- More work on preliminary bit rate control, just to be able to get an
pulento
parents:
232
diff
changeset
|
2178 } |
| 499 | 2179 |
| 2180 s->y_dc_scale= s->y_dc_scale_table[ s->qscale ]; | |
| 2181 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
|
2182 |
| 266 | 2183 s->block_index[0]= s->block_wrap[0]*(mb_y*2 + 1) - 1; |
| 2184 s->block_index[1]= s->block_wrap[0]*(mb_y*2 + 1); | |
| 2185 s->block_index[2]= s->block_wrap[0]*(mb_y*2 + 2) - 1; | |
| 2186 s->block_index[3]= s->block_wrap[0]*(mb_y*2 + 2); | |
| 2187 s->block_index[4]= s->block_wrap[4]*(mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2); | |
| 2188 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
|
2189 for(mb_x=0; mb_x < s->mb_width; mb_x++) { |
| 327 | 2190 const int mb_type= s->mb_type[mb_y * s->mb_width + mb_x]; |
| 2191 const int xy= (mb_y+1) * (s->mb_width+2) + mb_x + 1; | |
| 456 | 2192 // int d; |
| 294 | 2193 int dmin=10000000; |
| 0 | 2194 |
|
232
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
2195 s->mb_x = mb_x; |
|
b640ec5948b0
- Now the ME is done for the entire picture when enconding, the
pulento
parents:
231
diff
changeset
|
2196 s->mb_y = mb_y; |
| 266 | 2197 s->block_index[0]+=2; |
| 2198 s->block_index[1]+=2; | |
| 2199 s->block_index[2]+=2; | |
| 2200 s->block_index[3]+=2; | |
| 2201 s->block_index[4]++; | |
| 2202 s->block_index[5]++; | |
| 456 | 2203 |
| 2204 /* write gob / video packet header for formats which support it at any MB (MPEG4) */ | |
| 2205 if(s->rtp_mode && s->mb_y>0 && s->codec_id==CODEC_ID_MPEG4){ | |
| 2206 int pdif= pbBufPtr(&s->pb) - s->ptr_lastgob; | |
| 2207 | |
| 2208 //the *2 is there so we stay below the requested size | |
| 2209 if(pdif + s->mb_line_avgsize/s->mb_width >= s->rtp_payload_size){ | |
| 2210 if(s->codec_id==CODEC_ID_MPEG4){ | |
| 2211 if(s->data_partitioning && s->pict_type!=B_TYPE){ | |
| 2212 ff_mpeg4_merge_partitions(s); | |
| 2213 ff_mpeg4_init_partitions(s); | |
| 2214 } | |
| 2215 ff_mpeg4_encode_video_packet_header(s); | |
| 2216 | |
| 2217 if(s->flags&CODEC_FLAG_PASS1){ | |
| 2218 int bits= get_bit_count(&s->pb); | |
| 2219 s->misc_bits+= bits - s->last_bits; | |
| 2220 s->last_bits= bits; | |
| 2221 } | |
| 2222 ff_mpeg4_clean_buffers(s); | |
| 2223 } | |
| 2224 s->ptr_lastgob = pbBufPtr(&s->pb); | |
| 2225 s->first_slice_line=1; | |
| 2226 s->resync_mb_x=mb_x; | |
| 2227 s->resync_mb_y=mb_y; | |
| 2228 } | |
| 2229 | |
| 2230 if( (s->resync_mb_x == s->mb_x) | |
| 2231 && s->resync_mb_y+1 == s->mb_y){ | |
| 2232 s->first_slice_line=0; | |
| 2233 } | |
| 2234 } | |
| 2235 | |
| 294 | 2236 if(mb_type & (mb_type-1)){ // more than 1 MB type possible |
| 327 | 2237 int next_block=0; |
| 456 | 2238 int pb_bits_count, pb2_bits_count, tex_pb_bits_count; |
| 326 | 2239 |
| 2240 copy_context_before_encode(&backup_s, s, -1); | |
| 456 | 2241 backup_s.pb= s->pb; |
| 2242 best_s.data_partitioning= s->data_partitioning; | |
| 2243 if(s->data_partitioning){ | |
| 2244 backup_s.pb2= s->pb2; | |
| 2245 backup_s.tex_pb= s->tex_pb; | |
| 2246 } | |
| 326 | 2247 |
| 294 | 2248 if(mb_type&MB_TYPE_INTER){ |
| 327 | 2249 s->mv_dir = MV_DIR_FORWARD; |
| 295 | 2250 s->mv_type = MV_TYPE_16X16; |
| 294 | 2251 s->mb_intra= 0; |
| 324 | 2252 s->mv[0][0][0] = s->p_mv_table[xy][0]; |
| 2253 s->mv[0][0][1] = s->p_mv_table[xy][1]; | |
| 456 | 2254 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER, pb, pb2, tex_pb, |
| 2255 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); | |
| 0 | 2256 } |
| 326 | 2257 if(mb_type&MB_TYPE_INTER4V){ |
| 327 | 2258 s->mv_dir = MV_DIR_FORWARD; |
| 295 | 2259 s->mv_type = MV_TYPE_8X8; |
| 2260 s->mb_intra= 0; | |
| 2261 for(i=0; i<4; i++){ | |
| 2262 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; | |
| 2263 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; | |
| 2264 } | |
| 456 | 2265 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTER4V, pb, pb2, tex_pb, |
| 2266 &dmin, &next_block, 0, 0); | |
| 327 | 2267 } |
| 2268 if(mb_type&MB_TYPE_FORWARD){ | |
| 2269 s->mv_dir = MV_DIR_FORWARD; | |
| 2270 s->mv_type = MV_TYPE_16X16; | |
| 2271 s->mb_intra= 0; | |
| 2272 s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; | |
| 2273 s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; | |
| 456 | 2274 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_FORWARD, pb, pb2, tex_pb, |
| 2275 &dmin, &next_block, s->mv[0][0][0], s->mv[0][0][1]); | |
| 327 | 2276 } |
| 2277 if(mb_type&MB_TYPE_BACKWARD){ | |
| 2278 s->mv_dir = MV_DIR_BACKWARD; | |
| 2279 s->mv_type = MV_TYPE_16X16; | |
| 2280 s->mb_intra= 0; | |
| 2281 s->mv[1][0][0] = s->b_back_mv_table[xy][0]; | |
| 2282 s->mv[1][0][1] = s->b_back_mv_table[xy][1]; | |
| 456 | 2283 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BACKWARD, pb, pb2, tex_pb, |
| 2284 &dmin, &next_block, s->mv[1][0][0], s->mv[1][0][1]); | |
| 327 | 2285 } |
| 2286 if(mb_type&MB_TYPE_BIDIR){ | |
| 2287 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; | |
| 2288 s->mv_type = MV_TYPE_16X16; | |
| 2289 s->mb_intra= 0; | |
| 2290 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; | |
| 2291 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; | |
| 2292 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; | |
| 2293 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; | |
| 456 | 2294 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_BIDIR, pb, pb2, tex_pb, |
| 2295 &dmin, &next_block, 0, 0); | |
| 327 | 2296 } |
| 2297 if(mb_type&MB_TYPE_DIRECT){ | |
| 2298 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
| 2299 s->mv_type = MV_TYPE_16X16; //FIXME | |
| 2300 s->mb_intra= 0; | |
| 2301 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0]; | |
| 2302 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1]; | |
| 2303 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0]; | |
| 2304 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1]; | |
| 456 | 2305 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_DIRECT, pb, pb2, tex_pb, |
| 2306 &dmin, &next_block, s->b_direct_mv_table[xy][0], s->b_direct_mv_table[xy][1]); | |
| 295 | 2307 } |
| 294 | 2308 if(mb_type&MB_TYPE_INTRA){ |
| 327 | 2309 s->mv_dir = MV_DIR_FORWARD; |
| 295 | 2310 s->mv_type = MV_TYPE_16X16; |
| 294 | 2311 s->mb_intra= 1; |
| 2312 s->mv[0][0][0] = 0; | |
| 2313 s->mv[0][0][1] = 0; | |
| 456 | 2314 encode_mb_hq(s, &backup_s, &best_s, MB_TYPE_INTRA, pb, pb2, tex_pb, |
| 2315 &dmin, &next_block, 0, 0); | |
| 326 | 2316 /* force cleaning of ac/dc pred stuff if needed ... */ |
| 2317 if(s->h263_pred || s->h263_aic) | |
| 2318 s->mbintra_table[mb_x + mb_y*s->mb_width]=1; | |
| 295 | 2319 } |
| 326 | 2320 copy_context_after_encode(s, &best_s, -1); |
| 456 | 2321 |
| 2322 pb_bits_count= get_bit_count(&s->pb); | |
| 2323 flush_put_bits(&s->pb); | |
| 2324 ff_copy_bits(&backup_s.pb, bit_buf[next_block^1], pb_bits_count); | |
| 2325 s->pb= backup_s.pb; | |
| 2326 | |
| 2327 if(s->data_partitioning){ | |
| 2328 pb2_bits_count= get_bit_count(&s->pb2); | |
| 2329 flush_put_bits(&s->pb2); | |
| 2330 ff_copy_bits(&backup_s.pb2, bit_buf2[next_block^1], pb2_bits_count); | |
| 2331 s->pb2= backup_s.pb2; | |
| 2332 | |
| 2333 tex_pb_bits_count= get_bit_count(&s->tex_pb); | |
| 2334 flush_put_bits(&s->tex_pb); | |
| 2335 ff_copy_bits(&backup_s.tex_pb, bit_buf_tex[next_block^1], tex_pb_bits_count); | |
| 2336 s->tex_pb= backup_s.tex_pb; | |
| 2337 } | |
| 329 | 2338 s->last_bits= get_bit_count(&s->pb); |
| 294 | 2339 } else { |
| 324 | 2340 int motion_x, motion_y; |
| 2341 s->mv_type=MV_TYPE_16X16; | |
| 294 | 2342 // only one MB-Type possible |
| 327 | 2343 switch(mb_type){ |
| 2344 case MB_TYPE_INTRA: | |
| 324 | 2345 s->mv_dir = MV_DIR_FORWARD; |
| 294 | 2346 s->mb_intra= 1; |
| 324 | 2347 motion_x= s->mv[0][0][0] = 0; |
| 2348 motion_y= s->mv[0][0][1] = 0; | |
| 327 | 2349 break; |
| 2350 case MB_TYPE_INTER: | |
| 324 | 2351 s->mv_dir = MV_DIR_FORWARD; |
| 2352 s->mb_intra= 0; | |
| 2353 motion_x= s->mv[0][0][0] = s->p_mv_table[xy][0]; | |
| 2354 motion_y= s->mv[0][0][1] = s->p_mv_table[xy][1]; | |
| 327 | 2355 break; |
| 456 | 2356 case MB_TYPE_INTER4V: |
| 2357 s->mv_dir = MV_DIR_FORWARD; | |
| 2358 s->mv_type = MV_TYPE_8X8; | |
| 2359 s->mb_intra= 0; | |
| 2360 for(i=0; i<4; i++){ | |
| 2361 s->mv[0][i][0] = s->motion_val[s->block_index[i]][0]; | |
| 2362 s->mv[0][i][1] = s->motion_val[s->block_index[i]][1]; | |
| 2363 } | |
| 2364 motion_x= motion_y= 0; | |
| 2365 break; | |
| 327 | 2366 case MB_TYPE_DIRECT: |
| 324 | 2367 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; |
| 2368 s->mb_intra= 0; | |
| 327 | 2369 motion_x=s->b_direct_mv_table[xy][0]; |
| 2370 motion_y=s->b_direct_mv_table[xy][1]; | |
| 2371 s->mv[0][0][0] = s->b_direct_forw_mv_table[xy][0]; | |
| 2372 s->mv[0][0][1] = s->b_direct_forw_mv_table[xy][1]; | |
| 2373 s->mv[1][0][0] = s->b_direct_back_mv_table[xy][0]; | |
| 2374 s->mv[1][0][1] = s->b_direct_back_mv_table[xy][1]; | |
| 2375 break; | |
| 2376 case MB_TYPE_BIDIR: | |
| 324 | 2377 s->mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD; |
| 294 | 2378 s->mb_intra= 0; |
| 324 | 2379 motion_x=0; |
| 2380 motion_y=0; | |
| 2381 s->mv[0][0][0] = s->b_bidir_forw_mv_table[xy][0]; | |
| 2382 s->mv[0][0][1] = s->b_bidir_forw_mv_table[xy][1]; | |
| 2383 s->mv[1][0][0] = s->b_bidir_back_mv_table[xy][0]; | |
| 2384 s->mv[1][0][1] = s->b_bidir_back_mv_table[xy][1]; | |
| 327 | 2385 break; |
| 2386 case MB_TYPE_BACKWARD: | |
| 324 | 2387 s->mv_dir = MV_DIR_BACKWARD; |
| 2388 s->mb_intra= 0; | |
| 2389 motion_x= s->mv[1][0][0] = s->b_back_mv_table[xy][0]; | |
| 2390 motion_y= s->mv[1][0][1] = s->b_back_mv_table[xy][1]; | |
| 327 | 2391 break; |
| 2392 case MB_TYPE_FORWARD: | |
| 324 | 2393 s->mv_dir = MV_DIR_FORWARD; |
| 2394 s->mb_intra= 0; | |
| 2395 motion_x= s->mv[0][0][0] = s->b_forw_mv_table[xy][0]; | |
| 2396 motion_y= s->mv[0][0][1] = s->b_forw_mv_table[xy][1]; | |
| 2397 // printf(" %d %d ", motion_x, motion_y); | |
| 327 | 2398 break; |
| 2399 default: | |
| 324 | 2400 motion_x=motion_y=0; //gcc warning fix |
| 2401 printf("illegal MB type\n"); | |
| 294 | 2402 } |
| 324 | 2403 encode_mb(s, motion_x, motion_y); |
|
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2404 } |
| 327 | 2405 /* clean the MV table in IPS frames for direct mode in B frames */ |
| 2406 if(s->mb_intra /* && I,P,S_TYPE */){ | |
| 2407 s->p_mv_table[xy][0]=0; | |
| 2408 s->p_mv_table[xy][1]=0; | |
| 2409 } | |
| 0 | 2410 |
|
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
|
2411 MPV_decode_mb(s, s->block); |
| 456 | 2412 //printf("MB %d %d bits\n", s->mb_x+s->mb_y*s->mb_width, get_bit_count(&s->pb)); |
| 0 | 2413 } |
|
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
|
2414 |
|
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
|
2415 |
| 231 | 2416 /* Obtain average GOB size for RTP */ |
| 2417 if (s->rtp_mode) { | |
| 2418 if (!mb_y) | |
|
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
|
2419 s->mb_line_avgsize = pbBufPtr(&s->pb) - s->ptr_last_mb_line; |
| 231 | 2420 else if (!(mb_y % s->gob_index)) { |
|
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
|
2421 s->mb_line_avgsize = (s->mb_line_avgsize + pbBufPtr(&s->pb) - s->ptr_last_mb_line) >> 1; |
|
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
|
2422 s->ptr_last_mb_line = pbBufPtr(&s->pb); |
| 231 | 2423 } |
| 2424 //fprintf(stderr, "\nMB line: %d\tSize: %u\tAvg. Size: %u", s->mb_y, | |
| 2425 // (s->pb.buf_ptr - s->ptr_last_mb_line), s->mb_line_avgsize); | |
| 456 | 2426 if(s->codec_id!=CODEC_ID_MPEG4) s->first_slice_line = 0; //FIXME clean |
| 231 | 2427 } |
| 0 | 2428 } |
| 294 | 2429 emms_c(); |
| 286 | 2430 |
| 456 | 2431 if(s->codec_id==CODEC_ID_MPEG4 && s->data_partitioning && s->pict_type!=B_TYPE) |
| 2432 ff_mpeg4_merge_partitions(s); | |
| 2433 | |
| 2434 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type == I_TYPE) | |
| 208 | 2435 msmpeg4_encode_ext_header(s); |
| 2436 | |
| 456 | 2437 if(s->codec_id==CODEC_ID_MPEG4) |
| 2438 ff_mpeg4_stuffing(&s->pb); | |
| 2439 | |
| 162 | 2440 //if (s->gob_number) |
| 2441 // fprintf(stderr,"\nNumber of GOB: %d", s->gob_number); | |
| 231 | 2442 |
| 2443 /* Send the last GOB if RTP */ | |
| 2444 if (s->rtp_mode) { | |
| 2445 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
|
2446 pdif = pbBufPtr(&s->pb) - s->ptr_lastgob; |
| 231 | 2447 /* Call the RTP callback to send the last GOB */ |
| 2448 if (s->rtp_callback) | |
| 2449 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
|
2450 s->ptr_lastgob = pbBufPtr(&s->pb); |
| 231 | 2451 //fprintf(stderr,"\nGOB: %2d size: %d (last)", s->gob_number, pdif); |
| 2452 } | |
| 0 | 2453 } |
| 2454 | |
| 220 | 2455 static int dct_quantize_c(MpegEncContext *s, |
| 0 | 2456 DCTELEM *block, int n, |
| 344 | 2457 int qscale, int *overflow) |
| 0 | 2458 { |
| 2459 int i, j, level, last_non_zero, q; | |
| 2460 const int *qmat; | |
| 344 | 2461 int bias; |
| 2462 int max=0; | |
| 2463 unsigned int threshold1, threshold2; | |
| 456 | 2464 |
|
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
|
2465 s->fdct (block); |
| 0 | 2466 |
| 64 | 2467 /* we need this permutation so that we correct the IDCT |
| 2468 permutation. will be moved into DCT code */ | |
| 2469 block_permute(block); | |
| 2470 | |
| 0 | 2471 if (s->mb_intra) { |
|
350
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2472 if (!s->h263_aic) { |
|
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2473 if (n < 4) |
|
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2474 q = s->y_dc_scale; |
|
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2475 else |
|
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2476 q = s->c_dc_scale; |
|
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2477 q = q << 3; |
|
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2478 } else |
|
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2479 /* 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
|
2480 q = 1 << 3; |
|
6ebbecc10063
- Advanced Intra Coding (AIC) support for H.263+ encoder, just DC by now.
pulento
parents:
347
diff
changeset
|
2481 |
| 0 | 2482 /* note: block[0] is assumed to be positive */ |
| 2483 block[0] = (block[0] + (q >> 1)) / q; | |
| 2484 i = 1; | |
| 2485 last_non_zero = 0; | |
| 344 | 2486 qmat = s->q_intra_matrix[qscale]; |
| 2487 bias= s->intra_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT); | |
| 0 | 2488 } else { |
| 2489 i = 0; | |
| 2490 last_non_zero = -1; | |
| 344 | 2491 qmat = s->q_inter_matrix[qscale]; |
| 2492 bias= s->inter_quant_bias<<(QMAT_SHIFT - 3 - QUANT_BIAS_SHIFT); | |
| 0 | 2493 } |
| 344 | 2494 threshold1= (1<<(QMAT_SHIFT - 3)) - bias - 1; |
| 2495 threshold2= threshold1<<1; | |
| 0 | 2496 |
| 2497 for(;i<64;i++) { | |
| 2498 j = zigzag_direct[i]; | |
| 2499 level = block[j]; | |
| 2500 level = level * qmat[j]; | |
| 344 | 2501 |
| 2502 // if( bias+level >= (1<<(QMAT_SHIFT - 3)) | |
| 2503 // || bias-level >= (1<<(QMAT_SHIFT - 3))){ | |
| 2504 if(((unsigned)(level+threshold1))>threshold2){ | |
| 2505 if(level>0){ | |
| 2506 level= (bias + level)>>(QMAT_SHIFT - 3); | |
| 2507 block[j]= level; | |
| 2508 }else{ | |
| 2509 level= (bias - level)>>(QMAT_SHIFT - 3); | |
| 2510 block[j]= -level; | |
| 0 | 2511 } |
| 344 | 2512 max |=level; |
| 0 | 2513 last_non_zero = i; |
| 344 | 2514 }else{ |
| 2515 block[j]=0; | |
| 0 | 2516 } |
| 2517 } | |
| 344 | 2518 *overflow= s->max_qcoeff < max; //overflow might have happend |
| 2519 | |
| 0 | 2520 return last_non_zero; |
| 2521 } | |
| 2522 | |
|
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
|
2523 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
|
2524 DCTELEM *block, int n, int qscale) |
| 0 | 2525 { |
| 200 | 2526 int i, level, nCoeffs; |
| 0 | 2527 const UINT16 *quant_matrix; |
| 2528 | |
| 200 | 2529 if(s->alternate_scan) nCoeffs= 64; |
| 2530 else nCoeffs= s->block_last_index[n]+1; | |
| 2531 | |
| 0 | 2532 if (s->mb_intra) { |
| 2533 if (n < 4) | |
| 2534 block[0] = block[0] * s->y_dc_scale; | |
| 2535 else | |
| 2536 block[0] = block[0] * s->c_dc_scale; | |
| 2537 /* XXX: only mpeg1 */ | |
| 2538 quant_matrix = s->intra_matrix; | |
| 200 | 2539 for(i=1;i<nCoeffs;i++) { |
| 2540 int j= zigzag_direct[i]; | |
| 2541 level = block[j]; | |
| 0 | 2542 if (level) { |
| 2543 if (level < 0) { | |
| 2544 level = -level; | |
| 200 | 2545 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
| 0 | 2546 level = (level - 1) | 1; |
| 2547 level = -level; | |
| 2548 } else { | |
| 200 | 2549 level = (int)(level * qscale * quant_matrix[j]) >> 3; |
| 0 | 2550 level = (level - 1) | 1; |
| 2551 } | |
| 2552 #ifdef PARANOID | |
| 2553 if (level < -2048 || level > 2047) | |
| 2554 fprintf(stderr, "unquant error %d %d\n", i, level); | |
| 2555 #endif | |
| 200 | 2556 block[j] = level; |
| 0 | 2557 } |
| 2558 } | |
| 2559 } else { | |
| 2560 i = 0; | |
| 344 | 2561 quant_matrix = s->inter_matrix; |
| 217 | 2562 for(;i<nCoeffs;i++) { |
| 200 | 2563 int j= zigzag_direct[i]; |
| 2564 level = block[j]; | |
| 0 | 2565 if (level) { |
| 2566 if (level < 0) { | |
| 2567 level = -level; | |
| 2568 level = (((level << 1) + 1) * qscale * | |
| 200 | 2569 ((int) (quant_matrix[j]))) >> 4; |
| 0 | 2570 level = (level - 1) | 1; |
| 2571 level = -level; | |
| 2572 } else { | |
| 2573 level = (((level << 1) + 1) * qscale * | |
| 200 | 2574 ((int) (quant_matrix[j]))) >> 4; |
| 0 | 2575 level = (level - 1) | 1; |
| 2576 } | |
| 2577 #ifdef PARANOID | |
| 2578 if (level < -2048 || level > 2047) | |
| 2579 fprintf(stderr, "unquant error %d %d\n", i, level); | |
| 2580 #endif | |
| 200 | 2581 block[j] = level; |
| 0 | 2582 } |
| 2583 } | |
| 2584 } | |
| 2585 } | |
|
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
|
2586 |
| 325 | 2587 static void dct_unquantize_mpeg2_c(MpegEncContext *s, |
| 2588 DCTELEM *block, int n, int qscale) | |
| 2589 { | |
| 2590 int i, level, nCoeffs; | |
| 2591 const UINT16 *quant_matrix; | |
| 2592 | |
| 2593 if(s->alternate_scan) nCoeffs= 64; | |
| 2594 else nCoeffs= s->block_last_index[n]+1; | |
| 2595 | |
| 2596 if (s->mb_intra) { | |
| 2597 if (n < 4) | |
| 2598 block[0] = block[0] * s->y_dc_scale; | |
| 2599 else | |
| 2600 block[0] = block[0] * s->c_dc_scale; | |
| 2601 quant_matrix = s->intra_matrix; | |
| 2602 for(i=1;i<nCoeffs;i++) { | |
| 2603 int j= zigzag_direct[i]; | |
| 2604 level = block[j]; | |
| 2605 if (level) { | |
| 2606 if (level < 0) { | |
| 2607 level = -level; | |
| 2608 level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
| 2609 level = -level; | |
| 2610 } else { | |
| 2611 level = (int)(level * qscale * quant_matrix[j]) >> 3; | |
| 2612 } | |
| 2613 #ifdef PARANOID | |
| 2614 if (level < -2048 || level > 2047) | |
| 2615 fprintf(stderr, "unquant error %d %d\n", i, level); | |
| 2616 #endif | |
| 2617 block[j] = level; | |
| 2618 } | |
| 2619 } | |
| 2620 } else { | |
| 2621 int sum=-1; | |
| 2622 i = 0; | |
| 344 | 2623 quant_matrix = s->inter_matrix; |
| 325 | 2624 for(;i<nCoeffs;i++) { |
| 2625 int j= zigzag_direct[i]; | |
| 2626 level = block[j]; | |
| 2627 if (level) { | |
| 2628 if (level < 0) { | |
| 2629 level = -level; | |
| 2630 level = (((level << 1) + 1) * qscale * | |
| 2631 ((int) (quant_matrix[j]))) >> 4; | |
| 2632 level = -level; | |
| 2633 } else { | |
| 2634 level = (((level << 1) + 1) * qscale * | |
| 2635 ((int) (quant_matrix[j]))) >> 4; | |
| 2636 } | |
| 2637 #ifdef PARANOID | |
| 2638 if (level < -2048 || level > 2047) | |
| 2639 fprintf(stderr, "unquant error %d %d\n", i, level); | |
| 2640 #endif | |
| 2641 block[j] = level; | |
| 2642 sum+=level; | |
| 2643 } | |
| 2644 } | |
| 2645 block[63]^=sum&1; | |
| 2646 } | |
| 2647 } | |
| 2648 | |
| 2649 | |
|
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
|
2650 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
|
2651 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
|
2652 { |
|
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
|
2653 int i, level, qmul, qadd; |
| 200 | 2654 int nCoeffs; |
|
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2655 |
|
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
|
2656 if (s->mb_intra) { |
|
249
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2657 if (!s->h263_aic) { |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2658 if (n < 4) |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2659 block[0] = block[0] * s->y_dc_scale; |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2660 else |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2661 block[0] = block[0] * s->c_dc_scale; |
|
42a0b7b16738
- Bug fixes in H.263+ Advanced INTRA Coding decoder.
pulento
parents:
248
diff
changeset
|
2662 } |
|
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
|
2663 i = 1; |
| 200 | 2664 nCoeffs= 64; //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
|
2665 } 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
|
2666 i = 0; |
| 200 | 2667 nCoeffs= zigzag_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
|
2668 } |
|
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
|
2669 |
|
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
|
2670 qmul = s->qscale << 1; |
|
248
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2671 if (s->h263_aic && s->mb_intra) |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2672 qadd = 0; |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2673 else |
|
56ee684c48bb
- H.263+ decoder support for Advanded INTRA Coding (buggy)
pulento
parents:
244
diff
changeset
|
2674 qadd = (s->qscale - 1) | 1; |
|
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
|
2675 |
| 200 | 2676 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
|
2677 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
|
2678 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
|
2679 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
|
2680 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
|
2681 } 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
|
2682 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
|
2683 } |
|
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
|
2684 #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
|
2685 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
|
2686 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
|
2687 #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
|
2688 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
|
2689 } |
|
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
|
2690 } |
|
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
|
2691 } |
| 0 | 2692 |
| 456 | 2693 static void remove_ac(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int mb_x, int mb_y) |
| 2694 { | |
| 2695 int dc, dcb, dcr, y, i; | |
| 2696 for(i=0; i<4; i++){ | |
| 2697 dc= s->dc_val[0][mb_x*2+1 + (i&1) + (mb_y*2+1 + (i>>1))*(s->mb_width*2+2)]; | |
| 2698 for(y=0; y<8; y++){ | |
| 2699 int x; | |
| 2700 for(x=0; x<8; x++){ | |
| 2701 dest_y[x + (i&1)*8 + (y + (i>>1)*8)*s->linesize]= dc/8; | |
| 2702 } | |
| 2703 } | |
| 2704 } | |
| 2705 dcb = s->dc_val[1][mb_x+1 + (mb_y+1)*(s->mb_width+2)]; | |
| 2706 dcr= s->dc_val[2][mb_x+1 + (mb_y+1)*(s->mb_width+2)]; | |
| 2707 for(y=0; y<8; y++){ | |
| 2708 int x; | |
| 2709 for(x=0; x<8; x++){ | |
| 556 | 2710 dest_cb[x + y*(s->uvlinesize)]= dcb/8; |
| 2711 dest_cr[x + y*(s->uvlinesize)]= dcr/8; | |
| 456 | 2712 } |
| 2713 } | |
| 2714 } | |
| 2715 | |
| 2716 /** | |
| 2717 * will conceal past errors, and allso drop b frames if needed | |
| 2718 * | |
| 2719 */ | |
| 2720 void ff_conceal_past_errors(MpegEncContext *s, int unknown_pos) | |
| 2721 { | |
| 2722 int mb_x= s->mb_x; | |
| 2723 int mb_y= s->mb_y; | |
| 2724 int mb_dist=0; | |
| 2725 int i, intra_count=0, inter_count=0; | |
| 2726 int intra_conceal= s->msmpeg4_version ? 50 : 50; //FIXME finetune | |
| 2727 int inter_conceal= s->msmpeg4_version ? 50 : 50; | |
| 612 | 2728 |
| 456 | 2729 // for last block |
| 2730 if(mb_x>=s->mb_width) mb_x= s->mb_width -1; | |
| 2731 if(mb_y>=s->mb_height) mb_y= s->mb_height-1; | |
| 2732 | |
| 2733 if(s->decoding_error==0 && unknown_pos){ | |
| 2734 if(s->data_partitioning && s->pict_type!=B_TYPE) | |
| 2735 s->decoding_error= DECODING_AC_LOST; | |
| 2736 else | |
| 2737 s->decoding_error= DECODING_DESYNC; | |
| 2738 } | |
| 2739 | |
| 2740 if(s->decoding_error==DECODING_DESYNC && s->pict_type!=B_TYPE) s->next_p_frame_damaged=1; | |
| 2741 | |
| 2742 for(i=mb_x + mb_y*s->mb_width; i>=0; i--){ | |
| 2743 if(s->mbintra_table[i]) intra_count++; | |
| 2744 else inter_count++; | |
| 2745 } | |
| 2746 | |
| 2747 if(s->decoding_error==DECODING_AC_LOST){ | |
| 2748 intra_conceal*=2; | |
| 2749 inter_conceal*=2; | |
| 2750 }else if(s->decoding_error==DECODING_ACDC_LOST){ | |
| 2751 intra_conceal*=2; | |
| 2752 inter_conceal*=2; | |
| 2753 } | |
| 2754 | |
| 2755 if(unknown_pos && (intra_count<inter_count)){ | |
| 2756 intra_conceal= inter_conceal= s->mb_num; | |
| 2757 // printf("%d %d\n",intra_count, inter_count); | |
| 2758 } | |
| 2759 | |
| 2760 fprintf(stderr, "concealing errors\n"); | |
| 2761 | |
| 2762 /* for all MBs from the current one back until the last resync marker */ | |
| 2763 for(; mb_y>=0 && mb_y>=s->resync_mb_y; mb_y--){ | |
| 2764 for(; mb_x>=0; mb_x--){ | |
| 2765 uint8_t *dest_y = s->current_picture[0] + (mb_y * 16* s->linesize ) + mb_x * 16; | |
| 556 | 2766 uint8_t *dest_cb = s->current_picture[1] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; |
| 2767 uint8_t *dest_cr = s->current_picture[2] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; | |
| 456 | 2768 int mb_x_backup= s->mb_x; //FIXME pass xy to mpeg_motion |
| 2769 int mb_y_backup= s->mb_y; | |
| 2770 s->mb_x=mb_x; | |
| 2771 s->mb_y=mb_y; | |
| 2772 if(s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<intra_conceal){ | |
| 2773 if(s->decoding_error==DECODING_AC_LOST){ | |
| 2774 remove_ac(s, dest_y, dest_cb, dest_cr, mb_x, mb_y); | |
| 2775 // printf("remove ac to %d %d\n", mb_x, mb_y); | |
| 2776 }else{ | |
| 2777 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 2778 s->last_picture, 0, 0, put_pixels_tab, | |
| 2779 0/*mx*/, 0/*my*/, 16); | |
| 2780 } | |
| 2781 } | |
| 2782 else if(!s->mbintra_table[mb_y*s->mb_width + mb_x] && mb_dist<inter_conceal){ | |
| 2783 int mx=0; | |
| 2784 int my=0; | |
| 2785 | |
| 2786 if(s->decoding_error!=DECODING_DESYNC){ | |
| 2787 int xy= mb_x*2+1 + (mb_y*2+1)*(s->mb_width*2+2); | |
| 2788 mx= s->motion_val[ xy ][0]; | |
| 2789 my= s->motion_val[ xy ][1]; | |
| 2790 } | |
| 2791 | |
| 2792 mpeg_motion(s, dest_y, dest_cb, dest_cr, 0, | |
| 2793 s->last_picture, 0, 0, put_pixels_tab, | |
| 2794 mx, my, 16); | |
| 2795 } | |
| 2796 s->mb_x= mb_x_backup; | |
| 2797 s->mb_y= mb_y_backup; | |
| 2798 | |
| 2799 if(mb_x== s->resync_mb_x && mb_y== s->resync_mb_y) return; | |
| 2800 if(!s->mbskip_table[mb_x + mb_y*s->mb_width]) mb_dist++; | |
| 2801 } | |
| 2802 mb_x=s->mb_width-1; | |
| 2803 } | |
| 2804 } | |
| 2805 | |
| 0 | 2806 AVCodec mpeg1video_encoder = { |
| 2807 "mpeg1video", | |
| 2808 CODEC_TYPE_VIDEO, | |
| 2809 CODEC_ID_MPEG1VIDEO, | |
| 2810 sizeof(MpegEncContext), | |
| 2811 MPV_encode_init, | |
| 2812 MPV_encode_picture, | |
| 2813 MPV_encode_end, | |
| 2814 }; | |
| 2815 | |
| 2816 AVCodec h263_encoder = { | |
| 2817 "h263", | |
| 2818 CODEC_TYPE_VIDEO, | |
| 2819 CODEC_ID_H263, | |
| 2820 sizeof(MpegEncContext), | |
| 2821 MPV_encode_init, | |
| 2822 MPV_encode_picture, | |
| 2823 MPV_encode_end, | |
| 2824 }; | |
| 2825 | |
| 2826 AVCodec h263p_encoder = { | |
| 2827 "h263p", | |
| 2828 CODEC_TYPE_VIDEO, | |
| 2829 CODEC_ID_H263P, | |
| 2830 sizeof(MpegEncContext), | |
| 2831 MPV_encode_init, | |
| 2832 MPV_encode_picture, | |
| 2833 MPV_encode_end, | |
| 2834 }; | |
| 2835 | |
| 2836 AVCodec rv10_encoder = { | |
| 2837 "rv10", | |
| 2838 CODEC_TYPE_VIDEO, | |
| 2839 CODEC_ID_RV10, | |
| 2840 sizeof(MpegEncContext), | |
| 2841 MPV_encode_init, | |
| 2842 MPV_encode_picture, | |
| 2843 MPV_encode_end, | |
| 2844 }; | |
| 2845 | |
| 2846 AVCodec mjpeg_encoder = { | |
| 2847 "mjpeg", | |
| 2848 CODEC_TYPE_VIDEO, | |
| 2849 CODEC_ID_MJPEG, | |
| 2850 sizeof(MpegEncContext), | |
| 2851 MPV_encode_init, | |
| 2852 MPV_encode_picture, | |
| 2853 MPV_encode_end, | |
| 2854 }; | |
| 2855 | |
| 71 | 2856 AVCodec mpeg4_encoder = { |
| 2857 "mpeg4", | |
| 0 | 2858 CODEC_TYPE_VIDEO, |
| 71 | 2859 CODEC_ID_MPEG4, |
| 0 | 2860 sizeof(MpegEncContext), |
| 2861 MPV_encode_init, | |
| 2862 MPV_encode_picture, | |
| 2863 MPV_encode_end, | |
| 2864 }; | |
| 2865 | |
| 307 | 2866 AVCodec msmpeg4v1_encoder = { |
| 2867 "msmpeg4v1", | |
| 0 | 2868 CODEC_TYPE_VIDEO, |
| 307 | 2869 CODEC_ID_MSMPEG4V1, |
| 0 | 2870 sizeof(MpegEncContext), |
| 2871 MPV_encode_init, | |
| 2872 MPV_encode_picture, | |
| 2873 MPV_encode_end, | |
| 2874 }; | |
| 307 | 2875 |
| 2876 AVCodec msmpeg4v2_encoder = { | |
| 2877 "msmpeg4v2", | |
| 2878 CODEC_TYPE_VIDEO, | |
| 2879 CODEC_ID_MSMPEG4V2, | |
| 2880 sizeof(MpegEncContext), | |
| 2881 MPV_encode_init, | |
| 2882 MPV_encode_picture, | |
| 2883 MPV_encode_end, | |
| 2884 }; | |
| 2885 | |
| 2886 AVCodec msmpeg4v3_encoder = { | |
| 2887 "msmpeg4", | |
| 2888 CODEC_TYPE_VIDEO, | |
| 2889 CODEC_ID_MSMPEG4V3, | |
| 2890 sizeof(MpegEncContext), | |
| 2891 MPV_encode_init, | |
| 2892 MPV_encode_picture, | |
| 2893 MPV_encode_end, | |
| 2894 }; | |
| 499 | 2895 |
| 2896 AVCodec wmv1_encoder = { | |
| 2897 "wmv1", | |
| 2898 CODEC_TYPE_VIDEO, | |
| 2899 CODEC_ID_WMV1, | |
| 2900 sizeof(MpegEncContext), | |
| 2901 MPV_encode_init, | |
| 2902 MPV_encode_picture, | |
| 2903 MPV_encode_end, | |
| 2904 }; | |
| 2905 | |
| 2906 AVCodec wmv2_encoder = { | |
| 2907 "wmv2", | |
| 2908 CODEC_TYPE_VIDEO, | |
| 2909 CODEC_ID_WMV2, | |
| 2910 sizeof(MpegEncContext), | |
| 2911 MPV_encode_init, | |
| 2912 MPV_encode_picture, | |
| 2913 MPV_encode_end, | |
| 2914 }; |
