GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/mpeg4videoenc.c Lines: 617 784 78.7 %
Date: 2020-09-21 17:35:45 Branches: 301 438 68.7 %

Line Branch Exec Source
1
/*
2
 * MPEG-4 encoder
3
 * Copyright (c) 2000,2001 Fabrice Bellard
4
 * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at>
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg 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.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#include "libavutil/attributes.h"
24
#include "libavutil/log.h"
25
#include "libavutil/opt.h"
26
#include "mpegutils.h"
27
#include "mpegvideo.h"
28
#include "h263.h"
29
#include "mpeg4video.h"
30
#include "profiles.h"
31
32
/* The uni_DCtab_* tables below contain unified bits+length tables to encode DC
33
 * differences in MPEG-4. Unified in the sense that the specification specifies
34
 * this encoding in several steps. */
35
static uint8_t  uni_DCtab_lum_len[512];
36
static uint8_t  uni_DCtab_chrom_len[512];
37
static uint16_t uni_DCtab_lum_bits[512];
38
static uint16_t uni_DCtab_chrom_bits[512];
39
40
/* Unified encoding tables for run length encoding of coefficients.
41
 * Unified in the sense that the specification specifies the encoding in several steps. */
42
static uint32_t uni_mpeg4_intra_rl_bits[64 * 64 * 2 * 2];
43
static uint8_t  uni_mpeg4_intra_rl_len[64 * 64 * 2 * 2];
44
static uint32_t uni_mpeg4_inter_rl_bits[64 * 64 * 2 * 2];
45
static uint8_t  uni_mpeg4_inter_rl_len[64 * 64 * 2 * 2];
46
47
//#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 + (run) * 256 + (level))
48
//#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) + (level) * 64)
49
#define UNI_MPEG4_ENC_INDEX(last, run, level) ((last) * 128 * 64 + (run) * 128 + (level))
50
51
/* MPEG-4
52
 * inter
53
 * max level: 24/6
54
 * max run: 53/63
55
 *
56
 * intra
57
 * max level: 53/16
58
 * max run: 29/41
59
 */
60
61
/**
62
 * Return the number of bits that encoding the 8x8 block in block would need.
63
 * @param[in]  block_last_index last index in scantable order that refers to a non zero element in block.
64
 */
65
585288
static inline int get_block_rate(MpegEncContext *s, int16_t block[64],
66
                                 int block_last_index, uint8_t scantable[64])
67
{
68
585288
    int last = 0;
69
    int j;
70
585288
    int rate = 0;
71
72
12547868
    for (j = 1; j <= block_last_index; j++) {
73
11962580
        const int index = scantable[j];
74
11962580
        int level = block[index];
75
11962580
        if (level) {
76
5655201
            level += 64;
77
5655201
            if ((level & (~127)) == 0) {
78
5653593
                if (j < block_last_index)
79
5215921
                    rate += s->intra_ac_vlc_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
80
                else
81
437672
                    rate += s->intra_ac_vlc_last_length[UNI_AC_ENC_INDEX(j - last - 1, level)];
82
            } else
83
1608
                rate += s->ac_esc_length;
84
85
5655201
            last = j;
86
        }
87
    }
88
89
585288
    return rate;
90
}
91
92
/**
93
 * Restore the ac coefficients in block that have been changed by decide_ac_pred().
94
 * This function also restores s->block_last_index.
95
 * @param[in,out] block MB coefficients, these will be restored
96
 * @param[in] dir ac prediction direction for each 8x8 block
97
 * @param[out] st scantable for each 8x8 block
98
 * @param[in] zigzag_last_index index referring to the last non zero coefficient in zigzag order
99
 */
100
48774
static inline void restore_ac_coeffs(MpegEncContext *s, int16_t block[6][64],
101
                                     const int dir[6], uint8_t *st[6],
102
                                     const int zigzag_last_index[6])
103
{
104
    int i, n;
105
48774
    memcpy(s->block_last_index, zigzag_last_index, sizeof(int) * 6);
106
107
341418
    for (n = 0; n < 6; n++) {
108
292644
        int16_t *ac_val = &s->ac_val[0][0][0] + s->block_index[n] * 16;
109
110
292644
        st[n] = s->intra_scantable.permutated;
111
292644
        if (dir[n]) {
112
            /* top prediction */
113
641160
            for (i = 1; i < 8; i++)
114
561015
                block[n][s->idsp.idct_permutation[i]] = ac_val[i + 8];
115
        } else {
116
            /* left prediction */
117
1699992
            for (i = 1; i < 8; i++)
118
1487493
                block[n][s->idsp.idct_permutation[i << 3]] = ac_val[i];
119
        }
120
    }
121
48774
}
122
123
/**
124
 * Return the optimal value (0 or 1) for the ac_pred element for the given MB in MPEG-4.
125
 * This function will also update s->block_last_index and s->ac_val.
126
 * @param[in,out] block MB coefficients, these will be updated if 1 is returned
127
 * @param[in] dir ac prediction direction for each 8x8 block
128
 * @param[out] st scantable for each 8x8 block
129
 * @param[out] zigzag_last_index index referring to the last non zero coefficient in zigzag order
130
 */
131
48774
static inline int decide_ac_pred(MpegEncContext *s, int16_t block[6][64],
132
                                 const int dir[6], uint8_t *st[6],
133
                                 int zigzag_last_index[6])
134
{
135
48774
    int score = 0;
136
    int i, n;
137
48774
    int8_t *const qscale_table = s->current_picture.qscale_table;
138
139
48774
    memcpy(zigzag_last_index, s->block_last_index, sizeof(int) * 6);
140
141
341418
    for (n = 0; n < 6; n++) {
142
        int16_t *ac_val, *ac_val1;
143
144
585288
        score -= get_block_rate(s, block[n], s->block_last_index[n],
145
292644
                                s->intra_scantable.permutated);
146
147
292644
        ac_val  = &s->ac_val[0][0][0] + s->block_index[n] * 16;
148
292644
        ac_val1 = ac_val;
149
292644
        if (dir[n]) {
150
80145
            const int xy = s->mb_x + s->mb_y * s->mb_stride - s->mb_stride;
151
            /* top prediction */
152
80145
            ac_val -= s->block_wrap[n] * 16;
153


80145
            if (s->mb_y == 0 || s->qscale == qscale_table[xy] || n == 2 || n == 3) {
154
                /* same qscale */
155
641160
                for (i = 1; i < 8; i++) {
156
561015
                    const int level = block[n][s->idsp.idct_permutation[i]];
157
561015
                    block[n][s->idsp.idct_permutation[i]] = level - ac_val[i + 8];
158
561015
                    ac_val1[i]     = block[n][s->idsp.idct_permutation[i << 3]];
159
561015
                    ac_val1[i + 8] = level;
160
                }
161
            } else {
162
                /* different qscale, we must rescale */
163
                for (i = 1; i < 8; i++) {
164
                    const int level = block[n][s->idsp.idct_permutation[i]];
165
                    block[n][s->idsp.idct_permutation[i]] = level - ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], s->qscale);
166
                    ac_val1[i]     = block[n][s->idsp.idct_permutation[i << 3]];
167
                    ac_val1[i + 8] = level;
168
                }
169
            }
170
80145
            st[n] = s->intra_h_scantable.permutated;
171
        } else {
172
212499
            const int xy = s->mb_x - 1 + s->mb_y * s->mb_stride;
173
            /* left prediction */
174
212499
            ac_val -= 16;
175


212499
            if (s->mb_x == 0 || s->qscale == qscale_table[xy] || n == 1 || n == 3) {
176
                /* same qscale */
177
1699992
                for (i = 1; i < 8; i++) {
178
1487493
                    const int level = block[n][s->idsp.idct_permutation[i << 3]];
179
1487493
                    block[n][s->idsp.idct_permutation[i << 3]] = level - ac_val[i];
180
1487493
                    ac_val1[i]     = level;
181
1487493
                    ac_val1[i + 8] = block[n][s->idsp.idct_permutation[i]];
182
                }
183
            } else {
184
                /* different qscale, we must rescale */
185
                for (i = 1; i < 8; i++) {
186
                    const int level = block[n][s->idsp.idct_permutation[i << 3]];
187
                    block[n][s->idsp.idct_permutation[i << 3]] = level - ROUNDED_DIV(ac_val[i] * qscale_table[xy], s->qscale);
188
                    ac_val1[i]     = level;
189
                    ac_val1[i + 8] = block[n][s->idsp.idct_permutation[i]];
190
                }
191
            }
192
212499
            st[n] = s->intra_v_scantable.permutated;
193
        }
194
195
12538840
        for (i = 63; i > 0; i--)  // FIXME optimize
196
12470313
            if (block[n][st[n][i]])
197
224117
                break;
198
292644
        s->block_last_index[n] = i;
199
200
292644
        score += get_block_rate(s, block[n], s->block_last_index[n], st[n]);
201
    }
202
203
48774
    if (score < 0) {
204
7332
        return 1;
205
    } else {
206
41442
        restore_ac_coeffs(s, block, dir, st, zigzag_last_index);
207
41442
        return 0;
208
    }
209
}
210
211
/**
212
 * modify mb_type & qscale so that encoding is actually possible in MPEG-4
213
 */
214
400
void ff_clean_mpeg4_qscales(MpegEncContext *s)
215
{
216
    int i;
217
400
    int8_t *const qscale_table = s->current_picture.qscale_table;
218
219
400
    ff_clean_h263_qscales(s);
220
221
400
    if (s->pict_type == AV_PICTURE_TYPE_B) {
222
256
        int odd = 0;
223
        /* ok, come on, this isn't funny anymore, there's more code for
224
         * handling this MPEG-4 mess than for the actual adaptive quantization */
225
226
76864
        for (i = 0; i < s->mb_num; i++) {
227
76608
            int mb_xy = s->mb_index2xy[i];
228
76608
            odd += qscale_table[mb_xy] & 1;
229
        }
230
231
256
        if (2 * odd > s->mb_num)
232
143
            odd = 1;
233
        else
234
113
            odd = 0;
235
236
76864
        for (i = 0; i < s->mb_num; i++) {
237
76608
            int mb_xy = s->mb_index2xy[i];
238
76608
            if ((qscale_table[mb_xy] & 1) != odd)
239
10413
                qscale_table[mb_xy]++;
240
76608
            if (qscale_table[mb_xy] > 31)
241
                qscale_table[mb_xy] = 31;
242
        }
243
244
76608
        for (i = 1; i < s->mb_num; i++) {
245
76352
            int mb_xy = s->mb_index2xy[i];
246
76352
            if (qscale_table[mb_xy] != qscale_table[s->mb_index2xy[i - 1]] &&
247
13926
                (s->mb_type[mb_xy] & CANDIDATE_MB_TYPE_DIRECT)) {
248
13915
                s->mb_type[mb_xy] |= CANDIDATE_MB_TYPE_BIDIR;
249
            }
250
        }
251
    }
252
400
}
253
254
/**
255
 * Encode the dc value.
256
 * @param n block index (0-3 are luma, 4-5 are chroma)
257
 */
258
1205160
static inline void mpeg4_encode_dc(PutBitContext *s, int level, int n)
259
{
260
    /* DC will overflow if level is outside the [-255,255] range. */
261
1205160
    level += 256;
262
1205160
    if (n < 4) {
263
        /* luminance */
264
803440
        put_bits(s, uni_DCtab_lum_len[level], uni_DCtab_lum_bits[level]);
265
    } else {
266
        /* chrominance */
267
401720
        put_bits(s, uni_DCtab_chrom_len[level], uni_DCtab_chrom_bits[level]);
268
    }
269
1205160
}
270
271
static inline int mpeg4_get_dc_length(int level, int n)
272
{
273
    if (n < 4)
274
        return uni_DCtab_lum_len[level + 256];
275
    else
276
        return uni_DCtab_chrom_len[level + 256];
277
}
278
279
/**
280
 * Encode an 8x8 block.
281
 * @param n block index (0-3 are luma, 4-5 are chroma)
282
 */
283
10110834
static inline void mpeg4_encode_block(MpegEncContext *s,
284
                                      int16_t *block, int n, int intra_dc,
285
                                      uint8_t *scan_table, PutBitContext *dc_pb,
286
                                      PutBitContext *ac_pb)
287
{
288
    int i, last_non_zero;
289
    uint32_t *bits_tab;
290
    uint8_t *len_tab;
291
10110834
    const int last_index = s->block_last_index[n];
292
293
10110834
    if (s->mb_intra) {  // Note gcc (3.2.1 at least) will optimize this away
294
        /* MPEG-4 based DC predictor */
295
1205160
        mpeg4_encode_dc(dc_pb, intra_dc, n);
296
1205160
        if (last_index < 1)
297
279085
            return;
298
926075
        i = 1;
299
926075
        bits_tab = uni_mpeg4_intra_rl_bits;
300
926075
        len_tab  = uni_mpeg4_intra_rl_len;
301
    } else {
302
8905674
        if (last_index < 0)
303
5123090
            return;
304
3782584
        i = 0;
305
3782584
        bits_tab = uni_mpeg4_inter_rl_bits;
306
3782584
        len_tab  = uni_mpeg4_inter_rl_len;
307
    }
308
309
    /* AC coefs */
310
4708659
    last_non_zero = i - 1;
311
131763528
    for (; i < last_index; i++) {
312
127054869
        int level = block[scan_table[i]];
313
127054869
        if (level) {
314
42037994
            int run = i - last_non_zero - 1;
315
42037994
            level += 64;
316
42037994
            if ((level & (~127)) == 0) {
317
42020003
                const int index = UNI_MPEG4_ENC_INDEX(0, run, level);
318
42020003
                put_bits(ac_pb, len_tab[index], bits_tab[index]);
319
            } else {  // ESC3
320
17991
                put_bits(ac_pb,
321
                         7 + 2 + 1 + 6 + 1 + 12 + 1,
322
17991
                         (3 << 23) + (3 << 21) + (0 << 20) + (run << 14) +
323
17991
                         (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
324
            }
325
42037994
            last_non_zero = i;
326
        }
327
    }
328
    /* if (i <= last_index) */ {
329
4708659
        int level = block[scan_table[i]];
330
4708659
        int run   = i - last_non_zero - 1;
331
4708659
        level += 64;
332
4708659
        if ((level & (~127)) == 0) {
333
4708252
            const int index = UNI_MPEG4_ENC_INDEX(1, run, level);
334
4708252
            put_bits(ac_pb, len_tab[index], bits_tab[index]);
335
        } else {  // ESC3
336
407
            put_bits(ac_pb,
337
                     7 + 2 + 1 + 6 + 1 + 12 + 1,
338
407
                     (3 << 23) + (3 << 21) + (1 << 20) + (run << 14) +
339
407
                     (1 << 13) + (((level - 64) & 0xfff) << 1) + 1);
340
        }
341
    }
342
}
343
344
static int mpeg4_get_block_length(MpegEncContext *s,
345
                                  int16_t *block, int n,
346
                                  int intra_dc, uint8_t *scan_table)
347
{
348
    int i, last_non_zero;
349
    uint8_t *len_tab;
350
    const int last_index = s->block_last_index[n];
351
    int len = 0;
352
353
    if (s->mb_intra) {  // Note gcc (3.2.1 at least) will optimize this away
354
        /* MPEG-4 based DC predictor */
355
        len += mpeg4_get_dc_length(intra_dc, n);
356
        if (last_index < 1)
357
            return len;
358
        i = 1;
359
        len_tab = uni_mpeg4_intra_rl_len;
360
    } else {
361
        if (last_index < 0)
362
            return 0;
363
        i = 0;
364
        len_tab = uni_mpeg4_inter_rl_len;
365
    }
366
367
    /* AC coefs */
368
    last_non_zero = i - 1;
369
    for (; i < last_index; i++) {
370
        int level = block[scan_table[i]];
371
        if (level) {
372
            int run = i - last_non_zero - 1;
373
            level += 64;
374
            if ((level & (~127)) == 0) {
375
                const int index = UNI_MPEG4_ENC_INDEX(0, run, level);
376
                len += len_tab[index];
377
            } else {  // ESC3
378
                len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
379
            }
380
            last_non_zero = i;
381
        }
382
    }
383
    /* if (i <= last_index) */ {
384
        int level = block[scan_table[i]];
385
        int run   = i - last_non_zero - 1;
386
        level += 64;
387
        if ((level & (~127)) == 0) {
388
            const int index = UNI_MPEG4_ENC_INDEX(1, run, level);
389
            len += len_tab[index];
390
        } else {  // ESC3
391
            len += 7 + 2 + 1 + 6 + 1 + 12 + 1;
392
        }
393
    }
394
395
    return len;
396
}
397
398
1685139
static inline void mpeg4_encode_blocks(MpegEncContext *s, int16_t block[6][64],
399
                                       int intra_dc[6], uint8_t **scan_table,
400
                                       PutBitContext *dc_pb,
401
                                       PutBitContext *ac_pb)
402
{
403
    int i;
404
405
1685139
    if (scan_table) {
406
200860
        if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT) {
407
            for (i = 0; i < 6; i++)
408
                skip_put_bits(&s->pb,
409
                              mpeg4_get_block_length(s, block[i], i,
410
                                                     intra_dc[i], scan_table[i]));
411
        } else {
412
            /* encode each block */
413
1406020
            for (i = 0; i < 6; i++)
414
1205160
                mpeg4_encode_block(s, block[i], i,
415
1205160
                                   intra_dc[i], scan_table[i], dc_pb, ac_pb);
416
        }
417
    } else {
418
1484279
        if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT) {
419
            for (i = 0; i < 6; i++)
420
                skip_put_bits(&s->pb,
421
                              mpeg4_get_block_length(s, block[i], i, 0,
422
                                                     s->intra_scantable.permutated));
423
        } else {
424
            /* encode each block */
425
10389953
            for (i = 0; i < 6; i++)
426
8905674
                mpeg4_encode_block(s, block[i], i, 0,
427
8905674
                                   s->intra_scantable.permutated, dc_pb, ac_pb);
428
        }
429
    }
430
1685139
}
431
432
765962
static inline int get_b_cbp(MpegEncContext *s, int16_t block[6][64],
433
                            int motion_x, int motion_y, int mb_type)
434
{
435
765962
    int cbp = 0, i;
436
437
765962
    if (s->mpv_flags & FF_MPV_FLAG_CBP_RD) {
438
        int score        = 0;
439
        const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
440
441
        for (i = 0; i < 6; i++) {
442
            if (s->coded_score[i] < 0) {
443
                score += s->coded_score[i];
444
                cbp   |= 1 << (5 - i);
445
            }
446
        }
447
448
        if (cbp) {
449
            int zero_score = -6;
450
            if ((motion_x | motion_y | s->dquant | mb_type) == 0)
451
                zero_score -= 4;  // 2 * MV + mb_type + cbp bit
452
453
            zero_score *= lambda;
454
            if (zero_score <= score)
455
                cbp = 0;
456
        }
457
458
        for (i = 0; i < 6; i++) {
459
            if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i)) & 1) == 0) {
460
                s->block_last_index[i] = -1;
461
                s->bdsp.clear_block(s->block[i]);
462
            }
463
        }
464
    } else {
465
5361734
        for (i = 0; i < 6; i++) {
466
4595772
            if (s->block_last_index[i] >= 0)
467
1642053
                cbp |= 1 << (5 - i);
468
        }
469
    }
470
765962
    return cbp;
471
}
472
473
// FIXME this is duplicated to h263.c
474
static const int dquant_code[5] = { 1, 0, 9, 2, 3 };
475
476
1750611
void ff_mpeg4_encode_mb(MpegEncContext *s, int16_t block[6][64],
477
                        int motion_x, int motion_y)
478
{
479
    int cbpc, cbpy, pred_x, pred_y;
480
1750611
    PutBitContext *const pb2    = s->data_partitioning ? &s->pb2 : &s->pb;
481

1750611
    PutBitContext *const tex_pb = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B ? &s->tex_pb : &s->pb;
482

1750611
    PutBitContext *const dc_pb  = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_I ? &s->pb2 : &s->pb;
483

1750611
    const int interleaved_stats = (s->avctx->flags & AV_CODEC_FLAG_PASS1) && !s->data_partitioning ? 1 : 0;
484
485
1750611
    if (!s->mb_intra) {
486
        int i, cbp;
487
488
1549751
        if (s->pict_type == AV_PICTURE_TYPE_B) {
489
            /* convert from mv_dir to type */
490
            static const int mb_type_table[8] = { -1, 3, 2, 1, -1, -1, -1, 0 };
491
766080
            int mb_type = mb_type_table[s->mv_dir];
492
493
766080
            if (s->mb_x == 0) {
494
110403
                for (i = 0; i < 2; i++)
495
73602
                    s->last_mv[i][0][0] =
496
73602
                    s->last_mv[i][0][1] =
497
73602
                    s->last_mv[i][1][0] =
498
73602
                    s->last_mv[i][1][1] = 0;
499
            }
500
501
            av_assert2(s->dquant >= -2 && s->dquant <= 2);
502
            av_assert2((s->dquant & 1) == 0);
503
            av_assert2(mb_type >= 0);
504
505
            /* nothing to do if this MB was skipped in the next P-frame */
506
766080
            if (s->next_picture.mbskip_table[s->mb_y * s->mb_stride + s->mb_x]) {  // FIXME avoid DCT & ...
507
118
                s->skip_count++;
508
118
                s->mv[0][0][0] =
509
118
                s->mv[0][0][1] =
510
118
                s->mv[1][0][0] =
511
118
                s->mv[1][0][1] = 0;
512
118
                s->mv_dir  = MV_DIR_FORWARD;  // doesn't matter
513
118
                s->qscale -= s->dquant;
514
//                s->mb_skipped = 1;
515
516
65472
                return;
517
            }
518
519
765962
            cbp = get_b_cbp(s, block, motion_x, motion_y, mb_type);
520
521
765962
            if ((cbp | motion_x | motion_y | mb_type) == 0) {
522
                /* direct MB with MV={0,0} */
523
                av_assert2(s->dquant == 0);
524
525
44007
                put_bits(&s->pb, 1, 1); /* mb not coded modb1=1 */
526
527
44007
                if (interleaved_stats) {
528
                    s->misc_bits++;
529
                    s->last_bits++;
530
                }
531
44007
                s->skip_count++;
532
44007
                return;
533
            }
534
535
721955
            put_bits(&s->pb, 1, 0);            /* mb coded modb1=0 */
536
721955
            put_bits(&s->pb, 1, cbp ? 0 : 1);  /* modb2 */ // FIXME merge
537
721955
            put_bits(&s->pb, mb_type + 1, 1);  // this table is so simple that we don't need it :)
538
721955
            if (cbp)
539
480909
                put_bits(&s->pb, 6, cbp);
540
541

721955
            if (cbp && mb_type) {
542
333101
                if (s->dquant)
543
41971
                    put_bits(&s->pb, 2, (s->dquant >> 2) + 3);
544
                else
545
291130
                    put_bits(&s->pb, 1, 0);
546
            } else
547
388854
                s->qscale -= s->dquant;
548
549
721955
            if (!s->progressive_sequence) {
550
                if (cbp)
551
                    put_bits(&s->pb, 1, s->interlaced_dct);
552
                if (mb_type)                  // not direct mode
553
                    put_bits(&s->pb, 1, s->mv_type == MV_TYPE_FIELD);
554
            }
555
556
721955
            if (interleaved_stats)
557
                s->misc_bits += get_bits_diff(s);
558
559
721955
            if (!mb_type) {
560
                av_assert2(s->mv_dir & MV_DIRECT);
561
172311
                ff_h263_encode_motion_vector(s, motion_x, motion_y, 1);
562
172311
                s->b_count++;
563
172311
                s->f_count++;
564
            } else {
565
                av_assert2(mb_type > 0 && mb_type < 4);
566
549644
                if (s->mv_type != MV_TYPE_FIELD) {
567
549644
                    if (s->mv_dir & MV_DIR_FORWARD) {
568
372400
                        ff_h263_encode_motion_vector(s,
569
372400
                                                     s->mv[0][0][0] - s->last_mv[0][0][0],
570
372400
                                                     s->mv[0][0][1] - s->last_mv[0][0][1],
571
                                                     s->f_code);
572
372400
                        s->last_mv[0][0][0] =
573
372400
                        s->last_mv[0][1][0] = s->mv[0][0][0];
574
372400
                        s->last_mv[0][0][1] =
575
372400
                        s->last_mv[0][1][1] = s->mv[0][0][1];
576
372400
                        s->f_count++;
577
                    }
578
549644
                    if (s->mv_dir & MV_DIR_BACKWARD) {
579
375855
                        ff_h263_encode_motion_vector(s,
580
375855
                                                     s->mv[1][0][0] - s->last_mv[1][0][0],
581
375855
                                                     s->mv[1][0][1] - s->last_mv[1][0][1],
582
                                                     s->b_code);
583
375855
                        s->last_mv[1][0][0] =
584
375855
                        s->last_mv[1][1][0] = s->mv[1][0][0];
585
375855
                        s->last_mv[1][0][1] =
586
375855
                        s->last_mv[1][1][1] = s->mv[1][0][1];
587
375855
                        s->b_count++;
588
                    }
589
                } else {
590
                    if (s->mv_dir & MV_DIR_FORWARD) {
591
                        put_bits(&s->pb, 1, s->field_select[0][0]);
592
                        put_bits(&s->pb, 1, s->field_select[0][1]);
593
                    }
594
                    if (s->mv_dir & MV_DIR_BACKWARD) {
595
                        put_bits(&s->pb, 1, s->field_select[1][0]);
596
                        put_bits(&s->pb, 1, s->field_select[1][1]);
597
                    }
598
                    if (s->mv_dir & MV_DIR_FORWARD) {
599
                        for (i = 0; i < 2; i++) {
600
                            ff_h263_encode_motion_vector(s,
601
                                                         s->mv[0][i][0] - s->last_mv[0][i][0],
602
                                                         s->mv[0][i][1] - s->last_mv[0][i][1] / 2,
603
                                                         s->f_code);
604
                            s->last_mv[0][i][0] = s->mv[0][i][0];
605
                            s->last_mv[0][i][1] = s->mv[0][i][1] * 2;
606
                        }
607
                        s->f_count++;
608
                    }
609
                    if (s->mv_dir & MV_DIR_BACKWARD) {
610
                        for (i = 0; i < 2; i++) {
611
                            ff_h263_encode_motion_vector(s,
612
                                                         s->mv[1][i][0] - s->last_mv[1][i][0],
613
                                                         s->mv[1][i][1] - s->last_mv[1][i][1] / 2,
614
                                                         s->b_code);
615
                            s->last_mv[1][i][0] = s->mv[1][i][0];
616
                            s->last_mv[1][i][1] = s->mv[1][i][1] * 2;
617
                        }
618
                        s->b_count++;
619
                    }
620
                }
621
            }
622
623
721955
            if (interleaved_stats)
624
                s->mv_bits += get_bits_diff(s);
625
626
721955
            mpeg4_encode_blocks(s, block, NULL, NULL, NULL, &s->pb);
627
628
721955
            if (interleaved_stats)
629
                s->p_tex_bits += get_bits_diff(s);
630
        } else { /* s->pict_type==AV_PICTURE_TYPE_B */
631
783671
            cbp = get_p_cbp(s, block, motion_x, motion_y);
632
633
783671
            if ((cbp | motion_x | motion_y | s->dquant) == 0 &&
634
50598
                s->mv_type == MV_TYPE_16X16) {
635
                /* Check if the B-frames can skip it too, as we must skip it
636
                 * if we skip here why didn't they just compress
637
                 * the skip-mb bits instead of reusing them ?! */
638
21667
                if (s->max_b_frames > 0) {
639
                    int i;
640
                    int x, y, offset;
641
                    uint8_t *p_pic;
642
643
686
                    x = s->mb_x * 16;
644
686
                    y = s->mb_y * 16;
645
646
686
                    offset = x + y * s->linesize;
647
686
                    p_pic  = s->new_picture.f->data[0] + offset;
648
649
686
                    s->mb_skipped = 1;
650
858
                    for (i = 0; i < s->max_b_frames; i++) {
651
                        uint8_t *b_pic;
652
                        int diff;
653
772
                        Picture *pic = s->reordered_input_picture[i + 1];
654
655

772
                        if (!pic || pic->f->pict_type != AV_PICTURE_TYPE_B)
656
                            break;
657
658
492
                        b_pic = pic->f->data[0] + offset;
659
492
                        if (!pic->shared)
660
492
                            b_pic += INPLACE_OFFSET;
661
662

492
                        if (x + 16 > s->width || y + 16 > s->height) {
663
                            int x1, y1;
664
                            int xe = FFMIN(16, s->width - x);
665
                            int ye = FFMIN(16, s->height - y);
666
                            diff = 0;
667
                            for (y1 = 0; y1 < ye; y1++) {
668
                                for (x1 = 0; x1 < xe; x1++) {
669
                                    diff += FFABS(p_pic[x1 + y1 * s->linesize] - b_pic[x1 + y1 * s->linesize]);
670
                                }
671
                            }
672
                            diff = diff * 256 / (xe * ye);
673
                        } else {
674
492
                            diff = s->mecc.sad[0](NULL, p_pic, b_pic, s->linesize, 16);
675
                        }
676
492
                        if (diff > s->qscale * 70) {  // FIXME check that 70 is optimal
677
320
                            s->mb_skipped = 0;
678
320
                            break;
679
                        }
680
                    }
681
                } else
682
20981
                    s->mb_skipped = 1;
683
684
21667
                if (s->mb_skipped == 1) {
685
                    /* skip macroblock */
686
21347
                    put_bits(&s->pb, 1, 1);
687
688
21347
                    if (interleaved_stats) {
689
                        s->misc_bits++;
690
                        s->last_bits++;
691
                    }
692
21347
                    s->skip_count++;
693
694
21347
                    return;
695
                }
696
            }
697
698
762324
            put_bits(&s->pb, 1, 0);     /* mb coded */
699
762324
            cbpc  = cbp & 3;
700
762324
            cbpy  = cbp >> 2;
701
762324
            cbpy ^= 0xf;
702
762324
            if (s->mv_type == MV_TYPE_16X16) {
703
553114
                if (s->dquant)
704
43645
                    cbpc += 8;
705
553114
                put_bits(&s->pb,
706
553114
                         ff_h263_inter_MCBPC_bits[cbpc],
707
553114
                         ff_h263_inter_MCBPC_code[cbpc]);
708
709
553114
                put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
710
553114
                if (s->dquant)
711
43645
                    put_bits(pb2, 2, dquant_code[s->dquant + 2]);
712
713
553114
                if (!s->progressive_sequence) {
714
                    if (cbp)
715
                        put_bits(pb2, 1, s->interlaced_dct);
716
                    put_bits(pb2, 1, 0);
717
                }
718
719
553114
                if (interleaved_stats)
720
                    s->misc_bits += get_bits_diff(s);
721
722
                /* motion vectors: 16x16 mode */
723
553114
                ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
724
725
553114
                ff_h263_encode_motion_vector(s,
726
                                             motion_x - pred_x,
727
                                             motion_y - pred_y,
728
                                             s->f_code);
729
209210
            } else if (s->mv_type == MV_TYPE_FIELD) {
730
                if (s->dquant)
731
                    cbpc += 8;
732
                put_bits(&s->pb,
733
                         ff_h263_inter_MCBPC_bits[cbpc],
734
                         ff_h263_inter_MCBPC_code[cbpc]);
735
736
                put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
737
                if (s->dquant)
738
                    put_bits(pb2, 2, dquant_code[s->dquant + 2]);
739
740
                av_assert2(!s->progressive_sequence);
741
                if (cbp)
742
                    put_bits(pb2, 1, s->interlaced_dct);
743
                put_bits(pb2, 1, 1);
744
745
                if (interleaved_stats)
746
                    s->misc_bits += get_bits_diff(s);
747
748
                /* motion vectors: 16x8 interlaced mode */
749
                ff_h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
750
                pred_y /= 2;
751
752
                put_bits(&s->pb, 1, s->field_select[0][0]);
753
                put_bits(&s->pb, 1, s->field_select[0][1]);
754
755
                ff_h263_encode_motion_vector(s,
756
                                             s->mv[0][0][0] - pred_x,
757
                                             s->mv[0][0][1] - pred_y,
758
                                             s->f_code);
759
                ff_h263_encode_motion_vector(s,
760
                                             s->mv[0][1][0] - pred_x,
761
                                             s->mv[0][1][1] - pred_y,
762
                                             s->f_code);
763
            } else {
764
                av_assert2(s->mv_type == MV_TYPE_8X8);
765
209210
                put_bits(&s->pb,
766
209210
                         ff_h263_inter_MCBPC_bits[cbpc + 16],
767
209210
                         ff_h263_inter_MCBPC_code[cbpc + 16]);
768
209210
                put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
769
770

209210
                if (!s->progressive_sequence && cbp)
771
                    put_bits(pb2, 1, s->interlaced_dct);
772
773
209210
                if (interleaved_stats)
774
                    s->misc_bits += get_bits_diff(s);
775
776
1046050
                for (i = 0; i < 4; i++) {
777
                    /* motion vectors: 8x8 mode*/
778
836840
                    ff_h263_pred_motion(s, i, 0, &pred_x, &pred_y);
779
780
836840
                    ff_h263_encode_motion_vector(s,
781
836840
                                                 s->current_picture.motion_val[0][s->block_index[i]][0] - pred_x,
782
836840
                                                 s->current_picture.motion_val[0][s->block_index[i]][1] - pred_y,
783
                                                 s->f_code);
784
                }
785
            }
786
787
762324
            if (interleaved_stats)
788
                s->mv_bits += get_bits_diff(s);
789
790
762324
            mpeg4_encode_blocks(s, block, NULL, NULL, NULL, tex_pb);
791
792
762324
            if (interleaved_stats)
793
                s->p_tex_bits += get_bits_diff(s);
794
795
762324
            s->f_count++;
796
        }
797
    } else {
798
        int cbp;
799
        int dc_diff[6];  // dc values with the dc prediction subtracted
800
        int dir[6];      // prediction direction
801
        int zigzag_last_index[6];
802
        uint8_t *scan_table[6];
803
        int i;
804
805
1406020
        for (i = 0; i < 6; i++)
806
1205160
            dc_diff[i] = ff_mpeg4_pred_dc(s, i, block[i][0], &dir[i], 1);
807
808
200860
        if (s->avctx->flags & AV_CODEC_FLAG_AC_PRED) {
809
48774
            s->ac_pred = decide_ac_pred(s, block, dir, scan_table, zigzag_last_index);
810
        } else {
811
1064602
            for (i = 0; i < 6; i++)
812
912516
                scan_table[i] = s->intra_scantable.permutated;
813
        }
814
815
        /* compute cbp */
816
200860
        cbp = 0;
817
1406020
        for (i = 0; i < 6; i++)
818
1205160
            if (s->block_last_index[i] >= 1)
819
926075
                cbp |= 1 << (5 - i);
820
821
200860
        cbpc = cbp & 3;
822
200860
        if (s->pict_type == AV_PICTURE_TYPE_I) {
823
106386
            if (s->dquant)
824
26002
                cbpc += 4;
825
106386
            put_bits(&s->pb,
826
106386
                     ff_h263_intra_MCBPC_bits[cbpc],
827
106386
                     ff_h263_intra_MCBPC_code[cbpc]);
828
        } else {
829
94474
            if (s->dquant)
830
5678
                cbpc += 8;
831
94474
            put_bits(&s->pb, 1, 0);     /* mb coded */
832
94474
            put_bits(&s->pb,
833
94474
                     ff_h263_inter_MCBPC_bits[cbpc + 4],
834
94474
                     ff_h263_inter_MCBPC_code[cbpc + 4]);
835
        }
836
200860
        put_bits(pb2, 1, s->ac_pred);
837
200860
        cbpy = cbp >> 2;
838
200860
        put_bits(pb2, ff_h263_cbpy_tab[cbpy][1], ff_h263_cbpy_tab[cbpy][0]);
839
200860
        if (s->dquant)
840
31680
            put_bits(dc_pb, 2, dquant_code[s->dquant + 2]);
841
842
200860
        if (!s->progressive_sequence)
843
            put_bits(dc_pb, 1, s->interlaced_dct);
844
845
200860
        if (interleaved_stats)
846
            s->misc_bits += get_bits_diff(s);
847
848
200860
        mpeg4_encode_blocks(s, block, dc_diff, scan_table, dc_pb, tex_pb);
849
850
200860
        if (interleaved_stats)
851
            s->i_tex_bits += get_bits_diff(s);
852
200860
        s->i_count++;
853
854
        /* restore ac coeffs & last_index stuff
855
         * if we messed them up with the prediction */
856
200860
        if (s->ac_pred)
857
7332
            restore_ac_coeffs(s, block, dir, scan_table, zigzag_last_index);
858
    }
859
}
860
861
/**
862
 * add MPEG-4 stuffing bits (01...1)
863
 */
864
6494
void ff_mpeg4_stuffing(PutBitContext *pbc)
865
{
866
    int length;
867
6494
    put_bits(pbc, 1, 0);
868
6494
    length = (-put_bits_count(pbc)) & 7;
869
6494
    if (length)
870
5819
        put_bits(pbc, length, (1 << length) - 1);
871
6494
}
872
873
/* must be called before writing the header */
874
2490
void ff_set_mpeg4_time(MpegEncContext *s)
875
{
876
2490
    if (s->pict_type == AV_PICTURE_TYPE_B) {
877
640
        ff_mpeg4_init_direct_mv(s);
878
    } else {
879
1850
        s->last_time_base = s->time_base;
880
1850
        s->time_base      = FFUDIV(s->time, s->avctx->time_base.den);
881
    }
882
2490
}
883
884
255
static void mpeg4_encode_gop_header(MpegEncContext *s)
885
{
886
    int64_t hours, minutes, seconds;
887
    int64_t time;
888
889
255
    put_bits(&s->pb, 16, 0);
890
255
    put_bits(&s->pb, 16, GOP_STARTCODE);
891
892
255
    time = s->current_picture_ptr->f->pts;
893
255
    if (s->reordered_input_picture[1])
894
81
        time = FFMIN(time, s->reordered_input_picture[1]->f->pts);
895
255
    time = time * s->avctx->time_base.num;
896
255
    s->last_time_base = FFUDIV(time, s->avctx->time_base.den);
897
898
255
    seconds = FFUDIV(time, s->avctx->time_base.den);
899

255
    minutes = FFUDIV(seconds, 60); seconds = FFUMOD(seconds, 60);
900

255
    hours   = FFUDIV(minutes, 60); minutes = FFUMOD(minutes, 60);
901
255
    hours   = FFUMOD(hours  , 24);
902
903
255
    put_bits(&s->pb, 5, hours);
904
255
    put_bits(&s->pb, 6, minutes);
905
255
    put_bits(&s->pb, 1, 1);
906
255
    put_bits(&s->pb, 6, seconds);
907
908
255
    put_bits(&s->pb, 1, !!(s->avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
909
255
    put_bits(&s->pb, 1, 0);  // broken link == NO
910
911
255
    ff_mpeg4_stuffing(&s->pb);
912
255
}
913
914
213
static void mpeg4_encode_visual_object_header(MpegEncContext *s)
915
{
916
    int profile_and_level_indication;
917
    int vo_ver_id;
918
919
213
    if (s->avctx->profile != FF_PROFILE_UNKNOWN) {
920
        profile_and_level_indication = s->avctx->profile << 4;
921

213
    } else if (s->max_b_frames || s->quarter_sample) {
922
101
        profile_and_level_indication = 0xF0;  // adv simple
923
    } else {
924
112
        profile_and_level_indication = 0x00;  // simple
925
    }
926
927
213
    if (s->avctx->level != FF_LEVEL_UNKNOWN)
928
        profile_and_level_indication |= s->avctx->level;
929
    else
930
213
        profile_and_level_indication |= 1;   // level 1
931
932
213
    if (profile_and_level_indication >> 4 == 0xF)
933
101
        vo_ver_id = 5;
934
    else
935
112
        vo_ver_id = 1;
936
937
    // FIXME levels
938
939
213
    put_bits(&s->pb, 16, 0);
940
213
    put_bits(&s->pb, 16, VOS_STARTCODE);
941
942
213
    put_bits(&s->pb, 8, profile_and_level_indication);
943
944
213
    put_bits(&s->pb, 16, 0);
945
213
    put_bits(&s->pb, 16, VISUAL_OBJ_STARTCODE);
946
947
213
    put_bits(&s->pb, 1, 1);
948
213
    put_bits(&s->pb, 4, vo_ver_id);
949
213
    put_bits(&s->pb, 3, 1);     // priority
950
951
213
    put_bits(&s->pb, 4, 1);     // visual obj type== video obj
952
953
213
    put_bits(&s->pb, 1, 0);     // video signal type == no clue // FIXME
954
955
213
    ff_mpeg4_stuffing(&s->pb);
956
213
}
957
958
213
static void mpeg4_encode_vol_header(MpegEncContext *s,
959
                                    int vo_number,
960
                                    int vol_number)
961
{
962
    int vo_ver_id;
963
964
    if (!CONFIG_MPEG4_ENCODER)
965
        return;
966
967

213
    if (s->max_b_frames || s->quarter_sample) {
968
101
        vo_ver_id  = 5;
969
101
        s->vo_type = ADV_SIMPLE_VO_TYPE;
970
    } else {
971
112
        vo_ver_id  = 1;
972
112
        s->vo_type = SIMPLE_VO_TYPE;
973
    }
974
975
213
    put_bits(&s->pb, 16, 0);
976
213
    put_bits(&s->pb, 16, 0x100 + vo_number);        /* video obj */
977
213
    put_bits(&s->pb, 16, 0);
978
213
    put_bits(&s->pb, 16, 0x120 + vol_number);       /* video obj layer */
979
980
213
    put_bits(&s->pb, 1, 0);             /* random access vol */
981
213
    put_bits(&s->pb, 8, s->vo_type);    /* video obj type indication */
982
213
    if (s->workaround_bugs & FF_BUG_MS) {
983
        put_bits(&s->pb, 1, 0);         /* is obj layer id= no */
984
    } else {
985
213
        put_bits(&s->pb, 1, 1);         /* is obj layer id= yes */
986
213
        put_bits(&s->pb, 4, vo_ver_id); /* is obj layer ver id */
987
213
        put_bits(&s->pb, 3, 1);         /* is obj layer priority */
988
    }
989
990
213
    s->aspect_ratio_info = ff_h263_aspect_to_info(s->avctx->sample_aspect_ratio);
991
992
213
    put_bits(&s->pb, 4, s->aspect_ratio_info); /* aspect ratio info */
993
213
    if (s->aspect_ratio_info == FF_ASPECT_EXTENDED) {
994
        av_reduce(&s->avctx->sample_aspect_ratio.num, &s->avctx->sample_aspect_ratio.den,
995
                   s->avctx->sample_aspect_ratio.num,  s->avctx->sample_aspect_ratio.den, 255);
996
        put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.num);
997
        put_bits(&s->pb, 8, s->avctx->sample_aspect_ratio.den);
998
    }
999
1000
213
    if (s->workaround_bugs & FF_BUG_MS) {
1001
        put_bits(&s->pb, 1, 0);         /* vol control parameters= no @@@ */
1002
    } else {
1003
213
        put_bits(&s->pb, 1, 1);         /* vol control parameters= yes */
1004
213
        put_bits(&s->pb, 2, 1);         /* chroma format YUV 420/YV12 */
1005
213
        put_bits(&s->pb, 1, s->low_delay);
1006
213
        put_bits(&s->pb, 1, 0);         /* vbv parameters= no */
1007
    }
1008
1009
213
    put_bits(&s->pb, 2, RECT_SHAPE);    /* vol shape= rectangle */
1010
213
    put_bits(&s->pb, 1, 1);             /* marker bit */
1011
1012
213
    put_bits(&s->pb, 16, s->avctx->time_base.den);
1013
213
    if (s->time_increment_bits < 1)
1014
        s->time_increment_bits = 1;
1015
213
    put_bits(&s->pb, 1, 1);             /* marker bit */
1016
213
    put_bits(&s->pb, 1, 0);             /* fixed vop rate=no */
1017
213
    put_bits(&s->pb, 1, 1);             /* marker bit */
1018
213
    put_bits(&s->pb, 13, s->width);     /* vol width */
1019
213
    put_bits(&s->pb, 1, 1);             /* marker bit */
1020
213
    put_bits(&s->pb, 13, s->height);    /* vol height */
1021
213
    put_bits(&s->pb, 1, 1);             /* marker bit */
1022
213
    put_bits(&s->pb, 1, s->progressive_sequence ? 0 : 1);
1023
213
    put_bits(&s->pb, 1, 1);             /* obmc disable */
1024
213
    if (vo_ver_id == 1)
1025
112
        put_bits(&s->pb, 1, 0);       /* sprite enable */
1026
    else
1027
101
        put_bits(&s->pb, 2, 0);       /* sprite enable */
1028
1029
213
    put_bits(&s->pb, 1, 0);             /* not 8 bit == false */
1030
213
    put_bits(&s->pb, 1, s->mpeg_quant); /* quant type = (0 = H.263 style) */
1031
1032
213
    if (s->mpeg_quant) {
1033
        ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix);
1034
        ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix);
1035
    }
1036
1037
213
    if (vo_ver_id != 1)
1038
101
        put_bits(&s->pb, 1, s->quarter_sample);
1039
213
    put_bits(&s->pb, 1, 1);             /* complexity estimation disable */
1040
213
    put_bits(&s->pb, 1, s->rtp_mode ? 0 : 1); /* resync marker disable */
1041
213
    put_bits(&s->pb, 1, s->data_partitioning ? 1 : 0);
1042
213
    if (s->data_partitioning)
1043
60
        put_bits(&s->pb, 1, 0);         /* no rvlc */
1044
1045
213
    if (vo_ver_id != 1) {
1046
101
        put_bits(&s->pb, 1, 0);         /* newpred */
1047
101
        put_bits(&s->pb, 1, 0);         /* reduced res vop */
1048
    }
1049
213
    put_bits(&s->pb, 1, 0);             /* scalability */
1050
1051
213
    ff_mpeg4_stuffing(&s->pb);
1052
1053
    /* user data */
1054
213
    if (!(s->avctx->flags & AV_CODEC_FLAG_BITEXACT)) {
1055
        put_bits(&s->pb, 16, 0);
1056
        put_bits(&s->pb, 16, 0x1B2);    /* user_data */
1057
        avpriv_put_string(&s->pb, LIBAVCODEC_IDENT, 0);
1058
    }
1059
}
1060
1061
/* write MPEG-4 VOP header */
1062
2490
int ff_mpeg4_encode_picture_header(MpegEncContext *s, int picture_number)
1063
{
1064
    uint64_t time_incr;
1065
    int64_t time_div, time_mod;
1066
1067
2490
    if (s->pict_type == AV_PICTURE_TYPE_I) {
1068
255
        if (!(s->avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
1069
196
            if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT)  // HACK, the reference sw is buggy
1070
196
                mpeg4_encode_visual_object_header(s);
1071

196
            if (s->strict_std_compliance < FF_COMPLIANCE_VERY_STRICT || picture_number == 0)  // HACK, the reference sw is buggy
1072
196
                mpeg4_encode_vol_header(s, 0, 0);
1073
        }
1074
255
        if (!(s->workaround_bugs & FF_BUG_MS))
1075
255
            mpeg4_encode_gop_header(s);
1076
    }
1077
1078

2490
    s->partitioned_frame = s->data_partitioning && s->pict_type != AV_PICTURE_TYPE_B;
1079
1080
2490
    put_bits(&s->pb, 16, 0);                /* vop header */
1081
2490
    put_bits(&s->pb, 16, VOP_STARTCODE);    /* vop header */
1082
2490
    put_bits(&s->pb, 2, s->pict_type - 1);  /* pict type: I = 0 , P = 1 */
1083
1084
2490
    time_div  = FFUDIV(s->time, s->avctx->time_base.den);
1085
2490
    time_mod  = FFUMOD(s->time, s->avctx->time_base.den);
1086
2490
    time_incr = time_div - s->last_time_base;
1087
1088
    // This limits the frame duration to max 1 hour
1089
2490
    if (time_incr > 3600) {
1090
        av_log(s->avctx, AV_LOG_ERROR, "time_incr %"PRIu64" too large\n", time_incr);
1091
        return AVERROR(EINVAL);
1092
    }
1093
2571
    while (time_incr--)
1094
81
        put_bits(&s->pb, 1, 1);
1095
1096
2490
    put_bits(&s->pb, 1, 0);
1097
1098
2490
    put_bits(&s->pb, 1, 1);                             /* marker */
1099
2490
    put_bits(&s->pb, s->time_increment_bits, time_mod); /* time increment */
1100
2490
    put_bits(&s->pb, 1, 1);                             /* marker */
1101
2490
    put_bits(&s->pb, 1, 1);                             /* vop coded */
1102
2490
    if (s->pict_type == AV_PICTURE_TYPE_P) {
1103
1595
        put_bits(&s->pb, 1, s->no_rounding);    /* rounding type */
1104
    }
1105
2490
    put_bits(&s->pb, 3, 0);     /* intra dc VLC threshold */
1106
2490
    if (!s->progressive_sequence) {
1107
        put_bits(&s->pb, 1, s->current_picture_ptr->f->top_field_first);
1108
        put_bits(&s->pb, 1, s->alternate_scan);
1109
    }
1110
    // FIXME sprite stuff
1111
1112
2490
    put_bits(&s->pb, 5, s->qscale);
1113
1114
2490
    if (s->pict_type != AV_PICTURE_TYPE_I)
1115
2235
        put_bits(&s->pb, 3, s->f_code);  /* fcode_for */
1116
2490
    if (s->pict_type == AV_PICTURE_TYPE_B)
1117
640
        put_bits(&s->pb, 3, s->b_code);  /* fcode_back */
1118
1119
2490
    return 0;
1120
}
1121
1122
58
static av_cold void init_uni_dc_tab(void)
1123
{
1124
    int level, uni_code, uni_len;
1125
1126
29754
    for (level = -256; level < 256; level++) {
1127
        int size, v, l;
1128
        /* find number of bits */
1129
29696
        size = 0;
1130
29696
        v    = abs(level);
1131
238206
        while (v) {
1132
208510
            v >>= 1;
1133
208510
            size++;
1134
        }
1135
1136
29696
        if (level < 0)
1137
14848
            l = (-level) ^ ((1 << size) - 1);
1138
        else
1139
14848
            l = level;
1140
1141
        /* luminance */
1142
29696
        uni_code = ff_mpeg4_DCtab_lum[size][0];
1143
29696
        uni_len  = ff_mpeg4_DCtab_lum[size][1];
1144
1145
29696
        if (size > 0) {
1146
29638
            uni_code <<= size;
1147
29638
            uni_code  |= l;
1148
29638
            uni_len   += size;
1149
29638
            if (size > 8) {
1150
58
                uni_code <<= 1;
1151
58
                uni_code  |= 1;
1152
58
                uni_len++;
1153
            }
1154
        }
1155
29696
        uni_DCtab_lum_bits[level + 256] = uni_code;
1156
29696
        uni_DCtab_lum_len[level + 256]  = uni_len;
1157
1158
        /* chrominance */
1159
29696
        uni_code = ff_mpeg4_DCtab_chrom[size][0];
1160
29696
        uni_len  = ff_mpeg4_DCtab_chrom[size][1];
1161
1162
29696
        if (size > 0) {
1163
29638
            uni_code <<= size;
1164
29638
            uni_code  |= l;
1165
29638
            uni_len   += size;
1166
29638
            if (size > 8) {
1167
58
                uni_code <<= 1;
1168
58
                uni_code  |= 1;
1169
58
                uni_len++;
1170
            }
1171
        }
1172
29696
        uni_DCtab_chrom_bits[level + 256] = uni_code;
1173
29696
        uni_DCtab_chrom_len[level + 256]  = uni_len;
1174
    }
1175
58
}
1176
1177
116
static av_cold void init_uni_mpeg4_rl_tab(RLTable *rl, uint32_t *bits_tab,
1178
                                          uint8_t *len_tab)
1179
{
1180
    int slevel, run, last;
1181
1182
    av_assert0(MAX_LEVEL >= 64);
1183
    av_assert0(MAX_RUN >= 63);
1184
1185
14964
    for (slevel = -64; slevel < 64; slevel++) {
1186
14848
        if (slevel == 0)
1187
116
            continue;
1188
957580
        for (run = 0; run < 64; run++) {
1189
2828544
            for (last = 0; last <= 1; last++) {
1190
1885696
                const int index = UNI_MPEG4_ENC_INDEX(last, run, slevel + 64);
1191
1885696
                int level       = slevel < 0 ? -slevel : slevel;
1192
1885696
                int sign        = slevel < 0 ? 1 : 0;
1193
                int bits, len, code;
1194
                int level1, run1;
1195
1196
1885696
                len_tab[index] = 100;
1197
1198
                /* ESC0 */
1199
1885696
                code = get_rl_index(rl, last, run, level);
1200
1885696
                bits = rl->table_vlc[code][0];
1201
1885696
                len  = rl->table_vlc[code][1];
1202
1885696
                bits = bits * 2 + sign;
1203
1885696
                len++;
1204
1205

1885696
                if (code != rl->n && len < len_tab[index]) {
1206
23664
                    bits_tab[index] = bits;
1207
23664
                    len_tab[index]  = len;
1208
                }
1209
                /* ESC1 */
1210
1885696
                bits = rl->table_vlc[rl->n][0];
1211
1885696
                len  = rl->table_vlc[rl->n][1];
1212
1885696
                bits = bits * 2;
1213
1885696
                len++;                 // esc1
1214
1885696
                level1 = level - rl->max_level[last][run];
1215
1885696
                if (level1 > 0) {
1216
1862032
                    code   = get_rl_index(rl, last, run, level1);
1217
1862032
                    bits <<= rl->table_vlc[code][1];
1218
1862032
                    len   += rl->table_vlc[code][1];
1219
1862032
                    bits  += rl->table_vlc[code][0];
1220
1862032
                    bits   = bits * 2 + sign;
1221
1862032
                    len++;
1222
1223

1862032
                    if (code != rl->n && len < len_tab[index]) {
1224
23664
                        bits_tab[index] = bits;
1225
23664
                        len_tab[index]  = len;
1226
                    }
1227
                }
1228
                /* ESC2 */
1229
1885696
                bits = rl->table_vlc[rl->n][0];
1230
1885696
                len  = rl->table_vlc[rl->n][1];
1231
1885696
                bits = bits * 4 + 2;
1232
1885696
                len += 2;                 // esc2
1233
1885696
                run1 = run - rl->max_run[last][level] - 1;
1234
1885696
                if (run1 >= 0) {
1235
1838368
                    code   = get_rl_index(rl, last, run1, level);
1236
1838368
                    bits <<= rl->table_vlc[code][1];
1237
1838368
                    len   += rl->table_vlc[code][1];
1238
1838368
                    bits  += rl->table_vlc[code][0];
1239
1838368
                    bits   = bits * 2 + sign;
1240
1838368
                    len++;
1241
1242

1838368
                    if (code != rl->n && len < len_tab[index]) {
1243
14384
                        bits_tab[index] = bits;
1244
14384
                        len_tab[index]  = len;
1245
                    }
1246
                }
1247
                /* ESC3 */
1248
1885696
                bits = rl->table_vlc[rl->n][0];
1249
1885696
                len  = rl->table_vlc[rl->n][1];
1250
1885696
                bits = bits * 4 + 3;
1251
1885696
                len += 2;                 // esc3
1252
1885696
                bits = bits * 2 + last;
1253
1885696
                len++;
1254
1885696
                bits = bits * 64 + run;
1255
1885696
                len += 6;
1256
1885696
                bits = bits * 2 + 1;
1257
1885696
                len++;                    // marker
1258
1885696
                bits = bits * 4096 + (slevel & 0xfff);
1259
1885696
                len += 12;
1260
1885696
                bits = bits * 2 + 1;
1261
1885696
                len++;                    // marker
1262
1263
1885696
                if (len < len_tab[index]) {
1264
1825492
                    bits_tab[index] = bits;
1265
1825492
                    len_tab[index]  = len;
1266
                }
1267
            }
1268
        }
1269
    }
1270
116
}
1271
1272
58
static av_cold int encode_init(AVCodecContext *avctx)
1273
{
1274
58
    MpegEncContext *s = avctx->priv_data;
1275
    int ret;
1276
    static int done = 0;
1277
1278

58
    if (avctx->width >= (1<<13) || avctx->height >= (1<<13)) {
1279
        av_log(avctx, AV_LOG_ERROR, "dimensions too large for MPEG-4\n");
1280
        return AVERROR(EINVAL);
1281
    }
1282
1283
58
    if ((ret = ff_mpv_encode_init(avctx)) < 0)
1284
        return ret;
1285
1286
58
    if (!done) {
1287
58
        done = 1;
1288
1289
58
        init_uni_dc_tab();
1290
1291
58
        ff_rl_init(&ff_mpeg4_rl_intra, ff_mpeg4_static_rl_table_store[0]);
1292
1293
58
        init_uni_mpeg4_rl_tab(&ff_mpeg4_rl_intra, uni_mpeg4_intra_rl_bits, uni_mpeg4_intra_rl_len);
1294
58
        init_uni_mpeg4_rl_tab(&ff_h263_rl_inter, uni_mpeg4_inter_rl_bits, uni_mpeg4_inter_rl_len);
1295
    }
1296
1297
58
    s->min_qcoeff               = -2048;
1298
58
    s->max_qcoeff               = 2047;
1299
58
    s->intra_ac_vlc_length      = uni_mpeg4_intra_rl_len;
1300
58
    s->intra_ac_vlc_last_length = uni_mpeg4_intra_rl_len + 128 * 64;
1301
58
    s->inter_ac_vlc_length      = uni_mpeg4_inter_rl_len;
1302
58
    s->inter_ac_vlc_last_length = uni_mpeg4_inter_rl_len + 128 * 64;
1303
58
    s->luma_dc_vlc_length       = uni_DCtab_lum_len;
1304
58
    s->ac_esc_length            = 7 + 2 + 1 + 6 + 1 + 12 + 1;
1305
58
    s->y_dc_scale_table         = ff_mpeg4_y_dc_scale_table;
1306
58
    s->c_dc_scale_table         = ff_mpeg4_c_dc_scale_table;
1307
1308
58
    if (s->avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1309
17
        s->avctx->extradata = av_malloc(1024);
1310
17
        init_put_bits(&s->pb, s->avctx->extradata, 1024);
1311
1312
17
        if (!(s->workaround_bugs & FF_BUG_MS))
1313
17
            mpeg4_encode_visual_object_header(s);
1314
17
        mpeg4_encode_vol_header(s, 0, 0);
1315
1316
//            ff_mpeg4_stuffing(&s->pb); ?
1317
17
        flush_put_bits(&s->pb);
1318
17
        s->avctx->extradata_size = (put_bits_count(&s->pb) + 7) >> 3;
1319
    }
1320
58
    return 0;
1321
}
1322
1323
1760
void ff_mpeg4_init_partitions(MpegEncContext *s)
1324
{
1325
1760
    uint8_t *start = put_bits_ptr(&s->pb);
1326
1760
    uint8_t *end   = s->pb.buf_end;
1327
1760
    int size       = end - start;
1328
1760
    int pb_size    = (((intptr_t)start + size / 3) & (~3)) - (intptr_t)start;
1329
1760
    int tex_size   = (size - 2 * pb_size) & (~3);
1330
1331
1760
    set_put_bits_buffer_size(&s->pb, pb_size);
1332
1760
    init_put_bits(&s->tex_pb, start + pb_size, tex_size);
1333
1760
    init_put_bits(&s->pb2, start + pb_size + tex_size, pb_size);
1334
1760
}
1335
1336
1760
void ff_mpeg4_merge_partitions(MpegEncContext *s)
1337
{
1338
1760
    const int pb2_len    = put_bits_count(&s->pb2);
1339
1760
    const int tex_pb_len = put_bits_count(&s->tex_pb);
1340
1760
    const int bits       = put_bits_count(&s->pb);
1341
1342
1760
    if (s->pict_type == AV_PICTURE_TYPE_I) {
1343
512
        put_bits(&s->pb, 19, DC_MARKER);
1344
512
        s->misc_bits  += 19 + pb2_len + bits - s->last_bits;
1345
512
        s->i_tex_bits += tex_pb_len;
1346
    } else {
1347
1248
        put_bits(&s->pb, 17, MOTION_MARKER);
1348
1248
        s->misc_bits  += 17 + pb2_len;
1349
1248
        s->mv_bits    += bits - s->last_bits;
1350
1248
        s->p_tex_bits += tex_pb_len;
1351
    }
1352
1353
1760
    flush_put_bits(&s->pb2);
1354
1760
    flush_put_bits(&s->tex_pb);
1355
1356
1760
    set_put_bits_buffer_size(&s->pb, s->pb2.buf_end - s->pb.buf);
1357
1760
    avpriv_copy_bits(&s->pb, s->pb2.buf, pb2_len);
1358
1760
    avpriv_copy_bits(&s->pb, s->tex_pb.buf, tex_pb_len);
1359
1760
    s->last_bits = put_bits_count(&s->pb);
1360
1760
}
1361
1362
3323
void ff_mpeg4_encode_video_packet_header(MpegEncContext *s)
1363
{
1364
3323
    int mb_num_bits = av_log2(s->mb_num - 1) + 1;
1365
1366
3323
    put_bits(&s->pb, ff_mpeg4_get_video_packet_prefix_length(s), 0);
1367
3323
    put_bits(&s->pb, 1, 1);
1368
1369
3323
    put_bits(&s->pb, mb_num_bits, s->mb_x + s->mb_y * s->mb_width);
1370
3323
    put_bits(&s->pb, s->quant_precision, s->qscale);
1371
3323
    put_bits(&s->pb, 1, 0); /* no HEC */
1372
3323
}
1373
1374
#define OFFSET(x) offsetof(MpegEncContext, x)
1375
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1376
static const AVOption options[] = {
1377
    { "data_partitioning", "Use data partitioning.",      OFFSET(data_partitioning), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1378
    { "alternate_scan",    "Enable alternate scantable.", OFFSET(alternate_scan),    AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
1379
    FF_MPV_COMMON_OPTS
1380
    FF_MPEG4_PROFILE_OPTS
1381
    { NULL },
1382
};
1383
1384
static const AVClass mpeg4enc_class = {
1385
    .class_name = "MPEG4 encoder",
1386
    .item_name  = av_default_item_name,
1387
    .option     = options,
1388
    .version    = LIBAVUTIL_VERSION_INT,
1389
};
1390
1391
AVCodec ff_mpeg4_encoder = {
1392
    .name           = "mpeg4",
1393
    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 part 2"),
1394
    .type           = AVMEDIA_TYPE_VIDEO,
1395
    .id             = AV_CODEC_ID_MPEG4,
1396
    .priv_data_size = sizeof(MpegEncContext),
1397
    .init           = encode_init,
1398
    .encode2        = ff_mpv_encode_picture,
1399
    .close          = ff_mpv_encode_end,
1400
    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
1401
    .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS,
1402
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
1403
    .priv_class     = &mpeg4enc_class,
1404
};