FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/huffyuvenc.c
Date: 2024-04-20 14:10:07
Exec Total Coverage
Lines: 317 479 66.2%
Functions: 13 14 92.9%
Branches: 176 365 48.2%

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/emms.h"
43 #include "libavutil/mem.h"
44 #include "libavutil/opt.h"
45 #include "libavutil/pixdesc.h"
46
47 typedef struct HYuvEncContext {
48 AVClass *class;
49 AVCodecContext *avctx;
50 PutBitContext pb;
51 Predictor predictor;
52 int interlaced;
53 int decorrelate;
54 int bitstream_bpp;
55 int version;
56 int bps;
57 int n; // 1<<bps
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->n - 1, w);
88 }
89 134100 }
90
91 718400 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 718400 int min_width = FFMIN(w, 32);
96
97
2/2
✓ Branch 0 taken 359200 times.
✓ Branch 1 taken 359200 times.
718400 if (s->bps <= 8) {
98
2/2
✓ Branch 0 taken 11417900 times.
✓ Branch 1 taken 359200 times.
11777100 for (i = 0; i < min_width; i++) { /* scalar loop before dsp call */
99 11417900 const int temp = src[i];
100 11417900 dst[i] = temp - left;
101 11417900 left = temp;
102 }
103
2/2
✓ Branch 0 taken 5100 times.
✓ Branch 1 taken 354100 times.
359200 if (w < 32)
104 5100 return left;
105 354100 s->llvidencdsp.diff_bytes(dst + 32, src + 32, src + 31, w - 32);
106 354100 return src[w-1];
107 } else {
108 359200 const uint16_t *src16 = (const uint16_t *)src;
109 359200 uint16_t *dst16 = ( uint16_t *)dst;
110
2/2
✓ Branch 0 taken 11417900 times.
✓ Branch 1 taken 359200 times.
11777100 for (i = 0; i < min_width; i++) { /* scalar loop before dsp call */
111 11417900 const int temp = src16[i];
112 11417900 dst16[i] = temp - left;
113 11417900 left = temp;
114 }
115
2/2
✓ Branch 0 taken 5100 times.
✓ Branch 1 taken 354100 times.
359200 if (w < 32)
116 5100 return left;
117 354100 s->hencdsp.diff_int16(dst16 + 32, src16 + 32, src16 + 31, s->n - 1, w - 32);
118 354100 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 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 if (s->bps <= 8) {
191 s->llvidencdsp.sub_median_pred(dst, src1, src2, w , left, left_top);
192 } else {
193 s->hencdsp.sub_hfyu_median_pred_int16((uint16_t *)dst, (const uint16_t *)src1, (const uint16_t *)src2, s->n - 1, w , left, left_top);
194 }
195 }
196
197 96 static int store_table(HYuvEncContext *s, const uint8_t *len, uint8_t *buf)
198 {
199 int i;
200 96 int index = 0;
201 96 int n = s->vlc_n;
202
203
2/2
✓ Branch 0 taken 3876 times.
✓ Branch 1 taken 96 times.
3972 for (i = 0; i < n;) {
204 3876 int val = len[i];
205 3876 int repeat = 0;
206
207
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++)
208 273408 repeat++;
209
210
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);
211
2/2
✓ Branch 0 taken 2244 times.
✓ Branch 1 taken 1632 times.
3876 if (repeat > 7) {
212 2244 buf[index++] = val;
213 2244 buf[index++] = repeat;
214 } else {
215 1632 buf[index++] = val | (repeat << 5);
216 }
217 }
218
219 96 return index;
220 }
221
222 32 static int store_huffman_tables(HYuvEncContext *s, uint8_t *buf)
223 {
224 int i, ret;
225 32 int size = 0;
226 32 int count = 3;
227
228
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 if (s->version > 2)
229 16 count = 1 + s->alpha + 2*s->chroma;
230
231
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 32 times.
128 for (i = 0; i < count; i++) {
232
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)
233 return ret;
234
235 96 ret = ff_huffyuv_generate_bits_table(s->bits[i], s->len[i], s->vlc_n);
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (ret < 0)
237 return ret;
238
239 96 size += store_table(s, s->len[i], buf + size);
240 }
241 32 return size;
242 }
243
244 32 static av_cold int encode_init(AVCodecContext *avctx)
245 {
246 32 HYuvEncContext *s = avctx->priv_data;
247 int i, j;
248 int ret;
249 const AVPixFmtDescriptor *desc;
250
251 32 s->avctx = avctx;
252 32 s->flags = avctx->flags;
253
254 32 ff_bswapdsp_init(&s->bdsp);
255 32 ff_huffyuvencdsp_init(&s->hencdsp, avctx->pix_fmt);
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
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) {
278 8 case AV_PIX_FMT_YUV420P:
279 case AV_PIX_FMT_YUV422P:
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (avctx->width & 1) {
281 av_log(avctx, AV_LOG_ERROR, "Width must be even for this colorspace.\n");
282 return AVERROR(EINVAL);
283 }
284
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;
285 8 break;
286 16 case AV_PIX_FMT_YUV444P:
287 case AV_PIX_FMT_YUV410P:
288 case AV_PIX_FMT_YUV411P:
289 case AV_PIX_FMT_YUV440P:
290 case AV_PIX_FMT_GBRP:
291 case AV_PIX_FMT_GBRP9:
292 case AV_PIX_FMT_GBRP10:
293 case AV_PIX_FMT_GBRP12:
294 case AV_PIX_FMT_GBRP14:
295 case AV_PIX_FMT_GBRP16:
296 case AV_PIX_FMT_GRAY8:
297 case AV_PIX_FMT_GRAY16:
298 case AV_PIX_FMT_YUVA444P:
299 case AV_PIX_FMT_YUVA420P:
300 case AV_PIX_FMT_YUVA422P:
301 case AV_PIX_FMT_GBRAP:
302 case AV_PIX_FMT_YUV420P9:
303 case AV_PIX_FMT_YUV420P10:
304 case AV_PIX_FMT_YUV420P12:
305 case AV_PIX_FMT_YUV420P14:
306 case AV_PIX_FMT_YUV420P16:
307 case AV_PIX_FMT_YUV422P9:
308 case AV_PIX_FMT_YUV422P10:
309 case AV_PIX_FMT_YUV422P12:
310 case AV_PIX_FMT_YUV422P14:
311 case AV_PIX_FMT_YUV422P16:
312 case AV_PIX_FMT_YUV444P9:
313 case AV_PIX_FMT_YUV444P10:
314 case AV_PIX_FMT_YUV444P12:
315 case AV_PIX_FMT_YUV444P14:
316 case AV_PIX_FMT_YUV444P16:
317 case AV_PIX_FMT_YUVA420P9:
318 case AV_PIX_FMT_YUVA420P10:
319 case AV_PIX_FMT_YUVA420P16:
320 case AV_PIX_FMT_YUVA422P9:
321 case AV_PIX_FMT_YUVA422P10:
322 case AV_PIX_FMT_YUVA422P16:
323 case AV_PIX_FMT_YUVA444P9:
324 case AV_PIX_FMT_YUVA444P10:
325 case AV_PIX_FMT_YUVA444P16:
326 16 s->version = 3;
327 16 break;
328 4 case AV_PIX_FMT_RGB32:
329 4 s->bitstream_bpp = 32;
330 4 break;
331 4 case AV_PIX_FMT_RGB24:
332 4 s->bitstream_bpp = 24;
333 4 break;
334 default:
335 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
336 return AVERROR(EINVAL);
337 }
338 32 s->n = 1<<s->bps;
339 32 s->vlc_n = FFMIN(s->n, MAX_VLC_N);
340
341 32 avctx->bits_per_coded_sample = s->bitstream_bpp;
342
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);
343 32 s->interlaced = avctx->flags & AV_CODEC_FLAG_INTERLACED_ME ? 1 : 0;
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->context) {
345 if (s->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2)) {
346 av_log(avctx, AV_LOG_ERROR,
347 "context=1 is not compatible with "
348 "2 pass huffyuv encoding\n");
349 return AVERROR(EINVAL);
350 }
351 }
352
353
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 20 times.
32 if (avctx->codec->id == AV_CODEC_ID_HUFFYUV) {
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (s->interlaced != ( avctx->height > 288 ))
355 av_log(avctx, AV_LOG_INFO,
356 "using huffyuv 2.2.0 or newer interlacing flag\n");
357 }
358
359
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) {
360 av_log(avctx, AV_LOG_ERROR, "Ver > 3 is under development, files encoded with it may not be decodable with future versions!!!\n"
361 "Use vstrict=-2 / -strict -2 to use it anyway.\n");
362 return AVERROR(EINVAL);
363 }
364
365
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) {
366 av_log(avctx, AV_LOG_ERROR,
367 "Error: RGB is incompatible with median predictor\n");
368 return AVERROR(EINVAL);
369 }
370
371 32 avctx->extradata[0] = s->predictor | (s->decorrelate << 6);
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 avctx->extradata[2] = s->interlaced ? 0x10 : 0x20;
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->context)
374 avctx->extradata[2] |= 0x40;
375
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 if (s->version < 3) {
376 16 avctx->extradata[1] = s->bitstream_bpp;
377 16 avctx->extradata[3] = 0;
378 } else {
379 16 avctx->extradata[1] = ((s->bps-1)<<4) | s->chroma_h_shift | (s->chroma_v_shift<<2);
380
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (s->chroma)
381
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 avctx->extradata[2] |= s->yuv ? 1 : 2;
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (s->alpha)
383 avctx->extradata[2] |= 4;
384 16 avctx->extradata[3] = 1;
385 }
386 32 avctx->extradata_size = 4;
387
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (avctx->stats_in) {
389 char *p = avctx->stats_in;
390
391 for (i = 0; i < 4; i++)
392 for (j = 0; j < s->vlc_n; j++)
393 s->stats[i][j] = 1;
394
395 for (;;) {
396 for (i = 0; i < 4; i++) {
397 char *next;
398
399 for (j = 0; j < s->vlc_n; j++) {
400 s->stats[i][j] += strtol(p, &next, 0);
401 if (next == p) return -1;
402 p = next;
403 }
404 }
405 if (p[0] == 0 || p[1] == 0 || p[2] == 0) break;
406 }
407 } else {
408
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 32 times.
160 for (i = 0; i < 4; i++)
409
2/2
✓ Branch 0 taken 364544 times.
✓ Branch 1 taken 128 times.
364672 for (j = 0; j < s->vlc_n; j++) {
410 364544 int d = FFMIN(j, s->vlc_n - j);
411
412 364544 s->stats[i][j] = 100000000 / (d*d + 1);
413 }
414 }
415
416 32 ret = store_huffman_tables(s, avctx->extradata + avctx->extradata_size);
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
418 return ret;
419 32 avctx->extradata_size += ret;
420
421
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (s->context) {
422 for (i = 0; i < 4; i++) {
423 int pels = avctx->width * avctx->height / (i ? 40 : 10);
424 for (j = 0; j < s->vlc_n; j++) {
425 int d = FFMIN(j, s->vlc_n - j);
426 s->stats[i][j] = pels/(d*d + 1);
427 }
428 }
429 } else {
430
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 32 times.
160 for (i = 0; i < 4; i++)
431
2/2
✓ Branch 0 taken 364544 times.
✓ Branch 1 taken 128 times.
364672 for (j = 0; j < s->vlc_n; j++)
432 364544 s->stats[i][j]= 0;
433 }
434
435 32 s->picture_number=0;
436
437
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 32 times.
128 for (int i = 0; i < 3; i++) {
438 96 s->temp[i] = av_malloc(4 * avctx->width + 16);
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (!s->temp[i])
440 return AVERROR(ENOMEM);
441 }
442
443 32 return 0;
444 }
445 67350 static int encode_422_bitstream(HYuvEncContext *s, int offset, int count)
446 {
447 int i;
448 67350 const uint8_t *y = s->temp[0] + offset;
449 67350 const uint8_t *u = s->temp[1] + offset / 2;
450 67350 const uint8_t *v = s->temp[2] + offset / 2;
451
452
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 67350 times.
67350 if (put_bytes_left(&s->pb, 0) < 2 * 4 * count) {
453 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
454 return -1;
455 }
456
457 #define LOAD4\
458 int y0 = y[2 * i];\
459 int y1 = y[2 * i + 1];\
460 int u0 = u[i];\
461 int v0 = v[i];
462
463 67350 count /= 2;
464
465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67350 times.
67350 if (s->flags & AV_CODEC_FLAG_PASS1) {
466 for(i = 0; i < count; i++) {
467 LOAD4;
468 s->stats[0][y0]++;
469 s->stats[1][u0]++;
470 s->stats[0][y1]++;
471 s->stats[2][v0]++;
472 }
473 }
474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67350 times.
67350 if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)
475 return 0;
476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67350 times.
67350 if (s->context) {
477 for (i = 0; i < count; i++) {
478 LOAD4;
479 s->stats[0][y0]++;
480 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
481 s->stats[1][u0]++;
482 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
483 s->stats[0][y1]++;
484 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
485 s->stats[2][v0]++;
486 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
487 }
488 } else {
489
2/2
✓ Branch 0 taken 11447750 times.
✓ Branch 1 taken 67350 times.
11515100 for(i = 0; i < count; i++) {
490 11447750 LOAD4;
491 11447750 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
492 11447750 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
493 11447750 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
494 11447750 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
495 }
496 }
497 67350 return 0;
498 }
499
500 493900 static int encode_plane_bitstream(HYuvEncContext *s, int width, int plane)
501 {
502 493900 int count = width/2;
503
504
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 493900 times.
493900 if (put_bytes_left(&s->pb, 0) < count * s->bps / 2) {
505 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
506 return -1;
507 }
508
509 #define LOADEND\
510 int y0 = s->temp[0][width-1];
511 #define LOADEND_14\
512 int y0 = s->temp16[0][width-1] & mask;
513 #define LOADEND_16\
514 int y0 = s->temp16[0][width-1];
515 #define STATEND\
516 s->stats[plane][y0]++;
517 #define STATEND_16\
518 s->stats[plane][y0>>2]++;
519 #define WRITEEND\
520 put_bits(&s->pb, s->len[plane][y0], s->bits[plane][y0]);
521 #define WRITEEND_16\
522 put_bits(&s->pb, s->len[plane][y0>>2], s->bits[plane][y0>>2]);\
523 put_bits(&s->pb, 2, y0&3);
524
525 #define LOAD2\
526 int y0 = s->temp[0][2 * i];\
527 int y1 = s->temp[0][2 * i + 1];
528 #define LOAD2_14\
529 int y0 = s->temp16[0][2 * i] & mask;\
530 int y1 = s->temp16[0][2 * i + 1] & mask;
531 #define LOAD2_16\
532 int y0 = s->temp16[0][2 * i];\
533 int y1 = s->temp16[0][2 * i + 1];
534 #define STAT2\
535 s->stats[plane][y0]++;\
536 s->stats[plane][y1]++;
537 #define STAT2_16\
538 s->stats[plane][y0>>2]++;\
539 s->stats[plane][y1>>2]++;
540 #define WRITE2\
541 put_bits(&s->pb, s->len[plane][y0], s->bits[plane][y0]);\
542 put_bits(&s->pb, s->len[plane][y1], s->bits[plane][y1]);
543 #define WRITE2_16\
544 put_bits(&s->pb, s->len[plane][y0>>2], s->bits[plane][y0>>2]);\
545 put_bits(&s->pb, 2, y0&3);\
546 put_bits(&s->pb, s->len[plane][y1>>2], s->bits[plane][y1>>2]);\
547 put_bits(&s->pb, 2, y1&3);
548
549 #define ENCODE_PLANE(LOAD, LOADEND, WRITE, WRITEEND, STAT, STATEND) \
550 do { \
551 if (s->flags & AV_CODEC_FLAG_PASS1) { \
552 for (int i = 0; i < count; i++) { \
553 LOAD; \
554 STAT; \
555 } \
556 if (width & 1) { \
557 LOADEND; \
558 STATEND; \
559 } \
560 } \
561 if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT) \
562 return 0; \
563 \
564 if (s->context) { \
565 for (int i = 0; i < count; i++) { \
566 LOAD; \
567 STAT; \
568 WRITE; \
569 } \
570 if (width & 1) { \
571 LOADEND; \
572 STATEND; \
573 WRITEEND; \
574 } \
575 } else { \
576 for (int i = 0; i < count; i++) { \
577 LOAD; \
578 WRITE; \
579 } \
580 if (width & 1) { \
581 LOADEND; \
582 WRITEEND; \
583 } \
584 } \
585 } while (0)
586
587
2/2
✓ Branch 0 taken 134700 times.
✓ Branch 1 taken 359200 times.
493900 if (s->bps <= 8) {
588
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);
589
2/2
✓ Branch 0 taken 224500 times.
✓ Branch 1 taken 134700 times.
359200 } else if (s->bps <= 14) {
590 224500 int mask = s->n - 1;
591
592
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);
593 } else {
594
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);
595 }
596 #undef LOAD2
597 #undef STAT2
598 #undef WRITE2
599 493900 return 0;
600 }
601
602 22450 static int encode_gray_bitstream(HYuvEncContext *s, int count)
603 {
604 int i;
605
606
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22450 times.
22450 if (put_bytes_left(&s->pb, 0) < 4 * count) {
607 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
608 return -1;
609 }
610
611 #define LOAD2\
612 int y0 = s->temp[0][2 * i];\
613 int y1 = s->temp[0][2 * i + 1];
614 #define STAT2\
615 s->stats[0][y0]++;\
616 s->stats[0][y1]++;
617 #define WRITE2\
618 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
619 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
620
621 22450 count /= 2;
622
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22450 times.
22450 if (s->flags & AV_CODEC_FLAG_PASS1) {
624 for (i = 0; i < count; i++) {
625 LOAD2;
626 STAT2;
627 }
628 }
629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22450 times.
22450 if (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)
630 return 0;
631
632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22450 times.
22450 if (s->context) {
633 for (i = 0; i < count; i++) {
634 LOAD2;
635 STAT2;
636 WRITE2;
637 }
638 } else {
639
2/2
✓ Branch 0 taken 3816050 times.
✓ Branch 1 taken 22450 times.
3838500 for (i = 0; i < count; i++) {
640 3816050 LOAD2;
641 3816050 WRITE2;
642 }
643 }
644 22450 return 0;
645 }
646
647 89800 static inline int encode_bgra_bitstream(HYuvEncContext *s, int count, int planes)
648 {
649 int i;
650
651
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 89800 times.
89800 if (put_bytes_left(&s->pb, 0) < 4 * planes * count) {
652 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
653 return -1;
654 }
655
656 #define LOAD_GBRA \
657 int g = s->temp[0][planes == 3 ? 3 * i + 1 : 4 * i + G]; \
658 int b =(s->temp[0][planes == 3 ? 3 * i + 2 : 4 * i + B] - g) & 0xFF;\
659 int r =(s->temp[0][planes == 3 ? 3 * i + 0 : 4 * i + R] - g) & 0xFF;\
660 int a = s->temp[0][planes * i + A];
661
662 #define STAT_BGRA \
663 s->stats[0][b]++; \
664 s->stats[1][g]++; \
665 s->stats[2][r]++; \
666 if (planes == 4) \
667 s->stats[2][a]++;
668
669 #define WRITE_GBRA \
670 put_bits(&s->pb, s->len[1][g], s->bits[1][g]); \
671 put_bits(&s->pb, s->len[0][b], s->bits[0][b]); \
672 put_bits(&s->pb, s->len[2][r], s->bits[2][r]); \
673 if (planes == 4) \
674 put_bits(&s->pb, s->len[2][a], s->bits[2][a]);
675
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89800 times.
89800 if ((s->flags & AV_CODEC_FLAG_PASS1) &&
677 (s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)) {
678 for (i = 0; i < count; i++) {
679 LOAD_GBRA;
680 STAT_BGRA;
681 }
682
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)) {
683 for (i = 0; i < count; i++) {
684 LOAD_GBRA;
685 STAT_BGRA;
686 WRITE_GBRA;
687 }
688 } else {
689
2/2
✓ Branch 0 taken 30528000 times.
✓ Branch 1 taken 89800 times.
30617800 for (i = 0; i < count; i++) {
690
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;
691
2/2
✓ Branch 3 taken 15264000 times.
✓ Branch 4 taken 15264000 times.
30528000 WRITE_GBRA;
692 }
693 }
694 89800 return 0;
695 }
696
697 1600 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
698 const AVFrame *p, int *got_packet)
699 {
700 1600 HYuvEncContext *s = avctx->priv_data;
701 1600 const int width = avctx->width;
702 1600 const int width2 = avctx->width >> 1;
703 1600 const int height = avctx->height;
704 1600 const int fake_ystride = (1 + s->interlaced) * p->linesize[0];
705 1600 const int fake_ustride = (1 + s->interlaced) * p->linesize[1];
706 1600 const int fake_vstride = (1 + s->interlaced) * p->linesize[2];
707 1600 int i, j, size = 0, ret;
708
709
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)
710 return ret;
711
712
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
1600 if (s->context) {
713 size = store_huffman_tables(s, pkt->data);
714 if (size < 0)
715 return size;
716
717 for (i = 0; i < 4; i++)
718 for (j = 0; j < s->vlc_n; j++)
719 s->stats[i][j] >>= 1;
720 }
721
722 1600 init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
723
724
2/2
✓ Branch 0 taken 1400 times.
✓ Branch 1 taken 200 times.
1600 if (avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
725
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1200 times.
1800 avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
726 int lefty, leftu, leftv, y, cy;
727
728 400 put_bits(&s->pb, 8, leftv = p->data[2][0]);
729 400 put_bits(&s->pb, 8, lefty = p->data[0][1]);
730 400 put_bits(&s->pb, 8, leftu = p->data[1][0]);
731 400 put_bits(&s->pb, 8, p->data[0][0]);
732
733 400 lefty = sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
734 400 leftu = sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
735 400 leftv = sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
736
737 400 encode_422_bitstream(s, 2, width-2);
738
739
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 400 times.
400 if (s->predictor==MEDIAN) {
740 int lefttopy, lefttopu, lefttopv;
741 cy = y = 1;
742 if (s->interlaced) {
743 lefty = sub_left_prediction(s, s->temp[0], p->data[0] + p->linesize[0], width , lefty);
744 leftu = sub_left_prediction(s, s->temp[1], p->data[1] + p->linesize[1], width2, leftu);
745 leftv = sub_left_prediction(s, s->temp[2], p->data[2] + p->linesize[2], width2, leftv);
746
747 encode_422_bitstream(s, 0, width);
748 y++; cy++;
749 }
750
751 lefty = sub_left_prediction(s, s->temp[0], p->data[0] + fake_ystride, 4, lefty);
752 leftu = sub_left_prediction(s, s->temp[1], p->data[1] + fake_ustride, 2, leftu);
753 leftv = sub_left_prediction(s, s->temp[2], p->data[2] + fake_vstride, 2, leftv);
754
755 encode_422_bitstream(s, 0, 4);
756
757 lefttopy = p->data[0][3];
758 lefttopu = p->data[1][1];
759 lefttopv = p->data[2][1];
760 s->llvidencdsp.sub_median_pred(s->temp[0], p->data[0] + 4, p->data[0] + fake_ystride + 4, width - 4, &lefty, &lefttopy);
761 s->llvidencdsp.sub_median_pred(s->temp[1], p->data[1] + 2, p->data[1] + fake_ustride + 2, width2 - 2, &leftu, &lefttopu);
762 s->llvidencdsp.sub_median_pred(s->temp[2], p->data[2] + 2, p->data[2] + fake_vstride + 2, width2 - 2, &leftv, &lefttopv);
763 encode_422_bitstream(s, 0, width - 4);
764 y++; cy++;
765
766 for (; y < height; y++,cy++) {
767 const uint8_t *ydst, *udst, *vdst;
768
769 if (s->bitstream_bpp == 12) {
770 while (2 * cy > y) {
771 ydst = p->data[0] + p->linesize[0] * y;
772 s->llvidencdsp.sub_median_pred(s->temp[0], ydst - fake_ystride, ydst, width, &lefty, &lefttopy);
773 encode_gray_bitstream(s, width);
774 y++;
775 }
776 if (y >= height) break;
777 }
778 ydst = p->data[0] + p->linesize[0] * y;
779 udst = p->data[1] + p->linesize[1] * cy;
780 vdst = p->data[2] + p->linesize[2] * cy;
781
782 s->llvidencdsp.sub_median_pred(s->temp[0], ydst - fake_ystride, ydst, width, &lefty, &lefttopy);
783 s->llvidencdsp.sub_median_pred(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
784 s->llvidencdsp.sub_median_pred(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
785
786 encode_422_bitstream(s, 0, width);
787 }
788 } else {
789
2/2
✓ Branch 0 taken 67150 times.
✓ Branch 1 taken 200 times.
67350 for (cy = y = 1; y < height; y++, cy++) {
790 const uint8_t *ydst, *udst, *vdst;
791
792 /* encode a luma only line & y++ */
793
2/2
✓ Branch 0 taken 22450 times.
✓ Branch 1 taken 44700 times.
67150 if (s->bitstream_bpp == 12) {
794 22450 ydst = p->data[0] + p->linesize[0] * y;
795
796
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) {
797 s->llvidencdsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
798
799 lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
800 } else {
801 22450 lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
802 }
803 22450 encode_gray_bitstream(s, width);
804 22450 y++;
805
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 22250 times.
22450 if (y >= height) break;
806 }
807
808 66950 ydst = p->data[0] + p->linesize[0] * y;
809 66950 udst = p->data[1] + p->linesize[1] * cy;
810 66950 vdst = p->data[2] + p->linesize[2] * cy;
811
812
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 66950 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
66950 if (s->predictor == PLANE && s->interlaced < cy) {
813 s->llvidencdsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
814 s->llvidencdsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
815 s->llvidencdsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
816
817 lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
818 leftu = sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
819 leftv = sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
820 } else {
821 66950 lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
822 66950 leftu = sub_left_prediction(s, s->temp[1], udst, width2, leftu);
823 66950 leftv = sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
824 }
825
826 66950 encode_422_bitstream(s, 0, width);
827 }
828 }
829
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1000 times.
1200 } else if(avctx->pix_fmt == AV_PIX_FMT_RGB32) {
830 200 const uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
831 200 const int stride = -p->linesize[0];
832 200 const int fake_stride = -fake_ystride;
833 int leftr, leftg, leftb, lefta;
834
835 200 put_bits(&s->pb, 8, lefta = data[A]);
836 200 put_bits(&s->pb, 8, leftr = data[R]);
837 200 put_bits(&s->pb, 8, leftg = data[G]);
838 200 put_bits(&s->pb, 8, leftb = data[B]);
839
840 200 sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1,
841 &leftr, &leftg, &leftb, &lefta);
842 200 encode_bgra_bitstream(s, width - 1, 4);
843
844
2/2
✓ Branch 0 taken 44700 times.
✓ Branch 1 taken 200 times.
44900 for (int y = 1; y < height; y++) {
845 44700 const uint8_t *dst = data + y*stride;
846
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) {
847 s->llvidencdsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
848 sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width,
849 &leftr, &leftg, &leftb, &lefta);
850 } else {
851 44700 sub_left_prediction_bgr32(s, s->temp[0], dst, width,
852 &leftr, &leftg, &leftb, &lefta);
853 }
854 44700 encode_bgra_bitstream(s, width, 4);
855 }
856
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 800 times.
1000 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
857 200 const uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
858 200 const int stride = -p->linesize[0];
859 200 const int fake_stride = -fake_ystride;
860 int leftr, leftg, leftb;
861
862 200 put_bits(&s->pb, 8, leftr = data[0]);
863 200 put_bits(&s->pb, 8, leftg = data[1]);
864 200 put_bits(&s->pb, 8, leftb = data[2]);
865 200 put_bits(&s->pb, 8, 0);
866
867 200 sub_left_prediction_rgb24(s, s->temp[0], data + 3, width - 1,
868 &leftr, &leftg, &leftb);
869 200 encode_bgra_bitstream(s, width-1, 3);
870
871
2/2
✓ Branch 0 taken 44700 times.
✓ Branch 1 taken 200 times.
44900 for (int y = 1; y < height; y++) {
872 44700 const uint8_t *dst = data + y * stride;
873
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) {
874 s->llvidencdsp.diff_bytes(s->temp[1], dst, dst - fake_stride,
875 width * 3);
876 sub_left_prediction_rgb24(s, s->temp[0], s->temp[1], width,
877 &leftr, &leftg, &leftb);
878 } else {
879 44700 sub_left_prediction_rgb24(s, s->temp[0], dst, width,
880 &leftr, &leftg, &leftb);
881 }
882 44700 encode_bgra_bitstream(s, width, 3);
883 }
884
1/2
✓ Branch 0 taken 800 times.
✗ Branch 1 not taken.
800 } else if (s->version > 2) {
885 int plane;
886
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 800 times.
3200 for (plane = 0; plane < 1 + 2*s->chroma + s->alpha; plane++) {
887 int left, y;
888 2400 int w = width;
889 2400 int h = height;
890 2400 int fake_stride = fake_ystride;
891
892
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)) {
893 1600 w >>= s->chroma_h_shift;
894 1600 h >>= s->chroma_v_shift;
895
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
1600 fake_stride = plane == 1 ? fake_ustride : fake_vstride;
896 }
897
898 2400 left = sub_left_prediction(s, s->temp[0], p->data[plane], w , 0);
899
900 2400 encode_plane_bitstream(s, w, plane);
901
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2400 times.
2400 if (s->predictor==MEDIAN) {
903 int lefttop;
904 y = 1;
905 if (s->interlaced) {
906 left = sub_left_prediction(s, s->temp[0], p->data[plane] + p->linesize[plane], w , left);
907
908 encode_plane_bitstream(s, w, plane);
909 y++;
910 }
911
912 lefttop = p->data[plane][0];
913
914 for (; y < h; y++) {
915 const uint8_t *dst = p->data[plane] + p->linesize[plane] * y;
916
917 sub_median_prediction(s, s->temp[0], dst - fake_stride, dst, w , &left, &lefttop);
918
919 encode_plane_bitstream(s, w, plane);
920 }
921 } else {
922
2/2
✓ Branch 0 taken 491500 times.
✓ Branch 1 taken 2400 times.
493900 for (y = 1; y < h; y++) {
923 491500 const uint8_t *dst = p->data[plane] + p->linesize[plane] * y;
924
925
3/4
✓ Branch 0 taken 134100 times.
✓ Branch 1 taken 357400 times.
✓ Branch 2 taken 134100 times.
✗ Branch 3 not taken.
491500 if (s->predictor == PLANE && s->interlaced < y) {
926 134100 diff_bytes(s, s->temp[1], dst, dst - fake_stride, w);
927
928 134100 left = sub_left_prediction(s, s->temp[0], s->temp[1], w , left);
929 } else {
930 357400 left = sub_left_prediction(s, s->temp[0], dst, w , left);
931 }
932
933 491500 encode_plane_bitstream(s, w, plane);
934 }
935 }
936 }
937 } else {
938 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
939 }
940 1600 emms_c();
941
942 1600 size += (put_bits_count(&s->pb) + 31) / 8;
943 1600 put_bits(&s->pb, 16, 0);
944 1600 put_bits(&s->pb, 15, 0);
945 1600 size /= 4;
946
947
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) {
948 int j;
949 char *p = avctx->stats_out;
950 char *end = p + STATS_OUT_SIZE;
951 for (i = 0; i < 4; i++) {
952 for (j = 0; j < s->vlc_n; j++) {
953 snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
954 p += strlen(p);
955 s->stats[i][j]= 0;
956 }
957 snprintf(p, end-p, "\n");
958 p++;
959 if (end <= p)
960 return AVERROR(ENOMEM);
961 }
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1600 times.
1600 } else if (avctx->stats_out)
963 avctx->stats_out[0] = '\0';
964
1/2
✓ Branch 0 taken 1600 times.
✗ Branch 1 not taken.
1600 if (!(s->avctx->flags2 & AV_CODEC_FLAG2_NO_OUTPUT)) {
965 1600 flush_put_bits(&s->pb);
966 1600 s->bdsp.bswap_buf((uint32_t *) pkt->data, (uint32_t *) pkt->data, size);
967 }
968
969 1600 s->picture_number++;
970
971 1600 pkt->size = size * 4;
972 1600 *got_packet = 1;
973
974 1600 return 0;
975 }
976
977 32 static av_cold int encode_end(AVCodecContext *avctx)
978 {
979 32 HYuvEncContext *s = avctx->priv_data;
980
981 32 av_freep(&avctx->stats_out);
982
983
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 32 times.
128 for (int i = 0; i < 3; i++)
984 96 av_freep(&s->temp[i]);
985
986 32 return 0;
987 }
988
989 #define OFFSET(x) offsetof(HYuvEncContext, x)
990 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
991
992 static const AVOption options[] = {
993 /* ffvhuff-only options */
994 { "context", "Set per-frame huffman tables", OFFSET(context), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
995 /* Common options */
996 { "non_deterministic", "Allow multithreading for e.g. context=1 at the expense of determinism",
997 OFFSET(non_determ), AV_OPT_TYPE_BOOL, { .i64 = 0 },
998 0, 1, VE },
999 { "pred", "Prediction method", OFFSET(predictor), AV_OPT_TYPE_INT, { .i64 = LEFT }, LEFT, MEDIAN, VE, .unit = "pred" },
1000 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LEFT }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1001 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PLANE }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1002 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MEDIAN }, INT_MIN, INT_MAX, VE, .unit = "pred" },
1003 { NULL },
1004 };
1005
1006 static const AVClass normal_class = {
1007 .class_name = "huffyuv",
1008 .item_name = av_default_item_name,
1009 .option = options + 1,
1010 .version = LIBAVUTIL_VERSION_INT,
1011 };
1012
1013 const FFCodec ff_huffyuv_encoder = {
1014 .p.name = "huffyuv",
1015 CODEC_LONG_NAME("Huffyuv / HuffYUV"),
1016 .p.type = AVMEDIA_TYPE_VIDEO,
1017 .p.id = AV_CODEC_ID_HUFFYUV,
1018 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1019 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1020 .priv_data_size = sizeof(HYuvEncContext),
1021 .init = encode_init,
1022 FF_CODEC_ENCODE_CB(encode_frame),
1023 .close = encode_end,
1024 .p.priv_class = &normal_class,
1025 .p.pix_fmts = (const enum AVPixelFormat[]){
1026 AV_PIX_FMT_YUV422P, AV_PIX_FMT_RGB24,
1027 AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
1028 },
1029 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1030 };
1031
1032 #if CONFIG_FFVHUFF_ENCODER
1033 static const AVClass ff_class = {
1034 .class_name = "ffvhuff",
1035 .item_name = av_default_item_name,
1036 .option = options,
1037 .version = LIBAVUTIL_VERSION_INT,
1038 };
1039
1040 const FFCodec ff_ffvhuff_encoder = {
1041 .p.name = "ffvhuff",
1042 CODEC_LONG_NAME("Huffyuv FFmpeg variant"),
1043 .p.type = AVMEDIA_TYPE_VIDEO,
1044 .p.id = AV_CODEC_ID_FFVHUFF,
1045 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1046 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
1047 .priv_data_size = sizeof(HYuvEncContext),
1048 .init = encode_init,
1049 FF_CODEC_ENCODE_CB(encode_frame),
1050 .close = encode_end,
1051 .p.priv_class = &ff_class,
1052 .p.pix_fmts = (const enum AVPixelFormat[]){
1053 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV411P,
1054 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
1055 AV_PIX_FMT_GBRP,
1056 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
1057 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
1058 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
1059 AV_PIX_FMT_GBRAP,
1060 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV420P16,
1061 AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV422P16,
1062 AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16,
1063 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
1064 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P16,
1065 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P16,
1066 AV_PIX_FMT_RGB24,
1067 AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
1068 },
1069 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1070 };
1071 #endif
1072