FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/huffyuvenc.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 333 546 61.0%
Branches: 176 369 47.7%

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