lavfi: switch to AVFrame.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / huffyuvenc.c
blob13c0a795660f6416b9259133a6c1b9cd6ddea6c4
1 /*
2 * Copyright (c) 2002-2003 Michael Niedermayer <michaelni@gmx.at>
4 * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
5 * the algorithm used
7 * This file is part of Libav.
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 /**
25 * @file
26 * huffyuv encoder
29 #include "avcodec.h"
30 #include "huffyuv.h"
31 #include "huffman.h"
32 #include "put_bits.h"
34 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst,
35 uint8_t *src, int w, int left)
37 int i;
38 if (w < 32) {
39 for (i = 0; i < w; i++) {
40 const int temp = src[i];
41 dst[i] = temp - left;
42 left = temp;
44 return left;
45 } else {
46 for (i = 0; i < 16; i++) {
47 const int temp = src[i];
48 dst[i] = temp - left;
49 left = temp;
51 s->dsp.diff_bytes(dst + 16, src + 16, src + 15, w - 16);
52 return src[w-1];
56 static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst,
57 uint8_t *src, int w,
58 int *red, int *green, int *blue)
60 int i;
61 int r,g,b;
62 r = *red;
63 g = *green;
64 b = *blue;
66 for (i = 0; i < FFMIN(w, 4); i++) {
67 const int rt = src[i * 4 + R];
68 const int gt = src[i * 4 + G];
69 const int bt = src[i * 4 + B];
70 dst[i * 4 + R] = rt - r;
71 dst[i * 4 + G] = gt - g;
72 dst[i * 4 + B] = bt - b;
73 r = rt;
74 g = gt;
75 b = bt;
78 s->dsp.diff_bytes(dst + 16, src + 16, src + 12, w * 4 - 16);
80 *red = src[(w - 1) * 4 + R];
81 *green = src[(w - 1) * 4 + G];
82 *blue = src[(w - 1) * 4 + B];
85 static int store_table(HYuvContext *s, const uint8_t *len, uint8_t *buf)
87 int i;
88 int index = 0;
90 for (i = 0; i < 256;) {
91 int val = len[i];
92 int repeat = 0;
94 for (; i < 256 && len[i] == val && repeat < 255; i++)
95 repeat++;
97 assert(val < 32 && val >0 && repeat<256 && repeat>0);
98 if ( repeat > 7) {
99 buf[index++] = val;
100 buf[index++] = repeat;
101 } else {
102 buf[index++] = val | (repeat << 5);
106 return index;
109 static av_cold int encode_init(AVCodecContext *avctx)
111 HYuvContext *s = avctx->priv_data;
112 int i, j;
114 ff_huffyuv_common_init(avctx);
116 avctx->extradata = av_mallocz(1024*30); // 256*3+4 == 772
117 avctx->stats_out = av_mallocz(1024*30); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132
118 s->version = 2;
120 avctx->coded_frame = &s->picture;
122 switch (avctx->pix_fmt) {
123 case AV_PIX_FMT_YUV420P:
124 s->bitstream_bpp = 12;
125 break;
126 case AV_PIX_FMT_YUV422P:
127 s->bitstream_bpp = 16;
128 break;
129 case AV_PIX_FMT_RGB32:
130 s->bitstream_bpp = 24;
131 break;
132 default:
133 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
134 return -1;
136 avctx->bits_per_coded_sample = s->bitstream_bpp;
137 s->decorrelate = s->bitstream_bpp >= 24;
138 s->predictor = avctx->prediction_method;
139 s->interlaced = avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0;
140 if (avctx->context_model == 1) {
141 s->context = avctx->context_model;
142 if (s->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)) {
143 av_log(avctx, AV_LOG_ERROR,
144 "context=1 is not compatible with "
145 "2 pass huffyuv encoding\n");
146 return -1;
148 }else s->context= 0;
150 if (avctx->codec->id == AV_CODEC_ID_HUFFYUV) {
151 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
152 av_log(avctx, AV_LOG_ERROR,
153 "Error: YV12 is not supported by huffyuv; use "
154 "vcodec=ffvhuff or format=422p\n");
155 return -1;
157 if (avctx->context_model) {
158 av_log(avctx, AV_LOG_ERROR,
159 "Error: per-frame huffman tables are not supported "
160 "by huffyuv; use vcodec=ffvhuff\n");
161 return -1;
163 if (s->interlaced != ( s->height > 288 ))
164 av_log(avctx, AV_LOG_INFO,
165 "using huffyuv 2.2.0 or newer interlacing flag\n");
168 if (s->bitstream_bpp >= 24 && s->predictor == MEDIAN) {
169 av_log(avctx, AV_LOG_ERROR,
170 "Error: RGB is incompatible with median predictor\n");
171 return -1;
174 ((uint8_t*)avctx->extradata)[0] = s->predictor | (s->decorrelate << 6);
175 ((uint8_t*)avctx->extradata)[1] = s->bitstream_bpp;
176 ((uint8_t*)avctx->extradata)[2] = s->interlaced ? 0x10 : 0x20;
177 if (s->context)
178 ((uint8_t*)avctx->extradata)[2] |= 0x40;
179 ((uint8_t*)avctx->extradata)[3] = 0;
180 s->avctx->extradata_size = 4;
182 if (avctx->stats_in) {
183 char *p = avctx->stats_in;
185 for (i = 0; i < 3; i++)
186 for (j = 0; j < 256; j++)
187 s->stats[i][j] = 1;
189 for (;;) {
190 for (i = 0; i < 3; i++) {
191 char *next;
193 for (j = 0; j < 256; j++) {
194 s->stats[i][j] += strtol(p, &next, 0);
195 if (next == p) return -1;
196 p = next;
199 if (p[0] == 0 || p[1] == 0 || p[2] == 0) break;
201 } else {
202 for (i = 0; i < 3; i++)
203 for (j = 0; j < 256; j++) {
204 int d = FFMIN(j, 256 - j);
206 s->stats[i][j] = 100000000 / (d + 1);
210 for (i = 0; i < 3; i++) {
211 ff_huff_gen_len_table(s->len[i], s->stats[i]);
213 if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0) {
214 return -1;
217 s->avctx->extradata_size +=
218 store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]);
221 if (s->context) {
222 for (i = 0; i < 3; i++) {
223 int pels = s->width * s->height / (i ? 40 : 10);
224 for (j = 0; j < 256; j++) {
225 int d = FFMIN(j, 256 - j);
226 s->stats[i][j] = pels/(d + 1);
229 } else {
230 for (i = 0; i < 3; i++)
231 for (j = 0; j < 256; j++)
232 s->stats[i][j]= 0;
235 ff_huffyuv_alloc_temp(s);
237 s->picture_number=0;
239 return 0;
241 static int encode_422_bitstream(HYuvContext *s, int offset, int count)
243 int i;
244 const uint8_t *y = s->temp[0] + offset;
245 const uint8_t *u = s->temp[1] + offset / 2;
246 const uint8_t *v = s->temp[2] + offset / 2;
248 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 2 * 4 * count) {
249 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
250 return -1;
253 #define LOAD4\
254 int y0 = y[2 * i];\
255 int y1 = y[2 * i + 1];\
256 int u0 = u[i];\
257 int v0 = v[i];
259 count /= 2;
261 if (s->flags & CODEC_FLAG_PASS1) {
262 for(i = 0; i < count; i++) {
263 LOAD4;
264 s->stats[0][y0]++;
265 s->stats[1][u0]++;
266 s->stats[0][y1]++;
267 s->stats[2][v0]++;
270 if (s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)
271 return 0;
272 if (s->context) {
273 for (i = 0; i < count; i++) {
274 LOAD4;
275 s->stats[0][y0]++;
276 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
277 s->stats[1][u0]++;
278 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
279 s->stats[0][y1]++;
280 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
281 s->stats[2][v0]++;
282 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
284 } else {
285 for(i = 0; i < count; i++) {
286 LOAD4;
287 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
288 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
289 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
290 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
293 return 0;
296 static int encode_gray_bitstream(HYuvContext *s, int count)
298 int i;
300 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 4 * count) {
301 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
302 return -1;
305 #define LOAD2\
306 int y0 = s->temp[0][2 * i];\
307 int y1 = s->temp[0][2 * i + 1];
308 #define STAT2\
309 s->stats[0][y0]++;\
310 s->stats[0][y1]++;
311 #define WRITE2\
312 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
313 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
315 count /= 2;
317 if (s->flags & CODEC_FLAG_PASS1) {
318 for (i = 0; i < count; i++) {
319 LOAD2;
320 STAT2;
323 if (s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)
324 return 0;
326 if (s->context) {
327 for (i = 0; i < count; i++) {
328 LOAD2;
329 STAT2;
330 WRITE2;
332 } else {
333 for (i = 0; i < count; i++) {
334 LOAD2;
335 WRITE2;
338 return 0;
341 static int encode_bgr_bitstream(HYuvContext *s, int count)
343 int i;
345 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 3 * 4 * count) {
346 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
347 return -1;
350 #define LOAD3\
351 int g = s->temp[0][4 * i + G];\
352 int b = (s->temp[0][4 * i + B] - g) & 0xff;\
353 int r = (s->temp[0][4 * i + R] - g) & 0xff;
354 #define STAT3\
355 s->stats[0][b]++;\
356 s->stats[1][g]++;\
357 s->stats[2][r]++;
358 #define WRITE3\
359 put_bits(&s->pb, s->len[1][g], s->bits[1][g]);\
360 put_bits(&s->pb, s->len[0][b], s->bits[0][b]);\
361 put_bits(&s->pb, s->len[2][r], s->bits[2][r]);
363 if ((s->flags & CODEC_FLAG_PASS1) &&
364 (s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)) {
365 for (i = 0; i < count; i++) {
366 LOAD3;
367 STAT3;
369 } else if (s->context || (s->flags & CODEC_FLAG_PASS1)) {
370 for (i = 0; i < count; i++) {
371 LOAD3;
372 STAT3;
373 WRITE3;
375 } else {
376 for (i = 0; i < count; i++) {
377 LOAD3;
378 WRITE3;
381 return 0;
384 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
385 const AVFrame *pict, int *got_packet)
387 HYuvContext *s = avctx->priv_data;
388 const int width = s->width;
389 const int width2 = s->width>>1;
390 const int height = s->height;
391 const int fake_ystride = s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
392 const int fake_ustride = s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
393 const int fake_vstride = s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
394 AVFrame * const p = &s->picture;
395 int i, j, size = 0, ret;
397 if (!pkt->data &&
398 (ret = av_new_packet(pkt, width * height * 3 * 4 + FF_MIN_BUFFER_SIZE)) < 0) {
399 av_log(avctx, AV_LOG_ERROR, "Error allocating output packet.\n");
400 return ret;
403 *p = *pict;
404 p->pict_type = AV_PICTURE_TYPE_I;
405 p->key_frame = 1;
407 if (s->context) {
408 for (i = 0; i < 3; i++) {
409 ff_huff_gen_len_table(s->len[i], s->stats[i]);
410 if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0)
411 return -1;
412 size += store_table(s, s->len[i], &pkt->data[size]);
415 for (i = 0; i < 3; i++)
416 for (j = 0; j < 256; j++)
417 s->stats[i][j] >>= 1;
420 init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
422 if (avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
423 avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
424 int lefty, leftu, leftv, y, cy;
426 put_bits(&s->pb, 8, leftv = p->data[2][0]);
427 put_bits(&s->pb, 8, lefty = p->data[0][1]);
428 put_bits(&s->pb, 8, leftu = p->data[1][0]);
429 put_bits(&s->pb, 8, p->data[0][0]);
431 lefty = sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
432 leftu = sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
433 leftv = sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
435 encode_422_bitstream(s, 2, width-2);
437 if (s->predictor==MEDIAN) {
438 int lefttopy, lefttopu, lefttopv;
439 cy = y = 1;
440 if (s->interlaced) {
441 lefty = sub_left_prediction(s, s->temp[0], p->data[0] + p->linesize[0], width , lefty);
442 leftu = sub_left_prediction(s, s->temp[1], p->data[1] + p->linesize[1], width2, leftu);
443 leftv = sub_left_prediction(s, s->temp[2], p->data[2] + p->linesize[2], width2, leftv);
445 encode_422_bitstream(s, 0, width);
446 y++; cy++;
449 lefty = sub_left_prediction(s, s->temp[0], p->data[0] + fake_ystride, 4, lefty);
450 leftu = sub_left_prediction(s, s->temp[1], p->data[1] + fake_ustride, 2, leftu);
451 leftv = sub_left_prediction(s, s->temp[2], p->data[2] + fake_vstride, 2, leftv);
453 encode_422_bitstream(s, 0, 4);
455 lefttopy = p->data[0][3];
456 lefttopu = p->data[1][1];
457 lefttopv = p->data[2][1];
458 s->dsp.sub_hfyu_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride + 4, width - 4 , &lefty, &lefttopy);
459 s->dsp.sub_hfyu_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride + 2, width2 - 2, &leftu, &lefttopu);
460 s->dsp.sub_hfyu_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride + 2, width2 - 2, &leftv, &lefttopv);
461 encode_422_bitstream(s, 0, width - 4);
462 y++; cy++;
464 for (; y < height; y++,cy++) {
465 uint8_t *ydst, *udst, *vdst;
467 if (s->bitstream_bpp == 12) {
468 while (2 * cy > y) {
469 ydst = p->data[0] + p->linesize[0] * y;
470 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
471 encode_gray_bitstream(s, width);
472 y++;
474 if (y >= height) break;
476 ydst = p->data[0] + p->linesize[0] * y;
477 udst = p->data[1] + p->linesize[1] * cy;
478 vdst = p->data[2] + p->linesize[2] * cy;
480 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
481 s->dsp.sub_hfyu_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
482 s->dsp.sub_hfyu_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
484 encode_422_bitstream(s, 0, width);
486 } else {
487 for (cy = y = 1; y < height; y++, cy++) {
488 uint8_t *ydst, *udst, *vdst;
490 /* encode a luma only line & y++ */
491 if (s->bitstream_bpp == 12) {
492 ydst = p->data[0] + p->linesize[0] * y;
494 if (s->predictor == PLANE && s->interlaced < y) {
495 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
497 lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
498 } else {
499 lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
501 encode_gray_bitstream(s, width);
502 y++;
503 if (y >= height) break;
506 ydst = p->data[0] + p->linesize[0] * y;
507 udst = p->data[1] + p->linesize[1] * cy;
508 vdst = p->data[2] + p->linesize[2] * cy;
510 if (s->predictor == PLANE && s->interlaced < cy) {
511 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
512 s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
513 s->dsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
515 lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
516 leftu = sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
517 leftv = sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
518 } else {
519 lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
520 leftu = sub_left_prediction(s, s->temp[1], udst, width2, leftu);
521 leftv = sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
524 encode_422_bitstream(s, 0, width);
527 } else if(avctx->pix_fmt == AV_PIX_FMT_RGB32) {
528 uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
529 const int stride = -p->linesize[0];
530 const int fake_stride = -fake_ystride;
531 int y;
532 int leftr, leftg, leftb;
534 put_bits(&s->pb, 8, leftr = data[R]);
535 put_bits(&s->pb, 8, leftg = data[G]);
536 put_bits(&s->pb, 8, leftb = data[B]);
537 put_bits(&s->pb, 8, 0);
539 sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1, &leftr, &leftg, &leftb);
540 encode_bgr_bitstream(s, width - 1);
542 for (y = 1; y < s->height; y++) {
543 uint8_t *dst = data + y*stride;
544 if (s->predictor == PLANE && s->interlaced < y) {
545 s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
546 sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width, &leftr, &leftg, &leftb);
547 } else {
548 sub_left_prediction_bgr32(s, s->temp[0], dst, width, &leftr, &leftg, &leftb);
550 encode_bgr_bitstream(s, width);
552 } else {
553 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
555 emms_c();
557 size += (put_bits_count(&s->pb) + 31) / 8;
558 put_bits(&s->pb, 16, 0);
559 put_bits(&s->pb, 15, 0);
560 size /= 4;
562 if ((s->flags&CODEC_FLAG_PASS1) && (s->picture_number & 31) == 0) {
563 int j;
564 char *p = avctx->stats_out;
565 char *end = p + 1024*30;
566 for (i = 0; i < 3; i++) {
567 for (j = 0; j < 256; j++) {
568 snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
569 p += strlen(p);
570 s->stats[i][j]= 0;
572 snprintf(p, end-p, "\n");
573 p++;
575 } else
576 avctx->stats_out[0] = '\0';
577 if (!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)) {
578 flush_put_bits(&s->pb);
579 s->dsp.bswap_buf((uint32_t*)pkt->data, (uint32_t*)pkt->data, size);
582 s->picture_number++;
584 pkt->size = size * 4;
585 pkt->flags |= AV_PKT_FLAG_KEY;
586 *got_packet = 1;
588 return 0;
591 static av_cold int encode_end(AVCodecContext *avctx)
593 HYuvContext *s = avctx->priv_data;
595 ff_huffyuv_common_end(s);
597 av_freep(&avctx->extradata);
598 av_freep(&avctx->stats_out);
600 return 0;
603 #if CONFIG_HUFFYUV_ENCODER
604 AVCodec ff_huffyuv_encoder = {
605 .name = "huffyuv",
606 .type = AVMEDIA_TYPE_VIDEO,
607 .id = AV_CODEC_ID_HUFFYUV,
608 .priv_data_size = sizeof(HYuvContext),
609 .init = encode_init,
610 .encode2 = encode_frame,
611 .close = encode_end,
612 .pix_fmts = (const enum AVPixelFormat[]){
613 AV_PIX_FMT_YUV422P, AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
615 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
617 #endif
619 #if CONFIG_FFVHUFF_ENCODER
620 AVCodec ff_ffvhuff_encoder = {
621 .name = "ffvhuff",
622 .type = AVMEDIA_TYPE_VIDEO,
623 .id = AV_CODEC_ID_FFVHUFF,
624 .priv_data_size = sizeof(HYuvContext),
625 .init = encode_init,
626 .encode2 = encode_frame,
627 .close = encode_end,
628 .pix_fmts = (const enum AVPixelFormat[]){
629 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
631 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
633 #endif