FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/utvideoenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 245 291 84.2%
Functions: 11 11 100.0%
Branches: 101 131 77.1%

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