Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Ut Video encoder | ||
3 | * Copyright (c) 2012 Jan Ekström | ||
4 | * | ||
5 | * This file is part of FFmpeg. | ||
6 | * | ||
7 | * FFmpeg is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * FFmpeg is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with FFmpeg; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | /** | ||
23 | * @file | ||
24 | * Ut Video encoder | ||
25 | */ | ||
26 | |||
27 | #include "libavutil/avassert.h" | ||
28 | #include "libavutil/imgutils.h" | ||
29 | #include "libavutil/intreadwrite.h" | ||
30 | #include "libavutil/mem.h" | ||
31 | #include "libavutil/opt.h" | ||
32 | |||
33 | #include "avcodec.h" | ||
34 | #include "codec_internal.h" | ||
35 | #include "encode.h" | ||
36 | #include "bswapdsp.h" | ||
37 | #include "bytestream.h" | ||
38 | #include "lossless_videoencdsp.h" | ||
39 | #include "put_bits.h" | ||
40 | #include "utvideo.h" | ||
41 | #include "huffman.h" | ||
42 | |||
43 | typedef struct UtvideoContext { | ||
44 | const AVClass *class; | ||
45 | BswapDSPContext bdsp; | ||
46 | LLVidEncDSPContext llvidencdsp; | ||
47 | |||
48 | uint32_t frame_info_size, flags; | ||
49 | int planes; | ||
50 | int slices; | ||
51 | int compression; | ||
52 | int frame_pred; | ||
53 | |||
54 | ptrdiff_t slice_stride; | ||
55 | uint8_t *slice_bits, *slice_buffer[4]; | ||
56 | int slice_bits_size; | ||
57 | } UtvideoContext; | ||
58 | |||
59 | typedef struct HuffEntry { | ||
60 | uint16_t sym; | ||
61 | uint8_t len; | ||
62 | uint32_t code; | ||
63 | } HuffEntry; | ||
64 | |||
65 | /* Compare huffman tree nodes */ | ||
66 | 3203178 | static int ut_huff_cmp_len(const void *a, const void *b) | |
67 | { | ||
68 | 3203178 | const HuffEntry *aa = a, *bb = b; | |
69 | 3203178 | return (aa->len - bb->len)*256 + aa->sym - bb->sym; | |
70 | } | ||
71 | |||
72 | /* Compare huffentry symbols */ | ||
73 | 3158830 | static int huff_cmp_sym(const void *a, const void *b) | |
74 | { | ||
75 | 3158830 | const HuffEntry *aa = a, *bb = b; | |
76 | 3158830 | return aa->sym - bb->sym; | |
77 | } | ||
78 | |||
79 | 136 | static av_cold int utvideo_encode_close(AVCodecContext *avctx) | |
80 | { | ||
81 | 136 | UtvideoContext *c = avctx->priv_data; | |
82 | int i; | ||
83 | |||
84 | 136 | av_freep(&c->slice_bits); | |
85 |
2/2✓ Branch 0 taken 544 times.
✓ Branch 1 taken 136 times.
|
680 | for (i = 0; i < 4; i++) |
86 | 544 | av_freep(&c->slice_buffer[i]); | |
87 | |||
88 | 136 | return 0; | |
89 | } | ||
90 | |||
91 | 136 | static av_cold int utvideo_encode_init(AVCodecContext *avctx) | |
92 | { | ||
93 | 136 | UtvideoContext *c = avctx->priv_data; | |
94 | int i, subsampled_height; | ||
95 | uint32_t original_format; | ||
96 | |||
97 | 136 | c->frame_info_size = 4; | |
98 | 136 | c->slice_stride = FFALIGN(avctx->width, 32); | |
99 | |||
100 |
5/6✓ Branch 0 taken 27 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 27 times.
✗ Branch 5 not taken.
|
136 | switch (avctx->pix_fmt) { |
101 | 27 | case AV_PIX_FMT_GBRP: | |
102 | 27 | c->planes = 3; | |
103 | 27 | avctx->codec_tag = MKTAG('U', 'L', 'R', 'G'); | |
104 | 27 | original_format = UTVIDEO_RGB; | |
105 | 27 | break; | |
106 | 27 | case AV_PIX_FMT_GBRAP: | |
107 | 27 | c->planes = 4; | |
108 | 27 | avctx->codec_tag = MKTAG('U', 'L', 'R', 'A'); | |
109 | 27 | original_format = UTVIDEO_RGBA; | |
110 | 27 | avctx->bits_per_coded_sample = 32; | |
111 | 27 | break; | |
112 | 28 | case AV_PIX_FMT_YUV420P: | |
113 |
2/4✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
|
28 | if (avctx->width & 1 || avctx->height & 1) { |
114 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
115 | "4:2:0 video requires even width and height.\n"); | ||
116 | ✗ | return AVERROR_INVALIDDATA; | |
117 | } | ||
118 | 28 | c->planes = 3; | |
119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (avctx->colorspace == AVCOL_SPC_BT709) |
120 | ✗ | avctx->codec_tag = MKTAG('U', 'L', 'H', '0'); | |
121 | else | ||
122 | 28 | avctx->codec_tag = MKTAG('U', 'L', 'Y', '0'); | |
123 | 28 | original_format = UTVIDEO_420; | |
124 | 28 | break; | |
125 | 27 | case AV_PIX_FMT_YUV422P: | |
126 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (avctx->width & 1) { |
127 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
128 | "4:2:2 video requires even width.\n"); | ||
129 | ✗ | return AVERROR_INVALIDDATA; | |
130 | } | ||
131 | 27 | c->planes = 3; | |
132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (avctx->colorspace == AVCOL_SPC_BT709) |
133 | ✗ | avctx->codec_tag = MKTAG('U', 'L', 'H', '2'); | |
134 | else | ||
135 | 27 | avctx->codec_tag = MKTAG('U', 'L', 'Y', '2'); | |
136 | 27 | original_format = UTVIDEO_422; | |
137 | 27 | break; | |
138 | 27 | case AV_PIX_FMT_YUV444P: | |
139 | 27 | c->planes = 3; | |
140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
|
27 | if (avctx->colorspace == AVCOL_SPC_BT709) |
141 | ✗ | avctx->codec_tag = MKTAG('U', 'L', 'H', '4'); | |
142 | else | ||
143 | 27 | avctx->codec_tag = MKTAG('U', 'L', 'Y', '4'); | |
144 | 27 | original_format = UTVIDEO_444; | |
145 | 27 | break; | |
146 | ✗ | default: | |
147 | ✗ | av_unreachable("Already checked via CODEC_PIXFMTS"); | |
148 | } | ||
149 | |||
150 | 136 | ff_bswapdsp_init(&c->bdsp); | |
151 | 136 | ff_llvidencdsp_init(&c->llvidencdsp); | |
152 | |||
153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (c->frame_pred == PRED_GRADIENT) { |
154 | ✗ | av_log(avctx, AV_LOG_ERROR, "Gradient prediction is not supported.\n"); | |
155 | ✗ | return AVERROR_PATCHWELCOME; | |
156 | } | ||
157 | |||
158 | /* | ||
159 | * Check the asked slice count for obviously invalid | ||
160 | * values (> 256 or negative). | ||
161 | */ | ||
162 |
2/4✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 136 times.
|
136 | if (avctx->slices > 256 || avctx->slices < 0) { |
163 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
164 | "Slice count %d is not supported in Ut Video (theoretical range is 0-256).\n", | ||
165 | avctx->slices); | ||
166 | ✗ | return AVERROR(EINVAL); | |
167 | } | ||
168 | |||
169 | /* Check that the slice count is not larger than the subsampled height */ | ||
170 | 136 | subsampled_height = avctx->height >> av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_h; | |
171 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (avctx->slices > subsampled_height) { |
172 | ✗ | av_log(avctx, AV_LOG_ERROR, | |
173 | "Slice count %d is larger than the subsampling-applied height %d.\n", | ||
174 | avctx->slices, subsampled_height); | ||
175 | ✗ | return AVERROR(EINVAL); | |
176 | } | ||
177 | |||
178 | /* extradata size is 4 * 32 bits */ | ||
179 | 136 | avctx->extradata_size = 16; | |
180 | |||
181 | 136 | avctx->extradata = av_mallocz(avctx->extradata_size + | |
182 | AV_INPUT_BUFFER_PADDING_SIZE); | ||
183 | |||
184 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (!avctx->extradata) { |
185 | ✗ | av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata.\n"); | |
186 | ✗ | return AVERROR(ENOMEM); | |
187 | } | ||
188 | |||
189 |
2/2✓ Branch 0 taken 435 times.
✓ Branch 1 taken 136 times.
|
571 | for (i = 0; i < c->planes; i++) { |
190 | 435 | c->slice_buffer[i] = av_malloc(c->slice_stride * (avctx->height + 2) + | |
191 | AV_INPUT_BUFFER_PADDING_SIZE); | ||
192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 435 times.
|
435 | if (!c->slice_buffer[i]) { |
193 | ✗ | av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 1.\n"); | |
194 | ✗ | return AVERROR(ENOMEM); | |
195 | } | ||
196 | } | ||
197 | |||
198 | /* | ||
199 | * Set the version of the encoder. | ||
200 | * Last byte is "implementation ID", which is | ||
201 | * obtained from the creator of the format. | ||
202 | * Libavcodec has been assigned with the ID 0xF0. | ||
203 | */ | ||
204 | 136 | AV_WB32(avctx->extradata, MKTAG(1, 0, 0, 0xF0)); | |
205 | |||
206 | /* | ||
207 | * Set the "original format" | ||
208 | * Not used for anything during decoding. | ||
209 | */ | ||
210 | 136 | AV_WL32(avctx->extradata + 4, original_format); | |
211 | |||
212 | /* Write 4 as the 'frame info size' */ | ||
213 | 136 | AV_WL32(avctx->extradata + 8, c->frame_info_size); | |
214 | |||
215 | /* | ||
216 | * Set how many slices are going to be used. | ||
217 | * By default uses multiple slices depending on the subsampled height. | ||
218 | * This enables multithreading in the official decoder. | ||
219 | */ | ||
220 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 135 times.
|
136 | if (!avctx->slices) { |
221 | 1 | c->slices = subsampled_height / 120; | |
222 | |||
223 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!c->slices) |
224 | ✗ | c->slices = 1; | |
225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | else if (c->slices > 256) |
226 | ✗ | c->slices = 256; | |
227 | } else { | ||
228 | 135 | c->slices = avctx->slices; | |
229 | } | ||
230 | |||
231 | /* Set compression mode */ | ||
232 | 136 | c->compression = COMP_HUFF; | |
233 | |||
234 | /* | ||
235 | * Set the encoding flags: | ||
236 | * - Slice count minus 1 | ||
237 | * - Interlaced encoding mode flag, set to zero for now. | ||
238 | * - Compression mode (none/huff) | ||
239 | * And write the flags. | ||
240 | */ | ||
241 | 136 | c->flags = (c->slices - 1U) << 24; | |
242 | 136 | c->flags |= 0 << 11; // bit field to signal interlaced encoding mode | |
243 | 136 | c->flags |= c->compression; | |
244 | |||
245 | 136 | AV_WL32(avctx->extradata + 12, c->flags); | |
246 | |||
247 | 136 | return 0; | |
248 | } | ||
249 | |||
250 | 300 | static void mangle_rgb_planes(uint8_t *dst[4], ptrdiff_t dst_stride, | |
251 | uint8_t *const src[4], int planes, const int stride[4], | ||
252 | int width, int height) | ||
253 | { | ||
254 | int i, j; | ||
255 | 300 | int k = 2 * dst_stride; | |
256 | 300 | const uint8_t *sg = src[0]; | |
257 | 300 | const uint8_t *sb = src[1]; | |
258 | 300 | const uint8_t *sr = src[2]; | |
259 | 300 | const uint8_t *sa = src[3]; | |
260 | unsigned int g; | ||
261 | |||
262 |
2/2✓ Branch 0 taken 86400 times.
✓ Branch 1 taken 300 times.
|
86700 | for (j = 0; j < height; j++) { |
263 |
2/2✓ Branch 0 taken 43200 times.
✓ Branch 1 taken 43200 times.
|
86400 | if (planes == 3) { |
264 |
2/2✓ Branch 0 taken 15206400 times.
✓ Branch 1 taken 43200 times.
|
15249600 | for (i = 0; i < width; i++) { |
265 | 15206400 | g = sg[i]; | |
266 | 15206400 | dst[0][k] = g; | |
267 | 15206400 | g += 0x80; | |
268 | 15206400 | dst[1][k] = sb[i] - g; | |
269 | 15206400 | dst[2][k] = sr[i] - g; | |
270 | 15206400 | k++; | |
271 | } | ||
272 | } else { | ||
273 |
2/2✓ Branch 0 taken 15206400 times.
✓ Branch 1 taken 43200 times.
|
15249600 | for (i = 0; i < width; i++) { |
274 | 15206400 | g = sg[i]; | |
275 | 15206400 | dst[0][k] = g; | |
276 | 15206400 | g += 0x80; | |
277 | 15206400 | dst[1][k] = sb[i] - g; | |
278 | 15206400 | dst[2][k] = sr[i] - g; | |
279 | 15206400 | dst[3][k] = sa[i]; | |
280 | 15206400 | k++; | |
281 | } | ||
282 | 43200 | sa += stride[3]; | |
283 | } | ||
284 | 86400 | k += dst_stride - width; | |
285 | 86400 | sg += stride[0]; | |
286 | 86400 | sb += stride[1]; | |
287 | 86400 | sr += stride[2]; | |
288 | } | ||
289 | 300 | } | |
290 | |||
291 | #undef A | ||
292 | #undef B | ||
293 | |||
294 | /* Write data to a plane with median prediction */ | ||
295 | 800 | static void median_predict(UtvideoContext *c, const uint8_t *src, uint8_t *dst, | |
296 | ptrdiff_t stride, int width, int height) | ||
297 | { | ||
298 | int i, j; | ||
299 | int A, B; | ||
300 | uint8_t prev; | ||
301 | |||
302 | /* First line uses left neighbour prediction */ | ||
303 | 800 | prev = 0x80; /* Set the initial value */ | |
304 |
2/2✓ Branch 0 taken 246400 times.
✓ Branch 1 taken 800 times.
|
247200 | for (i = 0; i < width; i++) { |
305 | 246400 | *dst++ = src[i] - prev; | |
306 | 246400 | prev = src[i]; | |
307 | } | ||
308 | |||
309 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 800 times.
|
800 | if (height == 1) |
310 | ✗ | return; | |
311 | |||
312 | 800 | src += stride; | |
313 | |||
314 | /* | ||
315 | * Second line uses top prediction for the first sample, | ||
316 | * and median for the rest. | ||
317 | */ | ||
318 | 800 | A = B = 0; | |
319 | |||
320 | /* Rest of the coded part uses median prediction */ | ||
321 |
2/2✓ Branch 0 taken 215200 times.
✓ Branch 1 taken 800 times.
|
216000 | for (j = 1; j < height; j++) { |
322 | 215200 | c->llvidencdsp.sub_median_pred(dst, src - stride, src, width, &A, &B); | |
323 | 215200 | dst += width; | |
324 | 215200 | src += stride; | |
325 | } | ||
326 | } | ||
327 | |||
328 | /* Count the usage of values in a plane */ | ||
329 | 2403 | static void count_usage(uint8_t *src, int width, | |
330 | int height, uint64_t *counts) | ||
331 | { | ||
332 | int i, j; | ||
333 | |||
334 |
2/2✓ Branch 0 taken 649280 times.
✓ Branch 1 taken 2403 times.
|
651683 | for (j = 0; j < height; j++) { |
335 |
2/2✓ Branch 0 taken 205632000 times.
✓ Branch 1 taken 649280 times.
|
206281280 | for (i = 0; i < width; i++) { |
336 | 205632000 | counts[src[i]]++; | |
337 | } | ||
338 | 649280 | src += width; | |
339 | } | ||
340 | 2403 | } | |
341 | |||
342 | /* Calculate the actual huffman codes from the code lengths */ | ||
343 | 2353 | static void calculate_codes(HuffEntry *he) | |
344 | { | ||
345 | int last, i; | ||
346 | uint32_t code; | ||
347 | |||
348 | 2353 | qsort(he, 256, sizeof(*he), ut_huff_cmp_len); | |
349 | |||
350 | 2353 | last = 255; | |
351 |
3/4✓ Branch 0 taken 84131 times.
✓ Branch 1 taken 2353 times.
✓ Branch 2 taken 84131 times.
✗ Branch 3 not taken.
|
86484 | while (he[last].len == 255 && last) |
352 | 84131 | last--; | |
353 | |||
354 | 2353 | code = 0; | |
355 |
2/2✓ Branch 0 taken 518237 times.
✓ Branch 1 taken 2353 times.
|
520590 | for (i = last; i >= 0; i--) { |
356 | 518237 | he[i].code = code >> (32 - he[i].len); | |
357 | 518237 | code += 0x80000000u >> (he[i].len - 1); | |
358 | } | ||
359 | |||
360 | 2353 | qsort(he, 256, sizeof(*he), huff_cmp_sym); | |
361 | 2353 | } | |
362 | |||
363 | /* Write huffman bit codes to a memory block */ | ||
364 | 2356 | static int write_huff_codes(uint8_t *src, uint8_t *dst, int dst_size, | |
365 | int width, int height, HuffEntry *he) | ||
366 | { | ||
367 | PutBitContext pb; | ||
368 | int i, j; | ||
369 | int count; | ||
370 | |||
371 | 2356 | init_put_bits(&pb, dst, dst_size); | |
372 | |||
373 | /* Write the codes */ | ||
374 |
2/2✓ Branch 0 taken 634880 times.
✓ Branch 1 taken 2356 times.
|
637236 | for (j = 0; j < height; j++) { |
375 |
2/2✓ Branch 0 taken 200563200 times.
✓ Branch 1 taken 634880 times.
|
201198080 | for (i = 0; i < width; i++) |
376 | 200563200 | put_bits(&pb, he[src[i]].len, he[src[i]].code); | |
377 | |||
378 | 634880 | src += width; | |
379 | } | ||
380 | |||
381 | /* Pad output to a 32-bit boundary */ | ||
382 | 2356 | count = put_bits_count(&pb) & 0x1F; | |
383 | |||
384 |
2/2✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 180 times.
|
2356 | if (count) |
385 | 2176 | put_bits(&pb, 32 - count, 0); | |
386 | |||
387 | /* Flush the rest with zeroes */ | ||
388 | 2356 | flush_put_bits(&pb); | |
389 | |||
390 | /* Return the amount of bytes written */ | ||
391 | 2356 | return put_bytes_output(&pb); | |
392 | } | ||
393 | |||
394 | 2403 | static int encode_plane(AVCodecContext *avctx, const uint8_t *src, | |
395 | uint8_t *dst, ptrdiff_t stride, int plane_no, | ||
396 | int width, int height, PutByteContext *pb) | ||
397 | { | ||
398 | 2403 | UtvideoContext *c = avctx->priv_data; | |
399 | uint8_t lengths[256]; | ||
400 | 2403 | uint64_t counts[256] = { 0 }; | |
401 | |||
402 | HuffEntry he[256]; | ||
403 | |||
404 | 2403 | uint32_t offset = 0, slice_len = 0; | |
405 |
4/4✓ Branch 0 taken 751 times.
✓ Branch 1 taken 1652 times.
✓ Branch 2 taken 151 times.
✓ Branch 3 taken 600 times.
|
2403 | const int cmask = ~(!plane_no && avctx->pix_fmt == AV_PIX_FMT_YUV420P); |
406 | 2403 | int i, sstart, send = 0; | |
407 | int symbol; | ||
408 | int ret; | ||
409 | |||
410 | /* Do prediction / make planes */ | ||
411 |
3/4✓ Branch 0 taken 800 times.
✓ Branch 1 taken 803 times.
✓ Branch 2 taken 800 times.
✗ Branch 3 not taken.
|
2403 | switch (c->frame_pred) { |
412 | 800 | case PRED_NONE: | |
413 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
|
1600 | for (i = 0; i < c->slices; i++) { |
414 | 800 | sstart = send; | |
415 | 800 | send = height * (i + 1) / c->slices & cmask; | |
416 | 800 | av_image_copy_plane(dst + sstart * width, width, | |
417 | 800 | src + sstart * stride, stride, | |
418 | width, send - sstart); | ||
419 | } | ||
420 | 800 | break; | |
421 | 803 | case PRED_LEFT: | |
422 |
2/2✓ Branch 0 taken 806 times.
✓ Branch 1 taken 803 times.
|
1609 | for (i = 0; i < c->slices; i++) { |
423 | 806 | sstart = send; | |
424 | 806 | send = height * (i + 1) / c->slices & cmask; | |
425 | 806 | c->llvidencdsp.sub_left_predict(dst + sstart * width, src + sstart * stride, stride, width, send - sstart); | |
426 | } | ||
427 | 803 | break; | |
428 | 800 | case PRED_MEDIAN: | |
429 |
2/2✓ Branch 0 taken 800 times.
✓ Branch 1 taken 800 times.
|
1600 | for (i = 0; i < c->slices; i++) { |
430 | 800 | sstart = send; | |
431 | 800 | send = height * (i + 1) / c->slices & cmask; | |
432 | 800 | median_predict(c, src + sstart * stride, dst + sstart * width, | |
433 | stride, width, send - sstart); | ||
434 | } | ||
435 | 800 | break; | |
436 | ✗ | default: | |
437 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unknown prediction mode: %d\n", | |
438 | c->frame_pred); | ||
439 | ✗ | return AVERROR_OPTION_NOT_FOUND; | |
440 | } | ||
441 | |||
442 | /* Count the usage of values */ | ||
443 | 2403 | count_usage(dst, width, height, counts); | |
444 | |||
445 | /* Check for a special case where only one symbol was used */ | ||
446 |
1/2✓ Branch 0 taken 16843 times.
✗ Branch 1 not taken.
|
16843 | for (symbol = 0; symbol < 256; symbol++) { |
447 | /* If non-zero count is found, see if it matches width * height */ | ||
448 |
2/2✓ Branch 0 taken 2403 times.
✓ Branch 1 taken 14440 times.
|
16843 | if (counts[symbol]) { |
449 | /* Special case if only one symbol was used */ | ||
450 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 2353 times.
|
2403 | if (counts[symbol] == width * (int64_t)height) { |
451 | /* | ||
452 | * Write a zero for the single symbol | ||
453 | * used in the plane, else 0xFF. | ||
454 | */ | ||
455 |
2/2✓ Branch 0 taken 12800 times.
✓ Branch 1 taken 50 times.
|
12850 | for (i = 0; i < 256; i++) { |
456 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 12750 times.
|
12800 | if (i == symbol) |
457 | 50 | bytestream2_put_byte(pb, 0); | |
458 | else | ||
459 | 12750 | bytestream2_put_byte(pb, 0xFF); | |
460 | } | ||
461 | |||
462 | /* Write zeroes for lengths */ | ||
463 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 50 times.
|
100 | for (i = 0; i < c->slices; i++) |
464 | 50 | bytestream2_put_le32(pb, 0); | |
465 | |||
466 | /* And that's all for that plane folks */ | ||
467 | 50 | return 0; | |
468 | } | ||
469 | 2353 | break; | |
470 | } | ||
471 | } | ||
472 | |||
473 | /* Calculate huffman lengths */ | ||
474 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2353 times.
|
2353 | if ((ret = ff_huff_gen_len_table(lengths, counts, 256, 1)) < 0) |
475 | ✗ | return ret; | |
476 | |||
477 | /* | ||
478 | * Write the plane's header into the output packet: | ||
479 | * - huffman code lengths (256 bytes) | ||
480 | * - slice end offsets (gotten from the slice lengths) | ||
481 | */ | ||
482 |
2/2✓ Branch 0 taken 602368 times.
✓ Branch 1 taken 2353 times.
|
604721 | for (i = 0; i < 256; i++) { |
483 | 602368 | bytestream2_put_byte(pb, lengths[i]); | |
484 | |||
485 | 602368 | he[i].len = lengths[i]; | |
486 | 602368 | he[i].sym = i; | |
487 | } | ||
488 | |||
489 | /* Calculate the huffman codes themselves */ | ||
490 | 2353 | calculate_codes(he); | |
491 | |||
492 | 2353 | send = 0; | |
493 |
2/2✓ Branch 0 taken 2356 times.
✓ Branch 1 taken 2353 times.
|
4709 | for (i = 0; i < c->slices; i++) { |
494 | 2356 | sstart = send; | |
495 | 2356 | send = height * (i + 1) / c->slices & cmask; | |
496 | |||
497 | /* | ||
498 | * Write the huffman codes to a buffer, | ||
499 | * get the offset in bytes. | ||
500 | */ | ||
501 | 4712 | offset += write_huff_codes(dst + sstart * width, c->slice_bits, | |
502 | 2356 | width * height + 4, width, | |
503 | send - sstart, he); | ||
504 | |||
505 | 2356 | slice_len = offset - slice_len; | |
506 | |||
507 | /* Byteswap the written huffman codes */ | ||
508 | 2356 | c->bdsp.bswap_buf((uint32_t *) c->slice_bits, | |
509 | 2356 | (uint32_t *) c->slice_bits, | |
510 | 2356 | slice_len >> 2); | |
511 | |||
512 | /* Write the offset to the stream */ | ||
513 | 2356 | bytestream2_put_le32(pb, offset); | |
514 | |||
515 | /* Seek to the data part of the packet */ | ||
516 | 2356 | bytestream2_seek_p(pb, 4 * (c->slices - i - 1) + | |
517 | 2356 | offset - slice_len, SEEK_CUR); | |
518 | |||
519 | /* Write the slices' data into the output packet */ | ||
520 | 2356 | bytestream2_put_buffer(pb, c->slice_bits, slice_len); | |
521 | |||
522 | /* Seek back to the slice offsets */ | ||
523 | 2356 | bytestream2_seek_p(pb, -4 * (c->slices - i - 1) - offset, | |
524 | SEEK_CUR); | ||
525 | |||
526 | 2356 | slice_len = offset; | |
527 | } | ||
528 | |||
529 | /* And at the end seek to the end of written slice(s) */ | ||
530 | 2353 | bytestream2_seek_p(pb, offset, SEEK_CUR); | |
531 | |||
532 | 2353 | return 0; | |
533 | } | ||
534 | |||
535 | 751 | static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt, | |
536 | const AVFrame *pic, int *got_packet) | ||
537 | { | ||
538 | 751 | UtvideoContext *c = avctx->priv_data; | |
539 | PutByteContext pb; | ||
540 | |||
541 | uint32_t frame_info; | ||
542 | |||
543 | uint8_t *dst; | ||
544 | |||
545 | 751 | int width = avctx->width, height = avctx->height; | |
546 | 751 | int i, ret = 0; | |
547 | |||
548 | /* Allocate a new packet if needed, and set it to the pointer dst */ | ||
549 | 751 | ret = ff_alloc_packet(avctx, pkt, (256 + 4 * c->slices + width * height) | |
550 | 751 | * c->planes + 4); | |
551 | |||
552 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 751 times.
|
751 | if (ret < 0) |
553 | ✗ | return ret; | |
554 | |||
555 | 751 | dst = pkt->data; | |
556 | |||
557 | 751 | bytestream2_init_writer(&pb, dst, pkt->size); | |
558 | |||
559 | 751 | av_fast_padded_malloc(&c->slice_bits, &c->slice_bits_size, width * height + 4); | |
560 | |||
561 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 751 times.
|
751 | if (!c->slice_bits) { |
562 | ✗ | av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 2.\n"); | |
563 | ✗ | return AVERROR(ENOMEM); | |
564 | } | ||
565 | |||
566 | /* In case of RGB, mangle the planes to Ut Video's format */ | ||
567 |
4/4✓ Branch 0 taken 601 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 150 times.
✓ Branch 3 taken 451 times.
|
751 | if (avctx->pix_fmt == AV_PIX_FMT_GBRAP || avctx->pix_fmt == AV_PIX_FMT_GBRP) |
568 | 300 | mangle_rgb_planes(c->slice_buffer, c->slice_stride, pic->data, | |
569 | 300 | c->planes, pic->linesize, width, height); | |
570 | |||
571 | /* Deal with the planes */ | ||
572 |
4/5✓ Branch 0 taken 300 times.
✓ Branch 1 taken 150 times.
✓ Branch 2 taken 150 times.
✓ Branch 3 taken 151 times.
✗ Branch 4 not taken.
|
751 | switch (avctx->pix_fmt) { |
573 | 300 | case AV_PIX_FMT_GBRP: | |
574 | case AV_PIX_FMT_GBRAP: | ||
575 |
2/2✓ Branch 0 taken 1050 times.
✓ Branch 1 taken 300 times.
|
1350 | for (i = 0; i < c->planes; i++) { |
576 | 1050 | ret = encode_plane(avctx, c->slice_buffer[i] + 2 * c->slice_stride, | |
577 | c->slice_buffer[i], c->slice_stride, i, | ||
578 | width, height, &pb); | ||
579 | |||
580 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1050 times.
|
1050 | if (ret) { |
581 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i); | |
582 | ✗ | return ret; | |
583 | } | ||
584 | } | ||
585 | 300 | break; | |
586 | 150 | case AV_PIX_FMT_YUV444P: | |
587 |
2/2✓ Branch 0 taken 450 times.
✓ Branch 1 taken 150 times.
|
600 | for (i = 0; i < c->planes; i++) { |
588 | 450 | ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0], | |
589 | 450 | pic->linesize[i], i, width, height, &pb); | |
590 | |||
591 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
|
450 | if (ret) { |
592 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i); | |
593 | ✗ | return ret; | |
594 | } | ||
595 | } | ||
596 | 150 | break; | |
597 | 150 | case AV_PIX_FMT_YUV422P: | |
598 |
2/2✓ Branch 0 taken 450 times.
✓ Branch 1 taken 150 times.
|
600 | for (i = 0; i < c->planes; i++) { |
599 | 450 | ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0], | |
600 | 450 | pic->linesize[i], i, width >> !!i, height, &pb); | |
601 | |||
602 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 450 times.
|
450 | if (ret) { |
603 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i); | |
604 | ✗ | return ret; | |
605 | } | ||
606 | } | ||
607 | 150 | break; | |
608 | 151 | case AV_PIX_FMT_YUV420P: | |
609 |
2/2✓ Branch 0 taken 453 times.
✓ Branch 1 taken 151 times.
|
604 | for (i = 0; i < c->planes; i++) { |
610 | 453 | ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0], | |
611 | 453 | pic->linesize[i], i, width >> !!i, height >> !!i, | |
612 | &pb); | ||
613 | |||
614 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 453 times.
|
453 | if (ret) { |
615 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i); | |
616 | ✗ | return ret; | |
617 | } | ||
618 | } | ||
619 | 151 | break; | |
620 | ✗ | default: | |
621 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unknown pixel format: %d\n", | |
622 | ✗ | avctx->pix_fmt); | |
623 | ✗ | return AVERROR_INVALIDDATA; | |
624 | } | ||
625 | |||
626 | /* | ||
627 | * Write frame information (LE 32-bit unsigned) | ||
628 | * into the output packet. | ||
629 | * Contains the prediction method. | ||
630 | */ | ||
631 | 751 | frame_info = c->frame_pred << 8; | |
632 | 751 | bytestream2_put_le32(&pb, frame_info); | |
633 | |||
634 | 751 | pkt->size = bytestream2_tell_p(&pb); | |
635 | |||
636 | /* Packet should be done */ | ||
637 | 751 | *got_packet = 1; | |
638 | |||
639 | 751 | return 0; | |
640 | } | ||
641 | |||
642 | #define OFFSET(x) offsetof(UtvideoContext, x) | ||
643 | #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM | ||
644 | static const AVOption options[] = { | ||
645 | { "pred", "Prediction method", OFFSET(frame_pred), AV_OPT_TYPE_INT, { .i64 = PRED_LEFT }, PRED_NONE, PRED_MEDIAN, VE, .unit = "pred" }, | ||
646 | { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRED_NONE }, INT_MIN, INT_MAX, VE, .unit = "pred" }, | ||
647 | { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRED_LEFT }, INT_MIN, INT_MAX, VE, .unit = "pred" }, | ||
648 | { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PRED_MEDIAN }, INT_MIN, INT_MAX, VE, .unit = "pred" }, | ||
649 | |||
650 | { NULL}, | ||
651 | }; | ||
652 | |||
653 | static const AVClass utvideo_class = { | ||
654 | .class_name = "utvideo", | ||
655 | .item_name = av_default_item_name, | ||
656 | .option = options, | ||
657 | .version = LIBAVUTIL_VERSION_INT, | ||
658 | }; | ||
659 | |||
660 | const FFCodec ff_utvideo_encoder = { | ||
661 | .p.name = "utvideo", | ||
662 | CODEC_LONG_NAME("Ut Video"), | ||
663 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
664 | .p.id = AV_CODEC_ID_UTVIDEO, | ||
665 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS | | ||
666 | AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE, | ||
667 | .priv_data_size = sizeof(UtvideoContext), | ||
668 | .p.priv_class = &utvideo_class, | ||
669 | .init = utvideo_encode_init, | ||
670 | FF_CODEC_ENCODE_CB(utvideo_encode_frame), | ||
671 | .close = utvideo_encode_close, | ||
672 | CODEC_PIXFMTS(AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, | ||
673 | AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV444P), | ||
674 | .color_ranges = AVCOL_RANGE_MPEG, | ||
675 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
676 | }; | ||
677 |