Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4349 Serge 1
/*
2
 * H261 encoder
3
 * Copyright (c) 2002-2004 Michael Niedermayer 
4
 * Copyright (c) 2004 Maarten Daniels
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
/**
24
 * @file
25
 * H.261 encoder.
26
 */
27
 
28
#include "libavutil/attributes.h"
29
#include "libavutil/avassert.h"
30
#include "avcodec.h"
31
#include "mpegvideo.h"
32
#include "h263.h"
33
#include "h261.h"
34
 
35
int ff_h261_get_picture_format(int width, int height)
36
{
37
    // QCIF
38
    if (width == 176 && height == 144)
39
        return 0;
40
    // CIF
41
    else if (width == 352 && height == 288)
42
        return 1;
43
    // ERROR
44
    else
45
        return -1;
46
}
47
 
48
void ff_h261_encode_picture_header(MpegEncContext *s, int picture_number)
49
{
50
    H261Context *h = (H261Context *)s;
51
    int format, temp_ref;
52
 
53
    avpriv_align_put_bits(&s->pb);
54
 
55
    /* Update the pointer to last GOB */
56
    s->ptr_lastgob = put_bits_ptr(&s->pb);
57
 
58
    put_bits(&s->pb, 20, 0x10); /* PSC */
59
 
60
    temp_ref = s->picture_number * (int64_t)30000 * s->avctx->time_base.num /
61
               (1001 * (int64_t)s->avctx->time_base.den);   // FIXME maybe this should use a timestamp
62
    put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */
63
 
64
    put_bits(&s->pb, 1, 0); /* split screen off */
65
    put_bits(&s->pb, 1, 0); /* camera  off */
66
    put_bits(&s->pb, 1, 0); /* freeze picture release off */
67
 
68
    format = ff_h261_get_picture_format(s->width, s->height);
69
 
70
    put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
71
 
72
    put_bits(&s->pb, 1, 0); /* still image mode */
73
    put_bits(&s->pb, 1, 0); /* reserved */
74
 
75
    put_bits(&s->pb, 1, 0); /* no PEI */
76
    if (format == 0)
77
        h->gob_number = -1;
78
    else
79
        h->gob_number = 0;
80
    h->current_mba = 0;
81
}
82
 
83
/**
84
 * Encode a group of blocks header.
85
 */
86
static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
87
{
88
    H261Context *h = (H261Context *)s;
89
    if (ff_h261_get_picture_format(s->width, s->height) == 0) {
90
        h->gob_number += 2; // QCIF
91
    } else {
92
        h->gob_number++;    // CIF
93
    }
94
    put_bits(&s->pb, 16, 1);            /* GBSC */
95
    put_bits(&s->pb, 4, h->gob_number); /* GN */
96
    put_bits(&s->pb, 5, s->qscale);     /* GQUANT */
97
    put_bits(&s->pb, 1, 0);             /* no GEI */
98
    h->current_mba  = 0;
99
    h->previous_mba = 0;
100
    h->current_mv_x = 0;
101
    h->current_mv_y = 0;
102
}
103
 
104
void ff_h261_reorder_mb_index(MpegEncContext *s)
105
{
106
    int index = s->mb_x + s->mb_y * s->mb_width;
107
 
108
    if (index % 33 == 0)
109
        h261_encode_gob_header(s, 0);
110
 
111
    /* for CIF the GOB's are fragmented in the middle of a scanline
112
     * that's why we need to adjust the x and y index of the macroblocks */
113
    if (ff_h261_get_picture_format(s->width, s->height) == 1) { // CIF
114
        s->mb_x  = index % 11;
115
        index   /= 11;
116
        s->mb_y  = index % 3;
117
        index   /= 3;
118
        s->mb_x += 11 * (index % 2);
119
        index   /= 2;
120
        s->mb_y += 3 * index;
121
 
122
        ff_init_block_index(s);
123
        ff_update_block_index(s);
124
    }
125
}
126
 
127
static void h261_encode_motion(H261Context *h, int val)
128
{
129
    MpegEncContext *const s = &h->s;
130
    int sign, code;
131
    if (val == 0) {
132
        code = 0;
133
        put_bits(&s->pb, ff_h261_mv_tab[code][1], ff_h261_mv_tab[code][0]);
134
    } else {
135
        if (val > 15)
136
            val -= 32;
137
        if (val < -16)
138
            val += 32;
139
        sign = val < 0;
140
        code = sign ? -val : val;
141
        put_bits(&s->pb, ff_h261_mv_tab[code][1], ff_h261_mv_tab[code][0]);
142
        put_bits(&s->pb, 1, sign);
143
    }
144
}
145
 
146
static inline int get_cbp(MpegEncContext *s, int16_t block[6][64])
147
{
148
    int i, cbp;
149
    cbp = 0;
150
    for (i = 0; i < 6; i++)
151
        if (s->block_last_index[i] >= 0)
152
            cbp |= 1 << (5 - i);
153
    return cbp;
154
}
155
 
156
/**
157
 * Encode an 8x8 block.
158
 * @param block the 8x8 block
159
 * @param n block index (0-3 are luma, 4-5 are chroma)
160
 */
161
static void h261_encode_block(H261Context *h, int16_t *block, int n)
162
{
163
    MpegEncContext *const s = &h->s;
164
    int level, run, i, j, last_index, last_non_zero, sign, slevel, code;
165
    RLTable *rl;
166
 
167
    rl = &ff_h261_rl_tcoeff;
168
    if (s->mb_intra) {
169
        /* DC coef */
170
        level = block[0];
171
        /* 255 cannot be represented, so we clamp */
172
        if (level > 254) {
173
            level    = 254;
174
            block[0] = 254;
175
        }
176
        /* 0 cannot be represented also */
177
        else if (level < 1) {
178
            level    = 1;
179
            block[0] = 1;
180
        }
181
        if (level == 128)
182
            put_bits(&s->pb, 8, 0xff);
183
        else
184
            put_bits(&s->pb, 8, level);
185
        i = 1;
186
    } else if ((block[0] == 1 || block[0] == -1) &&
187
               (s->block_last_index[n] > -1)) {
188
        // special case
189
        put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
190
        i = 1;
191
    } else {
192
        i = 0;
193
    }
194
 
195
    /* AC coefs */
196
    last_index    = s->block_last_index[n];
197
    last_non_zero = i - 1;
198
    for (; i <= last_index; i++) {
199
        j     = s->intra_scantable.permutated[i];
200
        level = block[j];
201
        if (level) {
202
            run    = i - last_non_zero - 1;
203
            sign   = 0;
204
            slevel = level;
205
            if (level < 0) {
206
                sign  = 1;
207
                level = -level;
208
            }
209
            code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/,
210
                                run, level);
211
            if (run == 0 && level < 16)
212
                code += 1;
213
            put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
214
            if (code == rl->n) {
215
                put_bits(&s->pb, 6, run);
216
                av_assert1(slevel != 0);
217
                av_assert1(level <= 127);
218
                put_sbits(&s->pb, 8, slevel);
219
            } else {
220
                put_bits(&s->pb, 1, sign);
221
            }
222
            last_non_zero = i;
223
        }
224
    }
225
    if (last_index > -1)
226
        put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]); // EOB
227
}
228
 
229
void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
230
                       int motion_x, int motion_y)
231
{
232
    H261Context *h = (H261Context *)s;
233
    int mvd, mv_diff_x, mv_diff_y, i, cbp;
234
    cbp = 63; // avoid warning
235
    mvd = 0;
236
 
237
    h->current_mba++;
238
    h->mtype = 0;
239
 
240
    if (!s->mb_intra) {
241
        /* compute cbp */
242
        cbp = get_cbp(s, block);
243
 
244
        /* mvd indicates if this block is motion compensated */
245
        mvd = motion_x | motion_y;
246
 
247
        if ((cbp | mvd | s->dquant) == 0) {
248
            /* skip macroblock */
249
            s->skip_count++;
250
            h->current_mv_x = 0;
251
            h->current_mv_y = 0;
252
            return;
253
        }
254
    }
255
 
256
    /* MB is not skipped, encode MBA */
257
    put_bits(&s->pb,
258
             ff_h261_mba_bits[(h->current_mba - h->previous_mba) - 1],
259
             ff_h261_mba_code[(h->current_mba - h->previous_mba) - 1]);
260
 
261
    /* calculate MTYPE */
262
    if (!s->mb_intra) {
263
        h->mtype++;
264
 
265
        if (mvd || s->loop_filter)
266
            h->mtype += 3;
267
        if (s->loop_filter)
268
            h->mtype += 3;
269
        if (cbp || s->dquant)
270
            h->mtype++;
271
        av_assert1(h->mtype > 1);
272
    }
273
 
274
    if (s->dquant)
275
        h->mtype++;
276
 
277
    put_bits(&s->pb,
278
             ff_h261_mtype_bits[h->mtype],
279
             ff_h261_mtype_code[h->mtype]);
280
 
281
    h->mtype = ff_h261_mtype_map[h->mtype];
282
 
283
    if (IS_QUANT(h->mtype)) {
284
        ff_set_qscale(s, s->qscale + s->dquant);
285
        put_bits(&s->pb, 5, s->qscale);
286
    }
287
 
288
    if (IS_16X16(h->mtype)) {
289
        mv_diff_x       = (motion_x >> 1) - h->current_mv_x;
290
        mv_diff_y       = (motion_y >> 1) - h->current_mv_y;
291
        h->current_mv_x = (motion_x >> 1);
292
        h->current_mv_y = (motion_y >> 1);
293
        h261_encode_motion(h, mv_diff_x);
294
        h261_encode_motion(h, mv_diff_y);
295
    }
296
 
297
    h->previous_mba = h->current_mba;
298
 
299
    if (HAS_CBP(h->mtype)) {
300
        av_assert1(cbp > 0);
301
        put_bits(&s->pb,
302
                 ff_h261_cbp_tab[cbp - 1][1],
303
                 ff_h261_cbp_tab[cbp - 1][0]);
304
    }
305
    for (i = 0; i < 6; i++)
306
        /* encode each block */
307
        h261_encode_block(h, block[i], i);
308
 
309
    if ((h->current_mba == 11) || (h->current_mba == 22) ||
310
        (h->current_mba == 33) || (!IS_16X16(h->mtype))) {
311
        h->current_mv_x = 0;
312
        h->current_mv_y = 0;
313
    }
314
}
315
 
316
av_cold void ff_h261_encode_init(MpegEncContext *s)
317
{
318
    ff_h261_common_init();
319
 
320
    s->min_qcoeff       = -127;
321
    s->max_qcoeff       = 127;
322
    s->y_dc_scale_table =
323
    s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
324
}
325
 
326
FF_MPV_GENERIC_CLASS(h261)
327
 
328
AVCodec ff_h261_encoder = {
329
    .name           = "h261",
330
    .long_name      = NULL_IF_CONFIG_SMALL("H.261"),
331
    .type           = AVMEDIA_TYPE_VIDEO,
332
    .id             = AV_CODEC_ID_H261,
333
    .priv_data_size = sizeof(H261Context),
334
    .init           = ff_MPV_encode_init,
335
    .encode2        = ff_MPV_encode_picture,
336
    .close          = ff_MPV_encode_end,
337
    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
338
                                                     AV_PIX_FMT_NONE },
339
    .priv_class     = &h261_class,
340
};