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"
32 #include "bytestream.h"
34 #include "huffyuvencdsp.h"
39 /* Compare huffentry symbols */
40 static int huff_cmp_sym(const void *a, const void *b)
42 const HuffEntry *aa = a, *bb = b;
43 return aa->sym - bb->sym;
46 static av_cold int utvideo_encode_close(AVCodecContext *avctx)
48 UtvideoContext *c = avctx->priv_data;
51 av_freep(&avctx->coded_frame);
52 av_freep(&c->slice_bits);
53 for (i = 0; i < 4; i++)
54 av_freep(&c->slice_buffer[i]);
59 static av_cold int utvideo_encode_init(AVCodecContext *avctx)
61 UtvideoContext *c = avctx->priv_data;
62 int i, subsampled_height;
63 uint32_t original_format;
66 c->frame_info_size = 4;
67 c->slice_stride = FFALIGN(avctx->width, 32);
69 switch (avctx->pix_fmt) {
70 case AV_PIX_FMT_RGB24:
72 avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
73 original_format = UTVIDEO_RGB;
77 avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
78 original_format = UTVIDEO_RGBA;
80 case AV_PIX_FMT_YUV420P:
81 if (avctx->width & 1 || avctx->height & 1) {
82 av_log(avctx, AV_LOG_ERROR,
83 "4:2:0 video requires even width and height.\n");
84 return AVERROR_INVALIDDATA;
87 if (avctx->colorspace == AVCOL_SPC_BT709)
88 avctx->codec_tag = MKTAG('U', 'L', 'H', '0');
90 avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
91 original_format = UTVIDEO_420;
93 case AV_PIX_FMT_YUV422P:
94 if (avctx->width & 1) {
95 av_log(avctx, AV_LOG_ERROR,
96 "4:2:2 video requires even width.\n");
97 return AVERROR_INVALIDDATA;
100 if (avctx->colorspace == AVCOL_SPC_BT709)
101 avctx->codec_tag = MKTAG('U', 'L', 'H', '2');
103 avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
104 original_format = UTVIDEO_422;
107 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format: %d\n",
109 return AVERROR_INVALIDDATA;
112 ff_bswapdsp_init(&c->bdsp);
113 ff_huffyuvencdsp_init(&c->hdsp);
115 /* Check the prediction method, and error out if unsupported */
116 if (avctx->prediction_method < 0 || avctx->prediction_method > 4) {
117 av_log(avctx, AV_LOG_WARNING,
118 "Prediction method %d is not supported in Ut Video.\n",
119 avctx->prediction_method);
120 return AVERROR_OPTION_NOT_FOUND;
123 if (avctx->prediction_method == FF_PRED_PLANE) {
124 av_log(avctx, AV_LOG_ERROR,
125 "Plane prediction is not supported in Ut Video.\n");
126 return AVERROR_OPTION_NOT_FOUND;
129 /* Convert from libavcodec prediction type to Ut Video's */
130 c->frame_pred = ff_ut_pred_order[avctx->prediction_method];
132 if (c->frame_pred == PRED_GRADIENT) {
133 av_log(avctx, AV_LOG_ERROR, "Gradient prediction is not supported.\n");
134 return AVERROR_OPTION_NOT_FOUND;
138 * Check the asked slice count for obviously invalid
139 * values (> 256 or negative).
141 if (avctx->slices > 256 || avctx->slices < 0) {
142 av_log(avctx, AV_LOG_ERROR,
143 "Slice count %d is not supported in Ut Video (theoretical range is 0-256).\n",
145 return AVERROR(EINVAL);
148 /* Check that the slice count is not larger than the subsampled height */
149 subsampled_height = avctx->height >> av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_h;
150 if (avctx->slices > subsampled_height) {
151 av_log(avctx, AV_LOG_ERROR,
152 "Slice count %d is larger than the subsampling-applied height %d.\n",
153 avctx->slices, subsampled_height);
154 return AVERROR(EINVAL);
157 avctx->coded_frame = av_frame_alloc();
159 if (!avctx->coded_frame) {
160 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
161 utvideo_encode_close(avctx);
162 return AVERROR(ENOMEM);
165 /* extradata size is 4 * 32bit */
166 avctx->extradata_size = 16;
168 avctx->extradata = av_mallocz(avctx->extradata_size +
169 FF_INPUT_BUFFER_PADDING_SIZE);
171 if (!avctx->extradata) {
172 av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata.\n");
173 utvideo_encode_close(avctx);
174 return AVERROR(ENOMEM);
177 for (i = 0; i < c->planes; i++) {
178 c->slice_buffer[i] = av_malloc(c->slice_stride * (avctx->height + 2) +
179 FF_INPUT_BUFFER_PADDING_SIZE);
180 if (!c->slice_buffer[i]) {
181 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 1.\n");
182 utvideo_encode_close(avctx);
183 return AVERROR(ENOMEM);
188 * Set the version of the encoder.
189 * Last byte is "implementation ID", which is
190 * obtained from the creator of the format.
191 * Libavcodec has been assigned with the ID 0xF0.
193 AV_WB32(avctx->extradata, MKTAG(1, 0, 0, 0xF0));
196 * Set the "original format"
197 * Not used for anything during decoding.
199 AV_WL32(avctx->extradata + 4, original_format);
201 /* Write 4 as the 'frame info size' */
202 AV_WL32(avctx->extradata + 8, c->frame_info_size);
205 * Set how many slices are going to be used.
206 * By default uses multiple slices depending on the subsampled height.
207 * This enables multithreading in the official decoder.
209 if (!avctx->slices) {
210 c->slices = subsampled_height / 120;
214 else if (c->slices > 256)
217 c->slices = avctx->slices;
220 /* Set compression mode */
221 c->compression = COMP_HUFF;
224 * Set the encoding flags:
225 * - Slice count minus 1
226 * - Interlaced encoding mode flag, set to zero for now.
227 * - Compression mode (none/huff)
228 * And write the flags.
230 c->flags = (c->slices - 1) << 24;
231 c->flags |= 0 << 11; // bit field to signal interlaced encoding mode
232 c->flags |= c->compression;
234 AV_WL32(avctx->extradata + 12, c->flags);
239 static void mangle_rgb_planes(uint8_t *dst[4], int dst_stride, uint8_t *src,
240 int step, int stride, int width, int height)
243 int k = 2 * dst_stride;
246 for (j = 0; j < height; j++) {
248 for (i = 0; i < width * step; i += step) {
252 dst[1][k] = src[i + 2] - g;
253 dst[2][k] = src[i + 0] - g;
257 for (i = 0; i < width * step; i += step) {
261 dst[1][k] = src[i + 2] - g;
262 dst[2][k] = src[i + 0] - g;
263 dst[3][k] = src[i + 3];
267 k += dst_stride - width;
272 /* Write data to a plane with left prediction */
273 static void left_predict(uint8_t *src, uint8_t *dst, int stride,
274 int width, int height)
279 prev = 0x80; /* Set the initial value */
280 for (j = 0; j < height; j++) {
281 for (i = 0; i < width; i++) {
282 *dst++ = src[i] - prev;
289 /* Write data to a plane with median prediction */
290 static void median_predict(UtvideoContext *c, uint8_t *src, uint8_t *dst, int stride,
291 int width, int height)
297 /* First line uses left neighbour prediction */
298 prev = 0x80; /* Set the initial value */
299 for (i = 0; i < width; i++) {
300 *dst++ = src[i] - prev;
310 * Second line uses top prediction for the first sample,
311 * and median for the rest.
315 /* Rest of the coded part uses median prediction */
316 for (j = 1; j < height; j++) {
317 c->hdsp.sub_hfyu_median_pred(dst, src - stride, src, width, &A, &B);
323 /* Count the usage of values in a plane */
324 static void count_usage(uint8_t *src, int width,
325 int height, uint64_t *counts)
329 for (j = 0; j < height; j++) {
330 for (i = 0; i < width; i++) {
337 /* Calculate the actual huffman codes from the code lengths */
338 static void calculate_codes(HuffEntry *he)
343 qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
346 while (he[last].len == 255 && last)
350 for (i = last; i >= 0; i--) {
351 he[i].code = code >> (32 - he[i].len);
352 code += 0x80000000u >> (he[i].len - 1);
355 qsort(he, 256, sizeof(*he), huff_cmp_sym);
358 /* Write huffman bit codes to a memory block */
359 static int write_huff_codes(uint8_t *src, uint8_t *dst, int dst_size,
360 int width, int height, HuffEntry *he)
366 init_put_bits(&pb, dst, dst_size);
368 /* Write the codes */
369 for (j = 0; j < height; j++) {
370 for (i = 0; i < width; i++)
371 put_bits(&pb, he[src[i]].len, he[src[i]].code);
376 /* Pad output to a 32bit boundary */
377 count = put_bits_count(&pb) & 0x1F;
380 put_bits(&pb, 32 - count, 0);
382 /* Get the amount of bits written */
383 count = put_bits_count(&pb);
385 /* Flush the rest with zeroes */
391 static int encode_plane(AVCodecContext *avctx, uint8_t *src,
392 uint8_t *dst, int stride, int plane_no,
393 int width, int height, PutByteContext *pb)
395 UtvideoContext *c = avctx->priv_data;
396 uint8_t lengths[256];
397 uint64_t counts[256] = { 0 };
401 uint32_t offset = 0, slice_len = 0;
402 const int cmask = ~(!plane_no && avctx->pix_fmt == AV_PIX_FMT_YUV420P);
403 int i, sstart, send = 0;
407 /* Do prediction / make planes */
408 switch (c->frame_pred) {
410 for (i = 0; i < c->slices; i++) {
412 send = height * (i + 1) / c->slices & cmask;
413 av_image_copy_plane(dst + sstart * width, width,
414 src + sstart * stride, stride,
415 width, send - sstart);
419 for (i = 0; i < c->slices; i++) {
421 send = height * (i + 1) / c->slices & cmask;
422 left_predict(src + sstart * stride, dst + sstart * width,
423 stride, width, send - sstart);
427 for (i = 0; i < c->slices; i++) {
429 send = height * (i + 1) / c->slices & cmask;
430 median_predict(c, src + sstart * stride, dst + sstart * width,
431 stride, width, send - sstart);
435 av_log(avctx, AV_LOG_ERROR, "Unknown prediction mode: %d\n",
437 return AVERROR_OPTION_NOT_FOUND;
440 /* Count the usage of values */
441 count_usage(dst, width, height, counts);
443 /* Check for a special case where only one symbol was used */
444 for (symbol = 0; symbol < 256; symbol++) {
445 /* If non-zero count is found, see if it matches width * height */
446 if (counts[symbol]) {
447 /* Special case if only one symbol was used */
448 if (counts[symbol] == width * (int64_t)height) {
450 * Write a zero for the single symbol
451 * used in the plane, else 0xFF.
453 for (i = 0; i < 256; i++) {
455 bytestream2_put_byte(pb, 0);
457 bytestream2_put_byte(pb, 0xFF);
460 /* Write zeroes for lengths */
461 for (i = 0; i < c->slices; i++)
462 bytestream2_put_le32(pb, 0);
464 /* And that's all for that plane folks */
471 /* Calculate huffman lengths */
472 if ((ret = ff_huff_gen_len_table(lengths, counts, 256, 1)) < 0)
476 * Write the plane's header into the output packet:
477 * - huffman code lengths (256 bytes)
478 * - slice end offsets (gotten from the slice lengths)
480 for (i = 0; i < 256; i++) {
481 bytestream2_put_byte(pb, lengths[i]);
483 he[i].len = lengths[i];
487 /* Calculate the huffman codes themselves */
491 for (i = 0; i < c->slices; i++) {
493 send = height * (i + 1) / c->slices & cmask;
496 * Write the huffman codes to a buffer,
497 * get the offset in bits and convert to bytes.
499 offset += write_huff_codes(dst + sstart * width, c->slice_bits,
500 width * height + 4, width,
501 send - sstart, he) >> 3;
503 slice_len = offset - slice_len;
505 /* Byteswap the written huffman codes */
506 c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
507 (uint32_t *) c->slice_bits,
510 /* Write the offset to the stream */
511 bytestream2_put_le32(pb, offset);
513 /* Seek to the data part of the packet */
514 bytestream2_seek_p(pb, 4 * (c->slices - i - 1) +
515 offset - slice_len, SEEK_CUR);
517 /* Write the slices' data into the output packet */
518 bytestream2_put_buffer(pb, c->slice_bits, slice_len);
520 /* Seek back to the slice offsets */
521 bytestream2_seek_p(pb, -4 * (c->slices - i - 1) - offset,
527 /* And at the end seek to the end of written slice(s) */
528 bytestream2_seek_p(pb, offset, SEEK_CUR);
533 static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
534 const AVFrame *pic, int *got_packet)
536 UtvideoContext *c = avctx->priv_data;
543 int width = avctx->width, height = avctx->height;
546 /* Allocate a new packet if needed, and set it to the pointer dst */
547 ret = ff_alloc_packet2(avctx, pkt, (256 + 4 * c->slices + width * height) *
555 bytestream2_init_writer(&pb, dst, pkt->size);
557 av_fast_padded_malloc(&c->slice_bits, &c->slice_bits_size, width * height + 4);
559 if (!c->slice_bits) {
560 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 2.\n");
561 return AVERROR(ENOMEM);
564 /* In case of RGB, mangle the planes to Ut Video's format */
565 if (avctx->pix_fmt == AV_PIX_FMT_RGBA || avctx->pix_fmt == AV_PIX_FMT_RGB24)
566 mangle_rgb_planes(c->slice_buffer, c->slice_stride, pic->data[0],
567 c->planes, pic->linesize[0], width, height);
569 /* Deal with the planes */
570 switch (avctx->pix_fmt) {
571 case AV_PIX_FMT_RGB24:
572 case AV_PIX_FMT_RGBA:
573 for (i = 0; i < c->planes; i++) {
574 ret = encode_plane(avctx, c->slice_buffer[i] + 2 * c->slice_stride,
575 c->slice_buffer[i], c->slice_stride, i,
579 av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i);
584 case AV_PIX_FMT_YUV422P:
585 for (i = 0; i < c->planes; i++) {
586 ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0],
587 pic->linesize[i], i, width >> !!i, height, &pb);
590 av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i);
595 case AV_PIX_FMT_YUV420P:
596 for (i = 0; i < c->planes; i++) {
597 ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0],
598 pic->linesize[i], i, width >> !!i, height >> !!i,
602 av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i);
608 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format: %d\n",
610 return AVERROR_INVALIDDATA;
614 * Write frame information (LE 32bit unsigned)
615 * into the output packet.
616 * Contains the prediction method.
618 frame_info = c->frame_pred << 8;
619 bytestream2_put_le32(&pb, frame_info);
622 * At least currently Ut Video is IDR only.
623 * Set flags accordingly.
625 avctx->coded_frame->key_frame = 1;
626 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
628 pkt->size = bytestream2_tell_p(&pb);
629 pkt->flags |= AV_PKT_FLAG_KEY;
631 /* Packet should be done */
637 AVCodec ff_utvideo_encoder = {
639 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
640 .type = AVMEDIA_TYPE_VIDEO,
641 .id = AV_CODEC_ID_UTVIDEO,
642 .priv_data_size = sizeof(UtvideoContext),
643 .init = utvideo_encode_init,
644 .encode2 = utvideo_encode_frame,
645 .close = utvideo_encode_close,
646 .capabilities = CODEC_CAP_FRAME_THREADS | CODEC_CAP_INTRA_ONLY,
647 .pix_fmts = (const enum AVPixelFormat[]) {
648 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_YUV422P,
649 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE