FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/huffyuvenc.c
Date: 2026-04-24 10:13:59
Exec Total Coverage
Lines: 350 477 73.4%
Functions: 14 14 100.0%
Branches: 186 365 51.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2002-2014 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * see https://multimedia.cx/huffyuv.txt for a description of
5 * the algorithm used
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 * yuva, gray, 4:4:4, 4:1:1, 4:1:0 and >8 bit per sample support sponsored by NOA
24 */
25
26 /**
27 * @file
28 * huffyuv encoder
29 */
30
31 #include "config_components.h"
32
33 #include "avcodec.h"
34 #include "bswapdsp.h"
35 #include "codec_internal.h"
36 #include "encode.h"
37 #include "huffyuv.h"
38 #include "huffman.h"
39 #include "huffyuvencdsp.h"
40 #include "lossless_videoencdsp.h"
41 #include "put_bits.h"
42 #include "libavutil/mem.h"
43 #include "libavutil/opt.h"
44 #include "libavutil/pixdesc.h"
45
46 typedef struct HYuvEncContext {
47 AVClass *class;
48 AVCodecContext *avctx;
49 PutBitContext pb;
50 /* Predictor, use int for AVOption */
51 int predictor;
52 int interlaced;
53 int decorrelate;
54 int bitstream_bpp;
55 int version;
56 int bps;
57 unsigned mask; // (1<<bps)-1
58 int vlc_n; // number of vlc codes (FFMIN(1<<bps, MAX_VLC_N))
59 int alpha;
60 int chroma;
61 int yuv;
62 int chroma_h_shift;
63 int chroma_v_shift;
64 int flags;
65 int context;
66 int picture_number;
67
68 union {
69 uint8_t *temp[3];
70 uint16_t *temp16[3];
71 };
72 uint64_t stats[4][MAX_VLC_N];
73 uint8_t len[4][MAX_VLC_N];
74 uint32_t bits[4][MAX_VLC_N];
75 BswapDSPContext bdsp;
76 HuffYUVEncDSPContext hencdsp;
77 LLVidEncDSPContext llvidencdsp;
78 int non_determ; // non-deterministic, multi-threaded encoder allowed
79 } HYuvEncContext;
80
81 134100 static inline void diff_bytes(HYuvEncContext *s, uint8_t *dst,
82 const uint8_t *src0, const uint8_t *src1, int w)
83 {
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 134100 times.
134100 if (s->bps <= 8) {
85 s->llvidencdsp.diff_bytes(dst, src0, src1, w);
86 } else {
87 134100 s->hencdsp.diff_int16((uint16_t *)dst, (const uint16_t *)src0, (const uint16_t *)src1, s->mask, w);
88 }
89 134100 }
90
91 586300 static inline int sub_left_prediction(HYuvEncContext *s, uint8_t *dst,
92 const uint8_t *src, int w, int left)
93 {
94 int i;
95 586300 int min_width = FFMIN(w, 32);
96
97
2/2
✓ Branch 0 taken 316300 times.
✓ Branch 1 taken 270000 times.
586300 if (s->bps <= 8) {
98
2/2
✓ Branch 0 taken 10040700 times.
✓ Branch 1 taken 316300 times.
10357000 for (i = 0; i < min_width; i++) { /* scalar loop before dsp call */
99 10040700 const int temp = src[i];
100 10040700 dst[i] = temp - left;
101 10040700 left = temp;
102 }
103
2/2
✓ Branch 0 taken 5250 times.
✓ Branch 1 taken 311050 times.
316300 if (w < 32)
104 5250 return left;
105 311050 s->llvidencdsp.diff_bytes(dst + 32, src + 32, src + 31, w - 32);
106 311050 return src[w-1];
107 } else {
108 270000 const uint16_t *src16 = (const uint16_t *)src;
109 270000 uint16_t *dst16 = ( uint16_t *)dst;
110
2/2
✓ Branch 0 taken 8587500 times.
✓ Branch 1 taken 270000 times.
8857500 for (i = 0; i < min_width; i++) { /* scalar loop before dsp call */
111 8587500 const int temp = src16[i];
112 8587500 dst16[i] = temp - left;
113 8587500 left = temp;
114 }
115
2/2
✓ Branch 0 taken 3500 times.
✓ Branch 1 taken 266500 times.
270000 if (w < 32)
116 3500 return left;
117 266500 s->hencdsp.diff_int16(dst16 + 32, src16 + 32, src16 + 31, s->mask, w - 32);
118 266500 return src16[w-1];
119 }
120 }
121
122 44900 static inline void sub_left_prediction_bgr32(HYuvEncContext *s, uint8_t *dst,
123 const uint8_t *src, int w,
124 int *red, int *green, int *blue,
125 int *alpha)
126 {
127 int i;
128 int r, g, b, a;
129 44900 int min_width = FFMIN(w, 8);
130 44900 r = *red;
131 44900 g = *green;
132 44900 b = *blue;
133 44900 a = *alpha;
134
135
2/2
✓ Branch 0 taken 359200 times.
✓ Branch 1 taken 44900 times.
404100 for (i = 0; i < min_width; i++) {
136 359200 const int rt = src[i * 4 + R];
137 359200 const int gt = src[i * 4 + G];
138 359200 const int bt = src[i * 4 + B];
139 359200 const int at = src[i * 4 + A];
140 359200 dst[i * 4 + R] = rt - r;
141 359200 dst[i * 4 + G] = gt - g;
142 359200 dst[i * 4 + B] = bt - b;
143 359200 dst[i * 4 + A] = at - a;
144 359200 r = rt;
145 359200 g = gt;
146 359200 b = bt;
147 359200 a = at;
148 }
149
150 44900 s->llvidencdsp.diff_bytes(dst + 32, src + 32, src + 32 - 4, w * 4 - 32);
151
152 44900 *red = src[(w - 1) * 4 + R];
153 44900 *green = src[(w - 1) * 4 + G];
154 44900 *blue = src[(w - 1) * 4 + B];
155 44900 *alpha = src[(w - 1) * 4 + A];
156 44900 }
157
158 44900 static inline void sub_left_prediction_rgb24(HYuvEncContext *s, uint8_t *dst,
159 const uint8_t *src, int w,
160 int *red, int *green, int *blue)
161 {
162 int i;
163 int r, g, b;
164 44900 r = *red;
165 44900 g = *green;
166 44900 b = *blue;
167
2/2
✓ Branch 0 taken 718400 times.
✓ Branch 1 taken 44900 times.
763300 for (i = 0; i < FFMIN(w, 16); i++) {
168 718400 const int rt = src[i * 3 + 0];
169 718400 const int gt = src[i * 3 + 1];
170 718400 const int bt = src[i * 3 + 2];
171 718400 dst[i * 3 + 0] = rt - r;
172 718400 dst[i * 3 + 1] = gt - g;
173 718400 dst[i * 3 + 2] = bt - b;
174 718400 r = rt;
175 718400 g = gt;
176 718400 b = bt;
177 }
178
179 44900 s->llvidencdsp.diff_bytes(dst + 48, src + 48, src + 48 - 3, w * 3 - 48);
180
181 44900 *red = src[(w - 1) * 3 + 0];
182 44900 *green = src[(w - 1) * 3 + 1];
183 44900 *blue = src[(w - 1) * 3 + 2];
184 44900 }
185
186 89200 static void sub_median_prediction(HYuvEncContext *s, uint8_t *dst,
187 const uint8_t *src1, const uint8_t *src2,
188 int w, int *left, int *left_top)
189 {
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89200 times.
89200 if (s->bps <= 8) {
191 s->llvidencdsp.sub_median_pred(dst, src1, src2, w , left, left_top);
192 } else {
193 89200 s->hencdsp.sub_hfyu_median_pred_int16((uint16_t *)dst, (const uint16_t *)src1,
194 (const uint16_t *)src2, s->mask, w, left, left_top);
195 }
196 89200 }
197
198 96 static int store_table(HYuvEncContext *s, const uint8_t *len, uint8_t *buf)
199 {
200 int i;
201 96 int index = 0;
202 96 int n = s->vlc_n;
203
204
2/2
✓ Branch 0 taken 3876 times.
✓ Branch 1 taken 96 times.
3972 for (i = 0; i < n;) {
205 3876 int val = len[i];
206 3876 int repeat = 0;
207
208
6/6
✓ Branch 0 taken 277188 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 274140 times.
✓ Branch 3 taken 3048 times.
✓ Branch 4 taken 273408 times.
✓ Branch 5 taken 732 times.
277284 for (; i < n && len[i] == val && repeat < 255; i++)
209 273408 repeat++;
210
211
4/8
✓ Branch 0 taken 3876 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3876 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3876 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3876 times.
3876 av_assert0(val < 32 && val >0 && repeat < 256 && repeat>0);
212
2/2
✓ Branch 0 taken 2244 times.
✓ Branch 1 taken 1632 times.
3876 if (repeat > 7) {
213 2244 buf[index++] = val;
214 2244 buf[index++] = repeat;
215 } else {
216 1632 buf[index++] = val | (repeat << 5);
217 }
218 }
219
220 96 return index;
221 }
222
223 32 static int store_huffman_tables(HYuvEncContext *s, uint8_t *buf)
224 {
225 int i, ret;
226 32 int size = 0;
227 32 int count = 3;
228
229
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 if (s->version > 2)
230 16 count = 1 + s->alpha + 2*s->chroma;
231
232
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 32 times.
128 for (i = 0; i < count; i++) {
233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 96 times.
96 if ((ret = ff_huff_gen_len_table(s->len[i], s->stats[i], s->vlc_n, 0)) < 0)
234 return ret;
235
236 96 ret = ff_huffyuv_generate_bits_table(s->bits[i], s->len[i], s->vlc_n);
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (ret < 0)
238 return ret;
239
240 96 size += store_table(s, s->len[i], buf + size);
241 }
242 32 return size;
243 }
244
245 32 static av_cold int encode_init(AVCodecContext *avctx)
246 {
247 32 HYuvEncContext *s = avctx->priv_data;
248 int i, j;
249 int ret;
250 const AVPixFmtDescriptor *desc;
251
252 32 s->avctx = avctx;
253 32 s->flags = avctx->flags;
254
255 32 ff_bswapdsp_init(&s->bdsp);
256 32 ff_llvidencdsp_init(&s->llvidencdsp);
257
258 32 avctx->extradata = av_mallocz(3*MAX_N + 4);
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!avctx->extradata)
260 return AVERROR(ENOMEM);
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->flags&AV_CODEC_FLAG_PASS1) {
262 #define STATS_OUT_SIZE 21*MAX_N*3 + 4
263 avctx->stats_out = av_mallocz(STATS_OUT_SIZE); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132
264 if (!avctx->stats_out)
265 return AVERROR(ENOMEM);
266 }
267 32 s->version = 2;
268
269 32 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
270 32 s->bps = desc->comp[0].depth;
271
3/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
32 s->yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
272 32 s->chroma = desc->nb_components > 2;
273 32 s->alpha = !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA);
274 32 s->chroma_h_shift = desc->log2_chroma_w;
275 32 s->chroma_v_shift = desc->log2_chroma_h;
276
277 32 s->mask = (1 << s->bps) - 1;
278 32 s->vlc_n = FFMIN(1 << s->bps, MAX_VLC_N);
279
280 32 ff_huffyuvencdsp_init(&s->hencdsp, s->bps, avctx->width >> s->chroma_h_shift);
281
282
4/5
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
32 switch (avctx->pix_fmt) {
283 8 case AV_PIX_FMT_YUV420P:
284 case AV_PIX_FMT_YUV422P:
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (avctx->width & 1) {
286 av_log(avctx, AV_LOG_ERROR, "Width must be even for this colorspace.\n");
287 return AVERROR(EINVAL);
288 }
289
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 s->bitstream_bpp = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 12 : 16;
290 8 break;
291 16 case AV_PIX_FMT_YUV444P:
292 case AV_PIX_FMT_YUV410P:
293 case AV_PIX_FMT_YUV411P:
294 case AV_PIX_FMT_YUV440P:
295 case AV_PIX_FMT_GBRP:
296 case AV_PIX_FMT_GBRP9:
297 case AV_PIX_FMT_GBRP10:
298 case AV_PIX_FMT_GBRP12:
299 case AV_PIX_FMT_GBRP14:
300 case AV_PIX_FMT_GBRP16:
301 case AV_PIX_FMT_GRAY8:
302 case AV_PIX_FMT_GRAY16:
303 case AV_PIX_FMT_YUVA444P:
304 case AV_PIX_FMT_YUVA420P:
305 case AV_PIX_FMT_YUVA422P:
306 case AV_PIX_FMT_GBRAP:
307 case AV_PIX_FMT_YUV420P9:
308 case AV_PIX_FMT_YUV420P10:
309 case AV_PIX_FMT_YUV420P12:
310 case AV_PIX_FMT_YUV420P14:
311 case AV_PIX_FMT_YUV420P16:
312 case AV_PIX_FMT_YUV422P9:
313 case AV_PIX_FMT_YUV422P10:
314 case AV_PIX_FMT_YUV422P12:
315 case AV_PIX_FMT_YUV422P14:
316 case AV_PIX_FMT_YUV422P16:
317 case AV_PIX_FMT_YUV444P9:
318 case AV_PIX_FMT_YUV444P10:
319 case AV_PIX_FMT_YUV444P12:
320 case AV_PIX_FMT_YUV444P14:
321 case AV_PIX_FMT_YUV444P16:
322 case AV_PIX_FMT_YUVA420P9:
323 case AV_PIX_FMT_YUVA420P10:
324 case AV_PIX_FMT_YUVA420P16:
325 case AV_PIX_FMT_YUVA422P9:
326 case AV_PIX_FMT_YUVA422P10:
327 case AV_PIX_FMT_YUVA422P16:
328 case AV_PIX_FMT_YUVA444P9:
329 case AV_PIX_FMT_YUVA444P10:
330 case AV_PIX_FMT_YUVA444P16:
331 16 s->version = 3;
332 16 break;
333 4 case AV_PIX_FMT_RGB32:
334 4 s->bitstream_bpp = 32;
335 4 break;
336 4 case AV_PIX_FMT_RGB24:
337 4 s->bitstream_bpp = 24;
338 4 break;
339 default:
340 av_unreachable("Already checked via CODEC_PIXFMTS");
341 }
342
343 32 avctx->bits_per_coded_sample = s->bitstream_bpp;
344
4/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
32 s->decorrelate = s->bitstream_bpp >= 24 && !s->yuv && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR);
345 32 s->interlaced = avctx->flags & AV_CODEC_FLAG_INTERLACED_ME ? 1 : 0;
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->context) {
347 if (s->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
348 av_log(avctx, AV_LOG_ERROR,
349 "context=1 is not compatible with "
350 "2 pass huffyuv encoding\n");
351 return AVERROR(EINVAL);
352 }
353 }
354
355
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 20 times.
32 if (avctx->codec->id == AV_CODEC_ID_HUFFYUV) {
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (s->interlaced != ( avctx->height > 288 ))
357 av_log(avctx, AV_LOG_INFO,
358 "using huffyuv 2.2.0 or newer interlacing flag\n");
359 }
360
361
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
32 if (s->version > 3 && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
362 av_log(avctx, AV_LOG_ERROR, "Ver > 3 is under development, files encoded with it may not be decodable with future versions!!!\n"
363 "Use vstrict=-2 / -strict -2 to use it anyway.\n");
364 return AVERROR(EINVAL);
365 }
366
367
3/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
32 if (s->bitstream_bpp >= 24 && s->predictor == MEDIAN && s->version <= 2) {
368 av_log(avctx, AV_LOG_ERROR,
369 "Error: RGB is incompatible with median predictor\n");
370 return AVERROR(EINVAL);
371 }
372
373 32 avctx->extradata[0] = s->predictor | (s->decorrelate << 6);
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 avctx->extradata[2] = s->interlaced ? 0x10 : 0x20;
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->context)
376 avctx->extradata[2] |= 0x40;
377
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 if (s->version < 3) {
378 16 avctx->extradata[1] = s->bitstream_bpp;
379 16 avctx->extradata[3] = 0;
380 } else {
381 16 avctx->extradata[1] = ((s->bps-1)<<4) | s->chroma_h_shift | (s->chroma_v_shift<<2);
382
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (s->chroma)
383
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 avctx->extradata[2] |= s->yuv ? 1 : 2;
384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (s->alpha)
385 avctx->extradata[2] |= 4;
386 16 avctx->extradata[3] = 1;
387 }
388 32 avctx->extradata_size = 4;
389
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (avctx->stats_in) {
391 char *p = avctx->stats_in;
392
393 for (i = 0; i < 4; i++)
394 for (j = 0; j < s->vlc_n; j++)
395 s->stats[i][j] = 1;
396
397 for (;;) {
398 for (i = 0; i < 4; i++) {
399 char *next;
400
401 for (j = 0; j < s->vlc_n; j++) {
402 s->stats[i][j] += strtol(p, &next, 0);
403 if (next == p) return -1;
404 p = next;
405 }
406 }
407 if (p[0] == 0 || p[1] == 0 || p[2] == 0) break;
408 }
409 } else {
410
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 32 times.
160 for (i = 0; i < 4; i++)
411
2/2
✓ Branch 0 taken 364544 times.
✓ Branch 1 taken 128 times.
364672 for (j = 0; j < s->vlc_n; j++) {
412 364544 int d = FFMIN(j, s->vlc_n - j);
413
414 364544 s->stats[i][j] = 100000000 / (d*d + 1);
415 }
416 }
417
418 32 ret = store_huffman_tables(s, avctx->extradata + avctx->extradata_size);
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
420 return ret;
421 32 avctx->extradata_size += ret;
422
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->context) {
424 for (i = 0; i < 4; i++) {
425 int pels = avctx->width * avctx->height / (i ? 40 : 10);
426 for (j = 0; j < s->vlc_n; j++) {
427 int d = FFMIN(j, s->vlc_n - j);
428 s->stats[i][j] = pels/(d*d + 1);
429 }
430 }
431 } else {
432
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 32 times.
160 for (i = 0; i < 4; i++)
433
2/2
✓ Branch 0 taken 364544 times.
✓ Branch 1 taken 128 times.
364672 for (j = 0; j < s->vlc_n; j++)
434 364544 s->stats[i][j]= 0;
435 }
436
437 32 s->picture_number=0;
438
439
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 32 times.
128 for (int i = 0; i < 3; i++) {
440 96 s->temp[i] = av_malloc(4 * avctx->width + 16);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (!s->temp[i])
442 return AVERROR(ENOMEM);
443 }
444
445 32 return 0;
446 }
447 67400 static int encode_422_bitstream(HYuvEncContext *s, int offset, int count)
448 {
449 int i;
450 67400 const uint8_t *y = s->temp[0] + offset;
451 67400 const uint8_t *u = s->temp[1] + offset / 2;
452 67400 const uint8_t *v = s->temp[2] + offset / 2;
453
454
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 67400 times.
67400 if (put_bytes_left(&s->pb, 0) < 2 * 4 * count) {
455 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
456 return -1;
457 }
458
459 #define LOAD4\
460 int y0 = y[2 * i];\
461 int y1 = y[2 * i + 1];\
462 int u0 = u[i];\
463 int v0 = v[i];
464
465 67400 count /= 2;
466
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67400 times.
67400 if (s->flags & AV_CODEC_FLAG_PASS1) {
468 for(i = 0; i < count; i++) {
469 LOAD4;
470 s->stats[0][y0]++;
471 s->stats[1][u0]++;
472 s->stats[0][y1]++;
473 s->stats[2][v0]++;
474 }
475 }
476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67400 times.
67400 if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)
477 return 0;
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67400 times.
67400 if (s->context) {
479 for (i = 0; i < count; i++) {
480 LOAD4;
481 s->stats[0][y0]++;
482 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
483 s->stats[1][u0]++;
484 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
485 s->stats[0][y1]++;
486 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
487 s->stats[2][v0]++;
488 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
489 }
490 } else {
491
2/2
✓ Branch 0 taken 11447750 times.
✓ Branch 1 taken 67400 times.
11515150 for(i = 0; i < count; i++) {
492 11447750 LOAD4;
493 11447750 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
494 11447750 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
495 11447750 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
496 11447750 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
497 }
498 }
499 67400 return 0;
500 }
501
502 493900 static int encode_plane_bitstream(HYuvEncContext *s, int width, int plane)
503 {
504 493900 int count = width/2;
505
506
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 493900 times.
493900 if (put_bytes_left(&s->pb, 0) < count * s->bps / 2) {
507 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
508 return -1;
509 }
510
511 #define LOADEND\
512 int y0 = s->temp[0][width-1];
513 #define LOADEND_14\
514 int y0 = s->temp16[0][width-1] & mask;
515 #define LOADEND_16\
516 int y0 = s->temp16[0][width-1];
517 #define STATEND\
518 s->stats[plane][y0]++;
519 #define STATEND_16\
520 s->stats[plane][y0>>2]++;
521 #define WRITEEND\
522 put_bits(&s->pb, s->len[plane][y0], s->bits[plane][y0]);
523 #define WRITEEND_16\
524 put_bits(&s->pb, s->len[plane][y0>>2], s->bits[plane][y0>>2]);\
525 put_bits(&s->pb, 2, y0&3);
526
527 #define LOAD2\
528 int y0 = s->temp[0][2 * i];\
529 int y1 = s->temp[0][2 * i + 1];
530 #define LOAD2_14\
531 int y0 = s->temp16[0][2 * i] & mask;\
532 int y1 = s->temp16[0][2 * i + 1] & mask;
533 #define LOAD2_16\
534 int y0 = s->temp16[0][2 * i];\
535 int y1 = s->temp16[0][2 * i + 1];
536 #define STAT2\
537 s->stats[plane][y0]++;\
538 s->stats[plane][y1]++;
539 #define STAT2_16\
540 s->stats[plane][y0>>2]++;\
541 s->stats[plane][y1>>2]++;
542 #define WRITE2\
543 put_bits(&s->pb, s->len[plane][y0], s->bits[plane][y0]);\
544 put_bits(&s->pb, s->len[plane][y1], s->bits[plane][y1]);
545 #define WRITE2_16\
546 put_bits(&s->pb, s->len[plane][y0>>2], s->bits[plane][y0>>2]);\
547 put_bits(&s->pb, 2, y0&3);\
548 put_bits(&s->pb, s->len[plane][y1>>2], s->bits[plane][y1>>2]);\
549 put_bits(&s->pb, 2, y1&3);
550
551 #define ENCODE_PLANE(LOAD, LOADEND, WRITE, WRITEEND, STAT, STATEND) \
552 do { \
553 if (s->flags & AV_CODEC_FLAG_PASS1) { \
554 for (int i = 0; i < count; i++) { \
555 LOAD; \
556 STAT; \
557 } \
558 if (width & 1) { \
559 LOADEND; \
560 STATEND; \
561 } \
562 } \
563 if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT) \
564 return 0; \
565 \
566 if (s->context) { \
567 for (int i = 0; i < count; i++) { \
568 LOAD; \
569 STAT; \
570 WRITE; \
571 } \
572 if (width & 1) { \
573 LOADEND; \
574 STATEND; \
575 WRITEEND; \
576 } \
577 } else { \
578 for (int i = 0; i < count; i++) { \
579 LOAD; \
580 WRITE; \
581 } \
582 if (width & 1) { \
583 LOADEND; \
584 WRITEEND; \
585 } \
586 } \
587 } while (0)
588
589
2/2
✓ Branch 0 taken 134700 times.
✓ Branch 1 taken 359200 times.
493900 if (s->bps <= 8) {
590
6/18
✗ Branch 0 not taken.
✓ Branch 1 taken 134700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 134700 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 134700 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 22896300 times.
✓ Branch 20 taken 134700 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 134700 times.
23031000 ENCODE_PLANE(LOAD2, LOADEND, WRITE2, WRITEEND, STAT2, STATEND);
591
2/2
✓ Branch 0 taken 224500 times.
✓ Branch 1 taken 134700 times.
359200 } else if (s->bps <= 14) {
592 224500 unsigned mask = s->mask;
593
594
7/18
✗ Branch 0 not taken.
✓ Branch 1 taken 224500 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 224500 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 224500 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 26709800 times.
✓ Branch 20 taken 224500 times.
✓ Branch 21 taken 5100 times.
✓ Branch 22 taken 219400 times.
26934300 ENCODE_PLANE(LOAD2_14, LOADEND_14, WRITE2, WRITEEND, STAT2, STATEND);
595 } else {
596
6/18
✗ Branch 0 not taken.
✓ Branch 1 taken 134700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 134700 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 134700 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 24 taken 22896300 times.
✓ Branch 25 taken 134700 times.
✗ Branch 26 not taken.
✓ Branch 27 taken 134700 times.
23031000 ENCODE_PLANE(LOAD2_16, LOADEND_16, WRITE2_16, WRITEEND_16, STAT2_16, STATEND_16);
597 }
598 #undef LOAD2
599 #undef STAT2
600 #undef WRITE2
601 493900 return 0;
602 }
603
604 22450 static int encode_gray_bitstream(HYuvEncContext *s, int count)
605 {
606 int i;
607
608
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22450 times.
22450 if (put_bytes_left(&s->pb, 0) < 4 * count) {
609 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
610 return -1;
611 }
612
613 #define LOAD2\
614 int y0 = s->temp[0][2 * i];\
615 int y1 = s->temp[0][2 * i + 1];
616 #define STAT2\
617 s->stats[0][y0]++;\
618 s->stats[0][y1]++;
619 #define WRITE2\
620 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
621 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
622
623 22450 count /= 2;
624
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22450 times.
22450 if (s->flags & AV_CODEC_FLAG_PASS1) {
626 for (i = 0; i < count; i++) {
627 LOAD2;
628 STAT2;
629 }
630 }
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22450 times.
22450 if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)
632 return 0;
633
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22450 times.
22450 if (s->context) {
635 for (i = 0; i < count; i++) {
636 LOAD2;
637 STAT2;
638 WRITE2;
639 }
640 } else {
641
2/2
✓ Branch 0 taken 3816050 times.
✓ Branch 1 taken 22450 times.
3838500 for (i = 0; i < count; i++) {
642 3816050 LOAD2;
643 3816050 WRITE2;
644 }
645 }
646 22450 return 0;
647 }
648
649 89800 static inline int encode_bgra_bitstream(HYuvEncContext *s, int count, int planes)
650 {
651 int i;
652
653
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 89800 times.
89800 if (put_bytes_left(&s->pb, 0) < 4 * planes * count) {
654 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
655 return -1;
656 }
657
658 #define LOAD_GBRA \
659 int g = s->temp[0][planes == 3 ? 3 * i + 1 : 4 * i + G]; \
660 int b =(s->temp[0][planes == 3 ? 3 * i + 2 : 4 * i + B] - g) & 0xFF;\
661 int r =(s->temp[0][planes == 3 ? 3 * i + 0 : 4 * i + R] - g) & 0xFF;\
662 int a = s->temp[0][planes * i + A];
663
664 #define STAT_BGRA \
665 s->stats[0][b]++; \
666 s->stats[1][g]++; \
667 s->stats[2][r]++; \
668 if (planes == 4) \
669 s->stats[2][a]++;
670
671 #define WRITE_GBRA \
672 put_bits(&s->pb, s->len[1][g], s->bits[1][g]); \
673 put_bits(&s->pb, s->len[0][b], s->bits[0][b]); \
674 put_bits(&s->pb, s->len[2][r], s->bits[2][r]); \
675 if (planes == 4) \
676 put_bits(&s->pb, s->len[2][a], s->bits[2][a]);
677
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89800 times.
89800 if ((s->flags & AV_CODEC_FLAG_PASS1) &&
679 (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)) {
680 for (i = 0; i < count; i++) {
681 LOAD_GBRA;
682 STAT_BGRA;
683 }
684
2/4
✓ Branch 0 taken 89800 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 89800 times.
89800 } else if (s->context || (s->flags & AV_CODEC_FLAG_PASS1)) {
685 for (i = 0; i < count; i++) {
686 LOAD_GBRA;
687 STAT_BGRA;
688 WRITE_GBRA;
689 }
690 } else {
691
2/2
✓ Branch 0 taken 30528000 times.
✓ Branch 1 taken 89800 times.
30617800 for (i = 0; i < count; i++) {
692
6/6
✓ Branch 0 taken 15264000 times.
✓ Branch 1 taken 15264000 times.
✓ Branch 2 taken 15264000 times.
✓ Branch 3 taken 15264000 times.
✓ Branch 4 taken 15264000 times.
✓ Branch 5 taken 15264000 times.
30528000 LOAD_GBRA;
693
2/2
✓ Branch 3 taken 15264000 times.
✓ Branch 4 taken 15264000 times.
30528000 WRITE_GBRA;
694 }
695 }
696 89800 return 0;
697 }
698
699 1600 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
700 const AVFrame *p, int *got_packet)
701 {
702 1600 HYuvEncContext *s = avctx->priv_data;
703 1600 const int width = avctx->width;
704 1600 const int width2 = avctx->width >> 1;
705 1600 const int height = avctx->height;
706 1600 const int fake_ystride = (1 + s->interlaced) * p->linesize[0];
707 1600 const int fake_ustride = (1 + s->interlaced) * p->linesize[1];
708 1600 const int fake_vstride = (1 + s->interlaced) * p->linesize[2];
709 1600 int i, j, size = 0, ret;
710
711
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1600 times.
1600 if ((ret = ff_alloc_packet(avctx, pkt, width * height * 3 * 4 + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
712 return ret;
713
714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
1600 if (s->context) {
715 size = store_huffman_tables(s, pkt->data);
716 if (size < 0)
717 return size;
718
719 for (i = 0; i < 4; i++)
720 for (j = 0; j < s->vlc_n; j++)
721 s->stats[i][j] >>= 1;
722 }
723
724 1600 init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
725
726
2/2
✓ Branch 0 taken 1400 times.
✓ Branch 1 taken 200 times.
1600 if (avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
727
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1200 times.
1800 avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
728 int lefty, leftu, leftv, y, cy;
729
730 400 put_bits(&s->pb, 8, leftv = p->data[2][0]);
731 400 put_bits(&s->pb, 8, lefty = p->data[0][1]);
732 400 put_bits(&s->pb, 8, leftu = p->data[1][0]);
733 400 put_bits(&s->pb, 8, p->data[0][0]);
734
735 400 lefty = sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
736 400 leftu = sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
737 400 leftv = sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
738
739 400 encode_422_bitstream(s, 2, width-2);
740
741
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 350 times.
400 if (s->predictor==MEDIAN) {
742 int lefttopy, lefttopu, lefttopv;
743 50 cy = y = 1;
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (s->interlaced) {
745 lefty = sub_left_prediction(s, s->temp[0], p->data[0] + p->linesize[0], width , lefty);
746 leftu = sub_left_prediction(s, s->temp[1], p->data[1] + p->linesize[1], width2, leftu);
747 leftv = sub_left_prediction(s, s->temp[2], p->data[2] + p->linesize[2], width2, leftv);
748
749 encode_422_bitstream(s, 0, width);
750 y++; cy++;
751 }
752
753 50 lefty = sub_left_prediction(s, s->temp[0], p->data[0] + fake_ystride, 4, lefty);
754 50 leftu = sub_left_prediction(s, s->temp[1], p->data[1] + fake_ustride, 2, leftu);
755 50 leftv = sub_left_prediction(s, s->temp[2], p->data[2] + fake_vstride, 2, leftv);
756
757 50 encode_422_bitstream(s, 0, 4);
758
759 50 lefttopy = p->data[0][3];
760 50 lefttopu = p->data[1][1];
761 50 lefttopv = p->data[2][1];
762 50 s->llvidencdsp.sub_median_pred(s->temp[0], p->data[0] + 4, p->data[0] + fake_ystride + 4, width - 4, &lefty, &lefttopy);
763 50 s->llvidencdsp.sub_median_pred(s->temp[1], p->data[1] + 2, p->data[1] + fake_ustride + 2, width2 - 2, &leftu, &lefttopu);
764 50 s->llvidencdsp.sub_median_pred(s->temp[2], p->data[2] + 2, p->data[2] + fake_vstride + 2, width2 - 2, &leftv, &lefttopv);
765 50 encode_422_bitstream(s, 0, width - 4);
766 50 y++; cy++;
767
768
2/2
✓ Branch 0 taken 14300 times.
✓ Branch 1 taken 50 times.
14350 for (; y < height; y++,cy++) {
769 const uint8_t *ydst, *udst, *vdst;
770
771
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14300 times.
14300 if (s->bitstream_bpp == 12) {
772 while (2 * cy > y) {
773 ydst = p->data[0] + p->linesize[0] * y;
774 s->llvidencdsp.sub_median_pred(s->temp[0], ydst - fake_ystride, ydst, width, &lefty, &lefttopy);
775 encode_gray_bitstream(s, width);
776 y++;
777 }
778 if (y >= height) break;
779 }
780 14300 ydst = p->data[0] + p->linesize[0] * y;
781 14300 udst = p->data[1] + p->linesize[1] * cy;
782 14300 vdst = p->data[2] + p->linesize[2] * cy;
783
784 14300 s->llvidencdsp.sub_median_pred(s->temp[0], ydst - fake_ystride, ydst, width, &lefty, &lefttopy);
785 14300 s->llvidencdsp.sub_median_pred(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
786 14300 s->llvidencdsp.sub_median_pred(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
787
788 14300 encode_422_bitstream(s, 0, width);
789 }
790 } else {
791
2/2
✓ Branch 0 taken 52800 times.
✓ Branch 1 taken 150 times.
52950 for (cy = y = 1; y < height; y++, cy++) {
792 const uint8_t *ydst, *udst, *vdst;
793
794 /* encode a luma only line & y++ */
795
2/2
✓ Branch 0 taken 22450 times.
✓ Branch 1 taken 30350 times.
52800 if (s->bitstream_bpp == 12) {
796 22450 ydst = p->data[0] + p->linesize[0] * y;
797
798
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 22450 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
22450 if (s->predictor == PLANE && s->interlaced < y) {
799 s->llvidencdsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
800
801 lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
802 } else {
803 22450 lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
804 }
805 22450 encode_gray_bitstream(s, width);
806 22450 y++;
807
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 22250 times.
22450 if (y >= height) break;
808 }
809
810 52600 ydst = p->data[0] + p->linesize[0] * y;
811 52600 udst = p->data[1] + p->linesize[1] * cy;
812 52600 vdst = p->data[2] + p->linesize[2] * cy;
813
814
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 52600 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
52600 if (s->predictor == PLANE && s->interlaced < cy) {
815 s->llvidencdsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
816 s->llvidencdsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
817 s->llvidencdsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
818
819 lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
820 leftu = sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
821 leftv = sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
822 } else {
823 52600 lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
824 52600 leftu = sub_left_prediction(s, s->temp[1], udst, width2, leftu);
825 52600 leftv = sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
826 }
827
828 52600 encode_422_bitstream(s, 0, width);
829 }
830 }
831
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1000 times.
1200 } else if(avctx->pix_fmt == AV_PIX_FMT_RGB32) {
832 200 const uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
833 200 const int stride = -p->linesize[0];
834 200 const int fake_stride = -fake_ystride;
835 int leftr, leftg, leftb, lefta;
836
837 200 put_bits(&s->pb, 8, lefta = data[A]);
838 200 put_bits(&s->pb, 8, leftr = data[R]);
839 200 put_bits(&s->pb, 8, leftg = data[G]);
840 200 put_bits(&s->pb, 8, leftb = data[B]);
841
842 200 sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1,
843 &leftr, &leftg, &leftb, &lefta);
844 200 encode_bgra_bitstream(s, width - 1, 4);
845
846
2/2
✓ Branch 0 taken 44700 times.
✓ Branch 1 taken 200 times.
44900 for (int y = 1; y < height; y++) {
847 44700 const uint8_t *dst = data + y*stride;
848
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 44700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
44700 if (s->predictor == PLANE && s->interlaced < y) {
849 s->llvidencdsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
850 sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width,
851 &leftr, &leftg, &leftb, &lefta);
852 } else {
853 44700 sub_left_prediction_bgr32(s, s->temp[0], dst, width,
854 &leftr, &leftg, &leftb, &lefta);
855 }
856 44700 encode_bgra_bitstream(s, width, 4);
857 }
858
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 800 times.
1000 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
859 200 const uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
860 200 const int stride = -p->linesize[0];
861 200 const int fake_stride = -fake_ystride;
862 int leftr, leftg, leftb;
863
864 200 put_bits(&s->pb, 8, leftr = data[0]);
865 200 put_bits(&s->pb, 8, leftg = data[1]);
866 200 put_bits(&s->pb, 8, leftb = data[2]);
867 200 put_bits(&s->pb, 8, 0);
868
869 200 sub_left_prediction_rgb24(s, s->temp[0], data + 3, width - 1,
870 &leftr, &leftg, &leftb);
871 200 encode_bgra_bitstream(s, width-1, 3);
872
873
2/2
✓ Branch 0 taken 44700 times.
✓ Branch 1 taken 200 times.
44900 for (int y = 1; y < height; y++) {
874 44700 const uint8_t *dst = data + y * stride;
875
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 44700 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
44700 if (s->predictor == PLANE && s->interlaced < y) {
876 s->llvidencdsp.diff_bytes(s->temp[1], dst, dst - fake_stride,
877 width * 3);
878 sub_left_prediction_rgb24(s, s->temp[0], s->temp[1], width,
879 &leftr, &leftg, &leftb);
880 } else {
881 44700 sub_left_prediction_rgb24(s, s->temp[0], dst, width,
882 &leftr, &leftg, &leftb);
883 }
884 44700 encode_bgra_bitstream(s, width, 3);
885 }
886
1/2
✓ Branch 0 taken 800 times.
✗ Branch 1 not taken.
800 } else if (s->version > 2) {
887 int plane;
888
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 800 times.
3200 for (plane = 0; plane < 1 + 2*s->chroma + s->alpha; plane++) {
889 int left, y;
890 2400 int w = width;
891 2400 int h = height;
892 2400 int fake_stride = fake_ystride;
893
894
5/6
✓ Branch 0 taken 2400 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1600 times.
✓ Branch 3 taken 800 times.
✓ Branch 4 taken 800 times.
✓ Branch 5 taken 800 times.
2400 if (s->chroma && (plane == 1 || plane == 2)) {
895 1600 w >>= s->chroma_h_shift;
896 1600 h >>= s->chroma_v_shift;
897
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
1600 fake_stride = plane == 1 ? fake_ustride : fake_vstride;
898 }
899
900 2400 left = sub_left_prediction(s, s->temp[0], p->data[plane], w , 0);
901
902 2400 encode_plane_bitstream(s, w, plane);
903
904
2/2
✓ Branch 0 taken 600 times.
✓ Branch 1 taken 1800 times.
2400 if (s->predictor==MEDIAN) {
905 int lefttop;
906 600 y = 1;
907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 if (s->interlaced) {
908 left = sub_left_prediction(s, s->temp[0], p->data[plane] + p->linesize[plane], w , left);
909
910 encode_plane_bitstream(s, w, plane);
911 y++;
912 }
913
914 600 lefttop = p->data[plane][0];
915
916
2/2
✓ Branch 0 taken 89200 times.
✓ Branch 1 taken 600 times.
89800 for (; y < h; y++) {
917 89200 const uint8_t *dst = p->data[plane] + p->linesize[plane] * y;
918
919 89200 sub_median_prediction(s, s->temp[0], dst - fake_stride, dst, w , &left, &lefttop);
920
921 89200 encode_plane_bitstream(s, w, plane);
922 }
923 } else {
924
2/2
✓ Branch 0 taken 402300 times.
✓ Branch 1 taken 1800 times.
404100 for (y = 1; y < h; y++) {
925 402300 const uint8_t *dst = p->data[plane] + p->linesize[plane] * y;
926
927
3/4
✓ Branch 0 taken 134100 times.
✓ Branch 1 taken 268200 times.
✓ Branch 2 taken 134100 times.
✗ Branch 3 not taken.
402300 if (s->predictor == PLANE && s->interlaced < y) {
928 134100 diff_bytes(s, s->temp[1], dst, dst - fake_stride, w);
929
930 134100 left = sub_left_prediction(s, s->temp[0], s->temp[1], w , left);
931 } else {
932 268200 left = sub_left_prediction(s, s->temp[0], dst, w , left);
933 }
934
935 402300 encode_plane_bitstream(s, w, plane);
936 }
937 }
938 }
939 } else {
940 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
941 }
942
943 1600 size += (put_bits_count(&s->pb) + 31) / 8;
944 1600 put_bits(&s->pb, 16, 0);
945 1600 put_bits(&s->pb, 15, 0);
946 1600 size /= 4;
947
948
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1600 if ((s->flags & AV_CODEC_FLAG_PASS1) && (s->picture_number & 31) == 0) {
949 int j;
950 char *p = avctx->stats_out;
951 char *end = p + STATS_OUT_SIZE;
952 for (i = 0; i < 4; i++) {
953 for (j = 0; j < s->vlc_n; j++) {
954 snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
955 p += strlen(p);
956 s->stats[i][j]= 0;
957 }
958 snprintf(p, end-p, "\n");
959 p++;
960 if (end <= p)
961 return AVERROR(ENOMEM);
962 }
963
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
1600 } else if (avctx->stats_out)
964 avctx->stats_out[0] = '\0';
965
1/2
✓ Branch 0 taken 1600 times.
✗ Branch 1 not taken.
1600 if (!(s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)) {
966 1600 flush_put_bits(&s->pb);
967 1600 s->bdsp.bswap_buf((uint32_t *) pkt->data, (uint32_t *) pkt->data, size);
968 }
969
970 1600 s->picture_number++;
971
972 1600 pkt->size = size * 4;
973 1600 *got_packet = 1;
974
975 1600 return 0;
976 }
977
978 32 static av_cold int encode_end(AVCodecContext *avctx)
979 {
980 32 HYuvEncContext *s = avctx->priv_data;
981
982 32 av_freep(&avctx->stats_out);
983
984
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 32 times.
128 for (int i = 0; i < 3; i++)
985 96 av_freep(&s->temp[i]);
986
987 32 return 0;
988 }
989
990 #define OFFSET(x) offsetof(HYuvEncContext, x)
991 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
992
993 static const AVOption options[] = {
994 /* ffvhuff-only options */
995 { "context", "Set per-frame huffman tables", OFFSET(context), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
996 /* Common options */
997 { "non_deterministic", "Allow multithreading for e.g. context=1 at the expense of determinism",
998 OFFSET(non_determ), AV_OPT_TYPE_BOOL, { .i64 = 0 },
999 0, 1, VE },
1000 { "pred", "Prediction method", OFFSET(predictor), AV_OPT_TYPE_INT, { .i64 = LEFT }, LEFT, MEDIAN, VE, .unit = "pred" },
1001 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LEFT }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1002 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PLANE }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1003 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MEDIAN }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1004 { NULL },
1005 };
1006
1007 static const AVClass normal_class = {
1008 .class_name = "huffyuv",
1009 .item_name = av_default_item_name,
1010 .option = options + 1,
1011 .version = LIBAVUTIL_VERSION_INT,
1012 };
1013
1014 const FFCodec ff_huffyuv_encoder = {
1015 .p.name = "huffyuv",
1016 CODEC_LONG_NAME("Huffyuv / HuffYUV"),
1017 .p.type = AVMEDIA_TYPE_VIDEO,
1018 .p.id = AV_CODEC_ID_HUFFYUV,
1019 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1020 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1021 .priv_data_size = sizeof(HYuvEncContext),
1022 .init = encode_init,
1023 FF_CODEC_ENCODE_CB(encode_frame),
1024 .close = encode_end,
1025 .p.priv_class = &normal_class,
1026 CODEC_PIXFMTS(AV_PIX_FMT_YUV422P, AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32),
1027 .color_ranges = AVCOL_RANGE_MPEG,
1028 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1029 };
1030
1031 #if CONFIG_FFVHUFF_ENCODER
1032 static const AVClass ff_class = {
1033 .class_name = "ffvhuff",
1034 .item_name = av_default_item_name,
1035 .option = options,
1036 .version = LIBAVUTIL_VERSION_INT,
1037 };
1038
1039 const FFCodec ff_ffvhuff_encoder = {
1040 .p.name = "ffvhuff",
1041 CODEC_LONG_NAME("Huffyuv FFmpeg variant"),
1042 .p.type = AVMEDIA_TYPE_VIDEO,
1043 .p.id = AV_CODEC_ID_FFVHUFF,
1044 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1045 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1046 .priv_data_size = sizeof(HYuvEncContext),
1047 .init = encode_init,
1048 FF_CODEC_ENCODE_CB(encode_frame),
1049 .close = encode_end,
1050 .p.priv_class = &ff_class,
1051 CODEC_PIXFMTS(
1052 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV411P,
1053 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
1054 AV_PIX_FMT_GBRP,
1055 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
1056 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
1057 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
1058 AV_PIX_FMT_GBRAP,
1059 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
1060 AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
1061 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
1062 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
1063 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P16,
1064 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16,
1065 AV_PIX_FMT_RGB24,
1066 AV_PIX_FMT_RGB32),
1067 .color_ranges = AVCOL_RANGE_MPEG,
1068 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1069 };
1070 #endif
1071