1 /*
2 * H.261 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
39
40 #define H261_MAX_RUN 26
41 #define H261_MAX_LEVEL 15
42 #define H261_ESC_LEN (6 + 6 + 8)
43 #define MV_TAB_OFFSET 32
44
49
50 // Not const despite never being initialized because doing so would
51 // put it into .rodata instead of .bss and bloat the binary.
52 // mv_penalty exists so that the motion estimation code can avoid branches.
57
60
62
64 enum {
69
71 {
74 int temp_ref;
75
77
79
80 temp_ref =
s->picture_number * 30000LL *
s->c.avctx->time_base.num /
81 (1001LL *
s->c.avctx->time_base.den);
// FIXME maybe this should use a timestamp
82 put_sbits(&
s->pb, 5, temp_ref);
/* TemporalReference */
83
84 put_bits(&
s->pb, 1, 0);
/* split screen off */
87
88 put_bits(&
s->pb, 1,
h->format);
/* 0 == QCIF, 1 == CIF */
89
90 put_bits(&
s->pb, 1, 1);
/* still image mode */
92
94 h->gob_number =
h->format - 1;
96
97 return 0;
98 }
99
100 /**
101 * Encode a group of blocks header.
102 */
104 {
106 if (
h->format == H261_QCIF) {
107 h->gob_number += 2;
// QCIF
108 } else {
109 h->gob_number++;
// CIF
110 }
113 put_bits(&
s->pb, 5,
s->c.qscale);
/* GQUANT */
116 s->c.last_mv[0][0][0] = 0;
117 s->c.last_mv[0][0][1] = 0;
118 }
119
121 {
123 int index =
s->c.mb_x +
s->c.mb_y *
s->c.mb_width;
124
125 if (
index % 11 == 0) {
128 s->c.last_mv[0][0][0] = 0;
129 s->c.last_mv[0][0][1] = 0;
130 }
131
132 /* for CIF the GOB's are fragmented in the middle of a scanline
133 * that's why we need to adjust the x and y index of the macroblocks */
134 if (
h->format == H261_CIF) {
139 s->c.mb_x += 11 * (
index % 2);
142
145 }
146 }
147
149 {
152 }
153
154 static inline int get_cbp(
const int block_last_index[6])
155 {
157 cbp = 0;
158 for (
i = 0;
i < 6;
i++)
159 if (block_last_index[
i] >= 0)
161 return cbp;
162 }
163
164 /**
165 * Encode an 8x8 block.
166 * @param block the 8x8 block
167 * @param n block index (0-3 are luma, 4-5 are chroma)
168 */
170 {
172 int level,
run,
i, j, last_index, last_non_zero;
173
175 /* DC coef */
177 /* 255 cannot be represented, so we clamp */
181 }
182 /* 0 cannot be represented also */
183 else if (
level < 1) {
186 }
189 else
192 }
else if ((
block[0] == 1 ||
block[0] == -1) &&
193 (
s->c.block_last_index[n] > -1)) {
194 // special case
197 } else {
199 }
200
201 /* AC coefs */
202 last_index =
s->c.block_last_index[n];
203 last_non_zero =
i - 1;
204 for (;
i <= last_index;
i++) {
205 j =
s->c.intra_scantable.permutated[
i];
208 run =
i - last_non_zero - 1;
209
215 } else {
216 /* Escape */
221 }
223 }
224 }
225 if (last_index > -1)
227 }
228
230 int motion_x, int motion_y)
231 {
232 /* The following is only allowed because this encoder
233 * does not use slice threading. */
236 int mvd, mv_diff_x, mv_diff_y,
i, cbp;
237 cbp = 63; // avoid warning
238 mvd = 0;
239
241
242 if (!
s->c.mb_intra) {
243 /* compute cbp */
244 cbp =
get_cbp(
s->c.block_last_index);
245
246 /* mvd indicates if this block is motion compensated */
247 mvd = motion_x | motion_y;
248
249 if ((cbp | mvd) == 0) {
250 /* skip macroblock */
252 s->c.last_mv[0][0][0] = 0;
253 s->c.last_mv[0][0][1] = 0;
254 s->c.qscale -=
s->dquant;
255 return;
256 }
257 }
258
259 /* MB is not skipped, encode MBA */
264
265 /* calculate MTYPE */
266 if (!
s->c.mb_intra) {
268
269 if (mvd ||
s->loop_filter)
273 if (cbp)
276 }
277
278 if (
s->dquant && cbp) {
280 } else
281 s->c.qscale -=
s->dquant;
282
286
288
292 }
293
295 mv_diff_x = (motion_x >> 1) -
s->c.last_mv[0][0][0];
296 mv_diff_y = (motion_y >> 1) -
s->c.last_mv[0][0][1];
297 s->c.last_mv[0][0][0] = (motion_x >> 1);
298 s->c.last_mv[0][0][1] = (motion_y >> 1);
301 }
302
308 }
309 for (
i = 0;
i < 6;
i++)
310 /* encode each block */
312
314 s->c.last_mv[0][0][0] = 0;
315 s->c.last_mv[0][0][1] = 0;
316 }
317 }
318
320 {
324
325 // The following loop is over the ordinary elements, not EOB or escape.
331
334
339 }
340
341 for (ptrdiff_t
i = 1;;
i++) {
342 // sign-one MV codes; diff -16..-1, 16..31
343 mv_codes[32 -
i][0] = mv_codes[-
i][0] = (
ff_h261_mv_tab[
i][0] << 1) | 1
/* sign */;
346 break;
347 // sign-zero MV codes: diff -31..-17, 1..15
350 }
351 // MV code for difference zero; has no sign
352 mv_codes[0][0] = 1;
353 mv_codes[0][1] = 1;
354 }
355
357 {
361
363 h->format = H261_QCIF;
364 }
else if (avctx->
width == 352 && avctx->
height == 288) {
365 h->format = H261_CIF;
366 } else {
368 "The specified picture size of %dx%d is not valid for the "
369 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
372 }
373 s->c.private_ctx = &
h->common;
376
377 s->min_qcoeff = -127;
380
382
386
388 }
389
404 };