FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h261enc.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 181 192 94.3%
Functions: 9 9 100.0%
Branches: 67 80 83.8%

Line Branch Exec 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
39 #define H261_MAX_RUN 26
40 #define H261_MAX_LEVEL 15
41 #define H261_ESC_LEN (6 + 6 + 8)
42 #define MV_TAB_OFFSET 32
43
44 static struct VLCLUT {
45 uint8_t len;
46 uint16_t code;
47 } vlc_lut[H261_MAX_RUN + 1][32 /* 0..2 * H261_MAX_LEN are used */];
48
49 // Not const despite never being initialized because doing so would
50 // put it into .rodata instead of .bss and bloat the binary.
51 // mv_penalty exists so that the motion estimation code can avoid branches.
52 static uint8_t mv_penalty[MAX_FCODE + 1][MAX_DMV * 2 + 1];
53 static uint8_t uni_h261_rl_len [64 * 128];
54 static uint8_t uni_h261_rl_len_last[64 * 128];
55 static uint8_t h261_mv_codes[64][2];
56
57 typedef struct H261EncContext {
58 MpegEncContext s;
59
60 H261Context common;
61
62 int gob_number;
63 enum {
64 H261_QCIF = 0,
65 H261_CIF = 1,
66 } format;
67 } H261EncContext;
68
69 300 void ff_h261_encode_picture_header(MpegEncContext *s)
70 {
71 300 H261EncContext *const h = (H261EncContext *)s;
72 int temp_ref;
73
74 300 align_put_bits(&s->pb);
75
76 /* Update the pointer to last GOB */
77 300 s->ptr_lastgob = put_bits_ptr(&s->pb);
78
79 300 put_bits(&s->pb, 20, 0x10); /* PSC */
80
81 300 temp_ref = s->picture_number * 30000LL * s->avctx->time_base.num /
82 300 (1001LL * s->avctx->time_base.den); // FIXME maybe this should use a timestamp
83 300 put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */
84
85 300 put_bits(&s->pb, 1, 0); /* split screen off */
86 300 put_bits(&s->pb, 1, 0); /* camera off */
87 300 put_bits(&s->pb, 1, s->pict_type == AV_PICTURE_TYPE_I); /* freeze picture release on/off */
88
89 300 put_bits(&s->pb, 1, h->format); /* 0 == QCIF, 1 == CIF */
90
91 300 put_bits(&s->pb, 1, 1); /* still image mode */
92 300 put_bits(&s->pb, 1, 1); /* reserved */
93
94 300 put_bits(&s->pb, 1, 0); /* no PEI */
95 300 h->gob_number = h->format - 1;
96 300 s->mb_skip_run = 0;
97 300 }
98
99 /**
100 * Encode a group of blocks header.
101 */
102 3600 static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
103 {
104 3600 H261EncContext *const h = (H261EncContext *)s;
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3600 times.
3600 if (h->format == H261_QCIF) {
106 h->gob_number += 2; // QCIF
107 } else {
108 3600 h->gob_number++; // CIF
109 }
110 3600 put_bits(&s->pb, 16, 1); /* GBSC */
111 3600 put_bits(&s->pb, 4, h->gob_number); /* GN */
112 3600 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
113 3600 put_bits(&s->pb, 1, 0); /* no GEI */
114 3600 s->mb_skip_run = 0;
115 3600 s->last_mv[0][0][0] = 0;
116 3600 s->last_mv[0][0][1] = 0;
117 3600 }
118
119 118800 void ff_h261_reorder_mb_index(MpegEncContext *s)
120 {
121 118800 const H261EncContext *const h = (H261EncContext*)s;
122 118800 int index = s->mb_x + s->mb_y * s->mb_width;
123
124
2/2
✓ Branch 0 taken 10800 times.
✓ Branch 1 taken 108000 times.
118800 if (index % 11 == 0) {
125
2/2
✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 7200 times.
10800 if (index % 33 == 0)
126 3600 h261_encode_gob_header(s, 0);
127 10800 s->last_mv[0][0][0] = 0;
128 10800 s->last_mv[0][0][1] = 0;
129 }
130
131 /* for CIF the GOB's are fragmented in the middle of a scanline
132 * that's why we need to adjust the x and y index of the macroblocks */
133
1/2
✓ Branch 0 taken 118800 times.
✗ Branch 1 not taken.
118800 if (h->format == H261_CIF) {
134 118800 s->mb_x = index % 11;
135 118800 index /= 11;
136 118800 s->mb_y = index % 3;
137 118800 index /= 3;
138 118800 s->mb_x += 11 * (index % 2);
139 118800 index /= 2;
140 118800 s->mb_y += 3 * index;
141
142 118800 ff_init_block_index(s);
143 118800 ff_update_block_index(s, 8, 0, 1);
144 }
145 118800 }
146
147 205596 static void h261_encode_motion(PutBitContext *pb, int val)
148 {
149 205596 put_bits(pb, h261_mv_codes[MV_TAB_OFFSET + val][1],
150 205596 h261_mv_codes[MV_TAB_OFFSET + val][0]);
151 205596 }
152
153 105360 static inline int get_cbp(MpegEncContext *s, int16_t block[6][64])
154 {
155 int i, cbp;
156 105360 cbp = 0;
157
2/2
✓ Branch 0 taken 632160 times.
✓ Branch 1 taken 105360 times.
737520 for (i = 0; i < 6; i++)
158
2/2
✓ Branch 0 taken 276433 times.
✓ Branch 1 taken 355727 times.
632160 if (s->block_last_index[i] >= 0)
159 276433 cbp |= 1 << (5 - i);
160 105360 return cbp;
161 }
162
163 /**
164 * Encode an 8x8 block.
165 * @param block the 8x8 block
166 * @param n block index (0-3 are luma, 4-5 are chroma)
167 */
168 796986 static void h261_encode_block(H261EncContext *h, int16_t *block, int n)
169 {
170 796986 MpegEncContext *const s = &h->s;
171 int level, run, i, j, last_index, last_non_zero;
172
173
2/2
✓ Branch 0 taken 169980 times.
✓ Branch 1 taken 627006 times.
796986 if (s->mb_intra) {
174 /* DC coef */
175 169980 level = block[0];
176 /* 255 cannot be represented, so we clamp */
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 169980 times.
169980 if (level > 254) {
178 level = 254;
179 block[0] = 254;
180 }
181 /* 0 cannot be represented also */
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 169980 times.
169980 else if (level < 1) {
183 level = 1;
184 block[0] = 1;
185 }
186
2/2
✓ Branch 0 taken 2035 times.
✓ Branch 1 taken 167945 times.
169980 if (level == 128)
187 2035 put_bits(&s->pb, 8, 0xff);
188 else
189 167945 put_bits(&s->pb, 8, level);
190 169980 i = 1;
191
4/4
✓ Branch 0 taken 562894 times.
✓ Branch 1 taken 64112 times.
✓ Branch 2 taken 67071 times.
✓ Branch 3 taken 495823 times.
627006 } else if ((block[0] == 1 || block[0] == -1) &&
192
2/2
✓ Branch 0 taken 92217 times.
✓ Branch 1 taken 38966 times.
131183 (s->block_last_index[n] > -1)) {
193 // special case
194
2/2
✓ Branch 0 taken 44668 times.
✓ Branch 1 taken 47549 times.
92217 put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
195 92217 i = 1;
196 } else {
197 534789 i = 0;
198 }
199
200 /* AC coefs */
201 796986 last_index = s->block_last_index[n];
202 796986 last_non_zero = i - 1;
203
2/2
✓ Branch 0 taken 8994951 times.
✓ Branch 1 taken 796986 times.
9791937 for (; i <= last_index; i++) {
204 8994951 j = s->intra_scantable.permutated[i];
205 8994951 level = block[j];
206
2/2
✓ Branch 0 taken 2936627 times.
✓ Branch 1 taken 6058324 times.
8994951 if (level) {
207 2936627 run = i - last_non_zero - 1;
208
209
2/2
✓ Branch 0 taken 2928932 times.
✓ Branch 1 taken 7695 times.
2936627 if (run <= H261_MAX_RUN &&
210
2/2
✓ Branch 0 taken 2925517 times.
✓ Branch 1 taken 3415 times.
2928932 (unsigned)(level + H261_MAX_LEVEL) <= 2 * H261_MAX_LEVEL &&
211
2/2
✓ Branch 0 taken 2894680 times.
✓ Branch 1 taken 30837 times.
2925517 vlc_lut[run][level + H261_MAX_LEVEL].len) {
212 2894680 put_bits(&s->pb, vlc_lut[run][level + H261_MAX_LEVEL].len,
213 2894680 vlc_lut[run][level + H261_MAX_LEVEL].code);
214 } else {
215 /* Escape */
216 41947 put_bits(&s->pb, 6 + 6, (1 << 6) | run);
217 av_assert1(level != 0);
218 av_assert1(FFABS(level) <= 127);
219 41947 put_sbits(&s->pb, 8, level);
220 }
221 2936627 last_non_zero = i;
222 }
223 }
224
2/2
✓ Branch 0 taken 446413 times.
✓ Branch 1 taken 350573 times.
796986 if (last_index > -1)
225 446413 put_bits(&s->pb, 2, 0x2); // EOB
226 796986 }
227
228 133690 void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
229 int motion_x, int motion_y)
230 {
231 /* The following is only allowed because this encoder
232 * does not use slice threading. */
233 133690 H261EncContext *const h = (H261EncContext *)s;
234 133690 H261Context *const com = &h->common;
235 int mvd, mv_diff_x, mv_diff_y, i, cbp;
236 133690 cbp = 63; // avoid warning
237 133690 mvd = 0;
238
239 133690 com->mtype = 0;
240
241
2/2
✓ Branch 0 taken 105360 times.
✓ Branch 1 taken 28330 times.
133690 if (!s->mb_intra) {
242 /* compute cbp */
243 105360 cbp = get_cbp(s, block);
244
245 /* mvd indicates if this block is motion compensated */
246 105360 mvd = motion_x | motion_y;
247
248
2/2
✓ Branch 0 taken 859 times.
✓ Branch 1 taken 104501 times.
105360 if ((cbp | mvd) == 0) {
249 /* skip macroblock */
250 859 s->mb_skip_run++;
251 859 s->last_mv[0][0][0] = 0;
252 859 s->last_mv[0][0][1] = 0;
253 859 s->qscale -= s->dquant;
254 859 return;
255 }
256 }
257
258 /* MB is not skipped, encode MBA */
259 132831 put_bits(&s->pb,
260 132831 ff_h261_mba_bits[s->mb_skip_run],
261 132831 ff_h261_mba_code[s->mb_skip_run]);
262 132831 s->mb_skip_run = 0;
263
264 /* calculate MTYPE */
265
2/2
✓ Branch 0 taken 104501 times.
✓ Branch 1 taken 28330 times.
132831 if (!s->mb_intra) {
266 104501 com->mtype++;
267
268
4/4
✓ Branch 0 taken 3516 times.
✓ Branch 1 taken 100985 times.
✓ Branch 2 taken 1813 times.
✓ Branch 3 taken 1703 times.
104501 if (mvd || s->loop_filter)
269 102798 com->mtype += 3;
270
2/2
✓ Branch 0 taken 51663 times.
✓ Branch 1 taken 52838 times.
104501 if (s->loop_filter)
271 51663 com->mtype += 3;
272
2/2
✓ Branch 0 taken 85004 times.
✓ Branch 1 taken 19497 times.
104501 if (cbp)
273 85004 com->mtype++;
274 av_assert1(com->mtype > 1);
275 }
276
277
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 132831 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
132831 if (s->dquant && cbp) {
278 com->mtype++;
279 } else
280 132831 s->qscale -= s->dquant;
281
282 132831 put_bits(&s->pb,
283 132831 ff_h261_mtype_bits[com->mtype],
284 132831 ff_h261_mtype_code[com->mtype]);
285
286 132831 com->mtype = ff_h261_mtype_map[com->mtype];
287
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132831 times.
132831 if (IS_QUANT(com->mtype)) {
289 ff_set_qscale(s, s->qscale + s->dquant);
290 put_bits(&s->pb, 5, s->qscale);
291 }
292
293
2/2
✓ Branch 0 taken 102798 times.
✓ Branch 1 taken 30033 times.
132831 if (IS_16X16(com->mtype)) {
294 102798 mv_diff_x = (motion_x >> 1) - s->last_mv[0][0][0];
295 102798 mv_diff_y = (motion_y >> 1) - s->last_mv[0][0][1];
296 102798 s->last_mv[0][0][0] = (motion_x >> 1);
297 102798 s->last_mv[0][0][1] = (motion_y >> 1);
298 102798 h261_encode_motion(&s->pb, mv_diff_x);
299 102798 h261_encode_motion(&s->pb, mv_diff_y);
300 }
301
302
2/2
✓ Branch 0 taken 85004 times.
✓ Branch 1 taken 47827 times.
132831 if (HAS_CBP(com->mtype)) {
303 av_assert1(cbp > 0);
304 85004 put_bits(&s->pb,
305 85004 ff_h261_cbp_tab[cbp - 1][1],
306 85004 ff_h261_cbp_tab[cbp - 1][0]);
307 }
308
2/2
✓ Branch 0 taken 796986 times.
✓ Branch 1 taken 132831 times.
929817 for (i = 0; i < 6; i++)
309 /* encode each block */
310 796986 h261_encode_block(h, block[i], i);
311
312
2/2
✓ Branch 0 taken 30033 times.
✓ Branch 1 taken 102798 times.
132831 if (!IS_16X16(com->mtype)) {
313 30033 s->last_mv[0][0][0] = 0;
314 30033 s->last_mv[0][0][1] = 0;
315 }
316 }
317
318 6 static av_cold void h261_encode_init_static(void)
319 {
320 6 uint8_t (*const mv_codes)[2] = h261_mv_codes + MV_TAB_OFFSET;
321 6 memset(uni_h261_rl_len, H261_ESC_LEN, sizeof(uni_h261_rl_len));
322 6 memset(uni_h261_rl_len_last, H261_ESC_LEN + 2 /* EOB */, sizeof(uni_h261_rl_len_last));
323
324 // The following loop is over the ordinary elements, not EOB or escape.
325
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 6 times.
384 for (size_t i = 1; i < FF_ARRAY_ELEMS(ff_h261_tcoeff_vlc) - 1; i++) {
326 378 unsigned run = ff_h261_tcoeff_run[i];
327 378 unsigned level = ff_h261_tcoeff_level[i];
328 378 unsigned len = ff_h261_tcoeff_vlc[i][1] + 1 /* sign */;
329 378 unsigned code = ff_h261_tcoeff_vlc[i][0];
330
331 378 vlc_lut[run][H261_MAX_LEVEL + level] = (struct VLCLUT){ len, code << 1 };
332 378 vlc_lut[run][H261_MAX_LEVEL - level] = (struct VLCLUT){ len, (code << 1) | 1 };
333
334 378 uni_h261_rl_len [UNI_AC_ENC_INDEX(run, 64 + level)] = len;
335 378 uni_h261_rl_len [UNI_AC_ENC_INDEX(run, 64 - level)] = len;
336 378 uni_h261_rl_len_last[UNI_AC_ENC_INDEX(run, 64 + level)] = len + 2;
337 378 uni_h261_rl_len_last[UNI_AC_ENC_INDEX(run, 64 - level)] = len + 2;
338 }
339
340 6 for (size_t i = 1;; i++) {
341 // sign-one MV codes; diff -16..-1, 16..31
342 96 mv_codes[32 - i][0] = mv_codes[-i][0] = (ff_h261_mv_tab[i][0] << 1) | 1 /* sign */;
343 96 mv_codes[32 - i][1] = mv_codes[-i][1] = ff_h261_mv_tab[i][1] + 1;
344
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 90 times.
96 if (i == 16)
345 6 break;
346 // sign-zero MV codes: diff -31..-17, 1..15
347 90 mv_codes[i][0] = mv_codes[i - 32][0] = ff_h261_mv_tab[i][0] << 1;
348 90 mv_codes[i][1] = mv_codes[i - 32][1] = ff_h261_mv_tab[i][1] + 1;
349 }
350 // MV code for difference zero; has no sign
351 6 mv_codes[0][0] = 1;
352 6 mv_codes[0][1] = 1;
353 6 }
354
355 6 av_cold int ff_h261_encode_init(MpegEncContext *s)
356 {
357 6 H261EncContext *const h = (H261EncContext*)s;
358 static AVOnce init_static_once = AV_ONCE_INIT;
359
360
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 if (s->width == 176 && s->height == 144) {
361 h->format = H261_QCIF;
362
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 } else if (s->width == 352 && s->height == 288) {
363 6 h->format = H261_CIF;
364 } else {
365 av_log(s->avctx, AV_LOG_ERROR,
366 "The specified picture size of %dx%d is not valid for the "
367 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
368 s->width, s->height);
369 return AVERROR(EINVAL);
370 }
371 6 s->private_ctx = &h->common;
372
373 6 s->min_qcoeff = -127;
374 6 s->max_qcoeff = 127;
375 6 s->ac_esc_length = H261_ESC_LEN;
376
377 6 s->me.mv_penalty = mv_penalty;
378
379 6 s->intra_ac_vlc_length = s->inter_ac_vlc_length = uni_h261_rl_len;
380 6 s->intra_ac_vlc_last_length = s->inter_ac_vlc_last_length = uni_h261_rl_len_last;
381 6 ff_thread_once(&init_static_once, h261_encode_init_static);
382
383 6 return 0;
384 }
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 = ff_mpv_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 .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
399 AV_PIX_FMT_NONE },
400 .color_ranges = AVCOL_RANGE_MPEG,
401 };
402