/src/ffmpeg/libavcodec/h261enc.c
Line | Count | Source |
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 | | |
28 | | #include "libavutil/attributes.h" |
29 | | #include "libavutil/avassert.h" |
30 | | #include "libavutil/thread.h" |
31 | | #include "avcodec.h" |
32 | | #include "codec_internal.h" |
33 | | #include "mpegutils.h" |
34 | | #include "mpegvideo.h" |
35 | | #include "h261.h" |
36 | | #include "h261enc.h" |
37 | | #include "mpegvideoenc.h" |
38 | | #include "put_bits.h" |
39 | | |
40 | 7.92M | #define H261_MAX_RUN 26 |
41 | 22.7M | #define H261_MAX_LEVEL 15 |
42 | 453 | #define H261_ESC_LEN (6 + 6 + 8) |
43 | 1 | #define MV_TAB_OFFSET 32 |
44 | | |
45 | | static struct VLCLUT { |
46 | | uint8_t len; |
47 | | uint16_t code; |
48 | | } vlc_lut[H261_MAX_RUN + 1][32 /* 0..2 * H261_MAX_LEN are used */]; |
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. |
53 | | static uint8_t mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1]; |
54 | | static uint8_t uni_h261_rl_len [64 * 128]; |
55 | | static uint8_t uni_h261_rl_len_last[64 * 128]; |
56 | | static uint8_t h261_mv_codes[64][2]; |
57 | | |
58 | | typedef struct H261EncContext { |
59 | | MPVMainEncContext s; |
60 | | |
61 | | int gob_number; |
62 | | enum { |
63 | | H261_QCIF = 0, |
64 | | H261_CIF = 1, |
65 | | } format; |
66 | | } H261EncContext; |
67 | | |
68 | | static int h261_encode_picture_header(MPVMainEncContext *const m) |
69 | 419 | { |
70 | 419 | H261EncContext *const h = (H261EncContext *)m; |
71 | 419 | MPVEncContext *const s = &h->s.s; |
72 | 419 | int temp_ref; |
73 | | |
74 | 419 | put_bits_assume_flushed(&s->pb); |
75 | | |
76 | 419 | put_bits(&s->pb, 20, 0x10); /* PSC */ |
77 | | |
78 | 419 | temp_ref = s->picture_number * 30000LL * s->c.avctx->time_base.num / |
79 | 419 | (1001LL * s->c.avctx->time_base.den); // FIXME maybe this should use a timestamp |
80 | 419 | put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */ |
81 | | |
82 | 419 | put_bits(&s->pb, 1, 0); /* split screen off */ |
83 | 419 | put_bits(&s->pb, 1, 0); /* camera off */ |
84 | 419 | put_bits(&s->pb, 1, s->c.pict_type == AV_PICTURE_TYPE_I); /* freeze picture release on/off */ |
85 | | |
86 | 419 | put_bits(&s->pb, 1, h->format); /* 0 == QCIF, 1 == CIF */ |
87 | | |
88 | 419 | put_bits(&s->pb, 1, 1); /* still image mode */ |
89 | 419 | put_bits(&s->pb, 1, 1); /* reserved */ |
90 | | |
91 | 419 | put_bits(&s->pb, 1, 0); /* no PEI */ |
92 | 419 | h->gob_number = h->format - 1; |
93 | 419 | s->mb_skip_run = 0; |
94 | | |
95 | 419 | return 0; |
96 | 419 | } |
97 | | |
98 | | /** |
99 | | * Encode a group of blocks header. |
100 | | */ |
101 | | static void h261_encode_gob_header(MPVEncContext *const s, int mb_line) |
102 | 3.32k | { |
103 | 3.32k | H261EncContext *const h = (H261EncContext *)s; |
104 | 3.32k | if (h->format == H261_QCIF) { |
105 | 567 | h->gob_number += 2; // QCIF |
106 | 2.76k | } else { |
107 | 2.76k | h->gob_number++; // CIF |
108 | 2.76k | } |
109 | 3.32k | put_bits(&s->pb, 16, 1); /* GBSC */ |
110 | 3.32k | put_bits(&s->pb, 4, h->gob_number); /* GN */ |
111 | 3.32k | put_bits(&s->pb, 5, s->c.qscale); /* GQUANT */ |
112 | 3.32k | put_bits(&s->pb, 1, 0); /* no GEI */ |
113 | 3.32k | s->mb_skip_run = 0; |
114 | 3.32k | s->c.last_mv[0][0][0] = 0; |
115 | 3.32k | s->c.last_mv[0][0][1] = 0; |
116 | 3.32k | } |
117 | | |
118 | | void ff_h261_reorder_mb_index(MPVEncContext *const s) |
119 | 109k | { |
120 | 109k | const H261EncContext *const h = (H261EncContext*)s; |
121 | 109k | int index = s->c.mb_x + s->c.mb_y * s->c.mb_width; |
122 | | |
123 | 109k | if (index % 11 == 0) { |
124 | 9.98k | if (index % 33 == 0) |
125 | 3.32k | h261_encode_gob_header(s, 0); |
126 | 9.98k | s->c.last_mv[0][0][0] = 0; |
127 | 9.98k | s->c.last_mv[0][0][1] = 0; |
128 | 9.98k | } |
129 | | |
130 | | /* for CIF the GOB's are fragmented in the middle of a scanline |
131 | | * that's why we need to adjust the x and y index of the macroblocks */ |
132 | 109k | if (h->format == H261_CIF) { |
133 | 91.0k | s->c.mb_x = index % 11; |
134 | 91.0k | index /= 11; |
135 | 91.0k | s->c.mb_y = index % 3; |
136 | 91.0k | index /= 3; |
137 | 91.0k | s->c.mb_x += 11 * (index % 2); |
138 | 91.0k | index /= 2; |
139 | 91.0k | s->c.mb_y += 3 * index; |
140 | | |
141 | 91.0k | ff_init_block_index(&s->c); |
142 | 91.0k | ff_update_block_index(&s->c, 8, 0, 1); |
143 | 91.0k | } |
144 | 109k | } |
145 | | |
146 | | static void h261_encode_motion(PutBitContext *pb, int val) |
147 | 0 | { |
148 | 0 | put_bits(pb, h261_mv_codes[MV_TAB_OFFSET + val][1], |
149 | 0 | h261_mv_codes[MV_TAB_OFFSET + val][0]); |
150 | 0 | } |
151 | | |
152 | | static inline int get_cbp(const int block_last_index[6]) |
153 | 0 | { |
154 | 0 | int i, cbp; |
155 | 0 | cbp = 0; |
156 | 0 | for (i = 0; i < 6; i++) |
157 | 0 | if (block_last_index[i] >= 0) |
158 | 0 | cbp |= 1 << (5 - i); |
159 | 0 | return cbp; |
160 | 0 | } |
161 | | |
162 | | /** |
163 | | * Encode an 8x8 block. |
164 | | * @param block the 8x8 block |
165 | | * @param n block index (0-3 are luma, 4-5 are chroma) |
166 | | */ |
167 | | static void h261_encode_block(H261EncContext *h, int16_t *block, int n) |
168 | 658k | { |
169 | 658k | MPVEncContext *const s = &h->s.s; |
170 | 658k | int level, run, i, j, last_index, last_non_zero; |
171 | | |
172 | 658k | if (s->c.mb_intra) { |
173 | | /* DC coef */ |
174 | 658k | level = block[0]; |
175 | | /* 255 cannot be represented, so we clamp */ |
176 | 658k | if (level > 254) { |
177 | 565 | level = 254; |
178 | 565 | block[0] = 254; |
179 | 565 | } |
180 | | /* 0 cannot be represented also */ |
181 | 658k | else if (level < 1) { |
182 | 564k | level = 1; |
183 | 564k | block[0] = 1; |
184 | 564k | } |
185 | 658k | if (level == 128) |
186 | 6.88k | put_bits(&s->pb, 8, 0xff); |
187 | 651k | else |
188 | 651k | put_bits(&s->pb, 8, level); |
189 | 658k | i = 1; |
190 | 658k | } else if ((block[0] == 1 || block[0] == -1) && |
191 | 0 | (s->c.block_last_index[n] > -1)) { |
192 | | // special case |
193 | 0 | put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3); |
194 | 0 | i = 1; |
195 | 0 | } else { |
196 | 0 | i = 0; |
197 | 0 | } |
198 | | |
199 | | /* AC coefs */ |
200 | 658k | last_index = s->c.block_last_index[n]; |
201 | 658k | last_non_zero = i - 1; |
202 | 5.74M | for (; i <= last_index; i++) { |
203 | 5.08M | j = s->c.intra_scantable.permutated[i]; |
204 | 5.08M | level = block[j]; |
205 | 5.08M | if (level) { |
206 | 3.96M | run = i - last_non_zero - 1; |
207 | | |
208 | 3.96M | if (run <= H261_MAX_RUN && |
209 | 3.96M | (unsigned)(level + H261_MAX_LEVEL) <= 2 * H261_MAX_LEVEL && |
210 | 3.68M | vlc_lut[run][level + H261_MAX_LEVEL].len) { |
211 | 3.57M | put_bits(&s->pb, vlc_lut[run][level + H261_MAX_LEVEL].len, |
212 | 3.57M | vlc_lut[run][level + H261_MAX_LEVEL].code); |
213 | 3.57M | } else { |
214 | | /* Escape */ |
215 | 390k | put_bits(&s->pb, 6 + 6, (1 << 6) | run); |
216 | 390k | av_assert1(level != 0); |
217 | 390k | av_assert1(FFABS(level) <= 127); |
218 | 390k | put_sbits(&s->pb, 8, level); |
219 | 390k | } |
220 | 3.96M | last_non_zero = i; |
221 | 3.96M | } |
222 | 5.08M | } |
223 | 658k | if (last_index > -1) |
224 | 658k | put_bits(&s->pb, 2, 0x2); // EOB |
225 | 658k | } |
226 | | |
227 | | static void h261_encode_mb(MPVEncContext *const s, int16_t block[6][64], |
228 | | int motion_x, int motion_y) |
229 | 109k | { |
230 | | /* The following is only allowed because this encoder |
231 | | * does not use slice threading. */ |
232 | 109k | H261EncContext *const h = (H261EncContext *)s; |
233 | 109k | int mvd, mv_diff_x, mv_diff_y, i, cbp; |
234 | 109k | cbp = 63; // avoid warning |
235 | 109k | mvd = 0; |
236 | | |
237 | 109k | s->c.mtype = 0; |
238 | | |
239 | 109k | if (!s->c.mb_intra) { |
240 | | /* compute cbp */ |
241 | 0 | cbp = get_cbp(s->c.block_last_index); |
242 | | |
243 | | /* mvd indicates if this block is motion compensated */ |
244 | 0 | mvd = motion_x | motion_y; |
245 | |
|
246 | 0 | if ((cbp | mvd) == 0) { |
247 | | /* skip macroblock */ |
248 | 0 | s->mb_skip_run++; |
249 | 0 | s->c.last_mv[0][0][0] = 0; |
250 | 0 | s->c.last_mv[0][0][1] = 0; |
251 | 0 | s->c.qscale -= s->dquant; |
252 | 0 | return; |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | | /* MB is not skipped, encode MBA */ |
257 | 109k | put_bits(&s->pb, |
258 | 109k | ff_h261_mba_bits[s->mb_skip_run], |
259 | 109k | ff_h261_mba_code[s->mb_skip_run]); |
260 | 109k | s->mb_skip_run = 0; |
261 | | |
262 | | /* calculate MTYPE */ |
263 | 109k | if (!s->c.mb_intra) { |
264 | 0 | s->c.mtype++; |
265 | |
|
266 | 0 | if (mvd || s->loop_filter) |
267 | 0 | s->c.mtype += 3; |
268 | 0 | if (s->loop_filter) |
269 | 0 | s->c.mtype += 3; |
270 | 0 | if (cbp) |
271 | 0 | s->c.mtype++; |
272 | 0 | av_assert1(s->c.mtype > 1); |
273 | 0 | } |
274 | | |
275 | 109k | if (s->dquant && cbp) { |
276 | 0 | s->c.mtype++; |
277 | 0 | } else |
278 | 109k | s->c.qscale -= s->dquant; |
279 | | |
280 | 109k | put_bits(&s->pb, |
281 | 109k | ff_h261_mtype_bits[s->c.mtype], |
282 | 109k | ff_h261_mtype_code[s->c.mtype]); |
283 | | |
284 | 109k | s->c.mtype = ff_h261_mtype_map[s->c.mtype]; |
285 | | |
286 | 109k | if (IS_QUANT(s->c.mtype)) { |
287 | 0 | ff_set_qscale(&s->c, s->c.qscale + s->dquant); |
288 | 0 | put_bits(&s->pb, 5, s->c.qscale); |
289 | 0 | } |
290 | | |
291 | 109k | if (IS_16X16(s->c.mtype)) { |
292 | 0 | mv_diff_x = (motion_x >> 1) - s->c.last_mv[0][0][0]; |
293 | 0 | mv_diff_y = (motion_y >> 1) - s->c.last_mv[0][0][1]; |
294 | 0 | s->c.last_mv[0][0][0] = (motion_x >> 1); |
295 | 0 | s->c.last_mv[0][0][1] = (motion_y >> 1); |
296 | 0 | h261_encode_motion(&s->pb, mv_diff_x); |
297 | 0 | h261_encode_motion(&s->pb, mv_diff_y); |
298 | 0 | } |
299 | | |
300 | 109k | if (HAS_CBP(s->c.mtype)) { |
301 | 0 | av_assert1(cbp > 0); |
302 | 0 | put_bits(&s->pb, |
303 | 0 | ff_h261_cbp_tab[cbp - 1][1], |
304 | 0 | ff_h261_cbp_tab[cbp - 1][0]); |
305 | 0 | } |
306 | 768k | for (i = 0; i < 6; i++) |
307 | | /* encode each block */ |
308 | 658k | h261_encode_block(h, block[i], i); |
309 | | |
310 | 109k | if (!IS_16X16(s->c.mtype)) { |
311 | 109k | s->c.last_mv[0][0][0] = 0; |
312 | 109k | s->c.last_mv[0][0][1] = 0; |
313 | 109k | } |
314 | 109k | } |
315 | | |
316 | | static av_cold void h261_encode_init_static(void) |
317 | 1 | { |
318 | 1 | uint8_t (*const mv_codes)[2] = h261_mv_codes + MV_TAB_OFFSET; |
319 | 1 | memset(uni_h261_rl_len, H261_ESC_LEN, sizeof(uni_h261_rl_len)); |
320 | 1 | memset(uni_h261_rl_len_last, H261_ESC_LEN + 2 /* EOB */, sizeof(uni_h261_rl_len_last)); |
321 | | |
322 | | // The following loop is over the ordinary elements, not EOB or escape. |
323 | 64 | for (size_t i = 1; i < FF_ARRAY_ELEMS(ff_h261_tcoeff_vlc) - 1; i++) { |
324 | 63 | unsigned run = ff_h261_tcoeff_run[i]; |
325 | 63 | unsigned level = ff_h261_tcoeff_level[i]; |
326 | 63 | unsigned len = ff_h261_tcoeff_vlc[i][1] + 1 /* sign */; |
327 | 63 | unsigned code = ff_h261_tcoeff_vlc[i][0]; |
328 | | |
329 | 63 | vlc_lut[run][H261_MAX_LEVEL + level] = (struct VLCLUT){ len, code << 1 }; |
330 | 63 | vlc_lut[run][H261_MAX_LEVEL - level] = (struct VLCLUT){ len, (code << 1) | 1 }; |
331 | | |
332 | 63 | uni_h261_rl_len [UNI_AC_ENC_INDEX(run, 64 + level)] = len; |
333 | 63 | uni_h261_rl_len [UNI_AC_ENC_INDEX(run, 64 - level)] = len; |
334 | 63 | uni_h261_rl_len_last[UNI_AC_ENC_INDEX(run, 64 + level)] = len + 2; |
335 | 63 | uni_h261_rl_len_last[UNI_AC_ENC_INDEX(run, 64 - level)] = len + 2; |
336 | 63 | } |
337 | | |
338 | 16 | for (ptrdiff_t i = 1;; i++) { |
339 | | // sign-one MV codes; diff -16..-1, 16..31 |
340 | 16 | mv_codes[32 - i][0] = mv_codes[-i][0] = (ff_h261_mv_tab[i][0] << 1) | 1 /* sign */; |
341 | 16 | mv_codes[32 - i][1] = mv_codes[-i][1] = ff_h261_mv_tab[i][1] + 1; |
342 | 16 | if (i == 16) |
343 | 1 | break; |
344 | | // sign-zero MV codes: diff -31..-17, 1..15 |
345 | 15 | mv_codes[i][0] = mv_codes[i - 32][0] = ff_h261_mv_tab[i][0] << 1; |
346 | 15 | mv_codes[i][1] = mv_codes[i - 32][1] = ff_h261_mv_tab[i][1] + 1; |
347 | 15 | } |
348 | | // MV code for difference zero; has no sign |
349 | 1 | mv_codes[0][0] = 1; |
350 | 1 | mv_codes[0][1] = 1; |
351 | 1 | } |
352 | | |
353 | | static av_cold int h261_encode_init(AVCodecContext *avctx) |
354 | 514 | { |
355 | 514 | static AVOnce init_static_once = AV_ONCE_INIT; |
356 | 514 | H261EncContext *const h = avctx->priv_data; |
357 | 514 | MPVEncContext *const s = &h->s.s; |
358 | | |
359 | 514 | if (avctx->width == 176 && avctx->height == 144) { |
360 | 200 | h->format = H261_QCIF; |
361 | 314 | } else if (avctx->width == 352 && avctx->height == 288) { |
362 | 251 | h->format = H261_CIF; |
363 | 251 | } else { |
364 | 63 | av_log(avctx, AV_LOG_ERROR, |
365 | 63 | "The specified picture size of %dx%d is not valid for the " |
366 | 63 | "H.261 codec.\nValid sizes are 176x144, 352x288\n", |
367 | 63 | avctx->width, avctx->height); |
368 | 63 | return AVERROR(EINVAL); |
369 | 63 | } |
370 | 451 | h->s.encode_picture_header = h261_encode_picture_header; |
371 | 451 | s->encode_mb = h261_encode_mb; |
372 | | |
373 | 451 | s->min_qcoeff = -127; |
374 | 451 | s->max_qcoeff = 127; |
375 | 451 | s->ac_esc_length = H261_ESC_LEN; |
376 | | |
377 | 451 | s->me.mv_penalty = mv_penalty; |
378 | | |
379 | 451 | s->intra_ac_vlc_length = s->inter_ac_vlc_length = uni_h261_rl_len; |
380 | 451 | s->intra_ac_vlc_last_length = s->inter_ac_vlc_last_length = uni_h261_rl_len_last; |
381 | 451 | ff_thread_once(&init_static_once, h261_encode_init_static); |
382 | | |
383 | 451 | return ff_mpv_encode_init(avctx); |
384 | 514 | } |
385 | | |
386 | | const FFCodec ff_h261_encoder = { |
387 | | .p.name = "h261", |
388 | | CODEC_LONG_NAME("H.261"), |
389 | | .p.type = AVMEDIA_TYPE_VIDEO, |
390 | | .p.id = AV_CODEC_ID_H261, |
391 | | .p.priv_class = &ff_mpv_enc_class, |
392 | | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, |
393 | | .priv_data_size = sizeof(H261EncContext), |
394 | | .init = h261_encode_init, |
395 | | FF_CODEC_ENCODE_CB(ff_mpv_encode_picture), |
396 | | .close = ff_mpv_encode_end, |
397 | | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
398 | | CODEC_PIXFMTS(AV_PIX_FMT_YUV420P), |
399 | | .color_ranges = AVCOL_RANGE_MPEG, |
400 | | }; |