Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263.c @ b7c51ebc

History | View | Annotate | Download (210 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * H263/MPEG4 backend for ffmpeg encoder and decoder
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2000,2001 Fabrice Bellard.
4 6dbd39fe Juanjo
 * H263+ support.
5 de6d9b64 Fabrice Bellard
 * Copyright (c) 2001 Juan J. Sierralta P.
6 8f2ab833 Michael Niedermayer
 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
7 de6d9b64 Fabrice Bellard
 *
8 ff4ec49e Fabrice Bellard
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2 of the License, or (at your option) any later version.
12 de6d9b64 Fabrice Bellard
 *
13 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
14 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17 de6d9b64 Fabrice Bellard
 *
18 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 45870f57 Michael Niedermayer
 *
22 66370d3f Michael Niedermayer
 * ac prediction encoding, b-frame support, error resilience, optimizations,
23 115329f1 Diego Biurrun
 * qpel decoding, gmc decoding, interlaced decoding,
24 66370d3f Michael Niedermayer
 * by Michael Niedermayer <michaelni@gmx.at>
25 de6d9b64 Fabrice Bellard
 */
26 2ba8f6b8 Michael Niedermayer
27
/**
28
 * @file h263.c
29 983e3246 Michael Niedermayer
 * h263/mpeg4 codec.
30 2ba8f6b8 Michael Niedermayer
 */
31 115329f1 Diego Biurrun
32 1e209222 Juanjo
//#define DEBUG
33 f2f6134b Michael Niedermayer
#include <limits.h>
34
35 de6d9b64 Fabrice Bellard
#include "common.h"
36
#include "dsputil.h"
37
#include "avcodec.h"
38
#include "mpegvideo.h"
39
#include "h263data.h"
40
#include "mpeg4data.h"
41
42 c5d309f2 Michael Niedermayer
//#undef NDEBUG
43
//#include <assert.h>
44
45 edcf9890 Michael Niedermayer
#define INTRA_MCBPC_VLC_BITS 6
46 7af75e6c Michael Niedermayer
#define INTER_MCBPC_VLC_BITS 7
47 edcf9890 Michael Niedermayer
#define CBPY_VLC_BITS 6
48
#define MV_VLC_BITS 9
49
#define DC_VLC_BITS 9
50
#define SPRITE_TRAJ_VLC_BITS 6
51
#define MB_TYPE_B_VLC_BITS 4
52
#define TEX_VLC_BITS 9
53 b40cd4e0 Michael Niedermayer
#define H263_MBTYPE_B_VLC_BITS 6
54
#define CBPC_B_VLC_BITS 3
55 edcf9890 Michael Niedermayer
56 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
57 de6d9b64 Fabrice Bellard
static void h263_encode_block(MpegEncContext * s, DCTELEM * block,
58 d4f5d74a Garrick Meeker
                              int n);
59 6dbd39fe Juanjo
static void h263p_encode_umotion(MpegEncContext * s, int val);
60 3db48188 Michael Niedermayer
static inline void mpeg4_encode_block(MpegEncContext * s, DCTELEM * block,
61 115329f1 Diego Biurrun
                               int n, int dc, uint8_t *scan_table,
62 92073d01 Michael Niedermayer
                               PutBitContext *dc_pb, PutBitContext *ac_pb);
63 bbed9259 Michael Niedermayer
#endif
64
65 6f91bcd1 Michael Niedermayer
static int h263_decode_motion(MpegEncContext * s, int pred, int fcode);
66 6dbd39fe Juanjo
static int h263p_decode_umotion(MpegEncContext * s, int pred);
67 de6d9b64 Fabrice Bellard
static int h263_decode_block(MpegEncContext * s, DCTELEM * block,
68
                             int n, int coded);
69 92073d01 Michael Niedermayer
static inline int mpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
70
static inline int mpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
71 a4e8b587 Michael Niedermayer
                              int n, int coded, int intra, int rvlc);
72 115329f1 Diego Biurrun
static int mpeg4_get_block_length(MpegEncContext * s, DCTELEM * block, int n, int intra_dc,
73 1b781f82 Michael Niedermayer
                               uint8_t *scan_table);
74 0c1a9eda Zdenek Kabelac
static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr);
75 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
76 22ddd60b Michael Niedermayer
static void mpeg4_encode_visual_object_header(MpegEncContext * s);
77
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number);
78 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
79 3f9d89c6 Michael Niedermayer
static void mpeg4_decode_sprite_trajectory(MpegEncContext * s, GetBitContext *gb);
80 a7137a04 Michael Niedermayer
static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *dir_ptr, int encoding);
81 4d2858de Michael Niedermayer
82 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
83 0c1a9eda Zdenek Kabelac
static uint8_t uni_DCtab_lum_len[512];
84
static uint8_t uni_DCtab_chrom_len[512];
85
static uint16_t uni_DCtab_lum_bits[512];
86
static uint16_t uni_DCtab_chrom_bits[512];
87 bbed9259 Michael Niedermayer
88 30952237 Michael Niedermayer
static uint8_t (*mv_penalty)[MAX_MV*2+1]= NULL;
89 0c1a9eda Zdenek Kabelac
static uint8_t fcode_tab[MAX_MV*2+1];
90
static uint8_t umv_fcode_tab[MAX_MV*2+1];
91 45870f57 Michael Niedermayer
92 477ab036 Michael Niedermayer
static uint32_t uni_mpeg4_intra_rl_bits[64*64*2*2];
93
static uint8_t  uni_mpeg4_intra_rl_len [64*64*2*2];
94
static uint32_t uni_mpeg4_inter_rl_bits[64*64*2*2];
95
static uint8_t  uni_mpeg4_inter_rl_len [64*64*2*2];
96 15c5fa7b Michael Niedermayer
static uint8_t  uni_h263_intra_aic_rl_len [64*64*2*2];
97
static uint8_t  uni_h263_inter_rl_len [64*64*2*2];
98 477ab036 Michael Niedermayer
//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128 + (run)*256 + (level))
99 3db48188 Michael Niedermayer
//#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run) + (level)*64)
100 477ab036 Michael Niedermayer
#define UNI_MPEG4_ENC_INDEX(last,run,level) ((last)*128*64 + (run)*128 + (level))
101 3db48188 Michael Niedermayer
102
/* mpeg4
103
inter
104
max level: 24/6
105
max run: 53/63
106

107
intra
108
max level: 53/16
109
max run: 29/41
110
*/
111 bbed9259 Michael Niedermayer
#endif
112
113 893bc624 Kevin Baragona
#if 0 //3IV1 is quite rare and it slows things down a tiny bit
114 6d71b382 Michael Niedermayer
#define IS_3IV1 s->avctx->codec_tag == ff_get_fourcc("3IV1")
115 115329f1 Diego Biurrun
#else
116 6d71b382 Michael Niedermayer
#define IS_3IV1 0
117
#endif
118 2a164b25 Michael Niedermayer
119 de6d9b64 Fabrice Bellard
int h263_get_picture_format(int width, int height)
120
{
121
    int format;
122
123
    if (width == 128 && height == 96)
124 1e209222 Juanjo
        format = 1;
125 de6d9b64 Fabrice Bellard
    else if (width == 176 && height == 144)
126 1e209222 Juanjo
        format = 2;
127 de6d9b64 Fabrice Bellard
    else if (width == 352 && height == 288)
128 1e209222 Juanjo
        format = 3;
129 de6d9b64 Fabrice Bellard
    else if (width == 704 && height == 576)
130 1e209222 Juanjo
        format = 4;
131 de6d9b64 Fabrice Bellard
    else if (width == 1408 && height == 1152)
132 1e209222 Juanjo
        format = 5;
133 de6d9b64 Fabrice Bellard
    else
134
        format = 7;
135
    return format;
136
}
137
138 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
139
140 5ff85f1d Michael Niedermayer
static void aspect_to_info(MpegEncContext * s, AVRational aspect){
141 58b0b0dd Michael Niedermayer
    int i;
142
143 5ff85f1d Michael Niedermayer
    if(aspect.num==0) aspect= (AVRational){1,1};
144 58b0b0dd Michael Niedermayer
145
    for(i=1; i<6; i++){
146 5ff85f1d Michael Niedermayer
        if(av_cmp_q(pixel_aspect[i], aspect) == 0){
147 58b0b0dd Michael Niedermayer
            s->aspect_ratio_info=i;
148
            return;
149
        }
150
    }
151 115329f1 Diego Biurrun
152 58b0b0dd Michael Niedermayer
    s->aspect_ratio_info= FF_ASPECT_EXTENDED;
153 5d3cea3a Michael Niedermayer
}
154
155 44a2950f Michael Niedermayer
void ff_flv_encode_picture_header(MpegEncContext * s, int picture_number)
156 de6d9b64 Fabrice Bellard
{
157 44a2950f Michael Niedermayer
      int format;
158 de6d9b64 Fabrice Bellard
159 44a2950f Michael Niedermayer
      align_put_bits(&s->pb);
160 81401c1f Juanjo
161 d4f5d74a Garrick Meeker
      put_bits(&s->pb, 17, 1);
162
      put_bits(&s->pb, 5, (s->h263_flv-1)); /* 0: h263 escape codes 1: 11-bit escape codes */
163 c0df9d75 Michael Niedermayer
      put_bits(&s->pb, 8, (((int64_t)s->picture_number * 30 * s->avctx->time_base.num) / //FIXME use timestamp
164
                           s->avctx->time_base.den) & 0xff); /* TemporalReference */
165 d4f5d74a Garrick Meeker
      if (s->width == 352 && s->height == 288)
166
        format = 2;
167
      else if (s->width == 176 && s->height == 144)
168
        format = 3;
169
      else if (s->width == 128 && s->height == 96)
170
        format = 4;
171
      else if (s->width == 320 && s->height == 240)
172
        format = 5;
173
      else if (s->width == 160 && s->height == 120)
174
        format = 6;
175
      else if (s->width <= 255 && s->height <= 255)
176
        format = 0; /* use 1 byte width & height */
177
      else
178
        format = 1; /* use 2 bytes width & height */
179
      put_bits(&s->pb, 3, format); /* PictureSize */
180
      if (format == 0) {
181
        put_bits(&s->pb, 8, s->width);
182
        put_bits(&s->pb, 8, s->height);
183
      } else if (format == 1) {
184
        put_bits(&s->pb, 16, s->width);
185
        put_bits(&s->pb, 16, s->height);
186
      }
187
      put_bits(&s->pb, 2, s->pict_type == P_TYPE); /* PictureType */
188 f5ea4461 Michael Niedermayer
      put_bits(&s->pb, 1, 1); /* DeblockingFlag: on */
189 d4f5d74a Garrick Meeker
      put_bits(&s->pb, 5, s->qscale); /* Quantizer */
190
      put_bits(&s->pb, 1, 0); /* ExtraInformation */
191
192
      if(s->h263_aic){
193 115329f1 Diego Biurrun
        s->y_dc_scale_table=
194 68b94c35 Michael Niedermayer
          s->c_dc_scale_table= ff_aic_dc_scale_table;
195 d4f5d74a Garrick Meeker
      }else{
196
        s->y_dc_scale_table=
197
          s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
198
      }
199 44a2950f Michael Niedermayer
}
200
201
void h263_encode_picture_header(MpegEncContext * s, int picture_number)
202
{
203 b056e73c Michael Niedermayer
    int format, coded_frame_rate, coded_frame_rate_base, i, temp_ref;
204
    int best_clock_code=1;
205
    int best_divisor=60;
206
    int best_error= INT_MAX;
207 115329f1 Diego Biurrun
208 b056e73c Michael Niedermayer
    if(s->h263_plus){
209
        for(i=0; i<2; i++){
210
            int div, error;
211 c0df9d75 Michael Niedermayer
            div= (s->avctx->time_base.num*1800000LL + 500LL*s->avctx->time_base.den) / ((1000LL+i)*s->avctx->time_base.den);
212 b056e73c Michael Niedermayer
            div= clip(1, div, 127);
213 c0df9d75 Michael Niedermayer
            error= ABS(s->avctx->time_base.num*1800000LL - (1000LL+i)*s->avctx->time_base.den*div);
214 b056e73c Michael Niedermayer
            if(error < best_error){
215
                best_error= error;
216
                best_divisor= div;
217
                best_clock_code= i;
218
            }
219
        }
220
    }
221
    s->custom_pcf= best_clock_code!=1 || best_divisor!=60;
222
    coded_frame_rate= 1800000;
223
    coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
224 44a2950f Michael Niedermayer
225
    align_put_bits(&s->pb);
226 d4f5d74a Garrick Meeker
227 81401c1f Juanjo
    /* Update the pointer to last GOB */
228 17592475 Michael Niedermayer
    s->ptr_lastgob = pbBufPtr(&s->pb);
229 81401c1f Juanjo
    put_bits(&s->pb, 22, 0x20); /* PSC */
230 c0df9d75 Michael Niedermayer
    temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->time_base.num / //FIXME use timestamp
231
                         (coded_frame_rate_base * (int64_t)s->avctx->time_base.den);
232 b056e73c Michael Niedermayer
    put_bits(&s->pb, 8, temp_ref & 0xff); /* TemporalReference */
233 de6d9b64 Fabrice Bellard
234 bb270c08 Diego Biurrun
    put_bits(&s->pb, 1, 1);     /* marker */
235
    put_bits(&s->pb, 1, 0);     /* h263 id */
236
    put_bits(&s->pb, 1, 0);     /* split screen off */
237
    put_bits(&s->pb, 1, 0);     /* camera  off */
238
    put_bits(&s->pb, 1, 0);     /* freeze picture release off */
239 115329f1 Diego Biurrun
240 6dbd39fe Juanjo
    format = h263_get_picture_format(s->width, s->height);
241 de6d9b64 Fabrice Bellard
    if (!s->h263_plus) {
242
        /* H.263v1 */
243
        put_bits(&s->pb, 3, format);
244
        put_bits(&s->pb, 1, (s->pict_type == P_TYPE));
245
        /* By now UMV IS DISABLED ON H.263v1, since the restrictions
246
        of H.263v1 UMV implies to check the predicted MV after
247
        calculation of the current MB to see if we're on the limits */
248 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 0);         /* Unrestricted Motion Vector: off */
249
        put_bits(&s->pb, 1, 0);         /* SAC: off */
250
        put_bits(&s->pb, 1, s->obmc);   /* Advanced Prediction */
251
        put_bits(&s->pb, 1, 0);         /* only I/P frames, no PB frame */
252 de6d9b64 Fabrice Bellard
        put_bits(&s->pb, 5, s->qscale);
253 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 0);         /* Continuous Presence Multipoint mode: off */
254 de6d9b64 Fabrice Bellard
    } else {
255 b056e73c Michael Niedermayer
        int ufep=1;
256 de6d9b64 Fabrice Bellard
        /* H.263v2 */
257
        /* H.263 Plus PTYPE */
258 115329f1 Diego Biurrun
259 de6d9b64 Fabrice Bellard
        put_bits(&s->pb, 3, 7);
260 b056e73c Michael Niedermayer
        put_bits(&s->pb,3,ufep); /* Update Full Extended PTYPE */
261 6dbd39fe Juanjo
        if (format == 7)
262
            put_bits(&s->pb,3,6); /* Custom Source Format */
263
        else
264
            put_bits(&s->pb, 3, format);
265 115329f1 Diego Biurrun
266 b056e73c Michael Niedermayer
        put_bits(&s->pb,1, s->custom_pcf);
267 ba58dabc Michael Niedermayer
        put_bits(&s->pb,1, s->umvplus); /* Unrestricted Motion Vector */
268 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,1,0); /* SAC: off */
269 f7190f73 Michael Niedermayer
        put_bits(&s->pb,1,s->obmc); /* Advanced Prediction Mode */
270 ef5b1b5a Juanjo
        put_bits(&s->pb,1,s->h263_aic); /* Advanced Intra Coding */
271 332f9ac4 Michael Niedermayer
        put_bits(&s->pb,1,s->loop_filter); /* Deblocking Filter */
272 ba58dabc Michael Niedermayer
        put_bits(&s->pb,1,s->h263_slice_structured); /* Slice Structured */
273 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,1,0); /* Reference Picture Selection: off */
274
        put_bits(&s->pb,1,0); /* Independent Segment Decoding: off */
275 dba019da Michael Niedermayer
        put_bits(&s->pb,1,s->alt_inter_vlc); /* Alternative Inter VLC */
276 332f9ac4 Michael Niedermayer
        put_bits(&s->pb,1,s->modified_quant); /* Modified Quantization: */
277 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
278
        put_bits(&s->pb,3,0); /* Reserved */
279 115329f1 Diego Biurrun
280 de6d9b64 Fabrice Bellard
        put_bits(&s->pb, 3, s->pict_type == P_TYPE);
281 115329f1 Diego Biurrun
282 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,1,0); /* Reference Picture Resampling: off */
283
        put_bits(&s->pb,1,0); /* Reduced-Resolution Update: off */
284 ef5b1b5a Juanjo
        put_bits(&s->pb,1,s->no_rounding); /* Rounding Type */
285 de6d9b64 Fabrice Bellard
        put_bits(&s->pb,2,0); /* Reserved */
286
        put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
287 115329f1 Diego Biurrun
288 de6d9b64 Fabrice Bellard
        /* This should be here if PLUSPTYPE */
289 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 0); /* Continuous Presence Multipoint mode: off */
290 115329f1 Diego Biurrun
291 bb270c08 Diego Biurrun
                if (format == 7) {
292 6dbd39fe Juanjo
            /* Custom Picture Format (CPFMT) */
293 5ff85f1d Michael Niedermayer
            aspect_to_info(s, s->avctx->sample_aspect_ratio);
294 5d3cea3a Michael Niedermayer
295
            put_bits(&s->pb,4,s->aspect_ratio_info);
296 6dbd39fe Juanjo
            put_bits(&s->pb,9,(s->width >> 2) - 1);
297
            put_bits(&s->pb,1,1); /* "1" to prevent start code emulation */
298
            put_bits(&s->pb,9,(s->height >> 2));
299 5ff85f1d Michael Niedermayer
            if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
300
                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
301
                put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
302 bb270c08 Diego Biurrun
            }
303 6dbd39fe Juanjo
        }
304 b056e73c Michael Niedermayer
        if(s->custom_pcf){
305
            if(ufep){
306
                put_bits(&s->pb, 1, best_clock_code);
307
                put_bits(&s->pb, 7, best_divisor);
308
            }
309
            put_bits(&s->pb, 2, (temp_ref>>8)&3);
310
        }
311 115329f1 Diego Biurrun
312 de6d9b64 Fabrice Bellard
        /* Unlimited Unrestricted Motion Vectors Indicator (UUI) */
313 544286b3 Juanjo
        if (s->umvplus)
314 e51d6d27 Michael Niedermayer
//            put_bits(&s->pb,1,1); /* Limited according tables of Annex D */
315 332f9ac4 Michael Niedermayer
//FIXME check actual requested range
316 e51d6d27 Michael Niedermayer
            put_bits(&s->pb,2,1); /* unlimited */
317 ba58dabc Michael Niedermayer
        if(s->h263_slice_structured)
318
            put_bits(&s->pb,2,0); /* no weird submodes */
319 e51d6d27 Michael Niedermayer
320 de6d9b64 Fabrice Bellard
        put_bits(&s->pb, 5, s->qscale);
321
    }
322
323 bb270c08 Diego Biurrun
    put_bits(&s->pb, 1, 0);     /* no PEI */
324 8f8402e4 Michael Niedermayer
325 0ed714a7 Michael Niedermayer
    if(s->h263_slice_structured){
326
        put_bits(&s->pb, 1, 1);
327 115329f1 Diego Biurrun
328 0ed714a7 Michael Niedermayer
        assert(s->mb_x == 0 && s->mb_y == 0);
329
        ff_h263_encode_mba(s);
330
331
        put_bits(&s->pb, 1, 1);
332
    }
333
334 8f8402e4 Michael Niedermayer
    if(s->h263_aic){
335 115329f1 Diego Biurrun
         s->y_dc_scale_table=
336 68b94c35 Michael Niedermayer
         s->c_dc_scale_table= ff_aic_dc_scale_table;
337 8f8402e4 Michael Niedermayer
    }else{
338
        s->y_dc_scale_table=
339
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
340
    }
341 de6d9b64 Fabrice Bellard
}
342
343 945eeee1 Michael Niedermayer
/**
344
 * Encodes a group of blocks header.
345
 */
346 ba58dabc Michael Niedermayer
void h263_encode_gob_header(MpegEncContext * s, int mb_line)
347 644d98a4 Juanjo
{
348 ba58dabc Michael Niedermayer
    put_bits(&s->pb, 17, 1); /* GBSC */
349
350
    if(s->h263_slice_structured){
351
        put_bits(&s->pb, 1, 1);
352
353
        ff_h263_encode_mba(s);
354
355
        if(s->mb_num > 1583)
356
            put_bits(&s->pb, 1, 1);
357
        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
358
        put_bits(&s->pb, 1, 1);
359
        put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
360
    }else{
361
        int gob_number= mb_line / s->gob_index;
362
363
        put_bits(&s->pb, 5, gob_number); /* GN */
364
        put_bits(&s->pb, 2, s->pict_type == I_TYPE); /* GFID */
365
        put_bits(&s->pb, 5, s->qscale); /* GQUANT */
366
    }
367 644d98a4 Juanjo
}
368 d6231b9e Michael Niedermayer
369 8d8c0294 Michael Niedermayer
static inline int get_block_rate(MpegEncContext * s, DCTELEM block[64], int block_last_index, uint8_t scantable[64]){
370
    int last=0;
371
    int j;
372
    int rate=0;
373
374
    for(j=1; j<=block_last_index; j++){
375
        const int index= scantable[j];
376
        int level= block[index];
377
        if(level){
378
            level+= 64;
379
            if((level&(~127)) == 0){
380
                if(j<block_last_index) rate+= s->intra_ac_vlc_length     [UNI_AC_ENC_INDEX(j-last-1, level)];
381
                else                   rate+= s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j-last-1, level)];
382
            }else
383
                rate += s->ac_esc_length;
384
            level-= 64;
385
386
            last= j;
387
        }
388
    }
389 115329f1 Diego Biurrun
390 8d8c0294 Michael Niedermayer
    return rate;
391
}
392
393
static inline int decide_ac_pred(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6])
394 d6231b9e Michael Niedermayer
{
395 8d8c0294 Michael Niedermayer
    int score= 0;
396 d6231b9e Michael Niedermayer
    int i, n;
397 1e491e29 Michael Niedermayer
    int8_t * const qscale_table= s->current_picture.qscale_table;
398 d6231b9e Michael Niedermayer
399 8d8c0294 Michael Niedermayer
    memcpy(zigzag_last_index, s->block_last_index, sizeof(int)*6);
400 115329f1 Diego Biurrun
401 d6231b9e Michael Niedermayer
    for(n=0; n<6; n++){
402 0c1a9eda Zdenek Kabelac
        int16_t *ac_val, *ac_val1;
403 115329f1 Diego Biurrun
404 8d8c0294 Michael Niedermayer
        score -= get_block_rate(s, block[n], s->block_last_index[n], s->intra_scantable.permutated);
405 d6231b9e Michael Niedermayer
406 4278e7a6 Michael Niedermayer
        ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
407 d6231b9e Michael Niedermayer
        ac_val1= ac_val;
408
        if(dir[n]){
409 7bc9090a Michael Niedermayer
            const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
410 99609036 Michael Niedermayer
            /* top prediction */
411 4278e7a6 Michael Niedermayer
            ac_val-= s->block_wrap[n]*16;
412 1e491e29 Michael Niedermayer
            if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
413 99609036 Michael Niedermayer
                /* same qscale */
414
                for(i=1; i<8; i++){
415 b0368839 Michael Niedermayer
                    const int level= block[n][s->dsp.idct_permutation[i   ]];
416 8d8c0294 Michael Niedermayer
                    block[n][s->dsp.idct_permutation[i   ]] = level - ac_val[i+8];
417 b0368839 Michael Niedermayer
                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
418 99609036 Michael Niedermayer
                    ac_val1[i+8]= level;
419
                }
420
            }else{
421
                /* different qscale, we must rescale */
422
                for(i=1; i<8; i++){
423 b0368839 Michael Niedermayer
                    const int level= block[n][s->dsp.idct_permutation[i   ]];
424 8d8c0294 Michael Niedermayer
                    block[n][s->dsp.idct_permutation[i   ]] = level - ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
425 b0368839 Michael Niedermayer
                    ac_val1[i  ]=    block[n][s->dsp.idct_permutation[i<<3]];
426 99609036 Michael Niedermayer
                    ac_val1[i+8]= level;
427
                }
428 d6231b9e Michael Niedermayer
            }
429 8d8c0294 Michael Niedermayer
            st[n]= s->intra_h_scantable.permutated;
430 d6231b9e Michael Niedermayer
        }else{
431 7bc9090a Michael Niedermayer
            const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
432 99609036 Michael Niedermayer
            /* left prediction */
433 d6231b9e Michael Niedermayer
            ac_val-= 16;
434 1e491e29 Michael Niedermayer
            if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
435 99609036 Michael Niedermayer
                /* same qscale */
436
                for(i=1; i<8; i++){
437 b0368839 Michael Niedermayer
                    const int level= block[n][s->dsp.idct_permutation[i<<3]];
438 8d8c0294 Michael Niedermayer
                    block[n][s->dsp.idct_permutation[i<<3]]= level - ac_val[i];
439 99609036 Michael Niedermayer
                    ac_val1[i  ]= level;
440 b0368839 Michael Niedermayer
                    ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
441 99609036 Michael Niedermayer
                }
442
            }else{
443
                /* different qscale, we must rescale */
444
                for(i=1; i<8; i++){
445 b0368839 Michael Niedermayer
                    const int level= block[n][s->dsp.idct_permutation[i<<3]];
446 8d8c0294 Michael Niedermayer
                    block[n][s->dsp.idct_permutation[i<<3]]= level - ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
447 99609036 Michael Niedermayer
                    ac_val1[i  ]= level;
448 b0368839 Michael Niedermayer
                    ac_val1[i+8]=    block[n][s->dsp.idct_permutation[i   ]];
449 99609036 Michael Niedermayer
                }
450 d6231b9e Michael Niedermayer
            }
451 8d8c0294 Michael Niedermayer
            st[n]= s->intra_v_scantable.permutated;
452 d6231b9e Michael Niedermayer
        }
453 8d8c0294 Michael Niedermayer
454
        for(i=63; i>0; i--) //FIXME optimize
455
            if(block[n][ st[n][i] ]) break;
456
        s->block_last_index[n]= i;
457
458
        score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
459 d6231b9e Michael Niedermayer
    }
460
461 8d8c0294 Michael Niedermayer
    return score < 0;
462
}
463
464
static inline void restore_ac_coeffs(MpegEncContext * s, DCTELEM block[6][64], int dir[6], uint8_t *st[6], int zigzag_last_index[6])
465
{
466
    int i, n;
467
    memcpy(s->block_last_index, zigzag_last_index, sizeof(int)*6);
468
469
    for(n=0; n<6; n++){
470
        int16_t *ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
471
472
        st[n]= s->intra_scantable.permutated;
473
        if(dir[n]){
474
            /* top prediction */
475
            for(i=1; i<8; i++){
476
                block[n][s->dsp.idct_permutation[i   ]] = ac_val[i+8];
477
            }
478
        }else{
479
            /* left prediction */
480
            for(i=1; i<8; i++){
481
                block[n][s->dsp.idct_permutation[i<<3]]= ac_val[i  ];
482
            }
483
        }
484
    }
485 d6231b9e Michael Niedermayer
}
486
487 4d2858de Michael Niedermayer
/**
488
 * modify qscale so that encoding is acually possible in h263 (limit difference to -2..2)
489
 */
490 9e4c6948 Michael Niedermayer
void ff_clean_h263_qscales(MpegEncContext *s){
491 c5d309f2 Michael Niedermayer
    int i;
492 1e491e29 Michael Niedermayer
    int8_t * const qscale_table= s->current_picture.qscale_table;
493 115329f1 Diego Biurrun
494 c5d309f2 Michael Niedermayer
    for(i=1; i<s->mb_num; i++){
495 7bc9090a Michael Niedermayer
        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i-1] ] >2)
496
            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i-1] ]+2;
497 c5d309f2 Michael Niedermayer
    }
498
    for(i=s->mb_num-2; i>=0; i--){
499 7bc9090a Michael Niedermayer
        if(qscale_table[ s->mb_index2xy[i] ] - qscale_table[ s->mb_index2xy[i+1] ] >2)
500
            qscale_table[ s->mb_index2xy[i] ]= qscale_table[ s->mb_index2xy[i+1] ]+2;
501 c5d309f2 Michael Niedermayer
    }
502 ae4b7d59 Michael Niedermayer
503
    if(s->codec_id != CODEC_ID_H263P){
504
        for(i=1; i<s->mb_num; i++){
505
            int mb_xy= s->mb_index2xy[i];
506 115329f1 Diego Biurrun
507 ae4b7d59 Michael Niedermayer
            if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_INTER4V)){
508
                s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_INTER4V;
509
                s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_INTER;
510
            }
511
        }
512
    }
513 9e4c6948 Michael Niedermayer
}
514
515 4d2858de Michael Niedermayer
/**
516
 * modify mb_type & qscale so that encoding is acually possible in mpeg4
517
 */
518 9e4c6948 Michael Niedermayer
void ff_clean_mpeg4_qscales(MpegEncContext *s){
519
    int i;
520 1e491e29 Michael Niedermayer
    int8_t * const qscale_table= s->current_picture.qscale_table;
521
522 9e4c6948 Michael Niedermayer
    ff_clean_h263_qscales(s);
523 115329f1 Diego Biurrun
524 c5d309f2 Michael Niedermayer
    if(s->pict_type== B_TYPE){
525
        int odd=0;
526 893bc624 Kevin Baragona
        /* ok, come on, this isn't funny anymore, there's more code for handling this mpeg4 mess than for the actual adaptive quantization */
527 115329f1 Diego Biurrun
528 c5d309f2 Michael Niedermayer
        for(i=0; i<s->mb_num; i++){
529 7bc9090a Michael Niedermayer
            int mb_xy= s->mb_index2xy[i];
530
            odd += qscale_table[mb_xy]&1;
531 c5d309f2 Michael Niedermayer
        }
532 115329f1 Diego Biurrun
533 c5d309f2 Michael Niedermayer
        if(2*odd > s->mb_num) odd=1;
534
        else                  odd=0;
535 115329f1 Diego Biurrun
536 c5d309f2 Michael Niedermayer
        for(i=0; i<s->mb_num; i++){
537 7bc9090a Michael Niedermayer
            int mb_xy= s->mb_index2xy[i];
538
            if((qscale_table[mb_xy]&1) != odd)
539
                qscale_table[mb_xy]++;
540
            if(qscale_table[mb_xy] > 31)
541
                qscale_table[mb_xy]= 31;
542 115329f1 Diego Biurrun
        }
543
544 c5d309f2 Michael Niedermayer
        for(i=1; i<s->mb_num; i++){
545 7bc9090a Michael Niedermayer
            int mb_xy= s->mb_index2xy[i];
546 bb198e19 Michael Niedermayer
            if(qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i-1]] && (s->mb_type[mb_xy]&CANDIDATE_MB_TYPE_DIRECT)){
547
                s->mb_type[mb_xy]&= ~CANDIDATE_MB_TYPE_DIRECT;
548
                s->mb_type[mb_xy]|= CANDIDATE_MB_TYPE_BIDIR;
549 c5d309f2 Michael Niedermayer
            }
550
        }
551
    }
552
}
553
554 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
555 7bc9090a Michael Niedermayer
/**
556
 *
557
 * @return the mb_type
558
 */
559
int ff_mpeg4_set_direct_mv(MpegEncContext *s, int mx, int my){
560
    const int mb_index= s->mb_x + s->mb_y*s->mb_stride;
561 bb198e19 Michael Niedermayer
    const int colocated_mb_type= s->next_picture.mb_type[mb_index];
562 1457ab52 Michael Niedermayer
    int xy= s->block_index[0];
563
    uint16_t time_pp= s->pp_time;
564
    uint16_t time_pb= s->pb_time;
565
    int i;
566 115329f1 Diego Biurrun
567 1457ab52 Michael Niedermayer
    //FIXME avoid divides
568 115329f1 Diego Biurrun
569 7bc9090a Michael Niedermayer
    if(IS_8X8(colocated_mb_type)){
570 1457ab52 Michael Niedermayer
        s->mv_type = MV_TYPE_8X8;
571
        for(i=0; i<4; i++){
572
            xy= s->block_index[i];
573 8d7ec294 Wolfgang Hesseler
            s->mv[0][i][0] = s->next_picture.motion_val[0][xy][0]*time_pb/time_pp + mx;
574
            s->mv[0][i][1] = s->next_picture.motion_val[0][xy][1]*time_pb/time_pp + my;
575
            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->next_picture.motion_val[0][xy][0]
576
                                : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
577 115329f1 Diego Biurrun
            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->next_picture.motion_val[0][xy][1]
578 8d7ec294 Wolfgang Hesseler
                                : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
579 1457ab52 Michael Niedermayer
        }
580 7bc9090a Michael Niedermayer
        return MB_TYPE_DIRECT2 | MB_TYPE_8x8 | MB_TYPE_L0L1;
581
    } else if(IS_INTERLACED(colocated_mb_type)){
582 1457ab52 Michael Niedermayer
        s->mv_type = MV_TYPE_FIELD;
583
        for(i=0; i<2; i++){
584 7c4f71c4 Michael Niedermayer
            int field_select= s->next_picture.ref_index[0][s->block_index[2*i]];
585 1457ab52 Michael Niedermayer
            if(s->top_field_first){
586 7c4f71c4 Michael Niedermayer
                time_pp= s->pp_field_time - field_select + i;
587
                time_pb= s->pb_field_time - field_select + i;
588 1457ab52 Michael Niedermayer
            }else{
589 7c4f71c4 Michael Niedermayer
                time_pp= s->pp_field_time + field_select - i;
590
                time_pb= s->pb_field_time + field_select - i;
591 1457ab52 Michael Niedermayer
            }
592 bb198e19 Michael Niedermayer
            s->mv[0][i][0] = s->p_field_mv_table[i][0][mb_index][0]*time_pb/time_pp + mx;
593
            s->mv[0][i][1] = s->p_field_mv_table[i][0][mb_index][1]*time_pb/time_pp + my;
594
            s->mv[1][i][0] = mx ? s->mv[0][i][0] - s->p_field_mv_table[i][0][mb_index][0]
595
                                : s->p_field_mv_table[i][0][mb_index][0]*(time_pb - time_pp)/time_pp;
596 115329f1 Diego Biurrun
            s->mv[1][i][1] = my ? s->mv[0][i][1] - s->p_field_mv_table[i][0][mb_index][1]
597 bb198e19 Michael Niedermayer
                                : s->p_field_mv_table[i][0][mb_index][1]*(time_pb - time_pp)/time_pp;
598 1457ab52 Michael Niedermayer
        }
599 7bc9090a Michael Niedermayer
        return MB_TYPE_DIRECT2 | MB_TYPE_16x8 | MB_TYPE_L0L1 | MB_TYPE_INTERLACED;
600
    }else{
601 8d7ec294 Wolfgang Hesseler
        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;
602
        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;
603
        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]
604
                            : s->next_picture.motion_val[0][xy][0]*(time_pb - time_pp)/time_pp;
605 115329f1 Diego Biurrun
        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]
606 8d7ec294 Wolfgang Hesseler
                            : s->next_picture.motion_val[0][xy][1]*(time_pb - time_pp)/time_pp;
607 7bc9090a Michael Niedermayer
        if((s->avctx->workaround_bugs & FF_BUG_DIRECT_BLOCKSIZE) || !s->quarter_sample)
608
            s->mv_type= MV_TYPE_16X16;
609
        else
610
            s->mv_type= MV_TYPE_8X8;
611
        return MB_TYPE_DIRECT2 | MB_TYPE_16x16 | MB_TYPE_L0L1; //Note see prev line
612 1457ab52 Michael Niedermayer
    }
613
}
614
615 7d1c3fc1 Michael Niedermayer
void ff_h263_update_motion_val(MpegEncContext * s){
616
    const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
617 893bc624 Kevin Baragona
               //FIXME a lot of that is only needed for !low_delay
618 137c8468 Michael Niedermayer
    const int wrap = s->b8_stride;
619 7d1c3fc1 Michael Niedermayer
    const int xy = s->block_index[0];
620 115329f1 Diego Biurrun
621
    s->current_picture.mbskip_table[mb_xy]= s->mb_skipped;
622 7d1c3fc1 Michael Niedermayer
623
    if(s->mv_type != MV_TYPE_8X8){
624
        int motion_x, motion_y;
625
        if (s->mb_intra) {
626
            motion_x = 0;
627
            motion_y = 0;
628
        } else if (s->mv_type == MV_TYPE_16X16) {
629
            motion_x = s->mv[0][0][0];
630
            motion_y = s->mv[0][0][1];
631
        } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
632
            int i;
633
            motion_x = s->mv[0][0][0] + s->mv[0][1][0];
634
            motion_y = s->mv[0][0][1] + s->mv[0][1][1];
635
            motion_x = (motion_x>>1) | (motion_x&1);
636
            for(i=0; i<2; i++){
637 bb198e19 Michael Niedermayer
                s->p_field_mv_table[i][0][mb_xy][0]= s->mv[0][i][0];
638
                s->p_field_mv_table[i][0][mb_xy][1]= s->mv[0][i][1];
639 7d1c3fc1 Michael Niedermayer
            }
640 7c4f71c4 Michael Niedermayer
            s->current_picture.ref_index[0][xy           ]=
641
            s->current_picture.ref_index[0][xy        + 1]= s->field_select[0][0];
642
            s->current_picture.ref_index[0][xy + wrap    ]=
643
            s->current_picture.ref_index[0][xy + wrap + 1]= s->field_select[0][1];
644 7d1c3fc1 Michael Niedermayer
        }
645 7c4f71c4 Michael Niedermayer
646 7d1c3fc1 Michael Niedermayer
        /* no update if 8X8 because it has been done during parsing */
647 8d7ec294 Wolfgang Hesseler
        s->current_picture.motion_val[0][xy][0] = motion_x;
648
        s->current_picture.motion_val[0][xy][1] = motion_y;
649
        s->current_picture.motion_val[0][xy + 1][0] = motion_x;
650
        s->current_picture.motion_val[0][xy + 1][1] = motion_y;
651
        s->current_picture.motion_val[0][xy + wrap][0] = motion_x;
652
        s->current_picture.motion_val[0][xy + wrap][1] = motion_y;
653
        s->current_picture.motion_val[0][xy + 1 + wrap][0] = motion_x;
654
        s->current_picture.motion_val[0][xy + 1 + wrap][1] = motion_y;
655 7d1c3fc1 Michael Niedermayer
    }
656
657
    if(s->encoding){ //FIXME encoding MUST be cleaned up
658 115329f1 Diego Biurrun
        if (s->mv_type == MV_TYPE_8X8)
659 7d1c3fc1 Michael Niedermayer
            s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_8x8;
660 f7190f73 Michael Niedermayer
        else if(s->mb_intra)
661
            s->current_picture.mb_type[mb_xy]= MB_TYPE_INTRA;
662 7d1c3fc1 Michael Niedermayer
        else
663
            s->current_picture.mb_type[mb_xy]= MB_TYPE_L0 | MB_TYPE_16x16;
664
    }
665
}
666
667 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
668 f2f6134b Michael Niedermayer
669 1b781f82 Michael Niedermayer
static inline int h263_get_motion_length(MpegEncContext * s, int val, int f_code){
670
    int l, bit_size, code;
671
672
    if (val == 0) {
673
        return mvtab[0][1];
674
    } else {
675
        bit_size = f_code - 1;
676
        /* modulo encoding */
677
        l= INT_BIT - 6 - bit_size;
678
        val = (val<<l)>>l;
679
        val--;
680
        code = (val >> bit_size) + 1;
681
682
        return mvtab[code][1] + 1 + bit_size;
683
    }
684
}
685
686
static inline void ff_h263_encode_motion_vector(MpegEncContext * s, int x, int y, int f_code){
687
    if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
688 115329f1 Diego Biurrun
        skip_put_bits(&s->pb,
689 1b781f82 Michael Niedermayer
            h263_get_motion_length(s, x, f_code)
690
           +h263_get_motion_length(s, y, f_code));
691
    }else{
692
        ff_h263_encode_motion(s, x, f_code);
693
        ff_h263_encode_motion(s, y, f_code);
694
    }
695
}
696
697 f2f6134b Michael Niedermayer
static inline int get_p_cbp(MpegEncContext * s,
698
                      DCTELEM block[6][64],
699
                      int motion_x, int motion_y){
700
    int cbp, i;
701
702
    if(s->flags & CODEC_FLAG_CBP_RD){
703
        int best_cbpy_score= INT_MAX;
704
        int best_cbpc_score= INT_MAX;
705 9b879566 Michel Bardiaux
        int cbpc = (-1), cbpy= (-1);
706 f2f6134b Michael Niedermayer
        const int offset= (s->mv_type==MV_TYPE_16X16 ? 0 : 16) + (s->dquant ? 8 : 0);
707 158c7f05 Michael Niedermayer
        const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
708 f2f6134b Michael Niedermayer
709
        for(i=0; i<4; i++){
710
            int score= inter_MCBPC_bits[i + offset] * lambda;
711
            if(i&1) score += s->coded_score[5];
712
            if(i&2) score += s->coded_score[4];
713
714
            if(score < best_cbpc_score){
715
                best_cbpc_score= score;
716
                cbpc= i;
717
            }
718
        }
719
720
        for(i=0; i<16; i++){
721
            int score= cbpy_tab[i ^ 0xF][1] * lambda;
722
            if(i&1) score += s->coded_score[3];
723
            if(i&2) score += s->coded_score[2];
724
            if(i&4) score += s->coded_score[1];
725
            if(i&8) score += s->coded_score[0];
726
727
            if(score < best_cbpy_score){
728
                best_cbpy_score= score;
729
                cbpy= i;
730
            }
731
        }
732
        cbp= cbpc + 4*cbpy;
733
        if ((motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16){
734
            if(best_cbpy_score + best_cbpc_score + 2*lambda >= 0)
735
                cbp= 0;
736
        }
737
738
        for (i = 0; i < 6; i++) {
739
            if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
740
                s->block_last_index[i]= -1;
741
                memset(s->block[i], 0, sizeof(DCTELEM)*64);
742
            }
743
        }
744
    }else{
745
        cbp= 0;
746
        for (i = 0; i < 6; i++) {
747
            if (s->block_last_index[i] >= 0)
748
                cbp |= 1 << (5 - i);
749
        }
750
    }
751
    return cbp;
752
}
753
754
static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
755
                            int motion_x, int motion_y, int mb_type){
756
    int cbp=0, i;
757
758
    if(s->flags & CODEC_FLAG_CBP_RD){
759
        int score=0;
760 158c7f05 Michael Niedermayer
        const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
761 115329f1 Diego Biurrun
762 f2f6134b Michael Niedermayer
        for(i=0; i<6; i++){
763
            if(s->coded_score[i] < 0){
764
                score += s->coded_score[i];
765
                cbp |= 1 << (5 - i);
766
            }
767
        }
768 115329f1 Diego Biurrun
769 f2f6134b Michael Niedermayer
        if(cbp){
770
            int zero_score= -6;
771
            if ((motion_x | motion_y | s->dquant | mb_type) == 0){
772
                zero_score-= 4; //2*MV + mb_type + cbp bit
773
            }
774
775
            zero_score*= lambda;
776
            if(zero_score <= score){
777
                cbp=0;
778
            }
779
        }
780
781
        for (i = 0; i < 6; i++) {
782
            if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
783
                s->block_last_index[i]= -1;
784
                memset(s->block[i], 0, sizeof(DCTELEM)*64);
785
            }
786
        }
787
    }else{
788
        for (i = 0; i < 6; i++) {
789
            if (s->block_last_index[i] >= 0)
790
                cbp |= 1 << (5 - i);
791
        }
792
    }
793
    return cbp;
794
}
795
796 115329f1 Diego Biurrun
static inline void mpeg4_encode_blocks(MpegEncContext * s, DCTELEM block[6][64], int intra_dc[6],
797 1b781f82 Michael Niedermayer
                               uint8_t **scan_table, PutBitContext *dc_pb, PutBitContext *ac_pb){
798
    int i;
799 115329f1 Diego Biurrun
800 1b781f82 Michael Niedermayer
    if(scan_table){
801
        if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
802
            for (i = 0; i < 6; i++) {
803
                skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, intra_dc[i], scan_table[i]));
804
            }
805
        }else{
806
            /* encode each block */
807
            for (i = 0; i < 6; i++) {
808
                mpeg4_encode_block(s, block[i], i, intra_dc[i], scan_table[i], dc_pb, ac_pb);
809
            }
810
        }
811
    }else{
812
        if(s->flags2 & CODEC_FLAG2_NO_OUTPUT){
813
            for (i = 0; i < 6; i++) {
814
                skip_put_bits(&s->pb, mpeg4_get_block_length(s, block[i], i, 0, s->intra_scantable.permutated));
815
            }
816
        }else{
817
            /* encode each block */
818
            for (i = 0; i < 6; i++) {
819
                mpeg4_encode_block(s, block[i], i, 0, s->intra_scantable.permutated, dc_pb, ac_pb);
820
            }
821
        }
822
    }
823
}
824
825 d6231b9e Michael Niedermayer
void mpeg4_encode_mb(MpegEncContext * s,
826 bb270c08 Diego Biurrun
                    DCTELEM block[6][64],
827
                    int motion_x, int motion_y)
828 d6231b9e Michael Niedermayer
{
829 5c91a675 Zdenek Kabelac
    int cbpc, cbpy, pred_x, pred_y;
830 92073d01 Michael Niedermayer
    PutBitContext * const pb2    = s->data_partitioning                         ? &s->pb2    : &s->pb;
831
    PutBitContext * const tex_pb = s->data_partitioning && s->pict_type!=B_TYPE ? &s->tex_pb : &s->pb;
832
    PutBitContext * const dc_pb  = s->data_partitioning && s->pict_type!=I_TYPE ? &s->pb2    : &s->pb;
833
    const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
834 c5d309f2 Michael Niedermayer
    const int dquant_code[5]= {1,0,9,2,3};
835 115329f1 Diego Biurrun
836 d6231b9e Michael Niedermayer
    //    printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
837
    if (!s->mb_intra) {
838 f2f6134b Michael Niedermayer
        int i, cbp;
839 115329f1 Diego Biurrun
840 9dbcbd92 Michael Niedermayer
        if(s->pict_type==B_TYPE){
841
            static const int mb_type_table[8]= {-1, 2, 3, 1,-1,-1,-1, 0}; /* convert from mv_dir to type */
842
            int mb_type=  mb_type_table[s->mv_dir];
843 bb198e19 Michael Niedermayer
844 9dbcbd92 Michael Niedermayer
            if(s->mb_x==0){
845 bb198e19 Michael Niedermayer
                for(i=0; i<2; i++){
846 115329f1 Diego Biurrun
                    s->last_mv[i][0][0]=
847
                    s->last_mv[i][0][1]=
848
                    s->last_mv[i][1][0]=
849 bb198e19 Michael Niedermayer
                    s->last_mv[i][1][1]= 0;
850
                }
851 9dbcbd92 Michael Niedermayer
            }
852 115329f1 Diego Biurrun
853 c5d309f2 Michael Niedermayer
            assert(s->dquant>=-2 && s->dquant<=2);
854
            assert((s->dquant&1)==0);
855
            assert(mb_type>=0);
856 9dbcbd92 Michael Niedermayer
857 160d679c Mike Melanson
            /* nothing to do if this MB was skipped in the next P Frame */
858 7bc9090a Michael Niedermayer
            if(s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]){ //FIXME avoid DCT & ...
859 9dbcbd92 Michael Niedermayer
                s->skip_count++;
860 115329f1 Diego Biurrun
                s->mv[0][0][0]=
861
                s->mv[0][0][1]=
862
                s->mv[1][0][0]=
863 9dbcbd92 Michael Niedermayer
                s->mv[1][0][1]= 0;
864 893bc624 Kevin Baragona
                s->mv_dir= MV_DIR_FORWARD; //doesn't matter
865 c5d309f2 Michael Niedermayer
                s->qscale -= s->dquant;
866 160d679c Mike Melanson
//                s->mb_skipped=1;
867 1e491e29 Michael Niedermayer
868 9dbcbd92 Michael Niedermayer
                return;
869
            }
870 115329f1 Diego Biurrun
871 f2f6134b Michael Niedermayer
            cbp= get_b_cbp(s, block, motion_x, motion_y, mb_type);
872 115329f1 Diego Biurrun
873 9dbcbd92 Michael Niedermayer
            if ((cbp | motion_x | motion_y | mb_type) ==0) {
874
                /* direct MB with MV={0,0} */
875 c5d309f2 Michael Niedermayer
                assert(s->dquant==0);
876 115329f1 Diego Biurrun
877 9dbcbd92 Michael Niedermayer
                put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
878 92073d01 Michael Niedermayer
879
                if(interleaved_stats){
880
                    s->misc_bits++;
881
                    s->last_bits++;
882
                }
883 9dbcbd92 Michael Niedermayer
                s->skip_count++;
884
                return;
885
            }
886 115329f1 Diego Biurrun
887 bb270c08 Diego Biurrun
            put_bits(&s->pb, 1, 0);     /* mb coded modb1=0 */
888 9dbcbd92 Michael Niedermayer
            put_bits(&s->pb, 1, cbp ? 0 : 1); /* modb2 */ //FIXME merge
889 893bc624 Kevin Baragona
            put_bits(&s->pb, mb_type+1, 1); // this table is so simple that we don't need it :)
890 9dbcbd92 Michael Niedermayer
            if(cbp) put_bits(&s->pb, 6, cbp);
891 115329f1 Diego Biurrun
892 c5d309f2 Michael Niedermayer
            if(cbp && mb_type){
893
                if(s->dquant)
894
                    put_bits(&s->pb, 2, (s->dquant>>2)+3);
895
                else
896
                    put_bits(&s->pb, 1, 0);
897
            }else
898
                s->qscale -= s->dquant;
899 115329f1 Diego Biurrun
900 fcb48651 Michael Niedermayer
            if(!s->progressive_sequence){
901
                if(cbp)
902
                    put_bits(&s->pb, 1, s->interlaced_dct);
903 893bc624 Kevin Baragona
                if(mb_type) // not direct mode
904 bb198e19 Michael Niedermayer
                    put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
905 fcb48651 Michael Niedermayer
            }
906 9dbcbd92 Michael Niedermayer
907 92073d01 Michael Niedermayer
            if(interleaved_stats){
908 4d2a4834 Michael Niedermayer
                s->misc_bits+= get_bits_diff(s);
909 92073d01 Michael Niedermayer
            }
910 cf8039b2 Michael Niedermayer
911 bb198e19 Michael Niedermayer
            if(mb_type == 0){
912
                assert(s->mv_dir & MV_DIRECT);
913 1b781f82 Michael Niedermayer
                ff_h263_encode_motion_vector(s, motion_x, motion_y, 1);
914 66370d3f Michael Niedermayer
                s->b_count++;
915
                s->f_count++;
916 bb198e19 Michael Niedermayer
            }else{
917
                assert(mb_type > 0 && mb_type < 4);
918
                if(s->mv_type != MV_TYPE_FIELD){
919
                    if(s->mv_dir & MV_DIR_FORWARD){
920 1b781f82 Michael Niedermayer
                        ff_h263_encode_motion_vector(s, s->mv[0][0][0] - s->last_mv[0][0][0],
921
                                                        s->mv[0][0][1] - s->last_mv[0][0][1], s->f_code);
922 bb198e19 Michael Niedermayer
                        s->last_mv[0][0][0]= s->last_mv[0][1][0]= s->mv[0][0][0];
923
                        s->last_mv[0][0][1]= s->last_mv[0][1][1]= s->mv[0][0][1];
924
                        s->f_count++;
925
                    }
926
                    if(s->mv_dir & MV_DIR_BACKWARD){
927 1b781f82 Michael Niedermayer
                        ff_h263_encode_motion_vector(s, s->mv[1][0][0] - s->last_mv[1][0][0],
928
                                                        s->mv[1][0][1] - s->last_mv[1][0][1], s->b_code);
929 bb198e19 Michael Niedermayer
                        s->last_mv[1][0][0]= s->last_mv[1][1][0]= s->mv[1][0][0];
930
                        s->last_mv[1][0][1]= s->last_mv[1][1][1]= s->mv[1][0][1];
931
                        s->b_count++;
932
                    }
933
                }else{
934
                    if(s->mv_dir & MV_DIR_FORWARD){
935
                        put_bits(&s->pb, 1, s->field_select[0][0]);
936
                        put_bits(&s->pb, 1, s->field_select[0][1]);
937
                    }
938
                    if(s->mv_dir & MV_DIR_BACKWARD){
939
                        put_bits(&s->pb, 1, s->field_select[1][0]);
940
                        put_bits(&s->pb, 1, s->field_select[1][1]);
941
                    }
942
                    if(s->mv_dir & MV_DIR_FORWARD){
943
                        for(i=0; i<2; i++){
944 1b781f82 Michael Niedermayer
                            ff_h263_encode_motion_vector(s, s->mv[0][i][0] - s->last_mv[0][i][0]  ,
945
                                                            s->mv[0][i][1] - s->last_mv[0][i][1]/2, s->f_code);
946 bb198e19 Michael Niedermayer
                            s->last_mv[0][i][0]= s->mv[0][i][0];
947
                            s->last_mv[0][i][1]= s->mv[0][i][1]*2;
948
                        }
949
                        s->f_count++;
950
                    }
951
                    if(s->mv_dir & MV_DIR_BACKWARD){
952
                        for(i=0; i<2; i++){
953 1b781f82 Michael Niedermayer
                            ff_h263_encode_motion_vector(s, s->mv[1][i][0] - s->last_mv[1][i][0]  ,
954
                                                            s->mv[1][i][1] - s->last_mv[1][i][1]/2, s->b_code);
955 bb198e19 Michael Niedermayer
                            s->last_mv[1][i][0]= s->mv[1][i][0];
956
                            s->last_mv[1][i][1]= s->mv[1][i][1]*2;
957
                        }
958
                        s->b_count++;
959
                    }
960
                }
961 9dbcbd92 Michael Niedermayer
            }
962 92073d01 Michael Niedermayer
963
            if(interleaved_stats){
964 4d2a4834 Michael Niedermayer
                s->mv_bits+= get_bits_diff(s);
965 92073d01 Michael Niedermayer
            }
966 d6231b9e Michael Niedermayer
967 1b781f82 Michael Niedermayer
            mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb);
968 92073d01 Michael Niedermayer
969
            if(interleaved_stats){
970 4d2a4834 Michael Niedermayer
                s->p_tex_bits+= get_bits_diff(s);
971 9dbcbd92 Michael Niedermayer
            }
972 bb198e19 Michael Niedermayer
973 9dbcbd92 Michael Niedermayer
        }else{ /* s->pict_type==B_TYPE */
974 f2f6134b Michael Niedermayer
            cbp= get_p_cbp(s, block, motion_x, motion_y);
975 115329f1 Diego Biurrun
976 c5d309f2 Michael Niedermayer
            if ((cbp | motion_x | motion_y | s->dquant) == 0 && s->mv_type==MV_TYPE_16X16) {
977 115329f1 Diego Biurrun
                /* check if the B frames can skip it too, as we must skip it if we skip here
978 893bc624 Kevin Baragona
                   why didn't they just compress the skip-mb bits instead of reusing them ?! */
979 ee6f7861 Michael Niedermayer
                if(s->max_b_frames>0){
980
                    int i;
981 e71a4f74 Michael Niedermayer
                    int x,y, offset;
982
                    uint8_t *p_pic;
983
984
                    x= s->mb_x*16;
985
                    y= s->mb_y*16;
986
                    if(x+16 > s->width)  x= s->width-16;
987
                    if(y+16 > s->height) y= s->height-16;
988
989
                    offset= x + y*s->linesize;
990 1e491e29 Michael Niedermayer
                    p_pic= s->new_picture.data[0] + offset;
991 115329f1 Diego Biurrun
992 160d679c Mike Melanson
                    s->mb_skipped=1;
993 ee6f7861 Michael Niedermayer
                    for(i=0; i<s->max_b_frames; i++){
994 d697b89b Michael Niedermayer
                        uint8_t *b_pic;
995
                        int diff;
996 1e491e29 Michael Niedermayer
                        Picture *pic= s->reordered_input_picture[i+1];
997 d697b89b Michael Niedermayer
998 1e491e29 Michael Niedermayer
                        if(pic==NULL || pic->pict_type!=B_TYPE) break;
999 d697b89b Michael Niedermayer
1000 1e491e29 Michael Niedermayer
                        b_pic= pic->data[0] + offset + 16; //FIXME +16
1001 bb270c08 Diego Biurrun
                        diff= s->dsp.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
1002 e71a4f74 Michael Niedermayer
                        if(diff>s->qscale*70){ //FIXME check that 70 is optimal
1003 160d679c Mike Melanson
                            s->mb_skipped=0;
1004 ee6f7861 Michael Niedermayer
                            break;
1005
                        }
1006
                    }
1007
                }else
1008 115329f1 Diego Biurrun
                    s->mb_skipped=1;
1009 ee6f7861 Michael Niedermayer
1010 160d679c Mike Melanson
                if(s->mb_skipped==1){
1011 ee6f7861 Michael Niedermayer
                    /* skip macroblock */
1012
                    put_bits(&s->pb, 1, 1);
1013 92073d01 Michael Niedermayer
1014
                    if(interleaved_stats){
1015
                        s->misc_bits++;
1016
                        s->last_bits++;
1017
                    }
1018 ee6f7861 Michael Niedermayer
                    s->skip_count++;
1019 115329f1 Diego Biurrun
1020 ee6f7861 Michael Niedermayer
                    return;
1021
                }
1022 9dbcbd92 Michael Niedermayer
            }
1023 e71a4f74 Michael Niedermayer
1024 bb270c08 Diego Biurrun
            put_bits(&s->pb, 1, 0);     /* mb coded */
1025 f7190f73 Michael Niedermayer
            cbpc = cbp & 3;
1026
            cbpy = cbp >> 2;
1027
            cbpy ^= 0xf;
1028 9dbcbd92 Michael Niedermayer
            if(s->mv_type==MV_TYPE_16X16){
1029 c5d309f2 Michael Niedermayer
                if(s->dquant) cbpc+= 8;
1030 9dbcbd92 Michael Niedermayer
                put_bits(&s->pb,
1031
                        inter_MCBPC_bits[cbpc],
1032
                        inter_MCBPC_code[cbpc]);
1033 c5d309f2 Michael Niedermayer
1034 92073d01 Michael Niedermayer
                put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1035 c5d309f2 Michael Niedermayer
                if(s->dquant)
1036
                    put_bits(pb2, 2, dquant_code[s->dquant+2]);
1037 fcb48651 Michael Niedermayer
1038
                if(!s->progressive_sequence){
1039
                    if(cbp)
1040
                        put_bits(pb2, 1, s->interlaced_dct);
1041 bb198e19 Michael Niedermayer
                    put_bits(pb2, 1, 0);
1042 fcb48651 Michael Niedermayer
                }
1043 115329f1 Diego Biurrun
1044 92073d01 Michael Niedermayer
                if(interleaved_stats){
1045 4d2a4834 Michael Niedermayer
                    s->misc_bits+= get_bits_diff(s);
1046 92073d01 Michael Niedermayer
                }
1047 d6231b9e Michael Niedermayer
1048 9dbcbd92 Michael Niedermayer
                /* motion vectors: 16x16 mode */
1049 137c8468 Michael Niedermayer
                h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1050 115329f1 Diego Biurrun
1051 1b781f82 Michael Niedermayer
                ff_h263_encode_motion_vector(s, motion_x - pred_x,
1052
                                                motion_y - pred_y, s->f_code);
1053 bb198e19 Michael Niedermayer
            }else if(s->mv_type==MV_TYPE_FIELD){
1054
                if(s->dquant) cbpc+= 8;
1055
                put_bits(&s->pb,
1056
                        inter_MCBPC_bits[cbpc],
1057
                        inter_MCBPC_code[cbpc]);
1058
1059
                put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1060
                if(s->dquant)
1061
                    put_bits(pb2, 2, dquant_code[s->dquant+2]);
1062
1063
                assert(!s->progressive_sequence);
1064
                if(cbp)
1065
                    put_bits(pb2, 1, s->interlaced_dct);
1066
                put_bits(pb2, 1, 1);
1067 115329f1 Diego Biurrun
1068 bb198e19 Michael Niedermayer
                if(interleaved_stats){
1069
                    s->misc_bits+= get_bits_diff(s);
1070
                }
1071
1072
                /* motion vectors: 16x8 interlaced mode */
1073 137c8468 Michael Niedermayer
                h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1074 bb198e19 Michael Niedermayer
                pred_y /=2;
1075 115329f1 Diego Biurrun
1076 bb198e19 Michael Niedermayer
                put_bits(&s->pb, 1, s->field_select[0][0]);
1077
                put_bits(&s->pb, 1, s->field_select[0][1]);
1078 115329f1 Diego Biurrun
1079 1b781f82 Michael Niedermayer
                ff_h263_encode_motion_vector(s, s->mv[0][0][0] - pred_x,
1080
                                                s->mv[0][0][1] - pred_y, s->f_code);
1081
                ff_h263_encode_motion_vector(s, s->mv[0][1][0] - pred_x,
1082
                                                s->mv[0][1][1] - pred_y, s->f_code);
1083 9dbcbd92 Michael Niedermayer
            }else{
1084 bb198e19 Michael Niedermayer
                assert(s->mv_type==MV_TYPE_8X8);
1085 9dbcbd92 Michael Niedermayer
                put_bits(&s->pb,
1086 f7190f73 Michael Niedermayer
                        inter_MCBPC_bits[cbpc+16],
1087
                        inter_MCBPC_code[cbpc+16]);
1088 92073d01 Michael Niedermayer
                put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1089 9dbcbd92 Michael Niedermayer
1090 49943a18 Michael Niedermayer
                if(!s->progressive_sequence){
1091
                    if(cbp)
1092
                        put_bits(pb2, 1, s->interlaced_dct);
1093
                }
1094 115329f1 Diego Biurrun
1095 92073d01 Michael Niedermayer
                if(interleaved_stats){
1096 4d2a4834 Michael Niedermayer
                    s->misc_bits+= get_bits_diff(s);
1097 92073d01 Michael Niedermayer
                }
1098 9dbcbd92 Michael Niedermayer
1099
                for(i=0; i<4; i++){
1100
                    /* motion vectors: 8x8 mode*/
1101 137c8468 Michael Niedermayer
                    h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1102 9dbcbd92 Michael Niedermayer
1103 1b781f82 Michael Niedermayer
                    ff_h263_encode_motion_vector(s, s->current_picture.motion_val[0][ s->block_index[i] ][0] - pred_x,
1104
                                                    s->current_picture.motion_val[0][ s->block_index[i] ][1] - pred_y, s->f_code);
1105 9dbcbd92 Michael Niedermayer
                }
1106 cf8039b2 Michael Niedermayer
            }
1107 92073d01 Michael Niedermayer
1108 115329f1 Diego Biurrun
            if(interleaved_stats){
1109 4d2a4834 Michael Niedermayer
                s->mv_bits+= get_bits_diff(s);
1110 92073d01 Michael Niedermayer
            }
1111 098eefe1 Michael Niedermayer
1112 1b781f82 Michael Niedermayer
            mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb);
1113 92073d01 Michael Niedermayer
1114
            if(interleaved_stats){
1115 4d2a4834 Michael Niedermayer
                s->p_tex_bits+= get_bits_diff(s);
1116 9dbcbd92 Michael Niedermayer
            }
1117 66370d3f Michael Niedermayer
            s->f_count++;
1118 d6231b9e Michael Niedermayer
        }
1119
    } else {
1120 9dbcbd92 Michael Niedermayer
        int cbp;
1121 115329f1 Diego Biurrun
        int dc_diff[6];   //dc values with the dc prediction subtracted
1122 d6231b9e Michael Niedermayer
        int dir[6];  //prediction direction
1123
        int zigzag_last_index[6];
1124 bb270c08 Diego Biurrun
        uint8_t *scan_table[6];
1125 5c91a675 Zdenek Kabelac
        int i;
1126 d6231b9e Michael Niedermayer
1127
        for(i=0; i<6; i++){
1128 a7137a04 Michael Niedermayer
            dc_diff[i]= ff_mpeg4_pred_dc(s, i, block[i][0], &dir[i], 1);
1129 d6231b9e Michael Niedermayer
        }
1130
1131 8d8c0294 Michael Niedermayer
        if(s->flags & CODEC_FLAG_AC_PRED){
1132
            s->ac_pred= decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
1133
            if(!s->ac_pred)
1134
                restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
1135 d6231b9e Michael Niedermayer
        }else{
1136
            for(i=0; i<6; i++)
1137 2ad1516a Michael Niedermayer
                scan_table[i]= s->intra_scantable.permutated;
1138 d6231b9e Michael Niedermayer
        }
1139
1140
        /* compute cbp */
1141
        cbp = 0;
1142
        for (i = 0; i < 6; i++) {
1143
            if (s->block_last_index[i] >= 1)
1144
                cbp |= 1 << (5 - i);
1145
        }
1146
1147
        cbpc = cbp & 3;
1148
        if (s->pict_type == I_TYPE) {
1149 c5d309f2 Michael Niedermayer
            if(s->dquant) cbpc+=4;
1150 d6231b9e Michael Niedermayer
            put_bits(&s->pb,
1151
                intra_MCBPC_bits[cbpc],
1152
                intra_MCBPC_code[cbpc]);
1153
        } else {
1154 c5d309f2 Michael Niedermayer
            if(s->dquant) cbpc+=8;
1155 bb270c08 Diego Biurrun
            put_bits(&s->pb, 1, 0);     /* mb coded */
1156 d6231b9e Michael Niedermayer
            put_bits(&s->pb,
1157
                inter_MCBPC_bits[cbpc + 4],
1158
                inter_MCBPC_code[cbpc + 4]);
1159
        }
1160 92073d01 Michael Niedermayer
        put_bits(pb2, 1, s->ac_pred);
1161 d6231b9e Michael Niedermayer
        cbpy = cbp >> 2;
1162 92073d01 Michael Niedermayer
        put_bits(pb2, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1163 c5d309f2 Michael Niedermayer
        if(s->dquant)
1164
            put_bits(dc_pb, 2, dquant_code[s->dquant+2]);
1165 d6231b9e Michael Niedermayer
1166 fcb48651 Michael Niedermayer
        if(!s->progressive_sequence){
1167
            put_bits(dc_pb, 1, s->interlaced_dct);
1168
        }
1169
1170 92073d01 Michael Niedermayer
        if(interleaved_stats){
1171 4d2a4834 Michael Niedermayer
            s->misc_bits+= get_bits_diff(s);
1172 92073d01 Michael Niedermayer
        }
1173 098eefe1 Michael Niedermayer
1174 1b781f82 Michael Niedermayer
        mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb);
1175 d6231b9e Michael Niedermayer
1176 92073d01 Michael Niedermayer
        if(interleaved_stats){
1177 4d2a4834 Michael Niedermayer
            s->i_tex_bits+= get_bits_diff(s);
1178 92073d01 Michael Niedermayer
        }
1179 098eefe1 Michael Niedermayer
        s->i_count++;
1180
1181 d6231b9e Michael Niedermayer
        /* restore ac coeffs & last_index stuff if we messed them up with the prediction */
1182 8d8c0294 Michael Niedermayer
        if(s->ac_pred)
1183
            restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
1184 d6231b9e Michael Niedermayer
    }
1185
}
1186
1187 de6d9b64 Fabrice Bellard
void h263_encode_mb(MpegEncContext * s,
1188 bb270c08 Diego Biurrun
                    DCTELEM block[6][64],
1189
                    int motion_x, int motion_y)
1190 de6d9b64 Fabrice Bellard
{
1191
    int cbpc, cbpy, i, cbp, pred_x, pred_y;
1192 0c1a9eda Zdenek Kabelac
    int16_t pred_dc;
1193
    int16_t rec_intradc[6];
1194
    uint16_t *dc_ptr[6];
1195 d4f5d74a Garrick Meeker
    const int interleaved_stats= (s->flags&CODEC_FLAG_PASS1);
1196 9e4c6948 Michael Niedermayer
    const int dquant_code[5]= {1,0,9,2,3};
1197 115329f1 Diego Biurrun
1198 ef5b1b5a Juanjo
    //printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
1199 9dbcbd92 Michael Niedermayer
    if (!s->mb_intra) {
1200 ef5b1b5a Juanjo
        /* compute cbp */
1201 f2f6134b Michael Niedermayer
        cbp= get_p_cbp(s, block, motion_x, motion_y);
1202
1203 8cb5d4c0 Michael Niedermayer
        if ((cbp | motion_x | motion_y | s->dquant | (s->mv_type - MV_TYPE_16X16)) == 0) {
1204 9dbcbd92 Michael Niedermayer
            /* skip macroblock */
1205
            put_bits(&s->pb, 1, 1);
1206 d4f5d74a Garrick Meeker
            if(interleaved_stats){
1207
                s->misc_bits++;
1208
                s->last_bits++;
1209
            }
1210 65a24d0b Michael Niedermayer
            s->skip_count++;
1211
1212 9dbcbd92 Michael Niedermayer
            return;
1213
        }
1214 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 0);         /* mb coded */
1215 115329f1 Diego Biurrun
1216 9dbcbd92 Michael Niedermayer
        cbpc = cbp & 3;
1217
        cbpy = cbp >> 2;
1218 dba019da Michael Niedermayer
        if(s->alt_inter_vlc==0 || cbpc!=3)
1219
            cbpy ^= 0xF;
1220
        if(s->dquant) cbpc+= 8;
1221 f7190f73 Michael Niedermayer
        if(s->mv_type==MV_TYPE_16X16){
1222
            put_bits(&s->pb,
1223
                    inter_MCBPC_bits[cbpc],
1224
                    inter_MCBPC_code[cbpc]);
1225 9dbcbd92 Michael Niedermayer
1226 f7190f73 Michael Niedermayer
            put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1227
            if(s->dquant)
1228
                put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
1229 115329f1 Diego Biurrun
1230 f7190f73 Michael Niedermayer
            if(interleaved_stats){
1231
                s->misc_bits+= get_bits_diff(s);
1232
            }
1233 d4f5d74a Garrick Meeker
1234 f7190f73 Michael Niedermayer
            /* motion vectors: 16x16 mode */
1235 137c8468 Michael Niedermayer
            h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
1236 115329f1 Diego Biurrun
1237
            if (!s->umvplus) {
1238 1b781f82 Michael Niedermayer
                ff_h263_encode_motion_vector(s, motion_x - pred_x,
1239
                                                motion_y - pred_y, 1);
1240 f7190f73 Michael Niedermayer
            }
1241
            else {
1242
                h263p_encode_umotion(s, motion_x - pred_x);
1243
                h263p_encode_umotion(s, motion_y - pred_y);
1244
                if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
1245
                    /* To prevent Start Code emulation */
1246
                    put_bits(&s->pb,1,1);
1247
            }
1248
        }else{
1249
            put_bits(&s->pb,
1250
                    inter_MCBPC_bits[cbpc+16],
1251
                    inter_MCBPC_code[cbpc+16]);
1252
            put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1253
            if(s->dquant)
1254
                put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
1255
1256
            if(interleaved_stats){
1257
                s->misc_bits+= get_bits_diff(s);
1258
            }
1259
1260
            for(i=0; i<4; i++){
1261
                /* motion vectors: 8x8 mode*/
1262 137c8468 Michael Niedermayer
                h263_pred_motion(s, i, 0, &pred_x, &pred_y);
1263 f7190f73 Michael Niedermayer
1264 8d7ec294 Wolfgang Hesseler
                motion_x= s->current_picture.motion_val[0][ s->block_index[i] ][0];
1265
                motion_y= s->current_picture.motion_val[0][ s->block_index[i] ][1];
1266 115329f1 Diego Biurrun
                if (!s->umvplus) {
1267 1b781f82 Michael Niedermayer
                    ff_h263_encode_motion_vector(s, motion_x - pred_x,
1268
                                                    motion_y - pred_y, 1);
1269 f7190f73 Michael Niedermayer
                }
1270
                else {
1271
                    h263p_encode_umotion(s, motion_x - pred_x);
1272
                    h263p_encode_umotion(s, motion_y - pred_y);
1273
                    if (((motion_x - pred_x) == 1) && ((motion_y - pred_y) == 1))
1274
                        /* To prevent Start Code emulation */
1275
                        put_bits(&s->pb,1,1);
1276
                }
1277
            }
1278 9dbcbd92 Michael Niedermayer
        }
1279 d4f5d74a Garrick Meeker
1280
        if(interleaved_stats){
1281
            s->mv_bits+= get_bits_diff(s);
1282
        }
1283 ef5b1b5a Juanjo
    } else {
1284 68b94c35 Michael Niedermayer
        assert(s->mb_intra);
1285 115329f1 Diego Biurrun
1286 ef5b1b5a Juanjo
        cbp = 0;
1287 fb079883 Michael Niedermayer
        if (s->h263_aic) {
1288 ef5b1b5a Juanjo
            /* Predict DC */
1289 fb079883 Michael Niedermayer
            for(i=0; i<6; i++) {
1290 0c1a9eda Zdenek Kabelac
                int16_t level = block[i][0];
1291 fb079883 Michael Niedermayer
                int scale;
1292 115329f1 Diego Biurrun
1293 fb079883 Michael Niedermayer
                if(i<4) scale= s->y_dc_scale;
1294
                else    scale= s->c_dc_scale;
1295
1296 ef5b1b5a Juanjo
                pred_dc = h263_pred_dc(s, i, &dc_ptr[i]);
1297
                level -= pred_dc;
1298
                /* Quant */
1299 fb079883 Michael Niedermayer
                if (level >= 0)
1300
                    level = (level + (scale>>1))/scale;
1301 ef5b1b5a Juanjo
                else
1302 fb079883 Michael Niedermayer
                    level = (level - (scale>>1))/scale;
1303 115329f1 Diego Biurrun
1304 ef5b1b5a Juanjo
                /* AIC can change CBP */
1305
                if (level == 0 && s->block_last_index[i] == 0)
1306
                    s->block_last_index[i] = -1;
1307 fb079883 Michael Niedermayer
1308
                if(!s->modified_quant){
1309
                    if (level < -127)
1310
                        level = -127;
1311
                    else if (level > 127)
1312
                        level = 127;
1313
                }
1314
1315 ef5b1b5a Juanjo
                block[i][0] = level;
1316 115329f1 Diego Biurrun
                /* Reconstruction */
1317 fb079883 Michael Niedermayer
                rec_intradc[i] = scale*level + pred_dc;
1318 ef5b1b5a Juanjo
                /* Oddify */
1319
                rec_intradc[i] |= 1;
1320
                //if ((rec_intradc[i] % 2) == 0)
1321
                //    rec_intradc[i]++;
1322
                /* Clipping */
1323
                if (rec_intradc[i] < 0)
1324
                    rec_intradc[i] = 0;
1325
                else if (rec_intradc[i] > 2047)
1326
                    rec_intradc[i] = 2047;
1327 115329f1 Diego Biurrun
1328 ef5b1b5a Juanjo
                /* Update AC/DC tables */
1329
                *dc_ptr[i] = rec_intradc[i];
1330 fb079883 Michael Niedermayer
                if (s->block_last_index[i] >= 0)
1331
                    cbp |= 1 << (5 - i);
1332
            }
1333
        }else{
1334
            for(i=0; i<6; i++) {
1335
                /* compute cbp */
1336
                if (s->block_last_index[i] >= 1)
1337
                    cbp |= 1 << (5 - i);
1338 ef5b1b5a Juanjo
            }
1339
        }
1340 de6d9b64 Fabrice Bellard
1341 ef5b1b5a Juanjo
        cbpc = cbp & 3;
1342
        if (s->pict_type == I_TYPE) {
1343 9e4c6948 Michael Niedermayer
            if(s->dquant) cbpc+=4;
1344 ef5b1b5a Juanjo
            put_bits(&s->pb,
1345
                intra_MCBPC_bits[cbpc],
1346
                intra_MCBPC_code[cbpc]);
1347
        } else {
1348 9e4c6948 Michael Niedermayer
            if(s->dquant) cbpc+=8;
1349 bb270c08 Diego Biurrun
            put_bits(&s->pb, 1, 0);     /* mb coded */
1350 ef5b1b5a Juanjo
            put_bits(&s->pb,
1351
                inter_MCBPC_bits[cbpc + 4],
1352
                inter_MCBPC_code[cbpc + 4]);
1353
        }
1354
        if (s->h263_aic) {
1355
            /* XXX: currently, we do not try to use ac prediction */
1356 bb270c08 Diego Biurrun
            put_bits(&s->pb, 1, 0);     /* no AC prediction */
1357 ef5b1b5a Juanjo
        }
1358
        cbpy = cbp >> 2;
1359
        put_bits(&s->pb, cbpy_tab[cbpy][1], cbpy_tab[cbpy][0]);
1360 9e4c6948 Michael Niedermayer
        if(s->dquant)
1361
            put_bits(&s->pb, 2, dquant_code[s->dquant+2]);
1362 d4f5d74a Garrick Meeker
1363
        if(interleaved_stats){
1364
            s->misc_bits+= get_bits_diff(s);
1365
        }
1366 de6d9b64 Fabrice Bellard
    }
1367
1368 ef5b1b5a Juanjo
    for(i=0; i<6; i++) {
1369
        /* encode each block */
1370 44a2950f Michael Niedermayer
        h263_encode_block(s, block[i], i);
1371 115329f1 Diego Biurrun
1372 ef5b1b5a Juanjo
        /* Update INTRADC for decoding */
1373
        if (s->h263_aic && s->mb_intra) {
1374
            block[i][0] = rec_intradc[i];
1375 115329f1 Diego Biurrun
1376 ef5b1b5a Juanjo
        }
1377
    }
1378 d4f5d74a Garrick Meeker
1379
    if(interleaved_stats){
1380
        if (!s->mb_intra) {
1381
            s->p_tex_bits+= get_bits_diff(s);
1382
            s->f_count++;
1383
        }else{
1384
            s->i_tex_bits+= get_bits_diff(s);
1385
            s->i_count++;
1386
        }
1387
    }
1388 ef5b1b5a Juanjo
}
1389 bbed9259 Michael Niedermayer
#endif
1390 ef5b1b5a Juanjo
1391 cdb4e730 Michael Niedermayer
void ff_h263_loop_filter(MpegEncContext * s){
1392 332f9ac4 Michael Niedermayer
    int qp_c;
1393
    const int linesize  = s->linesize;
1394
    const int uvlinesize= s->uvlinesize;
1395
    const int xy = s->mb_y * s->mb_stride + s->mb_x;
1396
    uint8_t *dest_y = s->dest[0];
1397
    uint8_t *dest_cb= s->dest[1];
1398
    uint8_t *dest_cr= s->dest[2];
1399 115329f1 Diego Biurrun
1400 332f9ac4 Michael Niedermayer
//    if(s->pict_type==B_TYPE && !s->readable) return;
1401
1402
    /*
1403
       Diag Top
1404
       Left Center
1405
    */
1406
    if(!IS_SKIP(s->current_picture.mb_type[xy])){
1407
        qp_c= s->qscale;
1408
        s->dsp.h263_v_loop_filter(dest_y+8*linesize  , linesize, qp_c);
1409
        s->dsp.h263_v_loop_filter(dest_y+8*linesize+8, linesize, qp_c);
1410
    }else
1411
        qp_c= 0;
1412
1413
    if(s->mb_y){
1414
        int qp_dt, qp_t, qp_tc;
1415
1416
        if(IS_SKIP(s->current_picture.mb_type[xy-s->mb_stride]))
1417
            qp_t=0;
1418 115329f1 Diego Biurrun
        else
1419 332f9ac4 Michael Niedermayer
            qp_t= s->current_picture.qscale_table[xy-s->mb_stride];
1420
1421 115329f1 Diego Biurrun
        if(qp_c)
1422 332f9ac4 Michael Niedermayer
            qp_tc= qp_c;
1423
        else
1424
            qp_tc= qp_t;
1425 115329f1 Diego Biurrun
1426 332f9ac4 Michael Niedermayer
        if(qp_tc){
1427
            const int chroma_qp= s->chroma_qscale_table[qp_tc];
1428
            s->dsp.h263_v_loop_filter(dest_y  ,   linesize, qp_tc);
1429
            s->dsp.h263_v_loop_filter(dest_y+8,   linesize, qp_tc);
1430 115329f1 Diego Biurrun
1431 332f9ac4 Michael Niedermayer
            s->dsp.h263_v_loop_filter(dest_cb , uvlinesize, chroma_qp);
1432
            s->dsp.h263_v_loop_filter(dest_cr , uvlinesize, chroma_qp);
1433
        }
1434 115329f1 Diego Biurrun
1435 332f9ac4 Michael Niedermayer
        if(qp_t)
1436
            s->dsp.h263_h_loop_filter(dest_y-8*linesize+8  ,   linesize, qp_t);
1437 115329f1 Diego Biurrun
1438 332f9ac4 Michael Niedermayer
        if(s->mb_x){
1439
            if(qp_t || IS_SKIP(s->current_picture.mb_type[xy-1-s->mb_stride]))
1440
                qp_dt= qp_t;
1441
            else
1442
                qp_dt= s->current_picture.qscale_table[xy-1-s->mb_stride];
1443 115329f1 Diego Biurrun
1444 332f9ac4 Michael Niedermayer
            if(qp_dt){
1445
                const int chroma_qp= s->chroma_qscale_table[qp_dt];
1446
                s->dsp.h263_h_loop_filter(dest_y -8*linesize  ,   linesize, qp_dt);
1447
                s->dsp.h263_h_loop_filter(dest_cb-8*uvlinesize, uvlinesize, chroma_qp);
1448 b1114f18 Michael Niedermayer
                s->dsp.h263_h_loop_filter(dest_cr-8*uvlinesize, uvlinesize, chroma_qp);
1449 332f9ac4 Michael Niedermayer
            }
1450
        }
1451
    }
1452
1453
    if(qp_c){
1454
        s->dsp.h263_h_loop_filter(dest_y +8,   linesize, qp_c);
1455
        if(s->mb_y + 1 == s->mb_height)
1456
            s->dsp.h263_h_loop_filter(dest_y+8*linesize+8,   linesize, qp_c);
1457
    }
1458 115329f1 Diego Biurrun
1459 332f9ac4 Michael Niedermayer
    if(s->mb_x){
1460
        int qp_lc;
1461
        if(qp_c || IS_SKIP(s->current_picture.mb_type[xy-1]))
1462
            qp_lc= qp_c;
1463
        else
1464
            qp_lc= s->current_picture.qscale_table[xy-1];
1465 115329f1 Diego Biurrun
1466 332f9ac4 Michael Niedermayer
        if(qp_lc){
1467
            s->dsp.h263_h_loop_filter(dest_y,   linesize, qp_lc);
1468
            if(s->mb_y + 1 == s->mb_height){
1469
                const int chroma_qp= s->chroma_qscale_table[qp_lc];
1470
                s->dsp.h263_h_loop_filter(dest_y +8*  linesize,   linesize, qp_lc);
1471
                s->dsp.h263_h_loop_filter(dest_cb             , uvlinesize, chroma_qp);
1472
                s->dsp.h263_h_loop_filter(dest_cr             , uvlinesize, chroma_qp);
1473
            }
1474
        }
1475
    }
1476
}
1477
1478 0c1a9eda Zdenek Kabelac
static int h263_pred_dc(MpegEncContext * s, int n, uint16_t **dc_val_ptr)
1479 ef5b1b5a Juanjo
{
1480
    int x, y, wrap, a, c, pred_dc, scale;
1481 0c1a9eda Zdenek Kabelac
    int16_t *dc_val, *ac_val;
1482 ef5b1b5a Juanjo
1483
    /* find prediction */
1484
    if (n < 4) {
1485 137c8468 Michael Niedermayer
        x = 2 * s->mb_x + (n & 1);
1486
        y = 2 * s->mb_y + ((n & 2) >> 1);
1487
        wrap = s->b8_stride;
1488 ef5b1b5a Juanjo
        dc_val = s->dc_val[0];
1489
        ac_val = s->ac_val[0][0];
1490
        scale = s->y_dc_scale;
1491
    } else {
1492 137c8468 Michael Niedermayer
        x = s->mb_x;
1493
        y = s->mb_y;
1494
        wrap = s->mb_stride;
1495 ef5b1b5a Juanjo
        dc_val = s->dc_val[n - 4 + 1];
1496
        ac_val = s->ac_val[n - 4 + 1][0];
1497
        scale = s->c_dc_scale;
1498 de6d9b64 Fabrice Bellard
    }
1499 ef5b1b5a Juanjo
    /* B C
1500 115329f1 Diego Biurrun
     * A X
1501 ef5b1b5a Juanjo
     */
1502
    a = dc_val[(x - 1) + (y) * wrap];
1503
    c = dc_val[(x) + (y - 1) * wrap];
1504 115329f1 Diego Biurrun
1505 ef5b1b5a Juanjo
    /* No prediction outside GOB boundary */
1506 204b5c9e Michael Niedermayer
    if(s->first_slice_line && n!=3){
1507
        if(n!=2) c= 1024;
1508
        if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
1509
    }
1510 ef5b1b5a Juanjo
    pred_dc = 1024;
1511
    /* just DC prediction */
1512
    if (a != 1024 && c != 1024)
1513
        pred_dc = (a + c) >> 1;
1514
    else if (a != 1024)
1515
        pred_dc = a;
1516
    else
1517
        pred_dc = c;
1518 115329f1 Diego Biurrun
1519 ef5b1b5a Juanjo
    /* we assume pred is positive */
1520
    //pred_dc = (pred_dc + (scale >> 1)) / scale;
1521
    *dc_val_ptr = &dc_val[x + y * wrap];
1522
    return pred_dc;
1523 de6d9b64 Fabrice Bellard
}
1524
1525 5c91a675 Zdenek Kabelac
static void h263_pred_acdc(MpegEncContext * s, DCTELEM *block, int n)
1526 d140623f Juanjo
{
1527 9e15ad28 Juanjo
    int x, y, wrap, a, c, pred_dc, scale, i;
1528 0c1a9eda Zdenek Kabelac
    int16_t *dc_val, *ac_val, *ac_val1;
1529 d140623f Juanjo
1530
    /* find prediction */
1531
    if (n < 4) {
1532 137c8468 Michael Niedermayer
        x = 2 * s->mb_x + (n & 1);
1533
        y = 2 * s->mb_y + (n>> 1);
1534
        wrap = s->b8_stride;
1535 d140623f Juanjo
        dc_val = s->dc_val[0];
1536 9e15ad28 Juanjo
        ac_val = s->ac_val[0][0];
1537 d140623f Juanjo
        scale = s->y_dc_scale;
1538
    } else {
1539 137c8468 Michael Niedermayer
        x = s->mb_x;
1540
        y = s->mb_y;
1541
        wrap = s->mb_stride;
1542 d140623f Juanjo
        dc_val = s->dc_val[n - 4 + 1];
1543 9e15ad28 Juanjo
        ac_val = s->ac_val[n - 4 + 1][0];
1544 d140623f Juanjo
        scale = s->c_dc_scale;
1545
    }
1546 115329f1 Diego Biurrun
1547 9e15ad28 Juanjo
    ac_val += ((y) * wrap + (x)) * 16;
1548
    ac_val1 = ac_val;
1549 115329f1 Diego Biurrun
1550 d140623f Juanjo
    /* B C
1551 115329f1 Diego Biurrun
     * A X
1552 d140623f Juanjo
     */
1553
    a = dc_val[(x - 1) + (y) * wrap];
1554
    c = dc_val[(x) + (y - 1) * wrap];
1555 115329f1 Diego Biurrun
1556 ef5b1b5a Juanjo
    /* No prediction outside GOB boundary */
1557 68b94c35 Michael Niedermayer
    if(s->first_slice_line && n!=3){
1558
        if(n!=2) c= 1024;
1559
        if(n!=1 && s->mb_x == s->resync_mb_x) a= 1024;
1560
    }
1561 115329f1 Diego Biurrun
1562 d140623f Juanjo
    if (s->ac_pred) {
1563 68b94c35 Michael Niedermayer
        pred_dc = 1024;
1564 d140623f Juanjo
        if (s->h263_aic_dir) {
1565
            /* left prediction */
1566 9e15ad28 Juanjo
            if (a != 1024) {
1567
                ac_val -= 16;
1568
                for(i=1;i<8;i++) {
1569 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
1570 9e15ad28 Juanjo
                }
1571
                pred_dc = a;
1572 d140623f Juanjo
            }
1573
        } else {
1574
            /* top prediction */
1575 9e15ad28 Juanjo
            if (c != 1024) {
1576
                ac_val -= 16 * wrap;
1577
                for(i=1;i<8;i++) {
1578 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i   ]] += ac_val[i + 8];
1579 9e15ad28 Juanjo
                }
1580
                pred_dc = c;
1581 d140623f Juanjo
            }
1582
        }
1583 9e15ad28 Juanjo
    } else {
1584
        /* just DC prediction */
1585
        if (a != 1024 && c != 1024)
1586
            pred_dc = (a + c) >> 1;
1587
        else if (a != 1024)
1588
            pred_dc = a;
1589
        else
1590
            pred_dc = c;
1591 d140623f Juanjo
    }
1592 115329f1 Diego Biurrun
1593 9e15ad28 Juanjo
    /* we assume pred is positive */
1594
    block[0]=block[0]*scale + pred_dc;
1595 115329f1 Diego Biurrun
1596 9e15ad28 Juanjo
    if (block[0] < 0)
1597
        block[0] = 0;
1598 115329f1 Diego Biurrun
    else
1599 68b94c35 Michael Niedermayer
        block[0] |= 1;
1600 115329f1 Diego Biurrun
1601 9e15ad28 Juanjo
    /* Update AC/DC tables */
1602
    dc_val[(x) + (y) * wrap] = block[0];
1603 115329f1 Diego Biurrun
1604 d140623f Juanjo
    /* left copy */
1605
    for(i=1;i<8;i++)
1606 b0368839 Michael Niedermayer
        ac_val1[i    ] = block[s->dsp.idct_permutation[i<<3]];
1607 d140623f Juanjo
    /* top copy */
1608
    for(i=1;i<8;i++)
1609 b0368839 Michael Niedermayer
        ac_val1[8 + i] = block[s->dsp.idct_permutation[i   ]];
1610 d140623f Juanjo
}
1611
1612 137c8468 Michael Niedermayer
int16_t *h263_pred_motion(MpegEncContext * s, int block, int dir,
1613 de6d9b64 Fabrice Bellard
                        int *px, int *py)
1614
{
1615 137c8468 Michael Niedermayer
    int wrap;
1616 b40cd4e0 Michael Niedermayer
    int16_t *A, *B, *C, (*mot_val)[2];
1617
    static const int off[4]= {2, 1, 1, -1};
1618
1619
    wrap = s->b8_stride;
1620 137c8468 Michael Niedermayer
    mot_val = s->current_picture.motion_val[dir] + s->block_index[block];
1621 b40cd4e0 Michael Niedermayer
1622
    A = mot_val[ - 1];
1623
    /* special case for first (slice) line */
1624
    if (s->first_slice_line && block<3) {
1625 893bc624 Kevin Baragona
        // we can't just change some MVs to simulate that as we need them for the B frames (and ME)
1626 b40cd4e0 Michael Niedermayer
        // and if we ever support non rectangular objects than we need to do a few ifs here anyway :(
1627
        if(block==0){ //most common case
1628
            if(s->mb_x  == s->resync_mb_x){ //rare
1629
                *px= *py = 0;
1630
            }else if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
1631
                C = mot_val[off[block] - wrap];
1632
                if(s->mb_x==0){
1633
                    *px = C[0];
1634
                    *py = C[1];
1635
                }else{
1636
                    *px = mid_pred(A[0], 0, C[0]);
1637
                    *py = mid_pred(A[1], 0, C[1]);
1638
                }
1639
            }else{
1640
                *px = A[0];
1641
                *py = A[1];
1642
            }
1643
        }else if(block==1){
1644
            if(s->mb_x + 1 == s->resync_mb_x && s->h263_pred){ //rare
1645
                C = mot_val[off[block] - wrap];
1646
                *px = mid_pred(A[0], 0, C[0]);
1647
                *py = mid_pred(A[1], 0, C[1]);
1648
            }else{
1649
                *px = A[0];
1650
                *py = A[1];
1651
            }
1652
        }else{ /* block==2*/
1653
            B = mot_val[ - wrap];
1654
            C = mot_val[off[block] - wrap];
1655
            if(s->mb_x == s->resync_mb_x) //rare
1656
                A[0]=A[1]=0;
1657 115329f1 Diego Biurrun
1658 b40cd4e0 Michael Niedermayer
            *px = mid_pred(A[0], B[0], C[0]);
1659
            *py = mid_pred(A[1], B[1], C[1]);
1660
        }
1661
    } else {
1662
        B = mot_val[ - wrap];
1663
        C = mot_val[off[block] - wrap];
1664
        *px = mid_pred(A[0], B[0], C[0]);
1665
        *py = mid_pred(A[1], B[1], C[1]);
1666
    }
1667
    return *mot_val;
1668
}
1669
1670 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
1671 5e13a730 Michael Niedermayer
void ff_h263_encode_motion(MpegEncContext * s, int val, int f_code)
1672 de6d9b64 Fabrice Bellard
{
1673 a9c68306 Michael Niedermayer
    int range, l, bit_size, sign, code, bits;
1674 de6d9b64 Fabrice Bellard
1675
    if (val == 0) {
1676
        /* zero vector */
1677
        code = 0;
1678
        put_bits(&s->pb, mvtab[code][1], mvtab[code][0]);
1679
    } else {
1680 9dbcbd92 Michael Niedermayer
        bit_size = f_code - 1;
1681 de6d9b64 Fabrice Bellard
        range = 1 << bit_size;
1682
        /* modulo encoding */
1683 0ff93477 Michael Niedermayer
        l= INT_BIT - 6 - bit_size;
1684
        val = (val<<l)>>l;
1685 a9c68306 Michael Niedermayer
        sign = val>>31;
1686
        val= (val^sign)-sign;
1687
        sign&=1;
1688 de6d9b64 Fabrice Bellard
1689 3bf43d42 Michael Niedermayer
        val--;
1690
        code = (val >> bit_size) + 1;
1691
        bits = val & (range - 1);
1692 de6d9b64 Fabrice Bellard
1693 115329f1 Diego Biurrun
        put_bits(&s->pb, mvtab[code][1] + 1, (mvtab[code][0] << 1) | sign);
1694 de6d9b64 Fabrice Bellard
        if (bit_size > 0) {
1695
            put_bits(&s->pb, bit_size, bits);
1696
        }
1697
    }
1698
}
1699
1700 6dbd39fe Juanjo
/* Encode MV differences on H.263+ with Unrestricted MV mode */
1701
static void h263p_encode_umotion(MpegEncContext * s, int val)
1702
{
1703 115329f1 Diego Biurrun
    short sval = 0;
1704 6dbd39fe Juanjo
    short i = 0;
1705
    short n_bits = 0;
1706
    short temp_val;
1707
    int code = 0;
1708
    int tcode;
1709 115329f1 Diego Biurrun
1710 6dbd39fe Juanjo
    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 115329f1 Diego Biurrun
1718 6dbd39fe Juanjo
        sval = ((val < 0) ? (short)(-val):(short)val);
1719
        temp_val = sval;
1720 115329f1 Diego Biurrun
1721 6dbd39fe Juanjo
        while (temp_val != 0) {
1722
            temp_val = temp_val >> 1;
1723
            n_bits++;
1724
        }
1725 115329f1 Diego Biurrun
1726 6dbd39fe Juanjo
        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
1739 45870f57 Michael Niedermayer
static void init_mv_penalty_and_fcode(MpegEncContext *s)
1740
{
1741
    int f_code;
1742
    int mv;
1743 115329f1 Diego Biurrun
1744 bbed9259 Michael Niedermayer
    if(mv_penalty==NULL)
1745 30952237 Michael Niedermayer
        mv_penalty= av_mallocz( sizeof(uint8_t)*(MAX_FCODE+1)*(2*MAX_MV+1) );
1746 115329f1 Diego Biurrun
1747 45870f57 Michael Niedermayer
    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 7813d6a3 Michael Niedermayer
                bit_size = f_code - 1;
1756 45870f57 Michael Niedermayer
                range = 1 << bit_size;
1757
1758
                val=mv;
1759 115329f1 Diego Biurrun
                if (val < 0)
1760 45870f57 Michael Niedermayer
                    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 b7c51ebc Michael Niedermayer
                    len= mvtab[32][1] + av_log2(code>>5) + 2 + bit_size;
1767 45870f57 Michael Niedermayer
                }
1768
            }
1769
1770
            mv_penalty[f_code][mv+MAX_MV]= len;
1771
        }
1772
    }
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
    }
1779 9d2a0355 Michael Niedermayer
1780
    for(mv=0; mv<MAX_MV*2+1; mv++){
1781
        umv_fcode_tab[mv]= 1;
1782
    }
1783 45870f57 Michael Niedermayer
}
1784 bbed9259 Michael Niedermayer
#endif
1785 45870f57 Michael Niedermayer
1786 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
1787
1788 20695ec9 Falk H├╝ffner
static void init_uni_dc_tab(void)
1789 2a164b25 Michael Niedermayer
{
1790
    int level, uni_code, uni_len;
1791
1792 3bf43d42 Michael Niedermayer
    for(level=-256; level<256; level++){
1793 2a164b25 Michael Niedermayer
        int size, v, l;
1794
        /* find number of bits */
1795
        size = 0;
1796
        v = abs(level);
1797
        while (v) {
1798
            v >>= 1;
1799 bb270c08 Diego Biurrun
            size++;
1800 2a164b25 Michael Niedermayer
        }
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
        }
1819 67725183 Michael Niedermayer
        uni_DCtab_lum_bits[level+256]= uni_code;
1820
        uni_DCtab_lum_len [level+256]= uni_len;
1821 2a164b25 Michael Niedermayer
1822
        /* chrominance */
1823
        uni_code= DCtab_chrom[size][0];
1824
        uni_len = DCtab_chrom[size][1];
1825 115329f1 Diego Biurrun
1826 2a164b25 Michael Niedermayer
        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
        }
1834 67725183 Michael Niedermayer
        uni_DCtab_chrom_bits[level+256]= uni_code;
1835
        uni_DCtab_chrom_len [level+256]= uni_len;
1836 2a164b25 Michael Niedermayer
1837
    }
1838
}
1839
1840 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
1841
1842 bbed9259 Michael Niedermayer
#ifdef CONFIG_ENCODERS
1843 0c1a9eda Zdenek Kabelac
static void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
1844 3db48188 Michael Niedermayer
    int slevel, run, last;
1845 115329f1 Diego Biurrun
1846 3db48188 Michael Niedermayer
    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 115329f1 Diego Biurrun
1859 3db48188 Michael Niedermayer
                len_tab[index]= 100;
1860 115329f1 Diego Biurrun
1861 3db48188 Michael Niedermayer
                /* 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 115329f1 Diego Biurrun
1867 3db48188 Michael Niedermayer
                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 115329f1 Diego Biurrun
1884 3db48188 Michael Niedermayer
                    if(code!=rl->n && len < len_tab[index]){
1885
                        bits_tab[index]= bits;
1886
                        len_tab [index]= len;
1887
                    }
1888
                }
1889 115329f1 Diego Biurrun
#endif
1890 3db48188 Michael Niedermayer
#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 115329f1 Diego Biurrun
1903 3db48188 Michael Niedermayer
                    if(code!=rl->n && len < len_tab[index]){
1904
                        bits_tab[index]= bits;
1905
                        len_tab [index]= len;
1906
                    }
1907
                }
1908 115329f1 Diego Biurrun
#endif
1909
                /* ESC3 */
1910 3db48188 Michael Niedermayer
                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 115329f1 Diego Biurrun
1919 3db48188 Michael Niedermayer
                if(len < len_tab[index]){
1920
                    bits_tab[index]= bits;
1921
                    len_tab [index]= len;
1922
                }
1923
            }
1924
        }
1925
    }
1926
}
1927
1928 15c5fa7b Michael Niedermayer
static void init_uni_h263_rl_tab(RLTable *rl, uint32_t *bits_tab, uint8_t *len_tab){
1929
    int slevel, run, last;
1930 115329f1 Diego Biurrun
1931 15c5fa7b Michael Niedermayer
    assert(MAX_LEVEL >= 64);
1932
    assert(MAX_RUN   >= 63);
1933
1934
    for(slevel=-64; slevel<64; slevel++){
1935
        if(slevel==0) continue;
1936
        for(run=0; run<64; run++){
1937
            for(last=0; last<=1; last++){
1938
                const int index= UNI_MPEG4_ENC_INDEX(last, run, slevel+64);
1939
                int level= slevel < 0 ? -slevel : slevel;
1940
                int sign= slevel < 0 ? 1 : 0;
1941
                int bits, len, code;
1942 115329f1 Diego Biurrun
1943 15c5fa7b Michael Niedermayer
                len_tab[index]= 100;
1944 115329f1 Diego Biurrun
1945 15c5fa7b Michael Niedermayer
                /* ESC0 */
1946
                code= get_rl_index(rl, last, run, level);
1947
                bits= rl->table_vlc[code][0];
1948
                len=  rl->table_vlc[code][1];
1949
                bits=bits*2+sign; len++;
1950 115329f1 Diego Biurrun
1951 15c5fa7b Michael Niedermayer
                if(code!=rl->n && len < len_tab[index]){
1952
                    if(bits_tab) bits_tab[index]= bits;
1953
                    len_tab [index]= len;
1954
                }
1955
                /* ESC */
1956
                bits= rl->table_vlc[rl->n][0];
1957
                len = rl->table_vlc[rl->n][1];
1958
                bits=bits*2+last; len++;
1959
                bits=bits*64+run; len+=6;
1960
                bits=bits*256+(level&0xff); len+=8;
1961 115329f1 Diego Biurrun
1962 15c5fa7b Michael Niedermayer
                if(len < len_tab[index]){
1963
                    if(bits_tab) bits_tab[index]= bits;
1964
                    len_tab [index]= len;
1965
                }
1966
            }
1967
        }
1968
    }
1969
}
1970
1971 45870f57 Michael Niedermayer
void h263_encode_init(MpegEncContext *s)
1972 de6d9b64 Fabrice Bellard
{
1973
    static int done = 0;
1974
1975
    if (!done) {
1976
        done = 1;
1977 2a164b25 Michael Niedermayer
1978
        init_uni_dc_tab();
1979
1980 073c2593 Burkhard Plaum
        init_rl(&rl_inter, 1);
1981
        init_rl(&rl_intra, 1);
1982
        init_rl(&rl_intra_aic, 1);
1983 115329f1 Diego Biurrun
1984 3db48188 Michael Niedermayer
        init_uni_mpeg4_rl_tab(&rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1985
        init_uni_mpeg4_rl_tab(&rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1986 45870f57 Michael Niedermayer
1987 15c5fa7b Michael Niedermayer
        init_uni_h263_rl_tab(&rl_intra_aic, NULL, uni_h263_intra_aic_rl_len);
1988
        init_uni_h263_rl_tab(&rl_inter    , NULL, uni_h263_inter_rl_len);
1989
1990 45870f57 Michael Niedermayer
        init_mv_penalty_and_fcode(s);
1991 de6d9b64 Fabrice Bellard
    }
1992 1457ab52 Michael Niedermayer
    s->me.mv_penalty= mv_penalty; //FIXME exact table for msmpeg4 & h263p
1993 115329f1 Diego Biurrun
1994 15c5fa7b Michael Niedermayer
    s->intra_ac_vlc_length     =s->inter_ac_vlc_length     = uni_h263_inter_rl_len;
1995
    s->intra_ac_vlc_last_length=s->inter_ac_vlc_last_length= uni_h263_inter_rl_len + 128*64;
1996
    if(s->h263_aic){
1997
        s->intra_ac_vlc_length     = uni_h263_intra_aic_rl_len;
1998
        s->intra_ac_vlc_last_length= uni_h263_intra_aic_rl_len + 128*64;
1999
    }
2000
    s->ac_esc_length= 7+1+6+8;
2001
2002 9d2a0355 Michael Niedermayer
    // use fcodes >1 only for mpeg4 & h263 & h263p FIXME
2003 d7e9533a Michael Niedermayer
    switch(s->codec_id){
2004
    case CODEC_ID_MPEG4:
2005
        s->fcode_tab= fcode_tab;
2006
        s->min_qcoeff= -2048;
2007
        s->max_qcoeff=  2047;
2008 477ab036 Michael Niedermayer
        s->intra_ac_vlc_length     = uni_mpeg4_intra_rl_len;
2009
        s->intra_ac_vlc_last_length= uni_mpeg4_intra_rl_len + 128*64;
2010
        s->inter_ac_vlc_length     = uni_mpeg4_inter_rl_len;
2011
        s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
2012 67725183 Michael Niedermayer
        s->luma_dc_vlc_length= uni_DCtab_lum_len;
2013
        s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
2014 477ab036 Michael Niedermayer
        s->ac_esc_length= 7+2+1+6+1+12+1;
2015 9c3d33d6 Michael Niedermayer
        s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
2016
        s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
2017
2018 22ddd60b Michael Niedermayer
        if(s->flags & CODEC_FLAG_GLOBAL_HEADER){
2019 8b76418a Michael Niedermayer
2020 22ddd60b Michael Niedermayer
            s->avctx->extradata= av_malloc(1024);
2021 ed7debda Alex Beregszaszi
            init_put_bits(&s->pb, s->avctx->extradata, 1024);
2022 115329f1 Diego Biurrun
2023 59fa3f96 Michael Niedermayer
            if(!(s->workaround_bugs & FF_BUG_MS))
2024
                mpeg4_encode_visual_object_header(s);
2025 22ddd60b Michael Niedermayer
            mpeg4_encode_vol_header(s, 0, 0);
2026
2027
//            ff_mpeg4_stuffing(&s->pb); ?
2028
            flush_put_bits(&s->pb);
2029 fe455f33 Alex Beregszaszi
            s->avctx->extradata_size= (put_bits_count(&s->pb)+7)>>3;
2030 22ddd60b Michael Niedermayer
        }
2031 115329f1 Diego Biurrun
2032 d7e9533a Michael Niedermayer
        break;
2033
    case CODEC_ID_H263P:
2034 fb079883 Michael Niedermayer
        if(s->umvplus)
2035
            s->fcode_tab= umv_fcode_tab;
2036
        if(s->modified_quant){
2037
            s->min_qcoeff= -2047;
2038
            s->max_qcoeff=  2047;
2039
        }else{
2040
            s->min_qcoeff= -127;
2041
            s->max_qcoeff=  127;
2042
        }
2043 d7e9533a Michael Niedermayer
        break;
2044 115329f1 Diego Biurrun
        //Note for mpeg4 & h263 the dc-scale table will be set per frame as needed later
2045 d4f5d74a Garrick Meeker
    case CODEC_ID_FLV1:
2046
        if (s->h263_flv > 1) {
2047
            s->min_qcoeff= -1023;
2048
            s->max_qcoeff=  1023;
2049
        } else {
2050
            s->min_qcoeff= -127;
2051
            s->max_qcoeff=  127;
2052
        }
2053
        s->y_dc_scale_table=
2054
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
2055
        break;
2056 893bc624 Kevin Baragona
    default: //nothing needed - default table already set in mpegvideo.c
2057 e51d6d27 Michael Niedermayer
        s->min_qcoeff= -127;
2058 d7e9533a Michael Niedermayer
        s->max_qcoeff=  127;
2059 8f8402e4 Michael Niedermayer
        s->y_dc_scale_table=
2060
        s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
2061 d7e9533a Michael Niedermayer
    }
2062 de6d9b64 Fabrice Bellard
}
2063
2064 2ba8f6b8 Michael Niedermayer
/**
2065
 * encodes a 8x8 block.
2066
 * @param block the 8x8 block
2067
 * @param n block index (0-3 are luma, 4-5 are chroma)
2068
 */
2069 de6d9b64 Fabrice Bellard
static void h263_encode_block(MpegEncContext * s, DCTELEM * block, int n)
2070
{
2071 ef5b1b5a Juanjo
    int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
2072
    RLTable *rl;
2073 de6d9b64 Fabrice Bellard
2074 ef5b1b5a Juanjo
    rl = &rl_inter;
2075
    if (s->mb_intra && !s->h263_aic) {
2076 81401c1f Juanjo
        /* DC coef */
2077 e51d6d27 Michael Niedermayer
        level = block[0];
2078 de6d9b64 Fabrice Bellard
        /* 255 cannot be represented, so we clamp */
2079
        if (level > 254) {
2080
            level = 254;
2081
            block[0] = 254;
2082
        }
2083 81401c1f Juanjo
        /* 0 cannot be represented also */
2084 e51d6d27 Michael Niedermayer
        else if (level < 1) {
2085 81401c1f Juanjo
            level = 1;
2086
            block[0] = 1;
2087
        }
2088 17938602 Michael Niedermayer
        if (level == 128) //FIXME check rv10
2089 e51d6d27 Michael Niedermayer
            put_bits(&s->pb, 8, 0xff);
2090
        else
2091 65a24d0b Michael Niedermayer
            put_bits(&s->pb, 8, level);
2092 e51d6d27 Michael Niedermayer
        i = 1;
2093 de6d9b64 Fabrice Bellard
    } else {
2094 e51d6d27 Michael Niedermayer
        i = 0;
2095
        if (s->h263_aic && s->mb_intra)
2096
            rl = &rl_intra_aic;
2097 115329f1 Diego Biurrun
2098 dba019da Michael Niedermayer
        if(s->alt_inter_vlc && !s->mb_intra){
2099
            int aic_vlc_bits=0;
2100
            int inter_vlc_bits=0;
2101
            int wrong_pos=-1;
2102
            int aic_code;
2103 115329f1 Diego Biurrun
2104 dba019da Michael Niedermayer
            last_index = s->block_last_index[n];
2105
            last_non_zero = i - 1;
2106
            for (; i <= last_index; i++) {
2107
                j = s->intra_scantable.permutated[i];
2108
                level = block[j];
2109
                if (level) {
2110
                    run = i - last_non_zero - 1;
2111
                    last = (i == last_index);
2112 115329f1 Diego Biurrun
2113 ecfc44e6 Michael Niedermayer
                    if(level<0) level= -level;
2114 115329f1 Diego Biurrun
2115 dba019da Michael Niedermayer
                    code = get_rl_index(rl, last, run, level);
2116
                    aic_code = get_rl_index(&rl_intra_aic, last, run, level);
2117
                    inter_vlc_bits += rl->table_vlc[code][1]+1;
2118
                    aic_vlc_bits   += rl_intra_aic.table_vlc[aic_code][1]+1;
2119
2120
                    if (code == rl->n) {
2121 ecfc44e6 Michael Niedermayer
                        inter_vlc_bits += 1+6+8-1;
2122 115329f1 Diego Biurrun
                    }
2123 dba019da Michael Niedermayer
                    if (aic_code == rl_intra_aic.n) {
2124 ecfc44e6 Michael Niedermayer
                        aic_vlc_bits += 1+6+8-1;
2125 dba019da Michael Niedermayer
                        wrong_pos += run + 1;
2126
                    }else
2127
                        wrong_pos += wrong_run[aic_code];
2128
                    last_non_zero = i;
2129 115329f1 Diego Biurrun
                }
2130 dba019da Michael Niedermayer
            }
2131
            i = 0;
2132
            if(aic_vlc_bits < inter_vlc_bits && wrong_pos > 63)
2133
                rl = &rl_intra_aic;
2134
        }
2135 de6d9b64 Fabrice Bellard
    }
2136 115329f1 Diego Biurrun
2137 de6d9b64 Fabrice Bellard
    /* AC coefs */
2138
    last_index = s->block_last_index[n];
2139
    last_non_zero = i - 1;
2140
    for (; i <= last_index; i++) {
2141 2ad1516a Michael Niedermayer
        j = s->intra_scantable.permutated[i];
2142 ef5b1b5a Juanjo
        level = block[j];
2143
        if (level) {
2144
            run = i - last_non_zero - 1;
2145
            last = (i == last_index);
2146
            sign = 0;
2147
            slevel = level;
2148
            if (level < 0) {
2149
                sign = 1;
2150
                level = -level;
2151
            }
2152 de6d9b64 Fabrice Bellard
            code = get_rl_index(rl, last, run, level);
2153
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
2154
            if (code == rl->n) {
2155 44a2950f Michael Niedermayer
              if(s->h263_flv <= 1){
2156 de6d9b64 Fabrice Bellard
                put_bits(&s->pb, 1, last);
2157
                put_bits(&s->pb, 6, run);
2158 115329f1 Diego Biurrun
2159 e51d6d27 Michael Niedermayer
                assert(slevel != 0);
2160
2161 115329f1 Diego Biurrun
                if(level < 128)
2162 e51d6d27 Michael Niedermayer
                    put_bits(&s->pb, 8, slevel & 0xff);
2163
                else{
2164
                    put_bits(&s->pb, 8, 128);
2165
                    put_bits(&s->pb, 5, slevel & 0x1f);
2166
                    put_bits(&s->pb, 6, (slevel>>5)&0x3f);
2167
                }
2168 44a2950f Michael Niedermayer
              }else{
2169 65a24d0b Michael Niedermayer
                if(level < 64) { // 7-bit level
2170 44a2950f Michael Niedermayer
                        put_bits(&s->pb, 1, 0);
2171
                        put_bits(&s->pb, 1, last);
2172
                        put_bits(&s->pb, 6, run);
2173
2174
                        put_bits(&s->pb, 7, slevel & 0x7f);
2175
                    } else {
2176
                        /* 11-bit level */
2177
                        put_bits(&s->pb, 1, 1);
2178
                        put_bits(&s->pb, 1, last);
2179
                        put_bits(&s->pb, 6, run);
2180
2181
                        put_bits(&s->pb, 11, slevel & 0x7ff);
2182
                    }
2183
              }
2184 d4f5d74a Garrick Meeker
            } else {
2185
                put_bits(&s->pb, 1, sign);
2186
            }
2187
            last_non_zero = i;
2188
        }
2189
    }
2190
}
2191 bbed9259 Michael Niedermayer
#endif
2192 de6d9b64 Fabrice Bellard
2193 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
2194
2195 de6d9b64 Fabrice Bellard
/***************************************************/
2196 4d2858de Michael Niedermayer
/**
2197
 * add mpeg4 stuffing bits (01...1)
2198
 */
2199 92073d01 Michael Niedermayer
void ff_mpeg4_stuffing(PutBitContext * pbc)
2200 6f91bcd1 Michael Niedermayer
{
2201
    int length;
2202
    put_bits(pbc, 1, 0);
2203 fe455f33 Alex Beregszaszi
    length= (-put_bits_count(pbc))&7;
2204 92073d01 Michael Niedermayer
    if(length) put_bits(pbc, length, (1<<length)-1);
2205 6f91bcd1 Michael Niedermayer
}
2206
2207 91029be7 Michael Niedermayer
/* must be called before writing the header */
2208
void ff_set_mpeg4_time(MpegEncContext * s, int picture_number){
2209
    int time_div, time_mod;
2210
2211 9740beff Michael Niedermayer
    assert(s->current_picture_ptr->pts != AV_NOPTS_VALUE);
2212 c0df9d75 Michael Niedermayer
    s->time= s->current_picture_ptr->pts*s->avctx->time_base.num;
2213 e51f4948 Michael Niedermayer
2214 c0df9d75 Michael Niedermayer
    time_div= s->time/s->avctx->time_base.den;
2215
    time_mod= s->time%s->avctx->time_base.den;
2216 91029be7 Michael Niedermayer
2217
    if(s->pict_type==B_TYPE){
2218 bea669e5 Michael Niedermayer
        s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
2219 9ebb8e11 Michael Niedermayer
        assert(s->pb_time > 0 && s->pb_time < s->pp_time);
2220 91029be7 Michael Niedermayer
    }else{
2221
        s->last_time_base= s->time_base;
2222
        s->time_base= time_div;
2223
        s->pp_time= s->time - s->last_non_b_time;
2224
        s->last_non_b_time= s->time;
2225 1b781f82 Michael Niedermayer
        assert(picture_number==0 || s->pp_time > 0);
2226 91029be7 Michael Niedermayer
    }
2227
}
2228
2229 41773b73 Michael Niedermayer
static void mpeg4_encode_gop_header(MpegEncContext * s){
2230
    int hours, minutes, seconds;
2231 6ff6cff2 Michael Niedermayer
    int64_t time;
2232 115329f1 Diego Biurrun
2233 41773b73 Michael Niedermayer
    put_bits(&s->pb, 16, 0);
2234
    put_bits(&s->pb, 16, GOP_STARTCODE);
2235 115329f1 Diego Biurrun
2236 9740beff Michael Niedermayer
    time= s->current_picture_ptr->pts;
2237
    if(s->reordered_input_picture[1])
2238
        time= FFMIN(time, s->reordered_input_picture[1]->pts);
2239 c0df9d75 Michael Niedermayer
    time= time*s->avctx->time_base.num;
2240 6ff6cff2 Michael Niedermayer
2241 c0df9d75 Michael Niedermayer
    seconds= time/s->avctx->time_base.den;
2242 41773b73 Michael Niedermayer
    minutes= seconds/60; seconds %= 60;
2243
    hours= minutes/60; minutes %= 60;
2244
    hours%=24;
2245
2246
    put_bits(&s->pb, 5, hours);
2247
    put_bits(&s->pb, 6, minutes);
2248
    put_bits(&s->pb, 1, 1);
2249
    put_bits(&s->pb, 6, seconds);
2250 115329f1 Diego Biurrun
2251
    put_bits(&s->pb, 1, !!(s->flags&CODEC_FLAG_CLOSED_GOP));
2252 41773b73 Michael Niedermayer
    put_bits(&s->pb, 1, 0); //broken link == NO
2253 115329f1 Diego Biurrun
2254
    s->last_time_base= time / s->avctx->time_base.den;
2255 41773b73 Michael Niedermayer
2256
    ff_mpeg4_stuffing(&s->pb);
2257
}
2258
2259
static void mpeg4_encode_visual_object_header(MpegEncContext * s){
2260
    int profile_and_level_indication;
2261
    int vo_ver_id;
2262 115329f1 Diego Biurrun
2263 baced9f5 Michael Niedermayer
    if(s->avctx->profile != FF_PROFILE_UNKNOWN){
2264
        profile_and_level_indication = s->avctx->profile << 4;
2265
    }else if(s->max_b_frames || s->quarter_sample){
2266
        profile_and_level_indication= 0xF0; // adv simple
2267
    }else{
2268
        profile_and_level_indication= 0x00; // simple
2269
    }
2270
2271
    if(s->avctx->level != FF_LEVEL_UNKNOWN){
2272
        profile_and_level_indication |= s->avctx->level;
2273
    }else{
2274
        profile_and_level_indication |= 1; //level 1
2275
    }
2276
2277
    if(profile_and_level_indication>>4 == 0xF){
2278 41773b73 Michael Niedermayer
        vo_ver_id= 5;
2279
    }else{
2280
        vo_ver_id= 1;
2281
    }
2282 baced9f5 Michael Niedermayer
2283 41773b73 Michael Niedermayer
    //FIXME levels
2284
2285
    put_bits(&s->pb, 16, 0);
2286
    put_bits(&s->pb, 16, VOS_STARTCODE);
2287 3f6f76af Michael Niedermayer
2288 41773b73 Michael Niedermayer
    put_bits(&s->pb, 8, profile_and_level_indication);
2289 3f6f76af Michael Niedermayer
2290 41773b73 Michael Niedermayer
    put_bits(&s->pb, 16, 0);
2291
    put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
2292 115329f1 Diego Biurrun
2293 41773b73 Michael Niedermayer
    put_bits(&s->pb, 1, 1);
2294
        put_bits(&s->pb, 4, vo_ver_id);
2295
        put_bits(&s->pb, 3, 1); //priority
2296 115329f1 Diego Biurrun
2297 41773b73 Michael Niedermayer
    put_bits(&s->pb, 4, 1); //visual obj type== video obj
2298 115329f1 Diego Biurrun
2299 41773b73 Michael Niedermayer
    put_bits(&s->pb, 1, 0); //video signal type == no clue //FIXME
2300
2301
    ff_mpeg4_stuffing(&s->pb);
2302
}
2303
2304
static void mpeg4_encode_vol_header(MpegEncContext * s, int vo_number, int vol_number)
2305 de6d9b64 Fabrice Bellard
{
2306 41773b73 Michael Niedermayer
    int vo_ver_id;
2307 1ff662cc Michael Niedermayer
2308 41773b73 Michael Niedermayer
    if(s->max_b_frames || s->quarter_sample){
2309
        vo_ver_id= 5;
2310 f8af5cb5 Michael Niedermayer
        s->vo_type= ADV_SIMPLE_VO_TYPE;
2311
    }else{
2312 41773b73 Michael Niedermayer
        vo_ver_id= 1;
2313 f8af5cb5 Michael Niedermayer
        s->vo_type= SIMPLE_VO_TYPE;
2314
    }
2315 1ff662cc Michael Niedermayer
2316 10cd5815 Michael Niedermayer
    put_bits(&s->pb, 16, 0);
2317 41773b73 Michael Niedermayer
    put_bits(&s->pb, 16, 0x100 + vo_number);        /* video obj */
2318 10cd5815 Michael Niedermayer
    put_bits(&s->pb, 16, 0);
2319 41773b73 Michael Niedermayer
    put_bits(&s->pb, 16, 0x120 + vol_number);       /* video obj layer */
2320 10cd5815 Michael Niedermayer
2321 bb270c08 Diego Biurrun
    put_bits(&s->pb, 1, 0);             /* random access vol */
2322
    put_bits(&s->pb, 8, s->vo_type);    /* video obj type indication */
2323 59fa3f96 Michael Niedermayer
    if(s->workaround_bugs & FF_BUG_MS) {
2324 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 0);         /* is obj layer id= no */
2325 59fa3f96 Michael Niedermayer
    } else {
2326 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 1);         /* is obj layer id= yes */
2327
        put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
2328
        put_bits(&s->pb, 3, 1);         /* is obj layer priority */
2329 59fa3f96 Michael Niedermayer
    }
2330 115329f1 Diego Biurrun
2331 5ff85f1d Michael Niedermayer
    aspect_to_info(s, s->avctx->sample_aspect_ratio);
2332 5d3cea3a Michael Niedermayer
2333
    put_bits(&s->pb, 4, s->aspect_ratio_info);/* aspect ratio info */
2334 5ff85f1d Michael Niedermayer
    if (s->aspect_ratio_info == FF_ASPECT_EXTENDED){
2335
        put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
2336
        put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
2337 050fe8ba Alex Beregszaszi
    }
2338 1ff662cc Michael Niedermayer
2339 59fa3f96 Michael Niedermayer
    if(s->workaround_bugs & FF_BUG_MS) { //
2340 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 0);         /* vol control parameters= no @@@ */
2341 59fa3f96 Michael Niedermayer
    } else {
2342 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 1);         /* vol control parameters= yes */
2343
        put_bits(&s->pb, 2, 1);         /* chroma format YUV 420/YV12 */
2344 59fa3f96 Michael Niedermayer
        put_bits(&s->pb, 1, s->low_delay);
2345 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 0);         /* vbv parameters= no */
2346 59fa3f96 Michael Niedermayer
    }
2347 1ff662cc Michael Niedermayer
2348 bb270c08 Diego Biurrun
    put_bits(&s->pb, 2, RECT_SHAPE);    /* vol shape= rectangle */
2349
    put_bits(&s->pb, 1, 1);             /* marker bit */
2350 115329f1 Diego Biurrun
2351 c0df9d75 Michael Niedermayer
    put_bits(&s->pb, 16, s->avctx->time_base.den);
2352 10cd5815 Michael Niedermayer
    if (s->time_increment_bits < 1)
2353
        s->time_increment_bits = 1;
2354 bb270c08 Diego Biurrun
    put_bits(&s->pb, 1, 1);             /* marker bit */
2355
    put_bits(&s->pb, 1, 0);             /* fixed vop rate=no */
2356
    put_bits(&s->pb, 1, 1);             /* marker bit */
2357
    put_bits(&s->pb, 13, s->width);     /* vol width */
2358
    put_bits(&s->pb, 1, 1);             /* marker bit */
2359
    put_bits(&s->pb, 13, s->height);    /* vol height */
2360
    put_bits(&s->pb, 1, 1);             /* marker bit */
2361 fcb48651 Michael Niedermayer
    put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
2362 bb270c08 Diego Biurrun
    put_bits(&s->pb, 1, 1);             /* obmc disable */
2363 10cd5815 Michael Niedermayer
    if (vo_ver_id == 1) {
2364 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, s->vol_sprite_usage);       /* sprite enable */
2365 41773b73 Michael Niedermayer
    }else{
2366 bb270c08 Diego Biurrun
        put_bits(&s->pb, 2, s->vol_sprite_usage);       /* sprite enable */
2367 10cd5815 Michael Niedermayer
    }
2368 115329f1 Diego Biurrun
2369 bb270c08 Diego Biurrun
    put_bits(&s->pb, 1, 0);             /* not 8 bit == false */
2370
    put_bits(&s->pb, 1, s->mpeg_quant); /* quant type= (0=h263 style)*/
2371 d6eb3c50 Michael Niedermayer
2372
    if(s->mpeg_quant){
2373
        ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
2374
        ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
2375
    }
2376 87f8cab4 Michael Niedermayer
2377 10cd5815 Michael Niedermayer
    if (vo_ver_id != 1)
2378 1457ab52 Michael Niedermayer
        put_bits(&s->pb, 1, s->quarter_sample);
2379 bb270c08 Diego Biurrun
    put_bits(&s->pb, 1, 1);             /* complexity estimation disable */
2380 92073d01 Michael Niedermayer
    s->resync_marker= s->rtp_mode;
2381
    put_bits(&s->pb, 1, s->resync_marker ? 0 : 1);/* resync marker disable */
2382
    put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
2383
    if(s->data_partitioning){
2384 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 0);         /* no rvlc */
2385 92073d01 Michael Niedermayer
    }
2386
2387 10cd5815 Michael Niedermayer
    if (vo_ver_id != 1){
2388 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, 0);         /* newpred */
2389
        put_bits(&s->pb, 1, 0);         /* reduced res vop */
2390 10cd5815 Michael Niedermayer
    }
2391 bb270c08 Diego Biurrun
    put_bits(&s->pb, 1, 0);             /* scalability */
2392 115329f1 Diego Biurrun
2393 92073d01 Michael Niedermayer
    ff_mpeg4_stuffing(&s->pb);
2394 49c9325f Michael Niedermayer
2395 5596c60c Michael Niedermayer
    /* user data */
2396 b0368839 Michael Niedermayer
    if(!(s->flags & CODEC_FLAG_BITEXACT)){
2397 5596c60c Michael Niedermayer
        put_bits(&s->pb, 16, 0);
2398 bb270c08 Diego Biurrun
        put_bits(&s->pb, 16, 0x1B2);    /* user_data */
2399
        ff_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
2400 5596c60c Michael Niedermayer
    }
2401 10cd5815 Michael Niedermayer
}
2402
2403
/* write mpeg4 VOP header */
2404
void mpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
2405
{
2406 9dbcbd92 Michael Niedermayer
    int time_incr;
2407
    int time_div, time_mod;
2408 115329f1 Diego Biurrun
2409 92073d01 Michael Niedermayer
    if(s->pict_type==I_TYPE){
2410 70ac76c0 Michael Niedermayer
        if(!(s->flags&CODEC_FLAG_GLOBAL_HEADER)){
2411 9cd81798 Michael Niedermayer
            if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT) //HACK, the reference sw is buggy
2412 3f6f76af Michael Niedermayer
                mpeg4_encode_visual_object_header(s);
2413 9cd81798 Michael Niedermayer
            if(s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number==0) //HACK, the reference sw is buggy
2414 3f6f76af Michael Niedermayer
                mpeg4_encode_vol_header(s, 0, 0);
2415 41773b73 Michael Niedermayer
        }
2416 59fa3f96 Michael Niedermayer
        if(!(s->workaround_bugs & FF_BUG_MS))
2417
            mpeg4_encode_gop_header(s);
2418 92073d01 Michael Niedermayer
    }
2419 115329f1 Diego Biurrun
2420 4d2858de Michael Niedermayer
    s->partitioned_frame= s->data_partitioning && s->pict_type!=B_TYPE;
2421
2422 c0df9d75 Michael Niedermayer
//printf("num:%d rate:%d base:%d\n", s->picture_number, s->time_base.den, FRAME_RATE_BASE);
2423 115329f1 Diego Biurrun
2424 bb270c08 Diego Biurrun
    put_bits(&s->pb, 16, 0);                /* vop header */
2425
    put_bits(&s->pb, 16, VOP_STARTCODE);    /* vop header */
2426
    put_bits(&s->pb, 2, s->pict_type - 1);  /* pict type: I = 0 , P = 1 */
2427 9dbcbd92 Michael Niedermayer
2428 cf5c5c8f Michael Niedermayer
    assert(s->time>=0);
2429 c0df9d75 Michael Niedermayer
    time_div= s->time/s->avctx->time_base.den;
2430
    time_mod= s->time%s->avctx->time_base.den;
2431 9dbcbd92 Michael Niedermayer
    time_incr= time_div - s->last_time_base;
2432 6c205de2 Michael Niedermayer
    assert(time_incr >= 0);
2433 9dbcbd92 Michael Niedermayer
    while(time_incr--)
2434
        put_bits(&s->pb, 1, 1);
2435 115329f1 Diego Biurrun
2436 de6d9b64 Fabrice Bellard
    put_bits(&s->pb, 1, 0);
2437
2438 bb270c08 Diego Biurrun
    put_bits(&s->pb, 1, 1);                             /* marker */
2439
    put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
2440
    put_bits(&s->pb, 1, 1);                             /* marker */
2441
    put_bits(&s->pb, 1, 1);                             /* vop coded */
2442 115329f1 Diego Biurrun
    if (    s->pict_type == P_TYPE
2443 10cd5815 Michael Niedermayer
        || (s->pict_type == S_TYPE && s->vol_sprite_usage==GMC_SPRITE)) {
2444 bb270c08 Diego Biurrun
        put_bits(&s->pb, 1, s->no_rounding);    /* rounding type */
2445 de6d9b64 Fabrice Bellard
    }
2446 bb270c08 Diego Biurrun
    put_bits(&s->pb, 3, 0);     /* intra dc VLC threshold */
2447 fcb48651 Michael Niedermayer
    if(!s->progressive_sequence){
2448 2be9f03a Michael Niedermayer
         put_bits(&s->pb, 1, s->current_picture_ptr->top_field_first);
2449 fcb48651 Michael Niedermayer
         put_bits(&s->pb, 1, s->alternate_scan);
2450
    }
2451 10cd5815 Michael Niedermayer
    //FIXME sprite stuff
2452 de6d9b64 Fabrice Bellard
2453
    put_bits(&s->pb, 5, s->qscale);
2454
2455
    if (s->pict_type != I_TYPE)
2456 bb270c08 Diego Biurrun
        put_bits(&s->pb, 3, s->f_code); /* fcode_for */
2457 10cd5815 Michael Niedermayer
    if (s->pict_type == B_TYPE)
2458 bb270c08 Diego Biurrun
        put_bits(&s->pb, 3, s->b_code); /* fcode_back */
2459 de6d9b64 Fabrice Bellard
    //    printf("****frame %d\n", picture_number);
2460
}
2461
2462 7604246d Wolfgang Hesseler
#endif //CONFIG_ENCODERS
2463
2464 4d2858de Michael Niedermayer
/**
2465 554daa24 Michael Niedermayer
 * set qscale and update qscale dependant variables.
2466 4d2858de Michael Niedermayer
 */
2467 554daa24 Michael Niedermayer
void ff_set_qscale(MpegEncContext * s, int qscale)
2468 de6d9b64 Fabrice Bellard
{
2469 554daa24 Michael Niedermayer
    if (qscale < 1)
2470
        qscale = 1;
2471
    else if (qscale > 31)
2472
        qscale = 31;
2473 115329f1 Diego Biurrun
2474 554daa24 Michael Niedermayer
    s->qscale = qscale;
2475
    s->chroma_qscale= s->chroma_qscale_table[qscale];
2476 5482970b Michael Niedermayer
2477 554daa24 Michael Niedermayer
    s->y_dc_scale= s->y_dc_scale_table[ qscale ];
2478 332f9ac4 Michael Niedermayer
    s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
2479 de6d9b64 Fabrice Bellard
}
2480
2481 2ba8f6b8 Michael Niedermayer
/**
2482
 * predicts the dc.
2483 a7137a04 Michael Niedermayer
 * encoding quantized level -> quantized diff
2484 115329f1 Diego Biurrun
 * decoding quantized diff -> quantized level
2485 2ba8f6b8 Michael Niedermayer
 * @param n block index (0-3 are luma, 4-5 are chroma)
2486
 * @param dir_ptr pointer to an integer where the prediction direction will be stored
2487
 */
2488 a7137a04 Michael Niedermayer
static inline int ff_mpeg4_pred_dc(MpegEncContext * s, int n, int level, int *dir_ptr, int encoding)
2489 de6d9b64 Fabrice Bellard
{
2490 a7137a04 Michael Niedermayer
    int a, b, c, wrap, pred, scale, ret;
2491 0c1a9eda Zdenek Kabelac
    uint16_t *dc_val;
2492 de6d9b64 Fabrice Bellard
2493
    /* find prediction */
2494
    if (n < 4) {
2495 bb270c08 Diego Biurrun
        scale = s->y_dc_scale;
2496 de6d9b64 Fabrice Bellard
    } else {
2497 bb270c08 Diego Biurrun
        scale = s->c_dc_scale;
2498 de6d9b64 Fabrice Bellard
    }
2499 6d71b382 Michael Niedermayer
    if(IS_3IV1)
2500
        scale= 8;
2501
2502 4278e7a6 Michael Niedermayer
    wrap= s->block_wrap[n];
2503
    dc_val = s->dc_val[0] + s->block_index[n];
2504 de6d9b64 Fabrice Bellard
2505
    /* B C
2506 115329f1 Diego Biurrun
     * A X
2507 de6d9b64 Fabrice Bellard
     */
2508 4278e7a6 Michael Niedermayer
    a = dc_val[ - 1];
2509
    b = dc_val[ - 1 - wrap];
2510
    c = dc_val[ - wrap];
2511 de6d9b64 Fabrice Bellard
2512 893bc624 Kevin Baragona
    /* outside slice handling (we can't do that by memset as we need the dc for error resilience) */
2513 4d2858de Michael Niedermayer
    if(s->first_slice_line && n!=3){
2514
        if(n!=2) b=c= 1024;
2515
        if(n!=1 && s->mb_x == s->resync_mb_x) b=a= 1024;
2516
    }
2517
    if(s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y+1){
2518
        if(n==0 || n==4 || n==5)
2519
            b=1024;
2520
    }
2521
2522 de6d9b64 Fabrice Bellard
    if (abs(a - b) < abs(b - c)) {
2523 bb270c08 Diego Biurrun
        pred = c;
2524 de6d9b64 Fabrice Bellard
        *dir_ptr = 1; /* top */
2525
    } else {
2526 bb270c08 Diego Biurrun
        pred = a;
2527 de6d9b64 Fabrice Bellard
        *dir_ptr = 0; /* left */
2528
    }
2529
    /* we assume pred is positive */
2530 d4961b35 BERO
    pred = FASTDIV((pred + (scale >> 1)), scale);
2531 de6d9b64 Fabrice Bellard
2532 a7137a04 Michael Niedermayer
    if(encoding){
2533
        ret = level - pred;
2534
    }else{
2535
        level += pred;
2536
        ret= level;
2537
        if(s->error_resilience>=3){
2538
            if(level<0){
2539
                av_log(s->avctx, AV_LOG_ERROR, "dc<0 at %dx%d\n", s->mb_x, s->mb_y);
2540
                return -1;
2541
            }
2542
            if(level*scale > 2048 + scale){
2543
                av_log(s->avctx, AV_LOG_ERROR, "dc overflow at %dx%d\n", s->mb_x, s->mb_y);
2544
                return -1;
2545
            }
2546
        }
2547
    }
2548
    level *=scale;
2549 4ccde216 Michael Niedermayer
    if(level&(~2047)){
2550 115329f1 Diego Biurrun
        if(level<0)
2551 4ccde216 Michael Niedermayer
            level=0;
2552
        else if(!(s->workaround_bugs&FF_BUG_DC_CLIP))
2553
            level=2047;
2554
    }
2555 a7137a04 Michael Niedermayer
    dc_val[0]= level;
2556 de6d9b64 Fabrice Bellard
2557 a7137a04 Michael Niedermayer
    return ret;
2558 de6d9b64 Fabrice Bellard
}
2559
2560 2ba8f6b8 Michael Niedermayer
/**
2561
 * predicts the ac.
2562
 * @param n block index (0-3 are luma, 4-5 are chroma)
2563
 * @param dir the ac prediction direction
2564
 */
2565 0e15384d Michael Niedermayer
void mpeg4_pred_ac(MpegEncContext * s, DCTELEM *block, int n,
2566 de6d9b64 Fabrice Bellard
                   int dir)
2567
{
2568 4278e7a6 Michael Niedermayer
    int i;
2569 0c1a9eda Zdenek Kabelac
    int16_t *ac_val, *ac_val1;
2570 1e491e29 Michael Niedermayer
    int8_t * const qscale_table= s->current_picture.qscale_table;
2571 de6d9b64 Fabrice Bellard
2572
    /* find prediction */
2573 4278e7a6 Michael Niedermayer
    ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2574 de6d9b64 Fabrice Bellard
    ac_val1 = ac_val;
2575
    if (s->ac_pred) {
2576
        if (dir == 0) {
2577 7bc9090a Michael Niedermayer
            const int xy= s->mb_x-1 + s->mb_y*s->mb_stride;
2578 de6d9b64 Fabrice Bellard
            /* left prediction */
2579
            ac_val -= 16;
2580 115329f1 Diego Biurrun
2581 1e491e29 Michael Niedermayer
            if(s->mb_x==0 || s->qscale == qscale_table[xy] || n==1 || n==3){
2582 3e6a20a0 Michael Niedermayer
                /* same qscale */
2583
                for(i=1;i<8;i++) {
2584 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i<<3]] += ac_val[i];
2585 3e6a20a0 Michael Niedermayer
                }
2586
            }else{
2587
                /* different qscale, we must rescale */
2588
                for(i=1;i<8;i++) {
2589 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i<<3]] += ROUNDED_DIV(ac_val[i]*qscale_table[xy], s->qscale);
2590 3e6a20a0 Michael Niedermayer
                }
2591 de6d9b64 Fabrice Bellard
            }
2592
        } else {
2593 7bc9090a Michael Niedermayer
            const int xy= s->mb_x + s->mb_y*s->mb_stride - s->mb_stride;
2594 de6d9b64 Fabrice Bellard
            /* top prediction */
2595 4278e7a6 Michael Niedermayer
            ac_val -= 16 * s->block_wrap[n];
2596 3e6a20a0 Michael Niedermayer
2597 1e491e29 Michael Niedermayer
            if(s->mb_y==0 || s->qscale == qscale_table[xy] || n==2 || n==3){
2598 3e6a20a0 Michael Niedermayer
                /* same qscale */
2599
                for(i=1;i<8;i++) {
2600 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i]] += ac_val[i + 8];
2601 3e6a20a0 Michael Niedermayer
                }
2602
            }else{
2603
                /* different qscale, we must rescale */
2604
                for(i=1;i<8;i++) {
2605 b0368839 Michael Niedermayer
                    block[s->dsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8]*qscale_table[xy], s->qscale);
2606 3e6a20a0 Michael Niedermayer
                }
2607 de6d9b64 Fabrice Bellard
            }
2608
        }
2609
    }
2610
    /* left copy */
2611
    for(i=1;i<8;i++)
2612 b0368839 Michael Niedermayer
        ac_val1[i    ] = block[s->dsp.idct_permutation[i<<3]];
2613 d930ef19 Michael Niedermayer
2614 de6d9b64 Fabrice Bellard
    /* top copy */
2615
    for(i=1;i<8;i++)
2616 b0368839 Michael Niedermayer
        ac_val1[8 + i] = block[s->dsp.idct_permutation[i   ]];
2617 d930ef19 Michael Niedermayer
2618 de6d9b64 Fabrice Bellard
}
2619
2620 7604246d Wolfgang Hesseler
#ifdef CONFIG_ENCODERS
2621
2622 2ba8f6b8 Michael Niedermayer
/**
2623
 * encodes the dc value.
2624
 * @param n block index (0-3 are luma, 4-5 are chroma)
2625
 */
2626 92073d01 Michael Niedermayer
static inline void mpeg4_encode_dc(PutBitContext * s, int level, int n)
2627 d6231b9e Michael Niedermayer
{
2628 2a164b25 Michael Niedermayer
#if 1
2629 92073d01 Michael Niedermayer
//    if(level<-255 || level>255) printf("dc overflow\n");
2630 2a164b25 Michael Niedermayer
    level+=256;
2631
    if (n < 4) {
2632 bb270c08 Diego Biurrun
        /* luminance */
2633
        put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
2634 2a164b25 Michael Niedermayer </