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