3 * Copyright (c) 2012 Jan Ekström
5 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/imgutils.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/opt.h"
33 #include "codec_internal.h"
36 #include "bytestream.h"
37 #include "lossless_videoencdsp.h"
42 typedef struct UtvideoContext
{
45 LLVidEncDSPContext llvidencdsp
;
47 uint32_t frame_info_size
, flags
;
53 ptrdiff_t slice_stride
;
54 uint8_t *slice_bits
, *slice_buffer
[4];
58 typedef struct HuffEntry
{
64 /* Compare huffman tree nodes */
65 static int ut_huff_cmp_len(const void *a
, const void *b
)
67 const HuffEntry
*aa
= a
, *bb
= b
;
68 return (aa
->len
- bb
->len
)*256 + aa
->sym
- bb
->sym
;
71 /* Compare huffentry symbols */
72 static int huff_cmp_sym(const void *a
, const void *b
)
74 const HuffEntry
*aa
= a
, *bb
= b
;
75 return aa
->sym
- bb
->sym
;
78 static av_cold
int utvideo_encode_close(AVCodecContext
*avctx
)
80 UtvideoContext
*c
= avctx
->priv_data
;
83 av_freep(&c
->slice_bits
);
84 for (i
= 0; i
< 4; i
++)
85 av_freep(&c
->slice_buffer
[i
]);
90 static av_cold
int utvideo_encode_init(AVCodecContext
*avctx
)
92 UtvideoContext
*c
= avctx
->priv_data
;
93 int i
, subsampled_height
;
94 uint32_t original_format
;
96 c
->frame_info_size
= 4;
97 c
->slice_stride
= FFALIGN(avctx
->width
, 32);
99 switch (avctx
->pix_fmt
) {
100 case AV_PIX_FMT_GBRP
:
102 avctx
->codec_tag
= MKTAG('U', 'L', 'R', 'G');
103 original_format
= UTVIDEO_RGB
;
105 case AV_PIX_FMT_GBRAP
:
107 avctx
->codec_tag
= MKTAG('U', 'L', 'R', 'A');
108 original_format
= UTVIDEO_RGBA
;
109 avctx
->bits_per_coded_sample
= 32;
111 case AV_PIX_FMT_YUV420P
:
112 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
;
118 if (avctx
->colorspace
== AVCOL_SPC_BT709
)
119 avctx
->codec_tag
= MKTAG('U', 'L', 'H', '0');
121 avctx
->codec_tag
= MKTAG('U', 'L', 'Y', '0');
122 original_format
= UTVIDEO_420
;
124 case AV_PIX_FMT_YUV422P
:
125 if (avctx
->width
& 1) {
126 av_log(avctx
, AV_LOG_ERROR
,
127 "4:2:2 video requires even width.\n");
128 return AVERROR_INVALIDDATA
;
131 if (avctx
->colorspace
== AVCOL_SPC_BT709
)
132 avctx
->codec_tag
= MKTAG('U', 'L', 'H', '2');
134 avctx
->codec_tag
= MKTAG('U', 'L', 'Y', '2');
135 original_format
= UTVIDEO_422
;
137 case AV_PIX_FMT_YUV444P
:
139 if (avctx
->colorspace
== AVCOL_SPC_BT709
)
140 avctx
->codec_tag
= MKTAG('U', 'L', 'H', '4');
142 avctx
->codec_tag
= MKTAG('U', 'L', 'Y', '4');
143 original_format
= UTVIDEO_444
;
146 av_log(avctx
, AV_LOG_ERROR
, "Unknown pixel format: %d\n",
148 return AVERROR_INVALIDDATA
;
151 ff_bswapdsp_init(&c
->bdsp
);
152 ff_llvidencdsp_init(&c
->llvidencdsp
);
154 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
;
160 * Check the asked slice count for obviously invalid
161 * values (> 256 or negative).
163 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",
167 return AVERROR(EINVAL
);
170 /* Check that the slice count is not larger than the subsampled height */
171 subsampled_height
= avctx
->height
>> av_pix_fmt_desc_get(avctx
->pix_fmt
)->log2_chroma_h
;
172 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
);
179 /* extradata size is 4 * 32 bits */
180 avctx
->extradata_size
= 16;
182 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+
183 AV_INPUT_BUFFER_PADDING_SIZE
);
185 if (!avctx
->extradata
) {
186 av_log(avctx
, AV_LOG_ERROR
, "Could not allocate extradata.\n");
187 return AVERROR(ENOMEM
);
190 for (i
= 0; i
< c
->planes
; i
++) {
191 c
->slice_buffer
[i
] = av_malloc(c
->slice_stride
* (avctx
->height
+ 2) +
192 AV_INPUT_BUFFER_PADDING_SIZE
);
193 if (!c
->slice_buffer
[i
]) {
194 av_log(avctx
, AV_LOG_ERROR
, "Cannot allocate temporary buffer 1.\n");
195 return AVERROR(ENOMEM
);
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.
205 AV_WB32(avctx
->extradata
, MKTAG(1, 0, 0, 0xF0));
208 * Set the "original format"
209 * Not used for anything during decoding.
211 AV_WL32(avctx
->extradata
+ 4, original_format
);
213 /* Write 4 as the 'frame info size' */
214 AV_WL32(avctx
->extradata
+ 8, c
->frame_info_size
);
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.
221 if (!avctx
->slices
) {
222 c
->slices
= subsampled_height
/ 120;
226 else if (c
->slices
> 256)
229 c
->slices
= avctx
->slices
;
232 /* Set compression mode */
233 c
->compression
= COMP_HUFF
;
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.
242 c
->flags
= (c
->slices
- 1U) << 24;
243 c
->flags
|= 0 << 11; // bit field to signal interlaced encoding mode
244 c
->flags
|= c
->compression
;
246 AV_WL32(avctx
->extradata
+ 12, c
->flags
);
251 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
)
256 int k
= 2 * dst_stride
;
257 const uint8_t *sg
= src
[0];
258 const uint8_t *sb
= src
[1];
259 const uint8_t *sr
= src
[2];
260 const uint8_t *sa
= src
[3];
263 for (j
= 0; j
< height
; j
++) {
265 for (i
= 0; i
< width
; i
++) {
269 dst
[1][k
] = sb
[i
] - g
;
270 dst
[2][k
] = sr
[i
] - g
;
274 for (i
= 0; i
< width
; i
++) {
278 dst
[1][k
] = sb
[i
] - g
;
279 dst
[2][k
] = sr
[i
] - g
;
285 k
+= dst_stride
- width
;
295 /* Write data to a plane with median prediction */
296 static void median_predict(UtvideoContext
*c
, const uint8_t *src
, uint8_t *dst
,
297 ptrdiff_t stride
, int width
, int height
)
303 /* First line uses left neighbour prediction */
304 prev
= 0x80; /* Set the initial value */
305 for (i
= 0; i
< width
; i
++) {
306 *dst
++ = src
[i
] - prev
;
316 * Second line uses top prediction for the first sample,
317 * and median for the rest.
321 /* Rest of the coded part uses median prediction */
322 for (j
= 1; j
< height
; j
++) {
323 c
->llvidencdsp
.sub_median_pred(dst
, src
- stride
, src
, width
, &A
, &B
);
329 /* Count the usage of values in a plane */
330 static void count_usage(uint8_t *src
, int width
,
331 int height
, uint64_t *counts
)
335 for (j
= 0; j
< height
; j
++) {
336 for (i
= 0; i
< width
; i
++) {
343 /* Calculate the actual huffman codes from the code lengths */
344 static void calculate_codes(HuffEntry
*he
)
349 qsort(he
, 256, sizeof(*he
), ut_huff_cmp_len
);
352 while (he
[last
].len
== 255 && last
)
356 for (i
= last
; i
>= 0; i
--) {
357 he
[i
].code
= code
>> (32 - he
[i
].len
);
358 code
+= 0x80000000u
>> (he
[i
].len
- 1);
361 qsort(he
, 256, sizeof(*he
), huff_cmp_sym
);
364 /* Write huffman bit codes to a memory block */
365 static int write_huff_codes(uint8_t *src
, uint8_t *dst
, int dst_size
,
366 int width
, int height
, HuffEntry
*he
)
372 init_put_bits(&pb
, dst
, dst_size
);
374 /* Write the codes */
375 for (j
= 0; j
< height
; j
++) {
376 for (i
= 0; i
< width
; i
++)
377 put_bits(&pb
, he
[src
[i
]].len
, he
[src
[i
]].code
);
382 /* Pad output to a 32-bit boundary */
383 count
= put_bits_count(&pb
) & 0x1F;
386 put_bits(&pb
, 32 - count
, 0);
388 /* Flush the rest with zeroes */
391 /* Return the amount of bytes written */
392 return put_bytes_output(&pb
);
395 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
)
399 UtvideoContext
*c
= avctx
->priv_data
;
400 uint8_t lengths
[256];
401 uint64_t counts
[256] = { 0 };
405 uint32_t offset
= 0, slice_len
= 0;
406 const int cmask
= ~(!plane_no
&& avctx
->pix_fmt
== AV_PIX_FMT_YUV420P
);
407 int i
, sstart
, send
= 0;
411 /* Do prediction / make planes */
412 switch (c
->frame_pred
) {
414 for (i
= 0; i
< c
->slices
; i
++) {
416 send
= height
* (i
+ 1) / c
->slices
& cmask
;
417 av_image_copy_plane(dst
+ sstart
* width
, width
,
418 src
+ sstart
* stride
, stride
,
419 width
, send
- sstart
);
423 for (i
= 0; i
< c
->slices
; i
++) {
425 send
= height
* (i
+ 1) / c
->slices
& cmask
;
426 c
->llvidencdsp
.sub_left_predict(dst
+ sstart
* width
, src
+ sstart
* stride
, stride
, width
, send
- sstart
);
430 for (i
= 0; i
< c
->slices
; i
++) {
432 send
= height
* (i
+ 1) / c
->slices
& cmask
;
433 median_predict(c
, src
+ sstart
* stride
, dst
+ sstart
* width
,
434 stride
, width
, send
- sstart
);
438 av_log(avctx
, AV_LOG_ERROR
, "Unknown prediction mode: %d\n",
440 return AVERROR_OPTION_NOT_FOUND
;
443 /* Count the usage of values */
444 count_usage(dst
, width
, height
, counts
);
446 /* Check for a special case where only one symbol was used */
447 for (symbol
= 0; symbol
< 256; symbol
++) {
448 /* If non-zero count is found, see if it matches width * height */
449 if (counts
[symbol
]) {
450 /* Special case if only one symbol was used */
451 if (counts
[symbol
] == width
* (int64_t)height
) {
453 * Write a zero for the single symbol
454 * used in the plane, else 0xFF.
456 for (i
= 0; i
< 256; i
++) {
458 bytestream2_put_byte(pb
, 0);
460 bytestream2_put_byte(pb
, 0xFF);
463 /* Write zeroes for lengths */
464 for (i
= 0; i
< c
->slices
; i
++)
465 bytestream2_put_le32(pb
, 0);
467 /* And that's all for that plane folks */
474 /* Calculate huffman lengths */
475 if ((ret
= ff_huff_gen_len_table(lengths
, counts
, 256, 1)) < 0)
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)
483 for (i
= 0; i
< 256; i
++) {
484 bytestream2_put_byte(pb
, lengths
[i
]);
486 he
[i
].len
= lengths
[i
];
490 /* Calculate the huffman codes themselves */
494 for (i
= 0; i
< c
->slices
; i
++) {
496 send
= height
* (i
+ 1) / c
->slices
& cmask
;
499 * Write the huffman codes to a buffer,
500 * get the offset in bytes.
502 offset
+= write_huff_codes(dst
+ sstart
* width
, c
->slice_bits
,
503 width
* height
+ 4, width
,
506 slice_len
= offset
- slice_len
;
508 /* Byteswap the written huffman codes */
509 c
->bdsp
.bswap_buf((uint32_t *) c
->slice_bits
,
510 (uint32_t *) c
->slice_bits
,
513 /* Write the offset to the stream */
514 bytestream2_put_le32(pb
, offset
);
516 /* Seek to the data part of the packet */
517 bytestream2_seek_p(pb
, 4 * (c
->slices
- i
- 1) +
518 offset
- slice_len
, SEEK_CUR
);
520 /* Write the slices' data into the output packet */
521 bytestream2_put_buffer(pb
, c
->slice_bits
, slice_len
);
523 /* Seek back to the slice offsets */
524 bytestream2_seek_p(pb
, -4 * (c
->slices
- i
- 1) - offset
,
530 /* And at the end seek to the end of written slice(s) */
531 bytestream2_seek_p(pb
, offset
, SEEK_CUR
);
536 static int utvideo_encode_frame(AVCodecContext
*avctx
, AVPacket
*pkt
,
537 const AVFrame
*pic
, int *got_packet
)
539 UtvideoContext
*c
= avctx
->priv_data
;
546 int width
= avctx
->width
, height
= avctx
->height
;
549 /* Allocate a new packet if needed, and set it to the pointer dst */
550 ret
= ff_alloc_packet(avctx
, pkt
, (256 + 4 * c
->slices
+ width
* height
)
558 bytestream2_init_writer(&pb
, dst
, pkt
->size
);
560 av_fast_padded_malloc(&c
->slice_bits
, &c
->slice_bits_size
, width
* height
+ 4);
562 if (!c
->slice_bits
) {
563 av_log(avctx
, AV_LOG_ERROR
, "Cannot allocate temporary buffer 2.\n");
564 return AVERROR(ENOMEM
);
567 /* In case of RGB, mangle the planes to Ut Video's format */
568 if (avctx
->pix_fmt
== AV_PIX_FMT_GBRAP
|| avctx
->pix_fmt
== AV_PIX_FMT_GBRP
)
569 mangle_rgb_planes(c
->slice_buffer
, c
->slice_stride
, pic
->data
,
570 c
->planes
, pic
->linesize
, width
, height
);
572 /* Deal with the planes */
573 switch (avctx
->pix_fmt
) {
574 case AV_PIX_FMT_GBRP
:
575 case AV_PIX_FMT_GBRAP
:
576 for (i
= 0; i
< c
->planes
; i
++) {
577 ret
= encode_plane(avctx
, c
->slice_buffer
[i
] + 2 * c
->slice_stride
,
578 c
->slice_buffer
[i
], c
->slice_stride
, i
,
582 av_log(avctx
, AV_LOG_ERROR
, "Error encoding plane %d.\n", i
);
587 case AV_PIX_FMT_YUV444P
:
588 for (i
= 0; i
< c
->planes
; i
++) {
589 ret
= encode_plane(avctx
, pic
->data
[i
], c
->slice_buffer
[0],
590 pic
->linesize
[i
], i
, width
, height
, &pb
);
593 av_log(avctx
, AV_LOG_ERROR
, "Error encoding plane %d.\n", i
);
598 case AV_PIX_FMT_YUV422P
:
599 for (i
= 0; i
< c
->planes
; i
++) {
600 ret
= encode_plane(avctx
, pic
->data
[i
], c
->slice_buffer
[0],
601 pic
->linesize
[i
], i
, width
>> !!i
, height
, &pb
);
604 av_log(avctx
, AV_LOG_ERROR
, "Error encoding plane %d.\n", i
);
609 case AV_PIX_FMT_YUV420P
:
610 for (i
= 0; i
< c
->planes
; i
++) {
611 ret
= encode_plane(avctx
, pic
->data
[i
], c
->slice_buffer
[0],
612 pic
->linesize
[i
], i
, width
>> !!i
, height
>> !!i
,
616 av_log(avctx
, AV_LOG_ERROR
, "Error encoding plane %d.\n", i
);
622 av_log(avctx
, AV_LOG_ERROR
, "Unknown pixel format: %d\n",
624 return AVERROR_INVALIDDATA
;
628 * Write frame information (LE 32-bit unsigned)
629 * into the output packet.
630 * Contains the prediction method.
632 frame_info
= c
->frame_pred
<< 8;
633 bytestream2_put_le32(&pb
, frame_info
);
635 pkt
->size
= bytestream2_tell_p(&pb
);
637 /* Packet should be done */
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" },
655 static const AVClass utvideo_class
= {
656 .class_name
= "utvideo",
657 .item_name
= av_default_item_name
,
659 .version
= LIBAVUTIL_VERSION_INT
,
662 const FFCodec ff_utvideo_encoder
= {
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
678 .color_ranges
= AVCOL_RANGE_MPEG
,
679 .caps_internal
= FF_CODEC_CAP_INIT_CLEANUP
,