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"
31 #include "bytestream.h"
38 /* Compare huffentry symbols */
39 static int huff_cmp_sym(const void *a, const void *b)
41 const HuffEntry *aa = a, *bb = b;
42 return aa->sym - bb->sym;
45 static av_cold int utvideo_encode_close(AVCodecContext *avctx)
47 UtvideoContext *c = avctx->priv_data;
50 av_freep(&avctx->coded_frame);
51 av_freep(&c->slice_bits);
52 for (i = 0; i < 4; i++)
53 av_freep(&c->slice_buffer[i]);
58 static av_cold int utvideo_encode_init(AVCodecContext *avctx)
60 UtvideoContext *c = avctx->priv_data;
62 uint32_t original_format;
65 c->frame_info_size = 4;
66 c->slice_stride = FFALIGN(avctx->width, 32);
68 switch (avctx->pix_fmt) {
69 case AV_PIX_FMT_RGB24:
71 avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
72 original_format = UTVIDEO_RGB;
76 avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
77 original_format = UTVIDEO_RGBA;
79 case AV_PIX_FMT_YUV420P:
80 if (avctx->width & 1 || avctx->height & 1) {
81 av_log(avctx, AV_LOG_ERROR,
82 "4:2:0 video requires even width and height.\n");
83 return AVERROR_INVALIDDATA;
86 avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
87 original_format = UTVIDEO_420;
89 case AV_PIX_FMT_YUV422P:
90 if (avctx->width & 1) {
91 av_log(avctx, AV_LOG_ERROR,
92 "4:2:2 video requires even width.\n");
93 return AVERROR_INVALIDDATA;
96 avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
97 original_format = UTVIDEO_422;
100 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format: %d\n",
102 return AVERROR_INVALIDDATA;
105 ff_dsputil_init(&c->dsp, avctx);
107 /* Check the prediction method, and error out if unsupported */
108 if (avctx->prediction_method < 0 || avctx->prediction_method > 4) {
109 av_log(avctx, AV_LOG_WARNING,
110 "Prediction method %d is not supported in Ut Video.\n",
111 avctx->prediction_method);
112 return AVERROR_OPTION_NOT_FOUND;
115 if (avctx->prediction_method == FF_PRED_PLANE) {
116 av_log(avctx, AV_LOG_ERROR,
117 "Plane prediction is not supported in Ut Video.\n");
118 return AVERROR_OPTION_NOT_FOUND;
121 /* Convert from libavcodec prediction type to Ut Video's */
122 c->frame_pred = ff_ut_pred_order[avctx->prediction_method];
124 if (c->frame_pred == PRED_GRADIENT) {
125 av_log(avctx, AV_LOG_ERROR, "Gradient prediction is not supported.\n");
126 return AVERROR_OPTION_NOT_FOUND;
129 avctx->coded_frame = av_frame_alloc();
131 if (!avctx->coded_frame) {
132 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
133 utvideo_encode_close(avctx);
134 return AVERROR(ENOMEM);
137 /* extradata size is 4 * 32bit */
138 avctx->extradata_size = 16;
140 avctx->extradata = av_mallocz(avctx->extradata_size +
141 FF_INPUT_BUFFER_PADDING_SIZE);
143 if (!avctx->extradata) {
144 av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata.\n");
145 utvideo_encode_close(avctx);
146 return AVERROR(ENOMEM);
149 for (i = 0; i < c->planes; i++) {
150 c->slice_buffer[i] = av_malloc(c->slice_stride * (avctx->height + 2) +
151 FF_INPUT_BUFFER_PADDING_SIZE);
152 if (!c->slice_buffer[i]) {
153 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 1.\n");
154 utvideo_encode_close(avctx);
155 return AVERROR(ENOMEM);
160 * Set the version of the encoder.
161 * Last byte is "implementation ID", which is
162 * obtained from the creator of the format.
163 * Libavcodec has been assigned with the ID 0xF0.
165 AV_WB32(avctx->extradata, MKTAG(1, 0, 0, 0xF0));
168 * Set the "original format"
169 * Not used for anything during decoding.
171 AV_WL32(avctx->extradata + 4, original_format);
173 /* Write 4 as the 'frame info size' */
174 AV_WL32(avctx->extradata + 8, c->frame_info_size);
177 * Set how many slices are going to be used.
178 * Set one slice for now.
182 /* Set compression mode */
183 c->compression = COMP_HUFF;
186 * Set the encoding flags:
187 * - Slice count minus 1
188 * - Interlaced encoding mode flag, set to zero for now.
189 * - Compression mode (none/huff)
190 * And write the flags.
192 c->flags = (c->slices - 1) << 24;
193 c->flags |= 0 << 11; // bit field to signal interlaced encoding mode
194 c->flags |= c->compression;
196 AV_WL32(avctx->extradata + 12, c->flags);
201 static void mangle_rgb_planes(uint8_t *dst[4], int dst_stride, uint8_t *src,
202 int step, int stride, int width, int height)
205 int k = 2 * dst_stride;
208 for (j = 0; j < height; j++) {
210 for (i = 0; i < width * step; i += step) {
214 dst[1][k] = src[i + 2] - g;
215 dst[2][k] = src[i + 0] - g;
219 for (i = 0; i < width * step; i += step) {
223 dst[1][k] = src[i + 2] - g;
224 dst[2][k] = src[i + 0] - g;
225 dst[3][k] = src[i + 3];
229 k += dst_stride - width;
234 /* Write data to a plane with left prediction */
235 static void left_predict(uint8_t *src, uint8_t *dst, int stride,
236 int width, int height)
241 prev = 0x80; /* Set the initial value */
242 for (j = 0; j < height; j++) {
243 for (i = 0; i < width; i++) {
244 *dst++ = src[i] - prev;
251 /* Write data to a plane with median prediction */
252 static void median_predict(UtvideoContext *c, uint8_t *src, uint8_t *dst, int stride,
253 int width, int height)
259 /* First line uses left neighbour prediction */
260 prev = 0x80; /* Set the initial value */
261 for (i = 0; i < width; i++) {
262 *dst++ = src[i] - prev;
272 * Second line uses top prediction for the first sample,
273 * and median for the rest.
277 /* Rest of the coded part uses median prediction */
278 for (j = 1; j < height; j++) {
279 c->dsp.sub_hfyu_median_prediction(dst, src - stride, src, width, &A, &B);
285 /* Count the usage of values in a plane */
286 static void count_usage(uint8_t *src, int width,
287 int height, uint64_t *counts)
291 for (j = 0; j < height; j++) {
292 for (i = 0; i < width; i++) {
299 /* Calculate the actual huffman codes from the code lengths */
300 static void calculate_codes(HuffEntry *he)
305 qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
308 while (he[last].len == 255 && last)
312 for (i = last; i >= 0; i--) {
313 he[i].code = code >> (32 - he[i].len);
314 code += 0x80000000u >> (he[i].len - 1);
317 qsort(he, 256, sizeof(*he), huff_cmp_sym);
320 /* Write huffman bit codes to a memory block */
321 static int write_huff_codes(uint8_t *src, uint8_t *dst, int dst_size,
322 int width, int height, HuffEntry *he)
328 init_put_bits(&pb, dst, dst_size);
330 /* Write the codes */
331 for (j = 0; j < height; j++) {
332 for (i = 0; i < width; i++)
333 put_bits(&pb, he[src[i]].len, he[src[i]].code);
338 /* Pad output to a 32bit boundary */
339 count = put_bits_count(&pb) & 0x1F;
342 put_bits(&pb, 32 - count, 0);
344 /* Get the amount of bits written */
345 count = put_bits_count(&pb);
347 /* Flush the rest with zeroes */
353 static int encode_plane(AVCodecContext *avctx, uint8_t *src,
354 uint8_t *dst, int stride,
355 int width, int height, PutByteContext *pb)
357 UtvideoContext *c = avctx->priv_data;
358 uint8_t lengths[256];
359 uint64_t counts[256] = { 0 };
363 uint32_t offset = 0, slice_len = 0;
364 int i, sstart, send = 0;
367 /* Do prediction / make planes */
368 switch (c->frame_pred) {
370 for (i = 0; i < c->slices; i++) {
372 send = height * (i + 1) / c->slices;
373 av_image_copy_plane(dst + sstart * width, width,
374 src + sstart * stride, stride,
375 width, send - sstart);
379 for (i = 0; i < c->slices; i++) {
381 send = height * (i + 1) / c->slices;
382 left_predict(src + sstart * stride, dst + sstart * width,
383 stride, width, send - sstart);
387 for (i = 0; i < c->slices; i++) {
389 send = height * (i + 1) / c->slices;
390 median_predict(c, src + sstart * stride, dst + sstart * width,
391 stride, width, send - sstart);
395 av_log(avctx, AV_LOG_ERROR, "Unknown prediction mode: %d\n",
397 return AVERROR_OPTION_NOT_FOUND;
400 /* Count the usage of values */
401 count_usage(dst, width, height, counts);
403 /* Check for a special case where only one symbol was used */
404 for (symbol = 0; symbol < 256; symbol++) {
405 /* If non-zero count is found, see if it matches width * height */
406 if (counts[symbol]) {
407 /* Special case if only one symbol was used */
408 if (counts[symbol] == width * (int64_t)height) {
410 * Write a zero for the single symbol
411 * used in the plane, else 0xFF.
413 for (i = 0; i < 256; i++) {
415 bytestream2_put_byte(pb, 0);
417 bytestream2_put_byte(pb, 0xFF);
420 /* Write zeroes for lengths */
421 for (i = 0; i < c->slices; i++)
422 bytestream2_put_le32(pb, 0);
424 /* And that's all for that plane folks */
431 /* Calculate huffman lengths */
432 ff_huff_gen_len_table(lengths, counts);
435 * Write the plane's header into the output packet:
436 * - huffman code lengths (256 bytes)
437 * - slice end offsets (gotten from the slice lengths)
439 for (i = 0; i < 256; i++) {
440 bytestream2_put_byte(pb, lengths[i]);
442 he[i].len = lengths[i];
446 /* Calculate the huffman codes themselves */
450 for (i = 0; i < c->slices; i++) {
452 send = height * (i + 1) / c->slices;
455 * Write the huffman codes to a buffer,
456 * get the offset in bits and convert to bytes.
458 offset += write_huff_codes(dst + sstart * width, c->slice_bits,
459 width * (send - sstart), width,
460 send - sstart, he) >> 3;
462 slice_len = offset - slice_len;
464 /* Byteswap the written huffman codes */
465 c->dsp.bswap_buf((uint32_t *) c->slice_bits,
466 (uint32_t *) c->slice_bits,
469 /* Write the offset to the stream */
470 bytestream2_put_le32(pb, offset);
472 /* Seek to the data part of the packet */
473 bytestream2_seek_p(pb, 4 * (c->slices - i - 1) +
474 offset - slice_len, SEEK_CUR);
476 /* Write the slices' data into the output packet */
477 bytestream2_put_buffer(pb, c->slice_bits, slice_len);
479 /* Seek back to the slice offsets */
480 bytestream2_seek_p(pb, -4 * (c->slices - i - 1) - offset,
486 /* And at the end seek to the end of written slice(s) */
487 bytestream2_seek_p(pb, offset, SEEK_CUR);
492 static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
493 const AVFrame *pic, int *got_packet)
495 UtvideoContext *c = avctx->priv_data;
502 int width = avctx->width, height = avctx->height;
505 /* Allocate a new packet if needed, and set it to the pointer dst */
506 ret = ff_alloc_packet2(avctx, pkt, (256 + 4 * c->slices + width * height) *
514 bytestream2_init_writer(&pb, dst, pkt->size);
516 av_fast_padded_malloc(&c->slice_bits, &c->slice_bits_size, width * height);
518 if (!c->slice_bits) {
519 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 2.\n");
520 return AVERROR(ENOMEM);
523 /* In case of RGB, mangle the planes to Ut Video's format */
524 if (avctx->pix_fmt == AV_PIX_FMT_RGBA || avctx->pix_fmt == AV_PIX_FMT_RGB24)
525 mangle_rgb_planes(c->slice_buffer, c->slice_stride, pic->data[0],
526 c->planes, pic->linesize[0], width, height);
528 /* Deal with the planes */
529 switch (avctx->pix_fmt) {
530 case AV_PIX_FMT_RGB24:
531 case AV_PIX_FMT_RGBA:
532 for (i = 0; i < c->planes; i++) {
533 ret = encode_plane(avctx, c->slice_buffer[i] + 2 * c->slice_stride,
534 c->slice_buffer[i], c->slice_stride,
538 av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i);
543 case AV_PIX_FMT_YUV422P:
544 for (i = 0; i < c->planes; i++) {
545 ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0],
546 pic->linesize[i], width >> !!i, height, &pb);
549 av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i);
554 case AV_PIX_FMT_YUV420P:
555 for (i = 0; i < c->planes; i++) {
556 ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0],
557 pic->linesize[i], width >> !!i, height >> !!i,
561 av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i);
567 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format: %d\n",
569 return AVERROR_INVALIDDATA;
573 * Write frame information (LE 32bit unsigned)
574 * into the output packet.
575 * Contains the prediction method.
577 frame_info = c->frame_pred << 8;
578 bytestream2_put_le32(&pb, frame_info);
581 * At least currently Ut Video is IDR only.
582 * Set flags accordingly.
584 avctx->coded_frame->key_frame = 1;
585 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
587 pkt->size = bytestream2_tell_p(&pb);
588 pkt->flags |= AV_PKT_FLAG_KEY;
590 /* Packet should be done */
596 AVCodec ff_utvideo_encoder = {
598 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),
599 .type = AVMEDIA_TYPE_VIDEO,
600 .id = AV_CODEC_ID_UTVIDEO,
601 .priv_data_size = sizeof(UtvideoContext),
602 .init = utvideo_encode_init,
603 .encode2 = utvideo_encode_frame,
604 .close = utvideo_encode_close,
605 .pix_fmts = (const enum AVPixelFormat[]) {
606 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_YUV422P,
607 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE