h264: simplify calls to ff_er_add_slice().
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / utvideoenc.c
blob0df3fb75d150fda37899e4deb541abd63bc5b20a
1 /*
2 * Ut Video encoder
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
22 /**
23 * @file
24 * Ut Video encoder
27 #include "libavutil/intreadwrite.h"
28 #include "avcodec.h"
29 #include "internal.h"
30 #include "bytestream.h"
31 #include "put_bits.h"
32 #include "dsputil.h"
33 #include "mathops.h"
34 #include "utvideo.h"
35 #include "huffman.h"
37 /* Compare huffentry symbols */
38 static int huff_cmp_sym(const void *a, const void *b)
40 const HuffEntry *aa = a, *bb = b;
41 return aa->sym - bb->sym;
44 static av_cold int utvideo_encode_close(AVCodecContext *avctx)
46 UtvideoContext *c = avctx->priv_data;
47 int i;
49 av_freep(&avctx->coded_frame);
50 av_freep(&c->slice_bits);
51 for (i = 0; i < 4; i++)
52 av_freep(&c->slice_buffer[i]);
54 return 0;
57 static av_cold int utvideo_encode_init(AVCodecContext *avctx)
59 UtvideoContext *c = avctx->priv_data;
60 int i;
61 uint32_t original_format;
63 c->avctx = avctx;
64 c->frame_info_size = 4;
65 c->slice_stride = FFALIGN(avctx->width, 32);
67 switch (avctx->pix_fmt) {
68 case AV_PIX_FMT_RGB24:
69 c->planes = 3;
70 avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
71 original_format = UTVIDEO_RGB;
72 break;
73 case AV_PIX_FMT_RGBA:
74 c->planes = 4;
75 avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
76 original_format = UTVIDEO_RGBA;
77 break;
78 case AV_PIX_FMT_YUV420P:
79 if (avctx->width & 1 || avctx->height & 1) {
80 av_log(avctx, AV_LOG_ERROR,
81 "4:2:0 video requires even width and height.\n");
82 return AVERROR_INVALIDDATA;
84 c->planes = 3;
85 avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
86 original_format = UTVIDEO_420;
87 break;
88 case AV_PIX_FMT_YUV422P:
89 if (avctx->width & 1) {
90 av_log(avctx, AV_LOG_ERROR,
91 "4:2:2 video requires even width.\n");
92 return AVERROR_INVALIDDATA;
94 c->planes = 3;
95 avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
96 original_format = UTVIDEO_422;
97 break;
98 default:
99 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format: %d\n",
100 avctx->pix_fmt);
101 return AVERROR_INVALIDDATA;
104 ff_dsputil_init(&c->dsp, avctx);
106 /* Check the prediction method, and error out if unsupported */
107 if (avctx->prediction_method < 0 || avctx->prediction_method > 4) {
108 av_log(avctx, AV_LOG_WARNING,
109 "Prediction method %d is not supported in Ut Video.\n",
110 avctx->prediction_method);
111 return AVERROR_OPTION_NOT_FOUND;
114 if (avctx->prediction_method == FF_PRED_PLANE) {
115 av_log(avctx, AV_LOG_ERROR,
116 "Plane prediction is not supported in Ut Video.\n");
117 return AVERROR_OPTION_NOT_FOUND;
120 /* Convert from libavcodec prediction type to Ut Video's */
121 c->frame_pred = ff_ut_pred_order[avctx->prediction_method];
123 if (c->frame_pred == PRED_GRADIENT) {
124 av_log(avctx, AV_LOG_ERROR, "Gradient prediction is not supported.\n");
125 return AVERROR_OPTION_NOT_FOUND;
128 avctx->coded_frame = avcodec_alloc_frame();
130 if (!avctx->coded_frame) {
131 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
132 utvideo_encode_close(avctx);
133 return AVERROR(ENOMEM);
136 /* extradata size is 4 * 32bit */
137 avctx->extradata_size = 16;
139 avctx->extradata = av_mallocz(avctx->extradata_size +
140 FF_INPUT_BUFFER_PADDING_SIZE);
142 if (!avctx->extradata) {
143 av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata.\n");
144 utvideo_encode_close(avctx);
145 return AVERROR(ENOMEM);
148 for (i = 0; i < c->planes; i++) {
149 c->slice_buffer[i] = av_malloc(c->slice_stride * (avctx->height + 2) +
150 FF_INPUT_BUFFER_PADDING_SIZE);
151 if (!c->slice_buffer[i]) {
152 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 1.\n");
153 utvideo_encode_close(avctx);
154 return AVERROR(ENOMEM);
159 * Set the version of the encoder.
160 * Last byte is "implementation ID", which is
161 * obtained from the creator of the format.
162 * Libavcodec has been assigned with the ID 0xF0.
164 AV_WB32(avctx->extradata, MKTAG(1, 0, 0, 0xF0));
167 * Set the "original format"
168 * Not used for anything during decoding.
170 AV_WL32(avctx->extradata + 4, original_format);
172 /* Write 4 as the 'frame info size' */
173 AV_WL32(avctx->extradata + 8, c->frame_info_size);
176 * Set how many slices are going to be used.
177 * Set one slice for now.
179 c->slices = 1;
181 /* Set compression mode */
182 c->compression = COMP_HUFF;
185 * Set the encoding flags:
186 * - Slice count minus 1
187 * - Interlaced encoding mode flag, set to zero for now.
188 * - Compression mode (none/huff)
189 * And write the flags.
191 c->flags = (c->slices - 1) << 24;
192 c->flags |= 0 << 11; // bit field to signal interlaced encoding mode
193 c->flags |= c->compression;
195 AV_WL32(avctx->extradata + 12, c->flags);
197 return 0;
200 static void mangle_rgb_planes(uint8_t *dst[4], int dst_stride, uint8_t *src,
201 int step, int stride, int width, int height)
203 int i, j;
204 int k = 2 * dst_stride;
205 unsigned int g;
207 for (j = 0; j < height; j++) {
208 if (step == 3) {
209 for (i = 0; i < width * step; i += step) {
210 g = src[i + 1];
211 dst[0][k] = g;
212 g += 0x80;
213 dst[1][k] = src[i + 2] - g;
214 dst[2][k] = src[i + 0] - g;
215 k++;
217 } else {
218 for (i = 0; i < width * step; i += step) {
219 g = src[i + 1];
220 dst[0][k] = g;
221 g += 0x80;
222 dst[1][k] = src[i + 2] - g;
223 dst[2][k] = src[i + 0] - g;
224 dst[3][k] = src[i + 3];
225 k++;
228 k += dst_stride - width;
229 src += stride;
233 /* Write data to a plane, no prediction applied */
234 static void write_plane(uint8_t *src, uint8_t *dst, int stride,
235 int width, int height)
237 int i, j;
239 for (j = 0; j < height; j++) {
240 for (i = 0; i < width; i++)
241 *dst++ = src[i];
243 src += stride;
247 /* Write data to a plane with left prediction */
248 static void left_predict(uint8_t *src, uint8_t *dst, int stride,
249 int width, int height)
251 int i, j;
252 uint8_t prev;
254 prev = 0x80; /* Set the initial value */
255 for (j = 0; j < height; j++) {
256 for (i = 0; i < width; i++) {
257 *dst++ = src[i] - prev;
258 prev = src[i];
260 src += stride;
264 /* Write data to a plane with median prediction */
265 static void median_predict(UtvideoContext *c, uint8_t *src, uint8_t *dst, int stride,
266 int width, int height)
268 int i, j;
269 int A, B;
270 uint8_t prev;
272 /* First line uses left neighbour prediction */
273 prev = 0x80; /* Set the initial value */
274 for (i = 0; i < width; i++) {
275 *dst++ = src[i] - prev;
276 prev = src[i];
279 if (height == 1)
280 return;
282 src += stride;
285 * Second line uses top prediction for the first sample,
286 * and median for the rest.
288 A = B = 0;
290 /* Rest of the coded part uses median prediction */
291 for (j = 1; j < height; j++) {
292 c->dsp.sub_hfyu_median_prediction(dst, src - stride, src, width, &A, &B);
293 dst += width;
294 src += stride;
298 /* Count the usage of values in a plane */
299 static void count_usage(uint8_t *src, int width,
300 int height, uint64_t *counts)
302 int i, j;
304 for (j = 0; j < height; j++) {
305 for (i = 0; i < width; i++) {
306 counts[src[i]]++;
308 src += width;
312 /* Calculate the actual huffman codes from the code lengths */
313 static void calculate_codes(HuffEntry *he)
315 int last, i;
316 uint32_t code;
318 qsort(he, 256, sizeof(*he), ff_ut_huff_cmp_len);
320 last = 255;
321 while (he[last].len == 255 && last)
322 last--;
324 code = 1;
325 for (i = last; i >= 0; i--) {
326 he[i].code = code >> (32 - he[i].len);
327 code += 0x80000000u >> (he[i].len - 1);
330 qsort(he, 256, sizeof(*he), huff_cmp_sym);
333 /* Write huffman bit codes to a memory block */
334 static int write_huff_codes(uint8_t *src, uint8_t *dst, int dst_size,
335 int width, int height, HuffEntry *he)
337 PutBitContext pb;
338 int i, j;
339 int count;
341 init_put_bits(&pb, dst, dst_size);
343 /* Write the codes */
344 for (j = 0; j < height; j++) {
345 for (i = 0; i < width; i++)
346 put_bits(&pb, he[src[i]].len, he[src[i]].code);
348 src += width;
351 /* Pad output to a 32bit boundary */
352 count = put_bits_count(&pb) & 0x1F;
354 if (count)
355 put_bits(&pb, 32 - count, 0);
357 /* Get the amount of bits written */
358 count = put_bits_count(&pb);
360 /* Flush the rest with zeroes */
361 flush_put_bits(&pb);
363 return count;
366 static int encode_plane(AVCodecContext *avctx, uint8_t *src,
367 uint8_t *dst, int stride,
368 int width, int height, PutByteContext *pb)
370 UtvideoContext *c = avctx->priv_data;
371 uint8_t lengths[256];
372 uint64_t counts[256] = { 0 };
374 HuffEntry he[256];
376 uint32_t offset = 0, slice_len = 0;
377 int i, sstart, send = 0;
378 int symbol;
380 /* Do prediction / make planes */
381 switch (c->frame_pred) {
382 case PRED_NONE:
383 for (i = 0; i < c->slices; i++) {
384 sstart = send;
385 send = height * (i + 1) / c->slices;
386 write_plane(src + sstart * stride, dst + sstart * width,
387 stride, width, send - sstart);
389 break;
390 case PRED_LEFT:
391 for (i = 0; i < c->slices; i++) {
392 sstart = send;
393 send = height * (i + 1) / c->slices;
394 left_predict(src + sstart * stride, dst + sstart * width,
395 stride, width, send - sstart);
397 break;
398 case PRED_MEDIAN:
399 for (i = 0; i < c->slices; i++) {
400 sstart = send;
401 send = height * (i + 1) / c->slices;
402 median_predict(c, src + sstart * stride, dst + sstart * width,
403 stride, width, send - sstart);
405 break;
406 default:
407 av_log(avctx, AV_LOG_ERROR, "Unknown prediction mode: %d\n",
408 c->frame_pred);
409 return AVERROR_OPTION_NOT_FOUND;
412 /* Count the usage of values */
413 count_usage(dst, width, height, counts);
415 /* Check for a special case where only one symbol was used */
416 for (symbol = 0; symbol < 256; symbol++) {
417 /* If non-zero count is found, see if it matches width * height */
418 if (counts[symbol]) {
419 /* Special case if only one symbol was used */
420 if (counts[symbol] == width * height) {
422 * Write a zero for the single symbol
423 * used in the plane, else 0xFF.
425 for (i = 0; i < 256; i++) {
426 if (i == symbol)
427 bytestream2_put_byte(pb, 0);
428 else
429 bytestream2_put_byte(pb, 0xFF);
432 /* Write zeroes for lengths */
433 for (i = 0; i < c->slices; i++)
434 bytestream2_put_le32(pb, 0);
436 /* And that's all for that plane folks */
437 return 0;
439 break;
443 /* Calculate huffman lengths */
444 ff_huff_gen_len_table(lengths, counts);
447 * Write the plane's header into the output packet:
448 * - huffman code lengths (256 bytes)
449 * - slice end offsets (gotten from the slice lengths)
451 for (i = 0; i < 256; i++) {
452 bytestream2_put_byte(pb, lengths[i]);
454 he[i].len = lengths[i];
455 he[i].sym = i;
458 /* Calculate the huffman codes themselves */
459 calculate_codes(he);
461 send = 0;
462 for (i = 0; i < c->slices; i++) {
463 sstart = send;
464 send = height * (i + 1) / c->slices;
467 * Write the huffman codes to a buffer,
468 * get the offset in bits and convert to bytes.
470 offset += write_huff_codes(dst + sstart * width, c->slice_bits,
471 width * (send - sstart), width,
472 send - sstart, he) >> 3;
474 slice_len = offset - slice_len;
476 /* Byteswap the written huffman codes */
477 c->dsp.bswap_buf((uint32_t *) c->slice_bits,
478 (uint32_t *) c->slice_bits,
479 slice_len >> 2);
481 /* Write the offset to the stream */
482 bytestream2_put_le32(pb, offset);
484 /* Seek to the data part of the packet */
485 bytestream2_seek_p(pb, 4 * (c->slices - i - 1) +
486 offset - slice_len, SEEK_CUR);
488 /* Write the slices' data into the output packet */
489 bytestream2_put_buffer(pb, c->slice_bits, slice_len);
491 /* Seek back to the slice offsets */
492 bytestream2_seek_p(pb, -4 * (c->slices - i - 1) - offset,
493 SEEK_CUR);
495 slice_len = offset;
498 /* And at the end seek to the end of written slice(s) */
499 bytestream2_seek_p(pb, offset, SEEK_CUR);
501 return 0;
504 static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
505 const AVFrame *pic, int *got_packet)
507 UtvideoContext *c = avctx->priv_data;
508 PutByteContext pb;
510 uint32_t frame_info;
512 uint8_t *dst;
514 int width = avctx->width, height = avctx->height;
515 int i, ret = 0;
517 /* Allocate a new packet if needed, and set it to the pointer dst */
518 ret = ff_alloc_packet(pkt, (256 + 4 * c->slices + width * height) *
519 c->planes + 4);
521 if (ret < 0) {
522 av_log(avctx, AV_LOG_ERROR,
523 "Error allocating the output packet, or the provided packet "
524 "was too small.\n");
525 return ret;
528 dst = pkt->data;
530 bytestream2_init_writer(&pb, dst, pkt->size);
532 av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
533 width * height + FF_INPUT_BUFFER_PADDING_SIZE);
535 if (!c->slice_bits) {
536 av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer 2.\n");
537 return AVERROR(ENOMEM);
540 /* In case of RGB, mangle the planes to Ut Video's format */
541 if (avctx->pix_fmt == AV_PIX_FMT_RGBA || avctx->pix_fmt == AV_PIX_FMT_RGB24)
542 mangle_rgb_planes(c->slice_buffer, c->slice_stride, pic->data[0],
543 c->planes, pic->linesize[0], width, height);
545 /* Deal with the planes */
546 switch (avctx->pix_fmt) {
547 case AV_PIX_FMT_RGB24:
548 case AV_PIX_FMT_RGBA:
549 for (i = 0; i < c->planes; i++) {
550 ret = encode_plane(avctx, c->slice_buffer[i] + 2 * c->slice_stride,
551 c->slice_buffer[i], c->slice_stride,
552 width, height, &pb);
554 if (ret) {
555 av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i);
556 return ret;
559 break;
560 case AV_PIX_FMT_YUV422P:
561 for (i = 0; i < c->planes; i++) {
562 ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0],
563 pic->linesize[i], width >> !!i, height, &pb);
565 if (ret) {
566 av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i);
567 return ret;
570 break;
571 case AV_PIX_FMT_YUV420P:
572 for (i = 0; i < c->planes; i++) {
573 ret = encode_plane(avctx, pic->data[i], c->slice_buffer[0],
574 pic->linesize[i], width >> !!i, height >> !!i,
575 &pb);
577 if (ret) {
578 av_log(avctx, AV_LOG_ERROR, "Error encoding plane %d.\n", i);
579 return ret;
582 break;
583 default:
584 av_log(avctx, AV_LOG_ERROR, "Unknown pixel format: %d\n",
585 avctx->pix_fmt);
586 return AVERROR_INVALIDDATA;
590 * Write frame information (LE 32bit unsigned)
591 * into the output packet.
592 * Contains the prediction method.
594 frame_info = c->frame_pred << 8;
595 bytestream2_put_le32(&pb, frame_info);
598 * At least currently Ut Video is IDR only.
599 * Set flags accordingly.
601 avctx->coded_frame->key_frame = 1;
602 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
604 pkt->size = bytestream2_tell_p(&pb);
605 pkt->flags |= AV_PKT_FLAG_KEY;
607 /* Packet should be done */
608 *got_packet = 1;
610 return 0;
613 AVCodec ff_utvideo_encoder = {
614 .name = "utvideo",
615 .type = AVMEDIA_TYPE_VIDEO,
616 .id = AV_CODEC_ID_UTVIDEO,
617 .priv_data_size = sizeof(UtvideoContext),
618 .init = utvideo_encode_init,
619 .encode2 = utvideo_encode_frame,
620 .close = utvideo_encode_close,
621 .pix_fmts = (const enum AVPixelFormat[]) {
622 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_YUV422P,
623 AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
625 .long_name = NULL_IF_CONFIG_SMALL("Ut Video"),