3 * Copyright (c) 2012 Jan Ekström
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 if (avctx
->colorspace
== AVCOL_SPC_BT709
)
87 avctx
->codec_tag
= MKTAG('U', 'L', 'H', '0');
89 avctx
->codec_tag
= MKTAG('U', 'L', 'Y', '0');
90 original_format
= UTVIDEO_420
;
92 case AV_PIX_FMT_YUV422P
:
93 if (avctx
->width
& 1) {
94 av_log(avctx
, AV_LOG_ERROR
,
95 "4:2:2 video requires even width.\n");
96 return AVERROR_INVALIDDATA
;
99 if (avctx
->colorspace
== AVCOL_SPC_BT709
)
100 avctx
->codec_tag
= MKTAG('U', 'L', 'H', '2');
102 avctx
->codec_tag
= MKTAG('U', 'L', 'Y', '2');
103 original_format
= UTVIDEO_422
;
106 av_log(avctx
, AV_LOG_ERROR
, "Unknown pixel format: %d\n",
108 return AVERROR_INVALIDDATA
;
111 ff_dsputil_init(&c
->dsp
, avctx
);
113 /* Check the prediction method, and error out if unsupported */
114 if (avctx
->prediction_method
< 0 || avctx
->prediction_method
> 4) {
115 av_log(avctx
, AV_LOG_WARNING
,
116 "Prediction method %d is not supported in Ut Video.\n",
117 avctx
->prediction_method
);
118 return AVERROR_OPTION_NOT_FOUND
;
121 if (avctx
->prediction_method
== FF_PRED_PLANE
) {
122 av_log(avctx
, AV_LOG_ERROR
,
123 "Plane prediction is not supported in Ut Video.\n");
124 return AVERROR_OPTION_NOT_FOUND
;
127 /* Convert from libavcodec prediction type to Ut Video's */
128 c
->frame_pred
= ff_ut_pred_order
[avctx
->prediction_method
];
130 if (c
->frame_pred
== PRED_GRADIENT
) {
131 av_log(avctx
, AV_LOG_ERROR
, "Gradient prediction is not supported.\n");
132 return AVERROR_OPTION_NOT_FOUND
;
135 avctx
->coded_frame
= av_frame_alloc();
137 if (!avctx
->coded_frame
) {
138 av_log(avctx
, AV_LOG_ERROR
, "Could not allocate frame.\n");
139 utvideo_encode_close(avctx
);
140 return AVERROR(ENOMEM
);
143 /* extradata size is 4 * 32bit */
144 avctx
->extradata_size
= 16;
146 avctx
->extradata
= av_mallocz(avctx
->extradata_size
+
147 FF_INPUT_BUFFER_PADDING_SIZE
);
149 if (!avctx
->extradata
) {
150 av_log(avctx
, AV_LOG_ERROR
, "Could not allocate extradata.\n");
151 utvideo_encode_close(avctx
);
152 return AVERROR(ENOMEM
);
155 for (i
= 0; i
< c
->planes
; i
++) {
156 c
->slice_buffer
[i
] = av_malloc(c
->slice_stride
* (avctx
->height
+ 2) +
157 FF_INPUT_BUFFER_PADDING_SIZE
);
158 if (!c
->slice_buffer
[i
]) {
159 av_log(avctx
, AV_LOG_ERROR
, "Cannot allocate temporary buffer 1.\n");
160 utvideo_encode_close(avctx
);
161 return AVERROR(ENOMEM
);
166 * Set the version of the encoder.
167 * Last byte is "implementation ID", which is
168 * obtained from the creator of the format.
169 * Libavcodec has been assigned with the ID 0xF0.
171 AV_WB32(avctx
->extradata
, MKTAG(1, 0, 0, 0xF0));
174 * Set the "original format"
175 * Not used for anything during decoding.
177 AV_WL32(avctx
->extradata
+ 4, original_format
);
179 /* Write 4 as the 'frame info size' */
180 AV_WL32(avctx
->extradata
+ 8, c
->frame_info_size
);
183 * Set how many slices are going to be used.
184 * Set one slice for now.
188 /* Set compression mode */
189 c
->compression
= COMP_HUFF
;
192 * Set the encoding flags:
193 * - Slice count minus 1
194 * - Interlaced encoding mode flag, set to zero for now.
195 * - Compression mode (none/huff)
196 * And write the flags.
198 c
->flags
= (c
->slices
- 1) << 24;
199 c
->flags
|= 0 << 11; // bit field to signal interlaced encoding mode
200 c
->flags
|= c
->compression
;
202 AV_WL32(avctx
->extradata
+ 12, c
->flags
);
207 static void mangle_rgb_planes(uint8_t *dst
[4], int dst_stride
, uint8_t *src
,
208 int step
, int stride
, int width
, int height
)
211 int k
= 2 * dst_stride
;
214 for (j
= 0; j
< height
; j
++) {
216 for (i
= 0; i
< width
* step
; i
+= step
) {
220 dst
[1][k
] = src
[i
+ 2] - g
;
221 dst
[2][k
] = src
[i
+ 0] - g
;
225 for (i
= 0; i
< width
* step
; i
+= step
) {
229 dst
[1][k
] = src
[i
+ 2] - g
;
230 dst
[2][k
] = src
[i
+ 0] - g
;
231 dst
[3][k
] = src
[i
+ 3];
235 k
+= dst_stride
- width
;
240 /* Write data to a plane with left prediction */
241 static void left_predict(uint8_t *src
, uint8_t *dst
, int stride
,
242 int width
, int height
)
247 prev
= 0x80; /* Set the initial value */
248 for (j
= 0; j
< height
; j
++) {
249 for (i
= 0; i
< width
; i
++) {
250 *dst
++ = src
[i
] - prev
;
257 /* Write data to a plane with median prediction */
258 static void median_predict(UtvideoContext
*c
, uint8_t *src
, uint8_t *dst
, int stride
,
259 int width
, int height
)
265 /* First line uses left neighbour prediction */
266 prev
= 0x80; /* Set the initial value */
267 for (i
= 0; i
< width
; i
++) {
268 *dst
++ = src
[i
] - prev
;
278 * Second line uses top prediction for the first sample,
279 * and median for the rest.
283 /* Rest of the coded part uses median prediction */
284 for (j
= 1; j
< height
; j
++) {
285 c
->dsp
.sub_hfyu_median_prediction(dst
, src
- stride
, src
, width
, &A
, &B
);
291 /* Count the usage of values in a plane */
292 static void count_usage(uint8_t *src
, int width
,
293 int height
, uint64_t *counts
)
297 for (j
= 0; j
< height
; j
++) {
298 for (i
= 0; i
< width
; i
++) {
305 /* Calculate the actual huffman codes from the code lengths */
306 static void calculate_codes(HuffEntry
*he
)
311 qsort(he
, 256, sizeof(*he
), ff_ut_huff_cmp_len
);
314 while (he
[last
].len
== 255 && last
)
318 for (i
= last
; i
>= 0; i
--) {
319 he
[i
].code
= code
>> (32 - he
[i
].len
);
320 code
+= 0x80000000u
>> (he
[i
].len
- 1);
323 qsort(he
, 256, sizeof(*he
), huff_cmp_sym
);
326 /* Write huffman bit codes to a memory block */
327 static int write_huff_codes(uint8_t *src
, uint8_t *dst
, int dst_size
,
328 int width
, int height
, HuffEntry
*he
)
334 init_put_bits(&pb
, dst
, dst_size
);
336 /* Write the codes */
337 for (j
= 0; j
< height
; j
++) {
338 for (i
= 0; i
< width
; i
++)
339 put_bits(&pb
, he
[src
[i
]].len
, he
[src
[i
]].code
);
344 /* Pad output to a 32bit boundary */
345 count
= put_bits_count(&pb
) & 0x1F;
348 put_bits(&pb
, 32 - count
, 0);
350 /* Get the amount of bits written */
351 count
= put_bits_count(&pb
);
353 /* Flush the rest with zeroes */
359 static int encode_plane(AVCodecContext
*avctx
, uint8_t *src
,
360 uint8_t *dst
, int stride
,
361 int width
, int height
, PutByteContext
*pb
)
363 UtvideoContext
*c
= avctx
->priv_data
;
364 uint8_t lengths
[256];
365 uint64_t counts
[256] = { 0 };
369 uint32_t offset
= 0, slice_len
= 0;
370 int i
, sstart
, send
= 0;
373 /* Do prediction / make planes */
374 switch (c
->frame_pred
) {
376 for (i
= 0; i
< c
->slices
; i
++) {
378 send
= height
* (i
+ 1) / c
->slices
;
379 av_image_copy_plane(dst
+ sstart
* width
, width
,
380 src
+ sstart
* stride
, stride
,
381 width
, send
- sstart
);
385 for (i
= 0; i
< c
->slices
; i
++) {
387 send
= height
* (i
+ 1) / c
->slices
;
388 left_predict(src
+ sstart
* stride
, dst
+ sstart
* width
,
389 stride
, width
, send
- sstart
);
393 for (i
= 0; i
< c
->slices
; i
++) {
395 send
= height
* (i
+ 1) / c
->slices
;
396 median_predict(c
, src
+ sstart
* stride
, dst
+ sstart
* width
,
397 stride
, width
, send
- sstart
);
401 av_log(avctx
, AV_LOG_ERROR
, "Unknown prediction mode: %d\n",
403 return AVERROR_OPTION_NOT_FOUND
;
406 /* Count the usage of values */
407 count_usage(dst
, width
, height
, counts
);
409 /* Check for a special case where only one symbol was used */
410 for (symbol
= 0; symbol
< 256; symbol
++) {
411 /* If non-zero count is found, see if it matches width * height */
412 if (counts
[symbol
]) {
413 /* Special case if only one symbol was used */
414 if (counts
[symbol
] == width
* height
) {
416 * Write a zero for the single symbol
417 * used in the plane, else 0xFF.
419 for (i
= 0; i
< 256; i
++) {
421 bytestream2_put_byte(pb
, 0);
423 bytestream2_put_byte(pb
, 0xFF);
426 /* Write zeroes for lengths */
427 for (i
= 0; i
< c
->slices
; i
++)
428 bytestream2_put_le32(pb
, 0);
430 /* And that's all for that plane folks */
437 /* Calculate huffman lengths */
438 ff_huff_gen_len_table(lengths
, counts
);
441 * Write the plane's header into the output packet:
442 * - huffman code lengths (256 bytes)
443 * - slice end offsets (gotten from the slice lengths)
445 for (i
= 0; i
< 256; i
++) {
446 bytestream2_put_byte(pb
, lengths
[i
]);
448 he
[i
].len
= lengths
[i
];
452 /* Calculate the huffman codes themselves */
456 for (i
= 0; i
< c
->slices
; i
++) {
458 send
= height
* (i
+ 1) / c
->slices
;
461 * Write the huffman codes to a buffer,
462 * get the offset in bits and convert to bytes.
464 offset
+= write_huff_codes(dst
+ sstart
* width
, c
->slice_bits
,
465 width
* (send
- sstart
), width
,
466 send
- sstart
, he
) >> 3;
468 slice_len
= offset
- slice_len
;
470 /* Byteswap the written huffman codes */
471 c
->dsp
.bswap_buf((uint32_t *) c
->slice_bits
,
472 (uint32_t *) c
->slice_bits
,
475 /* Write the offset to the stream */
476 bytestream2_put_le32(pb
, offset
);
478 /* Seek to the data part of the packet */
479 bytestream2_seek_p(pb
, 4 * (c
->slices
- i
- 1) +
480 offset
- slice_len
, SEEK_CUR
);
482 /* Write the slices' data into the output packet */
483 bytestream2_put_buffer(pb
, c
->slice_bits
, slice_len
);
485 /* Seek back to the slice offsets */
486 bytestream2_seek_p(pb
, -4 * (c
->slices
- i
- 1) - offset
,
492 /* And at the end seek to the end of written slice(s) */
493 bytestream2_seek_p(pb
, offset
, SEEK_CUR
);
498 static int utvideo_encode_frame(AVCodecContext
*avctx
, AVPacket
*pkt
,
499 const AVFrame
*pic
, int *got_packet
)
501 UtvideoContext
*c
= avctx
->priv_data
;
508 int width
= avctx
->width
, height
= avctx
->height
;
511 /* Allocate a new packet if needed, and set it to the pointer dst */
512 ret
= ff_alloc_packet(pkt
, (256 + 4 * c
->slices
+ width
* height
) *
516 av_log(avctx
, AV_LOG_ERROR
,
517 "Error allocating the output packet, or the provided packet "
524 bytestream2_init_writer(&pb
, dst
, pkt
->size
);
526 av_fast_malloc(&c
->slice_bits
, &c
->slice_bits_size
,
527 width
* height
+ FF_INPUT_BUFFER_PADDING_SIZE
);
529 if (!c
->slice_bits
) {
530 av_log(avctx
, AV_LOG_ERROR
, "Cannot allocate temporary buffer 2.\n");
531 return AVERROR(ENOMEM
);
534 /* In case of RGB, mangle the planes to Ut Video's format */
535 if (avctx
->pix_fmt
== AV_PIX_FMT_RGBA
|| avctx
->pix_fmt
== AV_PIX_FMT_RGB24
)
536 mangle_rgb_planes(c
->slice_buffer
, c
->slice_stride
, pic
->data
[0],
537 c
->planes
, pic
->linesize
[0], width
, height
);
539 /* Deal with the planes */
540 switch (avctx
->pix_fmt
) {
541 case AV_PIX_FMT_RGB24
:
542 case AV_PIX_FMT_RGBA
:
543 for (i
= 0; i
< c
->planes
; i
++) {
544 ret
= encode_plane(avctx
, c
->slice_buffer
[i
] + 2 * c
->slice_stride
,
545 c
->slice_buffer
[i
], c
->slice_stride
,
549 av_log(avctx
, AV_LOG_ERROR
, "Error encoding plane %d.\n", i
);
554 case AV_PIX_FMT_YUV422P
:
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
, &pb
);
560 av_log(avctx
, AV_LOG_ERROR
, "Error encoding plane %d.\n", i
);
565 case AV_PIX_FMT_YUV420P
:
566 for (i
= 0; i
< c
->planes
; i
++) {
567 ret
= encode_plane(avctx
, pic
->data
[i
], c
->slice_buffer
[0],
568 pic
->linesize
[i
], width
>> !!i
, height
>> !!i
,
572 av_log(avctx
, AV_LOG_ERROR
, "Error encoding plane %d.\n", i
);
578 av_log(avctx
, AV_LOG_ERROR
, "Unknown pixel format: %d\n",
580 return AVERROR_INVALIDDATA
;
584 * Write frame information (LE 32bit unsigned)
585 * into the output packet.
586 * Contains the prediction method.
588 frame_info
= c
->frame_pred
<< 8;
589 bytestream2_put_le32(&pb
, frame_info
);
592 * At least currently Ut Video is IDR only.
593 * Set flags accordingly.
595 avctx
->coded_frame
->key_frame
= 1;
596 avctx
->coded_frame
->pict_type
= AV_PICTURE_TYPE_I
;
598 pkt
->size
= bytestream2_tell_p(&pb
);
599 pkt
->flags
|= AV_PKT_FLAG_KEY
;
601 /* Packet should be done */
607 AVCodec ff_utvideo_encoder
= {
609 .long_name
= NULL_IF_CONFIG_SMALL("Ut Video"),
610 .type
= AVMEDIA_TYPE_VIDEO
,
611 .id
= AV_CODEC_ID_UTVIDEO
,
612 .priv_data_size
= sizeof(UtvideoContext
),
613 .init
= utvideo_encode_init
,
614 .encode2
= utvideo_encode_frame
,
615 .close
= utvideo_encode_close
,
616 .pix_fmts
= (const enum AVPixelFormat
[]) {
617 AV_PIX_FMT_RGB24
, AV_PIX_FMT_RGBA
, AV_PIX_FMT_YUV422P
,
618 AV_PIX_FMT_YUV420P
, AV_PIX_FMT_NONE