2 * Copyright (c) 2002-2003 Michael Niedermayer <michaelni@gmx.at>
4 * see http://www.pcisys.net/~melanson/codecs/huffyuv.txt for a description of
7 * This file is part of FFmpeg.
9 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 static inline int sub_left_prediction(HYuvContext *s, uint8_t *dst,
36 const uint8_t *src, int w, int left)
40 for (i = 0; i < w; i++) {
41 const int temp = src[i];
47 for (i = 0; i < 16; i++) {
48 const int temp = src[i];
52 s->dsp.diff_bytes(dst + 16, src + 16, src + 15, w - 16);
57 static inline void sub_left_prediction_bgr32(HYuvContext *s, uint8_t *dst,
58 const uint8_t *src, int w,
59 int *red, int *green, int *blue,
69 for (i = 0; i < FFMIN(w, 4); i++) {
70 const int rt = src[i * 4 + R];
71 const int gt = src[i * 4 + G];
72 const int bt = src[i * 4 + B];
73 const int at = src[i * 4 + A];
74 dst[i * 4 + R] = rt - r;
75 dst[i * 4 + G] = gt - g;
76 dst[i * 4 + B] = bt - b;
77 dst[i * 4 + A] = at - a;
84 s->dsp.diff_bytes(dst + 16, src + 16, src + 12, w * 4 - 16);
86 *red = src[(w - 1) * 4 + R];
87 *green = src[(w - 1) * 4 + G];
88 *blue = src[(w - 1) * 4 + B];
89 *alpha = src[(w - 1) * 4 + A];
92 static inline void sub_left_prediction_rgb24(HYuvContext *s, uint8_t *dst,
94 int *red, int *green, int *blue)
101 for (i = 0; i < FFMIN(w, 16); i++) {
102 const int rt = src[i * 3 + 0];
103 const int gt = src[i * 3 + 1];
104 const int bt = src[i * 3 + 2];
105 dst[i * 3 + 0] = rt - r;
106 dst[i * 3 + 1] = gt - g;
107 dst[i * 3 + 2] = bt - b;
113 s->dsp.diff_bytes(dst + 48, src + 48, src + 48 - 3, w * 3 - 48);
115 *red = src[(w - 1) * 3 + 0];
116 *green = src[(w - 1) * 3 + 1];
117 *blue = src[(w - 1) * 3 + 2];
120 static int store_table(HYuvContext *s, const uint8_t *len, uint8_t *buf)
125 for (i = 0; i < 256;) {
129 for (; i < 256 && len[i] == val && repeat < 255; i++)
132 av_assert0(val < 32 && val >0 && repeat<256 && repeat>0);
135 buf[index++] = repeat;
137 buf[index++] = val | (repeat << 5);
144 static av_cold int encode_init(AVCodecContext *avctx)
146 HYuvContext *s = avctx->priv_data;
149 ff_huffyuv_common_init(avctx);
151 avctx->extradata = av_mallocz(1024*30); // 256*3+4 == 772
152 avctx->stats_out = av_mallocz(1024*30); // 21*256*3(%llu ) + 3(\n) + 1(0) = 16132
153 if (!avctx->extradata || !avctx->stats_out) {
154 av_freep(&avctx->stats_out);
155 return AVERROR(ENOMEM);
159 avctx->coded_frame = &s->picture;
161 switch (avctx->pix_fmt) {
162 case AV_PIX_FMT_YUV420P:
163 case AV_PIX_FMT_YUV422P:
165 av_log(avctx, AV_LOG_ERROR, "Width must be even for this colorspace.\n");
166 return AVERROR(EINVAL);
168 s->bitstream_bpp = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 12 : 16;
170 case AV_PIX_FMT_RGB32:
171 s->bitstream_bpp = 32;
173 case AV_PIX_FMT_RGB24:
174 s->bitstream_bpp = 24;
177 av_log(avctx, AV_LOG_ERROR, "format not supported\n");
178 return AVERROR(EINVAL);
180 avctx->bits_per_coded_sample = s->bitstream_bpp;
181 s->decorrelate = s->bitstream_bpp >= 24;
182 s->predictor = avctx->prediction_method;
183 s->interlaced = avctx->flags&CODEC_FLAG_INTERLACED_ME ? 1 : 0;
184 if (avctx->context_model == 1) {
185 s->context = avctx->context_model;
186 if (s->flags & (CODEC_FLAG_PASS1|CODEC_FLAG_PASS2)) {
187 av_log(avctx, AV_LOG_ERROR,
188 "context=1 is not compatible with "
189 "2 pass huffyuv encoding\n");
190 return AVERROR(EINVAL);
194 if (avctx->codec->id == AV_CODEC_ID_HUFFYUV) {
195 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
196 av_log(avctx, AV_LOG_ERROR,
197 "Error: YV12 is not supported by huffyuv; use "
198 "vcodec=ffvhuff or format=422p\n");
199 return AVERROR(EINVAL);
201 if (avctx->context_model) {
202 av_log(avctx, AV_LOG_ERROR,
203 "Error: per-frame huffman tables are not supported "
204 "by huffyuv; use vcodec=ffvhuff\n");
205 return AVERROR(EINVAL);
207 if (s->interlaced != ( s->height > 288 ))
208 av_log(avctx, AV_LOG_INFO,
209 "using huffyuv 2.2.0 or newer interlacing flag\n");
212 if (s->bitstream_bpp >= 24 && s->predictor == MEDIAN) {
213 av_log(avctx, AV_LOG_ERROR,
214 "Error: RGB is incompatible with median predictor\n");
215 return AVERROR(EINVAL);
218 ((uint8_t*)avctx->extradata)[0] = s->predictor | (s->decorrelate << 6);
219 ((uint8_t*)avctx->extradata)[1] = s->bitstream_bpp;
220 ((uint8_t*)avctx->extradata)[2] = s->interlaced ? 0x10 : 0x20;
222 ((uint8_t*)avctx->extradata)[2] |= 0x40;
223 ((uint8_t*)avctx->extradata)[3] = 0;
224 s->avctx->extradata_size = 4;
226 if (avctx->stats_in) {
227 char *p = avctx->stats_in;
229 for (i = 0; i < 3; i++)
230 for (j = 0; j < 256; j++)
234 for (i = 0; i < 3; i++) {
237 for (j = 0; j < 256; j++) {
238 s->stats[i][j] += strtol(p, &next, 0);
239 if (next == p) return -1;
243 if (p[0] == 0 || p[1] == 0 || p[2] == 0) break;
246 for (i = 0; i < 3; i++)
247 for (j = 0; j < 256; j++) {
248 int d = FFMIN(j, 256 - j);
250 s->stats[i][j] = 100000000 / (d + 1);
254 for (i = 0; i < 3; i++) {
255 ff_huff_gen_len_table(s->len[i], s->stats[i]);
257 if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0) {
261 s->avctx->extradata_size +=
262 store_table(s, s->len[i], &((uint8_t*)s->avctx->extradata)[s->avctx->extradata_size]);
266 for (i = 0; i < 3; i++) {
267 int pels = s->width * s->height / (i ? 40 : 10);
268 for (j = 0; j < 256; j++) {
269 int d = FFMIN(j, 256 - j);
270 s->stats[i][j] = pels/(d + 1);
274 for (i = 0; i < 3; i++)
275 for (j = 0; j < 256; j++)
279 if (ff_huffyuv_alloc_temp(s)) {
280 ff_huffyuv_common_end(s);
281 return AVERROR(ENOMEM);
288 static int encode_422_bitstream(HYuvContext *s, int offset, int count)
291 const uint8_t *y = s->temp[0] + offset;
292 const uint8_t *u = s->temp[1] + offset / 2;
293 const uint8_t *v = s->temp[2] + offset / 2;
295 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 2 * 4 * count) {
296 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
302 int y1 = y[2 * i + 1];\
308 if (s->flags & CODEC_FLAG_PASS1) {
309 for(i = 0; i < count; i++) {
317 if (s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)
320 for (i = 0; i < count; i++) {
323 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
325 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
327 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
329 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
332 for(i = 0; i < count; i++) {
334 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);
335 put_bits(&s->pb, s->len[1][u0], s->bits[1][u0]);
336 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
337 put_bits(&s->pb, s->len[2][v0], s->bits[2][v0]);
343 static int encode_gray_bitstream(HYuvContext *s, int count)
347 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) < 4 * count) {
348 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
353 int y0 = s->temp[0][2 * i];\
354 int y1 = s->temp[0][2 * i + 1];
359 put_bits(&s->pb, s->len[0][y0], s->bits[0][y0]);\
360 put_bits(&s->pb, s->len[0][y1], s->bits[0][y1]);
364 if (s->flags & CODEC_FLAG_PASS1) {
365 for (i = 0; i < count; i++) {
370 if (s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)
374 for (i = 0; i < count; i++) {
380 for (i = 0; i < count; i++) {
388 static inline int encode_bgra_bitstream(HYuvContext *s, int count, int planes)
392 if (s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb) >> 3) <
393 4 * planes * count) {
394 av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
399 int g = s->temp[0][planes == 3 ? 3 * i + 1 : 4 * i + G]; \
400 int b =(s->temp[0][planes == 3 ? 3 * i + 2 : 4 * i + B] - g) & 0xFF;\
401 int r =(s->temp[0][planes == 3 ? 3 * i + 0 : 4 * i + R] - g) & 0xFF;\
402 int a = s->temp[0][planes * i + A];
412 put_bits(&s->pb, s->len[1][g], s->bits[1][g]); \
413 put_bits(&s->pb, s->len[0][b], s->bits[0][b]); \
414 put_bits(&s->pb, s->len[2][r], s->bits[2][r]); \
416 put_bits(&s->pb, s->len[2][a], s->bits[2][a]);
418 if ((s->flags & CODEC_FLAG_PASS1) &&
419 (s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)) {
420 for (i = 0; i < count; i++) {
424 } else if (s->context || (s->flags & CODEC_FLAG_PASS1)) {
425 for (i = 0; i < count; i++) {
431 for (i = 0; i < count; i++) {
439 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
440 const AVFrame *pict, int *got_packet)
442 HYuvContext *s = avctx->priv_data;
443 const int width = s->width;
444 const int width2 = s->width>>1;
445 const int height = s->height;
446 const int fake_ystride = s->interlaced ? pict->linesize[0]*2 : pict->linesize[0];
447 const int fake_ustride = s->interlaced ? pict->linesize[1]*2 : pict->linesize[1];
448 const int fake_vstride = s->interlaced ? pict->linesize[2]*2 : pict->linesize[2];
449 AVFrame * const p = &s->picture;
450 int i, j, size = 0, ret;
452 if ((ret = ff_alloc_packet2(avctx, pkt, width * height * 3 * 4 + FF_MIN_BUFFER_SIZE)) < 0)
456 p->pict_type = AV_PICTURE_TYPE_I;
460 for (i = 0; i < 3; i++) {
461 ff_huff_gen_len_table(s->len[i], s->stats[i]);
462 if (ff_huffyuv_generate_bits_table(s->bits[i], s->len[i]) < 0)
464 size += store_table(s, s->len[i], &pkt->data[size]);
467 for (i = 0; i < 3; i++)
468 for (j = 0; j < 256; j++)
469 s->stats[i][j] >>= 1;
472 init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
474 if (avctx->pix_fmt == AV_PIX_FMT_YUV422P ||
475 avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
476 int lefty, leftu, leftv, y, cy;
478 put_bits(&s->pb, 8, leftv = p->data[2][0]);
479 put_bits(&s->pb, 8, lefty = p->data[0][1]);
480 put_bits(&s->pb, 8, leftu = p->data[1][0]);
481 put_bits(&s->pb, 8, p->data[0][0]);
483 lefty = sub_left_prediction(s, s->temp[0], p->data[0], width , 0);
484 leftu = sub_left_prediction(s, s->temp[1], p->data[1], width2, 0);
485 leftv = sub_left_prediction(s, s->temp[2], p->data[2], width2, 0);
487 encode_422_bitstream(s, 2, width-2);
489 if (s->predictor==MEDIAN) {
490 int lefttopy, lefttopu, lefttopv;
493 lefty = sub_left_prediction(s, s->temp[0], p->data[0] + p->linesize[0], width , lefty);
494 leftu = sub_left_prediction(s, s->temp[1], p->data[1] + p->linesize[1], width2, leftu);
495 leftv = sub_left_prediction(s, s->temp[2], p->data[2] + p->linesize[2], width2, leftv);
497 encode_422_bitstream(s, 0, width);
501 lefty = sub_left_prediction(s, s->temp[0], p->data[0] + fake_ystride, 4, lefty);
502 leftu = sub_left_prediction(s, s->temp[1], p->data[1] + fake_ustride, 2, leftu);
503 leftv = sub_left_prediction(s, s->temp[2], p->data[2] + fake_vstride, 2, leftv);
505 encode_422_bitstream(s, 0, 4);
507 lefttopy = p->data[0][3];
508 lefttopu = p->data[1][1];
509 lefttopv = p->data[2][1];
510 s->dsp.sub_hfyu_median_prediction(s->temp[0], p->data[0]+4, p->data[0] + fake_ystride + 4, width - 4 , &lefty, &lefttopy);
511 s->dsp.sub_hfyu_median_prediction(s->temp[1], p->data[1]+2, p->data[1] + fake_ustride + 2, width2 - 2, &leftu, &lefttopu);
512 s->dsp.sub_hfyu_median_prediction(s->temp[2], p->data[2]+2, p->data[2] + fake_vstride + 2, width2 - 2, &leftv, &lefttopv);
513 encode_422_bitstream(s, 0, width - 4);
516 for (; y < height; y++,cy++) {
517 uint8_t *ydst, *udst, *vdst;
519 if (s->bitstream_bpp == 12) {
521 ydst = p->data[0] + p->linesize[0] * y;
522 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
523 encode_gray_bitstream(s, width);
526 if (y >= height) break;
528 ydst = p->data[0] + p->linesize[0] * y;
529 udst = p->data[1] + p->linesize[1] * cy;
530 vdst = p->data[2] + p->linesize[2] * cy;
532 s->dsp.sub_hfyu_median_prediction(s->temp[0], ydst - fake_ystride, ydst, width , &lefty, &lefttopy);
533 s->dsp.sub_hfyu_median_prediction(s->temp[1], udst - fake_ustride, udst, width2, &leftu, &lefttopu);
534 s->dsp.sub_hfyu_median_prediction(s->temp[2], vdst - fake_vstride, vdst, width2, &leftv, &lefttopv);
536 encode_422_bitstream(s, 0, width);
539 for (cy = y = 1; y < height; y++, cy++) {
540 uint8_t *ydst, *udst, *vdst;
542 /* encode a luma only line & y++ */
543 if (s->bitstream_bpp == 12) {
544 ydst = p->data[0] + p->linesize[0] * y;
546 if (s->predictor == PLANE && s->interlaced < y) {
547 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
549 lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
551 lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
553 encode_gray_bitstream(s, width);
555 if (y >= height) break;
558 ydst = p->data[0] + p->linesize[0] * y;
559 udst = p->data[1] + p->linesize[1] * cy;
560 vdst = p->data[2] + p->linesize[2] * cy;
562 if (s->predictor == PLANE && s->interlaced < cy) {
563 s->dsp.diff_bytes(s->temp[1], ydst, ydst - fake_ystride, width);
564 s->dsp.diff_bytes(s->temp[2], udst, udst - fake_ustride, width2);
565 s->dsp.diff_bytes(s->temp[2] + width2, vdst, vdst - fake_vstride, width2);
567 lefty = sub_left_prediction(s, s->temp[0], s->temp[1], width , lefty);
568 leftu = sub_left_prediction(s, s->temp[1], s->temp[2], width2, leftu);
569 leftv = sub_left_prediction(s, s->temp[2], s->temp[2] + width2, width2, leftv);
571 lefty = sub_left_prediction(s, s->temp[0], ydst, width , lefty);
572 leftu = sub_left_prediction(s, s->temp[1], udst, width2, leftu);
573 leftv = sub_left_prediction(s, s->temp[2], vdst, width2, leftv);
576 encode_422_bitstream(s, 0, width);
579 } else if(avctx->pix_fmt == AV_PIX_FMT_RGB32) {
580 uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
581 const int stride = -p->linesize[0];
582 const int fake_stride = -fake_ystride;
584 int leftr, leftg, leftb, lefta;
586 put_bits(&s->pb, 8, lefta = data[A]);
587 put_bits(&s->pb, 8, leftr = data[R]);
588 put_bits(&s->pb, 8, leftg = data[G]);
589 put_bits(&s->pb, 8, leftb = data[B]);
591 sub_left_prediction_bgr32(s, s->temp[0], data + 4, width - 1,
592 &leftr, &leftg, &leftb, &lefta);
593 encode_bgra_bitstream(s, width - 1, 4);
595 for (y = 1; y < s->height; y++) {
596 uint8_t *dst = data + y*stride;
597 if (s->predictor == PLANE && s->interlaced < y) {
598 s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride, width * 4);
599 sub_left_prediction_bgr32(s, s->temp[0], s->temp[1], width,
600 &leftr, &leftg, &leftb, &lefta);
602 sub_left_prediction_bgr32(s, s->temp[0], dst, width,
603 &leftr, &leftg, &leftb, &lefta);
605 encode_bgra_bitstream(s, width, 4);
607 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
608 uint8_t *data = p->data[0] + (height - 1) * p->linesize[0];
609 const int stride = -p->linesize[0];
610 const int fake_stride = -fake_ystride;
612 int leftr, leftg, leftb;
614 put_bits(&s->pb, 8, leftr = data[0]);
615 put_bits(&s->pb, 8, leftg = data[1]);
616 put_bits(&s->pb, 8, leftb = data[2]);
617 put_bits(&s->pb, 8, 0);
619 sub_left_prediction_rgb24(s, s->temp[0], data + 3, width - 1,
620 &leftr, &leftg, &leftb);
621 encode_bgra_bitstream(s, width-1, 3);
623 for (y = 1; y < s->height; y++) {
624 uint8_t *dst = data + y * stride;
625 if (s->predictor == PLANE && s->interlaced < y) {
626 s->dsp.diff_bytes(s->temp[1], dst, dst - fake_stride,
628 sub_left_prediction_rgb24(s, s->temp[0], s->temp[1], width,
629 &leftr, &leftg, &leftb);
631 sub_left_prediction_rgb24(s, s->temp[0], dst, width,
632 &leftr, &leftg, &leftb);
634 encode_bgra_bitstream(s, width, 3);
637 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
641 size += (put_bits_count(&s->pb) + 31) / 8;
642 put_bits(&s->pb, 16, 0);
643 put_bits(&s->pb, 15, 0);
646 if ((s->flags&CODEC_FLAG_PASS1) && (s->picture_number & 31) == 0) {
648 char *p = avctx->stats_out;
649 char *end = p + 1024*30;
650 for (i = 0; i < 3; i++) {
651 for (j = 0; j < 256; j++) {
652 snprintf(p, end-p, "%"PRIu64" ", s->stats[i][j]);
656 snprintf(p, end-p, "\n");
660 avctx->stats_out[0] = '\0';
661 if (!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)) {
662 flush_put_bits(&s->pb);
663 s->dsp.bswap_buf((uint32_t*)pkt->data, (uint32_t*)pkt->data, size);
668 pkt->size = size * 4;
669 pkt->flags |= AV_PKT_FLAG_KEY;
675 static av_cold int encode_end(AVCodecContext *avctx)
677 HYuvContext *s = avctx->priv_data;
679 ff_huffyuv_common_end(s);
681 av_freep(&avctx->extradata);
682 av_freep(&avctx->stats_out);
687 #if CONFIG_HUFFYUV_ENCODER
688 AVCodec ff_huffyuv_encoder = {
690 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
691 .type = AVMEDIA_TYPE_VIDEO,
692 .id = AV_CODEC_ID_HUFFYUV,
693 .priv_data_size = sizeof(HYuvContext),
695 .encode2 = encode_frame,
697 .pix_fmts = (const enum AVPixelFormat[]){
698 AV_PIX_FMT_YUV422P, AV_PIX_FMT_RGB24,
699 AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
704 #if CONFIG_FFVHUFF_ENCODER
705 AVCodec ff_ffvhuff_encoder = {
707 .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
708 .type = AVMEDIA_TYPE_VIDEO,
709 .id = AV_CODEC_ID_FFVHUFF,
710 .priv_data_size = sizeof(HYuvContext),
712 .encode2 = encode_frame,
714 .pix_fmts = (const enum AVPixelFormat[]){
715 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_RGB24,
716 AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE