FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h261enc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 177 189 93.7%
Functions: 9 9 100.0%
Branches: 71 86 82.6%

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
43 static struct VLCLUT {
44 uint8_t len;
45 uint16_t code;
46 } vlc_lut[H261_MAX_RUN + 1][32 /* 0..2 * H261_MAX_LEN are used */];
47
48 static uint8_t uni_h261_rl_len [64 * 128];
49 static uint8_t uni_h261_rl_len_last[64 * 128];
50
51 typedef struct H261EncContext {
52 MpegEncContext s;
53
54 H261Context common;
55
56 int gob_number;
57 enum {
58 H261_QCIF = 0,
59 H261_CIF = 1,
60 } format;
61 } H261EncContext;
62
63 300 void ff_h261_encode_picture_header(MpegEncContext *s)
64 {
65 300 H261EncContext *const h = (H261EncContext *)s;
66 int temp_ref;
67
68 300 align_put_bits(&s->pb);
69
70 /* Update the pointer to last GOB */
71 300 s->ptr_lastgob = put_bits_ptr(&s->pb);
72
73 300 put_bits(&s->pb, 20, 0x10); /* PSC */
74
75 300 temp_ref = s->picture_number * 30000LL * s->avctx->time_base.num /
76 300 (1001LL * s->avctx->time_base.den); // FIXME maybe this should use a timestamp
77 300 put_sbits(&s->pb, 5, temp_ref); /* TemporalReference */
78
79 300 put_bits(&s->pb, 1, 0); /* split screen off */
80 300 put_bits(&s->pb, 1, 0); /* camera off */
81 300 put_bits(&s->pb, 1, s->pict_type == AV_PICTURE_TYPE_I); /* freeze picture release on/off */
82
83 300 put_bits(&s->pb, 1, h->format); /* 0 == QCIF, 1 == CIF */
84
85 300 put_bits(&s->pb, 1, 1); /* still image mode */
86 300 put_bits(&s->pb, 1, 1); /* reserved */
87
88 300 put_bits(&s->pb, 1, 0); /* no PEI */
89 300 h->gob_number = h->format - 1;
90 300 s->mb_skip_run = 0;
91 300 }
92
93 /**
94 * Encode a group of blocks header.
95 */
96 3600 static void h261_encode_gob_header(MpegEncContext *s, int mb_line)
97 {
98 3600 H261EncContext *const h = (H261EncContext *)s;
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3600 times.
3600 if (h->format == H261_QCIF) {
100 h->gob_number += 2; // QCIF
101 } else {
102 3600 h->gob_number++; // CIF
103 }
104 3600 put_bits(&s->pb, 16, 1); /* GBSC */
105 3600 put_bits(&s->pb, 4, h->gob_number); /* GN */
106 3600 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
107 3600 put_bits(&s->pb, 1, 0); /* no GEI */
108 3600 s->mb_skip_run = 0;
109 3600 s->last_mv[0][0][0] = 0;
110 3600 s->last_mv[0][0][1] = 0;
111 3600 }
112
113 118800 void ff_h261_reorder_mb_index(MpegEncContext *s)
114 {
115 118800 const H261EncContext *const h = (H261EncContext*)s;
116 118800 int index = s->mb_x + s->mb_y * s->mb_width;
117
118
2/2
✓ Branch 0 taken 10800 times.
✓ Branch 1 taken 108000 times.
118800 if (index % 11 == 0) {
119
2/2
✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 7200 times.
10800 if (index % 33 == 0)
120 3600 h261_encode_gob_header(s, 0);
121 10800 s->last_mv[0][0][0] = 0;
122 10800 s->last_mv[0][0][1] = 0;
123 }
124
125 /* for CIF the GOB's are fragmented in the middle of a scanline
126 * that's why we need to adjust the x and y index of the macroblocks */
127
1/2
✓ Branch 0 taken 118800 times.
✗ Branch 1 not taken.
118800 if (h->format == H261_CIF) {
128 118800 s->mb_x = index % 11;
129 118800 index /= 11;
130 118800 s->mb_y = index % 3;
131 118800 index /= 3;
132 118800 s->mb_x += 11 * (index % 2);
133 118800 index /= 2;
134 118800 s->mb_y += 3 * index;
135
136 118800 ff_init_block_index(s);
137 118800 ff_update_block_index(s, 8, 0, 1);
138 }
139 118800 }
140
141 201154 static void h261_encode_motion(PutBitContext *pb, int val)
142 {
143 int sign, code;
144
2/2
✓ Branch 0 taken 113773 times.
✓ Branch 1 taken 87381 times.
201154 if (val == 0) {
145 // Corresponds to ff_h261_mv_tab[0]
146 113773 put_bits(pb, 1, 1);
147 } else {
148
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 87341 times.
87381 if (val > 15)
149 40 val -= 32;
150
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 87342 times.
87381 if (val < -16)
151 39 val += 32;
152 87381 sign = val < 0;
153
2/2
✓ Branch 0 taken 47676 times.
✓ Branch 1 taken 39705 times.
87381 code = sign ? -val : val;
154 87381 put_bits(pb, ff_h261_mv_tab[code][1], ff_h261_mv_tab[code][0]);
155 87381 put_bits(pb, 1, sign);
156 }
157 201154 }
158
159 104879 static inline int get_cbp(MpegEncContext *s, int16_t block[6][64])
160 {
161 int i, cbp;
162 104879 cbp = 0;
163
2/2
✓ Branch 0 taken 629274 times.
✓ Branch 1 taken 104879 times.
734153 for (i = 0; i < 6; i++)
164
2/2
✓ Branch 0 taken 252185 times.
✓ Branch 1 taken 377089 times.
629274 if (s->block_last_index[i] >= 0)
165 252185 cbp |= 1 << (5 - i);
166 104879 return cbp;
167 }
168
169 /**
170 * Encode an 8x8 block.
171 * @param block the 8x8 block
172 * @param n block index (0-3 are luma, 4-5 are chroma)
173 */
174 796620 static void h261_encode_block(H261EncContext *h, int16_t *block, int n)
175 {
176 796620 MpegEncContext *const s = &h->s;
177 int level, run, i, j, last_index, last_non_zero;
178
179
2/2
✓ Branch 0 taken 172866 times.
✓ Branch 1 taken 623754 times.
796620 if (s->mb_intra) {
180 /* DC coef */
181 172866 level = block[0];
182 /* 255 cannot be represented, so we clamp */
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172866 times.
172866 if (level > 254) {
184 level = 254;
185 block[0] = 254;
186 }
187 /* 0 cannot be represented also */
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 172866 times.
172866 else if (level < 1) {
189 level = 1;
190 block[0] = 1;
191 }
192
2/2
✓ Branch 0 taken 2094 times.
✓ Branch 1 taken 170772 times.
172866 if (level == 128)
193 2094 put_bits(&s->pb, 8, 0xff);
194 else
195 170772 put_bits(&s->pb, 8, level);
196 172866 i = 1;
197
4/4
✓ Branch 0 taken 562202 times.
✓ Branch 1 taken 61552 times.
✓ Branch 2 taken 62076 times.
✓ Branch 3 taken 500126 times.
623754 } else if ((block[0] == 1 || block[0] == -1) &&
198
2/2
✓ Branch 0 taken 88354 times.
✓ Branch 1 taken 35274 times.
123628 (s->block_last_index[n] > -1)) {
199 // special case
200
2/2
✓ Branch 0 taken 43927 times.
✓ Branch 1 taken 44427 times.
88354 put_bits(&s->pb, 2, block[0] > 0 ? 2 : 3);
201 88354 i = 1;
202 } else {
203 535400 i = 0;
204 }
205
206 /* AC coefs */
207 796620 last_index = s->block_last_index[n];
208 796620 last_non_zero = i - 1;
209
2/2
✓ Branch 0 taken 8314036 times.
✓ Branch 1 taken 796620 times.
9110656 for (; i <= last_index; i++) {
210 8314036 j = s->intra_scantable.permutated[i];
211 8314036 level = block[j];
212
2/2
✓ Branch 0 taken 2757391 times.
✓ Branch 1 taken 5556645 times.
8314036 if (level) {
213 2757391 run = i - last_non_zero - 1;
214
215
2/2
✓ Branch 0 taken 2741254 times.
✓ Branch 1 taken 16137 times.
2757391 if (run <= H261_MAX_RUN &&
216
2/2
✓ Branch 0 taken 2737780 times.
✓ Branch 1 taken 3474 times.
2741254 (unsigned)(level + H261_MAX_LEVEL) <= 2 * H261_MAX_LEVEL &&
217
2/2
✓ Branch 0 taken 2714636 times.
✓ Branch 1 taken 23144 times.
2737780 vlc_lut[run][level + H261_MAX_LEVEL].len) {
218 2714636 put_bits(&s->pb, vlc_lut[run][level + H261_MAX_LEVEL].len,
219 2714636 vlc_lut[run][level + H261_MAX_LEVEL].code);
220 } else {
221 /* Escape */
222 42755 put_bits(&s->pb, 6 + 6, (1 << 6) | run);
223 av_assert1(level != 0);
224 av_assert1(FFABS(level) <= 127);
225 42755 put_sbits(&s->pb, 8, level);
226 }
227 2757391 last_non_zero = i;
228 }
229 }
230
2/2
✓ Branch 0 taken 425051 times.
✓ Branch 1 taken 371569 times.
796620 if (last_index > -1)
231 425051 put_bits(&s->pb, 2, 0x2); // EOB
232 796620 }
233
234 133690 void ff_h261_encode_mb(MpegEncContext *s, int16_t block[6][64],
235 int motion_x, int motion_y)
236 {
237 /* The following is only allowed because this encoder
238 * does not use slice threading. */
239 133690 H261EncContext *const h = (H261EncContext *)s;
240 133690 H261Context *const com = &h->common;
241 int mvd, mv_diff_x, mv_diff_y, i, cbp;
242 133690 cbp = 63; // avoid warning
243 133690 mvd = 0;
244
245 133690 com->mtype = 0;
246
247
2/2
✓ Branch 0 taken 104879 times.
✓ Branch 1 taken 28811 times.
133690 if (!s->mb_intra) {
248 /* compute cbp */
249 104879 cbp = get_cbp(s, block);
250
251 /* mvd indicates if this block is motion compensated */
252 104879 mvd = motion_x | motion_y;
253
254
2/2
✓ Branch 0 taken 920 times.
✓ Branch 1 taken 103959 times.
104879 if ((cbp | mvd) == 0) {
255 /* skip macroblock */
256 920 s->mb_skip_run++;
257 920 s->last_mv[0][0][0] = 0;
258 920 s->last_mv[0][0][1] = 0;
259 920 s->qscale -= s->dquant;
260 920 return;
261 }
262 }
263
264 /* MB is not skipped, encode MBA */
265 132770 put_bits(&s->pb,
266 132770 ff_h261_mba_bits[s->mb_skip_run],
267 132770 ff_h261_mba_code[s->mb_skip_run]);
268 132770 s->mb_skip_run = 0;
269
270 /* calculate MTYPE */
271
2/2
✓ Branch 0 taken 103959 times.
✓ Branch 1 taken 28811 times.
132770 if (!s->mb_intra) {
272 103959 com->mtype++;
273
274
3/4
✓ Branch 0 taken 3382 times.
✓ Branch 1 taken 100577 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3382 times.
103959 if (mvd || s->loop_filter)
275 100577 com->mtype += 3;
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 103959 times.
103959 if (s->loop_filter)
277 com->mtype += 3;
278
2/2
✓ Branch 0 taken 82706 times.
✓ Branch 1 taken 21253 times.
103959 if (cbp)
279 82706 com->mtype++;
280 av_assert1(com->mtype > 1);
281 }
282
283
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 132770 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
132770 if (s->dquant && cbp) {
284 com->mtype++;
285 } else
286 132770 s->qscale -= s->dquant;
287
288 132770 put_bits(&s->pb,
289 132770 ff_h261_mtype_bits[com->mtype],
290 132770 ff_h261_mtype_code[com->mtype]);
291
292 132770 com->mtype = ff_h261_mtype_map[com->mtype];
293
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132770 times.
132770 if (IS_QUANT(com->mtype)) {
295 ff_set_qscale(s, s->qscale + s->dquant);
296 put_bits(&s->pb, 5, s->qscale);
297 }
298
299
2/2
✓ Branch 0 taken 100577 times.
✓ Branch 1 taken 32193 times.
132770 if (IS_16X16(com->mtype)) {
300 100577 mv_diff_x = (motion_x >> 1) - s->last_mv[0][0][0];
301 100577 mv_diff_y = (motion_y >> 1) - s->last_mv[0][0][1];
302 100577 s->last_mv[0][0][0] = (motion_x >> 1);
303 100577 s->last_mv[0][0][1] = (motion_y >> 1);
304 100577 h261_encode_motion(&s->pb, mv_diff_x);
305 100577 h261_encode_motion(&s->pb, mv_diff_y);
306 }
307
308
2/2
✓ Branch 0 taken 82706 times.
✓ Branch 1 taken 50064 times.
132770 if (HAS_CBP(com->mtype)) {
309 av_assert1(cbp > 0);
310 82706 put_bits(&s->pb,
311 82706 ff_h261_cbp_tab[cbp - 1][1],
312 82706 ff_h261_cbp_tab[cbp - 1][0]);
313 }
314
2/2
✓ Branch 0 taken 796620 times.
✓ Branch 1 taken 132770 times.
929390 for (i = 0; i < 6; i++)
315 /* encode each block */
316 796620 h261_encode_block(h, block[i], i);
317
318
2/2
✓ Branch 0 taken 32193 times.
✓ Branch 1 taken 100577 times.
132770 if (!IS_16X16(com->mtype)) {
319 32193 s->last_mv[0][0][0] = 0;
320 32193 s->last_mv[0][0][1] = 0;
321 }
322 }
323
324 6 static av_cold void h261_encode_init_static(void)
325 {
326 6 memset(uni_h261_rl_len, H261_ESC_LEN, sizeof(uni_h261_rl_len));
327 6 memset(uni_h261_rl_len_last, H261_ESC_LEN + 2 /* EOB */, sizeof(uni_h261_rl_len_last));
328
329 // The following loop is over the ordinary elements, not EOB or escape.
330
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++) {
331 378 unsigned run = ff_h261_tcoeff_run[i];
332 378 unsigned level = ff_h261_tcoeff_level[i];
333 378 unsigned len = ff_h261_tcoeff_vlc[i][1] + 1 /* sign */;
334 378 unsigned code = ff_h261_tcoeff_vlc[i][0];
335
336 378 vlc_lut[run][H261_MAX_LEVEL + level] = (struct VLCLUT){ len, code << 1 };
337 378 vlc_lut[run][H261_MAX_LEVEL - level] = (struct VLCLUT){ len, (code << 1) | 1 };
338
339 378 uni_h261_rl_len [UNI_AC_ENC_INDEX(run, 64 + level)] = len;
340 378 uni_h261_rl_len [UNI_AC_ENC_INDEX(run, 64 - level)] = len;
341 378 uni_h261_rl_len_last[UNI_AC_ENC_INDEX(run, 64 + level)] = len + 2;
342 378 uni_h261_rl_len_last[UNI_AC_ENC_INDEX(run, 64 - level)] = len + 2;
343 }
344 6 }
345
346 6 av_cold int ff_h261_encode_init(MpegEncContext *s)
347 {
348 6 H261EncContext *const h = (H261EncContext*)s;
349 static AVOnce init_static_once = AV_ONCE_INIT;
350
351
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) {
352 h->format = H261_QCIF;
353
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) {
354 6 h->format = H261_CIF;
355 } else {
356 av_log(s->avctx, AV_LOG_ERROR,
357 "The specified picture size of %dx%d is not valid for the "
358 "H.261 codec.\nValid sizes are 176x144, 352x288\n",
359 s->width, s->height);
360 return AVERROR(EINVAL);
361 }
362 6 s->private_ctx = &h->common;
363
364 6 s->min_qcoeff = -127;
365 6 s->max_qcoeff = 127;
366 6 s->ac_esc_length = H261_ESC_LEN;
367
368 6 s->intra_ac_vlc_length = s->inter_ac_vlc_length = uni_h261_rl_len;
369 6 s->intra_ac_vlc_last_length = s->inter_ac_vlc_last_length = uni_h261_rl_len_last;
370 6 ff_thread_once(&init_static_once, h261_encode_init_static);
371
372 6 return 0;
373 }
374
375 const FFCodec ff_h261_encoder = {
376 .p.name = "h261",
377 CODEC_LONG_NAME("H.261"),
378 .p.type = AVMEDIA_TYPE_VIDEO,
379 .p.id = AV_CODEC_ID_H261,
380 .p.priv_class = &ff_mpv_enc_class,
381 .priv_data_size = sizeof(H261EncContext),
382 .init = ff_mpv_encode_init,
383 FF_CODEC_ENCODE_CB(ff_mpv_encode_picture),
384 .close = ff_mpv_encode_end,
385 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
386 .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
387 AV_PIX_FMT_NONE },
388 .color_ranges = AVCOL_RANGE_MPEG,
389 .p.capabilities = AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
390 };
391