interlaced motion estimation
[libav.git] / libavcodec / h263.c
CommitLineData
de6d9b64
FB
1/*
2 * H263/MPEG4 backend for ffmpeg encoder and decoder
ff4ec49e 3 * Copyright (c) 2000,2001 Fabrice Bellard.
6dbd39fe 4 * H263+ support.
de6d9b64
FB
5 * Copyright (c) 2001 Juan J. Sierralta P.
6 *
ff4ec49e
FB
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
de6d9b64 11 *
ff4ec49e 12 * This library is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
ff4ec49e
FB
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
45870f57 20 *
66370d3f
MN
21 * ac prediction encoding, b-frame support, error resilience, optimizations,
22 * qpel decoding, gmc decoding, interlaced decoding,
23 * by Michael Niedermayer <michaelni@gmx.at>
de6d9b64 24 */
2ba8f6b8
MN
25
26/**
27 * @file h263.c
983e3246 28 * h263/mpeg4 codec.
2ba8f6b8 29 */
1e209222
J
30
31//#define DEBUG
f2f6134b
MN
32#include <limits.h>
33
de6d9b64
FB
34#include "common.h"
35#include "dsputil.h"
36#include "avcodec.h"
37#include "mpegvideo.h"
38#include "h263data.h"
39#include "mpeg4data.h"
40
c5d309f2
MN
41//#undef NDEBUG
42//#include <assert.h>
43
edcf9890 44#define INTRA_MCBPC_VLC_BITS 6
7af75e6c 45#define INTER_MCBPC_VLC_BITS 7
edcf9890
MN
46#define CBPY_VLC_BITS 6
47#define MV_VLC_BITS 9
48#define DC_VLC_BITS 9
49#define SPRITE_TRAJ_VLC_BITS 6
50#define MB_TYPE_B_VLC_BITS 4
51#define TEX_VLC_BITS 9
b40cd4e0
MN
52#define H263_MBTYPE_B_VLC_BITS 6
53#define CBPC_B_VLC_BITS 3
edcf9890 54
bbed9259 55#ifdef CONFIG_ENCODERS
de6d9b64 56static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
d4f5d74a 57 int n);
9dbcbd92 58static void h263_encode_motion(MpegEncContext * s, int val, int fcode);
6dbd39fe 59static void h263p_encode_umotion(MpegEncContext * s, int val);
3db48188 60static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
d4f5d74a 61 int n, int dc, uint8_t *scan_table,
92073d01 62 PutBitContext *dc_pb, PutBitContext *ac_pb);
bbed9259
MN
63#endif
64
6f91bcd1 65static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
6dbd39fe 66static int h263p_decode_umotion(MpegEncContext * s, int pred);
de6d9b64
FB
67static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
68 int n, int coded);
92073d01
MN
69static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
70static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
a4e8b587 71 int n, int coded, int intra, int rvlc);
0c1a9eda 72static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr);
7604246d 73#ifdef CONFIG_ENCODERS
22ddd60b
MN
74static void mpeg4_encode_visual_object_header(MpegEncContext * s);
75static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number);
7604246d 76#endif //CONFIG_ENCODERS
49092244 77static void mpeg4_decode_sprite_trajectory(MpegEncContext * s);
0c1a9eda 78static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr, int *dir_ptr);
4d2858de 79
7604246d 80#ifdef CONFIG_ENCODERS
0c1a9eda
ZK
81static uint8_t uni_DCtab_lum_len[512];
82static uint8_t uni_DCtab_chrom_len[512];
83static uint16_t uni_DCtab_lum_bits[512];
84static uint16_t uni_DCtab_chrom_bits[512];
bbed9259 85
30952237 86static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
0c1a9eda
ZK
87static uint8_t fcode_tab[MAX_MV*2+1];
88static uint8_t umv_fcode_tab[MAX_MV*2+1];
45870f57 89
477ab036
MN
90static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2];
91static uint8_t uni_mpeg4_intra_rl_len [64*64*2*2];
92static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2];
93static uint8_t uni_mpeg4_inter_rl_len [64*64*2*2];
94//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
3db48188 95//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
477ab036 96#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
3db48188
MN
97
98/* mpeg4
99inter
100max level: 24/6
101max run: 53/63
102
103intra
104max level: 53/16
105max run: 29/41
106*/
bbed9259
MN
107#endif
108
6d71b382
MN
109#if 0 //3IV1 is quite rare and tis slows things down a tiny bit
110#define IS_3IV1 s->avctx->codec_tag == ff_get_fourcc("3IV1")
111#else
112#define IS_3IV1 0
113#endif
2a164b25 114
de6d9b64
FB
115int h263_get_picture_format(int width, int height)
116{
117 int format;
118
119 if (width == 128 && height == 96)
1e209222 120 format = 1;
de6d9b64 121 else if (width == 176 && height == 144)
1e209222 122 format = 2;
de6d9b64 123 else if (width == 352 && height == 288)
1e209222 124 format = 3;
de6d9b64 125 else if (width == 704 && height == 576)
1e209222 126 format = 4;
de6d9b64 127 else if (width == 1408 && height == 1152)
1e209222 128 format = 5;
de6d9b64
FB
129 else
130 format = 7;
131 return format;
132}
133
7604246d
WH
134#ifdef CONFIG_ENCODERS
135
5ff85f1d 136static void aspect_to_info(MpegEncContext * s, AVRational aspect){
58b0b0dd
MN
137 int i;
138
5ff85f1d 139 if(aspect.num==0) aspect= (AVRational){1,1};
58b0b0dd 140
58b0b0dd 141 for(i=1; i<6; i++){
5ff85f1d 142 if(av_cmp_q(pixel_aspect[i], aspect) == 0){
58b0b0dd
MN
143 s->aspect_ratio_info=i;
144 return;
145 }
146 }
5d3cea3a 147
58b0b0dd 148 s->aspect_ratio_info= FF_ASPECT_EXTENDED;
5d3cea3a
MN
149}
150
44a2950f 151void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number)
de6d9b64 152{
44a2950f 153 int format;
de6d9b64 154
44a2950f 155 align_put_bits(&s->pb);
81401c1f 156
d4f5d74a
GM
157 put_bits(&s->pb, 17, 1);
158 put_bits(&s->pb, 5, (s->h263_flv-1)); /* 0: h263 escape codes 1: 11-bit escape codes */
159 put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->frame_rate_base) /
160 s->avctx->frame_rate) & 0xff); /* TemporalReference */
161 if (s->width == 352 && s->height == 288)
162 format = 2;
163 else if (s->width == 176 && s->height == 144)
164 format = 3;
165 else if (s->width == 128 && s->height == 96)
166 format = 4;
167 else if (s->width == 320 && s->height == 240)
168 format = 5;
169 else if (s->width == 160 && s->height == 120)
170 format = 6;
171 else if (s->width <= 255 && s->height <= 255)
172 format = 0; /* use 1 byte width & height */
173 else
174 format = 1; /* use 2 bytes width & height */
175 put_bits(&s->pb, 3, format); /* PictureSize */
176 if (format == 0) {
177 put_bits(&s->pb, 8, s->width);
178 put_bits(&s->pb, 8, s->height);
179 } else if (format == 1) {
180 put_bits(&s->pb, 16, s->width);
181 put_bits(&s->pb, 16, s->height);
182 }
183 put_bits(&s->pb, 2, s->pict_type == P_TYPE); /* PictureType */
f5ea4461 184 put_bits(&s->pb, 1, 1); /* DeblockingFlag: on */
d4f5d74a
GM
185 put_bits(&s->pb, 5, s->qscale); /* Quantizer */
186 put_bits(&s->pb, 1, 0); /* ExtraInformation */
187
188 if(s->h263_aic){
189 s->y_dc_scale_table=
68b94c35 190 s->c_dc_scale_table= ff_aic_dc_scale_table;
d4f5d74a
GM
191 }else{
192 s->y_dc_scale_table=
193 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
194 }
44a2950f
MN
195}
196
197void h263_encode_picture_header(MpegEncContext * s, int picture_number)
198{
199 int format;
200
201 align_put_bits(&s->pb);
d4f5d74a 202
81401c1f 203 /* Update the pointer to last GOB */
17592475 204 s->ptr_lastgob = pbBufPtr(&s->pb);
81401c1f 205 put_bits(&s->pb, 22, 0x20); /* PSC */
14bea432
MN
206 put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->frame_rate_base) /
207 s->avctx->frame_rate) & 0xff);
de6d9b64
FB
208
209 put_bits(&s->pb, 1, 1); /* marker */
210 put_bits(&s->pb, 1, 0); /* h263 id */
211 put_bits(&s->pb, 1, 0); /* split screen off */
212 put_bits(&s->pb, 1, 0); /* camera off */
213 put_bits(&s->pb, 1, 0); /* freeze picture release off */
6dbd39fe
J
214
215 format = h263_get_picture_format(s->width, s->height);
de6d9b64
FB
216 if (!s->h263_plus) {
217 /* H.263v1 */
de6d9b64
FB
218 put_bits(&s->pb, 3, format);
219 put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
220 /* By now UMV IS DISABLED ON H.263v1, since the restrictions
221 of H.263v1 UMV implies to check the predicted MV after
222 calculation of the current MB to see if we're on the limits */
223 put_bits(&s->pb, 1, 0); /* unrestricted motion vector: off */
224 put_bits(&s->pb, 1, 0); /* SAC: off */
f7190f73 225 put_bits(&s->pb, 1, s->obmc); /* advanced prediction mode */
de6d9b64
FB
226 put_bits(&s->pb, 1, 0); /* not PB frame */
227 put_bits(&s->pb, 5, s->qscale);
228 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
229 } else {
230 /* H.263v2 */
231 /* H.263 Plus PTYPE */
232 put_bits(&s->pb, 3, 7);
233 put_bits(&s->pb,3,1); /* Update Full Extended PTYPE */
6dbd39fe
J
234 if (format == 7)
235 put_bits(&s->pb,3,6); /* Custom Source Format */
236 else
237 put_bits(&s->pb, 3, format);
238
de6d9b64 239 put_bits(&s->pb,1,0); /* Custom PCF: off */
ba58dabc 240 put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
de6d9b64 241 put_bits(&s->pb,1,0); /* SAC: off */
f7190f73 242 put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
ef5b1b5a 243 put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
332f9ac4 244 put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
ba58dabc 245 put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
de6d9b64
FB
246 put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
247 put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
dba019da 248 put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
332f9ac4 249 put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
de6d9b64
FB
250 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
251 put_bits(&s->pb,3,0); /* Reserved */
252
253 put_bits(&s->pb, 3, s->pict_type == P_TYPE);
254
255 put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
256 put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
ef5b1b5a 257 put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
de6d9b64
FB
258 put_bits(&s->pb,2,0); /* Reserved */
259 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
260
261 /* This should be here if PLUSPTYPE */
262 put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
263
6dbd39fe
J
264 if (format == 7) {
265 /* Custom Picture Format (CPFMT) */
5ff85f1d 266 aspect_to_info(s, s->avctx->sample_aspect_ratio);
5d3cea3a
MN
267
268 put_bits(&s->pb,4,s->aspect_ratio_info);
6dbd39fe
J
269 put_bits(&s->pb,9,(s->width >> 2) - 1);
270 put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
271 put_bits(&s->pb,9,(s->height >> 2));
5ff85f1d
MN
272 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
273 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
274 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
050fe8ba 275 }
6dbd39fe
J
276 }
277
de6d9b64 278 /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
544286b3 279 if (s->umvplus)
e51d6d27 280// put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
332f9ac4 281//FIXME check actual requested range
e51d6d27 282 put_bits(&s->pb,2,1); /* unlimited */
ba58dabc
MN
283 if(s->h263_slice_structured)
284 put_bits(&s->pb,2,0); /* no weird submodes */
e51d6d27 285
de6d9b64
FB
286 put_bits(&s->pb, 5, s->qscale);
287 }
288
289 put_bits(&s->pb, 1, 0); /* no PEI */
8f8402e4 290
0ed714a7
MN
291 if(s->h263_slice_structured){
292 put_bits(&s->pb, 1, 1);
293
294 assert(s->mb_x == 0 && s->mb_y == 0);
295 ff_h263_encode_mba(s);
296
297 put_bits(&s->pb, 1, 1);
298 }
299
8f8402e4
MN
300 if(s->h263_aic){
301 s->y_dc_scale_table=
68b94c35 302 s->c_dc_scale_table= ff_aic_dc_scale_table;
8f8402e4
MN
303 }else{
304 s->y_dc_scale_table=
305 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
306 }
de6d9b64
FB
307}
308
945eeee1
MN
309/**
310 * Encodes a group of blocks header.
311 */
ba58dabc 312void h263_encode_gob_header(MpegEncContext * s, int mb_line)
644d98a4 313{
ba58dabc
MN
314 put_bits(&s->pb, 17, 1); /* GBSC */
315
316 if(s->h263_slice_structured){
317 put_bits(&s->pb, 1, 1);
318
319 ff_h263_encode_mba(s);
320
321 if(s->mb_num > 1583)
322 put_bits(&s->pb, 1, 1);
323 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
324 put_bits(&s->pb, 1, 1);
325 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
326 }else{
327 int gob_number= mb_line / s->gob_index;
328
329 put_bits(&s->pb, 5, gob_number); /* GN */
330 put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
331 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
332 }
644d98a4 333}
d6231b9e 334
8d8c0294
MN
335static inline int get_block_rate(MpegEncContext * s, DCTELEM block[64], int block_last_index, uint8_t scantable[64]){
336 int last=0;
337 int j;
338 int rate=0;
339
340 for(j=1; j<=block_last_index; j++){
341 const int index= scantable[j];
342 int level= block[index];
343 if(level){
344 level+= 64;
345 if((level&(~127)) == 0){
346 if(j<block_last_index) rate+= s->intra_ac_vlc_length [UNI_AC_ENC_INDEX(j-last-1, level)];
347 else rate+= s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j-last-1, level)];
348 }else
349 rate += s->ac_esc_length;
350 level-= 64;
351
352 last= j;
353 }
354 }
355
356 return rate;
357}
358
359static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6])
d6231b9e 360{
8d8c0294 361 int score= 0;
d6231b9e 362 int i, n;
1e491e29 363 int8_t * const qscale_table= s->current_picture.qscale_table;
d6231b9e 364
8d8c0294
MN
365 memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);
366
d6231b9e 367 for(n=0; n<6; n++){
0c1a9eda 368 int16_t *ac_val, *ac_val1;
8d8c0294
MN
369
370 score -= get_block_rate(s, block[n], s->block_last_index[n], s->intra_scantable.permutated);
d6231b9e 371
4278e7a6 372 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
d6231b9e
MN
373 ac_val1= ac_val;
374 if(dir[n]){
7bc9090a 375 const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
99609036 376 /* top prediction */
4278e7a6 377 ac_val-= s->block_wrap[n]*16;
1e491e29 378 if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
99609036
MN
379 /* same qscale */
380 for(i=1; i<8; i++){
b0368839 381 const int level= block[n][s->dsp.idct_permutation[i ]];
8d8c0294 382 block[n][s->dsp.idct_permutation[i ]] = level - ac_val[i+8];
b0368839 383 ac_val1[i ]= block[n][s->dsp.idct_permutation[i<<3]];
99609036
MN
384 ac_val1[i+8]= level;
385 }
386 }else{
387 /* different qscale, we must rescale */
388 for(i=1; i<8; i++){
b0368839 389 const int level= block[n][s->dsp.idct_permutation[i ]];
8d8c0294 390 block[n][s->dsp.idct_permutation[i ]] = level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
b0368839 391 ac_val1[i ]= block[n][s->dsp.idct_permutation[i<<3]];
99609036
MN
392 ac_val1[i+8]= level;
393 }
d6231b9e 394 }
8d8c0294 395 st[n]= s->intra_h_scantable.permutated;
d6231b9e 396 }else{
7bc9090a 397 const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
99609036 398 /* left prediction */
d6231b9e 399 ac_val-= 16;
1e491e29 400 if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
99609036
MN
401 /* same qscale */
402 for(i=1; i<8; i++){
b0368839 403 const int level= block[n][s->dsp.idct_permutation[i<<3]];
8d8c0294 404 block[n][s->dsp.idct_permutation[i<<3]]= level - ac_val[i];
99609036 405 ac_val1[i ]= level;
b0368839 406 ac_val1[i+8]= block[n][s->dsp.idct_permutation[i ]];
99609036
MN
407 }
408 }else{
409 /* different qscale, we must rescale */
410 for(i=1; i<8; i++){
b0368839 411 const int level= block[n][s->dsp.idct_permutation[i<<3]];
8d8c0294 412 block[n][s->dsp.idct_permutation[i<<3]]= level - ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
99609036 413 ac_val1[i ]= level;
b0368839 414 ac_val1[i+8]= block[n][s->dsp.idct_permutation[i ]];
99609036 415 }
d6231b9e 416 }
8d8c0294 417 st[n]= s->intra_v_scantable.permutated;
d6231b9e 418 }
8d8c0294
MN
419
420 for(i=63; i>0; i--) //FIXME optimize
421 if(block[n][ st[n][i] ]) break;
422 s->block_last_index[n]= i;
423
424 score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
d6231b9e
MN
425 }
426
8d8c0294
MN
427 return score < 0;
428}
429
430static inline void restore_ac_coeffs(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6])
431{
432 int i, n;
433 memcpy(s->block_last_index, zigzag_last_index, sizeof(int)*6);
434
435 for(n=0; n<6; n++){
436 int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
437
438 st[n]= s->intra_scantable.permutated;
439 if(dir[n]){
440 /* top prediction */
441 for(i=1; i<8; i++){
442 block[n][s->dsp.idct_permutation[i ]] = ac_val[i+8];
443 }
444 }else{
445 /* left prediction */
446 for(i=1; i<8; i++){
447 block[n][s->dsp.idct_permutation[i<<3]]= ac_val[i ];
448 }
449 }
450 }
d6231b9e
MN
451}
452
4d2858de
MN
453/**
454 * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
455 */
9e4c6948 456void ff_clean_h263_qscales(MpegEncContext *s){
c5d309f2 457 int i;
1e491e29 458 int8_t * const qscale_table= s->current_picture.qscale_table;
c5d309f2
MN
459
460 for(i=1; i<s->mb_num; i++){
7bc9090a
MN
461 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
462 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
c5d309f2
MN
463 }
464 for(i=s->mb_num-2; i>=0; i--){
7bc9090a
MN
465 if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
466 qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
c5d309f2 467 }
9e4c6948
MN
468}
469
4d2858de
MN
470/**
471 * modify mb_type & qscale so that encoding is acually possible in mpeg4
472 */
9e4c6948
MN
473void ff_clean_mpeg4_qscales(MpegEncContext *s){
474 int i;
1e491e29
MN
475 int8_t * const qscale_table= s->current_picture.qscale_table;
476
9e4c6948 477 ff_clean_h263_qscales(s);
c5d309f2
MN
478
479 for(i=1; i<s->mb_num; i++){
7bc9090a
MN
480 int mb_xy= s->mb_index2xy[i];
481
bb198e19
MN
482 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
483 s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_INTER4V;
484 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
c5d309f2
MN
485 }
486 }
487
488 if(s->pict_type== B_TYPE){
489 int odd=0;
490 /* ok, come on, this isnt funny anymore, theres more code for handling this mpeg4 mess than
491 for the actual adaptive quantization */
492
493 for(i=0; i<s->mb_num; i++){
7bc9090a
MN
494 int mb_xy= s->mb_index2xy[i];
495 odd += qscale_table[mb_xy]&1;
c5d309f2
MN
496 }
497
498 if(2*odd > s->mb_num) odd=1;
499 else odd=0;
500
501 for(i=0; i<s->mb_num; i++){
7bc9090a
MN
502 int mb_xy= s->mb_index2xy[i];
503 if((qscale_table[mb_xy]&1) != odd)
504 qscale_table[mb_xy]++;
505 if(qscale_table[mb_xy] > 31)
506 qscale_table[mb_xy]= 31;
c5d309f2
MN
507 }
508
509 for(i=1; i<s->mb_num; i++){
7bc9090a 510 int mb_xy= s->mb_index2xy[i];
bb198e19
MN
511 if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_DIRECT)){
512 s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_DIRECT;
513 s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_BIDIR;
c5d309f2
MN
514 }
515 }
516 }
517}
518
7604246d 519#endif //CONFIG_ENCODERS
7bc9090a
MN
520/**
521 *
522 * @return the mb_type
523 */
524int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
525 const int mb_index= s->mb_x + s->mb_y*s->mb_stride;
bb198e19 526 const int colocated_mb_type= s->next_picture.mb_type[mb_index];
1457ab52
MN
527 int xy= s->block_index[0];
528 uint16_t time_pp= s->pp_time;
529 uint16_t time_pb= s->pb_time;
530 int i;
c40c3482 531
1457ab52 532 //FIXME avoid divides
7bc9090a
MN
533
534 if(IS_8X8(colocated_mb_type)){
1457ab52
MN
535 s->mv_type = MV_TYPE_8X8;
536 for(i=0; i<4; i++){
537 xy= s->block_index[i];
8d7ec294
WH
538 s->mv[0][i][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;
539 s->mv[0][i][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;
540 s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->next_picture.motion_val[0][xy][0]
541 : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
542 s->mv[1][i][1] = my ? s->mv[0][i][1] - s->next_picture.motion_val[0][xy][1]
543 : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
1457ab52 544 }
7bc9090a
MN
545 return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
546 } else if(IS_INTERLACED(colocated_mb_type)){
1457ab52
MN
547 s->mv_type = MV_TYPE_FIELD;
548 for(i=0; i<2; i++){
549 if(s->top_field_first){
bb198e19
MN
550 time_pp= s->pp_field_time - s->p_field_select_table[i][mb_index] + i;
551 time_pb= s->pb_field_time - s->p_field_select_table[i][mb_index] + i;
1457ab52 552 }else{
bb198e19
MN
553 time_pp= s->pp_field_time + s->p_field_select_table[i][mb_index] - i;
554 time_pb= s->pb_field_time + s->p_field_select_table[i][mb_index] - i;
1457ab52 555 }
bb198e19
MN
556 s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0]*time_pb/time_pp + mx;
557 s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1]*time_pb/time_pp + my;
558 s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->p_field_mv_table[i][0][mb_index][0]
559 : s->p_field_mv_table[i][0][mb_index][0]*(time_pb - time_pp)/time_pp;
560 s->mv[1][i][1] = my ? s->mv[0][i][1] - s->p_field_mv_table[i][0][mb_index][1]
561 : s->p_field_mv_table[i][0][mb_index][1]*(time_pb - time_pp)/time_pp;
1457ab52 562 }
7bc9090a
MN
563 return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED;
564 }else{
8d7ec294
WH
565 s->mv[0][0][0] = s->mv[0][1][0] = s->mv[0][2][0] = s->mv[0][3][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;
566 s->mv[0][0][1] = s->mv[0][1][1] = s->mv[0][2][1] = s->mv[0][3][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;
567 s->mv[1][0][0] = s->mv[1][1][0] = s->mv[1][2][0] = s->mv[1][3][0] = mx ? s->mv[0][0][0] - s->next_picture.motion_val[0][xy][0]
568 : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
569 s->mv[1][0][1] = s->mv[1][1][1] = s->mv[1][2][1] = s->mv[1][3][1] = my ? s->mv[0][0][1] - s->next_picture.motion_val[0][xy][1]
570 : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
7bc9090a
MN
571 if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample)
572 s->mv_type= MV_TYPE_16X16;
573 else
574 s->mv_type= MV_TYPE_8X8;
575 return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1; //Note see prev line
1457ab52
MN
576 }
577}
578
7d1c3fc1
MN
579void ff_h263_update_motion_val(MpegEncContext * s){
580 const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
581 //FIXME a lot of thet is only needed for !low_delay
582 const int wrap = s->block_wrap[0];
583 const int xy = s->block_index[0];
584
585 s->current_picture.mbskip_table[mb_xy]= s->mb_skiped;
586
587 if(s->mv_type != MV_TYPE_8X8){
588 int motion_x, motion_y;
589 if (s->mb_intra) {
590 motion_x = 0;
591 motion_y = 0;
592 } else if (s->mv_type == MV_TYPE_16X16) {
593 motion_x = s->mv[0][0][0];
594 motion_y = s->mv[0][0][1];
595 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
596 int i;
597 motion_x = s->mv[0][0][0] + s->mv[0][1][0];
598 motion_y = s->mv[0][0][1] + s->mv[0][1][1];
599 motion_x = (motion_x>>1) | (motion_x&1);
600 for(i=0; i<2; i++){
bb198e19
MN
601 s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
602 s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
603 s->p_field_select_table[i][mb_xy]= s->field_select[0][i];
7d1c3fc1
MN
604 }
605 }
606
607 /* no update if 8X8 because it has been done during parsing */
8d7ec294
WH
608 s->current_picture.motion_val[0][xy][0] = motion_x;
609 s->current_picture.motion_val[0][xy][1] = motion_y;
610 s->current_picture.motion_val[0][xy + 1][0] = motion_x;
611 s->current_picture.motion_val[0][xy + 1][1] = motion_y;
612 s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
613 s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
614 s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
615 s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
7d1c3fc1
MN
616 }
617
618 if(s->encoding){ //FIXME encoding MUST be cleaned up
619 if (s->mv_type == MV_TYPE_8X8)
620 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_8x8;
f7190f73
MN
621 else if(s->mb_intra)
622 s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA;
7d1c3fc1
MN
623 else
624 s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16;
625 }
626}
627
bbed9259 628#ifdef CONFIG_ENCODERS
f2f6134b
MN
629
630static inline int get_p_cbp(MpegEncContext * s,
631 DCTELEM block[6][64],
632 int motion_x, int motion_y){
633 int cbp, i;
634
635 if(s->flags & CODEC_FLAG_CBP_RD){
636 int best_cbpy_score= INT_MAX;
637 int best_cbpc_score= INT_MAX;
9b879566 638 int cbpc = (-1), cbpy= (-1);
f2f6134b 639 const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0);
158c7f05 640 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
f2f6134b
MN
641
642 for(i=0; i<4; i++){
643 int score= inter_MCBPC_bits[i + offset] * lambda;
644 if(i&1) score += s->coded_score[5];
645 if(i&2) score += s->coded_score[4];
646
647 if(score < best_cbpc_score){
648 best_cbpc_score= score;
649 cbpc= i;
650 }
651 }
652
653 for(i=0; i<16; i++){
654 int score= cbpy_tab[i ^ 0xF][1] * lambda;
655 if(i&1) score += s->coded_score[3];
656 if(i&2) score += s->coded_score[2];
657 if(i&4) score += s->coded_score[1];
658 if(i&8) score += s->coded_score[0];
659
660 if(score < best_cbpy_score){
661 best_cbpy_score= score;
662 cbpy= i;
663 }
664 }
665 cbp= cbpc + 4*cbpy;
666 if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){
667 if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0)
668 cbp= 0;
669 }
670
671 for (i = 0; i < 6; i++) {
672 if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
673 s->block_last_index[i]= -1;
674 memset(s->block[i], 0, sizeof(DCTELEM)*64);
675 }
676 }
677 }else{
678 cbp= 0;
679 for (i = 0; i < 6; i++) {
680 if (s->block_last_index[i] >= 0)
681 cbp |= 1 << (5 - i);
682 }
683 }
684 return cbp;
685}
686
687static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
688 int motion_x, int motion_y, int mb_type){
689 int cbp=0, i;
690
691 if(s->flags & CODEC_FLAG_CBP_RD){
692 int score=0;
158c7f05 693 const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
f2f6134b
MN
694
695 for(i=0; i<6; i++){
696 if(s->coded_score[i] < 0){
697 score += s->coded_score[i];
698 cbp |= 1 << (5 - i);
699 }
700 }
701
702 if(cbp){
703 int zero_score= -6;
704 if ((motion_x | motion_y | s->dquant | mb_type) == 0){
705 zero_score-= 4; //2*MV + mb_type + cbp bit
706 }
707
708 zero_score*= lambda;
709 if(zero_score <= score){
710 cbp=0;
711 }
712 }
713
714 for (i = 0; i < 6; i++) {
715 if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
716 s->block_last_index[i]= -1;
717 memset(s->block[i], 0, sizeof(DCTELEM)*64);
718 }
719 }
720 }else{
721 for (i = 0; i < 6; i++) {
722 if (s->block_last_index[i] >= 0)
723 cbp |= 1 << (5 - i);
724 }
725 }
726 return cbp;
727}
728
d6231b9e
MN
729void mpeg4_encode_mb(MpegEncContext * s,
730 DCTELEM block[6][64],
731 int motion_x, int motion_y)
732{
5c91a675 733 int cbpc, cbpy, pred_x, pred_y;
92073d01
MN
734 PutBitContext * const pb2 = s->data_partitioning ? &s->pb2 : &s->pb;
735 PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=B_TYPE ? &s->tex_pb : &s->pb;
736 PutBitContext * const dc_pb = s->data_partitioning && s->pict_type!=I_TYPE ? &s->pb2 : &s->pb;
737 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
c5d309f2 738 const int dquant_code[5]= {1,0,9,2,3};
644d98a4 739
d6231b9e
MN
740 // printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
741 if (!s->mb_intra) {
f2f6134b
MN
742 int i, cbp;
743
9dbcbd92
MN
744 if(s->pict_type==B_TYPE){
745 static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
746 int mb_type= mb_type_table[s->mv_dir];
bb198e19 747
9dbcbd92 748 if(s->mb_x==0){
bb198e19
MN
749 for(i=0; i<2; i++){
750 s->last_mv[i][0][0]=
751 s->last_mv[i][0][1]=
752 s->last_mv[i][1][0]=
753 s->last_mv[i][1][1]= 0;
754 }
9dbcbd92 755 }
c5d309f2
MN
756
757 assert(s->dquant>=-2 && s->dquant<=2);
758 assert((s->dquant&1)==0);
759 assert(mb_type>=0);
9dbcbd92
MN
760
761 /* nothing to do if this MB was skiped in the next P Frame */
7bc9090a 762 if(s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]){ //FIXME avoid DCT & ...
9dbcbd92
MN
763 s->skip_count++;
764 s->mv[0][0][0]=
765 s->mv[0][0][1]=
766 s->mv[1][0][0]=
767 s->mv[1][0][1]= 0;
91029be7 768 s->mv_dir= MV_DIR_FORWARD; //doesnt matter
c5d309f2 769 s->qscale -= s->dquant;
1e491e29
MN
770// s->mb_skiped=1;
771
9dbcbd92
MN
772 return;
773 }
1457ab52 774
f2f6134b
MN
775 cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type);
776
9dbcbd92
MN
777 if ((cbp | motion_x | motion_y | mb_type) ==0) {
778 /* direct MB with MV={0,0} */
c5d309f2
MN
779 assert(s->dquant==0);
780
9dbcbd92 781 put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
92073d01
MN
782
783 if(interleaved_stats){
784 s->misc_bits++;
785 s->last_bits++;
786 }
9dbcbd92
MN
787 s->skip_count++;
788 return;
789 }
1e491e29 790
9dbcbd92
MN
791 put_bits(&s->pb, 1, 0); /* mb coded modb1=0 */
792 put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
793 put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we dont need it :)
794 if(cbp) put_bits(&s->pb, 6, cbp);
795
c5d309f2
MN
796 if(cbp && mb_type){
797 if(s->dquant)
798 put_bits(&s->pb, 2, (s->dquant>>2)+3);
799 else
800 put_bits(&s->pb, 1, 0);
801 }else
802 s->qscale -= s->dquant;
fcb48651
MN
803
804 if(!s->progressive_sequence){
805 if(cbp)
806 put_bits(&s->pb, 1, s->interlaced_dct);
807 if(mb_type) // not diect mode
bb198e19 808 put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
fcb48651 809 }
9dbcbd92 810
92073d01 811 if(interleaved_stats){
4d2a4834 812 s->misc_bits+= get_bits_diff(s);
92073d01 813 }
cf8039b2 814
bb198e19
MN
815 if(mb_type == 0){
816 assert(s->mv_dir & MV_DIRECT);
9dbcbd92
MN
817 h263_encode_motion(s, motion_x, 1);
818 h263_encode_motion(s, motion_y, 1);
66370d3f
MN
819 s->b_count++;
820 s->f_count++;
bb198e19
MN
821 }else{
822 assert(mb_type > 0 && mb_type < 4);
823 if(s->mv_type != MV_TYPE_FIELD){
824 if(s->mv_dir & MV_DIR_FORWARD){
825 h263_encode_motion(s, s->mv[0][0][0] - s->last_mv[0][0][0], s->f_code);
826 h263_encode_motion(s, s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
827 s->last_mv[0][0][0]= s->last_mv[0][1][0]= s->mv[0][0][0];
828 s->last_mv[0][0][1]= s->last_mv[0][1][1]= s->mv[0][0][1];
829 s->f_count++;
830 }
831 if(s->mv_dir & MV_DIR_BACKWARD){
832 h263_encode_motion(s, s->mv[1][0][0] - s->last_mv[1][0][0], s->b_code);
833 h263_encode_motion(s, s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
834 s->last_mv[1][0][0]= s->last_mv[1][1][0]= s->mv[1][0][0];
835 s->last_mv[1][0][1]= s->last_mv[1][1][1]= s->mv[1][0][1];
836 s->b_count++;
837 }
838 }else{
839 if(s->mv_dir & MV_DIR_FORWARD){
840 put_bits(&s->pb, 1, s->field_select[0][0]);
841 put_bits(&s->pb, 1, s->field_select[0][1]);
842 }
843 if(s->mv_dir & MV_DIR_BACKWARD){
844 put_bits(&s->pb, 1, s->field_select[1][0]);
845 put_bits(&s->pb, 1, s->field_select[1][1]);
846 }
847 if(s->mv_dir & MV_DIR_FORWARD){
848 for(i=0; i<2; i++){
849 h263_encode_motion(s, s->mv[0][i][0] - s->last_mv[0][i][0] , s->f_code);
850 h263_encode_motion(s, s->mv[0][i][1] - s->last_mv[0][i][1]/2, s->f_code);
851 s->last_mv[0][i][0]= s->mv[0][i][0];
852 s->last_mv[0][i][1]= s->mv[0][i][1]*2;
853 }
854 s->f_count++;
855 }
856 if(s->mv_dir & MV_DIR_BACKWARD){
857 for(i=0; i<2; i++){
858 h263_encode_motion(s, s->mv[1][i][0] - s->last_mv[1][i][0] , s->b_code);
859 h263_encode_motion(s, s->mv[1][i][1] - s->last_mv[1][i][1]/2, s->b_code);
860 s->last_mv[1][i][0]= s->mv[1][i][0];
861 s->last_mv[1][i][1]= s->mv[1][i][1]*2;
862 }
863 s->b_count++;
864 }
865 }
9dbcbd92 866 }
92073d01
MN
867
868 if(interleaved_stats){
4d2a4834 869 s->mv_bits+= get_bits_diff(s);
92073d01 870 }
d6231b9e 871
9dbcbd92
MN
872 /* encode each block */
873 for (i = 0; i < 6; i++) {
2ad1516a 874 mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, NULL, &s->pb);
92073d01
MN
875 }
876
877 if(interleaved_stats){
4d2a4834 878 s->p_tex_bits+= get_bits_diff(s);
9dbcbd92 879 }
bb198e19 880
9dbcbd92 881 }else{ /* s->pict_type==B_TYPE */
f2f6134b
MN
882 cbp= get_p_cbp(s, block, motion_x, motion_y);
883
c5d309f2 884 if ((cbp | motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16) {
ee6f7861
MN
885 /* check if the B frames can skip it too, as we must skip it if we skip here
886 why didnt they just compress the skip-mb bits instead of reusing them ?! */
887 if(s->max_b_frames>0){
888 int i;
e71a4f74
MN
889 int x,y, offset;
890 uint8_t *p_pic;
891
892 x= s->mb_x*16;
893 y= s->mb_y*16;
894 if(x+16 > s->width) x= s->width-16;
895 if(y+16 > s->height) y= s->height-16;
896
897 offset= x + y*s->linesize;
1e491e29 898 p_pic= s->new_picture.data[0] + offset;
e71a4f74 899
ee6f7861
MN
900 s->mb_skiped=1;
901 for(i=0; i<s->max_b_frames; i++){
d697b89b
MN
902 uint8_t *b_pic;
903 int diff;
1e491e29 904 Picture *pic= s->reordered_input_picture[i+1];
d697b89b 905
1e491e29 906 if(pic==NULL || pic->pict_type!=B_TYPE) break;
d697b89b 907
1e491e29 908 b_pic= pic->data[0] + offset + 16; //FIXME +16
bb198e19 909 diff= s->dsp.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
e71a4f74 910 if(diff>s->qscale*70){ //FIXME check that 70 is optimal
ee6f7861
MN
911 s->mb_skiped=0;
912 break;
913 }
914 }
915 }else
916 s->mb_skiped=1;
917
918 if(s->mb_skiped==1){
919 /* skip macroblock */
920 put_bits(&s->pb, 1, 1);
92073d01
MN
921
922 if(interleaved_stats){
923 s->misc_bits++;
924 s->last_bits++;
925 }
ee6f7861 926 s->skip_count++;
6d71b382 927
ee6f7861
MN
928 return;
929 }
9dbcbd92 930 }
e71a4f74 931
9dbcbd92 932 put_bits(&s->pb, 1, 0); /* mb coded */
f7190f73
MN
933 cbpc = cbp & 3;
934 cbpy = cbp >> 2;
935 cbpy ^= 0xf;
9dbcbd92 936 if(s->mv_type==MV_TYPE_16X16){
c5d309f2 937 if(s->dquant) cbpc+= 8;
9dbcbd92
MN
938 put_bits(&s->pb,
939 inter_MCBPC_bits[cbpc],
940 inter_MCBPC_code[cbpc]);
c5d309f2 941
92073d01 942 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
c5d309f2
MN
943 if(s->dquant)
944 put_bits(pb2, 2, dquant_code[s->dquant+2]);
fcb48651
MN
945
946 if(!s->progressive_sequence){
947 if(cbp)
948 put_bits(pb2, 1, s->interlaced_dct);
bb198e19 949 put_bits(pb2, 1, 0);
fcb48651 950 }
9dbcbd92 951
92073d01 952 if(interleaved_stats){
4d2a4834 953 s->misc_bits+= get_bits_diff(s);
92073d01 954 }
d6231b9e 955
9dbcbd92
MN
956 /* motion vectors: 16x16 mode */
957 h263_pred_motion(s, 0, &pred_x, &pred_y);
958
959 h263_encode_motion(s, motion_x - pred_x, s->f_code);
960 h263_encode_motion(s, motion_y - pred_y, s->f_code);
bb198e19
MN
961 }else if(s->mv_type==MV_TYPE_FIELD){
962 if(s->dquant) cbpc+= 8;
963 put_bits(&s->pb,
964 inter_MCBPC_bits[cbpc],
965 inter_MCBPC_code[cbpc]);
966
967 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
968 if(s->dquant)
969 put_bits(pb2, 2, dquant_code[s->dquant+2]);
970
971 assert(!s->progressive_sequence);
972 if(cbp)
973 put_bits(pb2, 1, s->interlaced_dct);
974 put_bits(pb2, 1, 1);
975
976 if(interleaved_stats){
977 s->misc_bits+= get_bits_diff(s);
978 }
979
980 /* motion vectors: 16x8 interlaced mode */
981 h263_pred_motion(s, 0, &pred_x, &pred_y);
982 pred_y /=2;
983
984 put_bits(&s->pb, 1, s->field_select[0][0]);
985 put_bits(&s->pb, 1, s->field_select[0][1]);
986
987 h263_encode_motion(s, s->mv[0][0][0] - pred_x, s->f_code);
988 h263_encode_motion(s, s->mv[0][0][1] - pred_y, s->f_code);
989 h263_encode_motion(s, s->mv[0][1][0] - pred_x, s->f_code);
990 h263_encode_motion(s, s->mv[0][1][1] - pred_y, s->f_code);
9dbcbd92 991 }else{
bb198e19 992 assert(s->mv_type==MV_TYPE_8X8);
9dbcbd92 993 put_bits(&s->pb,
f7190f73
MN
994 inter_MCBPC_bits[cbpc+16],
995 inter_MCBPC_code[cbpc+16]);
92073d01 996 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
9dbcbd92 997
49943a18
MN
998 if(!s->progressive_sequence){
999 if(cbp)
1000 put_bits(pb2, 1, s->interlaced_dct);
1001 }
1002
92073d01 1003 if(interleaved_stats){
4d2a4834 1004 s->misc_bits+= get_bits_diff(s);
92073d01 1005 }
9dbcbd92
MN
1006
1007 for(i=0; i<4; i++){
1008 /* motion vectors: 8x8 mode*/
1009 h263_pred_motion(s, i, &pred_x, &pred_y);
1010
8d7ec294
WH
1011 h263_encode_motion(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x, s->f_code);
1012 h263_encode_motion(s, s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
9dbcbd92 1013 }
cf8039b2 1014 }
92073d01
MN
1015
1016 if(interleaved_stats){
4d2a4834 1017 s->mv_bits+= get_bits_diff(s);
92073d01 1018 }
098eefe1 1019
9dbcbd92
MN
1020 /* encode each block */
1021 for (i = 0; i < 6; i++) {
2ad1516a 1022 mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, NULL, tex_pb);
92073d01
MN
1023 }
1024
1025 if(interleaved_stats){
4d2a4834 1026 s->p_tex_bits+= get_bits_diff(s);
9dbcbd92 1027 }
66370d3f 1028 s->f_count++;
d6231b9e
MN
1029 }
1030 } else {
9dbcbd92 1031 int cbp;
d6231b9e
MN
1032 int dc_diff[6]; //dc values with the dc prediction subtracted
1033 int dir[6]; //prediction direction
1034 int zigzag_last_index[6];
0c1a9eda 1035 uint8_t *scan_table[6];
5c91a675 1036 int i;
d6231b9e
MN
1037
1038 for(i=0; i<6; i++){
1039 const int level= block[i][0];
0c1a9eda 1040 uint16_t *dc_ptr;
d6231b9e 1041
8f8402e4 1042 dc_diff[i]= level - ff_mpeg4_pred_dc(s, i, &dc_ptr, &dir[i]);
d6231b9e
MN
1043 if (i < 4) {
1044 *dc_ptr = level * s->y_dc_scale;
1045 } else {
1046 *dc_ptr = level * s->c_dc_scale;
1047 }
1048 }
1049
8d8c0294
MN
1050 if(s->flags & CODEC_FLAG_AC_PRED){
1051 s->ac_pred= decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
1052 if(!s->ac_pred)
1053 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
d6231b9e
MN
1054 }else{
1055 for(i=0; i<6; i++)
2ad1516a 1056 scan_table[i]= s->intra_scantable.permutated;
d6231b9e
MN
1057 }
1058
1059 /* compute cbp */
1060 cbp = 0;
1061 for (i = 0; i < 6; i++) {
1062 if (s->block_last_index[i] >= 1)
1063 cbp |= 1 << (5 - i);
1064 }
1065
1066 cbpc = cbp & 3;
1067 if (s->pict_type == I_TYPE) {
c5d309f2 1068 if(s->dquant) cbpc+=4;
d6231b9e
MN
1069 put_bits(&s->pb,
1070 intra_MCBPC_bits[cbpc],
1071 intra_MCBPC_code[cbpc]);
1072 } else {
c5d309f2 1073 if(s->dquant) cbpc+=8;
d6231b9e
MN
1074 put_bits(&s->pb, 1, 0); /* mb coded */
1075 put_bits(&s->pb,
1076 inter_MCBPC_bits[cbpc + 4],
1077 inter_MCBPC_code[cbpc + 4]);
1078 }
92073d01 1079 put_bits(pb2, 1, s->ac_pred);
d6231b9e 1080 cbpy = cbp >> 2;
92073d01 1081 put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
c5d309f2
MN
1082 if(s->dquant)
1083 put_bits(dc_pb, 2, dquant_code[s->dquant+2]);
d6231b9e 1084
fcb48651
MN
1085 if(!s->progressive_sequence){
1086 put_bits(dc_pb, 1, s->interlaced_dct);
1087 }
1088
92073d01 1089 if(interleaved_stats){
4d2a4834 1090 s->misc_bits+= get_bits_diff(s);
92073d01 1091 }
098eefe1 1092
d6231b9e
MN
1093 /* encode each block */
1094 for (i = 0; i < 6; i++) {
92073d01 1095 mpeg4_encode_block(s, block[i], i, dc_diff[i], scan_table[i], dc_pb, tex_pb);
d6231b9e
MN
1096 }
1097
92073d01 1098 if(interleaved_stats){
4d2a4834 1099 s->i_tex_bits+= get_bits_diff(s);
92073d01 1100 }
098eefe1
MN
1101 s->i_count++;
1102
d6231b9e 1103 /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
8d8c0294
MN
1104 if(s->ac_pred)
1105 restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
d6231b9e
MN
1106 }
1107}
1108
de6d9b64
FB
1109void h263_encode_mb(MpegEncContext * s,
1110 DCTELEM block[6][64],
1111 int motion_x, int motion_y)
1112{
1113 int cbpc, cbpy, i, cbp, pred_x, pred_y;
0c1a9eda
ZK
1114 int16_t pred_dc;
1115 int16_t rec_intradc[6];
1116 uint16_t *dc_ptr[6];
d4f5d74a 1117 const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
9e4c6948 1118 const int dquant_code[5]= {1,0,9,2,3};
ef5b1b5a
J
1119
1120 //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
9dbcbd92 1121 if (!s->mb_intra) {
ef5b1b5a 1122 /* compute cbp */
f2f6134b
MN
1123 cbp= get_p_cbp(s, block, motion_x, motion_y);
1124
9e4c6948 1125 if ((cbp | motion_x | motion_y | s->dquant) == 0) {
9dbcbd92
MN
1126 /* skip macroblock */
1127 put_bits(&s->pb, 1, 1);
d4f5d74a
GM
1128 if(interleaved_stats){
1129 s->misc_bits++;
1130 s->last_bits++;
1131 }
9dbcbd92
MN
1132 return;
1133 }
ef5b1b5a 1134 put_bits(&s->pb, 1, 0); /* mb coded */
f7190f73 1135
9dbcbd92 1136 cbpc = cbp & 3;
9dbcbd92 1137 cbpy = cbp >> 2;
dba019da
MN
1138 if(s->alt_inter_vlc==0 || cbpc!=3)
1139 cbpy ^= 0xF;
1140 if(s->dquant) cbpc+= 8;
f7190f73
MN
1141 if(s->mv_type==MV_TYPE_16X16){
1142 put_bits(&s->pb,
1143 inter_MCBPC_bits[cbpc],
1144 inter_MCBPC_code[cbpc]);
9dbcbd92 1145
f7190f73
MN
1146 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1147 if(s->dquant)
1148 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
1149
1150 if(interleaved_stats){
1151 s->misc_bits+= get_bits_diff(s);
1152 }
d4f5d74a 1153
f7190f73
MN
1154 /* motion vectors: 16x16 mode */
1155 h263_pred_motion(s, 0, &pred_x, &pred_y);
1156
1157 if (!s->umvplus) {
b40cd4e0
MN
1158 h263_encode_motion(s, motion_x - pred_x, 1);
1159 h263_encode_motion(s, motion_y - pred_y, 1);
f7190f73
MN
1160 }
1161 else {
1162 h263p_encode_umotion(s, motion_x - pred_x);
1163 h263p_encode_umotion(s, motion_y - pred_y);
1164 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
1165 /* To prevent Start Code emulation */
1166 put_bits(&s->pb,1,1);
1167 }
1168 }else{
1169 put_bits(&s->pb,
1170 inter_MCBPC_bits[cbpc+16],
1171 inter_MCBPC_code[cbpc+16]);
1172 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1173 if(s->dquant)
1174 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
1175
1176 if(interleaved_stats){
1177 s->misc_bits+= get_bits_diff(s);
1178 }
1179
1180 for(i=0; i<4; i++){
1181 /* motion vectors: 8x8 mode*/
1182 h263_pred_motion(s, i, &pred_x, &pred_y);
1183
8d7ec294
WH
1184 motion_x= s->current_picture.motion_val[0][ s->block_index[i] ][0];
1185 motion_y= s->current_picture.motion_val[0][ s->block_index[i] ][1];
f7190f73 1186 if (!s->umvplus) {
b40cd4e0
MN
1187 h263_encode_motion(s, motion_x - pred_x, 1);
1188 h263_encode_motion(s, motion_y - pred_y, 1);
f7190f73
MN
1189 }
1190 else {
1191 h263p_encode_umotion(s, motion_x - pred_x);
1192 h263p_encode_umotion(s, motion_y - pred_y);
1193 if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
1194 /* To prevent Start Code emulation */
1195 put_bits(&s->pb,1,1);
1196 }
1197 }
9dbcbd92 1198 }
d4f5d74a
GM
1199
1200 if(interleaved_stats){
1201 s->mv_bits+= get_bits_diff(s);
1202 }
ef5b1b5a 1203 } else {
68b94c35 1204 assert(s->mb_intra);
ef5b1b5a
J
1205
1206 cbp = 0;
fb079883 1207 if (s->h263_aic) {
ef5b1b5a 1208 /* Predict DC */
fb079883 1209 for(i=0; i<6; i++) {
0c1a9eda 1210 int16_t level = block[i][0];
fb079883
MN
1211 int scale;
1212
1213 if(i<4) scale= s->y_dc_scale;
1214 else scale= s->c_dc_scale;
1215
ef5b1b5a
J
1216 pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
1217 level -= pred_dc;
1218 /* Quant */
fb079883
MN
1219 if (level >= 0)
1220 level = (level + (scale>>1))/scale;
ef5b1b5a 1221 else
fb079883 1222 level = (level - (scale>>1))/scale;
ef5b1b5a
J
1223
1224 /* AIC can change CBP */
1225 if (level == 0 && s->block_last_index[i] == 0)
1226 s->block_last_index[i] = -1;
fb079883
MN
1227
1228 if(!s->modified_quant){
1229 if (level < -127)
1230 level = -127;
1231 else if (level > 127)
1232 level = 127;
1233 }
1234
ef5b1b5a
J
1235 block[i][0] = level;
1236 /* Reconstruction */
fb079883 1237 rec_intradc[i] = scale*level + pred_dc;
ef5b1b5a
J
1238 /* Oddify */
1239 rec_intradc[i] |= 1;
1240 //if ((rec_intradc[i] % 2) == 0)
1241 // rec_intradc[i]++;
1242 /* Clipping */
1243 if (rec_intradc[i] < 0)
1244 rec_intradc[i] = 0;
1245 else if (rec_intradc[i] > 2047)
1246 rec_intradc[i] = 2047;
1247
1248 /* Update AC/DC tables */
1249 *dc_ptr[i] = rec_intradc[i];
fb079883
MN
1250 if (s->block_last_index[i] >= 0)
1251 cbp |= 1 << (5 - i);
1252 }
1253 }else{
1254 for(i=0; i<6; i++) {
1255 /* compute cbp */
1256 if (s->block_last_index[i] >= 1)
1257 cbp |= 1 << (5 - i);
ef5b1b5a 1258 }
ef5b1b5a 1259 }
de6d9b64 1260
ef5b1b5a
J
1261 cbpc = cbp & 3;
1262 if (s->pict_type == I_TYPE) {
9e4c6948 1263 if(s->dquant) cbpc+=4;
ef5b1b5a
J
1264 put_bits(&s->pb,
1265 intra_MCBPC_bits[cbpc],
1266 intra_MCBPC_code[cbpc]);
1267 } else {
9e4c6948 1268 if(s->dquant) cbpc+=8;
ef5b1b5a
J
1269 put_bits(&s->pb, 1, 0); /* mb coded */
1270 put_bits(&s->pb,
1271 inter_MCBPC_bits[cbpc + 4],
1272 inter_MCBPC_code[cbpc + 4]);
1273 }
1274 if (s->h263_aic) {
1275 /* XXX: currently, we do not try to use ac prediction */
1276 put_bits(&s->pb, 1, 0); /* no AC prediction */
1277 }
1278 cbpy = cbp >> 2;
1279 put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
9e4c6948
MN
1280 if(s->dquant)
1281 put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
d4f5d74a
GM
1282
1283 if(interleaved_stats){
1284 s->misc_bits+= get_bits_diff(s);
1285 }
de6d9b64
FB
1286 }
1287
ef5b1b5a
J
1288 for(i=0; i<6; i++) {
1289 /* encode each block */
44a2950f 1290 h263_encode_block(s, block[i], i);
ef5b1b5a
J
1291
1292 /* Update INTRADC for decoding */
1293 if (s->h263_aic && s->mb_intra) {
1294 block[i][0] = rec_intradc[i];
1295
1296 }
1297 }
d4f5d74a
GM
1298
1299 if(interleaved_stats){
1300 if (!s->mb_intra) {
1301 s->p_tex_bits+= get_bits_diff(s);
1302 s->f_count++;
1303 }else{
1304 s->i_tex_bits+= get_bits_diff(s);
1305 s->i_count++;
1306 }
1307 }
ef5b1b5a 1308}
bbed9259 1309#endif
ef5b1b5a 1310
cdb4e730 1311void ff_h263_loop_filter(MpegEncContext * s){
332f9ac4
MN
1312 int qp_c;
1313 const int linesize = s->linesize;
1314 const int uvlinesize= s->uvlinesize;
1315 const int xy = s->mb_y * s->mb_stride + s->mb_x;
1316 uint8_t *dest_y = s->dest[0];
1317 uint8_t *dest_cb= s->dest[1];
1318 uint8_t *dest_cr= s->dest[2];
1319
1320// if(s->pict_type==B_TYPE && !s->readable) return;
1321
1322 /*
1323 Diag Top
1324 Left Center
1325 */
1326 if(!IS_SKIP(s->current_picture.mb_type[xy])){
1327 qp_c= s->qscale;
1328 s->dsp.h263_v_loop_filter(dest_y+8*linesize , linesize, qp_c);
1329 s->dsp.h263_v_loop_filter(dest_y+8*linesize+8, linesize, qp_c);
1330 }else
1331 qp_c= 0;
1332
1333 if(s->mb_y){
1334 int qp_dt, qp_t, qp_tc;
1335
1336 if(IS_SKIP(s->current_picture.mb_type[xy-s->mb_stride]))
1337 qp_t=0;
1338 else
1339 qp_t= s->current_picture.qscale_table[xy-s->mb_stride];
1340
1341 if(qp_c)
1342 qp_tc= qp_c;
1343 else
1344 qp_tc= qp_t;
1345
1346 if(qp_tc){
1347 const int chroma_qp= s->chroma_qscale_table[qp_tc];
1348 s->dsp.h263_v_loop_filter(dest_y , linesize, qp_tc);
1349 s->dsp.h263_v_loop_filter(dest_y+8, linesize, qp_tc);
1350
1351 s->dsp.h263_v_loop_filter(dest_cb , uvlinesize, chroma_qp);
1352 s->dsp.h263_v_loop_filter(dest_cr , uvlinesize, chroma_qp);
1353 }
1354
1355 if(qp_t)
1356 s->dsp.h263_h_loop_filter(dest_y-8*linesize+8 , linesize, qp_t);
1357
1358 if(s->mb_x){
1359 if(qp_t || IS_SKIP(s->current_picture.mb_type[xy-1-s->mb_stride]))
1360 qp_dt= qp_t;
1361 else
1362 qp_dt= s->current_picture.qscale_table[xy-1-s->mb_stride];
1363
1364 if(qp_dt){
1365 const int chroma_qp= s->chroma_qscale_table[qp_dt];
1366 s->dsp.h263_h_loop_filter(dest_y -8*linesize , linesize, qp_dt);
1367 s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp);
1368 s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp);
1369 }
1370 }
1371 }
1372
1373 if(qp_c){
1374 s->dsp.h263_h_loop_filter(dest_y +8, linesize, qp_c);
1375 if(s->mb_y + 1 == s->mb_height)
1376 s->dsp.h263_h_loop_filter(dest_y+8*linesize+8, linesize, qp_c);
1377 }
1378
1379 if(s->mb_x){
1380 int qp_lc;
1381 if(qp_c || IS_SKIP(s->current_picture.mb_type[xy-1]))
1382 qp_lc= qp_c;
1383 else
1384 qp_lc= s->current_picture.qscale_table[xy-1];
1385
1386 if(qp_lc){
1387 s->dsp.h263_h_loop_filter(dest_y, linesize, qp_lc);
1388 if(s->mb_y + 1 == s->mb_height){
1389 const int chroma_qp= s->chroma_qscale_table[qp_lc];
1390 s->dsp.h263_h_loop_filter(dest_y +8* linesize, linesize, qp_lc);
1391 s->dsp.h263_h_loop_filter(dest_cb , uvlinesize, chroma_qp);
1392 s->dsp.h263_h_loop_filter(dest_cr , uvlinesize, chroma_qp);
1393 }
1394 }
1395 }
1396}
1397
0c1a9eda 1398static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr)
ef5b1b5a
J
1399{
1400 int x, y, wrap, a, c, pred_dc, scale;
0c1a9eda 1401 int16_t *dc_val, *ac_val;
ef5b1b5a
J
1402
1403 /* find prediction */
1404 if (n < 4) {
1405 x = 2 * s->mb_x + 1 + (n & 1);
1406 y = 2 * s->mb_y + 1 + ((n & 2) >> 1);
1407 wrap = s->mb_width * 2 + 2;
1408 dc_val = s->dc_val[0];
1409 ac_val = s->ac_val[0][0];
1410 scale = s->y_dc_scale;
1411 } else {
1412 x = s->mb_x + 1;
1413 y = s->mb_y + 1;
1414 wrap = s->mb_width + 2;
1415 dc_val = s->dc_val[n - 4 + 1];
1416 ac_val = s->ac_val[n - 4 + 1][0];
1417 scale = s->c_dc_scale;
de6d9b64 1418 }
ef5b1b5a
J
1419 /* B C
1420 * A X
1421 */
1422 a = dc_val[(x - 1) + (y) * wrap];
1423 c = dc_val[(x) + (y - 1) * wrap];
1424
1425 /* No prediction outside GOB boundary */
92073d01 1426 if (s->first_slice_line && ((n < 2) || (n > 3)))
ef5b1b5a
J
1427 c = 1024;
1428 pred_dc = 1024;
1429 /* just DC prediction */
1430 if (a != 1024 && c != 1024)
1431 pred_dc = (a + c) >> 1;
1432 else if (a != 1024)
1433 pred_dc = a;
1434 else
1435 pred_dc = c;
1436
1437 /* we assume pred is positive */
1438 //pred_dc = (pred_dc + (scale >> 1)) / scale;
1439 *dc_val_ptr = &dc_val[x + y * wrap];
1440 return pred_dc;
de6d9b64
FB
1441}
1442
5c91a675 1443static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n)
d140623f 1444{
9e15ad28 1445 int x, y, wrap, a, c, pred_dc, scale, i;
0c1a9eda 1446 int16_t *dc_val, *ac_val, *ac_val1;
d140623f
J
1447
1448 /* find prediction */
1449 if (n < 4) {
1450 x = 2 * s->mb_x + 1 + (n & 1);
68b94c35 1451 y = 2 * s->mb_y + 1 + (n>> 1);
d140623f
J
1452 wrap = s->mb_width * 2 + 2;
1453 dc_val = s->dc_val[0];
9e15ad28 1454 ac_val = s->ac_val[0][0];
d140623f
J
1455 scale = s->y_dc_scale;
1456 } else {
1457 x = s->mb_x + 1;
1458 y = s->mb_y + 1;
1459 wrap = s->mb_width + 2;
1460 dc_val = s->dc_val[n - 4 + 1];
9e15ad28 1461 ac_val = s->ac_val[n - 4 + 1][0];
d140623f
J
1462 scale = s->c_dc_scale;
1463 }
9e15ad28
J
1464
1465 ac_val += ((y) * wrap + (x)) * 16;
1466 ac_val1 = ac_val;
1467
d140623f
J
1468 /* B C
1469 * A X
1470 */
1471 a = dc_val[(x - 1) + (y) * wrap];
1472 c = dc_val[(x) + (y - 1) * wrap];
1473
ef5b1b5a 1474 /* No prediction outside GOB boundary */
68b94c35
MN
1475 if(s->first_slice_line && n!=3){
1476 if(n!=2) c= 1024;
1477 if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
1478 }
1479
d140623f 1480 if (s->ac_pred) {
68b94c35 1481 pred_dc = 1024;
d140623f
J
1482 if (s->h263_aic_dir) {
1483 /* left prediction */
9e15ad28
J
1484 if (a != 1024) {
1485 ac_val -= 16;
1486 for(i=1;i<8;i++) {
b0368839 1487 block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
9e15ad28
J
1488 }
1489 pred_dc = a;
d140623f
J
1490 }
1491 } else {
1492 /* top prediction */
9e15ad28
J
1493 if (c != 1024) {
1494 ac_val -= 16 * wrap;
1495 for(i=1;i<8;i++) {
b0368839 1496 block[s->dsp.idct_permutation[i ]] += ac_val[i + 8];
9e15ad28
J
1497 }
1498 pred_dc = c;
d140623f
J
1499 }
1500 }
9e15ad28
J
1501 } else {
1502 /* just DC prediction */
1503 if (a != 1024 && c != 1024)
1504 pred_dc = (a + c) >> 1;
1505 else if (a != 1024)
1506 pred_dc = a;
1507 else
1508 pred_dc = c;
d140623f 1509 }
9e15ad28
J
1510
1511 /* we assume pred is positive */
1512 block[0]=block[0]*scale + pred_dc;
1513
1514 if (block[0] < 0)
1515 block[0] = 0;
68b94c35
MN
1516 else
1517 block[0] |= 1;
9e15ad28
J
1518
1519 /* Update AC/DC tables */
1520 dc_val[(x) + (y) * wrap] = block[0];
1521
d140623f
J
1522 /* left copy */
1523 for(i=1;i<8;i++)
b0368839 1524 ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
d140623f
J
1525 /* top copy */
1526 for(i=1;i<8;i++)
b0368839 1527 ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
d140623f
J
1528}
1529
0c1a9eda 1530int16_t *h263_pred_motion(MpegEncContext * s, int block,
de6d9b64
FB
1531 int *px, int *py)
1532{
4278e7a6 1533 int xy, wrap;
0c1a9eda 1534 int16_t *A, *B, *C, *mot_val;
49c9325f 1535 static const int off[4]= {2, 1, 1, -1};
de6d9b64 1536
4278e7a6
MN
1537 wrap = s->block_wrap[0];
1538 xy = s->block_index[block];
de6d9b64 1539
8d7ec294 1540 mot_val = s->current_picture.motion_val[0][xy];
de6d9b64 1541
8d7ec294 1542 A = s->current_picture.motion_val[0][xy - 1];
92073d01 1543 /* special case for first (slice) line */
4d2858de 1544 if (s->first_slice_line && block<3) {
92073d01
MN
1545 // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
1546 // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
1547 if(block==0){ //most common case
1548 if(s->mb_x == s->resync_mb_x){ //rare
1549 *px= *py = 0;
e1f03acc 1550 }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
8d7ec294 1551 C = s->current_picture.motion_val[0][xy + off[block] - wrap];
92073d01
MN
1552 if(s->mb_x==0){
1553 *px = C[0];
1554 *py = C[1];
1555 }else{
1556 *px = mid_pred(A[0], 0, C[0]);
1557 *py = mid_pred(A[1], 0, C[1]);
1558 }
1559 }else{
1560 *px = A[0];
1561 *py = A[1];
1562 }
1563 }else if(block==1){
e1f03acc 1564 if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
8d7ec294 1565 C = s->current_picture.motion_val[0][xy + off[block] - wrap];
92073d01
MN
1566 *px = mid_pred(A[0], 0, C[0]);
1567 *py = mid_pred(A[1], 0, C[1]);
1568 }else{
1569 *px = A[0];
1570 *py = A[1];
1571 }
1572 }else{ /* block==2*/
8d7ec294
WH
1573 B = s->current_picture.motion_val[0][xy - wrap];
1574 C = s->current_picture.motion_val[0][xy + off[block] - wrap];
92073d01
MN
1575 if(s->mb_x == s->resync_mb_x) //rare
1576 A[0]=A[1]=0;
1577
1578 *px = mid_pred(A[0], B[0], C[0]);
1579 *py = mid_pred(A[1], B[1], C[1]);
1580 }
de6d9b64 1581 } else {
8d7ec294
WH
1582 B = s->current_picture.motion_val[0][xy - wrap];
1583 C = s->current_picture.motion_val[0][xy + off[block] - wrap];
de6d9b64
FB
1584 *px = mid_pred(A[0], B[0], C[0]);
1585 *py = mid_pred(A[1], B[1], C[1]);
1586 }
1587 return mot_val;
1588}
1589
b40cd4e0
MN
1590// identical to above but with s->current_picture->motion_val, the above one will be removed, and this renamed to it
1591int16_t *h263_pred_motion2(MpegEncContext * s, int block, int dir,
1592 int *px, int *py)
1593{
1594 int xy, wrap;
1595 int16_t *A, *B, *C, (*mot_val)[2];
1596 static const int off[4]= {2, 1, 1, -1};
1597
1598 wrap = s->b8_stride;
8135f8df 1599 xy = 2*(s->mb_x + s->mb_y * wrap);
b40cd4e0 1600
8135f8df 1601 mot_val = s->current_picture.motion_val[dir] + xy;
b40cd4e0
MN
1602
1603 A = mot_val[ - 1];
1604 /* special case for first (slice) line */
1605 if (s->first_slice_line && block<3) {
1606 // we cant just change some MVs to simulate that as we need them for the B frames (and ME)
1607 // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
1608 if(block==0){ //most common case
1609 if(s->mb_x == s->resync_mb_x){ //rare
1610 *px= *py = 0;
1611 }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
1612 C = mot_val[off[block] - wrap];
1613 if(s->mb_x==0){
1614 *px = C[0];
1615 *py = C[1];
1616 }else{
1617 *px = mid_pred(A[0], 0, C[0]);
1618 *py = mid_pred(A[1], 0, C[1]);
1619 }
1620 }else{
1621 *px = A[0];
1622 *py = A[1];
1623 }
1624 }else if(block==1){
1625 if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
1626 C = mot_val[off[block] - wrap];
1627 *px = mid_pred(A[0], 0, C[0]);
1628 *py = mid_pred(A[1], 0, C[1]);
1629 }else{
1630 *px = A[0];
1631 *py = A[1];
1632 }
1633 }else{ /* block==2*/
1634 B = mot_val[ - wrap];
1635 C = mot_val[off[block] - wrap];
1636 if(s->mb_x == s->resync_mb_x) //rare
1637 A[0]=A[1]=0;
1638
1639 *px = mid_pred(A[0], B[0], C[0]);
1640 *py = mid_pred(A[1], B[1], C[1]);
1641 }
1642 } else {
1643 B = mot_val[ - wrap];
1644 C = mot_val[off[block] - wrap];
1645 *px = mid_pred(A[0], B[0], C[0]);
1646 *py = mid_pred(A[1], B[1], C[1]);
1647 }
1648 return *mot_val;
1649}
1650
bbed9259 1651#ifdef CONFIG_ENCODERS
9dbcbd92 1652static void h263_encode_motion(MpegEncContext * s, int val, int f_code)
de6d9b64 1653{
a9c68306 1654 int range, l, bit_size, sign, code, bits;
de6d9b64
FB
1655
1656 if (val == 0) {
1657 /* zero vector */
1658 code = 0;
1659 put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1660 } else {
9dbcbd92 1661 bit_size = f_code - 1;
de6d9b64
FB
1662 range = 1 << bit_size;
1663 /* modulo encoding */
1664 l = range * 32;
a9c68306
MN
1665#if 1
1666 val+= l;
1667 val&= 2*l-1;
1668 val-= l;
1669 sign = val>>31;
1670 val= (val^sign)-sign;
1671 sign&=1;
1672#else
de6d9b64 1673 if (val < -l) {
a9c68306 1674 val += 2*l;
de6d9b64 1675 } else if (val >= l) {
a9c68306 1676 val -= 2*l;
de6d9b64 1677 }
a9c68306 1678
c5d309f2 1679 assert(val>=-l && val<l);
de6d9b64
FB
1680
1681 if (val >= 0) {
de6d9b64
FB
1682 sign = 0;
1683 } else {
1684 val = -val;
de6d9b64
FB
1685 sign = 1;
1686 }
a9c68306 1687#endif
3bf43d42
MN
1688 val--;
1689 code = (val >> bit_size) + 1;
1690 bits = val & (range - 1);
de6d9b64
FB
1691
1692 put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
1693 if (bit_size > 0) {
1694 put_bits(&s->pb, bit_size, bits);
1695 }
1696 }
3a3d3a42 1697
de6d9b64
FB
1698}
1699
6dbd39fe
J
1700/* Encode MV differences on H.263+ with Unrestricted MV mode */
1701static void h263p_encode_umotion(MpegEncContext * s, int val)
1702{
1703 short sval = 0;
1704 short i = 0;
1705 short n_bits = 0;
1706 short temp_val;
1707 int code = 0;
1708 int tcode;
1709
1710 if ( val == 0)
1711 put_bits(&s->pb, 1, 1);
1712 else if (val == 1)
1713 put_bits(&s->pb, 3, 0);
1714 else if (val == -1)
1715 put_bits(&s->pb, 3, 2);
1716 else {
1717
1718 sval = ((val < 0) ? (short)(-val):(short)val);
1719 temp_val = sval;
1720
1721 while (temp_val != 0) {
1722 temp_val = temp_val >> 1;
1723 n_bits++;
1724 }
1725
1726 i = n_bits - 1;
1727 while (i > 0) {
1728 tcode = (sval & (1 << (i-1))) >> (i-1);
1729 tcode = (tcode << 1) | 1;
1730 code = (code << 2) | tcode;
1731 i--;
1732 }
1733 code = ((code << 1) | (val < 0)) << 1;
1734 put_bits(&s->pb, (2*n_bits)+1, code);
1735 //printf("\nVal = %d\tCode = %d", sval, code);
1736 }
1737}
1738
45870f57
MN
1739static void init_mv_penalty_and_fcode(MpegEncContext *s)
1740{
1741 int f_code;
1742 int mv;
bbed9259
MN
1743
1744 if(mv_penalty==NULL)
30952237 1745 mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
bbed9259 1746
45870f57
MN
1747 for(f_code=1; f_code<=MAX_FCODE; f_code++){
1748 for(mv=-MAX_MV; mv<=MAX_MV; mv++){
1749 int len;
1750
1751 if(mv==0) len= mvtab[0][1];
1752 else{
1753 int val, bit_size, range, code;
1754
1755 bit_size = s->f_code - 1;
1756 range = 1 << bit_size;
1757
1758 val=mv;
1759 if (val < 0)
1760 val = -val;
1761 val--;
1762 code = (val >> bit_size) + 1;
1763 if(code<33){
1764 len= mvtab[code][1] + 1 + bit_size;
1765 }else{
1766 len= mvtab[32][1] + 2 + bit_size;
1767 }
1768 }
1769
1770 mv_penalty[f_code][mv+MAX_MV]= len;
1771 }
1772 }
45870f57
MN
1773
1774 for(f_code=MAX_FCODE; f_code>0; f_code--){
1775 for(mv=-(16<<f_code); mv<(16<<f_code); mv++){
1776 fcode_tab[mv+MAX_MV]= f_code;
1777 }
1778 }
9d2a0355
MN
1779
1780 for(mv=0; mv<MAX_MV*2+1; mv++){
1781 umv_fcode_tab[mv]= 1;
1782 }
45870f57 1783}
bbed9259 1784#endif
45870f57 1785
7604246d
WH
1786#ifdef CONFIG_ENCODERS
1787
20695ec9 1788static void init_uni_dc_tab(void)
2a164b25
MN
1789{
1790 int level, uni_code, uni_len;
1791
3bf43d42 1792 for(level=-256; level<256; level++){
2a164b25
MN
1793 int size, v, l;
1794 /* find number of bits */
1795 size = 0;
1796 v = abs(level);
1797 while (v) {
1798 v >>= 1;
1799 size++;
1800 }
1801
1802 if (level < 0)
1803 l= (-level) ^ ((1 << size) - 1);
1804 else
1805 l= level;
1806
1807 /* luminance */
1808 uni_code= DCtab_lum[size][0];
1809 uni_len = DCtab_lum[size][1];
1810
1811 if (size > 0) {
1812 uni_code<<=size; uni_code|=l;
1813 uni_len+=size;
1814 if (size > 8){
1815 uni_code<<=1; uni_code|=1;
1816 uni_len++;
1817 }
1818 }
67725183
MN
1819 uni_DCtab_lum_bits[level+256]= uni_code;
1820 uni_DCtab_lum_len [level+256]= uni_len;
2a164b25
MN
1821
1822 /* chrominance */
1823 uni_code= DCtab_chrom[size][0];
1824 uni_len = DCtab_chrom[size][1];
1825
1826 if (size > 0) {
1827 uni_code<<=size; uni_code|=l;
1828 uni_len+=size;
1829 if (size > 8){
1830 uni_code<<=1; uni_code|=1;
1831 uni_len++;
1832 }
1833 }
67725183
MN
1834 uni_DCtab_chrom_bits[level+256]= uni_code;
1835 uni_DCtab_chrom_len [level+256]= uni_len;
2a164b25
MN
1836
1837 }
1838}
1839
7604246d
WH
1840#endif //CONFIG_ENCODERS
1841
bbed9259 1842#ifdef CONFIG_ENCODERS
0c1a9eda 1843static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
3db48188
MN
1844 int slevel, run, last;
1845
1846 assert(MAX_LEVEL >= 64);
1847 assert(MAX_RUN >= 63);
1848
1849 for(slevel=-64; slevel<64; slevel++){
1850 if(slevel==0) continue;
1851 for(run=0; run<64; run++){
1852 for(last=0; last<=1; last++){
1853 const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
1854 int level= slevel < 0 ? -slevel : slevel;
1855 int sign= slevel < 0 ? 1 : 0;
1856 int bits, len, code;
1857 int level1, run1;
1858
1859 len_tab[index]= 100;
1860
1861 /* ESC0 */
1862 code= get_rl_index(rl, last, run, level);
1863 bits= rl->table_vlc[code][0];
1864 len= rl->table_vlc[code][1];
1865 bits=bits*2+sign; len++;
1866
1867 if(code!=rl->n && len < len_tab[index]){
1868 bits_tab[index]= bits;
1869 len_tab [index]= len;
1870 }
1871#if 1
1872 /* ESC1 */
1873 bits= rl->table_vlc[rl->n][0];
1874 len= rl->table_vlc[rl->n][1];
1875 bits=bits*2; len++; //esc1
1876 level1= level - rl->max_level[last][run];
1877 if(level1>0){
1878 code= get_rl_index(rl, last, run, level1);
1879 bits<<= rl->table_vlc[code][1];
1880 len += rl->table_vlc[code][1];
1881 bits += rl->table_vlc[code][0];
1882 bits=bits*2+sign; len++;
1883
1884 if(code!=rl->n && len < len_tab[index]){
1885 bits_tab[index]= bits;
1886 len_tab [index]= len;
1887 }
1888 }
1889#endif
1890#if 1
1891 /* ESC2 */
1892 bits= rl->table_vlc[rl->n][0];
1893 len= rl->table_vlc[rl->n][1];
1894 bits=bits*4+2; len+=2; //esc2
1895 run1 = run - rl->max_run[last][level] - 1;
1896 if(run1>=0){
1897 code= get_rl_index(rl, last, run1, level);
1898 bits<<= rl->table_vlc[code][1];
1899 len += rl->table_vlc[code][1];
1900 bits += rl->table_vlc[code][0];
1901 bits=bits*2+sign; len++;
1902
1903 if(code!=rl->n && len < len_tab[index]){
1904 bits_tab[index]= bits;
1905 len_tab [index]= len;
1906 }
1907 }
1908#endif
1909 /* ESC3 */
1910 bits= rl->table_vlc[rl->n][0];
1911 len = rl->table_vlc[rl->n][1];
1912 bits=bits*4+3; len+=2; //esc3
1913 bits=bits*2+last; len++;
1914 bits=bits*64+run; len+=6;
1915 bits=bits*2+1; len++; //marker
1916 bits=bits*4096+(slevel&0xfff); len+=12;
1917 bits=bits*2+1; len++; //marker
1918
1919 if(len < len_tab[index]){
1920 bits_tab[index]= bits;
1921 len_tab [index]= len;
1922 }
1923 }
1924 }
1925 }
1926}
1927
45870f57 1928void h263_encode_init(MpegEncContext *s)
de6d9b64
FB
1929{
1930 static int done = 0;
1931
1932 if (!done) {
1933 done = 1;
2a164b25
MN
1934
1935 init_uni_dc_tab();
1936
de6d9b64
FB
1937 init_rl(&rl_inter);
1938 init_rl(&rl_intra);
ef5b1b5a 1939 init_rl(&rl_intra_aic);
3db48188
MN
1940
1941 init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1942 init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
45870f57
MN
1943
1944 init_mv_penalty_and_fcode(s);
de6d9b64 1945 }
1457ab52 1946 s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
45870f57 1947
9d2a0355 1948 // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
d7e9533a
MN
1949 switch(s->codec_id){
1950 case CODEC_ID_MPEG4:
1951 s->fcode_tab= fcode_tab;
1952 s->min_qcoeff= -2048;
1953 s->max_qcoeff= 2047;
477ab036
MN
1954 s->intra_ac_vlc_length = uni_mpeg4_intra_rl_len;
1955 s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
1956 s->inter_ac_vlc_length = uni_mpeg4_inter_rl_len;
1957 s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
67725183
MN
1958 s->luma_dc_vlc_length= uni_DCtab_lum_len;
1959 s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
477ab036 1960 s->ac_esc_length= 7+2+1+6+1+12+1;
22ddd60b
MN
1961
1962 if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
8b76418a 1963
22ddd60b 1964 s->avctx->extradata= av_malloc(1024);
ed7debda 1965 init_put_bits(&s->pb, s->avctx->extradata, 1024);
22ddd60b
MN
1966
1967 mpeg4_encode_visual_object_header(s);
1968 mpeg4_encode_vol_header(s, 0, 0);
1969
1970// ff_mpeg4_stuffing(&s->pb); ?
1971 flush_put_bits(&s->pb);
1972 s->avctx->extradata_size= (get_bit_count(&s->pb)+7)>>3;
1973 }
1974
d7e9533a
MN
1975 break;
1976 case CODEC_ID_H263P:
fb079883
MN
1977 if(s->umvplus)
1978 s->fcode_tab= umv_fcode_tab;
fb079883
MN
1979 if(s->modified_quant){
1980 s->min_qcoeff= -2047;
1981 s->max_qcoeff= 2047;
1982 }else{
1983 s->min_qcoeff= -127;
1984 s->max_qcoeff= 127;
1985 }
d7e9533a 1986 break;
8f8402e4 1987 //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
d4f5d74a
GM
1988 case CODEC_ID_FLV1:
1989 if (s->h263_flv > 1) {
1990 s->min_qcoeff= -1023;
1991 s->max_qcoeff= 1023;
1992 } else {
1993 s->min_qcoeff= -127;
1994 s->max_qcoeff= 127;
1995 }
1996 s->y_dc_scale_table=
1997 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
1998 break;
d7e9533a 1999 default: //nothing needed default table allready set in mpegvideo.c
e51d6d27 2000 s->min_qcoeff= -127;
d7e9533a 2001 s->max_qcoeff= 127;
8f8402e4
MN
2002 s->y_dc_scale_table=
2003 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
d7e9533a 2004 }
de6d9b64
FB
2005}
2006
2ba8f6b8
MN
2007/**
2008 * encodes a 8x8 block.
2009 * @param block the 8x8 block
2010 * @param n block index (0-3 are luma, 4-5 are chroma)
2011 */
de6d9b64
FB
2012static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
2013{
ef5b1b5a
J
2014 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
2015 RLTable *rl;
de6d9b64 2016
ef5b1b5a
J
2017 rl = &rl_inter;
2018 if (s->mb_intra && !s->h263_aic) {
81401c1f 2019 /* DC coef */
e51d6d27 2020 level = block[0];
de6d9b64
FB
2021 /* 255 cannot be represented, so we clamp */
2022 if (level > 254) {
2023 level = 254;
2024 block[0] = 254;
2025 }
81401c1f 2026 /* 0 cannot be represented also */
e51d6d27 2027 else if (level < 1) {
81401c1f
J
2028 level = 1;
2029 block[0] = 1;
2030 }
17938602 2031 if (level == 128) //FIXME check rv10
e51d6d27
MN
2032 put_bits(&s->pb, 8, 0xff);
2033 else
2034 put_bits(&s->pb, 8, level & 0xff);
2035 i = 1;
de6d9b64 2036 } else {
e51d6d27
MN
2037 i = 0;
2038 if (s->h263_aic && s->mb_intra)
2039 rl = &rl_intra_aic;
dba019da
MN
2040
2041 if(s->alt_inter_vlc && !s->mb_intra){
2042 int aic_vlc_bits=0;
2043 int inter_vlc_bits=0;
2044 int wrong_pos=-1;
2045 int aic_code;
2046
2047 last_index = s->block_last_index[n];
2048 last_non_zero = i - 1;
2049 for (; i <= last_index; i++) {
2050 j = s->intra_scantable.permutated[i];
2051 level = block[j];
2052 if (level) {
2053 run = i - last_non_zero - 1;
2054 last = (i == last_index);
ecfc44e6
MN
2055
2056 if(level<0) level= -level;
dba019da
MN
2057
2058 code = get_rl_index(rl, last, run, level);
2059 aic_code = get_rl_index(&rl_intra_aic, last, run, level);
2060 inter_vlc_bits += rl->table_vlc[code][1]+1;
2061 aic_vlc_bits += rl_intra_aic.table_vlc[aic_code][1]+1;
2062
2063 if (code == rl->n) {
ecfc44e6 2064 inter_vlc_bits += 1+6+8-1;
dba019da
MN
2065 }
2066 if (aic_code == rl_intra_aic.n) {
ecfc44e6 2067 aic_vlc_bits += 1+6+8-1;
dba019da
MN
2068 wrong_pos += run + 1;
2069 }else
2070 wrong_pos += wrong_run[aic_code];
2071 last_non_zero = i;
2072 }
2073 }
2074 i = 0;
2075 if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
2076 rl = &rl_intra_aic;
2077 }
de6d9b64 2078 }
ef5b1b5a 2079
de6d9b64
FB
2080 /* AC coefs */
2081 last_index = s->block_last_index[n];
2082 last_non_zero = i - 1;
2083 for (; i <= last_index; i++) {
2ad1516a 2084 j = s->intra_scantable.permutated[i];
ef5b1b5a
J
2085 level = block[j];
2086 if (level) {
2087 run = i - last_non_zero - 1;
2088 last = (i == last_index);
2089 sign = 0;
2090 slevel = level;
2091 if (level < 0) {
2092 sign = 1;
2093 level = -level;
2094 }
de6d9b64
FB
2095 code = get_rl_index(rl, last, run, level);
2096 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2097 if (code == rl->n) {
44a2950f 2098 if(s->h263_flv <= 1){
de6d9b64
FB
2099 put_bits(&s->pb, 1, last);
2100 put_bits(&s->pb, 6, run);
e51d6d27
MN
2101
2102 assert(slevel != 0);
2103
2104 if(slevel < 128 && slevel > -128)
2105 put_bits(&s->pb, 8, slevel & 0xff);
2106 else{
2107 put_bits(&s->pb, 8, 128);
2108 put_bits(&s->pb, 5, slevel & 0x1f);
2109 put_bits(&s->pb, 6, (slevel>>5)&0x3f);
2110 }
44a2950f
MN
2111 }else{
2112 if(slevel < 64 && slevel > -64) {
2113 /* 7-bit level */
2114 put_bits(&s->pb, 1, 0);
2115 put_bits(&s->pb, 1, last);
2116 put_bits(&s->pb, 6, run);
2117
2118 put_bits(&s->pb, 7, slevel & 0x7f);
2119 } else {
2120 /* 11-bit level */
2121 put_bits(&s->pb, 1, 1);
2122 put_bits(&s->pb, 1, last);
2123 put_bits(&s->pb, 6, run);
2124
2125 put_bits(&s->pb, 11, slevel & 0x7ff);
2126 }
2127 }
d4f5d74a
GM
2128 } else {
2129 put_bits(&s->pb, 1, sign);
2130 }
2131 last_non_zero = i;
2132 }
2133 }
2134}
bbed9259 2135#endif
de6d9b64 2136
7604246d
WH
2137#ifdef CONFIG_ENCODERS
2138
de6d9b64 2139/***************************************************/
4d2858de
MN
2140/**
2141 * add mpeg4 stuffing bits (01...1)
2142 */
92073d01 2143void ff_mpeg4_stuffing(PutBitContext * pbc)
6f91bcd1
MN
2144{
2145 int length;
2146 put_bits(pbc, 1, 0);
2147 length= (-get_bit_count(pbc))&7;
92073d01 2148 if(length) put_bits(pbc, length, (1<<length)-1);
6f91bcd1
MN
2149}
2150
91029be7
MN
2151/* must be called before writing the header */
2152void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){
2153 int time_div, time_mod;
2154
5f194811
MN
2155 if(s->current_picture_ptr->pts)
2156 s->time= (s->current_picture_ptr->pts*s->time_increment_resolution + 500*1000)/(1000*1000);
15415af4 2157 else
14bea432 2158 s->time= av_rescale(picture_number*(int64_t)s->avctx->frame_rate_base, s->time_increment_resolution, s->avctx->frame_rate);
91029be7
MN
2159 time_div= s->time/s->time_increment_resolution;
2160 time_mod= s->time%s->time_increment_resolution;
2161
2162 if(s->pict_type==B_TYPE){
bea669e5 2163 s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
91029be7
MN
2164 }else{
2165 s->last_time_base= s->time_base;
2166 s->time_base= time_div;
2167 s->pp_time= s->time - s->last_non_b_time;
2168 s->last_non_b_time= s->time;
2169 }
2170}
2171
41773b73
MN
2172static void mpeg4_encode_gop_header(MpegEncContext * s){
2173 int hours, minutes, seconds;
2174
2175 put_bits(&s->pb, 16, 0);
2176 put_bits(&s->pb, 16, GOP_STARTCODE);
2177
2178 seconds= s->time/s->time_increment_resolution;
2179 minutes= seconds/60; seconds %= 60;
2180 hours= minutes/60; minutes %= 60;
2181 hours%=24;
2182
2183 put_bits(&s->pb, 5, hours);
2184 put_bits(&s->pb, 6, minutes);
2185 put_bits(&s->pb, 1, 1);
2186 put_bits(&s->pb, 6, seconds);
2187
2188 put_bits(&s->pb, 1, 0); //closed gov == NO
2189 put_bits(&s->pb, 1, 0); //broken link == NO
2190
2191 ff_mpeg4_stuffing(&s->pb);
2192}
2193
2194static void mpeg4_encode_visual_object_header(MpegEncContext * s){
2195 int profile_and_level_indication;
2196 int vo_ver_id;
2197
2198 if(s->max_b_frames || s->quarter_sample){
2199 profile_and_level_indication= 0xF1; // adv simple level 1
2200 vo_ver_id= 5;
2201 }else{
2202 profile_and_level_indication= 0x01; // simple level 1
2203 vo_ver_id= 1;
2204 }
2205 //FIXME levels
2206
2207 put_bits(&s->pb, 16, 0);
2208 put_bits(&s->pb, 16, VOS_STARTCODE);
3f6f76af 2209
41773b73 2210 put_bits(&s->pb, 8, profile_and_level_indication);
3f6f76af 2211
41773b73
MN
2212 put_bits(&s->pb, 16, 0);
2213 put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
2214
2215 put_bits(&s->pb, 1, 1);
2216 put_bits(&s->pb, 4, vo_ver_id);
2217 put_bits(&s->pb, 3, 1); //priority
2218
2219 put_bits(&s->pb, 4, 1); //visual obj type== video obj
2220
2221 put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
2222
2223 ff_mpeg4_stuffing(&s->pb);
2224}
2225
2226static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
de6d9b64 2227{
41773b73 2228 int vo_ver_id;
1ff662cc 2229
41773b73
MN
2230 if(s->max_b_frames || s->quarter_sample){
2231 vo_ver_id= 5;
f8af5cb5
MN
2232 s->vo_type= ADV_SIMPLE_VO_TYPE;
2233 }else{
41773b73 2234 vo_ver_id= 1;
f8af5cb5
MN
2235 s->vo_type= SIMPLE_VO_TYPE;
2236 }
1ff662cc 2237
10cd5815 2238 put_bits(&s->pb, 16, 0);
41773b73 2239 put_bits(&s->pb, 16, 0x100 + vo_number); /* video obj */
10cd5815 2240 put_bits(&s->pb, 16, 0);
41773b73 2241 put_bits(&s->pb, 16, 0x120 + vol_number); /* video obj layer */
10cd5815
MN
2242
2243 put_bits(&s->pb, 1, 0); /* random access vol */
1ff662cc 2244 put_bits(&s->pb, 8, s->vo_type); /* video obj type indication */
10cd5815
MN
2245 put_bits(&s->pb, 1, 1); /* is obj layer id= yes */
2246 put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
2247 put_bits(&s->pb, 3, 1); /* is obj layer priority */
5d3cea3a 2248
5ff85f1d 2249 aspect_to_info(s, s->avctx->sample_aspect_ratio);
5d3cea3a
MN
2250
2251 put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
5ff85f1d
MN
2252 if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
2253 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
2254 put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
050fe8ba 2255 }
1ff662cc
MN
2256
2257 if(s->low_delay){
2258 put_bits(&s->pb, 1, 1); /* vol control parameters= yes */
1c2a8c7f 2259 put_bits(&s->pb, 2, 1); /* chroma format YUV 420/YV12 */
1ff662cc
MN
2260 put_bits(&s->pb, 1, s->low_delay);
2261 put_bits(&s->pb, 1, 0); /* vbv parameters= no */
2262 }else{
2263 put_bits(&s->pb, 1, 0); /* vol control parameters= no */
2264 }
2265
10cd5815
MN
2266 put_bits(&s->pb, 2, RECT_SHAPE); /* vol shape= rectangle */
2267 put_bits(&s->pb, 1, 1); /* marker bit */
9dbcbd92 2268
9dbcbd92 2269 put_bits(&s->pb, 16, s->time_increment_resolution);
10cd5815
MN
2270 if (s->time_increment_bits < 1)
2271 s->time_increment_bits = 1;
2272 put_bits(&s->pb, 1, 1); /* marker bit */
2273 put_bits(&s->pb, 1, 0); /* fixed vop rate=no */
2274 put_bits(&s->pb, 1, 1); /* marker bit */
2275 put_bits(&s->pb, 13, s->width); /* vol width */
2276 put_bits(&s->pb, 1, 1); /* marker bit */
2277 put_bits(&s->pb, 13, s->height); /* vol height */
2278 put_bits(&s->pb, 1, 1); /* marker bit */
fcb48651 2279 put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
10cd5815
MN
2280 put_bits(&s->pb, 1, 1); /* obmc disable */
2281 if (vo_ver_id == 1) {
2282 put_bits(&s->pb, 1, s->vol_sprite_usage=0); /* sprite enable */
41773b73 2283 }else{
10cd5815
MN
2284 put_bits(&s->pb, 2, s->vol_sprite_usage=0); /* sprite enable */
2285 }
4d2858de
MN
2286
2287 s->quant_precision=5;
2288 put_bits(&s->pb, 1, 0); /* not 8 bit == false */
87f8cab4 2289 put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
d6eb3c50
MN
2290
2291 if(s->mpeg_quant){
2292 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
2293 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
2294 }
87f8cab4 2295
10cd5815 2296 if (vo_ver_id != 1)
1457ab52 2297 put_bits(&s->pb, 1, s->quarter_sample);
10cd5815 2298 put_bits(&s->pb, 1, 1); /* complexity estimation disable */
92073d01
MN
2299 s->resync_marker= s->rtp_mode;
2300 put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
2301 put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
2302 if(s->data_partitioning){
2303 put_bits(&s->pb, 1, 0); /* no rvlc */
2304 }
2305
10cd5815
MN
2306 if (vo_ver_id != 1){
2307 put_bits(&s->pb, 1, 0); /* newpred */
2308 put_bits(&s->pb, 1, 0); /* reduced res vop */
2309 }
2310 put_bits(&s->pb, 1, 0); /* scalability */
5596c60c 2311
92073d01 2312 ff_mpeg4_stuffing(&s->pb);
49c9325f 2313
5596c60c 2314 /* user data */
b0368839 2315 if(!(s->flags & CODEC_FLAG_BITEXACT)){
5596c60c
MN
2316 put_bits(&s->pb, 16, 0);
2317 put_bits(&s->pb, 16, 0x1B2); /* user_data */
ce2749d2 2318 put_string(&s->pb, LIBAVCODEC_IDENT);
5596c60c
MN
2319 ff_mpeg4_stuffing(&s->pb);
2320 }
10cd5815
MN
2321}
2322
2323/* write mpeg4 VOP header */
2324void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
2325{
9dbcbd92
MN
2326 int time_incr;
2327 int time_div, time_mod;
2328
92073d01 2329 if(s->pict_type==I_TYPE){
70ac76c0 2330 if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
3f6f76af
MN
2331 if(s->strict_std_compliance < 2) //HACK, the reference sw is buggy
2332 mpeg4_encode_visual_object_header(s);
2333 if(s->strict_std_compliance < 2 || picture_number==0) //HACK, the reference sw is buggy
2334 mpeg4_encode_vol_header(s, 0, 0);
41773b73
MN
2335 }
2336 mpeg4_encode_gop_header(s);
92073d01 2337 }
9dbcbd92 2338
4d2858de
MN
2339 s->partitioned_frame= s->data_partitioning && s->pict_type!=B_TYPE;
2340
9dbcbd92
MN
2341//printf("num:%d rate:%d base:%d\n", s->picture_number, s->frame_rate, FRAME_RATE_BASE);
2342
17592475 2343 put_bits(&s->pb, 16, 0); /* vop header */
41773b73 2344 put_bits(&s->pb, 16, VOP_STARTCODE); /* vop header */
de6d9b64 2345 put_bits(&s->pb, 2, s->pict_type - 1); /* pict type: I = 0 , P = 1 */
9dbcbd92 2346
91029be7
MN
2347 time_div= s->time/s->time_increment_resolution;
2348 time_mod= s->time%s->time_increment_resolution;
9dbcbd92
MN
2349 time_incr= time_div - s->last_time_base;
2350 while(time_incr--)
2351 put_bits(&s->pb, 1, 1);
2352
de6d9b64
FB
2353 put_bits(&s->pb, 1, 0);
2354
2355 put_bits(&s->pb, 1, 1); /* marker */
9dbcbd92 2356 put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
de6d9b64
FB
2357 put_bits(&s->pb, 1, 1); /* marker */
2358 put_bits(&s->pb, 1, 1); /* vop coded */
10cd5815
MN
2359 if ( s->pict_type == P_TYPE
2360 || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
de6d9b64
FB
2361 put_bits(&s->pb, 1, s->no_rounding); /* rounding type */
2362 }
2363 put_bits(&s->pb, 3, 0); /* intra dc VLC threshold */
fcb48651 2364 if(!s->progressive_sequence){
2be9f03a 2365 put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
fcb48651
MN
2366 put_bits(&s->pb, 1, s->alternate_scan);
2367 }
10cd5815 2368 //FIXME sprite stuff
de6d9b64
FB
2369
2370 put_bits(&s->pb, 5, s->qscale);
2371
2372 if (s->pict_type != I_TYPE)
2373 put_bits(&s->pb, 3, s->f_code); /* fcode_for */
10cd5815
MN
2374 if (s->pict_type == B_TYPE)
2375 put_bits(&s->pb, 3, s->b_code); /* fcode_back */
de6d9b64 2376 // printf("****frame %d\n", picture_number);
8f8402e4
MN
2377
2378 s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table; //FIXME add short header support
2379 s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
de6d9b64
FB
2380}
2381
7604246d
WH
2382#endif //CONFIG_ENCODERS
2383
4d2858de 2384/**
554daa24 2385 * set qscale and update qscale dependant variables.
4d2858de 2386 */
554daa24 2387void ff_set_qscale(MpegEncContext * s, int qscale)
de6d9b64 2388{
554daa24
MN
2389 if (qscale < 1)
2390 qscale = 1;
2391 else if (qscale > 31)
2392 qscale = 31;
332f9ac4 2393
554daa24
MN
2394 s->qscale = qscale;
2395 s->chroma_qscale= s->chroma_qscale_table[qscale];
5482970b 2396
554daa24 2397 s->y_dc_scale= s->y_dc_scale_table[ qscale ];
332f9ac4 2398 s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
de6d9b64
FB
2399}
2400
2ba8f6b8
MN
2401/**
2402 * predicts the dc.
2403 * @param n block index (0-3 are luma, 4-5 are chroma)
2404 * @param dc_val_ptr a pointer to the dc_val entry for the current MB will be stored here
2405 * @param dir_ptr pointer to an integer where the prediction direction will be stored
2406 * @return the quantized predicted dc
2407 */
0c1a9eda 2408static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr, int *dir_ptr)
de6d9b64 2409{
4278e7a6 2410 int a, b, c, wrap, pred, scale;
0c1a9eda 2411 uint16_t *dc_val;
de6d9b64
FB
2412
2413 /* find prediction */
2414 if (n < 4) {
de6d9b64
FB
2415 scale = s->y_dc_scale;
2416 } else {
de6d9b64
FB
2417 scale = s->c_dc_scale;
2418 }
6d71b382
MN
2419 if(IS_3IV1)
2420 scale= 8;
2421
4278e7a6
MN
2422 wrap= s->block_wrap[n];
2423 dc_val = s->dc_val[0] + s->block_index[n];
de6d9b64
FB
2424
2425 /* B C
2426 * A X
2427 */
4278e7a6
MN
2428 a = dc_val[ - 1];
2429 b = dc_val[ - 1 - wrap];
2430 c = dc_val[ - wrap];
de6d9b64 2431
4d2858de
MN
2432 /* outside slice handling (we cant do that by memset as we need the dc for error resilience) */
2433 if(s->first_slice_line && n!=3){
2434 if(n!=2) b=c= 1024;
2435 if(n!=1 && s->mb_x == s->resync_mb_x) b=a= 1024;
2436 }
2437 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1){
2438 if(n==0 || n==4 || n==5)
2439 b=1024;
2440 }
2441
de6d9b64
FB
2442 if (abs(a - b) < abs(b - c)) {
2443 pred = c;
2444 *dir_ptr = 1; /* top */
2445 } else {
2446 pred = a;
2447 *dir_ptr = 0; /* left */
2448 }
2449 /* we assume pred is positive */
d4961b35 2450 pred = FASTDIV((pred + (scale >> 1)), scale);
de6d9b64
FB
2451
2452 /* prepare address for prediction update */
4278e7a6 2453 *dc_val_ptr = &dc_val[0];
de6d9b64
FB
2454
2455 return pred;
2456}
2457
2ba8f6b8
MN
2458/**
2459 * predicts the ac.
2460 * @param n block index (0-3 are luma, 4-5 are chroma)
2461 * @param dir the ac prediction direction
2462 */
0e15384d 2463void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
de6d9b64
FB
2464 int dir)
2465{
4278e7a6 2466 int i;
0c1a9eda 2467 int16_t *ac_val, *ac_val1;
1e491e29 2468 int8_t * const qscale_table= s->current_picture.qscale_table;
de6d9b64
FB
2469
2470 /* find prediction */
4278e7a6 2471 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
de6d9b64
FB
2472 ac_val1 = ac_val;
2473 if (s->ac_pred) {
2474 if (dir == 0) {
7bc9090a 2475 const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
de6d9b64
FB
2476 /* left prediction */
2477 ac_val -= 16;
3e6a20a0 2478
1e491e29 2479 if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
3e6a20a0
MN
2480 /* same qscale */
2481 for(i=1;i<8;i++) {
b0368839 2482 block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
3e6a20a0
MN
2483 }
2484 }else{
2485 /* different qscale, we must rescale */
2486 for(i=1;i<8;i++) {
b0368839 2487 block[s->dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
3e6a20a0 2488 }
de6d9b64
FB
2489 }
2490 } else {
7bc9090a 2491 const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
de6d9b64 2492 /* top prediction */
4278e7a6 2493 ac_val -= 16 * s->block_wrap[n];
3e6a20a0 2494
1e491e29 2495 if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
3e6a20a0
MN
2496 /* same qscale */
2497 for(i=1;i<8;i++) {
b0368839 2498 block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
3e6a20a0
MN
2499 }
2500 }else{
2501 /* different qscale, we must rescale */
2502 for(i=1;i<8;i++) {
b0368839 2503 block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
3e6a20a0 2504 }
de6d9b64
FB
2505 }
2506 }
2507 }
2508 /* left copy */
2509 for(i=1;i<8;i++)
b0368839 2510 ac_val1[i ] = block[s->dsp.idct_permutation[i<<3]];
d930ef19 2511
de6d9b64
FB
2512 /* top copy */
2513 for(i=1;i<8;i++)
b0368839 2514 ac_val1[8 + i] = block[s->dsp.idct_permutation[i ]];
d930ef19 2515
de6d9b64
FB
2516}
2517
7604246d
WH
2518#ifdef CONFIG_ENCODERS
2519
2ba8f6b8
MN
2520/**
2521 * encodes the dc value.
2522 * @param n block index (0-3 are luma, 4-5 are chroma)
2523 */
92073d01 2524static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
d6231b9e 2525{
2a164b25 2526#if 1
92073d01 2527// if(level<-255 || level>255) printf("dc overflow\n");
2a164b25
MN
2528 level+=256;
2529 if (n < 4) {
2530 /* luminance */
67725183 2531 put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
2a164b25
MN
2532 } else {
2533 /* chrominance */
67725183 2534 put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
2a164b25
MN
2535 }
2536#else
d6231b9e 2537 int size, v;
de6d9b64
FB
2538 /* find number of bits */
2539 size = 0;
2540 v = abs(level);
2541 while (v) {
2542 v >>= 1;
2543 size++;
2544 }
2545
2546 if (n < 4) {
2547 /* luminance */
2548 put_bits(&s->pb, DCtab_lum[size][1], DCtab_lum[size][0]);
2549 } else {
2550 /* chrominance */
2551 put_bits(&s->pb, DCtab_chrom[size][1], DCtab_chrom[size][0]);
2552 }
2553
2554 /* encode remaining bits */
2555 if (size > 0) {
2556 if (level < 0)
2557 level = (-level) ^ ((1 << size) - 1);
2558 put_bits(&s->pb, size, level);
2559 if (size > 8)
2560 put_bits(&s->pb, 1, 1);
2561 }
2a164b25 2562#endif
de6d9b64 2563}
7604246d 2564
2ba8f6b8
MN
2565/**
2566 * encodes a 8x8 block
2567 * @param n block index (0-3 are luma, 4-5 are chroma)
2568 */
3db48188 2569static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
0c1a9eda 2570 uint8_t *scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb)
de6d9b64 2571{
d2975f8d
MN
2572 int i, last_non_zero;
2573#if 0 //variables for the outcommented version
2574 int code, sign, last;
2575#endif
de6d9b64 2576 const RLTable *rl;
0c1a9eda
ZK
2577 uint32_t *bits_tab;
2578 uint8_t *len_tab;
3db48188 2579 const int last_index = s->block_last_index[n];
de6d9b64 2580
3db48188 2581 if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
de6d9b64 2582 /* mpeg4 based DC predictor */
92073d01 2583 mpeg4_encode_dc(dc_pb, intra_dc, n);
3db48188 2584 if(last_index<1) return;
de6d9b64
FB
2585 i = 1;
2586 rl = &rl_intra;
3db48188
MN
2587 bits_tab= uni_mpeg4_intra_rl_bits;
2588 len_tab = uni_mpeg4_intra_rl_len;
de6d9b64 2589 } else {
3db48188 2590 if(last_index<0) return;
de6d9b64
FB
2591 i = 0;
2592 rl = &rl_inter;
3db48188
MN
2593 bits_tab= uni_mpeg4_inter_rl_bits;
2594 len_tab = uni_mpeg4_inter_rl_len;
de6d9b64
FB
2595 }
2596
2597 /* AC coefs */
de6d9b64 2598 last_non_zero = i - 1;
3db48188
MN
2599#if 1
2600 for (; i < last_index; i++) {
2601 int level = block[ scan_table[i] ];
de6d9b64 2602 if (level) {
3db48188
MN
2603 int run = i - last_non_zero - 1;
2604 level+=64;
2605 if((level&(~127)) == 0){
2606 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
2607 put_bits(ac_pb, len_tab[index], bits_tab[index]);
2608 }else{ //ESC3
2609 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(0<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
2610 }
2611 last_non_zero = i;
2612 }
2613 }
2614 /*if(i<=last_index)*/{
2615 int level = block[ scan_table[i] ];
2616 int run = i - last_non_zero - 1;
2617 level+=64;
2618 if((level&(~127)) == 0){
2619 const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
2620 put_bits(ac_pb, len_tab[index], bits_tab[index]);
2621 }else{ //ESC3
2622 put_bits(ac_pb, 7+2+1+6+1+12+1, (3<<23)+(3<<21)+(1<<20)+(run<<14)+(1<<13)+(((level-64)&0xfff)<<1)+1);
2623 }
2624 }
2625#else
2626 for (; i <= last_index; i++) {
2627 const int slevel = block[ scan_table[i] ];
2628 if (slevel) {
2629 int level;
2630 int run = i - last_non_zero - 1;
de6d9b64
FB
2631 last = (i == last_index);
2632 sign = 0;
3db48188 2633 level = slevel;
de6d9b64
FB
2634 if (level < 0) {
2635 sign = 1;
2636 level = -level;
2637 }
2638 code = get_rl_index(rl, last, run, level);
92073d01 2639 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
de6d9b64
FB
2640 if (code == rl->n) {
2641 int level1, run1;
2642 level1 = level - rl->max_level[last][run];
2643 if (level1 < 1)
2644 goto esc2;
2645 code = get_rl_index(rl, last, run, level1);
2646 if (code == rl->n) {
2647 esc2:
92073d01 2648 put_bits(ac_pb, 1, 1);
de6d9b64
FB
2649 if (level > MAX_LEVEL)
2650 goto esc3;
2651 run1 = run - rl->max_run[last][level] - 1;
2652 if (run1 < 0)
2653 goto esc3;
2654 code = get_rl_index(rl, last, run1, level);
2655 if (code == rl->n) {
2656 esc3:
2657 /* third escape */
92073d01
MN
2658 put_bits(ac_pb, 1, 1);
2659 put_bits(ac_pb, 1, last);
2660 put_bits(ac_pb, 6, run);
2661 put_bits(ac_pb, 1, 1);
2662 put_bits(ac_pb, 12, slevel & 0xfff);
2663 put_bits(ac_pb, 1, 1);
de6d9b64
FB
2664 } else {
2665 /* second escape */
92073d01
MN
2666 put_bits(ac_pb, 1, 0);
2667 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2668 put_bits(ac_pb, 1, sign);
de6d9b64
FB
2669 }
2670 } else {
2671 /* first escape */
92073d01
MN
2672 put_bits(ac_pb, 1, 0);
2673 put_bits(ac_pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2674 put_bits(ac_pb, 1, sign);
de6d9b64
FB
2675 }
2676 } else {
92073d01 2677 put_bits(ac_pb, 1, sign);
de6d9b64
FB
2678 }
2679 last_non_zero = i;
2680 }
2681 }
3db48188 2682#endif
de6d9b64 2683}
1457ab52
MN
2684
2685static inline int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
0c1a9eda 2686 uint8_t *scan_table)
1457ab52
MN
2687{
2688 int i, last_non_zero;
2689 const RLTable *rl;
0c1a9eda 2690 uint8_t *len_tab;
1457ab52
MN
2691 const int last_index = s->block_last_index[n];
2692 int len=0;
2693
2694 if (s->mb_intra) { //Note gcc (3.2.1 at least) will optimize this away
2695 /* mpeg4 based DC predictor */
2696 //mpeg4_encode_dc(dc_pb, intra_dc, n); //FIXME
2697 if(last_index<1) return len;
2698 i = 1;
2699 rl = &rl_intra;
2700 len_tab = uni_mpeg4_intra_rl_len;
2701 } else {
2702 if(last_index<0) return 0;
2703 i = 0;
2704 rl = &rl_inter;
2705 len_tab = uni_mpeg4_inter_rl_len;
2706 }
2707
2708 /* AC coefs */
2709 last_non_zero = i - 1;
2710 for (; i < last_index; i++) {
2711 int level = block[ scan_table[i] ];
2712 if (level) {
2713 int run = i - last_non_zero - 1;
2714 level+=64;
2715 if((level&(~127)) == 0){
2716 const int index= UNI_MPEG4_ENC_INDEX(0, run, level);
2717 len += len_tab[index];
2718 }else{ //ESC3
2719 len += 7+2+1+6+1+12+1;
2720 }
2721 last_non_zero = i;
2722 }
2723 }
2724 /*if(i<=last_index)*/{
2725 int level = block[ scan_table[i] ];
2726 int run = i - last_non_zero - 1;
2727 level+=64;
2728 if((level&(~127)) == 0){
2729 const int index= UNI_MPEG4_ENC_INDEX(1, run, level);
2730 len += len_tab[index];
2731 }else{ //ESC3
2732 len += 7+2+1+6+1+12+1;
2733 }
2734 }
2735
2736 return len;
2737}
2738
bbed9259 2739#endif
de6d9b64
FB
2740
2741
2742/***********************************************/
2743/* decoding */
2744
2745static VLC intra_MCBPC_vlc;
2746static VLC inter_MCBPC_vlc;
2747static VLC cbpy_vlc;
2748static VLC mv_vlc;
2749static VLC dc_lum, dc_chrom;
73c8e514 2750static VLC sprite_trajectory;
6f91bcd1 2751static VLC mb_type_b_vlc;
b40cd4e0
MN
2752static VLC h263_mbtype_b_vlc;
2753static VLC cbpc_b_vlc;
de6d9b64 2754
de6d9b64
FB
2755void init_vlc_rl(RLTable *rl)
2756{
45a82edb
MN
2757 int i, q;
2758
de6d9b64
FB
2759 init_vlc(&rl->vlc, 9, rl->n + 1,
2760 &rl->table_vlc[0][1], 4, 2,
2761 &rl->table_vlc[0][0], 4, 2);
45a82edb
MN
2762
2763
2764 for(q=0; q<32; q++){
2765 int qmul= q*2;
2766 int qadd= (q-1)|1;
2767
2768 if(q==0){
2769 qmul=1;
2770 qadd=0;
2771 }
2772
2773 rl->rl_vlc[q]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
2774 for(i=0; i<rl->vlc.table_size; i++){
2775 int code= rl->vlc.table[i][0];
2776 int len = rl->vlc.table[i][1];
2777 int level, run;
2778
2779 if(len==0){ // illegal code
1a013246 2780 run= 66;
45a82edb
MN
2781 level= MAX_LEVEL;
2782 }else if(len<0){ //more bits needed
2783 run= 0;
2784 level= code;
2785 }else{
2786 if(code==rl->n){ //esc
1a013246 2787 run= 66;
45a82edb
MN
2788 level= 0;
2789 }else{
2790 run= rl->table_run [code] + 1;
2791 level= rl->table_level[code] * qmul + qadd;
2792 if(code >= rl->last) run+=192;
2793 }
2794 }
2795 rl->rl_vlc[q][i].len= len;
2796 rl->rl_vlc[q][i].level= level;
2797 rl->rl_vlc[q][i].run= run;
2798 }
2799 }
de6d9b64
FB
2800}
2801
2802/* init vlcs */
2803
2804/* XXX: find a better solution to handle static init */
2805void h263_decode_init_vlc(MpegEncContext *s)
2806{
2807 static int done = 0;
2808
2809 if (!done) {
2810 done = 1;
2811
7af75e6c 2812 init_vlc(&intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 9,
de6d9b64
FB
2813 intra_MCBPC_bits, 1, 1,
2814 intra_MCBPC_code, 1, 1);
7af75e6c 2815 init_vlc(&inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 28,
de6d9b64
FB
2816 inter_MCBPC_bits, 1, 1,
2817 inter_MCBPC_code, 1, 1);
edcf9890 2818 init_vlc(&cbpy_vlc, CBPY_VLC_BITS, 16,
de6d9b64
FB
2819 &cbpy_tab[0][1], 2, 1,
2820 &cbpy_tab[0][0], 2, 1);
edcf9890 2821 init_vlc(&mv_vlc, MV_VLC_BITS, 33,
de6d9b64
FB
2822 &mvtab[0][1], 2, 1,
2823 &mvtab[0][0], 2, 1);
2824 init_rl(&rl_inter);
2825 init_rl(&rl_intra);
a4e8b587
MN
2826 init_rl(&rvlc_rl_inter);
2827 init_rl(&rvlc_rl_intra);
d140623f 2828 init_rl(&rl_intra_aic);
de6d9b64
FB
2829 init_vlc_rl(&rl_inter);
2830 init_vlc_rl(&rl_intra);
a4e8b587
MN
2831 init_vlc_rl(&rvlc_rl_inter);
2832 init_vlc_rl(&rvlc_rl_intra);
d140623f 2833 init_vlc_rl(&rl_intra_aic);
c03a7172 2834 init_vlc(&dc_lum, DC_VLC_BITS, 10 /* 13 */,
de6d9b64
FB
2835 &DCtab_lum[0][1], 2, 1,
2836 &DCtab_lum[0][0], 2, 1);
c03a7172 2837 init_vlc(&dc_chrom, DC_VLC_BITS, 10 /* 13 */,
de6d9b64
FB
2838 &DCtab_chrom[0][1], 2, 1,
2839 &DCtab_chrom[0][0], 2, 1);
edcf9890 2840 init_vlc(&sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15,
73c8e514
MN
2841 &sprite_trajectory_tab[0][1], 4, 2,
2842 &sprite_trajectory_tab[0][0], 4, 2);
edcf9890 2843 init_vlc(&mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4,
6f91bcd1
MN
2844 &mb_type_b_tab[0][1], 2, 1,
2845 &mb_type_b_tab[0][0], 2, 1);
b40cd4e0
MN
2846 init_vlc(&h263_mbtype_b_vlc, H263_MBTYPE_B_VLC_BITS, 15,
2847 &h263_mbtype_b_tab[0][1], 2, 1,
2848 &h263_mbtype_b_tab[0][0], 2, 1);
2849 init_vlc(&cbpc_b_vlc, CBPC_B_VLC_BITS, 4,
2850 &cbpc_b_tab[0][1], 2, 1,
2851 &cbpc_b_tab[0][0], 2, 1);
de6d9b64 2852 }
4d2858de 2853}
fd7db0fd 2854
4d2858de
MN
2855/**
2856 * Get the GOB height based on picture height.
2857 */
2858int ff_h263_get_gob_height(MpegEncContext *s){
2859 if (s->height <= 400)
2860 return 1;
2861 else if (s->height <= 800)
2862 return 2;
2863 else
2864 return 4;
de6d9b64
FB
2865}
2866
ba58dabc
MN
2867int ff_h263_decode_mba(MpegEncContext *s)
2868{
2869 int i, mb_pos;
2870
2871 for(i=0; i<6; i++){
0ed714a7 2872 if(s->mb_num-1 <= ff_mba_max[i]) break;
ba58dabc
MN
2873 }
2874 mb_pos= get_bits(&s->gb, ff_mba_length[i]);
2875 s->mb_x= mb_pos % s->mb_width;
2876 s->mb_y= mb_pos / s->mb_width;
2877
2878 return mb_pos;
2879}
2880
2881void ff_h263_encode_mba(MpegEncContext *s)
2882{
2883 int i, mb_pos;
2884
2885 for(i=0; i<6; i++){
0ed714a7 2886 if(s->mb_num-1 <= ff_mba_max[i]) break;
ba58dabc
MN
2887 }
2888 mb_pos= s->mb_x + s->mb_width*s->mb_y;
2889 put_bits(&s->pb, ff_mba_length[i], mb_pos);
2890}
2891
4d2858de 2892/**
ba58dabc 2893 * decodes the group of blocks header or slice header.
4d2858de
MN
2894 * @return <0 if an error occured
2895 */
2896static int h263_decode_gob_header(MpegEncContext *s)
de6d9b64 2897{
ba58dabc 2898 unsigned int val, gfid, gob_number;
4d2858de 2899 int left;
102d3908 2900
4949028f 2901 /* Check for GOB Start Code */
644d98a4 2902 val = show_bits(&s->gb, 16);
4d2858de
MN
2903 if(val)
2904 return -1;
2905
644d98a4 2906 /* We have a GBSC probably with GSTUFF */
4d2858de 2907 skip_bits(&s->gb, 16); /* Drop the zeros */
68f593b4 2908 left= s->gb.size_in_bits - get_bits_count(&s->gb);
4d2858de
MN
2909 //MN: we must check the bits left or we might end in a infinite loop (or segfault)
2910 for(;left>13; left--){
2911 if(get_bits1(&s->gb)) break; /* Seek the '1' bit */
2912 }
2913 if(left<=13)
2914 return -1;
2915
ba58dabc
MN
2916 if(s->h263_slice_structured){
2917 if(get_bits1(&s->gb)==0)
2918 return -1;
332f9ac4 2919
ba58dabc
MN
2920 ff_h263_decode_mba(s);
2921
2922 if(s->mb_num > 1583)
2923 if(get_bits1(&s->gb)==0)
2924 return -1;
2925
2926 s->qscale = get_bits(&s->gb, 5); /* SQUANT */
2927 if(get_bits1(&s->gb)==0)
2928 return -1;
2929 gfid = get_bits(&s->gb, 2); /* GFID */
2930 }else{
2931 gob_number = get_bits(&s->gb, 5); /* GN */
2932 s->mb_x= 0;
2933 s->mb_y= s->gob_index* gob_number;
2934 gfid = get_bits(&s->gb, 2); /* GFID */
2935 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
2936 }
2937
ffba1dc0
MN
2938 if(s->mb_y >= s->mb_height)
2939 return -1;
ba58dabc
MN
2940
2941 if(s->qscale==0)
2942 return -1;
2943
644d98a4 2944 return 0;
644d98a4
J
2945}
2946
49092244
MN
2947static inline void memsetw(short *tab, int val, int n)
2948{
2949 int i;
2950 for(i=0;i<n;i++)
2951 tab[i] = val;
2952}
2953
7604246d
WH
2954#ifdef CONFIG_ENCODERS
2955
92073d01
MN
2956void ff_mpeg4_init_partitions(MpegEncContext *s)
2957{
ed7debda
AB
2958 init_put_bits(&s->tex_pb, s->tex_pb_buffer, PB_BUFFER_SIZE);
2959 init_put_bits(&s->pb2 , s->pb2_buffer , PB_BUFFER_SIZE);
92073d01
MN
2960}
2961
2962void ff_mpeg4_merge_partitions(MpegEncContext *s)
2963{
2964 const int pb2_len = get_bit_count(&s->pb2 );
2965 const int tex_pb_len= get_bit_count(&s->tex_pb);
2966 const int bits= get_bit_count(&s->pb);
2967
2968 if(s->pict_type==I_TYPE){
2969 put_bits(&s->pb, 19, DC_MARKER);
2970 s->misc_bits+=19 + pb2_len + bits - s->last_bits;
2971 s->i_tex_bits+= tex_pb_len;
2972 }else{
2973 put_bits(&s->pb, 17, MOTION_MARKER);
4d2858de 2974 s->misc_bits+=17 + pb2_len;
92073d01
MN
2975 s->mv_bits+= bits - s->last_bits;
2976 s->p_tex_bits+= tex_pb_len;
2977 }
2978
2979 flush_put_bits(&s->pb2);
2980 flush_put_bits(&s->tex_pb);
2981
2982 ff_copy_bits(&s->pb, s->pb2_buffer , pb2_len);
2983 ff_copy_bits(&s->pb, s->tex_pb_buffer, tex_pb_len);
2984 s->last_bits= get_bit_count(&s->pb);
2985}
2986
7604246d
WH
2987#endif //CONFIG_ENCODERS
2988
4d2858de
MN
2989int ff_mpeg4_get_video_packet_prefix_length(MpegEncContext *s){
2990 switch(s->pict_type){
2991 case I_TYPE:
2992 return 16;
2993 case P_TYPE:
2994 case S_TYPE:
2995 return s->f_code+15;
2996 case B_TYPE:
b8a78f41 2997 return FFMAX(FFMAX(s->f_code, s->b_code)+15, 17);
4d2858de
MN
2998 default:
2999 return -1;
3000 }
3001}
3002
7604246d
WH
3003#ifdef CONFIG_ENCODERS
3004
92073d01 3005void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
49092244 3006{
49092244 3007 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
49092244 3008
4d2858de 3009 put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
92073d01
MN
3010 put_bits(&s->pb, 1, 1);
3011
3012 put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y*s->mb_width);
4d2858de 3013 put_bits(&s->pb, s->quant_precision, s->qscale);
92073d01
MN
3014 put_bits(&s->pb, 1, 0); /* no HEC */
3015}
49092244 3016
7604246d
WH
3017#endif //CONFIG_ENCODERS
3018
92073d01 3019/**
4d2858de
MN
3020 * check if the next stuff is a resync marker or the end.
3021 * @return 0 if not
92073d01 3022 */
4d2858de
MN
3023static inline int mpeg4_is_resync(MpegEncContext *s){
3024 const int bits_count= get_bits_count(&s->gb);
3025
3026 if(s->workaround_bugs&FF_BUG_NO_PADDING){
3027 return 0;
3028 }
3029
68f593b4 3030 if(bits_count + 8 >= s->gb.size_in_bits){
4d2858de
MN
3031 int v= show_bits(&s->gb, 8);
3032 v|= 0x7F >> (7-(bits_count&7));
3033
3034 if(v==0x7F)
3035 return 1;
3036 }else{
3037 if(show_bits(&s->gb, 16) == ff_mpeg4_resync_prefix[bits_count&7]){
3038 int len;
3039 GetBitContext gb= s->gb;
3040
3041 skip_bits(&s->gb, 1);
3042 align_get_bits(&s->gb);
3043
3044 for(len=0; len<32; len++){
3045 if(get_bits1(&s->gb)) break;
3046 }
3047
3048 s->gb= gb;
3049
3050 if(len>=ff_mpeg4_get_video_packet_prefix_length(s))
3051 return 1;
3052 }
3053 }
3054 return 0;
3055}
3056
3057/**
3058 * decodes the next video packet.
3059 * @return <0 if something went wrong
3060 */
3061static int mpeg4_decode_video_packet_header(MpegEncContext *s)
92073d01 3062{
92073d01 3063 int mb_num_bits= av_log2(s->mb_num - 1) + 1;
4d2858de 3064 int header_extension=0, mb_num, len;
92073d01
MN
3065
3066 /* is there enough space left for a video packet + header */
68f593b4 3067 if( get_bits_count(&s->gb) > s->gb.size_in_bits-20) return -1;
92073d01 3068
4d2858de
MN
3069 for(len=0; len<32; len++){
3070 if(get_bits1(&s->gb)) break;
49092244 3071 }
92073d01 3072
4d2858de 3073 if(len!=ff_mpeg4_get_video_packet_prefix_length(s)){
9b879566 3074 av_log(s->avctx, AV_LOG_ERROR, "marker does not match f_code\n");
92073d01
MN
3075 return -1;
3076 }
4d2858de 3077
49092244 3078 if(s->shape != RECT_SHAPE){
4d2858de 3079 header_extension= get_bits1(&s->gb);
49092244
MN
3080 //FIXME more stuff here
3081 }
3082
4d2858de
MN
3083 mb_num= get_bits(&s->gb, mb_num_bits);
3084 if(mb_num>=s->mb_num){
9b879566 3085 av_log(s->avctx, AV_LOG_ERROR, "illegal mb_num in video packet (%d %d) \n", mb_num, s->mb_num);
92073d01
MN
3086 return -1;
3087 }
84876d36 3088 if(s->pict_type == B_TYPE){
7bc9090a 3089 while(s->next_picture.mbskip_table[ s->mb_index2xy[ mb_num ] ]) mb_num++;
84876d36
MN
3090 if(mb_num >= s->mb_num) return -1; // slice contains just skiped MBs which where allready decoded
3091 }
3092
4d2858de
MN
3093 s->mb_x= mb_num % s->mb_width;
3094 s->mb_y= mb_num / s->mb_width;
49092244
MN
3095
3096 if(s->shape != BIN_ONLY_SHAPE){
4d2858de
MN
3097 int qscale= get_bits(&s->gb, s->quant_precision);
3098 if(qscale)
332f9ac4 3099 s->chroma_qscale=s->qscale= qscale;
49092244
MN
3100 }
3101
3102 if(s->shape == RECT_SHAPE){
4d2858de 3103 header_extension= get_bits1(&s->gb);
49092244
MN
3104 }
3105 if(header_extension){
92073d01 3106 int time_increment;
49092244 3107 int time_incr=0;
92073d01 3108
4d2858de 3109 while (get_bits1(&s->gb) != 0)
49092244
MN
3110 time_incr++;
3111
4d2858de
MN
3112 check_marker(&s->gb, "before time_increment in video packed header");
3113 time_increment= get_bits(&s->gb, s->time_increment_bits);
3114 check_marker(&s->gb, "before vop_coding_type in video packed header");
49092244 3115
4d2858de 3116 skip_bits(&s->gb, 2); /* vop coding type */
49092244
MN
3117 //FIXME not rect stuff here
3118
3119 if(s->shape != BIN_ONLY_SHAPE){
4d2858de
MN
3120 skip_bits(&s->gb, 3); /* intra dc vlc threshold */
3121//FIXME dont just ignore everything
f56dd362 3122 if(s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
49092244 3123 mpeg4_decode_sprite_trajectory(s);
9b879566 3124 av_log(s->avctx, AV_LOG_ERROR, "untested\n");
49092244
MN
3125 }
3126
3127 //FIXME reduced res stuff here
3128
3129 if (s->pict_type != I_TYPE) {
4d2858de 3130 int f_code = get_bits(&s->gb, 3); /* fcode_for */
f3cfadd5 3131 if(f_code==0){
9b879566 3132 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (f_code=0)\n");
49092244
MN
3133 }
3134 }
3135 if (s->pict_type == B_TYPE) {
4d2858de 3136 int b_code = get_bits(&s->gb, 3);
f3cfadd5 3137 if(b_code==0){
9b879566 3138 av_log(s->avctx, AV_LOG_ERROR, "Error, video packet header damaged (b_code=0)\n");
f3cfadd5 3139 }
49092244
MN
3140 }
3141 }
49092244
MN
3142 }
3143 //FIXME new-pred stuff
92073d01
MN
3144
3145//printf("parse ok %d %d %d %d\n", mb_num, s->mb_x + s->mb_y*s->mb_width, get_bits_count(gb), get_bits_count(&s->gb));
3146
4d2858de 3147 return 0;
92073d01
MN
3148}
3149
3150void ff_mpeg4_clean_buffers(MpegEncContext *s)
3151{
3152 int c_wrap, c_xy, l_wrap, l_xy;
49092244
MN
3153
3154 l_wrap= s->block_wrap[0];
92073d01 3155 l_xy= s->mb_y*l_wrap*2 + s->mb_x*2;
49092244 3156 c_wrap= s->block_wrap[4];
92073d01 3157 c_xy= s->mb_y*c_wrap + s->mb_x;
49092244 3158
4d2858de 3159#if 0
49092244 3160 /* clean DC */
92073d01
MN
3161 memsetw(s->dc_val[0] + l_xy, 1024, l_wrap*2+1);
3162 memsetw(s->dc_val[1] + c_xy, 1024, c_wrap+1);
3163 memsetw(s->dc_val[2] + c_xy, 1024, c_wrap+1);
4d2858de 3164#endif
49092244
MN
3165
3166 /* clean AC */
0c1a9eda
ZK
3167 memset(s->ac_val[0] + l_xy, 0, (l_wrap*2+1)*16*sizeof(int16_t));
3168 memset(s->ac_val[1] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t));
3169 memset(s->ac_val[2] + c_xy, 0, (c_wrap +1)*16*sizeof(int16_t));
49092244
MN
3170
3171 /* clean MV */
92073d01 3172 // we cant clear the MVs as they might be needed by a b frame
0c1a9eda
ZK
3173// memset(s->motion_val + l_xy, 0, (l_wrap*2+1)*2*sizeof(int16_t));
3174// memset(s->motion_val, 0, 2*sizeof(int16_t)*(2 + s->mb_width*2)*(2 + s->mb_height*2));
92073d01
MN
3175 s->last_mv[0][0][0]=
3176 s->last_mv[0][0][1]=
3177 s->last_mv[1][0][0]=
3178 s->last_mv[1][0][1]= 0;
3179}
3180
4d2858de
MN
3181/**
3182 * decodes the group of blocks / video packet header.
3183 * @return <0 if no resync found
3184 */
3185int ff_h263_resync(MpegEncContext *s){
3186 int left, ret;
92073d01 3187
bba3a09b 3188 if(s->codec_id==CODEC_ID_MPEG4){
4d2858de 3189 skip_bits1(&s->gb);
bba3a09b
MN
3190 align_get_bits(&s->gb);
3191 }
92073d01 3192
4d2858de
MN
3193 if(show_bits(&s->gb, 16)==0){
3194 if(s->codec_id==CODEC_ID_MPEG4)
3195 ret= mpeg4_decode_video_packet_header(s);
3196 else
3197 ret= h263_decode_gob_header(s);
3198 if(ret>=0)
3199 return 0;
92073d01 3200 }
4d2858de
MN
3201 //ok, its not where its supposed to be ...
3202 s->gb= s->last_resync_gb;
3203 align_get_bits(&s->gb);
68f593b4 3204 left= s->gb.size_in_bits - get_bits_count(&s->gb);
92073d01 3205
4d2858de
MN
3206 for(;left>16+1+5+5; left-=8){
3207 if(show_bits(&s->gb, 16)==0){
3208 GetBitContext bak= s->gb;
92073d01 3209
4d2858de
MN
3210 if(s->codec_id==CODEC_ID_MPEG4)
3211 ret= mpeg4_decode_video_packet_header(s);
3212 else
3213 ret= h263_decode_gob_header(s);
3214 if(ret>=0)
3215 return 0;
92073d01 3216
4d2858de
MN
3217 s->gb= bak;
3218 }
3219 skip_bits(&s->gb, 8);
3220 }
feca25cc 3221
4d2858de 3222 return -1;
92073d01
MN
3223}
3224
3225/**
2ba8f6b8 3226 * gets the average motion vector for a GMC MB.
073b013d
MN
3227 * @param n either 0 for the x component or 1 for y
3228 * @returns the average MV for a GMC MB
3229 */
3230static inline int get_amv(MpegEncContext *s, int n){
3231 int x, y, mb_v, sum, dx, dy, shift;
3232 int len = 1 << (s->f_code + 4);
3233 const int a= s->sprite_warping_accuracy;
3234
3235 if(s->real_sprite_warping_points==1){
3236 if(s->divx_version==500 && s->divx_build==413)
3237 sum= s->sprite_offset[0][n] / (1<<(a - s->quarter_sample));
3238 else
3239 sum= RSHIFT(s->sprite_offset[0][n]<<s->quarter_sample, a);
3240 }else{
3241 dx= s->sprite_delta[n][0];
3242 dy= s->sprite_delta[n][1];
3243 shift= s->sprite_shift[0];
3244 if(n) dy -= 1<<(shift + a + 1);
3245 else dx -= 1<<(shift + a + 1);
3246 mb_v= s->sprite_offset[0][n] + dx*s->mb_x*16 + dy*s->mb_y*16;
3247
3248 sum=0;
3249 for(y=0; y<16; y++){
3250 int v;
3251
3252 v= mb_v + dy*y;
3253 //XXX FIXME optimize
3254 for(x=0; x<16; x++){
3255 sum+= v>>shift;
3256 v+= dx;
3257 }
3258 }
92148550 3259 sum= RSHIFT(sum, a+8-s->quarter_sample);
073b013d
MN
3260 }
3261
3262 if (sum < -len) sum= -len;
3263 else if (sum >= len) sum= len-1;
3264
3265 return sum;
3266}
3267
3268/**
4d2858de
MN
3269 * decodes first partition.
3270 * @return number of MBs decoded or <0 if an error occured
92073d01 3271 */
4d2858de 3272static int mpeg4_decode_partition_a(MpegEncContext *s){
92073d01 3273 int mb_num;
0c1a9eda 3274 static const int8_t quant_tab[4] = { -1, -2, 1, 2 };
92073d01
MN
3275
3276 /* decode first partition */
3277 mb_num=0;
49092244 3278 s->first_slice_line=1;
4d2858de
MN
3279 for(; s->mb_y<s->mb_height; s->mb_y++){
3280 ff_init_block_index(s);
3281 for(; s->mb_x<s->mb_width; s->mb_x++){
7bc9090a 3282 const int xy= s->mb_x + s->mb_y*s->mb_stride;
92073d01
MN
3283 int cbpc;
3284 int dir=0;
3285
3286 mb_num++;
4d2858de 3287 ff_update_block_index(s);
92073d01
MN
3288 if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1)
3289 s->first_slice_line=0;
3290
92073d01
MN
3291 if(s->pict_type==I_TYPE){
3292 int i;
3293
17fb5fd3 3294 if(show_bits_long(&s->gb, 19)==DC_MARKER){
4d2858de
MN
3295 return mb_num-1;
3296 }
3297
7af75e6c
MN
3298 do{
3299 cbpc = get_vlc2(&s->gb, intra_MCBPC_vlc.table, INTRA_MCBPC_VLC_BITS, 2);
3300 if (cbpc < 0){
9b879566 3301 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
7af75e6c
MN
3302 return -1;
3303 }
3304 }while(cbpc == 8);
4d2858de 3305
92073d01 3306 s->cbp_table[xy]= cbpc & 3;
7bc9090a 3307 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
92073d01
MN
3308 s->mb_intra = 1;
3309
3310 if(cbpc & 4) {
554daa24 3311 ff_set_qscale(s, s->qscale + quant_tab[get_bits(&s->gb, 2)]);
92073d01 3312 }
1e491e29 3313 s->current_picture.qscale_table[xy]= s->qscale;
92073d01
MN
3314
3315 s->mbintra_table[xy]= 1;
3316 for(i=0; i<6; i++){
3317 int dc_pred_dir;
3318 int dc= mpeg4_decode_dc(s, i, &dc_pred_dir);
3319 if(dc < 0){
9b879566 3320 av_log(s->avctx, AV_LOG_ERROR, "DC corrupted at %d %d\n", s->mb_x, s->mb_y);
4d2858de 3321 return -1;
92073d01
MN
3322 }
3323 dir<<=1;
3324 if(dc_pred_dir) dir|=1;
3325 }
3326 s->pred_dir_table[xy]= dir;
3327 }else{ /* P/S_TYPE */
4d2858de 3328 int mx, my, pred_x, pred_y, bits;
8d7ec294 3329 int16_t * const mot_val= s->current_picture.motion_val[0][s->block_index[0]];
92073d01
MN
3330 const int stride= s->block_wrap[0]*2;
3331
8b76418a 3332// do{ //FIXME
4d2858de
MN
3333 bits= show_bits(&s->gb, 17);
3334 if(bits==MOTION_MARKER){
3335 return mb_num-1;
3336 }
3337 skip_bits1(&s->gb);
3338 if(bits&0x10000){
92073d01 3339 /* skip mb */
92073d01 3340 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE){
7bc9090a 3341 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
073b013d
MN
3342 mx= get_amv(s, 0);
3343 my= get_amv(s, 1);
92073d01 3344 }else{
7bc9090a 3345 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
073b013d 3346 mx=my=0;
92073d01
MN
3347 }
3348 mot_val[0 ]= mot_val[2 ]=
3349 mot_val[0+stride]= mot_val[2+stride]= mx;
3350 mot_val[1 ]= mot_val[3 ]=
3351 mot_val[1+stride]= mot_val[3+stride]= my;
3352
3353 if(s->mbintra_table[xy])
3354 ff_clean_intra_table_entries(s);
92073d01
MN
3355 continue;
3356 }
7af75e6c 3357
edcf9890 3358 cbpc = get_vlc2(&s->gb, inter_MCBPC_vlc.table, INTER_MCBPC_VLC_BITS, 2);
92073d01 3359 if (cbpc < 0){
9b879566 3360 av_log(s->avctx, AV_LOG_ERROR, "cbpc corrupted at %d %d\n", s->mb_x, s->mb_y);
4d2858de 3361 return -1;
92073d01 3362 }
8b76418a 3363// }while(cbpc == 20);
7af75e6c 3364
92073d01
MN
3365 s->cbp_table[xy]= cbpc&(8+3); //8 is dquant
3366
3367 s->mb_intra = ((cbpc & 4) != 0);
3368
3369 if(s->mb_intra){
7bc9090a 3370 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
92073d01 3371 s->mbintra_table[xy]= 1;
92073d01
MN
3372 mot_val[0 ]= mot_val[2 ]=
3373 mot_val[0+stride]= mot_val[2+stride]= 0;
3374 mot_val[1 ]= mot_val[3 ]=
3375 mot_val[1+stride]= mot_val[3+stride]= 0;
3376 }else{
3377 if(s->mbintra_table[xy])
3378 ff_clean_intra_table_entries(s);
3379
3380 if(s->pict_type==S_TYPE && s->vol_sprite_usage==GMC_SPRITE && (cbpc & 16) == 0)
3381 s->mcsel= get_bits1(&s->gb);
3382 else s->mcsel= 0;
3383
3384 if ((cbpc & 16) == 0) {
92073d01 3385 /* 16x16 motion prediction */
92073d01
MN
3386
3387 h263_pred_motion(s, 0, &pred_x, &pred_y);
073b013d
MN
3388 if(!s->mcsel){
3389 mx = h263_decode_motion(s, pred_x, s->f_code);
3390 if (mx >= 0xffff)
3391 return -1;
3392
3393 my = h263_decode_motion(s, pred_y, s->f_code);
3394 if (my >= 0xffff)
3395 return -1;
7bc9090a 3396 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
073b013d
MN
3397 } else {
3398 mx = get_amv(s, 0);
3399 my = get_amv(s, 1);
7bc9090a 3400 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_GMC | MB_TYPE_L0;
92073d01 3401 }
073b013d 3402
92073d01
MN
3403 mot_val[0 ]= mot_val[2 ] =
3404 mot_val[0+stride]= mot_val[2+stride]= mx;
3405 mot_val[1 ]= mot_val[3 ]=
3406 mot_val[1+stride]= mot_val[3+stride]= my;
3407 } else {
3408 int i;
7bc9090a 3409 s->current_picture.mb_type[xy]= MB_TYPE_8x8 | MB_TYPE_L0;
92073d01 3410<