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