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